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
22 #include "bcdisplayinfo.h"
29 #include "loadbalance.h"
30 #include "pluginvclient.h"
42 class WhirlDefaultSettings;
48 #define RESET_DEFAULT_SETTINGS 10
50 #define RESET_RADIUS 1
59 void reset(int clear);
61 void copy_from(WhirlConfig &src);
62 int equivalent(WhirlConfig &src);
63 void interpolate(WhirlConfig &prev,
75 class WhirlAngle : public BC_FSlider
78 WhirlAngle(WhirlEffect *plugin, int x, int y);
85 class WhirlPinch : public BC_FSlider
88 WhirlPinch(WhirlEffect *plugin, int x, int y);
95 class WhirlRadius : public BC_FSlider
98 WhirlRadius(WhirlEffect *plugin, int x, int y);
104 class WhirlReset : public BC_GenericButton
107 WhirlReset(WhirlEffect *plugin, WhirlWindow *window, int x, int y);
114 class WhirlDefaultSettings : public BC_GenericButton
117 WhirlDefaultSettings(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w);
118 ~WhirlDefaultSettings();
124 class WhirlSliderClr : public BC_GenericButton
127 WhirlSliderClr(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w, int clear);
137 class WhirlWindow : public PluginClientWindow
140 WhirlWindow(WhirlEffect *plugin);
141 void create_objects();
142 void update_gui(int clear);
148 WhirlDefaultSettings *default_settings;
149 WhirlSliderClr *radiusClr, *pinchClr, *angleClr;
156 class WhirlPackage : public LoadPackage
163 class WhirlUnit : public LoadClient
166 WhirlUnit(WhirlEffect *plugin, WhirlEngine *server);
167 void process_package(LoadPackage *package);
174 class WhirlEngine : public LoadServer
177 WhirlEngine(WhirlEffect *plugin, int cpus);
178 void init_packages();
179 LoadClient* new_client();
180 LoadPackage* new_package();
186 class WhirlEffect : public PluginVClient
189 WhirlEffect(PluginServer *server);
192 PLUGIN_CLASS_MEMBERS(WhirlConfig)
193 int process_realtime(VFrame *input, VFrame *output);
196 void save_data(KeyFrame *keyframe);
197 void read_data(KeyFrame *keyframe);
201 VFrame *input, *output;
202 int need_reconfigure;
210 REGISTER_PLUGIN(WhirlEffect)
225 WhirlConfig::WhirlConfig()
230 void WhirlConfig::reset(int clear)
238 case RESET_RADIUS : radius = 0.0;
240 case RESET_PINCH : pinch = 0.0;
242 case RESET_ANGLE : angle = 0.0;
244 case RESET_DEFAULT_SETTINGS :
253 void WhirlConfig::copy_from(WhirlConfig &src)
255 this->angle = src.angle;
256 this->pinch = src.pinch;
257 this->radius = src.radius;
260 int WhirlConfig::equivalent(WhirlConfig &src)
262 return EQUIV(this->angle, src.angle) &&
263 EQUIV(this->pinch, src.pinch) &&
264 EQUIV(this->radius, src.radius);
267 void WhirlConfig::interpolate(WhirlConfig &prev,
273 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
274 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
276 this->angle = prev.angle * prev_scale + next.angle * next_scale;
277 this->pinch = prev.pinch * prev_scale + next.pinch * next_scale;
278 this->radius = prev.radius * prev_scale + next.radius * next_scale;
290 WhirlWindow::WhirlWindow(WhirlEffect *plugin)
291 : PluginClientWindow(plugin, 280, 195, 280, 195, 0)
293 this->plugin = plugin;
298 void WhirlWindow::create_objects()
301 int x1 = 0; int clrBtn_w = 50;
302 int defaultBtn_w = 100;
304 add_subwindow(new BC_Title(x, y, _("Radius")));
306 add_subwindow(radius = new WhirlRadius(plugin, x, y));
307 x1 = x + radius->get_w() + 10;
308 add_subwindow(radiusClr = new WhirlSliderClr(plugin, this, x1, y, clrBtn_w, RESET_RADIUS));
311 add_subwindow(new BC_Title(x, y, _("Pinch")));
313 add_subwindow(pinch = new WhirlPinch(plugin, x, y));
314 add_subwindow(pinchClr = new WhirlSliderClr(plugin, this, x1, y, clrBtn_w, RESET_PINCH));
317 add_subwindow(new BC_Title(x, y, _("Angle")));
319 add_subwindow(angle = new WhirlAngle(plugin, x, y));
320 add_subwindow(angleClr = new WhirlSliderClr(plugin, this, x1, y, clrBtn_w, RESET_ANGLE));
323 add_subwindow(reset = new WhirlReset(plugin, this, x, y));
324 add_subwindow(default_settings = new WhirlDefaultSettings(plugin, this,
325 (280 - 10 - defaultBtn_w), y, defaultBtn_w));
333 void WhirlWindow::update_gui(int clear)
336 case RESET_RADIUS : radius->update(plugin->config.radius);
338 case RESET_PINCH : pinch->update(plugin->config.pinch);
340 case RESET_ANGLE : angle->update(plugin->config.angle);
343 case RESET_DEFAULT_SETTINGS :
345 radius->update(plugin->config.radius);
346 pinch->update(plugin->config.pinch);
347 angle->update(plugin->config.angle);
362 WhirlAngle::WhirlAngle(WhirlEffect *plugin, int x, int y)
370 plugin->config.angle)
372 this->plugin = plugin;
374 int WhirlAngle::handle_event()
376 plugin->config.angle = get_value();
377 plugin->send_configure_change();
384 WhirlPinch::WhirlPinch(WhirlEffect *plugin, int x, int y)
392 plugin->config.pinch)
394 this->plugin = plugin;
396 int WhirlPinch::handle_event()
398 plugin->config.pinch = get_value();
399 plugin->send_configure_change();
406 WhirlRadius::WhirlRadius(WhirlEffect *plugin, int x, int y)
414 plugin->config.radius)
416 this->plugin = plugin;
418 int WhirlRadius::handle_event()
420 plugin->config.radius = get_value();
421 plugin->send_configure_change();
427 WhirlReset::WhirlReset(WhirlEffect *plugin, WhirlWindow *window, int x, int y)
428 : BC_GenericButton(x, y, _("Reset"))
430 this->plugin = plugin;
431 this->window = window;
433 WhirlReset::~WhirlReset()
436 int WhirlReset::handle_event()
438 plugin->config.reset(RESET_ALL);
439 window->update_gui(RESET_ALL);
440 plugin->send_configure_change();
444 WhirlDefaultSettings::WhirlDefaultSettings(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w)
445 : BC_GenericButton(x, y, w, _("Default"))
447 this->plugin = plugin;
448 this->window = window;
450 WhirlDefaultSettings::~WhirlDefaultSettings()
453 int WhirlDefaultSettings::handle_event()
455 plugin->config.reset(RESET_DEFAULT_SETTINGS);
456 window->update_gui(RESET_DEFAULT_SETTINGS);
457 plugin->send_configure_change();
461 WhirlSliderClr::WhirlSliderClr(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w, int clear)
462 : BC_GenericButton(x, y, w, _("⌂"))
464 this->plugin = plugin;
465 this->window = window;
468 WhirlSliderClr::~WhirlSliderClr()
471 int WhirlSliderClr::handle_event()
473 // clear==1 ==> Radius slider
474 // clear==2 ==> Pinch slider
475 // clear==3 ==> Angle slider
476 plugin->config.reset(clear);
477 window->update_gui(clear);
478 plugin->send_configure_change();
486 WhirlEffect::WhirlEffect(PluginServer *server)
487 : PluginVClient(server)
489 need_reconfigure = 1;
495 WhirlEffect::~WhirlEffect()
498 if(engine) delete engine;
499 if(temp_frame) delete temp_frame;
507 const char* WhirlEffect::plugin_title() { return N_("Whirl"); }
508 int WhirlEffect::is_realtime() { return 1; }
512 NEW_WINDOW_MACRO(WhirlEffect, WhirlWindow)
515 void WhirlEffect::update_gui()
519 load_configuration();
520 thread->window->lock_window();
521 ((WhirlWindow*)thread->window)->angle->update(config.angle);
522 ((WhirlWindow*)thread->window)->pinch->update(config.pinch);
523 ((WhirlWindow*)thread->window)->radius->update(config.radius);
524 thread->window->unlock_window();
528 LOAD_CONFIGURATION_MACRO(WhirlEffect, WhirlConfig)
533 void WhirlEffect::save_data(KeyFrame *keyframe)
537 // cause data to be stored directly in text
538 output.set_shared_output(keyframe->xbuf);
540 output.tag.set_title("WHIRL");
541 output.tag.set_property("ANGLE", config.angle);
542 output.tag.set_property("PINCH", config.pinch);
543 output.tag.set_property("RADIUS", config.radius);
545 output.tag.set_title("/WHIRL");
547 output.append_newline();
548 output.terminate_string();
549 // data is now in *text
552 void WhirlEffect::read_data(KeyFrame *keyframe)
556 input.set_shared_input(keyframe->xbuf);
562 result = input.read_tag();
566 if(input.tag.title_is("WHIRL"))
568 config.angle = input.tag.get_property("ANGLE", config.angle);
569 config.pinch = input.tag.get_property("PINCH", config.pinch);
570 config.radius = input.tag.get_property("RADIUS", config.radius);
576 int WhirlEffect::process_realtime(VFrame *input, VFrame *output)
578 need_reconfigure |= load_configuration();
580 this->output = output;
582 if(EQUIV(config.angle, 0) ||
583 (EQUIV(config.radius, 0) && EQUIV(config.pinch, 0)))
585 if(input->get_rows()[0] != output->get_rows()[0])
586 output->copy_from(input);
590 if(input->get_rows()[0] == output->get_rows()[0])
592 if(!temp_frame) temp_frame = new VFrame(input->get_w(), input->get_h(),
593 input->get_color_model(), 0);
594 temp_frame->copy_from(input);
595 this->input = temp_frame;
598 if(!engine) engine = new WhirlEngine(this, PluginClient::smp + 1);
600 engine->process_packages();
612 WhirlPackage::WhirlPackage()
619 WhirlUnit::WhirlUnit(WhirlEffect *plugin, WhirlEngine *server)
622 this->plugin = plugin;
627 static int calc_undistorted_coords(double cen_x,
644 double ang, sina, cosa;
647 /* Distances to center, scaled */
649 dx = (wx - cen_x) * scale_x;
650 dy = (wy - cen_y) * scale_y;
652 /* Distance^2 to center of *circle* (scaled ellipse) */
654 d = dx * dx + dy * dy;
656 /* If we are inside circle, then distort.
657 * Else, just return the same position
660 inside = (d < radius2);
664 dist = sqrt(d / radius3) / radius;
668 factor = pow(sin(M_PI / 2 * dist), -pinch);
677 ang = whirl * factor * factor;
682 x = (cosa * dx - sina * dy) / scale_x + cen_x;
683 y = (sina * dx + cosa * dy) / scale_y + cen_y;
691 #define GET_PIXEL(components, x, y, input_rows) \
692 input_rows[CLIP(y, 0, (h - 1))] + components * CLIP(x, 0, (w - 1))
699 static float bilinear(double x, double y, double *values)
705 if(x < 0.0) x += 1.0;
706 if(y < 0.0) y += 1.0;
708 m0 = (double)values[0] + x * ((double)values[1] - values[0]);
709 m1 = (double)values[2] + x * ((double)values[3] - values[2]);
710 return m0 + y * (m1 - m0);
717 #define WHIRL_MACRO(type, max, components) \
719 type **input_rows = (type**)plugin->input->get_rows(); \
720 /* Compiler error requires separate arrays */ \
721 double top_values[4], bot_values[4]; \
722 for( int i=0; i<4; ++i ) top_values[i] = bot_values[i] = 0; \
723 for(int row = pkg->row1 / 2; row <= (pkg->row2 + pkg->row1) / 2; row++) \
725 type *top_row = (type*)plugin->output->get_rows()[row]; \
726 type *bot_row = (type*)plugin->output->get_rows()[h - row - 1]; \
727 type *top_p = top_row; \
728 type *bot_p = bot_row + components * w - components; \
734 for(int col = 0; col < w; col++) \
736 if(calc_undistorted_coords(cen_x, \
750 /* Inside distortion area */ \
755 ix = -((int)-cx + 1); \
760 iy = -((int)-cy + 1); \
762 pixel1 = GET_PIXEL(components, ix, iy, input_rows); \
763 pixel2 = GET_PIXEL(components, ix + 1, iy, input_rows); \
764 pixel3 = GET_PIXEL(components, ix, iy + 1, input_rows); \
765 pixel4 = GET_PIXEL(components, ix + 1, iy + 1, input_rows); \
767 top_values[0] = pixel1[0]; \
768 top_values[1] = pixel2[0]; \
769 top_values[2] = pixel3[0]; \
770 top_values[3] = pixel4[0]; \
771 top_p[0] = (type)bilinear(cx, cy, top_values); \
773 top_values[0] = pixel1[1]; \
774 top_values[1] = pixel2[1]; \
775 top_values[2] = pixel3[1]; \
776 top_values[3] = pixel4[1]; \
777 top_p[1] = (type)bilinear(cx, cy, top_values); \
779 top_values[0] = pixel1[2]; \
780 top_values[1] = pixel2[2]; \
781 top_values[2] = pixel3[2]; \
782 top_values[3] = pixel4[2]; \
783 top_p[2] = (type)bilinear(cx, cy, top_values); \
785 if(components == 4) \
787 top_values[0] = pixel1[3]; \
788 top_values[1] = pixel2[3]; \
789 top_values[2] = pixel3[3]; \
790 top_values[3] = pixel4[3]; \
791 top_p[3] = (type)bilinear(cx, cy, top_values); \
794 top_p += components; \
797 cx = cen_x + (cen_x - cx); \
798 cy = cen_y + (cen_y - cy); \
803 ix = -((int)-cx + 1); \
808 iy = -((int)-cy + 1); \
810 pixel1 = GET_PIXEL(components, ix, iy, input_rows); \
811 pixel2 = GET_PIXEL(components, ix + 1, iy, input_rows); \
812 pixel3 = GET_PIXEL(components, ix, iy + 1, input_rows); \
813 pixel4 = GET_PIXEL(components, ix + 1, iy + 1, input_rows); \
817 bot_values[0] = pixel1[0]; \
818 bot_values[1] = pixel2[0]; \
819 bot_values[2] = pixel3[0]; \
820 bot_values[3] = pixel4[0]; \
821 bot_p[0] = (type)bilinear(cx, cy, bot_values); \
823 bot_values[0] = pixel1[1]; \
824 bot_values[1] = pixel2[1]; \
825 bot_values[2] = pixel3[1]; \
826 bot_values[3] = pixel4[1]; \
827 bot_p[1] = (type)bilinear(cx, cy, bot_values); \
829 bot_values[0] = pixel1[2]; \
830 bot_values[1] = pixel2[2]; \
831 bot_values[2] = pixel3[2]; \
832 bot_values[3] = pixel4[2]; \
833 bot_p[2] = (type)bilinear(cx, cy, bot_values); \
835 if(components == 4) \
837 bot_values[0] = pixel1[3]; \
838 bot_values[1] = pixel2[3]; \
839 bot_values[2] = pixel3[3]; \
840 bot_values[3] = pixel4[3]; \
841 bot_p[3] = (type)bilinear(cx, cy, bot_values); \
844 bot_p -= components; \
850 /* Outside distortion area */ \
852 top_p[0] = input_rows[row][components * col + 0]; \
853 top_p[1] = input_rows[row][components * col + 1]; \
854 top_p[2] = input_rows[row][components * col + 2]; \
855 if(components == 4) top_p[3] = input_rows[row][components * col + 3]; \
858 top_p += components; \
861 int bot_offset = w * components - col * components - components; \
862 int bot_row = h - 1 - row; \
863 bot_p[0] = input_rows[bot_row][bot_offset + 0]; \
864 bot_p[1] = input_rows[bot_row][bot_offset + 1]; \
865 bot_p[2] = input_rows[bot_row][bot_offset + 2]; \
866 if(components == 4) bot_p[3] = input_rows[bot_row][bot_offset + 3]; \
867 bot_p -= components; \
873 void WhirlUnit::process_package(LoadPackage *package)
875 WhirlPackage *pkg = (WhirlPackage*)package;
876 int w = plugin->input->get_w();
877 int h = plugin->input->get_h();
878 double whirl = plugin->config.angle * M_PI / 180;
879 double pinch = plugin->config.pinch / MAXPINCH;
882 double cen_x = (double)(w - 1) / 2.0;
883 double cen_y = (double)(h - 1) / 2.0;
884 double radius = MAX(w, h);
885 double radius3 = plugin->config.radius / MAXRADIUS;
886 double radius2 = radius * radius * radius3;
891 //printf("WhirlUnit::process_package 1 %f %f %f\n",
892 // plugin->config.angle, plugin->config.pinch, plugin->config.radius);
895 scale_x = (double)h / w;
902 scale_y = (double)w / h;
912 switch(plugin->input->get_color_model())
915 WHIRL_MACRO(float, 1, 3);
919 WHIRL_MACRO(unsigned char, 0xff, 3);
922 WHIRL_MACRO(float, 1, 4);
926 WHIRL_MACRO(unsigned char, 0xff, 4);
930 WHIRL_MACRO(uint16_t, 0xffff, 3);
932 case BC_RGBA16161616:
933 case BC_YUVA16161616:
934 WHIRL_MACRO(uint16_t, 0xffff, 4);
946 WhirlEngine::WhirlEngine(WhirlEffect *plugin, int cpus)
947 // : LoadServer(1, 1)
948 : LoadServer(cpus, cpus)
950 this->plugin = plugin;
952 void WhirlEngine::init_packages()
954 for(int i = 0; i < LoadServer::get_total_packages(); i++)
956 WhirlPackage *pkg = (WhirlPackage*)get_package(i);
957 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
958 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
963 LoadClient* WhirlEngine::new_client()
965 return new WhirlUnit(plugin, this);
968 LoadPackage* WhirlEngine::new_package()
970 return new WhirlPackage;