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)
54 void copy_from(PolarConfig &src);
55 int equivalent(PolarConfig &src);
56 void interpolate(PolarConfig &prev,
62 int polar_to_rectangular;
71 class PolarDepth : public BC_FSlider
74 PolarDepth(PolarEffect *plugin, int x, int y);
79 class PolarAngle : public BC_FSlider
82 PolarAngle(PolarEffect *plugin, int x, int y);
87 class PolarReset : public BC_GenericButton
90 PolarReset(PolarEffect *plugin, PolarWindow *window, int x, int y);
97 class PolarWindow : public PluginClientWindow
100 PolarWindow(PolarEffect *plugin);
101 void create_objects();
113 class PolarPackage : public LoadPackage
120 class PolarUnit : public LoadClient
123 PolarUnit(PolarEffect *plugin, PolarEngine *server);
124 void process_package(LoadPackage *package);
128 class PolarEngine : public LoadServer
131 PolarEngine(PolarEffect *plugin, int cpus);
132 void init_packages();
133 LoadClient* new_client();
134 LoadPackage* new_package();
138 class PolarEffect : public PluginVClient
141 PolarEffect(PluginServer *server);
144 PLUGIN_CLASS_MEMBERS(PolarConfig)
145 int process_realtime(VFrame *input, VFrame *output);
147 void save_data(KeyFrame *keyframe);
148 void read_data(KeyFrame *keyframe);
153 VFrame *input, *output;
154 int need_reconfigure;
159 REGISTER_PLUGIN(PolarEffect)
163 PolarConfig::PolarConfig()
168 void PolarConfig::reset()
174 polar_to_rectangular = 1;
177 void PolarConfig::copy_from(PolarConfig &src)
179 this->angle = src.angle;
180 this->depth = src.depth;
181 this->backwards = src.backwards;
182 this->invert = src.invert;
183 this->polar_to_rectangular = src.polar_to_rectangular;
186 int PolarConfig::equivalent(PolarConfig &src)
188 return EQUIV(this->angle, src.angle) && EQUIV(this->depth, src.depth);
191 void PolarConfig::interpolate(PolarConfig &prev,
197 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
198 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
200 this->depth = prev.depth * prev_scale + next.depth * next_scale;
201 this->angle = prev.angle * prev_scale + next.angle * next_scale;
209 PolarWindow::PolarWindow(PolarEffect *plugin)
210 : PluginClientWindow(plugin,
217 this->plugin = plugin;
220 void PolarWindow::create_objects()
223 add_subwindow(new BC_Title(x, y, _("Depth:")));
224 add_subwindow(depth = new PolarDepth(plugin, x + 50, y));
226 add_subwindow(new BC_Title(x, y, _("Angle:")));
227 add_subwindow(angle = new PolarAngle(plugin, x + 50, y));
229 add_subwindow(reset = new PolarReset(plugin, this, x, y));
236 void PolarWindow::update()
238 depth->update(plugin->config.depth);
239 angle->update(plugin->config.angle);
246 PolarDepth::PolarDepth(PolarEffect *plugin, int x, int y)
254 plugin->config.depth)
256 this->plugin = plugin;
258 int PolarDepth::handle_event()
260 plugin->config.depth = get_value();
261 plugin->send_configure_change();
269 PolarAngle::PolarAngle(PolarEffect *plugin, int x, int y)
277 plugin->config.angle)
279 this->plugin = plugin;
281 int PolarAngle::handle_event()
283 plugin->config.angle = get_value();
284 plugin->send_configure_change();
290 PolarReset::PolarReset(PolarEffect *plugin, PolarWindow *window, int x, int y)
291 : BC_GenericButton(x, y, _("Reset"))
293 this->plugin = plugin;
294 this->window = window;
296 PolarReset::~PolarReset()
299 int PolarReset::handle_event()
301 plugin->config.reset();
303 plugin->send_configure_change();
310 PolarEffect::PolarEffect(PluginServer *server)
311 : PluginVClient(server)
313 need_reconfigure = 1;
319 PolarEffect::~PolarEffect()
322 if(temp_frame) delete temp_frame;
323 if(engine) delete engine;
328 const char* PolarEffect::plugin_title() { return N_("Polar"); }
329 int PolarEffect::is_realtime() { return 1; }
334 NEW_WINDOW_MACRO(PolarEffect, PolarWindow)
336 void PolarEffect::update_gui()
340 load_configuration();
341 thread->window->lock_window();
342 ((PolarWindow*)thread->window)->angle->update(config.angle);
343 ((PolarWindow*)thread->window)->depth->update(config.depth);
344 thread->window->unlock_window();
348 LOAD_CONFIGURATION_MACRO(PolarEffect, PolarConfig)
352 void PolarEffect::save_data(KeyFrame *keyframe)
356 // cause data to be stored directly in text
357 output.set_shared_output(keyframe->xbuf);
358 output.tag.set_title("POLAR");
359 output.tag.set_property("DEPTH", config.depth);
360 output.tag.set_property("ANGLE", config.angle);
362 output.tag.set_title("/POLAR");
364 output.append_newline();
365 output.terminate_string();
368 void PolarEffect::read_data(KeyFrame *keyframe)
372 input.set_shared_input(keyframe->xbuf);
374 while(!input.read_tag())
376 if(input.tag.title_is("POLAR"))
378 config.depth = input.tag.get_property("DEPTH", config.depth);
379 config.angle = input.tag.get_property("ANGLE", config.angle);
384 int PolarEffect::process_realtime(VFrame *input, VFrame *output)
386 need_reconfigure |= load_configuration();
389 this->output = output;
391 if(EQUIV(config.depth, 0) || EQUIV(config.angle, 0))
393 if(input->get_rows()[0] != output->get_rows()[0])
394 output->copy_from(input);
398 if(input->get_rows()[0] == output->get_rows()[0])
401 temp_frame = new VFrame(input->get_w(), input->get_h(),
402 input->get_color_model(), 0);
403 temp_frame->copy_from(input);
404 this->input = temp_frame;
408 if(!engine) engine = new PolarEngine(this, PluginClient::smp + 1);
410 engine->process_packages();
419 PolarPackage::PolarPackage()
427 PolarUnit::PolarUnit(PolarEffect *plugin, PolarEngine *server)
430 this->plugin = plugin;
434 static int calc_undistorted_coords(int wx,
440 int polar_to_rectangular,
450 double xx, xm, ym, yy;
454 double xmax, ymax, rmax;
455 double x_calc, y_calc;
457 double circle, angl, t;
475 angl = (double)angle / 180.0 * M_PI;
477 if(polar_to_rectangular)
484 atan(((double)(wx - cen_x)) /
485 ((double)(wy - cen_y)));
486 r = sqrt(SQR(wx - cen_x) +
492 phi = atan(((double)(wx - cen_x)) /
493 ((double)(cen_y - wy)));
494 r = sqrt(SQR(wx - cen_x) +
509 atan(((double)(cen_x -wx)) /
510 ((double)(cen_y - wy)));
511 r = sqrt(SQR(cen_x - wx) +
518 atan(((double)(cen_x - wx)) /
519 ((double)(wy - cen_y)));
520 r = sqrt(SQR(cen_x - wx) +
531 m = fabs(((double)(wy - cen_y)) /
532 ((double)(wx - cen_x)));
539 if(m <= ((double)(y2 - y1) /
559 rmax = sqrt((double)(SQR(xmax) + SQR(ymax)));
561 t = ((cen_y - y1) < (cen_x - x1)) ? (cen_y - y1) : (cen_x - x1);
562 rmax = (rmax - t) / 100 * (100 - circle) + t;
564 phi = fmod(phi + angl, 2 * M_PI);
567 x_calc = x2 - 1 - (x2 - x1 - 1) / (2 * M_PI) * phi;
569 x_calc = (x2 - x1 - 1) / (2 * M_PI) * phi + x1;
572 y_calc = (y2 - y1) / rmax * r + y1;
574 y_calc = y2 - (y2 - y1) / rmax * r;
576 xi = (int)(x_calc + 0.5);
577 yi = (int)(y_calc + 0.5);
579 if(WITHIN(0, xi, w - 1) && WITHIN(0, yi, h - 1))
594 phi = (2 * M_PI) * (x2 - wx) / xdiff;
596 phi = (2 * M_PI) * (wx - x1) / xdiff;
598 phi = fmod (phi + angl, 2 * M_PI);
600 if(phi >= 1.5 * M_PI)
601 phi2 = 2 * M_PI - phi;
606 if(phi >= 0.5 * M_PI)
613 m = (double)1.0 / xx;
617 if(m <= ((double)(ydiff) / (double)(xdiff)))
636 rmax = sqrt((double)(SQR(xmax) + SQR(ymax)));
638 t = ((ym - y1) < (xm - x1)) ? (ym - y1) : (xm - x1);
640 rmax = (rmax - t) / 100.0 * (100 - circle) + t;
643 r = rmax * (double)((wy - y1) / (double)(ydiff));
645 r = rmax * (double)((y2 - wy) / (double)(ydiff));
650 if(phi >= 1.5 * M_PI)
652 x_calc = (double)xm - xx;
653 y_calc = (double)ym - yy;
658 x_calc = (double)xm - xx;
659 y_calc = (double)ym + yy;
662 if(phi >= 0.5 * M_PI)
664 x_calc = (double)xm + xx;
665 y_calc = (double)ym + yy;
669 x_calc = (double)xm + xx;
670 y_calc = (double)ym - yy;
673 xi = (int)(x_calc + 0.5);
674 yi = (int)(y_calc + 0.5);
676 if(WITHIN(0, xi, w - 1) &&
677 WITHIN(0, yi, h - 1))
693 static double bilinear(double x, double y, double *values)
699 if(x < 0.0) x += 1.0;
700 if(y < 0.0) y += 1.0;
702 m0 = values[0] + x * (values[1] - values[0]);
703 m1 = values[2] + x * (values[3] - values[2]);
704 return m0 + y * (m1 - m0);
707 #define GET_PIXEL(x, y, components, input_rows) \
708 input_rows[CLIP((y), 0, ((h) - 1))] + components * CLIP((x), 0, ((w) - 1))
710 #define POLAR_MACRO(type, max, components, chroma_offset) \
712 type **in_rows = (type**)plugin->input->get_rows(); \
713 type **out_rows = (type**)plugin->output->get_rows(); \
716 for(int y = pkg->row1; y < pkg->row2; y++) \
718 type *output_row = out_rows[y]; \
720 for(int x = 0; x < w; x++) \
722 type *output_pixel = output_row + x * components; \
723 if(calc_undistorted_coords(x, \
727 plugin->config.depth, \
728 plugin->config.angle, \
729 plugin->config.polar_to_rectangular, \
730 plugin->config.backwards, \
731 plugin->config.invert, \
737 type *pixel1 = GET_PIXEL((int)cx, (int)cy, components, in_rows); \
738 type *pixel2 = GET_PIXEL((int)cx + 1, (int)cy, components, in_rows); \
739 type *pixel3 = GET_PIXEL((int)cx, (int)cy + 1, components, in_rows); \
740 type *pixel4 = GET_PIXEL((int)cx + 1, (int)cy + 1, components, in_rows); \
742 values[0] = pixel1[0]; \
743 values[1] = pixel2[0]; \
744 values[2] = pixel3[0]; \
745 values[3] = pixel4[0]; \
746 output_pixel[0] = (type)bilinear(cx, cy, values); \
748 values[0] = pixel1[1]; \
749 values[1] = pixel2[1]; \
750 values[2] = pixel3[1]; \
751 values[3] = pixel4[1]; \
752 output_pixel[1] = (type)bilinear(cx, cy, values); \
754 values[0] = pixel1[2]; \
755 values[1] = pixel2[2]; \
756 values[2] = pixel3[2]; \
757 values[3] = pixel4[2]; \
758 output_pixel[2] = (type)bilinear(cx, cy, values); \
760 if(components == 4) \
762 values[0] = pixel1[3]; \
763 values[1] = pixel2[3]; \
764 values[2] = pixel3[3]; \
765 values[3] = pixel4[3]; \
766 output_pixel[3] = (type)bilinear(cx, cy, values); \
771 output_pixel[0] = 0; \
772 output_pixel[1] = chroma_offset; \
773 output_pixel[2] = chroma_offset; \
774 if(components == 4) output_pixel[3] = max; \
781 void PolarUnit::process_package(LoadPackage *package)
783 PolarPackage *pkg = (PolarPackage*)package;
784 int w = plugin->input->get_w();
785 int h = plugin->input->get_h();
788 double cen_x = (double)(w - 1) / 2.0;
789 double cen_y = (double)(h - 1) / 2.0;
791 switch(plugin->input->get_color_model())
794 POLAR_MACRO(float, 1, 3, 0x0)
797 POLAR_MACRO(float, 1, 4, 0x0)
800 POLAR_MACRO(unsigned char, 0xff, 3, 0x0)
803 POLAR_MACRO(unsigned char, 0xff, 4, 0x0)
806 POLAR_MACRO(uint16_t, 0xffff, 3, 0x0)
808 case BC_RGBA16161616:
809 POLAR_MACRO(uint16_t, 0xffff, 4, 0x0)
812 POLAR_MACRO(unsigned char, 0xff, 3, 0x80)
815 POLAR_MACRO(unsigned char, 0xff, 4, 0x80)
818 POLAR_MACRO(uint16_t, 0xffff, 3, 0x8000)
820 case BC_YUVA16161616:
821 POLAR_MACRO(uint16_t, 0xffff, 4, 0x8000)
829 PolarEngine::PolarEngine(PolarEffect *plugin, int cpus)
830 : LoadServer(cpus, cpus)
832 this->plugin = plugin;
835 void PolarEngine::init_packages()
837 for(int i = 0; i < LoadServer::get_total_packages(); i++)
839 PolarPackage *pkg = (PolarPackage*)get_package(i);
840 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
841 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
845 LoadClient* PolarEngine::new_client()
847 return new PolarUnit(plugin, this);
850 LoadPackage* PolarEngine::new_package()
852 return new PolarPackage;