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"
26 #include "scopewindow.h"
31 ScopePackage::ScopePackage()
36 ScopeUnit::ScopeUnit(ScopeGUI *gui,
44 #define incr_point(rows,h, iv,comp) { \
45 if(iy >= 0 && iy < h) { \
46 uint8_t *vp = rows[iy] + ix*3 + (comp); \
47 int v = *vp+(iv); *vp = v>0xff ? 0xff : v; } \
49 #define incr_points(rows,h, rv,gv,bv) { \
50 if(iy >= 0 && iy < h) { \
51 uint8_t *vp = rows[iy] + ix*3; \
52 int v = *vp+(rv); *vp++ = v>0xff ? 0xff : v; \
53 v = *vp+(gv); *vp++ = v>0xff ? 0xff : v; \
54 v = *vp+(bv); *vp = v>0xff ? 0xff : v; } \
56 #define clip_points(rows,h, rv,gv,bv) { \
57 if(iy >= 0 && iy < h) { \
58 uint8_t *vp = rows[iy] + ix*3; \
59 int v = *vp+(rv); *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
60 v = *vp+(gv); *vp++ = v>0xff ? 0xff : v<0 ? 0 : v; \
61 v = *vp+(bv); *vp = v>0xff ? 0xff : v<0 ? 0 : v; } \
64 #define PROCESS_PIXEL(column) { \
65 /* Calculate histogram */ \
67 int v_i = (intensity - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
68 CLAMP(v_i, 0, TOTAL_BINS - 1); \
71 if(use_hist_parade) { \
72 int r_i = (r - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
73 int g_i = (g - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
74 int b_i = (b - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
75 CLAMP(r_i, 0, TOTAL_BINS - 1); \
76 CLAMP(g_i, 0, TOTAL_BINS - 1); \
77 CLAMP(b_i, 0, TOTAL_BINS - 1); \
82 /* Calculate waveform */ \
83 if(use_wave || use_wave_parade) { \
84 int ix = column * wave_w / dat_w; \
85 if(ix >= 0 && ix < wave_w-1) { \
86 if(use_wave_parade > 0) { \
88 int iy = wave_h - ((r - FLOAT_MIN) / \
89 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
90 incr_point(waveform_rows,wave_h, winc,0); \
92 iy = wave_h - ((g - FLOAT_MIN) / \
93 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
94 incr_point(waveform_rows,wave_h, winc,1); \
96 iy = wave_h - ((b - FLOAT_MIN) / \
97 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
98 incr_point(waveform_rows,wave_h, winc,2); \
100 else if(use_wave_parade < 0) { \
101 int iy = wave_h - ((r - FLOAT_MIN) / \
102 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
103 clip_points(waveform_rows,wave_h, winc,-winc,-winc); \
104 iy = wave_h - ((g - FLOAT_MIN) / \
105 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
106 clip_points(waveform_rows,wave_h, -winc,winc,-winc); \
107 iy = wave_h - ((b - FLOAT_MIN) / \
108 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
109 clip_points(waveform_rows,wave_h, -winc,-winc,winc); \
111 else { float yinc = 3*winc; \
112 float rinc = yinc*(r-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
113 float ginc = yinc*(g-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
114 float binc = yinc*(b-FLOAT_MIN) / (FLOAT_MAX-FLOAT_MIN) + 3; \
115 int iy = wave_h - ((intensity - FLOAT_MIN) / \
116 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
117 incr_points(waveform_rows,wave_h, rinc, ginc, binc); \
121 /* Calculate vectorscope */ \
124 if( th < 0 ) th += 360; \
125 double t = TO_RAD(th); \
126 float adjacent = -cos(t), opposite = sin(t); \
127 int ix = vector_w / 2 + adjacent * (s) / (FLOAT_MAX) * radius; \
128 int iy = vector_h / 2 - opposite * (s) / (FLOAT_MAX) * radius; \
129 CLAMP(ix, 0, vector_w - 1); \
130 /* Get color with full saturation & value */ \
131 float r_f, g_f, b_f; \
132 if( (h=h-90) < 0 ) h += 360; \
133 HSV::hsv_to_rgb(r_f, g_f, b_f, h, s, 1); \
134 int rv = CLIP(r_f, 0, 1) * vinc + 3; \
135 int gv = CLIP(g_f, 0, 1) * vinc + 3; \
136 int bv = CLIP(b_f, 0, 1) * vinc + 3; \
137 incr_points(vector_rows,vector_h, rv,gv,bv); \
141 #define PROCESS_RGB_PIXEL(type,max, column) { \
142 type *rp = (type *)row; \
143 r = (float)rp[0] / max; \
144 g = (float)rp[1] / max; \
145 b = (float)rp[2] / max; \
146 HSV::rgb_to_hsv(r, g, b, h, s, v); \
148 PROCESS_PIXEL(column) \
151 #define PROCESS_BGR_PIXEL(type,max, column) { \
152 type *rp = (type *)row; \
153 b = (float)rp[0] / max; \
154 g = (float)rp[1] / max; \
155 r = (float)rp[2] / max; \
156 HSV::rgb_to_hsv(r, g, b, h, s, v); \
158 PROCESS_PIXEL(column) \
161 #define PROCESS_YUV_PIXEL(column, y_in, u_in, v_in) { \
162 YUV::yuv.yuv_to_rgb_f(r, g, b, y_in, u_in, v_in); \
163 HSV::rgb_to_hsv(r, g, b, h, s, v); \
165 PROCESS_PIXEL(column) \
168 void ScopeUnit::process_package(LoadPackage *package)
170 ScopePackage *pkg = (ScopePackage*)package;
175 int use_hist = gui->use_hist;
176 int use_hist_parade = gui->use_hist_parade;
177 int use_vector = gui->use_vector;
178 int use_wave = gui->use_wave;
179 int use_wave_parade = gui->use_wave_parade;
180 VFrame *waveform_vframe = gui->waveform_vframe;
181 VFrame *vector_vframe = gui->vector_vframe;
182 int wave_h = waveform_vframe->get_h();
183 int wave_w = waveform_vframe->get_w();
184 int vector_h = vector_vframe->get_h();
185 int vector_w = vector_vframe->get_w();
186 int dat_w = gui->data_frame->get_w();
187 int dat_h = gui->data_frame->get_h();
188 int winc = (wave_w * wave_h) / (dat_w * dat_h);
189 if( use_wave_parade ) winc *= 3;
190 winc += 2; winc *= gui->use_wave_gain;
191 int vinc = 3*(vector_w * vector_h) / (dat_w * dat_h) + 2;
192 vinc *= gui->use_vect_gain;
193 float radius = MIN(gui->vector_w / 2, gui->vector_h / 2);
194 uint8_t **waveform_rows = waveform_vframe->get_rows();
195 uint8_t **vector_rows = vector_vframe->get_rows();
196 uint8_t **rows = gui->data_frame->get_rows();
198 switch( gui->data_frame->get_color_model() ) {
200 for( int y=pkg->row1; y<pkg->row2; ++y ) {
201 uint8_t *row = rows[y];
202 for( int x=0; x<dat_w; ++x ) {
203 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
204 row += 3*sizeof(uint8_t);
209 for( int y=pkg->row1; y<pkg->row2; ++y ) {
210 uint8_t *row = rows[y];
211 for( int x=0; x<dat_w; ++x ) {
212 PROCESS_RGB_PIXEL(uint8_t,0xff, x)
213 row += 4*sizeof(uint8_t);
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(uint16_t,0xffff, x)
222 row += 3*sizeof(uint16_t);
226 case BC_RGBA16161616:
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(uint16_t,0xffff, x)
231 row += 4*sizeof(uint16_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_BGR_PIXEL(uint8_t,0xff, x)
240 row += 3*sizeof(uint8_t);
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_BGR_PIXEL(uint8_t,0xff, x)
249 row += 4*sizeof(uint8_t);
254 for( int y=pkg->row1; y<pkg->row2; ++y ) {
255 float *row = (float*)rows[y];
256 for( int x=0; x<dat_w; ++x ) {
257 PROCESS_RGB_PIXEL(float,1.f, x)
258 row += 3*sizeof(float);
263 for( int y=pkg->row1; y<pkg->row2; ++y ) {
264 float *row = (float*)rows[y];
265 for( int x=0; x<dat_w; ++x ) {
266 PROCESS_RGB_PIXEL(float,1.f, x)
267 row += 4*sizeof(float);
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_YUV_PIXEL(x, row[0], row[1], row[2])
276 row += 3*sizeof(uint8_t);
282 for( int y=pkg->row1; y<pkg->row2; ++y ) {
283 uint8_t *row = rows[y];
284 for( int x=0; x<dat_w; ++x ) {
285 PROCESS_YUV_PIXEL(x, row[0], row[1], row[2])
286 row += 4*sizeof(uint8_t);
291 uint8_t *yp = gui->data_frame->get_y();
292 uint8_t *up = gui->data_frame->get_u();
293 uint8_t *vp = gui->data_frame->get_v();
294 for( int y=pkg->row1; y<pkg->row2; ++y ) {
295 uint8_t *y_row = yp + y * dat_w;
296 uint8_t *u_row = up + (y / 2) * (dat_w / 2);
297 uint8_t *v_row = vp + (y / 2) * (dat_w / 2);
298 for( int x=0; x<dat_w; x+=2 ) {
299 PROCESS_YUV_PIXEL(x, *y_row, *u_row, *v_row);
301 PROCESS_YUV_PIXEL(x + 1, *y_row, *u_row, *v_row);
302 ++y_row; ++u_row; ++v_row;
307 for( int y=pkg->row1; y<pkg->row2; ++y ) {
308 uint8_t *row = rows[y];
309 for( int x=0; x<dat_w; x+=2 ) {
310 PROCESS_YUV_PIXEL(x, row[0], row[1], row[3]);
311 PROCESS_YUV_PIXEL(x + 1, row[2], row[1], row[3]);
312 row += 4*sizeof(uint8_t);
318 printf("ScopeUnit::process_package %d: color_model=%d unrecognized\n",
319 __LINE__, gui->data_frame->get_color_model());
325 ScopeEngine::ScopeEngine(ScopeGUI *gui, int cpus)
326 : LoadServer(cpus, cpus)
328 //printf("ScopeEngine::ScopeEngine %d cpus=%d\n", __LINE__, cpus);
332 ScopeEngine::~ScopeEngine()
336 void ScopeEngine::init_packages()
338 int y = 0, h = gui->data_frame->get_h();
339 for( int i=0,n=LoadServer::get_total_packages(); i<n; ) {
340 ScopePackage *pkg = (ScopePackage*)get_package(i);
342 pkg->row2 = y = (++i * h) / n;
345 for( int i=0,n=LoadServer::get_total_packages(); i<n; ++i ) {
346 ScopeUnit *unit = (ScopeUnit*)get_client(i);
347 for( int j=0; j<HIST_SECTIONS; ++j )
348 bzero(unit->bins[j], sizeof(int) * TOTAL_BINS);
353 LoadClient* ScopeEngine::new_client()
355 return new ScopeUnit(gui, this);
358 LoadPackage* ScopeEngine::new_package()
360 return new ScopePackage;
363 void ScopeEngine::process()
367 for(int i = 0; i < HIST_SECTIONS; i++)
368 bzero(gui->bins[i], sizeof(int) * TOTAL_BINS);
370 for(int i=0,n=get_total_clients(); i<n; ++i ) {
371 ScopeUnit *unit = (ScopeUnit*)get_client(i);
372 for( int j=0; j<HIST_SECTIONS; ++j ) {
373 int *bp = gui->bins[j], *up = unit->bins[j];
374 for( int k=TOTAL_BINS; --k>=0; ++bp,++up ) *bp += *up;
380 ScopeGUI::ScopeGUI(Theme *theme,
381 int x, int y, int w, int h, int cpus)
382 : PluginClientWindow(_(PROGRAM_NAME ": Scopes"),
383 x, y, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
389 this->temp_frame = 0;
395 ScopeGUI::ScopeGUI(PluginClient *plugin, int w, int h)
396 : PluginClientWindow(plugin, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
402 this->temp_frame = 0;
403 this->theme = plugin->get_theme();
404 this->cpus = plugin->PluginClient::smp + 1;
408 ScopeGUI::~ScopeGUI()
410 delete waveform_vframe;
411 delete vector_vframe;
417 void ScopeGUI::reset()
438 vector_w = vector_h = 0;
442 void ScopeGUI::create_objects()
444 if( use_hist && use_hist_parade )
446 if( use_wave && use_wave_parade )
448 if( !engine ) engine = new ScopeEngine(this, cpus);
450 lock_window("ScopeGUI::create_objects");
451 int x = theme->widget_border;
452 int y = theme->widget_border;
453 add_subwindow(smooth = new ScopeSmooth(this, x, y));
454 y += smooth->get_h() + theme->widget_border;
455 add_subwindow(scope_menu = new ScopeMenu(this, x, y));
456 scope_menu->create_objects();
457 x += scope_menu->get_w() + theme->widget_border;
458 add_subwindow(value_text = new BC_Title(x, y, ""));
459 y += scope_menu->get_h() + theme->widget_border;
468 void ScopeGUI::create_panels()
470 calculate_sizes(get_w(), get_h());
471 int slider_w = xS(100);
472 if( (use_wave || use_wave_parade) ) {
473 int px = wave_x + wave_w - slider_w - xS(5);
474 int py = wave_y - ScopeWaveSlider::get_span(0) - yS(5);
476 add_subwindow(waveform = new ScopeWaveform(this,
477 wave_x, wave_y, wave_w, wave_h));
478 waveform->create_objects();
479 add_subwindow(wave_slider = new ScopeWaveSlider(this, px, py, slider_w));
482 waveform->reposition_window(
483 wave_x, wave_y, wave_w, wave_h);
484 waveform->clear_box(0, 0, wave_w, wave_h);
485 wave_slider->reposition_window(px, py);
488 else if( !(use_wave || use_wave_parade) && waveform ) {
489 delete waveform; waveform = 0;
490 delete wave_slider; wave_slider = 0;
494 int vx = vector_x + vector_w - slider_w - xS(5);
495 int vy = vector_y - ScopeVectSlider::get_span(0) - yS(5);
497 add_subwindow(vectorscope = new ScopeVectorscope(this,
498 vector_x, vector_y, vector_w, vector_h));
499 vectorscope->create_objects();
500 add_subwindow(vect_slider = new ScopeVectSlider(this, vx, vy, slider_w));
503 vectorscope->reposition_window(
504 vector_x, vector_y, vector_w, vector_h);
505 vectorscope->clear_box(0, 0, vector_w, vector_h);
506 vect_slider->reposition_window(vx, vy);
509 else if( !use_vector && vectorscope ) {
510 delete vectorscope; vectorscope = 0;
511 delete vect_slider; vect_slider = 0;
514 if( (use_hist || use_hist_parade) ) {
516 // printf("ScopeGUI::create_panels %d %d %d %d %d\n", __LINE__,
517 // hist_x, hist_y, hist_w, hist_h);
518 add_subwindow(histogram = new ScopeHistogram(this,
519 hist_x, hist_y, hist_w, hist_h));
520 histogram->create_objects();
523 histogram->reposition_window(
524 hist_x, hist_y, hist_w, hist_h);
525 histogram->clear_box(0, 0, hist_w, hist_h);
528 else if( !(use_hist || use_hist_parade) ) {
529 delete histogram; histogram = 0;
534 draw_overlays(1, 1, 0);
537 void ScopeGUI::clear_points(int flash)
540 histogram->clear_point();
542 waveform->clear_point();
544 vectorscope->clear_point();
545 if( histogram && flash )
547 if( waveform && flash )
549 if( vectorscope && flash )
550 vectorscope->flash(0);
553 void ScopeGUI::toggle_event()
557 void ScopeGUI::calculate_sizes(int w, int h)
559 int margin = theme->widget_border;
560 int menu_h = smooth->get_h() + scope_menu->get_h() + margin * 3;
561 int text_w = get_text_width(SMALLFONT, "000") + margin * 2;
562 int total_panels = ((use_hist || use_hist_parade) ? 1 : 0) +
563 ((use_wave || use_wave_parade) ? 1 : 0) +
564 (use_vector ? 1 : 0);
567 int panel_w = (w - margin) / (total_panels > 0 ? total_panels : 1);
568 // Vectorscope determines the size of everything else
572 vector_x = w - panel_w + text_w;
573 vector_w = w - margin - vector_x;
575 vector_h = h - vector_y - margin;
577 if( vector_w > vector_h ) {
579 vector_x = w - theme->widget_border - vector_w;
583 panel_w = (vector_x - text_w - margin) / total_panels;
586 // Histogram is always 1st panel
587 if( use_hist || use_hist_parade ) {
590 hist_w = panel_w - margin;
591 hist_h = h - hist_y - margin;
597 if( use_wave || use_wave_parade ) {
600 wave_w = panel_w - margin - text_w;
601 wave_h = h - wave_y - margin;
607 void ScopeGUI::allocate_vframes()
609 if(waveform_vframe) delete waveform_vframe;
610 if(vector_vframe) delete vector_vframe;
613 //printf("ScopeGUI::allocate_vframes %d %d %d %d %d\n", __LINE__,
614 // wave_w, wave_h, vector_w, vector_h);
615 int xs16 = xS(16), ys16 = yS(16);
616 w = MAX(wave_w, xs16);
617 h = MAX(wave_h, ys16);
618 waveform_vframe = new VFrame(w, h, BC_RGB888);
619 w = MAX(vector_w, xs16);
620 h = MAX(vector_h, ys16);
621 vector_vframe = new VFrame(w, h, BC_RGB888);
625 int ScopeGUI::resize_event(int w, int h)
627 clear_box(0, 0, w, h);
630 calculate_sizes(w, h);
631 int margin = theme->widget_border;
634 waveform->reposition_window(wave_x, wave_y, wave_w, wave_h);
635 waveform->clear_box(0, 0, wave_w, wave_h);
636 int px = wave_x + wave_w - wave_slider->get_w() - margin;
637 int py = wave_y - wave_slider->get_h() - margin;
638 wave_slider->reposition_window(px, py);
642 histogram->reposition_window(hist_x, hist_y, hist_w, hist_h);
643 histogram->clear_box(0, 0, hist_w, hist_h);
647 vectorscope->reposition_window(vector_x, vector_y, vector_w, vector_h);
648 vectorscope->clear_box(0, 0, vector_w, vector_h);
649 int vx = vector_x + vector_w - vect_slider->get_w() - margin;
650 int vy = vector_y - vect_slider->get_h() - margin;
651 vect_slider->reposition_window(vx, vy);
657 draw_overlays(1, 1, 1);
661 int ScopeGUI::translation_event()
665 PluginClientWindow::translation_event();
670 void ScopeGUI::draw_overlays(int overlays, int borders, int flush)
672 BC_Resources *resources = BC_WindowBase::get_resources();
673 int text_color = GREEN;
674 int dark_color = (text_color>>2) & 0x3f3f3f;
675 if( resources->bg_color == 0xffffff ) {
676 text_color = dark_color;
679 if( overlays && borders ) {
680 clear_box(0, 0, get_w(), get_h());
685 set_color(text_color);
688 if( histogram && (use_hist || use_hist_parade) ) {
689 histogram->draw_line(hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), 0,
690 hist_w * -FLOAT_MIN / (FLOAT_MAX - FLOAT_MIN), hist_h);
691 histogram->draw_line(hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), 0,
692 hist_w * (1.0 - FLOAT_MIN) / (FLOAT_MAX - FLOAT_MIN), hist_h);
694 histogram->draw_point();
700 if( waveform && (use_wave || use_wave_parade) ) {
701 for( int i=0; i<=WAVEFORM_DIVISIONS; ++i ) {
702 int y = wave_h * i / WAVEFORM_DIVISIONS;
703 int text_y = y + wave_y + get_text_ascent(SMALLFONT) / 2;
704 CLAMP(text_y, waveform->get_y() + get_text_ascent(SMALLFONT), waveform->get_y() + waveform->get_h() - 1);
705 char string[BCTEXTLEN];
706 sprintf(string, "%d", (int)lround((FLOAT_MAX -
707 i * (FLOAT_MAX - FLOAT_MIN) / WAVEFORM_DIVISIONS) * 100));
708 int text_x = wave_x - get_text_width(SMALLFONT, string) - theme->widget_border;
709 set_color(text_color);
710 draw_text(text_x, text_y, string);
711 CLAMP(y, 0, waveform->get_h() - 1);
712 set_color(dark_color);
713 waveform->draw_line(0, y, wave_w, y);
714 waveform->draw_rectangle(0, 0, wave_w, wave_h);
717 waveform->draw_point();
721 // Vectorscope overlay
722 if( vectorscope && use_vector ) {
724 int radius = MIN(vector_w / 2, vector_h / 2);
725 for( int i=1; i<=VECTORSCOPE_DIVISIONS; i+=2 ) {
726 int y = vector_h / 2 - radius * i / VECTORSCOPE_DIVISIONS;
727 int text_y = y + vector_y + get_text_ascent(SMALLFONT) / 2;
728 set_color(text_color);
729 char string[BCTEXTLEN];
730 sprintf(string, "%d",
731 (int)((FLOAT_MAX / VECTORSCOPE_DIVISIONS * i) * 100));
732 int text_x = vector_x - get_text_width(SMALLFONT, string) - theme->widget_border;
733 draw_text(text_x, text_y, string);
734 int x = vector_w / 2 - radius * i / VECTORSCOPE_DIVISIONS;
735 int w = radius * i / VECTORSCOPE_DIVISIONS * 2;
736 int h = radius * i / VECTORSCOPE_DIVISIONS * 2;
737 if( i+2 > VECTORSCOPE_DIVISIONS )
739 set_color(dark_color);
740 vectorscope->draw_circle(x, y, w, h);
742 set_color(text_color);
743 vectorscope->draw_point();
745 vectorscope->flash(0);
748 set_font(MEDIUMFONT);
753 if( use_hist || use_hist_parade ) {
754 draw_3d_border(hist_x - 2, hist_y - 2, hist_w + 4, hist_h + 4,
755 get_bg_color(), BLACK, MDGREY, get_bg_color());
757 if( use_wave || use_wave_parade ) {
758 draw_3d_border(wave_x - 2, wave_y - 2, wave_w + 4, wave_h + 4,
759 get_bg_color(), BLACK, MDGREY, get_bg_color());
762 draw_3d_border(vector_x - 2, vector_y - 2, vector_w + 4, vector_h + 4,
763 get_bg_color(), BLACK, MDGREY, get_bg_color());
768 if(flush) this->flush();
773 void ScopeGUI::process(VFrame *output_frame)
775 lock_window("ScopeGUI::process");
776 this->output_frame = output_frame;
777 int ow = output_frame->get_w(), oh = output_frame->get_h();
779 VFrame::get_temp(temp_frame, ow, oh, BC_RGB161616);
780 temp_frame->transfer_from(output_frame);
781 if( !box_blur ) box_blur = new BoxBlur(cpus);
782 box_blur->blur(temp_frame, temp_frame, 2, 2);
783 data_frame = temp_frame;
786 data_frame = output_frame;
787 frame_w = data_frame->get_w();
788 //float radius = MIN(vector_w / 2, vector_h / 2);
789 bzero(waveform_vframe->get_data(), waveform_vframe->get_data_size());
790 bzero(vector_vframe->get_data(), vector_vframe->get_data_size());
794 histogram->draw(0, 0);
796 waveform->draw_vframe(waveform_vframe);
798 vectorscope->draw_vframe(vector_vframe);
800 draw_overlays(1, 0, 1);
804 void ScopeGUI::update_toggles()
806 scope_menu->update_toggles();
809 ScopePanel::ScopePanel(ScopeGUI *gui, int x, int y, int w, int h)
810 : BC_SubWindow(x, y, w, h, BLACK)
816 void ScopePanel::create_objects()
818 set_cursor(CROSS_CURSOR, 0, 0);
819 clear_box(0, 0, get_w(), get_h());
822 void ScopePanel::update_point(int x, int y)
826 void ScopePanel::draw_point()
830 void ScopePanel::clear_point()
834 int ScopePanel::button_press_event()
836 if( is_event_win() && cursor_inside() ) {
837 gui->clear_points(1);
839 int x = get_cursor_x();
840 int y = get_cursor_y();
841 CLAMP(x, 0, get_w() - 1);
842 CLAMP(y, 0, get_h() - 1);
850 int ScopePanel::cursor_motion_event()
853 int x = get_cursor_x();
854 int y = get_cursor_y();
855 CLAMP(x, 0, get_w() - 1);
856 CLAMP(y, 0, get_h() - 1);
864 int ScopePanel::button_release_event()
874 ScopeWaveform::ScopeWaveform(ScopeGUI *gui,
875 int x, int y, int w, int h)
876 : ScopePanel(gui, x, y, w, h)
882 void ScopeWaveform::update_point(int x, int y)
887 int frame_x = x * gui->frame_w / get_w();
889 if( gui->use_wave_parade ) {
890 if( x > get_w() / 3 * 2 )
891 frame_x = (x - get_w() / 3 * 2) * gui->frame_w / (get_w() / 3);
892 else if( x > get_w() / 3 )
893 frame_x = (x - get_w() / 3) * gui->frame_w / (get_w() / 3);
895 frame_x = x * gui->frame_w / (get_w() / 3);
898 float value = ((float)get_h() - y) / get_h() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
899 char string[BCTEXTLEN];
900 sprintf(string, "X: %d Value: %.3f", frame_x, value);
901 gui->value_text->update(string, 0);
907 void ScopeWaveform::draw_point()
913 draw_line(0, drag_y, get_w(), drag_y);
914 draw_line(drag_x, 0, drag_x, get_h());
920 void ScopeWaveform::clear_point()
927 ScopeVectorscope::ScopeVectorscope(ScopeGUI *gui,
928 int x, int y, int w, int h)
929 : ScopePanel(gui, x, y, w, h)
935 void ScopeVectorscope::clear_point()
943 void ScopeVectorscope::update_point(int x, int y)
948 int radius = MIN(get_w() / 2, get_h() / 2);
949 drag_radius = sqrt(SQR(x - get_w() / 2) + SQR(y - get_h() / 2));
950 drag_angle = atan2(y - get_h() / 2, x - get_w() / 2);
952 drag_radius = MIN(drag_radius, radius);
954 float saturation = (float)drag_radius / radius * FLOAT_MAX;
955 float hue = -drag_angle * 360 / 2 / M_PI - 90;
956 if( hue < 0 ) hue += 360;
958 char string[BCTEXTLEN];
959 sprintf(string, "Hue: %.3f Sat: %.3f", hue, saturation);
960 gui->value_text->update(string, 0);
967 void ScopeVectorscope::draw_point()
969 if( drag_radius > 0 ) {
970 int radius = MIN(get_w() / 2, get_h() / 2);
974 draw_circle(get_w() / 2 - drag_radius, get_h() / 2 - drag_radius,
975 drag_radius * 2, drag_radius * 2);
976 draw_line(get_w() / 2, get_h() / 2,
977 get_w() / 2 + radius * cos(drag_angle),
978 get_h() / 2 + radius * sin(drag_angle));
986 ScopeHistogram::ScopeHistogram(ScopeGUI *gui,
987 int x, int y, int w, int h)
988 : ScopePanel(gui, x, y, w, h)
993 void ScopeHistogram::clear_point()
1000 void ScopeHistogram::draw_point()
1004 set_color(0xffffff);
1006 draw_line(drag_x, 0, drag_x, get_h());
1012 void ScopeHistogram::update_point(int x, int y)
1016 float value = (float)x / get_w() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1018 char string[BCTEXTLEN];
1019 sprintf(string, "Value: %.3f", value);
1020 gui->value_text->update(string, 0);
1028 void ScopeHistogram::draw_mode(int mode, int color, int y, int h)
1030 // Highest of all bins
1031 int normalize = 1, w = get_w();
1032 int *bin = gui->bins[mode], v;
1033 for( int i=0; i<TOTAL_BINS; ++i )
1034 if( (v=bin[i]) > normalize ) normalize = v;
1035 double norm = normalize>1 ? log(normalize) : 1e-4;
1036 double vnorm = h / norm;
1038 for( int x=0; x<w; ++x ) {
1039 int accum_start = (int)(x * TOTAL_BINS / w);
1040 int accum_end = (int)((x + 1) * TOTAL_BINS / w);
1041 CLAMP(accum_start, 0, TOTAL_BINS);
1042 CLAMP(accum_end, 0, TOTAL_BINS);
1044 for(int i=accum_start; i<accum_end; ++i )
1045 if( (v=bin[i]) > max ) max = v;
1046 // max = max * h / normalize;
1047 max = log(max) * vnorm;
1048 draw_line(x,y+h - max, x,y+h);
1051 void ScopeHistogram::draw(int flash, int flush)
1053 clear_box(0, 0, get_w(), get_h());
1054 if( gui->use_hist_parade ) {
1055 draw_mode(0, 0xff0000, 0, get_h() / 3);
1056 draw_mode(1, 0x00ff00, get_h() / 3, get_h() / 3);
1057 draw_mode(2, 0x0000ff, get_h() / 3 * 2, get_h() / 3);
1060 draw_mode(3, LTGREY, 0, get_h());
1063 if(flash) this->flash(0);
1064 if(flush) this->flush();
1068 ScopeScopesOn::ScopeScopesOn(ScopeMenu *scope_menu, const char *text, int id)
1071 this->scope_menu = scope_menu;
1075 int ScopeScopesOn::handle_event()
1077 int v = get_checked() ? 0 : 1;
1079 ScopeGUI *gui = scope_menu->gui;
1081 case SCOPE_HISTOGRAM:
1083 if( v ) gui->use_hist_parade = 0;
1085 case SCOPE_HISTOGRAM_RGB:
1086 gui->use_hist_parade = v;
1087 if( v ) gui->use_hist = 0;
1089 case SCOPE_WAVEFORM:
1091 if( v ) gui->use_wave_parade = 0;
1093 case SCOPE_WAVEFORM_RGB:
1094 gui->use_wave_parade = v;
1095 if( v ) gui->use_wave = 0;
1097 case SCOPE_WAVEFORM_PLY:
1098 gui->use_wave_parade = -v;
1099 if( v ) gui->use_wave = 0;
1101 case SCOPE_VECTORSCOPE:
1102 gui->use_vector = v;
1105 gui->toggle_event();
1106 gui->update_toggles();
1107 gui->create_panels();
1108 gui->update_scope();
1113 ScopeMenu::ScopeMenu(ScopeGUI *gui, int x, int y)
1114 : BC_PopupMenu(x, y, xS(100), _("Scopes"))
1119 void ScopeMenu::create_objects()
1122 new ScopeScopesOn(this, _("Histogram"), SCOPE_HISTOGRAM));
1123 add_item(hist_rgb_on =
1124 new ScopeScopesOn(this, _("Histogram RGB"), SCOPE_HISTOGRAM_RGB));
1125 add_item(new BC_MenuItem("-"));
1127 new ScopeScopesOn(this, _("Waveform"), SCOPE_WAVEFORM));
1128 add_item(wave_rgb_on =
1129 new ScopeScopesOn(this, _("Waveform RGB"), SCOPE_WAVEFORM_RGB));
1130 add_item(wave_ply_on =
1131 new ScopeScopesOn(this, _("Waveform ply"), SCOPE_WAVEFORM_PLY));
1132 add_item(new BC_MenuItem("-"));
1134 new ScopeScopesOn(this, _("Vectorscope"), SCOPE_VECTORSCOPE));
1137 void ScopeMenu::update_toggles()
1139 hist_on->set_checked(gui->use_hist);
1140 hist_rgb_on->set_checked(gui->use_hist_parade);
1141 wave_on->set_checked(gui->use_wave);
1142 wave_rgb_on->set_checked(gui->use_wave_parade>0);
1143 wave_ply_on->set_checked(gui->use_wave_parade<0);
1144 vect_on->set_checked(gui->use_vector);
1147 ScopeWaveSlider::ScopeWaveSlider(ScopeGUI *gui, int x, int y, int w)
1148 : BC_ISlider(x, y, 0, w, w, 1, 9, gui->use_wave_gain)
1152 int ScopeWaveSlider::handle_event()
1154 gui->use_wave_gain = get_value();
1155 gui->update_scope();
1156 gui->toggle_event();
1160 ScopeVectSlider::ScopeVectSlider(ScopeGUI *gui, int x, int y, int w)
1161 : BC_ISlider(x, y, 0, w, w, 1, 9, gui->use_vect_gain)
1165 int ScopeVectSlider::handle_event()
1167 gui->use_vect_gain = get_value();
1168 gui->update_scope();
1169 gui->toggle_event();
1173 ScopeSmooth::ScopeSmooth(ScopeGUI *gui, int x, int y)
1174 : BC_CheckBox(x, y, gui->use_smooth, _("Smooth"))
1179 int ScopeSmooth::handle_event()
1181 gui->use_smooth = get_value();
1182 gui->update_scope();
1183 gui->toggle_event();
1187 // from ffmpeg vf_boxblur
1188 template<class dst_t, class src_t> static inline
1189 void blurt(dst_t *dst, int dst_step, src_t *src, int src_step, int len, int radius)
1191 const int length = radius*2 + 1;
1192 const int inv = ((1<<16) + length/2)/length;
1193 int x, n, sum = src[radius*src_step];
1195 for( x=0; x<radius; ++x )
1196 sum += src[x*src_step]<<1;
1197 sum = sum*inv + (1<<15);
1199 for( x=0; x<=radius; ++x ) {
1200 sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv;
1201 dst[x*dst_step] = sum>>16;
1205 sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv;
1206 dst[x*dst_step] = sum >>16;
1209 for ( ; x<len; ++x ) {
1210 sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv;
1211 dst[x*dst_step] = sum>>16;
1214 template<class dst_t, class src_t> static inline
1215 void blur_power(dst_t *dst, int dst_step, src_t *src, int src_step,
1216 int len, int radius, int power)
1218 dst_t atemp[len], btemp[len];
1219 dst_t *a = atemp, *b = btemp;
1220 blurt(a, 1, src, src_step, len, radius);
1221 while( power-- > 2 ) {
1222 blurt(b, 1, a, 1, len, radius);
1223 dst_t *t = a; a = b; b = t;
1226 blurt(dst, dst_step, a, 1, len, radius);
1228 for( int i = 0; i<len; ++i ) dst[i*dst_step] = a[i];
1232 BoxBlurPackage::BoxBlurPackage()
1237 BoxBlurUnit::BoxBlurUnit(BoxBlur *box_blur)
1238 : LoadClient(box_blur)
1242 template<class dst_t, class src_t>
1243 void BoxBlurUnit::blurt_package(LoadPackage *package)
1245 BoxBlur *box_blur = (BoxBlur *)server;
1246 src_t *src_data = (src_t *)box_blur->src_data;
1247 dst_t *dst_data = (dst_t *)box_blur->dst_data;
1248 int radius = box_blur->radius;
1249 int power = box_blur->power;
1250 int vlen = box_blur->vlen;
1251 int c0 = box_blur->c0, c1 = box_blur->c1;
1252 int src_ustep = box_blur->src_ustep;
1253 int dst_ustep = box_blur->dst_ustep;
1254 int src_vstep = box_blur->src_vstep;
1255 int dst_vstep = box_blur->dst_vstep;
1256 BoxBlurPackage *pkg = (BoxBlurPackage*)package;
1257 int u1 = pkg->u1, u2 = pkg->u2;
1258 for( int u=u1; u<u2; ++u ) {
1259 src_t *sp = src_data + u*src_ustep;
1260 dst_t *dp = dst_data + u*dst_ustep;
1261 for( int c=c0; c<=c1; ++c ) {
1262 blur_power(dp+c, dst_vstep, sp+c, src_vstep,
1263 vlen, radius, power);
1268 void BoxBlurUnit::process_package(LoadPackage *package)
1270 blurt_package<uint16_t, const uint16_t>(package);
1273 BoxBlur::BoxBlur(int cpus)
1274 : LoadServer(cpus, cpus)
1281 LoadClient* BoxBlur::new_client() { return new BoxBlurUnit(this); }
1282 LoadPackage* BoxBlur::new_package() { return new BoxBlurPackage(); }
1284 void BoxBlur::init_packages()
1287 for( int i=0,n=LoadServer::get_total_packages(); i<n; ) {
1288 BoxBlurPackage *pkg = (BoxBlurPackage*)get_package(i);
1290 pkg->u2 = u = (++i * ulen) / n;
1294 //dst can equal src, requires geom(dst)==geom(src)
1295 //uv: 0=hblur, 1=vblur; comp: -1=rgb,0=r,1=g,2=b
1296 void BoxBlur::process(VFrame *dst, VFrame *src, int uv,
1297 int radius, int power, int comp)
1299 this->radius = radius;
1300 this->power = power;
1301 int src_w = src->get_w(), src_h = src->get_h();
1302 ulen = !uv ? src_h : src_w;
1303 vlen = !uv ? src_w : src_h;
1304 c0 = comp<0 ? 0 : comp;
1305 c1 = comp<0 ? 2 : comp;
1306 src_data = src->get_data();
1307 dst_data = dst->get_data();
1308 int src_pixsz = BC_CModels::calculate_pixelsize(src->get_color_model());
1309 int src_comps = BC_CModels::components(src->get_color_model());
1310 int src_bpp = src_pixsz / src_comps;
1311 int dst_pixsz = BC_CModels::calculate_pixelsize(dst->get_color_model());
1312 int dst_comps = BC_CModels::components(dst->get_color_model());
1313 int dst_bpp = dst_pixsz / dst_comps;
1314 int dst_linsz = dst->get_bytes_per_line() / dst_bpp;
1315 int src_linsz = src->get_bytes_per_line() / src_bpp;
1316 src_ustep = !uv ? src_linsz : src_comps;
1317 dst_ustep = !uv ? dst_linsz: dst_comps;
1318 src_vstep = !uv ? src_comps : src_linsz;
1319 dst_vstep = !uv ? dst_comps : dst_linsz;
1324 void BoxBlur::hblur(VFrame *dst, VFrame *src, int radius, int power, int comp)
1326 process(dst, src, 0, radius, power, comp);
1328 void BoxBlur::vblur(VFrame *dst, VFrame *src, int radius, int power, int comp)
1330 process(dst, src, 1, radius, power, comp);
1332 void BoxBlur::blur(VFrame *dst, VFrame *src, int radius, int power, int comp)
1334 process(dst, src, 0, radius, power, comp);
1335 process(dst, dst, 1, radius, power, comp);