image

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2026 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package image provides support for embedding images in PDF documents.

The package supports various image formats and provides facilities for image masking, transparency, and color space handling. Images can be created from Go's standard image.Image interface or constructed manually using the provided types.

Key types:

  • Dict: represents a standard PDF image with full color support
  • Mask: represents a 1-bit image mask for transparency effects
  • SoftMask: represents a grayscale mask for graduated transparency
  • Indexed: represents an image with an indexed color palette

The package handles compression automatically and supports both lossless and lossy storage formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JPEG

func JPEG(src image.Image, opts *jpeg.Options) (graphics.Image, error)

JPEG creates a new PDF image from a JPEG image. The file is stored in the DCTDecode format in the PDF file.

Types

type Dict added in v0.6.0

type Dict struct {
	// Width is the width of the image in pixels.
	Width int

	// Height is the height of the image in pixels.
	Height int

	// ColorSpace is the color space in which image samples are specified.
	// It can be any type of color space except Pattern.
	ColorSpace color.Space

	// BitsPerComponent is the number of bits used to represent each color component.
	// The value must be 1, 2, 4, 8, or (from PDF 1.5) 16.
	BitsPerComponent int

	// Decode (optional) is an array of numbers describing how to map image
	// samples into the range of values appropriate for the image's color
	// space. The slice must have twice the number of color components in the
	// ColorSpace.
	Decode []float64

	// WriteData is a function that writes the image data to the provided
	// writer. The data should be written row by row, with each row containing
	// Width * ColorSpace.Channels() samples, each sample using
	// BitsPerComponent bits.
	WriteData func(io.Writer) error

	// MaskImage (optional) determines which parts of the image are to be
	// painted.
	//
	// Only one of MaskImage or MaskColors may be specified.
	MaskImage *Mask

	// MaskColors (optional) defines color ranges for transparency masking.
	// Contains pairs [min1, max1, min2, max2, ...] for each color component.
	// Each value must be in the range 0 to (2^BitsPerComponent - 1) and
	// represents raw color values before any Decode array processing. Pixels
	// with all components in their respective ranges become transparent.
	//
	// Only one of MaskImage or MaskColors may be specified.
	MaskColors []uint16

	// SMask (optional; PDF 1.4) is a subsidiary image XObject defining a
	// soft-mask image for transparency effects.
	SMask *SoftMask

	// SMaskInData (optional for JPXDecode; PDF 1.5) specifies how soft-mask
	// information encoded with image samples should be used:
	// 0 = ignore encoded soft-mask info (default)
	// 1 = image data includes encoded soft-mask values
	// 2 = image data includes premultiplied opacity channel
	SMaskInData int

	// Interpolate indicates whether image interpolation should be performed by
	// a PDF processor.
	Interpolate bool

	// Alternates (optional) is an array of alternate image dictionaries for this image.
	Alternates []*Dict

	// OptionalContent (optional) allows to control the visibility of the image.
	OptionalContent oc.Conditional

	// Intent (optional) is the name of a color rendering intent to be used in
	// rendering the image.
	Intent graphics.RenderingIntent

	// StructParent (required if the image is a structural content item)
	// is the integer key of the image's entry in the structural parent tree.
	StructParent optional.UInt

	// Metadata (optional) is a metadata stream containing metadata for the image.
	Metadata *metadata.Stream

	// AssociatedFiles (optional; PDF 2.0) is an array of files associated with
	// the image. The relationship that the associated files have to the
	// XObject is supplied by the Specification.AFRelationship field.
	//
	// This corresponds to the AF entry in the image dictionary.
	AssociatedFiles []*file.Specification

	// Measure (optional; PDF 2.0) specifies the scale and units that apply to
	// the image.
	Measure measure.Measure

	// PtData (optional; PDF 2.0) contains extended geospatial point data.
	PtData *measure.PtData

	// WebCaptureID (optional) is the digital identifier of the image's parent
	// Web Capture content set.
	//
	// This corresponds to the /ID entry in the image mask dictionary.
	WebCaptureID *webcapture.Identifier

	// Name is deprecated and should be left empty.
	// Only used in PDF 1.0 where it was the name used to reference the image
	// mask from within content streams.
	Name pdf.Name
}

Dict represents a PDF image XObject dictionary.

TODO(voss): currently there is no way to write JPXDecode images. Rethink the whole WriteData approach.

func ExtractDict added in v0.7.0

func ExtractDict(x *pdf.Extractor, obj pdf.Object) (*Dict, error)

ExtractDict extracts an image dictionary from a PDF stream.

func FromImage added in v0.6.0

