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
28 #include "bcdisplayinfo.h"
34 #include "loadbalance.h"
35 #include "pluginvclient.h"
40 class RadialBlurEngine;
46 class RadialBlurConfig
51 int equivalent(RadialBlurConfig &that);
52 void copy_from(RadialBlurConfig &that);
53 void interpolate(RadialBlurConfig &prev,
54 RadialBlurConfig &next,
71 class RadialBlurSize : public BC_ISlider
74 RadialBlurSize(RadialBlurMain *plugin,
81 RadialBlurMain *plugin;
85 class RadialBlurToggle : public BC_CheckBox
88 RadialBlurToggle(RadialBlurMain *plugin,
94 RadialBlurMain *plugin;
98 class RadialBlurWindow : public PluginClientWindow
101 RadialBlurWindow(RadialBlurMain *plugin);
104 void create_objects();
107 RadialBlurSize *x, *y, *steps, *angle;
108 RadialBlurToggle *r, *g, *b, *a;
109 RadialBlurMain *plugin;
117 class RadialBlurMain : public PluginVClient
120 RadialBlurMain(PluginServer *server);
123 int process_buffer(VFrame *frame,
124 int64_t start_position,
127 void save_data(KeyFrame *keyframe);
128 void read_data(KeyFrame *keyframe);
132 PLUGIN_CLASS_MEMBERS(RadialBlurConfig)
134 VFrame *input, *output, *temp;
135 RadialBlurEngine *engine;
136 // Rotate engine only used for OpenGL
137 AffineEngine *rotate;
140 class RadialBlurPackage : public LoadPackage
147 class RadialBlurUnit : public LoadClient
150 RadialBlurUnit(RadialBlurEngine *server, RadialBlurMain *plugin);
151 void process_package(LoadPackage *package);
152 RadialBlurEngine *server;
153 RadialBlurMain *plugin;
156 class RadialBlurEngine : public LoadServer
159 RadialBlurEngine(RadialBlurMain *plugin,
162 void init_packages();
163 LoadClient* new_client();
164 LoadPackage* new_package();
165 RadialBlurMain *plugin;
186 REGISTER_PLUGIN(RadialBlurMain)
190 RadialBlurConfig::RadialBlurConfig()
202 int RadialBlurConfig::equivalent(RadialBlurConfig &that)
205 angle == that.angle &&
208 steps == that.steps &&
215 void RadialBlurConfig::copy_from(RadialBlurConfig &that)
227 void RadialBlurConfig::interpolate(RadialBlurConfig &prev,
228 RadialBlurConfig &next,
233 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
234 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
235 this->x = (int)(prev.x * prev_scale + next.x * next_scale + 0.5);
236 this->y = (int)(prev.y * prev_scale + next.y * next_scale + 0.5);
237 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
238 this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale + 0.5);
257 RadialBlurWindow::RadialBlurWindow(RadialBlurMain *plugin)
258 : PluginClientWindow(plugin,
265 this->plugin = plugin;
268 RadialBlurWindow::~RadialBlurWindow()
272 void RadialBlurWindow::create_objects()
276 add_subwindow(new BC_Title(x, y, _("X:")));
278 add_subwindow(this->x = new RadialBlurSize(plugin, x, y, &plugin->config.x, 0, 100));
280 add_subwindow(new BC_Title(x, y, _("Y:")));
282 add_subwindow(this->y = new RadialBlurSize(plugin, x, y, &plugin->config.y, 0, 100));
284 add_subwindow(new BC_Title(x, y, _("Angle:")));
286 add_subwindow(angle = new RadialBlurSize(plugin, x, y, &plugin->config.angle, 0, 360));
288 add_subwindow(new BC_Title(x, y, _("Steps:")));
290 add_subwindow(steps = new RadialBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
292 add_subwindow(r = new RadialBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
294 add_subwindow(g = new RadialBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
296 add_subwindow(b = new RadialBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
298 add_subwindow(a = new RadialBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
315 RadialBlurToggle::RadialBlurToggle(RadialBlurMain *plugin,
320 : BC_CheckBox(x, y, *output, string)
322 this->plugin = plugin;
323 this->output = output;
326 int RadialBlurToggle::handle_event()
328 *output = get_value();
329 plugin->send_configure_change();
339 RadialBlurSize::RadialBlurSize(RadialBlurMain *plugin,
345 : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
347 this->plugin = plugin;
348 this->output = output;
350 int RadialBlurSize::handle_event()
352 *output = get_value();
353 plugin->send_configure_change();
366 RadialBlurMain::RadialBlurMain(PluginServer *server)
367 : PluginVClient(server)
375 RadialBlurMain::~RadialBlurMain()
378 if(engine) delete engine;
379 if(temp) delete temp;
383 const char* RadialBlurMain::plugin_title() { return N_("Radial Blur"); }
384 int RadialBlurMain::is_realtime() { return 1; }
388 NEW_WINDOW_MACRO(RadialBlurMain, RadialBlurWindow)
390 LOAD_CONFIGURATION_MACRO(RadialBlurMain, RadialBlurConfig)
392 int RadialBlurMain::process_buffer(VFrame *frame,
393 int64_t start_position,
396 load_configuration();
401 get_source_position(),
406 if(get_use_opengl()) return run_opengl();
408 if(!engine) engine = new RadialBlurEngine(this,
409 get_project_smp() + 1,
410 get_project_smp() + 1);
413 this->output = frame;
417 temp = new VFrame(frame->get_w(), frame->get_h(),
418 frame->get_color_model(), 0);
419 temp->copy_from(frame);
422 engine->process_packages();
427 void RadialBlurMain::update_gui()
431 load_configuration();
432 thread->window->lock_window();
433 ((RadialBlurWindow*)thread->window)->x->update(config.x);
434 ((RadialBlurWindow*)thread->window)->y->update(config.y);
435 ((RadialBlurWindow*)thread->window)->angle->update(config.angle);
436 ((RadialBlurWindow*)thread->window)->steps->update(config.steps);
437 ((RadialBlurWindow*)thread->window)->r->update(config.r);
438 ((RadialBlurWindow*)thread->window)->g->update(config.g);
439 ((RadialBlurWindow*)thread->window)->b->update(config.b);
440 ((RadialBlurWindow*)thread->window)->a->update(config.a);
441 thread->window->unlock_window();
448 void RadialBlurMain::save_data(KeyFrame *keyframe)
452 // cause data to be stored directly in text
453 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
454 output.tag.set_title("RADIALBLUR");
456 output.tag.set_property("X", config.x);
457 output.tag.set_property("Y", config.y);
458 output.tag.set_property("ANGLE", config.angle);
459 output.tag.set_property("STEPS", config.steps);
460 output.tag.set_property("R", config.r);
461 output.tag.set_property("G", config.g);
462 output.tag.set_property("B", config.b);
463 output.tag.set_property("A", config.a);
465 output.tag.set_title("/RADIALBLUR");
467 output.append_newline();
468 output.terminate_string();
471 void RadialBlurMain::read_data(KeyFrame *keyframe)
475 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
481 result = input.read_tag();
485 if(input.tag.title_is("RADIALBLUR"))
487 config.x = input.tag.get_property("X", config.x);
488 config.y = input.tag.get_property("Y", config.y);
489 config.angle = input.tag.get_property("ANGLE", config.angle);
490 config.steps = input.tag.get_property("STEPS", config.steps);
491 config.r = input.tag.get_property("R", config.r);
492 config.g = input.tag.get_property("G", config.g);
493 config.b = input.tag.get_property("B", config.b);
494 config.a = input.tag.get_property("A", config.a);
500 int RadialBlurMain::handle_opengl()
503 get_output()->to_texture();
504 get_output()->enable_opengl();
505 get_output()->init_screen();
506 get_output()->bind_texture(0);
509 //int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
510 glClearColor(0.0, 0.0, 0.0, 0.0);
511 glClear(GL_COLOR_BUFFER_BIT);
513 // Draw unselected channels
515 glBlendFunc(GL_ONE, GL_ONE);
516 glDrawBuffer(GL_BACK);
517 if(!config.r || !config.g || !config.b || !config.a)
519 glColor4f(config.r ? 0 : 1,
523 get_output()->draw_texture();
525 glAccum(GL_LOAD, 1.0);
528 // Blur selected channels
529 float fraction = 1.0 / config.steps;
530 for(int i = 0; i < config.steps; i++)
532 get_output()->set_opengl_state(VFrame::TEXTURE);
533 glClear(GL_COLOR_BUFFER_BIT);
534 glColor4f(config.r ? 1 : 0,
539 float w = get_output()->get_w();
540 float h = get_output()->get_h();
544 double current_angle = (double)config.angle *
547 (double)config.angle / 2;
549 if(!rotate) rotate = new AffineEngine(PluginClient::smp + 1,
550 PluginClient::smp + 1);
551 rotate->set_in_pivot((int)(config.x * w / 100),
552 (int)(config.y * h / 100));
553 rotate->set_out_pivot((int)(config.x * w / 100),
554 (int)(config.y * h / 100));
555 rotate->set_opengl(1);
556 rotate->rotate(get_output(),
560 glAccum(GL_ACCUM, fraction);
561 glEnable(GL_TEXTURE_2D);
562 glColor4f(config.r ? 1 : 0,
570 glReadBuffer(GL_BACK);
571 glDisable(GL_TEXTURE_2D);
572 glAccum(GL_RETURN, 1.0);
574 glColor4f(1, 1, 1, 1);
575 get_output()->set_opengl_state(VFrame::SCREEN);
590 RadialBlurPackage::RadialBlurPackage()
596 RadialBlurUnit::RadialBlurUnit(RadialBlurEngine *server,
597 RadialBlurMain *plugin)
600 this->plugin = plugin;
601 this->server = server;
605 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP_TYPE, MAX, DO_YUV) \
607 int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
608 TYPE **in_rows = (TYPE**)plugin->input->get_rows(); \
609 TYPE **out_rows = (TYPE**)plugin->output->get_rows(); \
610 int steps = plugin->config.steps; \
611 double step = (double)plugin->config.angle / 360 * 2 * M_PI / steps; \
613 for(int i = pkg->y1, out_y = pkg->y1 - center_y; \
617 TYPE *out_row = out_rows[i]; \
618 TYPE *in_row = in_rows[i]; \
619 int y_square = out_y * out_y; \
621 for(int j = 0, out_x = -center_x; j < w; j++, out_x++) \
623 TEMP_TYPE accum_r = 0; \
624 TEMP_TYPE accum_g = 0; \
625 TEMP_TYPE accum_b = 0; \
626 TEMP_TYPE accum_a = 0; \
628 /* Output coordinate to polar */ \
629 double magnitude = sqrt(y_square + out_x * out_x); \
632 angle = atan((double)out_x / out_y) + M_PI; \
635 angle = atan((double)out_x / out_y); \
640 angle = M_PI * 1.5; \
642 /* Overlay all steps on this pixel*/ \
643 angle -= (double)plugin->config.angle / 360 * M_PI; \
644 for(int k = 0; k < steps; k++, angle += step) \
646 /* Polar to input coordinate */ \
647 int in_x = (int)(magnitude * sin(angle)) + center_x; \
648 int in_y = (int)(magnitude * cos(angle)) + center_y; \
650 /* Accumulate input coordinate */ \
651 if(in_x >= 0 && in_x < w && in_y >= 0 && in_y < h) \
653 accum_r += in_rows[in_y][in_x * COMPONENTS]; \
656 accum_g += (int)in_rows[in_y][in_x * COMPONENTS + 1]; \
657 accum_b += (int)in_rows[in_y][in_x * COMPONENTS + 2]; \
661 accum_g += in_rows[in_y][in_x * COMPONENTS + 1]; \
662 accum_b += in_rows[in_y][in_x * COMPONENTS + 2]; \
664 if(COMPONENTS == 4) \
665 accum_a += in_rows[in_y][in_x * COMPONENTS + 3]; \
669 accum_g += chroma_offset; \
670 accum_b += chroma_offset; \
674 /* Accumulation to output */ \
677 *out_row++ = (accum_r * fraction) / 0x10000; \
682 *out_row++ = *in_row++; \
688 *out_row++ = ((accum_g * fraction) / 0x10000); \
690 *out_row++ = (accum_g * fraction) / 0x10000; \
695 *out_row++ = *in_row++; \
701 *out_row++ = (accum_b * fraction) / 0x10000; \
703 *out_row++ = (accum_b * fraction) / 0x10000; \
708 *out_row++ = *in_row++; \
711 if(COMPONENTS == 4) \
715 *out_row++ = (accum_a * fraction) / 0x10000; \
720 *out_row++ = *in_row++; \
727 void RadialBlurUnit::process_package(LoadPackage *package)
729 RadialBlurPackage *pkg = (RadialBlurPackage*)package;
730 int h = plugin->output->get_h();
731 int w = plugin->output->get_w();
732 int do_r = plugin->config.r;
733 int do_g = plugin->config.g;
734 int do_b = plugin->config.b;
735 int do_a = plugin->config.a;
736 int fraction = 0x10000 / plugin->config.steps;
737 int center_x = plugin->config.x * w / 100;
738 int center_y = plugin->config.y * h / 100;
740 switch(plugin->input->get_color_model())
743 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
746 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
749 BLEND_LAYER(3, float, float, 1, 0)
752 BLEND_LAYER(4, float, float, 1, 0)
755 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
757 case BC_RGBA16161616:
758 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
761 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
764 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
767 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
769 case BC_YUVA16161616:
770 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
780 RadialBlurEngine::RadialBlurEngine(RadialBlurMain *plugin,
783 : LoadServer(total_clients, total_packages)
784 // : LoadServer(1, 1)
786 this->plugin = plugin;
789 void RadialBlurEngine::init_packages()
791 for(int i = 0; i < get_total_packages(); i++)
793 RadialBlurPackage *package = (RadialBlurPackage*)get_package(i);
794 package->y1 = plugin->output->get_h() * i / get_total_packages();
795 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
799 LoadClient* RadialBlurEngine::new_client()
801 return new RadialBlurUnit(this, plugin);
804 LoadPackage* RadialBlurEngine::new_package()
806 return new RadialBlurPackage;