From bb1518ef1d858fc95fd673d13adff61dfd2f8497 Mon Sep 17 00:00:00 2001 From: "bernard.xiong@gmail.com" Date: Sat, 22 Jan 2011 01:30:42 +0000 Subject: [PATCH] update image_container. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1254 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/rtgui/common/image.c | 72 +++++++++ components/rtgui/common/image_container.c | 75 ++++++++- components/rtgui/include/rtgui/image.h | 151 +++++++++--------- components/rtgui/include/rtgui/image_bmp.h | 38 ++--- .../rtgui/include/rtgui/image_container.h | 24 +-- components/rtgui/include/rtgui/image_hdc.h | 74 ++++----- components/rtgui/include/rtgui/image_jpeg.h | 16 +- components/rtgui/include/rtgui/image_png.h | 42 ++--- components/rtgui/include/rtgui/image_xpm.h | 42 ++--- 9 files changed, 337 insertions(+), 197 deletions(-) diff --git a/components/rtgui/common/image.c b/components/rtgui/common/image.c index b7ffdd38f1..23f869d987 100644 --- a/components/rtgui/common/image.c +++ b/components/rtgui/common/image.c @@ -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) diff --git a/components/rtgui/common/image_container.c b/components/rtgui/common/image_container.c index 85f4d00e7b..fd33ff6e1d 100644 --- a/components/rtgui/common/image_container.c +++ b/components/rtgui/common/image_container.c @@ -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 */ + } + + node->image = image; + node->filename = rt_strdup(filename); + hash_table_add(image_hash_table, node->filename, node); + } + else + { + node->image->ref ++; /* increase refcount */ + } + + return node->image; } -rtgui_image_t* image_container_get_memref(const rt_uint8_t* memory, const char* type) +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 image_container_put(rtgui_image_t* image) +void rtgui_image_container_put(rtgui_image_t* image) { + image->ref --; + if (image->ref == 0) + { + } } + diff --git a/components/rtgui/include/rtgui/image.h b/components/rtgui/include/rtgui/image.h index c52d78f61a..1c71ef2f32 100644 --- a/components/rtgui/include/rtgui/image.h +++ b/components/rtgui/include/rtgui/image.h @@ -1,76 +1,77 @@ -/* - * File : image.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2009-10-16 Bernard first version - */ -#ifndef __RTGUI_IMAGE_H__ -#define __RTGUI_IMAGE_H__ - -#include -#include -#include - -struct rtgui_image; -struct rtgui_image_engine -{ - const char* name; - struct rtgui_list_node list; - - /* image engine function */ - rt_bool_t (*image_check)(struct rtgui_filerw* file); - - rt_bool_t (*image_load)(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load); - void (*image_unload)(struct rtgui_image* image); - - void (*image_blit)(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect); -}; - -struct rtgui_image_palette -{ - rtgui_color_t* colors; - rt_uint32_t ncolors; -}; -typedef struct rtgui_image_palette rtgui_image_palette_t; - -struct rtgui_image -{ - /* image metrics */ - rt_uint16_t w, h; - - /* image engine */ - const struct rtgui_image_engine* engine; - - /* image palette */ - rtgui_image_palette_t* palette; - - /* image private data */ - void* data; -}; -typedef struct rtgui_image rtgui_image_t; - -/* init rtgui image system */ -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); -#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); - -/* register an image engine */ -void rtgui_image_register_engine(struct rtgui_image_engine* engine); - -/* blit an image on DC */ -void rtgui_image_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect); +/* + * File : image.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#ifndef __RTGUI_IMAGE_H__ +#define __RTGUI_IMAGE_H__ + +#include +#include +#include + +struct rtgui_image; +struct rtgui_image_engine +{ + const char* name; + struct rtgui_list_node list; + + /* image engine function */ + rt_bool_t (*image_check)(struct rtgui_filerw* file); + + rt_bool_t (*image_load)(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load); + void (*image_unload)(struct rtgui_image* image); + + void (*image_blit)(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect); +}; + +struct rtgui_image_palette +{ + rtgui_color_t* colors; + rt_uint32_t ncolors; +}; +typedef struct rtgui_image_palette rtgui_image_palette_t; + +struct rtgui_image +{ + /* image metrics */ + rt_uint16_t w, h; + + /* image engine */ + const struct rtgui_image_engine* engine; + + /* image palette */ + rtgui_image_palette_t* palette; + + /* image private data */ + void* data; +}; +typedef struct rtgui_image rtgui_image_t; + +/* init rtgui image system */ +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); + +/* register an image engine */ +void rtgui_image_register_engine(struct rtgui_image_engine* engine); + +/* blit an image on DC */ +void rtgui_image_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect); struct rtgui_image_palette* rtgui_image_palette_create(rt_uint32_t ncolors); - - -#endif + + +#endif diff --git a/components/rtgui/include/rtgui/image_bmp.h b/components/rtgui/include/rtgui/image_bmp.h index 1dbcccff90..19fdfc7f6e 100644 --- a/components/rtgui/include/rtgui/image_bmp.h +++ b/components/rtgui/include/rtgui/image_bmp.h @@ -1,19 +1,19 @@ -/* - * File : image_bmp.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2010-08-10 Bernard first version - */ -#ifndef __RTGUI_IMAGE_BMP_H__ -#define __RTGUI_IMAGE_BMP_H__ - -void rtgui_image_bmp_init(void); - -#endif +/* + * File : image_bmp.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2010-08-10 Bernard first version + */ +#ifndef __RTGUI_IMAGE_BMP_H__ +#define __RTGUI_IMAGE_BMP_H__ + +void rtgui_image_bmp_init(void); + +#endif diff --git a/components/rtgui/include/rtgui/image_container.h b/components/rtgui/include/rtgui/image_container.h index 6764a5b19b..8708ef0944 100644 --- a/components/rtgui/include/rtgui/image_container.h +++ b/components/rtgui/include/rtgui/image_container.h @@ -1,12 +1,12 @@ -#ifndef __RTGUI_IMAGE_CONTAINER_H__ -#define __RTGUI_IMAGE_CONTAINER_H__ - -#include -#include - -void image_container_system_init(void); - -rtgui_image_t* image_container_get(const char* filename); -void image_container_put(rtgui_image_t* image); - -#endif +#ifndef __RTGUI_IMAGE_CONTAINER_H__ +#define __RTGUI_IMAGE_CONTAINER_H__ + +#include +#include + +void image_container_system_init(void); + +rtgui_image_t* image_container_get(const char* filename); +void image_container_put(rtgui_image_t* image); + +#endif diff --git a/components/rtgui/include/rtgui/image_hdc.h b/components/rtgui/include/rtgui/image_hdc.h index 8d253631f1..122a6a3313 100644 --- a/components/rtgui/include/rtgui/image_hdc.h +++ b/components/rtgui/include/rtgui/image_hdc.h @@ -1,37 +1,37 @@ -/* - * File : image_xpm.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2009-10-16 Bernard first version - */ -#ifndef __RTGUI_IMAGE_HDC_H__ -#define __RTGUI_IMAGE_HDC_H__ - -#include - -struct rtgui_image_hdcmm -{ - struct rtgui_image parent; - - /* hdc image information */ - rt_uint16_t byte_per_pixel; - rt_uint16_t pitch; - - rt_uint8_t *pixels; -}; - -void rtgui_image_hdc_init(void); -extern const struct rtgui_image_engine rtgui_image_hdcmm_engine; - -#define HDC_HEADER_SIZE (5 * 4) -#define RTGUI_IMAGE_HDC_DEF(bpp, w, h, pixels) \ - {{w, h, &rtgui_image_hdcmm_engine, RT_NULL}, bpp, (bpp * w), ((rt_uint8_t*)pixels + HDC_HEADER_SIZE)} - -#endif +/* + * File : image_xpm.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#ifndef __RTGUI_IMAGE_HDC_H__ +#define __RTGUI_IMAGE_HDC_H__ + +#include + +struct rtgui_image_hdcmm +{ + struct rtgui_image parent; + + /* hdc image information */ + rt_uint16_t byte_per_pixel; + rt_uint16_t pitch; + + rt_uint8_t *pixels; +}; + +void rtgui_image_hdc_init(void); +extern const struct rtgui_image_engine rtgui_image_hdcmm_engine; + +#define HDC_HEADER_SIZE (5 * 4) +#define RTGUI_IMAGE_HDC_DEF(bpp, w, h, pixels) \ + {{w, h, &rtgui_image_hdcmm_engine, RT_NULL}, bpp, (bpp * w), ((rt_uint8_t*)pixels + HDC_HEADER_SIZE)} + +#endif diff --git a/components/rtgui/include/rtgui/image_jpeg.h b/components/rtgui/include/rtgui/image_jpeg.h index 48e88e701d..9a22a68805 100644 --- a/components/rtgui/include/rtgui/image_jpeg.h +++ b/components/rtgui/include/rtgui/image_jpeg.h @@ -1,8 +1,8 @@ -#ifndef __RTGUI_IMAGE_JPEG_H__ -#define __RTGUI_IMAGE_JPEG_H__ - -#include - -void rtgui_image_jpeg_init(void); - -#endif +#ifndef __RTGUI_IMAGE_JPEG_H__ +#define __RTGUI_IMAGE_JPEG_H__ + +#include + +void rtgui_image_jpeg_init(void); + +#endif diff --git a/components/rtgui/include/rtgui/image_png.h b/components/rtgui/include/rtgui/image_png.h index 09454d6a75..f2a6d2311d 100644 --- a/components/rtgui/include/rtgui/image_png.h +++ b/components/rtgui/include/rtgui/image_png.h @@ -1,21 +1,21 @@ -/* - * File : image_png.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2009-10-16 Bernard first version - */ -#ifndef __RTGUI_IMAGE_PNG_H__ -#define __RTGUI_IMAGE_PNG_H__ - -#include - -void rtgui_image_png_init(void); - -#endif +/* + * File : image_png.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#ifndef __RTGUI_IMAGE_PNG_H__ +#define __RTGUI_IMAGE_PNG_H__ + +#include + +void rtgui_image_png_init(void); + +#endif diff --git a/components/rtgui/include/rtgui/image_xpm.h b/components/rtgui/include/rtgui/image_xpm.h index e580327f6d..5c6af21313 100644 --- a/components/rtgui/include/rtgui/image_xpm.h +++ b/components/rtgui/include/rtgui/image_xpm.h @@ -1,21 +1,21 @@ -/* - * File : image_xpm.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2009-10-16 Bernard first version - */ -#ifndef __RTGUI_IMAGE_XPM_H__ -#define __RTGUI_IMAGE_XPM_H__ - -#include - -void rtgui_image_xpm_init(void); - -#endif +/* + * File : image_xpm.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#ifndef __RTGUI_IMAGE_XPM_H__ +#define __RTGUI_IMAGE_XPM_H__ + +#include + +void rtgui_image_xpm_init(void); + +#endif -- GitLab