Skip to content

Commit 01d5639

Browse files
committed
Apply hoist optimizations to getbbox
1 parent e128ea8 commit 01d5639

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

src/libImaging/GetBBox.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ int
2222
ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) {
2323
/* Get the bounding box for any non-zero data in the image.*/
2424

25-
int x, y;
25+
int xsize = im->xsize, ysize = im->ysize;
2626
int has_data;
2727

2828
/* Initialize bounding box to max values */
29-
bbox[0] = im->xsize;
29+
bbox[0] = xsize;
3030
bbox[1] = -1;
3131
bbox[2] = bbox[3] = 0;
3232

33-
#define GETBBOX(image, mask) \
33+
#define GETBBOX(image, mask, type) \
3434
/* first stage: looking for any pixels from top */ \
35-
for (y = 0; y < im->ysize; y++) { \
35+
for (int y = 0; y < ysize; y++) { \
3636
has_data = 0; \
37-
for (x = 0; x < im->xsize; x++) { \
38-
if (im->image[y][x] & mask) { \
37+
const type *restrict row = im->image[y]; \
38+
for (int x = 0; x < xsize; x++) { \
39+
if (row[x] & mask) { \
3940
has_data = 1; \
4041
bbox[0] = x; \
4142
bbox[1] = y; \
@@ -51,14 +52,13 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) {
5152
return 0; /* no data */ \
5253
} \
5354
/* second stage: looking for any pixels from bottom */ \
54-
for (y = im->ysize - 1; y >= bbox[1]; y--) { \
55+
for (int y = ysize - 1; y >= bbox[1]; y--) { \
5556
has_data = 0; \
56-
for (x = 0; x < im->xsize; x++) { \
57-
if (im->image[y][x] & mask) { \
57+
const type *restrict row = im->image[y]; \
58+
for (int x = 0; x < xsize; x++) { \
59+
if (row[x] & mask) { \
5860
has_data = 1; \
59-
if (x < bbox[0]) { \
60-
bbox[0] = x; \
61-
} \
61+
bbox[0] = x < bbox[0] ? x : bbox[0]; \
6262
bbox[3] = y + 1; \
6363
break; \
6464
} \
@@ -68,23 +68,24 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) {
6868
} \
6969
} \
7070
/* third stage: looking for left and right boundaries */ \
71-
for (y = bbox[1]; y < bbox[3]; y++) { \
72-
for (x = 0; x < bbox[0]; x++) { \
73-
if (im->image[y][x] & mask) { \
71+
for (int y = bbox[1]; y < bbox[3]; y++) { \
72+
const type *restrict row = im->image[y]; \
73+
for (int x = 0; x < bbox[0]; x++) { \
74+
if (row[x] & mask) { \
7475
bbox[0] = x; \
7576
break; \
7677
} \
7778
} \
78-
for (x = im->xsize - 1; x >= bbox[2]; x--) { \
79-
if (im->image[y][x] & mask) { \
79+
for (int x = xsize - 1; x >= bbox[2]; x--) { \
80+
if (row[x] & mask) { \
8081
bbox[2] = x + 1; \
8182
break; \
8283
} \
8384
} \
8485
}
8586

8687
if (im->image8) {
87-
GETBBOX(image8, 0xff);
88+
GETBBOX(image8, 0xff, UINT8);
8889
} else {
8990
INT32 mask = 0xffffffff;
9091
if (im->bands == 3) {
@@ -101,7 +102,7 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) {
101102
mask = 0xff000000;
102103
#endif
103104
}
104-
GETBBOX(image32, mask);
105+
GETBBOX(image32, mask, INT32);
105106
}
106107

107108
return 1; /* ok */

0 commit comments

Comments
 (0)