Ignore disabled markers when building keying screen.
[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->movieClip = NULL;
46         this->framenumber = 0;
47         this->trackingObject[0] = 0;
48         setComplex(true);
49 }
50
51 void KeyingScreenOperation::initExecution()
52 {
53         initMutex();
54         this->cachedTriangulation = NULL;
55 }
56
57 void KeyingScreenOperation::deinitExecution()
58 {
59         if (this->cachedTriangulation) {
60                 TriangulationData *triangulation = 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->cachedTriangulation);
72
73                 this->cachedTriangulation = NULL;
74         }
75 }
76
77 KeyingScreenOperation::TriangulationData *KeyingScreenOperation::buildVoronoiTriangulation()
78 {
79         MovieClipUser user = {0};
80         TriangulationData *triangulation;
81         MovieTracking *tracking = &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->movieClip, framenumber);
92
93         if (this->trackingObject[0]) {
94                 MovieTrackingObject *object = BKE_tracking_object_get_named(tracking, this->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(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                 pattern_ibuf = BKE_tracking_get_pattern_imbuf(ibuf, track, marker, TRUE, FALSE);
137
138                 zero_v3(site->color);
139                 for (j = 0; j < pattern_ibuf->x * pattern_ibuf->y; j++) {
140                         if (pattern_ibuf->rect_float) {
141                                 add_v3_v3(site->color, &pattern_ibuf->rect_float[4 * j]);
142                         }
143                         else {
144                                 unsigned char *rrgb = (unsigned char *)pattern_ibuf->rect;
145
146                                 site->color[0] += srgb_to_linearrgb((float)rrgb[4 * j + 0] / 255.0f);
147                                 site->color[1] += srgb_to_linearrgb((float)rrgb[4 * j + 1] / 255.0f);
148                                 site->color[2] += srgb_to_linearrgb((float)rrgb[4 * j + 2] / 255.0f);
149                         }
150                 }
151
152                 mul_v3_fl(site->color, 1.0f / (pattern_ibuf->x * pattern_ibuf->y));
153                 IMB_freeImBuf(pattern_ibuf);
154
155                 site->co[0] = marker->pos[0] * width;
156                 site->co[1] = marker->pos[1] * height;
157         }
158
159         IMB_freeImBuf(ibuf);
160
161         BLI_voronoi_compute(sites, sites_total, width, height, &edges);
162
163         BLI_voronoi_triangulate(sites, sites_total, &edges, width, height,
164                                 &triangulation->triangulated_points, &triangulation->triangulated_points_total,
165                                 &triangulation->triangles, &triangulation->triangles_total);
166
167         MEM_freeN(sites);
168         BLI_freelistN(&edges);
169
170         if (triangulation->triangles_total) {
171                 rctf *rect;
172                 rect = triangulation->triangles_AABB =
173                         (rctf *) MEM_callocN(sizeof(rctf) * triangulation->triangles_total, "voronoi triangulation AABB");
174
175                 for (i = 0; i < triangulation->triangles_total; i++, rect++) {
176                         int *triangle = triangulation->triangles[i];
177                         VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
178                                                   *b = &triangulation->triangulated_points[triangle[1]],
179                                                   *c = &triangulation->triangulated_points[triangle[2]];
180
181                         float min[2], max[2];
182
183                         INIT_MINMAX2(min, max);
184
185                         DO_MINMAX2(a->co, min, max);
186                         DO_MINMAX2(b->co, min, max);
187                         DO_MINMAX2(c->co, min, max);
188
189                         rect->xmin = min[0];
190                         rect->ymin = min[1];
191
192                         rect->xmax = max[0];
193                         rect->ymax = max[1];
194                 }
195         }
196
197         return triangulation;
198 }
199
200 void *KeyingScreenOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers)
201 {
202         if (this->movieClip == NULL)
203                 return NULL;
204
205         if (this->cachedTriangulation)
206                 return this->cachedTriangulation;
207
208         lockMutex();
209         if (this->cachedTriangulation == NULL) {
210                 this->cachedTriangulation = buildVoronoiTriangulation();
211         }
212         unlockMutex();
213
214         return this->cachedTriangulation;
215 }
216
217 void KeyingScreenOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
218 {
219         resolution[0] = 0;
220         resolution[1] = 0;
221
222         if (this->movieClip) {
223                 MovieClipUser user = {0};
224                 int width, height;
225                 int clip_frame = BKE_movieclip_remap_scene_to_clip_frame(this->movieClip, framenumber);
226
227                 BKE_movieclip_user_set_frame(&user, clip_frame);
228                 BKE_movieclip_get_size(this->movieClip, &user, &width, &height);
229
230                 resolution[0] = width;
231                 resolution[1] = height;
232         }
233 }
234
235 void KeyingScreenOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)
236 {
237         color[0] = 0.0f;
238         color[1] = 0.0f;
239         color[2] = 0.0f;
240         color[3] = 1.0f;
241
242         if (this->movieClip && data) {
243                 TriangulationData *triangulation = (TriangulationData *) data;
244                 int i;
245                 float co[2] = {(float) x, (float) y};
246
247                 for (i = 0; i < triangulation->triangles_total; i++) {
248                         rctf *rect = &triangulation->triangles_AABB[i];
249
250                         if (IN_RANGE_INCL(x, rect->xmin, rect->xmax) && IN_RANGE_INCL(y, rect->ymin, rect->ymax)) {
251                                 int *triangle = triangulation->triangles[i];
252                                 VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
253                                                           *b = &triangulation->triangulated_points[triangle[1]],
254                                                           *c = &triangulation->triangulated_points[triangle[2]];
255                                 float w[3];
256
257                                 if (barycentric_coords_v2(a->co, b->co, c->co, co, w)) {
258                                         if (barycentric_inside_triangle_v2(w)) {
259                                                 color[0] += a->color[0] * w[0] + b->color[0] * w[1] + c->color[0] * w[2];
260                                                 color[1] += a->color[1] * w[0] + b->color[1] * w[1] + c->color[1] * w[2];
261                                                 color[2] += a->color[2] * w[0] + b->color[2] * w[1] + c->color[2] * w[2];
262                                         }
263                                 }
264                         }
265                 }
266         }
267 }