Mask node: create a copy of layers to be rasterized in initExecution
[blender.git] / source / blender / compositor / operations / COM_MaskOperation.cpp
1 /*
2  * Copyright 2012, 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  *              Sergey Sharybin
22  */
23
24 #include "COM_MaskOperation.h"
25
26 #include "MEM_guardedalloc.h"
27
28 #include "BLI_listbase.h"
29 #include "BLI_math.h"
30
31 #include "DNA_scene_types.h"
32
33 extern "C" {
34         #include "BKE_mask.h"
35         #include "../../../../intern/raskter/raskter.h"
36 }
37
38 MaskOperation::MaskOperation() : NodeOperation()
39 {
40         this->addOutputSocket(COM_DT_VALUE);
41         this->m_mask = NULL;
42         this->m_maskWidth = 0;
43         this->m_maskHeight = 0;
44         this->m_framenumber = 0;
45         this->m_rasterizedMask = NULL;
46         setComplex(true);
47 }
48
49 void MaskOperation::initExecution()
50 {
51         initMutex();
52
53         this->m_rasterizedMask = NULL;
54         this->m_maskLayers.first = this->m_maskLayers.last = NULL;
55
56         if (this->m_mask) {
57                 BKE_mask_layer_copy_list(&this->m_maskLayers, &this->m_mask->masklayers);
58         }
59 }
60
61 void MaskOperation::deinitExecution()
62 {
63         BKE_mask_layer_free_list(&this->m_maskLayers);
64
65         if (this->m_rasterizedMask) {
66                 MEM_freeN(this->m_rasterizedMask);
67                 this->m_rasterizedMask = NULL;
68         }
69 }
70
71 void *MaskOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers)
72 {
73         if (this->m_rasterizedMask)
74                 return this->m_rasterizedMask;
75
76         if (!this->m_mask)
77                 return NULL;
78
79         lockMutex();
80         if (this->m_rasterizedMask == NULL) {
81                 int width = this->getWidth();
82                 int height = this->getHeight();
83                 float *buffer;
84
85                 buffer = (float *)MEM_callocN(sizeof(float) * width * height, "rasterized mask");
86
87                 BKE_mask_rasterize_layers(&this->m_maskLayers, width, height, buffer, TRUE,
88                                           this->m_do_smooth, this->m_do_feather);
89
90                 if (this->m_do_smooth) {
91                         PLX_antialias_buffer(buffer, width, height);
92                 }
93
94                 this->m_rasterizedMask = buffer;
95         }
96         unlockMutex();
97         return this->m_rasterizedMask;
98 }
99
100 void MaskOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
101 {
102         if (this->m_maskWidth == 0 || this->m_maskHeight == 0) {
103                 NodeOperation::determineResolution(resolution, preferredResolution);
104         }
105         else {
106                 unsigned int nr[2];
107
108                 nr[0] = this->m_maskWidth;
109                 nr[1] = this->m_maskHeight;
110
111                 NodeOperation::determineResolution(resolution, nr);
112
113                 resolution[0] = this->m_maskWidth;
114                 resolution[1] = this->m_maskHeight;
115         }
116 }
117
118 void MaskOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)
119 {
120         if (!data) {
121                 color[0] = 0.0f;
122         }
123         else {
124                 float *buffer = (float *) data;
125                 int index = (y * this->getWidth() + x);
126
127                 color[0] = buffer[index];
128         }
129 }