doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / nodes / intern / CMP_nodes / CMP_mapUV.c
1 /*
2  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /* **************** Map UV  ******************** */
33
34 static bNodeSocketType cmp_node_mapuv_in[]= {
35         {       SOCK_RGBA, 1, "Image",                  0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
36         {       SOCK_VECTOR, 1, "UV",                   1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
37         {       -1, 0, ""       }
38 };
39 static bNodeSocketType cmp_node_mapuv_out[]= {
40         {       SOCK_RGBA, 0, "Image",                  0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
41         {       -1, 0, ""       }
42 };
43
44 /* foreach UV, use these values to read in cbuf and write to stackbuf */
45 /* stackbuf should be zeroed */
46 static void do_mapuv(CompBuf *stackbuf, CompBuf *cbuf, CompBuf *uvbuf, float threshold)
47 {
48         ImBuf *ibuf;
49         float *out= stackbuf->rect, *uv, *uvnext, *uvprev;
50         float dx, dy, alpha;
51         int x, y, sx, sy, row= 3*stackbuf->x;
52         
53         /* ibuf needed for sampling */
54         ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
55         ibuf->rect_float= cbuf->rect;
56         
57         /* vars for efficient looping */
58         uv= uvbuf->rect;
59         uvnext= uv+row;
60         uvprev= uv-row;
61         sx= stackbuf->x;
62         sy= stackbuf->y;
63         
64         for(y=0; y<sy; y++) {
65                 for(x=0; x<sx; x++, out+=4, uv+=3, uvnext+=3, uvprev+=3) {
66                         if(x>0 && x<sx-1 && y>0 && y<sy-1) {
67                                 if(uv[2]!=0.0f) {
68                                         float uv_l, uv_r;
69                                         
70                                         /* adaptive sampling, red (U) channel */
71                                         
72                                         /* prevent alpha zero UVs to be used */
73                                         uv_l= uv[-1]!=0.0f? fabs(uv[0]-uv[-3]) : 0.0f;
74                                         uv_r= uv[ 5]!=0.0f? fabs(uv[0]-uv[ 3]) : 0.0f;
75                                         
76                                         //dx= 0.5f*(fabs(uv[0]-uv[-3]) + fabs(uv[0]-uv[3]));
77                                         dx= 0.5f*(uv_l + uv_r);
78                                         
79                                         uv_l= uvprev[-1]!=0.0f? fabs(uv[0]-uvprev[-3]) : 0.0f;
80                                         uv_r= uvnext[-1]!=0.0f? fabs(uv[0]-uvnext[-3]) : 0.0f;
81                                         
82                                         //dx+= 0.25f*(fabs(uv[0]-uvprev[-3]) + fabs(uv[0]-uvnext[-3]));
83                                         dx+= 0.25f*(uv_l + uv_r);
84                                                 
85                                         uv_l= uvprev[ 5]!=0.0f? fabs(uv[0]-uvprev[+3]) : 0.0f;
86                                         uv_r= uvnext[ 5]!=0.0f? fabs(uv[0]-uvnext[+3]) : 0.0f;
87                                         
88                                         //dx+= 0.25f*(fabs(uv[0]-uvprev[+3]) + fabs(uv[0]-uvnext[+3]));
89                                         dx+= 0.25f*(uv_l + uv_r);
90                                         
91                                         /* adaptive sampling, green (V) channel */
92                                         
93                                         uv_l= uv[-row+2]!=0.0f? fabs(uv[1]-uv[-row+1]) : 0.0f;
94                                         uv_r= uv[ row+2]!=0.0f? fabs(uv[1]-uv[ row+1]) : 0.0f;
95                                         
96                                         //dy= 0.5f*(fabs(uv[1]-uv[-row+1]) + fabs(uv[1]-uv[row+1]));
97                                         dy= 0.5f*(uv_l + uv_r);
98                                         
99                                         uv_l= uvprev[-1]!=0.0f? fabs(uv[1]-uvprev[+1-3]) : 0.0f;
100                                         uv_r= uvnext[-1]!=0.0f? fabs(uv[1]-uvnext[+1-3]) : 0.0f;
101                                         
102                                         //dy+= 0.25f*(fabs(uv[1]-uvprev[+1-3]) + fabs(uv[1]-uvnext[+1-3]));
103                                         dy+= 0.25f*(uv_l + uv_r);
104                                         
105                                         uv_l= uvprev[ 5]!=0.0f? fabs(uv[1]-uvprev[+1+3]) : 0.0f;
106                                         uv_r= uvnext[ 5]!=0.0f? fabs(uv[1]-uvnext[+1+3]) : 0.0f;
107                                         
108                                         //dy+= 0.25f*(fabs(uv[1]-uvprev[+1+3]) + fabs(uv[1]-uvnext[+1+3]));
109                                         dy+= 0.25f*(uv_l + uv_r);
110                                         
111                                         /* UV to alpha threshold */
112                                         alpha= 1.0f - threshold*(dx+dy);
113                                         if(alpha<0.0f) alpha= 0.0f;
114                                         else alpha*= uv[2];
115                                         
116                                         /* should use mipmap */
117                                         if(dx > 0.20f) dx= 0.20f;
118                                         if(dy > 0.20f) dy= 0.20f;
119                                         
120                                         ibuf_sample(ibuf, uv[0], uv[1], dx, dy, out);
121                                         /* premul */
122                                         if(alpha<1.0f) {
123                                                 out[0]*= alpha;
124                                                 out[1]*= alpha;
125                                                 out[2]*= alpha;
126                                                 out[3]*= alpha;
127                                         }
128                                 }
129                         }
130                 }
131         }
132
133         IMB_freeImBuf(ibuf);    
134 }
135
136
137 static void node_composit_exec_mapuv(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
138 {
139         if(out[0]->hasoutput==0)
140                 return;
141         
142         if(in[0]->data && in[1]->data) {
143                 CompBuf *cbuf= in[0]->data;
144                 CompBuf *uvbuf= in[1]->data;
145                 CompBuf *stackbuf;
146                 
147                 cbuf= typecheck_compbuf(cbuf, CB_RGBA);
148                 uvbuf= typecheck_compbuf(uvbuf, CB_VEC3);
149                 stackbuf= alloc_compbuf(uvbuf->x, uvbuf->y, CB_RGBA, 1); /* allocs */;
150                 
151                 do_mapuv(stackbuf, cbuf, uvbuf, 0.05f*(float)node->custom1);
152                 
153                 out[0]->data= stackbuf;
154                 
155                 if(cbuf!=in[0]->data)
156                         free_compbuf(cbuf);
157                 if(uvbuf!=in[1]->data)
158                         free_compbuf(uvbuf);
159         }
160 }
161
162 void register_node_type_cmp_mapuv(ListBase *lb)
163 {
164         static bNodeType ntype;
165
166         node_type_base(&ntype, CMP_NODE_MAP_UV, "Map UV", NODE_CLASS_DISTORT, NODE_OPTIONS,
167                 cmp_node_mapuv_in, cmp_node_mapuv_out);
168         node_type_size(&ntype, 140, 100, 320);
169         node_type_exec(&ntype, node_composit_exec_mapuv);
170
171         nodeRegisterType(lb, &ntype);
172 }
173
174
175