Skip to content

fix: BMP save/load uses BI_BITFIELDS for correct RGB565 color encoding - #16

Merged
bdbarnett merged 2 commits into
mainfrom
copilot/fix-graphics-save-image-encoding
Aug 4, 2026
Merged

fix: BMP save/load uses BI_BITFIELDS for correct RGB565 color encoding#16
bdbarnett merged 2 commits into
mainfrom
copilot/fix-graphics-save-image-encoding

Conversation

Copilot AI commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

save_image() wrote RGB565 BMP files with BI_RGB compression (0), causing standard viewers (Pillow, Windows, etc.) to assume RGB555 layout and corrupt the green channel for any mixed-color pixel.

Root cause

BITMAPINFOHEADER with 16bpp and BI_RGB is underspecified — readers default to 5-5-5. Correct RGB565 requires BI_BITFIELDS (compression=3) with explicit channel masks.

Changes

  • lib/pygraphics/_bmp565.py — writer: Changed compression field from BI_RGB (0) → BI_BITFIELDS (3); appends three 4-byte masks (R=0xF800, G=0x07E0, B=0x001F) after the BITMAPINFOHEADER. Data offset updated 54 → 66 bytes.
  • lib/pygraphics/_bmp565.py — reader: read_bmp565_header now reads and validates the compression field, accepting both BI_RGB (0) and BI_BITFIELDS (3) for backward compatibility.
  • src/gfx_bmp565.c — writer/reader: Same fixes mirrored in the C implementation.

Before / After

# c1 = color565(0xFF, 0x8A, 0x3D) → 0xfc47
# Before fix (Pillow reads as RGB555):  (255,  16, 57)   ← green wrong
# After  fix (Pillow reads as RGB565): (255, 137, 57)   ← correct within 5/6-bit quantization
  • Fixes #68

…annel corruption

Co-authored-by: bdbarnett <127794626+bdbarnett@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix graphics.save_image() mis-encoding of mixed RGB565 colors to BMP fix: BMP save/load uses BI_BITFIELDS for correct RGB565 color encoding Aug 4, 2026
Copilot AI requested a review from bdbarnett August 4, 2026 19:10
@bdbarnett
bdbarnett marked this pull request as ready for review August 4, 2026 19:13
@bdbarnett
bdbarnett merged commit 0aefde9 into main Aug 4, 2026
1 check passed
@bdbarnett
bdbarnett deleted the copilot/fix-graphics-save-image-encoding branch August 4, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

graphics.save_image() mis-encodes mixed RGB565 colors to BMP

2 participants