From c2a502a611498182bec73f7a935712718d949923 Mon Sep 17 00:00:00 2001 From: Tomas Aparicio Date: Thu, 16 Apr 2015 02:24:44 +0200 Subject: [PATCH] fix(#31) --- resize.go | 28 ++++++++++++++-------------- resize_test.go | 2 +- vips.go | 3 ++- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/resize.go b/resize.go index 253c626..c348a57 100644 --- a/resize.go +++ b/resize.go @@ -48,6 +48,13 @@ func Resize(buf []byte, o Options) ([]byte, error) { shrink := int(math.Max(math.Floor(factor), 1)) residual := float64(shrink) / factor + // Calculate integral box shrink + windowSize := vipsWindowSize(o.Interpolator.String()) + if factor >= 2 && windowSize > 3 { + // Shrink less, affine more with interpolators that use at least 4x4 pixel window, e.g. bicubic + shrink = int(math.Max(float64(math.Floor(factor*3.0/windowSize)), 1)) + } + // Do not enlarge the output if the input width *or* height are already less than the required dimensions if o.Enlarge == false { if inWidth < o.Width && inHeight < o.Height { @@ -65,24 +72,16 @@ func Resize(buf []byte, o Options) ([]byte, error) { if err != nil { return nil, err } - if tmpImage != nil { - image = tmpImage - factor = math.Max(factor, 1.0) - shrink = int(math.Floor(factor)) - residual = float64(shrink) / factor - } - } - - // Calculate integral box shrink - windowSize := vipsWindowSize(o.Interpolator.String()) - if factor >= 2 && windowSize > 3 { - // Shrink less, affine more with interpolators that use at least 4x4 pixel window, e.g. bicubic - shrink = int(math.Max(float64(math.Floor(factor*3.0/windowSize)), 1)) + image = tmpImage + factor = math.Max(factor, 1.0) + shrink = int(math.Floor(factor)) + residual = float64(shrink) / factor } // Transform image if necessary shouldTransform := o.Width != inWidth || o.Height != inHeight || o.AreaWidth > 0 || o.AreaHeight > 0 if shouldTransform { + // Use vips_shrink with the integral reduction if shrink > 1 { image, residual, err = shrinkImage(image, o, residual, shrink) @@ -90,7 +89,8 @@ func Resize(buf []byte, o Options) ([]byte, error) { return nil, err } } - // Use vips_affine with the remaining float part + + // Affine with the remaining float part if residual != 0 { image, err = vipsAffine(image, residual, o.Interpolator) if err != nil { diff --git a/resize_test.go b/resize_test.go index cb8dfe3..131dc38 100644 --- a/resize_test.go +++ b/resize_test.go @@ -9,7 +9,7 @@ import ( func TestResize(t *testing.T) { options := Options{Width: 800, Height: 600} - buf, _ := Read("fixtures/test.jpg") + buf, _ := Read("../vips/fixtures/large.jpg") newImg, err := Resize(buf, options) if err != nil { diff --git a/vips.go b/vips.go index 53ae1f2..e675be8 100644 --- a/vips.go +++ b/vips.go @@ -229,6 +229,7 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) { err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) break default: + debug("Save JPEG options: Q: %s", o.Quality) err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) break } @@ -239,7 +240,7 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) { buf := C.GoBytes(ptr, C.int(length)) - // Cleanup + // Clean up C.g_free(C.gpointer(ptr)) C.vips_error_clear()