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

Support transparent PNG images #442

Merged
merged 1 commit into from
Jun 22, 2024

Conversation

nsfisis
Copy link
Contributor

@nsfisis nsfisis commented Jun 21, 2024

Overview

It will add support for transparent PNG images (images that have alpha channel).

Here is the screenshot of tests/images/test.pdf generated from tests/images/test.saty:

image

The last 4 images have alpha channel and newly supported by the PR.

Implementation details

Extract the raw byte sequence of the alpha channel and create an XObject for the alpha mask. Then, add /SMask field that refers to the created XObject to the main image's dictionary.

@gfngfn
Copy link
Owner

gfngfn commented Jun 22, 2024

Thank you very much for the great improvement!

Confirmed that:

  • the modified implementation works as intended by running it locally, and that
  • the implementation conforms to the specification of PDF 1.7 by consulting the spec:

8.9.5 Image Dictionaries

Key Type Value
SMask stream (Optional; PDF 1.4) A subsidiary image XObject defining a soft-mask image (see 11.6.5.3, "Soft-Mask Images") that shall be used as a source of mask shape or mask opacity values in the transparent imaging model. The alpha source parameter in the graphics state determines whether the mask values shall be interpreted as shape or opacity. If present, this entry shall override the current soft mask in the graphics state, as well as the image’s Mask entry, if any. However, the other transparency-related graphics state parameters—blend mode and alpha constant—shall remain in effect. If SMask is absent, the image shall have no associated soft mask (although the current soft mask in the graphics state may still apply).

11.6.4.3 Mask Shape and Opacity

  • An image XObject may contain its own soft-mask image in the form of a subsidiary image XObject in the SMask entry of the image dictionary (see “Image Dictionaries”). This mask, if present, shall override any explicit or colour key mask specified by the image dictionary’s Mask entry. Either form of mask in the image dictionary shall override the current soft mask in the graphics state.

11.6.5.3 Soft-Mask Images

The second way to define a soft mask is by associating a soft-mask image with an image XObject. This is a subsidiary image XObject specified in the SMask entry of the parent XObject’s image dictionary (see “Image Dictionaries”). Entries in the subsidiary image dictionary for such a soft-mask image shall have the same format and meaning as in that of an ordinary image XObject (as described in Table 89 in “Image Dictionaries”), subject to the restrictions listed in Table 145.

Table 145 – Restrictions on the entries in a soft-mask image dictionary

Key Restriction
Type If present, shall be XObject.
Subtype Shall be Image.
Width If a Matte entry (see Table 146) is present, shall be the same as the Width value of the parent image; otherwise independent of it. Both images shall be mapped to the unit square in user space (as are all images), regardless of whether the samples coincide individually.
Height Same considerations as for Width.
ColorSpace Required; shall be DeviceGray.
BitsPerComponent Required.
Intent Ignored.
ImageMask Shall be false or absent.

@gfngfn gfngfn merged commit 98d8eb2 into gfngfn:master Jun 22, 2024
6 checks passed
@nsfisis nsfisis deleted the feat/transparent-png-image branch June 22, 2024 07:52
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