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,
47 #define SCOPE_SEARCHPATH "/scopes"
49 #define incr_point(rows,h, iv,comp) { \
50 if(iy >= 0 && iy < h) { \
51 uint8_t *vp = rows[iy] + ix*3 + (comp); \
52 int v = *vp+(iv); *vp = v>0xff ? 0xff : v; } \
54 #define incr_points(rows,h, rv,gv,bv, comps) { \
55 if(iy >= 0 && iy < h) { \
56 uint8_t *vp = rows[iy] + ix*comps; \
57 int v = *vp+(rv); *vp++ = v>0xff ? 0xff : v; \
58 v = *vp+(gv); *vp++ = v>0xff ? 0xff : v; \
59 v = *vp+(bv); *vp = v>0xff ? 0xff : v; } \
61 #define decr_points(rows,h, rv,gv,bv, comps) { \
62 if(iy >= 0 && iy < h) { \
63 uint8_t *vp = rows[iy] + ix*comps; \
64 int v = *vp-(rv); *vp++ = v<0 ? 0 : v; \
65 v = *vp-(gv); *vp++ = v<0 ? 0 : v; \
66 v = *vp-(bv); *vp = v<0 ? 0 : v; } \
68 #define clip_points(rows,h, rv,gv,bv) { \
69 if(iy >= 0 && iy < h) { \
70 uint8_t *vp = rows[iy] + ix*3; \
71 int v = *vp+(rv); *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
72 v = *vp+(gv); *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
73 v = *vp+(bv); *vp = v>0xff ? 0xff : v<0 ? 0 : v; } \
76 #define PROCESS_PIXEL(column) { \
77 /* Calculate histogram */ \
79 int v_i = (intensity - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
80 CLAMP(v_i, 0, TOTAL_BINS - 1); \
83 if(use_hist_parade) { \
84 int r_i = (r - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
85 int g_i = (g - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
86 int b_i = (b - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
87 CLAMP(r_i, 0, TOTAL_BINS - 1); \
88 CLAMP(g_i, 0, TOTAL_BINS - 1); \
89 CLAMP(b_i, 0, TOTAL_BINS - 1); \
94 /* Calculate waveform */ \
95 if(use_wave || use_wave_parade) { \
96 int ix = column * wave_w / dat_w; \
97 if(ix >= 0 && ix < wave_w-1) { \
98 if(use_wave_parade > 0) { \
100 int iy = wave_h - ((r - FLOAT_MIN) / \
101 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
102 incr_point(waveform_rows,wave_h, winc,0); \
104 iy = wave_h - ((g - FLOAT_MIN) / \
105 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
106 incr_point(waveform_rows,wave_h, winc,1); \
108 iy = wave_h - ((b - FLOAT_MIN) / \
109 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
110 incr_point(waveform_rows,wave_h, winc,2); \
112 else if(use_wave_parade < 0) { \
113 int iy = wave_h - ((r - FLOAT_MIN) / \
114 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
115 clip_points(waveform_rows,wave_h, winc,-winc,-winc); \
116 iy = wave_h - ((g - FLOAT_MIN) / \
117 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
118 clip_points(waveform_rows,wave_h, -winc,winc,-winc); \
119 iy = wave_h - ((b - FLOAT_MIN) / \
120 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
121 clip_points(waveform_rows,wave_h, -winc,-winc,winc); \
123 else { float yinc = 3*winc; \
124 float rinc = yinc*(r-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
125 float ginc = yinc*(g-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
126 float binc = yinc*(b-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
127 int iy = wave_h - ((intensity - FLOAT_MIN) / \
128 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
129 incr_points(waveform_rows,wave_h, rinc, ginc, binc, 3); \
133 /* Calculate vectorscope */ \
134 if(use_vector > 0) { \
135 double t = TO_RAD(-h); \
136 float adjacent = sin(t), opposite = cos(t); \
137 int ix = vector_cx + adjacent * (s) / (FLOAT_MAX) * radius; \
138 int iy = vector_cy - opposite * (s) / (FLOAT_MAX) * radius; \
139 CLAMP(ix, 0, vector_w - 1); \
140 HSV::hsv_to_rgb(r, g, b, h, s, vincf); \
144 incr_points(vector_rows,vector_h, rv,gv,bv, 4); \
146 else if(use_vector < 0) { \
147 double t = TO_RAD(-h); \
148 float adjacent = sin(t), opposite = cos(t); \
149 int ix = vector_cx + adjacent * (s) / (FLOAT_MAX) * radius; \
150 int iy = vector_cy - opposite * (s) / (FLOAT_MAX) * radius; \
151 CLAMP(ix, 0, vector_w - 1); \
152 decr_points(vector_rows,vector_h, vinc,vinc,vinc, 4); \
156 #define PROCESS_RGB_PIXEL(type,max, column) { \
157 type *rp = (type *)row; \
158 r = (float)rp[0] / max; \
159 g = (float)rp[1] / max; \
160 b = (float)rp[2] / max; \
161 HSV::rgb_to_hsv(r, g, b, h, s, v); \
163 PROCESS_PIXEL(column) \
166 #define PROCESS_BGR_PIXEL(type,max, column) { \
167 type *rp = (type *)row; \
168 b = (float)rp[0] / max; \
169 g = (float)rp[1] / max; \
170 r = (float)rp[2] / max; \
171 HSV::rgb_to_hsv(r, g, b, h, s, v); \
173 PROCESS_PIXEL(column) \
176 #define PROCESS_YUV_PIXEL(column, y_in, u_in, v_in) { \
177 YUV::yuv.yuv_to_rgb_f(r, g, b, y_in, u_in, v_in); \
178 HSV::rgb_to_hsv(r, g, b, h, s, v); \
180 PROCESS_PIXEL(column) \
183 void ScopeUnit::process_package(LoadPackage *package)
185 ScopePackage *pkg = (ScopePackage*)package;
190 int use_hist = gui->use_hist;
191 int use_hist_parade = gui->use_hist_parade;
192 int use_vector = gui->use_vector;
193 int use_wave = gui->use_wave;
194 int use_wave_parade = gui->use_wave_parade;
195 int vector_cx = gui->vector_cx;
196 int vector_cy = gui->vector_cy;
197 int radius = gui->radius;
198 VFrame *waveform_vframe = gui->waveform_vframe;
199 VFrame *vector_vframe = gui->vector_vframe;
200 int wave_h = waveform_vframe->get_h();
201 int wave_w = waveform_vframe->get_w();
202 int vector_h = vector_vframe->get_h();
203 int vector_w = vector_vframe->get_w();
204 int dat_w = gui->data_frame->get_w();
205 int dat_h = gui->data_frame->get_h();
206 int winc = (wave_w * wave_h) / (dat_w * dat_h);
207 if( use_wave_parade ) winc *= 3;
208 winc += 2; winc = (winc << gui->use_wave_gain) / 4;
209 int vinc = 3*(vector_w * vector_h) / (dat_w * dat_h);
210 vinc += 2; vinc = (vinc << gui->use_vect_gain) / 4;
212 uint8_t **waveform_rows = waveform_vframe->get_rows();
213 uint8_t **vector_rows = vector_vframe->get_rows();
214 uint8_t **rows = gui->data_frame->get_rows();
216 switch( gui->data_frame->get_color_model() ) {
218 for( int y=pkg->row1; y<pkg->row2; ++y ) {
219 uint8_t *row = rows[y];
220 for( int x=0; x<dat_w; ++x ) {
221 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
222 row += 3*sizeof(uint8_t);
227 for( int y=pkg->row1; y<pkg->row2; ++y ) {
228 uint8_t *row = rows[y];
229 for( int x=0; x<dat_w; ++x ) {
230 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
231 row += 4*sizeof(uint8_t);
236 for( int y=pkg->row1; y<pkg->row2; ++y ) {
237 uint8_t *row = rows[y];
238 for( int x=0; x<dat_w; ++x ) {
239 PROCESS_RGB_PIXEL(uint16_t,0xffff, x)
240 row += 3*sizeof(uint16_t);
244 case BC_RGBA16161616:
245 for( int y=pkg->row1; y<pkg->row2; ++y ) {
246 uint8_t *row = rows[y];
247 for( int x=0; x<dat_w; ++x ) {
248 PROCESS_RGB_PIXEL(uint16_t,0xffff, x)
249 row += 4*sizeof(uint16_t);
254 for( int y=pkg->row1; y<pkg->row2; ++y ) {
255 uint8_t *row = rows[y];
256 for( int x=0; x<dat_w; ++x ) {
257 PROCESS_BGR_PIXEL(uint8_t,0xff, x)
258 row += 3*sizeof(uint8_t);
263 for( int y=pkg->row1; y<pkg->row2; ++y ) {
264 uint8_t *row = rows[y];
265 for( int x=0; x<dat_w; ++x ) {
266 PROCESS_BGR_PIXEL(uint8_t,0xff, x)
267 row += 4*sizeof(uint8_t);
272 for( int y=pkg->row1; y<pkg->row2; ++y ) {
273 uint8_t *row = rows[y];
274 for( int x=0; x<dat_w; ++x ) {
275 PROCESS_RGB_PIXEL(float,1.f, x)
276 row += 3*sizeof(float);
281 for( int y=pkg->row1; y<pkg->row2; ++y ) {
282 uint8_t *row = rows[y];
283 for( int x=0; x<dat_w; ++x ) {
284 PROCESS_RGB_PIXEL(float,1.f, x)
285 row += 4*sizeof(float);
290 for( int y=pkg->row1; y<pkg->row2; ++y ) {
291 uint8_t *row = rows[y];
292 for( int x=0; x<dat_w; ++x ) {
293 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
294 row += 3*sizeof(uint8_t);
300 for( int y=pkg->row1; y<pkg->row2; ++y ) {
301 uint8_t *row = rows[y];
302 for( int x=0; x<dat_w; ++x ) {
303 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
304 row += 4*sizeof(uint8_t);
309 uint8_t *yp = gui->data_frame->get_y();
310 uint8_t *up = gui->data_frame->get_u();
311 uint8_t *vp = gui->data_frame->get_v();
312 for( int y=pkg->row1; y<pkg->row2; ++y ) {
313 uint8_t *y_row = yp + y * dat_w;
314 uint8_t *u_row = up + (y / 2) * (dat_w / 2);
315 uint8_t *v_row = vp + (y / 2) * (dat_w / 2);
316 for( int x=0; x<dat_w; x+=2 ) {
317 PROCESS_YUV_PIXEL(x, *y_row, *u_row, *v_row);
319 PROCESS_YUV_PIXEL(x + 1, *y_row, *u_row, *v_row);
320 ++y_row; ++u_row; ++v_row;
325 for( int y=pkg->row1; y<pkg->row2; ++y ) {
326 uint8_t *row = rows[y];
327 for( int x=0; x<dat_w; x+=2 ) {
328 PROCESS_YUV_PIXEL(x, row[0], row[1], row[3]);
329 PROCESS_YUV_PIXEL(x + 1, row[2], row[1], row[3]);
330 row += 4*sizeof(uint8_t);
336 printf("ScopeUnit::process_package %d: color_model=%d unrecognized\n",
337 __LINE__, gui->data_frame->get_color_model());
343 ScopeEngine::ScopeEngine(ScopeGUI *gui, int cpus)
344 : LoadServer(cpus, cpus)
346 //printf("ScopeEngine::ScopeEngine %d cpus=%d\n", __LINE__, cpus);
350 ScopeEngine::~ScopeEngine()
354 void ScopeEngine::init_packages()
356 int y = 0, h = gui->data_frame->get_h();
357 for( int i=0,n=LoadServer::get_total_packages(); i<n; ) {
358 ScopePackage *pkg = (ScopePackage*)get_package(i);
360 pkg->row2 = y = (++i * h) / n;
363 for( int i=0,n=LoadServer::get_total_packages(); i<n; ++i ) {
364 ScopeUnit *unit = (ScopeUnit*)get_client(i);
365 for( int j=0; j<HIST_SECTIONS; ++j )
366 bzero(unit->bins[j], sizeof(int) * TOTAL_BINS);
371 LoadClient* ScopeEngine::new_client()
373 return new ScopeUnit(gui, this);
376 LoadPackage* ScopeEngine::new_package()
378 return new ScopePackage;
381 void ScopeEngine::process()
385 for(int i = 0; i < HIST_SECTIONS; i++)
386 bzero(gui->bins[i], sizeof(int) * TOTAL_BINS);
388 for(int i=0,n=get_total_clients(); i<n; ++i ) {
389 ScopeUnit *unit = (ScopeUnit*)get_client(i);
390 for( int j=0; j<HIST_SECTIONS; ++j ) {
391 int *bp = gui->bins[j], *up = unit->bins[j];
392 for( int k=TOTAL_BINS; --k>=0; ++bp,++up ) *bp += *up;
398 ScopeGUI::ScopeGUI(Theme *theme,
399 int x, int y, int w, int h, int cpus)
400 : PluginClientWindow(_(PROGRAM_NAME ": Scopes"),
401 x, y, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
412 ScopeGUI::ScopeGUI(PluginClient *plugin, int w, int h)
413 : PluginClientWindow(plugin, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
419 this->theme = plugin->get_theme();
420 this->cpus = plugin->PluginClient::smp + 1;
424 ScopeGUI::~ScopeGUI()
426 delete waveform_vframe;
427 delete vector_vframe;
438 void ScopeGUI::reset()
471 vector_w = vector_h = 0;
472 vector_cx = vector_cy = 0;
475 dark_color = (text_color>>2) & 0x3f3f3f;
476 BC_Resources *resources = BC_WindowBase::get_resources();
477 if( resources->bg_color == 0xffffff )
478 text_color = dark_color;
481 void ScopeGUI::create_objects()
483 if( use_hist && use_hist_parade )
485 if( use_wave && use_wave_parade )
487 if( !engine ) engine = new ScopeEngine(this, cpus);
488 grat_idx = use_graticule; // last graticule
491 lock_window("ScopeGUI::create_objects");
492 int margin = theme->widget_border;
493 int x = margin, y = margin;
494 add_subwindow(scope_menu = new ScopeMenu(this, x, y));
495 scope_menu->create_objects();
496 int x1 = x + scope_menu->get_w() + 2*margin;
497 add_subwindow(settings = new ScopeSettings(this, x1, y));
498 settings->create_objects();
507 void ScopeGUI::create_panels()
509 calculate_sizes(get_w(), get_h());
510 int slider_w = xS(100);
511 if( (use_wave || use_wave_parade) ) {
512 int px = wave_x + wave_w - slider_w - xS(5);
513 int py = wave_y - ScopeGain::calculate_h() - yS(5);
515 add_subwindow(waveform = new ScopeWaveform(this,
516 wave_x, wave_y, wave_w, wave_h));
517 waveform->create_objects();
518 wave_slider = new ScopeWaveSlider(this, px, py, slider_w);
519 wave_slider->create_objects();
522 waveform->reposition_window(
523 wave_x, wave_y, wave_w, wave_h);
524 waveform->clear_box(0, 0, wave_w, wave_h);
525 wave_slider->reposition_window(px, py);
528 else if( !(use_wave || use_wave_parade) && waveform ) {
529 delete waveform; waveform = 0;
530 delete wave_slider; wave_slider = 0;
534 int vx = vector_x + vector_w - slider_w - xS(5);
535 int vy = vector_y - ScopeGain::calculate_h() - yS(5);
537 add_subwindow(vectorscope = new ScopeVectorscope(this,
538 vector_x, vector_y, vector_w, vector_h));
539 vectorscope->create_objects();
540 vect_slider = new ScopeVectSlider(this, vx, vy, slider_w);
541 vect_slider->create_objects();
544 vectorscope->reposition_window(
545 vector_x, vector_y, vector_w, vector_h);
546 vectorscope->clear_box(0, 0, vector_w, vector_h);
547 vect_slider->reposition_window(vx, vy);
550 else if( !use_vector && vectorscope ) {
551 delete vectorscope; vectorscope = 0;
552 delete vect_slider; vect_slider = 0;
555 if( (use_hist || use_hist_parade) ) {
557 // printf("ScopeGUI::create_panels %d %d %d %d %d\n", __LINE__,
558 // hist_x, hist_y, hist_w, hist_h);
559 add_subwindow(histogram = new ScopeHistogram(this,
560 hist_x, hist_y, hist_w, hist_h));
561 histogram->create_objects();
564 histogram->reposition_window(
565 hist_x, hist_y, hist_w, hist_h);
566 histogram->clear_box(0, 0, hist_w, hist_h);
569 else if( !(use_hist || use_hist_parade) ) {
570 delete histogram; histogram = 0;
575 draw_overlays(1, 1, 0);
578 void ScopeGUI::clear_points(int flash)
581 histogram->clear_point();
583 waveform->clear_point();
585 vectorscope->clear_point();
586 if( histogram && flash )
588 if( waveform && flash )
590 if( vectorscope && flash )
591 vectorscope->flash(0);
594 void ScopeGUI::toggle_event()
598 void ScopeGUI::calculate_sizes(int w, int h)
600 int margin = theme->widget_border;
601 int menu_h = scope_menu->get_h() + ScopeGain::calculate_h() + margin * 3;
602 int text_w = get_text_width(SMALLFONT, "000") + margin * 2;
603 int total_panels = ((use_hist || use_hist_parade) ? 1 : 0) +
604 ((use_wave || use_wave_parade) ? 1 : 0) +
605 (use_vector ? 1 : 0);
608 int panel_w = (w - margin) / (total_panels > 0 ? total_panels : 1);
609 // Vectorscope determines the size of everything else
613 vector_x = w - panel_w + text_w;
614 vector_w = w - margin - vector_x;
616 vector_h = h - vector_y - margin;
618 if( vector_w > vector_h ) {
620 vector_x = w - margin - vector_w;
624 panel_w = (vector_x - text_w - margin) / total_panels;
626 vector_cx = vector_w / 2;
627 vector_cy = vector_h / 2;
628 radius = bmin(vector_cx, vector_cy);
630 // Histogram is always 1st panel
631 if( use_hist || use_hist_parade ) {
634 hist_w = panel_w - margin;
635 hist_h = h - hist_y - margin;
641 if( use_wave || use_wave_parade ) {
644 wave_w = panel_w - margin - text_w;
645 wave_h = h - wave_y - margin;
651 void ScopeGUI::allocate_vframes()
653 if(waveform_vframe) delete waveform_vframe;
654 if(vector_vframe) delete vector_vframe;
655 if(wheel_vframe) delete wheel_vframe;
658 //printf("ScopeGUI::allocate_vframes %d %d %d %d %d\n", __LINE__,
659 // wave_w, wave_h, vector_w, vector_h);
660 int xs16 = xS(16), ys16 = yS(16);
661 w = MAX(wave_w, xs16);
662 h = MAX(wave_h, ys16);
663 waveform_vframe = new VFrame(w, h, BC_RGB888);
664 w = MAX(vector_w, xs16);
665 h = MAX(vector_h, ys16);
666 vector_vframe = new VFrame(w, h, BC_RGBA8888);
667 vector_vframe->set_clear_color(BLACK, 0xff);
672 int ScopeGUI::resize_event(int w, int h)
674 clear_box(0, 0, w, h);
677 calculate_sizes(w, h);
678 int margin = theme->widget_border;
681 waveform->reposition_window(wave_x, wave_y, wave_w, wave_h);
682 waveform->clear_box(0, 0, wave_w, wave_h);
683 int px = wave_x + wave_w - wave_slider->get_w() - margin;
684 int py = wave_y - wave_slider->get_h() - margin;
685 wave_slider->reposition_window(px, py);
689 histogram->reposition_window(hist_x, hist_y, hist_w, hist_h);
690 histogram->clear_box(0, 0, hist_w, hist_h);
694 vectorscope->reposition_window(vector_x, vector_y, vector_w, vector_h);
695 vectorscope->clear_box(0, 0, vector_w, vector_h);
696 int vx = vector_x + vector_w - vect_slider->get_w() - margin;
697 int vy = vector_y - vect_slider->get_h() - margin;
698 vect_slider->reposition_window(vx, vy);
703 draw_overlays(1, 1, 1);
708 int ScopeGUI::translation_event()
712 PluginClientWindow::translation_event();
717 void ScopeGUI::draw_overlays(int overlays, int borders, int flush)
719 int margin = theme->widget_border;
720 if( overlays && borders ) {
721 clear_box(0, 0, get_w(), get_h());
726 set_color(text_color);
729 if( histogram && (use_hist || use_hist_parade) ) {
730 histogram->draw_line(hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), 0,
731 hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), hist_h);
732 histogram->draw_line(hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), 0,
733 hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), hist_h);
735 histogram->draw_point();
741 if( waveform && (use_wave || use_wave_parade) ) {
742 for( int i=0; i<=WAVEFORM_DIVISIONS; ++i ) {
743 int y = wave_h * i / WAVEFORM_DIVISIONS;
744 int text_y = y + wave_y + get_text_ascent(SMALLFONT) / 2;
745 CLAMP(text_y, waveform->get_y() + get_text_ascent(SMALLFONT), waveform->get_y() + waveform->get_h() - 1);
746 char string[BCTEXTLEN];
747 sprintf(string, "%d", (int)lround((FLOAT_MAX -
748 i * (FLOAT_MAX - FLOAT_MIN) / WAVEFORM_DIVISIONS) * 100));
749 int text_x = wave_x - get_text_width(SMALLFONT, string) - margin;
750 set_color(text_color);
751 draw_text(text_x, text_y, string);
752 CLAMP(y, 0, waveform->get_h() - 1);
753 set_color(dark_color);
754 waveform->draw_line(0, y, wave_w, y);
755 waveform->draw_rectangle(0, 0, wave_w, wave_h);
758 int y1 = wave_h * 1.8 / WAVEFORM_DIVISIONS;
759 int text_y1 = y1 + wave_y + get_text_ascent(SMALLFONT) / 2;
760 CLAMP(text_y1, waveform->get_y() + get_text_ascent(SMALLFONT), waveform->get_y() + waveform->get_h() - 1);
761 char string1[BCTEXTLEN];
762 sprintf( string1, "%d",(int)lround((FLOAT_MAX -
763 1.8 * (FLOAT_MAX - FLOAT_MIN ) / WAVEFORM_DIVISIONS ) * 100) );
764 int text_x1 = wave_x + get_text_width(SMALLFONT, string1) - margin +wave_w;
765 set_color(text_color);
766 draw_text(text_x1, text_y1, string1);
767 CLAMP(y1, 0, waveform->get_h() - 1);
768 set_color(dark_color);
769 waveform->draw_line(0, y1, wave_w, y1);
771 int y2 = wave_h * 10.4 / WAVEFORM_DIVISIONS;
772 int text_y2 = y2 + wave_y + get_text_ascent(SMALLFONT) / 2;
773 CLAMP(text_y2, waveform->get_y() + get_text_ascent(SMALLFONT), waveform->get_y() + waveform->get_h() - 1);
774 char string2[BCTEXTLEN];
775 sprintf( string2, "%d",(int)lround((FLOAT_MAX -
776 10.4 * (FLOAT_MAX - FLOAT_MIN ) / WAVEFORM_DIVISIONS) * 100) );
777 set_color(text_color);
778 draw_text(text_x1, text_y2, string2);
779 CLAMP(y2, 0, waveform->get_h() - 1);
780 set_color(dark_color);
781 waveform->draw_line(0, y2, wave_w, y2);
784 waveform->draw_point();
788 // Vectorscope overlay
789 if( vectorscope && use_vector ) {
790 set_color(text_color);
791 vectorscope->draw_point();
792 vectorscope->flash(0);
796 set_font(MEDIUMFONT);
801 if( use_hist || use_hist_parade ) {
802 draw_3d_border(hist_x - 2, hist_y - 2, hist_w + 4, hist_h + 4,
803 get_bg_color(), BLACK, MDGREY, get_bg_color());
805 if( use_wave || use_wave_parade ) {
806 draw_3d_border(wave_x - 2, wave_y - 2, wave_w + 4, wave_h + 4,
807 get_bg_color(), BLACK, MDGREY, get_bg_color());
810 draw_3d_border(vector_x - 2, vector_y - 2, vector_w + 4, vector_h + 4,
811 get_bg_color(), BLACK, MDGREY, get_bg_color());
816 if(flush) this->flush();
819 void ScopeGUI::draw_scope()
821 int graticule = use_vector < 0 ? grat_idx : 0;
822 if( grat_image && use_graticule != graticule ) {
823 delete grat_image; grat_image = 0;
825 if( !grat_image && graticule > 0 )
826 grat_image = VFramePng::vframe_png(grat_paths[graticule]);
829 overlay = new OverlayFrame(1);
830 int cx = vector_cx, cy = vector_cy, r = radius;
831 int iw = grat_image->get_w(), ih = grat_image->get_h();
832 overlay->overlay(vector_vframe, grat_image,
833 0,0, iw, ih, cx-r,cy-r, cx+r, cy+r,
834 1, TRANSFER_NORMAL, CUBIC_CUBIC);
836 use_graticule = graticule;
837 vectorscope->draw_vframe(vector_vframe);
838 if( use_vector > 0 ) {
839 int margin = theme->widget_border;
841 for( int i=1; i<=VECTORSCOPE_DIVISIONS; i+=2 ) {
842 int y = vector_cy - radius * i / VECTORSCOPE_DIVISIONS;
843 int text_y = y + vector_y + get_text_ascent(SMALLFONT) / 2;
844 set_color(text_color);
845 char string[BCTEXTLEN];
846 sprintf(string, "%d",
847 (int)((FLOAT_MAX / VECTORSCOPE_DIVISIONS * i) * 100));
848 int text_x = vector_x - get_text_width(SMALLFONT, string) - margin;
849 draw_text(text_x, text_y, string);
850 int x = vector_cx - radius * i / VECTORSCOPE_DIVISIONS;
851 int w = radius * i / VECTORSCOPE_DIVISIONS * 2;
852 int h = radius * i / VECTORSCOPE_DIVISIONS * 2;
853 if( i+2 > VECTORSCOPE_DIVISIONS )
855 set_color(dark_color);
856 vectorscope->draw_circle(x, y, w, h);
858 float th = TO_RAD(90 + 32.875);
859 vectorscope->draw_radient(th, 0.1f, .75f, dark_color);
864 void ScopeGUI::update_graticule(int idx)
871 void ScopeGUI::draw_colorwheel(VFrame *dst, int bg_color)
873 // downfactor radius to prevent extreme edge from showing behind graticule
874 float cx = vector_cx, cy = vector_cy, rad = radius * 0.99;
875 int color_model = dst->get_color_model();
876 int bpp = BC_CModels::calculate_pixelsize(color_model);
877 int bg_r = (bg_color>>16) & 0xff;
878 int bg_g = (bg_color>> 8) & 0xff;
879 int bg_b = (bg_color>> 0) & 0xff;
880 int w = dst->get_w(), h = dst->get_h();
881 unsigned char **rows = dst->get_rows();
882 for( int y=0; y<h; ++y ) {
883 unsigned char *row = rows[y];
884 for( int x=0; x<w; ++x,row+=bpp ) {
885 int dx = cx-x, dy = cy-y;
886 float d = sqrt(dx*dx + dy*dy);
889 float h = TO_DEG(atan2(cx-x, cy-y));
890 if( h < 0 ) h += 360;
891 float s = d / rad, v = 255;
892 HSV::hsv_to_rgb(r, g, b, h, s, v);
895 r = bg_r; g = bg_g; b = bg_b;
897 row[0] = r; row[1] = g; row[2] = b; row[3] = 0xff;
903 void ScopeGUI::process(VFrame *output_frame)
905 lock_window("ScopeGUI::process");
906 this->output_frame = output_frame;
907 int ow = output_frame->get_w(), oh = output_frame->get_h();
909 VFrame::get_temp(temp_frame, ow, oh, BC_RGB161616);
910 temp_frame->transfer_from(output_frame);
911 if( !box_blur ) box_blur = new BoxBlur(cpus);
912 box_blur->blur(temp_frame, temp_frame, 2, 2);
913 data_frame = temp_frame;
916 data_frame = output_frame;
917 frame_w = data_frame->get_w();
918 bzero(waveform_vframe->get_data(), waveform_vframe->get_data_size());
920 vector_vframe->clear_frame();
921 else if( use_vector < 0 ) {
922 if( wheel_vframe && (
923 wheel_vframe->get_w() != vector_w ||
924 wheel_vframe->get_h() != vector_h ) ) {
925 delete wheel_vframe; wheel_vframe = 0;
927 if( !wheel_vframe ) {
928 wheel_vframe = new VFrame(vector_w, vector_h, BC_RGBA8888);
929 draw_colorwheel(wheel_vframe, BLACK);
931 vector_vframe->copy_from(wheel_vframe);
936 histogram->draw(0, 0);
938 waveform->draw_vframe(waveform_vframe);
942 draw_overlays(1, 0, 1);
946 void ScopeGUI::update_toggles()
948 scope_menu->update_toggles();
949 settings->update_toggles();
952 ScopePanel::ScopePanel(ScopeGUI *gui, int x, int y, int w, int h)
953 : BC_SubWindow(x, y, w, h, BLACK)
959 void ScopePanel::create_objects()
961 set_cursor(CROSS_CURSOR, 0, 0);
962 clear_box(0, 0, get_w(), get_h());
965 void ScopePanel::update_point(int x, int y)
969 void ScopePanel::draw_point()
973 void ScopePanel::clear_point()
977 int ScopePanel::button_press_event()
979 if( is_event_win() && cursor_inside() ) {
980 gui->clear_points(1);
982 int x = get_cursor_x();
983 int y = get_cursor_y();
984 CLAMP(x, 0, get_w() - 1);
985 CLAMP(y, 0, get_h() - 1);
993 int ScopePanel::cursor_motion_event()
996 int x = get_cursor_x();
997 int y = get_cursor_y();
998 CLAMP(x, 0, get_w() - 1);
999 CLAMP(y, 0, get_h() - 1);
1007 int ScopePanel::button_release_event()
1018 ScopeWaveform::ScopeWaveform(ScopeGUI *gui,
1019 int x, int y, int w, int h)
1020 : ScopePanel(gui, x, y, w, h)
1026 void ScopeWaveform::update_point(int x, int y)
1031 int frame_x = x * gui->frame_w / get_w();
1033 if( gui->use_wave_parade ) {
1034 if( x > get_w() / 3 * 2 )
1035 frame_x = (x - get_w() / 3 * 2) * gui->frame_w / (get_w() / 3);
1036 else if( x > get_w() / 3 )
1037 frame_x = (x - get_w() / 3) * gui->frame_w / (get_w() / 3);
1039 frame_x = x * gui->frame_w / (get_w() / 3);
1042 float value = ((float)get_h() - y) / get_h() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1043 char string[BCTEXTLEN];
1044 sprintf(string, "X: %d Value: %.3f", frame_x, value);
1045 show_tooltip(string);
1051 void ScopeWaveform::draw_point()
1055 set_color(0xffffff);
1057 draw_line(0, drag_y, get_w(), drag_y);
1058 draw_line(drag_x, 0, drag_x, get_h());
1064 void ScopeWaveform::clear_point()
1071 ScopeVectorscope::ScopeVectorscope(ScopeGUI *gui,
1072 int x, int y, int w, int h)
1073 : ScopePanel(gui, x, y, w, h)
1079 void ScopeVectorscope::clear_point()
1087 void ScopeVectorscope::update_point(int x, int y)
1091 int dx = x - gui->vector_cx, dy = y - gui->vector_cy;
1092 drag_radius = sqrt(dx*dx + dy*dy);
1093 if( drag_radius > gui->radius ) drag_radius = gui->radius;
1094 float saturation = (float)drag_radius / gui->radius * FLOAT_MAX;
1095 drag_angle = atan2(-dy, dx);
1096 float hue = TO_DEG(drag_angle) - 90;
1097 if( hue < 0 ) hue += 360;
1099 char string[BCTEXTLEN];
1100 sprintf(string, "Hue: %.3f Sat: %.3f", hue, saturation);
1101 show_tooltip(string);
1108 void ScopeVectorscope::draw_point()
1111 draw_point(drag_angle, drag_radius, RED);
1115 void ScopeVectorscope::draw_point(float th, float r, int color)
1120 int cx = gui->vector_cx, cy = gui->vector_cy;
1121 draw_circle(cx-r, cy-r, r*2, r*2);
1123 float radius = gui->radius;
1124 draw_radient(th, 0.f, drag_radius/radius, color);
1128 void ScopeVectorscope::draw_radient(float th, float r1, float r2, int color)
1130 if( r2 > 0 && r2 >= r1 ) {
1133 int r = gui->radius;
1134 double cth = r*cos(th), sth = -r*sin(th);
1135 int cx = gui->vector_cx, cy = gui->vector_cy;
1136 draw_line(cx+r1*cth, cy+r1*sth, cx+r2*cth, cy+r2*sth);
1142 ScopeHistogram::ScopeHistogram(ScopeGUI *gui,
1143 int x, int y, int w, int h)
1144 : ScopePanel(gui, x, y, w, h)
1149 void ScopeHistogram::clear_point()
1156 void ScopeHistogram::draw_point()
1160 set_color(0xffffff);
1162 draw_line(drag_x, 0, drag_x, get_h());
1168 void ScopeHistogram::update_point(int x, int y)
1172 float value = (float)x / get_w() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1174 char string[BCTEXTLEN];
1175 sprintf(string, "Value: %.3f", value);
1176 show_tooltip(string);
1184 void ScopeHistogram::draw_mode(int mode, int color, int y, int h)
1186 // Highest of all bins
1187 int normalize = 1, w = get_w();
1188 int *bin = gui->bins[mode], v;
1189 for( int i=0; i<TOTAL_BINS; ++i )
1190 if( (v=bin[i]) > normalize ) normalize = v;
1191 double norm = normalize>1 ? log(normalize) : 1e-4;
1192 double vnorm = h / norm;
1194 for( int x=0; x<w; ++x ) {
1195 int accum_start = (int)(x * TOTAL_BINS / w);
1196 int accum_end = (int)((x + 1) * TOTAL_BINS / w);
1197 CLAMP(accum_start, 0, TOTAL_BINS);
1198 CLAMP(accum_end, 0, TOTAL_BINS);
1200 for(int i=accum_start; i<accum_end; ++i )
1201 if( (v=bin[i]) > max ) max = v;
1202 // max = max * h / normalize;
1203 max = log(max) * vnorm;
1204 draw_line(x,y+h - max, x,y+h);
1207 void ScopeHistogram::draw(int flash, int flush)
1209 clear_box(0, 0, get_w(), get_h());
1210 if( gui->use_hist_parade ) {
1211 draw_mode(0, 0xff0000, 0, get_h() / 3);
1212 draw_mode(1, 0x00ff00, get_h() / 3, get_h() / 3);
1213 draw_mode(2, 0x0000ff, get_h() / 3 * 2, get_h() / 3);
1216 draw_mode(3, LTGREY, 0, get_h());
1219 if(flash) this->flash(0);
1220 if(flush) this->flush();
1224 ScopeScopesOn::ScopeScopesOn(ScopeMenu *scope_menu, const char *text, int id)
1227 this->scope_menu = scope_menu;
1231 int ScopeScopesOn::handle_event()
1233 int v = get_checked() ? 0 : 1;
1235 ScopeGUI *gui = scope_menu->gui;
1237 case SCOPE_HISTOGRAM:
1239 if( v ) gui->use_hist_parade = 0;
1241 case SCOPE_HISTOGRAM_RGB:
1242 gui->use_hist_parade = v;
1243 if( v ) gui->use_hist = 0;
1245 case SCOPE_WAVEFORM:
1247 if( v ) gui->use_wave_parade = 0;
1249 case SCOPE_WAVEFORM_RGB:
1250 gui->use_wave_parade = v;
1251 if( v ) gui->use_wave = 0;
1253 case SCOPE_WAVEFORM_PLY:
1254 gui->use_wave_parade = -v;
1255 if( v ) gui->use_wave = 0;
1257 case SCOPE_VECTORSCOPE:
1258 gui->use_vector = v;
1260 case SCOPE_VECTORWHEEL:
1261 gui->use_vector = -v;
1264 gui->toggle_event();
1265 gui->update_toggles();
1266 gui->create_panels();
1267 gui->update_scope();
1272 ScopeMenu::ScopeMenu(ScopeGUI *gui, int x, int y)
1273 : BC_PopupMenu(x, y, xS(110), _("Scopes"))
1278 void ScopeMenu::create_objects()
1281 new ScopeScopesOn(this, _("Histogram"), SCOPE_HISTOGRAM));
1282 add_item(hist_rgb_on =
1283 new ScopeScopesOn(this, _("Histogram RGB"), SCOPE_HISTOGRAM_RGB));
1284 add_item(new BC_MenuItem("-"));
1286 new ScopeScopesOn(this, _("Waveform"), SCOPE_WAVEFORM));
1287 add_item(wave_rgb_on =
1288 new ScopeScopesOn(this, _("Waveform RGB"), SCOPE_WAVEFORM_RGB));
1289 add_item(wave_ply_on =
1290 new ScopeScopesOn(this, _("Waveform ply"), SCOPE_WAVEFORM_PLY));
1291 add_item(new BC_MenuItem("-"));
1293 new ScopeScopesOn(this, _("Vectorscope"), SCOPE_VECTORSCOPE));
1294 add_item(vect_wheel =
1295 new ScopeScopesOn(this, _("VectorWheel"), SCOPE_VECTORWHEEL));
1298 void ScopeMenu::update_toggles()
1300 hist_on->set_checked(gui->use_hist);
1301 hist_rgb_on->set_checked(gui->use_hist_parade);
1302 wave_on->set_checked(gui->use_wave);
1303 wave_rgb_on->set_checked(gui->use_wave_parade>0);
1304 wave_ply_on->set_checked(gui->use_wave_parade<0);
1305 vect_on->set_checked(gui->use_vector>0);
1306 vect_wheel->set_checked(gui->use_vector<0);
1310 ScopeSettingOn::ScopeSettingOn(ScopeSettings *settings, const char *text, int id)
1313 this->settings = settings;
1317 int ScopeSettingOn::handle_event()
1319 int v = get_checked() ? 0 : 1;
1321 ScopeGUI *gui = settings->gui;
1324 gui->use_smooth = v;
1327 gui->use_refresh = v;
1328 gui->use_release = 0;
1331 gui->use_release = v;
1332 gui->use_refresh = 0;
1334 gui->toggle_event();
1335 gui->update_toggles();
1336 gui->update_scope();
1341 ScopeSettings::ScopeSettings(ScopeGUI *gui, int x, int y)
1342 : BC_PopupMenu(x, y, xS(150), _("Settings"))
1349 void ScopeSettings::create_objects()
1351 add_item(smooth_on =
1352 new ScopeSettingOn(this, _("Smooth"), SCOPE_SMOOTH));
1353 smooth_on->set_checked(gui->use_smooth);
1354 if( gui->use_refresh >= 0 ) {
1355 add_item(refresh_on =
1356 new ScopeSettingOn(this, _("Refresh on Stop"), SCOPE_REFRESH));
1357 add_item(release_on =
1358 new ScopeSettingOn(this, _("Refresh on Release"), SCOPE_RELEASE));
1359 refresh_on->set_checked(gui->use_refresh);
1360 release_on->set_checked(gui->use_release);
1362 add_item(new BC_MenuItem(_("-VectorWheel Grids-")));
1364 gui->grat_paths.remove_all_objects();
1365 ScopeGratItem *item;
1366 add_item(item = new ScopeGratItem(this, _("None"), 0));
1367 if( item->idx == gui->grat_idx ) item->set_checked(1);
1368 gui->grat_paths.append(0);
1370 fs.set_filter("[*.png]");
1371 char scope_path[BCTEXTLEN];
1372 sprintf(scope_path, "%s%s", File::get_plugin_path(), SCOPE_SEARCHPATH);
1373 fs.update(scope_path);
1374 for( int i=0; i<fs.total_files(); ++i ) {
1375 FileItem *file_item = fs.get_entry(i);
1376 if( file_item->get_is_dir() ) continue;
1377 strcpy(scope_path, file_item->get_name());
1378 char *cp = strrchr(scope_path, '.');
1380 add_item(item = new ScopeGratItem(this, scope_path, gui->grat_paths.size()));
1381 if( item->idx == gui->grat_idx ) item->set_checked(1);
1382 gui->grat_paths.append(cstrdup(file_item->get_path()));
1386 void ScopeSettings::update_toggles()
1389 refresh_on->set_checked(gui->use_refresh);
1391 release_on->set_checked(gui->use_release);
1394 ScopeGratItem::ScopeGratItem(ScopeSettings *settings, const char *text, int idx)
1397 this->settings = settings;
1401 int ScopeGratItem::handle_event()
1403 for( int i=0,n=settings->total_items(); i<n; ++i ) {
1404 ScopeGratItem *item = (ScopeGratItem *)settings->get_item(i);
1405 item->set_checked(item->idx == idx);
1407 settings->gui->update_graticule(idx);
1412 ScopeGainReset::ScopeGainReset(ScopeGain *gain, int x, int y)
1413 : BC_Button(x, y, gain->gui->theme->get_image_set("reset_button"))
1418 int ScopeGainReset::calculate_w(BC_Theme *theme)
1420 VFrame *vfrm = *theme->get_image_set("reset_button");
1421 return vfrm->get_w();
1424 int ScopeGainReset::handle_event()
1426 gain->slider->update(5);
1427 return gain->handle_event();
1430 ScopeGainSlider::ScopeGainSlider(ScopeGain *gain, int x, int y, int w)
1431 : BC_ISlider(x, y, 0, w, w, 1, 9, *gain->value)
1436 int ScopeGainSlider::handle_event()
1438 return gain->handle_event();
1441 ScopeGain::ScopeGain(ScopeGUI *gui, int x, int y, int w, int *value)
1447 this->value = value;
1452 ScopeGain::~ScopeGain()
1458 int ScopeGain::calculate_h()
1460 return BC_ISlider::get_span(0);
1463 void ScopeGain::create_objects()
1465 reset_w = ScopeGainReset::calculate_w(gui->theme);
1466 gui->add_subwindow(slider = new ScopeGainSlider(this, x, y, w-reset_w-xS(5)));
1467 gui->add_subwindow(reset = new ScopeGainReset(this, x+w-reset_w, y));
1470 int ScopeGain::handle_event()
1472 *value = slider->get_value();
1473 gui->update_scope();
1474 gui->toggle_event();
1478 void ScopeGain::reposition_window(int x, int y)
1480 this->x = x; this->y = y;
1481 slider->reposition_window(x, y);
1482 reset->reposition_window(x+w-reset_w, y);
1485 ScopeWaveSlider::ScopeWaveSlider(ScopeGUI *gui, int x, int y, int w)
1486 : ScopeGain(gui, x, y, w, &gui->use_wave_gain)
1490 ScopeVectSlider::ScopeVectSlider(ScopeGUI *gui, int x, int y, int w)
1491 : ScopeGain(gui, x, y, w, &gui->use_vect_gain)