Documentation
¶
Overview ¶
Package webp implements a WebP image decoder and encoder based on the libwebp, libwebpdemux and libwebpmux shared libraries used via purego (CGo-free).
Index ¶
Constants ¶
const ( // DefaultQuality is the default quality (cwebp -q). DefaultQuality = 75 // DefaultAlphaQuality is the default quality of the alpha channel (cwebp -alpha_q). DefaultAlphaQuality = 100 // DefaultMethod is the default compression method (cwebp -m). DefaultMethod = 4 )
Defaults of the encoding parameters. They are the same as the defaults of cwebp.
Variables ¶
var ( ErrDecode = errors.New("webp: decode failed") ErrEncode = errors.New("webp: encode failed") ErrOptions = errors.New("webp: invalid options") )
Errors returned by this package, wrapped with details.
Functions ¶
func Decode ¶
Decode reads a WebP image from r and returns it as an image.Image. For an animation, it returns the first frame.
func DecodeConfig ¶
DecodeConfig returns the color model and dimensions of a WebP image without decoding it.
func Dynamic ¶
func Dynamic() error
Dynamic returns the error, if any, that makes the libwebp shared libraries unusable.
Types ¶
type Options ¶
type Options struct {
// Preset applied before the other options (cwebp -preset). Default: PresetDefault.
Preset Preset
// Quality in [0,100] (cwebp -q). Default: 75. In lossless mode it is the compression effort:
// higher values give smaller files, more slowly.
Quality *int
// Quality of the alpha channel in [0,100] where 100 is lossless (cwebp -alpha_q). Default: 100.
AlphaQuality *int
// Compression method in [0,6] where 0 is the fastest and 6 the slowest with the best
// compression (cwebp -m). Default: 4.
Method *int
// Encode losslessly (cwebp -lossless).
Lossless bool
// Near-lossless preprocessing level in [0,100] where 0 is the strongest and 100 is off
// (cwebp -near_lossless). Setting it implies Lossless.
NearLossless *int
// Preserve the RGB values of transparent pixels (cwebp -exact). By default they are changed
// for a better compression.
Exact bool
// Use the sharper and slower RGB to YUV conversion (cwebp -sharp_yuv).
SharpYUV bool
// Target size in bytes (cwebp -size). 0 disables it. Lossy only.
TargetSize int
// Target PSNR in dB (cwebp -psnr). It takes precedence over TargetSize. 0 disables it.
// Lossy only.
TargetPSNR float64
// EncodeAll only: encode every frame both lossy and lossless and keep the smaller one
// (img2webp -mixed).
AllowMixed bool
// EncodeAll only: minimize the output size, slowly (img2webp -min_size).
MinimizeSize bool
}
Options are the encoding parameters. They mirror the options of cwebp, and of img2webp for EncodeAll, with the same defaults. The zero value of every field means "use the cwebp default". Fields that accept 0 as a meaningful value are pointers, e.g. Quality: new(80).
type Preset ¶
type Preset int
Preset is a set of encoding parameters tuned for a kind of image (cwebp -preset).
const ( PresetDefault Preset = iota // PresetPicture is for digital pictures, like portraits or inner shots. PresetPicture // PresetPhoto is for outdoor photographs, with natural lighting. PresetPhoto // PresetDrawing is for hand or line drawings, with high-contrast details. PresetDrawing // PresetIcon is for small-sized colorful images. PresetIcon // PresetText is for text-like images. PresetText )
Presets, in the order of the WebPPreset enum of libwebp.
type WEBP ¶
type WEBP struct {
// Images, one per frame. Decoded images are *image.NRGBA when the WebP has alpha and
// *image.RGBA otherwise.
Image []image.Image
// Display durations, one per frame, in milliseconds.
Delay []int
// Number of times an animation is played, 0 meaning infinitely.
LoopCount int
}
WEBP represents the possibly multiple images stored in a WebP file.