Skip to content

shared-module/OnDiskBitmap.c: Fix loading small monochrome bitmaps #10097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2025

Conversation

SamantazFox
Copy link

@SamantazFox SamantazFox commented Feb 24, 2025

This PR fixes a bug where OnDiskBitmap was unable to read indexed bitmaps with a small header (e.g BITMAPCOREHEADER or BITMAPINFOHEADER) with a file size inferior to 138 bytes. This is even more obvious with monochrome bitmaps.

I have attached the required code + bitmap to reproduce this issue. Here is the output of the script on a build that doesn't include this PR (warning: french):

Auto-chargement activé. Copiez ou sauvegardez les fichiers via USB pour les lancer ou démarrez le REPL pour le désactiver.
main.py sortie :
Traceback (appels les plus récents en dernier) :
  Fichier "main.py", ligne 17, dans <module>
ValueError: file invalide

cpy_bug_small_bmp_cannnot_load.zip

Technical details

This was caused by the first f_read() call which expected 138 bytes, that is the size of the BMP file header (14 bytes) followed by a BITMAPV5HEADER (124 bytes).

The new code only reads the bare minimum at first: 14 bytes of the file header, plus 12 bytes for a BITMAPCOREHEADER (the smallest possible). If the header size is not 12 bytes, then the code will do another call to f_read() to read the remainder of the header.

Other changes

I removed some intermediary variables (indexed and bits_per_pixel).

I also removed the useless check on number_of_colors, as the code for an indexed bitmap will always compute that value from the number of bits per pixel and this variable is not used outside of the indexed section anyway.

Both of these might save a bit of RAM but more importantly flash size.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tannewt tannewt merged commit d857656 into adafruit:main Feb 25, 2025
530 checks passed
@SamantazFox SamantazFox deleted the fix-small-bitmaps branch February 25, 2025 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants