'Pixel format bit endianness
I am currently implementing a program to read and decode image from a BMP file. But I am really confused about the pixel format and especially about bit endianness.
My current understanding is when referring to a pixel format, e.g. RGBA32, we are saying it in our human way, which is using big endian, thus the first byte is R, second G, third B, fourth A:
RGBA32
11111111 22222222 33333333 44444444
R G B A
And when appending big/little endian to this format, such as RGBA32BE/RGBA32LE, this will change the byte order accordingly, as:
RGBA32BE (unchanged)
11111111 22222222 33333333 44444444
R G B A
RGBA32LE (reversed)
11111111 22222222 33333333 44444444
A B G R
This all looks naturally to me, as long as we are assuming the individual value of each component is exactly the value of byte we read.
However things start to confuse me when component size is less than 1 byte or 8 bits. Say RGB555BE, I suppose below is how it is represented as byte array:
RGB555BE
1'22222'33 333'44444
A R G B
0'00001'00 000'00000
Do we read the R component as
10000=16or00001=1?I'm even more confused about if the way we read the component (bit endianness) is related to byte endianness?
Which way is RGB555LE format represented in byte array?
RGB555LE_v1 333'44444 1'22222'33 G" B A R G' 000'00000 0'00001'00 or RGB555LE_v2 ? 44444'333 33'22222'1 B G R A 00000'000 00'10000'0
Solution 1:[1]
I don't think endianness matters to rgb555, and that's why your link bunches rgb555, rgb555le, and rgb555be together. For that matter, your example with rgba also isn't endian sensitive as its components are all <= 8 bits.
As for how rgb555 is represented in 2 bytes (well, 15 bits), you can search FFmpeg repo for rgb555 and see how encoders/decoders handle such pixel format. Here is one I found rpzaenc.c Line 138:
static uint16_t rgb24_to_rgb555(uint8_t *rgb24)
{
uint16_t rgb555 = 0;
uint32_t r, g, b;
r = rgb24[0] >> 3;
g = rgb24[1] >> 3;
b = rgb24[2] >> 3;
rgb555 |= (r << 10);
rgb555 |= (g << 5);
rgb555 |= (b << 0);
return rgb555;
}
the encoder takes rgb555, a 16-bit unsigned int, an uses put_bits() utility function to push these 15 bits to its bitstream as shown on Line 683
put_bits(&s->pb, 16, rgb24_to_rgb555(avg_color));
Here is the link to put_bits.h
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
