From 8475faa6482291d13e59573c93e9f35fb9a08ded Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 1 Jan 2023 18:45:54 +0100 Subject: [PATCH] add brightness and contrast --- README.md | 6 ++-- internal/epub/core.go | 2 ++ internal/epub/image_processing.go | 4 +-- main.go | 54 ++++++++++++++++++++++--------- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a38f10b..e3b21be 100644 --- a/README.md +++ b/README.md @@ -76,10 +76,12 @@ The ePub include as a first page: # go-comic-converter -h Usage of go-comic-converter: - -algo string - Algo for RGB to Grayscale: default, mean, luma, luster (default "default") -author string Author of the epub (default "GO Comic Converter") + -brightness int + Brightness readjustement: between -100 and 100, > 0 lighter, < 0 darker + -contrast int + Contrast readjustement: between -100 and 100, > 0 more contrast, < 0 less contrast -input string Source of comic to convert: directory, cbz, zip, cbr, rar, pdf -limitmb int diff --git a/internal/epub/core.go b/internal/epub/core.go index 6c3e392..1c6cef0 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -18,6 +18,8 @@ type ImageOptions struct { Quality int Algo string Palette color.Palette + Brightness int + Contrast int } type EpubOptions struct { diff --git a/internal/epub/image_processing.go b/internal/epub/image_processing.go index bebbe23..01c36ae 100644 --- a/internal/epub/image_processing.go +++ b/internal/epub/image_processing.go @@ -139,9 +139,9 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) { // prepare filter g := gift.New( gift.Crop(findMarging(src)), + gift.Contrast(float32(options.Contrast)), + gift.Brightness(float32(options.Brightness)), gift.ResizeToFit(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), - // gift.Gamma(1.8), - // gift.Sigmoid(0.5, 5), ) g.SetParallelization(false) diff --git a/main.go b/main.go index 6f025a1..315fd0b 100644 --- a/main.go +++ b/main.go @@ -56,14 +56,16 @@ func init() { } type Option struct { - Input string - Output string - Profile string - Author string - Title string - Quality int - NoCrop bool - LimitMb int + Input string + Output string + Profile string + Author string + Title string + Quality int + NoCrop bool + Brightness int + Contrast int + LimitMb int } func (o *Option) String() string { @@ -84,14 +86,16 @@ func (o *Option) String() string { return fmt.Sprintf(`Go Comic Converter Options: - Input : %s - Output : %s - Profile : %s - %s - %dx%d - %d levels of gray - Author : %s - Title : %s - Quality : %d - Crop : %v - LimitMb : %s + Input : %s + Output : %s + Profile : %s - %s - %dx%d - %d levels of gray + Author : %s + Title : %s + Quality : %d + Crop : %v + Brightness: %d + Contrast : %d + LimitMb : %s `, o.Input, o.Output, @@ -100,6 +104,8 @@ Options: o.Title, o.Quality, !o.NoCrop, + o.Brightness, + o.Contrast, limitmb, ) } @@ -124,6 +130,8 @@ func main() { flag.StringVar(&opt.Title, "title", "", "Title of the epub") flag.IntVar(&opt.Quality, "quality", 85, "Quality of the image") flag.BoolVar(&opt.NoCrop, "nocrop", false, "Disable cropping") + flag.IntVar(&opt.Brightness, "brightness", 0, "Brightness readjustement: between -100 and 100, > 0 lighter, < 0 darker") + flag.IntVar(&opt.Contrast, "contrast", 0, "Contrast readjustement: between -100 and 100, > 0 more contrast, < 0 less contrast") flag.IntVar(&opt.LimitMb, "limitmb", 0, "Limit size of the ePub: Default nolimit (0), Minimum 20") flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0])) @@ -188,6 +196,18 @@ func main() { os.Exit(1) } + if opt.Brightness < -100 || opt.Brightness > 100 { + fmt.Fprintln(os.Stderr, "Brightness should be between -100 and 100") + flag.Usage() + os.Exit(1) + } + + if opt.Contrast < -100 || opt.Contrast > 100 { + fmt.Fprintln(os.Stderr, "Contrast should be between -100 and 100") + flag.Usage() + os.Exit(1) + } + if opt.Title == "" { ext := filepath.Ext(defaultOutput) opt.Title = filepath.Base(defaultOutput[0 : len(defaultOutput)-len(ext)]) @@ -207,6 +227,8 @@ func main() { Quality: opt.Quality, Crop: !opt.NoCrop, Palette: profile.Palette, + Brightness: opt.Brightness, + Contrast: opt.Contrast, }, }).Write(); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err)