From a2426b8f87a115bfdce4ea72fdcad9fd99675fd7 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Wed, 12 Jun 2024 16:19:31 -0300 Subject: [PATCH] fix: type coersion I should probably have testes this thing before commiting --- internals/image.go | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/internals/image.go b/internals/image.go index 7596a51..8d372f7 100644 --- a/internals/image.go +++ b/internals/image.go @@ -16,7 +16,7 @@ import ( ) type Image struct { - image.Image + img image.Image mime string JpegOptions jpeg.Options GifOptions gif.Options @@ -47,21 +47,21 @@ func NewImage(b []byte) (Image, error) { return Image{}, err } - image := img.(Image) - image.mime = m - image.JpegOptions = jpeg.Options{ - Quality: 70, - } - image.GifOptions = gif.Options{ - NumColors: 256 / 2, - } - image.WebpOptions = webp.Options{ - Lossless: true, - Quality: 70.0, - Exact: true, - } - - return image, nil + return Image{ + img: img, + mime: m, + JpegOptions: jpeg.Options{ + Quality: 70, + }, + GifOptions: gif.Options{ + NumColors: 256, + }, + WebpOptions: webp.Options{ + Lossless: true, + Quality: 70.0, + Exact: true, + }, + }, nil } @@ -76,13 +76,13 @@ func (i *Image) Encode(w io.Writer) error { switch i.mime { case "image/png": - err = png.Encode(w, i.Image) + err = png.Encode(w, i.img) case "image/jpeg": - err = jpeg.Encode(w, i.Image, &jpeg.Options{Quality: 70}) + err = jpeg.Encode(w, i.img, &jpeg.Options{Quality: 70}) case "image/gif": - err = gif.Encode(w, i.Image, &gif.Options{NumColors: 256}) + err = gif.Encode(w, i.img, &gif.Options{NumColors: 256}) case "image/webp": - err = webp.Encode(w, i.Image, &webp.Options{Lossless: true}) + err = webp.Encode(w, i.img, &webp.Options{Lossless: true}) default: err = errors.ErrUnsupported } @@ -99,7 +99,7 @@ func (i *Image) Quality(q float64) { } func (i *Image) Optimize(threshold int) { - w := i.Bounds().Max.X + w := i.img.Bounds().Max.X if threshold >= w { return @@ -110,7 +110,7 @@ func (i *Image) Optimize(threshold int) { } func (i *Image) Scale(s int) { - r := i.Bounds() + r := i.img.Bounds() w, h := r.Max.X, r.Max.Y var nw, nh int @@ -124,7 +124,7 @@ func (i *Image) Scale(s int) { nw, nh = w, h } - imaging.Resize(i, nw, nh, imaging.CatmullRom) + imaging.Resize(i.img, nw, nh, imaging.CatmullRom) } func (i *Image) GetMime() string {