apng

package module
v0.0.0-...-80a546b Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2018 License: BSD-3-Clause Imports: 10 Imported by: 0

README

APNG golang library

This apng package provides methods for decoding and encoding APNG files. It is based upon the work in the official "image/png" package.

NOTE: The decoder should work for most anything you throw at it. Malformed PNGs should result in an error message. The encoder currently doesn't handle differences of Image formats and similar and has not been tested as thoroughly.

If a regular PNG file is read, the first Frame of the APNG returned by DecodeAll(*File) will be the PNG data.

Types

APNG

The APNG type contains the frames of a decoded .apng file, along with any important properties. It may also be created and used for Encoding.

Signature Description
Frames []Frame The stored frames of the APNG.
LoopCount uint The number of times an animation should be restarted during display. A value of 0 means to loop forever.
Frame

The Frame type contains an individual frame of an APNG. The following table provides the important properties and methods.

Signature Description
Image image.Image Frame image data.
IsDefault bool Indicates if this frame is a default image that should not be included as part of the animation frames. May only be true for the first Frame.
XOffset int Returns the x offset of the frame.
YOffset int Returns the y offset of the frame.
DelayNumerator int Returns the delay numerator.
DelayDenominator int Returns the delay denominator.
DisposeOp byte Returns the frame disposal operation. May be apng.DISPOSE_OP_NONE, apng.DISPOSE_OP_BACKGROUND, or apng.DISPOSE_OP_PREVIOUS. See the APNG Specification for more information.
BlendOp byte Returns the frame blending operation. May be apng.BLEND_OP_SOURCE or apng.BLEND_OP_OVER. See the APNG Specification for more information.

Methods

DecodeAll(io.Reader) (APNG, error)

This method returns an APNG type containing the frames and associated data within the passed file.

Example
package main

import (
  "github.com/kettek/apng"
  "os"
  "log"
)

func main() {
  // Open our animated PNG file
  f, err := os.Open("animation.png")
  if err != nil {
    panic(err)
  }
  defer f.Close()
  // Decode all frames into an APNG
  a, err := apng.DecodeAll(f)
  if err != nil {
    panic(err)
  }
  // Print some information on the APNG
  log.Printf("Found %d frames\n", len(a.Frames))
  for i, frame := range a.Frames {
    b := frame.Image.Bounds()
    log.Printf("Frame %d: %dx%d\n", i, b.Max.X, b.Max.Y)
  }
}

DecodeFirst(io.Reader) (image.Image, error)

This method returns the Image of the default frame of an APNG file.

Encode(io.Writer, APNG) error

This method writes the passed APNG object to the given io.Writer as an APNG binary file.

Example
package main

import (
  "github.com/kettek/apng"
  "image/png"
  "os"
)

func main() {
  // Define our variables
  output := "animation.png"
  images := [4]string{"0.png", "1.png", "2.png", "3.png"}
  a := apng.APNG{
    Frames: make([]apng.Frame, len(images)),
  }
  // Open our file for writing
  out, err := os.Create(output)
  if err != nil {
    panic(err)
  }
  defer out.Close()
  // Assign each decoded PNG's Image to the appropriate Frame Image
  for i, s := range images {
    in, err := os.Open(s)
    if err != nil {
      panic(err)
    }
    defer in.Close()
    m, err := png.Decode(in)
    if err != nil {
      panic(err)
    }
    a.Frames[i].Image = m
  }
  // Write APNG to our output file
  apng.Encode(out, a)
}

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

View Source
const (
	DISPOSE_OP_NONE       = 0
	DISPOSE_OP_BACKGROUND = 1
	DISPOSE_OP_PREVIOUS   = 2
)

dispose_op values, as per the APNG spec.

View Source
const (
	BLEND_OP_SOURCE = 0
	BLEND_OP_OVER   = 1
)

blend_op values, as per the APNG spec.

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader) (image.Image, error)

Decode reads an APNG file from r and returns the default image.

func DecodeConfig

func DecodeConfig(r io.Reader) (image.Config, error)

DecodeConfig returns the color model and dimensions of a PNG image without decoding the entire image.

func Encode

func Encode(w io.Writer, a APNG) error

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.

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

func DecodeAll(r io.Reader) (APNG, error)

Decode 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 a. The type of Image returned depends on the PNG contents.

type CompressionLevel

type CompressionLevel int
const (
	DefaultCompression CompressionLevel = 0
	NoCompression      CompressionLevel = -1
	BestSpeed          CompressionLevel = -2
	BestCompression    CompressionLevel = -3
)

type Encoder

type Encoder struct {
	CompressionLevel CompressionLevel

	// BufferPool optionally specifies a buffer pool to get temporary
	// EncoderBuffers when encoding an image.
	BufferPool EncoderBufferPool
}

Encoder configures encoding PNG images.

func (*Encoder) Encode

func (enc *Encoder) Encode(w io.Writer, a APNG) error

Encode writes the Animation a to w in PNG format.

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
}

func (*Frame) GetDelay

func (f *Frame) GetDelay() float64

GetDelay returns the number of seconds in the frame.

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

Jump to

Keyboard shortcuts

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