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++)
364 // // Create current items
365 // plugin->load_presets();
366 // for(int i = 0; i < plugin->presets.total; i++)
368 // add_item(new BC_MenuItem(plugin->presets.values[i]->title));
372 // if(plugin->current_preset >= 0 &&
373 // plugin->current_preset < plugin->presets.total)
375 // set_text(plugin->presets.values[plugin->current_preset]->title);
383 // int LensPresets::from_text(LensMain *plugin, char *text)
387 // char* LensPresets::to_text(LensMain *plugin, int preset)
391 // void LensPresets::update(int preset)
400 // LensSavePreset::LensSavePreset(LensMain *plugin,
404 // : BC_GenericButton(x, y, "Save Preset")
406 // this->plugin = plugin;
410 // int LensSavePreset::handle_event()
420 // LensDeletePreset::LensDeletePreset(LensMain *plugin,
424 // : BC_GenericButton(x, y, "Delete Preset")
428 // int LensDeletePreset::handle_event()
438 // LensPresetText::LensPresetText(LensMain *plugin,
443 // : BC_TextBox(x, y, w, 1, "")
445 // this->plugin = plugin;
449 // int LensPresetText::handle_event()
465 LensGUI::LensGUI(LensMain *client)
466 : PluginClientWindow(client,
473 this->client = client;
481 void LensGUI::create_objects()
489 for(int i = 0; i < FOV_CHANNELS; i++)
493 case 0: add_tool(title = new BC_Title(x, y, _("R Field of View:"))); break;
494 case 1: add_tool(title = new BC_Title(x, y, _("G Field of View:"))); break;
495 case 2: add_tool(title = new BC_Title(x, y, _("B Field of View:"))); break;
496 case 3: add_tool(title = new BC_Title(x, y, _("A Field of View:"))); break;
499 y += title->get_h() + 5;
500 add_tool(fov_slider[i] = new LensSlider(client,
503 &client->config.fov[i],
508 x1 = x + fov_slider[i]->get_w() + 5;
509 add_tool(fov_text[i] = new LensText(client,
512 &client->config.fov[i],
515 fov_slider[i]->text = fov_text[i];
516 y += fov_text[i]->get_h() + 5;
519 add_tool(toggle = new LensToggle(client,
524 y += toggle->get_h() + 10;
527 add_tool(bar = new BC_Bar(x, y, get_w() - x * 2));
528 y += bar->get_h() + 5;
530 add_tool(title = new BC_Title(x, y, _("Aspect Ratio:")));
531 y += title->get_h() + 5;
532 add_tool(aspect_slider = new LensSlider(client,
535 &client->config.aspect,
540 x1 = x + aspect_slider->get_w() + 5;
541 add_tool(aspect_text = new LensText(client,
544 &client->config.aspect,
547 aspect_slider->text = aspect_text;
548 y += aspect_text->get_h() + 5;
551 add_tool(title = new BC_Title(x, y, _("Radius:")));
552 y += title->get_h() + 5;
553 add_tool(radius_slider = new LensSlider(client,
556 &client->config.radius,
561 x1 = x + radius_slider->get_w() + 5;
562 add_tool(radius_text = new LensText(client,
565 &client->config.radius,
568 radius_slider->text = radius_text;
569 y += radius_text->get_h() + 5;
572 add_tool(title = new BC_Title(x, y, _("Center X:")));
573 y += title->get_h() + 5;
574 add_tool(centerx_slider = new LensSlider(client,
577 &client->config.center_x,
582 x1 = x + centerx_slider->get_w() + 5;
583 add_tool(centerx_text = new LensText(client,
586 &client->config.center_x,
589 centerx_slider->text = centerx_text;
590 centerx_slider->set_precision(1.0);
591 y += centerx_text->get_h() + 5;
594 add_tool(title = new BC_Title(x, y, _("Center Y:")));
595 y += title->get_h() + 5;
596 add_tool(centery_slider = new LensSlider(client,
599 &client->config.center_y,
604 x1 = x + centery_slider->get_w() + 5;
605 add_tool(centery_text = new LensText(client,
608 &client->config.center_y,
611 centery_slider->text = centery_text;
612 centery_slider->set_precision(1.0);
613 y += centery_text->get_h() + 10;
615 add_tool(bar = new BC_Bar(x, y, get_w() - x * 2));
616 y += bar->get_h() + 5;
619 // add_tool(reverse = new LensToggle(client,
620 // &client->config.reverse,
624 // y += reverse->get_h() + 5;
626 add_tool(draw_guides = new LensToggle(client,
627 &client->config.draw_guides,
631 y += draw_guides->get_h() + 5;
634 add_tool(title = new BC_Title(x, y, _("Mode:")));
635 add_tool(mode = new LensMode(client,
637 x + title->get_w() + 5,
639 mode->create_objects();
640 y += mode->get_h() + 5;
643 // add_tool(title = new BC_Title(x, y, _("Preset:")));
644 // add_tool(presets = new LensPresets(client,
646 // x + title->get_w() + 5,
648 // get_w() - x - title->get_w() - 50));
649 // presets->create_objects();
650 // y += presets->get_h() + 5;
652 // add_tool(save_preset = new LensSavePreset(client,
656 // add_tool(preset_text = new LensPresetText(client,
658 // x + save_preset->get_w() + 5,
660 // get_w() - x - save_preset->get_w() - 10));
661 // y += preset_text->get_h() + 5;
662 // add_tool(delete_preset = new LensDeletePreset(client,
677 LensMain::LensMain(PluginServer *server)
678 : PluginVClient(server)
686 LensMain::~LensMain()
690 presets.remove_all_objects();
693 NEW_WINDOW_MACRO(LensMain, LensGUI)
694 LOAD_CONFIGURATION_MACRO(LensMain, LensConfig)
695 int LensMain::is_realtime() { return 1; }
696 const char* LensMain::plugin_title() { return _("Lens"); }
698 void LensMain::update_gui()
702 if(load_configuration())
704 ((LensGUI*)thread->window)->lock_window("LensMain::update_gui");
705 for(int i = 0; i < FOV_CHANNELS; i++)
707 ((LensGUI*)thread->window)->fov_slider[i]->update(config.fov[i]);
708 ((LensGUI*)thread->window)->fov_text[i]->update(config.fov[i]);
710 ((LensGUI*)thread->window)->aspect_slider->update(config.aspect);
711 ((LensGUI*)thread->window)->aspect_text->update(config.aspect);
712 ((LensGUI*)thread->window)->radius_slider->update(config.radius);
713 ((LensGUI*)thread->window)->radius_text->update(config.radius);
714 ((LensGUI*)thread->window)->centerx_slider->update(config.center_x);
715 ((LensGUI*)thread->window)->centerx_text->update(config.center_x);
716 ((LensGUI*)thread->window)->centery_slider->update(config.center_y);
717 ((LensGUI*)thread->window)->centery_text->update(config.center_y);
718 ((LensGUI*)thread->window)->mode->update(config.mode);
719 ((LensGUI*)thread->window)->draw_guides->update(config.draw_guides);
720 ((LensGUI*)thread->window)->unlock_window();
725 //void LensMain::save_presets()
727 // char path[BCTEXTLEN], string[BCTEXTLEN];
728 // sprintf(path, "%slenspresets.rc", BCASTDIR);
729 // BC_Hash *defaults = new BC_Hash(path);
732 // defaults->update("TOTAL_PRESETS", presets.total);
733 // for(int i = 0; i < presets.total; i++)
735 // LensPreset *preset = presets.values[i];
736 // sprintf(string, "TITLE_%d", i);
737 // defaults->update(string, preset->title);
739 // for(int j = 0; j < FOV_CHANNELS; j++)
741 // sprintf(string, "FOCAL_LENGTH_%d_%d", i, j);
742 // defaults->update(string, preset->fov[j]);
745 // sprintf(string, "ASPECT_%d", i);
746 // defaults->update(string, preset->aspect);
747 // sprintf(string, "RADIUS_%d", i);
748 // defaults->update(string, preset->radius);
749 // sprintf(string, "MODE_%d", i);
750 // defaults->update(string, preset->mode);
758 void LensMain::save_data(KeyFrame *keyframe)
761 char string[BCTEXTLEN];
765 // cause data to be stored directly in text
766 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
767 output.tag.set_title("LENS");
768 for(int i = 0; i < FOV_CHANNELS; i++)
770 sprintf(string, "FOCAL_LENGTH%d", i);
771 output.tag.set_property(string, config.fov[i]);
773 output.tag.set_property("ASPECT", config.aspect);
774 output.tag.set_property("RADIUS", config.radius);
775 output.tag.set_property("MODE", config.mode);
776 output.tag.set_property("CENTER_X", config.center_x);
777 output.tag.set_property("CENTER_Y", config.center_y);
778 output.tag.set_property("DRAW_GUIDES", config.draw_guides);
780 output.tag.set_title("/LENS");
782 output.append_newline();
783 output.terminate_string();
788 void LensMain::read_data(KeyFrame *keyframe)
791 char string[BCTEXTLEN];
794 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
800 result = input.read_tag();
804 if(input.tag.title_is("LENS"))
806 for(int i = 0; i < FOV_CHANNELS; i++)
808 sprintf(string, "FOCAL_LENGTH%d", i);
809 config.fov[i] = input.tag.get_property(string, config.fov[i]);
811 config.aspect = input.tag.get_property("ASPECT", config.aspect);
812 config.radius = input.tag.get_property("RADIUS", config.radius);
813 config.mode = input.tag.get_property("MODE", config.mode);
814 config.center_x = input.tag.get_property("CENTER_X", config.center_x);
815 config.center_y = input.tag.get_property("CENTER_Y", config.center_y);
816 config.draw_guides = input.tag.get_property("DRAW_GUIDES", config.draw_guides);
824 int LensMain::process_buffer(VFrame *frame,
825 int64_t start_position,
829 load_configuration();
837 input = new_temp(frame->get_w(), frame->get_h(), frame->get_color_model());
854 if(!engine) engine = new LensEngine(this);
855 engine->process_packages();
856 if(config.draw_guides)
861 #define DRAW_GUIDES(components, type, max) \
863 type **rows = (type**)get_output()->get_rows(); \
864 if( (center_x >= 0 && center_x < w) || (center_y >= 0 && center_y < h) ) { \
865 type *hrow = rows[center_y] + components * (center_x - CENTER_W / 2); \
866 for(int i = center_x - CENTER_W / 2; i <= center_x + CENTER_W / 2; i++) \
868 if(i >= 0 && i < w) { \
869 hrow[0] = max - hrow[0]; \
870 hrow[1] = max - hrow[1]; \
871 hrow[2] = max - hrow[2]; \
872 hrow += components; \
876 for(int i = center_y - CENTER_W / 2; i <= center_y + CENTER_W / 2; i++) \
878 if(i >= 0 && i < h) { \
879 type *vrow = rows[i] + center_x * components; \
880 vrow[0] = max - vrow[0]; \
881 vrow[1] = max - vrow[1]; \
882 vrow[2] = max - vrow[2]; \
888 int w = get_output()->get_w();
889 int h = get_output()->get_h();
890 int center_x = (int)(config.center_x * w / 100);
891 int center_y = (int)(config.center_y * h / 100);
892 switch(get_output()->get_color_model())
895 DRAW_GUIDES(3, float, 1.0)
898 DRAW_GUIDES(4, float, 1.0)
901 DRAW_GUIDES(3, unsigned char, 0xff)
904 DRAW_GUIDES(4, unsigned char, 0xff)
907 DRAW_GUIDES(3, unsigned char, 0xff)
910 DRAW_GUIDES(4, unsigned char, 0xff)
921 int LensMain::handle_opengl()
924 static const char *shrink_frag =
925 "uniform sampler2D tex;\n"
926 "uniform vec2 texture_extents;\n"
927 "uniform vec2 image_extents;\n"
928 "uniform vec2 aspect;\n"
929 "uniform vec2 center_coord;\n"
930 "uniform vec4 border_color;\n"
932 "uniform vec4 max_z;\n"
935 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
936 " vec2 coord_diff = outcoord - center_coord;\n"
937 " if(coord_diff.x == 0.0 && coord_diff.y == 0.0)\n"
939 " gl_FragColor = texture2D(tex, outcoord);\n"
943 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
944 " coord_diff.y * coord_diff.y);\n"
945 " float a2 = atan(coord_diff.y, coord_diff.x);\n"
946 " vec4 a1 = asin(vec4(z, z, z, z) / r);\n"
947 " vec4 z_in = a1 * max_z * 2.0 / 3.14159;\n"
950 " in_x = z_in * cos(a2) * aspect.x + center_coord.x;\n"
951 " in_y = z_in * sin(a2) * aspect.y + center_coord.y;\n"
952 " 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"
953 " gl_FragColor.r = border_color.r;\n"
955 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
956 " 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"
957 " gl_FragColor.g = border_color.g;\n"
959 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
960 " 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"
961 " gl_FragColor.b = border_color.b;\n"
963 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
964 " 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"
965 " gl_FragColor.a = border_color.a;\n"
967 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
971 static const char *stretch_frag =
972 "uniform sampler2D tex;\n"
973 "uniform vec2 texture_extents;\n"
974 "uniform vec2 image_extents;\n"
975 "uniform vec2 aspect;\n"
976 "uniform vec2 center_coord;\n"
977 "uniform vec4 border_color;\n"
981 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
982 " vec2 coord_diff = outcoord - center_coord;\n"
983 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
984 " coord_diff.y * coord_diff.y);\n"
985 " vec4 a1 = (vec4(z, z, z, z) / (3.14159 * r / 2.0)) * (3.14159 / 2.0);\n"
986 " vec4 z_in = r * sin(a1);\n"
988 " if(coord_diff.x == 0.0)\n"
990 " if(coord_diff.y < 0.0)\n"
991 " a2 = 3.0 * 3.14159 / 2.0;\n"
993 " a2 = 3.14159 / 2.0;\n"
996 " a2 = atan(coord_diff.y, coord_diff.x);\n"
999 " in_x = z_in * cos(a2) * aspect.x + center_coord.x;\n"
1000 " in_y = z_in * sin(a2) * aspect.y + center_coord.y;\n"
1001 " if(in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y)\n"
1002 " gl_FragColor.r = border_color.r;\n"
1004 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
1005 " if(in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y)\n"
1006 " gl_FragColor.g = border_color.g;\n"
1008 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
1009 " if(in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y)\n"
1010 " gl_FragColor.b = border_color.b;\n"
1012 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
1013 " if(in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y)\n"
1014 " gl_FragColor.a = border_color.a;\n"
1016 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
1020 static const char *rectilinear_stretch_frag =
1021 "uniform sampler2D tex;\n"
1022 "uniform vec2 texture_extents;\n"
1023 "uniform vec2 image_extents;\n"
1024 "uniform vec2 aspect;\n"
1025 "uniform vec2 center_coord;\n"
1026 "uniform vec4 border_color;\n"
1028 "uniform float radius;\n"
1031 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
1032 " vec2 coord_diff = outcoord - center_coord;\n"
1033 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
1034 " coord_diff.y * coord_diff.y);\n"
1035 " vec4 radius1 = (vec4(z, z, z, z) / r) * 2.0 * radius;\n"
1036 " vec4 z_in = r * atan(radius1) / (3.14159 / 2.0);\n"
1039 " if(coord_diff.x == 0.0)\n"
1041 " if(coord_diff.y < 0.0)\n"
1042 " angle = 3.0 * 3.14159 / 2.0;\n"
1044 " angle = 3.14159 / 2.0;\n"
1047 " angle = atan(coord_diff.y, coord_diff.x);\n"
1051 " in_x = z_in * cos(angle) * aspect.x + center_coord.x;\n"
1052 " in_y = z_in * sin(angle) * aspect.y + center_coord.y;\n"
1053 " if(in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y)\n"
1054 " gl_FragColor.r = border_color.r;\n"
1056 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
1057 " if(in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y)\n"
1058 " gl_FragColor.g = border_color.g;\n"
1060 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
1061 " if(in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y)\n"
1062 " gl_FragColor.b = border_color.b;\n"
1064 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
1065 " if(in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y)\n"
1066 " gl_FragColor.a = border_color.a;\n"
1068 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
1071 static const char *rectilinear_shrink_frag =
1072 "uniform sampler2D tex;\n"
1073 "uniform vec2 texture_extents;\n"
1074 "uniform vec2 image_extents;\n"
1075 "uniform vec2 aspect;\n"
1076 "uniform vec2 center_coord;\n"
1077 "uniform vec4 border_color;\n"
1079 "uniform float radius;\n"
1082 " vec2 outcoord = gl_TexCoord[0].st * texture_extents;\n"
1083 " vec2 coord_diff = outcoord - center_coord;\n"
1084 " float z = sqrt(coord_diff.x * coord_diff.x +\n"
1085 " coord_diff.y * coord_diff.y);\n"
1086 " vec4 radius1 = (vec4(z, z, z, z) / r) * 2.0 * radius;\n"
1087 " vec4 z_in = r * atan(radius1) / (3.14159 / 2.0);\n"
1090 " if(coord_diff.x == 0.0)\n"
1092 " if(coord_diff.y < 0.0)\n"
1093 " angle = 3.0 * 3.14159 / 2.0;\n"
1095 " angle = 3.14159 / 2.0;\n"
1098 " angle = atan(coord_diff.y, coord_diff.x);\n"
1102 " in_x = z_in * cos(angle) * aspect.x + center_coord.x;\n"
1103 " in_y = z_in * sin(angle) * aspect.y + center_coord.y;\n"
1104 " if(in_x.r < 0.0 || in_x.r >= image_extents.x || in_y.r < 0.0 || in_y.r >= image_extents.y)\n"
1105 " gl_FragColor.r = border_color.r;\n"
1107 " gl_FragColor.r = texture2D(tex, vec2(in_x.r, in_y.r) / texture_extents).r;\n"
1108 " if(in_x.g < 0.0 || in_x.g >= image_extents.x || in_y.g < 0.0 || in_y.g >= image_extents.y)\n"
1109 " gl_FragColor.g = border_color.g;\n"
1111 " gl_FragColor.g = texture2D(tex, vec2(in_x.g, in_y.g) / texture_extents).g;\n"
1112 " if(in_x.b < 0.0 || in_x.b >= image_extents.x || in_y.b < 0.0 || in_y.b >= image_extents.y)\n"
1113 " gl_FragColor.b = border_color.b;\n"
1115 " gl_FragColor.b = texture2D(tex, vec2(in_x.b, in_y.b) / texture_extents).b;\n"
1116 " if(in_x.a < 0.0 || in_x.a >= image_extents.x || in_y.a < 0.0 || in_y.a >= image_extents.y)\n"
1117 " gl_FragColor.a = border_color.a;\n"
1119 " gl_FragColor.a = texture2D(tex, vec2(in_x.a, in_y.a) / texture_extents).a;\n"
1122 get_output()->to_texture();
1123 get_output()->enable_opengl();
1124 unsigned int frag_shader = 0;
1128 case LensConfig::SHRINK:
1129 frag_shader = VFrame::make_shader(0, shrink_frag, 0);
1131 case LensConfig::STRETCH:
1132 frag_shader = VFrame::make_shader(0, stretch_frag, 0);
1134 case LensConfig::RECTILINEAR_STRETCH:
1135 frag_shader = VFrame::make_shader(0, rectilinear_stretch_frag, 0);
1137 case LensConfig::RECTILINEAR_SHRINK:
1138 frag_shader = VFrame::make_shader(0, rectilinear_shrink_frag, 0);
1144 float border_color[] = { 0, 0, 0, 0 };
1145 if(BC_CModels::is_yuv(get_output()->get_color_model()))
1147 border_color[1] = 0.5;
1148 border_color[2] = 0.5;
1151 double x_factor = config.aspect;
1152 double y_factor = 1.0 / config.aspect;
1153 if(x_factor < 1) x_factor = 1;
1154 if(y_factor < 1) y_factor = 1;
1156 glUseProgram(frag_shader);
1157 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
1158 glUniform2f(glGetUniformLocation(frag_shader, "aspect"),
1161 glUniform2f(glGetUniformLocation(frag_shader, "center_coord"),
1162 (GLfloat)get_input()->get_w() * config.center_x / 100.0,
1163 (GLfloat)get_input()->get_h() * config.center_y / 100.0);
1164 glUniform2f(glGetUniformLocation(frag_shader, "texture_extents"),
1165 (GLfloat)get_input()->get_texture_w(),
1166 (GLfloat)get_input()->get_texture_h());
1167 glUniform2f(glGetUniformLocation(frag_shader, "image_extents"),
1168 (GLfloat)get_input()->get_w(),
1169 (GLfloat)get_input()->get_h());
1171 int width = get_output()->get_w();
1172 int height = get_output()->get_h();
1173 float *fov = config.fov;
1178 case LensConfig::SHRINK:
1179 dim = MAX(width, height) * config.radius;
1180 max_z = dim * sqrt(2.0) / 2;
1181 glUniform4fv(glGetUniformLocation(frag_shader, "border_color"),
1183 (GLfloat*)border_color);
1184 glUniform4f(glGetUniformLocation(frag_shader, "max_z"),
1189 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1190 (max_z / fov[0]) * 2 / M_PI,
1191 (max_z / fov[1]) * 2 / M_PI,
1192 (max_z / fov[2]) * 2 / M_PI,
1193 (max_z / fov[3]) * 2 / M_PI);
1196 case LensConfig::STRETCH:
1197 dim = MAX(width, height) * config.radius;
1198 max_z = dim * sqrt(2.0) / 2;
1199 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1200 max_z / M_PI / (fov[0] / 2.0),
1201 max_z / M_PI / (fov[1] / 2.0),
1202 max_z / M_PI / (fov[2] / 2.0),
1203 max_z / M_PI / (fov[3] / 2.0));
1206 case LensConfig::RECTILINEAR_STRETCH:
1207 max_z = sqrt(SQR(width) + SQR(height)) / 2;
1208 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1209 max_z / M_PI / (fov[0] / 2.0),
1210 max_z / M_PI / (fov[1] / 2.0),
1211 max_z / M_PI / (fov[2] / 2.0),
1212 max_z / M_PI / (fov[3] / 2.0));
1213 glUniform1f(glGetUniformLocation(frag_shader, "radius"),
1217 case LensConfig::RECTILINEAR_SHRINK:
1218 max_z = sqrt(SQR(width) + SQR(height)) / 2;
1219 glUniform4f(glGetUniformLocation(frag_shader, "r"),
1220 max_z / M_PI / (fov[0] / 2.0),
1221 max_z / M_PI / (fov[1] / 2.0),
1222 max_z / M_PI / (fov[2] / 2.0),
1223 max_z / M_PI / (fov[3] / 2.0));
1224 glUniform1f(glGetUniformLocation(frag_shader, "radius"),
1230 get_output()->init_screen();
1231 get_output()->bind_texture(0);
1232 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);
1233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
1234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
1235 get_output()->draw_texture();
1239 if(config.draw_guides)
1241 int w = get_output()->get_w();
1242 int h = get_output()->get_h();
1243 int center_x = (int)(config.center_x * w / 100);
1244 int center_y = (int)(config.center_y * h / 100);
1246 glDisable(GL_TEXTURE_2D);
1247 glColor4f(0.0, 0.0, 0.0, 1.0);
1249 glVertex3f(center_x, -h + center_y - CENTER_H / 2, 0.0);
1250 glVertex3f(center_x, -h + center_y + CENTER_H / 2, 0.0);
1253 glVertex3f(center_x - CENTER_W / 2, -h + center_y, 0.0);
1254 glVertex3f(center_x + CENTER_W / 2, -h + center_y, 0.0);
1256 glColor4f(1.0, 1.0, 1.0, 1.0);
1258 glVertex3f(center_x - 1, -h + center_y - CENTER_H / 2 - 1, 0.0);
1259 glVertex3f(center_x - 1, -h + center_y + CENTER_H / 2 - 1, 0.0);
1262 glVertex3f(center_x - CENTER_W / 2 - 1, -h + center_y - 1, 0.0);
1263 glVertex3f(center_x + CENTER_W / 2 - 1, -h + center_y - 1, 0.0);
1266 get_output()->set_opengl_state(VFrame::SCREEN);
1280 LensPackage::LensPackage()
1287 LensUnit::LensUnit(LensEngine *engine, LensMain *plugin)
1288 : LoadClient(engine)
1290 this->plugin = plugin;
1293 LensUnit::~LensUnit()
1297 void LensUnit::process_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_SHRINK(type, components, chroma) \
1331 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1332 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1333 type black[4] = { 0, chroma, chroma, 0 }; \
1335 for(int y = row1; y < row2; y++) \
1337 type *out_row = out_rows[y]; \
1338 type *in_row = in_rows[y]; \
1339 double y_diff = y - center_y; \
1341 for(int x = 0; x < width; x++) \
1343 double x_diff = x - center_x; \
1344 if(!x_diff && !y_diff) \
1346 type *in_pixel = in_row + x * components; \
1347 for(int c = 0; c < components; c++) \
1349 *out_row++ = *in_pixel++; \
1354 double z = sqrt(x_diff * x_diff + y_diff * y_diff); \
1355 double a2 = atan(y_diff / x_diff); \
1356 if(x_diff < 0.0) a2 += M_PI; \
1358 for(int i = 0; i < components; i++) \
1362 *out_row++ = black[i]; \
1366 double a1 = asin(z / r[i]); \
1367 double z_in = a1 * max_z[i] * 2 / M_PI; \
1369 float x_in = z_in * cos(a2) * x_factor + center_x; \
1370 float y_in = z_in * sin(a2) * y_factor + center_y; \
1372 if(x_in < 0.0 || x_in >= width - 1 || \
1373 y_in < 0.0 || y_in >= height - 1) \
1375 *out_row++ = black[i]; \
1379 float y1_fraction = y_in - floor(y_in); \
1380 float y2_fraction = 1.0 - y1_fraction; \
1381 float x1_fraction = x_in - floor(x_in); \
1382 float x2_fraction = 1.0 - x1_fraction; \
1383 type *in_pixel1 = in_rows[(int)y_in] + (int)x_in * components; \
1384 type *in_pixel2 = in_rows[(int)y_in + 1] + (int)x_in * components; \
1385 *out_row++ = (type)(in_pixel1[i] * x2_fraction * y2_fraction + \
1386 in_pixel2[i] * x2_fraction * y1_fraction + \
1387 in_pixel1[i + components] * x1_fraction * y2_fraction + \
1388 in_pixel2[i + components] * x1_fraction * y1_fraction); \
1395 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1396 type *in_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1397 for(int c = 0; c < components; c++) \
1399 *out_pixel++ = *in_pixel++; \
1404 switch(plugin->get_input()->get_color_model())
1407 DO_LENS_SHRINK(unsigned char, 3, 0x0);
1410 DO_LENS_SHRINK(unsigned char, 4, 0x0);
1413 DO_LENS_SHRINK(float, 3, 0.0);
1416 DO_LENS_SHRINK(float, 4, 0.0);
1419 DO_LENS_SHRINK(unsigned char, 3, 0x80);
1422 DO_LENS_SHRINK(unsigned char, 4, 0x80);
1427 void LensUnit::process_stretch(LensPackage *pkg)
1429 float *fov = plugin->config.fov;
1430 float aspect = plugin->config.aspect;
1431 int row1 = pkg->row1;
1432 int row2 = pkg->row2;
1433 double x_factor = aspect;
1434 double y_factor = 1.0 / aspect;
1435 if(x_factor < 1) x_factor = 1;
1436 if(y_factor < 1) y_factor = 1;
1437 int width = plugin->get_input()->get_w();
1438 int height = plugin->get_input()->get_h();
1439 double dim = MAX(width, height) * plugin->config.radius;
1440 double max_z = dim * sqrt(2.0) / 2;
1441 double center_x = width * plugin->config.center_x / 100.0;
1442 double center_y = height * plugin->config.center_y / 100.0;
1443 double r[FOV_CHANNELS];
1445 r[0] = max_z / M_PI / (fov[0] / 2.0);
1446 r[1] = max_z / M_PI / (fov[1] / 2.0);
1447 r[2] = max_z / M_PI / (fov[2] / 2.0);
1448 r[3] = max_z / M_PI / (fov[3] / 2.0);
1450 #define DO_LENS_STRETCH(type, components, chroma) \
1452 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1453 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1454 type black[4] = { 0, chroma, chroma, 0 }; \
1456 for(int y = row1; y < row2; y++) \
1458 type *out_row = out_rows[y]; \
1459 double y_diff = y - center_y; \
1461 for(int x = 0; x < width; x++) \
1463 double x_diff = (x - center_x); \
1464 double z = sqrt(x_diff * x_diff + \
1470 a2 = 3 * M_PI / 2; \
1476 a2 = atan(y_diff / x_diff); \
1478 if(x_diff < 0.0) a2 += M_PI; \
1480 for(int i = 0; i < components; i++) \
1482 double a1 = (z / (M_PI * r[i] / 2)) * (M_PI / 2); \
1483 double z_in = r[i] * sin(a1); \
1485 double x_in = z_in * cos(a2) * x_factor + center_x; \
1486 double y_in = z_in * sin(a2) * y_factor + center_y; \
1488 if(x_in < 0.0 || x_in >= width - 1 || \
1489 y_in < 0.0 || y_in >= height - 1) \
1491 *out_row++ = black[i]; \
1495 float y1_fraction = y_in - floor(y_in); \
1496 float y2_fraction = 1.0 - y1_fraction; \
1497 float x1_fraction = x_in - floor(x_in); \
1498 float x2_fraction = 1.0 - x1_fraction; \
1499 type *in_pixel1 = in_rows[(int)y_in] + (int)x_in * components; \
1500 type *in_pixel2 = in_rows[(int)y_in + 1] + (int)x_in * components; \
1501 *out_row++ = (type)(in_pixel1[i] * x2_fraction * y2_fraction + \
1502 in_pixel2[i] * x2_fraction * y1_fraction + \
1503 in_pixel1[i + components] * x1_fraction * y2_fraction + \
1504 in_pixel2[i + components] * x1_fraction * y1_fraction); \
1510 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1511 type *in_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1512 for(int c = 0; c < components; c++) \
1514 *out_pixel++ = *in_pixel++; \
1519 switch(plugin->get_input()->get_color_model())
1522 DO_LENS_STRETCH(unsigned char, 3, 0x0);
1525 DO_LENS_STRETCH(unsigned char, 4, 0x0);
1528 DO_LENS_STRETCH(float, 3, 0.0);
1531 DO_LENS_STRETCH(float, 4, 0.0);
1534 DO_LENS_STRETCH(unsigned char, 3, 0x80);
1537 DO_LENS_STRETCH(unsigned char, 4, 0x80);
1542 void LensUnit::process_rectilinear_stretch(LensPackage *pkg)
1544 float *fov = plugin->config.fov;
1545 float aspect = plugin->config.aspect;
1546 int row1 = pkg->row1;
1547 int row2 = pkg->row2;
1548 double x_factor = aspect;
1549 double y_factor = 1.0 / aspect;
1550 if(x_factor < 1) x_factor = 1;
1551 if(y_factor < 1) y_factor = 1;
1552 int width = plugin->get_input()->get_w();
1553 int height = plugin->get_input()->get_h();
1554 // double dim = MAX(width, height) * plugin->config.radius;
1555 // double max_z = dim * sqrt(2.0) / 2;
1556 double max_z = sqrt(SQR(width) + SQR(height)) / 2;
1557 double center_x = width * plugin->config.center_x / 100.0;
1558 double center_y = height * plugin->config.center_y / 100.0;
1559 double r[FOV_CHANNELS];
1561 r[0] = max_z / M_PI / (fov[0] / 2.0);
1562 r[1] = max_z / M_PI / (fov[1] / 2.0);
1563 r[2] = max_z / M_PI / (fov[2] / 2.0);
1564 r[3] = max_z / M_PI / (fov[3] / 2.0);
1566 #define DO_LENS_RECTILINEAR_STRETCH(type, components, chroma) \
1568 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1569 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1570 type black[4] = { 0, chroma, chroma, 0 }; \
1572 for(int y = row1; y < row2; y++) \
1574 type *out_row = out_rows[y]; \
1575 double y_diff = y - center_y; \
1577 for(int x = 0; x < width; x++) \
1579 double x_diff = (x - center_x); \
1580 /* Compute magnitude */ \
1581 double z = sqrt(x_diff * x_diff + \
1583 /* Compute angle */ \
1588 angle = 3 * M_PI / 2; \
1594 angle = atan(y_diff / x_diff); \
1596 if(x_diff < 0.0) angle += M_PI; \
1598 for(int i = 0; i < components; i++) \
1600 /* Compute new radius */ \
1601 double radius1 = (z / r[i]) * 2 * plugin->config.radius; \
1602 double z_in = r[i] * atan(radius1) / (M_PI / 2); \
1604 double x_in = z_in * cos(angle) * x_factor + center_x; \
1605 double y_in = z_in * sin(angle) * y_factor + center_y; \
1607 if(x_in < 0.0 || x_in >= width - 1 || \
1608 y_in < 0.0 || y_in >= height - 1) \
1610 *out_row++ = black[i]; \
1614 float y1_fraction = y_in - floor(y_in); \
1615 float y2_fraction = 1.0 - y1_fraction; \
1616 float x1_fraction = x_in - floor(x_in); \
1617 float x2_fraction = 1.0 - x1_fraction; \
1618 type *in_pixel1 = in_rows[(int)y_in] + (int)x_in * components; \
1619 type *in_pixel2 = in_rows[(int)y_in + 1] + (int)x_in * components; \
1620 *out_row++ = (type)(in_pixel1[i] * x2_fraction * y2_fraction + \
1621 in_pixel2[i] * x2_fraction * y1_fraction + \
1622 in_pixel1[i + components] * x1_fraction * y2_fraction + \
1623 in_pixel2[i + components] * x1_fraction * y1_fraction); \
1629 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1630 type *in_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1631 for(int c = 0; c < components; c++) \
1633 *out_pixel++ = *in_pixel++; \
1638 switch(plugin->get_input()->get_color_model())
1641 DO_LENS_RECTILINEAR_STRETCH(unsigned char, 3, 0x0);
1644 DO_LENS_RECTILINEAR_STRETCH(unsigned char, 4, 0x0);
1647 DO_LENS_RECTILINEAR_STRETCH(float, 3, 0.0);
1650 DO_LENS_RECTILINEAR_STRETCH(float, 4, 0.0);
1653 DO_LENS_RECTILINEAR_STRETCH(unsigned char, 3, 0x80);
1656 DO_LENS_RECTILINEAR_STRETCH(unsigned char, 4, 0x80);
1661 void LensUnit::process_rectilinear_shrink(LensPackage *pkg)
1663 float *fov = plugin->config.fov;
1664 float aspect = plugin->config.aspect;
1665 int row1 = pkg->row1;
1666 int row2 = pkg->row2;
1667 double x_factor = aspect;
1668 double y_factor = 1.0 / aspect;
1669 if(x_factor < 1) x_factor = 1;
1670 if(y_factor < 1) y_factor = 1;
1671 int width = plugin->get_input()->get_w();
1672 int height = plugin->get_input()->get_h();
1673 double max_z = MAX(width, height) / 2 * plugin->config.radius;
1674 double center_x = width * plugin->config.center_x / 100.0;
1675 double center_y = height * plugin->config.center_y / 100.0;
1676 double r[FOV_CHANNELS];
1678 r[0] = max_z / fov[0];
1679 r[1] = max_z / fov[1];
1680 r[2] = max_z / fov[2];
1681 r[3] = max_z / fov[3];
1683 #define DO_LENS_RECTILINEAR_SHRINK(type, components, chroma) \
1685 type **in_rows = (type**)plugin->get_temp()->get_rows(); \
1686 type **out_rows = (type**)plugin->get_input()->get_rows(); \
1687 type black[4] = { 0, chroma, chroma, 0 }; \
1689 for(int y = row1; y < row2; y++) \
1691 type *out_row = out_rows[y]; \
1692 double y_diff = y - center_y; \
1694 for(int x = 0; x < width; x++) \
1696 double x_diff = (x - center_x); \
1697 /* Compute magnitude */ \
1698 double z = sqrt(x_diff * x_diff + \
1700 /* Compute angle */ \
1705 angle = 3 * M_PI / 2; \
1711 angle = atan(y_diff / x_diff); \
1713 if(x_diff < 0.0) angle += M_PI; \
1715 for(int i = 0; i < components; i++) \
1717 /* Compute new radius */ \
1718 double radius1 = z / r[i]; \
1719 double z_in = r[i] * tan(radius1) / (M_PI / 2); \
1721 double x_in = z_in * cos(angle) * x_factor + center_x; \
1722 double y_in = z_in * sin(angle) * y_factor + center_y; \
1724 if(x_in < 0.0 || x_in >= width - 1 || \
1725 y_in < 0.0 || y_in >= height - 1) \
1727 *out_row++ = black[i]; \
1731 float y1_fraction = y_in - floor(y_in); \
1732 float y2_fraction = 1.0 - y1_fraction; \
1733 float x1_fraction = x_in - floor(x_in); \
1734 float x2_fraction = 1.0 - x1_fraction; \
1735 type *in_pixel1 = in_rows[(int)y_in] + (int)x_in * components; \
1736 type *in_pixel2 = in_rows[(int)y_in + 1] + (int)x_in * components; \
1737 *out_row++ = (type)(in_pixel1[i] * x2_fraction * y2_fraction + \
1738 in_pixel2[i] * x2_fraction * y1_fraction + \
1739 in_pixel1[i + components] * x1_fraction * y2_fraction + \
1740 in_pixel2[i + components] * x1_fraction * y1_fraction); \
1746 type *out_pixel = out_rows[(int)center_y] + (int)center_x * components; \
1747 type *in_pixel = in_rows[(int)center_y] + (int)center_x * components; \
1748 for(int c = 0; c < components; c++) \
1750 *out_pixel++ = *in_pixel++; \
1755 switch(plugin->get_input()->get_color_model())
1758 DO_LENS_RECTILINEAR_SHRINK(unsigned char, 3, 0x0);
1761 DO_LENS_RECTILINEAR_SHRINK(unsigned char, 4, 0x0);
1764 DO_LENS_RECTILINEAR_SHRINK(float, 3, 0.0);
1767 DO_LENS_RECTILINEAR_SHRINK(float, 4, 0.0);
1770 DO_LENS_RECTILINEAR_SHRINK(unsigned char, 3, 0x80);
1773 DO_LENS_RECTILINEAR_SHRINK(unsigned char, 4, 0x80);
1778 void LensUnit::process_package(LoadPackage *package)
1780 LensPackage *pkg = (LensPackage*)package;
1782 switch(plugin->config.mode)
1784 case LensConfig::SHRINK:
1785 process_shrink(pkg);
1787 case LensConfig::STRETCH:
1788 process_stretch(pkg);
1790 case LensConfig::RECTILINEAR_STRETCH:
1791 process_rectilinear_stretch(pkg);
1793 case LensConfig::RECTILINEAR_SHRINK :
1794 process_rectilinear_shrink(pkg);
1803 LensEngine::LensEngine(LensMain *plugin)
1804 : LoadServer(plugin->PluginClient::smp + 1, plugin->PluginClient::smp + 1)
1805 // : LoadServer(1, 1)
1807 this->plugin = plugin;
1810 LensEngine::~LensEngine()
1814 void LensEngine::init_packages()
1816 for(int i = 0; i < LoadServer::get_total_packages(); i++)
1818 LensPackage *package = (LensPackage*)LoadServer::get_package(i);
1819 package->row1 = plugin->get_input()->get_h() * i / LoadServer::get_total_packages();
1820 package->row2 = plugin->get_input()->get_h() * (i + 1) / LoadServer::get_total_packages();
1824 LoadClient* LensEngine::new_client()
1826 return new LensUnit(this, plugin);
1829 LoadPackage* LensEngine::new_package()
1831 return new LensPackage;