image.c 4.1 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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
 */
#include <rtthread.h>
B
bernard.xiong 已提交
15 16
#include <rtgui/image.h>

B
bernard.xiong 已提交
17 18
#include <rtgui/image_xpm.h>
#include <rtgui/image_hdc.h>
B
bernard.xiong 已提交
19 20
#include <rtgui/rtgui_system.h>

B
bernard.xiong 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <string.h>

#ifdef RTGUI_IMAGE_BMP
#include <rtgui/image_bmp.h>
#endif
#ifdef RTGUI_IMAGE_JPEG
#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};

/* init rtgui image system */
void rtgui_system_image_init(void)
{
	/* always support XPM image */
	rtgui_image_xpm_init();
B
bernard.xiong 已提交
40 41
	rtgui_image_hdc_init();
	
B
bernard.xiong 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
#ifdef RTGUI_IMAGE_BMP
	rtgui_image_bmp_init();
#endif

#ifdef RTGUI_IMAGE_JPEG
	rtgui_image_jpeg_init();
#endif

#ifdef RTGUI_IMAGE_PNG
	rtgui_image_png_init();
#endif
}

static struct rtgui_image_engine* rtgui_image_get_engine(const char* type)
{
	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);

		if (strncasecmp(engine->name, type, strlen(engine->name)) ==0)
			return engine;
	}

	return RT_NULL;
}

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;
		}

		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;
}

struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length)
{
	struct rtgui_filerw* filerw;
	struct rtgui_image_engine* engine;
	struct rtgui_image* image = RT_NULL;

	/* create filerw context */
	filerw = rtgui_filerw_create_mem((rt_uint8_t*)data, length);
	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;
		}

		if (engine->image_load(image, filerw, RT_TRUE) != 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;
}

void rtgui_image_destroy(struct rtgui_image* image)
{
	RT_ASSERT(image != RT_NULL);

	image->engine->image_unload(image);
	rtgui_free(image);
}

/* 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));
}

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);
	}
}