36883b68d5da6355c0db01966913435a48838d5e
[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 "clip.h"
24 #include "cursors.h"
25 #include "language.h"
26 #include "scopewindow.h"
27 #include "theme.h"
28
29 #include <string.h>
30
31 ScopePackage::ScopePackage()
32  : LoadPackage()
33 {
34 }
35
36 ScopeUnit::ScopeUnit(ScopeGUI *gui,
37         ScopeEngine *server)
38  : LoadClient(server)
39 {
40         this->gui = gui;
41 }
42
43 #define BPP 3
44 #define incr_point(rows,h, n, comp) \
45 if(iy >= 0 && iy < h) { int v; \
46   uint8_t *vp = rows[iy] + ix*BPP + (comp); \
47   v = *vp+(n);  *vp = v>0xff ? 0xff : v; \
48 }
49 #define incr_points(rows,h, rv, gv, bv) \
50 if(iy >= 0 && iy < h) { int v; \
51   uint8_t *vp = rows[iy] + ix*BPP; \
52   v = *vp+(rv);  *vp++ = v>0xff ? 0xff : v; \
53   v = *vp+(gv);  *vp++ = v>0xff ? 0xff : v; \
54   v = *vp+(bv);  *vp   = v>0xff ? 0xff : v; \
55 }
56 #define clip_points(rows,h, rv, gv, bv) \
57 if(iy >= 0 && iy < h) { int v; \
58   uint8_t *vp = rows[iy] + ix*BPP; \
59   v = *vp+(rv);  *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
60   v = *vp+(gv);  *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
61   v = *vp+(bv);  *vp   = v>0xff ? 0xff : v<0 ? 0 : v; \
62 }
63
64 #define PROCESS_PIXEL(column) { \
65 /* Calculate histogram */ \
66         if(use_hist) { \
67                 int v_i = (intensity - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
68                 CLAMP(v_i, 0, TOTAL_BINS - 1); \
69                 bins[3][v_i]++; \
70         } \
71         if(use_hist_parade) { \
72                 int r_i = (r - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
73                 int g_i = (g - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
74                 int b_i = (b - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
75                 CLAMP(r_i, 0, TOTAL_BINS - 1); \
76                 CLAMP(g_i, 0, TOTAL_BINS - 1); \
77                 CLAMP(b_i, 0, TOTAL_BINS - 1); \
78                 bins[0][r_i]++; \
79                 bins[1][g_i]++; \
80                 bins[2][b_i]++; \
81         } \
82 /* Calculate waveform */ \
83         if(use_wave || use_wave_parade) { \
84                 int ix = (column) * wave_w / out_w; \
85                 if(ix >= 0 && ix < wave_w) { \
86                         if(use_wave_parade > 0) { \
87                                 ix /= 3; \
88                                 int iy = wave_h - (int)((r - FLOAT_MIN) /  \
89                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
90                                 incr_point(waveform_rows,wave_h, winc, 0); \
91                                 ix += wave_w/3; \
92                                 iy = wave_h - (int)((g - FLOAT_MIN) /  \
93                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
94                                 incr_point(waveform_rows,wave_h, winc, 1); \
95                                 ix += wave_w/3; \
96                                 iy = wave_h - (int)((b - FLOAT_MIN) /  \
97                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
98                                 incr_point(waveform_rows,wave_h, winc, 2); \
99                         } \
100                         else if(use_wave_parade < 0) { \
101                                 int iy = wave_h - (int)((r - FLOAT_MIN) /  \
102                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
103                                 clip_points(waveform_rows,wave_h, winc, -winc, -winc); \
104                                 iy = wave_h - (int)((g - FLOAT_MIN) /  \
105                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
106                                 clip_points(waveform_rows,wave_h, -winc, winc, -winc); \
107                                 iy = wave_h - (int)((b - FLOAT_MIN) /  \
108                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
109                                 clip_points(waveform_rows,wave_h, -winc, -winc, winc); \
110                         } \
111                         else { int yinc = 3*winc; \
112                                 int rinc = yinc*(r-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
113                                 int ginc = yinc*(g-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
114                                 int binc = yinc*(b-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
115                                 int iy = wave_h - ((intensity - FLOAT_MIN) /  \
116                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
117                                 incr_points(waveform_rows,wave_h, rinc, ginc, binc); \
118                         } \
119                 } \
120         } \
121 /* Calculate vectorscope */ \
122         if(use_vector) { \
123                 float th = 90 - h; \
124                 if( th < 0 ) th += 360; \
125                 double t = TO_RAD(th); \
126                 float adjacent = -cos(t), opposite = sin(t); \
127                 int ix = vector_w / 2 + adjacent * (s) / (FLOAT_MAX) * radius; \
128                 int iy = vector_h / 2 - opposite * (s) / (FLOAT_MAX) * radius; \
129                 CLAMP(ix, 0, vector_w - 1); \
130         /* Get color with full saturation & value */ \
131                 float r_f, g_f, b_f; \
132                 if( (h=h-90) < 0 ) h += 360; \
133                 HSV::hsv_to_rgb(r_f, g_f, b_f, h, s, 1); \
134                 int rv = CLIP(r_f, 0, 1) * vinc + 3; \
135                 int gv = CLIP(g_f, 0, 1) * vinc + 3; \
136                 int bv = CLIP(b_f, 0, 1) * vinc + 3; \
137                 incr_points(vector_rows,vector_h, rv, gv, bv); \
138         } \
139 }
140
141 #define PROCESS_RGB_PIXEL(column, max) { \
142         r = (float)*row++ / max; \
143         g = (float)*row++ / max; \
144         b = (float)*row++ / max; \
145         HSV::rgb_to_hsv(r, g, b, h, s, v); \
146         intensity = v; \
147         PROCESS_PIXEL(column) \
148 }
149
150 #define PROCESS_BGR_PIXEL(column, max) { \
151         b = (float)*row++ / max; \
152         g = (float)*row++ / max; \
153         r = (float)*row++ / max; \
154         HSV::rgb_to_hsv(r, g, b, h, s, v); \
155         intensity = v; \
156         PROCESS_PIXEL(column) \
157 }
158
159 #define PROCESS_YUV_PIXEL(column, y_in, u_in, v_in) { \
160         YUV::yuv.yuv_to_rgb_f(r, g, b, y_in, u_in, v_in); \
161         HSV::rgb_to_hsv(r, g, b, h, s, v); \
162         intensity = v; \
163         PROCESS_PIXEL(column) \
164 }
165
166 void ScopeUnit::process_package(LoadPackage *package)
167 {
168         ScopePackage *pkg = (ScopePackage*)package;
169
170         float r, g, b;
171         float h, s, v;
172         float intensity;
173         int use_hist = gui->use_hist;
174         int use_hist_parade = gui->use_hist_parade;
175         int use_vector = gui->use_vector;
176         int use_wave = gui->use_wave;
177         int use_wave_parade = gui->use_wave_parade;
178         VFrame *waveform_vframe = gui->waveform_vframe;
179         VFrame *vector_vframe = gui->vector_vframe;
180         int wave_h = waveform_vframe->get_h();
181         int wave_w = waveform_vframe->get_w();
182         int vector_h = vector_vframe->get_h();
183         int vector_w = vector_vframe->get_w();
184         int out_w = gui->output_frame->get_w();
185         int out_h = gui->output_frame->get_h();
186         int winc = (wave_w * wave_h) / (out_w * out_h);
187         if( use_wave_parade ) winc *= 3;
188         winc += 2;  winc *= gui->wdial;
189         int vinc = 3*(vector_w * vector_h) / (out_w * out_h) + 2;
190         vinc *= gui->vdial;
191         float radius = MIN(gui->vector_w / 2, gui->vector_h / 2);
192         unsigned char **waveform_rows = waveform_vframe->get_rows();
193         unsigned char **vector_rows = vector_vframe->get_rows();
194         unsigned char **rows = gui->output_frame->get_rows();
195
196         switch( gui->output_frame->get_color_model() ) {
197         case BC_RGB888:
198                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
199                         unsigned char *row = rows[y];
200                         for( int x=0; x<out_w; ++x ) {
201                                 PROCESS_RGB_PIXEL(x, 255)
202                         }
203                 }
204                 break;
205         case BC_RGBA8888:
206                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
207                         unsigned char *row = rows[y];
208                         for( int x=0; x<out_w; ++x ) {
209                                 PROCESS_RGB_PIXEL(x, 255)
210                                 ++row;
211                         }
212                 }
213                 break;
214         case BC_BGR888:
215                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
216                         unsigned char *row = rows[y];
217                         for( int x=0; x<out_w; ++x ) {
218                                 PROCESS_BGR_PIXEL(x, 255)
219                         }
220                 }
221                 break;
222         case BC_BGR8888:
223                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
224                         unsigned char *row = rows[y];
225                         for( int x=0; x<out_w; ++x ) {
226                                 PROCESS_BGR_PIXEL(x, 255)
227                                 ++row;
228                         }
229                 }
230                 break;
231         case BC_RGB_FLOAT:
232                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
233                         float *row = (float*)rows[y];
234                         for( int x=0; x<out_w; ++x ) {
235                                 PROCESS_RGB_PIXEL(x, 1.0)
236                         }
237                 }
238                 break;
239         case BC_RGBA_FLOAT:
240                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
241                         float *row = (float*)rows[y];
242                         for( int x=0; x<out_w; ++x ) {
243                                 PROCESS_RGB_PIXEL(x, 1.0)
244                                 ++row;
245                         }
246                 }
247                 break;
248         case BC_YUV888:
249                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
250                         unsigned char *row = rows[y];
251                         for( int x=0; x<out_w; ++x ) {
252                                 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
253                                 row += 3;
254                         }
255                 }
256                 break;
257
258         case BC_YUVA8888:
259                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
260                         unsigned char *row = rows[y];
261                         for( int x=0; x<out_w; ++x ) {
262                                 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
263                                 row += 4;
264                         }
265                 }
266                 break;
267         case BC_YUV420P: {
268                 unsigned char *yp = gui->output_frame->get_y();
269                 unsigned char *up = gui->output_frame->get_u();
270                 unsigned char *vp = gui->output_frame->get_v();
271                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
272                         unsigned char *y_row = yp + y * out_w;
273                         unsigned char *u_row = up + (y / 2) * (out_w / 2);
274                         unsigned char *v_row = vp + (y / 2) * (out_w / 2);
275                         for( int x=0; x<out_w; x+=2 ) {
276                                 PROCESS_YUV_PIXEL(x, *y_row, *u_row, *v_row);
277                                 ++y_row;
278                                 PROCESS_YUV_PIXEL(x + 1, *y_row, *u_row, *v_row);
279                                 ++y_row;
280                                 ++u_row;  ++v_row;
281                         }
282                 }
283                 break; }
284         case BC_YUV422:
285                 for( int y=pkg->row1; y<pkg->row2; ++y ) {
286                         unsigned char *row = rows[y];
287                         for( int x=0; x<out_w; x+=2 ) {
288                                 PROCESS_YUV_PIXEL(x, row[0], row[1], row[3]);
289                                 PROCESS_YUV_PIXEL(x + 1, row[2], row[1], row[3]);
290                                 row += 4;
291                         }
292                 }
293                 break;
294
295         default:
296                 printf("ScopeUnit::process_package %d: color_model=%d unrecognized\n",
297                         __LINE__, gui->output_frame->get_color_model());
298                 break;
299         }
300 }
301
302
303 ScopeEngine::ScopeEngine(ScopeGUI *gui, int cpus)
304  : LoadServer(cpus, cpus)
305 {
306 //printf("ScopeEngine::ScopeEngine %d cpus=%d\n", __LINE__, cpus);
307         this->gui = gui;
308 }
309
310 ScopeEngine::~ScopeEngine()
311 {
312 }
313
314 void ScopeEngine::init_packages()
315 {
316         int y = 0, h = gui->output_frame->get_h();
317         for( int i=0,n=LoadServer::get_total_packages(); i<n; ) {
318                 ScopePackage *pkg = (ScopePackage*)get_package(i);
319                 pkg->row1 = y;
320                 pkg->row2 = y = (++i * h) / n;
321         }
322
323         for( int i=0,n=LoadServer::get_total_packages(); i<n; ++i ) {
324                 ScopeUnit *unit = (ScopeUnit*)get_client(i);
325                 for( int j=0; j<HIST_SECTIONS; ++j )
326                         bzero(unit->bins[j], sizeof(int) * TOTAL_BINS);
327         }
328 }
329
330
331 LoadClient* ScopeEngine::new_client()
332 {
333         return new ScopeUnit(gui, this);
334 }
335
336 LoadPackage* ScopeEngine::new_package()
337 {
338         return new ScopePackage;
339 }
340
341 void ScopeEngine::process()
342 {
343         process_packages();
344
345         for(int i = 0; i < HIST_SECTIONS; i++)
346                 bzero(gui->bins[i], sizeof(int) * TOTAL_BINS);
347
348         for(int i=0,n=get_total_clients(); i<n; ++i ) {
349                 ScopeUnit *unit = (ScopeUnit*)get_client(i);
350                 for( int j=0; j<HIST_SECTIONS; ++j ) {
351                         int *bp = gui->bins[j], *up = unit->bins[j];
352                         for( int k=TOTAL_BINS; --k>=0; ++bp,++up ) *bp += *up;
353                 }
354         }
355 }
356
357
358 ScopeGUI::ScopeGUI(Theme *theme,
359         int x, int y, int w, int h, int cpus)
360  : PluginClientWindow(_(PROGRAM_NAME ": Scopes"),
361         x, y, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
362 {
363         this->x = x;
364         this->y = y;
365         this->w = w;
366         this->h = h;
367         this->theme = theme;
368         this->cpus = cpus;
369         reset();
370 }
371
372 ScopeGUI::ScopeGUI(PluginClient *plugin, int w, int h)
373  : PluginClientWindow(plugin, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
374 {
375         this->x = get_x();
376         this->y = get_y();
377         this->w = w;
378         this->h = h;
379         this->theme = plugin->get_theme();
380         this->cpus = plugin->PluginClient::smp + 1;
381         reset();
382 }
383
384 ScopeGUI::~ScopeGUI()
385 {
386         delete waveform_vframe;
387         delete vector_vframe;
388         delete engine;
389 }
390
391 void ScopeGUI::reset()
392 {
393         frame_w = 1;
394         waveform_vframe = 0;
395         vector_vframe = 0;
396         engine = 0;
397         use_hist = 0;
398         use_wave = 1;
399         use_vector = 1;
400         use_hist_parade = 0;
401         use_wave_parade = 0;
402         waveform = 0;
403         vectorscope = 0;
404         histogram = 0;
405         wave_w = wave_h = vector_w = vector_h = 0;
406         wdial = vdial = 5.f;
407 }
408
409
410 void ScopeGUI::create_objects()
411 {
412         if( use_hist && use_hist_parade )
413                 use_hist = 0;
414         if( use_wave && use_wave_parade )
415                 use_wave = 0;
416         if( !engine ) engine = new ScopeEngine(this, cpus);
417
418         lock_window("ScopeGUI::create_objects");
419         int x = theme->widget_border;
420         int y = theme->widget_border +
421                 ScopeWaveDial::calculate_h() - ScopeMenu::calculate_h();
422         add_subwindow(scope_menu = new ScopeMenu(this, x, y));
423         scope_menu->create_objects();
424         x += scope_menu->get_w() + theme->widget_border;
425         add_subwindow(value_text = new BC_Title(x, y, ""));
426
427         create_panels();
428         update_toggles();
429         show_window();
430         update_scope();
431         unlock_window();
432 }
433
434 void ScopeGUI::create_panels()
435 {
436         calculate_sizes(get_w(), get_h());
437         if( (use_wave || use_wave_parade) ) {
438                 int px = wave_x + wave_w - ScopeWaveDial::calculate_w() - xS(5);
439                 int py = wave_y - ScopeWaveDial::calculate_h() - yS(5);
440                 if( !waveform ) {
441                         add_subwindow(waveform = new ScopeWaveform(this,
442                                 wave_x, wave_y, wave_w, wave_h));
443                         waveform->create_objects();
444                         add_subwindow(wave_dial = new ScopeWaveDial(this, px, py));
445                 }
446                 else {
447                         waveform->reposition_window(
448                                 wave_x, wave_y, wave_w, wave_h);
449                         waveform->clear_box(0, 0, wave_w, wave_h);
450                         wave_dial->reposition_window(px, py);
451                 }
452         }
453         else if( !(use_wave || use_wave_parade) && waveform ) {
454                 delete waveform;   waveform = 0;
455                 delete wave_dial;  wave_dial = 0;
456         }
457
458         if( use_vector ) {
459                 int vx = vector_x + vector_w - ScopeVectDial::calculate_w() - xS(5);
460                 int vy = vector_y - ScopeVectDial::calculate_h() - yS(5);
461                 if( !vectorscope ) {
462                         add_subwindow(vectorscope = new ScopeVectorscope(this,
463                                 vector_x, vector_y, vector_w, vector_h));
464                         vectorscope->create_objects();
465                         add_subwindow(vect_dial = new ScopeVectDial(this, vx, vy));
466                 }
467                 else {
468                         vectorscope->reposition_window(
469                                 vector_x, vector_y, vector_w, vector_h);
470                         vectorscope->clear_box(0, 0, vector_w, vector_h);
471                         vect_dial->reposition_window(vx, vy);
472                 }
473         }
474         else if( !use_vector && vectorscope ) {
475                 delete vectorscope;  vectorscope = 0;
476                 delete vect_dial;    vect_dial = 0;
477         }
478
479         if( (use_hist || use_hist_parade) ) {
480                 if( !histogram ) {
481 // printf("ScopeGUI::create_panels %d %d %d %d %d\n", __LINE__,
482 //  hist_x, hist_y, hist_w, hist_h);
483                         add_subwindow(histogram = new ScopeHistogram(this,
484                                 hist_x, hist_y, hist_w, hist_h));
485                         histogram->create_objects();
486                 }
487                 else {
488                         histogram->reposition_window(
489                                 hist_x, hist_y, hist_w, hist_h);
490                         histogram->clear_box(0, 0, hist_w, hist_h);
491                 }
492         }
493         else if( !(use_hist || use_hist_parade) ) {
494                 delete histogram;  histogram = 0;
495         }
496
497         allocate_vframes();
498         clear_points(0);
499         draw_overlays(1, 1, 0);
500 }
501
502 void ScopeGUI::clear_points(int flash)
503 {
504         if( histogram )
505                 histogram->clear_point();
506         if( waveform )
507                 waveform->clear_point();
508         if( vectorscope )
509                 vectorscope->clear_point();
510         if( histogram && flash )
511                 histogram->flash(0);
512         if( waveform && flash )
513                 waveform->flash(0);
514         if( vectorscope && flash )
515                 vectorscope->flash(0);
516 }
517
518 void ScopeGUI::toggle_event()
519 {
520 }
521
522 void ScopeGUI::calculate_sizes(int w, int h)
523 {
524         int margin = theme->widget_border;
525         int menu_h = ScopeWaveDial::calculate_h() + margin * 2;
526         int text_w = get_text_width(SMALLFONT, "000") + margin * 2;
527         int total_panels = ((use_hist || use_hist_parade) ? 1 : 0) +
528                 ((use_wave || use_wave_parade) ? 1 : 0) +
529                 (use_vector ? 1 : 0);
530         int x = margin;
531
532         int panel_w = (w - margin) / (total_panels > 0 ? total_panels : 1);
533 // Vectorscope determines the size of everything else
534 // Always last panel
535         vector_w = 0;
536         if( use_vector ) {
537                 vector_x = w - panel_w + text_w;
538                 vector_w = w - margin - vector_x;
539                 vector_y = menu_h;
540                 vector_h = h - vector_y - margin;
541
542                 if( vector_w > vector_h ) {
543                         vector_w = vector_h;
544                         vector_x = w - theme->widget_border - vector_w;
545                 }
546                 --total_panels;
547                 if(total_panels > 0)
548                         panel_w = (vector_x - text_w - margin) / total_panels;
549         }
550
551 // Histogram is always 1st panel
552         if( use_hist || use_hist_parade ) {
553                 hist_x = x;
554                 hist_y = menu_h;
555                 hist_w = panel_w - margin;
556                 hist_h = h - hist_y - margin;
557
558                 --total_panels;
559                 x += panel_w;
560         }
561
562         if( use_wave || use_wave_parade ) {
563                 wave_x = x + text_w;
564                 wave_y = menu_h;
565                 wave_w = panel_w - margin - text_w;
566                 wave_h = h - wave_y - margin;
567         }
568
569 }
570
571
572 void ScopeGUI::allocate_vframes()
573 {
574         if(waveform_vframe) delete waveform_vframe;
575         if(vector_vframe) delete vector_vframe;
576
577         int w, h;
578 //printf("ScopeGUI::allocate_vframes %d %d %d %d %d\n", __LINE__,
579 // wave_w, wave_h, vector_w, vector_h);
580         int xs16 = xS(16), ys16 = yS(16);
581         w = MAX(wave_w, xs16);
582         h = MAX(wave_h, ys16);
583         waveform_vframe = new VFrame(w, h, BC_RGB888);
584         w = MAX(vector_w, xs16);
585         h = MAX(vector_h, ys16);
586         vector_vframe = new VFrame(w, h, BC_RGB888);
587 }
588
589
590 int ScopeGUI::resize_event(int w, int h)
591 {
592         clear_box(0, 0, w, h);
593         this->w = w;
594         this->h = h;
595         calculate_sizes(w, h);
596
597         if( waveform ) {
598                 waveform->reposition_window(wave_x, wave_y, wave_w, wave_h);
599                 waveform->clear_box(0, 0, wave_w, wave_h);
600                 int px = wave_x + wave_w - ScopeWaveDial::calculate_w() - xS(5);
601                 int py = wave_y - ScopeWaveDial::calculate_h() - yS(5);
602                 wave_dial->reposition_window(px, py);
603         }
604
605         if( histogram ) {
606                 histogram->reposition_window(hist_x, hist_y, hist_w, hist_h);
607                 histogram->clear_box(0, 0, hist_w, hist_h);
608         }
609
610         if( vectorscope ) {
611                 vectorscope->reposition_window(vector_x, vector_y, vector_w, vector_h);
612                 vectorscope->clear_box(0, 0, vector_w, vector_h);
613                 int vx = vector_x + vector_w - ScopeVectDial::calculate_w() - xS(5);
614                 int vy = vector_y - ScopeVectDial::calculate_h() - yS(5);
615                 vect_dial->reposition_window(vx, vy);
616         }
617
618         allocate_vframes();
619         clear_points(0);
620         update_scope();
621         draw_overlays(1, 1, 1);
622         return 1;
623 }
624
625 int ScopeGUI::translation_event()
626 {
627         x = get_x();
628         y = get_y();
629         PluginClientWindow::translation_event();
630         return 0;
631 }
632
633
634 void ScopeGUI::draw_overlays(int overlays, int borders, int flush)
635 {
636         BC_Resources *resources = BC_WindowBase::get_resources();
637         int text_color = GREEN;
638         int dark_color = (text_color>>2) & 0x3f3f3f;
639         if( resources->bg_color == 0xffffff ) {
640                 text_color = dark_color;
641         }
642
643         if( overlays && borders ) {
644                 clear_box(0, 0, get_w(), get_h());
645         }
646
647         if( overlays ) {
648                 set_line_dashes(1);
649                 set_color(text_color);
650                 set_font(SMALLFONT);
651
652                 if( histogram && (use_hist || use_hist_parade) ) {
653                         histogram->draw_line(hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), 0,
654                                         hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), hist_h);
655                         histogram->draw_line(hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), 0,
656                                         hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), hist_h);
657                         set_line_dashes(0);
658                         histogram->draw_point();
659                         set_line_dashes(1);
660                         histogram->flash(0);
661                 }
662
663 // Waveform overlay
664                 if( waveform && (use_wave || use_wave_parade) ) {
665                         for( int i=0; i<=WAVEFORM_DIVISIONS; ++i ) {
666                                 int y = wave_h * i / WAVEFORM_DIVISIONS;
667                                 int text_y = y + wave_y + get_text_ascent(SMALLFONT) / 2;
668                                 CLAMP(text_y, waveform->get_y() + get_text_ascent(SMALLFONT), waveform->get_y() + waveform->get_h() - 1);
669                                 char string[BCTEXTLEN];
670                                 sprintf(string, "%d", (int)lround((FLOAT_MAX -
671                                         i * (FLOAT_MAX - FLOAT_MIN) / WAVEFORM_DIVISIONS) * 100));
672                                 int text_x = wave_x - get_text_width(SMALLFONT, string) - theme->widget_border;
673                                 set_color(text_color);
674                                 draw_text(text_x, text_y, string);
675                                 CLAMP(y, 0, waveform->get_h() - 1);
676                                 set_color(dark_color);
677                                 waveform->draw_line(0, y, wave_w, y);
678                                 waveform->draw_rectangle(0, 0, wave_w, wave_h);
679                         }
680                         set_line_dashes(0);
681                         waveform->draw_point();
682                         set_line_dashes(1);
683                         waveform->flash(0);
684                 }
685 // Vectorscope overlay
686                 if( vectorscope && use_vector ) {
687                         set_line_dashes(1);
688                         int radius = MIN(vector_w / 2, vector_h / 2);
689                         for( int i=1; i<=VECTORSCOPE_DIVISIONS; i+=2 ) {
690                                 int y = vector_h / 2 - radius * i / VECTORSCOPE_DIVISIONS;
691                                 int text_y = y + vector_y + get_text_ascent(SMALLFONT) / 2;
692                                 set_color(text_color);
693                                 char string[BCTEXTLEN];
694                                 sprintf(string, "%d",
695                                         (int)((FLOAT_MAX / VECTORSCOPE_DIVISIONS * i) * 100));
696                                 int text_x = vector_x - get_text_width(SMALLFONT, string) - theme->widget_border;
697                                 draw_text(text_x, text_y, string);
698                                 int x = vector_w / 2 - radius * i / VECTORSCOPE_DIVISIONS;
699                                 int w = radius * i / VECTORSCOPE_DIVISIONS * 2;
700                                 int h = radius * i / VECTORSCOPE_DIVISIONS * 2;
701                                 if( i+2 > VECTORSCOPE_DIVISIONS )
702                                         set_line_dashes(0);
703                                 set_color(dark_color);
704                                 vectorscope->draw_circle(x, y, w, h);
705                         }
706                         set_color(text_color);
707                         vectorscope->draw_point();
708                         set_line_dashes(1);
709                         vectorscope->flash(0);
710                 }
711
712                 set_font(MEDIUMFONT);
713                 set_line_dashes(0);
714         }
715
716         if( borders ) {
717                 if( use_hist || use_hist_parade ) {
718                         draw_3d_border(hist_x - 2, hist_y - 2, hist_w + 4, hist_h + 4,
719                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
720                 }
721                 if( use_wave || use_wave_parade ) {
722                         draw_3d_border(wave_x - 2, wave_y - 2, wave_w + 4, wave_h + 4,
723                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
724                 }
725                 if( use_vector ) {
726                         draw_3d_border(vector_x - 2, vector_y - 2, vector_w + 4, vector_h + 4,
727                                 get_bg_color(), BLACK, MDGREY, get_bg_color());
728                 }
729         }
730
731         flash(0);
732         if(flush) this->flush();
733 }
734
735
736
737 void ScopeGUI::process(VFrame *output_frame)
738 {
739         lock_window("ScopeGUI::process");
740         this->output_frame = output_frame;
741         frame_w = output_frame->get_w();
742         //float radius = MIN(vector_w / 2, vector_h / 2);
743         bzero(waveform_vframe->get_data(), waveform_vframe->get_data_size());
744         bzero(vector_vframe->get_data(), vector_vframe->get_data_size());
745         engine->process();
746
747         if( histogram )
748                 histogram->draw(0, 0);
749         if( waveform )
750                 waveform->draw_vframe(waveform_vframe, 1, 0, 0);
751         if( vectorscope )
752                 vectorscope->draw_vframe(vector_vframe, 1, 0, 0);
753
754         draw_overlays(1, 0, 1);
755         unlock_window();
756 }
757
758 void ScopeGUI::update_toggles()
759 {
760         scope_menu->update_toggles();
761 }
762
763 ScopePanel::ScopePanel(ScopeGUI *gui, int x, int y, int w, int h)
764  : BC_SubWindow(x, y, w, h, BLACK)
765 {
766         this->gui = gui;
767         is_dragging = 0;
768 }
769
770 void ScopePanel::create_objects()
771 {
772         set_cursor(CROSS_CURSOR, 0, 0);
773         clear_box(0, 0, get_w(), get_h());
774 }
775
776 void ScopePanel::update_point(int x, int y)
777 {
778 }
779
780 void ScopePanel::draw_point()
781 {
782 }
783
784 void ScopePanel::clear_point()
785 {
786 }
787
788 int ScopePanel::button_press_event()
789 {
790         if( is_event_win() && cursor_inside() ) {
791                 gui->clear_points(1);
792                 is_dragging = 1;
793                 int x = get_cursor_x();
794                 int y = get_cursor_y();
795                 CLAMP(x, 0, get_w() - 1);
796                 CLAMP(y, 0, get_h() - 1);
797                 update_point(x, y);
798                 return 1;
799         }
800         return 0;
801 }
802
803
804 int ScopePanel::cursor_motion_event()
805 {
806         if( is_dragging ) {
807                 int x = get_cursor_x();
808                 int y = get_cursor_y();
809                 CLAMP(x, 0, get_w() - 1);
810                 CLAMP(y, 0, get_h() - 1);
811                 update_point(x, y);
812                 return 1;
813         }
814         return 0;
815 }
816
817
818 int ScopePanel::button_release_event()
819 {
820         if( is_dragging ) {
821                 is_dragging = 0;
822                 return 1;
823         }
824         return 0;
825 }
826
827
828 ScopeWaveform::ScopeWaveform(ScopeGUI *gui,
829                 int x, int y, int w, int h)
830  : ScopePanel(gui, x, y, w, h)
831 {
832         drag_x = -1;
833         drag_y = -1;
834 }
835
836 void ScopeWaveform::update_point(int x, int y)
837 {
838         draw_point();
839         drag_x = x;
840         drag_y = y;
841         int frame_x = x * gui->frame_w / get_w();
842
843         if( gui->use_wave_parade ) {
844                 if( x > get_w() / 3 * 2 )
845                         frame_x = (x - get_w() / 3 * 2) * gui->frame_w / (get_w() / 3);
846                 else if( x > get_w() / 3 )
847                         frame_x = (x - get_w() / 3) * gui->frame_w / (get_w() / 3);
848                 else
849                         frame_x = x * gui->frame_w / (get_w() / 3);
850         }
851
852         float value = ((float)get_h() - y) / get_h() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
853         char string[BCTEXTLEN];
854         sprintf(string, "X: %d Value: %.3f", frame_x, value);
855         gui->value_text->update(string, 0);
856
857         draw_point();
858         flash(1);
859 }
860
861 void ScopeWaveform::draw_point()
862 {
863         if( drag_x >= 0 ) {
864                 set_inverse();
865                 set_color(0xffffff);
866                 set_line_width(2);
867                 draw_line(0, drag_y, get_w(), drag_y);
868                 draw_line(drag_x, 0, drag_x, get_h());
869                 set_line_width(1);
870                 set_opaque();
871         }
872 }
873
874 void ScopeWaveform::clear_point()
875 {
876         draw_point();
877         drag_x = -1;
878         drag_y = -1;
879 }
880
881 ScopeVectorscope::ScopeVectorscope(ScopeGUI *gui,
882                 int x, int y, int w, int h)
883  : ScopePanel(gui, x, y, w, h)
884 {
885         drag_radius = 0;
886         drag_angle = 0;
887 }
888
889 void ScopeVectorscope::clear_point()
890 {
891 // Hide it
892         draw_point();
893         drag_radius = 0;
894         drag_angle = 0;
895 }
896
897 void ScopeVectorscope::update_point(int x, int y)
898 {
899 // Hide it
900         draw_point();
901
902         int radius = MIN(get_w() / 2, get_h() / 2);
903         drag_radius = sqrt(SQR(x - get_w() / 2) + SQR(y - get_h() / 2));
904         drag_angle = atan2(y - get_h() / 2, x - get_w() / 2);
905
906         drag_radius = MIN(drag_radius, radius);
907
908         float saturation = (float)drag_radius / radius * FLOAT_MAX;
909         float hue = -drag_angle * 360 / 2 / M_PI - 90;
910         if( hue < 0 ) hue += 360;
911
912         char string[BCTEXTLEN];
913         sprintf(string, "Hue: %.3f Sat: %.3f", hue, saturation);
914         gui->value_text->update(string, 0);
915
916 // Show it
917         draw_point();
918         flash(1);
919 }
920
921 void ScopeVectorscope::draw_point()
922 {
923         if( drag_radius > 0 ) {
924                 int radius = MIN(get_w() / 2, get_h() / 2);
925                 set_inverse();
926                 set_color(0xff0000);
927                 set_line_width(2);
928                 draw_circle(get_w() / 2 - drag_radius, get_h() / 2 - drag_radius,
929                         drag_radius * 2, drag_radius * 2);
930                 draw_line(get_w() / 2, get_h() / 2,
931                         get_w() / 2 + radius * cos(drag_angle),
932                         get_h() / 2 + radius * sin(drag_angle));
933                 set_line_width(1);
934                 set_opaque();
935         }
936 }
937
938
939
940 ScopeHistogram::ScopeHistogram(ScopeGUI *gui,
941                 int x, int y, int w, int h)
942  : ScopePanel(gui, x, y, w, h)
943 {
944         drag_x = -1;
945 }
946
947 void ScopeHistogram::clear_point()
948 {
949 // Hide it
950         draw_point();
951         drag_x = -1;
952 }
953
954 void ScopeHistogram::draw_point()
955 {
956         if( drag_x >= 0 ) {
957                 set_inverse();
958                 set_color(0xffffff);
959                 set_line_width(2);
960                 draw_line(drag_x, 0, drag_x, get_h());
961                 set_line_width(1);
962                 set_opaque();
963         }
964 }
965
966 void ScopeHistogram::update_point(int x, int y)
967 {
968         draw_point();
969         drag_x = x;
970         float value = (float)x / get_w() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
971
972         char string[BCTEXTLEN];
973         sprintf(string, "Value: %.3f", value);
974         gui->value_text->update(string, 0);
975
976         draw_point();
977         flash(1);
978 }
979
980
981
982 void ScopeHistogram::draw_mode(int mode, int color, int y, int h)
983 {
984 // Highest of all bins
985         int normalize = 1, w = get_w();
986         int *bin = gui->bins[mode], v;
987         for( int i=0; i<TOTAL_BINS; ++i )
988                 if( (v=bin[i]) > normalize ) normalize = v;
989         double norm = normalize>1 ? log(normalize) : 1e-4;
990         double vnorm = h / norm;
991         set_color(color);
992         for( int x=0; x<w; ++x ) {
993                 int accum_start = (int)(x * TOTAL_BINS / w);
994                 int accum_end = (int)((x + 1) * TOTAL_BINS / w);
995                 CLAMP(accum_start, 0, TOTAL_BINS);
996                 CLAMP(accum_end, 0, TOTAL_BINS);
997                 int max = 0;
998                 for(int i=accum_start; i<accum_end; ++i )
999                         if( (v=bin[i]) > max ) max = v;
1000 //              max = max * h / normalize;
1001                 max = log(max) * vnorm;
1002                 draw_line(x,y+h - max, x,y+h);
1003         }
1004 }
1005 void ScopeHistogram::draw(int flash, int flush)
1006 {
1007         clear_box(0, 0, get_w(), get_h());
1008         if( gui->use_hist_parade ) {
1009                 draw_mode(0, 0xff0000, 0, get_h() / 3);
1010                 draw_mode(1, 0x00ff00, get_h() / 3, get_h() / 3);
1011                 draw_mode(2, 0x0000ff, get_h() / 3 * 2, get_h() / 3);
1012         }
1013         else {
1014                 draw_mode(3, LTGREY, 0, get_h());
1015         }
1016
1017         if(flash) this->flash(0);
1018         if(flush) this->flush();
1019 }
1020
1021
1022 ScopeScopesOn::ScopeScopesOn(ScopeMenu *scope_menu, const char *text, int id)
1023  : BC_MenuItem(text)
1024 {
1025         this->scope_menu = scope_menu;
1026         this->id = id;
1027 }
1028
1029 int ScopeScopesOn::handle_event()
1030 {
1031         int v = get_checked() ? 0 : 1;
1032         set_checked(v);
1033         ScopeGUI *gui = scope_menu->gui;
1034         switch( id ) {
1035         case SCOPE_HISTOGRAM:
1036                 gui->use_hist = v;
1037                 if( v ) gui->use_hist_parade = 0;
1038                 break;
1039         case SCOPE_HISTOGRAM_RGB:
1040                 gui->use_hist_parade = v;
1041                 if( v ) gui->use_hist = 0;
1042                 break;
1043         case SCOPE_WAVEFORM:
1044                 gui->use_wave = v;
1045                 if( v ) gui->use_wave_parade = 0;
1046                 break;
1047         case SCOPE_WAVEFORM_RGB:
1048                 gui->use_wave_parade = v;
1049                 if( v ) gui->use_wave = 0;
1050                 break;
1051         case SCOPE_WAVEFORM_PLY:
1052                 gui->use_wave_parade = -v;
1053                 if( v ) gui->use_wave = 0;
1054                 break;
1055         case SCOPE_VECTORSCOPE:
1056                 gui->use_vector = v;
1057                 break;
1058         }
1059         gui->toggle_event();
1060         gui->update_toggles();
1061         gui->create_panels();
1062         gui->update_scope();
1063         gui->show_window();
1064         return 1;
1065 }
1066
1067 ScopeMenu::ScopeMenu(ScopeGUI *gui, int x, int y)
1068  : BC_PopupMenu(x, y, xS(100), _("Scopes"))
1069 {
1070         this->gui = gui;
1071 }
1072
1073 void ScopeMenu::create_objects()
1074 {
1075         add_item(hist_on =
1076                 new ScopeScopesOn(this, _("Histogram"), SCOPE_HISTOGRAM));
1077         add_item(hist_rgb_on =
1078                 new ScopeScopesOn(this, _("Histogram RGB"), SCOPE_HISTOGRAM_RGB));
1079         add_item(wave_on =
1080                 new ScopeScopesOn(this, _("Waveform"), SCOPE_WAVEFORM));
1081         add_item(wave_rgb_on =
1082                 new ScopeScopesOn(this, _("Waveform RGB"), SCOPE_WAVEFORM_RGB));
1083         add_item(wave_ply_on =
1084                 new ScopeScopesOn(this, _("Waveform ply"), SCOPE_WAVEFORM_PLY));
1085         add_item(vect_on =
1086                 new ScopeScopesOn(this, _("Vectorscope"), SCOPE_VECTORSCOPE));
1087 }
1088
1089 void ScopeMenu::update_toggles()
1090 {
1091         hist_on->set_checked(gui->use_hist);
1092         hist_rgb_on->set_checked(gui->use_hist_parade);
1093         wave_on->set_checked(gui->use_wave);
1094         wave_rgb_on->set_checked(gui->use_wave_parade>0);
1095         wave_ply_on->set_checked(gui->use_wave_parade<0);
1096         vect_on->set_checked(gui->use_vector);
1097 }
1098
1099 ScopeWaveDial::ScopeWaveDial(ScopeGUI *gui, int x, int y)
1100  : BC_FPot(x, y, gui->wdial, 1.f, 9.f)
1101 {
1102         this->gui = gui;
1103 }
1104 int ScopeWaveDial::handle_event()
1105 {
1106         gui->wdial = get_value();
1107         gui->update_scope();
1108         return 1;
1109 }
1110
1111 ScopeVectDial::ScopeVectDial(ScopeGUI *gui, int x, int y)
1112  : BC_FPot(x, y, gui->vdial, 1.f, 9.f)
1113 {
1114         this->gui = gui;
1115 }
1116 int ScopeVectDial::handle_event()
1117 {
1118         gui->vdial = get_value();
1119         gui->update_scope();
1120         return 1;
1121 }
1122