mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
move auto crop to filters
This commit is contained in:
parent
4dc1f7275f
commit
2b7582e776
@ -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
|
@ -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)
|
||||
}
|
||||
}
|
@ -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(),
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user