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
27 #include "bcdisplayinfo.h"
31 #include "linearblur.h"
34 #include "loadbalance.h"
35 #include "pluginvclient.h"
44 REGISTER_PLUGIN(LinearBlurMain)
49 LinearBlurConfig::LinearBlurConfig()
51 reset(RESET_DEFAULT_SETTINGS);
54 void LinearBlurConfig::reset(int clear)
66 case RESET_RADIUS : radius = 0;
68 case RESET_ANGLE : angle = 0;
70 case RESET_STEPS : steps = 1;
72 case RESET_DEFAULT_SETTINGS :
85 int LinearBlurConfig::equivalent(LinearBlurConfig &that)
88 radius == that.radius &&
89 angle == that.angle &&
90 steps == that.steps &&
97 void LinearBlurConfig::copy_from(LinearBlurConfig &that)
108 void LinearBlurConfig::interpolate(LinearBlurConfig &prev,
109 LinearBlurConfig &next,
114 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
115 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
116 this->radius = (int)(prev.radius * prev_scale + next.radius * next_scale + 0.5);
117 this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale + 0.5);
118 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
136 LinearBlurWindow::LinearBlurWindow(LinearBlurMain *plugin)
137 : PluginClientWindow(plugin,
144 this->plugin = plugin;
147 LinearBlurWindow::~LinearBlurWindow()
151 void LinearBlurWindow::create_objects()
153 int xs10 = xS(10), xs100 = xS(100), xs200 = xS(200);
154 int ys10 = yS(10), ys30 = yS(30), ys40 = yS(40);
155 int x = xs10, y = ys10;
156 int x2 = xS(80), x3 = xS(180);
157 int clr_x = get_w()-x - xS(22); // note: clrBtn_w = 22
158 int defaultBtn_w = xs100;
163 add_subwindow(new BC_Title(x, y, _("Length:")));
164 radius_text = new LinearBlurIText(this, plugin,
165 0, &plugin->config.radius, (x + x2), y, RADIUS_MIN, RADIUS_MAX);
166 radius_text->create_objects();
167 radius_slider = new LinearBlurISlider(plugin,
168 radius_text, &plugin->config.radius, x3, y, RADIUS_MIN, RADIUS_MAX, xs200);
169 add_subwindow(radius_slider);
170 radius_text->slider = radius_slider;
171 clr_x = x3 + radius_slider->get_w() + x;
172 add_subwindow(radius_Clr = new LinearBlurClr(plugin, this, clr_x, y, RESET_RADIUS));
175 add_subwindow(new BC_Title(x, y, _("Angle:")));
176 angle_text = new LinearBlurIText(this, plugin,
177 0, &plugin->config.angle, (x + x2), y, -ANGLE_MAX, ANGLE_MAX);
178 angle_text->create_objects();
179 angle_slider = new LinearBlurISlider(plugin,
180 angle_text, &plugin->config.angle, x3, y, -ANGLE_MAX, ANGLE_MAX, xs200);
181 add_subwindow(angle_slider);
182 angle_text->slider = angle_slider;
183 add_subwindow(angle_Clr = new LinearBlurClr(plugin, this, clr_x, y, RESET_ANGLE));
186 add_subwindow(new BC_Title(x, y, _("Steps:")));
187 steps_text = new LinearBlurIText(this, plugin,
188 0, &plugin->config.steps, (x + x2), y, STEPS_MIN, STEPS_MAX);
189 steps_text->create_objects();
190 steps_slider = new LinearBlurISlider(plugin,
191 steps_text, &plugin->config.steps, x3, y, STEPS_MIN, STEPS_MAX, xs200);
192 add_subwindow(steps_slider);
193 steps_text->slider = steps_slider;
194 add_subwindow(steps_Clr = new LinearBlurClr(plugin, this, clr_x, y, RESET_STEPS));
197 add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
200 int toggle_w = (get_w()-2*x) / 4;
201 add_subwindow(r = new LinearBlurToggle(plugin, x1, y, &plugin->config.r, _("Red")));
203 add_subwindow(g = new LinearBlurToggle(plugin, x1, y, &plugin->config.g, _("Green")));
205 add_subwindow(b = new LinearBlurToggle(plugin, x1, y, &plugin->config.b, _("Blue")));
207 add_subwindow(a = new LinearBlurToggle(plugin, x1, y, &plugin->config.a, _("Alpha")));
211 add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
213 add_subwindow(reset = new LinearBlurReset(plugin, this, x, y));
214 add_subwindow(default_settings = new LinearBlurDefaultSettings(plugin, this,
215 (get_w() - xs10 - defaultBtn_w), y, defaultBtn_w));
223 void LinearBlurWindow::update_gui(int clear)
227 radius_text->update((int64_t)plugin->config.radius);
228 radius_slider->update(plugin->config.radius);
231 angle_text->update((int64_t)plugin->config.angle);
232 angle_slider->update(plugin->config.angle);
235 steps_text->update((int64_t)plugin->config.steps);
236 steps_slider->update(plugin->config.steps);
239 case RESET_DEFAULT_SETTINGS :
241 radius_text->update((int64_t)plugin->config.radius);
242 radius_slider->update(plugin->config.radius);
243 angle_text->update((int64_t)plugin->config.angle);
244 angle_slider->update(plugin->config.angle);
245 steps_text->update((int64_t)plugin->config.steps);
246 steps_slider->update(plugin->config.steps);
247 r->update(plugin->config.r);
248 g->update(plugin->config.g);
249 b->update(plugin->config.b);
250 a->update(plugin->config.a);
264 LinearBlurToggle::LinearBlurToggle(LinearBlurMain *plugin,
269 : BC_CheckBox(x, y, *output, string)
271 this->plugin = plugin;
272 this->output = output;
275 int LinearBlurToggle::handle_event()
277 *output = get_value();
278 plugin->send_configure_change();
285 LinearBlurIText::LinearBlurIText(LinearBlurWindow *gui, LinearBlurMain *plugin,
286 LinearBlurISlider *slider, int *output, int x, int y, int min, int max)
287 : BC_TumbleTextBox(gui, *output,
288 min, max, x, y, xS(60), 0)
291 this->plugin = plugin;
292 this->output = output;
293 this->slider = slider;
299 LinearBlurIText::~LinearBlurIText()
303 int LinearBlurIText::handle_event()
305 *output = atoi(get_text());
306 if(*output > max) *output = max;
307 if(*output < min) *output = min;
308 slider->update(*output);
309 plugin->send_configure_change();
314 LinearBlurISlider::LinearBlurISlider(LinearBlurMain *plugin,
315 LinearBlurIText *text, int *output, int x, int y, int min, int max, int w)
316 : BC_ISlider(x, y, 0, w, w, min, max, *output)
318 this->plugin = plugin;
319 this->output = output;
321 enable_show_value(0); // Hide caption
324 LinearBlurISlider::~LinearBlurISlider()
328 int LinearBlurISlider::handle_event()
330 *output = get_value();
331 text->update((int64_t)*output);
332 plugin->send_configure_change();
337 LinearBlurSize::LinearBlurSize(LinearBlurMain *plugin,
343 : BC_ISlider(x, y, 0, xS(200), yS(200), min, max, *output)
345 this->plugin = plugin;
346 this->output = output;
348 int LinearBlurSize::handle_event()
350 *output = get_value();
351 plugin->send_configure_change();
356 LinearBlurReset::LinearBlurReset(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y)
357 : BC_GenericButton(x, y, _("Reset"))
359 this->plugin = plugin;
362 LinearBlurReset::~LinearBlurReset()
365 int LinearBlurReset::handle_event()
367 plugin->config.reset(RESET_ALL);
368 gui->update_gui(RESET_ALL);
369 plugin->send_configure_change();
374 LinearBlurDefaultSettings::LinearBlurDefaultSettings(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y, int w)
375 : BC_GenericButton(x, y, w, _("Default"))
377 this->plugin = plugin;
380 LinearBlurDefaultSettings::~LinearBlurDefaultSettings()
383 int LinearBlurDefaultSettings::handle_event()
385 plugin->config.reset(RESET_DEFAULT_SETTINGS);
386 gui->update_gui(RESET_DEFAULT_SETTINGS);
387 plugin->send_configure_change();
392 LinearBlurClr::LinearBlurClr(LinearBlurMain *plugin, LinearBlurWindow *gui, int x, int y, int clear)
393 : BC_Button(x, y, plugin->get_theme()->get_image_set("reset_button"))
395 this->plugin = plugin;
399 LinearBlurClr::~LinearBlurClr()
402 int LinearBlurClr::handle_event()
404 // clear==1 ==> Radius slider
405 // clear==2 ==> Angle slider
406 // clear==3 ==> Steps slider
407 plugin->config.reset(clear);
408 gui->update_gui(clear);
409 plugin->send_configure_change();
420 LinearBlurMain::LinearBlurMain(PluginServer *server)
421 : PluginVClient(server)
429 need_reconfigure = 1;
434 LinearBlurMain::~LinearBlurMain()
437 if(engine) delete engine;
439 if(accum) delete [] accum;
440 if(temp) delete temp;
443 const char* LinearBlurMain::plugin_title() { return N_("Linear Blur"); }
444 int LinearBlurMain::is_realtime() { return 1; }
447 NEW_WINDOW_MACRO(LinearBlurMain, LinearBlurWindow)
448 LOAD_CONFIGURATION_MACRO(LinearBlurMain, LinearBlurConfig)
450 void LinearBlurMain::delete_tables()
454 for(int i = 0; i < table_entries; i++)
455 delete [] scale_x_table[i];
456 delete [] scale_x_table;
461 for(int i = 0; i < table_entries; i++)
462 delete [] scale_y_table[i];
463 delete [] scale_y_table;
465 delete [] layer_table;
472 int LinearBlurMain::process_buffer(VFrame *frame,
473 int64_t start_position,
476 need_reconfigure |= load_configuration();
480 get_source_position(),
484 // Generate tables here. The same table is used by many packages to render
485 // each horizontal stripe. Need to cover the entire output range in each
486 // table to avoid green borders
489 int w = frame->get_w();
490 int h = frame->get_h();
493 int angle = config.angle;
494 int radius = config.radius * MIN(w, h) / 100;
496 while(angle < 0) angle += 360;
517 y_offset = (int)(sin((float)config.angle / 360 * 2 * M_PI) * radius);
518 x_offset = (int)(cos((float)config.angle / 360 * 2 * M_PI) * radius);
524 scale_x_table = new int*[config.steps];
525 scale_y_table = new int*[config.steps];
526 table_entries = config.steps;
527 layer_table = new LinearBlurLayer[table_entries];
529 //printf("LinearBlurMain::process_realtime 1 %d %d %d\n", radius, x_offset, y_offset);
531 for(int i = 0; i < config.steps; i++)
533 float fraction = (float)(i - config.steps / 2) / config.steps;
534 int x = (int)(fraction * x_offset);
535 int y = (int)(fraction * y_offset);
539 scale_y_table[i] = y_table = new int[h];
540 scale_x_table[i] = x_table = new int[w];
542 layer_table[i].x = x;
543 layer_table[i].y = y;
544 for(int j = 0; j < h; j++)
547 CLAMP(y_table[j], 0, h - 1);
549 for(int j = 0; j < w; j++)
552 CLAMP(x_table[j], 0, w - 1);
555 need_reconfigure = 0;
558 if(get_use_opengl()) return run_opengl();
561 if(!engine) engine = new LinearBlurEngine(this,
562 get_project_smp() + 1,
563 get_project_smp() + 1);
564 if(!accum) accum = new unsigned char[frame->get_w() *
566 BC_CModels::components(frame->get_color_model()) *
567 MAX(sizeof(int), sizeof(float))];
570 this->output = frame;
574 temp = new VFrame(frame->get_w(), frame->get_h(),
575 frame->get_color_model(), 0);
576 temp->copy_from(frame);
583 BC_CModels::components(frame->get_color_model()) *
584 MAX(sizeof(int), sizeof(float)));
585 engine->process_packages();
590 void LinearBlurMain::update_gui()
594 load_configuration();
595 ((LinearBlurWindow*)thread->window)->lock_window();
596 ((LinearBlurWindow*)thread->window)->radius_text->update((int64_t)config.radius);
597 ((LinearBlurWindow*)thread->window)->radius_slider->update(config.radius);
598 ((LinearBlurWindow*)thread->window)->angle_text->update((int64_t)config.angle);
599 ((LinearBlurWindow*)thread->window)->angle_slider->update(config.angle);
600 ((LinearBlurWindow*)thread->window)->steps_text->update((int64_t)config.steps);
601 ((LinearBlurWindow*)thread->window)->steps_slider->update(config.steps);
603 ((LinearBlurWindow*)thread->window)->r->update(config.r);
604 ((LinearBlurWindow*)thread->window)->g->update(config.g);
605 ((LinearBlurWindow*)thread->window)->b->update(config.b);
606 ((LinearBlurWindow*)thread->window)->a->update(config.a);
607 ((LinearBlurWindow*)thread->window)->unlock_window();
613 void LinearBlurMain::save_data(KeyFrame *keyframe)
617 // cause data to be stored directly in text
618 output.set_shared_output(keyframe->xbuf);
619 output.tag.set_title("LINEARBLUR");
621 output.tag.set_property("RADIUS", config.radius);
622 output.tag.set_property("ANGLE", config.angle);
623 output.tag.set_property("STEPS", config.steps);
624 output.tag.set_property("R", config.r);
625 output.tag.set_property("G", config.g);
626 output.tag.set_property("B", config.b);
627 output.tag.set_property("A", config.a);
629 output.tag.set_title("/LINEARBLUR");
631 output.append_newline();
632 output.terminate_string();
635 void LinearBlurMain::read_data(KeyFrame *keyframe)
639 input.set_shared_input(keyframe->xbuf);
645 result = input.read_tag();
649 if(input.tag.title_is("LINEARBLUR"))
651 config.radius = input.tag.get_property("RADIUS", config.radius);
652 config.angle = input.tag.get_property("ANGLE", config.angle);
653 config.steps = input.tag.get_property("STEPS", config.steps);
654 config.r = input.tag.get_property("R", config.r);
655 config.g = input.tag.get_property("G", config.g);
656 config.b = input.tag.get_property("B", config.b);
657 config.a = input.tag.get_property("A", config.a);
664 static void draw_box(float x1, float y1, float x2, float y2)
667 glVertex3f(x1, y1, 0.0);
668 glVertex3f(x2, y1, 0.0);
669 glVertex3f(x2, y2, 0.0);
670 glVertex3f(x1, y2, 0.0);
675 int LinearBlurMain::handle_opengl()
678 get_output()->to_texture();
679 get_output()->enable_opengl();
680 get_output()->init_screen();
681 get_output()->bind_texture(0);
683 int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
684 glClearColor(0.0, 0.0, 0.0, 0.0);
685 glClear(GL_COLOR_BUFFER_BIT);
687 // Draw unselected channels
689 glBlendFunc(GL_ONE, GL_ONE);
690 glDrawBuffer(GL_BACK);
691 if(!config.r || !config.g || !config.b || !config.a)
693 glColor4f(config.r ? 0 : 1,
697 get_output()->draw_texture();
699 glAccum(GL_LOAD, 1.0);
701 // Blur selected channels
702 float fraction = 1.0 / config.steps;
703 for(int i = 0; i < config.steps; i++)
705 glClear(GL_COLOR_BUFFER_BIT);
706 glColor4f(config.r ? 1 : 0,
711 int w = get_output()->get_w();
712 int h = get_output()->get_h();
713 get_output()->draw_texture(0,
718 get_output()->get_h() - layer_table[i].y,
719 layer_table[i].x + w,
720 get_output()->get_h() - layer_table[i].y - h,
725 glDisable(GL_TEXTURE_2D);
728 glColor4f(config.r ? 0.0 : 0,
732 float center_x1 = 0.0;
733 float center_x2 = get_output()->get_w();
734 float project_x1 = layer_table[i].x;
735 float project_x2 = layer_table[i].x + get_output()->get_w();
736 float project_y1 = layer_table[i].y;
737 float project_y2 = layer_table[i].y + get_output()->get_h();
740 center_x1 = project_x1;
741 draw_box(0, 0, project_x1, -get_output()->get_h());
743 if(project_x2 < get_output()->get_w())
745 center_x2 = project_x2;
746 draw_box(project_x2, 0, get_output()->get_w(), -get_output()->get_h());
751 -get_output()->get_h(),
753 -get_output()->get_h() + project_y1);
755 if(project_y2 < get_output()->get_h())
758 -get_output()->get_h() + project_y2,
767 glAccum(GL_ACCUM, fraction);
768 glEnable(GL_TEXTURE_2D);
769 glColor4f(config.r ? 1 : 0,
776 glDisable(GL_TEXTURE_2D);
777 glReadBuffer(GL_BACK);
778 glAccum(GL_RETURN, 1.0);
780 glColor4f(1, 1, 1, 1);
781 get_output()->set_opengl_state(VFrame::SCREEN);
791 LinearBlurPackage::LinearBlurPackage()
799 LinearBlurUnit::LinearBlurUnit(LinearBlurEngine *server,
800 LinearBlurMain *plugin)
803 this->plugin = plugin;
804 this->server = server;
808 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP, MAX, DO_YUV) \
810 for(int j = pkg->y1; j < pkg->y2; j++) \
812 TEMP *out_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
813 int in_y = y_table[j]; \
816 TYPE *in_row = (TYPE*)plugin->input->get_rows()[in_y]; \
817 for(int k = 0; k < w; k++) \
819 int in_x = x_table[k]; \
821 int in_offset = in_x * COMPONENTS; \
822 *out_row++ += in_row[in_offset]; \
825 *out_row++ += in_row[in_offset + 1]; \
826 *out_row++ += in_row[in_offset + 2]; \
830 *out_row++ += in_row[in_offset + 1]; \
831 *out_row++ += in_row[in_offset + 2]; \
833 if(COMPONENTS == 4) \
834 *out_row++ += in_row[in_offset + 3]; \
838 /* Copy to output */ \
839 if(i == plugin->config.steps - 1) \
841 for(int j = pkg->y1; j < pkg->y2; j++) \
843 TEMP *in_row = (TEMP*)plugin->accum + COMPONENTS * w * j; \
844 TYPE *in_backup = (TYPE*)plugin->input->get_rows()[j]; \
845 TYPE *out_row = (TYPE*)plugin->output->get_rows()[j]; \
846 for(int k = 0; k < w; k++) \
850 *out_row++ = (*in_row++ * fraction) / 0x10000; \
855 *out_row++ = *in_backup++; \
863 *out_row++ = (*in_row++ * fraction) / 0x10000; \
868 *out_row++ = *in_backup++; \
874 *out_row++ = (*in_row++ * fraction) / 0x10000; \
879 *out_row++ = *in_backup++; \
887 *out_row++ = (*in_row++ * fraction) / 0x10000; \
892 *out_row++ = *in_backup++; \
898 *out_row++ = (*in_row++ * fraction) / 0x10000; \
903 *out_row++ = *in_backup++; \
908 if(COMPONENTS == 4) \
912 *out_row++ = (*in_row++ * fraction) / 0x10000; \
917 *out_row++ = *in_backup++; \
926 void LinearBlurUnit::process_package(LoadPackage *package)
928 LinearBlurPackage *pkg = (LinearBlurPackage*)package;
929 //int h = plugin->output->get_h();
930 int w = plugin->output->get_w();
932 int fraction = 0x10000 / plugin->config.steps;
933 int do_r = plugin->config.r;
934 int do_g = plugin->config.g;
935 int do_b = plugin->config.b;
936 int do_a = plugin->config.a;
937 for(int i = 0; i < plugin->config.steps; i++)
939 int *x_table = plugin->scale_x_table[i];
940 int *y_table = plugin->scale_y_table[i];
942 switch(plugin->input->get_color_model())
945 BLEND_LAYER(3, float, float, 1, 0)
948 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
951 BLEND_LAYER(4, float, float, 1, 0)
954 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
957 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
959 case BC_RGBA16161616:
960 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
963 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
966 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
969 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
971 case BC_YUVA16161616:
972 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
983 LinearBlurEngine::LinearBlurEngine(LinearBlurMain *plugin,
986 : LoadServer(total_clients, total_packages)
988 this->plugin = plugin;
991 void LinearBlurEngine::init_packages()
993 for(int i = 0; i < get_total_packages(); i++)
995 LinearBlurPackage *package = (LinearBlurPackage*)get_package(i);
996 package->y1 = plugin->output->get_h() * i / get_total_packages();
997 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
1001 LoadClient* LinearBlurEngine::new_client()
1003 return new LinearBlurUnit(this, plugin);
1006 LoadPackage* LinearBlurEngine::new_package()
1008 return new LinearBlurPackage;