A PDF image is a rectangle of samples: a width and a height in pixels, a number of
colour components and a number of bits for each. It is stored as an image XObject, a
stream drawn with Do, or, for a small image, inline in the content stream.
The image is always painted into the unit square of the current transformation matrix,
so q 200 0 0 100 72 500 cm /Im1 Do Q draws it 200 by 100 units with its
lower left corner at (72, 500), whatever its pixel size.
Image XObjects
| Key | Since | Meaning |
|---|---|---|
Type, Subtype | 1.0 | XObject and Image (required) |
Width, Height | 1.0 | Required. Pixels across and down |
ColorSpace | 1.0 | The colour space. Required except for masks and for JPXDecode images |
BitsPerComponent | 1.0 | 1, 2, 4, 8 or 16 bits a component. Required except for masks, which are 1, and JPXDecode images, where it is ignored |
Intent | 1.1 | The rendering intent for this image |
ImageMask | 1.0 | true for a stencil mask; default false |
Mask | 1.3 | An explicit mask image, or a colour-key array |
Decode | 1.0 | How to map sample values to colour components; see Decode |
Interpolate | 1.0 | Whether to smooth the image when it is enlarged; default false |
Alternates | 1.3 | Alternate versions of the image |
SMask | 1.4 | A soft mask image |
SMaskInData | 1.5 | JPXDecode only: 0 ignore the JPEG 2000 alpha, 1 use it as the soft mask, 2 it is premultiplied |
Name | 1.0 | The resource name; 2.0 deprecated |
StructParent | 1.3 | Its key in the structure parent tree |
ID | 1.3 | A Web Capture digest; 2.0 deprecated |
OPI | 1.2 | Open Prepress Interface proxy information; 2.0 deprecated |
Metadata | 1.4 | XMP metadata |
OC | 1.5 | The optional content the image belongs to |
AF, Measure, PtData | 2.0 | Associated files, and geospatial measurement and point data |
The data, after the filters are undone, is the samples row by row from the top left, each row padded to a whole byte, components interleaved: Height × ⌈Width × components × BitsPerComponent / 8⌉ bytes. Image-format filters (DCT, JPX, JBIG2, CCITT) produce samples in that layout themselves.
Colour spaces and bit depths
An image may use any colour space except Pattern. Indexed images may
have 1, 2, 4 or 8 bits a sample; 16 bits, from PDF 1.5, is for the other spaces.
Typical forms:
| Image | ColorSpace | BitsPerComponent | Filter |
|---|---|---|---|
| Photo | DeviceRGB or ICCBased | 8 | DCTDecode or JPXDecode |
| Screenshot or drawing | DeviceRGB | 8 | FlateDecode, often with a PNG predictor |
| Paletted | [/Indexed /DeviceRGB 255 <…>] | 8 or fewer | FlateDecode |
| Scanned text | DeviceGray, or a stencil mask | 1 | CCITTFaxDecode or JBIG2Decode |
DeviceCMYK | 8 | DCTDecode or FlateDecode |
Decode arrays
Decode has two numbers, Dmin and Dmax, for each
component. A sample x of n bits maps to
Dmin + x × (Dmax − Dmin) /
(2n − 1). The defaults:
| Colour space | Default Decode |
|---|---|
DeviceGray, CalGray, Separation | [0 1] |
DeviceRGB, CalRGB | [0 1 0 1 0 1] |
DeviceCMYK | [0 1 0 1 0 1 0 1] |
Lab | [0 100 amin amax bmin bmax], from its Range |
ICCBased | the space’s Range |
DeviceN | [0 1 0 1 …], one pair a colourant |
Indexed | [0 2n−1] |
| Stencil mask | [0 1] |
Swapping the pair inverts a component: [1 0] on a grey image makes a
negative, and on a stencil mask paints the 1 bits instead of the 0 bits.
Masks
Four ways to show only part of an image:
- Stencil mask (1.0):
ImageMask true, one bit a pixel, no colour space. The 0 bits are painted in the current fill colour and the 1 bits left alone (with the defaultDecode [0 1]). Used for one-colour scanned text and Type 3 bitmap glyphs. - Explicit mask (1.3):
Maskis another image, a stencil mask of any resolution, laid over the same unit square. Where it is 1, the image is hidden. - Colour-key mask (1.3):
Maskis an array of a minimum and maximum for each component, such as[250 255 250 255 250 255]. Pixels whose every component falls in the ranges are not painted, as with a transparent colour in GIF. - Soft mask (1.4): an image of opacity, for smooth edges and partial transparency.
Soft masks
An image’s SMask, from PDF 1.4, is an image XObject of its own, in
DeviceGray, whose samples give the opacity of the image at each point:
0 transparent, 1 opaque. It need not have the same size as the image; both are mapped
onto the same unit square. Its keys are those of an image, without
Mask, SMask or ImageMask, and with one of its
own:
| Key | Meaning |
|---|---|
Matte | The colour the image was premultiplied against, one value a component of the image’s colour space. When present, the image’s colours must be un-premultiplied before use |
A PNG with alpha becomes, in a PDF, an RGB image and a grey soft mask, each Flate
compressed. When SMask is present, Mask is ignored; a
soft mask set in the graphics state is overridden for the image as well.
Image data
- DCTDecode – a whole JPEG file. Most viewers hand it to a JPEG decoder as it is, which is why a PDF can hold a JPEG that can be extracted byte for byte
- JPXDecode – a JPEG 2000 file or codestream, from PDF 1.5; see below
- JBIG2Decode – a one-bit JBIG2 image, with its global segments in a separate stream
- CCITTFaxDecode – a one-bit fax-coded image, as in a TIFF Group 4 file
- FlateDecode, LZWDecode, RunLengthDecode – raw samples, compressed without loss
Only the image filters change how the samples are laid out. With every other filter the decoded data must already be the rows of samples described above.
JPEG 2000 images
A JPXDecode image, from PDF 1.5, can carry what the image dictionary
would otherwise say. The rules:
ColorSpacemay be left out. The colour space is then the one in the JPEG 2000 data: a JP2colrbox with an enumerated space or an ICC profile. IfColorSpaceis given, it overrides the data’s.BitsPerComponentis ignored; the data says. Components may differ in depth.Decodeis ignored, except for an image mask.SMaskInDatasays what to do with an alpha channel in the data: 0 ignore it (default), 1 use it as a soft mask, 2 use it as a premultiplied soft mask. IfSMaskis given, it wins.- A JPEG 2000 image may be an image mask, with one component of 1 bit.
PDF/A-1, based on PDF 1.4, does not allow JPEG 2000; PDF/A-2 and later allow it with limits on its colour boxes and bit depths.
Inline images
A small image can be written straight into the content stream:
q 17 0 0 17 298 388 cm
BI /W 17 /H 17 /CS /RGB /BPC 8 /F [/AHx /Fl]
ID
789CF3… >
EI Q
Between BI and ID come key and value pairs, usually in short
forms. After ID comes one white-space byte, then the data, then
EI.
| Full key | Short form |
|---|---|
BitsPerComponent | BPC |
ColorSpace | CS |
Decode | D |
DecodeParms | DP |
Filter | F |
Height | H |
ImageMask | IM |
Interpolate | I |
Width | W |
Intent | (no short form, 1.1) |
Length | L (2.0, optional: the length of the data) |
Values may be short too: colour spaces G, RGB,
CMYK and I (Indexed), and the filter names
AHx, A85, LZW, Fl, RL,
CCF and DCT. A colour space may also be a name in the
ColorSpace resources, and an Indexed space may be written in
full with its lookup string. There are no short names for JBIG2Decode
and JPXDecode, and an inline image has no way to refer to another
stream, so it has no soft mask, no mask image and no JBIG2 globals. The standard
advises keeping inline images to 4 KB or less, and making larger ones XObjects.
Finding the end is the hard part, since the data may hold the bytes
EI. Readers compute the data’s length from the width, height and depth
when there is no filter, decode the filter to find where it stops when there is one,
or look for EI preceded by white space and followed by white space or the
end. PDF 2.0’s L settles it where writers give it.
Alternates
Alternates, from PDF 1.3, is an array of alternate image dictionaries:
each has Image, another image XObject, and
DefaultForPrinting, true for the one to print. A viewer may show a
low-resolution image on screen and print a high-resolution one. Few readers use it,
and PDF/A forbids it.