642d18780498e694768eb7b7f817ba2e073b0987
[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->m_inputOperation = NULL;
37         this->m_inputXOperation = NULL;
38         this->m_inputYOperation = NULL;
39 }
40 void ScaleOperation::initExecution()
41 {
42         this->m_inputOperation = this->getInputSocketReader(0);
43         this->m_inputXOperation = this->getInputSocketReader(1);
44         this->m_inputYOperation = this->getInputSocketReader(2);
45         this->m_centerX = this->getWidth() / 2.0;
46         this->m_centerY = this->getHeight() / 2.0;
47 }
48
49 void ScaleOperation::deinitExecution()
50 {
51         this->m_inputOperation = NULL;
52         this->m_inputXOperation = NULL;
53         this->m_inputYOperation = NULL;
54 }
55
56
57 void ScaleOperation::executePixel(float *color, float x, float y, PixelSampler sampler)
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->m_inputXOperation->read(scaleX, x, y, sampler);
67         this->m_inputYOperation->read(scaleY, x, y, sampler);
68
69         const float scx = scaleX[0];
70         const float scy = scaleY[0];
71
72         float nx = this->m_centerX + (x - this->m_centerX) / scx;
73         float ny = this->m_centerY + (y - this->m_centerY) / scy;
74         this->m_inputOperation->read(color, nx, ny, sampler);
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->m_inputXOperation->read(scaleX, 0, 0, COM_PS_NEAREST);
84         this->m_inputYOperation->read(scaleY, 0, 0, COM_PS_NEAREST);
85
86         const float scx = scaleX[0];
87         const float scy = scaleY[0];
88
89         newInput.xmax = this->m_centerX + (input->xmax - this->m_centerX) / scx;
90         newInput.xmin = this->m_centerX + (input->xmin - this->m_centerX) / scx;
91         newInput.ymax = this->m_centerY + (input->ymax - this->m_centerY) / scy;
92         newInput.ymin = this->m_centerY + (input->ymin - this->m_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->m_inputOperation = NULL;
107         this->m_inputXOperation = NULL;
108         this->m_inputYOperation = NULL;
109 }
110 void ScaleAbsoluteOperation::initExecution()
111 {
112         this->m_inputOperation = this->getInputSocketReader(0);
113         this->m_inputXOperation = this->getInputSocketReader(1);
114         this->m_inputYOperation = this->getInputSocketReader(2);
115         this->m_centerX = this->getWidth() / 2.0;
116         this->m_centerY = this->getHeight() / 2.0;
117 }
118
119 void ScaleAbsoluteOperation::deinitExecution()
120 {
121         this->m_inputOperation = NULL;
122         this->m_inputXOperation = NULL;
123         this->m_inputYOperation = NULL;
124 }
125
126
127 void ScaleAbsoluteOperation::executePixel(float *color, float x, float y, PixelSampler sampler)
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->m_inputXOperation->read(scaleX, x, y, sampler);
137         this->m_inputYOperation->read(scaleY, x, y, sampler);
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->m_centerX + (x - this->m_centerX) / relativeXScale;
149         float ny = this->m_centerY + (y - this->m_centerY) / relativeYScale;
150
151         this->m_inputOperation->read(color, nx, ny, sampler);
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->m_inputXOperation->read(scaleX, 0, 0, COM_PS_NEAREST);
161         this->m_inputYOperation->read(scaleY, 0, 0, COM_PS_NEAREST);
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->m_centerX + (input->xmax - this->m_centerX) / relateveXScale;
172         newInput.xmin = this->m_centerX + (input->xmin - this->m_centerX) / relateveXScale;
173         newInput.ymax = this->m_centerY + (input->ymax - this->m_centerY) / relateveYScale;
174         newInput.ymin = this->m_centerY + (input->ymin - this->m_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, COM_SC_NO_RESIZE);
184         this->addOutputSocket(COM_DT_COLOR);
185         this->setResolutionInputSocketIndex(0);
186         this->m_inputOperation = NULL;
187         this->m_is_offset = false;
188 }
189 void ScaleFixedSizeOperation::initExecution()
190 {
191         this->m_inputOperation = this->getInputSocketReader(0);
192         this->m_relX = this->m_inputOperation->getWidth() / (float)this->m_newWidth;
193         this->m_relY = this->m_inputOperation->getHeight() / (float)this->m_newHeight;
194
195
196         /* *** all the options below are for a fairly special case - camera framing *** */
197         if (this->m_offsetX != 0.0f || this->m_offsetY != 0.0f) {
198                 this->m_is_offset = true;
199
200                 if (this->m_newWidth > this->m_newHeight) {
201                         this->m_offsetX *= this->m_newWidth;
202                         this->m_offsetY *= this->m_newWidth;
203                 }
204                 else {
205                         this->m_offsetX *= this->m_newHeight;
206                         this->m_offsetY *= this->m_newHeight;
207                 }
208         }
209
210         if (this->m_is_aspect) {
211                 /* apply aspect from clip */
212                 const float w_src = this->m_inputOperation->getWidth();
213                 const float h_src = this->m_inputOperation->getHeight();
214
215                 /* destination aspect is already applied from the camera frame */
216                 const float w_dst = this->m_newWidth;
217                 const float h_dst = this->m_newHeight;
218
219                 const float asp_src = w_src / h_src;
220                 const float asp_dst = w_dst / h_dst;
221
222                 if (fabsf(asp_src - asp_dst) >= FLT_EPSILON) {
223                         if ((asp_src > asp_dst) == (this->m_is_crop == true)) {
224                                 /* fit X */
225                                 const float div = asp_src / asp_dst;
226                                 this->m_relX /= div;
227                                 this->m_offsetX += ((w_src - (w_src * div)) / (w_src / w_dst)) / 2.0f;
228                         }
229                         else {
230                                 /* fit Y */
231                                 const float div = asp_dst / asp_src;
232                                 this->m_relY /= div;
233                                 this->m_offsetY += ((h_src - (h_src * div)) / (h_src / h_dst)) / 2.0f;
234                         }
235
236                         this->m_is_offset = true;
237                 }
238         }
239         /* *** end framing options *** */
240 }
241
242 void ScaleFixedSizeOperation::deinitExecution()
243 {
244         this->m_inputOperation = NULL;
245 }
246
247
248 void ScaleFixedSizeOperation::executePixel(float *color, float x, float y, PixelSampler sampler)
249 {
250 #ifdef USE_FORCE_BICUBIC
251         sampler = COM_PS_BICUBIC;
252 #endif
253
254         if (this->m_is_offset) {
255                 float nx = ((x - this->m_offsetX) * this->m_relX);
256                 float ny = ((y - this->m_offsetY) * this->m_relY);
257                 this->m_inputOperation->read(color, nx, ny, sampler);
258         }
259         else {
260                 this->m_inputOperation->read(color, x * this->m_relX, y * this->m_relY, sampler);
261         }
262 }
263
264 bool ScaleFixedSizeOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
265 {
266         rcti newInput;
267
268         newInput.xmax = input->xmax * this->m_relX;
269         newInput.xmin = input->xmin * this->m_relX;
270         newInput.ymax = input->ymax * this->m_relY;
271         newInput.ymin = input->ymin * this->m_relY;
272
273         return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
274 }
275
276 void ScaleFixedSizeOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
277 {
278         unsigned int nr[2];
279         nr[0] = this->m_newWidth;
280         nr[1] = this->m_newHeight;
281         NodeOperation::determineResolution(resolution, nr);
282         resolution[0] = this->m_newWidth;
283         resolution[1] = this->m_newHeight;
284 }