support grayscale or color mode

grayscale is on by default
as the conversion is mainly
useful for eInk device.
This commit is contained in:
Celogeek 2023-04-30 18:20:54 +02:00
parent 1c1c1fd38c
commit 8b04cbc38f
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
5 changed files with 44 additions and 6 deletions

View File

@ -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.")

View File

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

View File

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

View File

@ -30,6 +30,7 @@ type Image struct {
Manga bool
HasCover bool
View *View
GrayScale bool
}
type Options struct {

View File

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