Internal refactoring of tracking module, should be no functional changes
[blender.git] / source / blender / nodes / composite / nodes / node_composite_keyingscreen.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
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  * The Original Code is Copyright (C) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Blender Foundation,
24  *                 Sergey Sharybin
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/nodes/composite/nodes/node_composite_keyingscreen.c
30  *  \ingroup cmpnodes
31  */
32
33 #include "BLF_translation.h"
34
35 #include "DNA_movieclip_types.h"
36
37 #include "BKE_movieclip.h"
38
39 #include "BLI_listbase.h"
40 #include "BLI_math_base.h"
41 #include "BLI_math_color.h"
42 #include "BLI_voronoi.h"
43
44 #include "node_composite_util.h"
45
46 /* **************** Translate  ******************** */
47
48 static bNodeSocketTemplate cmp_node_keyingscreen_out[] = {
49         {       SOCK_RGBA,  0, "Screen"},
50         {       -1, 0, ""       }
51 };
52
53
54 static void compute_gradient_screen(RenderData *rd, NodeKeyingScreenData *keyingscreen_data, MovieClip *clip, CompBuf *screenbuf)
55 {
56         MovieClipUser user = {0};
57         MovieTracking *tracking = &clip->tracking;
58         MovieTrackingTrack *track;
59         VoronoiTriangulationPoint *triangulated_points;
60         VoronoiSite *sites;
61         ImBuf *ibuf;
62         ListBase *tracksbase;
63         ListBase edges = {NULL, NULL};
64         int sites_total, triangulated_points_total, triangles_total;
65         int (*triangles)[3];
66         int i, x, y;
67         float *rect = screenbuf->rect;
68
69         if (keyingscreen_data->tracking_object[0]) {
70                 MovieTrackingObject *object = BKE_tracking_object_get_named(tracking, keyingscreen_data->tracking_object);
71
72                 if (!object)
73                         return;
74
75                 tracksbase = BKE_tracking_object_get_tracks(tracking, object);
76         }
77         else
78                 tracksbase = BKE_tracking_get_active_tracks(tracking);
79
80         sites_total = BLI_countlist(tracksbase);
81
82         if (!sites_total)
83                 return;
84
85         BKE_movieclip_user_set_frame(&user, rd->cfra);
86         ibuf = BKE_movieclip_get_ibuf(clip, &user);
87
88         sites = MEM_callocN(sizeof(VoronoiSite) * sites_total, "keyingscreen voronoi sites");
89         track = tracksbase->first;
90         i = 0;
91         while (track) {
92                 VoronoiSite *site = &sites[i];
93                 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, rd->cfra);
94                 ImBuf *pattern_ibuf = BKE_tracking_get_pattern_imbuf(ibuf, track, marker, TRUE, FALSE);
95                 int j;
96
97                 zero_v3(site->color);
98                 for (j = 0; j < pattern_ibuf->x * pattern_ibuf->y; j++) {
99                         if (pattern_ibuf->rect_float) {
100                                 add_v3_v3(site->color, &pattern_ibuf->rect_float[4 * j]);
101                         }
102                         else {
103                                 unsigned char *rrgb = (unsigned char *)pattern_ibuf->rect;
104
105                                 site->color[0] += srgb_to_linearrgb((float)rrgb[4 * j + 0] / 255.0f);
106                                 site->color[1] += srgb_to_linearrgb((float)rrgb[4 * j + 1] / 255.0f);
107                                 site->color[2] += srgb_to_linearrgb((float)rrgb[4 * j + 2] / 255.0f);
108                         }
109                 }
110
111                 mul_v3_fl(site->color, 1.0f / (pattern_ibuf->x * pattern_ibuf->y));
112                 IMB_freeImBuf(pattern_ibuf);
113
114                 site->co[0] = marker->pos[0] * screenbuf->x;
115                 site->co[1] = marker->pos[1] * screenbuf->y;
116
117                 track = track->next;
118                 i++;
119         }
120
121         IMB_freeImBuf(ibuf);
122
123         BLI_voronoi_compute(sites, sites_total, screenbuf->x, screenbuf->y, &edges);
124
125         BLI_voronoi_triangulate(sites, sites_total, &edges, screenbuf->x, screenbuf->y,
126                                 &triangulated_points, &triangulated_points_total,
127                             &triangles, &triangles_total);
128
129         for (y = 0; y < screenbuf->y; y++) {
130                 for (x = 0; x < screenbuf->x; x++) {
131                         int index = 4 * (y * screenbuf->x + x);
132
133                         rect[index + 0] = rect[index + 1] = rect[index + 2] = 0.0f;
134                         rect[index + 3] = 1.0f;
135
136                         for (i = 0; i < triangles_total; i++) {
137                                 int *triangle = triangles[i];
138                                 VoronoiTriangulationPoint *a = &triangulated_points[triangle[0]],
139                                                           *b = &triangulated_points[triangle[1]],
140                                                           *c = &triangulated_points[triangle[2]];
141                                 float co[2] = {x, y}, w[3];
142
143                                 if (barycentric_coords_v2(a->co, b->co, c->co, co, w)) {
144                                         if (barycentric_inside_triangle_v2(w)) {
145                                                 rect[index + 0] += a->color[0] * w[0] + b->color[0] * w[1] + c->color[0] * w[2];
146                                                 rect[index + 1] += a->color[1] * w[0] + b->color[1] * w[1] + c->color[1] * w[2];
147                                                 rect[index + 2] += a->color[2] * w[0] + b->color[2] * w[1] + c->color[2] * w[2];
148                                         }
149                                 }
150                         }
151                 }
152         }
153
154         MEM_freeN(triangulated_points);
155         MEM_freeN(triangles);
156         MEM_freeN(sites);
157         BLI_freelistN(&edges);
158 }
159
160 static void exec(void *data, bNode *node, bNodeStack **UNUSED(in), bNodeStack **out)
161 {
162         NodeKeyingScreenData *keyingscreen_data = node->storage;
163         RenderData *rd = data;
164         CompBuf *screenbuf = NULL;
165
166         if (node->id) {
167                 MovieClip *clip = (MovieClip *) node->id;
168                 MovieClipUser user = {0};
169                 int width, height;
170
171                 BKE_movieclip_user_set_frame(&user, rd->cfra);
172                 BKE_movieclip_get_size(clip, &user, &width, &height);
173
174                 screenbuf = alloc_compbuf(width, height, CB_RGBA, TRUE);
175                 compute_gradient_screen(rd, keyingscreen_data, clip, screenbuf);
176         }
177
178         out[0]->data = screenbuf;
179 }
180
181 static void node_composit_init_keyingscreen(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
182 {
183         NodeKeyingScreenData *data;
184
185         data = MEM_callocN(sizeof(NodeKeyingScreenData), "node keyingscreen data");
186
187         node->storage = data;
188 }
189
190 void register_node_type_cmp_keyingscreen(bNodeTreeType *ttype)
191 {
192         static bNodeType ntype;
193
194         node_type_base(ttype, &ntype, CMP_NODE_KEYINGSCREEN, "Keying Screen", NODE_CLASS_MATTE, NODE_OPTIONS);
195         node_type_socket_templates(&ntype, NULL, cmp_node_keyingscreen_out);
196         node_type_size(&ntype, 140, 100, 320);
197         node_type_init(&ntype, node_composit_init_keyingscreen);
198         node_type_storage(&ntype, "NodeKeyingScreenData", node_free_standard_storage, node_copy_standard_storage);
199         node_type_exec(&ntype, exec);
200
201         nodeRegisterType(ttype, &ntype);
202 }