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

Add test to validate resizing of animated image. #443

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions vips/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
var (
// ErrUnsupportedImageFormat when image type is unsupported
ErrUnsupportedImageFormat = errors.New("unsupported image format")
// ErrInvalidScaleForAnimatedImage when an animated image will not resize correctly with the given scale value
ErrInvalidScaleForAnimatedImage = errors.New("invalid scale for animated image")
)

func handleImageError(out *C.VipsImage) error {
Expand Down
17 changes: 12 additions & 5 deletions vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,18 @@ func (r *ImageRef) ResizeWithVScale(hScale, vScale float64, kernel Kernel) error

pages := r.Pages()
pageHeight := r.GetPageHeight()
newPageHeight := 0

if pages > 1 {
scale := hScale
if vScale != -1 {
scale = vScale
}
newPageHeight = int(float64(pageHeight) * scale)
if float64(newPageHeight) != float64(pageHeight)*scale {
return ErrInvalidScaleForAnimatedImage
}
}

out, err := vipsResizeWithVScale(r.image, hScale, vScale, kernel)
if err != nil {
Expand All @@ -1789,11 +1801,6 @@ func (r *ImageRef) ResizeWithVScale(hScale, vScale float64, kernel Kernel) error
r.setImage(out)

if pages > 1 {
scale := hScale
if vScale != -1 {
scale = vScale
}
newPageHeight := int(float64(pageHeight)*scale + 0.5)
if err := r.SetPageHeight(newPageHeight); err != nil {
return err
}
Expand Down
20 changes: 20 additions & 0 deletions vips/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,26 @@ func TestImageRef_Resize__Error(t *testing.T) {
require.Error(t, err)
}

func TestImageRef_Resize_Animated_CorrectScale(t *testing.T) {
Startup(nil)

image, err := NewImageFromFile(resources + "gif-animated.gif")
require.NoError(t, err)

err = image.Resize(0.5, KernelLanczos3)
require.NoError(t, err)
}

func TestImageRef_Resize_Animated_InvalidScale(t *testing.T) {
Startup(nil)

image, err := NewImageFromFile(resources + "gif-animated.gif")
require.NoError(t, err)

err = image.Resize(0.2, KernelLanczos3)
require.Error(t, err)
}

func TestImageRef_ExtractArea__Error(t *testing.T) {
Startup(nil)

Expand Down
Loading