3 #include "brightness.h"
12 #define _(String) gettext(String)
13 #define gettext_noop(String) String
14 #define N_(String) gettext_noop (String)
16 #define SQR(a) ((a) * (a))
18 REGISTER_PLUGIN(BrightnessMain)
22 BrightnessConfig::BrightnessConfig()
29 int BrightnessConfig::equivalent(BrightnessConfig &that)
31 return (brightness == that.brightness &&
32 contrast == that.contrast &&
36 void BrightnessConfig::copy_from(BrightnessConfig &that)
38 brightness = that.brightness;
39 contrast = that.contrast;
43 void BrightnessConfig::interpolate(BrightnessConfig &prev,
44 BrightnessConfig &next,
47 int64_t current_frame)
49 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
50 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
52 this->brightness = prev.brightness * prev_scale + next.brightness * next_scale;
53 this->contrast = prev.contrast * prev_scale + next.contrast * next_scale;
54 this->luma = (int)(prev.luma * prev_scale + next.luma * next_scale);
65 YUV BrightnessMain::yuv;
67 BrightnessMain::BrightnessMain(PluginServer *server)
68 : PluginVClient(server)
72 PLUGIN_CONSTRUCTOR_MACRO
75 BrightnessMain::~BrightnessMain()
77 PLUGIN_DESTRUCTOR_MACRO
78 if(engine) delete engine;
81 char* BrightnessMain::plugin_title() { return _("Brightness/Contrast"); }
82 int BrightnessMain::is_realtime() { return 1; }
84 NEW_PICON_MACRO(BrightnessMain)
85 SHOW_GUI_MACRO(BrightnessMain, BrightnessThread)
86 RAISE_WINDOW_MACRO(BrightnessMain)
87 SET_STRING_MACRO(BrightnessMain)
88 LOAD_CONFIGURATION_MACRO(BrightnessMain, BrightnessConfig)
90 int BrightnessMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
93 if(!engine) engine = new BrightnessEngine(this, PluginClient::smp + 1);
95 this->input = input_ptr;
96 this->output = output_ptr;
98 if(!EQUIV(config.brightness, 0) || !EQUIV(config.contrast, 0))
100 engine->process_packages();
103 // Data never processed so copy if necessary
104 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
106 output_ptr->copy_from(input_ptr);
114 void BrightnessMain::update_gui()
118 load_configuration();
119 thread->window->lock_window();
120 thread->window->brightness->update(config.brightness);
121 thread->window->contrast->update(config.contrast);
122 thread->window->luma->update(config.luma);
123 thread->window->unlock_window();
127 int BrightnessMain::load_defaults()
129 char directory[1024], string[1024];
130 // set the default directory
131 sprintf(directory, "%sbrightness.rc", BCASTDIR);
134 defaults = new Defaults(directory);
137 config.brightness = defaults->get("BRIGHTNESS", config.brightness);
138 config.contrast = defaults->get("CONTRAST", config.contrast);
139 config.luma = defaults->get("LUMA", config.luma);
143 int BrightnessMain::save_defaults()
145 defaults->update("BRIGHTNESS", config.brightness);
146 defaults->update("CONTRAST", config.contrast);
147 defaults->update("LUMA", config.luma);
153 void BrightnessMain::save_data(KeyFrame *keyframe)
157 // cause data to be stored directly in text
158 output.set_shared_string(keyframe->data, MESSAGESIZE);
159 output.tag.set_title("BRIGHTNESS");
160 output.tag.set_property("BRIGHTNESS", config.brightness);
161 output.tag.set_property("CONTRAST", config.contrast);
162 output.tag.set_property("LUMA", config.luma);
163 //printf("BrightnessMain::save_data %d\n", config.luma);
165 output.terminate_string();
168 void BrightnessMain::read_data(KeyFrame *keyframe)
172 input.set_shared_string(keyframe->data, strlen(keyframe->data));
178 result = input.read_tag();
182 if(input.tag.title_is("BRIGHTNESS"))
184 config.brightness = input.tag.get_property("BRIGHTNESS", config.brightness);
185 config.contrast = input.tag.get_property("CONTRAST", config.contrast);
186 config.luma = input.tag.get_property("LUMA", config.luma);
204 BrightnessPackage::BrightnessPackage()
212 BrightnessUnit::BrightnessUnit(BrightnessEngine *server, BrightnessMain *plugin)
215 this->plugin = plugin;
218 BrightnessUnit::~BrightnessUnit()
222 void BrightnessUnit::process_package(LoadPackage *package)
224 BrightnessPackage *pkg = (BrightnessPackage*)package;
227 VFrame *output = plugin->output;
228 VFrame *input = plugin->input;
234 #define DO_BRIGHTNESS(max, type, components, is_yuv) \
236 type **input_rows = (type**)input->get_rows(); \
237 type **output_rows = (type**)output->get_rows(); \
238 int row1 = pkg->row1; \
239 int row2 = pkg->row2; \
240 int width = output->get_w(); \
243 if(!EQUIV(plugin->config.brightness, 0)) \
245 int offset = (int)(plugin->config.brightness / 100 * max); \
246 /*printf("DO_BRIGHTNESS offset=%d\n", offset);*/ \
248 for(int i = row1; i < row2; i++) \
250 type *input_row = input_rows[i]; \
251 type *output_row = output_rows[i]; \
253 for(int j = 0; j < width; j++) \
255 r = input_row[j * components] + offset; \
259 g = input_row[j * components + 1] + offset; \
260 b = input_row[j * components + 2] + offset; \
270 output_row[j * components] = r; \
274 output_row[j * components + 1] = g; \
275 output_row[j * components + 2] = b; \
279 output_row[j * components + 1] = input_row[j * components + 1]; \
280 output_row[j * components + 2] = input_row[j * components + 2]; \
283 if(components == 4) \
284 output_row[j * components + 3] = input_row[j * components + 3]; \
288 /* Data to be processed is now in the output buffer */ \
289 input_rows = output_rows; \
292 if(!EQUIV(plugin->config.contrast, 0)) \
294 float contrast = (plugin->config.contrast < 0) ? \
295 (plugin->config.contrast + 100) / 100 : \
296 (plugin->config.contrast + 25) / 25; \
297 /*printf("DO_BRIGHTNESS contrast=%f\n", contrast);*/ \
299 int scalar = (int)(contrast * 0x100); \
300 int offset = (max << 8) / 2 - max * scalar / 2; \
303 for(int i = row1; i < row2; i++) \
305 type *input_row = input_rows[i]; \
306 type *output_row = output_rows[i]; \
308 if(plugin->config.luma) \
310 for(int j = 0; j < width; j++) \
314 y = input_row[j * components]; \
318 r = input_row[j * components]; \
319 g = input_row[j * components + 1]; \
320 b = input_row[j * components + 2]; \
323 BrightnessMain::yuv.rgb_to_yuv_8( \
333 BrightnessMain::yuv.rgb_to_yuv_16( \
344 y = (y * scalar + offset) >> 8; \
350 output_row[j * components] = y; \
351 output_row[j * components + 1] = input_row[j * components + 1]; \
352 output_row[j * components + 2] = input_row[j * components + 2]; \
358 BrightnessMain::yuv.yuv_to_rgb_8( \
368 BrightnessMain::yuv.yuv_to_rgb_16( \
376 input_row[j * components] = r; \
377 input_row[j * components + 1] = g; \
378 input_row[j * components + 2] = b; \
381 if(components == 4) \
382 output_row[j * components + 3] = input_row[j * components + 3]; \
387 for(int j = 0; j < width; j++) \
389 r = input_row[j * components]; \
390 g = input_row[j * components + 1]; \
391 b = input_row[j * components + 2]; \
393 r = (r * scalar + offset) >> 8; \
394 g = (g * scalar + offset) >> 8; \
395 b = (b * scalar + offset) >> 8; \
401 output_row[j * components] = r; \
402 output_row[j * components + 1] = g; \
403 output_row[j * components + 2] = b; \
405 if(components == 4) \
406 output_row[j * components + 3] = input_row[j * components + 3]; \
414 switch(input->get_color_model())
417 DO_BRIGHTNESS(0xff, unsigned char, 3, 0)
421 DO_BRIGHTNESS(0xff, unsigned char, 3, 1)
425 DO_BRIGHTNESS(0xff, unsigned char, 4, 0)
429 DO_BRIGHTNESS(0xff, unsigned char, 4, 1)
433 DO_BRIGHTNESS(0xffff, uint16_t, 3, 0)
437 DO_BRIGHTNESS(0xffff, uint16_t, 3, 1)
440 case BC_RGBA16161616:
441 DO_BRIGHTNESS(0xffff, uint16_t, 4, 0)
444 case BC_YUVA16161616:
445 DO_BRIGHTNESS(0xffff, uint16_t, 4, 1)
464 BrightnessEngine::BrightnessEngine(BrightnessMain *plugin, int cpus)
465 : LoadServer(cpus, cpus)
467 this->plugin = plugin;
470 BrightnessEngine::~BrightnessEngine()
475 void BrightnessEngine::init_packages()
477 for(int i = 0; i < LoadServer::total_packages; i++)
479 BrightnessPackage *package = (BrightnessPackage*)LoadServer::packages[i];
480 package->row1 = (int)(plugin->input->get_h() /
481 LoadServer::total_packages *
483 package->row2 = package->row1 +
484 (int)(plugin->input->get_h() /
485 LoadServer::total_packages);
487 if(i >= LoadServer::total_packages - 1)
488 package->row2 = plugin->input->get_h();
492 LoadClient* BrightnessEngine::new_client()
494 return new BrightnessUnit(this, plugin);
497 LoadPackage* BrightnessEngine::new_package()
499 return new BrightnessPackage;