boxblur: apply changes requested by @Floessie in code review

This commit is contained in:
Ingo Weyrich
2019-09-23 13:39:21 +02:00
parent ca162e8ffc
commit f03605b735

View File

@@ -20,6 +20,7 @@
#define _BOXBLUR_H_ #define _BOXBLUR_H_
#include <assert.h> #include <assert.h>
#include <memory>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
@@ -337,9 +338,10 @@ inline void boxblur (float** src, float** dst, int radius, int W, int H, bool mu
#pragma omp parallel if (multiThread) #pragma omp parallel if (multiThread)
#endif #endif
{ {
float* const buffer = new float[std::max(W, 8 * H)]; std::unique_ptr<float> buffer(new float[std::max(W, 8 * H)]);
//horizontal blur //horizontal blur
float* const lineBuffer = buffer; float* const lineBuffer = buffer.get();
#ifdef _OPENMP #ifdef _OPENMP
#pragma omp for #pragma omp for
#endif #endif
@@ -356,8 +358,9 @@ inline void boxblur (float** src, float** dst, int radius, int W, int H, bool mu
for (int col = 1; col <= radius; col++) { for (int col = 1; col <= radius; col++) {
lineBuffer[col] = src[row][col]; lineBuffer[col] = src[row][col];
dst[row][col] = tempval = (tempval * len + src[row][col + radius]) / (len + 1); tempval = (tempval * len + src[row][col + radius]) / (len + 1);
len ++; dst[row][col] = tempval;
++len;
} }
for (int col = radius + 1; col < W - radius; col++) { for (int col = radius + 1; col < W - radius; col++) {
@@ -367,15 +370,15 @@ inline void boxblur (float** src, float** dst, int radius, int W, int H, bool mu
for (int col = W - radius; col < W; col++) { for (int col = W - radius; col < W; col++) {
dst[row][col] = tempval = (tempval * len - lineBuffer[col - radius - 1]) / (len - 1); dst[row][col] = tempval = (tempval * len - lineBuffer[col - radius - 1]) / (len - 1);
len --; --len;
} }
} }
//vertical blur //vertical blur
#ifdef __SSE2__ #ifdef __SSE2__
vfloat (* const rowBuffer)[2] = (vfloat(*)[2]) buffer; vfloat (* const rowBuffer)[2] = (vfloat(*)[2]) buffer.get();
vfloat leninitv = F2V(radius + 1); const vfloat leninitv = F2V(radius + 1);
vfloat onev = F2V(1.f); const vfloat onev = F2V(1.f);
vfloat tempv, temp1v, lenv, lenp1v, lenm1v, rlenv; vfloat tempv, temp1v, lenv, lenp1v, lenm1v, rlenv;
#ifdef _OPENMP #ifdef _OPENMP
@@ -432,7 +435,7 @@ inline void boxblur (float** src, float** dst, int radius, int W, int H, bool mu
} }
#else #else
float (* const rowBuffer)[8] = (float(*)[8]) buffer; float (* const rowBuffer)[8] = (float(*)[8]) buffer.get();
#ifdef _OPENMP #ifdef _OPENMP
#pragma omp for nowait #pragma omp for nowait
#endif #endif
@@ -440,12 +443,12 @@ inline void boxblur (float** src, float** dst, int radius, int W, int H, bool mu
for (int col = 0; col < W - numCols + 1; col += 8) { for (int col = 0; col < W - numCols + 1; col += 8) {
float len = radius + 1; float len = radius + 1;
for(int k = 0; k < numCols; k++) { for (int k = 0; k < numCols; k++) {
rowBuffer[0][k] = dst[0][col + k]; rowBuffer[0][k] = dst[0][col + k];
} }
for (int i = 1; i <= radius; i++) { for (int i = 1; i <= radius; i++) {
for(int k = 0; k < numCols; k++) { for (int k = 0; k < numCols; k++) {
dst[0][col + k] += dst[i][col + k]; dst[0][col + k] += dst[i][col + k];
} }
} }
@@ -488,7 +491,7 @@ inline void boxblur (float** src, float** dst, int radius, int W, int H, bool mu
const int remaining = W % numCols; const int remaining = W % numCols;
if (remaining > 0) { if (remaining > 0) {
float (* const rowBuffer)[8] = (float(*)[8]) buffer; float (* const rowBuffer)[8] = (float(*)[8]) buffer.get();
const int col = W - remaining; const int col = W - remaining;
float len = radius + 1; float len = radius + 1;
@@ -525,7 +528,6 @@ inline void boxblur (float** src, float** dst, int radius, int W, int H, bool mu
} }
} }
} }
delete [] buffer;
} }
} }