Skip to content

Commit

Permalink
fix ResizeWithVScale and remove code duplication (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Laufer authored Dec 23, 2021
1 parent 5eb920c commit 5934011
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 26 deletions.
19 changes: 7 additions & 12 deletions vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,12 +1168,18 @@ func (r *ImageRef) Rank(width int, height int, index int) error {

// Resize resizes the image based on the scale, maintaining aspect ratio
func (r *ImageRef) Resize(scale float64, kernel Kernel) error {
return r.ResizeWithVScale(scale, -1, kernel)
}

// ResizeWithVScale resizes the image with both horizontal as well as vertical scaling.
// The parameters are the scaling factors.
func (r *ImageRef) ResizeWithVScale(hScale, vScale float64, kernel Kernel) error {
err := r.PremultiplyAlpha()
if err != nil {
return err
}

out, err := vipsResize(r.image, scale, kernel)
out, err := vipsResizeWithVScale(r.image, hScale, vScale, kernel)
if err != nil {
return err
}
Expand All @@ -1182,17 +1188,6 @@ func (r *ImageRef) Resize(scale float64, kernel Kernel) error {
return r.UnpremultiplyAlpha()
}

// ResizeWithVScale resizes the image with both horizontal as well as vertical scaling.
// The parameters are the scaling factors.
func (r *ImageRef) ResizeWithVScale(hScale, vScale float64, kernel Kernel) error {
out, err := vipsResizeWithVScale(r.image, hScale, vScale, kernel)
if err != nil {
return err
}
r.setImage(out)
return nil
}

// Thumbnail resizes the image to the given width and height.
// If crop is true the returned image size will be exactly the given height and width,
// otherwise the width and height will be within the given parameters.
Expand Down
16 changes: 2 additions & 14 deletions vips/resample.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
)

// https://libvips.github.io/libvips/API/current/libvips-resample.html#vips-resize
func vipsResize(in *C.VipsImage, scale float64, kernel Kernel) (*C.VipsImage, error) {
func vipsResizeWithVScale(in *C.VipsImage, hscale, vscale float64, kernel Kernel) (*C.VipsImage, error) {
incOpCounter("resize")
var out *C.VipsImage

Expand All @@ -27,19 +27,7 @@ func vipsResize(in *C.VipsImage, scale float64, kernel Kernel) (*C.VipsImage, er
kernel = KernelLanczos3
}

if err := C.resize_image(in, &out, C.double(scale), C.double(-1), C.int(kernel)); err != 0 {
return nil, handleImageError(out)
}

return out, nil
}

// https://libvips.github.io/libvips/API/current/libvips-resample.html#vips-resize
func vipsResizeWithVScale(in *C.VipsImage, scale, vscale float64, kernel Kernel) (*C.VipsImage, error) {
incOpCounter("resize")
var out *C.VipsImage

if err := C.resize_image(in, &out, C.double(scale), C.gdouble(vscale), C.int(kernel)); err != 0 {
if err := C.resize_image(in, &out, C.double(hscale), C.double(vscale), C.int(kernel)); err != 0 {
return nil, handleImageError(out)
}

Expand Down

0 comments on commit 5934011

Please sign in to comment.