From 30bc7088be8b3a84c275ceffa8da41473f7ea7f2 Mon Sep 17 00:00:00 2001 From: "steve.3282" Date: Mon, 24 Feb 2025 16:37:19 +0900 Subject: [PATCH] add Min function --- vips/arithmetic.c | 4 ++++ vips/arithmetic.go | 13 +++++++++++++ vips/arithmetic.h | 1 + vips/image.go | 5 +++++ vips/image_test.go | 3 +++ 5 files changed, 26 insertions(+) diff --git a/vips/arithmetic.c b/vips/arithmetic.c index 7de1c608..8d1dfb33 100644 --- a/vips/arithmetic.c +++ b/vips/arithmetic.c @@ -81,3 +81,7 @@ int absOp(VipsImage *img, VipsImage **out) { int project(VipsImage *in, VipsImage **col, VipsImage **row) { return vips_project(in, col, row, NULL); } + +int minOp(VipsImage *in, double *out, int *x, int *y, int size) { + return vips_min(in, out, "x", x, "y", y, "size", size, NULL); +} diff --git a/vips/arithmetic.go b/vips/arithmetic.go index f2a104a8..a7967c7f 100644 --- a/vips/arithmetic.go +++ b/vips/arithmetic.go @@ -208,3 +208,16 @@ func vipsProject(in *C.VipsImage) (*C.VipsImage, *C.VipsImage, error) { } return col, row, nil } + +// https://www.libvips.org/API/current/libvips-arithmetic.html#vips-min +func vipsMin(in *C.VipsImage) (float64, int, int, error) { + incOpCounter("min") + var out C.double + var x, y C.int + + if err := C.minOp(in, &out, &x, &y, C.int(1)); err != 0 { + return 0, 0, 0, handleVipsError() + } + + return float64(out), int(x), int(y), nil +} diff --git a/vips/arithmetic.h b/vips/arithmetic.h index 96d36029..76f965cd 100644 --- a/vips/arithmetic.h +++ b/vips/arithmetic.h @@ -21,3 +21,4 @@ int hist_entropy(VipsImage *in, double *out); int subtract(VipsImage *in1, VipsImage *in2, VipsImage **out); int absOp(VipsImage *img, VipsImage **out); int project(VipsImage *in, VipsImage **col, VipsImage **row); +int minOp(VipsImage *in, double *out, int *x, int *y, int size); diff --git a/vips/image.go b/vips/image.go index 0dc4fcf5..09174157 100644 --- a/vips/image.go +++ b/vips/image.go @@ -1824,6 +1824,11 @@ func (r *ImageRef) Project() (*ImageRef, *ImageRef, error) { return newImageRef(col, r.format, r.originalFormat, nil), newImageRef(row, r.format, r.originalFormat, nil), nil } +// Min finds the minimum value in an image. +func (r *ImageRef) Min() (float64, int, int, error) { + return vipsMin(r.image) +} + // Rank does rank filtering on an image. A window of size width by height is passed over the image. // At each position, the pixels inside the window are sorted into ascending order and the pixel at position // index is output. index numbers from 0. diff --git a/vips/image_test.go b/vips/image_test.go index 6ac2a77f..6c6fd045 100644 --- a/vips/image_test.go +++ b/vips/image_test.go @@ -1212,6 +1212,9 @@ func TestImageRef_ArithmeticOperation(t *testing.T) { orgWidth := image.Width() orgHeight := image.Height() + _, _, _, err = image.Min() + require.NoError(t, err) + err = image2.Abs() require.NoError(t, err)