mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 08:12:36 +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
|
Palette color.Palette
|
||||||
Brightness int
|
Brightness int
|
||||||
Contrast int
|
Contrast int
|
||||||
|
AutoRotate bool
|
||||||
|
Workers int
|
||||||
}
|
}
|
||||||
|
|
||||||
type EpubOptions struct {
|
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"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/disintegration/gift"
|
"github.com/disintegration/gift"
|
||||||
|
|
||||||
"github.com/nwaples/rardecode"
|
"github.com/nwaples/rardecode"
|
||||||
pdfimage "github.com/raff/pdfreader/image"
|
pdfimage "github.com/raff/pdfreader/image"
|
||||||
"github.com/raff/pdfreader/pdfread"
|
"github.com/raff/pdfreader/pdfread"
|
||||||
@ -122,9 +120,8 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
|
|||||||
|
|
||||||
// processing
|
// processing
|
||||||
wg := &sync.WaitGroup{}
|
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)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
@ -136,14 +133,14 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare filter
|
if options.AutoRotate {
|
||||||
g := gift.New(
|
g := gift.New(gift.Crop(findMarging(src)))
|
||||||
gift.Crop(findMarging(src)),
|
newSrc := image.NewNRGBA(g.Bounds(src.Bounds()))
|
||||||
gift.Contrast(float32(options.Contrast)),
|
g.Draw(newSrc, src)
|
||||||
gift.Brightness(float32(options.Brightness)),
|
src = newSrc
|
||||||
gift.ResizeToFit(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
}
|
||||||
)
|
|
||||||
g.SetParallelization(false)
|
g := NewGift(options)
|
||||||
|
|
||||||
// Convert image
|
// Convert image
|
||||||
dst := image.NewPaletted(g.Bounds(src.Bounds()), options.Palette)
|
dst := image.NewPaletted(g.Bounds(src.Bounds()), options.Palette)
|
||||||
|
11
main.go
11
main.go
@ -6,6 +6,7 @@ import (
|
|||||||
"image/color"
|
"image/color"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/celogeek/go-comic-converter/internal/epub"
|
"github.com/celogeek/go-comic-converter/internal/epub"
|
||||||
@ -65,6 +66,8 @@ type Option struct {
|
|||||||
NoCrop bool
|
NoCrop bool
|
||||||
Brightness int
|
Brightness int
|
||||||
Contrast int
|
Contrast int
|
||||||
|
AutoRotate bool
|
||||||
|
Workers int
|
||||||
LimitMb int
|
LimitMb int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +98,9 @@ Options:
|
|||||||
Crop : %v
|
Crop : %v
|
||||||
Brightness: %d
|
Brightness: %d
|
||||||
Contrast : %d
|
Contrast : %d
|
||||||
|
AutoRotate: %v
|
||||||
LimitMb : %s
|
LimitMb : %s
|
||||||
|
Workers : %d
|
||||||
`,
|
`,
|
||||||
o.Input,
|
o.Input,
|
||||||
o.Output,
|
o.Output,
|
||||||
@ -106,7 +111,9 @@ Options:
|
|||||||
!o.NoCrop,
|
!o.NoCrop,
|
||||||
o.Brightness,
|
o.Brightness,
|
||||||
o.Contrast,
|
o.Contrast,
|
||||||
|
o.AutoRotate,
|
||||||
limitmb,
|
limitmb,
|
||||||
|
o.Workers,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,7 +139,9 @@ func main() {
|
|||||||
flag.BoolVar(&opt.NoCrop, "nocrop", false, "Disable cropping")
|
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.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.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.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() {
|
flag.Usage = func() {
|
||||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0]))
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0]))
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
@ -229,6 +238,8 @@ func main() {
|
|||||||
Palette: profile.Palette,
|
Palette: profile.Palette,
|
||||||
Brightness: opt.Brightness,
|
Brightness: opt.Brightness,
|
||||||
Contrast: opt.Contrast,
|
Contrast: opt.Contrast,
|
||||||
|
AutoRotate: opt.AutoRotate,
|
||||||
|
Workers: opt.Workers,
|
||||||
},
|
},
|
||||||
}).Write(); err != nil {
|
}).Write(); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user