00001 #include "bcdisplayinfo.h"
00002 #include "clip.h"
00003 #include "bchash.h"
00004 #include "filexml.h"
00005 #include "guicast.h"
00006 #include "language.h"
00007 #include "loadbalance.h"
00008 #include "picon_png.h"
00009 #include "plugincolors.h"
00010 #include "playback3d.h"
00011 #include "pluginvclient.h"
00012 #include "vframe.h"
00013
00014 #include <stdint.h>
00015 #include <string.h>
00016
00017
00018 class HueEffect;
00019
00020 #define MINHUE -180
00021 #define MAXHUE 180
00022 #define MINSATURATION -100
00023 #define MAXSATURATION 100
00024 #define MINVALUE -100
00025 #define MAXVALUE 100
00026
00027
00028
00029
00030
00031
00032 class HueConfig
00033 {
00034 public:
00035 HueConfig();
00036
00037 void copy_from(HueConfig &src);
00038 int equivalent(HueConfig &src);
00039 void interpolate(HueConfig &prev,
00040 HueConfig &next,
00041 long prev_frame,
00042 long next_frame,
00043 long current_frame);
00044 float hue, saturation, value;
00045 };
00046
00047 class HueSlider : public BC_FSlider
00048 {
00049 public:
00050 HueSlider(HueEffect *plugin, int x, int y, int w);
00051 int handle_event();
00052 HueEffect *plugin;
00053 };
00054
00055 class SaturationSlider : public BC_FSlider
00056 {
00057 public:
00058 SaturationSlider(HueEffect *plugin, int x, int y, int w);
00059 int handle_event();
00060 char* get_caption();
00061 HueEffect *plugin;
00062 char string[BCTEXTLEN];
00063 };
00064
00065 class ValueSlider : public BC_FSlider
00066 {
00067 public:
00068 ValueSlider(HueEffect *plugin, int x, int y, int w);
00069 int handle_event();
00070 char* get_caption();
00071 HueEffect *plugin;
00072 char string[BCTEXTLEN];
00073 };
00074
00075 class HueWindow : public BC_Window
00076 {
00077 public:
00078 HueWindow(HueEffect *plugin, int x, int y);
00079 void create_objects();
00080 int close_event();
00081 HueEffect *plugin;
00082 HueSlider *hue;
00083 SaturationSlider *saturation;
00084 ValueSlider *value;
00085 };
00086
00087 PLUGIN_THREAD_HEADER(HueEffect, HueThread, HueWindow)
00088
00089 class HueEngine : public LoadServer
00090 {
00091 public:
00092 HueEngine(HueEffect *plugin, int cpus);
00093 void init_packages();
00094 LoadClient* new_client();
00095 LoadPackage* new_package();
00096 HueEffect *plugin;
00097 };
00098
00099 class HuePackage : public LoadPackage
00100 {
00101 public:
00102 HuePackage();
00103 int row1, row2;
00104 };
00105
00106 class HueUnit : public LoadClient
00107 {
00108 public:
00109 HueUnit(HueEffect *plugin, HueEngine *server);
00110 void process_package(LoadPackage *package);
00111 HueEffect *plugin;
00112 YUV yuv;
00113 };
00114
00115 class HueEffect : public PluginVClient
00116 {
00117 public:
00118 HueEffect(PluginServer *server);
00119 ~HueEffect();
00120
00121 int process_buffer(VFrame *frame,
00122 int64_t start_position,
00123 double frame_rate);
00124 int is_realtime();
00125 char* plugin_title();
00126 VFrame* new_picon();
00127 int load_configuration();
00128 int load_defaults();
00129 int save_defaults();
00130 void save_data(KeyFrame *keyframe);
00131 void read_data(KeyFrame *keyframe);
00132 int show_gui();
00133 int set_string();
00134 void raise_window();
00135 void update_gui();
00136 int handle_opengl();
00137
00138 HueConfig config;
00139 VFrame *input, *output;
00140 BC_Hash *defaults;
00141 HueThread *thread;
00142 HueEngine *engine;
00143 };
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 HueConfig::HueConfig()
00165 {
00166 hue = saturation = value = 0;
00167 }
00168
00169 void HueConfig::copy_from(HueConfig &src)
00170 {
00171 hue = src.hue;
00172 saturation = src.saturation;
00173 value = src.value;
00174 }
00175 int HueConfig::equivalent(HueConfig &src)
00176 {
00177 return EQUIV(hue, src.hue) &&
00178 EQUIV(saturation, src.saturation) &&
00179 EQUIV(value, src.value);
00180 }
00181 void HueConfig::interpolate(HueConfig &prev,
00182 HueConfig &next,
00183 long prev_frame,
00184 long next_frame,
00185 long current_frame)
00186 {
00187 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
00188 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
00189
00190 this->hue = prev.hue * prev_scale + next.hue * next_scale;
00191 this->saturation = prev.saturation * prev_scale + next.saturation * next_scale;
00192 this->value = prev.value * prev_scale + next.value * next_scale;
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202 HueSlider::HueSlider(HueEffect *plugin, int x, int y, int w)
00203 : BC_FSlider(x,
00204 y,
00205 0,
00206 w,
00207 w,
00208 (float)MINHUE,
00209 (float)MAXHUE,
00210 plugin->config.hue)
00211 {
00212 this->plugin = plugin;
00213 }
00214 int HueSlider::handle_event()
00215 {
00216 plugin->config.hue = get_value();
00217 plugin->send_configure_change();
00218 return 1;
00219 }
00220
00221
00222
00223
00224
00225
00226
00227 SaturationSlider::SaturationSlider(HueEffect *plugin, int x, int y, int w)
00228 : BC_FSlider(x,
00229 y,
00230 0,
00231 w,
00232 w,
00233 (float)MINSATURATION,
00234 (float)MAXSATURATION,
00235 plugin->config.saturation)
00236 {
00237 this->plugin = plugin;
00238 }
00239 int SaturationSlider::handle_event()
00240 {
00241 plugin->config.saturation = get_value();
00242 plugin->send_configure_change();
00243 return 1;
00244 }
00245
00246 char* SaturationSlider::get_caption()
00247 {
00248 float fraction = ((float)plugin->config.saturation - MINSATURATION) /
00249 MAXSATURATION;;
00250 sprintf(string, "%0.4f", fraction);
00251 return string;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260 ValueSlider::ValueSlider(HueEffect *plugin, int x, int y, int w)
00261 : BC_FSlider(x,
00262 y,
00263 0,
00264 w,
00265 w,
00266 (float)MINVALUE,
00267 (float)MAXVALUE,
00268 plugin->config.value)
00269 {
00270 this->plugin = plugin;
00271 }
00272 int ValueSlider::handle_event()
00273 {
00274 plugin->config.value = get_value();
00275 plugin->send_configure_change();
00276 return 1;
00277 }
00278
00279 char* ValueSlider::get_caption()
00280 {
00281 float fraction = ((float)plugin->config.value - MINVALUE) / MAXVALUE;
00282 sprintf(string, "%0.4f", fraction);
00283 return string;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 HueWindow::HueWindow(HueEffect *plugin, int x, int y)
00293 : BC_Window(plugin->gui_string,
00294 x,
00295 y,
00296 310,
00297 100,
00298 310,
00299 100,
00300 0,
00301 0,
00302 1)
00303 {
00304 this->plugin = plugin;
00305 }
00306 void HueWindow::create_objects()
00307 {
00308 int x = 10, y = 10, x1 = 100;
00309 add_subwindow(new BC_Title(x, y, _("Hue:")));
00310 add_subwindow(hue = new HueSlider(plugin, x1, y, 200));
00311 y += 30;
00312 add_subwindow(new BC_Title(x, y, _("Saturation:")));
00313 add_subwindow(saturation = new SaturationSlider(plugin, x1, y, 200));
00314 y += 30;
00315 add_subwindow(new BC_Title(x, y, _("Value:")));
00316 add_subwindow(value = new ValueSlider(plugin, x1, y, 200));
00317 show_window();
00318 flush();
00319 }
00320
00321
00322 WINDOW_CLOSE_EVENT(HueWindow)
00323
00324
00325
00326
00327
00328
00329
00330
00331 PLUGIN_THREAD_OBJECT(HueEffect, HueThread, HueWindow)
00332
00333 HueEngine::HueEngine(HueEffect *plugin, int cpus)
00334 : LoadServer(cpus, cpus)
00335 {
00336 this->plugin = plugin;
00337 }
00338 void HueEngine::init_packages()
00339 {
00340 for(int i = 0; i < LoadServer::get_total_packages(); i++)
00341 {
00342 HuePackage *pkg = (HuePackage*)get_package(i);
00343 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
00344 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
00345 }
00346 }
00347 LoadClient* HueEngine::new_client()
00348 {
00349 return new HueUnit(plugin, this);
00350 }
00351 LoadPackage* HueEngine::new_package()
00352 {
00353 return new HuePackage;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363 HuePackage::HuePackage()
00364 : LoadPackage()
00365 {
00366 }
00367
00368 HueUnit::HueUnit(HueEffect *plugin, HueEngine *server)
00369 : LoadClient(server)
00370 {
00371 this->plugin = plugin;
00372 }
00373
00374
00375
00376
00377
00378
00379
00380 #define HUESATURATION(type, max, components, use_yuv) \
00381 { \
00382 float h_offset = plugin->config.hue; \
00383 float s_offset = ((float)plugin->config.saturation - MINSATURATION) / MAXSATURATION; \
00384 float v_offset = ((float)plugin->config.value - MINVALUE) / MAXVALUE; \
00385 for(int i = pkg->row1; i < pkg->row2; i++) \
00386 { \
00387 type* in_row = (type*)plugin->input->get_rows()[i]; \
00388 type* out_row = (type*)plugin->output->get_rows()[i]; \
00389 \
00390 for(int j = 0; j < w; j++) \
00391 { \
00392 float h, s, va; \
00393 int y, u, v; \
00394 float r, g, b; \
00395 int r_i, g_i, b_i; \
00396 \
00397 if(use_yuv) \
00398 { \
00399 y = (int)in_row[0]; \
00400 u = (int)in_row[1]; \
00401 v = (int)in_row[2]; \
00402 if(max == 0xffff) \
00403 yuv.yuv_to_rgb_16(r_i, g_i, b_i, y, u, v); \
00404 else \
00405 yuv.yuv_to_rgb_8(r_i, g_i, b_i, y, u, v); \
00406 HSV::rgb_to_hsv((float)r_i / max, \
00407 (float)g_i / max, \
00408 (float)b_i / max, \
00409 h, \
00410 s, \
00411 va); \
00412 } \
00413 else \
00414 { \
00415 r = (float)in_row[0] / max; \
00416 g = (float)in_row[1] / max; \
00417 b = (float)in_row[2] / max; \
00418 HSV::rgb_to_hsv(r, g, b, h, s, va); \
00419 } \
00420 \
00421 \
00422 h += h_offset; \
00423 s *= s_offset; \
00424 va *= v_offset; \
00425 \
00426 if(h >= 360) h -= 360; \
00427 if(h < 0) h += 360; \
00428 if(sizeof(type) < 4) \
00429 { \
00430 if(s > 1) s = 1; \
00431 if(va > 1) va = 1; \
00432 if(s < 0) s = 0; \
00433 if(va < 0) va = 0; \
00434 } \
00435 \
00436 if(use_yuv) \
00437 { \
00438 HSV::hsv_to_yuv(y, u, v, h, s, va, max); \
00439 out_row[0] = y; \
00440 out_row[1] = u; \
00441 out_row[2] = v; \
00442 } \
00443 else \
00444 { \
00445 HSV::hsv_to_rgb(r, g, b, h, s, va); \
00446 if(sizeof(type) < 4) \
00447 { \
00448 r *= max; \
00449 g *= max; \
00450 b *= max; \
00451 out_row[0] = (type)CLIP(r, 0, max); \
00452 out_row[1] = (type)CLIP(g, 0, max); \
00453 out_row[2] = (type)CLIP(b, 0, max); \
00454 } \
00455 else \
00456 { \
00457 out_row[0] = (type)r; \
00458 out_row[1] = (type)g; \
00459 out_row[2] = (type)b; \
00460 } \
00461 } \
00462 \
00463 in_row += components; \
00464 out_row += components; \
00465 } \
00466 } \
00467 }
00468
00469
00470 void HueUnit::process_package(LoadPackage *package)
00471 {
00472 HuePackage *pkg = (HuePackage*)package;
00473 int w = plugin->input->get_w();
00474
00475 switch(plugin->input->get_color_model())
00476 {
00477 case BC_RGB888:
00478 HUESATURATION(unsigned char, 0xff, 3, 0)
00479 break;
00480
00481 case BC_RGB_FLOAT:
00482 HUESATURATION(float, 1, 3, 0)
00483 break;
00484
00485 case BC_YUV888:
00486 HUESATURATION(unsigned char, 0xff, 3, 1)
00487 break;
00488
00489 case BC_RGB161616:
00490 HUESATURATION(uint16_t, 0xffff, 3, 0)
00491 break;
00492
00493 case BC_YUV161616:
00494 HUESATURATION(uint16_t, 0xffff, 3, 1)
00495 break;
00496
00497 case BC_RGBA_FLOAT:
00498 HUESATURATION(float, 1, 4, 0)
00499 break;
00500
00501 case BC_RGBA8888:
00502 HUESATURATION(unsigned char, 0xff, 4, 0)
00503 break;
00504
00505 case BC_YUVA8888:
00506 HUESATURATION(unsigned char, 0xff, 4, 1)
00507 break;
00508
00509 case BC_RGBA16161616:
00510 HUESATURATION(uint16_t, 0xffff, 4, 0)
00511 break;
00512
00513 case BC_YUVA16161616:
00514 HUESATURATION(uint16_t, 0xffff, 4, 1)
00515 break;
00516
00517 }
00518 }
00519
00520
00521
00522
00523 REGISTER_PLUGIN(HueEffect)
00524
00525
00526 HueEffect::HueEffect(PluginServer *server)
00527 : PluginVClient(server)
00528 {
00529 engine = 0;
00530 PLUGIN_CONSTRUCTOR_MACRO
00531 }
00532 HueEffect::~HueEffect()
00533 {
00534 PLUGIN_DESTRUCTOR_MACRO
00535 if(engine) delete engine;
00536 }
00537
00538 int HueEffect::process_buffer(VFrame *frame,
00539 int64_t start_position,
00540 double frame_rate)
00541 {
00542 load_configuration();
00543
00544 read_frame(frame,
00545 0,
00546 start_position,
00547 frame_rate,
00548 get_use_opengl());
00549
00550
00551 this->input = frame;
00552 this->output = frame;
00553 if(EQUIV(config.hue, 0) && EQUIV(config.saturation, 0) && EQUIV(config.value, 0))
00554 {
00555 return 0;
00556 }
00557 else
00558 {
00559 if(get_use_opengl())
00560 {
00561 run_opengl();
00562 return 0;
00563 }
00564
00565 if(!engine) engine = new HueEngine(this, PluginClient::smp + 1);
00566
00567 engine->process_packages();
00568 }
00569 return 0;
00570 }
00571
00572 char* HueEffect::plugin_title() { return N_("Hue saturation"); }
00573 int HueEffect::is_realtime() { return 1; }
00574
00575 NEW_PICON_MACRO(HueEffect)
00576 SHOW_GUI_MACRO(HueEffect, HueThread)
00577 SET_STRING_MACRO(HueEffect)
00578 RAISE_WINDOW_MACRO(HueEffect)
00579 LOAD_CONFIGURATION_MACRO(HueEffect, HueConfig)
00580
00581 int HueEffect::load_defaults()
00582 {
00583 char directory[BCTEXTLEN];
00584 sprintf(directory, "%shuesaturation.rc", BCASTDIR);
00585 defaults = new BC_Hash(directory);
00586 defaults->load();
00587 config.hue = defaults->get("HUE", config.hue);
00588 config.saturation = defaults->get("SATURATION", config.saturation);
00589 config.value = defaults->get("VALUE", config.value);
00590 return 0;
00591 }
00592 int HueEffect::save_defaults()
00593 {
00594 defaults->update("HUE", config.hue);
00595 defaults->update("SATURATION", config.saturation);
00596 defaults->update("VALUE", config.value);
00597 defaults->save();
00598 return 0;
00599 }
00600 void HueEffect::save_data(KeyFrame *keyframe)
00601 {
00602 FileXML output;
00603 output.set_shared_string(keyframe->data, MESSAGESIZE);
00604 output.tag.set_title("HUESATURATION");
00605 output.tag.set_property("HUE", config.hue);
00606 output.tag.set_property("SATURATION", config.saturation);
00607 output.tag.set_property("VALUE", config.value);
00608 output.append_tag();
00609 output.tag.set_title("/HUESATURATION");
00610 output.append_tag();
00611 output.terminate_string();
00612 }
00613 void HueEffect::read_data(KeyFrame *keyframe)
00614 {
00615 FileXML input;
00616 input.set_shared_string(keyframe->data, strlen(keyframe->data));
00617 while(!input.read_tag())
00618 {
00619 if(input.tag.title_is("HUESATURATION"))
00620 {
00621 config.hue = input.tag.get_property("HUE", config.hue);
00622 config.saturation = input.tag.get_property("SATURATION", config.saturation);
00623 config.value = input.tag.get_property("VALUE", config.value);
00624 }
00625 }
00626 }
00627 void HueEffect::update_gui()
00628 {
00629 if(thread)
00630 {
00631 thread->window->lock_window();
00632 load_configuration();
00633 thread->window->hue->update(config.hue);
00634 thread->window->saturation->update(config.saturation);
00635 thread->window->value->update(config.value);
00636 thread->window->unlock_window();
00637 }
00638 }
00639
00640 int HueEffect::handle_opengl()
00641 {
00642 #ifdef HAVE_GL
00643 static char *yuv_saturation_frag =
00644 "uniform sampler2D tex;\n"
00645 "uniform float s_offset;\n"
00646 "uniform float v_offset;\n"
00647 "void main()\n"
00648 "{\n"
00649 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
00650 " pixel.r *= v_offset;\n"
00651 " pixel.gb -= vec2(0.5, 0.5);\n"
00652 " pixel.g *= s_offset;\n"
00653 " pixel.b *= s_offset;\n"
00654 " pixel.gb += vec2(0.5, 0.5);\n"
00655 " gl_FragColor = pixel;\n"
00656 "}\n";
00657
00658
00659 static char *yuv_frag =
00660 "uniform sampler2D tex;\n"
00661 "uniform float h_offset;\n"
00662 "uniform float s_offset;\n"
00663 "uniform float v_offset;\n"
00664 "void main()\n"
00665 "{\n"
00666 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
00667 YUV_TO_RGB_FRAG("pixel")
00668 RGB_TO_HSV_FRAG("pixel")
00669 " pixel.r += h_offset;\n"
00670 " pixel.g *= s_offset;\n"
00671 " pixel.b *= v_offset;\n"
00672 " if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
00673 " if(pixel.r < 0.0) pixel.r += 360.0;\n"
00674 HSV_TO_RGB_FRAG("pixel")
00675 RGB_TO_YUV_FRAG("pixel")
00676 " gl_FragColor = pixel;\n"
00677 "}\n";
00678
00679 static char *rgb_frag =
00680 "uniform sampler2D tex;\n"
00681 "uniform float h_offset;\n"
00682 "uniform float s_offset;\n"
00683 "uniform float v_offset;\n"
00684 "void main()\n"
00685 "{\n"
00686 " vec4 pixel = texture2D(tex, gl_TexCoord[0].st);\n"
00687 RGB_TO_HSV_FRAG("pixel")
00688 " pixel.r += h_offset;\n"
00689 " pixel.g *= s_offset;\n"
00690 " pixel.b *= v_offset;\n"
00691 " if(pixel.r >= 360.0) pixel.r -= 360.0;\n"
00692 " if(pixel.r < 0.0) pixel.r += 360.0;\n"
00693 HSV_TO_RGB_FRAG("pixel")
00694 " gl_FragColor = pixel;\n"
00695 "}\n";
00696
00697
00698 get_output()->to_texture();
00699 get_output()->enable_opengl();
00700
00701 unsigned int frag_shader = 0;
00702 switch(get_output()->get_color_model())
00703 {
00704 case BC_YUV888:
00705 case BC_YUVA8888:
00706
00707 if(EQUIV(config.hue, 0))
00708 frag_shader = VFrame::make_shader(0,
00709 yuv_saturation_frag,
00710 0);
00711 else
00712 frag_shader = VFrame::make_shader(0,
00713 yuv_frag,
00714 0);
00715 break;
00716 default:
00717 frag_shader = VFrame::make_shader(0,
00718 rgb_frag,
00719 0);
00720 break;
00721 }
00722
00723
00724 if(frag_shader > 0)
00725 {
00726 glUseProgram(frag_shader);
00727 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
00728 glUniform1f(glGetUniformLocation(frag_shader, "h_offset"), config.hue);
00729 glUniform1f(glGetUniformLocation(frag_shader, "s_offset"),
00730 ((float)config.saturation - MINSATURATION) / MAXSATURATION);
00731 glUniform1f(glGetUniformLocation(frag_shader, "v_offset"),
00732 ((float)config.value - MINVALUE) / MAXVALUE);
00733 }
00734
00735 get_output()->init_screen();
00736 get_output()->bind_texture(0);
00737 get_output()->draw_texture();
00738 glUseProgram(0);
00739 get_output()->set_opengl_state(VFrame::SCREEN);
00740 #endif
00741 }
00742
00743
00744
00745
00746