remove whitespace at eol
[goodguy/history.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 "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.tag.set_title("/GRADIENT");
776         output.append_tag();
777         output.append_newline();
778         output.terminate_string();
779 }
780
781 void GradientMain::read_data(KeyFrame *keyframe)
782 {
783         FileXML input;
784
785         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
786
787         int result = 0;
788
789         while(!result)
790         {
791                 result = input.read_tag();
792
793                 if(!result)
794                 {
795                         if(input.tag.title_is("GRADIENT"))
796                         {
797                                 config.angle = input.tag.get_property("ANGLE", config.angle);
798                                 config.rate = input.tag.get_property("RATE", config.rate);
799                                 config.in_radius = input.tag.get_property("IN_RADIUS", config.in_radius);
800                                 config.out_radius = input.tag.get_property("OUT_RADIUS", config.out_radius);
801                                 config.in_r = input.tag.get_property("IN_R", config.in_r);
802                                 config.in_g = input.tag.get_property("IN_G", config.in_g);
803                                 config.in_b = input.tag.get_property("IN_B", config.in_b);
804                                 config.in_a = input.tag.get_property("IN_A", config.in_a);
805                                 config.out_r = input.tag.get_property("OUT_R", config.out_r);
806                                 config.out_g = input.tag.get_property("OUT_G", config.out_g);
807                                 config.out_b = input.tag.get_property("OUT_B", config.out_b);
808                                 config.out_a = input.tag.get_property("OUT_A", config.out_a);
809                                 config.shape = input.tag.get_property("SHAPE", config.shape);
810                                 config.center_x = input.tag.get_property("CENTER_X", config.center_x);
811                                 config.center_y = input.tag.get_property("CENTER_Y", config.center_y);
812                         }
813                 }
814         }
815 }
816
817 int GradientMain::handle_opengl()
818 {
819 #ifdef HAVE_GL
820         const char *head_frag =
821                 "uniform sampler2D tex;\n"
822                 "uniform float half_w;\n"
823                 "uniform float half_h;\n"
824                 "uniform float center_x;\n"
825                 "uniform float center_y;\n"
826                 "uniform float half_gradient_size;\n"
827                 "uniform float sin_angle;\n"
828                 "uniform float cos_angle;\n"
829                 "uniform vec4 out_color;\n"
830                 "uniform vec4 in_color;\n"
831                 "uniform float in_radius;\n"
832                 "uniform float out_radius;\n"
833                 "uniform float radius_diff;\n"
834                 "\n"
835                 "void main()\n"
836                 "{\n"
837                 "       vec2 out_coord = gl_TexCoord[0].st;\n";
838
839         const char *linear_shape =
840                 "       vec2 in_coord = vec2(out_coord.x - half_w, half_h - out_coord.y);\n"
841                 "       float mag = half_gradient_size - \n"
842                 "               (in_coord.x * sin_angle + in_coord.y * cos_angle);\n";
843
844         const char *radial_shape =
845                 "       vec2 in_coord = vec2(out_coord.x - center_x, out_coord.y - center_y);\n"
846                 "       float mag = length(vec2(in_coord.x, in_coord.y));\n";
847
848 // No clamp function in NVidia
849         const char *linear_rate =
850                 "       mag = min(max(mag, in_radius), out_radius);\n"
851                 "       float opacity = (mag - in_radius) / radius_diff;\n";
852
853 // NVidia warns about exp, but exp is in the GLSL spec.
854         const char *log_rate =
855                 "       mag = max(mag, in_radius);\n"
856                 "       float opacity = 1.0 - \n"
857                 "               exp(1.0 * -(mag - in_radius) / radius_diff);\n";
858
859         const char *square_rate =
860                 "       mag = min(max(mag, in_radius), out_radius);\n"
861                 "       float opacity = pow((mag - in_radius) / radius_diff, 2.0);\n"
862                 "       opacity = min(opacity, 1.0);\n";
863
864         const char *tail_frag =
865                 "       vec4 color = mix(in_color, out_color, opacity);\n"
866                 "       vec4 bg_color = texture2D(tex, out_coord);\n"
867                 "       gl_FragColor.rgb = mix(bg_color.rgb, color.rgb, color.a);\n"
868                 "       gl_FragColor.a = max(bg_color.a, color.a);\n"
869                 "}\n";
870
871
872         const char *shader_stack[5] = { 0, 0, 0, 0, 0 };
873         shader_stack[0] = head_frag;
874
875         switch(config.shape)
876         {
877                 case GradientConfig::LINEAR:
878                         shader_stack[1] = linear_shape;
879                         break;
880
881                 default:
882                         shader_stack[1] = radial_shape;
883                         break;
884         }
885
886         switch(config.rate)
887         {
888                 case GradientConfig::LINEAR:
889                         shader_stack[2] = linear_rate;
890                         break;
891                 case GradientConfig::LOG:
892                         shader_stack[2] = log_rate;
893                         break;
894                 case GradientConfig::SQUARE:
895                         shader_stack[2] = square_rate;
896                         break;
897         }
898
899         shader_stack[3] = tail_frag;
900 // Force frame to create texture without copying to it if full alpha.
901         if(config.in_a >= 0xff &&
902                 config.out_a >= 0xff)
903                 get_output()->set_opengl_state(VFrame::TEXTURE);
904         get_output()->to_texture();
905         get_output()->enable_opengl();
906         get_output()->init_screen();
907         get_output()->bind_texture(0);
908
909         unsigned int frag = VFrame::make_shader(0,
910                 shader_stack[0],
911                 shader_stack[1],
912                 shader_stack[2],
913                 shader_stack[3],
914                 0);
915
916         if(frag)
917         {
918                 glUseProgram(frag);
919                 float w = get_output()->get_w();
920                 float h = get_output()->get_h();
921                 float texture_w = get_output()->get_texture_w();
922                 float texture_h = get_output()->get_texture_h();
923                 glUniform1i(glGetUniformLocation(frag, "tex"), 0);
924                 glUniform1f(glGetUniformLocation(frag, "half_w"), w / 2 / texture_w);
925                 glUniform1f(glGetUniformLocation(frag, "half_h"), h / 2 / texture_h);
926                 if(config.shape == GradientConfig::LINEAR)
927                 {
928                         glUniform1f(glGetUniformLocation(frag, "center_x"),
929                                 w / 2 / texture_w);
930                         glUniform1f(glGetUniformLocation(frag, "center_y"),
931                                 h / 2 / texture_h);
932                 }
933                 else
934                 {
935                         glUniform1f(glGetUniformLocation(frag, "center_x"),
936                                 (float)config.center_x * w / 100 / texture_w);
937                         glUniform1f(glGetUniformLocation(frag, "center_y"),
938                                 (float)config.center_y * h / 100 / texture_h);
939                 }
940                 float gradient_size = hypotf(w / texture_w, h / texture_h);
941                 glUniform1f(glGetUniformLocation(frag, "half_gradient_size"),
942                         gradient_size / 2);
943                 glUniform1f(glGetUniformLocation(frag, "sin_angle"),
944                         sin(config.angle * (M_PI / 180)));
945                 glUniform1f(glGetUniformLocation(frag, "cos_angle"),
946                         cos(config.angle * (M_PI / 180)));
947                 float in_radius = (float)config.in_radius / 100 * gradient_size;
948                 glUniform1f(glGetUniformLocation(frag, "in_radius"), in_radius);
949                 float out_radius = (float)config.out_radius / 100 * gradient_size;
950                 glUniform1f(glGetUniformLocation(frag, "out_radius"), out_radius);
951                 glUniform1f(glGetUniformLocation(frag, "radius_diff"),
952                         out_radius - in_radius);
953
954                 switch(get_output()->get_color_model())
955                 {
956                         case BC_YUV888:
957                         case BC_YUVA8888:
958                         {
959                                 float in1, in2, in3, in4;
960                                 float out1, out2, out3, out4;
961                                 YUV::rgb_to_yuv_f((float)config.in_r / 0xff,
962                                         (float)config.in_g / 0xff,
963                                         (float)config.in_b / 0xff,
964                                         in1,
965                                         in2,
966                                         in3);
967                                 in4 = (float)config.in_a / 0xff;
968                                 YUV::rgb_to_yuv_f((float)config.out_r / 0xff,
969                                         (float)config.out_g / 0xff,
970                                         (float)config.out_b / 0xff,
971                                         out1,
972                                         out2,
973                                         out3);
974                                 in2 += 0.5;
975                                 in3 += 0.5;
976                                 out2 += 0.5;
977                                 out3 += 0.5;
978                                 out4 = (float)config.out_a / 0xff;
979                                 glUniform4f(glGetUniformLocation(frag, "out_color"),
980                                         out1, out2, out3, out4);
981                                 glUniform4f(glGetUniformLocation(frag, "in_color"),
982                                         in1, in2, in3, in4);
983                                 break;
984                         }
985
986                         default:
987                                 glUniform4f(glGetUniformLocation(frag, "out_color"),
988                                         (float)config.out_r / 0xff,
989                                         (float)config.out_g / 0xff,
990                                         (float)config.out_b / 0xff,
991                                         (float)config.out_a / 0xff);
992                                 glUniform4f(glGetUniformLocation(frag, "in_color"),
993                                         (float)config.in_r / 0xff,
994                                         (float)config.in_g / 0xff,
995                                         (float)config.in_b / 0xff,
996                                         (float)config.in_a / 0xff);
997                                 break;
998                 }
999         }
1000
1001         get_output()->draw_texture();
1002         glUseProgram(0);
1003         get_output()->set_opengl_state(VFrame::SCREEN);
1004
1005 #endif
1006         return 0;
1007 }
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019 GradientPackage::GradientPackage()
1020  : LoadPackage()
1021 {
1022 }
1023
1024
1025
1026
1027 GradientUnit::GradientUnit(GradientServer *server, GradientMain *plugin)
1028  : LoadClient(server)
1029 {
1030         this->plugin = plugin;
1031         this->server = server;
1032 }
1033
1034
1035
1036
1037 static float calculate_opacity(float mag,
1038         float in_radius, float out_radius, int rate)
1039 {
1040         float opacity = 0.0;
1041         switch(rate)
1042         {
1043                 case GradientConfig::LINEAR:
1044                         if(mag < in_radius)
1045                                 opacity = 0.0;
1046                         else
1047                         if(mag >= out_radius)
1048                                 opacity = 1.0;
1049                         else
1050                                 opacity = (float)(mag - in_radius) / (out_radius - in_radius);
1051                         break;
1052
1053                 case GradientConfig::LOG:
1054                         if(mag < in_radius)
1055                                 opacity = 0;
1056                         else
1057 // Let this one decay beyond out_radius
1058                                 opacity = 1 - exp(1.0 * -(float)(mag - in_radius) /
1059                                         (out_radius - in_radius));
1060                         break;
1061
1062                 case GradientConfig::SQUARE:
1063                         if(mag < in_radius)
1064                                 opacity = 0.0;
1065                         else
1066                         if(mag >= out_radius)
1067                                 opacity = 1.0;
1068                         else
1069                                 opacity = powf((float)(mag - in_radius) /
1070                                         (out_radius - in_radius), 2.0);
1071                         break;
1072         }
1073         CLAMP(opacity, 0.0, 1.0);
1074         return opacity;
1075 }
1076
1077 #define CREATE_GRADIENT(type, temp, components, max) \
1078 { \
1079 /* Synthesize linear gradient for lookups */ \
1080  \
1081         r_table = malloc(sizeof(type) * gradient_size); \
1082         g_table = malloc(sizeof(type) * gradient_size); \
1083         b_table = malloc(sizeof(type) * gradient_size); \
1084         a_table = malloc(sizeof(type) * gradient_size); \
1085  \
1086         for(int i = 0; i < gradient_size; i++) \
1087         { \
1088                 float opacity = calculate_opacity(i, in_radius, out_radius, plugin->config.rate); \
1089                 float transparency; \
1090  \
1091                 transparency = 1.0 - opacity; \
1092                 ((type*)r_table)[i] = (type)(out1 * opacity + in1 * transparency); \
1093                 ((type*)g_table)[i] = (type)(out2 * opacity + in2 * transparency); \
1094                 ((type*)b_table)[i] = (type)(out3 * opacity + in3 * transparency); \
1095                 ((type*)a_table)[i] = (type)(out4 * opacity + in4 * transparency); \
1096         } \
1097  \
1098         for(int i = pkg->y1; i < pkg->y2; i++) \
1099         { \
1100                 type *gradient_row = (type*)plugin->gradient->get_rows()[i]; \
1101                 type *out_row = (type*)plugin->get_output()->get_rows()[i]; \
1102  \
1103                 switch(plugin->config.shape) \
1104                 { \
1105                         case GradientConfig::LINEAR: \
1106                                 for(int j = 0; j < w; j++) \
1107                                 { \
1108                                         int x = j - half_w; \
1109                                         int y = -(i - half_h); \
1110                  \
1111 /* Rotate by effect angle */ \
1112                                         int mag = (int)(gradient_size / 2 - \
1113                                                 (x * sin_angle + y * cos_angle) + \
1114                                                 0.5); \
1115                  \
1116 /* Get gradient value from these coords */ \
1117                  \
1118                                         if(sizeof(type) == 4) \
1119                                         { \
1120                                                 float opacity = calculate_opacity(mag,  \
1121                                                         in_radius,  \
1122                                                         out_radius, \
1123                                                         plugin->config.rate); \
1124                                                 float transparency = 1.0 - opacity; \
1125                                                 gradient_row[0] = (type)(out1 * opacity + in1 * transparency); \
1126                                                 gradient_row[1] = (type)(out2 * opacity + in2 * transparency); \
1127                                                 gradient_row[2] = (type)(out3 * opacity + in3 * transparency); \
1128                                                 if(components == 4) gradient_row[3] = (type)(out4 * opacity + in4 * transparency); \
1129                                         } \
1130                                         else \
1131                                         if(mag < 0) \
1132                                         { \
1133                                                 gradient_row[0] = out1; \
1134                                                 gradient_row[1] = out2; \
1135                                                 gradient_row[2] = out3; \
1136                                                 if(components == 4) gradient_row[3] = out4; \
1137                                         } \
1138                                         else \
1139                                         if(mag >= gradient_size) \
1140                                         { \
1141                                                 gradient_row[0] = in1; \
1142                                                 gradient_row[1] = in2; \
1143                                                 gradient_row[2] = in3; \
1144                                                 if(components == 4) gradient_row[3] = in4; \
1145                                         } \
1146                                         else \
1147                                         { \
1148                                                 gradient_row[0] = ((type*)r_table)[mag]; \
1149                                                 gradient_row[1] = ((type*)g_table)[mag]; \
1150                                                 gradient_row[2] = ((type*)b_table)[mag]; \
1151                                                 if(components == 4) gradient_row[3] = ((type*)a_table)[mag]; \
1152                                         } \
1153  \
1154 /* Overlay mixed colormodels onto output */ \
1155                                         if(gradient_cmodel != output_cmodel) \
1156                                         { \
1157                                                 temp opacity = gradient_row[3]; \
1158                                                 temp transparency = max - opacity; \
1159                                                 out_row[0] = (transparency * out_row[0] + opacity * gradient_row[0]) / max; \
1160                                                 out_row[1] = (transparency * out_row[1] + opacity * gradient_row[1]) / max; \
1161                                                 out_row[2] = (transparency * out_row[2] + opacity * gradient_row[2]) / max; \
1162                                                 out_row += 3; \
1163                                         } \
1164  \
1165                                         gradient_row += components; \
1166                                 } \
1167                                 break; \
1168  \
1169                         case GradientConfig::RADIAL: \
1170                                 for(int j = 0; j < w; j++) \
1171                                 { \
1172                                         double x = j - center_x; \
1173                                         double y = i - center_y; \
1174                                         double magnitude = hypot(x, y); \
1175                                         int mag = (int)magnitude; \
1176                                         if(sizeof(type) == 4) \
1177                                         { \
1178                                                 float opacity = calculate_opacity(mag,  \
1179                                                         in_radius,  \
1180                                                         out_radius, \
1181                                                         plugin->config.rate); \
1182                                                 float transparency = 1.0 - opacity; \
1183                                                 gradient_row[0] = (type)(out1 * opacity + in1 * transparency); \
1184                                                 gradient_row[1] = (type)(out2 * opacity + in2 * transparency); \
1185                                                 gradient_row[2] = (type)(out3 * opacity + in3 * transparency); \
1186                                                 if(components == 4) gradient_row[3] = (type)(out4 * opacity + in4 * transparency); \
1187                                         } \
1188                                         else \
1189                                         { \
1190                                                 gradient_row[0] = ((type*)r_table)[mag]; \
1191                                                 gradient_row[1] = ((type*)g_table)[mag]; \
1192                                                 gradient_row[2] = ((type*)b_table)[mag]; \
1193                                                 if(components == 4) gradient_row[3] = ((type*)a_table)[mag]; \
1194                                         } \
1195  \
1196 /* Overlay mixed colormodels onto output */ \
1197                                         if(gradient_cmodel != output_cmodel) \
1198                                         { \
1199                                                 temp opacity = gradient_row[3]; \
1200                                                 temp transparency = max - opacity; \
1201                                                 out_row[0] = (transparency * out_row[0] + opacity * gradient_row[0]) / max; \
1202                                                 out_row[1] = (transparency * out_row[1] + opacity * gradient_row[1]) / max; \
1203                                                 out_row[2] = (transparency * out_row[2] + opacity * gradient_row[2]) / max; \
1204                                                 out_row += 3; \
1205                                         } \
1206  \
1207                                         gradient_row += components; \
1208                                 } \
1209                                 break; \
1210                 } \
1211         } \
1212 }
1213
1214 void GradientUnit::process_package(LoadPackage *package)
1215 {
1216         GradientPackage *pkg = (GradientPackage*)package;
1217         int h = plugin->input->get_h();
1218         int w = plugin->input->get_w();
1219         int half_w = w / 2;
1220         int half_h = h / 2;
1221         int gradient_size = (int)(ceil(hypot(w, h)));
1222         int in_radius = (int)(plugin->config.in_radius / 100 * gradient_size);
1223         int out_radius = (int)(plugin->config.out_radius / 100 * gradient_size);
1224         double sin_angle = sin(plugin->config.angle * (M_PI / 180));
1225         double cos_angle = cos(plugin->config.angle * (M_PI / 180));
1226         double center_x = plugin->config.center_x * w / 100;
1227         double center_y = plugin->config.center_y * h / 100;
1228         void *r_table = 0;
1229         void *g_table = 0;
1230         void *b_table = 0;
1231         void *a_table = 0;
1232         int gradient_cmodel = plugin->gradient->get_color_model();
1233         int output_cmodel = plugin->get_output()->get_color_model();
1234
1235         if(in_radius > out_radius)
1236         {
1237             in_radius ^= out_radius;
1238             out_radius ^= in_radius;
1239             in_radius ^= out_radius;
1240         }
1241
1242
1243         switch(gradient_cmodel)
1244         {
1245                 case BC_RGB888:
1246                 {
1247                         int in1 = plugin->config.in_r;
1248                         int in2 = plugin->config.in_g;
1249                         int in3 = plugin->config.in_b;
1250                         int in4 = plugin->config.in_a;
1251                         int out1 = plugin->config.out_r;
1252                         int out2 = plugin->config.out_g;
1253                         int out3 = plugin->config.out_b;
1254                         int out4 = plugin->config.out_a;
1255                         CREATE_GRADIENT(unsigned char, int, 3, 0xff)
1256                         break;
1257                 }
1258
1259                 case BC_RGBA8888:
1260                 {
1261                         int in1 = plugin->config.in_r;
1262                         int in2 = plugin->config.in_g;
1263                         int in3 = plugin->config.in_b;
1264                         int in4 = plugin->config.in_a;
1265                         int out1 = plugin->config.out_r;
1266                         int out2 = plugin->config.out_g;
1267                         int out3 = plugin->config.out_b;
1268                         int out4 = plugin->config.out_a;
1269                         CREATE_GRADIENT(unsigned char, int, 4, 0xff)
1270                         break;
1271                 }
1272
1273                 case BC_RGB_FLOAT:
1274                 {
1275                         float in1 = (float)plugin->config.in_r / 0xff;
1276                         float in2 = (float)plugin->config.in_g / 0xff;
1277                         float in3 = (float)plugin->config.in_b / 0xff;
1278                         float in4 = (float)plugin->config.in_a / 0xff;
1279                         float out1 = (float)plugin->config.out_r / 0xff;
1280                         float out2 = (float)plugin->config.out_g / 0xff;
1281                         float out3 = (float)plugin->config.out_b / 0xff;
1282                         float out4 = (float)plugin->config.out_a / 0xff;
1283                         CREATE_GRADIENT(float, float, 3, 1.0)
1284                         break;
1285                 }
1286
1287                 case BC_RGBA_FLOAT:
1288                 {
1289                         float in1 = (float)plugin->config.in_r / 0xff;
1290                         float in2 = (float)plugin->config.in_g / 0xff;
1291                         float in3 = (float)plugin->config.in_b / 0xff;
1292                         float in4 = (float)plugin->config.in_a / 0xff;
1293                         float out1 = (float)plugin->config.out_r / 0xff;
1294                         float out2 = (float)plugin->config.out_g / 0xff;
1295                         float out3 = (float)plugin->config.out_b / 0xff;
1296                         float out4 = (float)plugin->config.out_a / 0xff;
1297                         CREATE_GRADIENT(float, float, 4, 1.0)
1298                         break;
1299                 }
1300
1301                 case BC_YUV888:
1302                 {
1303                         int in1, in2, in3, in4;
1304                         int out1, out2, out3, out4;
1305                         yuv.rgb_to_yuv_8(plugin->config.in_r,
1306                                 plugin->config.in_g,
1307                                 plugin->config.in_b,
1308                                 in1,
1309                                 in2,
1310                                 in3);
1311                         in4 = plugin->config.in_a;
1312                         yuv.rgb_to_yuv_8(plugin->config.out_r,
1313                                 plugin->config.out_g,
1314                                 plugin->config.out_b,
1315                                 out1,
1316                                 out2,
1317                                 out3);
1318                         out4 = plugin->config.out_a;
1319                         CREATE_GRADIENT(unsigned char, int, 3, 0xff)
1320                         break;
1321                 }
1322
1323                 case BC_YUVA8888:
1324                 {
1325                         int in1, in2, in3, in4;
1326                         int out1, out2, out3, out4;
1327                         yuv.rgb_to_yuv_8(plugin->config.in_r,
1328                                 plugin->config.in_g,
1329                                 plugin->config.in_b,
1330                                 in1,
1331                                 in2,
1332                                 in3);
1333                         in4 = plugin->config.in_a;
1334                         yuv.rgb_to_yuv_8(plugin->config.out_r,
1335                                 plugin->config.out_g,
1336                                 plugin->config.out_b,
1337                                 out1,
1338                                 out2,
1339                                 out3);
1340                         out4 = plugin->config.out_a;
1341                         CREATE_GRADIENT(unsigned char, int, 4, 0xff)
1342                         break;
1343                 }
1344         }
1345
1346         if(r_table) free(r_table);
1347         if(g_table) free(g_table);
1348         if(b_table) free(b_table);
1349         if(a_table) free(a_table);
1350 }
1351
1352
1353
1354
1355
1356
1357 GradientServer::GradientServer(GradientMain *plugin,
1358         int total_clients,
1359         int total_packages)
1360  : LoadServer(total_clients, total_packages)
1361 {
1362         this->plugin = plugin;
1363 }
1364
1365 void GradientServer::init_packages()
1366 {
1367         for(int i = 0; i < get_total_packages(); i++)
1368         {
1369                 GradientPackage *package = (GradientPackage*)get_package(i);
1370                 package->y1 = plugin->input->get_h() *
1371                         i /
1372                         get_total_packages();
1373                 package->y2 = plugin->input->get_h() *
1374                         (i + 1) /
1375                         get_total_packages();
1376         }
1377 }
1378
1379 LoadClient* GradientServer::new_client()
1380 {
1381         return new GradientUnit(this, plugin);
1382 }
1383
1384 LoadPackage* GradientServer::new_package()
1385 {
1386         return new GradientPackage;
1387 }
1388
1389
1390
1391
1392