version: 1.10

package png

import "image/png"

Overview

Package png implements a PNG image decoder and encoder.

The PNG specification is at http://www.w3.org/TR/PNG/.

Index

Examples

Package files

paeth.go reader.go writer.go

func Decode

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

Decode reads a PNG image from r and returns it as an image.Image. The type of
Image returned depends on the PNG contents.


Example:

  1. // This example uses png.Decode which can only decode PNG images.
  2. // Consider using the general image.Decode as it can sniff and decode any registered image format.
  3. img, err := png.Decode(gopherPNG())
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. levels := []string{" ", "░", "▒", "▓", "█"}
  8. for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
  9. for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
  10. c := color.GrayModel.Convert(img.At(x, y)).(color.Gray)
  11. level := c.Y / 51 // 51 * 5 = 255
  12. if level == 5 {
  13. level--
  14. }
  15. fmt.Print(levels[level])
  16. }
  17. fmt.Print("\n")
  18. }

func DecodeConfig

  1. 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

  1. func Encode(w io.Writer, m image.Image) error

Encode writes the Image m to w in PNG format. Any Image may be encoded, but
images that are not image.NRGBA might be encoded lossily.


Example:

  1. const width, height = 256, 256
  2. // Create a colored image of the given width and height.
  3. img := image.NewNRGBA(image.Rect(0, 0, width, height))
  4. for y := 0; y < height; y++ {
  5. for x := 0; x < width; x++ {
  6. img.Set(x, y, color.NRGBA{
  7. R: uint8((x + y) & 255),
  8. G: uint8((x + y) << 1 & 255),
  9. B: uint8((x + y) << 2 & 255),
  10. A: 255,
  11. })
  12. }
  13. }
  14. f, err := os.Create("image.png")
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. if err := png.Encode(f, img); err != nil {
  19. f.Close()
  20. log.Fatal(err)
  21. }
  22. if err := f.Close(); err != nil {
  23. log.Fatal(err)
  24. }

type CompressionLevel

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

type Encoder

  1. type Encoder struct {
  2. CompressionLevel CompressionLevel
  3.  
  4. // BufferPool optionally specifies a buffer pool to get temporary
  5. // EncoderBuffers when encoding an image.
  6. BufferPool EncoderBufferPool
  7. }

Encoder configures encoding PNG images.

func (*Encoder) Encode

  1. func (enc *Encoder) Encode(w io.Writer, m image.Image) error

Encode writes the Image m to w in PNG format.

type EncoderBuffer

  1. type EncoderBuffer encoder

EncoderBuffer holds the buffers used for encoding PNG images.

type EncoderBufferPool

  1. type EncoderBufferPool interface {
  2. Get() *EncoderBuffer
  3. Put(*EncoderBuffer)
  4. }

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

  1. type FormatError string

A FormatError reports that the input is not a valid PNG.

func (FormatError) Error

  1. func (e FormatError) Error() string

type UnsupportedError

  1. type UnsupportedError string

An UnsupportedError reports that the input uses a valid but unimplemented PNG
feature.

func (UnsupportedError) Error

  1. func (e UnsupportedError) Error() string