image_hdc.c 6.3 KB
Newer Older
B
bernard.xiong 已提交
1
#include <rtthread.h>
2
#include <rtgui/dc_hw.h>
B
bernard.xiong 已提交
3
#include <rtgui/image.h>
B
bernard.xiong 已提交
4
#include <rtgui/rtgui_system.h>
5
#include <rtgui/image_hdc.h>
B
bernard.xiong 已提交
6 7 8 9 10

#define HDC_MAGIC_LEN       4

struct rtgui_image_hdc
{
11
    rt_bool_t is_loaded;
B
bernard.xiong 已提交
12

13 14
    /* hdc image information */
    rt_uint16_t byte_per_pixel;
B
bernard.xiong 已提交
15
    rt_uint16_t pitch;
B
bernard.xiong 已提交
16

17 18
    rt_size_t   pixel_offset;
    rt_uint8_t *pixels;
19

20 21
    struct rtgui_filerw *filerw;
    const struct rtgui_graphic_driver *hw_driver;
B
bernard.xiong 已提交
22 23
};

24 25 26 27 28
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);
static void rtgui_image_hdcmm_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *dst_rect);
B
bernard.xiong 已提交
29 30 31

struct rtgui_image_engine rtgui_image_hdc_engine =
{
32 33 34 35 36 37 38 39
    "hdc",
    { RT_NULL },
    rtgui_image_hdc_check,
    rtgui_image_hdc_load,
    rtgui_image_hdc_unload,
    rtgui_image_hdc_blit,
    RT_NULL,
    RT_NULL
B
bernard.xiong 已提交
40 41
};

42
const struct rtgui_image_engine rtgui_image_hdcmm_engine =
43
{
44 45 46 47 48 49 50 51
    "hdcmm",
    {RT_NULL},
    RT_NULL,
    RT_NULL,
    RT_NULL,
    rtgui_image_hdcmm_blit,
    RT_NULL,
    RT_NULL
52 53
};

54
static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw *file)
B
bernard.xiong 已提交
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
    int start;
    rt_bool_t is_HDC;
    rt_uint8_t magic[4];

    if (!file) return 0;

    start = rtgui_filerw_tell(file);

    /* move to the beginning of file */
    rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET);

    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;
        }
    }
    rtgui_filerw_seek(file, start, RTGUI_FILE_SEEK_SET);

    return(is_HDC);
B
bernard.xiong 已提交
81 82
}

83
static rt_bool_t rtgui_image_hdc_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load)
B
bernard.xiong 已提交
84 85
{
    rt_uint32_t header[5];
86
    struct rtgui_image_hdc *hdc;
B
bernard.xiong 已提交
87

88
    hdc = (struct rtgui_image_hdc *) rtgui_malloc(sizeof(struct rtgui_image_hdc));
B
bernard.xiong 已提交
89 90
    if (hdc == RT_NULL) return RT_FALSE;

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
    hdc->hw_driver = rtgui_graphic_driver_get_default();
    if (hdc->hw_driver == RT_NULL)
    {
        rtgui_free(hdc);
        return RT_FALSE;
    }

    rtgui_filerw_read(file, (char *)&header, 1, sizeof(header));

    /* set image information */
    image->w = (rt_uint16_t)header[1];
    image->h = (rt_uint16_t)header[2];
    image->engine = &rtgui_image_hdc_engine;
    image->data = hdc;
    hdc->filerw = file;
    hdc->byte_per_pixel = hdc->hw_driver->bits_per_pixel / 8;
    hdc->pitch = image->w * hdc->byte_per_pixel;
    hdc->pixel_offset = rtgui_filerw_tell(file);

    if (load == RT_TRUE)
    {
        /* load all pixels */
        hdc->pixels = rtgui_malloc(image->h * hdc->pitch);
        if (hdc->pixels == RT_NULL)
        {
B
bernard.xiong 已提交
116
            /* release data */
B
bernard.xiong 已提交
117
            rtgui_free(hdc);
B
bernard.xiong 已提交
118
            return RT_FALSE;
119 120 121 122 123 124 125 126 127 128 129 130 131
        }

        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;
    }
    else
    {
        hdc->pixels = RT_NULL;
    }

    return RT_TRUE;
B
bernard.xiong 已提交
132 133
}

