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 = xS(50);
52 case RESET_YSLIDER : y = yS(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()
144 int xs10 = xS(10), xs50 = xS(50), xs100 = xS(100);
145 int ys10 = yS(10), ys20 = yS(20), ys30 = yS(30), ys40 = yS(45);
146 int x = xs10, y = ys10;
147 int x1 = 0; int clrBtn_w = xs50;
148 int defaultBtn_w = xs100;
150 add_subwindow(new BC_Title(x, y, _("X:")));
152 add_subwindow(this->x = new RadialBlurSize(plugin, x, y, &plugin->config.x, 0, 100));
153 x1 = x + this->x->get_w() + xs10;
154 add_subwindow(xClr = new RadialBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_XSLIDER));
157 add_subwindow(new BC_Title(x, y, _("Y:")));
159 add_subwindow(this->y = new RadialBlurSize(plugin, x, y, &plugin->config.y, 0, 100));
160 add_subwindow(yClr = new RadialBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_YSLIDER));
163 add_subwindow(new BC_Title(x, y, _("Angle:")));
165 add_subwindow(angle = new RadialBlurSize(plugin, x, y, &plugin->config.angle, 0, 360));
166 add_subwindow(angleClr = new RadialBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_ANGLE));
169 add_subwindow(new BC_Title(x, y, _("Steps:")));
171 add_subwindow(steps = new RadialBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
172 add_subwindow(stepsClr = new RadialBlurSliderClr(plugin, this, x1, y, clrBtn_w, RESET_STEPS));
175 add_subwindow(r = new RadialBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
177 add_subwindow(g = new RadialBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
179 add_subwindow(b = new RadialBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
181 add_subwindow(a = new RadialBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
183 add_subwindow(reset = new RadialBlurReset(plugin, this, x, y));
184 add_subwindow(default_settings = new RadialBlurDefaultSettings(plugin, this,
185 (xS(250) - xS(10) - defaultBtn_w), y, defaultBtn_w));
192 void RadialBlurWindow::update_gui(int clear)
195 case RESET_XSLIDER : this->x->update(plugin->config.x);
197 case RESET_YSLIDER : this->y->update(plugin->config.y);
199 case RESET_ANGLE : angle->update(plugin->config.angle);
201 case RESET_STEPS : steps->update(plugin->config.steps);
204 case RESET_DEFAULT_SETTINGS :
206 this->x->update(plugin->config.x);
207 this->y->update(plugin->config.y);
208 angle->update(plugin->config.angle);
209 steps->update(plugin->config.steps);
210 r->update(plugin->config.r);
211 g->update(plugin->config.g);
212 b->update(plugin->config.b);
213 a->update(plugin->config.a);
226 RadialBlurToggle::RadialBlurToggle(RadialBlurMain *plugin,
231 : BC_CheckBox(x, y, *output, string)
233 this->plugin = plugin;
234 this->output = output;
237 int RadialBlurToggle::handle_event()
239 *output = get_value();
240 plugin->send_configure_change();
250 RadialBlurSize::RadialBlurSize(RadialBlurMain *plugin,
256 : BC_ISlider(x, y, 0, xS(200), yS(200), min, max, *output)
258 this->plugin = plugin;
259 this->output = output;
261 int RadialBlurSize::handle_event()
263 *output = get_value();
264 plugin->send_configure_change();
274 RadialBlurReset::RadialBlurReset(RadialBlurMain *plugin, RadialBlurWindow *gui, int x, int y)
275 : BC_GenericButton(x, y, _("Reset"))
277 this->plugin = plugin;
280 RadialBlurReset::~RadialBlurReset()
283 int RadialBlurReset::handle_event()
285 plugin->config.reset(RESET_ALL);
286 gui->update_gui(RESET_ALL);
287 plugin->send_configure_change();
292 RadialBlurDefaultSettings::RadialBlurDefaultSettings(RadialBlurMain *plugin, RadialBlurWindow *gui, int x, int y, int w)
293 : BC_GenericButton(x, y, w, _("Default"))
295 this->plugin = plugin;
298 RadialBlurDefaultSettings::~RadialBlurDefaultSettings()
301 int RadialBlurDefaultSettings::handle_event()
303 plugin->config.reset(RESET_DEFAULT_SETTINGS);
304 gui->update_gui(RESET_DEFAULT_SETTINGS);
305 plugin->send_configure_change();
310 RadialBlurSliderClr::RadialBlurSliderClr(RadialBlurMain *plugin, RadialBlurWindow *gui, int x, int y, int w, int clear)
311 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
313 this->plugin = plugin;
317 RadialBlurSliderClr::~RadialBlurSliderClr()
320 int RadialBlurSliderClr::handle_event()
322 // clear==1 ==> X slider
323 // clear==2 ==> Y slider
324 // clear==3 ==> Angle slider
325 // clear==4 ==> Steps slider
326 plugin->config.reset(clear);
327 gui->update_gui(clear);
328 plugin->send_configure_change();
335 RadialBlurMain::RadialBlurMain(PluginServer *server)
336 : PluginVClient(server)
344 RadialBlurMain::~RadialBlurMain()
347 if(engine) delete engine;
348 if(temp) delete temp;
352 const char* RadialBlurMain::plugin_title() { return N_("Radial Blur"); }
353 int RadialBlurMain::is_realtime() { return 1; }
357 NEW_WINDOW_MACRO(RadialBlurMain, RadialBlurWindow)
359 LOAD_CONFIGURATION_MACRO(RadialBlurMain, RadialBlurConfig)
361 int RadialBlurMain::process_buffer(VFrame *frame,
362 int64_t start_position,
365 load_configuration();
370 get_source_position(),
375 if(get_use_opengl()) return run_opengl();
377 if(!engine) engine = new RadialBlurEngine(this,
378 get_project_smp() + 1,
379 get_project_smp() + 1);
382 this->output = frame;
386 temp = new VFrame(frame->get_w(), frame->get_h(),
387 frame->get_color_model(), 0);
388 temp->copy_from(frame);
391 engine->process_packages();
396 void RadialBlurMain::update_gui()
400 load_configuration();
401 thread->window->lock_window();
402 ((RadialBlurWindow*)thread->window)->x->update(config.x);
403 ((RadialBlurWindow*)thread->window)->y->update(config.y);
404 ((RadialBlurWindow*)thread->window)->angle->update(config.angle);
405 ((RadialBlurWindow*)thread->window)->steps->update(config.steps);
406 ((RadialBlurWindow*)thread->window)->r->update(config.r);
407 ((RadialBlurWindow*)thread->window)->g->update(config.g);
408 ((RadialBlurWindow*)thread->window)->b->update(config.b);
409 ((RadialBlurWindow*)thread->window)->a->update(config.a);
410 thread->window->unlock_window();
417 void RadialBlurMain::save_data(KeyFrame *keyframe)
421 // cause data to be stored directly in text
422 output.set_shared_output(keyframe->xbuf);
423 output.tag.set_title("RADIALBLUR");
425 output.tag.set_property("X", config.x);
426 output.tag.set_property("Y", config.y);
427 output.tag.set_property("ANGLE", config.angle);
428 output.tag.set_property("STEPS", config.steps);
429 output.tag.set_property("R", config.r);
430 output.tag.set_property("G", config.g);
431 output.tag.set_property("B", config.b);
432 output.tag.set_property("A", config.a);
434 output.tag.set_title("/RADIALBLUR");
436 output.append_newline();
437 output.terminate_string();
440 void RadialBlurMain::read_data(KeyFrame *keyframe)
444 input.set_shared_input(keyframe->xbuf);
450 result = input.read_tag();
454 if(input.tag.title_is("RADIALBLUR"))
456 config.x = input.tag.get_property("X", config.x);
457 config.y = input.tag.get_property("Y", config.y);
458 config.angle = input.tag.get_property("ANGLE", config.angle);
459 config.steps = input.tag.get_property("STEPS", config.steps);
460 config.r = input.tag.get_property("R", config.r);
461 config.g = input.tag.get_property("G", config.g);
462 config.b = input.tag.get_property("B", config.b);
463 config.a = input.tag.get_property("A", config.a);
469 int RadialBlurMain::handle_opengl()
472 get_output()->to_texture();
473 get_output()->enable_opengl();
474 get_output()->init_screen();
475 get_output()->bind_texture(0);
478 //int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
479 glClearColor(0.0, 0.0, 0.0, 0.0);
480 glClear(GL_COLOR_BUFFER_BIT);
482 // Draw unselected channels
484 glBlendFunc(GL_ONE, GL_ONE);
485 glDrawBuffer(GL_BACK);
486 if(!config.r || !config.g || !config.b || !config.a)
488 glColor4f(config.r ? 0 : 1,
492 get_output()->draw_texture();
494 glAccum(GL_LOAD, 1.0);
497 // Blur selected channels
498 float fraction = 1.0 / config.steps;
499 for(int i = 0; i < config.steps; i++)
501 get_output()->set_opengl_state(VFrame::TEXTURE);
502 glClear(GL_COLOR_BUFFER_BIT);
503 glColor4f(config.r ? 1 : 0,
508 float w = get_output()->get_w();
509 float h = get_output()->get_h();
513 double current_angle = (double)config.angle *
516 (double)config.angle / 2;
518 if(!rotate) rotate = new AffineEngine(PluginClient::smp + 1,
519 PluginClient::smp + 1);
520 rotate->set_in_pivot((int)(config.x * w / 100),
521 (int)(config.y * h / 100));
522 rotate->set_out_pivot((int)(config.x * w / 100),
523 (int)(config.y * h / 100));
524 rotate->set_opengl(1);
525 rotate->rotate(get_output(),
529 glAccum(GL_ACCUM, fraction);
530 glEnable(GL_TEXTURE_2D);
531 glColor4f(config.r ? 1 : 0,
539 glReadBuffer(GL_BACK);
540 glDisable(GL_TEXTURE_2D);
541 glAccum(GL_RETURN, 1.0);
543 glColor4f(1, 1, 1, 1);
544 get_output()->set_opengl_state(VFrame::SCREEN);
559 RadialBlurPackage::RadialBlurPackage()
565 RadialBlurUnit::RadialBlurUnit(RadialBlurEngine *server,
566 RadialBlurMain *plugin)
569 this->plugin = plugin;
570 this->server = server;
574 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP_TYPE, MAX, DO_YUV) \
576 int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
577 TYPE **in_rows = (TYPE**)plugin->input->get_rows(); \
578 TYPE **out_rows = (TYPE**)plugin->output->get_rows(); \
579 int steps = plugin->config.steps; \
580 double step = (double)plugin->config.angle / 360 * 2 * M_PI / steps; \
582 for(int i = pkg->y1, out_y = pkg->y1 - center_y; \
586 TYPE *out_row = out_rows[i]; \
587 TYPE *in_row = in_rows[i]; \
588 int y_square = out_y * out_y; \
590 for(int j = 0, out_x = -center_x; j < w; j++, out_x++) \
592 TEMP_TYPE accum_r = 0; \
593 TEMP_TYPE accum_g = 0; \
594 TEMP_TYPE accum_b = 0; \
595 TEMP_TYPE accum_a = 0; \
597 /* Output coordinate to polar */ \
598 double magnitude = sqrt(y_square + out_x * out_x); \
601 angle = atan((double)out_x / out_y) + M_PI; \
604 angle = atan((double)out_x / out_y); \
609 angle = M_PI * 1.5; \
611 /* Overlay all steps on this pixel*/ \
612 angle -= (double)plugin->config.angle / 360 * M_PI; \
613 for(int k = 0; k < steps; k++, angle += step) \
615 /* Polar to input coordinate */ \
616 int in_x = (int)(magnitude * sin(angle)) + center_x; \
617 int in_y = (int)(magnitude * cos(angle)) + center_y; \
619 /* Accumulate input coordinate */ \
620 if(in_x >= 0 && in_x < w && in_y >= 0 && in_y < h) \
622 accum_r += in_rows[in_y][in_x * COMPONENTS]; \
625 accum_g += (int)in_rows[in_y][in_x * COMPONENTS + 1]; \
626 accum_b += (int)in_rows[in_y][in_x * COMPONENTS + 2]; \
630 accum_g += in_rows[in_y][in_x * COMPONENTS + 1]; \
631 accum_b += in_rows[in_y][in_x * COMPONENTS + 2]; \
633 if(COMPONENTS == 4) \
634 accum_a += in_rows[in_y][in_x * COMPONENTS + 3]; \
638 accum_g += chroma_offset; \
639 accum_b += chroma_offset; \
643 /* Accumulation to output */ \
646 *out_row++ = (accum_r * fraction) / 0x10000; \
651 *out_row++ = *in_row++; \
657 *out_row++ = ((accum_g * fraction) / 0x10000); \
659 *out_row++ = (accum_g * fraction) / 0x10000; \
664 *out_row++ = *in_row++; \
670 *out_row++ = (accum_b * fraction) / 0x10000; \
672 *out_row++ = (accum_b * fraction) / 0x10000; \
677 *out_row++ = *in_row++; \
680 if(COMPONENTS == 4) \
684 *out_row++ = (accum_a * fraction) / 0x10000; \
689 *out_row++ = *in_row++; \
696 void RadialBlurUnit::process_package(LoadPackage *package)
698 RadialBlurPackage *pkg = (RadialBlurPackage*)package;
699 int h = plugin->output->get_h();
700 int w = plugin->output->get_w();
701 int do_r = plugin->config.r;
702 int do_g = plugin->config.g;
703 int do_b = plugin->config.b;
704 int do_a = plugin->config.a;
705 int fraction = 0x10000 / plugin->config.steps;
706 int center_x = plugin->config.x * w / 100;
707 int center_y = plugin->config.y * h / 100;
709 switch(plugin->input->get_color_model())
712 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
715 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
718 BLEND_LAYER(3, float, float, 1, 0)
721 BLEND_LAYER(4, float, float, 1, 0)
724 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
726 case BC_RGBA16161616:
727 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
730 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
733 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
736 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
738 case BC_YUVA16161616:
739 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
749 RadialBlurEngine::RadialBlurEngine(RadialBlurMain *plugin,
752 : LoadServer(total_clients, total_packages)
753 // : LoadServer(1, 1)
755 this->plugin = plugin;
758 void RadialBlurEngine::init_packages()
760 for(int i = 0; i < get_total_packages(); i++)
762 RadialBlurPackage *package = (RadialBlurPackage*)get_package(i);
763 package->y1 = plugin->output->get_h() * i / get_total_packages();
764 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
768 LoadClient* RadialBlurEngine::new_client()
770 return new RadialBlurUnit(this, plugin);
773 LoadPackage* RadialBlurEngine::new_package()
775 return new RadialBlurPackage;