Documentation
¶
Overview ¶
Package lilliput resizes and encodes images from compressed images
Index ¶
- Constants
- Variables
- func SetGIFMaxFrameDimension(dim uint64)
- type BlendMethod
- type Decoder
- type DisposeMethod
- type Encoder
- type Framebuffer
- func (f *Framebuffer) Clear()
- func (f *Framebuffer) ClearToTransparent(rect image.Rectangle) error
- func (f *Framebuffer) Close()
- func (f *Framebuffer) CopyToOffsetNoBlend(src *Framebuffer, rect image.Rectangle) error
- func (f *Framebuffer) CopyToOffsetWithAlphaBlending(src *Framebuffer, rect image.Rectangle) error
- func (f *Framebuffer) Create3Channel(width, height int) error
- func (f *Framebuffer) Create4Channel(width, height int) error
- func (f *Framebuffer) Duration() time.Duration
- func (f *Framebuffer) Fit(width, height int, dst *Framebuffer) error
- func (f *Framebuffer) Height() int
- func (f *Framebuffer) OrientationTransform(orientation ImageOrientation)
- func (f *Framebuffer) PixelType() PixelType
- func (f *Framebuffer) ResizeTo(width, height int, dst *Framebuffer) error
- func (f *Framebuffer) Width() int
- type ImageHeader
- type ImageOps
- type ImageOpsSizeMethod
- type ImageOptions
- type ImageOrientation
- type PixelType
Constants ¶
const ( // Standard image encoding constants JpegQuality = int(C.CV_IMWRITE_JPEG_QUALITY) // Quality parameter for JPEG encoding (0-100) PngCompression = int(C.CV_IMWRITE_PNG_COMPRESSION) // Compression level for PNG encoding (0-9) WebpQuality = int(C.CV_IMWRITE_WEBP_QUALITY) // Quality parameter for WebP encoding (0-100) JpegProgressive = int(C.CV_IMWRITE_JPEG_PROGRESSIVE) // Enable progressive JPEG encoding // Image orientation constants OrientationTopLeft = ImageOrientation(C.CV_IMAGE_ORIENTATION_TL) OrientationTopRight = ImageOrientation(C.CV_IMAGE_ORIENTATION_TR) OrientationBottomRight = ImageOrientation(C.CV_IMAGE_ORIENTATION_BR) OrientationBottomLeft = ImageOrientation(C.CV_IMAGE_ORIENTATION_BL) OrientationLeftTop = ImageOrientation(C.CV_IMAGE_ORIENTATION_LT) OrientationRightTop = ImageOrientation(C.CV_IMAGE_ORIENTATION_RT) OrientationRightBottom = ImageOrientation(C.CV_IMAGE_ORIENTATION_RB) OrientationLeftBottom = ImageOrientation(C.CV_IMAGE_ORIENTATION_LB) )
Variables ¶
var ( ErrInvalidImage = errors.New("unrecognized image format") ErrDecodingFailed = errors.New("failed to decode image") ErrBufTooSmall = errors.New("buffer too small to hold image") ErrFrameBufNoPixels = errors.New("Framebuffer contains no pixels") ErrSkipNotSupported = errors.New("skip operation not supported by this decoder") ErrEncodeTimeout = errors.New("encode timed out") )
var (
ErrGifEncoderNeedsDecoder = errors.New("GIF encoder needs decoder used to create image")
)
Functions ¶
func SetGIFMaxFrameDimension ¶
func SetGIFMaxFrameDimension(dim uint64)
SetGIFMaxFrameDimension sets the largest GIF width/height that can be decoded. This helps prevent loading extremely large GIF images that could exhaust memory.
Types ¶
type BlendMethod ¶
type BlendMethod int
BlendMethod describes how the previous frame should be blended with the next frame.
const ( // UseAlphaBlending indicates alpha blending should be used when compositing frames UseAlphaBlending BlendMethod = iota // NoBlend indicates frames should be copied directly without blending NoBlend )
type Decoder ¶
type Decoder interface {
// Header returns basic image metadata from the image.
// This is done lazily, reading only the first part of the image and not
// a full decode.
Header() (*ImageHeader, error)
// Close releases any resources associated with the Decoder
Close()
// Description returns a string description of the image type, such as
// "PNG"
Description() string
// Duration returns the duration of the content. This property is 0 for
// static images and animated GIFs.
Duration() time.Duration
// DecodeTo fully decodes the image pixel data into f. Generally users should
// prefer instead using the ImageOps object to decode images.
DecodeTo(f *Framebuffer) error
// SkipFrame skips a frame if the decoder supports multiple frames
// and returns io.EOF if the last frame has been reached
SkipFrame() error
// IsStreamable indicates whether the content is optimized for streaming. This is true
// for static images and animated GIFs.
IsStreamable() bool
// HasSubtitles indicates whether the content has one or more subtitle tracks.
HasSubtitles() bool
// BackgroundColor as BGRA
BackgroundColor() uint32
// ICC returns the ICC color profile, if any
ICC() []byte
// LoopCount() returns the number of loops in the image
LoopCount() int
}
A Decoder decompresses compressed image data.
func NewDecoder ¶
NewDecoder returns a Decoder which can be used to decode image data provided in buf with tone mapping enabled.
func NewDecoderWithOptionalToneMapping ¶
NewDecoderWithOptionalToneMapping returns a Decoder which can be used to decode image data provided in buf with tone mapping optionally enabled. If the first few bytes of buf do not point to a valid magic string, an error will be returned.
type DisposeMethod ¶
type DisposeMethod int
DisposeMethod describes how the previous frame should be disposed before rendering the next frame.
const ( // NoDispose indicates the previous frame should remain as-is NoDispose DisposeMethod = iota // DisposeToBackgroundColor indicates the previous frame area should be cleared to background color DisposeToBackgroundColor )
type Encoder ¶
type Encoder interface {
// Encode encodes the pixel data in f into the dst provided to NewEncoder. Encode quality
// options can be passed into opt, such as map[int]int{lilliput.JpegQuality: 80}
Encode(f *Framebuffer, opt map[int]int) ([]byte, error)
// Close releases any resources associated with the Encoder
Close()
}
An Encoder compresses raw pixel data into a well-known image type.
func NewEncoder ¶
NewEncoder returns an Encode which can be used to encode Framebuffer into compressed image data. ext should be a string like ".jpeg" or ".png". decodedBy is optional and can be the Decoder used to make the Framebuffer. dst is where an encoded image will be written.
type Framebuffer ¶
type Framebuffer struct {
// contains filtered or unexported fields
}
Framebuffer contains an array of raw, decoded pixel data.
func NewFramebuffer ¶
func NewFramebuffer(width, height int) *Framebuffer
NewFramebuffer creates a backing store for a pixel frame buffer with the specified dimensions.
func (*Framebuffer) Clear ¶
func (f *Framebuffer) Clear()
Clear resets all pixel data in Framebuffer for the active frame and resets the mat if it exists.
func (*Framebuffer) ClearToTransparent ¶
func (f *Framebuffer) ClearToTransparent(rect image.Rectangle) error
ClearToTransparent clears a rectangular region of the framebuffer to transparent.
func (*Framebuffer) Close ¶
func (f *Framebuffer) Close()
Close releases the resources associated with Framebuffer.
func (*Framebuffer) CopyToOffsetNoBlend ¶
func (f *Framebuffer) CopyToOffsetNoBlend(src *Framebuffer, rect image.Rectangle) error
CopyToOffsetNoBlend copies the source framebuffer to a specified rectangle within the destination framebuffer. This function does not perform any blending.
func (*Framebuffer) CopyToOffsetWithAlphaBlending ¶
func (f *Framebuffer) CopyToOffsetWithAlphaBlending(src *Framebuffer, rect image.Rectangle) error
CopyToOffsetWithAlphaBlending copies the source framebuffer to a specified rectangle within the destination framebuffer. This function performs alpha blending.
func (*Framebuffer) Create3Channel ¶
func (f *Framebuffer) Create3Channel(width, height int) error
Create3Channel initializes the framebuffer for 3-channel (RGB) image data.
func (*Framebuffer) Create4Channel ¶
func (f *Framebuffer) Create4Channel(width, height int) error
Create4Channel initializes the framebuffer for 4-channel (RGBA) image data.
func (*Framebuffer) Duration ¶
func (f *Framebuffer) Duration() time.Duration
Duration returns the length of time this frame plays out in an animated image
func (*Framebuffer) Fit ¶
func (f *Framebuffer) Fit(width, height int, dst *Framebuffer) error
Fit performs a resizing and cropping transform on the Framebuffer and puts the result in the provided destination Framebuffer. This function does preserve aspect ratio but will crop columns or rows from the edges of the image as necessary in order to keep from stretching the image content. Returns an error if the destination is not large enough to hold the given dimensions.
func (*Framebuffer) Height ¶
func (f *Framebuffer) Height() int
Height returns the height of the contained pixel data in number of pixels. This may differ from the capacity of the framebuffer.
func (*Framebuffer) OrientationTransform ¶
func (f *Framebuffer) OrientationTransform(orientation ImageOrientation)
OrientationTransform rotates and/or mirrors the Framebuffer according to the given orientation. Passing the orientation from ImageHeader will normalize the orientation.
func (*Framebuffer) PixelType ¶
func (f *Framebuffer) PixelType() PixelType
PixelType returns the PixelType information of the contained pixel data, if any.
func (*Framebuffer) ResizeTo ¶
func (f *Framebuffer) ResizeTo(width, height int, dst *Framebuffer) error
ResizeTo performs a resizing transform on the Framebuffer and puts the result in the provided destination Framebuffer. This function does not preserve aspect ratio if the given dimensions differ in ratio from the source. Returns an error if the destination is not large enough to hold the given dimensions.
func (*Framebuffer) Width ¶
func (f *Framebuffer) Width() int
Width returns the width of the contained pixel data in number of pixels. This may differ from the capacity of the framebuffer.
type ImageHeader ¶
type ImageHeader struct {
// contains filtered or unexported fields
}
ImageHeader contains basic decoded image metadata.
func (*ImageHeader) ContentLength ¶
func (h *ImageHeader) ContentLength() int
ContentLength returns the length of the necessary image data. Data past this point can be safely truncated using data[:h.ContentLength()]. This helps handle padding bytes and potential unwanted trailing data. This could be applicable to images with unwanted data at the end (e.g. "acropalypse" bug).
func (*ImageHeader) HasAlpha ¶
func (h *ImageHeader) HasAlpha() bool
HasAlpha returns true if the image has an alpha channel.
func (*ImageHeader) Height ¶
func (h *ImageHeader) Height() int
Height returns the height of the image in number of pixels.
func (*ImageHeader) IsAnimated ¶
func (h *ImageHeader) IsAnimated() bool
IsAnimated returns true if the image contains multiple frames.
func (*ImageHeader) Orientation ¶
func (h *ImageHeader) Orientation() ImageOrientation
Orientation returns the metadata-based image orientation.
func (*ImageHeader) PixelType ¶
func (h *ImageHeader) PixelType() PixelType
PixelType returns a PixelType describing the image's pixels.
func (*ImageHeader) Width ¶
func (h *ImageHeader) Width() int
Width returns the width of the image in number of pixels.
type ImageOps ¶
type ImageOps struct {
// contains filtered or unexported fields
}
ImageOps is a reusable object that can resize and encode images.
func NewImageOps ¶
NewImageOps creates a new ImageOps object that will operate on images up to maxSize on each axis. It initializes two framebuffers for double-buffering operations.
func (*ImageOps) Clear ¶
func (o *ImageOps) Clear()
Clear frees the pixel data held in all framebuffers. While not required between Transform operations, you can call this to reduce memory usage when the ImageOps object will be idle for a while.
func (*ImageOps) Close ¶
func (o *ImageOps) Close()
Close releases resources associated with ImageOps
func (*ImageOps) Transform ¶
Transform performs the requested transform operations on the Decoder specified by d. The result is written into the output buffer dst. A new slice pointing to dst is returned with its length set to the length of the resulting image. Errors may occur if the decoded image is too large for ImageOps or if Encoding fails.
It is important that .Decode() not have been called already on d.
type ImageOpsSizeMethod ¶
type ImageOpsSizeMethod int
const ( ImageOpsNoResize ImageOpsSizeMethod = iota ImageOpsFit ImageOpsResize )
type ImageOptions ¶
type ImageOptions struct {
// FileType should be a string starting with '.', e.g.
// ".jpeg"
FileType string
// Width controls the width of the output image
Width int
// Height controls the height of the output image
Height int
// ResizeMethod controls how the image will be transformed to
// its output size. Notably, ImageOpsFit will do a cropping
// resize, while ImageOpsResize will stretch the image.
ResizeMethod ImageOpsSizeMethod
// NormalizeOrientation will flip and rotate the image as necessary
// in order to undo EXIF-based orientation
NormalizeOrientation bool
// EncodeOptions controls the encode quality options
EncodeOptions map[int]int
// MaxEncodeFrames controls the maximum number of frames that will be resized
MaxEncodeFrames int
// MaxEncodeDuration controls the maximum duration of animated image that will be resized
MaxEncodeDuration time.Duration
// This is a best effort timeout when encoding multiple frames
EncodeTimeout time.Duration
// DisableAnimatedOutput controls the encoder behavior when given a multi-frame input
DisableAnimatedOutput bool
}
ImageOptions controls how ImageOps resizes and encodes the pixel data decoded from a Decoder
type ImageOrientation ¶
type ImageOrientation int
ImageOrientation describes how the decoded image is oriented according to its metadata.