From 21cbde14bc91e09a21ef8a1e41c6e99eeb394907 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 23 Sep 2023 09:51:47 +0200 Subject: [PATCH] add option keepdoublepageifsplitted --- internal/converter/converter.go | 1 + .../converter/options/converter_options.go | 35 ++++++++++--------- internal/epub/epub.go | 5 ++- .../imageprocessor/epub_image_processor.go | 8 +++++ internal/epub/options/epub_options.go | 31 ++++++++-------- .../epub/templates/epub_templates_content.go | 9 +++-- main.go | 31 ++++++++-------- 7 files changed, 71 insertions(+), 49 deletions(-) diff --git a/internal/converter/converter.go b/internal/converter/converter.go index 0dcb1bb..9bd7eec 100644 --- a/internal/converter/converter.go +++ b/internal/converter/converter.go @@ -119,6 +119,7 @@ func (c *Converter) InitParse() { c.AddBoolParam(&c.Options.AutoContrast, "autocontrast", c.Options.AutoContrast, "Improve contrast automatically") c.AddBoolParam(&c.Options.AutoRotate, "autorotate", c.Options.AutoRotate, "Auto Rotate page when width > height") c.AddBoolParam(&c.Options.AutoSplitDoublePage, "autosplitdoublepage", c.Options.AutoSplitDoublePage, "Auto Split double page when width > height") + c.AddBoolParam(&c.Options.KeepDoublePageIfSplitted, "keepdoublepageifsplitted", c.Options.KeepDoublePageIfSplitted, "Keep the double page if splitted") c.AddBoolParam(&c.Options.NoBlankImage, "noblankimage", c.Options.NoBlankImage, "Remove blank image") c.AddBoolParam(&c.Options.Manga, "manga", c.Options.Manga, "Manga mode (right to left)") c.AddBoolParam(&c.Options.HasCover, "hascover", c.Options.HasCover, "Has cover. Indicate if your comic have a cover. The first page will be used as a cover and include after the title.") diff --git a/internal/converter/options/converter_options.go b/internal/converter/options/converter_options.go index 808d527..6187aea 100644 --- a/internal/converter/options/converter_options.go +++ b/internal/converter/options/converter_options.go @@ -35,6 +35,7 @@ type Options struct { AutoContrast bool `yaml:"auto_contrast"` AutoRotate bool `yaml:"auto_rotate"` AutoSplitDoublePage bool `yaml:"auto_split_double_page"` + KeepDoublePageIfSplitted bool `yaml:"keep_double_page_if_splitted"` NoBlankImage bool `yaml:"no_blank_image"` Manga bool `yaml:"manga"` HasCover bool `yaml:"has_cover"` @@ -77,22 +78,23 @@ type Options struct { // Initialize default options. func New() *Options { return &Options{ - Profile: "SR", - Quality: 85, - Grayscale: true, - Crop: true, - CropRatioLeft: 1, - CropRatioUp: 1, - CropRatioRight: 1, - CropRatioBottom: 3, - NoBlankImage: true, - HasCover: true, - SortPathMode: 1, - ForegroundColor: "000", - BackgroundColor: "FFF", - Format: "jpeg", - TitlePage: 1, - profiles: profiles.New(), + Profile: "SR", + Quality: 85, + Grayscale: true, + Crop: true, + CropRatioLeft: 1, + CropRatioUp: 1, + CropRatioRight: 1, + CropRatioBottom: 3, + NoBlankImage: true, + HasCover: true, + KeepDoublePageIfSplitted: true, + SortPathMode: 1, + ForegroundColor: "000", + BackgroundColor: "FFF", + Format: "jpeg", + TitlePage: 1, + profiles: profiles.New(), } } @@ -208,6 +210,7 @@ func (o *Options) ShowConfig() string { {"AutoContrast", o.AutoContrast, true}, {"AutoRotate", o.AutoRotate, true}, {"AutoSplitDoublePage", o.AutoSplitDoublePage, true}, + {"KeepDoublePageIfSplitted", o.KeepDoublePageIfSplitted, o.AutoSplitDoublePage}, {"NoBlankImage", o.NoBlankImage, true}, {"Manga", o.Manga, true}, {"HasCover", o.HasCover, true}, diff --git a/internal/epub/epub.go b/internal/epub/epub.go index fa4154a..d8d3de8 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -442,7 +442,10 @@ func (e *ePub) Write() error { } // Double Page or Last Image that is not a double page - if !e.Image.View.PortraitOnly && (img.DoublePage || (img.Part == 0 && img == lastImage)) { + if !e.Image.View.PortraitOnly && + (img.DoublePage || + (!e.Image.KeepDoublePageIfSplitted && img.Part == 1) || + (img.Part == 0 && img == lastImage)) { if err := e.writeBlank(wz, img); err != nil { return err } diff --git a/internal/epub/imageprocessor/epub_image_processor.go b/internal/epub/imageprocessor/epub_image_processor.go index ce728a4..bcabab4 100644 --- a/internal/epub/imageprocessor/epub_image_processor.go +++ b/internal/epub/imageprocessor/epub_image_processor.go @@ -101,6 +101,14 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) { Error: input.Error, } + // do not keep double page if requested + if !img.IsCover && + img.DoublePage && + e.Options.Image.AutoSplitDoublePage && + !e.Options.Image.KeepDoublePageIfSplitted { + continue + } + if err = imgStorage.Add(img.EPUBImgPath(), dst, e.Image.Quality); err != nil { bar.Close() fmt.Fprintf(os.Stderr, "error with %s: %s", input.Name, err) diff --git a/internal/epub/options/epub_options.go b/internal/epub/options/epub_options.go index b926b00..3f6f44c 100644 --- a/internal/epub/options/epub_options.go +++ b/internal/epub/options/epub_options.go @@ -22,21 +22,22 @@ type View struct { } type Image struct { - Crop *Crop - Quality int - Brightness int - Contrast int - AutoContrast bool - AutoRotate bool - AutoSplitDoublePage bool - NoBlankImage bool - Manga bool - HasCover bool - View *View - GrayScale bool - GrayScaleMode int - Resize bool - Format string + Crop *Crop + Quality int + Brightness int + Contrast int + AutoContrast bool + AutoRotate bool + AutoSplitDoublePage bool + KeepDoublePageIfSplitted bool + NoBlankImage bool + Manga bool + HasCover bool + View *View + GrayScale bool + GrayScaleMode int + Resize bool + Format string } type Options struct { diff --git a/internal/epub/templates/epub_templates_content.go b/internal/epub/templates/epub_templates_content.go index 46609a9..55e5cec 100644 --- a/internal/epub/templates/epub_templates_content.go +++ b/internal/epub/templates/epub_templates_content.go @@ -174,7 +174,12 @@ func getManifest(o *ContentOptions) []tag { lastImage := o.Images[len(o.Images)-1] for _, img := range o.Images { - addTag(img, !o.ImageOptions.View.PortraitOnly && (img.DoublePage || (img.Part == 0 && img == lastImage))) + addTag( + img, + !o.ImageOptions.View.PortraitOnly && + (img.DoublePage || + (!o.ImageOptions.KeepDoublePageIfSplitted && img.Part == 1) || + (img.Part == 0 && img == lastImage))) } items = append(items, imageTags...) @@ -212,7 +217,7 @@ func getSpineAuto(o *ContentOptions) []tag { ) } for _, img := range o.Images { - if img.DoublePage && o.ImageOptions.Manga == isOnTheRight { + if (img.DoublePage || img.Part == 1) && o.ImageOptions.Manga == isOnTheRight { spine = append(spine, tag{ "itemref", tagAttrs{"idref": img.SpaceKey(), "properties": getSpreadBlank()}, diff --git a/main.go b/main.go index 9a72cf4..e600f78 100644 --- a/main.go +++ b/main.go @@ -114,21 +114,22 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s DryVerbose: cmd.Options.DryVerbose, Quiet: cmd.Options.Quiet, Image: &epuboptions.Image{ - Crop: &epuboptions.Crop{Enabled: cmd.Options.Crop, Left: cmd.Options.CropRatioLeft, Up: cmd.Options.CropRatioUp, Right: cmd.Options.CropRatioRight, Bottom: cmd.Options.CropRatioBottom}, - Quality: cmd.Options.Quality, - Brightness: cmd.Options.Brightness, - Contrast: cmd.Options.Contrast, - AutoContrast: cmd.Options.AutoContrast, - AutoRotate: cmd.Options.AutoRotate, - AutoSplitDoublePage: cmd.Options.AutoSplitDoublePage, - NoBlankImage: cmd.Options.NoBlankImage, - Manga: cmd.Options.Manga, - HasCover: cmd.Options.HasCover, - View: &epuboptions.View{Width: profile.Width, Height: profile.Height, AspectRatio: cmd.Options.AspectRatio, PortraitOnly: cmd.Options.PortraitOnly, Color: epuboptions.Color{Foreground: cmd.Options.ForegroundColor, Background: cmd.Options.BackgroundColor}}, - GrayScale: cmd.Options.Grayscale, - GrayScaleMode: cmd.Options.GrayscaleMode, - Resize: !cmd.Options.NoResize, - Format: cmd.Options.Format, + Crop: &epuboptions.Crop{Enabled: cmd.Options.Crop, Left: cmd.Options.CropRatioLeft, Up: cmd.Options.CropRatioUp, Right: cmd.Options.CropRatioRight, Bottom: cmd.Options.CropRatioBottom}, + Quality: cmd.Options.Quality, + Brightness: cmd.Options.Brightness, + Contrast: cmd.Options.Contrast, + AutoContrast: cmd.Options.AutoContrast, + AutoRotate: cmd.Options.AutoRotate, + AutoSplitDoublePage: cmd.Options.AutoSplitDoublePage, + KeepDoublePageIfSplitted: cmd.Options.KeepDoublePageIfSplitted, + NoBlankImage: cmd.Options.NoBlankImage, + Manga: cmd.Options.Manga, + HasCover: cmd.Options.HasCover, + View: &epuboptions.View{Width: profile.Width, Height: profile.Height, AspectRatio: cmd.Options.AspectRatio, PortraitOnly: cmd.Options.PortraitOnly, Color: epuboptions.Color{Foreground: cmd.Options.ForegroundColor, Background: cmd.Options.BackgroundColor}}, + GrayScale: cmd.Options.Grayscale, + GrayScaleMode: cmd.Options.GrayscaleMode, + Resize: !cmd.Options.NoResize, + Format: cmd.Options.Format, }, }).Write(); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err)