Results 1 to 2 of 2
  1. #1
    Jonah Bron's Avatar
    Jonah Bron is offline Member
    Join Date
    Sep 2010
    Location
    California
    Posts
    26
    Rep Power
    0

    Default Conversion from BufferedImage to SWT Image

    Hello, world!

    After much looking, I found a bit of code that converts a BufferedImage to a SWT Image:

    Java Code:
    public static ImageData convertToSWT(BufferedImage bufferedImage) {
    	if (bufferedImage.getColorModel() instanceof DirectColorModel) {
    		DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
    		PaletteData palette = new PaletteData(
    			colorModel.getRedMask(),
    			colorModel.getGreenMask(),
    			colorModel.getBlueMask()
    		);
    		ImageData data = new ImageData(
    			bufferedImage.getWidth(),
    			bufferedImage.getHeight(), colorModel.getPixelSize(),
    			palette
    		);
    		WritableRaster raster = bufferedImage.getRaster();
    		int[] pixelArray = new int[3];
    		for (int y = 0; y < data.height; y++) {
    			for (int x = 0; x < data.width; x++) {
    				raster.getPixel(x, y, pixelArray);
    				int pixel = palette.getPixel(
    					new RGB(pixelArray[0], pixelArray[1], pixelArray[2])
    				);
    				data.setPixel(x, y, pixel);
    			}
    		}
    		return data;
    	} else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
    		IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
    		int size = colorModel.getMapSize();
    		byte[] reds = new byte[size];
    		byte[] greens = new byte[size];
    		byte[] blues = new byte[size];
    		colorModel.getReds(reds);
    		colorModel.getGreens(greens);
    		colorModel.getBlues(blues);
    		RGB[] rgbs = new RGB[size];
    		for (int i = 0; i < rgbs.length; i++) {
    			rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
    		}
    		PaletteData palette = new PaletteData(rgbs);
    		ImageData data = new ImageData(
    			bufferedImage.getWidth(),
    			bufferedImage.getHeight(),
    			colorModel.getPixelSize(),
    			palette
    		);
    		data.transparentPixel = colorModel.getTransparentPixel();
    		WritableRaster raster = bufferedImage.getRaster();
    		int[] pixelArray = new int[1];
    		for (int y = 0; y < data.height; y++) {
    			for (int x = 0; x < data.width; x++) {
    				raster.getPixel(x, y, pixelArray);
    				data.setPixel(x, y, pixelArray[0]);
    			}
    		}
    		return data;
    	}
    	return null;
    }
    (found here: Converts a buffered image to SWT ImageData. : SWT Swing AWT**SWT JFace Eclipse**Java).

    I've tested it, and it works just fine. The problem is that I don't understand it (my best guess is that it uses the raw data interfaces of both to make the transfer). It occurred to me that a much simpler solution would be to write the BufferedImage out to ByteArrayOutputStream, and then read it back into a SWT Image with ByteArrayInputStream. Are there any problems with this solution? What about speed?

    This conversion is necessary because all of the image resizing libraries out there are for AWT, and yet I'm displaying the image with SWT.

    Thanks!

  2. #2
    Jonah Bron's Avatar
    Jonah Bron is offline Member
    Join Date
    Sep 2010
    Location
    California
    Posts
    26
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 01-27-2011, 09:14 AM
  2. Replies: 2
    Last Post: 10-04-2010, 03:50 PM
  3. Replies: 4
    Last Post: 03-23-2010, 04:46 PM
  4. read image without bufferedimage
    By mr_empty in forum Java 2D
    Replies: 5
    Last Post: 12-23-2009, 04:59 AM
  5. Replies: 0
    Last Post: 07-02-2008, 09:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •