3 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
21 #include "bcsignals.h"
27 #include "filesystem.h"
29 #include "overlayframe.h"
30 #include "scopewindow.h"
35 ScopePackage::ScopePackage()
40 ScopeUnit::ScopeUnit(ScopeGUI *gui,
48 #define incr_point(rows,h, iv,comp) { \
49 if(iy >= 0 && iy < h) { \
50 uint8_t *vp = rows[iy] + ix*3 + (comp); \
51 int v = *vp+(iv); *vp = v>0xff ? 0xff : v; } \
53 #define incr_points(rows,h, rv,gv,bv, comps) { \
54 if(iy >= 0 && iy < h) { \
55 uint8_t *vp = rows[iy] + ix*comps; \
56 int v = *vp+(rv); *vp++ = v>0xff ? 0xff : v; \
57 v = *vp+(gv); *vp++ = v>0xff ? 0xff : v; \
58 v = *vp+(bv); *vp = v>0xff ? 0xff : v; } \
60 #define decr_points(rows,h, rv,gv,bv, comps) { \
61 if(iy >= 0 && iy < h) { \
62 uint8_t *vp = rows[iy] + ix*comps; \
63 int v = *vp-(rv); *vp++ = v<0 ? 0 : v; \
64 v = *vp-(gv); *vp++ = v<0 ? 0 : v; \
65 v = *vp-(bv); *vp = v<0 ? 0 : v; } \
67 #define clip_points(rows,h, rv,gv,bv) { \
68 if(iy >= 0 && iy < h) { \
69 uint8_t *vp = rows[iy] + ix*3; \
70 int v = *vp+(rv); *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
71 v = *vp+(gv); *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
72 v = *vp+(bv); *vp = v>0xff ? 0xff : v<0 ? 0 : v; } \
75 #define PROCESS_PIXEL(column) { \
76 /* Calculate histogram */ \
78 int v_i = (intensity - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
79 CLAMP(v_i, 0, TOTAL_BINS - 1); \
82 if(use_hist_parade) { \
83 int r_i = (r - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
84 int g_i = (g - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
85 int b_i = (b - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
86 CLAMP(r_i, 0, TOTAL_BINS - 1); \
87 CLAMP(g_i, 0, TOTAL_BINS - 1); \
88 CLAMP(b_i, 0, TOTAL_BINS - 1); \
93 /* Calculate waveform */ \
94 if(use_wave || use_wave_parade) { \
95 int ix = column * wave_w / dat_w; \
96 if(ix >= 0 && ix < wave_w-1) { \
97 if(use_wave_parade > 0) { \
99 int iy = wave_h - ((r - FLOAT_MIN) / \
100 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
101 incr_point(waveform_rows,wave_h, winc,0); \
103 iy = wave_h - ((g - FLOAT_MIN) / \
104 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
105 incr_point(waveform_rows,wave_h, winc,1); \
107 iy = wave_h - ((b - FLOAT_MIN) / \
108 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
109 incr_point(waveform_rows,wave_h, winc,2); \
111 else if(use_wave_parade < 0) { \
112 int iy = wave_h - ((r - FLOAT_MIN) / \
113 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
114 clip_points(waveform_rows,wave_h, winc,-winc,-winc); \
115 iy = wave_h - ((g - FLOAT_MIN) / \
116 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
117 clip_points(waveform_rows,wave_h, -winc,winc,-winc); \
118 iy = wave_h - ((b - FLOAT_MIN) / \
119 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
120 clip_points(waveform_rows,wave_h, -winc,-winc,winc); \
122 else { float yinc = 3*winc; \
123 float rinc = yinc*(r-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
124 float ginc = yinc*(g-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
125 float binc = yinc*(b-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
126 int iy = wave_h - ((intensity - FLOAT_MIN) / \
127 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
128 incr_points(waveform_rows,wave_h, rinc, ginc, binc, 3); \
132 /* Calculate vectorscope */ \
133 if(use_vector > 0) { \
134 double t = TO_RAD(-h); \
135 float adjacent = sin(t), opposite = cos(t); \
136 int ix = vector_cx + adjacent * (s) / (FLOAT_MAX) * radius; \
137 int iy = vector_cy - opposite * (s) / (FLOAT_MAX) * radius; \
138 CLAMP(ix, 0, vector_w - 1); \
139 HSV::hsv_to_rgb(r, g, b, h, s, vincf); \
143 incr_points(vector_rows,vector_h, rv,gv,bv, 4); \
145 else if(use_vector < 0) { \
146 double t = TO_RAD(-h); \
147 float adjacent = sin(t), opposite = cos(t); \
148 int ix = vector_cx + adjacent * (s) / (FLOAT_MAX) * radius; \
149 int iy = vector_cy - opposite * (s) / (FLOAT_MAX) * radius; \
150 CLAMP(ix, 0, vector_w - 1); \
151 decr_points(vector_rows,vector_h, vinc,vinc,vinc, 4); \
155 #define PROCESS_RGB_PIXEL(type,max, column) { \
156 type *rp = (type *)row; \
157 r = (float)rp[0] / max; \
158 g = (float)rp[1] / max; \
159 b = (float)rp[2] / max; \
160 HSV::rgb_to_hsv(r, g, b, h, s, v); \
162 PROCESS_PIXEL(column) \
165 #define PROCESS_BGR_PIXEL(type,max, column) { \
166 type *rp = (type *)row; \
167 b = (float)rp[0] / max; \
168 g = (float)rp[1] / max; \
169 r = (float)rp[2] / max; \
170 HSV::rgb_to_hsv(r, g, b, h, s, v); \
172 PROCESS_PIXEL(column) \
175 #define PROCESS_YUV_PIXEL(column, y_in, u_in, v_in) { \
176 YUV::yuv.yuv_to_rgb_f(r, g, b, y_in, u_in, v_in); \
177 HSV::rgb_to_hsv(r, g, b, h, s, v); \
179 PROCESS_PIXEL(column) \
182 void ScopeUnit::process_package(LoadPackage *package)
184 ScopePackage *pkg = (ScopePackage*)package;
189 int use_hist = gui->use_hist;
190 int use_hist_parade = gui->use_hist_parade;
191 int use_vector = gui->use_vector;
192 int use_wave = gui->use_wave;
193 int use_wave_parade = gui->use_wave_parade;
194 int vector_cx = gui->vector_cx;
195 int vector_cy = gui->vector_cy;
196 int radius = gui->radius;
197 VFrame *waveform_vframe = gui->waveform_vframe;
198 VFrame *vector_vframe = gui->vector_vframe;
199 int wave_h = waveform_vframe->get_h();
200 int wave_w = waveform_vframe->get_w();
201 int vector_h = vector_vframe->get_h();
202 int vector_w = vector_vframe->get_w();
203 int dat_w = gui->data_frame->get_w();
204 int dat_h = gui->data_frame->get_h();
205 int winc = (wave_w * wave_h) / (dat_w * dat_h);
206 if( use_wave_parade ) winc *= 3;
207 winc += 2; winc = (winc << gui->use_wave_gain) / 4;
208 int vinc = 3*(vector_w * vector_h) / (dat_w * dat_h);
209 vinc += 2; vinc = (vinc << gui->use_vect_gain) / 4;
211 uint8_t **waveform_rows = waveform_vframe->get_rows();
212 uint8_t **vector_rows = vector_vframe->get_rows();
213 uint8_t **rows = gui->data_frame->get_rows();
215 switch( gui->data_frame->get_color_model() ) {
217 for( int y=pkg->row1; y<pkg->row2; ++y ) {
218 uint8_t *row = rows[y];
219 for( int x=0; x<dat_w; ++x ) {
220 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
221 row += 3*sizeof(uint8_t);
226 for( int y=pkg->row1; y<pkg->row2; ++y ) {
227 uint8_t *row = rows[y];
228 for( int x=0; x<dat_w; ++x ) {
229 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
230 row += 4*sizeof(uint8_t);
235 for( int y=pkg->row1; y<pkg->row2; ++y ) {
236 uint8_t *row = rows[y];
237 for( int x=0; x<dat_w; ++x ) {
238 PROCESS_RGB_PIXEL(uint16_t,0xffff, x)
239 row += 3*sizeof(uint16_t);
243 case BC_RGBA16161616:
244 for( int y=pkg->row1; y<pkg->row2; ++y ) {
245 uint8_t *row = rows[y];
246 for( int x=0; x<dat_w; ++x ) {
247 PROCESS_RGB_PIXEL(uint16_t,0xffff, x)
248 row += 4*sizeof(uint16_t);
253 for( int y=pkg->row1; y<pkg->row2; ++y ) {
254 uint8_t *row = rows[y];
255 for( int x=0; x<dat_w; ++x ) {
256 PROCESS_BGR_PIXEL(uint8_t,0xff, x)
257 row += 3*sizeof(uint8_t);
262 for( int y=pkg->row1; y<pkg->row2; ++y ) {
263 uint8_t *row = rows[y];
264 for( int x=0; x<dat_w; ++x ) {
265 PROCESS_BGR_PIXEL(uint8_t,0xff, x)
266 row += 4*sizeof(uint8_t);
271 for( int y=pkg->row1; y<pkg->row2; ++y ) {
272 uint8_t *row = rows[y];
273 for( int x=0; x<dat_w; ++x ) {
274 PROCESS_RGB_PIXEL(float,1.f, x)
275 row += 3*sizeof(float);
280 for( int y=pkg->row1; y<pkg->row2; ++y ) {
281 uint8_t *row = rows[y];
282 for( int x=0; x<dat_w; ++x ) {
283 PROCESS_RGB_PIXEL(float,1.f, x)
284 row += 4*sizeof(float);
289 for( int y=pkg->row1; y<pkg->row2; ++y ) {
290 uint8_t *row = rows[y];
291 for( int x=0; x<dat_w; ++x ) {
292 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
293 row += 3*sizeof(uint8_t);
299 for( int y=pkg->row1; y<pkg->row2; ++y ) {
300 uint8_t *row = rows[y];
301 for( int x=0; x<dat_w; ++x ) {
302 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
303 row += 4*sizeof(uint8_t);
308 uint8_t *yp = gui->data_frame->get_y();
309 uint8_t *up = gui->data_frame->get_u();
310 uint8_t *vp = gui->data_frame->get_v();
311 for( int y=pkg->row1; y<pkg->row2; ++y ) {
312 uint8_t *y_row = yp + y * dat_w;
313 uint8_t *u_row = up + (y / 2) * (dat_w / 2);
314 uint8_t *v_row = vp + (y / 2) * (dat_w / 2);
315 for( int x=0; x<dat_w; x+=2 ) {
316 PROCESS_YUV_PIXEL(x, *y_row, *u_row, *v_row);
318 PROCESS_YUV_PIXEL(x + 1, *y_row, *u_row, *v_row);
319 ++y_row; ++u_row; ++v_row;
324 for( int y=pkg->row1; y<pkg->row2; ++y ) {
325 uint8_t *row = rows[y];
326 for( int x=0; x<dat_w; x+=2 ) {
327 PROCESS_YUV_PIXEL(x, row[0], row[1], row[3]);
328 PROCESS_YUV_PIXEL(x + 1, row[2], row[1], row[3]);
329 row += 4*sizeof(uint8_t);
335 printf("ScopeUnit::process_package %d: color_model=%d unrecognized\n",
336 __LINE__, gui->data_frame->get_color_model());
342 ScopeEngine::ScopeEngine(ScopeGUI *gui, int cpus)
343 : LoadServer(cpus, cpus)
345 //printf("ScopeEngine::ScopeEngine %d cpus=%d\n", __LINE__, cpus);
349 ScopeEngine::~ScopeEngine()
353 void ScopeEngine::init_packages()
355 int y = 0, h = gui->data_frame->get_h();
356 for( int i=0,n=LoadServer::get_total_packages(); i<n; ) {
357 ScopePackage *pkg = (ScopePackage*)get_package(i);
359 pkg->row2 = y = (++i * h) / n;
362 for( int i=0,n=LoadServer::get_total_packages(); i<n; ++i ) {
363 ScopeUnit *unit = (ScopeUnit*)get_client(i);
364 for( int j=0; j<HIST_SECTIONS; ++j )
365 bzero(unit->bins[j], sizeof(int) * TOTAL_BINS);
370 LoadClient* ScopeEngine::new_client()
372 return new ScopeUnit(gui, this);
375 LoadPackage* ScopeEngine::new_package()
377 return new ScopePackage;
380 void ScopeEngine::process()
384 for(int i = 0; i < HIST_SECTIONS; i++)
385 bzero(gui->bins[i], sizeof(int) * TOTAL_BINS);
387 for(int i=0,n=get_total_clients(); i<n; ++i ) {
388 ScopeUnit *unit = (ScopeUnit*)get_client(i);
389 for( int j=0; j<HIST_SECTIONS; ++j ) {
390 int *bp = gui->bins[j], *up = unit->bins[j];
391 for( int k=TOTAL_BINS; --k>=0; ++bp,++up ) *bp += *up;
397 ScopeGUI::ScopeGUI(Theme *theme,
398 int x, int y, int w, int h, int cpus)
399 : PluginClientWindow(_(PROGRAM_NAME ": Scopes"),
400 x, y, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
411 ScopeGUI::ScopeGUI(PluginClient *plugin, int w, int h)
412 : PluginClientWindow(plugin, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
418 this->theme = plugin->get_theme();
419 this->cpus = plugin->PluginClient::smp + 1;
423 ScopeGUI::~ScopeGUI()
425 delete waveform_vframe;
426 delete vector_vframe;
437 void ScopeGUI::reset()
469 vector_w = vector_h = 0;
470 vector_cx = vector_cy = 0;
473 dark_color = (text_color>>2) & 0x3f3f3f;
474 BC_Resources *resources = BC_WindowBase::get_resources();
475 if( resources->bg_color == 0xffffff )
476 text_color = dark_color;
479 void ScopeGUI::create_objects()
481 if( use_hist && use_hist_parade )
483 if( use_wave && use_wave_parade )
485 if( !engine ) engine = new ScopeEngine(this, cpus);
486 grat_idx = use_graticule; // last graticule
489 lock_window("ScopeGUI::create_objects");
490 int margin = theme->widget_border;
493 add_subwindow(scope_menu = new ScopeMenu(this, x, y));
494 scope_menu->create_objects();
495 int x1 = x + scope_menu->get_w() + 2*margin;
496 add_subwindow(smooth = new ScopeSmooth(this, x1, y));
497 if( use_refresh >= 0 ) {
498 y += smooth->get_h() + margin;
499 add_subwindow(refresh = new ScopeRefresh(this, x, y));
508 void ScopeGUI::create_panels()
510 calculate_sizes(get_w(), get_h());
511 int slider_w = xS(100);
512 int margin = theme->widget_border;
513 if( (use_wave || use_wave_parade) ) {
514 int px = wave_x + wave_w - slider_w - xS(5);
515 int py = wave_y - ScopeGain::calculate_h() - yS(5);
517 add_subwindow(waveform = new ScopeWaveform(this,
518 wave_x, wave_y, wave_w, wave_h));
519 waveform->create_objects();
520 wave_slider = new ScopeWaveSlider(this, px, py, slider_w);
521 wave_slider->create_objects();
524 waveform->reposition_window(
525 wave_x, wave_y, wave_w, wave_h);
526 waveform->clear_box(0, 0, wave_w, wave_h);
527 wave_slider->reposition_window(px, py);
530 else if( !(use_wave || use_wave_parade) && waveform ) {
531 delete waveform; waveform = 0;
532 delete wave_slider; wave_slider = 0;
536 int vx = vector_x + vector_w - slider_w - xS(5);
537 int vy = vector_y - ScopeGain::calculate_h() - yS(5);
539 add_subwindow(vectorscope = new ScopeVectorscope(this,
540 vector_x, vector_y, vector_w, vector_h));
541 vectorscope->create_objects();
542 vect_slider = new ScopeVectSlider(this, vx, vy, slider_w);
543 vect_slider->create_objects();
544 if( use_vector < 0 ) {
545 vect_grats = new ScopeVectGrats(this, vx, 2*margin);
546 add_subwindow(vect_grats);
547 vect_grats->create_objects();
551 vectorscope->reposition_window(
552 vector_x, vector_y, vector_w, vector_h);
553 vectorscope->clear_box(0, 0, vector_w, vector_h);
554 vect_slider->reposition_window(vx, vy);
555 if( use_vector > 0 ) {
556 delete vect_grats; vect_grats = 0;
558 else if( !vect_grats ) {
559 vect_grats = new ScopeVectGrats(this, vx, 2*margin);
560 add_subwindow(vect_grats);
561 vect_grats->create_objects();
564 vect_grats->reposition_window(vx, 2*margin);
567 else if( !use_vector && vectorscope ) {
568 delete vectorscope; vectorscope = 0;
569 delete vect_slider; vect_slider = 0;
570 delete vect_grats; vect_grats = 0;
573 if( (use_hist || use_hist_parade) ) {
575 // printf("ScopeGUI::create_panels %d %d %d %d %d\n", __LINE__,
576 // hist_x, hist_y, hist_w, hist_h);
577 add_subwindow(histogram = new ScopeHistogram(this,
578 hist_x, hist_y, hist_w, hist_h));
579 histogram->create_objects();
582 histogram->reposition_window(
583 hist_x, hist_y, hist_w, hist_h);
584 histogram->clear_box(0, 0, hist_w, hist_h);
587 else if( !(use_hist || use_hist_parade) ) {
588 delete histogram; histogram = 0;
593 draw_overlays(1, 1, 0);
596 void ScopeGUI::clear_points(int flash)
599 histogram->clear_point();
601 waveform->clear_point();
603 vectorscope->clear_point();
604 if( histogram && flash )
606 if( waveform && flash )
608 if( vectorscope && flash )
609 vectorscope->flash(0);
612 void ScopeGUI::toggle_event()
616 void ScopeGUI::calculate_sizes(int w, int h)
618 int margin = theme->widget_border;
619 int menu_h = smooth->get_h() + scope_menu->get_h() + margin * 3;
620 int text_w = get_text_width(SMALLFONT, "000") + margin * 2;
621 int total_panels = ((use_hist || use_hist_parade) ? 1 : 0) +
622 ((use_wave || use_wave_parade) ? 1 : 0) +
623 (use_vector ? 1 : 0);
626 int panel_w = (w - margin) / (total_panels > 0 ? total_panels : 1);
627 // Vectorscope determines the size of everything else
631 vector_x = w - panel_w + text_w;
632 vector_w = w - margin - vector_x;
634 vector_h = h - vector_y - margin;
636 if( vector_w > vector_h ) {
638 vector_x = w - margin - vector_w;
642 panel_w = (vector_x - text_w - margin) / total_panels;
644 vector_cx = vector_w / 2;
645 vector_cy = vector_h / 2;
646 radius = bmin(vector_cx, vector_cy);
648 // Histogram is always 1st panel
649 if( use_hist || use_hist_parade ) {
652 hist_w = panel_w - margin;
653 hist_h = h - hist_y - margin;
659 if( use_wave || use_wave_parade ) {
662 wave_w = panel_w - margin - text_w;
663 wave_h = h - wave_y - margin;
669 void ScopeGUI::allocate_vframes()
671 if(waveform_vframe) delete waveform_vframe;
672 if(vector_vframe) delete vector_vframe;
673 if(wheel_vframe) delete wheel_vframe;
676 //printf("ScopeGUI::allocate_vframes %d %d %d %d %d\n", __LINE__,
677 // wave_w, wave_h, vector_w, vector_h);
678 int xs16 = xS(16), ys16 = yS(16);
679 w = MAX(wave_w, xs16);
680 h = MAX(wave_h, ys16);
681 waveform_vframe = new VFrame(w, h, BC_RGB888);
682 w = MAX(vector_w, xs16);
683 h = MAX(vector_h, ys16);
684 vector_vframe = new VFrame(w, h, BC_RGBA8888);
685 vector_vframe->set_clear_color(BLACK, 0xff);
690 int ScopeGUI::resize_event(int w, int h)
692 clear_box(0, 0, w, h);
695 calculate_sizes(w, h);
696 int margin = theme->widget_border;
699 waveform->reposition_window(wave_x, wave_y, wave_w, wave_h);
700 waveform->clear_box(0, 0, wave_w, wave_h);
701 int px = wave_x + wave_w - wave_slider->get_w() - margin;
702 int py = wave_y - wave_slider->get_h() - margin;
703 wave_slider->reposition_window(px, py);
707 histogram->reposition_window(hist_x, hist_y, hist_w, hist_h);
708 histogram->clear_box(0, 0, hist_w, hist_h);
712 vectorscope->reposition_window(vector_x, vector_y, vector_w, vector_h);
713 vectorscope->clear_box(0, 0, vector_w, vector_h);
714 int vx = vector_x + vector_w - vect_slider->get_w() - margin;
715 int vy = vector_y - vect_slider->get_h() - margin;
716 vect_slider->reposition_window(vx, vy);
718 vect_grats->reposition_window(vx, 2*margin);
723 draw_overlays(1, 1, 1);
728 int ScopeGUI::translation_event()
732 PluginClientWindow::translation_event();
737 void ScopeGUI::draw_overlays(int overlays, int borders, int flush)
739 int margin = theme->widget_border;
740 if( overlays && borders ) {
741 clear_box(0, 0, get_w(), get_h());
746 set_color(text_color);
749 if( histogram && (use_hist || use_hist_parade) ) {
750 histogram->draw_line(hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), 0,
751 hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), hist_h);
752 histogram->draw_line(hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), 0,
753 hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), hist_h);
755 histogram->draw_point();
761 if( waveform && (use_wave || use_wave_parade) ) {
762 for( int i=0; i<=WAVEFORM_DIVISIONS; ++i ) {
763 int y = wave_h * i / WAVEFORM_DIVISIONS;
764 int text_y = y + wave_y + get_text_ascent(SMALLFONT) / 2;
765 CLAMP(text_y, waveform->get_y() + get_text_ascent(SMALLFONT), waveform->get_y() + waveform->get_h() - 1);
766 char string[BCTEXTLEN];
767 sprintf(string, "%d", (int)lround((FLOAT_MAX -
768 i * (FLOAT_MAX - FLOAT_MIN) / WAVEFORM_DIVISIONS) * 100));
769 int text_x = wave_x - get_text_width(SMALLFONT, string) - margin;
770 set_color(text_color);
771 draw_text(text_x, text_y, string);
772 CLAMP(y, 0, waveform->get_h() - 1);
773 set_color(dark_color);
774 waveform->draw_line(0, y, wave_w, y);
775 waveform->draw_rectangle(0, 0, wave_w, wave_h);
778 waveform->draw_point();
782 // Vectorscope overlay
783 if( vectorscope && use_vector ) {
784 set_color(text_color);
785 vectorscope->draw_point();
786 vectorscope->flash(0);
790 set_font(MEDIUMFONT);
795 if( use_hist || use_hist_parade ) {
796 draw_3d_border(hist_x - 2, hist_y - 2, hist_w + 4, hist_h + 4,
797 get_bg_color(), BLACK, MDGREY, get_bg_color());
799 if( use_wave || use_wave_parade ) {
800 draw_3d_border(wave_x - 2, wave_y - 2, wave_w + 4, wave_h + 4,
801 get_bg_color(), BLACK, MDGREY, get_bg_color());
804 draw_3d_border(vector_x - 2, vector_y - 2, vector_w + 4, vector_h + 4,
805 get_bg_color(), BLACK, MDGREY, get_bg_color());
810 if(flush) this->flush();
813 void ScopeGUI::draw_scope()
815 int graticule = use_vector < 0 ? grat_idx : 0;
816 if( grat_image && use_graticule != graticule ) {
817 delete grat_image; grat_image = 0;
819 if( !grat_image && graticule > 0 )
820 grat_image = VFramePng::vframe_png(grat_paths[graticule]);
823 overlay = new OverlayFrame(1);
824 int cx = vector_cx, cy = vector_cy, r = radius;
825 int iw = grat_image->get_w(), ih = grat_image->get_h();
826 overlay->overlay(vector_vframe, grat_image,
827 0,0, iw, ih, cx-r,cy-r, cx+r, cy+r,
828 1, TRANSFER_NORMAL, CUBIC_CUBIC);
830 use_graticule = graticule;
831 vectorscope->draw_vframe(vector_vframe);
832 if( use_vector > 0 ) {
833 int margin = theme->widget_border;
835 for( int i=1; i<=VECTORSCOPE_DIVISIONS; i+=2 ) {
836 int y = vector_cy - radius * i / VECTORSCOPE_DIVISIONS;
837 int text_y = y + vector_y + get_text_ascent(SMALLFONT) / 2;
838 set_color(text_color);
839 char string[BCTEXTLEN];
840 sprintf(string, "%d",
841 (int)((FLOAT_MAX / VECTORSCOPE_DIVISIONS * i) * 100));
842 int text_x = vector_x - get_text_width(SMALLFONT, string) - margin;
843 draw_text(text_x, text_y, string);
844 int x = vector_cx - radius * i / VECTORSCOPE_DIVISIONS;
845 int w = radius * i / VECTORSCOPE_DIVISIONS * 2;
846 int h = radius * i / VECTORSCOPE_DIVISIONS * 2;
847 if( i+2 > VECTORSCOPE_DIVISIONS )
849 set_color(dark_color);
850 vectorscope->draw_circle(x, y, w, h);
852 float th = TO_RAD(90 + 32.875);
853 vectorscope->draw_radient(th, 0.1f, .75f, dark_color);
858 void ScopeGUI::update_graticule(int idx)
865 void ScopeGUI::draw_colorwheel(VFrame *dst, int bg_color)
867 // downfactor radius to prevent extreme edge from showing behind graticule
868 float cx = vector_cx, cy = vector_cy, rad = radius * 0.99;
869 int color_model = dst->get_color_model();
870 int bpp = BC_CModels::calculate_pixelsize(color_model);
871 int bg_r = (bg_color>>16) & 0xff;
872 int bg_g = (bg_color>> 8) & 0xff;
873 int bg_b = (bg_color>> 0) & 0xff;
874 int w = dst->get_w(), h = dst->get_h();
875 unsigned char **rows = dst->get_rows();
876 for( int y=0; y<h; ++y ) {
877 unsigned char *row = rows[y];
878 for( int x=0; x<w; ++x,row+=bpp ) {
879 int dx = cx-x, dy = cy-y;
880 float d = sqrt(dx*dx + dy*dy);
883 float h = TO_DEG(atan2(cx-x, cy-y));
884 if( h < 0 ) h += 360;
885 float s = d / rad, v = 255;
886 HSV::hsv_to_rgb(r, g, b, h, s, v);
889 r = bg_r; g = bg_g; b = bg_b;
891 row[0] = r; row[1] = g; row[2] = b; row[3] = 0xff;
897 void ScopeGUI::process(VFrame *output_frame)
899 lock_window("ScopeGUI::process");
900 this->output_frame = output_frame;
901 int ow = output_frame->get_w(), oh = output_frame->get_h();
903 VFrame::get_temp(temp_frame, ow, oh, BC_RGB161616);
904 temp_frame->transfer_from(output_frame);
905 if( !box_blur ) box_blur = new BoxBlur(cpus);
906 box_blur->blur(temp_frame, temp_frame, 2, 2);
907 data_frame = temp_frame;
910 data_frame = output_frame;
911 frame_w = data_frame->get_w();
912 bzero(waveform_vframe->get_data(), waveform_vframe->get_data_size());
914 vector_vframe->clear_frame();
915 else if( use_vector < 0 ) {
916 if( wheel_vframe && (
917 wheel_vframe->get_w() != vector_w ||
918 wheel_vframe->get_h() != vector_h ) ) {
919 delete wheel_vframe; wheel_vframe = 0;
921 if( !wheel_vframe ) {
922 wheel_vframe = new VFrame(vector_w, vector_h, BC_RGBA8888);
923 draw_colorwheel(wheel_vframe, BLACK);
925 vector_vframe->copy_from(wheel_vframe);
930 histogram->draw(0, 0);
932 waveform->draw_vframe(waveform_vframe);
936 draw_overlays(1, 0, 1);
940 void ScopeGUI::update_toggles()
942 scope_menu->update_toggles();
945 ScopePanel::ScopePanel(ScopeGUI *gui, int x, int y, int w, int h)
946 : BC_SubWindow(x, y, w, h, BLACK)
952 void ScopePanel::create_objects()
954 set_cursor(CROSS_CURSOR, 0, 0);
955 clear_box(0, 0, get_w(), get_h());
958 void ScopePanel::update_point(int x, int y)
962 void ScopePanel::draw_point()
966 void ScopePanel::clear_point()
970 int ScopePanel::button_press_event()
972 if( is_event_win() && cursor_inside() ) {
973 gui->clear_points(1);
975 int x = get_cursor_x();
976 int y = get_cursor_y();
977 CLAMP(x, 0, get_w() - 1);
978 CLAMP(y, 0, get_h() - 1);
986 int ScopePanel::cursor_motion_event()
989 int x = get_cursor_x();
990 int y = get_cursor_y();
991 CLAMP(x, 0, get_w() - 1);
992 CLAMP(y, 0, get_h() - 1);
1000 int ScopePanel::button_release_event()
1011 ScopeWaveform::ScopeWaveform(ScopeGUI *gui,
1012 int x, int y, int w, int h)
1013 : ScopePanel(gui, x, y, w, h)
1019 void ScopeWaveform::update_point(int x, int y)
1024 int frame_x = x * gui->frame_w / get_w();
1026 if( gui->use_wave_parade ) {
1027 if( x > get_w() / 3 * 2 )
1028 frame_x = (x - get_w() / 3 * 2) * gui->frame_w / (get_w() / 3);
1029 else if( x > get_w() / 3 )
1030 frame_x = (x - get_w() / 3) * gui->frame_w / (get_w() / 3);
1032 frame_x = x * gui->frame_w / (get_w() / 3);
1035 float value = ((float)get_h() - y) / get_h() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1036 char string[BCTEXTLEN];
1037 sprintf(string, "X: %d Value: %.3f", frame_x, value);
1038 show_tooltip(string);
1044 void ScopeWaveform::draw_point()
1048 set_color(0xffffff);
1050 draw_line(0, drag_y, get_w(), drag_y);
1051 draw_line(drag_x, 0, drag_x, get_h());
1057 void ScopeWaveform::clear_point()
1064 ScopeVectorscope::ScopeVectorscope(ScopeGUI *gui,
1065 int x, int y, int w, int h)
1066 : ScopePanel(gui, x, y, w, h)
1072 void ScopeVectorscope::clear_point()
1080 void ScopeVectorscope::update_point(int x, int y)
1084 int dx = x - gui->vector_cx, dy = y - gui->vector_cy;
1085 drag_radius = sqrt(dx*dx + dy*dy);
1086 if( drag_radius > gui->radius ) drag_radius = gui->radius;
1087 float saturation = (float)drag_radius / gui->radius * FLOAT_MAX;
1088 drag_angle = atan2(-dy, dx);
1089 float hue = TO_DEG(drag_angle) - 90;
1090 if( hue < 0 ) hue += 360;
1092 char string[BCTEXTLEN];
1093 sprintf(string, "Hue: %.3f Sat: %.3f", hue, saturation);
1094 show_tooltip(string);
1101 void ScopeVectorscope::draw_point()
1104 draw_point(drag_angle, drag_radius, RED);
1108 void ScopeVectorscope::draw_point(float th, float r, int color)
1113 int cx = gui->vector_cx, cy = gui->vector_cy;
1114 draw_circle(cx-r, cy-r, r*2, r*2);
1116 float radius = gui->radius;
1117 draw_radient(th, 0.f, drag_radius/radius, color);
1121 void ScopeVectorscope::draw_radient(float th, float r1, float r2, int color)
1123 if( r2 > 0 && r2 >= r1 ) {
1126 int r = gui->radius;
1127 double cth = r*cos(th), sth = -r*sin(th);
1128 int cx = gui->vector_cx, cy = gui->vector_cy;
1129 draw_line(cx+r1*cth, cy+r1*sth, cx+r2*cth, cy+r2*sth);
1135 ScopeHistogram::ScopeHistogram(ScopeGUI *gui,
1136 int x, int y, int w, int h)
1137 : ScopePanel(gui, x, y, w, h)
1142 void ScopeHistogram::clear_point()
1149 void ScopeHistogram::draw_point()
1153 set_color(0xffffff);
1155 draw_line(drag_x, 0, drag_x, get_h());
1161 void ScopeHistogram::update_point(int x, int y)
1165 float value = (float)x / get_w() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1167 char string[BCTEXTLEN];
1168 sprintf(string, "Value: %.3f", value);
1169 show_tooltip(string);
1177 void ScopeHistogram::draw_mode(int mode, int color, int y, int h)
1179 // Highest of all bins
1180 int normalize = 1, w = get_w();
1181 int *bin = gui->bins[mode], v;
1182 for( int i=0; i<TOTAL_BINS; ++i )
1183 if( (v=bin[i]) > normalize ) normalize = v;
1184 double norm = normalize>1 ? log(normalize) : 1e-4;
1185 double vnorm = h / norm;
1187 for( int x=0; x<w; ++x ) {
1188 int accum_start = (int)(x * TOTAL_BINS / w);
1189 int accum_end = (int)((x + 1) * TOTAL_BINS / w);
1190 CLAMP(accum_start, 0, TOTAL_BINS);
1191 CLAMP(accum_end, 0, TOTAL_BINS);
1193 for(int i=accum_start; i<accum_end; ++i )
1194 if( (v=bin[i]) > max ) max = v;
1195 // max = max * h / normalize;
1196 max = log(max) * vnorm;
1197 draw_line(x,y+h - max, x,y+h);
1200 void ScopeHistogram::draw(int flash, int flush)
1202 clear_box(0, 0, get_w(), get_h());
1203 if( gui->use_hist_parade ) {
1204 draw_mode(0, 0xff0000, 0, get_h() / 3);
1205 draw_mode(1, 0x00ff00, get_h() / 3, get_h() / 3);
1206 draw_mode(2, 0x0000ff, get_h() / 3 * 2, get_h() / 3);
1209 draw_mode(3, LTGREY, 0, get_h());
1212 if(flash) this->flash(0);
1213 if(flush) this->flush();
1217 ScopeScopesOn::ScopeScopesOn(ScopeMenu *scope_menu, const char *text, int id)
1220 this->scope_menu = scope_menu;
1224 int ScopeScopesOn::handle_event()
1226 int v = get_checked() ? 0 : 1;
1228 ScopeGUI *gui = scope_menu->gui;
1230 case SCOPE_HISTOGRAM:
1232 if( v ) gui->use_hist_parade = 0;
1234 case SCOPE_HISTOGRAM_RGB:
1235 gui->use_hist_parade = v;
1236 if( v ) gui->use_hist = 0;
1238 case SCOPE_WAVEFORM:
1240 if( v ) gui->use_wave_parade = 0;
1242 case SCOPE_WAVEFORM_RGB:
1243 gui->use_wave_parade = v;
1244 if( v ) gui->use_wave = 0;
1246 case SCOPE_WAVEFORM_PLY:
1247 gui->use_wave_parade = -v;
1248 if( v ) gui->use_wave = 0;
1250 case SCOPE_VECTORSCOPE:
1251 gui->use_vector = v;
1253 case SCOPE_VECTORWHEEL:
1254 gui->use_vector = -v;
1257 gui->toggle_event();
1258 gui->update_toggles();
1259 gui->create_panels();
1260 gui->update_scope();
1265 ScopeMenu::ScopeMenu(ScopeGUI *gui, int x, int y)
1266 : BC_PopupMenu(x, y, xS(100), _("Scopes"))
1271 void ScopeMenu::create_objects()
1274 new ScopeScopesOn(this, _("Histogram"), SCOPE_HISTOGRAM));
1275 add_item(hist_rgb_on =
1276 new ScopeScopesOn(this, _("Histogram RGB"), SCOPE_HISTOGRAM_RGB));
1277 add_item(new BC_MenuItem("-"));
1279 new ScopeScopesOn(this, _("Waveform"), SCOPE_WAVEFORM));
1280 add_item(wave_rgb_on =
1281 new ScopeScopesOn(this, _("Waveform RGB"), SCOPE_WAVEFORM_RGB));
1282 add_item(wave_ply_on =
1283 new ScopeScopesOn(this, _("Waveform ply"), SCOPE_WAVEFORM_PLY));
1284 add_item(new BC_MenuItem("-"));
1286 new ScopeScopesOn(this, _("Vectorscope"), SCOPE_VECTORSCOPE));
1287 add_item(vect_wheel =
1288 new ScopeScopesOn(this, _("VectorWheel"), SCOPE_VECTORWHEEL));
1291 void ScopeMenu::update_toggles()
1293 hist_on->set_checked(gui->use_hist);
1294 hist_rgb_on->set_checked(gui->use_hist_parade);
1295 wave_on->set_checked(gui->use_wave);
1296 wave_rgb_on->set_checked(gui->use_wave_parade>0);
1297 wave_ply_on->set_checked(gui->use_wave_parade<0);
1298 vect_on->set_checked(gui->use_vector>0);
1299 vect_wheel->set_checked(gui->use_vector<0);
1303 ScopeVectGrats::ScopeVectGrats(ScopeGUI *gui, int x, int y)
1304 : BC_PopupMenu(x, y, _("Overlay"))
1309 #define SCOPE_SEARCHPATH "/scopes"
1310 void ScopeVectGrats::create_objects()
1312 gui->grat_paths.remove_all_objects();
1313 ScopeGratItem *item;
1314 add_item(item = new ScopeGratItem(this, _("none"), 0));
1315 if( item->idx == gui->grat_idx ) item->set_checked(1);
1316 gui->grat_paths.append(0);
1318 fs.set_filter("[*.png]");
1319 char scope_path[BCTEXTLEN];
1320 sprintf(scope_path, "%s%s", File::get_plugin_path(), SCOPE_SEARCHPATH);
1321 fs.update(scope_path);
1322 for( int i=0; i<fs.total_files(); ++i ) {
1323 FileItem *file_item = fs.get_entry(i);
1324 if( file_item->get_is_dir() ) continue;
1325 strcpy(scope_path, file_item->get_name());
1326 char *cp = strrchr(scope_path, '.');
1328 add_item(item = new ScopeGratItem(this, scope_path, gui->grat_paths.size()));
1329 if( item->idx == gui->grat_idx ) item->set_checked(1);
1330 gui->grat_paths.append(cstrdup(file_item->get_path()));
1334 ScopeGratItem::ScopeGratItem(ScopeVectGrats *vect_grats, const char *text, int idx)
1337 this->vect_grats = vect_grats;
1341 int ScopeGratItem::handle_event()
1343 for( int i=0,n=vect_grats->total_items(); i<n; ++i ) {
1344 ScopeGratItem *item = (ScopeGratItem *)vect_grats->get_item(i);
1345 item->set_checked(item->idx == idx);
1347 vect_grats->gui->update_graticule(idx);
1352 ScopeGainReset::ScopeGainReset(ScopeGain *gain, int x, int y)
1353 : BC_Button(x, y, gain->gui->theme->get_image_set("reset_button"))
1358 int ScopeGainReset::calculate_w(BC_Theme *theme)
1360 VFrame *vfrm = *theme->get_image_set("reset_button");
1361 return vfrm->get_w();
1364 int ScopeGainReset::handle_event()
1366 gain->slider->update(5);
1367 return gain->handle_event();
1370 ScopeGainSlider::ScopeGainSlider(ScopeGain *gain, int x, int y, int w)
1371 : BC_ISlider(x, y, 0, w, w, 1, 9, *gain->value)
1376 int ScopeGainSlider::handle_event()
1378 return gain->handle_event();
1381 ScopeGain::ScopeGain(ScopeGUI *gui, int x, int y, int w, int *value)
1387 this->value = value;
1392 ScopeGain::~ScopeGain()
1398 int ScopeGain::calculate_h()
1400 return BC_ISlider::get_span(0);
1403 void ScopeGain::create_objects()
1405 reset_w = ScopeGainReset::calculate_w(gui->theme);
1406 gui->add_subwindow(slider = new ScopeGainSlider(this, x, y, w-reset_w-xS(5)));
1407 gui->add_subwindow(reset = new ScopeGainReset(this, x+w-reset_w, y));
1410 int ScopeGain::handle_event()
1412 *value = slider->get_value();
1413 gui->update_scope();
1414 gui->toggle_event();
1418 void ScopeGain::reposition_window(int x, int y)
1420 this->x = x; this->y = y;
1421 slider->reposition_window(x, y);
1422 reset->reposition_window(x+w-reset_w, y);
1425 ScopeWaveSlider::ScopeWaveSlider(ScopeGUI *gui, int x, int y, int w)
1426 : ScopeGain(gui, x, y, w, &gui->use_wave_gain)
1430 ScopeVectSlider::ScopeVectSlider(ScopeGUI *gui, int x, int y, int w)
1431 : ScopeGain(gui, x, y, w, &gui->use_vect_gain)
1435 ScopeSmooth::ScopeSmooth(ScopeGUI *gui, int x, int y)
1436 : BC_CheckBox(x, y, gui->use_smooth, _("Smooth"))
1441 int ScopeSmooth::handle_event()
1443 gui->use_smooth = get_value();
1444 gui->update_scope();
1445 gui->toggle_event();
1449 ScopeRefresh::ScopeRefresh(ScopeGUI *gui, int x, int y)
1450 : BC_CheckBox(x, y, gui->use_refresh, _("Refresh only"))
1455 int ScopeRefresh::handle_event()
1457 gui->use_refresh = get_value();
1458 gui->toggle_event();