compute original aspect ratio

This commit is contained in:
Celogeek 2023-05-04 16:56:30 +02:00
parent abf3b8facf
commit 88928168b4
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
6 changed files with 63 additions and 51 deletions

View File

@ -145,7 +145,7 @@ func (o *Options) LoadConfig() error {
// Get current settings for fields that can be saved // Get current settings for fields that can be saved
func (o *Options) ShowConfig() string { func (o *Options) ShowConfig() string {
var profileDesc, viewDesc string var profileDesc string
profile := o.GetProfile() profile := o.GetProfile()
if profile != nil { if profile != nil {
profileDesc = fmt.Sprintf( profileDesc = fmt.Sprintf(
@ -155,13 +155,6 @@ func (o *Options) ShowConfig() string {
profile.Width, profile.Width,
profile.Height, profile.Height,
) )
perfectWidth, perfectHeight := profile.PerfectDim()
viewDesc = fmt.Sprintf(
"%dx%d",
perfectWidth,
perfectHeight,
)
} }
sortpathmode := "" sortpathmode := ""
@ -181,8 +174,6 @@ func (o *Options) ShowConfig() string {
Condition bool Condition bool
}{ }{
{"Profile", profileDesc, true}, {"Profile", profileDesc, true},
{"ViewRatio", fmt.Sprintf("1:%s", strings.TrimRight(fmt.Sprintf("%f", profiles.PerfectRatio), "0")), true},
{"View", viewDesc, true},
{"Format", o.Format, true}, {"Format", o.Format, true},
{"Quality", o.Quality, o.Format == "jpeg"}, {"Quality", o.Quality, o.Format == "jpeg"},
{"Grayscale", o.Grayscale, true}, {"Grayscale", o.Grayscale, true},

View File

@ -15,21 +15,6 @@ type Profile struct {
Height int 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 type Profiles []Profile
// Initialize list of all supported profiles. // Initialize list of all supported profiles.

View File

@ -6,6 +6,7 @@ package epub
import ( import (
"archive/zip" "archive/zip"
"fmt" "fmt"
"math"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -193,6 +194,39 @@ func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
return c.WriteString("") 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 // create the zip
func (e *ePub) Write() error { func (e *ePub) Write() error {
type zipContent struct { type zipContent struct {
@ -231,6 +265,7 @@ func (e *ePub) Write() error {
Quiet: e.Quiet, Quiet: e.Quiet,
}) })
e.ComputeViewPort(epubParts)
for i, part := range epubParts { for i, part := range epubParts {
ext := filepath.Ext(e.Output) ext := filepath.Ext(e.Output)
suffix := "" suffix := ""

View File

@ -10,18 +10,19 @@ import (
) )
type Image struct { type Image struct {
Id int Id int
Part int Part int
Raw image.Image Raw image.Image
Width int Width int
Height int Height int
IsCover bool IsCover bool
IsBlank bool IsBlank bool
DoublePage bool DoublePage bool
Path string Path string
Name string Name string
Position string Position string
Format string Format string
OriginalAspectRatio float64
} }
// key name of the blank plage after the image // key name of the blank plage after the image

View File

@ -85,17 +85,18 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
} }
img := &epubimage.Image{ img := &epubimage.Image{
Id: input.Id, Id: input.Id,
Part: part, Part: part,
Raw: raw, Raw: raw,
Width: dst.Bounds().Dx(), Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(), Height: dst.Bounds().Dy(),
IsCover: input.Id == 0 && part == 0, IsCover: input.Id == 0 && part == 0,
IsBlank: dst.Bounds().Dx() == 1 && dst.Bounds().Dy() == 1, IsBlank: dst.Bounds().Dx() == 1 && dst.Bounds().Dy() == 1,
DoublePage: part == 0 && src.Bounds().Dx() > src.Bounds().Dy(), DoublePage: part == 0 && src.Bounds().Dx() > src.Bounds().Dy(),
Path: input.Path, Path: input.Path,
Name: input.Name, Name: input.Name,
Format: e.Image.Format, 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 { if err = imgStorage.Add(img.EPUBImgPath(), dst, e.Image.Quality); err != nil {

View File

@ -99,7 +99,6 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
fmt.Fprintln(os.Stderr, cmd.Options) fmt.Fprintln(os.Stderr, cmd.Options)
profile := cmd.Options.GetProfile() profile := cmd.Options.GetProfile()
perfectWidth, perfectHeight := profile.PerfectDim()
if err := epub.New(&epuboptions.Options{ if err := epub.New(&epuboptions.Options{
Input: cmd.Options.Input, Input: cmd.Options.Input,
@ -131,8 +130,8 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
Manga: cmd.Options.Manga, Manga: cmd.Options.Manga,
HasCover: cmd.Options.HasCover, HasCover: cmd.Options.HasCover,
View: &epuboptions.View{ View: &epuboptions.View{
Width: perfectWidth, Width: profile.Width,
Height: perfectHeight, Height: profile.Height,
Color: epuboptions.Color{ Color: epuboptions.Color{
Foreground: cmd.Options.ForegroundColor, Foreground: cmd.Options.ForegroundColor,
Background: cmd.Options.BackgroundColor, Background: cmd.Options.BackgroundColor,