scale node - framing offset: compatible with camera shiftX/Y and the viewport option.
[blender.git] / source / blender / compositor / operations / COM_ScaleOperation.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
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  * Contributor:
19  *              Jeroen Bakker
20  *              Monique Dewanchand
21  */
22
23 #include "COM_ScaleOperation.h"
24
25 #define USE_FORCE_BICUBIC
26 /* XXX - ignore input and use default from old compositor,
27  * could become an option like the transform node - campbell */
28
29 ScaleOperation::ScaleOperation() : NodeOperation()
30 {
31         this->addInputSocket(COM_DT_COLOR);
32         this->addInputSocket(COM_DT_VALUE);
33         this->addInputSocket(COM_DT_VALUE);
34         this->addOutputSocket(COM_DT_COLOR);
35         this->setResolutionInputSocketIndex(0);
36         this->inputOperation = NULL;
37         this->inputXOperation = NULL;
38         this->inputYOperation = NULL;
39 }
40 void ScaleOperation::initExecution()
41 {
42         this->inputOperation = this->getInputSocketReader(0);
43         this->inputXOperation = this->getInputSocketReader(1);
44         this->inputYOperation = this->getInputSocketReader(2);
45         this->centerX = this->getWidth() / 2.0;
46         this->centerY = this->getHeight() / 2.0;
47 }
48
49 void ScaleOperation::deinitExecution()
50 {
51         this->inputOperation = NULL;
52         this->inputXOperation = NULL;
53         this->inputYOperation = NULL;
54 }
55
56
57 void ScaleOperation::executePixel(float *color, float x, float y, PixelSampler sampler, MemoryBuffer *inputBuffers[])
58 {
59 #ifdef USE_FORCE_BICUBIC
60         sampler = COM_PS_BICUBIC;
61 #endif
62
63         float scaleX[4];
64         float scaleY[4];
65
66         this->inputXOperation->read(scaleX, x, y, sampler, inputBuffers);
67         this->inputYOperation->read(scaleY, x, y, sampler, inputBuffers);
68
69         const float scx = scaleX[0];
70         const float scy = scaleY[0];
71
72         float nx = this->centerX + (x - this->centerX) / scx;
73         float ny = this->centerY + (y - this->centerY) / scy;
74         this->inputOperation->read(color, nx, ny, sampler, inputBuffers);
75 }
76
77 bool ScaleOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
78 {
79         rcti newInput;
80         float scaleX[4];
81         float scaleY[4];
82
83         this->inputXOperation->read(scaleX, 0, 0, COM_PS_NEAREST, NULL);
84         this->inputYOperation->read(scaleY, 0, 0, COM_PS_NEAREST, NULL);
85
86         const float scx = scaleX[0];
87         const float scy = scaleY[0];
88
89         newInput.xmax = this->centerX + (input->xmax - this->centerX) / scx;
90         newInput.xmin = this->centerX + (input->xmin - this->centerX) / scx;
91         newInput.ymax = this->centerY + (input->ymax - this->centerY) / scy;
92         newInput.ymin = this->centerY + (input->ymin - this->centerY) / scy;
93
94         return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
95 }
96
97
98 // SCALE ABSOLUTE
99 ScaleAbsoluteOperation::ScaleAbsoluteOperation() : NodeOperation()
100 {
101         this->addInputSocket(COM_DT_COLOR);
102         this->addInputSocket(COM_DT_VALUE);
103         this->addInputSocket(COM_DT_VALUE);
104         this->addOutputSocket(COM_DT_COLOR);
105         this->setResolutionInputSocketIndex(0);
106         this->inputOperation = NULL;
107         this->inputXOperation = NULL;
108         this->inputYOperation = NULL;
109 }
110 void ScaleAbsoluteOperation::initExecution()
111 {
112         this->inputOperation = this->getInputSocketReader(0);
113         this->inputXOperation = this->getInputSocketReader(1);
114         this->inputYOperation = this->getInputSocketReader(2);
115         this->centerX = this->getWidth() / 2.0;
116         this->centerY = this->getHeight() / 2.0;
117 }
118
119 void ScaleAbsoluteOperation::deinitExecution()
120 {
121         this->inputOperation = NULL;
122         this->inputXOperation = NULL;
123         this->inputYOperation = NULL;
124 }
125
126
127 void ScaleAbsoluteOperation::executePixel(float *color, float x, float y, PixelSampler sampler, MemoryBuffer *inputBuffers[])
128 {
129 #ifdef USE_FORCE_BICUBIC
130         sampler = COM_PS_BICUBIC;
131 #endif
132
133         float scaleX[4];
134         float scaleY[4];
135
136         this->inputXOperation->read(scaleX, x, y, sampler, inputBuffers);
137         this->inputYOperation->read(scaleY, x, y, sampler, inputBuffers);
138
139         const float scx = scaleX[0]; // target absolute scale
140         const float scy = scaleY[0]; // target absolute scale
141
142         const float width = this->getWidth();
143         const float height = this->getHeight();
144         //div
145         float relativeXScale = scx / width;
146         float relativeYScale = scy / height;
147
148         float nx = this->centerX + (x - this->centerX) / relativeXScale;
149         float ny = this->centerY + (y - this->centerY) / relativeYScale;
150
151         this->inputOperation->read(color, nx, ny, sampler, inputBuffers);
152 }
153
154 bool ScaleAbsoluteOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
155 {
156         rcti newInput;
157         float scaleX[4];
158         float scaleY[4];
159
160         this->inputXOperation->read(scaleX, 0, 0, COM_PS_NEAREST, NULL);
161         this->inputYOperation->read(scaleY, 0, 0, COM_PS_NEAREST, NULL);
162
163         const float scx = scaleX[0];
164         const float scy = scaleY[0];
165         const float width = this->getWidth();
166         const float height = this->getHeight();
167         //div
168         float relateveXScale = scx / width;
169         float relateveYScale = scy / height;
170
171         newInput.xmax = this->centerX + (input->xmax - this->centerX) / relateveXScale;
172         newInput.xmin = this->centerX + (input->xmin - this->centerX) / relateveXScale;
173         newInput.ymax = this->centerY + (input->ymax - this->centerY) / relateveYScale;
174         newInput.ymin = this->centerY + (input->ymin - this->centerY) / relateveYScale;
175
176         return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
177 }
178
179
180 // Absolute fixed siez
181 ScaleFixedSizeOperation::ScaleFixedSizeOperation() : NodeOperation()
182 {
183         this->addInputSocket(COM_DT_COLOR);
184         this->addOutputSocket(COM_DT_COLOR);
185         this->setResolutionInputSocketIndex(0);
186         this->inputOperation = NULL;
187 }
188 void ScaleFixedSizeOperation::initExecution()
189 {
190         this->inputOperation = this->getInputSocketReader(0);
191         this->relX = inputOperation->getWidth() / (float)this->newWidth;
192         this->relY = inputOperation->getHeight() / (float)this->newHeight;
193
194
195         /* *** all the options below are for a fairly special case - camera framing *** */
196         if (this->offsetX != 0.0f || this->offsetY != 0.0f) {
197                 this->is_offset = true;
198
199                 if (this->newWidth > this->newHeight) {
200                         this->offsetX *= this->newWidth;
201                         this->offsetY *= this->newWidth;
202                 }
203                 else {
204                         this->offsetX *= this->newHeight;
205                         this->offsetY *= this->newHeight;
206                 }
207         }
208
209         if (this->is_aspect) {
210                 /* apply aspect from clip */
211                 const float w_src = inputOperation->getWidth();
212                 const float h_src = inputOperation->getHeight();
213
214                 /* destination aspect is already applied from the camera frame */
215                 const float w_dst = this->newWidth;
216                 const float h_dst = this->newHeight;
217
218                 const float asp_src = w_src / h_src;
219                 const float asp_dst = w_dst / h_dst;
220
221                 if (fabsf(asp_src - asp_dst) >= FLT_EPSILON) {
222                         if ((asp_src > asp_dst) == (this->is_crop == true)) {
223                                 /* fit X */
224                                 const float div = asp_src / asp_dst;
225                                 this->relX /= div;
226                                 this->offsetX = ((w_src - (w_src * div)) / (w_src / w_dst)) / 2.0f;
227                         }
228                         else {
229                                 /* fit Y */
230                                 const float div = asp_dst / asp_src;
231                                 this->relY /= div;
232                                 this->offsetY = ((h_src - (h_src * div)) / (h_src / h_dst)) / 2.0f;
233                         }
234
235                         this->is_offset = true;
236                 }
237         }
238         /* *** end framing options *** */
239 }
240
241 void ScaleFixedSizeOperation::deinitExecution()
242 {
243         this->inputOperation = NULL;
244 }
245
246
247 void ScaleFixedSizeOperation::executePixel(float *color, float x, float y, PixelSampler sampler, MemoryBuffer *inputBuffers[])
248 {
249 #ifdef USE_FORCE_BICUBIC
250         sampler = COM_PS_BICUBIC;
251 #endif
252
253         if (this->is_offset) {
254                 float nx = ((x - this->offsetX) * relX);
255                 float ny = ((y - this->offsetY) * relY);
256                 this->inputOperation->read(color, nx, ny, sampler, inputBuffers);
257         }
258         else {
259                 this->inputOperation->read(color, x * relX, y * relY, sampler, inputBuffers);
260         }
261 }
262
263 bool ScaleFixedSizeOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
264 {
265         rcti newInput;
266
267         newInput.xmax = input->xmax * relX;
268         newInput.xmin = input->xmin * relX;
269         newInput.ymax = input->ymax * relY;
270         newInput.ymin = input->ymin * relY;
271
272         return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
273 }
274
275 void ScaleFixedSizeOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
276 {
277         unsigned int nr[2];
278         nr[0] = newWidth;
279         nr[1] = newHeight;
280         NodeOperation::determineResolution(resolution, nr);
281         resolution[0] = newWidth;
282         resolution[1] = newHeight;
283 }