Blender V4.3
COM_ChromaMatteOperation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7namespace blender::compositor {
8
17
19 const rcti &area,
21{
22 const float acceptance = settings_->t1; /* In radians. */
23 const float cutoff = settings_->t2; /* In radians. */
24 const float gain = settings_->fstrength;
25 for (BuffersIterator<float> it = output->iterate_with(inputs, area); !it.is_end(); ++it) {
26 const float *in_image = it.in(0);
27 const float *in_key = it.in(1);
28
29 /* Store matte(alpha) value in [0] to go with
30 * #COM_SetAlphaMultiplyOperation and the Value output. */
31
32 /* Algorithm from book "Video Demystified", does not include the spill reduction part. */
33 /* Find theta, the angle that the color space should be rotated based on key. */
34
35 /* Rescale to `-1.0..1.0`. */
36 // const float image_Y = (in_image[0] * 2.0f) - 1.0f; // UNUSED
37 const float image_cb = (in_image[1] * 2.0f) - 1.0f;
38 const float image_cr = (in_image[2] * 2.0f) - 1.0f;
39
40 // const float key_Y = (in_key[0] * 2.0f) - 1.0f; // UNUSED
41 const float key_cb = (in_key[1] * 2.0f) - 1.0f;
42 const float key_cr = (in_key[2] * 2.0f) - 1.0f;
43
44 const float theta = atan2(key_cr, key_cb);
45
46 /* Rotate the cb and cr into x/z space. */
47 const float x_angle = image_cb * cosf(theta) + image_cr * sinf(theta);
48 const float z_angle = image_cr * cosf(theta) - image_cb * sinf(theta);
49
50 /* If within the acceptance angle. */
51 /* If kfg is <0 then the pixel is outside of the key color. */
52 const float kfg = x_angle - (fabsf(z_angle) / tanf(acceptance / 2.0f));
53
54 if (kfg > 0.0f) { /* Found a pixel that is within key color. */
55 const float beta = atan2(z_angle, x_angle);
56 float alpha = 1.0f - (kfg / gain);
57
58 /* Ff beta is within the cutoff angle. */
59 if (fabsf(beta) < (cutoff / 2.0f)) {
60 alpha = 0.0f;
61 }
62
63 /* Don't make something that was more transparent less transparent. */
64 it.out[0] = alpha < in_image[3] ? alpha : in_image[3];
65 }
66 else { /* Pixel is outside key color. */
67 it.out[0] = in_image[3]; /* Make pixel just as transparent as it was before. */
68 }
69 }
70}
71
72} // namespace blender::compositor
void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
a MemoryBuffer contains access to the data
void add_output_socket(DataType datatype)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
#define sinf(x)
#define cosf(x)
#define tanf(x)
#define fabsf(x)
typename BuffersIteratorBuilder< T >::Iterator BuffersIterator
ccl_device_inline float beta(float x, float y)
Definition util/math.h:833