style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / nodes / composite / nodes / node_composite_scale.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_scale.c
29  *  \ingroup cmpnodes
30  */
31
32
33 #include "node_composite_util.h"
34
35 /* **************** Scale  ******************** */
36
37 static bNodeSocketTemplate cmp_node_scale_in[]= {
38         {       SOCK_RGBA, 1, "Image",                  1.0f, 1.0f, 1.0f, 1.0f},
39         {       SOCK_FLOAT, 1, "X",                             1.0f, 0.0f, 0.0f, 0.0f, 0.0001f, CMP_SCALE_MAX, PROP_FACTOR},
40         {       SOCK_FLOAT, 1, "Y",                             1.0f, 0.0f, 0.0f, 0.0f, 0.0001f, CMP_SCALE_MAX, PROP_FACTOR},
41         {       -1, 0, ""       }
42 };
43 static bNodeSocketTemplate cmp_node_scale_out[]= {
44         {       SOCK_RGBA, 0, "Image"},
45         {       -1, 0, ""       }
46 };
47
48 /* only supports RGBA nodes now */
49 /* node->custom1 stores if input values are absolute or relative scale */
50 static void node_composit_exec_scale(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
51 {
52         if (out[0]->hasoutput==0)
53                 return;
54         
55         if (in[0]->data) {
56                 RenderData *rd= data;
57                 CompBuf *stackbuf, *cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
58                 ImBuf *ibuf;
59                 int newx, newy;
60                 
61                 if (node->custom1==CMP_SCALE_RELATIVE) {
62                         newx= MAX2((int)(in[1]->vec[0]*cbuf->x), 1);
63                         newy= MAX2((int)(in[2]->vec[0]*cbuf->y), 1);
64                 }
65                 else if (node->custom1==CMP_SCALE_SCENEPERCENT) {
66                         newx = cbuf->x * (rd->size / 100.0f);
67                         newy = cbuf->y * (rd->size / 100.0f);
68                 }
69                 else if (node->custom1==CMP_SCALE_RENDERPERCENT) {
70                         newx= (rd->xsch * rd->size)/100;
71                         newy= (rd->ysch * rd->size)/100;
72                 }
73                 else {  /* CMP_SCALE_ABSOLUTE */
74                         newx= MAX2((int)in[1]->vec[0], 1);
75                         newy= MAX2((int)in[2]->vec[0], 1);
76                 }
77                 newx= MIN2(newx, CMP_SCALE_MAX);
78                 newy= MIN2(newy, CMP_SCALE_MAX);
79
80                 ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
81                 if (ibuf) {
82                         ibuf->rect_float= cbuf->rect;
83                         IMB_scaleImBuf(ibuf, newx, newy);
84                         
85                         if (ibuf->rect_float == cbuf->rect) {
86                                 /* no scaling happened. */
87                                 stackbuf= pass_on_compbuf(in[0]->data);
88                         }
89                         else {
90                                 stackbuf= alloc_compbuf(newx, newy, CB_RGBA, 0);
91                                 stackbuf->rect= ibuf->rect_float;
92                                 stackbuf->malloc= 1;
93                         }
94
95                         ibuf->rect_float= NULL;
96                         ibuf->mall &= ~IB_rectfloat;
97                         IMB_freeImBuf(ibuf);
98                         
99                         /* also do the translation vector */
100                         stackbuf->xof = (int)(((float)newx/(float)cbuf->x) * (float)cbuf->xof);
101                         stackbuf->yof = (int)(((float)newy/(float)cbuf->y) * (float)cbuf->yof);
102                 }
103                 else {
104                         stackbuf= dupalloc_compbuf(cbuf);
105                         printf("Scaling to %dx%d failed\n", newx, newy);
106                 }
107                 
108                 out[0]->data= stackbuf;
109                 if (cbuf!=in[0]->data)
110                         free_compbuf(cbuf);
111         }
112         else if (node->custom1==CMP_SCALE_ABSOLUTE) {
113                 CompBuf *stackbuf;
114                 int a, x, y;
115                 float *fp;
116
117                 x = MAX2((int)in[1]->vec[0], 1);
118                 y = MAX2((int)in[2]->vec[0], 1);
119
120                 stackbuf = alloc_compbuf(x, y, CB_RGBA, 1);
121                 fp = stackbuf->rect;
122
123                 a = stackbuf->x * stackbuf->y;
124                 while(a--) {
125                         copy_v4_v4(fp, in[0]->vec);
126                         fp += 4;
127                 }
128
129                 out[0]->data= stackbuf;
130         }
131 }
132
133 void register_node_type_cmp_scale(bNodeTreeType *ttype)
134 {
135         static bNodeType ntype;
136
137         node_type_base(ttype, &ntype, CMP_NODE_SCALE, "Scale", NODE_CLASS_DISTORT, NODE_OPTIONS);
138         node_type_socket_templates(&ntype, cmp_node_scale_in, cmp_node_scale_out);
139         node_type_size(&ntype, 140, 100, 320);
140         node_type_exec(&ntype, node_composit_exec_scale);
141
142         nodeRegisterType(ttype, &ntype);
143 }