Skip to content

Commit f224ed1

Browse files
committed
OnDiskBitmap: Correct handling of "0 color palette" images
Microsoft documentation says: > If biCompression equals BI_RGB and the bitmap uses 8 bpp or less, the bitmap has a color table immediatelly following the BITMAPINFOHEADER structure. The color table consists of an array of RGBQUAD values. The size of the array is given by the biClrUsed member. If biClrUsed is zero, the array contains the maximum number of colors for the given bitdepth; that is, 2^biBitCount colors. Formerly, we treated 0 colors as "no image palette" during construction, but then during common_hal_displayio_ondiskbitmap_get_pixel indexed into the palette anyway. This could have unpredictable results. On a pygamer, it gave an image that was blue and black. On magtag, it gave a crash.
1 parent 28bd29e commit f224ed1

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

shared-module/displayio/OnDiskBitmap.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
5757
uint32_t compression = read_word(bmp_header, 15);
5858
uint32_t number_of_colors = read_word(bmp_header, 23);
5959

60-
bool indexed = ((bits_per_pixel <= 8) && (number_of_colors != 0));
60+
bool indexed = bits_per_pixel <= 8;
6161
self->bitfield_compressed = (compression == 3);
6262
self->bits_per_pixel = bits_per_pixel;
6363
self->width = read_word(bmp_header, 9);
@@ -75,6 +75,9 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
7575
self->b_bitmask = 0x1f;
7676
}
7777
} else if (indexed && self->bits_per_pixel != 1) {
78+
if (number_of_colors == 0) {
79+
number_of_colors = 1 << bits_per_pixel;
80+
}
7881
uint16_t palette_size = number_of_colors * sizeof(uint32_t);
7982
uint16_t palette_offset = 0xe + header_size;
8083

0 commit comments

Comments
 (0)