From 48d2f210ee3c6c440f05f7269ba18687220a291e Mon Sep 17 00:00:00 2001 From: "Kwabena W. Agyeman" Date: Fri, 12 Jan 2024 18:09:48 -0800 Subject: [PATCH] ports: Fix sensor bayer image with cropping and transpose. --- src/omv/imlib/bayer.c | 47 ++++++++++++++++++++++++++ src/omv/imlib/imlib.h | 1 + src/omv/modules/py_image.c | 48 +-------------------------- src/omv/ports/mimxrt/sensor.c | 1 + src/omv/ports/stm32/omv_portconfig.mk | 1 + src/omv/ports/stm32/sensor.c | 1 + 6 files changed, 52 insertions(+), 47 deletions(-) diff --git a/src/omv/imlib/bayer.c b/src/omv/imlib/bayer.c index 1a4722c61..ec9cba7bf 100644 --- a/src/omv/imlib/bayer.c +++ b/src/omv/imlib/bayer.c @@ -10,6 +10,53 @@ */ #include "imlib.h" +pixformat_t imlib_bayer_shift(pixformat_t pixfmt, int x, int y, bool transpose) { + bool shift_right = x % 2; + bool shift_down = y % 2; + + switch (pixfmt) { + case PIXFORMAT_BAYER_BGGR: { + if (shift_right && shift_down) { + return PIXFORMAT_BAYER_RGGB; + } else if (shift_right) { + return transpose ? PIXFORMAT_BAYER_GRBG : PIXFORMAT_BAYER_GBRG; + } else if (shift_down) { + return transpose ? PIXFORMAT_BAYER_GBRG : PIXFORMAT_BAYER_GRBG; + } + } + case PIXFORMAT_BAYER_GBRG: { + if (shift_right && shift_down) { + return transpose ? PIXFORMAT_BAYER_GBRG : PIXFORMAT_BAYER_GRBG; + } else if (shift_right) { + return PIXFORMAT_BAYER_BGGR; + } else if (shift_down) { + return PIXFORMAT_BAYER_RGGB; + } + } + case PIXFORMAT_BAYER_GRBG: { + if (shift_right && shift_down) { + return transpose ? PIXFORMAT_BAYER_GRBG : PIXFORMAT_BAYER_GBRG; + } else if (shift_right) { + return PIXFORMAT_BAYER_RGGB; + } else if (shift_down) { + return PIXFORMAT_BAYER_BGGR; + } + } + case PIXFORMAT_BAYER_RGGB: { + if (shift_right && shift_down) { + return PIXFORMAT_BAYER_BGGR; + } else if (shift_right) { + return transpose ? PIXFORMAT_BAYER_GBRG : PIXFORMAT_BAYER_GRBG; + } else if (shift_down) { + return transpose ? PIXFORMAT_BAYER_GRBG : PIXFORMAT_BAYER_GBRG; + } + } + default: { + return pixfmt; + } + } +} + void imlib_debayer_line(int x_start, int x_end, int y_row, void *dst_row_ptr, pixformat_t pixfmt, image_t *src) { int src_w = src->w, w_limit = src_w - 1, w_limit_m_1 = w_limit - 1; int src_h = src->h, h_limit = src_h - 1, h_limit_m_1 = h_limit - 1; diff --git a/src/omv/imlib/imlib.h b/src/omv/imlib/imlib.h index cb845a1c6..60ed8c470 100644 --- a/src/omv/imlib/imlib.h +++ b/src/omv/imlib/imlib.h @@ -1137,6 +1137,7 @@ void imlib_fill_image_from_float(image_t *img, int w, int h, float *data, float bool mirror, bool flip, bool dst_transpose, bool src_transpose); // Bayer Image Processing +pixformat_t imlib_bayer_shift(pixformat_t pixfmt, int x, int y, bool transpose); void imlib_debayer_line(int x_start, int x_end, int y_row, void *dst_row_ptr, pixformat_t pixfmt, image_t *src); void imlib_debayer_image(image_t *dst, image_t *src); diff --git a/src/omv/modules/py_image.c b/src/omv/modules/py_image.c index a4f930151..c8b17d6b4 100644 --- a/src/omv/modules/py_image.c +++ b/src/omv/modules/py_image.c @@ -1055,53 +1055,7 @@ static mp_obj_t py_image_to(pixformat_t pixfmt, const uint16_t *default_color_pa (alpha_palette != NULL)) { mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Only bayer copying/cropping is supported!")); } else { - bool shift_right = arg_roi.x % 2; - bool shift_down = arg_roi.y % 2; - switch (dst_img.pixfmt) { - case PIXFORMAT_BAYER_BGGR: { - if (shift_right && shift_down) { - dst_img.pixfmt = PIXFORMAT_BAYER_RGGB; - } else if (shift_right) { - dst_img.pixfmt = PIXFORMAT_BAYER_GBRG; - } else if (shift_down) { - dst_img.pixfmt = PIXFORMAT_BAYER_GRBG; - } - break; - } - case PIXFORMAT_BAYER_GBRG: { - if (shift_right && shift_down) { - dst_img.pixfmt = PIXFORMAT_BAYER_GRBG; - } else if (shift_right) { - dst_img.pixfmt = PIXFORMAT_BAYER_BGGR; - } else if (shift_down) { - dst_img.pixfmt = PIXFORMAT_BAYER_RGGB; - } - break; - } - case PIXFORMAT_BAYER_GRBG: { - if (shift_right && shift_down) { - dst_img.pixfmt = PIXFORMAT_BAYER_GBRG; - } else if (shift_right) { - dst_img.pixfmt = PIXFORMAT_BAYER_RGGB; - } else if (shift_down) { - dst_img.pixfmt = PIXFORMAT_BAYER_BGGR; - } - break; - } - case PIXFORMAT_BAYER_RGGB: { - if (shift_right && shift_down) { - dst_img.pixfmt = PIXFORMAT_BAYER_BGGR; - } else if (shift_right) { - dst_img.pixfmt = PIXFORMAT_BAYER_GRBG; - } else if (shift_down) { - dst_img.pixfmt = PIXFORMAT_BAYER_GBRG; - } - break; - } - default: { - break; - } - } + dst_img.pixfmt = imlib_bayer_shift(dst_img.pixfmt, arg_roi.x, arg_roi.y, false); hint &= ~(IMAGE_HINT_AREA | IMAGE_HINT_BICUBIC | IMAGE_HINT_BILINEAR | diff --git a/src/omv/ports/mimxrt/sensor.c b/src/omv/ports/mimxrt/sensor.c index 5c9d3d9f6..dc29e838f 100644 --- a/src/omv/ports/mimxrt/sensor.c +++ b/src/omv/ports/mimxrt/sensor.c @@ -376,6 +376,7 @@ int sensor_snapshot(sensor_t *sensor, image_t *image, uint32_t flags) { case PIXFORMAT_BAYER: MAIN_FB()->pixfmt = PIXFORMAT_BAYER; MAIN_FB()->subfmt_id = sensor->hw_flags.bayer; + MAIN_FB()->pixfmt = imlib_bayer_shift(MAIN_FB()->pixfmt, MAIN_FB()->x, MAIN_FB()->y, sensor->transpose); break; case PIXFORMAT_YUV422: { bool yuv_order = sensor->hw_flags.yuv_order == SENSOR_HW_FLAGS_YUV422; diff --git a/src/omv/ports/stm32/omv_portconfig.mk b/src/omv/ports/stm32/omv_portconfig.mk index fc7a04a3e..0b21b1867 100644 --- a/src/omv/ports/stm32/omv_portconfig.mk +++ b/src/omv/ports/stm32/omv_portconfig.mk @@ -572,6 +572,7 @@ UVC_OBJ += $(addprefix $(BUILD)/$(OMV_DIR)/sensors/, \ ) UVC_OBJ += $(addprefix $(BUILD)/$(OMV_DIR)/imlib/,\ + bayer.o \ lab_tab.o \ xyz_tab.o \ rainbow_tab.o \ diff --git a/src/omv/ports/stm32/sensor.c b/src/omv/ports/stm32/sensor.c index 7bdd402ba..99b3a6837 100644 --- a/src/omv/ports/stm32/sensor.c +++ b/src/omv/ports/stm32/sensor.c @@ -976,6 +976,7 @@ int sensor_snapshot(sensor_t *sensor, image_t *image, uint32_t flags) { case PIXFORMAT_BAYER: MAIN_FB()->pixfmt = PIXFORMAT_BAYER; MAIN_FB()->subfmt_id = sensor->hw_flags.bayer; + MAIN_FB()->pixfmt = imlib_bayer_shift(MAIN_FB()->pixfmt, MAIN_FB()->x, MAIN_FB()->y, sensor->transpose); break; case PIXFORMAT_YUV422: { bool yuv_order = sensor->hw_flags.yuv_order == SENSOR_HW_FLAGS_YUV422;