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 "histogramengine.h"
25 #include "threshold.h"
26 #include "thresholdwindow.h"
34 ThresholdMin::ThresholdMin(ThresholdMain *plugin,
39 : BC_TumbleTextBox(gui,
47 this->plugin = plugin;
52 int ThresholdMin::handle_event()
54 plugin->config.min = atof(get_text());
56 plugin->send_configure_change();
68 ThresholdMax::ThresholdMax(ThresholdMain *plugin,
73 : BC_TumbleTextBox(gui,
81 this->plugin = plugin;
85 int ThresholdMax::handle_event()
87 plugin->config.max = atof(get_text());
89 plugin->send_configure_change();
99 ThresholdPlot::ThresholdPlot(ThresholdMain *plugin,
102 : BC_CheckBox(x, y, plugin->config.plot, _("Plot histogram"))
104 this->plugin = plugin;
107 int ThresholdPlot::handle_event()
109 plugin->config.plot = get_value();
111 plugin->send_configure_change();
120 ThresholdCanvas::ThresholdCanvas(ThresholdMain *plugin,
121 ThresholdWindow *gui,
126 : BC_SubWindow(x, y, w, h)
128 this->plugin = plugin;
130 state = NO_OPERATION;
133 int ThresholdCanvas::button_press_event()
135 if(is_event_win() && cursor_inside())
138 state = DRAG_SELECTION;
141 x1 = (int)((plugin->config.min - HISTOGRAM_MIN) /
142 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
144 x2 = (int)((plugin->config.max - HISTOGRAM_MIN) /
145 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
147 center_x = (x2 + x1) / 2;
148 if(abs(get_cursor_x() - x1) < abs(get_cursor_x() - x2))
161 x1 = x2 = center_x = get_cursor_x();
164 plugin->config.min = x1 *
165 (HISTOGRAM_MAX - HISTOGRAM_MIN) /
168 plugin->config.max = x2 *
169 (HISTOGRAM_MAX - HISTOGRAM_MIN) /
179 int ThresholdCanvas::button_release_event()
181 if(state == DRAG_SELECTION)
183 state = NO_OPERATION;
189 int ThresholdCanvas::cursor_motion_event()
191 if(state == DRAG_SELECTION)
193 if(get_cursor_x() > center_x)
204 plugin->config.min = x1 *
205 (HISTOGRAM_MAX - HISTOGRAM_MIN) /
209 plugin->config.max = x2 *
210 (HISTOGRAM_MAX - HISTOGRAM_MIN) /
214 gui->min->update(plugin->config.min);
215 gui->max->update(plugin->config.max);
217 plugin->send_configure_change();
223 void ThresholdCanvas::draw()
227 draw_box(0, 0, get_w(), get_h());
228 int border_x1 = (int)((0 - HISTOGRAM_MIN) /
229 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
231 int border_x2 = (int)((1.0 - HISTOGRAM_MIN) /
232 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
235 int x1 = (int)((plugin->config.min - HISTOGRAM_MIN) /
236 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
238 int x2 = (int)((plugin->config.max - HISTOGRAM_MIN) /
239 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
244 int64_t *array = plugin->engine->accum[HISTOGRAM_VALUE];
247 // Get normalizing factor
248 for(int i = 0; i < get_w(); i++)
250 int division1 = i * HISTOGRAM_RANGE / get_w();
251 int division2 = (i + 1) * HISTOGRAM_RANGE / get_w();
253 for(int j = division1; j < division2; j++)
257 if(total > max) max = total;
260 for(int i = 0; i < get_w(); i++)
262 int division1 = i * HISTOGRAM_RANGE / get_w();
263 int division2 = (i + 1) * HISTOGRAM_RANGE / get_w();
265 for(int j = division1; j < division2; j++)
272 pixels = total * get_h() / max;
275 if(i >= x1 && i < x2)
278 draw_line(i, 0, i, get_h() - pixels);
280 draw_line(i, get_h(), i, get_h() - pixels);
285 draw_line(i, get_h(), i, get_h() - pixels);
294 draw_box(x1, 0, x2 - x1, get_h());
298 draw_line(border_x1, 0, border_x1, get_h());
299 draw_line(border_x2, 0, border_x2, get_h());
309 ThresholdLowColorButton::ThresholdLowColorButton(ThresholdMain *plugin, ThresholdWindow *window, int x, int y)
310 : BC_GenericButton(x, y, _("Low Color"))
312 this->plugin = plugin;
313 this->window = window;
316 int ThresholdLowColorButton::handle_event()
318 RGBA & color = plugin->config.low_color;
319 window->low_color_thread->start_window(
329 ThresholdMidColorButton::ThresholdMidColorButton(ThresholdMain *plugin, ThresholdWindow *window, int x, int y)
330 : BC_GenericButton(x, y, _("Mid Color"))
332 this->plugin = plugin;
333 this->window = window;
336 int ThresholdMidColorButton::handle_event()
338 RGBA & color = plugin->config.mid_color;
339 window->mid_color_thread->start_window(
349 ThresholdHighColorButton::ThresholdHighColorButton(ThresholdMain *plugin, ThresholdWindow *window, int x, int y)
350 : BC_GenericButton(x, y, _("High Color"))
352 this->plugin = plugin;
353 this->window = window;
356 int ThresholdHighColorButton::handle_event()
358 RGBA & color = plugin->config.high_color;
359 window->high_color_thread->start_window(
369 ThresholdLowColorThread::ThresholdLowColorThread(ThresholdMain *plugin, ThresholdWindow *window)
370 : ColorPicker(1, _("Low color"))
372 this->plugin = plugin;
373 this->window = window;
376 int ThresholdLowColorThread::handle_new_color(int output, int alpha)
378 plugin->config.low_color.set(output, alpha);
379 window->update_low_color();
381 plugin->send_configure_change();
389 ThresholdMidColorThread::ThresholdMidColorThread(ThresholdMain *plugin, ThresholdWindow *window)
390 : ColorPicker(1, _("Mid color"))
392 this->plugin = plugin;
393 this->window = window;
396 int ThresholdMidColorThread::handle_new_color(int output, int alpha)
398 plugin->config.mid_color.set(output, alpha);
399 window->update_mid_color();
401 plugin->send_configure_change();
409 ThresholdHighColorThread::ThresholdHighColorThread(ThresholdMain *plugin, ThresholdWindow *window)
410 : ColorPicker(1, _("High color"))
412 this->plugin = plugin;
413 this->window = window;
416 int ThresholdHighColorThread::handle_new_color(int output, int alpha)
418 plugin->config.high_color.set(output, alpha);
419 window->update_high_color();
421 plugin->send_configure_change();
430 ThresholdWindow::ThresholdWindow(ThresholdMain *plugin)
431 : PluginClientWindow(plugin, 450, 450, 450, 450, 1)
433 this->plugin = plugin;
440 this->high_color = 0;
441 this->low_color_thread = 0;
442 this->mid_color_thread = 0;
443 this->high_color_thread = 0;
446 ThresholdWindow::~ThresholdWindow()
450 void ThresholdWindow::create_objects()
454 add_subwindow(canvas = new ThresholdCanvas(plugin,
461 y += canvas->get_h() + 10;
463 add_subwindow(plot = new ThresholdPlot(plugin, x, y));
464 y += plot->get_h() + 10;
467 add_subwindow(low_color = new ThresholdLowColorButton(plugin, this, x, y));
468 low_color_x = x + 10;
469 low_color_y = y + low_color->get_h() + 10;
470 x += low_color->get_w() + 10;
472 add_subwindow(mid_color = new ThresholdMidColorButton(plugin, this, x, y));
473 mid_color_x = x + 10;
474 mid_color_y = y + mid_color->get_h() + 10;
475 x += mid_color->get_w() + 10;
477 add_subwindow(high_color = new ThresholdHighColorButton(plugin, this, x, y));
478 high_color_x = x + 10;
479 high_color_y = y + high_color->get_h() + 10;
481 y += low_color->get_h() + COLOR_H + 10 + 10;
484 BC_Title * min_title;
485 add_subwindow(min_title = new BC_Title(x, y, _("Min:")));
486 x += min_title->get_w() + 10;
487 min = new ThresholdMin(plugin,
492 min->create_objects();
493 min->set_increment(0.1);
496 x = mid_color->get_x() + mid_color->get_w() / 2;
497 BC_Title * max_title;
498 add_subwindow(max_title = new BC_Title(x, y, _("Max:")));
499 x += max_title->get_w() + 10;
500 max = new ThresholdMax(plugin,
505 max->create_objects();
506 max->set_increment(0.1);
509 low_color_thread = new ThresholdLowColorThread(plugin, this);
510 mid_color_thread = new ThresholdMidColorThread(plugin, this);
511 high_color_thread = new ThresholdHighColorThread(plugin, this);
519 void ThresholdWindow::update_low_color()
521 set_color(plugin->config.low_color.getRGB());
522 draw_box(low_color_x, low_color_y, COLOR_W, COLOR_H);
523 flash(low_color_x, low_color_y, COLOR_W, COLOR_H);
526 void ThresholdWindow::update_mid_color()
528 set_color(plugin->config.mid_color.getRGB());
529 draw_box(mid_color_x, mid_color_y, COLOR_W, COLOR_H);
530 flash(mid_color_x, mid_color_y, COLOR_W, COLOR_H);
533 void ThresholdWindow::update_high_color()
535 set_color(plugin->config.high_color.getRGB());
536 draw_box(high_color_x, high_color_y, COLOR_W, COLOR_H);
537 flash(high_color_x, high_color_y, COLOR_W, COLOR_H);
540 void ThresholdWindow::done_event(int result)
542 low_color_thread->close_window();
543 mid_color_thread->close_window();
544 high_color_thread->close_window();