134
static void rtgui_image_hdc_unload(struct rtgui_image *image)
B
bernard.xiong 已提交
135
{
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    struct rtgui_image_hdc *hdc;

    if (image != RT_NULL)
    {
        hdc = (struct rtgui_image_hdc *) image->data;

        if (hdc->pixels != RT_NULL) rtgui_free(hdc->pixels);
        if (hdc->filerw != RT_NULL)
        {
            rtgui_filerw_close(hdc->filerw);
            hdc->filerw = RT_NULL;
        }

        /* release data */
        rtgui_free(hdc);
    }
B
bernard.xiong 已提交
152 153
}

154
static void rtgui_image_hdc_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *dst_rect)
B
bernard.xiong 已提交
155
{
156 157
    rt_uint16_t y, w, h;
    struct rtgui_image_hdc *hdc;
B
bernard.xiong 已提交
158

159
    RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
B
bernard.xiong 已提交
160

161 162
    /* this dc is not visible */
    if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
B
bernard.xiong 已提交
163

164 165
    hdc = (struct rtgui_image_hdc *) image->data;
    RT_ASSERT(hdc != RT_NULL);
B
bernard.xiong 已提交
166

167
    /* the minimum rect */
B
bernard.xiong 已提交
168 169 170 171
    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);
B
bernard.xiong 已提交
172

B
bernard.xiong 已提交
173
    if (hdc->pixels != RT_NULL)
B
bernard.xiong 已提交
174
    {
175
        rt_uint8_t *ptr;
B
bernard.xiong 已提交
176

177 178
        /* get pixel pointer */
        ptr = hdc->pixels;
B
bernard.xiong 已提交
179

180 181 182 183 184
        for (y = 0; y < h; y ++)
        {
            dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr);
            ptr += hdc->pitch;
        }
B
bernard.xiong 已提交
185 186 187
    }
    else
    {
188 189 190
        rt_uint8_t *ptr;
        ptr = rtgui_malloc(hdc->pitch);
        if (ptr == RT_NULL) return; /* no memory */
B
bernard.xiong 已提交
191

192 193
        /* seek to the begin of pixel data */
        rtgui_filerw_seek(hdc->filerw, hdc->pixel_offset, RTGUI_FILE_SEEK_SET);
B
bernard.xiong 已提交
194

195 196 197 198 199
        for (y = 0; y < h; y ++)
        {
            /* read pixel data */
            if (rtgui_filerw_read(hdc->filerw, ptr, 1, hdc->pitch) != hdc->pitch)
                break; /* read data failed */
B
bernard.xiong 已提交
200

201 202
            dc->engine->blit_line(dc, dst_rect->x1,  dst_rect->x1 + w, dst_rect->y1 + y, ptr);
        }
B
bernard.xiong 已提交
203

204
        rtgui_free(ptr);
B
bernard.xiong 已提交
205 206 207
    }
}

208
static void rtgui_image_hdcmm_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *dst_rect)
209
{
210 211 212
    rt_uint8_t *ptr;
    rt_uint16_t y, w, h;
    struct rtgui_image_hdcmm *hdc;
213

214
    RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
215

216 217
    /* this dc is not visible */
    if (rtgui_dc_get_visible(dc) != RT_TRUE) return;
218

219 220
    hdc = (struct rtgui_image_hdcmm *) image;
    RT_ASSERT(hdc != RT_NULL);
221

222
    /* the minimum rect */
223 224 225 226 227 228
    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);


229 230
    /* get pixel pointer */
    ptr = hdc->pixels;
231

232 233 234 235 236
    for (y = 0; y < h; y ++)
    {
        dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr);
        ptr += hdc->pitch;
    }
237 238
}

B
bernard.xiong 已提交
239 240
void rtgui_image_hdc_init()
{
241 242
    /* register hdc on image system */
    rtgui_image_register_engine(&rtgui_image_hdc_engine);
B
bernard.xiong 已提交
243
}