mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 08:12:36 +02:00
upscale or downscale image automatically
will make panel view works for small image
This commit is contained in:
parent
0360e297b8
commit
ef8e88c56b
47
internal/epub/filters/resize.go
Normal file
47
internal/epub/filters/resize.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package filters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/draw"
|
||||||
|
|
||||||
|
"github.com/disintegration/gift"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Resize(viewWidth, viewHeight int, resampling gift.Resampling) gift.Filter {
|
||||||
|
return &resizeFilter{
|
||||||
|
viewWidth, viewHeight, resampling,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type resizeFilter struct {
|
||||||
|
viewWidth, viewHeight int
|
||||||
|
resampling gift.Resampling
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *resizeFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
|
||||||
|
w, h := p.viewWidth, p.viewHeight
|
||||||
|
srcw, srch := srcBounds.Dx(), srcBounds.Dy()
|
||||||
|
|
||||||
|
if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 {
|
||||||
|
return image.Rect(0, 0, 0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
wratio := float64(srcw) / float64(w)
|
||||||
|
hratio := float64(srch) / float64(h)
|
||||||
|
|
||||||
|
var dstw, dsth int
|
||||||
|
if wratio > hratio {
|
||||||
|
dstw = w
|
||||||
|
dsth = int(float64(srch)/wratio + 0.5)
|
||||||
|
} else {
|
||||||
|
dsth = h
|
||||||
|
dstw = int(float64(srcw)/hratio + 0.5)
|
||||||
|
}
|
||||||
|
|
||||||
|
return image.Rect(0, 0, dstw, dsth)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *resizeFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
|
||||||
|
b := p.Bounds(src.Bounds())
|
||||||
|
gift.Resize(b.Dx(), b.Dy(), p.resampling).Draw(dst, src, options)
|
||||||
|
}
|
@ -19,7 +19,7 @@ func NewGift(options *ImageOptions) *gift.GIFT {
|
|||||||
g.Add(gift.Brightness(float32(options.Brightness)))
|
g.Add(gift.Brightness(float32(options.Brightness)))
|
||||||
}
|
}
|
||||||
g.Add(
|
g.Add(
|
||||||
gift.ResizeToFit(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||||
filters.Pixel(),
|
filters.Pixel(),
|
||||||
)
|
)
|
||||||
return g
|
return g
|
||||||
|
Loading…
x
Reference in New Issue
Block a user