package utio import ( "image" "image/png" "io" ) // PNGer describes a PNG encoder/decoder. type PNGer interface { Encoder Decoder Image() image.Image } // PNG creates a PNG encoder/decoder for m, m is only required for encoding. func PNG(m image.Image) PNGer { return &pnger{m} } type pnger struct { m image.Image } 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 } func (p *pnger) Image() image.Image { return p.m } // 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)) }