support load image from filesystem

This commit is contained in:
lixianjing 2019-06-13 15:15:12 +08:00
parent 743bcc1882
commit 260cd73f57
6 changed files with 83 additions and 6 deletions

View File

@ -1,4 +1,8 @@
# 最新动态
* 2019/06/13
* 修改lcd\_sdl2\_resize的bug(感谢尧燊提供补丁)。
* image/svg/gif支持从文件系统中加载图片。
* 2019/06/12
* 修改全局字体缩放的问题。
* 修改 scroll\_view\_on\_layout\_children 在特殊情况的 BUG。

View File

@ -208,7 +208,44 @@ static asset_info_t* try_load_assets(assets_manager_t* am, const char* name, con
return NULL;
}
asset_info_t* assets_manager_load(assets_manager_t* am, asset_type_t type, const char* name) {
static uint16_t subtype_from_extname(const char* extname) {
uint16_t subtype = 0;
return_value_if_fail(extname != NULL, 0);
if(tk_str_ieq(extname, ".gif")) {
subtype = ASSET_TYPE_IMAGE_GIF;
} else if(tk_str_ieq(extname, ".png")) {
subtype = ASSET_TYPE_IMAGE_PNG;
} else if(tk_str_ieq(extname, ".bmp")) {
subtype = ASSET_TYPE_IMAGE_BMP;
} else if(tk_str_ieq(extname, ".bsvg")) {
subtype = ASSET_TYPE_IMAGE_BSVG;
} else if(tk_str_ieq(extname, ".jpg")) {
subtype = ASSET_TYPE_IMAGE_JPG;
} else if(tk_str_ieq(extname, ".jpeg")) {
subtype = ASSET_TYPE_IMAGE_JPG;
} else if(tk_str_ieq(extname, "ttf")) {
subtype = ASSET_TYPE_FONT_TTF;
} else {
log_debug("not supported %s\n", extname);
}
return subtype;
}
asset_info_t* assets_manager_load_file(assets_manager_t* am, asset_type_t type, const char* path) {
if (file_exist(path)) {
int32_t size = file_get_size(path);
const char* extname = strrchr(path, '.');
uint16_t subtype = subtype_from_extname(extname);
return load_asset(type, subtype, size, path, path);
}
return NULL;
}
asset_info_t* assets_manager_load_asset(assets_manager_t* am, asset_type_t type, const char* name) {
asset_info_t* info = NULL;
switch (type) {
case ASSET_TYPE_FONT: {
@ -303,6 +340,14 @@ asset_info_t* assets_manager_load(assets_manager_t* am, asset_type_t type, const
return info;
}
asset_info_t* assets_manager_load(assets_manager_t* am, asset_type_t type, const char* name) {
if(strncmp(name, STR_SCHEMA_FILE, strlen(STR_SCHEMA_FILE)) == 0) {
return assets_manager_load_file(am, type, name + strlen(STR_SCHEMA_FILE));
} else {
return assets_manager_load_asset(am, type, name);
}
}
#else
asset_info_t* assets_manager_load(assets_manager_t* am, asset_type_t type, const char* name) {
(void)type;

View File

@ -44,6 +44,13 @@ BEGIN_C_DECLS
*
* > 使image\_t
*
* "file://"
*
*```
* <image draw_type="center" image="file://./demos/assets/raw/images/xx/flag_CN.png" />
* <gif image="file://./demos/assets/raw/images/x2/bee.gif" />
* <svg image="file://./demos/assets/raw/images/svg/china.bsvg" />
* ```
*/
typedef struct _image_base_t {
widget_t widget;
@ -107,6 +114,9 @@ typedef struct _image_base_t {
/**
* @method image_base_set_image
*
*
*> "file://"
*
* @annotation ["scriptable"]
* @param {widget_t*} widget image对象
* @param {char*} name

View File

@ -27,7 +27,7 @@
typedef struct _bitmap_cache_t {
bitmap_t image;
char name[TK_NAME_LEN + 1];
char* name;
uint32_t access_count;
uint32_t created_time;
uint32_t last_access_time;
@ -49,6 +49,7 @@ static ret_t bitmap_cache_destroy(bitmap_cache_t* cache) {
return_value_if_fail(cache != NULL, RET_BAD_PARAMS);
bitmap_destroy(&(cache->image));
TKMEM_FREE(cache->name);
TKMEM_FREE(cache);
return RET_OK;
@ -92,10 +93,9 @@ ret_t image_manager_add(image_manager_t* imm, const char* name, const bitmap_t*
cache->access_count = 1;
cache->created_time = time_now_s();
cache->image.should_free_handle = FALSE;
tk_strncpy(cache->name, name, TK_NAME_LEN);
cache->last_access_time = cache->created_time;
cache->name = tk_strdup(name);
cache->image.name = cache->name;
cache->last_access_time = cache->created_time;
return darray_push(&(imm->images), cache);
}
@ -105,7 +105,9 @@ ret_t image_manager_lookup(image_manager_t* imm, const char* name, bitmap_t* ima
bitmap_cache_t* iter = NULL;
return_value_if_fail(imm != NULL && name != NULL && image != NULL, RET_BAD_PARAMS);
tk_strncpy(info.name, name, TK_NAME_LEN);
memset(&info, 0x00, sizeof(info));
info.name = (char*)name;
imm->images.compare = (tk_compare_t)bitmap_cache_cmp_name;
iter = darray_find(&(imm->images), &info);

View File

@ -290,4 +290,8 @@ typedef struct _assets_manager_t assets_manager_t;
struct _system_info_t;
typedef struct _system_info_t system_info_t;
#define STR_SCHEMA_FILE "file://"
#define STR_SCHEMA_HTTP "http://"
#define STR_SCHEMA_HTTPS "https://"
#endif /*TK_TYPES_DEF_H*/

View File

@ -78,3 +78,15 @@ TEST(ImageManager, locale) {
assets_manager_destroy(am);
image_manager_destroy(imm);
}
#ifdef WITH_FS_RES
TEST(ImageManager, fs) {
bitmap_t bmp;
const char* filename = "file://./demos/assets/raw/images/xx/flag_CN.png";
memset(&bmp, 0x00, sizeof(bmp));
ASSERT_EQ(image_manager_get_bitmap(image_manager(), filename, &bmp), RET_OK);
ASSERT_EQ(image_manager_lookup(image_manager(), filename, &bmp), RET_OK);
ASSERT_EQ(image_manager_unload_bitmap(image_manager(), &bmp), RET_OK);
}
#endif /*WITH_FS_RES*/