PaperForge.Images.PNG (PaperForge v0.2.0)

Copy Markdown View Source

Reads PNG metadata and image data for embedding in PDF image XObjects.

PNG image data is stored as zlib-compressed scanlines in IDAT chunks. PDF can consume non-alpha image data directly through /FlateDecode when paired with PNG predictor decode parameters.

For alpha PNGs, this module decodes PNG scanline filters, separates color bytes from alpha bytes, and returns a separate soft-mask stream.

This module supports non-interlaced 8-bit grayscale, RGB, grayscale with alpha, and RGBA PNG images. Adam7 interlacing and palette PNGs are intentionally unsupported.

Summary

Functions

Reads PNG metadata and concatenated compressed IDAT data.

Reads PNG metadata and raises when the binary is invalid or unsupported.

Returns whether a binary begins with the PNG signature.

Types

color_space()

@type color_space() :: :device_gray | :device_rgb

error_reason()

@type error_reason() ::
  :invalid_png
  | :missing_ihdr
  | :missing_idat
  | :truncated_chunk
  | :invalid_ihdr
  | :unsupported_compression
  | :unsupported_filter
  | :unsupported_interlace
  | :invalid_image_data
  | {:unsupported_color_type, non_neg_integer()}
  | {:unsupported_bit_depth, non_neg_integer(), non_neg_integer()}

metadata()

@type metadata() :: %{
  width: pos_integer(),
  height: pos_integer(),
  color_type: non_neg_integer(),
  bits_per_component: pos_integer(),
  components: pos_integer(),
  color_space: color_space(),
  compressed_data: binary(),
  smask_compressed_data: binary() | nil
}

Functions

parse(arg1)

@spec parse(binary()) :: {:ok, metadata()} | {:error, error_reason()}

Reads PNG metadata and concatenated compressed IDAT data.

parse!(data)

@spec parse!(binary()) :: metadata()

Reads PNG metadata and raises when the binary is invalid or unsupported.

png?(arg1)

@spec png?(binary()) :: boolean()

Returns whether a binary begins with the PNG signature.