mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 00:02:37 +02:00
use gift filters to create aligned images
This commit is contained in:
parent
17d8161d99
commit
a2eeda8479
55
internal/epub/filters/position.go
Normal file
55
internal/epub/filters/position.go
Normal file
@ -0,0 +1,55 @@
|
||||
package filters
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
const (
|
||||
PositionCenter = iota
|
||||
PositionLeft
|
||||
PositionRight
|
||||
)
|
||||
|
||||
func Position(viewWidth, viewHeight int, align int) gift.Filter {
|
||||
return &positionFilter{
|
||||
viewWidth, viewHeight, align,
|
||||
}
|
||||
}
|
||||
|
||||
type positionFilter struct {
|
||||
viewWidth, viewHeight, align int
|
||||
}
|
||||
|
||||
func (p *positionFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
|
||||
return image.Rect(0, 0, p.viewWidth, p.viewHeight)
|
||||
}
|
||||
|
||||
func (p *positionFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
|
||||
draw.Draw(dst, dst.Bounds(), image.White, dst.Bounds().Min, draw.Over)
|
||||
|
||||
srcBounds := src.Bounds()
|
||||
left, top := (p.viewWidth-srcBounds.Dx())/2, (p.viewHeight-srcBounds.Dy())/2
|
||||
if p.align == PositionLeft {
|
||||
left = 0
|
||||
}
|
||||
|
||||
if p.align == PositionRight {
|
||||
left = p.viewWidth - srcBounds.Dx()
|
||||
}
|
||||
|
||||
draw.Draw(
|
||||
dst,
|
||||
image.Rect(
|
||||
left,
|
||||
top,
|
||||
p.viewWidth,
|
||||
p.viewHeight,
|
||||
),
|
||||
src,
|
||||
srcBounds.Min,
|
||||
draw.Over,
|
||||
)
|
||||
}
|
@ -21,6 +21,7 @@ func NewGift(options *ImageOptions) *gift.GIFT {
|
||||
g.Add(
|
||||
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||
filters.Pixel(),
|
||||
filters.Position(options.ViewWidth, options.ViewHeight, filters.PositionCenter),
|
||||
)
|
||||
return g
|
||||
}
|
||||
@ -28,25 +29,30 @@ func NewGift(options *ImageOptions) *gift.GIFT {
|
||||
func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT {
|
||||
gifts := make([]*gift.GIFT, 2)
|
||||
|
||||
rightFirst := options.Manga
|
||||
|
||||
gifts[0] = gift.New(
|
||||
filters.CropSplitDoublePage(rightFirst),
|
||||
filters.CropSplitDoublePage(options.Manga),
|
||||
)
|
||||
|
||||
gifts[1] = gift.New(
|
||||
filters.CropSplitDoublePage(!rightFirst),
|
||||
filters.CropSplitDoublePage(!options.Manga),
|
||||
)
|
||||
|
||||
for _, g := range gifts {
|
||||
for i, g := range gifts {
|
||||
if options.Contrast != 0 {
|
||||
g.Add(gift.Contrast(float32(options.Contrast)))
|
||||
}
|
||||
if options.Brightness != 0 {
|
||||
g.Add(gift.Brightness(float32(options.Brightness)))
|
||||
}
|
||||
|
||||
position := filters.PositionLeft
|
||||
if (i == 1) == options.Manga {
|
||||
position = filters.PositionRight
|
||||
}
|
||||
|
||||
g.Add(
|
||||
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||
filters.Position(options.ViewWidth, options.ViewHeight, position),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -226,6 +226,7 @@ func (e *ePub) LoadImages() ([]*Image, error) {
|
||||
part := i + 1
|
||||
dst := image.NewGray(g.Bounds(src.Bounds()))
|
||||
g.Draw(dst, src)
|
||||
|
||||
imageOutput <- &Image{
|
||||
Id: img.Id,
|
||||
Part: part,
|
||||
|
Loading…
x
Reference in New Issue
Block a user