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
25 #include "colorbalance.h"
28 #include "playback3d.h"
30 #include "aggregated.h"
31 #include "../interpolate/aggregated.h"
32 #include "../gamma/aggregated.h"
37 // 1000 corresponds to (1.0 + MAX_COLOR) * input
40 REGISTER_PLUGIN(ColorBalanceMain)
44 ColorBalanceConfig::ColorBalanceConfig()
53 int ColorBalanceConfig::equivalent(ColorBalanceConfig &that)
55 return (cyan == that.cyan &&
56 magenta == that.magenta &&
57 yellow == that.yellow &&
58 lock_params == that.lock_params &&
59 preserve == that.preserve);
62 void ColorBalanceConfig::copy_from(ColorBalanceConfig &that)
65 magenta = that.magenta;
67 lock_params = that.lock_params;
68 preserve = that.preserve;
71 void ColorBalanceConfig::interpolate(ColorBalanceConfig &prev,
72 ColorBalanceConfig &next,
75 int64_t current_frame)
77 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
78 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
80 this->cyan = prev.cyan * prev_scale + next.cyan * next_scale;
81 this->magenta = prev.magenta * prev_scale + next.magenta * next_scale;
82 this->yellow = prev.yellow * prev_scale + next.yellow * next_scale;
83 this->preserve = prev.preserve;
84 this->lock_params = prev.lock_params;
96 ColorBalanceEngine::ColorBalanceEngine(ColorBalanceMain *plugin)
99 this->plugin = plugin;
104 ColorBalanceEngine::~ColorBalanceEngine()
112 int ColorBalanceEngine::start_process_frame(VFrame *output, VFrame *input, int row_start, int row_end)
114 this->output = output;
116 this->row_start = row_start;
117 this->row_end = row_end;
123 int ColorBalanceEngine::wait_process_frame()
125 output_lock.lock("ColorBalanceEngine::wait_process_frame");
129 void ColorBalanceEngine::run()
133 input_lock.lock("ColorBalanceEngine::run");
136 output_lock.unlock();
140 #define PROCESS(yuvtorgb, \
151 int y, cr, cb, r, g, b, r_n, g_n, b_n; \
152 float h, s, v, h_old, s_old, r_f, g_f, b_f; \
153 type **input_rows, **output_rows; \
154 input_rows = (type**)input->get_rows(); \
155 output_rows = (type**)output->get_rows(); \
157 for(j = row_start; j < row_end; j++) \
159 for(k = 0; k < input->get_w() * components; k += components) \
163 y = input_rows[j][k]; \
164 cb = input_rows[j][k + 1]; \
165 cr = input_rows[j][k + 2]; \
166 yuvtorgb(r, g, b, y, cb, cr); \
170 r = input_rows[j][k]; \
171 g = input_rows[j][k + 1]; \
172 b = input_rows[j][k + 2]; \
175 r_n = plugin->r_lookup[r]; \
176 g_n = plugin->g_lookup[g]; \
177 b_n = plugin->b_lookup[b]; \
179 if(plugin->config.preserve) \
181 HSV::rgb_to_hsv((float)r_n, (float)g_n, (float)b_n, h, s, v); \
182 HSV::rgb_to_hsv((float)r, (float)g, (float)b, h_old, s_old, v); \
183 HSV::hsv_to_rgb(r_f, g_f, b_f, h, s, v); \
197 rgbtoyuv(CLAMP(r, 0, max), CLAMP(g, 0, max), CLAMP(b, 0, max), y, cb, cr); \
198 output_rows[j][k] = y; \
199 output_rows[j][k + 1] = cb; \
200 output_rows[j][k + 2] = cr; \
204 output_rows[j][k] = CLAMP(r, 0, max); \
205 output_rows[j][k + 1] = CLAMP(g, 0, max); \
206 output_rows[j][k + 2] = CLAMP(b, 0, max); \
212 #define PROCESS_F(components) \
215 float r, g, b, r_n, g_n, b_n; \
216 float h, s, v, h_old, s_old, r_f, g_f, b_f; \
217 float **input_rows, **output_rows; \
218 input_rows = (float**)input->get_rows(); \
219 output_rows = (float**)output->get_rows(); \
220 cyan_f = plugin->calculate_transfer(plugin->config.cyan); \
221 magenta_f = plugin->calculate_transfer(plugin->config.magenta); \
222 yellow_f = plugin->calculate_transfer(plugin->config.yellow); \
224 printf("PROCESS_F %f\n", cyan_f); \
225 for(j = row_start; j < row_end; j++) \
227 for(k = 0; k < input->get_w() * components; k += components) \
229 r = input_rows[j][k]; \
230 g = input_rows[j][k + 1]; \
231 b = input_rows[j][k + 2]; \
234 g_n = g * magenta_f; \
235 b_n = b * yellow_f; \
237 if(plugin->config.preserve) \
239 HSV::rgb_to_hsv(r_n, g_n, b_n, h, s, v); \
240 HSV::rgb_to_hsv(r, g, b, h_old, s_old, v); \
241 HSV::hsv_to_rgb(r_f, g_f, b_f, h, s, v); \
253 output_rows[j][k] = r; \
254 output_rows[j][k + 1] = g; \
255 output_rows[j][k + 2] = b; \
260 switch(input->get_color_model())
263 PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
264 r_lookup_8, g_lookup_8, b_lookup_8,
265 unsigned char, 0xff, 3, 0);
273 PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
274 r_lookup_8, g_lookup_8, b_lookup_8,
275 unsigned char, 0xff, 3, 1);
283 PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
284 r_lookup_8, g_lookup_8, b_lookup_8,
285 unsigned char, 0xff, 4, 0);
289 PROCESS(YUV::yuv.yuv_to_rgb_8, YUV::yuv.rgb_to_yuv_8,
290 r_lookup_8, g_lookup_8, b_lookup_8,
291 unsigned char, 0xff, 4, 1);
295 PROCESS(YUV::yuv.yuv_to_rgb_16, YUV::yuv.rgb_to_yuv_16,
296 r_lookup_16, g_lookup_16, b_lookup_16,
297 u_int16_t, 0xffff, 3, 1);
300 case BC_YUVA16161616:
301 PROCESS(YUV::yuv.yuv_to_rgb_16, YUV::yuv.rgb_to_yuv_16,
302 r_lookup_16, g_lookup_16, b_lookup_16,
303 u_int16_t, 0xffff, 4, 1);
309 output_lock.unlock();
316 ColorBalanceMain::ColorBalanceMain(PluginServer *server)
317 : PluginVClient(server)
319 need_reconfigure = 1;
324 ColorBalanceMain::~ColorBalanceMain()
331 for(int i = 0; i < total_engines; i++)
339 const char* ColorBalanceMain::plugin_title() { return _("Color Balance"); }
340 int ColorBalanceMain::is_realtime() { return 1; }
343 int ColorBalanceMain::reconfigure()
345 float r_scale = calculate_transfer(config.cyan);
346 float g_scale = calculate_transfer(config.magenta);
347 float b_scale = calculate_transfer(config.yellow);
349 #define RECONFIGURE(r_lookup, g_lookup, b_lookup, max) \
350 for(int i = 0; i <= max; i++) \
352 r_lookup[i] = CLIP((int)(r_scale * i), 0, max); \
353 g_lookup[i] = CLIP((int)(g_scale * i), 0, max); \
354 b_lookup[i] = CLIP((int)(b_scale * i), 0, max); \
357 RECONFIGURE(r_lookup_8, g_lookup_8, b_lookup_8, 0xff);
358 RECONFIGURE(r_lookup_16, g_lookup_16, b_lookup_16, 0xffff);
363 int64_t ColorBalanceMain::calculate_slider(float in)
367 return (int64_t)(in * 1000 - 1000.0);
372 return (int64_t)(1000 * (in - 1.0) / MAX_COLOR);
378 float ColorBalanceMain::calculate_transfer(float in)
382 return (1000.0 + in) / 1000.0;
387 return 1.0 + in / 1000.0 * MAX_COLOR;
396 int ColorBalanceMain::test_boundary(float &value)
399 if(value < -1000) value = -1000;
400 if(value > 1000) value = 1000;
404 int ColorBalanceMain::synchronize_params(ColorBalanceSlider *slider, float difference)
406 if(thread && config.lock_params)
408 if(slider != ((ColorBalanceWindow*)thread->window)->cyan)
410 config.cyan += difference;
411 test_boundary(config.cyan);
412 ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
414 if(slider != ((ColorBalanceWindow*)thread->window)->magenta)
416 config.magenta += difference;
417 test_boundary(config.magenta);
418 ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
420 if(slider != ((ColorBalanceWindow*)thread->window)->yellow)
422 config.yellow += difference;
423 test_boundary(config.yellow);
424 ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
435 LOAD_CONFIGURATION_MACRO(ColorBalanceMain, ColorBalanceConfig)
436 NEW_WINDOW_MACRO(ColorBalanceMain, ColorBalanceWindow)
442 int ColorBalanceMain::process_buffer(VFrame *frame,
443 int64_t start_position,
446 need_reconfigure |= load_configuration();
448 //printf("ColorBalanceMain::process_realtime 1 %d\n", need_reconfigure);
453 total_engines = PluginClient::smp + 1;
454 engine = new ColorBalanceEngine*[total_engines];
455 for(int i = 0; i < total_engines; i++)
457 engine[i] = new ColorBalanceEngine(this);
463 need_reconfigure = 0;
466 frame->get_params()->update("COLORBALANCE_PRESERVE", config.preserve);
467 frame->get_params()->update("COLORBALANCE_CYAN", calculate_transfer(config.cyan));
468 frame->get_params()->update("COLORBALANCE_MAGENTA", calculate_transfer(config.magenta));
469 frame->get_params()->update("COLORBALANCE_YELLOW", calculate_transfer(config.yellow));
474 get_source_position(),
478 int aggregate_interpolate = 0;
479 int aggregate_gamma = 0;
480 get_aggregation(&aggregate_interpolate,
483 if(!EQUIV(config.cyan, 0) ||
484 !EQUIV(config.magenta, 0) ||
485 !EQUIV(config.yellow, 0) ||
487 (aggregate_interpolate ||
492 //get_output()->dump_stacks();
494 if(next_effect_is(_("Histogram"))) return 0;
498 for(int i = 0; i < total_engines; i++)
500 engine[i]->start_process_frame(frame,
502 frame->get_h() * i / total_engines,
503 frame->get_h() * (i + 1) / total_engines);
506 for(int i = 0; i < total_engines; i++)
508 engine[i]->wait_process_frame();
517 void ColorBalanceMain::update_gui()
521 load_configuration();
522 ((ColorBalanceWindow*)thread->window)->lock_window("ColorBalanceMain::update_gui");
523 ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
524 ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
525 ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
526 ((ColorBalanceWindow*)thread->window)->preserve->update(config.preserve);
527 ((ColorBalanceWindow*)thread->window)->lock_params->update(config.lock_params);
528 ((ColorBalanceWindow*)thread->window)->unlock_window();
535 void ColorBalanceMain::save_data(KeyFrame *keyframe)
539 // cause data to be stored directly in text
540 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
541 output.tag.set_title("COLORBALANCE");
542 output.tag.set_property("CYAN", config.cyan);
543 output.tag.set_property("MAGENTA", config.magenta);
544 output.tag.set_property("YELLOW", config.yellow);
545 output.tag.set_property("PRESERVELUMINOSITY", config.preserve);
546 output.tag.set_property("LOCKPARAMS", config.lock_params);
548 output.tag.set_title("/COLORBALANCE");
550 output.append_newline();
551 output.terminate_string();
554 void ColorBalanceMain::read_data(KeyFrame *keyframe)
558 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
564 result = input.read_tag();
568 if(input.tag.title_is("COLORBALANCE"))
570 config.cyan = input.tag.get_property("CYAN", config.cyan);
571 config.magenta = input.tag.get_property("MAGENTA", config.magenta);
572 config.yellow = input.tag.get_property("YELLOW", config.yellow);
573 config.preserve = input.tag.get_property("PRESERVELUMINOSITY", config.preserve);
574 config.lock_params = input.tag.get_property("LOCKPARAMS", config.lock_params);
580 void ColorBalanceMain::get_aggregation(int *aggregate_interpolate,
581 int *aggregate_gamma)
583 if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
584 !strcmp(get_output()->get_prev_effect(0), _("Gamma")))
586 *aggregate_interpolate = 1;
587 *aggregate_gamma = 1;
590 if(!strcmp(get_output()->get_prev_effect(0), _("Interpolate Pixels")))
592 *aggregate_interpolate = 1;
595 if(!strcmp(get_output()->get_prev_effect(0), _("Gamma")))
597 *aggregate_gamma = 1;
601 int ColorBalanceMain::handle_opengl()
605 get_output()->to_texture();
606 get_output()->enable_opengl();
608 unsigned int shader = 0;
609 const char *shader_stack[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
610 int current_shader = 0;
611 int aggregate_interpolate = 0;
612 int aggregate_gamma = 0;
614 get_aggregation(&aggregate_interpolate,
617 //printf("ColorBalanceMain::handle_opengl %d %d\n", aggregate_interpolate, aggregate_gamma);
618 if(aggregate_interpolate)
619 INTERPOLATE_COMPILE(shader_stack, current_shader)
622 GAMMA_COMPILE(shader_stack, current_shader, aggregate_interpolate)
624 COLORBALANCE_COMPILE(shader_stack,
626 aggregate_gamma || aggregate_interpolate)
628 shader = VFrame::make_shader(0,
641 glUseProgram(shader);
642 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
644 if(aggregate_interpolate) INTERPOLATE_UNIFORMS(shader);
645 if(aggregate_gamma) GAMMA_UNIFORMS(shader);
647 COLORBALANCE_UNIFORMS(shader);
651 get_output()->init_screen();
652 get_output()->bind_texture(0);
653 get_output()->draw_texture();
655 get_output()->set_opengl_state(VFrame::SCREEN);