6715d50bb95df77492c811c47e63feefedf10643
[blender.git] / source / blender / compositor / operations / COM_GlareBaseOperation.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_GlareBaseOperation.h"
24 #include "BLI_math.h"
25
26 GlareBaseOperation::GlareBaseOperation(): NodeOperation()
27 {
28         this->addInputSocket(COM_DT_COLOR);
29         this->addOutputSocket(COM_DT_COLOR);
30         this->settings = NULL;
31         this->cachedInstance = NULL;
32         setComplex(true);
33 }
34 void GlareBaseOperation::initExecution()
35 {
36         initMutex();
37         this->inputProgram = getInputSocketReader(0);
38         this->cachedInstance = NULL;
39 }
40
41 void GlareBaseOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)\
42 {
43         float *buffer = (float*) data;
44         int index = (y*this->getWidth() + x) * COM_NUMBER_OF_CHANNELS;
45         color[0] = buffer[index];
46         color[1] = buffer[index+1];
47         color[2] = buffer[index+2];
48         color[3] = buffer[index+3];
49 }
50
51 void GlareBaseOperation::deinitExecution()
52 {
53         deinitMutex();
54         this->inputProgram = NULL;
55         if (this->cachedInstance) {
56                 delete cachedInstance;
57                 this->cachedInstance = NULL;
58         }
59 }
60 void *GlareBaseOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers)
61 {
62         BLI_mutex_lock(getMutex());
63         if (this->cachedInstance == NULL) {
64                 MemoryBuffer *tile = (MemoryBuffer*)inputProgram->initializeTileData(rect, memoryBuffers);
65                 float *data = new float[this->getWidth()*this->getHeight()*COM_NUMBER_OF_CHANNELS];
66                 this->generateGlare(data, tile, this->settings);
67                 this->cachedInstance = data;
68         }
69         BLI_mutex_unlock(getMutex());
70         return this->cachedInstance;
71 }
72
73 bool GlareBaseOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
74 {
75         rcti newInput;
76         newInput.xmax = this->getWidth();
77         newInput.xmin = 0;
78         newInput.ymax = this->getHeight();
79         newInput.ymin = 0;
80         return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
81 }