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