Directional Blur Node
[blender.git] / source / blender / nodes / intern / CMP_nodes / CMP_directionalblur.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. 
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2006 Blender Foundation.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): Alfredo de Greef  (eeshlo)
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "../CMP_util.h"
30
31 static bNodeSocketType cmp_node_dblur_in[]= {
32         {       SOCK_RGBA, 1, "Image", 0.8f, 0.8f, 0.8f, 1.f, 0.f, 1.f},
33         {       -1, 0, ""       }
34 };
35
36 static bNodeSocketType cmp_node_dblur_out[]= {
37         {       SOCK_RGBA, 0, "Image", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
38         {       -1, 0, ""       }
39 };
40
41 static CompBuf *dblur(bNode *node, CompBuf *img, int iterations, int wrap,
42                   float center_x, float center_y, float dist, float angle, float spin, float zoom)
43 {
44         if ((dist != 0.f) || (spin != 0.f) || (zoom != 0.f)) {
45                 void (*getpix)(CompBuf*, float, float, float*) = wrap ? qd_getPixelLerpWrap : qd_getPixelLerp;
46                 const float a= angle * (float)M_PI / 180.f;
47                 const float itsc= 1.f / pow(2.f, (float)iterations);
48                 float D;
49                 float center_x_pix, center_y_pix;
50                 float tx, ty;
51                 float sc, rot;
52                 CompBuf *tmp;
53                 int i, j;
54                 
55                 tmp= dupalloc_compbuf(img);
56                 
57                 D= dist * sqrtf(img->x * img->x + img->y * img->y);
58                 center_x_pix= center_x * img->x;
59                 center_y_pix= center_y * img->y;
60
61                 tx=  itsc * D * cos(a);
62                 ty= -itsc * D * sin(a);
63                 sc=  itsc * zoom;
64                 rot= itsc * spin * (float)M_PI / 180.f;
65
66                 /* blur the image */
67                 for(i= 0; i < iterations; ++i) {
68                         const float cs= cos(rot), ss= sin(rot);
69                         const float isc= 1.f / (1.f + sc);
70                         unsigned int x, y;
71                         float col[4]= {0,0,0,0};
72
73                         for(y= 0; y < img->y; ++y) {
74                                 const float v= isc * (y - center_y_pix) + ty;
75
76                                 for(x= 0; x < img->x; ++x) {
77                                         const float  u= isc * (x - center_x_pix) + tx;
78                                         unsigned int p= (x + y * img->x) * img->type;
79
80                                         getpix(tmp, cs * u + ss * v + center_x_pix, cs * v - ss * u + center_y_pix, col);
81
82                                         /* mix img and transformed tmp */
83                                         for(j= 0; j < 4; ++j)
84                                                 img->rect[p + j]= AVG2(img->rect[p + j], col[j]);
85                                 }
86                         }
87
88                         /* copy img to tmp */
89                         if(i != (iterations - 1)) 
90                                 memcpy(tmp->rect, img->rect, sizeof(float) * img->x * img->y * img->type);
91
92                         /* double transformations */
93                         tx *= 2.f, ty  *= 2.f;
94                         sc *= 2.f, rot *= 2.f;
95
96                         if(node->exec & NODE_BREAK) break;
97                 }
98
99                 free_compbuf(tmp);
100         }
101
102         return img;
103 }
104
105 static void node_composit_exec_dblur(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
106 {
107         NodeDBlurData *ndbd= node->storage;
108         CompBuf *new, *img= in[0]->data;
109         
110         if((img == NULL) || (out[0]->hasoutput == 0)) return;
111
112         if (img->type != CB_RGBA)
113                 new = typecheck_compbuf(img, CB_RGBA);
114         else
115                 new = dupalloc_compbuf(img);
116         
117         out[0]->data= dblur(node, new, ndbd->iter, ndbd->wrap, ndbd->center_x, ndbd->center_y, ndbd->distance, ndbd->angle, ndbd->spin, ndbd->zoom);
118 }
119
120 static void node_composit_init_dblur(bNode* node)
121 {
122         NodeDBlurData *ndbd= MEM_callocN(sizeof(NodeDBlurData), "node dblur data");
123         node->storage= ndbd;
124         ndbd->center_x= 0.5;
125         ndbd->center_y= 0.5;
126 }
127
128 bNodeType cmp_node_dblur = {
129         /* *next,*prev */       NULL, NULL,
130         /* type code   */       CMP_NODE_DBLUR,
131         /* name        */       "Directional Blur",
132         /* width+range */       150, 120, 200,
133         /* class+opts  */       NODE_CLASS_OP_FILTER, NODE_OPTIONS,
134         /* input sock  */       cmp_node_dblur_in,
135         /* output sock */       cmp_node_dblur_out,
136         /* storage     */       "NodeDBlurData",
137         /* execfunc    */       node_composit_exec_dblur,
138         /* butfunc     */       NULL,
139         /* initfunc    */       node_composit_init_dblur,
140         /* freestoragefunc    */        node_free_standard_storage,
141         /* copystoragefunc    */        node_copy_standard_storage,
142         /* id          */       NULL
143 };