From 5e3fb688355d06aa8fdf07499e1e841571101c8b Mon Sep 17 00:00:00 2001 From: Neo Xu Date: Tue, 19 Dec 2023 19:54:30 +0800 Subject: [PATCH] refactor(image): add magic to image header (#5051) Signed-off-by: Xu Xingliang --- examples/libs/gif/lv_example_gif_1.py | 2 +- examples/libs/lodepng/lv_example_lodepng_1.py | 2 +- scripts/LVGLImage.py | 3 ++- src/draw/lv_image_buf.h | 24 +++++++------------ src/libs/bin_decoder/lv_bin_decoder.c | 11 +++++++++ 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/examples/libs/gif/lv_example_gif_1.py b/examples/libs/gif/lv_example_gif_1.py index f11468b1f..54256d9c8 100755 --- a/examples/libs/gif/lv_example_gif_1.py +++ b/examples/libs/gif/lv_example_gif_1.py @@ -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, } diff --git a/examples/libs/lodepng/lv_example_lodepng_1.py b/examples/libs/lodepng/lv_example_lodepng_1.py index 0a3e646df..aa17a5824 100755 --- a/examples/libs/lodepng/lv_example_lodepng_1.py +++ b/examples/libs/lodepng/lv_example_lodepng_1.py @@ -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, } diff --git a/scripts/LVGLImage.py b/scripts/LVGLImage.py index ae708e4b3..86e0e4c9e 100755 --- a/scripts/LVGLImage.py +++ b/scripts/LVGLImage.py @@ -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}, diff --git a/src/draw/lv_image_buf.h b/src/draw/lv_image_buf.h index 5ac89f74f..d3482cc5f 100644 --- a/src/draw/lv_image_buf.h +++ b/src/draw/lv_image_buf.h @@ -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; diff --git a/src/libs/bin_decoder/lv_bin_decoder.c b/src/libs/bin_decoder/lv_bin_decoder.c index 53837d1bc..f4c00a55d 100644 --- a/src/libs/bin_decoder/lv_bin_decoder.c +++ b/src/libs/bin_decoder/lv_bin_decoder.c @@ -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; }