aea4da920a0c20f4b605d9b5d2e0b752390196af
[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         if (this->is_aspect) {
195                 /* apply aspect from clip */
196                 const float w_src = inputOperation->getWidth();
197                 const float h_src = inputOperation->getHeight();
198
199                 /* destination aspect is already applied from the camera frame */
200                 const float w_dst = this->newWidth;
201                 const float h_dst = this->newHeight;
202
203                 const float asp_src = w_src / h_src;
204                 const float asp_dst = w_dst / h_dst;
205
206                 this->offsetX = 0.0f;
207                 this->offsetY = 0.0f;
208
209                 if (fabsf(asp_src - asp_dst) >= FLT_EPSILON) {
210                         if ((asp_src > asp_dst) == (this->is_crop == true)) {
211                                 /* fit X */
212                                 const float div = asp_src / asp_dst;
213                                 this->relX /= div;
214                                 this->offsetX = ((w_src - (w_src * div)) / (w_src / w_dst)) / 2.0f;
215                         }
216                         else {
217                                 /* fit Y */
218                                 const float div = asp_dst / asp_src;
219                                 this->relY /= div;
220                                 this->offsetY = ((h_src - (h_src * div)) / (h_src / h_dst)) / 2.0f;
221                         }
222                 }
223
224         }
225 }
226
227 void ScaleFixedSizeOperation::deinitExecution()
228 {
229         this->inputOperation = NULL;
230 }
231
232
233 void ScaleFixedSizeOperation::executePixel(float *color, float x, float y, PixelSampler sampler, MemoryBuffer *inputBuffers[])
234 {
235 #ifdef USE_FORCE_BICUBIC
236         sampler = COM_PS_BICUBIC;
237 #endif
238
239         if (this->is_aspect) {
240                 float nx = ((x - this->offsetX) * relX);
241                 float ny = ((y - this->offsetY) * relY);
242                 this->inputOperation->read(color, nx, ny, sampler, inputBuffers);
243         }
244         else {
245                 this->inputOperation->read(color, x * relX, y * relY, sampler, inputBuffers);
246         }
247 }
248
249 bool ScaleFixedSizeOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
250 {
251         rcti newInput;
252
253         newInput.xmax = input->xmax * relX;
254         newInput.xmin = input->xmin * relX;
255         newInput.ymax = input->ymax * relY;
256         newInput.ymin = input->ymin * relY;
257
258         return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
259 }
260
261 void ScaleFixedSizeOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
262 {
263         unsigned int nr[2];
264         nr[0] = newWidth;
265         nr[1] = newHeight;
266         NodeOperation::determineResolution(resolution, nr);
267         resolution[0] = newWidth;
268         resolution[1] = newHeight;
269 }