Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / plugins / threshold / thresholdwindow.C
blobc6a3c82f6f9447b83648d1e341bdb8ff1d77b79a
1 #include "bcdisplayinfo.h"
2 #include "histogramengine.h"
3 #include "language.h"
4 #include "threshold.h"
5 #include "thresholdwindow.h"
11 ThresholdMin::ThresholdMin(ThresholdMain *plugin,
12         ThresholdWindow *gui,
13         int x,
14         int y,
15         int w)
16  : BC_TumbleTextBox(gui, 
17         plugin->config.min,
18         HISTOGRAM_MIN,
19         HISTOGRAM_MAX,
20         x, 
21         y, 
22         w)
24         this->plugin = plugin;
25         this->gui = gui;
29 int ThresholdMin::handle_event()
31         plugin->config.min = atof(get_text());
32         gui->canvas->draw();
33         plugin->send_configure_change();
34         return 1;
45 ThresholdMax::ThresholdMax(ThresholdMain *plugin,
46         ThresholdWindow *gui,
47         int x,
48         int y,
49         int w)
50  : BC_TumbleTextBox(gui, 
51         plugin->config.max,
52         HISTOGRAM_MIN,
53         HISTOGRAM_MAX,
54         x, 
55         y, 
56         w)
58         this->plugin = plugin;
59         this->gui = gui;
62 int ThresholdMax::handle_event()
64         plugin->config.max = atof(get_text());
65         gui->canvas->draw();
66         plugin->send_configure_change();
67         return 1;
76 ThresholdPlot::ThresholdPlot(ThresholdMain *plugin,
77         int x,
78         int y)
79  : BC_CheckBox(x, y, plugin->config.plot, _("Plot histogram"))
81         this->plugin = plugin;
84 int ThresholdPlot::handle_event()
86         plugin->config.plot = get_value();
87         
88         plugin->send_configure_change();
89         return 1;
97 ThresholdCanvas::ThresholdCanvas(ThresholdMain *plugin,
98         ThresholdWindow *gui,
99         int x,
100         int y,
101         int w,
102         int h)
103  : BC_SubWindow(x, y, w, h)
105         this->plugin = plugin;
106         this->gui = gui;
107         state = NO_OPERATION;
110 int ThresholdCanvas::button_press_event()
112         if(is_event_win() && cursor_inside())
113         {
114                 activate();
115                 state = DRAG_SELECTION;
116                 if(shift_down())
117                 {
118                         x1 = (int)((plugin->config.min - HISTOGRAM_MIN) / 
119                                 (HISTOGRAM_MAX - HISTOGRAM_MIN) * 
120                                 get_w());
121                         x2 = (int)((plugin->config.max - HISTOGRAM_MIN) /
122                                 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
123                                 get_w());
124                         center_x = (x2 + x1) / 2;
125                         if(abs(get_cursor_x() - x1) < abs(get_cursor_x() - x2))
126                         {
127                                 x1 = get_cursor_x();
128                                 center_x = x2;
129                         }
130                         else
131                         {
132                                 x2 = get_cursor_x();
133                                 center_x = x1;
134                         }
135                 }
136                 else
137                 {
138                         x1 = x2 = center_x = get_cursor_x();
139                 }
141                 plugin->config.min = x1 * 
142                         (HISTOGRAM_MAX - HISTOGRAM_MIN) / 
143                         get_w() + 
144                         HISTOGRAM_MIN;
145                 plugin->config.max = x2 * 
146                         (HISTOGRAM_MAX - HISTOGRAM_MIN) / 
147                         get_w() + 
148                         HISTOGRAM_MIN;
150                 draw();
151                 return 1;
152         }
153         return 0;
156 int ThresholdCanvas::button_release_event()
158         if(state == DRAG_SELECTION)
159         {
160                 state = NO_OPERATION;
161                 return 1;
162         }
163         return 0;
166 int ThresholdCanvas::cursor_motion_event()
168         if(state == DRAG_SELECTION)
169         {
170                 if(get_cursor_x() > center_x)
171                 {
172                         x1 = center_x;
173                         x2 = get_cursor_x();
174                 }
175                 else
176                 {
177                         x1 = get_cursor_x();
178                         x2 = center_x;
179                 }
181                 plugin->config.min = x1 * 
182                         (HISTOGRAM_MAX - HISTOGRAM_MIN) / 
183                         get_w() + 
184                         HISTOGRAM_MIN;
186                 plugin->config.max = x2 * 
187                         (HISTOGRAM_MAX - HISTOGRAM_MIN) / 
188                         get_w() + 
189                         HISTOGRAM_MIN;
191                 gui->min->update(plugin->config.min);
192                 gui->max->update(plugin->config.max);
193                 draw();
194                 plugin->send_configure_change();
195                 return 1;
196         }
197         return 0;
200 void ThresholdCanvas::draw()
202         int max = 0;
203         set_color(WHITE);
204         draw_box(0, 0, get_w(), get_h());
205         int border_x1 = (int)((0 - HISTOGRAM_MIN) / 
206                 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
207                 get_w());
208         int border_x2 = (int)((1.0 - HISTOGRAM_MIN) / 
209                 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
210                 get_w());
212         int x1 = (int)((plugin->config.min - HISTOGRAM_MIN) / 
213                 (HISTOGRAM_MAX - HISTOGRAM_MIN) * 
214                 get_w());
215         int x2 = (int)((plugin->config.max - HISTOGRAM_MIN) /
216                 (HISTOGRAM_MAX - HISTOGRAM_MIN) *
217                 get_w());
219         if(plugin->engine)
220         {
221                 int64_t *array = plugin->engine->accum[HISTOGRAM_VALUE];
224 // Get normalizing factor
225                 for(int i = 0; i < get_w(); i++)
226                 {
227                         int division1 = i * HISTOGRAM_RANGE / get_w();
228                         int division2 = (i + 1) * HISTOGRAM_RANGE / get_w();
229                         int total = 0;
230                         for(int j = division1; j < division2; j++)
231                         {
232                                 total += array[j];
233                         }
234                         if(total > max) max = total;
235                 }
237                 for(int i = 0; i < get_w(); i++)
238                 {
239                         int division1 = i * HISTOGRAM_RANGE / get_w();
240                         int division2 = (i + 1) * HISTOGRAM_RANGE / get_w();
241                         int total = 0;
242                         for(int j = division1; j < division2; j++)
243                         {
244                                 total += array[j];
245                         }
247                         int pixels;
248                         if(max)
249                                 pixels = total * get_h() / max;
250                         else
251                                 pixels = 0;
252                         if(i >= x1 && i < x2)
253                         {
254                                 set_color(BLUE);
255                                 draw_line(i, 0, i, get_h() - pixels);
256                                 set_color(WHITE);
257                                 draw_line(i, get_h(), i, get_h() - pixels);
258                         }
259                         else
260                         {
261                                 set_color(BLACK);
262                                 draw_line(i, get_h(), i, get_h() - pixels);
263                         }
264                 }
265         }
266         else
267         {
270                 set_color(BLUE);
271                 draw_box(x1, 0, x2 - x1, get_h());
272         }
274         set_color(RED);
275         draw_line(border_x1, 0, border_x1, get_h());
276         draw_line(border_x2, 0, border_x2, get_h());
278         flash(1);
293 ThresholdWindow::ThresholdWindow(ThresholdMain *plugin, int x, int y)
294 : BC_Window(plugin->gui_string, x, y, 440, 350, 440, 350, 0, 1)
296         this->plugin = plugin;
299 ThresholdWindow::~ThresholdWindow()
303 int ThresholdWindow::create_objects()
305         int x1 = 10, x = 10;
306         int y = 10;
307         add_subwindow(canvas = new ThresholdCanvas(plugin,
308                 this,
309                 x,
310                 y,
311                 get_w() - x - 10,
312                 get_h() - 100));
313         canvas->draw();
314         y += canvas->get_h() + 10;
316         add_subwindow(new BC_Title(x, y, _("Min:")));
317         x += 50;
318         min = new ThresholdMin(plugin,
319                 this,
320                 x,
321                 y,
322                 100);
323         min->create_objects();
324         min->set_increment(0.1);
326         x += 200;
327         add_subwindow(new BC_Title(x, y, _("Max:")));
328         x += 50;
329         max = new ThresholdMax(plugin,
330                 this,
331                 x,
332                 y,
333                 100);
334         max->create_objects();
335         max->set_increment(0.1);
337         y += max->get_h();
338         x = x1;
340         add_subwindow(plot = new ThresholdPlot(plugin, x, y));
342         show_window(1);
345 WINDOW_CLOSE_EVENT(ThresholdWindow)
351 PLUGIN_THREAD_OBJECT(ThresholdMain, ThresholdThread, ThresholdWindow)