Fix #1534: TIFF/1-bit indexed images rendered inverted#1597
Open
andreasrosdalw wants to merge 1 commit into
Open
Fix #1534: TIFF/1-bit indexed images rendered inverted#1597andreasrosdalw wants to merge 1 commit into
andreasrosdalw wants to merge 1 commit into
Conversation
…f CCITT-encoding them The indexed-color path introduced in LibrePDF#1499/LibrePDF#1524 created the image via Image.getInstance(width, height, 1, bpc, pixelData), which for 1-bit data compresses the pixels with CCITT G4 and BlackIs1 semantics. That discards the palette (PdfImage never applies the additional /Indexed colorspace to CCITT images) and renders palette index 1 as black. Bilevel TIFFs decoded with PhotometricInterpretation WhiteIsZero (palette {white, black}) were therefore rendered inverted since 3.0.1. Create the ImgRaw directly so the pixel data stays palette indices and the /Indexed colorspace is preserved. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 6 |
| Duplication | 2 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
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.



Fixes #1534.
Root cause
The indexed-color fast path added in #1499 (3.0.1) and reworked in #1524 (3.0.2) builds the PDF image via
Image.getInstance(width, height, 1, bitsPerPixel, pixelData). For 1-bit data that factory has a special case that CCITT-G4-compresses the pixels and returns a CCITT image withCCITT_BLACKIS1semantics:This breaks 1-bit indexed images twice over:
PdfImagenever applies theadditional/Indexedcolorspace dictionary to CCITT images, so the palette is silently dropped and the data is rendered as DeviceGray.BlackIs1, palette index 1 renders as black. A bilevel TIFF withPhotometricInterpretation = WhiteIsZerodecodes to aTYPE_BYTE_BINARYBufferedImagewith palette{0 = white, 1 = black}, so black and white swap — exactly the inversion reported in the issue. 3.0.0 was unaffected because this code path didn't exist yet (images went through the generic RGB path).Fix
Create the
ImgRawdirectly in the indexed path so the pixel data stays palette indices and the/Indexedcolorspace is preserved. The image is still flate-compressed byPdfImageas before.Verification
TYPE_BYTE_BINARYimage with a{white, black}palette, and the issue's exact flow (binary image → PNG bytes →Image.getInstance(byte[])). Both resolve every pixel of the resulting image through the embedded palette and compare with the sourceBufferedImage; both fail before the fix and pass after.sample.tifattached to TIFF images are rendered inverted starting with 3.0.1 #1534 (decoded with TwelveMonkeys, converted with the reporter's reproduction code): the image embedded in the produced PDF now matches all 8,748,480 pixels of the original TIFF exactly (previously fully inverted).openpdf-coretest suite passes (2082 tests, 0 failures).