prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / plugins / huesaturation / huesaturation.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 "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "bchash.h"
25 #include "filexml.h"
26 #include "guicast.h"
27 #include "language.h"
28 #include "loadbalance.h"
29 #include "cicolors.h"
30 #include "playback3d.h"
31 #include "pluginvclient.h"
32 #include "vframe.h"
33
34 #include <stdint.h>
35 #include <string.h>
36
37
38 class HueEffect;
39
40 #define MINHUE -180
41 #define MAXHUE 180
42 #define MINSATURATION -100
43 #define MAXSATURATION 100
44 #define MINVALUE -100
45 #define MAXVALUE 100
46
47
48
49
50
51
52 class HueConfig
53 {
54 public:
55         HueConfig();
56         
57         void copy_from(HueConfig &src);
58         int equivalent(HueConfig &src);
59         void interpolate(HueConfig &prev, 
60                 HueConfig &next, 
61                 long prev_frame, 
62                 long next_frame, 
63                 long current_frame);
64         float hue, saturation, value;
65 };
66
67 class HueSlider : public BC_FSlider
68 {
69 public:
70         HueSlider(HueEffect *plugin, int x, int y, int w);
71         int handle_event();
72         HueEffect *plugin;
73 };
74
75 class SaturationSlider : public BC_FSlider
76 {
77 public:
78         SaturationSlider(HueEffect *plugin, int x, int y, int w);
79         int handle_event();
80         char* get_caption();
81         HueEffect *plugin;
82         char string[BCTEXTLEN];
83 };
84
85 class ValueSlider : public BC_FSlider
86 {
87 public:
88         ValueSlider(HueEffect *plugin, int x, int y, int w);
89         int handle_event();
90         char* get_caption();
91         HueEffect *plugin;
92         char string[BCTEXTLEN];
93 };
94
95 class HueWindow : public PluginClientWindow
96 {
97 public:
98         HueWindow(HueEffect *plugin);
99         void create_objects();
100         HueEffect *plugin;
101         HueSlider *hue;
102         SaturationSlider *saturation;
103         ValueSlider *value;
104 };
105
106
107
108 class HueEngine : public LoadServer
109 {
110 public:
111         HueEngine(HueEffect *plugin, int cpus);
112         void init_packages();
113         LoadClient* new_client();
114         LoadPackage* new_package();
115         HueEffect *plugin;
116 };
117
118 class HuePackage : public LoadPackage
119 {
120 public:
121         HuePackage();
122         int row1, row2;
123 };
124
125 class HueUnit : public LoadClient
126 {
127 public:
128         HueUnit(HueEffect *plugin, HueEngine *server);
129         void process_package(LoadPackage *package);
130         HueEffect *plugin;
131         YUV yuv;
132 };
133
134 class HueEffect : public PluginVClient
135 {
136 public:
137         HueEffect(PluginServer *server);
138         ~HueEffect();
139         
140         
141         PLUGIN_CLASS_MEMBERS(HueConfig);
142         int process_buffer(VFrame *frame,
143                 int64_t start_position,
144                 double frame_rate);
145         int is_realtime();
146         void save_data(KeyFrame *keyframe);
147         void read_data(KeyFrame *keyframe);
148         void update_gui();
149         int handle_opengl();
150
151         VFrame *input, *output;
152         HueEngine *engine;
153 };
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 HueConfig::HueConfig()
175 {
176         hue = saturation = value = 0;
177 }
178         
179 void HueConfig::copy_from(HueConfig &src)
180 {
181         hue = src.hue;
182         saturation = src.saturation;
183         value = src.value;
184 }
185 int HueConfig::equivalent(HueConfig &src)
186 {
187         return EQUIV(hue, src.hue) && 
188                 EQUIV(saturation, src.saturation) && 
189                 EQUIV(value, src.value);
190 }
191 void HueConfig::interpolate(HueConfig &prev, 
192         HueConfig &next, 
193         long prev_frame, 
194         long next_frame, 
195         long current_frame)
196 {
197         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
198         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
199
200         this->hue = prev.hue * prev_scale + next.hue * next_scale;
201         this->saturation = prev.saturation * prev_scale + next.saturation * next_scale;
202         this->value = prev.value * prev_scale + next.value * next_scale;
203 }
204
205
206
207
208
209
210
211
212 HueSlider::HueSlider(HueEffect *plugin, int x, int y, int w)
213  : BC_FSlider(x, 
214                         y,
215                         0,
216                         w, 
217                         w, 
218                         (float)MINHUE, 
219                         (float)MAXHUE, 
220                         plugin->config.hue)
221 {
222         this->plugin = plugin;
223 }
224 int HueSlider::handle_event()
225 {
226         plugin->config.hue = get_value();
227         plugin->send_configure_change();
228         return 1;
229 }
230
231
232
233
234
235
236
237 SaturationSlider::SaturationSlider(HueEffect *plugin, int x, int y, int w)
238  : BC_FSlider(x, 
239                         y,
240                         0,
241                         w, 
242                         w, 
243                         (float)MINSATURATION, 
244                         (float)MAXSATURATION, 
245                         plugin->config.saturation)
246 {
247         this->plugin = plugin;
248 }
249 int SaturationSlider::handle_event()
250 {
251         plugin->config.saturation = get_value();
252         plugin->send_configure_change();
253         return 1;
254 }
255
256 char* SaturationSlider::get_caption()
257 {
258         float fraction = ((float)plugin->config.saturation - MINSATURATION) / 
259                 MAXSATURATION;;
260         sprintf(string, "%0.4f", fraction);
261         return string;
262 }
263
264
265
266
267
268
269
270 ValueSlider::ValueSlider(HueEffect *plugin, int x, int y, int w)
271  : BC_FSlider(x, 
272                         y,
273                         0,
274                         w, 
275                         w, 
276                         (float)MINVALUE, 
277                         (float)MAXVALUE, 
278                         plugin->config.value)
279 {
280         this->plugin = plugin;
281 }
282 int ValueSlider::handle_event()
283 {
284         plugin->config.value = get_value();
285         plugin->send_configure_change();
286         return 1;
287 }
288
289 char* ValueSlider::get_caption()
290 {
291         float fraction = ((float)plugin->config.value - MINVALUE) / MAXVALUE;
292         sprintf(string, "%0.4f", fraction);
293         return string;
294 }
295
296
297
298
299
300
301
302 HueWindow::HueWindow(HueEffect *plugin)
303  : PluginClientWindow(plugin,
304                         310, 
305                         100, 
306                         310, 
307                         100, 
308                         0)
309 {
310         this->plugin = plugin;
311 }
312 void HueWindow::create_objects()
313 {
314         int x = 10, y = 10, x1 = 100;
315         add_subwindow(new BC_Title(x, y, _("Hue:")));
316         add_subwindow(hue = new HueSlider(plugin, x1, y, 200));
317         y += 30;
318         add_subwindow(new BC_Title(x, y, _("Saturation:")));
319         add_subwindow(saturation = new SaturationSlider(plugin, x1, y, 200));
320         y += 30;
321         add_subwindow(new BC_Title(x, y, _("Value:")));
322         add_subwindow(value = new ValueSlider(plugin, x1, y, 200));
323         show_window();
324         flush();
325 }
326
327
328
329
330
331
332
333
334
335
336
337 HueEngine::HueEngine(HueEffect *plugin, int cpus)
338  : LoadServer(cpus, cpus)
339 {
340         this->plugin = plugin;
341 }
342 void HueEngine::init_packages()
343 {
344         for(int i = 0; i < LoadServer::get_total_packages(); i++)
345         {
346                 HuePackage *pkg = (HuePackage*)get_package(i);
347                 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
348                 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
349         }
350 }
351 LoadClient* HueEngine::new_client()
352 {
353         return new HueUnit(plugin, this);
354 }
355 LoadPackage* HueEngine::new_package()
356 {
357         return new HuePackage;
358 }
359
360
361
362
363
364
365
366
367 HuePackage::HuePackage()
368  : LoadPackage()
369 {
370 }
371
372 HueUnit::HueUnit(HueEffect *plugin, HueEngine *server)
373  : LoadClient(server)
374 {
375         this->plugin = plugin;
376 }
377
378
379
380
381
382
383
384 #define HUESATURATION(type, max, components, use_yuv) \
385 { \
386         float h_offset = plugin->config.hue; \
387         float s_offset = ((float)plugin->config.saturation - MINSATURATION) / MAXSATURATION; \
388         float v_offset = ((float)plugin->config.value - MINVALUE) / MAXVALUE; \
389         for(int i = pkg->row1; i < pkg->row2; i++) \
390         { \
391                 type* in_row = (type*)plugin->input->get_rows()[i]; \
392                 type* out_row = (type*)plugin->output->get_rows()[i]; \
393  \
394                 for(int j = 0; j < w; j++) \
395                 { \
396                         float h, s, va; \
397                         int y, u, v; \
398                         float r, g, b; \
399                         int r_i, g_i, b_i; \
400  \
401                         if(use_yuv) \
402                         { \
403                                 y = (int)in_row[0]; \
404                                 u = (int)in_row[1]; \
405                                 v = (int)in_row[2]; \
406                                 if(max == 0xffff) \
407                                         yuv.yuv_to_rgb_16(r_i, g_i, b_i, y, u, v); \
408                                 else \
409                                         yuv.yuv_to_rgb_8(r_i, g_i, b_i, y, u, v); \
410                                 HSV::rgb_to_hsv((float)r_i / max, \
411                                         (float)g_i / max, \
412                                         (float)b_i / max, \
413                                         h, \
414                                         s, \
415                                         va); \
416                         } \
417                         else \
418                         { \
419                                 r = (float)in_row[0] / max; \
420                                 g = (float)in_row[1] / max; \
421                                 b = (float)in_row[2] / max; \
422                                 HSV::rgb_to_hsv(r, g, b, h, s, va); \
423                         } \
424  \
425  \
426                         h += h_offset; \
427                         s *= s_offset; \
428                         va *= v_offset; \
429  \
430                         if(h >= 360) h -= 360; \
431                         if(h < 0) h += 360; \
432                         if(sizeof(type) < 4) \
433                         { \
434                                 if(s > 1) s = 1; \
435                                 if(va > 1) va = 1; \
436                                 if(s < 0) s = 0; \
437                                 if(va < 0) va = 0; \
438                         } \
439  \
440                         if(use_yuv) \
441                         { \
442                                 HSV::hsv_to_yuv(y, u, v, h, s, va, max); \
443                                 out_row[0] = y; \
444                                 out_row[1] = u; \
445                                 out_row[2] = v; \
446                         } \
447                         else \
448                         { \
449                                 HSV::hsv_to_rgb(r, g, b, h, s, va); \
450                                 if(sizeof(type) < 4) \
451                                 { \
452                                         r *= max; \
453                                         g *= max; \
454                                         b *= max; \
455                                         out_row[0] = (type)CLIP(r, 0, max); \
456                                         out_row[1] = (type)CLIP(g, 0, max); \
457                                         out_row[2] = (type)CLIP(b, 0, max); \
458                                 } \
459                                 else \
460                                 { \
461                                         out_row[0] = (type)r; \
462                                         out_row[1] = (type)g; \
463                                         out_row[2] = (type)b; \
464                                 } \
465                         } \
466  \
467                         in_row += components; \
468                         out_row += components; \
469                 } \
470         } \
471 }
472
473
474 void HueUnit::process_package(LoadPackage *package)
475 {
476         HuePackage *pkg = (HuePackage*)package;
477         int w = plugin->input->get_w();
478
479         switch(plugin->input->get_color_model())
480         {
481                 case BC_RGB888:
482                         HUESATURATION(unsigned char, 0xff, 3, 0)
483                         break;
484
485                 case BC_RGB_FLOAT:
486                         HUESATURATION(float, 1, 3, 0)
487                         break;
488
489                 case BC_YUV888:
490                         HUESATURATION(unsigned char, 0xff, 3, 1)
491                         break;
492
493                 case BC_RGB161616:
494                         HUESATURATION(uint16_t, 0xffff, 3, 0)
495                         break;
496
497                 case BC_YUV161616:
498                         HUESATURATION(uint16_t, 0xffff, 3, 1)
499                         break;
500
501                 case BC_RGBA_FLOAT:
502                         HUESATURATION(float, 1, 4, 0)
503                         break;
504
505                 case BC_RGBA8888:
506                         HUESATURATION(unsigned char, 0xff, 4, 0)
507                         break;
508
509                 case BC_YUVA8888:
510                         HUESATURATION(unsigned char, 0xff, 4, 1)
511                         break;
512
513                 case BC_RGBA16161616:
514                         HUESATURATION(uint16_t, 0xffff, 4, 0)
515                         break;
516
517                 case BC_YUVA16161616:
518                         HUESATURATION(uint16_t, 0xffff, 4, 1)
519                         break;
520
521         }
522 }
523
524
525
526
527 REGISTER_PLUGIN(HueEffect)
528
529
530 HueEffect::HueEffect(PluginServer *server)
531  : PluginVClient(server)
532 {
533         engine = 0;
534         
535 }
536 HueEffect::~HueEffect()
537 {
538         
539         if(engine) delete engine;
540 }
541
542 int HueEffect::process_buffer(VFrame *frame,
543         int64_t start_position,
544         double frame_rate)
545 {
546         load_configuration();
547
548         read_frame(frame, 
549                 0, 
550                 start_position, 
551                 frame_rate,
552                 get_use_opengl());
553         
554
555         this->input = frame;
556         this->output = frame;
557         if(EQUIV(config.hue, 0) && EQUIV(config.saturation, 0) && EQUIV(config.value, 0))
558         {
559                 return 0;
560         }
561         else
562         {
563                 if(get_use_opengl())
564                 {
565                         run_opengl();
566                         return 0;
567                 }
568
569                 if(!engine) engine = new HueEngine(this, PluginClient::smp + 1);
570                 
571                 engine->process_packages();
572         }
573         return 0;
574 }
575
576 const char* HueEffect::plugin_title() { return _("Hue saturation"); }
577 int HueEffect::is_realtime() { return 1; }
578
579 NEW_WINDOW_MACRO(HueEffect, HueWindow)
580 LOAD_CONFIGURATION_MACRO(HueEffect, HueConfig)
581
582
583 void HueEffect::save_data(KeyFrame *keyframe)
584 {
585         FileXML output;
586         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
587         output.tag.set_title("HUESATURATION");
588         output.tag.set_property("HUE", config.hue);
589         output.tag.set_property("SATURATION", config.saturation);
590         output.tag.set_property("VALUE", config.value);
591         output.append_tag();
592         output.tag.set_title("/HUESATURATION");
593         output.append_tag();
594         output.append_newline();
595         output.terminate_string();
596 }
597 void HueEffect::read_data(KeyFrame *keyframe)
598 {
599         FileXML input;
600         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
601         while(!input.read_tag())
602         {
603                 if(input.tag.title_is("HUESATURATION"))
604                 {
605                         config.hue = input.tag.get_property("HUE", config.hue);
606                         config.saturation = input.tag.get_property("SATURATION", config.saturation);
607                         config.value = input.tag.get_property("VALUE", config.value);
608                 }
609         }
610 }
611 void HueEffect::update_gui()
612 {
613         if(thread)
614         {
615                 ((HueWindow*)thread->window)->lock_window();
616                 load_configuration();
617                 ((HueWindow*)thread->window)->hue->update(config.hue);
618                 ((HueWindow*)thread->window)->saturation->update(config.saturation);
619                 ((HueWindow*)thread->window)->value->update(config.value);
620                 ((HueWindow*)thread->window)->unlock_window();
621         }
622 }
623
624 int HueEffect::handle_opengl()
625 {
626 #ifdef HAVE_GL
627         const char *yuv_saturation_frag = 
628                 "uniform sampler2D tex;\n"
629                 "uniform float s_offset;\n"
630                 "uniform float v_offset;\n"
631                 "void main()\n"
632                 "{\n"
633                 "       vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
634                 "       pixel.r *= v_offset;\n"
635                 "       pixel.gb -= vec2(0.5, 0.5);\n"
636                 "       pixel.g *= s_offset;\n"
637                 "       pixel.b *= s_offset;\n"
638                 "       pixel.gb += vec2(0.5, 0.5);\n"
639                 "       gl_FragColor = pixel;\n"
640                 "}\n";
641
642
643         const char *yuv_frag = 
644                 "uniform sampler2D tex;\n"
645                 "uniform float h_offset;\n"
646                 "uniform float s_offset;\n"
647                 "uniform float v_offset;\n"
648                 "void main()\n"
649                 "{\n"
650                 "       vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
651                         YUV_TO_RGB_FRAG("pixel")
652                         RGB_TO_HSV_FRAG("pixel")
653                 "       pixel.r += h_offset;\n"
654                 "       pixel.g *= s_offset;\n"
655                 "       pixel.b *= v_offset;\n"
656                 "       if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
657                 "       if(pixel.r < 0.0) pixel.r += 360.0;\n"
658                         HSV_TO_RGB_FRAG("pixel")
659                         RGB_TO_YUV_FRAG("pixel")
660                 "       gl_FragColor = pixel;\n"
661                 "}\n";
662
663         const char *rgb_frag = 
664                 "uniform sampler2D tex;\n"
665                 "uniform float h_offset;\n"
666                 "uniform float s_offset;\n"
667                 "uniform float v_offset;\n"
668                 "void main()\n"
669                 "{\n"
670                 "       vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
671                         RGB_TO_HSV_FRAG("pixel")
672                 "       pixel.r += h_offset;\n"
673                 "       pixel.g *= s_offset;\n"
674                 "       pixel.b *= v_offset;\n"
675                 "       if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
676                 "       if(pixel.r < 0.0) pixel.r += 360.0;\n"
677                         HSV_TO_RGB_FRAG("pixel")
678                 "       gl_FragColor = pixel;\n"
679                 "}\n";
680
681
682         get_output()->to_texture();
683         get_output()->enable_opengl();
684
685         unsigned int frag_shader = 0;
686         switch(get_output()->get_color_model())
687         {
688                 case BC_YUV888:
689                 case BC_YUVA8888:
690 // This is a lousy approximation but good enough for the masker.
691                         if(EQUIV(config.hue, 0))
692                                 frag_shader = VFrame::make_shader(0,
693                                         yuv_saturation_frag,
694                                         0);
695                         else
696                                 frag_shader = VFrame::make_shader(0,
697                                         yuv_frag,
698                                         0);
699                         break;
700                 default:
701                         frag_shader = VFrame::make_shader(0,
702                                 rgb_frag,
703                                 0);
704                         break;
705         }
706
707
708         if(frag_shader > 0) 
709         {
710                 glUseProgram(frag_shader);
711                 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
712                 glUniform1f(glGetUniformLocation(frag_shader, "h_offset"), config.hue);
713                 glUniform1f(glGetUniformLocation(frag_shader, "s_offset"), 
714                         ((float)config.saturation - MINSATURATION) / MAXSATURATION);
715                 glUniform1f(glGetUniformLocation(frag_shader, "v_offset"), 
716                         ((float)config.value - MINVALUE) / MAXVALUE);
717         }
718
719         get_output()->init_screen();
720         get_output()->bind_texture(0);
721         get_output()->draw_texture();
722         glUseProgram(0);
723         get_output()->set_opengl_state(VFrame::SCREEN);
724 #endif
725         return 0;
726 }
727
728
729
730
731