std/png
On this page
Index
- type
Model- method
Model.bytes_per_pixel
- method
- type
Image- method
Image.at
- method
- type
Config - type
Compression - type
PngErr- method
PngErr.display
- method
- fn
image - fn
decode - fn
decode_config - fn
encode
Types
Model
enum Model {
Gray
Gray16
Paletted
RGBA
NRGBA
RGBA64
NRGBA64
} derive Eq, DisplayThe image types image/png decodes to and encodes, each with Go’s pix
layout (rows of width * bytes with no padding):
Gray (1 byte a pixel), Gray16 (2, big endian), Paletted (1, an index
into palette), RGBA (4, alpha premultiplied; decode makes it from
opaque truecolor, so alpha is 255), NRGBA (4, not premultiplied),
RGBA64 (8, big endian 16 bit channels, premultiplied), NRGBA64 (8).
Model.bytes_per_pixel
fn (m: Model) bytes_per_pixel() -> IntThe bytes of one pixel: 1, 2, 4 or 8.
Image
struct Image {
width: Int
height: Int
model: Model
pix: Bytes
palette: List[Int] = []
}A raster image. pix has width * height * bytes per pixel bytes;
palette is only for Paletted, one 0xRRGGBBAA (not premultiplied)
per index, at most 256.
Image.at
fn (img: Image) at(x: Int, y: Int) -> IntThe pixel at (x, y) as 0xRRGGBBAA, 8 bits a channel and not
premultiplied (Go’s color.NRGBAModel on At(x, y), then the high byte of
each channel); a Paletted index past the palette reads as 0. Faults
for a point outside the image.
Config
struct Config {
width: Int
height: Int
model: Model
}The size and image type of a PNG (Go’s image.Config; the palette is not read).
Compression
enum Compression {
Default
None
Speed
Best
} derive EqGo’s png.CompressionLevel: Default (std/compress’s default level), None, Speed (compress level 1) or Best (level 9).
PngErr
enum PngErr {
Format(msg: Str)
Unsupported(msg: Str)
Truncated
Flate(msg: Str)
}Go’s errors: png: invalid format: ... (Format), png: unsupported feature: ... (Unsupported), unexpected EOF (Truncated), and the
zlib reader’s own errors, such as zlib: invalid header or flate: corrupt input before offset 12 (Flate); also Format for a PNG whose
header asks for more than max_pixels pixels, image too large.
PngErr.display
fn (e: PngErr) display() -> StrFunctions
image
fn image(width: Int, height: Int, model: Model) -> ImageA zeroed image of the model (pix all 0; a Paletted one has an empty
palette, set it before encode). Faults for a width or height under 1.
decode
fn decode(data: Bytes, max_pixels: Int = 100000000) -> Image ! PngErrGo’s png.Decode: the image type depends on the color type (gray and truecolor with a tRNS chunk, gray with alpha and truecolor with alpha are NRGBA or NRGBA64; opaque truecolor is RGBA or RGBA64; gray is Gray or Gray16; palettes are Paletted). Adam7 interlacing is read. A header over max_pixels pixels is refused before anything is allocated (Go allocates from the header).
decode_config
fn decode_config(data: Bytes) -> Config ! PngErrGo’s png.DecodeConfig: the size and image type without the pixels.
encode
fn encode(img: Image, level: Compression = Compression.Default) -> Bytes ! PngErrGo’s png.Encoder.Encode: Gray is 8 bit gray, Gray16 16 bit gray, a
Paletted image of 2, 4, 16 or more colors is 1, 2, 4 or 8 bit palette
indexed (the palette’s alpha goes in a tRNS chunk), RGBA and NRGBA are
8 bit truecolor when every pixel is opaque and truecolor with alpha
otherwise, RGBA64 and NRGBA64 likewise at 16 bits. The IDAT chunks hold
32,768 bytes of compressed data each. Faults when pix is not the size
the width, height and model say; Format for a size under 1 or a bad
palette length.