svn merge ^/trunk/blender -r47961:48070
[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                 MEM_freeN(this->cachedTriangulation);
69
70                 this->cachedTriangulation = NULL;
71         }
72 }
73
74 KeyingScreenOperation::TriangulationData *KeyingScreenOperation::buildVoronoiTriangulation()
75 {
76         MovieClipUser user = {0};
77         TriangulationData *triangulation;
78         MovieTracking *tracking = &movieClip->tracking;
79         MovieTrackingTrack *track;
80         VoronoiSite *sites;
81         ImBuf *ibuf;
82         ListBase *tracksbase;
83         ListBase edges = {NULL, NULL};
84         int sites_total;
85         int i;
86         int width = this->getWidth();
87         int height = this->getHeight();
88
89         if (this->trackingObject[0]) {
90                 MovieTrackingObject *object = BKE_tracking_object_get_named(tracking, this->trackingObject);
91
92                 if (!object)
93                         return NULL;
94
95                 tracksbase = BKE_tracking_object_get_tracks(tracking, object);
96         }
97         else
98                 tracksbase = BKE_tracking_get_active_tracks(tracking);
99
100         sites_total = BLI_countlist(tracksbase);
101
102         if (!sites_total)
103                 return NULL;
104
105         BKE_movieclip_user_set_frame(&user, framenumber);
106         ibuf = BKE_movieclip_get_ibuf(movieClip, &user);
107
108         if (!ibuf)
109                 return NULL;
110
111         triangulation = (TriangulationData *) MEM_callocN(sizeof(TriangulationData), "keying screen triangulation data");
112
113         sites = (VoronoiSite *) MEM_callocN(sizeof(VoronoiSite) * sites_total, "keyingscreen voronoi sites");
114         track = (MovieTrackingTrack *) tracksbase->first;
115         i = 0;
116         while (track) {
117                 VoronoiSite *site = &sites[i];
118                 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenumber);
119                 ImBuf *pattern_ibuf = BKE_tracking_get_pattern_imbuf(ibuf, track, marker, TRUE, FALSE);
120                 int j;
121
122                 zero_v3(site->color);
123                 for (j = 0; j < pattern_ibuf->x * pattern_ibuf->y; j++) {
124                         if (pattern_ibuf->rect_float) {
125                                 add_v3_v3(site->color, &pattern_ibuf->rect_float[4 * j]);
126                         }
127                         else {
128                                 unsigned char *rrgb = (unsigned char *)pattern_ibuf->rect;
129
130                                 site->color[0] += srgb_to_linearrgb((float)rrgb[4 * j + 0] / 255.0f);
131                                 site->color[1] += srgb_to_linearrgb((float)rrgb[4 * j + 1] / 255.0f);
132                                 site->color[2] += srgb_to_linearrgb((float)rrgb[4 * j + 2] / 255.0f);
133                         }
134                 }
135
136                 mul_v3_fl(site->color, 1.0f / (pattern_ibuf->x * pattern_ibuf->y));
137                 IMB_freeImBuf(pattern_ibuf);
138
139                 site->co[0] = marker->pos[0] * width;
140                 site->co[1] = marker->pos[1] * height;
141
142                 track = track->next;
143                 i++;
144         }
145
146         IMB_freeImBuf(ibuf);
147
148         BLI_voronoi_compute(sites, sites_total, width, height, &edges);
149
150         BLI_voronoi_triangulate(sites, sites_total, &edges, width, height,
151                                 &triangulation->triangulated_points, &triangulation->triangulated_points_total,
152                                 &triangulation->triangles, &triangulation->triangles_total);
153
154         MEM_freeN(sites);
155         BLI_freelistN(&edges);
156
157         return triangulation;
158 }
159
160 void *KeyingScreenOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers)
161 {
162         if (this->movieClip == NULL)
163                 return NULL;
164
165         if (this->cachedTriangulation)
166                 return this->cachedTriangulation;
167
168         lockMutex();
169         if (this->cachedTriangulation == NULL) {
170                 this->cachedTriangulation = buildVoronoiTriangulation();
171         }
172         unlockMutex();
173
174         return this->cachedTriangulation;
175 }
176
177 void KeyingScreenOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
178 {
179         resolution[0] = 0;
180         resolution[1] = 0;
181
182         if (this->movieClip) {
183                 MovieClipUser user = {0};
184                 int width, height;
185
186                 BKE_movieclip_user_set_frame(&user, framenumber);
187                 BKE_movieclip_get_size(this->movieClip, &user, &width, &height);
188
189                 resolution[0] = width;
190                 resolution[1] = height;
191         }
192 }
193
194 void KeyingScreenOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)
195 {
196         color[0] = 0.0f;
197         color[1] = 0.0f;
198         color[2] = 0.0f;
199         color[3] = 1.0f;
200
201         if (this->movieClip && data) {
202                 TriangulationData *triangulation = (TriangulationData *) data;
203                 int i;
204                 for (i = 0; i < triangulation->triangles_total; i++) {
205                         int *triangle = triangulation->triangles[i];
206                         VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
207                         *b = &triangulation->triangulated_points[triangle[1]],
208                         *c = &triangulation->triangulated_points[triangle[2]];
209                         float co[2] = {(float) x, (float) y}, w[3];
210
211                         if (barycentric_coords_v2(a->co, b->co, c->co, co, w)) {
212                                 if (barycentric_inside_triangle_v2(w)) {
213                                         color[0] += a->color[0] * w[0] + b->color[0] * w[1] + c->color[0] * w[2];
214                                         color[1] += a->color[1] * w[0] + b->color[1] * w[1] + c->color[1] * w[2];
215                                         color[2] += a->color[2] * w[0] + b->color[2] * w[1] + c->color[2] * w[2];
216                                 }
217                         }
218                 }
219         }
220 }