Blender  V2.93
COM_KeyingScreenOperation.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2012, Blender Foundation.
17  */
18 
20 
21 #include "MEM_guardedalloc.h"
22 
23 #include "BLI_listbase.h"
24 #include "BLI_math.h"
25 #include "BLI_math_color.h"
26 
27 #include "BKE_movieclip.h"
28 #include "BKE_tracking.h"
29 
30 #include "IMB_imbuf.h"
31 #include "IMB_imbuf_types.h"
32 
33 namespace blender::compositor {
34 
36 {
38  this->m_movieClip = nullptr;
39  this->m_framenumber = 0;
40  this->m_trackingObject[0] = 0;
41  flags.complex = true;
42 }
43 
45 {
46  initMutex();
47  this->m_cachedTriangulation = nullptr;
48 }
49 
51 {
52  if (this->m_cachedTriangulation) {
53  TriangulationData *triangulation = this->m_cachedTriangulation;
54 
55  if (triangulation->triangulated_points) {
56  MEM_freeN(triangulation->triangulated_points);
57  }
58 
59  if (triangulation->triangles) {
60  MEM_freeN(triangulation->triangles);
61  }
62 
63  if (triangulation->triangles_AABB) {
64  MEM_freeN(triangulation->triangles_AABB);
65  }
66 
68 
69  this->m_cachedTriangulation = nullptr;
70  }
71 }
72 
74 {
75  MovieClipUser user = {0};
76  TriangulationData *triangulation;
77  MovieTracking *tracking = &this->m_movieClip->tracking;
78  MovieTrackingTrack *track;
79  VoronoiSite *sites, *site;
80  ImBuf *ibuf;
81  ListBase *tracksbase;
82  ListBase edges = {nullptr, nullptr};
83  int sites_total;
84  int i;
85  int width = this->getWidth();
86  int height = this->getHeight();
88 
89  if (this->m_trackingObject[0]) {
91 
92  if (!object) {
93  return nullptr;
94  }
95 
96  tracksbase = BKE_tracking_object_get_tracks(tracking, object);
97  }
98  else {
99  tracksbase = BKE_tracking_get_active_tracks(tracking);
100  }
101 
102  /* count sites */
103  for (track = (MovieTrackingTrack *)tracksbase->first, sites_total = 0; track;
104  track = track->next) {
105  MovieTrackingMarker *marker = BKE_tracking_marker_get(track, clip_frame);
106  float pos[2];
107 
108  if (marker->flag & MARKER_DISABLED) {
109  continue;
110  }
111 
112  add_v2_v2v2(pos, marker->pos, track->offset);
113 
114  if (!IN_RANGE_INCL(pos[0], 0.0f, 1.0f) || !IN_RANGE_INCL(pos[1], 0.0f, 1.0f)) {
115  continue;
116  }
117 
118  sites_total++;
119  }
120 
121  if (!sites_total) {
122  return nullptr;
123  }
124 
125  BKE_movieclip_user_set_frame(&user, clip_frame);
126  ibuf = BKE_movieclip_get_ibuf(this->m_movieClip, &user);
127 
128  if (!ibuf) {
129  return nullptr;
130  }
131 
132  triangulation = (TriangulationData *)MEM_callocN(sizeof(TriangulationData),
133  "keying screen triangulation data");
134 
135  sites = (VoronoiSite *)MEM_callocN(sizeof(VoronoiSite) * sites_total,
136  "keyingscreen voronoi sites");
137  track = (MovieTrackingTrack *)tracksbase->first;
138  for (track = (MovieTrackingTrack *)tracksbase->first, site = sites; track; track = track->next) {
139  MovieTrackingMarker *marker = BKE_tracking_marker_get(track, clip_frame);
140  ImBuf *pattern_ibuf;
141  int j;
142  float pos[2];
143 
144  if (marker->flag & MARKER_DISABLED) {
145  continue;
146  }
147 
148  add_v2_v2v2(pos, marker->pos, track->offset);
149 
150  if (!IN_RANGE_INCL(pos[0], 0.0f, 1.0f) || !IN_RANGE_INCL(pos[1], 0.0f, 1.0f)) {
151  continue;
152  }
153 
154  pattern_ibuf = BKE_tracking_get_pattern_imbuf(ibuf, track, marker, true, false);
155 
156  zero_v3(site->color);
157 
158  if (pattern_ibuf) {
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 
176  site->co[0] = pos[0] * width;
177  site->co[1] = pos[1] * height;
178 
179  site++;
180  }
181 
182  IMB_freeImBuf(ibuf);
183 
184  BLI_voronoi_compute(sites, sites_total, width, height, &edges);
185 
187  sites_total,
188  &edges,
189  width,
190  height,
191  &triangulation->triangulated_points,
192  &triangulation->triangulated_points_total,
193  &triangulation->triangles,
194  &triangulation->triangles_total);
195 
196  MEM_freeN(sites);
197  BLI_freelistN(&edges);
198 
199  if (triangulation->triangles_total) {
200  rcti *rect;
201  rect = triangulation->triangles_AABB = (rcti *)MEM_callocN(
202  sizeof(rcti) * triangulation->triangles_total, "voronoi triangulation AABB");
203 
204  for (i = 0; i < triangulation->triangles_total; i++, rect++) {
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 
210  float min[2], max[2];
211 
212  INIT_MINMAX2(min, max);
213 
214  minmax_v2v2_v2(min, max, a->co);
215  minmax_v2v2_v2(min, max, b->co);
216  minmax_v2v2_v2(min, max, c->co);
217 
218  rect->xmin = (int)min[0];
219  rect->ymin = (int)min[1];
220 
221  rect->xmax = (int)max[0] + 1;
222  rect->ymax = (int)max[1] + 1;
223  }
224  }
225 
226  return triangulation;
227 }
228 
230 {
231  TileData *tile_data;
232  TriangulationData *triangulation;
233  int triangles_allocated = 0;
234  int chunk_size = 20;
235  int i;
236 
237  if (this->m_movieClip == nullptr) {
238  return nullptr;
239  }
240 
241  if (!this->m_cachedTriangulation) {
242  lockMutex();
243  if (this->m_cachedTriangulation == nullptr) {
245  }
246  unlockMutex();
247  }
248 
249  triangulation = this->m_cachedTriangulation;
250 
251  if (!triangulation) {
252  return nullptr;
253  }
254 
255  tile_data = (TileData *)MEM_callocN(sizeof(TileData), "keying screen tile data");
256 
257  for (i = 0; i < triangulation->triangles_total; i++) {
258  if (BLI_rcti_isect(rect, &triangulation->triangles_AABB[i], nullptr)) {
259  tile_data->triangles_total++;
260 
261  if (tile_data->triangles_total > triangles_allocated) {
262  if (!tile_data->triangles) {
263  tile_data->triangles = (int *)MEM_mallocN(sizeof(int) * chunk_size,
264  "keying screen tile triangles chunk");
265  }
266  else {
267  tile_data->triangles = (int *)MEM_reallocN(
268  tile_data->triangles, sizeof(int) * (triangles_allocated + chunk_size));
269  }
270 
271  triangles_allocated += chunk_size;
272  }
273 
274  tile_data->triangles[tile_data->triangles_total - 1] = i;
275  }
276  }
277 
278  return tile_data;
279 }
280 
282 {
283  TileData *tile_data = (TileData *)data;
284 
285  if (tile_data->triangles) {
286  MEM_freeN(tile_data->triangles);
287  }
288 
289  MEM_freeN(tile_data);
290 }
291 
292 void KeyingScreenOperation::determineResolution(unsigned int resolution[2],
293  unsigned int /*preferredResolution*/[2])
294 {
295  resolution[0] = 0;
296  resolution[1] = 0;
297 
298  if (this->m_movieClip) {
299  MovieClipUser user = {0};
300  int width, height;
302  this->m_framenumber);
303 
304  BKE_movieclip_user_set_frame(&user, clip_frame);
306 
307  resolution[0] = width;
308  resolution[1] = height;
309  }
310 }
311 
312 void KeyingScreenOperation::executePixel(float output[4], int x, int y, void *data)
313 {
314  output[0] = 0.0f;
315  output[1] = 0.0f;
316  output[2] = 0.0f;
317  output[3] = 1.0f;
318 
319  if (this->m_movieClip && data) {
320  TriangulationData *triangulation = this->m_cachedTriangulation;
321  TileData *tile_data = (TileData *)data;
322  int i;
323  float co[2] = {(float)x, (float)y};
324 
325  for (i = 0; i < tile_data->triangles_total; i++) {
326  int triangle_idx = tile_data->triangles[i];
327  rcti *rect = &triangulation->triangles_AABB[triangle_idx];
328 
329  if (IN_RANGE_INCL(x, rect->xmin, rect->xmax) && IN_RANGE_INCL(y, rect->ymin, rect->ymax)) {
330  int *triangle = triangulation->triangles[triangle_idx];
331  VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
332  *b = &triangulation->triangulated_points[triangle[1]],
333  *c = &triangulation->triangulated_points[triangle[2]];
334  float w[3];
335 
336  if (barycentric_coords_v2(a->co, b->co, c->co, co, w)) {
338  output[0] = a->color[0] * w[0] + b->color[0] * w[1] + c->color[0] * w[2];
339  output[1] = a->color[1] * w[0] + b->color[1] * w[1] + c->color[1] * w[2];
340  output[2] = a->color[2] * w[0] + b->color[2] * w[1] + c->color[2] * w[2];
341 
342  break;
343  }
344  }
345  }
346  }
347  }
348 }
349 
350 } // namespace blender::compositor
typedef float(TangentPoint)[2]
float BKE_movieclip_remap_scene_to_clip_frame(const struct MovieClip *clip, float framenr)
void BKE_movieclip_user_set_frame(struct MovieClipUser *user, int framenr)
Definition: movieclip.c:1633
void BKE_movieclip_get_size(struct MovieClip *clip, struct MovieClipUser *user, int *width, int *height)
Definition: movieclip.c:1540
struct ImBuf * BKE_movieclip_get_ibuf(struct MovieClip *clip, struct MovieClipUser *user)
Definition: movieclip.c:1349
struct ImBuf * BKE_tracking_get_pattern_imbuf(struct ImBuf *ibuf, struct MovieTrackingTrack *track, struct MovieTrackingMarker *marker, bool anchored, bool disable_channels)
Definition: tracking.c:2823
struct ListBase * BKE_tracking_get_active_tracks(struct MovieTracking *tracking)
Definition: tracking.c:365
struct MovieTrackingObject * BKE_tracking_object_get_named(struct MovieTracking *tracking, const char *name)
Definition: tracking.c:2183
struct ListBase * BKE_tracking_object_get_tracks(struct MovieTracking *tracking, struct MovieTrackingObject *object)
Definition: tracking.c:2218
struct MovieTrackingMarker * BKE_tracking_marker_get(struct MovieTrackingTrack *track, int framenr)
Definition: tracking.c:1523
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
float srgb_to_linearrgb(float c)
Definition: math_color.c:434
int barycentric_inside_triangle_v2(const float w[3])
Definition: math_geom.c:3897
bool barycentric_coords_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
Definition: math_geom.c:3913
MINLINE void mul_v3_fl(float r[3], float f)
void minmax_v2v2_v2(float min[2], float max[2], const float vec[2])
Definition: math_vector.c:1043
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
bool BLI_rcti_isect(const struct rcti *src1, const struct rcti *src2, struct rcti *dest)
#define INIT_MINMAX2(min, max)
#define IN_RANGE_INCL(a, b, c)
void BLI_voronoi_triangulate(const VoronoiSite *sites, int sites_total, struct ListBase *edges, int width, int height, VoronoiTriangulationPoint **r_triangulated_points, int *r_triangulated_points_total, int(**r_triangles)[3], int *r_triangles_total)
Definition: voronoi_2d.c:792
void BLI_voronoi_compute(const VoronoiSite *sites, int sites_total, int width, int height, struct ListBase *edges)
Definition: voronoi_2d.c:667
@ MARKER_DISABLED
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
void IMB_freeImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:211
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
#define MEM_reallocN(vmemh, len)
#define output
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
void executePixel(float output[4], int x, int y, void *data) override
calculate a single pixel
void deinitializeTileData(rcti *rect, void *data) override
void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2]) override
void addOutputSocket(DataType datatype)
uint pos
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static unsigned c
Definition: RandGen.cpp:97
static unsigned a[3]
Definition: RandGen.cpp:92
#define min(a, b)
Definition: sort.c:51
unsigned int * rect
float * rect_float
void * first
Definition: DNA_listBase.h:47
struct MovieTracking tracking
struct MovieTrackingTrack * next
float co[2]
float color[3]
int ymin
Definition: DNA_vec_types.h:80
int ymax
Definition: DNA_vec_types.h:80
int xmin
Definition: DNA_vec_types.h:79
int xmax
Definition: DNA_vec_types.h:79
float max