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
|
/*
* This file is subject to the terms of the GFX License. If a copy of
* the license was not distributed with this file, you can obtain one at:
*
* http://ugfx.io/license.html
*/
#include "../../gfx.h"
#if GFX_USE_GDISP && GDISP_NEED_IMAGE
#include "gdisp_image_support.h"
#if GDISP_NEED_IMAGE_NATIVE
extern gdispImageError gdispImageOpen_NATIVE(gdispImage *img);
extern void gdispImageClose_NATIVE(gdispImage *img);
extern gdispImageError gdispImageCache_NATIVE(gdispImage *img);
extern gdispImageError gdispGImageDraw_NATIVE(GDisplay *g, gdispImage *img, gCoord x, gCoord y, gCoord cx, gCoord cy, gCoord sx, gCoord sy);
extern gDelay gdispImageNext_NATIVE(gdispImage *img);
#endif
#if GDISP_NEED_IMAGE_GIF
extern gdispImageError gdispImageOpen_GIF(gdispImage *img);
extern void gdispImageClose_GIF(gdispImage *img);
extern gdispImageError gdispImageCache_GIF(gdispImage *img);
extern gdispImageError gdispGImageDraw_GIF(GDisplay *g, gdispImage *img, gCoord x, gCoord y, gCoord cx, gCoord cy, gCoord sx, gCoord sy);
extern gDelay gdispImageNext_GIF(gdispImage *img);
#endif
#if GDISP_NEED_IMAGE_BMP
extern gdispImageError gdispImageOpen_BMP(gdispImage *img);
extern void gdispImageClose_BMP(gdispImage *img);
extern gdispImageError gdispImageCache_BMP(gdispImage *img);
extern gdispImageError gdispGImageDraw_BMP(GDisplay *g, gdispImage *img, gCoord x, gCoord y, gCoord cx, gCoord cy, gCoord sx, gCoord sy);
extern gDelay gdispImageNext_BMP(gdispImage *img);
extern uint16_t gdispImageGetPaletteSize_BMP(gdispImage *img);
extern gColor gdispImageGetPalette_BMP(gdispImage *img, uint16_t index);
extern gBool gdispImageAdjustPalette_BMP(gdispImage *img, uint16_t index, gColor newColor);
#endif
#if GDISP_NEED_IMAGE_JPG
extern gdispImageError gdispImageOpen_JPG(gdispImage *img);
extern void gdispImageClose_JPG(gdispImage *img);
extern gdispImageError gdispImageCache_JPG(gdispImage *img);
extern gdispImageError gdispGImageDraw_JPG(GDisplay *g, gdispImage *img, gCoord x, gCoord y, gCoord cx, gCoord cy, gCoord sx, gCoord sy);
extern gDelay gdispImageNext_JPG(gdispImage *img);
#endif
#if GDISP_NEED_IMAGE_PNG
extern gdispImageError gdispImageOpen_PNG(gdispImage *img);
extern void gdispImageClose_PNG(gdispImage *img);
extern gdispImageError gdispImageCache_PNG(gdispImage *img);
extern gdispImageError gdispGImageDraw_PNG(GDisplay *g, gdispImage *img, gCoord x, gCoord y, gCoord cx, gCoord cy, gCoord sx, gCoord sy);
extern gDelay gdispImageNext_PNG(gdispImage *img);
#endif
/* The structure defining the routines for image drawing */
typedef struct gdispImageHandlers {
gdispImageError (*open)(gdispImage *img); /* The open function */
void (*close)(gdispImage *img); /* The close function */
gdispImageError (*cache)(gdispImage *img); /* The cache function */
gdispImageError (*draw)(GDisplay *g,
gdispImage *img,
gCoord x, gCoord y,
gCoord cx, gCoord cy,
gCoord sx, gCoord sy); /* The draw function */
gDelay (*next)(gdispImage *img); /* The next frame function */
uint16_t (*getPaletteSize)(gdispImage *img); /* Retrieve the size of the palette (number of entries) */
gColor (*getPalette)(gdispImage *img, uint16_t index); /* Retrieve a specific color value of the palette */
gBool (*adjustPalette)(gdispImage *img, uint16_t index, gColor newColor); /* Replace a color value in the palette */
} gdispImageHandlers;
static gdispImageHandlers ImageHandlers[] = {
#if GDISP_NEED_IMAGE_NATIVE
{ gdispImageOpen_NATIVE, gdispImageClose_NATIVE,
gdispImageCache_NATIVE, gdispGImageDraw_NATIVE, gdispImageNext_NATIVE,
0, 0, 0
},
#endif
#if GDISP_NEED_IMAGE_GIF
{ gdispImageOpen_GIF, gdispImageClose_GIF,
gdispImageCache_GIF, gdispGImageDraw_GIF, gdispImageNext_GIF,
0, 0, 0
},
#endif
#if GDISP_NEED_IMAGE_BMP
{ gdispImageOpen_BMP, gdispImageClose_BMP,
gdispImageCache_BMP, gdispGImageDraw_BMP, gdispImageNext_BMP,
gdispImageGetPaletteSize_BMP, gdispImageGetPalette_BMP, gdispImageAdjustPalette_BMP
},
#endif
#if GDISP_NEED_IMAGE_JPG
{ gdispImageOpen_JPG, gdispImageClose_JPG,
gdispImageCache_JPG, gdispGImageDraw_JPG, gdispImageNext_JPG,
0, 0, 0
},
#endif
#if GDISP_NEED_IMAGE_PNG
{ gdispImageOpen_PNG, gdispImageClose_PNG,
gdispImageCache_PNG, gdispGImageDraw_PNG, gdispImageNext_PNG,
0, 0, 0
},
#endif
};
void gdispImageInit(gdispImage *img) {
img->type = GDISP_IMAGE_TYPE_UNKNOWN;
}
gdispImageError gdispImageOpenGFile(gdispImage *img, GFILE *f) {
gdispImageError err;
if (!img)
return GDISP_IMAGE_ERR_NULLPOINTER;
if (!f)
return GDISP_IMAGE_ERR_NOSUCHFILE;
img->f = f;
img->bgcolor = GFX_WHITE;
for(img->fns = ImageHandlers; img->fns < ImageHandlers+sizeof(ImageHandlers)/sizeof(ImageHandlers[0]); img->fns++) {
err = img->fns->open(img);
if (err != GDISP_IMAGE_ERR_BADFORMAT) {
if ((err & GDISP_IMAGE_ERR_UNRECOVERABLE))
goto unrecoverable;
// Everything is possible
return err;
}
// Try the next decoder
gfileSetPos(img->f, 0);
}
err = GDISP_IMAGE_ERR_BADFORMAT;
img->type = GDISP_IMAGE_TYPE_UNKNOWN;
unrecoverable:
gfileClose(img->f);
img->f = 0;
img->flags = 0;
img->fns = 0;
img->priv = 0;
return err;
}
void gdispImageClose(gdispImage *img) {
if (!img)
return;
if (img->fns)
img->fns->close(img);
gfileClose(img->f);
img->type = GDISP_IMAGE_TYPE_UNKNOWN;
img->flags = 0;
img->fns = 0;
img->priv = 0;
}
gBool gdispImageIsOpen(gdispImage *img) {
return img && img->type != GDISP_IMAGE_TYPE_UNKNOWN && img->fns != 0;
}
void gdispImageSetBgColor(gdispImage *img, gColor bgcolor) {
if (!img)
return;
img->bgcolor = bgcolor;
}
gdispImageError gdispImageCache(gdispImage *img) {
if (!img) return GDISP_IMAGE_ERR_NULLPOINTER;
if (!img->fns) return GDISP_IMAGE_ERR_BADFORMAT;
return img->fns->cache(img);
}
gdispImageError gdispGImageDraw(GDisplay *g, gdispImage *img, gCoord x, gCoord y, gCoord cx, gCoord cy, gCoord sx, gCoord sy) {
if (!img) return GDISP_IMAGE_ERR_NULLPOINTER;
if (!img->fns) return GDISP_IMAGE_ERR_BADFORMAT;
// Check on window
if (cx <= 0 || cy <= 0) return GDISP_IMAGE_ERR_OK;
if (sx < 0) sx = 0;
if (sy < 0) sy = 0;
if (sx >= img->width || sy >= img->height) return GDISP_IMAGE_ERR_OK;
if (sx + cx > img->width) cx = img->width - sx;
if (sy + cy > img->height) cy = img->height - sy;
// Draw
return img->fns->draw(g, img, x, y, cx, cy, sx, sy);
}
gDelay gdispImageNext(gdispImage *img) {
if (!img) return GDISP_IMAGE_ERR_NULLPOINTER;
if (!img->fns) return GDISP_IMAGE_ERR_BADFORMAT;
return img->fns->next(img);
}
uint16_t gdispImageGetPaletteSize(gdispImage *img) {
if (!img || !img->fns) return 0;
if (!img->fns->getPaletteSize) return 0;
return img->fns->getPaletteSize(img);
}
gColor gdispImageGetPalette(gdispImage *img, uint16_t index) {
if (!img || !img->fns) return 0;
if (!img->fns->getPalette) return 0;
return img->fns->getPalette(img, index);
}
gBool gdispImageAdjustPalette(gdispImage *img, uint16_t index, gColor newColor) {
if (!img || !img->fns) return gFalse;
if (!img->fns->adjustPalette) return gFalse;
return img->fns->adjustPalette(img, index, newColor);
}
// Helper Routines
void *gdispImageAlloc(gdispImage *img, size_t sz) {
#if GDISP_NEED_IMAGE_ACCOUNTING
void *ptr;
ptr = gfxAlloc(sz);
if (ptr) {
img->memused += sz;
if (img->memused > img->maxmemused)
img->maxmemused = img->memused;
}
return ptr;
#else
(void) img;
return gfxAlloc(sz);
#endif
}
void gdispImageFree(gdispImage *img, void *ptr, size_t sz) {
#if GDISP_NEED_IMAGE_ACCOUNTING
gfxFree(ptr);
img->memused -= sz;
#else
(void) img;
(void) sz;
gfxFree(ptr);
#endif
}
#if GFX_CPU_ENDIAN != GFX_CPU_ENDIAN_LITTLE && GFX_CPU_ENDIAN != GFX_CPU_ENDIAN_BIG \
&& GFX_CPU_ENDIAN != GFX_CPU_ENDIAN_WBDWL && GFX_CPU_ENDIAN != GFX_CPU_ENDIAN_WLDWB
union wbyteorder_u {
uint8_t b[2];
uint32_t w;
};
union dwbyteorder_u {
uint8_t b[4];
uint32_t l;
};
uint16_t gdispImageH16toLE16(uint16_t w) {
union wbyteorder_u we;
we.w = w;
return (((uint16_t)we.b[0]))|(((uint16_t)we.b[1]) << 8);
}
uint16_t gdispImageH16toBE16(uint16_t w) {
union wbyteorder_u we;
we.w = w;
return (((uint16_t)we.b[0]) << 8)|(((uint16_t)we.b[1]));
}
uint32_t gdispImageH32toLE32(uint32_t dw) {
union dwbyteorder_u we;
we.l = dw;
return (((uint32_t)we.b[0]))
|(((uint32_t)we.b[1]) << 8)
|(((uint32_t)we.b[2]) << 16)
|(((uint32_t)we.b[3]) << 24);
}
uint32_t gdispImageH32toBE32(uint32_t dw) {
union dwbyteorder_u we;
we.l = dw;
return (((uint32_t)we.b[0]) << 24)
|(((uint32_t)we.b[1]) << 16)
|(((uint32_t)we.b[2]) << 8)
|(((uint32_t)we.b[3]));
}
#endif
#endif /* GFX_USE_GDISP && GDISP_NEED_IMAGE */
|