1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00

lv_img_draw: support indexed images from file

This commit is contained in:
Gabor Kiss-Vamosi 2018-08-22 01:33:46 +02:00
parent 6f308aba11
commit b39a802336

View File

@ -229,9 +229,15 @@ lv_img_src_t lv_img_src_get_type(const void * src)
const uint8_t * u8_p = src;
/*The first byte shows the type of the image source*/
if(u8_p[0] >= 0x20 && u8_p[0] <= 0x7F) return LV_IMG_SRC_FILE; /*If it's an ASCII character then it's file name*/
else if(u8_p[0] >= 0x80) return LV_IMG_SRC_SYMBOL; /*Symbols begins after 0x7F*/
else return LV_IMG_SRC_VARIABLE; /*`lv_img_dsc_t` is design to the first byte < 0x20*/
if(u8_p[0] >= 0x20 && u8_p[0] <= 0x7F) {
return LV_IMG_SRC_FILE; /*If it's an ASCII character then it's file name*/
}
else if(u8_p[0] >= 0x80) {
return LV_IMG_SRC_SYMBOL; /*Symbols begins after 0x7F*/
}
else {
return LV_IMG_SRC_VARIABLE; /*`lv_img_dsc_t` is design to the first byte < 0x20*/
}
return LV_IMG_SRC_UNKNOWN;
}
@ -287,6 +293,7 @@ static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mas
if(img_data == LV_IMG_DECODER_OPEN_FAIL) {
LV_LOG_WARN("Image draw cannot open the image resource");
lv_img_decoder_close();
return LV_RES_INV;
}
/* The decoder open could open the image and gave the entire uncompressed image.
@ -350,66 +357,86 @@ static const uint8_t * lv_img_decoder_open(const void * src, const lv_style_t *
decoder_src = src;
decoder_style = style;
lv_img_src_t decoder_src_type = lv_img_src_get_type(src);
decoder_src_type = lv_img_src_get_type(src);
/*It will be a path to a file*/
if(decoder_src_type == LV_IMG_SRC_FILE) {
#if USE_LV_FILESYSTEM
lv_fs_res_t res = lv_fs_open(&decoder_file, src, LV_FS_MODE_RD);
if(res != LV_FS_RES_OK) {
decoder_src = NULL;
decoder_src_type = LV_IMG_SRC_UNKNOWN;
LV_LOG_WARN("Built-in image decoder can't open the src");
return NULL;
}
lv_res_t header_res;
header_res = lv_img_dsc_get_info(src, &decoder_header);
if(header_res == LV_RES_INV) {
lv_fs_close(&decoder_file);
decoder_src = NULL;
decoder_src_type = LV_IMG_SRC_UNKNOWN;
LV_LOG_WARN("Built-in image decoder can't get the header info");
return NULL;
}
#endif
}
/*It will be a variable in the RAM/ROM*/
else if(decoder_src_type == LV_IMG_SRC_VARIABLE) {
const lv_img_dsc_t * img_dsc = src;
lv_img_color_format_t cf = img_dsc->header.cf;
if(cf == LV_IMG_FORMAT_TRUE_COLOR ||
cf == LV_IMG_FORMAT_TRUE_COLOR_ALPHA ||
cf == LV_IMG_FORMAT_TRUE_COLOR_CHROMA_KEYED)
{
/*In case of uncompressed formats return the whole array*/
return img_dsc->data;
}
else if(cf == LV_IMG_FORMAT_INDEXED_1BIT ||
cf == LV_IMG_FORMAT_INDEXED_2BIT ||
cf == LV_IMG_FORMAT_INDEXED_4BIT ||
cf == LV_IMG_FORMAT_INDEXED_8BIT)
{
uint8_t px_size = lv_img_color_format_get_px_size(cf);
uint32_t palette_size = 1 << px_size;
lv_color24_t * cbuf = (lv_color24_t *) img_dsc->data;
uint32_t i;
for(i = 0; i < palette_size; i++) {
decoder_index_map[i] = LV_COLOR_MAKE(cbuf[i].red, cbuf[i].green, cbuf[i].blue);
}
return NULL;
}
else {
return NULL;
}
} else {
lv_res_t header_res;
header_res = lv_img_dsc_get_info(src, &decoder_header);
if(header_res == LV_RES_INV) {
lv_fs_close(&decoder_file);
decoder_src = NULL;
decoder_src_type = LV_IMG_SRC_UNKNOWN;
LV_LOG_WARN("Built-in image decoder don't know the src type");
return NULL;
LV_LOG_WARN("Built-in image decoder can't get the header info");
return LV_IMG_DECODER_OPEN_FAIL;
}
return NULL;
#if USE_LV_FILESYSTEM
/*Open the file if it's a file*/
if(decoder_src_type == LV_IMG_SRC_FILE) {
lv_fs_res_t res = lv_fs_open(&decoder_file, src, LV_FS_MODE_RD);
if(res != LV_FS_RES_OK) {
LV_LOG_WARN("Built-in image decoder can't open the file");
return LV_IMG_DECODER_OPEN_FAIL;
}
}
#else
LV_LOG_WARN("Image built-in decoder can read file because USE_LV_FILESYSTEM = 0");
return LV_IMG_DECODER_OPEN_FAIL;
#endif
/*Process the different color formats*/
lv_img_color_format_t cf = decoder_header.cf;
if(cf == LV_IMG_FORMAT_TRUE_COLOR ||
cf == LV_IMG_FORMAT_TRUE_COLOR_ALPHA ||
cf == LV_IMG_FORMAT_TRUE_COLOR_CHROMA_KEYED)
{
if(decoder_src_type == LV_IMG_SRC_VARIABLE) {
/*In case of uncompressed formats if the image stored in the ROM/RAM simply give it's pointer*/
return ((lv_img_dsc_t *)decoder_src)->data;
} else {
/*If it's file it need to be read line by line later*/
return NULL;
}
}
else if (cf == LV_IMG_FORMAT_INDEXED_1BIT ||
cf == LV_IMG_FORMAT_INDEXED_2BIT ||
cf == LV_IMG_FORMAT_INDEXED_4BIT ||
cf == LV_IMG_FORMAT_INDEXED_8BIT)
{
lv_color24_t palette_file[256];
lv_color24_t * palette_p = NULL;
uint8_t px_size = lv_img_color_format_get_px_size(cf);
uint32_t palette_size = 1 << px_size;
if(decoder_src_type == LV_IMG_SRC_FILE) {
#if USE_LV_FILESYSTEM
lv_fs_seek(&decoder_file, 4); /*Skip the header*/
lv_fs_read(&decoder_file, palette_file, palette_size * sizeof(lv_color24_t), NULL);
palette_p = palette_file;
#else
palette_file[0] = 0; /*Just to solve warnings*/
#endif
} else {
palette_p = (lv_color_t*)((lv_img_dsc_t *)decoder_src)->data;
}
uint32_t i;
for(i = 0; i < palette_size; i++) {
decoder_index_map[i] = LV_COLOR_MAKE(palette_p[i].red, palette_p[i].green, palette_p[i].blue);
}
return NULL;
}
else if (cf == LV_IMG_FORMAT_ALPHA_1BIT ||
cf == LV_IMG_FORMAT_ALPHA_2BIT ||
cf == LV_IMG_FORMAT_ALPHA_4BIT ||
cf == LV_IMG_FORMAT_ALPHA_8BIT)
{
return NULL; /*Nothing to process*/
}
else
{
LV_LOG_WARN("Image decoder open: unknown color format")
return LV_IMG_DECODER_OPEN_FAIL;
}
}
@ -427,49 +454,73 @@ static lv_res_t lv_img_decoder_read_line(lv_coord_t x, lv_coord_t y, lv_coord_t
return LV_RES_INV; /*It"s an error if not returned earlier*/
}
if(decoder_src_type == LV_IMG_SRC_FILE) {
#if USE_LV_FILESYSTEM
if(decoder_src_type == LV_IMG_SRC_FILE) {
#if USE_LV_FILESYSTEM
uint8_t px_size = lv_img_color_format_get_px_size(decoder_header.cf);
uint8_t px_size = lv_img_color_format_get_px_size(decoder_header.cf);
lv_fs_res_t res;
uint32_t pos = ((y * decoder_header.w + x) * px_size) >> 3;
res = lv_fs_seek(&decoder_file, pos);
if(res != LV_FS_RES_OK) {
LV_LOG_WARN("Built-in image decoder seek failed");
return false;
}
uint32_t btr = len * (px_size >> 3);
uint32_t br = 0;
lv_fs_read(&decoder_file, buf, btr, &br);
if(res != LV_FS_RES_OK || btr != br) {
LV_LOG_WARN("Built-in image decoder read failed");
return false;
}
#endif
} else {
const lv_img_dsc_t * img_dsc = decoder_src;
lv_fs_res_t res;
if(img_dsc->header.cf == LV_IMG_FORMAT_ALPHA_1BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_ALPHA_2BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_ALPHA_4BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_ALPHA_8BIT)
{
lv_img_built_in_decoder_line_alpha(x, y, len, buf);
} else if(img_dsc->header.cf == LV_IMG_FORMAT_INDEXED_1BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_INDEXED_2BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_INDEXED_4BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_INDEXED_8BIT)
{
lv_img_built_in_decoder_line_indexed(x, y, len, buf);
} else {
LV_LOG_WARN("Built-in image decoder not supports the color format");
return false;
}
}
if(decoder_header.cf == LV_IMG_FORMAT_TRUE_COLOR ||
decoder_header.cf == LV_IMG_FORMAT_TRUE_COLOR_ALPHA ||
decoder_header.cf == LV_IMG_FORMAT_TRUE_COLOR_CHROMA_KEYED)
{
uint32_t pos = ((y * decoder_header.w + x) * px_size) >> 3;
res = lv_fs_seek(&decoder_file, pos);
if(res != LV_FS_RES_OK) {
LV_LOG_WARN("Built-in image decoder seek failed");
return false;
}
uint32_t btr = len * (px_size >> 3);
uint32_t br = 0;
lv_fs_read(&decoder_file, buf, btr, &br);
if(res != LV_FS_RES_OK || btr != br) {
LV_LOG_WARN("Built-in image decoder read failed");
return false;
}
}
else if(decoder_header.cf == LV_IMG_FORMAT_ALPHA_1BIT ||
decoder_header.cf == LV_IMG_FORMAT_ALPHA_2BIT ||
decoder_header.cf == LV_IMG_FORMAT_ALPHA_4BIT ||
decoder_header.cf == LV_IMG_FORMAT_ALPHA_8BIT)
{
lv_img_built_in_decoder_line_alpha(x, y, len, buf);
} else if(decoder_header.cf == LV_IMG_FORMAT_INDEXED_1BIT ||
decoder_header.cf == LV_IMG_FORMAT_INDEXED_2BIT ||
decoder_header.cf == LV_IMG_FORMAT_INDEXED_4BIT ||
decoder_header.cf == LV_IMG_FORMAT_INDEXED_8BIT)
{
lv_img_built_in_decoder_line_indexed(x, y, len, buf);
} else {
LV_LOG_WARN("Built-in image decoder read not supports the color format");
return false;
}
#else
LV_LOG_WARN("Image built-in decoder can't read file because USE_LV_FILESYSTEM = 0");
return false;
#endif
} else if (decoder_src_type == LV_IMG_SRC_VARIABLE) {
const lv_img_dsc_t * img_dsc = decoder_src;
return true;
if(img_dsc->header.cf == LV_IMG_FORMAT_ALPHA_1BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_ALPHA_2BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_ALPHA_4BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_ALPHA_8BIT)
{
lv_img_built_in_decoder_line_alpha(x, y, len, buf);
} else if(img_dsc->header.cf == LV_IMG_FORMAT_INDEXED_1BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_INDEXED_2BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_INDEXED_4BIT ||
img_dsc->header.cf == LV_IMG_FORMAT_INDEXED_8BIT)
{
lv_img_built_in_decoder_line_indexed(x, y, len, buf);
} else {
LV_LOG_WARN("Built-in image decoder not supports the color format");
return false;
}
}
return true;
}
static void lv_img_decoder_close(void)
@ -574,43 +625,53 @@ static lv_res_t lv_img_built_in_decoder_line_alpha(lv_coord_t x, lv_coord_t y, l
static lv_res_t lv_img_built_in_decoder_line_indexed(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf)
{
const lv_img_dsc_t * img_dsc = decoder_src;
const uint8_t * data_tmp = img_dsc->data;
uint8_t px_size = lv_img_color_format_get_px_size(img_dsc->header.cf);
uint8_t px_size = lv_img_color_format_get_px_size(decoder_header.cf);
uint16_t mask = (1 << px_size) - 1; /*E.g. px_size = 2; mask = 0x03*/
lv_coord_t w = 0;
int8_t pos = 0;
switch(img_dsc->header.cf) {
uint32_t ofs = 0;
switch(decoder_header.cf) {
case LV_IMG_FORMAT_INDEXED_1BIT:
w = (img_dsc->header.w >> 3); /*E.g. w = 20 -> w = 2 + 1*/
if(img_dsc->header.w & 0x7) w++;
data_tmp += w * y + (x >> 3); /*First pixel*/
data_tmp += 8; /*Skip the palette*/
w = (decoder_header.w >> 3); /*E.g. w = 20 -> w = 2 + 1*/
if(decoder_header.w & 0x7) w++;
ofs += w * y + (x >> 3); /*First pixel*/
ofs += 8; /*Skip the palette*/
pos = 7 - (x & 0x7);
break;
case LV_IMG_FORMAT_INDEXED_2BIT:
w = (img_dsc->header.w >> 2); /*E.g. w = 13 -> w = 3 + 1 (bytes)*/
if(img_dsc->header.w & 0x3) w++;
data_tmp += w * y + (x >> 2); /*First pixel*/
data_tmp += 16; /*Skip the palette*/
w = (decoder_header.w >> 2); /*E.g. w = 13 -> w = 3 + 1 (bytes)*/
if(decoder_header.w & 0x3) w++;
ofs += w * y + (x >> 2); /*First pixel*/
ofs += 16; /*Skip the palette*/
pos = 6 - ((x & 0x3) * 2);
break;
case LV_IMG_FORMAT_INDEXED_4BIT:
w = (img_dsc->header.w >> 1); /*E.g. w = 13 -> w = 6 + 1 (bytes)*/
if(img_dsc->header.w & 0x1) w++;
data_tmp += w * y + (x >> 1); /*First pixel*/
data_tmp += 64; /*Skip the palette*/
w = (decoder_header.w >> 1); /*E.g. w = 13 -> w = 6 + 1 (bytes)*/
if(decoder_header.w & 0x1) w++;
ofs += w * y + (x >> 1); /*First pixel*/
ofs += 64; /*Skip the palette*/
pos = 4 - ((x & 0x1) * 4);
break;
case LV_IMG_FORMAT_INDEXED_8BIT:
w = img_dsc->header.w; /*E.g. x = 7 -> w = 7 (bytes)*/
data_tmp += w * y + x; /*First pixel*/
data_tmp += 1024; /*Skip the palette*/
w = decoder_header.w; /*E.g. x = 7 -> w = 7 (bytes)*/
ofs += w * y + x; /*First pixel*/
ofs += 1024; /*Skip the palette*/
pos = 0;
break;
}
uint8_t fs_buf[1024];
const uint8_t * data_tmp = NULL;
if(decoder_src_type == LV_IMG_SRC_VARIABLE) {
const lv_img_dsc_t * img_dsc = decoder_src;
data_tmp = img_dsc->data + ofs;
} else {
lv_fs_seek(&decoder_file, ofs + 4); /*+4 to skip the header*/
lv_fs_read(&decoder_file, fs_buf, w, NULL);
data_tmp = fs_buf;
}
uint8_t byte_act = 0;
uint8_t val_act;
uint32_t i;