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.append_newline();
218 output.terminate_string();
221 void HistogramMain::read_data(KeyFrame *keyframe)
225 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
232 result = input.read_tag();
236 if(input.tag.title_is("HISTOGRAM"))
238 config.automatic = input.tag.get_property("AUTOMATIC", config.automatic);
239 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
240 config.plot = input.tag.get_property("PLOT", config.plot);
241 config.split = input.tag.get_property("SPLIT", config.split);
245 w = input.tag.get_property("W", w);
246 h = input.tag.get_property("H", h);
247 parade = input.tag.get_property("PARADE", parade);
248 mode = input.tag.get_property("MODE", mode);
251 char string[BCTEXTLEN];
252 for(int i = 0; i < HISTOGRAM_MODES; i++)
254 sprintf(string, "LOW_OUTPUT_%d", i);
255 config.low_output[i] = input.tag.get_property(string, config.low_output[i]);
256 sprintf(string, "HIGH_OUTPUT_%d", i);
257 config.high_output[i] = input.tag.get_property(string, config.high_output[i]);
258 sprintf(string, "GAMMA_%d", i);
259 config.gamma[i] = input.tag.get_property(string, config.gamma[i]);
261 if(i == HISTOGRAM_VALUE || !config.automatic)
263 sprintf(string, "LOW_INPUT_%d", i);
264 config.low_input[i] = input.tag.get_property(string, config.low_input[i]);
265 sprintf(string, "HIGH_INPUT_%d", i);
266 config.high_input[i] = input.tag.get_property(string, config.high_input[i]);
268 //printf("HistogramMain::read_data %d %f %d\n", config.input_min[i], config.input_mid[i], config.input_max[i]);
278 float HistogramMain::calculate_level(float input,
284 // Scale to input range
285 if(!EQUIV(config.high_input[mode], config.low_input[mode]))
287 output = (input - config.low_input[mode]) /
288 (config.high_input[mode] - config.low_input[mode]);
297 if(!EQUIV(config.gamma[mode], 0))
299 output = pow(output, 1.0 / config.gamma[mode]);
300 CLAMP(output, 0, 1.0);
304 if(use_value && mode != HISTOGRAM_VALUE)
306 output = calculate_level(output, HISTOGRAM_VALUE, 0);
312 // scale to output range
313 if(!EQUIV(config.low_output[mode], config.high_output[mode]))
315 output = output * (config.high_output[mode] - config.low_output[mode]) +
316 config.low_output[mode];
319 CLAMP(output, 0, 1.0);
326 void HistogramMain::calculate_histogram(VFrame *data, int do_value)
329 if(!engine) engine = new HistogramEngine(this,
330 get_project_smp() + 1,
331 get_project_smp() + 1);
335 for(int i = 0; i < HISTOGRAM_MODES; i++)
336 accum[i] = new int[HISTOGRAM_SLOTS];
339 engine->process_packages(HistogramEngine::HISTOGRAM, data, do_value);
341 for(int i = 0; i < engine->get_total_clients(); i++)
343 HistogramUnit *unit = (HistogramUnit*)engine->get_client(i);
347 for(int j = 0; j < HISTOGRAM_MODES; j++)
349 memcpy(accum[j], unit->accum[j], sizeof(int) * HISTOGRAM_SLOTS);
354 for(int j = 0; j < HISTOGRAM_MODES; j++)
357 int *in = unit->accum[j];
358 for(int k = 0; k < HISTOGRAM_SLOTS; k++)
364 // Remove top and bottom from calculations. Doesn't work in high
365 // precision colormodels.
366 for(int i = 0; i < HISTOGRAM_MODES; i++)
369 accum[i][HISTOGRAM_SLOTS - 1] = 0;
374 void HistogramMain::calculate_automatic(VFrame *data)
376 calculate_histogram(data, 0);
377 config.reset_points(1);
380 for(int i = 0; i < 3; i++)
382 int *accum = this->accum[i];
383 int pixels = data->get_w() * data->get_h();
384 float white_fraction = 1.0 - (1.0 - config.threshold) / 2;
385 int threshold = (int)(white_fraction * pixels);
387 float max_level = 1.0;
388 float min_level = 0.0;
390 // Get histogram slot above threshold of pixels
391 for(int j = 0; j < HISTOGRAM_SLOTS; j++)
394 if(total >= threshold)
396 max_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
401 // Get slot below 99% of pixels
403 for(int j = HISTOGRAM_SLOTS - 1; j >= 0; j--)
406 if(total >= threshold)
408 min_level = (float)j / HISTOGRAM_SLOTS * FLOAT_RANGE + HIST_MIN_INPUT;
414 config.low_input[i] = min_level;
415 config.high_input[i] = max_level;
423 int HistogramMain::calculate_use_opengl()
425 // glHistogram doesn't work.
426 int result = get_use_opengl() &&
428 (!config.plot || !gui_open());
433 int HistogramMain::process_buffer(VFrame *frame,
434 int64_t start_position,
437 int need_reconfigure = load_configuration();
441 int use_opengl = calculate_use_opengl();
443 //printf("%d\n", use_opengl);
450 // Apply histogram in hardware
451 if(use_opengl) return run_opengl();
453 if(!engine) engine = new HistogramEngine(this,
454 get_project_smp() + 1,
455 get_project_smp() + 1);
457 this->output = frame;
458 // Always plot to set the curves if automatic
459 if(config.plot || config.automatic) 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]);
497 engine->process_packages(HistogramEngine::APPLY, input, 0);
503 void HistogramMain::tabulate_curve(int subscript, int use_value)
506 if(!lookup[subscript])
507 lookup[subscript] = new int[HISTOGRAM_SLOTS];
508 if(!preview_lookup[subscript])
509 preview_lookup[subscript] = new int[HISTOGRAM_SLOTS];
511 //printf("HistogramMain::tabulate_curve %d input=%p\n", __LINE__, input);
514 // Generate lookup tables for integer colormodels
517 switch(input->get_color_model())
521 for(i = 0; i < 0x100; i++)
523 lookup[subscript][i] =
524 (int)(calculate_level((float)i / 0xff, subscript, use_value) *
526 CLAMP(lookup[subscript][i], 0, 0xff);
529 // All other integer colormodels are converted to 16 bit RGB
531 for(i = 0; i < 0x10000; i++)
533 lookup[subscript][i] =
534 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
536 CLAMP(lookup[subscript][i], 0, 0xffff);
538 // for(i = 0; i < 0x100; i++)
540 // if(subscript == HISTOGRAM_BLUE) printf("%d ", lookup[subscript][i * 0x100]);
542 // if(subscript == HISTOGRAM_BLUE) printf("\n");
548 // Lookup table for preview only used for GUI
551 for(i = 0; i < 0x10000; i++)
553 preview_lookup[subscript][i] =
554 (int)(calculate_level((float)i / 0xffff, subscript, use_value) *
556 CLAMP(preview_lookup[subscript][i], 0, 0xffff);
561 int HistogramMain::handle_opengl()
564 // Functions to get pixel from either previous effect or texture
565 static const char *histogram_get_pixel1 =
566 "vec4 histogram_get_pixel()\n"
568 " return gl_FragColor;\n"
571 static const char *histogram_get_pixel2 =
572 "uniform sampler2D tex;\n"
573 "vec4 histogram_get_pixel()\n"
575 " return texture2D(tex, gl_TexCoord[0].st);\n"
578 static const char *head_frag =
579 "uniform vec4 low_input;\n"
580 "uniform vec4 high_input;\n"
581 "uniform vec4 gamma;\n"
582 "uniform vec4 low_output;\n"
583 "uniform vec4 output_scale;\n"
586 " float temp = 0.0;\n";
588 static const char *get_rgb_frag =
589 " vec4 pixel = histogram_get_pixel();\n";
591 static const char *get_yuv_frag =
592 " vec4 pixel = histogram_get_pixel();\n"
593 YUV_TO_RGB_FRAG("pixel");
595 #define APPLY_INPUT_CURVE(PIXEL, LOW_INPUT, HIGH_INPUT, GAMMA) \
596 "// apply input curve\n" \
597 " temp = (" PIXEL " - " LOW_INPUT ") / \n" \
598 " (" HIGH_INPUT " - " LOW_INPUT ");\n" \
599 " temp = max(temp, 0.0);\n" \
600 " " PIXEL " = pow(temp, 1.0 / " GAMMA ");\n"
604 static const char *apply_histogram_frag =
605 APPLY_INPUT_CURVE("pixel.r", "low_input.r", "high_input.r", "gamma.r")
606 APPLY_INPUT_CURVE("pixel.g", "low_input.g", "high_input.g", "gamma.g")
607 APPLY_INPUT_CURVE("pixel.b", "low_input.b", "high_input.b", "gamma.b")
608 "// apply output curve\n"
609 " pixel.rgb *= output_scale.rgb;\n"
610 " pixel.rgb += low_output.rgb;\n"
611 APPLY_INPUT_CURVE("pixel.r", "low_input.a", "high_input.a", "gamma.a")
612 APPLY_INPUT_CURVE("pixel.g", "low_input.a", "high_input.a", "gamma.a")
613 APPLY_INPUT_CURVE("pixel.b", "low_input.a", "high_input.a", "gamma.a")
614 "// apply output curve\n"
615 " pixel.rgb *= vec3(output_scale.a, output_scale.a, output_scale.a);\n"
616 " pixel.rgb += vec3(low_output.a, low_output.a, low_output.a);\n";
618 static const char *put_rgb_frag =
619 " gl_FragColor = pixel;\n"
622 static const char *put_yuv_frag =
623 RGB_TO_YUV_FRAG("pixel")
624 " gl_FragColor = pixel;\n"
629 get_output()->to_texture();
630 get_output()->enable_opengl();
632 const char *shader_stack[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
633 int current_shader = 0;
634 int aggregate_interpolation = 0;
635 int aggregate_gamma = 0;
636 int aggregate_colorbalance = 0;
637 // All aggregation possibilities must be accounted for because unsupported
638 // effects can get in between the aggregation members.
639 if(!strcmp(get_output()->get_prev_effect(2), _("Interpolate Pixels")) &&
640 !strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
641 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
643 aggregate_interpolation = 1;
645 aggregate_colorbalance = 1;
648 if(!strcmp(get_output()->get_prev_effect(1), _("Gamma")) &&
649 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
652 aggregate_colorbalance = 1;
655 if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
656 !strcmp(get_output()->get_prev_effect(0), _("Gamma")))
658 aggregate_interpolation = 1;
662 if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
663 !strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
665 aggregate_interpolation = 1;
666 aggregate_colorbalance = 1;
669 if(!strcmp(get_output()->get_prev_effect(0), _("Interpolate Pixels")))
670 aggregate_interpolation = 1;
672 if(!strcmp(get_output()->get_prev_effect(0), _("Gamma")))
675 if(!strcmp(get_output()->get_prev_effect(0), _("Color Balance")))
676 aggregate_colorbalance = 1;
679 // The order of processing is fixed by this sequence
680 if(aggregate_interpolation)
681 INTERPOLATE_COMPILE(shader_stack,
685 GAMMA_COMPILE(shader_stack,
687 aggregate_interpolation)
689 if(aggregate_colorbalance)
690 COLORBALANCE_COMPILE(shader_stack,
692 aggregate_interpolation || aggregate_gamma)
695 if(aggregate_interpolation || aggregate_gamma || aggregate_colorbalance)
696 shader_stack[current_shader++] = histogram_get_pixel1;
698 shader_stack[current_shader++] = histogram_get_pixel2;
700 unsigned int shader = 0;
701 switch(get_output()->get_color_model())
705 shader_stack[current_shader++] = head_frag;
706 shader_stack[current_shader++] = get_yuv_frag;
707 shader_stack[current_shader++] = apply_histogram_frag;
708 shader_stack[current_shader++] = put_yuv_frag;
711 shader_stack[current_shader++] = head_frag;
712 shader_stack[current_shader++] = get_rgb_frag;
713 shader_stack[current_shader++] = apply_histogram_frag;
714 shader_stack[current_shader++] = put_rgb_frag;
718 shader = VFrame::make_shader(0,
737 // printf("HistogramMain::handle_opengl %d %d %d %d shader=%d\n",
738 // aggregate_interpolation,
740 // aggregate_colorbalance,
748 float output_scale[4];
751 // printf("min x min y max x max y\n");
752 // printf("%f %f %f %f\n", input_min_r[0], input_min_r[1], input_max_r[0], input_max_r[1]);
753 // printf("%f %f %f %f\n", input_min_g[0], input_min_g[1], input_max_g[0], input_max_g[1]);
754 // printf("%f %f %f %f\n", input_min_b[0], input_min_b[1], input_max_b[0], input_max_b[1]);
755 // printf("%f %f %f %f\n", input_min_v[0], input_min_v[1], input_max_v[0], input_max_v[1]);
757 for(int i = 0; i < HISTOGRAM_MODES; i++)
759 low_input[i] = config.low_input[i];
760 high_input[i] = config.high_input[i];
761 gamma[i] = config.gamma[i];
762 low_output[i] = config.low_output[i];
763 output_scale[i] = config.high_output[i] - config.low_output[i];
768 glUseProgram(shader);
769 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
770 if(aggregate_gamma) GAMMA_UNIFORMS(shader)
771 if(aggregate_interpolation) INTERPOLATE_UNIFORMS(shader)
772 if(aggregate_colorbalance) COLORBALANCE_UNIFORMS(shader)
773 glUniform4fv(glGetUniformLocation(shader, "low_input"), 1, low_input);
774 glUniform4fv(glGetUniformLocation(shader, "high_input"), 1, high_input);
775 glUniform4fv(glGetUniformLocation(shader, "gamma"), 1, gamma);
776 glUniform4fv(glGetUniformLocation(shader, "low_output"), 1, low_output);
777 glUniform4fv(glGetUniformLocation(shader, "output_scale"), 1, output_scale);
780 get_output()->init_screen();
781 get_output()->bind_texture(0);
785 // Draw the affected half
788 glBegin(GL_TRIANGLES);
789 glNormal3f(0, 0, 1.0);
791 glTexCoord2f(0.0 / get_output()->get_texture_w(),
792 0.0 / get_output()->get_texture_h());
793 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
796 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
797 (float)get_output()->get_h() / get_output()->get_texture_h());
798 glVertex3f((float)get_output()->get_w(), -0.0, 0);
800 glTexCoord2f(0.0 / get_output()->get_texture_w(),
801 (float)get_output()->get_h() / get_output()->get_texture_h());
802 glVertex3f(0.0, -0.0, 0);
809 get_output()->draw_texture();
814 // Draw the unaffected half
817 glBegin(GL_TRIANGLES);
818 glNormal3f(0, 0, 1.0);
821 glTexCoord2f(0.0 / get_output()->get_texture_w(),
822 0.0 / get_output()->get_texture_h());
823 glVertex3f(0.0, -(float)get_output()->get_h(), 0);
825 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
826 0.0 / get_output()->get_texture_h());
827 glVertex3f((float)get_output()->get_w(),
828 -(float)get_output()->get_h(), 0);
830 glTexCoord2f((float)get_output()->get_w() / get_output()->get_texture_w(),
831 (float)get_output()->get_h() / get_output()->get_texture_h());
832 glVertex3f((float)get_output()->get_w(), -0.0, 0);
838 get_output()->set_opengl_state(VFrame::SCREEN);
854 HistogramPackage::HistogramPackage()
862 HistogramUnit::HistogramUnit(HistogramEngine *server,
863 HistogramMain *plugin)
866 this->plugin = plugin;
867 this->server = server;
868 for(int i = 0; i < HISTOGRAM_MODES; i++)
869 accum[i] = new int[HISTOGRAM_SLOTS];
872 HistogramUnit::~HistogramUnit()
874 for(int i = 0; i < HISTOGRAM_MODES; i++)
878 void HistogramUnit::process_package(LoadPackage *package)
880 HistogramPackage *pkg = (HistogramPackage*)package;
882 if(server->operation == HistogramEngine::HISTOGRAM)
884 int do_value = server->do_value;
887 #define HISTOGRAM_HEAD(type) \
889 for(int i = pkg->start; i < pkg->end; i++) \
891 type *row = (type*)data->get_rows()[i]; \
892 for(int j = 0; j < w; j++) \
895 #define HISTOGRAM_TAIL(components) \
896 /* Value takes the maximum of the output RGB values */ \
899 CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
900 CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
901 CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
902 r_out = lookup_r[r]; \
903 g_out = lookup_g[g]; \
904 b_out = lookup_b[b]; \
905 /* v = (r * 76 + g * 150 + b * 29) >> 8; */ \
906 v = MAX(r_out, g_out); \
908 v += -HISTOGRAM_MIN * 0xffff / 100; \
909 CLAMP(v, 0, HISTOGRAM_SLOTS - 1); \
913 r += -HISTOGRAM_MIN * 0xffff / 100; \
914 g += -HISTOGRAM_MIN * 0xffff / 100; \
915 b += -HISTOGRAM_MIN * 0xffff / 100; \
916 CLAMP(r, 0, HISTOGRAM_SLOTS - 1); \
917 CLAMP(g, 0, HISTOGRAM_SLOTS - 1); \
918 CLAMP(b, 0, HISTOGRAM_SLOTS - 1); \
930 VFrame *data = server->data;
931 int w = data->get_w();
932 //int h = data->get_h();
933 int *accum_r = accum[HISTOGRAM_RED];
934 int *accum_g = accum[HISTOGRAM_GREEN];
935 int *accum_b = accum[HISTOGRAM_BLUE];
936 int *accum_v = accum[HISTOGRAM_VALUE];
937 int32_t r, g, b, y, u, v;
938 int r_out, g_out, b_out;
939 int *lookup_r = plugin->preview_lookup[HISTOGRAM_RED];
940 int *lookup_g = plugin->preview_lookup[HISTOGRAM_GREEN];
941 int *lookup_b = plugin->preview_lookup[HISTOGRAM_BLUE];
943 switch(data->get_color_model())
946 HISTOGRAM_HEAD(unsigned char)
947 r = (row[0] << 8) | row[0];
948 g = (row[1] << 8) | row[1];
949 b = (row[2] << 8) | row[2];
953 HISTOGRAM_HEAD(float)
954 r = (int)(row[0] * 0xffff);
955 g = (int)(row[1] * 0xffff);
956 b = (int)(row[2] * 0xffff);
960 HISTOGRAM_HEAD(unsigned char)
961 y = (row[0] << 8) | row[0];
962 u = (row[1] << 8) | row[1];
963 v = (row[2] << 8) | row[2];
964 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
968 HISTOGRAM_HEAD(unsigned char)
969 r = (row[0] << 8) | row[0];
970 g = (row[1] << 8) | row[1];
971 b = (row[2] << 8) | row[2];
975 HISTOGRAM_HEAD(float)
976 r = (int)(row[0] * 0xffff);
977 g = (int)(row[1] * 0xffff);
978 b = (int)(row[2] * 0xffff);
982 HISTOGRAM_HEAD(unsigned char)
983 y = (row[0] << 8) | row[0];
984 u = (row[1] << 8) | row[1];
985 v = (row[2] << 8) | row[2];
986 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
990 HISTOGRAM_HEAD(uint16_t)
997 HISTOGRAM_HEAD(uint16_t)
1001 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
1004 case BC_RGBA16161616:
1005 HISTOGRAM_HEAD(uint16_t)
1011 case BC_YUVA16161616:
1012 HISTOGRAM_HEAD(uint16_t)
1016 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v);
1022 if(server->operation == HistogramEngine::APPLY)
1027 #define PROCESS(type, components) \
1029 for(int i = pkg->start; i < pkg->end; i++) \
1031 type *row = (type*)input->get_rows()[i]; \
1032 for(int j = 0; j < w; j++) \
1034 if ( plugin->config.split && ((j + i * w / h) < w) ) \
1036 row[0] = lookup_r[row[0]]; \
1037 row[1] = lookup_g[row[1]]; \
1038 row[2] = lookup_b[row[2]]; \
1039 row += components; \
1044 #define PROCESS_YUV(type, components, max) \
1046 for(int i = pkg->start; i < pkg->end; i++) \
1048 type *row = (type*)input->get_rows()[i]; \
1049 for(int j = 0; j < w; j++) \
1051 if ( plugin->config.split && ((j + i * w / h) < w) ) \
1053 /* Convert to 16 bit RGB */ \
1056 y = (row[0] << 8) | row[0]; \
1057 u = (row[1] << 8) | row[1]; \
1058 v = (row[2] << 8) | row[2]; \
1067 plugin->yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
1069 /* Look up in RGB domain */ \
1074 /* Convert to 16 bit YUV */ \
1075 plugin->yuv.rgb_to_yuv_16(r, g, b, y, u, v); \
1089 row += components; \
1094 #define PROCESS_FLOAT(components) \
1096 for(int i = pkg->start; i < pkg->end; i++) \
1098 float *row = (float*)input->get_rows()[i]; \
1099 for(int j = 0; j < w; j++) \
1101 if ( plugin->config.split && ((j + i * w / h) < w) ) \
1107 r = plugin->calculate_level(r, HISTOGRAM_RED, 1); \
1108 g = plugin->calculate_level(g, HISTOGRAM_GREEN, 1); \
1109 b = plugin->calculate_level(b, HISTOGRAM_BLUE, 1); \
1115 row += components; \
1121 VFrame *input = plugin->input;
1122 //VFrame *output = plugin->output;
1123 int w = input->get_w();
1124 int h = input->get_h();
1125 int *lookup_r = plugin->lookup[0];
1126 int *lookup_g = plugin->lookup[1];
1127 int *lookup_b = plugin->lookup[2];
1128 int r, g, b, y, u, v;
1129 switch(input->get_color_model())
1132 PROCESS(unsigned char, 3)
1138 PROCESS(unsigned char, 4)
1144 PROCESS(uint16_t, 3)
1146 case BC_RGBA16161616:
1147 PROCESS(uint16_t, 4)
1150 PROCESS_YUV(unsigned char, 3, 0xff)
1153 PROCESS_YUV(unsigned char, 4, 0xff)
1156 PROCESS_YUV(uint16_t, 3, 0xffff)
1158 case BC_YUVA16161616:
1159 PROCESS_YUV(uint16_t, 4, 0xffff)
1170 HistogramEngine::HistogramEngine(HistogramMain *plugin,
1173 : LoadServer(total_clients, total_packages)
1175 this->plugin = plugin;
1178 void HistogramEngine::init_packages()
1183 total_size = data->get_h();
1186 total_size = data->get_h();
1191 //int package_size = (int)((float)total_size / get_total_packages() + 1);
1194 for(int i = 0; i < get_total_packages(); i++)
1196 HistogramPackage *package = (HistogramPackage*)get_package(i);
1197 package->start = total_size * i / get_total_packages();
1198 package->end = total_size * (i + 1) / get_total_packages();
1201 // Initialize clients here in case some don't get run.
1202 for(int i = 0; i < get_total_clients(); i++)
1204 HistogramUnit *unit = (HistogramUnit*)get_client(i);
1205 for(int i = 0; i < HISTOGRAM_MODES; i++)
1206 bzero(unit->accum[i], sizeof(int) * HISTOGRAM_SLOTS);
1211 LoadClient* HistogramEngine::new_client()
1213 return new HistogramUnit(this, plugin);
1216 LoadPackage* HistogramEngine::new_package()
1218 return new HistogramPackage;
1221 void HistogramEngine::process_packages(int operation, VFrame *data, int do_value)
1224 this->operation = operation;
1225 this->do_value = do_value;
1226 LoadServer::process_packages();