From 8b04cbc38f219533779843f2703d8a85ff9947bb Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 30 Apr 2023 18:20:54 +0200 Subject: [PATCH] support grayscale or color mode grayscale is on by default as the conversion is mainly useful for eInk device. --- internal/converter/converter.go | 1 + .../converter/options/converter_options.go | 3 ++ .../imageprocessor/epub_image_processor.go | 42 ++++++++++++++++--- internal/epub/options/epub_options.go | 1 + main.go | 3 +- 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/internal/converter/converter.go b/internal/converter/converter.go index 7ba6e1e..8d1e026 100644 --- a/internal/converter/converter.go +++ b/internal/converter/converter.go @@ -101,6 +101,7 @@ func (c *Converter) InitParse() { c.AddSection("Config") 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.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.") diff --git a/internal/converter/options/converter_options.go b/internal/converter/options/converter_options.go index d121d96..7461f5c 100644 --- a/internal/converter/options/converter_options.go +++ b/internal/converter/options/converter_options.go @@ -23,6 +23,7 @@ type Options struct { // Config Profile string `yaml:"profile"` Quality int `yaml:"quality"` + Grayscale bool `yaml:"grayscale"` Crop bool `yaml:"crop"` CropRatioLeft int `yaml:"crop_ratio_left"` CropRatioUp int `yaml:"crop_ratio_up"` @@ -64,6 +65,7 @@ func New() *Options { return &Options{ Profile: "", Quality: 85, + Grayscale: true, Crop: true, CropRatioLeft: 1, CropRatioUp: 1, @@ -174,6 +176,7 @@ func (o *Options) ShowConfig() string { {"ViewRatio", fmt.Sprintf("1:%s", strings.TrimRight(fmt.Sprintf("%f", profiles.PerfectRatio), "0"))}, {"View", viewDesc}, {"Quality", o.Quality}, + {"Grayscale", o.Grayscale}, {"Crop", o.Crop}, {"CropRatio", fmt.Sprintf("%d Left - %d Up - %d Right - %d Bottom", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom)}, {"Brightness", o.Brightness}, diff --git a/internal/epub/imageprocessor/epub_image_processor.go b/internal/epub/imageprocessor/epub_image_processor.go index f874fde..44a794e 100644 --- a/internal/epub/imageprocessor/epub_image_processor.go +++ b/internal/epub/imageprocessor/epub_image_processor.go @@ -6,6 +6,7 @@ package epubimageprocessor import ( "fmt" "image" + "image/draw" "os" "sync" @@ -126,6 +127,37 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) { return images, nil } +func (e *EPUBImageProcessor) createImage(src image.Image, r image.Rectangle) draw.Image { + if e.Options.Image.GrayScale { + return image.NewGray(r) + } + + switch t := src.(type) { + case *image.Gray: + return image.NewGray(r) + case *image.Gray16: + return image.NewGray16(r) + case *image.RGBA: + return image.NewRGBA(r) + case *image.RGBA64: + return image.NewRGBA64(r) + case *image.NRGBA: + return image.NewNRGBA(r) + case *image.NRGBA64: + return image.NewNRGBA64(r) + case *image.Alpha: + return image.NewAlpha(r) + case *image.Alpha16: + return image.NewAlpha16(r) + case *image.CMYK: + return image.NewCMYK(r) + case *image.Paletted: + return image.NewPaletted(r, t.Palette) + default: + return image.NewNRGBA64(r) + } +} + // transform image into 1 or 3 images // only doublepage with autosplit has 3 versions func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image.Image { @@ -177,7 +209,7 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image. // convert { g := gift.New(filters...) - dst := image.NewGray(g.Bounds(src.Bounds())) + dst := e.createImage(src, g.Bounds(src.Bounds())) g.Draw(dst, src) images = append(images, dst) } @@ -204,7 +236,7 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image. epubimagefilters.CropSplitDoublePage(b), epubimagefilters.Resize(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling), ) - dst := image.NewGray(g.Bounds(src.Bounds())) + dst := e.createImage(src, g.Bounds(src.Bounds())) g.Draw(dst, src) images = append(images, dst) } @@ -213,11 +245,11 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image. } // create a title page with the cover -func (e *EPUBImageProcessor) CoverTitleData(img image.Image, title string) (*epubzip.ZipImage, error) { +func (e *EPUBImageProcessor) CoverTitleData(src image.Image, title string) (*epubzip.ZipImage, error) { // Create a blur version of the cover g := gift.New(epubimagefilters.CoverTitle(title)) - dst := image.NewGray(g.Bounds(img.Bounds())) - g.Draw(dst, img) + dst := e.createImage(src, g.Bounds(src.Bounds())) + g.Draw(dst, src) return epubzip.CompressImage("OEBPS/Images/title.jpg", dst, e.Image.Quality) } diff --git a/internal/epub/options/epub_options.go b/internal/epub/options/epub_options.go index 8f14be1..9fb62cd 100644 --- a/internal/epub/options/epub_options.go +++ b/internal/epub/options/epub_options.go @@ -30,6 +30,7 @@ type Image struct { Manga bool HasCover bool View *View + GrayScale bool } type Options struct { diff --git a/main.go b/main.go index 8a571d3..d418a34 100644 --- a/main.go +++ b/main.go @@ -114,6 +114,8 @@ $ 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, Crop: &epuboptions.Crop{ Enabled: cmd.Options.Crop, Left: cmd.Options.CropRatioLeft, @@ -121,7 +123,6 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s Right: cmd.Options.CropRatioRight, Bottom: cmd.Options.CropRatioBottom, }, - Quality: cmd.Options.Quality, Brightness: cmd.Options.Brightness, Contrast: cmd.Options.Contrast, AutoRotate: cmd.Options.AutoRotate,