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"
34 #include "overlayframe.h"
38 REGISTER_PLUGIN(GradientMain)
40 GradientConfig::GradientConfig()
45 void GradientConfig::reset()
50 in_r = in_g = in_b = in_a = 0xff;
51 out_r = out_g = out_b = out_a = 0x00;
52 shape = GradientConfig::LINEAR;
53 rate = GradientConfig::LINEAR;
58 int GradientConfig::equivalent(GradientConfig &that)
60 return (EQUIV(angle, that.angle) &&
61 EQUIV(in_radius, that.in_radius) &&
62 EQUIV(out_radius, that.out_radius) &&
67 out_r == that.out_r &&
68 out_g == that.out_g &&
69 out_b == that.out_b &&
70 out_a == that.out_a &&
71 shape == that.shape &&
73 EQUIV(center_x, that.center_x) &&
74 EQUIV(center_y, that.center_y));
77 void GradientConfig::copy_from(GradientConfig &that)
80 in_radius = that.in_radius;
81 out_radius = that.out_radius;
92 center_x = that.center_x;
93 center_y = that.center_y;
96 void GradientConfig::interpolate(GradientConfig &prev, GradientConfig &next,
97 long prev_frame, long next_frame, long current_frame)
99 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
100 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
102 this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale);
103 this->in_radius = (int)(prev.in_radius * prev_scale + next.in_radius * next_scale);
104 this->out_radius = (int)(prev.out_radius * prev_scale + next.out_radius * next_scale);
105 in_r = (int)(prev.in_r * prev_scale + next.in_r * next_scale);
106 in_g = (int)(prev.in_g * prev_scale + next.in_g * next_scale);
107 in_b = (int)(prev.in_b * prev_scale + next.in_b * next_scale);
108 in_a = (int)(prev.in_a * prev_scale + next.in_a * next_scale);
109 out_r = (int)(prev.out_r * prev_scale + next.out_r * next_scale);
110 out_g = (int)(prev.out_g * prev_scale + next.out_g * next_scale);
111 out_b = (int)(prev.out_b * prev_scale + next.out_b * next_scale);
112 out_a = (int)(prev.out_a * prev_scale + next.out_a * next_scale);
115 center_x = prev.center_x * prev_scale + next.center_x * next_scale;
116 center_y = prev.center_y * prev_scale + next.center_y * next_scale;
119 int GradientConfig::get_in_color()
121 int result = (in_r << 16) | (in_g << 8) | (in_b);
125 int GradientConfig::get_out_color()
127 int result = (out_r << 16) | (out_g << 8) | (out_b);
131 #define COLOR_W xS(100)
132 #define COLOR_H yS(30)
134 GradientWindow::GradientWindow(GradientMain *plugin)
135 : PluginClientWindow(plugin, xS(350), yS(290), xS(350), yS(290), 0)
137 this->plugin = plugin;
145 GradientWindow::~GradientWindow()
150 void GradientWindow::create_objects()
154 int margin = plugin->get_theme()->widget_border;
155 int x = xs10, y = ys10;
158 add_subwindow(title = new BC_Title(x, y, _("Shape:")));
159 add_subwindow(shape = new GradientShape(plugin, this,
160 x + title->get_w() + margin, y));
161 shape->create_objects();
162 y += shape->get_h() + margin;
165 y += BC_Pot::calculate_h() + margin;
167 add_subwindow(title = new BC_Title(x, y, _("Rate:")));
168 add_subwindow(rate = new GradientRate(plugin,
169 x + title->get_w() + margin, y));
170 rate->create_objects();
171 y += rate->get_h() + 3*margin;
175 add_subwindow(title1 = new BC_Title(x, y, _("Inner radius:")));
176 y += BC_Slider::get_span(0) + margin;
178 add_subwindow(title2 = new BC_Title(x, y, _("Outer radius:")));
181 x += MAX(title1->get_w(), title2->get_w()) + margin;
182 add_subwindow(in_radius = new GradientInRadius(plugin, x, y));
183 y += in_radius->get_h() + margin;
184 add_subwindow(out_radius = new GradientOutRadius(plugin, x, y));
185 y += out_radius->get_h() + 3*margin;
188 add_subwindow(title1 = new BC_Title(x, y, _("Inner Color:")));
189 y1 = y + COLOR_H+4 + 2*margin;
190 add_subwindow(title2 = new BC_Title(x, y1, _("Outer Color:")));
191 int x2 = x + MAX(title1->get_w(), title2->get_w()) + margin;
192 int in_rgb = plugin->config.get_in_color();
193 int in_a = plugin->config.in_a;
194 add_subwindow(in_color = new GradientInColorButton(plugin, this, x2+2, y+2, in_rgb, in_a));
195 draw_3d_border(x2,y, COLOR_W+4,COLOR_H+4, 1);
196 in_color->create_objects();
198 int out_rgb = plugin->config.get_out_color();
199 int out_a = plugin->config.out_a;
200 add_subwindow(out_color = new GradientOutColorButton(plugin, this, x2+2, y1+2, out_rgb, out_a));
201 draw_3d_border(x2,y1, COLOR_W+4,COLOR_H+4, 1);
202 out_color->create_objects();
203 y = y1 + COLOR_H+4 + 3*margin;
205 add_subwindow(reset = new GradientReset(plugin, this, x, y));
210 void GradientWindow::update_shape()
212 int x = shape_x, y = shape_y;
214 if( plugin->config.shape == GradientConfig::LINEAR ) {
215 delete center_x_title; center_x_title = 0;
216 delete center_y_title; center_y_title = 0;
217 delete center_x; center_x = 0;
218 delete center_y; center_y = 0;
220 add_subwindow(angle_title = new BC_Title(x, y, _("Angle:")));
221 add_subwindow(angle = new GradientAngle(plugin, x + angle_title->get_w() + xS(10), y));
225 delete angle_title; angle_title = 0;
226 delete angle; angle = 0;
228 add_subwindow(center_x_title = new BC_Title(x, y, _("Center X:")));
229 add_subwindow(center_x = new GradientCenterX(plugin,
230 x + center_x_title->get_w() + xS(10), y));
231 x += center_x_title->get_w() + xS(10) + center_x->get_w() + xS(10);
232 add_subwindow(center_y_title = new BC_Title(x, y, _("Center Y:")));
233 add_subwindow(center_y = new GradientCenterY(plugin,
234 x + center_y_title->get_w() + xS(10), y));
240 void GradientWindow::done_event(int result)
242 in_color->close_picker();
243 out_color->close_picker();
246 GradientShape::GradientShape(GradientMain *plugin, GradientWindow *gui, int x, int y)
247 : BC_PopupMenu(x, y, xS(100), to_text(plugin->config.shape), 1)
249 this->plugin = plugin;
252 void GradientShape::create_objects()
254 add_item(new BC_MenuItem(to_text(GradientConfig::LINEAR)));
255 add_item(new BC_MenuItem(to_text(GradientConfig::RADIAL)));
257 char* GradientShape::to_text(int shape)
260 case GradientConfig::LINEAR: return _("Linear");
264 int GradientShape::from_text(char *text)
266 if( !strcmp(text, to_text(GradientConfig::LINEAR)) )
267 return GradientConfig::LINEAR;
268 return GradientConfig::RADIAL;
270 int GradientShape::handle_event()
272 plugin->config.shape = from_text(get_text());
274 plugin->send_configure_change();
279 GradientCenterX::GradientCenterX(GradientMain *plugin, int x, int y)
280 : BC_FPot(x, y, plugin->config.center_x, 0, xS(100))
282 this->plugin = plugin;
284 int GradientCenterX::handle_event()
286 plugin->config.center_x = get_value();
287 plugin->send_configure_change();
292 GradientCenterY::GradientCenterY(GradientMain *plugin, int x, int y)
293 : BC_FPot(x, y, plugin->config.center_y, 0, xS(100))
295 this->plugin = plugin;
298 int GradientCenterY::handle_event()
300 plugin->config.center_y = get_value();
301 plugin->send_configure_change();
306 GradientAngle::GradientAngle(GradientMain *plugin, int x, int y)
307 : BC_FPot(x, y, plugin->config.angle, -180, 180)
309 this->plugin = plugin;
312 int GradientAngle::handle_event()
314 plugin->config.angle = get_value();
315 plugin->send_configure_change();
320 GradientRate::GradientRate(GradientMain *plugin, int x, int y)
321 : BC_PopupMenu(x, y, xS(100), to_text(plugin->config.rate), 1)
323 this->plugin = plugin;
325 void GradientRate::create_objects()
327 add_item(new BC_MenuItem(to_text(GradientConfig::LINEAR)));
328 add_item(new BC_MenuItem(to_text(GradientConfig::LOG)));
329 add_item(new BC_MenuItem(to_text(GradientConfig::SQUARE)));
331 char* GradientRate::to_text(int shape)
334 case GradientConfig::LINEAR: return _("Linear");
335 case GradientConfig::LOG: return _("Log");
339 int GradientRate::from_text(char *text)
341 if( !strcmp(text, to_text(GradientConfig::LINEAR)) )
342 return GradientConfig::LINEAR;
343 if( !strcmp(text, to_text(GradientConfig::LOG)) )
344 return GradientConfig::LOG;
345 return GradientConfig::SQUARE;
347 int GradientRate::handle_event()
349 plugin->config.rate = from_text(get_text());
350 plugin->send_configure_change();
355 GradientInRadius::GradientInRadius(GradientMain *plugin, int x, int y)
356 : BC_FSlider(x, y, 0, xS(200), yS(200),
357 0.f, 100.f, (float)plugin->config.in_radius)
359 this->plugin = plugin;
362 int GradientInRadius::handle_event()
364 plugin->config.in_radius = get_value();
365 plugin->send_configure_change();
370 GradientOutRadius::GradientOutRadius(GradientMain *plugin, int x, int y)
371 : BC_FSlider(x, y, 0, xS(200), yS(200),
372 0.f, 100.f, (float)plugin->config.out_radius)
374 this->plugin = plugin;
377 int GradientOutRadius::handle_event()
379 plugin->config.out_radius = get_value();
380 plugin->send_configure_change();
385 GradientInColorButton::GradientInColorButton(GradientMain *plugin, GradientWindow *gui,
386 int x, int y, int color, int alpha)
387 : ColorBoxButton(_("Inner color:"), x, y, COLOR_W, COLOR_H, color, alpha, 1)
389 this->plugin = plugin;
393 GradientInColorButton::~GradientInColorButton()
397 void GradientInColorButton::handle_done_event(int result)
400 gui->lock_window("GradientInColorButton::handle_done_event");
401 update_gui(orig_color, orig_alpha);
402 gui->unlock_window();
403 handle_new_color(orig_color, orig_alpha);
407 int GradientInColorButton::handle_new_color(int color, int alpha)
409 plugin->config.in_r = (color & 0xff0000) >> 16;
410 plugin->config.in_g = (color & 0xff00) >> 8;
411 plugin->config.in_b = (color & 0xff);
412 plugin->config.in_a = alpha;
413 plugin->send_configure_change();
417 GradientOutColorButton::GradientOutColorButton(GradientMain *plugin, GradientWindow *gui,
418 int x, int y, int color, int alpha)
419 : ColorBoxButton(_("Outer color:"), x, y, COLOR_W, COLOR_H, color, alpha, 1)
421 this->plugin = plugin;
425 GradientOutColorButton::~GradientOutColorButton()
429 void GradientOutColorButton::handle_done_event(int result)
432 gui->lock_window("GradientOutColorButton::handle_done_event");
433 update_gui(orig_color, orig_alpha);
434 gui->unlock_window();
435 handle_new_color(orig_color, orig_alpha);
439 int GradientOutColorButton::handle_new_color(int color, int alpha)
441 plugin->config.out_r = (color & 0xff0000) >> 16;
442 plugin->config.out_g = (color & 0xff00) >> 8;
443 plugin->config.out_b = (color & 0xff);
444 plugin->config.out_a = alpha;
445 plugin->send_configure_change();
450 GradientReset::GradientReset(GradientMain *plugin, GradientWindow *window, int x, int y)
451 : BC_GenericButton(x, y, _("Reset"))
453 this->plugin = plugin;
454 this->window = window;
457 int GradientReset::handle_event()
459 plugin->config.reset();
460 window->update_gui();
461 plugin->send_configure_change();
466 GradientMain::GradientMain(PluginServer *server)
467 : PluginVClient(server)
470 need_reconfigure = 1;
478 GradientMain::~GradientMain()
486 const char* GradientMain::plugin_title() { return N_("Gradient"); }
487 int GradientMain::is_realtime() { return 1; }
490 NEW_WINDOW_MACRO(GradientMain, GradientWindow)
492 LOAD_CONFIGURATION_MACRO(GradientMain, GradientConfig)
494 int GradientMain::is_synthesis()
500 int GradientMain::process_buffer(VFrame *frame,
501 int64_t start_position,
505 this->output = frame;
506 float fw = input->get_w(), fh = input->get_h();
507 gradient_size = hypotf(fw, fh);
508 need_reconfigure = load_configuration();
510 int need_alpha = config.in_a != 0xff || config.out_a != 0xff;
512 read_frame(frame, 0, start_position, frame_rate, get_use_opengl());
513 if( get_use_opengl() ) return run_opengl();
515 int gradient_cmodel = input->get_color_model();
516 if( need_alpha && BC_CModels::components(gradient_cmodel) == 3 ) {
517 switch( gradient_cmodel ) {
518 case BC_RGB888: gradient_cmodel = BC_RGBA8888; break;
519 case BC_YUV888: gradient_cmodel = BC_YUVA8888; break;
520 case BC_RGB_FLOAT: gradient_cmodel = BC_RGBA_FLOAT; break;
524 int bpp = BC_CModels::calculate_pixelsize(gradient_cmodel);
525 int comps = BC_CModels::components(gradient_cmodel);
526 int grad_size1 = gradient_size + 1;
527 int sz = 4 * (bpp / comps) * grad_size1;
528 if( table_size < sz ) {
529 delete [] table; table = 0;
532 table = new uint8_t[table_size = sz];
533 need_reconfigure = 1;
536 if( gradient && gradient->get_color_model() != gradient_cmodel ) {
542 gradient = new VFrame(input->get_w(), input->get_h(),
546 engine = new GradientServer(this,
547 get_project_smp() + 1, get_project_smp() + 1);
548 engine->process_packages();
550 // Use overlay routine in GradientServer if mismatched colormodels
551 if( gradient->get_color_model() == output->get_color_model() ) {
552 if( !overlayer ) overlayer = new OverlayFrame(get_project_smp() + 1);
553 overlayer->overlay(output, gradient,
554 0, 0, input->get_w(), input->get_h(),
555 0, 0, input->get_w(), input->get_h(),
556 1.0, TRANSFER_NORMAL, NEAREST_NEIGHBOR);
563 void GradientMain::update_gui()
565 if( !thread ) return;
566 if( !load_configuration() ) return;
567 thread->window->lock_window("GradientMain::update_gui");
568 if( load_configuration() ) {
569 GradientWindow *window = (GradientWindow *)thread->window;
570 window->update_gui();
573 thread->window->unlock_window();
576 void GradientWindow::update_gui()
578 GradientConfig &config = plugin->config;
579 rate->set_text(GradientRate::to_text(config.rate));
580 in_radius->update(config.in_radius);
581 out_radius->update(config.out_radius);
582 shape->set_text(GradientShape::to_text(config.shape));
583 if( angle ) angle->update(config.angle);
584 if( center_x ) center_x->update(config.center_x);
585 if( center_y ) center_y->update(config.center_y);
587 in_color->update_gui(config.get_in_color(), config.in_a);
588 out_color->update_gui(config.get_out_color(), config.out_a);
592 void GradientMain::save_data(KeyFrame *keyframe)
596 // cause data to be stored directly in text
597 output.set_shared_output(keyframe->xbuf);
598 output.tag.set_title("GRADIENT");
600 output.tag.set_property("ANGLE", config.angle);
601 output.tag.set_property("IN_RADIUS", config.in_radius);
602 output.tag.set_property("OUT_RADIUS", config.out_radius);
603 output.tag.set_property("IN_R", config.in_r);
604 output.tag.set_property("IN_G", config.in_g);
605 output.tag.set_property("IN_B", config.in_b);
606 output.tag.set_property("IN_A", config.in_a);
607 output.tag.set_property("OUT_R", config.out_r);
608 output.tag.set_property("OUT_G", config.out_g);
609 output.tag.set_property("OUT_B", config.out_b);
610 output.tag.set_property("OUT_A", config.out_a);
611 output.tag.set_property("SHAPE", config.shape);
612 output.tag.set_property("RATE", config.rate);
613 output.tag.set_property("CENTER_X", config.center_x);
614 output.tag.set_property("CENTER_Y", config.center_y);
616 output.tag.set_title("/GRADIENT");
618 output.append_newline();
619 output.terminate_string();
622 void GradientMain::read_data(KeyFrame *keyframe)
626 input.set_shared_input(keyframe->xbuf);
630 while( !(result = input.read_tag()) ) {
631 if( input.tag.title_is("GRADIENT") ) {
632 config.angle = input.tag.get_property("ANGLE", config.angle);
633 config.rate = input.tag.get_property("RATE", config.rate);
634 config.in_radius = input.tag.get_property("IN_RADIUS", config.in_radius);
635 config.out_radius = input.tag.get_property("OUT_RADIUS", config.out_radius);
636 config.in_r = input.tag.get_property("IN_R", config.in_r);
637 config.in_g = input.tag.get_property("IN_G", config.in_g);
638 config.in_b = input.tag.get_property("IN_B", config.in_b);
639 config.in_a = input.tag.get_property("IN_A", config.in_a);
640 config.out_r = input.tag.get_property("OUT_R", config.out_r);
641 config.out_g = input.tag.get_property("OUT_G", config.out_g);
642 config.out_b = input.tag.get_property("OUT_B", config.out_b);
643 config.out_a = input.tag.get_property("OUT_A", config.out_a);
644 config.shape = input.tag.get_property("SHAPE", config.shape);
645 config.center_x = input.tag.get_property("CENTER_X", config.center_x);
646 config.center_y = input.tag.get_property("CENTER_Y", config.center_y);
651 int GradientMain::handle_opengl()
654 const char *head_frag =
655 "uniform sampler2D tex;\n"
656 "uniform vec2 tex_dimensions;\n"
657 "uniform vec2 center;\n"
658 "uniform vec2 angle;\n"
659 "uniform float gradient_size;\n"
660 "uniform vec4 out_color;\n"
661 "uniform vec4 in_color;\n"
662 "uniform float in_radius;\n"
663 "uniform float out_radius;\n"
664 "uniform float radius_diff;\n"
668 " vec2 out_coord = gl_TexCoord[0].st;\n"
669 " vec2 in_coord = out_coord * tex_dimensions - center;\n";
671 const char *linear_shape =
672 " float mag = 0.5 + dot(in_coord,angle)/gradient_size;\n";
674 const char *radial_shape =
675 " float mag = length(in_coord)/gradient_size;\n";
677 // No clamp function in NVidia
678 const char *linear_rate =
679 " mag = min(max(mag, in_radius), out_radius);\n"
680 " float opacity = (mag - in_radius) / radius_diff;\n";
682 // NVidia warns about exp, but exp is in the GLSL spec.
683 const char *log_rate =
684 " mag = max(mag, in_radius);\n"
685 " float opacity = 1.0 - \n"
686 " exp(1.0 * -(mag - in_radius) / radius_diff);\n";
688 const char *square_rate =
689 " mag = min(max(mag, in_radius), out_radius);\n"
690 " float opacity = pow((mag - in_radius) / radius_diff, 2.0);\n"
691 " opacity = min(opacity, 1.0);\n";
693 const char *tail_frag =
694 " vec4 color = mix(in_color, out_color, opacity);\n"
695 " vec4 bg_color = texture2D(tex, out_coord);\n"
696 " gl_FragColor.rgb = mix(bg_color.rgb, color.rgb, color.a);\n"
697 " gl_FragColor.a = mix(bg_color.a, 1., color.a);\n"
701 const char *shader_stack[16];
702 memset(shader_stack,0, sizeof(shader_stack));
703 int current_shader = 0;
705 shader_stack[current_shader++] = head_frag;
707 const char *shape_frag = 0;
708 switch( config.shape ) {
709 case GradientConfig::LINEAR:
710 shape_frag = linear_shape;
713 shape_frag = radial_shape;
717 shader_stack[current_shader++] = shape_frag;
719 const char *rate_frag = 0;
720 switch( config.rate ) {
721 case GradientConfig::LINEAR:
722 rate_frag = linear_rate;
724 case GradientConfig::LOG:
725 rate_frag = log_rate;
727 case GradientConfig::SQUARE:
728 rate_frag = square_rate;
732 shader_stack[current_shader++] = rate_frag;
734 shader_stack[current_shader++] = tail_frag;
736 // Force frame to create texture without copying to it if full alpha.
737 if( config.in_a >= 0xff && config.out_a >= 0xff )
738 get_output()->set_opengl_state(VFrame::TEXTURE);
739 get_output()->to_texture();
740 get_output()->enable_opengl();
741 get_output()->init_screen();
742 get_output()->bind_texture(0);
744 shader_stack[current_shader] = 0;
745 unsigned int shader = VFrame::make_shader(shader_stack);
747 glUseProgram(shader);
748 float w = get_output()->get_w();
749 float h = get_output()->get_h();
750 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
751 float texture_w = get_output()->get_texture_w();
752 float texture_h = get_output()->get_texture_h();
753 glUniform2f(glGetUniformLocation(shader, "tex_dimensions"),
754 texture_w, texture_h);
755 float u = config.shape == GradientConfig::LINEAR ?
756 0.5f : config.center_x/100.f;
757 float v = config.shape == GradientConfig::LINEAR ?
758 0.5f : config.center_y/100.f;
759 glUniform2f(glGetUniformLocation(shader, "center"),
761 glUniform1f(glGetUniformLocation(shader, "gradient_size"),
763 glUniform2f(glGetUniformLocation(shader, "angle"),
764 -sin(config.angle * (M_PI / 180)), cos(config.angle * (M_PI / 180)));
766 float in_radius = (float)config.in_radius/100;
767 float out_radius = (float)config.out_radius/100;
768 if( in_radius > out_radius ) {
770 in_radius = out_radius;
773 glUniform1f(glGetUniformLocation(shader, "in_radius"), in_radius);
774 glUniform1f(glGetUniformLocation(shader, "out_radius"), out_radius);
775 glUniform1f(glGetUniformLocation(shader, "radius_diff"),
776 out_radius - in_radius);
778 float in_r = (float)config.in_r / 0xff;
779 float in_g = (float)config.in_g / 0xff;
780 float in_b = (float)config.in_b / 0xff;
781 float in_a = (float)config.in_a / 0xff;
782 float out_r = (float)config.out_r / 0xff;
783 float out_g = (float)config.out_g / 0xff;
784 float out_b = (float)config.out_b / 0xff;
785 float out_a = (float)config.out_a / 0xff;
786 switch( get_output()->get_color_model() ) {
789 float in1, in2, in3, in4;
790 float out1, out2, out3, out4;
791 YUV::yuv.rgb_to_yuv_f(in_r,in_g,in_b, in1,in2,in3);
792 in2 += 0.5; in3 += 0.5; in4 = in_a;
793 YUV::yuv.rgb_to_yuv_f(out_r,out_g,out_b, out1,out2,out3);
794 out2 += 0.5; out3 += 0.5; out4 = out_a;
795 glUniform4f(glGetUniformLocation(shader, "out_color"),
796 out1, out2, out3, out4);
797 glUniform4f(glGetUniformLocation(shader, "in_color"),
802 glUniform4f(glGetUniformLocation(shader, "out_color"),
803 out_r, out_g, out_b, out_a);
804 glUniform4f(glGetUniformLocation(shader, "in_color"),
805 in_r, in_g, in_b, in_a);
810 get_output()->draw_texture();
812 get_output()->set_opengl_state(VFrame::SCREEN);
819 GradientPackage::GradientPackage()
824 GradientUnit::GradientUnit(GradientServer *server, GradientMain *plugin)
827 this->plugin = plugin;
828 this->server = server;
832 static float calculate_opacity(float mag,
833 float in_radius, float out_radius, int rate)
835 float mag_diff = mag - in_radius;
836 float radius_diff = out_radius - in_radius;
837 if( !radius_diff ) return 1.0;
840 case GradientConfig::LINEAR:
841 opacity = mag < in_radius ? 0.0 : mag >= out_radius ? 1.0 :
842 mag_diff / radius_diff;
844 case GradientConfig::LOG:
845 opacity = mag < in_radius ? 0.0 : // Let this one decay beyond out_radius
846 1 - exp(1.0 * -mag_diff / radius_diff);
848 case GradientConfig::SQUARE:
849 opacity = mag < in_radius ? 0.0 : mag >= out_radius ? 1.0 :
850 powf(mag_diff / radius_diff, 2.);
856 #define CREATE_GRADIENT(type, temp, components, max) { \
857 /* Synthesize linear gradient for lookups */ \
858 int grad_size = plugin->gradient_size; \
859 int n = sizeof(type) * (grad_size+1); \
860 void *r_table = (void*) (plugin->table + 0*n); \
861 void *g_table = (void*) (plugin->table + 1*n); \
862 void *b_table = (void*) (plugin->table + 2*n); \
863 void *a_table = (void*) (plugin->table + 3*n); \
864 double grad_size2 = grad_size / 2.; \
866 if( plugin->need_reconfigure ) { \
867 for( int i = 0; i <= grad_size; i++ ) { \
868 float opacity = calculate_opacity(i, in_radius, out_radius, plugin->config.rate); \
869 float transparency = 1.0 - opacity; \
870 ((type*)r_table)[i] = (type)(out1 * opacity + in1 * transparency); \
871 ((type*)g_table)[i] = (type)(out2 * opacity + in2 * transparency); \
872 ((type*)b_table)[i] = (type)(out3 * opacity + in3 * transparency); \
873 ((type*)a_table)[i] = (type)(out4 * opacity + in4 * transparency); \
877 for( int i = pkg->y1; i < pkg->y2; i++ ) { \
878 double y = half_h - i; \
879 type *gradient_row = (type*)plugin->gradient->get_rows()[i]; \
880 type *out_row = (type*)plugin->get_output()->get_rows()[i]; \
881 switch( plugin->config.shape ) { \
882 case GradientConfig::LINEAR: \
883 for( int j = 0; j < w; j++ ) { \
884 double x = j - half_w; \
885 /* Rotate by effect angle */ \
886 int mag = (grad_size2 - (x * sin_angle + y * cos_angle)) + 0.5; \
887 /* Get gradient value from these coords */ \
888 if( sizeof(type) == 4 ) { \
889 float opacity = calculate_opacity(mag, \
890 in_radius, out_radius, plugin->config.rate); \
891 float transparency = 1.0 - opacity; \
892 gradient_row[0] = (type)(out1 * opacity + in1 * transparency); \
893 gradient_row[1] = (type)(out2 * opacity + in2 * transparency); \
894 gradient_row[2] = (type)(out3 * opacity + in3 * transparency); \
895 if( components == 4 ) \
896 gradient_row[3] = (type)(out4 * opacity + in4 * transparency); \
898 else if( mag < 0 ) { \
899 gradient_row[0] = in1; \
900 gradient_row[1] = in2; \
901 gradient_row[2] = in3; \
902 if( components == 4 ) \
903 gradient_row[3] = in4; \
905 else if( mag >= grad_size ) { \
906 gradient_row[0] = out1; \
907 gradient_row[1] = out2; \
908 gradient_row[2] = out3; \
909 if( components == 4 ) \
910 gradient_row[3] = out4; \
913 gradient_row[0] = ((type*)r_table)[mag]; \
914 gradient_row[1] = ((type*)g_table)[mag]; \
915 gradient_row[2] = ((type*)b_table)[mag]; \
916 if( components == 4 ) \
917 gradient_row[3] = ((type*)a_table)[mag]; \
919 /* no alpha output, Overlay mixed colormodels */ \
920 if( gradient_cmodel != output_cmodel ) { \
921 temp opacity = gradient_row[3]; \
922 temp transparency = max - opacity; \
923 out_row[0] = (transparency * out_row[0] + opacity * gradient_row[0]) / max; \
924 out_row[1] = (transparency * out_row[1] + opacity * gradient_row[1]) / max; \
925 out_row[2] = (transparency * out_row[2] + opacity * gradient_row[2]) / max; \
928 gradient_row += components; \
932 case GradientConfig::RADIAL: \
933 for( int j = 0; j < w; j++ ) { \
934 double x = j - center_x, y = i - center_y; \
935 double magnitude = hypot(x, y); \
936 int mag = (int)magnitude; \
937 if( sizeof(type) == 4 ) { \
938 float opacity = calculate_opacity(mag, \
939 in_radius, out_radius, plugin->config.rate); \
940 float transparency = 1.0 - opacity; \
941 gradient_row[0] = (type)(out1 * opacity + in1 * transparency); \
942 gradient_row[1] = (type)(out2 * opacity + in2 * transparency); \
943 gradient_row[2] = (type)(out3 * opacity + in3 * transparency); \
944 if( components == 4 ) \
945 gradient_row[3] = (type)(out4 * opacity + in4 * transparency); \
948 gradient_row[0] = ((type*)r_table)[mag]; \
949 gradient_row[1] = ((type*)g_table)[mag]; \
950 gradient_row[2] = ((type*)b_table)[mag]; \
951 if( components == 4 ) \
952 gradient_row[3] = ((type*)a_table)[mag]; \
954 /* Overlay mixed colormodels onto output */ \
955 if( gradient_cmodel != output_cmodel ) { \
956 temp opacity = gradient_row[3]; \
957 temp transparency = max - opacity; \
958 out_row[0] = (transparency * out_row[0] + opacity * gradient_row[0]) / max; \
959 out_row[1] = (transparency * out_row[1] + opacity * gradient_row[1]) / max; \
960 out_row[2] = (transparency * out_row[2] + opacity * gradient_row[2]) / max; \
963 gradient_row += components; \
970 void GradientUnit::process_package(LoadPackage *package)
972 GradientPackage *pkg = (GradientPackage*)package;
973 int h = plugin->input->get_h();
974 int w = plugin->input->get_w();
975 int grad_size = plugin->gradient_size;
978 int in_radius = (int)(plugin->config.in_radius / 100 * grad_size);
979 int out_radius = (int)(plugin->config.out_radius / 100 * grad_size);
980 double sin_angle = sin(plugin->config.angle * (M_PI / 180));
981 double cos_angle = cos(plugin->config.angle * (M_PI / 180));
982 double center_x = plugin->config.center_x * w / 100;
983 double center_y = plugin->config.center_y * h / 100;
984 int gradient_cmodel = plugin->gradient->get_color_model();
985 int output_cmodel = plugin->get_output()->get_color_model();
987 if( in_radius > out_radius ) {
989 in_radius = out_radius;
993 switch( gradient_cmodel ) {
995 int in1 = plugin->config.in_r;
996 int in2 = plugin->config.in_g;
997 int in3 = plugin->config.in_b;
998 int in4 = plugin->config.in_a;
999 int out1 = plugin->config.out_r;
1000 int out2 = plugin->config.out_g;
1001 int out3 = plugin->config.out_b;
1002 int out4 = plugin->config.out_a;
1003 CREATE_GRADIENT(unsigned char, int, 3, 0xff)
1007 int in1 = plugin->config.in_r;
1008 int in2 = plugin->config.in_g;
1009 int in3 = plugin->config.in_b;
1010 int in4 = plugin->config.in_a;
1011 int out1 = plugin->config.out_r;
1012 int out2 = plugin->config.out_g;
1013 int out3 = plugin->config.out_b;
1014 int out4 = plugin->config.out_a;
1015 CREATE_GRADIENT(unsigned char, int, 4, 0xff)
1018 case BC_RGB_FLOAT: {
1019 float in1 = (float)plugin->config.in_r / 0xff;
1020 float in2 = (float)plugin->config.in_g / 0xff;
1021 float in3 = (float)plugin->config.in_b / 0xff;
1022 float in4 = (float)plugin->config.in_a / 0xff;
1023 float out1 = (float)plugin->config.out_r / 0xff;
1024 float out2 = (float)plugin->config.out_g / 0xff;
1025 float out3 = (float)plugin->config.out_b / 0xff;
1026 float out4 = (float)plugin->config.out_a / 0xff;
1027 CREATE_GRADIENT(float, float, 3, 1.0)
1030 case BC_RGBA_FLOAT: {
1031 float in1 = (float)plugin->config.in_r / 0xff;
1032 float in2 = (float)plugin->config.in_g / 0xff;
1033 float in3 = (float)plugin->config.in_b / 0xff;
1034 float in4 = (float)plugin->config.in_a / 0xff;
1035 float out1 = (float)plugin->config.out_r / 0xff;
1036 float out2 = (float)plugin->config.out_g / 0xff;
1037 float out3 = (float)plugin->config.out_b / 0xff;
1038 float out4 = (float)plugin->config.out_a / 0xff;
1039 CREATE_GRADIENT(float, float, 4, 1.0)
1043 int in_r = plugin->config.in_r;
1044 int in_g = plugin->config.in_g;
1045 int in_b = plugin->config.in_b;
1046 int in1, in2, in3, in4;
1047 int out1, out2, out3, out4;
1048 YUV::yuv.rgb_to_yuv_8(in_r,in_g,in_b, in1,in2,in3);
1049 in4 = plugin->config.in_a;
1050 int out_r = plugin->config.out_r;
1051 int out_g = plugin->config.out_g;
1052 int out_b = plugin->config.out_b;
1053 YUV::yuv.rgb_to_yuv_8(out_r,out_g,out_b, out1,out2,out3);
1054 out4 = plugin->config.out_a;
1055 CREATE_GRADIENT(unsigned char, int, 3, 0xff)
1059 int in_r = plugin->config.in_r;
1060 int in_g = plugin->config.in_g;
1061 int in_b = plugin->config.in_b;
1062 int in1, in2, in3, in4;
1063 int out1, out2, out3, out4;
1064 YUV::yuv.rgb_to_yuv_8(in_r,in_g,in_b, in1,in2,in3);
1065 in4 = plugin->config.in_a;
1066 int out_r = plugin->config.out_r;
1067 int out_g = plugin->config.out_g;
1068 int out_b = plugin->config.out_b;
1069 YUV::yuv.rgb_to_yuv_8(out_r,out_g,out_b, out1,out2,out3);
1070 out4 = plugin->config.out_a;
1071 CREATE_GRADIENT(unsigned char, int, 4, 0xff)
1077 GradientServer::GradientServer(GradientMain *plugin,
1078 int total_clients, int total_packages)
1079 : LoadServer(total_clients, total_packages)
1081 this->plugin = plugin;
1084 void GradientServer::init_packages()
1086 for( int i = 0; i < get_total_packages(); i++ ) {
1087 GradientPackage *package = (GradientPackage*)get_package(i);
1088 package->y1 = plugin->input->get_h() *
1090 get_total_packages();
1091 package->y2 = plugin->input->get_h() *
1093 get_total_packages();
1097 LoadClient* GradientServer::new_client()
1099 return new GradientUnit(this, plugin);
1102 LoadPackage* GradientServer::new_package()
1104 return new GradientPackage;