Skip to content
Merged
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
117 changes: 82 additions & 35 deletions src/libImaging/RankFilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,76 @@
} \
}

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

// 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.
// 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)))

// Minimum or maximum (per `op`) over each size x size window.
#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++) { \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain s == 0 to me? It doesn't occur in the RANK_BODY version, I'm guessing because you've moved the x loop inside this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that skips the (0, 0) position in the window, which already went into out[] in the for (int x = ...) loop above.

If you like, we can do a comment in the code explaining it:

Suggested change
for (int s2 = (s == 0); s2 < size; s2++) { \
/* (0, 0) already seeded out[] above */ \
for (int s2 = (s == 0); s2 < size; s2++) { \

RANK_BODY can't do the same thing, because it needs to fill the window for each pixel it processes, then reorders it in place with the quickselect "fast rank algorithm" above. For min or max, we can just accumulate as we go.

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)

// 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) {
Expand All @@ -71,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");
}
Expand All @@ -91,31 +154,15 @@ 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); \
#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) {
Expand All @@ -127,16 +174,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);
Expand Down
Loading