1 #include "bcdisplayinfo.h"
8 #include "loadbalance.h"
10 #include "pluginvclient.h"
35 void copy_from(WhirlConfig &src);
36 int equivalent(WhirlConfig &src);
37 void interpolate(WhirlConfig &prev,
49 class WhirlAngle : public BC_FSlider
52 WhirlAngle(WhirlEffect *plugin, int x, int y);
59 class WhirlPinch : public BC_FSlider
62 WhirlPinch(WhirlEffect *plugin, int x, int y);
69 class WhirlRadius : public BC_FSlider
72 WhirlRadius(WhirlEffect *plugin, int x, int y);
77 class WhirlWindow : public BC_Window
80 WhirlWindow(WhirlEffect *plugin, int x, int y);
81 void create_objects();
90 PLUGIN_THREAD_HEADER(WhirlEffect, WhirlThread, WhirlWindow)
93 class WhirlPackage : public LoadPackage
100 class WhirlUnit : public LoadClient
103 WhirlUnit(WhirlEffect *plugin, WhirlEngine *server);
104 void process_package(LoadPackage *package);
111 class WhirlEngine : public LoadServer
114 WhirlEngine(WhirlEffect *plugin, int cpus);
115 void init_packages();
116 LoadClient* new_client();
117 LoadPackage* new_package();
123 class WhirlEffect : public PluginVClient
126 WhirlEffect(PluginServer *server);
129 int process_realtime(VFrame *input, VFrame *output);
131 char* plugin_title();
137 int load_configuration();
140 void save_data(KeyFrame *keyframe);
141 void read_data(KeyFrame *keyframe);
145 VFrame *input, *output;
149 int need_reconfigure;
155 PLUGIN_THREAD_OBJECT(WhirlEffect, WhirlThread, WhirlWindow)
158 REGISTER_PLUGIN(WhirlEffect)
175 WhirlConfig::WhirlConfig()
182 void WhirlConfig::copy_from(WhirlConfig &src)
184 this->angle = src.angle;
185 this->pinch = src.pinch;
186 this->radius = src.radius;
189 int WhirlConfig::equivalent(WhirlConfig &src)
191 return EQUIV(this->angle, src.angle) &&
192 EQUIV(this->pinch, src.pinch) &&
193 EQUIV(this->radius, src.radius);
196 void WhirlConfig::interpolate(WhirlConfig &prev,
202 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
203 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
205 this->angle = prev.angle * prev_scale + next.angle * next_scale;
206 this->pinch = prev.pinch * prev_scale + next.pinch * next_scale;
207 this->radius = prev.radius * prev_scale + next.radius * next_scale;
219 WhirlWindow::WhirlWindow(WhirlEffect *plugin, int x, int y)
220 : BC_Window(plugin->gui_string,
231 this->plugin = plugin;
236 void WhirlWindow::create_objects()
239 add_subwindow(new BC_Title(x, y, _("Radius")));
241 add_subwindow(radius = new WhirlRadius(plugin, x, y));
243 add_subwindow(new BC_Title(x, y, _("Pinch")));
245 add_subwindow(pinch = new WhirlPinch(plugin, x, y));
247 add_subwindow(new BC_Title(x, y, _("Angle")));
249 add_subwindow(angle = new WhirlAngle(plugin, x, y));
255 int WhirlWindow::close_event()
273 WhirlAngle::WhirlAngle(WhirlEffect *plugin, int x, int y)
281 plugin->config.angle)
283 this->plugin = plugin;
285 int WhirlAngle::handle_event()
287 plugin->config.angle = get_value();
288 plugin->send_configure_change();
295 WhirlPinch::WhirlPinch(WhirlEffect *plugin, int x, int y)
303 plugin->config.pinch)
305 this->plugin = plugin;
307 int WhirlPinch::handle_event()
309 plugin->config.pinch = get_value();
310 plugin->send_configure_change();
317 WhirlRadius::WhirlRadius(WhirlEffect *plugin, int x, int y)
325 plugin->config.radius)
327 this->plugin = plugin;
329 int WhirlRadius::handle_event()
331 plugin->config.radius = get_value();
332 plugin->send_configure_change();
346 WhirlEffect::WhirlEffect(PluginServer *server)
347 : PluginVClient(server)
349 need_reconfigure = 1;
352 PLUGIN_CONSTRUCTOR_MACRO
355 WhirlEffect::~WhirlEffect()
357 PLUGIN_DESTRUCTOR_MACRO
358 if(engine) delete engine;
359 if(temp_frame) delete temp_frame;
367 char* WhirlEffect::plugin_title() { return N_("Whirl"); }
368 int WhirlEffect::is_realtime() { return 1; }
370 NEW_PICON_MACRO(WhirlEffect)
372 SHOW_GUI_MACRO(WhirlEffect, WhirlThread)
374 RAISE_WINDOW_MACRO(WhirlEffect)
376 SET_STRING_MACRO(WhirlEffect)
378 void WhirlEffect::update_gui()
382 load_configuration();
383 thread->window->lock_window();
384 thread->window->angle->update(config.angle);
385 thread->window->pinch->update(config.pinch);
386 thread->window->radius->update(config.radius);
387 thread->window->unlock_window();
391 LOAD_CONFIGURATION_MACRO(WhirlEffect, WhirlConfig)
396 int WhirlEffect::load_defaults()
398 char directory[1024], string[1024];
399 // set the default directory
400 sprintf(directory, "%swhirl.rc", BCASTDIR);
403 defaults = new BC_Hash(directory);
406 config.angle = defaults->get("ANGLE", config.angle);
407 config.pinch = defaults->get("PINCH", config.pinch);
408 config.radius = defaults->get("RADIUS", config.radius);
411 int WhirlEffect::save_defaults()
413 defaults->update("ANGLE", config.angle);
414 defaults->update("PINCH", config.pinch);
415 defaults->update("RADIUS", config.radius);
419 void WhirlEffect::save_data(KeyFrame *keyframe)
423 // cause data to be stored directly in text
424 output.set_shared_string(keyframe->data, MESSAGESIZE);
426 output.tag.set_title("WHIRL");
427 output.tag.set_property("ANGLE", config.angle);
428 output.tag.set_property("PINCH", config.pinch);
429 output.tag.set_property("RADIUS", config.radius);
431 output.terminate_string();
432 // data is now in *text
435 void WhirlEffect::read_data(KeyFrame *keyframe)
439 input.set_shared_string(keyframe->data, strlen(keyframe->data));
445 result = input.read_tag();
449 if(input.tag.title_is("WHIRL"))
451 config.angle = input.tag.get_property("ANGLE", config.angle);
452 config.pinch = input.tag.get_property("PINCH", config.pinch);
453 config.radius = input.tag.get_property("RADIUS", config.radius);
459 int WhirlEffect::process_realtime(VFrame *input, VFrame *output)
461 need_reconfigure |= load_configuration();
463 this->output = output;
465 if(EQUIV(config.angle, 0) ||
466 (EQUIV(config.radius, 0) && EQUIV(config.pinch, 0)))
468 if(input->get_rows()[0] != output->get_rows()[0])
469 output->copy_from(input);
473 if(input->get_rows()[0] == output->get_rows()[0])
475 if(!temp_frame) temp_frame = new VFrame(0,
478 input->get_color_model());
479 temp_frame->copy_from(input);
480 this->input = temp_frame;
481 //printf("WhirlEffect::process_realtime 1\n");
484 if(!engine) engine = new WhirlEngine(this, PluginClient::smp + 1);
486 engine->process_packages();
497 WhirlPackage::WhirlPackage()
504 WhirlUnit::WhirlUnit(WhirlEffect *plugin, WhirlEngine *server)
507 this->plugin = plugin;
512 static int calc_undistorted_coords(double cen_x,
529 double ang, sina, cosa;
532 /* Distances to center, scaled */
534 dx = (wx - cen_x) * scale_x;
535 dy = (wy - cen_y) * scale_y;
537 /* Distance^2 to center of *circle* (scaled ellipse) */
539 d = dx * dx + dy * dy;
541 /* If we are inside circle, then distort.
542 * Else, just return the same position
545 inside = (d < radius2);
549 dist = sqrt(d / radius3) / radius;
553 factor = pow(sin(M_PI / 2 * dist), -pinch);
562 ang = whirl * factor * factor;
567 x = (cosa * dx - sina * dy) / scale_x + cen_x;
568 y = (sina * dx + cosa * dy) / scale_y + cen_y;
576 #define GET_PIXEL(components, x, y, input_rows) \
577 input_rows[CLIP(y, 0, (h - 1))] + components * CLIP(x, 0, (w - 1))
584 static float bilinear(double x, double y, double *values)
590 if(x < 0.0) x += 1.0;
591 if(y < 0.0) y += 1.0;
593 m0 = (double)values[0] + x * ((double)values[1] - values[0]);
594 m1 = (double)values[2] + x * ((double)values[3] - values[2]);
595 return m0 + y * (m1 - m0);
602 #define WHIRL_MACRO(type, max, components) \
604 type **input_rows = (type**)plugin->input->get_rows(); \
605 double values[components]; \
606 for(int row = pkg->row1; row <= (pkg->row2 + pkg->row1) / 2; row++) \
608 type *top_row = (type*)plugin->output->get_rows()[row]; \
609 type *bot_row = (type*)plugin->output->get_rows()[h - row - 1]; \
610 type *top_p = top_row; \
611 type *bot_p = bot_row + components * w - components; \
613 for(int col = 0; col < w; col++) \
615 if(calc_undistorted_coords(cen_x, \
629 /* Inside distortion area */ \
634 ix = -((int)-cx + 1); \
639 iy = -((int)-cy + 1); \
641 type *pixel1 = GET_PIXEL(components, ix, iy, input_rows); \
642 type *pixel2 = GET_PIXEL(components, ix + 1, iy, input_rows); \
643 type *pixel3 = GET_PIXEL(components, ix, iy + 1, input_rows); \
644 type *pixel4 = GET_PIXEL(components, ix + 1, iy + 1, input_rows); \
646 values[0] = pixel1[0]; \
647 values[1] = pixel2[0]; \
648 values[2] = pixel3[0]; \
649 values[3] = pixel4[0]; \
650 top_p[0] = (type)bilinear(cx, cy, values); \
652 values[0] = pixel1[1]; \
653 values[1] = pixel2[1]; \
654 values[2] = pixel3[1]; \
655 values[3] = pixel4[1]; \
656 top_p[1] = (type)bilinear(cx, cy, values); \
658 values[0] = pixel1[2]; \
659 values[1] = pixel2[2]; \
660 values[2] = pixel3[2]; \
661 values[3] = pixel4[2]; \
662 top_p[2] = (type)bilinear(cx, cy, values); \
664 if(components == 4) \
666 values[0] = pixel1[3]; \
667 values[1] = pixel2[3]; \
668 values[2] = pixel3[3]; \
669 values[3] = pixel4[3]; \
670 top_p[3] = (type)bilinear(cx, cy, values); \
673 top_p += components; \
676 cx = cen_x + (cen_x - cx); \
677 cy = cen_y + (cen_y - cy); \
682 ix = -((int)-cx + 1); \
687 iy = -((int)-cy + 1); \
689 pixel1 = GET_PIXEL(components, ix, iy, input_rows); \
690 pixel2 = GET_PIXEL(components, ix + 1, iy, input_rows); \
691 pixel3 = GET_PIXEL(components, ix, iy + 1, input_rows); \
692 pixel4 = GET_PIXEL(components, ix + 1, iy + 1, input_rows); \
696 values[0] = pixel1[0]; \
697 values[1] = pixel2[0]; \
698 values[2] = pixel3[0]; \
699 values[3] = pixel4[0]; \
700 bot_p[0] = (type)bilinear(cx, cy, values); \
702 values[0] = pixel1[1]; \
703 values[1] = pixel2[1]; \
704 values[2] = pixel3[1]; \
705 values[3] = pixel4[1]; \
706 bot_p[1] = (type)bilinear(cx, cy, values); \
708 values[0] = pixel1[2]; \
709 values[1] = pixel2[2]; \
710 values[2] = pixel3[2]; \
711 values[3] = pixel4[2]; \
712 bot_p[2] = (type)bilinear(cx, cy, values); \
714 if(components == 4) \
716 values[0] = pixel1[3]; \
717 values[1] = pixel2[3]; \
718 values[2] = pixel3[3]; \
719 values[3] = pixel4[3]; \
720 bot_p[3] = (type)bilinear(cx, cy, values); \
723 bot_p -= components; \
729 /* Outside distortion area */ \
731 top_p[0] = input_rows[row][components * col + 0]; \
732 top_p[1] = input_rows[row][components * col + 1]; \
733 top_p[2] = input_rows[row][components * col + 2]; \
734 if(components == 4) top_p[3] = input_rows[row][components * col + 3]; \
737 top_p += components; \
740 int bot_offset = w * components - col * components - components; \
741 int bot_row = h - 1 - row; \
742 bot_p[0] = input_rows[bot_row][bot_offset + 0]; \
743 bot_p[1] = input_rows[bot_row][bot_offset + 1]; \
744 bot_p[2] = input_rows[bot_row][bot_offset + 2]; \
745 if(components == 4) bot_p[3] = input_rows[bot_row][bot_offset + 3]; \
746 bot_p -= components; \
752 void WhirlUnit::process_package(LoadPackage *package)
754 WhirlPackage *pkg = (WhirlPackage*)package;
755 int w = plugin->input->get_w();
756 int h = plugin->input->get_h();
757 double whirl = plugin->config.angle * M_PI / 180;
758 double pinch = plugin->config.pinch / MAXPINCH;
761 double cen_x = (double)(w - 1) / 2.0;
762 double cen_y = (double)(h - 1) / 2.0;
763 double radius = MAX(w, h);
764 double radius3 = plugin->config.radius / MAXRADIUS;
765 double radius2 = radius * radius * radius3;
770 //printf("WhirlUnit::process_package 1 %f %f %f\n",
771 // plugin->config.angle, plugin->config.pinch, plugin->config.radius);
774 scale_x = (double)h / w;
781 scale_y = (double)w / h;
791 switch(plugin->input->get_color_model())
794 WHIRL_MACRO(float, 1, 3);
798 WHIRL_MACRO(unsigned char, 0xff, 3);
801 WHIRL_MACRO(float, 1, 4);
805 WHIRL_MACRO(unsigned char, 0xff, 4);
809 WHIRL_MACRO(uint16_t, 0xffff, 3);
811 case BC_RGBA16161616:
812 case BC_YUVA16161616:
813 WHIRL_MACRO(uint16_t, 0xffff, 4);
825 WhirlEngine::WhirlEngine(WhirlEffect *plugin, int cpus)
826 : LoadServer(cpus, cpus)
828 this->plugin = plugin;
830 void WhirlEngine::init_packages()
832 for(int i = 0; i < LoadServer::get_total_packages(); i++)
834 WhirlPackage *pkg = (WhirlPackage*)get_package(i);
835 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
836 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
841 LoadClient* WhirlEngine::new_client()
843 return new WhirlUnit(plugin, this);
846 LoadPackage* WhirlEngine::new_package()
848 return new WhirlPackage;