2012-06-14 12:18:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012, Blender Foundation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Contributor:
|
|
|
|
* Jeroen Bakker
|
|
|
|
* Monique Dewanchand
|
|
|
|
* Sergey Sharybin
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "COM_KeyingScreenOperation.h"
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "BLI_listbase.h"
|
|
|
|
#include "BLI_math.h"
|
|
|
|
#include "BLI_math_color.h"
|
|
|
|
|
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "BKE_movieclip.h"
|
|
|
|
#include "BKE_tracking.h"
|
|
|
|
|
|
|
|
#include "IMB_imbuf.h"
|
|
|
|
#include "IMB_imbuf_types.h"
|
|
|
|
}
|
|
|
|
|
2012-06-15 18:42:03 +00:00
|
|
|
KeyingScreenOperation::KeyingScreenOperation() : NodeOperation()
|
2012-06-14 12:18:42 +00:00
|
|
|
{
|
|
|
|
this->addOutputSocket(COM_DT_COLOR);
|
|
|
|
this->movieClip = NULL;
|
|
|
|
this->framenumber = 0;
|
|
|
|
this->trackingObject[0] = 0;
|
|
|
|
setComplex(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyingScreenOperation::initExecution()
|
|
|
|
{
|
|
|
|
initMutex();
|
|
|
|
this->cachedTriangulation = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyingScreenOperation::deinitExecution()
|
|
|
|
{
|
|
|
|
if (this->cachedTriangulation) {
|
|
|
|
TriangulationData *triangulation = cachedTriangulation;
|
|
|
|
|
|
|
|
if (triangulation->triangulated_points)
|
|
|
|
MEM_freeN(triangulation->triangulated_points);
|
|
|
|
|
|
|
|
if (triangulation->triangles)
|
|
|
|
MEM_freeN(triangulation->triangles);
|
|
|
|
|
2012-06-24 15:29:43 +00:00
|
|
|
if (triangulation->triangles_AABB)
|
|
|
|
MEM_freeN(triangulation->triangles_AABB);
|
|
|
|
|
2012-06-14 12:18:42 +00:00
|
|
|
MEM_freeN(this->cachedTriangulation);
|
|
|
|
|
|
|
|
this->cachedTriangulation = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyingScreenOperation::TriangulationData *KeyingScreenOperation::buildVoronoiTriangulation()
|
|
|
|
{
|
|
|
|
MovieClipUser user = {0};
|
|
|
|
TriangulationData *triangulation;
|
|
|
|
MovieTracking *tracking = &movieClip->tracking;
|
|
|
|
MovieTrackingTrack *track;
|
|
|
|
VoronoiSite *sites;
|
|
|
|
ImBuf *ibuf;
|
|
|
|
ListBase *tracksbase;
|
|
|
|
ListBase edges = {NULL, NULL};
|
|
|
|
int sites_total;
|
|
|
|
int i;
|
|
|
|
int width = this->getWidth();
|
|
|
|
int height = this->getHeight();
|
2012-06-19 17:57:51 +00:00
|
|
|
int clip_frame = BKE_movieclip_remap_scene_to_clip_frame(this->movieClip, framenumber);
|
2012-06-14 12:18:42 +00:00
|
|
|
|
|
|
|
if (this->trackingObject[0]) {
|
2012-06-15 11:03:23 +00:00
|
|
|
MovieTrackingObject *object = BKE_tracking_object_get_named(tracking, this->trackingObject);
|
2012-06-14 12:18:42 +00:00
|
|
|
|
|
|
|
if (!object)
|
|
|
|
return NULL;
|
|
|
|
|
2012-06-15 11:03:23 +00:00
|
|
|
tracksbase = BKE_tracking_object_get_tracks(tracking, object);
|
2012-06-14 12:18:42 +00:00
|
|
|
}
|
|
|
|
else
|
2012-06-15 11:03:23 +00:00
|
|
|
tracksbase = BKE_tracking_get_active_tracks(tracking);
|
2012-06-14 12:18:42 +00:00
|
|
|
|
|
|
|
sites_total = BLI_countlist(tracksbase);
|
|
|
|
|
|
|
|
if (!sites_total)
|
|
|
|
return NULL;
|
|
|
|
|
2012-06-19 17:57:51 +00:00
|
|
|
BKE_movieclip_user_set_frame(&user, clip_frame);
|
2012-06-14 12:18:42 +00:00
|
|
|
ibuf = BKE_movieclip_get_ibuf(movieClip, &user);
|
|
|
|
|
|
|
|
if (!ibuf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
triangulation = (TriangulationData *) MEM_callocN(sizeof(TriangulationData), "keying screen triangulation data");
|
|
|
|
|
|
|
|
sites = (VoronoiSite *) MEM_callocN(sizeof(VoronoiSite) * sites_total, "keyingscreen voronoi sites");
|
|
|
|
track = (MovieTrackingTrack *) tracksbase->first;
|
|
|
|
i = 0;
|
|
|
|
while (track) {
|
|
|
|
VoronoiSite *site = &sites[i];
|
2012-06-19 17:57:51 +00:00
|
|
|
MovieTrackingMarker *marker = BKE_tracking_marker_get(track, clip_frame);
|
2012-06-14 12:18:42 +00:00
|
|
|
ImBuf *pattern_ibuf = BKE_tracking_get_pattern_imbuf(ibuf, track, marker, TRUE, FALSE);
|
|
|
|
int j;
|
|
|
|
|
|
|
|
zero_v3(site->color);
|
|
|
|
for (j = 0; j < pattern_ibuf->x * pattern_ibuf->y; j++) {
|
|
|
|
if (pattern_ibuf->rect_float) {
|
|
|
|
add_v3_v3(site->color, &pattern_ibuf->rect_float[4 * j]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unsigned char *rrgb = (unsigned char *)pattern_ibuf->rect;
|
|
|
|
|
|
|
|
site->color[0] += srgb_to_linearrgb((float)rrgb[4 * j + 0] / 255.0f);
|
|
|
|
site->color[1] += srgb_to_linearrgb((float)rrgb[4 * j + 1] / 255.0f);
|
|
|
|
site->color[2] += srgb_to_linearrgb((float)rrgb[4 * j + 2] / 255.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mul_v3_fl(site->color, 1.0f / (pattern_ibuf->x * pattern_ibuf->y));
|
|
|
|
IMB_freeImBuf(pattern_ibuf);
|
|
|
|
|
|
|
|
site->co[0] = marker->pos[0] * width;
|
|
|
|
site->co[1] = marker->pos[1] * height;
|
|
|
|
|
|
|
|
track = track->next;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMB_freeImBuf(ibuf);
|
|
|
|
|
|
|
|
BLI_voronoi_compute(sites, sites_total, width, height, &edges);
|
|
|
|
|
|
|
|
BLI_voronoi_triangulate(sites, sites_total, &edges, width, height,
|
|
|
|
&triangulation->triangulated_points, &triangulation->triangulated_points_total,
|
2012-06-15 18:42:03 +00:00
|
|
|
&triangulation->triangles, &triangulation->triangles_total);
|
2012-06-14 12:18:42 +00:00
|
|
|
|
|
|
|
MEM_freeN(sites);
|
|
|
|
BLI_freelistN(&edges);
|
|
|
|
|
2012-06-24 15:29:43 +00:00
|
|
|
if (triangulation->triangles_total) {
|
|
|
|
rctf *rect;
|
|
|
|
rect = triangulation->triangles_AABB =
|
|
|
|
(rctf *) MEM_callocN(sizeof(rctf) * triangulation->triangles_total, "voronoi triangulation AABB");
|
|
|
|
|
|
|
|
for (i = 0; i < triangulation->triangles_total; i++, rect++) {
|
|
|
|
int *triangle = triangulation->triangles[i];
|
|
|
|
VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
|
|
|
|
*b = &triangulation->triangulated_points[triangle[1]],
|
|
|
|
*c = &triangulation->triangulated_points[triangle[2]];
|
|
|
|
|
|
|
|
float min[2], max[2];
|
|
|
|
|
|
|
|
INIT_MINMAX2(min, max);
|
|
|
|
|
|
|
|
DO_MINMAX2(a->co, min, max);
|
|
|
|
DO_MINMAX2(b->co, min, max);
|
|
|
|
DO_MINMAX2(c->co, min, max);
|
|
|
|
|
|
|
|
rect->xmin = min[0];
|
|
|
|
rect->ymin = min[1];
|
|
|
|
|
|
|
|
rect->xmax = max[0];
|
|
|
|
rect->ymax = max[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 12:18:42 +00:00
|
|
|
return triangulation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *KeyingScreenOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers)
|
|
|
|
{
|
|
|
|
if (this->movieClip == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (this->cachedTriangulation)
|
|
|
|
return this->cachedTriangulation;
|
|
|
|
|
|
|
|
lockMutex();
|
|
|
|
if (this->cachedTriangulation == NULL) {
|
|
|
|
this->cachedTriangulation = buildVoronoiTriangulation();
|
|
|
|
}
|
|
|
|
unlockMutex();
|
|
|
|
|
|
|
|
return this->cachedTriangulation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyingScreenOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
|
|
|
|
{
|
|
|
|
resolution[0] = 0;
|
|
|
|
resolution[1] = 0;
|
|
|
|
|
|
|
|
if (this->movieClip) {
|
|
|
|
MovieClipUser user = {0};
|
|
|
|
int width, height;
|
2012-06-19 17:57:51 +00:00
|
|
|
int clip_frame = BKE_movieclip_remap_scene_to_clip_frame(this->movieClip, framenumber);
|
2012-06-14 12:18:42 +00:00
|
|
|
|
2012-06-19 17:57:51 +00:00
|
|
|
BKE_movieclip_user_set_frame(&user, clip_frame);
|
2012-06-14 12:18:42 +00:00
|
|
|
BKE_movieclip_get_size(this->movieClip, &user, &width, &height);
|
|
|
|
|
|
|
|
resolution[0] = width;
|
|
|
|
resolution[1] = height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyingScreenOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)
|
|
|
|
{
|
|
|
|
color[0] = 0.0f;
|
|
|
|
color[1] = 0.0f;
|
|
|
|
color[2] = 0.0f;
|
|
|
|
color[3] = 1.0f;
|
|
|
|
|
|
|
|
if (this->movieClip && data) {
|
|
|
|
TriangulationData *triangulation = (TriangulationData *) data;
|
|
|
|
int i;
|
2012-06-24 15:29:43 +00:00
|
|
|
float co[2] = {(float) x, (float) y};
|
|
|
|
|
2012-06-14 12:18:42 +00:00
|
|
|
for (i = 0; i < triangulation->triangles_total; i++) {
|
2012-06-24 15:29:43 +00:00
|
|
|
rctf *rect = &triangulation->triangles_AABB[i];
|
|
|
|
|
|
|
|
if (IN_RANGE_INCL(x, rect->xmin, rect->xmax) && IN_RANGE_INCL(y, rect->ymin, rect->ymax)) {
|
|
|
|
int *triangle = triangulation->triangles[i];
|
|
|
|
VoronoiTriangulationPoint *a = &triangulation->triangulated_points[triangle[0]],
|
|
|
|
*b = &triangulation->triangulated_points[triangle[1]],
|
|
|
|
*c = &triangulation->triangulated_points[triangle[2]];
|
|
|
|
float w[3];
|
|
|
|
|
|
|
|
if (barycentric_coords_v2(a->co, b->co, c->co, co, w)) {
|
|
|
|
if (barycentric_inside_triangle_v2(w)) {
|
|
|
|
color[0] += a->color[0] * w[0] + b->color[0] * w[1] + c->color[0] * w[2];
|
|
|
|
color[1] += a->color[1] * w[0] + b->color[1] * w[1] + c->color[1] * w[2];
|
|
|
|
color[2] += a->color[2] * w[0] + b->color[2] * w[1] + c->color[2] * w[2];
|
|
|
|
}
|
2012-06-14 12:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|