Camera tracking integration
[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                 } else {        /* CMP_SCALE_ABSOLUTE */
73                         newx= MAX2((int)in[1]->vec[0], 1);
74                         newy= MAX2((int)in[2]->vec[0], 1);
75                 }
76                 newx= MIN2(newx, CMP_SCALE_MAX);
77                 newy= MIN2(newy, CMP_SCALE_MAX);
78
79                 ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
80                 if(ibuf) {
81                         ibuf->rect_float= cbuf->rect;
82                         IMB_scaleImBuf(ibuf, newx, newy);
83                         
84                         if(ibuf->rect_float == cbuf->rect) {
85                                 /* no scaling happened. */
86                                 stackbuf= pass_on_compbuf(in[0]->data);
87                         }
88                         else {
89                                 stackbuf= alloc_compbuf(newx, newy, CB_RGBA, 0);
90                                 stackbuf->rect= ibuf->rect_float;
91                                 stackbuf->malloc= 1;
92                         }
93
94                         ibuf->rect_float= NULL;
95                         ibuf->mall &= ~IB_rectfloat;
96                         IMB_freeImBuf(ibuf);
97                         
98                         /* also do the translation vector */
99                         stackbuf->xof = (int)(((float)newx/(float)cbuf->x) * (float)cbuf->xof);
100                         stackbuf->yof = (int)(((float)newy/(float)cbuf->y) * (float)cbuf->yof);
101                 }
102                 else {
103                         stackbuf= dupalloc_compbuf(cbuf);
104                         printf("Scaling to %dx%d failed\n", newx, newy);
105                 }
106                 
107                 out[0]->data= stackbuf;
108                 if(cbuf!=in[0]->data)
109                         free_compbuf(cbuf);
110         }
111         else if (node->custom1==CMP_SCALE_ABSOLUTE) {
112                 CompBuf *stackbuf;
113                 int a, x, y;
114                 float *fp;
115
116                 x = MAX2((int)in[1]->vec[0], 1);
117                 y = MAX2((int)in[2]->vec[0], 1);
118
119                 stackbuf = alloc_compbuf(x, y, CB_RGBA, 1);
120                 fp = stackbuf->rect;
121
122                 a = stackbuf->x * stackbuf->y;
123                 while(a--) {
124                         copy_v4_v4(fp, in[0]->vec);
125                         fp += 4;
126                 }
127
128                 out[0]->data= stackbuf;
129         }
130 }
131
132 void register_node_type_cmp_scale(ListBase *lb)
133 {
134         static bNodeType ntype;
135
136         node_type_base(&ntype, CMP_NODE_SCALE, "Scale", NODE_CLASS_DISTORT, NODE_OPTIONS);
137         node_type_socket_templates(&ntype, cmp_node_scale_in, cmp_node_scale_out);
138         node_type_size(&ntype, 140, 100, 320);
139         node_type_exec(&ntype, node_composit_exec_scale);
140
141         nodeRegisterType(lb, &ntype);
142 }
143
144
145
146
147
148
149