modify clr btn 16 plugins, add regdmp for sigtraps, rework mask_engine, mask rotate...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / wave / wave.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 "wave.h"
23
24
25
26
27
28
29
30
31 WaveConfig::WaveConfig()
32 {
33         reset(RESET_ALL);
34 }
35
36
37 void WaveConfig::reset(int clear)
38 {
39         switch(clear) {
40                 case RESET_ALL :
41                         mode = SMEAR;
42                         reflective = 0;
43                         amplitude = 0;
44                         phase = 0;
45                         wavelength = 0;
46                         break;
47                 case RESET_AMPLITUDE : amplitude = 0;
48                         break;
49                 case RESET_PHASE : phase = 0;
50                         break;
51                 case RESET_WAVELENGTH : wavelength = 0;
52                         break;
53                 case RESET_DEFAULT_SETTINGS :
54                 default:
55                         mode = SMEAR;
56                         reflective = 0;
57                         amplitude = 10;
58                         phase = 0;
59                         wavelength = 10;
60                         break;
61         }
62 }
63
64 void WaveConfig::copy_from(WaveConfig &src)
65 {
66         this->mode = src.mode;
67         this->reflective = src.reflective;
68         this->amplitude = src.amplitude;
69         this->phase = src.phase;
70         this->wavelength = src.wavelength;
71 }
72
73 int WaveConfig::equivalent(WaveConfig &src)
74 {
75         return
76                 (this->mode == src.mode) &&
77                 EQUIV(this->reflective, src.reflective) &&
78                 EQUIV(this->amplitude, src.amplitude) &&
79                 EQUIV(this->phase, src.phase) &&
80                 EQUIV(this->wavelength, src.wavelength);
81 }
82
83 void WaveConfig::interpolate(WaveConfig &prev,
84                 WaveConfig &next,
85                 long prev_frame,
86                 long next_frame,
87                 long current_frame)
88 {
89         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
90         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
91
92         this->amplitude = prev.amplitude * prev_scale + next.amplitude * next_scale;
93         this->phase = prev.phase * prev_scale + next.phase * next_scale;
94         this->wavelength = prev.wavelength * prev_scale + next.wavelength * next_scale;
95         this->mode = prev.mode;
96         this->reflective = prev.reflective;
97 }
98
99
100
101
102
103
104
105
106 WaveSmear::WaveSmear(WaveEffect *plugin, WaveWindow *window, int x, int y)
107  : BC_Radial(x, y, plugin->config.mode == SMEAR, _("Smear"))
108 {
109         this->plugin = plugin;
110         this->window = window;
111 }
112 int WaveSmear::handle_event()
113 {
114         plugin->config.mode = SMEAR;
115         window->update_mode();
116         plugin->send_configure_change();
117         return 1;
118 }
119
120
121
122
123 WaveBlacken::WaveBlacken(WaveEffect *plugin, WaveWindow *window, int x, int y)
124  : BC_Radial(x, y, plugin->config.mode == BLACKEN, _("Blacken"))
125 {
126         this->plugin = plugin;
127         this->window = window;
128 }
129 int WaveBlacken::handle_event()
130 {
131         plugin->config.mode = BLACKEN;
132         window->update_mode();
133         plugin->send_configure_change();
134         return 1;
135 }
136
137
138
139
140
141
142 WaveReflective::WaveReflective(WaveEffect *plugin, int x, int y)
143  : BC_CheckBox(x, y, plugin->config.reflective, _("Reflective"))
144 {
145         this->plugin = plugin;
146 }
147 int WaveReflective::handle_event()
148 {
149         plugin->config.reflective = get_value();
150         plugin->send_configure_change();
151         return 1;
152 }
153
154
155 WaveAmplitude::WaveAmplitude(WaveEffect *plugin, int x, int y)
156  : BC_FSlider(x,
157                         y,
158                         0,
159                         200,
160                         200,
161                         (float)0,
162                         (float)100,
163                         plugin->config.amplitude)
164 {
165         this->plugin = plugin;
166 }
167 int WaveAmplitude::handle_event()
168 {
169         plugin->config.amplitude = get_value();
170         plugin->send_configure_change();
171         return 1;
172 }
173
174
175
176 WavePhase::WavePhase(WaveEffect *plugin, int x, int y)
177  : BC_FSlider(x,
178                         y,
179                         0,
180                         200,
181                         200,
182                         (float)0,
183                         (float)360,
184                         plugin->config.phase)
185 {
186         this->plugin = plugin;
187 }
188 int WavePhase::handle_event()
189 {
190         plugin->config.phase = get_value();
191         plugin->send_configure_change();
192         return 1;
193 }
194
195 WaveLength::WaveLength(WaveEffect *plugin, int x, int y)
196  : BC_FSlider(x,
197                         y,
198                         0,
199                         200,
200                         200,
201                         (float)0,
202                         (float)50,
203                         plugin->config.wavelength)
204 {
205         this->plugin = plugin;
206 }
207 int WaveLength::handle_event()
208 {
209         plugin->config.wavelength = get_value();
210         plugin->send_configure_change();
211         return 1;
212 }
213
214 WaveReset::WaveReset(WaveEffect *plugin, WaveWindow *gui, int x, int y)
215  : BC_GenericButton(x, y, _("Reset"))
216 {
217         this->plugin = plugin;
218         this->gui = gui;
219 }
220 WaveReset::~WaveReset()
221 {
222 }
223 int WaveReset::handle_event()
224 {
225         plugin->config.reset(RESET_ALL);
226         gui->update_gui(RESET_ALL);
227         plugin->send_configure_change();
228         return 1;
229 }
230
231 WaveDefaultSettings::WaveDefaultSettings(WaveEffect *plugin, WaveWindow *gui, int x, int y, int w)
232  : BC_GenericButton(x, y, w, _("Default"))
233 {
234         this->plugin = plugin;
235         this->gui = gui;
236 }
237 WaveDefaultSettings::~WaveDefaultSettings()
238 {
239 }
240 int WaveDefaultSettings::handle_event()
241 {
242         plugin->config.reset(RESET_DEFAULT_SETTINGS);
243         gui->update_gui(RESET_DEFAULT_SETTINGS);
244         plugin->send_configure_change();
245         return 1;
246 }
247
248 WaveSliderClr::WaveSliderClr(WaveEffect *plugin, WaveWindow *gui, int x, int y, int w, int clear)
249  : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
250 {
251         this->plugin = plugin;
252         this->gui = gui;
253         this->clear = clear;
254 }
255 WaveSliderClr::~WaveSliderClr()
256 {
257 }
258 int WaveSliderClr::handle_event()
259 {
260         // clear==1 ==> Amplitude slider
261         // clear==2 ==> Phase slider
262         // clear==3 ==> Steps slider
263         plugin->config.reset(clear);
264         gui->update_gui(clear);
265         plugin->send_configure_change();
266         return 1;
267 }
268
269
270
271
272 WaveWindow::WaveWindow(WaveEffect *plugin)
273  : PluginClientWindow(plugin, 385, 140, 385, 140, 0)
274 {
275         this->plugin = plugin;
276 }
277
278 WaveWindow::~WaveWindow()
279 {
280 }
281
282 void WaveWindow::create_objects()
283 {
284         int x = 10, y = 10, x1 = 115;
285         int x2 = 0; int clrBtn_w = 50;
286         int defaultBtn_w = 100;
287
288 //      add_subwindow(new BC_Title(x, y, _("Mode:")));
289 //      add_subwindow(smear = new WaveSmear(plugin, this, x1, y));
290 //      y += 20;
291 //      add_subwindow(blacken = new WaveBlacken(plugin, this, x1, y));
292 //      y += 30;
293 //      add_subwindow(reflective = new WaveReflective(plugin, x1, y));
294 //      y += 30;
295         add_subwindow(new BC_Title(x, y, _("Amplitude:")));
296         add_subwindow(amplitude = new WaveAmplitude(plugin, x1, y));
297         x2 = x1 + amplitude->get_w() + 10;
298         add_subwindow(amplitudeClr = new WaveSliderClr(plugin, this, x2, y, clrBtn_w, RESET_AMPLITUDE));
299
300         y += 30;
301         add_subwindow(new BC_Title(x, y, _("Phase:")));
302         add_subwindow(phase = new WavePhase(plugin, x1, y));
303         add_subwindow(phaseClr = new WaveSliderClr(plugin, this, x2, y, clrBtn_w, RESET_PHASE));
304
305         y += 30;
306         add_subwindow(new BC_Title(x, y, _("Wavelength:")));
307         add_subwindow(wavelength = new WaveLength(plugin, x1, y));
308         add_subwindow(wavelengthClr = new WaveSliderClr(plugin, this, x2, y, clrBtn_w, RESET_WAVELENGTH));
309
310         y += 40;
311         add_subwindow(reset = new WaveReset(plugin, this, x, y));
312         add_subwindow(default_settings = new WaveDefaultSettings(plugin, this,
313                 (385 - 10 - defaultBtn_w), y, defaultBtn_w));
314
315         show_window();
316         flush();
317 }
318
319 void WaveWindow::update_mode()
320 {
321 //      smear->update(plugin->config.mode == SMEAR);
322 //      blacken->update(plugin->config.mode == BLACKEN);
323 }
324
325 // for Reset button
326 void WaveWindow::update_gui(int clear)
327 {
328         switch(clear) {
329                 case RESET_AMPLITUDE : amplitude->update(plugin->config.amplitude);
330                         break;
331                 case RESET_PHASE : phase->update(plugin->config.phase);
332                         break;
333                 case RESET_WAVELENGTH : wavelength->update(plugin->config.wavelength);
334                         break;
335                 case RESET_ALL :
336                 case RESET_DEFAULT_SETTINGS :
337                 default:
338                         amplitude->update(plugin->config.amplitude);
339                         phase->update(plugin->config.phase);
340                         wavelength->update(plugin->config.wavelength);
341                         break;
342         }
343 }
344
345
346
347
348
349
350
351
352
353 REGISTER_PLUGIN(WaveEffect)
354
355
356
357
358 WaveEffect::WaveEffect(PluginServer *server)
359  : PluginVClient(server)
360 {
361         temp_frame = 0;
362         engine = 0;
363
364 }
365
366 WaveEffect::~WaveEffect()
367 {
368
369
370         if(temp_frame) delete temp_frame;
371         if(engine) delete engine;
372 }
373
374
375 const char* WaveEffect::plugin_title() { return N_("Wave"); }
376 int WaveEffect::is_realtime() { return 1; }
377
378
379 NEW_WINDOW_MACRO(WaveEffect, WaveWindow)
380
381 void WaveEffect::update_gui()
382 {
383         if(thread)
384         {
385                 thread->window->lock_window();
386                 load_configuration();
387                 ((WaveWindow*)thread->window)->update_mode();
388 //              thread->window->reflective->update(config.reflective);
389                 ((WaveWindow*)thread->window)->amplitude->update(config.amplitude);
390                 ((WaveWindow*)thread->window)->phase->update(config.phase);
391                 ((WaveWindow*)thread->window)->wavelength->update(config.wavelength);
392                 thread->window->unlock_window();
393         }
394 }
395
396
397 LOAD_CONFIGURATION_MACRO(WaveEffect, WaveConfig)
398
399
400 void WaveEffect::save_data(KeyFrame *keyframe)
401 {
402         FileXML output;
403
404 // cause data to be stored directly in text
405         output.set_shared_output(keyframe->xbuf);
406         output.tag.set_title("WAVE");
407         output.tag.set_property("MODE", config.mode);
408         output.tag.set_property("REFLECTIVE", config.reflective);
409         output.tag.set_property("AMPLITUDE", config.amplitude);
410         output.tag.set_property("PHASE", config.phase);
411         output.tag.set_property("WAVELENGTH", config.wavelength);
412         output.append_tag();
413         output.tag.set_title("/WAVE");
414         output.append_tag();
415         output.append_newline();
416         output.terminate_string();
417 }
418
419 void WaveEffect::read_data(KeyFrame *keyframe)
420 {
421         FileXML input;
422
423         input.set_shared_input(keyframe->xbuf);
424
425         while(!input.read_tag())
426         {
427                 if(input.tag.title_is("WAVE"))
428                 {
429                         config.mode = input.tag.get_property("MODE", config.mode);
430                         config.reflective = input.tag.get_property("REFLECTIVE", config.reflective);
431                         config.amplitude = input.tag.get_property("AMPLITUDE", config.amplitude);
432                         config.phase = input.tag.get_property("PHASE", config.phase);
433                         config.wavelength = input.tag.get_property("WAVELENGTH", config.wavelength);
434                 }
435         }
436 }
437
438
439 int WaveEffect::process_realtime(VFrame *input, VFrame *output)
440 {
441         load_configuration();
442
443
444
445 //printf("WaveEffect::process_realtime %f %d\n", config.radius, config.use_intensity);
446         this->input = input;
447         this->output = output;
448
449         if(EQUIV(config.amplitude, 0) || EQUIV(config.wavelength, 0))
450         {
451                 if(input->get_rows()[0] != output->get_rows()[0])
452                         output->copy_from(input);
453         }
454         else
455         {
456                 if(input->get_rows()[0] == output->get_rows()[0])
457                 {
458                         if(!temp_frame) temp_frame = new VFrame(input->get_w(), input->get_h(),
459                                 input->get_color_model(), 0);
460                         temp_frame->copy_from(input);
461                         this->input = temp_frame;
462                 }
463
464
465                 if(!engine)
466                 {
467                         engine = new WaveServer(this, (PluginClient::smp + 1));
468                 }
469
470                 engine->process_packages();
471         }
472
473
474
475         return 0;
476 }
477
478
479
480
481
482
483 WavePackage::WavePackage()
484  : LoadPackage()
485 {
486 }
487
488
489
490
491
492
493 WaveUnit::WaveUnit(WaveEffect *plugin, WaveServer *server)
494  : LoadClient(server)
495 {
496         this->plugin = plugin;
497 }
498
499
500
501 #define WITHIN(a, b, c) ((((a) <= (b)) && ((b) <= (c))) ? 1 : 0)
502
503 static float bilinear(double  x,
504           double  y,
505           float  *v)
506 {
507         double m0, m1;
508         x = fmod(x, 1.0);
509         y = fmod(y, 1.0);
510
511         if(x < 0)
512         x += 1.0;
513         if(y < 0)
514         y += 1.0;
515
516         m0 = (1.0 - x) * v[0] + x * v[1];
517         m1 = (1.0 - x) * v[2] + x * v[3];
518
519         return((1.0 - y) * m0 + y * m1);
520 }
521
522 void WaveUnit::process_package(LoadPackage *package)
523 {
524         WavePackage *pkg = (WavePackage*)package;
525         int w = plugin->input->get_w();
526         int h = plugin->input->get_h();
527         double cen_x, cen_y;       /* Center of wave */
528         double xhsiz, yhsiz;       /* Half size of selection */
529         double radius;             /* Radius */
530         double amnt, d;
531         double needx, needy;
532         double dx, dy;
533         double xscale, yscale;
534         double wavelength;
535         int xi, yi;
536         float values[4];
537         float val;
538         int x1, y1, x2, y2;
539         int x1_in, y1_in, x2_in, y2_in;
540         double phase = plugin->config.phase * M_PI / 180;
541
542
543         x1 = y1 = 0;
544         x2 = w;
545         y2 = h;
546         cen_x = (double) (x2 - 1 + x1) / 2.0;
547         cen_y = (double) (y2 - 1 + y1) / 2.0;
548         xhsiz = (double) (x2 - x1) / 2.0;
549         yhsiz = (double) (y2 - y1) / 2.0;
550
551         if (xhsiz < yhsiz)
552     {
553         xscale = yhsiz / xhsiz;
554         yscale = 1.0;
555     }
556         else if (xhsiz > yhsiz)
557     {
558         xscale = 1.0;
559         yscale = xhsiz / yhsiz;
560     }
561         else
562     {
563         xscale = 1.0;
564         yscale = 1.0;
565     }
566
567         radius  = MAX(xhsiz, yhsiz);
568
569
570         wavelength = plugin->config.wavelength / 100 * radius;
571
572
573
574
575 #define WAVE(type, components, chroma_offset) \
576 { \
577         int row_size = w * components; \
578         type **in_rows = (type**)plugin->input->get_rows(); \
579         for(int y = pkg->row1; y < pkg->row2; y++) \
580         { \
581                 type *dest = (type*)plugin->output->get_rows()[y]; \
582  \
583                 for(int x = x1; x < x2; x++) \
584                 { \
585                         dx = (x - cen_x) * xscale; \
586                         dy = (y - cen_y) * yscale; \
587                     d = sqrt(dx * dx + dy * dy); \
588  \
589                         if(plugin->config.reflective) \
590                         { \
591                         amnt = plugin->config.amplitude *  \
592                                         fabs(sin(((d / wavelength) *  \
593                                                 (2.0 * M_PI) + \
594                                                 phase))); \
595  \
596                         needx = (amnt * dx) / xscale + cen_x; \
597                         needy = (amnt * dy) / yscale + cen_y; \
598                         } \
599                         else \
600                         { \
601                         amnt = plugin->config.amplitude *  \
602                                         sin(((d / wavelength) *  \
603                                                 (2.0 * M_PI) + \
604                                         phase)); \
605  \
606                         needx = (amnt + dx) / xscale + cen_x; \
607                         needy = (amnt + dy) / yscale + cen_y; \
608                         } \
609  \
610                         xi = (int)needx; \
611                         yi = (int)needy; \
612  \
613                         if(plugin->config.mode == SMEAR) \
614                 { \
615                         if(xi > w - 2) \
616                                 { \
617                                         xi = w - 2; \
618                                 } \
619                         else  \
620                                 if(xi < 0) \
621                                 { \
622                                         xi = 0; \
623                                 } \
624  \
625                         if(yi > h - 2) \
626                                 { \
627                                         yi = h - 2; \
628                                 } \
629                         else  \
630                                 if(yi < 0) \
631                                 { \
632                                         yi = 0; \
633                                 } \
634                 } \
635  \
636                         type *p = in_rows[CLIP(yi, 0, h - 1)] + \
637                                 CLIP(xi, 0, w - 1) * components; \
638                         x1_in = WITHIN(0, xi, w - 1); \
639                         y1_in = WITHIN(0, yi, h - 1); \
640                         x2_in = WITHIN(0, xi + 1, w - 1); \
641                         y2_in = WITHIN(0, yi + 1, h - 1); \
642  \
643  \
644                         for(int k = 0; k < components; k++) \
645                 { \
646                         if (x1_in && y1_in) \
647                                         values[0] = *(p + k); \
648                         else \
649                                         values[0] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
650  \
651                         if (x2_in && y1_in) \
652                                         values[1] = *(p + components + k); \
653                         else \
654                                         values[1] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
655  \
656                         if (x1_in && y2_in) \
657                                         values[2] = *(p + row_size + k); \
658                         else \
659                                         values[2] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
660  \
661                         if (x2_in) \
662                                 { \
663                                         if (y2_in) \
664                                         values[3] = *(p + row_size + components + k); \
665                                         else \
666                                         values[3] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
667                                 } \
668                         else \
669                                         values[3] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
670  \
671                         val = bilinear(needx, needy, values); \
672  \
673                         *dest++ = (type)val; \
674                 } \
675                 } \
676         } \
677 }
678
679
680
681
682         switch(plugin->input->get_color_model())
683         {
684                 case BC_RGB888:
685                         WAVE(unsigned char, 3, 0x0);
686                         break;
687                 case BC_RGB_FLOAT:
688                         WAVE(float, 3, 0x0);
689                         break;
690                 case BC_YUV888:
691                         WAVE(unsigned char, 3, 0x80);
692                         break;
693                 case BC_RGB161616:
694                         WAVE(uint16_t, 3, 0x0);
695                         break;
696                 case BC_YUV161616:
697                         WAVE(uint16_t, 3, 0x8000);
698                         break;
699                 case BC_RGBA_FLOAT:
700                         WAVE(unsigned char, 4, 0x0);
701                         break;
702                 case BC_RGBA8888:
703                         WAVE(unsigned char, 4, 0x0);
704                         break;
705                 case BC_YUVA8888:
706                         WAVE(unsigned char, 4, 0x8000);
707                         break;
708                 case BC_RGBA16161616:
709                         WAVE(uint16_t, 4, 0x0);
710                         break;
711                 case BC_YUVA16161616:
712                         WAVE(uint16_t, 4, 0x8000);
713                         break;
714         }
715
716
717
718
719 }
720
721
722
723
724
725
726 WaveServer::WaveServer(WaveEffect *plugin, int cpus)
727  : LoadServer(cpus, cpus)
728 {
729         this->plugin = plugin;
730 }
731
732 void WaveServer::init_packages()
733 {
734         for(int i = 0; i < LoadServer::get_total_packages(); i++)
735         {
736                 WavePackage *pkg = (WavePackage*)get_package(i);
737                 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
738                 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
739         }
740 }
741
742
743 LoadClient* WaveServer::new_client()
744 {
745         return new WaveUnit(plugin, this);
746 }
747
748 LoadPackage* WaveServer::new_package()
749 {
750         return new WavePackage;
751 }
752