remove $Id: tags after discussion on the mailign list: http://markmail.org/message...
[blender.git] / source / blender / nodes / composite / nodes / node_composite_mapUV.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/composite/nodes/node_composite_mapUV.c
29  *  \ingroup cmpnodes
30  */
31
32
33 #include "node_composite_util.h"
34
35 /* **************** Map UV  ******************** */
36
37 static bNodeSocketTemplate cmp_node_mapuv_in[]= {
38         {       SOCK_RGBA, 1, "Image",                  0.8f, 0.8f, 0.8f, 1.0f},
39         {       SOCK_VECTOR, 1, "UV",                   1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE},
40         {       -1, 0, ""       }
41 };
42 static bNodeSocketTemplate cmp_node_mapuv_out[]= {
43         {       SOCK_RGBA, 0, "Image"},
44         {       -1, 0, ""       }
45 };
46
47 /* foreach UV, use these values to read in cbuf and write to stackbuf */
48 /* stackbuf should be zeroed */
49 static void do_mapuv(CompBuf *stackbuf, CompBuf *cbuf, CompBuf *uvbuf, float threshold)
50 {
51         ImBuf *ibuf;
52         float *out= stackbuf->rect, *uv, *uvnext, *uvprev;
53         float dx, dy, alpha;
54         int x, y, sx, sy, row= 3*stackbuf->x;
55         
56         /* ibuf needed for sampling */
57         ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
58         ibuf->rect_float= cbuf->rect;
59         
60         /* vars for efficient looping */
61         uv= uvbuf->rect;
62         uvnext= uv+row;
63         uvprev= uv-row;
64         sx= stackbuf->x;
65         sy= stackbuf->y;
66         
67         for(y=0; y<sy; y++) {
68                 for(x=0; x<sx; x++, out+=4, uv+=3, uvnext+=3, uvprev+=3) {
69                         if(x>0 && x<sx-1 && y>0 && y<sy-1) {
70                                 if(uv[2]!=0.0f) {
71                                         float uv_l, uv_r;
72                                         
73                                         /* adaptive sampling, red (U) channel */
74                                         
75                                         /* prevent alpha zero UVs to be used */
76                                         uv_l= uv[-1]!=0.0f? fabs(uv[0]-uv[-3]) : 0.0f;
77                                         uv_r= uv[ 5]!=0.0f? fabs(uv[0]-uv[ 3]) : 0.0f;
78                                         
79                                         //dx= 0.5f*(fabs(uv[0]-uv[-3]) + fabs(uv[0]-uv[3]));
80                                         dx= 0.5f*(uv_l + uv_r);
81                                         
82                                         uv_l= uvprev[-1]!=0.0f? fabs(uv[0]-uvprev[-3]) : 0.0f;
83                                         uv_r= uvnext[-1]!=0.0f? fabs(uv[0]-uvnext[-3]) : 0.0f;
84                                         
85                                         //dx+= 0.25f*(fabs(uv[0]-uvprev[-3]) + fabs(uv[0]-uvnext[-3]));
86                                         dx+= 0.25f*(uv_l + uv_r);
87                                                 
88                                         uv_l= uvprev[ 5]!=0.0f? fabs(uv[0]-uvprev[+3]) : 0.0f;
89                                         uv_r= uvnext[ 5]!=0.0f? fabs(uv[0]-uvnext[+3]) : 0.0f;
90                                         
91                                         //dx+= 0.25f*(fabs(uv[0]-uvprev[+3]) + fabs(uv[0]-uvnext[+3]));
92                                         dx+= 0.25f*(uv_l + uv_r);
93                                         
94                                         /* adaptive sampling, green (V) channel */
95                                         
96                                         uv_l= uv[-row+2]!=0.0f? fabs(uv[1]-uv[-row+1]) : 0.0f;
97                                         uv_r= uv[ row+2]!=0.0f? fabs(uv[1]-uv[ row+1]) : 0.0f;
98                                         
99                                         //dy= 0.5f*(fabs(uv[1]-uv[-row+1]) + fabs(uv[1]-uv[row+1]));
100                                         dy= 0.5f*(uv_l + uv_r);
101                                         
102                                         uv_l= uvprev[-1]!=0.0f? fabs(uv[1]-uvprev[+1-3]) : 0.0f;
103                                         uv_r= uvnext[-1]!=0.0f? fabs(uv[1]-uvnext[+1-3]) : 0.0f;
104                                         
105                                         //dy+= 0.25f*(fabs(uv[1]-uvprev[+1-3]) + fabs(uv[1]-uvnext[+1-3]));
106                                         dy+= 0.25f*(uv_l + uv_r);
107                                         
108                                         uv_l= uvprev[ 5]!=0.0f? fabs(uv[1]-uvprev[+1+3]) : 0.0f;
109                                         uv_r= uvnext[ 5]!=0.0f? fabs(uv[1]-uvnext[+1+3]) : 0.0f;
110                                         
111                                         //dy+= 0.25f*(fabs(uv[1]-uvprev[+1+3]) + fabs(uv[1]-uvnext[+1+3]));
112                                         dy+= 0.25f*(uv_l + uv_r);
113                                         
114                                         /* UV to alpha threshold */
115                                         alpha= 1.0f - threshold*(dx+dy);
116                                         if(alpha<0.0f) alpha= 0.0f;
117                                         else alpha*= uv[2];
118                                         
119                                         /* should use mipmap */
120                                         if(dx > 0.20f) dx= 0.20f;
121                                         if(dy > 0.20f) dy= 0.20f;
122                                         
123                                         ibuf_sample(ibuf, uv[0], uv[1], dx, dy, out);
124                                         /* premul */
125                                         if(alpha<1.0f) {
126                                                 out[0]*= alpha;
127                                                 out[1]*= alpha;
128                                                 out[2]*= alpha;
129                                                 out[3]*= alpha;
130                                         }
131                                 }
132                         }
133                 }
134         }
135
136         IMB_freeImBuf(ibuf);    
137 }
138
139
140 static void node_composit_exec_mapuv(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
141 {
142         if(out[0]->hasoutput==0)
143                 return;
144         
145         if(in[0]->data && in[1]->data) {
146                 CompBuf *cbuf= in[0]->data;
147                 CompBuf *uvbuf= in[1]->data;
148                 CompBuf *stackbuf;
149                 
150                 cbuf= typecheck_compbuf(cbuf, CB_RGBA);
151                 uvbuf= typecheck_compbuf(uvbuf, CB_VEC3);
152                 stackbuf= alloc_compbuf(uvbuf->x, uvbuf->y, CB_RGBA, 1); /* allocs */;
153                 
154                 do_mapuv(stackbuf, cbuf, uvbuf, 0.05f*(float)node->custom1);
155                 
156                 out[0]->data= stackbuf;
157                 
158                 if(cbuf!=in[0]->data)
159                         free_compbuf(cbuf);
160                 if(uvbuf!=in[1]->data)
161                         free_compbuf(uvbuf);
162         }
163 }
164
165 void register_node_type_cmp_mapuv(ListBase *lb)
166 {
167         static bNodeType ntype;
168
169         node_type_base(&ntype, CMP_NODE_MAP_UV, "Map UV", NODE_CLASS_DISTORT, NODE_OPTIONS);
170         node_type_socket_templates(&ntype, cmp_node_mapuv_in, cmp_node_mapuv_out);
171         node_type_size(&ntype, 140, 100, 320);
172         node_type_exec(&ntype, node_composit_exec_mapuv);
173
174         nodeRegisterType(lb, &ntype);
175 }
176
177
178