-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Support ImageFilter.BuiltinFilter for I;16* images #8438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |||||
|
|
||||||
| #include "Imaging.h" | ||||||
|
|
||||||
| #define ROUND_UP(f) ((int)((f) >= 0.0 ? (f) + 0.5F : (f) - 0.5F)) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this a function instead?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason? This was copying Pillow/src/libImaging/Reduce.c Line 5 in 27c1bb2
Pillow/src/libImaging/Resample.c Line 5 in 27c1bb2
If there's a reason to prefer functions, then maybe they should all be changed. I'd suggest a common function for all three, except that this uses a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Functions are generally safer than macros, which need extra care around parentheses and don't get extra checking. But if this is a copy/paste of existing ones then we should be fine. |
||||||
|
|
||||||
| static inline UINT8 | ||||||
| clip8(float in) { | ||||||
| if (in <= 0.0) { | ||||||
|
|
@@ -105,6 +107,22 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin) { | |||||
| return imOut; | ||||||
| } | ||||||
|
|
||||||
| float | ||||||
| kernel_i16(int size, UINT8 *in0, int x, const float *kernel, int bigendian) { | ||||||
| int i; | ||||||
| float result = 0; | ||||||
| int half_size = (size - 1) / 2; | ||||||
| for (i = 0; i < size; i++) { | ||||||
| int x1 = x + i - half_size; | ||||||
| result += _i2f( | ||||||
| in0[x1 * 2 + (bigendian ? 1 : 0)] + | ||||||
| (in0[x1 * 2 + (bigendian ? 0 : 1)] >> 8) | ||||||
| ) * | ||||||
| kernel[i]; | ||||||
| } | ||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| void | ||||||
| ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) { | ||||||
| #define KERNEL1x3(in0, x, kernel, d) \ | ||||||
|
|
@@ -135,21 +153,48 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) { | |||||
| out[x] = in0[x]; | ||||||
| } | ||||||
| } else { | ||||||
| int bigendian = 0; | ||||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||||
| if (strcmp(im->mode, "I;16B") == 0 | ||||||
| #ifdef WORDS_BIGENDIAN | ||||||
| || strcmp(im->mode, "I;16N") == 0 | ||||||
| #endif | ||||||
| ) { | ||||||
| bigendian = 1; | ||||||
| } | ||||||
| } | ||||||
| for (y = 1; y < im->ysize - 1; y++) { | ||||||
| UINT8 *in_1 = (UINT8 *)im->image[y - 1]; | ||||||
| UINT8 *in0 = (UINT8 *)im->image[y]; | ||||||
| UINT8 *in1 = (UINT8 *)im->image[y + 1]; | ||||||
| UINT8 *out = (UINT8 *)imOut->image[y]; | ||||||
|
|
||||||
| out[0] = in0[0]; | ||||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||||
| out[1] = in0[1]; | ||||||
| } | ||||||
| for (x = 1; x < im->xsize - 1; x++) { | ||||||
| float ss = offset; | ||||||
| ss += KERNEL1x3(in1, x, &kernel[0], 1); | ||||||
| ss += KERNEL1x3(in0, x, &kernel[3], 1); | ||||||
| ss += KERNEL1x3(in_1, x, &kernel[6], 1); | ||||||
| out[x] = clip8(ss); | ||||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||||
| ss += kernel_i16(3, in1, x, &kernel[0], bigendian); | ||||||
| ss += kernel_i16(3, in0, x, &kernel[3], bigendian); | ||||||
| ss += kernel_i16(3, in_1, x, &kernel[6], bigendian); | ||||||
| int ss_int = ROUND_UP(ss); | ||||||
| out[x * 2 + (bigendian ? 1 : 0)] = clip8(ss_int % 256); | ||||||
| out[x * 2 + (bigendian ? 0 : 1)] = clip8(ss_int >> 8); | ||||||
| } else { | ||||||
| ss += KERNEL1x3(in1, x, &kernel[0], 1); | ||||||
| ss += KERNEL1x3(in0, x, &kernel[3], 1); | ||||||
| ss += KERNEL1x3(in_1, x, &kernel[6], 1); | ||||||
| out[x] = clip8(ss); | ||||||
| } | ||||||
| } | ||||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||||
| out[x * 2] = in0[x * 2]; | ||||||
| out[x * 2 + 1] = in0[x * 2 + 1]; | ||||||
| } else { | ||||||
| out[x] = in0[x]; | ||||||
| } | ||||||
| out[x] = in0[x]; | ||||||
| } | ||||||
| } | ||||||
| } else { | ||||||
|
|
@@ -261,6 +306,16 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) { | |||||
| out[x + 1] = in0[x + 1]; | ||||||
| } | ||||||
| } else { | ||||||
| int bigendian = 0; | ||||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||||
| if (strcmp(im->mode, "I;16B") == 0 | ||||||
| #ifdef WORDS_BIGENDIAN | ||||||
| || strcmp(im->mode, "I;16N") == 0 | ||||||
| #endif | ||||||
| ) { | ||||||
| bigendian = 1; | ||||||
| } | ||||||
| } | ||||||
| for (y = 2; y < im->ysize - 2; y++) { | ||||||
| UINT8 *in_2 = (UINT8 *)im->image[y - 2]; | ||||||
| UINT8 *in_1 = (UINT8 *)im->image[y - 1]; | ||||||
|
|
@@ -271,17 +326,39 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) { | |||||
|
|
||||||
| out[0] = in0[0]; | ||||||
| out[1] = in0[1]; | ||||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||||
| out[2] = in0[2]; | ||||||
| out[3] = in0[3]; | ||||||
| } | ||||||
| for (x = 2; x < im->xsize - 2; x++) { | ||||||
| float ss = offset; | ||||||
| ss += KERNEL1x5(in2, x, &kernel[0], 1); | ||||||
| ss += KERNEL1x5(in1, x, &kernel[5], 1); | ||||||
| ss += KERNEL1x5(in0, x, &kernel[10], 1); | ||||||
| ss += KERNEL1x5(in_1, x, &kernel[15], 1); | ||||||
| ss += KERNEL1x5(in_2, x, &kernel[20], 1); | ||||||
| out[x] = clip8(ss); | ||||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||||
| ss += kernel_i16(5, in2, x, &kernel[0], bigendian); | ||||||
| ss += kernel_i16(5, in1, x, &kernel[5], bigendian); | ||||||
| ss += kernel_i16(5, in0, x, &kernel[10], bigendian); | ||||||
| ss += kernel_i16(5, in_1, x, &kernel[15], bigendian); | ||||||
| ss += kernel_i16(5, in_2, x, &kernel[20], bigendian); | ||||||
| int ss_int = ROUND_UP(ss); | ||||||
| out[x * 2 + (bigendian ? 1 : 0)] = clip8(ss_int % 256); | ||||||
| out[x * 2 + (bigendian ? 0 : 1)] = clip8(ss_int >> 8); | ||||||
| } else { | ||||||
| ss += KERNEL1x5(in2, x, &kernel[0], 1); | ||||||
| ss += KERNEL1x5(in1, x, &kernel[5], 1); | ||||||
| ss += KERNEL1x5(in0, x, &kernel[10], 1); | ||||||
| ss += KERNEL1x5(in_1, x, &kernel[15], 1); | ||||||
| ss += KERNEL1x5(in_2, x, &kernel[20], 1); | ||||||
| out[x] = clip8(ss); | ||||||
| } | ||||||
| } | ||||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||||
| out[x * 2 + 0] = in0[x * 2 + 0]; | ||||||
| out[x * 2 + 1] = in0[x * 2 + 1]; | ||||||
| out[x * 2 + 2] = in0[x * 2 + 2]; | ||||||
| out[x * 2 + 3] = in0[x * 2 + 3]; | ||||||
| } else { | ||||||
| out[x + 0] = in0[x + 0]; | ||||||
| out[x + 1] = in0[x + 1]; | ||||||
| } | ||||||
| out[x + 0] = in0[x + 0]; | ||||||
| out[x + 1] = in0[x + 1]; | ||||||
| } | ||||||
| } | ||||||
| } else { | ||||||
|
|
@@ -383,7 +460,8 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32 *kernel, FLOAT32 o | |||||
| Imaging imOut; | ||||||
| ImagingSectionCookie cookie; | ||||||
|
|
||||||
| if (im->type != IMAGING_TYPE_UINT8 && im->type != IMAGING_TYPE_INT32) { | ||||||
| if (im->type == IMAGING_TYPE_FLOAT32 || | ||||||
| (im->type == IMAGING_TYPE_SPECIAL && im->bands != 1)) { | ||||||
| return (Imaging)ImagingError_ModeError(); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
isinstancecondition, added in #7108, was never true - eachBuiltinFilterrun of this test uses aBuiltinFilterclass, not an instance. So this corrects it.