fa001041c975768c42833924f3d40a951f4f2193
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / gradient / gradient.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <math.h>
23 #include <stdint.h>
24 #include <string.h>
25
26 #include "bccolors.h"
27 #include "bcdisplayinfo.h"
28 #include "clip.h"
29 #include "bchash.h"
30 #include "filexml.h"
31 #include "gradient.h"
32 #include "keyframe.h"
33 #include "language.h"
34 #include "overlayframe.h"
35 #include "theme.h"
36 #include "vframe.h"
37
38 REGISTER_PLUGIN(GradientMain)
39
40 GradientConfig::GradientConfig()
41 {
42         reset();
43 }
44
45 void GradientConfig::reset()
46 {
47         angle = 0;
48         in_radius = 0;
49         out_radius = 100;
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;
54         center_x = 50;
55         center_y = 50;
56 }
57
58 int GradientConfig::equivalent(GradientConfig &that)
59 {
60         return (EQUIV(angle, that.angle) &&
61                 EQUIV(in_radius, that.in_radius) &&
62                 EQUIV(out_radius, that.out_radius) &&
63                 in_r == that.in_r &&
64                 in_g == that.in_g &&
65                 in_b == that.in_b &&
66                 in_a == that.in_a &&
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 &&
72                 rate == that.rate &&
73                 EQUIV(center_x, that.center_x) &&
74                 EQUIV(center_y, that.center_y));
75 }
76
77 void GradientConfig::copy_from(GradientConfig &that)
78 {
79         angle = that.angle;
80         in_radius = that.in_radius;
81         out_radius = that.out_radius;
82         in_r = that.in_r;
83         in_g = that.in_g;
84         in_b = that.in_b;
85         in_a = that.in_a;
86         out_r = that.out_r;
87         out_g = that.out_g;
88         out_b = that.out_b;
89         out_a = that.out_a;
90         shape = that.shape;
91         rate = that.rate;
92         center_x = that.center_x;
93         center_y = that.center_y;
94 }
95
96 void GradientConfig::interpolate(GradientConfig &prev, GradientConfig &next,
97                 long prev_frame, long next_frame, long current_frame)
98 {
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);
101
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);
113         shape = prev.shape;
114         rate = prev.rate;
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;
117 }
118
119 int GradientConfig::get_in_color()
120 {
121         int result = (in_r << 16) | (in_g << 8) | (in_b);
122         return result;
123 }
124
125 int GradientConfig::get_out_color()
126 {
127         int result = (out_r << 16) | (out_g << 8) | (out_b);
128         return result;
129 }
130
131 #define COLOR_W 100
132 #define COLOR_H 30
133
134 GradientWindow::GradientWindow(GradientMain *plugin)
135  : PluginClientWindow(plugin, 350, 290, 350, 290, 0)
136 {
137         this->plugin = plugin;
138         angle = 0;
139         angle_title = 0;
140         center_x = 0;
141         center_y = 0;
142         center_x_title = 0;
143         center_y_title = 0;
144 }
145 GradientWindow::~GradientWindow()
146 {
147 }
148
149
150 void GradientWindow::create_objects()
151 {
152         int margin = plugin->get_theme()->widget_border;
153         int x = 10, y = 10;
154         BC_Title *title;
155
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;
161         shape_x = x;
162         shape_y = y;
163         y += BC_Pot::calculate_h() + margin;
164
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;
170
171         int x1 = x, y1 = y;
172         BC_Title *title1;
173         add_subwindow(title1 = new BC_Title(x, y, _("Inner radius:")));
174         y += BC_Slider::get_span(0) + margin;
175         BC_Title *title2;
176         add_subwindow(title2 = new BC_Title(x, y, _("Outer radius:")));
177
178         y = y1;
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;
184
185         x = x1;
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();
195
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;
202
203         add_subwindow(reset = new GradientReset(plugin, this, x, y));
204         update_shape();
205         show_window();
206 }
207
208 void GradientWindow::update_shape()
209 {
210         int x = shape_x, y = shape_y;
211
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;
217                 if( !angle ) {
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));
220                 }
221         }
222         else {
223                 delete angle_title;  angle_title = 0;
224                 delete angle;  angle = 0;
225                 if( !center_x ) {
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));
233                 }
234         }
235         show_window();
236 }
237
238 void GradientWindow::done_event(int result)
239 {
240         in_color->close_picker();
241         out_color->close_picker();
242 }
243
244 GradientShape::GradientShape(GradientMain *plugin, GradientWindow *gui, int x, int y)
245  : BC_PopupMenu(x, y, 100, to_text(plugin->config.shape), 1)
246 {
247         this->plugin = plugin;
248         this->gui = gui;
249 }
250 void GradientShape::create_objects()
251 {
252         add_item(new BC_MenuItem(to_text(GradientConfig::LINEAR)));
253         add_item(new BC_MenuItem(to_text(GradientConfig::RADIAL)));
254 }
255 char* GradientShape::to_text(int shape)
256 {
257         switch( shape ) {
258         case GradientConfig::LINEAR:    return _("Linear");
259         }
260         return _("Radial");
261 }
262 int GradientShape::from_text(char *text)
263 {
264         if( !strcmp(text, to_text(GradientConfig::LINEAR)) )
265                 return GradientConfig::LINEAR;
266         return GradientConfig::RADIAL;
267 }
268 int GradientShape::handle_event()
269 {
270         plugin->config.shape = from_text(get_text());
271         gui->update_shape();
272         plugin->send_configure_change();
273         return 1;
274 }
275
276
277 GradientCenterX::GradientCenterX(GradientMain *plugin, int x, int y)
278  : BC_FPot(x, y, plugin->config.center_x, 0, 100)
279 {
280         this->plugin = plugin;
281 }
282 int GradientCenterX::handle_event()
283 {
284         plugin->config.center_x = get_value();
285         plugin->send_configure_change();
286         return 1;
287 }
288
289
290 GradientCenterY::GradientCenterY(GradientMain *plugin, int x, int y)
291  : BC_FPot(x, y, plugin->config.center_y, 0, 100)
292 {
293         this->plugin = plugin;
294 }
295
296 int GradientCenterY::handle_event()
297 {
298         plugin->config.center_y = get_value();
299         plugin->send_configure_change();
300         return 1;
301 }
302
303
304 GradientAngle::GradientAngle(GradientMain *plugin, int x, int y)
305  : BC_FPot(x, y, plugin->config.angle, -180, 180)
306 {
307         this->plugin = plugin;
308 }
309
310 int GradientAngle::handle_event()
311 {
312         plugin->config.angle = get_value();
313         plugin->send_configure_change();
314         return 1;
315 }
316
317
318 GradientRate::GradientRate(GradientMain *plugin, int x, int y)
319  : BC_PopupMenu(x, y, 100, to_text(plugin->config.rate), 1)
320 {
321         this->plugin = plugin;
322 }
323 void GradientRate::create_objects()
324 {
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)));
328 }
329 char* GradientRate::to_text(int shape)
330 {
331         switch( shape ) {
332         case GradientConfig::LINEAR:    return _("Linear");
333         case GradientConfig::LOG:       return _("Log");
334         }
335         return _("Square");
336 }
337 int GradientRate::from_text(char *text)
338 {
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;
344 }
345 int GradientRate::handle_event()
346 {
347         plugin->config.rate = from_text(get_text());
348         plugin->send_configure_change();
349         return 1;
350 }
351
352
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)
356 {
357         this->plugin = plugin;
358 }
359
360 int GradientInRadius::handle_event()
361 {
362         plugin->config.in_radius = get_value();
363         plugin->send_configure_change();
364         return 1;
365 }
366
367
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)
371 {
372         this->plugin = plugin;
373 }
374
375 int GradientOutRadius::handle_event()
376 {
377         plugin->config.out_radius = get_value();
378         plugin->send_configure_change();
379         return 1;
380 }
381
382
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)
386 {
387         this->plugin = plugin;
388         this->gui = gui;
389 }
390
391 GradientInColorButton::~GradientInColorButton()
392 {
393 }
394
395 void GradientInColorButton::handle_done_event(int result)
396 {
397         if( 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);
402         }
403 }
404
405 int GradientInColorButton::handle_new_color(int color, int alpha)
406 {
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();
412         return 1;
413 }
414
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)
418 {
419         this->plugin = plugin;
420         this->gui = gui;
421 }
422
423 GradientOutColorButton::~GradientOutColorButton()
424 {
425 }
426
427 void GradientOutColorButton::handle_done_event(int result)
428 {
429         if( 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);
434         }
435 }
436
437 int GradientOutColorButton::handle_new_color(int color, int alpha)
438 {
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();
444         return 1;
445 }
446
447
448 GradientReset::GradientReset(GradientMain *plugin, GradientWindow *window, int x, int y)
449  : BC_GenericButton(x, y, _("Reset"))
450 {
451         this->plugin = plugin;
452         this->window = window;
453 }
454
455 int GradientReset::handle_event()
456 {
457         plugin->config.reset();
458         window->update_gui();
459         plugin->send_configure_change();
460         return 1;
461 }
462
463
464 GradientMain::GradientMain(PluginServer *server)
465  : PluginVClient(server)
466 {
467
468         need_reconfigure = 1;
469         gradient = 0;
470         engine = 0;
471         overlayer = 0;
472         table = 0;
473         table_size = 0;
474 }
475
476 GradientMain::~GradientMain()
477 {
478         delete [] table;
479         delete gradient;
480         delete engine;
481         delete overlayer;
482 }
483
484 const char* GradientMain::plugin_title() { return N_("Gradient"); }
485 int GradientMain::is_realtime() { return 1; }
486
487
488 NEW_WINDOW_MACRO(GradientMain, GradientWindow)
489
490 LOAD_CONFIGURATION_MACRO(GradientMain, GradientConfig)
491
492 int GradientMain::is_synthesis()
493 {
494         return 1;
495 }
496
497
498 int GradientMain::process_buffer(VFrame *frame,
499         int64_t start_position,
500         double frame_rate)
501 {
502         this->input = frame;
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();
507
508         int need_alpha = config.in_a != 0xff || config.out_a != 0xff;
509         if( need_alpha )
510                 read_frame(frame, 0, start_position, frame_rate, get_use_opengl());
511         if( get_use_opengl() ) return run_opengl();
512
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;
519                 }
520         }
521
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;
528         }
529         if( !table ) {
530                 table = new uint8_t[table_size = sz];
531                 need_reconfigure = 1;
532         }
533
534         if( gradient && gradient->get_color_model() != gradient_cmodel ) {
535                 delete gradient;
536                 gradient = 0;
537         }
538
539         if( !gradient )
540                 gradient = new VFrame(input->get_w(), input->get_h(),
541                         gradient_cmodel, 0);
542
543         if( !engine )
544                 engine = new GradientServer(this,
545                         get_project_smp() + 1, get_project_smp() + 1);
546         engine->process_packages();
547
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);
555         }
556
557         return 0;
558 }
559
560
561 void GradientMain::update_gui()
562 {
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();
569                 window->flush();
570         }
571         thread->window->unlock_window();
572 }
573
574 void GradientWindow::update_gui()
575 {
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);
584         update_shape();
585         in_color->update_gui(config.get_in_color(), config.in_a);
586         out_color->update_gui(config.get_out_color(), config.out_a);
587 }
588
589
590 void GradientMain::save_data(KeyFrame *keyframe)
591 {
592         FileXML output;
593
594 // cause data to be stored directly in text
595         output.set_shared_output(keyframe->xbuf);
596         output.tag.set_title("GRADIENT");
597
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);
613         output.append_tag();
614         output.tag.set_title("/GRADIENT");
615         output.append_tag();
616         output.append_newline();
617         output.terminate_string();
618 }
619
620 void GradientMain::read_data(KeyFrame *keyframe)
621 {
622         FileXML input;
623
624         input.set_shared_input(keyframe->xbuf);
625
626         int result = 0;
627
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);
645                 }
646         }
647 }
648
649 int GradientMain::handle_opengl()
650 {
651 #ifdef HAVE_GL
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"
663                 "\n"
664                 "void main()\n"
665                 "{\n"
666                 "       vec2 out_coord = gl_TexCoord[0].st;\n"
667                 "       vec2 in_coord = out_coord * tex_dimensions - center;\n";
668
669         const char *linear_shape =
670                 "       float mag = 0.5 + dot(in_coord,angle)/gradient_size;\n";
671
672         const char *radial_shape =
673                 "       float mag = length(in_coord)/gradient_size;\n";
674
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";
679
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";
685
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";
690
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"
696                 "}\n";
697
698
699         const char *shader_stack[16];
700         memset(shader_stack,0, sizeof(shader_stack));
701         int current_shader = 0;
702
703         shader_stack[current_shader++] = head_frag;
704
705         const char *shape_frag = 0;
706         switch( config.shape ) {
707         case GradientConfig::LINEAR:
708                 shape_frag = linear_shape;
709                 break;
710         default:
711                 shape_frag = radial_shape;
712                 break;
713         }
714         if( shape_frag )
715                 shader_stack[current_shader++] = shape_frag;
716
717         const char *rate_frag = 0;
718         switch( config.rate ) {
719         case GradientConfig::LINEAR:
720                 rate_frag = linear_rate;
721                 break;
722         case GradientConfig::LOG:
723                 rate_frag = log_rate;
724                 break;
725         case GradientConfig::SQUARE:
726                 rate_frag = square_rate;
727                 break;
728         }
729         if( rate_frag )
730                 shader_stack[current_shader++] = rate_frag;
731
732         shader_stack[current_shader++] = tail_frag;
733
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);
741
742         shader_stack[current_shader] = 0;
743         unsigned int shader = VFrame::make_shader(shader_stack);
744         if( shader > 0 ) {
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"),
758                         u * w, v * h);
759                 glUniform1f(glGetUniformLocation(shader, "gradient_size"),
760                         gradient_size);
761                 glUniform2f(glGetUniformLocation(shader, "angle"),
762                         -sin(config.angle * (M_PI / 180)), cos(config.angle * (M_PI / 180)));
763
764                 float in_radius = (float)config.in_radius/100;
765                 float out_radius = (float)config.out_radius/100;
766                 if( in_radius > out_radius ) {
767                         float r = in_radius;
768                         in_radius = out_radius;
769                         out_radius = r;
770                 }
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);
775
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() ) {
785                 case BC_YUV888:
786                 case BC_YUVA8888: {
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"),
796                                 in1, in2, in3, in4);
797                         break; }
798
799                 default: {
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);
804                         break; }
805                 }
806         }
807
808         get_output()->draw_texture();
809         glUseProgram(0);
810         get_output()->set_opengl_state(VFrame::SCREEN);
811
812 #endif
813         return 0;
814 }
815
816
817 GradientPackage::GradientPackage()
818  : LoadPackage()
819 {
820 }
821
822 GradientUnit::GradientUnit(GradientServer *server, GradientMain *plugin)
823  : LoadClient(server)
824 {
825         this->plugin = plugin;
826         this->server = server;
827 }
828
829
830 static float calculate_opacity(float mag,
831                 float in_radius, float out_radius, int rate)
832 {
833         float mag_diff = mag - in_radius;
834         float radius_diff = out_radius - in_radius;
835         if( !radius_diff ) return 1.0;
836         float opacity = 0.0;
837         switch( rate ) {
838         case GradientConfig::LINEAR:
839                 opacity = mag < in_radius ? 0.0 : mag >= out_radius ? 1.0 :
840                                 mag_diff / radius_diff;
841                 break;
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);
845                 break;
846         case GradientConfig::SQUARE:
847                 opacity = mag < in_radius ? 0.0 : mag >= out_radius ? 1.0 :
848                                 powf(mag_diff / radius_diff, 2.);
849                 break;
850         }
851         return opacity;
852 }
853
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.; \
863  \
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); \
872                 } \
873         } \
874  \
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); \
895                                 } \
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; \
902                                 } \
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; \
909                                 } \
910                                 else { \
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]; \
916                                 } \
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; \
924                                         out_row += 3; \
925                                 } \
926                                 gradient_row += components; \
927                         } \
928                         break; \
929  \
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); \
944                                 } \
945                                 else { \
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]; \
951                                 } \
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; \
959                                         out_row += 3; \
960                                 } \
961                                 gradient_row += components; \
962                         } \
963                         break; \
964                 } \
965         } \
966 }
967
968 void GradientUnit::process_package(LoadPackage *package)
969 {
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;
974         int half_w = w / 2;
975         int half_h = h / 2;
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();
984
985         if( in_radius > out_radius ) {
986                 int r = in_radius;
987                 in_radius = out_radius;
988                 out_radius = r;
989         }
990
991         switch( gradient_cmodel ) {
992         case BC_RGB888: {
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)
1002                 break; }
1003
1004         case BC_RGBA8888: {
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)
1014                 break; }
1015
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)
1026                 break; }
1027
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)
1038                 break; }
1039
1040         case BC_YUV888: {
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.in_r;
1049                 int out_g = plugin->config.in_g;
1050                 int out_b = plugin->config.in_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)
1054                 break; }
1055
1056         case BC_YUVA8888: {
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.in_r;
1065                 int out_g = plugin->config.in_g;
1066                 int out_b = plugin->config.in_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)
1070                 break; }
1071         }
1072 }
1073
1074
1075 GradientServer::GradientServer(GradientMain *plugin,
1076         int total_clients, int total_packages)
1077  : LoadServer(total_clients, total_packages)
1078 {
1079         this->plugin = plugin;
1080 }
1081
1082 void GradientServer::init_packages()
1083 {
1084         for( int i = 0; i < get_total_packages(); i++ ) {
1085                 GradientPackage *package = (GradientPackage*)get_package(i);
1086                 package->y1 = plugin->input->get_h() *
1087                         i /
1088                         get_total_packages();
1089                 package->y2 = plugin->input->get_h() *
1090                         (i + 1) /
1091                         get_total_packages();
1092         }
1093 }
1094
1095 LoadClient* GradientServer::new_client()
1096 {
1097         return new GradientUnit(this, plugin);
1098 }
1099
1100 LoadPackage* GradientServer::new_package()
1101 {
1102         return new GradientPackage;
1103 }
1104