new scaling options to scale footage without stretching - add stretch/fit/crop to...
[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, N_("Image"),          1.0f, 1.0f, 1.0f, 1.0f},
39         {   SOCK_FLOAT, 1, N_("X"),             1.0f, 0.0f, 0.0f, 0.0f, 0.0001f, CMP_SCALE_MAX, PROP_FACTOR},
40         {   SOCK_FLOAT, 1, N_("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, N_("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                         /* supports framing options */
71                         if (node->custom2 & CMP_SCALE_RENDERSIZE_FRAME_ASPECT) {
72                                 /* apply aspect from clip */
73                                 const float w_src = cbuf->x;
74                                 const float h_src = cbuf->y;
75
76                                 /* destination aspect is already applied from the camera frame */
77                                 const float w_dst = (rd->xsch * rd->size) / 100;
78                                 const float h_dst = (rd->ysch * rd->size) / 100;
79
80                                 const float asp_src = w_src / h_src;
81                                 const float asp_dst = w_dst / h_dst;
82
83                                 if (fabsf(asp_src - asp_dst) >= FLT_EPSILON) {
84                                         if ((asp_src > asp_dst) == ((node->custom2 & CMP_SCALE_RENDERSIZE_FRAME_CROP) != 0)) {
85                                                 /* fit X */
86                                                 const float div = asp_src / asp_dst;
87                                                 newx = w_dst * div;
88                                                 newy = h_dst;
89                                         }
90                                         else {
91                                                 /* fit Y */
92                                                 const float div = asp_dst / asp_src;
93                                                 newx = w_dst;
94                                                 newy = h_dst * div;
95                                         }
96                                 }
97                                 else {
98                                         /* same as below - no aspect correction needed  */
99                                         newx = w_dst;
100                                         newy = h_dst;
101                                 }
102                         }
103                         else {
104                                 /* stretch */
105                                 newx = (rd->xsch * rd->size) / 100;
106                                 newy = (rd->ysch * rd->size) / 100;
107                         }
108                 }
109                 else {  /* CMP_SCALE_ABSOLUTE */
110                         newx = MAX2((int)in[1]->vec[0], 1);
111                         newy = MAX2((int)in[2]->vec[0], 1);
112                 }
113                 newx = MIN2(newx, CMP_SCALE_MAX);
114                 newy = MIN2(newy, CMP_SCALE_MAX);
115
116                 ibuf = IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
117                 if (ibuf) {
118                         ibuf->rect_float = cbuf->rect;
119                         IMB_scaleImBuf(ibuf, newx, newy);
120
121                         if (ibuf->rect_float == cbuf->rect) {
122                                 /* no scaling happened. */
123                                 stackbuf = pass_on_compbuf(in[0]->data);
124                         }
125                         else {
126                                 stackbuf = alloc_compbuf(newx, newy, CB_RGBA, 0);
127                                 stackbuf->rect = ibuf->rect_float;
128                                 stackbuf->malloc = 1;
129                         }
130
131                         ibuf->rect_float = NULL;
132                         ibuf->mall &= ~IB_rectfloat;
133                         IMB_freeImBuf(ibuf);
134
135                         /* also do the translation vector */
136                         stackbuf->xof = (int)(((float)newx / (float)cbuf->x) * (float)cbuf->xof);
137                         stackbuf->yof = (int)(((float)newy / (float)cbuf->y) * (float)cbuf->yof);
138                 }
139                 else {
140                         stackbuf = dupalloc_compbuf(cbuf);
141                         printf("Scaling to %dx%d failed\n", newx, newy);
142                 }
143
144                 out[0]->data = stackbuf;
145                 if (cbuf != in[0]->data)
146                         free_compbuf(cbuf);
147         }
148         else if (node->custom1 == CMP_SCALE_ABSOLUTE) {
149                 CompBuf *stackbuf;
150                 int a, x, y;
151                 float *fp;
152
153                 x = MAX2((int)in[1]->vec[0], 1);
154                 y = MAX2((int)in[2]->vec[0], 1);
155
156                 stackbuf = alloc_compbuf(x, y, CB_RGBA, 1);
157                 fp = stackbuf->rect;
158
159                 a = stackbuf->x * stackbuf->y;
160                 while (a--) {
161                         copy_v4_v4(fp, in[0]->vec);
162                         fp += 4;
163                 }
164
165                 out[0]->data = stackbuf;
166         }
167 }
168
169 void register_node_type_cmp_scale(bNodeTreeType *ttype)
170 {
171         static bNodeType ntype;
172
173         node_type_base(ttype, &ntype, CMP_NODE_SCALE, "Scale", NODE_CLASS_DISTORT, NODE_OPTIONS);
174         node_type_socket_templates(&ntype, cmp_node_scale_in, cmp_node_scale_out);
175         node_type_size(&ntype, 140, 100, 320);
176         node_type_exec(&ntype, node_composit_exec_scale);
177
178         nodeRegisterType(ttype, &ntype);
179 }