Blender  V2.93
COM_GaussianAlphaYBlurOperation.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 #include "MEM_guardedalloc.h"
22 
23 #include "RE_pipeline.h"
24 
25 namespace blender::compositor {
26 
28 {
29  this->m_gausstab = nullptr;
30  this->m_filtersize = 0;
31  this->m_falloff = -1; /* intentionally invalid, so we can detect uninitialized values */
32 }
33 
35 {
36  lockMutex();
37  if (!this->m_sizeavailable) {
38  updateGauss();
39  }
40  void *buffer = getInputOperation(0)->initializeTileData(nullptr);
41  unlockMutex();
42  return buffer;
43 }
44 
46 {
47  /* Until we support size input - comment this. */
48  // BlurBaseOperation::initExecution();
49 
50  initMutex();
51 
52  if (this->m_sizeavailable) {
53  float rad = max_ff(m_size * m_data.sizey, 0.0f);
54  m_filtersize = min_ii(ceil(rad), MAX_GAUSSTAB_RADIUS);
55 
56  m_gausstab = BlurBaseOperation::make_gausstab(rad, m_filtersize);
57  m_distbuf_inv = BlurBaseOperation::make_dist_fac_inverse(rad, m_filtersize, m_falloff);
58  }
59 }
60 
61 void GaussianAlphaYBlurOperation::updateGauss()
62 {
63  if (this->m_gausstab == nullptr) {
64  updateSize();
65  float rad = max_ff(m_size * m_data.sizey, 0.0f);
66  rad = min_ff(rad, MAX_GAUSSTAB_RADIUS);
67  m_filtersize = min_ii(ceil(rad), MAX_GAUSSTAB_RADIUS);
68 
69  m_gausstab = BlurBaseOperation::make_gausstab(rad, m_filtersize);
70  }
71 
72  if (this->m_distbuf_inv == nullptr) {
73  updateSize();
74  float rad = max_ff(m_size * m_data.sizey, 0.0f);
75  m_filtersize = min_ii(ceil(rad), MAX_GAUSSTAB_RADIUS);
76 
77  m_distbuf_inv = BlurBaseOperation::make_dist_fac_inverse(rad, m_filtersize, m_falloff);
78  }
79 }
80 
81 BLI_INLINE float finv_test(const float f, const bool test)
82 {
83  return (LIKELY(test == false)) ? f : 1.0f - f;
84 }
85 
86 void GaussianAlphaYBlurOperation::executePixel(float output[4], int x, int y, void *data)
87 {
88  const bool do_invert = this->m_do_subtract;
89  MemoryBuffer *inputBuffer = (MemoryBuffer *)data;
90  const rcti &input_rect = inputBuffer->get_rect();
91  float *buffer = inputBuffer->getBuffer();
92  int bufferwidth = inputBuffer->getWidth();
93  int bufferstartx = input_rect.xmin;
94  int bufferstarty = input_rect.ymin;
95 
96  int xmin = max_ii(x, input_rect.xmin);
97  int ymin = max_ii(y - m_filtersize, input_rect.ymin);
98  int ymax = min_ii(y + m_filtersize + 1, input_rect.ymax);
99 
100  /* *** this is the main part which is different to 'GaussianYBlurOperation' *** */
101  int step = getStep();
102 
103  /* gauss */
104  float alpha_accum = 0.0f;
105  float multiplier_accum = 0.0f;
106 
107  /* dilate */
108  float value_max = finv_test(
109  buffer[(x) + (y * bufferwidth)],
110  do_invert); /* init with the current color to avoid unneeded lookups */
111  float distfacinv_max = 1.0f; /* 0 to 1 */
112 
113  for (int ny = ymin; ny < ymax; ny += step) {
114  int bufferindex = ((xmin - bufferstartx)) + ((ny - bufferstarty) * bufferwidth);
115 
116  const int index = (ny - y) + this->m_filtersize;
117  float value = finv_test(buffer[bufferindex], do_invert);
118  float multiplier;
119 
120  /* gauss */
121  {
122  multiplier = this->m_gausstab[index];
123  alpha_accum += value * multiplier;
124  multiplier_accum += multiplier;
125  }
126 
127  /* dilate - find most extreme color */
128  if (value > value_max) {
129  multiplier = this->m_distbuf_inv[index];
130  value *= multiplier;
131  if (value > value_max) {
132  value_max = value;
133  distfacinv_max = multiplier;
134  }
135  }
136  }
137 
138  /* blend between the max value and gauss blue - gives nice feather */
139  const float value_blur = alpha_accum / multiplier_accum;
140  const float value_final = (value_max * distfacinv_max) + (value_blur * (1.0f - distfacinv_max));
141  output[0] = finv_test(value_final, do_invert);
142 }
143 
145 {
147 
148  if (this->m_gausstab) {
149  MEM_freeN(this->m_gausstab);
150  this->m_gausstab = nullptr;
151  }
152 
153  if (this->m_distbuf_inv) {
154  MEM_freeN(this->m_distbuf_inv);
155  this->m_distbuf_inv = nullptr;
156  }
157 
158  deinitMutex();
159 }
160 
162  rcti *input, ReadBufferOperation *readOperation, rcti *output)
163 {
164  rcti newInput;
165 #if 0 /* until we add size input */
166  rcti sizeInput;
167  sizeInput.xmin = 0;
168  sizeInput.ymin = 0;
169  sizeInput.xmax = 5;
170  sizeInput.ymax = 5;
171 
172  NodeOperation *operation = this->getInputOperation(1);
173  if (operation->determineDependingAreaOfInterest(&sizeInput, readOperation, output)) {
174  return true;
175  }
176  else
177 #endif
178  {
179  if (this->m_sizeavailable && this->m_gausstab != nullptr) {
180  newInput.xmax = input->xmax;
181  newInput.xmin = input->xmin;
182  newInput.ymax = input->ymax + this->m_filtersize + 1;
183  newInput.ymin = input->ymin - this->m_filtersize - 1;
184  }
185  else {
186  newInput.xmax = this->getWidth();
187  newInput.xmin = 0;
188  newInput.ymax = this->getHeight();
189  newInput.ymin = 0;
190  }
191  return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
192  }
193 }
194 
195 } // namespace blender::compositor
#define BLI_INLINE
MINLINE float max_ff(float a, float b)
MINLINE int min_ii(int a, int b)
MINLINE float min_ff(float a, float b)
MINLINE int max_ii(int a, int b)
#define LIKELY(x)
#define MAX_GAUSSTAB_RADIUS
_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 GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble ny
_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.
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White Value
#define output
float * make_gausstab(float rad, int size)
float * make_dist_fac_inverse(float rad, int size, int falloff)
void executePixel(float output[4], int x, int y, void *data) override
bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output) override
a MemoryBuffer contains access to the data of a chunk
const rcti & get_rect() const
get the rect of this MemoryBuffer
const int getWidth() const
get the width of this MemoryBuffer
float * getBuffer()
get the data of this MemoryBuffer
NodeOperation contains calculation logic.
virtual void * initializeTileData(rcti *)
NodeOperation * getInputOperation(unsigned int inputSocketindex)
virtual bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
DataType
possible data types for sockets
Definition: COM_defines.h:27
__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
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
BLI_INLINE float finv_test(const float f, const bool test)
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)