From 317979356658010b507e82a1898324efdb799f5d Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 20 Nov 2026 13:04:50 +0200 Subject: [PATCH 1/3] Special-case min/max filters to not use quickselect Co-authored-by: Andrew Murray --- src/libImaging/RankFilter.c | 58 +++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/src/libImaging/RankFilter.c b/src/libImaging/RankFilter.c index 8757e0fc0a6..d7b51ecaa2d 100644 --- a/src/libImaging/RankFilter.c +++ b/src/libImaging/RankFilter.c @@ -49,13 +49,42 @@ } \ } -// For RANK_BODY, +#define RANK_MIN(a, b) ((a) < (b) ? (a) : (b)) +#define RANK_MAX(a, b) ((a) < (b) ? (b) : (a)) + +#define RANK_IDENTITY(v) (v) +#define RANK_BSWAP16(v) ((UINT16)(((v) >> 8) | ((v) << 8))) + +// For both MINMAX_BODY and RANK_BODY, // `pixel` converts a stored value to one that compares correctly // in the native byte order, and converts it back. -// It is `RANK_IDENTITY` except when dealing with 16bpc images +// They are `RANK_IDENTITY` except when dealing with 16bpc images // that are not in native byte order. -#define RANK_IDENTITY(v) (v) -#define RANK_BSWAP16(v) ((UINT16)(((v) >> 8) | ((v) << 8))) + +#define MINMAX_BODY(type, op, pixel) \ + do { \ + ImagingSectionEnter(&cookie); \ + for (int y = 0; y < ysize; y++) { \ + type *out = (type *)imOut->image[y]; \ + const type *in0 = (const type *)im->image[y]; \ + for (int x = 0; x < xsize; x++) { \ + out[x] = pixel(in0[x]); \ + } \ + for (int s = 0; s < size; s++) { \ + const type *row = (const type *)im->image[y + s]; \ + for (int s2 = (s == 0); s2 < size; s2++) { \ + const type *in = row + s2; \ + for (int x = 0; x < xsize; x++) { \ + out[x] = op(out[x], pixel(in[x])); \ + } \ + } \ + } \ + for (int x = 0; x < xsize; x++) { \ + out[x] = pixel(out[x]); \ + } \ + } \ + ImagingSectionLeave(&cookie); \ + } while (0) Imaging ImagingRankFilter(Imaging im, int size, int rank) { @@ -118,6 +147,17 @@ ImagingRankFilter(Imaging im, int size, int rank) { free(buf); \ } while (0) +#define RANK_DISPATCH(type, pixel) \ + do { \ + if (rank == 0) { \ + MINMAX_BODY(type, RANK_MIN, pixel); \ + } else if (rank == size2 - 1) { \ + MINMAX_BODY(type, RANK_MAX, pixel); \ + } else { \ + RANK_BODY(type, pixel); \ + } \ + } while (0) + if (im->type == IMAGING_TYPE_I16) { // Compare in native byte order if ( @@ -127,16 +167,16 @@ ImagingRankFilter(Imaging im, int size, int rank) { im->mode == IMAGING_MODE_I_16B #endif ) { - RANK_BODY(UINT16, RANK_BSWAP16); + RANK_DISPATCH(UINT16, RANK_BSWAP16); } else { - RANK_BODY(UINT16, RANK_IDENTITY); + RANK_DISPATCH(UINT16, RANK_IDENTITY); } } else if (im->image8) { - RANK_BODY(UINT8, RANK_IDENTITY); + RANK_DISPATCH(UINT8, RANK_IDENTITY); } else if (im->type == IMAGING_TYPE_INT32) { - RANK_BODY(INT32, RANK_IDENTITY); + RANK_DISPATCH(INT32, RANK_IDENTITY); } else if (im->type == IMAGING_TYPE_FLOAT32) { - RANK_BODY(FLOAT32, RANK_IDENTITY); + RANK_DISPATCH(FLOAT32, RANK_IDENTITY); } else { /* safety net (we shouldn't end up here) */ ImagingDelete(imOut); From 0d033007f77becf6b468e599b2c6f8f71f81a559 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 20 Nov 2026 16:10:28 +0200 Subject: [PATCH 2/3] Move RANK_BODY out of ImagingRankFilter for symmetry --- src/libImaging/RankFilter.c | 61 +++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/src/libImaging/RankFilter.c b/src/libImaging/RankFilter.c index d7b51ecaa2d..430544091c6 100644 --- a/src/libImaging/RankFilter.c +++ b/src/libImaging/RankFilter.c @@ -55,12 +55,16 @@ #define RANK_IDENTITY(v) (v) #define RANK_BSWAP16(v) ((UINT16)(((v) >> 8) | ((v) << 8))) +// MINMAX_BODY and RANK_BODY are only meant to be expanded +// inside ImagingRankFilter as they use its locals and labels. +// // For both MINMAX_BODY and RANK_BODY, // `pixel` converts a stored value to one that compares correctly // in the native byte order, and converts it back. // They are `RANK_IDENTITY` except when dealing with 16bpc images // that are not in native byte order. +// Minimum or maximum (per `op`) over each size x size window. #define MINMAX_BODY(type, op, pixel) \ do { \ ImagingSectionEnter(&cookie); \ @@ -86,6 +90,36 @@ ImagingSectionLeave(&cookie); \ } while (0) +// General rank: gather size x size windows into `buf`, then +// select the rank-th smallest value with RANK_INNER_BODY. +// malloc check ok: ImagingRankFilter rejects any size for which +// size * size * sizeof(FLOAT32) would overflow. +// restrict safe: buf is a private allocation, imOut is a fresh allocation. +#define RANK_BODY(type, pixel) \ + do { \ + type *restrict buf = malloc(size2 * sizeof(type)); \ + if (!buf) { \ + goto nomemory; \ + } \ + ImagingSectionEnter(&cookie); \ + for (int y = 0; y < ysize; y++) { \ + type *restrict out = (type *)imOut->image[y]; \ + for (int x = 0; x < xsize; x++) { \ + type *restrict p = buf; \ + for (int s = 0; s < size; s++) { \ + const type *row = (const type *)im->image[y + s] + x; \ + for (int s2 = 0; s2 < size; s2++) { \ + *p++ = pixel(row[s2]); \ + } \ + } \ + RANK_INNER_BODY(type, buf, size2, rank); \ + out[x] = pixel(buf[rank]); \ + } \ + } \ + ImagingSectionLeave(&cookie); \ + free(buf); \ + } while (0) + Imaging ImagingRankFilter(Imaging im, int size, int rank) { Imaging imOut = NULL; @@ -120,33 +154,6 @@ ImagingRankFilter(Imaging im, int size, int rank) { return NULL; } - /* malloc check ok, checked above */ - // restrict safe: buf is a private allocation, imOut is a fresh allocation. -#define RANK_BODY(type, pixel) \ - do { \ - type *restrict buf = malloc(size2 * sizeof(type)); \ - if (!buf) { \ - goto nomemory; \ - } \ - ImagingSectionEnter(&cookie); \ - for (int y = 0; y < ysize; y++) { \ - type *restrict out = (type *)imOut->image[y]; \ - for (int x = 0; x < xsize; x++) { \ - type *restrict p = buf; \ - for (int s = 0; s < size; s++) { \ - const type *row = (const type *)im->image[y + s] + x; \ - for (int s2 = 0; s2 < size; s2++) { \ - *p++ = pixel(row[s2]); \ - } \ - } \ - RANK_INNER_BODY(type, buf, size2, rank); \ - out[x] = pixel(buf[rank]); \ - } \ - } \ - ImagingSectionLeave(&cookie); \ - free(buf); \ - } while (0) - #define RANK_DISPATCH(type, pixel) \ do { \ if (rank == 0) { \ From 4adb76fdc163ad4809f483ecace411bda0ef98a1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 21 Nov 2026 10:08:12 +1100 Subject: [PATCH 3/3] Update comment --- src/libImaging/RankFilter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libImaging/RankFilter.c b/src/libImaging/RankFilter.c index 430544091c6..473130ed758 100644 --- a/src/libImaging/RankFilter.c +++ b/src/libImaging/RankFilter.c @@ -134,7 +134,7 @@ ImagingRankFilter(Imaging im, int size, int rank) { return (Imaging)ImagingError_ValueError("bad filter size"); } - /* malloc check ok, for overflow in the define below */ + /* malloc check ok, for overflow in RANK_BODY */ if (size > INT_MAX / (size * (int)sizeof(FLOAT32))) { return (Imaging)ImagingError_ValueError("filter size too large"); }