Initial commit. Not in build system so shouldn't interfere with anything at this...
[blender-staging.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., 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 /* **************** 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, 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                                         
69                                         /* adaptive sampling, red (U) channel */
70                                         dx= 0.5f*(fabs(uv[0]-uv[-3]) + fabs(uv[0]-uv[3]));
71                                         
72                                         dx+= 0.25f*(fabs(uv[0]-uvprev[-3]) + fabs(uv[0]-uvnext[-3]));
73                                         dx+= 0.25f*(fabs(uv[0]-uvprev[+3]) + fabs(uv[0]-uvnext[+3]));
74                                         
75                                         /* adaptive sampling, green (V) channel */
76                                         dy= 0.5f*(fabs(uv[1]-uv[-row+1]) + fabs(uv[1]-uv[row+1]));
77                                                          
78                                         dy+= 0.25f*(fabs(uv[1]-uvprev[+1-3]) + fabs(uv[1]-uvnext[+1-3]));
79                                         dy+= 0.25f*(fabs(uv[1]-uvprev[+1+3]) + fabs(uv[1]-uvnext[+1+3]));
80                                         
81                                         /* UV to alpha threshold */
82                                         alpha= 1.0f - threshold*(dx+dy);
83                                         if(alpha<0.0f) alpha= 0.0f;
84                                         else alpha*= uv[2];
85                                         
86                                         /* should use mipmap */
87                                         if(dx > 0.20f) dx= 0.20f;
88                                         if(dy > 0.20f) dy= 0.20f;
89                                         
90                                         ibuf_sample(ibuf, uv[0], uv[1], dx, dy, out);
91                                         /* premul */
92                                         if(alpha<1.0f) {
93                                                 out[0]*= alpha;
94                                                 out[1]*= alpha;
95                                                 out[2]*= alpha;
96                                                 out[3]*= alpha;
97                                         }
98                                 }
99                         }
100                 }
101         }
102
103         IMB_freeImBuf(ibuf);    
104 }
105
106
107 static void node_composit_exec_mapuv(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
108 {
109         if(out[0]->hasoutput==0)
110                 return;
111         
112         if(in[0]->data && in[1]->data) {
113                 CompBuf *cbuf= in[0]->data;
114                 CompBuf *uvbuf= in[1]->data;
115                 CompBuf *stackbuf;
116                 
117                 cbuf= typecheck_compbuf(cbuf, CB_RGBA);
118                 uvbuf= typecheck_compbuf(uvbuf, CB_VEC3);
119                 stackbuf= alloc_compbuf(uvbuf->x, uvbuf->y, CB_RGBA, 1); /* allocs */;
120                 
121                 do_mapuv(stackbuf, cbuf, uvbuf, 0.05f*(float)node->custom1);
122                 
123                 out[0]->data= stackbuf;
124                 
125                 if(cbuf!=in[0]->data)
126                         free_compbuf(cbuf);
127                 if(uvbuf!=in[1]->data)
128                         free_compbuf(uvbuf);
129         }
130 }
131
132 static int node_composit_buts_map_uv(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
133 {
134    if(block) {
135       uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Alpha:",
136          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
137          &node->custom1, 0, 100, 0, 0, "Conversion percentage of UV differences to Alpha");
138    }
139    return 20;
140 }
141
142 bNodeType cmp_node_mapuv= {
143         /* type code   */       CMP_NODE_MAP_UV,
144         /* name        */       "Map UV",
145         /* width+range */       140, 100, 320,
146         /* class+opts  */       NODE_CLASS_DISTORT, NODE_OPTIONS,
147         /* input sock  */       cmp_node_mapuv_in,
148         /* output sock */       cmp_node_mapuv_out,
149         /* storage     */       "",
150         /* execfunc    */       node_composit_exec_mapuv,
151    /* butfunc     */ node_composit_buts_map_uv
152 };
153
154