mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 00:02:37 +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 (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"github.com/disintegration/gift"
|
||||||
)
|
)
|
||||||
|
|
||||||
// only accept jpg, png and webp as source file
|
func AutoCrop(img image.Image, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter {
|
||||||
func isSupportedImage(path string) bool {
|
return gift.Crop(
|
||||||
switch strings.ToLower(filepath.Ext(path)) {
|
findMarging(img, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}),
|
||||||
case ".jpg", ".jpeg", ".png", ".webp":
|
)
|
||||||
{
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the color is blank enough
|
// 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
|
package epubimage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"image"
|
||||||
|
|
||||||
epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
|
epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
|
||||||
"github.com/disintegration/gift"
|
"github.com/disintegration/gift"
|
||||||
)
|
)
|
||||||
|
|
||||||
// create filter to apply to the source
|
// 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 := gift.New()
|
||||||
g.SetParallelization(false)
|
g.SetParallelization(false)
|
||||||
|
|
||||||
if options.AutoRotate {
|
if options.Crop {
|
||||||
g.Add(epubfilters.AutoRotate())
|
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 {
|
if options.Contrast != 0 {
|
||||||
g.Add(gift.Contrast(float32(options.Contrast)))
|
g.Add(gift.Contrast(float32(options.Contrast)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.Brightness != 0 {
|
if options.Brightness != 0 {
|
||||||
g.Add(gift.Brightness(float32(options.Brightness)))
|
g.Add(gift.Brightness(float32(options.Brightness)))
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Add(
|
g.Add(
|
||||||
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||||
epubfilters.Pixel(),
|
epubfilters.Pixel(),
|
||||||
|
@ -29,6 +29,17 @@ type tasks struct {
|
|||||||
Name string
|
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
|
// extract and convert images
|
||||||
func LoadImages(o *Options) ([]*epubimage.Image, error) {
|
func LoadImages(o *Options) ([]*epubimage.Image, error) {
|
||||||
images := make([]*epubimage.Image, 0)
|
images := make([]*epubimage.Image, 0)
|
||||||
@ -101,20 +112,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if o.Image.Crop {
|
g := epubimage.NewGift(src, o.Image)
|
||||||
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)
|
|
||||||
|
|
||||||
// Convert image
|
// Convert image
|
||||||
dst := image.NewGray(g.Bounds(src.Bounds()))
|
dst := image.NewGray(g.Bounds(src.Bounds()))
|
||||||
g.Draw(dst, src)
|
g.Draw(dst, src)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user