Skip to content

fix: color allocation offset in RasterizedImage for LUT indexes - #745

Merged
PrinsFrank merged 2 commits into
PrinsFrank:mainfrom
ahoogerheide:main
Dec 4, 2026
Merged

PrinsFrank merged 2 commits into
PrinsFrank:mainfrom
ahoogerheide:main

Conversation

@ahoogerheide

Copy link
Copy Markdown
Contributor

This issue was analyzed by cursor. The code and pull request was reviewed by me for correctness before submitting.


Is there an existing issue for this?

  • I have searched the existing issues and found no similar reports

Are you using the latest version of this package?

  • The issue I'm reporting exists in the latest release

Did you experience this in a real-world PDF?

  • The PDF I'm trying to read is not synthetic and generated by an actual PDF generator

Can other PDF readers read the file?

  • The PDF I'm trying to read opens correctly in at least one other PDF reader

When running this snippet

issf-10m-air-rifle-target.pdf

$document = (new PdfParser())->parseFile(__DIR__ . '/issf-10m-air-rifle-target.pdf');
$image = $document->getPages()[0]->getImages()[0];
file_put_contents(
    __DIR__ . '/issf-10m-air-rifle-target.png',
    $image->getContent()->toString(),
);

I run into the following issue/exception

The image is gray/white instead of the original black/white.
issf-10m-air-rifle-target-original

Code fix

RasterizedImage::toPNG looks up an indexed color one byte into the palette instead of one color into the palette.

For RGB that is bytes i, i + 1, and i + 2. It should be bytes i * 3, i * 3 + 1, and i * 3 + 2.
The CMYK case is also corrected.
The Gray case needs no correction because it is 1 byte per pixel.

You should be aware this uses the enum Components->value directly. It is currently equal to the nr of bytes per pixel.

The image is now equal to when viewed in a Adobe Acrobat.
issf-10m-air-rifle-target-fixed

Do you allow attachment files to be used in tests to prevent regressions?

The issf-10m-air-rifle-target.pdf file is a page 197 from the following document: ISSF Rule Book 2026 v3. I do not have the authorization to give permission for reuse.

@ahoogerheide

ahoogerheide commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

On page 42 of issue-235; the stored image was produced by the old lookup, so index 2 was saved as near-white (255, 254, 254) rather than the black (0, 0, 0) stored in the PDF. The indexed image stores a black rounded frame.

However, every one of those pixels has alpha 0 in its /SMask, so a PDF viewer does not paint it. This library recognizes /SMask as a dictionary key and does not apply it when rasterizing, So that image extraction sample is still somewhat wrong.

@PrinsFrank

Copy link
Copy Markdown
Owner

Thanks for the targeted and minimal PR! There's already an open issue for softmasks, images are currently not as fully functional as text but we're getting there!

@PrinsFrank
PrinsFrank merged commit 742563a into PrinsFrank:main Dec 4, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants