diff --git a/internal/converter/options/converter_options.go b/internal/converter/options/converter_options.go index d5e8796..3a39859 100644 --- a/internal/converter/options/converter_options.go +++ b/internal/converter/options/converter_options.go @@ -145,7 +145,7 @@ func (o *Options) LoadConfig() error { // Get current settings for fields that can be saved func (o *Options) ShowConfig() string { - var profileDesc, viewDesc string + var profileDesc string profile := o.GetProfile() if profile != nil { profileDesc = fmt.Sprintf( @@ -155,13 +155,6 @@ func (o *Options) ShowConfig() string { profile.Width, profile.Height, ) - - perfectWidth, perfectHeight := profile.PerfectDim() - viewDesc = fmt.Sprintf( - "%dx%d", - perfectWidth, - perfectHeight, - ) } sortpathmode := "" @@ -181,8 +174,6 @@ func (o *Options) ShowConfig() string { Condition bool }{ {"Profile", profileDesc, true}, - {"ViewRatio", fmt.Sprintf("1:%s", strings.TrimRight(fmt.Sprintf("%f", profiles.PerfectRatio), "0")), true}, - {"View", viewDesc, true}, {"Format", o.Format, true}, {"Quality", o.Quality, o.Format == "jpeg"}, {"Grayscale", o.Grayscale, true}, diff --git a/internal/converter/profiles/converter_profiles.go b/internal/converter/profiles/converter_profiles.go index 413cebf..cb4df00 100644 --- a/internal/converter/profiles/converter_profiles.go +++ b/internal/converter/profiles/converter_profiles.go @@ -15,21 +15,6 @@ type Profile struct { Height int } -// Recommended ratio of image for perfect rendering Portrait or Landscape. -const PerfectRatio = 1.6 - -// Compute best dimension based on device size -func (p Profile) PerfectDim() (int, int) { - width, height := float64(p.Width), float64(p.Height) - perfectWidth, perfectHeight := height/PerfectRatio, width*PerfectRatio - if perfectWidth > width { - perfectWidth = width - } else { - perfectHeight = height - } - return int(perfectWidth), int(perfectHeight) -} - type Profiles []Profile // Initialize list of all supported profiles. diff --git a/internal/epub/epub.go b/internal/epub/epub.go index 380082a..633c361 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -6,6 +6,7 @@ package epub import ( "archive/zip" "fmt" + "math" "os" "path/filepath" "regexp" @@ -193,6 +194,39 @@ func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string { return c.WriteString("") } +func (e *ePub) ComputeViewPort(epubParts []*epubPart) { + // readjusting view port + var ( + bestAspectRatio float64 + bestAspectRatioCount int + aspectRatio = map[float64]int{} + ) + + trunc := func(v float64) float64 { + return float64(math.Round(v*100)) / 100 + } + + for _, p := range epubParts { + aspectRatio[trunc(p.Cover.OriginalAspectRatio)]++ + for _, i := range p.Images { + aspectRatio[trunc(i.OriginalAspectRatio)/100]++ + } + } + + for k, v := range aspectRatio { + if v > bestAspectRatioCount { + bestAspectRatio, bestAspectRatioCount = k, v + } + } + + viewWidth, viewHeight := int(float64(e.Image.View.Height)/bestAspectRatio), int(float64(e.Image.View.Width)*bestAspectRatio) + if viewWidth > e.Image.View.Width { + e.Image.View.Height = viewHeight + } else { + e.Image.View.Width = viewWidth + } +} + // create the zip func (e *ePub) Write() error { type zipContent struct { @@ -231,6 +265,7 @@ func (e *ePub) Write() error { Quiet: e.Quiet, }) + e.ComputeViewPort(epubParts) for i, part := range epubParts { ext := filepath.Ext(e.Output) suffix := "" diff --git a/internal/epub/image/epub_image.go b/internal/epub/image/epub_image.go index f6d5e73..046ce03 100644 --- a/internal/epub/image/epub_image.go +++ b/internal/epub/image/epub_image.go @@ -10,18 +10,19 @@ import ( ) type Image struct { - Id int - Part int - Raw image.Image - Width int - Height int - IsCover bool - IsBlank bool - DoublePage bool - Path string - Name string - Position string - Format string + Id int + Part int + Raw image.Image + Width int + Height int + IsCover bool + IsBlank bool + DoublePage bool + Path string + Name string + Position string + Format string + OriginalAspectRatio float64 } // key name of the blank plage after the image diff --git a/internal/epub/imageprocessor/epub_image_processor.go b/internal/epub/imageprocessor/epub_image_processor.go index e308255..e8d3bd3 100644 --- a/internal/epub/imageprocessor/epub_image_processor.go +++ b/internal/epub/imageprocessor/epub_image_processor.go @@ -85,17 +85,18 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) { } img := &epubimage.Image{ - Id: input.Id, - Part: part, - Raw: raw, - Width: dst.Bounds().Dx(), - Height: dst.Bounds().Dy(), - IsCover: input.Id == 0 && part == 0, - IsBlank: dst.Bounds().Dx() == 1 && dst.Bounds().Dy() == 1, - DoublePage: part == 0 && src.Bounds().Dx() > src.Bounds().Dy(), - Path: input.Path, - Name: input.Name, - Format: e.Image.Format, + Id: input.Id, + Part: part, + Raw: raw, + Width: dst.Bounds().Dx(), + Height: dst.Bounds().Dy(), + IsCover: input.Id == 0 && part == 0, + IsBlank: dst.Bounds().Dx() == 1 && dst.Bounds().Dy() == 1, + DoublePage: part == 0 && src.Bounds().Dx() > src.Bounds().Dy(), + Path: input.Path, + Name: input.Name, + Format: e.Image.Format, + OriginalAspectRatio: float64(src.Bounds().Dy()) / float64(src.Bounds().Dx()), } if err = imgStorage.Add(img.EPUBImgPath(), dst, e.Image.Quality); err != nil { diff --git a/main.go b/main.go index d354def..441c8ad 100644 --- a/main.go +++ b/main.go @@ -99,7 +99,6 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s fmt.Fprintln(os.Stderr, cmd.Options) profile := cmd.Options.GetProfile() - perfectWidth, perfectHeight := profile.PerfectDim() if err := epub.New(&epuboptions.Options{ Input: cmd.Options.Input, @@ -131,8 +130,8 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s Manga: cmd.Options.Manga, HasCover: cmd.Options.HasCover, View: &epuboptions.View{ - Width: perfectWidth, - Height: perfectHeight, + Width: profile.Width, + Height: profile.Height, Color: epuboptions.Color{ Foreground: cmd.Options.ForegroundColor, Background: cmd.Options.BackgroundColor,