00001 #include "bcdisplayinfo.h"
00002 #include "histogramengine.h"
00003 #include "language.h"
00004 #include "threshold.h"
00005 #include "thresholdwindow.h"
00006
00007 #define COLOR_W 100
00008 #define COLOR_H 30
00009
00010
00011
00012
00013 ThresholdMin::ThresholdMin(ThresholdMain *plugin,
00014 ThresholdWindow *gui,
00015 int x,
00016 int y,
00017 int w)
00018 : BC_TumbleTextBox(gui,
00019 plugin->config.min,
00020 HISTOGRAM_MIN,
00021 HISTOGRAM_MAX,
00022 x,
00023 y,
00024 w)
00025 {
00026 this->plugin = plugin;
00027 this->gui = gui;
00028 }
00029
00030
00031 int ThresholdMin::handle_event()
00032 {
00033 plugin->config.min = atof(get_text());
00034 gui->canvas->draw();
00035 plugin->send_configure_change();
00036 return 1;
00037 }
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 ThresholdMax::ThresholdMax(ThresholdMain *plugin,
00048 ThresholdWindow *gui,
00049 int x,
00050 int y,
00051 int w)
00052 : BC_TumbleTextBox(gui,
00053 plugin->config.max,
00054 HISTOGRAM_MIN,
00055 HISTOGRAM_MAX,
00056 x,
00057 y,
00058 w)
00059 {
00060 this->plugin = plugin;
00061 this->gui = gui;
00062 }
00063
00064 int ThresholdMax::handle_event()
00065 {
00066 plugin->config.max = atof(get_text());
00067 gui->canvas->draw();
00068 plugin->send_configure_change();
00069 return 1;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078 ThresholdPlot::ThresholdPlot(ThresholdMain *plugin,
00079 int x,
00080 int y)
00081 : BC_CheckBox(x, y, plugin->config.plot, _("Plot histogram"))
00082 {
00083 this->plugin = plugin;
00084 }
00085
00086 int ThresholdPlot::handle_event()
00087 {
00088 plugin->config.plot = get_value();
00089
00090 plugin->send_configure_change();
00091 return 1;
00092 }
00093
00094
00095
00096
00097
00098
00099 ThresholdCanvas::ThresholdCanvas(ThresholdMain *plugin,
00100 ThresholdWindow *gui,
00101 int x,
00102 int y,
00103 int w,
00104 int h)
00105 : BC_SubWindow(x, y, w, h)
00106 {
00107 this->plugin = plugin;
00108 this->gui = gui;
00109 state = NO_OPERATION;
00110 }
00111
00112 int ThresholdCanvas::button_press_event()
00113 {
00114 if(is_event_win() && cursor_inside())
00115 {
00116 activate();
00117 state = DRAG_SELECTION;
00118 if(shift_down())
00119 {
00120 x1 = (int)((plugin->config.min - HISTOGRAM_MIN) /
00121 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
00122 get_w());
00123 x2 = (int)((plugin->config.max - HISTOGRAM_MIN) /
00124 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
00125 get_w());
00126 center_x = (x2 + x1) / 2;
00127 if(abs(get_cursor_x() - x1) < abs(get_cursor_x() - x2))
00128 {
00129 x1 = get_cursor_x();
00130 center_x = x2;
00131 }
00132 else
00133 {
00134 x2 = get_cursor_x();
00135 center_x = x1;
00136 }
00137 }
00138 else
00139 {
00140 x1 = x2 = center_x = get_cursor_x();
00141 }
00142
00143 plugin->config.min = x1 *
00144 (HISTOGRAM_MAX - HISTOGRAM_MIN) /
00145 get_w() +
00146 HISTOGRAM_MIN;
00147 plugin->config.max = x2 *
00148 (HISTOGRAM_MAX - HISTOGRAM_MIN) /
00149 get_w() +
00150 HISTOGRAM_MIN;
00151
00152 draw();
00153 return 1;
00154 }
00155 return 0;
00156 }
00157
00158 int ThresholdCanvas::button_release_event()
00159 {
00160 if(state == DRAG_SELECTION)
00161 {
00162 state = NO_OPERATION;
00163 return 1;
00164 }
00165 return 0;
00166 }
00167
00168 int ThresholdCanvas::cursor_motion_event()
00169 {
00170 if(state == DRAG_SELECTION)
00171 {
00172 if(get_cursor_x() > center_x)
00173 {
00174 x1 = center_x;
00175 x2 = get_cursor_x();
00176 }
00177 else
00178 {
00179 x1 = get_cursor_x();
00180 x2 = center_x;
00181 }
00182
00183 plugin->config.min = x1 *
00184 (HISTOGRAM_MAX - HISTOGRAM_MIN) /
00185 get_w() +
00186 HISTOGRAM_MIN;
00187
00188 plugin->config.max = x2 *
00189 (HISTOGRAM_MAX - HISTOGRAM_MIN) /
00190 get_w() +
00191 HISTOGRAM_MIN;
00192
00193 gui->min->update(plugin->config.min);
00194 gui->max->update(plugin->config.max);
00195 draw();
00196 plugin->send_configure_change();
00197 return 1;
00198 }
00199 return 0;
00200 }
00201
00202 void ThresholdCanvas::draw()
00203 {
00204 int max = 0;
00205 set_color(WHITE);
00206 draw_box(0, 0, get_w(), get_h());
00207 int border_x1 = (int)((0 - HISTOGRAM_MIN) /
00208 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
00209 get_w());
00210 int border_x2 = (int)((1.0 - HISTOGRAM_MIN) /
00211 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
00212 get_w());
00213
00214 int x1 = (int)((plugin->config.min - HISTOGRAM_MIN) /
00215 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
00216 get_w());
00217 int x2 = (int)((plugin->config.max - HISTOGRAM_MIN) /
00218 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
00219 get_w());
00220
00221 if(plugin->engine)
00222 {
00223 int64_t *array = plugin->engine->accum[HISTOGRAM_VALUE];
00224
00225
00226
00227 for(int i = 0; i < get_w(); i++)
00228 {
00229 int division1 = i * HISTOGRAM_RANGE / get_w();
00230 int division2 = (i + 1) * HISTOGRAM_RANGE / get_w();
00231 int total = 0;
00232 for(int j = division1; j < division2; j++)
00233 {
00234 total += array[j];
00235 }
00236 if(total > max) max = total;
00237 }
00238
00239 for(int i = 0; i < get_w(); i++)
00240 {
00241 int division1 = i * HISTOGRAM_RANGE / get_w();
00242 int division2 = (i + 1) * HISTOGRAM_RANGE / get_w();
00243 int total = 0;
00244 for(int j = division1; j < division2; j++)
00245 {
00246 total += array[j];
00247 }
00248
00249 int pixels;
00250 if(max)
00251 pixels = total * get_h() / max;
00252 else
00253 pixels = 0;
00254 if(i >= x1 && i < x2)
00255 {
00256 set_color(BLUE);
00257 draw_line(i, 0, i, get_h() - pixels);
00258 set_color(WHITE);
00259 draw_line(i, get_h(), i, get_h() - pixels);
00260 }
00261 else
00262 {
00263 set_color(BLACK);
00264 draw_line(i, get_h(), i, get_h() - pixels);
00265 }
00266 }
00267 }
00268 else
00269 {
00270
00271
00272 set_color(BLUE);
00273 draw_box(x1, 0, x2 - x1, get_h());
00274 }
00275
00276 set_color(RED);
00277 draw_line(border_x1, 0, border_x1, get_h());
00278 draw_line(border_x2, 0, border_x2, get_h());
00279
00280 flash(1);
00281 }
00282
00283
00284
00285
00286
00287
00288 ThresholdLowColorButton::ThresholdLowColorButton(ThresholdMain *plugin, ThresholdWindow *window, int x, int y)
00289 : BC_GenericButton(x, y, _("Low Color"))
00290 {
00291 this->plugin = plugin;
00292 this->window = window;
00293 }
00294
00295 int ThresholdLowColorButton::handle_event()
00296 {
00297 RGBA & color = plugin->config.low_color;
00298 window->low_color_thread->start_window(
00299 color.getRGB(),
00300 color.a);
00301 return 1;
00302 }
00303
00304
00305
00306
00307
00308 ThresholdMidColorButton::ThresholdMidColorButton(ThresholdMain *plugin, ThresholdWindow *window, int x, int y)
00309 : BC_GenericButton(x, y, _("Mid Color"))
00310 {
00311 this->plugin = plugin;
00312 this->window = window;
00313 }
00314
00315 int ThresholdMidColorButton::handle_event()
00316 {
00317 RGBA & color = plugin->config.mid_color;
00318 window->mid_color_thread->start_window(
00319 color.getRGB(),
00320 color.a);
00321 return 1;
00322 }
00323
00324
00325
00326
00327
00328 ThresholdHighColorButton::ThresholdHighColorButton(ThresholdMain *plugin, ThresholdWindow *window, int x, int y)
00329 : BC_GenericButton(x, y, _("High Color"))
00330 {
00331 this->plugin = plugin;
00332 this->window = window;
00333 }
00334
00335 int ThresholdHighColorButton::handle_event()
00336 {
00337 RGBA & color = plugin->config.high_color;
00338 window->high_color_thread->start_window(
00339 color.getRGB(),
00340 color.a);
00341 return 1;
00342 }
00343
00344
00345
00346
00347
00348 ThresholdLowColorThread::ThresholdLowColorThread(ThresholdMain *plugin, ThresholdWindow *window)
00349 : ColorThread(1, _("Low color"))
00350 {
00351 this->plugin = plugin;
00352 this->window = window;
00353 }
00354
00355 int ThresholdLowColorThread::handle_new_color(int output, int alpha)
00356 {
00357 plugin->config.low_color.set(output, alpha);
00358 window->update_low_color();
00359 window->flush();
00360 plugin->send_configure_change();
00361 }
00362
00363
00364
00365
00366
00367 ThresholdMidColorThread::ThresholdMidColorThread(ThresholdMain *plugin, ThresholdWindow *window)
00368 : ColorThread(1, _("Mid color"))
00369 {
00370 this->plugin = plugin;
00371 this->window = window;
00372 }
00373
00374 int ThresholdMidColorThread::handle_new_color(int output, int alpha)
00375 {
00376 plugin->config.mid_color.set(output, alpha);
00377 window->update_mid_color();
00378 window->flush();
00379 plugin->send_configure_change();
00380 }
00381
00382
00383
00384
00385
00386 ThresholdHighColorThread::ThresholdHighColorThread(ThresholdMain *plugin, ThresholdWindow *window)
00387 : ColorThread(1, _("High color"))
00388 {
00389 this->plugin = plugin;
00390 this->window = window;
00391 }
00392
00393 int ThresholdHighColorThread::handle_new_color(int output, int alpha)
00394 {
00395 plugin->config.high_color.set(output, alpha);
00396 window->update_high_color();
00397 window->flush();
00398 plugin->send_configure_change();
00399 }
00400
00401
00402
00403
00404
00405
00406 ThresholdWindow::ThresholdWindow(ThresholdMain *plugin, int x, int y)
00407 : BC_Window(plugin->gui_string, x, y, 450, 450, 450, 450, 0, 1)
00408 {
00409 this->plugin = plugin;
00410 this->min = 0;
00411 this->max = 0;
00412 this->canvas = 0;
00413 this->plot = 0;
00414 this->low_color = 0;
00415 this->mid_color = 0;
00416 this->high_color = 0;
00417 this->low_color_thread = 0;
00418 this->mid_color_thread = 0;
00419 this->high_color_thread = 0;
00420 }
00421
00422 ThresholdWindow::~ThresholdWindow()
00423 {
00424 }
00425
00426 int ThresholdWindow::create_objects()
00427 {
00428 int x = 10;
00429 int y = 10;
00430 add_subwindow(canvas = new ThresholdCanvas(plugin,
00431 this,
00432 x,
00433 y,
00434 get_w() - x - 10,
00435 get_h() - 160));
00436 canvas->draw();
00437 y += canvas->get_h() + 10;
00438
00439 add_subwindow(plot = new ThresholdPlot(plugin, x, y));
00440 y += plot->get_h() + 10;
00441
00442
00443 add_subwindow(low_color = new ThresholdLowColorButton(plugin, this, x, y));
00444 low_color_x = x + 10;
00445 low_color_y = y + low_color->get_h() + 10;
00446 x += low_color->get_w() + 10;
00447
00448 add_subwindow(mid_color = new ThresholdMidColorButton(plugin, this, x, y));
00449 mid_color_x = x + 10;
00450 mid_color_y = y + mid_color->get_h() + 10;
00451 x += mid_color->get_w() + 10;
00452
00453 add_subwindow(high_color = new ThresholdHighColorButton(plugin, this, x, y));
00454 high_color_x = x + 10;
00455 high_color_y = y + high_color->get_h() + 10;
00456
00457 y += low_color->get_h() + COLOR_H + 10 + 10;
00458
00459 x = 30;
00460 BC_Title * min_title;
00461 add_subwindow(min_title = new BC_Title(x, y, _("Min:")));
00462 x += min_title->get_w() + 10;
00463 min = new ThresholdMin(plugin,
00464 this,
00465 x,
00466 y,
00467 100);
00468 min->create_objects();
00469 min->set_increment(0.1);
00470
00471
00472 x = mid_color->get_x() + mid_color->get_w() / 2;
00473 BC_Title * max_title;
00474 add_subwindow(max_title = new BC_Title(x, y, _("Max:")));
00475 x += max_title->get_w() + 10;
00476 max = new ThresholdMax(plugin,
00477 this,
00478 x,
00479 y,
00480 100);
00481 max->create_objects();
00482 max->set_increment(0.1);
00483
00484
00485 low_color_thread = new ThresholdLowColorThread(plugin, this);
00486 mid_color_thread = new ThresholdMidColorThread(plugin, this);
00487 high_color_thread = new ThresholdHighColorThread(plugin, this);
00488 update_low_color();
00489 update_mid_color();
00490 update_high_color();
00491
00492 show_window(1);
00493 }
00494
00495 void ThresholdWindow::update_low_color()
00496 {
00497 set_color(plugin->config.low_color.getRGB());
00498 draw_box(low_color_x, low_color_y, COLOR_W, COLOR_H);
00499 flash(low_color_x, low_color_y, COLOR_W, COLOR_H);
00500 }
00501
00502 void ThresholdWindow::update_mid_color()
00503 {
00504 set_color(plugin->config.mid_color.getRGB());
00505 draw_box(mid_color_x, mid_color_y, COLOR_W, COLOR_H);
00506 flash(mid_color_x, mid_color_y, COLOR_W, COLOR_H);
00507 }
00508
00509 void ThresholdWindow::update_high_color()
00510 {
00511 set_color(plugin->config.high_color.getRGB());
00512 draw_box(high_color_x, high_color_y, COLOR_W, COLOR_H);
00513 flash(high_color_x, high_color_y, COLOR_W, COLOR_H);
00514 }
00515
00516
00517
00518 WINDOW_CLOSE_EVENT(ThresholdWindow)
00519
00520
00521
00522
00523 PLUGIN_THREAD_OBJECT(ThresholdMain, ThresholdThread, ThresholdWindow)
00524
00525