ports: Fix sensor bayer image with cropping and transpose.

This commit is contained in:
Kwabena W. Agyeman 2024-01-12 18:09:48 -08:00
parent 87d2da4640
commit 48d2f210ee
6 changed files with 52 additions and 47 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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 \

View File

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