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 _("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->get_data(), MESSAGESIZE);
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->get_data(), strlen(keyframe->get_data()));
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]) /
284 (config.high_input[mode] - config.low_input[mode]);
293 if(!EQUIV(config.gamma[mode], 0))
295 output = pow(output, 1.0 / config.gamma[mode]);
296 CLAMP(output, 0, 1.0);
300 if(use_value && mode != HISTOGRAM_VALUE)
302 output = calculate_level(output, HISTOGRAM_VALUE, 0);
308 // scale to output range
309 if(!EQUIV(config.low_output[mode], config.high_output[mode]))
311 output = output * (config.high_output[mode] - config.low_output[mode]) +
312 config.low_output[mode];
315 CLAMP(output, 0, 1.0);
322 void HistogramMain::calculate_histogram(VFrame *data, int do_value)
325 if(!engine) engine = new HistogramEngine(this,
326 get_project_smp() + 1,
327 get_project_smp() + 1);
331 for(int i = 0; i < HISTOGRAM_MODES; i++)
332 accum[i] = new int[HISTOGRAM_SLOTS];
335 engine->process_packages(HistogramEngine::HISTOGRAM, data, do_value);
337 for(int i = 0; i < engine->get_total_clients(); i++)
339 HistogramUnit *unit = (HistogramUnit*)engine->get_client(i);
343 for(int j = 0; j < HISTOGRAM_MODES; j++)
345 memcpy(accum[j], unit->accum[j], sizeof(int) * HISTOGRAM_SLOTS);
350 for(int j = 0; j < HISTOGRAM_MODES; j++)
353 int *in = unit->accum[j];
354 for(int k = 0; k < HISTOGRAM_SLOTS; k++)
360 // Remove top and bottom from calculations. Doesn't work in high
361 // precision colormodels.
362 for(int i = 0; i < HISTOGRAM_MODES; i++)
365 accum[i][HISTOGRAM_SLOTS - 1] = 0;
370 void HistogramMain::calculate_automatic(VFrame *data)
372 calculate_histogram(data, 0);
373 config.reset_points(1);
376 for(int i = 0; i < 3; i++)
378 int *accum = this->accum[i];
379 int pixels = data->get_w() * data->get_h();
380 float white_fraction = 1.0 - (1.0 - config.threshold) / 2;
381 int threshold = (int)(white_fraction * pixels);
383 float max_level = 1.0;
384 float min_level = 0.0;
386 // Get histogram slot above threshold of pixels
387 for(int j = 0; j < HISTOGRAM_SLOTS; j++)
390 if(total >= threshold)
392 max_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
397 // Get slot below 99% of pixels
399 for(int j = HISTOGRAM_SLOTS - 1; j >= 0; j--)
402 if(total >= threshold)
404 min_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
410 config.low_input[i] = min_level;
411 config.high_input[i] = max_level;
419 int HistogramMain::calculate_use_opengl()
421 // glHistogram doesn't work.
422 int result = get_use_opengl() &&
424 (!config.plot || !gui_open());
429 int HistogramMain::process_buffer(VFrame *frame,
430 int64_t start_position,
433 int need_reconfigure = load_configuration();
437 int use_opengl = calculate_use_opengl();
439 //printf("%d\n", use_opengl);
446 // Apply histogram in hardware
447 if(use_opengl) return run_opengl();
449 if(!engine) engine = new HistogramEngine(this,
450 get_project_smp() + 1,
451 get_project_smp() + 1);
453 this->output = frame;
454 // Always plot to set the curves if automatic
455 if(config.plot || config.automatic) send_render_gui(frame);
457 // Generate tables here. The same table is used by many packages to render
458 // each horizontal stripe. Need to cover the entire output range in each
459 // table to avoid green borders
462 if(need_reconfigure ||
466 // Calculate new curves
469 calculate_automatic(input);
473 // Generate transfer tables with value function for integer colormodels.
474 for(int i = 0; i < 3; i++)
475 tabulate_curve(i, 1);
478 // printf("HistogramMain::process_buffer %d %f %f %f %f %f %f %f %f %f\n",
480 // config.low_input[HISTOGRAM_RED],
481 // config.gamma[HISTOGRAM_RED],
482 // config.high_input[HISTOGRAM_RED],
483 // config.low_input[HISTOGRAM_GREEN],
484 // config.gamma[HISTOGRAM_GREEN],
485 // config.high_input[HISTOGRAM_GREEN],
486 // config.low_input[HISTOGRAM_BLUE],
487 // config.gamma[HISTOGRAM_BLUE],
488 // config.high_input[HISTOGRAM_BLUE]);
493 engine->process_packages(HistogramEngine::APPLY, input, 0);
499 void HistogramMain::tabulate_curve(int subscript, int use_value)
502 if(!lookup[subscript])
503 lookup[subscript] = new int[HISTOGRAM_SLOTS];
504 if(!preview_lookup[subscript])
505 preview_lookup[subscript] = new int[HISTOGRAM_SLOTS];
507 //printf("HistogramMain::tabulate_curve %d input=%p\n", __LINE__, input);
510 // Generate lookup tables for integer colormodels
513 switch(input->get_color_model())
517 for(i = 0; i < 0x100; i++)
519 lookup[subscript][i] =
520 (int)(calculate_level((float)i / 0xff, subscript, use_value) *
522 CLAMP(lookup[subscript][i], 0, 0xff);
525 // All other integer colormodels are converted to 16 bit RGB
527 for(i = 0; i < 0x10000; i++)
529 lookup[subscript][i] =
530 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
532 CLAMP(lookup[subscript][i], 0, 0xffff);
534 // for(i = 0; i < 0x100; i++)
536 // if(subscript == HISTOGRAM_BLUE) printf("%d ", lookup[subscript][i * 0x100]);
538 // if(subscript == HISTOGRAM_BLUE) printf("\n");
544 // Lookup table for preview only used for GUI
547 for(i = 0; i < 0x10000; i++)
549 preview_lookup[subscript][i] =
550 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
552 CLAMP(preview_lookup[subscript][i], 0, 0xffff);
557 int HistogramMain::handle_opengl()
560 // Functions to get pixel from either previous effect or texture
561 static const char *histogram_get_pixel1 =
562 "vec4 histogram_get_pixel()\n"
564 " return gl_FragColor;\n"
567 static const char *histogram_get_pixel2 =
568 "uniform sampler2D tex;\n"
569 "vec4 histogram_get_pixel()\n"
571 " return texture2D(tex, gl_TexCoord[0].st);\n"
574 static const char *head_frag =
575 "uniform vec4 low_input;\n"
576 "uniform vec4 high_input;\n"
577 "uniform vec4 gamma;\n"
578 "uniform vec4 low_output;\n"
579 "uniform vec4 output_scale;\n"
582 " float temp = 0.0;\n";
584 static const char *get_rgb_frag =
585 " vec4 pixel = histogram_get_pixel();\n";
587 static const char *get_yuv_frag =
588 " vec4 pixel = histogram_get_pixel();\n"
589 YUV_TO_RGB_FRAG("pixel");
591 #define APPLY_INPUT_CURVE(PIXEL, LOW_INPUT, HIGH_INPUT, GAMMA) \
592 "// apply input curve\n" \
593 " temp = (" PIXEL " - " LOW_INPUT ") / \n" \
594 " (" HIGH_INPUT " - " LOW_INPUT ");\n" \
595 " temp = max(temp, 0.0);\n" \
596 " " PIXEL " = pow(temp, 1.0 / " GAMMA ");\n"
600 static const char *apply_histogram_frag =
601 APPLY_INPUT_CURVE("pixel.r", "low_input.r", "high_input.r", "gamma.r")
602 APPLY_INPUT_CURVE("pixel.g", "low_input.g", "high_input.g", "gamma.g")
603 APPLY_INPUT_CURVE("pixel.b", "low_input.b", "high_input.b", "gamma.b")
604 "// apply output curve\n"
605 " pixel.rgb *= output_scale.rgb;\n"
606 " pixel.rgb += low_output.rgb;\n"
607 APPLY_INPUT_CURVE("pixel.r", "low_input.a", "high_input.a", "gamma.a")
608 APPLY_INPUT_CURVE("pixel.g", "low_input.a", "high_input.a", "gamma.a")
609 APPLY_INPUT_CURVE("pixel.b", "low_input.a", "high_input.a", "gamma.a")
610 "// apply output curve\n"
611 " pixel.rgb *= vec3(output_scale.a, output_scale.a, output_scale.a);\n"
612 " pixel.rgb += vec3(low_output.a, low_output.a, low_output.a);\n";
614 static const char *put_rgb_frag =
615 " gl_FragColor = pixel;\n"
618 static const char *put_yuv_frag =
619 RGB_TO_YUV_FRAG("pixel")
620 " gl_FragColor = pixel;\n"
625 get_output()->to_texture();
626 get_output()->enable_opengl();
628 const char *shader_stack[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
629 int current_shader = 0;
630 int aggregate_interpolation = 0;
631 int aggregate_gamma = 0;
632 int aggregate_colorbalance = 0;
633 // All aggregation possibilities must be accounted for because unsupported
634 // effects can get in between the aggregation members.
635 if(!strcmp(get_output()->get_prev_effect(2), _("Interpolate Pixels")) &&
636 !strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
637 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
639 aggregate_interpolation = 1;
641 aggregate_colorbalance = 1;
644 if(!strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
645 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
648 aggregate_colorbalance = 1;
651 if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
652 !strcmp(get_output()->get_prev_effect(0), _("Gamma")))
654 aggregate_interpolation = 1;
658 if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
659 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
661 aggregate_interpolation = 1;
662 aggregate_colorbalance = 1;
665 if(!strcmp(get_output()->get_prev_effect(0), _("Interpolate Pixels")))
666 aggregate_interpolation = 1;
668 if(!strcmp(get_output()->get_prev_effect(0), _("Gamma")))
671 if(!strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
672 aggregate_colorbalance = 1;
675 // The order of processing is fixed by this sequence
676 if(aggregate_interpolation)
677 INTERPOLATE_COMPILE(shader_stack,
681 GAMMA_COMPILE(shader_stack,
683 aggregate_interpolation)
685 if(aggregate_colorbalance)
686 COLORBALANCE_COMPILE(shader_stack,
688 aggregate_interpolation || aggregate_gamma)
691 if(aggregate_interpolation || aggregate_gamma || aggregate_colorbalance)
692 shader_stack[current_shader++] = histogram_get_pixel1;
694 shader_stack[current_shader++] = histogram_get_pixel2;
696 unsigned int shader = 0;
697 switch(get_output()->get_color_model())
701 shader_stack[current_shader++] = head_frag;
702 shader_stack[current_shader++] = get_yuv_frag;
703 shader_stack[current_shader++] = apply_histogram_frag;
704 shader_stack[current_shader++] = put_yuv_frag;
707 shader_stack[current_shader++] = head_frag;
708 shader_stack[current_shader++] = get_rgb_frag;
709 shader_stack[current_shader++] = apply_histogram_frag;
710 shader_stack[current_shader++] = put_rgb_frag;
714 shader = VFrame::make_shader(0,
733 // printf("HistogramMain::handle_opengl %d %d %d %d shader=%d\n",
734 // aggregate_interpolation,
736 // aggregate_colorbalance,
744 float output_scale[4];
747 // printf("min x min y max x max y\n");
748 // printf("%f %f %f %f\n", input_min_r[0], input_min_r[1], input_max_r[0], input_max_r[1]);
749 // printf("%f %f %f %f\n", input_min_g[0], input_min_g[1], input_max_g[0], input_max_g[1]);
750 // printf("%f %f %f %f\n", input_min_b[0], input_min_b[1], input_max_b[0], input_max_b[1]);
751 // printf("%f %f %f %f\n", input_min_v[0], input_min_v[1], input_max_v[0], input_max_v[1]);
753 for(int i = 0; i < HISTOGRAM_MODES; i++)
755 low_input[i] = config.low_input[i];
756 high_input[i] = config.high_input[i];
757 gamma[i] = config.gamma[i];
758 low_output[i] = config.low_output[i];
759 output_scale[i] = config.high_output[i] - config.low_output[i];
764 glUseProgram(shader);
765 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
766 if(aggregate_gamma) GAMMA_UNIFORMS(shader)
767 if(aggregate_interpolation) INTERPOLATE_UNIFORMS(shader)
768 if(aggregate_colorbalance) COLORBALANCE_UNIFORMS(shader)
769 glUniform4fv(glGetUniformLocation(shader, "low_input"), 1, low_input);
770 glUniform4fv(glGetUniformLocation(shader, "high_input"), 1, high_input);
771 glUniform4fv(glGetUniformLocation(shader, "gamma"), 1, gamma);
772 glUniform4fv(glGetUniformLocation(shader, "low_output"), 1, low_output);
773 glUniform4fv(glGetUniformLocation(shader, "output_scale"), 1, output_scale);
776 get_output()->init_screen();
777 get_output()->bind_texture(0);
781 // Draw the affected half
784 glBegin(GL_TRIANGLES);
785 glNormal3f(0, 0, 1.0);
787 glTexCoord2f(0.0 / get_output()->get_texture_w(),
788 0.0 / get_output()->get_texture_h());
789 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
792 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
793 (float)get_output()->get_h() / get_output()->get_texture_h());
794 glVertex3f((float)get_output()->get_w(), -0.0, 0);
796 glTexCoord2f(0.0 / get_output()->get_texture_w(),
797 (float)get_output()->get_h() / get_output()->get_texture_h());
798 glVertex3f(0.0, -0.0, 0);
805 get_output()->draw_texture();
810 // Draw the unaffected half
813 glBegin(GL_TRIANGLES);
814 glNormal3f(0, 0, 1.0);
817 glTexCoord2f(0.0 / get_output()->get_texture_w(),
818 0.0 / get_output()->get_texture_h());
819 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
821 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
822 0.0 / get_output()->get_texture_h());
823 glVertex3f((float)get_output()->get_w(),
824 -(float)get_output()->get_h(), 0);
826 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
827 (float)get_output()->get_h() / get_output()->get_texture_h());
828 glVertex3f((float)get_output()->get_w(), -0.0, 0);
834 get_output()->set_opengl_state(VFrame::SCREEN);
850 HistogramPackage::HistogramPackage()
858 HistogramUnit::HistogramUnit(HistogramEngine *server,
859 HistogramMain *plugin)
862 this->plugin = plugin;
863 this->server = server;
864 for(int i = 0; i < HISTOGRAM_MODES; i++)
865 accum[i] = new int[HISTOGRAM_SLOTS];
868 HistogramUnit::~HistogramUnit()
870 for(int i = 0; i < HISTOGRAM_MODES; i++)
874 void HistogramUnit::process_package(LoadPackage *package)
876 HistogramPackage *pkg = (HistogramPackage*)package;
878 if(server->operation == HistogramEngine::HISTOGRAM)
880 int do_value = server->do_value;
883 #define HISTOGRAM_HEAD(type) \
885 for(int i = pkg->start; i < pkg->end; i++) \
887 type *row = (type*)data->get_rows()[i]; \
888 for(int j = 0; j < w; j++) \
891 #define HISTOGRAM_TAIL(components) \
892 /* Value takes the maximum of the output RGB values */ \
895 CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
896 CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
897 CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
898 r_out = lookup_r[r]; \
899 g_out = lookup_g[g]; \
900 b_out = lookup_b[b]; \
901 /* v = (r * 76 + g * 150 + b * 29) >> 8; */ \
902 v = MAX(r_out, g_out); \
904 v += -HISTOGRAM_MIN * 0xffff / 100; \
905 CLAMP(v, 0, HISTOGRAM_SLOTS - 1); \
909 r += -HISTOGRAM_MIN * 0xffff / 100; \
910 g += -HISTOGRAM_MIN * 0xffff / 100; \
911 b += -HISTOGRAM_MIN * 0xffff / 100; \
912 CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
913 CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
914 CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
926 VFrame *data = server->data;
927 int w = data->get_w();
928 //int h = data->get_h();
929 int *accum_r = accum[HISTOGRAM_RED];
930 int *accum_g = accum[HISTOGRAM_GREEN];
931 int *accum_b = accum[HISTOGRAM_BLUE];
932 int *accum_v = accum[HISTOGRAM_VALUE];
933 int32_t r, g, b, y, u, v;
934 int r_out, g_out, b_out;
935 int *lookup_r = plugin->preview_lookup[HISTOGRAM_RED];
936 int *lookup_g = plugin->preview_lookup[HISTOGRAM_GREEN];
937 int *lookup_b = plugin->preview_lookup[HISTOGRAM_BLUE];
939 switch(data->get_color_model())
942 HISTOGRAM_HEAD(unsigned char)
943 r = (row[0] << 8) | row[0];
944 g = (row[1] << 8) | row[1];
945 b = (row[2] << 8) | row[2];
949 HISTOGRAM_HEAD(float)
950 r = (int)(row[0] * 0xffff);
951 g = (int)(row[1] * 0xffff);
952 b = (int)(row[2] * 0xffff);
956 HISTOGRAM_HEAD(unsigned char)
957 y = (row[0] << 8) | row[0];
958 u = (row[1] << 8) | row[1];
959 v = (row[2] << 8) | row[2];
960 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
964 HISTOGRAM_HEAD(unsigned char)
965 r = (row[0] << 8) | row[0];
966 g = (row[1] << 8) | row[1];
967 b = (row[2] << 8) | row[2];
971 HISTOGRAM_HEAD(float)
972 r = (int)(row[0] * 0xffff);
973 g = (int)(row[1] * 0xffff);
974 b = (int)(row[2] * 0xffff);
978 HISTOGRAM_HEAD(unsigned char)
979 y = (row[0] << 8) | row[0];
980 u = (row[1] << 8) | row[1];
981 v = (row[2] << 8) | row[2];
982 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
986 HISTOGRAM_HEAD(uint16_t)
993 HISTOGRAM_HEAD(uint16_t)
997 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
1000 case BC_RGBA16161616:
1001 HISTOGRAM_HEAD(uint16_t)
1007 case BC_YUVA16161616:
1008 HISTOGRAM_HEAD(uint16_t)
1012 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
1018 if(server->operation == HistogramEngine::APPLY)
1023 #define PROCESS(type, components) \
1025 for(int i = pkg->start; i < pkg->end; i++) \
1027 type *row = (type*)input->get_rows()[i]; \
1028 for(int j = 0; j < w; j++) \
1030 if ( plugin->config.split && ((j + i * w / h) < w) ) \
1032 row[0] = lookup_r[row[0]]; \
1033 row[1] = lookup_g[row[1]]; \
1034 row[2] = lookup_b[row[2]]; \
1035 row += components; \
1040 #define PROCESS_YUV(type, components, max) \
1042 for(int i = pkg->start; i < pkg->end; i++) \
1044 type *row = (type*)input->get_rows()[i]; \
1045 for(int j = 0; j < w; j++) \
1047 if ( plugin->config.split && ((j + i * w / h) < w) ) \
1049 /* Convert to 16 bit RGB */ \
1052 y = (row[0] << 8) | row[0]; \
1053 u = (row[1] << 8) | row[1]; \
1054 v = (row[2] << 8) | row[2]; \
1063 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
1065 /* Look up in RGB domain */ \
1070 /* Convert to 16 bit YUV */ \
1071 plugin->yuv.rgb_to_yuv_16(r, g, b, y, u, v); \
1085 row += components; \
1090 #define PROCESS_FLOAT(components) \
1092 for(int i = pkg->start; i < pkg->end; i++) \
1094 float *row = (float*)input->get_rows()[i]; \
1095 for(int j = 0; j < w; j++) \
1097 if ( plugin->config.split && ((j + i * w / h) < w) ) \
1103 r = plugin->calculate_level(r, HISTOGRAM_RED, 1); \
1104 g = plugin->calculate_level(g, HISTOGRAM_GREEN, 1); \
1105 b = plugin->calculate_level(b, HISTOGRAM_BLUE, 1); \
1111 row += components; \
1117 VFrame *input = plugin->input;
1118 //VFrame *output = plugin->output;
1119 int w = input->get_w();
1120 int h = input->get_h();
1121 int *lookup_r = plugin->lookup[0];
1122 int *lookup_g = plugin->lookup[1];
1123 int *lookup_b = plugin->lookup[2];
1124 int r, g, b, y, u, v;
1125 switch(input->get_color_model())
1128 PROCESS(unsigned char, 3)
1134 PROCESS(unsigned char, 4)
1140 PROCESS(uint16_t, 3)
1142 case BC_RGBA16161616:
1143 PROCESS(uint16_t, 4)
1146 PROCESS_YUV(unsigned char, 3, 0xff)
1149 PROCESS_YUV(unsigned char, 4, 0xff)
1152 PROCESS_YUV(uint16_t, 3, 0xffff)
1154 case BC_YUVA16161616:
1155 PROCESS_YUV(uint16_t, 4, 0xffff)
1166 HistogramEngine::HistogramEngine(HistogramMain *plugin,
1169 : LoadServer(total_clients, total_packages)
1171 this->plugin = plugin;
1174 void HistogramEngine::init_packages()
1179 total_size = data->get_h();
1182 total_size = data->get_h();
1187 //int package_size = (int)((float)total_size / get_total_packages() + 1);
1190 for(int i = 0; i < get_total_packages(); i++)
1192 HistogramPackage *package = (HistogramPackage*)get_package(i);
1193 package->start = total_size * i / get_total_packages();
1194 package->end = total_size * (i + 1) / get_total_packages();
1197 // Initialize clients here in case some don't get run.
1198 for(int i = 0; i < get_total_clients(); i++)
1200 HistogramUnit *unit = (HistogramUnit*)get_client(i);
1201 for(int i = 0; i < HISTOGRAM_MODES; i++)
1202 bzero(unit->accum[i], sizeof(int) * HISTOGRAM_SLOTS);
1207 LoadClient* HistogramEngine::new_client()
1209 return new HistogramUnit(this, plugin);
1212 LoadPackage* HistogramEngine::new_package()
1214 return new HistogramPackage;
1217 void HistogramEngine::process_packages(int operation, VFrame *data, int do_value)
1220 this->operation = operation;
1221 this->do_value = do_value;
1222 LoadServer::process_packages();