rasterfit

package
v1.166.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package rasterfit reduces a raster image to fit a box, decoding it in the platform under a bound on the memory its pixels take (#2697). A browser decodes an image at its natural size, so its memory grows with the image's pixels rather than with the size it is shown at; the thumbnail renderer was taken past its memory limit by one photograph a mail attachment carried. The tile worker hands the renderer the reduced copy instead.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnreadable = errors.New("rasterfit: the image could not be decoded")

ErrUnreadable is an image whose header or pixels no decoder here reads. A browser may still read it (an animated WebP), so a caller can hand it on as stored.

Functions

func BytesPerPixel

func BytesPerPixel(model color.Model) int64

BytesPerPixel is what decoding an image of model holds per pixel. A JPEG's YCbCr counts 4: a progressive JPEG keeps its coefficients beside the image while it decodes.

func Decodes

func Decodes(contentType string) bool

Decodes reports whether an image of contentType is one this package reads.

func Fit

func Fit(ctx context.Context, data []byte, width, height int, maxBytes int64) (out []byte, reduced bool, err error)

Fit returns data as a PNG reduced to fit width x height, proportions and transparency kept, and reduced true. An image already within the box is returned unchanged with reduced false. An image whose decoded pixels would take more than maxBytes is a *TooLargeError, read from its header alone; one that cannot be decoded is ErrUnreadable. A context ended while waiting for the decode slot returns its error.

Types

type TooLargeError

type TooLargeError struct {
	Width, Height int
	Limit         int64
}

TooLargeError is an image whose header declares more decoded pixels than the bound allows.

func (*TooLargeError) Error

func (e *TooLargeError) Error() string

Jump to

Keyboard shortcuts

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