summaryrefslogtreecommitdiff
path: root/components/gui/src/image.c
blob: e8ab6c00a2b2bc269daa1d482edea09e8d3723c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * File      : image.c
 * This file is part of RT-Thread GUI Engine
 * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-10-16     Bernard      first version
 * 2012-01-24     onelife      add TJpgDec (Tiny JPEG Decompressor) support
 * 2012-08-29     amsl         add Image zoom interface.
 */
#include <rtthread.h>
#include <rtgui/image.h>

#include <rtgui/image_hdc.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/image_container.h>

#include <string.h>
#ifdef _WIN32
#define strncasecmp  strnicmp
#endif

#ifdef RTGUI_IMAGE_XPM
extern void rtgui_image_xpm_init(void);
#endif

#ifdef RTGUI_IMAGE_BMP
#include <rtgui/image_bmp.h>
#endif

#if (defined(RTGUI_IMAGE_JPEG) || defined(RTGUI_IMAGE_TJPGD))
extern void rtgui_image_jpeg_init(void);
#endif
#if defined(RTGUI_IMAGE_PNG) || defined(RTGUI_IMAGE_LODEPNG)
extern void rtgui_image_png_init(void);
#endif

static rtgui_list_t _rtgui_system_image_list = {RT_NULL};

/* initialize rtgui image system */
void rtgui_system_image_init(void)
{
    /* always support HDC image */
    rtgui_image_hdc_init();

#ifdef RTGUI_IMAGE_XPM
    rtgui_image_xpm_init();
#endif

#ifdef RTGUI_IMAGE_BMP
    rtgui_image_bmp_init();
#endif

#if (defined(RTGUI_IMAGE_JPEG) || defined(RTGUI_IMAGE_TJPGD))
    rtgui_image_jpeg_init();
#endif

#if defined(RTGUI_IMAGE_PNG) || defined(RTGUI_IMAGE_LODEPNG)
    rtgui_image_png_init();
#endif

#ifdef RTGUI_IMAGE_CONTAINER
    /* initialize image container */
    rtgui_system_image_container_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;
}

#if defined(RTGUI_USING_DFS_FILERW)
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;
}
RTM_EXPORT(rtgui_image_get_engine_by_filename);

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

        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;
}
RTM_EXPORT(rtgui_image_create_from_file);

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)
    {
        rt_kprintf("create filerw failed!\n");
        return RT_NULL;
    }

    /* get image engine */
    engine = rtgui_image_get_engine_by_filename(filename);
    if (engine == RT_NULL)
    {
        rt_kprintf("no engine for file: %s\n", filename);
        /* 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)
        {
            rt_kprintf("out of memory\n");
            /* close filerw context */
            rtgui_filerw_close(filerw);
            return RT_NULL;
        }

        image->palette = RT_NULL;
        if (engine->image_load(image, filerw, load) != RT_TRUE)
        {
            rt_kprintf("load image:%s failed!\n", filename);
            /* close filerw context */
            rtgui_filerw_close(filerw);
            return RT_NULL;
        }

        /* set image engine */
        image->engine = engine;
    }
    else
    {
        rt_kprintf("image:%s check failed!\n", filename);
        rtgui_filerw_close(filerw);
    }

    return image;
}
RTM_EXPORT(rtgui_image_create);
#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)
{
    struct rtgui_filerw *filerw;
    struct rtgui_image_engine *engine;
    struct rtgui_image *image = RT_NULL;

    /* create filerw context */
    filerw = rtgui_filerw_create_mem(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;
        }

        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;
}
RTM_EXPORT(rtgui_image_create_from_mem);

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

    image->engine->image_unload(image);
    if (image->palette != RT_NULL)
        rtgui_free(image->palette);
    rtgui_free(image);
}
RTM_EXPORT(rtgui_image_destroy);

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

void rtgui_image_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect)
{
    struct rtgui_rect r;
    RT_ASSERT(dc    != RT_NULL);

    if (rtgui_dc_get_visible(dc) != RT_TRUE) return;

    rtgui_dc_get_rect(dc, &r);

    /* use rect of DC */
    if (rect == RT_NULL)
    {
        rect = &r;
    }
    else
    {
        /* Don't modify x1, y1, they are handled in engine->image_blit. */
        if (rect->x1 > r.x2)
            return;
        if (rect->y1 > r.y2)
            return;

        if (rect->x2 > r.x2)
            rect->x2 = r.x2;
        if (rect->y2 > r.y2)
            rect->y2 = r.y2;
    }

    if (image != RT_NULL && image->engine != RT_NULL)
    {
        /* use image engine to blit */
        image->engine->image_blit(image, dc, rect);
    }
}
RTM_EXPORT(rtgui_image_blit);

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;
}
RTM_EXPORT(rtgui_image_palette_create);

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;
}
RTM_EXPORT(rtgui_image_get_rect);