Documentation
¶
Overview ¶
Package simple_webp 解析与编辑 WebP(RIFF)容器:读取 RIFF 头与全部 chunk, 解码容器级 chunk(VP8X、VP8、VP8L、ALPH、ANIM、ANMF、ICCP、EXIF、XMP), 并支持在不重新编码像素的前提下增删元数据 chunk、按原始顺序写回整个流。 纯 Go 实现,不依赖 cgo 与 libwebp;浏览器与 Node.js 用的 WebAssembly (WASM)解码前端在 wasm 子包里。
Package simple_webp parses and edits WebP (RIFF) containers: it reads the RIFF header and every chunk, decodes the container-level chunks (VP8X, VP8, VP8L, ALPH, ANIM, ANMF, ICCP, EXIF, XMP), and supports adding or removing metadata chunks and writing the stream back in its original order without re-encoding the pixels. It is pure Go — no cgo, no libwebp — and the WebAssembly (WASM) decoder front end for the browser and Node.js lives in the wasm subpackage.
Index ¶
- Constants
- Variables
- func Decode(r io.Reader) (image.Image, error)
- func DecodeConfig(r io.Reader) (image.Config, error)
- func DecodeWebp(r io.Reader, opt DecodeOptions) (image.Image, error)
- func Encode(w io.Writer, m image.Image, opt *EncodeOptions) error
- func EncodeAnimation(w io.Writer, a *Animation, opt *AnimationOptions) error
- func EncodeAnimationBytes(a *Animation, opt *AnimationOptions) ([]byte, error)
- func EncodeBytes(m image.Image, opt *EncodeOptions) ([]byte, error)
- func EncodeLossy(w io.Writer, m image.Image, opt *LossyOptions) error
- func EncodeLossyBytes(m image.Image, opt *LossyOptions) ([]byte, error)
- type ALPH
- type ANIM
- type ANMF
- type Animation
- type AnimationInfo
- type AnimationOptions
- type ChunkParse
- type DecodeOptions
- type EXIF
- type EncodeOptions
- type FourCC
- type Frame
- type FrameOptions
- type ICCP
- type ImageOptions
- type LossyOptions
- type ParseOptions
- type PixelFormat
- type RawChunk
- type VP8
- type VP8L
- type VP8X
- type Webp
- func (w *Webp) AddFrame(f *ANMF) error
- func (w *Webp) AddRawChunk(name FourCC, data []byte) error
- func (w *Webp) AnimationInfo() (AnimationInfo, 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) DecodeInto(dst *image.NRGBA) error
- func (w *Webp) DeleteRawChunk(name FourCC) int
- func (w *Webp) FrameAt(i int, opt FrameOptions) (*image.NRGBA, int, error)
- func (w *Webp) Frames(maxPixels int) ([]*image.NRGBA, []int, error)
- func (w *Webp) FramesWith(opt FrameOptions) ([]*image.NRGBA, []int, error)
- func (w *Webp) GetOtherChunkByName(name FourCC) ([]ChunkParse, error)
- func (w *Webp) Image() (image.Image, error)
- func (w *Webp) InvalidateCache()
- 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) RemoveFrame(i int) error
- func (w *Webp) SetImage(m image.Image, opt *ImageOptions) error
- 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 特性标志位,位位置由 WebP 容器规范定义。 VP8X feature flags; the bit positions are defined by the container spec.
const ( ALPHCompressionNone uint8 = 0 ALPHCompressionLossless uint8 = 1 )
ALPH 的 alpha 通道压缩方法常量。 ALPH alpha channel compression methods.
const ( ALPHFilterNone uint8 = 0 ALPHFilterHorizontal uint8 = 1 ALPHFilterVertical uint8 = 2 ALPHFilterGradient uint8 = 3 )
ALPH 的 alpha 通道过滤方法常量。 ALPH alpha channel filtering methods.
Variables ¶
var ( // ErrNotWebP 在流不以 RIFF....WEBP 签名开头时返回。 // ErrNotWebP is returned when the stream does not start with RIFF....WEBP. ErrNotWebP = errors.New("invalid webp: bad RIFF/WEBP signature") // ErrChunkNotFound 在 ParseChunk 找不到所请求类型的剩余 chunk 时返回。 // ErrChunkNotFound is returned by ParseChunk when no chunk of the // requested type is left in the stream. ErrChunkNotFound = errors.New("chunk not found") // ErrUnsupported 对应格式合法但本库尚未实现的 WebP 特性。 // ErrUnsupported is returned for valid but unimplemented webp features. ErrUnsupported = errors.New("unsupported webp feature") // ErrImageTooLarge 在图像像素数超出配置的像素预算时返回。 // ErrImageTooLarge is returned when the image exceeds the configured // pixel budget. ErrImageTooLarge = errors.New("webp image too large") )
解析器与解码器共用的哨兵错误,调用方可用 errors.Is 判断具体类型。
Sentinel errors returned by the parser and the decoders; callers can test them with errors.Is.
Functions ¶
func Decode ¶
Decode 读取 webp 流并返回解码后的图像,实现 image.Decode 签名。 动图会返回合成后的画布帧。
Decode reads a webp stream and returns the decoded image, implementing the image.Decode signature. For animated files it returns the composited canvas frame.
func DecodeConfig ¶
DecodeConfig 只读取容器头部(RIFF 头 + 第一个决定尺寸的 chunk), 不解码任何像素数据,因此开销很小。VP8X、VP8、VP8L 三种 chunk 都被 支持;颜色模型固定返回 NRGBAModel。
DecodeConfig reads only the container header (the RIFF header plus the first chunk that determines the size) and decodes no pixel data at all, so it is cheap. It supports the VP8X, VP8 and VP8L chunks; the color model is always NRGBAModel.
func DecodeWebp ¶ added in v0.2.8
DecodeWebp 从任意 io.Reader 流式解析并解码一个 WebP 文件,等价于 ParseWebp(r) 后接 ToImageWith(opt)。容器按 chunk 逐个流入,调用方不需要 事先把整个文件读进内存——文件、网络流、bytes.Reader 都可以直接喂进来。
DecodeWebp parses and decodes a webp file from any io.Reader, the streaming counterpart of ParseWebp(r) followed by ToImageWith(opt). The container streams in chunk by chunk, so the caller never needs the whole file in memory: files, network streams and bytes.Reader all feed straight in.
func EncodeAnimation ¶ added in v0.2.7
func EncodeAnimation(w io.Writer, a *Animation, opt *AnimationOptions) error
EncodeAnimation 把 a 编码为动图 WebP 并写入 w。
EncodeAnimation writes a as an animated WebP file.
func EncodeAnimationBytes ¶ added in v0.2.7
func EncodeAnimationBytes(a *Animation, opt *AnimationOptions) ([]byte, error)
EncodeAnimationBytes 把 a 编码为完整的动图 WebP 文件并保留在内存中返回。 每帧独立编码(不做帧间差分),带透明度的帧写出 ALPH 子 chunk,容器顺序为 VP8X、ANIM、ANMF…;画布尺寸、偶数坐标与帧矩形落在画布内都会在编码前校验。
EncodeAnimationBytes encodes a as an animated WebP file held in memory. Every frame is encoded on its own (no inter-frame differencing), a transparent frame carries an ALPH sub-chunk, and the container order is VP8X, ANIM, ANMF…; the canvas size, the even positions and the frame rectangles are validated before encoding.
func EncodeBytes ¶
func EncodeBytes(m image.Image, opt *EncodeOptions) ([]byte, error)
EncodeBytes 把 m 编码为完整的无损 WebP 文件并保留在内存中返回。 非空的 ICCP/EXIF/XMP 直接随文件写出;三者都为空时输出仍是不带 VP8X 的简单容器。
EncodeBytes encodes m as a lossless WebP file held in memory. Non-empty ICCP/EXIF/XMP are written straight into the file; when all three are empty the output is still the simple container without a VP8X chunk.
func EncodeLossy ¶ added in v0.2.2
EncodeLossy 把 m 编码为有损 WebP 文件并写入 w。
EncodeLossy writes m as a lossy WebP file.
func EncodeLossyBytes ¶ added in v0.2.2
func EncodeLossyBytes(m image.Image, opt *LossyOptions) ([]byte, error)
EncodeLossyBytes 把 m 编码为完整的有损 WebP 文件并保留在内存中返回。 非空的 ICCP/EXIF/XMP 直接随文件写出;三者都为空时输出仍是不带 VP8X 的简单容器。
带透明度的输入写成扩展格式:VP8X(Alpha 位)+ ALPH + VP8,alpha 平面无损 压缩(或按 AlphaUncompressed 原样存储),完全不透明的输入仍走简单格式。
EncodeLossyBytes encodes m as a lossy WebP file held in memory. Non-empty ICCP/EXIF/XMP are written straight into the file; when all three are empty the output is still the simple container without a VP8X chunk.
An input with transparency is written in the extended format: VP8X (alpha flag) + ALPH + VP8, with the alpha plane compressed losslessly (or stored verbatim with AlphaUncompressed); a fully opaque input still uses the simple format.
A Quality outside 0..100 returns an error rather than being clamped.
Types ¶
type ALPH ¶
type ALPH struct {
// Compression 是压缩方法,见 ALPHCompression* 常量。
// Compression is the compression method, see the ALPHCompression* constants.
Compression uint8
// Filter 是过滤方法,见 ALPHFilter* 常量。
// Filter is the filtering method, see the ALPHFilter* constants.
Filter uint8
// Preprocessing 是预处理方法(0 无,1 level reduction)。
// Preprocessing is the preprocessing method (0 none, 1 level reduction).
Preprocessing uint8
// Data 是去掉首字节标志后的压缩 alpha 数据。
// Data is the compressed alpha data without the leading flag byte.
Data []byte
// contains filtered or unexported fields
}
ALPH 携带有损图像的 alpha 平面:首字节为标志位,其余为压缩后的 alpha 数据。
0 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +-+-+-+-+-+-+-+-+ |C|F|P|Rsv|
C: 压缩方法(0 none, 1 lossless);F: 过滤方法;P: 预处理(0 none, 1 level reduction)。
ALPH carries the alpha plane of a lossy image: one flag byte followed by the compressed alpha data. C: compression method (0 none, 1 lossless), F: filtering method, P: preprocessing (0 none, 1 level reduction).
func (*ALPH) Parse ¶
Parse 解析 ALPH 首字节中的压缩(bit0-1)、过滤(bit2-3)与预处理(bit4-5) 方法,其余字节作为压缩 alpha 数据保留;压缩方法大于 1 报错。 Parse decodes the compression (bits 0-1), filtering (bits 2-3) and preprocessing (bits 4-5) methods from the ALPH flag byte and keeps the remaining bytes as compressed alpha data; a compression method above 1 is rejected.
type ANIM ¶
type ANIM struct {
// BackgroundColor 按文件的 BGRA 字节序读入,Go 中记作 0xAARRGGBB。
// BackgroundColor is read in the file's BGRA byte order and held in Go
// as 0xAARRGGBB.
BackgroundColor uint32
// LoopCount 是播放完所有帧后的循环次数,0 表示无限循环。
// LoopCount is how often the animation is played again after the last
// frame; 0 means loop forever.
LoopCount uint16
}
ANIM 保存整段动画的全局参数,6 字节载荷布局如下(WebP 容器规范):
Background Color: 32 bits (BGRA) Loop Count: 16 bits
ANIM holds the global parameters of an animation. Its 6-byte payload is laid out as follows (WebP container spec): a 32-bit background colour in BGRA byte order and a 16-bit loop count.
type ANMF ¶
type ANMF struct {
// X、Y 是帧左上角在画布上的坐标;文件中按一半存储,读出后乘 2。
// X and Y are the frame's top-left position on the canvas; they are
// stored halved in the file and doubled on read.
X, Y int
// Width、Height 是帧尺寸,文件中按减一存储,读出后加一。
// Width and Height are the frame size, stored minus one and incremented on read.
Width, Height int
// Duration 是本帧的显示时长,单位毫秒。/ Duration is how long the frame is shown, in milliseconds.
Duration int
// Blend 为 true 时帧以 alpha 混合叠加到画布,为 false 时直接覆盖画布对应区域。
// Blend is true when the frame is alpha blended onto the canvas, false
// when it overwrites the canvas region.
Blend bool
// Dispose 为 true 时本帧显示完后把画布清为背景色。
// Dispose is true when the canvas is cleared to the background color
// after the frame was displayed.
Dispose bool
// ALPH 是本帧的 alpha 平面,可为 nil。/ ALPH is the frame's alpha plane, may be nil.
ALPH *ALPH
// VP8 与 VP8L 恰有一个非 nil,承载本帧的图像码流。
// Exactly one of VP8 and VP8L is non-nil and carries the frame bitstream.
VP8 *VP8
VP8L *VP8L
// contains filtered or unexported fields
}
ANMF 表示动画中的一帧:16 字节头部给出帧在画布上的位置、尺寸、显示时长与 混合/清理标志,其后嵌套本帧的图像数据 chunk(可选的 ALPH,加 VP8 或 VP8L 之一)。头部字段布局(WebP 容器规范)见下:
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
ANMF is one animation frame: a 16-byte header with the frame position, size, duration and blend/disposal flags, followed by the frame's nested image chunk (an optional ALPH plus either VP8 or VP8L); the header field layout is shown above (WebP container spec).
func (*ANMF) Parse ¶
Parse 解析 ANMF:位置 x/y 读出后乘 2、宽高读出后加一,时长单位为毫秒; 载荷剩余部分按嵌套 RIFF 流处理,依次解出其中的 ALPH、VP8、VP8L 子 chunk; 既无 VP8 也无 VP8L 时报错。 Parse decodes an ANMF chunk: the position gets doubled and the size incremented on read, the duration is in milliseconds; the rest of the payload is treated as a nested RIFF stream from which the ALPH, VP8 and VP8L sub-chunks are parsed. A frame without any image data is rejected.
type Animation ¶ added in v0.2.7
type Animation struct {
// CanvasWidth、CanvasHeight 是画布尺寸,帧矩形必须落在画布内。
//
// CanvasWidth and CanvasHeight are the canvas size; every frame must fit.
CanvasWidth, CanvasHeight int
// BackgroundColor 是背景色,写在 ANIM 里。
//
// BackgroundColor is written into ANIM.
BackgroundColor color.NRGBA
// LoopCount 是循环次数,0 表示无限循环。
//
// LoopCount is the number of repeats; 0 loops forever.
LoopCount uint16
// Frames 按显示顺序排列,至少一帧。
//
// Frames are in display order and hold at least one frame.
Frames []Frame
}
Animation 是 EncodeAnimation 的输入:画布参数与帧序列。
Animation is the EncodeAnimation input: the canvas parameters and the frames.
type AnimationInfo ¶ added in v0.2.7
type AnimationInfo struct {
// Width、Height 是画布的像素尺寸。/ Width and Height are the canvas size in pixels.
Width, Height int
// BackgroundColor 是 ANIM 声明的背景色(straight alpha)。
// BackgroundColor is the background colour ANIM declares (straight alpha).
BackgroundColor color.NRGBA
// LoopCount 是最后一帧之后的循环次数,0 表示无限循环。
// LoopCount is how often playback repeats after the last frame; 0 loops forever.
LoopCount uint16
// FrameCount 是 ANMF 帧的数量。/ FrameCount is the number of ANMF frames.
FrameCount int
}
AnimationInfo 描述一段动画的画布参数与帧数,不涉及像素解码。
AnimationInfo describes the canvas parameters and the frame count of an animation without decoding any pixels.
type AnimationOptions ¶ added in v0.2.7
type AnimationOptions struct {
// Lossy 为 true 时每帧用 VP8 有损编码,否则用 VP8L 无损编码。
//
// Lossy encodes every frame with VP8; false uses VP8L lossless.
Lossy bool
// Encode 是无损帧的编码选项。/ Encode holds the lossless options.
Encode *EncodeOptions
// LossyOpt 是有损帧的编码选项。/ LossyOpt holds the lossy options.
LossyOpt *LossyOptions
}
AnimationOptions 选择每一帧的编码方式。
AnimationOptions selects how every frame is encoded.
type ChunkParse ¶
type ChunkParse interface {
// ChunkName 返回解析器接管的 chunk 类型码。
// ChunkName returns the chunk type code the parser handles.
ChunkName() FourCC
// Parse 解码给定 chunk 的载荷,出错时返回非 nil error。
// Parse decodes the payload of the given chunk and returns a non-nil
// error on failure.
Parse(chunk *RawChunk) error
}
ChunkParse 是自定义 chunk 解析器必须实现的接口,也是本包对外开放的扩展点 (与 simple-png 的做法一致):实现方在 ChunkName 中声明接管的 chunk 类型码, 由 Parse 解码该 chunk 的载荷。
ChunkParse is the interface a custom chunk parser has to implement, the same extension point simple-png exposes: an implementation states in ChunkName which chunk type code it takes over and decodes that chunk's payload in Parse.
type DecodeOptions ¶
type DecodeOptions struct {
// Format 选择返回的像素布局。PixelFormatYCbCr 只适用于没有 alpha 平面的
// 静态有损码流,其余情况返回 ErrUnsupported。
//
// Format selects the returned pixel layout. PixelFormatYCbCr applies only to
// a still lossy bitstream without an alpha plane; other cases return
// ErrUnsupported.
Format PixelFormat
// Frame 选择动画文件的帧,0 表示第一帧。
//
// Frame selects the frame of an animated file; 0 is the first frame.
Frame int
// MaxPixels 拒绝像素数超过该值的图像,0 表示使用 defaultMaxPixels。
//
// MaxPixels rejects larger images; 0 uses defaultMaxPixels.
MaxPixels int
}
DecodeOptions 调节像素解码行为。
DecodeOptions tunes the pixel decoding.
type EXIF ¶
type EXIF struct {
// Data 是原始 EXIF 字节。/ Data is the raw EXIF bytes.
Data []byte
}
EXIF 携带内嵌的 EXIF 元数据。/ EXIF carries the embedded EXIF metadata.
type EncodeOptions ¶
type EncodeOptions struct {
// Lossless 仅为兼容保留:Encode 始终写出无损 WebP,需要有损输出请改用
// EncodeLossy。之所以不按该值分叉,是因为它的零值是 false——一旦生效,
// 所有没有显式设置它的调用方都会静默地从无损变成有损。
//
// Lossless is kept for compatibility: Encode always writes lossless webp and
// EncodeLossy is the lossy entry point. The value is not honoured because
// its zero value is false, so making it meaningful would silently turn every
// caller that never set it into a lossy encoder.
Lossless bool
// Method 以速度换压缩率:0 只存储字面量,1 追加减绿(subtract green)
// 变换、颜色索引和 LZ77,2(默认)再应用预测器变换。取值必须是 0、1 或 2;
// 越界的 Method 不会被静默兜底,而是返回错误。
//
// 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. Only 0, 1 and 2 are accepted; an
// out-of-range Method is not silently fallen back to the default but
// returns an error.
Method int
// ICCP 是内嵌的 ICC 色彩配置原始字节;非空时写出带标志位的 VP8X 与 ICCP。
//
// ICCP is the embedded ICC profile; when it is non-empty a flagged VP8X and
// an ICCP chunk are written.
ICCP []byte
// EXIF 是内嵌的 EXIF 元数据原始字节;非空时写出带标志位的 VP8X 与 EXIF。
//
// EXIF is the embedded EXIF metadata; when it is non-empty a flagged VP8X
// and an EXIF chunk are written.
EXIF []byte
// XMP 是内嵌的 XMP 元数据原始字节;非空时写出带标志位的 VP8X 与 XMP。
//
// XMP is the embedded XMP metadata; when it is non-empty a flagged VP8X and
// an XMP chunk are written.
XMP []byte
}
EncodeOptions 调节无损编码器的行为。
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 " )
Chunk type codes of the WebP container, see https://developers.google.com/speed/webp/docs/riff_container
WebP 容器的各 chunk 类型码,见 https://developers.google.com/speed/webp/docs/riff_container
type Frame ¶ added in v0.2.7
type Frame struct {
// Image 是这一帧的像素,尺寸即帧矩形的大小。
//
// Image holds the frame pixels; its size is the frame rectangle.
Image image.Image
// X、Y 是帧左上角在画布上的坐标,必须是偶数(文件中按一半存储)。
//
// X and Y are the frame's top-left position, which must be even (the file
// stores them halved).
X, Y int
// Duration 是这一帧的显示时长,单位毫秒。
//
// Duration is how long the frame is shown, in milliseconds.
Duration int
// Blend 为 true 时帧与画布按 alpha 混合,为 false 时直接覆盖。
//
// Blend composites the frame onto the canvas with alpha; false overwrites.
Blend bool
// Dispose 为 true 时本帧显示完后把画布清为背景色。
//
// Dispose clears the canvas to the background colour after the frame.
Dispose bool
}
Frame 是 EncodeAnimation 的一帧输入:画布上的位置、显示时长、混合与清理 方式,以及这一帧的像素。
Frame is one input frame of EncodeAnimation: its position on the canvas, its display duration, its blend and disposal mode, and its pixels.
type FrameOptions ¶ added in v0.2.7
type FrameOptions struct {
// MaxPixels 拒绝画布像素数超过该值的动画,0 表示 defaultMaxPixels。
// 画布尺寸来自 VP8X,可能远大于实际帧,所以它必须单独受预算约束。
//
// MaxPixels rejects animations whose canvas is larger; 0 uses
// defaultMaxPixels. The canvas comes from VP8X and can be far larger than
// the frames themselves, so it needs a budget of its own.
MaxPixels int
// MaxTotalPixels 拒绝画布像素 × 帧数超过该值的动画,0 表示
// defaultMaxTotalPixels。Frames 会一次性返回全部帧,总量必须有上限。
//
// MaxTotalPixels rejects animations whose canvas times frame count exceeds
// it; 0 uses defaultMaxTotalPixels. Frames returns every frame at once, so
// the total has to be bounded.
MaxTotalPixels int
}
FrameOptions 调节动图解码。
FrameOptions tunes animation decoding.
type ICCP ¶
type ICCP struct {
// Data 是原始 ICC 配置字节。/ Data is the raw ICC profile bytes.
Data []byte
}
ICCP 携带内嵌的 ICC 色彩配置文件。/ ICCP carries the embedded ICC color profile.
type ImageOptions ¶ added in v0.2.7
type ImageOptions struct {
// Lossy 为 true 时用 VP8 有损编码,否则用 VP8L 无损编码。
//
// Lossy selects the VP8 lossy encoder; false selects VP8L lossless.
Lossy bool
// Encode 是无损编码选项,Lossy 为 false 时使用。
//
// Encode holds the lossless options, used when Lossy is false.
Encode *EncodeOptions
// LossyOpt 是有损编码选项,Lossy 为 true 时使用。
//
// LossyOpt holds the lossy options, used when Lossy is true.
LossyOpt *LossyOptions
}
ImageOptions 选择 SetImage 的编码方式。
ImageOptions selects how SetImage encodes the pixels.
type LossyOptions ¶ added in v0.2.2
type LossyOptions struct {
// Quality 取 0(体积最小)到 100(质量最高);超出 0..100 返回错误,不静默钳制。
// 零值默认等同于 75,除非 QualityZero 为 true。
//
// Quality runs from 0 (smallest) to 100 (best); a value outside 0..100
// returns an error instead of being silently clamped. The zero value means
// the default 75 unless QualityZero is set.
Quality int
// QualityZero 让 Quality 的零值表示「质量 0」而不是默认的 75。
// 不设置时保持旧语义:Quality == 0 等于默认质量。
//
// QualityZero makes the zero value of Quality mean quality 0 instead of
// the default 75. When it is false the old meaning stays: Quality == 0
// selects the default quality.
QualityZero bool
// ICCP 是内嵌的 ICC 色彩配置原始字节;非空时写出带标志位的 VP8X 与 ICCP。
//
// ICCP is the embedded ICC profile; when it is non-empty a flagged VP8X and
// an ICCP chunk are written.
ICCP []byte
// EXIF 是内嵌的 EXIF 元数据原始字节;非空时写出带标志位的 VP8X 与 EXIF。
//
// EXIF is the embedded EXIF metadata; when it is non-empty a flagged VP8X
// and an EXIF chunk are written.
EXIF []byte
// XMP 是内嵌的 XMP 元数据原始字节;非空时写出带标志位的 VP8X 与 XMP。
//
// XMP is the embedded XMP metadata; when it is non-empty a flagged VP8X and
// an XMP chunk are written.
XMP []byte
// AlphaFilter 选择 ALPH 的行滤波方式:0 不滤波(默认)、1 水平、2 垂直、
// 3 梯度;其它值返回错误。滤波只影响 alpha 的压缩率,不改变像素值。
//
// AlphaFilter selects the ALPH row filter: 0 none (the default), 1
// horizontal, 2 vertical, 3 gradient; any other value is an error. The
// filter changes only the compressed size, never the alpha values.
AlphaFilter uint8
// AlphaUncompressed 为 true 时 alpha 平面按 C=0 原样存储;默认按 C=1 用
// VP8L 无损压缩。两种方式都不量化 alpha。
//
// AlphaUncompressed stores the alpha plane verbatim (C=0); by default it is
// compressed losslessly with VP8L (C=1). Neither mode quantises the alpha.
AlphaUncompressed bool
// FilterLevel 是 VP8 环路滤波强度 1..63;0(默认)按质量自动选择,
// -1 关闭滤波。强度越大块效应越弱,细节也更软。
//
// FilterLevel is the VP8 loop filter strength, 1..63. Zero (the default)
// derives it from the quality, -1 disables the filter. A stronger filter
// removes more blocking and softens detail.
FilterLevel int
// SimpleFilter 为 true 时用简单滤波(只处理亮度边缘),否则用普通滤波。
//
// SimpleFilter selects the simple filter (luma edges only).
SimpleFilter bool
// TargetBytes 打开码率控制:在质量 0..100 之间二分,取最接近该体积的编码
// 结果(只对 VP8 码流生效,alpha 与元数据不计入)。0 表示按 Quality 编码。
//
// TargetBytes enables rate control: the quality is bisected over 0..100 and the
// encode closest to that size wins (it covers the VP8 bitstream only, alpha
// and metadata are not counted). 0 encodes at Quality.
TargetBytes int
// Segment 打开逐段量化:按宏块活动度把帧分成四段,平坦区域用更粗的
// 量化、繁忙区域保持基础量化。编码器会同时试一遍不分段的版本并只保留
// 更小的,体积不会大于关闭时。默认关闭。
//
// Segment enables per-segment quantization: the frame is split into four
// segments by macroblock activity, flat regions getting a coarser
// quantizer while busy ones keep the base. The encoder also tries one
// unsegmented pass and keeps the smaller stream, so the size never grows
// over having it off. It is off by default.
Segment bool
}
LossyOptions 调节有损(VP8)编码器的行为。
LossyOptions tunes the lossy (VP8) encoder.
type ParseOptions ¶ added in v0.2.7
type ParseOptions struct {
// Strict 打开规范 MUST 校验:VP8X 必须第一个、重建类 chunk 顺序、
// VP8X 保留位为 0、动画位必须有 ANIM、帧矩形落在画布内、VP8L 帧不得带
// ALPH、填充字节为 0。默认关闭,以免拒绝真实世界里的脏文件。
//
// Strict enables the spec MUST checks: VP8X first, the order of the
// reconstructing chunks, zeroed VP8X reserved bits, an ANIM chunk behind
// the animation flag, frames inside the canvas, no ALPH on a VP8L frame,
// and zero padding bytes. It is off by default so real world files that
// violate these rules still parse.
Strict bool
// VerifySize 要求 RIFF File Size 与实际流长度一致,也就是 ParseWebp 的
// verifySize 参数。
//
// VerifySize requires the RIFF File Size to match the actual stream length,
// the verifySize argument of ParseWebp.
VerifySize bool
// MaxChunkSize 限制单个 chunk 载荷的字节数,0 表示默认的 1<<30。
// 解析不可信输入时调小它可以更早拒绝畸形文件;载荷按流式读入,
// 超限的 chunk 在分配任何内存之前就被拒绝。
//
// MaxChunkSize caps the payload of a single chunk in bytes; 0 means the
// default 1<<30. Turn it down when parsing untrusted input to reject
// malformed files earlier; payloads are streamed in, so an oversized
// chunk is refused before any memory is allocated for it.
MaxChunkSize int
}
ParseOptions 调节容器解析。零值等价于 ParseWebp 的宽松行为。
ParseOptions tunes container parsing. The zero value matches the lenient behaviour of ParseWebp.
type PixelFormat ¶
type PixelFormat int
PixelFormat 选择解码结果的像素布局:NRGBA(默认)、RGBA,或有损码流的 YCbCr 平面。
PixelFormat selects the pixel layout of the decoded image: NRGBA (the default), RGBA, or the YCbCr planes of a lossy bitstream.
const ( // PixelFormatNRGBA 返回 *image.NRGBA,即 straight alpha(非预乘)布局, // 与 png 解码器处理 RGBA 图像时使用的布局一致。这是默认值。 // // 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 返回 *image.RGBA,即预乘 alpha(premultiplied)布局。 // // PixelFormatRGBA returns an *image.RGBA, the premultiplied layout. PixelFormatRGBA // PixelFormatYCbCr 返回有损码流解码得到的 *image.YCbCr(4:2:0),不做 YUV→RGB // 转换。无损码流、带 alpha 平面的图以及动画没有 YCbCr 表示,会返回 ErrUnsupported。 // // PixelFormatYCbCr returns the *image.YCbCr (4:2:0) planes decoded from a // lossy bitstream without a YUV to RGB conversion. Lossless bitstreams, // images with an alpha plane and animations have no YCbCr representation and // return ErrUnsupported. PixelFormatYCbCr )
type RawChunk ¶
type RawChunk struct {
// Name 是 4 字节 chunk 类型码。/ Name is the 4-byte chunk type code.
Name FourCC
// Data 是去掉 chunk 头与对齐填充后的载荷。
// Data is the chunk payload without the header and the pad byte.
Data []byte
}
RawChunk 表示未被原生解码、以原始字节形式保留的 chunk(例如未知的应用私有 chunk);重编码容器时它会原样写回。
RawChunk is a chunk that was not decoded natively and is kept in its raw form, e.g. an application-specific chunk of unknown type; it is written back verbatim when the container is re-encoded.
type VP8 ¶
type VP8 struct {
// Data 是整个 VP8 chunk 载荷,含帧头,原样保留。
// Data is the whole VP8 chunk payload including the frame header, kept verbatim.
Data []byte
// Width、Height 是帧头声明的画布尺寸(各 16 位字段的低 14 位)。
// Width and Height are the canvas dimensions declared by the frame
// header (the low 14 bits of each 16-bit field).
Width, Height int
// Version 是帧头中的编码器版本(bit1-3)。/ Version is the encoder version from the frame header (bits 1-3).
Version uint8
// ShowFrame 表示该帧解码后立即显示。/ ShowFrame reports that the frame is displayed right after decoding.
ShowFrame bool
// FirstPartSize 是首分区的字节数。/ FirstPartSize is the size of the first partition in bytes.
FirstPartSize int
}
VP8 是有损码流 chunk。载荷原样保留、不作改动:WebP 静态有损图像只有一帧 VP8 关键帧,这里仅解析帧头,不解码即可拿到图像尺寸。
VP8 is the lossy bitstream chunk. The payload is kept untouched: a WebP lossy still image is a single VP8 key frame, so parsing the frame header alone already yields the image dimensions without decoding the frame.
func (*VP8) Parse ¶
Parse 解析 VP8 帧头:3 字节 tag(bit0 为 0 表示关键帧,bit1-3 为版本, bit4 为 show_frame,bit5 起为首分区字节长)、随后 3 字节起始码 0x9d 0x01 0x2a、 以及宽高字段(各 16 位,取低 14 位,高 2 位为缩放系数,此处忽略)。 Data 保留整个载荷;非关键帧、坏起始码或零尺寸都判为损坏。 Parse decodes the VP8 frame header: the 3-byte tag (bit 0 clear means a key frame, bits 1-3 the version, bit 4 show_frame, bits 5+ the size of the first partition), the 3-byte start code 0x9d 0x01 0x2a that follows, and the dimension fields (16 bits each, the low 14 bits taken, the high 2 scale bits ignored here). Data keeps the whole payload; a non-key frame, a bad start code or a zero dimension is rejected.
type VP8L ¶
type VP8L struct {
// Data 是整个 VP8L chunk 载荷,含 5 字节头,原样保留。
// Data is the whole VP8L chunk payload including the 5-byte header, kept verbatim.
Data []byte
// Width、Height 是从头部读出并加一后的实际尺寸。
// Width and Height are the actual dimensions read from the header and incremented.
Width, Height int
// AlphaUsed 是头部的 alpha_is_used 标志位。
// AlphaUsed is the alpha_is_used flag from the header.
AlphaUsed bool
// Version 是头部的版本号,非 0 会被 Parse 拒绝。
// Version is the header version number; anything but 0 is rejected by Parse.
Version uint8
}
VP8L 是无损码流 chunk,头部布局见无损码流规范 https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification: 5 字节头按位排列如下,宽高按减一存储(读出后各加一),版本号必须为 0。
VP8L is the lossless bitstream chunk; its header layout follows the lossless bitstream specification at https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification. The 5-byte header is laid out bit-wise as follows; the dimensions are stored minus one (and get incremented) and the version number must be 0.
signature 8 bits (0x2f) width - 1 14 bits height - 1 14 bits alpha_is_used 1 bit version_number 3 bits (must be 0)
func (*VP8L) Parse ¶
Parse 解析 VP8L 的 5 字节头:先校验签名 0x2f,再读出宽高(减一存储,各加一)、 alpha 使用标志与版本号;版本号非 0 报错。Data 保留整个载荷。 Parse decodes the 5-byte VP8L header: it checks the 0x2f signature first, then reads the dimensions (stored minus one, so incremented), the alpha-used flag and the version number; a version other than 0 is rejected. Data keeps the whole payload.
type VP8X ¶
VP8X 是扩展 chunk:10 字节载荷声明画布宽高(各按减一存储)以及文件包含哪些 扩展特性,其位布局如下(WebP 容器规范):
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。
VP8X is the extended chunk: its 10-byte payload declares the canvas size (each dimension stored minus one) and which extended features the file carries. The bit layout above follows the WebP container spec; the flag letters are: I: ICC profile, L: alpha, E: EXIF, X: XMP, A: animation.
func (*VP8X) HasAlpha ¶
HasAlpha 报告图像是否带 alpha 通道。/ HasAlpha reports whether an alpha channel is present.
func (*VP8X) HasAnimation ¶
HasAnimation 报告该文件是否为动画。/ HasAnimation reports whether the file is an animation.
func (*VP8X) HasEXIF ¶
HasEXIF 报告文件是否内嵌 EXIF 元数据。/ HasEXIF reports whether EXIF metadata is embedded.
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 按 chunk 类型存放经 ParseChunk 解析出的自定义 chunk。
// 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 是解析后的 WebP 容器。图像数据 chunk 的负载以原始字节保留, 各解析结构体只是对这些字节的视图,因此可以修改元数据并原样写回, 无需重新编码像素。
Webp is a parsed WebP container. The chunk payloads of the image data are kept as raw bytes and the parsed structs are views onto them, so metadata can be edited and written back without re-encoding the pixels.
func NewWebp ¶ added in v0.2.7
NewWebp 创建一个空容器,画布尺寸为 width × height。容器没有任何 chunk, 需要先用 SetImage 或 AddFrame 放入图像数据再 WriteTo;写一个空容器会报错。
NewWebp creates an empty container with a width times height canvas. It holds no chunk yet, so SetImage or AddFrame has to supply the image data before WriteTo; writing an empty container is an error.
func ParseWebp ¶
ParseWebp 解析 WebP 流:读取 RIFF 头与全部 chunk,并解码容器级 chunk (VP8X、VP8、VP8L、ALPH、ANIM、ANMF、ICCP、EXIF、XMP)。未知类型的 chunk 以原始形式保留,见 RawChunks。仅当 verifySize 为 true 时才校验 RIFF 大小字段与实际流长度是否一致。
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 解析内存中的 WebP 字节流,不校验 RIFF 大小字段 (等价于调用 ParseWebp 时不传 verifySize)。
ParseWebpBytes parses a webp stream held in memory; the RIFF size field is not verified (ParseWebp is called without verifySize).
func ParseWebpBytesWith ¶ added in v0.2.7
func ParseWebpBytesWith(b []byte, opt ParseOptions) (*Webp, error)
ParseWebpBytesWith 按给定选项解析内存中的 WebP 字节流,见 ParseOptions。
ParseWebpBytesWith parses a webp stream held in memory with the given options, see ParseOptions.
func ParseWebpWith ¶ added in v0.2.7
func ParseWebpWith(r io.Reader, opt ParseOptions) (*Webp, error)
ParseWebpWith 按给定选项解析 WebP 流,见 ParseOptions。
ParseWebpWith parses a webp stream with the given options, see ParseOptions.
func (*Webp) AddFrame ¶ added in v0.2.7
AddFrame 把一帧追加到动画容器末尾:帧坐标必须为偶数、帧矩形必须落在画布内, 帧内必须有 VP8 或 VP8L 码流。容器里已有静态图像时返回错误,尚未有 ANIM 时 会自动补一个默认的 ANIM。
AddFrame appends a frame to an animated container: the frame position must be even, the frame rectangle must fit the canvas and the frame must carry a VP8 or VP8L bitstream. A container that already holds a still image is an error; a missing ANIM chunk is created with default values.
func (*Webp) AddRawChunk ¶
AddRawChunk 把一个新的原始 chunk 按类型排位插入流中:跳过所有排位不大于 自身的已有 chunk,即 VP8X 最前,ICCP 在 VP8X 之后、ANIM/ANMF/图像数据之前, ANIM 在第一个 ANMF 之前,ALPH 在 ANIM/ANMF 之后、VP8/VP8L 之前,VP8/VP8L 之后是 EXIF、XMP 等尾部 chunk;排位 6 一律追加到末尾。可用于给已解析的文件 补充 EXIF 或 XMP 等 chunk。name 必须是合法 FourCC,data 不得超过 maxChunkSize,且 Webp 必须来自成功的 ParseWebp。
单实例 chunk(VP8X/VP8/VP8L/ALPH/ANIM/ICCP/EXIF/XMP)在流中已存在时拒绝 重复插入——两份会让文件非法;ANMF 可以追加,但载荷必须是一帧合法的帧 (手工构造帧请用 AddFrame)。
AddRawChunk inserts a new raw chunk at the position its type dictates: it skips every existing chunk whose rank is not greater than its own, so VP8X comes first, ICCP follows VP8X and precedes ANIM, ANMF and the image data, ANIM precedes the first ANMF, ALPH follows ANIM/ANMF and precedes VP8/VP8L, and EXIF, XMP or unknown types land at the end. Use it e.g. to add an EXIF or XMP chunk to an already parsed file. name must be a valid FourCC, data must fit in maxChunkSize and the Webp must come from a successful ParseWebp.
A single-instance chunk (VP8X/VP8/VP8L/ALPH/ANIM/ICCP/EXIF/XMP) that is already present is refused, because a second copy makes the file invalid; an ANMF may be appended, but its payload has to be a valid frame (use AddFrame to build one).
func (*Webp) AnimationInfo ¶ added in v0.2.7
func (w *Webp) AnimationInfo() (AnimationInfo, error)
AnimationInfo 返回动画的画布尺寸、背景色、循环次数与帧数;非动画文件返回错误。
AnimationInfo returns the canvas size, background colour, loop count and frame count of the animation; a non-animated file is an error.
func (*Webp) At ¶
At 实现 image.Image。图像在首次调用时才解码并缓存;解码失败时 返回透明的 color.NRGBA{}。
At implements image.Image. The image is decoded lazily on the first call and cached afterwards; if decoding fails, a transparent color.NRGBA{} is returned.
func (*Webp) Bounds ¶
Bounds 实现 image.Image:返回文件的画布矩形(从原点开始,宽高来自头部)。
Bounds implements image.Image: it returns the canvas rectangle of the file (origin at (0,0), size taken from the header).
func (*Webp) ColorModel ¶
ColorModel 实现 image.Image。解码器输出的是 straight alpha(非预乘) NRGBA 像素,因此这里固定返回 color.NRGBAModel。
ColorModel implements image.Image. The decoders produce straight alpha (non-premultiplied) NRGBA pixels, so color.NRGBAModel is returned unconditionally.
func (*Webp) DecodeInto ¶ added in v0.2.8
DecodeInto 把一张静态图解到调用方提供的 NRGBA 缓冲里:除色度上采样的暂存 行外不再分配图像内存,连续解码多张图时可以复用同一块缓冲。dst 的边界必须 恰好是 image.Rect(0, 0, w.Width, w.Height);尺寸不符、nil 目标或动画文件 都会返回错误,dst 内容不被修改。像素预算取默认值(1<<27 个像素)。
DecodeInto decodes one still image into an NRGBA buffer supplied by the caller: apart from the chroma upsampling scratch rows no image memory is allocated, so a pipeline that decodes image after image can reuse one buffer. The bounds of dst must be exactly image.Rect(0, 0, w.Width, w.Height); a size mismatch, a nil target or an animated file is an error and leaves dst untouched. The pixel budget is the default one (1<<27 pixels).
func (*Webp) DeleteRawChunk ¶
DeleteRawChunk 从流中删除所有指定类型的 chunk 并返回删除数量。 既适用于原生解码的元数据 chunk(ICCP、EXIF、XMP),也适用于解析时已被 消费的 ALPH、ANIM、ANMF、VP8X、VP8、VP8L,以及未知的原始 chunk;删除后 调用 WriteTo 写出的流不再包含这些 chunk,VP8X 的对应标志位同步清除, 保持标志与数据一致。三条例外都会产出非法流,因此不删除任何东西,返回 0: 还有 ANMF 帧时删 ANIM、还有扩展特性时删 VP8X、以及删掉唯一的图像数据。 DeleteRawChunk removes every chunk of the given type from the stream and returns how many chunks were removed. It covers the metadata chunks decoded natively (ICCP, EXIF, XMP), the chunks already consumed by parsing (ALPH, ANIM, ANMF, VP8X, VP8, VP8L), and unknown raw chunks; a WriteTo afterwards writes the stream without those chunks, and the matching VP8X flag bit is cleared so the flags stay consistent with the data. Three cases are refused because they would produce an invalid stream: deleting ANIM while ANMF frames still exist, deleting VP8X while an extended feature still needs it, and deleting the only image data chunk. In all three cases nothing is removed and the result is 0.
func (*Webp) FrameAt ¶ added in v0.2.7
FrameAt 只合成到第 i 帧(0 起)并返回该帧与它的显示时长:中间帧不克隆, 因此内存只需要一块画布,而不是「帧数 × 画布」。像素预算与 FramesWith 相同, 总量按 i+1 帧计算;i 越界返回错误。
FrameAt composites only up to frame i (zero based) and returns that frame and its display duration. The intermediate frames are never cloned, so one canvas is enough instead of frames times canvas. The budgets are the ones of FramesWith, with the total counted over i+1 frames; an out-of-range index is an error.
func (*Webp) Frames ¶
Frames 逐帧解码动图,返回按画布尺寸合成的各帧(NRGBA)及其显示时长 (毫秒)。maxPixels 像素预算同时约束画布尺寸与单帧解码规模,0 表示 defaultMaxPixels;等价于 FramesWith(FrameOptions{MaxPixels: maxPixels})。
Frames decodes every frame of an animated file and returns the composited frames at canvas size together with their display duration in milliseconds. The maxPixels budget caps both the canvas size and the per-frame decoding size; 0 means defaultMaxPixels. It is FramesWith(FrameOptions{MaxPixels: maxPixels}).
func (*Webp) FramesWith ¶ added in v0.2.7
FramesWith 按给定选项逐帧解码动图,见 Frames。每帧先解码为静止图像再混合到 画布上,随后克隆整块画布作为输出帧;画布尺寸与「画布 × 帧数」的总量都会在 分配像素缓冲之前按预算预检,超限返回 ErrImageTooLarge。
FramesWith decodes an animated file frame by frame with the given options, see Frames. Each frame is decoded as a still image, blended onto the canvas, and the whole canvas is then cloned as the output frame. The canvas size and the canvas-times-frames total are checked against the budget before any pixel buffer is allocated; exceeding it returns ErrImageTooLarge.
func (*Webp) GetOtherChunkByName ¶
func (w *Webp) GetOtherChunkByName(name FourCC) ([]ChunkParse, error)
GetOtherChunkByName 返回为某类 chunk 记录的自定义解析器列表; 该类型没有记录时返回 ErrChunkNotFound。 GetOtherChunkByName returns the custom chunk parsers recorded for a type; it returns ErrChunkNotFound when nothing was recorded for that type.
func (*Webp) Image ¶
Image 只解码一次并缓存结果,是 Webp 实现 image.Image 的基础: 首次调用触发完整解码,之后(含失败原因)都复用同一结果。
Image decodes the image once and caches the result; it backs the image.Image implementation of Webp. The first call triggers the full decode; later calls reuse the same result, including any error.
func (*Webp) InvalidateCache ¶ added in v0.2.7
func (w *Webp) InvalidateCache()
InvalidateCache 丢弃 Image/At 的解码缓存;直接修改像素结构体后调用它, 下一次 Image/At 会重新解码。
InvalidateCache drops the decode cache of Image/At; call it after mutating the pixel structs directly so the next Image/At decodes again.
func (*Webp) MarshalBytes ¶
MarshalBytes returns the webp stream of the current state as a byte slice, see WriteTo for the details.
MarshalBytes 以字节切片返回当前状态的 webp 流,细节见 WriteTo。
func (*Webp) ParseChunk ¶
func (w *Webp) ParseChunk(c ChunkParse, notSave ...bool) error
ParseChunk 把第一个类型与 c.ChunkName() 匹配的剩余 chunk 解析进 c。 解析成功后该 chunk 被视为已消费并从原始 chunk 列表移除;除非 notSave 为 true,解析器实例会记录到 OtherChunk,之后可用 GetOtherChunkByName 取回。找不到匹配 chunk 时返回 ErrChunkNotFound。 ParseChunk parses the first remaining chunk whose type matches c.ChunkName() into c. 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. It returns ErrChunkNotFound when no chunk matches.
func (*Webp) RawChunkByName ¶
RawChunkByName 返回第一个仍存在的指定类型 chunk 的原始负载; 不存在时 ok 为 false。 RawChunkByName returns the raw payload of the first still-present chunk of the given type; ok is false when no such chunk remains.
func (*Webp) RawChunks ¶
RawChunks 返回基础解析后仍以原始形式保留的所有 chunk,按其在文件中 出现的顺序排列。 RawChunks returns all chunks that are still left after base parsing, in the order they appear in the file.
func (*Webp) RemoveFrame ¶ added in v0.2.7
RemoveFrame 删除第 i 帧(0 起);删掉最后一帧时同时清掉 ANIM 与 animation 标志位,输出会退化成没有图像数据的容器,因此需要再 AddFrame 或 SetImage。
RemoveFrame drops frame i (zero based); removing the last frame also clears the ANIM chunk and the animation flag, leaving a container without image data, so a later AddFrame or SetImage has to supply some.
func (*Webp) SetImage ¶ added in v0.2.7
func (w *Webp) SetImage(m image.Image, opt *ImageOptions) error
SetImage 用 m 替换容器里的图像数据:按 opt 选择有损或无损编码,重编码像素, 同步画布尺寸与 VP8X,保留已有的 ICCP/EXIF/XMP 元数据,并丢弃解码缓存。 动画容器不能直接替换静态图像,请用 RemoveFrame/AddFrame 编辑帧。
SetImage replaces the image data of the container: it re-encodes the pixels with the lossy or lossless encoder selected by opt, syncs the canvas size and the VP8X chunk, keeps the ICCP/EXIF/XMP metadata and drops the decode cache. An animated container cannot take a still image; edit its frames with RemoveFrame/AddFrame instead.
func (*Webp) ToImage ¶
ToImage 解码图像并以 image.Image 返回。无损文件走 VP8L 解码器,有损文件走 VP8 解码器并叠加 ALPH alpha 通道;动画文件返回第一帧。
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 按给定选项解码图像。
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.
An ICCP/EXIF/XMP/ALPH/ANIM assigned to its struct is written even when the original file had none; combinations that would produce an invalid stream (an ALPH next to a VP8L, an ANIM without frames, an odd frame position, a duration beyond 24 bits) are errors instead of a file whose meaning quietly changed.
WriteTo 把当前状态的 webp 流写出,实现 io.WriterTo。解析时被消费(consumed)的 chunk 会从解析结构体重新序列化,因此对结构体的修改、以及通过 AddRawChunk / DeleteRawChunk 增删的 chunk 都会反映到输出;未被消费、保持原始形态的 chunk 则逐字节原样拷贝。 不做任何修改地解析再写回,可逐字节复现原文件。
图像载荷(VP8 / VP8L / ALPH)永远不会被重新编码,因此编辑元数据 chunk 完全不触及像素数据。
直接赋值给结构体的 ICCP/EXIF/XMP/ALPH/ANIM 即使原文件没有也会被写出;会 产出非法流的组合(ALPH 配 VP8L、没有帧的 ANIM、奇数帧坐标、超出 24 位的 时长)返回错误,而不是写出一个语义被悄悄改变的文件。
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
vp8l
Package vp8l 实现 WebP 无损(VP8L)位流解码器,规范见 https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification
|
Package vp8l 实现 WebP 无损(VP8L)位流解码器,规范见 https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification |
|
Package wasm 将纯 Go 实现的 webp 解码器暴露给 WebAssembly / JavaScript。
|
Package wasm 将纯 Go 实现的 webp 解码器暴露给 WebAssembly / JavaScript。 |
|
main
command
Command wasm is the WebAssembly entry point.
|
Command wasm is the WebAssembly entry point. |