Documentation
¶
Overview ¶
Package apng implements an APNG image decoder.
The PNG specification is at https://www.w3.org/TR/PNG/. The APNG specification is at https://wiki.mozilla.org/APNG_Specification
Index ¶
- Constants
- func Decode(r io.Reader) (image.Image, error)
- func DecodeConfig(r io.Reader) (image.Config, error)
- func Encode(w io.Writer, a APNG) error
- type APNG
- type CompressionLevel
- type CompressionWriter
- type Encoder
- type EncoderBuffer
- type EncoderBufferPool
- type FormatError
- type Frame
- type UnsupportedError
Examples ¶
Constants ¶
const ( DISPOSE_OP_NONE = 0 DISPOSE_OP_BACKGROUND = 1 DISPOSE_OP_PREVIOUS = 2 )
dispose_op values, as per the APNG spec.
const ( BLEND_OP_SOURCE = 0 BLEND_OP_OVER = 1 )
blend_op values, as per the APNG spec.
Variables ¶
This section is empty.
Functions ¶
func DecodeConfig ¶
DecodeConfig returns the color model and dimensions of a PNG image without decoding the entire image.
func Encode ¶
Encode writes the APNG a to w in PNG format. Any Image may be encoded, but images that are not image.NRGBA might be encoded lossily.
Example ¶
w, h := 200, 200
am := APNG{Frames: make([]Frame, 20)}
var circles [3]image.Point
for i := range am.Frames {
im := image.NewRGBA(image.Rect(0, 0, w, h))
am.Frames[i].Image = im
theta := float64(i) * 2 * math.Pi / float64(len(am.Frames))
for c := range circles {
theta0 := float64(c) * 2 * math.Pi / 3
circles[c].X = int(float64(w) * (0.5 - 0.15*math.Sin(theta0) - 0.1*math.Sin(theta0+theta)))
circles[c].Y = int(float64(h) * (0.5 - 0.15*math.Cos(theta0) - 0.1*math.Cos(theta0+theta)))
}
for y := range h {
for x := range w {
var rgb [3]uint8
for c := range circles {
dx, dy := x-circles[c].X, y-circles[c].Y
if dx*dx+dy*dy < int(float64(w*h)/20) {
rgb[c] = 0xff
}
}
im.Set(x, y, color.RGBA{R: rgb[0], G: rgb[1], B: rgb[2], A: 0xff})
}
}
}
f, err := os.Create("rgb.png")
if err != nil {
panic(err)
}
defer f.Close()
if err := Encode(f, am); err != nil {
panic(err)
}
Types ¶
type APNG ¶
type APNG struct {
Frames []Frame
// LoopCount defines the number of times an animation will be
// restarted during display.
// A LoopCount of 0 means to loop forever
LoopCount uint
}
func DecodeAll ¶
DecodeAll reads an APNG file from r and returns it as an APNG Type. If the first frame returns true for IsDefault(), that frame should not be part of the result. The type of Image returned depends on the PNG contents.
Example ¶
am, err := DecodeAll(gopherPNG())
if err != nil {
panic(err)
}
levels := []rune(" ░▒▓█")
loops := int64(am.LoopCount)
if loops == 0 {
loops = -1
}
rect := am.Frames[0].Image.Bounds()
buf := make([][]rune, rect.Dy())
for y := range buf {
buf[y] = make([]rune, rect.Dx())
for x := range buf[y] {
buf[y][x] = ' '
}
}
n := time.Now()
print("\x1b[2J") // clear screen
for loops != 0 {
for i, fr := range am.Frames {
for y := 0; y < fr.Image.Bounds().Dy(); y++ {
for x := 0; x < fr.Image.Bounds().Dx(); x++ {
c := fr.Image.At(x, y)
if _, _, _, a := c.RGBA(); a > 0 {
buf[y+fr.YOffset][x+fr.XOffset] = levels[(color.GrayModel.Convert(c).(color.Gray).Y / 52)]
} else if fr.BlendOp == BLEND_OP_SOURCE {
buf[y+fr.YOffset][x+fr.XOffset] = ' '
}
}
}
print("\x1b[H") // move top left
for y := range buf {
println(string(buf[y]))
}
time.Sleep(time.Duration(float64(time.Second)*float64(fr.DelayNumerator)/float64(fr.DelayDenominator)) - time.Since(n))
n = time.Now()
if fr.DisposeOp == DISPOSE_OP_BACKGROUND || i == len(am.Frames)-1 {
for y := 0; y < fr.Image.Bounds().Dy(); y++ {
for x := 0; x < fr.Image.Bounds().Dx(); x++ {
buf[y+fr.YOffset][x+fr.XOffset] = ' '
}
}
}
}
if loops > 0 {
loops--
}
}
type CompressionLevel ¶
type CompressionLevel int
CompressionLevel indicates the compression level.
const ( DefaultCompression CompressionLevel = 0 NoCompression CompressionLevel = -1 BestSpeed CompressionLevel = -2 BestCompression CompressionLevel = -3 )
type CompressionWriter ¶
type CompressionWriter interface {
Write(p []byte) (n int, err error)
Reset(w io.Writer)
Close() error
}
CompressionWriter zlib compression writer interface.
type Encoder ¶
type Encoder struct {
CompressionLevel CompressionLevel
// BufferPool optionally specifies a buffer pool to get temporary
// EncoderBuffers when encoding an image.
BufferPool EncoderBufferPool
// CompressionWriter optionally provides a external zlib compression
// writer for writing PNG image data.
CompressionWriter func(w io.Writer) (CompressionWriter, error)
}
Encoder configures encoding PNG images.
type EncoderBuffer ¶
type EncoderBuffer encoder
EncoderBuffer holds the buffers used for encoding PNG images.
type EncoderBufferPool ¶
type EncoderBufferPool interface {
Get() *EncoderBuffer
Put(*EncoderBuffer)
}
EncoderBufferPool is an interface for getting and returning temporary instances of the EncoderBuffer struct. This can be used to reuse buffers when encoding multiple images.
type FormatError ¶
type FormatError string
A FormatError reports that the input is not a valid PNG.
func (FormatError) Error ¶
func (e FormatError) Error() string
type Frame ¶
type Frame struct {
Image image.Image
XOffset, YOffset int
DelayNumerator uint16
DelayDenominator uint16
DisposeOp byte
BlendOp byte
// IsDefault indicates if the Frame is a default image that
// should not be used in the animation. IsDefault can only
// be true on the first frame.
IsDefault bool
// contains filtered or unexported fields
}
type UnsupportedError ¶
type UnsupportedError string
An UnsupportedError reports that the input uses a valid but unimplemented PNG feature.
func (UnsupportedError) Error ¶
func (e UnsupportedError) Error() string