update internationalization data
[goodguy/history.git] / cinelerra-5.0 / 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 "bcdisplayinfo.h"
27 #include "clip.h"
28 #include "bchash.h"
29 #include "filexml.h"
30 #include "gradient.h"
31 #include "keyframe.h"
32 #include "language.h"
33 #include "overlayframe.h"
34 #include "theme.h"
35 #include "vframe.h"
36
37
38
39
40 REGISTER_PLUGIN(GradientMain)
41
42
43
44
45
46
47 GradientConfig::GradientConfig()
48 {
49         angle = 0;
50         in_radius = 0;
51         out_radius = 100;
52         in_r = 0xff;
53         in_g = 0xff;
54         in_b = 0xff;
55         in_a = 0xff;
56         out_r = 0x0;
57         out_g = 0x0;
58         out_b = 0x0;
59         out_a = 0x0;
60         shape = GradientConfig::LINEAR;
61         rate = GradientConfig::LINEAR;
62         center_x = 50;
63         center_y = 50;
64 }
65
66 int GradientConfig::equivalent(GradientConfig &that)
67 {
68         return (EQUIV(angle, that.angle) &&
69                 EQUIV(in_radius, that.in_radius) &&
70                 EQUIV(out_radius, that.out_radius) &&
71                 in_r == that.in_r &&
72                 in_g == that.in_g &&
73                 in_b == that.in_b &&
74                 in_a == that.in_a &&
75                 out_r == that.out_r &&
76                 out_g == that.out_g &&
77                 out_b == that.out_b &&
78                 out_a == that.out_a &&
79                 shape == that.shape &&
80                 rate == that.rate &&
81                 EQUIV(center_x, that.center_x) &&
82                 EQUIV(center_y, that.center_y));
83 }
84
85 void GradientConfig::copy_from(GradientConfig &that)
86 {
87         angle = that.angle;
88         in_radius = that.in_radius;
89         out_radius = that.out_radius;
90         in_r = that.in_r;
91         in_g = that.in_g;
92         in_b = that.in_b;
93         in_a = that.in_a;
94         out_r = that.out_r;
95         out_g = that.out_g;
96         out_b = that.out_b;
97         out_a = that.out_a;
98         shape = that.shape;
99         rate = that.rate;
100         center_x = that.center_x;
101         center_y = that.center_y;
102 }
103
104 void GradientConfig::interpolate(GradientConfig &prev, 
105         GradientConfig &next, 
106         long prev_frame, 
107         long next_frame, 
108         long current_frame)
109 {
110         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
111         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
112
113
114         this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale);
115         this->in_radius = (int)(prev.in_radius * prev_scale + next.in_radius * next_scale);
116         this->out_radius = (int)(prev.out_radius * prev_scale + next.out_radius * next_scale);
117         in_r = (int)(prev.in_r * prev_scale + next.in_r * next_scale);
118         in_g = (int)(prev.in_g * prev_scale + next.in_g * next_scale);
119         in_b = (int)(prev.in_b * prev_scale + next.in_b * next_scale);
120         in_a = (int)(prev.in_a * prev_scale + next.in_a * next_scale);
121         out_r = (int)(prev.out_r * prev_scale + next.out_r * next_scale);
122         out_g = (int)(prev.out_g * prev_scale + next.out_g * next_scale);
123         out_b = (int)(prev.out_b * prev_scale + next.out_b * next_scale);
124         out_a = (int)(prev.out_a * prev_scale + next.out_a * next_scale);
125         shape = prev.shape;
126         rate = prev.rate;
127         center_x = prev.center_x * prev_scale + next.center_x * next_scale;
128         center_y = prev.center_y * prev_scale + next.center_y * next_scale;
129 }
130
131 int GradientConfig::get_in_color()
132 {
133         int result = (in_r << 16) | (in_g << 8) | (in_b);
134         return result;
135 }
136
137 int GradientConfig::get_out_color()
138 {
139         int result = (out_r << 16) | (out_g << 8) | (out_b);
140         return result;
141 }
142
143
144
145
146
147
148
149
150
151
152
153 #define COLOR_W 100
154 #define COLOR_H 30
155
156 GradientWindow::GradientWindow(GradientMain *plugin)
157  : PluginClientWindow(plugin,
158         350, 
159         290, 
160         350, 
161         290, 
162         0)
163 {
164         this->plugin = plugin;
165         angle = 0;
166         angle_title = 0;
167         center_x = 0;
168         center_y = 0;
169         center_x_title = 0;
170         center_y_title = 0;
171 }
172
173 GradientWindow::~GradientWindow()
174 {
175         delete in_color_thread;
176         delete out_color_thread;
177 }
178
179 void GradientWindow::create_objects()
180 {
181         int margin = plugin->get_theme()->widget_border;
182         int x = 10, y = 10;
183         BC_Title *title;
184
185         add_subwindow(title = new BC_Title(x, y, _("Shape:")));
186         add_subwindow(shape = new GradientShape(plugin, 
187                 this, 
188                 x + title->get_w() + margin, 
189                 y));
190         shape->create_objects();
191         y += shape->get_h() + margin;
192         shape_x = x;
193         shape_y = y;
194         y += BC_Pot::calculate_h() + margin;
195
196         add_subwindow(title = new BC_Title(x, y, _("Rate:")));
197         add_subwindow(rate = new GradientRate(plugin,
198                 x + title->get_w() + margin,
199                 y));
200         rate->create_objects();
201         y += rate->get_h() + margin;
202
203         int x1 = x;
204         int y1 = y;
205
206         BC_Title *title1;
207         add_subwindow(title1 = new BC_Title(x, y, _("Inner radius:")));
208
209         y += BC_Slider::get_span(0) + margin;
210
211         BC_Title *title2;
212         add_subwindow(title2 = new BC_Title(x, y, _("Outer radius:")));
213
214         y = y1;
215         x += MAX(title1->get_w(), title2->get_w()) + margin;
216         
217         add_subwindow(in_radius = new GradientInRadius(plugin, x, y));
218         y += in_radius->get_h() + margin;
219
220         add_subwindow(out_radius = new GradientOutRadius(plugin, x, y));
221         y += out_radius->get_h() + margin;
222
223         x = x1;
224         y1 = y;
225         add_subwindow(in_color = new GradientInColorButton(plugin, this, x, y));
226         y += COLOR_H + margin;
227
228
229         add_subwindow(out_color = new GradientOutColorButton(plugin, this, x, y));
230         x += MAX(in_color->get_w(), out_color->get_w()) + margin;
231         y = y1;
232
233         in_color_x = x;
234         in_color_y = y;
235         y += COLOR_H + margin;
236         out_color_x = x;
237         out_color_y = y;
238         in_color_thread = new GradientInColorThread(plugin, this);
239         out_color_thread = new GradientOutColorThread(plugin, this);
240         update_in_color();
241         update_out_color();
242         update_shape();
243
244         draw_3d_border(in_color_x - 2, 
245                 in_color_y - 2, 
246                 COLOR_W + 4, 
247                 COLOR_H + 4, 
248                 1);
249
250         draw_3d_border(out_color_x - 2, 
251                 out_color_y - 2, 
252                 COLOR_W + 4, 
253                 COLOR_H + 4, 
254                 1);
255
256         show_window();
257 }
258
259 void GradientWindow::update_shape()
260 {
261         int x = shape_x, y = shape_y;
262
263         if(plugin->config.shape == GradientConfig::LINEAR)
264         {
265                 delete center_x_title;
266                 delete center_y_title;
267                 delete center_x;
268                 delete center_y;
269                 center_x_title = 0;
270                 center_y_title = 0;
271                 center_x = 0;
272                 center_y = 0;
273
274                 if(!angle)
275                 {
276                         add_subwindow(angle_title = new BC_Title(x, y, _("Angle:")));
277                         add_subwindow(angle = new GradientAngle(plugin, x + angle_title->get_w() + 10, y));
278                 }
279         }
280         else
281         {
282                 delete angle_title;
283                 delete angle;
284                 angle_title = 0;
285                 angle = 0;
286                 if(!center_x)
287                 {
288                         add_subwindow(center_x_title = new BC_Title(x, y, _("Center X:")));
289                         add_subwindow(center_x = new GradientCenterX(plugin,
290                                 x + center_x_title->get_w() + 10,
291                                 y));
292                         x += center_x_title->get_w() + 10 + center_x->get_w() + 10;
293                         add_subwindow(center_y_title = new BC_Title(x, y, _("Center Y:")));
294                         add_subwindow(center_y = new GradientCenterY(plugin,
295                                 x + center_y_title->get_w() + 10,
296                                 y));
297                 }
298         }
299 }
300
301
302
303
304 void GradientWindow::update_in_color()
305 {
306 //printf("GradientWindow::update_in_color 1 %08x\n", plugin->config.get_in_color());
307         set_color(plugin->config.get_in_color());
308         draw_box(in_color_x, in_color_y, COLOR_W, COLOR_H);
309         flash(in_color_x, in_color_y, COLOR_W, COLOR_H);
310 }
311
312 void GradientWindow::update_out_color()
313 {
314 //printf("GradientWindow::update_out_color 1 %08x\n", plugin->config.get_in_color());
315         set_color(plugin->config.get_out_color());
316         draw_box(out_color_x, out_color_y, COLOR_W, COLOR_H);
317         flash(out_color_x, out_color_y, COLOR_W, COLOR_H);
318 }
319
320
321
322
323
324
325
326
327
328 GradientShape::GradientShape(GradientMain *plugin, 
329         GradientWindow *gui, 
330         int x, 
331         int y)
332  : BC_PopupMenu(x, y, 100, to_text(plugin->config.shape), 1)
333 {
334         this->plugin = plugin;
335         this->gui = gui;
336 }
337 void GradientShape::create_objects()
338 {
339         add_item(new BC_MenuItem(to_text(GradientConfig::LINEAR)));
340         add_item(new BC_MenuItem(to_text(GradientConfig::RADIAL)));
341 }
342 char* GradientShape::to_text(int shape)
343 {
344         switch(shape)
345         {
346                 case GradientConfig::LINEAR:
347                         return _("Linear");
348                 default:
349                         return _("Radial");
350         }
351 }
352 int GradientShape::from_text(char *text)
353 {
354         if(!strcmp(text, to_text(GradientConfig::LINEAR))) 
355                 return GradientConfig::LINEAR;
356         return GradientConfig::RADIAL;
357 }
358 int GradientShape::handle_event()
359 {
360         plugin->config.shape = from_text(get_text());
361         gui->update_shape();
362         plugin->send_configure_change();
363         return 1;
364 }
365
366
367
368
369 GradientCenterX::GradientCenterX(GradientMain *plugin, int x, int y)
370  : BC_FPot(x, y, plugin->config.center_x, 0, 100)
371 {
372         this->plugin = plugin;
373 }
374 int GradientCenterX::handle_event()
375 {
376         plugin->config.center_x = get_value();
377         plugin->send_configure_change();
378         return 1;
379 }
380
381
382
383 GradientCenterY::GradientCenterY(GradientMain *plugin, int x, int y)
384  : BC_FPot(x, y, plugin->config.center_y, 0, 100)
385 {
386         this->plugin = plugin;
387 }
388
389 int GradientCenterY::handle_event()
390 {
391         plugin->config.center_y = get_value();
392         plugin->send_configure_change();
393         return 1;
394 }
395
396
397
398
399 GradientAngle::GradientAngle(GradientMain *plugin, int x, int y)
400  : BC_FPot(x,
401         y,
402         plugin->config.angle,
403         -180,
404         180)
405 {
406         this->plugin = plugin;
407 }
408
409 int GradientAngle::handle_event()
410 {
411         plugin->config.angle = get_value();
412         plugin->send_configure_change();
413         return 1;
414 }
415
416
417 GradientRate::GradientRate(GradientMain *plugin, int x, int y)
418  : BC_PopupMenu(x,
419         y,
420         100,
421         to_text(plugin->config.rate),
422         1)
423 {
424         this->plugin = plugin;
425 }
426 void GradientRate::create_objects()
427 {
428         add_item(new BC_MenuItem(to_text(GradientConfig::LINEAR)));
429         add_item(new BC_MenuItem(to_text(GradientConfig::LOG)));
430         add_item(new BC_MenuItem(to_text(GradientConfig::SQUARE)));
431 }
432 char* GradientRate::to_text(int shape)
433 {
434         switch(shape)
435         {
436                 case GradientConfig::LINEAR:
437                         return _("Linear");
438                 case GradientConfig::LOG:
439                         return _("Log");
440                 default:
441                         return _("Square");
442         }
443 }
444 int GradientRate::from_text(char *text)
445 {
446         if(!strcmp(text, to_text(GradientConfig::LINEAR))) 
447                 return GradientConfig::LINEAR;
448         if(!strcmp(text, to_text(GradientConfig::LOG)))
449                 return GradientConfig::LOG;
450         return GradientConfig::SQUARE;
451 }
452 int GradientRate::handle_event()
453 {
454         plugin->config.rate = from_text(get_text());
455         plugin->send_configure_change();
456         return 1;
457 }
458
459
460
461 GradientInRadius::GradientInRadius(GradientMain *plugin, int x, int y)
462  : BC_FSlider(x,
463         y,
464         0,
465         200,
466         200,
467         (float)0,
468         (float)100,
469         (float)plugin->config.in_radius)
470 {
471         this->plugin = plugin;
472 }
473
474 int GradientInRadius::handle_event()
475 {
476         plugin->config.in_radius = get_value();
477         plugin->send_configure_change();
478         return 1;
479 }
480
481
482 GradientOutRadius::GradientOutRadius(GradientMain *plugin, int x, int y)
483  : BC_FSlider(x,
484         y,
485         0,
486         200,
487         200,
488         (float)0,
489         (float)100,
490         (float)plugin->config.out_radius)
491 {
492         this->plugin = plugin;
493 }
494
495 int GradientOutRadius::handle_event()
496 {
497         plugin->config.out_radius = get_value();
498         plugin->send_configure_change();
499         return 1;
500 }
501
502 GradientInColorButton::GradientInColorButton(GradientMain *plugin, GradientWindow *window, int x, int y)
503  : BC_GenericButton(x, y, _("Inner color:"))
504 {
505         this->plugin = plugin;
506         this->window = window;
507 }
508
509 int GradientInColorButton::handle_event()
510 {
511         window->in_color_thread->start_window(
512                 plugin->config.get_in_color(),
513                 plugin->config.in_a);
514         return 1;
515 }
516
517
518 GradientOutColorButton::GradientOutColorButton(GradientMain *plugin, GradientWindow *window, int x, int y)
519  : BC_GenericButton(x, y, _("Outer color:"))
520 {
521         this->plugin = plugin;
522         this->window = window;
523 }
524
525 int GradientOutColorButton::handle_event()
526 {
527         window->out_color_thread->start_window(
528                 plugin->config.get_out_color(),
529                 plugin->config.out_a);
530         return 1;
531 }
532
533
534
535 GradientInColorThread::GradientInColorThread(GradientMain *plugin, 
536         GradientWindow *window)
537  : ColorThread(1, _("Inner color"))
538 {
539         this->plugin = plugin;
540         this->window = window;
541 }
542
543 int GradientInColorThread::handle_new_color(int output, int alpha)
544 {
545         plugin->config.in_r = (output & 0xff0000) >> 16;
546         plugin->config.in_g = (output & 0xff00) >> 8;
547         plugin->config.in_b = (output & 0xff);
548         plugin->config.in_a = alpha;
549         
550         window->lock_window("GradientInColorThread::handle_new_color");
551         window->update_in_color();
552         window->flush();
553         window->unlock_window();
554         plugin->send_configure_change();
555 // printf("GradientInColorThread::handle_event 1 %d %d %d %d %d %d %d %d\n",
556 // plugin->config.in_r,
557 // plugin->config.in_g,
558 // plugin->config.in_b,
559 // plugin->config.in_a,
560 // plugin->config.out_r,
561 // plugin->config.out_g,
562 // plugin->config.out_b,
563 // plugin->config.out_a);
564
565         return 1;
566 }
567
568
569
570 GradientOutColorThread::GradientOutColorThread(GradientMain *plugin, 
571         GradientWindow *window)
572  : ColorThread(1, _("Outer color"))
573 {
574         this->plugin = plugin;
575         this->window = window;
576 }
577
578 int GradientOutColorThread::handle_new_color(int output, int alpha)
579 {
580         plugin->config.out_r = (output & 0xff0000) >> 16;
581         plugin->config.out_g = (output & 0xff00) >> 8;
582         plugin->config.out_b = (output & 0xff);
583         plugin->config.out_a = alpha;
584         window->lock_window("GradientOutColorThread::handle_new_color");
585         window->update_out_color();
586         window->flush();
587         window->unlock_window();
588         plugin->send_configure_change();
589 // printf("GradientOutColorThread::handle_event 1 %d %d %d %d %d %d %d %d\n",
590 // plugin->config.in_r,
591 // plugin->config.in_g,
592 // plugin->config.in_b,
593 // plugin->config.in_a,
594 // plugin->config.out_r,
595 // plugin->config.out_g,
596 // plugin->config.out_b,
597 // plugin->config.out_a);
598         return 1;
599 }
600
601
602
603
604
605
606
607
608
609
610
611
612 GradientMain::GradientMain(PluginServer *server)
613  : PluginVClient(server)
614 {
615         
616         need_reconfigure = 1;
617         gradient = 0;
618         engine = 0;
619         overlayer = 0;
620 }
621
622 GradientMain::~GradientMain()
623 {
624         
625
626         if(gradient) delete gradient;
627         if(engine) delete engine;
628         if(overlayer) delete overlayer;
629 }
630
631 const char* GradientMain::plugin_title() { return _("Gradient"); }
632 int GradientMain::is_realtime() { return 1; }
633
634
635 NEW_WINDOW_MACRO(GradientMain, GradientWindow)
636
637 LOAD_CONFIGURATION_MACRO(GradientMain, GradientConfig)
638
639 int GradientMain::is_synthesis()
640 {
641         return 1;
642 }
643
644
645 int GradientMain::process_buffer(VFrame *frame,
646         int64_t start_position,
647         double frame_rate)
648 {
649         this->input = frame;
650         this->output = frame;
651         need_reconfigure |= load_configuration();
652
653         int need_alpha = config.in_a != 0xff || config.out_a != 0xff;
654         if(need_alpha)
655                 read_frame(frame, 
656                         0, 
657                         start_position, 
658                         frame_rate,
659                         get_use_opengl());
660         if(get_use_opengl()) return run_opengl();
661
662         int gradient_cmodel = input->get_color_model();
663         if(need_alpha && BC_CModels::components(gradient_cmodel) == 3)
664         {
665                 switch(gradient_cmodel)
666                 {
667                         case BC_RGB888:
668                                 gradient_cmodel = BC_RGBA8888;
669                                 break;
670                         case BC_RGB_FLOAT:
671                                 gradient_cmodel = BC_RGBA_FLOAT;
672                                 break;
673                         case BC_YUV888:
674                                 gradient_cmodel = BC_YUVA8888;
675                                 break;
676                 }
677         }
678
679         if(gradient && gradient->get_color_model() != gradient_cmodel)
680         {
681                 delete gradient;
682                 gradient = 0;
683         }
684
685         if(!gradient) gradient = new VFrame(0, 
686                 -1,
687                 input->get_w(),
688                 input->get_h(),
689                 gradient_cmodel,
690                 -1);
691
692         if(!engine) engine = new GradientServer(this,
693                 get_project_smp() + 1,
694                 get_project_smp() + 1);
695         engine->process_packages();
696
697 // Use overlay routine in GradientServer if mismatched colormodels
698         if(gradient->get_color_model() == output->get_color_model())
699         {
700                 if(!overlayer) overlayer = new OverlayFrame(get_project_smp() + 1);
701                 overlayer->overlay(output, 
702                         gradient,
703                         0, 
704                         0, 
705                         input->get_w(), 
706                         input->get_h(),
707                         0, 
708                         0, 
709                         input->get_w(), 
710                         input->get_h(), 
711                         1.0, 
712                         TRANSFER_NORMAL,
713                         NEAREST_NEIGHBOR);
714         }
715
716
717         return 0;
718 }
719
720
721 void GradientMain::update_gui()
722 {
723         if(thread)
724         {
725                 if(load_configuration())
726                 {
727                         ((GradientWindow*)thread->window)->lock_window("GradientMain::update_gui");
728                         ((GradientWindow*)thread->window)->rate->set_text(GradientRate::to_text(config.rate));
729                         ((GradientWindow*)thread->window)->in_radius->update(config.in_radius);
730                         ((GradientWindow*)thread->window)->out_radius->update(config.out_radius);
731                         ((GradientWindow*)thread->window)->shape->set_text(GradientShape::to_text(config.shape));
732                         if(((GradientWindow*)thread->window)->angle)
733                                 ((GradientWindow*)thread->window)->angle->update(config.angle);
734                         if(((GradientWindow*)thread->window)->center_x)
735                                 ((GradientWindow*)thread->window)->center_x->update(config.center_x);
736                         if(((GradientWindow*)thread->window)->center_y)
737                                 ((GradientWindow*)thread->window)->center_y->update(config.center_y);
738                         ((GradientWindow*)thread->window)->update_in_color();
739                         ((GradientWindow*)thread->window)->update_out_color();
740                         ((GradientWindow*)thread->window)->update_shape();
741                         ((GradientWindow*)thread->window)->unlock_window();
742                         ((GradientWindow*)thread->window)->in_color_thread->update_gui(config.get_in_color(), config.in_a);
743                         ((GradientWindow*)thread->window)->out_color_thread->update_gui(config.get_out_color(), config.out_a);
744                 }
745         }
746 }
747
748
749
750
751 void GradientMain::save_data(KeyFrame *keyframe)
752 {
753         FileXML output;
754
755 // cause data to be stored directly in text
756         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
757         output.tag.set_title("GRADIENT");
758
759         output.tag.set_property("ANGLE", config.angle);
760         output.tag.set_property("IN_RADIUS", config.in_radius);
761         output.tag.set_property("OUT_RADIUS", config.out_radius);
762         output.tag.set_property("IN_R", config.in_r);
763         output.tag.set_property("IN_G", config.in_g);
764         output.tag.set_property("IN_B", config.in_b);
765         output.tag.set_property("IN_A", config.in_a);
766         output.tag.set_property("OUT_R", config.out_r);
767         output.tag.set_property("OUT_G", config.out_g);
768         output.tag.set_property("OUT_B", config.out_b);
769         output.tag.set_property("OUT_A", config.out_a);
770         output.tag.set_property("SHAPE", config.shape);
771         output.tag.set_property("RATE", config.rate);
772         output.tag.set_property("CENTER_X", config.center_x);
773         output.tag.set_property("CENTER_Y", config.center_y);
774         output.append_tag();
775         output.terminate_string();
776 }
777
778 void GradientMain::read_data(KeyFrame *keyframe)
779 {
780         FileXML input;
781
782         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
783
784         int result = 0;
785
786         while(!result)
787         {
788                 result = input.read_tag();
789
790                 if(!result)
791                 {
792                         if(input.tag.title_is("GRADIENT"))
793                         {
794                                 config.angle = input.tag.get_property("ANGLE", config.angle);
795                                 config.rate = input.tag.get_property("RATE", config.rate);
796                                 config.in_radius = input.tag.get_property("IN_RADIUS", config.in_radius);
797                                 config.out_radius = input.tag.get_property("OUT_RADIUS", config.out_radius);
798                                 config.in_r = input.tag.get_property("IN_R", config.in_r);
799                                 config.in_g = input.tag.get_property("IN_G", config.in_g);
800                                 config.in_b = input.tag.get_property("IN_B", config.in_b);
801                                 config.in_a = input.tag.get_property("IN_A", config.in_a);
802                                 config.out_r = input.tag.get_property("OUT_R", config.out_r);
803                                 config.out_g = input.tag.get_property("OUT_G", config.out_g);
804                                 config.out_b = input.tag.get_property("OUT_B", config.out_b);
805                                 config.out_a = input.tag.get_property("OUT_A", config.out_a);
806                                 config.shape = input.tag.get_property("SHAPE", config.shape);
807                                 config.center_x = input.tag.get_property("CENTER_X", config.center_x);
808                                 config.center_y = input.tag.get_property("CENTER_Y", config.center_y);
809                         }
810                 }
811         }
812 }
813
814 int GradientMain::handle_opengl()
815 {
816 #ifdef HAVE_GL
817         const char *head_frag =
818                 "uniform sampler2D tex;\n"
819                 "uniform float half_w;\n"
820                 "uniform float half_h;\n"
821                 "uniform float center_x;\n"
822                 "uniform float center_y;\n"
823                 "uniform float half_gradient_size;\n"
824                 "uniform float sin_angle;\n"
825                 "uniform float cos_angle;\n"
826                 "uniform vec4 out_color;\n"
827                 "uniform vec4 in_color;\n"
828                 "uniform float in_radius;\n"
829                 "uniform float out_radius;\n"
830                 "uniform float radius_diff;\n"
831                 "\n"
832                 "void main()\n"
833                 "{\n"
834                 "       vec2 out_coord = gl_TexCoord[0].st;\n";
835
836         const char *linear_shape = 
837                 "       vec2 in_coord = vec2(out_coord.x - half_w, half_h - out_coord.y);\n"
838                 "       float mag = half_gradient_size - \n"
839                 "               (in_coord.x * sin_angle + in_coord.y * cos_angle);\n";
840
841         const char *radial_shape =
842                 "       vec2 in_coord = vec2(out_coord.x - center_x, out_coord.y - center_y);\n"
843                 "       float mag = length(vec2(in_coord.x, in_coord.y));\n";
844
845 // No clamp function in NVidia
846         const char *linear_rate = 
847                 "       mag = min(max(mag, in_radius), out_radius);\n"
848                 "       float opacity = (mag - in_radius) / radius_diff;\n";
849
850 // NVidia warns about exp, but exp is in the GLSL spec.
851         const char *log_rate = 
852                 "       mag = max(mag, in_radius);\n"
853                 "       float opacity = 1.0 - \n"
854                 "               exp(1.0 * -(mag - in_radius) / radius_diff);\n";
855
856         const char *square_rate = 
857                 "       mag = min(max(mag, in_radius), out_radius);\n"
858                 "       float opacity = pow((mag - in_radius) / radius_diff, 2.0);\n"
859                 "       opacity = min(opacity, 1.0);\n";
860
861         const char *tail_frag = 
862                 "       vec4 color = mix(in_color, out_color, opacity);\n"
863                 "       vec4 bg_color = texture2D(tex, out_coord);\n"
864                 "       gl_FragColor.rgb = mix(bg_color.rgb, color.rgb, color.a);\n"
865                 "       gl_FragColor.a = max(bg_color.a, color.a);\n"
866                 "}\n";
867
868
869         const char *shader_stack[5] = { 0, 0, 0, 0, 0 };
870         shader_stack[0] = head_frag;
871
872         switch(config.shape)
873         {
874                 case GradientConfig::LINEAR:
875                         shader_stack[1] = linear_shape;
876                         break;
877
878                 default:
879                         shader_stack[1] = radial_shape;
880                         break;
881         }
882
883         switch(config.rate)
884         {
885                 case GradientConfig::LINEAR:
886                         shader_stack[2] = linear_rate;
887                         break;
888                 case GradientConfig::LOG:
889                         shader_stack[2] = log_rate;
890                         break;
891                 case GradientConfig::SQUARE:
892                         shader_stack[2] = square_rate;
893                         break;
894         }
895
896         shader_stack[3] = tail_frag;
897 // Force frame to create texture without copying to it if full alpha.
898         if(config.in_a >= 0xff &&
899                 config.out_a >= 0xff)
900                 get_output()->set_opengl_state(VFrame::TEXTURE);
901         get_output()->to_texture();
902         get_output()->enable_opengl();
903         get_output()->init_screen();
904         get_output()->bind_texture(0);
905
906         unsigned int frag = VFrame::make_shader(0, 
907                 shader_stack[0], 
908                 shader_stack[1], 
909                 shader_stack[2], 
910                 shader_stack[3], 
911                 0);
912
913         if(frag)
914         {
915                 glUseProgram(frag);
916                 float w = get_output()->get_w();
917                 float h = get_output()->get_h();
918                 float texture_w = get_output()->get_texture_w();
919                 float texture_h = get_output()->get_texture_h();
920                 glUniform1i(glGetUniformLocation(frag, "tex"), 0);
921                 glUniform1f(glGetUniformLocation(frag, "half_w"), w / 2 / texture_w);
922                 glUniform1f(glGetUniformLocation(frag, "half_h"), h / 2 / texture_h);
923                 if(config.shape == GradientConfig::LINEAR)
924                 {
925                         glUniform1f(glGetUniformLocation(frag, "center_x"), 
926                                 w / 2 / texture_w);
927                         glUniform1f(glGetUniformLocation(frag, "center_y"), 
928                                 h / 2 / texture_h);
929                 }
930                 else
931                 {
932                         glUniform1f(glGetUniformLocation(frag, "center_x"), 
933                                 (float)config.center_x * w / 100 / texture_w);
934                         glUniform1f(glGetUniformLocation(frag, "center_y"), 
935                                 (float)config.center_y * h / 100 / texture_h);
936                 }
937                 float gradient_size = hypotf(w / texture_w, h / texture_h);
938                 glUniform1f(glGetUniformLocation(frag, "half_gradient_size"), 
939                         gradient_size / 2);
940                 glUniform1f(glGetUniformLocation(frag, "sin_angle"), 
941                         sin(config.angle * (M_PI / 180)));
942                 glUniform1f(glGetUniformLocation(frag, "cos_angle"), 
943                         cos(config.angle * (M_PI / 180)));
944                 float in_radius = (float)config.in_radius / 100 * gradient_size;
945                 glUniform1f(glGetUniformLocation(frag, "in_radius"), in_radius);
946                 float out_radius = (float)config.out_radius / 100 * gradient_size;
947                 glUniform1f(glGetUniformLocation(frag, "out_radius"), out_radius);
948                 glUniform1f(glGetUniformLocation(frag, "radius_diff"), 
949                         out_radius - in_radius);
950
951                 switch(get_output()->get_color_model())
952                 {
953                         case BC_YUV888:
954                         case BC_YUVA8888:
955                         {
956                                 float in1, in2, in3, in4;
957                                 float out1, out2, out3, out4;
958                                 YUV::rgb_to_yuv_f((float)config.in_r / 0xff,
959                                         (float)config.in_g / 0xff,
960                                         (float)config.in_b / 0xff,
961                                         in1,
962                                         in2,
963                                         in3);
964                                 in4 = (float)config.in_a / 0xff;
965                                 YUV::rgb_to_yuv_f((float)config.out_r / 0xff,
966                                         (float)config.out_g / 0xff,
967                                         (float)config.out_b / 0xff,
968                                         out1,
969                                         out2,
970                                         out3);
971                                 in2 += 0.5;
972                                 in3 += 0.5;
973                                 out2 += 0.5;
974                                 out3 += 0.5;
975                                 out4 = (float)config.out_a / 0xff;
976                                 glUniform4f(glGetUniformLocation(frag, "out_color"), 
977                                         out1, out2, out3, out4);
978                                 glUniform4f(glGetUniformLocation(frag, "in_color"), 
979                                         in1, in2, in3, in4);
980                                 break;
981                         }
982
983                         default:
984                                 glUniform4f(glGetUniformLocation(frag, "out_color"), 
985                                         (float)config.out_r / 0xff,
986                                         (float)config.out_g / 0xff,
987                                         (float)config.out_b / 0xff,
988                                         (float)config.out_a / 0xff);
989                                 glUniform4f(glGetUniformLocation(frag, "in_color"), 
990                                         (float)config.in_r / 0xff,
991                                         (float)config.in_g / 0xff,
992                                         (float)config.in_b / 0xff,
993                                         (float)config.in_a / 0xff);
994                                 break;
995                 }
996         }
997
998         get_output()->draw_texture();
999         glUseProgram(0);
1000         get_output()->set_opengl_state(VFrame::SCREEN);
1001         
1002 #endif
1003         return 0;
1004 }
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016 GradientPackage::GradientPackage()
1017  : LoadPackage()
1018 {
1019 }
1020
1021
1022
1023
1024 GradientUnit::GradientUnit(GradientServer *server, GradientMain *plugin)
1025  : LoadClient(server)
1026 {
1027         this->plugin = plugin;
1028         this->server = server;
1029 }
1030
1031
1032
1033
1034 static float calculate_opacity(float mag, 
1035         float in_radius, float out_radius, int rate)
1036 {
1037         float opacity = 0.0;
1038         switch(rate)
1039         {
1040                 case GradientConfig::LINEAR:
1041                         if(mag < in_radius)
1042                                 opacity = 0.0;
1043                         else
1044                         if(mag >= out_radius)
1045                                 opacity = 1.0;
1046                         else
1047                                 opacity = (float)(mag - in_radius) / (out_radius - in_radius);
1048                         break;
1049
1050                 case GradientConfig::LOG:
1051                         if(mag < in_radius)
1052                                 opacity = 0;
1053                         else
1054 // Let this one decay beyond out_radius
1055                                 opacity = 1 - exp(1.0 * -(float)(mag - in_radius) /
1056                                         (out_radius - in_radius));
1057                         break;
1058
1059                 case GradientConfig::SQUARE:
1060                         if(mag < in_radius)
1061                                 opacity = 0.0; 
1062                         else
1063                         if(mag >= out_radius) 
1064                                 opacity = 1.0;
1065                         else
1066                                 opacity = powf((float)(mag - in_radius) /
1067                                         (out_radius - in_radius), 2.0);
1068                         break;
1069         }
1070         CLAMP(opacity, 0.0, 1.0);
1071         return opacity;
1072 }
1073
1074 #define CREATE_GRADIENT(type, temp, components, max) \
1075 { \
1076 /* Synthesize linear gradient for lookups */ \
1077  \
1078         r_table = malloc(sizeof(type) * gradient_size); \
1079         g_table = malloc(sizeof(type) * gradient_size); \
1080         b_table = malloc(sizeof(type) * gradient_size); \
1081         a_table = malloc(sizeof(type) * gradient_size); \
1082  \
1083         for(int i = 0; i < gradient_size; i++) \
1084         { \
1085                 float opacity = calculate_opacity(i, in_radius, out_radius, plugin->config.rate); \
1086                 float transparency; \
1087  \
1088                 transparency = 1.0 - opacity; \
1089                 ((type*)r_table)[i] = (type)(out1 * opacity + in1 * transparency); \
1090                 ((type*)g_table)[i] = (type)(out2 * opacity + in2 * transparency); \
1091                 ((type*)b_table)[i] = (type)(out3 * opacity + in3 * transparency); \
1092                 ((type*)a_table)[i] = (type)(out4 * opacity + in4 * transparency); \
1093         } \
1094  \
1095         for(int i = pkg->y1; i < pkg->y2; i++) \
1096         { \
1097                 type *gradient_row = (type*)plugin->gradient->get_rows()[i]; \
1098                 type *out_row = (type*)plugin->get_output()->get_rows()[i]; \
1099  \
1100                 switch(plugin->config.shape) \
1101                 { \
1102                         case GradientConfig::LINEAR: \
1103                                 for(int j = 0; j < w; j++) \
1104                                 { \
1105                                         int x = j - half_w; \
1106                                         int y = -(i - half_h); \
1107                  \
1108 /* Rotate by effect angle */ \
1109                                         int mag = (int)(gradient_size / 2 - \
1110                                                 (x * sin_angle + y * cos_angle) + \
1111                                                 0.5); \
1112                  \
1113 /* Get gradient value from these coords */ \
1114                  \
1115                                         if(sizeof(type) == 4) \
1116                                         { \
1117                                                 float opacity = calculate_opacity(mag,  \
1118                                                         in_radius,  \
1119                                                         out_radius, \
1120                                                         plugin->config.rate); \
1121                                                 float transparency = 1.0 - opacity; \
1122                                                 gradient_row[0] = (type)(out1 * opacity + in1 * transparency); \
1123                                                 gradient_row[1] = (type)(out2 * opacity + in2 * transparency); \
1124                                                 gradient_row[2] = (type)(out3 * opacity + in3 * transparency); \
1125                                                 if(components == 4) gradient_row[3] = (type)(out4 * opacity + in4 * transparency); \
1126                                         } \
1127                                         else \
1128                                         if(mag < 0) \
1129                                         { \
1130                                                 gradient_row[0] = out1; \
1131                                                 gradient_row[1] = out2; \
1132                                                 gradient_row[2] = out3; \
1133                                                 if(components == 4) gradient_row[3] = out4; \
1134                                         } \
1135                                         else \
1136                                         if(mag >= gradient_size) \
1137                                         { \
1138                                                 gradient_row[0] = in1; \
1139                                                 gradient_row[1] = in2; \
1140                                                 gradient_row[2] = in3; \
1141                                                 if(components == 4) gradient_row[3] = in4; \
1142                                         } \
1143                                         else \
1144                                         { \
1145                                                 gradient_row[0] = ((type*)r_table)[mag]; \
1146                                                 gradient_row[1] = ((type*)g_table)[mag]; \
1147                                                 gradient_row[2] = ((type*)b_table)[mag]; \
1148                                                 if(components == 4) gradient_row[3] = ((type*)a_table)[mag]; \
1149                                         } \
1150  \
1151 /* Overlay mixed colormodels onto output */ \
1152                                         if(gradient_cmodel != output_cmodel) \
1153                                         { \
1154                                                 temp opacity = gradient_row[3]; \
1155                                                 temp transparency = max - opacity; \
1156                                                 out_row[0] = (transparency * out_row[0] + opacity * gradient_row[0]) / max; \
1157                                                 out_row[1] = (transparency * out_row[1] + opacity * gradient_row[1]) / max; \
1158                                                 out_row[2] = (transparency * out_row[2] + opacity * gradient_row[2]) / max; \
1159                                                 out_row += 3; \
1160                                         } \
1161  \
1162                                         gradient_row += components; \
1163                                 } \
1164                                 break; \
1165  \
1166                         case GradientConfig::RADIAL: \
1167                                 for(int j = 0; j < w; j++) \
1168                                 { \
1169                                         double x = j - center_x; \
1170                                         double y = i - center_y; \
1171                                         double magnitude = hypot(x, y); \
1172                                         int mag = (int)magnitude; \
1173                                         if(sizeof(type) == 4) \
1174                                         { \
1175                                                 float opacity = calculate_opacity(mag,  \
1176                                                         in_radius,  \
1177                                                         out_radius, \
1178                                                         plugin->config.rate); \
1179                                                 float transparency = 1.0 - opacity; \
1180                                                 gradient_row[0] = (type)(out1 * opacity + in1 * transparency); \
1181                                                 gradient_row[1] = (type)(out2 * opacity + in2 * transparency); \
1182                                                 gradient_row[2] = (type)(out3 * opacity + in3 * transparency); \
1183                                                 if(components == 4) gradient_row[3] = (type)(out4 * opacity + in4 * transparency); \
1184                                         } \
1185                                         else \
1186                                         { \
1187                                                 gradient_row[0] = ((type*)r_table)[mag]; \
1188                                                 gradient_row[1] = ((type*)g_table)[mag]; \
1189                                                 gradient_row[2] = ((type*)b_table)[mag]; \
1190                                                 if(components == 4) gradient_row[3] = ((type*)a_table)[mag]; \
1191                                         } \
1192  \
1193 /* Overlay mixed colormodels onto output */ \
1194                                         if(gradient_cmodel != output_cmodel) \
1195                                         { \
1196                                                 temp opacity = gradient_row[3]; \
1197                                                 temp transparency = max - opacity; \
1198                                                 out_row[0] = (transparency * out_row[0] + opacity * gradient_row[0]) / max; \
1199                                                 out_row[1] = (transparency * out_row[1] + opacity * gradient_row[1]) / max; \
1200                                                 out_row[2] = (transparency * out_row[2] + opacity * gradient_row[2]) / max; \
1201                                                 out_row += 3; \
1202                                         } \
1203  \
1204                                         gradient_row += components; \
1205                                 } \
1206                                 break; \
1207                 } \
1208         } \
1209 }
1210
1211 void GradientUnit::process_package(LoadPackage *package)
1212 {
1213         GradientPackage *pkg = (GradientPackage*)package;
1214         int h = plugin->input->get_h();
1215         int w = plugin->input->get_w();
1216         int half_w = w / 2;
1217         int half_h = h / 2;
1218         int gradient_size = (int)(ceil(hypot(w, h)));
1219         int in_radius = (int)(plugin->config.in_radius / 100 * gradient_size);
1220         int out_radius = (int)(plugin->config.out_radius / 100 * gradient_size);
1221         double sin_angle = sin(plugin->config.angle * (M_PI / 180));
1222         double cos_angle = cos(plugin->config.angle * (M_PI / 180));
1223         double center_x = plugin->config.center_x * w / 100;
1224         double center_y = plugin->config.center_y * h / 100;
1225         void *r_table = 0;
1226         void *g_table = 0;
1227         void *b_table = 0;
1228         void *a_table = 0;
1229         int gradient_cmodel = plugin->gradient->get_color_model();
1230         int output_cmodel = plugin->get_output()->get_color_model();
1231
1232         if(in_radius > out_radius)
1233         {
1234             in_radius ^= out_radius;
1235             out_radius ^= in_radius;
1236             in_radius ^= out_radius;
1237         }
1238
1239
1240         switch(gradient_cmodel)
1241         {
1242                 case BC_RGB888:
1243                 {
1244                         int in1 = plugin->config.in_r;
1245                         int in2 = plugin->config.in_g;
1246                         int in3 = plugin->config.in_b;
1247                         int in4 = plugin->config.in_a;
1248                         int out1 = plugin->config.out_r;
1249                         int out2 = plugin->config.out_g;
1250                         int out3 = plugin->config.out_b;
1251                         int out4 = plugin->config.out_a;
1252                         CREATE_GRADIENT(unsigned char, int, 3, 0xff)
1253                         break;
1254                 }
1255
1256                 case BC_RGBA8888:
1257                 {
1258                         int in1 = plugin->config.in_r;
1259                         int in2 = plugin->config.in_g;
1260                         int in3 = plugin->config.in_b;
1261                         int in4 = plugin->config.in_a;
1262                         int out1 = plugin->config.out_r;
1263                         int out2 = plugin->config.out_g;
1264                         int out3 = plugin->config.out_b;
1265                         int out4 = plugin->config.out_a;
1266                         CREATE_GRADIENT(unsigned char, int, 4, 0xff)
1267                         break;
1268                 }
1269
1270                 case BC_RGB_FLOAT:
1271                 {
1272                         float in1 = (float)plugin->config.in_r / 0xff;
1273                         float in2 = (float)plugin->config.in_g / 0xff;
1274                         float in3 = (float)plugin->config.in_b / 0xff;
1275                         float in4 = (float)plugin->config.in_a / 0xff;
1276                         float out1 = (float)plugin->config.out_r / 0xff;
1277                         float out2 = (float)plugin->config.out_g / 0xff;
1278                         float out3 = (float)plugin->config.out_b / 0xff;
1279                         float out4 = (float)plugin->config.out_a / 0xff;
1280                         CREATE_GRADIENT(float, float, 3, 1.0)
1281                         break;
1282                 }
1283
1284                 case BC_RGBA_FLOAT:
1285                 {
1286                         float in1 = (float)plugin->config.in_r / 0xff;
1287                         float in2 = (float)plugin->config.in_g / 0xff;
1288                         float in3 = (float)plugin->config.in_b / 0xff;
1289                         float in4 = (float)plugin->config.in_a / 0xff;
1290                         float out1 = (float)plugin->config.out_r / 0xff;
1291                         float out2 = (float)plugin->config.out_g / 0xff;
1292                         float out3 = (float)plugin->config.out_b / 0xff;
1293                         float out4 = (float)plugin->config.out_a / 0xff;
1294                         CREATE_GRADIENT(float, float, 4, 1.0)
1295                         break;
1296                 }
1297
1298                 case BC_YUV888:
1299                 {
1300                         int in1, in2, in3, in4;
1301                         int out1, out2, out3, out4;
1302                         yuv.rgb_to_yuv_8(plugin->config.in_r,
1303                                 plugin->config.in_g,
1304                                 plugin->config.in_b,
1305                                 in1,
1306                                 in2,
1307                                 in3);
1308                         in4 = plugin->config.in_a;
1309                         yuv.rgb_to_yuv_8(plugin->config.out_r,
1310                                 plugin->config.out_g,
1311                                 plugin->config.out_b,
1312                                 out1,
1313                                 out2,
1314                                 out3);
1315                         out4 = plugin->config.out_a;
1316                         CREATE_GRADIENT(unsigned char, int, 3, 0xff)
1317                         break;
1318                 }
1319
1320                 case BC_YUVA8888:
1321                 {
1322                         int in1, in2, in3, in4;
1323                         int out1, out2, out3, out4;
1324                         yuv.rgb_to_yuv_8(plugin->config.in_r,
1325                                 plugin->config.in_g,
1326                                 plugin->config.in_b,
1327                                 in1,
1328                                 in2,
1329                                 in3);
1330                         in4 = plugin->config.in_a;
1331                         yuv.rgb_to_yuv_8(plugin->config.out_r,
1332                                 plugin->config.out_g,
1333                                 plugin->config.out_b,
1334                                 out1,
1335                                 out2,
1336                                 out3);
1337                         out4 = plugin->config.out_a;
1338                         CREATE_GRADIENT(unsigned char, int, 4, 0xff)
1339                         break;
1340                 }
1341         }
1342
1343         if(r_table) free(r_table);
1344         if(g_table) free(g_table);
1345         if(b_table) free(b_table);
1346         if(a_table) free(a_table);
1347 }
1348
1349
1350
1351
1352
1353
1354 GradientServer::GradientServer(GradientMain *plugin, 
1355         int total_clients, 
1356         int total_packages)
1357  : LoadServer(total_clients, total_packages)
1358 {
1359         this->plugin = plugin;
1360 }
1361
1362 void GradientServer::init_packages()
1363 {
1364         for(int i = 0; i < get_total_packages(); i++)
1365         {
1366                 GradientPackage *package = (GradientPackage*)get_package(i);
1367                 package->y1 = plugin->input->get_h() * 
1368                         i / 
1369                         get_total_packages();
1370                 package->y2 = plugin->input->get_h() * 
1371                         (i + 1) /
1372                         get_total_packages();
1373         }
1374 }
1375
1376 LoadClient* GradientServer::new_client()
1377 {
1378         return new GradientUnit(this, plugin);
1379 }
1380
1381 LoadPackage* GradientServer::new_package()
1382 {
1383         return new GradientPackage;
1384 }
1385
1386
1387
1388
1389