I was trying to convert a BufferedImage
's byte[]
from 32-bit RGBA to 24-bit RGB. According to this answer the fastest way to get the byte[]
from the image is:
byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
So I iterate over all bytes assuming their order is R G B A and for every 4 bytes, I write the first 3 in an output byte[] (i.e. ignoring the alpha value).
This works fine when run from Eclipse and the bytes are converted correctly. However when I run the same program from the command line the same bytes are returned with the opposite byte order!
The test image I use for my test is a 5x5 black image where only its top-left corner is different having the RGBA color [aa cc ee ff]
:
And a zoomed-in version for conveniency:
My folder structure is:
- src/
- test.png
- test/
- TestBufferedImage.java
The SSCCE is the following:
package test;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class TestBufferedImage {
private static void log(String s) {
System.out.println(s);
}
private static String toByteString(byte b) {
// Perform a bitwise AND for convenience while printing.
// Otherwise Integer.toHexString() interprets values as integers and a negative byte 0xFF will be printed as "ffffffff"
return Integer.toHexString(b & 0xFF);
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
InputStream stream = TestBufferedImage.class.getClassLoader().getResourceAsStream("test.png");
BufferedImage image = ImageIO.read(stream);
stream.close();
log("Image loaded succesfully, width=" + image.getWidth() + " height=" + image.getHeight());
log("Converting from 32-bit to 24-bit...");
DataBufferByte buffer = (DataBufferByte)image.getRaster().getDataBuffer();
byte[] input = buffer.getData();
byte[] output = convertTo24Bit(input);
log("Converted total of " + input.length + " bytes to " + output.length + " bytes");
}
private static byte[] convertTo24Bit(byte[] input) {
int dataLength = input.length;
byte[] convertedData = new byte[ dataLength * 3 / 4 ];
for (int i = 0, j = 0; i < dataLength; i+=4, j+=3) {
convertIntByteToByte(input, i, convertedData, j);
}
return convertedData;
}
private static void convertIntByteToByte(byte[] src, int srcIndex, byte[] out, int outIndex) {
byte r = src[srcIndex];
byte g = src[srcIndex+1];
byte b = src[srcIndex+2];
byte a = src[srcIndex+3];
out[outIndex] = r;
out[outIndex+1] = g;
out[outIndex+2] = b;
log("i=" + srcIndex
+ " Converting [" + toByteString(r) + ", " + toByteString(g)
+ ", " + toByteString(b) + ", " + toByteString(a) + "] --> ["
+ toByteString(out[outIndex]) + ", " + toByteString(out[outIndex+1])
+ ", " + toByteString(out[outIndex+2]) + "]"
);
}
}
Output when run from Eclipse (Version: Juno Service Release 2 Build id: 20130225-0426):
Image loaded succesfully, width=5 height=5
Converting from 32-bit to 24-bit...
i=0 Converting [aa, cc, ee, ff] --> [aa, cc, ee] // <-- Bytes have the correct order
i=4 Converting [0, 0, 0, ff] --> [0, 0, 0]
i=8 Converting [0, 0, 0, ff] --> [0, 0, 0]
.....
i=96 Converting [0, 0, 0, ff] --> [0, 0, 0]
Converted total of 100 bytes to 75 bytes
Output when run from command line (Windows Vista) with java test.TestBufferedImage
:
Image loaded succesfully, width=5 height=5
Converting from 32-bit to 24-bit...
i=0 Converting [ff, ee, cc, aa] --> [ff, ee, cc] // <-- Bytes are returned with a different byte order!
i=4 Converting [ff, 0, 0, 0] --> [ff, 0, 0]
i=8 Converting [ff, 0, 0, 0] --> [ff, 0, 0]
.....
i=96 Converting [ff, 0, 0, 0] --> [ff, 0, 0]
Converted total of 100 bytes to 75 bytes
So has anyone encountered a similar issue and/or can explain what is actually going on? Why the byte order is different when running from inside Eclipse?