4 * Copyright (C) 2008-2012 Adam Williams <broadcast at earthling dot net>
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.
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.
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
27 #include "bcdisplayinfo.h"
28 #include "bcsignals.h"
32 #include "histogram.h"
33 #include "histogramconfig.h"
34 #include "histogramwindow.h"
37 #include "loadbalance.h"
38 #include "playback3d.h"
41 #include "workarounds.h"
43 #include "aggregated.h"
44 #include "../colorbalance/aggregated.h"
45 #include "../interpolate/aggregated.h"
46 #include "../gamma/aggregated.h"
49 class HistogramEngine;
50 class HistogramWindow;
56 REGISTER_PLUGIN(HistogramMain)
66 HistogramMain::HistogramMain(PluginServer *server)
67 : PluginVClient(server)
71 for(int i = 0; i < HISTOGRAM_MODES; i++)
75 preview_lookup[i] = 0;
78 mode = HISTOGRAM_VALUE;
87 HistogramMain::~HistogramMain()
90 for(int i = 0; i < HISTOGRAM_MODES;i++)
94 delete [] preview_lookup[i];
99 const char* HistogramMain::plugin_title() { return N_("Histogram"); }
100 int HistogramMain::is_realtime() { return 1; }
104 NEW_WINDOW_MACRO(HistogramMain, HistogramWindow)
106 LOAD_CONFIGURATION_MACRO(HistogramMain, HistogramConfig)
108 void HistogramMain::render_gui(void *data)
112 // Process just the RGB values to determine the automatic points or
113 // all the points if manual
114 if(!config.automatic)
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();
125 calculate_histogram((VFrame*)data, !config.automatic);
130 calculate_automatic((VFrame*)data);
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();
141 // Need a second pass to get the luminance values.
142 calculate_histogram((VFrame*)data, 1);
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,
152 ((HistogramWindow*)thread->window)->unlock_window();
156 void HistogramMain::update_gui()
160 ((HistogramWindow*)thread->window)->lock_window("HistogramMain::update_gui");
161 int reconfigure = load_configuration();
164 ((HistogramWindow*)thread->window)->update(1,
169 ((HistogramWindow*)thread->window)->unlock_window();
176 void HistogramMain::save_data(KeyFrame *keyframe)
180 // cause data to be stored directly in text
181 output.set_shared_output(keyframe->xbuf);
182 output.tag.set_title("HISTOGRAM");
184 char string[BCTEXTLEN];
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);
195 for(int i = 0; i < HISTOGRAM_MODES; i++)
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]);
211 output.tag.set_title("/HISTOGRAM");
213 output.append_newline();
214 output.terminate_string();
217 void HistogramMain::read_data(KeyFrame *keyframe)
221 input.set_shared_input(keyframe->xbuf);
228 result = input.read_tag();
232 if(input.tag.title_is("HISTOGRAM"))
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);
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);
247 char string[BCTEXTLEN];
248 for(int i = 0; i < HISTOGRAM_MODES; i++)
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]);
257 if(i == HISTOGRAM_VALUE || !config.automatic)
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]);
264 //printf("HistogramMain::read_data %d %f %d\n", config.input_min[i], config.input_mid[i], config.input_max[i]);
274 float HistogramMain::calculate_level(float input,
280 // Scale to input range
281 if(!EQUIV(config.high_input[mode], config.low_input[mode]))
283 output = input < config.low_input[mode] ? 0 :
284 (input - config.low_input[mode]) /
285 (config.high_input[mode] - config.low_input[mode]);
294 if(!EQUIV(config.gamma[mode], 0))
296 output = pow(output, 1.0 / config.gamma[mode]);
297 CLAMP(output, 0, 1.0);
301 if(use_value && mode != HISTOGRAM_VALUE)
303 output = calculate_level(output, HISTOGRAM_VALUE, 0);
309 // scale to output range
310 if(!EQUIV(config.low_output[mode], config.high_output[mode]))
312 output = output * (config.high_output[mode] - config.low_output[mode]) +
313 config.low_output[mode];
316 CLAMP(output, 0, 1.0);
323 void HistogramMain::calculate_histogram(VFrame *data, int do_value)
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);
334 for(int i = 0; i < HISTOGRAM_MODES; i++)
335 accum[i] = new int[HISTOGRAM_SLOTS];
338 engine->process_packages(HistogramEngine::HISTOGRAM, data, do_value);
340 for(int i = 0; i < engine->get_total_clients(); i++)
342 HistogramUnit *unit = (HistogramUnit*)engine->get_client(i);
346 for(int j = 0; j < HISTOGRAM_MODES; j++)
348 memcpy(accum[j], unit->accum[j], sizeof(int) * HISTOGRAM_SLOTS);
353 for(int j = 0; j < HISTOGRAM_MODES; j++)
356 int *in = unit->accum[j];
357 for(int k = 0; k < HISTOGRAM_SLOTS; k++)
363 // Remove top and bottom from calculations. Doesn't work in high
364 // precision colormodels.
365 for(int i = 0; i < HISTOGRAM_MODES; i++)
368 accum[i][HISTOGRAM_SLOTS - 1] = 0;
373 void HistogramMain::calculate_automatic(VFrame *data)
375 calculate_histogram(data, 0);
376 config.reset_points(1);
379 for(int i = 0; i < 3; i++)
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);
386 float max_level = 1.0;
387 float min_level = 0.0;
389 // Get histogram slot above threshold of pixels
390 for(int j = 0; j < HISTOGRAM_SLOTS; j++)
393 if(total >= threshold)
395 max_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
400 // Get slot below 99% of pixels
402 for(int j = HISTOGRAM_SLOTS - 1; j >= 0; j--)
405 if(total >= threshold)
407 min_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
413 config.low_input[i] = min_level;
414 config.high_input[i] = max_level;
422 int HistogramMain::calculate_use_opengl()
424 // glHistogram doesn't work.
425 int result = get_use_opengl() &&
427 (!config.plot || !gui_open());
432 int HistogramMain::process_buffer(VFrame *frame,
433 int64_t start_position,
436 int need_reconfigure = load_configuration();
440 int use_opengl = calculate_use_opengl();
442 //printf("%d\n", use_opengl);
450 this->output = frame;
453 int cpus = input->get_w() * input->get_h() / 0x80000 + 2;
454 int smps = get_project_smp();
455 if( cpus > smps ) cpus = smps;
456 engine = new HistogramEngine(this, cpus, cpus);
458 // if to plot histogram
459 if(config.plot) send_render_gui(frame);
461 // Generate tables here. The same table is used by many packages to render
462 // each horizontal stripe. Need to cover the entire output range in each
463 // table to avoid green borders
466 if(need_reconfigure ||
470 // Calculate new curves
473 calculate_automatic(input);
477 // Generate transfer tables with value function for integer colormodels.
478 for(int i = 0; i < 3; i++)
479 tabulate_curve(i, 1);
482 // printf("HistogramMain::process_buffer %d %f %f %f %f %f %f %f %f %f\n",
484 // config.low_input[HISTOGRAM_RED],
485 // config.gamma[HISTOGRAM_RED],
486 // config.high_input[HISTOGRAM_RED],
487 // config.low_input[HISTOGRAM_GREEN],
488 // config.gamma[HISTOGRAM_GREEN],
489 // config.high_input[HISTOGRAM_GREEN],
490 // config.low_input[HISTOGRAM_BLUE],
491 // config.gamma[HISTOGRAM_BLUE],
492 // config.high_input[HISTOGRAM_BLUE]);
494 // Apply histogram in hardware
495 if(use_opengl) return run_opengl();
498 engine->process_packages(HistogramEngine::APPLY, input, 0);
502 void HistogramMain::tabulate_curve(int subscript, int use_value)
505 if(!lookup[subscript])
506 lookup[subscript] = new int[HISTOGRAM_SLOTS];
507 if(!preview_lookup[subscript])
508 preview_lookup[subscript] = new int[HISTOGRAM_SLOTS];
510 //printf("HistogramMain::tabulate_curve %d input=%p\n", __LINE__, input);
513 // Generate lookup tables for integer colormodels
516 switch(input->get_color_model())
520 for(i = 0; i < 0x100; i++)
522 lookup[subscript][i] =
523 (int)(calculate_level((float)i / 0xff, subscript, use_value) *
525 CLAMP(lookup[subscript][i], 0, 0xff);
528 // All other integer colormodels are converted to 16 bit RGB
530 for(i = 0; i < 0x10000; i++)
532 lookup[subscript][i] =
533 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
535 CLAMP(lookup[subscript][i], 0, 0xffff);
537 // for(i = 0; i < 0x100; i++)
539 // if(subscript == HISTOGRAM_BLUE) printf("%d ", lookup[subscript][i * 0x100]);
541 // if(subscript == HISTOGRAM_BLUE) printf("\n");
547 // Lookup table for preview only used for GUI
550 for(i = 0; i < 0x10000; i++)
552 preview_lookup[subscript][i] =
553 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
555 CLAMP(preview_lookup[subscript][i], 0, 0xffff);
560 int HistogramMain::handle_opengl()
563 // Functions to get pixel from either previous effect or texture
564 static const char *histogram_get_pixel1 =
565 "vec4 histogram_get_pixel()\n"
567 " return gl_FragColor;\n"
570 static const char *histogram_get_pixel2 =
571 "uniform sampler2D tex;\n"
572 "vec4 histogram_get_pixel()\n"
574 " return texture2D(tex, gl_TexCoord[0].st);\n"
577 static const char *head_frag =
578 "uniform vec4 low_input;\n"
579 "uniform vec4 high_input;\n"
580 "uniform vec4 gamma;\n"
581 "uniform vec4 low_output;\n"
582 "uniform vec4 output_scale;\n"
585 " float temp = 0.0;\n";
587 static const char *get_rgb_frag =
588 " vec4 pixel = histogram_get_pixel();\n";
590 static const char *get_yuv_frag =
591 " vec4 pixel = histogram_get_pixel();\n"
592 YUV_TO_RGB_FRAG("pixel");
594 #define APPLY_INPUT_CURVE(PIXEL, LOW_INPUT, HIGH_INPUT, GAMMA) \
595 "// apply input curve\n" \
596 " temp = (" PIXEL " - " LOW_INPUT ") / \n" \
597 " (" HIGH_INPUT " - " LOW_INPUT ");\n" \
598 " temp = max(temp, 0.0);\n" \
599 " " PIXEL " = pow(temp, 1.0 / " GAMMA ");\n"
603 static const char *apply_histogram_frag =
604 APPLY_INPUT_CURVE("pixel.r", "low_input.r", "high_input.r", "gamma.r")
605 APPLY_INPUT_CURVE("pixel.g", "low_input.g", "high_input.g", "gamma.g")
606 APPLY_INPUT_CURVE("pixel.b", "low_input.b", "high_input.b", "gamma.b")
607 "// apply output curve\n"
608 " pixel.rgb *= output_scale.rgb;\n"
609 " pixel.rgb += low_output.rgb;\n"
610 APPLY_INPUT_CURVE("pixel.r", "low_input.a", "high_input.a", "gamma.a")
611 APPLY_INPUT_CURVE("pixel.g", "low_input.a", "high_input.a", "gamma.a")
612 APPLY_INPUT_CURVE("pixel.b", "low_input.a", "high_input.a", "gamma.a")
613 "// apply output curve\n"
614 " pixel.rgb *= vec3(output_scale.a, output_scale.a, output_scale.a);\n"
615 " pixel.rgb += vec3(low_output.a, low_output.a, low_output.a);\n";
617 static const char *put_rgb_frag =
618 " gl_FragColor = pixel;\n"
621 static const char *put_yuv_frag =
622 RGB_TO_YUV_FRAG("pixel")
623 " gl_FragColor = pixel;\n"
628 get_output()->to_texture();
629 get_output()->enable_opengl();
631 const char *shader_stack[16];
632 memset(shader_stack,0, sizeof(shader_stack));
633 int current_shader = 0;
635 int need_color_matrix = BC_CModels::is_yuv(get_output()->get_color_model()) ? 1 : 0;
636 if( need_color_matrix )
637 shader_stack[current_shader++] = bc_gl_colors;
639 int aggregate_interpolation = 0;
640 int aggregate_gamma = 0;
641 int aggregate_colorbalance = 0;
642 // All aggregation possibilities must be accounted for because unsupported
643 // effects can get in between the aggregation members.
644 if(!strcmp(get_output()->get_prev_effect(2), _("Interpolate Pixels")) &&
645 !strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
646 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
648 aggregate_interpolation = 1;
650 aggregate_colorbalance = 1;
653 if(!strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
654 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
657 aggregate_colorbalance = 1;
660 if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
661 !strcmp(get_output()->get_prev_effect(0), _("Gamma")))
663 aggregate_interpolation = 1;
667 if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
668 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
670 aggregate_interpolation = 1;
671 aggregate_colorbalance = 1;
674 if(!strcmp(get_output()->get_prev_effect(0), _("Interpolate Pixels")))
675 aggregate_interpolation = 1;
677 if(!strcmp(get_output()->get_prev_effect(0), _("Gamma")))
680 if(!strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
681 aggregate_colorbalance = 1;
683 // The order of processing is fixed by this sequence
684 if(aggregate_interpolation)
685 INTERPOLATE_COMPILE(shader_stack, current_shader);
688 GAMMA_COMPILE(shader_stack, current_shader,
689 aggregate_interpolation);
691 if(aggregate_colorbalance)
692 COLORBALANCE_COMPILE(shader_stack, current_shader,
693 aggregate_interpolation || aggregate_gamma);
695 shader_stack[current_shader++] =
696 aggregate_interpolation || aggregate_gamma || aggregate_colorbalance ?
697 histogram_get_pixel1 : histogram_get_pixel2;
699 shader_stack[current_shader++] = head_frag;
700 shader_stack[current_shader++] = BC_CModels::is_yuv(get_output()->get_color_model()) ?
701 get_yuv_frag : get_rgb_frag;
702 shader_stack[current_shader++] = apply_histogram_frag;
703 shader_stack[current_shader++] = BC_CModels::is_yuv(get_output()->get_color_model()) ?
704 put_yuv_frag : put_rgb_frag;
706 shader_stack[current_shader] = 0;
707 unsigned int shader = VFrame::make_shader(shader_stack);
709 // printf("HistogramMain::handle_opengl %d %d %d %d shader=%d\n",
710 // aggregate_interpolation,
712 // aggregate_colorbalance,
720 float output_scale[4];
723 // printf("min x min y max x max y\n");
724 // printf("%f %f %f %f\n", input_min_r[0], input_min_r[1], input_max_r[0], input_max_r[1]);
725 // printf("%f %f %f %f\n", input_min_g[0], input_min_g[1], input_max_g[0], input_max_g[1]);
726 // printf("%f %f %f %f\n", input_min_b[0], input_min_b[1], input_max_b[0], input_max_b[1]);
727 // printf("%f %f %f %f\n", input_min_v[0], input_min_v[1], input_max_v[0], input_max_v[1]);
729 for(int i = 0; i < HISTOGRAM_MODES; i++)
731 low_input[i] = config.low_input[i];
732 high_input[i] = config.high_input[i];
733 gamma[i] = config.gamma[i];
734 low_output[i] = config.low_output[i];
735 output_scale[i] = config.high_output[i] - config.low_output[i];
740 glUseProgram(shader);
741 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
742 if(aggregate_gamma) GAMMA_UNIFORMS(shader);
743 if(aggregate_interpolation) INTERPOLATE_UNIFORMS(shader);
744 if(aggregate_colorbalance) COLORBALANCE_UNIFORMS(shader);
745 glUniform4fv(glGetUniformLocation(shader, "low_input"), 1, low_input);
746 glUniform4fv(glGetUniformLocation(shader, "high_input"), 1, high_input);
747 glUniform4fv(glGetUniformLocation(shader, "gamma"), 1, gamma);
748 glUniform4fv(glGetUniformLocation(shader, "low_output"), 1, low_output);
749 glUniform4fv(glGetUniformLocation(shader, "output_scale"), 1, output_scale);
750 if( need_color_matrix ) BC_GL_COLORS(shader);
753 get_output()->init_screen();
754 get_output()->bind_texture(0);
758 // Draw the affected half
761 glBegin(GL_TRIANGLES);
762 glNormal3f(0, 0, 1.0);
764 glTexCoord2f(0.0 / get_output()->get_texture_w(),
765 0.0 / get_output()->get_texture_h());
766 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
769 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
770 (float)get_output()->get_h() / get_output()->get_texture_h());
771 glVertex3f((float)get_output()->get_w(), -0.0, 0);
773 glTexCoord2f(0.0 / get_output()->get_texture_w(),
774 (float)get_output()->get_h() / get_output()->get_texture_h());
775 glVertex3f(0.0, -0.0, 0);
782 get_output()->draw_texture();
787 // Draw the unaffected half
790 glBegin(GL_TRIANGLES);
791 glNormal3f(0, 0, 1.0);
794 glTexCoord2f(0.0 / get_output()->get_texture_w(),
795 0.0 / get_output()->get_texture_h());
796 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
798 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
799 0.0 / get_output()->get_texture_h());
800 glVertex3f((float)get_output()->get_w(),
801 -(float)get_output()->get_h(), 0);
803 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
804 (float)get_output()->get_h() / get_output()->get_texture_h());
805 glVertex3f((float)get_output()->get_w(), -0.0, 0);
811 get_output()->set_opengl_state(VFrame::SCREEN);
827 HistogramPackage::HistogramPackage()
835 HistogramUnit::HistogramUnit(HistogramEngine *server,
836 HistogramMain *plugin)
839 this->plugin = plugin;
840 this->server = server;
841 for(int i = 0; i < HISTOGRAM_MODES; i++)
842 accum[i] = new int[HISTOGRAM_SLOTS];
845 HistogramUnit::~HistogramUnit()
847 for(int i = 0; i < HISTOGRAM_MODES; i++)
851 void HistogramUnit::process_package(LoadPackage *package)
853 HistogramPackage *pkg = (HistogramPackage*)package;
855 if(server->operation == HistogramEngine::HISTOGRAM)
857 int do_value = server->do_value;
860 #define HISTOGRAM_HEAD(type) \
862 for(int i = pkg->start; i < pkg->end; i++) \
864 type *row = (type*)data->get_rows()[i]; \
865 for(int j = 0; j < w; j++) \
868 #define HISTOGRAM_TAIL(components) \
869 /* Value takes the maximum of the output RGB values */ \
872 CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
873 CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
874 CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
875 r_out = lookup_r[r]; \
876 g_out = lookup_g[g]; \
877 b_out = lookup_b[b]; \
878 /* v = (r * 76 + g * 150 + b * 29) >> 8; */ \
879 v = MAX(r_out, g_out); \
881 v += -HISTOGRAM_MIN * 0xffff / 100; \
882 CLAMP(v, 0, HISTOGRAM_SLOTS - 1); \
886 r += -HISTOGRAM_MIN * 0xffff / 100; \
887 g += -HISTOGRAM_MIN * 0xffff / 100; \
888 b += -HISTOGRAM_MIN * 0xffff / 100; \
889 CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
890 CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
891 CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
903 VFrame *data = server->data;
904 int w = data->get_w();
905 //int h = data->get_h();
906 int *accum_r = accum[HISTOGRAM_RED];
907 int *accum_g = accum[HISTOGRAM_GREEN];
908 int *accum_b = accum[HISTOGRAM_BLUE];
909 int *accum_v = accum[HISTOGRAM_VALUE];
910 int32_t r, g, b, y, u, v;
911 int r_out, g_out, b_out;
912 int *lookup_r = plugin->preview_lookup[HISTOGRAM_RED];
913 int *lookup_g = plugin->preview_lookup[HISTOGRAM_GREEN];
914 int *lookup_b = plugin->preview_lookup[HISTOGRAM_BLUE];
916 switch(data->get_color_model())
919 HISTOGRAM_HEAD(unsigned char)
920 r = (row[0] << 8) | row[0];
921 g = (row[1] << 8) | row[1];
922 b = (row[2] << 8) | row[2];
926 HISTOGRAM_HEAD(float)
927 r = (int)(row[0] * 0xffff);
928 g = (int)(row[1] * 0xffff);
929 b = (int)(row[2] * 0xffff);
933 HISTOGRAM_HEAD(unsigned char)
934 y = (row[0] << 8) | row[0];
935 u = (row[1] << 8) | row[1];
936 v = (row[2] << 8) | row[2];
937 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
941 HISTOGRAM_HEAD(unsigned char)
942 r = (row[0] << 8) | row[0];
943 g = (row[1] << 8) | row[1];
944 b = (row[2] << 8) | row[2];
948 HISTOGRAM_HEAD(float)
949 r = (int)(row[0] * 0xffff);
950 g = (int)(row[1] * 0xffff);
951 b = (int)(row[2] * 0xffff);
955 HISTOGRAM_HEAD(unsigned char)
956 y = (row[0] << 8) | row[0];
957 u = (row[1] << 8) | row[1];
958 v = (row[2] << 8) | row[2];
959 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
963 HISTOGRAM_HEAD(uint16_t)
970 HISTOGRAM_HEAD(uint16_t)
974 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
977 case BC_RGBA16161616:
978 HISTOGRAM_HEAD(uint16_t)
984 case BC_YUVA16161616:
985 HISTOGRAM_HEAD(uint16_t)
989 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
995 if(server->operation == HistogramEngine::APPLY)
1000 #define PROCESS(type, components) \
1002 for(int i = pkg->start; i < pkg->end; i++) \
1004 type *row = (type*)input->get_rows()[i]; \
1005 for(int j = 0; j < w; j++) \
1007 if ( plugin->config.split && ((j + i * w / h) < w) ) \
1009 row[0] = lookup_r[row[0]]; \
1010 row[1] = lookup_g[row[1]]; \
1011 row[2] = lookup_b[row[2]]; \
1012 row += components; \
1017 #define PROCESS_YUV(type, components, max) \
1019 for(int i = pkg->start; i < pkg->end; i++) \
1021 type *row = (type*)input->get_rows()[i]; \
1022 for(int j = 0; j < w; j++) \
1024 if ( plugin->config.split && ((j + i * w / h) < w) ) \
1026 /* Convert to 16 bit RGB */ \
1029 y = (row[0] << 8) | row[0]; \
1030 u = (row[1] << 8) | row[1]; \
1031 v = (row[2] << 8) | row[2]; \
1040 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
1042 /* Look up in RGB domain */ \
1047 /* Convert to 16 bit YUV */ \
1048 YUV::yuv.rgb_to_yuv_16(r, g, b, y, u, v); \
1062 row += components; \
1067 #define PROCESS_FLOAT(components) \
1069 for(int i = pkg->start; i < pkg->end; i++) \
1071 float *row = (float*)input->get_rows()[i]; \
1072 for(int j = 0; j < w; j++) \
1074 if ( plugin->config.split && ((j + i * w / h) < w) ) \
1080 r = plugin->calculate_level(r, HISTOGRAM_RED, 1); \
1081 g = plugin->calculate_level(g, HISTOGRAM_GREEN, 1); \
1082 b = plugin->calculate_level(b, HISTOGRAM_BLUE, 1); \
1088 row += components; \
1094 VFrame *input = plugin->input;
1095 //VFrame *output = plugin->output;
1096 int w = input->get_w();
1097 int h = input->get_h();
1098 int *lookup_r = plugin->lookup[0];
1099 int *lookup_g = plugin->lookup[1];
1100 int *lookup_b = plugin->lookup[2];
1101 int r, g, b, y, u, v;
1102 switch(input->get_color_model())
1105 PROCESS(unsigned char, 3)
1111 PROCESS(unsigned char, 4)
1117 PROCESS(uint16_t, 3)
1119 case BC_RGBA16161616:
1120 PROCESS(uint16_t, 4)
1123 PROCESS_YUV(unsigned char, 3, 0xff)
1126 PROCESS_YUV(unsigned char, 4, 0xff)
1129 PROCESS_YUV(uint16_t, 3, 0xffff)
1131 case BC_YUVA16161616:
1132 PROCESS_YUV(uint16_t, 4, 0xffff)
1143 HistogramEngine::HistogramEngine(HistogramMain *plugin,
1146 : LoadServer(total_clients, total_packages)
1148 this->plugin = plugin;
1151 void HistogramEngine::init_packages()
1156 total_size = data->get_h();
1159 total_size = data->get_h();
1164 //int package_size = (int)((float)total_size / get_total_packages() + 1);
1167 for(int i = 0; i < get_total_packages(); i++)
1169 HistogramPackage *package = (HistogramPackage*)get_package(i);
1170 package->start = total_size * i / get_total_packages();
1171 package->end = total_size * (i + 1) / get_total_packages();
1174 // Initialize clients here in case some don't get run.
1175 for(int i = 0; i < get_total_clients(); i++)
1177 HistogramUnit *unit = (HistogramUnit*)get_client(i);
1178 for(int i = 0; i < HISTOGRAM_MODES; i++)
1179 bzero(unit->accum[i], sizeof(int) * HISTOGRAM_SLOTS);
1184 LoadClient* HistogramEngine::new_client()
1186 return new HistogramUnit(this, plugin);
1189 LoadPackage* HistogramEngine::new_package()
1191 return new HistogramPackage;
1194 void HistogramEngine::process_packages(int operation, VFrame *data, int do_value)
1197 this->operation = operation;
1198 this->do_value = do_value;
1199 LoadServer::process_packages();