Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Converting a BufferedImage which uses ComponentColorModel
Converting a BufferedImage which uses ComponentColorModel [message #760687] Sun, 04 December 2011 09:05 Go to next message
Alexey Romanov is currently offline Alexey RomanovFriend
Messages: 263
Registered: May 2010
Senior Member
Snippet 156 should convert AWT BufferedImage to SWT. However, it requires that the image use either DirectColorModel or IndexColorModel. There is a third subclass of ColorModel: ComponentColorModel. Does anybody know how to convert an image which uses it? I've found this unanswered thread from 2006, so I am not too hopeful Sad
Re: Converting a BufferedImage which uses ComponentColorModel [message #760847 is a reply to message #760687] Mon, 05 December 2011 12:39 Go to previous message
Alexey Romanov is currently offline Alexey RomanovFriend
Messages: 263
Registered: May 2010
Senior Member
I've found this implementation:
if (bufferedImage.getColorModel() instanceof ComponentColorModel) {
    ComponentColorModel colorModel = (ComponentColorModel)bufferedImage.getColorModel();

    //ASSUMES: 3 BYTE BGR IMAGE TYPE

    PaletteData palette = new PaletteData(0x0000FF, 0x00FF00,0xFF0000);
    ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);

    //This is valid because we are using a 3-byte Data model with no transparent pixels
    data.transparentPixel = -1;

    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;
Previous Topic:How to scroll in this composite with Page-Up/Down?
Next Topic:Unable to hide icon in dock for SWT bundled application
Goto Forum:
  


Current Time: Wed Apr 24 21:16:47 GMT 2024

Powered by FUDForum. Page generated in 0.03068 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top