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 ¶
- func JPEG(src image.Image, opts *jpeg.Options) (graphics.Image, error)
- type Dict
- func ExtractDict(x *pdf.Extractor, obj pdf.Object) (*Dict, error)
- func FromImage(img image.Image, colorSpace color.Space, bitsPerComponent int) *Dict
- func FromImageWithMask(img image.Image, mask image.Image, colorSpace color.Space, ...) *Dict
- func PNG(img image.Image, colorSpace color.Space) (*Dict, error)
- type Indexed
- type Mask
- type PixelRow
- type SoftMask
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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
ExtractDict extracts an image dictionary from a PDF stream.
func FromImage ¶ added in v0.6.0
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 ¶
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.
type Indexed ¶ added in v0.6.0
Indexed represents an image with an indexed color space.
func NewIndexed ¶ added in v0.6.0
NewIndexed returns a new Indexed image of the given size.
func (*Indexed) Bounds ¶ added in v0.6.0
Bounds returns the image bounds. This implements the graphics.Image interface.
func (*Indexed) Embed ¶ added in v0.6.0
Embed adds the image to the PDF file. 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
ExtractMask extracts an image mask from a PDF stream.
func FromImageMask ¶ added in v0.6.0
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) Embed ¶ added in v0.7.0
Embed adds the mask to the PDF file and returns the embedded object.
func (*Mask) IsImageMask ¶ added in v0.7.0
IsImageMask returns true, indicating this is a stencil mask. This implements the graphics.ImageMask interface.
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
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
AppendBits appends the specified number of bits to the row. Only the low-order bitsPerElem bits of the value are used.
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
ExtractSoftMask extracts a soft-mask image from a PDF stream.
func FromImageAlpha ¶ added in v0.7.0
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.