17bb811ca0eca906cd9399fb49cc382eae9e3702
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / scopewindow.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "bcsignals.h"
22 #include "bccolors.h"
23 #include "clip.h"
24 #include "cursors.h"
25 #include "language.h"
26 #include "scopewindow.h"
27 #include "theme.h"
28
29 #include <string.h>
30
31 ScopePackage::ScopePackage()
32  : LoadPackage()
33 {
34 }
35
36 ScopeUnit::ScopeUnit(ScopeGUI *gui,
37         ScopeEngine *server)
38  : LoadClient(server)
39 {
40         this->gui = gui;
41 }
42
43
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; } \
48 }
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; } \
55 }
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; } \
62 }
63
64 #define PROCESS_PIXEL(column) { \
65 /* Calculate histogram */ \
66         if(use_hist) { \
67                 int v_i = (intensity - FLOAT_MIN) * (TOTAL_BINS / (FLOAT_MAX - FLOAT_MIN)); \
68                 CLAMP(v_i, 0, TOTAL_BINS - 1); \
69                 bins[3][v_i]++; \
70         } \
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); \
78                 bins[0][r_i]++; \
79                 bins[1][g_i]++; \
80                 bins[2][b_i]++; \
81         } \
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) { \
87                                 ix /= 3; \
88                                 int iy = wave_h - ((r - FLOAT_MIN) /  \
89                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
90                                 incr_point(waveform_rows,wave_h, winc,0); \
91                                 ix += wave_w/3; \
92                                 iy = wave_h - ((g - FLOAT_MIN) /  \
93                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
94                                 incr_point(waveform_rows,wave_h, winc,1); \
95                                 ix += wave_w/3; \
96                                 iy = wave_h - ((b - FLOAT_MIN) /  \
97                                                 (FLOAT_MAX - FLOAT_MIN) * wave_h); \
98                                 incr_point(waveform_rows,wave_h, winc,2); \
99                         } \
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); \
110                         } \
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); \
118                         } \
119                 } \
120         } \
121 /* Calculate vectorscope */ \
122         if(use_vector) { \
123                 float th = 90 - h; \
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); \
138         } \
139 }
140
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); \
147         intensity = v; \
148         PROCESS_PIXEL(column) \
149 }
150
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); \
157         intensity = v; \
158         PROCESS_PIXEL(column) \
159 }
160
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); \
164         intensity = v; \
165         PROCESS_PIXEL(column) \
166 }
167
168 void ScopeUnit::process_package(LoadPackage *package)
169 {
170         ScopePackage *pkg = (ScopePackage*)package;
171
172         float r, g, b;
173         float h, s, v;
174         float intensity;
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();
197
198         switch( gui->data_frame->get_color_model() ) {
199         case BC_RGB888:
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);
205                         }
206                 }
207                 break;
208         case BC_RGBA8888:
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);
214                         }
215                 }
216                 break;
217         case BC_RGB161616:
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);
223                         }
224                 }
225                 break;
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);
232                         }
233                 }
234                 break;
235         case BC_BGR888:
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);
241                         }
242                 }
243                 break;
244         case BC_BGR8888:
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);
250                         }
251                 }
252                 break;
253         case BC_RGB_FLOAT:
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);
259                         }
260                 }
261                 break;
262         case BC_RGBA_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);
268                         }
269                 }
270                 break;
271         case BC_YUV888:
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);
277                         }
278                 }
279                 break;
280
281         case BC_YUVA8888:
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);
287                         }
288                 }
289                 break;
290         case BC_YUV420P: {
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);
300                                 ++y_row;
301                                 PROCESS_YUV_PIXEL(x + 1, *y_row, *u_row, *v_row);
302                                 ++y_row;  ++u_row;  ++v_row;
303                         }
304                 }
305                 break; }
306         case BC_YUV422:
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);
313                         }
314                 }
315                 break;
316
317         default:
318                 printf("ScopeUnit::process_package %d: color_model=%d unrecognized\n",
319                         __LINE__, gui->data_frame->get_color_model());
320                 break;
321         }
322 }
323
324
325 ScopeEngine::ScopeEngine(ScopeGUI *gui, int cpus)
326  : LoadServer(cpus, cpus)
327 {
328 //printf("ScopeEngine::ScopeEngine %d cpus=%d\n", __LINE__, cpus);
329         this->gui = gui;
330 }
331
332 ScopeEngine::~ScopeEngine()
333 {
334 }
335
336 void ScopeEngine::init_packages()
337 {
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);
341                 pkg->row1 = y;
342                 pkg->row2 = y = (++i * h) / n;
343         }
344
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);
349         }
350 }
351
352
353 LoadClient* ScopeEngine::new_client()
354 {
355         return new ScopeUnit(gui, this);
356 }
357
358 LoadPackage* ScopeEngine::new_package()
359 {
360         return new ScopePackage;
361 }
362
363 void ScopeEngine::process()
364 {
365         process_packages();
366
367         for(int i = 0; i < HIST_SECTIONS; i++)
368                 bzero(gui->bins[i], sizeof(int) * TOTAL_BINS);
369
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;
375                 }
376         }
377 }
378
379
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)
384 {
385         this->x = x;
386         this->y = y;
387         this->w = w;
388         this->h = h;
389         this->temp_frame = 0;
390         this->theme = theme;
391         this->cpus = cpus;
392         reset();
393 }
394
395 ScopeGUI::ScopeGUI(PluginClient *plugin, int w, int h)
396  : PluginClientWindow(plugin, w, h, MIN_SCOPE_W, MIN_SCOPE_H, 1)
397 {
398         this->x = get_x();
399         this->y = get_y();
400         this->w = w;
401         this->h = h;
402         this->temp_frame = 0;
403         this->theme = plugin->get_theme();
404         this->cpus = plugin->PluginClient::smp + 1;
405         reset();
406 }
407
408 ScopeGUI::~ScopeGUI()
409 {
410         delete waveform_vframe;
411         delete vector_vframe;
412         delete engine;
413         delete box_blur;
414         delete temp_frame;
415 }
416
417 void ScopeGUI::reset()
418 {
419         output_frame = 0;
420         data_frame = 0;
421         frame_w = 1;
422         use_smooth = 1;
423         use_wave_gain = 5;
424         use_vect_gain = 5;
425         waveform_vframe = 0;
426         vector_vframe = 0;
427         engine = 0;
428         box_blur = 0;
429         use_hist = 0;
430         use_wave = 1;
431         use_vector = 1;
432         use_hist_parade = 0;
433         use_wave_parade = 0;
434         waveform = 0;
435         vectorscope = 0;
436         histogram = 0;
437         wave_w = wave_h = 0;
438         vector_w = vector_h = 0;
439 }
440
441
442 void ScopeGUI::create_objects()
443 {
444         if( use_hist && use_hist_parade )
445                 use_hist = 0;
446         if( use_wave && use_wave_parade )
447                 use_wave = 0;
448         if( !engine ) engine = new ScopeEngine(this, cpus);
449
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;
460
461         create_panels();
462         update_toggles();
463         show_window();
464         update_scope();
465         unlock_window();
466 }
467
468 void ScopeGUI::create_panels()
469 {
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);
475                 if( !waveform ) {
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));
480                 }
481                 else {
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);
486                 }
487         }
488         else if( !(use_wave || use_wave_parade) && waveform ) {
489                 delete waveform;     waveform = 0;
490                 delete wave_slider;  wave_slider = 0;
491         }
492
493         if( use_vector ) {
494                 int vx = vector_x + vector_w - slider_w - xS(5);
495                 int vy = vector_y - ScopeVectSlider::get_span(0) - yS(5);
496                 if( !vectorscope ) {
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));
501                 }
502                 else {
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);
507                 }
508         }
509         else if( !use_vector && vectorscope ) {
510                 delete vectorscope;  vectorscope = 0;
511                 delete vect_slider;  vect_slider = 0;
512         }
513
514         if( (use_hist || use_hist_parade) ) {
515                 if( !histogram ) {
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();
521                 }
522                 else {
523                         histogram->reposition_window(
524                                 hist_x, hist_y, hist_w, hist_h);
525                         histogram->clear_box(0, 0, hist_w, hist_h);
526                 }
527         }
528         else if( !(use_hist || use_hist_parade) ) {
529                 delete histogram;  histogram = 0;
530         }
531
532         allocate_vframes();
533         clear_points(0);
534         draw_overlays(1, 1, 0);
535 }
536
537 void ScopeGUI::clear_points(int flash)
538 {
539         if( histogram )
540                 histogram->clear_point();
541         if( waveform )
542                 waveform->clear_point();
543         if( vectorscope )
544                 vectorscope->clear_point();
545         if( histogram && flash )
546                 histogram->flash(0);
547         if( waveform && flash )
548                 waveform->flash(0);
549         if( vectorscope && flash )
550                 vectorscope->flash(0);
551 }
552
553 void ScopeGUI::toggle_event()
554 {
555 }
556
557 void ScopeGUI::calculate_sizes(int w, int h)
558 {
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);
565         int x = margin;
566
567         int panel_w = (w - margin) / (total_panels > 0 ? total_panels : 1);
568 // Vectorscope determines the size of everything else
569 // Always last panel
570         vector_w = 0;
571         if( use_vector ) {
572                 vector_x = w - panel_w + text_w;
573                 vector_w = w - margin - vector_x;
574                 vector_y = menu_h;
575                 vector_h = h - vector_y - margin;
576
577                 if( vector_w > vector_h ) {
578                         vector_w = vector_h;
579                         vector_x = w - theme->widget_border - vector_w;
580                 }
581                 --total_panels;
582                 if(total_panels > 0)
583                         panel_w = (vector_x - text_w - margin) / total_panels;
584         }
585
586 // Histogram is always 1st panel
587         if( use_hist || use_hist_parade ) {
588                 hist_x = x;
589                 hist_y = menu_h;
590                 hist_w = panel_w - margin;
591                 hist_h = h - hist_y - margin;
592
593                 --total_panels;
594                 x += panel_w;
595         }
596
597         if( use_wave || use_wave_parade ) {
598                 wave_x = x + text_w;
599                 wave_y = menu_h;
600                 wave_w = panel_w - margin - text_w;
601                 wave_h = h - wave_y - margin;
602         }
603
604 }
605
606
607 void ScopeGUI::allocate_vframes()
608 {
609         if(waveform_vframe) delete waveform_vframe;
610         if(vector_vframe) delete vector_vframe;
611
612         int w, h;
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);
622 }
623
624
625 int ScopeGUI::resize_event(int w, int h)
626 {
627         clear_box(0, 0, w, h);
628         this->w = w;
629         this->h = h;
630         calculate_sizes(w, h);
631         int margin = theme->widget_border;
632
633         if( waveform ) {
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);
639         }
640
641         if( histogram ) {
642                 histogram->reposition_window(hist_x, hist_y, hist_w, hist_h);
643                 histogram->clear_box(0, 0, hist_w, hist_h);
644         }
645
646         if( vectorscope ) {
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);
652         }
653
654         allocate_vframes();
655         clear_points(0);
656         update_scope();
657         draw_overlays(1, 1, 1);
658         return 1;
659 }
660
661 int ScopeGUI::translation_event()
662 {
663         x = get_x();
664         y = get_y();
665         PluginClientWindow::translation_event();
666         return 0;
667 }
668
669
670 void ScopeGUI::draw_overlays(int overlays, int borders, int flush)
671 {
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;
677         }
678
679         if( overlays && borders ) {
680                 clear_box(0, 0, get_w(), get_h());
681         }
682
683         if( overlays ) {
684                 set_line_dashes(1);
685                 set_color(text_color);
686                 set_font(SMALLFONT);
687
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);
693                         set_line_dashes(0);
694                         histogram->draw_point();
695                         set_line_dashes(1);
696                         histogram->flash(0);
697                 }
698
699 // Waveform overlay
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);
715                         }
716                         set_line_dashes(0);
717                         waveform->draw_point();
718                         set_line_dashes(1);
719                         waveform->flash(0);
720                 }
721 // Vectorscope overlay
722                 if( vectorscope && use_vector ) {
723                         set_line_dashes(1);
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 )
738                                         set_line_dashes(0);
739                                 set_color(dark_color);
740                                 vectorscope->draw_circle(x, y, w, h);
741                         }
742                         set_color(text_color);
743                         vectorscope->draw_point();
744                         set_line_dashes(1);
745                         vectorscope->flash(0);
746                 }
747
748                 set_font(MEDIUMFONT);
749                 set_line_dashes(0);
750         }
751
752         if( borders ) {
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());
756                 }
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());
760                 }
761                 if( use_vector ) {
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());
764                 }
765         }
766
767         flash(0);
768         if(flush) this->flush();
769 }
770
771
772
773 void ScopeGUI::process(VFrame *output_frame)
774 {
775         lock_window("ScopeGUI::process");
776         this->output_frame = output_frame;
777         int ow = output_frame->get_w(), oh = output_frame->get_h();
778         if( use_smooth ) {
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;
784         }
785         else
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());
791         engine->process();
792
793         if( histogram )
794                 histogram->draw(0, 0);
795         if( waveform )
796                 waveform->draw_vframe(waveform_vframe);
797         if( vectorscope )
798                 vectorscope->draw_vframe(vector_vframe);
799
800         draw_overlays(1, 0, 1);
801         unlock_window();
802 }
803
804 void ScopeGUI::update_toggles()
805 {
806         scope_menu->update_toggles();
807 }
808
809 ScopePanel::ScopePanel(ScopeGUI *gui, int x, int y, int w, int h)
810  : BC_SubWindow(x, y, w, h, BLACK)
811 {
812         this->gui = gui;
813         is_dragging = 0;
814 }
815
816 void ScopePanel::create_objects()
817 {
818         set_cursor(CROSS_CURSOR, 0, 0);
819         clear_box(0, 0, get_w(), get_h());
820 }
821
822 void ScopePanel::update_point(int x, int y)
823 {
824 }
825
826 void ScopePanel::draw_point()
827 {
828 }
829
830 void ScopePanel::clear_point()
831 {
832 }
833
834 int ScopePanel::button_press_event()
835 {
836         if( is_event_win() && cursor_inside() ) {
837                 gui->clear_points(1);
838                 is_dragging = 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);
843                 update_point(x, y);
844                 return 1;
845         }
846         return 0;
847 }
848
849
850 int ScopePanel::cursor_motion_event()
851 {
852         if( is_dragging ) {
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);
857                 update_point(x, y);
858                 return 1;
859         }
860         return 0;
861 }
862
863
864 int ScopePanel::button_release_event()
865 {
866         if( is_dragging ) {
867                 is_dragging = 0;
868                 return 1;
869         }
870         return 0;
871 }
872
873
874 ScopeWaveform::ScopeWaveform(ScopeGUI *gui,
875                 int x, int y, int w, int h)
876  : ScopePanel(gui, x, y, w, h)
877 {
878         drag_x = -1;
879         drag_y = -1;
880 }
881
882 void ScopeWaveform::update_point(int x, int y)
883 {
884         draw_point();
885         drag_x = x;
886         drag_y = y;
887         int frame_x = x * gui->frame_w / get_w();
888
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);
894                 else
895                         frame_x = x * gui->frame_w / (get_w() / 3);
896         }
897
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);
902
903         draw_point();
904         flash(1);
905 }
906
907 void ScopeWaveform::draw_point()
908 {
909         if( drag_x >= 0 ) {
910                 set_inverse();
911                 set_color(0xffffff);
912                 set_line_width(2);
913                 draw_line(0, drag_y, get_w(), drag_y);
914                 draw_line(drag_x, 0, drag_x, get_h());
915                 set_line_width(1);
916                 set_opaque();
917         }
918 }
919
920 void ScopeWaveform::clear_point()
921 {
922         draw_point();
923         drag_x = -1;
924         drag_y = -1;
925 }
926
927 ScopeVectorscope::ScopeVectorscope(ScopeGUI *gui,
928                 int x, int y, int w, int h)
929  : ScopePanel(gui, x, y, w, h)
930 {
931         drag_radius = 0;
932         drag_angle = 0;
933 }
934
935 void ScopeVectorscope::clear_point()
936 {
937 // Hide it
938         draw_point();
939         drag_radius = 0;
940         drag_angle = 0;
941 }
942
943 void ScopeVectorscope::update_point(int x, int y)
944 {
945 // Hide it
946         draw_point();
947
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);
951
952         drag_radius = MIN(drag_radius, radius);
953
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;
957
958         char string[BCTEXTLEN];
959         sprintf(string, "Hue: %.3f Sat: %.3f", hue, saturation);
960         gui->value_text->update(string, 0);
961
962 // Show it
963         draw_point();
964         flash(1);
965 }
966
967 void ScopeVectorscope::draw_point()
968 {
969         if( drag_radius > 0 ) {
970                 int radius = MIN(get_w() / 2, get_h() / 2);
971                 set_inverse();
972                 set_color(0xff0000);
973                 set_line_width(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));
979                 set_line_width(1);
980                 set_opaque();
981         }
982 }
983
984
985
986 ScopeHistogram::ScopeHistogram(ScopeGUI *gui,
987                 int x, int y, int w, int h)
988  : ScopePanel(gui, x, y, w, h)
989 {
990         drag_x = -1;
991 }
992
993 void ScopeHistogram::clear_point()
994 {
995 // Hide it
996         draw_point();
997         drag_x = -1;
998 }
999
1000 void ScopeHistogram::draw_point()
1001 {
1002         if( drag_x >= 0 ) {
1003                 set_inverse();
1004                 set_color(0xffffff);
1005                 set_line_width(2);
1006                 draw_line(drag_x, 0, drag_x, get_h());
1007                 set_line_width(1);
1008                 set_opaque();
1009         }
1010 }
1011
1012 void ScopeHistogram::update_point(int x, int y)
1013 {
1014         draw_point();
1015         drag_x = x;
1016         float value = (float)x / get_w() * (FLOAT_MAX - FLOAT_MIN) + FLOAT_MIN;
1017
1018         char string[BCTEXTLEN];
1019         sprintf(string, "Value: %.3f", value);
1020         gui->value_text->update(string, 0);
1021
1022         draw_point();
1023         flash(1);
1024 }
1025
1026
1027
1028 void ScopeHistogram::draw_mode(int mode, int color, int y, int h)
1029 {
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;
1037         set_color(color);
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);
1043                 int max = 0;
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);
1049         }
1050 }
1051 void ScopeHistogram::draw(int flash, int flush)
1052 {
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);
1058         }
1059         else {
1060                 draw_mode(3, LTGREY, 0, get_h());
1061         }
1062
1063         if(flash) this->flash(0);
1064         if(flush) this->flush();
1065 }
1066
1067
1068 ScopeScopesOn::ScopeScopesOn(ScopeMenu *scope_menu, const char *text, int id)
1069  : BC_MenuItem(text)
1070 {
1071         this->scope_menu = scope_menu;
1072         this->id = id;
1073 }
1074
1075 int ScopeScopesOn::handle_event()
1076 {
1077         int v = get_checked() ? 0 : 1;
1078         set_checked(v);
1079         ScopeGUI *gui = scope_menu->gui;
1080         switch( id ) {
1081         case SCOPE_HISTOGRAM:
1082                 gui->use_hist = v;
1083                 if( v ) gui->use_hist_parade = 0;
1084                 break;
1085         case SCOPE_HISTOGRAM_RGB:
1086                 gui->use_hist_parade = v;
1087                 if( v ) gui->use_hist = 0;
1088                 break;
1089         case SCOPE_WAVEFORM:
1090                 gui->use_wave = v;
1091                 if( v ) gui->use_wave_parade = 0;
1092                 break;
1093         case SCOPE_WAVEFORM_RGB:
1094                 gui->use_wave_parade = v;
1095                 if( v ) gui->use_wave = 0;
1096                 break;
1097         case SCOPE_WAVEFORM_PLY:
1098                 gui->use_wave_parade = -v;
1099                 if( v ) gui->use_wave = 0;
1100                 break;
1101         case SCOPE_VECTORSCOPE:
1102                 gui->use_vector = v;
1103                 break;
1104         }
1105         gui->toggle_event();
1106         gui->update_toggles();
1107         gui->create_panels();
1108         gui->update_scope();
1109         gui->show_window();
1110         return 1;
1111 }
1112
1113 ScopeMenu::ScopeMenu(ScopeGUI *gui, int x, int y)
1114  : BC_PopupMenu(x, y, xS(100), _("Scopes"))
1115 {
1116         this->gui = gui;
1117 }
1118
1119 void ScopeMenu::create_objects()
1120 {
1121         add_item(hist_on =
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("-"));
1126         add_item(wave_on =
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("-"));
1133         add_item(vect_on =
1134                 new ScopeScopesOn(this, _("Vectorscope"), SCOPE_VECTORSCOPE));
1135 }
1136
1137 void ScopeMenu::update_toggles()
1138 {
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);
1145 }
1146
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)
1149 {
1150         this->gui = gui;
1151 }
1152 int ScopeWaveSlider::handle_event()
1153 {
1154         gui->use_wave_gain = get_value();
1155         gui->update_scope();
1156         gui->toggle_event();
1157         return 1;
1158 }
1159
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)
1162 {
1163         this->gui = gui;
1164 }
1165 int ScopeVectSlider::handle_event()
1166 {
1167         gui->use_vect_gain = get_value();
1168         gui->update_scope();
1169         gui->toggle_event();
1170         return 1;
1171 }
1172
1173 ScopeSmooth::ScopeSmooth(ScopeGUI *gui, int x, int y)
1174  : BC_CheckBox(x, y, gui->use_smooth, _("Smooth"))
1175 {
1176         this->gui = gui;
1177 }
1178
1179 int ScopeSmooth::handle_event()
1180 {
1181         gui->use_smooth = get_value();
1182         gui->update_scope();
1183         gui->toggle_event();
1184         return 1;
1185 }
1186
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)
1190 {
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];
1194
1195         for( x=0; x<radius; ++x )
1196                 sum += src[x*src_step]<<1;
1197         sum = sum*inv + (1<<15);
1198
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;
1202         }
1203         n = len - radius;
1204         for( ; x<n; ++x ) {
1205                 sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv;
1206                 dst[x*dst_step] = sum >>16;
1207         }
1208
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;
1212         }
1213 }
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)
1217 {
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;
1224         }
1225         if( power > 1 )
1226                 blurt(dst, dst_step, a, 1, len, radius);
1227         else
1228                 for( int i = 0; i<len; ++i ) dst[i*dst_step] = a[i];
1229 }
1230
1231
1232 BoxBlurPackage::BoxBlurPackage()
1233  : LoadPackage()
1234 {
1235 }
1236
1237 BoxBlurUnit::BoxBlurUnit(BoxBlur *box_blur)
1238  : LoadClient(box_blur)
1239 {
1240 }
1241
1242 template<class dst_t, class src_t>
1243 void BoxBlurUnit::blurt_package(LoadPackage *package)
1244 {
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);
1264                 }
1265         }
1266 }
1267
1268 void BoxBlurUnit::process_package(LoadPackage *package)
1269 {
1270         blurt_package<uint16_t, const uint16_t>(package);
1271 }
1272
1273 BoxBlur::BoxBlur(int cpus)
1274  : LoadServer(cpus, cpus)
1275 {
1276 }
1277 BoxBlur::~BoxBlur()
1278 {
1279 }
1280
1281 LoadClient* BoxBlur::new_client() { return new BoxBlurUnit(this); }
1282 LoadPackage* BoxBlur::new_package() { return new BoxBlurPackage(); }
1283
1284 void BoxBlur::init_packages()
1285 {
1286         int u = 0;
1287         for( int i=0,n=LoadServer::get_total_packages(); i<n; ) {
1288                 BoxBlurPackage *pkg = (BoxBlurPackage*)get_package(i);
1289                 pkg->u1 = u;
1290                 pkg->u2 = u = (++i * ulen) / n;
1291         }
1292 }
1293
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)
1298 {
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;
1320
1321         process_packages();
1322 }
1323
1324 void BoxBlur::hblur(VFrame *dst, VFrame *src, int radius, int power, int comp)
1325 {
1326         process(dst, src, 0, radius, power, comp);
1327 }
1328 void BoxBlur::vblur(VFrame *dst, VFrame *src, int radius, int power, int comp)
1329 {
1330         process(dst, src, 1, radius, power, comp);
1331 }
1332 void BoxBlur::blur(VFrame *dst, VFrame *src, int radius, int power, int comp)
1333 {
1334         process(dst, src, 0, radius, power, comp);
1335         process(dst, dst, 1, radius, power, comp);
1336 }
1337