Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / 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         345, 100, 345, 100, 0)
305 {
306         this->plugin = plugin;
307 }
308 void HueWindow::create_objects()
309 {
310         int x = 10, y = 10, x1 = 100;
311         add_subwindow(new BC_Title(x, y, _("Hue:")));
312         add_subwindow(hue = new HueSlider(plugin, x1, y, 200));
313         y += 30;
314         add_subwindow(new BC_Title(x, y, _("Saturation:")));
315         add_subwindow(saturation = new SaturationSlider(plugin, x1, y, 200));
316         y += 30;
317         add_subwindow(new BC_Title(x, y, _("Value:")));
318         add_subwindow(value = new ValueSlider(plugin, x1, y, 200));
319         show_window();
320         flush();
321 }
322
323
324
325
326
327
328
329
330
331
332
333 HueEngine::HueEngine(HueEffect *plugin, int cpus)
334  : LoadServer(cpus, cpus)
335 {
336         this->plugin = plugin;
337 }
338 void HueEngine::init_packages()
339 {
340         for(int i = 0; i < LoadServer::get_total_packages(); i++)
341         {
342                 HuePackage *pkg = (HuePackage*)get_package(i);
343                 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
344                 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
345         }
346 }
347 LoadClient* HueEngine::new_client()
348 {
349         return new HueUnit(plugin, this);
350 }
351 LoadPackage* HueEngine::new_package()
352 {
353         return new HuePackage;
354 }
355
356
357
358
359
360
361
362
363 HuePackage::HuePackage()
364  : LoadPackage()
365 {
366 }
367
368 HueUnit::HueUnit(HueEffect *plugin, HueEngine *server)
369  : LoadClient(server)
370 {
371         this->plugin = plugin;
372 }
373
374
375
376
377
378
379
380 #define HUESATURATION(type, max, components, use_yuv) \
381 { \
382         float h_offset = plugin->config.hue; \
383         float s_offset = ((float)plugin->config.saturation - MINSATURATION) / MAXSATURATION; \
384         float v_offset = ((float)plugin->config.value - MINVALUE) / MAXVALUE; \
385         for(int i = pkg->row1; i < pkg->row2; i++) \
386         { \
387                 type* in_row = (type*)plugin->input->get_rows()[i]; \
388                 type* out_row = (type*)plugin->output->get_rows()[i]; \
389  \
390                 for(int j = 0; j < w; j++) \
391                 { \
392                         float h, s, va; \
393                         int y, u, v; \
394                         float r, g, b; \
395                         int r_i, g_i, b_i; \
396  \
397                         if(use_yuv) \
398                         { \
399                                 y = (int)in_row[0]; \
400                                 u = (int)in_row[1]; \
401                                 v = (int)in_row[2]; \
402                                 if(max == 0xffff) \
403                                         yuv.yuv_to_rgb_16(r_i, g_i, b_i, y, u, v); \
404                                 else \
405                                         yuv.yuv_to_rgb_8(r_i, g_i, b_i, y, u, v); \
406                                 HSV::rgb_to_hsv((float)r_i / max, \
407                                         (float)g_i / max, \
408                                         (float)b_i / max, \
409                                         h, \
410                                         s, \
411                                         va); \
412                         } \
413                         else \
414                         { \
415                                 r = (float)in_row[0] / max; \
416                                 g = (float)in_row[1] / max; \
417                                 b = (float)in_row[2] / max; \
418                                 HSV::rgb_to_hsv(r, g, b, h, s, va); \
419                         } \
420  \
421  \
422                         h += h_offset; \
423                         s *= s_offset; \
424                         va *= v_offset; \
425  \
426                         if(h >= 360) h -= 360; \
427                         if(h < 0) h += 360; \
428                         if(sizeof(type) < 4) \
429                         { \
430                                 if(s > 1) s = 1; \
431                                 if(va > 1) va = 1; \
432                                 if(s < 0) s = 0; \
433                                 if(va < 0) va = 0; \
434                         } \
435  \
436                         if(use_yuv) \
437                         { \
438                                 HSV::hsv_to_yuv(y, u, v, h, s, va, max); \
439                                 out_row[0] = y; \
440                                 out_row[1] = u; \
441                                 out_row[2] = v; \
442                         } \
443                         else \
444                         { \
445                                 HSV::hsv_to_rgb(r, g, b, h, s, va); \
446                                 if(sizeof(type) < 4) \
447                                 { \
448                                         r *= max; \
449                                         g *= max; \
450                                         b *= max; \
451                                         out_row[0] = (type)CLIP(r, 0, max); \
452                                         out_row[1] = (type)CLIP(g, 0, max); \
453                                         out_row[2] = (type)CLIP(b, 0, max); \
454                                 } \
455                                 else \
456                                 { \
457                                         out_row[0] = (type)r; \
458                                         out_row[1] = (type)g; \
459                                         out_row[2] = (type)b; \
460                                 } \
461                         } \
462  \
463                         in_row += components; \
464                         out_row += components; \
465                 } \
466         } \
467 }
468
469
470 void HueUnit::process_package(LoadPackage *package)
471 {
472         HuePackage *pkg = (HuePackage*)package;
473         int w = plugin->input->get_w();
474
475         switch(plugin->input->get_color_model())
476         {
477                 case BC_RGB888:
478                         HUESATURATION(unsigned char, 0xff, 3, 0)
479                         break;
480
481                 case BC_RGB_FLOAT:
482                         HUESATURATION(float, 1, 3, 0)
483                         break;
484
485                 case BC_YUV888:
486                         HUESATURATION(unsigned char, 0xff, 3, 1)
487                         break;
488
489                 case BC_RGB161616:
490                         HUESATURATION(uint16_t, 0xffff, 3, 0)
491                         break;
492
493                 case BC_YUV161616:
494                         HUESATURATION(uint16_t, 0xffff, 3, 1)
495                         break;
496
497                 case BC_RGBA_FLOAT:
498                         HUESATURATION(float, 1, 4, 0)
499                         break;
500
501                 case BC_RGBA8888:
502                         HUESATURATION(unsigned char, 0xff, 4, 0)
503                         break;
504
505                 case BC_YUVA8888:
506                         HUESATURATION(unsigned char, 0xff, 4, 1)
507                         break;
508
509                 case BC_RGBA16161616:
510                         HUESATURATION(uint16_t, 0xffff, 4, 0)
511                         break;
512
513                 case BC_YUVA16161616:
514                         HUESATURATION(uint16_t, 0xffff, 4, 1)
515                         break;
516
517         }
518 }
519
520
521
522
523 REGISTER_PLUGIN(HueEffect)
524
525
526 HueEffect::HueEffect(PluginServer *server)
527  : PluginVClient(server)
528 {
529         engine = 0;
530         
531 }
532 HueEffect::~HueEffect()
533 {
534         
535         if(engine) delete engine;
536 }
537
538 int HueEffect::process_buffer(VFrame *frame,
539         int64_t start_position,
540         double frame_rate)
541 {
542         load_configuration();
543
544         read_frame(frame, 
545                 0, 
546                 start_position, 
547                 frame_rate,
548                 get_use_opengl());
549         
550
551         this->input = frame;
552         this->output = frame;
553         if(EQUIV(config.hue, 0) && EQUIV(config.saturation, 0) && EQUIV(config.value, 0))
554         {
555                 return 0;
556         }
557         else
558         {
559                 if(get_use_opengl())
560                 {
561                         run_opengl();
562                         return 0;
563                 }
564
565                 if(!engine) engine = new HueEngine(this, PluginClient::smp + 1);
566                 
567                 engine->process_packages();
568         }
569         return 0;
570 }
571
572 const char* HueEffect::plugin_title() { return _("Hue saturation"); }
573 int HueEffect::is_realtime() { return 1; }
574
575 NEW_WINDOW_MACRO(HueEffect, HueWindow)
576 LOAD_CONFIGURATION_MACRO(HueEffect, HueConfig)
577
578
579 void HueEffect::save_data(KeyFrame *keyframe)
580 {
581         FileXML output;
582         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
583         output.tag.set_title("HUESATURATION");
584         output.tag.set_property("HUE", config.hue);
585         output.tag.set_property("SATURATION", config.saturation);
586         output.tag.set_property("VALUE", config.value);
587         output.append_tag();
588         output.tag.set_title("/HUESATURATION");
589         output.append_tag();
590         output.append_newline();
591         output.terminate_string();
592 }
593 void HueEffect::read_data(KeyFrame *keyframe)
594 {
595         FileXML input;
596         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
597         while(!input.read_tag())
598         {
599                 if(input.tag.title_is("HUESATURATION"))
600                 {
601                         config.hue = input.tag.get_property("HUE", config.hue);
602                         config.saturation = input.tag.get_property("SATURATION", config.saturation);
603                         config.value = input.tag.get_property("VALUE", config.value);
604                 }
605         }
606 }
607 void HueEffect::update_gui()
608 {
609         if(thread)
610         {
611                 ((HueWindow*)thread->window)->lock_window();
612                 load_configuration();
613                 ((HueWindow*)thread->window)->hue->update(config.hue);
614                 ((HueWindow*)thread->window)->saturation->update(config.saturation);
615                 ((HueWindow*)thread->window)->value->update(config.value);
616                 ((HueWindow*)thread->window)->unlock_window();
617         }
618 }
619
620 int HueEffect::handle_opengl()
621 {
622 #ifdef HAVE_GL
623         const char *yuv_saturation_frag = 
624                 "uniform sampler2D tex;\n"
625                 "uniform float s_offset;\n"
626                 "uniform float v_offset;\n"
627                 "void main()\n"
628                 "{\n"
629                 "       vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
630                 "       pixel.r *= v_offset;\n"
631                 "       pixel.gb -= vec2(0.5, 0.5);\n"
632                 "       pixel.g *= s_offset;\n"
633                 "       pixel.b *= s_offset;\n"
634                 "       pixel.gb += vec2(0.5, 0.5);\n"
635                 "       gl_FragColor = pixel;\n"
636                 "}\n";
637
638
639         const char *yuv_frag = 
640                 "uniform sampler2D tex;\n"
641                 "uniform float h_offset;\n"
642                 "uniform float s_offset;\n"
643                 "uniform float v_offset;\n"
644                 "void main()\n"
645                 "{\n"
646                 "       vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
647                         YUV_TO_RGB_FRAG("pixel")
648                         RGB_TO_HSV_FRAG("pixel")
649                 "       pixel.r += h_offset;\n"
650                 "       pixel.g *= s_offset;\n"
651                 "       pixel.b *= v_offset;\n"
652                 "       if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
653                 "       if(pixel.r < 0.0) pixel.r += 360.0;\n"
654                         HSV_TO_RGB_FRAG("pixel")
655                         RGB_TO_YUV_FRAG("pixel")
656                 "       gl_FragColor = pixel;\n"
657                 "}\n";
658
659         const char *rgb_frag = 
660                 "uniform sampler2D tex;\n"
661                 "uniform float h_offset;\n"
662                 "uniform float s_offset;\n"
663                 "uniform float v_offset;\n"
664                 "void main()\n"
665                 "{\n"
666                 "       vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
667                         RGB_TO_HSV_FRAG("pixel")
668                 "       pixel.r += h_offset;\n"
669                 "       pixel.g *= s_offset;\n"
670                 "       pixel.b *= v_offset;\n"
671                 "       if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
672                 "       if(pixel.r < 0.0) pixel.r += 360.0;\n"
673                         HSV_TO_RGB_FRAG("pixel")
674                 "       gl_FragColor = pixel;\n"
675                 "}\n";
676
677
678         get_output()->to_texture();
679         get_output()->enable_opengl();
680
681         unsigned int frag_shader = 0;
682         switch(get_output()->get_color_model())
683         {
684                 case BC_YUV888:
685                 case BC_YUVA8888:
686 // This is a lousy approximation but good enough for the masker.
687                         if(EQUIV(config.hue, 0))
688                                 frag_shader = VFrame::make_shader(0,
689                                         yuv_saturation_frag,
690                                         0);
691                         else
692                                 frag_shader = VFrame::make_shader(0,
693                                         yuv_frag,
694                                         0);
695                         break;
696                 default:
697                         frag_shader = VFrame::make_shader(0,
698                                 rgb_frag,
699                                 0);
700                         break;
701         }
702
703
704         if(frag_shader > 0) 
705         {
706                 glUseProgram(frag_shader);
707                 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
708                 glUniform1f(glGetUniformLocation(frag_shader, "h_offset"), config.hue);
709                 glUniform1f(glGetUniformLocation(frag_shader, "s_offset"), 
710                         ((float)config.saturation - MINSATURATION) / MAXSATURATION);
711                 glUniform1f(glGetUniformLocation(frag_shader, "v_offset"), 
712                         ((float)config.value - MINVALUE) / MAXVALUE);
713         }
714
715         get_output()->init_screen();
716         get_output()->bind_texture(0);
717         get_output()->draw_texture();
718         glUseProgram(0);
719         get_output()->set_opengl_state(VFrame::SCREEN);
720 #endif
721         return 0;
722 }
723
724
725
726
727