1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

Merge branch 'img_decoder' of https://github.com/littlevgl/lvgl into img_decoder

This commit is contained in:
Gabor Kiss-Vamosi 2018-08-26 14:22:26 +02:00
commit f0753a82b5
4 changed files with 286 additions and 157 deletions

View File

@ -49,7 +49,6 @@ void (*map_fp)(const lv_area_t * cords_p, const lv_area_t * mask_p,
lv_color_t recolor, lv_opa_t recolor_opa) = lv_rmap;
#endif
/**********************
* MACROS
**********************/

View File

@ -32,6 +32,7 @@ static lv_res_t lv_img_built_in_decoder_line_indexed(lv_coord_t x, lv_coord_t y,
/**********************
* STATIC VARIABLES
**********************/
static bool decoder_custom;
static const void * decoder_src;
static lv_img_src_t decoder_src_type;
static lv_img_header_t decoder_header;
@ -39,10 +40,10 @@ static const lv_style_t * decoder_style;
static lv_fs_file_t decoder_file;
static lv_color_t decoder_index_map[256] = {LV_COLOR_RED, LV_COLOR_BLUE, LV_COLOR_GREEN, LV_COLOR_PURPLE};
lv_res_t (*lv_img_dsc_get_info_custom)(const char * src, lv_img_header_t * header);
const uint8_t * (*lv_img_decoder_open_custom)(const char * src, const lv_style_t * style);
lv_res_t (*lv_img_decoder_read_line_custom)(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf);
lv_res_t (*lv_img_decoder_close_custom)(void);
static lv_img_decoder_info_f_t lv_img_decoder_info_custom;
static lv_img_decoder_open_f_t lv_img_decoder_open_custom;
static lv_img_decoder_read_line_f_t lv_img_decoder_read_line_custom;
static lv_img_decoder_close_f_t lv_img_decoder_close_custom;
/**********************
* MACROS
@ -93,18 +94,18 @@ lv_res_t lv_img_dsc_get_info(const char * src, lv_img_header_t * header)
{
header->always_zero = 0;
/*Try to get info with the custom functions first*/
if(lv_img_dsc_get_info_custom) {
if(lv_img_decoder_info_custom) {
lv_res_t custom_res;
custom_res = lv_img_dsc_get_info_custom(src, header);
if(custom_res == LV_RES_OK) return LV_RES_OK; /*Custom info supported this source*/
custom_res = lv_img_decoder_info_custom(src, header);
if(custom_res == LV_RES_OK) return LV_RES_OK; /*Custom info has supported this source*/
}
lv_img_src_t src_type = lv_img_src_get_type(src);
if(src_type == LV_IMG_SRC_VARIABLE) {
header->color_format =
header->cf =
header->w = ((lv_img_dsc_t *)src)->header.w;
header->h = ((lv_img_dsc_t *)src)->header.h;
header->color_format = ((lv_img_dsc_t *)src)->header.color_format;
header->cf = ((lv_img_dsc_t *)src)->header.cf;
}
#if USE_LV_FILESYSTEM
else if(src_type == LV_IMG_SRC_FILE) {
@ -120,7 +121,7 @@ lv_res_t lv_img_dsc_get_info(const char * src, lv_img_header_t * header)
if(res != LV_FS_RES_OK || rn != sizeof(lv_img_header_t)) {
header->w = LV_DPI;
header->h = LV_DPI;
header->color_format = LV_IMG_FORMAT_UNKOWN;
header->cf = LV_IMG_FORMAT_UNKOWN;
}
lv_fs_close(&file);
@ -132,7 +133,7 @@ lv_res_t lv_img_dsc_get_info(const char * src, lv_img_header_t * header)
header->h = 1;
/* Symbols always have transparent parts. Important because of cover check in the design function.
* The actual value doesn't matter because lv_draw_label will draw it*/
header->color_format = LV_IMG_FORMAT_ALPHA_1BIT;
header->cf = LV_IMG_FORMAT_ALPHA_1BIT;
} else {
LV_LOG_WARN("Image get info found unknown src type");
return false;
@ -183,6 +184,7 @@ bool lv_img_color_format_is_chroma_keyed(lv_img_color_format_t cf)
{
switch(cf) {
case LV_IMG_FORMAT_TRUE_COLOR_CHROMA_KEYED:
case LV_IMG_FORMAT_RAW_CHROMA_KEYED:
case LV_IMG_FORMAT_INDEXED_1BIT:
case LV_IMG_FORMAT_INDEXED_2BIT:
case LV_IMG_FORMAT_INDEXED_4BIT:
@ -200,6 +202,7 @@ bool lv_img_color_format_has_alpha(lv_img_color_format_t cf)
{
switch(cf) {
case LV_IMG_FORMAT_TRUE_COLOR_ALPHA:
case LV_IMG_FORMAT_RAW_ALPHA:
case LV_IMG_FORMAT_ALPHA_1BIT:
case LV_IMG_FORMAT_ALPHA_2BIT:
case LV_IMG_FORMAT_ALPHA_4BIT:
@ -226,13 +229,34 @@ 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;
}
/**
* Set custom decoder functions. See the typdefs of the function typed above for more info about them
* @param info_fp info get function
* @param open_fp open function
* @param read_fp read line function
* @param close_fp clode function
*/
void lv_img_decoder_set_custom(lv_img_decoder_info_f_t info_fp, lv_img_decoder_open_f_t open_fp,
lv_img_decoder_read_line_f_t read_fp, lv_img_decoder_close_f_t close_fp)
{
lv_img_decoder_info_custom = info_fp;
lv_img_decoder_open_custom = open_fp;
lv_img_decoder_read_line_custom= read_fp;
lv_img_decoder_close_custom = close_fp;
}
/**********************
@ -262,13 +286,14 @@ static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mas
return LV_RES_INV;
}
bool chroma_keyed = lv_img_color_format_is_chroma_keyed(header.color_format);
bool alpha_byte = lv_img_color_format_has_alpha(header.color_format);
bool chroma_keyed = lv_img_color_format_is_chroma_keyed(header.cf);
bool alpha_byte = lv_img_color_format_has_alpha(header.cf);
const uint8_t * img_data = lv_img_decoder_open(src, style);
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.
@ -318,141 +343,195 @@ static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * mas
static const uint8_t * lv_img_decoder_open(const void * src, const lv_style_t * style)
{
decoder_custom = false;
/*Try to open with the custom functions first*/
if(lv_img_decoder_open_custom) {
const uint8_t * custom_res;
custom_res = lv_img_decoder_open_custom(src, style);
if(custom_res != LV_IMG_DECODER_OPEN_FAIL) return custom_res; /*Custom open supported this source*/
if(custom_res != LV_IMG_DECODER_OPEN_FAIL) {
decoder_custom = true; /*Mark that custom decoder function should be used for this img source.*/
return custom_res; /*Custom open supported this source*/
}
}
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.color_format;
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;
}
}
static lv_res_t lv_img_decoder_read_line(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf)
{
/*Try to read the line with the custom functions first*/
if(lv_img_decoder_read_line_custom) {
lv_res_t custom_res;
custom_res = lv_img_decoder_read_line_custom(x, y, len, buf);
if(custom_res == LV_RES_OK) return LV_RES_OK; /*Custom read line supported this source*/
/*Try to read the line with the custom functions*/
if(decoder_custom) {
if(lv_img_decoder_read_line_custom) {
lv_res_t custom_res;
custom_res = lv_img_decoder_read_line_custom(x, y, len, buf);
return custom_res;
} else {
LV_LOG_WARN("Image open with custom decoder but read not supported")
}
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.color_format);
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.color_format == LV_IMG_FORMAT_ALPHA_1BIT ||
img_dsc->header.color_format == LV_IMG_FORMAT_ALPHA_2BIT ||
img_dsc->header.color_format == LV_IMG_FORMAT_ALPHA_4BIT ||
img_dsc->header.color_format == LV_IMG_FORMAT_ALPHA_8BIT)
{
lv_img_built_in_decoder_line_alpha(x, y, len, buf);
} if(img_dsc->header.color_format == LV_IMG_FORMAT_INDEXED_1BIT ||
img_dsc->header.color_format == LV_IMG_FORMAT_INDEXED_2BIT ||
img_dsc->header.color_format == LV_IMG_FORMAT_INDEXED_4BIT ||
img_dsc->header.color_format == 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)
{
/*Try to close with the custom functions first*/
if(lv_img_decoder_close_custom) {
lv_res_t custom_res;
custom_res = lv_img_decoder_close_custom();
if(custom_res == LV_RES_OK) return; /*Custom close supported this source*/
/*Try to close with the custom functions*/
if(decoder_custom) {
if(lv_img_decoder_close_custom) lv_img_decoder_close_custom();
return;
}
/*It was opened with built-in decoder*/
if(decoder_src) {
if(decoder_src_type == LV_IMG_SRC_FILE) {
lv_fs_close(&decoder_file);
@ -491,12 +570,12 @@ static lv_res_t lv_img_built_in_decoder_line_alpha(lv_coord_t x, lv_coord_t y, l
const lv_img_dsc_t * img_dsc = decoder_src;
const uint8_t * data_tmp = img_dsc->data;
const lv_opa_t * opa_table = NULL;
uint8_t px_size = lv_img_color_format_get_px_size(img_dsc->header.color_format);
uint8_t px_size = lv_img_color_format_get_px_size(img_dsc->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.color_format) {
switch(img_dsc->header.cf) {
case LV_IMG_FORMAT_ALPHA_1BIT:
w = (img_dsc->header.w >> 3); /*E.g. w = 20 -> w = 2 + 1*/
if(img_dsc->header.w & 0x7) w++;
@ -531,7 +610,7 @@ static lv_res_t lv_img_built_in_decoder_line_alpha(lv_coord_t x, lv_coord_t y, l
val_act = (data_tmp[byte_act] & (mask << pos)) >> pos;
buf[i * LV_IMG_PX_SIZE_ALPHA_BYTE + LV_IMG_PX_SIZE_ALPHA_BYTE - 1] =
img_dsc->header.color_format == LV_IMG_FORMAT_ALPHA_8BIT ? val_act : opa_table[val_act];
img_dsc->header.cf == LV_IMG_FORMAT_ALPHA_8BIT ? val_act : opa_table[val_act];
pos -= px_size;
if(pos < 0) {
@ -546,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.color_format);
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.color_format) {
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;

View File

@ -30,10 +30,10 @@ typedef struct {
/* The first 8 bit is very important to distinguish the different source types.
* For more info see `lv_img_get_src_type()` in lv_img.c */
uint32_t color_format :5; /*See: lv_img_px_format*/
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_img_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 compression :2;
uint32_t reserved :2; /*Reserved to be used later*/
uint32_t w:11; /*Width of the image map*/
uint32_t h:11; /*Height of the image map*/
@ -51,6 +51,8 @@ typedef enum {
LV_IMG_FORMAT_UNKOWN = 0,
LV_IMG_FORMAT_RAW, /*Contains the file as it is. Needs custom decoder function*/
LV_IMG_FORMAT_RAW_ALPHA, /*Contains the file as it is. The image has alpha. Needs custom decoder function*/
LV_IMG_FORMAT_RAW_CHROMA_KEYED, /*Contains the file as it is. The image is chroma keyed. Needs custom decoder function*/
LV_IMG_FORMAT_TRUE_COLOR, /*Color format and depth should match with LV_COLOR settings*/
LV_IMG_FORMAT_TRUE_COLOR_ALPHA, /*Same as `LV_IMG_FORMAT_TRUE_COLOR` but every pixel has an alpha byte*/
@ -72,9 +74,48 @@ typedef enum {
typedef struct
{
lv_img_header_t header;
uint32_t data_size;
const uint8_t * data;
} lv_img_dsc_t;
/* Decoder function definitions */
/**
* Get info from an image and store in the `header`
* @param src the image source. Can be a pointer to a C array or a file name (Use `lv_img_src_get_type` to determine the type)
* @param header store the info here
* @return LV_RES_OK: info written correctly; LV_RES_INV: failed
*/
typedef lv_res_t (*lv_img_decoder_info_f_t)(const void * src, lv_img_header_t * header);
/**
* Open an image for decoding. Prepare it as it is required to read it later
* @param src the image source. Can be a pointer to a C array or a file name (Use `lv_img_src_get_type` to determine the type)
* @param style the style of image (maybe it will be required to determine a color or something)
* @return there are 3 possible return values:
* 1) buffer with the decoded image
* 2) if can decode the whole image NULL. decoder_read_line will be called to read the image line-by-line
* 3) LV_IMG_DECODER_OPEN_FAIL if the image format is unknown to the decoder or an error occurred
*/
typedef const uint8_t * (*lv_img_decoder_open_f_t)(const void * src, const lv_style_t * style);
/**
* Decode `len` pixels starting from the given `x`, `y` coordinates and store them in `buf`.
* Required only if the "open" function can't return with the whole decoded pixel array.
* @param x start x coordinate
* @param y startt y coordinate
* @param len number of pixels to decode
* @param buf a buffer to store the decoded pixels
* @return LV_RES_OK: ok; LV_RES_INV: failed
*/
typedef lv_res_t (*lv_img_decoder_read_line_f_t)(lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf);
/**
* Close the pending decoding. Free resources etc.
*/
typedef void (*lv_img_decoder_close_f_t)(void);
/**********************
* GLOBAL PROTOTYPES
**********************/
@ -101,6 +142,16 @@ void lv_draw_img(const lv_area_t * coords, const lv_area_t * mask,
*/
lv_img_src_t lv_img_src_get_type(const void * src);
/**
* Set custom decoder functions. See the typdefs of the function typed above for more info about them
* @param info_fp info get function
* @param open_fp open function
* @param read_fp read line function
* @param close_fp clode function
*/
void lv_img_decoder_set_custom(lv_img_decoder_info_f_t info_fp, lv_img_decoder_open_f_t open_fp,
lv_img_decoder_read_line_f_t read_fp, lv_img_decoder_close_f_t close_fp);
lv_res_t lv_img_dsc_get_info(const char * src, lv_img_header_t * header);
uint8_t lv_img_color_format_get_px_size(lv_img_color_format_t cf);
@ -109,16 +160,6 @@ bool lv_img_color_format_is_chroma_keyed(lv_img_color_format_t cf);
bool lv_img_color_format_has_alpha(lv_img_color_format_t cf);
/**
* Get the type of an image source
* @param src pointer to an image source:
* - pointer to an 'lv_img_t' variable (image stored internally and compiled into the code)
* - a path to a file (e.g. "S:/folder/image.bin")
* - or a symbol (e.g. SYMBOL_CLOSE)
* @return type of the image source LV_IMG_SRC_VARIABLE/FILE/SYMBOL/UNKOWN
*/
lv_img_src_t lv_img_get_src_type(const void * src);
/**********************
* MACROS

View File

@ -167,7 +167,7 @@ void lv_img_set_src(lv_obj_t * img, const void * src_img)
ext->src_type = src_type;
ext->w = header.w;
ext->h = header.h;
ext->cf = header.color_format;
ext->cf = header.cf;
if(lv_img_get_auto_size(img) != false) {
lv_obj_set_size(img, ext->w, ext->h);