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