Skip to content

Commit

Permalink
Add test to validate resizing of animated image.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKigen committed Jul 29, 2024
1 parent 0c98a28 commit a8f1199
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
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

0 comments on commit a8f1199

Please sign in to comment.