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);
134 GradientWindow::GradientWindow(GradientMain *plugin)
135 : PluginClientWindow(plugin, 350, 290, 350, 290, 0)
137 this->plugin = plugin;
145 GradientWindow::~GradientWindow()
150 void GradientWindow::create_objects()
152 int margin = plugin->get_theme()->widget_border;
156 add_subwindow(title = new BC_Title(x, y, _("Shape:")));
157 add_subwindow(shape = new GradientShape(plugin, this,
158 x + title->get_w() + margin, y));
159 shape->create_objects();
160 y += shape->get_h() + margin;
163 y += BC_Pot::calculate_h() + margin;
165 add_subwindow(title = new BC_Title(x, y, _("Rate:")));
166 add_subwindow(rate = new GradientRate(plugin,
167 x + title->get_w() + margin, y));
168 rate->create_objects();
169 y += rate->get_h() + 3*margin;
173 add_subwindow(title1 = new BC_Title(x, y, _("Inner radius:")));
174 y += BC_Slider::get_span(0) + margin;
176 add_subwindow(title2 = new BC_Title(x, y, _("Outer radius:")));
179 x += MAX(title1->get_w(), title2->get_w()) + margin;
180 add_subwindow(in_radius = new GradientInRadius(plugin, x, y));
181 y += in_radius->get_h() + margin;
182 add_subwindow(out_radius = new GradientOutRadius(plugin, x, y));
183 y += out_radius->get_h() + 3*margin;
186 add_subwindow(title1 = new BC_Title(x, y, _("Inner Color:")));
187 y1 = y + COLOR_H+4 + 2*margin;
188 add_subwindow(title2 = new BC_Title(x, y1, _("Outer Color:")));
189 int x2 = x + MAX(title1->get_w(), title2->get_w()) + margin;
190 int in_rgb = plugin->config.get_in_color();
191 int in_a = plugin->config.in_a;
192 add_subwindow(in_color = new GradientInColorButton(plugin, this, x2+2, y+2, in_rgb, in_a));
193 draw_3d_border(x2,y, COLOR_W+4,COLOR_H+4, 1);
194 in_color->create_objects();
196 int out_rgb = plugin->config.get_out_color();
197 int out_a = plugin->config.out_a;
198 add_subwindow(out_color = new GradientOutColorButton(plugin, this, x2+2, y1+2, out_rgb, out_a));
199 draw_3d_border(x2,y1, COLOR_W+4,COLOR_H+4, 1);
200 out_color->create_objects();
201 y = y1 + COLOR_H+4 + 3*margin;
203 add_subwindow(reset = new GradientReset(plugin, this, x, y));
208 void GradientWindow::update_shape()
210 int x = shape_x, y = shape_y;
212 if( plugin->config.shape == GradientConfig::LINEAR ) {
213 delete center_x_title; center_x_title = 0;
214 delete center_y_title; center_y_title = 0;
215 delete center_x; center_x = 0;
216 delete center_y; center_y = 0;
218 add_subwindow(angle_title = new BC_Title(x, y, _("Angle:")));
219 add_subwindow(angle = new GradientAngle(plugin, x + angle_title->get_w() + 10, y));
223 delete angle_title; angle_title = 0;
224 delete angle; angle = 0;
226 add_subwindow(center_x_title = new BC_Title(x, y, _("Center X:")));
227 add_subwindow(center_x = new GradientCenterX(plugin,
228 x + center_x_title->get_w() + 10, y));
229 x += center_x_title->get_w() + 10 + center_x->get_w() + 10;
230 add_subwindow(center_y_title = new BC_Title(x, y, _("Center Y:")));
231 add_subwindow(center_y = new GradientCenterY(plugin,
232 x + center_y_title->get_w() + 10, y));
238 void GradientWindow::done_event(int result)
240 in_color->close_picker();
241 out_color->close_picker();
244 GradientShape::GradientShape(GradientMain *plugin, GradientWindow *gui, int x, int y)
245 : BC_PopupMenu(x, y, 100, to_text(plugin->config.shape), 1)
247 this->plugin = plugin;
250 void GradientShape::create_objects()
252 add_item(new BC_MenuItem(to_text(GradientConfig::LINEAR)));
253 add_item(new BC_MenuItem(to_text(GradientConfig::RADIAL)));
255 char* GradientShape::to_text(int shape)
258 case GradientConfig::LINEAR: return _("Linear");
262 int GradientShape::from_text(char *text)
264 if( !strcmp(text, to_text(GradientConfig::LINEAR)) )
265 return GradientConfig::LINEAR;
266 return GradientConfig::RADIAL;
268 int GradientShape::handle_event()
270 plugin->config.shape = from_text(get_text());
272 plugin->send_configure_change();
277 GradientCenterX::GradientCenterX(GradientMain *plugin, int x, int y)
278 : BC_FPot(x, y, plugin->config.center_x, 0, 100)
280 this->plugin = plugin;
282 int GradientCenterX::handle_event()
284 plugin->config.center_x = get_value();
285 plugin->send_configure_change();
290 GradientCenterY::GradientCenterY(GradientMain *plugin, int x, int y)
291 : BC_FPot(x, y, plugin->config.center_y, 0, 100)
293 this->plugin = plugin;
296 int GradientCenterY::handle_event()
298 plugin->config.center_y = get_value();
299 plugin->send_configure_change();
304 GradientAngle::GradientAngle(GradientMain *plugin, int x, int y)
305 : BC_FPot(x, y, plugin->config.angle, -180, 180)
307 this->plugin = plugin;
310 int GradientAngle::handle_event()
312 plugin->config.angle = get_value();
313 plugin->send_configure_change();
318 GradientRate::GradientRate(GradientMain *plugin, int x, int y)
319 : BC_PopupMenu(x, y, 100, to_text(plugin->config.rate), 1)
321 this->plugin = plugin;
323 void GradientRate::create_objects()
325 add_item(new BC_MenuItem(to_text(GradientConfig::LINEAR)));
326 add_item(new BC_MenuItem(to_text(GradientConfig::LOG)));
327 add_item(new BC_MenuItem(to_text(GradientConfig::SQUARE)));
329 char* GradientRate::to_text(int shape)
332 case GradientConfig::LINEAR: return _("Linear");
333 case GradientConfig::LOG: return _("Log");
337 int GradientRate::from_text(char *text)
339 if( !strcmp(text, to_text(GradientConfig::LINEAR)) )
340 return GradientConfig::LINEAR;
341 if( !strcmp(text, to_text(GradientConfig::LOG)) )
342 return GradientConfig::LOG;
343 return GradientConfig::SQUARE;
345 int GradientRate::handle_event()
347 plugin->config.rate = from_text(get_text());
348 plugin->send_configure_change();
353 GradientInRadius::GradientInRadius(GradientMain *plugin, int x, int y)
354 : BC_FSlider(x, y, 0, 200, 200,
355 0.f, 100.f, (float)plugin->config.in_radius)
357 this->plugin = plugin;
360 int GradientInRadius::handle_event()
362 plugin->config.in_radius = get_value();
363 plugin->send_configure_change();
368 GradientOutRadius::GradientOutRadius(GradientMain *plugin, int x, int y)
369 : BC_FSlider(x, y, 0, 200, 200,
370 0.f, 100.f, (float)plugin->config.out_radius)
372 this->plugin = plugin;
375 int GradientOutRadius::handle_event()
377 plugin->config.out_radius = get_value();
378 plugin->send_configure_change();
383 GradientInColorButton::GradientInColorButton(GradientMain *plugin, GradientWindow *gui,
384 int x, int y, int color, int alpha)
385 : ColorBoxButton(_("Inner color:"), x, y, COLOR_W, COLOR_H, color, alpha, 1)
387 this->plugin = plugin;
391 GradientInColorButton::~GradientInColorButton()
395 void GradientInColorButton::handle_done_event(int result)
398 gui->lock_window("GradientInColorButton::handle_done_event");
399 update_gui(orig_color, orig_alpha);
400 gui->unlock_window();
401 handle_new_color(orig_color, orig_alpha);
405 int GradientInColorButton::handle_new_color(int color, int alpha)
407 plugin->config.in_r = (color & 0xff0000) >> 16;
408 plugin->config.in_g = (color & 0xff00) >> 8;
409 plugin->config.in_b = (color & 0xff);
410 plugin->config.in_a = alpha;
411 plugin->send_configure_change();
415 GradientOutColorButton::GradientOutColorButton(GradientMain *plugin, GradientWindow *gui,
416 int x, int y, int color, int alpha)
417 : ColorBoxButton(_("Outer color:"), x, y, COLOR_W, COLOR_H, color, alpha, 1)
419 this->plugin = plugin;
423 GradientOutColorButton::~GradientOutColorButton()
427 void GradientOutColorButton::handle_done_event(int result)
430 gui->lock_window("GradientOutColorButton::handle_done_event");
431 update_gui(orig_color, orig_alpha);
432 gui->unlock_window();
433 handle_new_color(orig_color, orig_alpha);
437 int GradientOutColorButton::handle_new_color(int color, int alpha)
439 plugin->config.out_r = (color & 0xff0000) >> 16;
440 plugin->config.out_g = (color & 0xff00) >> 8;
441 plugin->config.out_b = (color & 0xff);
442 plugin->config.out_a = alpha;
443 plugin->send_configure_change();
448 GradientReset::GradientReset(GradientMain *plugin, GradientWindow *window, int x, int y)
449 : BC_GenericButton(x, y, _("Reset"))
451 this->plugin = plugin;
452 this->window = window;
455 int GradientReset::handle_event()
457 plugin->config.reset();
458 window->update_gui();
459 plugin->send_configure_change();
464 GradientMain::GradientMain(PluginServer *server)
465 : PluginVClient(server)
468 need_reconfigure = 1;
476 GradientMain::~GradientMain()
484 const char* GradientMain::plugin_title() { return N_("Gradient"); }
485 int GradientMain::is_realtime() { return 1; }
488 NEW_WINDOW_MACRO(GradientMain, GradientWindow)
490 LOAD_CONFIGURATION_MACRO(GradientMain, GradientConfig)
492 int GradientMain::is_synthesis()
498 int GradientMain::process_buffer(VFrame *frame,
499 int64_t start_position,
503 this->output = frame;
504 float fw = input->get_w(), fh = input->get_h();
505 gradient_size = hypotf(fw, fh);
506 need_reconfigure = load_configuration();
508 int need_alpha = config.in_a != 0xff || config.out_a != 0xff;
510 read_frame(frame, 0, start_position, frame_rate, get_use_opengl());
511 if( get_use_opengl() ) return run_opengl();
513 int gradient_cmodel = input->get_color_model();
514 if( need_alpha && BC_CModels::components(gradient_cmodel) == 3 ) {
515 switch( gradient_cmodel ) {
516 case BC_RGB888: gradient_cmodel = BC_RGBA8888; break;
517 case BC_YUV888: gradient_cmodel = BC_YUVA8888; break;
518 case BC_RGB_FLOAT: gradient_cmodel = BC_RGBA_FLOAT; break;
522 int bpp = BC_CModels::calculate_pixelsize(gradient_cmodel);
523 int comps = BC_CModels::components(gradient_cmodel);
524 int grad_size1 = gradient_size + 1;
525 int sz = 4 * (bpp / comps) * grad_size1;
526 if( table_size < sz ) {
527 delete [] table; table = 0;
530 table = new uint8_t[table_size = sz];
531 need_reconfigure = 1;
534 if( gradient && gradient->get_color_model() != gradient_cmodel ) {
540 gradient = new VFrame(input->get_w(), input->get_h(),
544 engine = new GradientServer(this,
545 get_project_smp() + 1, get_project_smp() + 1);
546 engine->process_packages();
548 // Use overlay routine in GradientServer if mismatched colormodels
549 if( gradient->get_color_model() == output->get_color_model() ) {
550 if( !overlayer ) overlayer = new OverlayFrame(get_project_smp() + 1);
551 overlayer->overlay(output, gradient,
552 0, 0, input->get_w(), input->get_h(),
553 0, 0, input->get_w(), input->get_h(),
554 1.0, TRANSFER_NORMAL, NEAREST_NEIGHBOR);
561 void GradientMain::update_gui()
563 if( !thread ) return;
564 if( !load_configuration() ) return;
565 thread->window->lock_window("GradientMain::update_gui");
566 if( load_configuration() ) {
567 GradientWindow *window = (GradientWindow *)thread->window;
568 window->update_gui();
571 thread->window->unlock_window();
574 void GradientWindow::update_gui()
576 GradientConfig &config = plugin->config;
577 rate->set_text(GradientRate::to_text(config.rate));
578 in_radius->update(config.in_radius);
579 out_radius->update(config.out_radius);
580 shape->set_text(GradientShape::to_text(config.shape));
581 if( angle ) angle->update(config.angle);
582 if( center_x ) center_x->update(config.center_x);
583 if( center_y ) center_y->update(config.center_y);
585 in_color->update_gui(config.get_in_color(), config.in_a);
586 out_color->update_gui(config.get_out_color(), config.out_a);
590 void GradientMain::save_data(KeyFrame *keyframe)
594 // cause data to be stored directly in text
595 output.set_shared_output(keyframe->xbuf);
596 output.tag.set_title("GRADIENT");
598 output.tag.set_property("ANGLE", config.angle);
599 output.tag.set_property("IN_RADIUS", config.in_radius);
600 output.tag.set_property("OUT_RADIUS", config.out_radius);
601 output.tag.set_property("IN_R", config.in_r);
602 output.tag.set_property("IN_G", config.in_g);
603 output.tag.set_property("IN_B", config.in_b);
604 output.tag.set_property("IN_A", config.in_a);
605 output.tag.set_property("OUT_R", config.out_r);
606 output.tag.set_property("OUT_G", config.out_g);
607 output.tag.set_property("OUT_B", config.out_b);
608 output.tag.set_property("OUT_A", config.out_a);
609 output.tag.set_property("SHAPE", config.shape);
610 output.tag.set_property("RATE", config.rate);
611 output.tag.set_property("CENTER_X", config.center_x);
612 output.tag.set_property("CENTER_Y", config.center_y);
614 output.tag.set_title("/GRADIENT");
616 output.append_newline();
617 output.terminate_string();
620 void GradientMain::read_data(KeyFrame *keyframe)
624 input.set_shared_input(keyframe->xbuf);
628 while( !(result = input.read_tag()) ) {
629 if( input.tag.title_is("GRADIENT") ) {
630 config.angle = input.tag.get_property("ANGLE", config.angle);
631 config.rate = input.tag.get_property("RATE", config.rate);
632 config.in_radius = input.tag.get_property("IN_RADIUS", config.in_radius);
633 config.out_radius = input.tag.get_property("OUT_RADIUS", config.out_radius);
634 config.in_r = input.tag.get_property("IN_R", config.in_r);
635 config.in_g = input.tag.get_property("IN_G", config.in_g);
636 config.in_b = input.tag.get_property("IN_B", config.in_b);
637 config.in_a = input.tag.get_property("IN_A", config.in_a);
638 config.out_r = input.tag.get_property("OUT_R", config.out_r);
639 config.out_g = input.tag.get_property("OUT_G", config.out_g);
640 config.out_b = input.tag.get_property("OUT_B", config.out_b);
641 config.out_a = input.tag.get_property("OUT_A", config.out_a);
642 config.shape = input.tag.get_property("SHAPE", config.shape);
643 config.center_x = input.tag.get_property("CENTER_X", config.center_x);
644 config.center_y = input.tag.get_property("CENTER_Y", config.center_y);
649 int GradientMain::handle_opengl()
652 const char *head_frag =
653 "uniform sampler2D tex;\n"
654 "uniform vec2 tex_dimensions;\n"
655 "uniform vec2 center;\n"
656 "uniform vec2 angle;\n"
657 "uniform float gradient_size;\n"
658 "uniform vec4 out_color;\n"
659 "uniform vec4 in_color;\n"
660 "uniform float in_radius;\n"
661 "uniform float out_radius;\n"
662 "uniform float radius_diff;\n"
666 " vec2 out_coord = gl_TexCoord[0].st;\n"
667 " vec2 in_coord = out_coord * tex_dimensions - center;\n";
669 const char *linear_shape =
670 " float mag = 0.5 + dot(in_coord,angle)/gradient_size;\n";
672 const char *radial_shape =
673 " float mag = length(in_coord)/gradient_size;\n";
675 // No clamp function in NVidia
676 const char *linear_rate =
677 " mag = min(max(mag, in_radius), out_radius);\n"
678 " float opacity = (mag - in_radius) / radius_diff;\n";
680 // NVidia warns about exp, but exp is in the GLSL spec.
681 const char *log_rate =
682 " mag = max(mag, in_radius);\n"
683 " float opacity = 1.0 - \n"
684 " exp(1.0 * -(mag - in_radius) / radius_diff);\n";
686 const char *square_rate =
687 " mag = min(max(mag, in_radius), out_radius);\n"
688 " float opacity = pow((mag - in_radius) / radius_diff, 2.0);\n"
689 " opacity = min(opacity, 1.0);\n";
691 const char *tail_frag =
692 " vec4 color = mix(in_color, out_color, opacity);\n"
693 " vec4 bg_color = texture2D(tex, out_coord);\n"
694 " gl_FragColor.rgb = mix(bg_color.rgb, color.rgb, color.a);\n"
695 " gl_FragColor.a = mix(bg_color.a, 1., color.a);\n"
699 const char *shader_stack[16];
700 memset(shader_stack,0, sizeof(shader_stack));
701 int current_shader = 0;
703 shader_stack[current_shader++] = head_frag;
705 const char *shape_frag = 0;
706 switch( config.shape ) {
707 case GradientConfig::LINEAR:
708 shape_frag = linear_shape;
711 shape_frag = radial_shape;
715 shader_stack[current_shader++] = shape_frag;
717 const char *rate_frag = 0;
718 switch( config.rate ) {
719 case GradientConfig::LINEAR:
720 rate_frag = linear_rate;
722 case GradientConfig::LOG:
723 rate_frag = log_rate;
725 case GradientConfig::SQUARE:
726 rate_frag = square_rate;
730 shader_stack[current_shader++] = rate_frag;
732 shader_stack[current_shader++] = tail_frag;
734 // Force frame to create texture without copying to it if full alpha.
735 if( config.in_a >= 0xff && config.out_a >= 0xff )
736 get_output()->set_opengl_state(VFrame::TEXTURE);
737 get_output()->to_texture();
738 get_output()->enable_opengl();
739 get_output()->init_screen();
740 get_output()->bind_texture(0);
742 shader_stack[current_shader] = 0;
743 unsigned int shader = VFrame::make_shader(shader_stack);
745 glUseProgram(shader);
746 float w = get_output()->get_w();
747 float h = get_output()->get_h();
748 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
749 float texture_w = get_output()->get_texture_w();
750 float texture_h = get_output()->get_texture_h();
751 glUniform2f(glGetUniformLocation(shader, "tex_dimensions"),
752 texture_w, texture_h);
753 float u = config.shape == GradientConfig::LINEAR ?
754 0.5f : config.center_x/100.f;
755 float v = config.shape == GradientConfig::LINEAR ?
756 0.5f : config.center_y/100.f;
757 glUniform2f(glGetUniformLocation(shader, "center"),
759 glUniform1f(glGetUniformLocation(shader, "gradient_size"),
761 glUniform2f(glGetUniformLocation(shader, "angle"),
762 -sin(config.angle * (M_PI / 180)), cos(config.angle * (M_PI / 180)));
764 float in_radius = (float)config.in_radius/100;
765 float out_radius = (float)config.out_radius/100;
766 if( in_radius > out_radius ) {
768 in_radius = out_radius;
771 glUniform1f(glGetUniformLocation(shader, "in_radius"), in_radius);
772 glUniform1f(glGetUniformLocation(shader, "out_radius"), out_radius);
773 glUniform1f(glGetUniformLocation(shader, "radius_diff"),
774 out_radius - in_radius);
776 float in_r = (float)config.in_r / 0xff;
777 float in_g = (float)config.in_g / 0xff;
778 float in_b = (float)config.in_b / 0xff;
779 float in_a = (float)config.in_a / 0xff;
780 float out_r = (float)config.out_r / 0xff;
781 float out_g = (float)config.out_g / 0xff;
782 float out_b = (float)config.out_b / 0xff;
783 float out_a = (float)config.out_a / 0xff;
784 switch( get_output()->get_color_model() ) {
787 float in1, in2, in3, in4;
788 float out1, out2, out3, out4;
789 YUV::yuv.rgb_to_yuv_f(in_r,in_g,in_b, in1,in2,in3);
790 in2 += 0.5; in3 += 0.5; in4 = in_a;
791 YUV::yuv.rgb_to_yuv_f(out_r,out_g,out_b, out1,out2,out3);
792 out2 += 0.5; out3 += 0.5; out4 = out_a;
793 glUniform4f(glGetUniformLocation(shader, "out_color"),
794 out1, out2, out3, out4);
795 glUniform4f(glGetUniformLocation(shader, "in_color"),
800 glUniform4f(glGetUniformLocation(shader, "out_color"),
801 out_r, out_g, out_b, out_a);
802 glUniform4f(glGetUniformLocation(shader, "in_color"),
803 in_r, in_g, in_b, in_a);
808 get_output()->draw_texture();
810 get_output()->set_opengl_state(VFrame::SCREEN);
817 GradientPackage::GradientPackage()
822 GradientUnit::GradientUnit(GradientServer *server, GradientMain *plugin)
825 this->plugin = plugin;
826 this->server = server;
830 static float calculate_opacity(float mag,
831 float in_radius, float out_radius, int rate)
833 float mag_diff = mag - in_radius;
834 float radius_diff = out_radius - in_radius;
835 if( !radius_diff ) return 1.0;
838 case GradientConfig::LINEAR:
839 opacity = mag < in_radius ? 0.0 : mag >= out_radius ? 1.0 :
840 mag_diff / radius_diff;
842 case GradientConfig::LOG:
843 opacity = mag < in_radius ? 0.0 : // Let this one decay beyond out_radius
844 1 - exp(1.0 * -mag_diff / radius_diff);
846 case GradientConfig::SQUARE:
847 opacity = mag < in_radius ? 0.0 : mag >= out_radius ? 1.0 :
848 powf(mag_diff / radius_diff, 2.);
854 #define CREATE_GRADIENT(type, temp, components, max) { \
855 /* Synthesize linear gradient for lookups */ \
856 int grad_size = plugin->gradient_size; \
857 int n = sizeof(type) * (grad_size+1); \
858 void *r_table = (void*) (plugin->table + 0*n); \
859 void *g_table = (void*) (plugin->table + 1*n); \
860 void *b_table = (void*) (plugin->table + 2*n); \
861 void *a_table = (void*) (plugin->table + 3*n); \
862 double grad_size2 = grad_size / 2.; \
864 if( plugin->need_reconfigure ) { \
865 for( int i = 0; i <= grad_size; i++ ) { \
866 float opacity = calculate_opacity(i, in_radius, out_radius, plugin->config.rate); \
867 float transparency = 1.0 - opacity; \
868 ((type*)r_table)[i] = (type)(out1 * opacity + in1 * transparency); \
869 ((type*)g_table)[i] = (type)(out2 * opacity + in2 * transparency); \
870 ((type*)b_table)[i] = (type)(out3 * opacity + in3 * transparency); \
871 ((type*)a_table)[i] = (type)(out4 * opacity + in4 * transparency); \
875 for( int i = pkg->y1; i < pkg->y2; i++ ) { \
876 double y = half_h - i; \
877 type *gradient_row = (type*)plugin->gradient->get_rows()[i]; \
878 type *out_row = (type*)plugin->get_output()->get_rows()[i]; \
879 switch( plugin->config.shape ) { \
880 case GradientConfig::LINEAR: \
881 for( int j = 0; j < w; j++ ) { \
882 double x = j - half_w; \
883 /* Rotate by effect angle */ \
884 int mag = (grad_size2 - (x * sin_angle + y * cos_angle)) + 0.5; \
885 /* Get gradient value from these coords */ \
886 if( sizeof(type) == 4 ) { \
887 float opacity = calculate_opacity(mag, \
888 in_radius, out_radius, plugin->config.rate); \
889 float transparency = 1.0 - opacity; \
890 gradient_row[0] = (type)(out1 * opacity + in1 * transparency); \
891 gradient_row[1] = (type)(out2 * opacity + in2 * transparency); \
892 gradient_row[2] = (type)(out3 * opacity + in3 * transparency); \
893 if( components == 4 ) \
894 gradient_row[3] = (type)(out4 * opacity + in4 * transparency); \
896 else if( mag < 0 ) { \
897 gradient_row[0] = in1; \
898 gradient_row[1] = in2; \
899 gradient_row[2] = in3; \
900 if( components == 4 ) \
901 gradient_row[3] = in4; \
903 else if( mag >= grad_size ) { \
904 gradient_row[0] = out1; \
905 gradient_row[1] = out2; \
906 gradient_row[2] = out3; \
907 if( components == 4 ) \
908 gradient_row[3] = out4; \
911 gradient_row[0] = ((type*)r_table)[mag]; \
912 gradient_row[1] = ((type*)g_table)[mag]; \
913 gradient_row[2] = ((type*)b_table)[mag]; \
914 if( components == 4 ) \
915 gradient_row[3] = ((type*)a_table)[mag]; \
917 /* no alpha output, Overlay mixed colormodels */ \
918 if( gradient_cmodel != output_cmodel ) { \
919 temp opacity = gradient_row[3]; \
920 temp transparency = max - opacity; \
921 out_row[0] = (transparency * out_row[0] + opacity * gradient_row[0]) / max; \
922 out_row[1] = (transparency * out_row[1] + opacity * gradient_row[1]) / max; \
923 out_row[2] = (transparency * out_row[2] + opacity * gradient_row[2]) / max; \
926 gradient_row += components; \
930 case GradientConfig::RADIAL: \
931 for( int j = 0; j < w; j++ ) { \
932 double x = j - center_x, y = i - center_y; \
933 double magnitude = hypot(x, y); \
934 int mag = (int)magnitude; \
935 if( sizeof(type) == 4 ) { \
936 float opacity = calculate_opacity(mag, \
937 in_radius, out_radius, plugin->config.rate); \
938 float transparency = 1.0 - opacity; \
939 gradient_row[0] = (type)(out1 * opacity + in1 * transparency); \
940 gradient_row[1] = (type)(out2 * opacity + in2 * transparency); \
941 gradient_row[2] = (type)(out3 * opacity + in3 * transparency); \
942 if( components == 4 ) \
943 gradient_row[3] = (type)(out4 * opacity + in4 * transparency); \
946 gradient_row[0] = ((type*)r_table)[mag]; \
947 gradient_row[1] = ((type*)g_table)[mag]; \
948 gradient_row[2] = ((type*)b_table)[mag]; \
949 if( components == 4 ) \
950 gradient_row[3] = ((type*)a_table)[mag]; \
952 /* Overlay mixed colormodels onto output */ \
953 if( gradient_cmodel != output_cmodel ) { \
954 temp opacity = gradient_row[3]; \
955 temp transparency = max - opacity; \
956 out_row[0] = (transparency * out_row[0] + opacity * gradient_row[0]) / max; \
957 out_row[1] = (transparency * out_row[1] + opacity * gradient_row[1]) / max; \
958 out_row[2] = (transparency * out_row[2] + opacity * gradient_row[2]) / max; \
961 gradient_row += components; \
968 void GradientUnit::process_package(LoadPackage *package)
970 GradientPackage *pkg = (GradientPackage*)package;
971 int h = plugin->input->get_h();
972 int w = plugin->input->get_w();
973 int grad_size = plugin->gradient_size;
976 int in_radius = (int)(plugin->config.in_radius / 100 * grad_size);
977 int out_radius = (int)(plugin->config.out_radius / 100 * grad_size);
978 double sin_angle = sin(plugin->config.angle * (M_PI / 180));
979 double cos_angle = cos(plugin->config.angle * (M_PI / 180));
980 double center_x = plugin->config.center_x * w / 100;
981 double center_y = plugin->config.center_y * h / 100;
982 int gradient_cmodel = plugin->gradient->get_color_model();
983 int output_cmodel = plugin->get_output()->get_color_model();
985 if( in_radius > out_radius ) {
987 in_radius = out_radius;
991 switch( gradient_cmodel ) {
993 int in1 = plugin->config.in_r;
994 int in2 = plugin->config.in_g;
995 int in3 = plugin->config.in_b;
996 int in4 = plugin->config.in_a;
997 int out1 = plugin->config.out_r;
998 int out2 = plugin->config.out_g;
999 int out3 = plugin->config.out_b;
1000 int out4 = plugin->config.out_a;
1001 CREATE_GRADIENT(unsigned char, int, 3, 0xff)
1005 int in1 = plugin->config.in_r;
1006 int in2 = plugin->config.in_g;
1007 int in3 = plugin->config.in_b;
1008 int in4 = plugin->config.in_a;
1009 int out1 = plugin->config.out_r;
1010 int out2 = plugin->config.out_g;
1011 int out3 = plugin->config.out_b;
1012 int out4 = plugin->config.out_a;
1013 CREATE_GRADIENT(unsigned char, int, 4, 0xff)
1016 case BC_RGB_FLOAT: {
1017 float in1 = (float)plugin->config.in_r / 0xff;
1018 float in2 = (float)plugin->config.in_g / 0xff;
1019 float in3 = (float)plugin->config.in_b / 0xff;
1020 float in4 = (float)plugin->config.in_a / 0xff;
1021 float out1 = (float)plugin->config.out_r / 0xff;
1022 float out2 = (float)plugin->config.out_g / 0xff;
1023 float out3 = (float)plugin->config.out_b / 0xff;
1024 float out4 = (float)plugin->config.out_a / 0xff;
1025 CREATE_GRADIENT(float, float, 3, 1.0)
1028 case BC_RGBA_FLOAT: {
1029 float in1 = (float)plugin->config.in_r / 0xff;
1030 float in2 = (float)plugin->config.in_g / 0xff;
1031 float in3 = (float)plugin->config.in_b / 0xff;
1032 float in4 = (float)plugin->config.in_a / 0xff;
1033 float out1 = (float)plugin->config.out_r / 0xff;
1034 float out2 = (float)plugin->config.out_g / 0xff;
1035 float out3 = (float)plugin->config.out_b / 0xff;
1036 float out4 = (float)plugin->config.out_a / 0xff;
1037 CREATE_GRADIENT(float, float, 4, 1.0)
1041 int in_r = plugin->config.in_r;
1042 int in_g = plugin->config.in_g;
1043 int in_b = plugin->config.in_b;
1044 int in1, in2, in3, in4;
1045 int out1, out2, out3, out4;
1046 YUV::yuv.rgb_to_yuv_8(in_r,in_g,in_b, in1,in2,in3);
1047 in4 = plugin->config.in_a;
1048 int out_r = plugin->config.out_r;
1049 int out_g = plugin->config.out_g;
1050 int out_b = plugin->config.out_b;
1051 YUV::yuv.rgb_to_yuv_8(out_r,out_g,out_b, out1,out2,out3);
1052 out4 = plugin->config.out_a;
1053 CREATE_GRADIENT(unsigned char, int, 3, 0xff)
1057 int in_r = plugin->config.in_r;
1058 int in_g = plugin->config.in_g;
1059 int in_b = plugin->config.in_b;
1060 int in1, in2, in3, in4;
1061 int out1, out2, out3, out4;
1062 YUV::yuv.rgb_to_yuv_8(in_r,in_g,in_b, in1,in2,in3);
1063 in4 = plugin->config.in_a;
1064 int out_r = plugin->config.out_r;
1065 int out_g = plugin->config.out_g;
1066 int out_b = plugin->config.out_b;
1067 YUV::yuv.rgb_to_yuv_8(out_r,out_g,out_b, out1,out2,out3);
1068 out4 = plugin->config.out_a;
1069 CREATE_GRADIENT(unsigned char, int, 4, 0xff)
1075 GradientServer::GradientServer(GradientMain *plugin,
1076 int total_clients, int total_packages)
1077 : LoadServer(total_clients, total_packages)
1079 this->plugin = plugin;
1082 void GradientServer::init_packages()
1084 for( int i = 0; i < get_total_packages(); i++ ) {
1085 GradientPackage *package = (GradientPackage*)get_package(i);
1086 package->y1 = plugin->input->get_h() *
1088 get_total_packages();
1089 package->y2 = plugin->input->get_h() *
1091 get_total_packages();
1095 LoadClient* GradientServer::new_client()
1097 return new GradientUnit(this, plugin);
1100 LoadPackage* GradientServer::new_package()
1102 return new GradientPackage;