diff --git a/internal/converter/converter.go b/internal/converter/converter.go index 83c0b07..c45d791 100644 --- a/internal/converter/converter.go +++ b/internal/converter/converter.go @@ -113,6 +113,7 @@ func (c *Converter) InitParse() { c.AddIntParam(&c.Options.CropRatioUp, "crop-ratio-up", c.Options.CropRatioUp, "Crop ratio up: ratio of pixels allow to be non blank while cutting on the top.") c.AddIntParam(&c.Options.CropRatioRight, "crop-ratio-right", c.Options.CropRatioRight, "Crop ratio right: ratio of pixels allow to be non blank while cutting on the right.") c.AddIntParam(&c.Options.CropRatioBottom, "crop-ratio-bottom", c.Options.CropRatioBottom, "Crop ratio bottom: ratio of pixels allow to be non blank while cutting on the bottom.") + c.AddIntParam(&c.Options.CropLimit, "crop-limit", c.Options.CropLimit, "Crop limit: maximum number of pixel to crop on all side. 0 mean unlimited.") c.AddIntParam(&c.Options.Brightness, "brightness", c.Options.Brightness, "Brightness readjustment: between -100 and 100, > 0 lighter, < 0 darker") c.AddIntParam(&c.Options.Contrast, "contrast", c.Options.Contrast, "Contrast readjustment: between -100 and 100, > 0 more contrast, < 0 less contrast") c.AddBoolParam(&c.Options.AutoContrast, "autocontrast", c.Options.AutoContrast, "Improve contrast automatically") diff --git a/internal/converter/options/converter_options.go b/internal/converter/options/converter_options.go index fc7daaf..9e18f17 100644 --- a/internal/converter/options/converter_options.go +++ b/internal/converter/options/converter_options.go @@ -8,8 +8,9 @@ import ( "path/filepath" "strings" - "github.com/celogeek/go-comic-converter/v2/internal/converter/profiles" "gopkg.in/yaml.v3" + + "github.com/celogeek/go-comic-converter/v2/internal/converter/profiles" ) type Options struct { @@ -29,6 +30,7 @@ type Options struct { CropRatioUp int `yaml:"crop_ratio_up"` CropRatioRight int `yaml:"crop_ratio_right"` CropRatioBottom int `yaml:"crop_ratio_bottom"` + CropLimit int `yaml:"crop_limit"` Brightness int `yaml:"brightness"` Contrast int `yaml:"contrast"` AutoContrast bool `yaml:"auto_contrast"` @@ -161,6 +163,7 @@ func (o *Options) MarshalJSON() ([]byte, error) { "up": o.CropRatioUp, "bottom": o.CropRatioBottom, } + out["crop_limit"] = o.CropLimit } if o.Brightness != 0 { out["brightness"] = o.Brightness @@ -265,7 +268,7 @@ func (o *Options) ShowConfig() string { {"Grayscale", o.Grayscale, true}, {"Grayscale mode", grayscaleMode, o.Grayscale}, {"Crop", o.Crop, true}, - {"Crop ratio", fmt.Sprintf("%d Left - %d Up - %d Right - %d Bottom", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom), o.Crop}, + {"Crop ratio", fmt.Sprintf("%d Left - %d Up - %d Right - %d Bottom - %d Limit", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom, o.CropLimit), o.Crop}, {"Brightness", o.Brightness, o.Brightness != 0}, {"Contrast", o.Contrast, o.Contrast != 0}, {"Auto contrast", o.AutoContrast, true}, diff --git a/internal/epub/imagefilters/epub_image_filters_autocrop.go b/internal/epub/imagefilters/epub_image_filters_autocrop.go index e3929fb..8d48de2 100644 --- a/internal/epub/imagefilters/epub_image_filters_autocrop.go +++ b/internal/epub/imagefilters/epub_image_filters_autocrop.go @@ -8,9 +8,9 @@ import ( ) // AutoCrop Lookup for margin and crop -func AutoCrop(img image.Image, bounds image.Rectangle, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter { +func AutoCrop(img image.Image, bounds image.Rectangle, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int, limit int) gift.Filter { return gift.Crop( - findMargin(img, bounds, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}), + findMargin(img, bounds, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}, limit), ) } @@ -25,11 +25,11 @@ type cutRatioOptions struct { Left, Up, Right, Bottom int } -func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions) image.Rectangle { +func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions, limit int) image.Rectangle { imgArea := bounds LEFT: - for x := imgArea.Min.X; x < imgArea.Max.X; x++ { + for x, maxCut := imgArea.Min.X, limit; x < imgArea.Max.X && (limit == 0 || maxCut > 0); x, maxCut = x+1, maxCut-1 { allowNonBlank := imgArea.Dy() * cutRatio.Left / 100 for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { if !colorIsBlank(img.At(x, y)) { @@ -43,7 +43,7 @@ LEFT: } UP: - for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { + for y, maxCut := imgArea.Min.Y, limit; y < imgArea.Max.Y && (limit == 0 || maxCut > 0); y, maxCut = y+1, maxCut-1 { allowNonBlank := imgArea.Dx() * cutRatio.Up / 100 for x := imgArea.Min.X; x < imgArea.Max.X; x++ { if !colorIsBlank(img.At(x, y)) { @@ -57,7 +57,7 @@ UP: } RIGHT: - for x := imgArea.Max.X - 1; x >= imgArea.Min.X; x-- { + for x, maxCut := imgArea.Max.X-1, limit; x >= imgArea.Min.X && (limit == 0 || maxCut > 0); x, maxCut = x-1, maxCut-1 { allowNonBlank := imgArea.Dy() * cutRatio.Right / 100 for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { if !colorIsBlank(img.At(x, y)) { @@ -71,7 +71,7 @@ RIGHT: } BOTTOM: - for y := imgArea.Max.Y - 1; y >= imgArea.Min.Y; y-- { + for y, maxCut := imgArea.Max.Y-1, limit; y >= imgArea.Min.Y && (limit == 0 || maxCut > 0); y, maxCut = y-1, maxCut-1 { allowNonBlank := imgArea.Dx() * cutRatio.Bottom / 100 for x := imgArea.Min.X; x < imgArea.Max.X; x++ { if !colorIsBlank(img.At(x, y)) { diff --git a/internal/epub/imageprocessor/epub_image_processor.go b/internal/epub/imageprocessor/epub_image_processor.go index cfa2385..30db08e 100644 --- a/internal/epub/imageprocessor/epub_image_processor.go +++ b/internal/epub/imageprocessor/epub_image_processor.go @@ -9,12 +9,13 @@ import ( "os" "sync" + "github.com/disintegration/gift" + epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image" epubimagefilters "github.com/celogeek/go-comic-converter/v2/internal/epub/imagefilters" epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options" epubprogress "github.com/celogeek/go-comic-converter/v2/internal/epub/progress" epubzip "github.com/celogeek/go-comic-converter/v2/internal/epub/zip" - "github.com/disintegration/gift" ) type EPUBImageProcessor struct { @@ -189,6 +190,7 @@ func (e *EPUBImageProcessor) transformImage(input *task, part int, right bool) * e.Image.Crop.Up, e.Image.Crop.Right, e.Image.Crop.Bottom, + e.Image.Crop.Limit, ) // detect if blank image diff --git a/internal/epub/options/epub_options.go b/internal/epub/options/epub_options.go index 17665dc..041ae43 100644 --- a/internal/epub/options/epub_options.go +++ b/internal/epub/options/epub_options.go @@ -6,6 +6,7 @@ import "fmt" type Crop struct { Enabled bool Left, Up, Right, Bottom int + Limit int } type Color struct { diff --git a/main.go b/main.go index 7ae381d..8be2cd6 100644 --- a/main.go +++ b/main.go @@ -13,10 +13,11 @@ import ( "os" "runtime/debug" + "github.com/tcnksm/go-latest" + "github.com/celogeek/go-comic-converter/v2/internal/converter" "github.com/celogeek/go-comic-converter/v2/internal/epub" epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options" - "github.com/tcnksm/go-latest" ) func main() { @@ -122,18 +123,34 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s Quiet: cmd.Options.Quiet, Json: cmd.Options.Json, Image: &epuboptions.Image{ - Crop: &epuboptions.Crop{Enabled: cmd.Options.Crop, Left: cmd.Options.CropRatioLeft, Up: cmd.Options.CropRatioUp, Right: cmd.Options.CropRatioRight, Bottom: cmd.Options.CropRatioBottom}, - Quality: cmd.Options.Quality, - Brightness: cmd.Options.Brightness, - Contrast: cmd.Options.Contrast, - AutoContrast: cmd.Options.AutoContrast, - AutoRotate: cmd.Options.AutoRotate, - AutoSplitDoublePage: cmd.Options.AutoSplitDoublePage, - KeepDoublePageIfSplit: cmd.Options.KeepDoublePageIfSplit, - NoBlankImage: cmd.Options.NoBlankImage, - Manga: cmd.Options.Manga, - HasCover: cmd.Options.HasCover, - View: &epuboptions.View{Width: profile.Width, Height: profile.Height, AspectRatio: cmd.Options.AspectRatio, PortraitOnly: cmd.Options.PortraitOnly, Color: epuboptions.Color{Foreground: cmd.Options.ForegroundColor, Background: cmd.Options.BackgroundColor}}, + Crop: &epuboptions.Crop{ + Enabled: cmd.Options.Crop, + Left: cmd.Options.CropRatioLeft, + Up: cmd.Options.CropRatioUp, + Right: cmd.Options.CropRatioRight, + Bottom: cmd.Options.CropRatioBottom, + Limit: cmd.Options.CropLimit, + }, + Quality: cmd.Options.Quality, + Brightness: cmd.Options.Brightness, + Contrast: cmd.Options.Contrast, + AutoContrast: cmd.Options.AutoContrast, + AutoRotate: cmd.Options.AutoRotate, + AutoSplitDoublePage: cmd.Options.AutoSplitDoublePage, + KeepDoublePageIfSplit: cmd.Options.KeepDoublePageIfSplit, + NoBlankImage: cmd.Options.NoBlankImage, + Manga: cmd.Options.Manga, + HasCover: cmd.Options.HasCover, + View: &epuboptions.View{ + Width: profile.Width, + Height: profile.Height, + AspectRatio: cmd.Options.AspectRatio, + PortraitOnly: cmd.Options.PortraitOnly, + Color: epuboptions.Color{ + Foreground: cmd.Options.ForegroundColor, + Background: cmd.Options.BackgroundColor, + }, + }, GrayScale: cmd.Options.Grayscale, GrayScaleMode: cmd.Options.GrayscaleMode, Resize: !cmd.Options.NoResize,