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"
39 #define WITHIN(a, b, c) ((((a) <= (b)) && ((b) <= (c))) ? 1 : 0)
57 void reset(int clear);
58 void copy_from(PolarConfig &src);
59 int equivalent(PolarConfig &src);
60 void interpolate(PolarConfig &prev,
66 int polar_to_rectangular;
75 class PolarDepth : public BC_FSlider
78 PolarDepth(PolarEffect *plugin, int x, int y);
83 class PolarAngle : public BC_FSlider
86 PolarAngle(PolarEffect *plugin, int x, int y);
91 class PolarReset : public BC_GenericButton
94 PolarReset(PolarEffect *plugin, PolarWindow *window, int x, int y);
101 class PolarSliderClr : public BC_GenericButton
104 PolarSliderClr(PolarEffect *plugin, PolarWindow *window, int x, int y, int w, int clear);
112 class PolarWindow : public PluginClientWindow
115 PolarWindow(PolarEffect *plugin);
116 void create_objects();
117 void update_gui(int clear);
122 PolarSliderClr *depthClr;
123 PolarSliderClr *angleClr;
130 class PolarPackage : public LoadPackage
137 class PolarUnit : public LoadClient
140 PolarUnit(PolarEffect *plugin, PolarEngine *server);
141 void process_package(LoadPackage *package);
145 class PolarEngine : public LoadServer
148 PolarEngine(PolarEffect *plugin, int cpus);
149 void init_packages();
150 LoadClient* new_client();
151 LoadPackage* new_package();
155 class PolarEffect : public PluginVClient
158 PolarEffect(PluginServer *server);
161 PLUGIN_CLASS_MEMBERS(PolarConfig)
162 int process_realtime(VFrame *input, VFrame *output);
164 void save_data(KeyFrame *keyframe);
165 void read_data(KeyFrame *keyframe);
170 VFrame *input, *output;
171 int need_reconfigure;
176 REGISTER_PLUGIN(PolarEffect)
180 PolarConfig::PolarConfig()
185 void PolarConfig::reset(int clear)
188 case RESET_DEPTH : depth = 1.0;
190 case RESET_ANGLE : angle = 1.0;
198 polar_to_rectangular = 1;
203 void PolarConfig::copy_from(PolarConfig &src)
205 this->angle = src.angle;
206 this->depth = src.depth;
207 this->backwards = src.backwards;
208 this->invert = src.invert;
209 this->polar_to_rectangular = src.polar_to_rectangular;
212 int PolarConfig::equivalent(PolarConfig &src)
214 return EQUIV(this->angle, src.angle) && EQUIV(this->depth, src.depth);
217 void PolarConfig::interpolate(PolarConfig &prev,
223 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
224 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
226 this->depth = prev.depth * prev_scale + next.depth * next_scale;
227 this->angle = prev.angle * prev_scale + next.angle * next_scale;
235 PolarWindow::PolarWindow(PolarEffect *plugin)
236 : PluginClientWindow(plugin,
243 this->plugin = plugin;
246 void PolarWindow::create_objects()
248 int x = 10, y = 10, x1 = x + 50;
249 int x2 = 0; int clrBtn_w = 50;
251 add_subwindow(new BC_Title(x, y, _("Depth:")));
252 add_subwindow(depth = new PolarDepth(plugin, x1, y));
253 x2 = x1 + depth->get_w() + 10;
254 add_subwindow(depthClr = new PolarSliderClr(plugin, this, x2, y, clrBtn_w, RESET_DEPTH));
257 add_subwindow(new BC_Title(x, y, _("Angle:")));
258 add_subwindow(angle = new PolarAngle(plugin, x1, y));
259 add_subwindow(angleClr = new PolarSliderClr(plugin, this, x2, y, clrBtn_w, RESET_ANGLE));
261 add_subwindow(reset = new PolarReset(plugin, this, x, y));
268 void PolarWindow::update_gui(int clear)
271 case RESET_DEPTH : depth->update(plugin->config.depth);
273 case RESET_ANGLE : angle->update(plugin->config.angle);
277 depth->update(plugin->config.depth);
278 angle->update(plugin->config.angle);
287 PolarDepth::PolarDepth(PolarEffect *plugin, int x, int y)
295 plugin->config.depth)
297 this->plugin = plugin;
299 int PolarDepth::handle_event()
301 plugin->config.depth = get_value();
302 plugin->send_configure_change();
310 PolarAngle::PolarAngle(PolarEffect *plugin, int x, int y)
318 plugin->config.angle)
320 this->plugin = plugin;
322 int PolarAngle::handle_event()
324 plugin->config.angle = get_value();
325 plugin->send_configure_change();
331 PolarReset::PolarReset(PolarEffect *plugin, PolarWindow *window, int x, int y)
332 : BC_GenericButton(x, y, _("Reset"))
334 this->plugin = plugin;
335 this->window = window;
337 PolarReset::~PolarReset()
340 int PolarReset::handle_event()
342 plugin->config.reset(RESET_ALL);
343 window->update_gui(RESET_ALL);
344 plugin->send_configure_change();
349 PolarSliderClr::PolarSliderClr(PolarEffect *plugin, PolarWindow *window, int x, int y, int w, int clear)
350 : BC_GenericButton(x, y, w, _("⌂"))
352 this->plugin = plugin;
353 this->window = window;
356 PolarSliderClr::~PolarSliderClr()
359 int PolarSliderClr::handle_event()
361 // clear==1 ==> Depth slider
362 // clear==2 ==> Angle slider
363 plugin->config.reset(clear);
364 window->update_gui(clear);
365 plugin->send_configure_change();
372 PolarEffect::PolarEffect(PluginServer *server)
373 : PluginVClient(server)
375 need_reconfigure = 1;
381 PolarEffect::~PolarEffect()
384 if(temp_frame) delete temp_frame;
385 if(engine) delete engine;
390 const char* PolarEffect::plugin_title() { return N_("Polar"); }
391 int PolarEffect::is_realtime() { return 1; }
396 NEW_WINDOW_MACRO(PolarEffect, PolarWindow)
398 void PolarEffect::update_gui()
402 load_configuration();
403 thread->window->lock_window();
404 ((PolarWindow*)thread->window)->angle->update(config.angle);
405 ((PolarWindow*)thread->window)->depth->update(config.depth);
406 thread->window->unlock_window();
410 LOAD_CONFIGURATION_MACRO(PolarEffect, PolarConfig)
414 void PolarEffect::save_data(KeyFrame *keyframe)
418 // cause data to be stored directly in text
419 output.set_shared_output(keyframe->xbuf);
420 output.tag.set_title("POLAR");
421 output.tag.set_property("DEPTH", config.depth);
422 output.tag.set_property("ANGLE", config.angle);
424 output.tag.set_title("/POLAR");
426 output.append_newline();
427 output.terminate_string();
430 void PolarEffect::read_data(KeyFrame *keyframe)
434 input.set_shared_input(keyframe->xbuf);
436 while(!input.read_tag())
438 if(input.tag.title_is("POLAR"))
440 config.depth = input.tag.get_property("DEPTH", config.depth);
441 config.angle = input.tag.get_property("ANGLE", config.angle);
446 int PolarEffect::process_realtime(VFrame *input, VFrame *output)
448 need_reconfigure |= load_configuration();
451 this->output = output;
453 if(EQUIV(config.depth, 0) || EQUIV(config.angle, 0))
455 if(input->get_rows()[0] != output->get_rows()[0])
456 output->copy_from(input);
460 if(input->get_rows()[0] == output->get_rows()[0])
463 temp_frame = new VFrame(input->get_w(), input->get_h(),
464 input->get_color_model(), 0);
465 temp_frame->copy_from(input);
466 this->input = temp_frame;
470 if(!engine) engine = new PolarEngine(this, PluginClient::smp + 1);
472 engine->process_packages();
481 PolarPackage::PolarPackage()
489 PolarUnit::PolarUnit(PolarEffect *plugin, PolarEngine *server)
492 this->plugin = plugin;
496 static int calc_undistorted_coords(int wx,
502 int polar_to_rectangular,
512 double xx, xm, ym, yy;
516 double xmax, ymax, rmax;
517 double x_calc, y_calc;
519 double circle, angl, t;
537 angl = (double)angle / 180.0 * M_PI;
539 if(polar_to_rectangular)
546 atan(((double)(wx - cen_x)) /
547 ((double)(wy - cen_y)));
548 r = sqrt(SQR(wx - cen_x) +
554 phi = atan(((double)(wx - cen_x)) /
555 ((double)(cen_y - wy)));
556 r = sqrt(SQR(wx - cen_x) +
571 atan(((double)(cen_x -wx)) /
572 ((double)(cen_y - wy)));
573 r = sqrt(SQR(cen_x - wx) +
580 atan(((double)(cen_x - wx)) /
581 ((double)(wy - cen_y)));
582 r = sqrt(SQR(cen_x - wx) +
593 m = fabs(((double)(wy - cen_y)) /
594 ((double)(wx - cen_x)));
601 if(m <= ((double)(y2 - y1) /
621 rmax = sqrt((double)(SQR(xmax) + SQR(ymax)));
623 t = ((cen_y - y1) < (cen_x - x1)) ? (cen_y - y1) : (cen_x - x1);
624 rmax = (rmax - t) / 100 * (100 - circle) + t;
626 phi = fmod(phi + angl, 2 * M_PI);
629 x_calc = x2 - 1 - (x2 - x1 - 1) / (2 * M_PI) * phi;
631 x_calc = (x2 - x1 - 1) / (2 * M_PI) * phi + x1;
634 y_calc = (y2 - y1) / rmax * r + y1;
636 y_calc = y2 - (y2 - y1) / rmax * r;
638 xi = (int)(x_calc + 0.5);
639 yi = (int)(y_calc + 0.5);
641 if(WITHIN(0, xi, w - 1) && WITHIN(0, yi, h - 1))
656 phi = (2 * M_PI) * (x2 - wx) / xdiff;
658 phi = (2 * M_PI) * (wx - x1) / xdiff;
660 phi = fmod (phi + angl, 2 * M_PI);
662 if(phi >= 1.5 * M_PI)
663 phi2 = 2 * M_PI - phi;
668 if(phi >= 0.5 * M_PI)
675 m = (double)1.0 / xx;
679 if(m <= ((double)(ydiff) / (double)(xdiff)))
698 rmax = sqrt((double)(SQR(xmax) + SQR(ymax)));
700 t = ((ym - y1) < (xm - x1)) ? (ym - y1) : (xm - x1);
702 rmax = (rmax - t) / 100.0 * (100 - circle) + t;
705 r = rmax * (double)((wy - y1) / (double)(ydiff));
707 r = rmax * (double)((y2 - wy) / (double)(ydiff));
712 if(phi >= 1.5 * M_PI)
714 x_calc = (double)xm - xx;
715 y_calc = (double)ym - yy;
720 x_calc = (double)xm - xx;
721 y_calc = (double)ym + yy;
724 if(phi >= 0.5 * M_PI)
726 x_calc = (double)xm + xx;
727 y_calc = (double)ym + yy;
731 x_calc = (double)xm + xx;
732 y_calc = (double)ym - yy;
735 xi = (int)(x_calc + 0.5);
736 yi = (int)(y_calc + 0.5);
738 if(WITHIN(0, xi, w - 1) &&
739 WITHIN(0, yi, h - 1))
755 static double bilinear(double x, double y, double *values)
761 if(x < 0.0) x += 1.0;
762 if(y < 0.0) y += 1.0;
764 m0 = values[0] + x * (values[1] - values[0]);
765 m1 = values[2] + x * (values[3] - values[2]);
766 return m0 + y * (m1 - m0);
769 #define GET_PIXEL(x, y, components, input_rows) \
770 input_rows[CLIP((y), 0, ((h) - 1))] + components * CLIP((x), 0, ((w) - 1))
772 #define POLAR_MACRO(type, max, components, chroma_offset) \
774 type **in_rows = (type**)plugin->input->get_rows(); \
775 type **out_rows = (type**)plugin->output->get_rows(); \
778 for(int y = pkg->row1; y < pkg->row2; y++) \
780 type *output_row = out_rows[y]; \
782 for(int x = 0; x < w; x++) \
784 type *output_pixel = output_row + x * components; \
785 if(calc_undistorted_coords(x, \
789 plugin->config.depth, \
790 plugin->config.angle, \
791 plugin->config.polar_to_rectangular, \
792 plugin->config.backwards, \
793 plugin->config.invert, \
799 type *pixel1 = GET_PIXEL((int)cx, (int)cy, components, in_rows); \
800 type *pixel2 = GET_PIXEL((int)cx + 1, (int)cy, components, in_rows); \
801 type *pixel3 = GET_PIXEL((int)cx, (int)cy + 1, components, in_rows); \
802 type *pixel4 = GET_PIXEL((int)cx + 1, (int)cy + 1, components, in_rows); \
804 values[0] = pixel1[0]; \
805 values[1] = pixel2[0]; \
806 values[2] = pixel3[0]; \
807 values[3] = pixel4[0]; \
808 output_pixel[0] = (type)bilinear(cx, cy, values); \
810 values[0] = pixel1[1]; \
811 values[1] = pixel2[1]; \
812 values[2] = pixel3[1]; \
813 values[3] = pixel4[1]; \
814 output_pixel[1] = (type)bilinear(cx, cy, values); \
816 values[0] = pixel1[2]; \
817 values[1] = pixel2[2]; \
818 values[2] = pixel3[2]; \
819 values[3] = pixel4[2]; \
820 output_pixel[2] = (type)bilinear(cx, cy, values); \
822 if(components == 4) \
824 values[0] = pixel1[3]; \
825 values[1] = pixel2[3]; \
826 values[2] = pixel3[3]; \
827 values[3] = pixel4[3]; \
828 output_pixel[3] = (type)bilinear(cx, cy, values); \
833 output_pixel[0] = 0; \
834 output_pixel[1] = chroma_offset; \
835 output_pixel[2] = chroma_offset; \
836 if(components == 4) output_pixel[3] = max; \
843 void PolarUnit::process_package(LoadPackage *package)
845 PolarPackage *pkg = (PolarPackage*)package;
846 int w = plugin->input->get_w();
847 int h = plugin->input->get_h();
850 double cen_x = (double)(w - 1) / 2.0;
851 double cen_y = (double)(h - 1) / 2.0;
853 switch(plugin->input->get_color_model())
856 POLAR_MACRO(float, 1, 3, 0x0)
859 POLAR_MACRO(float, 1, 4, 0x0)
862 POLAR_MACRO(unsigned char, 0xff, 3, 0x0)
865 POLAR_MACRO(unsigned char, 0xff, 4, 0x0)
868 POLAR_MACRO(uint16_t, 0xffff, 3, 0x0)
870 case BC_RGBA16161616:
871 POLAR_MACRO(uint16_t, 0xffff, 4, 0x0)
874 POLAR_MACRO(unsigned char, 0xff, 3, 0x80)
877 POLAR_MACRO(unsigned char, 0xff, 4, 0x80)
880 POLAR_MACRO(uint16_t, 0xffff, 3, 0x8000)
882 case BC_YUVA16161616:
883 POLAR_MACRO(uint16_t, 0xffff, 4, 0x8000)
891 PolarEngine::PolarEngine(PolarEffect *plugin, int cpus)
892 : LoadServer(cpus, cpus)
894 this->plugin = plugin;
897 void PolarEngine::init_packages()
899 for(int i = 0; i < LoadServer::get_total_packages(); i++)
901 PolarPackage *pkg = (PolarPackage*)get_package(i);
902 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
903 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
907 LoadClient* PolarEngine::new_client()
909 return new PolarUnit(plugin, this);
912 LoadPackage* PolarEngine::new_package()
914 return new PolarPackage;