update image_container.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1254 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
d20a26430d
commit
bb1518ef1d
@ -52,6 +52,30 @@ void rtgui_system_image_init(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
static struct rtgui_image_engine* rtgui_image_get_engine_by_filename(const char* fn)
|
||||
{
|
||||
struct rtgui_list_node *node;
|
||||
struct rtgui_image_engine *engine;
|
||||
const char* ext;
|
||||
|
||||
ext = fn + rt_strlen(fn);
|
||||
while (ext != fn)
|
||||
{
|
||||
if (*ext == '.') { ext ++; break; }
|
||||
ext --;
|
||||
}
|
||||
if (ext == fn) return RT_NULL; /* no ext */
|
||||
|
||||
rtgui_list_foreach(node, &_rtgui_system_image_list)
|
||||
{
|
||||
engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
|
||||
if (strncasecmp(engine->name, ext, strlen(engine->name)) == 0)
|
||||
return engine;
|
||||
}
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
static struct rtgui_image_engine* rtgui_image_get_engine(const char* type)
|
||||
{
|
||||
struct rtgui_list_node *node;
|
||||
@ -116,6 +140,54 @@ struct rtgui_image* rtgui_image_create_from_file(const char* type, const char* f
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
struct rtgui_image* rtgui_image_create(const char* filename, rt_bool_t load)
|
||||
{
|
||||
struct rtgui_filerw* filerw;
|
||||
struct rtgui_image_engine* engine;
|
||||
struct rtgui_image* image = RT_NULL;
|
||||
|
||||
/* create filerw context */
|
||||
filerw = rtgui_filerw_create_file(filename, "rb");
|
||||
if (filerw == RT_NULL) return RT_NULL;
|
||||
|
||||
/* get image engine */
|
||||
engine = rtgui_image_get_engine_by_filename(filename);
|
||||
if (engine == RT_NULL)
|
||||
{
|
||||
/* close filerw context */
|
||||
rtgui_filerw_close(filerw);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
if (engine->image_check(filerw) == RT_TRUE)
|
||||
{
|
||||
image = (struct rtgui_image*) rtgui_malloc(sizeof(struct rtgui_image));
|
||||
if (image == RT_NULL)
|
||||
{
|
||||
/* close filerw context */
|
||||
rtgui_filerw_close(filerw);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
image->palette = RT_NULL;
|
||||
if (engine->image_load(image, filerw, load) != RT_TRUE)
|
||||
{
|
||||
/* close filerw context */
|
||||
rtgui_filerw_close(filerw);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
/* set image engine */
|
||||
image->engine = engine;
|
||||
}
|
||||
else
|
||||
{
|
||||
rtgui_filerw_close(filerw);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length, rt_bool_t load)
|
||||
|
@ -352,6 +352,13 @@ rt_bool_t string_equal_func(const void* a, const void* b)
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
/* hash node for imnage */
|
||||
struct image_hash_node
|
||||
{
|
||||
rt_image_t *image;
|
||||
char *filename;
|
||||
};
|
||||
|
||||
static rtgui_hash_table_t* image_hash_table;
|
||||
rt_bool_t load_image = RT_FALSE;
|
||||
void image_container_system_init(rt_bool_t is_load)
|
||||
@ -362,15 +369,75 @@ void image_container_system_init(rt_bool_t is_load)
|
||||
load_image = is_load;
|
||||
}
|
||||
|
||||
rtgui_image_t* image_container_get(const char* filename)
|
||||
rtgui_image_t* rtgui_image_container_get(const char* filename)
|
||||
{
|
||||
/* get image type */
|
||||
struct image_hash_node* node;
|
||||
|
||||
node = hash_table_find(image_hash_table, filename);
|
||||
if (node == RT_NULL)
|
||||
{
|
||||
rtgui_image_t *image;
|
||||
|
||||
node = (struct image_hash_node*) rt_malloc (sizeof(struct image_hash_node));
|
||||
if (node == RT_NULL) return RT_NULL;
|
||||
|
||||
/* create a image object */
|
||||
image = rtgui_image_create(filename, load_image);
|
||||
if (image == RT_NULL)
|
||||
{
|
||||
rt_free(node);
|
||||
return RT_NULL; /* create image failed */
|
||||
}
|
||||
|
||||
rtgui_image_t* image_container_get_memref(const rt_uint8_t* memory, const char* type)
|
||||
node->image = image;
|
||||
node->filename = rt_strdup(filename);
|
||||
hash_table_add(image_hash_table, node->filename, node);
|
||||
}
|
||||
else
|
||||
{
|
||||
node->image->ref ++; /* increase refcount */
|
||||
}
|
||||
|
||||
void image_container_put(rtgui_image_t* image)
|
||||
return node->image;
|
||||
}
|
||||
|
||||
rtgui_image_t* rtgui_image_container_get_memref(const rt_uint8_t* memory, const char* type)
|
||||
{
|
||||
struct image_hash_node* node;
|
||||
char filename[32];
|
||||
|
||||
/* create filename for image identification */
|
||||
rt_snprintf(filename, sizeof(filename), "0x%08x_%s", memory, type);
|
||||
|
||||
/* search in container */
|
||||
node = hash_table_find(image_hash_table, filename);
|
||||
if (node == RT_NULL)
|
||||
{
|
||||
node = (struct image_hash_node*) rt_malloc (sizeof(struct image_hash_node));
|
||||
if (node == RT_NULL) return RT_NULL;
|
||||
|
||||
/* create image object */
|
||||
image = rtgui_image_create_from_mem(memory, type, load_image);
|
||||
if (image == RT_NULL)
|
||||
{
|
||||
rt_free(node);
|
||||
return RT_NULL; /* create image failed */
|
||||
}
|
||||
|
||||
node->image = image;
|
||||
node->filename = rt_strdup(image_id);
|
||||
hash_table_add(image_hash_table, node->filename, node);
|
||||
}
|
||||
else node->image->ref ++;
|
||||
|
||||
return node->image;
|
||||
}
|
||||
|
||||
void rtgui_image_container_put(rtgui_image_t* image)
|
||||
{
|
||||
image->ref --;
|
||||
if (image->ref == 0)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,6 +61,7 @@ void rtgui_system_image_init(void);
|
||||
|
||||
#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
|
||||
struct rtgui_image* rtgui_image_create_from_file(const char* type, const char* filename, rt_bool_t load);
|
||||
struct rtgui_image* rtgui_image_create(const char* filename, rt_bool_t load);
|
||||
#endif
|
||||
struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length, rt_bool_t load);
|
||||
void rtgui_image_destroy(struct rtgui_image* image);
|
||||
|
Loading…
x
Reference in New Issue
Block a user