Java – conversion from bufferedimage to SWT image

After careful consideration, I found some codes to convert buffered image into SWT image (I won't read it for now):

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,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(),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,pixelArray);
                data.setPixel(x,pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

(see: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/ConvertsabufferedimagetoSWTImageData.htm ).

I tested it and it works well The problem is that I don't understand it (my best guess is that it uses the original data interface of both for transmission) In my opinion, a simpler solution is to write bufferedimage to bytearrayoutputstream and then read it back to SWT image withbytearrayinputstream Is there a problem with this solution? How's the speed?

This conversion is necessary because all image resizing libraries are used for AWT, but I use SWT to display images

thank you!

Solution

The complexity of the code is mainly due to two possible color models of bufferedimage I don't think you can improve in this respect First, using an intermediate stream will require both image systems to have a common format, and the conversion to / from stream will certainly be slower than the current code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>