Compare commits

..

No commits in common. "0dde6e02a48123a194b4d8a84ad17a9a63bc7045" and "c75c58dede4ee90a41be818159a0af644e1017c5" have entirely different histories.

6 changed files with 32 additions and 59 deletions

View File

@ -113,8 +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 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.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")
@ -390,11 +389,6 @@ func (c *Converter) Validate() error {
return errors.New("grayscale mode should be 0, 1 or 2")
}
// crop
if c.Options.CropLimit < 0 || c.Options.CropLimit > 100 {
return errors.New("crop limit should be between 0 and 100")
}
return nil
}

View File

@ -31,7 +31,6 @@ 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"`
@ -82,25 +81,23 @@ 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,
CropLimit: 10,
CropSkipIfLimitReached: true,
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,
NoBlankImage: true,
HasCover: true,
KeepDoublePageIfSplit: true,
SortPathMode: 1,
ForegroundColor: "000",
BackgroundColor: "FFF",
Format: "jpeg",
TitlePage: 1,
profiles: profiles.New(),
}
}
@ -167,7 +164,6 @@ 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
@ -272,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 - Limit %d%% - Skip %v", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom, o.CropLimit, o.CropSkipIfLimitReached), 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},

View File

@ -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, skipIfLimitReached bool) 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}, limit, skipIfLimitReached),
findMargin(img, bounds, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}, limit),
)
}
@ -25,13 +25,11 @@ type cutRatioOptions struct {
Left, Up, Right, Bottom int
}
func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions, limit int, skipIfLimitReached bool) image.Rectangle {
func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions, limit int) image.Rectangle {
imgArea := bounds
maxCropX, maxCropY := imgArea.Dx()*limit/100, imgArea.Dy()*limit/100
LEFT:
for x, maxCrop := imgArea.Min.X, maxCropX; x < imgArea.Max.X && (limit == 0 || maxCrop > 0); x, maxCrop = x+1, maxCrop-1 {
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)) {
@ -42,13 +40,10 @@ LEFT:
}
}
imgArea.Min.X++
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
return bounds
}
}
UP:
for y, maxCrop := imgArea.Min.Y, maxCropY; y < imgArea.Max.Y && (limit == 0 || maxCrop > 0); y, maxCrop = y+1, maxCrop-1 {
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)) {
@ -59,13 +54,10 @@ UP:
}
}
imgArea.Min.Y++
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
return bounds
}
}
RIGHT:
for x, maxCrop := imgArea.Max.X-1, maxCropX; x >= imgArea.Min.X && (limit == 0 || maxCrop > 0); x, maxCrop = x-1, maxCrop-1 {
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)) {
@ -76,13 +68,10 @@ RIGHT:
}
}
imgArea.Max.X--
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
return bounds
}
}
BOTTOM:
for y, maxCrop := imgArea.Max.Y-1, maxCropY; y >= imgArea.Min.Y && (limit == 0 || maxCrop > 0); y, maxCrop = y-1, maxCrop-1 {
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)) {
@ -93,9 +82,6 @@ BOTTOM:
}
}
imgArea.Max.Y--
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
return bounds
}
}
return imgArea

View File

@ -191,7 +191,6 @@ 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

View File

@ -7,7 +7,6 @@ type Crop struct {
Enabled bool
Left, Up, Right, Bottom int
Limit int
SkipIfLimitReached bool
}
type Color struct {

13
main.go
View File

@ -124,13 +124,12 @@ $ 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,
SkipIfLimitReached: cmd.Options.CropSkipIfLimitReached,
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,