2
* $Id: CMP_chromaMatte.c,v 1.5 2007/04/04 13:58:09 jesterking Exp $
4
* ***** BEGIN GPL LICENSE BLOCK *****
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software Foundation,
18
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
* The Original Code is Copyright (C) 2006 Blender Foundation.
21
* All rights reserved.
23
* The Original Code is: all of this file.
25
* Contributor(s): none yet.
27
* ***** END GPL LICENSE BLOCK *****
30
#include "../CMP_util.h"
32
/* ******************* Chroma Key ********************************************************** */
33
static bNodeSocketType cmp_node_chroma_in[]={
34
{SOCK_RGBA,1,"Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
35
{SOCK_RGBA,1,"Key Color", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
39
static bNodeSocketType cmp_node_chroma_out[]={
40
{SOCK_RGBA,0,"Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
41
{SOCK_VALUE,0,"Matte",0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
45
static void do_rgba_to_ycca_normalized(bNode *node, float *out, float *in)
47
/*normalize to the range -1.0 to 1.0) */
48
rgb_to_ycc(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
49
out[0]=((out[0])-16)/255.0;
50
out[1]=((out[1])-128)/255.0;
51
out[2]=((out[2])-128)/255.0;
55
static void do_ycca_to_rgba_normalized(bNode *node, float *out, float *in)
57
/*un-normalize the normalize from above */
58
in[0]=(in[0]*255.0)+16;
59
in[1]=(in[1]*255.0)+128;
60
in[2]=(in[2]*255.0)+128;
61
ycc_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
65
static void do_chroma_key(bNode *node, float *out, float *in)
69
float theta, beta, angle;
70
float kfg, newY, newCb, newCr;
74
/* Algorithm from book "Video Demistified" */
76
/* find theta, the angle that the color space should be rotated based on key*/
77
theta=atan2(c->key[2],c->key[1]);
79
/*rotate the cb and cr into x/z space */
80
x=in[1]*cos(theta)+in[2]*sin(theta);
81
z=in[2]*cos(theta)-in[1]*sin(theta);
83
/*if within the acceptance angle */
84
angle=c->t1*M_PI/180.0; /* convert to radians */
86
/* if kfg is <0 then the pixel is outside of the key color */
87
kfg=x-(fabs(z)/tan(angle/2.0));
89
if(kfg>0.0) { /* found a pixel that is within key color */
91
newY=in[0]-(1-c->t3)*kfg;
92
newCb=in[1]-kfg*cosf(theta);
93
newCr=in[2]-kfg*sinf(theta);
94
alpha=(kfg+c->fsize)*(c->fstrength);
96
beta=atan2(newCr,newCb);
97
beta=beta*180.0/M_PI; /* convert to degrees for compare*/
99
/* if beta is within the clippin angle */
100
if(fabs(beta)<(c->t2/2.0)) {
110
/* don't make something that was more transparent less transparent */
118
else { /*pixel is outside key color */
122
out[3]=in[3]; /* make pixel just as transparent as it was before */
126
static void node_composit_exec_chroma_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
132
if(in[0]->hasinput==0) return;
133
if(in[0]->data==NULL) return;
134
if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
136
cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
138
chromabuf= dupalloc_compbuf(cbuf);
142
/*convert rgbbuf to normalized chroma space*/
143
composit1_pixel_processor(node, chromabuf, cbuf, in[0]->vec, do_rgba_to_ycca_normalized, CB_RGBA);
144
/*convert key to normalized chroma color space */
145
do_rgba_to_ycca_normalized(node, c->key, in[1]->vec);
147
/*per pixel chroma key*/
148
composit1_pixel_processor(node, chromabuf, chromabuf, in[0]->vec, do_chroma_key, CB_RGBA);
151
composit1_pixel_processor(node, chromabuf, chromabuf, in[0]->vec, do_ycca_to_rgba_normalized, CB_RGBA);
153
out[0]->data= chromabuf;
154
if(out[1]->hasoutput)
155
out[1]->data= valbuf_from_rgbabuf(chromabuf, CHAN_A);
157
generate_preview(node, chromabuf);
159
if(cbuf!=in[0]->data)
164
static void node_composit_init_chroma_matte(bNode *node)
166
NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
175
bNodeType cmp_node_chroma={
176
/* *next,*prev */ NULL, NULL,
177
/* type code */ CMP_NODE_CHROMA,
178
/* name */ "Chroma Key",
179
/* width+range */ 200, 80, 300,
180
/* class+opts */ NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS,
181
/* input sock */ cmp_node_chroma_in,
182
/* output sock */ cmp_node_chroma_out,
183
/* storage */ "NodeChroma",
184
/* execfunc */ node_composit_exec_chroma_matte,
186
/* initfunc */ node_composit_init_chroma_matte,
187
/* freestoragefunc */ node_free_standard_storage,
188
/* copystoragefunc */ node_copy_standard_storage,