Merging r48303 through r48305 from trunk into soc-2011-tomato
[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
108                 if ((marker->flag & MARKER_DISABLED) == 0) {
109                         sites_total++;
110                 }
111         }
112
113         if (!sites_total)
114                 return NULL;
115
116         BKE_movieclip_user_set_frame(&user, clip_frame);
117         ibuf = BKE_movieclip_get_ibuf(this->m_movieClip, &user);
118
119         if (!ibuf)
120                 return NULL;
121
122         triangulation = (TriangulationData *) MEM_callocN(sizeof(TriangulationData), "keying screen triangulation data");
123
124         sites = (VoronoiSite *) MEM_callocN(sizeof(VoronoiSite) * sites_total, "keyingscreen voronoi sites");
125         track = (MovieTrackingTrack *) tracksbase->first;
126         for (track = (MovieTrackingTrack *) tracksbase->first, i = 0; track; track = track->next, i++) {
127                 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, clip_frame);
128                 VoronoiSite *site;
129                 ImBuf *pattern_ibuf;
130                 int j;
131
132                 if (marker->flag & MARKER_DISABLED)
133                         continue;
134
135                 site = &sites[i];
136
137                 pattern_ibuf = BKE_tracking_get_pattern_imbuf(ibuf, track, marker, TRUE, FALSE);
138
139                 zero_v3(site->color);
140                 for (j = 0; j < pattern_ibuf->x * pattern_ibuf->y; j++) {
141                         if (pattern_ibuf->rect_float) {
142                                 add_v3_v3(site->color, &pattern_ibuf->rect_float[4 * j]);
143                         }
144                         else {
145                                 unsigned char *rrgb = (unsigned char *)pattern_ibuf->rect;
146
147                                 site->color[0] += srgb_to_linearrgb((float)rrgb[4 * j + 0] / 255.0f);
148                                 site->color[1] += srgb_to_linearrgb((float)rrgb[4 * j + 1] / 255.0f);
149                                 site->color[2] += srgb_to_linearrgb((float)rrgb[4 * j + 2] / 255.0f);
150                         }
151                 }
152
153                 mul_v3_fl(site->color, 1.0f / (pattern_ibuf->x * pattern_ibuf->y));
154                 IMB_freeImBuf(pattern_ibuf);
155
156                 site->co[0] = marker->pos[0] * width;
157                 site->co[1] = marker->pos[1] * height;
158         }
159
160         IMB_freeImBuf(ibuf);
161
162         BLI_voronoi_compute(sites, sites_total, width, height, &edges);
163
164         BLI_voronoi_triangulate(sites, sites_total, &edges, width, height,
165                                 &triangulation->triangulated_points, &triangulation->triangulated_points_total,
166                                 &triangulation->triangles, &triangulation->triangles_total);
167
168         MEM_freeN(sites);
169         BLI_freelistN(&edges);
170
171         if (triangulation->triangles_total) {
172                 rctf *rect;
173                 rect = triangulation->triangles_AABB =
174                         (rctf *) MEM_callocN(sizeof(rctf) * triangulation->triangles_total, "voronoi triangulation AABB");
175
176                 for (i = 0; i < triangulation->triangles_total; i++, rect++) {
177                         int *triangle = triangulation->triangles[i];
178                         VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
179                                                   *b = &triangulation->triangulated_points[triangle[1]],
180                                                   *c = &triangulation->triangulated_points[triangle[2]];
181
182                         float min[2], max[2];
183
184                         INIT_MINMAX2(min, max);
185
186                         DO_MINMAX2(a->co, min, max);
187                         DO_MINMAX2(b->co, min, max);
188                         DO_MINMAX2(c->co, min, max);
189
190                         rect->xmin = min[0];
191                         rect->ymin = min[1];
192
193                         rect->xmax = max[0];
194                         rect->ymax = max[1];
195                 }
196         }
197
198         return triangulation;
199 }
200
201 void *KeyingScreenOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers)
202 {
203         TileData *tile_data;
204         TriangulationData *triangulation;
205         int triangles_allocated = 0;
206         int chunk_size = 20;
207         int i;
208         rctf rect_float;
209
210         if (this->m_movieClip == NULL)
211                 return NULL;
212
213         if (!this->m_cachedTriangulation) {
214                 lockMutex();
215                 if (this->m_cachedTriangulation == NULL) {
216                         this->m_cachedTriangulation = buildVoronoiTriangulation();
217                 }
218                 unlockMutex();
219         }
220
221         BLI_init_rctf(&rect_float, rect->xmin, rect->xmax, rect->ymin, rect->ymax);
222
223         triangulation = this->m_cachedTriangulation;
224         tile_data = (TileData *) MEM_callocN(sizeof(TileData), "keying screen tile data");
225
226         for (i = 0; i < triangulation->triangles_total; i++) {
227                 bool ok = BLI_isect_rctf(&rect_float, &triangulation->triangles_AABB[i], NULL);
228
229                 if (ok) {
230                         tile_data->triangles_total++;
231
232                         if (tile_data->triangles_total > triangles_allocated) {
233                                 if (!tile_data->triangles) {
234                                         tile_data->triangles = (int *) MEM_mallocN(sizeof(int) * chunk_size,
235                                                                                    "keying screen tile triangles chunk");
236                                 }
237                                 else {
238                                         tile_data->triangles = (int *) MEM_reallocN(tile_data->triangles,
239                                                                                     sizeof(int) * (triangles_allocated + chunk_size));
240                                 }
241
242                                 triangles_allocated += chunk_size;
243                         }
244
245                         tile_data->triangles[tile_data->triangles_total - 1] = i;
246                 }
247         }
248
249         return tile_data;
250 }
251
252 void KeyingScreenOperation::deinitializeTileData(rcti *rect, MemoryBuffer **memoryBuffers, void *data)
253 {
254         TileData *tile_data = (TileData *) data;
255
256         if (tile_data->triangles) {
257                 MEM_freeN(tile_data->triangles);
258         }
259
260         MEM_freeN(tile_data);
261 }
262
263 void KeyingScreenOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
264 {
265         resolution[0] = 0;
266         resolution[1] = 0;
267
268         if (this->m_movieClip) {
269                 MovieClipUser user = {0};
270                 int width, height;
271                 int clip_frame = BKE_movieclip_remap_scene_to_clip_frame(this->m_movieClip, this->m_framenumber);
272
273                 BKE_movieclip_user_set_frame(&user, clip_frame);
274                 BKE_movieclip_get_size(this->m_movieClip, &user, &width, &height);
275
276                 resolution[0] = width;
277                 resolution[1] = height;
278         }
279 }
280
281 void KeyingScreenOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)
282 {
283         color[0] = 0.0f;
284         color[1] = 0.0f;
285         color[2] = 0.0f;
286         color[3] = 1.0f;
287
288         if (this->m_movieClip && data) {
289                 TriangulationData *triangulation = this->m_cachedTriangulation;
290                 TileData *tile_data = (TileData *) data;
291                 int i;
292                 float co[2] = {(float) x, (float) y};
293
294                 for (i = 0; i < tile_data->triangles_total; i++) {
295                         int triangle_idx = tile_data->triangles[i];
296                         rctf *rect = &triangulation->triangles_AABB[triangle_idx];
297
298                         if (IN_RANGE_INCL(x, rect->xmin, rect->xmax) && IN_RANGE_INCL(y, rect->ymin, rect->ymax)) {
299                                 int *triangle = triangulation->triangles[triangle_idx];
300                                 VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
301                                                           *b = &triangulation->triangulated_points[triangle[1]],
302                                                           *c = &triangulation->triangulated_points[triangle[2]];
303                                 float w[3];
304
305                                 if (barycentric_coords_v2(a->co, b->co, c->co, co, w)) {
306                                         if (barycentric_inside_triangle_v2(w)) {
307                                                 color[0] += a->color[0] * w[0] + b->color[0] * w[1] + c->color[0] * w[2];
308                                                 color[1] += a->color[1] * w[0] + b->color[1] * w[1] + c->color[1] * w[2];
309                                                 color[2] += a->color[2] * w[0] + b->color[2] * w[1] + c->color[2] * w[2];
310                                         }
311                                 }
312                         }
313                 }
314         }
315 }