Blender  V2.93
COM_KeyingClipOperation.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 
20 
21 #include "MEM_guardedalloc.h"
22 
23 #include "BLI_listbase.h"
24 #include "BLI_math.h"
25 
26 namespace blender::compositor {
27 
29 {
32 
33  this->m_kernelRadius = 3;
34  this->m_kernelTolerance = 0.1f;
35 
36  this->m_clipBlack = 0.0f;
37  this->m_clipWhite = 1.0f;
38 
39  this->m_isEdgeMatte = false;
40 
41  this->flags.complex = true;
42 }
43 
45 {
47 
48  return buffer;
49 }
50 
51 void KeyingClipOperation::executePixel(float output[4], int x, int y, void *data)
52 {
53  const int delta = this->m_kernelRadius;
54  const float tolerance = this->m_kernelTolerance;
55 
56  MemoryBuffer *inputBuffer = (MemoryBuffer *)data;
57  float *buffer = inputBuffer->getBuffer();
58 
59  int bufferWidth = inputBuffer->getWidth();
60  int bufferHeight = inputBuffer->getHeight();
61 
62  float value = buffer[(y * bufferWidth + x)];
63 
64  bool ok = false;
65  int start_x = max_ff(0, x - delta + 1), start_y = max_ff(0, y - delta + 1),
66  end_x = min_ff(x + delta - 1, bufferWidth - 1),
67  end_y = min_ff(y + delta - 1, bufferHeight - 1);
68 
69  int count = 0, totalCount = (end_x - start_x + 1) * (end_y - start_y + 1) - 1;
70  int thresholdCount = ceil((float)totalCount * 0.9f);
71 
72  if (delta == 0) {
73  ok = true;
74  }
75 
76  for (int cx = start_x; ok == false && cx <= end_x; cx++) {
77  for (int cy = start_y; ok == false && cy <= end_y; cy++) {
78  if (UNLIKELY(cx == x && cy == y)) {
79  continue;
80  }
81 
82  int bufferIndex = (cy * bufferWidth + cx);
83  float currentValue = buffer[bufferIndex];
84 
85  if (fabsf(currentValue - value) < tolerance) {
86  count++;
87  if (count >= thresholdCount) {
88  ok = true;
89  }
90  }
91  }
92  }
93 
94  if (this->m_isEdgeMatte) {
95  if (ok) {
96  output[0] = 0.0f;
97  }
98  else {
99  output[0] = 1.0f;
100  }
101  }
102  else {
103  output[0] = value;
104 
105  if (ok) {
106  if (output[0] < this->m_clipBlack) {
107  output[0] = 0.0f;
108  }
109  else if (output[0] >= this->m_clipWhite) {
110  output[0] = 1.0f;
111  }
112  else {
113  output[0] = (output[0] - this->m_clipBlack) / (this->m_clipWhite - this->m_clipBlack);
114  }
115  }
116  }
117 }
118 
120  ReadBufferOperation *readOperation,
121  rcti *output)
122 {
123  rcti newInput;
124 
125  newInput.xmin = input->xmin - this->m_kernelRadius;
126  newInput.ymin = input->ymin - this->m_kernelRadius;
127  newInput.xmax = input->xmax + this->m_kernelRadius;
128  newInput.ymax = input->ymax + this->m_kernelRadius;
129 
130  return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
131 }
132 
133 } // namespace blender::compositor
MINLINE float max_ff(float a, float b)
MINLINE float min_ff(float a, float b)
#define UNLIKELY(x)
_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 executePixel(float output[4], int x, int y, void *data) override
calculate a single pixel
bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output) override
a MemoryBuffer contains access to the data of a chunk
const int getHeight() const
get the height of this MemoryBuffer
const int getWidth() const
get the width of this MemoryBuffer
float * getBuffer()
get the data of this MemoryBuffer
virtual void * initializeTileData(rcti *)
void addInputSocket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
NodeOperation * getInputOperation(unsigned int inputSocketindex)
void addOutputSocket(DataType datatype)
virtual bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
int count
#define fabsf(x)
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
int ymin
Definition: DNA_vec_types.h:80
int ymax
Definition: DNA_vec_types.h:80
int xmin
Definition: DNA_vec_types.h:79
int xmax
Definition: DNA_vec_types.h:79
ccl_device_inline float3 ceil(const float3 &a)