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"
40 #define WITHIN(a, b, c) ((((a) <= (b)) && ((b) <= (c))) ? 1 : 0)
58 void reset(int clear);
59 void copy_from(PolarConfig &src);
60 int equivalent(PolarConfig &src);
61 void interpolate(PolarConfig &prev,
67 int polar_to_rectangular;
76 class PolarDepth : public BC_FSlider
79 PolarDepth(PolarEffect *plugin, int x, int y);
84 class PolarAngle : public BC_FSlider
87 PolarAngle(PolarEffect *plugin, int x, int y);
92 class PolarReset : public BC_GenericButton
95 PolarReset(PolarEffect *plugin, PolarWindow *window, int x, int y);
102 class PolarSliderClr : public BC_Button
105 PolarSliderClr(PolarEffect *plugin, PolarWindow *window, int x, int y, int w, int clear);
113 class PolarWindow : public PluginClientWindow
116 PolarWindow(PolarEffect *plugin);
117 void create_objects();
118 void update_gui(int clear);
123 PolarSliderClr *depthClr;
124 PolarSliderClr *angleClr;
131 class PolarPackage : public LoadPackage
138 class PolarUnit : public LoadClient
141 PolarUnit(PolarEffect *plugin, PolarEngine *server);
142 void process_package(LoadPackage *package);
146 class PolarEngine : public LoadServer
149 PolarEngine(PolarEffect *plugin, int cpus);
150 void init_packages();
151 LoadClient* new_client();
152 LoadPackage* new_package();
156 class PolarEffect : public PluginVClient
159 PolarEffect(PluginServer *server);
162 PLUGIN_CLASS_MEMBERS(PolarConfig)
163 int process_realtime(VFrame *input, VFrame *output);
165 void save_data(KeyFrame *keyframe);
166 void read_data(KeyFrame *keyframe);
171 VFrame *input, *output;
172 int need_reconfigure;
177 REGISTER_PLUGIN(PolarEffect)
181 PolarConfig::PolarConfig()
186 void PolarConfig::reset(int clear)
189 case RESET_DEPTH : depth = 1.0;
191 case RESET_ANGLE : angle = 1.0;
199 polar_to_rectangular = 1;
204 void PolarConfig::copy_from(PolarConfig &src)
206 this->angle = src.angle;
207 this->depth = src.depth;
208 this->backwards = src.backwards;
209 this->invert = src.invert;
210 this->polar_to_rectangular = src.polar_to_rectangular;
213 int PolarConfig::equivalent(PolarConfig &src)
215 return EQUIV(this->angle, src.angle) && EQUIV(this->depth, src.depth);
218 void PolarConfig::interpolate(PolarConfig &prev,
224 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
225 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
227 this->depth = prev.depth * prev_scale + next.depth * next_scale;
228 this->angle = prev.angle * prev_scale + next.angle * next_scale;
236 PolarWindow::PolarWindow(PolarEffect *plugin)
237 : PluginClientWindow(plugin,
244 this->plugin = plugin;
247 void PolarWindow::create_objects()
249 int x = 10, y = 10, x1 = x + 50;
250 int x2 = 0; int clrBtn_w = 50;
252 add_subwindow(new BC_Title(x, y, _("Depth:")));
253 add_subwindow(depth = new PolarDepth(plugin, x1, y));
254 x2 = x1 + depth->get_w() + 10;
255 add_subwindow(depthClr = new PolarSliderClr(plugin, this, x2, y, clrBtn_w, RESET_DEPTH));
258 add_subwindow(new BC_Title(x, y, _("Angle:")));
259 add_subwindow(angle = new PolarAngle(plugin, x1, y));
260 add_subwindow(angleClr = new PolarSliderClr(plugin, this, x2, y, clrBtn_w, RESET_ANGLE));
262 add_subwindow(reset = new PolarReset(plugin, this, x, y));
269 void PolarWindow::update_gui(int clear)
272 case RESET_DEPTH : depth->update(plugin->config.depth);
274 case RESET_ANGLE : angle->update(plugin->config.angle);
278 depth->update(plugin->config.depth);
279 angle->update(plugin->config.angle);
288 PolarDepth::PolarDepth(PolarEffect *plugin, int x, int y)
296 plugin->config.depth)
298 this->plugin = plugin;
300 int PolarDepth::handle_event()
302 plugin->config.depth = get_value();
303 plugin->send_configure_change();
311 PolarAngle::PolarAngle(PolarEffect *plugin, int x, int y)
319 plugin->config.angle)
321 this->plugin = plugin;
323 int PolarAngle::handle_event()
325 plugin->config.angle = get_value();
326 plugin->send_configure_change();
332 PolarReset::PolarReset(PolarEffect *plugin, PolarWindow *window, int x, int y)
333 : BC_GenericButton(x, y, _("Reset"))
335 this->plugin = plugin;
336 this->window = window;
338 PolarReset::~PolarReset()
341 int PolarReset::handle_event()
343 plugin->config.reset(RESET_ALL);
344 window->update_gui(RESET_ALL);
345 plugin->send_configure_change();
350 PolarSliderClr::PolarSliderClr(PolarEffect *plugin, PolarWindow *window, int x, int y, int w, int clear)
351 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
353 this->plugin = plugin;
354 this->window = window;
357 PolarSliderClr::~PolarSliderClr()
360 int PolarSliderClr::handle_event()
362 // clear==1 ==> Depth slider
363 // clear==2 ==> Angle slider
364 plugin->config.reset(clear);
365 window->update_gui(clear);
366 plugin->send_configure_change();
373 PolarEffect::PolarEffect(PluginServer *server)
374 : PluginVClient(server)
376 need_reconfigure = 1;
382 PolarEffect::~PolarEffect()
385 if(temp_frame) delete temp_frame;
386 if(engine) delete engine;
391 const char* PolarEffect::plugin_title() { return N_("Polar"); }
392 int PolarEffect::is_realtime() { return 1; }
397 NEW_WINDOW_MACRO(PolarEffect, PolarWindow)
399 void PolarEffect::update_gui()
403 load_configuration();
404 thread->window->lock_window();
405 ((PolarWindow*)thread->window)->angle->update(config.angle);
406 ((PolarWindow*)thread->window)->depth->update(config.depth);
407 thread->window->unlock_window();
411 LOAD_CONFIGURATION_MACRO(PolarEffect, PolarConfig)
415 void PolarEffect::save_data(KeyFrame *keyframe)
419 // cause data to be stored directly in text
420 output.set_shared_output(keyframe->xbuf);
421 output.tag.set_title("POLAR");
422 output.tag.set_property("DEPTH", config.depth);
423 output.tag.set_property("ANGLE", config.angle);
425 output.tag.set_title("/POLAR");
427 output.append_newline();
428 output.terminate_string();
431 void PolarEffect::read_data(KeyFrame *keyframe)
435 input.set_shared_input(keyframe->xbuf);
437 while(!input.read_tag())
439 if(input.tag.title_is("POLAR"))
441 config.depth = input.tag.get_property("DEPTH", config.depth);
442 config.angle = input.tag.get_property("ANGLE", config.angle);
447 int PolarEffect::process_realtime(VFrame *input, VFrame *output)
449 need_reconfigure |= load_configuration();
452 this->output = output;
454 if(EQUIV(config.depth, 0) || EQUIV(config.angle, 0))
456 if(input->get_rows()[0] != output->get_rows()[0])
457 output->copy_from(input);
461 if(input->get_rows()[0] == output->get_rows()[0])
464 temp_frame = new VFrame(input->get_w(), input->get_h(),
465 input->get_color_model(), 0);
466 temp_frame->copy_from(input);
467 this->input = temp_frame;
471 if(!engine) engine = new PolarEngine(this, PluginClient::smp + 1);
473 engine->process_packages();
482 PolarPackage::PolarPackage()
490 PolarUnit::PolarUnit(PolarEffect *plugin, PolarEngine *server)
493 this->plugin = plugin;
497 static int calc_undistorted_coords(int wx,
503 int polar_to_rectangular,
513 double xx, xm, ym, yy;
517 double xmax, ymax, rmax;
518 double x_calc, y_calc;
520 double circle, angl, t;
538 angl = (double)angle / 180.0 * M_PI;
540 if(polar_to_rectangular)
547 atan(((double)(wx - cen_x)) /
548 ((double)(wy - cen_y)));
549 r = sqrt(SQR(wx - cen_x) +
555 phi = atan(((double)(wx - cen_x)) /
556 ((double)(cen_y - wy)));
557 r = sqrt(SQR(wx - cen_x) +
572 atan(((double)(cen_x -wx)) /
573 ((double)(cen_y - wy)));
574 r = sqrt(SQR(cen_x - wx) +
581 atan(((double)(cen_x - wx)) /
582 ((double)(wy - cen_y)));
583 r = sqrt(SQR(cen_x - wx) +
594 m = fabs(((double)(wy - cen_y)) /
595 ((double)(wx - cen_x)));
602 if(m <= ((double)(y2 - y1) /
622 rmax = sqrt((double)(SQR(xmax) + SQR(ymax)));
624 t = ((cen_y - y1) < (cen_x - x1)) ? (cen_y - y1) : (cen_x - x1);
625 rmax = (rmax - t) / 100 * (100 - circle) + t;
627 phi = fmod(phi + angl, 2 * M_PI);
630 x_calc = x2 - 1 - (x2 - x1 - 1) / (2 * M_PI) * phi;
632 x_calc = (x2 - x1 - 1) / (2 * M_PI) * phi + x1;
635 y_calc = (y2 - y1) / rmax * r + y1;
637 y_calc = y2 - (y2 - y1) / rmax * r;
639 xi = (int)(x_calc + 0.5);
640 yi = (int)(y_calc + 0.5);
642 if(WITHIN(0, xi, w - 1) && WITHIN(0, yi, h - 1))
657 phi = (2 * M_PI) * (x2 - wx) / xdiff;
659 phi = (2 * M_PI) * (wx - x1) / xdiff;
661 phi = fmod (phi + angl, 2 * M_PI);
663 if(phi >= 1.5 * M_PI)
664 phi2 = 2 * M_PI - phi;
669 if(phi >= 0.5 * M_PI)
676 m = (double)1.0 / xx;
680 if(m <= ((double)(ydiff) / (double)(xdiff)))
699 rmax = sqrt((double)(SQR(xmax) + SQR(ymax)));
701 t = ((ym - y1) < (xm - x1)) ? (ym - y1) : (xm - x1);
703 rmax = (rmax - t) / 100.0 * (100 - circle) + t;
706 r = rmax * (double)((wy - y1) / (double)(ydiff));
708 r = rmax * (double)((y2 - wy) / (double)(ydiff));
713 if(phi >= 1.5 * M_PI)
715 x_calc = (double)xm - xx;
716 y_calc = (double)ym - yy;
721 x_calc = (double)xm - xx;
722 y_calc = (double)ym + yy;
725 if(phi >= 0.5 * M_PI)
727 x_calc = (double)xm + xx;
728 y_calc = (double)ym + yy;
732 x_calc = (double)xm + xx;
733 y_calc = (double)ym - yy;
736 xi = (int)(x_calc + 0.5);
737 yi = (int)(y_calc + 0.5);
739 if(WITHIN(0, xi, w - 1) &&
740 WITHIN(0, yi, h - 1))
756 static double bilinear(double x, double y, double *values)
762 if(x < 0.0) x += 1.0;
763 if(y < 0.0) y += 1.0;
765 m0 = values[0] + x * (values[1] - values[0]);
766 m1 = values[2] + x * (values[3] - values[2]);
767 return m0 + y * (m1 - m0);
770 #define GET_PIXEL(x, y, components, input_rows) \
771 input_rows[CLIP((y), 0, ((h) - 1))] + components * CLIP((x), 0, ((w) - 1))
773 #define POLAR_MACRO(type, max, components, chroma_offset) \
775 type **in_rows = (type**)plugin->input->get_rows(); \
776 type **out_rows = (type**)plugin->output->get_rows(); \
779 for(int y = pkg->row1; y < pkg->row2; y++) \
781 type *output_row = out_rows[y]; \
783 for(int x = 0; x < w; x++) \
785 type *output_pixel = output_row + x * components; \
786 if(calc_undistorted_coords(x, \
790 plugin->config.depth, \
791 plugin->config.angle, \
792 plugin->config.polar_to_rectangular, \
793 plugin->config.backwards, \
794 plugin->config.invert, \
800 type *pixel1 = GET_PIXEL((int)cx, (int)cy, components, in_rows); \
801 type *pixel2 = GET_PIXEL((int)cx + 1, (int)cy, components, in_rows); \
802 type *pixel3 = GET_PIXEL((int)cx, (int)cy + 1, components, in_rows); \
803 type *pixel4 = GET_PIXEL((int)cx + 1, (int)cy + 1, components, in_rows); \
805 values[0] = pixel1[0]; \
806 values[1] = pixel2[0]; \
807 values[2] = pixel3[0]; \
808 values[3] = pixel4[0]; \
809 output_pixel[0] = (type)bilinear(cx, cy, values); \
811 values[0] = pixel1[1]; \
812 values[1] = pixel2[1]; \
813 values[2] = pixel3[1]; \
814 values[3] = pixel4[1]; \
815 output_pixel[1] = (type)bilinear(cx, cy, values); \
817 values[0] = pixel1[2]; \
818 values[1] = pixel2[2]; \
819 values[2] = pixel3[2]; \
820 values[3] = pixel4[2]; \
821 output_pixel[2] = (type)bilinear(cx, cy, values); \
823 if(components == 4) \
825 values[0] = pixel1[3]; \
826 values[1] = pixel2[3]; \
827 values[2] = pixel3[3]; \
828 values[3] = pixel4[3]; \
829 output_pixel[3] = (type)bilinear(cx, cy, values); \
834 output_pixel[0] = 0; \
835 output_pixel[1] = chroma_offset; \
836 output_pixel[2] = chroma_offset; \
837 if(components == 4) output_pixel[3] = max; \
844 void PolarUnit::process_package(LoadPackage *package)
846 PolarPackage *pkg = (PolarPackage*)package;
847 int w = plugin->input->get_w();
848 int h = plugin->input->get_h();
851 double cen_x = (double)(w - 1) / 2.0;
852 double cen_y = (double)(h - 1) / 2.0;
854 switch(plugin->input->get_color_model())
857 POLAR_MACRO(float, 1, 3, 0x0)
860 POLAR_MACRO(float, 1, 4, 0x0)
863 POLAR_MACRO(unsigned char, 0xff, 3, 0x0)
866 POLAR_MACRO(unsigned char, 0xff, 4, 0x0)
869 POLAR_MACRO(uint16_t, 0xffff, 3, 0x0)
871 case BC_RGBA16161616:
872 POLAR_MACRO(uint16_t, 0xffff, 4, 0x0)
875 POLAR_MACRO(unsigned char, 0xff, 3, 0x80)
878 POLAR_MACRO(unsigned char, 0xff, 4, 0x80)
881 POLAR_MACRO(uint16_t, 0xffff, 3, 0x8000)
883 case BC_YUVA16161616:
884 POLAR_MACRO(uint16_t, 0xffff, 4, 0x8000)
892 PolarEngine::PolarEngine(PolarEffect *plugin, int cpus)
893 : LoadServer(cpus, cpus)
895 this->plugin = plugin;
898 void PolarEngine::init_packages()
900 for(int i = 0; i < LoadServer::get_total_packages(); i++)
902 PolarPackage *pkg = (PolarPackage*)get_package(i);
903 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
904 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
908 LoadClient* PolarEngine::new_client()
910 return new PolarUnit(plugin, this);
913 LoadPackage* PolarEngine::new_package()
915 return new PolarPackage;