mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
add workers and autorotate options
This commit is contained in:
parent
8475faa648
commit
3524ce6d04
@ -20,6 +20,8 @@ type ImageOptions struct {
|
||||
Palette color.Palette
|
||||
Brightness int
|
||||
Contrast int
|
||||
AutoRotate bool
|
||||
Workers int
|
||||
}
|
||||
|
||||
type EpubOptions struct {
|
||||
|
46
internal/epub/image_filters.go
Normal file
46
internal/epub/image_filters.go
Normal file
@ -0,0 +1,46 @@
|
||||
package epub
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
func NewGift(options *ImageOptions) *gift.GIFT {
|
||||
g := gift.New()
|
||||
g.SetParallelization(false)
|
||||
|
||||
if options.AutoRotate {
|
||||
g.Add(&autoRotateFilter{})
|
||||
}
|
||||
if options.Contrast != 0 {
|
||||
g.Add(gift.Contrast(float32(options.Contrast)))
|
||||
}
|
||||
if options.Brightness != 0 {
|
||||
g.Add(gift.Brightness(float32(options.Brightness)))
|
||||
}
|
||||
g.Add(
|
||||
gift.ResizeToFit(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||
)
|
||||
return g
|
||||
}
|
||||
|
||||
type autoRotateFilter struct{}
|
||||
|
||||
func (p *autoRotateFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
|
||||
if srcBounds.Dx() > srcBounds.Dy() {
|
||||
dstBounds = gift.Rotate90().Bounds(srcBounds)
|
||||
} else {
|
||||
dstBounds = srcBounds
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *autoRotateFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
|
||||
if src.Bounds().Dx() > src.Bounds().Dy() {
|
||||
gift.Rotate90().Draw(dst, src, options)
|
||||
} else {
|
||||
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src)
|
||||
}
|
||||
}
|
@ -11,13 +11,11 @@ import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/disintegration/gift"
|
||||
|
||||
"github.com/nwaples/rardecode"
|
||||
pdfimage "github.com/raff/pdfreader/image"
|
||||
"github.com/raff/pdfreader/pdfread"
|
||||
@ -122,9 +120,8 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
|
||||
|
||||
// processing
|
||||
wg := &sync.WaitGroup{}
|
||||
bar := NewBar(imageCount, "Processing", 1, 2)
|
||||
|
||||
for i := 0; i < runtime.NumCPU(); i++ {
|
||||
for i := 0; i < options.Workers; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
@ -136,14 +133,14 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// prepare filter
|
||||
g := gift.New(
|
||||
gift.Crop(findMarging(src)),
|
||||
gift.Contrast(float32(options.Contrast)),
|
||||
gift.Brightness(float32(options.Brightness)),
|
||||
gift.ResizeToFit(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||
)
|
||||
g.SetParallelization(false)
|
||||
if options.AutoRotate {
|
||||
g := gift.New(gift.Crop(findMarging(src)))
|
||||
newSrc := image.NewNRGBA(g.Bounds(src.Bounds()))
|
||||
g.Draw(newSrc, src)
|
||||
src = newSrc
|
||||
}
|
||||
|
||||
g := NewGift(options)
|
||||
|
||||
// Convert image
|
||||
dst := image.NewPaletted(g.Bounds(src.Bounds()), options.Palette)
|
||||
|
11
main.go
11
main.go
@ -6,6 +6,7 @@ import (
|
||||
"image/color"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/internal/epub"
|
||||
@ -65,6 +66,8 @@ type Option struct {
|
||||
NoCrop bool
|
||||
Brightness int
|
||||
Contrast int
|
||||
AutoRotate bool
|
||||
Workers int
|
||||
LimitMb int
|
||||
}
|
||||
|
||||
@ -95,7 +98,9 @@ Options:
|
||||
Crop : %v
|
||||
Brightness: %d
|
||||
Contrast : %d
|
||||
AutoRotate: %v
|
||||
LimitMb : %s
|
||||
Workers : %d
|
||||
`,
|
||||
o.Input,
|
||||
o.Output,
|
||||
@ -106,7 +111,9 @@ Options:
|
||||
!o.NoCrop,
|
||||
o.Brightness,
|
||||
o.Contrast,
|
||||
o.AutoRotate,
|
||||
limitmb,
|
||||
o.Workers,
|
||||
)
|
||||
}
|
||||
|
||||
@ -132,7 +139,9 @@ func main() {
|
||||
flag.BoolVar(&opt.NoCrop, "nocrop", false, "Disable cropping")
|
||||
flag.IntVar(&opt.Brightness, "brightness", 0, "Brightness readjustement: between -100 and 100, > 0 lighter, < 0 darker")
|
||||
flag.IntVar(&opt.Contrast, "contrast", 0, "Contrast readjustement: between -100 and 100, > 0 more contrast, < 0 less contrast")
|
||||
flag.BoolVar(&opt.AutoRotate, "autorotate", false, "Auto Rotate page when width > height")
|
||||
flag.IntVar(&opt.LimitMb, "limitmb", 0, "Limit size of the ePub: Default nolimit (0), Minimum 20")
|
||||
flag.IntVar(&opt.Workers, "workers", runtime.NumCPU(), "Number of workers")
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0]))
|
||||
flag.PrintDefaults()
|
||||
@ -229,6 +238,8 @@ func main() {
|
||||
Palette: profile.Palette,
|
||||
Brightness: opt.Brightness,
|
||||
Contrast: opt.Contrast,
|
||||
AutoRotate: opt.AutoRotate,
|
||||
Workers: opt.Workers,
|
||||
},
|
||||
}).Write(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user