Documentation
¶
Index ¶
- Constants
- Variables
- func Decode(r io.Reader) (image.Image, error)
- func DecodeConfig(r io.Reader) (image.Config, error)
- func Encode(w io.Writer, m image.Image, opt *EncodeOptions) error
- func EncodeBytes(m image.Image, opt *EncodeOptions) ([]byte, error)
- type ALPH
- type ANIM
- type ANMF
- type ChunkParse
- type DecodeOptions
- type EXIF
- type EncodeOptions
- type FourCC
- type ICCP
- type PixelFormat
- type RawChunk
- type VP8
- type VP8L
- type VP8X
- type Webp
- func (w *Webp) AddRawChunk(name FourCC, data []byte) error
- func (w *Webp) At(x, y int) color.Color
- func (w *Webp) Bounds() image.Rectangle
- func (w *Webp) ColorModel() color.Model
- func (w *Webp) DeleteRawChunk(name FourCC) int
- func (w *Webp) Frames(maxPixels int) ([]*image.NRGBA, []int, error)
- func (w *Webp) GetOtherChunkByName(name FourCC) ([]ChunkParse, error)
- func (w *Webp) Image() (image.Image, error)
- func (w *Webp) MarshalBytes() ([]byte, error)
- func (w *Webp) ParseChunk(c ChunkParse, notSave ...bool) error
- func (w *Webp) RawChunkByName(name FourCC) ([]byte, bool)
- func (w *Webp) RawChunks() []RawChunk
- func (w *Webp) ToImage() (image.Image, error)
- func (w *Webp) ToImageWith(opt DecodeOptions) (image.Image, error)
- func (w *Webp) WriteTo(out io.Writer) (int64, error)
- type XMP
Constants ¶
const ( FlagICC uint8 = 0x20 FlagAlpha uint8 = 0x10 FlagEXIF uint8 = 0x08 FlagXMP uint8 = 0x04 FlagAnimation uint8 = 0x02 )
VP8X flags, the bit positions defined by the container specification.
const ( ALPHCompressionNone uint8 = 0 ALPHCompressionLossless uint8 = 1 )
ALPH alpha channel compression methods.
const ( ALPHFilterNone uint8 = 0 ALPHFilterHorizontal uint8 = 1 ALPHFilterVertical uint8 = 2 ALPHFilterGradient uint8 = 3 )
ALPH alpha channel filtering methods.
Variables ¶
var ( // ErrNotWebP is returned when the stream does not start with RIFF....WEBP. ErrNotWebP = errors.New("invalid webp: bad RIFF/WEBP signature") // ErrChunkNotFound is returned by ParseChunk when no chunk of the // requested type is left in the stream. ErrChunkNotFound = errors.New("chunk not found") // ErrUnsupported is returned for valid but unimplemented webp features. ErrUnsupported = errors.New("unsupported webp feature") // ErrImageTooLarge is returned when the image exceeds the configured // pixel budget. ErrImageTooLarge = errors.New("webp image too large") )
Sentinel errors returned by the parser and the decoders.
Functions ¶
func Decode ¶
Decode reads a webp stream and returns the decoded image, implementing the image.Decode signature.
func DecodeConfig ¶
DecodeConfig reads only the container header, it does not decode any pixel data.
func EncodeBytes ¶
func EncodeBytes(m image.Image, opt *EncodeOptions) ([]byte, error)
EncodeBytes encodes m as a lossless WebP file held in memory.
Types ¶
type ALPH ¶
ALPH carries the alpha plane of a lossy image.
0 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +-+-+-+-+-+-+-+-+ |C|F|P|Rsv|
C: compression method (0 none, 1 lossless), F: filtering method, P: preprocessing (0 none, 1 level reduction).
type ANIM ¶
type ANIM struct {
// BackgroundColor is stored as 0xAARRGGBB in the file's BGRA byte order.
BackgroundColor uint32
LoopCount uint16
}
ANIM holds the global animation parameters.
Background Color: 32 bits (BGRA) Loop Count: 16 bits
type ANMF ¶
type ANMF struct {
X, Y int
Width, Height int
Duration int
// Blend is true when the frame is alpha blended onto the canvas, false
// when it overwrites the canvas region.
Blend bool
// Dispose is true when the canvas is cleared to the background color
// after the frame was displayed.
Dispose bool
ALPH *ALPH
VP8 *VP8
VP8L *VP8L
}
ANMF is one animation frame.
Frame X: 24 bits (actual x / 2) Frame Y: 24 bits (actual y / 2) Frame Width: 24 bits (width - 1) Frame Height: 24 bits (height - 1) Duration: 24 bits (milliseconds) Flags: 8 bits (bit1 blending, bit0 disposal) Frame Data: ALPH (optional) + VP8 / VP8L
type ChunkParse ¶
ChunkParse is the interface a custom chunk parser has to implement, the same extension point simple-png exposes.
type DecodeOptions ¶
type DecodeOptions struct {
// Format selects the returned pixel layout.
Format PixelFormat
// Frame selects the frame of an animated file, 0 is the first frame.
Frame int
// MaxPixels rejects larger images, 0 uses defaultMaxPixels.
MaxPixels int
}
DecodeOptions tunes the pixel decoding.
type EncodeOptions ¶
type EncodeOptions struct {
// Lossless is informational: this library only writes lossless webp, so
// the value is accepted and ignored.
Lossless bool
// Method trades speed for size: 0 stores literals only, 1 adds the
// subtract green transform, colour indexing and LZ77, 2 (the default)
// also applies a predictor transform.
Method int
}
EncodeOptions tunes the lossless encoder.
type FourCC ¶
type FourCC string
FourCC is a 4 byte chunk type code, e.g. "VP8L".
const ( RIFFChunk FourCC = "RIFF" WEBPChunk FourCC = "WEBP" VP8XChunk FourCC = "VP8X" VP8Chunk FourCC = "VP8 " VP8LChunk FourCC = "VP8L" ALPHChunk FourCC = "ALPH" ANIMChunk FourCC = "ANIM" ANMFChunk FourCC = "ANMF" ICCPChunk FourCC = "ICCP" EXIFChunk FourCC = "EXIF" XMPChunk FourCC = "XMP " )
The chunk types of the WebP container, see https://developers.google.com/speed/webp/docs/riff_container
type PixelFormat ¶
type PixelFormat int
PixelFormat selects the pixel layout of the decoded image.
const ( // PixelFormatNRGBA returns an *image.NRGBA, the straight alpha layout // used by the png decoder for RGBA images. It is the default. PixelFormatNRGBA PixelFormat = iota // PixelFormatRGBA returns an *image.RGBA, the premultiplied layout. PixelFormatRGBA )
type RawChunk ¶
type RawChunk struct {
// Name is the 4 byte chunk type code.
Name FourCC
// Data is the chunk payload without header and padding.
Data []byte
}
RawChunk is a chunk that was not decoded natively and is kept in its raw form, e.g. an unknown application specific chunk.
type VP8 ¶
VP8 is the lossy bitstream chunk. The payload is kept untouched: WebP lossy images are single VP8 key frames, the header is parsed here so the dimensions are known without decoding the frame.
type VP8L ¶
VP8L is the lossless bitstream chunk, see https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification
The 5 byte header is:
signature 8 bits (0x2f) width - 1 14 bits height - 1 14 bits alpha_is_used 1 bit version_number 3 bits (must be 0)
type VP8X ¶
VP8X
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Rsv|I|L|E|X|A|R| Reserved | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Canvas Width Minus One | ... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ... Canvas Height Minus One | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
I: ICC profile, L: alpha, E: EXIF, X: XMP, A: animation.
func (*VP8X) HasAnimation ¶
type Webp ¶
type Webp struct {
Width, Height int
HasAlpha bool
HasAnimation bool
VP8X *VP8X
VP8 *VP8
VP8L *VP8L
ALPH *ALPH
ANIM *ANIM
ANMFs []*ANMF
ICCP *ICCP
EXIF *EXIF
XMP *XMP
// OtherChunk holds the custom chunks parsed through ParseChunk, grouped
// by chunk type.
OtherChunk map[FourCC][]ChunkParse
sync.RWMutex
sync.Once
// contains filtered or unexported fields
}
Webp is a parsed WebP container. The chunk payloads of the image data are kept as raw bytes, the parsed structs are views on them, so metadata can be edited and written back without re-encoding the pixels.
func ParseWebp ¶
ParseWebp parses a webp stream: it reads the RIFF header, all chunks and decodes the container chunks (VP8X, VP8, VP8L, ALPH, ANIM, ANMF, ICCP, EXIF, XMP). Chunks of unknown type are kept in raw form, see RawChunks.
The RIFF size field is validated only when verifySize is true.
func ParseWebpBytes ¶
ParseWebpBytes parses a webp stream held in memory.
func (*Webp) AddRawChunk ¶
AddRawChunk inserts a new raw chunk into the stream before the first image chunk (VP8, VP8L or ANMF); if there is no image chunk it is appended. Use it e.g. to add an EXIF or XMP chunk to an already parsed file.
func (*Webp) At ¶
At implements image.Image. The image is decoded on the first call and then cached.
func (*Webp) ColorModel ¶
ColorModel implements image.Image. The decoders produce straight alpha NRGBA pixels.
func (*Webp) DeleteRawChunk ¶
DeleteRawChunk removes every chunk of the given type from the stream and returns how many chunks were removed. It works for the metadata chunks that are decoded natively (ICCP, EXIF, XMP) as well as for unknown raw chunks; a WriteTo afterwards writes the stream without those chunks.
func (*Webp) Frames ¶
Frames decodes every frame of an animated file and returns the composited frames at canvas size together with their display duration in milliseconds. The pixel budget is forwarded to the sub-decoders.
func (*Webp) GetOtherChunkByName ¶
func (w *Webp) GetOtherChunkByName(name FourCC) ([]ChunkParse, error)
GetOtherChunkByName returns the custom chunk parsers recorded for a type.
func (*Webp) Image ¶
Image decodes the image once and caches the result, it backs the image.Image implementation of Webp.
func (*Webp) MarshalBytes ¶
MarshalBytes returns the webp stream of the current state as a byte slice, see WriteTo for the details.
func (*Webp) ParseChunk ¶
func (w *Webp) ParseChunk(c ChunkParse, notSave ...bool) error
ParseChunk parses the first remaining chunk whose type matches c.ChunkName(). After a successful parse the chunk is consumed and removed from the raw chunk list. Unless notSave is true, the parser instance is recorded in OtherChunk and can be retrieved later with GetOtherChunkByName.
func (*Webp) RawChunkByName ¶
RawChunkByName returns the raw payload of the first still-present chunk of the given type.
func (*Webp) RawChunks ¶
RawChunks returns all chunks that are still left after base parsing, in the order they appear in the file.
func (*Webp) ToImage ¶
ToImage decodes the image and returns it as an image.Image. Lossless files are decoded by the VP8L decoder, lossy files by the VP8 decoder with the ALPH alpha channel applied. For animated files the first frame is returned.
func (*Webp) ToImageWith ¶
func (w *Webp) ToImageWith(opt DecodeOptions) (image.Image, error)
ToImageWith decodes the image with the given options.
func (*Webp) WriteTo ¶
WriteTo writes the webp stream of the current state, implementing io.WriterTo. Chunks that were consumed while parsing are re-serialized from their parsed structs, so modifications made to the structs (or chunks added/deleted through AddRawChunk / DeleteRawChunk) are reflected, while the chunks that were left raw are copied verbatim. Parsing and writing back a file without touching anything reproduces it byte for byte.
The image payload (VP8 / VP8L / ALPH) is never re-encoded, so editing the metadata chunks does not touch the pixels at all.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
vp8l
Package vp8l implements the WebP lossless bitstream decoder, see https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification
|
Package vp8l implements the WebP lossless bitstream decoder, see https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification |
|
Package wasm exposes the pure-Go simple-webp decoder to WebAssembly / JavaScript.
|
Package wasm exposes the pure-Go simple-webp decoder to WebAssembly / JavaScript. |
|
main
command
Command wasm is the WebAssembly entry point.
|
Command wasm is the WebAssembly entry point. |