Documentation
¶
Index ¶
- Variables
- func Decode(r io.Reader) (image.Image, error)
- func DecodeConfig(r io.Reader) (image.Config, error)
- func DecodeIgnoreAlphaFlag(r io.Reader) (image.Image, error)
- func Encode(w io.Writer, img image.Image, o *Options) error
- func EncodeAll(w io.Writer, ani *Animation, o *Options) error
- type Animation
- type BT601NYCbCrA
- type BT601NYCbCrAColor
- type BT601YCbCr
- type BT601YCbCrColor
- type Options
Constants ¶
This section is empty.
Variables ¶
var ( BT601YCbCrColorModel = color.ModelFunc(bt601YCbCrModel) BT601NYCbCrAColorModel = color.ModelFunc(bt601NYCbCrAModel) )
BT601YCbCrColorModel and BT601NYCbCrAColorModel convert an arbitrary color into the limited-range BT.601 representation.
Functions ¶
func Decode ¶
Decode reads a WebP image from the provided io.Reader and returns it as an image.Image.
For VP8L (lossless) sources the returned image is an *image.NRGBA (or compatible) from the underlying x/image/webp decoder. For VP8 (lossy) sources it is wrapped as a *BT601YCbCr (or *BT601NYCbCrA when the source has an ALPH chunk) so that img.At(x, y).RGBA() produces the colors a spec-compliant VP8 decoder would show; Go's stdlib color.YCbCrToRGB uses JFIF full-range math which shifts VP8 pixels by several units per channel.
Callers that specifically need the raw *image.YCbCr / *image.NYCbCrA (e.g. for zero-copy plane access) can type-assert to *BT601YCbCr or *BT601NYCbCrA and read the embedded field.
func DecodeConfig ¶
DecodeConfig reads the image configuration from the provided io.Reader without fully decoding the image.
This function is a wrapper around the underlying WebP decode package (golang.org/x/image/webp) and provides access to the image's metadata, such as its dimensions and color model. It is useful for obtaining image information before performing a full decode.
Parameters:
r - The source io.Reader containing the WebP encoded image.
Returns:
An image.Config containing the image's dimensions and color model, or an error if the configuration cannot be retrieved
func DecodeIgnoreAlphaFlag ¶
DecodeIgnoreAlphaFlag reads a WebP image from the provided io.Reader and returns it as an image.Image.
This function fixes x/image/webp rejecting VP8L images with the VP8X alpha flag, expecting an ALPHA chunk. VP8L handles transparency internally, and the WebP spec requires the flag for transparency.
This function is a wrapper around the underlying WebP decode package (golang.org/x/image/webp). It supports both lossy and lossless WebP formats, decoding the image accordingly.
Parameters:
r - The source io.Reader containing the WebP encoded image.
Returns:
The decoded image as image.Image or an error if the decoding fails.
func Encode ¶
Encode writes the provided image.Image to the specified io.Writer in WebP format.
This function always encodes the image using VP8L (lossless WebP). If `UseExtendedFormat` is enabled, it wraps the VP8L frame inside a VP8X container, allowing the use of metadata such as EXIF, ICC color profiles, or XMP metadata.
Note: VP8L already supports transparency, so VP8X is **not required** for alpha support.
Parameters:
w - The destination writer where the encoded WebP image will be written.
img - The input image to be encoded.
o - Pointer to Options containing encoding settings:
- UseExtendedFormat: If true, wraps the image in a VP8X container to enable
extended WebP features like metadata.
Returns:
An error if encoding fails or writing to the io.Writer encounters an issue.
func EncodeAll ¶
EncodeAll writes the provided animation sequence to the specified io.Writer in WebP format.
This function encodes a list of frames as a WebP animation using the VP8X container, which supports features like looping, frame timing, disposal methods, and background color settings. Each frame is individually compressed using the VP8L (lossless) format.
Note: Even if `UseExtendedFormat` is not explicitly set, animations always use the VP8X container because it is required for WebP animation support.
Parameters:
w - The destination writer where the encoded WebP animation will be written.
ani - Pointer to Animation containing the frames and animation settings:
- Images: List of frames to encode.
- Durations: Display times for each frame in milliseconds.
- Disposals: Disposal methods after frame display (keep or clear).
- LoopCount: Number of times the animation should loop (0 = infinite).
- BackgroundColor: Background color for the canvas, used when clearing.
o - Pointer to Options containing additional encoding settings:
- UseExtendedFormat: Currently unused for animations, but accepted for consistency.
Returns:
An error if encoding fails or writing to the io.Writer encounters an issue.
Types ¶
type Animation ¶
type Animation struct {
Images []image.Image
Durations []uint
Disposals []uint
LoopCount uint16
BackgroundColor uint32
}
Animation holds configuration settings for WebP animations.
It allows encoding a sequence of frames with individual timing and disposal options, supporting features like looping and background color settings.
Fields:
- Images: A list of frames to be displayed in sequence.
- Durations: Timing for each frame in milliseconds, matching the Images slice.
- Disposals: Disposal methods for frames after display; 0 = keep, 1 = clear to background.
- LoopCount: Number of times the animation should repeat; 0 means infinite looping.
- BackgroundColor: Canvas background color in BGRA order, used for clear operations.
type BT601NYCbCrA ¶ added in v0.1.1
BT601NYCbCrA wraps an *image.NYCbCrA for sources with an ALPH chunk.
func (*BT601NYCbCrA) BT601NYCbCrAAt ¶ added in v0.1.1
func (b *BT601NYCbCrA) BT601NYCbCrAAt(x, y int) BT601NYCbCrAColor
func (*BT601NYCbCrA) ColorModel ¶ added in v0.1.1
func (b *BT601NYCbCrA) ColorModel() color.Model
type BT601NYCbCrAColor ¶ added in v0.1.1
type BT601NYCbCrAColor struct {
BT601YCbCrColor
A uint8
}
BT601NYCbCrAColor is BT601YCbCrColor with a non-premultiplied alpha channel. Used by the WebP decoder output when the source has an ALPH chunk.
func (BT601NYCbCrAColor) RGBA ¶ added in v0.1.1
func (c BT601NYCbCrAColor) RGBA() (r, g, b, a uint32)
RGBA returns premultiplied 16-bit components.
type BT601YCbCr ¶ added in v0.1.1
BT601YCbCr wraps an *image.YCbCr whose samples are in VP8's limited-range BT.601. It forwards everything to the embedded image except At(), which returns BT601YCbCrColor for spec-correct RGB conversion.
Callers who specifically want the raw stdlib YCbCr (e.g. for fast plane-level access) can dereference the embedded *image.YCbCr field directly.
func (*BT601YCbCr) BT601YCbCrAt ¶ added in v0.1.1
func (b *BT601YCbCr) BT601YCbCrAt(x, y int) BT601YCbCrColor
BT601YCbCrAt is a typed helper that avoids the color.Color interface allocation when callers know they want BT601YCbCrColor.
func (*BT601YCbCr) ColorModel ¶ added in v0.1.1
func (b *BT601YCbCr) ColorModel() color.Model
type BT601YCbCrColor ¶ added in v0.1.1
type BT601YCbCrColor struct {
Y, Cb, Cr uint8
}
BT601YCbCrColor is a single pixel in VP8 limited-range BT.601 color space (luma 16–235, chroma 16–240), the color space WebP files actually store per RFC 6386.
It exists because Go's stdlib `image/color.YCbCrToRGB` applies the JFIF (full-range) inverse even to values that were produced under limited-range encoding, which shifts the resulting RGB by 2–5 units per channel. BT601YCbCrColor.RGBA() uses the spec-correct inverse:
R = clip((298·(Y-16) + 409·(Cr-128) + 128) >> 8) G = clip((298·(Y-16) - 100·(Cb-128) - 208·(Cr-128) + 128) >> 8) B = clip((298·(Y-16) + 516·(Cb-128) + 128) >> 8)
Decode returns images whose pixels are BT601YCbCrColor (wrapped in BT601YCbCr or BT601NYCbCrA) for VP8 sources, so calling img.At(x, y).RGBA() produces the colors a real VP8 decoder (libwebp, browsers) would show.
func (BT601YCbCrColor) RGBA ¶ added in v0.1.1
func (c BT601YCbCrColor) RGBA() (r, g, b, a uint32)
RGBA implements color.Color using the VP8 spec's limited-range BT.601 inverse. Returns premultiplied 16-bit components (α is always 0xffff for this type).
type Options ¶
Options holds configuration settings for WebP encoding.
Fields:
- UseExtendedFormat: If true, wraps the VP8L frame inside a VP8X container to enable metadata support. This does not affect image compression or encoding itself, as VP8L remains the encoding format.
- Lossy: If true, encode as VP8 (lossy). Otherwise (default) encode as VP8L (lossless), preserving existing behavior byte-for-byte.
- Quality: Lossy quality in [0, 100]. Higher values preserve more detail. Default (when zero) is 75. Ignored when Lossy is false.
- Method: Lossy speed/quality tradeoff in [0, 6]. Higher values spend more time searching for better compression. Default 4. Ignored when Lossy is false.