4 * Copyright (C) 2008 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
23 #include "bcdisplayinfo.h"
27 #include "huesaturation.h"
30 #include "loadbalance.h"
32 #include "playback3d.h"
33 #include "pluginvclient.h"
42 REGISTER_PLUGIN(HueEffect)
48 HueConfig::HueConfig()
53 void HueConfig::reset(int clear)
56 case RESET_HUV : hue = 0;
58 case RESET_SAT : saturation = 0;
60 case RESET_VAL : value = 0;
64 hue = saturation = value = 0;
69 void HueConfig::copy_from(HueConfig &src)
72 saturation = src.saturation;
75 int HueConfig::equivalent(HueConfig &src)
77 return EQUIV(hue, src.hue) &&
78 EQUIV(saturation, src.saturation) &&
79 EQUIV(value, src.value);
81 void HueConfig::interpolate(HueConfig &prev,
87 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
88 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
90 this->hue = prev.hue * prev_scale + next.hue * next_scale;
91 this->saturation = prev.saturation * prev_scale + next.saturation * next_scale;
92 this->value = prev.value * prev_scale + next.value * next_scale;
102 HueSlider::HueSlider(HueEffect *plugin, int x, int y, int w)
112 this->plugin = plugin;
114 int HueSlider::handle_event()
116 plugin->config.hue = get_value();
117 plugin->send_configure_change();
127 SaturationSlider::SaturationSlider(HueEffect *plugin, int x, int y, int w)
133 (float)MINSATURATION,
134 (float)MAXSATURATION,
135 plugin->config.saturation)
137 this->plugin = plugin;
139 int SaturationSlider::handle_event()
141 plugin->config.saturation = get_value();
142 plugin->send_configure_change();
146 char* SaturationSlider::get_caption()
148 float fraction = ((float)plugin->config.saturation - MINSATURATION) /
150 sprintf(string, "%0.4f", fraction);
160 ValueSlider::ValueSlider(HueEffect *plugin, int x, int y, int w)
168 plugin->config.value)
170 this->plugin = plugin;
172 int ValueSlider::handle_event()
174 plugin->config.value = get_value();
175 plugin->send_configure_change();
179 char* ValueSlider::get_caption()
181 float fraction = ((float)plugin->config.value - MINVALUE) / MAXVALUE;
182 sprintf(string, "%0.4f", fraction);
187 HueReset::HueReset(HueEffect *plugin, HueWindow *gui, int x, int y)
188 : BC_GenericButton(x, y, _("Reset"))
190 this->plugin = plugin;
193 HueReset::~HueReset()
196 int HueReset::handle_event()
198 plugin->config.reset(RESET_ALL); // clear=0 ==> reset all
199 gui->update_gui(RESET_ALL);
200 plugin->send_configure_change();
205 HueSliderClr::HueSliderClr(HueEffect *plugin, HueWindow *gui, int x, int y, int w, int clear)
206 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
208 this->plugin = plugin;
212 HueSliderClr::~HueSliderClr()
215 int HueSliderClr::handle_event()
217 // clear==1 ==> Hue slider
218 // clear==2 ==> Saturation slider
219 // clear==3 ==> Value slider
220 plugin->config.reset(clear);
221 gui->update_gui(clear);
222 plugin->send_configure_change();
229 HueWindow::HueWindow(HueEffect *plugin)
230 : PluginClientWindow(plugin, 370, 140, 370, 140, 0)
232 this->plugin = plugin;
234 void HueWindow::create_objects()
236 int x = 10, y = 10, x1 = 100;
237 int x2 = 0; int clrBtn_w = 50;
239 add_subwindow(new BC_Title(x, y, _("Hue:")));
240 add_subwindow(hue = new HueSlider(plugin, x1, y, 200));
241 x2 = x1 + hue->get_w() + 10;
242 add_subwindow(hueClr = new HueSliderClr(plugin, this, x2, y, clrBtn_w, RESET_HUV));
245 add_subwindow(new BC_Title(x, y, _("Saturation:")));
246 add_subwindow(saturation = new SaturationSlider(plugin, x1, y, 200));
247 add_subwindow(satClr = new HueSliderClr(plugin, this, x2, y, clrBtn_w, RESET_SAT));
250 add_subwindow(new BC_Title(x, y, _("Value:")));
251 add_subwindow(value = new ValueSlider(plugin, x1, y, 200));
252 add_subwindow(valClr = new HueSliderClr(plugin, this, x2, y, clrBtn_w, RESET_VAL));
255 add_subwindow(reset = new HueReset(plugin, this, x, y));
262 void HueWindow::update_gui(int clear)
265 case RESET_HUV : hue->update(plugin->config.hue);
267 case RESET_SAT : saturation->update(plugin->config.saturation);
269 case RESET_VAL : value->update(plugin->config.value);
273 hue->update(plugin->config.hue);
274 saturation->update(plugin->config.saturation);
275 value->update(plugin->config.value);
287 HueEngine::HueEngine(HueEffect *plugin, int cpus)
288 : LoadServer(cpus, cpus)
290 this->plugin = plugin;
292 void HueEngine::init_packages()
294 for(int i = 0; i < LoadServer::get_total_packages(); i++)
296 HuePackage *pkg = (HuePackage*)get_package(i);
297 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
298 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
301 LoadClient* HueEngine::new_client()
303 return new HueUnit(plugin, this);
305 LoadPackage* HueEngine::new_package()
307 return new HuePackage;
317 HuePackage::HuePackage()
322 HueUnit::HueUnit(HueEffect *plugin, HueEngine *server)
325 this->plugin = plugin;
334 #define HUESATURATION(type, max, components, use_yuv) \
336 float h_offset = plugin->config.hue; \
337 float s_offset = ((float)plugin->config.saturation - MINSATURATION) / MAXSATURATION; \
338 float v_offset = ((float)plugin->config.value - MINVALUE) / MAXVALUE; \
339 for(int i = pkg->row1; i < pkg->row2; i++) \
341 type* in_row = (type*)plugin->input->get_rows()[i]; \
342 type* out_row = (type*)plugin->output->get_rows()[i]; \
344 for(int j = 0; j < w; j++) \
353 y = (int)in_row[0]; \
354 u = (int)in_row[1]; \
355 v = (int)in_row[2]; \
357 YUV::yuv.yuv_to_rgb_16(r_i, g_i, b_i, y, u, v); \
359 YUV::yuv.yuv_to_rgb_8(r_i, g_i, b_i, y, u, v); \
360 HSV::rgb_to_hsv((float)r_i / max, \
369 r = (float)in_row[0] / max; \
370 g = (float)in_row[1] / max; \
371 b = (float)in_row[2] / max; \
372 HSV::rgb_to_hsv(r, g, b, h, s, va); \
380 if(h >= 360) h -= 360; \
381 if(h < 0) h += 360; \
382 if(sizeof(type) < 4) \
392 HSV::hsv_to_yuv(y, u, v, h, s, va, max); \
399 HSV::hsv_to_rgb(r, g, b, h, s, va); \
400 if(sizeof(type) < 4) \
405 out_row[0] = (type)CLIP(r, 0, max); \
406 out_row[1] = (type)CLIP(g, 0, max); \
407 out_row[2] = (type)CLIP(b, 0, max); \
411 out_row[0] = (type)r; \
412 out_row[1] = (type)g; \
413 out_row[2] = (type)b; \
417 in_row += components; \
418 out_row += components; \
424 void HueUnit::process_package(LoadPackage *package)
426 HuePackage *pkg = (HuePackage*)package;
427 int w = plugin->input->get_w();
429 switch(plugin->input->get_color_model())
432 HUESATURATION(unsigned char, 0xff, 3, 0)
436 HUESATURATION(float, 1, 3, 0)
440 HUESATURATION(unsigned char, 0xff, 3, 1)
444 HUESATURATION(uint16_t, 0xffff, 3, 0)
448 HUESATURATION(uint16_t, 0xffff, 3, 1)
452 HUESATURATION(float, 1, 4, 0)
456 HUESATURATION(unsigned char, 0xff, 4, 0)
460 HUESATURATION(unsigned char, 0xff, 4, 1)
463 case BC_RGBA16161616:
464 HUESATURATION(uint16_t, 0xffff, 4, 0)
467 case BC_YUVA16161616:
468 HUESATURATION(uint16_t, 0xffff, 4, 1)
479 HueEffect::HueEffect(PluginServer *server)
480 : PluginVClient(server)
485 HueEffect::~HueEffect()
488 if(engine) delete engine;
491 int HueEffect::process_buffer(VFrame *frame,
492 int64_t start_position,
495 load_configuration();
505 this->output = frame;
506 if(EQUIV(config.hue, 0) && EQUIV(config.saturation, 0) && EQUIV(config.value, 0))
518 if(!engine) engine = new HueEngine(this, PluginClient::smp + 1);
520 engine->process_packages();
525 const char* HueEffect::plugin_title() { return N_("Hue saturation"); }
526 int HueEffect::is_realtime() { return 1; }
528 NEW_WINDOW_MACRO(HueEffect, HueWindow)
529 LOAD_CONFIGURATION_MACRO(HueEffect, HueConfig)
532 void HueEffect::save_data(KeyFrame *keyframe)
535 output.set_shared_output(keyframe->xbuf);
536 output.tag.set_title("HUESATURATION");
537 output.tag.set_property("HUE", config.hue);
538 output.tag.set_property("SATURATION", config.saturation);
539 output.tag.set_property("VALUE", config.value);
541 output.tag.set_title("/HUESATURATION");
543 output.append_newline();
544 output.terminate_string();
546 void HueEffect::read_data(KeyFrame *keyframe)
549 input.set_shared_input(keyframe->xbuf);
550 while(!input.read_tag())
552 if(input.tag.title_is("HUESATURATION"))
554 config.hue = input.tag.get_property("HUE", config.hue);
555 config.saturation = input.tag.get_property("SATURATION", config.saturation);
556 config.value = input.tag.get_property("VALUE", config.value);
560 void HueEffect::update_gui()
564 ((HueWindow*)thread->window)->lock_window();
565 load_configuration();
566 ((HueWindow*)thread->window)->hue->update(config.hue);
567 ((HueWindow*)thread->window)->saturation->update(config.saturation);
568 ((HueWindow*)thread->window)->value->update(config.value);
569 ((HueWindow*)thread->window)->unlock_window();
573 int HueEffect::handle_opengl()
576 const char *yuv_saturation_frag =
577 "uniform sampler2D tex;\n"
578 "uniform float s_offset;\n"
579 "uniform float v_offset;\n"
582 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
583 " pixel.r *= v_offset;\n"
584 " pixel.gb -= vec2(0.5, 0.5);\n"
585 " pixel.g *= s_offset;\n"
586 " pixel.b *= s_offset;\n"
587 " pixel.gb += vec2(0.5, 0.5);\n"
588 " gl_FragColor = pixel;\n"
592 const char *yuv_frag =
593 "uniform sampler2D tex;\n"
594 "uniform float h_offset;\n"
595 "uniform float s_offset;\n"
596 "uniform float v_offset;\n"
599 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
600 YUV_TO_RGB_FRAG("pixel")
601 RGB_TO_HSV_FRAG("pixel")
602 " pixel.r += h_offset;\n"
603 " pixel.g *= s_offset;\n"
604 " pixel.b *= v_offset;\n"
605 " if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
606 " if(pixel.r < 0.0) pixel.r += 360.0;\n"
607 HSV_TO_RGB_FRAG("pixel")
608 RGB_TO_YUV_FRAG("pixel")
609 " gl_FragColor = pixel;\n"
612 const char *rgb_frag =
613 "uniform sampler2D tex;\n"
614 "uniform float h_offset;\n"
615 "uniform float s_offset;\n"
616 "uniform float v_offset;\n"
619 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
620 RGB_TO_HSV_FRAG("pixel")
621 " pixel.r += h_offset;\n"
622 " pixel.g *= s_offset;\n"
623 " pixel.b *= v_offset;\n"
624 " if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
625 " if(pixel.r < 0.0) pixel.r += 360.0;\n"
626 HSV_TO_RGB_FRAG("pixel")
627 " gl_FragColor = pixel;\n"
631 get_output()->to_texture();
632 get_output()->enable_opengl();
634 const char *shader_stack[16];
635 memset(shader_stack,0, sizeof(shader_stack));
636 int current_shader = 0;
638 int need_color_matrix = BC_CModels::is_yuv(get_output()->get_color_model()) ? 1 : 0;
639 if( need_color_matrix ) shader_stack[current_shader++] = bc_gl_colors;
640 shader_stack[current_shader++] = !need_color_matrix ? rgb_frag :
641 EQUIV(config.hue, 0) ? yuv_saturation_frag: yuv_frag ;
643 shader_stack[current_shader] = 0;
644 unsigned int shader = VFrame::make_shader(shader_stack);
646 glUseProgram(shader);
647 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
648 glUniform1f(glGetUniformLocation(shader, "h_offset"), config.hue);
649 glUniform1f(glGetUniformLocation(shader, "s_offset"),
650 ((float)config.saturation - MINSATURATION) / MAXSATURATION);
651 glUniform1f(glGetUniformLocation(shader, "v_offset"),
652 ((float)config.value - MINVALUE) / MAXVALUE);
653 if( need_color_matrix ) BC_GL_COLORS(shader);
656 get_output()->init_screen();
657 get_output()->bind_texture(0);
658 get_output()->draw_texture();
660 get_output()->set_opengl_state(VFrame::SCREEN);