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