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"
37 #define RESET_Y_SLIDER 1
38 #define RESET_U_SLIDER 2
39 #define RESET_V_SLIDER 3
51 void reset(int clear);
53 void copy_from(YUVConfig &src);
54 int equivalent(YUVConfig &src);
55 void interpolate(YUVConfig &prev,
64 class YUVLevel : public BC_FSlider
67 YUVLevel(YUVEffect *plugin, float *output, int x, int y);
73 class YUVReset : public BC_GenericButton
76 YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y);
83 class YUVSliderClr : public BC_Button
86 YUVSliderClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int w, int clear);
94 class YUVWindow : public PluginClientWindow
97 YUVWindow(YUVEffect *plugin);
98 void create_objects();
99 void update_gui(int clear);
103 YUVSliderClr *yClr, *uClr, *vClr;
108 class YUVEffect : public PluginVClient
111 YUVEffect(PluginServer *server);
115 PLUGIN_CLASS_MEMBERS(YUVConfig)
116 int process_realtime(VFrame *input, VFrame *output);
118 void save_data(KeyFrame *keyframe);
119 void read_data(KeyFrame *keyframe);
127 REGISTER_PLUGIN(YUVEffect)
135 YUVConfig::YUVConfig()
140 void YUVConfig::reset(int clear)
143 case RESET_Y_SLIDER : y = 0;
145 case RESET_U_SLIDER : u = 0;
147 case RESET_V_SLIDER : v = 0;
156 void YUVConfig::copy_from(YUVConfig &src)
163 int YUVConfig::equivalent(YUVConfig &src)
165 return EQUIV(y, src.y) && EQUIV(u, src.u) && EQUIV(v, src.v);
168 void YUVConfig::interpolate(YUVConfig &prev,
174 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
175 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
177 y = prev.y * prev_scale + next.y * next_scale;
178 u = prev.u * prev_scale + next.u * next_scale;
179 v = prev.v * prev_scale + next.v * next_scale;
189 YUVLevel::YUVLevel(YUVEffect *plugin, float *output, int x, int y)
199 this->plugin = plugin;
200 this->output = output;
203 int YUVLevel::handle_event()
205 *output = get_value();
206 plugin->send_configure_change();
211 YUVReset::YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y)
212 : BC_GenericButton(x, y, _("Reset"))
214 this->plugin = plugin;
215 this->window = window;
217 YUVReset::~YUVReset()
220 int YUVReset::handle_event()
222 plugin->config.reset(RESET_ALL);
223 window->update_gui(RESET_ALL);
224 plugin->send_configure_change();
229 YUVSliderClr::YUVSliderClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int w, int clear)
230 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
232 this->plugin = plugin;
233 this->window = window;
236 YUVSliderClr::~YUVSliderClr()
239 int YUVSliderClr::handle_event()
241 // clear==1 ==> Y slider
242 // clear==2 ==> U slider
243 // clear==3 ==> V slider
244 plugin->config.reset(clear);
245 window->update_gui(clear);
246 plugin->send_configure_change();
251 YUVWindow::YUVWindow(YUVEffect *plugin)
252 : PluginClientWindow(plugin, xS(310), yS(135), xS(310), yS(135), 0)
254 this->plugin = plugin;
257 void YUVWindow::create_objects()
259 int xs10 = xS(10), xs40 = xS(40), xs50 = xS(50);
260 int ys10 = yS(10), ys30 = yS(30), ys35 = yS(35);
261 int x = xs10, y = ys10, x1 = xs40;
262 int x2 = 0; int clrBtn_w = xs50;
264 add_subwindow(new BC_Title(x, y, _("Y:")));
265 add_subwindow(this->y = new YUVLevel(plugin, &plugin->config.y, x1, y));
266 x2 = x1 + this->y->get_w() + xs10;
267 add_subwindow(yClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_Y_SLIDER));
270 add_subwindow(new BC_Title(x, y, _("U:")));
271 add_subwindow(u = new YUVLevel(plugin, &plugin->config.u, x1, y));
272 add_subwindow(uClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_U_SLIDER));
275 add_subwindow(new BC_Title(x, y, _("V:")));
276 add_subwindow(v = new YUVLevel(plugin, &plugin->config.v, x1, y));
277 add_subwindow(vClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_V_SLIDER));
280 add_subwindow(reset = new YUVReset(plugin, this, x, y));
288 void YUVWindow::update_gui(int clear)
291 case RESET_Y_SLIDER : this->y->update(plugin->config.y);
293 case RESET_U_SLIDER : u->update(plugin->config.u);
295 case RESET_V_SLIDER : v->update(plugin->config.v);
299 this->y->update(plugin->config.y);
300 u->update(plugin->config.u);
301 v->update(plugin->config.v);
311 YUVEffect::YUVEffect(PluginServer *server)
312 : PluginVClient(server)
316 YUVEffect::~YUVEffect()
321 const char* YUVEffect::plugin_title() { return N_("YUV"); }
322 int YUVEffect::is_realtime() { return 1; }
325 NEW_WINDOW_MACRO(YUVEffect, YUVWindow)
326 LOAD_CONFIGURATION_MACRO(YUVEffect, YUVConfig)
328 void YUVEffect::update_gui()
332 thread->window->lock_window();
333 load_configuration();
334 ((YUVWindow*)thread->window)->y->update(config.y);
335 ((YUVWindow*)thread->window)->u->update(config.u);
336 ((YUVWindow*)thread->window)->v->update(config.v);
337 thread->window->unlock_window();
341 void YUVEffect::save_data(KeyFrame *keyframe)
344 output.set_shared_output(keyframe->xbuf);
345 output.tag.set_title("YUV");
346 output.tag.set_property("Y", config.y);
347 output.tag.set_property("U", config.u);
348 output.tag.set_property("V", config.v);
350 output.tag.set_title("/YUV");
352 output.append_newline();
353 output.terminate_string();
356 void YUVEffect::read_data(KeyFrame *keyframe)
359 input.set_shared_input(keyframe->xbuf);
360 while(!input.read_tag())
362 if(input.tag.title_is("YUV"))
364 config.y = input.tag.get_property("Y", config.y);
365 config.u = input.tag.get_property("U", config.u);
366 config.v = input.tag.get_property("V", config.v);
372 #define YUV_MACRO(type, temp_type, max, components, use_yuv) \
374 for(int i = 0; i < input->get_h(); i++) \
376 type *in_row = (type*)input->get_rows()[i]; \
377 type *out_row = (type*)output->get_rows()[i]; \
378 const float round = (sizeof(type) == 4) ? 0.0 : 0.5; \
380 for(int j = 0; j < w; j++) \
384 int y = (int)((float)in_row[0] * y_scale + round); \
385 int u = (int)((float)(in_row[1] - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
386 int v = (int)((float)(in_row[2] - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
387 out_row[0] = CLIP(y, 0, max); \
388 out_row[1] = CLIP(u, 0, max); \
389 out_row[2] = CLIP(v, 0, max); \
393 temp_type y, u, v, r, g, b; \
394 if(sizeof(type) == 4) \
396 YUV::yuv.rgb_to_yuv_f(in_row[0], in_row[1], in_row[2], y, u, v); \
399 if(sizeof(type) == 2) \
401 YUV::yuv.rgb_to_yuv_16(in_row[0], in_row[1], in_row[2], y, u, v); \
405 YUV::yuv.rgb_to_yuv_8(in_row[0], in_row[1], in_row[2], y, u, v); \
408 if(sizeof(type) < 4) \
414 y = temp_type((float)y * y_scale + round); \
415 u = temp_type((float)(u - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
416 v = temp_type((float)(v - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
424 y = temp_type((float)y * y_scale + round); \
425 u = temp_type((float)u * u_scale + round); \
426 v = temp_type((float)v * v_scale + round); \
429 if(sizeof(type) == 4) \
430 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v); \
432 if(sizeof(type) == 2) \
433 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
435 YUV::yuv.yuv_to_rgb_8(r, g, b, y, u, v); \
442 in_row += components; \
443 out_row += components; \
448 int YUVEffect::process_realtime(VFrame *input, VFrame *output)
450 load_configuration();
452 if(EQUIV(config.y, 0) && EQUIV(config.u, 0) && EQUIV(config.v, 0))
454 if(input->get_rows()[0] != output->get_rows()[0])
455 output->copy_from(input);
459 int w = input->get_w();
461 float y_scale = (float)(config.y + MAXVALUE) / MAXVALUE;
462 float u_scale = (float)(config.u + MAXVALUE) / MAXVALUE;
463 float v_scale = (float)(config.v + MAXVALUE) / MAXVALUE;
465 if(u_scale > 1) u_scale = 1 + (u_scale - 1) * 4;
466 if(v_scale > 1) v_scale = 1 + (v_scale - 1) * 4;
468 switch(input->get_color_model())
471 YUV_MACRO(float, float, 1, 3, 0)
475 YUV_MACRO(unsigned char, int, 0xff, 3, 0)
479 YUV_MACRO(unsigned char, int, 0xff, 3, 1)
483 YUV_MACRO(uint16_t, int, 0xffff, 3, 0)
487 YUV_MACRO(uint16_t, int, 0xffff, 3, 1)
491 YUV_MACRO(float, float, 1, 4, 0)
495 YUV_MACRO(unsigned char, int, 0xff, 4, 0)
499 YUV_MACRO(unsigned char, int, 0xff, 4, 1)
502 case BC_RGBA16161616:
503 YUV_MACRO(uint16_t, int, 0xffff, 4, 0)
506 case BC_YUVA16161616:
507 YUV_MACRO(uint16_t, int, 0xffff, 4, 1)