Blender  V2.93
COM_ChromaMatteOperation.cc
Go to the documentation of this file.
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 
20 #include "BLI_math.h"
21 
22 namespace blender::compositor {
23 
25 {
29 
30  this->m_inputImageProgram = nullptr;
31  this->m_inputKeyProgram = nullptr;
32 }
33 
35 {
36  this->m_inputImageProgram = this->getInputSocketReader(0);
37  this->m_inputKeyProgram = this->getInputSocketReader(1);
38 }
39 
41 {
42  this->m_inputImageProgram = nullptr;
43  this->m_inputKeyProgram = nullptr;
44 }
45 
47  float x,
48  float y,
49  PixelSampler sampler)
50 {
51  float inKey[4];
52  float inImage[4];
53 
54  const float acceptance = this->m_settings->t1; /* in radians */
55  const float cutoff = this->m_settings->t2; /* in radians */
56  const float gain = this->m_settings->fstrength;
57 
58  float x_angle, z_angle, alpha;
59  float theta, beta;
60  float kfg;
61 
62  this->m_inputKeyProgram->readSampled(inKey, x, y, sampler);
63  this->m_inputImageProgram->readSampled(inImage, x, y, sampler);
64 
65  /* Store matte(alpha) value in [0] to go with
66  * #COM_SetAlphaMultiplyOperation and the Value output. */
67 
68  /* Algorithm from book "Video Demystified", does not include the spill reduction part. */
69  /* Find theta, the angle that the color space should be rotated based on key. */
70 
71  /* rescale to -1.0..1.0 */
72  // inImage[0] = (inImage[0] * 2.0f) - 1.0f; // UNUSED
73  inImage[1] = (inImage[1] * 2.0f) - 1.0f;
74  inImage[2] = (inImage[2] * 2.0f) - 1.0f;
75 
76  // inKey[0] = (inKey[0] * 2.0f) - 1.0f; // UNUSED
77  inKey[1] = (inKey[1] * 2.0f) - 1.0f;
78  inKey[2] = (inKey[2] * 2.0f) - 1.0f;
79 
80  theta = atan2(inKey[2], inKey[1]);
81 
82  /*rotate the cb and cr into x/z space */
83  x_angle = inImage[1] * cosf(theta) + inImage[2] * sinf(theta);
84  z_angle = inImage[2] * cosf(theta) - inImage[1] * sinf(theta);
85 
86  /*if within the acceptance angle */
87  /* if kfg is <0 then the pixel is outside of the key color */
88  kfg = x_angle - (fabsf(z_angle) / tanf(acceptance / 2.0f));
89 
90  if (kfg > 0.0f) { /* found a pixel that is within key color */
91  alpha = 1.0f - (kfg / gain);
92 
93  beta = atan2(z_angle, x_angle);
94 
95  /* if beta is within the cutoff angle */
96  if (fabsf(beta) < (cutoff / 2.0f)) {
97  alpha = 0.0f;
98  }
99 
100  /* don't make something that was more transparent less transparent */
101  if (alpha < inImage[3]) {
102  output[0] = alpha;
103  }
104  else {
105  output[0] = inImage[3];
106  }
107  }
108  else { /*pixel is outside key color */
109  output[0] = inImage[3]; /* make pixel just as transparent as it was before */
110  }
111 }
112 
113 } // namespace blender::compositor
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
#define output
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
void readSampled(float result[4], float x, float y, PixelSampler sampler)
void addInputSocket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
void addOutputSocket(DataType datatype)
SocketReader * getInputSocketReader(unsigned int inputSocketindex)
static CCL_NAMESPACE_BEGIN const double alpha
#define sinf(x)
#define cosf(x)
#define tanf(x)
#define fabsf(x)
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
Definition: rall1d.h:429
ccl_device_inline float beta(float x, float y)
Definition: util_math.h:666