I'm trying to load a byte array containing the data of a 24-bit bitmap file into a Bitmap object in Android but I'm having trouble since Bitmap only supports 32-bit and 16-bit formats. Here's what I came up with in order to convert the 24-bit data into 32-bit:
byte[] file = new byte[is.available()];
is.read(file);
is.close();
byte[] paddedFile = new byte[file.length + file.length / 3]; //we will be adding 1 more byte (alpha value) for each RGB triplet
for (int i = 0; i < file.length / 3; i++) {
paddedFile[i * 4] = file[i * 3];
paddedFile[i * 4 + 1] = file[i * 3 + 1];
paddedFile[i * 4 + 2] = file[i * 3 + 2];
paddedFile[i * 4 + 3] = -1; //manually added alpha channel
}
Bitmap screen = Bitmap.createBitmap(479, 616, Bitmap.Config.ARGB_8888); //exact dimensions of the 24-bit bitmap file
screen.copyPixelsFromBuffer(ByteBuffer.wrap(paddedFile));
iv.setImageBitmap(screen);
It kind of/almost works. Here's the bitmap I'm working with:
And here's what shows up after the code above:
Why is it distorted? Any leads on how to fix this problem is greatly appreciated.
If I'm right about the padding, you should be able to do this:
int w = 479;
int h = 616;
byte[] file = /* as before */;
// Convert interleaved byte RGB to packed int ARGB
int[] paddedFile = new int[file.length / 3];
for (int i = 0; i < file.length / 3; i++) {
paddedFile[i] = 0xff << 24 // Alpha (all opaque)
| ((file[i * 3] & 0xff) << 16)
| ((file[i * 3 + 1] & 0xff) << 8)
| ((file[i * 3 + 2] & 0xff))
}
int stride = w + (w % 4 == 0 ? 0 : 4 - (w % 4));
Bitmap screen = Bitmap.createBitmap(paddedFile, 0, stride, w, h, Bitmap.Config.ARGB_8888);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With