imagedecode

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 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 imagedecode decodes image bytes into premultiplied pixels for the backends that decode images themselves: PNG, JPEG, GIF and WebP, animated GIF and WebP with every frame composited, and SVG drawn by a rasterizer of its own from core.ParseSVG's drawing, as NSH's svg module draws with resvg. It is pure Go, so every target decodes alike.

Images come from the network, so decoding is bounded: an SVG document is at most MaxBytes (an .svgz once inflated) and its raster at most MaxSide a side and MaxPixels in all; a bitmap is at most MaxBitmapPixels; an animation keeps at most MaxFrames frames and MaxAnimationBytes of pixels. A panic while decoding is an error. Every backend's decodes Wait for one of as many slots as processors.

Index

Constants

View Source
const (
	// MaxBytes is the largest SVG document read, in bytes, an .svgz once
	// inflated.
	MaxBytes = 16 << 20
	// MaxSide is the longest side of an SVG raster, in pixels.
	MaxSide = 8192
	// MaxPixels is the most pixels of an SVG raster or an animation's
	// canvas: a full-screen image on a 5K display fits. An SVG asked for
	// larger is drawn smaller, keeping its aspect ratio.
	MaxPixels = 1 << 24
	// MaxBitmapPixels is the most pixels of a still bitmap: a
	// 48-megapixel photo fits. Larger ones are refused.
	MaxBitmapPixels = 1 << 26
	// Oversample is how many pixels per point Decode draws an SVG at
	// when Options asks for no size.
	Oversample = 4
	// MaxOversampledSide is the longest side of an oversampled SVG, in
	// pixels.
	MaxOversampledSide = 2048
	// MaxFrames is the most frames of an animation decoded; longer ones
	// stop at the last of them.
	MaxFrames = 600
	// MaxAnimationBytes is the most bytes an animation's frames take:
	// larger animations decode their first frame only.
	MaxAnimationBytes = 48 << 20
)

Bounds on what decoding one image takes, NSH's where it has them.

Variables

View Source
var (
	// ErrFormat is returned for bytes in no format Decode reads.
	ErrFormat = errors.New("imagedecode: unknown image format")
	// ErrTooLarge is returned for an SVG document over MaxBytes or a
	// bitmap over MaxBitmapPixels (an animation's canvas over MaxPixels).
	ErrTooLarge = errors.New("imagedecode: image too large")
	// ErrTooComplex is returned for an SVG whose drawing takes more work
	// than a document may ask for.
	ErrTooComplex = errors.New("imagedecode: SVG too complex to draw")
)

Functions

func AsPNG added in v0.2.0

AsPNG returns src with its Pixels, when it has them, as the Data of a PNG, for the backends that show images only from encoded bytes; it leaves any other source as it is. The PNG is written without compression, as it is decoded at once in the same process. Each call encodes anew: a backend keeps the PNG of the copy a view shows, which the core sends again when only the look changes.

func EncodePNG

func EncodePNG(pix []byte, width, height int, layout Layout) ([]byte, error)

EncodePNG encodes a premultiplied RGBA or BGRA pixel buffer as a straight-alpha PNG.

func FitPremultiplied

func FitPremultiplied(pix []byte, srcW, srcH, dstW, dstH int) []byte

FitPremultiplied fits a premultiplied 4-byte-per-pixel image (RGBA or BGRA) into a dstW×dstH buffer, centered with its aspect ratio preserved. Area averaging is used when downscaling and bilinear interpolation when upscaling.

func SVGSize

func SVGSize(data []byte) (size driver.Size, err error)

SVGSize returns an SVG's intrinsic size in points: its width and height, one of them and its viewBox's aspect ratio, or its viewBox's size.

func Size

func Size(data []byte) (size driver.Size, err error)

Size returns an image's size as an image view's EventLoad reports it, read from its header without decoding its pixels: a bitmap's pixels, an SVG's intrinsic size in whole points.

func TintPremultiplied

func TintPremultiplied(pix []byte, layout Layout, color driver.Color)

TintPremultiplied replaces the RGB channels of a premultiplied bitmap with color scaled by each pixel's alpha, preserving alpha.

func Wait

func Wait(ctx context.Context) (done func(), err error)

Wait waits for a slot to decode an image in, every backend's decoder or this package's, and returns the function that gives it back; it returns ctx's error instead if ctx ends first.

Types

type Format

type Format uint8

Format is an image file format.

const (
	Unknown Format = iota
	PNG
	JPEG
	GIF
	WebP
	SVG
)

func Sniff

func Sniff(data []byte) Format

Sniff tells an image's format from its first bytes; file names and MIME types are never trusted. An SVG is a document whose root element is <svg, after an optional byte order mark, XML declaration, comments and doctype, or such a document compressed with gzip (.svgz).

func (Format) String

func (f Format) String() string

type Frame

type Frame struct {
	// Pix holds Width*Height pixels of 4 premultiplied bytes in the
	// Options' Layout, rows top to bottom without padding.
	Pix []byte
	// Delay is how long an animation shows the frame; 0 for a still
	// image. Delays of 10ms or less, which tools write meaning "as fast as
	// possible", are 100ms, as browsers show them.
	Delay time.Duration
}

Frame is one picture of an Image.

type Image

type Image struct {
	Format Format
	// Width and Height are the frames' size in pixels.
	Width, Height int
	// Size is the image's intrinsic size: an SVG's in points (its width
	// and height, or its viewBox's), whatever size it was drawn at; a
	// bitmap's pixels.
	Size   driver.Size
	Frames []Frame
	// Plays is how many times an animation plays: 0 for ever.
	Plays int
}

Image is a decoded image: one frame, or the frames of an animation, each a whole picture of the same size.

func Decode

func Decode(data []byte, opts Options) (img *Image, err error)

Decode decodes an image in any format Sniff tells.

func FromPixels added in v0.2.0

func FromPixels(p *image.RGBA, layout Layout) *Image

FromPixels returns the pixels of an ImageSource, as the core copied them (driver.CopyPixels: at the origin, rows without padding), as a still image in layout: a copy, so the core's buffer stays as it was, and nothing decodes. Its size is a point a pixel.

func RasterizeSVG

func RasterizeSVG(s *driver.SVG, width, height int, color driver.Color, layout Layout) (*Image, error)

RasterizeSVG draws a parsed *driver.SVG into a width×height premultiplied bitmap in layout (BGRA or RGBA), painting currentColor with color (black when zero), fitted and centered while preserving aspect ratio.

type Layout

type Layout uint8

Layout is the byte order of a pixel. Pixels are always premultiplied.

const (
	// BGRA is blue, green, red and alpha, as Direct2D and Cairo take.
	BGRA Layout = iota
	// RGBA is red, green, blue and alpha, as image.RGBA holds.
	RGBA
)

type Options

type Options struct {
	Layout Layout
	// Cover, when positive, draws an SVG with its shorter side Cover
	// pixels long: given a view's longer side in pixels, the raster is
	// never enlarged however the view fits it (NSH's
	// svg::rasterize_covering).
	Cover int
	// Scale, when positive and Cover is not, draws an SVG at its
	// intrinsic size times Scale, a screen's scale factor
	// (svg::rasterize_scaled). With neither, an SVG is drawn at
	// Oversample pixels per point and at most MaxOversampledSide a side
	// (svg::rasterize_oversampled).
	Scale float32
}

Options control Decode. The zero value decodes premultiplied BGRA and draws an SVG oversampled.

Jump to

Keyboard shortcuts

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