Skip to content

Fix reading BMP grayscale palettes with a different bit depth - #10716

Open
vladosskins178 wants to merge 1 commit into
python-pillow:mainfrom
vladosskins178:fix/bmp-grayscale-palette-bits
Open

vladosskins178 wants to merge 1 commit into
python-pillow:mainfrom
vladosskins178:fix/bmp-grayscale-palette-bits

Conversation

@vladosskins178

Copy link
Copy Markdown
Contributor

Fixes #10711.

When a BMP palette is grayscale, the palette is dropped and the pixel data is read with the raw mode "1" or "L", without checking the bit depth of the file. So an 8-bit image with a black and white palette was read as 1 bit per pixel, and a 4-bit image with a grayscale palette was read as 8 bits per pixel.

Changes proposed in this pull request:

  • For uncompressed 8-bit images with up to two colors, keep mode "1" but read the data with the "1;8" raw mode
  • Keep 4-bit images, and 1-bit images with more than two colors in the palette, as mode "P". There is no raw mode that reads their indices directly as "1" or "L" values
  • RLE images are unchanged, since the decoder already produces one byte per pixel

The mode of those "P" images changes ("1" or "L" before), but their pixels were wrong before.

I tested with a local build of main without libjpeg: the BMP, ICO, CUR and DIB tests pass, except for test_save_bmp_with_dpi, which needs the JPEG encoder. The full test suite was not run locally.

This change was written with help from an AI coding assistant.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A P image with a black and white palette does not survive a BMP round trip: 8-bit data is read as 1-bit

2 participants