1 #include "bcdisplayinfo.h"
7 #include "loadbalance.h"
9 #include "pluginvclient.h"
18 #define _(String) gettext(String)
19 #define gettext_noop(String) String
20 #define N_(String) gettext_noop (String)
22 #define SQR(x) ((x) * (x))
23 #define WITHIN(a, b, c) ((((a) <= (b)) && ((b) <= (c))) ? 1 : 0)
36 void copy_from(PolarConfig &src);
37 int equivalent(PolarConfig &src);
38 void interpolate(PolarConfig &prev,
44 int polar_to_rectangular;
53 class PolarDepth : public BC_FSlider
56 PolarDepth(PolarEffect *plugin, int x, int y);
61 class PolarAngle : public BC_FSlider
64 PolarAngle(PolarEffect *plugin, int x, int y);
69 class PolarWindow : public BC_Window
72 PolarWindow(PolarEffect *plugin, int x, int y);
73 void create_objects();
81 PLUGIN_THREAD_HEADER(PolarEffect, PolarThread, PolarWindow)
84 class PolarPackage : public LoadPackage
91 class PolarUnit : public LoadClient
94 PolarUnit(PolarEffect *plugin, PolarEngine *server);
95 void process_package(LoadPackage *package);
99 class PolarEngine : public LoadServer
102 PolarEngine(PolarEffect *plugin, int cpus);
103 void init_packages();
104 LoadClient* new_client();
105 LoadPackage* new_package();
109 class PolarEffect : public PluginVClient
112 PolarEffect(PluginServer *server);
115 int process_realtime(VFrame *input, VFrame *output);
117 char* plugin_title();
119 int load_configuration();
122 void save_data(KeyFrame *keyframe);
123 void read_data(KeyFrame *keyframe);
134 VFrame *input, *output;
135 int need_reconfigure;
140 REGISTER_PLUGIN(PolarEffect)
144 PolarConfig::PolarConfig()
150 polar_to_rectangular = 1;
154 void PolarConfig::copy_from(PolarConfig &src)
156 this->angle = src.angle;
157 this->depth = src.depth;
158 this->backwards = src.backwards;
159 this->invert = src.invert;
160 this->polar_to_rectangular = src.polar_to_rectangular;
163 int PolarConfig::equivalent(PolarConfig &src)
165 return EQUIV(this->angle, src.angle) && EQUIV(this->depth, src.depth);
168 void PolarConfig::interpolate(PolarConfig &prev,
174 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
175 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
177 this->depth = prev.depth * prev_scale + next.depth * next_scale;
178 this->angle = prev.angle * prev_scale + next.angle * next_scale;
186 PolarWindow::PolarWindow(PolarEffect *plugin, int x, int y)
187 : BC_Window(plugin->gui_string,
198 this->plugin = plugin;
201 void PolarWindow::create_objects()
204 add_subwindow(new BC_Title(x, y, _("Depth:")));
205 add_subwindow(depth = new PolarDepth(plugin, x + 50, y));
207 add_subwindow(new BC_Title(x, y, _("Angle:")));
208 add_subwindow(angle = new PolarAngle(plugin, x + 50, y));
214 int PolarWindow::close_event()
220 PLUGIN_THREAD_OBJECT(PolarEffect, PolarThread, PolarWindow)
223 PolarDepth::PolarDepth(PolarEffect *plugin, int x, int y)
231 plugin->config.depth)
233 this->plugin = plugin;
235 int PolarDepth::handle_event()
237 plugin->config.depth = get_value();
238 plugin->send_configure_change();
246 PolarAngle::PolarAngle(PolarEffect *plugin, int x, int y)
254 plugin->config.angle)
256 this->plugin = plugin;
258 int PolarAngle::handle_event()
260 plugin->config.angle = get_value();
261 plugin->send_configure_change();
269 PolarEffect::PolarEffect(PluginServer *server)
270 : PluginVClient(server)
272 need_reconfigure = 1;
275 PLUGIN_CONSTRUCTOR_MACRO
278 PolarEffect::~PolarEffect()
280 PLUGIN_DESTRUCTOR_MACRO
281 if(temp_frame) delete temp_frame;
282 if(engine) delete engine;
286 int PolarEffect::is_realtime()
291 char* PolarEffect::plugin_title()
296 NEW_PICON_MACRO(PolarEffect)
298 SHOW_GUI_MACRO(PolarEffect, PolarThread)
300 RAISE_WINDOW_MACRO(PolarEffect)
302 SET_STRING_MACRO(PolarEffect)
304 void PolarEffect::update_gui()
308 load_configuration();
309 thread->window->lock_window();
310 thread->window->angle->update(config.angle);
311 thread->window->depth->update(config.depth);
312 thread->window->unlock_window();
316 LOAD_CONFIGURATION_MACRO(PolarEffect, PolarConfig)
319 int PolarEffect::load_defaults()
321 char directory[BCTEXTLEN];
322 // set the default directory
323 sprintf(directory, "%spolar.rc", BCASTDIR);
326 defaults = new Defaults(directory);
329 config.depth = defaults->get("DEPTH", config.depth);
330 config.angle = defaults->get("ANGLE", config.angle);
334 int PolarEffect::save_defaults()
336 defaults->update("DEPTH", config.depth);
337 defaults->update("ANGLE", config.angle);
342 void PolarEffect::save_data(KeyFrame *keyframe)
346 // cause data to be stored directly in text
347 output.set_shared_string(keyframe->data, MESSAGESIZE);
348 output.tag.set_title("POLAR");
349 output.tag.set_property("DEPTH", config.depth);
350 output.tag.set_property("ANGLE", config.angle);
352 output.terminate_string();
355 void PolarEffect::read_data(KeyFrame *keyframe)
359 input.set_shared_string(keyframe->data, strlen(keyframe->data));
363 while(!input.read_tag())
365 if(input.tag.title_is("POLAR"))
367 config.depth = input.tag.get_property("DEPTH", config.depth);
368 config.angle = input.tag.get_property("ANGLE", config.angle);
373 int PolarEffect::process_realtime(VFrame *input, VFrame *output)
375 need_reconfigure |= load_configuration();
378 this->output = output;
380 if(EQUIV(config.depth, 0) || EQUIV(config.angle, 0))
382 if(input->get_rows()[0] != output->get_rows()[0])
383 output->copy_from(input);
387 if(input->get_rows()[0] == output->get_rows()[0])
389 if(!temp_frame) temp_frame = new VFrame(0,
392 input->get_color_model());
393 temp_frame->copy_from(input);
394 this->input = temp_frame;
398 if(!engine) engine = new PolarEngine(this, PluginClient::smp + 1);
400 engine->process_packages();
409 PolarPackage::PolarPackage()
417 PolarUnit::PolarUnit(PolarEffect *plugin, PolarEngine *server)
420 this->plugin = plugin;
424 static int calc_undistorted_coords(int wx,
430 int polar_to_rectangular,
440 double xx, xm, ym, yy;
444 double xmax, ymax, rmax;
445 double x_calc, y_calc;
447 double circle, angl, t;
465 angl = (double)angle / 180.0 * M_PI;
467 if(polar_to_rectangular)
474 atan(((double)(wx - cen_x)) /
475 ((double)(wy - cen_y)));
476 r = sqrt(SQR(wx - cen_x) +
482 phi = atan(((double)(wx - cen_x)) /
483 ((double)(cen_y - wy)));
484 r = sqrt(SQR(wx - cen_x) +
499 atan(((double)(cen_x -wx)) /
500 ((double)(cen_y - wy)));
501 r = sqrt(SQR(cen_x - wx) +
508 atan(((double)(cen_x - wx)) /
509 ((double)(wy - cen_y)));
510 r = sqrt(SQR(cen_x - wx) +
521 m = fabs(((double)(wy - cen_y)) /
522 ((double)(wx - cen_x)));
529 if(m <= ((double)(y2 - y1) /
549 rmax = sqrt((double)(SQR(xmax) + SQR(ymax)));
551 t = ((cen_y - y1) < (cen_x - x1)) ? (cen_y - y1) : (cen_x - x1);
552 rmax = (rmax - t) / 100 * (100 - circle) + t;
554 phi = fmod(phi + angl, 2 * M_PI);
557 x_calc = x2 - 1 - (x2 - x1 - 1) / (2 * M_PI) * phi;
559 x_calc = (x2 - x1 - 1) / (2 * M_PI) * phi + x1;
562 y_calc = (y2 - y1) / rmax * r + y1;
564 y_calc = y2 - (y2 - y1) / rmax * r;
566 xi = (int)(x_calc + 0.5);
567 yi = (int)(y_calc + 0.5);
569 if(WITHIN(0, xi, w - 1) && WITHIN(0, yi, h - 1))
584 phi = (2 * M_PI) * (x2 - wx) / xdiff;
586 phi = (2 * M_PI) * (wx - x1) / xdiff;
588 phi = fmod (phi + angl, 2 * M_PI);
590 if(phi >= 1.5 * M_PI)
591 phi2 = 2 * M_PI - phi;
596 if(phi >= 0.5 * M_PI)
603 m = (double)1.0 / xx;
607 if(m <= ((double)(ydiff) / (double)(xdiff)))
626 rmax = sqrt((double)(SQR(xmax) + SQR(ymax)));
628 t = ((ym - y1) < (xm - x1)) ? (ym - y1) : (xm - x1);
630 rmax = (rmax - t) / 100.0 * (100 - circle) + t;
633 r = rmax * (double)((wy - y1) / (double)(ydiff));
635 r = rmax * (double)((y2 - wy) / (double)(ydiff));
640 if(phi >= 1.5 * M_PI)
642 x_calc = (double)xm - xx;
643 y_calc = (double)ym - yy;
648 x_calc = (double)xm - xx;
649 y_calc = (double)ym + yy;
652 if(phi >= 0.5 * M_PI)
654 x_calc = (double)xm + xx;
655 y_calc = (double)ym + yy;
659 x_calc = (double)xm + xx;
660 y_calc = (double)ym - yy;
663 xi = (int)(x_calc + 0.5);
664 yi = (int)(y_calc + 0.5);
666 if(WITHIN(0, xi, w - 1) &&
667 WITHIN(0, yi, h - 1))
683 static double bilinear(double x, double y, double *values)
689 if(x < 0.0) x += 1.0;
690 if(y < 0.0) y += 1.0;
692 m0 = values[0] + x * (values[1] - values[0]);
693 m1 = values[2] + x * (values[3] - values[2]);
694 return m0 + y * (m1 - m0);
697 #define GET_PIXEL(x, y, components, input_rows) \
698 input_rows[CLIP((y), 0, ((h) - 1))] + components * CLIP((x), 0, ((w) - 1))
700 #define POLAR_MACRO(type, max, components, chroma_offset) \
702 type **in_rows = (type**)plugin->input->get_rows(); \
703 type **out_rows = (type**)plugin->output->get_rows(); \
706 for(int y = pkg->row1; y < pkg->row2; y++) \
708 type *output_row = out_rows[y]; \
710 for(int x = 0; x < w; x++) \
712 type *output_pixel = output_row + x * components; \
713 if(calc_undistorted_coords(x, \
717 plugin->config.depth, \
718 plugin->config.angle, \
719 plugin->config.polar_to_rectangular, \
720 plugin->config.backwards, \
721 plugin->config.invert, \
727 type *pixel1 = GET_PIXEL((int)cx, (int)cy, components, in_rows); \
728 type *pixel2 = GET_PIXEL((int)cx + 1, (int)cy, components, in_rows); \
729 type *pixel3 = GET_PIXEL((int)cx, (int)cy + 1, components, in_rows); \
730 type *pixel4 = GET_PIXEL((int)cx + 1, (int)cy + 1, components, in_rows); \
732 values[0] = pixel1[0]; \
733 values[1] = pixel2[0]; \
734 values[2] = pixel3[0]; \
735 values[3] = pixel4[0]; \
736 output_pixel[0] = (type)bilinear(cx, cy, values); \
738 values[0] = pixel1[1]; \
739 values[1] = pixel2[1]; \
740 values[2] = pixel3[1]; \
741 values[3] = pixel4[1]; \
742 output_pixel[1] = (type)bilinear(cx, cy, values); \
744 values[0] = pixel1[2]; \
745 values[1] = pixel2[2]; \
746 values[2] = pixel3[2]; \
747 values[3] = pixel4[2]; \
748 output_pixel[2] = (type)bilinear(cx, cy, values); \
750 if(components == 4) \
752 values[0] = pixel1[3]; \
753 values[1] = pixel2[3]; \
754 values[2] = pixel3[3]; \
755 values[3] = pixel4[3]; \
756 output_pixel[3] = (type)bilinear(cx, cy, values); \
761 output_pixel[0] = 0; \
762 output_pixel[1] = chroma_offset; \
763 output_pixel[2] = chroma_offset; \
764 if(components == 4) output_pixel[3] = max; \
771 void PolarUnit::process_package(LoadPackage *package)
773 PolarPackage *pkg = (PolarPackage*)package;
774 int w = plugin->input->get_w();
775 int h = plugin->input->get_h();
778 double cen_x = (double)(w - 1) / 2.0;
779 double cen_y = (double)(h - 1) / 2.0;
781 switch(plugin->input->get_color_model())
784 POLAR_MACRO(unsigned char, 0xff, 3, 0x0)
787 POLAR_MACRO(unsigned char, 0xff, 4, 0x0)
790 POLAR_MACRO(uint16_t, 0xffff, 3, 0x0)
792 case BC_RGBA16161616:
793 POLAR_MACRO(uint16_t, 0xffff, 4, 0x0)
796 POLAR_MACRO(unsigned char, 0xff, 3, 0x80)
799 POLAR_MACRO(unsigned char, 0xff, 4, 0x80)
802 POLAR_MACRO(uint16_t, 0xffff, 3, 0x8000)
804 case BC_YUVA16161616:
805 POLAR_MACRO(uint16_t, 0xffff, 4, 0x8000)
813 PolarEngine::PolarEngine(PolarEffect *plugin, int cpus)
814 : LoadServer(cpus, cpus)
816 this->plugin = plugin;
819 void PolarEngine::init_packages()
821 int increment = plugin->input->get_h() / LoadServer::total_packages + 1;
823 for(int i = 0; i < LoadServer::total_packages; i++)
825 PolarPackage *pkg = (PolarPackage*)packages[i];
827 pkg->row2 = y + increment;
829 if(pkg->row2 > plugin->input->get_h())
831 y = pkg->row2 = plugin->input->get_h();
836 LoadClient* PolarEngine::new_client()
838 return new PolarUnit(plugin, this);
841 LoadPackage* PolarEngine::new_package()
843 return new PolarPackage;