7f141409e1126ed80537ef4c95f027aed6f9d49f
[goodguy/history.git] / cinelerra-5.1 / plugins / radialblur / radialblur.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
27 #include "affine.h"
28 #include "bcdisplayinfo.h"
29 #include "clip.h"
30 #include "bchash.h"
31 #include "filexml.h"
32 #include "keyframe.h"
33 #include "language.h"
34 #include "loadbalance.h"
35 #include "pluginvclient.h"
36 #include "vframe.h"
37
38
39 class RadialBlurMain;
40 class RadialBlurEngine;
41
42
43
44
45
46 class RadialBlurConfig
47 {
48 public:
49         RadialBlurConfig();
50
51         int equivalent(RadialBlurConfig &that);
52         void copy_from(RadialBlurConfig &that);
53         void interpolate(RadialBlurConfig &prev,
54                 RadialBlurConfig &next,
55                 long prev_frame,
56                 long next_frame,
57                 long current_frame);
58
59         int x;
60         int y;
61         int steps;
62         int angle;
63         int r;
64         int g;
65         int b;
66         int a;
67 };
68
69
70
71 class RadialBlurSize : public BC_ISlider
72 {
73 public:
74         RadialBlurSize(RadialBlurMain *plugin,
75                 int x,
76                 int y,
77                 int *output,
78                 int min,
79                 int max);
80         int handle_event();
81         RadialBlurMain *plugin;
82         int *output;
83 };
84
85 class RadialBlurToggle : public BC_CheckBox
86 {
87 public:
88         RadialBlurToggle(RadialBlurMain *plugin,
89                 int x,
90                 int y,
91                 int *output,
92                 char *string);
93         int handle_event();
94         RadialBlurMain *plugin;
95         int *output;
96 };
97
98 class RadialBlurWindow : public PluginClientWindow
99 {
100 public:
101         RadialBlurWindow(RadialBlurMain *plugin);
102         ~RadialBlurWindow();
103
104         void create_objects();
105
106
107         RadialBlurSize *x, *y, *steps, *angle;
108         RadialBlurToggle *r, *g, *b, *a;
109         RadialBlurMain *plugin;
110 };
111
112
113
114
115
116
117 class RadialBlurMain : public PluginVClient
118 {
119 public:
120         RadialBlurMain(PluginServer *server);
121         ~RadialBlurMain();
122
123         int process_buffer(VFrame *frame,
124                 int64_t start_position,
125                 double frame_rate);
126         int is_realtime();
127         void save_data(KeyFrame *keyframe);
128         void read_data(KeyFrame *keyframe);
129         void update_gui();
130         int handle_opengl();
131
132         PLUGIN_CLASS_MEMBERS(RadialBlurConfig)
133
134         VFrame *input, *output, *temp;
135         RadialBlurEngine *engine;
136 // Rotate engine only used for OpenGL
137         AffineEngine *rotate;
138 };
139
140 class RadialBlurPackage : public LoadPackage
141 {
142 public:
143         RadialBlurPackage();
144         int y1, y2;
145 };
146
147 class RadialBlurUnit : public LoadClient
148 {
149 public:
150         RadialBlurUnit(RadialBlurEngine *server, RadialBlurMain *plugin);
151         void process_package(LoadPackage *package);
152         RadialBlurEngine *server;
153         RadialBlurMain *plugin;
154 };
155
156 class RadialBlurEngine : public LoadServer
157 {
158 public:
159         RadialBlurEngine(RadialBlurMain *plugin,
160                 int total_clients,
161                 int total_packages);
162         void init_packages();
163         LoadClient* new_client();
164         LoadPackage* new_package();
165         RadialBlurMain *plugin;
166 };
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 REGISTER_PLUGIN(RadialBlurMain)
187
188
189
190 RadialBlurConfig::RadialBlurConfig()
191 {
192         x = 50;
193         y = 50;
194         steps = 10;
195         angle = 33;
196         r = 1;
197         g = 1;
198         b = 1;
199         a = 1;
200 }
201
202 int RadialBlurConfig::equivalent(RadialBlurConfig &that)
203 {
204         return
205                 angle == that.angle &&
206                 x == that.x &&
207                 y == that.y &&
208                 steps == that.steps &&
209                 r == that.r &&
210                 g == that.g &&
211                 b == that.b &&
212                 a == that.a;
213 }
214
215 void RadialBlurConfig::copy_from(RadialBlurConfig &that)
216 {
217         x = that.x;
218         y = that.y;
219         angle = that.angle;
220         steps = that.steps;
221         r = that.r;
222         g = that.g;
223         b = that.b;
224         a = that.a;
225 }
226
227 void RadialBlurConfig::interpolate(RadialBlurConfig &prev,
228         RadialBlurConfig &next,
229         long prev_frame,
230         long next_frame,
231         long current_frame)
232 {
233         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
234         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
235         this->x = (int)(prev.x * prev_scale + next.x * next_scale + 0.5);
236         this->y = (int)(prev.y * prev_scale + next.y * next_scale + 0.5);
237         this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale + 0.5);
238         this->angle = (int)(prev.angle * prev_scale + next.angle * next_scale + 0.5);
239         r = prev.r;
240         g = prev.g;
241         b = prev.b;
242         a = prev.a;
243 }
244
245
246
247
248
249
250
251
252
253
254
255
256
257 RadialBlurWindow::RadialBlurWindow(RadialBlurMain *plugin)
258  : PluginClientWindow(plugin,
259         230,
260         340,
261         230,
262         340,
263         0)
264 {
265         this->plugin = plugin;
266 }
267
268 RadialBlurWindow::~RadialBlurWindow()
269 {
270 }
271
272 void RadialBlurWindow::create_objects()
273 {
274         int x = 10, y = 10;
275
276         add_subwindow(new BC_Title(x, y, _("X:")));
277         y += 20;
278         add_subwindow(this->x = new RadialBlurSize(plugin, x, y, &plugin->config.x, 0, 100));
279         y += 30;
280         add_subwindow(new BC_Title(x, y, _("Y:")));
281         y += 20;
282         add_subwindow(this->y = new RadialBlurSize(plugin, x, y, &plugin->config.y, 0, 100));
283         y += 30;
284         add_subwindow(new BC_Title(x, y, _("Angle:")));
285         y += 20;
286         add_subwindow(angle = new RadialBlurSize(plugin, x, y, &plugin->config.angle, 0, 360));
287         y += 30;
288         add_subwindow(new BC_Title(x, y, _("Steps:")));
289         y += 20;
290         add_subwindow(steps = new RadialBlurSize(plugin, x, y, &plugin->config.steps, 1, 100));
291         y += 30;
292         add_subwindow(r = new RadialBlurToggle(plugin, x, y, &plugin->config.r, _("Red")));
293         y += 30;
294         add_subwindow(g = new RadialBlurToggle(plugin, x, y, &plugin->config.g, _("Green")));
295         y += 30;
296         add_subwindow(b = new RadialBlurToggle(plugin, x, y, &plugin->config.b, _("Blue")));
297         y += 30;
298         add_subwindow(a = new RadialBlurToggle(plugin, x, y, &plugin->config.a, _("Alpha")));
299         y += 30;
300
301         show_window();
302         flush();
303 }
304
305
306
307
308
309
310
311
312
313
314
315 RadialBlurToggle::RadialBlurToggle(RadialBlurMain *plugin,
316         int x,
317         int y,
318         int *output,
319         char *string)
320  : BC_CheckBox(x, y, *output, string)
321 {
322         this->plugin = plugin;
323         this->output = output;
324 }
325
326 int RadialBlurToggle::handle_event()
327 {
328         *output = get_value();
329         plugin->send_configure_change();
330         return 1;
331 }
332
333
334
335
336
337
338
339 RadialBlurSize::RadialBlurSize(RadialBlurMain *plugin,
340         int x,
341         int y,
342         int *output,
343         int min,
344         int max)
345  : BC_ISlider(x, y, 0, 200, 200, min, max, *output)
346 {
347         this->plugin = plugin;
348         this->output = output;
349 }
350 int RadialBlurSize::handle_event()
351 {
352         *output = get_value();
353         plugin->send_configure_change();
354         return 1;
355 }
356
357
358
359
360
361
362
363
364
365
366 RadialBlurMain::RadialBlurMain(PluginServer *server)
367  : PluginVClient(server)
368 {
369
370         engine = 0;
371         temp = 0;
372         rotate = 0;
373 }
374
375 RadialBlurMain::~RadialBlurMain()
376 {
377
378         if(engine) delete engine;
379         if(temp) delete temp;
380         delete rotate;
381 }
382
383 const char* RadialBlurMain::plugin_title() { return _("Radial Blur"); }
384 int RadialBlurMain::is_realtime() { return 1; }
385
386
387
388 NEW_WINDOW_MACRO(RadialBlurMain, RadialBlurWindow)
389
390 LOAD_CONFIGURATION_MACRO(RadialBlurMain, RadialBlurConfig)
391
392 int RadialBlurMain::process_buffer(VFrame *frame,
393                                                         int64_t start_position,
394                                                         double frame_rate)
395 {
396         load_configuration();
397
398
399         read_frame(frame,
400                 0,
401                 get_source_position(),
402                 get_framerate(),
403 //              0);
404                 get_use_opengl());
405
406         if(get_use_opengl()) return run_opengl();
407
408         if(!engine) engine = new RadialBlurEngine(this,
409                 get_project_smp() + 1,
410                 get_project_smp() + 1);
411
412         this->input = frame;
413         this->output = frame;
414
415
416         if(!temp) temp = new VFrame(0,
417                 -1,
418                 frame->get_w(),
419                 frame->get_h(),
420                 frame->get_color_model(),
421                 -1);
422         temp->copy_from(frame);
423         this->input = temp;
424
425         engine->process_packages();
426         return 0;
427 }
428
429
430 void RadialBlurMain::update_gui()
431 {
432         if(thread)
433         {
434                 load_configuration();
435                 thread->window->lock_window();
436                 ((RadialBlurWindow*)thread->window)->x->update(config.x);
437                 ((RadialBlurWindow*)thread->window)->y->update(config.y);
438                 ((RadialBlurWindow*)thread->window)->angle->update(config.angle);
439                 ((RadialBlurWindow*)thread->window)->steps->update(config.steps);
440                 ((RadialBlurWindow*)thread->window)->r->update(config.r);
441                 ((RadialBlurWindow*)thread->window)->g->update(config.g);
442                 ((RadialBlurWindow*)thread->window)->b->update(config.b);
443                 ((RadialBlurWindow*)thread->window)->a->update(config.a);
444                 thread->window->unlock_window();
445         }
446 }
447
448
449
450
451 void RadialBlurMain::save_data(KeyFrame *keyframe)
452 {
453         FileXML output;
454
455 // cause data to be stored directly in text
456         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
457         output.tag.set_title("RADIALBLUR");
458
459         output.tag.set_property("X", config.x);
460         output.tag.set_property("Y", config.y);
461         output.tag.set_property("ANGLE", config.angle);
462         output.tag.set_property("STEPS", config.steps);
463         output.tag.set_property("R", config.r);
464         output.tag.set_property("G", config.g);
465         output.tag.set_property("B", config.b);
466         output.tag.set_property("A", config.a);
467         output.append_tag();
468         output.tag.set_title("/RADIALBLUR");
469         output.append_tag();
470         output.append_newline();
471         output.terminate_string();
472 }
473
474 void RadialBlurMain::read_data(KeyFrame *keyframe)
475 {
476         FileXML input;
477
478         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
479
480         int result = 0;
481
482         while(!result)
483         {
484                 result = input.read_tag();
485
486                 if(!result)
487                 {
488                         if(input.tag.title_is("RADIALBLUR"))
489                         {
490                                 config.x = input.tag.get_property("X", config.x);
491                                 config.y = input.tag.get_property("Y", config.y);
492                                 config.angle = input.tag.get_property("ANGLE", config.angle);
493                                 config.steps = input.tag.get_property("STEPS", config.steps);
494                                 config.r = input.tag.get_property("R", config.r);
495                                 config.g = input.tag.get_property("G", config.g);
496                                 config.b = input.tag.get_property("B", config.b);
497                                 config.a = input.tag.get_property("A", config.a);
498                         }
499                 }
500         }
501 }
502
503 int RadialBlurMain::handle_opengl()
504 {
505 #ifdef HAVE_GL
506         get_output()->to_texture();
507         get_output()->enable_opengl();
508         get_output()->init_screen();
509         get_output()->bind_texture(0);
510
511
512         //int is_yuv = BC_CModels::is_yuv(get_output()->get_color_model());
513         glClearColor(0.0, 0.0, 0.0, 0.0);
514         glClear(GL_COLOR_BUFFER_BIT);
515
516 // Draw unselected channels
517         glEnable(GL_BLEND);
518         glBlendFunc(GL_ONE, GL_ONE);
519         glDrawBuffer(GL_BACK);
520         if(!config.r || !config.g || !config.b || !config.a)
521         {
522                 glColor4f(config.r ? 0 : 1,
523                         config.g ? 0 : 1,
524                         config.b ? 0 : 1,
525                         config.a ? 0 : 1);
526                 get_output()->draw_texture();
527         }
528         glAccum(GL_LOAD, 1.0);
529
530
531 // Blur selected channels
532         float fraction = 1.0 / config.steps;
533         for(int i = 0; i < config.steps; i++)
534         {
535                 get_output()->set_opengl_state(VFrame::TEXTURE);
536                 glClear(GL_COLOR_BUFFER_BIT);
537                 glColor4f(config.r ? 1 : 0,
538                         config.g ? 1 : 0,
539                         config.b ? 1 : 0,
540                         config.a ? 1 : 0);
541
542                 float w = get_output()->get_w();
543                 float h = get_output()->get_h();
544
545
546
547                 double current_angle = (double)config.angle *
548                         i /
549                         config.steps -
550                         (double)config.angle / 2;
551
552                 if(!rotate) rotate = new AffineEngine(PluginClient::smp + 1,
553                         PluginClient::smp + 1);
554                 rotate->set_in_pivot((int)(config.x * w / 100),
555                         (int)(config.y * h / 100));
556                 rotate->set_out_pivot((int)(config.x * w / 100),
557                         (int)(config.y * h / 100));
558                 rotate->set_opengl(1);
559                 rotate->rotate(get_output(),
560                         get_output(),
561                         current_angle);
562
563                 glAccum(GL_ACCUM, fraction);
564                 glEnable(GL_TEXTURE_2D);
565                 glColor4f(config.r ? 1 : 0,
566                         config.g ? 1 : 0,
567                         config.b ? 1 : 0,
568                         config.a ? 1 : 0);
569         }
570
571
572         glDisable(GL_BLEND);
573         glReadBuffer(GL_BACK);
574         glDisable(GL_TEXTURE_2D);
575         glAccum(GL_RETURN, 1.0);
576
577         glColor4f(1, 1, 1, 1);
578         get_output()->set_opengl_state(VFrame::SCREEN);
579 #endif
580         return 0;
581 }
582
583
584
585
586
587
588
589
590
591
592
593 RadialBlurPackage::RadialBlurPackage()
594  : LoadPackage()
595 {
596 }
597
598
599 RadialBlurUnit::RadialBlurUnit(RadialBlurEngine *server,
600         RadialBlurMain *plugin)
601  : LoadClient(server)
602 {
603         this->plugin = plugin;
604         this->server = server;
605 }
606
607
608 #define BLEND_LAYER(COMPONENTS, TYPE, TEMP_TYPE, MAX, DO_YUV) \
609 { \
610         int chroma_offset = (DO_YUV ? ((MAX + 1) / 2) : 0); \
611         TYPE **in_rows = (TYPE**)plugin->input->get_rows(); \
612         TYPE **out_rows = (TYPE**)plugin->output->get_rows(); \
613         int steps = plugin->config.steps; \
614         double step = (double)plugin->config.angle / 360 * 2 * M_PI / steps; \
615  \
616         for(int i = pkg->y1, out_y = pkg->y1 - center_y; \
617                 i < pkg->y2; \
618                 i++, out_y++) \
619         { \
620                 TYPE *out_row = out_rows[i]; \
621                 TYPE *in_row = in_rows[i]; \
622                 int y_square = out_y * out_y; \
623  \
624                 for(int j = 0, out_x = -center_x; j < w; j++, out_x++) \
625                 { \
626                         TEMP_TYPE accum_r = 0; \
627                         TEMP_TYPE accum_g = 0; \
628                         TEMP_TYPE accum_b = 0; \
629                         TEMP_TYPE accum_a = 0; \
630  \
631 /* Output coordinate to polar */ \
632                         double magnitude = sqrt(y_square + out_x * out_x); \
633                         double angle; \
634                         if(out_y < 0) \
635                                 angle = atan((double)out_x / out_y) + M_PI; \
636                         else \
637                         if(out_y > 0) \
638                                 angle = atan((double)out_x / out_y); \
639                         else \
640                         if(out_x > 0) \
641                                 angle = M_PI / 2; \
642                         else \
643                                 angle = M_PI * 1.5; \
644  \
645 /* Overlay all steps on this pixel*/ \
646                         angle -= (double)plugin->config.angle / 360 * M_PI; \
647                         for(int k = 0; k < steps; k++, angle += step) \
648                         { \
649 /* Polar to input coordinate */ \
650                                 int in_x = (int)(magnitude * sin(angle)) + center_x; \
651                                 int in_y = (int)(magnitude * cos(angle)) + center_y; \
652  \
653 /* Accumulate input coordinate */ \
654                                 if(in_x >= 0 && in_x < w && in_y >= 0 && in_y < h) \
655                                 { \
656                                         accum_r += in_rows[in_y][in_x * COMPONENTS]; \
657                                         if(DO_YUV) \
658                                         { \
659                                                 accum_g += (int)in_rows[in_y][in_x * COMPONENTS + 1]; \
660                                                 accum_b += (int)in_rows[in_y][in_x * COMPONENTS + 2]; \
661                                         } \
662                                         else \
663                                         { \
664                                                 accum_g += in_rows[in_y][in_x * COMPONENTS + 1]; \
665                                                 accum_b += in_rows[in_y][in_x * COMPONENTS + 2]; \
666                                         } \
667                                         if(COMPONENTS == 4) \
668                                                 accum_a += in_rows[in_y][in_x * COMPONENTS + 3]; \
669                                 } \
670                                 else \
671                                 { \
672                                         accum_g += chroma_offset; \
673                                         accum_b += chroma_offset; \
674                                 } \
675                         } \
676  \
677 /* Accumulation to output */ \
678                         if(do_r) \
679                         { \
680                                 *out_row++ = (accum_r * fraction) / 0x10000; \
681                                 in_row++; \
682                         } \
683                         else \
684                         { \
685                                 *out_row++ = *in_row++; \
686                         } \
687  \
688                         if(do_g) \
689                         { \
690                                 if(DO_YUV) \
691                                         *out_row++ = ((accum_g * fraction) / 0x10000); \
692                                 else \
693                                         *out_row++ = (accum_g * fraction) / 0x10000; \
694                                 in_row++; \
695                         } \
696                         else \
697                         { \
698                                 *out_row++ = *in_row++; \
699                         } \
700  \
701                         if(do_b) \
702                         { \
703                                 if(DO_YUV) \
704                                         *out_row++ = (accum_b * fraction) / 0x10000; \
705                                 else \
706                                         *out_row++ = (accum_b * fraction) / 0x10000; \
707                                 in_row++; \
708                         } \
709                         else \
710                         { \
711                                 *out_row++ = *in_row++; \
712                         } \
713  \
714                         if(COMPONENTS == 4) \
715                         { \
716                                 if(do_a) \
717                                 { \
718                                         *out_row++ = (accum_a * fraction) / 0x10000; \
719                                         in_row++; \
720                                 } \
721                                 else \
722                                 { \
723                                         *out_row++ = *in_row++; \
724                                 } \
725                         } \
726                 } \
727         } \
728 }
729
730 void RadialBlurUnit::process_package(LoadPackage *package)
731 {
732         RadialBlurPackage *pkg = (RadialBlurPackage*)package;
733         int h = plugin->output->get_h();
734         int w = plugin->output->get_w();
735         int do_r = plugin->config.r;
736         int do_g = plugin->config.g;
737         int do_b = plugin->config.b;
738         int do_a = plugin->config.a;
739         int fraction = 0x10000 / plugin->config.steps;
740         int center_x = plugin->config.x * w / 100;
741         int center_y = plugin->config.y * h / 100;
742
743         switch(plugin->input->get_color_model())
744         {
745                 case BC_RGB888:
746                         BLEND_LAYER(3, uint8_t, int, 0xff, 0)
747                         break;
748                 case BC_RGBA8888:
749                         BLEND_LAYER(4, uint8_t, int, 0xff, 0)
750                         break;
751                 case BC_RGB_FLOAT:
752                         BLEND_LAYER(3, float, float, 1, 0)
753                         break;
754                 case BC_RGBA_FLOAT:
755                         BLEND_LAYER(4, float, float, 1, 0)
756                         break;
757                 case BC_RGB161616:
758                         BLEND_LAYER(3, uint16_t, int, 0xffff, 0)
759                         break;
760                 case BC_RGBA16161616:
761                         BLEND_LAYER(4, uint16_t, int, 0xffff, 0)
762                         break;
763                 case BC_YUV888:
764                         BLEND_LAYER(3, uint8_t, int, 0xff, 1)
765                         break;
766                 case BC_YUVA8888:
767                         BLEND_LAYER(4, uint8_t, int, 0xff, 1)
768                         break;
769                 case BC_YUV161616:
770                         BLEND_LAYER(3, uint16_t, int, 0xffff, 1)
771                         break;
772                 case BC_YUVA16161616:
773                         BLEND_LAYER(4, uint16_t, int, 0xffff, 1)
774                         break;
775         }
776 }
777
778
779
780
781
782
783 RadialBlurEngine::RadialBlurEngine(RadialBlurMain *plugin,
784         int total_clients,
785         int total_packages)
786  : LoadServer(total_clients, total_packages)
787 // : LoadServer(1, 1)
788 {
789         this->plugin = plugin;
790 }
791
792 void RadialBlurEngine::init_packages()
793 {
794         for(int i = 0; i < get_total_packages(); i++)
795         {
796                 RadialBlurPackage *package = (RadialBlurPackage*)get_package(i);
797                 package->y1 = plugin->output->get_h() * i / get_total_packages();
798                 package->y2 = plugin->output->get_h() * (i + 1) / get_total_packages();
799         }
800 }
801
802 LoadClient* RadialBlurEngine::new_client()
803 {
804         return new RadialBlurUnit(this, plugin);
805 }
806
807 LoadPackage* RadialBlurEngine::new_package()
808 {
809         return new RadialBlurPackage;
810 }
811
812
813
814
815