[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: swt decrease image quality

I found a solution by getting the depth of the current image and recreate them

/**
* Retrieve an image data with an 8 bit palette for an image. We assume that
* the image has less than 256 colours.
*
* @param imageData
* the imageData for the image.
* @return the new 8 bit imageData or null if the image has more than 256
* colours.
*/
private ImageData get8BitPaletteImageData(ImageData imageData) {
PaletteData palette = imageData.palette;
RGB colours[] = new RGB[256];
PaletteData newPaletteData = new PaletteData(colours);
ImageData newImageData = new ImageData(imageData.width,
imageData.height, 8, newPaletteData);


        int lastPixel = -1;
        int newPixel = -1;
        for (int i = 0; i < imageData.width; ++i) {
            for (int j = 0; j < imageData.height; ++j) {
                int pixel = imageData.getPixel(i, j);

                if (pixel != lastPixel) {
                    lastPixel = pixel;

                    RGB colour = palette.getRGB(pixel);
                    for (newPixel = 0; newPixel < 256; ++newPixel) {
                        if (colours[newPixel] == null) {
                            colours[newPixel] = colour;
                            break;
                        }
                        if (colours[newPixel].equals(colour))
                            break;
                    }

                    if (newPixel >= 256) {
                        /**
                         * Diagram has more than 256 colors, return null
                         */
                        return null;
                    }
                }

                newImageData.setPixel(i, j, newPixel);
            }
        }

        RGB colour = new RGB(0, 0, 0);
        for (int k = 0; k < 256; ++k) {
            if (colours[k] == null)
                colours[k] = colour;
        }

        return newImageData;
    }