style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / nodes / composite / nodes / node_composite_displace.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_displace.c
29  *  \ingroup cmpnodes
30  */
31
32
33 #include "node_composite_util.h"
34
35
36 /* **************** Displace  ******************** */
37
38 static bNodeSocketTemplate cmp_node_displace_in[]= {
39         {       SOCK_RGBA, 1, "Image",                  1.0f, 1.0f, 1.0f, 1.0f},
40         {       SOCK_VECTOR, 1, "Vector",                       1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_TRANSLATION},
41         {       SOCK_FLOAT, 1, "X Scale",                               0.0f, 0.0f, 0.0f, 0.0f, -1000.0f, 1000.0f, PROP_FACTOR},
42         {       SOCK_FLOAT, 1, "Y Scale",                               0.0f, 0.0f, 0.0f, 0.0f, -1000.0f, 1000.0f, PROP_FACTOR},
43         {       -1, 0, ""       }
44 };
45 static bNodeSocketTemplate cmp_node_displace_out[]= {
46         {       SOCK_RGBA, 0, "Image"},
47         {       -1, 0, ""       }
48 };
49
50 /* minimum distance (in pixels) a pixel has to be displaced
51  * in order to take effect */
52 #define DISPLACE_EPSILON        0.01f
53
54 static void do_displace(bNode *node, CompBuf *stackbuf, CompBuf *cbuf, CompBuf *vecbuf, float *UNUSED(veccol), CompBuf *xbuf,  CompBuf *ybuf, float *xscale, float *yscale)
55 {
56         ImBuf *ibuf;
57         int x, y;
58         float p_dx, p_dy;       /* main displacement in pixel space */
59         float d_dx, d_dy;
60         float dxt, dyt;
61         float u, v;
62         float xs, ys;
63         float vec[3], vecdx[3], vecdy[3];
64         float col[3];
65         
66         ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
67         ibuf->rect_float= cbuf->rect;
68         
69         for (y=0; y < stackbuf->y; y++) {
70                 for (x=0; x < stackbuf->x; x++) {
71                         /* calc pixel coordinates */
72                         qd_getPixel(vecbuf, x-vecbuf->xof, y-vecbuf->yof, vec);
73                         
74                         if (xbuf)
75                                 qd_getPixel(xbuf, x-xbuf->xof, y-xbuf->yof, &xs);
76                         else
77                                 xs = xscale[0];
78                         
79                         if (ybuf)
80                                 qd_getPixel(ybuf, x-ybuf->xof, y-ybuf->yof, &ys);
81                         else
82                                 ys = yscale[0];
83
84                         /* clamp x and y displacement to triple image resolution - 
85                          * to prevent hangs from huge values mistakenly plugged in eg. z buffers */
86                         CLAMP(xs, -stackbuf->x*4, stackbuf->x*4);
87                         CLAMP(ys, -stackbuf->y*4, stackbuf->y*4);
88                         
89                         p_dx = vec[0] * xs;
90                         p_dy = vec[1] * ys;
91                         
92                         /* if no displacement, then just copy this pixel */
93                         if (fabsf(p_dx) < DISPLACE_EPSILON && fabsf(p_dy) < DISPLACE_EPSILON) {
94                                 qd_getPixel(cbuf, x-cbuf->xof, y-cbuf->yof, col);
95                                 qd_setPixel(stackbuf, x, y, col);
96                                 continue;
97                         }
98                         
99                         /* displaced pixel in uv coords, for image sampling */
100                         u = (x - cbuf->xof - p_dx + 0.5f) / (float)stackbuf->x;
101                         v = (y - cbuf->yof - p_dy + 0.5f) / (float)stackbuf->y;
102                         
103                         
104                         /* calc derivatives */
105                         qd_getPixel(vecbuf, x-vecbuf->xof+1, y-vecbuf->yof, vecdx);
106                         qd_getPixel(vecbuf, x-vecbuf->xof, y-vecbuf->yof+1, vecdy);
107                         d_dx = vecdx[0] * xs;
108                         d_dy = vecdy[0] * ys;
109
110                         /* clamp derivatives to minimum displacement distance in UV space */
111                         dxt = p_dx - d_dx;
112                         dyt = p_dy - d_dy;
113
114                         dxt = signf(dxt)*maxf(fabsf(dxt), DISPLACE_EPSILON)/(float)stackbuf->x;
115                         dyt = signf(dyt)*maxf(fabsf(dyt), DISPLACE_EPSILON)/(float)stackbuf->y;
116                         
117                         ibuf_sample(ibuf, u, v, dxt, dyt, col);
118                         qd_setPixel(stackbuf, x, y, col);
119                         
120                         if (node->exec & NODE_BREAK) break;
121                 }
122                 
123                 if (node->exec & NODE_BREAK) break;
124         }
125         IMB_freeImBuf(ibuf);
126         
127         
128 /* simple method for reference, linear interpolation */
129 /*      
130         int x, y;
131         float dx, dy;
132         float u, v;
133         float vec[3];
134         float col[3];
135         
136         for (y=0; y < stackbuf->y; y++) {
137                 for (x=0; x < stackbuf->x; x++) {
138                         qd_getPixel(vecbuf, x, y, vec);
139                         
140                         dx = vec[0] * (xscale[0]);
141                         dy = vec[1] * (yscale[0]);
142                         
143                         u = (x - dx + 0.5f) / (float)stackbuf->x;
144                         v = (y - dy + 0.5f) / (float)stackbuf->y;
145                         
146                         qd_getPixelLerp(cbuf, u*cbuf->x - 0.5f, v*cbuf->y - 0.5f, col);
147                         qd_setPixel(stackbuf, x, y, col);
148                 }
149         }
150 */
151 }
152
153
154 static void node_composit_exec_displace(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
155 {
156         if (out[0]->hasoutput==0)
157                 return;
158         
159         if (in[0]->data && in[1]->data) {
160                 CompBuf *cbuf= in[0]->data;
161                 CompBuf *vecbuf= in[1]->data;
162                 CompBuf *xbuf= in[2]->data;
163                 CompBuf *ybuf= in[3]->data;
164                 CompBuf *stackbuf;
165                 
166                 cbuf= typecheck_compbuf(cbuf, CB_RGBA);
167                 vecbuf= typecheck_compbuf(vecbuf, CB_VEC3);
168                 xbuf= typecheck_compbuf(xbuf, CB_VAL);
169                 ybuf= typecheck_compbuf(ybuf, CB_VAL);
170                 
171                 stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* allocs */
172
173                 do_displace(node, stackbuf, cbuf, vecbuf, in[1]->vec, xbuf, ybuf, in[2]->vec, in[3]->vec);
174                 
175                 out[0]->data= stackbuf;
176                 
177                 
178                 if (cbuf!=in[0]->data)
179                         free_compbuf(cbuf);
180                 if (vecbuf!=in[1]->data)
181                         free_compbuf(vecbuf);
182         }
183 }
184
185 void register_node_type_cmp_displace(bNodeTreeType *ttype)
186 {
187         static bNodeType ntype;
188
189         node_type_base(ttype, &ntype, CMP_NODE_DISPLACE, "Displace", NODE_CLASS_DISTORT, NODE_OPTIONS);
190         node_type_socket_templates(&ntype, cmp_node_displace_in, cmp_node_displace_out);
191         node_type_size(&ntype, 140, 100, 320);
192         node_type_exec(&ntype, node_composit_exec_displace);
193
194         nodeRegisterType(ttype, &ntype);
195 }