Repository navigation
BUG: Decode image samples with 16 bits per component - #4435
Open
metsw24-max wants to merge 1 commit into
Open
metsw24-max wants to merge 1 commit into
metsw24-max wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4435 +/- ##
=======================================
Coverage 98.87% 98.87%
=======================================
Files 59 59
Lines 12299 12307 +8
Branches 2342 2346 +4
=======================================
+ Hits 12161 12169 +8
Misses 68 68
Partials 70 70 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
MartinThoma
reviewed
Nov 15, 2026
Comment on lines
+231
to
+240
| # Samples are big-endian 16-bit values. Adding them byte by byte | ||
| # would drop the carry from the low byte into the high byte. | ||
| bpp = colors * 2 | ||
| for i in range(0, len(str_data_mut) - 1, 2): | ||
| if i % row_length >= bpp: | ||
| value = ( | ||
| int.from_bytes(str_data_mut[i:i + 2], "big") | ||
| + int.from_bytes(str_data_mut[i - bpp:i - bpp + 2], "big") | ||
| ) % 65536 | ||
| str_data_mut[i:i + 2] = value.to_bytes(2, "big") |
MartinThoma
reviewed
Nov 15, 2026
Comment on lines
+233
to
+240
| bpp = colors * 2 | ||
| for i in range(0, len(str_data_mut) - 1, 2): | ||
| if i % row_length >= bpp: | ||
| value = ( | ||
| int.from_bytes(str_data_mut[i:i + 2], "big") | ||
| + int.from_bytes(str_data_mut[i - bpp:i - bpp + 2], "big") | ||
| ) % 65536 | ||
| str_data_mut[i:i + 2] = value.to_bytes(2, "big") |
Member
There was a problem hiding this comment.
I'd prefer if this was moved to FlateDecode in a new static function _decode_tiff_prediction_16_bit next to _decode_png_prediction:
Suggested change
| bpp = colors * 2 | |
| for i in range(0, len(str_data_mut) - 1, 2): | |
| if i % row_length >= bpp: | |
| value = ( | |
| int.from_bytes(str_data_mut[i:i + 2], "big") | |
| + int.from_bytes(str_data_mut[i - bpp:i - bpp + 2], "big") | |
| ) % 65536 | |
| str_data_mut[i:i + 2] = value.to_bytes(2, "big") | |
| str_data = FlateDecode._decode_tiff_prediction_16_bit(str_data, columns, colors) |
This makes tracebacks easier to read, doesn't cost much in performance, and follow-up PRs might also get easier to read :-)
This branch has not been deployed
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.
16-bit image samples are decoded as 8-bit
Problem:
page.imagesreturns wrong pixels for image XObjects with/BitsPerComponent 16(PDF 1.5 and later; img2pdf writes them for 16-bit PNGs, Photoshop for 16-bit exports)._xobj_to_imagehands the big-endian 16-bit samples to Pillow in an 8-bit mode, so the result is built from the interleaved high and low bytes of the first half of the data. Streams that also use/Predictor 2are wrong a second time: the TIFF branch ofFlateDecode.decodeundoes the prediction byte by byte, which drops the carry from the low into the high byte, and the error accumulates along each row.Fix: the TIFF predictor adds whole 16-bit samples when
/BitsPerComponentis 16, and_xobj_to_imagekeeps the most significant byte of each sample for the raw-sample filters before the data reaches Pillow. DCT, JPX, CCITT and JBIG2 data is left alone, as are images with 8 bits or fewer. Pillow has no 16-bit mode for multi-band images, and pdf.js and MuPDF reduce to 8 bits the same way, so this seemed the least surprising behaviour.Checked against MuPDF: img2pdf output for 16-bit grey and RGB PNGs, synthetic
/Predictor 2images, and the 16-bit Photoshop image iniss2925.pdffrom the test cache (object 456 with its 16-bit/SMask) now decode byte-identically to MuPDF's pixmaps. Tests added intests/test_filters.pyandtests/test_images.py; they fail onmainand pass with the change. Sub-byte depths with/Predictor 2are not touched here.