macro formatting & remve some unused code.
[blender.git] / source / blender / nodes / composite / nodes / node_composite_bilateralblur.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): Vilem Novak
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/composite/nodes/node_composite_bilateralblur.c
29  *  \ingroup cmpnodes
30  */
31
32 #include "node_composite_util.h"
33
34 /* **************** BILATERALBLUR ******************** */
35 static bNodeSocketTemplate cmp_node_bilateralblur_in[]= {
36         { SOCK_RGBA, 1, "Image", 1.0f, 1.0f, 1.0f, 1.0f}, 
37         { SOCK_RGBA, 1, "Determinator", 1.0f, 1.0f, 1.0f, 1.0f}, 
38         { -1, 0, "" } 
39 };
40
41 static bNodeSocketTemplate cmp_node_bilateralblur_out[]= { 
42         { SOCK_RGBA, 0, "Image"}, 
43         { -1, 0, "" } 
44 };
45
46 #define INIT_C3                                                               \
47         mean0 = 1;                                                                \
48         mean1[0] = src[0];                                                        \
49         mean1[1] = src[1];                                                        \
50         mean1[2] = src[2];                                                        \
51         mean1[3] = src[3];
52
53 /* finds color distances */
54 #define COLOR_DISTANCE_C3(c1, c2)                                             \
55         ((c1[0] - c2[0])*(c1[0] - c2[0]) +                                        \
56          (c1[1] - c2[1])*(c1[1] - c2[1]) +                                        \
57          (c1[2] - c2[2])*(c1[2] - c2[2]) +                                        \
58          (c1[3] - c2[3])*(c1[3] - c2[3]))
59
60 /* this is the main kernel function for comparing color distances
61  and adding them weighted to the final color */
62 #define KERNEL_ELEMENT_C3(k)                                                  \
63         temp_color = src + deltas[k];                                             \
64         ref_color = ref + deltas[k];                                              \
65         w = weight_tab[k] + COLOR_DISTANCE_C3(ref, ref_color )*i2sigma_color;     \
66         w = 1.0/(w*w + 1);                                                        \
67         mean0 += w;                                                               \
68         mean1[0] += temp_color[0]*w;                                              \
69         mean1[1] += temp_color[1]*w;                                              \
70         mean1[2] += temp_color[2]*w;                                              \
71         mean1[3] += temp_color[3]*w;
72
73 /* write blurred values to image */
74 #define UPDATE_OUTPUT_C3                                                      \
75         mean0 = 1.0/mean0;                                                        \
76         dest[x*pix + 0] = mean1[0]*mean0;                                         \
77         dest[x*pix + 1] = mean1[1]*mean0;                                         \
78         dest[x*pix + 2] = mean1[2]*mean0;                                         \
79         dest[x*pix + 3] = mean1[3]*mean0;
80
81 /* initializes deltas for fast access to neighbour pixels */
82 #define INIT_3X3_DELTAS( deltas, step, nch )                                  \
83         ((deltas)[0] =  (nch),  (deltas)[1] = -(step) + (nch),                    \
84          (deltas)[2] = -(step), (deltas)[3] = -(step) - (nch),                    \
85          (deltas)[4] = -(nch),  (deltas)[5] =  (step) - (nch),                    \
86          (deltas)[6] =  (step), (deltas)[7] =  (step) + (nch));
87
88
89 /* code of this node was heavily inspired by the smooth function of opencv library.
90 The main change is an optional image input */
91 static void node_composit_exec_bilateralblur(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
92 {
93         NodeBilateralBlurData *nbbd= node->storage;
94         CompBuf *new, *source, *img= in[0]->data , *refimg= in[1]->data;
95         double mean0, w, i2sigma_color, i2sigma_space;
96         double mean1[4];
97         double weight_tab[8];
98         float *src, *dest, *ref, *temp_color, *ref_color;
99         float sigma_color, sigma_space;
100         int imgx, imgy, x, y, pix, i, step;
101         int deltas[8];
102         short found_determinator= 0;
103
104         if(img == NULL || out[0]->hasoutput == 0)
105                 return;
106
107         if(img->type != CB_RGBA) {
108                 img= typecheck_compbuf(in[0]->data, CB_RGBA);
109         }
110
111         imgx= img->x;
112         imgy= img->y;
113         pix= img->type;
114         step= pix * imgx;
115
116         if(refimg) {
117                 if(refimg->x == imgx && refimg->y == imgy) {
118                         if(ELEM3(refimg->type, CB_VAL, CB_VEC2, CB_VEC3)) {
119                                 refimg= typecheck_compbuf(in[1]->data, CB_RGBA);
120                                 found_determinator= 1;
121                         }
122                 }
123         }
124         else {
125                 refimg= img;
126         }
127
128         /* allocs */
129         source= dupalloc_compbuf(img);
130         new= alloc_compbuf(imgx, imgy, pix, 1);
131         
132         /* accept image offsets from other nodes */
133         new->xof= img->xof;
134         new->yof= img->yof;
135
136         /* bilateral code properties */
137         sigma_color= nbbd->sigma_color;
138         sigma_space= nbbd->sigma_space;
139         
140         i2sigma_color= 1. / (sigma_color * sigma_color);
141         i2sigma_space= 1. / (sigma_space * sigma_space);
142
143         INIT_3X3_DELTAS(deltas, step, pix);
144
145         weight_tab[0] = weight_tab[2] = weight_tab[4] = weight_tab[6] = i2sigma_space;
146         weight_tab[1] = weight_tab[3] = weight_tab[5] = weight_tab[7] = i2sigma_space * 2;
147
148         /* iterations */
149         for(i= 0; i < nbbd->iter; i++) {
150                 src= source->rect;
151                 ref= refimg->rect;
152                 dest= new->rect;
153                 /*goes through image, there are more loops for 1st/last line and all other lines*/
154                 /*kernel element accumulates surrounding colors, which are then written with the update_output function*/
155                 for(x= 0; x < imgx; x++, src+= pix, ref+= pix) {
156                         INIT_C3;
157
158                         KERNEL_ELEMENT_C3(6);
159
160                         if(x > 0) {
161                                 KERNEL_ELEMENT_C3(5);
162                                 KERNEL_ELEMENT_C3(4);
163                         }
164
165                         if(x < imgx - 1) {
166                                 KERNEL_ELEMENT_C3(7);
167                                 KERNEL_ELEMENT_C3(0);
168                         }
169
170                         UPDATE_OUTPUT_C3;
171                 }
172
173                 dest+= step;
174
175                 for(y= 1; y < imgy - 1; y++, dest+= step, src+= pix, ref+= pix) {
176                         x= 0;
177
178                         INIT_C3;
179
180                         KERNEL_ELEMENT_C3(0);
181                         KERNEL_ELEMENT_C3(1);
182                         KERNEL_ELEMENT_C3(2);
183                         KERNEL_ELEMENT_C3(6);
184                         KERNEL_ELEMENT_C3(7);
185
186                         UPDATE_OUTPUT_C3;
187
188                         src+= pix;
189                         ref+= pix;
190
191                         for(x= 1; x < imgx - 1; x++, src+= pix, ref+= pix) {
192                                 INIT_C3;
193
194                                 KERNEL_ELEMENT_C3(0);
195                                 KERNEL_ELEMENT_C3(1);
196                                 KERNEL_ELEMENT_C3(2);
197                                 KERNEL_ELEMENT_C3(3);
198                                 KERNEL_ELEMENT_C3(4);
199                                 KERNEL_ELEMENT_C3(5);
200                                 KERNEL_ELEMENT_C3(6);
201                                 KERNEL_ELEMENT_C3(7);
202
203                                 UPDATE_OUTPUT_C3;
204                         }
205
206                         INIT_C3;
207
208                         KERNEL_ELEMENT_C3(2);
209                         KERNEL_ELEMENT_C3(3);
210                         KERNEL_ELEMENT_C3(4);
211                         KERNEL_ELEMENT_C3(5);
212                         KERNEL_ELEMENT_C3(6);
213
214                         UPDATE_OUTPUT_C3;
215                 }
216
217                 for(x= 0; x < imgx; x++, src+= pix, ref+= pix) {
218                         INIT_C3;
219
220                         KERNEL_ELEMENT_C3(2);
221
222                         if(x > 0) {
223                                 KERNEL_ELEMENT_C3(3);
224                                 KERNEL_ELEMENT_C3(4);
225                         }
226                         if(x < imgx - 1) {
227                                 KERNEL_ELEMENT_C3(1);
228                                 KERNEL_ELEMENT_C3(0);
229                         }
230
231                         UPDATE_OUTPUT_C3;
232                 }
233
234                 if(node->exec & NODE_BREAK) break;
235
236                 SWAP(CompBuf, *source, *new);
237         }
238
239         if(img != in[0]->data)
240                 free_compbuf(img);
241
242         if(found_determinator == 1) {
243                 if(refimg != in[1]->data)
244                         free_compbuf(refimg);
245         }
246
247         out[0]->data= source;
248
249         free_compbuf(new);
250 }
251
252 static void node_composit_init_bilateralblur(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
253 {
254         NodeBilateralBlurData *nbbd= MEM_callocN(sizeof(NodeBilateralBlurData), "node bilateral blur data");
255         node->storage= nbbd;
256         nbbd->sigma_color= 0.3;
257         nbbd->sigma_space= 5.0;
258 }
259
260 void register_node_type_cmp_bilateralblur(ListBase *lb)
261 {
262         static bNodeType ntype;
263
264         node_type_base(&ntype, CMP_NODE_BILATERALBLUR, "Bilateral Blur", NODE_CLASS_OP_FILTER, NODE_OPTIONS);
265         node_type_socket_templates(&ntype, cmp_node_bilateralblur_in, cmp_node_bilateralblur_out);
266         node_type_size(&ntype, 150, 120, 200);
267         node_type_init(&ntype, node_composit_init_bilateralblur);
268         node_type_storage(&ntype, "NodeBilateralBlurData", node_free_standard_storage, node_copy_standard_storage);
269         node_type_exec(&ntype, node_composit_exec_bilateralblur);
270
271         nodeRegisterType(lb, &ntype);
272 }
273