Removed parameter from executePixel and initializeTileData.
[blender.git] / source / blender / compositor / operations / COM_KeyingScreenOperation.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_KeyingScreenOperation.h"
25
26 #include "MEM_guardedalloc.h"
27
28 #include "BLI_listbase.h"
29 #include "BLI_math.h"
30 #include "BLI_math_color.h"
31
32 #include "DNA_scene_types.h"
33
34 extern "C" {
35         #include "BKE_movieclip.h"
36         #include "BKE_tracking.h"
37
38         #include "IMB_imbuf.h"
39         #include "IMB_imbuf_types.h"
40 }
41
42 KeyingScreenOperation::KeyingScreenOperation() : NodeOperation()
43 {
44         this->addOutputSocket(COM_DT_COLOR);
45         this->m_movieClip = NULL;
46         this->m_framenumber = 0;
47         this->m_trackingObject[0] = 0;
48         setComplex(true);
49 }
50
51 void KeyingScreenOperation::initExecution()
52 {
53         initMutex();
54         this->m_cachedTriangulation = NULL;
55 }
56
57 void KeyingScreenOperation::deinitExecution()
58 {
59         if (this->m_cachedTriangulation) {
60                 TriangulationData *triangulation = this->m_cachedTriangulation;
61
62                 if (triangulation->triangulated_points)
63                         MEM_freeN(triangulation->triangulated_points);
64
65                 if (triangulation->triangles)
66                         MEM_freeN(triangulation->triangles);
67
68                 if (triangulation->triangles_AABB)
69                         MEM_freeN(triangulation->triangles_AABB);
70
71                 MEM_freeN(this->m_cachedTriangulation);
72
73                 this->m_cachedTriangulation = NULL;
74         }
75 }
76
77 KeyingScreenOperation::TriangulationData *KeyingScreenOperation::buildVoronoiTriangulation()
78 {
79         MovieClipUser user = {0};
80         TriangulationData *triangulation;
81         MovieTracking *tracking = &this->m_movieClip->tracking;
82         MovieTrackingTrack *track;
83         VoronoiSite *sites;
84         ImBuf *ibuf;
85         ListBase *tracksbase;
86         ListBase edges = {NULL, NULL};
87         int sites_total;
88         int i;
89         int width = this->getWidth();
90         int height = this->getHeight();
91         int clip_frame = BKE_movieclip_remap_scene_to_clip_frame(this->m_movieClip, this->m_framenumber);
92
93         if (this->m_trackingObject[0]) {
94                 MovieTrackingObject *object = BKE_tracking_object_get_named(tracking, this->m_trackingObject);
95
96                 if (!object)
97                         return NULL;
98
99                 tracksbase = BKE_tracking_object_get_tracks(tracking, object);
100         }
101         else
102                 tracksbase = BKE_tracking_get_active_tracks(tracking);
103
104         /* count sites */
105         for (track = (MovieTrackingTrack *) tracksbase->first, sites_total = 0; track; track = track->next) {
106                 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, clip_frame);
107                 float pos[2];
108
109                 if (marker->flag & MARKER_DISABLED)
110                         continue;
111
112                 add_v2_v2v2(pos, marker->pos, track->offset);
113
114                 if (!IN_RANGE_INCL(pos[0], 0.0f, 1.0f) ||
115                     !IN_RANGE_INCL(pos[1], 0.0f, 1.0f))
116                 {
117                         continue;
118                 }
119
120                 sites_total++;
121         }
122
123         if (!sites_total)
124                 return NULL;
125
126         BKE_movieclip_user_set_frame(&user, clip_frame);
127         ibuf = BKE_movieclip_get_ibuf(this->m_movieClip, &user);
128
129         if (!ibuf)
130                 return NULL;
131
132         triangulation = (TriangulationData *) MEM_callocN(sizeof(TriangulationData), "keying screen triangulation data");
133
134         sites = (VoronoiSite *) MEM_callocN(sizeof(VoronoiSite) * sites_total, "keyingscreen voronoi sites");
135         track = (MovieTrackingTrack *) tracksbase->first;
136         for (track = (MovieTrackingTrack *) tracksbase->first, i = 0; track; track = track->next, i++) {
137                 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, clip_frame);
138                 VoronoiSite *site;
139                 ImBuf *pattern_ibuf;
140                 int j;
141                 float pos[2];
142
143                 if (marker->flag & MARKER_DISABLED)
144                         continue;
145
146                 add_v2_v2v2(pos, marker->pos, track->offset);
147
148                 if (!IN_RANGE_INCL(pos[0], 0.0f, 1.0f) ||
149                     !IN_RANGE_INCL(pos[1], 0.0f, 1.0f))
150                 {
151                         continue;
152                 }
153
154                 site = &sites[i];
155
156                 pattern_ibuf = BKE_tracking_get_pattern_imbuf(ibuf, track, marker, TRUE, FALSE);
157
158                 zero_v3(site->color);
159                 for (j = 0; j < pattern_ibuf->x * pattern_ibuf->y; j++) {
160                         if (pattern_ibuf->rect_float) {
161                                 add_v3_v3(site->color, &pattern_ibuf->rect_float[4 * j]);
162                         }
163                         else {
164                                 unsigned char *rrgb = (unsigned char *)pattern_ibuf->rect;
165
166                                 site->color[0] += srgb_to_linearrgb((float)rrgb[4 * j + 0] / 255.0f);
167                                 site->color[1] += srgb_to_linearrgb((float)rrgb[4 * j + 1] / 255.0f);
168                                 site->color[2] += srgb_to_linearrgb((float)rrgb[4 * j + 2] / 255.0f);
169                         }
170                 }
171
172                 mul_v3_fl(site->color, 1.0f / (pattern_ibuf->x * pattern_ibuf->y));
173                 IMB_freeImBuf(pattern_ibuf);
174
175                 site->co[0] = pos[0] * width;
176                 site->co[1] = pos[1] * height;
177         }
178
179         IMB_freeImBuf(ibuf);
180
181         BLI_voronoi_compute(sites, sites_total, width, height, &edges);
182
183         BLI_voronoi_triangulate(sites, sites_total, &edges, width, height,
184                                 &triangulation->triangulated_points, &triangulation->triangulated_points_total,
185                                 &triangulation->triangles, &triangulation->triangles_total);
186
187         MEM_freeN(sites);
188         BLI_freelistN(&edges);
189
190         if (triangulation->triangles_total) {
191                 rctf *rect;
192                 rect = triangulation->triangles_AABB =
193                         (rctf *) MEM_callocN(sizeof(rctf) * triangulation->triangles_total, "voronoi triangulation AABB");
194
195                 for (i = 0; i < triangulation->triangles_total; i++, rect++) {
196                         int *triangle = triangulation->triangles[i];
197                         VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
198                                                   *b = &triangulation->triangulated_points[triangle[1]],
199                                                   *c = &triangulation->triangulated_points[triangle[2]];
200
201                         float min[2], max[2];
202
203                         INIT_MINMAX2(min, max);
204
205                         DO_MINMAX2(a->co, min, max);
206                         DO_MINMAX2(b->co, min, max);
207                         DO_MINMAX2(c->co, min, max);
208
209                         rect->xmin = min[0];
210                         rect->ymin = min[1];
211
212                         rect->xmax = max[0];
213                         rect->ymax = max[1];
214                 }
215         }
216
217         return triangulation;
218 }
219
220 void *KeyingScreenOperation::initializeTileData(rcti *rect)
221 {
222         TileData *tile_data;
223         TriangulationData *triangulation;
224         int triangles_allocated = 0;
225         int chunk_size = 20;
226         int i;
227         rctf rect_float;
228
229         if (this->m_movieClip == NULL)
230                 return NULL;
231
232         if (!this->m_cachedTriangulation) {
233                 lockMutex();
234                 if (this->m_cachedTriangulation == NULL) {
235                         this->m_cachedTriangulation = buildVoronoiTriangulation();
236                 }
237                 unlockMutex();
238         }
239
240         triangulation = this->m_cachedTriangulation;
241
242         if (!triangulation)
243                 return NULL;
244
245         BLI_rctf_init(&rect_float, rect->xmin, rect->xmax, rect->ymin, rect->ymax);
246
247         tile_data = (TileData *) MEM_callocN(sizeof(TileData), "keying screen tile data");
248
249         for (i = 0; i < triangulation->triangles_total; i++) {
250                 bool ok = BLI_isect_rctf(&rect_float, &triangulation->triangles_AABB[i], NULL);
251
252                 if (ok) {
253                         tile_data->triangles_total++;
254
255                         if (tile_data->triangles_total > triangles_allocated) {
256                                 if (!tile_data->triangles) {
257                                         tile_data->triangles = (int *) MEM_mallocN(sizeof(int) * chunk_size,
258                                                                                    "keying screen tile triangles chunk");
259                                 }
260                                 else {
261                                         tile_data->triangles = (int *) MEM_reallocN(tile_data->triangles,
262                                                                                     sizeof(int) * (triangles_allocated + chunk_size));
263                                 }
264
265                                 triangles_allocated += chunk_size;
266                         }
267
268                         tile_data->triangles[tile_data->triangles_total - 1] = i;
269                 }
270         }
271
272         return tile_data;
273 }
274
275 void KeyingScreenOperation::deinitializeTileData(rcti *rect, void *data)
276 {
277         TileData *tile_data = (TileData *) data;
278
279         if (tile_data->triangles) {
280                 MEM_freeN(tile_data->triangles);
281         }
282
283         MEM_freeN(tile_data);
284 }
285
286 void KeyingScreenOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
287 {
288         resolution[0] = 0;
289         resolution[1] = 0;
290
291         if (this->m_movieClip) {
292                 MovieClipUser user = {0};
293                 int width, height;
294                 int clip_frame = BKE_movieclip_remap_scene_to_clip_frame(this->m_movieClip, this->m_framenumber);
295
296                 BKE_movieclip_user_set_frame(&user, clip_frame);
297                 BKE_movieclip_get_size(this->m_movieClip, &user, &width, &height);
298
299                 resolution[0] = width;
300                 resolution[1] = height;
301         }
302 }
303
304 void KeyingScreenOperation::executePixel(float *color, int x, int y, void *data)
305 {
306         color[0] = 0.0f;
307         color[1] = 0.0f;
308         color[2] = 0.0f;
309         color[3] = 1.0f;
310
311         if (this->m_movieClip && data) {
312                 TriangulationData *triangulation = this->m_cachedTriangulation;
313                 TileData *tile_data = (TileData *) data;
314                 int i;
315                 float co[2] = {(float) x, (float) y};
316
317                 for (i = 0; i < tile_data->triangles_total; i++) {
318                         int triangle_idx = tile_data->triangles[i];
319                         rctf *rect = &triangulation->triangles_AABB[triangle_idx];
320
321                         if (IN_RANGE_INCL(x, rect->xmin, rect->xmax) && IN_RANGE_INCL(y, rect->ymin, rect->ymax)) {
322                                 int *triangle = triangulation->triangles[triangle_idx];
323                                 VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
324                                                           *b = &triangulation->triangulated_points[triangle[1]],
325                                                           *c = &triangulation->triangulated_points[triangle[2]];
326                                 float w[3];
327
328                                 if (barycentric_coords_v2(a->co, b->co, c->co, co, w)) {
329                                         if (barycentric_inside_triangle_v2(w)) {
330                                                 color[0] = a->color[0] * w[0] + b->color[0] * w[1] + c->color[0] * w[2];
331                                                 color[1] = a->color[1] * w[0] + b->color[1] * w[1] + c->color[1] * w[2];
332                                                 color[2] = a->color[2] * w[0] + b->color[2] * w[1] + c->color[2] * w[2];
333
334                                                 break;
335                                         }
336                                 }
337                         }
338                 }
339         }
340 }