image

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
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

func LoadGIF(path string) (*Image, error)

LoadGIF reads a GIF file from disk and creates an Image. Files larger than MaxFileSize are rejected with ErrFileTooLarge.

func LoadJPEG

func LoadJPEG(path string) (*Image, error)

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

func LoadPNG(path string) (*Image, error)

LoadPNG reads a PNG file from disk and creates an Image. Files larger than MaxFileSize are rejected with ErrFileTooLarge.

func LoadTIFF

func LoadTIFF(path string) (*Image, error)

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

func LoadWebP(path string) (*Image, error)

LoadWebP reads a WebP file from disk and creates an Image. Files larger than MaxFileSize are rejected with ErrFileTooLarge.

func NewFromGoImage

func NewFromGoImage(src *goimage.RGBA) *Image

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

func NewGIF(data []byte) (*Image, error)

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

func NewJPEG(data []byte) (*Image, error)

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

func NewPNG(data []byte) (*Image, error)

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

func NewTIFF(data []byte) (*Image, error)

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

func NewWebP(data []byte) (*Image, error)

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

func (img *Image) AspectRatio() float64

AspectRatio returns the ratio of width to height.

func (*Image) BuildXObject

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.

func (*Image) Height

func (img *Image) Height() int

Height returns the image height in pixels.

func (*Image) Width

func (img *Image) Width() int

Width returns the image width in pixels.

Jump to

Keyboard shortcuts

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