mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 16:22:37 +02:00
add option to choose aspect ratio
This commit is contained in:
parent
88928168b4
commit
bb15fa5538
@ -84,6 +84,12 @@ func (c *Converter) AddIntParam(p *int, name string, value int, usage string) {
|
|||||||
c.order = append(c.order, converterOrderName{value: name})
|
c.order = append(c.order, converterOrderName{value: name})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add an float parameter
|
||||||
|
func (c *Converter) AddFloatParam(p *float64, name string, value float64, usage string) {
|
||||||
|
c.Cmd.Float64Var(p, name, value, usage)
|
||||||
|
c.order = append(c.order, converterOrderName{value: name})
|
||||||
|
}
|
||||||
|
|
||||||
// Add a boolean parameter
|
// Add a boolean parameter
|
||||||
func (c *Converter) AddBoolParam(p *bool, name string, value bool, usage string) {
|
func (c *Converter) AddBoolParam(p *bool, name string, value bool, usage string) {
|
||||||
c.Cmd.BoolVar(p, name, value, usage)
|
c.Cmd.BoolVar(p, name, value, usage)
|
||||||
@ -121,6 +127,7 @@ func (c *Converter) InitParse() {
|
|||||||
c.AddStringParam(&c.Options.BackgroundColor, "background-color", c.Options.BackgroundColor, "Background color in hexa format RGB. Black=000, White=FFF, Light Gray=DDD, Dark Gray=777")
|
c.AddStringParam(&c.Options.BackgroundColor, "background-color", c.Options.BackgroundColor, "Background color in hexa format RGB. Black=000, White=FFF, Light Gray=DDD, Dark Gray=777")
|
||||||
c.AddBoolParam(&c.Options.NoResize, "noresize", c.Options.NoResize, "Do not reduce image size if exceed device size")
|
c.AddBoolParam(&c.Options.NoResize, "noresize", c.Options.NoResize, "Do not reduce image size if exceed device size")
|
||||||
c.AddStringParam(&c.Options.Format, "format", c.Options.Format, "Format of output images: jpeg (lossy), png (lossless)")
|
c.AddStringParam(&c.Options.Format, "format", c.Options.Format, "Format of output images: jpeg (lossy), png (lossless)")
|
||||||
|
c.AddFloatParam(&c.Options.AspectRatio, "aspect-ratio", c.Options.AspectRatio, "Aspect ratio (height/width) of the output\n -1 = same as device\n 0 = same as source\n1.6 = amazon advice for kindle")
|
||||||
|
|
||||||
c.AddSection("Default config")
|
c.AddSection("Default config")
|
||||||
c.AddBoolParam(&c.Options.Show, "show", false, "Show your default parameters")
|
c.AddBoolParam(&c.Options.Show, "show", false, "Show your default parameters")
|
||||||
@ -351,6 +358,11 @@ func (c *Converter) Validate() error {
|
|||||||
return errors.New("format should be jpeg or png")
|
return errors.New("format should be jpeg or png")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Aspect Ratio
|
||||||
|
if c.Options.AspectRatio < 0 && c.Options.AspectRatio != -1 {
|
||||||
|
return errors.New("aspect ratio should be: -1, 0, > 0")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@ type Options struct {
|
|||||||
BackgroundColor string `yaml:"background_color"`
|
BackgroundColor string `yaml:"background_color"`
|
||||||
NoResize bool `yaml:"noresize"`
|
NoResize bool `yaml:"noresize"`
|
||||||
Format string `yaml:"format"`
|
Format string `yaml:"format"`
|
||||||
|
AspectRatio float64 `yaml:"aspect_ratio"`
|
||||||
|
|
||||||
// Default Config
|
// Default Config
|
||||||
Show bool `yaml:"-"`
|
Show bool `yaml:"-"`
|
||||||
@ -94,6 +95,7 @@ func New() *Options {
|
|||||||
BackgroundColor: "FFF",
|
BackgroundColor: "FFF",
|
||||||
NoResize: false,
|
NoResize: false,
|
||||||
Format: "jpeg",
|
Format: "jpeg",
|
||||||
|
AspectRatio: 0,
|
||||||
profiles: profiles.New(),
|
profiles: profiles.New(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,6 +169,13 @@ func (o *Options) ShowConfig() string {
|
|||||||
sortpathmode = "path=alphanum, file=alphanum"
|
sortpathmode = "path=alphanum, file=alphanum"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
aspectRatio := "auto"
|
||||||
|
if o.AspectRatio > 0 {
|
||||||
|
aspectRatio = fmt.Sprintf("1:%.02f", o.AspectRatio)
|
||||||
|
} else if o.AspectRatio < 0 {
|
||||||
|
aspectRatio = fmt.Sprintf("1:%0.2f (device)", float64(profile.Height)/float64(profile.Width))
|
||||||
|
}
|
||||||
|
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
for _, v := range []struct {
|
for _, v := range []struct {
|
||||||
Key string
|
Key string
|
||||||
@ -192,6 +201,7 @@ func (o *Options) ShowConfig() string {
|
|||||||
{"Foreground Color", fmt.Sprintf("#%s", o.ForegroundColor), true},
|
{"Foreground Color", fmt.Sprintf("#%s", o.ForegroundColor), true},
|
||||||
{"Background Color", fmt.Sprintf("#%s", o.BackgroundColor), true},
|
{"Background Color", fmt.Sprintf("#%s", o.BackgroundColor), true},
|
||||||
{"Resize", !o.NoResize, true},
|
{"Resize", !o.NoResize, true},
|
||||||
|
{"Aspect Ratio", aspectRatio, true},
|
||||||
} {
|
} {
|
||||||
if v.Condition {
|
if v.Condition {
|
||||||
b.WriteString(fmt.Sprintf("\n %-26s: %v", v.Key, v.Value))
|
b.WriteString(fmt.Sprintf("\n %-26s: %v", v.Key, v.Value))
|
||||||
|
@ -194,8 +194,7 @@ func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
|
|||||||
return c.WriteString("")
|
return c.WriteString("")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ePub) ComputeViewPort(epubParts []*epubPart) {
|
func (e *ePub) computeAspectRatio(epubParts []*epubPart) float64 {
|
||||||
// readjusting view port
|
|
||||||
var (
|
var (
|
||||||
bestAspectRatio float64
|
bestAspectRatio float64
|
||||||
bestAspectRatioCount int
|
bestAspectRatioCount int
|
||||||
@ -209,7 +208,7 @@ func (e *ePub) ComputeViewPort(epubParts []*epubPart) {
|
|||||||
for _, p := range epubParts {
|
for _, p := range epubParts {
|
||||||
aspectRatio[trunc(p.Cover.OriginalAspectRatio)]++
|
aspectRatio[trunc(p.Cover.OriginalAspectRatio)]++
|
||||||
for _, i := range p.Images {
|
for _, i := range p.Images {
|
||||||
aspectRatio[trunc(i.OriginalAspectRatio)/100]++
|
aspectRatio[trunc(i.OriginalAspectRatio)]++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,6 +218,20 @@ func (e *ePub) ComputeViewPort(epubParts []*epubPart) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return bestAspectRatio
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ePub) computeViewPort(epubParts []*epubPart) {
|
||||||
|
if e.Image.View.AspectRatio == -1 {
|
||||||
|
return //keep device size
|
||||||
|
}
|
||||||
|
|
||||||
|
// readjusting view port
|
||||||
|
bestAspectRatio := e.Image.View.AspectRatio
|
||||||
|
if bestAspectRatio == 0 {
|
||||||
|
bestAspectRatio = e.computeAspectRatio(epubParts)
|
||||||
|
}
|
||||||
|
|
||||||
viewWidth, viewHeight := int(float64(e.Image.View.Height)/bestAspectRatio), int(float64(e.Image.View.Width)*bestAspectRatio)
|
viewWidth, viewHeight := int(float64(e.Image.View.Height)/bestAspectRatio), int(float64(e.Image.View.Width)*bestAspectRatio)
|
||||||
if viewWidth > e.Image.View.Width {
|
if viewWidth > e.Image.View.Width {
|
||||||
e.Image.View.Height = viewHeight
|
e.Image.View.Height = viewHeight
|
||||||
@ -265,7 +278,7 @@ func (e *ePub) Write() error {
|
|||||||
Quiet: e.Quiet,
|
Quiet: e.Quiet,
|
||||||
})
|
})
|
||||||
|
|
||||||
e.ComputeViewPort(epubParts)
|
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 := ""
|
||||||
|
@ -16,6 +16,7 @@ type Color struct {
|
|||||||
|
|
||||||
type View struct {
|
type View struct {
|
||||||
Width, Height int
|
Width, Height int
|
||||||
|
AspectRatio float64
|
||||||
Color Color
|
Color Color
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
main.go
1
main.go
@ -132,6 +132,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
|
|||||||
View: &epuboptions.View{
|
View: &epuboptions.View{
|
||||||
Width: profile.Width,
|
Width: profile.Width,
|
||||||
Height: profile.Height,
|
Height: profile.Height,
|
||||||
|
AspectRatio: cmd.Options.AspectRatio,
|
||||||
Color: epuboptions.Color{
|
Color: epuboptions.Color{
|
||||||
Foreground: cmd.Options.ForegroundColor,
|
Foreground: cmd.Options.ForegroundColor,
|
||||||
Background: cmd.Options.BackgroundColor,
|
Background: cmd.Options.BackgroundColor,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user