Package png
In the call graph viewer below, each node
is a function belonging to this package
and its children are the functions it
calls—perhaps dynamically.
The root nodes are the entry points of the
package: functions that may be called from
outside the package.
There may be non-exported or anonymous
functions among them if they are called
dynamically from another package.
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring func
token.
Functions may be omitted if they were
determined to be unreachable in the
particular programs or tests that were
analyzed.
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
Code:
img, err := png.Decode(gopherPNG())
if err != nil {
log.Fatal(err)
}
levels := []string{" ", "░", "▒", "▓", "█"}
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
c := color.GrayModel.Convert(img.At(x, y)).(color.Gray)
level := c.Y / 51
if level == 5 {
level--
}
fmt.Print(levels[level])
}
fmt.Print("\n")
}
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(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
Code:
const width, height = 256, 256
img := image.NewNRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.Set(x, y, color.NRGBA{
R: uint8((x + y) & 255),
G: uint8((x + y) << 1 & 255),
B: uint8((x + y) << 2 & 255),
A: 255,
})
}
}
f, err := os.Create("image.png")
if err != nil {
log.Fatal(err)
}
if err := png.Encode(f, img); err != nil {
f.Close()
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
type CompressionLevel int
const (
DefaultCompression CompressionLevel = 0
NoCompression CompressionLevel = -1
BestSpeed CompressionLevel = -2
BestCompression CompressionLevel = -3
)
Encoder configures encoding PNG images.
type Encoder struct {
CompressionLevel CompressionLevel
}
func (*Encoder) Encode
¶
func (enc *Encoder) Encode(w io.Writer, m image.Image) error
Encode writes the Image m to w in PNG format.
A FormatError reports that the input is not a valid PNG.
type FormatError string
func (e FormatError) Error() string
An UnsupportedError reports that the input uses a valid but unimplemented PNG feature.
type UnsupportedError string
func (UnsupportedError) Error
¶
func (e UnsupportedError) Error() string