From a6de61f475b231bf1bb8ec0cd5a0af10c7c7a896 Mon Sep 17 00:00:00 2001 From: Martijn Braam Date: Wed, 16 Aug 2023 14:45:57 +0200 Subject: [PATCH] Implement whitebalance --- data/debayer.frag | 1 - src/io_pipeline.c | 3 ++- src/main.c | 6 ++++-- src/process_pipeline.c | 29 +++++++++++++++++++++++------ 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/data/debayer.frag b/data/debayer.frag index ede66a3..d0900e3 100644 --- a/data/debayer.frag +++ b/data/debayer.frag @@ -61,7 +61,6 @@ main() #endif color -= blacklevel; - color *= 1.0-(1.0/blacklevel); color *= color_matrix; vec3 gamma_color = pow(color, vec3(inv_gamma)); diff --git a/src/io_pipeline.c b/src/io_pipeline.c index 6e7445f..5002c67 100644 --- a/src/io_pipeline.c +++ b/src/io_pipeline.c @@ -532,7 +532,8 @@ update_state(MPPipeline *pipeline, const mp_state_io *new_state) state_io.flash_enabled = new_state->flash_enabled; state_io.stats.exposure = new_state->stats.exposure; - state_io.stats.whitebalance = new_state->stats.whitebalance; + state_io.stats.temp = new_state->stats.temp; + state_io.stats.tint = new_state->stats.tint; state_io.stats.focus = new_state->stats.focus; } diff --git a/src/main.c b/src/main.c index 5c492a2..9353603 100644 --- a/src/main.c +++ b/src/main.c @@ -133,7 +133,8 @@ update_io_pipeline() .focus.manual_req = state.focus.manual_req, .stats.exposure = state.stats.exposure, - .stats.whitebalance = state.stats.whitebalance, + .stats.temp = state.stats.temp, + .stats.tint = state.stats.tint, .stats.focus = state.stats.focus, }; mp_io_pipeline_update_state(&new_state); @@ -171,7 +172,8 @@ update_state(const mp_state_main *new_state) state.preview_buffer_height = new_state->preview_buffer_height; state.stats.exposure = new_state->stats.exposure; - state.stats.whitebalance = new_state->stats.whitebalance; + state.stats.temp = new_state->stats.temp; + state.stats.tint = new_state->stats.tint; state.stats.focus = new_state->stats.focus; // Make the right settings available for the camera diff --git a/src/process_pipeline.c b/src/process_pipeline.c index b4b3f66..028e5c7 100644 --- a/src/process_pipeline.c +++ b/src/process_pipeline.c @@ -388,6 +388,17 @@ mp_process_pipeline_init_gl(GdkSurface *surface) sizeof(GdkSurface *)); } +float +clamp_float(float value, float min, float max) { + if(value > max) + return max; + + if(value < min) + return min; + + return value; +} + static GdkTexture * process_image_for_preview(const uint8_t *image) { @@ -461,10 +472,10 @@ process_image_for_preview(const uint8_t *image) mp_main_set_preview(output_buffer); if (!state_proc.exposure.manual && state_proc.exposure.auto_control == 0) { - int width = output_buffer_width / 3; + int width = output_buffer_width; int height = output_buffer_height / 3; uint32_t *center = g_malloc_n(width * height * sizeof(uint32_t), 1); - glReadPixels(width, + glReadPixels(0, height, width, height, @@ -474,9 +485,14 @@ process_image_for_preview(const uint8_t *image) libmegapixels_aaa_software_statistics( center, width, height, &state_proc.stats); - state_proc.red += (state_proc.stats.whitebalance * -0.02f) + (state_proc.stats.tint * 0.01f); - state_proc.blue += (state_proc.stats.whitebalance * +0.02f) + (state_proc.stats.tint * 0.01f); - state_proc.blacklevel += state_proc.stats.blacklevel * 0.01f; + float w_gain = 0.02f; + float t_gain = 0.01f; + state_proc.red += (state_proc.stats.temp * +w_gain) + (state_proc.stats.tint * t_gain); + state_proc.blue += (state_proc.stats.temp * -w_gain) + (state_proc.stats.tint * t_gain); + state_proc.blacklevel -= state_proc.stats.blacklevel * 0.001f; + state_proc.blacklevel = clamp_float(state_proc.blacklevel, 0.0f, 0.07f); + state_proc.red = clamp_float(state_proc.red, 0.5f, 3.0f); + state_proc.blue = clamp_float(state_proc.blue, 0.5f, 3.0f); gles2_debayer_set_shading(gles2_debayer, state_proc.red, state_proc.blue, state_proc.blacklevel); } @@ -1052,7 +1068,8 @@ update_state(MPPipeline *pipeline, const mp_state_proc *new_state) .focus.manual = state_proc.focus.manual, .stats.exposure = state_proc.stats.exposure, - .stats.whitebalance = state_proc.stats.whitebalance, + .stats.temp = state_proc.stats.temp, + .stats.tint = state_proc.stats.tint, .stats.focus = state_proc.stats.focus, }; mp_main_update_state(&new_main);