add grayscale mode

it will apply different formula to convert image into grayscale.

it works only if the source is not already in grayscale.

0 = normal
1 = average
2 = luminance
This commit is contained in:
Celogeek 2023-05-05 12:24:12 +02:00
parent a54e093844
commit 2650b926f8
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
5 changed files with 37 additions and 4 deletions

View File

@ -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
}

View File

@ -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},

View File

@ -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())

View File

@ -33,6 +33,7 @@ type Image struct {
HasCover bool
View *View
GrayScale bool
GrayScaleMode int
Resize bool
Format string
}

View File

@ -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,