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
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},

View File

@ -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.

View File

@ -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 := ""

View File

@ -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

View File

@ -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 {

View File

@ -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,