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