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 N_("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) {
407 if(slider != ((ColorBalanceWindow*)thread->window)->cyan) {
408 config.cyan += difference;
409 test_boundary(config.cyan);
410 ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
412 if(slider != ((ColorBalanceWindow*)thread->window)->magenta) {
413 config.magenta += difference;
414 test_boundary(config.magenta);
415 ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
417 if(slider != ((ColorBalanceWindow*)thread->window)->yellow) {
418 config.yellow += difference;
419 test_boundary(config.yellow);
420 ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
431 LOAD_CONFIGURATION_MACRO(ColorBalanceMain, ColorBalanceConfig)
432 NEW_WINDOW_MACRO(ColorBalanceMain, ColorBalanceWindow)
438 int ColorBalanceMain::process_buffer(VFrame *frame,
439 int64_t start_position,
442 need_reconfigure |= load_configuration();
444 //printf("ColorBalanceMain::process_realtime 1 %d\n", need_reconfigure);
449 total_engines = PluginClient::smp + 1;
450 engine = new ColorBalanceEngine*[total_engines];
451 for(int i = 0; i < total_engines; i++)
453 engine[i] = new ColorBalanceEngine(this);
459 need_reconfigure = 0;
462 frame->get_params()->update("COLORBALANCE_PRESERVE", config.preserve);
463 frame->get_params()->update("COLORBALANCE_CYAN", calculate_transfer(config.cyan));
464 frame->get_params()->update("COLORBALANCE_MAGENTA", calculate_transfer(config.magenta));
465 frame->get_params()->update("COLORBALANCE_YELLOW", calculate_transfer(config.yellow));
470 get_source_position(),
474 int aggregate_interpolate = 0;
475 int aggregate_gamma = 0;
476 get_aggregation(&aggregate_interpolate,
479 if(!EQUIV(config.cyan, 0) ||
480 !EQUIV(config.magenta, 0) ||
481 !EQUIV(config.yellow, 0) ||
483 (aggregate_interpolate ||
488 //get_output()->dump_stacks();
490 if(next_effect_is(_("Histogram"))) return 0;
494 for(int i = 0; i < total_engines; i++)
496 engine[i]->start_process_frame(frame,
498 frame->get_h() * i / total_engines,
499 frame->get_h() * (i + 1) / total_engines);
502 for(int i = 0; i < total_engines; i++)
504 engine[i]->wait_process_frame();
513 void ColorBalanceMain::update_gui()
517 load_configuration();
518 ((ColorBalanceWindow*)thread->window)->lock_window("ColorBalanceMain::update_gui");
519 ((ColorBalanceWindow*)thread->window)->cyan->update((int64_t)config.cyan);
520 ((ColorBalanceWindow*)thread->window)->magenta->update((int64_t)config.magenta);
521 ((ColorBalanceWindow*)thread->window)->yellow->update((int64_t)config.yellow);
522 ((ColorBalanceWindow*)thread->window)->preserve->update(config.preserve);
523 ((ColorBalanceWindow*)thread->window)->lock_params->update(config.lock_params);
524 ((ColorBalanceWindow*)thread->window)->unlock_window();
531 void ColorBalanceMain::save_data(KeyFrame *keyframe)
535 // cause data to be stored directly in text
536 output.set_shared_output(keyframe->xbuf);
537 output.tag.set_title("COLORBALANCE");
538 output.tag.set_property("CYAN", config.cyan);
539 output.tag.set_property("MAGENTA", config.magenta);
540 output.tag.set_property("YELLOW", config.yellow);
541 output.tag.set_property("PRESERVELUMINOSITY", config.preserve);
542 output.tag.set_property("LOCKPARAMS", config.lock_params);
544 output.tag.set_title("/COLORBALANCE");
546 output.append_newline();
547 output.terminate_string();
550 void ColorBalanceMain::read_data(KeyFrame *keyframe)
554 input.set_shared_input(keyframe->xbuf);
560 result = input.read_tag();
564 if(input.tag.title_is("COLORBALANCE"))
566 config.cyan = input.tag.get_property("CYAN", config.cyan);
567 config.magenta = input.tag.get_property("MAGENTA", config.magenta);
568 config.yellow = input.tag.get_property("YELLOW", config.yellow);
569 config.preserve = input.tag.get_property("PRESERVELUMINOSITY", config.preserve);
570 config.lock_params = input.tag.get_property("LOCKPARAMS", config.lock_params);
576 void ColorBalanceMain::get_aggregation(int *aggregate_interpolate,
577 int *aggregate_gamma)
579 if(!strcmp(get_output()->get_prev_effect(1), _("Interpolate Pixels")) &&
580 !strcmp(get_output()->get_prev_effect(0), _("Gamma")))
582 *aggregate_interpolate = 1;
583 *aggregate_gamma = 1;
586 if(!strcmp(get_output()->get_prev_effect(0), _("Interpolate Pixels")))
588 *aggregate_interpolate = 1;
591 if(!strcmp(get_output()->get_prev_effect(0), _("Gamma")))
593 *aggregate_gamma = 1;
597 int ColorBalanceMain::handle_opengl()
601 get_output()->to_texture();
602 get_output()->enable_opengl();
604 const char *shader_stack[16];
605 memset(shader_stack,0, sizeof(shader_stack));
606 int current_shader = 0;
608 int need_color_matrix = BC_CModels::is_yuv(get_output()->get_color_model()) ? 1 : 0;
609 if( need_color_matrix )
610 shader_stack[current_shader++] = bc_gl_colors;
612 int aggregate_interpolate = 0;
613 int aggregate_gamma = 0;
615 get_aggregation(&aggregate_interpolate,
618 //printf("ColorBalanceMain::handle_opengl %d %d\n", aggregate_interpolate, aggregate_gamma);
619 if(aggregate_interpolate)
620 INTERPOLATE_COMPILE(shader_stack, current_shader);
623 GAMMA_COMPILE(shader_stack, current_shader,
624 aggregate_interpolate);
626 COLORBALANCE_COMPILE(shader_stack, current_shader,
627 aggregate_gamma || aggregate_interpolate);
629 shader_stack[current_shader] = 0;
630 unsigned int shader = VFrame::make_shader(shader_stack);
632 glUseProgram(shader);
633 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
635 if(aggregate_interpolate) INTERPOLATE_UNIFORMS(shader);
636 if(aggregate_gamma) GAMMA_UNIFORMS(shader);
638 COLORBALANCE_UNIFORMS(shader);
639 if( need_color_matrix ) BC_GL_COLORS(shader);
642 get_output()->init_screen();
643 get_output()->bind_texture(0);
644 get_output()->draw_texture();
646 get_output()->set_opengl_state(VFrame::SCREEN);