opengl colorspace + BT2020
[goodguy/history.git] / cinelerra-5.1 / plugins / histogram / histogram.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008-2012 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 <math.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "bcdisplayinfo.h"
28 #include "bcsignals.h"
29 #include "clip.h"
30 #include "bchash.h"
31 #include "filexml.h"
32 #include "histogram.h"
33 #include "histogramconfig.h"
34 #include "histogramwindow.h"
35 #include "keyframe.h"
36 #include "language.h"
37 #include "loadbalance.h"
38 #include "playback3d.h"
39 #include "bccolors.h"
40 #include "vframe.h"
41 #include "workarounds.h"
42
43 #include "aggregated.h"
44 #include "../colorbalance/aggregated.h"
45 #include "../interpolate/aggregated.h"
46 #include "../gamma/aggregated.h"
47
48 class HistogramMain;
49 class HistogramEngine;
50 class HistogramWindow;
51
52
53
54
55
56 REGISTER_PLUGIN(HistogramMain)
57
58
59
60
61
62
63
64
65
66 HistogramMain::HistogramMain(PluginServer *server)
67  : PluginVClient(server)
68 {
69
70         engine = 0;
71         for(int i = 0; i < HISTOGRAM_MODES; i++)
72         {
73                 lookup[i] = 0;
74                 accum[i] = 0;
75                 preview_lookup[i] = 0;
76         }
77         current_point = -1;
78         mode = HISTOGRAM_VALUE;
79         dragging_point = 0;
80         input = 0;
81         output = 0;
82         w = 440;
83         h = 500;
84         parade = 0;
85 }
86
87 HistogramMain::~HistogramMain()
88 {
89
90         for(int i = 0; i < HISTOGRAM_MODES;i++)
91         {
92                 delete [] lookup[i];
93                 delete [] accum[i];
94                 delete [] preview_lookup[i];
95         }
96         delete engine;
97 }
98
99 const char* HistogramMain::plugin_title() { return _("Histogram"); }
100 int HistogramMain::is_realtime() { return 1; }
101
102
103
104 NEW_WINDOW_MACRO(HistogramMain, HistogramWindow)
105
106 LOAD_CONFIGURATION_MACRO(HistogramMain, HistogramConfig)
107
108 void HistogramMain::render_gui(void *data)
109 {
110         if(thread)
111         {
112 // Process just the RGB values to determine the automatic points or
113 // all the points if manual
114                 if(!config.automatic)
115                 {
116 // Generate curves for value histogram
117 // Lock out changes to curves
118                         ((HistogramWindow*)thread->window)->lock_window("HistogramMain::render_gui 1");
119                         tabulate_curve(HISTOGRAM_RED, 0);
120                         tabulate_curve(HISTOGRAM_GREEN, 0);
121                         tabulate_curve(HISTOGRAM_BLUE, 0);
122                         ((HistogramWindow*)thread->window)->unlock_window();
123                 }
124
125                 calculate_histogram((VFrame*)data, !config.automatic);
126
127
128                 if(config.automatic)
129                 {
130                         calculate_automatic((VFrame*)data);
131
132 // Generate curves for value histogram
133 // Lock out changes to curves
134                         ((HistogramWindow*)thread->window)->lock_window("HistogramMain::render_gui 1");
135                         tabulate_curve(HISTOGRAM_RED, 0);
136                         tabulate_curve(HISTOGRAM_GREEN, 0);
137                         tabulate_curve(HISTOGRAM_BLUE, 0);
138                         ((HistogramWindow*)thread->window)->unlock_window();
139
140
141 // Need a second pass to get the luminance values.
142                         calculate_histogram((VFrame*)data, 1);
143                 }
144
145                 ((HistogramWindow*)thread->window)->lock_window("HistogramMain::render_gui 2");
146 // Always draw the histogram but don't update widgets if automatic
147                 ((HistogramWindow*)thread->window)->update(1,
148                         config.automatic && mode != HISTOGRAM_VALUE,
149                         config.automatic && mode != HISTOGRAM_VALUE,
150                         0);
151
152                 ((HistogramWindow*)thread->window)->unlock_window();
153         }
154 }
155
156 void HistogramMain::update_gui()
157 {
158         if(thread)
159         {
160                 ((HistogramWindow*)thread->window)->lock_window("HistogramMain::update_gui");
161                 int reconfigure = load_configuration();
162                 if(reconfigure)
163                 {
164                         ((HistogramWindow*)thread->window)->update(1,
165                                 1,
166                                 1,
167                                 1);
168                 }
169                 ((HistogramWindow*)thread->window)->unlock_window();
170         }
171 }
172
173
174
175
176 void HistogramMain::save_data(KeyFrame *keyframe)
177 {
178         FileXML output;
179
180 // cause data to be stored directly in text
181         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
182         output.tag.set_title("HISTOGRAM");
183
184         char string[BCTEXTLEN];
185
186         output.tag.set_property("AUTOMATIC", config.automatic);
187         output.tag.set_property("THRESHOLD", config.threshold);
188         output.tag.set_property("PLOT", config.plot);
189         output.tag.set_property("SPLIT", config.split);
190         output.tag.set_property("W", w);
191         output.tag.set_property("H", h);
192         output.tag.set_property("PARADE", parade);
193         output.tag.set_property("MODE", mode);
194
195         for(int i = 0; i < HISTOGRAM_MODES; i++)
196         {
197                 sprintf(string, "LOW_OUTPUT_%d", i);
198                 output.tag.set_property(string, config.low_output[i]);
199                 sprintf(string, "HIGH_OUTPUT_%d", i);
200                 output.tag.set_property(string, config.high_output[i]);
201                 sprintf(string, "LOW_INPUT_%d", i);
202                 output.tag.set_property(string, config.low_input[i]);
203                 sprintf(string, "HIGH_INPUT_%d", i);
204                 output.tag.set_property(string, config.high_input[i]);
205                 sprintf(string, "GAMMA_%d", i);
206                 output.tag.set_property(string, config.gamma[i]);
207 //printf("HistogramMain::save_data %d %f %d\n", config.input_min[i], config.input_mid[i], config.input_max[i]);
208         }
209
210         output.append_tag();
211         output.tag.set_title("/HISTOGRAM");
212         output.append_tag();
213         output.append_newline();
214         output.terminate_string();
215 }
216
217 void HistogramMain::read_data(KeyFrame *keyframe)
218 {
219         FileXML input;
220
221         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
222
223         int result = 0;
224
225
226         while(!result)
227         {
228                 result = input.read_tag();
229
230                 if(!result)
231                 {
232                         if(input.tag.title_is("HISTOGRAM"))
233                         {
234                                 config.automatic = input.tag.get_property("AUTOMATIC", config.automatic);
235                                 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
236                                 config.plot = input.tag.get_property("PLOT", config.plot);
237                                 config.split = input.tag.get_property("SPLIT", config.split);
238
239                                 if(is_defaults())
240                                 {
241                                         w = input.tag.get_property("W", w);
242                                         h = input.tag.get_property("H", h);
243                                         parade = input.tag.get_property("PARADE", parade);
244                                         mode = input.tag.get_property("MODE", mode);
245                                 }
246
247                                 char string[BCTEXTLEN];
248                                 for(int i = 0; i < HISTOGRAM_MODES; i++)
249                                 {
250                                         sprintf(string, "LOW_OUTPUT_%d", i);
251                                         config.low_output[i] = input.tag.get_property(string, config.low_output[i]);
252                                         sprintf(string, "HIGH_OUTPUT_%d", i);
253                                         config.high_output[i] = input.tag.get_property(string, config.high_output[i]);
254                                         sprintf(string, "GAMMA_%d", i);
255                                         config.gamma[i] = input.tag.get_property(string, config.gamma[i]);
256
257                                         if(i == HISTOGRAM_VALUE || !config.automatic)
258                                         {
259                                                 sprintf(string, "LOW_INPUT_%d", i);
260                                                 config.low_input[i] = input.tag.get_property(string, config.low_input[i]);
261                                                 sprintf(string, "HIGH_INPUT_%d", i);
262                                                 config.high_input[i] = input.tag.get_property(string, config.high_input[i]);
263                                         }
264 //printf("HistogramMain::read_data %d %f %d\n", config.input_min[i], config.input_mid[i], config.input_max[i]);
265                                 }
266                         }
267                 }
268         }
269
270         config.boundaries();
271
272 }
273
274 float HistogramMain::calculate_level(float input,
275         int mode,
276         int use_value)
277 {
278         float output = 0.0;
279
280 // Scale to input range
281         if(!EQUIV(config.high_input[mode], config.low_input[mode]))
282         {
283                 output = (input - config.low_input[mode]) /
284                         (config.high_input[mode] - config.low_input[mode]);
285         }
286         else
287         {
288                 output = input;
289         }
290
291
292
293         if(!EQUIV(config.gamma[mode], 0))
294         {
295                 output = pow(output, 1.0 / config.gamma[mode]);
296                 CLAMP(output, 0, 1.0);
297         }
298
299 // Apply value curve
300         if(use_value && mode != HISTOGRAM_VALUE)
301         {
302                 output = calculate_level(output, HISTOGRAM_VALUE, 0);
303         }
304
305
306
307
308 // scale to output range
309         if(!EQUIV(config.low_output[mode], config.high_output[mode]))
310         {
311                 output = output * (config.high_output[mode] - config.low_output[mode]) +
312                         config.low_output[mode];
313         }
314
315         CLAMP(output, 0, 1.0);
316
317         return output;
318 }
319
320
321
322 void HistogramMain::calculate_histogram(VFrame *data, int do_value)
323 {
324
325         if(!engine) engine = new HistogramEngine(this,
326                 get_project_smp() + 1,
327                 get_project_smp() + 1);
328
329         if(!accum[0])
330         {
331                 for(int i = 0; i < HISTOGRAM_MODES; i++)
332                         accum[i] = new int[HISTOGRAM_SLOTS];
333         }
334
335         engine->process_packages(HistogramEngine::HISTOGRAM, data, do_value);
336
337         for(int i = 0; i < engine->get_total_clients(); i++)
338         {
339                 HistogramUnit *unit = (HistogramUnit*)engine->get_client(i);
340
341                 if(i == 0)
342                 {
343                         for(int j = 0; j < HISTOGRAM_MODES; j++)
344                         {
345                                 memcpy(accum[j], unit->accum[j], sizeof(int) * HISTOGRAM_SLOTS);
346                         }
347                 }
348                 else
349                 {
350                         for(int j = 0; j < HISTOGRAM_MODES; j++)
351                         {
352                                 int *out = accum[j];
353                                 int *in = unit->accum[j];
354                                 for(int k = 0; k < HISTOGRAM_SLOTS; k++)
355                                         out[k] += in[k];
356                         }
357                 }
358         }
359
360 // Remove top and bottom from calculations.  Doesn't work in high
361 // precision colormodels.
362         for(int i = 0; i < HISTOGRAM_MODES; i++)
363         {
364                 accum[i][0] = 0;
365                 accum[i][HISTOGRAM_SLOTS - 1] = 0;
366         }
367 }
368
369
370 void HistogramMain::calculate_automatic(VFrame *data)
371 {
372         calculate_histogram(data, 0);
373         config.reset_points(1);
374
375 // Do each channel
376         for(int i = 0; i < 3; i++)
377         {
378                 int *accum = this->accum[i];
379                 int pixels = data->get_w() * data->get_h();
380                 float white_fraction = 1.0 - (1.0 - config.threshold) / 2;
381                 int threshold = (int)(white_fraction * pixels);
382                 int total = 0;
383                 float max_level = 1.0;
384                 float min_level = 0.0;
385
386 // Get histogram slot above threshold of pixels
387                 for(int j = 0; j < HISTOGRAM_SLOTS; j++)
388                 {
389                         total += accum[j];
390                         if(total >= threshold)
391                         {
392                                 max_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
393                                 break;
394                         }
395                 }
396
397 // Get slot below 99% of pixels
398                 total = 0;
399                 for(int j = HISTOGRAM_SLOTS - 1; j >= 0; j--)
400                 {
401                         total += accum[j];
402                         if(total >= threshold)
403                         {
404                                 min_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
405                                 break;
406                         }
407                 }
408
409
410                 config.low_input[i] = min_level;
411                 config.high_input[i] = max_level;
412         }
413 }
414
415
416
417
418
419 int HistogramMain::calculate_use_opengl()
420 {
421 // glHistogram doesn't work.
422         int result = get_use_opengl() &&
423                 !config.automatic &&
424                 (!config.plot || !gui_open());
425         return result;
426 }
427
428
429 int HistogramMain::process_buffer(VFrame *frame,
430         int64_t start_position,
431         double frame_rate)
432 {
433         int need_reconfigure = load_configuration();
434
435
436
437         int use_opengl = calculate_use_opengl();
438
439 //printf("%d\n", use_opengl);
440         read_frame(frame,
441                 0,
442                 start_position,
443                 frame_rate,
444                 use_opengl);
445
446 // Apply histogram in hardware
447         if(use_opengl) return run_opengl();
448
449         if(!engine) engine = new HistogramEngine(this,
450                 get_project_smp() + 1,
451                 get_project_smp() + 1);
452         this->input = frame;
453         this->output = frame;
454 // Always plot to set the curves if automatic
455         if(config.plot || config.automatic) send_render_gui(frame);
456
457 // Generate tables here.  The same table is used by many packages to render
458 // each horizontal stripe.  Need to cover the entire output range in  each
459 // table to avoid green borders
460
461
462         if(need_reconfigure ||
463                 !lookup[0] ||
464                 config.automatic)
465         {
466 // Calculate new curves
467                 if(config.automatic)
468                 {
469                         calculate_automatic(input);
470                 }
471
472
473 // Generate transfer tables with value function for integer colormodels.
474                 for(int i = 0; i < 3; i++)
475                         tabulate_curve(i, 1);
476         }
477
478 // printf("HistogramMain::process_buffer %d %f %f %f  %f %f %f  %f %f %f\n",
479 // __LINE__,
480 // config.low_input[HISTOGRAM_RED],
481 // config.gamma[HISTOGRAM_RED],
482 // config.high_input[HISTOGRAM_RED],
483 // config.low_input[HISTOGRAM_GREEN],
484 // config.gamma[HISTOGRAM_GREEN],
485 // config.high_input[HISTOGRAM_GREEN],
486 // config.low_input[HISTOGRAM_BLUE],
487 // config.gamma[HISTOGRAM_BLUE],
488 // config.high_input[HISTOGRAM_BLUE]);
489
490
491
492 // Apply histogram
493         engine->process_packages(HistogramEngine::APPLY, input, 0);
494
495
496         return 0;
497 }
498
499 void HistogramMain::tabulate_curve(int subscript, int use_value)
500 {
501         int i;
502         if(!lookup[subscript])
503                 lookup[subscript] = new int[HISTOGRAM_SLOTS];
504         if(!preview_lookup[subscript])
505                 preview_lookup[subscript] = new int[HISTOGRAM_SLOTS];
506
507 //printf("HistogramMain::tabulate_curve %d input=%p\n", __LINE__, input);
508
509
510 // Generate lookup tables for integer colormodels
511         if(input)
512         {
513                 switch(input->get_color_model())
514                 {
515                         case BC_RGB888:
516                         case BC_RGBA8888:
517                                 for(i = 0; i < 0x100; i++)
518                                 {
519                                         lookup[subscript][i] =
520                                                 (int)(calculate_level((float)i / 0xff, subscript, use_value) *
521                                                 0xff);
522                                         CLAMP(lookup[subscript][i], 0, 0xff);
523                                 }
524                                 break;
525 // All other integer colormodels are converted to 16 bit RGB
526                         default:
527                                 for(i = 0; i < 0x10000; i++)
528                                 {
529                                         lookup[subscript][i] =
530                                                 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
531                                                 0xffff);
532                                         CLAMP(lookup[subscript][i], 0, 0xffff);
533                                 }
534 // for(i = 0; i < 0x100; i++)
535 // {
536 // if(subscript == HISTOGRAM_BLUE) printf("%d ", lookup[subscript][i * 0x100]);
537 // }
538 // if(subscript == HISTOGRAM_BLUE) printf("\n");
539
540                                 break;
541                 }
542         }
543
544 // Lookup table for preview only used for GUI
545         if(!use_value)
546         {
547                 for(i = 0; i < 0x10000; i++)
548                 {
549                         preview_lookup[subscript][i] =
550                                 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
551                                 0xffff);
552                         CLAMP(preview_lookup[subscript][i], 0, 0xffff);
553                 }
554         }
555 }
556
557 int HistogramMain::handle_opengl()
558 {
559 #ifdef HAVE_GL
560 // Functions to get pixel from either previous effect or texture
561         static const char *histogram_get_pixel1 =
562                 "vec4 histogram_get_pixel()\n"
563                 "{\n"
564                 "       return gl_FragColor;\n"
565                 "}\n";
566
567         static const char *histogram_get_pixel2 =
568                 "uniform sampler2D tex;\n"
569                 "vec4 histogram_get_pixel()\n"
570                 "{\n"
571                 "       return texture2D(tex, gl_TexCoord[0].st);\n"
572                 "}\n";
573
574         static const char *head_frag =
575                 "uniform vec4 low_input;\n"
576                 "uniform vec4 high_input;\n"
577                 "uniform vec4 gamma;\n"
578                 "uniform vec4 low_output;\n"
579                 "uniform vec4 output_scale;\n"
580                 "void main()\n"
581                 "{\n"
582                 "       float temp = 0.0;\n";
583
584         static const char *get_rgb_frag =
585                 "       vec4 pixel = histogram_get_pixel();\n";
586
587         static const char *get_yuv_frag =
588                 "       vec4 pixel = histogram_get_pixel();\n"
589                         YUV_TO_RGB_FRAG("pixel");
590
591 #define APPLY_INPUT_CURVE(PIXEL, LOW_INPUT, HIGH_INPUT, GAMMA) \
592                 "// apply input curve\n" \
593                 "       temp = (" PIXEL " - " LOW_INPUT ") / \n" \
594                 "               (" HIGH_INPUT " - " LOW_INPUT ");\n" \
595                 "       temp = max(temp, 0.0);\n" \
596                 "       " PIXEL " = pow(temp, 1.0 / " GAMMA ");\n"
597
598
599
600         static const char *apply_histogram_frag =
601                 APPLY_INPUT_CURVE("pixel.r", "low_input.r", "high_input.r", "gamma.r")
602                 APPLY_INPUT_CURVE("pixel.g", "low_input.g", "high_input.g", "gamma.g")
603                 APPLY_INPUT_CURVE("pixel.b", "low_input.b", "high_input.b", "gamma.b")
604                 "// apply output curve\n"
605                 "       pixel.rgb *= output_scale.rgb;\n"
606                 "       pixel.rgb += low_output.rgb;\n"
607                 APPLY_INPUT_CURVE("pixel.r", "low_input.a", "high_input.a", "gamma.a")
608                 APPLY_INPUT_CURVE("pixel.g", "low_input.a", "high_input.a", "gamma.a")
609                 APPLY_INPUT_CURVE("pixel.b", "low_input.a", "high_input.a", "gamma.a")
610                 "// apply output curve\n"
611                 "       pixel.rgb *= vec3(output_scale.a, output_scale.a, output_scale.a);\n"
612                 "       pixel.rgb += vec3(low_output.a, low_output.a, low_output.a);\n";
613
614         static const char *put_rgb_frag =
615                 "       gl_FragColor = pixel;\n"
616                 "}\n";
617
618         static const char *put_yuv_frag =
619                         RGB_TO_YUV_FRAG("pixel")
620                 "       gl_FragColor = pixel;\n"
621                 "}\n";
622
623
624
625         get_output()->to_texture();
626         get_output()->enable_opengl();
627
628         const char *shader_stack[16];
629         memset(shader_stack,0, sizeof(shader_stack));
630         int current_shader = 0;
631
632         int need_color_matrix = BC_CModels::is_yuv(get_output()->get_color_model()) ? 1 : 0;
633         if( need_color_matrix )
634                 shader_stack[current_shader++] = bc_gl_colors;
635
636         int aggregate_interpolation = 0;
637         int aggregate_gamma = 0;
638         int aggregate_colorbalance = 0;
639 // All aggregation possibilities must be accounted for because unsupported
640 // effects can get in between the aggregation members.
641         if(!strcmp(get_output()->get_prev_effect(2), _("Interpolate Pixels")) &&
642                 !strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
643                 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
644         {
645                 aggregate_interpolation = 1;
646                 aggregate_gamma = 1;
647                 aggregate_colorbalance = 1;
648         }
649         else
650         if(!strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
651                 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
652         {
653                 aggregate_gamma = 1;
654                 aggregate_colorbalance = 1;
655         }
656         else
657         if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
658                 !strcmp(get_output()->get_prev_effect(0), _("Gamma")))
659         {
660                 aggregate_interpolation = 1;
661                 aggregate_gamma = 1;
662         }
663         else
664         if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
665                 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
666         {
667                 aggregate_interpolation = 1;
668                 aggregate_colorbalance = 1;
669         }
670         else
671         if(!strcmp(get_output()->get_prev_effect(0), _("Interpolate Pixels")))
672                 aggregate_interpolation = 1;
673         else
674         if(!strcmp(get_output()->get_prev_effect(0), _("Gamma")))
675                 aggregate_gamma = 1;
676         else
677         if(!strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
678                 aggregate_colorbalance = 1;
679
680         if( BC_CModels::is_yuv(get_output()->get_color_model()) )
681                 shader_stack[current_shader++] = bc_gl_colors;
682
683 // The order of processing is fixed by this sequence
684         if(aggregate_interpolation)
685                 INTERPOLATE_COMPILE(shader_stack, current_shader);
686
687         if(aggregate_gamma)
688                 GAMMA_COMPILE(shader_stack, current_shader,
689                         aggregate_interpolation);
690
691         if(aggregate_colorbalance)
692                 COLORBALANCE_COMPILE(shader_stack, current_shader,
693                         aggregate_interpolation || aggregate_gamma);
694
695         shader_stack[current_shader++] = 
696                 aggregate_interpolation || aggregate_gamma || aggregate_colorbalance ?
697                         histogram_get_pixel1 : histogram_get_pixel2;
698
699         shader_stack[current_shader++] = head_frag;
700         shader_stack[current_shader++] = BC_CModels::is_yuv(get_output()->get_color_model()) ?
701                         get_yuv_frag : get_rgb_frag;
702         shader_stack[current_shader++] = apply_histogram_frag;
703         shader_stack[current_shader++] = BC_CModels::is_yuv(get_output()->get_color_model()) ?
704                         put_yuv_frag : put_rgb_frag;
705
706         shader_stack[current_shader] = 0;
707         unsigned int shader = VFrame::make_shader(shader_stack);
708
709 // printf("HistogramMain::handle_opengl %d %d %d %d shader=%d\n",
710 // aggregate_interpolation,
711 // aggregate_gamma,
712 // aggregate_colorbalance,
713 // current_shader,
714 // shader);
715
716         float low_input[4];
717         float high_input[4];
718         float gamma[4];
719         float low_output[4];
720         float output_scale[4];
721
722
723 // printf("min x    min y    max x    max y\n");
724 // printf("%f %f %f %f\n", input_min_r[0], input_min_r[1], input_max_r[0], input_max_r[1]);
725 // printf("%f %f %f %f\n", input_min_g[0], input_min_g[1], input_max_g[0], input_max_g[1]);
726 // printf("%f %f %f %f\n", input_min_b[0], input_min_b[1], input_max_b[0], input_max_b[1]);
727 // printf("%f %f %f %f\n", input_min_v[0], input_min_v[1], input_max_v[0], input_max_v[1]);
728
729         for(int i = 0; i < HISTOGRAM_MODES; i++)
730         {
731                 low_input[i] = config.low_input[i];
732                 high_input[i] = config.high_input[i];
733                 gamma[i] = config.gamma[i];
734                 low_output[i] = config.low_output[i];
735                 output_scale[i] = config.high_output[i] - config.low_output[i];
736         }
737
738         if(shader > 0)
739         {
740                 glUseProgram(shader);
741                 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
742                 if(aggregate_gamma) GAMMA_UNIFORMS(shader);
743                 if(aggregate_interpolation) INTERPOLATE_UNIFORMS(shader);
744                 if(aggregate_colorbalance) COLORBALANCE_UNIFORMS(shader);
745                 glUniform4fv(glGetUniformLocation(shader, "low_input"), 1, low_input);
746                 glUniform4fv(glGetUniformLocation(shader, "high_input"), 1, high_input);
747                 glUniform4fv(glGetUniformLocation(shader, "gamma"), 1, gamma);
748                 glUniform4fv(glGetUniformLocation(shader, "low_output"), 1, low_output);
749                 glUniform4fv(glGetUniformLocation(shader, "output_scale"), 1, output_scale);
750                 if( need_color_matrix ) BC_GL_COLORS(shader);
751         }
752
753         get_output()->init_screen();
754         get_output()->bind_texture(0);
755
756         glDisable(GL_BLEND);
757
758 // Draw the affected half
759         if(config.split)
760         {
761                 glBegin(GL_TRIANGLES);
762                 glNormal3f(0, 0, 1.0);
763
764                 glTexCoord2f(0.0 / get_output()->get_texture_w(),
765                         0.0 / get_output()->get_texture_h());
766                 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
767
768
769                 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
770                         (float)get_output()->get_h() / get_output()->get_texture_h());
771                 glVertex3f((float)get_output()->get_w(), -0.0, 0);
772
773                 glTexCoord2f(0.0 / get_output()->get_texture_w(),
774                         (float)get_output()->get_h() / get_output()->get_texture_h());
775                 glVertex3f(0.0, -0.0, 0);
776
777
778                 glEnd();
779         }
780         else
781         {
782                 get_output()->draw_texture();
783         }
784
785         glUseProgram(0);
786
787 // Draw the unaffected half
788         if(config.split)
789         {
790                 glBegin(GL_TRIANGLES);
791                 glNormal3f(0, 0, 1.0);
792
793
794                 glTexCoord2f(0.0 / get_output()->get_texture_w(),
795                         0.0 / get_output()->get_texture_h());
796                 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
797
798                 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
799                         0.0 / get_output()->get_texture_h());
800                 glVertex3f((float)get_output()->get_w(),
801                         -(float)get_output()->get_h(), 0);
802
803                 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
804                         (float)get_output()->get_h() / get_output()->get_texture_h());
805                 glVertex3f((float)get_output()->get_w(), -0.0, 0);
806
807
808                 glEnd();
809         }
810
811         get_output()->set_opengl_state(VFrame::SCREEN);
812 #endif
813         return 0;
814 }
815
816
817
818
819
820
821
822
823
824
825
826
827 HistogramPackage::HistogramPackage()
828  : LoadPackage()
829 {
830 }
831
832
833
834
835 HistogramUnit::HistogramUnit(HistogramEngine *server,
836         HistogramMain *plugin)
837  : LoadClient(server)
838 {
839         this->plugin = plugin;
840         this->server = server;
841         for(int i = 0; i < HISTOGRAM_MODES; i++)
842                 accum[i] = new int[HISTOGRAM_SLOTS];
843 }
844
845 HistogramUnit::~HistogramUnit()
846 {
847         for(int i = 0; i < HISTOGRAM_MODES; i++)
848                 delete [] accum[i];
849 }
850
851 void HistogramUnit::process_package(LoadPackage *package)
852 {
853         HistogramPackage *pkg = (HistogramPackage*)package;
854
855         if(server->operation == HistogramEngine::HISTOGRAM)
856         {
857                 int do_value = server->do_value;
858
859
860 #define HISTOGRAM_HEAD(type) \
861 { \
862         for(int i = pkg->start; i < pkg->end; i++) \
863         { \
864                 type *row = (type*)data->get_rows()[i]; \
865                 for(int j = 0; j < w; j++) \
866                 {
867
868 #define HISTOGRAM_TAIL(components) \
869 /* Value takes the maximum of the output RGB values */ \
870                         if(do_value) \
871                         { \
872                                 CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
873                                 CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
874                                 CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
875                                 r_out = lookup_r[r]; \
876                                 g_out = lookup_g[g]; \
877                                 b_out = lookup_b[b]; \
878 /*                              v = (r * 76 + g * 150 + b * 29) >> 8; */ \
879                                 v = MAX(r_out, g_out); \
880                                 v = MAX(v, b_out); \
881                                 v += -HISTOGRAM_MIN * 0xffff / 100; \
882                                 CLAMP(v, 0, HISTOGRAM_SLOTS - 1); \
883                                 accum_v[v]++; \
884                         } \
885  \
886                         r += -HISTOGRAM_MIN * 0xffff / 100; \
887                         g += -HISTOGRAM_MIN * 0xffff / 100; \
888                         b += -HISTOGRAM_MIN * 0xffff / 100; \
889                         CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
890                         CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
891                         CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
892                         accum_r[r]++; \
893                         accum_g[g]++; \
894                         accum_b[b]++; \
895                         row += components; \
896                 } \
897         } \
898 }
899
900
901
902
903                 VFrame *data = server->data;
904                 int w = data->get_w();
905                 //int h = data->get_h();
906                 int *accum_r = accum[HISTOGRAM_RED];
907                 int *accum_g = accum[HISTOGRAM_GREEN];
908                 int *accum_b = accum[HISTOGRAM_BLUE];
909                 int *accum_v = accum[HISTOGRAM_VALUE];
910                 int32_t r, g, b, y, u, v;
911                 int r_out, g_out, b_out;
912                 int *lookup_r = plugin->preview_lookup[HISTOGRAM_RED];
913                 int *lookup_g = plugin->preview_lookup[HISTOGRAM_GREEN];
914                 int *lookup_b = plugin->preview_lookup[HISTOGRAM_BLUE];
915
916                 switch(data->get_color_model())
917                 {
918                         case BC_RGB888:
919                                 HISTOGRAM_HEAD(unsigned char)
920                                 r = (row[0] << 8) | row[0];
921                                 g = (row[1] << 8) | row[1];
922                                 b = (row[2] << 8) | row[2];
923                                 HISTOGRAM_TAIL(3)
924                                 break;
925                         case BC_RGB_FLOAT:
926                                 HISTOGRAM_HEAD(float)
927                                 r = (int)(row[0] * 0xffff);
928                                 g = (int)(row[1] * 0xffff);
929                                 b = (int)(row[2] * 0xffff);
930                                 HISTOGRAM_TAIL(3)
931                                 break;
932                         case BC_YUV888:
933                                 HISTOGRAM_HEAD(unsigned char)
934                                 y = (row[0] << 8) | row[0];
935                                 u = (row[1] << 8) | row[1];
936                                 v = (row[2] << 8) | row[2];
937                                 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
938                                 HISTOGRAM_TAIL(3)
939                                 break;
940                         case BC_RGBA8888:
941                                 HISTOGRAM_HEAD(unsigned char)
942                                 r = (row[0] << 8) | row[0];
943                                 g = (row[1] << 8) | row[1];
944                                 b = (row[2] << 8) | row[2];
945                                 HISTOGRAM_TAIL(4)
946                                 break;
947                         case BC_RGBA_FLOAT:
948                                 HISTOGRAM_HEAD(float)
949                                 r = (int)(row[0] * 0xffff);
950                                 g = (int)(row[1] * 0xffff);
951                                 b = (int)(row[2] * 0xffff);
952                                 HISTOGRAM_TAIL(4)
953                                 break;
954                         case BC_YUVA8888:
955                                 HISTOGRAM_HEAD(unsigned char)
956                                 y = (row[0] << 8) | row[0];
957                                 u = (row[1] << 8) | row[1];
958                                 v = (row[2] << 8) | row[2];
959                                 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
960                                 HISTOGRAM_TAIL(4)
961                                 break;
962                         case BC_RGB161616:
963                                 HISTOGRAM_HEAD(uint16_t)
964                                 r = row[0];
965                                 g = row[1];
966                                 b = row[2];
967                                 HISTOGRAM_TAIL(3)
968                                 break;
969                         case BC_YUV161616:
970                                 HISTOGRAM_HEAD(uint16_t)
971                                 y = row[0];
972                                 u = row[1];
973                                 v = row[2];
974                                 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
975                                 HISTOGRAM_TAIL(3)
976                                 break;
977                         case BC_RGBA16161616:
978                                 HISTOGRAM_HEAD(uint16_t)
979                                 r = row[0];
980                                 g = row[1];
981                                 b = row[2];
982                                 HISTOGRAM_TAIL(3)
983                                 break;
984                         case BC_YUVA16161616:
985                                 HISTOGRAM_HEAD(uint16_t)
986                                 y = row[0];
987                                 u = row[1];
988                                 v = row[2];
989                                 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
990                                 HISTOGRAM_TAIL(4)
991                                 break;
992                 }
993         }
994         else
995         if(server->operation == HistogramEngine::APPLY)
996         {
997
998
999
1000 #define PROCESS(type, components) \
1001 { \
1002         for(int i = pkg->start; i < pkg->end; i++) \
1003         { \
1004                 type *row = (type*)input->get_rows()[i]; \
1005                 for(int j = 0; j < w; j++) \
1006                 { \
1007                         if ( plugin->config.split && ((j + i * w / h) < w) ) \
1008                         continue; \
1009                         row[0] = lookup_r[row[0]]; \
1010                         row[1] = lookup_g[row[1]]; \
1011                         row[2] = lookup_b[row[2]]; \
1012                         row += components; \
1013                 } \
1014         } \
1015 }
1016
1017 #define PROCESS_YUV(type, components, max) \
1018 { \
1019         for(int i = pkg->start; i < pkg->end; i++) \
1020         { \
1021                 type *row = (type*)input->get_rows()[i]; \
1022                 for(int j = 0; j < w; j++) \
1023                 { \
1024                         if ( plugin->config.split && ((j + i * w / h) < w) ) \
1025                         continue; \
1026 /* Convert to 16 bit RGB */ \
1027                         if(max == 0xff) \
1028                         { \
1029                                 y = (row[0] << 8) | row[0]; \
1030                                 u = (row[1] << 8) | row[1]; \
1031                                 v = (row[2] << 8) | row[2]; \
1032                         } \
1033                         else \
1034                         { \
1035                                 y = row[0]; \
1036                                 u = row[1]; \
1037                                 v = row[2]; \
1038                         } \
1039  \
1040                         YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
1041  \
1042 /* Look up in RGB domain */ \
1043                         r = lookup_r[r]; \
1044                         g = lookup_g[g]; \
1045                         b = lookup_b[b]; \
1046  \
1047 /* Convert to 16 bit YUV */ \
1048                         YUV::yuv.rgb_to_yuv_16(r, g, b, y, u, v); \
1049  \
1050                         if(max == 0xff) \
1051                         { \
1052                                 row[0] = y >> 8; \
1053                                 row[1] = u >> 8; \
1054                                 row[2] = v >> 8; \
1055                         } \
1056                         else \
1057                         { \
1058                                 row[0] = y; \
1059                                 row[1] = u; \
1060                                 row[2] = v; \
1061                         } \
1062                         row += components; \
1063                 } \
1064         } \
1065 }
1066
1067 #define PROCESS_FLOAT(components) \
1068 { \
1069         for(int i = pkg->start; i < pkg->end; i++) \
1070         { \
1071                 float *row = (float*)input->get_rows()[i]; \
1072                 for(int j = 0; j < w; j++) \
1073                 { \
1074                         if ( plugin->config.split && ((j + i * w / h) < w) ) \
1075                         continue; \
1076                         float r = row[0]; \
1077                         float g = row[1]; \
1078                         float b = row[2]; \
1079  \
1080                         r = plugin->calculate_level(r, HISTOGRAM_RED, 1); \
1081                         g = plugin->calculate_level(g, HISTOGRAM_GREEN, 1); \
1082                         b = plugin->calculate_level(b, HISTOGRAM_BLUE, 1); \
1083  \
1084                         row[0] = r; \
1085                         row[1] = g; \
1086                         row[2] = b; \
1087  \
1088                         row += components; \
1089                 } \
1090         } \
1091 }
1092
1093
1094                 VFrame *input = plugin->input;
1095                 //VFrame *output = plugin->output;
1096                 int w = input->get_w();
1097                 int h = input->get_h();
1098                 int *lookup_r = plugin->lookup[0];
1099                 int *lookup_g = plugin->lookup[1];
1100                 int *lookup_b = plugin->lookup[2];
1101                 int r, g, b, y, u, v;
1102                 switch(input->get_color_model())
1103                 {
1104                         case BC_RGB888:
1105                                 PROCESS(unsigned char, 3)
1106                                 break;
1107                         case BC_RGB_FLOAT:
1108                                 PROCESS_FLOAT(3);
1109                                 break;
1110                         case BC_RGBA8888:
1111                                 PROCESS(unsigned char, 4)
1112                                 break;
1113                         case BC_RGBA_FLOAT:
1114                                 PROCESS_FLOAT(4);
1115                                 break;
1116                         case BC_RGB161616:
1117                                 PROCESS(uint16_t, 3)
1118                                 break;
1119                         case BC_RGBA16161616:
1120                                 PROCESS(uint16_t, 4)
1121                                 break;
1122                         case BC_YUV888:
1123                                 PROCESS_YUV(unsigned char, 3, 0xff)
1124                                 break;
1125                         case BC_YUVA8888:
1126                                 PROCESS_YUV(unsigned char, 4, 0xff)
1127                                 break;
1128                         case BC_YUV161616:
1129                                 PROCESS_YUV(uint16_t, 3, 0xffff)
1130                                 break;
1131                         case BC_YUVA16161616:
1132                                 PROCESS_YUV(uint16_t, 4, 0xffff)
1133                                 break;
1134                 }
1135         }
1136 }
1137
1138
1139
1140
1141
1142
1143 HistogramEngine::HistogramEngine(HistogramMain *plugin,
1144         int total_clients,
1145         int total_packages)
1146  : LoadServer(total_clients, total_packages)
1147 {
1148         this->plugin = plugin;
1149 }
1150
1151 void HistogramEngine::init_packages()
1152 {
1153         switch(operation)
1154         {
1155                 case HISTOGRAM:
1156                         total_size = data->get_h();
1157                         break;
1158                 case APPLY:
1159                         total_size = data->get_h();
1160                         break;
1161         }
1162
1163
1164         //int package_size = (int)((float)total_size / get_total_packages() + 1);
1165         //int start = 0;
1166
1167         for(int i = 0; i < get_total_packages(); i++)
1168         {
1169                 HistogramPackage *package = (HistogramPackage*)get_package(i);
1170                 package->start = total_size * i / get_total_packages();
1171                 package->end = total_size * (i + 1) / get_total_packages();
1172         }
1173
1174 // Initialize clients here in case some don't get run.
1175         for(int i = 0; i < get_total_clients(); i++)
1176         {
1177                 HistogramUnit *unit = (HistogramUnit*)get_client(i);
1178                 for(int i = 0; i < HISTOGRAM_MODES; i++)
1179                         bzero(unit->accum[i], sizeof(int) * HISTOGRAM_SLOTS);
1180         }
1181
1182 }
1183
1184 LoadClient* HistogramEngine::new_client()
1185 {
1186         return new HistogramUnit(this, plugin);
1187 }
1188
1189 LoadPackage* HistogramEngine::new_package()
1190 {
1191         return new HistogramPackage;
1192 }
1193
1194 void HistogramEngine::process_packages(int operation, VFrame *data, int do_value)
1195 {
1196         this->data = data;
1197         this->operation = operation;
1198         this->do_value = do_value;
1199         LoadServer::process_packages();
1200 }
1201
1202