~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/blender/nodes/intern/CMP_nodes/CMP_chromaMatte.c

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2007-05-17 11:47:59 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517114759-yp4ybrnhp2u7pk66
Tags: 2.44-1
* New upstream release.
* Drop debian/patches/01_64bits_stupidity, not needed anymore: as of this
  version blender is 64 bits safe again. Adjust README.Debian accordingly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * $Id: CMP_chromaMatte.c,v 1.5 2007/04/04 13:58:09 jesterking Exp $
 
3
 *
 
4
 * ***** BEGIN GPL LICENSE BLOCK *****
 
5
 *
 
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. 
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 *
 
20
 * The Original Code is Copyright (C) 2006 Blender Foundation.
 
21
 * All rights reserved.
 
22
 *
 
23
 * The Original Code is: all of this file.
 
24
 *
 
25
 * Contributor(s): none yet.
 
26
 *
 
27
 * ***** END GPL LICENSE BLOCK *****
 
28
 */
 
29
 
 
30
#include "../CMP_util.h"
 
31
 
 
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},
 
36
        {-1,0,""}
 
37
};
 
38
 
 
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},
 
42
        {-1,0,""}
 
43
};
 
44
 
 
45
static void do_rgba_to_ycca_normalized(bNode *node, float *out, float *in)
 
46
{
 
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;
 
52
        out[3]=in[3];
 
53
}
 
54
 
 
55
static void do_ycca_to_rgba_normalized(bNode *node, float *out, float *in)
 
56
{
 
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]);
 
62
        out[3]=in[3];
 
63
}
 
64
 
 
65
static void do_chroma_key(bNode *node, float *out, float *in)
 
66
{
 
67
        NodeChroma *c;
 
68
        float x, z, alpha;
 
69
        float theta, beta, angle;
 
70
        float kfg, newY, newCb, newCr;
 
71
 
 
72
        c=node->storage;
 
73
 
 
74
        /* Algorithm from book "Video Demistified" */
 
75
 
 
76
        /* find theta, the angle that the color space should be rotated based on key*/
 
77
        theta=atan2(c->key[2],c->key[1]);
 
78
 
 
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);
 
82
 
 
83
        /*if within the acceptance angle */
 
84
        angle=c->t1*M_PI/180.0; /* convert to radians */
 
85
        
 
86
        /* if kfg is <0 then the pixel is outside of the key color */
 
87
        kfg=x-(fabs(z)/tan(angle/2.0));
 
88
 
 
89
        if(kfg>0.0) {  /* found a pixel that is within key color */
 
90
 
 
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);
 
95
 
 
96
                beta=atan2(newCr,newCb);
 
97
                beta=beta*180.0/M_PI; /* convert to degrees for compare*/
 
98
 
 
99
                /* if beta is within the clippin angle */
 
100
                if(fabs(beta)<(c->t2/2.0)) {
 
101
                        newCb=0.0;
 
102
                        newCr=0.0;
 
103
                        alpha=0.0;
 
104
                }
 
105
 
 
106
                out[0]=newY;
 
107
                out[1]=newCb;
 
108
                out[2]=newCr;
 
109
 
 
110
                /* don't make something that was more transparent less transparent */
 
111
                if (alpha<in[3]) {
 
112
                        out[3]=alpha;
 
113
                }
 
114
                else {
 
115
                        out[3]=in[3];
 
116
                }
 
117
        }
 
118
        else { /*pixel is outside key color */
 
119
                out[0]=in[0];
 
120
                out[1]=in[1];
 
121
                out[2]=in[2];
 
122
                out[3]=in[3]; /* make pixel just as transparent as it was before */
 
123
        }
 
124
}
 
125
 
 
126
static void node_composit_exec_chroma_matte(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
 
127
{
 
128
        CompBuf *cbuf;
 
129
        CompBuf *chromabuf;
 
130
        NodeChroma *c;
 
131
        
 
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;
 
135
        
 
136
        cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
 
137
        
 
138
        chromabuf= dupalloc_compbuf(cbuf);
 
139
        
 
140
        c=node->storage;
 
141
        
 
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);
 
146
        
 
147
        /*per pixel chroma key*/
 
148
        composit1_pixel_processor(node, chromabuf, chromabuf, in[0]->vec, do_chroma_key, CB_RGBA);
 
149
        
 
150
        /*convert back*/
 
151
        composit1_pixel_processor(node, chromabuf, chromabuf, in[0]->vec, do_ycca_to_rgba_normalized, CB_RGBA);
 
152
        
 
153
        out[0]->data= chromabuf;
 
154
        if(out[1]->hasoutput)
 
155
                out[1]->data= valbuf_from_rgbabuf(chromabuf, CHAN_A);
 
156
        
 
157
        generate_preview(node, chromabuf);
 
158
 
 
159
        if(cbuf!=in[0]->data)
 
160
                free_compbuf(cbuf);
 
161
};
 
162
 
 
163
 
 
164
static void node_composit_init_chroma_matte(bNode *node)
 
165
{
 
166
   NodeChroma *c= MEM_callocN(sizeof(NodeChroma), "node chroma");
 
167
   node->storage= c;
 
168
   c->t1= 30.0f;
 
169
   c->t2= 10.0f;
 
170
   c->t3= 0.0f;
 
171
   c->fsize= 0.0f;
 
172
   c->fstrength= 1.0f;
 
173
};
 
174
 
 
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,
 
185
        /* butfunc     */       NULL,
 
186
        /* initfunc    */       node_composit_init_chroma_matte,
 
187
        /* freestoragefunc    */        node_free_standard_storage,
 
188
        /* copystoragefunc    */        node_copy_standard_storage,
 
189
        /* id          */       NULL
 
190
};
 
191
 
 
192