From 58bc9312b6e2f7a4ccc1d2fda15aafe2ad4f5703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martti=20Lepp=C3=A4nen?= Date: Thu, 9 Nov 2017 15:32:58 -0500 Subject: [PATCH 1/2] Add the option to use background and threshold options on trim --- image_test.go | 25 +++++++++++++++++++++++++ options.go | 1 + resizer.go | 2 +- vips.go | 7 +++++-- vips.h | 12 ++++++++++-- 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/image_test.go b/image_test.go index 37bb4bd..4d72b68 100644 --- a/image_test.go +++ b/image_test.go @@ -495,6 +495,31 @@ func TestImageTrim(t *testing.T) { Write("testdata/transparent_trim.png", buf) } +func TestImageTrimParameters(t *testing.T) { + + if !(VipsMajorVersion >= 8 && VipsMinorVersion >= 6) { + t.Skipf("Skipping this test, libvips doesn't meet version requirement %s >= 8.6", VipsVersion) + } + + i := initImage("test.png") + options := Options{ + Trim: true, + Background: Color{0.0, 0.0, 0.0}, + Threshold: 10.0, + } + buf, err := i.Process(options) + if err != nil { + t.Errorf("Cannot process the image: %#v", err) + } + + err = assertSize(buf, 400, 257) + if err != nil { + t.Errorf("The image wasn't trimmed.") + } + + Write("testdata/transparent_trim.png", buf) +} + func TestImageLength(t *testing.T) { i := initImage("test.jpg") diff --git a/options.go b/options.go index 609de60..0e0dc65 100644 --- a/options.go +++ b/options.go @@ -217,5 +217,6 @@ type Options struct { Interpretation Interpretation GaussianBlur GaussianBlur Sharpen Sharpen + Threshold float32 OutputICC string } diff --git a/resizer.go b/resizer.go index 4b78b47..d1f6453 100644 --- a/resizer.go +++ b/resizer.go @@ -273,7 +273,7 @@ func extractOrEmbedImage(image *C.VipsImage, o Options) (*C.VipsImage, error) { image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend, o.Background) break case o.Trim: - left, top, width, height, err := vipsTrim(image) + left, top, width, height, err := vipsTrim(image, o.Background, o.Threshold) if err == nil { image, err = vipsExtract(image, left, top, width, height) } diff --git a/vips.go b/vips.go index e46b03f..0d14de5 100644 --- a/vips.go +++ b/vips.go @@ -503,7 +503,7 @@ func vipsSmartCrop(image *C.VipsImage, width, height int) (*C.VipsImage, error) return buf, nil } -func vipsTrim(image *C.VipsImage) (int, int, int, int, error) { +func vipsTrim(image *C.VipsImage, background Color, threshold float32) (int, int, int, int, error) { defer C.g_object_unref(C.gpointer(image)) top := C.int(0) @@ -511,7 +511,10 @@ func vipsTrim(image *C.VipsImage) (int, int, int, int, error) { width := C.int(0) height := C.int(0) - err := C.vips_find_trim_bridge(image, &top, &left, &width, &height) + err := C.vips_find_trim_bridge(image, + &top, &left, &width, &height, + C.double(background.R), C.double(background.G), C.double(background.B), + C.double(threshold)) if err != 0 { return 0, 0, 0, 0, catchVipsError() } diff --git a/vips.h b/vips.h index 569086a..229a0b3 100644 --- a/vips.h +++ b/vips.h @@ -545,9 +545,17 @@ vips_smartcrop_bridge(VipsImage *in, VipsImage **out, int width, int height) { #endif } -int vips_find_trim_bridge(VipsImage *in, int *top, int *left, int *width, int *height) { +int vips_find_trim_bridge(VipsImage *in, int *top, int *left, int *width, int *height, double r, double g, double b, double threshold) { #if (VIPS_MAJOR_VERSION >= 8 && VIPS_MINOR_VERSION >= 6) - return vips_find_trim(in, top, left, width, height, NULL); + if (vips_is_16bit(in->Type)) { + r = 65535 * r / 255; + g = 65535 * g / 255; + b = 65535 * b / 255; + } + + double background[3] = {r, g, b}; + VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3); + return vips_find_trim(in, top, left, width, height, "background", vipsBackground, "threshold", threshold, NULL); #else return 0; #endif From 700ff34f6e3991a50fbfdbde224a60d6e3196a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martti=20Lepp=C3=A4nen?= Date: Thu, 9 Nov 2017 15:43:59 -0500 Subject: [PATCH 2/2] Set the test file to write its own file --- image_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/image_test.go b/image_test.go index 4d72b68..5af0431 100644 --- a/image_test.go +++ b/image_test.go @@ -517,7 +517,7 @@ func TestImageTrimParameters(t *testing.T) { t.Errorf("The image wasn't trimmed.") } - Write("testdata/transparent_trim.png", buf) + Write("testdata/parameter_trim.png", buf) } func TestImageLength(t *testing.T) {