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