lv2_blklst additions, ffmpeg est/bad times, proxy preview, cin.svg icon
[goodguy/cinelerra.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 N_("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->xbuf);
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->xbuf);
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         if( !engine )
325         {
326                 int cpus = data->get_w() * data->get_h() / 0x80000 + 2;
327                 int smps = get_project_smp();
328                 if( cpus > smps ) cpus = smps;
329                 engine = new HistogramEngine(this, cpus, cpus);
330         }
331         if(!accum[0])
332         {
333                 for(int i = 0; i < HISTOGRAM_MODES; i++)
334                         accum[i] = new int[HISTOGRAM_SLOTS];
335         }
336
337         engine->process_packages(HistogramEngine::HISTOGRAM, data, do_value);
338
339         for(int i = 0; i < engine->get_total_clients(); i++)
340         {
341                 HistogramUnit *unit = (HistogramUnit*)engine->get_client(i);
342
343                 if(i == 0)
344                 {
345                         for(int j = 0; j < HISTOGRAM_MODES; j++)
346                         {
347                                 memcpy(accum[j], unit->accum[j], sizeof(int) * HISTOGRAM_SLOTS);
348                         }
349                 }
350                 else
351                 {
352                         for(int j = 0; j < HISTOGRAM_MODES; j++)
353                         {
354                                 int *out = accum[j];
355                                 int *in = unit->accum[j];
356                                 for(int k = 0; k < HISTOGRAM_SLOTS; k++)
357                                         out[k] += in[k];
358                         }
359                 }
360         }
361
362 // Remove top and bottom from calculations.  Doesn't work in high
363 // precision colormodels.
364         for(int i = 0; i < HISTOGRAM_MODES; i++)
365         {
366                 accum[i][0] = 0;
367                 accum[i][HISTOGRAM_SLOTS - 1] = 0;
368         }
369 }
370
371
372 void HistogramMain::calculate_automatic(VFrame *data)
373 {
374         calculate_histogram(data, 0);
375         config.reset_points(1);
376
377 // Do each channel
378         for(int i = 0; i < 3; i++)
379         {
380                 int *accum = this->accum[i];
381                 int pixels = data->get_w() * data->get_h();
382                 float white_fraction = 1.0 - (1.0 - config.threshold) / 2;
383                 int threshold = (int)(white_fraction * pixels);
384                 int total = 0;
385                 float max_level = 1.0;
386                 float min_level = 0.0;
387
388 // Get histogram slot above threshold of pixels
389                 for(int j = 0; j < HISTOGRAM_SLOTS; j++)
390                 {
391                         total += accum[j];
392                         if(total >= threshold)
393                         {
394                                 max_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
395                                 break;
396                         }
397                 }
398
399 // Get slot below 99% of pixels
400                 total = 0;
401                 for(int j = HISTOGRAM_SLOTS - 1; j >= 0; j--)
402                 {
403                         total += accum[j];
404                         if(total >= threshold)
405                         {
406                                 min_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
407                                 break;
408                         }
409                 }
410
411
412                 config.low_input[i] = min_level;
413                 config.high_input[i] = max_level;
414         }
415 }
416
417
418
419
420
421 int HistogramMain::calculate_use_opengl()
422 {
423 // glHistogram doesn't work.
424         int result = get_use_opengl() &&
425                 !config.automatic &&
426                 (!config.plot || !gui_open());
427         return result;
428 }
429
430
431 int HistogramMain::process_buffer(VFrame *frame,
432         int64_t start_position,
433         double frame_rate)
434 {
435         int need_reconfigure = load_configuration();
436
437
438
439         int use_opengl = calculate_use_opengl();
440
441 //printf("%d\n", use_opengl);
442         read_frame(frame,
443                 0,
444                 start_position,
445                 frame_rate,
446                 use_opengl);
447
448 // Apply histogram in hardware
449         if(use_opengl) return run_opengl();
450
451         this->input = frame;
452         this->output = frame;
453         if( !engine )
454         {
455                 int cpus = input->get_w() * input->get_h() / 0x80000 + 2;
456                 int smps = get_project_smp();
457                 if( cpus > smps ) cpus = smps;
458                 engine = new HistogramEngine(this, cpus, cpus);
459         }
460 // Always plot to set the curves if automatic
461         if(config.plot || config.automatic) send_render_gui(frame);
462
463 // Generate tables here.  The same table is used by many packages to render
464 // each horizontal stripe.  Need to cover the entire output range in  each
465 // table to avoid green borders
466
467
468         if(need_reconfigure ||
469                 !lookup[0] ||
470                 config.automatic)
471         {
472 // Calculate new curves
473                 if(config.automatic)
474                 {
475                         calculate_automatic(input);
476                 }
477
478
479 // Generate transfer tables with value function for integer colormodels.
480                 for(int i = 0; i < 3; i++)
481                         tabulate_curve(i, 1);
482         }
483
484 // printf("HistogramMain::process_buffer %d %f %f %f  %f %f %f  %f %f %f\n",
485 // __LINE__,
486 // config.low_input[HISTOGRAM_RED],
487 // config.gamma[HISTOGRAM_RED],
488 // config.high_input[HISTOGRAM_RED],
489 // config.low_input[HISTOGRAM_GREEN],
490 // config.gamma[HISTOGRAM_GREEN],
491 // config.high_input[HISTOGRAM_GREEN],
492 // config.low_input[HISTOGRAM_BLUE],
493 // config.gamma[HISTOGRAM_BLUE],
494 // config.high_input[HISTOGRAM_BLUE]);
495
496
497
498 // Apply histogram
499         engine->process_packages(HistogramEngine::APPLY, input, 0);
500
501
502         return 0;
503 }
504
505 void HistogramMain::tabulate_curve(int subscript, int use_value)
506 {
507         int i;
508         if(!lookup[subscript])
509                 lookup[subscript] = new int[HISTOGRAM_SLOTS];
510         if(!preview_lookup[subscript])
511                 preview_lookup[subscript] = new int[HISTOGRAM_SLOTS];
512
513 //printf("HistogramMain::tabulate_curve %d input=%p\n", __LINE__, input);
514
515
516 // Generate lookup tables for integer colormodels
517         if(input)
518         {
519                 switch(input->get_color_model())
520                 {
521                         case BC_RGB888:
522                         case BC_RGBA8888:
523                                 for(i = 0; i < 0x100; i++)
524                                 {
525                                         lookup[subscript][i] =
526                                                 (int)(calculate_level((float)i / 0xff, subscript, use_value) *
527                                                 0xff);
528                                         CLAMP(lookup[subscript][i], 0, 0xff);
529                                 }
530                                 break;
531 // All other integer colormodels are converted to 16 bit RGB
532                         default:
533                                 for(i = 0; i < 0x10000; i++)
534                                 {
535                                         lookup[subscript][i] =
536                                                 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
537                                                 0xffff);
538                                         CLAMP(lookup[subscript][i], 0, 0xffff);
539                                 }
540 // for(i = 0; i < 0x100; i++)
541 // {
542 // if(subscript == HISTOGRAM_BLUE) printf("%d ", lookup[subscript][i * 0x100]);
543 // }
544 // if(subscript == HISTOGRAM_BLUE) printf("\n");
545
546                                 break;
547                 }
548         }
549
550 // Lookup table for preview only used for GUI
551         if(!use_value)
552         {
553                 for(i = 0; i < 0x10000; i++)
554                 {
555                         preview_lookup[subscript][i] =
556                                 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
557                                 0xffff);
558                         CLAMP(preview_lookup[subscript][i], 0, 0xffff);
559                 }
560         }
561 }
562
563 int HistogramMain::handle_opengl()
564 {
565 #ifdef HAVE_GL
566 // Functions to get pixel from either previous effect or texture
567         static const char *histogram_get_pixel1 =
568                 "vec4 histogram_get_pixel()\n"
569                 "{\n"
570                 "       return gl_FragColor;\n"
571                 "}\n";
572
573         static const char *histogram_get_pixel2 =
574                 "uniform sampler2D tex;\n"
575                 "vec4 histogram_get_pixel()\n"
576                 "{\n"
577                 "       return texture2D(tex, gl_TexCoord[0].st);\n"
578                 "}\n";
579
580         static const char *head_frag =
581                 "uniform vec4 low_input;\n"
582                 "uniform vec4 high_input;\n"
583                 "uniform vec4 gamma;\n"
584                 "uniform vec4 low_output;\n"
585                 "uniform vec4 output_scale;\n"
586                 "void main()\n"
587                 "{\n"
588                 "       float temp = 0.0;\n";
589
590         static const char *get_rgb_frag =
591                 "       vec4 pixel = histogram_get_pixel();\n";
592
593         static const char *get_yuv_frag =
594                 "       vec4 pixel = histogram_get_pixel();\n"
595                         YUV_TO_RGB_FRAG("pixel");
596
597 #define APPLY_INPUT_CURVE(PIXEL, LOW_INPUT, HIGH_INPUT, GAMMA) \
598                 "// apply input curve\n" \
599                 "       temp = (" PIXEL " - " LOW_INPUT ") / \n" \
600                 "               (" HIGH_INPUT " - " LOW_INPUT ");\n" \
601                 "       temp = max(temp, 0.0);\n" \
602                 "       " PIXEL " = pow(temp, 1.0 / " GAMMA ");\n"
603
604
605
606         static const char *apply_histogram_frag =
607                 APPLY_INPUT_CURVE("pixel.r", "low_input.r", "high_input.r", "gamma.r")
608                 APPLY_INPUT_CURVE("pixel.g", "low_input.g", "high_input.g", "gamma.g")
609                 APPLY_INPUT_CURVE("pixel.b", "low_input.b", "high_input.b", "gamma.b")
610                 "// apply output curve\n"
611                 "       pixel.rgb *= output_scale.rgb;\n"
612                 "       pixel.rgb += low_output.rgb;\n"
613                 APPLY_INPUT_CURVE("pixel.r", "low_input.a", "high_input.a", "gamma.a")
614                 APPLY_INPUT_CURVE("pixel.g", "low_input.a", "high_input.a", "gamma.a")
615                 APPLY_INPUT_CURVE("pixel.b", "low_input.a", "high_input.a", "gamma.a")
616                 "// apply output curve\n"
617                 "       pixel.rgb *= vec3(output_scale.a, output_scale.a, output_scale.a);\n"
618                 "       pixel.rgb += vec3(low_output.a, low_output.a, low_output.a);\n";
619
620         static const char *put_rgb_frag =
621                 "       gl_FragColor = pixel;\n"
622                 "}\n";
623
624         static const char *put_yuv_frag =
625                         RGB_TO_YUV_FRAG("pixel")
626                 "       gl_FragColor = pixel;\n"
627                 "}\n";
628
629
630
631         get_output()->to_texture();
632         get_output()->enable_opengl();
633
634         const char *shader_stack[16];
635         memset(shader_stack,0, sizeof(shader_stack));
636         int current_shader = 0;
637
638         int need_color_matrix = BC_CModels::is_yuv(get_output()->get_color_model()) ? 1 : 0;
639         if( need_color_matrix )
640                 shader_stack[current_shader++] = bc_gl_colors;
641
642         int aggregate_interpolation = 0;
643         int aggregate_gamma = 0;
644         int aggregate_colorbalance = 0;
645 // All aggregation possibilities must be accounted for because unsupported
646 // effects can get in between the aggregation members.
647         if(!strcmp(get_output()->get_prev_effect(2), _("Interpolate Pixels")) &&
648                 !strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
649                 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
650         {
651                 aggregate_interpolation = 1;
652                 aggregate_gamma = 1;
653                 aggregate_colorbalance = 1;
654         }
655         else
656         if(!strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
657                 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
658         {
659                 aggregate_gamma = 1;
660                 aggregate_colorbalance = 1;
661         }
662         else
663         if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
664                 !strcmp(get_output()->get_prev_effect(0), _("Gamma")))
665         {
666                 aggregate_interpolation = 1;
667                 aggregate_gamma = 1;
668         }
669         else
670         if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
671                 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
672         {
673                 aggregate_interpolation = 1;
674                 aggregate_colorbalance = 1;
675         }
676         else
677         if(!strcmp(get_output()->get_prev_effect(0), _("Interpolate Pixels")))
678                 aggregate_interpolation = 1;
679         else
680         if(!strcmp(get_output()->get_prev_effect(0), _("Gamma")))
681                 aggregate_gamma = 1;
682         else
683         if(!strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
684                 aggregate_colorbalance = 1;
685
686 // The order of processing is fixed by this sequence
687         if(aggregate_interpolation)
688                 INTERPOLATE_COMPILE(shader_stack, current_shader);
689
690         if(aggregate_gamma)
691                 GAMMA_COMPILE(shader_stack, current_shader,
692                         aggregate_interpolation);
693
694         if(aggregate_colorbalance)
695                 COLORBALANCE_COMPILE(shader_stack, current_shader,
696                         aggregate_interpolation || aggregate_gamma);
697
698         shader_stack[current_shader++] = 
699                 aggregate_interpolation || aggregate_gamma || aggregate_colorbalance ?
700                         histogram_get_pixel1 : histogram_get_pixel2;
701
702         shader_stack[current_shader++] = head_frag;
703         shader_stack[current_shader++] = BC_CModels::is_yuv(get_output()->get_color_model()) ?
704                         get_yuv_frag : get_rgb_frag;
705         shader_stack[current_shader++] = apply_histogram_frag;
706         shader_stack[current_shader++] = BC_CModels::is_yuv(get_output()->get_color_model()) ?
707                         put_yuv_frag : put_rgb_frag;
708
709         shader_stack[current_shader] = 0;
710         unsigned int shader = VFrame::make_shader(shader_stack);
711
712 // printf("HistogramMain::handle_opengl %d %d %d %d shader=%d\n",
713 // aggregate_interpolation,
714 // aggregate_gamma,
715 // aggregate_colorbalance,
716 // current_shader,
717 // shader);
718
719         float low_input[4];
720         float high_input[4];
721         float gamma[4];
722         float low_output[4];
723         float output_scale[4];
724
725
726 // printf("min x    min y    max x    max y\n");
727 // printf("%f %f %f %f\n", input_min_r[0], input_min_r[1], input_max_r[0], input_max_r[1]);
728 // printf("%f %f %f %f\n", input_min_g[0], input_min_g[1], input_max_g[0], input_max_g[1]);
729 // printf("%f %f %f %f\n", input_min_b[0], input_min_b[1], input_max_b[0], input_max_b[1]);
730 // printf("%f %f %f %f\n", input_min_v[0], input_min_v[1], input_max_v[0], input_max_v[1]);
731
732         for(int i = 0; i < HISTOGRAM_MODES; i++)
733         {
734                 low_input[i] = config.low_input[i];
735                 high_input[i] = config.high_input[i];
736                 gamma[i] = config.gamma[i];
737                 low_output[i] = config.low_output[i];
738                 output_scale[i] = config.high_output[i] - config.low_output[i];
739         }
740
741         if(shader > 0)
742         {
743                 glUseProgram(shader);
744                 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
745                 if(aggregate_gamma) GAMMA_UNIFORMS(shader);
746                 if(aggregate_interpolation) INTERPOLATE_UNIFORMS(shader);
747                 if(aggregate_colorbalance) COLORBALANCE_UNIFORMS(shader);
748                 glUniform4fv(glGetUniformLocation(shader, "low_input"), 1, low_input);
749                 glUniform4fv(glGetUniformLocation(shader, "high_input"), 1, high_input);
750                 glUniform4fv(glGetUniformLocation(shader, "gamma"), 1, gamma);
751                 glUniform4fv(glGetUniformLocation(shader, "low_output"), 1, low_output);
752                 glUniform4fv(glGetUniformLocation(shader, "output_scale"), 1, output_scale);
753                 if( need_color_matrix ) BC_GL_COLORS(shader);
754         }
755
756         get_output()->init_screen();
757         get_output()->bind_texture(0);
758
759         glDisable(GL_BLEND);
760
761 // Draw the affected half
762         if(config.split)
763         {
764                 glBegin(GL_TRIANGLES);
765                 glNormal3f(0, 0, 1.0);
766
767                 glTexCoord2f(0.0 / get_output()->get_texture_w(),
768                         0.0 / get_output()->get_texture_h());
769                 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
770
771
772                 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
773                         (float)get_output()->get_h() / get_output()->get_texture_h());
774                 glVertex3f((float)get_output()->get_w(), -0.0, 0);
775
776                 glTexCoord2f(0.0 / get_output()->get_texture_w(),
777                         (float)get_output()->get_h() / get_output()->get_texture_h());
778                 glVertex3f(0.0, -0.0, 0);
779
780
781                 glEnd();
782         }
783         else
784         {
785                 get_output()->draw_texture();
786         }
787
788         glUseProgram(0);
789
790 // Draw the unaffected half
791         if(config.split)
792         {
793                 glBegin(GL_TRIANGLES);
794                 glNormal3f(0, 0, 1.0);
795
796
797                 glTexCoord2f(0.0 / get_output()->get_texture_w(),
798                         0.0 / get_output()->get_texture_h());
799                 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
800
801                 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
802                         0.0 / get_output()->get_texture_h());
803                 glVertex3f((float)get_output()->get_w(),
804                         -(float)get_output()->get_h(), 0);
805
806                 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
807                         (float)get_output()->get_h() / get_output()->get_texture_h());
808                 glVertex3f((float)get_output()->get_w(), -0.0, 0);
809
810
811                 glEnd();
812         }
813
814         get_output()->set_opengl_state(VFrame::SCREEN);
815 #endif
816         return 0;
817 }
818
819
820
821
822
823
824
825
826
827
828
829
830 HistogramPackage::HistogramPackage()
831  : LoadPackage()
832 {
833 }
834
835
836
837
838 HistogramUnit::HistogramUnit(HistogramEngine *server,
839         HistogramMain *plugin)
840  : LoadClient(server)
841 {
842         this->plugin = plugin;
843         this->server = server;
844         for(int i = 0; i < HISTOGRAM_MODES; i++)
845                 accum[i] = new int[HISTOGRAM_SLOTS];
846 }
847
848 HistogramUnit::~HistogramUnit()
849 {
850         for(int i = 0; i < HISTOGRAM_MODES; i++)
851                 delete [] accum[i];
852 }
853
854 void HistogramUnit::process_package(LoadPackage *package)
855 {
856         HistogramPackage *pkg = (HistogramPackage*)package;
857
858         if(server->operation == HistogramEngine::HISTOGRAM)
859         {
860                 int do_value = server->do_value;
861
862
863 #define HISTOGRAM_HEAD(type) \
864 { \
865         for(int i = pkg->start; i < pkg->end; i++) \
866         { \
867                 type *row = (type*)data->get_rows()[i]; \
868                 for(int j = 0; j < w; j++) \
869                 {
870
871 #define HISTOGRAM_TAIL(components) \
872 /* Value takes the maximum of the output RGB values */ \
873                         if(do_value) \
874                         { \
875                                 CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
876                                 CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
877                                 CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
878                                 r_out = lookup_r[r]; \
879                                 g_out = lookup_g[g]; \
880                                 b_out = lookup_b[b]; \
881 /*                              v = (r * 76 + g * 150 + b * 29) >> 8; */ \
882                                 v = MAX(r_out, g_out); \
883                                 v = MAX(v, b_out); \
884                                 v += -HISTOGRAM_MIN * 0xffff / 100; \
885                                 CLAMP(v, 0, HISTOGRAM_SLOTS - 1); \
886                                 accum_v[v]++; \
887                         } \
888  \
889                         r += -HISTOGRAM_MIN * 0xffff / 100; \
890                         g += -HISTOGRAM_MIN * 0xffff / 100; \
891                         b += -HISTOGRAM_MIN * 0xffff / 100; \
892                         CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
893                         CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
894                         CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
895                         accum_r[r]++; \
896                         accum_g[g]++; \
897                         accum_b[b]++; \
898                         row += components; \
899                 } \
900         } \
901 }
902
903
904
905
906                 VFrame *data = server->data;
907                 int w = data->get_w();
908                 //int h = data->get_h();
909                 int *accum_r = accum[HISTOGRAM_RED];
910                 int *accum_g = accum[HISTOGRAM_GREEN];
911                 int *accum_b = accum[HISTOGRAM_BLUE];
912                 int *accum_v = accum[HISTOGRAM_VALUE];
913                 int32_t r, g, b, y, u, v;
914                 int r_out, g_out, b_out;
915                 int *lookup_r = plugin->preview_lookup[HISTOGRAM_RED];
916                 int *lookup_g = plugin->preview_lookup[HISTOGRAM_GREEN];
917                 int *lookup_b = plugin->preview_lookup[HISTOGRAM_BLUE];
918
919                 switch(data->get_color_model())
920                 {
921                         case BC_RGB888:
922                                 HISTOGRAM_HEAD(unsigned char)
923                                 r = (row[0] << 8) | row[0];
924                                 g = (row[1] << 8) | row[1];
925                                 b = (row[2] << 8) | row[2];
926                                 HISTOGRAM_TAIL(3)
927                                 break;
928                         case BC_RGB_FLOAT:
929                                 HISTOGRAM_HEAD(float)
930                                 r = (int)(row[0] * 0xffff);
931                                 g = (int)(row[1] * 0xffff);
932                                 b = (int)(row[2] * 0xffff);
933                                 HISTOGRAM_TAIL(3)
934                                 break;
935                         case BC_YUV888:
936                                 HISTOGRAM_HEAD(unsigned char)
937                                 y = (row[0] << 8) | row[0];
938                                 u = (row[1] << 8) | row[1];
939                                 v = (row[2] << 8) | row[2];
940                                 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
941                                 HISTOGRAM_TAIL(3)
942                                 break;
943                         case BC_RGBA8888:
944                                 HISTOGRAM_HEAD(unsigned char)
945                                 r = (row[0] << 8) | row[0];
946                                 g = (row[1] << 8) | row[1];
947                                 b = (row[2] << 8) | row[2];
948                                 HISTOGRAM_TAIL(4)
949                                 break;
950                         case BC_RGBA_FLOAT:
951                                 HISTOGRAM_HEAD(float)
952                                 r = (int)(row[0] * 0xffff);
953                                 g = (int)(row[1] * 0xffff);
954                                 b = (int)(row[2] * 0xffff);
955                                 HISTOGRAM_TAIL(4)
956                                 break;
957                         case BC_YUVA8888:
958                                 HISTOGRAM_HEAD(unsigned char)
959                                 y = (row[0] << 8) | row[0];
960                                 u = (row[1] << 8) | row[1];
961                                 v = (row[2] << 8) | row[2];
962                                 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
963                                 HISTOGRAM_TAIL(4)
964                                 break;
965                         case BC_RGB161616:
966                                 HISTOGRAM_HEAD(uint16_t)
967                                 r = row[0];
968                                 g = row[1];
969                                 b = row[2];
970                                 HISTOGRAM_TAIL(3)
971                                 break;
972                         case BC_YUV161616:
973                                 HISTOGRAM_HEAD(uint16_t)
974                                 y = row[0];
975                                 u = row[1];
976                                 v = row[2];
977                                 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
978                                 HISTOGRAM_TAIL(3)
979                                 break;
980                         case BC_RGBA16161616:
981                                 HISTOGRAM_HEAD(uint16_t)
982                                 r = row[0];
983                                 g = row[1];
984                                 b = row[2];
985                                 HISTOGRAM_TAIL(3)
986                                 break;
987                         case BC_YUVA16161616:
988                                 HISTOGRAM_HEAD(uint16_t)
989                                 y = row[0];
990                                 u = row[1];
991                                 v = row[2];
992                                 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
993                                 HISTOGRAM_TAIL(4)
994                                 break;
995                 }
996         }
997         else
998         if(server->operation == HistogramEngine::APPLY)
999         {
1000
1001
1002
1003 #define PROCESS(type, components) \
1004 { \
1005         for(int i = pkg->start; i < pkg->end; i++) \
1006         { \
1007                 type *row = (type*)input->get_rows()[i]; \
1008                 for(int j = 0; j < w; j++) \
1009                 { \
1010                         if ( plugin->config.split && ((j + i * w / h) < w) ) \
1011                         continue; \
1012                         row[0] = lookup_r[row[0]]; \
1013                         row[1] = lookup_g[row[1]]; \
1014                         row[2] = lookup_b[row[2]]; \
1015                         row += components; \
1016                 } \
1017         } \
1018 }
1019
1020 #define PROCESS_YUV(type, components, max) \
1021 { \
1022         for(int i = pkg->start; i < pkg->end; i++) \
1023         { \
1024                 type *row = (type*)input->get_rows()[i]; \
1025                 for(int j = 0; j < w; j++) \
1026                 { \
1027                         if ( plugin->config.split && ((j + i * w / h) < w) ) \
1028                         continue; \
1029 /* Convert to 16 bit RGB */ \
1030                         if(max == 0xff) \
1031                         { \
1032                                 y = (row[0] << 8) | row[0]; \
1033                                 u = (row[1] << 8) | row[1]; \
1034                                 v = (row[2] << 8) | row[2]; \
1035                         } \
1036                         else \
1037                         { \
1038                                 y = row[0]; \
1039                                 u = row[1]; \
1040                                 v = row[2]; \
1041                         } \
1042  \
1043                         YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
1044  \
1045 /* Look up in RGB domain */ \
1046                         r = lookup_r[r]; \
1047                         g = lookup_g[g]; \
1048                         b = lookup_b[b]; \
1049  \
1050 /* Convert to 16 bit YUV */ \
1051                         YUV::yuv.rgb_to_yuv_16(r, g, b, y, u, v); \
1052  \
1053                         if(max == 0xff) \
1054                         { \
1055                                 row[0] = y >> 8; \
1056                                 row[1] = u >> 8; \
1057                                 row[2] = v >> 8; \
1058                         } \
1059                         else \
1060                         { \
1061                                 row[0] = y; \
1062                                 row[1] = u; \
1063                                 row[2] = v; \
1064                         } \
1065                         row += components; \
1066                 } \
1067         } \
1068 }
1069
1070 #define PROCESS_FLOAT(components) \
1071 { \
1072         for(int i = pkg->start; i < pkg->end; i++) \
1073         { \
1074                 float *row = (float*)input->get_rows()[i]; \
1075                 for(int j = 0; j < w; j++) \
1076                 { \
1077                         if ( plugin->config.split && ((j + i * w / h) < w) ) \
1078                         continue; \
1079                         float r = row[0]; \
1080                         float g = row[1]; \
1081                         float b = row[2]; \
1082  \
1083                         r = plugin->calculate_level(r, HISTOGRAM_RED, 1); \
1084                         g = plugin->calculate_level(g, HISTOGRAM_GREEN, 1); \
1085                         b = plugin->calculate_level(b, HISTOGRAM_BLUE, 1); \
1086  \
1087                         row[0] = r; \
1088                         row[1] = g; \
1089                         row[2] = b; \
1090  \
1091                         row += components; \
1092                 } \
1093         } \
1094 }
1095
1096
1097                 VFrame *input = plugin->input;
1098                 //VFrame *output = plugin->output;
1099                 int w = input->get_w();
1100                 int h = input->get_h();
1101                 int *lookup_r = plugin->lookup[0];
1102                 int *lookup_g = plugin->lookup[1];
1103                 int *lookup_b = plugin->lookup[2];
1104                 int r, g, b, y, u, v;
1105                 switch(input->get_color_model())
1106                 {
1107                         case BC_RGB888:
1108                                 PROCESS(unsigned char, 3)
1109                                 break;
1110                         case BC_RGB_FLOAT:
1111                                 PROCESS_FLOAT(3);
1112                                 break;
1113                         case BC_RGBA8888:
1114                                 PROCESS(unsigned char, 4)
1115                                 break;
1116                         case BC_RGBA_FLOAT:
1117                                 PROCESS_FLOAT(4);
1118                                 break;
1119                         case BC_RGB161616:
1120                                 PROCESS(uint16_t, 3)
1121                                 break;
1122                         case BC_RGBA16161616:
1123                                 PROCESS(uint16_t, 4)
1124                                 break;
1125                         case BC_YUV888:
1126                                 PROCESS_YUV(unsigned char, 3, 0xff)
1127                                 break;
1128                         case BC_YUVA8888:
1129                                 PROCESS_YUV(unsigned char, 4, 0xff)
1130                                 break;
1131                         case BC_YUV161616:
1132                                 PROCESS_YUV(uint16_t, 3, 0xffff)
1133                                 break;
1134                         case BC_YUVA16161616:
1135                                 PROCESS_YUV(uint16_t, 4, 0xffff)
1136                                 break;
1137                 }
1138         }
1139 }
1140
1141
1142
1143
1144
1145
1146 HistogramEngine::HistogramEngine(HistogramMain *plugin,
1147         int total_clients,
1148         int total_packages)
1149  : LoadServer(total_clients, total_packages)
1150 {
1151         this->plugin = plugin;
1152 }
1153
1154 void HistogramEngine::init_packages()
1155 {
1156         switch(operation)
1157         {
1158                 case HISTOGRAM:
1159                         total_size = data->get_h();
1160                         break;
1161                 case APPLY:
1162                         total_size = data->get_h();
1163                         break;
1164         }
1165
1166
1167         //int package_size = (int)((float)total_size / get_total_packages() + 1);
1168         //int start = 0;
1169
1170         for(int i = 0; i < get_total_packages(); i++)
1171         {
1172                 HistogramPackage *package = (HistogramPackage*)get_package(i);
1173                 package->start = total_size * i / get_total_packages();
1174                 package->end = total_size * (i + 1) / get_total_packages();
1175         }
1176
1177 // Initialize clients here in case some don't get run.
1178         for(int i = 0; i < get_total_clients(); i++)
1179         {
1180                 HistogramUnit *unit = (HistogramUnit*)get_client(i);
1181                 for(int i = 0; i < HISTOGRAM_MODES; i++)
1182                         bzero(unit->accum[i], sizeof(int) * HISTOGRAM_SLOTS);
1183         }
1184
1185 }
1186
1187 LoadClient* HistogramEngine::new_client()
1188 {
1189         return new HistogramUnit(this, plugin);
1190 }
1191
1192 LoadPackage* HistogramEngine::new_package()
1193 {
1194         return new HistogramPackage;
1195 }
1196
1197 void HistogramEngine::process_packages(int operation, VFrame *data, int do_value)
1198 {
1199         this->data = data;
1200         this->operation = operation;
1201         this->do_value = do_value;
1202         LoadServer::process_packages();
1203 }
1204
1205