Fix #2467 bmp encoding issue for BMP with 1 bit per pixel and more pixels per row than divisible by 8. #2471
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:
bwwbbbwbww
The bytes should look like:"
0x01100010 0x11000000
However, the current implementation of Write1BitPixelData will when it reaches the last byte looks like:
So
quantizedPixelRow.Length
will be 10, resulting in astartIdx
of 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 0x00010110
If 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
quantizedPixelRow
when X equals the remaining pixels instead of always the last 7 pixels. So thestartIdx
needs 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.