mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 07:42:37 +02:00
detect gray to avoid convertion
This commit is contained in:
parent
74c8a884f1
commit
3d1edaee0b
@ -10,7 +10,7 @@ import (
|
||||
|
||||
type ComicConverter struct {
|
||||
Options ComicConverterOptions
|
||||
img image.Image
|
||||
img *image.Gray
|
||||
}
|
||||
|
||||
type ComicConverterOptions struct {
|
||||
@ -33,25 +33,15 @@ func (c *ComicConverter) Load(file string) *ComicConverter {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
newImg := image.NewRGBA(img.Bounds())
|
||||
draw.Draw(newImg, newImg.Bounds(), img, image.Point{}, draw.Src)
|
||||
|
||||
c.img = newImg
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *ComicConverter) GrayScale() *ComicConverter {
|
||||
if c.img == nil {
|
||||
panic("load image first")
|
||||
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)
|
||||
c.img = newImg
|
||||
}
|
||||
|
||||
newImg := image.NewGray(c.img.Bounds())
|
||||
|
||||
draw.Draw(newImg, newImg.Bounds(), c.img, image.Point{}, draw.Src)
|
||||
|
||||
c.img = newImg
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
@ -107,14 +97,14 @@ BOTTOM:
|
||||
imgArea.Max.Y--
|
||||
}
|
||||
|
||||
newImg := image.NewRGBA(image.Rectangle{
|
||||
newImg := image.NewGray(image.Rectangle{
|
||||
Min: image.Point{0, 0},
|
||||
Max: image.Point{imgArea.Dx(), imgArea.Dy()},
|
||||
})
|
||||
|
||||
draw.Draw(newImg, newImg.Bounds(), c.img, imgArea.Min, draw.Src)
|
||||
|
||||
c.img = newImg
|
||||
c.img = c.img.SubImage(imgArea).(*image.Gray)
|
||||
|
||||
return c
|
||||
}
|
||||
@ -128,18 +118,22 @@ func (c *ComicConverter) Resize(w, h int) *ComicConverter {
|
||||
origWidth := dim.Dx()
|
||||
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 {
|
||||
width = w
|
||||
}
|
||||
if heigth > h {
|
||||
heigth = h
|
||||
if height > h {
|
||||
height = h
|
||||
}
|
||||
|
||||
newImg := image.NewRGBA(image.Rectangle{
|
||||
newImg := image.NewGray(image.Rectangle{
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user