refactor(image): add magic to image header (#5051)

Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
This commit is contained in:
Neo Xu
2023-12-19 19:54:30 +08:00
committed by GitHub
parent 08803f9a11
commit 5e3fb68835
5 changed files with 24 additions and 18 deletions

View File

@@ -11,7 +11,7 @@ fs_driver.fs_register(fs_drv, 'S')
#
image_bulb_gif = lv.image_dsc_t(
{
"header": {"always_zero": 0, "w": 0, "h": 0, "cf": lv.COLOR_FORMAT.RAW},
"header": {"w": 0, "h": 0, "cf": lv.COLOR_FORMAT.RAW},
"data_size": 0,
"data": img_bulb_gif_map,
}

View File

@@ -5,7 +5,7 @@ from img_wink_png import img_wink_png_map
image_wink_png = lv.image_dsc_t(
{
"header": {"always_zero": 0, "w": 50, "h": 50, "cf": lv.COLOR_FORMAT.RAW_ALPHA},
"header": {"w": 50, "h": 50, "cf": lv.COLOR_FORMAT.RAW_ALPHA},
"data_size": 5158,
"data": img_wink_png_map,
}

View File

@@ -325,8 +325,8 @@ class LVGLImageHeader:
@property
def binary(self) -> bytearray:
binary = bytearray()
binary += uint8_t(0x19) # magic number for lvgl version 9
binary += uint8_t(self.cf.value)
binary += uint8_t(0) # 8bits format
binary += uint16_t(self.flags) # 16bits flags
binary += uint16_t(self.w) # 16bits width
@@ -612,6 +612,7 @@ uint8_t {varname}_map[] = {{
}};
const lv_img_dsc_t {varname} = {{
.header.magic = LV_IMAGE_HEADER_MAGIC,
.header.cf = LV_COLOR_FORMAT_{self.cf.name},
.header.flags = {flags},
.header.w = {self.w},

View File

@@ -40,6 +40,11 @@ extern "C" {
#define _LV_ZOOM_INV_UPSCALE 5
/** Magic number for lvgl image, 9 means lvgl version 9
* It must not be a valid ASCII character nor larger than 0x80. See `lv_image_src_get_type`.
*/
#define LV_IMAGE_HEADER_MAGIC (0x19)
/**********************
* TYPEDEFS
**********************/
@@ -89,12 +94,6 @@ typedef enum {
LV_IMAGE_COMPRESS_LZ4,
} lv_image_compress_t;
/**
* The first 8 bit is very important to distinguish the different source types.
* For more info see `lv_image_get_src_type()` in lv_img.c
* On big endian systems the order is reversed so cf and always_zero must be at
* the end of the struct.
*/
#if LV_BIG_ENDIAN_SYSTEM
typedef struct {
uint32_t reserved_2: 16; /*Reserved to be used later*/
@@ -102,18 +101,13 @@ typedef struct {
uint32_t h: 16;
uint32_t w: 16;
uint32_t flags: 16; /*Image flags, see `lv_image_flags_t`*/
uint32_t reserved_1: 8; /*Reserved by LVGL for later use*/
uint32_t always_zero : 3; /*It the upper bits of the first byte. Always zero to look like a
non-printable character*/
uint32_t cf : 5; /*Color format: See `lv_color_format_t`*/
uint32_t cf : 8; /*Color format: See `lv_color_format_t`*/
uint32_t magic: 8; /*Magic number. Must be LV_IMAGE_HEADER_MAGIC*/
} lv_image_header_t;
#else
typedef struct {
uint32_t cf : 5; /*Color format: See `lv_color_format_t`*/
uint32_t always_zero : 3; /*It the upper bits of the first byte. Always zero to look like a
non-printable character*/
uint32_t reserved_1: 8; /*Reserved by LVGL for later use*/
uint32_t magic: 8; /*Magic number. Must be LV_IMAGE_HEADER_MAGIC*/
uint32_t cf : 8; /*Color format: See `lv_color_format_t`*/
uint32_t flags: 16; /*Image flags, see `lv_image_flags_t`*/
uint32_t w: 16;

View File

@@ -123,6 +123,17 @@ lv_result_t lv_bin_decoder_info(lv_image_decoder_t * decoder, const void * src,
return LV_RESULT_INVALID;
}
/**
* @todo
* This is a temp backward compatibility solution after adding
* magic in image header.
*/
if(header->magic != LV_IMAGE_HEADER_MAGIC) {
LV_LOG_WARN("Legacy bin image detected: %s", (char *)src);
header->cf = header->magic;
header->magic = LV_IMAGE_HEADER_MAGIC;
}
/*File is always read to buf, thus data can be modified.*/
header->flags |= LV_IMAGE_FLAGS_MODIFIABLE;
}