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 waveform->draw_point();
762 // Vectorscope overlay
763 if( vectorscope && use_vector ) {
764 set_color(text_color);
765 vectorscope->draw_point();
766 vectorscope->flash(0);
770 set_font(MEDIUMFONT);
775 if( use_hist || use_hist_parade ) {
776 draw_3d_border(hist_x - 2, hist_y - 2, hist_w + 4, hist_h + 4,
777 get_bg_color(), BLACK, MDGREY, get_bg_color());
779 if( use_wave || use_wave_parade ) {
780 draw_3d_border(wave_x - 2, wave_y - 2, wave_w + 4, wave_h + 4,
781 get_bg_color(), BLACK, MDGREY, get_bg_color());
784 draw_3d_border(vector_x - 2, vector_y - 2, vector_w + 4, vector_h + 4,
785 get_bg_color(), BLACK, MDGREY, get_bg_color());
790 if(flush) this->flush();
793 void ScopeGUI::draw_scope()
795 int graticule = use_vector < 0 ? grat_idx : 0;
796 if( grat_image && use_graticule != graticule ) {
797 delete grat_image; grat_image = 0;
799 if( !grat_image && graticule > 0 )
800 grat_image = VFramePng::vframe_png(grat_paths[graticule]);
803 overlay = new OverlayFrame(1);
804 int cx = vector_cx, cy = vector_cy, r = radius;
805 int iw = grat_image->get_w(), ih = grat_image->get_h();
806 overlay->overlay(vector_vframe, grat_image,
807 0,0, iw, ih, cx-r,cy-r, cx+r, cy+r,
808 1, TRANSFER_NORMAL, CUBIC_CUBIC);
810 use_graticule = graticule;
811 vectorscope->draw_vframe(vector_vframe);
812 if( use_vector > 0 ) {
813 int margin = theme->widget_border;
815 for( int i=1; i<=VECTORSCOPE_DIVISIONS; i+=2 ) {
816 int y = vector_cy - radius * i / VECTORSCOPE_DIVISIONS;
817 int text_y = y + vector_y + get_text_ascent(SMALLFONT) / 2;
818 set_color(text_color);
819 char string[BCTEXTLEN];
820 sprintf(string, "%d",
821 (int)((FLOAT_MAX / VECTORSCOPE_DIVISIONS * i) * 100));
822 int text_x = vector_x - get_text_width(SMALLFONT, string) - margin;
823 draw_text(text_x, text_y, string);
824 int x = vector_cx - radius * i / VECTORSCOPE_DIVISIONS;
825 int w = radius * i / VECTORSCOPE_DIVISIONS * 2;
826 int h = radius * i / VECTORSCOPE_DIVISIONS * 2;
827 if( i+2 > VECTORSCOPE_DIVISIONS )
829 set_color(dark_color);
830 vectorscope->draw_circle(x, y, w, h);
832 float th = TO_RAD(90 + 32.875);
833 vectorscope->draw_radient(th, 0.1f, .75f, dark_color);
838 void ScopeGUI::update_graticule(int idx)
845 void ScopeGUI::draw_colorwheel(VFrame *dst, int bg_color)
847 // downfactor radius to prevent extreme edge from showing behind graticule
848 float cx = vector_cx, cy = vector_cy, rad = radius * 0.99;
849 int color_model = dst->get_color_model();
850 int bpp = BC_CModels::calculate_pixelsize(color_model);
851 int bg_r = (bg_color>>16) & 0xff;
852 int bg_g = (bg_color>> 8) & 0xff;
853 int bg_b = (bg_color>> 0) & 0xff;
854 int w = dst->get_w(), h = dst->get_h();
855 unsigned char **rows = dst->get_rows();
856 for( int y=0; y<h; ++y ) {
857 unsigned char *row = rows[y];
858 for( int x=0; x<w; ++x,row+=bpp ) {
859 int dx = cx-x, dy = cy-y;
860 float d = sqrt(dx*dx + dy*dy);
863 float h = TO_DEG(atan2(cx-x, cy-y));
864 if( h < 0 ) h += 360;
865 float s = d / rad, v = 255;
866 HSV::hsv_to_rgb(r, g, b, h, s, v);
869 r = bg_r; g = bg_g; b = bg_b;
871 row[0] = r; row[1] = g; row[2] = b; row[3] = 0xff;
877 void ScopeGUI::process(VFrame *output_frame)
879 lock_window("ScopeGUI::process");
880 this->output_frame = output_frame;
881 int ow = output_frame->get_w(), oh = output_frame->get_h();
883 VFrame::get_temp(temp_frame, ow, oh, BC_RGB161616);
884 temp_frame->transfer_from(output_frame);
885 if( !box_blur ) box_blur = new BoxBlur(cpus);
886 box_blur->blur(temp_frame, temp_frame, 2, 2);
887 data_frame = temp_frame;
890 data_frame = output_frame;
891 frame_w = data_frame->get_w();
892 bzero(waveform_vframe->get_data(), waveform_vframe->get_data_size());
894 vector_vframe->clear_frame();
895 else if( use_vector < 0 ) {
896 if( wheel_vframe && (
897 wheel_vframe->get_w() != vector_w ||
898 wheel_vframe->get_h() != vector_h ) ) {
899 delete wheel_vframe; wheel_vframe = 0;
901 if( !wheel_vframe ) {
902 wheel_vframe = new VFrame(vector_w, vector_h, BC_RGBA8888);
903 draw_colorwheel(wheel_vframe, BLACK);
905 vector_vframe->copy_from(wheel_vframe);
910 histogram->draw(0, 0);
912 waveform->draw_vframe(waveform_vframe);
916 draw_overlays(1, 0, 1);
920 void ScopeGUI::update_toggles()
922 scope_menu->update_toggles();
923 settings->update_toggles();
926 ScopePanel::ScopePanel(ScopeGUI *gui, int x, int y, int w, int h)
927 : BC_SubWindow(x, y, w, h, BLACK)
933 void ScopePanel::create_objects()
935 set_cursor(CROSS_CURSOR, 0, 0);
936 clear_box(0, 0, get_w(), get_h());
939 void ScopePanel::update_point(int x, int y)
943 void ScopePanel::draw_point()
947 void ScopePanel::clear_point()
951 int ScopePanel::button_press_event()
953 if( is_event_win() && cursor_inside() ) {
954 gui->clear_points(1);
956 int x = get_cursor_x();
957 int y = get_cursor_y();
958 CLAMP(x, 0, get_w() - 1);
959 CLAMP(y, 0, get_h() - 1);
967 int ScopePanel::cursor_motion_event()
970 int x = get_cursor_x();
971 int y = get_cursor_y();
972 CLAMP(x, 0, get_w() - 1);
973 CLAMP(y, 0, get_h() - 1);
981 int ScopePanel::button_release_event()
992 ScopeWaveform::ScopeWaveform(ScopeGUI *gui,
993 int x, int y, int w, int h)
994 : ScopePanel(gui, x, y, w, h)
1000 void ScopeWaveform::update_point(int x, int y)
1005 int frame_x = x * gui->frame_w / get_w();
1007 if( gui->use_wave_parade ) {
1008 if( x > get_w() / 3 * 2 )
1009 frame_x = (x - get_w() / 3 * 2) * gui->frame_w / (get_w() / 3);
1010 else if( x > get_w() / 3 )
1011 frame_x = (x - get_w() / 3) * gui->frame_w / (get_w() / 3);
1013 frame_x = x * gui->frame_w / (get_w() / 3);
1016 float value = ((float)get_h() - y) / get_h() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1017 char string[BCTEXTLEN];
1018 sprintf(string, "X: %d Value: %.3f", frame_x, value);
1019 show_tooltip(string);
1025 void ScopeWaveform::draw_point()
1029 set_color(0xffffff);
1031 draw_line(0, drag_y, get_w(), drag_y);
1032 draw_line(drag_x, 0, drag_x, get_h());
1038 void ScopeWaveform::clear_point()
1045 ScopeVectorscope::ScopeVectorscope(ScopeGUI *gui,
1046 int x, int y, int w, int h)
1047 : ScopePanel(gui, x, y, w, h)
1053 void ScopeVectorscope::clear_point()
1061 void ScopeVectorscope::update_point(int x, int y)
1065 int dx = x - gui->vector_cx, dy = y - gui->vector_cy;
1066 drag_radius = sqrt(dx*dx + dy*dy);
1067 if( drag_radius > gui->radius ) drag_radius = gui->radius;
1068 float saturation = (float)drag_radius / gui->radius * FLOAT_MAX;
1069 drag_angle = atan2(-dy, dx);
1070 float hue = TO_DEG(drag_angle) - 90;
1071 if( hue < 0 ) hue += 360;
1073 char string[BCTEXTLEN];
1074 sprintf(string, "Hue: %.3f Sat: %.3f", hue, saturation);
1075 show_tooltip(string);
1082 void ScopeVectorscope::draw_point()
1085 draw_point(drag_angle, drag_radius, RED);
1089 void ScopeVectorscope::draw_point(float th, float r, int color)
1094 int cx = gui->vector_cx, cy = gui->vector_cy;
1095 draw_circle(cx-r, cy-r, r*2, r*2);
1097 float radius = gui->radius;
1098 draw_radient(th, 0.f, drag_radius/radius, color);
1102 void ScopeVectorscope::draw_radient(float th, float r1, float r2, int color)
1104 if( r2 > 0 && r2 >= r1 ) {
1107 int r = gui->radius;
1108 double cth = r*cos(th), sth = -r*sin(th);
1109 int cx = gui->vector_cx, cy = gui->vector_cy;
1110 draw_line(cx+r1*cth, cy+r1*sth, cx+r2*cth, cy+r2*sth);
1116 ScopeHistogram::ScopeHistogram(ScopeGUI *gui,
1117 int x, int y, int w, int h)
1118 : ScopePanel(gui, x, y, w, h)
1123 void ScopeHistogram::clear_point()
1130 void ScopeHistogram::draw_point()
1134 set_color(0xffffff);
1136 draw_line(drag_x, 0, drag_x, get_h());
1142 void ScopeHistogram::update_point(int x, int y)
1146 float value = (float)x / get_w() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1148 char string[BCTEXTLEN];
1149 sprintf(string, "Value: %.3f", value);
1150 show_tooltip(string);
1158 void ScopeHistogram::draw_mode(int mode, int color, int y, int h)
1160 // Highest of all bins
1161 int normalize = 1, w = get_w();
1162 int *bin = gui->bins[mode], v;
1163 for( int i=0; i<TOTAL_BINS; ++i )
1164 if( (v=bin[i]) > normalize ) normalize = v;
1165 double norm = normalize>1 ? log(normalize) : 1e-4;
1166 double vnorm = h / norm;
1168 for( int x=0; x<w; ++x ) {
1169 int accum_start = (int)(x * TOTAL_BINS / w);
1170 int accum_end = (int)((x + 1) * TOTAL_BINS / w);
1171 CLAMP(accum_start, 0, TOTAL_BINS);
1172 CLAMP(accum_end, 0, TOTAL_BINS);
1174 for(int i=accum_start; i<accum_end; ++i )
1175 if( (v=bin[i]) > max ) max = v;
1176 // max = max * h / normalize;
1177 max = log(max) * vnorm;
1178 draw_line(x,y+h - max, x,y+h);
1181 void ScopeHistogram::draw(int flash, int flush)
1183 clear_box(0, 0, get_w(), get_h());
1184 if( gui->use_hist_parade ) {
1185 draw_mode(0, 0xff0000, 0, get_h() / 3);
1186 draw_mode(1, 0x00ff00, get_h() / 3, get_h() / 3);
1187 draw_mode(2, 0x0000ff, get_h() / 3 * 2, get_h() / 3);
1190 draw_mode(3, LTGREY, 0, get_h());
1193 if(flash) this->flash(0);
1194 if(flush) this->flush();
1198 ScopeScopesOn::ScopeScopesOn(ScopeMenu *scope_menu, const char *text, int id)
1201 this->scope_menu = scope_menu;
1205 int ScopeScopesOn::handle_event()
1207 int v = get_checked() ? 0 : 1;
1209 ScopeGUI *gui = scope_menu->gui;
1211 case SCOPE_HISTOGRAM:
1213 if( v ) gui->use_hist_parade = 0;
1215 case SCOPE_HISTOGRAM_RGB:
1216 gui->use_hist_parade = v;
1217 if( v ) gui->use_hist = 0;
1219 case SCOPE_WAVEFORM:
1221 if( v ) gui->use_wave_parade = 0;
1223 case SCOPE_WAVEFORM_RGB:
1224 gui->use_wave_parade = v;
1225 if( v ) gui->use_wave = 0;
1227 case SCOPE_WAVEFORM_PLY:
1228 gui->use_wave_parade = -v;
1229 if( v ) gui->use_wave = 0;
1231 case SCOPE_VECTORSCOPE:
1232 gui->use_vector = v;
1234 case SCOPE_VECTORWHEEL:
1235 gui->use_vector = -v;
1238 gui->toggle_event();
1239 gui->update_toggles();
1240 gui->create_panels();
1241 gui->update_scope();
1246 ScopeMenu::ScopeMenu(ScopeGUI *gui, int x, int y)
1247 : BC_PopupMenu(x, y, xS(110), _("Scopes"))
1252 void ScopeMenu::create_objects()
1255 new ScopeScopesOn(this, _("Histogram"), SCOPE_HISTOGRAM));
1256 add_item(hist_rgb_on =
1257 new ScopeScopesOn(this, _("Histogram RGB"), SCOPE_HISTOGRAM_RGB));
1258 add_item(new BC_MenuItem("-"));
1260 new ScopeScopesOn(this, _("Waveform"), SCOPE_WAVEFORM));
1261 add_item(wave_rgb_on =
1262 new ScopeScopesOn(this, _("Waveform RGB"), SCOPE_WAVEFORM_RGB));
1263 add_item(wave_ply_on =
1264 new ScopeScopesOn(this, _("Waveform ply"), SCOPE_WAVEFORM_PLY));
1265 add_item(new BC_MenuItem("-"));
1267 new ScopeScopesOn(this, _("Vectorscope"), SCOPE_VECTORSCOPE));
1268 add_item(vect_wheel =
1269 new ScopeScopesOn(this, _("VectorWheel"), SCOPE_VECTORWHEEL));
1272 void ScopeMenu::update_toggles()
1274 hist_on->set_checked(gui->use_hist);
1275 hist_rgb_on->set_checked(gui->use_hist_parade);
1276 wave_on->set_checked(gui->use_wave);
1277 wave_rgb_on->set_checked(gui->use_wave_parade>0);
1278 wave_ply_on->set_checked(gui->use_wave_parade<0);
1279 vect_on->set_checked(gui->use_vector>0);
1280 vect_wheel->set_checked(gui->use_vector<0);
1284 ScopeSettingOn::ScopeSettingOn(ScopeSettings *settings, const char *text, int id)
1287 this->settings = settings;
1291 int ScopeSettingOn::handle_event()
1293 int v = get_checked() ? 0 : 1;
1295 ScopeGUI *gui = settings->gui;
1298 gui->use_smooth = v;
1301 gui->use_refresh = v;
1302 gui->use_release = 0;
1305 gui->use_release = v;
1306 gui->use_refresh = 0;
1308 gui->toggle_event();
1309 gui->update_toggles();
1310 gui->update_scope();
1315 ScopeSettings::ScopeSettings(ScopeGUI *gui, int x, int y)
1316 : BC_PopupMenu(x, y, xS(125), _("Settings"))
1323 void ScopeSettings::create_objects()
1325 add_item(smooth_on =
1326 new ScopeSettingOn(this, _("Smooth"), SCOPE_SMOOTH));
1327 smooth_on->set_checked(gui->use_smooth);
1328 if( gui->use_refresh >= 0 ) {
1329 add_item(refresh_on =
1330 new ScopeSettingOn(this, _("Refresh on Stop"), SCOPE_REFRESH));
1331 add_item(release_on =
1332 new ScopeSettingOn(this, _("Refresh on Release"), SCOPE_RELEASE));
1333 refresh_on->set_checked(gui->use_refresh);
1334 release_on->set_checked(gui->use_release);
1336 add_item(new BC_MenuItem(_("-VectorWheel Grids-")));
1338 gui->grat_paths.remove_all_objects();
1339 ScopeGratItem *item;
1340 add_item(item = new ScopeGratItem(this, _("None"), 0));
1341 if( item->idx == gui->grat_idx ) item->set_checked(1);
1342 gui->grat_paths.append(0);
1344 fs.set_filter("[*.png]");
1345 char scope_path[BCTEXTLEN];
1346 sprintf(scope_path, "%s%s", File::get_plugin_path(), SCOPE_SEARCHPATH);
1347 fs.update(scope_path);
1348 for( int i=0; i<fs.total_files(); ++i ) {
1349 FileItem *file_item = fs.get_entry(i);
1350 if( file_item->get_is_dir() ) continue;
1351 strcpy(scope_path, file_item->get_name());
1352 char *cp = strrchr(scope_path, '.');
1354 add_item(item = new ScopeGratItem(this, scope_path, gui->grat_paths.size()));
1355 if( item->idx == gui->grat_idx ) item->set_checked(1);
1356 gui->grat_paths.append(cstrdup(file_item->get_path()));
1360 void ScopeSettings::update_toggles()
1363 refresh_on->set_checked(gui->use_refresh);
1365 release_on->set_checked(gui->use_release);
1368 ScopeGratItem::ScopeGratItem(ScopeSettings *settings, const char *text, int idx)
1371 this->settings = settings;
1375 int ScopeGratItem::handle_event()
1377 for( int i=0,n=settings->total_items(); i<n; ++i ) {
1378 ScopeGratItem *item = (ScopeGratItem *)settings->get_item(i);
1379 item->set_checked(item->idx == idx);
1381 settings->gui->update_graticule(idx);
1386 ScopeGainReset::ScopeGainReset(ScopeGain *gain, int x, int y)
1387 : BC_Button(x, y, gain->gui->theme->get_image_set("reset_button"))
1392 int ScopeGainReset::calculate_w(BC_Theme *theme)
1394 VFrame *vfrm = *theme->get_image_set("reset_button");
1395 return vfrm->get_w();
1398 int ScopeGainReset::handle_event()
1400 gain->slider->update(5);
1401 return gain->handle_event();
1404 ScopeGainSlider::ScopeGainSlider(ScopeGain *gain, int x, int y, int w)
1405 : BC_ISlider(x, y, 0, w, w, 1, 9, *gain->value)
1410 int ScopeGainSlider::handle_event()
1412 return gain->handle_event();
1415 ScopeGain::ScopeGain(ScopeGUI *gui, int x, int y, int w, int *value)
1421 this->value = value;
1426 ScopeGain::~ScopeGain()
1432 int ScopeGain::calculate_h()
1434 return BC_ISlider::get_span(0);
1437 void ScopeGain::create_objects()
1439 reset_w = ScopeGainReset::calculate_w(gui->theme);
1440 gui->add_subwindow(slider = new ScopeGainSlider(this, x, y, w-reset_w-xS(5)));
1441 gui->add_subwindow(reset = new ScopeGainReset(this, x+w-reset_w, y));
1444 int ScopeGain::handle_event()
1446 *value = slider->get_value();
1447 gui->update_scope();
1448 gui->toggle_event();
1452 void ScopeGain::reposition_window(int x, int y)
1454 this->x = x; this->y = y;
1455 slider->reposition_window(x, y);
1456 reset->reposition_window(x+w-reset_w, y);
1459 ScopeWaveSlider::ScopeWaveSlider(ScopeGUI *gui, int x, int y, int w)
1460 : ScopeGain(gui, x, y, w, &gui->use_wave_gain)
1464 ScopeVectSlider::ScopeVectSlider(ScopeGUI *gui, int x, int y, int w)
1465 : ScopeGain(gui, x, y, w, &gui->use_vect_gain)