4 * Copyright (C) 2008-2012 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "loadbalance.h"
38 REGISTER_PLUGIN(HistEqMain)
40 HistEqConfig::HistEqConfig()
48 HistEqConfig::~HistEqConfig()
52 void HistEqConfig::copy_from(HistEqConfig &that)
60 int HistEqConfig::equivalent(HistEqConfig &that)
65 void HistEqConfig::interpolate(HistEqConfig &prev, HistEqConfig &next,
66 int64_t prev_frame, int64_t next_frame, int64_t current_frame)
72 HistEqWindow::HistEqWindow(HistEqMain *plugin)
73 : PluginClientWindow(plugin, plugin->w, plugin->h, plugin->w, plugin->h, 0)
75 this->plugin = plugin;
78 HistEqWindow::~HistEqWindow()
83 void HistEqWindow::create_objects()
86 int cw = get_w()-2*x, ch = cw*3/4;
87 add_subwindow(canvas = new HistEqCanvas(this, plugin, x, y, cw, ch));
88 y += canvas->get_h() + 10;
90 add_subwindow(split = new HistEqSplit(this, plugin, x, y));
91 y += split->get_h() + 10;
93 add_subwindow(plot = new HistEqPlot(this, plugin, x, y));
94 y += plot->get_h() + 10;
97 add_subwindow(new BC_Title(x, y, _("Blend:")));
98 add_subwindow(blend = new HistEqBlend(this, plugin, x1, y));
99 y += blend->get_h() + 10;
101 add_subwindow(new BC_Title(x, y, _("Gain:")));
102 add_subwindow(gain = new HistEqGain(this, plugin, x1, y));
103 y += gain->get_h() + 10;
108 void HistEqWindow::update()
112 HistEqCanvas::HistEqCanvas(HistEqWindow *gui, HistEqMain *plugin,
113 int x, int y, int w, int h)
114 : BC_SubWindow(x, y, w, h, BLACK)
117 this->plugin = plugin;
119 void HistEqCanvas::clear()
121 clear_box(0,0, get_w(),get_h());
124 void HistEqCanvas::draw_bins(HistEqMain *plugin)
127 int *data = plugin->bins;
128 int n = plugin->binsz, max = 0;
129 for( int i=0; i<n; ++i )
130 if( max < data[i] ) max = data[i];
131 double lmax = log(max);
132 int cw = get_w(), ch = get_h();
136 int x1 = (n * ++x) / cw;
137 for( int i=x0; ++i<x1; ) {
141 double lmx = mx>0 ? log(mx) : 0;
142 int y1 = ch * (1 - lmx/lmax);
143 draw_line(x,0, x,y1);
148 void HistEqCanvas::draw_wts(HistEqMain *plugin)
150 float *wts = plugin->wts;
151 int n = plugin->binsz;
153 int cw1 = get_w()-1, ch1 = get_h()-1;
154 float g0 = plugin->config.gain, g1 = 1-g0;
155 int x1 = 0, y1 = ch1;
157 int x0 = x1++, y0 = y1;
158 int is = (n * x1) / cw1;
159 float fy = wts[is]*g0 + ((float)x1/cw1)*g1;
161 draw_line(x0,y0, x1,y1);
165 void HistEqCanvas::draw_reg(HistEqMain *plugin)
168 int cw1 = get_w()-1, ch1 = get_h()-1;
169 float g0 = plugin->config.gain, g1 = 1-g0;
170 int x0 = 0, x1 = plugin->binsz-1;
171 double a = plugin->a, b = plugin->b;
172 float fy0 = (a*x0 + b)*g0 + 0*g1;
173 float fy1 = (a*x1 + b)*g0 + 1*g1;
174 int y0 = (1 - fy0) * ch1;
175 int y1 = (1 - fy1) * ch1;
176 draw_line(0,y0, cw1,y1);
179 void HistEqCanvas::draw_lut(HistEqMain *plugin)
181 int *lut = plugin->lut;
182 int n = plugin->lutsz-1;
185 int cw1 = get_w()-1, ch1 = get_h()-1;
186 int x1 = 0, y1 = ch1;
188 int x0 = x1++, y0 = y1;
189 int is = (n * x1) / cw1;
190 y1 = (1-s*lut[is]) * ch1;
191 draw_line(x0,y0, x1,y1);
195 void HistEqCanvas::update(HistEqMain *plugin)
205 HistEqSplit::HistEqSplit(HistEqWindow *gui, HistEqMain *plugin, int x, int y)
206 : BC_CheckBox(x, y, plugin->config.split, _("Split output"))
209 this->plugin = plugin;
211 HistEqSplit::~HistEqSplit()
215 int HistEqSplit::handle_event()
217 plugin->config.split = get_value();
218 plugin->send_configure_change();
222 HistEqPlot::HistEqPlot(HistEqWindow *gui, HistEqMain *plugin, int x, int y)
223 : BC_CheckBox(x, y, plugin->config.plot, _("Plot bins/lut"))
226 this->plugin = plugin;
228 HistEqPlot::~HistEqPlot()
232 int HistEqPlot::handle_event()
234 plugin->config.plot = get_value();
235 plugin->send_configure_change();
239 HistEqBlend::HistEqBlend(HistEqWindow *gui, HistEqMain *plugin, int x, int y)
240 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, plugin->config.blend, 0)
243 this->plugin = plugin;
246 HistEqBlend::~HistEqBlend()
250 int HistEqBlend::handle_event()
252 plugin->config.blend = get_value();
253 plugin->send_configure_change();
258 HistEqGain::HistEqGain(HistEqWindow *gui, HistEqMain *plugin, int x, int y)
259 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, plugin->config.gain, 0)
262 this->plugin = plugin;
265 HistEqGain::~HistEqGain()
269 int HistEqGain::handle_event()
271 plugin->config.gain = get_value();
272 plugin->send_configure_change();
277 HistEqMain::HistEqMain(PluginServer *server)
278 : PluginVClient(server)
283 binsz = bsz = 0; bins = 0;
284 lutsz = lsz = 0; lut = 0;
289 HistEqMain::~HistEqMain()
297 const char* HistEqMain::plugin_title() { return N_("HistEq"); }
298 int HistEqMain::is_realtime() { return 1; }
301 NEW_WINDOW_MACRO(HistEqMain, HistEqWindow)
303 LOAD_CONFIGURATION_MACRO(HistEqMain, HistEqConfig)
305 void HistEqMain::update_gui()
307 if( !thread ) return;
308 if( !load_configuration() ) return;
309 ((HistEqWindow*)thread->window)->lock_window("HistEqMain::update_gui");
310 HistEqWindow* gui = (HistEqWindow*)thread->window;
312 gui->unlock_window();
315 void HistEqMain::save_data(KeyFrame *keyframe)
318 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
319 output.tag.set_title("HISTEQ");
320 output.tag.set_property("W", w);
321 output.tag.set_property("H", h);
322 output.tag.set_property("SPLIT", config.split);
323 output.tag.set_property("PLOT", config.plot);
324 output.tag.set_property("BLEND", config.blend);
325 output.tag.set_property("GAIN", config.gain);
327 output.tag.set_title("/HISTEQ");
329 output.append_newline();
330 output.terminate_string();
333 void HistEqMain::read_data(KeyFrame *keyframe)
336 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
338 while( !(result=input.read_tag()) ) {
339 if( input.tag.title_is("HISTEQ") ) {
341 w = input.tag.get_property("W", w);
342 h = input.tag.get_property("H", h);
344 config.split = input.tag.get_property("SPLIT", config.split);
345 config.plot = input.tag.get_property("PLOT", config.plot);
346 config.blend = input.tag.get_property("BLEND", config.blend);
347 config.gain = input.tag.get_property("GAIN", config.gain);
352 void HistEqMain::render_gui(void *data)
354 if( !thread ) return;
355 ((HistEqWindow*)thread->window)->lock_window("HistEqMain::render_gui 1");
356 HistEqWindow *gui = (HistEqWindow*)thread->window;
357 gui->canvas->update((HistEqMain *)data);
358 gui->unlock_window();
362 static void fit(float *dat, int n, double &a, double &b)
365 for( int i=0; i<n; ++i ) sy += dat[i];
366 double s = 0, mx = (n-1)/2., my = sy / n;
367 for( int i=0; i<n; ++i ) s += (i-mx) * dat[i];
369 double ss = 2. * ((m+1)*m*(m-1)/3. + m*(m-1)/2.);
374 int HistEqMain::process_buffer(VFrame *frame, int64_t start_position, double frame_rate)
376 load_configuration();
377 read_frame(frame, 0, start_position, frame_rate, 0);
378 this->input = this->output = frame;
380 int colormodel = frame->get_color_model();
382 binsz = (3*0xffff) + 1;
383 switch( colormodel ) {
386 binsz = (3*0xff) + 1;
391 binsz = (3*0x5555) + 1;
394 if( binsz != bsz ) { delete bins; bins = 0; bsz = 0; }
395 if( !bins ) bins = new int[bsz = binsz];
396 if( binsz+1 != wsz ) { delete wts; wts = 0; wsz = 0; }
397 if( !wts ) wts = new float[wsz = binsz+1];
398 if( lutsz != lsz ) { delete lut; lut = 0; lsz = 0; }
399 if( !lut ) lut = new int[lsz = lutsz];
401 if(!engine) engine = new HistEqEngine(this,
402 get_project_smp() + 1, get_project_smp() + 1);
403 engine->process_packages(HistEqEngine::HISTEQ, frame);
405 // sum the results, not all clients may have run
406 memset(bins, 0, binsz*sizeof(bins[0]));
407 for( int i=0, n=engine->get_total_clients(); i<n; ++i ) {
408 HistEqUnit *unit = (HistEqUnit*)engine->get_client(i);
409 if( !unit->valid ) continue;
410 for( int i=0; i<binsz; ++i ) bins[i] += unit->bins[i];
413 // Remove top and bottom from calculations.
414 // Doesn't work in high precision colormodels.
415 int n = frame->get_w() * frame->get_h();
416 int binsz1 = binsz-1, lutsz1 = lutsz-1;
417 n -= bins[0]; bins[0] = 0;
418 n -= bins[binsz1]; bins[binsz1] = 0;
421 // integrate and normalize
422 for( int i=0,t=0; i<binsz; ++i ) { t += bins[i]; wts[i] = (float)t / n; }
424 // exclude margins (+-2%)
425 float fmn = 0.02f, fmx = 1. - fmn;
426 int mn = 0; while( mn < binsz && wts[mn] < fmn ) ++mn;
427 int mx = binsz; while( mx > mn && wts[mx-1] > fmx ) --mx;
428 n = mx-mn+1; fit(&wts[mn], n, a, b);
431 if( (a*n + b) < 1 ) a = (1 - b) / n;
432 if( b > 0 ) { a = (a*n + b) / n; b = 0; }
433 double blend = config.blend, blend1 = 1-blend;
434 double r = (double)binsz1 / lutsz1;
435 float g0 = config.gain, g1 = 1-g0;
436 for( int i=0; i<lutsz; ++i ) {
438 int is0 = is, is1 = is0+1;
439 double s0 = is-is0, s1 = 1-s0;
440 // piecewise linear interp btwn wts[is]..wts[is+1]
441 float u = wts[is0]*s0 + wts[is1]*s1;
444 // blend bins eq with linear regression, add scalar gain
445 float t = u*blend + v*blend1;
446 int iy = (t*lutsz1)*g0 + i*g1;
447 lut[i] = bclip(iy, 0, lutsz1);
449 if( config.plot && gui_open() )
450 send_render_gui(this);
452 engine->process_packages(HistEqEngine::APPLY, frame);
456 HistEqPackage::HistEqPackage()
461 HistEqUnit::HistEqUnit(HistEqEngine *server, HistEqMain *plugin)
464 this->plugin = plugin;
465 this->server = server;
471 HistEqUnit::~HistEqUnit()
476 void HistEqUnit::process_package(LoadPackage *package)
478 if( binsz != plugin->binsz ) {
479 delete bins; bins = 0; binsz = 0;
482 bins = new int[binsz = plugin->binsz];
486 bzero(bins, binsz*sizeof(bins[0]));
489 HistEqPackage *pkg = (HistEqPackage*)package;
490 switch( server->operation ) {
491 case HistEqEngine::HISTEQ: {
493 #define HISTEQ_HEAD(type) { \
494 type **rows = (type**)data->get_rows(); \
495 for( int iy=pkg->y0; iy<pkg->y1; ++iy ) { \
496 type *row = rows[iy]; \
497 for( int ix=0; ix<w; ++ix ) {
499 #define HISTEQ_TAIL(components) \
505 VFrame *data = server->data;
506 int colormodel = data->get_color_model();
507 int w = data->get_w(), comps = BC_CModels::components(colormodel);
509 switch( colormodel ) {
512 HISTEQ_HEAD(unsigned char)
513 int r = row[0], g = row[1], b = row[2];
518 case BC_RGBA16161616:
519 HISTEQ_HEAD(uint16_t)
520 int r = row[0], g = row[1], b = row[2];
527 int r = (int)(row[0] * 0x5555);
528 int g = (int)(row[1] * 0x5555);
529 int b = (int)(row[2] * 0x5555);
530 int i = r + g + b; bclamp(i, 0,0xffff);
535 HISTEQ_HEAD(unsigned char)
536 int y = (row[0]<<8) + row[0];
537 int u = (row[1]<<8) + row[1];
538 int v = (row[2]<<8) + row[2];
540 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
545 case BC_YUVA16161616:
546 HISTEQ_HEAD(uint16_t)
548 YUV::yuv.yuv_to_rgb_16(r, g, b, row[0], row[1], row[2]);
554 case HistEqEngine::APPLY: {
555 #define PROCESS_RGB(type, components, max) { \
556 type **rows = (type**)input->get_rows(); \
557 for( int iy=pkg->y0; iy<pkg->y1; ++iy ) { \
558 type *row = rows[iy]; \
559 int x1 = !split ? w : (iy * w) / h; \
560 for( int x=0; x<x1; ++x ) { \
561 int r = row[0], g = row[1], b = row[2]; \
562 row[0] = lut[r]; row[1] = lut[g]; row[2] = lut[b]; \
567 #define PROCESS_YUV(type, components, max) { \
568 type **rows = (type**)input->get_rows(); \
569 for( int iy=pkg->y0; iy<pkg->y1; ++iy ) { \
570 type *row = rows[iy]; \
571 int x1 = !split ? w : (iy * w) / h; \
572 for( int ix=0; ix<x1; ++ix ) { \
573 int r, g, b, y, u, v; \
574 if( max == 0xff ) { \
575 y = (row[0] << 8) | row[0]; \
576 u = (row[1] << 8) | row[1]; \
577 v = (row[2] << 8) | row[2]; \
580 y = row[0]; u = row[1]; v = row[2]; \
582 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
583 YUV::yuv.rgb_to_yuv_16(lut[r], lut[g], lut[b], y, u, v); \
584 if( max == 0xff ) { \
590 row[0] = y; row[1] = u; row[2] = v; \
596 #define PROCESS_FLOAT(components) { \
597 float **rows = (float**)input->get_rows(); \
598 for( int iy=pkg->y0; iy<pkg->y1; ++iy ) { \
599 float *row = rows[iy]; \
600 int x1 = !split ? w : (iy * w) / h; \
601 for( int ix=0; ix<x1; ++ix ) { \
602 int r = row[0]*0xffff, g = row[1]*0xffff, b = row[2]*0xffff; \
603 bclamp(r, 0,0xffff); bclamp(g, 0,0xffff); bclamp(b, 0,0xffff); \
604 row[0] = (float)lut[r] / 0xffff; \
605 row[1] = (float)lut[g] / 0xffff; \
606 row[2] = (float)lut[b] / 0xffff; \
612 VFrame *input = plugin->input;
613 int w = input->get_w(), h = input->get_h();
614 int split = plugin->config.split;
615 int *lut = plugin->lut;
617 switch(input->get_color_model()) {
619 PROCESS_RGB(unsigned char, 3, 0xff)
622 PROCESS_RGB(unsigned char, 4, 0xff)
625 PROCESS_RGB(uint16_t, 3, 0xffff)
627 case BC_RGBA16161616:
628 PROCESS_RGB(uint16_t, 4, 0xffff)
637 PROCESS_YUV(unsigned char, 3, 0xff)
640 PROCESS_YUV(unsigned char, 4, 0xff)
643 PROCESS_YUV(uint16_t, 3, 0xffff)
645 case BC_YUVA16161616:
646 PROCESS_YUV(uint16_t, 4, 0xffff)
653 HistEqEngine::HistEqEngine(HistEqMain *plugin,
656 : LoadServer(total_clients, total_packages)
658 this->plugin = plugin;
661 void HistEqEngine::init_packages()
663 int h = data->get_h();
666 for( int i=0, n=get_total_packages(); i<n; ) {
667 HistEqPackage *pkg = (HistEqPackage *)get_package(i);
668 int y1 = ++i * h / n;
669 pkg->y0 = y0; pkg->y1 = y1;
672 for( int i=0, n=get_total_clients(); i<n; ++i ) {
673 HistEqUnit *unit = (HistEqUnit*)get_client(i);
674 unit->valid = 0; // set if unit runs
678 LoadClient* HistEqEngine::new_client()
680 return new HistEqUnit(this, plugin);
683 LoadPackage* HistEqEngine::new_package()
685 return new HistEqPackage;
688 void HistEqEngine::process_packages(int operation, VFrame *data)
691 this->operation = operation;
693 LoadServer::process_packages();