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"
43 class WhirlDefaultSettings;
49 #define RESET_DEFAULT_SETTINGS 10
51 #define RESET_RADIUS 1
60 void reset(int clear);
62 void copy_from(WhirlConfig &src);
63 int equivalent(WhirlConfig &src);
64 void interpolate(WhirlConfig &prev,
76 class WhirlAngle : public BC_FSlider
79 WhirlAngle(WhirlEffect *plugin, int x, int y);
86 class WhirlPinch : public BC_FSlider
89 WhirlPinch(WhirlEffect *plugin, int x, int y);
96 class WhirlRadius : public BC_FSlider
99 WhirlRadius(WhirlEffect *plugin, int x, int y);
105 class WhirlReset : public BC_GenericButton
108 WhirlReset(WhirlEffect *plugin, WhirlWindow *window, int x, int y);
115 class WhirlDefaultSettings : public BC_GenericButton
118 WhirlDefaultSettings(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w);
119 ~WhirlDefaultSettings();
125 class WhirlSliderClr : public BC_Button
128 WhirlSliderClr(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w, int clear);
138 class WhirlWindow : public PluginClientWindow
141 WhirlWindow(WhirlEffect *plugin);
142 void create_objects();
143 void update_gui(int clear);
149 WhirlDefaultSettings *default_settings;
150 WhirlSliderClr *radiusClr, *pinchClr, *angleClr;
157 class WhirlPackage : public LoadPackage
164 class WhirlUnit : public LoadClient
167 WhirlUnit(WhirlEffect *plugin, WhirlEngine *server);
168 void process_package(LoadPackage *package);
175 class WhirlEngine : public LoadServer
178 WhirlEngine(WhirlEffect *plugin, int cpus);
179 void init_packages();
180 LoadClient* new_client();
181 LoadPackage* new_package();
187 class WhirlEffect : public PluginVClient
190 WhirlEffect(PluginServer *server);
193 PLUGIN_CLASS_MEMBERS(WhirlConfig)
194 int process_realtime(VFrame *input, VFrame *output);
197 void save_data(KeyFrame *keyframe);
198 void read_data(KeyFrame *keyframe);
202 VFrame *input, *output;
203 int need_reconfigure;
211 REGISTER_PLUGIN(WhirlEffect)
226 WhirlConfig::WhirlConfig()
231 void WhirlConfig::reset(int clear)
239 case RESET_RADIUS : radius = 0.0;
241 case RESET_PINCH : pinch = 0.0;
243 case RESET_ANGLE : angle = 0.0;
245 case RESET_DEFAULT_SETTINGS :
254 void WhirlConfig::copy_from(WhirlConfig &src)
256 this->angle = src.angle;
257 this->pinch = src.pinch;
258 this->radius = src.radius;
261 int WhirlConfig::equivalent(WhirlConfig &src)
263 return EQUIV(this->angle, src.angle) &&
264 EQUIV(this->pinch, src.pinch) &&
265 EQUIV(this->radius, src.radius);
268 void WhirlConfig::interpolate(WhirlConfig &prev,
274 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
275 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
277 this->angle = prev.angle * prev_scale + next.angle * next_scale;
278 this->pinch = prev.pinch * prev_scale + next.pinch * next_scale;
279 this->radius = prev.radius * prev_scale + next.radius * next_scale;
291 WhirlWindow::WhirlWindow(WhirlEffect *plugin)
292 : PluginClientWindow(plugin, 280, 195, 280, 195, 0)
294 this->plugin = plugin;
299 void WhirlWindow::create_objects()
302 int x1 = 0; int clrBtn_w = 50;
303 int defaultBtn_w = 100;
305 add_subwindow(new BC_Title(x, y, _("Radius")));
307 add_subwindow(radius = new WhirlRadius(plugin, x, y));
308 x1 = x + radius->get_w() + 10;
309 add_subwindow(radiusClr = new WhirlSliderClr(plugin, this, x1, y, clrBtn_w, RESET_RADIUS));
312 add_subwindow(new BC_Title(x, y, _("Pinch")));
314 add_subwindow(pinch = new WhirlPinch(plugin, x, y));
315 add_subwindow(pinchClr = new WhirlSliderClr(plugin, this, x1, y, clrBtn_w, RESET_PINCH));
318 add_subwindow(new BC_Title(x, y, _("Angle")));
320 add_subwindow(angle = new WhirlAngle(plugin, x, y));
321 add_subwindow(angleClr = new WhirlSliderClr(plugin, this, x1, y, clrBtn_w, RESET_ANGLE));
324 add_subwindow(reset = new WhirlReset(plugin, this, x, y));
325 add_subwindow(default_settings = new WhirlDefaultSettings(plugin, this,
326 (280 - 10 - defaultBtn_w), y, defaultBtn_w));
334 void WhirlWindow::update_gui(int clear)
337 case RESET_RADIUS : radius->update(plugin->config.radius);
339 case RESET_PINCH : pinch->update(plugin->config.pinch);
341 case RESET_ANGLE : angle->update(plugin->config.angle);
344 case RESET_DEFAULT_SETTINGS :
346 radius->update(plugin->config.radius);
347 pinch->update(plugin->config.pinch);
348 angle->update(plugin->config.angle);
363 WhirlAngle::WhirlAngle(WhirlEffect *plugin, int x, int y)
371 plugin->config.angle)
373 this->plugin = plugin;
375 int WhirlAngle::handle_event()
377 plugin->config.angle = get_value();
378 plugin->send_configure_change();
385 WhirlPinch::WhirlPinch(WhirlEffect *plugin, int x, int y)
393 plugin->config.pinch)
395 this->plugin = plugin;
397 int WhirlPinch::handle_event()
399 plugin->config.pinch = get_value();
400 plugin->send_configure_change();
407 WhirlRadius::WhirlRadius(WhirlEffect *plugin, int x, int y)
415 plugin->config.radius)
417 this->plugin = plugin;
419 int WhirlRadius::handle_event()
421 plugin->config.radius = get_value();
422 plugin->send_configure_change();
428 WhirlReset::WhirlReset(WhirlEffect *plugin, WhirlWindow *window, int x, int y)
429 : BC_GenericButton(x, y, _("Reset"))
431 this->plugin = plugin;
432 this->window = window;
434 WhirlReset::~WhirlReset()
437 int WhirlReset::handle_event()
439 plugin->config.reset(RESET_ALL);
440 window->update_gui(RESET_ALL);
441 plugin->send_configure_change();
445 WhirlDefaultSettings::WhirlDefaultSettings(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w)
446 : BC_GenericButton(x, y, w, _("Default"))
448 this->plugin = plugin;
449 this->window = window;
451 WhirlDefaultSettings::~WhirlDefaultSettings()
454 int WhirlDefaultSettings::handle_event()
456 plugin->config.reset(RESET_DEFAULT_SETTINGS);
457 window->update_gui(RESET_DEFAULT_SETTINGS);
458 plugin->send_configure_change();
462 WhirlSliderClr::WhirlSliderClr(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w, int clear)
463 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
465 this->plugin = plugin;
466 this->window = window;
469 WhirlSliderClr::~WhirlSliderClr()
472 int WhirlSliderClr::handle_event()
474 // clear==1 ==> Radius slider
475 // clear==2 ==> Pinch slider
476 // clear==3 ==> Angle slider
477 plugin->config.reset(clear);
478 window->update_gui(clear);
479 plugin->send_configure_change();
487 WhirlEffect::WhirlEffect(PluginServer *server)
488 : PluginVClient(server)
490 need_reconfigure = 1;
496 WhirlEffect::~WhirlEffect()
499 if(engine) delete engine;
500 if(temp_frame) delete temp_frame;
508 const char* WhirlEffect::plugin_title() { return N_("Whirl"); }
509 int WhirlEffect::is_realtime() { return 1; }
513 NEW_WINDOW_MACRO(WhirlEffect, WhirlWindow)
516 void WhirlEffect::update_gui()
520 load_configuration();
521 thread->window->lock_window();
522 ((WhirlWindow*)thread->window)->angle->update(config.angle);
523 ((WhirlWindow*)thread->window)->pinch->update(config.pinch);
524 ((WhirlWindow*)thread->window)->radius->update(config.radius);
525 thread->window->unlock_window();
529 LOAD_CONFIGURATION_MACRO(WhirlEffect, WhirlConfig)
534 void WhirlEffect::save_data(KeyFrame *keyframe)
538 // cause data to be stored directly in text
539 output.set_shared_output(keyframe->xbuf);
541 output.tag.set_title("WHIRL");
542 output.tag.set_property("ANGLE", config.angle);
543 output.tag.set_property("PINCH", config.pinch);
544 output.tag.set_property("RADIUS", config.radius);
546 output.tag.set_title("/WHIRL");
548 output.append_newline();
549 output.terminate_string();
550 // data is now in *text
553 void WhirlEffect::read_data(KeyFrame *keyframe)
557 input.set_shared_input(keyframe->xbuf);
563 result = input.read_tag();
567 if(input.tag.title_is("WHIRL"))
569 config.angle = input.tag.get_property("ANGLE", config.angle);
570 config.pinch = input.tag.get_property("PINCH", config.pinch);
571 config.radius = input.tag.get_property("RADIUS", config.radius);
577 int WhirlEffect::process_realtime(VFrame *input, VFrame *output)
579 need_reconfigure |= load_configuration();
581 this->output = output;
583 if(EQUIV(config.angle, 0) ||
584 (EQUIV(config.radius, 0) && EQUIV(config.pinch, 0)))
586 if(input->get_rows()[0] != output->get_rows()[0])
587 output->copy_from(input);
591 if(input->get_rows()[0] == output->get_rows()[0])
593 if(!temp_frame) temp_frame = new VFrame(input->get_w(), input->get_h(),
594 input->get_color_model(), 0);
595 temp_frame->copy_from(input);
596 this->input = temp_frame;
599 if(!engine) engine = new WhirlEngine(this, PluginClient::smp + 1);
601 engine->process_packages();
613 WhirlPackage::WhirlPackage()
620 WhirlUnit::WhirlUnit(WhirlEffect *plugin, WhirlEngine *server)
623 this->plugin = plugin;
628 static int calc_undistorted_coords(double cen_x,
645 double ang, sina, cosa;
648 /* Distances to center, scaled */
650 dx = (wx - cen_x) * scale_x;
651 dy = (wy - cen_y) * scale_y;
653 /* Distance^2 to center of *circle* (scaled ellipse) */
655 d = dx * dx + dy * dy;
657 /* If we are inside circle, then distort.
658 * Else, just return the same position
661 inside = (d < radius2);
665 dist = sqrt(d / radius3) / radius;
669 factor = pow(sin(M_PI / 2 * dist), -pinch);
678 ang = whirl * factor * factor;
683 x = (cosa * dx - sina * dy) / scale_x + cen_x;
684 y = (sina * dx + cosa * dy) / scale_y + cen_y;
692 #define GET_PIXEL(components, x, y, input_rows) \
693 input_rows[CLIP(y, 0, (h - 1))] + components * CLIP(x, 0, (w - 1))
700 static float bilinear(double x, double y, double *values)
706 if(x < 0.0) x += 1.0;
707 if(y < 0.0) y += 1.0;
709 m0 = (double)values[0] + x * ((double)values[1] - values[0]);
710 m1 = (double)values[2] + x * ((double)values[3] - values[2]);
711 return m0 + y * (m1 - m0);
718 #define WHIRL_MACRO(type, max, components) \
720 type **input_rows = (type**)plugin->input->get_rows(); \
721 /* Compiler error requires separate arrays */ \
722 double top_values[4], bot_values[4]; \
723 for( int i=0; i<4; ++i ) top_values[i] = bot_values[i] = 0; \
724 for(int row = pkg->row1 / 2; row <= (pkg->row2 + pkg->row1) / 2; row++) \
726 type *top_row = (type*)plugin->output->get_rows()[row]; \
727 type *bot_row = (type*)plugin->output->get_rows()[h - row - 1]; \
728 type *top_p = top_row; \
729 type *bot_p = bot_row + components * w - components; \
735 for(int col = 0; col < w; col++) \
737 if(calc_undistorted_coords(cen_x, \
751 /* Inside distortion area */ \
756 ix = -((int)-cx + 1); \
761 iy = -((int)-cy + 1); \
763 pixel1 = GET_PIXEL(components, ix, iy, input_rows); \
764 pixel2 = GET_PIXEL(components, ix + 1, iy, input_rows); \
765 pixel3 = GET_PIXEL(components, ix, iy + 1, input_rows); \
766 pixel4 = GET_PIXEL(components, ix + 1, iy + 1, input_rows); \
768 top_values[0] = pixel1[0]; \
769 top_values[1] = pixel2[0]; \
770 top_values[2] = pixel3[0]; \
771 top_values[3] = pixel4[0]; \
772 top_p[0] = (type)bilinear(cx, cy, top_values); \
774 top_values[0] = pixel1[1]; \
775 top_values[1] = pixel2[1]; \
776 top_values[2] = pixel3[1]; \
777 top_values[3] = pixel4[1]; \
778 top_p[1] = (type)bilinear(cx, cy, top_values); \
780 top_values[0] = pixel1[2]; \
781 top_values[1] = pixel2[2]; \
782 top_values[2] = pixel3[2]; \
783 top_values[3] = pixel4[2]; \
784 top_p[2] = (type)bilinear(cx, cy, top_values); \
786 if(components == 4) \
788 top_values[0] = pixel1[3]; \
789 top_values[1] = pixel2[3]; \
790 top_values[2] = pixel3[3]; \
791 top_values[3] = pixel4[3]; \
792 top_p[3] = (type)bilinear(cx, cy, top_values); \
795 top_p += components; \
798 cx = cen_x + (cen_x - cx); \
799 cy = cen_y + (cen_y - cy); \
804 ix = -((int)-cx + 1); \
809 iy = -((int)-cy + 1); \
811 pixel1 = GET_PIXEL(components, ix, iy, input_rows); \
812 pixel2 = GET_PIXEL(components, ix + 1, iy, input_rows); \
813 pixel3 = GET_PIXEL(components, ix, iy + 1, input_rows); \
814 pixel4 = GET_PIXEL(components, ix + 1, iy + 1, input_rows); \
818 bot_values[0] = pixel1[0]; \
819 bot_values[1] = pixel2[0]; \
820 bot_values[2] = pixel3[0]; \
821 bot_values[3] = pixel4[0]; \
822 bot_p[0] = (type)bilinear(cx, cy, bot_values); \
824 bot_values[0] = pixel1[1]; \
825 bot_values[1] = pixel2[1]; \
826 bot_values[2] = pixel3[1]; \
827 bot_values[3] = pixel4[1]; \
828 bot_p[1] = (type)bilinear(cx, cy, bot_values); \
830 bot_values[0] = pixel1[2]; \
831 bot_values[1] = pixel2[2]; \
832 bot_values[2] = pixel3[2]; \
833 bot_values[3] = pixel4[2]; \
834 bot_p[2] = (type)bilinear(cx, cy, bot_values); \
836 if(components == 4) \
838 bot_values[0] = pixel1[3]; \
839 bot_values[1] = pixel2[3]; \
840 bot_values[2] = pixel3[3]; \
841 bot_values[3] = pixel4[3]; \
842 bot_p[3] = (type)bilinear(cx, cy, bot_values); \
845 bot_p -= components; \
851 /* Outside distortion area */ \
853 top_p[0] = input_rows[row][components * col + 0]; \
854 top_p[1] = input_rows[row][components * col + 1]; \
855 top_p[2] = input_rows[row][components * col + 2]; \
856 if(components == 4) top_p[3] = input_rows[row][components * col + 3]; \
859 top_p += components; \
862 int bot_offset = w * components - col * components - components; \
863 int bot_row = h - 1 - row; \
864 bot_p[0] = input_rows[bot_row][bot_offset + 0]; \
865 bot_p[1] = input_rows[bot_row][bot_offset + 1]; \
866 bot_p[2] = input_rows[bot_row][bot_offset + 2]; \
867 if(components == 4) bot_p[3] = input_rows[bot_row][bot_offset + 3]; \
868 bot_p -= components; \
874 void WhirlUnit::process_package(LoadPackage *package)
876 WhirlPackage *pkg = (WhirlPackage*)package;
877 int w = plugin->input->get_w();
878 int h = plugin->input->get_h();
879 double whirl = plugin->config.angle * M_PI / 180;
880 double pinch = plugin->config.pinch / MAXPINCH;
883 double cen_x = (double)(w - 1) / 2.0;
884 double cen_y = (double)(h - 1) / 2.0;
885 double radius = MAX(w, h);
886 double radius3 = plugin->config.radius / MAXRADIUS;
887 double radius2 = radius * radius * radius3;
892 //printf("WhirlUnit::process_package 1 %f %f %f\n",
893 // plugin->config.angle, plugin->config.pinch, plugin->config.radius);
896 scale_x = (double)h / w;
903 scale_y = (double)w / h;
913 switch(plugin->input->get_color_model())
916 WHIRL_MACRO(float, 1, 3);
920 WHIRL_MACRO(unsigned char, 0xff, 3);
923 WHIRL_MACRO(float, 1, 4);
927 WHIRL_MACRO(unsigned char, 0xff, 4);
931 WHIRL_MACRO(uint16_t, 0xffff, 3);
933 case BC_RGBA16161616:
934 case BC_YUVA16161616:
935 WHIRL_MACRO(uint16_t, 0xffff, 4);
947 WhirlEngine::WhirlEngine(WhirlEffect *plugin, int cpus)
948 // : LoadServer(1, 1)
949 : LoadServer(cpus, cpus)
951 this->plugin = plugin;
953 void WhirlEngine::init_packages()
955 for(int i = 0; i < LoadServer::get_total_packages(); i++)
957 WhirlPackage *pkg = (WhirlPackage*)get_package(i);
958 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
959 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
964 LoadClient* WhirlEngine::new_client()
966 return new WhirlUnit(plugin, this);
969 LoadPackage* WhirlEngine::new_package()
971 return new WhirlPackage;