Fix for 34703 Mix node (Hue, Saturation) update and rendering error
[blender.git] / source / blender / compositor / operations / COM_ZCombineOperation.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_ZCombineOperation.h"
24 #include "BLI_utildefines.h"
25
26 ZCombineOperation::ZCombineOperation() : NodeOperation()
27 {
28         this->addInputSocket(COM_DT_COLOR);
29         this->addInputSocket(COM_DT_VALUE);
30         this->addInputSocket(COM_DT_COLOR);
31         this->addInputSocket(COM_DT_VALUE);
32         this->addOutputSocket(COM_DT_COLOR);
33
34         this->m_image1Reader = NULL;
35         this->m_depth1Reader = NULL;
36         this->m_image2Reader = NULL;
37         this->m_depth2Reader = NULL;
38
39 }
40
41 void ZCombineOperation::initExecution()
42 {
43         this->m_image1Reader = this->getInputSocketReader(0);
44         this->m_depth1Reader = this->getInputSocketReader(1);
45         this->m_image2Reader = this->getInputSocketReader(2);
46         this->m_depth2Reader = this->getInputSocketReader(3);
47 }
48
49 void ZCombineOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
50 {
51         float depth1[4];
52         float depth2[4];
53
54         this->m_depth1Reader->read(depth1, x, y, sampler);
55         this->m_depth2Reader->read(depth2, x, y, sampler);
56         if (depth1[0] < depth2[0]) {
57                 this->m_image1Reader->read(output, x, y, sampler);
58         }
59         else {
60                 this->m_image2Reader->read(output, x, y, sampler);
61         }
62 }
63 void ZCombineAlphaOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
64 {
65         float depth1[4];
66         float depth2[4];
67         float color1[4];
68         float color2[4];
69
70         this->m_depth1Reader->read(depth1, x, y, sampler);
71         this->m_depth2Reader->read(depth2, x, y, sampler);
72         if (depth1[0] <= depth2[0]) {
73                 this->m_image1Reader->read(color1, x, y, sampler);
74                 this->m_image2Reader->read(color2, x, y, sampler);
75         }
76         else {
77                 this->m_image1Reader->read(color2, x, y, sampler);
78                 this->m_image2Reader->read(color1, x, y, sampler);
79         }
80         float fac = color1[3];
81         float ifac = 1.0f - fac;
82         output[0] = fac * color1[0] + ifac * color2[0];
83         output[1] = fac * color1[1] + ifac * color2[1];
84         output[2] = fac * color1[2] + ifac * color2[2];
85         output[3] = max(color1[3], color2[3]);
86 }
87
88 void ZCombineOperation::deinitExecution()
89 {
90         this->m_image1Reader = NULL;
91         this->m_depth1Reader = NULL;
92         this->m_image2Reader = NULL;
93         this->m_depth2Reader = NULL;
94 }
95
96 // MASK combine
97 ZCombineMaskOperation::ZCombineMaskOperation() : NodeOperation()
98 {
99         this->addInputSocket(COM_DT_VALUE); //mask
100         this->addInputSocket(COM_DT_COLOR);
101         this->addInputSocket(COM_DT_COLOR);
102         this->addOutputSocket(COM_DT_COLOR);
103
104         this->m_maskReader = NULL;
105         this->m_image1Reader = NULL;
106         this->m_image2Reader = NULL;
107 }
108
109 void ZCombineMaskOperation::initExecution()
110 {
111         this->m_maskReader = this->getInputSocketReader(0);
112         this->m_image1Reader = this->getInputSocketReader(1);
113         this->m_image2Reader = this->getInputSocketReader(2);
114 }
115
116 void ZCombineMaskOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
117 {
118         float mask[4];
119         float color1[4];
120         float color2[4];
121
122         this->m_maskReader->read(mask, x, y, sampler);
123         this->m_image1Reader->read(color1, x, y, sampler);
124         this->m_image2Reader->read(color2, x, y, sampler);
125
126         float fac = mask[0];
127         // multiply mask with alpha, if mask == 0 color1, else color2 make sure
128         float mfac = 1.0f-fac;
129         output[0] = color1[0]*mfac + color2[0]*fac;
130         output[1] = color1[1]*mfac + color2[1]*fac;
131         output[2] = color1[2]*mfac + color2[2]*fac;
132         output[3] = max(color1[3], color2[3]);
133 }
134
135 void ZCombineMaskAlphaOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
136 {
137         float mask[4];
138         float color1[4];
139         float color2[4];
140
141         this->m_maskReader->read(mask, x, y, sampler);
142         this->m_image1Reader->read(color1, x, y, sampler);
143         this->m_image2Reader->read(color2, x, y, sampler);
144
145         float fac = mask[0];
146         // multiply mask with alpha, if mask == 0 color1, else color2 make sure
147         float mfac = 1.0f-fac;
148         float alpha = color1[3]*mfac + color2[3]*fac;
149         float facalpha = fac * alpha;
150         mfac = 1.0f-facalpha;
151         output[0] = color1[0]*mfac + color2[0]*facalpha;
152         output[1] = color1[1]*mfac + color2[1]*facalpha;
153         output[2] = color1[2]*mfac + color2[2]*facalpha;
154         output[3] = max(color1[3], color2[3]);
155 }
156
157 void ZCombineMaskOperation::deinitExecution()
158 {
159         this->m_image1Reader = NULL;
160         this->m_maskReader = NULL;
161         this->m_image2Reader = NULL;
162 }