4 * Copyright (C) 2008 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
24 #include "colorbalance.h"
27 #include "playback3d.h"
29 #include "aggregated.h"
30 #include "../interpolate/aggregated.h"
31 #include "../gamma/aggregated.h"
36 // 1000 corresponds to (1.0 + MAX_COLOR) * input
39 REGISTER_PLUGIN(ColorBalanceMain)
43 ColorBalanceConfig::ColorBalanceConfig()
52 int ColorBalanceConfig::equivalent(ColorBalanceConfig &that)
54 return (cyan == that.cyan &&
55 magenta == that.magenta &&
56 yellow == that.yellow &&
57 lock_params == that.lock_params &&
58 preserve == that.preserve);
61 void ColorBalanceConfig::copy_from(ColorBalanceConfig &that)
64 magenta = that.magenta;
66 lock_params = that.lock_params;
67 preserve = that.preserve;
70 void ColorBalanceConfig::interpolate(ColorBalanceConfig &prev,
71 ColorBalanceConfig &next,
74 int64_t current_frame)
76 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
77 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
79 this->cyan = prev.cyan * prev_scale + next.cyan * next_scale;
80 this->magenta = prev.magenta * prev_scale + next.magenta * next_scale;
81 this->yellow = prev.yellow * prev_scale + next.yellow * next_scale;
82 this->preserve = prev.preserve;
83 this->lock_params = prev.lock_params;
95 ColorBalanceEngine::ColorBalanceEngine(ColorBalanceMain *plugin)
98 this->plugin = plugin;
103 ColorBalanceEngine::~ColorBalanceEngine()
111 int ColorBalanceEngine::start_process_frame(VFrame *output, VFrame *input, int row_start, int row_end)
113 this->output = output;
115 this->row_start = row_start;
116 this->row_end = row_end;
122 int ColorBalanceEngine::wait_process_frame()
124 output_lock.lock("ColorBalanceEngine::wait_process_frame");
128 void ColorBalanceEngine::run()
132 input_lock.lock("ColorBalanceEngine::run");
135 output_lock.unlock();
139 #define PROCESS(yuvtorgb, \
150 int y, cr, cb, r, g, b, r_n, g_n, b_n; \
151 float h, s, v, h_old, s_old, r_f, g_f, b_f; \
152 type **input_rows, **output_rows; \
153 input_rows = (type**)input->get_rows(); \
154 output_rows = (type**)output->get_rows(); \
156 for(j = row_start; j < row_end; j++) \
158 for(k = 0; k < input->get_w() * components; k += components) \
162 y = input_rows[j][k]; \
163 cb = input_rows[j][k + 1]; \
164 cr = input_rows[j][k + 2]; \
165 yuvtorgb(r, g, b, y, cb, cr); \
169 r = input_rows[j][k]; \
170 g = input_rows[j][k + 1]; \
171 b = input_rows[j][k + 2]; \
174 r_n = plugin->r_lookup[r]; \
175 g_n = plugin->g_lookup[g]; \
176 b_n = plugin->b_lookup[b]; \
178 if(plugin->config.preserve) \
180 HSV::rgb_to_hsv((float)r_n, (float)g_n, (float)b_n, h, s, v); \
181 HSV::rgb_to_hsv((float)r, (float)g, (float)b, h_old, s_old, v); \
182 HSV::hsv_to_rgb(r_f, g_f, b_f, h, s, v); \
196 rgbtoyuv(CLAMP(r, 0, max), CLAMP(g, 0, max), CLAMP(b, 0, max), y, cb, cr); \
197 output_rows[j][k] = y; \
198 output_rows[j][k + 1] = cb; \
199 output_rows[j][k + 2] = cr; \
203 output_rows[j][k] = CLAMP(r, 0, max); \
204 output_rows[j][k + 1] = CLAMP(g, 0, max); \
205 output_rows[j][k + 2] = CLAMP(b, 0, max); \
211 #define PROCESS_F(components) \
214 float r, g, b, r_n, g_n, b_n; \
215 float h, s, v, h_old, s_old, r_f, g_f, b_f; \
216 float **input_rows, **output_rows; \
217 input_rows = (float**)input->get_rows(); \
218 output_rows = (float**)output->get_rows(); \
219 cyan_f = plugin->calculate_transfer(plugin->config.cyan); \
220 magenta_f = plugin->calculate_transfer(plugin->config.magenta); \
221 yellow_f = plugin->calculate_transfer(plugin->config.yellow); \
223 printf("PROCESS_F %f\n", cyan_f); \
224 for(j = row_start; j < row_end; j++) \
226 for(k = 0; k < input->get_w() * components; k += components) \
228 r = input_rows[j][k]; \
229 g = input_rows[j][k + 1]; \
230 b = input_rows[j][k + 2]; \
233 g_n = g * magenta_f; \
234 b_n = b * yellow_f; \
236 if(plugin->config.preserve) \
238 HSV::rgb_to_hsv(r_n, g_n, b_n, h, s, v); \
239 HSV::rgb_to_hsv(r, g, b, h_old, s_old, v); \
240 HSV::hsv_to_rgb(r_f, g_f, b_f, h, s, v); \
252 output_rows[j][k] = r; \
253 output_rows[j][k + 1] = g; \
254 output_rows[j][k + 2] = b; \
259 switch(input->get_color_model())
262 PROCESS(yuv.yuv_to_rgb_8, yuv.rgb_to_yuv_8,
263 r_lookup_8, g_lookup_8, b_lookup_8,
264 unsigned char, 0xff, 3, 0);
272 PROCESS(yuv.yuv_to_rgb_8, yuv.rgb_to_yuv_8,
273 r_lookup_8, g_lookup_8, b_lookup_8,
274 unsigned char, 0xff, 3, 1);
282 PROCESS(yuv.yuv_to_rgb_8, yuv.rgb_to_yuv_8,
283 r_lookup_8, g_lookup_8, b_lookup_8,
284 unsigned char, 0xff, 4, 0);
288 PROCESS(yuv.yuv_to_rgb_8, yuv.rgb_to_yuv_8,
289 r_lookup_8, g_lookup_8, b_lookup_8,
290 unsigned char, 0xff, 4, 1);
294 PROCESS(yuv.yuv_to_rgb_16, yuv.rgb_to_yuv_16,
295 r_lookup_16, g_lookup_16, b_lookup_16,
296 u_int16_t, 0xffff, 3, 1);
299 case BC_YUVA16161616:
300 PROCESS(yuv.yuv_to_rgb_16, yuv.rgb_to_yuv_16,
301 r_lookup_16, g_lookup_16, b_lookup_16,
302 u_int16_t, 0xffff, 4, 1);
308 output_lock.unlock();
315 ColorBalanceMain::ColorBalanceMain(PluginServer *server)
316 : PluginVClient(server)
318 need_reconfigure = 1;
323 ColorBalanceMain::~ColorBalanceMain()
330 for(int i = 0; i < total_engines; i++)
338 const char* ColorBalanceMain::plugin_title() { return _("Color Balance"); }
339 int ColorBalanceMain::is_realtime() { return 1; }
342 int ColorBalanceMain::reconfigure()
344 float r_scale = calculate_transfer(config.cyan);
345 float g_scale = calculate_transfer(config.magenta);
346 float b_scale = calculate_transfer(config.yellow);
348 #define RECONFIGURE(r_lookup, g_lookup, b_lookup, max) \
349 for(int i = 0; i <= max; i++) \
351 r_lookup[i] = CLIP((int)(r_scale * i), 0, max); \
352 g_lookup[i] = CLIP((int)(g_scale * i), 0, max); \
353 b_lookup[i] = CLIP((int)(b_scale * i), 0, max); \
356 RECONFIGURE(r_lookup_8, g_lookup_8, b_lookup_8, 0xff);
357 RECONFIGURE(r_lookup_16, g_lookup_16, b_lookup_16, 0xffff);
362 int64_t ColorBalanceMain::calculate_slider(float in)
366 return (int64_t)(in * 1000 - 1000.0);
371 return (int64_t)(1000 * (in - 1.0) / MAX_COLOR);
377 float ColorBalanceMain::calculate_transfer(float in)
381 return (1000.0 + in) / 1000.0;
386 return 1.0 + in / 1000.0 * MAX_COLOR;
395 int ColorBalanceMain::test_boundary(float &value)
398 if(value < -1000) value = -1000;
399 if(value > 1000) value = 1000;
403 int ColorBalanceMain::synchronize_params(ColorBalanceSlider *slider, float difference)
405 if(thread && config.lock_params)
407 if(slider != ((ColorBalanceWindow*)thread->window)->cyan)
409 config.cyan += difference;
410 test_boundary(config.cyan);
411 ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
413 if(slider != ((ColorBalanceWindow*)thread->window)->magenta)
415 config.magenta += difference;
416 test_boundary(config.magenta);
417 ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
419 if(slider != ((ColorBalanceWindow*)thread->window)->yellow)
421 config.yellow += difference;
422 test_boundary(config.yellow);
423 ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
434 LOAD_CONFIGURATION_MACRO(ColorBalanceMain, ColorBalanceConfig)
435 NEW_WINDOW_MACRO(ColorBalanceMain, ColorBalanceWindow)
441 int ColorBalanceMain::process_buffer(VFrame *frame,
442 int64_t start_position,
445 need_reconfigure |= load_configuration();
447 //printf("ColorBalanceMain::process_realtime 1 %d\n", need_reconfigure);
452 total_engines = PluginClient::smp + 1;
453 engine = new ColorBalanceEngine*[total_engines];
454 for(int i = 0; i < total_engines; i++)
456 engine[i] = new ColorBalanceEngine(this);
462 need_reconfigure = 0;
465 frame->get_params()->update("COLORBALANCE_PRESERVE", config.preserve);
466 frame->get_params()->update("COLORBALANCE_CYAN", calculate_transfer(config.cyan));
467 frame->get_params()->update("COLORBALANCE_MAGENTA", calculate_transfer(config.magenta));
468 frame->get_params()->update("COLORBALANCE_YELLOW", calculate_transfer(config.yellow));
473 get_source_position(),
477 int aggregate_interpolate = 0;
478 int aggregate_gamma = 0;
479 get_aggregation(&aggregate_interpolate,
482 if(!EQUIV(config.cyan, 0) ||
483 !EQUIV(config.magenta, 0) ||
484 !EQUIV(config.yellow, 0) ||
486 (aggregate_interpolate ||
491 //get_output()->dump_stacks();
493 if(next_effect_is(_("Histogram"))) return 0;
497 for(int i = 0; i < total_engines; i++)
499 engine[i]->start_process_frame(frame,
501 frame->get_h() * i / total_engines,
502 frame->get_h() * (i + 1) / total_engines);
505 for(int i = 0; i < total_engines; i++)
507 engine[i]->wait_process_frame();
516 void ColorBalanceMain::update_gui()
520 load_configuration();
521 ((ColorBalanceWindow*)thread->window)->lock_window("ColorBalanceMain::update_gui");
522 ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
523 ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
524 ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
525 ((ColorBalanceWindow*)thread->window)->preserve->update(config.preserve);
526 ((ColorBalanceWindow*)thread->window)->lock_params->update(config.lock_params);
527 ((ColorBalanceWindow*)thread->window)->unlock_window();
534 void ColorBalanceMain::save_data(KeyFrame *keyframe)
538 // cause data to be stored directly in text
539 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
540 output.tag.set_title("COLORBALANCE");
541 output.tag.set_property("CYAN", config.cyan);
542 output.tag.set_property("MAGENTA", config.magenta);
543 output.tag.set_property("YELLOW", config.yellow);
544 output.tag.set_property("PRESERVELUMINOSITY", config.preserve);
545 output.tag.set_property("LOCKPARAMS", config.lock_params);
547 output.tag.set_title("/COLORBALANCE");
549 output.append_newline();
550 output.terminate_string();
553 void ColorBalanceMain::read_data(KeyFrame *keyframe)
557 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
563 result = input.read_tag();
567 if(input.tag.title_is("COLORBALANCE"))
569 config.cyan = input.tag.get_property("CYAN", config.cyan);
570 config.magenta = input.tag.get_property("MAGENTA", config.magenta);
571 config.yellow = input.tag.get_property("YELLOW", config.yellow);
572 config.preserve = input.tag.get_property("PRESERVELUMINOSITY", config.preserve);
573 config.lock_params = input.tag.get_property("LOCKPARAMS", config.lock_params);
579 void ColorBalanceMain::get_aggregation(int *aggregate_interpolate,
580 int *aggregate_gamma)
582 if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
583 !strcmp(get_output()->get_prev_effect(0), _("Gamma")))
585 *aggregate_interpolate = 1;
586 *aggregate_gamma = 1;
589 if(!strcmp(get_output()->get_prev_effect(0), _("Interpolate Pixels")))
591 *aggregate_interpolate = 1;
594 if(!strcmp(get_output()->get_prev_effect(0), _("Gamma")))
596 *aggregate_gamma = 1;
600 int ColorBalanceMain::handle_opengl()
604 get_output()->to_texture();
605 get_output()->enable_opengl();
607 unsigned int shader = 0;
608 const char *shader_stack[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
609 int current_shader = 0;
610 int aggregate_interpolate = 0;
611 int aggregate_gamma = 0;
613 get_aggregation(&aggregate_interpolate,
616 //printf("ColorBalanceMain::handle_opengl %d %d\n", aggregate_interpolate, aggregate_gamma);
617 if(aggregate_interpolate)
618 INTERPOLATE_COMPILE(shader_stack, current_shader)
621 GAMMA_COMPILE(shader_stack, current_shader, aggregate_interpolate)
623 COLORBALANCE_COMPILE(shader_stack,
625 aggregate_gamma || aggregate_interpolate)
627 shader = VFrame::make_shader(0,
640 glUseProgram(shader);
641 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
643 if(aggregate_interpolate) INTERPOLATE_UNIFORMS(shader);
644 if(aggregate_gamma) GAMMA_UNIFORMS(shader);
646 COLORBALANCE_UNIFORMS(shader);
650 get_output()->init_screen();
651 get_output()->bind_texture(0);
652 get_output()->draw_texture();
654 get_output()->set_opengl_state(VFrame::SCREEN);