Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Tests/test_image_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
)
Expand All @@ -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 INT32 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)
)
Expand Down
7 changes: 7 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
4 changes: 2 additions & 2 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand All @@ -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++;
Expand Down
66 changes: 35 additions & 31 deletions src/libImaging/Filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,56 +72,60 @@ 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;
if (imIn->xsize == 0 || imIn->ysize == 0) {
imOut = ImagingNew(imIn->mode, xsize, ysize);
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;
}
ImagingCopyPalette(imOut, imIn);
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]; \
} \
#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);
Expand Down
4 changes: 0 additions & 4 deletions src/libImaging/Imaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
154 changes: 85 additions & 69 deletions src/libImaging/RankFilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,53 @@
/* 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, 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; \
} \
}

#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]; \
}

MakeRankFunction(UINT8) MakeRankFunction(INT32) MakeRankFunction(FLOAT32)
// For 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
// 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) {
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) {
if (!im || im->bands != 1) {
return (Imaging)ImagingError_ModeError();
}

Expand All @@ -86,41 +84,59 @@ MakeRankFunction(UINT8) MakeRankFunction(INT32) MakeRankFunction(FLOAT32)
}

// 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;
}

/* 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, 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)

if (im->image8) {
RANK_BODY(UINT8);
if (im->type == IMAGING_TYPE_I16) {
// Compare in native byte order
if (
#ifdef WORDS_BIGENDIAN
im->mode == IMAGING_MODE_I_16 || im->mode == IMAGING_MODE_I_16L
#else
im->mode == IMAGING_MODE_I_16B
#endif
) {
RANK_BODY(UINT16, RANK_BSWAP16);
} else {
RANK_BODY(UINT16, RANK_IDENTITY);
}
} else if (im->image8) {
RANK_BODY(UINT8, RANK_IDENTITY);
} else if (im->type == IMAGING_TYPE_INT32) {
RANK_BODY(INT32);
RANK_BODY(INT32, RANK_IDENTITY);
} else if (im->type == IMAGING_TYPE_FLOAT32) {
RANK_BODY(FLOAT32);
RANK_BODY(FLOAT32, RANK_IDENTITY);
} else {
/* safety net (we shouldn't end up here) */
ImagingDelete(imOut);
Expand Down
Loading