Overview

Package png implements a PNG image decoder and encoder.

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

Index

paeth.go writer.go

func

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. c := color.GrayModel.Convert(img.At(x, y)).(color.Gray)
  10. level := c.Y / 51 // 51 * 5 = 255
  11. if level == 5 {
  12. level--
  13. fmt.Print(levels[level])
  14. }
  15. fmt.Print("\n")
  16. }

  1. func DecodeConfig(r io.) (image., error)

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

func

  1. func Encode(w .Writer, m .Image)

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. })
  11. }
  12. }
  13. f, err := os.Create("image.png")
  14. log.Fatal(err)
  15. }
  16. if err := png.Encode(f, img); err != nil {
  17. f.Close()
  18. log.Fatal(err)
  19. }
  20. if err := f.Close(); err != nil {
  21. }

type

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

type

  1. type Encoder struct {
  2. 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.

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

Encode writes the Image m to w in PNG format.

  1. type EncoderBuffer encoder

type

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

  1. type FormatError

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

  1. func (e FormatError) Error()

type UnsupportedError

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

    1. func (e UnsupportedError) Error()