code cleanup: spelling
[blender.git] / source / blender / compositor / operations / COM_DespeckleOperation.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: Campbell Barton
19  */
20
21 #include "MEM_guardedalloc.h"
22
23 #include "COM_DespeckleOperation.h"
24
25 #include "BLI_utildefines.h"
26
27 DespeckleOperation::DespeckleOperation() : NodeOperation()
28 {
29         this->addInputSocket(COM_DT_COLOR);
30         this->addInputSocket(COM_DT_VALUE);
31         this->addOutputSocket(COM_DT_COLOR);
32         this->setResolutionInputSocketIndex(0);
33         this->m_inputOperation = NULL;
34         this->setComplex(true);
35 }
36 void DespeckleOperation::initExecution()
37 {
38         this->m_inputOperation = this->getInputSocketReader(0);
39         this->m_inputValueOperation = this->getInputSocketReader(1);
40 }
41
42 void DespeckleOperation::deinitExecution()
43 {
44         this->m_inputOperation = NULL;
45         this->m_inputValueOperation = NULL;
46 }
47
48 BLI_INLINE int color_diff(const float a[3], const float b[3], const float threshold)
49 {
50         return ((fabsf(a[0] - b[0]) > threshold) ||
51                 (fabsf(a[1] - b[1]) > threshold) ||
52                 (fabsf(a[2] - b[2]) > threshold));
53 }
54
55 void DespeckleOperation::executePixel(float output[4], int x, int y, void *data)
56 {
57         float w = 0.0f;
58         float color_org[4];
59         float color_mid[4];
60         float color_mid_ok[4];
61         float in1[4];
62         int x1 = x - 1;
63         int x2 = x;
64         int x3 = x + 1;
65         int y1 = y - 1;
66         int y2 = y;
67         int y3 = y + 1;
68         CLAMP(x1, 0, getWidth() - 1);
69         CLAMP(x2, 0, getWidth() - 1);
70         CLAMP(x3, 0, getWidth() - 1);
71         CLAMP(y1, 0, getHeight() - 1);
72         CLAMP(y2, 0, getHeight() - 1);
73         CLAMP(y3, 0, getHeight() - 1);
74         float value[4];
75         this->m_inputValueOperation->read(value, x2, y2, NULL);
76         //const float mval = 1.0f - value[0];
77
78
79         this->m_inputOperation->read(color_org, x2, y2, NULL);
80
81 #define TOT_DIV_ONE 1.0f
82 #define TOT_DIV_CNR (float)M_SQRT1_2
83
84 #define WTOT (TOT_DIV_ONE * 4 + TOT_DIV_CNR * 4)
85
86 #define COLOR_ADD(fac)  \
87 { \
88         madd_v4_v4fl(color_mid, in1, fac); \
89         if (color_diff(in1, color_org, this->m_threshold)) { \
90                 w += fac; \
91                 madd_v4_v4fl(color_mid_ok, in1, fac); \
92         } \
93 }
94
95         zero_v4(color_mid);
96         zero_v4(color_mid_ok);
97
98         this->m_inputOperation->read(in1, x1, y1, NULL); COLOR_ADD(TOT_DIV_CNR)
99         this->m_inputOperation->read(in1, x2, y1, NULL); COLOR_ADD(TOT_DIV_ONE)
100         this->m_inputOperation->read(in1, x3, y1, NULL); COLOR_ADD(TOT_DIV_CNR)
101         this->m_inputOperation->read(in1, x1, y2, NULL); COLOR_ADD(TOT_DIV_ONE)
102
103 #if 0
104         this->m_inputOperation->read(in2, x2, y2, NULL);
105         madd_v4_v4fl(color_mid, in2, this->m_filter[4]);
106 #endif
107
108         this->m_inputOperation->read(in1, x3, y2, NULL); COLOR_ADD(TOT_DIV_ONE)
109         this->m_inputOperation->read(in1, x1, y3, NULL); COLOR_ADD(TOT_DIV_CNR)
110         this->m_inputOperation->read(in1, x2, y3, NULL); COLOR_ADD(TOT_DIV_ONE)
111         this->m_inputOperation->read(in1, x3, y3, NULL); COLOR_ADD(TOT_DIV_CNR)
112
113         mul_v4_fl(color_mid, 1.0f / (4.0f + (4.0f * (float)M_SQRT1_2)));
114         //mul_v4_fl(color_mid, 1.0f / w);
115
116         if ((w != 0.0f) &&
117             ((w / WTOT) > (this->m_threshold_neighbor)) &&
118             color_diff(color_mid, color_org, this->m_threshold))
119         {
120                 mul_v4_fl(color_mid_ok, 1.0f / w);
121                 interp_v4_v4v4(output, color_org, color_mid_ok, value[0]);
122         }
123         else {
124                 copy_v4_v4(output, color_org);
125         }
126 }
127
128 bool DespeckleOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
129 {
130         rcti newInput;
131         int addx = 2; //(this->m_filterWidth - 1) / 2 + 1;
132         int addy = 2; //(this->m_filterHeight - 1) / 2 + 1;
133         newInput.xmax = input->xmax + addx;
134         newInput.xmin = input->xmin - addx;
135         newInput.ymax = input->ymax + addy;
136         newInput.ymin = input->ymin - addy;
137
138         return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
139 }