switch from eclipse to android studio
[goodguy/cinelerra.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 "bccolors.h"
23 #include "bcdisplayinfo.h"
24 #include "clip.h"
25 #include "bchash.h"
26 #include "filexml.h"
27 #include "guicast.h"
28 #include "language.h"
29 #include "loadbalance.h"
30 #include "bccolors.h"
31 #include "playback3d.h"
32 #include "pluginvclient.h"
33 #include "vframe.h"
34
35 #include <stdint.h>
36 #include <string.h>
37
38
39 class HueEffect;
40
41 #define MINHUE -180
42 #define MAXHUE 180
43 #define MINSATURATION -100
44 #define MAXSATURATION 100
45 #define MINVALUE -100
46 #define MAXVALUE 100
47
48
49
50
51
52
53 class HueConfig
54 {
55 public:
56         HueConfig();
57
58         void copy_from(HueConfig &src);
59         int equivalent(HueConfig &src);
60         void interpolate(HueConfig &prev,
61                 HueConfig &next,
62                 long prev_frame,
63                 long next_frame,
64                 long current_frame);
65         float hue, saturation, value;
66 };
67
68 class HueSlider : public BC_FSlider
69 {
70 public:
71         HueSlider(HueEffect *plugin, int x, int y, int w);
72         int handle_event();
73         HueEffect *plugin;
74 };
75
76 class SaturationSlider : public BC_FSlider
77 {
78 public:
79         SaturationSlider(HueEffect *plugin, int x, int y, int w);
80         int handle_event();
81         char* get_caption();
82         HueEffect *plugin;
83         char string[BCTEXTLEN];
84 };
85
86 class ValueSlider : public BC_FSlider
87 {
88 public:
89         ValueSlider(HueEffect *plugin, int x, int y, int w);
90         int handle_event();
91         char* get_caption();
92         HueEffect *plugin;
93         char string[BCTEXTLEN];
94 };
95
96 class HueWindow : public PluginClientWindow
97 {
98 public:
99         HueWindow(HueEffect *plugin);
100         void create_objects();
101         HueEffect *plugin;
102         HueSlider *hue;
103         SaturationSlider *saturation;
104         ValueSlider *value;
105 };
106
107
108
109 class HueEngine : public LoadServer
110 {
111 public:
112         HueEngine(HueEffect *plugin, int cpus);
113         void init_packages();
114         LoadClient* new_client();
115         LoadPackage* new_package();
116         HueEffect *plugin;
117 };
118
119 class HuePackage : public LoadPackage
120 {
121 public:
122         HuePackage();
123         int row1, row2;
124 };
125
126 class HueUnit : public LoadClient
127 {
128 public:
129         HueUnit(HueEffect *plugin, HueEngine *server);
130         void process_package(LoadPackage *package);
131         HueEffect *plugin;
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.yuv_to_rgb_16(r_i, g_i, b_i, y, u, v); \
403                                 else \
404                                         YUV::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 N_("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->xbuf);
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->xbuf);
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         const char *shader_stack[16];
681         memset(shader_stack,0, sizeof(shader_stack));
682         int current_shader = 0;
683
684         int need_color_matrix = BC_CModels::is_yuv(get_output()->get_color_model()) ? 1 : 0;
685         if( need_color_matrix ) shader_stack[current_shader++] = bc_gl_colors;
686         shader_stack[current_shader++] = !need_color_matrix ? rgb_frag :
687                 EQUIV(config.hue, 0) ? yuv_saturation_frag: yuv_frag ;
688
689         shader_stack[current_shader] = 0;
690         unsigned int shader = VFrame::make_shader(shader_stack);
691         if(shader > 0) {
692                 glUseProgram(shader);
693                 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
694                 glUniform1f(glGetUniformLocation(shader, "h_offset"), config.hue);
695                 glUniform1f(glGetUniformLocation(shader, "s_offset"),
696                         ((float)config.saturation - MINSATURATION) / MAXSATURATION);
697                 glUniform1f(glGetUniformLocation(shader, "v_offset"),
698                         ((float)config.value - MINVALUE) / MAXVALUE);
699                 if( need_color_matrix ) BC_GL_COLORS(shader);
700         }
701
702         get_output()->init_screen();
703         get_output()->bind_texture(0);
704         get_output()->draw_texture();
705         glUseProgram(0);
706         get_output()->set_opengl_state(VFrame::SCREEN);
707 #endif
708         return 0;
709 }
710
711
712
713
714