Skip to content

Commit

Permalink
feat(image): add LZ4 compressed binary image support (lvgl#4873)
Browse files Browse the repository at this point in the history
Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
  • Loading branch information
XuNeo authored Nov 30, 2023
1 parent 2dadaae commit 0233247
Show file tree
Hide file tree
Showing 20 changed files with 92 additions and 1 deletion.
11 changes: 10 additions & 1 deletion scripts/LVGLImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
raise ImportError("Need pypng package, do `pip3 install pypng`")


try:
import lz4.block
except ImportError:
raise ImportError("Need lz4 package, do `pip3 install lz4`")


def uint8_t(val) -> bytes:
return val.to_bytes(1, byteorder='little')

Expand Down Expand Up @@ -94,6 +100,7 @@ def convert(self, filename) -> bytes:
class CompressMethod(Enum):
NONE = 0x00
RLE = 0x01
LZ4 = 0x02


class ColorFormat(Enum):
Expand Down Expand Up @@ -365,6 +372,8 @@ def _compress(self, raw_data: bytes) -> bytearray:
pad = b'\x00' * (self.blk_size - self.raw_data_len % self.blk_size)
self.raw_data_len += len(pad)
compressed = RLEImage().rle_compress(raw_data + pad, self.blk_size)
elif self.compress == CompressMethod.LZ4:
compressed = lz4.block.compress(raw_data, store_size=False)
else:
raise ParameterError(f"Invalid compress method: {self.compress}")

Expand Down Expand Up @@ -1069,7 +1078,7 @@ def main():
parser.add_argument('--compress',
help=("Binary data compress method, default to NONE"),
default="NONE",
choices=["NONE", "RLE"])
choices=["NONE", "RLE", "LZ4"])

parser.add_argument('--align',
help="stride alignment in bytes for bin image",
Expand Down
18 changes: 18 additions & 0 deletions src/libs/bin_decoder/lv_bin_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../../stdlib/lv_string.h"
#include "../../stdlib/lv_sprintf.h"
#include "../../libs/rle/lv_rle.h"
#include "../../libs/lz4/lz4.h"

/*********************
* DEFINES
Expand Down Expand Up @@ -846,6 +847,23 @@ static lv_result_t decompress_image(lv_image_decoder_dsc_t * dsc, const lv_image
return LV_RESULT_INVALID;
#endif
}
else if(compressed->method == LV_IMAGE_COMPRESS_LZ4) {
#if LV_USE_LZ4
const char * input = (const char *)compressed->data;
char * output = (char *)decompressed;
int len;
len = LZ4_decompress_safe(input, output, input_len, out_len);
if(len < 0 || (uint32_t)len != compressed->decompressed_size) {
LV_LOG_WARN("Decompress failed: %" LV_PRId32 ", got: %" LV_PRId32, out_len, len);
lv_draw_buf_free(decompressed);
return LV_RESULT_INVALID;
}
#else
LV_LOG_WARN("LZ4 decompress is not enabled");
lv_draw_buf_free(decompressed);
return LV_RESULT_INVALID;
#endif
}

decoder_data->decompressed = decompressed; /*Free on decoder close*/
return LV_RESULT_OK;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/src/lv_test_conf_full.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#define LV_USE_SYSMON 1
#define LV_USE_SNAPSHOT 1
#define LV_USE_THORVG_INTERNAL 1
#define LV_USE_LZ4 1
#define LV_USE_LZ4_INTERNAL 1
#define LV_USE_VECTOR_GRAPHIC 1

Expand Down
63 changes: 63 additions & 0 deletions tests/src/test_cases/draw/test_image_formats.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,67 @@ void test_image_rle_compressed_decode_rotate_recolor(void)
#endif
}

