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