From 0dde6e02a48123a194b4d8a84ad17a9a63bc7045 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 28 Apr 2024 19:28:35 +0200 Subject: [PATCH] add skip crop if limit reached --- internal/converter/converter.go | 1 + .../converter/options/converter_options.go | 40 ++++++++++--------- .../epub_image_filters_autocrop.go | 28 +++++++++---- .../imageprocessor/epub_image_processor.go | 1 + internal/epub/options/epub_options.go | 1 + main.go | 13 +++--- 6 files changed, 52 insertions(+), 32 deletions(-) diff --git a/internal/converter/converter.go b/internal/converter/converter.go index 39f6730..69db9e0 100644 --- a/internal/converter/converter.go +++ b/internal/converter/converter.go @@ -114,6 +114,7 @@ func (c *Converter) InitParse() { 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 cropping in percentage allowed. 0 mean unlimited.") + c.AddBoolParam(&c.Options.CropSkipIfLimitReached, "crop-skip-if-limit-reached", c.Options.CropSkipIfLimitReached, "Crop skip if limit reached.") 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 432033b..cfd89ec 100644 --- a/internal/converter/options/converter_options.go +++ b/internal/converter/options/converter_options.go @@ -31,6 +31,7 @@ type Options struct { CropRatioRight int `yaml:"crop_ratio_right"` CropRatioBottom int `yaml:"crop_ratio_bottom"` CropLimit int `yaml:"crop_limit"` + CropSkipIfLimitReached bool `yaml:"crop_skip_if_limit_reached"` Brightness int `yaml:"brightness"` Contrast int `yaml:"contrast"` AutoContrast bool `yaml:"auto_contrast"` @@ -81,23 +82,25 @@ type Options struct { // New Initialize default options. func New() *Options { return &Options{ - Profile: "SR", - Quality: 85, - Grayscale: true, - Crop: true, - CropRatioLeft: 1, - CropRatioUp: 1, - CropRatioRight: 1, - CropRatioBottom: 3, - NoBlankImage: true, - HasCover: true, - KeepDoublePageIfSplit: true, - SortPathMode: 1, - ForegroundColor: "000", - BackgroundColor: "FFF", - Format: "jpeg", - TitlePage: 1, - profiles: profiles.New(), + Profile: "SR", + Quality: 85, + Grayscale: true, + Crop: true, + CropRatioLeft: 1, + CropRatioUp: 1, + CropRatioRight: 1, + CropRatioBottom: 3, + CropLimit: 10, + CropSkipIfLimitReached: true, + NoBlankImage: true, + HasCover: true, + KeepDoublePageIfSplit: true, + SortPathMode: 1, + ForegroundColor: "000", + BackgroundColor: "FFF", + Format: "jpeg", + TitlePage: 1, + profiles: profiles.New(), } } @@ -164,6 +167,7 @@ func (o *Options) MarshalJSON() ([]byte, error) { "bottom": o.CropRatioBottom, } out["crop_limit"] = o.CropLimit + out["crop_skip_if_limit_reached"] = o.CropSkipIfLimitReached } if o.Brightness != 0 { out["brightness"] = o.Brightness @@ -268,7 +272,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 - Limit %d%%", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom, o.CropLimit), o.Crop}, + {"Crop ratio", fmt.Sprintf("%d Left - %d Up - %d Right - %d Bottom - Limit %d%% - Skip %v", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom, o.CropLimit, o.CropSkipIfLimitReached), 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 53abf0a..aa51b85 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, limit int) gift.Filter { +func AutoCrop(img image.Image, bounds image.Rectangle, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int, limit int, skipIfLimitReached bool) gift.Filter { return gift.Crop( - findMargin(img, bounds, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}, limit), + findMargin(img, bounds, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}, limit, skipIfLimitReached), ) } @@ -25,13 +25,13 @@ type cutRatioOptions struct { Left, Up, Right, Bottom int } -func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions, limit int) image.Rectangle { +func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions, limit int, skipIfLimitReached bool) image.Rectangle { imgArea := bounds - maxCutX, maxCutY := imgArea.Dx()*limit/100, imgArea.Dy()*limit/100 + maxCropX, maxCropY := imgArea.Dx()*limit/100, imgArea.Dy()*limit/100 LEFT: - for x, maxCut := imgArea.Min.X, maxCutX; x < imgArea.Max.X && (maxCutX == 0 || maxCut > 0); x, maxCut = x+1, maxCut-1 { + for x, maxCrop := imgArea.Min.X, maxCropX; x < imgArea.Max.X && (limit == 0 || maxCrop > 0); x, maxCrop = x+1, maxCrop-1 { allowNonBlank := imgArea.Dy() * cutRatio.Left / 100 for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { if !colorIsBlank(img.At(x, y)) { @@ -42,10 +42,13 @@ LEFT: } } imgArea.Min.X++ + if limit > 0 && maxCrop == 1 && skipIfLimitReached { + return bounds + } } UP: - for y, maxCut := imgArea.Min.Y, maxCutY; y < imgArea.Max.Y && (maxCutY == 0 || maxCut > 0); y, maxCut = y+1, maxCut-1 { + for y, maxCrop := imgArea.Min.Y, maxCropY; y < imgArea.Max.Y && (limit == 0 || maxCrop > 0); y, maxCrop = y+1, maxCrop-1 { allowNonBlank := imgArea.Dx() * cutRatio.Up / 100 for x := imgArea.Min.X; x < imgArea.Max.X; x++ { if !colorIsBlank(img.At(x, y)) { @@ -56,10 +59,13 @@ UP: } } imgArea.Min.Y++ + if limit > 0 && maxCrop == 1 && skipIfLimitReached { + return bounds + } } RIGHT: - for x, maxCut := imgArea.Max.X-1, maxCutX; x >= imgArea.Min.X && (maxCutX == 0 || maxCut > 0); x, maxCut = x-1, maxCut-1 { + for x, maxCrop := imgArea.Max.X-1, maxCropX; x >= imgArea.Min.X && (limit == 0 || maxCrop > 0); x, maxCrop = x-1, maxCrop-1 { allowNonBlank := imgArea.Dy() * cutRatio.Right / 100 for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { if !colorIsBlank(img.At(x, y)) { @@ -70,10 +76,13 @@ RIGHT: } } imgArea.Max.X-- + if limit > 0 && maxCrop == 1 && skipIfLimitReached { + return bounds + } } BOTTOM: - for y, maxCut := imgArea.Max.Y-1, maxCutY; y >= imgArea.Min.Y && (maxCutY == 0 || maxCut > 0); y, maxCut = y-1, maxCut-1 { + for y, maxCrop := imgArea.Max.Y-1, maxCropY; y >= imgArea.Min.Y && (limit == 0 || maxCrop > 0); y, maxCrop = y-1, maxCrop-1 { allowNonBlank := imgArea.Dx() * cutRatio.Bottom / 100 for x := imgArea.Min.X; x < imgArea.Max.X; x++ { if !colorIsBlank(img.At(x, y)) { @@ -84,6 +93,9 @@ BOTTOM: } } imgArea.Max.Y-- + if limit > 0 && maxCrop == 1 && skipIfLimitReached { + return bounds + } } return imgArea diff --git a/internal/epub/imageprocessor/epub_image_processor.go b/internal/epub/imageprocessor/epub_image_processor.go index 30db08e..a152c2b 100644 --- a/internal/epub/imageprocessor/epub_image_processor.go +++ b/internal/epub/imageprocessor/epub_image_processor.go @@ -191,6 +191,7 @@ func (e *EPUBImageProcessor) transformImage(input *task, part int, right bool) * e.Image.Crop.Right, e.Image.Crop.Bottom, e.Image.Crop.Limit, + e.Image.Crop.SkipIfLimitReached, ) // detect if blank image diff --git a/internal/epub/options/epub_options.go b/internal/epub/options/epub_options.go index 041ae43..6715ee8 100644 --- a/internal/epub/options/epub_options.go +++ b/internal/epub/options/epub_options.go @@ -7,6 +7,7 @@ type Crop struct { Enabled bool Left, Up, Right, Bottom int Limit int + SkipIfLimitReached bool } type Color struct { diff --git a/main.go b/main.go index 8be2cd6..ea6cab9 100644 --- a/main.go +++ b/main.go @@ -124,12 +124,13 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s 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, - Limit: cmd.Options.CropLimit, + Enabled: cmd.Options.Crop, + Left: cmd.Options.CropRatioLeft, + Up: cmd.Options.CropRatioUp, + Right: cmd.Options.CropRatioRight, + Bottom: cmd.Options.CropRatioBottom, + Limit: cmd.Options.CropLimit, + SkipIfLimitReached: cmd.Options.CropSkipIfLimitReached, }, Quality: cmd.Options.Quality, Brightness: cmd.Options.Brightness,