webp

package
v0.0.0-...-61eedf9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2022 License: BSD-2-Clause Imports: 8 Imported by: 10

Documentation

Overview

Package webp provides an interface to libwebp library to decoding/encoding WebP image.

Index

Constants

View Source
const (
	// PreprocessingNone specifies to disable preprocessing filter.
	PreprocessingNone = 0
	// PreprocessingSegmentSmooth specifies segment-smooth filter.
	PreprocessingSegmentSmooth = 1
	//PreprocessingPseudoRandomDithering specifies pseudo-random dithering filter.
	PreprocessingPseudoRandomDithering = 2
)

Variables

View Source
var RGBModel = color.ModelFunc(rgbModel)

RGBModel is RGB color model instance

Functions

func DecodeNRGBA

func DecodeNRGBA(data []byte, options *DecoderOptions) (img *image.NRGBA, err error)

DecodeNRGBA decodes WebP image into RGBA image and returns it as an *image.NRGBA.

func DecodeRGBA

func DecodeRGBA(data []byte, options *DecoderOptions) (img *image.RGBA, err error)

DecodeRGBA decodes WebP image into rgbA image and returns it as an *image.RGBA.

func EncodeGray

func EncodeGray(w io.Writer, p *image.Gray, c *Config) (err error)

func EncodeRGBA

func EncodeRGBA(w io.Writer, img image.Image, c *Config) (err error)

EncodeRGBA encodes and writes image.Image into the writer as WebP. Now supports image.RGBA or image.NRGBA.

func EncodeYUVA

func EncodeYUVA(w io.Writer, img *YUVAImage, c *Config) (err error)

EncodeYUVA encodes and writes YUVA Image data into the writer as WebP.

func GetDecoderVersion

func GetDecoderVersion() (v int)

GetDecoderVersion returns decoder's version number, packed in hexadecimal. e.g; v0.4.2 is 0x000402

func GetDestinationManagerMapLen

func GetDestinationManagerMapLen() int

GetDestinationManagerMapLen returns the number of globally working sourceManagers for debug

func GetInfo

func GetInfo(data []byte) (width, height int)

GetInfo retrives width/height from data bytes.

Types

type BitstreamFeatures

type BitstreamFeatures struct {
	Width        int  // Image width in pixels
	Height       int  // Image height in pixles
	HasAlpha     bool // True if data stream contains a alpha channel.
	HasAnimation bool // True if data stream is an animation
	Format       int  // Image compression format
}

BitstreamFeatures represents the image properties which are retrived from data stream.

func GetFeatures

func GetFeatures(data []byte) (f *BitstreamFeatures, err error)

GetFeatures returns features as BitstreamFeatures retrived from data stream.

type ColorSpace

type ColorSpace int

ColorSpace represents encoding color space in WebP

const (
	// YUV420 specifies YUV4:2:0
	YUV420 ColorSpace = C.WEBP_YUV420
	// YUV420A specifies YUV4:2:0 with alpha channel
	YUV420A ColorSpace = C.WEBP_YUV420A
)

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config specifies WebP encoding configuration.

func ConfigLosslessPreset

func ConfigLosslessPreset(level int) (*Config, error)

ConfigLosslessPreset returns initialized configuration for lossless encoding. Given level specifies desired efficiency level between 0 (fastest, lowest compression) and 9 (slower, best compression).

func ConfigPreset

func ConfigPreset(preset Preset, quality float32) (*Config, error)

ConfigPreset returns initialized configuration with given preset and quality factor.

func (*Config) AlphaCompression

func (c *Config) AlphaCompression() int

AlphaCompression returns alpha compression parameter.

func (*Config) AlphaFiltering

func (c *Config) AlphaFiltering() int

AlphaFiltering returns alpha filtering parameter.

func (*Config) AlphaQuality

func (c *Config) AlphaQuality() int

AlphaQuality returns alpha quality parameter.

func (*Config) AutoFilter

func (c *Config) AutoFilter() bool

AutoFilter returns auto filter flag.

func (*Config) EmulateJPEGSize

func (c *Config) EmulateJPEGSize() bool

EmulateJPEGSize returns the flag whether to enable emulating JPEG size.

func (*Config) Exact

func (c *Config) Exact() bool

Exact returns exact flag.

func (*Config) FilterSharpness

func (c *Config) FilterSharpness() int

FilterSharpness returns filter sharpness parameter.

func (*Config) FilterStrength

func (c *Config) FilterStrength() int

FilterStrength returns filter strength parameter.

func (*Config) FilterType

func (c *Config) FilterType() FilterType

FilterType returns filter type parameter.

func (*Config) ImageHint

func (c *Config) ImageHint() ImageHint

ImageHint returns hint parameter for image type.

func (*Config) Lossless

func (c *Config) Lossless() bool

Lossless returns lossless parameter flag whether to enable lossless encoding.

func (*Config) LowMemory

func (c *Config) LowMemory() bool

LowMemory returns low memory flag.

func (*Config) Method

