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"
36 REGISTER_PLUGIN(LensMain)
41 LensConfig::LensConfig()
43 for(int i = 0; i < FOV_CHANNELS; i++)
47 mode = LensConfig::SHRINK;
53 int LensConfig::equivalent(LensConfig &that)
55 for(int i = 0; i < FOV_CHANNELS; i++)
56 if(!EQUIV(fov[i], that.fov[i])) return 0;
57 return EQUIV(aspect, that.aspect) &&
58 EQUIV(radius, that.radius) &&
59 EQUIV(center_x, that.center_x) &&
60 EQUIV(center_y, that.center_y) &&
62 draw_guides == that.draw_guides;
65 void LensConfig::copy_from(LensConfig &that)
67 for(int i = 0; i < FOV_CHANNELS; i++)
72 center_x = that.center_x;
73 center_y = that.center_y;
74 draw_guides = that.draw_guides;
77 void LensConfig::interpolate(LensConfig &prev,
81 int64_t current_frame)
83 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
84 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
86 for(int i = 0; i < FOV_CHANNELS; i++)
87 fov[i] = prev.fov[i] * prev_scale + next.fov[i] * next_scale;
88 aspect = prev.aspect * prev_scale + next.aspect * next_scale;
89 radius = prev.radius * prev_scale + next.radius * next_scale;
90 center_x = prev.center_x * prev_scale + next.center_x * next_scale;
91 center_y = prev.center_y * prev_scale + next.center_y * next_scale;
93 draw_guides = prev.draw_guides;
98 void LensConfig::boundaries()
100 CLAMP(center_x, 0.0, 99.0);
101 CLAMP(center_y, 0.0, 99.0);
102 for(int i = 0; i < FOV_CHANNELS; i++)
103 CLAMP(fov[i], 0.0, 1.0);
104 CLAMP(aspect, 0.3, 3.0);
105 CLAMP(radius, 0.3, 3.0);
111 LensSlider::LensSlider(LensMain *client,
119 : BC_FSlider(x, y, 0, 200, 200, min, max, *output)
122 this->client = client;
123 this->output = output;
128 int LensSlider::handle_event()
130 float prev_output = *output;
131 *output = get_value();
132 text->update(*output);
134 float difference = *output - prev_output;
139 for(int i = 0; i < FOV_CHANNELS; i++)
141 if(output == &client->config.fov[i])
150 for(int i = 0; i < FOV_CHANNELS; i++)
152 if(output != &client->config.fov[i])
154 client->config.fov[i] += difference;
155 client->config.boundaries();
156 gui->fov_slider[i]->update(client->config.fov[i]);
157 gui->fov_text[i]->update(client->config.fov[i]);
163 client->send_configure_change();
169 LensText::LensText(LensMain *client,
175 : BC_TextBox(x, y, 100, 1, *output)
178 this->client = client;
179 this->output = output;
180 this->slider = slider;
183 int LensText::handle_event()
185 float prev_output = *output;
186 *output = atof(get_text());
187 slider->update(*output);
189 float difference = *output - prev_output;
194 for(int i = 0; i < FOV_CHANNELS; i++)
196 if(output == &client->config.fov[i])
205 for(int i = 0; i < FOV_CHANNELS; i++)
207 if(output != &client->config.fov[i])
209 client->config.fov[i] += difference;
210 client->config.boundaries();
211 gui->fov_slider[i]->update(client->config.fov[i]);
212 gui->fov_text[i]->update(client->config.fov[i]);
218 client->send_configure_change();
224 LensToggle::LensToggle(LensMain *client,
229 : BC_CheckBox(x, y, *output, text)
231 this->output = output;
232 this->client = client;
235 int LensToggle::handle_event()
237 *output = get_value();
238 client->send_configure_change();
251 LensMode::LensMode(LensMain *plugin,
261 this->plugin = plugin;
265 int LensMode::handle_event()
267 plugin->config.mode = from_text(get_text());
268 plugin->send_configure_change();
273 void LensMode::create_objects()
275 add_item(new BC_MenuItem(to_text(LensConfig::SHRINK)));
276 add_item(new BC_MenuItem(to_text(LensConfig::STRETCH)));
277 add_item(new BC_MenuItem(to_text(LensConfig::RECTILINEAR_STRETCH)));
278 add_item(new BC_MenuItem(to_text(LensConfig::RECTILINEAR_SHRINK)));
279 update(plugin->config.mode);
282 void LensMode::update(int mode)
284 char string[BCTEXTLEN];
285 sprintf(string, "%s", to_text(mode));
289 int LensMode::calculate_w(LensGUI *gui)
292 result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(LensConfig::STRETCH)));
293 result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(LensConfig::SHRINK)));
294 result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(LensConfig::RECTILINEAR_STRETCH)));
295 result = MAX(result, gui->get_text_width(MEDIUMFONT, to_text(LensConfig::RECTILINEAR_SHRINK)));
299 int LensMode::from_text(char *text)
301 if(!strcmp(text, _("Sphere Stretch"))) return LensConfig::STRETCH;
303 if(!strcmp(text, _("Sphere Shrink"))) return LensConfig::SHRINK;
305 if(!strcmp(text, _("Rectilinear Stretch"))) return LensConfig::RECTILINEAR_STRETCH;
307 if(!strcmp(text, _("Rectilinear Shrink"))) return LensConfig::RECTILINEAR_SHRINK;
308 return LensConfig::STRETCH;
311 const char* LensMode::to_text(int mode)
315 case LensConfig::STRETCH:
316 return _("Sphere Stretch");
318 case LensConfig::SHRINK:
319 return _("Sphere Shrink");
321 case LensConfig::RECTILINEAR_STRETCH:
322 return _("Rectilinear Stretch");
324 case LensConfig::RECTILINEAR_SHRINK:
325 return _("Rectilinear Shrink");
335 // LensPresets::LensPresets(LensMain *plugin,
346 // this->plugin = plugin;
350 // int LensPresets::handle_event()
355 // void LensPresets::create_objects()
357 // // Remove existing items
358 // int total = total_items();
359 // for(int i = 0; i < total; i++)
361 // BC_MenuItem *item = get_item(0);
362 // remove_item(item);
365 // // Create current items
366 // plugin->load_presets();
367 // for(int i = 0; i < plugin->presets.total; i++)
369 // add_item(new BC_MenuItem(plugin->presets.values[i]->title));
373 // if(plugin->current_preset >= 0 &&
374 // plugin->current_preset < plugin->presets.total)
376 // set_text(plugin->presets.values[plugin->current_preset]->title);
384 // int LensPresets::from_text(LensMain *plugin, char *text)
388 // char* LensPresets::to_text(LensMain *plugin, int preset)
392 // void LensPresets::update(int preset)
401 // LensSavePreset::LensSavePreset(LensMain *plugin,
405 // : BC_GenericButton(x, y, "Save Preset")
407 // this->plugin = plugin;
411 // int LensSavePreset::handle_event()
421 // LensDeletePreset::LensDeletePreset(LensMain *plugin,
425 // : BC_GenericButton(x, y, "Delete Preset")
429 // int LensDeletePreset::handle_event()
439 // LensPresetText::LensPresetText(LensMain *plugin,
444 // : BC_TextBox(x, y, w, 1, "")
446 // this->plugin = plugin;
450 // int LensPresetText::handle_event()
466 LensGUI::LensGUI(LensMain *client)
467 : PluginClientWindow(client,
474 this->client = client;
482 void LensGUI::create_objects()
490 for(int i = 0; i < FOV_CHANNELS; i++)
494 case 0: add_tool(title = new BC_Title(x, y, _("R Field of View:"))); break;
495 case 1: add_tool(title = new BC_Title(x, y, _("G Field of View:"))); break;
496 case 2: add_tool(title = new BC_Title(x, y, _("B Field of View:"))); break;
497 case 3: add_tool(title = new BC_Title(x, y, _("A Field of View:"))); break;
500 y += title->get_h() + 5;
501 add_tool(fov_slider[i] = new LensSlider(client,
504 &client->config.fov[i],
509 x1 = x + fov_slider[i]->get_w() + 5;
510 add_tool(fov_text[i] = new LensText(client,
513 &client->config.fov[i],
516 fov_slider[i]->text = fov_text[i];
517 y += fov_text[i]->get_h() + 5;
520 add_tool(toggle = new LensToggle(client,
525 y += toggle->get_h() + 10;
528 add_tool(bar = new BC_Bar(x, y, get_w() - x * 2));
529 y += bar->get_h() + 5;
531 add_tool(title = new BC_Title(x, y, _("Aspect Ratio:")));
532 y += title->get_h() + 5;
533 add_tool(aspect_slider = new LensSlider(client,
536 &client->config.aspect,
541 x1 = x + aspect_slider->get_w() + 5;
542 add_tool(aspect_text = new LensText(client,
545 &client->config.aspect,
548 aspect_slider->text = aspect_text;
549 y += aspect_text->get_h() + 5;
552 add_tool(title = new BC_Title(x, y, _("Radius:")));
553 y += title->get_h() + 5;
554 add_tool(radius_slider = new LensSlider(client,
557 &client->config.radius,
562 x1 = x + radius_slider->get_w() + 5;
563 add_tool(radius_text = new LensText(client,
566 &client->config.radius,
569 radius_slider->text = radius_text;
570 y += radius_text->get_h() + 5;
573 add_tool(title = new BC_Title(x, y, _("Center X:")));
574 y += title->get_h() + 5;
575 add_tool(centerx_slider = new LensSlider(client,
578 &client->config.center_x,
583 x1 = x + centerx_slider->get_w() + 5;
584 add_tool(centerx_text = new LensText(client,
587 &client->config.center_x,
590 centerx_slider->text = centerx_text;
591 centerx_slider->set_precision(1.0);
592 y += centerx_text->get_h() + 5;
595 add_tool(title = new BC_Title(x, y, _("Center Y:")));
596 y += title->get_h() + 5;
597 add_tool(centery_slider = new LensSlider(client,
600 &client->config.center_y,
605 x1 = x + centery_slider->get_w() + 5;
606 add_tool(centery_text = new LensText(client,
609 &client->config.center_y,
612 centery_slider->text = centery_text;
613 centery_slider->set_precision(1.0);
614 y += centery_text->get_h() + 10;
616 add_tool(bar = new BC_Bar(x, y, get_w() - x * 2));
617 y += bar->get_h() + 5;
620 // add_tool(reverse = new LensToggle(client,
621 // &client->config.reverse,
625 // y += reverse->get_h() + 5;
627 add_tool(draw_guides = new LensToggle(client,
628 &client->config.draw_guides,
632 y += draw_guides->get_h() + 5;
635 add_tool(title = new BC_Title(x, y, _("Mode:")));
636 add_tool(mode = new LensMode(client,
638 x + title->get_w() + 5,
640 mode->create_objects();
641 y += mode->get_h() + 5;
644 // add_tool(title = new BC_Title(x, y, _("Preset:")));
645 // add_tool(presets = new LensPresets(client,
647 // x + title->get_w() + 5,
649 // get_w() - x - title->get_w() - 50));
650 // presets->create_objects();
651 // y += presets->get_h() + 5;
653 // add_tool(save_preset = new LensSavePreset(client,
657 // add_tool(preset_text = new LensPresetText(client,
659 // x + save_preset->get_w() + 5,
661 // get_w() - x - save_preset->get_w() - 10));
662 // y += preset_text->get_h() + 5;
663 // add_tool(delete_preset = new LensDeletePreset(client,
678 LensMain::LensMain(PluginServer *server)
679 : PluginVClient(server)
687 LensMain::~LensMain()
691 presets.remove_all_objects();
694 NEW_WINDOW_MACRO(LensMain, LensGUI)
695 LOAD_CONFIGURATION_MACRO(LensMain, LensConfig)
696 int LensMain::is_realtime() { return 1; }
697 const char* LensMain::plugin_title() { return _("Lens"); }
699 void LensMain::update_gui()
703 if(load_configuration())
705 ((LensGUI*)thread->window)->lock_window("LensMain::update_gui");
706 for(int i = 0; i < FOV_CHANNELS; i++)
708 ((LensGUI*)thread->window)->fov_slider[i]->update(config.fov[i]);
709 ((LensGUI*)thread->window)->fov_text[i]->update(config.fov[i]);
711 ((LensGUI*)thread->window)->aspect_slider->update(config.aspect);
712 ((LensGUI*)thread->window)->aspect_text->update(config.aspect);
713 ((LensGUI*)thread->window)->radius_slider->update(config.radius);
714 ((LensGUI*)thread->window)->radius_text->update(config.radius);
715 ((LensGUI*)thread->window)->centerx_slider->update(config.center_x);
716 ((LensGUI*)thread->window)->centerx_text->update(config.center_x);
717 ((LensGUI*)thread->window)->centery_slider->update(config.center_y);
718 ((LensGUI*)thread->window)->centery_text->update(config.center_y);
719 ((LensGUI*)thread->window)->mode->update(config.mode);
720 ((LensGUI*)thread->window)->draw_guides->update(config.draw_guides);
721 ((LensGUI*)thread->window)->unlock_window();
726 //void LensMain::save_presets()
728 // char path[BCTEXTLEN], string[BCTEXTLEN];
729 // sprintf(path, "%slenspresets.rc", BCASTDIR);
730 // BC_Hash *defaults = new BC_Hash(path);
733 // defaults->update("TOTAL_PRESETS", presets.total);
734 // for(int i = 0; i < presets.total; i++)
736 // LensPreset *preset = presets.values[i];
737 // sprintf(string, "TITLE_%d", i);
738 // defaults->update(string, preset->title);
740 // for(int j = 0; j < FOV_CHANNELS; j++)
742 // sprintf(string, "FOCAL_LENGTH_%d_%d", i, j);
743 // defaults->update(string, preset->fov[j]);
746 // sprintf(string, "ASPECT_%d", i);
747 // defaults->update(string, preset->aspect);
748 // sprintf(string, "RADIUS_%d", i);
749 // defaults->update(string, preset->radius);
750 // sprintf(string, "MODE_%d", i);
751 // defaults->update(string, preset->mode);
759 void LensMain::save_data(KeyFrame *keyframe)
762 char string[BCTEXTLEN];
766 // cause data to be stored directly in text
767 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
768 output.tag.set_title("LENS");
769 for(int i = 0; i < FOV_CHANNELS; i++)
771 sprintf(string, "FOCAL_LENGTH%d", i);
772 output.tag.set_property(string, config.fov[i]);
774 output.tag.set_property("ASPECT", config.aspect);
775 output.tag.set_property("RADIUS", config.radius);
776 output.tag.set_property("MODE", config.mode);
777 output.tag.set_property("CENTER_X", config.center_x);
778 output.tag.set_property("CENTER_Y", config.center_y);
779 output.tag.set_property("DRAW_GUIDES", config.draw_guides);
781 output.terminate_string();
786 void LensMain::read_data(KeyFrame *keyframe)
789 char string[BCTEXTLEN];
792 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
798 result = input.read_tag();
802 if(input.tag.title_is("LENS"))
804 for(int i = 0; i < FOV_CHANNELS; i++)
806 sprintf(string, "FOCAL_LENGTH%d", i);
807 config.fov[i] = input.tag.get_property(string, config.fov[i]);
809 config.aspect = input.tag.get_property("ASPECT", config.aspect);
810 config.radius = input.tag.get_property("RADIUS", config.radius);
811 config.mode = input.tag.get_property("MODE", config.mode);
812 config.center_x = input.tag.get_property("CENTER_X", config.center_x);
813 config.center_y = input.tag.get_property("CENTER_Y", config.center_y);
814 config.draw_guides = input.tag.get_property("DRAW_GUIDES", config.draw_guides);
822 int LensMain::process_buffer(VFrame *frame,
823 int64_t start_position,
827 load_configuration();
835 input = new_temp(frame->get_w(), frame->get_h(), frame->get_color_model());
852 if(!engine) engine = new LensEngine(this);
853 engine->process_packages();
854 if(config.draw_guides)
859 #define DRAW_GUIDES(components, type, max) \
861 type **rows = (type**)get_output()->get_rows(); \
862 if( (center_x >= 0 && center_x < w) || (center_y >= 0 && center_y < h) ) { \
863 type *hrow = rows[center_y] + components * (center_x - CENTER_W / 2); \
864 for(int i = center_x - CENTER_W / 2; i <= center_x + CENTER_W / 2; i++) \
866 if(i >= 0 && i < w) { \
867 hrow[0] = max - hrow[0]; \
868 hrow[1] = max - hrow[1]; \
869 hrow[2] = max - hrow[2]; \
870 hrow += components; \
874 for(int i = center_y - CENTER_W / 2; i <= center_y + CENTER_W / 2; i++) \
876 if(i >= 0 && i < h) { \
877 type *vrow = rows[i] + center_x * components; \
878 vrow[0] = max - vrow[0]; \
879 vrow[1] = max - vrow[1]; \
880 vrow[2] = max - vrow[2]; \
886 int w = get_output()->get_w();
887 int h = get_output()->get_h();
888 int center_x = (int)(config.center_x * w / 100);
889 int center_y = (int)(config.center_y * h / 100);
890 switch(get_output()->get_color_model())
893 DRAW_GUIDES(3, float, 1.0)
896 DRAW_GUIDES(4, float, 1.0)
899 DRAW_GUIDES(3, unsigned char, 0xff)
902 DRAW_GUIDES(4, unsigned char, 0xff)
905 DRAW_GUIDES(3, unsigned char, 0xff)
908 DRAW_GUIDES(4, unsigned char, 0xff)
919 int LensMain::handle_opengl()
922 static const char *shrink_frag =
923 "uniform sampler2D tex;\n"
924 "uniform vec2 texture_extents;\n"
925 "uniform vec2 image_extents;\n"
926 "uniform vec2 aspect;\n"
927 "uniform vec2 center_coord;\n"
928 "uniform vec4 border_color;\n"
930 "uniform vec4 max_z;\n"
933 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
934 " vec2 coord_diff = outcoord - center_coord;\n"
935 " if(coord_diff.x == 0.0 && coord_diff.y == 0.0)\n"
937 " gl_FragColor = texture2D(tex, outcoord);\n"
941 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
942 " coord_diff.y * coord_diff.y);\n"
943 " float a2 = atan(coord_diff.y, coord_diff.x);\n"
944 " vec4 a1 = asin(vec4(z, z, z, z) / r);\n"
945 " vec4 z_in = a1 * max_z * 2.0 / 3.14159;\n"
948 " in_x = z_in * cos(a2) * aspect.x + center_coord.x;\n"
949 " in_y = z_in * sin(a2) * aspect.y + center_coord.y;\n"
950 " 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"
951 " gl_FragColor.r = border_color.r;\n"
953 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
954 " 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"
955 " gl_FragColor.g = border_color.g;\n"
957 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
958 " 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"
959 " gl_FragColor.b = border_color.b;\n"
961 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
962 " 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"
963 " gl_FragColor.a = border_color.a;\n"
965 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
969 static const char *stretch_frag =
970 "uniform sampler2D tex;\n"
971 "uniform vec2 texture_extents;\n"
972 "uniform vec2 image_extents;\n"
973 "uniform vec2 aspect;\n"
974 "uniform vec2 center_coord;\n"
975 "uniform vec4 border_color;\n"
979 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
980 " vec2 coord_diff = outcoord - center_coord;\n"
981 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
982 " coord_diff.y * coord_diff.y);\n"
983 " vec4 a1 = (vec4(z, z, z, z) / (3.14159 * r / 2.0)) * (3.14159 / 2.0);\n"
984 " vec4 z_in = r * sin(a1);\n"
986 " if(coord_diff.x == 0.0)\n"
988 " if(coord_diff.y < 0.0)\n"
989 " a2 = 3.0 * 3.14159 / 2.0;\n"
991 " a2 = 3.14159 / 2.0;\n"
994 " a2 = atan(coord_diff.y, coord_diff.x);\n"
997 " in_x = z_in * cos(a2) * aspect.x + center_coord.x;\n"
998 " in_y = z_in * sin(a2) * aspect.y + center_coord.y;\n"
999 " if(in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y)\n"
1000 " gl_FragColor.r = border_color.r;\n"
1002 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
1003 " if(in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y)\n"
1004 " gl_FragColor.g = border_color.g;\n"
1006 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
1007 " if(in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y)\n"
1008 " gl_FragColor.b = border_color.b;\n"
1010 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
1011 " if(in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y)\n"
1012 " gl_FragColor.a = border_color.a;\n"
1014 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
1018 static const char *rectilinear_stretch_frag =
1019 "uniform sampler2D tex;\n"
1020 "uniform vec2 texture_extents;\n"
1021 "uniform vec2 image_extents;\n"
1022 "uniform vec2 aspect;\n"
1023 "uniform vec2 center_coord;\n"
1024 "uniform vec4 border_color;\n"
1026 "uniform float radius;\n"
1029 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
1030 " vec2 coord_diff = outcoord - center_coord;\n"
1031 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
1032 " coord_diff.y * coord_diff.y);\n"
1033 " vec4 radius1 = (vec4(z, z, z, z) / r) * 2.0 * radius;\n"
1034 " vec4 z_in = r * atan(radius1) / (3.14159 / 2.0);\n"
1037 " if(coord_diff.x == 0.0)\n"
1039 " if(coord_diff.y < 0.0)\n"
1040 " angle = 3.0 * 3.14159 / 2.0;\n"
1042 " angle = 3.14159 / 2.0;\n"
1045 " angle = atan(coord_diff.y, coord_diff.x);\n"
1049 " in_x = z_in * cos(angle) * aspect.x + center_coord.x;\n"
1050 " in_y = z_in * sin(angle) * aspect.y + center_coord.y;\n"
1051 " if(in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y)\n"
1052 " gl_FragColor.r = border_color.r;\n"
1054 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
1055 " if(in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y)\n"
1056 " gl_FragColor.g = border_color.g;\n"
1058 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
1059 " if(in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y)\n"
1060 " gl_FragColor.b = border_color.b;\n"
1062 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
1063 " if(in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y)\n"
1064 " gl_FragColor.a = border_color.a;\n"
1066 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
1069 static const char *rectilinear_shrink_frag =
1070 "uniform sampler2D tex;\n"
1071 "uniform vec2 texture_extents;\n"
1072 "uniform vec2 image_extents;\n"
1073 "uniform vec2 aspect;\n"
1074 "uniform vec2 center_coord;\n"
1075 "uniform vec4 border_color;\n"
1077 "uniform float radius;\n"
1080 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
1081 " vec2 coord_diff = outcoord - center_coord;\n"
1082 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
1083 " coord_diff.y * coord_diff.y);\n"
1084 " vec4 radius1 = (vec4(z, z, z, z) / r) * 2.0 * radius;\n"
1085 " vec4 z_in = r * atan(radius1) / (3.14159 / 2.0);\n"
1088 " if(coord_diff.x == 0.0)\n"
1090 " if(coord_diff.y < 0.0)\n"
1091 " angle = 3.0 * 3.14159 / 2.0;\n"
1093 " angle = 3.14159 / 2.0;\n"
1096 " angle = atan(coord_diff.y, coord_diff.x);\n"
1100 " in_x = z_in * cos(angle) * aspect.x + center_coord.x;\n"
1101 " in_y = z_in * sin(angle) * aspect.y + center_coord.y;\n"
1102 " if(in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y)\n"
1103 " gl_FragColor.r = border_color.r;\n"
1105 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
1106 " if(in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y)\n"
1107 " gl_FragColor.g = border_color.g;\n"
1109 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
1110 " if(in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y)\n"
1111 " gl_FragColor.b = border_color.b;\n"
1113 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
1114 " if(in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y)\n"
1115 " gl_FragColor.a = border_color.a;\n"
1117 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
1120 get_output()->to_texture();
1121 get_output()->enable_opengl();
1122 unsigned int frag_shader = 0;
1126 case LensConfig::SHRINK:
1127 frag_shader = VFrame::make_shader(0, shrink_frag, 0);
1129 case LensConfig::STRETCH:
1130 frag_shader = VFrame::make_shader(0, stretch_frag, 0);
1132 case LensConfig::RECTILINEAR_STRETCH:
1133 frag_shader = VFrame::make_shader(0, rectilinear_stretch_frag, 0);
1135 case LensConfig::RECTILINEAR_SHRINK:
1136 frag_shader = VFrame::make_shader(0, rectilinear_shrink_frag, 0);
1142 float border_color[] = { 0, 0, 0, 0 };
1143 if(BC_CModels::is_yuv(get_output()->get_color_model()))
1145 border_color[1] = 0.5;
1146 border_color[2] = 0.5;
1149 double x_factor = config.aspect;
1150 double y_factor = 1.0 / config.aspect;
1151 if(x_factor < 1) x_factor = 1;
1152 if(y_factor < 1) y_factor = 1;
1154 glUseProgram(frag_shader);
1155 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
1156 glUniform2f(glGetUniformLocation(frag_shader, "aspect"),
1159 glUniform2f(glGetUniformLocation(frag_shader, "center_coord"),
1160 (GLfloat)get_input()->get_w() * config.center_x / 100.0,
1161 (GLfloat)get_input()->get_h() * config.center_y / 100.0);
1162 glUniform2f(glGetUniformLocation(frag_shader, "texture_extents"),
1163 (GLfloat)get_input()->get_texture_w(),
1164 (GLfloat)get_input()->get_texture_h());
1165 glUniform2f(glGetUniformLocation(frag_shader, "image_extents"),
1166 (GLfloat)get_input()->get_w(),
1167 (GLfloat)get_input()->get_h());
1169 int width = get_output()->get_w();
1170 int height = get_output()->get_h();
1171 float *fov = config.fov;
1176 case LensConfig::SHRINK:
1177 dim = MAX(width, height) * config.radius;
1178 max_z = dim * sqrt(2.0) / 2;
1179 glUniform4fv(glGetUniformLocation(frag_shader, "border_color"),
1181 (GLfloat*)border_color);
1182 glUniform4f(glGetUniformLocation(frag_shader, "max_z"),
1187 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1188 (max_z / fov[0]) * 2 / M_PI,
1189 (max_z / fov[1]) * 2 / M_PI,
1190 (max_z / fov[2]) * 2 / M_PI,
1191 (max_z / fov[3]) * 2 / M_PI);
1194 case LensConfig::STRETCH:
1195 dim = MAX(width, height) * config.radius;
1196 max_z = dim * sqrt(2.0) / 2;
1197 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1198 max_z / M_PI / (fov[0] / 2.0),
1199 max_z / M_PI / (fov[1] / 2.0),
1200 max_z / M_PI / (fov[2] / 2.0),
1201 max_z / M_PI / (fov[3] / 2.0));
1204 case LensConfig::RECTILINEAR_STRETCH:
1205 max_z = sqrt(SQR(width) + SQR(height)) / 2;
1206 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1207 max_z / M_PI / (fov[0] / 2.0),
1208 max_z / M_PI / (fov[1] / 2.0),
1209 max_z / M_PI / (fov[2] / 2.0),
1210 max_z / M_PI / (fov[3] / 2.0));
1211 glUniform1f(glGetUniformLocation(frag_shader, "radius"),
1215 case LensConfig::RECTILINEAR_SHRINK:
1216 max_z = sqrt(SQR(width) + SQR(height)) / 2;
1217 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1218 max_z / M_PI / (fov[0] / 2.0),
1219 max_z / M_PI / (fov[1] / 2.0),
1220 max_z / M_PI / (fov[2] / 2.0),
1221 max_z / M_PI / (fov[3] / 2.0));
1222 glUniform1f(glGetUniformLocation(frag_shader, "radius"),
1228 get_output()->init_screen();
1229 get_output()->bind_texture(0);
1230 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);
1231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
1232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
1233 get_output()->draw_texture();
1237 if(config.draw_guides)
1239 int w = get_output()->get_w();
1240 int h = get_output()->get_h();
1241 int center_x = (int)(config.center_x * w / 100);
1242 int center_y = (int)(config.center_y * h / 100);
1244 glDisable(GL_TEXTURE_2D);
1245 glColor4f(0.0, 0.0, 0.0, 1.0);
1247 glVertex3f(center_x, -h + center_y - CENTER_H / 2, 0.0);
1248 glVertex3f(center_x, -h + center_y + CENTER_H / 2, 0.0);
1251 glVertex3f(center_x - CENTER_W / 2, -h + center_y, 0.0);
1252 glVertex3f(center_x + CENTER_W / 2, -h + center_y, 0.0);
1254 glColor4f(1.0, 1.0, 1.0, 1.0);
1256 glVertex3f(center_x - 1, -h + center_y - CENTER_H / 2 - 1, 0.0);
1257 glVertex3f(center_x - 1, -h + center_y + CENTER_H / 2 - 1, 0.0);
1260 glVertex3f(center_x - CENTER_W / 2 - 1, -h + center_y - 1, 0.0);
1261 glVertex3f(center_x + CENTER_W / 2 - 1, -h + center_y - 1, 0.0);
1264 get_output()->set_opengl_state(VFrame::SCREEN);
1278 LensPackage::LensPackage()
1285 LensUnit::LensUnit(LensEngine *engine, LensMain *plugin)
1286 : LoadClient(engine)
1288 this->plugin = plugin;
1291 LensUnit::~LensUnit()
1295 void LensUnit::process_shrink(LensPackage *pkg)
1298 float *fov = plugin->config.fov;
1299 float aspect = plugin->config.aspect;
1300 int row1 = pkg->row1;
1301 int row2 = pkg->row2;
1302 int width = plugin->get_input()->get_w();
1303 int height = plugin->get_input()->get_h();
1304 double x_factor = aspect;
1305 double y_factor = 1.0 / aspect;
1306 if(x_factor < 1) x_factor = 1;
1307 if(y_factor < 1) y_factor = 1;
1308 double dim = MAX(width, height) * plugin->config.radius;
1309 double max_z[FOV_CHANNELS];
1310 double center_x = width * plugin->config.center_x / 100.0;
1311 double center_y = height * plugin->config.center_y / 100.0;
1312 double r[FOV_CHANNELS];
1314 // max_z[0] = sqrt(SQR(width) + SQR(height)) / 2 / fov[0];
1315 // max_z[1] = sqrt(SQR(width) + SQR(height)) / 2 / fov[1];
1316 // max_z[2] = sqrt(SQR(width) + SQR(height)) / 2 / fov[2];
1317 // max_z[3] = sqrt(SQR(width) + SQR(height)) / 2 / fov[3];
1318 max_z[0] = dim * sqrt(2.0) / 2 / fov[0];
1319 max_z[1] = dim * sqrt(2.0) / 2 / fov[1];
1320 max_z[2] = dim * sqrt(2.0) / 2 / fov[2];
1321 max_z[3] = dim * sqrt(2.0) / 2 / fov[3];
1322 r[0] = max_z[0] * 2 / M_PI;
1323 r[1] = max_z[1] * 2 / M_PI;
1324 r[2] = max_z[2] * 2 / M_PI;
1325 r[3] = max_z[3] * 2 / M_PI;
1327 #define DO_LENS_SHRINK(type, components, chroma) \
1329 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1330 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1331 type black[4] = { 0, chroma, chroma, 0 }; \
1333 for(int y = row1; y < row2; y++) \
1335 type *out_row = out_rows[y]; \
1336 type *in_row = in_rows[y]; \
1337 double y_diff = y - center_y; \
1339 for(int x = 0; x < width; x++) \
1341 double x_diff = x - center_x; \
1342 if(!x_diff && !y_diff) \
1344 type *in_pixel = in_row + x * components; \
1345 for(int c = 0; c < components; c++) \
1347 *out_row++ = *in_pixel++; \
1352 double z = sqrt(x_diff * x_diff + y_diff * y_diff); \
1353 double a2 = atan(y_diff / x_diff); \
1354 if(x_diff < 0.0) a2 += M_PI; \
1356 for(int i = 0; i < components; i++) \
1360 *out_row++ = black[i]; \
1364 double a1 = asin(z / r[i]); \
1365 double z_in = a1 * max_z[i] * 2 / M_PI; \
1367 float x_in = z_in * cos(a2) * x_factor + center_x; \
1368 float y_in = z_in * sin(a2) * y_factor + center_y; \
1370 if(x_in < 0.0 || x_in >= width - 1 || \
1371 y_in < 0.0 || y_in >= height - 1) \
1373 *out_row++ = black[i]; \
1377 float y1_fraction = y_in - floor(y_in); \
1378 float y2_fraction = 1.0 - y1_fraction; \
1379 float x1_fraction = x_in - floor(x_in); \
1380 float x2_fraction = 1.0 - x1_fraction; \
1381 type *in_pixel1 = in_rows[(int)y_in] + (int)x_in * components; \
1382 type *in_pixel2 = in_rows[(int)y_in + 1] + (int)x_in * components; \
1383 *out_row++ = (type)(in_pixel1[i] * x2_fraction * y2_fraction + \
1384 in_pixel2[i] * x2_fraction * y1_fraction + \
1385 in_pixel1[i + components] * x1_fraction * y2_fraction + \
1386 in_pixel2[i + components] * x1_fraction * y1_fraction); \
1393 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1394 type *in_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1395 for(int c = 0; c < components; c++) \
1397 *out_pixel++ = *in_pixel++; \
1402 switch(plugin->get_input()->get_color_model())
1405 DO_LENS_SHRINK(unsigned char, 3, 0x0);
1408 DO_LENS_SHRINK(unsigned char, 4, 0x0);
1411 DO_LENS_SHRINK(float, 3, 0.0);
1414 DO_LENS_SHRINK(float, 4, 0.0);
1417 DO_LENS_SHRINK(unsigned char, 3, 0x80);
1420 DO_LENS_SHRINK(unsigned char, 4, 0x80);
1425 void LensUnit::process_stretch(LensPackage *pkg)
1427 float *fov = plugin->config.fov;
1428 float aspect = plugin->config.aspect;
1429 int row1 = pkg->row1;
1430 int row2 = pkg->row2;
1431 double x_factor = aspect;
1432 double y_factor = 1.0 / aspect;
1433 if(x_factor < 1) x_factor = 1;
1434 if(y_factor < 1) y_factor = 1;
1435 int width = plugin->get_input()->get_w();
1436 int height = plugin->get_input()->get_h();
1437 double dim = MAX(width, height) * plugin->config.radius;
1438 double max_z = dim * sqrt(2.0) / 2;
1439 double center_x = width * plugin->config.center_x / 100.0;
1440 double center_y = height * plugin->config.center_y / 100.0;
1441 double r[FOV_CHANNELS];
1443 r[0] = max_z / M_PI / (fov[0] / 2.0);
1444 r[1] = max_z / M_PI / (fov[1] / 2.0);
1445 r[2] = max_z / M_PI / (fov[2] / 2.0);
1446 r[3] = max_z / M_PI / (fov[3] / 2.0);
1448 #define DO_LENS_STRETCH(type, components, chroma) \
1450 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1451 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1452 type black[4] = { 0, chroma, chroma, 0 }; \
1454 for(int y = row1; y < row2; y++) \
1456 type *out_row = out_rows[y]; \
1457 double y_diff = y - center_y; \
1459 for(int x = 0; x < width; x++) \
1461 double x_diff = (x - center_x); \
1462 double z = sqrt(x_diff * x_diff + \
1468 a2 = 3 * M_PI / 2; \
1474 a2 = atan(y_diff / x_diff); \
1476 if(x_diff < 0.0) a2 += M_PI; \
1478 for(int i = 0; i < components; i++) \
1480 double a1 = (z / (M_PI * r[i] / 2)) * (M_PI / 2); \
1481 double z_in = r[i] * sin(a1); \
1483 double x_in = z_in * cos(a2) * x_factor + center_x; \
1484 double y_in = z_in * sin(a2) * y_factor + center_y; \
1486 if(x_in < 0.0 || x_in >= width - 1 || \
1487 y_in < 0.0 || y_in >= height - 1) \
1489 *out_row++ = black[i]; \
1493 float y1_fraction = y_in - floor(y_in); \
1494 float y2_fraction = 1.0 - y1_fraction; \
1495 float x1_fraction = x_in - floor(x_in); \
1496 float x2_fraction = 1.0 - x1_fraction; \
1497 type *in_pixel1 = in_rows[(int)y_in] + (int)x_in * components; \
1498 type *in_pixel2 = in_rows[(int)y_in + 1] + (int)x_in * components; \
1499 *out_row++ = (type)(in_pixel1[i] * x2_fraction * y2_fraction + \
1500 in_pixel2[i] * x2_fraction * y1_fraction + \
1501 in_pixel1[i + components] * x1_fraction * y2_fraction + \
1502 in_pixel2[i + components] * x1_fraction * y1_fraction); \
1508 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1509 type *in_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1510 for(int c = 0; c < components; c++) \
1512 *out_pixel++ = *in_pixel++; \
1517 switch(plugin->get_input()->get_color_model())
1520 DO_LENS_STRETCH(unsigned char, 3, 0x0);
1523 DO_LENS_STRETCH(unsigned char, 4, 0x0);
1526 DO_LENS_STRETCH(float, 3, 0.0);
1529 DO_LENS_STRETCH(float, 4, 0.0);
1532 DO_LENS_STRETCH(unsigned char, 3, 0x80);
1535 DO_LENS_STRETCH(unsigned char, 4, 0x80);
1540 void LensUnit::process_rectilinear_stretch(LensPackage *pkg)
1542 float *fov = plugin->config.fov;
1543 float aspect = plugin->config.aspect;
1544 int row1 = pkg->row1;
1545 int row2 = pkg->row2;
1546 double x_factor = aspect;
1547 double y_factor = 1.0 / aspect;
1548 if(x_factor < 1) x_factor = 1;
1549 if(y_factor < 1) y_factor = 1;
1550 int width = plugin->get_input()->get_w();
1551 int height = plugin->get_input()->get_h();
1552 // double dim = MAX(width, height) * plugin->config.radius;
1553 // double max_z = dim * sqrt(2.0) / 2;
1554 double max_z = sqrt(SQR(width) + SQR(height)) / 2;
1555 double center_x = width * plugin->config.center_x / 100.0;
1556 double center_y = height * plugin->config.center_y / 100.0;
1557 double r[FOV_CHANNELS];
1559 r[0] = max_z / M_PI / (fov[0] / 2.0);
1560 r[1] = max_z / M_PI / (fov[1] / 2.0);
1561 r[2] = max_z / M_PI / (fov[2] / 2.0);
1562 r[3] = max_z / M_PI / (fov[3] / 2.0);
1564 #define DO_LENS_RECTILINEAR_STRETCH(type, components, chroma) \
1566 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1567 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1568 type black[4] = { 0, chroma, chroma, 0 }; \
1570 for(int y = row1; y < row2; y++) \
1572 type *out_row = out_rows[y]; \
1573 double y_diff = y - center_y; \
1575 for(int x = 0; x < width; x++) \
1577 double x_diff = (x - center_x); \
1578 /* Compute magnitude */ \
1579 double z = sqrt(x_diff * x_diff + \
1581 /* Compute angle */ \
1586 angle = 3 * M_PI / 2; \
1592 angle = atan(y_diff / x_diff); \
1594 if(x_diff < 0.0) angle += M_PI; \
1596 for(int i = 0; i < components; i++) \
1598 /* Compute new radius */ \
1599 double radius1 = (z / r[i]) * 2 * plugin->config.radius; \
1600 double z_in = r[i] * atan(radius1) / (M_PI / 2); \
1602 double x_in = z_in * cos(angle) * x_factor + center_x; \
1603 double y_in = z_in * sin(angle) * y_factor + center_y; \
1605 if(x_in < 0.0 || x_in >= width - 1 || \
1606 y_in < 0.0 || y_in >= height - 1) \
1608 *out_row++ = black[i]; \
1612 float y1_fraction = y_in - floor(y_in); \
1613 float y2_fraction = 1.0 - y1_fraction; \
1614 float x1_fraction = x_in - floor(x_in); \
1615 float x2_fraction = 1.0 - x1_fraction; \
1616 type *in_pixel1 = in_rows[(int)y_in] + (int)x_in * components; \
1617 type *in_pixel2 = in_rows[(int)y_in + 1] + (int)x_in * components; \
1618 *out_row++ = (type)(in_pixel1[i] * x2_fraction * y2_fraction + \
1619 in_pixel2[i] * x2_fraction * y1_fraction + \
1620 in_pixel1[i + components] * x1_fraction * y2_fraction + \
1621 in_pixel2[i + components] * x1_fraction * y1_fraction); \
1627 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1628 type *in_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1629 for(int c = 0; c < components; c++) \
1631 *out_pixel++ = *in_pixel++; \
1636 switch(plugin->get_input()->get_color_model())
1639 DO_LENS_RECTILINEAR_STRETCH(unsigned char, 3, 0x0);
1642 DO_LENS_RECTILINEAR_STRETCH(unsigned char, 4, 0x0);
1645 DO_LENS_RECTILINEAR_STRETCH(float, 3, 0.0);
1648 DO_LENS_RECTILINEAR_STRETCH(float, 4, 0.0);
1651 DO_LENS_RECTILINEAR_STRETCH(unsigned char, 3, 0x80);
1654 DO_LENS_RECTILINEAR_STRETCH(unsigned char, 4, 0x80);
1659 void LensUnit::process_rectilinear_shrink(LensPackage *pkg)
1661 float *fov = plugin->config.fov;
1662 float aspect = plugin->config.aspect;
1663 int row1 = pkg->row1;
1664 int row2 = pkg->row2;
1665 double x_factor = aspect;
1666 double y_factor = 1.0 / aspect;
1667 if(x_factor < 1) x_factor = 1;
1668 if(y_factor < 1) y_factor = 1;
1669 int width = plugin->get_input()->get_w();
1670 int height = plugin->get_input()->get_h();
1671 double max_z = MAX(width, height) / 2 * plugin->config.radius;
1672 double center_x = width * plugin->config.center_x / 100.0;
1673 double center_y = height * plugin->config.center_y / 100.0;
1674 double r[FOV_CHANNELS];
1676 r[0] = max_z / fov[0];
1677 r[1] = max_z / fov[1];
1678 r[2] = max_z / fov[2];
1679 r[3] = max_z / fov[3];
1681 #define DO_LENS_RECTILINEAR_SHRINK(type, components, chroma) \
1683 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1684 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1685 type black[4] = { 0, chroma, chroma, 0 }; \
1687 for(int y = row1; y < row2; y++) \
1689 type *out_row = out_rows[y]; \
1690 double y_diff = y - center_y; \
1692 for(int x = 0; x < width; x++) \
1694 double x_diff = (x - center_x); \
1695 /* Compute magnitude */ \
1696 double z = sqrt(x_diff * x_diff + \
1698 /* Compute angle */ \
1703 angle = 3 * M_PI / 2; \
1709 angle = atan(y_diff / x_diff); \
1711 if(x_diff < 0.0) angle += M_PI; \
1713 for(int i = 0; i < components; i++) \
1715 /* Compute new radius */ \
1716 double radius1 = z / r[i]; \
1717 double z_in = r[i] * tan(radius1) / (M_PI / 2); \
1719 double x_in = z_in * cos(angle) * x_factor + center_x; \
1720 double y_in = z_in * sin(angle) * y_factor + center_y; \
1722 if(x_in < 0.0 || x_in >= width - 1 || \
1723 y_in < 0.0 || y_in >= height - 1) \
1725 *out_row++ = black[i]; \
1729 float y1_fraction = y_in - floor(y_in); \
1730 float y2_fraction = 1.0 - y1_fraction; \
1731 float x1_fraction = x_in - floor(x_in); \
1732 float x2_fraction = 1.0 - x1_fraction; \
1733 type *in_pixel1 = in_rows[(int)y_in] + (int)x_in * components; \
1734 type *in_pixel2 = in_rows[(int)y_in + 1] + (int)x_in * components; \
1735 *out_row++ = (type)(in_pixel1[i] * x2_fraction * y2_fraction + \
1736 in_pixel2[i] * x2_fraction * y1_fraction + \
1737 in_pixel1[i + components] * x1_fraction * y2_fraction + \
1738 in_pixel2[i + components] * x1_fraction * y1_fraction); \
1744 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1745 type *in_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1746 for(int c = 0; c < components; c++) \
1748 *out_pixel++ = *in_pixel++; \
1753 switch(plugin->get_input()->get_color_model())
1756 DO_LENS_RECTILINEAR_SHRINK(unsigned char, 3, 0x0);
1759 DO_LENS_RECTILINEAR_SHRINK(unsigned char, 4, 0x0);
1762 DO_LENS_RECTILINEAR_SHRINK(float, 3, 0.0);
1765 DO_LENS_RECTILINEAR_SHRINK(float, 4, 0.0);
1768 DO_LENS_RECTILINEAR_SHRINK(unsigned char, 3, 0x80);
1771 DO_LENS_RECTILINEAR_SHRINK(unsigned char, 4, 0x80);
1776 void LensUnit::process_package(LoadPackage *package)
1778 LensPackage *pkg = (LensPackage*)package;
1780 switch(plugin->config.mode)
1782 case LensConfig::SHRINK:
1783 process_shrink(pkg);
1785 case LensConfig::STRETCH:
1786 process_stretch(pkg);
1788 case LensConfig::RECTILINEAR_STRETCH:
1789 process_rectilinear_stretch(pkg);
1791 case LensConfig::RECTILINEAR_SHRINK :
1792 process_rectilinear_shrink(pkg);
1801 LensEngine::LensEngine(LensMain *plugin)
1802 : LoadServer(plugin->PluginClient::smp + 1, plugin->PluginClient::smp + 1)
1803 // : LoadServer(1, 1)
1805 this->plugin = plugin;
1808 LensEngine::~LensEngine()
1812 void LensEngine::init_packages()
1814 for(int i = 0; i < LoadServer::get_total_packages(); i++)
1816 LensPackage *package = (LensPackage*)LoadServer::get_package(i);
1817 package->row1 = plugin->get_input()->get_h() * i / LoadServer::get_total_packages();
1818 package->row2 = plugin->get_input()->get_h() * (i + 1) / LoadServer::get_total_packages();
1822 LoadClient* LensEngine::new_client()
1824 return new LensUnit(this, plugin);
1827 LoadPackage* LensEngine::new_package()
1829 return new LensPackage;