2 * C41 plugin for Cinelerra
3 * Copyright (C) 2011 Florent Delannoy <florent at plui dot es>
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
24 #include "edlsession.h"
29 REGISTER_PLUGIN(C41Effect);
31 C41Config::C41Config()
38 fix_min_r = fix_min_g = fix_min_b = fix_light = 0.;
39 fix_gamma_g = fix_gamma_b = fix_coef1 = fix_coef2 = 0.;
40 min_col = max_col = min_row = max_row = 0;
41 window_w = xS(500); window_h = yS(510);
44 void C41Config::copy_from(C41Config &src)
47 compute_magic = src.compute_magic;
48 postproc = src.postproc;
49 show_box = src.show_box;
51 fix_min_r = src.fix_min_r;
52 fix_min_g = src.fix_min_g;
53 fix_min_b = src.fix_min_b;
54 fix_light = src.fix_light;
55 fix_gamma_g = src.fix_gamma_g;
56 fix_gamma_b = src.fix_gamma_b;
57 fix_coef1 = src.fix_coef1;
58 fix_coef2 = src.fix_coef2;
59 min_row = src.min_row;
60 max_row = src.max_row;
61 min_col = src.min_col;
62 max_col = src.max_col;
65 int C41Config::equivalent(C41Config &src)
67 return (active == src.active &&
68 compute_magic == src.compute_magic &&
69 postproc == src.postproc &&
70 show_box == src.show_box &&
71 EQUIV(fix_min_r, src.fix_min_r) &&
72 EQUIV(fix_min_g, src.fix_min_g) &&
73 EQUIV(fix_min_b, src.fix_min_b) &&
74 EQUIV(fix_light, src.fix_light) &&
75 EQUIV(fix_gamma_g, src.fix_gamma_g) &&
76 EQUIV(fix_gamma_b, src.fix_gamma_b) &&
77 EQUIV(fix_coef1, src.fix_coef1) &&
78 EQUIV(fix_coef2, src.fix_coef2) &&
79 min_row == src.min_row &&
80 max_row == src.max_row &&
81 min_col == src.min_col &&
82 max_col == src.max_col);
85 void C41Config::interpolate(C41Config &prev, C41Config &next,
86 long prev_frame, long next_frame, long current_frame)
89 compute_magic = prev.compute_magic;
90 postproc = prev.postproc;
91 show_box = prev.show_box;
93 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
94 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
95 fix_min_r = prev.fix_min_r * prev_scale + next.fix_min_r * next_scale;
96 fix_min_g = prev.fix_min_g * prev_scale + next.fix_min_g * next_scale;
97 fix_min_b = prev.fix_min_b * prev_scale + next.fix_min_b * next_scale;
98 fix_light = prev.fix_light * prev_scale + next.fix_light * next_scale;
99 fix_gamma_g = prev.fix_gamma_g * prev_scale + next.fix_gamma_g * next_scale;
100 fix_gamma_b = prev.fix_gamma_b * prev_scale + next.fix_gamma_b * next_scale;
101 fix_coef1 = prev.fix_coef1 * prev_scale + next.fix_coef1 * next_scale;
102 fix_coef2 = prev.fix_coef2 * prev_scale + next.fix_coef2 * next_scale;
103 min_row = round(prev.min_row * prev_scale + next.min_row * next_scale);
104 min_col = round(prev.min_col * prev_scale + next.min_col * next_scale);
105 max_row = round(prev.max_row * prev_scale + next.max_row * next_scale);
106 max_col = round(prev.max_col * prev_scale + next.max_col * next_scale);
110 C41Enable::C41Enable(C41Effect *plugin, int *output, int x, int y, const char *text)
111 : BC_CheckBox(x, y, *output, text)
113 this->plugin = plugin;
114 this->output = output;
117 int C41Enable::handle_event()
119 *output = get_value();
120 plugin->send_configure_change();
125 C41TextBox::C41TextBox(C41Effect *plugin, float *value, int x, int y)
126 : BC_TextBox(x, y, xS(160), 1, *value)
128 this->plugin = plugin;
129 this->boxValue = value;
132 int C41TextBox::handle_event()
134 *boxValue = atof(get_text());
135 plugin->send_configure_change();
141 C41Button::C41Button(C41Effect *plugin, C41Window *window, int x, int y)
142 : BC_GenericButton(x, y, _("Apply values"))
144 this->plugin = plugin;
145 this->window = window;
148 int C41Button::handle_event()
150 plugin->config.fix_min_r = plugin->values.min_r;
151 plugin->config.fix_min_g = plugin->values.min_g;
152 plugin->config.fix_min_b = plugin->values.min_b;
153 plugin->config.fix_light = plugin->values.light;
154 plugin->config.fix_gamma_g = plugin->values.gamma_g;
155 plugin->config.fix_gamma_b = plugin->values.gamma_b;
156 if( plugin->values.coef1 > 0 )
157 plugin->config.fix_coef1 = plugin->values.coef1;
158 if( plugin->values.coef2 > 0 )
159 plugin->config.fix_coef2 = plugin->values.coef2;
163 plugin->send_configure_change();
168 C41BoxButton::C41BoxButton(C41Effect *plugin, C41Window *window, int x, int y)
169 : BC_GenericButton(x, y, _("Apply default box"))
171 this->plugin = plugin;
172 this->window = window;
175 int C41BoxButton::handle_event()
177 plugin->config.min_row = plugin->values.shave_min_row;
178 plugin->config.max_row = plugin->values.shave_max_row;
179 plugin->config.min_col = plugin->values.shave_min_col;
180 plugin->config.max_col = plugin->values.shave_max_col;
184 plugin->send_configure_change();
189 C41Slider::C41Slider(C41Effect *plugin, int *output, int x, int y, int is_row)
190 : BC_ISlider(x, y, 0, xS(200), yS(200), 0, is_row ?
191 plugin->get_edl()->session->output_h :
192 plugin->get_edl()->session->output_w , *output)
194 this->plugin = plugin;
195 this->output = output;
196 this->is_row = is_row;
197 EDLSession *session = plugin->get_edl()->session;
198 this->max = is_row ? session->output_h : session->output_w;
201 int C41Slider::handle_event()
203 *output = get_value();
204 plugin->send_configure_change();
208 int C41Slider::update(int v)
210 EDLSession *session = plugin->get_edl()->session;
211 int max = is_row ? session->output_h : session->output_w;
213 if( this->max != max ) return BC_ISlider::update(xS(200), v, 0, this->max = max);
214 if( v != get_value() ) return BC_ISlider::update(v);
218 const char* C41Effect::plugin_title() { return N_("C41"); }
219 int C41Effect::is_realtime() { return 1; }
220 int C41Effect::is_synthesis() { return 1; }
222 NEW_WINDOW_MACRO(C41Effect, C41Window);
223 LOAD_CONFIGURATION_MACRO(C41Effect, C41Config)
226 C41Window::C41Window(C41Effect *plugin)
227 : PluginClientWindow(plugin,
228 plugin->config.window_w, plugin->config.window_h,
233 void C41Window::create_objects()
235 int xs10 = xS(10), xs20 = xS(20), xs40 = xS(40), xs80 = xS(80);
236 int ys10 = yS(10), ys20 = yS(20), ys25 = yS(25), ys30 = yS(30), ys40 = yS(40), ys60 = yS(60);
237 int x = xs10, y = ys10;
238 C41Effect *plugin = (C41Effect *)client;
239 add_subwindow(active = new C41Enable(plugin,
240 &plugin->config.active, x, y, _("Activate processing")));
243 add_subwindow(compute_magic = new C41Enable(plugin,
244 &plugin->config.compute_magic, x, y, _("Compute negfix values")));
247 add_subwindow(new BC_Title(x + xs20, y, _("(uncheck for faster rendering)")));
250 add_subwindow(new BC_Title(x, y, _("Computed negfix values:")));
253 add_subwindow(new BC_Title(x, y, _("Min/Max R:")));
254 add_subwindow(min_r = new BC_Title(x + xs80, y, "0.0000 / 0.0000"));
257 add_subwindow(new BC_Title(x, y, _("Min/Max G:")));
258 add_subwindow(min_g = new BC_Title(x + xs80, y, "0.0000 / 0.0000"));
261 add_subwindow(new BC_Title(x, y, _("Min/Max B:")));
262 add_subwindow(min_b = new BC_Title(x + xs80, y, "0.0000 / 0.0000"));
265 add_subwindow(new BC_Title(x, y, _("Light:")));
266 add_subwindow(light = new BC_Title(x + xs80, y, "0.0000"));
269 add_subwindow(new BC_Title(x, y, _("Gamma G:")));
270 add_subwindow(gamma_g = new BC_Title(x + xs80, y, "0.0000"));
273 add_subwindow(new BC_Title(x, y, _("Gamma B:")));
274 add_subwindow(gamma_b = new BC_Title(x + xs80, y, "0.0000"));
277 add_subwindow(new BC_Title(x, y, _("Contrast:")));
278 add_subwindow(coef1 = new BC_Title(x + xs80, y, "0.0000"));
281 add_subwindow(new BC_Title(x, y, _("Brightness:")));
282 add_subwindow(coef2 = new BC_Title(x + xs80, y, "0.0000"));
285 add_subwindow(lock = new C41Button(plugin, this, x, y));
289 add_subwindow(new BC_Title(x, y, _("Box col:")));
290 add_subwindow(box_col_min = new BC_Title(x + xs80, y, "0"));
291 add_subwindow(box_col_max = new BC_Title(x + BOX_COL, y, "0"));
294 add_subwindow(new BC_Title(x, y, _("Box row:")));
295 add_subwindow(box_row_min = new BC_Title(x + xs80, y, "0"));
296 add_subwindow(box_row_max = new BC_Title(x + BOX_COL, y, "0"));
299 add_subwindow(boxlock = new C41BoxButton(plugin, this, x, y));
303 add_subwindow(show_box = new C41Enable(plugin,
304 &plugin->config.show_box, x, y, _("Show active area")));
307 add_subwindow(postproc = new C41Enable(plugin,
308 &plugin->config.postproc, x, y, _("Postprocess")));
311 add_subwindow(new BC_Title(x, y, _("negfix values to apply:")));
314 add_subwindow(new BC_Title(x, y, _("Min R:")));
315 add_subwindow(fix_min_r = new C41TextBox(plugin,
316 &plugin->config.fix_min_r, x + xs80, y));
319 add_subwindow(new BC_Title(x, y, _("Min G:")));
320 add_subwindow(fix_min_g = new C41TextBox(plugin,
321 &plugin->config.fix_min_g, x + xs80, y));
324 add_subwindow(new BC_Title(x, y, _("Min B:")));
325 add_subwindow(fix_min_b = new C41TextBox(plugin,
326 &plugin->config.fix_min_b, x + xs80, y));
329 add_subwindow(new BC_Title(x, y, _("Light:")));
330 add_subwindow(fix_light = new C41TextBox(plugin,
331 &plugin->config.fix_light, x + xs80, y));
334 add_subwindow(new BC_Title(x, y, _("Gamma G:")));
335 add_subwindow(fix_gamma_g = new C41TextBox(plugin,
336 &plugin->config.fix_gamma_g, x + xs80, y));
339 add_subwindow(new BC_Title(x, y, _("Gamma B:")));
340 add_subwindow(fix_gamma_b = new C41TextBox(plugin,
341 &plugin->config.fix_gamma_b, x + xs80, y));
344 add_subwindow(new BC_Title(x, y, _("Contrast:")));
345 add_subwindow(fix_coef1 = new C41TextBox(plugin,
346 &plugin->config.fix_coef1, x + xs80, y));
349 add_subwindow(new BC_Title(x, y, _("Brightness:")));
350 add_subwindow(fix_coef2 = new C41TextBox(plugin,
351 &plugin->config.fix_coef2, x + xs80, y));
355 add_subwindow(new BC_Title(x - xs40, y, _("Col:")));
356 add_subwindow(min_col = new C41Slider(plugin,
357 &plugin->config.min_col, x, y, 0));
360 add_subwindow(max_col = new C41Slider(plugin,
361 &plugin->config.max_col, x, y, 0));
364 add_subwindow(new BC_Title(x - xs40, y, _("Row:")));
365 add_subwindow(min_row = new C41Slider(plugin,
366 &plugin->config.min_row, x, y, 1));
369 add_subwindow(max_row = new C41Slider(plugin,
370 &plugin->config.max_row, x, y, 1));
377 void C41Window::update()
379 C41Effect *plugin = (C41Effect *)client;
380 // Updating the GUI itself
381 active->update(plugin->config.active);
382 compute_magic->update(plugin->config.compute_magic);
383 postproc->update(plugin->config.postproc);
384 show_box->update(plugin->config.show_box);
386 fix_min_r->update(plugin->config.fix_min_r);
387 fix_min_g->update(plugin->config.fix_min_g);
388 fix_min_b->update(plugin->config.fix_min_b);
389 fix_light->update(plugin->config.fix_light);
390 fix_gamma_g->update(plugin->config.fix_gamma_g);
391 fix_gamma_b->update(plugin->config.fix_gamma_b);
392 fix_coef1->update(plugin->config.fix_coef1);
393 fix_coef2->update(plugin->config.fix_coef2);
395 min_row->update(plugin->config.min_row);
396 max_row->update(plugin->config.max_row);
397 min_col->update(plugin->config.min_col);
398 max_col->update(plugin->config.max_col);
403 void C41Window::update_magic()
405 C41Effect *plugin = (C41Effect *)client;
407 sprintf(text, "%0.4f / %0.4f", plugin->values.min_r, plugin->values.max_r);
409 sprintf(text, "%0.4f / %0.4f", plugin->values.min_g, plugin->values.max_g);
411 sprintf(text, "%0.4f / %0.4f", plugin->values.min_b, plugin->values.max_b);
413 light->update(plugin->values.light);
414 gamma_g->update(plugin->values.gamma_g);
415 gamma_b->update(plugin->values.gamma_b);
417 if( plugin->values.coef1 > 0 || plugin->values.coef2 > 0 ) {
418 coef1->update(plugin->values.coef1);
419 coef2->update(plugin->values.coef2);
421 sprintf(text, "%d", plugin->values.shave_min_col);
422 box_col_min->update(text);
423 sprintf(text, "%d", plugin->values.shave_max_col);
424 box_col_max->update(text);
425 sprintf(text, "%d", plugin->values.shave_min_row);
426 box_row_min->update(text);
427 sprintf(text, "%d", plugin->values.shave_max_row);
428 box_row_max->update(text);
433 C41Effect::C41Effect(PluginServer *server)
434 : PluginVClient(server)
439 memset(&values, 0, sizeof(values));
440 shave_min_row = shave_max_row = 0;
441 shave_min_col = shave_max_col = 0;
442 min_col = max_col = 0;
443 min_row = max_row = 0;
444 pix_max = 0; pix_len = 0;
447 C41Effect::~C41Effect()
454 void C41Effect::render_gui(void* data)
456 // Updating values computed by process_frame
457 struct magic *vp = (struct magic *)data;
461 C41Window *window = (C41Window *) thread->window;
462 window->lock_window("C41Effect::render_gui");
463 window->update_magic();
464 window->unlock_window();
468 void C41Effect::save_data(KeyFrame *keyframe)
471 output.set_shared_output(keyframe->xbuf);
473 output.tag.set_title("C41");
474 output.tag.set_property("ACTIVE", config.active);
475 output.tag.set_property("COMPUTE_MAGIC", config.compute_magic);
476 output.tag.set_property("POSTPROC", config.postproc);
477 output.tag.set_property("SHOW_BOX", config.show_box);
479 output.tag.set_property("FIX_MIN_R", config.fix_min_r);
480 output.tag.set_property("FIX_MIN_G", config.fix_min_g);
481 output.tag.set_property("FIX_MIN_B", config.fix_min_b);
482 output.tag.set_property("FIX_LIGHT", config.fix_light);
483 output.tag.set_property("FIX_GAMMA_G", config.fix_gamma_g);
484 output.tag.set_property("FIX_GAMMA_B", config.fix_gamma_b);
485 output.tag.set_property("FIX_COEF1", config.fix_coef1);
486 output.tag.set_property("FIX_COEF2", config.fix_coef2);
487 output.tag.set_property("MIN_ROW", config.min_row);
488 output.tag.set_property("MAX_ROW", config.max_row);
489 output.tag.set_property("MIN_COL", config.min_col);
490 output.tag.set_property("MAX_COL", config.max_col);
492 output.tag.set_title("/C41");
494 output.terminate_string();
497 void C41Effect::read_data(KeyFrame *keyframe)
500 input.set_shared_input(keyframe->xbuf);
502 while(!input.read_tag())
504 if( input.tag.title_is("C41") ) {
505 config.active = input.tag.get_property("ACTIVE", config.active);
506 config.compute_magic = input.tag.get_property("COMPUTE_MAGIC", config.compute_magic);
507 config.postproc = input.tag.get_property("POSTPROC", config.postproc);
508 config.show_box = input.tag.get_property("SHOW_BOX", config.show_box);
509 config.fix_min_r = input.tag.get_property("FIX_MIN_R", config.fix_min_r);
510 config.fix_min_g = input.tag.get_property("FIX_MIN_G", config.fix_min_g);
511 config.fix_min_b = input.tag.get_property("FIX_MIN_B", config.fix_min_b);
512 config.fix_light = input.tag.get_property("FIX_LIGHT", config.fix_light);
513 config.fix_gamma_g = input.tag.get_property("FIX_GAMMA_G", config.fix_gamma_g);
514 config.fix_gamma_b = input.tag.get_property("FIX_GAMMA_B", config.fix_gamma_b);
515 config.fix_coef1 = input.tag.get_property("FIX_COEF1", config.fix_coef2);
516 config.fix_coef2 = input.tag.get_property("FIX_COEF2", config.fix_coef2);
517 config.min_row = input.tag.get_property("MIN_ROW", config.min_row);
518 config.max_row = input.tag.get_property("MAX_ROW", config.max_row);
519 config.min_col = input.tag.get_property("MIN_COL", config.min_col);
520 config.max_col = input.tag.get_property("MAX_COL", config.max_col);
525 #if defined(C41_FAST_POW)
527 float C41Effect::myLog2(float i)
530 float LogBodge = 0.346607f;
532 x *= 1.0 / (1 << 23); // 1/pow(2,23);
536 y = (y - y * y) * LogBodge;
540 float C41Effect::myPow2(float i)
542 float PowBodge = 0.33971f;
544 float y = i - floorf(i);
545 y = (y - y * y) * PowBodge;
553 float C41Effect::myPow(float a, float b)
555 return myPow2(b * myLog2(a));
560 void C41Effect::pix_fix(float &pix, float min, float gamma)
565 if( gamma != 1 ) val = POWF(val, gamma);
566 bclamp(val -= config.fix_light, 0, pix_max);
567 if( config.postproc )
568 val = config.fix_coef1 * val + config.fix_coef2;
575 int C41Effect::process_realtime(VFrame *input, VFrame *output)
577 load_configuration();
579 int frame_w = input->get_w(), frame_h = input->get_h();
580 int input_model = input->get_color_model();
581 int has_alpha = BC_CModels::has_alpha(input_model);
582 int color_model = has_alpha ? BC_RGBA_FLOAT : BC_RGB_FLOAT;
584 if( input_model != color_model ) {
586 ( frame->get_w() != frame_w || frame->get_h() != frame_h ||
587 frame->get_color_model() != color_model ) ) {
588 delete frame; frame = 0;
591 frame = new VFrame(frame_w, frame_h, color_model);
592 frame->transfer_from(input);
599 pix_max = BC_CModels::calculate_max(color_model);
600 pix_len = BC_CModels::components(color_model);
602 min_row = config.min_row; bclamp(min_row, 0,frame_h);
603 max_row = config.max_row; bclamp(max_row, 0,frame_h);
604 min_col = config.min_col; bclamp(min_col, 0,frame_w);
605 max_col = config.max_col; bclamp(max_col, 0,frame_w);
607 if( config.compute_magic ) {
608 // Convert frame to RGB for magic computing
610 (tmp_frame->get_w() != frame_w || tmp_frame->get_h() != frame_h) ) {
611 delete tmp_frame; tmp_frame = 0;
614 tmp_frame = new VFrame(frame_w, frame_h, BC_RGB_FLOAT);
616 (blurry_frame->get_w() != frame_w || blurry_frame->get_h() != frame_h) ) {
617 delete blurry_frame; blurry_frame = 0;
620 blurry_frame = new VFrame(frame_w, frame_h, BC_RGB_FLOAT);
622 float **rows = (float **)frame->get_rows();
623 float **tmp_rows = (float **)tmp_frame->get_rows();
624 float **blurry_rows = (float **)blurry_frame->get_rows();
626 for( int i=0; i<frame_h; ++i ) {
627 float *row = rows[i], *tmp = tmp_rows[i];
628 for( int j=frame_w; --j>=0; row+=pix_len ) {
629 *tmp++ = bclip(row[0], 0, pix_max);
630 *tmp++ = bclip(row[1], 0, pix_max);
631 *tmp++ = bclip(row[2], 0, pix_max);
635 // 10 passes of Box blur should be good
636 int dx = 5, dy = 5, y_up, y_dn, x_rt, x_lt;
637 float **src = tmp_rows, **tgt = blurry_rows;
638 for( int pass = 0; pass < 10; pass++ ) {
639 for( int y = 0; y < frame_h; y++ ) {
640 if( (y_up=y-dy) < 0 ) y_up = 0;
641 if( (y_dn=y+dy) >= frame_h ) y_dn = frame_h-1;
642 float *up = src[y_up], *dn = src[y_dn], *row = tgt[y];
643 for( int x = 0; x < frame_w; ++x ) {
644 if( (x_lt=x-dx) < 0 ) x_lt = 0;
645 if( (x_rt=x+dx) >= frame_w ) x_lt = frame_w-1;
646 float *dn_l = dn + 3*x_lt, *dn_r = dn + 3*x_rt;
647 float *up_l = up + 3*x_lt, *up_r = up + 3*x_rt;
648 row[0] = (dn_l[0] + dn_r[0] + up_l[0] + up_r[0]) / 4;
649 row[1] = (dn_l[1] + dn_r[1] + up_l[1] + up_r[1]) / 4;
650 row[2] = (dn_l[2] + dn_r[2] + up_l[2] + up_r[2]) / 4;
654 float **rows = src; src = tgt; tgt = rows;
657 // Shave image: cut off border areas where min max difference
658 // is less than C41_SHAVE_TOLERANCE, starting/ending at C41_SHAVE_MARGIN
660 shave_min_row = shave_min_col = 0;
661 shave_max_col = frame_w;
662 shave_max_row = frame_h;
663 int min_w = frame_w * C41_SHAVE_MARGIN;
664 int max_w = frame_w * (1-C41_SHAVE_MARGIN);
665 int min_h = frame_h * C41_SHAVE_MARGIN;
666 int max_h = frame_h * (1-C41_SHAVE_MARGIN);
668 float yv_min[frame_h], yv_max[frame_h];
669 for( int y=0; y<frame_h; ++y ) {
674 for( int y = 0; y < frame_h; y++ ) {
675 float *row = blurry_rows[y];
676 for( int x = 0; x < frame_w; x++, row += 3 ) {
677 float yv = (row[0] + row[1] + row[2]) / 3;
678 if( yv_min[y] > yv ) yv_min[y] = yv;
679 if( yv_max[y] < yv ) yv_max[y] = yv;
683 for( shave_min_row = min_h; shave_min_row < max_h; shave_min_row++ )
684 if( yv_max[shave_min_row] - yv_min[shave_min_row] > C41_SHAVE_TOLERANCE )
686 for( shave_max_row = max_h - 1; shave_max_row > shave_min_row; shave_max_row-- )
687 if( yv_max[shave_max_row] - yv_min[shave_max_row] > C41_SHAVE_TOLERANCE )
691 float xv_min[frame_w], xv_max[frame_w];
692 for( int x=0; x<frame_w; ++x ) {
697 for( int y = shave_min_row; y < shave_max_row; y++ ) {
698 float *row = blurry_rows[y];
699 for( int x = 0; x < frame_w; x++, row += 3 ) {
700 float xv = (row[0] + row[1] + row[2]) / 3;
701 if( xv_min[x] > xv ) xv_min[x] = xv;
702 if( xv_max[x] < xv ) xv_max[x] = xv;
706 for( shave_min_col = min_w; shave_min_col < max_w; shave_min_col++ )
707 if( xv_max[shave_min_col] - xv_min[shave_min_col] > C41_SHAVE_TOLERANCE )
709 for( shave_max_col = max_w - 1; shave_max_col > shave_min_col; shave_max_col-- )
710 if( xv_max[shave_max_col] - xv_min[shave_max_col] > C41_SHAVE_TOLERANCE )
714 // Compute magic negfix values
715 float minima_r = 50., minima_g = 50., minima_b = 50.;
716 float maxima_r = 0., maxima_g = 0., maxima_b = 0.;
718 // Check if config_parameters are usable
719 if( config.min_col >= config.max_col || config.min_row >= config.max_row ) {
720 min_col = shave_min_col;
721 max_col = shave_max_col;
722 min_row = shave_min_row;
723 max_row = shave_max_row;
726 for( int i = min_row; i < max_row; i++ ) {
727 float *row = blurry_rows[i] + 3 * min_col;
728 for( int j = min_col; j < max_col; j++, row+=3 ) {
729 if( row[0] < minima_r ) minima_r = row[0];
730 if( row[0] > maxima_r ) maxima_r = row[0];
731 if( row[1] < minima_g ) minima_g = row[1];
732 if( row[1] > maxima_g ) maxima_g = row[1];
733 if( row[2] < minima_b ) minima_b = row[2];
734 if( row[2] > maxima_b ) maxima_b = row[2];
737 values.min_r = minima_r; values.max_r = maxima_r;
738 values.min_g = minima_g; values.max_g = maxima_g;
739 values.min_b = minima_b; values.max_b = maxima_b;
740 values.light = maxima_r < 1e-6 ? 1.0 : minima_r / maxima_r;
741 bclamp(minima_r, 1e-6, 1-1e-6); bclamp(maxima_r, 1e-6, 1-1e-6);
742 bclamp(minima_g, 1e-6, 1-1e-6); bclamp(maxima_g, 1e-6, 1-1e-6);
743 bclamp(minima_b, 1e-6, 1-1e-6); bclamp(maxima_b, 1e-6, 1-1e-6);
744 float log_r = logf(maxima_r / minima_r);
745 float log_g = logf(maxima_g / minima_g);
746 float log_b = logf(maxima_b / minima_b);
747 if( log_g < 1e-6 ) log_g = 1e-6;
748 if( log_b < 1e-6 ) log_b = 1e-6;
749 values.gamma_g = log_r / log_g;
750 values.gamma_b = log_r / log_b;
751 values.shave_min_col = shave_min_col;
752 values.shave_max_col = shave_max_col;
753 values.shave_min_row = shave_min_row;
754 values.shave_max_row = shave_max_row;
755 values.coef1 = values.coef2 = -1;
758 send_render_gui(&values);
761 if( config.compute_magic ) {
762 float minima_r = 50., minima_g = 50., minima_b = 50.;
763 float maxima_r = 0., maxima_g = 0., maxima_b = 0.;
765 for( int i = min_row; i < max_row; i++ ) {
766 float *row = (float*)frame->get_rows()[i];
768 for( int j = min_col; j < max_col; j++, row += 3 ) {
769 if( row[0] < minima_r ) minima_r = row[0];
770 if( row[0] > maxima_r ) maxima_r = row[0];
772 if( row[1] < minima_g ) minima_g = row[1];
773 if( row[1] > maxima_g ) maxima_g = row[1];
775 if( row[2] < minima_b ) minima_b = row[2];
776 if( row[2] > maxima_b ) maxima_b = row[2];
780 // Calculate postprocessing coeficents
781 values.coef2 = minima_r;
782 if( minima_g < values.coef2 ) values.coef2 = minima_g;
783 if( minima_b < values.coef2 ) values.coef2 = minima_b;
784 values.coef1 = maxima_r;
785 if( maxima_g > values.coef1 ) values.coef1 = maxima_g;
786 if( maxima_b > values.coef1 ) values.coef1 = maxima_b;
787 // Try not to overflow RGB601 (235 - 16) / 256 * 0.9
788 float den = values.coef1 - values.coef2;
789 if( fabs(den) < 1e-6 ) den = 1e-6;
790 values.coef1 = 0.770 / den;
791 values.coef2 = 0.065 - values.coef2 * values.coef1;
792 send_render_gui(&values);
795 // Apply the transformation
796 if( config.active ) {
797 for( int i = 0; i < frame_h; i++ ) {
798 float *row = (float*)frame->get_rows()[i];
799 for( int j = 0; j < frame_w; j++, row += pix_len ) {
800 pix_fix(row[0], config.fix_min_r, 1);
801 pix_fix(row[1], config.fix_min_g, config.fix_gamma_g);
802 pix_fix(row[2], config.fix_min_b, config.fix_gamma_b);
807 if( config.show_box ) {
808 EDLSession *session = get_edl()->session;
809 int line_w = bmax(session->output_w,session->output_h) / 600 + 1;
810 for( int k=0; k<line_w; ++k ) {
811 float **rows = (float **)frame->get_rows();
812 if( min_row < max_row - 1 ) {
813 float *row1 = (float *)rows[min_row+k];
814 float *row2 = (float *)rows[max_row-k - 1];
816 for( int i = 0; i < frame_w; i++ ) {
817 for( int j = 0; j < 3; j++ ) {
818 row1[j] = pix_max - row1[j];
819 row2[j] = pix_max - row2[j];
830 if( min_col < max_col - 1 ) {
831 int pix1 = pix_len * (min_col+k);
832 int pix2 = pix_len * (max_col-k - 1);
834 for( int i = 0; i < frame_h; i++ ) {
835 float *row1 = rows[i] + pix1;
836 float *row2 = rows[i] + pix2;
838 for( int j = 0; j < 3; j++ ) {
839 row1[j] = pix_max - row1[j];
840 row2[j] = pix_max - row2[j];
851 if( frame != output )
852 output->transfer_from(frame);