public static ImageData invertImageColors(ImageData imageData) { if (!imageData.palette.isDirect) { int width = imageData.width; int height = imageData.height; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { imageData.setPixel(i, j, 255 - imageData.getPixel(i, j)); } } return imageData; } else { /* * My basic algorithm here is to convert to AWT buffered image, re-pack that * into a BufferedImage.TYPE_INT_RGB buffered image, and then convert that * back to an SWT image */ BufferedImage awtImage = convertToAWT(imageData); BufferedImage destImage = new BufferedImage(awtImage.getWidth(), awtImage.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D awtGraphicsContext = destImage.createGraphics(); awtGraphicsContext.drawImage(awtImage, null, 0, 0); awtGraphicsContext.dispose(); short[] invert = new short[256]; for (int i = 0; i < invert.length; i++) invert[i] = (short) (255 - i); BufferedImageOp invertOp = new LookupOp(new ShortLookupTable(0, invert), null); destImage = invertOp.filter(destImage, null); return convertToSWT(destImage); } }
And the code to convert between an SWT image and AWT BufferedImage. Note, I found this code on the interwebs
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; }
This didn't work for me. Everything looks mostly gray both before and after. I'm color blind but I don't think that has anything to do with it.
ReplyDelete