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

COM_KeyingClipOperation.cc « operations « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03e3f0611c0efd126a740948d1b99e9d6d9788a1 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * 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 2012, Blender Foundation.
 */

#include "COM_KeyingClipOperation.h"

namespace blender::compositor {

KeyingClipOperation::KeyingClipOperation()
{
  this->add_input_socket(DataType::Value);
  this->add_output_socket(DataType::Value);

  kernel_radius_ = 3;
  kernel_tolerance_ = 0.1f;

  clip_black_ = 0.0f;
  clip_white_ = 1.0f;

  is_edge_matte_ = false;

  this->flags.complex = true;
}

void *KeyingClipOperation::initialize_tile_data(rcti *rect)
{
  void *buffer = get_input_operation(0)->initialize_tile_data(rect);

  return buffer;
}

void KeyingClipOperation::execute_pixel(float output[4], int x, int y, void *data)
{
  const int delta = kernel_radius_;
  const float tolerance = kernel_tolerance_;

  MemoryBuffer *input_buffer = (MemoryBuffer *)data;
  float *buffer = input_buffer->get_buffer();

  int buffer_width = input_buffer->get_width();
  int buffer_height = input_buffer->get_height();

  float value = buffer[(y * buffer_width + x)];

  bool ok = false;
  int start_x = max_ff(0, x - delta + 1), start_y = max_ff(0, y - delta + 1),
      end_x = min_ff(x + delta - 1, buffer_width - 1),
      end_y = min_ff(y + delta - 1, buffer_height - 1);

  int count = 0, total_count = (end_x - start_x + 1) * (end_y - start_y + 1) - 1;
  int threshold_count = ceil((float)total_count * 0.9f);

  if (delta == 0) {
    ok = true;
  }

  for (int cx = start_x; ok == false && cx <= end_x; cx++) {
    for (int cy = start_y; ok == false && cy <= end_y; cy++) {
      if (UNLIKELY(cx == x && cy == y)) {
        continue;
      }

      int buffer_index = (cy * buffer_width + cx);
      float current_value = buffer[buffer_index];

      if (fabsf(current_value - value) < tolerance) {
        count++;
        if (count >= threshold_count) {
          ok = true;
        }
      }
    }
  }

  if (is_edge_matte_) {
    if (ok) {
      output[0] = 0.0f;
    }
    else {
      output[0] = 1.0f;
    }
  }
  else {
    output[0] = value;

    if (ok) {
      if (output[0] < clip_black_) {
        output[0] = 0.0f;
      }
      else if (output[0] >= clip_white_) {
        output[0] = 1.0f;
      }
      else {
        output[0] = (output[0] - clip_black_) / (clip_white_ - clip_black_);
      }
    }
  }
}

bool KeyingClipOperation::determine_depending_area_of_interest(rcti *input,
                                                               ReadBufferOperation *read_operation,
                                                               rcti *output)
{
  rcti new_input;

  new_input.xmin = input->xmin - kernel_radius_;
  new_input.ymin = input->ymin - kernel_radius_;
  new_input.xmax = input->xmax + kernel_radius_;
  new_input.ymax = input->ymax + kernel_radius_;

  return NodeOperation::determine_depending_area_of_interest(&new_input, read_operation, output);
}

void KeyingClipOperation::get_area_of_interest(const int input_idx,
                                               const rcti &output_area,
                                               rcti &r_input_area)
{
  BLI_assert(input_idx == 0);
  UNUSED_VARS_NDEBUG(input_idx);
  r_input_area.xmin = output_area.xmin - kernel_radius_;
  r_input_area.xmax = output_area.xmax + kernel_radius_;
  r_input_area.ymin = output_area.ymin - kernel_radius_;
  r_input_area.ymax = output_area.ymax + kernel_radius_;
}

void KeyingClipOperation::update_memory_buffer_partial(MemoryBuffer *output,
                                                       const rcti &area,
                                                       Span<MemoryBuffer *> inputs)
{
  const MemoryBuffer *input = inputs[0];
  BuffersIterator<float> it = output->iterate_with(inputs, area);

  const int delta = kernel_radius_;
  const float tolerance = kernel_tolerance_;
  const int width = this->get_width();
  const int height = this->get_height();
  const int row_stride = input->row_stride;
  const int elem_stride = input->elem_stride;
  for (; !it.is_end(); ++it) {
    const int x = it.x;
    const int y = it.y;

    const int start_x = MAX2(0, x - delta + 1);
    const int start_y = MAX2(0, y - delta + 1);
    const int end_x = MIN2(x + delta, width);
    const int end_y = MIN2(y + delta, height);
    const int x_len = end_x - start_x;
    const int y_len = end_y - start_y;

    const int total_count = x_len * y_len - 1;
    const int threshold_count = ceil((float)total_count * 0.9f);
    bool ok = false;
    if (delta == 0) {
      ok = true;
    }

    const float *main_elem = it.in(0);
    const float value = *main_elem;
    const float *row = input->get_elem(start_x, start_y);
    const float *end_row = row + y_len * row_stride;
    int count = 0;
    for (; ok == false && row < end_row; row += row_stride) {
      const float *end_elem = row + x_len * elem_stride;
      for (const float *elem = row; ok == false && elem < end_elem; elem += elem_stride) {
        if (UNLIKELY(elem == main_elem)) {
          continue;
        }

        const float current_value = *elem;
        if (fabsf(current_value - value) < tolerance) {
          count++;
          if (count >= threshold_count) {
            ok = true;
          }
        }
      }
    }

    if (is_edge_matte_) {
      *it.out = ok ? 0.0f : 1.0f;
    }
    else {
      if (!ok) {
        *it.out = value;
      }
      else if (value < clip_black_) {
        *it.out = 0.0f;
      }
      else if (value >= clip_white_) {
        *it.out = 1.0f;
      }
      else {
        *it.out = (value - clip_black_) / (clip_white_ - clip_black_);
      }
    }
  }
}

}  // namespace blender::compositor