5 #include "bcdisplayinfo.h"
10 #include "loadbalance.h"
11 #include "picon_png.h"
12 #include "pluginvclient.h"
16 #define _(String) gettext(String)
17 #define gettext_noop(String) String
18 #define N_(String) gettext_noop (String)
33 int equivalent(ZoomBlurConfig &that);
34 void copy_from(ZoomBlurConfig &that);
35 void interpolate(ZoomBlurConfig &prev,
53 class ZoomBlurSize : public BC_ISlider
56 ZoomBlurSize(ZoomBlurMain *plugin,
67 class ZoomBlurToggle : public BC_CheckBox
70 ZoomBlurToggle(ZoomBlurMain *plugin,
80 class ZoomBlurWindow : public BC_Window
83 ZoomBlurWindow(ZoomBlurMain *plugin, int x, int y);
89 ZoomBlurSize *x, *y, *radius, *steps;
90 ZoomBlurToggle *r, *g, *b, *a;
96 PLUGIN_THREAD_HEADER(ZoomBlurMain, ZoomBlurThread, ZoomBlurWindow)
99 class ZoomBlurMain : public PluginVClient
102 ZoomBlurMain(PluginServer *server);
105 int process_realtime(VFrame *input_ptr, VFrame *output_ptr);
109 void save_data(KeyFrame *keyframe);
110 void read_data(KeyFrame *keyframe);
113 PLUGIN_CLASS_MEMBERS(ZoomBlurConfig, ZoomBlurThread)
115 void delete_tables();
116 VFrame *input, *output, *temp;
117 ZoomBlurEngine *engine;
121 int need_reconfigure;
125 class ZoomBlurPackage : public LoadPackage
132 class ZoomBlurUnit : public LoadClient
135 ZoomBlurUnit(ZoomBlurEngine *server, ZoomBlurMain *plugin);
136 void process_package(LoadPackage *package);
137 ZoomBlurEngine *server;
138 ZoomBlurMain *plugin;
141 class ZoomBlurEngine : public LoadServer
144 ZoomBlurEngine(ZoomBlurMain *plugin,
147 void init_packages();
148 LoadClient* new_client();
149 LoadPackage* new_package();
150 ZoomBlurMain *plugin;
171 REGISTER_PLUGIN(ZoomBlurMain)
175 ZoomBlurConfig::ZoomBlurConfig()
187 int ZoomBlurConfig::equivalent(ZoomBlurConfig &that)
192 radius == that.radius &&
193 steps == that.steps &&
200 void ZoomBlurConfig::copy_from(ZoomBlurConfig &that)
204 radius = that.radius;
212 void ZoomBlurConfig::interpolate(ZoomBlurConfig &prev,
213 ZoomBlurConfig &next,
218 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
219 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
220 this->x = (int)(prev.x * prev_scale + next.x * next_scale + 0.5);
221 this->y = (int)(prev.y * prev_scale + next.y * next_scale + 0.5);
222 this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale + 0.5);
223 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
238 PLUGIN_THREAD_OBJECT(ZoomBlurMain, ZoomBlurThread, ZoomBlurWindow)
242 ZoomBlurWindow::ZoomBlurWindow(ZoomBlurMain *plugin, int x, int y)
243 : BC_Window(plugin->gui_string,
253 this->plugin = plugin;
256 ZoomBlurWindow::~ZoomBlurWindow()
260 int ZoomBlurWindow::create_objects()
264 add_subwindow(new BC_Title(x, y, _("X:")));
266 add_subwindow(this->x = new ZoomBlurSize(plugin, x, y, &plugin->config.x, 0, 100));
268 add_subwindow(new BC_Title(x, y, _("Y:")));
270 add_subwindow(this->y = new ZoomBlurSize(plugin, x, y, &plugin->config.y, 0, 100));
272 add_subwindow(new BC_Title(x, y, _("Radius:")));
274 add_subwindow(radius = new ZoomBlurSize(plugin, x, y, &plugin->config.radius, -100, 100));
276 add_subwindow(new BC_Title(x, y, _("Steps:")));
278 add_subwindow(steps = new ZoomBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
280 add_subwindow(r = new ZoomBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
282 add_subwindow(g = new ZoomBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
284 add_subwindow(b = new ZoomBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
286 add_subwindow(a = new ZoomBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
294 int ZoomBlurWindow::close_event()
296 // Set result to 1 to indicate a plugin side close
310 ZoomBlurToggle::ZoomBlurToggle(ZoomBlurMain *plugin,
315 : BC_CheckBox(x, y, *output, string)
317 this->plugin = plugin;
318 this->output = output;
321 int ZoomBlurToggle::handle_event()
323 *output = get_value();
324 plugin->send_configure_change();
334 ZoomBlurSize::ZoomBlurSize(ZoomBlurMain *plugin,
340 : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
342 this->plugin = plugin;
343 this->output = output;
345 int ZoomBlurSize::handle_event()
347 *output = get_value();
348 plugin->send_configure_change();
361 ZoomBlurMain::ZoomBlurMain(PluginServer *server)
362 : PluginVClient(server)
364 PLUGIN_CONSTRUCTOR_MACRO
370 need_reconfigure = 1;
374 ZoomBlurMain::~ZoomBlurMain()
376 PLUGIN_DESTRUCTOR_MACRO
377 if(engine) delete engine;
379 if(accum) delete [] accum;
380 if(temp) delete temp;
383 char* ZoomBlurMain::plugin_title() { return _("Zoom Blur"); }
384 int ZoomBlurMain::is_realtime() { return 1; }
387 NEW_PICON_MACRO(ZoomBlurMain)
389 SHOW_GUI_MACRO(ZoomBlurMain, ZoomBlurThread)
391 SET_STRING_MACRO(ZoomBlurMain)
393 RAISE_WINDOW_MACRO(ZoomBlurMain)
395 LOAD_CONFIGURATION_MACRO(ZoomBlurMain, ZoomBlurConfig)
397 void ZoomBlurMain::delete_tables()
401 for(int i = 0; i < table_entries; i++)
402 delete [] scale_x_table[i];
403 delete [] scale_x_table;
408 for(int i = 0; i < table_entries; i++)
409 delete [] scale_y_table[i];
410 delete [] scale_y_table;
417 int ZoomBlurMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
419 need_reconfigure |= load_configuration();
421 if(!engine) engine = new ZoomBlurEngine(this,
422 get_project_smp() + 1,
423 get_project_smp() + 1);
424 if(!accum) accum = new int[input_ptr->get_w() *
426 cmodel_components(input_ptr->get_color_model())];
428 this->input = input_ptr;
429 this->output = output_ptr;
432 if(input_ptr->get_rows()[0] == output_ptr->get_rows()[0])
434 if(!temp) temp = new VFrame(0,
437 input_ptr->get_color_model());
438 temp->copy_from(input_ptr);
442 // Generate tables here. The same table is used by many packages to render
443 // each horizontal stripe. Need to cover the entire output range in each
444 // table to avoid green borders
447 float w = input->get_w();
448 float h = input->get_h();
449 float center_x = (float)config.x / 100 * w;
450 float center_y = (float)config.y / 100 * h;
451 float radius = (float)(100 + config.radius) / 100;
454 int steps = config.steps ? config.steps : 1;
465 // printf("ZoomBlurMain::process_realtime 1 %d %d\n",
469 center_x = (center_x - w / 2) * (1.0 - radius) + w / 2;
470 center_y = (center_y - h / 2) * (1.0 - radius) + h / 2;
475 min_x1 = center_x - min_w / 2;
476 min_y1 = center_y - min_h / 2;
477 min_x2 = center_x + min_w / 2;
478 min_y2 = center_y + min_h / 2;
484 // printf("ZoomBlurMain::process_realtime 2 w=%f radius=%f center_x=%f\n",
490 // Dimensions of outermost rectangle
493 scale_x_table = new int*[config.steps];
494 scale_y_table = new int*[config.steps];
495 table_entries = config.steps;
498 for(int i = 0; i < config.steps; i++)
500 float fraction = (float)i / config.steps;
501 float inv_fraction = 1.0 - fraction;
502 float out_x1 = min_x1 * fraction + max_x1 * inv_fraction;
503 float out_x2 = min_x2 * fraction + max_x2 * inv_fraction;
504 float out_y1 = min_y1 * fraction + max_y1 * inv_fraction;
505 float out_y2 = min_y2 * fraction + max_y2 * inv_fraction;
506 float out_w = out_x2 - out_x1;
507 float out_h = out_y2 - out_y1;
508 if(out_w < 0) out_w = 0;
509 if(out_h < 0) out_h = 0;
510 float scale_x = (float)w / out_w;
511 float scale_y = (float)h / out_h;
515 scale_y_table[i] = y_table = new int[(int)(h + 1)];
516 scale_x_table[i] = x_table = new int[(int)(w + 1)];
518 for(int j = 0; j < h; j++)
520 y_table[j] = (int)((j - out_y1) * scale_y);
522 for(int j = 0; j < w; j++)
524 x_table[j] = (int)((j - out_x1) * scale_x);
525 //printf("ZoomBlurMain::process_realtime %d %d\n", j, x_table[j]);
528 need_reconfigure = 0;
531 bzero(accum, input_ptr->get_w() *
533 cmodel_components(input_ptr->get_color_model()) *
535 engine->process_packages();
540 void ZoomBlurMain::update_gui()
544 load_configuration();
545 thread->window->lock_window();
546 thread->window->x->update(config.x);
547 thread->window->y->update(config.y);
548 thread->window->radius->update(config.radius);
549 thread->window->steps->update(config.steps);
550 thread->window->r->update(config.r);
551 thread->window->g->update(config.g);
552 thread->window->b->update(config.b);
553 thread->window->a->update(config.a);
554 thread->window->unlock_window();
559 int ZoomBlurMain::load_defaults()
561 char directory[1024], string[1024];
562 // set the default directory
563 sprintf(directory, "%szoomblur.rc", BCASTDIR);
566 defaults = new Defaults(directory);
569 config.x = defaults->get("X", config.x);
570 config.y = defaults->get("Y", config.y);
571 config.radius = defaults->get("RADIUS", config.radius);
572 config.steps = defaults->get("STEPS", config.steps);
573 config.r = defaults->get("R", config.r);
574 config.g = defaults->get("G", config.g);
575 config.b = defaults->get("B", config.b);
576 config.a = defaults->get("A", config.a);
581 int ZoomBlurMain::save_defaults()
583 defaults->update("X", config.x);
584 defaults->update("Y", config.y);
585 defaults->update("RADIUS", config.radius);
586 defaults->update("STEPS", config.steps);
587 defaults->update("R", config.r);
588 defaults->update("G", config.g);
589 defaults->update("B", config.b);
590 defaults->update("A", config.a);
597 void ZoomBlurMain::save_data(KeyFrame *keyframe)
601 // cause data to be stored directly in text
602 output.set_shared_string(keyframe->data, MESSAGESIZE);
603 output.tag.set_title("ZOOMBLUR");
605 output.tag.set_property("X", config.x);
606 output.tag.set_property("Y", config.y);
607 output.tag.set_property("RADIUS", config.radius);
608 output.tag.set_property("STEPS", config.steps);
609 output.tag.set_property("R", config.r);
610 output.tag.set_property("G", config.g);
611 output.tag.set_property("B", config.b);
612 output.tag.set_property("A", config.a);
614 output.terminate_string();
617 void ZoomBlurMain::read_data(KeyFrame *keyframe)
621 input.set_shared_string(keyframe->data, strlen(keyframe->data));
627 result = input.read_tag();
631 if(input.tag.title_is("ZOOMBLUR"))
633 config.x = input.tag.get_property("X", config.x);
634 config.y = input.tag.get_property("Y", config.y);
635 config.radius = input.tag.get_property("RADIUS", config.radius);
636 config.steps = input.tag.get_property("STEPS", config.steps);
637 config.r = input.tag.get_property("R", config.r);
638 config.g = input.tag.get_property("G", config.g);
639 config.b = input.tag.get_property("B", config.b);
640 config.a = input.tag.get_property("A", config.a);
651 ZoomBlurPackage::ZoomBlurPackage()
659 ZoomBlurUnit::ZoomBlurUnit(ZoomBlurEngine *server,
660 ZoomBlurMain *plugin)
663 this->plugin = plugin;
664 this->server = server;
668 #define BLEND_LAYER(COMPONENTS, TYPE, MAX, DO_YUV) \
670 const int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
671 for(int j = pkg->y1; j < pkg->y2; j++) \
673 int *out_row = plugin->accum + COMPONENTS * w * j; \
674 int in_y = y_table[j]; \
677 if(in_y >= 0 && in_y < h) \
679 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
680 for(int k = 0; k < w; k++) \
682 int in_x = x_table[k]; \
684 if(in_x >= 0 && in_x < w) \
686 int in_offset = in_x * COMPONENTS; \
687 *out_row++ += in_row[in_offset]; \
690 *out_row++ += in_row[in_offset + 1]; \
691 *out_row++ += in_row[in_offset + 2]; \
695 *out_row++ += (int)in_row[in_offset + 1]; \
696 *out_row++ += (int)in_row[in_offset + 2]; \
698 if(COMPONENTS == 4) \
699 *out_row++ += in_row[in_offset + 3]; \
701 /* Blend nothing */ \
707 *out_row++ += chroma_offset; \
708 *out_row++ += chroma_offset; \
714 if(COMPONENTS == 4) out_row++; \
721 for(int k = 0; k < w; k++) \
724 *out_row++ += chroma_offset; \
725 *out_row++ += chroma_offset; \
726 if(COMPONENTS == 4) out_row++; \
731 /* Copy to output */ \
732 if(i == plugin->config.steps - 1) \
734 for(int j = pkg->y1; j < pkg->y2; j++) \
736 int *in_row = plugin->accum + COMPONENTS * w * j; \
737 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
738 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
739 for(int k = 0; k < w; k++) \
743 *out_row++ = (*in_row++ * fraction) >> 16; \
748 *out_row++ = *in_backup++; \
756 *out_row++ = ((*in_row++ * fraction) >> 16); \
761 *out_row++ = *in_backup++; \
767 *out_row++ = ((*in_row++ * fraction) >> 16); \
772 *out_row++ = *in_backup++; \
780 *out_row++ = (*in_row++ * fraction) >> 16; \
785 *out_row++ = *in_backup++; \
791 *out_row++ = (*in_row++ * fraction) >> 16; \
796 *out_row++ = *in_backup++; \
801 if(COMPONENTS == 4) \
805 *out_row++ = (*in_row++ * fraction) >> 16; \
810 *out_row++ = *in_backup++; \
819 void ZoomBlurUnit::process_package(LoadPackage *package)
821 ZoomBlurPackage *pkg = (ZoomBlurPackage*)package;
822 int h = plugin->output->get_h();
823 int w = plugin->output->get_w();
824 int do_r = plugin->config.r;
825 int do_g = plugin->config.g;
826 int do_b = plugin->config.b;
827 int do_a = plugin->config.a;
829 int fraction = 0x10000 / plugin->config.steps;
830 for(int i = 0; i < plugin->config.steps; i++)
832 int *x_table = plugin->scale_x_table[i];
833 int *y_table = plugin->scale_y_table[i];
835 switch(plugin->input->get_color_model())
838 BLEND_LAYER(3, uint8_t, 0xff, 0)
841 BLEND_LAYER(4, uint8_t, 0xff, 0)
844 BLEND_LAYER(3, uint16_t, 0xffff, 0)
846 case BC_RGBA16161616:
847 BLEND_LAYER(4, uint16_t, 0xffff, 0)
850 BLEND_LAYER(3, uint8_t, 0xff, 1)
853 BLEND_LAYER(4, uint8_t, 0xff, 1)
856 BLEND_LAYER(3, uint16_t, 0xffff, 1)
858 case BC_YUVA16161616:
859 BLEND_LAYER(4, uint16_t, 0xffff, 1)
870 ZoomBlurEngine::ZoomBlurEngine(ZoomBlurMain *plugin,
873 : LoadServer(total_clients, total_packages)
875 this->plugin = plugin;
878 void ZoomBlurEngine::init_packages()
880 int package_h = (int)((float)plugin->output->get_h() /
883 for(int i = 0; i < total_packages; i++)
885 ZoomBlurPackage *package = (ZoomBlurPackage*)packages[i];
887 package->y2 = y1 + package_h;
888 package->y1 = MIN(plugin->output->get_h(), package->y1);
889 package->y2 = MIN(plugin->output->get_h(), package->y2);
894 LoadClient* ZoomBlurEngine::new_client()
896 return new ZoomBlurUnit(this, plugin);
899 LoadPackage* ZoomBlurEngine::new_package()
901 return new ZoomBlurPackage;