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