ading auto brightness

main
Steffen Pohle 2 weeks ago
parent df5e657068
commit 2da2e2748a

@ -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();
};

@ -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(&currentimage, config.web_width, config.web_height);
inputimage.CopyTo(&currentimage, config.web_width, config.web_height, &current_brightness);
if (inputfloatfilter.AddScaledImage(&inputfloat, config.filter_ratio) == 0)
inputfloat.CopyTo(&inputfloatfilter);
inputfloatfilter.CopyTo(&currentimagefloat);
UnLock();
// calculate and set new brightness value if auto_brightness is enabled
if (config.auto_brightness > 0) {
std::list<VideoDevCtrl> ctrls = vdev->GetDevCtrls();
std::list<VideoDevCtrl>::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;
}

@ -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;
};

@ -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);
};

@ -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;
}

@ -103,6 +103,7 @@
<script>
const image = document.getElementById("webcamimage");
const previewStage = document.querySelector(".preview-stage");
const controlsElement = document.getElementById("controls");
const variablesElement = document.getElementById("variables");
const notice = document.getElementById("notice");
@ -112,6 +113,11 @@
let filterEnabled = false;
let refreshCount = 0;
function setPreviewAspectRatio() {
if (!previewStage || !image.naturalWidth || !image.naturalHeight) return;
previewStage.style.setProperty("--preview-ratio", image.naturalWidth + " / " + image.naturalHeight);
}
function post(url) {
return fetch(url, { method: "POST" }).then(function(response) {
if (!response.ok) throw new Error("Request failed: " + response.status);
@ -142,6 +148,7 @@
}
image.addEventListener("load", function() {
setPreviewAspectRatio();
document.getElementById("preview-error").hidden = true;
document.getElementById("last-updated").textContent =
"Updated " + new Date().toLocaleTimeString();
@ -286,7 +293,8 @@
const data = JSON.parse(response);
const variables = data.variables || [];
const activeName = document.activeElement &&
document.activeElement.classList.contains("variable-value")
(document.activeElement.classList.contains("variable-value") ||
document.activeElement.classList.contains("variable-slider"))
? document.activeElement.dataset.name
: null;
if (activeName !== null) return;
@ -312,13 +320,75 @@
const editor = document.createElement("div");
editor.className = "setting-editor";
const input = document.createElement("input");
const isControlId = variable.name === "auto_brightness";
const isRatio = variable.name === "filter_ratio" ||
variable.name === "auto_brightness_target";
const input = document.createElement(isControlId ? "select" : "input");
input.className = "variable-value";
input.type = "number";
input.step = "any";
input.value = variable.value;
input.dataset.name = variable.name;
input.setAttribute("aria-label", variable.name + " value");
if (isControlId) {
const currentId = String(variable.value);
const disabledOption = document.createElement("option");
disabledOption.value = "0";
disabledOption.textContent = "Disabled (ID: 0)";
input.appendChild(disabledOption);
if (currentId !== "0" && !controls.some(function(control) {
return String(control.id) === currentId;
})) {
const currentOption = document.createElement("option");
currentOption.value = currentId;
currentOption.textContent = "Unavailable control (ID: " + currentId + ")";
input.appendChild(currentOption);
}
controls.forEach(function(control) {
if (String(control.id) === "0") return;
const option = document.createElement("option");
option.value = String(control.id);
option.textContent = control.name + " (ID: " + control.id + ")";
input.appendChild(option);
});
input.value = currentId;
} else if (isRatio) {
const slider = document.createElement("input");
slider.className = "control-slider variable-slider";
slider.type = "range";
slider.min = "0";
slider.max = "1";
slider.step = "0.01";
slider.value = variable.value;
slider.dataset.name = variable.name;
slider.setAttribute("aria-label", variable.name + " slider");
input.type = "number";
input.min = "0";
input.max = "1";
input.step = "0.01";
input.value = variable.value;
input.setAttribute("aria-label", variable.name + " value");
let submitTimeout;
slider.addEventListener("input", function() {
input.value = slider.value;
window.clearTimeout(submitTimeout);
submitTimeout = window.setTimeout(function() {
submitVariable(input);
}, 300);
});
slider.addEventListener("change", function() {
window.clearTimeout(submitTimeout);
submitVariable(input);
});
input.addEventListener("input", function() {
slider.value = input.value;
});
editor.classList.add("setting-slider-editor");
editor.append(slider);
} else {
input.type = "number";
input.step = "any";
input.value = variable.value;
}
const apply = document.createElement("button");
apply.className = "button button-small";
apply.type = "button";
@ -384,13 +454,11 @@
function refresh() {
reloadImage();
if (refreshCount++ % 25 === 0) {
refreshCtrls();
refreshVariables();
refreshCtrls().then(refreshVariables);
}
}
refreshCtrls();
refreshVariables();
refreshCtrls().then(refreshVariables);
window.setInterval(refresh, 500);
</script>
</body>

Loading…
Cancel
Save