2
* $Id: CMP_diffMatte.c,v 1.4 2007/04/04 13:58:10 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
/* ******************* channel Difference Matte ********************************* */
33
static bNodeSocketType cmp_node_diff_matte_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_diff_matte_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
/* note, keyvals is passed on from caller as stack array */
46
/* might have been nicer as temp struct though... */
47
static void do_diff_matte(bNode *node, float *colorbuf, float *inbuf, float *keyvals)
49
NodeChroma *c= (NodeChroma *)node->storage;
50
float *keymin= keyvals;
51
float *keymax= keyvals+3;
52
float *key= keyvals+6;
53
float tolerance= keyvals[9];
54
float distance, alpha;
56
/*process the pixel if it is close to the key or already transparent*/
57
if(((colorbuf[0]>keymin[0] && colorbuf[0]<keymax[0]) &&
58
(colorbuf[1]>keymin[1] && colorbuf[1]<keymax[1]) &&
59
(colorbuf[2]>keymin[2] && colorbuf[2]<keymax[2])) || inbuf[3]<1.0f) {
61
/*true distance from key*/
62
distance= sqrt((colorbuf[0]-key[0])*(colorbuf[0]-key[0])+
63
(colorbuf[1]-key[1])*(colorbuf[1]-key[1])+
64
(colorbuf[2]-key[2])*(colorbuf[2]-key[2]));
66
/*is it less transparent than the prevous pixel*/
67
alpha= distance/tolerance;
68
if(alpha > inbuf[3]) alpha= inbuf[3];
69
if(alpha > c->fstrength) alpha= 0.0f;
72
if (alpha>1.0f) alpha=1.0f;
73
if (alpha<0.0f) alpha=0.0f;
75
/*premultiplied picture*/
80
colorbuf[3]= inbuf[3];
84
static void node_composit_exec_diff_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
87
Losely based on the Sequencer chroma key plug-in, but enhanced to work in other color spaces and
88
uses a differnt difference function (suggested in forums of vfxtalk.com).
94
float *keymin= keyvals;
95
float *keymax= keyvals+3;
96
float *key= keyvals+6;
97
float *tolerance= keyvals+9;
100
/*is anything connected?*/
101
if(out[0]->hasoutput==0 && out[1]->hasoutput==0) return;
102
/*must have an image imput*/
103
if(in[0]->data==NULL) return;
105
inbuf=typecheck_compbuf(in[0]->data, CB_RGBA);
108
workbuf=dupalloc_compbuf(inbuf);
110
/*use the input color*/
111
key[0]= in[1]->vec[0];
112
key[1]= in[1]->vec[1];
113
key[2]= in[1]->vec[2];
115
/*get the tolerances from the UI*/
120
/*convert to colorspace*/
121
switch(node->custom1) {
125
/*convert the key (in place)*/
126
rgb_to_hsv(key[0], key[1], key[2], &key[0], &key[1], &key[2]);
127
composit1_pixel_processor(node, workbuf, inbuf, in[1]->vec, do_rgba_to_hsva, CB_RGBA);
130
rgb_to_yuv(key[0], key[1], key[2], &key[0], &key[1], &key[2]);
131
composit1_pixel_processor(node, workbuf, inbuf, in[1]->vec, do_rgba_to_yuva, CB_RGBA);
134
rgb_to_ycc(key[0], key[1], key[2], &key[0], &key[1], &key[2]);
135
composit1_pixel_processor(node, workbuf, inbuf, in[1]->vec, do_rgba_to_ycca, CB_RGBA);
136
/*account for ycc is on a 0..255 scale*/
145
/*find min/max tolerances*/
146
keymin[0]= key[0]-t[0];
147
keymin[1]= key[1]-t[1];
148
keymin[2]= key[2]-t[2];
149
keymax[0]= key[0]+t[0];
150
keymax[1]= key[1]+t[1];
151
keymax[2]= key[2]+t[2];
154
*tolerance= sqrt((t[0])*(t[0])+
158
/* note, processor gets a keyvals array passed on as buffer constant */
159
composit2_pixel_processor(node, workbuf, workbuf, in[0]->vec, NULL, keyvals, do_diff_matte, CB_RGBA, CB_VAL);
161
/*convert back to RGB colorspace*/
162
switch(node->custom1) {
164
composit1_pixel_processor(node, workbuf, workbuf, in[1]->vec, do_copy_rgba, CB_RGBA);
167
composit1_pixel_processor(node, workbuf, workbuf, in[1]->vec, do_hsva_to_rgba, CB_RGBA);
170
composit1_pixel_processor(node, workbuf, workbuf, in[1]->vec, do_yuva_to_rgba, CB_RGBA);
173
composit1_pixel_processor(node, workbuf, workbuf, in[1]->vec, do_ycca_to_rgba, CB_RGBA);
179
out[0]->data=workbuf;
180
if(out[1]->hasoutput)
181
out[1]->data=valbuf_from_rgbabuf(workbuf, CHAN_A);
182
generate_preview(node, workbuf);
184
if(inbuf!=in[0]->data)
188
static void node_composit_init_diff_matte(bNode *node)
190
NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
197
node->custom1= 1; /* RGB */
200
bNodeType cmp_node_diff_matte={
201
/* *next,*prev */ NULL, NULL,
202
/* type code */ CMP_NODE_DIFF_MATTE,
203
/* name */ "Difference Key",
204
/* width+range */ 200, 80, 250,
205
/* class+opts */ NODE_CLASS_MATTE, NODE_PREVIEW|NODE_OPTIONS,
206
/* input sock */ cmp_node_diff_matte_in,
207
/* output sock */ cmp_node_diff_matte_out,
208
/* storage */ "NodeChroma",
209
/* execfunc */ node_composit_exec_diff_matte,
211
/* initfunc */ node_composit_init_diff_matte,
212
/* freestoragefunc */ node_free_standard_storage,
213
/* copystoragefunc */ node_copy_standard_storage,