Skip to content

Commit

Permalink
check for color depth mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kisvegabor committed Jan 21, 2022
1 parent c24547e commit 6c5b59e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/libs/bmp/lv_example_bmp_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ void lv_example_bmp_1(void)
lv_obj_t * img = lv_img_create(lv_scr_act());
/* Assuming a File system is attached to letter 'A'
* E.g. set LV_USE_FS_STDIO 'A' in lv_conf.h */
#if LV_COLOR_DEPTH == 32
lv_img_set_src(img, "A:lvgl/examples/libs/bmp/example_32bit.bmp");
#elif LV_COLOR_DEPTH == 16
lv_img_set_src(img, "A:lvgl/examples/libs/bmp/example_16bit.bmp");
#endif
lv_obj_center(img);

}
Expand Down
21 changes: 21 additions & 0 deletions src/extra/libs/bmp/lv_bmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ static lv_res_t decoder_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t *
lv_fs_read(&b.f, header, 54, NULL);

if(0x42 != header[0] || 0x4d != header[1]) {
lv_fs_close(&b.f);
return LV_RES_INV;
}

Expand All @@ -150,6 +151,26 @@ static lv_res_t decoder_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t *
memcpy(&b.bpp, header + 28, 2);
b.row_size_bytes = ((b.bpp * b.px_width + 31) / 32) * 4;

bool color_depth_error = false;
if(LV_COLOR_DEPTH == 32 && (b.bpp != 32 || b.bpp != 24)) {
LV_LOG_WARN("LV_COLOR_DEPTH == 32 but bpp is %d (should be 32 or 24)", b.bpp);
color_depth_error = true;
}
else if(LV_COLOR_DEPTH == 16 && b.bpp != 16) {
LV_LOG_WARN("LV_COLOR_DEPTH == 16 but bpp is %d (should be 16)", b.bpp);
color_depth_error = true;
}
else if(LV_COLOR_DEPTH == 8 && b.bpp != 8) {
LV_LOG_WARN("LV_COLOR_DEPTH == 8 but bpp is %d (should be 8)", b.bpp);
color_depth_error = true;
}

if(color_depth_error) {
dsc->error_msg = "Color depth mismatch";
lv_fs_close(&b.f);
return LV_RES_INV;
}

dsc->user_data = lv_mem_alloc(sizeof(bmp_dsc_t));
LV_ASSERT_MALLOC(dsc->user_data);
if(dsc->user_data == NULL) return LV_RES_INV;
Expand Down

0 comments on commit 6c5b59e

Please sign in to comment.