Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DCTDecode and SMask when saving LA and RGBA PDF images #8097

Closed
wants to merge 1 commit into from

Conversation

radarhere
Copy link
Member

Resolves #8074

Discussion in the issue revealed that JPXDecode PDFs are not displayed correctly in Firefox. This is the filter that Pillow uses to save LA and RGBA images. There is an open issue about the Firefox problem, but in the meantime, there was a suggestion that we could use SMasks instead.

@sl2c
Copy link

sl2c commented Jun 2, 2024

Thanks for the commit!

I've just briefly glanced over the code and it seems like you're encoding every mode except 'P' as '/DCTDecode', while 'P' is encoded as '/ASCIIHexDecode'. Some comments:

  1. why use '/ASCIIHexDecode' for 'P'? If the palette is not random (i.e., if, on the opposite side of this, the palette is monotone — increasing palette index means non-decreasing luminosity) — and it's almost never random in real life — there's quite a bit of correlation between consecutive palette indices, and applying a .DecodeParms.Predictor = 11-15 (see Adobe PDF Ref. v. 1.7 page 76) and then compressing with /FlateDecode would give a much smaller file size. But even without the predictors, usually ZIP compresses these real-life images much better.

  2. it makes sense to use '/DCTDecode' only if a) the user asks for it; b) if the image.format is 'JPEG', which would imply that we already have the quantization tables etc. In all other situations the image has to be saved using a lossless filter, e.g. the /FlateDecode + /Predictor combination (see previous paragraph).

  3. A single exception to this is when image.mode is '1', for which the best (and lossless) filter to use is /CCITTFaxDecode.

For examples of how all this could be done please take a look at pdfimage.py

Finally, if all this is too complicated the simplest solution to code would be for all image.mode-s to call .tobytest() and then zlib.compress() and put the result into the PDF image dictionary stream, and then add /Filter = /FlateDecode. The bottom logic is that Pillow should just produce the PDF format and then it's up to the user to compress/re-encode it as they wish.

@radarhere
Copy link
Member Author

radarhere commented Jun 3, 2024

I don't think your comments relate to this PR or the problem it is trying to fix, but are rather comments on existing state of PdfImagePlugin. So I would like to leave this PR as it is.

That said,

  1. I think our code is already aware there could be an improvement.

    # FIXME: Should replace ASCIIHexDecode with RunLengthDecode
    # (packbits) or LZWDecode (tiff/lzw compression). Note that
    # PDF 1.2 also supports Flatedecode (zip compression).

  2. You're advocating for lossless saving. However, the natural downside of this would be larger file sizes. I think a good solution would be to add a 'lossless' argument like WebP has.

@radarhere
Copy link
Member Author

radarhere commented Jun 4, 2024

The PDF.js issue has now been resolved! mozilla/pdf.js#16782

It will still require another release of PDF.js, and then a release of Firefox to include that, but it is a positive development.

@radarhere radarhere closed this Jun 4, 2024
@radarhere radarhere deleted the pdf branch June 4, 2024 12:43
@radarhere
Copy link
Member Author

Testing with the latest version of Firefox (129.0.2), it is now rendering the PDF correctly - so the PDF.js fix has made it through.

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.

RGBA PNG saved as PDF renders incorrectly in some applications
2 participants