From 3b9b619d4254e88e908cd864d4bc546c12c7ae21 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 22 Sep 2026 22:31:15 +0300 Subject: [PATCH 1/5] Make ImagingExpand work correctly for I16 images, apply hoist optimizations --- Tests/test_image_filter.py | 18 +++++++++++ src/libImaging/Filter.c | 65 ++++++++++++++++++++------------------ 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index c85ddeaaf88..d43baf599ae 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -209,6 +209,24 @@ def test_rankfilter_overflow() -> None: im.filter(rankfilter) +@pytest.mark.parametrize( + "mode, fill", + ( + ("L", 0x12), + ("I", 0x12345678), + ("I;16", 0x1234), + ("I;16B", 0x1234), + ("F", 1.5), + ("RGB", (0x12, 0x34, 0x56)), + ), +) +@pytest.mark.parametrize("margin", (0, 1, 3)) +def test_expand(mode: str, fill: float | tuple[int, ...], margin: int) -> None: + im = Image.new(mode, (5, 4), fill) + out = im._new(im.im.expand(margin)) # As done internally by RankFilter + assert_image_equal(out, Image.new(mode, (5 + 2 * margin, 4 + 2 * margin), fill)) + + @pytest.mark.parametrize("mode", ("P", "PA")) def test_builtinfilter_palette(mode: str) -> None: builtin_filter = ImageFilter.BuiltinFilter() diff --git a/src/libImaging/Filter.c b/src/libImaging/Filter.c index 185d7851979..c0c3e10e24d 100644 --- a/src/libImaging/Filter.c +++ b/src/libImaging/Filter.c @@ -72,10 +72,10 @@ ImagingExpand(Imaging imIn, int margin) { return (Imaging)ImagingError_ValueError("filter size too large"); } - int xsize = imIn->xsize + 2 * margin; - int ysize = imIn->ysize + 2 * margin; + int out_xsize = imIn->xsize + 2 * margin; + int out_ysize = imIn->ysize + 2 * margin; if (imIn->xsize == 0 || imIn->ysize == 0) { - imOut = ImagingNew(imIn->mode, xsize, ysize); + imOut = ImagingNew(imIn->mode, out_xsize, out_ysize); if (!imOut) { return NULL; } @@ -83,45 +83,50 @@ ImagingExpand(Imaging imIn, int margin) { return imOut; } - imOut = ImagingNewDirty(imIn->mode, xsize, ysize); + imOut = ImagingNewDirty(imIn->mode, out_xsize, out_ysize); if (!imOut) { return NULL; } -#define EXPAND_LINE(type, image, yin, yout) \ - { \ - for (x = 0; x < margin; x++) { \ - imOut->image[yout][x] = imIn->image[yin][0]; \ - } \ - for (x = 0; x < imIn->xsize; x++) { \ - imOut->image[yout][x + margin] = imIn->image[yin][x]; \ - } \ - for (x = 0; x < margin; x++) { \ - imOut->image[yout][margin + imIn->xsize + x] = \ - imIn->image[yin][imIn->xsize - 1]; \ - } \ + int xsize = imIn->xsize, ysize = imIn->ysize; + +#define EXPAND_LINE(type, yin, yout) \ + { \ + const type *in = (const type *)imIn->image[yin]; \ + type *out = (type *)imOut->image[yout]; \ + for (x = 0; x < margin; x++) { \ + out[x] = in[0]; \ + } \ + for (x = 0; x < xsize; x++) { \ + out[x + margin] = in[x]; \ + } \ + for (x = 0; x < margin; x++) { \ + out[margin + xsize + x] = in[xsize - 1]; \ + } \ } -#define EXPAND(type, image) \ - { \ - for (y = 0; y < margin; y++) { \ - EXPAND_LINE(type, image, 0, y); \ - } \ - for (y = 0; y < imIn->ysize; y++) { \ - EXPAND_LINE(type, image, y, y + margin); \ - } \ - for (y = 0; y < margin; y++) { \ - EXPAND_LINE(type, image, imIn->ysize - 1, margin + imIn->ysize + y); \ - } \ +#define EXPAND(type) \ + { \ + for (y = 0; y < margin; y++) { \ + EXPAND_LINE(type, 0, y); \ + } \ + for (y = 0; y < ysize; y++) { \ + EXPAND_LINE(type, y, y + margin); \ + } \ + for (y = 0; y < margin; y++) { \ + EXPAND_LINE(type, ysize - 1, margin + ysize + y); \ + } \ } ImagingSectionEnter(&cookie); MUTEX_LOCK(&imIn->mutex); - if (imIn->image8) { - EXPAND(UINT8, image8); + if (imIn->type == IMAGING_TYPE_I16) { + EXPAND(UINT16); + } else if (imIn->image8) { + EXPAND(UINT8); } else { - EXPAND(INT32, image32); + EXPAND(INT32); } MUTEX_UNLOCK(&imIn->mutex); ImagingSectionLeave(&cookie); From 9dc45c9b6909a8ba8d359ea6283731fc2453b50c Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 17 Nov 2026 10:42:30 +1100 Subject: [PATCH 2/5] Use xsize and ysize earlier --- src/libImaging/Filter.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libImaging/Filter.c b/src/libImaging/Filter.c index c0c3e10e24d..f03eeed7297 100644 --- a/src/libImaging/Filter.c +++ b/src/libImaging/Filter.c @@ -72,9 +72,10 @@ ImagingExpand(Imaging imIn, int margin) { return (Imaging)ImagingError_ValueError("filter size too large"); } - int out_xsize = imIn->xsize + 2 * margin; - int out_ysize = imIn->ysize + 2 * margin; - if (imIn->xsize == 0 || imIn->ysize == 0) { + int xsize = imIn->xsize, ysize = imIn->ysize; + int out_xsize = xsize + 2 * margin; + int out_ysize = ysize + 2 * margin; + if (xsize == 0 || ysize == 0) { imOut = ImagingNew(imIn->mode, out_xsize, out_ysize); if (!imOut) { return NULL; @@ -88,8 +89,6 @@ ImagingExpand(Imaging imIn, int margin) { return NULL; } - int xsize = imIn->xsize, ysize = imIn->ysize; - #define EXPAND_LINE(type, yin, yout) \ { \ const type *in = (const type *)imIn->image[yin]; \ From e19def590f0d09c8a128eab7ccaf20bd03ac91f3 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 22 Sep 2026 21:54:59 +0300 Subject: [PATCH 3/5] Clean up and speed up RankFilter This removes the `IMAGING_PIXEL_(type)` macros from `Imaging.h` too; they were only used in `RankFilter.c` and in one place in `_imaging.c`. --- src/_imaging.c | 4 +- src/libImaging/Imaging.h | 4 -- src/libImaging/RankFilter.c | 135 +++++++++++++++++++----------------- 3 files changed, 74 insertions(+), 69 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index 28eae224222..ae0da2fc63e 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1951,7 +1951,7 @@ _putdata_impl(ImagingObject *self, PyObject *args) { for (i = x = y = 0; i < n; i++) { double value; set_value_to_item(seq, i); - IMAGING_PIXEL_INT32(image, x, y) = (INT32)(value * scale + offset); + image->image32[y][x] = (INT32)(value * scale + offset); if (++x >= (int)image->xsize) { x = 0, y++; } @@ -1961,7 +1961,7 @@ _putdata_impl(ImagingObject *self, PyObject *args) { for (i = x = y = 0; i < n; i++) { double value; set_value_to_item(seq, i); - IMAGING_PIXEL_FLOAT32(image, x, y) = + ((FLOAT32 *)image->image32[y])[x] = (FLOAT32)(value * scale + offset); if (++x >= (int)image->xsize) { x = 0, y++; diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h index ac5dbee989f..1d053828b5d 100644 --- a/src/libImaging/Imaging.h +++ b/src/libImaging/Imaging.h @@ -132,10 +132,6 @@ struct ImagingMemoryInstance { #define IMAGING_PIXEL_CMYK(im, x, y) ((im)->image[(y)][(x) * 4]) #define IMAGING_PIXEL_YCbCr(im, x, y) ((im)->image[(y)][(x) * 4]) -#define IMAGING_PIXEL_UINT8(im, x, y) ((im)->image8[(y)][(x)]) -#define IMAGING_PIXEL_INT32(im, x, y) ((im)->image32[(y)][(x)]) -#define IMAGING_PIXEL_FLOAT32(im, x, y) (((FLOAT32 *)(im)->image32[y])[x]) - struct ImagingAccessInstance { ModeID mode; void (*get_pixel)(Imaging im, int x, int y, void *pixel); diff --git a/src/libImaging/RankFilter.c b/src/libImaging/RankFilter.c index 5c0a09d877c..7f4ddd86f03 100644 --- a/src/libImaging/RankFilter.c +++ b/src/libImaging/RankFilter.c @@ -17,53 +17,59 @@ /* Fast rank algorithm (due to Wirth), based on public domain code by Nicolas Devillard, available at http://ndevilla.free.fr */ -#define SWAP(type, a, b) \ - { \ - register type t = (a); \ - (a) = (b); \ - (b) = t; \ - } +#define RANK_INNER_BODY(type) \ + int i, j, l, m; \ + type x; \ + l = 0; \ + m = n - 1; \ + while (l < m) { \ + x = a[k]; \ + i = l; \ + j = m; \ + do { \ + while (a[i] < x) { \ + i++; \ + } \ + while (x < a[j]) { \ + j--; \ + } \ + if (i <= j) { \ + type t = a[i]; \ + a[i] = a[j]; \ + a[j] = t; \ + i++; \ + j--; \ + } \ + } while (i <= j); \ + if (j < k) { \ + l = i; \ + } \ + if (k < i) { \ + m = j; \ + } \ + } \ + return a[k] + +static UINT8 +RankUINT8(UINT8 a[], int n, int k) { + RANK_INNER_BODY(UINT8); +} -#define MakeRankFunction(type) \ - static type Rank##type(type a[], int n, int k) { \ - register int i, j, l, m; \ - register type x; \ - l = 0; \ - m = n - 1; \ - while (l < m) { \ - x = a[k]; \ - i = l; \ - j = m; \ - do { \ - while (a[i] < x) { \ - i++; \ - } \ - while (x < a[j]) { \ - j--; \ - } \ - if (i <= j) { \ - SWAP(type, a[i], a[j]); \ - i++; \ - j--; \ - } \ - } while (i <= j); \ - if (j < k) { \ - l = i; \ - } \ - if (k < i) { \ - m = j; \ - } \ - } \ - return a[k]; \ - } +static INT32 +RankINT32(INT32 a[], int n, int k) { + RANK_INNER_BODY(INT32); +} -MakeRankFunction(UINT8) MakeRankFunction(INT32) MakeRankFunction(FLOAT32) +static FLOAT32 +RankFLOAT32(FLOAT32 a[], int n, int k) { + RANK_INNER_BODY(FLOAT32); +} - Imaging ImagingRankFilter(Imaging im, int size, int rank) { +Imaging +ImagingRankFilter(Imaging im, int size, int rank) { Imaging imOut = NULL; ImagingSectionCookie cookie; - int x, y; - int i, margin, size2; + int margin, size2; if (!im || im->bands != 1 || im->type == IMAGING_TYPE_I16) { return (Imaging)ImagingError_ModeError(); @@ -90,29 +96,32 @@ MakeRankFunction(UINT8) MakeRankFunction(INT32) MakeRankFunction(FLOAT32) if (!imOut) { return NULL; } + int xsize = imOut->xsize, ysize = imOut->ysize; /* malloc check ok, checked above */ -#define RANK_BODY(type) \ - do { \ - type *buf = malloc(size2 * sizeof(type)); \ - if (!buf) { \ - goto nomemory; \ - } \ - ImagingSectionEnter(&cookie); \ - for (y = 0; y < imOut->ysize; y++) { \ - for (x = 0; x < imOut->xsize; x++) { \ - for (i = 0; i < size; i++) { \ - memcpy( \ - buf + i * size, \ - &IMAGING_PIXEL_##type(im, x, y + i), \ - size * sizeof(type) \ - ); \ - } \ - IMAGING_PIXEL_##type(imOut, x, y) = Rank##type(buf, size2, rank); \ - } \ - } \ - ImagingSectionLeave(&cookie); \ - free(buf); \ + // restrict safe: buf is a private allocation, imOut is a fresh allocation. +#define RANK_BODY(type) \ + 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 i = 0; i < size; i++) { \ + const type *row = (const type *)im->image[y + i] + x; \ + for (int k = 0; k < size; k++) { \ + *p++ = row[k]; \ + } \ + } \ + out[x] = Rank##type(buf, size2, rank); \ + } \ + } \ + ImagingSectionLeave(&cookie); \ + free(buf); \ } while (0) if (im->image8) { From 315249c523b60e45ff21f545256eb233f0260ab2 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 11 Nov 2026 10:51:57 +0200 Subject: [PATCH 4/5] Support I;16* modes in RankFilter --- Tests/test_image_filter.py | 26 ++++++++ docs/releasenotes/13.1.0.rst | 7 ++ src/libImaging/RankFilter.c | 120 ++++++++++++++++++----------------- 3 files changed, 96 insertions(+), 57 deletions(-) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index d43baf599ae..c84fea0d4a0 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -119,6 +119,8 @@ def test_modefilter( ("L", (0, 4, 8)), ("RGB", ((0, 0, 0), (4, 0, 0), (8, 0, 0))), ("I", (0, 4, 8)), + ("I;16", (0, 4, 8)), + ("I;16B", (0, 4, 8)), ("F", (0.0, 4.0, 8.0)), ), ) @@ -141,6 +143,30 @@ def test_rankfilter( assert (minimum, med, maximum) == expected +@pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N")) +@pytest.mark.parametrize( + "filter", + ( + ImageFilter.MinFilter(3), + ImageFilter.MedianFilter(3), + ImageFilter.MaxFilter(3), + ImageFilter.RankFilter(5, 7), + ), +) +def test_rankfilter_i16(mode: str, filter: ImageFilter.RankFilter) -> None: + # Arrange values whose high and low bytes order differently, + # so comparing in the wrong byte order would give wrong results. + data = [(i * 0x1F3D) & 0xFFFF for i in range(10 * 10)] + im = Image.new(mode, (10, 10)) + im.putdata(data) + reference = Image.new("I", (10, 10)) # Reference image in I32 mode. + reference.putdata(data) + + out = im.filter(filter) + assert out.mode == mode + assert out.get_flattened_data() == reference.filter(filter).get_flattened_data() + + @pytest.mark.parametrize( "filter", (ImageFilter.MinFilter, ImageFilter.MedianFilter, ImageFilter.MaxFilter) ) diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 6b0f1292020..94006519ce2 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -116,6 +116,13 @@ Blurring I;16 images :py:class:`~PIL.ImageFilter.GaussianBlur` and :py:class:`~PIL.ImageFilter.BoxBlur` now support I;16, I;16L, I;16B and I;16N images. +Rank filters for I;16 images +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +:py:class:`~PIL.ImageFilter.RankFilter`, :py:class:`~PIL.ImageFilter.MedianFilter`, +:py:class:`~PIL.ImageFilter.MinFilter` and :py:class:`~PIL.ImageFilter.MaxFilter` +now support I;16, I;16L, I;16B and I;16N images. + Respect Python build-tree configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/libImaging/RankFilter.c b/src/libImaging/RankFilter.c index 7f4ddd86f03..3f52dfcc33e 100644 --- a/src/libImaging/RankFilter.c +++ b/src/libImaging/RankFilter.c @@ -17,53 +17,45 @@ /* Fast rank algorithm (due to Wirth), based on public domain code by Nicolas Devillard, available at http://ndevilla.free.fr */ -#define RANK_INNER_BODY(type) \ - int i, j, l, m; \ - type x; \ - l = 0; \ - m = n - 1; \ - while (l < m) { \ - x = a[k]; \ - i = l; \ - j = m; \ - do { \ - while (a[i] < x) { \ - i++; \ - } \ - while (x < a[j]) { \ - j--; \ - } \ - if (i <= j) { \ - type t = a[i]; \ - a[i] = a[j]; \ - a[j] = t; \ - i++; \ - j--; \ - } \ - } while (i <= j); \ - if (j < k) { \ - l = i; \ - } \ - if (k < i) { \ - m = j; \ - } \ - } \ - return a[k] - -static UINT8 -RankUINT8(UINT8 a[], int n, int k) { - RANK_INNER_BODY(UINT8); -} - -static INT32 -RankINT32(INT32 a[], int n, int k) { - RANK_INNER_BODY(INT32); -} +#define RANK_INNER_BODY(type, a, n, k) \ + int i, j, l, m; \ + type t; \ + l = 0; \ + m = n - 1; \ + while (l < m) { \ + t = a[k]; \ + i = l; \ + j = m; \ + do { \ + while (a[i] < t) { \ + i++; \ + } \ + while (t < a[j]) { \ + j--; \ + } \ + if (i <= j) { \ + type t2 = a[i]; \ + a[i] = a[j]; \ + a[j] = t2; \ + i++; \ + j--; \ + } \ + } while (i <= j); \ + if (j < k) { \ + l = i; \ + } \ + if (k < i) { \ + m = j; \ + } \ + } -static FLOAT32 -RankFLOAT32(FLOAT32 a[], int n, int k) { - RANK_INNER_BODY(FLOAT32); -} +// For RANK_BODY, +// `load` converts a stored value to one that compares correctly +// in the native byte order, and `store` converts it back. +// 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))) Imaging ImagingRankFilter(Imaging im, int size, int rank) { @@ -71,7 +63,7 @@ ImagingRankFilter(Imaging im, int size, int rank) { ImagingSectionCookie cookie; int margin, size2; - if (!im || im->bands != 1 || im->type == IMAGING_TYPE_I16) { + if (!im || im->bands != 1) { return (Imaging)ImagingError_ModeError(); } @@ -100,7 +92,7 @@ ImagingRankFilter(Imaging im, int size, int rank) { /* malloc check ok, checked above */ // restrict safe: buf is a private allocation, imOut is a fresh allocation. -#define RANK_BODY(type) \ +#define RANK_BODY(type, load, store) \ do { \ type *restrict buf = malloc(size2 * sizeof(type)); \ if (!buf) { \ @@ -111,25 +103,39 @@ ImagingRankFilter(Imaging im, int size, int rank) { type *restrict out = (type *)imOut->image[y]; \ for (int x = 0; x < xsize; x++) { \ type *restrict p = buf; \ - for (int i = 0; i < size; i++) { \ - const type *row = (const type *)im->image[y + i] + x; \ - for (int k = 0; k < size; k++) { \ - *p++ = row[k]; \ + 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++ = load(row[s2]); \ } \ } \ - out[x] = Rank##type(buf, size2, rank); \ + RANK_INNER_BODY(type, buf, size2, rank); \ + out[x] = store(buf[rank]); \ } \ } \ ImagingSectionLeave(&cookie); \ free(buf); \ } while (0) - if (im->image8) { - RANK_BODY(UINT8); + if (im->type == IMAGING_TYPE_I16) { + int bigendian = isImageI16BigEndian(im); + // Compare in native byte order +#ifdef WORDS_BIGENDIAN + int swap = !bigendian; +#else + int swap = bigendian; +#endif + if (swap) { + RANK_BODY(UINT16, RANK_BSWAP16, RANK_BSWAP16); + } else { + RANK_BODY(UINT16, RANK_IDENTITY, RANK_IDENTITY); + } + } else if (im->image8) { + RANK_BODY(UINT8, RANK_IDENTITY, RANK_IDENTITY); } else if (im->type == IMAGING_TYPE_INT32) { - RANK_BODY(INT32); + RANK_BODY(INT32, RANK_IDENTITY, RANK_IDENTITY); } else if (im->type == IMAGING_TYPE_FLOAT32) { - RANK_BODY(FLOAT32); + RANK_BODY(FLOAT32, RANK_IDENTITY, RANK_IDENTITY); } else { /* safety net (we shouldn't end up here) */ ImagingDelete(imOut); From f53584a603a2dec90d22bd34b017088ff776c42b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 19 Nov 2026 14:41:36 +1100 Subject: [PATCH 5/5] Combine load and store --- Tests/test_image_filter.py | 20 +------------------- src/libImaging/RankFilter.c | 35 ++++++++++++++++++----------------- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index c84fea0d4a0..889ca6bbaad 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -159,7 +159,7 @@ def test_rankfilter_i16(mode: str, filter: ImageFilter.RankFilter) -> None: data = [(i * 0x1F3D) & 0xFFFF for i in range(10 * 10)] im = Image.new(mode, (10, 10)) im.putdata(data) - reference = Image.new("I", (10, 10)) # Reference image in I32 mode. + reference = Image.new("I", (10, 10)) # Reference image in INT32 mode. reference.putdata(data) out = im.filter(filter) @@ -235,24 +235,6 @@ def test_rankfilter_overflow() -> None: im.filter(rankfilter) -@pytest.mark.parametrize( - "mode, fill", - ( - ("L", 0x12), - ("I", 0x12345678), - ("I;16", 0x1234), - ("I;16B", 0x1234), - ("F", 1.5), - ("RGB", (0x12, 0x34, 0x56)), - ), -) -@pytest.mark.parametrize("margin", (0, 1, 3)) -def test_expand(mode: str, fill: float | tuple[int, ...], margin: int) -> None: - im = Image.new(mode, (5, 4), fill) - out = im._new(im.im.expand(margin)) # As done internally by RankFilter - assert_image_equal(out, Image.new(mode, (5 + 2 * margin, 4 + 2 * margin), fill)) - - @pytest.mark.parametrize("mode", ("P", "PA")) def test_builtinfilter_palette(mode: str) -> None: builtin_filter = ImageFilter.BuiltinFilter() diff --git a/src/libImaging/RankFilter.c b/src/libImaging/RankFilter.c index 3f52dfcc33e..8757e0fc0a6 100644 --- a/src/libImaging/RankFilter.c +++ b/src/libImaging/RankFilter.c @@ -50,9 +50,9 @@ } // For RANK_BODY, -// `load` converts a stored value to one that compares correctly -// in the native byte order, and `store` converts it back. -// They are `RANK_IDENTITY` except when dealing with 16bpc images +// `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 // that are not in native byte order. #define RANK_IDENTITY(v) (v) #define RANK_BSWAP16(v) ((UINT16)(((v) >> 8) | ((v) << 8))) @@ -84,15 +84,16 @@ ImagingRankFilter(Imaging im, int size, int rank) { } // Every output pixel is written by the rank loop below - imOut = ImagingNewDirty(im->mode, im->xsize - 2 * margin, im->ysize - 2 * margin); + int xsize = im->xsize - 2 * margin; + int ysize = im->ysize - 2 * margin; + imOut = ImagingNewDirty(im->mode, xsize, ysize); if (!imOut) { return NULL; } - int xsize = imOut->xsize, ysize = imOut->ysize; /* malloc check ok, checked above */ // restrict safe: buf is a private allocation, imOut is a fresh allocation. -#define RANK_BODY(type, load, store) \ +#define RANK_BODY(type, pixel) \ do { \ type *restrict buf = malloc(size2 * sizeof(type)); \ if (!buf) { \ @@ -106,11 +107,11 @@ ImagingRankFilter(Imaging im, int size, int rank) { 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++ = load(row[s2]); \ + *p++ = pixel(row[s2]); \ } \ } \ RANK_INNER_BODY(type, buf, size2, rank); \ - out[x] = store(buf[rank]); \ + out[x] = pixel(buf[rank]); \ } \ } \ ImagingSectionLeave(&cookie); \ @@ -118,24 +119,24 @@ ImagingRankFilter(Imaging im, int size, int rank) { } while (0) if (im->type == IMAGING_TYPE_I16) { - int bigendian = isImageI16BigEndian(im); // Compare in native byte order + if ( #ifdef WORDS_BIGENDIAN - int swap = !bigendian; + im->mode == IMAGING_MODE_I_16 || im->mode == IMAGING_MODE_I_16L #else - int swap = bigendian; + im->mode == IMAGING_MODE_I_16B #endif - if (swap) { - RANK_BODY(UINT16, RANK_BSWAP16, RANK_BSWAP16); + ) { + RANK_BODY(UINT16, RANK_BSWAP16); } else { - RANK_BODY(UINT16, RANK_IDENTITY, RANK_IDENTITY); + RANK_BODY(UINT16, RANK_IDENTITY); } } else if (im->image8) { - RANK_BODY(UINT8, RANK_IDENTITY, RANK_IDENTITY); + RANK_BODY(UINT8, RANK_IDENTITY); } else if (im->type == IMAGING_TYPE_INT32) { - RANK_BODY(INT32, RANK_IDENTITY, RANK_IDENTITY); + RANK_BODY(INT32, RANK_IDENTITY); } else if (im->type == IMAGING_TYPE_FLOAT32) { - RANK_BODY(FLOAT32, RANK_IDENTITY, RANK_IDENTITY); + RANK_BODY(FLOAT32, RANK_IDENTITY); } else { /* safety net (we shouldn't end up here) */ ImagingDelete(imOut);