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
- Variables
- func AsPNG(src driver.ImageSource) driver.ImageSource
- func EncodePNG(pix []byte, width, height int, layout Layout) ([]byte, error)
- func FitPremultiplied(pix []byte, srcW, srcH, dstW, dstH int) []byte
- func SVGSize(data []byte) (size driver.Size, err error)
- func Size(data []byte) (size driver.Size, err error)
- func TintPremultiplied(pix []byte, layout Layout, color driver.Color)
- func Wait(ctx context.Context) (done func(), err error)
- type Format
- type Frame
- type Image
- type Layout
- type Options
Constants ¶
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 ¶
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
func AsPNG(src driver.ImageSource) driver.ImageSource
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 ¶
EncodePNG encodes a premultiplied RGBA or BGRA pixel buffer as a straight-alpha PNG.
func FitPremultiplied ¶
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 ¶
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 ¶
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 ¶
TintPremultiplied replaces the RGB channels of a premultiplied bitmap with color scaled by each pixel's alpha, preserving alpha.
Types ¶
type Format ¶
type Format uint8
Format is an image file format.
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 FromPixels ¶ added in v0.2.0
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.
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.