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
23 #include "radialblur.h"
27 REGISTER_PLUGIN(RadialBlurMain)
31 RadialBlurConfig::RadialBlurConfig()
37 void RadialBlurConfig::reset()
49 int RadialBlurConfig::equivalent(RadialBlurConfig &that)
52 angle == that.angle &&
55 steps == that.steps &&
62 void RadialBlurConfig::copy_from(RadialBlurConfig &that)
74 void RadialBlurConfig::interpolate(RadialBlurConfig &prev,
75 RadialBlurConfig &next,
80 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
81 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
82 this->x = (int)(prev.x * prev_scale + next.x * next_scale + 0.5);
83 this->y = (int)(prev.y * prev_scale + next.y * next_scale + 0.5);
84 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
85 this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale + 0.5);
104 RadialBlurWindow::RadialBlurWindow(RadialBlurMain *plugin)
105 : PluginClientWindow(plugin,
112 this->plugin = plugin;
115 RadialBlurWindow::~RadialBlurWindow()
119 void RadialBlurWindow::create_objects()
123 add_subwindow(new BC_Title(x, y, _("X:")));
125 add_subwindow(this->x = new RadialBlurSize(plugin, x, y, &plugin->config.x, 0, 100));
127 add_subwindow(new BC_Title(x, y, _("Y:")));
129 add_subwindow(this->y = new RadialBlurSize(plugin, x, y, &plugin->config.y, 0, 100));
131 add_subwindow(new BC_Title(x, y, _("Angle:")));
133 add_subwindow(angle = new RadialBlurSize(plugin, x, y, &plugin->config.angle, 0, 360));
135 add_subwindow(new BC_Title(x, y, _("Steps:")));
137 add_subwindow(steps = new RadialBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
139 add_subwindow(r = new RadialBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
141 add_subwindow(g = new RadialBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
143 add_subwindow(b = new RadialBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
145 add_subwindow(a = new RadialBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
147 add_subwindow(reset = new RadialBlurReset(plugin, this, x, y));
154 void RadialBlurWindow::update()
156 this->x->update(plugin->config.x);
157 this->y->update(plugin->config.y);
158 angle->update(plugin->config.angle);
159 steps->update(plugin->config.steps);
160 r->update(plugin->config.r);
161 g->update(plugin->config.g);
162 b->update(plugin->config.b);
163 a->update(plugin->config.a);
174 RadialBlurToggle::RadialBlurToggle(RadialBlurMain *plugin,
179 : BC_CheckBox(x, y, *output, string)
181 this->plugin = plugin;
182 this->output = output;
185 int RadialBlurToggle::handle_event()
187 *output = get_value();
188 plugin->send_configure_change();
198 RadialBlurSize::RadialBlurSize(RadialBlurMain *plugin,
204 : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
206 this->plugin = plugin;
207 this->output = output;
209 int RadialBlurSize::handle_event()
211 *output = get_value();
212 plugin->send_configure_change();
222 RadialBlurReset::RadialBlurReset(RadialBlurMain *plugin, RadialBlurWindow *gui, int x, int y)
223 : BC_GenericButton(x, y, _("Reset"))
225 this->plugin = plugin;
228 RadialBlurReset::~RadialBlurReset()
231 int RadialBlurReset::handle_event()
233 plugin->config.reset();
235 plugin->send_configure_change();
245 RadialBlurMain::RadialBlurMain(PluginServer *server)
246 : PluginVClient(server)
254 RadialBlurMain::~RadialBlurMain()
257 if(engine) delete engine;
258 if(temp) delete temp;
262 const char* RadialBlurMain::plugin_title() { return N_("Radial Blur"); }
263 int RadialBlurMain::is_realtime() { return 1; }
267 NEW_WINDOW_MACRO(RadialBlurMain, RadialBlurWindow)
269 LOAD_CONFIGURATION_MACRO(RadialBlurMain, RadialBlurConfig)
271 int RadialBlurMain::process_buffer(VFrame *frame,
272 int64_t start_position,
275 load_configuration();
280 get_source_position(),
285 if(get_use_opengl()) return run_opengl();
287 if(!engine) engine = new RadialBlurEngine(this,
288 get_project_smp() + 1,
289 get_project_smp() + 1);
292 this->output = frame;
296 temp = new VFrame(frame->get_w(), frame->get_h(),
297 frame->get_color_model(), 0);
298 temp->copy_from(frame);
301 engine->process_packages();
306 void RadialBlurMain::update_gui()
310 load_configuration();
311 thread->window->lock_window();
312 ((RadialBlurWindow*)thread->window)->x->update(config.x);
313 ((RadialBlurWindow*)thread->window)->y->update(config.y);
314 ((RadialBlurWindow*)thread->window)->angle->update(config.angle);
315 ((RadialBlurWindow*)thread->window)->steps->update(config.steps);
316 ((RadialBlurWindow*)thread->window)->r->update(config.r);
317 ((RadialBlurWindow*)thread->window)->g->update(config.g);
318 ((RadialBlurWindow*)thread->window)->b->update(config.b);
319 ((RadialBlurWindow*)thread->window)->a->update(config.a);
320 thread->window->unlock_window();
327 void RadialBlurMain::save_data(KeyFrame *keyframe)
331 // cause data to be stored directly in text
332 output.set_shared_output(keyframe->xbuf);
333 output.tag.set_title("RADIALBLUR");
335 output.tag.set_property("X", config.x);
336 output.tag.set_property("Y", config.y);
337 output.tag.set_property("ANGLE", config.angle);
338 output.tag.set_property("STEPS", config.steps);
339 output.tag.set_property("R", config.r);
340 output.tag.set_property("G", config.g);
341 output.tag.set_property("B", config.b);
342 output.tag.set_property("A", config.a);
344 output.tag.set_title("/RADIALBLUR");
346 output.append_newline();
347 output.terminate_string();
350 void RadialBlurMain::read_data(KeyFrame *keyframe)
354 input.set_shared_input(keyframe->xbuf);
360 result = input.read_tag();
364 if(input.tag.title_is("RADIALBLUR"))
366 config.x = input.tag.get_property("X", config.x);
367 config.y = input.tag.get_property("Y", config.y);
368 config.angle = input.tag.get_property("ANGLE", config.angle);
369 config.steps = input.tag.get_property("STEPS", config.steps);
370 config.r = input.tag.get_property("R", config.r);
371 config.g = input.tag.get_property("G", config.g);
372 config.b = input.tag.get_property("B", config.b);
373 config.a = input.tag.get_property("A", config.a);
379 int RadialBlurMain::handle_opengl()
382 get_output()->to_texture();
383 get_output()->enable_opengl();
384 get_output()->init_screen();
385 get_output()->bind_texture(0);
388 //int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
389 glClearColor(0.0, 0.0, 0.0, 0.0);
390 glClear(GL_COLOR_BUFFER_BIT);
392 // Draw unselected channels
394 glBlendFunc(GL_ONE, GL_ONE);
395 glDrawBuffer(GL_BACK);
396 if(!config.r || !config.g || !config.b || !config.a)
398 glColor4f(config.r ? 0 : 1,
402 get_output()->draw_texture();
404 glAccum(GL_LOAD, 1.0);
407 // Blur selected channels
408 float fraction = 1.0 / config.steps;
409 for(int i = 0; i < config.steps; i++)
411 get_output()->set_opengl_state(VFrame::TEXTURE);
412 glClear(GL_COLOR_BUFFER_BIT);
413 glColor4f(config.r ? 1 : 0,
418 float w = get_output()->get_w();
419 float h = get_output()->get_h();
423 double current_angle = (double)config.angle *
426 (double)config.angle / 2;
428 if(!rotate) rotate = new AffineEngine(PluginClient::smp + 1,
429 PluginClient::smp + 1);
430 rotate->set_in_pivot((int)(config.x * w / 100),
431 (int)(config.y * h / 100));
432 rotate->set_out_pivot((int)(config.x * w / 100),
433 (int)(config.y * h / 100));
434 rotate->set_opengl(1);
435 rotate->rotate(get_output(),
439 glAccum(GL_ACCUM, fraction);
440 glEnable(GL_TEXTURE_2D);
441 glColor4f(config.r ? 1 : 0,
449 glReadBuffer(GL_BACK);
450 glDisable(GL_TEXTURE_2D);
451 glAccum(GL_RETURN, 1.0);
453 glColor4f(1, 1, 1, 1);
454 get_output()->set_opengl_state(VFrame::SCREEN);
469 RadialBlurPackage::RadialBlurPackage()
475 RadialBlurUnit::RadialBlurUnit(RadialBlurEngine *server,
476 RadialBlurMain *plugin)
479 this->plugin = plugin;
480 this->server = server;
484 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP_TYPE, MAX, DO_YUV) \
486 int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
487 TYPE **in_rows = (TYPE**)plugin->input->get_rows(); \
488 TYPE **out_rows = (TYPE**)plugin->output->get_rows(); \
489 int steps = plugin->config.steps; \
490 double step = (double)plugin->config.angle / 360 * 2 * M_PI / steps; \
492 for(int i = pkg->y1, out_y = pkg->y1 - center_y; \
496 TYPE *out_row = out_rows[i]; \
497 TYPE *in_row = in_rows[i]; \
498 int y_square = out_y * out_y; \
500 for(int j = 0, out_x = -center_x; j < w; j++, out_x++) \
502 TEMP_TYPE accum_r = 0; \
503 TEMP_TYPE accum_g = 0; \
504 TEMP_TYPE accum_b = 0; \
505 TEMP_TYPE accum_a = 0; \
507 /* Output coordinate to polar */ \
508 double magnitude = sqrt(y_square + out_x * out_x); \
511 angle = atan((double)out_x / out_y) + M_PI; \
514 angle = atan((double)out_x / out_y); \
519 angle = M_PI * 1.5; \
521 /* Overlay all steps on this pixel*/ \
522 angle -= (double)plugin->config.angle / 360 * M_PI; \
523 for(int k = 0; k < steps; k++, angle += step) \
525 /* Polar to input coordinate */ \
526 int in_x = (int)(magnitude * sin(angle)) + center_x; \
527 int in_y = (int)(magnitude * cos(angle)) + center_y; \
529 /* Accumulate input coordinate */ \
530 if(in_x >= 0 && in_x < w && in_y >= 0 && in_y < h) \
532 accum_r += in_rows[in_y][in_x * COMPONENTS]; \
535 accum_g += (int)in_rows[in_y][in_x * COMPONENTS + 1]; \
536 accum_b += (int)in_rows[in_y][in_x * COMPONENTS + 2]; \
540 accum_g += in_rows[in_y][in_x * COMPONENTS + 1]; \
541 accum_b += in_rows[in_y][in_x * COMPONENTS + 2]; \
543 if(COMPONENTS == 4) \
544 accum_a += in_rows[in_y][in_x * COMPONENTS + 3]; \
548 accum_g += chroma_offset; \
549 accum_b += chroma_offset; \
553 /* Accumulation to output */ \
556 *out_row++ = (accum_r * fraction) / 0x10000; \
561 *out_row++ = *in_row++; \
567 *out_row++ = ((accum_g * fraction) / 0x10000); \
569 *out_row++ = (accum_g * fraction) / 0x10000; \
574 *out_row++ = *in_row++; \
580 *out_row++ = (accum_b * fraction) / 0x10000; \
582 *out_row++ = (accum_b * fraction) / 0x10000; \
587 *out_row++ = *in_row++; \
590 if(COMPONENTS == 4) \
594 *out_row++ = (accum_a * fraction) / 0x10000; \
599 *out_row++ = *in_row++; \
606 void RadialBlurUnit::process_package(LoadPackage *package)
608 RadialBlurPackage *pkg = (RadialBlurPackage*)package;
609 int h = plugin->output->get_h();
610 int w = plugin->output->get_w();
611 int do_r = plugin->config.r;
612 int do_g = plugin->config.g;
613 int do_b = plugin->config.b;
614 int do_a = plugin->config.a;
615 int fraction = 0x10000 / plugin->config.steps;
616 int center_x = plugin->config.x * w / 100;
617 int center_y = plugin->config.y * h / 100;
619 switch(plugin->input->get_color_model())
622 BLEND_LAYER(3, uint8_t, int, 0xff, 0)
625 BLEND_LAYER(4, uint8_t, int, 0xff, 0)
628 BLEND_LAYER(3, float, float, 1, 0)
631 BLEND_LAYER(4, float, float, 1, 0)
634 BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
636 case BC_RGBA16161616:
637 BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
640 BLEND_LAYER(3, uint8_t, int, 0xff, 1)
643 BLEND_LAYER(4, uint8_t, int, 0xff, 1)
646 BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
648 case BC_YUVA16161616:
649 BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
659 RadialBlurEngine::RadialBlurEngine(RadialBlurMain *plugin,
662 : LoadServer(total_clients, total_packages)
663 // : LoadServer(1, 1)
665 this->plugin = plugin;
668 void RadialBlurEngine::init_packages()
670 for(int i = 0; i < get_total_packages(); i++)
672 RadialBlurPackage *package = (RadialBlurPackage*)get_package(i);
673 package->y1 = plugin->output->get_h() * i / get_total_packages();
674 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
678 LoadClient* RadialBlurEngine::new_client()
680 return new RadialBlurUnit(this, plugin);
683 LoadPackage* RadialBlurEngine::new_package()
685 return new RadialBlurPackage;