ut/utio/jpeg.go

51 lines
936 B
Go
Raw Permalink Normal View History

package utio
import (
"image"
"image/jpeg"
"io"
)
// JPEG creates a JPEG encoder/decoder for m, m is only required for encoding.
func JPEG(m image.Image) Imager {
return JPEGOpts(m, 95)
}
func JPEGOpts(m image.Image, quality int) Imager {
return &jpeger{imager{m}, quality}
}
type jpeger struct {
imager
quality int
}
func (j *jpeger) Encode(w io.Writer) error {
return jpeg.Encode(w, j.m, &jpeg.Options{Quality: j.quality})
}
func (j *jpeger) Decode(r io.Reader) error {
var m, err = jpeg.Decode(r)
if nil != err {
return err
}
j.m = m
return nil
}
// LoadJPEG loads a JPEG image from the supplied path.
func LoadJPEG(path string) (image.Image, error) {
var j = JPEG(nil)
var err = DecodeFile(path, j)
if nil != err {
return nil, err
}
return j.Image(), nil
}
// SaveJPEG writes an image as JPEG to the supplied path.
func SaveJPEG(path string, m image.Image) error {
return EncodeFile(path, JPEG(m))
}