style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / nodes / composite / nodes / node_composite_rotate.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_rotate.c
29  *  \ingroup cmpnodes
30  */
31
32
33 #include "node_composite_util.h"
34
35 /* **************** Rotate  ******************** */
36
37 static bNodeSocketTemplate cmp_node_rotate_in[]= {
38         {       SOCK_RGBA, 1, "Image",                  1.0f, 1.0f, 1.0f, 1.0f},
39         {       SOCK_FLOAT, 1, "Degr",                  0.0f, 0.0f, 0.0f, 0.0f, -10000.0f, 10000.0f, PROP_ANGLE},
40         {       -1, 0, ""       }
41 };
42 static bNodeSocketTemplate cmp_node_rotate_out[]= {
43         {       SOCK_RGBA, 0, "Image"},
44         {       -1, 0, ""       }
45 };
46
47 /* only supports RGBA nodes now */
48 static void node_composit_exec_rotate(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
49 {
50
51         if (out[0]->hasoutput==0)
52                 return;
53
54         if (in[0]->data) {
55                 CompBuf *cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
56                 CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* note, this returns zero'd image */
57                 float rad, u, v, s, c, centx, centy, miny, maxy, minx, maxx;
58                 int x, y, yo, xo;
59                 ImBuf *ibuf, *obuf;
60
61                 rad= in[1]->vec[0];
62
63
64                 s= sin(rad);
65                 c= cos(rad);
66                 centx= cbuf->x/2;
67                 centy= cbuf->y/2;
68
69                 minx= -centx;
70                 maxx= -centx + (float)cbuf->x;
71                 miny= -centy;
72                 maxy= -centy + (float)cbuf->y;
73
74
75                 ibuf=IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
76                 obuf=IMB_allocImBuf(stackbuf->x, stackbuf->y, 32, 0);
77
78                 if (ibuf && obuf) {
79                         ibuf->rect_float=cbuf->rect;
80                         obuf->rect_float=stackbuf->rect;
81
82                         for (y=miny; y<maxy; y++) {
83                                 yo= y+(int)centy;
84
85                                 for (x=minx; x<maxx;x++) {
86                                         u=c*x + y*s + centx;
87                                         v=-s*x + c*y + centy;
88                                         xo= x+(int)centx;
89
90                                         switch(node->custom1) {
91                                         case 0:
92                                                 neareast_interpolation(ibuf, obuf, u, v, xo, yo);
93                                                 break;
94                                         case 1:
95                                                 bilinear_interpolation(ibuf, obuf, u, v, xo, yo);
96                                                 break;
97                                         case 2:
98                                                 bicubic_interpolation(ibuf, obuf, u, v, xo, yo);
99                                                 break;
100                                         }
101
102                                 }
103                         }
104
105                         /* rotate offset vector too, but why negative rad, ehh?? Has to be replaced with [3][3] matrix once (ton) */
106                         s= sin(-rad);
107                         c= cos(-rad);
108                         centx= (float)cbuf->xof; centy= (float)cbuf->yof;
109                         stackbuf->xof= (int)( c*centx + s*centy);
110                         stackbuf->yof= (int)(-s*centx + c*centy);
111
112                         IMB_freeImBuf(ibuf);
113                         IMB_freeImBuf(obuf);
114                 }
115
116                 /* pass on output and free */
117                 out[0]->data= stackbuf;
118                 if (cbuf!=in[0]->data) {
119                         free_compbuf(cbuf);
120                 }
121         }
122 }
123
124 static void node_composit_init_rotate(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
125 {
126         node->custom1= 1; /* Bilinear Filter*/
127 }
128
129 void register_node_type_cmp_rotate(bNodeTreeType *ttype)
130 {
131         static bNodeType ntype;
132
133         node_type_base(ttype, &ntype, CMP_NODE_ROTATE, "Rotate", NODE_CLASS_DISTORT, NODE_OPTIONS);
134         node_type_socket_templates(&ntype, cmp_node_rotate_in, cmp_node_rotate_out);
135         node_type_size(&ntype, 140, 100, 320);
136         node_type_init(&ntype, node_composit_init_rotate);
137         node_type_exec(&ntype, node_composit_exec_rotate);
138
139         nodeRegisterType(ttype, &ntype);
140 }