bg/clr color tweaks, clear borders rework, fc31 depends
[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 xS(100)
132 #define COLOR_H yS(30)
133
134 GradientWindow::GradientWindow(GradientMain *plugin)
135  : PluginClientWindow(plugin, xS(350), yS(290), xS(350), yS(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 xs10 = xS(10);
153         int ys10 = yS(10);
154         int margin = plugin->get_theme()->widget_border;
155         int x = xs10, y = ys10;
156         BC_Title *title;
157
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;
163         shape_x = x;
164         shape_y = y;
165         y += BC_Pot::calculate_h() + margin;
166
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;
172
173         int x1 = x, y1 = y;
174         BC_Title *title1;
175         add_subwindow(title1 = new BC_Title(x, y, _("Inner radius:")));
176         y += BC_Slider::get_span(0) + margin;
177         BC_Title *title2;
178         add_subwindow(title2 = new BC_Title(x, y, _("Outer radius:")));
179
180         y = y1;
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;
186
187         x = x1;
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();
197
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;
204
205         add_subwindow(reset = new GradientReset(plugin, this, x, y));
206         update_shape();
207         show_window();
208 }
209
210 void GradientWindow::update_shape()
211 {
212         int x = shape_x, y = shape_y;
213
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;
219                 if( !angle ) {
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));
222                 }
223         }
224         else {
225                 delete angle_title;  angle_title = 0;
226                 delete angle;  angle = 0;
227                 if( !center_x ) {
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));
235                 }
236         }
237         show_window();
238 }
239
240 void GradientWindow::done_event(int result)
241 {
242         in_color->close_picker();
243         out_color->close_picker();
244 }
245
246 GradientShape::GradientShape(GradientMain *plugin, GradientWindow *gui, int x, int y)
247  : BC_PopupMenu(x, y, xS(100), to_text(plugin->config.shape), 1)
248 {
249         this->plugin = plugin;
250         this->gui = gui;
251 }
252 void GradientShape::create_objects()
253 {
254         add_item(new BC_MenuItem(to_text(GradientConfig::LINEAR)));
255         add_item(new BC_MenuItem(to_text(GradientConfig::RADIAL)));
256 }
257 char* GradientShape::to_text(int shape)
258 {
259         switch( shape ) {
260         case GradientConfig::LINEAR:    return _("Linear");
261         }
262         return _("Radial");
263 }
264 int GradientShape::from_text(char *text)
265 {
266         if( !strcmp(text, to_text(GradientConfig::LINEAR)) )
267                 return GradientConfig::LINEAR;
268         return GradientConfig::RADIAL;
269 }
270 int GradientShape::handle_event()
271 {
272         plugin->config.shape = from_text(get_text());
273         gui->update_shape();
274         plugin->send_configure_change();
275         return 1;
276 }
277
278
279 GradientCenterX::GradientCenterX(GradientMain *plugin, int x, int y)
280  : BC_FPot(x, y, plugin->config.center_x, 0, xS(100))
281 {
282         this->plugin = plugin;
283 }
284 int GradientCenterX::handle_event()
285 {
286         plugin->config.center_x = get_value();
287         plugin->send_configure_change();
288         return 1;
289 }
290
291
292 GradientCenterY::GradientCenterY(GradientMain *plugin, int x, int y)
293  : BC_FPot(x, y, plugin->config.center_y, 0, xS(100))
294 {
295         this->plugin = plugin;
296 }
297
298 int GradientCenterY::handle_event()
299 {
300         plugin->config.center_y = get_value();
301         plugin->send_configure_change();
302         return 1;
303 }
304
305
306 GradientAngle::GradientAngle(GradientMain *plugin, int x, int y)
307  : BC_FPot(x, y, plugin->config.angle, -180, 180)
308 {
309         this->plugin = plugin;
310 }
311
312 int GradientAngle::handle_event()
313 {
314         plugin->config.angle = get_value();
315         plugin->send_configure_change();
316         return 1;
317 }
318
319
320 GradientRate::GradientRate(GradientMain *plugin, int x, int y)
321  : BC_PopupMenu(x, y, xS(100), to_text(plugin->config.rate), 1)
322 {
323         this->plugin = plugin;
324 }
325 void GradientRate::create_objects()
326 {
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)));
330 }
331 char* GradientRate::to_text(int shape)
332 {
333         switch( shape ) {
334         case GradientConfig::LINEAR:    return _("Linear");
335         case GradientConfig::LOG:       return _("Log");
336         }
337         return _("Square");
338 }
339 int GradientRate::from_text(char *text)
340 {
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;
346 }
347 int GradientRate::handle_event()
348 {
349         plugin->config.rate = from_text(get_text());
350         plugin->send_configure_change();
351         return 1;
352 }
353
354
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)
358 {
359         this->plugin = plugin;
360 }
361
362 int GradientInRadius::handle_event()
363 {
364         plugin->config.in_radius = get_value();
365         plugin->send_configure_change();
366         return 1;
367 }
368
369
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)
373 {
374         this->plugin = plugin;
375 }
376
377 int GradientOutRadius::handle_event()
378 {
379         plugin->config.out_radius = get_value();
380         plugin->send_configure_change();
381         return 1;
382 }
383
384
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)
388 {
389         this->plugin = plugin;
390         this->gui = gui;
391 }
392
393 GradientInColorButton::~GradientInColorButton()
394 {
395 }
396
397 void GradientInColorButton::handle_done_event(int result)
398 {
399         if( 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);
404         }
405 }
406
407 int GradientInColorButton::handle_new_color(int color, int alpha)
408 {
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();
414         return 1;
415 }
416
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)
420 {
421         this->plugin = plugin;
422         this->gui = gui;
423 }
424
425 GradientOutColorButton::~GradientOutColorButton()
426 {
427 }
428
429 void GradientOutColorButton::handle_done_event(int result)
430 {
431         if( 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);
436         }
437 }
438
439 int GradientOutColorButton::handle_new_color(int color, int alpha)
440 {
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();
446         return 1;
447 }
448
449
450 GradientReset::GradientReset(GradientMain *plugin, GradientWindow *window, int x, int y)
451  : BC_GenericButton(x, y, _("Reset"))
452 {
453         this->plugin = plugin;
454         this->window = window;
455 }
456
457 int GradientReset::handle_event()
458 {
459         plugin->config.reset();
460         window->update_gui();
461         plugin->send_configure_change();
462         return 1;
463 }
464
465
466 GradientMain::GradientMain(PluginServer *server)
467  : PluginVClient(server)
468 {
469
470         need_reconfigure = 1;
471         gradient = 0;
472         engine = 0;
473         overlayer = 0;
474         table = 0;
475         table_size = 0;
476 }
477
478 GradientMain::~GradientMain()
479 {
480         delete [] table;
481         delete gradient;
482         delete engine;
483         delete overlayer;
484 }
485
486 const char* GradientMain::plugin_title() { return N_("Gradient"); }
487 int GradientMain::is_realtime() { return 1; }
488
489
490 NEW_WINDOW_MACRO(GradientMain, GradientWindow)
491
492 LOAD_CONFIGURATION_MACRO(GradientMain, GradientConfig)
493
494 int GradientMain::is_synthesis()
495 {
496         return 1;
497 }
498
499
500 int GradientMain::process_buffer(VFrame *frame,
501         int64_t start_position,
502         double frame_rate)
503 {
504         this->input = frame;
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();
509
510         int need_alpha = config.in_a != 0xff || config.out_a != 0xff;
511         if( need_alpha )
512                 read_frame(frame, 0, start_position, frame_rate, get_use_opengl());
513         if( get_use_opengl() ) return run_opengl();
514
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;
521                 }
522         }
523
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;
530         }
531         if( !table ) {
532                 table = new uint8_t[table_size = sz];
533                 need_reconfigure = 1;
534         }
535
536         if( gradient && gradient->get_color_model() != gradient_cmodel ) {
537                 delete gradient;
538                 gradient = 0;
539         }
540
541         if( !gradient )
542                 gradient = new VFrame(input->get_w(), input->get_h(),
543                         gradient_cmodel, 0);
544
545         if( !engine )
546                 engine = new GradientServer(this,
547                         get_project_smp() + 1, get_project_smp() + 1);
548         engine->process_packages();
549
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);
557         }
558
559         return 0;
560 }
561
562
563 void GradientMain::update_gui()
564 {
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();
571                 window->flush();
572         }
573         thread->window->unlock_window();
574 }
575
576 void GradientWindow::update_gui()
577 {
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);
586         update_shape();
587         in_color->update_gui(config.get_in_color(), config.in_a);
588         out_color->update_gui(config.get_out_color(), config.out_a);
589 }
590
591
592 void GradientMain::save_data(KeyFrame *keyframe)
593 {
594         FileXML output;
595
596 // cause data to be stored directly in text
597         output.set_shared_output(keyframe->xbuf);
598         output.tag.set_title("GRADIENT");
599
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);
615         output.append_tag();
616         output.tag.set_title("/GRADIENT");
617         output.append_tag();
618         output.append_newline();
619         output.terminate_string();
620 }
621
622 void GradientMain::read_data(KeyFrame *keyframe)
623 {
624         FileXML input;
625
626         input.set_shared_input(keyframe->xbuf);
627
628         int result = 0;
629
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);
647                 }
648         }
649 }
650
651 int GradientMain::handle_opengl()
652 {
653 #ifdef HAVE_GL
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"
665                 "\n"
666                 "void main()\n"
667                 "{\n"
668                 "       vec2 out_coord = gl_TexCoord[0].st;\n"
669                 "       vec2 in_coord = out_coord * tex_dimensions - center;\n";
670
671         const char *linear_shape =
672                 "       float mag = 0.5 + dot(in_coord,angle)/gradient_size;\n";
673
674         const char *radial_shape =
675                 "       float mag = length(in_coord)/gradient_size;\n";
676
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";
681
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";
687
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";
692
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"
698                 "}\n";
699
700
701         const char *shader_stack[16];
702         memset(shader_stack,0, sizeof(shader_stack));
703         int current_shader = 0;
704
705         shader_stack[current_shader++] = head_frag;
706
707         const char *shape_frag = 0;
708         switch( config.shape ) {
709         case GradientConfig::LINEAR:
710                 shape_frag = linear_shape;
711                 break;
712         default:
713                 shape_frag = radial_shape;
714                 break;
715         }
716         if( shape_frag )
717                 shader_stack[current_shader++] = shape_frag;
718
719         const char *rate_frag = 0;
720         switch( config.rate ) {
721         case GradientConfig::LINEAR:
722                 rate_frag = linear_rate;
723                 break;
724         case GradientConfig::LOG:
725                 rate_frag = log_rate;
726                 break;
727         case GradientConfig::SQUARE:
728                 rate_frag = square_rate;
729                 break;
730         }
731         if( rate_frag )
732                 shader_stack[current_shader++] = rate_frag;
733
734         shader_stack[current_shader++] = tail_frag;
735
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);
743
744         shader_stack[current_shader] = 0;
745         unsigned int shader = VFrame::make_shader(shader_stack);
746         if( shader > 0 ) {
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"),
760                         u * w, v * h);
761                 glUniform1f(glGetUniformLocation(shader, "gradient_size"),
762                         gradient_size);
763                 glUniform2f(glGetUniformLocation(shader, "angle"),
764                         -sin(config.angle * (M_PI / 180)), cos(config.angle * (M_PI / 180)));
765
766                 float in_radius = (float)config.in_radius/100;
767                 float out_radius = (float)config.out_radius/100;
768                 if( in_radius > out_radius ) {
769                         float r = in_radius;
770                         in_radius = out_radius;
771                         out_radius = r;
772                 }
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);
777
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() ) {
787                 case BC_YUV888:
788                 case BC_YUVA8888: {
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"),
798                                 in1, in2, in3, in4);
799                         break; }
800
801                 default: {
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);
806                         break; }
807                 }
808         }
809
810         get_output()->draw_texture();
811         glUseProgram(0);
812         get_output()->set_opengl_state(VFrame::SCREEN);
813
814 #endif
815         return 0;
816 }
817
818
819 GradientPackage::GradientPackage()
820  : LoadPackage()
821 {
822 }
823
824 GradientUnit::GradientUnit(GradientServer *server, GradientMain *plugin)
825  : LoadClient(server)
826 {
827         this->plugin = plugin;
828         this->server = server;
829 }
830
831
832 static float calculate_opacity(float mag,
833                 float in_radius, float out_radius, int rate)
834 {
835         float mag_diff = mag - in_radius;
836         float radius_diff = out_radius - in_radius;
837         if( !radius_diff ) return 1.0;
838         float opacity = 0.0;
839         switch( rate ) {
840         case GradientConfig::LINEAR:
841                 opacity = mag < in_radius ? 0.0 : mag >= out_radius ? 1.0 :
842                                 mag_diff / radius_diff;
843                 break;
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);
847                 break;
848         case GradientConfig::SQUARE:
849                 opacity = mag < in_radius ? 0.0 : mag >= out_radius ? 1.0 :
850                                 powf(mag_diff / radius_diff, 2.);
851                 break;
852         }
853         return opacity;
854 }
855
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.; \
865  \
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); \
874                 } \
875         } \
876  \
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); \
897                                 } \
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; \
904                                 } \
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; \
911                                 } \
912                                 else { \
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]; \
918                                 } \
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; \
926                                         out_row += 3; \
927                                 } \
928                                 gradient_row += components; \
929                         } \
930                         break; \
931  \
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); \
946                                 } \
947                                 else { \
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]; \
953                                 } \
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; \
961                                         out_row += 3; \
962                                 } \
963                                 gradient_row += components; \
964                         } \
965                         break; \
966                 } \
967         } \
968 }
969
970 void GradientUnit::process_package(LoadPackage *package)
971 {
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;
976         int half_w = w / 2;
977         int half_h = h / 2;
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();
986
987         if( in_radius > out_radius ) {
988                 int r = in_radius;
989                 in_radius = out_radius;
990                 out_radius = r;
991         }
992
993         switch( gradient_cmodel ) {
994         case BC_RGB888: {
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)
1004                 break; }
1005
1006         case BC_RGBA8888: {
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)
1016                 break; }
1017
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)
1028                 break; }
1029
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)
1040                 break; }
1041
1042         case BC_YUV888: {
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)
1056                 break; }
1057
1058         case BC_YUVA8888: {
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)
1072                 break; }
1073         }
1074 }
1075
1076
1077 GradientServer::GradientServer(GradientMain *plugin,
1078         int total_clients, int total_packages)
1079  : LoadServer(total_clients, total_packages)
1080 {
1081         this->plugin = plugin;
1082 }
1083
1084 void GradientServer::init_packages()
1085 {
1086         for( int i = 0; i < get_total_packages(); i++ ) {
1087                 GradientPackage *package = (GradientPackage*)get_package(i);
1088                 package->y1 = plugin->input->get_h() *
1089                         i /
1090                         get_total_packages();
1091                 package->y2 = plugin->input->get_h() *
1092                         (i + 1) /
1093                         get_total_packages();
1094         }
1095 }
1096
1097 LoadClient* GradientServer::new_client()
1098 {
1099         return new GradientUnit(this, plugin);
1100 }
1101
1102 LoadPackage* GradientServer::new_package()
1103 {
1104         return new GradientPackage;
1105 }
1106