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"
24 #include "bcsignals.h"
38 REGISTER_PLUGIN(LensMain)
40 LensConfig::LensConfig()
45 void LensConfig::reset()
47 for( int i=0; i<FOV_CHANNELS; ++i )
51 mode = SPHERICAL_SHRINK;
52 interp = INTERP_BILINEAR;
58 int LensConfig::equivalent(LensConfig &that)
60 for( int i=0; i<FOV_CHANNELS; ++i )
61 if( !EQUIV(fov[i], that.fov[i]) ) return 0;
62 return EQUIV(aspect, that.aspect) &&
63 EQUIV(radius, that.radius) &&
64 EQUIV(center_x, that.center_x) &&
65 EQUIV(center_y, that.center_y) &&
67 interp == that.interp &&
68 draw_guides == that.draw_guides;
71 void LensConfig::copy_from(LensConfig &that)
73 for( int i=0; i<FOV_CHANNELS; ++i )
79 center_x = that.center_x;
80 center_y = that.center_y;
81 draw_guides = that.draw_guides;
84 void LensConfig::interpolate(LensConfig &prev, LensConfig &next,
85 int64_t prev_frame, int64_t next_frame, int64_t current_frame)
87 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
88 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
90 for( int i=0; i<FOV_CHANNELS; ++i )
91 fov[i] = prev.fov[i] * prev_scale + next.fov[i] * next_scale;
92 aspect = prev.aspect * prev_scale + next.aspect * next_scale;
93 radius = prev.radius * prev_scale + next.radius * next_scale;
94 center_x = prev.center_x * prev_scale + next.center_x * next_scale;
95 center_y = prev.center_y * prev_scale + next.center_y * next_scale;
98 draw_guides = prev.draw_guides;
103 void LensConfig::boundaries()
105 CLAMP(center_x, 0.0, 99.0);
106 CLAMP(center_y, 0.0, 99.0);
107 for( int i=0; i<FOV_CHANNELS; ++i )
108 CLAMP(fov[i], 0.0, 1.0);
109 CLAMP(aspect, 0.3, 3.0);
110 CLAMP(radius, 0.3, 3.0);
116 LensSlider::LensSlider(LensMain *plugin, LensGUI *gui,
117 LensText *text, float *output, int x, int y, float min, float max)
118 : BC_FSlider(x, y, 0, 200, 200, min, max, *output)
121 this->plugin = plugin;
122 this->output = output;
127 int LensSlider::handle_event()
129 float prev_output = *output;
130 *output = get_value();
131 text->update(*output);
133 float difference = *output - prev_output;
137 for( int i=0; i<FOV_CHANNELS; ++i ) {
138 if( output == &plugin->config.fov[i] ) {
145 for( int i=0; i<FOV_CHANNELS; ++i ) {
146 if( output != &plugin->config.fov[i] ) {
147 plugin->config.fov[i] += difference;
148 plugin->config.boundaries();
149 gui->fov_slider[i]->update(plugin->config.fov[i]);
150 gui->fov_text[i]->update(plugin->config.fov[i]);
156 plugin->send_configure_change();
162 LensText::LensText(LensMain *plugin, LensGUI *gui,
163 LensSlider *slider, float *output, int x, int y)
164 : BC_TextBox(x, y, 100, 1, *output)
167 this->plugin = plugin;
168 this->output = output;
169 this->slider = slider;
172 int LensText::handle_event()
174 float prev_output = *output;
175 *output = atof(get_text());
176 slider->update(*output);
178 float difference = *output - prev_output;
182 for( int i=0; i<FOV_CHANNELS; ++i ) {
183 if( output == &plugin->config.fov[i] ) {
190 for( int i=0; i<FOV_CHANNELS; ++i ) {
191 if( output != &plugin->config.fov[i] ) {
192 plugin->config.fov[i] += difference;
193 plugin->config.boundaries();
194 gui->fov_slider[i]->update(plugin->config.fov[i]);
195 gui->fov_text[i]->update(plugin->config.fov[i]);
201 plugin->send_configure_change();
206 LensToggle::LensToggle(LensMain *plugin,
211 : BC_CheckBox(x, y, *output, text)
213 this->output = output;
214 this->plugin = plugin;
217 int LensToggle::handle_event()
219 *output = get_value();
220 plugin->send_configure_change();
225 LensMode::LensMode(LensMain *plugin, LensGUI *gui, int x, int y)
226 : BC_PopupMenu(x, y, calculate_w(gui), "", 1)
228 this->plugin = plugin;
232 int LensMode::handle_event()
234 plugin->config.mode = from_text(get_text());
235 plugin->send_configure_change();
240 void LensMode::create_objects()
242 add_item(new BC_MenuItem(to_text(LensConfig::SPHERICAL_SHRINK)));
243 add_item(new BC_MenuItem(to_text(LensConfig::SPHERICAL_STRETCH)));
244 add_item(new BC_MenuItem(to_text(LensConfig::RECTILINEAR_STRETCH)));
245 add_item(new BC_MenuItem(to_text(LensConfig::RECTILINEAR_SHRINK)));
246 update(plugin->config.mode);
249 void LensMode::update(int mode)
251 char string[BCTEXTLEN];
252 sprintf(string, "%s", to_text(mode));
256 int LensMode::calculate_w(LensGUI *gui)
259 result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(LensConfig::SPHERICAL_STRETCH)));
260 result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(LensConfig::SPHERICAL_SHRINK)));
261 result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(LensConfig::RECTILINEAR_STRETCH)));
262 result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(LensConfig::RECTILINEAR_SHRINK)));
266 int LensMode::from_text(char *text)
268 if( !strcmp(text, _("Sphere Stretch")) ) return LensConfig::SPHERICAL_STRETCH;
269 else if( !strcmp(text, _("Sphere Shrink")) ) return LensConfig::SPHERICAL_SHRINK;
270 else if( !strcmp(text, _("Rectilinear Stretch")) ) return LensConfig::RECTILINEAR_STRETCH;
271 else if( !strcmp(text, _("Rectilinear Shrink")) ) return LensConfig::RECTILINEAR_SHRINK;
272 return LensConfig::SPHERICAL_STRETCH;
275 const char* LensMode::to_text(int mode)
278 case LensConfig::SPHERICAL_STRETCH: return _("Sphere Stretch");
279 case LensConfig::SPHERICAL_SHRINK: return _("Sphere Shrink");
280 case LensConfig::RECTILINEAR_STRETCH: return _("Rectilinear Stretch");
281 case LensConfig::RECTILINEAR_SHRINK: return _("Rectilinear Shrink");
283 return _("Sphere Stretch");
287 // LensPresets::LensPresets(LensMain *plugin,
288 // LensGUI *gui, int x, int y, int w)
289 // : BC_PopupMenu(x, y, w, "", 1)
291 // this->plugin = plugin;
295 // int LensPresets::handle_event()
300 // void LensPresets::create_objects()
302 // // Remove existing items
303 // int total = total_items();
304 // for( int i=0; i<total; ++i ) {
308 // // Create current items
309 // plugin->load_presets();
310 // for( int i=0; i<plugin->presets.total; ++i ) {
311 // add_item(new BC_MenuItem(plugin->presets.values[i]->title));
315 // if( plugin->current_preset >= 0 &&
316 // plugin->current_preset < plugin->presets.total ) {
317 // set_text(plugin->presets.values[plugin->current_preset]->title);
324 // int LensPresets::from_text(LensMain *plugin, char *text)
328 // char* LensPresets::to_text(LensMain *plugin, int preset)
332 // void LensPresets::update(int preset)
337 // LensSavePreset::LensSavePreset(LensMain *plugin, LensGUI *gui, int x, int y)
338 // : BC_GenericButton(x, y, _("Save Preset"))
340 // this->plugin = plugin;
344 // int LensSavePreset::handle_event()
349 // LensDeletePreset::LensDeletePreset(LensMain *plugin, LensGUI *gui, int x, int y)
350 // : BC_GenericButton(x, y, _("Delete Preset"))
354 // int LensDeletePreset::handle_event()
359 // LensPresetText::LensPresetText(LensMain *plugin, LensGUI *gui, int x, int y, int w)
360 // : BC_TextBox(x, y, w, 1, "")
362 // this->plugin = plugin;
366 // int LensPresetText::handle_event()
371 LensInterpItem::LensInterpItem(const char *text, int id)
377 int LensInterpItem::handle_event()
379 LensInterp *interp = (LensInterp *)get_popup_menu();
380 interp->set_value(id);
381 LensMain *plugin = interp->plugin;
382 plugin->config.interp = id;
383 plugin->send_configure_change();
387 LensInterp::LensInterp(LensMain *plugin, int x, int y)
388 : BC_PopupMenu(x, y, 120, "")
390 this->plugin = plugin;
393 void LensInterp::create_objects()
395 add_item(new LensInterpItem(_("Nearest"), LensConfig::INTERP_NEAREST));
396 add_item(new LensInterpItem(_("BiLinear"), LensConfig::INTERP_BILINEAR));
397 add_item(new LensInterpItem(_("BiCubic"), LensConfig::INTERP_BICUBIC));
398 set_value(plugin->config.interp);
401 void LensInterp::set_value(int id)
403 for( int i=0, n=total_items(); i<n; ++i ) {
404 LensInterpItem *item = (LensInterpItem *)get_item(i);
405 if( item->id == id ) {
406 set_text(item->get_text());
413 int LensInterp::get_value()
419 LensReset::LensReset(LensMain *plugin, LensGUI *gui, int x, int y)
420 : BC_GenericButton(x, y, _("Reset"))
422 this->plugin = plugin;
426 int LensReset::handle_event()
428 plugin->config.reset();
430 plugin->send_configure_change();
435 LensGUI::LensGUI(LensMain *plugin)
436 : PluginClientWindow(plugin, 350, 550, 350, 550, 0)
438 this->plugin = plugin;
446 void LensGUI::create_objects()
454 for( int i=0; i<FOV_CHANNELS; ++i ) {
456 case 0: add_tool(title = new BC_Title(x, y, _("R Field of View:"))); break;
457 case 1: add_tool(title = new BC_Title(x, y, _("G Field of View:"))); break;
458 case 2: add_tool(title = new BC_Title(x, y, _("B Field of View:"))); break;
459 case 3: add_tool(title = new BC_Title(x, y, _("A Field of View:"))); break;
462 y += title->get_h() + 5;
463 add_tool(fov_slider[i] = new LensSlider(plugin, this,
464 0, &plugin->config.fov[i], x, y, 0.0001, 1.0));
465 x1 = x + fov_slider[i]->get_w() + 5;
466 add_tool(fov_text[i] = new LensText(plugin, this,
467 fov_slider[i], &plugin->config.fov[i], x1, y));
468 fov_slider[i]->text = fov_text[i];
469 y += fov_text[i]->get_h() + 5;
472 add_tool(toggle = new LensToggle(plugin, &plugin->lock, x, y, _("Lock")));
473 y += toggle->get_h() + 10;
476 add_tool(bar = new BC_Bar(x, y, get_w() - x * 2));
477 y += bar->get_h() + 5;
479 add_tool(title = new BC_Title(x, y, _("Aspect Ratio:")));
480 y += title->get_h() + 5;
481 add_tool(aspect_slider = new LensSlider(plugin, this,
482 0, &plugin->config.aspect, x, y, 0.333, 3.0));
483 x1 = x + aspect_slider->get_w() + 5;
484 add_tool(aspect_text = new LensText(plugin, this,
485 aspect_slider, &plugin->config.aspect, x1, y));
486 aspect_slider->text = aspect_text;
487 y += aspect_text->get_h() + 5;
489 add_tool(title = new BC_Title(x, y, _("Radius:")));
490 y += title->get_h() + 5;
491 add_tool(radius_slider = new LensSlider(plugin, this,
492 0, &plugin->config.radius, x, y, 0.333, 3.0));
493 x1 = x + radius_slider->get_w() + 5;
494 add_tool(radius_text = new LensText(plugin, this,
495 radius_slider, &plugin->config.radius, x1, y));
496 radius_slider->text = radius_text;
497 y += radius_text->get_h() + 5;
499 add_tool(title = new BC_Title(x, y, _("Center X:")));
500 y += title->get_h() + 5;
501 add_tool(centerx_slider = new LensSlider(plugin, this,
502 0, &plugin->config.center_x, x, y, 0.0, 99.0));
503 x1 = x + centerx_slider->get_w() + 5;
504 add_tool(centerx_text = new LensText(plugin, this,
505 centerx_slider, &plugin->config.center_x, x1, y));
506 centerx_slider->text = centerx_text;
507 centerx_slider->set_precision(1.0);
508 y += centerx_text->get_h() + 5;
511 add_tool(title = new BC_Title(x, y, _("Center Y:")));
512 y += title->get_h() + 5;
513 add_tool(centery_slider = new LensSlider(plugin, this,
514 0, &plugin->config.center_y, x, y, 0.0, 99.0));
515 x1 = x + centery_slider->get_w() + 5;
516 add_tool(centery_text = new LensText(plugin, this,
517 centery_slider, &plugin->config.center_y, x1, y));
518 centery_slider->text = centery_text;
519 centery_slider->set_precision(1.0);
520 y += centery_text->get_h() + 10;
522 add_tool(bar = new BC_Bar(x, y, get_w() - x * 2));
523 y += bar->get_h() + 5;
526 // add_tool(reverse = new LensToggle(plugin,
527 // &plugin->config.reverse, x, y, _("Reverse")));
528 // y += reverse->get_h() + 5;
529 add_tool(draw_guides = new LensToggle(plugin,
530 &plugin->config.draw_guides, x, y, _("Draw center")));
531 y += draw_guides->get_h() + 5;
534 add_tool(title = new BC_Title(x, y, _("Mode:")));
535 add_tool(mode = new LensMode(plugin, this,
536 x + title->get_w() + 5, y));
537 mode->create_objects();
538 y += mode->get_h() + 5;
540 add_tool(title = new BC_Title(x, y, _("Interp:")));
541 x1 = x + title->get_w() + 5;
542 add_tool(interp = new LensInterp(plugin, x1, y));
543 interp->create_objects();
544 y += interp->get_h() + 5;
546 add_tool(reset = new LensReset(plugin, this, x, y));
547 y += reset->get_h() + 5;
549 // add_tool(title = new BC_Title(x, y, _("Preset:")));
550 // add_tool(presets = new LensPresets(plugin, this,
551 // x + title->get_w() + 5, y, get_w() - x - title->get_w() - 50));
552 // presets->create_objects();
553 // y += presets->get_h() + 5;
555 // add_tool(save_preset = new LensSavePreset(plugin, this, x, y));
556 // add_tool(preset_text = new LensPresetText(plugin, this,
557 // x + save_preset->get_w() + 5, y,
558 // get_w() - x - save_preset->get_w() - 10));
559 // y += preset_text->get_h() + 5;
560 // add_tool(delete_preset = new LensDeletePreset(plugin, this, x, y));
567 LensMain::LensMain(PluginServer *server)
568 : PluginVClient(server)
576 LensMain::~LensMain()
580 presets.remove_all_objects();
583 NEW_WINDOW_MACRO(LensMain, LensGUI)
584 LOAD_CONFIGURATION_MACRO(LensMain, LensConfig)
585 int LensMain::is_realtime() { return 1; }
586 const char* LensMain::plugin_title() { return _("Lens"); }
588 void LensMain::update_gui()
590 if( !thread ) return;
591 if( !load_configuration() ) return;
592 ((LensGUI *)thread->window)->lock_window("LensMain::update_gui");
593 LensGUI *gui = (LensGUI *)thread->window;
595 gui->unlock_window();
598 void LensGUI::update_gui()
600 LensConfig &config = plugin->config;
601 for( int i=0; i<FOV_CHANNELS; ++i ) {
602 fov_slider[i]->update(config.fov[i]);
603 fov_text[i]->update(config.fov[i]);
605 aspect_slider->update(config.aspect);
606 aspect_text->update(config.aspect);
607 radius_slider->update(config.radius);
608 radius_text->update(config.radius);
609 centerx_slider->update(config.center_x);
610 centerx_text->update(config.center_x);
611 centery_slider->update(config.center_y);
612 centery_text->update(config.center_y);
613 mode->update(config.mode);
614 draw_guides->update(config.draw_guides);
615 interp->set_value(config.interp);
618 //void LensMain::save_presets()
620 // char path[BCTEXTLEN], string[BCTEXTLEN];
621 // sprintf(path, "%s/lenspresets.rc", File::get_config_path());
622 // BC_Hash *defaults = new BC_Hash(path);
625 // defaults->update("TOTAL_PRESETS", presets.total);
626 // for( int i=0; i<presets.total; ++i ) {
627 // LensPreset *preset = presets.values[i];
628 // sprintf(string, "TITLE_%d", i);
629 // defaults->update(string, preset->title);
631 // for( int j=0; j<FOV_CHANNELS; ++j ) {
632 // sprintf(string, "FOCAL_LENGTH_%d_%d", i, j);
633 // defaults->update(string, preset->fov[j]);
636 // sprintf(string, "ASPECT_%d", i);
637 // defaults->update(string, preset->aspect);
638 // sprintf(string, "RADIUS_%d", i);
639 // defaults->update(string, preset->radius);
640 // sprintf(string, "MODE_%d", i);
641 // defaults->update(string, preset->mode);
649 void LensMain::save_data(KeyFrame *keyframe)
652 char string[BCTEXTLEN];
656 // cause data to be stored directly in text
657 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
658 output.tag.set_title("LENS");
659 for( int i = 0; i < FOV_CHANNELS; ++i ) {
660 sprintf(string, "FOCAL_LENGTH%d", i);
661 output.tag.set_property(string, config.fov[i]);
663 output.tag.set_property("ASPECT", config.aspect);
664 output.tag.set_property("RADIUS", config.radius);
665 output.tag.set_property("MODE", config.mode);
666 output.tag.set_property("INTERP", config.interp);
667 output.tag.set_property("CENTER_X", config.center_x);
668 output.tag.set_property("CENTER_Y", config.center_y);
669 output.tag.set_property("DRAW_GUIDES", config.draw_guides);
671 output.tag.set_title("/LENS");
673 output.append_newline();
674 output.terminate_string();
679 void LensMain::read_data(KeyFrame *keyframe)
682 char string[BCTEXTLEN];
685 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
691 result = input.read_tag();
694 if( input.tag.title_is("LENS") ) {
695 for( int i=0; i<FOV_CHANNELS; ++i ) {
696 sprintf(string, "FOCAL_LENGTH%d", i);
697 config.fov[i] = input.tag.get_property(string, config.fov[i]);
699 config.aspect = input.tag.get_property("ASPECT", config.aspect);
700 config.radius = input.tag.get_property("RADIUS", config.radius);
701 config.mode = input.tag.get_property("MODE", config.mode);
702 config.interp = input.tag.get_property("INTERP", config.interp);
703 config.center_x = input.tag.get_property("CENTER_X", config.center_x);
704 config.center_y = input.tag.get_property("CENTER_Y", config.center_y);
705 config.draw_guides = input.tag.get_property("DRAW_GUIDES", config.draw_guides);
713 int LensMain::process_buffer(VFrame *frame,
714 int64_t start_position,
718 load_configuration();
720 if( get_use_opengl() ) {
724 input = new_temp(frame->get_w(), frame->get_h(), frame->get_color_model());
734 if( get_use_opengl() ) {
739 if( !engine ) engine = new LensEngine(this);
740 engine->process_packages();
741 if( config.draw_guides ) {
745 #define DRAW_GUIDES(components, type, max) \
747 type **rows = (type**)get_output()->get_rows(); \
748 if( (center_x >= 0 && center_x < w) || (center_y >= 0 && center_y < h) ) { \
749 type *hrow = rows[center_y] + components * (center_x - CENTER_W / 2); \
750 for( int i=center_x-CENTER_W/2; i<=center_x+CENTER_W/2; ++i ) { \
751 if( i >= 0 && i < w ) { \
752 hrow[0] = max - hrow[0]; \
753 hrow[1] = max - hrow[1]; \
754 hrow[2] = max - hrow[2]; \
755 hrow += components; \
759 for( int i=center_y-CENTER_H/2; i<=center_y+CENTER_H/2; ++i ) { \
760 if( i >= 0 && i < h ) { \
761 type *vrow = rows[i] + center_x * components; \
762 vrow[0] = max - vrow[0]; \
763 vrow[1] = max - vrow[1]; \
764 vrow[2] = max - vrow[2]; \
770 int w = get_output()->get_w();
771 int h = get_output()->get_h();
772 int center_x = (int)(config.center_x * w / 100);
773 int center_y = (int)(config.center_y * h / 100);
774 switch( get_output()->get_color_model() )
777 DRAW_GUIDES(3, float, 1.0)
780 DRAW_GUIDES(4, float, 1.0)
783 DRAW_GUIDES(3, unsigned char, 0xff)
786 DRAW_GUIDES(4, unsigned char, 0xff)
789 DRAW_GUIDES(3, unsigned char, 0xff)
792 DRAW_GUIDES(4, unsigned char, 0xff)
803 int LensMain::handle_opengl()
806 static const char *shrink_frag =
807 "uniform sampler2D tex;\n"
808 "uniform vec2 texture_extents;\n"
809 "uniform vec2 image_extents;\n"
810 "uniform vec2 aspect;\n"
811 "uniform vec2 center_coord;\n"
812 "uniform vec4 border_color;\n"
814 "uniform vec4 max_z;\n"
817 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
818 " vec2 coord_diff = outcoord - center_coord;\n"
819 " if( coord_diff.x == 0.0 && coord_diff.y == 0.0 )\n"
821 " gl_FragColor = texture2D(tex, outcoord);\n"
825 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
826 " coord_diff.y * coord_diff.y);\n"
827 " float a2 = atan(coord_diff.y, coord_diff.x);\n"
828 " vec4 a1 = asin(vec4(z, z, z, z) / r);\n"
829 " vec4 z_in = a1 * max_z * 2.0 / 3.14159;\n"
832 " in_x = z_in * cos(a2) * aspect.x + center_coord.x;\n"
833 " in_y = z_in * sin(a2) * aspect.y + center_coord.y;\n"
834 " if( z > r.r || in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y )\n"
835 " gl_FragColor.r = border_color.r;\n"
837 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
838 " if( z > r.g || in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y )\n"
839 " gl_FragColor.g = border_color.g;\n"
841 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
842 " if( z > r.b || in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y )\n"
843 " gl_FragColor.b = border_color.b;\n"
845 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
846 " if( z > r.a || in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y )\n"
847 " gl_FragColor.a = border_color.a;\n"
849 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
853 static const char *stretch_frag =
854 "uniform sampler2D tex;\n"
855 "uniform vec2 texture_extents;\n"
856 "uniform vec2 image_extents;\n"
857 "uniform vec2 aspect;\n"
858 "uniform vec2 center_coord;\n"
859 "uniform vec4 border_color;\n"
863 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
864 " vec2 coord_diff = outcoord - center_coord;\n"
865 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
866 " coord_diff.y * coord_diff.y);\n"
867 " vec4 a1 = (vec4(z, z, z, z) / (3.14159 * r / 2.0)) * (3.14159 / 2.0);\n"
868 " vec4 z_in = r * sin(a1);\n"
870 " if( coord_diff.x == 0.0 )\n"
872 " if( coord_diff.y < 0.0 )\n"
873 " a2 = 3.0 * 3.14159 / 2.0;\n"
875 " a2 = 3.14159 / 2.0;\n"
878 " a2 = atan(coord_diff.y, coord_diff.x);\n"
881 " in_x = z_in * cos(a2) * aspect.x + center_coord.x;\n"
882 " in_y = z_in * sin(a2) * aspect.y + center_coord.y;\n"
883 " if( in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y )\n"
884 " gl_FragColor.r = border_color.r;\n"
886 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
887 " if( in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y )\n"
888 " gl_FragColor.g = border_color.g;\n"
890 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
891 " if( in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y )\n"
892 " gl_FragColor.b = border_color.b;\n"
894 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
895 " if( in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y )\n"
896 " gl_FragColor.a = border_color.a;\n"
898 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
902 static const char *rectilinear_stretch_frag =
903 "uniform sampler2D tex;\n"
904 "uniform vec2 texture_extents;\n"
905 "uniform vec2 image_extents;\n"
906 "uniform vec2 aspect;\n"
907 "uniform vec2 center_coord;\n"
908 "uniform vec4 border_color;\n"
910 "uniform float radius;\n"
913 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
914 " vec2 coord_diff = outcoord - center_coord;\n"
915 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
916 " coord_diff.y * coord_diff.y);\n"
917 " vec4 radius1 = (vec4(z, z, z, z) / r) * 2.0 * radius;\n"
918 " vec4 z_in = r * atan(radius1) / (3.14159 / 2.0);\n"
921 " if( coord_diff.x == 0.0 )\n"
923 " if( coord_diff.y < 0.0 )\n"
924 " angle = 3.0 * 3.14159 / 2.0;\n"
926 " angle = 3.14159 / 2.0;\n"
929 " angle = atan(coord_diff.y, coord_diff.x);\n"
933 " in_x = z_in * cos(angle) * aspect.x + center_coord.x;\n"
934 " in_y = z_in * sin(angle) * aspect.y + center_coord.y;\n"
935 " if( in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y )\n"
936 " gl_FragColor.r = border_color.r;\n"
938 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
939 " if( in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y )\n"
940 " gl_FragColor.g = border_color.g;\n"
942 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
943 " if( in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y )\n"
944 " gl_FragColor.b = border_color.b;\n"
946 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
947 " if( in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y )\n"
948 " gl_FragColor.a = border_color.a;\n"
950 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
953 static const char *rectilinear_shrink_frag =
954 "uniform sampler2D tex;\n"
955 "uniform vec2 texture_extents;\n"
956 "uniform vec2 image_extents;\n"
957 "uniform vec2 aspect;\n"
958 "uniform vec2 center_coord;\n"
959 "uniform vec4 border_color;\n"
961 "uniform float radius;\n"
964 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
965 " vec2 coord_diff = outcoord - center_coord;\n"
966 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
967 " coord_diff.y * coord_diff.y);\n"
968 " vec4 radius1 = (vec4(z, z, z, z) / r) * 2.0 * radius;\n"
969 " vec4 z_in = r * atan(radius1) / (3.14159 / 2.0);\n"
972 " if( coord_diff.x == 0.0 )\n"
974 " if( coord_diff.y < 0.0 )\n"
975 " angle = 3.0 * 3.14159 / 2.0;\n"
977 " angle = 3.14159 / 2.0;\n"
980 " angle = atan(coord_diff.y, coord_diff.x);\n"
984 " in_x = z_in * cos(angle) * aspect.x + center_coord.x;\n"
985 " in_y = z_in * sin(angle) * aspect.y + center_coord.y;\n"
986 " if( in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y )\n"
987 " gl_FragColor.r = border_color.r;\n"
989 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
990 " if( in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y )\n"
991 " gl_FragColor.g = border_color.g;\n"
993 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
994 " if( in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y )\n"
995 " gl_FragColor.b = border_color.b;\n"
997 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
998 " if( in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y )\n"
999 " gl_FragColor.a = border_color.a;\n"
1001 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
1004 get_output()->to_texture();
1005 get_output()->enable_opengl();
1006 unsigned int frag_shader = 0;
1008 switch( config.mode )
1010 case LensConfig::SPHERICAL_SHRINK:
1011 frag_shader = VFrame::make_shader(0, shrink_frag, 0);
1013 case LensConfig::SPHERICAL_STRETCH:
1014 frag_shader = VFrame::make_shader(0, stretch_frag, 0);
1016 case LensConfig::RECTILINEAR_STRETCH:
1017 frag_shader = VFrame::make_shader(0, rectilinear_stretch_frag, 0);
1019 case LensConfig::RECTILINEAR_SHRINK:
1020 frag_shader = VFrame::make_shader(0, rectilinear_shrink_frag, 0);
1024 if( frag_shader > 0 ) {
1025 float border_color[] = { 0, 0, 0, 0 };
1026 if( BC_CModels::is_yuv(get_output()->get_color_model()) ) {
1027 border_color[1] = 0.5;
1028 border_color[2] = 0.5;
1031 double x_factor = config.aspect;
1032 double y_factor = 1.0 / config.aspect;
1033 if( x_factor < 1 ) x_factor = 1;
1034 if( y_factor < 1 ) y_factor = 1;
1036 glUseProgram(frag_shader);
1037 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
1038 glUniform2f(glGetUniformLocation(frag_shader, "aspect"),
1041 glUniform2f(glGetUniformLocation(frag_shader, "center_coord"),
1042 (GLfloat)get_input()->get_w() * config.center_x / 100.0,
1043 (GLfloat)get_input()->get_h() * config.center_y / 100.0);
1044 glUniform2f(glGetUniformLocation(frag_shader, "texture_extents"),
1045 (GLfloat)get_input()->get_texture_w(),
1046 (GLfloat)get_input()->get_texture_h());
1047 glUniform2f(glGetUniformLocation(frag_shader, "image_extents"),
1048 (GLfloat)get_input()->get_w(),
1049 (GLfloat)get_input()->get_h());
1051 int width = get_output()->get_w();
1052 int height = get_output()->get_h();
1053 float *fov = config.fov;
1056 switch( config.mode )
1058 case LensConfig::SPHERICAL_SHRINK:
1059 dim = MAX(width, height) * config.radius;
1060 max_z = dim * sqrt(2.0) / 2;
1061 glUniform4fv(glGetUniformLocation(frag_shader, "border_color"),
1063 (GLfloat*)border_color);
1064 glUniform4f(glGetUniformLocation(frag_shader, "max_z"),
1069 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1070 (max_z / fov[0]) * 2 / M_PI,
1071 (max_z / fov[1]) * 2 / M_PI,
1072 (max_z / fov[2]) * 2 / M_PI,
1073 (max_z / fov[3]) * 2 / M_PI);
1076 case LensConfig::SPHERICAL_STRETCH:
1077 dim = MAX(width, height) * config.radius;
1078 max_z = dim * sqrt(2.0) / 2;
1079 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1080 max_z / M_PI / (fov[0] / 2.0),
1081 max_z / M_PI / (fov[1] / 2.0),
1082 max_z / M_PI / (fov[2] / 2.0),
1083 max_z / M_PI / (fov[3] / 2.0));
1086 case LensConfig::RECTILINEAR_STRETCH:
1087 max_z = sqrt(SQR(width) + SQR(height)) / 2;
1088 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1089 max_z / M_PI / (fov[0] / 2.0),
1090 max_z / M_PI / (fov[1] / 2.0),
1091 max_z / M_PI / (fov[2] / 2.0),
1092 max_z / M_PI / (fov[3] / 2.0));
1093 glUniform1f(glGetUniformLocation(frag_shader, "radius"),
1097 case LensConfig::RECTILINEAR_SHRINK:
1098 max_z = sqrt(SQR(width) + SQR(height)) / 2;
1099 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1100 max_z / M_PI / (fov[0] / 2.0),
1101 max_z / M_PI / (fov[1] / 2.0),
1102 max_z / M_PI / (fov[2] / 2.0),
1103 max_z / M_PI / (fov[3] / 2.0));
1104 glUniform1f(glGetUniformLocation(frag_shader, "radius"),
1110 get_output()->init_screen();
1111 get_output()->bind_texture(0);
1112 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);
1113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
1114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
1115 get_output()->draw_texture();
1119 if( config.draw_guides ) {
1120 int w = get_output()->get_w();
1121 int h = get_output()->get_h();
1122 int center_x = (int)(config.center_x * w / 100);
1123 int center_y = (int)(config.center_y * h / 100);
1125 glDisable(GL_TEXTURE_2D);
1126 glColor4f(0.0, 0.0, 0.0, 1.0);
1128 glVertex3f(center_x, -h + center_y - CENTER_H / 2, 0.0);
1129 glVertex3f(center_x, -h + center_y + CENTER_H / 2, 0.0);
1132 glVertex3f(center_x - CENTER_W / 2, -h + center_y, 0.0);
1133 glVertex3f(center_x + CENTER_W / 2, -h + center_y, 0.0);
1135 glColor4f(1.0, 1.0, 1.0, 1.0);
1137 glVertex3f(center_x - 1, -h + center_y - CENTER_H / 2 - 1, 0.0);
1138 glVertex3f(center_x - 1, -h + center_y + CENTER_H / 2 - 1, 0.0);
1141 glVertex3f(center_x - CENTER_W / 2 - 1, -h + center_y - 1, 0.0);
1142 glVertex3f(center_x + CENTER_W / 2 - 1, -h + center_y - 1, 0.0);
1145 get_output()->set_opengl_state(VFrame::SCREEN);
1152 // do using specified interpolation
1153 #define DO_LENS(type) \
1154 int icolor_model = plugin->get_input()->get_color_model(); \
1155 switch( plugin->config.interp ) { \
1156 case LensConfig::INTERP_NEAREST: \
1157 switch( icolor_model ) { \
1159 DO_LENS_##type(unsigned char, 3, 0xff, 0x0, nearest); \
1162 DO_LENS_##type(unsigned char, 4, 0xff, 0x0, nearest); \
1164 case BC_RGB_FLOAT: \
1165 DO_LENS_##type(float, 3, 1.0, 0.0, nearest); \
1167 case BC_RGBA_FLOAT: \
1168 DO_LENS_##type(float, 4, 1.0, 0.0, nearest); \
1171 DO_LENS_##type(unsigned char, 3, 0xff, 0x80, nearest); \
1174 DO_LENS_##type(unsigned char, 4, 0xff, 0x80, nearest); \
1178 case LensConfig::INTERP_BILINEAR: \
1179 switch( icolor_model ) { \
1181 DO_LENS_##type(unsigned char, 3, 0xff, 0x0, bi_linear); \
1184 DO_LENS_##type(unsigned char, 4, 0xff, 0x0, bi_linear); \
1186 case BC_RGB_FLOAT: \
1187 DO_LENS_##type(float, 3, 1.0, 0.0, bi_linear); \
1189 case BC_RGBA_FLOAT: \
1190 DO_LENS_##type(float, 4, 1.0, 0.0, bi_linear); \
1193 DO_LENS_##type(unsigned char, 3, 0xff, 0x80, bi_linear); \
1196 DO_LENS_##type(unsigned char, 4, 0xff, 0x80, bi_linear); \
1200 case LensConfig::INTERP_BICUBIC: \
1201 switch( icolor_model ) { \
1203 DO_LENS_##type(unsigned char, 3, 0xff, 0x0, bi_cubic); \
1206 DO_LENS_##type(unsigned char, 4, 0xff, 0x0, bi_cubic); \
1208 case BC_RGB_FLOAT: \
1209 DO_LENS_##type(float, 3, 1.0, 0.0, bi_cubic); \
1211 case BC_RGBA_FLOAT: \
1212 DO_LENS_##type(float, 4, 1.0, 0.0, bi_cubic); \
1215 DO_LENS_##type(unsigned char, 3, 0xff, 0x80, bi_cubic); \
1218 DO_LENS_##type(unsigned char, 4, 0xff, 0x80, bi_cubic); \
1224 LensPackage::LensPackage()
1229 LensUnit::LensUnit(LensEngine *engine, LensMain *plugin)
1230 : LoadClient(engine)
1232 this->plugin = plugin;
1235 LensUnit::~LensUnit()
1239 void LensUnit::process_spherical_stretch(LensPackage *pkg)
1241 float *fov = plugin->config.fov;
1242 float aspect = plugin->config.aspect;
1243 int row1 = pkg->row1;
1244 int row2 = pkg->row2;
1245 double x_factor = aspect;
1246 double y_factor = 1.0 / aspect;
1247 if( x_factor < 1 ) x_factor = 1;
1248 if( y_factor < 1 ) y_factor = 1;
1249 int width = plugin->get_input()->get_w();
1250 int height = plugin->get_input()->get_h();
1251 double dim = MAX(width, height) * plugin->config.radius;
1252 double max_z = dim * sqrt(2.0) / 2;
1253 double center_x = width * plugin->config.center_x / 100.0;
1254 double center_y = height * plugin->config.center_y / 100.0;
1255 double r[FOV_CHANNELS];
1257 r[0] = max_z / M_PI / (fov[0] / 2.0);
1258 r[1] = max_z / M_PI / (fov[1] / 2.0);
1259 r[2] = max_z / M_PI / (fov[2] / 2.0);
1260 r[3] = max_z / M_PI / (fov[3] / 2.0);
1262 #define DO_LENS_SPHERICAL_STRETCH(type, components, max, chroma, interp) { \
1263 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1264 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1265 type black[4] = { 0, chroma, chroma, 0 }; \
1266 INTERP_SETUP(in_rows, max, 0,0, width,height); \
1268 for( int y=row1; y<row2; ++y ) { \
1269 type *out_row = out_rows[y]; \
1270 double y_diff = y - center_y; \
1271 for( int x=0; x<width; ++x ) { \
1272 double x_diff = (x - center_x); \
1273 double z = sqrt(x_diff * x_diff + y_diff * y_diff); \
1274 double a2 = x != center_x ? atan(y_diff / x_diff) : \
1275 y < center_y ? 3*M_PI/2 : M_PI/2; \
1276 if( x_diff < 0.0 ) a2 += M_PI; \
1277 for( int i=0; i<components; ++i ) { \
1278 double a1 = (z / (M_PI * r[i] / 2)) * (M_PI / 2); \
1279 double z_in = r[i] * sin(a1); \
1280 double x_in = z_in * cos(a2) * x_factor + center_x; \
1281 double y_in = z_in * sin(a2) * y_factor + center_y; \
1282 interp##_SETUP(type, components, x_in, y_in); \
1283 for( int j=0; j<i; ++j ) interp##_next(); \
1284 *out_row++ = interp##_interp(black[i], black[i]); \
1289 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1290 type *inp_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1291 for( int i=0; i<components; ++i ) *out_pixel++ = *inp_pixel++; \
1294 DO_LENS(SPHERICAL_STRETCH);
1297 void LensUnit::process_spherical_shrink(LensPackage *pkg)
1300 float *fov = plugin->config.fov;
1301 float aspect = plugin->config.aspect;
1302 int row1 = pkg->row1;
1303 int row2 = pkg->row2;
1304 int width = plugin->get_input()->get_w();
1305 int height = plugin->get_input()->get_h();
1306 double x_factor = aspect;
1307 double y_factor = 1.0 / aspect;
1308 if( x_factor < 1 ) x_factor = 1;
1309 if( y_factor < 1 ) y_factor = 1;
1310 double dim = MAX(width, height) * plugin->config.radius;
1311 double max_z[FOV_CHANNELS];
1312 double center_x = width * plugin->config.center_x / 100.0;
1313 double center_y = height * plugin->config.center_y / 100.0;
1314 double r[FOV_CHANNELS];
1316 // max_z[0] = sqrt(SQR(width) + SQR(height)) / 2 / fov[0];
1317 // max_z[1] = sqrt(SQR(width) + SQR(height)) / 2 / fov[1];
1318 // max_z[2] = sqrt(SQR(width) + SQR(height)) / 2 / fov[2];
1319 // max_z[3] = sqrt(SQR(width) + SQR(height)) / 2 / fov[3];
1320 max_z[0] = dim * sqrt(2.0) / 2 / fov[0];
1321 max_z[1] = dim * sqrt(2.0) / 2 / fov[1];
1322 max_z[2] = dim * sqrt(2.0) / 2 / fov[2];
1323 max_z[3] = dim * sqrt(2.0) / 2 / fov[3];
1324 r[0] = max_z[0] * 2 / M_PI;
1325 r[1] = max_z[1] * 2 / M_PI;
1326 r[2] = max_z[2] * 2 / M_PI;
1327 r[3] = max_z[3] * 2 / M_PI;
1329 #define DO_LENS_SPHERICAL_SHRINK(type, components, max, chroma, interp) { \
1330 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1331 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1332 type black[4] = { 0, chroma, chroma, 0 }; \
1333 INTERP_SETUP(in_rows, max, 0,0, width,height); \
1335 for( int y=row1; y<row2; ++y ) { \
1336 type *out_row = out_rows[y]; \
1337 type *in_row = in_rows[y]; \
1338 double y_diff = y - center_y; \
1339 for( int x=0; x<width; ++x ) { \
1340 double x_diff = x - center_x; \
1341 if( !x_diff && !y_diff ) { \
1342 type *inp_pixel = in_row + x * components; \
1343 for( int c=0; c<components; ++c ) \
1344 *out_row++ = *inp_pixel++; \
1347 double z = sqrt(x_diff * x_diff + y_diff * y_diff); \
1348 double a2 = atan(y_diff / x_diff); \
1349 if( x_diff < 0.0 ) a2 += M_PI; \
1350 for( int i=0; i<components; ++i ) { \
1351 if( z > r[i] ) { *out_row++ = black[i]; continue; } \
1352 double a1 = asin(z / r[i]); \
1353 double z_in = a1 * max_z[i] * 2 / M_PI; \
1354 float x_in = z_in * cos(a2) * x_factor + center_x; \
1355 float y_in = z_in * sin(a2) * y_factor + center_y; \
1356 interp##_SETUP(type, components, x_in, y_in); \
1357 for( int j=0; j<i; ++j ) interp##_next(); \
1358 *out_row++ = interp##_interp(black[i], black[i]); \
1363 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1364 type *inp_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1365 for( int i=0; i<components; ++i ) *out_pixel++ = *inp_pixel++; \
1368 DO_LENS(SPHERICAL_SHRINK);
1371 void LensUnit::process_rectilinear_stretch(LensPackage *pkg)
1373 float *fov = plugin->config.fov;
1374 float aspect = plugin->config.aspect;
1375 int row1 = pkg->row1;
1376 int row2 = pkg->row2;
1377 double x_factor = aspect;
1378 double y_factor = 1.0 / aspect;
1379 if( x_factor < 1 ) x_factor = 1;
1380 if( y_factor < 1 ) y_factor = 1;
1381 int width = plugin->get_input()->get_w();
1382 int height = plugin->get_input()->get_h();
1383 // double dim = MAX(width, height) * plugin->config.radius;
1384 // double max_z = dim * sqrt(2.0) / 2;
1385 double max_z = sqrt(SQR(width) + SQR(height)) / 2;
1386 double center_x = width * plugin->config.center_x / 100.0;
1387 double center_y = height * plugin->config.center_y / 100.0;
1388 double r[FOV_CHANNELS];
1390 r[0] = max_z / M_PI / (fov[0] / 2.0);
1391 r[1] = max_z / M_PI / (fov[1] / 2.0);
1392 r[2] = max_z / M_PI / (fov[2] / 2.0);
1393 r[3] = max_z / M_PI / (fov[3] / 2.0);
1395 #define DO_LENS_RECTILINEAR_STRETCH(type, components, max, chroma, interp) { \
1396 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1397 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1398 type black[4] = { 0, chroma, chroma, 0 }; \
1399 INTERP_SETUP(in_rows, max, 0,0, width,height); \
1401 for( int y=row1; y<row2; ++y ) { \
1402 type *out_row = out_rows[y]; \
1403 double y_diff = y - center_y; \
1404 for( int x=0; x<width; ++x ) { \
1405 double x_diff = (x - center_x); /* Compute magnitude / angle */ \
1406 double z = sqrt(x_diff * x_diff + y_diff * y_diff); \
1407 double angle = x != center_x ? atan(y_diff / x_diff) : \
1408 y < center_y ? 3*M_PI/2 : M_PI/2; \
1409 if( x_diff < 0.0 ) angle += M_PI; \
1410 for( int i=0; i<components; ++i ) { /* Compute new radius */ \
1411 double radius1 = (z / r[i]) * 2 * plugin->config.radius; \
1412 double z_in = r[i] * atan(radius1) / (M_PI / 2); \
1413 double x_in = z_in * cos(angle) * x_factor + center_x; \
1414 double y_in = z_in * sin(angle) * y_factor + center_y; \
1415 interp##_SETUP(type, components, x_in, y_in); \
1416 for( int j=0; j<i; ++j ) interp##_next(); \
1417 *out_row++ = interp##_interp(black[i], black[i]); \
1422 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1423 type *inp_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1424 for( int i=0; i<components; ++i ) *out_pixel++ = *inp_pixel++; \
1427 DO_LENS(RECTILINEAR_STRETCH);
1430 void LensUnit::process_rectilinear_shrink(LensPackage *pkg)
1432 float *fov = plugin->config.fov;
1433 float aspect = plugin->config.aspect;
1434 int row1 = pkg->row1;
1435 int row2 = pkg->row2;
1436 double x_factor = aspect;
1437 double y_factor = 1.0 / aspect;
1438 if( x_factor < 1 ) x_factor = 1;
1439 if( y_factor < 1 ) y_factor = 1;
1440 int width = plugin->get_input()->get_w();
1441 int height = plugin->get_input()->get_h();
1442 double max_z = MAX(width, height) / 2 * plugin->config.radius;
1443 double center_x = width * plugin->config.center_x / 100.0;
1444 double center_y = height * plugin->config.center_y / 100.0;
1445 double r[FOV_CHANNELS];
1447 r[0] = max_z / fov[0];
1448 r[1] = max_z / fov[1];
1449 r[2] = max_z / fov[2];
1450 r[3] = max_z / fov[3];
1452 #define DO_LENS_RECTILINEAR_SHRINK(type, components, max, chroma, interp) { \
1453 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1454 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1455 type black[4] = { 0, chroma, chroma, 0 }; \
1456 INTERP_SETUP(in_rows, max, 0,0, width,height); \
1458 for( int y=row1; y<row2; ++y ) { \
1459 type *out_row = out_rows[y]; \
1460 double y_diff = y - center_y; \
1461 for( int x=0; x<width; ++x ) { \
1462 double x_diff = (x - center_x); /* Compute magnitude/angle */ \
1463 double z = sqrt(x_diff * x_diff + y_diff * y_diff); \
1464 double angle = x != center_x ? atan(y_diff / x_diff) : \
1465 y < center_y ? 3*M_PI/2 : M_PI/2; \
1466 if( x_diff < 0.0 ) angle += M_PI; \
1467 for( int i=0; i<components; ++i ) { /* Compute new radius */ \
1468 double radius1 = z / r[i]; \
1469 double z_in = r[i] * tan(radius1) / (M_PI / 2); \
1470 double x_in = z_in * cos(angle) * x_factor + center_x; \
1471 double y_in = z_in * sin(angle) * y_factor + center_y; \
1472 interp##_SETUP(type, components, x_in, y_in); \
1473 for( int j=0; j<i; ++j ) interp##_next(); \
1474 *out_row++ = interp##_interp(black[i], black[i]); \
1479 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1480 type *inp_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1481 for( int i=0; i<components; ++i ) *out_pixel++ = *inp_pixel++; \
1484 DO_LENS(RECTILINEAR_SHRINK);
1487 void LensUnit::process_package(LoadPackage *package)
1489 LensPackage *pkg = (LensPackage*)package;
1491 switch( plugin->config.mode ) {
1492 case LensConfig::SPHERICAL_STRETCH:
1493 process_spherical_stretch(pkg);
1495 case LensConfig::SPHERICAL_SHRINK:
1496 process_spherical_shrink(pkg);
1498 case LensConfig::RECTILINEAR_STRETCH:
1499 process_rectilinear_stretch(pkg);
1501 case LensConfig::RECTILINEAR_SHRINK:
1502 process_rectilinear_shrink(pkg);
1508 LensEngine::LensEngine(LensMain *plugin)
1509 : LoadServer(plugin->PluginClient::smp + 1, plugin->PluginClient::smp + 1)
1510 // : LoadServer(1, 1)
1512 this->plugin = plugin;
1515 LensEngine::~LensEngine()
1519 void LensEngine::init_packages()
1521 int row1 = 0, row2 = 0, n = LoadServer::get_total_packages();
1522 for( int i=0; i<n; row1=row2 ) {
1523 LensPackage *package = (LensPackage*)LoadServer::get_package(i);
1524 row2 = plugin->get_input()->get_h() * ++i / n;
1525 package->row1 = row1; package->row2 = row2;
1529 LoadClient* LensEngine::new_client()
1531 return new LensUnit(this, plugin);
1534 LoadPackage* LensEngine::new_package()
1536 return new LensPackage;