func (c *Config) Method() int

Method returns method parameter.

func (*Config) NearLossless

func (c *Config) NearLossless() int

NearLossless returns near lossless encoding factor.

func (*Config) PartitionLimit

func (c *Config) PartitionLimit() int

PartitionLimit returns partition limit parameter.

func (*Config) Partitions

func (c *Config) Partitions() int

Partitions returns partitions parameter.

func (*Config) Pass

func (c *Config) Pass() int

Pass returns pass parameter.

func (*Config) Preprocessing

func (c *Config) Preprocessing() Preprocessing

Preprocessing returns preprocessing filter.

func (*Config) Quality

func (c *Config) Quality() float32

Quality returns encoding quality factor.

func (*Config) SNSStrength

func (c *Config) SNSStrength() int

SNSStrength returns SNS strength parameter.

func (*Config) Segments

func (c *Config) Segments() int

Segments returns segments parameter.

func (*Config) SetAlphaCompression

func (c *Config) SetAlphaCompression(v int)

SetAlphaCompression sets alpha compression parameter.

func (*Config) SetAlphaFiltering

func (c *Config) SetAlphaFiltering(v int)

SetAlphaFiltering sets alpha filtering parameter.

func (*Config) SetAlphaQuality

func (c *Config) SetAlphaQuality(v int)

SetAlphaQuality sets alpha quality parameter.

func (*Config) SetAutoFilter

func (c *Config) SetAutoFilter(v bool)

SetAutoFilter sets auto filter flag that specifies whether to auto adjust filter strength.

func (*Config) SetEmulateJPEGSize

func (c *Config) SetEmulateJPEGSize(v bool)

SetEmulateJPEGSize sets flag whether the compression parameters remaps to match the expected output size from JPEG compression.

func (*Config) SetExact

func (c *Config) SetExact(v bool)

SetExact sets the flag whether to preserve the exact RGB values under transparent area.

func (*Config) SetFilterSharpness

func (c *Config) SetFilterSharpness(v int)

SetFilterSharpness sets filter sharpness parameter between 0 (off) and 7 (least sharp).

func (*Config) SetFilterStrength

func (c *Config) SetFilterStrength(v int)

SetFilterStrength sets filter strength parameter between 0 (off) and 100 (strongest).

func (*Config) SetFilterType

func (c *Config) SetFilterType(v FilterType)

SetFilterType sets filter type parameter.

func (*Config) SetImageHint

func (c *Config) SetImageHint(v ImageHint)

SetImageHint sets hint for image type. It is used to only lossless encoding for now.

func (*Config) SetLossless

func (c *Config) SetLossless(v bool)

SetLossless sets lossless parameter that specifies whether to enable lossless encoding.

func (*Config) SetLowMemory

func (c *Config) SetLowMemory(v bool)

SetLowMemory sets flag whether to reduce memory usage.

func (*Config) SetMethod

func (c *Config) SetMethod(v int)

SetMethod sets method parameter that specifies quality/speed trade-off (0=fast, 6=slower-better).

func (*Config) SetNearLossless

func (c *Config) SetNearLossless(v int)

SetNearLossless sets near lossless encoding factor between 0 (max loss) and 100 (disable near lossless encoding, default).

func (*Config) SetPartitionLimit

func (c *Config) SetPartitionLimit(v int)

SetPartitionLimit returns partition limit parameter.

func (*Config) SetPartitions

func (c *Config) SetPartitions(v int)

SetPartitions sets partitions parameter.

func (*Config) SetPass

func (c *Config) SetPass(v int)

SetPass sets pass parameter that specifies number of entropy-analysis passes between 1 and 10.

func (*Config) SetPreprocessing

func (c *Config) SetPreprocessing(v Preprocessing)

SetPreprocessing sets preprocessing filter.

func (*Config) SetQuality

func (c *Config) SetQuality(v float32)

SetQuality sets encoding quality factor between 0 (smallest file) and 100 (biggest).

func (*Config) SetSNSStrength

func (c *Config) SetSNSStrength(v int)

SetSNSStrength sets SNS strength parameter between 0 (off) and 100 (maximum).

func (*Config) SetSegments

func (c *Config) SetSegments(v int)

SetSegments sets segments parameter that specifies the maximum number of segments to use, in [1..4].

func (*Config) SetTargetPSNR

func (c *Config) SetTargetPSNR(v float32)

SetTargetPSNR sets target PSNR value that specifies the minimal distortion to try to achieve. If it sets 0, disable target PSNR.

func (*Config) SetThreadLevel

func (c *Config) SetThreadLevel(v int)

SetThreadLevel sets thread level parameter. If non-zero value is specified, try and use multi-threaded encoding.

func (*Config) TargetPSNR

func (c *Config) TargetPSNR() float32

TargetPSNR returns target PSNR value.

func (*Config) ThreadLevel

func (c *Config) ThreadLevel() int

ThreadLevel returns thread level parameter.

type DecoderOptions

