2009-10-16 08:03:50 +08:00
|
|
|
/*
|
|
|
|
* File : image.c
|
|
|
|
* 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
|
2012-07-20 20:37:56 +08:00
|
|
|
* 2012-01-24 onelife add TJpgDec (Tiny JPEG Decompressor) support
|
2009-10-16 08:03:50 +08:00
|
|
|
*/
|
|
|
|
#include <rtthread.h>
|
2009-11-24 07:34:58 +08:00
|
|
|
#include <rtgui/image.h>
|
2009-10-26 07:26:04 +08:00
|
|
|
|
2009-11-09 18:06:46 +08:00
|
|
|
#include <rtgui/image_hdc.h>
|
2009-11-24 07:34:58 +08:00
|
|
|
#include <rtgui/rtgui_system.h>
|
2011-01-23 21:41:34 +08:00
|
|
|
#include <rtgui/image_container.h>
|
2009-10-26 07:26:04 +08:00
|
|
|
|
2009-10-16 08:03:50 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-01-23 21:41:34 +08:00
|
|
|
#ifdef RTGUI_IMAGE_XPM
|
|
|
|
#include <rtgui/image_xpm.h>
|
|
|
|
#endif
|
|
|
|
|
2009-10-16 08:03:50 +08:00
|
|
|
#ifdef RTGUI_IMAGE_BMP
|
|
|
|
#include <rtgui/image_bmp.h>
|
|
|
|
#endif
|
2012-07-20 20:37:56 +08:00
|
|
|
#if (defined(RTGUI_IMAGE_JPEG) || defined(RTGUI_IMAGE_TJPGD))
|
2009-10-16 08:03:50 +08:00
|
|
|
#include <rtgui/image_jpeg.h>
|
|
|
|
#endif
|
|
|
|
#ifdef RTGUI_IMAGE_PNG
|
|
|
|
#include <rtgui/image_png.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static rtgui_list_t _rtgui_system_image_list = {RT_NULL};
|
|
|
|
|
2011-01-23 21:41:34 +08:00
|
|
|
/* initialize rtgui image system */
|
2009-10-16 08:03:50 +08:00
|
|
|
void rtgui_system_image_init(void)
|
|
|
|
{
|
2011-01-23 21:41:34 +08:00
|
|
|
/* always support HDC image */
|
2009-11-24 07:34:58 +08:00
|
|
|
rtgui_image_hdc_init();
|
2011-01-23 21:41:34 +08:00
|
|
|
|
|
|
|
#ifdef RTGUI_IMAGE_XPM
|
|
|
|
rtgui_image_xpm_init();
|
|
|
|
#endif
|
2009-11-09 07:57:21 +08:00
|
|
|
|
2009-10-16 08:03:50 +08:00
|
|
|
#ifdef RTGUI_IMAGE_BMP
|
|
|
|
rtgui_image_bmp_init();
|
|
|
|
#endif
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
#if (defined(RTGUI_IMAGE_JPEG) || defined(RTGUI_IMAGE_TJPGD))
|
2009-10-16 08:03:50 +08:00
|
|
|
rtgui_image_jpeg_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RTGUI_IMAGE_PNG
|
|
|
|
rtgui_image_png_init();
|
|
|
|
#endif
|
2011-01-23 21:41:34 +08:00
|
|
|
|
|
|
|
#ifdef RTGUI_IMAGE_CONTAINER
|
|
|
|
/* initialize image container */
|
|
|
|
rtgui_system_image_container_init(RT_FALSE);
|
|
|
|
#endif
|
2009-10-16 08:03:50 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 10:14:21 +08:00
|
|
|
static struct rtgui_image_engine* rtgui_image_get_engine(const char* type)
|
2011-01-22 09:30:42 +08:00
|
|
|
{
|
|
|
|
struct rtgui_list_node *node;
|
|
|
|
struct rtgui_image_engine *engine;
|
|
|
|
|
|
|
|
rtgui_list_foreach(node, &_rtgui_system_image_list)
|
|
|
|
{
|
|
|
|
engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
|
2012-04-19 10:14:21 +08:00
|
|
|
|
|
|
|
if (strncasecmp(engine->name, type, strlen(engine->name)) ==0)
|
2011-01-22 09:30:42 +08:00
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-27 15:57:19 +08:00
|
|
|
#if defined(RTGUI_USING_DFS_FILERW)
|
2012-07-26 14:36:49 +08:00
|
|
|
struct rtgui_image_engine* rtgui_image_get_engine_by_filename(const char* fn)
|
2009-10-16 08:03:50 +08:00
|
|
|
{
|
|
|
|
struct rtgui_list_node *node;
|
|
|
|
struct rtgui_image_engine *engine;
|
2012-04-19 10:14:21 +08:00
|
|
|
const char* ext;
|
|
|
|
|
|
|
|
ext = fn + rt_strlen(fn);
|
|
|
|
while (ext != fn)
|
|
|
|
{
|
|
|
|
if (*ext == '.') { ext ++; break; }
|
|
|
|
ext --;
|
|
|
|
}
|
|
|
|
if (ext == fn) return RT_NULL; /* no ext */
|
2009-10-16 08:03:50 +08:00
|
|
|
|
|
|
|
rtgui_list_foreach(node, &_rtgui_system_image_list)
|
|
|
|
{
|
|
|
|
engine = rtgui_list_entry(node, struct rtgui_image_engine, list);
|
2012-04-19 10:14:21 +08:00
|
|
|
if (strncasecmp(engine->name, ext, strlen(engine->name)) == 0)
|
2009-10-16 08:03:50 +08:00
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_image_get_engine_by_filename);
|
2009-10-16 08:03:50 +08:00
|
|
|
|
|
|
|
struct rtgui_image* rtgui_image_create_from_file(const char* type, 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(type);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-11 07:56:26 +08:00
|
|
|
image->palette = RT_NULL;
|
2009-10-16 08:03:50 +08:00
|
|
|
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;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_image_create_from_file);
|
2011-01-22 09:30:42 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_image_create);
|
2010-07-30 17:47:56 +08:00
|
|
|
#endif
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2009-12-01 23:27:22 +08:00
|
|
|
struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length, rt_bool_t load)
|
2009-10-16 08:03:50 +08:00
|
|
|
{
|
|
|
|
struct rtgui_filerw* filerw;
|
|
|
|
struct rtgui_image_engine* engine;
|
|
|
|
struct rtgui_image* image = RT_NULL;
|
|
|
|
|
|
|
|
/* create filerw context */
|
2009-11-26 17:28:06 +08:00
|
|
|
filerw = rtgui_filerw_create_mem(data, length);
|
2009-10-16 08:03:50 +08:00
|
|
|
if (filerw == RT_NULL) return RT_NULL;
|
|
|
|
|
|
|
|
/* get image engine */
|
|
|
|
engine = rtgui_image_get_engine(type);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-11 07:56:26 +08:00
|
|
|
image->palette = RT_NULL;
|
2009-12-01 23:27:22 +08:00
|
|
|
if (engine->image_load(image, filerw, load) != RT_TRUE)
|
2009-10-16 08:03:50 +08:00
|
|
|
{
|
|
|
|
/* close filerw context */
|
|
|
|
rtgui_filerw_close(filerw);
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set image engine */
|
|
|
|
image->engine = engine;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rtgui_filerw_close(filerw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_image_create_from_mem);
|
2009-10-16 08:03:50 +08:00
|
|
|
|
|
|
|
void rtgui_image_destroy(struct rtgui_image* image)
|
|
|
|
{
|
|
|
|
RT_ASSERT(image != RT_NULL);
|
|
|
|
|
|
|
|
image->engine->image_unload(image);
|
2010-08-11 07:56:26 +08:00
|
|
|
if (image->palette != RT_NULL)
|
|
|
|
rtgui_free(image->palette);
|
2009-10-16 08:03:50 +08:00
|
|
|
rtgui_free(image);
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_image_destroy);
|
2009-10-16 08:03:50 +08:00
|
|
|
|
|
|
|
/* register an image engine */
|
|
|
|
void rtgui_image_register_engine(struct rtgui_image_engine* engine)
|
|
|
|
{
|
|
|
|
RT_ASSERT(engine!= RT_NULL);
|
|
|
|
|
|
|
|
rtgui_list_append(&_rtgui_system_image_list, &(engine->list));
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_image_register_engine);
|
2009-10-16 08:03:50 +08:00
|
|
|
|
|
|
|
void rtgui_image_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect)
|
|
|
|
{
|
|
|
|
RT_ASSERT(dc != RT_NULL);
|
|
|
|
RT_ASSERT(rect != RT_NULL);
|
|
|
|
|
|
|
|
if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
|
|
|
|
|
|
|
|
if (image != RT_NULL && image->engine != RT_NULL)
|
|
|
|
{
|
|
|
|
/* use image engine to blit */
|
|
|
|
image->engine->image_blit(image, dc, rect);
|
|
|
|
}
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_image_blit);
|
2009-10-16 08:03:50 +08:00
|
|
|
|
2010-08-11 07:56:26 +08:00
|
|
|
struct rtgui_image_palette* rtgui_image_palette_create(rt_uint32_t ncolors)
|
|
|
|
{
|
|
|
|
struct rtgui_image_palette* palette = RT_NULL;
|
|
|
|
|
|
|
|
if (ncolors > 0)
|
|
|
|
{
|
|
|
|
palette = (struct rtgui_image_palette*) rtgui_malloc(sizeof(struct rtgui_image_palette) +
|
|
|
|
sizeof(rtgui_color_t) * ncolors);
|
|
|
|
if (palette != RT_NULL) palette->colors = (rtgui_color_t*)(palette + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return palette;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_image_palette_create);
|
2011-06-05 23:19:19 +08:00
|
|
|
|
|
|
|
void rtgui_image_get_rect(struct rtgui_image* image, struct rtgui_rect* rect)
|
|
|
|
{
|
|
|
|
RT_ASSERT(image != RT_NULL);
|
|
|
|
RT_ASSERT(rect != RT_NULL);
|
|
|
|
|
|
|
|
rect->x1 = 0; rect->y1 = 0;
|
|
|
|
rect->x2 = image->w; rect->y2 = image->h;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_image_get_rect);
|
2011-06-05 23:19:19 +08:00
|
|
|
|