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
22 #include "bcdisplayinfo.h"
29 #include "pluginvclient.h"
39 #define RESET_Y_SLIDER 1
40 #define RESET_U_SLIDER 2
41 #define RESET_V_SLIDER 3
55 void reset(int clear);
57 void copy_from(YUVConfig &src);
58 int equivalent(YUVConfig &src);
59 void interpolate(YUVConfig &prev,
71 class YUVFText : public BC_TumbleTextBox
74 YUVFText(YUVWindow *window, YUVEffect *plugin,
75 YUVFSlider *slider, float *output, int x, int y, float min, float max);
86 class YUVFSlider : public BC_FSlider
89 YUVFSlider(YUVEffect *plugin, YUVFText *text, float *output, int x, int y);
98 class YUVReset : public BC_GenericButton
101 YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y);
108 class YUVClr : public BC_Button
111 YUVClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int clear);
119 class YUVWindow : public PluginClientWindow
122 YUVWindow(YUVEffect *plugin);
123 void create_objects();
124 void update_gui(int clear);
127 YUVFSlider *y_slider;
131 YUVFSlider *u_slider;
135 YUVFSlider *v_slider;
144 class YUVEffect : public PluginVClient
147 YUVEffect(PluginServer *server);
151 PLUGIN_CLASS_MEMBERS(YUVConfig)
152 int process_realtime(VFrame *input, VFrame *output);
154 void save_data(KeyFrame *keyframe);
155 void read_data(KeyFrame *keyframe);
163 REGISTER_PLUGIN(YUVEffect)
171 YUVConfig::YUVConfig()
176 void YUVConfig::reset(int clear)
179 case RESET_Y_SLIDER : y = 0;
181 case RESET_U_SLIDER : u = 0;
183 case RESET_V_SLIDER : v = 0;
192 void YUVConfig::copy_from(YUVConfig &src)
199 int YUVConfig::equivalent(YUVConfig &src)
201 return EQUIV(y, src.y) && EQUIV(u, src.u) && EQUIV(v, src.v);
204 void YUVConfig::interpolate(YUVConfig &prev,
210 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
211 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
213 y = prev.y * prev_scale + next.y * next_scale;
214 u = prev.u * prev_scale + next.u * next_scale;
215 v = prev.v * prev_scale + next.v * next_scale;
225 YUVFText::YUVFText(YUVWindow *window, YUVEffect *plugin,
226 YUVFSlider *slider, float *output, int x, int y, float min, float max)
227 : BC_TumbleTextBox(window, *output,
228 min, max, x, y, xS(60), 2)
230 this->window = window;
231 this->plugin = plugin;
232 this->output = output;
233 this->slider = slider;
239 YUVFText::~YUVFText()
243 int YUVFText::handle_event()
245 *output = atof(get_text());
246 if(*output > max) *output = max;
247 if(*output < min) *output = min;
248 slider->update(*output);
249 plugin->send_configure_change();
254 YUVFSlider::YUVFSlider(YUVEffect *plugin, YUVFText *text, float *output, int x, int y)
255 : BC_FSlider(x, y, 0, xS(200), xS(200), -MAXVALUE, MAXVALUE, *output)
257 this->plugin = plugin;
258 this->output = output;
260 enable_show_value(0); // Hide caption
263 YUVFSlider::~YUVFSlider()
267 int YUVFSlider::handle_event()
269 *output = get_value();
270 text->update(*output);
271 plugin->send_configure_change();
276 YUVReset::YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y)
277 : BC_GenericButton(x, y, _("Reset"))
279 this->plugin = plugin;
280 this->window = window;
282 YUVReset::~YUVReset()
285 int YUVReset::handle_event()
287 plugin->config.reset(RESET_ALL);
288 window->update_gui(RESET_ALL);
289 plugin->send_configure_change();
294 YUVClr::YUVClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int clear)
295 : BC_Button(x, y, plugin->get_theme()->get_image_set("reset_button"))
297 this->plugin = plugin;
298 this->window = window;
304 int YUVClr::handle_event()
306 // clear==1 ==> Y slider
307 // clear==2 ==> U slider
308 // clear==3 ==> V slider
309 plugin->config.reset(clear);
310 window->update_gui(clear);
311 plugin->send_configure_change();
316 YUVWindow::YUVWindow(YUVEffect *plugin)
317 : PluginClientWindow(plugin, xS(420), yS(160), xS(420), yS(160), 0)
319 this->plugin = plugin;
322 void YUVWindow::create_objects()
325 int ys10 = yS(10), ys30 = yS(30), ys40 = yS(40);
326 int x = xs10, y = ys10;
327 int x2 = xS(80), x3 = xS(180);
328 int clr_x = get_w()-x - xS(22); // note: clrBtn_w = 22
333 add_subwindow(new BC_Title(x, y, _("Y:")));
334 y_text = new YUVFText(this, plugin,
335 0, &plugin->config.y, (x + x2), y, -MAXVALUE, MAXVALUE);
336 y_text->create_objects();
337 y_slider = new YUVFSlider(plugin, y_text, &plugin->config.y, x3, y);
338 add_subwindow(y_slider);
339 y_text->slider = y_slider;
340 clr_x = x3 + y_slider->get_w() + x;
341 add_subwindow(y_Clr = new YUVClr(plugin, this, clr_x, y, RESET_Y_SLIDER));
344 add_subwindow(new BC_Title(x, y, _("U:")));
345 u_text = new YUVFText(this, plugin,
346 0, &plugin->config.u, (x + x2), y, -MAXVALUE, MAXVALUE);
347 u_text->create_objects();
348 u_slider = new YUVFSlider(plugin, u_text, &plugin->config.u, x3, y);
349 add_subwindow(u_slider);
350 u_text->slider = u_slider;
351 add_subwindow(u_Clr = new YUVClr(plugin, this, clr_x, y, RESET_U_SLIDER));
354 add_subwindow(new BC_Title(x, y, _("V:")));
355 v_text = new YUVFText(this, plugin,
356 0, &plugin->config.v, (x + x2), y, -MAXVALUE, MAXVALUE);
357 v_text->create_objects();
358 v_slider = new YUVFSlider(plugin, v_text, &plugin->config.v, x3, y);
359 add_subwindow(v_slider);
360 v_text->slider = v_slider;
361 add_subwindow(v_Clr = new YUVClr(plugin, this, clr_x, y, RESET_V_SLIDER));
365 add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
367 add_subwindow(reset = new YUVReset(plugin, this, x, y));
375 void YUVWindow::update_gui(int clear)
378 case RESET_Y_SLIDER :
379 y_text->update(plugin->config.y);
380 y_slider->update(plugin->config.y);
382 case RESET_U_SLIDER :
383 u_text->update(plugin->config.u);
384 u_slider->update(plugin->config.u);
386 case RESET_V_SLIDER :
387 v_text->update(plugin->config.v);
388 v_slider->update(plugin->config.v);
392 y_text->update(plugin->config.y);
393 y_slider->update(plugin->config.y);
394 u_text->update(plugin->config.u);
395 u_slider->update(plugin->config.u);
396 v_text->update(plugin->config.v);
397 v_slider->update(plugin->config.v);
407 YUVEffect::YUVEffect(PluginServer *server)
408 : PluginVClient(server)
412 YUVEffect::~YUVEffect()
417 const char* YUVEffect::plugin_title() { return N_("YUV"); }
418 int YUVEffect::is_realtime() { return 1; }
421 NEW_WINDOW_MACRO(YUVEffect, YUVWindow)
422 LOAD_CONFIGURATION_MACRO(YUVEffect, YUVConfig)
424 void YUVEffect::update_gui()
428 thread->window->lock_window();
429 load_configuration();
430 ((YUVWindow*)thread->window)->y_text->update(config.y);
431 ((YUVWindow*)thread->window)->y_slider->update(config.y);
432 ((YUVWindow*)thread->window)->u_text->update(config.u);
433 ((YUVWindow*)thread->window)->u_slider->update(config.u);
434 ((YUVWindow*)thread->window)->v_text->update(config.v);
435 ((YUVWindow*)thread->window)->v_slider->update(config.v);
436 thread->window->unlock_window();
440 void YUVEffect::save_data(KeyFrame *keyframe)
443 output.set_shared_output(keyframe->xbuf);
444 output.tag.set_title("YUV");
445 output.tag.set_property("Y", config.y);
446 output.tag.set_property("U", config.u);
447 output.tag.set_property("V", config.v);
449 output.tag.set_title("/YUV");
451 output.append_newline();
452 output.terminate_string();
455 void YUVEffect::read_data(KeyFrame *keyframe)
458 input.set_shared_input(keyframe->xbuf);
459 while(!input.read_tag())
461 if(input.tag.title_is("YUV"))
463 config.y = input.tag.get_property("Y", config.y);
464 config.u = input.tag.get_property("U", config.u);
465 config.v = input.tag.get_property("V", config.v);
471 #define YUV_MACRO(type, temp_type, max, components, use_yuv) \
473 for(int i = 0; i < input->get_h(); i++) \
475 type *in_row = (type*)input->get_rows()[i]; \
476 type *out_row = (type*)output->get_rows()[i]; \
477 const float round = (sizeof(type) == 4) ? 0.0 : 0.5; \
479 for(int j = 0; j < w; j++) \
483 int y = (int)((float)in_row[0] * y_scale + round); \
484 int u = (int)((float)(in_row[1] - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
485 int v = (int)((float)(in_row[2] - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
486 out_row[0] = CLIP(y, 0, max); \
487 out_row[1] = CLIP(u, 0, max); \
488 out_row[2] = CLIP(v, 0, max); \
492 temp_type y, u, v, r, g, b; \
493 if(sizeof(type) == 4) \
495 YUV::yuv.rgb_to_yuv_f(in_row[0], in_row[1], in_row[2], y, u, v); \
498 if(sizeof(type) == 2) \
500 YUV::yuv.rgb_to_yuv_16(in_row[0], in_row[1], in_row[2], y, u, v); \
504 YUV::yuv.rgb_to_yuv_8(in_row[0], in_row[1], in_row[2], y, u, v); \
507 if(sizeof(type) < 4) \
513 y = temp_type((float)y * y_scale + round); \
514 u = temp_type((float)(u - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
515 v = temp_type((float)(v - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
523 y = temp_type((float)y * y_scale + round); \
524 u = temp_type((float)u * u_scale + round); \
525 v = temp_type((float)v * v_scale + round); \
528 if(sizeof(type) == 4) \
529 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v); \
531 if(sizeof(type) == 2) \
532 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
534 YUV::yuv.yuv_to_rgb_8(r, g, b, y, u, v); \
541 in_row += components; \
542 out_row += components; \
547 int YUVEffect::process_realtime(VFrame *input, VFrame *output)
549 load_configuration();
551 if(EQUIV(config.y, 0) && EQUIV(config.u, 0) && EQUIV(config.v, 0))
553 if(input->get_rows()[0] != output->get_rows()[0])
554 output->copy_from(input);
558 int w = input->get_w();
560 float y_scale = (float)(config.y + MAXVALUE) / MAXVALUE;
561 float u_scale = (float)(config.u + MAXVALUE) / MAXVALUE;
562 float v_scale = (float)(config.v + MAXVALUE) / MAXVALUE;
564 if(u_scale > 1) u_scale = 1 + (u_scale - 1) * 4;
565 if(v_scale > 1) v_scale = 1 + (v_scale - 1) * 4;
567 switch(input->get_color_model())
570 YUV_MACRO(float, float, 1, 3, 0)
574 YUV_MACRO(unsigned char, int, 0xff, 3, 0)
578 YUV_MACRO(unsigned char, int, 0xff, 3, 1)
582 YUV_MACRO(uint16_t, int, 0xffff, 3, 0)
586 YUV_MACRO(uint16_t, int, 0xffff, 3, 1)
590 YUV_MACRO(float, float, 1, 4, 0)
594 YUV_MACRO(unsigned char, int, 0xff, 4, 0)
598 YUV_MACRO(unsigned char, int, 0xff, 4, 1)
601 case BC_RGBA16161616:
602 YUV_MACRO(uint16_t, int, 0xffff, 4, 0)
605 case BC_YUVA16161616:
606 YUV_MACRO(uint16_t, int, 0xffff, 4, 1)