4 * Copyright (C) 2012 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bcdisplayinfo.h"
23 #include "bcsignals.h"
24 #include "chromakey.h"
31 #include "loadbalance.h"
32 #include "playback3d.h"
34 #include "pluginvclient.h"
43 ChromaKeyConfig::ChromaKeyConfig ()
49 min_brightness = 50.0;
50 max_brightness = 100.0;
53 min_saturation = 50.0;
59 spill_threshold = 0.0;
68 ChromaKeyConfig::copy_from (ChromaKeyConfig & src)
73 spill_threshold = src.spill_threshold;
74 spill_amount = src.spill_amount;
75 min_brightness = src.min_brightness;
76 max_brightness = src.max_brightness;
77 saturation = src.saturation;
78 min_saturation = src.min_saturation;
79 tolerance = src.tolerance;
80 in_slope = src.in_slope;
81 out_slope = src.out_slope;
82 alpha_offset = src.alpha_offset;
83 show_mask = src.show_mask;
87 ChromaKeyConfig::equivalent (ChromaKeyConfig & src)
89 return (EQUIV (red, src.red) &&
90 EQUIV (green, src.green) &&
91 EQUIV (blue, src.blue) &&
92 EQUIV (spill_threshold, src.spill_threshold) &&
93 EQUIV (spill_amount, src.spill_amount) &&
94 EQUIV (min_brightness, src.min_brightness) &&
95 EQUIV (max_brightness, src.max_brightness) &&
96 EQUIV (saturation, src.saturation) &&
97 EQUIV (min_saturation, src.min_saturation) &&
98 EQUIV (tolerance, src.tolerance) &&
99 EQUIV (in_slope, src.in_slope) &&
100 EQUIV (out_slope, src.out_slope) &&
101 EQUIV (show_mask, src.show_mask) &&
102 EQUIV (alpha_offset, src.alpha_offset));
106 ChromaKeyConfig::interpolate (ChromaKeyConfig & prev,
107 ChromaKeyConfig & next,
109 int64_t next_frame, int64_t current_frame)
112 (double) (current_frame - prev_frame) / (next_frame - prev_frame);
114 (double) (next_frame - current_frame) / (next_frame - prev_frame);
116 this->red = prev.red * prev_scale + next.red * next_scale;
117 this->green = prev.green * prev_scale + next.green * next_scale;
118 this->blue = prev.blue * prev_scale + next.blue * next_scale;
119 this->spill_threshold =
120 prev.spill_threshold * prev_scale + next.spill_threshold * next_scale;
122 prev.spill_amount * prev_scale + next.tolerance * next_scale;
123 this->min_brightness =
124 prev.min_brightness * prev_scale + next.min_brightness * next_scale;
125 this->max_brightness =
126 prev.max_brightness * prev_scale + next.max_brightness * next_scale;
128 prev.saturation * prev_scale + next.saturation * next_scale;
129 this->min_saturation =
130 prev.min_saturation * prev_scale + next.min_saturation * next_scale;
131 this->tolerance = prev.tolerance * prev_scale + next.tolerance * next_scale;
132 this->in_slope = prev.in_slope * prev_scale + next.in_slope * next_scale;
133 this->out_slope = prev.out_slope * prev_scale + next.out_slope * next_scale;
135 prev.alpha_offset * prev_scale + next.alpha_offset * next_scale;
136 this->show_mask = next.show_mask;
141 ChromaKeyConfig::get_color ()
143 int red = (int) (CLIP (this->red, 0, 1) * 0xff);
144 int green = (int) (CLIP (this->green, 0, 1) * 0xff);
145 int blue = (int) (CLIP (this->blue, 0, 1) * 0xff);
146 return (red << 16) | (green << 8) | blue;
151 ChromaKeyWindow::ChromaKeyWindow (ChromaKeyHSV * plugin)
152 : PluginClientWindow(plugin,
159 this->plugin = plugin;
163 ChromaKeyWindow::~ChromaKeyWindow ()
169 ChromaKeyWindow::create_objects ()
171 int y = 10, y1, x1 = 0, x2 = 10;
176 int ymargin = get_text_height(MEDIUMFONT) + 5;
177 int ymargin2 = get_text_height(MEDIUMFONT) + 10;
179 add_subwindow (title = new BC_Title (x2, y, _("Color:")));
181 add_subwindow (color = new ChromaKeyColor (plugin, this, x, y + 25));
183 add_subwindow (sample =
184 new BC_SubWindow (x + color->get_w () + 10, y, 100, 50));
185 y += sample->get_h () + 10;
187 add_subwindow (use_colorpicker =
188 new ChromaKeyUseColorPicker (plugin, this, x, y));
189 y += use_colorpicker->get_h() + 10;
190 add_subwindow (show_mask = new ChromaKeyShowMask (plugin, x2, y));
191 y += show_mask->get_h() + 5;
193 add_subwindow(bar = new BC_Bar(x2, y, get_w() - x2 * 2));
194 y += bar->get_h() + 5;
196 add_subwindow (new BC_Title (x2, y, _("Key parameters:")));
198 add_subwindow (title = new BC_Title (x, y, _("Hue Tolerance:")));
199 if(title->get_w() > x1) x1 = title->get_w();
201 add_subwindow (title = new BC_Title (x, y, _("Min. Brightness:")));
202 if(title->get_w() > x1) x1 = title->get_w();
204 add_subwindow (title = new BC_Title (x, y, _("Max. Brightness:")));
205 if(title->get_w() > x1) x1 = title->get_w();
207 add_subwindow (title = new BC_Title (x, y, _("Saturation Offset:")));
208 if(title->get_w() > x1) x1 = title->get_w();
210 add_subwindow (title = new BC_Title (x, y, _("Min Saturation:")));
211 if(title->get_w() > x1) x1 = title->get_w();
214 add_subwindow(bar = new BC_Bar(x2, y, get_w() - x2 * 2));
215 y += bar->get_h() + 5;
216 add_subwindow (title = new BC_Title (x2, y, _("Mask tweaking:")));
218 add_subwindow (title = new BC_Title (x, y, _("In Slope:")));
219 if(title->get_w() > x1) x1 = title->get_w();
221 add_subwindow (title = new BC_Title (x, y, _("Out Slope:")));
222 if(title->get_w() > x1) x1 = title->get_w();
224 add_subwindow (title = new BC_Title (x, y, _("Alpha Offset:")));
225 if(title->get_w() > x1) x1 = title->get_w();
230 add_subwindow(bar = new BC_Bar(x2, y, get_w() - x2 * 2));
231 y += bar->get_h() + 5;
232 add_subwindow (title = new BC_Title (x2, y, _("Spill light control:")));
234 add_subwindow (title = new BC_Title (x, y, _("Spill Threshold:")));
235 if(title->get_w() > x1) x1 = title->get_w();
237 add_subwindow (title = new BC_Title (x, y, _("Spill Compensation:")));
238 if(title->get_w() > x1) x1 = title->get_w();
248 add_subwindow (tolerance = new ChromaKeyTolerance (plugin, x1, y));
250 add_subwindow (min_brightness = new ChromaKeyMinBrightness (plugin, x1, y));
252 add_subwindow (max_brightness = new ChromaKeyMaxBrightness (plugin, x1, y));
254 add_subwindow (saturation = new ChromaKeySaturation (plugin, x1, y));
256 add_subwindow (min_saturation = new ChromaKeyMinSaturation (plugin, x1, y));
259 y += bar->get_h() + 5;
261 add_subwindow (in_slope = new ChromaKeyInSlope (plugin, x1, y));
263 add_subwindow (out_slope = new ChromaKeyOutSlope (plugin, x1, y));
265 add_subwindow (alpha_offset = new ChromaKeyAlphaOffset (plugin, x1, y));
268 y += bar->get_h() + 5;
270 add_subwindow (spill_threshold = new ChromaKeySpillThreshold (plugin, x1, y));
272 add_subwindow (spill_amount = new ChromaKeySpillAmount (plugin, x1, y));
274 color_thread = new ChromaKeyColorThread (plugin, this);
281 ChromaKeyWindow::update_sample ()
283 sample->set_color (plugin->config.get_color ());
284 sample->draw_box (0, 0, sample->get_w (), sample->get_h ());
285 sample->set_color (BLACK);
286 sample->draw_rectangle (0, 0, sample->get_w (), sample->get_h ());
290 void ChromaKeyWindow::done_event(int result)
292 color_thread->close_window();
296 ChromaKeyColor::ChromaKeyColor (ChromaKeyHSV * plugin,
297 ChromaKeyWindow * gui, int x, int y):
298 BC_GenericButton (x, y, _("Color..."))
300 this->plugin = plugin;
305 ChromaKeyColor::handle_event ()
307 gui->color_thread->start_window (plugin->config.get_color (), 0xff);
314 ChromaKeyMinBrightness::ChromaKeyMinBrightness (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
317 200, 200, (float) 0, (float) 100, plugin->config.min_brightness)
319 this->plugin = plugin;
320 set_precision (0.01);
324 ChromaKeyMinBrightness::handle_event ()
326 plugin->config.min_brightness = get_value ();
327 plugin->send_configure_change ();
331 ChromaKeyMaxBrightness::ChromaKeyMaxBrightness (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
334 200, 200, (float) 0, (float) 100, plugin->config.max_brightness)
336 this->plugin = plugin;
337 set_precision (0.01);
341 ChromaKeyMaxBrightness::handle_event ()
343 plugin->config.max_brightness = get_value ();
344 plugin->send_configure_change ();
349 ChromaKeySaturation::ChromaKeySaturation (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
351 0, 200, 200, (float) 0, (float) 100, plugin->config.saturation)
353 this->plugin = plugin;
354 set_precision (0.01);
358 ChromaKeySaturation::handle_event ()
360 plugin->config.saturation = get_value ();
361 plugin->send_configure_change ();
365 ChromaKeyMinSaturation::ChromaKeyMinSaturation (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
368 200, 200, (float) 0, (float) 100, plugin->config.min_saturation)
370 this->plugin = plugin;
371 set_precision (0.01);
375 ChromaKeyMinSaturation::handle_event ()
377 plugin->config.min_saturation = get_value ();
378 plugin->send_configure_change ();
383 ChromaKeyTolerance::ChromaKeyTolerance (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
385 0, 200, 200, (float) 0, (float) 100, plugin->config.tolerance)
387 this->plugin = plugin;
388 set_precision (0.01);
392 ChromaKeyTolerance::handle_event ()
394 plugin->config.tolerance = get_value ();
395 plugin->send_configure_change ();
401 ChromaKeyInSlope::ChromaKeyInSlope (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
403 0, 200, 200, (float) 0, (float) 20, plugin->config.in_slope)
405 this->plugin = plugin;
406 set_precision (0.01);
410 ChromaKeyInSlope::handle_event ()
412 plugin->config.in_slope = get_value ();
413 plugin->send_configure_change ();
418 ChromaKeyOutSlope::ChromaKeyOutSlope (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
420 0, 200, 200, (float) 0, (float) 20, plugin->config.out_slope)
422 this->plugin = plugin;
423 set_precision (0.01);
427 ChromaKeyOutSlope::handle_event ()
429 plugin->config.out_slope = get_value ();
430 plugin->send_configure_change ();
435 ChromaKeyAlphaOffset::ChromaKeyAlphaOffset (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
438 200, 200, (float) -100, (float) 100, plugin->config.alpha_offset)
440 this->plugin = plugin;
441 set_precision (0.01);
445 ChromaKeyAlphaOffset::handle_event ()
447 plugin->config.alpha_offset = get_value ();
448 plugin->send_configure_change ();
452 ChromaKeyShowMask::ChromaKeyShowMask (ChromaKeyHSV * plugin, int x, int y):BC_CheckBox (x, y, plugin->config.show_mask,
456 this->plugin = plugin;
461 ChromaKeyShowMask::handle_event ()
463 plugin->config.show_mask = get_value ();
464 plugin->send_configure_change ();
468 ChromaKeyUseColorPicker::ChromaKeyUseColorPicker (ChromaKeyHSV * plugin, ChromaKeyWindow * gui, int x, int y)
469 : BC_GenericButton (x, y,
471 ("Use color picker"))
473 this->plugin = plugin;
478 ChromaKeyUseColorPicker::handle_event ()
480 plugin->config.red = plugin->get_red ();
481 plugin->config.green = plugin->get_green ();
482 plugin->config.blue = plugin->get_blue ();
484 gui->update_sample ();
487 plugin->send_configure_change ();
493 ChromaKeySpillThreshold::ChromaKeySpillThreshold (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
496 200, 200, (float) 0, (float) 100, plugin->config.spill_threshold)
498 this->plugin = plugin;
499 set_precision (0.01);
503 ChromaKeySpillThreshold::handle_event ()
505 plugin->config.spill_threshold = get_value ();
506 plugin->send_configure_change ();
510 ChromaKeySpillAmount::ChromaKeySpillAmount (ChromaKeyHSV * plugin, int x, int y):BC_FSlider (x,
512 0, 200, 200, (float) 0, (float) 100, plugin->config.spill_amount)
514 this->plugin = plugin;
515 set_precision (0.01);
519 ChromaKeySpillAmount::handle_event ()
521 plugin->config.spill_amount = get_value ();
522 plugin->send_configure_change ();
531 ChromaKeyColorThread::ChromaKeyColorThread (ChromaKeyHSV * plugin, ChromaKeyWindow * gui)
532 : ColorPicker (1, _("Inner color"))
534 this->plugin = plugin;
539 ChromaKeyColorThread::handle_new_color (int output, int alpha)
541 plugin->config.red = (float) (output & 0xff0000) / 0xff0000;
542 plugin->config.green = (float) (output & 0xff00) / 0xff00;
543 plugin->config.blue = (float) (output & 0xff) / 0xff;
545 get_gui()->unlock_window();
546 gui->lock_window("ChromaKeyColorThread::handle_new_color");
547 gui->update_sample ();
548 gui->unlock_window();
549 get_gui()->lock_window("ChromaKeyColorThread::handle_new_color");
552 plugin->send_configure_change ();
563 ChromaKeyServer::ChromaKeyServer (ChromaKeyHSV * plugin):LoadServer (plugin->PluginClient::smp + 1,
564 plugin->PluginClient::smp +
567 this->plugin = plugin;
571 ChromaKeyServer::init_packages ()
573 for (int i = 0; i < get_total_packages (); i++)
575 ChromaKeyPackage *pkg = (ChromaKeyPackage *) get_package (i);
576 pkg->y1 = plugin->input->get_h () * i / get_total_packages ();
577 pkg->y2 = plugin->input->get_h () * (i + 1) / get_total_packages ();
582 ChromaKeyServer::new_client ()
584 return new ChromaKeyUnit (plugin, this);
588 ChromaKeyServer::new_package ()
590 return new ChromaKeyPackage;
595 ChromaKeyPackage::ChromaKeyPackage ():LoadPackage ()
599 ChromaKeyUnit::ChromaKeyUnit (ChromaKeyHSV * plugin, ChromaKeyServer * server):LoadClient
602 this->plugin = plugin;
608 #define ABS(a) ((a<0)?-(a):a)
609 // Reuse as much as possible in the opengl version
610 #define OUTER_VARIABLES \
611 float red = plugin->config.red; \
612 float green = plugin->config.green; \
613 float blue = plugin->config.blue; \
615 float in_slope = plugin->config.in_slope / 100; \
616 float out_slope = plugin->config.out_slope / 100; \
618 float tolerance = plugin->config.tolerance / 100; \
619 float tolerance_in = tolerance - in_slope; \
620 float tolerance_out = tolerance + out_slope; \
622 float sat = plugin->config.saturation / 100; \
623 float min_s = plugin->config.min_saturation / 100; \
624 float min_s_in = min_s + in_slope; \
625 float min_s_out = min_s - out_slope; \
627 float min_v = plugin->config.min_brightness / 100; \
628 float min_v_in = min_v + in_slope; \
629 float min_v_out = min_v - out_slope; \
631 float max_v = plugin->config.max_brightness / 100; \
632 float max_v_in = max_v - in_slope; \
633 float max_v_out = max_v + out_slope; \
635 float spill_threshold = plugin->config.spill_threshold / 100; \
636 float spill_amount = 1.0 - plugin->config.spill_amount / 100; \
638 float alpha_offset = plugin->config.alpha_offset / 100; \
640 /* Convert RGB key to HSV key */ \
641 float hue_key, saturation_key, value_key; \
642 HSV::rgb_to_hsv(red, \
650 template <typename component_type>
651 void ChromaKeyUnit::process_chromakey(int components,
654 ChromaKeyPackage *pkg)
658 int w = plugin->input->get_w();
660 for (int i = pkg->y1; i < pkg->y2; i++)
662 component_type *row = (component_type *) plugin->input->get_rows ()[i];
664 for (int j = 0; j < w; j++)
668 float r = (float) row[0] / max;
669 float g = (float) row[1] / max;
670 float b = (float) row[2] / max;
674 float av = 1, ah = 1, as = 1, avm = 1;
675 bool has_match = true;
679 /* Convert pixel to RGB float */
683 YUV::yuv_to_rgb_f (r, g, b, y, u - 0.5, v - 0.5);
686 HSV::rgb_to_hsv (r, g, b, h, s, v);
688 // First, test if the hue is in range
691 if(h <= hue_key - tolerance_in * 180.0)
694 if(h >= hue_key + tolerance_in * 180.0)
701 if (ABS (h - hue_key) < tolerance_in * 180)
704 if ((out_slope != 0) && (ABS (h - hue_key) < tolerance * 180))
705 /* we scale alpha between 0 and 1/2 */
706 ah = ABS (h - hue_key) / tolerance / 360;
708 if (ABS (h - hue_key) < tolerance_out * 180)
709 /* we scale alpha between 1/2 and 1 */
710 ah = ABS (h - hue_key) / tolerance_out / 360;
714 // Check if the saturation matches
719 else if (s - sat >= min_s_in)
721 else if ((out_slope != 0) && (s - sat > min_s))
722 as = (s - sat - min_s) / (min_s * 2);
723 else if (s - sat > min_s_out)
724 as = (s - sat - min_s_out) / (min_s_out * 2);
729 // Check if the value is more than the minimun
734 else if (v >= min_v_in)
736 else if ((out_slope != 0) && (v > min_v))
737 av = (v - min_v) / (min_v * 2);
738 else if (v > min_v_out)
739 av = (v - min_v_out) / (min_v_out * 2);
744 // Check if the value is less than the maximum
749 else if (v <= max_v_in)
751 else if ((out_slope != 0) && (v < max_v))
752 avm = (v - max_v) / (max_v * 2);
753 else if (v < max_v_out)
754 avm = (v - max_v_out) / (max_v_out * 2);
759 // If the color is part of the key, update the alpha channel
761 a = MAX (MAX (ah, av), MAX (as, avm));
763 // Spill light processing
764 if ((ABS (h - hue_key) < spill_threshold * 180) ||
765 ((ABS (h - hue_key) > 360)
766 && (ABS (h - hue_key) - 360 < spill_threshold * 180)))
768 s = s * spill_amount * ABS (h - hue_key) / (spill_threshold * 180);
770 HSV::hsv_to_rgb (r, g, b, h, s, v);
777 YUV::rgb_to_yuv_f (r, g, b, y, u, v);
781 row[0] = (component_type) ((float) y * max);
782 row[1] = (component_type) ((float) (u + 0.5) * max);
783 row[2] = (component_type) ((float) (v + 0.5) * max);
790 row[0] = (component_type) ((float) r * max);
791 row[1] = (component_type) ((float) g * max);
792 row[2] = (component_type) ((float) b * max);
799 if (plugin->config.show_mask)
804 row[0] = (component_type) ((float) a * max);
805 row[1] = (component_type) ((float) max / 2);
806 row[2] = (component_type) ((float) max / 2);
810 row[0] = (component_type) ((float) a * max);
811 row[1] = (component_type) ((float) a * max);
812 row[2] = (component_type) ((float) a * max);
816 /* Multiply alpha and put back in frame */
819 row[3] = MIN ((component_type) (a * max), row[3]);
823 row[0] = (component_type) ((float) a * row[0]);
825 (component_type) ((float) a * (row[1] - (max / 2 + 1)) +
828 (component_type) ((float) a * (row[2] - (max / 2 + 1)) +
833 row[0] = (component_type) ((float) a * row[0]);
834 row[1] = (component_type) ((float) a * row[1]);
835 row[2] = (component_type) ((float) a * row[2]);
846 void ChromaKeyUnit::process_package(LoadPackage *package)
848 ChromaKeyPackage *pkg = (ChromaKeyPackage*)package;
851 switch(plugin->input->get_color_model())
854 process_chromakey<float> (3, 1.0, 0, pkg);
857 process_chromakey<float> ( 4, 1.0, 0, pkg);
860 process_chromakey<unsigned char> ( 3, 0xff, 0, pkg);
863 process_chromakey<unsigned char> ( 4, 0xff, 0, pkg);
866 process_chromakey<unsigned char> ( 3, 0xff, 1, pkg);
869 process_chromakey<unsigned char> ( 4, 0xff, 1, pkg);
872 process_chromakey<uint16_t> (3, 0xffff, 1, pkg);
874 case BC_YUVA16161616:
875 process_chromakey<uint16_t> (4, 0xffff, 1, pkg);
885 REGISTER_PLUGIN(ChromaKeyHSV)
889 ChromaKeyHSV::ChromaKeyHSV(PluginServer *server)
890 : PluginVClient(server)
896 ChromaKeyHSV::~ChromaKeyHSV()
899 if(engine) delete engine;
903 int ChromaKeyHSV::process_buffer(VFrame *frame,
904 int64_t start_position,
907 load_configuration();
909 this->output = frame;
917 if(get_use_opengl()) return run_opengl();
920 if(!engine) engine = new ChromaKeyServer(this);
921 engine->process_packages();
926 const char* ChromaKeyHSV::plugin_title() { return _("Chroma key (HSV)"); }
927 int ChromaKeyHSV::is_realtime() { return 1; }
930 LOAD_CONFIGURATION_MACRO(ChromaKeyHSV, ChromaKeyConfig)
933 void ChromaKeyHSV::save_data(KeyFrame * keyframe)
936 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
937 output.tag.set_title("CHROMAKEY_HSV");
938 output.tag.set_property("RED", config.red);
939 output.tag.set_property("GREEN", config.green);
940 output.tag.set_property("BLUE", config.blue);
941 output.tag.set_property("MIN_BRIGHTNESS", config.min_brightness);
942 output.tag.set_property("MAX_BRIGHTNESS", config.max_brightness);
943 output.tag.set_property("SATURATION", config.saturation);
944 output.tag.set_property("MIN_SATURATION", config.min_saturation);
945 output.tag.set_property("TOLERANCE", config.tolerance);
946 output.tag.set_property("IN_SLOPE", config.in_slope);
947 output.tag.set_property("OUT_SLOPE", config.out_slope);
948 output.tag.set_property("ALPHA_OFFSET", config.alpha_offset);
949 output.tag.set_property("SPILL_THRESHOLD", config.spill_threshold);
950 output.tag.set_property("SPILL_AMOUNT", config.spill_amount);
951 output.tag.set_property("SHOW_MASK", config.show_mask);
953 output.tag.set_title("/CHROMAKEY_HSV");
955 output.append_newline();
956 output.terminate_string();
959 void ChromaKeyHSV::read_data(KeyFrame * keyframe)
963 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
965 while( !input.read_tag() ) {
966 if( input.tag.title_is("CHROMAKEY_HSV") ) {
967 config.red = input.tag.get_property("RED", config.red);
968 config.green = input.tag.get_property("GREEN", config.green);
969 config.blue = input.tag.get_property("BLUE", config.blue);
970 config.min_brightness =
971 input.tag.get_property("MIN_BRIGHTNESS", config.min_brightness);
972 config.max_brightness =
973 input.tag.get_property("MAX_BRIGHTNESS", config.max_brightness);
975 input.tag.get_property("SATURATION", config.saturation);
976 config.min_saturation =
977 input.tag.get_property("MIN_SATURATION", config.min_saturation);
979 input.tag.get_property("TOLERANCE", config.tolerance);
981 input.tag.get_property("IN_SLOPE", config.in_slope);
983 input.tag.get_property("OUT_SLOPE", config.out_slope);
984 config.alpha_offset =
985 input.tag.get_property("ALPHA_OFFSET", config.alpha_offset);
986 config.spill_threshold =
987 input.tag.get_property("SPILL_THRESHOLD",
988 config.spill_threshold);
989 config.spill_amount =
990 input.tag.get_property("SPILL_AMOUNT", config.spill_amount);
992 input.tag.get_property("SHOW_MASK", config.show_mask);
998 NEW_WINDOW_MACRO(ChromaKeyHSV, ChromaKeyWindow)
1000 void ChromaKeyHSV::update_gui()
1003 load_configuration();
1004 ChromaKeyWindow *window = (ChromaKeyWindow*)thread->window;
1005 window->lock_window();
1006 window->min_brightness->update(config.min_brightness);
1007 window->max_brightness->update(config.max_brightness);
1008 window->saturation->update(config.saturation);
1009 window->min_saturation->update(config.min_saturation);
1010 window->tolerance->update(config.tolerance);
1011 window->in_slope->update(config.in_slope);
1012 window->out_slope->update(config.out_slope);
1013 window->alpha_offset->update(config.alpha_offset);
1014 window->spill_threshold->update(config.spill_threshold);
1015 window->spill_amount->update(config.spill_amount);
1016 window->show_mask->update(config.show_mask);
1017 window->update_sample();
1018 window->unlock_window();
1025 int ChromaKeyHSV::handle_opengl()
1029 ChromaKeyHSV *plugin = this;
1032 static const char *yuv_shader =
1033 "const vec3 black = vec3(0.0, 0.5, 0.5);\n"
1035 "vec4 yuv_to_rgb(vec4 color)\n"
1037 YUV_TO_RGB_FRAG("color")
1041 "vec4 rgb_to_yuv(vec4 color)\n"
1043 RGB_TO_YUV_FRAG("color")
1047 static const char *rgb_shader =
1048 "const vec3 black = vec3(0.0, 0.0, 0.0);\n"
1050 "vec4 yuv_to_rgb(vec4 color)\n"
1054 "vec4 rgb_to_yuv(vec4 color)\n"
1059 static const char *hsv_shader =
1060 "vec4 rgb_to_hsv(vec4 color)\n"
1062 RGB_TO_HSV_FRAG("color")
1066 "vec4 hsv_to_rgb(vec4 color)\n"
1068 HSV_TO_RGB_FRAG("color")
1073 static const char *show_rgbmask_shader =
1074 "vec4 show_mask(vec4 color, vec4 color2)\n"
1076 " return vec4(1.0, 1.0, 1.0, min(color.a, color2.a));"
1079 static const char *show_yuvmask_shader =
1080 "vec4 show_mask(vec4 color, vec4 color2)\n"
1082 " return vec4(1.0, 0.5, 0.5, min(color.a, color2.a));"
1085 static const char *nomask_shader =
1086 "vec4 show_mask(vec4 color, vec4 color2)\n"
1088 " return vec4(color.rgb, min(color.a, color2.a));"
1091 extern unsigned char _binary_chromakey_sl_start[];
1092 static const char *shader = (char*)_binary_chromakey_sl_start;
1094 get_output()->to_texture();
1095 get_output()->enable_opengl();
1096 get_output()->init_screen();
1098 const char* shader_stack[] = { 0, 0, 0, 0, 0 };
1100 switch(get_output()->get_color_model())
1104 shader_stack[0] = yuv_shader;
1105 shader_stack[1] = hsv_shader;
1106 if(config.show_mask)
1107 shader_stack[2] = show_yuvmask_shader;
1109 shader_stack[2] = nomask_shader;
1110 shader_stack[3] = shader;
1114 shader_stack[0] = rgb_shader;
1115 shader_stack[1] = hsv_shader;
1116 if(config.show_mask)
1117 shader_stack[2] = show_rgbmask_shader;
1119 shader_stack[2] = nomask_shader;
1120 shader_stack[3] = shader;
1125 unsigned int frag = VFrame::make_shader(0,
1135 glUniform1i(glGetUniformLocation(frag, "tex"), 0);
1136 glUniform1f(glGetUniformLocation(frag, "red"), red);
1137 glUniform1f(glGetUniformLocation(frag, "green"), green);
1138 glUniform1f(glGetUniformLocation(frag, "blue"), blue);
1139 glUniform1f(glGetUniformLocation(frag, "in_slope"), in_slope);
1140 glUniform1f(glGetUniformLocation(frag, "out_slope"), out_slope);
1141 glUniform1f(glGetUniformLocation(frag, "tolerance"), tolerance);
1142 glUniform1f(glGetUniformLocation(frag, "tolerance_in"), tolerance_in);
1143 glUniform1f(glGetUniformLocation(frag, "tolerance_out"), tolerance_out);
1144 glUniform1f(glGetUniformLocation(frag, "sat"), sat);
1145 glUniform1f(glGetUniformLocation(frag, "min_s"), min_s);
1146 glUniform1f(glGetUniformLocation(frag, "min_s_in"), min_s_in);
1147 glUniform1f(glGetUniformLocation(frag, "min_s_out"), min_s_out);
1148 glUniform1f(glGetUniformLocation(frag, "min_v"), min_v);
1149 glUniform1f(glGetUniformLocation(frag, "min_v_in"), min_v_in);
1150 glUniform1f(glGetUniformLocation(frag, "min_v_out"), min_v_out);
1151 glUniform1f(glGetUniformLocation(frag, "max_v"), max_v);
1152 glUniform1f(glGetUniformLocation(frag, "max_v_in"), max_v_in);
1153 glUniform1f(glGetUniformLocation(frag, "max_v_out"), max_v_out);
1154 glUniform1f(glGetUniformLocation(frag, "spill_threshold"), spill_threshold);
1155 glUniform1f(glGetUniformLocation(frag, "spill_amount"), spill_amount);
1156 glUniform1f(glGetUniformLocation(frag, "alpha_offset"), alpha_offset);
1157 glUniform1f(glGetUniformLocation(frag, "hue_key"), hue_key);
1158 glUniform1f(glGetUniformLocation(frag, "saturation_key"), saturation_key);
1159 glUniform1f(glGetUniformLocation(frag, "value_key"), value_key);
1163 get_output()->bind_texture(0);
1164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1167 if(BC_CModels::components(get_output()->get_color_model()) == 3)
1170 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1171 get_output()->clear_pbuffer();
1173 get_output()->draw_texture();
1176 get_output()->set_opengl_state(VFrame::SCREEN);
1177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1179 glDisable(GL_BLEND);