FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Speed up `Image.blend()` by akx · Pull Request #9649 · python-pillow/Pillow · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
29 changes: 18 additions & 11 deletions src/libImaging/Blend.c
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
Expand Up @@ -35,7 +35,7 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) {
}

/* Shortcuts */
if (alpha == 0.0) {
if (imIn1 == imIn2 || alpha == 0.0) {
return ImagingCopy(imIn1);
} else if (alpha == 1.0) {
return ImagingCopy(imIn2);
Expand All @@ -46,23 +46,30 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) {
return NULL;
}

// We know these won't change in the loops below,
// so the compiler can optimize better if we pull them out.
int ysize = imIn1->ysize;
int linesize = imIn1->linesize;

if (alpha >= 0 && alpha <= 1.0) {
/* Interpolate between bands */
for (y = 0; y < imIn1->ysize; y++) {
UINT8 *in1 = (UINT8 *)imIn1->image[y];
UINT8 *in2 = (UINT8 *)imIn2->image[y];
UINT8 *out = (UINT8 *)imOut->image[y];
for (x = 0; x < imIn1->linesize; x++) {
for (y = 0; y < ysize; y++) {
// restrict safe: imIn1 and imIn2 are read-only; imOut is a new allocation
UINT8 *restrict in1 = (UINT8 *)imIn1->image[y];
UINT8 *restrict in2 = (UINT8 *)imIn2->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (x = 0; x < linesize; x++) {
out[x] = (UINT8)((int)in1[x] + alpha * ((int)in2[x] - (int)in1[x]));
}
}
} else {
/* Extrapolation; must make sure to clip resulting values */
for (y = 0; y < imIn1->ysize; y++) {
UINT8 *in1 = (UINT8 *)imIn1->image[y];
UINT8 *in2 = (UINT8 *)imIn2->image[y];
UINT8 *out = (UINT8 *)imOut->image[y];
for (x = 0; x < imIn1->linesize; x++) {
for (y = 0; y < ysize; y++) {
// restrict safe: imIn1 and imIn2 are read-only; imOut is a new allocation
UINT8 *restrict in1 = (UINT8 *)imIn1->image[y];
UINT8 *restrict in2 = (UINT8 *)imIn2->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (x = 0; x < linesize; x++) {
float temp = (float)((int)in1[x] + alpha * ((int)in2[x] - (int)in1[x]));
if (temp <= 0.0) {
out[x] = 0;
Expand Down
Loading

Back | FazBrowse Home | New Git URL