Fix for blurry images from WEBP input and small output dimensions

master
Nils Juenemann 5 years ago
parent d0fb333ce4
commit c51a11f6d2

@ -69,6 +69,7 @@ func resizer(buf []byte, o Options) ([]byte, error) {
shrink := calculateShrink(factor, o.Interpolator) shrink := calculateShrink(factor, o.Interpolator)
residual := calculateResidual(factor, shrink) residual := calculateResidual(factor, shrink)
fmt.Printf("size=%vx%v, factor=%v, shrink=%v, residual=%v, opts=%+v\n", inWidth, inHeight, factor, shrink, residual, o)
// Do not enlarge the output if the input width or height // Do not enlarge the output if the input width or height
// are already less than the required dimensions // are already less than the required dimensions
if !o.Enlarge && !o.Force { if !o.Enlarge && !o.Force {
@ -449,11 +450,15 @@ func shrinkImage(image *C.VipsImage, o Options, residual float64, shrink int) (*
} }
func shrinkOnLoad(buf []byte, input *C.VipsImage, imageType ImageType, factor float64, shrink int) (*C.VipsImage, float64, error) { func shrinkOnLoad(buf []byte, input *C.VipsImage, imageType ImageType, factor float64, shrink int) (*C.VipsImage, float64, error) {
var image *C.VipsImage var (
var err error image *C.VipsImage
err error
)
if shrink < 2 {
return nil, 0, fmt.Errorf("only available for shrink >=2")
}
// Reload input using shrink-on-load
if imageType == JPEG && shrink >= 2 {
shrinkOnLoad := 1 shrinkOnLoad := 1
// Recalculate integral shrink and double residual // Recalculate integral shrink and double residual
switch { switch {
@ -468,10 +473,13 @@ func shrinkOnLoad(buf []byte, input *C.VipsImage, imageType ImageType, factor fl
shrinkOnLoad = 2 shrinkOnLoad = 2
} }
// Reload input using shrink-on-load
switch imageType {
case JPEG:
image, err = vipsShrinkJpeg(buf, input, shrinkOnLoad) image, err = vipsShrinkJpeg(buf, input, shrinkOnLoad)
} else if imageType == WEBP { case WEBP:
image, err = vipsShrinkWebp(buf, input, shrink) image, err = vipsShrinkWebp(buf, input, shrinkOnLoad)
} else { default:
return nil, 0, fmt.Errorf("%v doesn't support shrink on load", ImageTypeName(imageType)) return nil, 0, fmt.Errorf("%v doesn't support shrink on load", ImageTypeName(imageType))
} }

Loading…
Cancel
Save