From 4cf5688cfd42caed6f25db2d5ea29271ea8c1c9f Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Tue, 2 Jul 2024 16:43:34 +0400 Subject: [PATCH 1/6] Optimize getbbox() and getextrema() --- src/libImaging/GetBBox.c | 62 +++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/src/libImaging/GetBBox.c b/src/libImaging/GetBBox.c index c61a27d3b7f..680d1cf77ba 100644 --- a/src/libImaging/GetBBox.c +++ b/src/libImaging/GetBBox.c @@ -30,26 +30,45 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) { bbox[1] = -1; bbox[2] = bbox[3] = 0; -#define GETBBOX(image, mask) \ - for (y = 0; y < im->ysize; y++) { \ - has_data = 0; \ - for (x = 0; x < im->xsize; x++) { \ - if (im->image[y][x] & mask) { \ - has_data = 1; \ - if (x < bbox[0]) { \ - bbox[0] = x; \ - } \ - if (x >= bbox[2]) { \ - bbox[2] = x + 1; \ - } \ - } \ - } \ - if (has_data) { \ - if (bbox[1] < 0) { \ - bbox[1] = y; \ - } \ - bbox[3] = y + 1; \ - } \ +#define GETBBOX(image, mask) \ + /* first stage: looking for any px from top */ \ + for (y = 0; y < im->ysize; y++) { \ + has_data = 0; \ + for (x = 0; x < im->xsize; x++) \ + if (im->image[y][x] & mask) { \ + has_data = 1; \ + bbox[0] = x; \ + bbox[1] = y; \ + break; \ + } \ + if (has_data) break; \ + } \ + /* Check that we got a box */ \ + if (bbox[1] < 0) \ + return 0; /* no data */ \ + /* second stage: looking for any px from bottom */ \ + for (y = im->ysize - 1; y >= bbox[1]; y--) { \ + has_data = 0; \ + for (x = 0; x < im->xsize; x++) \ + if (im->image[y][x] & mask) { \ + has_data = 1; \ + bbox[3] = y + 1; \ + break; \ + } \ + if (has_data) break; \ + } \ + /* third stage: looking for left and right boundaries */ \ + for (y = bbox[1]; y < bbox[3]; y++) { \ + for (x = 0; x < bbox[0]; x++) \ + if (im->image[y][x] & mask) { \ + bbox[0] = x; \ + break; \ + } \ + for (x = im->xsize - 1; x >= bbox[2]; x--) \ + if (im->image[y][x] & mask) { \ + bbox[2] = x + 1; \ + break; \ + } \ } if (im->image8) { @@ -144,6 +163,9 @@ ImagingGetExtrema(Imaging im, void *extrema) { imax = in[x]; } } + if (imin == 0 && imax == 255) { + break; + } } ((UINT8 *)extrema)[0] = (UINT8)imin; ((UINT8 *)extrema)[1] = (UINT8)imax; From 6a87df2c1fdd0dea1a3bdd5fb85e388e535e063b Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Tue, 2 Jul 2024 18:40:19 +0400 Subject: [PATCH 2/6] clang-format --- src/libImaging/GetBBox.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libImaging/GetBBox.c b/src/libImaging/GetBBox.c index 680d1cf77ba..46a67807826 100644 --- a/src/libImaging/GetBBox.c +++ b/src/libImaging/GetBBox.c @@ -30,7 +30,7 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) { bbox[1] = -1; bbox[2] = bbox[3] = 0; -#define GETBBOX(image, mask) \ +#define GETBBOX(image, mask) \ /* first stage: looking for any px from top */ \ for (y = 0; y < im->ysize; y++) { \ has_data = 0; \ @@ -41,7 +41,8 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) { bbox[1] = y; \ break; \ } \ - if (has_data) break; \ + if (has_data) \ + break; \ } \ /* Check that we got a box */ \ if (bbox[1] < 0) \ @@ -55,7 +56,8 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) { bbox[3] = y + 1; \ break; \ } \ - if (has_data) break; \ + if (has_data) \ + break; \ } \ /* third stage: looking for left and right boundaries */ \ for (y = bbox[1]; y < bbox[3]; y++) { \ From 5fb44ab694bc5576c6cd7e7ee54dfce660f497af Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Tue, 2 Jul 2024 18:45:13 +0400 Subject: [PATCH 3/6] This check is useless, since moved after the first stage --- src/libImaging/GetBBox.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libImaging/GetBBox.c b/src/libImaging/GetBBox.c index 46a67807826..2e8afc8d54d 100644 --- a/src/libImaging/GetBBox.c +++ b/src/libImaging/GetBBox.c @@ -92,11 +92,6 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) { GETBBOX(image32, mask); } - /* Check that we got a box */ - if (bbox[1] < 0) { - return 0; /* no data */ - } - return 1; /* ok */ } From 8256b9bb7fd22abfe61ed0013c02fa18255f398f Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Tue, 2 Jul 2024 19:05:24 +0400 Subject: [PATCH 4/6] Correct left boundary on the second stage --- src/libImaging/GetBBox.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libImaging/GetBBox.c b/src/libImaging/GetBBox.c index 2e8afc8d54d..50b2253a0e0 100644 --- a/src/libImaging/GetBBox.c +++ b/src/libImaging/GetBBox.c @@ -53,6 +53,8 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) { for (x = 0; x < im->xsize; x++) \ if (im->image[y][x] & mask) { \ has_data = 1; \ + if (x < bbox[0]) \ + bbox[0] = x; \ bbox[3] = y + 1; \ break; \ } \ From 2b6c5a27a1a6813fef4f06d45cf63428b2ae2da6 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 3 Jul 2024 22:42:52 +1000 Subject: [PATCH 5/6] Added braces --- src/libImaging/GetBBox.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/libImaging/GetBBox.c b/src/libImaging/GetBBox.c index 50b2253a0e0..1303617ad1e 100644 --- a/src/libImaging/GetBBox.c +++ b/src/libImaging/GetBBox.c @@ -34,45 +34,53 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) { /* first stage: looking for any px from top */ \ for (y = 0; y < im->ysize; y++) { \ has_data = 0; \ - for (x = 0; x < im->xsize; x++) \ + for (x = 0; x < im->xsize; x++) { \ if (im->image[y][x] & mask) { \ has_data = 1; \ bbox[0] = x; \ bbox[1] = y; \ break; \ } \ - if (has_data) \ + } \ + if (has_data) { \ break; \ + } \ } \ /* Check that we got a box */ \ - if (bbox[1] < 0) \ + if (bbox[1] < 0) { \ return 0; /* no data */ \ + } \ /* second stage: looking for any px from bottom */ \ for (y = im->ysize - 1; y >= bbox[1]; y--) { \ has_data = 0; \ - for (x = 0; x < im->xsize; x++) \ + for (x = 0; x < im->xsize; x++) { \ if (im->image[y][x] & mask) { \ has_data = 1; \ - if (x < bbox[0]) \ + if (x < bbox[0]) { \ bbox[0] = x; \ + } \ bbox[3] = y + 1; \ break; \ } \ - if (has_data) \ + } \ + if (has_data) { \ break; \ + } \ } \ /* third stage: looking for left and right boundaries */ \ for (y = bbox[1]; y < bbox[3]; y++) { \ - for (x = 0; x < bbox[0]; x++) \ + for (x = 0; x < bbox[0]; x++) { \ if (im->image[y][x] & mask) { \ bbox[0] = x; \ break; \ } \ - for (x = im->xsize - 1; x >= bbox[2]; x--) \ + } \ + for (x = im->xsize - 1; x >= bbox[2]; x--) { \ if (im->image[y][x] & mask) { \ bbox[2] = x + 1; \ break; \ } \ + } \ } if (im->image8) { From e4e2cd65642fa3dc02793fe426ba22a00d163db9 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 3 Jul 2024 22:45:10 +1000 Subject: [PATCH 6/6] Updated comments --- src/libImaging/GetBBox.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libImaging/GetBBox.c b/src/libImaging/GetBBox.c index 1303617ad1e..d430893ddb2 100644 --- a/src/libImaging/GetBBox.c +++ b/src/libImaging/GetBBox.c @@ -31,7 +31,7 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) { bbox[2] = bbox[3] = 0; #define GETBBOX(image, mask) \ - /* first stage: looking for any px from top */ \ + /* first stage: looking for any pixels from top */ \ for (y = 0; y < im->ysize; y++) { \ has_data = 0; \ for (x = 0; x < im->xsize; x++) { \ @@ -46,11 +46,11 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) { break; \ } \ } \ - /* Check that we got a box */ \ + /* Check that we have a box */ \ if (bbox[1] < 0) { \ return 0; /* no data */ \ } \ - /* second stage: looking for any px from bottom */ \ + /* second stage: looking for any pixels from bottom */ \ for (y = im->ysize - 1; y >= bbox[1]; y--) { \ has_data = 0; \ for (x = 0; x < im->xsize; x++) { \