Skip to content

Commit

Permalink
add option keepdoublepageifsplitted
Browse files Browse the repository at this point in the history
  • Loading branch information
celogeek committed Sep 23, 2023
1 parent 7ea9b9f commit 21cbde1
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 49 deletions.
1 change: 1 addition & 0 deletions internal/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
35 changes: 19 additions & 16 deletions internal/converter/options/converter_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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},
Expand Down
5 changes: 4 additions & 1 deletion internal/epub/epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions internal/epub/imageprocessor/epub_image_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 16 additions & 15 deletions internal/epub/options/epub_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions internal/epub/templates/epub_templates_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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()},
Expand Down
31 changes: 16 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 21cbde1

Please sign in to comment.