-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Speed up Image.filter() for I;16*
#9760
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
Open
akx
wants to merge
10
commits into
python-pillow:main
Choose a base branch
from
akx:faster-filter-still
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−101
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
183d6ba
Deduplicate modes list in test_image_filter
akx c008a7a
Remove inaccurate docstring text
akx e48212f
Rename lying test
akx 54c9499
Remove unused parametrization from test_consistency_*
akx 9eeec34
Refactor test_consistency; add 16bpc test
akx 08548eb
Split I16 ImageFilter loops so they are optimized by compiler
akx b726bfa
Use size to refer to test images
radarhere 3debcfe
Combine matrix constants
radarhere 1189ca1
Move kernel outside image opens
radarhere 80b9256
Simplified code
radarhere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -161,17 +161,16 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) { | |||
| } | ||||
| out[x] = in0[x]; | ||||
| } | ||||
| } else { | ||||
| } else if (im->type == IMAGING_TYPE_SPECIAL) { | ||||
| // Check for I;16 mode once, not per pixel | ||||
| int bigendian = 0; | ||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||
| if ( | ||||
| im->mode == IMAGING_MODE_I_16B | ||||
| if ( | ||||
| im->mode == IMAGING_MODE_I_16B | ||||
| #ifdef WORDS_BIGENDIAN | ||||
| || im->mode == IMAGING_MODE_I_16N | ||||
| || im->mode == IMAGING_MODE_I_16N | ||||
| #endif | ||||
| ) { | ||||
| bigendian = 1; | ||||
| } | ||||
| ) { | ||||
| bigendian = 1; | ||||
| } | ||||
| for (y = 1; y < ysize - 1; y++) { | ||||
| UINT8 *restrict in_1 = (UINT8 *)im->image[y - 1]; | ||||
|
|
@@ -180,32 +179,36 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) { | |||
| UINT8 *restrict out = (UINT8 *)imOut->image[y]; | ||||
|
|
||||
| out[0] = in0[0]; | ||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||
| out[1] = in0[1]; | ||||
| } | ||||
| out[1] = in0[1]; | ||||
| for (x = 1; x < xsize - 1; x++) { | ||||
| float ss = offset; | ||||
| 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); | ||||
| // NOT rounding here because `offset` already has a +0.5 bias. | ||||
| int ss_int = clip16(ss); | ||||
| out[x * 2 + (bigendian ? 1 : 0)] = (UINT8)(ss_int & 0xff); | ||||
| out[x * 2 + (bigendian ? 0 : 1)] = (UINT8)(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); | ||||
| } | ||||
| 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); | ||||
| // NOT rounding here because `offset` already has a +0.5 bias. | ||||
| int ss_int = clip16(ss); | ||||
| out[x * 2 + (bigendian ? 1 : 0)] = (UINT8)(ss_int & 0xff); | ||||
| out[x * 2 + (bigendian ? 0 : 1)] = (UINT8)(ss_int >> 8); | ||||
| } | ||||
| 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 * 2] = in0[x * 2]; | ||||
| out[x * 2 + 1] = in0[x * 2 + 1]; | ||||
| } | ||||
| } else { | ||||
| for (y = 1; y < ysize - 1; y++) { | ||||
| UINT8 *restrict in_1 = (UINT8 *)im->image[y - 1]; | ||||
| UINT8 *restrict in0 = (UINT8 *)im->image[y]; | ||||
| UINT8 *restrict in1 = (UINT8 *)im->image[y + 1]; | ||||
| UINT8 *restrict out = (UINT8 *)imOut->image[y]; | ||||
|
|
||||
| out[0] = in0[0]; | ||||
| for (x = 1; x < 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); | ||||
| } | ||||
| out[x] = in0[x]; | ||||
| } | ||||
| } | ||||
| } else { | ||||
|
|
@@ -322,17 +325,16 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) { | |||
| out[x + 0] = in0[x + 0]; | ||||
| out[x + 1] = in0[x + 1]; | ||||
| } | ||||
| } else { | ||||
| } else if (im->type == IMAGING_TYPE_SPECIAL) { | ||||
| // Check for I;16 mode once, not per pixel | ||||
|
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.
Suggested change
|
||||
| int bigendian = 0; | ||||
| if (im->type == IMAGING_TYPE_SPECIAL) { | ||||
| if ( | ||||
| im->mode == IMAGING_MODE_I_16B | ||||
| if ( | ||||
| im->mode == IMAGING_MODE_I_16B | ||||
| #ifdef WORDS_BIGENDIAN | ||||
| || im->mode == IMAGING_MODE_I_16N | ||||
| || im->mode == IMAGING_MODE_I_16N | ||||
| #endif | ||||
| ) { | ||||
| bigendian = 1; | ||||
| } | ||||
| ) { | ||||
| bigendian = 1; | ||||
| } | ||||
| for (y = 2; y < ysize - 2; y++) { | ||||
| UINT8 *restrict in_2 = (UINT8 *)im->image[y - 2]; | ||||
|
|
@@ -344,40 +346,47 @@ 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]; | ||||
| } | ||||
| out[2] = in0[2]; | ||||
| out[3] = in0[3]; | ||||
| for (x = 2; x < xsize - 2; x++) { | ||||
| float ss = offset; | ||||
| 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); | ||||
| // NOT rounding here because `offset` already has a +0.5 bias. | ||||
| int ss_int = clip16(ss); | ||||
| out[x * 2 + (bigendian ? 1 : 0)] = (UINT8)(ss_int & 0xff); | ||||
| out[x * 2 + (bigendian ? 0 : 1)] = (UINT8)(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); | ||||
| } | ||||
| 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); | ||||
| // NOT rounding here because `offset` already has a +0.5 bias. | ||||
| int ss_int = clip16(ss); | ||||
| out[x * 2 + (bigendian ? 1 : 0)] = (UINT8)(ss_int & 0xff); | ||||
| out[x * 2 + (bigendian ? 0 : 1)] = (UINT8)(ss_int >> 8); | ||||
| } | ||||
| 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 * 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 { | ||||
| for (y = 2; y < ysize - 2; y++) { | ||||
| UINT8 *restrict in_2 = (UINT8 *)im->image[y - 2]; | ||||
| UINT8 *restrict in_1 = (UINT8 *)im->image[y - 1]; | ||||
| UINT8 *restrict in0 = (UINT8 *)im->image[y]; | ||||
| UINT8 *restrict in1 = (UINT8 *)im->image[y + 1]; | ||||
| UINT8 *restrict in2 = (UINT8 *)im->image[y + 2]; | ||||
| UINT8 *restrict out = (UINT8 *)imOut->image[y]; | ||||
|
|
||||
| out[0] = in0[0]; | ||||
| out[1] = in0[1]; | ||||
| for (x = 2; x < 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); | ||||
| } | ||||
| out[x + 0] = in0[x + 0]; | ||||
| out[x + 1] = in0[x + 1]; | ||||
| } | ||||
| } | ||||
| } else { | ||||
|
|
||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Once this is merged, I don't think it's obvious what the comment is referring to