Blender  V2.93
COM_KeyingOperation.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 2012, Blender Foundation.
17  */
18 
19 #include "COM_KeyingOperation.h"
20 
21 #include "MEM_guardedalloc.h"
22 
23 #include "BLI_listbase.h"
24 #include "BLI_math.h"
25 
26 namespace blender::compositor {
27 
28 static float get_pixel_saturation(const float pixelColor[4],
29  float screen_balance,
30  int primary_channel)
31 {
32  const int other_1 = (primary_channel + 1) % 3;
33  const int other_2 = (primary_channel + 2) % 3;
34 
35  const int min_channel = MIN2(other_1, other_2);
36  const int max_channel = MAX2(other_1, other_2);
37 
38  const float val = screen_balance * pixelColor[min_channel] +
39  (1.0f - screen_balance) * pixelColor[max_channel];
40 
41  return (pixelColor[primary_channel] - val) * fabsf(1.0f - val);
42 }
43 
45 {
49 
50  this->m_screenBalance = 0.5f;
51 
52  this->m_pixelReader = nullptr;
53  this->m_screenReader = nullptr;
54 }
55 
57 {
58  this->m_pixelReader = this->getInputSocketReader(0);
59  this->m_screenReader = this->getInputSocketReader(1);
60 }
61 
63 {
64  this->m_pixelReader = nullptr;
65  this->m_screenReader = nullptr;
66 }
67 
68 void KeyingOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
69 {
70  float pixel_color[4];
71  float screen_color[4];
72 
73  this->m_pixelReader->readSampled(pixel_color, x, y, sampler);
74  this->m_screenReader->readSampled(screen_color, x, y, sampler);
75 
76  const int primary_channel = max_axis_v3(screen_color);
77  const float min_pixel_color = min_fff(pixel_color[0], pixel_color[1], pixel_color[2]);
78 
79  if (min_pixel_color > 1.0f) {
80  /* overexposure doesn't happen on screen itself and usually happens
81  * on light sources in the shot, this need to be checked separately
82  * because saturation and falloff calculation is based on the fact
83  * that pixels are not overexposed
84  */
85  output[0] = 1.0f;
86  }
87  else {
88  float saturation = get_pixel_saturation(pixel_color, this->m_screenBalance, primary_channel);
89  float screen_saturation = get_pixel_saturation(
90  screen_color, this->m_screenBalance, primary_channel);
91 
92  if (saturation < 0) {
93  /* means main channel of pixel is different from screen,
94  * assume this is completely a foreground
95  */
96  output[0] = 1.0f;
97  }
98  else if (saturation >= screen_saturation) {
99  /* matched main channels and higher saturation on pixel
100  * is treated as completely background
101  */
102  output[0] = 0.0f;
103  }
104  else {
105  /* nice alpha falloff on edges */
106  float distance = 1.0f - saturation / screen_saturation;
107 
108  output[0] = distance;
109  }
110  }
111 }
112 
113 } // namespace blender::compositor
MINLINE float min_fff(float a, float b, float c)
MINLINE int max_axis_v3(const float vec[3])
#define MAX2(a, b)
#define MIN2(a, b)
_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
Read Guarded memory(de)allocation.
#define output
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
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)
#define fabsf(x)
static float get_pixel_saturation(const float pixelColor[4], float screen_balance, int primary_channel)
ccl_device_inline float distance(const float2 &a, const float2 &b)