diff --git a/configuration.cc b/configuration.cc index be01907..0921665 100644 --- a/configuration.cc +++ b/configuration.cc @@ -47,6 +47,8 @@ Configuration::Configuration() { debug = 0; filter_ratio = 0.5; + auto_brightness = 0; + auto_brightness_target = 0.5; }; Configuration::~Configuration() { @@ -298,9 +300,9 @@ void Configuration::SetValue(std::string name, std::string value) { auto_brightness = atol(value.c_str()); debug ("Auto Brightness set to : %ld", auto_brightness); } - if (name.compare("auto_birghtness_target") == 0) { + if (name.compare("auto_brightness_target") == 0) { auto_brightness_target = atof(value.c_str()); - debug ("Filter Ration set to : %g", auto_brightness_target); + debug ("Auto Brightness Target set to : %g", auto_brightness_target); } }; @@ -336,8 +338,6 @@ std::string Configuration::GetValues() { je.SetAddArrayEscaped("variables", jpe.ToString()); // - - jp.AddObject(je); return jp.ToString(); }; diff --git a/main.cc b/main.cc index cc6b2c9..bf2d682 100644 --- a/main.cc +++ b/main.cc @@ -52,6 +52,7 @@ static void *thread_webserver(void *ignored_argument) { static void *thread_video(void *ignored_argument) { timeval tv_fps; time_t time_fps, time_refreshctls; + float current_brightness = 0.5; time_fps = time(NULL); time_refreshctls = time(NULL); @@ -92,13 +93,47 @@ static void *thread_video(void *ignored_argument) { if (time(NULL) - time_fps > 5) { time_fps = time(NULL); debug ("FPS: %f", framespersecond); + debug ("Brightness: %f", current_brightness); } Lock(); - inputimage.CopyTo(¤timage, config.web_width, config.web_height); + inputimage.CopyTo(¤timage, config.web_width, config.web_height, ¤t_brightness); if (inputfloatfilter.AddScaledImage(&inputfloat, config.filter_ratio) == 0) inputfloat.CopyTo(&inputfloatfilter); inputfloatfilter.CopyTo(¤timagefloat); UnLock(); + + // calculate and set new brightness value if auto_brightness is enabled + if (config.auto_brightness > 0) { + std::list ctrls = vdev->GetDevCtrls(); + std::list::iterator it; + int found = 0; + + for (it = ctrls.begin(); it != ctrls.end(); ++it) { + if (it->id == config.auto_brightness) { + found = 1; + break; + } + } + if (!found) { + debug ("Auto Brightness control ID %d not found in device controls.", config.auto_brightness); + config.auto_brightness = 0; // disable auto brightness if control not found + } + if (found) { + int new_ctrl_value = it->value; + + if (config.auto_brightness_target < 0.0f) config.auto_brightness_target = 0.0f; + if (config.auto_brightness_target > 1.0f) config.auto_brightness_target = 1.0f; + + if (current_brightness < config.auto_brightness_target) { + new_ctrl_value += 1; // increase brightness + } else if (current_brightness > config.auto_brightness_target) { + new_ctrl_value -= 1; // decrease brightness + } + vdev->SetDevCtrl(config.auto_brightness, new_ctrl_value); + } + } + + // calculate the frames per second if (framespersecond == -1) framespersecond = 1.0/(timedeiff_ms(&tv_fps)/1000.0); else framespersecond = (framespersecond * 59.0 + 1.0/(timedeiff_ms(&tv_fps)/1000.0)) / 60.0; } diff --git a/videoframe.cc b/videoframe.cc index 2e36bc3..2c41aae 100644 --- a/videoframe.cc +++ b/videoframe.cc @@ -127,7 +127,7 @@ int VideoFrame::TestScreen(int w, int h) { } -int VideoFrame::CopyTo(VideoFrame *dest, int destw, int desth) { +int VideoFrame::CopyTo(VideoFrame *dest, int destw, int desth, float *brightness) { unsigned char *destptr; if (dest == NULL) return 0; @@ -159,9 +159,20 @@ int VideoFrame::CopyTo(VideoFrame *dest, int destw, int desth) { // Copy the pixel data (e.g., all 3 RGB bytes) memcpy(&destptr[dest_idx], &mem[src_idx], 3); + + // calculate the overall brightness of the image and store it in the brightness array if provided + if (brightness != NULL) { + float pixel_brightness = (0.299f * mem[src_idx] + 0.587f * mem[src_idx + 1] + 0.114f * mem[src_idx + 2]) / 255.0f; + *brightness += pixel_brightness; + } } } + if (brightness != NULL) { + *brightness /= (destw * desth); + *brightness = sqrtf(*brightness); + } + return 1; }; diff --git a/videoframe.h b/videoframe.h index 75fd4b6..8325003 100644 --- a/videoframe.h +++ b/videoframe.h @@ -24,7 +24,7 @@ class VideoFrame { int SetSize(int w, int h); int ConvertToJpeg(InMemoryFile *imf, int quality); int TestScreen(int w, int h); - int CopyTo(VideoFrame *dest, int destw, int desth); + int CopyTo(VideoFrame *dest, int destw, int desth, float *brightness = NULL); int CopyFrom(int sw, int sh, int ssize, unsigned char *sdata); }; diff --git a/www/default.css b/www/default.css index 8bfe8fb..73403e5 100644 --- a/www/default.css +++ b/www/default.css @@ -240,6 +240,11 @@ h1 { box-shadow: 0 18px 50px rgba(0, 0, 0, 0.14); } +.preview-card { + display: flex; + flex-direction: column; +} + .section-bar, .preview-footer { display: flex; @@ -272,11 +277,12 @@ h1 { } .preview-stage { + --preview-ratio: 16 / 9; position: relative; display: grid; - min-height: 240px; - aspect-ratio: 16 / 8.5; - max-height: 68vh; + width: min(100%, 1200px); + aspect-ratio: var(--preview-ratio); + margin-inline: auto; place-items: center; overflow: hidden; background: @@ -289,6 +295,7 @@ h1 { width: 100%; height: 100%; object-fit: contain; + object-position: center; } .preview-error { @@ -513,6 +520,11 @@ h1 { gap: 8px; } +.setting-slider-editor { + grid-template-columns: minmax(0, 1fr) 78px auto; + align-items: center; +} + .panel-hint { margin: 13px 1px 0; color: var(--muted); @@ -559,10 +571,6 @@ h1 { .content-grid { grid-template-columns: minmax(0, 1fr); } - - .preview-stage { - aspect-ratio: 16 / 10; - } } @media (max-width: 560px) { @@ -589,11 +597,6 @@ h1 { width: 100%; } - .preview-stage { - min-height: 190px; - aspect-ratio: 4 / 3; - } - .section-bar, .preview-footer { padding-right: 12px; @@ -618,6 +621,10 @@ h1 { grid-column: 2; } + .setting-slider-editor .control-slider { + grid-column: 1; + } + .control-value { grid-column: 3; } diff --git a/www/index.html b/www/index.html index 06a72e1..31997b4 100644 --- a/www/index.html +++ b/www/index.html @@ -103,6 +103,7 @@