gfngfn / SATySFi

A statically-typed, functional typesetting system
GNU Lesser General Public License v3.0
1.17k stars 83 forks source link

Support transparent PNG images #442

Closed nsfisis closed 3 months ago

nsfisis commented 3 months ago

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 commented 3 months ago

Thank you very much for the great improvement!

Confirmed that:

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.