Merged changes in the trunk up to revision 48893.
[blender-staging.git] / source / blender / compositor / operations / COM_GaussianBokehBlurOperation.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor: 
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23 #include "COM_GaussianBokehBlurOperation.h"
24 #include "BLI_math.h"
25
26 extern "C" {
27         #include "RE_pipeline.h"
28 }
29
30 GaussianBokehBlurOperation::GaussianBokehBlurOperation() : BlurBaseOperation(COM_DT_COLOR)
31 {
32         this->m_gausstab = NULL;
33 }
34
35 void *GaussianBokehBlurOperation::initializeTileData(rcti *rect)
36 {
37         lockMutex();
38         if (!this->m_sizeavailable) {
39                 updateGauss();
40         }
41         void *buffer = getInputOperation(0)->initializeTileData(NULL);
42         unlockMutex();
43         return buffer;
44 }
45
46 void GaussianBokehBlurOperation::initExecution()
47 {
48         BlurBaseOperation::initExecution();
49
50         initMutex();
51
52         if (this->m_sizeavailable) {
53                 updateGauss();
54         }
55 }
56
57 void GaussianBokehBlurOperation::updateGauss()
58 {
59         if (this->m_gausstab == NULL) {
60                 float radxf;
61                 float radyf;
62                 int n;
63                 float *dgauss;
64                 float *ddgauss;
65                 float val;
66                 int j, i;
67                 const float width = this->getWidth();
68                 const float height = this->getHeight();
69                 if (!this->m_sizeavailable) {
70                         updateSize();
71                 }
72                 radxf = this->m_size * (float)this->m_data->sizex;
73                 if (radxf > width / 2.0f)
74                         radxf = width / 2.0f;
75                 else if (radxf < 1.0f)
76                         radxf = 1.0f;
77         
78                 /* vertical */
79                 radyf = this->m_size * (float)this->m_data->sizey;
80                 if (radyf > height / 2.0f)
81                         radyf = height / 2.0f;
82                 else if (radyf < 1.0f)
83                         radyf = 1.0f;
84         
85                 this->m_radx = ceil(radxf);
86                 this->m_rady = ceil(radyf);
87         
88                 n = (2 * this->m_radx + 1) * (2 * this->m_rady + 1);
89         
90                 /* create a full filter image */
91                 ddgauss = new float[n];
92                 dgauss = ddgauss;
93                 val = 0.0f;
94                 for (j = -this->m_rady; j <= this->m_rady; j++) {
95                         for (i = -this->m_radx; i <= this->m_radx; i++, dgauss++) {
96                                 float fj = (float)j / radyf;
97                                 float fi = (float)i / radxf;
98                                 float dist = sqrt(fj * fj + fi * fi);
99                                 *dgauss = RE_filter_value(this->m_data->filtertype, dist);
100                                 
101                                 val += *dgauss;
102                         }
103                 }
104                 if (val != 0.0f) {
105                         val = 1.0f / val;
106                         for (j = n - 1; j >= 0; j--)
107                                 ddgauss[j] *= val;
108                 }
109                 else ddgauss[4] = 1.0f;
110                 
111                 this->m_gausstab = ddgauss;
112         }
113 }
114
115 void GaussianBokehBlurOperation::executePixel(float *color, int x, int y, void *data)
116 {
117         float tempColor[4];
118         tempColor[0] = 0;
119         tempColor[1] = 0;
120         tempColor[2] = 0;
121         tempColor[3] = 0;
122         float multiplier_accum = 0;
123         MemoryBuffer *inputBuffer = (MemoryBuffer *)data;
124         float *buffer = inputBuffer->getBuffer();
125         int bufferwidth = inputBuffer->getWidth();
126         int bufferstartx = inputBuffer->getRect()->xmin;
127         int bufferstarty = inputBuffer->getRect()->ymin;
128
129         int miny = y - this->m_rady;
130         int maxy = y + this->m_rady;
131         int minx = x - this->m_radx;
132         int maxx = x + this->m_radx;
133         miny = max(miny, inputBuffer->getRect()->ymin);
134         minx = max(minx, inputBuffer->getRect()->xmin);
135         maxy = min(maxy, inputBuffer->getRect()->ymax);
136         maxx = min(maxx, inputBuffer->getRect()->xmax);
137
138         int index;
139         int step = QualityStepHelper::getStep();
140         int offsetadd = QualityStepHelper::getOffsetAdd();
141         const int addConst = (minx - x + this->m_radx);
142         const int mulConst = (this->m_radx * 2 + 1);
143         for (int ny = miny; ny < maxy; ny += step) {
144                 index = ((ny - y) + this->m_rady) * mulConst + addConst;
145                 int bufferindex = ((minx - bufferstartx) * 4) + ((ny - bufferstarty) * 4 * bufferwidth);
146                 for (int nx = minx; nx < maxx; nx += step) {
147                         const float multiplier = this->m_gausstab[index];
148                         madd_v4_v4fl(tempColor, &buffer[bufferindex], multiplier);
149                         multiplier_accum += multiplier;
150                         index += step;
151                         bufferindex += offsetadd;
152                 }
153         }
154
155         mul_v4_v4fl(color, tempColor, 1.0f / multiplier_accum);
156 }
157
158 void GaussianBokehBlurOperation::deinitExecution()
159 {
160         BlurBaseOperation::deinitExecution();
161         delete [] this->m_gausstab;
162         this->m_gausstab = NULL;
163
164         deinitMutex();
165 }
166
167 bool GaussianBokehBlurOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
168 {
169         rcti newInput;
170         rcti sizeInput;
171         sizeInput.xmin = 0;
172         sizeInput.ymin = 0;
173         sizeInput.xmax = 5;
174         sizeInput.ymax = 5;
175         NodeOperation *operation = this->getInputOperation(1);
176         
177         if (operation->determineDependingAreaOfInterest(&sizeInput, readOperation, output)) {
178                 return true;
179         }
180         else {
181                 if (this->m_sizeavailable && this->m_gausstab != NULL) {
182                         newInput.xmin = 0;
183                         newInput.ymin = 0;
184                         newInput.xmax = this->getWidth();
185                         newInput.ymax = this->getHeight();
186                 }
187                 else {
188                         int addx = this->m_radx;
189                         int addy = this->m_rady;
190                         newInput.xmax = input->xmax + addx;
191                         newInput.xmin = input->xmin - addx;
192                         newInput.ymax = input->ymax + addy;
193                         newInput.ymin = input->ymin - addy;
194
195                 }
196                 return BlurBaseOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
197         }
198 }
199
200 // reference image
201 GaussianBokehBlurReferenceOperation::GaussianBokehBlurReferenceOperation() : NodeOperation()
202 {
203         this->addInputSocket(COM_DT_COLOR);
204         this->addInputSocket(COM_DT_VALUE);
205         this->addOutputSocket(COM_DT_COLOR);
206         this->setComplex(true);
207         this->m_gausstab = NULL;
208         this->m_inputImage = NULL;
209         this->m_inputSize = NULL;
210 }
211
212 void *GaussianBokehBlurReferenceOperation::initializeTileData(rcti *rect)
213 {
214         void *buffer = getInputOperation(0)->initializeTileData(NULL);
215         return buffer;
216 }
217
218 void GaussianBokehBlurReferenceOperation::initExecution()
219 {
220         // setup gaustab
221         this->m_data->image_in_width = this->getWidth();
222         this->m_data->image_in_height = this->getHeight();
223         if (this->m_data->relative) {
224                 switch (this->m_data->aspect) {
225                         case CMP_NODE_BLUR_ASPECT_NONE:
226                                 this->m_data->sizex = (int)(this->m_data->percentx * 0.01f * this->m_data->image_in_width);
227                                 this->m_data->sizey = (int)(this->m_data->percenty * 0.01f * this->m_data->image_in_height);
228                                 break;
229                         case CMP_NODE_BLUR_ASPECT_Y:
230                                 this->m_data->sizex = (int)(this->m_data->percentx * 0.01f * this->m_data->image_in_width);
231                                 this->m_data->sizey = (int)(this->m_data->percenty * 0.01f * this->m_data->image_in_width);
232                                 break;
233                         case CMP_NODE_BLUR_ASPECT_X:
234                                 this->m_data->sizex = (int)(this->m_data->percentx * 0.01f * this->m_data->image_in_height);
235                                 this->m_data->sizey = (int)(this->m_data->percenty * 0.01f * this->m_data->image_in_height);
236                                 break;
237                 }
238         }
239         
240         updateGauss();
241         this->m_inputImage = this->getInputSocketReader(0);
242         this->m_inputSize = this->getInputSocketReader(1);
243 }
244
245 void GaussianBokehBlurReferenceOperation::updateGauss()
246 {
247         int n;
248         float *dgauss;
249         float *ddgauss;
250         int j, i;
251
252         n = (2 * radx + 1) * (2 * rady + 1);
253
254         /* create a full filter image */
255         ddgauss = new float[n];
256         dgauss = ddgauss;
257         for (j = -rady; j <= rady; j++) {
258                 for (i = -radx; i <= radx; i++, dgauss++) {
259                         float fj = (float)j / radyf;
260                         float fi = (float)i / radxf;
261                         float dist = sqrt(fj * fj + fi * fi);
262                         *dgauss = RE_filter_value(this->m_data->filtertype, dist);
263                 }
264         }
265         this->m_gausstab = ddgauss;
266 }
267
268 void GaussianBokehBlurReferenceOperation::executePixel(float *color, int x, int y, void *data)
269 {
270         float tempColor[4];
271         float tempSize[4];
272         tempColor[0] = 0;
273         tempColor[1] = 0;
274         tempColor[2] = 0;
275         tempColor[3] = 0;
276         float multiplier_accum = 0;
277         MemoryBuffer *inputBuffer = (MemoryBuffer *)data;
278         float *buffer = inputBuffer->getBuffer();
279         int bufferwidth = inputBuffer->getWidth();
280         int bufferstartx = inputBuffer->getRect()->xmin;
281         int bufferstarty = inputBuffer->getRect()->ymin;
282         this->m_inputSize->read(tempSize, x, y, data);
283         float size = tempSize[0];
284         CLAMP(size, 0.0f, 1.0f);
285         float sizeX = ceil(this->m_data->sizex * size);
286         float sizeY = ceil(this->m_data->sizey * size);
287
288         if (sizeX <= 0.5f && sizeY <= 0.5f) {
289                 this->m_inputImage->read(color, x, y, data);
290                 return;
291         }
292         
293         int miny = y - sizeY;
294         int maxy = y + sizeY;
295         int minx = x - sizeX;
296         int maxx = x + sizeX;
297         miny = max(miny, inputBuffer->getRect()->ymin);
298         minx = max(minx, inputBuffer->getRect()->xmin);
299         maxy = min(maxy, inputBuffer->getRect()->ymax);
300         maxx = min(maxx, inputBuffer->getRect()->xmax);
301
302         int step = QualityStepHelper::getStep();
303         int offsetadd = QualityStepHelper::getOffsetAdd();
304         for (int ny = miny; ny < maxy; ny += step) {
305                 int u = ny - y;
306                 float uf = ((u/sizeY)*radyf)+radyf;
307                 int indexu = uf * (radx*2+1);
308                 int bufferindex = ((minx - bufferstartx) * 4) + ((ny - bufferstarty) * 4 * bufferwidth);
309                 for (int nx = minx; nx < maxx; nx += step) {
310                         int v = nx - x;
311                         float vf = ((v/sizeX)*radxf)+radxf;
312                         int index = indexu + vf;
313                         const float multiplier = this->m_gausstab[index];
314                         madd_v4_v4fl(tempColor, &buffer[bufferindex], multiplier);
315                         multiplier_accum += multiplier;
316                         index += step;
317                         bufferindex += offsetadd;
318                 }
319         }
320
321         mul_v4_v4fl(color, tempColor, 1.0f / multiplier_accum);
322 }
323
324 void GaussianBokehBlurReferenceOperation::deinitExecution()
325 {
326         delete [] this->m_gausstab;
327         this->m_gausstab = NULL;
328         this->m_inputImage = NULL;
329         this->m_inputSize = NULL;
330         
331 }
332
333 bool GaussianBokehBlurReferenceOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
334 {
335         rcti newInput;
336         NodeOperation *operation = this->getInputOperation(1);
337         
338         if (operation->determineDependingAreaOfInterest(input, readOperation, output)) {
339                 return true;
340         }
341         else {
342                 int addx = this->m_data->sizex+2;
343                 int addy = this->m_data->sizey+2;
344                 newInput.xmax = input->xmax + addx;
345                 newInput.xmin = input->xmin - addx;
346                 newInput.ymax = input->ymax + addy;
347                 newInput.ymin = input->ymin - addy;
348                 return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
349         }
350 }
351