2 #include "colorbalance.h"
10 #define _(String) gettext(String)
11 #define gettext_noop(String) String
12 #define N_(String) gettext_noop (String)
14 #define SQR(a) ((a) * (a))
16 REGISTER_PLUGIN(ColorBalanceMain)
20 ColorBalanceConfig::ColorBalanceConfig()
29 int ColorBalanceConfig::equivalent(ColorBalanceConfig &that)
31 return (cyan == that.cyan &&
32 magenta == that.magenta &&
33 yellow == that.yellow &&
34 lock_params == that.lock_params &&
35 preserve == that.preserve);
38 void ColorBalanceConfig::copy_from(ColorBalanceConfig &that)
41 magenta = that.magenta;
43 lock_params = that.lock_params;
44 preserve = that.preserve;
47 void ColorBalanceConfig::interpolate(ColorBalanceConfig &prev,
48 ColorBalanceConfig &next,
51 int64_t current_frame)
53 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
54 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
56 this->cyan = prev.cyan * prev_scale + next.cyan * next_scale;
57 this->magenta = prev.magenta * prev_scale + next.magenta * next_scale;
58 this->yellow = prev.yellow * prev_scale + next.yellow * next_scale;
70 ColorBalanceEngine::ColorBalanceEngine(ColorBalanceMain *plugin)
73 this->plugin = plugin;
80 ColorBalanceEngine::~ColorBalanceEngine()
88 int ColorBalanceEngine::start_process_frame(VFrame *output, VFrame *input, int row_start, int row_end)
90 this->output = output;
92 this->row_start = row_start;
93 this->row_end = row_end;
99 int ColorBalanceEngine::wait_process_frame()
106 void ColorBalanceEngine::run()
113 output_lock.unlock();
117 #define PROCESS(yuvtorgb, \
128 int y, cb, cr, r, g, b, r_n, g_n, b_n; \
129 float h, s, v, h_old, s_old, r_f, g_f, b_f; \
130 type **input_rows, **output_rows; \
131 input_rows = (type**)input->get_rows(); \
132 output_rows = (type**)output->get_rows(); \
134 for(j = row_start; j < row_end; j++) \
136 for(k = 0; k < input->get_w() * components; k += components) \
140 y = input_rows[j][k]; \
141 cb = input_rows[j][k + 1]; \
142 cr = input_rows[j][k + 2]; \
143 yuvtorgb(r, g, b, y, cb, cr); \
147 r = input_rows[j][k]; \
148 g = input_rows[j][k + 1]; \
149 b = input_rows[j][k + 2]; \
152 r_n = plugin->r_lookup[r]; \
153 g_n = plugin->g_lookup[g]; \
154 b_n = plugin->b_lookup[b]; \
156 if(plugin->config.preserve) \
158 HSV::rgb_to_hsv((float)r_n, (float)g_n, (float)b_n, h, s, v); \
159 HSV::rgb_to_hsv((float)r, (float)g, (float)b, h_old, s_old, v); \
160 HSV::hsv_to_rgb(r_f, g_f, b_f, h, s, v); \
174 rgbtoyuv(CLAMP(r, 0, max), CLAMP(g, 0, max), CLAMP(b, 0, max), y, cb, cr); \
175 output_rows[j][k] = y; \
176 output_rows[j][k + 1] = cb; \
177 output_rows[j][k + 2] = cr; \
181 output_rows[j][k] = CLAMP(r, 0, max); \
182 output_rows[j][k + 1] = CLAMP(g, 0, max); \
183 output_rows[j][k + 2] = CLAMP(b, 0, max); \
189 switch(input->get_color_model())
192 PROCESS(yuv.yuv_to_rgb_8,
204 PROCESS(yuv.yuv_to_rgb_8,
216 PROCESS(yuv.yuv_to_rgb_8,
228 PROCESS(yuv.yuv_to_rgb_8,
240 PROCESS(yuv.yuv_to_rgb_16,
252 PROCESS(yuv.yuv_to_rgb_16,
263 case BC_RGBA16161616:
264 PROCESS(yuv.yuv_to_rgb_16,
275 case BC_YUVA16161616:
276 PROCESS(yuv.yuv_to_rgb_16,
290 output_lock.unlock();
297 ColorBalanceMain::ColorBalanceMain(PluginServer *server)
298 : PluginVClient(server)
300 need_reconfigure = 1;
302 PLUGIN_CONSTRUCTOR_MACRO
305 ColorBalanceMain::~ColorBalanceMain()
307 PLUGIN_DESTRUCTOR_MACRO
312 for(int i = 0; i < total_engines; i++)
320 char* ColorBalanceMain::plugin_title() { return _("Color Balance"); }
321 int ColorBalanceMain::is_realtime() { return 1; }
324 int ColorBalanceMain::reconfigure()
327 double *cyan_red_transfer;
328 double *magenta_green_transfer;
329 double *yellow_blue_transfer;
332 #define RECONFIGURE(highlights_add, highlights_sub, r_lookup, g_lookup, b_lookup, max) \
333 cyan_red_transfer = config.cyan > 0 ? highlights_add : highlights_sub; \
334 magenta_green_transfer = config.magenta > 0 ? highlights_add : highlights_sub; \
335 yellow_blue_transfer = config.yellow > 0 ? highlights_add : highlights_sub; \
336 for(int i = 0; i < max; i++) \
338 r_n = g_n = b_n = i; \
339 r_n += (int)(config.cyan / 100 * max * cyan_red_transfer[r_n]); \
340 g_n += (int)(config.magenta / 100 * max * magenta_green_transfer[g_n]); \
341 b_n += (int)(config.yellow / 100 * max * yellow_blue_transfer[b_n]); \
343 if(r_n > max) r_n = max; \
345 if(r_n < 0) r_n = 0; \
346 if(g_n > max) g_n = max; \
348 if(g_n < 0) g_n = 0; \
349 if(b_n > max) b_n = max; \
351 if(b_n < 0) b_n = 0; \
359 RECONFIGURE(highlights_add_8, highlights_sub_8, r_lookup_8, g_lookup_8, b_lookup_8, 0xff);
360 RECONFIGURE(highlights_add_16, highlights_sub_16, r_lookup_16, g_lookup_16, b_lookup_16, 0xffff);
365 int ColorBalanceMain::test_boundary(float &value)
367 if(value < -100) value = -100;
368 if(value > 100) value = 100;
372 int ColorBalanceMain::synchronize_params(ColorBalanceSlider *slider, float difference)
374 if(thread && config.lock_params)
376 if(slider != thread->window->cyan)
378 config.cyan += difference;
379 test_boundary(config.cyan);
380 thread->window->cyan->update((int64_t)config.cyan);
382 if(slider != thread->window->magenta)
384 config.magenta += difference;
385 test_boundary(config.magenta);
386 thread->window->magenta->update((int64_t)config.magenta);
388 if(slider != thread->window->yellow)
390 config.yellow += difference;
391 test_boundary(config.yellow);
392 thread->window->yellow->update((int64_t)config.yellow);
403 NEW_PICON_MACRO(ColorBalanceMain)
404 LOAD_CONFIGURATION_MACRO(ColorBalanceMain, ColorBalanceConfig)
405 SHOW_GUI_MACRO(ColorBalanceMain, ColorBalanceThread)
406 RAISE_WINDOW_MACRO(ColorBalanceMain)
407 SET_STRING_MACRO(ColorBalanceMain)
413 int ColorBalanceMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
415 need_reconfigure |= load_configuration();
417 //printf("ColorBalanceMain::process_realtime 1 %d\n", need_reconfigure);
424 #define CALCULATE_HIGHLIGHTS(add, sub, max) \
425 for(i = 0; i < max; i++) \
427 add[i] = sub[i] = 0.667 * (1 - SQR(((double)i - (max / 2)) / (max / 2))); \
430 CALCULATE_HIGHLIGHTS(highlights_add_8, highlights_sub_8, 0xff);
431 CALCULATE_HIGHLIGHTS(highlights_add_16, highlights_sub_16, 0xffff);
433 total_engines = PluginClient::smp > 1 ? 2 : 1;
434 engine = new ColorBalanceEngine*[total_engines];
435 for(int i = 0; i < total_engines; i++)
437 engine[i] = new ColorBalanceEngine(this);
443 need_reconfigure = 0;
447 if(config.cyan != 0 || config.magenta != 0 || config.yellow != 0)
449 int64_t row_step = input_ptr->get_h() / total_engines + 1;
450 for(int i = 0; i < input_ptr->get_h(); i += row_step)
452 if(i + row_step > input_ptr->get_h())
453 row_step = input_ptr->get_h() - i;
454 engine[i]->start_process_frame(output_ptr,
460 for(int i = 0; i < total_engines; i++)
462 engine[i]->wait_process_frame();
466 // Data never processed so copy if necessary
467 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
469 output_ptr->copy_from(input_ptr);
471 //printf("ColorBalanceMain::process_realtime 2\n");
477 int ColorBalanceMain::load_defaults()
479 char directory[1024], string[1024];
480 // set the default directory
481 sprintf(directory, "%scolorbalance.rc", BCASTDIR);
484 defaults = new Defaults(directory);
487 config.cyan = defaults->get("CYAN", config.cyan);
488 config.magenta = defaults->get("MAGENTA", config.magenta);
489 config.yellow = defaults->get("YELLOW", config.yellow);
490 config.preserve = defaults->get("PRESERVELUMINOSITY", config.preserve);
491 config.lock_params = defaults->get("LOCKPARAMS", config.lock_params);
495 int ColorBalanceMain::save_defaults()
497 defaults->update("CYAN", config.cyan);
498 defaults->update("MAGENTA", config.magenta);
499 defaults->update("YELLOW", config.yellow);
500 defaults->update("PRESERVELUMINOSITY", config.preserve);
501 defaults->update("LOCKPARAMS", config.lock_params);
506 void ColorBalanceMain::save_data(KeyFrame *keyframe)
510 // cause data to be stored directly in text
511 output.set_shared_string(keyframe->data, MESSAGESIZE);
512 output.tag.set_title("COLORBALANCE");
513 output.tag.set_property("CYAN", config.cyan);
514 output.tag.set_property("MAGENTA", config.magenta);
515 output.tag.set_property("YELLOW", config.yellow);
516 output.tag.set_property("PRESERVELUMINOSITY", config.preserve);
517 output.tag.set_property("LOCKPARAMS", config.lock_params);
519 output.terminate_string();
522 void ColorBalanceMain::read_data(KeyFrame *keyframe)
526 //printf("ColorBalanceMain::read_data 1\n");
527 input.set_shared_string(keyframe->data, strlen(keyframe->data));
528 //printf("ColorBalanceMain::read_data 1\n");
531 //printf("ColorBalanceMain::read_data 1\n");
535 result = input.read_tag();
539 if(input.tag.title_is("COLORBALANCE"))
541 config.cyan = input.tag.get_property("CYAN", config.cyan);
542 config.magenta = input.tag.get_property("MAGENTA", config.magenta);
543 config.yellow = input.tag.get_property("YELLOW", config.yellow);
544 config.preserve = input.tag.get_property("PRESERVELUMINOSITY", config.preserve);
545 config.lock_params = input.tag.get_property("LOCKPARAMS", config.lock_params);
549 //printf("ColorBalanceMain::read_data 2\n");