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 "radialblur.h"
27 REGISTER_PLUGIN(RadialBlurMain)
31 RadialBlurConfig::RadialBlurConfig()
33 reset(RESET_DEFAULT_SETTINGS);
37 void RadialBlurConfig::reset(int clear)
50 case RESET_XSLIDER : x = 50;
52 case RESET_YSLIDER : y = 50;
54 case RESET_ANGLE : angle = 0;
56 case RESET_STEPS : steps = 1;
58 case RESET_DEFAULT_SETTINGS :
72 int RadialBlurConfig::equivalent(RadialBlurConfig &that)
75 angle == that.angle &&
78 steps == that.steps &&
85 void RadialBlurConfig::copy_from(RadialBlurConfig &that)
97 void RadialBlurConfig::interpolate(RadialBlurConfig &prev,
98 RadialBlurConfig &next,
103 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
104 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
105 this->x = (int)(prev.x * prev_scale + next.x * next_scale + 0.5);
106 this->y = (int)(prev.y * prev_scale + next.y * next_scale + 0.5);
107 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
108 this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale + 0.5);
127 RadialBlurWindow::RadialBlurWindow(RadialBlurMain *plugin)
128 : PluginClientWindow(plugin,
135 this->plugin = plugin;
138 RadialBlurWindow::~RadialBlurWindow()
142 void RadialBlurWindow::create_objects()
145 int x1 = 0; int clrBtn_w = 50;
146 int defaultBtn_w = 100;
148 add_subwindow(new BC_Title(x, y, _("X:")));
150 add_subwindow(this->x = new RadialBlurSize(plugin, x, y, &plugin->config.x, 0, 100));
151 x1 = x + this->x->get_w() + 10;
152 add_subwindow(xClr = new RadialBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_XSLIDER));
155 add_subwindow(new BC_Title(x, y, _("Y:")));
157 add_subwindow(this->y = new RadialBlurSize(plugin, x, y, &plugin->config.y, 0, 100));
158 add_subwindow(yClr = new RadialBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_YSLIDER));
161 add_subwindow(new BC_Title(x, y, _("Angle:")));
163 add_subwindow(angle = new RadialBlurSize(plugin, x, y, &plugin->config.angle, 0, 360));
164 add_subwindow(angleClr = new RadialBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_ANGLE));
167 add_subwindow(new BC_Title(x, y, _("Steps:")));
169 add_subwindow(steps = new RadialBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
170 add_subwindow(stepsClr = new RadialBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_STEPS));
173 add_subwindow(r = new RadialBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
175 add_subwindow(g = new RadialBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
177 add_subwindow(b = new RadialBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
179 add_subwindow(a = new RadialBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
181 add_subwindow(reset = new RadialBlurReset(plugin, this, x, y));
182 add_subwindow(default_settings = new RadialBlurDefaultSettings(plugin, this,
183 (280 - 10 - defaultBtn_w), y, defaultBtn_w));
190 void RadialBlurWindow::update_gui(int clear)
193 case RESET_XSLIDER : this->x->update(plugin->config.x);
195 case RESET_YSLIDER : this->y->update(plugin->config.y);
197 case RESET_ANGLE : angle->update(plugin->config.angle);
199 case RESET_STEPS : steps->update(plugin->config.steps);
202 case RESET_DEFAULT_SETTINGS :
204 this->x->update(plugin->config.x);
205 this->y->update(plugin->config.y);
206 angle->update(plugin->config.angle);
207 steps->update(plugin->config.steps);
208 r->update(plugin->config.r);
209 g->update(plugin->config.g);
210 b->update(plugin->config.b);
211 a->update(plugin->config.a);
224 RadialBlurToggle::RadialBlurToggle(RadialBlurMain *plugin,
229 : BC_CheckBox(x, y, *output, string)
231 this->plugin = plugin;
232 this->output = output;
235 int RadialBlurToggle::handle_event()
237 *output = get_value();
238 plugin->send_configure_change();
248 RadialBlurSize::RadialBlurSize(RadialBlurMain *plugin,
254 : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
256 this->plugin = plugin;
257 this->output = output;
259 int RadialBlurSize::handle_event()
261 *output = get_value();
262 plugin->send_configure_change();
272 RadialBlurReset::RadialBlurReset(RadialBlurMain *plugin, RadialBlurWindow *gui, int x, int y)
273 : BC_GenericButton(x, y, _("Reset"))
275 this->plugin = plugin;
278 RadialBlurReset::~RadialBlurReset()
281 int RadialBlurReset::handle_event()
283 plugin->config.reset(RESET_ALL);
284 gui->update_gui(RESET_ALL);
285 plugin->send_configure_change();
290 RadialBlurDefaultSettings::RadialBlurDefaultSettings(RadialBlurMain *plugin, RadialBlurWindow *gui, int x, int y, int w)
291 : BC_GenericButton(x, y, w, _("Default"))
293 this->plugin = plugin;
296 RadialBlurDefaultSettings::~RadialBlurDefaultSettings()
299 int RadialBlurDefaultSettings::handle_event()
301 plugin->config.reset(RESET_DEFAULT_SETTINGS);
302 gui->update_gui(RESET_DEFAULT_SETTINGS);
303 plugin->send_configure_change();
308 RadialBlurSliderClr::RadialBlurSliderClr(RadialBlurMain *plugin, RadialBlurWindow *gui, int x, int y, int w, int clear)
309 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
311 this->plugin = plugin;
315 RadialBlurSliderClr::~RadialBlurSliderClr()
318 int RadialBlurSliderClr::handle_event()
320 // clear==1 ==> X slider
321 // clear==2 ==> Y slider
322 // clear==3 ==> Angle slider
323 // clear==4 ==> Steps slider
324 plugin->config.reset(clear);
325 gui->update_gui(clear);
326 plugin->send_configure_change();
333 RadialBlurMain::RadialBlurMain(PluginServer *server)
334 : PluginVClient(server)
342 RadialBlurMain::~RadialBlurMain()
345 if(engine) delete engine;
346 if(temp) delete temp;
350 const char* RadialBlurMain::plugin_title() { return N_("Radial Blur"); }
351 int RadialBlurMain::is_realtime() { return 1; }
355 NEW_WINDOW_MACRO(RadialBlurMain, RadialBlurWindow)
357 LOAD_CONFIGURATION_MACRO(RadialBlurMain, RadialBlurConfig)
359 int RadialBlurMain::process_buffer(VFrame *frame,
360 int64_t start_position,
363 load_configuration();
368 get_source_position(),
373 if(get_use_opengl()) return run_opengl();
375 if(!engine) engine = new RadialBlurEngine(this,
376 get_project_smp() + 1,
377 get_project_smp() + 1);
380 this->output = frame;
384 temp = new VFrame(frame->get_w(), frame->get_h(),
385 frame->get_color_model(), 0);
386 temp->copy_from(frame);
389 engine->process_packages();
394 void RadialBlurMain::update_gui()
398 load_configuration();
399 thread->window->lock_window();
400 ((RadialBlurWindow*)thread->window)->x->update(config.x);
401 ((RadialBlurWindow*)thread->window)->y->update(config.y);
402 ((RadialBlurWindow*)thread->window)->angle->update(config.angle);
403 ((RadialBlurWindow*)thread->window)->steps->update(config.steps);
404 ((RadialBlurWindow*)thread->window)->r->update(config.r);
405 ((RadialBlurWindow*)thread->window)->g->update(config.g);
406 ((RadialBlurWindow*)thread->window)->b->update(config.b);
407 ((RadialBlurWindow*)thread->window)->a->update(config.a);
408 thread->window->unlock_window();
415 void RadialBlurMain::save_data(KeyFrame *keyframe)
419 // cause data to be stored directly in text
420 output.set_shared_output(keyframe->xbuf);
421 output.tag.set_title("RADIALBLUR");
423 output.tag.set_property("X", config.x);
424 output.tag.set_property("Y", config.y);
425 output.tag.set_property("ANGLE", config.angle);
426 output.tag.set_property("STEPS", config.steps);
427 output.tag.set_property("R", config.r);
428 output.tag.set_property("G", config.g);
429 output.tag.set_property("B", config.b);
430 output.tag.set_property("A", config.a);
432 output.tag.set_title("/RADIALBLUR");
434 output.append_newline();
435 output.terminate_string();
438 void RadialBlurMain::read_data(KeyFrame *keyframe)
442 input.set_shared_input(keyframe->xbuf);
448 result = input.read_tag();
452 if(input.tag.title_is("RADIALBLUR"))
454 config.x = input.tag.get_property("X", config.x);
455 config.y = input.tag.get_property("Y", config.y);
456 config.angle = input.tag.get_property("ANGLE", config.angle);
457 config.steps = input.tag.get_property("STEPS", config.steps);
458 config.r = input.tag.get_property("R", config.r);
459 config.g = input.tag.get_property("G", config.g);
460 config.b = input.tag.get_property("B", config.b);
461 config.a = input.tag.get_property("A", config.a);
467 int RadialBlurMain::handle_opengl()
470 get_output()->to_texture();
471 get_output()->enable_opengl();
472 get_output()->init_screen();
473 get_output()->bind_texture(0);
476 //int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
477 glClearColor(0.0, 0.0, 0.0, 0.0);
478 glClear(GL_COLOR_BUFFER_BIT);
480 // Draw unselected channels
482 glBlendFunc(GL_ONE, GL_ONE);
483 glDrawBuffer(GL_BACK);
484 if(!config.r || !config.g || !config.b || !config.a)
486 glColor4f(config.r ? 0 : 1,
490 get_output()->draw_texture();
492 glAccum(GL_LOAD, 1.0);
495 // Blur selected channels
496 float fraction = 1.0 / config.steps;
497 for(int i = 0; i < config.steps; i++)
499 get_output()->set_opengl_state(VFrame::TEXTURE);
500 glClear(GL_COLOR_BUFFER_BIT);
501 glColor4f(config.r ? 1 : 0,
506 float w = get_output()->get_w();
507 float h = get_output()->get_h();
511 double current_angle = (double)config.angle *
514 (double)config.angle / 2;
516 if(!rotate) rotate = new AffineEngine(PluginClient::smp + 1,
517 PluginClient::smp + 1);
518 rotate->set_in_pivot((int)(config.x * w / 100),
519 (int)(config.y * h / 100));
520 rotate->set_out_pivot((int)(config.x * w / 100),
521 (int)(config.y * h / 100));
522 rotate->set_opengl(1);
523 rotate->rotate(get_output(),
527 glAccum(GL_ACCUM, fraction);
528 glEnable(GL_TEXTURE_2D);
529 glColor4f(config.r ? 1 : 0,
537 glReadBuffer(GL_BACK);
538 glDisable(GL_TEXTURE_2D);
539 glAccum(GL_RETURN, 1.0);
541 glColor4f(1, 1, 1, 1);
542 get_output()->set_opengl_state(VFrame::SCREEN);
557 RadialBlurPackage::RadialBlurPackage()
563 RadialBlurUnit::RadialBlurUnit(RadialBlurEngine *server,
564 RadialBlurMain *plugin)
567 this->plugin = plugin;
568 this->server = server;
572 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP_TYPE, MAX, DO_YUV) \
574 int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
575 TYPE **in_rows = (TYPE**)plugin->input->get_rows(); \
576 TYPE **out_rows = (TYPE**)plugin->output->get_rows(); \
577 int steps = plugin->config.steps; \
578 double step = (double)plugin->config.angle / 360 * 2 * M_PI / steps; \
580 for(int i = pkg->y1, out_y = pkg->y1 - center_y; \
584 TYPE *out_row = out_rows[i]; \
585 TYPE *in_row = in_rows[i]; \
586 int y_square = out_y * out_y; \
588 for(int j = 0, out_x = -center_x; j < w; j++, out_x++) \
590 TEMP_TYPE accum_r = 0; \
591 TEMP_TYPE accum_g = 0; \
592 TEMP_TYPE accum_b = 0; \
593 TEMP_TYPE accum_a = 0; \
595 /* Output coordinate to polar */ \
596 double magnitude = sqrt(y_square + out_x * out_x); \
599 angle = atan((double)out_x / out_y) + M_PI; \
602 angle = atan((double)out_x / out_y); \
607 angle = M_PI * 1.5; \
609 /* Overlay all steps on this pixel*/ \
610 angle -= (double)plugin->config.angle / 360 * M_PI; \
611 for(int k = 0; k < steps; k++, angle += step) \
613 /* Polar to input coordinate */ \
614 int in_x = (int)(magnitude * sin(angle)) + center_x; \
615 int in_y = (int)(magnitude * cos(angle)) + center_y; \
617 /* Accumulate input coordinate */ \
618 if(in_x >= 0 && in_x < w && in_y >= 0 && in_y < h) \
620 accum_r += in_rows[in_y][in_x * COMPONENTS]; \
623 accum_g += (int)in_rows[in_y][in_x * COMPONENTS + 1]; \
624 accum_b += (int)in_rows[in_y][in_x * COMPONENTS + 2]; \
628 accum_g += in_rows[in_y][in_x * COMPONENTS + 1]; \
629 accum_b += in_rows[in_y][in_x * COMPONENTS + 2]; \
631 if(COMPONENTS == 4) \
632 accum_a += in_rows[in_y][in_x * COMPONENTS + 3]; \
636 accum_g += chroma_offset; \
637 accum_b += chroma_offset; \
641 /* Accumulation to output */ \
644 *out_row++ = (accum_r * fraction) / 0x10000; \
649 *out_row++ = *in_row++; \
655 *out_row++ = ((accum_g * fraction) / 0x10000); \
657 *out_row++ = (accum_g * fraction) / 0x10000; \
662 *out_row++ = *in_row++; \
668 *out_row++ = (accum_b * fraction) / 0x10000; \
670 *out_row++ = (accum_b * fraction) / 0x10000; \
675 *out_row++ = *in_row++; \
678 if(COMPONENTS == 4) \
682 *out_row++ = (accum_a * fraction) / 0x10000; \
687 *out_row++ = *in_row++; \
694 void RadialBlurUnit::process_package(LoadPackage *package)
696 RadialBlurPackage *pkg = (RadialBlurPackage*)package;
697 int h = plugin->output->get_h();
698 int w = plugin->output->get_w();
699 int do_r = plugin->config.r;
700 int do_g = plugin->config.g;
701 int do_b = plugin->config.b;
702 int do_a = plugin->config.a;
703 int fraction = 0x10000 / plugin->config.steps;
704 int center_x = plugin->config.x * w / 100;
705 int center_y = plugin->config.y * h / 100;
707 switch(plugin->input->get_color_model())
710 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
713 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
716 BLEND_LAYER(3, float, float, 1, 0)
719 BLEND_LAYER(4, float, float, 1, 0)
722 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
724 case BC_RGBA16161616:
725 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
728 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
731 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
734 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
736 case BC_YUVA16161616:
737 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
747 RadialBlurEngine::RadialBlurEngine(RadialBlurMain *plugin,
750 : LoadServer(total_clients, total_packages)
751 // : LoadServer(1, 1)
753 this->plugin = plugin;
756 void RadialBlurEngine::init_packages()
758 for(int i = 0; i < get_total_packages(); i++)
760 RadialBlurPackage *package = (RadialBlurPackage*)get_package(i);
761 package->y1 = plugin->output->get_h() * i / get_total_packages();
762 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
766 LoadClient* RadialBlurEngine::new_client()
768 return new RadialBlurUnit(this, plugin);
771 LoadPackage* RadialBlurEngine::new_package()
773 return new RadialBlurPackage;