From 25ae464421b6610999ca9b39b9855bd17fb81add Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Sun, 11 Aug 2024 21:55:30 +0400 Subject: [PATCH] Make SSE4 optional --- src/libImaging/BoxBlur.c | 164 ++++++++++++++++++++++++++++++++------- 1 file changed, 134 insertions(+), 30 deletions(-) diff --git a/src/libImaging/BoxBlur.c b/src/libImaging/BoxBlur.c index ea250252b0c..6e40d722844 100644 --- a/src/libImaging/BoxBlur.c +++ b/src/libImaging/BoxBlur.c @@ -3,7 +3,11 @@ #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MIN(x, y) (((x) < (y)) ? (x) : (y)) -void static inline ImagingLineBoxBlur32( +typedef UINT8 pixel[4]; + +#if defined(__SSE4__) + +void static inline ImagingLineBoxBlur32_SSE4( UINT32 *lineOut, UINT32 *lineIn, int lastx, @@ -115,7 +119,7 @@ void static inline ImagingLineBoxBlur32( #undef SAVE } -void static inline ImagingLineBoxBlurZero32( +void static inline ImagingLineBoxBlurZero32_SSE4( UINT32 *lineOut, UINT32 *lineIn, int lastx, @@ -177,6 +181,105 @@ void static inline ImagingLineBoxBlurZero32( #undef SAVE } +#endif // defined(__SSE4__) + +void static inline ImagingLineBoxBlur32( + pixel *lineOut, + pixel *lineIn, + int lastx, + int radius, + int edgeA, + int edgeB, + UINT32 ww, + UINT32 fw +) { + int x; + UINT32 acc[4]; + UINT32 bulk[4]; + +#define MOVE_ACC(acc, subtract, add) \ + acc[0] += lineIn[add][0] - lineIn[subtract][0]; \ + acc[1] += lineIn[add][1] - lineIn[subtract][1]; \ + acc[2] += lineIn[add][2] - lineIn[subtract][2]; \ + acc[3] += lineIn[add][3] - lineIn[subtract][3]; + +#define ADD_FAR(bulk, acc, left, right) \ + bulk[0] = (acc[0] * ww) + (lineIn[left][0] + lineIn[right][0]) * fw; \ + bulk[1] = (acc[1] * ww) + (lineIn[left][1] + lineIn[right][1]) * fw; \ + bulk[2] = (acc[2] * ww) + (lineIn[left][2] + lineIn[right][2]) * fw; \ + bulk[3] = (acc[3] * ww) + (lineIn[left][3] + lineIn[right][3]) * fw; + +#define SAVE(x, bulk) \ + lineOut[x][0] = (UINT8)((bulk[0] + (1 << 23)) >> 24); \ + lineOut[x][1] = (UINT8)((bulk[1] + (1 << 23)) >> 24); \ + lineOut[x][2] = (UINT8)((bulk[2] + (1 << 23)) >> 24); \ + lineOut[x][3] = (UINT8)((bulk[3] + (1 << 23)) >> 24); + + /* Compute acc for -1 pixel (outside of image): + From "-radius-1" to "-1" get first pixel, + then from "0" to "radius-1". */ + acc[0] = lineIn[0][0] * (radius + 1); + acc[1] = lineIn[0][1] * (radius + 1); + acc[2] = lineIn[0][2] * (radius + 1); + acc[3] = lineIn[0][3] * (radius + 1); + /* As radius can be bigger than xsize, iterate to edgeA -1. */ + for (x = 0; x < edgeA - 1; x++) { + acc[0] += lineIn[x][0]; + acc[1] += lineIn[x][1]; + acc[2] += lineIn[x][2]; + acc[3] += lineIn[x][3]; + } + /* Then multiply remainder to last x. */ + acc[0] += lineIn[lastx][0] * (radius - edgeA + 1); + acc[1] += lineIn[lastx][1] * (radius - edgeA + 1); + acc[2] += lineIn[lastx][2] * (radius - edgeA + 1); + acc[3] += lineIn[lastx][3] * (radius - edgeA + 1); + + if (edgeA <= edgeB) { + /* Subtract pixel from left ("0"). + Add pixels from radius. */ + for (x = 0; x < edgeA; x++) { + MOVE_ACC(acc, 0, x + radius); + ADD_FAR(bulk, acc, 0, x + radius + 1); + SAVE(x, bulk); + } + /* Subtract previous pixel from "-radius". + Add pixels from radius. */ + for (x = edgeA; x < edgeB; x++) { + MOVE_ACC(acc, x - radius - 1, x + radius); + ADD_FAR(bulk, acc, x - radius - 1, x + radius + 1); + SAVE(x, bulk); + } + /* Subtract previous pixel from "-radius". + Add last pixel. */ + for (x = edgeB; x <= lastx; x++) { + MOVE_ACC(acc, x - radius - 1, lastx); + ADD_FAR(bulk, acc, x - radius - 1, lastx); + SAVE(x, bulk); + } + } else { + for (x = 0; x < edgeB; x++) { + MOVE_ACC(acc, 0, x + radius); + ADD_FAR(bulk, acc, 0, x + radius + 1); + SAVE(x, bulk); + } + for (x = edgeB; x < edgeA; x++) { + MOVE_ACC(acc, 0, lastx); + ADD_FAR(bulk, acc, 0, lastx); + SAVE(x, bulk); + } + for (x = edgeA; x <= lastx; x++) { + MOVE_ACC(acc, x - radius - 1, lastx); + ADD_FAR(bulk, acc, x - radius - 1, lastx); + SAVE(x, bulk); + } + } + +#undef MOVE_ACC +#undef ADD_FAR +#undef SAVE +} + void static inline ImagingLineBoxBlur8( UINT8 *lineOut, UINT8 *lineIn, @@ -302,8 +405,8 @@ ImagingHorizontalBoxBlur(Imaging imOut, Imaging imIn, float floatRadius) { ImagingSectionEnter(&cookie); if (imIn->image8) { - if (radius) { - for (y = 0; y < imIn->ysize; y++) { + for (y = 0; y < imIn->ysize; y++) { + if (radius) { ImagingLineBoxBlur8( (imIn == imOut ? (UINT8 *)lineOut : imOut->image8[y]), imIn->image8[y], @@ -314,13 +417,7 @@ ImagingHorizontalBoxBlur(Imaging imOut, Imaging imIn, float floatRadius) { ww, fw ); - if (imIn == imOut) { - // Commit. - memcpy(imOut->image8[y], lineOut, imIn->xsize); - } - } - } else { - for (y = 0; y < imIn->ysize; y++) { + } else { ImagingLineBoxBlurZero8( (imIn == imOut ? (UINT8 *)lineOut : imOut->image8[y]), imIn->image8[y], @@ -330,16 +427,17 @@ ImagingHorizontalBoxBlur(Imaging imOut, Imaging imIn, float floatRadius) { ww, fw ); - if (imIn == imOut) { - // Commit. - memcpy(imOut->image8[y], lineOut, imIn->xsize); - } + } + if (imIn == imOut) { + // Commit. + memcpy(imOut->image8[y], lineOut, imIn->xsize); } } } else { - if (radius) { - for (y = 0; y < imIn->ysize; y++) { - ImagingLineBoxBlur32( + for (y = 0; y < imIn->ysize; y++) { +#if defined(__SSE4__) + if (radius) { + ImagingLineBoxBlur32_SSE4( imIn == imOut ? (UINT32 *)lineOut : (UINT32 *)imOut->image32[y], (UINT32 *)imIn->image32[y], imIn->xsize - 1, @@ -349,14 +447,8 @@ ImagingHorizontalBoxBlur(Imaging imOut, Imaging imIn, float floatRadius) { ww, fw ); - if (imIn == imOut) { - // Commit. - memcpy(imOut->image32[y], lineOut, imIn->xsize * 4); - } - } - } else { - for (y = 0; y < imIn->ysize; y++) { - ImagingLineBoxBlurZero32( + } else { + ImagingLineBoxBlurZero32_SSE4( imIn == imOut ? (UINT32 *)lineOut : (UINT32 *)imOut->image32[y], (UINT32 *)imIn->image32[y], imIn->xsize - 1, @@ -365,10 +457,22 @@ ImagingHorizontalBoxBlur(Imaging imOut, Imaging imIn, float floatRadius) { ww, fw ); - if (imIn == imOut) { - // Commit. - memcpy(imOut->image32[y], lineOut, imIn->xsize * 4); - } + } +#else // defined(__SSE4__) + ImagingLineBoxBlur32( + imIn == imOut ? (pixel *)lineOut : (pixel *)imOut->image32[y], + (pixel *)imIn->image32[y], + imIn->xsize - 1, + radius, + edgeA, + edgeB, + ww, + fw + ); +#endif // defined(__SSE4__) + if (imIn == imOut) { + // Commit. + memcpy(imOut->image32[y], lineOut, imIn->xsize * 4); } } }