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"
45 class WhirlDefaultSettings;
51 #define RESET_DEFAULT_SETTINGS 10
53 #define RESET_RADIUS 1
57 #define RADIUS_MIN 0.00
58 #define RADIUS_MAX 100.00
59 #define PINCH_MIN 0.00
60 #define PINCH_MAX 100.00
61 #define ANGLE_MIN 0.00
62 #define ANGLE_MAX 360.00
70 void reset(int clear);
72 void copy_from(WhirlConfig &src);
73 int equivalent(WhirlConfig &src);
74 void interpolate(WhirlConfig &prev,
86 class WhirlFText : public BC_TumbleTextBox
89 WhirlFText(WhirlWindow *window, WhirlEffect *plugin,
90 WhirlFSlider *slider, float *output, int x, int y, float min, float max);
101 class WhirlFSlider : public BC_FSlider
104 WhirlFSlider(WhirlEffect *plugin,
105 WhirlFText *text, float *output, int x, int y,
106 float min, float max);
115 class WhirlReset : public BC_GenericButton
118 WhirlReset(WhirlEffect *plugin, WhirlWindow *window, int x, int y);
125 class WhirlDefaultSettings : public BC_GenericButton
128 WhirlDefaultSettings(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w);
129 ~WhirlDefaultSettings();
135 class WhirlClr : public BC_Button
138 WhirlClr(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int clear);
148 class WhirlWindow : public PluginClientWindow
151 WhirlWindow(WhirlEffect *plugin);
152 void create_objects();
153 void update_gui(int clear);
156 WhirlFText *radius_text;
157 WhirlFSlider *radius_slider;
158 WhirlClr *radius_Clr;
160 WhirlFText *pinch_text;
161 WhirlFSlider *pinch_slider;
164 WhirlFText *angle_text;
165 WhirlFSlider *angle_slider;
169 WhirlDefaultSettings *default_settings;
176 class WhirlPackage : public LoadPackage
183 class WhirlUnit : public LoadClient
186 WhirlUnit(WhirlEffect *plugin, WhirlEngine *server);
187 void process_package(LoadPackage *package);
194 class WhirlEngine : public LoadServer
197 WhirlEngine(WhirlEffect *plugin, int cpus);
198 void init_packages();
199 LoadClient* new_client();
200 LoadPackage* new_package();
206 class WhirlEffect : public PluginVClient
209 WhirlEffect(PluginServer *server);
212 PLUGIN_CLASS_MEMBERS(WhirlConfig)
213 int process_realtime(VFrame *input, VFrame *output);
216 void save_data(KeyFrame *keyframe);
217 void read_data(KeyFrame *keyframe);
221 VFrame *input, *output;
222 int need_reconfigure;
230 REGISTER_PLUGIN(WhirlEffect)
245 WhirlConfig::WhirlConfig()
250 void WhirlConfig::reset(int clear)
258 case RESET_RADIUS : radius = 0.0;
260 case RESET_PINCH : pinch = 0.0;
262 case RESET_ANGLE : angle = 0.0;
264 case RESET_DEFAULT_SETTINGS :
273 void WhirlConfig::copy_from(WhirlConfig &src)
275 this->angle = src.angle;
276 this->pinch = src.pinch;
277 this->radius = src.radius;
280 int WhirlConfig::equivalent(WhirlConfig &src)
282 return EQUIV(this->angle, src.angle) &&
283 EQUIV(this->pinch, src.pinch) &&
284 EQUIV(this->radius, src.radius);
287 void WhirlConfig::interpolate(WhirlConfig &prev,
293 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
294 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
296 this->angle = prev.angle * prev_scale + next.angle * next_scale;
297 this->pinch = prev.pinch * prev_scale + next.pinch * next_scale;
298 this->radius = prev.radius * prev_scale + next.radius * next_scale;
310 WhirlWindow::WhirlWindow(WhirlEffect *plugin)
311 : PluginClientWindow(plugin, xS(420), yS(160), xS(420), yS(160), 0)
313 this->plugin = plugin;
318 void WhirlWindow::create_objects()
320 int xs10 = xS(10), xs100 = xS(100);
321 int ys10 = yS(10), ys30 = yS(30), ys40 = yS(40);
322 int x = xs10, y = ys10;
323 int x2 = xS(80), x3 = xS(180);
324 int clr_x = get_w()-x - xS(22); // note: clrBtn_w = 22
325 int defaultBtn_w = xs100;
330 add_subwindow(new BC_Title(x, y, _("Radius:")));
331 radius_text = new WhirlFText(this, plugin,
332 0, &plugin->config.radius, (x + x2), y, RADIUS_MIN, RADIUS_MAX);
333 radius_text->create_objects();
334 radius_slider = new WhirlFSlider(plugin,
335 radius_text, &plugin->config.radius, x3, y, RADIUS_MIN, RADIUS_MAX);
336 add_subwindow(radius_slider);
337 radius_text->slider = radius_slider;
338 clr_x = x3 + radius_slider->get_w() + x;
339 add_subwindow(radius_Clr = new WhirlClr(plugin, this, clr_x, y, RESET_RADIUS));
342 add_subwindow(new BC_Title(x, y, _("Pinch:")));
343 pinch_text = new WhirlFText(this, plugin,
344 0, &plugin->config.pinch, (x + x2), y, PINCH_MIN, PINCH_MAX);
345 pinch_text->create_objects();
346 pinch_slider = new WhirlFSlider(plugin,
347 pinch_text, &plugin->config.pinch, x3, y, PINCH_MIN, PINCH_MAX);
348 add_subwindow(pinch_slider);
349 pinch_text->slider = pinch_slider;
350 add_subwindow(pinch_Clr = new WhirlClr(plugin, this, clr_x, y, RESET_PINCH));
353 add_subwindow(new BC_Title(x, y, _("Angle:")));
354 angle_text = new WhirlFText(this, plugin,
355 0, &plugin->config.angle, (x + x2), y, ANGLE_MIN, ANGLE_MAX);
356 angle_text->create_objects();
357 angle_slider = new WhirlFSlider(plugin,
358 angle_text, &plugin->config.angle, x3, y, ANGLE_MIN, ANGLE_MAX);
359 add_subwindow(angle_slider);
360 angle_text->slider = angle_slider;
361 add_subwindow(angle_Clr = new WhirlClr(plugin, this, clr_x, y, RESET_ANGLE));
365 add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
367 add_subwindow(reset = new WhirlReset(plugin, this, x, y));
368 add_subwindow(default_settings = new WhirlDefaultSettings(plugin, this,
369 (get_w() - xs10 - defaultBtn_w), y, defaultBtn_w));
377 void WhirlWindow::update_gui(int clear)
381 radius_text->update(plugin->config.radius);
382 radius_slider->update(plugin->config.radius);
385 pinch_text->update(plugin->config.pinch);
386 pinch_slider->update(plugin->config.pinch);
389 angle_text->update(plugin->config.angle);
390 angle_slider->update(plugin->config.angle);
393 case RESET_DEFAULT_SETTINGS :
395 radius_text->update(plugin->config.radius);
396 radius_slider->update(plugin->config.radius);
397 pinch_text->update(plugin->config.pinch);
398 pinch_slider->update(plugin->config.pinch);
399 angle_text->update(plugin->config.angle);
400 angle_slider->update(plugin->config.angle);
410 WhirlFText::WhirlFText(WhirlWindow *window, WhirlEffect *plugin,
411 WhirlFSlider *slider, float *output, int x, int y, float min, float max)
412 : BC_TumbleTextBox(window, *output,
413 min, max, x, y, xS(60), 2)
415 this->window = window;
416 this->plugin = plugin;
417 this->output = output;
418 this->slider = slider;
424 WhirlFText::~WhirlFText()
428 int WhirlFText::handle_event()
430 *output = atof(get_text());
431 if(*output > max) *output = max;
432 if(*output < min) *output = min;
433 slider->update(*output);
434 plugin->send_configure_change();
439 WhirlFSlider::WhirlFSlider(WhirlEffect *plugin,
440 WhirlFText *text, float *output, int x, int y, float min, float max)
441 : BC_FSlider(x, y, 0, xS(200), xS(200), min, max, *output)
443 this->plugin = plugin;
444 this->output = output;
446 enable_show_value(0); // Hide caption
449 WhirlFSlider::~WhirlFSlider()
453 int WhirlFSlider::handle_event()
455 *output = get_value();
456 text->update(*output);
457 plugin->send_configure_change();
462 WhirlReset::WhirlReset(WhirlEffect *plugin, WhirlWindow *window, int x, int y)
463 : BC_GenericButton(x, y, _("Reset"))
465 this->plugin = plugin;
466 this->window = window;
468 WhirlReset::~WhirlReset()
471 int WhirlReset::handle_event()
473 plugin->config.reset(RESET_ALL);
474 window->update_gui(RESET_ALL);
475 plugin->send_configure_change();
479 WhirlDefaultSettings::WhirlDefaultSettings(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int w)
480 : BC_GenericButton(x, y, w, _("Default"))
482 this->plugin = plugin;
483 this->window = window;
485 WhirlDefaultSettings::~WhirlDefaultSettings()
488 int WhirlDefaultSettings::handle_event()
490 plugin->config.reset(RESET_DEFAULT_SETTINGS);
491 window->update_gui(RESET_DEFAULT_SETTINGS);
492 plugin->send_configure_change();
496 WhirlClr::WhirlClr(WhirlEffect *plugin, WhirlWindow *window, int x, int y, int clear)
497 : BC_Button(x, y, plugin->get_theme()->get_image_set("reset_button"))
499 this->plugin = plugin;
500 this->window = window;
503 WhirlClr::~WhirlClr()
506 int WhirlClr::handle_event()
508 // clear==1 ==> Radius slider
509 // clear==2 ==> Pinch slider
510 // clear==3 ==> Angle slider
511 plugin->config.reset(clear);
512 window->update_gui(clear);
513 plugin->send_configure_change();
521 WhirlEffect::WhirlEffect(PluginServer *server)
522 : PluginVClient(server)
524 need_reconfigure = 1;
530 WhirlEffect::~WhirlEffect()
533 if(engine) delete engine;
534 if(temp_frame) delete temp_frame;
542 const char* WhirlEffect::plugin_title() { return N_("Whirl"); }
543 int WhirlEffect::is_realtime() { return 1; }
547 NEW_WINDOW_MACRO(WhirlEffect, WhirlWindow)
550 void WhirlEffect::update_gui()
554 load_configuration();
555 thread->window->lock_window();
556 ((WhirlWindow*)thread->window)->angle_text->update(config.angle);
557 ((WhirlWindow*)thread->window)->angle_slider->update(config.angle);
558 ((WhirlWindow*)thread->window)->pinch_text->update(config.pinch);
559 ((WhirlWindow*)thread->window)->pinch_slider->update(config.pinch);
560 ((WhirlWindow*)thread->window)->radius_text->update(config.radius);
561 ((WhirlWindow*)thread->window)->radius_slider->update(config.radius);
562 thread->window->unlock_window();
566 LOAD_CONFIGURATION_MACRO(WhirlEffect, WhirlConfig)
571 void WhirlEffect::save_data(KeyFrame *keyframe)
575 // cause data to be stored directly in text
576 output.set_shared_output(keyframe->xbuf);
578 output.tag.set_title("WHIRL");
579 output.tag.set_property("ANGLE", config.angle);
580 output.tag.set_property("PINCH", config.pinch);
581 output.tag.set_property("RADIUS", config.radius);
583 output.tag.set_title("/WHIRL");
585 output.append_newline();
586 output.terminate_string();
587 // data is now in *text
590 void WhirlEffect::read_data(KeyFrame *keyframe)
594 input.set_shared_input(keyframe->xbuf);
600 result = input.read_tag();
604 if(input.tag.title_is("WHIRL"))
606 config.angle = input.tag.get_property("ANGLE", config.angle);
607 config.pinch = input.tag.get_property("PINCH", config.pinch);
608 config.radius = input.tag.get_property("RADIUS", config.radius);
614 int WhirlEffect::process_realtime(VFrame *input, VFrame *output)
616 need_reconfigure |= load_configuration();
618 this->output = output;
620 if(EQUIV(config.angle, 0) ||
621 (EQUIV(config.radius, 0) && EQUIV(config.pinch, 0)))
623 if(input->get_rows()[0] != output->get_rows()[0])
624 output->copy_from(input);
628 if(input->get_rows()[0] == output->get_rows()[0])
630 if(!temp_frame) temp_frame = new VFrame(input->get_w(), input->get_h(),
631 input->get_color_model(), 0);
632 temp_frame->copy_from(input);
633 this->input = temp_frame;
636 if(!engine) engine = new WhirlEngine(this, PluginClient::smp + 1);
638 engine->process_packages();
650 WhirlPackage::WhirlPackage()
657 WhirlUnit::WhirlUnit(WhirlEffect *plugin, WhirlEngine *server)
660 this->plugin = plugin;
665 static int calc_undistorted_coords(double cen_x,
682 double ang, sina, cosa;
685 /* Distances to center, scaled */
687 dx = (wx - cen_x) * scale_x;
688 dy = (wy - cen_y) * scale_y;
690 /* Distance^2 to center of *circle* (scaled ellipse) */
692 d = dx * dx + dy * dy;
694 /* If we are inside circle, then distort.
695 * Else, just return the same position
698 inside = (d < radius2);
702 dist = sqrt(d / radius3) / radius;
706 factor = pow(sin(M_PI / 2 * dist), -pinch);
715 ang = whirl * factor * factor;
720 x = (cosa * dx - sina * dy) / scale_x + cen_x;
721 y = (sina * dx + cosa * dy) / scale_y + cen_y;
729 #define GET_PIXEL(components, x, y, input_rows) \
730 input_rows[CLIP(y, 0, (h - 1))] + components * CLIP(x, 0, (w - 1))
737 static float bilinear(double x, double y, double *values)
743 if(x < 0.0) x += 1.0;
744 if(y < 0.0) y += 1.0;
746 m0 = (double)values[0] + x * ((double)values[1] - values[0]);
747 m1 = (double)values[2] + x * ((double)values[3] - values[2]);
748 return m0 + y * (m1 - m0);
755 #define WHIRL_MACRO(type, max, components) \
757 type **input_rows = (type**)plugin->input->get_rows(); \
758 /* Compiler error requires separate arrays */ \
759 double top_values[4], bot_values[4]; \
760 for( int i=0; i<4; ++i ) top_values[i] = bot_values[i] = 0; \
761 for(int row = pkg->row1 / 2; row <= (pkg->row2 + pkg->row1) / 2; row++) \
763 type *top_row = (type*)plugin->output->get_rows()[row]; \
764 type *bot_row = (type*)plugin->output->get_rows()[h - row - 1]; \
765 type *top_p = top_row; \
766 type *bot_p = bot_row + components * w - components; \
772 for(int col = 0; col < w; col++) \
774 if(calc_undistorted_coords(cen_x, \
788 /* Inside distortion area */ \
793 ix = -((int)-cx + 1); \
798 iy = -((int)-cy + 1); \
800 pixel1 = GET_PIXEL(components, ix, iy, input_rows); \
801 pixel2 = GET_PIXEL(components, ix + 1, iy, input_rows); \
802 pixel3 = GET_PIXEL(components, ix, iy + 1, input_rows); \
803 pixel4 = GET_PIXEL(components, ix + 1, iy + 1, input_rows); \
805 top_values[0] = pixel1[0]; \
806 top_values[1] = pixel2[0]; \
807 top_values[2] = pixel3[0]; \
808 top_values[3] = pixel4[0]; \
809 top_p[0] = (type)bilinear(cx, cy, top_values); \
811 top_values[0] = pixel1[1]; \
812 top_values[1] = pixel2[1]; \
813 top_values[2] = pixel3[1]; \
814 top_values[3] = pixel4[1]; \
815 top_p[1] = (type)bilinear(cx, cy, top_values); \
817 top_values[0] = pixel1[2]; \
818 top_values[1] = pixel2[2]; \
819 top_values[2] = pixel3[2]; \
820 top_values[3] = pixel4[2]; \
821 top_p[2] = (type)bilinear(cx, cy, top_values); \
823 if(components == 4) \
825 top_values[0] = pixel1[3]; \
826 top_values[1] = pixel2[3]; \
827 top_values[2] = pixel3[3]; \
828 top_values[3] = pixel4[3]; \
829 top_p[3] = (type)bilinear(cx, cy, top_values); \
832 top_p += components; \
835 cx = cen_x + (cen_x - cx); \
836 cy = cen_y + (cen_y - cy); \
841 ix = -((int)-cx + 1); \
846 iy = -((int)-cy + 1); \
848 pixel1 = GET_PIXEL(components, ix, iy, input_rows); \
849 pixel2 = GET_PIXEL(components, ix + 1, iy, input_rows); \
850 pixel3 = GET_PIXEL(components, ix, iy + 1, input_rows); \
851 pixel4 = GET_PIXEL(components, ix + 1, iy + 1, input_rows); \
855 bot_values[0] = pixel1[0]; \
856 bot_values[1] = pixel2[0]; \
857 bot_values[2] = pixel3[0]; \
858 bot_values[3] = pixel4[0]; \
859 bot_p[0] = (type)bilinear(cx, cy, bot_values); \
861 bot_values[0] = pixel1[1]; \
862 bot_values[1] = pixel2[1]; \
863 bot_values[2] = pixel3[1]; \
864 bot_values[3] = pixel4[1]; \
865 bot_p[1] = (type)bilinear(cx, cy, bot_values); \
867 bot_values[0] = pixel1[2]; \
868 bot_values[1] = pixel2[2]; \
869 bot_values[2] = pixel3[2]; \
870 bot_values[3] = pixel4[2]; \
871 bot_p[2] = (type)bilinear(cx, cy, bot_values); \
873 if(components == 4) \
875 bot_values[0] = pixel1[3]; \
876 bot_values[1] = pixel2[3]; \
877 bot_values[2] = pixel3[3]; \
878 bot_values[3] = pixel4[3]; \
879 bot_p[3] = (type)bilinear(cx, cy, bot_values); \
882 bot_p -= components; \
888 /* Outside distortion area */ \
890 top_p[0] = input_rows[row][components * col + 0]; \
891 top_p[1] = input_rows[row][components * col + 1]; \
892 top_p[2] = input_rows[row][components * col + 2]; \
893 if(components == 4) top_p[3] = input_rows[row][components * col + 3]; \
896 top_p += components; \
899 int bot_offset = w * components - col * components - components; \
900 int bot_row = h - 1 - row; \
901 bot_p[0] = input_rows[bot_row][bot_offset + 0]; \
902 bot_p[1] = input_rows[bot_row][bot_offset + 1]; \
903 bot_p[2] = input_rows[bot_row][bot_offset + 2]; \
904 if(components == 4) bot_p[3] = input_rows[bot_row][bot_offset + 3]; \
905 bot_p -= components; \
911 void WhirlUnit::process_package(LoadPackage *package)
913 WhirlPackage *pkg = (WhirlPackage*)package;
914 int w = plugin->input->get_w();
915 int h = plugin->input->get_h();
916 double whirl = plugin->config.angle * M_PI / 180;
917 double pinch = plugin->config.pinch / MAXPINCH;
920 double cen_x = (double)(w-1) / 2.0;
921 double cen_y = (double)(h-1) / 2.0;
922 double radius = MAX(w, h);
923 double radius3 = plugin->config.radius / MAXRADIUS;
924 double radius2 = radius * radius * radius3;
929 //printf("WhirlUnit::process_package 1 %f %f %f\n",
930 // plugin->config.angle, plugin->config.pinch, plugin->config.radius);
933 scale_x = (double)h / w;
940 scale_y = (double)w / h;
950 switch(plugin->input->get_color_model())
953 WHIRL_MACRO(float, 1, 3);
957 WHIRL_MACRO(unsigned char, 0xff, 3);
960 WHIRL_MACRO(float, 1, 4);
964 WHIRL_MACRO(unsigned char, 0xff, 4);
968 WHIRL_MACRO(uint16_t, 0xffff, 3);
970 case BC_RGBA16161616:
971 case BC_YUVA16161616:
972 WHIRL_MACRO(uint16_t, 0xffff, 4);
984 WhirlEngine::WhirlEngine(WhirlEffect *plugin, int cpus)
985 // : LoadServer(1, 1)
986 : LoadServer(cpus, cpus)
988 this->plugin = plugin;
990 void WhirlEngine::init_packages()
992 for(int i = 0; i < LoadServer::get_total_packages(); i++)
994 WhirlPackage *pkg = (WhirlPackage*)get_package(i);
995 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
996 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
1001 LoadClient* WhirlEngine::new_client()
1003 return new WhirlUnit(plugin, this);
1006 LoadPackage* WhirlEngine::new_package()
1008 return new WhirlPackage;