4 * Copyright (C) 1997-2011 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
22 #include "bcdisplayinfo.h"
23 #include "bcsignals.h"
25 #include "histogram.h"
26 #include "histogramconfig.h"
27 #include "histogramwindow.h"
37 HistogramWindow::HistogramWindow(HistogramMain *plugin)
38 : PluginClientWindow(plugin,
45 this->plugin = plugin;
49 HistogramWindow::~HistogramWindow()
54 void HistogramWindow::create_objects()
56 int margin = plugin->get_theme()->widget_border;
57 int x = margin, y = margin, x1 = margin;
59 add_subwindow(mode_v = new HistogramMode(plugin,
64 x += mode_v->get_w() + margin;
65 add_subwindow(mode_r = new HistogramMode(plugin,
70 x += mode_r->get_w() + margin;
71 add_subwindow(mode_g = new HistogramMode(plugin,
76 x += mode_g->get_w() + margin;
77 add_subwindow(mode_b = new HistogramMode(plugin,
84 x = get_w() - margin - plugin->get_theme()->get_image_set("histogram_rgb_toggle")[0]->get_w();
85 add_subwindow(parade_on = new HistogramParade(plugin,
90 x -= parade_on->get_w() + margin;
91 add_subwindow(parade_off = new HistogramParade(plugin,
99 y += parade_on->get_h() + margin;
100 add_subwindow(canvas_title1 = new BC_Title(margin,
103 add_subwindow(canvas_title2 = new BC_Title(get_w() - get_text_width(MEDIUMFONT, "110%") - margin,
107 y += canvas_title2->get_h() + margin;
109 canvas_h = get_h() - y - 210;
112 add_subwindow(low_input_carrot = new HistogramCarrot(plugin,
117 x = low_input_carrot->get_w() / 2 + x;
118 canvas_w = get_w() - x - x;
121 title2_x = x + (int)(canvas_w * -HIST_MIN_INPUT / FLOAT_RANGE);
122 title3_x = x + (int)(canvas_w * (1.0 - HIST_MIN_INPUT) / FLOAT_RANGE);
123 title4_x = x + (int)(canvas_w);
129 add_subwindow(canvas = new HistogramCanvas(plugin,
137 draw_3d_border(x - 2,
146 // Calculate output curve with no value function
147 plugin->tabulate_curve(plugin->mode, 0);
149 y += canvas->get_h();
152 add_subwindow(gamma_carrot = new HistogramCarrot(plugin,
155 canvas->get_w() / 2 -
156 low_input_carrot->get_w() / 2 ,
159 add_subwindow(high_input_carrot = new HistogramCarrot(plugin,
163 low_input_carrot->get_w() / 2,
165 y += low_input_carrot->get_h() + margin;
168 // add_subwindow(title = new BC_Title(x, y, _("Input:")));
169 // x += title->get_w() + margin;
170 low_input = new HistogramText(plugin,
174 low_input->create_objects();
176 x = get_w() / 2 - low_input->get_w() / 2;
177 gamma = new HistogramText(plugin,
181 gamma->create_objects();
184 x = get_w() - low_input->get_w() - margin;
185 high_input = new HistogramText(plugin,
189 high_input->create_objects();
192 y += high_input->get_h() + margin;
197 add_subwindow(output = new HistogramSlider(plugin,
207 draw_3d_border(output->get_x() - 2,
217 y += output->get_h();
221 add_subwindow(low_output_carrot = new HistogramCarrot(plugin,
226 add_subwindow(high_output_carrot = new HistogramCarrot(plugin,
230 low_output_carrot->get_w() / 2,
232 y += high_output_carrot->get_h() + margin;
235 // add_subwindow(title = new BC_Title(x, y, _("Output:")));
236 // x += title->get_w() + margin;
237 low_output = new HistogramText(plugin,
241 low_output->create_objects();
242 high_output = new HistogramText(plugin,
244 get_w() - low_output->get_w() - margin,
246 high_output->create_objects();
249 y += high_output->get_h() + margin;
251 add_subwindow(bar = new BC_Bar(x, y, get_w() - margin * 2));
252 y += bar->get_h() + margin;
254 add_subwindow(automatic = new HistogramAuto(plugin,
260 add_subwindow(threshold_title = new BC_Title(x, y, _("Threshold:")));
261 x += threshold_title->get_w() + margin;
262 threshold = new HistogramText(plugin,
266 threshold->create_objects();
269 add_subwindow(reset = new HistogramReset(plugin,
271 y + threshold->get_h() + margin));
274 y += automatic->get_h() + margin;
275 add_subwindow(plot = new HistogramPlot(plugin,
279 y += plot->get_h() + 5;
280 add_subwindow(split = new HistogramSplit(plugin,
292 int HistogramWindow::resize_event(int w, int h)
294 int xdiff = w - get_w();
295 int ydiff = h - get_h();
297 parade_on->reposition_window(parade_on->get_x() + xdiff,
299 parade_off->reposition_window(parade_off->get_x() + xdiff,
301 canvas_title2->reposition_window(canvas_title2->get_x() + xdiff,
302 canvas_title2->get_y());
304 // Canvas follows window size
305 canvas_w = canvas_w + xdiff;
306 canvas_h = canvas_h + ydiff;
307 canvas->reposition_window(canvas->get_x(),
313 draw_3d_border(canvas->get_x() - 2,
322 low_input_carrot->reposition_window(low_input_carrot->get_x(),
323 low_input_carrot->get_y() + ydiff);
324 gamma_carrot->reposition_window(gamma_carrot->get_x(),
325 gamma_carrot->get_y() + ydiff);
326 high_input_carrot->reposition_window(high_input_carrot->get_x(),
327 high_input_carrot->get_y() + ydiff);
329 low_input->reposition_window(low_input->get_x(),
330 low_input->get_y() + ydiff);
331 gamma->reposition_window(w / 2 - gamma->get_w() / 2,
332 gamma->get_y() + ydiff);
333 high_input->reposition_window(high_input->get_x() + xdiff,
334 low_input->get_y() + ydiff);
336 output->reposition_window(output->get_x(),
337 output->get_y() + ydiff,
338 output->get_w() + xdiff,
343 draw_3d_border(output->get_x() - 2,
352 low_output_carrot->reposition_window(low_output_carrot->get_x(),
353 low_output_carrot->get_y() + ydiff);
354 high_output_carrot->reposition_window(high_output_carrot->get_x(),
355 high_output_carrot->get_y() + ydiff);
357 low_output->reposition_window(low_output->get_x(),
358 low_output->get_y() + ydiff);
359 high_output->reposition_window(high_output->get_x() + xdiff,
360 high_output->get_y() + ydiff);
362 bar->reposition_window(bar->get_x(),
363 bar->get_y() + ydiff,
364 bar->get_w() + xdiff);
366 automatic->reposition_window(automatic->get_x(),
367 automatic->get_y() + ydiff);
368 threshold_title->reposition_window(threshold_title->get_x(),
369 threshold_title->get_y() + ydiff);
370 threshold->reposition_window(threshold->get_x(),
371 threshold->get_y() + ydiff);
372 reset->reposition_window(reset->get_x(),
373 reset->get_y() + ydiff);
375 plot->reposition_window(plot->get_x(),
376 plot->get_y() + ydiff);
377 split->reposition_window(split->get_x(),
378 split->get_y() + ydiff);
390 int HistogramWindow::keypress_event()
397 for(int i = 0; i < HISTOGRAM_MODES; i++)
399 if(active_value == &plugin->config.gamma[i])
403 if(get_keypress() == RIGHT || get_keypress() == UP)
405 *active_value += sign * PRECISION;
406 plugin->config.boundaries();
408 plugin->send_configure_change();
412 if(get_keypress() == LEFT || get_keypress() == DOWN)
414 *active_value -= sign * PRECISION;
415 plugin->config.boundaries();
417 plugin->send_configure_change();
425 void HistogramWindow::update(int do_canvases,
432 automatic->update(plugin->config.automatic);
433 mode_v->update(plugin->mode == HISTOGRAM_VALUE ? 1 : 0);
434 mode_r->update(plugin->mode == HISTOGRAM_RED ? 1 : 0);
435 mode_g->update(plugin->mode == HISTOGRAM_GREEN ? 1 : 0);
436 mode_b->update(plugin->mode == HISTOGRAM_BLUE ? 1 : 0);
437 plot->update(plugin->config.plot);
438 split->update(plugin->config.split);
439 parade_on->update(plugin->parade ? 1 : 0);
440 parade_off->update(plugin->parade ? 0 : 1);
451 low_input_carrot->update();
452 high_input_carrot->update();
453 gamma_carrot->update();
454 low_output_carrot->update();
455 high_output_carrot->update();
462 high_input->update();
463 low_output->update();
464 high_output->update();
472 void HistogramWindow::draw_canvas_mode(int mode, int color, int y, int h)
474 int *accum = plugin->accum[mode];
475 int accum_per_canvas_i = HISTOGRAM_SLOTS / canvas_w + 1;
476 float accum_per_canvas_f = (float)HISTOGRAM_SLOTS / canvas_w;
482 for(int i = 0; i < HISTOGRAM_SLOTS; i++)
484 if(accum && accum[i] > normalize) normalize = accum[i];
490 for(int i = 0; i < canvas_w; i++)
492 int accum_start = (int)(accum_per_canvas_f * i);
493 int accum_end = accum_start + accum_per_canvas_i;
495 for(int j = accum_start; j < accum_end; j++)
497 max = MAX(accum[j], max);
500 // max = max * h / normalize;
501 max = (int)(log(max) / log(normalize) * h);
503 canvas->set_color(BLACK);
504 canvas->draw_line(i, y, i, y + h - max);
505 canvas->set_color(color);
506 canvas->draw_line(i, y + h - max, i, y + h);
511 canvas->set_color(BLACK);
512 canvas->draw_box(0, y, canvas_w, h);
516 canvas->set_color(WHITE);
517 canvas->set_line_width(2);
521 for(int i = 0; i < canvas_w; i++)
523 float input = (float)i /
527 float output = plugin->calculate_level(input,
531 int y2 = h - (int)(output * h);
534 canvas->draw_line(i - 1, y + y1, i, y + y2);
539 canvas->set_line_width(1);
544 void HistogramWindow::update_canvas()
548 draw_canvas_mode(HISTOGRAM_RED, RED, 0, canvas_h / 3);
549 draw_canvas_mode(HISTOGRAM_GREEN, GREEN, canvas_h / 3, canvas_h / 3);
550 draw_canvas_mode(HISTOGRAM_BLUE, BLUE, canvas_h * 2 / 3, canvas_h - canvas_h * 2 / 3);
554 draw_canvas_mode(plugin->mode, MEGREY, 0, canvas_h);
558 // Draw 0 and 100% lines.
559 canvas->set_color(RED);
560 int x = (int)(canvas_w * -HIST_MIN_INPUT / FLOAT_RANGE);
565 x = (int)(canvas_w * (1.0 - HIST_MIN_INPUT) / FLOAT_RANGE);
576 HistogramParade::HistogramParade(HistogramMain *plugin,
577 HistogramWindow *gui,
583 value ? plugin->get_theme()->get_image_set("histogram_rgb_toggle") :
584 plugin->get_theme()->get_image_set("histogram_toggle"),
587 this->plugin = plugin;
591 set_tooltip(_("RGB Parade on"));
593 set_tooltip(_("RGB Parade off"));
596 int HistogramParade::handle_event()
599 plugin->parade = value;
613 HistogramCanvas::HistogramCanvas(HistogramMain *plugin,
614 HistogramWindow *gui,
625 this->plugin = plugin;
629 int HistogramCanvas::button_press_event()
632 if(is_event_win() && cursor_inside())
634 if(!plugin->dragging_point &&
635 (!plugin->config.automatic || plugin->mode == HISTOGRAM_VALUE))
643 int HistogramCanvas::cursor_motion_event()
645 if(is_event_win() && cursor_inside())
651 int HistogramCanvas::button_release_event()
669 HistogramReset::HistogramReset(HistogramMain *plugin,
672 : BC_GenericButton(x, y, _("Reset"))
674 this->plugin = plugin;
676 int HistogramReset::handle_event()
678 plugin->config.reset(0);
679 ((HistogramWindow*)plugin->thread->window)->update(1, 1, 1, 1);
680 plugin->send_configure_change();
690 HistogramCarrot::HistogramCarrot(HistogramMain *plugin,
691 HistogramWindow *gui,
696 plugin->get_theme()->get_image_set("histogram_carrot"),
699 this->plugin = plugin;
704 HistogramCarrot::~HistogramCarrot()
708 float* HistogramCarrot::get_value()
710 if(this == gui->low_input_carrot)
712 return &plugin->config.low_input[plugin->mode];
715 if(this == gui->high_input_carrot)
717 return &plugin->config.high_input[plugin->mode];
720 if(this == gui->gamma_carrot)
722 return &plugin->config.gamma[plugin->mode];
725 if(this == gui->low_output_carrot)
727 return &plugin->config.low_output[plugin->mode];
730 if(this == gui->high_output_carrot)
732 return &plugin->config.high_output[plugin->mode];
737 void HistogramCarrot::update()
740 float *value = get_value();
742 if(this != gui->gamma_carrot)
744 new_x = (int)(gui->canvas->get_x() +
745 (*value - HIST_MIN_INPUT) *
746 gui->canvas->get_w() /
747 (HIST_MAX_INPUT - HIST_MIN_INPUT) -
752 float min = plugin->config.low_input[plugin->mode];
753 float max = plugin->config.high_input[plugin->mode];
754 float delta = (max - min) / 2.0;
755 float mid = min + delta;
756 float tmp = log10(1.0 / *value);
757 tmp = mid + delta * tmp;
759 //printf("HistogramCarrot::update %d %f %f\n", __LINE__, *value, tmp);
761 new_x = gui->canvas->get_x() -
763 (int)(gui->canvas->get_w() *
764 (tmp - HIST_MIN_INPUT) /
765 (HIST_MAX_INPUT - HIST_MIN_INPUT));
768 reposition_window(new_x, get_y());
771 int HistogramCarrot::button_press_event()
773 if(is_event_win() && get_buttonpress() == 1)
777 set_status(BC_Toggle::TOGGLE_DOWN);
779 BC_Toggle::update(0);
780 gui->active_value = get_value();
781 // Disable the other toggles
782 if(this != gui->low_input_carrot) gui->low_input_carrot->BC_Toggle::update(0);
783 if(this != gui->high_input_carrot) gui->high_input_carrot->BC_Toggle::update(0);
784 if(this != gui->gamma_carrot) gui->gamma_carrot->BC_Toggle::update(0);
785 if(this != gui->low_output_carrot) gui->low_output_carrot->BC_Toggle::update(0);
786 if(this != gui->high_output_carrot) gui->high_output_carrot->BC_Toggle::update(0);
787 starting_x = get_x();
788 offset_x = gui->get_relative_cursor_x();
789 offset_y = gui->get_relative_cursor_y();
790 //printf("HistogramCarrot::button_press_event %d %d %d\n", __LINE__, starting_x, offset_x);
798 int HistogramCarrot::button_release_event()
800 int result = BC_Toggle::button_release_event();
806 int HistogramCarrot::cursor_motion_event()
808 int cursor_x = gui->get_relative_cursor_x();
812 //printf("HistogramCarrot::cursor_motion_event %d %d\n", __LINE__, cursor_x);
813 int new_x = starting_x + cursor_x - offset_x;
817 float *value = get_value();
818 if(this == gui->gamma_carrot)
820 float min = gui->low_input_carrot->get_x();
821 float max = gui->high_input_carrot->get_x();
822 float delta = (max - min) / 2.0;
825 float mid = min + delta;
826 float tmp = (float)(new_x - mid) /
828 tmp = 1.0 / pow(10, tmp);
829 CLAMP(tmp, MIN_GAMMA, MAX_GAMMA);
831 //printf("HistogramCarrot::update %d %f\n", __LINE__, tmp);
836 int min_x = gui->canvas->get_x() - get_w() / 2;
837 int max_x = gui->canvas->get_x() + gui->canvas->get_w() - get_w() / 2;
838 CLAMP(new_x, min_x, max_x);
839 *value = HIST_MIN_INPUT +
840 (HIST_MAX_INPUT - HIST_MIN_INPUT) *
845 reposition_window(new_x, get_y());
849 (this == gui->low_input_carrot || this == gui->high_input_carrot),
852 plugin->send_configure_change();
866 HistogramSlider::HistogramSlider(HistogramMain *plugin,
867 HistogramWindow *gui,
873 : BC_SubWindow(x, y, w, h)
875 this->plugin = plugin;
877 this->is_input = is_input;
881 int HistogramSlider::input_to_pixel(float input)
883 return (int)((input - HIST_MIN_INPUT) / FLOAT_RANGE * get_w());
886 void HistogramSlider::update()
890 //int half_h = get_h() / 2;
891 //int quarter_h = get_h() / 4;
892 int mode = plugin->mode;
897 clear_box(0, 0, w, h);
904 case HISTOGRAM_GREEN:
912 for(int i = 0; i < w; i++)
914 int color = (int)(i * 0xff / w);
915 set_color(((r * color / 0xff) << 16) |
916 ((g * color / 0xff) << 8) |
919 draw_line(i, 0, i, h);
934 HistogramAuto::HistogramAuto(HistogramMain *plugin,
937 : BC_CheckBox(x, y, plugin->config.automatic, _("Automatic"))
939 this->plugin = plugin;
942 int HistogramAuto::handle_event()
944 plugin->config.automatic = get_value();
945 plugin->send_configure_change();
952 HistogramPlot::HistogramPlot(HistogramMain *plugin,
955 : BC_CheckBox(x, y, plugin->config.plot, _("Plot histogram"))
957 this->plugin = plugin;
960 int HistogramPlot::handle_event()
962 plugin->config.plot = get_value();
963 plugin->send_configure_change();
970 HistogramSplit::HistogramSplit(HistogramMain *plugin,
973 : BC_CheckBox(x, y, plugin->config.split, _("Split output"))
975 this->plugin = plugin;
978 int HistogramSplit::handle_event()
980 plugin->config.split = get_value();
981 plugin->send_configure_change();
987 HistogramMode::HistogramMode(HistogramMain *plugin,
992 : BC_Radial(x, y, plugin->mode == value, text)
994 this->plugin = plugin;
997 int HistogramMode::handle_event()
999 HistogramWindow *gui = (HistogramWindow*)plugin->thread->window;
1000 plugin->mode = value;
1001 plugin->current_point= -1;
1002 gui->active_value = 0;
1003 gui->low_input_carrot->BC_Toggle::update(0);
1004 gui->gamma_carrot->BC_Toggle::update(0);
1005 gui->high_input_carrot->BC_Toggle::update(0);
1006 gui->low_output_carrot->BC_Toggle::update(0);
1007 gui->high_output_carrot->BC_Toggle::update(0);
1008 gui->update(1, 1, 1, 1);
1009 // plugin->send_configure_change();
1022 HistogramText::HistogramText(HistogramMain *plugin,
1023 HistogramWindow *gui,
1026 : BC_TumbleTextBox(gui,
1028 (float)HIST_MIN_INPUT,
1029 (float)HIST_MAX_INPUT,
1034 this->plugin = plugin;
1036 set_precision(DIGITS);
1037 set_increment(PRECISION);
1040 float* HistogramText::get_value()
1042 if(this == gui->low_input)
1044 return &plugin->config.low_input[plugin->mode];
1047 if(this == gui->high_input)
1049 return &plugin->config.high_input[plugin->mode];
1052 if(this == gui->gamma)
1054 return &plugin->config.gamma[plugin->mode];
1057 if(this == gui->low_output)
1059 return &plugin->config.low_output[plugin->mode];
1062 if(this == gui->high_output)
1064 return &plugin->config.high_output[plugin->mode];
1067 if(this == gui->threshold)
1069 return &plugin->config.threshold;
1075 int HistogramText::handle_event()
1077 float *output = get_value();
1080 *output = atof(get_text());
1083 gui->update(1, 1, 0, 0);
1084 plugin->send_configure_change();
1088 void HistogramText::update()
1090 float *output = get_value();
1093 BC_TumbleTextBox::update(*output);