| 格式 | 每像素位元 | R | G | B | 記憶體排列 |
|---|---|---|---|---|---|
| RGB565 | 16-bit (2 bytes) | 5-bit | 6-bit | 5-bit | Byte0: RRRRRGGG, Byte1: GGGBBBBB |
| RGB888 | 24-bit (3 bytes) | 8-bit | 8-bit | 8-bit | Byte0: R, Byte1: G, Byte2: B |
Bit: 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0
R4 R3 R2 R1 R0 G5 G4 G3 | G2 G1 G0 B4 B3 B2 B1 B0
Byte1 (高位址) Byte0 (低位址)
void RGB565_to_RGB24(unsigned char *source, unsigned char *convert, int width) {
for (int i = 0; i < width; i++) {
*convert++ = (*source) << 3; // B
*convert++ = ((*(source+1) & 0x07) << 5) | ((*source & 0xE0) >> 3); // G
*convert++ = *(source+1); // R
source += 2;
}
}
void RGB16toRGB24(unsigned char *rgb24, unsigned char *rgb16, int width, int height) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
unsigned short *dot = (unsigned short *)(rgb16 + (i * width + j) * 2);
*rgb24++ = ((*dot) & 0x1F) << 3; // R (5-bit → 8-bit)
*rgb24++ = ((*dot) & 0x7E0) >> 3; // G (6-bit → 8-bit)
*rgb24++ = ((*dot) & 0xF800) >> 8; // B (5-bit → 8-bit)
}
}
}
注意:方法二假設記憶體為 Little Endian,且 RGB565 排列為 R-G-B 依序對應高到低位元,需依實際平台驗證位元遮罩。
低位元擴展時,單純左移會造成精度遺失(如 5-bit 0x1F 左移 3 成 0xF8,非 0xFF)。量化補償策略:將原低位元複製填補低位,例如 5-bit 擴展 8-bit:(val << 3) | (val >> 2),使最大值 0x1F → 0xFF,視覺更均勻。