move auto crop to filters

This commit is contained in:
Celogeek 2023-04-26 20:56:02 +02:00
parent 4dc1f7275f
commit 2b7582e776
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
5 changed files with 36 additions and 64 deletions

View File

@ -1,21 +1,16 @@
package epubimageprocessing
package epubfilters
import (
"image"
"image/color"
"path/filepath"
"strings"
"github.com/disintegration/gift"
)
// only accept jpg, png and webp as source file
func isSupportedImage(path string) bool {
switch strings.ToLower(filepath.Ext(path)) {
case ".jpg", ".jpeg", ".png", ".webp":
{
return true
}
}
return false
func AutoCrop(img image.Image, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter {
return gift.Crop(
findMarging(img, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}),
)
}
// check if the color is blank enough

View File

@ -1,35 +0,0 @@
/*
Rotate image if the source width > height.
*/
package epubfilters
import (
"image"
"image/draw"
"github.com/disintegration/gift"
)
func AutoRotate() gift.Filter {
return &autoRotateFilter{}
}
type autoRotateFilter struct {
}
func (p *autoRotateFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
if srcBounds.Dx() > srcBounds.Dy() {
dstBounds = gift.Rotate90().Bounds(srcBounds)
} else {
dstBounds = srcBounds
}
return
}
func (p *autoRotateFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
if src.Bounds().Dx() > src.Bounds().Dy() {
gift.Rotate90().Draw(dst, src, options)
} else {
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src)
}
}

View File

@ -1,24 +1,38 @@
package epubimage
import (
"image"
epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
"github.com/disintegration/gift"
)
// create filter to apply to the source
func NewGift(options *Options) *gift.GIFT {
func NewGift(img image.Image, options *Options) *gift.GIFT {
g := gift.New()
g.SetParallelization(false)
if options.AutoRotate {
g.Add(epubfilters.AutoRotate())
if options.Crop {
g.Add(epubfilters.AutoCrop(
img,
options.CropRatioLeft,
options.CropRatioUp,
options.CropRatioRight,
options.CropRatioBottom,
))
}
if options.AutoRotate && img.Bounds().Dx() > img.Bounds().Dy() {
g.Add(gift.Rotate90())
}
if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast)))
}
if options.Brightness != 0 {
g.Add(gift.Brightness(float32(options.Brightness)))
}
g.Add(
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
epubfilters.Pixel(),

View File

@ -29,6 +29,17 @@ type tasks struct {
Name string
}
// only accept jpg, png and webp as source file
func isSupportedImage(path string) bool {
switch strings.ToLower(filepath.Ext(path)) {
case ".jpg", ".jpeg", ".png", ".webp":
{
return true
}
}
return false
}
// extract and convert images
func LoadImages(o *Options) ([]*epubimage.Image, error) {
images := make([]*epubimage.Image, 0)
@ -101,20 +112,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
os.Exit(1)
}
if o.Image.Crop {
g := gift.New(gift.Crop(findMarging(src, cutRatioOptions{
Left: o.Image.CropRatioLeft,
Up: o.Image.CropRatioUp,
Right: o.Image.CropRatioRight,
Bottom: o.Image.CropRatioBottom,
})))
newSrc := image.NewNRGBA(g.Bounds(src.Bounds()))
g.Draw(newSrc, src)
src = newSrc
}
g := epubimage.NewGift(o.Image)
g := epubimage.NewGift(src, o.Image)
// Convert image
dst := image.NewGray(g.Bounds(src.Bounds()))
g.Draw(dst, src)