videoscope tweaks, add scope overlay grid (andrea), hw probe fix for sw fallback
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / scopewindow.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "bcsignals.h"
22 #include "bccolors.h"
23 #include "boxblur.h"
24 #include "clip.h"
25 #include "cursors.h"
26 #include "file.h"
27 #include "filesystem.h"
28 #include "language.h"
29 #include "scopewindow.h"
30 #include "theme.h"
31
32 #include <string.h>
33
34 ScopePackage::ScopePackage()
35  : LoadPackage()
36 {
37 }
38
39 ScopeUnit::ScopeUnit(ScopeGUI *gui,
40         ScopeEngine *server)
41  : LoadClient(server)
42 {
43         this->gui = gui;
44 }
45
46
47 #define incr_point(rows,h, iv,comp) { \
48 if(iy >= 0 && iy < h) { \
49   uint8_t *vp = rows[iy] + ix*3 + (comp); \
50   int v = *vp+(iv);  *vp = v>0xff ? 0xff : v; } \
51 }
52 #define incr_points(rows,h, rv,gv,bv) { \
53 if(iy >= 0 && iy < h) { \
54   uint8_t *vp = rows[iy] + ix*3; \
55   int v = *vp+(rv);  *vp++ = v>0xff ? 0xff : v; \
56   v = *vp+(gv);  *vp++ = v>0xff ? 0xff : v; \
57   v = *vp+(bv);  *vp = v>0xff ? 0xff : v; } \
58 }
59 #define decr_points(rows,h, rv,gv,bv) { \
60 if(iy >= 0 && iy < h) { \
61   uint8_t *vp = rows[iy] + ix*3; \
62   int v = *vp-(rv);  *vp++ = v<0 ? 0 : v; \
63   v = *vp-(gv);  *vp++ = v<0 ? 0 : v; \
64   v = *vp-(bv);  *vp = v<0 ? 0 : v; } \
65 }
66 #define clip_points(rows,h, rv,gv,bv) { \
67 if(iy >= 0 && iy < h) { \
68   uint8_t *vp = rows[iy] + ix*3; \
69   int v = *vp+(rv);  *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
70   v = *vp+(gv);  *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
71   v = *vp+(bv);  *vp = v>0xff ? 0xff : v<0 ? 0 : v; } \
72 }
73
74 #define PROCESS_PIXEL(column) { \
75 /* Calculate histogram */ \
76         if(use_hist) { \
77                 int v_i = (intensity - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
78                 CLAMP(v_i, 0, TOTAL_BINS - 1); \
79                 bins[3][v_i]++; \
80         } \
81         if(use_hist_parade) { \
82                 int r_i = (r - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
83                 int g_i = (g - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
84                 int b_i = (b - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
85                 CLAMP(r_i, 0, TOTAL_BINS - 1); \
86                 CLAMP(g_i, 0, TOTAL_BINS - 1); \
87                 CLAMP(b_i, 0, TOTAL_BINS - 1); \
88                 bins[0][r_i]++; \
89                 bins[1][g_i]++; \
90                 bins[2][b_i]++; \
91         } \
92 /* Calculate waveform */ \
93         if(use_wave || use_wave_parade) { \
94                 int ix = column * wave_w / dat_w; \
95                 if(ix >= 0 && ix < wave_w-1) { \
96                         if(use_wave_parade > 0) { \
97                                 ix /= 3; \
98                                 int iy = wave_h - ((r - FLOAT_MIN) /  \
99                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
100                                 incr_point(waveform_rows,wave_h, winc,0); \
101                                 ix += wave_w/3; \
102                                 iy = wave_h - ((g - FLOAT_MIN) /  \
103                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
104                                 incr_point(waveform_rows,wave_h, winc,1); \
105                                 ix += wave_w/3; \
106                                 iy = wave_h - ((b - FLOAT_MIN) /  \
107                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
108                                 incr_point(waveform_rows,wave_h, winc,2); \
109                         } \
110                         else if(use_wave_parade < 0) { \
111                                 int iy = wave_h - ((r - FLOAT_MIN) /  \
112                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
113                                 clip_points(waveform_rows,wave_h, winc,-winc,-winc); \
114                                 iy = wave_h - ((g - FLOAT_MIN) /  \
115                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
116                                 clip_points(waveform_rows,wave_h, -winc,winc,-winc); \
117                                 iy = wave_h - ((b - FLOAT_MIN) /  \
118                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
119                                 clip_points(waveform_rows,wave_h, -winc,-winc,winc); \
120                         } \
121                         else { float yinc = 3*winc; \
122                                 float rinc = yinc*(r-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
123                                 float ginc = yinc*(g-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
124                                 float binc = yinc*(b-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
125                                 int iy = wave_h - ((intensity - FLOAT_MIN) /  \
126                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
127                                 incr_points(waveform_rows,wave_h, rinc, ginc, binc); \
128                         } \
129                 } \
130         } \
131 /* Calculate vectorscope */ \
132         if(use_vector > 0) { \
133                 double t = TO_RAD(-h); \
134                 float adjacent = sin(t), opposite = cos(t); \
135                 int ix = vector_cx + adjacent * (s) / (FLOAT_MAX) * radius; \
136                 int iy = vector_cy - opposite * (s) / (FLOAT_MAX) * radius; \
137                 CLAMP(ix, 0, vector_w - 1); \
138                 HSV::hsv_to_rgb(r, g, b, h, s, vincf); \
139                 int rv = r + 3; \
140                 int gv = g + 3; \
141                 int bv = b + 3; \
142                 incr_points(vector_rows,vector_h, rv,gv,bv); \
143         } \
144         else if(use_vector < 0) { \
145                 double t = TO_RAD(-h); \
146                 float adjacent = sin(t), opposite = cos(t); \
147                 int ix = vector_cx + adjacent * (s) / (FLOAT_MAX) * radius; \
148                 int iy = vector_cy - opposite * (s) / (FLOAT_MAX) * radius; \
149                 CLAMP(ix, 0, vector_w - 1); \
150                 decr_points(vector_rows,vector_h, vinc,vinc,vinc); \
151         } \
152 }
153
154 #define PROCESS_RGB_PIXEL(type,max, column) { \
155         type *rp = (type *)row; \
156         r = (float)rp[0] / max; \
157         g = (float)rp[1] / max; \
158         b = (float)rp[2] / max; \
159         HSV::rgb_to_hsv(r, g, b, h, s, v); \
160         intensity = v; \
161         PROCESS_PIXEL(column) \
162 }
163
164 #define PROCESS_BGR_PIXEL(type,max, column) { \
165         type *rp = (type *)row; \
166         b = (float)rp[0] / max; \
167         g = (float)rp[1] / max; \
168         r = (float)rp[2] / max; \
169         HSV::rgb_to_hsv(r, g, b, h, s, v); \
170         intensity = v; \
171         PROCESS_PIXEL(column) \
172 }
173
174 #define PROCESS_YUV_PIXEL(column, y_in, u_in, v_in) { \
175         YUV::yuv.yuv_to_rgb_f(r, g, b, y_in, u_in, v_in); \
176         HSV::rgb_to_hsv(r, g, b, h, s, v); \
177         intensity = v; \
178         PROCESS_PIXEL(column) \
179 }
180
181 void ScopeUnit::process_package(LoadPackage *package)
182 {
183         ScopePackage *pkg = (ScopePackage*)package;
184
185         float r, g, b;
186         float h, s, v;
187         float intensity;
188         int use_hist = gui->use_hist;
189         int use_hist_parade = gui->use_hist_parade;
190         int use_vector = gui->use_vector;
191         int use_wave = gui->use_wave;
192         int use_wave_parade = gui->use_wave_parade;
193         int vector_cx = gui->vector_cx;
194         int vector_cy = gui->vector_cy;
195         int radius = gui->radius;
196         VFrame *waveform_vframe = gui->waveform_vframe;
197         VFrame *vector_vframe = gui->vector_vframe;
198         int wave_h = waveform_vframe->get_h();
199         int wave_w = waveform_vframe->get_w();
200         int vector_h = vector_vframe->get_h();
201         int vector_w = vector_vframe->get_w();
202         int dat_w = gui->data_frame->get_w();
203         int dat_h = gui->data_frame->get_h();
204         int winc = (wave_w * wave_h) / (dat_w * dat_h);
205         if( use_wave_parade ) winc *= 3;
206         winc += 2;  winc = (winc << gui->use_wave_gain) / 4;
207         int vinc = 3*(vector_w * vector_h) / (dat_w * dat_h);
208         vinc += 2;  vinc = (vinc << gui->use_vect_gain) / 4;
209         float vincf = vinc;
210         uint8_t **waveform_rows = waveform_vframe->get_rows();
211         uint8_t **vector_rows = vector_vframe->get_rows();
212         uint8_t **rows = gui->data_frame->get_rows();
213
214         switch( gui->data_frame->get_color_model() ) {
215         case BC_RGB888:
216                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
217                         uint8_t *row = rows[y];
218                         for( int x=0; x<dat_w; ++x ) {
219                                 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
220                                 row += 3*sizeof(uint8_t);
221                         }
222                 }
223                 break;
224         case BC_RGBA8888:
225                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
226                         uint8_t *row = rows[y];
227                         for( int x=0; x<dat_w; ++x ) {
228                                 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
229                                 row += 4*sizeof(uint8_t);
230                         }
231                 }
232                 break;
233         case BC_RGB161616:
234                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
235                         uint8_t *row = rows[y];
236                         for( int x=0; x<dat_w; ++x ) {
237                                 PROCESS_RGB_PIXEL(uint16_t,0xffff, x)
238                                 row += 3*sizeof(uint16_t);
239                         }
240                 }
241                 break;
242         case BC_RGBA16161616:
243                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
244                         uint8_t *row = rows[y];
245                         for( int x=0; x<dat_w; ++x ) {
246                                 PROCESS_RGB_PIXEL(uint16_t,0xffff, x)
247                                 row += 4*sizeof(uint16_t);
248                         }
249                 }
250                 break;
251         case BC_BGR888:
252                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
253                         uint8_t *row = rows[y];
254                         for( int x=0; x<dat_w; ++x ) {
255                                 PROCESS_BGR_PIXEL(uint8_t,0xff, x)
256                                 row += 3*sizeof(uint8_t);
257                         }
258                 }
259                 break;
260         case BC_BGR8888:
261                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
262                         uint8_t *row = rows[y];
263                         for( int x=0; x<dat_w; ++x ) {
264                                 PROCESS_BGR_PIXEL(uint8_t,0xff, x)
265                                 row += 4*sizeof(uint8_t);
266                         }
267                 }
268                 break;
269         case BC_RGB_FLOAT:
270                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
271                         uint8_t *row = rows[y];
272                         for( int x=0; x<dat_w; ++x ) {
273                                 PROCESS_RGB_PIXEL(float,1.f, x)
274                                 row += 3*sizeof(float);
275                         }
276                 }
277                 break;
278         case BC_RGBA_FLOAT:
279                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
280                         uint8_t *row = rows[y];
281                         for( int x=0; x<dat_w; ++x ) {
282                                 PROCESS_RGB_PIXEL(float,1.f, x)
283                                 row += 4*sizeof(float);
284                         }
285                 }
286                 break;
287         case BC_YUV888:
288                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
289                         uint8_t *row = rows[y];
290                         for( int x=0; x<dat_w; ++x ) {
291                                 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
292                                 row += 3*sizeof(uint8_t);
293                         }
294                 }
295                 break;
296
297         case BC_YUVA8888:
298                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
299                         uint8_t *row = rows[y];
300                         for( int x=0; x<dat_w; ++x ) {
301                                 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
302                                 row += 4*sizeof(uint8_t);
303                         }
304                 }
305                 break;
306         case BC_YUV420P: {
307                 uint8_t *yp = gui->data_frame->get_y();
308                 uint8_t *up = gui->data_frame->get_u();
309                 uint8_t *vp = gui->data_frame->get_v();
310                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
311                         uint8_t *y_row = yp + y * dat_w;
312                         uint8_t *u_row = up + (y / 2) * (dat_w / 2);
313                         uint8_t *v_row = vp + (y / 2) * (dat_w / 2);
314                         for( int x=0; x<dat_w; x+=2 ) {
315                                 PROCESS_YUV_PIXEL(x, *y_row, *u_row, *v_row);
316                                 ++y_row;
317                                 PROCESS_YUV_PIXEL(x + 1, *y_row, *u_row, *v_row);
318                                 ++y_row;  ++u_row;  ++v_row;
319                         }
320                 }
321                 break; }
322         case BC_YUV422:
323                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
324                         uint8_t *row = rows[y];
325                         for( int x=0; x<dat_w; x+=2 ) {
326                                 PROCESS_YUV_PIXEL(x, row[0], row[1], row[3]);
327                                 PROCESS_YUV_PIXEL(x + 1, row[2], row[1], row[3]);
328                                 row += 4*sizeof(uint8_t);
329                         }
330                 }
331                 break;
332
333         default:
334                 printf("ScopeUnit::process_package %d: color_model=%d unrecognized\n",
335                         __LINE__, gui->data_frame->get_color_model());
336                 break;
337         }
338 }
339
340
341 ScopeEngine::ScopeEngine(ScopeGUI *gui, int cpus)
342  : LoadServer(cpus, cpus)
343 {
344 //printf("ScopeEngine::ScopeEngine %d cpus=%d\n", __LINE__, cpus);
345         this->gui = gui;
346 }
347
348 ScopeEngine::~ScopeEngine()
349 {
350 }
351
352 void ScopeEngine::init_packages()
353 {
354         int y = 0, h = gui->data_frame->get_h();
355         for( int i=0,n=LoadServer::get_total_packages(); i<n; ) {
356                 ScopePackage *pkg = (ScopePackage*)get_package(i);
357                 pkg->row1 = y;
358                 pkg->row2 = y = (++i * h) / n;
359         }
360
361         for( int i=0,n=LoadServer::get_total_packages(); i<n; ++i ) {
362                 ScopeUnit *unit = (ScopeUnit*)get_client(i);
363                 for( int j=0; j<HIST_SECTIONS; ++j )
364                         bzero(unit->bins[j], sizeof(int) * TOTAL_BINS);
365         }
366 }
367
368
369 LoadClient* ScopeEngine::new_client()
370 {
371         return new ScopeUnit(gui, this);
372 }
373
374 LoadPackage* ScopeEngine::new_package()
375 {
376         return new ScopePackage;
377 }
378
379 void ScopeEngine::process()
380 {
381         process_packages();
382
383         for(int i = 0; i < HIST_SECTIONS; i++)
384                 bzero(gui->bins[i], sizeof(int) * TOTAL_BINS);
385
386         for(int i=0,n=get_total_clients(); i<n; ++i ) {
387                 ScopeUnit *unit = (ScopeUnit*)get_client(i);
388                 for( int j=0; j<HIST_SECTIONS; ++j ) {
389                         int *bp = gui->bins[j], *up = unit->bins[j];
390                         for( int k=TOTAL_BINS; --k>=0; ++bp,++up ) *bp += *up;
391                 }
392         }
393 }
394
395
396 ScopeGUI::ScopeGUI(Theme *theme,
397         int x, int y, int w, int h, int cpus)
398  : PluginClientWindow(_(PROGRAM_NAME ": Scopes"),
399         x, y, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
400 {
401         this->x = x;
402         this->y = y;
403         this->w = w;
404         this->h = h;
405         this->theme = theme;
406         this->cpus = cpus;
407         reset();
408 }
409
410 ScopeGUI::ScopeGUI(PluginClient *plugin, int w, int h)
411  : PluginClientWindow(plugin, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
412 {
413         this->x = get_x();
414         this->y = get_y();
415         this->w = w;
416         this->h = h;
417         this->theme = plugin->get_theme();
418         this->cpus = plugin->PluginClient::smp + 1;
419         reset();
420 }
421
422 ScopeGUI::~ScopeGUI()
423 {
424         delete waveform_vframe;
425         delete vector_vframe;
426         delete wheel_vframe;
427         delete engine;
428         delete box_blur;
429         delete temp_frame;
430         delete wave_slider;
431         delete vect_slider;
432         delete grad_image;
433         delete grad_pixmap;
434 }
435
436 void ScopeGUI::reset()
437 {
438         waveform_vframe = 0;
439         vector_vframe = 0;
440         wheel_vframe = 0;
441         engine = 0;
442         box_blur = 0;
443         temp_frame = 0;
444         wave_slider = 0;
445         vect_slider = 0;
446         grad_image = 0;
447         grad_pixmap = 0;
448         grad_idx = 0;
449         vect_grads = 0;
450
451         output_frame = 0;
452         data_frame = 0;
453         frame_w = 1;
454         use_smooth = 1;
455         use_wave_gain = 5;
456         use_vect_gain = 5;
457         use_hist = 0;
458         use_wave = 1;
459         use_vector = 1;
460         use_hist_parade = 0;
461         use_wave_parade = 0;
462         use_graticule = 0;
463         waveform = 0;
464         vectorscope = 0;
465         histogram = 0;
466         wave_w = wave_h = 0;
467         vector_w = vector_h = 0;
468         vector_cx = vector_cy = 0;
469         radius = 0;
470         text_color = GREEN;
471         dark_color = (text_color>>2) & 0x3f3f3f;
472         BC_Resources *resources = BC_WindowBase::get_resources();
473         if( resources->bg_color == 0xffffff )
474                 text_color = dark_color;
475 }
476
477 void ScopeGUI::create_objects()
478 {
479         if( use_hist && use_hist_parade )
480                 use_hist = 0;
481         if( use_wave && use_wave_parade )
482                 use_wave = 0;
483         if( !engine ) engine = new ScopeEngine(this, cpus);
484         grad_idx = use_graticule; // last graticule
485         use_graticule = 0;
486
487         lock_window("ScopeGUI::create_objects");
488         int margin = theme->widget_border;
489         int x = margin;
490         int y = margin;
491         add_subwindow(smooth = new ScopeSmooth(this, x, y));
492         y += smooth->get_h() + margin;
493         add_subwindow(scope_menu = new ScopeMenu(this, x, y));
494         scope_menu->create_objects();
495         x += scope_menu->get_w() + margin;
496         add_subwindow(value_text = new BC_Title(x, y, ""));
497         y += scope_menu->get_h() + margin;
498
499         create_panels();
500         update_toggles();
501         show_window();
502         update_scope();
503         unlock_window();
504 }
505
506 void ScopeGUI::create_panels()
507 {
508         calculate_sizes(get_w(), get_h());
509         int slider_w = xS(100);
510         int margin = theme->widget_border;
511         if( (use_wave || use_wave_parade) ) {
512                 int px = wave_x + wave_w - slider_w - xS(5);
513                 int py = wave_y - ScopeGain::calculate_h() - yS(5);
514                 if( !waveform ) {
515                         add_subwindow(waveform = new ScopeWaveform(this,
516                                 wave_x, wave_y, wave_w, wave_h));
517                         waveform->create_objects();
518                         wave_slider = new ScopeWaveSlider(this, px, py, slider_w);
519                         wave_slider->create_objects();
520                 }
521                 else {
522                         waveform->reposition_window(
523                                 wave_x, wave_y, wave_w, wave_h);
524                         waveform->clear_box(0, 0, wave_w, wave_h);
525                         wave_slider->reposition_window(px, py);
526                 }
527         }
528         else if( !(use_wave || use_wave_parade) && waveform ) {
529                 delete waveform;     waveform = 0;
530                 delete wave_slider;  wave_slider = 0;
531         }
532
533         if( use_vector ) {
534                 int vx = vector_x + vector_w - slider_w - xS(5);
535                 int vy = vector_y - ScopeGain::calculate_h() - yS(5);
536                 if( !vectorscope ) {
537                         add_subwindow(vectorscope = new ScopeVectorscope(this,
538                                 vector_x, vector_y, vector_w, vector_h));
539                         vectorscope->create_objects();
540                         vect_slider = new ScopeVectSlider(this, vx, vy, slider_w);
541                         vect_slider->create_objects();
542                         if( use_vector < 0 ) {
543                                 vect_grads = new ScopeVectGrads(this, vector_x, 2*margin);
544                                 add_subwindow(vect_grads);
545                                 vect_grads->create_objects();
546                         }
547                 }
548                 else {
549                         vectorscope->reposition_window(
550                                 vector_x, vector_y, vector_w, vector_h);
551                         vectorscope->clear_box(0, 0, vector_w, vector_h);
552                         vect_slider->reposition_window(vx, vy);
553                         if( use_vector > 0 ) {
554                                 delete vect_grads;  vect_grads = 0;
555                         }
556                         else if( !vect_grads ) {
557                                 vect_grads = new ScopeVectGrads(this, vector_x, 2*margin);
558                                 add_subwindow(vect_grads);
559                                 vect_grads->create_objects();
560                         }
561                         else
562                                 vect_grads->reposition_window(vector_x, 2*margin);
563                 }
564         }
565         else if( !use_vector && vectorscope ) {
566                 delete vectorscope;  vectorscope = 0;
567                 delete vect_slider;  vect_slider = 0;
568                 delete vect_grads;   vect_grads = 0;
569         }
570
571         if( (use_hist || use_hist_parade) ) {
572                 if( !histogram ) {
573 // printf("ScopeGUI::create_panels %d %d %d %d %d\n", __LINE__,
574 //  hist_x, hist_y, hist_w, hist_h);
575                         add_subwindow(histogram = new ScopeHistogram(this,
576                                 hist_x, hist_y, hist_w, hist_h));
577                         histogram->create_objects();
578                 }
579                 else {
580                         histogram->reposition_window(
581                                 hist_x, hist_y, hist_w, hist_h);
582                         histogram->clear_box(0, 0, hist_w, hist_h);
583                 }
584         }
585         else if( !(use_hist || use_hist_parade) ) {
586                 delete histogram;  histogram = 0;
587         }
588
589         allocate_vframes();
590         clear_points(0);
591         draw_overlays(1, 1, 0);
592 }
593
594 void ScopeGUI::clear_points(int flash)
595 {
596         if( histogram )
597                 histogram->clear_point();
598         if( waveform )
599                 waveform->clear_point();
600         if( vectorscope )
601                 vectorscope->clear_point();
602         if( histogram && flash )
603                 histogram->flash(0);
604         if( waveform && flash )
605                 waveform->flash(0);
606         if( vectorscope && flash )
607                 vectorscope->flash(0);
608 }
609
610 void ScopeGUI::toggle_event()
611 {
612 }
613
614 void ScopeGUI::calculate_sizes(int w, int h)
615 {
616         int margin = theme->widget_border;
617         int menu_h = smooth->get_h() + scope_menu->get_h() + margin * 3;
618         int text_w = get_text_width(SMALLFONT, "000") + margin * 2;
619         int total_panels = ((use_hist || use_hist_parade) ? 1 : 0) +
620                 ((use_wave || use_wave_parade) ? 1 : 0) +
621                 (use_vector ? 1 : 0);
622         int x = margin;
623
624         int panel_w = (w - margin) / (total_panels > 0 ? total_panels : 1);
625 // Vectorscope determines the size of everything else
626 // Always last panel
627         vector_w = 0;
628         if( use_vector ) {
629                 vector_x = w - panel_w + text_w;
630                 vector_w = w - margin - vector_x;
631                 vector_y = menu_h;
632                 vector_h = h - vector_y - margin;
633
634                 if( vector_w > vector_h ) {
635                         vector_w = vector_h;
636                         vector_x = w - margin - vector_w;
637                 }
638                 --total_panels;
639                 if(total_panels > 0)
640                         panel_w = (vector_x - text_w - margin) / total_panels;
641         }
642         vector_cx = vector_w / 2;
643         vector_cy = vector_h / 2;
644         radius = bmin(vector_cx, vector_cy);
645
646 // Histogram is always 1st panel
647         if( use_hist || use_hist_parade ) {
648                 hist_x = x;
649                 hist_y = menu_h;
650                 hist_w = panel_w - margin;
651                 hist_h = h - hist_y - margin;
652
653                 --total_panels;
654                 x += panel_w;
655         }
656
657         if( use_wave || use_wave_parade ) {
658                 wave_x = x + text_w;
659                 wave_y = menu_h;
660                 wave_w = panel_w - margin - text_w;
661                 wave_h = h - wave_y - margin;
662         }
663
664 }
665
666
667 void ScopeGUI::allocate_vframes()
668 {
669         if(waveform_vframe) delete waveform_vframe;
670         if(vector_vframe) delete vector_vframe;
671         if(wheel_vframe) delete wheel_vframe;
672
673         int w, h;
674 //printf("ScopeGUI::allocate_vframes %d %d %d %d %d\n", __LINE__,
675 // wave_w, wave_h, vector_w, vector_h);
676         int xs16 = xS(16), ys16 = yS(16);
677         w = MAX(wave_w, xs16);
678         h = MAX(wave_h, ys16);
679         waveform_vframe = new VFrame(w, h, BC_RGB888);
680         w = MAX(vector_w, xs16);
681         h = MAX(vector_h, ys16);
682         vector_vframe = new VFrame(w, h, BC_RGB888);
683         wheel_vframe = 0;
684 }
685
686
687 int ScopeGUI::resize_event(int w, int h)
688 {
689         clear_box(0, 0, w, h);
690         this->w = w;
691         this->h = h;
692         calculate_sizes(w, h);
693         int margin = theme->widget_border;
694
695         if( waveform ) {
696                 waveform->reposition_window(wave_x, wave_y, wave_w, wave_h);
697                 waveform->clear_box(0, 0, wave_w, wave_h);
698                 int px = wave_x + wave_w - wave_slider->get_w() - margin;
699                 int py = wave_y - wave_slider->get_h() - margin;
700                 wave_slider->reposition_window(px, py);
701         }
702
703         if( histogram ) {
704                 histogram->reposition_window(hist_x, hist_y, hist_w, hist_h);
705                 histogram->clear_box(0, 0, hist_w, hist_h);
706         }
707
708         if( vectorscope ) {
709                 vectorscope->reposition_window(vector_x, vector_y, vector_w, vector_h);
710                 vectorscope->clear_box(0, 0, vector_w, vector_h);
711                 int vx = vector_x + vector_w - vect_slider->get_w() - margin;
712                 int vy = vector_y - vect_slider->get_h() - margin;
713                 vect_slider->reposition_window(vx, vy);
714                 if( vect_grads )
715                         vect_grads->reposition_window(vector_x, 2*margin);
716         }
717
718         allocate_vframes();
719         clear_points(0);
720         update_scope();
721         draw_overlays(1, 1, 1);
722         return 1;
723 }
724
725 int ScopeGUI::translation_event()
726 {
727         x = get_x();
728         y = get_y();
729         PluginClientWindow::translation_event();
730         return 0;
731 }
732
733
734 void ScopeGUI::draw_overlays(int overlays, int borders, int flush)
735 {
736         int margin = theme->widget_border;
737         if( overlays && borders ) {
738                 clear_box(0, 0, get_w(), get_h());
739         }
740
741         if( overlays ) {
742                 set_line_dashes(1);
743                 set_color(text_color);
744                 set_font(SMALLFONT);
745
746                 if( histogram && (use_hist || use_hist_parade) ) {
747                         histogram->draw_line(hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), 0,
748                                         hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), hist_h);
749                         histogram->draw_line(hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), 0,
750                                         hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), hist_h);
751                         set_line_dashes(0);
752                         histogram->draw_point();
753                         set_line_dashes(1);
754                         histogram->flash(0);
755                 }
756
757 // Waveform overlay
758                 if( waveform && (use_wave || use_wave_parade) ) {
759                         for( int i=0; i<=WAVEFORM_DIVISIONS; ++i ) {
760                                 int y = wave_h * i / WAVEFORM_DIVISIONS;
761                                 int text_y = y + wave_y + get_text_ascent(SMALLFONT) / 2;
762                                 CLAMP(text_y, waveform->get_y() + get_text_ascent(SMALLFONT), waveform->get_y() + waveform->get_h() - 1);
763                                 char string[BCTEXTLEN];
764                                 sprintf(string, "%d", (int)lround((FLOAT_MAX -
765                                         i * (FLOAT_MAX - FLOAT_MIN) / WAVEFORM_DIVISIONS) * 100));
766                                 int text_x = wave_x - get_text_width(SMALLFONT, string) - margin;
767                                 set_color(text_color);
768                                 draw_text(text_x, text_y, string);
769                                 CLAMP(y, 0, waveform->get_h() - 1);
770                                 set_color(dark_color);
771                                 waveform->draw_line(0, y, wave_w, y);
772                                 waveform->draw_rectangle(0, 0, wave_w, wave_h);
773                         }
774                         set_line_dashes(0);
775                         waveform->draw_point();
776                         set_line_dashes(1);
777                         waveform->flash(0);
778                 }
779 // Vectorscope overlay
780                 if( vectorscope && use_vector ) {
781                         draw_graticule();
782                         set_color(text_color);
783                         vectorscope->draw_point();
784                         vectorscope->flash(0);
785                         set_line_dashes(1);
786                 }
787
788                 set_font(MEDIUMFONT);
789                 set_line_dashes(0);
790         }
791
792         if( borders ) {
793                 if( use_hist || use_hist_parade ) {
794                         draw_3d_border(hist_x - 2, hist_y - 2, hist_w + 4, hist_h + 4,
795                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
796                 }
797                 if( use_wave || use_wave_parade ) {
798                         draw_3d_border(wave_x - 2, wave_y - 2, wave_w + 4, wave_h + 4,
799                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
800                 }
801                 if( use_vector ) {
802                         draw_3d_border(vector_x - 2, vector_y - 2, vector_w + 4, vector_h + 4,
803                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
804                 }
805         }
806
807         flash(0);
808         if(flush) this->flush();
809 }
810
811 void ScopeGUI::draw_graticule()
812 {
813         if( use_vector > 0 ) {
814                 int margin = theme->widget_border;
815                 set_line_dashes(1);
816                 for( int i=1; i<=VECTORSCOPE_DIVISIONS; i+=2 ) {
817                         int y = vector_cy - radius * i / VECTORSCOPE_DIVISIONS;
818                         int text_y = y + vector_y + get_text_ascent(SMALLFONT) / 2;
819                         set_color(text_color);
820                         char string[BCTEXTLEN];
821                         sprintf(string, "%d",
822                                 (int)((FLOAT_MAX / VECTORSCOPE_DIVISIONS * i) * 100));
823                         int text_x = vector_x - get_text_width(SMALLFONT, string) - margin;
824                         draw_text(text_x, text_y, string);
825                         int x = vector_cx - radius * i / VECTORSCOPE_DIVISIONS;
826                         int w = radius * i / VECTORSCOPE_DIVISIONS * 2;
827                         int h = radius * i / VECTORSCOPE_DIVISIONS * 2;
828                         if( i+2 > VECTORSCOPE_DIVISIONS )
829                                 set_line_dashes(0);
830                         set_color(dark_color);
831                         vectorscope->draw_circle(x, y, w, h);
832                 }
833                 float th = TO_RAD(90 + 32.875);
834                 vectorscope->draw_radient(th, 0.1f, .75f, dark_color);
835         }
836         else if( use_vector < 0 ) {
837                 if( grad_image && grad_idx != use_graticule ) {
838                         delete grad_image;   grad_image = 0;
839                         use_graticule = 0;
840                 }
841                 if( !grad_image && grad_idx > 0 ) {
842                         grad_image = VFramePng::vframe_png(grad_paths[grad_idx]);
843                 }
844                 int rr = 2*radius;
845                 if( grad_pixmap && (!use_graticule ||
846                       rr != grad_pixmap->get_w() || rr != grad_pixmap->get_h()) ) {
847                         delete grad_pixmap;  grad_pixmap = 0;
848                         use_graticule = 0;
849                 }
850                 if( !grad_pixmap && grad_image ) {
851                         VFrame grad(rr, rr, BC_RGBA8888);
852                         grad.transfer_from(grad_image);
853                         grad_pixmap = new BC_Pixmap(this, &grad, PIXMAP_ALPHA);
854                         use_graticule = grad_idx;
855                 }
856                 if( grad_pixmap ) {
857                         int px = vector_cx - radius, py = vector_cy - radius;
858                         vectorscope->draw_pixmap(grad_pixmap, px, py);
859 //                      vectorscope->flash(0);
860                 }
861         }
862 }
863
864
865 void ScopeGUI::update_graticule(int idx)
866 {
867         grad_idx = idx;
868         update_scope();
869         toggle_event();
870 }
871
872 void ScopeGUI::draw_colorwheel(VFrame *dst, int bg_color)
873 {
874 // downfactor radius to prevent extreme edge from showing behind graticule
875         float cx = vector_cx, cy = vector_cy, rad = radius * 0.99;
876         int color_model = dst->get_color_model();
877         int bpp = BC_CModels::calculate_pixelsize(color_model);
878         int bg_r = (bg_color>>16) & 0xff;
879         int bg_g = (bg_color>> 8) & 0xff;
880         int bg_b = (bg_color>> 0) & 0xff;
881         int w = dst->get_w(), h = dst->get_h();
882         unsigned char **rows = dst->get_rows();
883         for( int y=0; y<h; ++y ) {
884                 unsigned char *row = rows[y];
885                 for( int x=0; x<w; ++x,row+=bpp ) {
886                         int dx = cx-x, dy = cy-y;
887                         float d = sqrt(dx*dx + dy*dy);
888                         float r, g, b;
889                         if( d < rad ) {
890                                 float h = TO_DEG(atan2(cx-x, cy-y));
891                                 if( h < 0 ) h += 360;
892                                 float s = d / rad, v = 255;
893                                 HSV::hsv_to_rgb(r, g, b, h, s, v);
894                         }
895                         else {
896                                  r = bg_r; g = bg_g; b = bg_b;
897                         }
898                         row[0] = r; row[1] = g; row[2] = b;
899                 }
900         }
901 }
902
903
904 void ScopeGUI::process(VFrame *output_frame)
905 {
906         lock_window("ScopeGUI::process");
907         this->output_frame = output_frame;
908         int ow = output_frame->get_w(), oh = output_frame->get_h();
909         if( use_smooth ) {
910                 VFrame::get_temp(temp_frame, ow, oh, BC_RGB161616);
911                 temp_frame->transfer_from(output_frame);
912                 if( !box_blur ) box_blur = new BoxBlur(cpus);
913                 box_blur->blur(temp_frame, temp_frame, 2, 2);
914                 data_frame = temp_frame;
915         }
916         else
917                 data_frame = output_frame;
918         frame_w = data_frame->get_w();
919         bzero(waveform_vframe->get_data(), waveform_vframe->get_data_size());
920         if( use_vector > 0 )
921                 bzero(vector_vframe->get_data(), vector_vframe->get_data_size());
922         else if( use_vector < 0 ) {
923                 if( wheel_vframe && (
924                      wheel_vframe->get_w() != vector_w ||
925                      wheel_vframe->get_h() != vector_h ) ) {
926                         delete wheel_vframe;  wheel_vframe = 0;
927                 }
928                 if( !wheel_vframe ) {
929                         wheel_vframe = new VFrame(vector_w, vector_h, BC_RGB888);
930                         draw_colorwheel(wheel_vframe, BLACK);
931                 }
932                 vector_vframe->copy_from(wheel_vframe);
933         }
934         engine->process();
935
936         if( histogram )
937                 histogram->draw(0, 0);
938         if( waveform )
939                 waveform->draw_vframe(waveform_vframe);
940         if( vectorscope )
941                 vectorscope->draw_vframe(vector_vframe);
942
943         draw_overlays(1, 0, 1);
944         unlock_window();
945 }
946
947 void ScopeGUI::update_toggles()
948 {
949         scope_menu->update_toggles();
950 }
951
952 ScopePanel::ScopePanel(ScopeGUI *gui, int x, int y, int w, int h)
953  : BC_SubWindow(x, y, w, h, BLACK)
954 {
955         this->gui = gui;
956         is_dragging = 0;
957 }
958
959 void ScopePanel::create_objects()
960 {
961         set_cursor(CROSS_CURSOR, 0, 0);
962         clear_box(0, 0, get_w(), get_h());
963 }
964
965 void ScopePanel::update_point(int x, int y)
966 {
967 }
968
969 void ScopePanel::draw_point()
970 {
971 }
972
973 void ScopePanel::clear_point()
974 {
975 }
976
977 int ScopePanel::button_press_event()
978 {
979         if( is_event_win() && cursor_inside() ) {
980                 gui->clear_points(1);
981                 is_dragging = 1;
982                 int x = get_cursor_x();
983                 int y = get_cursor_y();
984                 CLAMP(x, 0, get_w() - 1);
985                 CLAMP(y, 0, get_h() - 1);
986                 update_point(x, y);
987                 return 1;
988         }
989         return 0;
990 }
991
992
993 int ScopePanel::cursor_motion_event()
994 {
995         if( is_dragging ) {
996                 int x = get_cursor_x();
997                 int y = get_cursor_y();
998                 CLAMP(x, 0, get_w() - 1);
999                 CLAMP(y, 0, get_h() - 1);
1000                 update_point(x, y);
1001                 return 1;
1002         }
1003         return 0;
1004 }
1005
1006
1007 int ScopePanel::button_release_event()
1008 {
1009         if( is_dragging ) {
1010                 is_dragging = 0;
1011                 return 1;
1012         }
1013         return 0;
1014 }
1015
1016
1017 ScopeWaveform::ScopeWaveform(ScopeGUI *gui,
1018                 int x, int y, int w, int h)
1019  : ScopePanel(gui, x, y, w, h)
1020 {
1021         drag_x = -1;
1022         drag_y = -1;
1023 }
1024
1025 void ScopeWaveform::update_point(int x, int y)
1026 {
1027         draw_point();
1028         drag_x = x;
1029         drag_y = y;
1030         int frame_x = x * gui->frame_w / get_w();
1031
1032         if( gui->use_wave_parade ) {
1033                 if( x > get_w() / 3 * 2 )
1034                         frame_x = (x - get_w() / 3 * 2) * gui->frame_w / (get_w() / 3);
1035                 else if( x > get_w() / 3 )
1036                         frame_x = (x - get_w() / 3) * gui->frame_w / (get_w() / 3);
1037                 else
1038                         frame_x = x * gui->frame_w / (get_w() / 3);
1039         }
1040
1041         float value = ((float)get_h() - y) / get_h() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1042         char string[BCTEXTLEN];
1043         sprintf(string, "X: %d Value: %.3f", frame_x, value);
1044         gui->value_text->update(string, 0);
1045
1046         draw_point();
1047         flash(1);
1048 }
1049
1050 void ScopeWaveform::draw_point()
1051 {
1052         if( drag_x >= 0 ) {
1053                 set_inverse();
1054                 set_color(0xffffff);
1055                 set_line_width(2);
1056                 draw_line(0, drag_y, get_w(), drag_y);
1057                 draw_line(drag_x, 0, drag_x, get_h());
1058                 set_line_width(1);
1059                 set_opaque();
1060         }
1061 }
1062
1063 void ScopeWaveform::clear_point()
1064 {
1065         draw_point();
1066         drag_x = -1;
1067         drag_y = -1;
1068 }
1069
1070 ScopeVectorscope::ScopeVectorscope(ScopeGUI *gui,
1071                 int x, int y, int w, int h)
1072  : ScopePanel(gui, x, y, w, h)
1073 {
1074         drag_radius = 0;
1075         drag_angle = 0;
1076 }
1077
1078 void ScopeVectorscope::clear_point()
1079 {
1080 // Hide it
1081         draw_point();
1082         drag_radius = 0;
1083         drag_angle = 0;
1084 }
1085
1086 void ScopeVectorscope::update_point(int x, int y)
1087 {
1088 // Hide it
1089         draw_point();
1090         int dx = x - gui->vector_cx, dy = y - gui->vector_cy;
1091         drag_radius = sqrt(dx*dx + dy*dy);
1092         if( drag_radius > gui->radius ) drag_radius = gui->radius;
1093         float saturation = (float)drag_radius / gui->radius * FLOAT_MAX;
1094         drag_angle = atan2(-dy, dx);
1095         float hue = TO_DEG(drag_angle) - 90;
1096         if( hue < 0 ) hue += 360;
1097
1098         char string[BCTEXTLEN];
1099         sprintf(string, "Hue: %.3f Sat: %.3f", hue, saturation);
1100         gui->value_text->update(string, 0);
1101
1102 // Show it
1103         draw_point();
1104         flash(1);
1105 }
1106
1107 void ScopeVectorscope::draw_point()
1108 {
1109         set_inverse();
1110         draw_point(drag_angle, drag_radius, RED);
1111         set_opaque();
1112 }
1113
1114 void ScopeVectorscope::draw_point(float th, float r, int color)
1115 {
1116         if( r > 0 ) {
1117                 set_color(color);
1118                 set_line_width(2);
1119                 int cx = gui->vector_cx, cy = gui->vector_cy;
1120                 draw_circle(cx-r, cy-r, r*2, r*2);
1121                 set_line_width(1);
1122                 float radius = gui->radius;
1123                 draw_radient(th, 0.f, drag_radius/radius, color);
1124         }
1125 }
1126
1127 void ScopeVectorscope::draw_radient(float th, float r1, float r2, int color)
1128 {
1129         if( r2 > 0 && r2 >= r1 ) {
1130                 set_color(color);
1131                 set_line_width(2);
1132                 int r = gui->radius;
1133                 double cth = r*cos(th), sth = -r*sin(th);
1134                 int cx = gui->vector_cx, cy = gui->vector_cy;
1135                 draw_line(cx+r1*cth, cy+r1*sth, cx+r2*cth, cy+r2*sth);
1136                 set_line_width(1);
1137         }
1138 }
1139
1140
1141 ScopeHistogram::ScopeHistogram(ScopeGUI *gui,
1142                 int x, int y, int w, int h)
1143  : ScopePanel(gui, x, y, w, h)
1144 {
1145         drag_x = -1;
1146 }
1147
1148 void ScopeHistogram::clear_point()
1149 {
1150 // Hide it
1151         draw_point();
1152         drag_x = -1;
1153 }
1154
1155 void ScopeHistogram::draw_point()
1156 {
1157         if( drag_x >= 0 ) {
1158                 set_inverse();
1159                 set_color(0xffffff);
1160                 set_line_width(2);
1161                 draw_line(drag_x, 0, drag_x, get_h());
1162                 set_line_width(1);
1163                 set_opaque();
1164         }
1165 }
1166
1167 void ScopeHistogram::update_point(int x, int y)
1168 {
1169         draw_point();
1170         drag_x = x;
1171         float value = (float)x / get_w() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1172
1173         char string[BCTEXTLEN];
1174         sprintf(string, "Value: %.3f", value);
1175         gui->value_text->update(string, 0);
1176
1177         draw_point();
1178         flash(1);
1179 }
1180
1181
1182
1183 void ScopeHistogram::draw_mode(int mode, int color, int y, int h)
1184 {
1185 // Highest of all bins
1186         int normalize = 1, w = get_w();
1187         int *bin = gui->bins[mode], v;
1188         for( int i=0; i<TOTAL_BINS; ++i )
1189                 if( (v=bin[i]) > normalize ) normalize = v;
1190         double norm = normalize>1 ? log(normalize) : 1e-4;
1191         double vnorm = h / norm;
1192         set_color(color);
1193         for( int x=0; x<w; ++x ) {
1194                 int accum_start = (int)(x * TOTAL_BINS / w);
1195                 int accum_end = (int)((x + 1) * TOTAL_BINS / w);
1196                 CLAMP(accum_start, 0, TOTAL_BINS);
1197                 CLAMP(accum_end, 0, TOTAL_BINS);
1198                 int max = 0;
1199                 for(int i=accum_start; i<accum_end; ++i )
1200                         if( (v=bin[i]) > max ) max = v;
1201 //              max = max * h / normalize;
1202                 max = log(max) * vnorm;
1203                 draw_line(x,y+h - max, x,y+h);
1204         }
1205 }
1206 void ScopeHistogram::draw(int flash, int flush)
1207 {
1208         clear_box(0, 0, get_w(), get_h());
1209         if( gui->use_hist_parade ) {
1210                 draw_mode(0, 0xff0000, 0, get_h() / 3);
1211                 draw_mode(1, 0x00ff00, get_h() / 3, get_h() / 3);
1212                 draw_mode(2, 0x0000ff, get_h() / 3 * 2, get_h() / 3);
1213         }
1214         else {
1215                 draw_mode(3, LTGREY, 0, get_h());
1216         }
1217
1218         if(flash) this->flash(0);
1219         if(flush) this->flush();
1220 }
1221
1222
1223 ScopeScopesOn::ScopeScopesOn(ScopeMenu *scope_menu, const char *text, int id)
1224  : BC_MenuItem(text)
1225 {
1226         this->scope_menu = scope_menu;
1227         this->id = id;
1228 }
1229
1230 int ScopeScopesOn::handle_event()
1231 {
1232         int v = get_checked() ? 0 : 1;
1233         set_checked(v);
1234         ScopeGUI *gui = scope_menu->gui;
1235         switch( id ) {
1236         case SCOPE_HISTOGRAM:
1237                 gui->use_hist = v;
1238                 if( v ) gui->use_hist_parade = 0;
1239                 break;
1240         case SCOPE_HISTOGRAM_RGB:
1241                 gui->use_hist_parade = v;
1242                 if( v ) gui->use_hist = 0;
1243                 break;
1244         case SCOPE_WAVEFORM:
1245                 gui->use_wave = v;
1246                 if( v ) gui->use_wave_parade = 0;
1247                 break;
1248         case SCOPE_WAVEFORM_RGB:
1249                 gui->use_wave_parade = v;
1250                 if( v ) gui->use_wave = 0;
1251                 break;
1252         case SCOPE_WAVEFORM_PLY:
1253                 gui->use_wave_parade = -v;
1254                 if( v ) gui->use_wave = 0;
1255                 break;
1256         case SCOPE_VECTORSCOPE:
1257                 gui->use_vector = v;
1258                 break;
1259         case SCOPE_VECTORWHEEL:
1260                 gui->use_vector = -v;
1261                 break;
1262         }
1263         gui->toggle_event();
1264         gui->update_toggles();
1265         gui->create_panels();
1266         gui->update_scope();
1267         gui->show_window();
1268         return 1;
1269 }
1270
1271 ScopeMenu::ScopeMenu(ScopeGUI *gui, int x, int y)
1272  : BC_PopupMenu(x, y, xS(100), _("Scopes"))
1273 {
1274         this->gui = gui;
1275 }
1276
1277 void ScopeMenu::create_objects()
1278 {
1279         add_item(hist_on =
1280                 new ScopeScopesOn(this, _("Histogram"), SCOPE_HISTOGRAM));
1281         add_item(hist_rgb_on =
1282                 new ScopeScopesOn(this, _("Histogram RGB"), SCOPE_HISTOGRAM_RGB));
1283         add_item(new BC_MenuItem("-"));
1284         add_item(wave_on =
1285                 new ScopeScopesOn(this, _("Waveform"), SCOPE_WAVEFORM));
1286         add_item(wave_rgb_on =
1287                 new ScopeScopesOn(this, _("Waveform RGB"), SCOPE_WAVEFORM_RGB));
1288         add_item(wave_ply_on =
1289                 new ScopeScopesOn(this, _("Waveform ply"), SCOPE_WAVEFORM_PLY));
1290         add_item(new BC_MenuItem("-"));
1291         add_item(vect_on =
1292                 new ScopeScopesOn(this, _("Vectorscope"), SCOPE_VECTORSCOPE));
1293         add_item(vect_wheel =
1294                 new ScopeScopesOn(this, _("VectorWheel"), SCOPE_VECTORWHEEL));
1295 }
1296
1297 void ScopeMenu::update_toggles()
1298 {
1299         hist_on->set_checked(gui->use_hist);
1300         hist_rgb_on->set_checked(gui->use_hist_parade);
1301         wave_on->set_checked(gui->use_wave);
1302         wave_rgb_on->set_checked(gui->use_wave_parade>0);
1303         wave_ply_on->set_checked(gui->use_wave_parade<0);
1304         vect_on->set_checked(gui->use_vector>0);
1305         vect_wheel->set_checked(gui->use_vector<0);
1306 }
1307
1308
1309 ScopeVectGrads::ScopeVectGrads(ScopeGUI *gui, int x, int y)
1310  : BC_PopupMenu(x, y, _("Overlay"))
1311 {
1312         this->gui = gui;
1313 }
1314
1315 #define SCOPE_SEARCHPATH "/scopes"
1316 void ScopeVectGrads::create_objects()
1317 {
1318         gui->grad_paths.remove_all_objects();
1319         ScopeGradItem *item;
1320         add_item(item = new ScopeGradItem(this, _("none"), 0));
1321         if( item->idx == gui->grad_idx ) item->set_checked(1);
1322         gui->grad_paths.append(0);
1323         FileSystem fs;
1324         fs.set_filter("[*.png]");
1325         char scope_path[BCTEXTLEN];
1326         sprintf(scope_path, "%s%s", File::get_plugin_path(), SCOPE_SEARCHPATH);
1327         fs.update(scope_path);
1328         for( int i=0; i<fs.total_files(); ++i ) {
1329                 FileItem *file_item = fs.get_entry(i);
1330                 if( file_item->get_is_dir() ) continue;
1331                 strcpy(scope_path, file_item->get_name());
1332                 char *cp = strrchr(scope_path, '.');
1333                 if( cp ) *cp = 0;
1334                 add_item(item = new ScopeGradItem(this, scope_path, gui->grad_paths.size()));
1335                 if( item->idx == gui->grad_idx ) item->set_checked(1);
1336                 gui->grad_paths.append(cstrdup(file_item->get_path()));
1337         }
1338 }
1339
1340 ScopeGradItem::ScopeGradItem(ScopeVectGrads *vect_grads, const char *text, int idx)
1341  : BC_MenuItem(text)
1342 {
1343         this->vect_grads = vect_grads;
1344         this->idx = idx;
1345 }
1346
1347 int ScopeGradItem::handle_event()
1348 {
1349         for( int i=0,n=vect_grads->total_items(); i<n; ++i ) {
1350                 ScopeGradItem *item = (ScopeGradItem *)vect_grads->get_item(i);
1351                 item->set_checked(item->idx == idx);
1352         }       
1353         vect_grads->gui->update_graticule(idx);
1354         return 1;
1355 }
1356
1357
1358 ScopeGainReset::ScopeGainReset(ScopeGain *gain, int x, int y)
1359  : BC_Button(x, y, gain->gui->theme->get_image_set("reset_button"))
1360 {
1361         this->gain = gain;
1362 }
1363
1364 int ScopeGainReset::calculate_w(BC_Theme *theme)
1365 {
1366         VFrame *vfrm = *theme->get_image_set("reset_button");
1367         return vfrm->get_w();
1368 }
1369
1370 int ScopeGainReset::handle_event()
1371 {
1372         gain->slider->update(5);
1373         return gain->handle_event();
1374 }
1375
1376 ScopeGainSlider::ScopeGainSlider(ScopeGain *gain, int x, int y, int w)
1377  : BC_ISlider(x, y, 0, w, w, 1, 9, *gain->value)
1378 {
1379         this->gain = gain;
1380 }
1381
1382 int ScopeGainSlider::handle_event()
1383 {
1384         return gain->handle_event();
1385 }
1386
1387 ScopeGain::ScopeGain(ScopeGUI *gui, int x, int y, int w, int *value)
1388 {
1389         this->gui = gui;
1390         this->x = x;
1391         this->y = y;
1392         this->w = w;
1393         this->value = value;
1394
1395         slider = 0;
1396         reset = 0;
1397 }
1398 ScopeGain::~ScopeGain()
1399 {
1400         delete reset;
1401         delete slider;
1402 }
1403
1404 int ScopeGain::calculate_h()
1405 {
1406         return BC_ISlider::get_span(0);
1407 }
1408
1409 void ScopeGain::create_objects()
1410 {
1411         reset_w = ScopeGainReset::calculate_w(gui->theme);
1412         gui->add_subwindow(slider = new ScopeGainSlider(this, x, y, w-reset_w-xS(5)));
1413         gui->add_subwindow(reset = new ScopeGainReset(this, x+w-reset_w, y));
1414 }
1415
1416 int ScopeGain::handle_event()
1417 {
1418         *value = slider->get_value();
1419         gui->update_scope();
1420         gui->toggle_event();
1421         return 1;
1422 }
1423
1424 void ScopeGain::reposition_window(int x, int y)
1425 {
1426         this->x = x;  this->y = y;
1427         slider->reposition_window(x, y);
1428         reset->reposition_window(x+w-reset_w, y);
1429 }
1430
1431 ScopeWaveSlider::ScopeWaveSlider(ScopeGUI *gui, int x, int y, int w)
1432  : ScopeGain(gui, x, y, w, &gui->use_wave_gain)
1433 {
1434 }
1435
1436 ScopeVectSlider::ScopeVectSlider(ScopeGUI *gui, int x, int y, int w)
1437  : ScopeGain(gui, x, y, w, &gui->use_vect_gain)
1438 {
1439 }
1440
1441 ScopeSmooth::ScopeSmooth(ScopeGUI *gui, int x, int y)
1442  : BC_CheckBox(x, y, gui->use_smooth, _("Smooth"))
1443 {
1444         this->gui = gui;
1445 }
1446
1447 int ScopeSmooth::handle_event()
1448 {
1449         gui->use_smooth = get_value();
1450         gui->update_scope();
1451         gui->toggle_event();
1452         return 1;
1453 }
1454