2009-11-06 07:57:02 +08:00
|
|
|
#include <rtthread.h>
|
2010-04-10 18:06:35 +08:00
|
|
|
#include <rtgui/dc_hw.h>
|
2009-11-06 15:44:39 +08:00
|
|
|
#include <rtgui/image.h>
|
2009-11-06 07:57:02 +08:00
|
|
|
#include <rtgui/rtgui_system.h>
|
2010-05-26 23:25:34 +08:00
|
|
|
#include <rtgui/image_hdc.h>
|
2009-11-06 07:57:02 +08:00
|
|
|
|
|
|
|
#define HDC_MAGIC_LEN 4
|
|
|
|
|
|
|
|
struct rtgui_image_hdc
|
|
|
|
{
|
|
|
|
rt_bool_t is_loaded;
|
|
|
|
|
|
|
|
/* hdc image information */
|
2009-11-06 15:44:39 +08:00
|
|
|
rt_uint16_t byte_per_pixel;
|
|
|
|
rt_uint16_t pitch;
|
2009-11-06 07:57:02 +08:00
|
|
|
|
2009-11-06 15:44:39 +08:00
|
|
|
rt_size_t pixel_offset;
|
2009-11-06 07:57:02 +08:00
|
|
|
rt_uint8_t *pixels;
|
2010-05-26 23:25:34 +08:00
|
|
|
|
|
|
|
struct rtgui_filerw* filerw;
|
2010-07-05 07:58:35 +08:00
|
|
|
const struct rtgui_graphic_driver* hw_driver;
|
2009-11-06 07:57:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw* file);
|
|
|
|
static rt_bool_t rtgui_image_hdc_load(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load);
|
|
|
|
static void rtgui_image_hdc_unload(struct rtgui_image* image);
|
|
|
|
static void rtgui_image_hdc_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect);
|
2010-05-26 23:25:34 +08:00
|
|
|
static void rtgui_image_hdcmm_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* dst_rect);
|
2009-11-06 07:57:02 +08:00
|
|
|
|
|
|
|
struct rtgui_image_engine rtgui_image_hdc_engine =
|
|
|
|
{
|
|
|
|
"hdc",
|
|
|
|
{ RT_NULL },
|
|
|
|
rtgui_image_hdc_check,
|
|
|
|
rtgui_image_hdc_load,
|
|
|
|
rtgui_image_hdc_unload,
|
2012-09-07 09:50:13 +08:00
|
|
|
rtgui_image_hdc_blit,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL
|
2009-11-06 07:57:02 +08:00
|
|
|
};
|
|
|
|
|
2010-05-31 06:44:21 +08:00
|
|
|
const struct rtgui_image_engine rtgui_image_hdcmm_engine =
|
2010-05-26 23:25:34 +08:00
|
|
|
{
|
|
|
|
"hdcmm",
|
|
|
|
{RT_NULL},
|
2010-08-03 16:47:39 +08:00
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
2012-09-07 09:50:13 +08:00
|
|
|
rtgui_image_hdcmm_blit,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL
|
2010-05-26 23:25:34 +08:00
|
|
|
};
|
|
|
|
|
2009-11-06 07:57:02 +08:00
|
|
|
static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw* file)
|
|
|
|
{
|
|
|
|
int start;
|
|
|
|
rt_bool_t is_HDC;
|
|
|
|
rt_uint8_t magic[4];
|
|
|
|
|
|
|
|
if ( !file ) return 0;
|
|
|
|
|
|
|
|
start = rtgui_filerw_tell(file);
|
|
|
|
|
2009-11-06 15:44:39 +08:00
|
|
|
/* move to the beginning of file */
|
|
|
|
rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET);
|
2009-11-06 07:57:02 +08:00
|
|
|
|
|
|
|
is_HDC = RT_FALSE;
|
|
|
|
if ( rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic) )
|
|
|
|
{
|
|
|
|
if ( magic[0] == 'H' &&
|
|
|
|
magic[1] == 'D' &&
|
|
|
|
magic[2] == 'C' &&
|
|
|
|
magic[3] == '\0' )
|
|
|
|
{
|
|
|
|
is_HDC = RT_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2009-11-06 15:44:39 +08:00
|
|
|
rtgui_filerw_seek(file, start, RTGUI_FILE_SEEK_SET);
|
2009-11-06 07:57:02 +08:00
|
|
|
|
|
|
|
return(is_HDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_bool_t rtgui_image_hdc_load(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load)
|
|
|
|
{
|
|
|
|
rt_uint32_t header[5];
|
|
|
|
struct rtgui_image_hdc* hdc;
|
|
|
|
|
|
|
|
hdc = (struct rtgui_image_hdc*) rtgui_malloc(sizeof(struct rtgui_image_hdc));
|
|
|
|
if (hdc == RT_NULL) return RT_FALSE;
|
|
|
|
|
2009-11-06 15:44:39 +08:00
|
|
|
hdc->hw_driver = rtgui_graphic_driver_get_default();
|
|
|
|
if (hdc->hw_driver == RT_NULL) { rtgui_free(hdc); return RT_FALSE; }
|
|
|
|
|
2009-11-06 07:57:02 +08:00
|
|
|
rtgui_filerw_read(file, (char*)&header, 1, sizeof(header));
|
|
|
|
|
|
|
|
/* set image information */
|
2009-11-24 07:34:58 +08:00
|
|
|
image->w = (rt_uint16_t)header[1]; image->h = (rt_uint16_t)header[2];
|
2009-11-06 07:57:02 +08:00
|
|
|
image->engine = &rtgui_image_hdc_engine;
|
2009-11-06 15:44:39 +08:00
|
|
|
image->data = hdc;
|
|
|
|
hdc->filerw = file;
|
2011-06-06 17:10:13 +08:00
|
|
|
hdc->byte_per_pixel = hdc->hw_driver->bits_per_pixel/8;
|
2009-11-06 15:44:39 +08:00
|
|
|
hdc->pitch = image->w * hdc->byte_per_pixel;
|
|
|
|
hdc->pixel_offset = rtgui_filerw_tell(file);
|
2009-11-06 07:57:02 +08:00
|
|
|
|
|
|
|
if (load == RT_TRUE)
|
|
|
|
{
|
|
|
|
/* load all pixels */
|
2009-11-06 15:44:39 +08:00
|
|
|
hdc->pixels = rtgui_malloc(image->h * hdc->pitch);
|
|
|
|
if (hdc->pixels == RT_NULL)
|
2009-11-06 07:57:02 +08:00
|
|
|
{
|
|
|
|
/* release data */
|
2009-11-06 15:44:39 +08:00
|
|
|
rtgui_free(hdc);
|
2009-11-06 07:57:02 +08:00
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
2009-11-06 15:44:39 +08:00
|
|
|
rtgui_filerw_read(hdc->filerw, hdc->pixels, 1, image->h * hdc->pitch);
|
|
|
|
rtgui_filerw_close(hdc->filerw);
|
|
|
|
hdc->filerw = RT_NULL;
|
|
|
|
hdc->pixel_offset = 0;
|
2009-11-06 07:57:02 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-06 15:44:39 +08:00
|
|
|
hdc->pixels = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2009-11-06 07:57:02 +08:00
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtgui_image_hdc_unload(struct rtgui_image* image)
|
|
|
|
{
|
2009-11-06 15:44:39 +08:00
|
|
|
struct rtgui_image_hdc* hdc;
|
2009-11-06 07:57:02 +08:00
|
|
|
|
|
|
|
if (image != RT_NULL)
|
|
|
|
{
|
2009-11-06 15:44:39 +08:00
|
|
|
hdc = (struct rtgui_image_hdc*) image->data;
|
2009-11-06 07:57:02 +08:00
|
|
|
|
2009-11-06 15:44:39 +08:00
|
|
|
if (hdc->pixels != RT_NULL) rtgui_free(hdc->pixels);
|
|
|
|
if (hdc->filerw != RT_NULL)
|
|
|
|
{
|
|
|
|
rtgui_filerw_close(hdc->filerw);
|
|
|
|
hdc->filerw = RT_NULL;
|
|
|
|
}
|
2009-11-06 07:57:02 +08:00
|
|
|
|
|
|
|
/* release data */
|
2009-11-06 15:44:39 +08:00
|
|
|
rtgui_free(hdc);
|
2009-11-06 07:57:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-09 07:57:21 +08:00
|
|
|
static void rtgui_image_hdc_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* dst_rect)
|
2009-11-06 07:57:02 +08:00
|
|
|
{
|
2009-11-06 15:44:39 +08:00
|
|
|
rt_uint16_t y, w, h;
|
|
|
|
struct rtgui_image_hdc* hdc;
|
2009-11-09 07:57:21 +08:00
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
|
2009-11-06 07:57:02 +08:00
|
|
|
|
2009-11-06 15:44:39 +08:00
|
|
|
/* this dc is not visible */
|
2010-06-01 08:07:59 +08:00
|
|
|
if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
|
2009-11-06 15:44:39 +08:00
|
|
|
|
|
|
|
hdc = (struct rtgui_image_hdc*) image->data;
|
|
|
|
RT_ASSERT(hdc != RT_NULL);
|
|
|
|
|
|
|
|
/* the minimum rect */
|
2009-11-24 07:34:58 +08:00
|
|
|
if (image->w < rtgui_rect_width(*dst_rect)) w = image->w;
|
|
|
|
else w = rtgui_rect_width(*dst_rect);
|
|
|
|
if (image->h < rtgui_rect_height(*dst_rect)) h = image->h;
|
|
|
|
else h = rtgui_rect_height(*dst_rect);
|
2009-11-06 07:57:02 +08:00
|
|
|
|
2009-11-06 15:44:39 +08:00
|
|
|
if (hdc->pixels != RT_NULL)
|
2009-11-06 07:57:02 +08:00
|
|
|
{
|
2009-11-24 07:34:58 +08:00
|
|
|
rt_uint8_t* ptr;
|
2009-11-06 15:44:39 +08:00
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
/* get pixel pointer */
|
|
|
|
ptr = hdc->pixels;
|
2009-11-06 15:44:39 +08:00
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
for (y = 0; y < h; y ++)
|
|
|
|
{
|
2010-08-17 19:33:18 +08:00
|
|
|
dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr);
|
2009-11-24 07:34:58 +08:00
|
|
|
ptr += hdc->pitch;
|
2009-11-06 15:44:39 +08:00
|
|
|
}
|
2009-11-06 07:57:02 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-24 07:34:58 +08:00
|
|
|
rt_uint8_t* ptr;
|
|
|
|
ptr = rtgui_malloc(hdc->pitch);
|
|
|
|
if (ptr == RT_NULL) return; /* no memory */
|
2009-11-06 15:44:39 +08:00
|
|
|
|
|
|
|
/* seek to the begin of pixel data */
|
|
|
|
rtgui_filerw_seek(hdc->filerw, hdc->pixel_offset, RTGUI_FILE_SEEK_SET);
|
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
for (y = 0; y < h; y ++)
|
2009-11-06 15:44:39 +08:00
|
|
|
{
|
2009-11-24 07:34:58 +08:00
|
|
|
/* read pixel data */
|
|
|
|
if (rtgui_filerw_read(hdc->filerw, ptr, 1, hdc->pitch) != hdc->pitch)
|
|
|
|
break; /* read data failed */
|
|
|
|
|
2010-08-17 19:33:18 +08:00
|
|
|
dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr);
|
2009-11-06 15:44:39 +08:00
|
|
|
}
|
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
rtgui_free(ptr);
|
2009-11-06 07:57:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-26 23:25:34 +08:00
|
|
|
static void rtgui_image_hdcmm_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* dst_rect)
|
|
|
|
{
|
|
|
|
rt_uint8_t* ptr;
|
|
|
|
rt_uint16_t y, w, h;
|
|
|
|
struct rtgui_image_hdcmm* hdc;
|
|
|
|
|
|
|
|
RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
|
|
|
|
|
|
|
|
/* this dc is not visible */
|
2010-08-17 19:33:18 +08:00
|
|
|
if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
|
2010-05-26 23:25:34 +08:00
|
|
|
|
|
|
|
hdc = (struct rtgui_image_hdcmm*) image;
|
|
|
|
RT_ASSERT(hdc != RT_NULL);
|
|
|
|
|
|
|
|
/* the minimum rect */
|
|
|
|
if (image->w < rtgui_rect_width(*dst_rect)) w = image->w;
|
|
|
|
else w = rtgui_rect_width(*dst_rect);
|
|
|
|
if (image->h < rtgui_rect_height(*dst_rect)) h = image->h;
|
|
|
|
else h = rtgui_rect_height(*dst_rect);
|
|
|
|
|
|
|
|
|
|
|
|
/* get pixel pointer */
|
|
|
|
ptr = hdc->pixels;
|
|
|
|
|
|
|
|
for (y = 0; y < h; y ++)
|
|
|
|
{
|
2010-08-17 19:33:18 +08:00
|
|
|
dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr);
|
2010-05-26 23:25:34 +08:00
|
|
|
ptr += hdc->pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-06 07:57:02 +08:00
|
|
|
void rtgui_image_hdc_init()
|
|
|
|
{
|
2009-11-06 15:44:39 +08:00
|
|
|
/* register hdc on image system */
|
2009-11-06 07:57:02 +08:00
|
|
|
rtgui_image_register_engine(&rtgui_image_hdc_engine);
|
|
|
|
}
|