Fix #2467 bmp encoding issue for BMP with 1 bit per pixel and more pixels per row than divisible by 8.#2471
Merged
JimBobSquarePants merged 2 commits intoSixLabors:mainfrom Jun 8, 2023
synercoder:fixes/bit1-pallet-bmp-bug
Merged
Fix #2467 bmp encoding issue for BMP with 1 bit per pixel and more pixels per row than divisible by 8.#2471JimBobSquarePants merged 2 commits intoSixLabors:mainfrom synercoder:fixes/bit1-pallet-bmp-bug
JimBobSquarePants merged 2 commits intoSixLabors:mainfrom
synercoder:fixes/bit1-pallet-bmp-bug
Conversation
JimBobSquarePants
approved these changes
Jun 8, 2023
Member
JimBobSquarePants
left a comment
There was a problem hiding this comment.
Legend!
I would have been wasting time looking for issues in the quantizer. Thanks for providing a fix.
This was referenced Jul 30, 2025
This was referenced Sep 29, 2025
This was referenced Nov 17, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Prerequisites
Description
Fix for issue #2467.
If you have a BMP with 1 bit per pixel, and more pixels per row than divisible by 8, the last byte of the row will need to be padded with extra zeros.
So if you have a black/white image of 10 pixels in a row, and the row looks like:
bwwbbbwbwwThe bytes should look like:"
0x01100010 0x11000000However, the current implementation of Write1BitPixelData will when it reaches the last byte looks like:
So
quantizedPixelRow.Lengthwill be 10, resulting in astartIdxof 10-7 = 3It will then write pixels to the byte starting at index 3 again, meaning that the last bytes looks like:
0x00010110, the last byte will always contain the last 7 pixels, instead of the amount of pixels remaining.This results in that when the image in opened again, the row bytes looks like:
0x01100010 0x00010110If you check the images in #2467, you can see that the corrupted image shows column 4 & 5 again in column 9 & 10.
The fix
The fix is to read the last X pixels from the
quantizedPixelRowwhen X equals the remaining pixels instead of always the last 7 pixels. So thestartIdxneeds to be adjusted:From:
int startIdx = quantizedPixelRow.Length - 7;To:
int startIdx = quantizedPixelRow.Length - (quantizedPixelRow.Length % 8);Small note
I am unsure of my test name, so I named it after my issue. I hope this is fine.