From 24445162fa4fae4bab001a56a625f52d068b03a7 Mon Sep 17 00:00:00 2001 From: Martijn Braam Date: Wed, 16 Sep 2020 11:55:13 +0200 Subject: [PATCH] WIP --- main.c | 51 +++++++++++++++++++++++++++++++++++++++++++------- quickdebayer.c | 28 +++++++++++++++++++++++++-- quickdebayer.h | 2 +- 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 1830e34..4355db4 100644 --- a/main.c +++ b/main.c @@ -68,6 +68,10 @@ static int current_fmt = 0; static int current_rotate = 0; static int current_fd; static int capture = 0; +static int exposure = 0; +static int exposure_max = 0; +static int gain = 0; +static int gain_max = 0; static int current_is_rear = 1; static cairo_surface_t *surface = NULL; static int preview_width = -1; @@ -243,7 +247,15 @@ init_sensor(char *fn, int width, int height, int mbus, int rate) fmt.format.code); // Placeholder, default is also 1 - //v4l2_ctrl_set(fd, V4L2_CID_AUTOGAIN, 1); + // Exposure on the ov5640 is in number of lines + v4l2_ctrl_set(fd, V4L2_CID_AUTOGAIN, 0); + v4l2_ctrl_set(fd, V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL); + v4l2_ctrl_set(fd, V4L2_CID_EXPOSURE, height/2); + v4l2_ctrl_set(fd, V4L2_CID_GAIN, 0); + gain = 0; + exposure = height/2; + exposure_max = height; + gain_max = 1024; close(current_fd); current_fd = fd; } @@ -362,8 +374,8 @@ process_image(const int *p, int size) GError *error = NULL; double scale; cairo_t *cr; - t = clock(); - + int feedback; + int iso; dc1394bayer_method_t method = DC1394_BAYER_METHOD_DOWNSAMPLE; dc1394color_filter_t filter = DC1394_COLOR_FILTER_BGGR; @@ -376,7 +388,13 @@ process_image(const int *p, int size) } else { pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current_width / 6, current_height / 6); pixels = gdk_pixbuf_get_pixels(pixbuf); - quick_debayer_bggr8((const uint8_t *)p, pixels, current_width, current_height, 3); + t = clock(); + feedback = quick_debayer_bggr8((const uint8_t *)p, pixels, current_width, current_height, 3); + t = clock() - t; + time_taken = ((double) t) / CLOCKS_PER_SEC; + iso = 100+((float)gain/(float)gain_max * 6400); + printf("%f fps (%f ms) feedback %d, iso %d, exposure %fdeg\n", 1.0 / time_taken, time_taken*1000, feedback, + iso, (float)exposure/(float)exposure_max*360.0); } if (current_rotate == 0) { @@ -411,9 +429,28 @@ process_image(const int *p, int size) gtk_widget_queue_draw_area(preview, 0, 0, preview_width, preview_height); } capture = 0; - t = clock() - t; - time_taken = ((double) t) / CLOCKS_PER_SEC; - printf("%f fps\n", 1.0 / time_taken); + if (feedback > 0) { + exposure = exposure + (exposure_max/100.0 * feedback); + if(exposure > exposure_max) { + exposure = exposure_max; + gain = gain + (gain_max/600.0 * feedback); + } + if ( gain > gain_max ) { + gain = gain_max; + } + } + if(feedback < 0) { + gain = gain + (gain_max/600.0 * feedback); + if(gain < 1){ + gain = 0; + exposure = exposure + (exposure_max/100.0 * feedback); + } + if(exposure < 1){ + exposure = 0; + } + } + v4l2_ctrl_set(current_fd, V4L2_CID_EXPOSURE, exposure); + v4l2_ctrl_set(current_fd, V4L2_CID_GAIN, gain); } static gboolean diff --git a/quickdebayer.c b/quickdebayer.c index a97977f..72be5ce 100644 --- a/quickdebayer.c +++ b/quickdebayer.c @@ -3,7 +3,7 @@ // Fast but bad debayer method that scales and rotates by skipping source pixels and // doesn't interpolate any values at all -void +int quick_debayer_bggr8(const uint8_t *source, uint8_t *destination, int width, int height, int skip) { int byteskip = 2 * skip; @@ -11,13 +11,24 @@ quick_debayer_bggr8(const uint8_t *source, uint8_t *destination, int width, int int i; int j=0; int row_left = width; - + int clipped = 0; + int above80 = 0; + int above50 = 0; // B G // G R for(i=0;i 250) { + clipped++; + } + if(source[i+1] > 205) { + above80++; + } + if(source[i+1] > 128) { + above50++; + } i = i + byteskip; row_left = row_left - byteskip; if(row_left <= 0){ @@ -26,4 +37,17 @@ quick_debayer_bggr8(const uint8_t *source, uint8_t *destination, int width, int i = i + (width * 2 * (skip-1)); } } + if(clipped > 300) { + return -10; + } + if(clipped > 10) { + return -1; + } + if(above50 < 10) { + return 10; + } + if(above80 < 10) { + return 1; + } + return 0; } diff --git a/quickdebayer.h b/quickdebayer.h index 6a31910..03c5fcf 100644 --- a/quickdebayer.h +++ b/quickdebayer.h @@ -1,4 +1,4 @@ #include -void quick_debayer_bggr8(const uint8_t *source, uint8_t *destination, int width, int height, int skip); +int quick_debayer_bggr8(const uint8_t *source, uint8_t *destination, int width, int height, int skip);