Fix various compiler warnings.
[blender-staging.git] / source / blender / compositor / operations / COM_GaussianBokehBlurOperation.cpp
index a9bcb2dd75209a39eddda081ae10e6cf61f1b1fe..7c4132238e349016815eabd5782406a71f5b4e7f 100644 (file)
@@ -24,7 +24,7 @@
 #include "BLI_math.h"
 #include "MEM_guardedalloc.h"
 extern "C" {
-       #include "RE_pipeline.h"
+#  include "RE_pipeline.h"
 }
 
 GaussianBokehBlurOperation::GaussianBokehBlurOperation() : BlurBaseOperation(COM_DT_COLOR)
@@ -32,7 +32,7 @@ GaussianBokehBlurOperation::GaussianBokehBlurOperation() : BlurBaseOperation(COM
        this->m_gausstab = NULL;
 }
 
-void *GaussianBokehBlurOperation::initializeTileData(rcti *rect)
+void *GaussianBokehBlurOperation::initializeTileData(rcti * /*rect*/)
 {
        lockMutex();
        if (!this->m_sizeavailable) {
@@ -62,52 +62,53 @@ void GaussianBokehBlurOperation::updateGauss()
                int n;
                float *dgauss;
                float *ddgauss;
-               float val;
                int j, i;
                const float width = this->getWidth();
                const float height = this->getHeight();
                if (!this->m_sizeavailable) {
                        updateSize();
                }
-               radxf = this->m_size * (float)this->m_data->sizex;
-               if (radxf > width / 2.0f)
-                       radxf = width / 2.0f;
-               else if (radxf < 1.0f)
-                       radxf = 1.0f;
+               radxf = this->m_size * (float)this->m_data.sizex;
+               CLAMP(radxf, 0.0f, width / 2.0f);
        
                /* vertical */
-               radyf = this->m_size * (float)this->m_data->sizey;
-               if (radyf > height / 2.0f)
-                       radyf = height / 2.0f;
-               else if (radyf < 1.0f)
-                       radyf = 1.0f;
+               radyf = this->m_size * (float)this->m_data.sizey;
+               CLAMP(radyf, 0.0f, height / 2.0f);
        
                this->m_radx = ceil(radxf);
                this->m_rady = ceil(radyf);
-       
-               n = (2 * this->m_radx + 1) * (2 * this->m_rady + 1);
+               
+               int ddwidth = 2 * this->m_radx + 1;
+               int ddheight = 2 * this->m_rady + 1;
+               n = ddwidth * ddheight;
        
                /* create a full filter image */
                ddgauss = (float *)MEM_mallocN(sizeof(float) * n, __func__);
                dgauss = ddgauss;
-               val = 0.0f;
+               float sum = 0.0f;
+               float facx = (radxf > 0.0f ? 1.0f / radxf : 0.0f);
+               float facy = (radyf > 0.0f ? 1.0f / radyf : 0.0f);
                for (j = -this->m_rady; j <= this->m_rady; j++) {
                        for (i = -this->m_radx; i <= this->m_radx; i++, dgauss++) {
-                               float fj = (float)j / radyf;
-                               float fi = (float)i / radxf;
+                               float fj = (float)j * facy;
+                               float fi = (float)i * facx;
                                float dist = sqrt(fj * fj + fi * fi);
-                               *dgauss = RE_filter_value(this->m_data->filtertype, dist);
+                               *dgauss = RE_filter_value(this->m_data.filtertype, dist);
                                
-                               val += *dgauss;
+                               sum += *dgauss;
                        }
                }
-               if (val != 0.0f) {
-                       val = 1.0f / val;
-                       for (j = n - 1; j >= 0; j--) {
-                               ddgauss[j] *= val;
-                       }
+
+               if (sum > 0.0f) {
+                       /* normalize */
+                       float norm = 1.0f / sum;
+                       for (j = n - 1; j >= 0; j--)
+                               ddgauss[j] *= norm;
+               }
+               else {
+                       int center = m_rady * ddwidth + m_radx;
+                       ddgauss[center] = 1.0f;
                }
-               else ddgauss[4] = 1.0f;
                
                this->m_gausstab = ddgauss;
        }
@@ -127,24 +128,21 @@ void GaussianBokehBlurOperation::executePixel(float output[4], int x, int y, voi
        int bufferstartx = inputBuffer->getRect()->xmin;
        int bufferstarty = inputBuffer->getRect()->ymin;
 
-       int miny = y - this->m_rady;
-       int maxy = y + this->m_rady;
-       int minx = x - this->m_radx;
-       int maxx = x + this->m_radx;
-       miny = max(miny, inputBuffer->getRect()->ymin);
-       minx = max(minx, inputBuffer->getRect()->xmin);
-       maxy = min(maxy, inputBuffer->getRect()->ymax);
-       maxx = min(maxx, inputBuffer->getRect()->xmax);
+       rcti &rect = *inputBuffer->getRect();
+       int ymin = max_ii(y - this->m_rady, rect.ymin);
+       int ymax = min_ii(y + this->m_rady + 1,  rect.ymax);
+       int xmin = max_ii(x - this->m_radx, rect.xmin);
+       int xmax = min_ii(x + this->m_radx + 1,  rect.xmax);
 
        int index;
        int step = QualityStepHelper::getStep();
        int offsetadd = QualityStepHelper::getOffsetAdd();
-       const int addConst = (minx - x + this->m_radx);
+       const int addConst = (xmin - x + this->m_radx);
        const int mulConst = (this->m_radx * 2 + 1);
-       for (int ny = miny; ny < maxy; ny += step) {
+       for (int ny = ymin; ny < ymax; ny += step) {
                index = ((ny - y) + this->m_rady) * mulConst + addConst;
-               int bufferindex = ((minx - bufferstartx) * 4) + ((ny - bufferstarty) * 4 * bufferwidth);
-               for (int nx = minx; nx < maxx; nx += step) {
+               int bufferindex = ((xmin - bufferstartx) * 4) + ((ny - bufferstarty) * 4 * bufferwidth);
+               for (int nx = xmin; nx < xmax; nx += step) {
                        const float multiplier = this->m_gausstab[index];
                        madd_v4_v4fl(tempColor, &buffer[bufferindex], multiplier);
                        multiplier_accum += multiplier;
@@ -159,8 +157,11 @@ void GaussianBokehBlurOperation::executePixel(float output[4], int x, int y, voi
 void GaussianBokehBlurOperation::deinitExecution()
 {
        BlurBaseOperation::deinitExecution();
-       MEM_freeN(this->m_gausstab);
-       this->m_gausstab = NULL;
+
+       if (this->m_gausstab) {
+               MEM_freeN(this->m_gausstab);
+               this->m_gausstab = NULL;
+       }
 
        deinitMutex();
 }
@@ -204,7 +205,7 @@ GaussianBlurReferenceOperation::GaussianBlurReferenceOperation() : BlurBaseOpera
        this->m_maintabs = NULL;
 }
 
-void *GaussianBlurReferenceOperation::initializeTileData(rcti *rect)
+void *GaussianBlurReferenceOperation::initializeTileData(rcti * /*rect*/)
 {
        void *buffer = getInputOperation(0)->initializeTileData(NULL);
        return buffer;
@@ -214,53 +215,53 @@ void GaussianBlurReferenceOperation::initExecution()
 {
        BlurBaseOperation::initExecution();
        // setup gaustab
-       this->m_data->image_in_width = this->getWidth();
-       this->m_data->image_in_height = this->getHeight();
-       if (this->m_data->relative) {
-               switch (this->m_data->aspect) {
+       this->m_data.image_in_width = this->getWidth();
+       this->m_data.image_in_height = this->getHeight();
+       if (this->m_data.relative) {
+               switch (this->m_data.aspect) {
                        case CMP_NODE_BLUR_ASPECT_NONE:
-                               this->m_data->sizex = (int)(this->m_data->percentx * 0.01f * this->m_data->image_in_width);
-                               this->m_data->sizey = (int)(this->m_data->percenty * 0.01f * this->m_data->image_in_height);
+                               this->m_data.sizex = (int)(this->m_data.percentx * 0.01f * this->m_data.image_in_width);
+                               this->m_data.sizey = (int)(this->m_data.percenty * 0.01f * this->m_data.image_in_height);
                                break;
                        case CMP_NODE_BLUR_ASPECT_Y:
-                               this->m_data->sizex = (int)(this->m_data->percentx * 0.01f * this->m_data->image_in_width);
-                               this->m_data->sizey = (int)(this->m_data->percenty * 0.01f * this->m_data->image_in_width);
+                               this->m_data.sizex = (int)(this->m_data.percentx * 0.01f * this->m_data.image_in_width);
+                               this->m_data.sizey = (int)(this->m_data.percenty * 0.01f * this->m_data.image_in_width);
                                break;
                        case CMP_NODE_BLUR_ASPECT_X:
-                               this->m_data->sizex = (int)(this->m_data->percentx * 0.01f * this->m_data->image_in_height);
-                               this->m_data->sizey = (int)(this->m_data->percenty * 0.01f * this->m_data->image_in_height);
+                               this->m_data.sizex = (int)(this->m_data.percentx * 0.01f * this->m_data.image_in_height);
+                               this->m_data.sizey = (int)(this->m_data.percenty * 0.01f * this->m_data.image_in_height);
                                break;
                }
        }
        
        
        /* horizontal */
-       m_radx = (float)this->m_data->sizex;
-       int imgx = getWidth()/2;
-       if (m_radx > imgx)
-               m_radx = imgx;
-       else if (m_radx < 1)
-               m_radx = 1;
-       m_radxf = (float)m_radx;
+       m_filtersizex = (float)this->m_data.sizex;
+       int imgx = getWidth() / 2;
+       if (m_filtersizex > imgx)
+               m_filtersizex = imgx;
+       else if (m_filtersizex < 1)
+               m_filtersizex = 1;
+       m_radx = (float)m_filtersizex;
 
        /* vertical */
-       m_rady = (float)this->m_data->sizey;
-       int imgy = getHeight()/2;
-       if (m_rady > imgy)
-               m_rady = imgy;
-       else if (m_rady < 1)
-               m_rady = 1;
-       m_radyf = (float)m_rady;
+       m_filtersizey = (float)this->m_data.sizey;
+       int imgy = getHeight() / 2;
+       if (m_filtersizey > imgy)
+               m_filtersizey = imgy;
+       else if (m_filtersizey < 1)
+               m_filtersizey = 1;
+       m_rady = (float)m_filtersizey;
        updateGauss();
 }
 
 void GaussianBlurReferenceOperation::updateGauss()
 {
        int i;
-       int x = MAX2(m_radx, m_rady);
-       this->m_maintabs = (float **)MEM_mallocN(x * sizeof(float *), "gauss array");
+       int x = max(m_filtersizex, m_filtersizey);
+       m_maintabs = (float **)MEM_mallocN(x * sizeof(float *), "gauss array");
        for (i = 0; i < x; i++) {
-               m_maintabs[i] = make_gausstab(i + 1);
+               m_maintabs[i] = make_gausstab(i + 1, i + 1);
        }
 }
 
@@ -272,18 +273,18 @@ void GaussianBlurReferenceOperation::executePixel(float output[4], int x, int y,
        float *gausstaby, *gausstabcentx;
        int i, j;
        float *src;
-       register float sum, val;
+       float sum, val;
        float rval, gval, bval, aval;
        int imgx = getWidth();
        int imgy = getHeight();
        float tempSize[4];
        this->m_inputSize->read(tempSize, x, y, data);
        float refSize = tempSize[0];
-       int refradx = (int)(refSize * m_radxf);
-       int refrady = (int)(refSize * m_radyf);
-       if (refradx > m_radx) refradx = m_radx;
+       int refradx = (int)(refSize * m_radx);
+       int refrady = (int)(refSize * m_rady);
+       if (refradx > m_filtersizex) refradx = m_filtersizex;
        else if (refradx < 1) refradx = 1;
-       if (refrady > m_rady) refrady = m_rady;
+       if (refrady > m_filtersizey) refrady = m_filtersizey;
        else if (refrady < 1) refrady = 1;
 
        if (refradx == 1 && refrady == 1) {
@@ -295,7 +296,7 @@ void GaussianBlurReferenceOperation::executePixel(float output[4], int x, int y,
                int minyr = y - refrady < 0 ? -y : -refrady;
                int maxyr = y + refrady > imgy ? imgy - y : refrady;
 
-               float *srcd = buffer + COM_NUMBER_OF_CHANNELS * ( (y + minyr) * imgx + x + minxr);
+               float *srcd = buffer + COM_NUM_CHANNELS_COLOR * ( (y + minyr) * imgx + x + minxr);
 
                gausstabx = m_maintabs[refradx - 1];
                gausstabcentx = gausstabx + refradx;
@@ -303,9 +304,9 @@ void GaussianBlurReferenceOperation::executePixel(float output[4], int x, int y,
                gausstabcenty = gausstaby + refrady;
 
                sum = gval = rval = bval = aval = 0.0f;
-               for (i = minyr; i < maxyr; i++, srcd += COM_NUMBER_OF_CHANNELS * imgx) {
+               for (i = minyr; i < maxyr; i++, srcd += COM_NUM_CHANNELS_COLOR * imgx) {
                        src = srcd;
-                       for (j = minxr; j < maxxr; j++, src += COM_NUMBER_OF_CHANNELS) {
+                       for (j = minxr; j < maxxr; j++, src += COM_NUM_CHANNELS_COLOR) {
                        
                                val = gausstabcenty[i] * gausstabcentx[j];
                                sum += val;
@@ -327,11 +328,11 @@ void GaussianBlurReferenceOperation::executePixel(float output[4], int x, int y,
 void GaussianBlurReferenceOperation::deinitExecution()
 {
        int x, i;
-       x = MAX2(m_radx, m_rady);
+       x = max(this->m_filtersizex, this->m_filtersizey);
        for (i = 0; i < x; i++) {
-               MEM_freeN(m_maintabs[i]);
+               MEM_freeN(this->m_maintabs[i]);
        }
-       MEM_freeN(m_maintabs);
+       MEM_freeN(this->m_maintabs);
        BlurBaseOperation::deinitExecution();
 }
 
@@ -344,8 +345,8 @@ bool GaussianBlurReferenceOperation::determineDependingAreaOfInterest(rcti *inpu
                return true;
        }
        else {
-               int addx = this->m_data->sizex+2;
-               int addy = this->m_data->sizey+2;
+               int addx = this->m_data.sizex + 2;
+               int addy = this->m_data.sizey + 2;
                newInput.xmax = input->xmax + addx;
                newInput.xmin = input->xmin - addx;
                newInput.ymax = input->ymax + addy;