Skip to content

Commit 92a4479

Browse files
akxhugovk
authored andcommitted
Blend: hoist static image size variables out of loops for optimization
1 parent 9787733 commit 92a4479

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/libImaging/Blend.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,28 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) {
4646
return NULL;
4747
}
4848

49+
// We know these won't change in the loops below,
50+
// so the compiler can optimize better if we pull them out.
51+
int ysize = imIn1->ysize;
52+
int linesize = imIn1->linesize;
53+
4954
if (alpha >= 0 && alpha <= 1.0) {
5055
/* Interpolate between bands */
51-
for (y = 0; y < imIn1->ysize; y++) {
56+
for (y = 0; y < ysize; y++) {
5257
UINT8 *in1 = (UINT8 *)imIn1->image[y];
5358
UINT8 *in2 = (UINT8 *)imIn2->image[y];
5459
UINT8 *out = (UINT8 *)imOut->image[y];
55-
for (x = 0; x < imIn1->linesize; x++) {
60+
for (x = 0; x < linesize; x++) {
5661
out[x] = (UINT8)((int)in1[x] + alpha * ((int)in2[x] - (int)in1[x]));
5762
}
5863
}
5964
} else {
6065
/* Extrapolation; must make sure to clip resulting values */
61-
for (y = 0; y < imIn1->ysize; y++) {
66+
for (y = 0; y < ysize; y++) {
6267
UINT8 *in1 = (UINT8 *)imIn1->image[y];
6368
UINT8 *in2 = (UINT8 *)imIn2->image[y];
6469
UINT8 *out = (UINT8 *)imOut->image[y];
65-
for (x = 0; x < imIn1->linesize; x++) {
70+
for (x = 0; x < linesize; x++) {
6671
float temp = (float)((int)in1[x] + alpha * ((int)in2[x] - (int)in1[x]));
6772
if (temp <= 0.0) {
6873
out[x] = 0;

0 commit comments

Comments
 (0)