Blender V5.3
node_composite_keying.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
5#include "BLI_math_color_c.hh"
7
8#include "DNA_scene_types.h"
9
10#include "RNA_enum_types.hh"
11
12#include "GPU_shader.hh"
13
17#include "COM_node_operation.hh"
18#include "COM_utilities.hh"
19
21
23
25{
26 b.use_custom_socket_order();
27 b.allow_any_socket_order();
28
29 b.add_input<decl::Color>("Image"_ustr)
30 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
31 .hide_value()
32 .structure_type(StructureType::Dynamic);
33 b.add_output<decl::Color>("Image"_ustr)
34 .structure_type(StructureType::Dynamic)
35 .align_with_previous();
36
37 b.add_output<decl::Float>("Matte"_ustr).structure_type(StructureType::Dynamic);
38 b.add_output<decl::Float>("Edges"_ustr)
39 .structure_type(StructureType::Dynamic)
41
42 b.add_input<decl::Color>("Key Color"_ustr)
43 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
44 .structure_type(StructureType::Dynamic);
45
46 PanelDeclarationBuilder &preprocess_panel = b.add_panel("Preprocess"_ustr).default_closed(true);
47 preprocess_panel.add_input<decl::Int>("Blur Size"_ustr, "Preprocess Blur Size"_ustr)
48 .default_value(0)
50 .min(0)
51 .description(
52 "Blur the color of the input image in YCC color space before keying while leaving the "
53 "luminance intact using a Gaussian blur of the given size");
54
55 PanelDeclarationBuilder &key_panel = b.add_panel("Key"_ustr)
56 .default_closed(true)
57 .translation_context(BLT_I18NCONTEXT_ID_NODETREE);
58 key_panel.add_input<decl::Float>("Balance"_ustr, "Key Balance"_ustr)
59 .default_value(0.5f)
61 .min(0.0f)
62 .max(1.0f)
63 .description(
64 "Balances between the two non primary color channels that the primary channel compares "
65 "against. 0 means the latter channel of the two is used, while 1 means the former of "
66 "the two is used");
67
68 PanelDeclarationBuilder &tweak_panel = b.add_panel("Tweak"_ustr).default_closed(true);
69 tweak_panel.add_input<decl::Float>("Black Level"_ustr)
70 .default_value(0.0f)
72 .min(0.0f)
73 .max(1.0f)
74 .description(
75 "The matte gets remapped such matte values lower than the black level become black. "
76 "Pixels at the identified edges are excluded from the remapping to preserve details");
77 tweak_panel.add_input<decl::Float>("White Level"_ustr)
78 .default_value(1.0f)
80 .min(0.0f)
81 .max(1.0f)
82 .description(
83 "The matte gets remapped such matte values higher than the white level become white. "
84 "Pixels at the identified edges are excluded from the remapping to preserve details");
85
86 PanelDeclarationBuilder &edges_panel = tweak_panel.add_panel("Edges"_ustr)
87 .default_closed(true)
89 edges_panel.add_input<decl::Int>("Size"_ustr, "Edge Search Size"_ustr)
90 .subtype(PROP_PIXEL)
92 .min(0)
93 .description(
94 "Size of the search window used to identify edges. Higher search size corresponds to "
95 "less noisy and higher quality edges, not necessarily bigger edges. Edge tolerance can "
96 "be used to expend the size of the edges");
97 edges_panel.add_input<decl::Float>("Tolerance"_ustr, "Edge Tolerance"_ustr)
98 .default_value(0.1f)
100 .min(0.0f)
101 .max(1.0f)
102 .description(
103 "Pixels are considered part of the edges if more than 10% of the neighbouring pixels "
104 "have matte values that differ from the pixel's matte value by this tolerance");
105
106 PanelDeclarationBuilder &mask_panel = b.add_panel("Mask"_ustr).default_closed(true);
107 mask_panel.add_input<decl::Float>("Garbage Matte"_ustr)
108 .default_value(0.0f)
110 .min(0.0f)
111 .max(1.0f)
112 .structure_type(StructureType::Dynamic)
113 .description("Areas in the garbage matte mask are excluded from the matte");
114 mask_panel.add_input<decl::Float>("Core Matte"_ustr)
115 .default_value(0.0f)
117 .min(0.0f)
118 .max(1.0f)
119 .structure_type(StructureType::Dynamic)
120 .description("Areas in the core matte mask are included in the matte");
121
122 PanelDeclarationBuilder &postprocess_panel =
123 b.add_panel("Postprocess"_ustr).default_closed(true);
124 postprocess_panel.add_input<decl::Int>("Blur Size"_ustr, "Postprocess Blur Size"_ustr)
125 .subtype(PROP_PIXEL)
126 .default_value(0)
127 .min(0)
128 .description("Blur the computed matte using a Gaussian blur of the given size");
129 postprocess_panel.add_input<decl::Int>("Dilate Size"_ustr, "Postprocess Dilate Size"_ustr)
130 .subtype(PROP_PIXEL)
131 .default_value(0)
132 .description(
133 "Dilate or erode the computed matte using a circular structuring element of the "
134 "specified size. Negative sizes means erosion while positive means dilation");
135 postprocess_panel.add_input<decl::Int>("Feather Size"_ustr, "Postprocess Feather Size"_ustr)
136 .subtype(PROP_PIXEL)
137 .default_value(0)
138 .description(
139 "Dilate or erode the computed matte using an inverse distance operation evaluated at "
140 "the given falloff of the specified size. Negative sizes means erosion while positive "
141 "means dilation");
142 postprocess_panel.add_input<decl::Menu>("Feather Falloff"_ustr)
143 .default_value(PROP_SMOOTH)
146 .translation_context(BLT_I18NCONTEXT_ID_CURVE_LEGACY);
147
148 PanelDeclarationBuilder &despill_panel = b.add_panel("Despill"_ustr).default_closed(true);
149 despill_panel.add_input<decl::Float>("Strength"_ustr, "Despill Strength"_ustr)
150 .default_value(1.0f)
152 .min(0.0f)
153 .max(1.0f)
154 .description("Specifies the strength of the despill");
155 despill_panel.add_input<decl::Float>("Balance"_ustr, "Despill Balance"_ustr)
156 .default_value(0.5f)
158 .min(0.0f)
159 .max(1.0f)
160 .description(
161 "Defines the channel used for despill limiting. Balances between the two non primary "
162 "color channels that the primary channel compares against. 0 means the latter channel "
163 "of the two is used, while 1 means the former of the two is used");
164}
165
166static void node_init(bNodeTree * /*ntree*/, bNode *node)
167{
168 /* Unused, only kept for forward compatibility. */
169 NodeKeyingData *data = MEM_new<NodeKeyingData>(__func__);
170 node->storage = data;
171}
172
173using namespace blender::compositor;
174
176 public:
178
179 void execute() override
180 {
181 const Result &input_image = get_input("Image");
182 Result &output_image = get_result("Image");
183 if (input_image.is_single_value()) {
184 if (output_image.should_compute()) {
185 output_image.share_data(input_image);
186 }
188 return;
189 }
190
191 Result blurred_input = compute_blurred_input();
192
193 Result matte = compute_matte(blurred_input);
194 blurred_input.release();
195
196 /* This also computes the edges output if needed. */
197 Result tweaked_matte = compute_tweaked_matte(matte);
198 matte.release();
199
200 Result &output_matte = this->get_result("Matte");
201 if (output_image.should_compute() || output_matte.should_compute()) {
202 Result blurred_matte = compute_blurred_matte(tweaked_matte);
203 tweaked_matte.release();
204
205 Result morphed_matte = compute_morphed_matte(blurred_matte);
206 blurred_matte.release();
207
208 Result feathered_matte = compute_feathered_matte(morphed_matte);
209 morphed_matte.release();
210
211 if (output_image.should_compute()) {
212 compute_image(feathered_matte);
213 }
214
215 if (output_matte.should_compute()) {
216 output_matte.share_data(feathered_matte);
217 }
218
219 feathered_matte.release();
220 }
221 else {
222 tweaked_matte.release();
223 }
224 }
225
227 {
228 /* No blur needed, return the original matte. We also increment the reference count of the
229 * input because the caller will release it after the call, and we want to extend its life
230 * since it is now returned as the output. */
231 const float blur_size = this->get_preprocess_blur_size();
232 if (blur_size == 0.0f) {
234 output.share_data(this->get_input("Image"));
235 return output;
236 }
237
238 Result chroma = extract_input_chroma();
239
240 Result blurred_chroma = context().create_result(ResultType::Color);
242 context(), chroma, blurred_chroma, float2(blur_size) / 2, math::FilterKernel::Box);
243 chroma.release();
244
245 Result blurred_input = replace_input_chroma(blurred_chroma);
246 blurred_chroma.release();
247
248 return blurred_input;
249 }
250
252 {
253 return math::max(0, this->get_input("Preprocess Blur Size").get_single_value_default<int>());
254 }
255
257 {
258 if (this->context().use_gpu()) {
259 return this->extract_input_chroma_gpu();
260 }
261 return this->extract_input_chroma_cpu();
262 }
263
265 {
266 gpu::Shader *shader = context().get_shader("compositor_keying_extract_chroma");
268
269 Result &input = get_input("Image");
270 input.bind_as_texture(shader, "input_tx");
271
273 output.allocate_texture(input.domain());
274 output.bind_as_image(shader, "output_img");
275
277
279 input.unbind_as_texture();
280 output.unbind_as_image();
281
282 return output;
283 }
284
286 {
287 Result &input = get_input("Image");
288
290 output.allocate_texture(input.domain());
291
292 parallel_for(input.domain().data_size, [&](const int2 texel) {
293 const Color color = input.load_pixel<Color>(texel);
294 float4 color_ycca;
295 rgb_to_ycc(color.r,
296 color.g,
297 color.b,
298 &color_ycca.x,
299 &color_ycca.y,
300 &color_ycca.z,
301 BLI_YCC_ITU_BT709);
302 color_ycca /= 255.0f;
303 color_ycca.w = color.a;
304
305 output.store_pixel(texel, Color(color_ycca));
306 });
307
308 return output;
309 }
310
312 {
313 if (this->context().use_gpu()) {
314 return this->replace_input_chroma_gpu(new_chroma);
315 }
316 return this->replace_input_chroma_cpu(new_chroma);
317 }
318
320 {
321 gpu::Shader *shader = context().get_shader("compositor_keying_replace_chroma");
323
324 Result &input = get_input("Image");
325 input.bind_as_texture(shader, "input_tx");
326
327 new_chroma.bind_as_texture(shader, "new_chroma_tx");
328
330 output.allocate_texture(input.domain());
331 output.bind_as_image(shader, "output_img");
332
334
336 input.unbind_as_texture();
337 new_chroma.unbind_as_texture();
338 output.unbind_as_image();
339
340 return output;
341 }
342
344 {
345 Result &input = get_input("Image");
346
348 output.allocate_texture(input.domain());
349
350 parallel_for(input.domain().data_size, [&](const int2 texel) {
351 const Color color = input.load_pixel<Color>(texel);
352 float4 color_ycca;
353 rgb_to_ycc(color.r,
354 color.g,
355 color.b,
356 &color_ycca.x,
357 &color_ycca.y,
358 &color_ycca.z,
359 BLI_YCC_ITU_BT709);
360
361 const float2 new_chroma_cb_cr = float4(new_chroma.load_pixel<Color>(texel)).yz();
362 color_ycca.y = new_chroma_cb_cr.x * 255.0f;
363 color_ycca.z = new_chroma_cb_cr.y * 255.0f;
364
365 float4 color_rgba;
366 ycc_to_rgb(color_ycca.x,
367 color_ycca.y,
368 color_ycca.z,
369 &color_rgba.x,
370 &color_rgba.y,
371 &color_rgba.z,
372 BLI_YCC_ITU_BT709);
373 color_rgba.w = color.a;
374
375 output.store_pixel(texel, Color(color_rgba));
376 });
377
378 return output;
379 }
380
382 {
383 if (this->context().use_gpu()) {
384 return this->compute_matte_gpu(input);
385 }
386 return this->compute_matte_cpu(input);
387 }
388
390 {
391 gpu::Shader *shader = context().get_shader("compositor_keying_compute_matte");
393
394 GPU_shader_uniform_1f(shader, "key_balance", this->get_key_balance());
395
396 input.bind_as_texture(shader, "input_tx");
397
398 Result &key_color = get_input("Key Color");
399 gpu::Texture *key_color_texture = key_color.bind_as_texture_or_single_value(shader, "key_tx");
400
402 output.allocate_texture(input.domain());
403 output.bind_as_image(shader, "output_img");
404
406
408 input.unbind_as_texture();
409 key_color.unbind_as_texture_or_single_value(key_color_texture);
410 output.unbind_as_image();
411
412 return output;
413 }
414
416 {
417 const float key_balance = this->get_key_balance();
418
419 Result &key = get_input("Key Color");
420
422 output.allocate_texture(input.domain());
423
424 auto compute_saturation_indices = [](const float3 &v) {
425 int index_of_max = ((v.x > v.y) ? ((v.x > v.z) ? 0 : 2) : ((v.y > v.z) ? 1 : 2));
426 int2 other_indices = (int2(index_of_max) + int2(1, 2)) % 3;
427 int min_index = math::min(other_indices.x, other_indices.y);
428 int max_index = math::max(other_indices.x, other_indices.y);
429 return int3(index_of_max, max_index, min_index);
430 };
431
432 auto compute_saturation = [&](const float4 &color, const int3 &indices) {
433 float weighted_average = math::interpolate(color[indices.y], color[indices.z], key_balance);
434 return (color[indices.x] - weighted_average) * math::abs(1.0f - weighted_average);
435 };
436
437 parallel_for(input.domain().data_size, [&](const int2 texel) {
438 float4 input_color = float4(input.load_pixel<Color>(texel));
439
440 /* We assume that the keying screen will not be overexposed in the image, so if the input
441 * brightness is high, we assume the pixel is opaque. */
442 if (math::reduce_min(input_color) > 1.0f) {
443 output.store_pixel(texel, 1.0f);
444 return;
445 }
446
447 float4 key_color = float4(key.load_pixel<Color, true>(texel));
448 int3 key_saturation_indices = compute_saturation_indices(key_color.xyz());
449 float input_saturation = compute_saturation(input_color, key_saturation_indices);
450 float key_saturation = compute_saturation(key_color, key_saturation_indices);
451
452 float matte;
453 if (input_saturation < 0.0f) {
454 /* Means main channel of pixel is different from screen, assume this is completely a
455 * foreground. */
456 matte = 1.0f;
457 }
458 else if (input_saturation >= key_saturation) {
459 /* Matched main channels and higher saturation on pixel is treated as completely
460 * background. */
461 matte = 0.0f;
462 }
463 else {
464 matte = 1.0f - math::clamp(input_saturation / key_saturation, 0.0f, 1.0f);
465 }
466
467 output.store_pixel(texel, matte);
468 });
469
470 return output;
471 }
472
474 {
475 return math::clamp(
476 this->get_input("Key Balance").get_single_value_default<float>(), 0.0f, 1.0f);
477 }
478
480 {
481 const float black_level = this->get_black_level();
482 const float white_level = this->get_black_level();
483 const Result &garbage_matte = this->get_input("Garbage Matte");
484 const Result &core_matte = this->get_input("Core Matte");
485
486 /* The edges output is not needed and the matte is not tweaked, so return the original matte.
487 * We also increment the reference count of the input because the caller will release it after
488 * the call, and we want to extend its life since it is now returned as the output. */
489 Result &output_edges = get_result("Edges");
490 if (!output_edges.should_compute() && black_level == 0.0f && white_level == 1.0f &&
491 core_matte.get_single_value_default<float>() == 0.0f &&
492 garbage_matte.get_single_value_default<float>() == 0.0f)
493 {
494 Result output_matte = this->context().create_result(ResultType::Float);
495 output_matte.share_data(input_matte);
496 return output_matte;
497 }
498
499 if (this->context().use_gpu()) {
500 return this->compute_tweaked_matte_gpu(input_matte);
501 }
502 return this->compute_tweaked_matte_cpu(input_matte);
503 }
504
506 {
507 gpu::Shader *shader = context().get_shader(this->get_tweak_matte_shader_name());
509
510 GPU_shader_uniform_1i(shader, "edge_search_radius", this->get_edge_search_size());
511 GPU_shader_uniform_1f(shader, "edge_tolerance", this->get_edge_tolerance());
512 GPU_shader_uniform_1f(shader, "black_level", this->get_black_level());
513 GPU_shader_uniform_1f(shader, "white_level", this->get_white_level());
514
515 input_matte.bind_as_texture(shader, "input_matte_tx");
516
517 Result &garbage_matte = get_input("Garbage Matte");
518 gpu::Texture *garbage_matte_texture = garbage_matte.bind_as_texture_or_single_value(
519 shader, "garbage_matte_tx");
520
521 Result &core_matte = get_input("Core Matte");
522 gpu::Texture *core_matte_texture = core_matte.bind_as_texture_or_single_value(shader,
523 "core_matte_tx");
524
525 Result output_matte = context().create_result(ResultType::Float);
526 output_matte.allocate_texture(input_matte.domain());
527 output_matte.bind_as_image(shader, "output_matte_img");
528
529 Result &output_edges = get_result("Edges");
530 if (output_edges.should_compute()) {
531 output_edges.allocate_texture(input_matte.domain());
532 output_edges.bind_as_image(shader, "output_edges_img");
533 }
534
536
538 input_matte.unbind_as_texture();
539 garbage_matte.unbind_as_texture_or_single_value(garbage_matte_texture);
540 core_matte.unbind_as_texture_or_single_value(core_matte_texture);
541 output_matte.unbind_as_image();
542 if (output_edges.should_compute()) {
543 output_edges.unbind_as_image();
544 }
545
546 return output_matte;
547 }
548
550 {
551 Result &output_edges = get_result("Edges");
552 return output_edges.should_compute() ? "compositor_keying_tweak_matte_with_edges" :
553 "compositor_keying_tweak_matte_without_edges";
554 }
555
557 {
558 const int edge_search_radius = this->get_edge_search_size();
559 const float edge_tolerance = this->get_edge_tolerance();
560 const float black_level = this->get_black_level();
561 const float white_level = this->get_white_level();
562
563 Result &garbage_matte_image = get_input("Garbage Matte");
564 Result &core_matte_image = get_input("Core Matte");
565
566 Result output_matte = context().create_result(ResultType::Float);
567 output_matte.allocate_texture(input_matte.domain());
568
569 Result &output_edges = this->get_result("Edges");
570 const bool compute_edges = output_edges.should_compute();
571 if (compute_edges) {
572 output_edges.allocate_texture(input_matte.domain());
573 }
574
575 parallel_for(input_matte.domain().data_size, [&](const int2 texel) {
576 float matte = input_matte.load_pixel<float>(texel);
577
578 /* Search the neighborhood around the current matte value and identify if it lies along the
579 * edges of the matte. This is needs to be computed only when we need to compute the edges
580 * output or tweak the levels of the matte. */
581 bool is_edge = false;
582 if (compute_edges || black_level != 0.0f || white_level != 1.0f) {
583 /* Count the number of neighbors whose matte is sufficiently similar to the current matte,
584 * as controlled by the edge_tolerance factor. */
585 int count = 0;
586 for (int j = -edge_search_radius; j <= edge_search_radius; j++) {
587 for (int i = -edge_search_radius; i <= edge_search_radius; i++) {
588 float neighbor_matte = input_matte.load_pixel_extended<float>(texel + int2(i, j));
589 count += int(math::distance(matte, neighbor_matte) < edge_tolerance);
590 }
591 }
592
593 /* If the number of neighbors that are sufficiently similar to the center matte is less
594 * that 90% of the total number of neighbors, then that means the variance is high in that
595 * areas and it is considered an edge. */
596 is_edge = count < ((edge_search_radius * 2 + 1) * (edge_search_radius * 2 + 1)) * 0.9f;
597 }
598
599 float tweaked_matte = matte;
600
601 /* Remap the matte using the black and white levels, but only for areas that are not on the
602 * edge of the matte to preserve details. Also check for equality between levels to avoid
603 * zero division. */
604 if (!is_edge && white_level != black_level) {
605 tweaked_matte = math::clamp(
606 (matte - black_level) / (white_level - black_level), 0.0f, 1.0f);
607 }
608
609 /* Exclude unwanted areas using the provided garbage matte, 1 means unwanted, so invert the
610 * garbage matte and take the minimum. */
611 float garbage_matte = garbage_matte_image.load_pixel<float, true>(texel);
612 tweaked_matte = math::min(tweaked_matte, 1.0f - garbage_matte);
613
614 /* Include wanted areas that were incorrectly keyed using the provided core matte. */
615 float core_matte = core_matte_image.load_pixel<float, true>(texel);
616 tweaked_matte = math::max(tweaked_matte, core_matte);
617
618 output_matte.store_pixel(texel, tweaked_matte);
619 if (compute_edges) {
620 output_edges.store_pixel(texel, is_edge ? 1.0f : 0.0f);
621 }
622 });
623
624 return output_matte;
625 }
626
628 {
629 return math::max(0, this->get_input("Edge Search Size").get_single_value_default<int>());
630 }
631
633 {
634 return math::clamp(
635 this->get_input("Edge Tolerance").get_single_value_default<float>(), 0.0f, 1.0f);
636 }
637
639 {
640 return math::clamp(
641 this->get_input("Black Level").get_single_value_default<float>(), 0.0f, 1.0f);
642 }
643
645 {
646 return math::clamp(
647 this->get_input("White Level").get_single_value_default<float>(), 0.0f, 1.0f);
648 }
649
651 {
652 const float blur_size = this->get_postprocess_blur_size();
653 /* No blur needed, return the original matte. We also increment the reference count of the
654 * input because the caller will release it after the call, and we want to extend its life
655 * since it is now returned as the output. */
656 if (blur_size == 0.0f) {
657 Result output_matte = this->context().create_result(ResultType::Float);
658 output_matte.share_data(input_matte);
659 return output_matte;
660 }
661
662 Result blurred_matte = context().create_result(ResultType::Float);
664 context(), input_matte, blurred_matte, float2(blur_size) / 2, math::FilterKernel::Box);
665
666 return blurred_matte;
667 }
668
670 {
671 return math::max(0, this->get_input("Postprocess Blur Size").get_single_value_default<int>());
672 }
673
675 {
676 const int distance = this->get_postprocess_dilate_size();
677 /* No morphology needed, return the original matte. We also increment the reference count of
678 * the input because the caller will release it after the call, and we want to extend its life
679 * since it is now returned as the output. */
680 if (distance == 0) {
681 Result output_matte = this->context().create_result(ResultType::Float);
682 output_matte.share_data(input_matte);
683 return output_matte;
684 }
685
686 Result morphed_matte = context().create_result(ResultType::Float);
687 morphological_distance(context(), input_matte, morphed_matte, distance);
688
689 return morphed_matte;
690 }
691
693 {
694 return this->get_input("Postprocess Dilate Size").get_single_value_default<int>();
695 }
696
698 {
699 const int distance = this->get_postprocess_feather_size();
700 /* No feathering needed, return the original matte. We also increment the reference count of
701 * the input because the caller will release it after the call, and we want to extend its life
702 * since it is now returned as the output. */
703 if (distance == 0) {
704 Result output_matte = this->context().create_result(ResultType::Float);
705 output_matte.share_data(input_matte);
706 return output_matte;
707 }
708
709 Result feathered_matte = context().create_result(ResultType::Float);
711 context(), input_matte, feathered_matte, distance, this->get_feather_falloff());
712
713 return feathered_matte;
714 }
715
717 {
718 return this->get_input("Postprocess Feather Size").get_single_value_default<int>();
719 }
720
722 {
723 return this->get_input("Feather Falloff").get_single_value_default<MenuValue>().value;
724 }
725
727 {
728 if (this->context().use_gpu()) {
729 this->compute_image_gpu(matte);
730 }
731 else {
732 this->compute_image_cpu(matte);
733 }
734 }
735
737 {
738 gpu::Shader *shader = context().get_shader("compositor_keying_compute_image");
740
741 GPU_shader_uniform_1f(shader, "despill_factor", this->get_despill_strength());
742 GPU_shader_uniform_1f(shader, "despill_balance", this->get_despill_balance());
743
744 Result &input = get_input("Image");
745 input.bind_as_texture(shader, "input_tx");
746
747 Result &key = get_input("Key Color");
748 gpu::Texture *key_color_texture = key.bind_as_texture_or_single_value(shader, "key_tx");
749
750 matte.bind_as_texture(shader, "matte_tx");
751
752 Result &output = get_result("Image");
753 output.allocate_texture(matte.domain());
754 output.bind_as_image(shader, "output_img");
755
757
759 input.unbind_as_texture();
760 key.unbind_as_texture_or_single_value(key_color_texture);
761 matte.unbind_as_texture();
762 output.unbind_as_image();
763 }
764
765 void compute_image_cpu(Result &matte_image)
766 {
767 const float despill_factor = this->get_despill_strength();
768 const float despill_balance = this->get_despill_balance();
769
770 Result &input = get_input("Image");
771 Result &key = get_input("Key Color");
772
773 Result &output = get_result("Image");
774 output.allocate_texture(matte_image.domain());
775
776 auto compute_saturation_indices = [](const float3 &v) {
777 int index_of_max = ((v.x > v.y) ? ((v.x > v.z) ? 0 : 2) : ((v.y > v.z) ? 1 : 2));
778 int2 other_indices = (int2(index_of_max) + int2(1, 2)) % 3;
779 int min_index = math::min(other_indices.x, other_indices.y);
780 int max_index = math::max(other_indices.x, other_indices.y);
781 return int3(index_of_max, max_index, min_index);
782 };
783
784 parallel_for(input.domain().data_size, [&](const int2 texel) {
785 float4 key_color = float4(key.load_pixel<Color, true>(texel));
786 float4 color = float4(input.load_pixel<Color>(texel));
787 float matte = matte_image.load_pixel<float>(texel);
788
789 /* Alpha multiply the matte to the image. */
790 color *= matte;
791
792 /* Color despill. */
793 int3 indices = compute_saturation_indices(key_color.xyz());
794 float weighted_average = math::interpolate(
795 color[indices.y], color[indices.z], despill_balance);
796 color[indices.x] -= math::max(0.0f, (color[indices.x] - weighted_average) * despill_factor);
797
798 output.store_pixel(texel, Color(color));
799 });
800 }
801
803 {
804 return math::max(0.0f, this->get_input("Despill Strength").get_single_value_default<float>());
805 }
806
808 {
809 return math::clamp(
810 this->get_input("Despill Balance").get_single_value_default<float>(), 0.0f, 1.0f);
811 }
812};
813
815{
816 return new KeyingOperation(context, node);
817}
818
819static void node_register()
820{
821 static bke::bNodeType ntype;
822
823 cmp_node_type_base(&ntype, "CompositorNodeKeying"_ustr, CMP_NODE_KEYING);
824 ntype.ui_name = "Keying";
825 ntype.ui_description =
826 "Perform both chroma keying (to remove the backdrop) and despill (to correct color cast "
827 "from the backdrop)";
828 ntype.enum_name_legacy = "KEYING";
829 ntype.nclass = NODE_CLASS_MATTE;
830 ntype.declare = node_declare;
831 ntype.initfunc = node_init;
836
838}
839NOD_REGISTER_NODE(node_register)
840
841} // namespace blender::nodes::node_composite_keying_cc
#define NODE_CLASS_MATTE
Definition BKE_node.hh:485
#define CMP_NODE_KEYING
#define BLT_I18NCONTEXT_ID_NODETREE
#define BLT_I18NCONTEXT_ID_IMAGE
#define BLT_I18NCONTEXT_ID_CURVE_LEGACY
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
Result create_result(ResultType type, ResultPrecision precision)
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
void share_data(const Result &source)
Definition result.cc:996
void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
Definition result.cc:970
void store_pixel(const int2 &texel, const T &pixel_value)
bool should_compute() const
Definition result.cc:1124
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
T load_pixel(const int2 &texel) const
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
gpu::Texture * bind_as_texture_or_single_value(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:861
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
PanelDeclarationBuilder & add_panel(UString name, int identifier=-1)
Self & translation_context(std::optional< std::string > value=std::nullopt)
std::optional< std::string > translation_context
float distance(float, float) RET
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
void symmetric_separable_blur(Context &context, const Result &input, Result &output, const float2 &radius, const math::FilterKernel filter_type=math::FilterKernel::Gauss, const bool extend_bounds=false)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void morphological_distance(Context &context, const Result &input, Result &output, const int distance)
void morphological_distance_feather(Context &context, const Result &input, Result &output, const int distance, const int falloff_type=PROP_SMOOTH)
void parallel_for(const int2 range, const Function &function)
T clamp(const T &a, const T &min, const T &max)
T min(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T max(const T &a, const T &b)
T abs(const T &a)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMesh const char void * data
void GPU_shader_unbind()
const EnumPropertyItem rna_enum_proportional_falloff_curve_only_items[]
Definition rna_scene.cc:119
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static constexpr int _160
Definition BKE_node.hh:246
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393