func FromImage(img image.Image, colorSpace color.Space, bitsPerComponent int) *Dict

FromImage creates a Dict from an image.Image. The ColorSpace and BitsPerComponent must be set appropriately for the image.

func FromImageWithMask added in v0.6.0

func FromImageWithMask(img image.Image, mask image.Image, colorSpace color.Space, bitsPerComponent int) *Dict

FromImageWithMask creates a Dict with an associated ImageMask from two image.Image objects.

func PNG

func PNG(img image.Image, colorSpace color.Space) (*Dict, error)

PNG creates an image dictionary for lossless storage similar to PNG format. The image is stored with 8 bits per component and uses PNG-style predictors for compression. If the image has an alpha channel, a soft mask is automatically created.

If colorSpace is nil, DeviceRGB will be used as the default color space.

func (*Dict) Bounds added in v0.6.0

func (d *Dict) Bounds() rect.IntRect

Bounds returns the dimensions of the image.

func (*Dict) Embed added in v0.6.0

func (d *Dict) Embed(rm *pdf.EmbedHelper) (pdf.Native, error)

Embed adds the image to the PDF file and returns the embedded object.

func (*Dict) Subtype added in v0.6.0

func (d *Dict) Subtype() pdf.Name

Subtype returns the PDF XObject subtype for images.

type Indexed added in v0.6.0

type Indexed struct {
	Pix        []uint8
	Width      int
	Height     int
	ColorSpace color.Space
}

Indexed represents an image with an indexed color space.

func NewIndexed added in v0.6.0

func NewIndexed(width, height int, cs color.Space) *Indexed

NewIndexed returns a new Indexed image of the given size.

func (*Indexed) Bounds added in v0.6.0

func (im *Indexed) Bounds() rect.IntRect

Bounds returns the image bounds. This implements the graphics.Image interface.

func (*Indexed) Embed added in v0.6.0

func (im *Indexed) Embed(rm *pdf.EmbedHelper) (pdf.Native, error)

Embed adds the image to the PDF file. This implements the graphics.Image interface.

func (*Indexed) Subtype added in v0.6.0

func (im *Indexed) Subtype() pdf.Name

Subtype returns /Image. This implements the graphics.Image interface.

type Mask added in v0.7.0

type Mask struct {
	// Width is the width of the image mask in pixels.
	Width int

	// Height is the height of the image mask in pixels.
	Height int

	// Inverted indicates the meaning of individual bits in the image data:
	//   - false: 0=opaque and 1=transparent
	//   - true: 1=opaque and 0=transparent
	Inverted bool

	// WriteData is a function that writes the mask data to the provided writer.
	// The data should be written as a continuous bit stream, with each row
	// starting at a new byte boundary. 0 = opaque, 1 = transparent.
	WriteData func(io.Writer) error

	// Interpolate enables edge smoothing for the mask to reduce jagged
	// appearance in low-resolution stencil masks.
	Interpolate bool

	// Alternates (optional) is an array of alternate image dictionaries for this mask.
	Alternates []*Mask

	// OptionalContent (optional) allows to control the visibility of the mask.
	OptionalContent oc.Conditional

	// StructParent (required if the image mask is a structural content item)
	// is the integer key of the image mask's entry in the structural parent tree.
	StructParent optional.UInt

	// Metadata (optional) is a metadata stream containing metadata for the image.
	Metadata *metadata.Stream

	// AssociatedFiles (optional; PDF 2.0) is an array of files associated with
	// the mask. The relationship that the associated files have to the
	// XObject is supplied by the Specification.AFRelationship field.
	//
	// This corresponds to the AF entry in the image mask dictionary.
	AssociatedFiles []*file.Specification

	// Measure (optional; PDF 2.0) specifies the scale and units which apply to
	// the mask.
	Measure measure.Measure

	// PtData (optional; PDF 2.0) contains extended geospatial point data.
	PtData *measure.PtData

	// WebCaptureID (optional) is the digital identifier of the image's parent
	// Web Capture content set.
	//
	// This corresponds to the /ID entry in the image mask dictionary.
	WebCaptureID *webcapture.Identifier

	// Name is deprecated and should be left empty.
	// Only used in PDF 1.0 where it was the name used to reference the image
	// from within content streams.
	Name pdf.Name
}

func ExtractMask added in v0.7.0

func ExtractMask(x *pdf.Extractor, obj pdf.Object) (*Mask, error)

ExtractMask extracts an image mask from a PDF stream.

func FromImageMask added in v0.6.0

func FromImageMask(img image.Image) *Mask

FromImageMask creates an ImageMask from an image.Image. Only the alpha channel is used, with alpha values rounded to full opacity or full transparency.

