mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
detect gray to avoid convertion
This commit is contained in:
parent
74c8a884f1
commit
3d1edaee0b
@ -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) {
|
||||||
draw.Draw(newImg, newImg.Bounds(), img, image.Point{}, draw.Src)
|
case *image.Gray:
|
||||||
|
c.img = imgt
|
||||||
c.img = newImg
|
default:
|
||||||
|
newImg := image.NewGray(img.Bounds())
|
||||||
return c
|
draw.Draw(newImg, newImg.Bounds(), img, image.Point{}, draw.Src)
|
||||||
}
|
c.img = newImg
|
||||||
|
|
||||||
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user