Question

What in Java matches to C# PixelFormat's members.

i.e. Format24bppRgb matches to BufferedImage.TYPE_INT_RGB?

Here is my code. I got an image which has .Net's PixelFormat = Format32bppArgb. I am creating BufferedImage like this:

        int sizeBytes = width * height;
        DataBufferByte dataBuffer = new DataBufferByte(myImageBytes, sizeBytes);

        WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, // dataBuffer
                width, // width
                height, // height
                width * 4, // scanlineStride
                4, // pixelStride
                new int[]{0, 1, 2, 3}, // bandOffsets
                null); // location

        java.awt.image.ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), // ColorSpace
                new int[]{8, 8, 8, 8}, // bits
                true, // hasAlpha
                false, // isPreMultiplied
                ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);

        BufferedImage result = new BufferedImage(colorModel, raster, false, null);

After I create a bufferedImage red and blue colors are swapped in it.

Next, I tried to create an image as follow

        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        WritableRaster r = result.getRaster();
        int[] pixels = byteToInt(bytes);
        r.setPixels(0, 0, width, height , pixels); // ! Here an exception occures, because after I converted the byte array to int one the width becomes too long.

Byte array was convert by this method

private int[] byteToInt(byte[] pixels) {
    int[] ints = new int[pixels.length / 3];
    int byteIdx = 0;
    for (int pixel = 0; pixel < ints.length; pixel++) {
        int red = (int) pixels[byteIdx++] & 0xFF;
        int green = (int) pixels[byteIdx++] & 0xFF;
        int blue = (int) pixels[byteIdx++] & 0xFF;
        int rgb = (red << 16) | (green << 8) | blue;
        ints[pixel] = rgb;
    }
    return ints;
}

The colors look fine now, but I got the exception

java.lang.ArrayIndexOutOfBoundsException: 27600
at sun.awt.image.ByteInterleavedRaster.setPixels(ByteInterleavedRaster.java:1106)

If I use the smaller width (e.g. width / 3) the colors look fine but the picture itself shrinks.

Was it helpful?

Solution

BufferedImage is definately a good place to start. Many of the values in PixelFormat will match up to values in BufferedImage - they each have 24-bit and 32-bit RGB/ARGB values, both have 5-5-5 and 5-6-5 combinations, etc.

If you're having trouble, post some code and we'll have a look at it, try to help. The thing I would recommend would be to play around with the byte ordering (in the pixel ints) until you get the result that you expect - try drawing the BufferedImage onto a GUI object like JPanel so you can see what it looks like.

If you've got an array of int[] for your pixel values, this is the code I usually use for displaying the array as an image...

int[] pixels;
ColorModel model = new DirectColorModel(32,0x00ff0000,0x0000ff00,0x000000ff,0xff000000);
Image image = new JLabel().createImage(new MemoryImageSource(width,height,model,pixels,0,width));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top