From fcf4866b4b9bcf7442326e7cceb5fd4c59f7222b Mon Sep 17 00:00:00 2001 From: n0vad3v Date: Thu, 14 Nov 2024 16:07:00 +0800 Subject: [PATCH] port MaxPos and MinPos --- vips/arithmetic.c | 9 ++++----- vips/arithmetic.go | 18 ++++++++++-------- vips/arithmetic.h | 4 ++-- vips/image.go | 6 ++++-- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/vips/arithmetic.c b/vips/arithmetic.c index 4e10ae2e..6356d814 100644 --- a/vips/arithmetic.c +++ b/vips/arithmetic.c @@ -28,14 +28,13 @@ int average(VipsImage *in, double *out) { return vips_avg(in, out, NULL); } -int maxpos(VipsImage *in, double *out) { - return vips_max(in, out, NULL); +int maxpos(VipsImage *in, double *out, int *x, int *y) { + return vips_max(in, out, "x", x, "y", y, NULL); } -int minpos(VipsImage *in, double *out) { - return vips_min(in, out, NULL); +int minpos(VipsImage *in, double *out, int *x, int *y) { + return vips_min(in, out, "x", x, "y", y, NULL); } - int find_trim(VipsImage *in, int *left, int *top, int *width, int *height, double threshold, double r, double g, double b) { diff --git a/vips/arithmetic.go b/vips/arithmetic.go index eb7ee155..98cbd7d4 100644 --- a/vips/arithmetic.go +++ b/vips/arithmetic.go @@ -152,27 +152,29 @@ func vipsHistFindNdim(in *C.VipsImage, bins int) (*C.VipsImage, error) { } // https://www.libvips.org/API/current/libvips-arithmetic.html#vips-max -func vipsMax(in *C.VipsImage) (float64, error) { +func vipsMax(in *C.VipsImage) (int, int, error) { incOpCounter("max") var out C.double + var x, y C.int - if err := C.maxpos(in, &out); err != 0 { - return 0, handleVipsError() + if err := C.maxpos(in, &out, &x, &y); err != 0 { + return -1, -1, handleVipsError() } - return float64(out), nil + return int(x), int(y), nil } // https://www.libvips.org/API/current/libvips-arithmetic.html#vips-min -func vipsMin(in *C.VipsImage) (float64, error) { +func vipsMin(in *C.VipsImage) (int, int, error) { incOpCounter("min") var out C.double + var x, y C.int - if err := C.minpos(in, &out); err != 0 { - return 0, handleVipsError() + if err := C.maxpos(in, &out, &x, &y); err != 0 { + return -1, -1, handleVipsError() } - return float64(out), nil + return int(x), int(y), nil } // https://www.libvips.org/API/current/libvips-histogram.html#vips-hist-norm diff --git a/vips/arithmetic.h b/vips/arithmetic.h index 62bea788..eb484a41 100644 --- a/vips/arithmetic.h +++ b/vips/arithmetic.h @@ -14,8 +14,8 @@ int find_trim(VipsImage *in, int *left, int *top, int *width, int *height, double threshold, double r, double g, double b); int getpoint(VipsImage *in, double **vector, int n, int x, int y); int stats(VipsImage *in, VipsImage **out); -int maxpos(VipsImage *in, double *out); -int minpos(VipsImage *in, double *out); +int maxpos(VipsImage *in, double *out, int *x, int *y); +int minpos(VipsImage *in, double *out, int *x, int *y); int hist_find(VipsImage *in, VipsImage **out); int hist_find_ndim(VipsImage *in, VipsImage **out, int bins); int hist_cum(VipsImage *in, VipsImage **out); diff --git a/vips/image.go b/vips/image.go index 956ba470..d02a9daa 100644 --- a/vips/image.go +++ b/vips/image.go @@ -1730,11 +1730,13 @@ func (r *ImageRef) HistogramFindNdim(bins int) error { return nil } -func (r *ImageRef) MaxPos() (float64, error) { +// This operation finds the maximum value in an image. +func (r *ImageRef) MaxPos() (x, y int, err error) { return vipsMax(r.image) } -func (r *ImageRef) MinPos() (float64, error) { +// This operation finds the maximum value in an image. +func (r *ImageRef) MinPos() (x, y int, err error) { return vipsMin(r.image) }