Documentation
¶
Overview ¶
Package image decodes raster images and builds PDF image XObjects (ISO 32000 §8.8.5) for embedding in documents.
Supported formats:
- JPEG — raw passthrough via DCTDecode (§7.4.8)
- PNG — decompressed to FlateDecode (§7.4.4)
- TIFF — via golang.org/x/image/tiff
- WebP — via golang.org/x/image/webp
- GIF — first frame only, via stdlib image/gif
Each format has a [Load*] function (from file path) and a [New*] function (from []byte). NewFromGoImage accepts a Go image.RGBA directly. All constructors return an Image which can produce a PDF XObject via Image.BuildXObject, including soft-mask (SMask) generation for images with transparency.
Index ¶
- Constants
- Variables
- type Image
- func LoadGIF(path string) (*Image, error)
- func LoadJPEG(path string) (*Image, error)
- func LoadPNG(path string) (*Image, error)
- func LoadTIFF(path string) (*Image, error)
- func LoadWebP(path string) (*Image, error)
- func NewFromGoImage(src *goimage.RGBA) *Image
- func NewGIF(data []byte) (*Image, error)
- func NewJPEG(data []byte) (*Image, error)
- func NewPNG(data []byte) (*Image, error)
- func NewTIFF(data []byte) (*Image, error)
- func NewWebP(data []byte) (*Image, error)
Constants ¶
const ( // MaxFileSize is the maximum size in bytes accepted by the Load* // functions. Files larger than this are rejected without allocating // a full-file buffer. MaxFileSize int64 = 200 * 1024 * 1024 // 200 MB // MaxDimension is the maximum width or height accepted by any // decoder. Images declaring larger dimensions are rejected before // pixel buffers are allocated. MaxDimension = 16384 // MaxPixels is the maximum total pixel count (width × height). Even a // 16384×16384 image would allocate roughly 800 MB of RGB data; this // tighter bound keeps worst-case memory usage reasonable and guards // against int overflow on 32-bit platforms (e.g. WASM). MaxPixels = 100 * 1000 * 1000 // 100M pixels )
Decoding limits protect against decompression bombs and malicious input. These are conservative defaults intended to handle realistic document images while bounding worst-case memory use. All bounds are enforced by the Load* and New* constructors before pixel buffers are allocated.
Variables ¶
var ( ErrFileTooLarge = errors.New("image: file exceeds maximum size") ErrDimensionTooLarge = errors.New("image: dimensions exceed maximum") ErrPixelCountTooLarge = errors.New("image: pixel count exceeds maximum") ErrDimensionInvalid = errors.New("image: invalid dimensions") )
Sentinel errors for input validation. Tests and callers can use errors.Is to distinguish these from decoder-produced errors.
Functions ¶
This section is empty.
Types ¶
type Image ¶
type Image struct {
// contains filtered or unexported fields
}
Image holds decoded image data ready for embedding in a PDF.
func LoadGIF ¶ added in v0.6.0
LoadGIF reads a GIF file from disk and creates an Image. Files larger than MaxFileSize are rejected with ErrFileTooLarge.
func LoadJPEG ¶
LoadJPEG reads a JPEG file from disk and creates an Image. Files larger than MaxFileSize are rejected with ErrFileTooLarge before being buffered into memory.
func LoadPNG ¶
LoadPNG reads a PNG file from disk and creates an Image. Files larger than MaxFileSize are rejected with ErrFileTooLarge.
func LoadTIFF ¶
LoadTIFF reads a TIFF file from disk and creates an Image. Files larger than MaxFileSize are rejected with ErrFileTooLarge.
func LoadWebP ¶ added in v0.6.0
LoadWebP reads a WebP file from disk and creates an Image. Files larger than MaxFileSize are rejected with ErrFileTooLarge.
func NewFromGoImage ¶
NewFromGoImage creates an Image from a Go image.RGBA. Pixel data is extracted as raw RGB bytes for FlateDecode embedding; if any pixel is non-opaque, a soft mask is generated.
NewFromGoImage returns nil when:
- src is nil
- dimensions are non-positive
- the stride is smaller than width*4 (would read past the pixel buffer)
- dimensions exceed MaxDimension or the pixel count exceeds MaxPixels
func NewGIF ¶ added in v0.6.0
NewGIF creates an Image from raw GIF data. Only the first frame of an animation is used; subsequent frames are discarded. Alpha is preserved as a soft mask if present. Dimensions are validated against MaxDimension and MaxPixels before the pixel buffer is allocated.
func NewJPEG ¶
NewJPEG creates an Image from raw JPEG data. It parses the JPEG header to extract dimensions and color space, rejecting dimensions that exceed the package limits (MaxDimension, MaxPixels).
When the JPEG is 4-component CMYK with an Adobe APP14 marker, the image is flagged for inverted-CMYK decoding: Photoshop-exported CMYK JPEGs store values in a convention opposite to what PDF viewers expect, so the resulting XObject is emitted with a /Decode array that flips every channel. This matches the behavior of poppler, mupdf, and Chrome.
func NewPNG ¶
NewPNG creates an Image from raw PNG data. It decodes the PNG and re-encodes pixels for FlateDecode embedding. Alpha channels are extracted into a separate soft mask. Dimensions are validated against MaxDimension and MaxPixels before the pixel buffer is allocated.
func NewTIFF ¶
NewTIFF creates an Image from raw TIFF data. It decodes the TIFF and re-encodes pixels as FlateDecode. Grayscale images are preserved as DeviceGray. Alpha channels are discarded because TIFF alpha is uncommon in PDF workflows. Dimensions are validated against MaxDimension and MaxPixels before the pixel buffer is allocated.
func NewWebP ¶ added in v0.6.0
NewWebP creates an Image from raw WebP data. Alpha is preserved as a soft mask if present. Dimensions are validated against MaxDimension and MaxPixels before the pixel buffer is allocated.
func (*Image) AspectRatio ¶
AspectRatio returns the ratio of width to height.
func (*Image) BuildXObject ¶
func (img *Image) BuildXObject(addObject func(core.PdfObject) *core.PdfIndirectReference) (*core.PdfIndirectReference, *core.PdfIndirectReference)
BuildXObject creates the PDF image XObject dictionary and stream. It returns the image XObject reference and, if the image has an alpha channel, a separate SMask XObject reference. The addObject callback registers each indirect object in the PDF file.