void test_image_lz4_compressed_decode(void)
{
#if LV_USE_LZ4
img_create("lz4A1", "A:src/test_files/lz4_compressed/cogwheel.A1.bin", false, false);
img_create("lz4A2", "A:src/test_files/lz4_compressed/cogwheel.A2.bin", false, false);
img_create("lz4A4", "A:src/test_files/lz4_compressed/cogwheel.A4.bin", false, false);
img_create("lz4A8", "A:src/test_files/lz4_compressed/cogwheel.A8.bin", false, false);
img_create("lz4I1", "A:src/test_files/lz4_compressed/cogwheel.I1.bin", false, false);
img_create("lz4I2", "A:src/test_files/lz4_compressed/cogwheel.I2.bin", false, false);
img_create("lz4I4", "A:src/test_files/lz4_compressed/cogwheel.I4.bin", false, false);
img_create("lz4I8", "A:src/test_files/lz4_compressed/cogwheel.I8.bin", false, false);
img_create("lz4RGB565A8", "A:src/test_files/lz4_compressed/cogwheel.RGB565A8.bin", false, false);
img_create("lz4RGB565", "A:src/test_files/lz4_compressed/cogwheel.RGB565.bin", false, false);
img_create("lz4RGB888", "A:src/test_files/lz4_compressed/cogwheel.RGB888.bin", false, false);
img_create("lz4XRGB8888", "A:src/test_files/lz4_compressed/cogwheel.XRGB8888.bin", false, false);
img_create("lz4ARGB8888", "A:src/test_files/lz4_compressed/cogwheel.ARGB8888.bin", false, false);

TEST_ASSERT_EQUAL_SCREENSHOT("draw/image_format_lz4_compressed.png");
#endif
}

void test_image_lz4_compressed_decode_rotate(void)
{
#if LV_USE_LZ4
img_create("lz4A1", "A:src/test_files/lz4_compressed/cogwheel.A1.bin", true, false);
img_create("lz4A2", "A:src/test_files/lz4_compressed/cogwheel.A2.bin", true, false);
img_create("lz4A4", "A:src/test_files/lz4_compressed/cogwheel.A4.bin", true, false);
img_create("lz4A8", "A:src/test_files/lz4_compressed/cogwheel.A8.bin", true, false);
img_create("lz4I1", "A:src/test_files/lz4_compressed/cogwheel.I1.bin", true, false);
img_create("lz4I2", "A:src/test_files/lz4_compressed/cogwheel.I2.bin", true, false);
img_create("lz4I4", "A:src/test_files/lz4_compressed/cogwheel.I4.bin", true, false);
img_create("lz4I8", "A:src/test_files/lz4_compressed/cogwheel.I8.bin", true, false);
img_create("lz4RGB565A8", "A:src/test_files/lz4_compressed/cogwheel.RGB565A8.bin", true, false);
img_create("lz4RGB565", "A:src/test_files/lz4_compressed/cogwheel.RGB565.bin", true, false);
img_create("lz4RGB888", "A:src/test_files/lz4_compressed/cogwheel.RGB888.bin", true, false);
img_create("lz4XRGB8888", "A:src/test_files/lz4_compressed/cogwheel.XRGB8888.bin", true, false);
img_create("lz4ARGB8888", "A:src/test_files/lz4_compressed/cogwheel.ARGB8888.bin", true, false);

TEST_ASSERT_EQUAL_SCREENSHOT("draw/image_format_lz4_compressed_rotate.png");
#endif
}

void test_image_lz4_compressed_decode_rotate_recolor(void)
{
#if LV_USE_LZ4
img_create("lz4A1", "A:src/test_files/lz4_compressed/cogwheel.A1.bin", true, true);
img_create("lz4A2", "A:src/test_files/lz4_compressed/cogwheel.A2.bin", true, true);
img_create("lz4A4", "A:src/test_files/lz4_compressed/cogwheel.A4.bin", true, true);
img_create("lz4A8", "A:src/test_files/lz4_compressed/cogwheel.A8.bin", true, true);
img_create("lz4I1", "A:src/test_files/lz4_compressed/cogwheel.I1.bin", true, true);
img_create("lz4I2", "A:src/test_files/lz4_compressed/cogwheel.I2.bin", true, true);
img_create("lz4I4", "A:src/test_files/lz4_compressed/cogwheel.I4.bin", true, true);
img_create("lz4I8", "A:src/test_files/lz4_compressed/cogwheel.I8.bin", true, true);
img_create("lz4RGB565A8", "A:src/test_files/lz4_compressed/cogwheel.RGB565A8.bin", true, true);
img_create("lz4RGB565", "A:src/test_files/lz4_compressed/cogwheel.RGB565.bin", true, true);
img_create("lz4RGB888", "A:src/test_files/lz4_compressed/cogwheel.RGB888.bin", true, true);
img_create("lz4XRGB8888", "A:src/test_files/lz4_compressed/cogwheel.XRGB8888.bin", true, true);
img_create("lz4ARGB8888", "A:src/test_files/lz4_compressed/cogwheel.ARGB8888.bin", true, true);

TEST_ASSERT_EQUAL_SCREENSHOT("draw/image_format_lz4_compressed_rotate_recolor.png");
#endif
}

#endif
Binary file added tests/src/test_files/lz4_compressed/cogwheel.A1.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/src/test_files/lz4_compressed/cogwheel.I1.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 0233247

Please sign in to comment.