diff --git a/internal/converter/converter.go b/internal/converter/converter.go index 73d592e..a95e974 100644 --- a/internal/converter/converter.go +++ b/internal/converter/converter.go @@ -108,6 +108,7 @@ func (c *Converter) InitParse() { c.AddStringParam(&c.Options.Profile, "profile", c.Options.Profile, fmt.Sprintf("Profile to use: \n%s", c.Options.AvailableProfiles())) c.AddIntParam(&c.Options.Quality, "quality", c.Options.Quality, "Quality of the image") c.AddBoolParam(&c.Options.Grayscale, "grayscale", c.Options.Grayscale, "Grayscale image. Ideal for eInk devices.") + c.AddIntParam(&c.Options.GrayscaleMode, "grayscale-mode", c.Options.GrayscaleMode, "Grayscale Mode\n0 = normal\n1 = average\n2 = luminance") c.AddBoolParam(&c.Options.Crop, "crop", c.Options.Crop, "Crop images") c.AddIntParam(&c.Options.CropRatioLeft, "crop-ratio-left", c.Options.CropRatioLeft, "Crop ratio left: ratio of pixels allow to be non blank while cutting on the left.") 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.") @@ -370,6 +371,11 @@ func (c *Converter) Validate() error { return errors.New("title page should be 0, 1 or 2") } + // Grayscale Mode + if c.Options.GrayscaleMode < 0 || c.Options.GrayscaleMode > 2 { + return errors.New("grayscale mode should be 0, 1 or 2") + } + return nil } diff --git a/internal/converter/options/converter_options.go b/internal/converter/options/converter_options.go index 6362663..e9bcf9d 100644 --- a/internal/converter/options/converter_options.go +++ b/internal/converter/options/converter_options.go @@ -24,6 +24,7 @@ type Options struct { Profile string `yaml:"profile"` Quality int `yaml:"quality"` Grayscale bool `yaml:"grayscale"` + GrayscaleMode int `yaml:"grayscale_mode"` // 0 = normal, 1 = average, 2 = luminance Crop bool `yaml:"crop"` CropRatioLeft int `yaml:"crop_ratio_left"` CropRatioUp int `yaml:"crop_ratio_up"` @@ -179,6 +180,14 @@ func (o *Options) ShowConfig() string { titlePage = "when epub is splitted" } + grayscaleMode := "normal" + switch o.GrayscaleMode { + case 1: + grayscaleMode = "average" + case 2: + grayscaleMode = "luminance" + } + var b strings.Builder for _, v := range []struct { Key string @@ -189,6 +198,7 @@ func (o *Options) ShowConfig() string { {"Format", o.Format, true}, {"Quality", o.Quality, o.Format == "jpeg"}, {"Grayscale", o.Grayscale, true}, + {"Grayscale Mode", grayscaleMode, o.Grayscale}, {"Crop", o.Crop, true}, {"CropRatio", fmt.Sprintf("%d Left - %d Up - %d Right - %d Bottom", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom), o.Crop}, {"Brightness", o.Brightness, o.Brightness != 0}, diff --git a/internal/epub/imageprocessor/epub_image_processor.go b/internal/epub/imageprocessor/epub_image_processor.go index a895aa7..8836490 100644 --- a/internal/epub/imageprocessor/epub_image_processor.go +++ b/internal/epub/imageprocessor/epub_image_processor.go @@ -215,8 +215,23 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image. } if e.Image.GrayScale { - filters = append(filters, gift.Grayscale()) - splitFilters = append(splitFilters, gift.Grayscale()) + var f gift.Filter + switch e.Image.GrayScaleMode { + case 1: // average + f = gift.ColorFunc(func(r0, g0, b0, a0 float32) (r float32, g float32, b float32, a float32) { + y := (r0 + g0 + b0) / 3 + return y, y, y, a0 + }) + case 2: // luminance + f = gift.ColorFunc(func(r0, g0, b0, a0 float32) (r float32, g float32, b float32, a float32) { + y := 0.2126*r0 + 0.7152*g0 + 0.0722*b0 + return y, y, y, a0 + }) + default: + f = gift.Grayscale() + } + filters = append(filters, f) + splitFilters = append(splitFilters, f) } filters = append(filters, epubimagefilters.Pixel()) diff --git a/internal/epub/options/epub_options.go b/internal/epub/options/epub_options.go index 83dedec..1cc3515 100644 --- a/internal/epub/options/epub_options.go +++ b/internal/epub/options/epub_options.go @@ -33,6 +33,7 @@ type Image struct { HasCover bool View *View GrayScale bool + GrayScaleMode int Resize bool Format string } diff --git a/main.go b/main.go index b8b72a1..abfeafc 100644 --- a/main.go +++ b/main.go @@ -114,8 +114,9 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s DryVerbose: cmd.Options.DryVerbose, Quiet: cmd.Options.Quiet, Image: &epuboptions.Image{ - Quality: cmd.Options.Quality, - GrayScale: cmd.Options.Grayscale, + Quality: cmd.Options.Quality, + GrayScale: cmd.Options.Grayscale, + GrayScaleMode: cmd.Options.GrayscaleMode, Crop: &epuboptions.Crop{ Enabled: cmd.Options.Crop, Left: cmd.Options.CropRatioLeft,