Blender  V2.93
COM_BlurBaseOperation.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 
19 #include "COM_BlurBaseOperation.h"
20 #include "BLI_math.h"
21 #include "MEM_guardedalloc.h"
22 
23 #include "RE_pipeline.h"
24 
25 namespace blender::compositor {
26 
28 {
29  /* data_type is almost always DataType::Color except for alpha-blur */
30  this->addInputSocket(data_type);
32  this->addOutputSocket(data_type);
33  this->flags.complex = true;
34  this->m_inputProgram = nullptr;
35  memset(&m_data, 0, sizeof(NodeBlurData));
36  this->m_size = 1.0f;
37  this->m_sizeavailable = false;
38  this->m_extend_bounds = false;
39 }
41 {
42  this->m_inputProgram = this->getInputSocketReader(0);
43  this->m_inputSize = this->getInputSocketReader(1);
44  this->m_data.image_in_width = this->getWidth();
45  this->m_data.image_in_height = this->getHeight();
46  if (this->m_data.relative) {
47  int sizex, sizey;
48  switch (this->m_data.aspect) {
50  sizex = sizey = this->m_data.image_in_width;
51  break;
53  sizex = sizey = this->m_data.image_in_height;
54  break;
55  default:
57  sizex = this->m_data.image_in_width;
58  sizey = this->m_data.image_in_height;
59  break;
60  }
61  this->m_data.sizex = round_fl_to_int(this->m_data.percentx * 0.01f * sizex);
62  this->m_data.sizey = round_fl_to_int(this->m_data.percenty * 0.01f * sizey);
63  }
64 
66 }
67 
68 float *BlurBaseOperation::make_gausstab(float rad, int size)
69 {
70  float *gausstab, sum, val;
71  int i, n;
72 
73  n = 2 * size + 1;
74 
75  gausstab = (float *)MEM_mallocN(sizeof(float) * n, __func__);
76 
77  sum = 0.0f;
78  float fac = (rad > 0.0f ? 1.0f / rad : 0.0f);
79  for (i = -size; i <= size; i++) {
80  val = RE_filter_value(this->m_data.filtertype, (float)i * fac);
81  sum += val;
82  gausstab[i + size] = val;
83  }
84 
85  sum = 1.0f / sum;
86  for (i = 0; i < n; i++) {
87  gausstab[i] *= sum;
88  }
89 
90  return gausstab;
91 }
92 
93 #ifdef BLI_HAVE_SSE2
94 __m128 *BlurBaseOperation::convert_gausstab_sse(const float *gausstab, int size)
95 {
96  int n = 2 * size + 1;
97  __m128 *gausstab_sse = (__m128 *)MEM_mallocN_aligned(sizeof(__m128) * n, 16, "gausstab sse");
98  for (int i = 0; i < n; i++) {
99  gausstab_sse[i] = _mm_set1_ps(gausstab[i]);
100  }
101  return gausstab_sse;
102 }
103 #endif
104 
105 /* normalized distance from the current (inverted so 1.0 is close and 0.0 is far)
106  * 'ease' is applied after, looks nicer */
107 float *BlurBaseOperation::make_dist_fac_inverse(float rad, int size, int falloff)
108 {
109  float *dist_fac_invert, val;
110  int i, n;
111 
112  n = 2 * size + 1;
113 
114  dist_fac_invert = (float *)MEM_mallocN(sizeof(float) * n, __func__);
115 
116  float fac = (rad > 0.0f ? 1.0f / rad : 0.0f);
117  for (i = -size; i <= size; i++) {
118  val = 1.0f - fabsf((float)i * fac);
119 
120  /* keep in sync with rna_enum_proportional_falloff_curve_only_items */
121  switch (falloff) {
122  case PROP_SMOOTH:
123  /* ease - gives less hard lines for dilate/erode feather */
124  val = (3.0f * val * val - 2.0f * val * val * val);
125  break;
126  case PROP_SPHERE:
127  val = sqrtf(2.0f * val - val * val);
128  break;
129  case PROP_ROOT:
130  val = sqrtf(val);
131  break;
132  case PROP_SHARP:
133  val = val * val;
134  break;
135  case PROP_INVSQUARE:
136  val = val * (2.0f - val);
137  break;
138  case PROP_LIN:
139  /* nothing to do */
140  break;
141 #ifndef NDEBUG
142  case -1:
143  /* uninitialized! */
144  BLI_assert(0);
145  break;
146 #endif
147  default:
148  /* nothing */
149  break;
150  }
151  dist_fac_invert[i + size] = val;
152  }
153 
154  return dist_fac_invert;
155 }
156 
158 {
159  this->m_inputProgram = nullptr;
160  this->m_inputSize = nullptr;
161 }
162 
164 {
165  memcpy(&m_data, data, sizeof(NodeBlurData));
166 }
167 
169 {
170  if (!this->m_sizeavailable) {
171  float result[4];
172  this->getInputSocketReader(1)->readSampled(result, 0, 0, PixelSampler::Nearest);
173  this->m_size = result[0];
174  this->m_sizeavailable = true;
175  }
176 }
177 
178 void BlurBaseOperation::determineResolution(unsigned int resolution[2],
179  unsigned int preferredResolution[2])
180 {
181  NodeOperation::determineResolution(resolution, preferredResolution);
182  if (this->m_extend_bounds) {
183  resolution[0] += 2 * this->m_size * m_data.sizex;
184  resolution[1] += 2 * this->m_size * m_data.sizey;
185  }
186 }
187 
188 } // namespace blender::compositor
#define BLI_assert(a)
Definition: BLI_assert.h:58
MINLINE int round_fl_to_int(float a)
#define CMP_NODE_BLUR_ASPECT_X
#define CMP_NODE_BLUR_ASPECT_Y
#define CMP_NODE_BLUR_ASPECT_NONE
#define PROP_LIN
#define PROP_SPHERE
#define PROP_SMOOTH
#define PROP_ROOT
#define PROP_INVSQUARE
#define PROP_SHARP
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
static T sum(const btAlignedObjectArray< T > &items)
float * make_gausstab(float rad, int size)
void setData(const NodeBlurData *data)
void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2]) override
determine the resolution of this node
float * make_dist_fac_inverse(float rad, int size, int falloff)
void readSampled(float result[4], float x, float y, PixelSampler sampler)
void addInputSocket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
void addOutputSocket(DataType datatype)
virtual void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2])
determine the resolution of this node
SocketReader * getInputSocketReader(unsigned int inputSocketindex)
DataType
possible data types for sockets
Definition: COM_defines.h:27
float RE_filter_value(int type, float x)
Definition: initrender.c:128
#define fabsf(x)
#define sqrtf(x)
void *(* MEM_mallocN_aligned)(size_t len, size_t alignment, const char *str)
Definition: mallocn.c:49
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47