Bugfix [#28515] Dupliframes start/end animation broken since 2.56A
[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",                  0.8f, 0.8f, 0.8f, 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= (M_PI*in[1]->vec[0])/180.0f;
62
63                 s= sin(rad);
64                 c= cos(rad);
65                 centx= cbuf->x/2;
66                 centy= cbuf->y/2;
67
68                 minx= -centx;
69                 maxx= -centx + (float)cbuf->x;
70                 miny= -centy;
71                 maxy= -centy + (float)cbuf->y;
72
73
74                 ibuf=IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
75                 obuf=IMB_allocImBuf(stackbuf->x, stackbuf->y, 32, 0);
76
77                 if(ibuf && obuf){
78                         ibuf->rect_float=cbuf->rect;
79                         obuf->rect_float=stackbuf->rect;
80
81                         for(y=miny; y<maxy; y++) {
82                                 yo= y+(int)centy;
83
84                                 for(x=minx; x<maxx;x++) {
85                                         u=c*x + y*s + centx;
86                                         v=-s*x + c*y + centy;
87                                         xo= x+(int)centx;
88
89                                         switch(node->custom1) {
90                                         case 0:
91                                                 neareast_interpolation(ibuf, obuf, u, v, xo, yo);
92                                                 break ;
93                                         case 1:
94                                                 bilinear_interpolation(ibuf, obuf, u, v, xo, yo);
95                                                 break;
96                                         case 2:
97                                                 bicubic_interpolation(ibuf, obuf, u, v, xo, yo);
98                                                 break;
99                                         }
100
101                                 }
102                         }
103
104                         /* rotate offset vector too, but why negative rad, ehh?? Has to be replaced with [3][3] matrix once (ton) */
105                         s= sin(-rad);
106                         c= cos(-rad);
107                         centx= (float)cbuf->xof; centy= (float)cbuf->yof;
108                         stackbuf->xof= (int)( c*centx + s*centy);
109                         stackbuf->yof= (int)(-s*centx + c*centy);
110
111                         IMB_freeImBuf(ibuf);
112                         IMB_freeImBuf(obuf);
113                 }
114
115                 /* pass on output and free */
116                 out[0]->data= stackbuf;
117                 if(cbuf!=in[0]->data) {
118                         free_compbuf(cbuf);
119                 }
120         }
121 }
122
123 static void node_composit_init_rotate(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
124 {
125         node->custom1= 1; /* Bilinear Filter*/
126 }
127
128 void register_node_type_cmp_rotate(ListBase *lb)
129 {
130         static bNodeType ntype;
131
132         node_type_base(&ntype, CMP_NODE_ROTATE, "Rotate", NODE_CLASS_DISTORT, NODE_OPTIONS);
133         node_type_socket_templates(&ntype, cmp_node_rotate_in, cmp_node_rotate_out);
134         node_type_size(&ntype, 140, 100, 320);
135         node_type_init(&ntype, node_composit_init_rotate);
136         node_type_exec(&ntype, node_composit_exec_rotate);
137
138         nodeRegisterType(lb, &ntype);
139 }
140