mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 08:12:36 +02:00
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:
parent
a54e093844
commit
2650b926f8
@ -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.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.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.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.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.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.")
|
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")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@ type Options struct {
|
|||||||
Profile string `yaml:"profile"`
|
Profile string `yaml:"profile"`
|
||||||
Quality int `yaml:"quality"`
|
Quality int `yaml:"quality"`
|
||||||
Grayscale bool `yaml:"grayscale"`
|
Grayscale bool `yaml:"grayscale"`
|
||||||
|
GrayscaleMode int `yaml:"grayscale_mode"` // 0 = normal, 1 = average, 2 = luminance
|
||||||
Crop bool `yaml:"crop"`
|
Crop bool `yaml:"crop"`
|
||||||
CropRatioLeft int `yaml:"crop_ratio_left"`
|
CropRatioLeft int `yaml:"crop_ratio_left"`
|
||||||
CropRatioUp int `yaml:"crop_ratio_up"`
|
CropRatioUp int `yaml:"crop_ratio_up"`
|
||||||
@ -179,6 +180,14 @@ func (o *Options) ShowConfig() string {
|
|||||||
titlePage = "when epub is splitted"
|
titlePage = "when epub is splitted"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
grayscaleMode := "normal"
|
||||||
|
switch o.GrayscaleMode {
|
||||||
|
case 1:
|
||||||
|
grayscaleMode = "average"
|
||||||
|
case 2:
|
||||||
|
grayscaleMode = "luminance"
|
||||||
|
}
|
||||||
|
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
for _, v := range []struct {
|
for _, v := range []struct {
|
||||||
Key string
|
Key string
|
||||||
@ -189,6 +198,7 @@ func (o *Options) ShowConfig() string {
|
|||||||
{"Format", o.Format, true},
|
{"Format", o.Format, true},
|
||||||
{"Quality", o.Quality, o.Format == "jpeg"},
|
{"Quality", o.Quality, o.Format == "jpeg"},
|
||||||
{"Grayscale", o.Grayscale, true},
|
{"Grayscale", o.Grayscale, true},
|
||||||
|
{"Grayscale Mode", grayscaleMode, o.Grayscale},
|
||||||
{"Crop", o.Crop, true},
|
{"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},
|
{"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},
|
{"Brightness", o.Brightness, o.Brightness != 0},
|
||||||
|
@ -215,8 +215,23 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image.
|
|||||||
}
|
}
|
||||||
|
|
||||||
if e.Image.GrayScale {
|
if e.Image.GrayScale {
|
||||||
filters = append(filters, gift.Grayscale())
|
var f gift.Filter
|
||||||
splitFilters = append(splitFilters, gift.Grayscale())
|
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())
|
filters = append(filters, epubimagefilters.Pixel())
|
||||||
|
@ -33,6 +33,7 @@ type Image struct {
|
|||||||
HasCover bool
|
HasCover bool
|
||||||
View *View
|
View *View
|
||||||
GrayScale bool
|
GrayScale bool
|
||||||
|
GrayScaleMode int
|
||||||
Resize bool
|
Resize bool
|
||||||
Format string
|
Format string
|
||||||
}
|
}
|
||||||
|
1
main.go
1
main.go
@ -116,6 +116,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
|
|||||||
Image: &epuboptions.Image{
|
Image: &epuboptions.Image{
|
||||||
Quality: cmd.Options.Quality,
|
Quality: cmd.Options.Quality,
|
||||||
GrayScale: cmd.Options.Grayscale,
|
GrayScale: cmd.Options.Grayscale,
|
||||||
|
GrayScaleMode: cmd.Options.GrayscaleMode,
|
||||||
Crop: &epuboptions.Crop{
|
Crop: &epuboptions.Crop{
|
||||||
Enabled: cmd.Options.Crop,
|
Enabled: cmd.Options.Crop,
|
||||||
Left: cmd.Options.CropRatioLeft,
|
Left: cmd.Options.CropRatioLeft,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user