option to disable feather, since its so slow - for interactively editing masks its...
[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->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         lockMutex();
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, TRUE, this->do_smooth, this->do_feather);
79                 if (this->do_smooth) {
80                         PLX_antialias_buffer(buffer, width, height);
81                 }
82
83                 this->rasterizedMask = buffer;
84         }
85         unlockMutex();
86         return this->rasterizedMask;
87 }
88
89 void MaskOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
90 {
91         if (maskWidth == 0 || maskHeight == 0) {
92                 NodeOperation::determineResolution(resolution, preferredResolution);
93         }
94         else {
95                 unsigned int nr[2];
96
97                 nr[0] = maskWidth;
98                 nr[1] = maskHeight;
99
100                 NodeOperation::determineResolution(resolution, nr);
101
102                 resolution[0] = maskWidth;
103                 resolution[1] = maskHeight;
104         }
105 }
106
107 void MaskOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)
108 {
109         if (!data) {
110                 color[0] = 0.0f;
111         }
112         else {
113                 float *buffer = (float *) data;
114                 int index = (y * this->getWidth() + x);
115
116                 color[0] = buffer[index];
117         }
118 }