detect gray to avoid convertion

This commit is contained in:
Celogeek 2022-12-25 23:00:38 +01:00
parent 74c8a884f1
commit 3d1edaee0b
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A

View File

@ -10,7 +10,7 @@ import (
type ComicConverter struct { type ComicConverter struct {
Options ComicConverterOptions Options ComicConverterOptions
img image.Image img *image.Gray
} }
type ComicConverterOptions struct { type ComicConverterOptions struct {
@ -33,25 +33,15 @@ func (c *ComicConverter) Load(file string) *ComicConverter {
panic(err) panic(err)
} }
newImg := image.NewRGBA(img.Bounds()) switch imgt := img.(type) {
case *image.Gray:
c.img = imgt
default:
newImg := image.NewGray(img.Bounds())
draw.Draw(newImg, newImg.Bounds(), img, image.Point{}, draw.Src) draw.Draw(newImg, newImg.Bounds(), img, image.Point{}, draw.Src)
c.img = newImg c.img = newImg
return c
} }
func (c *ComicConverter) GrayScale() *ComicConverter {
if c.img == nil {
panic("load image first")
}
newImg := image.NewGray(c.img.Bounds())
draw.Draw(newImg, newImg.Bounds(), c.img, image.Point{}, draw.Src)
c.img = newImg
return c return c
} }
@ -107,14 +97,14 @@ BOTTOM:
imgArea.Max.Y-- imgArea.Max.Y--
} }
newImg := image.NewRGBA(image.Rectangle{ newImg := image.NewGray(image.Rectangle{
Min: image.Point{0, 0}, Min: image.Point{0, 0},
Max: image.Point{imgArea.Dx(), imgArea.Dy()}, Max: image.Point{imgArea.Dx(), imgArea.Dy()},
}) })
draw.Draw(newImg, newImg.Bounds(), c.img, imgArea.Min, draw.Src) draw.Draw(newImg, newImg.Bounds(), c.img, imgArea.Min, draw.Src)
c.img = newImg c.img = c.img.SubImage(imgArea).(*image.Gray)
return c return c
} }
@ -128,18 +118,22 @@ func (c *ComicConverter) Resize(w, h int) *ComicConverter {
origWidth := dim.Dx() origWidth := dim.Dx()
origHeight := dim.Dy() origHeight := dim.Dy()
width, heigth := origWidth*h/origHeight, origHeight*w/origWidth width, height := 1, 1
if origHeight > 0 && origWidth > 0 {
width, height = origWidth*h/origHeight, origHeight*w/origWidth
}
if width > w { if width > w {
width = w width = w
} }
if heigth > h { if height > h {
heigth = h height = h
} }
newImg := image.NewRGBA(image.Rectangle{ newImg := image.NewGray(image.Rectangle{
Min: image.Point{0, 0}, Min: image.Point{0, 0},
Max: image.Point{width, heigth}, Max: image.Point{width, height},
}) })
draw.BiLinear.Scale(newImg, newImg.Bounds(), c.img, c.img.Bounds(), draw.Src, nil) draw.BiLinear.Scale(newImg, newImg.Bounds(), c.img, c.img.Bounds(), draw.Src, nil)