shared-module/OnDiskBitmap.c: Fix loading small monochrome bitmaps #10097
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug where
OnDiskBitmap
was unable to read indexed bitmaps with a small header (e.gBITMAPCOREHEADER
orBITMAPINFOHEADER
) 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):
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 aBITMAPV5HEADER
(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 tof_read()
to read the remainder of the header.Other changes
I removed some intermediary variables (
indexed
andbits_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.