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
22 #include "bcsignals.h"
29 #include "../interpolate/aggregated.h"
30 #include "playback3d.h"
31 #include "workarounds.h"
37 #include "aggregated.h"
40 REGISTER_PLUGIN(GammaMain)
44 GammaConfig::GammaConfig()
52 int GammaConfig::equivalent(GammaConfig &that)
54 return (EQUIV(max, that.max) &&
55 EQUIV(gamma, that.gamma) &&
56 automatic == that.automatic) &&
60 void GammaConfig::copy_from(GammaConfig &that)
64 automatic = that.automatic;
68 void GammaConfig::interpolate(GammaConfig &prev,
72 int64_t current_frame)
74 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
75 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
77 this->max = prev.max * prev_scale + next.max * next_scale;
78 this->gamma = prev.gamma * prev_scale + next.gamma * next_scale;
79 this->automatic = prev.automatic;
80 this->plot = prev.plot;
90 GammaPackage::GammaPackage()
105 GammaUnit::GammaUnit(GammaMain *plugin)
107 this->plugin = plugin;
111 void GammaUnit::process_package(LoadPackage *package)
113 GammaPackage *pkg = (GammaPackage*)package;
114 GammaEngine *engine = (GammaEngine*)get_server();
115 VFrame *data = engine->data;
116 int w = data->get_w();
117 float r, g, b, y, u, v;
119 // The same algorithm used by dcraw
120 if(engine->operation == GammaEngine::HISTOGRAM)
122 #define HISTOGRAM_HEAD(type) \
123 for(int i = pkg->start; i < pkg->end; i++) \
125 type *row = (type*)data->get_rows()[i]; \
126 for(int j = 0; j < w; j++) \
129 #define HISTOGRAM_TAIL(components) \
131 slot = (int)(r * HISTOGRAM_SIZE); \
132 accum[CLIP(slot, 0, HISTOGRAM_SIZE - 1)]++; \
133 slot = (int)(g * HISTOGRAM_SIZE); \
134 accum[CLIP(slot, 0, HISTOGRAM_SIZE - 1)]++; \
135 slot = (int)(b * HISTOGRAM_SIZE); \
136 accum[CLIP(slot, 0, HISTOGRAM_SIZE - 1)]++; \
142 switch(data->get_color_model())
145 HISTOGRAM_HEAD(unsigned char)
146 r = (float)row[0] / 0xff;
147 g = (float)row[1] / 0xff;
148 b = (float)row[2] / 0xff;
152 HISTOGRAM_HEAD(unsigned char)
153 r = (float)row[0] / 0xff;
154 g = (float)row[1] / 0xff;
155 b = (float)row[2] / 0xff;
159 HISTOGRAM_HEAD(float)
166 HISTOGRAM_HEAD(float)
173 HISTOGRAM_HEAD(unsigned char)
178 u = (float)((u - 0x80) / 0xff);
179 v = (float)((v - 0x80) / 0xff);
180 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
184 HISTOGRAM_HEAD(unsigned char)
189 u = (float)((u - 0x80) / 0xff);
190 v = (float)((v - 0x80) / 0xff);
191 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
198 float max = plugin->config.max * plugin->config.gamma;
199 float scale = 1.0 / max;
200 float gamma = plugin->config.gamma - 1.0;
202 #define GAMMA_HEAD(type) \
203 for(int i = pkg->start; i < pkg->end; i++) \
205 type *row = (type*)data->get_rows()[i]; \
206 for(int j = 0; j < w; j++) \
209 // powf errors don't show up until later in the pipeline, which makes
210 // this very hard to isolate.
211 #define MY_POW(x, y) ((x > 0.0) ? powf(x * 2 / max, y) : 0.0)
214 r = r * scale * MY_POW(r, gamma); \
215 g = g * scale * MY_POW(g, gamma); \
216 b = b * scale * MY_POW(b, gamma); \
218 #define GAMMA_TAIL(components) \
224 switch(data->get_color_model())
227 GAMMA_HEAD(unsigned char)
228 r = (float)row[0] / 0xff;
229 g = (float)row[1] / 0xff;
230 b = (float)row[2] / 0xff;
232 row[0] = (int)CLIP(r * 0xff, 0, 0xff);
233 row[1] = (int)CLIP(g * 0xff, 0, 0xff);
234 row[2] = (int)CLIP(b * 0xff, 0, 0xff);
238 GAMMA_HEAD(unsigned char)
239 r = (float)row[0] / 0xff;
240 g = (float)row[1] / 0xff;
241 b = (float)row[2] / 0xff;
243 row[0] = (int)CLIP(r * 0xff, 0, 0xff);
244 row[1] = (int)CLIP(g * 0xff, 0, 0xff);
245 row[2] = (int)CLIP(b * 0xff, 0, 0xff);
271 GAMMA_HEAD(unsigned char)
276 u = (float)((u - 0x80) / 0xff);
277 v = (float)((v - 0x80) / 0xff);
278 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
280 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
284 row[0] = (int)CLIP(y, 0, 0xff);
285 row[1] = (int)CLIP(u, 0, 0xff);
286 row[2] = (int)CLIP(v, 0, 0xff);
290 GAMMA_HEAD(unsigned char)
295 u = (float)((u - 0x80) / 0xff);
296 v = (float)((v - 0x80) / 0xff);
297 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
299 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
303 row[0] = (int)CLIP(y, 0, 0xff);
304 row[1] = (int)CLIP(u, 0, 0xff);
305 row[2] = (int)CLIP(v, 0, 0xff);
322 GammaEngine::GammaEngine(GammaMain *plugin)
323 : LoadServer(plugin->get_project_smp() + 1,
324 plugin->get_project_smp() + 1)
326 this->plugin = plugin;
329 void GammaEngine::init_packages()
331 for(int i = 0; i < get_total_packages(); i++)
333 GammaPackage *package = (GammaPackage*)get_package(i);
334 package->start = data->get_h() * i / get_total_packages();
335 package->end = data->get_h() * (i + 1) / get_total_packages();
338 // Initialize clients here in case some don't get run.
339 for(int i = 0; i < get_total_clients(); i++)
341 GammaUnit *unit = (GammaUnit*)get_client(i);
342 bzero(unit->accum, sizeof(int) * HISTOGRAM_SIZE);
344 bzero(accum, sizeof(int) * HISTOGRAM_SIZE);
347 LoadClient* GammaEngine::new_client()
349 return new GammaUnit(plugin);
352 LoadPackage* GammaEngine::new_package()
354 return new GammaPackage;
357 void GammaEngine::process_packages(int operation, VFrame *data)
360 this->operation = operation;
361 LoadServer::process_packages();
362 for(int i = 0; i < get_total_clients(); i++)
364 GammaUnit *unit = (GammaUnit*)get_client(i);
365 for(int j = 0; j < HISTOGRAM_SIZE; j++)
367 accum[j] += unit->accum[j];
387 GammaMain::GammaMain(PluginServer *server)
388 : PluginVClient(server)
394 GammaMain::~GammaMain()
401 const char* GammaMain::plugin_title() { return N_("Gamma"); }
402 int GammaMain::is_realtime() { return 1; }
408 NEW_WINDOW_MACRO(GammaMain, GammaWindow)
409 LOAD_CONFIGURATION_MACRO(GammaMain, GammaConfig)
415 int GammaMain::process_buffer(VFrame *frame,
416 int64_t start_position,
420 load_configuration();
422 frame->get_params()->update("GAMMA_GAMMA", config.gamma);
423 frame->get_params()->update("GAMMA_MAX", config.max);
425 int use_opengl = get_use_opengl() &&
427 (!config.plot || !gui_open());
438 if(next_effect_is(_("Histogram")))
440 if(next_effect_is(_("Color Balance")))
449 calculate_max(frame);
450 // Always plot to set the slider
451 send_render_gui(this);
456 send_render_gui(this);
459 if(!engine) engine = new GammaEngine(this);
460 engine->process_packages(GammaEngine::APPLY, frame);
464 void GammaMain::calculate_max(VFrame *frame)
466 if(!engine) engine = new GammaEngine(this);
467 engine->process_packages(GammaEngine::HISTOGRAM, frame);
468 int total_pixels = frame->get_w() * frame->get_h() * 3;
469 int max_fraction = (int)((int64_t)total_pixels * 99 / 100);
472 for(int i = 0; i < HISTOGRAM_SIZE; i++)
474 current += engine->accum[i];
475 if(current > max_fraction)
477 config.max = (float)i / HISTOGRAM_SIZE;
484 void GammaMain::update_gui()
488 if(load_configuration())
490 thread->window->lock_window("GammaMain::update_gui");
491 ((GammaWindow*)thread->window)->update();
492 thread->window->unlock_window();
497 void GammaMain::render_gui(void *data)
499 GammaMain *ptr = (GammaMain*)data;
500 config.max = ptr->config.max;
502 if(!engine) engine = new GammaEngine(this);
503 if(ptr->engine && ptr->config.automatic)
505 memcpy(engine->accum,
507 sizeof(int) * HISTOGRAM_SIZE);
508 thread->window->lock_window("GammaMain::render_gui");
509 ((GammaWindow*)thread->window)->update();
510 thread->window->unlock_window();
514 engine->process_packages(GammaEngine::HISTOGRAM,
516 thread->window->lock_window("GammaMain::render_gui");
517 ((GammaWindow*)thread->window)->update_histogram();
518 thread->window->unlock_window();
524 void GammaMain::save_data(KeyFrame *keyframe)
528 // cause data to be stored directly in text
529 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
530 output.tag.set_title("GAMMA");
531 output.tag.set_property("MAX", config.max);
532 output.tag.set_property("GAMMA", config.gamma);
533 output.tag.set_property("AUTOMATIC", config.automatic);
534 output.tag.set_property("PLOT", config.plot);
536 output.tag.set_title("/GAMMA");
538 output.append_newline();
539 output.terminate_string();
542 void GammaMain::read_data(KeyFrame *keyframe)
546 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
552 result = input.read_tag();
556 if(input.tag.title_is("GAMMA"))
558 config.max = input.tag.get_property("MAX", config.max);
559 config.gamma = input.tag.get_property("GAMMA", config.gamma);
560 config.automatic = input.tag.get_property("AUTOMATIC", config.automatic);
561 config.plot = input.tag.get_property("PLOT", config.plot);
562 //printf("GammaMain::read_data %f\n", config.max);
568 int GammaMain::handle_opengl()
571 //printf("GammaMain::handle_opengl 1\n");
573 get_output()->to_texture();
574 get_output()->enable_opengl();
576 const char *shader_stack[16];
577 memset(shader_stack,0, sizeof(shader_stack));
578 int current_shader = 0;
580 int need_color_matrix = BC_CModels::is_yuv(get_output()->get_color_model()) ? 1 : 0;
581 if( need_color_matrix )
582 shader_stack[current_shader++] = bc_gl_colors;
584 // Aggregate with interpolate
585 int aggregate = prev_effect_is(_("Interpolate Pixels")) ? 1 : 0;
587 INTERPOLATE_COMPILE(shader_stack, current_shader);
589 GAMMA_COMPILE(shader_stack, current_shader, aggregate);
591 shader_stack[current_shader] = 0;
592 unsigned int shader = VFrame::make_shader(shader_stack);
594 glUseProgram(shader);
595 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
597 INTERPOLATE_UNIFORMS(shader);
598 GAMMA_UNIFORMS(shader);
599 if( need_color_matrix ) BC_GL_COLORS(shader);
602 get_output()->init_screen();
603 get_output()->bind_texture(0);
604 get_output()->draw_texture();
606 get_output()->set_opengl_state(VFrame::SCREEN);