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_to_rgb_f(r, g, b, y, u, v);
184 HISTOGRAM_HEAD(unsigned char)
185 y = (float)row[0] / 0xff;
186 u = (float)row[1] / 0xff;
187 v = (float)row[2] / 0xff;
188 YUV::yuv_to_rgb_f(r, g, b, y, u, v);
195 float max = plugin->config.max * plugin->config.gamma;
196 float scale = 1.0 / max;
197 float gamma = plugin->config.gamma - 1.0;
199 #define GAMMA_HEAD(type) \
200 for(int i = pkg->start; i < pkg->end; i++) \
202 type *row = (type*)data->get_rows()[i]; \
203 for(int j = 0; j < w; j++) \
206 // powf errors don't show up until later in the pipeline, which makes
207 // this very hard to isolate.
208 #define MY_POW(x, y) ((x > 0.0) ? powf(x * 2 / max, y) : 0.0)
211 r = r * scale * MY_POW(r, gamma); \
212 g = g * scale * MY_POW(g, gamma); \
213 b = b * scale * MY_POW(b, gamma); \
215 #define GAMMA_TAIL(components) \
221 switch(data->get_color_model())
224 GAMMA_HEAD(unsigned char)
225 r = (float)row[0] / 0xff;
226 g = (float)row[1] / 0xff;
227 b = (float)row[2] / 0xff;
229 row[0] = (int)CLIP(r * 0xff, 0, 0xff);
230 row[1] = (int)CLIP(g * 0xff, 0, 0xff);
231 row[2] = (int)CLIP(b * 0xff, 0, 0xff);
235 GAMMA_HEAD(unsigned char)
236 r = (float)row[0] / 0xff;
237 g = (float)row[1] / 0xff;
238 b = (float)row[2] / 0xff;
240 row[0] = (int)CLIP(r * 0xff, 0, 0xff);
241 row[1] = (int)CLIP(g * 0xff, 0, 0xff);
242 row[2] = (int)CLIP(b * 0xff, 0, 0xff);
268 GAMMA_HEAD(unsigned char)
273 u = (float)((u - 0x80) / 0xff);
274 v = (float)((v - 0x80) / 0xff);
275 YUV::yuv_to_rgb_f(r, g, b, y, u, v);
277 YUV::rgb_to_yuv_f(r, g, b, y, u, v);
281 row[0] = (int)CLIP(y, 0, 0xff);
282 row[1] = (int)CLIP(u, 0, 0xff);
283 row[2] = (int)CLIP(v, 0, 0xff);
287 GAMMA_HEAD(unsigned char)
292 u = (float)((u - 0x80) / 0xff);
293 v = (float)((v - 0x80) / 0xff);
294 YUV::yuv_to_rgb_f(r, g, b, y, u, v);
296 YUV::rgb_to_yuv_f(r, g, b, y, u, v);
300 row[0] = (int)CLIP(y, 0, 0xff);
301 row[1] = (int)CLIP(u, 0, 0xff);
302 row[2] = (int)CLIP(v, 0, 0xff);
319 GammaEngine::GammaEngine(GammaMain *plugin)
320 : LoadServer(plugin->get_project_smp() + 1,
321 plugin->get_project_smp() + 1)
323 this->plugin = plugin;
326 void GammaEngine::init_packages()
328 for(int i = 0; i < get_total_packages(); i++)
330 GammaPackage *package = (GammaPackage*)get_package(i);
331 package->start = data->get_h() * i / get_total_packages();
332 package->end = data->get_h() * (i + 1) / get_total_packages();
335 // Initialize clients here in case some don't get run.
336 for(int i = 0; i < get_total_clients(); i++)
338 GammaUnit *unit = (GammaUnit*)get_client(i);
339 bzero(unit->accum, sizeof(int) * HISTOGRAM_SIZE);
341 bzero(accum, sizeof(int) * HISTOGRAM_SIZE);
344 LoadClient* GammaEngine::new_client()
346 return new GammaUnit(plugin);
349 LoadPackage* GammaEngine::new_package()
351 return new GammaPackage;
354 void GammaEngine::process_packages(int operation, VFrame *data)
357 this->operation = operation;
358 LoadServer::process_packages();
359 for(int i = 0; i < get_total_clients(); i++)
361 GammaUnit *unit = (GammaUnit*)get_client(i);
362 for(int j = 0; j < HISTOGRAM_SIZE; j++)
364 accum[j] += unit->accum[j];
384 GammaMain::GammaMain(PluginServer *server)
385 : PluginVClient(server)
391 GammaMain::~GammaMain()
398 const char* GammaMain::plugin_title() { return _("Gamma"); }
399 int GammaMain::is_realtime() { return 1; }
405 NEW_WINDOW_MACRO(GammaMain, GammaWindow)
406 LOAD_CONFIGURATION_MACRO(GammaMain, GammaConfig)
412 int GammaMain::process_buffer(VFrame *frame,
413 int64_t start_position,
417 load_configuration();
419 frame->get_params()->update("GAMMA_GAMMA", config.gamma);
420 frame->get_params()->update("GAMMA_MAX", config.max);
422 int use_opengl = get_use_opengl() &&
424 (!config.plot || !gui_open());
435 if(next_effect_is(_("Histogram")))
437 if(next_effect_is(_("Color Balance")))
446 calculate_max(frame);
447 // Always plot to set the slider
448 send_render_gui(this);
453 send_render_gui(this);
456 if(!engine) engine = new GammaEngine(this);
457 engine->process_packages(GammaEngine::APPLY, frame);
461 void GammaMain::calculate_max(VFrame *frame)
463 if(!engine) engine = new GammaEngine(this);
464 engine->process_packages(GammaEngine::HISTOGRAM, frame);
465 int total_pixels = frame->get_w() * frame->get_h() * 3;
466 int max_fraction = (int)((int64_t)total_pixels * 99 / 100);
469 for(int i = 0; i < HISTOGRAM_SIZE; i++)
471 current += engine->accum[i];
472 if(current > max_fraction)
474 config.max = (float)i / HISTOGRAM_SIZE;
481 void GammaMain::update_gui()
485 if(load_configuration())
487 thread->window->lock_window("GammaMain::update_gui");
488 ((GammaWindow*)thread->window)->update();
489 thread->window->unlock_window();
494 void GammaMain::render_gui(void *data)
496 GammaMain *ptr = (GammaMain*)data;
497 config.max = ptr->config.max;
499 if(!engine) engine = new GammaEngine(this);
500 if(ptr->engine && ptr->config.automatic)
502 memcpy(engine->accum,
504 sizeof(int) * HISTOGRAM_SIZE);
505 thread->window->lock_window("GammaMain::render_gui");
506 ((GammaWindow*)thread->window)->update();
507 thread->window->unlock_window();
511 engine->process_packages(GammaEngine::HISTOGRAM,
513 thread->window->lock_window("GammaMain::render_gui");
514 ((GammaWindow*)thread->window)->update_histogram();
515 thread->window->unlock_window();
521 void GammaMain::save_data(KeyFrame *keyframe)
525 // cause data to be stored directly in text
526 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
527 output.tag.set_title("GAMMA");
528 output.tag.set_property("MAX", config.max);
529 output.tag.set_property("GAMMA", config.gamma);
530 output.tag.set_property("AUTOMATIC", config.automatic);
531 output.tag.set_property("PLOT", config.plot);
533 output.terminate_string();
536 void GammaMain::read_data(KeyFrame *keyframe)
540 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
546 result = input.read_tag();
550 if(input.tag.title_is("GAMMA"))
552 config.max = input.tag.get_property("MAX", config.max);
553 config.gamma = input.tag.get_property("GAMMA", config.gamma);
554 config.automatic = input.tag.get_property("AUTOMATIC", config.automatic);
555 config.plot = input.tag.get_property("PLOT", config.plot);
556 //printf("GammaMain::read_data %f\n", config.max);
562 int GammaMain::handle_opengl()
565 //printf("GammaMain::handle_opengl 1\n");
567 get_output()->to_texture();
568 get_output()->enable_opengl();
571 const char *shader_stack[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
572 int current_shader = 0;
575 // Aggregate with interpolate
577 if(prev_effect_is(_("Interpolate Pixels")))
580 INTERPOLATE_COMPILE(shader_stack, current_shader)
583 GAMMA_COMPILE(shader_stack, current_shader, aggregate);
585 unsigned int shader = VFrame::make_shader(0,
599 glUseProgram(shader);
600 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
604 INTERPOLATE_UNIFORMS(shader)
606 GAMMA_UNIFORMS(shader)
609 get_output()->init_screen();
610 get_output()->bind_texture(0);
611 get_output()->draw_texture();
613 get_output()->set_opengl_state(VFrame::SCREEN);