Fixed crash of mask node when mask is not set
[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 }
36
37 MaskOperation::MaskOperation(): NodeOperation()
38 {
39         this->addInputSocket(COM_DT_COLOR);
40         this->addOutputSocket(COM_DT_COLOR);
41         this->mask = NULL;
42         this->maskWidth = 0;
43         this->maskHeight = 0;
44         this->framenumber = 0;
45         this->rasterizedMask = NULL;
46         setComplex(true);
47 }
48
49 void MaskOperation::initExecution()
50 {
51         initMutex();
52         this->rasterizedMask = NULL;
53 }
54
55 void MaskOperation::deinitExecution()
56 {
57         if (this->rasterizedMask) {
58                 MEM_freeN(rasterizedMask);
59                 this->rasterizedMask = NULL;
60         }
61 }
62
63 void *MaskOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers)
64 {
65         if (this->rasterizedMask)
66                 return this->rasterizedMask;
67
68         if (!this->mask)
69                 return NULL;
70
71         BLI_mutex_lock(getMutex());
72         if (this->rasterizedMask == NULL) {
73                 int width = this->getWidth();
74                 int height = this->getHeight();
75                 float *buffer;
76
77                 buffer = (float *)MEM_callocN(sizeof(float) * width * height, "rasterized mask");
78                 BKE_mask_rasterize(mask, width, height, buffer);
79
80                 this->rasterizedMask = buffer;
81         }
82         BLI_mutex_unlock(getMutex());
83
84         return this->rasterizedMask;
85 }
86
87 void MaskOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
88 {
89         if (maskWidth == 0 || maskHeight == 0) {
90                 NodeOperation::determineResolution(resolution, preferredResolution);
91         }
92         else {
93                 unsigned int nr[2];
94
95                 nr[0] = maskWidth;
96                 nr[1] = maskHeight;
97
98                 NodeOperation::determineResolution(resolution, nr);
99
100                 resolution[0] = maskWidth;
101                 resolution[1] = maskHeight;
102         }
103 }
104
105 void MaskOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)
106 {
107         if (!data) {
108                 color[0] = 0;
109                 color[1] = 0;
110                 color[2] = 0;
111                 color[3] = 1.0f;
112         }
113         else {
114                 float *buffer = (float*) data;
115                 int index = (y * this->getWidth() + x);
116
117                 color[0] = buffer[index];
118                 color[1] = buffer[index];
119                 color[2] = buffer[index];
120                 color[3] = 1.0f;
121         }
122 }
123
124