2018-01-13 16:14:54 +00:00
|
|
|
package utio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/png"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PNGer describes a PNG encoder/decoder.
|
2021-02-11 19:33:06 +00:00
|
|
|
type PNGer Imager
|
2018-01-13 16:14:54 +00:00
|
|
|
|
|
|
|
// PNG creates a PNG encoder/decoder for m, m is only required for encoding.
|
|
|
|
func PNG(m image.Image) PNGer {
|
2021-02-11 19:33:06 +00:00
|
|
|
return &pnger{imager{m}}
|
2018-01-13 16:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type pnger struct {
|
2021-02-11 19:33:06 +00:00
|
|
|
imager
|
2018-01-13 16:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|