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

Image Embed Issues with Grayscale Image and RGBA PNG #366

Open
lanyeeee opened this issue Dec 27, 2024 · 0 comments
Open

Image Embed Issues with Grayscale Image and RGBA PNG #366

lanyeeee opened this issue Dec 27, 2024 · 0 comments

Comments

@lanyeeee
Copy link
Contributor

lanyeeee commented Dec 27, 2024

Description

  1. Grayscale JPG not display at all
  2. Grayscale PNG display incorrectly/with abnormal appearance
  3. RGBA PNG not displaying at all

Minimal Reproduction Project

embed-img-to-pdf-err.zip

lopdf Version

0.34.0

Additional Information

In fact I have found the cause of this issue

  1. Grayscale Image Not Displaying

    let bits = color_type.bits_per_pixel() / 3;

    The code incorrectly divides bits_per_pixel by 3 for all image types.
    For grayscale images, bits_per_pixel is 8 and should not be divided by 3.

  2. RGBA PNG Not Displaying

    ColorType::Rgba8 => b"DeviceN".to_vec(),

    The code incorrectly sets the color_space to DeviceN for RGBA images.
    RGBA should be convert to RGB before embed, then use DeviceRGB color space instead of DeviceN

I have preliminarily resolved this issue, they works good in my local project.

I plan to make a PR, but I noticed this interesting comment in the code:

lopdf/src/xobject.rs

Lines 71 to 75 in 34e2d20

// It looks like Adobe Illustrator uses a predictor offset of 2 bytes rather than 1 byte as
// the PNG specification suggests. This seems to come from the fact that the PNG specification
// doesn't allow 4-bit color images (only 8-bit and 16-bit color). With 1-bit, 2-bit and 4-bit
// mono images there isn't the same problem because there's only one component.
let bits = color_type.bits_per_pixel() / 3;

  1. The comment discusses predictor offset behavior in Adobe Illustrator
  2. However, the code that follows (bits_per_pixel() / 3) seems unrelated to the predictor behavior described in the comment
  3. This division by 3 is actually causing issues with grayscale images

Before submitting the PR, I'd like to understand:

  1. Is there any historical context for this implementation?
  2. Does the Adobe Illustrator predictor behavior still need to be considered?
  3. Should we update or remove this comment?
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

No branches or pull requests

1 participant