Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / histogram / histogramwindow.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "bcsignals.h"
24 #include "cursors.h"
25 #include "edl.h"
26 #include "histogram.h"
27 #include "histogramconfig.h"
28 #include "histogramwindow.h"
29 #include "keys.h"
30 #include "language.h"
31 #include "localsession.h"
32 #include "mwindow.h"
33 #include "pluginserver.h"
34 #include "theme.h"
35
36 #include <unistd.h>
37
38
39
40
41 HistogramWindow::HistogramWindow(HistogramMain *plugin)
42  : PluginClientWindow(plugin,
43         plugin->w,
44         plugin->h,
45         xS(440),
46         yS(500),
47         1)
48 {
49         this->plugin = plugin;
50         active_value = 0;
51 }
52
53 HistogramWindow::~HistogramWindow()
54 {
55 }
56
57
58 void HistogramWindow::create_objects()
59 {
60         int margin = plugin->get_theme()->widget_border;
61         int x = margin, y = margin, x1 = margin;
62
63         add_subwindow(mode_v = new HistogramMode(plugin, x, y,
64                 HISTOGRAM_VALUE, _("Value")));
65         x += mode_v->get_w() + margin;
66         add_subwindow(mode_r = new HistogramMode(plugin, x, y,
67                 HISTOGRAM_RED, _("Red")));
68         x += mode_r->get_w() + margin;
69         add_subwindow(mode_g = new HistogramMode(plugin, x, y,
70                 HISTOGRAM_GREEN, _("Green")));
71         x += mode_g->get_w() + margin;
72         add_subwindow(mode_b = new HistogramMode(plugin, x, y,
73                 HISTOGRAM_BLUE, _("Blue")));
74         x = get_w() - margin - plugin->get_theme()->get_image_set("histogram_rgb_toggle")[0]->get_w();
75         add_subwindow(parade_on = new HistogramParade(plugin, this,
76                 x, y, 1));
77         x -= parade_on->get_w() + margin;
78         add_subwindow(parade_off = new HistogramParade(plugin, this, x, y, 0));
79
80         x = x1;
81         y += parade_on->get_h() + margin;
82         add_subwindow(canvas_title1 = new BC_Title(margin, y, "-10%"));
83         x = get_w() - get_text_width(MEDIUMFONT, "110%") - margin;
84         add_subwindow(canvas_title2 = new BC_Title(x, y, "110%"));
85
86         y += canvas_title2->get_h() + margin;
87         x = x1;
88         canvas_h = get_h() - y - yS(210);
89
90         add_subwindow(low_input_carrot = new HistogramCarrot(plugin, this, x, y + canvas_h));
91
92         x = low_input_carrot->get_w() / 2 + x;
93         canvas_w = get_w() - x - x;
94
95         title1_x = x;
96         title2_x = x + (int)(canvas_w * -HIST_MIN_INPUT / FLOAT_RANGE);
97         title3_x = x + (int)(canvas_w * (1.0 - HIST_MIN_INPUT) / FLOAT_RANGE);
98         title4_x = x + (int)(canvas_w);
99
100         add_subwindow(canvas = new HistogramCanvas(plugin, this,
101                         x, y, canvas_w, canvas_h));
102
103 // Canvas border
104         draw_3d_border(x - 2, y - 2, canvas_w + 4, canvas_h + 4,
105                 get_bg_color(), BLACK, MDGREY, get_bg_color());
106
107 // Calculate output curve with no value function
108         plugin->tabulate_curve(plugin->preview_lookup, plugin->mode, 0x10000, 0);
109
110         y += canvas->get_h();
111         x = margin;
112
113         add_subwindow(gamma_carrot = new HistogramCarrot(plugin, this,
114                 canvas->get_x() + canvas->get_w() / 2 -
115                         low_input_carrot->get_w() / 2 , y));
116
117         add_subwindow(high_input_carrot = new HistogramCarrot(plugin, this,
118                 canvas->get_x() + canvas->get_w() -
119                         low_input_carrot->get_w() / 2, y));
120         y += low_input_carrot->get_h() + margin;
121
122 //      add_subwindow(title = new BC_Title(x, y, _("Input:")));
123 //      x += title->get_w() + margin;
124         low_input = new HistogramText(plugin, this, x, y);
125         low_input->create_objects();
126
127         x = get_w() / 2 - low_input->get_w() / 2;
128         gamma = new HistogramText(plugin, this, x, y, 0.01, 100.);
129         gamma->create_objects();
130
131         x = get_w() - low_input->get_w() - margin;
132         high_input = new HistogramText(plugin, this, x, y);
133         high_input->create_objects();
134
135         y += high_input->get_h() + margin;
136         x = x1;
137
138         add_subwindow(output = new HistogramSlider(plugin, this,
139                 canvas->get_x(), y, canvas->get_w(), yS(20), 0));
140         output->update();
141
142 // Output border
143         draw_3d_border(output->get_x() - 2, output->get_y() - 2,
144                 output->get_w() + 4, output->get_h() + 4,
145                 get_bg_color(), BLACK, MDGREY, get_bg_color());
146         y += output->get_h();
147
148         add_subwindow(low_output_carrot = new HistogramCarrot(plugin,
149                 this, margin, y));
150
151         add_subwindow(high_output_carrot = new HistogramCarrot(plugin,
152                 this, canvas->get_x() + canvas->get_w() -
153                         low_output_carrot->get_w() / 2, y));
154         y += high_output_carrot->get_h() + margin;
155
156
157 //      add_subwindow(title = new BC_Title(x, y, _("Output:")));
158 //      x += title->get_w() + margin;
159         low_output = new HistogramText(plugin, this, x, y);
160         low_output->create_objects();
161
162         const char *linear_text = _("Linear");
163         int xs = get_w()/2 - xS(50);
164         x = xs - BC_Title::calculate_w(this, linear_text) - margin;
165         add_subwindow(log_title1 = new BC_Title(x, y, linear_text));
166         add_subwindow(log_slider = new HistogramLogSlider(plugin, this, xs, y));
167         xs += log_slider->get_w() + margin;
168         add_subwindow(log_title2 = new BC_Title(xs, y, _("Log")));
169
170         high_output = new HistogramText(plugin, this,
171                 get_w() - low_output->get_w() - margin, y);
172         high_output->create_objects();
173
174         x = x1;
175         y += high_output->get_h() + margin;
176
177         add_subwindow(bar = new BC_Bar(x, y, get_w() - margin * 2));
178         y += bar->get_h() + margin;
179
180         add_subwindow(automatic = new HistogramAuto(plugin, x, y));
181         int x2 = xS(190);
182         add_subwindow(threshold_title = new BC_Title(x2, y, _("Threshold:")));
183         int x3 = xS(305);
184         threshold = new HistogramText(plugin, this, x3, y);
185         threshold->create_objects();
186         y += automatic->get_h() + margin;
187
188         add_subwindow(plot= new HistogramPlot(plugin, x, y));
189         add_subwindow(sum_frames = new HistogramSumFrames(plugin, x2, y));
190         y += plot->get_h() + margin;
191
192         x = x1;
193         add_subwindow(split = new HistogramSplit(plugin, x, y));
194         x = xS(340);
195         add_subwindow(reset = new HistogramReset(plugin, x, y + yS(5)));
196
197         update(1, 1, 1, 1);
198
199         flash(0);
200         show_window();
201 }
202
203
204
205 int HistogramWindow::resize_event(int w, int h)
206 {
207         int xdiff = w - get_w();
208         int ydiff = h - get_h();
209
210         parade_on->reposition_window(parade_on->get_x() + xdiff,
211                 parade_on->get_y());
212         parade_off->reposition_window(parade_off->get_x() + xdiff,
213                 parade_on->get_y());
214         canvas_title2->reposition_window(canvas_title2->get_x() + xdiff,
215                 canvas_title2->get_y());
216
217 // Canvas follows window size
218         canvas_w = canvas_w + xdiff;
219         canvas_h = canvas_h + ydiff;
220         canvas->reposition_window(canvas->get_x(), canvas->get_y(),
221                 canvas_w, canvas_h);
222
223 // Canvas border
224         draw_3d_border(canvas->get_x() - 2, canvas->get_y() - 2,
225                 canvas_w + 4, canvas_h + 4,
226                 get_bg_color(), BLACK, MDGREY, get_bg_color());
227
228         low_input_carrot->reposition_window(low_input_carrot->get_x(),
229                 low_input_carrot->get_y() + ydiff);
230         gamma_carrot->reposition_window(gamma_carrot->get_x(),
231                 gamma_carrot->get_y() + ydiff);
232         high_input_carrot->reposition_window(high_input_carrot->get_x(),
233                 high_input_carrot->get_y() + ydiff);
234
235         low_input->reposition_window(low_input->get_x(),
236                 low_input->get_y() + ydiff);
237         gamma->reposition_window(w / 2 - gamma->get_w() / 2,
238                 gamma->get_y() + ydiff);
239         high_input->reposition_window(high_input->get_x() + xdiff,
240                 low_input->get_y() + ydiff);
241
242         output->reposition_window(output->get_x(),
243                 output->get_y() + ydiff,
244                 output->get_w() + xdiff,
245                 output->get_h());
246         output->update();
247
248 // Output border
249         draw_3d_border(output->get_x() - 2, output->get_y() - 2,
250                 output->get_w() + 4, output->get_h() + 4,
251                 get_bg_color(), BLACK, MDGREY, get_bg_color());
252
253         low_output_carrot->reposition_window(low_output_carrot->get_x(),
254                 low_output_carrot->get_y() + ydiff);
255         high_output_carrot->reposition_window(high_output_carrot->get_x(),
256                 high_output_carrot->get_y() + ydiff);
257
258         low_output->reposition_window(low_output->get_x(),
259                 low_output->get_y() + ydiff);
260         int xs = (w - log_slider->get_w()) / 2;
261         int margin = plugin->get_theme()->widget_border;
262         log_title1->reposition_window(xs - log_title1->get_w() - margin,
263                 log_title1->get_y() + ydiff);
264         log_slider->reposition_window(xs,
265                 log_slider->get_y() + ydiff);
266         log_title2->reposition_window(xs + log_slider->get_w() + margin,
267                 log_title2->get_y() + ydiff);
268         high_output->reposition_window(high_output->get_x() + xdiff,
269                 high_output->get_y() + ydiff);
270
271         bar->reposition_window(bar->get_x(),
272                 bar->get_y() + ydiff,
273                 bar->get_w() + xdiff);
274
275         automatic->reposition_window(automatic->get_x(),
276                 automatic->get_y() + ydiff);
277         threshold_title->reposition_window(threshold_title->get_x(),
278                 threshold_title->get_y() + ydiff);
279         threshold->reposition_window(threshold->get_x(),
280                 threshold->get_y() + ydiff);
281         plot->reposition_window(plot->get_x(),
282                 plot->get_y() + ydiff);
283         sum_frames->reposition_window(sum_frames->get_x(),
284                 sum_frames->get_y() + ydiff);
285
286         split->reposition_window(split->get_x(),
287                 split->get_y() + ydiff);
288         reset->reposition_window(reset->get_x(),
289                 reset->get_y() + ydiff);
290
291         update(1, 1, 1, 1);
292
293         plugin->w = w;
294         plugin->h = h;
295         flash();
296         return 1;
297 }
298
299
300
301 int HistogramWindow::keypress_event()
302 {
303         int result = 0;
304
305         if(active_value)
306         {
307                 float sign = 1;
308                 for(int i = 0; i < HISTOGRAM_MODES; i++)
309                 {
310                         if(active_value == &plugin->config.gamma[i])
311                                 sign = -1;
312                 }
313
314                 if(get_keypress() == RIGHT || get_keypress() == UP)
315                 {
316                         *active_value += sign * PRECISION;
317                         plugin->config.boundaries();
318                         update(1, 1, 1, 0);
319                         plugin->send_configure_change();
320                         return 1;
321                 }
322                 else
323                 if(get_keypress() == LEFT || get_keypress() == DOWN)
324                 {
325                         *active_value -= sign * PRECISION;
326                         plugin->config.boundaries();
327                         update(1, 1, 1, 0);
328                         plugin->send_configure_change();
329                         return 1;
330                 }
331         }
332
333         return result;
334 }
335
336 void HistogramWindow::update(int do_canvases, int do_carrots, int do_text, int do_toggles)
337 {
338         if(do_toggles) {
339                 automatic->update(plugin->config.automatic);
340                 mode_v->update(plugin->mode == HISTOGRAM_VALUE ? 1 : 0);
341                 mode_r->update(plugin->mode == HISTOGRAM_RED ? 1 : 0);
342                 mode_g->update(plugin->mode == HISTOGRAM_GREEN ? 1 : 0);
343                 mode_b->update(plugin->mode == HISTOGRAM_BLUE ? 1 : 0);
344                 plot->update(plugin->config.plot);
345                 sum_frames->update(plugin->config.sum_frames);
346                 split->update(plugin->config.split);
347                 parade_on->update(plugin->parade ? 1 : 0);
348                 parade_off->update(plugin->parade ? 0 : 1);
349                 log_slider->update(plugin->config.log_slider);
350                 output->update();
351         }
352
353         if(do_canvases) {
354                 update_canvas();
355         }
356
357         if(do_carrots) {
358                 low_input_carrot->update();
359                 high_input_carrot->update();
360                 gamma_carrot->update();
361                 low_output_carrot->update();
362                 high_output_carrot->update();
363         }
364
365         if(do_text) {
366                 low_input->update();
367                 gamma->update();
368                 high_input->update();
369                 low_output->update();
370                 high_output->update();
371                 threshold->update();
372         }
373 }
374
375
376 void HistogramWindow::draw_canvas_mode(int mode, int color, int y, int h)
377 {
378 // Draw histogram
379         int64_t max = 0;
380         int64_t *accum = plugin->accum[mode];
381         for( int k0=0,x=0; x<canvas_w; ++x ) {
382                 int k1 = (HISTOGRAM_SLOTS * (x+1)) / canvas_w;
383                 if( k0 == k1 ) continue;
384                 int64_t m = accum[k0];
385                 for( int k=k0; ++k<k1; ) {
386                         if( m < accum[k] ) m = accum[k];
387                 }
388                 if( max < m ) max = m;
389                 k0 = k1;
390         }
391         if( max > 0 ) {
392                 double log_slider = plugin->config.log_slider;
393                 double lin_slider = 1. - log_slider;
394                 double lin_max = (lin_slider * h) / max;
395                 double log_max = (log_slider * h) / log(max);
396                 for( int k0=0,x=0; x<canvas_w; ++x ) {
397                         int k1 = (HISTOGRAM_SLOTS * (x+1)) / canvas_w;
398                         if( k0 == k1 ) continue;
399                         int64_t m = accum[k0];
400                         for( int k=k0; ++k<k1; ) {
401                                 if( m < accum[k] ) m = accum[k];
402                         }
403                         int y1 = y+h;
404                         double vv = m > 0 ? (double)m : 0;
405                         m = vv > 0 ? vv*lin_max + log(vv)*log_max : 0;
406                         canvas->set_color(BLACK);
407                         int y0 = y1 - m;
408                         canvas->draw_line(x, y, x, y0);
409                         canvas->set_color(color);
410                         canvas->draw_line(x, y0, x, y1);
411                         k0 = k1;
412                 }
413         }
414         else {
415                 canvas->set_color(BLACK);
416                 canvas->draw_box(0, y, canvas_w, h);
417         }
418
419 // Draw overlays
420         canvas->set_color(WHITE);
421         canvas->set_line_width(2);
422         int y1 = 0;
423
424 // Draw output line
425         for( int x=0; x<canvas_w; ++x ) {
426                 float input = (float)x / canvas_w * FLOAT_RANGE + HIST_MIN_INPUT;
427                 float output = plugin->calculate_level(input, mode, 0);
428                 int y2 = h - (int)(output * h);
429                 if( x > 0 )
430                         canvas->draw_line(x-1, y+y1, x, y+y2);
431                 y1 = y2;
432         }
433
434         canvas->set_line_width(1);
435 }
436
437
438 void HistogramWindow::update_canvas()
439 {
440         if(plugin->parade)
441         {
442                 draw_canvas_mode(HISTOGRAM_RED, RED, 0, canvas_h / 3);
443                 draw_canvas_mode(HISTOGRAM_GREEN, GREEN, canvas_h / 3, canvas_h / 3);
444                 draw_canvas_mode(HISTOGRAM_BLUE, BLUE, canvas_h * 2 / 3, canvas_h - canvas_h * 2 / 3);
445         }
446         else
447         {
448                 draw_canvas_mode(plugin->mode, MEGREY, 0, canvas_h);
449         }
450
451
452 // Draw 0 and 100% lines.
453         canvas->set_color(RED);
454         int x = (int)(canvas_w * -HIST_MIN_INPUT / FLOAT_RANGE);
455         canvas->draw_line(x, 0, x, canvas_h);
456         x = (int)(canvas_w * (1.0 - HIST_MIN_INPUT) / FLOAT_RANGE);
457         canvas->draw_line(x, 0, x, canvas_h);
458         canvas->flash();
459 }
460
461
462 HistogramParade::HistogramParade(HistogramMain *plugin, HistogramWindow *gui,
463                 int x, int y, int value)
464  : BC_Toggle(x, y,
465         value ? plugin->get_theme()->get_image_set("histogram_rgb_toggle") :
466                 plugin->get_theme()->get_image_set("histogram_toggle"),
467         0)
468 {
469         this->plugin = plugin;
470         this->gui = gui;
471         this->value = value;
472         if(value)
473                 set_tooltip(_("RGB Parade on"));
474         else
475                 set_tooltip(_("RGB Parade off"));
476 }
477
478 int HistogramParade::handle_event()
479 {
480         update(1);
481         plugin->parade = value;
482         gui->update(1, 0, 0, 1);
483         return 1;
484 }
485
486 HistogramCanvas::HistogramCanvas(HistogramMain *plugin, HistogramWindow *gui,
487                 int x, int y, int w, int h)
488  : BC_SubWindow(x, y, w, h, BLACK)
489 {
490         this->plugin = plugin;
491         this->gui = gui;
492 }
493
494 int HistogramCanvas::button_press_event()
495 {
496         int result = 0;
497         if(is_event_win() && cursor_inside())
498         {
499                 if(!plugin->dragging_point &&
500                         (!plugin->config.automatic || plugin->mode == HISTOGRAM_VALUE))
501                 {
502                         gui->deactivate();
503                 }
504         }
505         return result;
506 }
507
508 int HistogramCanvas::cursor_motion_event()
509 {
510         if(is_event_win() && cursor_inside())
511         {
512         }
513         return 0;
514 }
515
516 int HistogramCanvas::button_release_event()
517 {
518         return 0;
519 }
520
521
522 HistogramReset::HistogramReset(HistogramMain *plugin, int x, int y)
523  : BC_GenericButton(x, y, _("Reset"))
524 {
525         this->plugin = plugin;
526 }
527 int HistogramReset::handle_event()
528 {
529         plugin->config.reset(0);
530         ((HistogramWindow*)plugin->thread->window)->update(1, 1, 1, 1);
531         plugin->send_configure_change();
532         return 1;
533 }
534
535
536 HistogramLogSlider::HistogramLogSlider(HistogramMain *plugin, HistogramWindow *gui,
537                 int x, int y)
538 : BC_FSlider(x, y, 0, xS(100), xS(100), 0., 1., plugin->config.log_slider)
539
540 {
541         this->plugin = plugin;
542         this->gui = gui;
543 }
544
545 int HistogramLogSlider::handle_event()
546 {
547         plugin->config.log_slider = get_value();
548         plugin->send_configure_change();
549         return 1;
550 }
551
552 HistogramCarrot::HistogramCarrot(HistogramMain *plugin, HistogramWindow *gui, int x, int y)
553  : BC_Toggle(x, y, plugin->get_theme()->get_image_set("histogram_carrot"), 0)
554 {
555         this->plugin = plugin;
556         this->gui = gui;
557         drag_operation = 0;
558 }
559
560 HistogramCarrot::~HistogramCarrot()
561 {
562 }
563
564 float* HistogramCarrot::get_value()
565 {
566         if( this == gui->low_input_carrot )
567                 return &plugin->config.low_input[plugin->mode];
568         if( this == gui->high_input_carrot )
569                 return &plugin->config.high_input[plugin->mode];
570         if( this == gui->gamma_carrot )
571                 return &plugin->config.gamma[plugin->mode];
572         if( this == gui->low_output_carrot )
573                 return &plugin->config.low_output[plugin->mode];
574         if( this == gui->high_output_carrot )
575                 return &plugin->config.high_output[plugin->mode];
576         return 0;
577 }
578
579 void HistogramCarrot::update()
580 {
581         int new_x = 0;
582         float *value = get_value();
583
584         if(this != gui->gamma_carrot)
585         {
586                 new_x = (int)(gui->canvas->get_x() +
587                         (*value - HIST_MIN_INPUT) *
588                         gui->canvas->get_w() /
589                         (HIST_MAX_INPUT - HIST_MIN_INPUT) -
590                         get_w() / 2);
591         }
592         else
593         {
594                 float min = plugin->config.low_input[plugin->mode];
595                 float max = plugin->config.high_input[plugin->mode];
596                 float delta = (max - min) / 2.0;
597                 float mid = min + delta;
598                 float tmp = log10(1.0 / *value);
599                 tmp = mid + delta * tmp;
600
601 //printf("HistogramCarrot::update %d %f %f\n", __LINE__, *value, tmp);
602
603                         new_x = gui->canvas->get_x() -
604                                 get_w() / 2 +
605                                 (int)(gui->canvas->get_w() *
606                                 (tmp - HIST_MIN_INPUT) /
607                                 (HIST_MAX_INPUT - HIST_MIN_INPUT));
608         }
609
610         reposition_window(new_x, get_y());
611 }
612
613 int HistogramCarrot::button_press_event()
614 {
615         if(is_event_win() && get_buttonpress() == 1)
616         {
617                 //int w = get_w();
618                 gui->deactivate();
619                 set_status(BC_Toggle::TOGGLE_DOWN);
620
621                 BC_Toggle::update(0);
622                 gui->active_value = get_value();
623 // Disable the other toggles
624                 if(this != gui->low_input_carrot) gui->low_input_carrot->BC_Toggle::update(0);
625                 if(this != gui->high_input_carrot) gui->high_input_carrot->BC_Toggle::update(0);
626                 if(this != gui->gamma_carrot) gui->gamma_carrot->BC_Toggle::update(0);
627                 if(this != gui->low_output_carrot) gui->low_output_carrot->BC_Toggle::update(0);
628                 if(this != gui->high_output_carrot) gui->high_output_carrot->BC_Toggle::update(0);
629                 starting_x = get_x();
630                 offset_x = gui->get_relative_cursor_x();
631                 offset_y = gui->get_relative_cursor_y();
632 //printf("HistogramCarrot::button_press_event %d %d %d\n", __LINE__, starting_x, offset_x);
633                 drag_operation = 1;
634                 draw_face(1, 1);
635                 return 1;
636         }
637         return 0;
638 }
639
640 int HistogramCarrot::button_release_event()
641 {
642         int result = BC_Toggle::button_release_event();
643         handle_event();
644         drag_operation = 0;
645         return result;
646 }
647
648 int HistogramCarrot::cursor_motion_event()
649 {
650         int cursor_x = gui->get_relative_cursor_x();
651
652         if(drag_operation)
653         {
654 //printf("HistogramCarrot::cursor_motion_event %d %d\n", __LINE__, cursor_x);
655                 int new_x = starting_x + cursor_x - offset_x;
656
657 // Clamp x
658 // Get level from x
659                 float *value = get_value();
660                 if(this == gui->gamma_carrot)
661                 {
662                         float min = gui->low_input_carrot->get_x();
663                         float max = gui->high_input_carrot->get_x();
664                         float delta = (max - min) / 2.0;
665                         if(delta >= 0.5)
666                         {
667                                 float mid = min + delta;
668                                 float tmp = (float)(new_x - mid) /
669                                         delta;
670                                 tmp = 1.0 / pow(10, tmp);
671                                 CLAMP(tmp, MIN_GAMMA, MAX_GAMMA);
672                                 *value = tmp;
673 //printf("HistogramCarrot::update %d %f\n", __LINE__, tmp);
674                         }
675                 }
676                 else
677                 {
678                         int min_x = gui->canvas->get_x() - get_w() / 2;
679                         int max_x = gui->canvas->get_x() + gui->canvas->get_w() - get_w() / 2;
680                         CLAMP(new_x, min_x, max_x);
681                         *value = HIST_MIN_INPUT +
682                                 (HIST_MAX_INPUT - HIST_MIN_INPUT) *
683                                 (new_x - min_x) /
684                                 (max_x - min_x);
685                 }
686
687                 reposition_window(new_x, get_y());
688                 flush();
689
690                 gui->update(1,
691                         (this == gui->low_input_carrot || this == gui->high_input_carrot),
692                         1,
693                         0);
694                 plugin->send_configure_change();
695
696                 return 1;
697         }
698         return 0;
699 }
700
701
702
703 HistogramSlider::HistogramSlider(HistogramMain *plugin, HistogramWindow *gui,
704                 int x, int y, int w, int h, int is_input)
705  : BC_SubWindow(x, y, w, h)
706 {
707         this->plugin = plugin;
708         this->gui = gui;
709         this->is_input = is_input;
710         operation = NONE;
711 }
712
713 int HistogramSlider::input_to_pixel(float input)
714 {
715         return (int)((input - HIST_MIN_INPUT) / FLOAT_RANGE * get_w());
716 }
717
718 void HistogramSlider::update()
719 {
720         int w = get_w();
721         int h = get_h();
722         //int half_h = get_h() / 2;
723         //int quarter_h = get_h() / 4;
724         int mode = plugin->mode;
725         int r = 0xff;
726         int g = 0xff;
727         int b = 0xff;
728
729         clear_box(0, 0, w, h);
730
731         switch(mode) {
732         case HISTOGRAM_RED:
733                 g = b = 0x00;
734                 break;
735         case HISTOGRAM_GREEN:
736                 r = b = 0x00;
737                 break;
738         case HISTOGRAM_BLUE:
739                 r = g = 0x00;
740                 break;
741         }
742
743         for( int i = 0; i < w; i++ ) {
744                 int color = (int)(i * 0xff / w);
745                 set_color(((r * color / 0xff) << 16) |
746                         ((g * color / 0xff) << 8) |
747                         (b * color / 0xff));
748                 draw_line(i, 0, i, h);
749         }
750         flash();
751 }
752
753
754 HistogramAuto::HistogramAuto(HistogramMain *plugin, int x, int y)
755  : BC_CheckBox(x, y, plugin->config.automatic, _("Automatic"))
756 {
757         this->plugin = plugin;
758 }
759
760 int HistogramAuto::handle_event()
761 {
762         plugin->config.automatic = get_value();
763         plugin->send_configure_change();
764         return 1;
765 }
766
767
768 HistogramPlot::HistogramPlot(HistogramMain *plugin, int x, int y)
769  : BC_CheckBox(x, y, plugin->config.plot, _("Plot histogram"))
770 {
771         this->plugin = plugin;
772 }
773
774 int HistogramPlot::handle_event()
775 {
776         plugin->config.plot = get_value();
777         plugin->send_configure_change();
778         return 1;
779 }
780
781
782 HistogramSumFrames::HistogramSumFrames(HistogramMain *plugin, int x, int y)
783  : BC_CheckBox(x, y, plugin->config.sum_frames, _("Sum frames"))
784 {
785         this->plugin = plugin;
786 }
787
788 int HistogramSumFrames::handle_event()
789 {
790         plugin->config.sum_frames = get_value();
791         plugin->send_configure_change();
792         return 1;
793 }
794
795
796 HistogramSplit::HistogramSplit(HistogramMain *plugin, int x, int y)
797  : BC_CheckBox(x, y, plugin->config.split, _("Split output"))
798 {
799         this->plugin = plugin;
800 }
801
802 int HistogramSplit::handle_event()
803 {
804         plugin->config.split = get_value();
805         plugin->send_configure_change();
806         return 1;
807 }
808
809
810 HistogramMode::HistogramMode(HistogramMain *plugin,
811                 int x, int y, int value, char *text)
812  : BC_Radial(x, y, plugin->mode == value, text)
813 {
814         this->plugin = plugin;
815         this->value = value;
816 }
817 int HistogramMode::handle_event()
818 {
819         HistogramWindow *gui = (HistogramWindow*)plugin->thread->window;
820         plugin->mode = value;
821         plugin->current_point= -1;
822         gui->active_value = 0;
823         gui->low_input_carrot->BC_Toggle::update(0);
824         gui->gamma_carrot->BC_Toggle::update(0);
825         gui->high_input_carrot->BC_Toggle::update(0);
826         gui->low_output_carrot->BC_Toggle::update(0);
827         gui->high_output_carrot->BC_Toggle::update(0);
828         gui->update(1, 1, 1, 1);
829 //      plugin->send_configure_change();
830         return 1;
831 }
832
833
834 HistogramText::HistogramText(HistogramMain *plugin,
835         HistogramWindow *gui, int x, int y, float hist_min, float hist_max)
836  : BC_TumbleTextBox(gui, 0.0, hist_min, hist_max, x, y, xS(80))
837 {
838         this->plugin = plugin;
839         this->gui = gui;
840         set_precision(DIGITS);
841         set_increment(PRECISION);
842 }
843
844 float* HistogramText::get_value()
845 {
846         if( this == gui->low_input )
847                 return &plugin->config.low_input[plugin->mode];
848         if( this == gui->high_input )
849                 return &plugin->config.high_input[plugin->mode];
850         if( this == gui->gamma )
851                 return &plugin->config.gamma[plugin->mode];
852         if( this == gui->low_output )
853                 return &plugin->config.low_output[plugin->mode];
854         if( this == gui->high_output )
855                 return &plugin->config.high_output[plugin->mode];
856         if( this == gui->threshold )
857                 return &plugin->config.threshold;
858         return 0;
859 }
860
861 int HistogramText::handle_event()
862 {
863         float *output = get_value();
864         if( output )
865                 *output = atof(get_text());
866
867         gui->update(1, 1, 0, 0);
868         plugin->send_configure_change();
869         return 1;
870 }
871
872 void HistogramText::update()
873 {
874         float *output = get_value();
875         if( output )
876                 BC_TumbleTextBox::update(*output);
877 }
878