Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_composite_scale.c « nodes « composite « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1df677247620841ef9e9fd90fb6630c62184a7ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2006 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/nodes/composite/nodes/node_composite_scale.c
 *  \ingroup cmpnodes
 */


#include "node_composite_util.h"

/* **************** Scale  ******************** */

static bNodeSocketTemplate cmp_node_scale_in[] = {
	{   SOCK_RGBA, 1, N_("Image"),          1.0f, 1.0f, 1.0f, 1.0f},
	{   SOCK_FLOAT, 1, N_("X"),             1.0f, 0.0f, 0.0f, 0.0f, 0.0001f, CMP_SCALE_MAX, PROP_FACTOR},
	{   SOCK_FLOAT, 1, N_("Y"),             1.0f, 0.0f, 0.0f, 0.0f, 0.0001f, CMP_SCALE_MAX, PROP_FACTOR},
	{   -1, 0, ""   }
};
static bNodeSocketTemplate cmp_node_scale_out[] = {
	{   SOCK_RGBA, 0, N_("Image")},
	{   -1, 0, ""   }
};

/* only supports RGBA nodes now */
/* node->custom1 stores if input values are absolute or relative scale */
static void node_composit_exec_scale(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
{
	if (out[0]->hasoutput == 0)
		return;

	if (in[0]->data) {
		RenderData *rd = data;
		CompBuf *stackbuf, *cbuf = typecheck_compbuf(in[0]->data, CB_RGBA);
		ImBuf *ibuf;
		int newx, newy;
		float ofsx = 0.0f, ofsy = 0.0f;

		if (node->custom1 == CMP_SCALE_RELATIVE) {
			newx = MAX2((int)(in[1]->vec[0] * cbuf->x), 1);
			newy = MAX2((int)(in[2]->vec[0] * cbuf->y), 1);
		}
		else if (node->custom1 == CMP_SCALE_SCENEPERCENT) {
			newx = cbuf->x * (rd->size / 100.0f);
			newy = cbuf->y * (rd->size / 100.0f);
		}
		else if (node->custom1 == CMP_SCALE_RENDERPERCENT) {

			if (node->custom3 != 0.0f || node->custom4 != 0.0f) {
				const float w_dst = (rd->xsch * rd->size) / 100;
				const float h_dst = (rd->ysch * rd->size) / 100;

				if (w_dst > h_dst) {
					ofsx = node->custom3 * w_dst;
					ofsy = node->custom4 * w_dst;
				}
				else {
					ofsx = node->custom3 * h_dst;
					ofsy = node->custom4 * h_dst;
				}
			}

			/* supports framing options */
			if (node->custom2 & CMP_SCALE_RENDERSIZE_FRAME_ASPECT) {
				/* apply aspect from clip */
				const float w_src = cbuf->x;
				const float h_src = cbuf->y;

				/* destination aspect is already applied from the camera frame */
				const float w_dst = (rd->xsch * rd->size) / 100;
				const float h_dst = (rd->ysch * rd->size) / 100;

				const float asp_src = w_src / h_src;
				const float asp_dst = w_dst / h_dst;

				if (fabsf(asp_src - asp_dst) >= FLT_EPSILON) {
					if ((asp_src > asp_dst) == ((node->custom2 & CMP_SCALE_RENDERSIZE_FRAME_CROP) != 0)) {
						/* fit X */
						const float div = asp_src / asp_dst;
						newx = w_dst * div;
						newy = h_dst;
					}
					else {
						/* fit Y */
						const float div = asp_dst / asp_src;
						newx = w_dst;
						newy = h_dst * div;
					}
				}
				else {
					/* same as below - no aspect correction needed  */
					newx = w_dst;
					newy = h_dst;
				}
			}
			else {
				/* stretch */
				newx = (rd->xsch * rd->size) / 100;
				newy = (rd->ysch * rd->size) / 100;
			}
		}
		else {  /* CMP_SCALE_ABSOLUTE */
			newx = MAX2((int)in[1]->vec[0], 1);
			newy = MAX2((int)in[2]->vec[0], 1);
		}
		newx = MIN2(newx, CMP_SCALE_MAX);
		newy = MIN2(newy, CMP_SCALE_MAX);

		ibuf = IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
		if (ibuf) {
			ibuf->rect_float = cbuf->rect;
			IMB_scaleImBuf(ibuf, newx, newy);

			if (ibuf->rect_float == cbuf->rect) {
				/* no scaling happened. */
				stackbuf = pass_on_compbuf(in[0]->data);
			}
			else {
				stackbuf = alloc_compbuf(newx, newy, CB_RGBA, 0);
				stackbuf->rect = ibuf->rect_float;
				stackbuf->malloc = 1;
			}

			ibuf->rect_float = NULL;
			ibuf->mall &= ~IB_rectfloat;
			IMB_freeImBuf(ibuf);

			/* also do the translation vector */
			stackbuf->xof = (int)(ofsx + (((float)newx / (float)cbuf->x) * (float)cbuf->xof));
			stackbuf->yof = (int)(ofsy + (((float)newy / (float)cbuf->y) * (float)cbuf->yof));
		}
		else {
			stackbuf = dupalloc_compbuf(cbuf);
			printf("Scaling to %dx%d failed\n", newx, newy);
		}

		out[0]->data = stackbuf;
		if (cbuf != in[0]->data)
			free_compbuf(cbuf);
	}
	else if (node->custom1 == CMP_SCALE_ABSOLUTE) {
		CompBuf *stackbuf;
		int a, x, y;
		float *fp;

		x = MAX2((int)in[1]->vec[0], 1);
		y = MAX2((int)in[2]->vec[0], 1);

		stackbuf = alloc_compbuf(x, y, CB_RGBA, 1);
		fp = stackbuf->rect;

		a = stackbuf->x * stackbuf->y;
		while (a--) {
			copy_v4_v4(fp, in[0]->vec);
			fp += 4;
		}

		out[0]->data = stackbuf;
	}
}

void register_node_type_cmp_scale(bNodeTreeType *ttype)
{
	static bNodeType ntype;

	node_type_base(ttype, &ntype, CMP_NODE_SCALE, "Scale", NODE_CLASS_DISTORT, NODE_OPTIONS);
	node_type_socket_templates(&ntype, cmp_node_scale_in, cmp_node_scale_out);
	node_type_size(&ntype, 140, 100, 320);
	node_type_exec(&ntype, node_composit_exec_scale);

	nodeRegisterType(ttype, &ntype);
}