Blender  V2.93
COM_GaussianAlphaXBlurOperation.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.sizex, 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 GaussianAlphaXBlurOperation::updateGauss()
62 {
63  if (this->m_gausstab == nullptr) {
64  updateSize();
65  float rad = max_ff(m_size * m_data.sizex, 0.0f);
66  m_filtersize = min_ii(ceil(rad), MAX_GAUSSTAB_RADIUS);
67 
68  m_gausstab = BlurBaseOperation::make_gausstab(rad, m_filtersize);
69  }
70 
71  if (this->m_distbuf_inv == nullptr) {
72  updateSize();
73  float rad = max_ff(m_size * m_data.sizex, 0.0f);
74  rad = min_ff(rad, MAX_GAUSSTAB_RADIUS);
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 GaussianAlphaXBlurOperation::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  float *buffer = inputBuffer->getBuffer();
91  int bufferwidth = inputBuffer->getWidth();
92  const rcti &input_rect = inputBuffer->get_rect();
93  int bufferstartx = input_rect.xmin;
94  int bufferstarty = input_rect.ymin;
95 
96  const rcti &rect = inputBuffer->get_rect();
97  int xmin = max_ii(x - m_filtersize, rect.xmin);
98  int xmax = min_ii(x + m_filtersize + 1, rect.xmax);
99  int ymin = max_ii(y, rect.ymin);
100 
101  /* *** this is the main part which is different to 'GaussianXBlurOperation' *** */
102  int step = getStep();
103  int bufferindex = ((xmin - bufferstartx)) + ((ymin - bufferstarty) * bufferwidth);
104 
105  /* gauss */
106  float alpha_accum = 0.0f;
107  float multiplier_accum = 0.0f;
108 
109  /* dilate */
110  float value_max = finv_test(
111  buffer[(x) + (y * bufferwidth)],
112  do_invert); /* init with the current color to avoid unneeded lookups */
113  float distfacinv_max = 1.0f; /* 0 to 1 */
114 
115  for (int nx = xmin; nx < xmax; nx += step) {
116  const int index = (nx - x) + 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  bufferindex += step;
137  }
138 
139  /* blend between the max value and gauss blue - gives nice feather */
140  const float value_blur = alpha_accum / multiplier_accum;
141  const float value_final = (value_max * distfacinv_max) + (value_blur * (1.0f - distfacinv_max));
142  output[0] = finv_test(value_final, do_invert);
143 }
144 
146 {
148 
149  if (this->m_gausstab) {
150  MEM_freeN(this->m_gausstab);
151  this->m_gausstab = nullptr;
152  }
153 
154  if (this->m_distbuf_inv) {
155  MEM_freeN(this->m_distbuf_inv);
156  this->m_distbuf_inv = nullptr;
157  }
158 
159  deinitMutex();
160 }
161 
163  rcti *input, ReadBufferOperation *readOperation, rcti *output)
164 {
165  rcti newInput;
166 #if 0 /* until we add size input */
167  rcti sizeInput;
168  sizeInput.xmin = 0;
169  sizeInput.ymin = 0;
170  sizeInput.xmax = 5;
171  sizeInput.ymax = 5;
172 
173  NodeOperation *operation = this->getInputOperation(1);
174  if (operation->determineDependingAreaOfInterest(&sizeInput, readOperation, output)) {
175  return true;
176  }
177  else
178 #endif
179  {
180  if (this->m_sizeavailable && this->m_gausstab != nullptr) {
181  newInput.xmax = input->xmax + this->m_filtersize + 1;
182  newInput.xmin = input->xmin - this->m_filtersize - 1;
183  newInput.ymax = input->ymax;
184  newInput.ymin = input->ymin;
185  }
186  else {
187  newInput.xmax = this->getWidth();
188  newInput.xmin = 0;
189  newInput.ymax = this->getHeight();
190  newInput.ymin = 0;
191  }
192  return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
193  }
194 }
195 
196 } // 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 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 deinitExecution() override
Deinitialize the execution.
bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output) override
void executePixel(float output[4], int x, int y, void *data) override
The inner loop of this operation.
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)