Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff8cec3

Browse files
committed
ColorConverter was inaccurate when converting rgb555 (and bgr555) colorspace to rgb888. For details look at: https://stackoverflow.com/questions/71106056/what-is-the-correct-way-to-convert-rgb555-to-rgb888. I only tested rgb555 and not the bgr555 code path!
1 parent bd572f8 commit ff8cec3

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

shared-module/displayio/ColorConverter.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,12 @@ uint32_t displayio_colorconverter_convert_pixel(displayio_colorspace_t colorspac
197197
pixel = __builtin_bswap16(pixel);
198198
MP_FALLTHROUGH;
199199
case DISPLAYIO_COLORSPACE_RGB555: {
200-
uint32_t r8 = (pixel >> 10) << 3;
201-
uint32_t g8 = ((pixel >> 5) << 3) & 0xff;
202-
uint32_t b8 = (pixel << 3) & 0xff;
200+
uint32_t r8 = (pixel >> 10) & 0x1f;
201+
uint32_t g8 = (pixel >> 5) & 0x1f;
202+
uint32_t b8 = pixel & 0x1f;
203+
r8 = (r8 << 3) | ((r8 >> 2) & 0b111);
204+
g8 = (g8 << 3) | ((g8 >> 2) & 0b111);
205+
b8 = (b8 << 3) | ((b8 >> 2) & 0b111);
203206
pixel = (r8 << 16) | (g8 << 8) | b8;
204207
}
205208
break;
@@ -219,9 +222,12 @@ uint32_t displayio_colorconverter_convert_pixel(displayio_colorspace_t colorspac
219222
pixel = __builtin_bswap16(pixel);
220223
MP_FALLTHROUGH;
221224
case DISPLAYIO_COLORSPACE_BGR555: {
222-
uint32_t b8 = (pixel >> 10) << 3;
223-
uint32_t g8 = ((pixel >> 5) << 3) & 0xff;
224-
uint32_t r8 = (pixel << 3) & 0xff;
225+
uint32_t b8 = (pixel >> 10) & 0x1f;
226+
uint32_t g8 = (pixel >> 5) & 0x1f;
227+
uint32_t r8 = pixel & 0x1f;
228+
r8 = (r8 << 3) | ((r8 >> 2) & 0b111);
229+
g8 = (g8 << 3) | ((g8 >> 2) & 0b111);
230+
b8 = (b8 << 3) | ((b8 >> 2) & 0b111);
225231
pixel = (r8 << 16) | (g8 << 8) | b8;
226232
}
227233
break;

0 commit comments

Comments
 (0)