From 99abaf0752ebb7b0b8db01618eddefb086f6601e Mon Sep 17 00:00:00 2001 From: Chuck Neerdaels Date: Mon, 16 Nov 2015 15:46:08 -0800 Subject: [PATCH] Added interface and test for sharpen --- options.go | 10 ++++++++++ resize.go | 13 ++++++++++--- resize_test.go | 17 +++++++++++++++++ vips.go | 11 +++++++++++ vips.h | 9 +++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) diff --git a/options.go b/options.go index 8dcc2a9..fae5fd7 100644 --- a/options.go +++ b/options.go @@ -100,6 +100,15 @@ type GaussianBlur struct { MinAmpl float64 } +type Sharpen struct { + Radius int + X1 float64 + Y2 float64 + Y3 float64 + M1 float64 + M2 float64 +} + // Supported image transformation options type Options struct { Height int @@ -129,4 +138,5 @@ type Options struct { Interpolator Interpolator Interpretation Interpretation GaussianBlur GaussianBlur + Sharpen Sharpen } diff --git a/resize.go b/resize.go index f0ba8e3..563a787 100644 --- a/resize.go +++ b/resize.go @@ -149,7 +149,7 @@ func shouldTransformImage(o Options, inWidth, inHeight int) bool { } func shouldApplyEffects(o Options) bool { - return o.GaussianBlur.Sigma > 0 || o.GaussianBlur.MinAmpl > 0 + return o.GaussianBlur.Sigma > 0 || o.GaussianBlur.MinAmpl > 0 || o.Sharpen.Radius > 0 && o.Sharpen.Y2 > 0 || o.Sharpen.Y3 > 0 } func transformImage(image *C.VipsImage, o Options, shrink int, residual float64) (*C.VipsImage, error) { @@ -202,8 +202,15 @@ func applyEffects(image *C.VipsImage, o Options) (*C.VipsImage, error) { } } - debug("Effects: gaussSigma=%v, gaussMinAmpl=%v", - o.GaussianBlur.Sigma, o.GaussianBlur.MinAmpl) + if o.Sharpen.Radius > 0 && o.Sharpen.Y2 > 0 || o.Sharpen.Y3 > 0 { + image, err = vipsSharpen(image, o.Sharpen) + if err != nil { + return nil, err + } + } + + debug("Effects: gaussSigma=%v, gaussMinAmpl=%v, sharpenRadius=%v", + o.GaussianBlur.Sigma, o.GaussianBlur.MinAmpl, o.Sharpen.Radius) return image, nil } diff --git a/resize_test.go b/resize_test.go index c83ff39..d57852c 100644 --- a/resize_test.go +++ b/resize_test.go @@ -168,6 +168,23 @@ func TestGaussianBlur(t *testing.T) { Write("fixtures/test_gaussian.jpg", newImg) } +func TestSharpen(t *testing.T) { + options := Options{Width: 800, Height: 600, Sharpen: Sharpen{Radius: 1, X1: 1.5, Y2: 20, Y3: 50, M1: 1, M2: 2}} + buf, _ := Read("fixtures/test.jpg") + + newImg, err := Resize(buf, options) + if err != nil { + t.Errorf("Resize(imgData, %#v) error: %#v", options, err) + } + + size, _ := Size(newImg) + if size.Height != options.Height || size.Width != options.Width { + t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height) + } + + Write("fixtures/test_sharpen.jpg", newImg) +} + func TestConvert(t *testing.T) { width, height := 300, 240 formats := [3]ImageType{PNG, WEBP, JPEG} diff --git a/vips.go b/vips.go index 57bca95..a2555d4 100644 --- a/vips.go +++ b/vips.go @@ -473,3 +473,14 @@ func vipsGaussianBlur(image *C.VipsImage, o GaussianBlur) (*C.VipsImage, error) } return out, nil } + +func vipsSharpen(image *C.VipsImage, o Sharpen) (*C.VipsImage, error) { + var out *C.VipsImage + defer C.g_object_unref(C.gpointer(image)) + + err := C.vips_sharpen_bridge(image, &out, C.int(o.Radius), C.double(o.X1), C.double(o.Y2), C.double(o.Y3), C.double(o.M1), C.double(o.M2)) + if err != 0 { + return nil, catchVipsError() + } + return out, nil +} diff --git a/vips.h b/vips.h index aa97668..1eecdc3 100644 --- a/vips.h +++ b/vips.h @@ -341,3 +341,12 @@ vips_gaussblur_bridge(VipsImage *in, VipsImage **out, double sigma, double min_a return vips_gaussblur(in, out, sigma, NULL, "min_ampl", min_ampl, NULL); #endif } + +int +vips_sharpen_bridge(VipsImage *in, VipsImage **out, int radius, double x1, double y2, double y3, double m1, double m2) { +#if (VIPS_MAJOR_VERSION == 7 && VIPS_MINOR_VERSION < 41) + return vips_sharpen(in, out, radius, x1, y2, y3, m1, m2, NULL); +#else + return vips_sharpen(in, out, "radius", radius, "x1", x1, "y2", y2, "y3", y3, "m1", m1, "m2", m2, NULL); +#endif +}