Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_BlurBaseOperation.cc « operations « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe6ca1cfd4e1f1223452584486074ee5f44d0b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2011, Blender Foundation.
 */

#include "COM_BlurBaseOperation.h"
#include "BLI_math.h"
#include "MEM_guardedalloc.h"

#include "RE_pipeline.h"

BlurBaseOperation::BlurBaseOperation(DataType data_type)
{
  /* data_type is almost always DataType::Color except for alpha-blur */
  this->addInputSocket(data_type);
  this->addInputSocket(DataType::Value);
  this->addOutputSocket(data_type);
  this->setComplex(true);
  this->m_inputProgram = nullptr;
  memset(&m_data, 0, sizeof(NodeBlurData));
  this->m_size = 1.0f;
  this->m_sizeavailable = false;
  this->m_extend_bounds = false;
}
void BlurBaseOperation::initExecution()
{
  this->m_inputProgram = this->getInputSocketReader(0);
  this->m_inputSize = this->getInputSocketReader(1);
  this->m_data.image_in_width = this->getWidth();
  this->m_data.image_in_height = this->getHeight();
  if (this->m_data.relative) {
    int sizex, sizey;
    switch (this->m_data.aspect) {
      case CMP_NODE_BLUR_ASPECT_Y:
        sizex = sizey = this->m_data.image_in_width;
        break;
      case CMP_NODE_BLUR_ASPECT_X:
        sizex = sizey = this->m_data.image_in_height;
        break;
      default:
        BLI_assert(this->m_data.aspect == CMP_NODE_BLUR_ASPECT_NONE);
        sizex = this->m_data.image_in_width;
        sizey = this->m_data.image_in_height;
        break;
    }
    this->m_data.sizex = round_fl_to_int(this->m_data.percentx * 0.01f * sizex);
    this->m_data.sizey = round_fl_to_int(this->m_data.percenty * 0.01f * sizey);
  }

  QualityStepHelper::initExecution(COM_QH_MULTIPLY);
}

float *BlurBaseOperation::make_gausstab(float rad, int size)
{
  float *gausstab, sum, val;
  int i, n;

  n = 2 * size + 1;

  gausstab = (float *)MEM_mallocN(sizeof(float) * n, __func__);

  sum = 0.0f;
  float fac = (rad > 0.0f ? 1.0f / rad : 0.0f);
  for (i = -size; i <= size; i++) {
    val = RE_filter_value(this->m_data.filtertype, (float)i * fac);
    sum += val;
    gausstab[i + size] = val;
  }

  sum = 1.0f / sum;
  for (i = 0; i < n; i++) {
    gausstab[i] *= sum;
  }

  return gausstab;
}

#ifdef BLI_HAVE_SSE2
__m128 *BlurBaseOperation::convert_gausstab_sse(const float *gausstab, int size)
{
  int n = 2 * size + 1;
  __m128 *gausstab_sse = (__m128 *)MEM_mallocN_aligned(sizeof(__m128) * n, 16, "gausstab sse");
  for (int i = 0; i < n; i++) {
    gausstab_sse[i] = _mm_set1_ps(gausstab[i]);
  }
  return gausstab_sse;
}
#endif

/* normalized distance from the current (inverted so 1.0 is close and 0.0 is far)
 * 'ease' is applied after, looks nicer */
float *BlurBaseOperation::make_dist_fac_inverse(float rad, int size, int falloff)
{
  float *dist_fac_invert, val;
  int i, n;

  n = 2 * size + 1;

  dist_fac_invert = (float *)MEM_mallocN(sizeof(float) * n, __func__);

  float fac = (rad > 0.0f ? 1.0f / rad : 0.0f);
  for (i = -size; i <= size; i++) {
    val = 1.0f - fabsf((float)i * fac);

    /* keep in sync with rna_enum_proportional_falloff_curve_only_items */
    switch (falloff) {
      case PROP_SMOOTH:
        /* ease - gives less hard lines for dilate/erode feather */
        val = (3.0f * val * val - 2.0f * val * val * val);
        break;
      case PROP_SPHERE:
        val = sqrtf(2.0f * val - val * val);
        break;
      case PROP_ROOT:
        val = sqrtf(val);
        break;
      case PROP_SHARP:
        val = val * val;
        break;
      case PROP_INVSQUARE:
        val = val * (2.0f - val);
        break;
      case PROP_LIN:
        /* nothing to do */
        break;
#ifndef NDEBUG
      case -1:
        /* uninitialized! */
        BLI_assert(0);
        break;
#endif
      default:
        /* nothing */
        break;
    }
    dist_fac_invert[i + size] = val;
  }

  return dist_fac_invert;
}

void BlurBaseOperation::deinitExecution()
{
  this->m_inputProgram = nullptr;
  this->m_inputSize = nullptr;
}

void BlurBaseOperation::setData(const NodeBlurData *data)
{
  memcpy(&m_data, data, sizeof(NodeBlurData));
}

void BlurBaseOperation::updateSize()
{
  if (!this->m_sizeavailable) {
    float result[4];
    this->getInputSocketReader(1)->readSampled(result, 0, 0, COM_PS_NEAREST);
    this->m_size = result[0];
    this->m_sizeavailable = true;
  }
}

void BlurBaseOperation::determineResolution(unsigned int resolution[2],
                                            unsigned int preferredResolution[2])
{
  NodeOperation::determineResolution(resolution, preferredResolution);
  if (this->m_extend_bounds) {
    resolution[0] += 2 * this->m_size * m_data.sizex;
    resolution[1] += 2 * this->m_size * m_data.sizey;
  }
}