remove references to raskter from compositor and BKE mask.
[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->addOutputSocket(COM_DT_VALUE);
40         this->m_mask = NULL;
41         this->m_maskWidth = 0;
42         this->m_maskHeight = 0;
43         this->m_maskWidthInv = 0.0f;
44         this->m_maskHeightInv = 0.0f;
45         this->m_frame_shutter = 0.0f;
46         this->m_frame_number = 0;
47         this->m_rasterMaskHandleTot = 1;
48         memset(this->m_rasterMaskHandles, 0, sizeof(this->m_rasterMaskHandles));
49 }
50
51 void MaskOperation::initExecution()
52 {
53         if (this->m_mask && this->m_rasterMaskHandles[0] == NULL) {
54                 if (this->m_rasterMaskHandleTot == 1) {
55                         this->m_rasterMaskHandles[0] = BKE_maskrasterize_handle_new();
56
57                         BKE_maskrasterize_handle_init(this->m_rasterMaskHandles[0], this->m_mask,
58                                 this->m_maskWidth, this->m_maskHeight,
59                                 TRUE, this->m_do_smooth, this->m_do_feather);
60                 }
61                 else {
62                         /* make a throw away copy of the mask */
63                         const float frame = (float)this->m_frame_number - this->m_frame_shutter;
64                         const float frame_step = (this->m_frame_shutter * 2.0f) / this->m_rasterMaskHandleTot;
65                         float frame_iter = frame;
66
67                         Mask *mask_temp;
68
69                         mask_temp = BKE_mask_copy_nolib(this->m_mask);
70
71                         /* trick so we can get unkeyed edits to display */
72                         {
73                                 MaskLayer *masklay;
74                                 MaskLayerShape *masklay_shape;
75
76                                 for (masklay = (MaskLayer *)mask_temp->masklayers.first;
77                                      masklay;
78                                      masklay = (MaskLayer *)masklay->next)
79                                 {
80                                         masklay_shape = BKE_mask_layer_shape_varify_frame(masklay, this->m_frame_number);
81                                         BKE_mask_layer_shape_from_mask(masklay, masklay_shape);
82                                 }
83                         }
84
85                         for (unsigned int i = 0; i < this->m_rasterMaskHandleTot; i++) {
86                                 this->m_rasterMaskHandles[i] = BKE_maskrasterize_handle_new();
87
88                                 /* re-eval frame info */
89                                 BKE_mask_evaluate(mask_temp, frame_iter, TRUE);
90
91                                 BKE_maskrasterize_handle_init(this->m_rasterMaskHandles[i], mask_temp,
92                                                               this->m_maskWidth, this->m_maskHeight,
93                                                               TRUE, this->m_do_smooth, this->m_do_feather);
94
95                                 frame_iter += frame_step;
96                         }
97
98                         BKE_mask_free(mask_temp);
99                         MEM_freeN(mask_temp);
100                 }
101         }
102 }
103
104 void MaskOperation::deinitExecution()
105 {
106         for (unsigned int i = 0; i < this->m_rasterMaskHandleTot; i++) {
107                 if (this->m_rasterMaskHandles[i]) {
108                         BKE_maskrasterize_handle_free(this->m_rasterMaskHandles[i]);
109                         this->m_rasterMaskHandles[i] = NULL;
110                 }
111         }
112 }
113
114 void MaskOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
115 {
116         if (this->m_maskWidth == 0 || this->m_maskHeight == 0) {
117                 NodeOperation::determineResolution(resolution, preferredResolution);
118         }
119         else {
120                 unsigned int nr[2];
121
122                 nr[0] = this->m_maskWidth;
123                 nr[1] = this->m_maskHeight;
124
125                 NodeOperation::determineResolution(resolution, nr);
126
127                 resolution[0] = this->m_maskWidth;
128                 resolution[1] = this->m_maskHeight;
129         }
130 }
131
132 void MaskOperation::executePixel(float *color, float x, float y, PixelSampler sampler)
133 {
134         const float xy[2] = {x * this->m_maskWidthInv, y * this->m_maskHeightInv};
135
136         if (this->m_rasterMaskHandleTot == 1) {
137                 if (this->m_rasterMaskHandles[0]) {
138                         color[0] = BKE_maskrasterize_handle_sample(this->m_rasterMaskHandles[0], xy);
139                 }
140                 else {
141                         color[0] = 0.0f;
142                 }
143         }
144         else {
145                 /* incase loop below fails */
146                 color[0] = 0.0f;
147
148                 for (unsigned int i = 0; i < this->m_rasterMaskHandleTot; i++) {
149                         if (this->m_rasterMaskHandles[i]) {
150                                 color[0] += BKE_maskrasterize_handle_sample(this->m_rasterMaskHandles[i], xy);
151                         }
152                 }
153
154                 /* until we get better falloff */
155                 color[0] /= this->m_rasterMaskHandleTot;
156         }
157 }