1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2011, Blender Foundation.
17  */
18 
19 #include "COM_RotateOperation.h"
20 #include "BLI_math.h"
21 
RotateOperation()22 RotateOperation::RotateOperation() : NodeOperation()
23 {
24   this->addInputSocket(COM_DT_COLOR);
25   this->addInputSocket(COM_DT_VALUE);
26   this->addOutputSocket(COM_DT_COLOR);
27   this->setResolutionInputSocketIndex(0);
28   this->m_imageSocket = NULL;
29   this->m_degreeSocket = NULL;
30   this->m_doDegree2RadConversion = false;
31   this->m_isDegreeSet = false;
32 }
initExecution()33 void RotateOperation::initExecution()
34 {
35   this->m_imageSocket = this->getInputSocketReader(0);
36   this->m_degreeSocket = this->getInputSocketReader(1);
37   this->m_centerX = (getWidth() - 1) / 2.0;
38   this->m_centerY = (getHeight() - 1) / 2.0;
39 }
40 
deinitExecution()41 void RotateOperation::deinitExecution()
42 {
43   this->m_imageSocket = NULL;
44   this->m_degreeSocket = NULL;
45 }
46 
ensureDegree()47 inline void RotateOperation::ensureDegree()
48 {
49   if (!this->m_isDegreeSet) {
50     float degree[4];
51     this->m_degreeSocket->readSampled(degree, 0, 0, COM_PS_NEAREST);
52     double rad;
53     if (this->m_doDegree2RadConversion) {
54       rad = DEG2RAD((double)degree[0]);
55     }
56     else {
57       rad = degree[0];
58     }
59     this->m_cosine = cos(rad);
60     this->m_sine = sin(rad);
61 
62     this->m_isDegreeSet = true;
63   }
64 }
65 
executePixelSampled(float output[4],float x,float y,PixelSampler sampler)66 void RotateOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
67 {
68   ensureDegree();
69   const float dy = y - this->m_centerY;
70   const float dx = x - this->m_centerX;
71   const float nx = this->m_centerX + (this->m_cosine * dx + this->m_sine * dy);
72   const float ny = this->m_centerY + (-this->m_sine * dx + this->m_cosine * dy);
73   this->m_imageSocket->readSampled(output, nx, ny, sampler);
74 }
75 
determineDependingAreaOfInterest(rcti * input,ReadBufferOperation * readOperation,rcti * output)76 bool RotateOperation::determineDependingAreaOfInterest(rcti *input,
77                                                        ReadBufferOperation *readOperation,
78                                                        rcti *output)
79 {
80   ensureDegree();
81   rcti newInput;
82 
83   const float dxmin = input->xmin - this->m_centerX;
84   const float dymin = input->ymin - this->m_centerY;
85   const float dxmax = input->xmax - this->m_centerX;
86   const float dymax = input->ymax - this->m_centerY;
87 
88   const float x1 = this->m_centerX + (this->m_cosine * dxmin + this->m_sine * dymin);
89   const float x2 = this->m_centerX + (this->m_cosine * dxmax + this->m_sine * dymin);
90   const float x3 = this->m_centerX + (this->m_cosine * dxmin + this->m_sine * dymax);
91   const float x4 = this->m_centerX + (this->m_cosine * dxmax + this->m_sine * dymax);
92   const float y1 = this->m_centerY + (-this->m_sine * dxmin + this->m_cosine * dymin);
93   const float y2 = this->m_centerY + (-this->m_sine * dxmax + this->m_cosine * dymin);
94   const float y3 = this->m_centerY + (-this->m_sine * dxmin + this->m_cosine * dymax);
95   const float y4 = this->m_centerY + (-this->m_sine * dxmax + this->m_cosine * dymax);
96   const float minx = min(x1, min(x2, min(x3, x4)));
97   const float maxx = max(x1, max(x2, max(x3, x4)));
98   const float miny = min(y1, min(y2, min(y3, y4)));
99   const float maxy = max(y1, max(y2, max(y3, y4)));
100 
101   newInput.xmax = ceil(maxx) + 1;
102   newInput.xmin = floor(minx) - 1;
103   newInput.ymax = ceil(maxy) + 1;
104   newInput.ymin = floor(miny) - 1;
105 
106   return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
107 }
108