func (*Mask) Bounds added in v0.7.0

func (m *Mask) Bounds() rect.IntRect

Bounds returns the dimensions of the mask.

func (*Mask) Embed added in v0.7.0

func (m *Mask) Embed(rm *pdf.EmbedHelper) (pdf.Native, error)

Embed adds the mask to the PDF file and returns the embedded object.

func (*Mask) IsImageMask added in v0.7.0

func (m *Mask) IsImageMask() bool

IsImageMask returns true, indicating this is a stencil mask. This implements the graphics.ImageMask interface.

func (*Mask) Subtype added in v0.7.0

func (m *Mask) Subtype() pdf.Name

Subtype returns the PDF XObject subtype for image masks.

type PixelRow added in v0.6.0

type PixelRow struct {
	// contains filtered or unexported fields
}

PixelRow is a helper for efficiently packing pixel data into bytes for PDF image streams. It handles arbitrary bits per pixel and packs them into a byte array.

func NewPixelRow added in v0.6.0

func NewPixelRow(numElems, bitsPerElem int) *PixelRow

NewPixelRow creates a new PixelRow for packing image data. numElems is the number of elements (pixels * channels) in the row. bitsPerElem is the number of bits per element (1, 2, 4, 8, or 16).

func (*PixelRow) AppendBits added in v0.6.0

func (r *PixelRow) AppendBits(bits uint16)

AppendBits appends the specified number of bits to the row. Only the low-order bitsPerElem bits of the value are used.

func (*PixelRow) Bytes added in v0.6.0

func (r *PixelRow) Bytes() []byte

Bytes returns the underlying byte slice containing the packed pixel data.

func (*PixelRow) Reset added in v0.6.0

func (r *PixelRow) Reset()

Reset clears the row buffer and resets position counters.

type SoftMask added in v0.7.0

type SoftMask struct {
	// Width is the width of the soft mask in pixels.
	// If Matte is present, this must match the parent image width.
	Width int

	// Height is the height of the soft mask in pixels.
	// If Matte is present, this must match the parent image height.
	Height int

	// BitsPerComponent is the number of bits used to represent each grayscale
	// component. The value must be 1, 2, 4, 8, or 16.
	BitsPerComponent int

	// Decode (optional) is an array of numbers describing how to map mask
	// samples into the range 0.0 to 1.0. If present, the array must contain
	// exactly 2 values [Dmin, Dmax]. Default: [0.0, 1.0] which maps 0 to
	// transparent, max to opaque.
	Decode []float64

	// WriteData is a function that writes the grayscale mask data to the
	// provided writer. The data should be written row by row, with each row
	// containing Width samples, each sample using BitsPerComponent bits.
	WriteData func(io.Writer) error

	// Interpolate indicates whether mask interpolation should be performed by
	// a PDF processor to reduce pixelation in low-resolution masks.
	Interpolate bool

	// Matte (optional) specifies the matte color used for pre-blended image
	// data. The array must contain n values where n is the number of
	// components in the parent image's color space. When present, the parent
	// image data has been pre-multiplied with this matte color using the soft
	// mask as the alpha channel.
	Matte []float64
}

SoftMask represents a soft-mask image used for graduated transparency. Soft-mask images are subsidiary image XObjects that provide alpha values for smooth transparency effects.

func ExtractSoftMask added in v0.7.0

func ExtractSoftMask(x *pdf.Extractor, obj pdf.Object) (*SoftMask, error)

ExtractSoftMask extracts a soft-mask image from a PDF stream.

func FromImageAlpha added in v0.7.0

func FromImageAlpha(img image.Image, bitsPerComponent int) *SoftMask

FromImageAlpha creates a SoftMask from the alpha channel of an image.Image. The alpha channel is converted to grayscale values where 0=transparent, max=opaque.

func (*SoftMask) Bounds added in v0.7.0

func (sm *SoftMask) Bounds() rect.IntRect

Bounds returns the dimensions of the soft mask.

func (*SoftMask) Embed added in v0.7.0

func (sm *SoftMask) Embed(rm *pdf.EmbedHelper) (pdf.Native, error)

Embed embeds the soft mask as a PDF image XObject stream.

func (*SoftMask) Subtype added in v0.7.0

func (sm *SoftMask) Subtype() pdf.Name

Subtype returns "Image". This implements the graphics.Image interface.

Directories

Path Synopsis
Package lab provides image types for the CIE Lab color space.
Package lab provides image types for the CIE Lab color space.
Package thumbnail provides support for PDF page thumbnail images.
Package thumbnail provides support for PDF page thumbnail images.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL