style cleanup: use this->m_* for private members.
[blender.git] / source / blender / compositor / operations / COM_InpaintOperation.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: Peter Schlaile
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23 #include "COM_InpaintOperation.h"
24 #include "BLI_math.h"
25 #include "COM_OpenCLDevice.h"
26
27 // Inpaint (simple convolve using average of known pixels)
28 InpaintSimpleOperation::InpaintSimpleOperation() : NodeOperation()
29 {
30         this->addInputSocket(COM_DT_COLOR);
31         this->addOutputSocket(COM_DT_COLOR);
32         this->setComplex(true);
33         this->m_inputImageProgram = NULL;
34         this->m_pixelorder = NULL;
35         this->m_manhatten_distance = NULL;
36         this->m_cached_buffer = NULL;
37         this->m_cached_buffer_ready = false;
38 }
39 void InpaintSimpleOperation::initExecution()
40 {
41         this->m_inputImageProgram = this->getInputSocketReader(0);
42
43         this->m_cached_buffer = NULL;
44         this->m_pixelorder = NULL;
45         this->m_manhatten_distance = NULL;
46         this->m_cached_buffer = NULL;
47         this->m_cached_buffer_ready = false;
48
49         this->initMutex();
50 }
51
52 void InpaintSimpleOperation::clamp_xy(int & x, int & y) 
53 {
54         int width = this->getWidth();
55         int height = this->getHeight();
56
57         if (x < 0) {
58                 x = 0;
59         }
60         if (x >= width) {
61                 x = width - 1;
62         }
63         if (y < 0) {
64                 y = 0;
65         }
66         if (y >= height) {
67                 y = height - 1;
68         }
69 }
70
71 float InpaintSimpleOperation::get(int x, int y, int component) 
72 {
73         int width = this->getWidth();
74
75         clamp_xy(x, y);
76         return this->m_cached_buffer[
77                    y * width * COM_NUMBER_OF_CHANNELS
78                    + x * COM_NUMBER_OF_CHANNELS + component];
79 }
80
81 void InpaintSimpleOperation::set(int x, int y, int component, float v) 
82 {
83         int width = this->getWidth();
84
85         this->m_cached_buffer[
86             y * width * COM_NUMBER_OF_CHANNELS
87             + x * COM_NUMBER_OF_CHANNELS + component] = v;
88 }
89
90 int InpaintSimpleOperation::mdist(int x, int y) 
91 {
92         int width = this->getWidth();
93         clamp_xy(x, y);
94         return this->m_manhatten_distance[y * width + x];
95 }
96
97 bool InpaintSimpleOperation::next_pixel(int & x, int & y, int & curr, int iters)
98 {
99         int width = this->getWidth();
100
101         if (curr >= this->m_area_size) {
102                 return false;
103         }
104         
105         int r = this->m_pixelorder[curr++];
106
107         x = r % width;
108         y = r / width;
109
110         if (mdist(x, y) > iters) {
111                 return false;
112         }
113         
114         return true;
115 }
116
117 void InpaintSimpleOperation::calc_manhatten_distance() 
118 {
119         int width = this->getWidth();
120         int height = this->getHeight();
121         short *m = this->m_manhatten_distance = new short[width * height];
122         int offsets[width + height + 1];
123
124         memset(offsets, 0, sizeof(offsets));
125
126         for (int j = 0; j < height; j++) {
127                 for (int i = 0; i < width; i++) {
128                         int r = 0;
129                         if (get(i, j, 3) < 1.0) {
130                                 r = width + height;
131                                 if (i > 0) 
132                                         r = MIN2(r, m[j * width + i - 1] + 1);
133                                 if (j > 0) 
134                                         r = MIN2(r, m[(j - 1) * width + i] + 1);
135                         }
136                         m[j * width + i] = r;
137                 }
138         }
139         
140         for (int j = height - 1; j >= 0; j--) {
141                 for (int i = width; i >= 0; i--) {
142                         int r = m[j * width + i];
143                         
144                         if (i + 1 < width) 
145                                 r = MIN2(r, m[j * width + i + 1] + 1);
146                         if (j + 1 < height) 
147                                 r = MIN2(r, m[(j + 1) * width + i] + 1);
148                         
149                         m[j * width + i] = r;
150                         
151                         offsets[r]++;
152                 }
153         }
154         
155         offsets[0] = 0;
156         
157         for (int i = 1; i < width + height + 1; i++) {
158                 offsets[i] += offsets[i - 1];
159         }
160         
161         this->m_area_size = offsets[width + height];
162         this->m_pixelorder = new int[this->m_area_size];
163         
164         for (int i = 0; i < width * height; i++) {
165                 if (m[i] > 0) {
166                         this->m_pixelorder[offsets[m[i] - 1]++] = i;
167                 }
168         }
169 }
170
171 void InpaintSimpleOperation::pix_step(int x, int y)
172 {
173         int d = this->mdist(x, y);
174
175         float n = 0;
176
177         float pix[3];
178
179         memset(pix, 0, sizeof(pix));
180
181         for (int dx = -1; dx <= 1; dx++) {
182                 for (int dy = -1; dy <= 1; dy++) {
183                         if (dx != 0 && dy != 0 && 
184                             this->mdist(x + dx, y + dy) < d) {
185                                 float weight = 1.0f / sqrt(2);
186
187                                 if (dx == 0 || dy == 0) {
188                                         weight = 1;
189                                 }
190                                 
191                                 for (int c = 0; c < 3; c++) {
192                                         float fk = this->get(x + dx, y + dy, c);
193
194                                         pix[c] += fk * weight;
195                                 }
196                                 n += weight;
197                         }
198                 }
199         }
200
201         for (int c = 0; c < 3; c++) {
202                 this->set(x, y, c, pix[c] / n);
203         }
204 }
205
206 void *InpaintSimpleOperation::initializeTileData(rcti *rect)
207 {
208         if (this->m_cached_buffer_ready) {
209                 return this->m_cached_buffer;
210         }
211         lockMutex();
212         if (!this->m_cached_buffer_ready) {
213                 MemoryBuffer *buf = (MemoryBuffer *)this->m_inputImageProgram->initializeTileData(rect);
214
215                 this->m_cached_buffer = new float[this->getWidth() * this->getHeight() * COM_NUMBER_OF_CHANNELS];
216                 memcpy(this->m_cached_buffer, buf->getBuffer(), this->getWidth() * this->getHeight() * COM_NUMBER_OF_CHANNELS * sizeof(float));
217
218                 calc_manhatten_distance();
219
220                 int curr = 0;
221                 int x, y;
222
223         
224                 while (next_pixel(x, y, curr, this->m_iterations)) {
225                         pix_step(x, y);
226                 }
227                 this->m_cached_buffer_ready = true;
228         }
229
230         unlockMutex();
231         return this->m_cached_buffer;
232 }
233
234 void InpaintSimpleOperation::executePixel(float *color, int x, int y, void *data)
235 {
236         for (int c = 0; c < 3; c++) {
237                 color[c] = get(x, y, c);
238         }
239         color[3] = 1.0f;
240 }
241
242 void InpaintSimpleOperation::deinitExecution()
243 {
244         this->m_inputImageProgram = NULL;
245         this->deinitMutex();
246         if (this->m_cached_buffer) {
247                 delete this->m_cached_buffer;
248                 this->m_cached_buffer = NULL;
249         }
250
251         if (this->m_pixelorder) {
252                 delete this->m_pixelorder;
253                 this->m_pixelorder = NULL;
254         }
255
256         if (this->m_manhatten_distance) {
257                 delete this->m_manhatten_distance;
258                 this->m_manhatten_distance = NULL;
259         }
260         this->m_cached_buffer_ready = false;
261 }
262
263 bool InpaintSimpleOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
264 {
265         if (this->m_cached_buffer_ready) {
266                 return false;
267         }
268         else {
269                 rcti newInput;
270         
271                 newInput.xmax = getWidth();
272                 newInput.xmin = 0;
273                 newInput.ymax = getHeight();
274                 newInput.ymin = 0;
275         
276                 return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
277         }
278 }
279