4 * Copyright (C) 2008 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"
24 #include "bistogram.h"
25 #include "bistogramconfig.h"
26 #include "bistogramwindow.h"
34 HistogramWindow::HistogramWindow(HistogramMain *plugin)
35 : PluginClientWindow(plugin, 440, 480, 440, 480, 0)
37 this->plugin = plugin;
40 HistogramWindow::~HistogramWindow()
44 #include "min_picon_png.h"
45 #include "mid_picon_png.h"
46 #include "max_picon_png.h"
47 static VFramePng max_picon_image(max_picon_png);
48 static VFramePng mid_picon_image(mid_picon_png);
49 static VFramePng min_picon_image(min_picon_png);
51 void HistogramWindow::create_objects()
53 int x = 10, y = 10, x1 = 10;
56 max_picon = create_pixmap(&max_picon_image);
57 mid_picon = create_pixmap(&mid_picon_image);
58 min_picon = create_pixmap(&min_picon_image);
59 add_subwindow(mode_v = new HistogramMode(plugin,
65 add_subwindow(mode_r = new HistogramMode(plugin,
71 add_subwindow(mode_g = new HistogramMode(plugin,
77 add_subwindow(mode_b = new HistogramMode(plugin,
83 // add_subwindow(mode_a = new HistogramMode(plugin,
91 add_subwindow(title = new BC_Title(x, y, _("Input X:")));
92 x += title->get_w() + 10;
93 input_x = new HistogramInputText(plugin,
98 input_x->create_objects();
100 x += input_x->get_w() + 10;
101 add_subwindow(title = new BC_Title(x, y, _("Input Y:")));
102 x += title->get_w() + 10;
103 input_y = new HistogramInputText(plugin,
108 input_y->create_objects();
113 canvas_w = get_w() - x - x;
114 canvas_h = get_h() - y - 170;
116 title2_x = x + (int)(canvas_w * -HIST_MIN_INPUT / FLOAT_RANGE);
117 title3_x = x + (int)(canvas_w * (1.0 - HIST_MIN_INPUT) / FLOAT_RANGE);
118 title4_x = x + (int)(canvas_w);
119 add_subwindow(canvas = new HistogramCanvas(plugin,
125 draw_canvas_overlay();
128 y += canvas->get_h() + 1;
129 add_subwindow(new BC_Title(title1_x,
132 add_subwindow(new BC_Title(title2_x,
135 add_subwindow(new BC_Title(title3_x - get_text_width(MEDIUMFONT, "100"),
138 add_subwindow(new BC_Title(title4_x - get_text_width(MEDIUMFONT, "110"),
143 add_subwindow(title = new BC_Title(x, y, _("Output min:")));
144 x += title->get_w() + 10;
145 output_min = new HistogramOutputText(plugin,
149 &plugin->config.output_min[plugin->mode]);
150 output_min->create_objects();
151 x += output_min->get_w() + 10;
152 add_subwindow(new BC_Title(x, y, _("Output Max:")));
153 x += title->get_w() + 10;
154 output_max = new HistogramOutputText(plugin,
158 &plugin->config.output_max[plugin->mode]);
159 output_max->create_objects();
164 add_subwindow(output = new HistogramSlider(plugin,
175 add_subwindow(automatic = new HistogramAuto(plugin,
180 add_subwindow(new HistogramReset(plugin,
184 add_subwindow(new BC_Title(x, y, _("Threshold:")));
186 threshold = new HistogramOutputText(plugin,
190 &plugin->config.threshold);
191 threshold->create_objects();
194 add_subwindow(split = new HistogramSplit(plugin, x, y));
197 add_subwindow(new BC_Title(x,y, _("Interpolation:")));
199 add_subwindow(smoothModeChoser = new HistogramSmoothMode(plugin, this, x, y));
200 smoothModeChoser->create_objects();
205 int HistogramWindow::keypress_event()
208 if(get_keypress() == BACKSPACE ||
209 get_keypress() == DELETE)
211 if(plugin->current_point >= 0)
213 HistogramPoint *current =
214 plugin->config.points[plugin->mode].get_item_number(plugin->current_point);
216 plugin->current_point = -1;
219 plugin->send_configure_change();
226 void HistogramWindow::update(int do_input)
228 automatic->update(plugin->config.automatic);
229 threshold->update(plugin->config.threshold);
232 if(do_input) update_input();
236 void HistogramWindow::update_input()
242 void HistogramWindow::update_output()
245 output_min->update(plugin->config.output_min[plugin->mode]);
246 output_max->update(plugin->config.output_max[plugin->mode]);
249 void HistogramWindow::update_mode()
251 mode_v->update(plugin->mode == HISTOGRAM_VALUE ? 1 : 0);
252 mode_r->update(plugin->mode == HISTOGRAM_RED ? 1 : 0);
253 mode_g->update(plugin->mode == HISTOGRAM_GREEN ? 1 : 0);
254 mode_b->update(plugin->mode == HISTOGRAM_BLUE ? 1 : 0);
255 output_min->output = &plugin->config.output_min[plugin->mode];
256 output_max->output = &plugin->config.output_max[plugin->mode];
259 void HistogramWindow::draw_canvas_overlay()
261 canvas->set_color(0x00ff00);
264 // Calculate output curve
265 plugin->tabulate_curve(plugin->mode, 0);
268 for(int i = 0; i < canvas_w; i++)
270 float input = (float)i /
274 float output = plugin->calculate_smooth(input, plugin->mode);
276 int y2 = canvas_h - (int)(output * canvas_h);
279 canvas->draw_line(i - 1, y1, i, y2);
284 // Draw output points
285 HistogramPoint *current = plugin->config.points[plugin->mode].first;
289 int x = (int)((current->x - HIST_MIN_INPUT) * canvas_w / FLOAT_RANGE);
290 int y = (int)(canvas_h - current->y * canvas_h);
291 if(number == plugin->current_point)
292 canvas->draw_box(x - BOX_SIZE / 2, y - BOX_SIZE / 2, BOX_SIZE, BOX_SIZE);
294 canvas->draw_rectangle(x - BOX_SIZE / 2, y - BOX_SIZE / 2, BOX_SIZE, BOX_SIZE);
297 if (plugin->config.smoothMode > HISTOGRAM_LINEAR)
300 canvas->set_color(0x0000ff);
301 x2 = (int)((current->x + current->xoffset_right - HIST_MIN_INPUT) * canvas_w / FLOAT_RANGE);
302 x1 = (int)((current->x + current->xoffset_left - HIST_MIN_INPUT) * canvas_w / FLOAT_RANGE);
303 y2 = (int)(canvas_h - (current->y + current->xoffset_right * current->gradient) * canvas_h);
304 y1 = (int)(canvas_h - (current->y + current->xoffset_left * current->gradient) * canvas_h);
305 /* x2 = x + (title3_x - title2_x)/20;
306 x1 = x - (title3_x - title2_x)/20;
307 y1 = y + (int)(current->gradient * (float)(canvas_h)/20.0);
308 y2 = y - (int)(current->gradient * (float)(canvas_h)/20.0);
309 // int y2 = (int)(canvas_h - canvas_h * (current->y + current->gradient /10));*/
310 canvas->draw_line(x1,y1,x2,y2);
312 canvas->draw_circle(x1 - BOX_SIZE / 4, y1 - BOX_SIZE / 4, BOX_SIZE/2, BOX_SIZE/2);
313 canvas->draw_circle(x2 - BOX_SIZE / 4, y2 - BOX_SIZE / 4, BOX_SIZE/2, BOX_SIZE/2);
314 canvas->set_color(0x00ff00);
322 // Draw 0 and 100% lines.
323 canvas->set_color(0xff0000);
324 canvas->draw_line(title2_x - canvas->get_x(),
326 title2_x - canvas->get_x(),
328 canvas->draw_line(title3_x - canvas->get_x(),
330 title3_x - canvas->get_x(),
334 void HistogramWindow::update_canvas()
336 int *accum = plugin->accum[plugin->mode];
337 int accum_per_canvas_i = HISTOGRAM_SLOTS / canvas_w + 1;
338 float accum_per_canvas_f = (float)HISTOGRAM_SLOTS / canvas_w;
342 for(int i = 0; i < HISTOGRAM_SLOTS; i++)
344 if(accum && accum[i] > normalize) normalize = accum[i];
350 for(int i = 0; i < canvas_w; i++)
352 int accum_start = (int)(accum_per_canvas_f * i);
353 int accum_end = accum_start + accum_per_canvas_i;
355 for(int j = accum_start; j < accum_end; j++)
357 max = MAX(accum[j], max);
360 // max = max * canvas_h / normalize;
361 max = (int)(log(max) / log(normalize) * canvas_h);
363 canvas->set_color(0xffffff);
364 canvas->draw_line(i, 0, i, canvas_h - max);
365 canvas->set_color(0x000000);
366 canvas->draw_line(i, canvas_h - max, i, canvas_h);
371 canvas->set_color(0xffffff);
372 canvas->draw_box(0, 0, canvas_w, canvas_h);
376 draw_canvas_overlay();
387 HistogramCanvas::HistogramCanvas(HistogramMain *plugin,
388 HistogramWindow *gui,
399 this->plugin = plugin;
403 int HistogramCanvas::button_press_event()
406 if(is_event_win() && cursor_inside())
408 if(!plugin->dragging_point)
410 HistogramPoint *new_point = 0;
412 // Search for existing point under cursor
413 HistogramPoint *current = plugin->config.points[plugin->mode].first;
414 plugin->current_point = -1;
418 int x = (int)((current->x - HIST_MIN_INPUT) * gui->canvas_w / FLOAT_RANGE);
419 int y = (int)(gui->canvas_h - current->y * gui->canvas_h);
421 /* Check for click on main point */
422 if(get_cursor_x() >= x - BOX_SIZE / 2 &&
423 get_cursor_y() >= y - BOX_SIZE / 2 &&
424 get_cursor_x() < x + BOX_SIZE / 2 &&
425 get_cursor_y() < y + BOX_SIZE / 2)
427 plugin->current_point =
428 plugin->config.points[plugin->mode].number_of(current);
429 plugin->point_x_offset = get_cursor_x() - x;
430 plugin->point_y_offset = get_cursor_y() - y;
434 if (plugin->config.smoothMode == HISTOGRAM_LINEAR)
438 (int)((current->x + current->xoffset_right - HIST_MIN_INPUT) * gui->canvas_w / FLOAT_RANGE);
440 (int)(gui->canvas_h - (current->y + current->xoffset_right * current->gradient) *
443 /* Check for click on right handle */
444 if(get_cursor_x() >= xright - BOX_SIZE / 2 &&
445 get_cursor_y() >= yright - BOX_SIZE / 2 &&
446 get_cursor_x() < xright + BOX_SIZE / 2 &&
447 get_cursor_y() < yright + BOX_SIZE / 2)
449 plugin->current_point =
450 plugin->config.points[plugin->mode].number_of(current);
451 plugin->point_x_offset = get_cursor_x() - xright;
452 plugin->point_y_offset = get_cursor_y() - yright;
457 /* Check for click on left handle */
459 (int)((current->x + current->xoffset_left - HIST_MIN_INPUT) * gui->canvas_w / FLOAT_RANGE);
461 (int)(gui->canvas_h - (current->y + current->xoffset_left * current->gradient) *
463 if(get_cursor_x() >= xleft - BOX_SIZE / 2 &&
464 get_cursor_y() >= yleft - BOX_SIZE / 2 &&
465 get_cursor_x() < xleft + BOX_SIZE / 2 &&
466 get_cursor_y() < yleft + BOX_SIZE / 2)
468 plugin->current_point =
469 plugin->config.points[plugin->mode].number_of(current);
470 plugin->point_x_offset = get_cursor_x() - xleft;
471 plugin->point_y_offset = get_cursor_y() - yleft;
480 if(plugin->current_point < 0)
482 // Create new point under cursor
483 float current_x = (float)get_cursor_x() *
487 float current_y = 1.0 -
488 (float)get_cursor_y() /
491 plugin->config.points[plugin->mode].insert(current_x, current_y);
492 plugin->current_point =
493 plugin->config.points[plugin->mode].number_of(new_point);
494 plugin->point_x_offset = 0;
495 plugin->point_y_offset = 0;
498 // Get 2 points surrounding current position
499 float x1 = 0, x2 = 1, y1 = 0, y2 = 1;
501 HistogramPoint *current = plugin->config.points[plugin->mode].first;
503 while(current && !done)
505 if(current->x > current_x)
515 current = plugin->config.points[plugin->mode].last;
517 while(current && !done)
519 if(current->x <= current_x)
528 new_point->gradient = (y2 - y1) / (x2 - x1);
534 plugin->dragging_point = dragID;
537 plugin->config.boundaries();
539 gui->update_canvas();
542 plugin->send_configure_change();
549 int HistogramCanvas::cursor_motion_event()
551 if(plugin->dragging_point)
553 HistogramPoint * current_point = plugin->config.points[plugin->mode].get_item_number(plugin->current_point);
556 (float)(get_cursor_x() - plugin->point_x_offset) *
560 float current_y = 1.0 -
561 (float)(get_cursor_y() - plugin->point_y_offset) /
564 switch(plugin->dragging_point)
566 case 1: /* Main point dragged */
567 current_point->x = current_x;
568 current_point->y = current_y;
570 case 2: /* Right control point dragged */
571 if (current_x - current_point->x > 0)
573 current_point->xoffset_right = current_x - current_point->x;
574 current_point->gradient = (current_y - current_point->y) / (current_x - current_point->x);
577 case 3: /* Left control point dragged */
578 if (current_x - current_point->x < 0)
580 current_point->xoffset_left = current_x - current_point->x;
581 current_point->gradient = (current_point->y - current_y) / (current_point->x - current_x);
586 plugin->config.boundaries();
588 gui->update_canvas();
589 plugin->send_configure_change();
595 int HistogramCanvas::button_release_event()
597 if(plugin->dragging_point)
599 // Test for out of order points to delete.
600 HistogramPoint *current =
601 plugin->config.points[plugin->mode].get_item_number(plugin->current_point);
602 HistogramPoint *prev = PREVIOUS;
603 HistogramPoint *next = NEXT;
605 if((prev && prev->x >= current->x) ||
606 (next && next->x <= current->x))
609 plugin->current_point = -1;
610 plugin->config.boundaries();
612 gui->update_canvas();
613 plugin->send_configure_change();
616 plugin->dragging_point = 0;
627 HistogramReset::HistogramReset(HistogramMain *plugin,
630 : BC_GenericButton(x, y, _("Reset"))
632 this->plugin = plugin;
634 int HistogramReset::handle_event()
636 plugin->config.reset(0);
637 HistogramWindow *window = (HistogramWindow *)plugin->thread->window;
639 window->update_canvas();
640 plugin->send_configure_change();
652 HistogramSlider::HistogramSlider(HistogramMain *plugin,
653 HistogramWindow *gui,
659 : BC_SubWindow(x, y, w, h)
661 this->plugin = plugin;
663 this->is_input = is_input;
667 int HistogramSlider::input_to_pixel(float input)
669 return (int)((input - HIST_MIN_INPUT) / FLOAT_RANGE * get_w());
672 int HistogramSlider::button_press_event()
674 if(is_event_win() && cursor_inside())
678 int half_h = get_h() / 2;
682 if(operation == NONE)
684 int x1 = input_to_pixel(plugin->config.output_min[plugin->mode]) -
685 gui->mid_picon->get_w() / 2;
686 int x2 = x1 + gui->mid_picon->get_w();
687 if(get_cursor_x() >= x1 && get_cursor_x() < x2 &&
688 get_cursor_y() >= half_h && get_cursor_y() < h)
690 operation = DRAG_MIN_OUTPUT;
694 if(operation == NONE)
696 int x1 = input_to_pixel(plugin->config.output_max[plugin->mode]) -
697 gui->mid_picon->get_w() / 2;
698 int x2 = x1 + gui->mid_picon->get_w();
699 if(get_cursor_x() >= x1 && get_cursor_x() < x2 &&
700 get_cursor_y() >= half_h && get_cursor_y() < h)
702 operation = DRAG_MAX_OUTPUT;
710 int HistogramSlider::button_release_event()
712 if(operation != NONE)
720 int HistogramSlider::cursor_motion_event()
722 if(operation != NONE)
724 float value = (float)get_cursor_x() / get_w() * FLOAT_RANGE + HIST_MIN_INPUT;
725 CLAMP(value, HIST_MIN_INPUT, HIST_MAX_INPUT);
729 case DRAG_MIN_OUTPUT:
730 value = MIN(plugin->config.output_max[plugin->mode], value);
731 plugin->config.output_min[plugin->mode] = value;
733 case DRAG_MAX_OUTPUT:
734 value = MAX(plugin->config.output_min[plugin->mode], value);
735 plugin->config.output_max[plugin->mode] = value;
739 plugin->config.boundaries();
740 gui->update_output();
742 plugin->send_configure_change();
748 void HistogramSlider::update()
752 int half_h = get_h() / 2;
753 // int quarter_h = get_h() / 4;
754 int mode = plugin->mode;
759 clear_box(0, 0, w, h);
766 case HISTOGRAM_GREEN:
774 for(int i = 0; i < w; i++)
776 int color = (int)(i * 0xff / w);
777 set_color(((r * color / 0xff) << 16) |
778 ((g * color / 0xff) << 8) |
781 draw_line(i, 0, i, half_h);
786 min = plugin->config.output_min[plugin->mode];
787 max = plugin->config.output_max[plugin->mode];
789 draw_pixmap(gui->min_picon,
790 input_to_pixel(min) - gui->min_picon->get_w() / 2,
792 draw_pixmap(gui->max_picon,
793 input_to_pixel(max) - gui->max_picon->get_w() / 2,
808 HistogramAuto::HistogramAuto(HistogramMain *plugin,
811 : BC_CheckBox(x, y, plugin->config.automatic, _("Automatic"))
813 this->plugin = plugin;
816 int HistogramAuto::handle_event()
818 plugin->config.automatic = get_value();
819 plugin->send_configure_change();
826 HistogramSplit::HistogramSplit(HistogramMain *plugin,
829 : BC_CheckBox(x, y, plugin->config.split, _("Split picture"))
831 this->plugin = plugin;
834 int HistogramSplit::handle_event()
836 plugin->config.split = get_value();
837 plugin->send_configure_change();
844 HistogramMode::HistogramMode(HistogramMain *plugin,
849 : BC_Radial(x, y, plugin->mode == value, text)
851 this->plugin = plugin;
854 int HistogramMode::handle_event()
856 plugin->mode = value;
857 plugin->current_point= -1;
858 HistogramWindow *window = (HistogramWindow *)plugin->thread->window;
859 window->update_canvas();
860 window->update_mode();
861 window->update_input();
862 window->update_canvas();
863 window->update_output();
864 window->output->update();
865 // plugin->send_configure_change();
877 HistogramOutputText::HistogramOutputText(HistogramMain *plugin,
878 HistogramWindow *gui,
882 : BC_TumbleTextBox(gui,
883 output ? (float)*output : 0.0,
884 (float)HIST_MIN_INPUT,
885 (float)HIST_MAX_INPUT,
890 this->plugin = plugin;
891 this->output = output;
892 set_precision(DIGITS);
893 set_increment(PRECISION);
897 int HistogramOutputText::handle_event()
901 *output = atof(get_text());
904 HistogramWindow *window = (HistogramWindow *)plugin->thread->window;
905 window->output->update();
906 plugin->send_configure_change();
917 HistogramInputText::HistogramInputText(HistogramMain *plugin,
918 HistogramWindow *gui,
922 : BC_TumbleTextBox(gui,
924 (float)HIST_MIN_INPUT,
925 (float)HIST_MAX_INPUT,
931 this->plugin = plugin;
933 set_precision(DIGITS);
934 set_increment(PRECISION);
938 int HistogramInputText::handle_event()
940 if(plugin->current_point >= 0 &&
941 plugin->current_point < plugin->config.points[plugin->mode].total())
943 HistogramPoint *point =
944 plugin->config.points[plugin->mode].get_item_number(
945 plugin->current_point);
950 point->x = atof(get_text());
952 point->y = atof(get_text());
954 plugin->config.boundaries();
955 gui->update_canvas();
957 HistogramWindow *window = (HistogramWindow *)plugin->thread->window;
958 window->output->update();
959 plugin->send_configure_change();
965 void HistogramInputText::update()
967 if(plugin->current_point >= 0 &&
968 plugin->current_point < plugin->config.points[plugin->mode].total())
970 HistogramPoint *point =
972 plugin->config.points[plugin->mode].get_item_number(
973 plugin->current_point);
978 BC_TumbleTextBox::update(point->x);
980 BC_TumbleTextBox::update(point->y);
984 BC_TumbleTextBox::update((float)0.0);
989 BC_TumbleTextBox::update((float)0.0);
995 HistogramSmoothMode::HistogramSmoothMode(HistogramMain*plugin,
996 HistogramWindow *gui,
999 : BC_PopupMenu(x, y, 120, to_text(plugin->config.smoothMode), 1)
1001 this->plugin = plugin;
1004 void HistogramSmoothMode::create_objects()
1006 add_item(new BC_MenuItem(to_text(HISTOGRAM_LINEAR)));
1007 add_item(new BC_MenuItem(to_text(HISTOGRAM_POLYNOMINAL)));
1008 add_item(new BC_MenuItem(to_text(HISTOGRAM_BEZIER)));
1011 char* HistogramSmoothMode::to_text(int mode)
1015 case HISTOGRAM_LINEAR:
1017 case HISTOGRAM_POLYNOMINAL:
1018 return _("Polynominal");
1019 case HISTOGRAM_BEZIER:
1025 int HistogramSmoothMode::from_text(char *text)
1027 if(!strcmp(text, to_text(HISTOGRAM_LINEAR)))
1028 return HISTOGRAM_LINEAR;
1029 if(!strcmp(text, to_text(HISTOGRAM_POLYNOMINAL)))
1030 return HISTOGRAM_POLYNOMINAL;
1031 if(!strcmp(text, to_text(HISTOGRAM_BEZIER)))
1032 return HISTOGRAM_BEZIER;
1033 return HISTOGRAM_LINEAR;
1036 int HistogramSmoothMode::handle_event()
1038 plugin->config.smoothMode = from_text(get_text());
1039 gui->update_canvas();
1040 plugin->send_configure_change();