Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for blurry images from WEBP input and small output dimensions #368

Merged
merged 2 commits into from
Aug 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions resizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,29 +449,36 @@ 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) {
var image *C.VipsImage
var err error
var (
image *C.VipsImage
err error
)

// Reload input using shrink-on-load
if imageType == JPEG && shrink >= 2 {
shrinkOnLoad := 1
// Recalculate integral shrink and double residual
switch {
case shrink >= 8:
factor = factor / 8
shrinkOnLoad = 8
case shrink >= 4:
factor = factor / 4
shrinkOnLoad = 4
case shrink >= 2:
factor = factor / 2
shrinkOnLoad = 2
}
if shrink < 2 {
return nil, 0, fmt.Errorf("only available for shrink >=2")
}

shrinkOnLoad := 1
// Recalculate integral shrink and double residual
switch {
case shrink >= 8:
factor = factor / 8
shrinkOnLoad = 8
case shrink >= 4:
factor = factor / 4
shrinkOnLoad = 4
case shrink >= 2:
factor = factor / 2
shrinkOnLoad = 2
}

// Reload input using shrink-on-load
switch imageType {
case JPEG:
image, err = vipsShrinkJpeg(buf, input, shrinkOnLoad)
} else if imageType == WEBP {
image, err = vipsShrinkWebp(buf, input, shrink)
} else {
case WEBP:
image, err = vipsShrinkWebp(buf, input, shrinkOnLoad)
default:
return nil, 0, fmt.Errorf("%v doesn't support shrink on load", ImageTypeName(imageType))
}

Expand Down