type DecoderOptions struct {
	BypassFiltering        bool            // If true, bypass filtering process
	NoFancyUpsampling      bool            // If true, do not fancy upsampling
	Crop                   image.Rectangle // Do cropping if image.Rectangle is not empty.
	Scale                  image.Rectangle // Do scaling if image.Rectangle is not empty.
	UseThreads             bool            // If true, use multi threads
	DitheringStrength      int             // Specify dithering strength [0=Off .. 100=full]
	Flip                   bool            // If true, flip output vertically
	AlphaDitheringStrength int             // Specify alpha dithering strength in [0..100]
}

DecoderOptions specifies decoding options of WebP.

type FilterType

type FilterType int

FilterType corresponds to filter types in compression parameters.

const (
	// SimpleFilter (=0, default)
	SimpleFilter FilterType = iota
	// StrongFilter (=1)
	StrongFilter
)

type ImageHint

type ImageHint int

ImageHint corresponds to C.WebPImageHint.

const (
	HintDefault ImageHint = C.WEBP_HINT_DEFAULT
	HintPicture ImageHint = C.WEBP_HINT_PICTURE
	HintPhoto   ImageHint = C.WEBP_HINT_PHOTO
	HintGraph   ImageHint = C.WEBP_HINT_GRAPH
	HintLast    ImageHint = C.WEBP_HINT_LAST
)

type Preprocessing

type Preprocessing int

Preprocessing corresponds to preprocessing filter parameter.

type Preset

type Preset int

Preset corresponds to C.WebPPreset.

const (
	// PresetDefault corresponds to WEBP_PRESET_DEFAULT, for default preset.
	PresetDefault Preset = C.WEBP_PRESET_DEFAULT
	// PresetPicture corresponds to WEBP_PRESET_PICTURE, for digital picture, like portrait, inner shot
	PresetPicture Preset = C.WEBP_PRESET_PICTURE
	// PresetPhoto corresponds to WEBP_PRESET_PHOTO, for outdoor photograph, with natural lighting
	PresetPhoto Preset = C.WEBP_PRESET_PHOTO
	// PresetDrawing corresponds to WEBP_PRESET_DRAWING, for hand or line drawing, with high-contrast details
	PresetDrawing Preset = C.WEBP_PRESET_DRAWING
	// PresetIcon corresponds to WEBP_PRESET_ICON, for small-sized colorful images
	PresetIcon Preset = C.WEBP_PRESET_ICON
	// PresetText corresponds to WEBP_PRESET_TEXT, for text-like
	PresetText Preset = C.WEBP_PRESET_TEXT
)

type RGB

type RGB struct {
	R, G, B uint8
}

RGB color

func (RGB) RGBA

func (c RGB) RGBA() (r, g, b, a uint32)

RGBA implements Color.RGBA

type RGBImage

type RGBImage struct {
	// Pix holds the image's stream, in R, G, B order.
	Pix []uint8
	// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
	Stride int
	// Rect is the image's bounds.
	Rect image.Rectangle
}

RGBImage represent image data which has RGB colors. RGBImage is compatible with image.RGBA, but does not have alpha channel to reduce using memory.

func NewRGBImage

func NewRGBImage(r image.Rectangle) *RGBImage

NewRGBImage allocates and returns RGB image

func (*RGBImage) At

func (p *RGBImage) At(x, y int) color.Color

At implements image.Image.At

func (*RGBImage) Bounds

func (p *RGBImage) Bounds() image.Rectangle

Bounds implements image.Image.At

func (*RGBImage) ColorModel

func (p *RGBImage) ColorModel() color.Model

ColorModel returns RGB color model.

func (*RGBImage) RGBAAt

func (p *RGBImage) RGBAAt(x, y int) color.RGBA

RGBAAt returns the color of the pixel at (x, y) as RGBA.

type YUVAImage

type YUVAImage struct {
	Y, Cb, Cr, A []uint8
	YStride      int
	CStride      int
	AStride      int
	ColorSpace   ColorSpace
	Rect         image.Rectangle
}

YUVAImage represents a image of YUV colors with alpha channel image.

YUVAImage contains decoded YCbCr image data with alpha channel, but it is not compatible with image.YCbCr. Because, the RGB-YCbCr conversion that used in WebP is following to ITU-R BT.601 standard. In contrast, the conversion of Image.YCbCr (and color.YCbCrModel) is following to the JPEG standard (JFIF). If you need the image as image.YCBCr, you will first convert from WebP to RGB image, then convert from RGB image to JPEG's YCbCr image.

See: http://en.wikipedia.org/wiki/YCbCr

func DecodeYUVA

func DecodeYUVA(data []byte, options *DecoderOptions) (img *YUVAImage, err error)

DecodeYUVA decodes WebP image into YUV image with alpha channel, and returns it as *YUVAImage.

func NewYUVAImage

func NewYUVAImage(r image.Rectangle, c ColorSpace) (image *YUVAImage)

NewYUVAImage creates and allocates image buffer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL