From a5ef2a292737832778da8c0bb813d84a0c474f6e Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 15 Feb 2025 18:29:12 +0100 Subject: [PATCH] add format copy option --- internal/pkg/converter/converter.go | 7 ++++--- internal/pkg/epubimageprocessor/loader.go | 2 +- internal/pkg/epubimageprocessor/passthrough.go | 8 ++++++++ pkg/epub/epub.go | 14 +++++++++++++- 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 internal/pkg/epubimageprocessor/passthrough.go diff --git a/internal/pkg/converter/converter.go b/internal/pkg/converter/converter.go index c0703c8..50e85ac 100644 --- a/internal/pkg/converter/converter.go +++ b/internal/pkg/converter/converter.go @@ -15,6 +15,7 @@ import ( "reflect" "regexp" "runtime" + "slices" "strings" "time" @@ -131,7 +132,7 @@ func (c *Converter) InitParse() { c.AddStringParam(&c.Options.Image.View.Color.Foreground, "foreground-color", c.Options.Image.View.Color.Foreground, "Foreground color in hexadecimal format RGB. Black=000, White=FFF") c.AddStringParam(&c.Options.Image.View.Color.Background, "background-color", c.Options.Image.View.Color.Background, "Background color in hexadecimal format RGB. Black=000, White=FFF, Light Gray=DDD, Dark Gray=777") c.AddBoolParam(&c.Options.Image.Resize, "resize", c.Options.Image.Resize, "Reduce image size if exceed device size") - c.AddStringParam(&c.Options.Image.Format, "format", c.Options.Image.Format, "Format of output images: jpeg (lossy), png (lossless)") + c.AddStringParam(&c.Options.Image.Format, "format", c.Options.Image.Format, "Format of output images: jpeg (lossy), png (lossless), copy (no processing)") c.AddFloatParam(&c.Options.Image.View.AspectRatio, "aspect-ratio", c.Options.Image.View.AspectRatio, "Aspect ratio (height/width) of the output\n -1 = same as device\n 0 = same as source\n1.6 = amazon advice for kindle") c.AddBoolParam(&c.Options.Image.View.PortraitOnly, "portrait-only", c.Options.Image.View.PortraitOnly, "Portrait only: force orientation to portrait only.") c.AddIntParam(&c.Options.TitlePage, "titlepage", c.Options.TitlePage, "Title page\n0 = never\n1 = always\n2 = only if epub is split") @@ -379,8 +380,8 @@ func (c *Converter) Validate() error { } // Format - if !(c.Options.Image.Format == "jpeg" || c.Options.Image.Format == "png") { - return errors.New("format should be jpeg or png") + if !slices.Contains([]string{"jpeg", "png", "copy"}, c.Options.Image.Format) { + return errors.New("format should be jpeg, png or copy") } // Aspect Ratio diff --git a/internal/pkg/epubimageprocessor/loader.go b/internal/pkg/epubimageprocessor/loader.go index a120385..ec2761c 100644 --- a/internal/pkg/epubimageprocessor/loader.go +++ b/internal/pkg/epubimageprocessor/loader.go @@ -53,7 +53,7 @@ func (e EPUBImageProcessor) isSupportedImage(path string) bool { return false } -// Load images from input +// load images from input func (e EPUBImageProcessor) load() (totalImages int, output chan task, err error) { fi, err := os.Stat(e.Input) if err != nil { diff --git a/internal/pkg/epubimageprocessor/passthrough.go b/internal/pkg/epubimageprocessor/passthrough.go new file mode 100644 index 0000000..672769a --- /dev/null +++ b/internal/pkg/epubimageprocessor/passthrough.go @@ -0,0 +1,8 @@ +package epubimageprocessor + +import "github.com/celogeek/go-comic-converter/v3/internal/pkg/epubimage" + +func (e EPUBImageProcessor) PassThrough() (images []epubimage.EPUBImage, err error) { + images = make([]epubimage.EPUBImage, 0) + return images, nil +} diff --git a/pkg/epub/epub.go b/pkg/epub/epub.go index 9c44d66..ee22e05 100644 --- a/pkg/epub/epub.go +++ b/pkg/epub/epub.go @@ -3,6 +3,7 @@ package epub import ( "archive/zip" + "errors" "fmt" "math" "path/filepath" @@ -202,12 +203,23 @@ func (e epub) writeTitleImage(wz epubzip.EPUBZip, img epubimage.EPUBImage, title // extract image and split it into part func (e epub) getParts() (parts []epubPart, imgStorage epubzip.StorageImageReader, err error) { - images, err := e.imageProcessor.Load() + images, err := func() ([]epubimage.EPUBImage, error) { + if e.EPUBOptions.Image.Format == "copy" { + return e.imageProcessor.PassThrough() + } else { + return e.imageProcessor.Load() + } + }() if err != nil { return } + if len(images) == 0 { + err = errors.New("no image found") + return + } + // sort result by id and part sort.Slice(images, func(i, j int) bool { if images[i].Id == images[j].Id {