ut/utio/png.go

48 lines
842 B
Go
Raw Normal View History

package utio
import (
"image"
"image/png"
"io"
)
// PNGer describes a PNG encoder/decoder.
type PNGer Imager
// PNG creates a PNG encoder/decoder for m, m is only required for encoding.
func PNG(m image.Image) PNGer {
return &pnger{imager{m}}
}
type pnger struct {
imager
}
func (p *pnger) Encode(w io.Writer) error {
return png.Encode(w, p.m)
}
func (p *pnger) Decode(r io.Reader) error {
var m, err = png.Decode(r)
if nil != err {
return err
}
p.m = m
return nil
}
// LoadPNG loads a PNG image from the supplied path.
func LoadPNG(path string) (image.Image, error) {
var p = PNG(nil)
var err = DecodeFile(path, p)
if nil != err {
return nil, err
}
return p.Image(), nil
}
// SavePNG writes an image as PNG to the supplied path.
func SavePNG(path string, m image.Image) error {
return EncodeFile(path, PNG(m))
}