4f9cd77198872cbf95925d538997bf9af0355496
[blender-staging.git] / source / blender / compositor / operations / COM_CropOperation.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_CropOperation.h"
24 #include "BLI_math.h"
25
26 CropBaseOperation::CropBaseOperation() : NodeOperation()
27 {
28         this->addInputSocket(COM_DT_COLOR, COM_SC_NO_RESIZE);
29         this->addOutputSocket(COM_DT_COLOR);
30         this->m_inputOperation = NULL;
31         this->m_settings = NULL;
32 }
33
34 void CropBaseOperation::updateArea()
35 {
36         SocketReader *inputReference = this->getInputSocketReader(0);
37         float width = inputReference->getWidth();
38         float height = inputReference->getHeight();
39         
40         if (width > 0.0f && height > 0.0f) {
41                 if (this->m_relative) {
42                         this->m_settings->x1 = width * this->m_settings->fac_x1;
43                         this->m_settings->x2 = width * this->m_settings->fac_x2;
44                         this->m_settings->y1 = height * this->m_settings->fac_y1;
45                         this->m_settings->y2 = height * this->m_settings->fac_y2;
46                 }
47                 if (width <= this->m_settings->x1 + 1)
48                         this->m_settings->x1 = width - 1;
49                 if (height <= this->m_settings->y1 + 1)
50                         this->m_settings->y1 = height - 1;
51                 if (width <= this->m_settings->x2 + 1)
52                         this->m_settings->x2 = width - 1;
53                 if (height <= this->m_settings->y2 + 1)
54                         this->m_settings->y2 = height - 1;
55                 
56                 this->m_xmax = max(this->m_settings->x1, this->m_settings->x2) + 1;
57                 this->m_xmin = min(this->m_settings->x1, this->m_settings->x2);
58                 this->m_ymax = max(this->m_settings->y1, this->m_settings->y2) + 1;
59                 this->m_ymin = min(this->m_settings->y1, this->m_settings->y2);
60         }
61         else {
62                 this->m_xmax = 0;
63                 this->m_xmin = 0;
64                 this->m_ymax = 0;
65                 this->m_ymin = 0;
66         }
67 }
68
69 void CropBaseOperation::initExecution()
70 {
71         this->m_inputOperation = this->getInputSocketReader(0);
72         updateArea();
73 }
74
75 void CropBaseOperation::deinitExecution()
76 {
77         this->m_inputOperation = NULL;
78 }
79
80 CropOperation::CropOperation() : CropBaseOperation()
81 {
82         /* pass */
83 }
84
85 void CropOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
86 {
87         if ((x < this->m_xmax && x >= this->m_xmin) && (y < this->m_ymax && y >= this->m_ymin)) {
88                 this->m_inputOperation->read(output, x, y, sampler);
89         }
90         else {
91                 zero_v4(output);
92         }
93 }
94
95 CropImageOperation::CropImageOperation() : CropBaseOperation()
96 {
97         /* pass */
98 }
99
100 bool CropImageOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
101 {
102         rcti newInput;
103         
104         newInput.xmax = input->xmax + this->m_xmin;
105         newInput.xmin = input->xmin + this->m_xmin;
106         newInput.ymax = input->ymax + this->m_ymin;
107         newInput.ymin = input->ymin + this->m_ymin;
108         
109         return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
110 }
111
112 void CropImageOperation::determineResolution(unsigned int resolution[2], unsigned int preferedResolution[2])
113 {
114         NodeOperation::determineResolution(resolution, preferedResolution);
115         updateArea();
116         resolution[0] = this->m_xmax - this->m_xmin;
117         resolution[1] = this->m_ymax - this->m_ymin;
118 }
119
120 void CropImageOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
121 {
122         if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight())  {
123                 this->m_inputOperation->read(output, (x + this->m_xmin), (y + this->m_ymin), sampler);
124         }
125         else {
126                 zero_v4(output);
127         }
128 }