lucas.ss.05
lucas.ss.05

Reputation: 65

Getting a RGBA byte array from a BufferedImage Java

I have a BufferedImage and would like to get a byte array in the format R G B A (one channel per byte). How can I do this?

Upvotes: 1

Views: 2042

Answers (3)

Harald K
Harald K

Reputation: 27054

The easy way is to use BufferedImage.getRGB (which despite its name gives you ARGB values), and convert the packed int[] to byte[] four times as long. Input can be any file ImageIO can read, a PNG will work fine.

public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File(args[0]));

    int[] argb = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
    byte[] rgba = intARGBtoByteRGBA(argb);
}

private static byte[] intARGBtoByteRGBA(int[] argb) {
    byte[] rgba = new byte[argb.length * 4];

    for (int i = 0; i < argb.length; i++) {
        rgba[4 * i    ] = (byte) ((argb[i] >> 16) & 0xff); // R
        rgba[4 * i + 1] = (byte) ((argb[i] >>  8) & 0xff); // G
        rgba[4 * i + 2] = (byte) ((argb[i]      ) & 0xff); // B
        rgba[4 * i + 3] = (byte) ((argb[i] >> 24) & 0xff); // A
    }

    return rgba;
}

A slightly more fun way, is to create a BufferedImage that is backed by a byte[] already in RGBA format, like this:

public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File(args[0]));

    ComponentColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
    WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), image.getWidth() * 4, 4, new int[] {0, 1, 2, 3}, null); // R, G, B, A order
    BufferedImage imageToo = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);

    // This array will be in the same R, G, B, A order
    byte[] rgbaToo = ((DataBufferByte) raster.getDataBuffer()).getData();

    // Draw the image onto the RGBA buffer, which will be updated immediately
    Graphics2D g = imageToo.createGraphics();
    try {
        g.setComposite(AlphaComposite.Src);
        g.drawImage(image, 0, 0, null);
    }
    finally {
        g.dispose();
    }
}

Which one of the above examples is better to use, depends on the use case.

  • If you just need a one time conversion, the first one is probably easier to reason about and works just fine.

  • If you need to update the buffer many times over, the second approach might yield better performance.

PS: I get the exact same results using both alternatives for all my test inputs, except the ones where the original is in grayscale (using ColorSpace.CS_GRAY). I believe this is a known issue that has troubled Java2D users for ages...

Upvotes: 4

lucas.ss.05
lucas.ss.05

Reputation: 65

Eventually I just used the java library PNGJ to load it, and loaded each row accordingly in RGBA format:

for(int j = 0; j < reader.imgInfo.rows;j++) {
        IImageLine row = reader.readRow();
        for(int b = 0; b < reader.imgInfo.cols; b++) { 
                int argb = ch == 3 ? ImageLineHelper.getPixelRGB8(row, b) : ch == 4 ? ImageLineHelper.getPixelARGB8(row, b) : 0xFF000000;

Which I then shifted into RGBA:

pixels.write((argb & 0x00FF0000) >> 16);
pixels.write((argb & 0x0000FF00) >> 8 );
pixels.write((argb & 0x000000FF) >> 0 );//neatness lol
pixels.write((argb & 0xFF000000) >> 24);

Upvotes: 0

nobody
nobody

Reputation: 1

Start with BufferedImage#getRGB which returns ARGB values no matter the ColorModel of the imgage - check its documentation.

Then use the default instance of ColorModel to either get the components (getRed(), getGreen(), ...) or to get an int array of the components (e.g. getComponents()). Or just split the value returned by getRGB(), format is described in ColorModel#getRGBdefault.

Eventually the image (or its raster data) can be converted to TYPE_4BYTE_ABGR, so the raster data can be used directly (just a guess, I never done it)

Upvotes: -1

Related Questions