@geastack/engine 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +29 -0
  3. package/bitmap_font.cpp +165 -0
  4. package/canvas.cpp +4608 -0
  5. package/components/Button.d.ts +4 -0
  6. package/components/Button.tsx +32 -0
  7. package/components/Image.d.ts +3 -0
  8. package/components/Image.tsx +17 -0
  9. package/components/Text.d.ts +3 -0
  10. package/components/Text.tsx +9 -0
  11. package/components/View.d.ts +3 -0
  12. package/components/View.tsx +24 -0
  13. package/components/index.d.ts +8 -0
  14. package/components/index.ts +4 -0
  15. package/css/animation.h +252 -0
  16. package/css/declarative.h +131 -0
  17. package/css/easing.h +160 -0
  18. package/css/engine.h +223 -0
  19. package/css/interpolate.h +38 -0
  20. package/image_store.cpp +807 -0
  21. package/package.json +62 -0
  22. package/rasterized_font.cpp +570 -0
  23. package/touch_runtime.cpp +513 -0
  24. package/ui/absolute_leaf_refresh.cpp +279 -0
  25. package/ui/absolute_leaf_refresh.h +12 -0
  26. package/ui/canvas_element.cpp +2314 -0
  27. package/ui/canvas_element.h +353 -0
  28. package/ui/canvas_store.cpp +188 -0
  29. package/ui/canvas_store.h +59 -0
  30. package/ui/core.cpp +135 -0
  31. package/ui/css_atom.cpp +155 -0
  32. package/ui/css_atom.h +21 -0
  33. package/ui/dirty_regions.cpp +66 -0
  34. package/ui/dirty_regions.h +34 -0
  35. package/ui/display_invalidation.cpp +101 -0
  36. package/ui/display_invalidation.h +15 -0
  37. package/ui/document.cpp +356 -0
  38. package/ui/document.h +101 -0
  39. package/ui/image.h +16 -0
  40. package/ui/image_element.cpp +13 -0
  41. package/ui/image_node.cpp +165 -0
  42. package/ui/input.cpp +936 -0
  43. package/ui/input_render.cpp +213 -0
  44. package/ui/internal.h +567 -0
  45. package/ui/layout.cpp +2025 -0
  46. package/ui/layout_snapshot.cpp +82 -0
  47. package/ui/layout_snapshot.h +12 -0
  48. package/ui/mirror.h +25 -0
  49. package/ui/node.cpp +243 -0
  50. package/ui/node.h +154 -0
  51. package/ui/node_lifecycle.cpp +137 -0
  52. package/ui/node_lifecycle.h +13 -0
  53. package/ui/node_model.h +597 -0
  54. package/ui/refresh_perf.h +235 -0
  55. package/ui/refresh_trace.h +68 -0
  56. package/ui/render.cpp +12097 -0
  57. package/ui/root_scroll_refresh.cpp +859 -0
  58. package/ui/root_scroll_refresh.h +30 -0
  59. package/ui/state_init.h +21 -0
  60. package/ui/style.cpp +14994 -0
  61. package/ui/style.h +727 -0
  62. package/ui/style_values.h +31 -0
  63. package/ui/text.cpp +2010 -0
  64. package/ui/text.h +15 -0
  65. package/ui/text_element.cpp +15 -0
  66. package/ui/tree_events.cpp +621 -0
  67. package/ui/tree_events.h +69 -0
  68. package/ui/tree_internal.h +191 -0
  69. package/ui/tree_nodes.cpp +628 -0
  70. package/ui/tree_render.cpp +3138 -0
  71. package/ui/tree_scroll_mirror.cpp +101 -0
  72. package/ui/tree_state.cpp +574 -0
  73. package/ui/tree_state.h +235 -0
  74. package/ui/tree_style.cpp +698 -0
  75. package/ui/ui.h +9 -0
  76. package/ui/view.cpp +2232 -0
  77. package/ui/view.h +14 -0
  78. package/ui/view_element.cpp +13 -0
  79. package/ui/viewport_region.cpp +36 -0
  80. package/ui/viewport_region.h +12 -0
  81. package/ui/virtual_keyboard.cpp +908 -0
  82. package/ui/virtual_keyboard.h +59 -0
  83. package/vendor/AnimatedGIF/AnimatedGIF.c +982 -0
  84. package/vendor/AnimatedGIF/AnimatedGIF.h +216 -0
  85. package/vendor/stb/stb_image.h +7988 -0
  86. package/vendor/stb/stb_truetype.h +5079 -0
@@ -0,0 +1,982 @@
1
+ //
2
+ // GIF Animator
3
+ // written by Larry Bank
4
+ // bitbank@pobox.com
5
+ // Arduino port started 7/5/2020
6
+ // Original GIF code written 20+ years ago :)
7
+ // The goal of this code is to decode images up to 480x320
8
+ // using no more than 22K of RAM (if sent directly to an LCD display)
9
+ //
10
+ // Copyright 2020 BitBank Software, Inc. All Rights Reserved.
11
+ // Licensed under the Apache License, Version 2.0 (the "License");
12
+ // you may not use this file except in compliance with the License.
13
+ // You may obtain a copy of the License at
14
+ // http://www.apache.org/licenses/LICENSE-2.0
15
+ // Unless required by applicable law or agreed to in writing, software
16
+ // distributed under the License is distributed on an "AS IS" BASIS,
17
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ // See the License for the specific language governing permissions and
19
+ // limitations under the License.
20
+ //===========================================================================
21
+ #include "AnimatedGIF.h"
22
+
23
+ #include <string.h>
24
+
25
+ #ifndef c_memcpy
26
+ #define c_memcpy memcpy
27
+ #endif
28
+ #ifndef c_memcmp
29
+ #define c_memcmp memcmp
30
+ #endif
31
+
32
+ #ifdef HAL_ESP32_HAL_H_
33
+ #define memcpy_P memcpy
34
+ #endif
35
+
36
+ static const unsigned char cGIFBits[9] = {1,4,4,4,8,8,8,8,8}; // convert odd bpp values to ones we can handle
37
+
38
+ // forward references
39
+ static int GIFInit(GIFIMAGE *pGIF);
40
+ static int GIFParseInfo(GIFIMAGE *pPage, int bInfoOnly);
41
+ static int GIFGetMoreData(GIFIMAGE *pPage);
42
+ static void GIFMakePels(GIFIMAGE *pPage, unsigned int code);
43
+ static int DecodeLZW(GIFIMAGE *pImage, int iOptions);
44
+ static int32_t readMem(GIFFILE *pFile, uint8_t *pBuf, int32_t iLen);
45
+ static int32_t seekMem(GIFFILE *pFile, int32_t iPosition);
46
+ int GIFGetInfo(GIFIMAGE *pPage, GIFINFO *pInfo, unsigned short *pPalette);
47
+ #if defined( PICO_BUILD ) || defined( __LINUX__ ) || defined( __MCUXPRESSO ) || defined( __EMSCRIPTEN__ ) || defined( ESP_PLATFORM ) || defined( GEA_EMBEDDED_GIF_C_API )
48
+ static int32_t readFile(GIFFILE *pFile, uint8_t *pBuf, int32_t iLen);
49
+ static int32_t seekFile(GIFFILE *pFile, int32_t iPosition);
50
+ static void closeFile(void *handle);
51
+
52
+ // C API
53
+ int GIF_openRAM(GIFIMAGE *pGIF, uint8_t *pData, int iDataSize, GIF_DRAW_CALLBACK *pfnDraw)
54
+ {
55
+ pGIF->iError = GIF_SUCCESS;
56
+ pGIF->pfnRead = readMem;
57
+ pGIF->pfnSeek = seekMem;
58
+ pGIF->pfnDraw = pfnDraw;
59
+ pGIF->pfnOpen = NULL;
60
+ pGIF->pfnClose = NULL;
61
+ pGIF->GIFFile.iSize = iDataSize;
62
+ pGIF->GIFFile.pData = pData;
63
+ return GIFInit(pGIF);
64
+ } /* GIF_openRAM() */
65
+
66
+ #ifdef __LINUX__
67
+ int GIF_openFile(GIFIMAGE *pGIF, const char *szFilename, GIF_DRAW_CALLBACK *pfnDraw)
68
+ {
69
+ pGIF->iError = GIF_SUCCESS;
70
+ pGIF->pfnRead = readFile;
71
+ pGIF->pfnSeek = seekFile;
72
+ pGIF->pfnDraw = pfnDraw;
73
+ pGIF->pfnOpen = NULL;
74
+ pGIF->pfnClose = closeFile;
75
+ pGIF->GIFFile.fHandle = fopen(szFilename, "r+b");
76
+ if (pGIF->GIFFile.fHandle == NULL)
77
+ return 0;
78
+ fseek((FILE *)pGIF->GIFFile.fHandle, 0, SEEK_END);
79
+ pGIF->GIFFile.iSize = (int)ftell((FILE *)pGIF->GIFFile.fHandle);
80
+ fseek((FILE *)pGIF->GIFFile.fHandle, 0, SEEK_SET);
81
+ return GIFInit(pGIF);
82
+ } /* GIF_openFile() */
83
+ #endif
84
+
85
+ void GIF_close(GIFIMAGE *pGIF)
86
+ {
87
+ if (pGIF->pfnClose)
88
+ (*pGIF->pfnClose)(pGIF->GIFFile.fHandle);
89
+ } /* GIF_close() */
90
+
91
+ void GIF_begin(GIFIMAGE *pGIF, int iEndian, unsigned char ucPaletteType)
92
+ {
93
+ memset(pGIF, 0, sizeof(GIFIMAGE));
94
+ pGIF->ucLittleEndian = (iEndian == LITTLE_ENDIAN_PIXELS);
95
+ pGIF->ucPaletteType = ucPaletteType;
96
+ } /* GIF_begin() */
97
+
98
+ void GIF_reset(GIFIMAGE *pGIF)
99
+ {
100
+ (*pGIF->pfnSeek)(&pGIF->GIFFile, 0);
101
+ } /* GIF_reset() */
102
+
103
+ //
104
+ // Return value:
105
+ // 1 = good decode, more frames exist
106
+ // 0 = good decode, no more frames
107
+ // -1 = error
108
+ //
109
+ int GIF_playFrame(GIFIMAGE *pGIF, int *delayMilliseconds)
110
+ {
111
+ int rc;
112
+
113
+ if (delayMilliseconds)
114
+ *delayMilliseconds = 0; // clear any old valid
115
+ if (pGIF->GIFFile.iPos >= pGIF->GIFFile.iSize-1) // no more data exists
116
+ {
117
+ (*pGIF->pfnSeek)(&pGIF->GIFFile, 0); // seek to start
118
+ }
119
+ if (GIFParseInfo(pGIF, 0))
120
+ {
121
+ if (pGIF->iError == GIF_EMPTY_FRAME) // don't try to decode it
122
+ return 0;
123
+ rc = DecodeLZW(pGIF, 0);
124
+ if (rc != 0) // problem
125
+ return -1;
126
+ }
127
+ else
128
+ {
129
+ return -1; // error parsing the frame info, we may be at the end of the file
130
+ }
131
+ // Return 1 for more frames or 0 if this was the last frame
132
+ if (delayMilliseconds) // if not NULL, return the frame delay time
133
+ *delayMilliseconds = pGIF->iFrameDelay;
134
+ return (pGIF->GIFFile.iPos < pGIF->GIFFile.iSize-1);
135
+ } /* GIF_playFrame() */
136
+
137
+ int GIF_getCanvasWidth(GIFIMAGE *pGIF)
138
+ {
139
+ return pGIF->iCanvasWidth;
140
+ } /* GIF_getCanvasWidth() */
141
+
142
+ int GIF_getCanvasHeight(GIFIMAGE *pGIF)
143
+ {
144
+ return pGIF->iCanvasHeight;
145
+ } /* GIF_getCanvasHeight() */
146
+
147
+ int GIF_getComment(GIFIMAGE *pGIF, char *pDest)
148
+ {
149
+ int32_t iOldPos;
150
+
151
+ iOldPos = pGIF->GIFFile.iPos; // keep old position
152
+ (*pGIF->pfnSeek)(&pGIF->GIFFile, pGIF->iCommentPos);
153
+ (*pGIF->pfnRead)(&pGIF->GIFFile, (uint8_t *)pDest, pGIF->sCommentLen);
154
+ (*pGIF->pfnSeek)(&pGIF->GIFFile, iOldPos);
155
+ pDest[pGIF->sCommentLen] = 0; // zero terminate the string
156
+ return (int)pGIF->sCommentLen;
157
+
158
+ } /* GIF_getComment() */
159
+
160
+ int GIF_getLastError(GIFIMAGE *pGIF)
161
+ {
162
+ return pGIF->iError;
163
+ } /* GIF_getLastError() */
164
+
165
+ int GIF_getInfo(GIFIMAGE *pGIF, GIFINFO *pInfo, unsigned short *pPalette)
166
+ {
167
+ return GIFGetInfo(pGIF, pInfo, pPalette);
168
+ } /* GIF_getInfo() */
169
+
170
+ #endif // !__cplusplus
171
+ //
172
+ // Helper functions for memory based images
173
+ //
174
+ static int32_t readMem(GIFFILE *pFile, uint8_t *pBuf, int32_t iLen)
175
+ {
176
+ int32_t iBytesRead;
177
+
178
+ iBytesRead = iLen;
179
+ if ((pFile->iSize - pFile->iPos) < iLen)
180
+ iBytesRead = pFile->iSize - pFile->iPos;
181
+ if (iBytesRead <= 0)
182
+ return 0;
183
+ c_memcpy(pBuf, &pFile->pData[pFile->iPos], iBytesRead);
184
+ pFile->iPos += iBytesRead;
185
+ return iBytesRead;
186
+ } /* readMem() */
187
+
188
+ static int32_t seekMem(GIFFILE *pFile, int32_t iPosition)
189
+ {
190
+ if (iPosition < 0) iPosition = 0;
191
+ else if (iPosition >= pFile->iSize) iPosition = pFile->iSize-1;
192
+ pFile->iPos = iPosition;
193
+ return iPosition;
194
+ } /* seekMem() */
195
+
196
+ #if defined ( __LINUX__ ) || defined( __MCUXPRESSO )
197
+ static void closeFile(void *handle)
198
+ {
199
+ fclose((FILE *)handle);
200
+ } /* closeFile() */
201
+
202
+ static int32_t seekFile(GIFFILE *pFile, int32_t iPosition)
203
+ {
204
+ if (iPosition < 0) iPosition = 0;
205
+ else if (iPosition >= pFile->iSize) iPosition = pFile->iSize-1;
206
+ pFile->iPos = iPosition;
207
+ fseek((FILE *)pFile->fHandle, iPosition, SEEK_SET);
208
+ return iPosition;
209
+ } /* seekMem() */
210
+
211
+ static int32_t readFile(GIFFILE *pFile, uint8_t *pBuf, int32_t iLen)
212
+ {
213
+ int32_t iBytesRead;
214
+
215
+ iBytesRead = iLen;
216
+ if ((pFile->iSize - pFile->iPos) < iLen)
217
+ iBytesRead = pFile->iSize - pFile->iPos;
218
+ if (iBytesRead <= 0)
219
+ return 0;
220
+ iBytesRead = (int)fread(pBuf, 1, iBytesRead, (FILE *)pFile->fHandle);
221
+ pFile->iPos += iBytesRead;
222
+ return iBytesRead;
223
+ } /* readFile() */
224
+
225
+ #endif // __LINUX__
226
+ //
227
+ // The following functions are written in plain C and have no
228
+ // 3rd party dependencies, not even the C runtime library
229
+ //
230
+ //
231
+ // Initialize a GIF file and callback access from a file on SD or memory
232
+ // returns 1 for success, 0 for failure
233
+ // Fills in the canvas size of the GIFIMAGE structure
234
+ //
235
+ static int GIFInit(GIFIMAGE *pGIF)
236
+ {
237
+ pGIF->GIFFile.iPos = 0; // start at beginning of file
238
+ if (!GIFParseInfo(pGIF, 1)) // gather info for the first frame
239
+ return 0; // something went wrong; not a GIF file?
240
+ (*pGIF->pfnSeek)(&pGIF->GIFFile, 0); // seek back to start of the file
241
+ if (pGIF->iCanvasWidth > MAX_WIDTH) { // need to allocate more space
242
+ pGIF->iError = GIF_TOO_WIDE;
243
+ return 0;
244
+ }
245
+ return 1;
246
+ } /* GIFInit() */
247
+
248
+ //
249
+ // Parse the GIF header, gather the size and palette info
250
+ // If called with bInfoOnly set to true, it will test for a valid file
251
+ // and return the canvas size only
252
+ // Returns 1 for success, 0 for failure
253
+ //
254
+ static int GIFParseInfo(GIFIMAGE *pPage, int bInfoOnly)
255
+ {
256
+ int i, j, iColorTableBits;
257
+ int iBytesRead;
258
+ unsigned char c, *p;
259
+ int32_t iOffset = 0;
260
+ int32_t iStartPos = pPage->GIFFile.iPos; // starting file position
261
+ int iReadSize;
262
+
263
+ pPage->bUseLocalPalette = 0; // assume no local palette
264
+ pPage->bEndOfFrame = 0; // we're just getting started
265
+ pPage->iFrameDelay = 0; // may not have a gfx extension block
266
+ iReadSize = (bInfoOnly) ? 12 : MAX_CHUNK_SIZE;
267
+ // If you try to read past the EOF, the SD lib will return garbage data
268
+ if (iStartPos + iReadSize > pPage->GIFFile.iSize)
269
+ iReadSize = (pPage->GIFFile.iSize - iStartPos - 1);
270
+ p = pPage->ucFileBuf;
271
+ iBytesRead = (*pPage->pfnRead)(&pPage->GIFFile, pPage->ucFileBuf, iReadSize); // 255 is plenty for now
272
+
273
+ if (iBytesRead != iReadSize) // we're at the end of the file
274
+ {
275
+ pPage->iError = GIF_EARLY_EOF;
276
+ return 0;
277
+ }
278
+ if (iStartPos == 0) // start of the file
279
+ { // canvas size
280
+ if (c_memcmp(p, "GIF89", 5) != 0 && c_memcmp(p, "GIF87", 5) != 0) // not a GIF file
281
+ {
282
+ pPage->iError = GIF_BAD_FILE;
283
+ return 0;
284
+ }
285
+ pPage->iCanvasWidth = pPage->iWidth = INTELSHORT(&p[6]);
286
+ pPage->iCanvasHeight = pPage->iHeight = INTELSHORT(&p[8]);
287
+ pPage->iBpp = ((p[10] & 0x70) >> 4) + 1;
288
+ if (bInfoOnly)
289
+ return 1; // we've got the info we needed, leave
290
+ iColorTableBits = (p[10] & 7) + 1; // Log2(size) of the color table
291
+ pPage->ucBackground = p[11]; // background color
292
+ pPage->ucGIFBits = 0;
293
+ pPage->sGlobalColorTableCount = 0;
294
+ iOffset = 13;
295
+ if (p[10] & 0x80) // global color table?
296
+ { // by default, convert to byte-reversed RGB565 for immediate use
297
+ // Read enough additional data for the color table
298
+ iBytesRead += (*pPage->pfnRead)(&pPage->GIFFile, &pPage->ucFileBuf[iBytesRead], 3*(1<<iColorTableBits));
299
+ if (pPage->ucPaletteType == GIF_PALETTE_RGB565)
300
+ {
301
+ for (i=0; i<(1<<iColorTableBits); i++)
302
+ {
303
+ uint16_t usRGB565;
304
+ usRGB565 = ((p[iOffset] >> 3) << 11); // R
305
+ usRGB565 |= ((p[iOffset+1] >> 2) << 5); // G
306
+ usRGB565 |= (p[iOffset+2] >> 3); // B
307
+ if (pPage->ucLittleEndian)
308
+ pPage->pPalette[i] = usRGB565;
309
+ else
310
+ #if WIN32
311
+ pPage->pPalette[i] = _byteswap_ushort(usRGB565); // SPI wants MSB first
312
+ #else
313
+ pPage->pPalette[i] = __builtin_bswap16(usRGB565); // SPI wants MSB first
314
+ #endif
315
+ iOffset += 3;
316
+ }
317
+ }
318
+ else // just copy it as-is
319
+ {
320
+ c_memcpy(pPage->pPalette, &p[iOffset], (1<<iColorTableBits) * 3);
321
+ iOffset += (1 << iColorTableBits) * 3;
322
+ }
323
+ pPage->sGlobalColorTableCount = 1 << iColorTableBits;
324
+ }
325
+ }
326
+ while (p[iOffset] != ',') /* Wait for image separator */
327
+ {
328
+ if (p[iOffset] == '!') /* Extension block */
329
+ {
330
+ iOffset++;
331
+ switch(p[iOffset++]) /* Block type */
332
+ {
333
+ case 0xf9: /* Graphic extension */
334
+ if (p[iOffset] == 4) // correct length
335
+ {
336
+ pPage->ucGIFBits = p[iOffset+1]; // packed fields
337
+ pPage->iFrameDelay = (INTELSHORT(&p[iOffset+2]))*10; // delay in ms
338
+ if (pPage->iFrameDelay <= 1) // 0-1 is going to make it run at 60fps; use 100 (10fps) as a reasonable substitute
339
+ pPage->iFrameDelay = 100;
340
+ if (pPage->ucGIFBits & 1) // transparent color is used
341
+ pPage->ucTransparent = p[iOffset+4]; // transparent color index
342
+ iOffset += 6;
343
+ }
344
+ // else // error
345
+ break;
346
+ case 0xff: /* App extension */
347
+ c = 1;
348
+ while (c) /* Skip all data sub-blocks */
349
+ {
350
+ c = p[iOffset++]; /* Block length */
351
+ if ((iBytesRead - iOffset) < (c+32)) // need to read more data first
352
+ {
353
+ c_memcpy(pPage->ucFileBuf, &pPage->ucFileBuf[iOffset], (iBytesRead-iOffset)); // move existing data down
354
+ iBytesRead -= iOffset;
355
+ iStartPos += iOffset;
356
+ iOffset = 0;
357
+ iBytesRead += (*pPage->pfnRead)(&pPage->GIFFile, &pPage->ucFileBuf[iBytesRead], c+32);
358
+ }
359
+ if (c == 11) // fixed block length
360
+ { // Netscape app block contains the repeat count
361
+ if (c_memcmp(&p[iOffset], "NETSCAPE2.0", 11) == 0)
362
+ {
363
+ // if (p[iOffset+11] == 3 && p[iOffset+12] == 1) // loop count
364
+ // pPage->iRepeatCount = INTELSHORT(&p[iOffset+13]);
365
+ }
366
+ }
367
+ iOffset += (int)c; /* Skip to next sub-block */
368
+ }
369
+ break;
370
+ case 0x01: /* Text extension */
371
+ c = 1;
372
+ j = 0;
373
+ while (c) /* Skip all data sub-blocks */
374
+ {
375
+ c = p[iOffset++]; /* Block length */
376
+ if (j == 0) // use only first block
377
+ {
378
+ j = c;
379
+ if (j > 127) // max comment length = 127
380
+ j = 127;
381
+ // memcpy(pPage->szInfo1, &p[iOffset], j);
382
+ // pPage->szInfo1[j] = '\0';
383
+ j = 1;
384
+ }
385
+ iOffset += (int)c; /* Skip this sub-block */
386
+ }
387
+ break;
388
+ case 0xfe: /* Comment */
389
+ c = 1;
390
+ while (c) /* Skip all data sub-blocks */
391
+ {
392
+ c = p[iOffset++]; /* Block length */
393
+ if ((iBytesRead - iOffset) < (c+32)) // need to read more data first
394
+ {
395
+ c_memcpy(pPage->ucFileBuf, &pPage->ucFileBuf[iOffset], (iBytesRead-iOffset)); // move existing data down
396
+ iBytesRead -= iOffset;
397
+ iStartPos += iOffset;
398
+ iOffset = 0;
399
+ iBytesRead += (*pPage->pfnRead)(&pPage->GIFFile, &pPage->ucFileBuf[iBytesRead], c+32);
400
+ }
401
+ if (pPage->iCommentPos == 0) // Save first block info
402
+ {
403
+ pPage->iCommentPos = iStartPos + iOffset;
404
+ pPage->sCommentLen = c;
405
+ }
406
+ iOffset += (int)c; /* Skip this sub-block */
407
+ }
408
+ break;
409
+ default:
410
+ /* Bad header info */
411
+ pPage->iError = GIF_DECODE_ERROR;
412
+ return 0;
413
+ } /* switch */
414
+ }
415
+ else // invalid byte, stop decoding
416
+ {
417
+ if (0x3b == p[iOffset])
418
+ pPage->iError = GIF_EARLY_EOF;
419
+ else if (pPage->GIFFile.iSize - iStartPos < 32) // non-image bytes at end of file?
420
+ pPage->iError = GIF_EMPTY_FRAME;
421
+ else
422
+ /* Bad header info */
423
+ pPage->iError = GIF_DECODE_ERROR;
424
+ return 0;
425
+ }
426
+ } /* while */
427
+ if (p[iOffset] == ',')
428
+ iOffset++;
429
+ // This particular frame's size and position on the main frame (if animated)
430
+ pPage->iX = INTELSHORT(&p[iOffset]);
431
+ pPage->iY = INTELSHORT(&p[iOffset+2]);
432
+ pPage->iWidth = INTELSHORT(&p[iOffset+4]);
433
+ pPage->iHeight = INTELSHORT(&p[iOffset+6]);
434
+ iOffset += 8;
435
+
436
+ /* Image descriptor
437
+ 7 6 5 4 3 2 1 0 M=0 - use global color map, ignore pixel
438
+ M I 0 0 0 pixel M=1 - local color map follows, use pixel
439
+ I=0 - Image in sequential order
440
+ I=1 - Image in interlaced order
441
+ pixel+1 = # bits per pixel for this image
442
+ */
443
+ pPage->ucMap = p[iOffset++];
444
+ if (pPage->ucMap & 0x80) // local color table?
445
+ {// by default, convert to byte-reversed RGB565 for immediate use
446
+ j = (1<<((pPage->ucMap & 7)+1));
447
+ // Read enough additional data for the color table
448
+ iBytesRead += (*pPage->pfnRead)(&pPage->GIFFile, &pPage->ucFileBuf[iBytesRead], j*3);
449
+ if (pPage->ucPaletteType == GIF_PALETTE_RGB565)
450
+ {
451
+ for (i=0; i<j; i++)
452
+ {
453
+ uint16_t usRGB565;
454
+ usRGB565 = ((p[iOffset] >> 3) << 11); // R
455
+ usRGB565 |= ((p[iOffset+1] >> 2) << 5); // G
456
+ usRGB565 |= (p[iOffset+2] >> 3); // B
457
+ if (pPage->ucLittleEndian)
458
+ pPage->pLocalPalette[i] = usRGB565;
459
+ else
460
+ #if WIN32
461
+ pPage->pLocalPalette[i] = _byteswap_ushort(usRGB565); // SPI wants MSB first
462
+ #else
463
+ pPage->pLocalPalette[i] = __builtin_bswap16(usRGB565); // SPI wants MSB first
464
+ #endif
465
+ iOffset += 3;
466
+ }
467
+ }
468
+ else // just copy it as-is
469
+ {
470
+ c_memcpy(pPage->pLocalPalette, &p[iOffset], j * 3);
471
+ iOffset += j*3;
472
+ }
473
+ pPage->bUseLocalPalette = 1;
474
+ }
475
+ pPage->ucCodeStart = p[iOffset++]; /* initial code size */
476
+ /* Since GIF can be 1-8 bpp, we only allow 1,4,8 */
477
+ pPage->iBpp = cGIFBits[pPage->ucCodeStart];
478
+ // we are re-using the same buffer turning GIF file data
479
+ // into "pure" LZW
480
+ pPage->iLZWSize = 0; // we're starting with no LZW data yet
481
+ c = 1; // get chunk length
482
+ while (c && iOffset < iBytesRead)
483
+ {
484
+ // Serial.printf("iOffset=%d, iBytesRead=%d\n", iOffset, iBytesRead);
485
+ c = p[iOffset++]; // get chunk length
486
+ // Serial.printf("Chunk size = %d\n", c);
487
+ if (c <= (iBytesRead - iOffset))
488
+ {
489
+ c_memcpy(&pPage->ucLZW[pPage->iLZWSize], &p[iOffset], c);
490
+ pPage->iLZWSize += c;
491
+ iOffset += c;
492
+ }
493
+ else // partial chunk in our buffer
494
+ {
495
+ int iPartialLen = (iBytesRead - iOffset);
496
+ c_memcpy(&pPage->ucLZW[pPage->iLZWSize], &p[iOffset], iPartialLen);
497
+ pPage->iLZWSize += iPartialLen;
498
+ iOffset += iPartialLen;
499
+ (*pPage->pfnRead)(&pPage->GIFFile, &pPage->ucLZW[pPage->iLZWSize], c - iPartialLen);
500
+ pPage->iLZWSize += (c - iPartialLen);
501
+ }
502
+ if (c == 0)
503
+ pPage->bEndOfFrame = 1; // signal not to read beyond the end of the frame
504
+ }
505
+ // seeking on an SD card is VERY VERY SLOW, so use the data we've already read by de-chunking it
506
+ // in this case, there's too much data, so we have to seek backwards a bit
507
+ if (iOffset < iBytesRead)
508
+ {
509
+ // Serial.printf("Need to seek back %d bytes\n", iBytesRead - iOffset);
510
+ (*pPage->pfnSeek)(&pPage->GIFFile, iStartPos + iOffset); // position file to new spot
511
+ }
512
+ return 1; // we are now at the start of the chunk data
513
+ } /* GIFParseInfo() */
514
+ //
515
+ // Gather info about an animated GIF file
516
+ //
517
+ int GIFGetInfo(GIFIMAGE *pPage, GIFINFO *pInfo, unsigned short *pPalette)
518
+ {
519
+ int iOff, iNumFrames;
520
+ int iDelay, iMaxDelay, iMinDelay, iTotalDelay;
521
+ int iReadAmount;
522
+ int iDataAvailable = 0;
523
+ int iDataRemaining = 0;
524
+ uint32_t lFileOff = 0;
525
+ int bDone = 0;
526
+ int bExt;
527
+ uint8_t c, *cBuf;
528
+ uint16_t sGlobalColorTableSize = 0;
529
+ uint8_t ucHasLocalColorTable = 0, ucHasTransparent = 0, ucBackground = 0;
530
+
531
+ iMaxDelay = iTotalDelay = 0;
532
+ iMinDelay = 10000;
533
+ iNumFrames = 1;
534
+ iDataRemaining = pPage->GIFFile.iSize;
535
+ cBuf = (uint8_t *) pPage->ucFileBuf;
536
+ (*pPage->pfnSeek)(&pPage->GIFFile, 0);
537
+ iDataAvailable = (*pPage->pfnRead)(&pPage->GIFFile, cBuf, FILE_BUF_SIZE);
538
+ iOff = 10;
539
+ c = cBuf[iOff]; // get info bits
540
+ ucBackground = cBuf[iOff + 1];
541
+ iOff += 3; /* Skip flags, background color & aspect ratio */
542
+ if (c & 0x80) /* Deal with global color table */
543
+ {
544
+ c &= 7; /* Get the number of colors defined */
545
+ sGlobalColorTableSize = 2 << c;
546
+ if (pPalette) {
547
+ unsigned short i;
548
+ for (i=0; i<sGlobalColorTableSize; i++)
549
+ {
550
+ pPalette[i] =
551
+ ((cBuf[iOff] >> 3) << 11) |
552
+ ((cBuf[iOff+1] >> 2) << 5) |
553
+ (cBuf[iOff+2] >> 3);
554
+ iOff += 3;
555
+ }
556
+ }
557
+ else
558
+ iOff += sGlobalColorTableSize*3; /* skip color table */
559
+ }
560
+ while (!bDone) // && iNumFrames < MAX_FRAMES)
561
+ {
562
+ bExt = 1; /* skip extension blocks */
563
+ while (bExt && iOff < iDataAvailable)
564
+ {
565
+ switch(cBuf[iOff])
566
+ {
567
+ case 0x3b: /* End of file */
568
+ /* we were fooled into thinking there were more pages */
569
+ iNumFrames--;
570
+ goto gifpagesz;
571
+ // F9 = Graphic Control Extension (fixed length of 4 bytes)
572
+ // FE = Comment Extension
573
+ // FF = Application Extension
574
+ // 01 = Plain Text Extension
575
+ case 0x21: /* Extension block */
576
+ if (cBuf[iOff+1] == 0xf9 && cBuf[iOff+2] == 4) // Graphic Control Extension
577
+ {
578
+ //cBuf[iOff+3]; // page disposition flags
579
+ iDelay = cBuf[iOff+4]; // delay low byte
580
+ iDelay |= (cBuf[iOff+5] << 8); // delay high byte
581
+ if (iDelay < 2) // too fast, provide a default
582
+ iDelay = 2;
583
+ iDelay *= 10; // turn JIFFIES into milliseconds
584
+ iTotalDelay += iDelay;
585
+ if (iDelay > iMaxDelay) iMaxDelay = iDelay;
586
+ else if (iDelay < iMinDelay) iMinDelay = iDelay;
587
+ // (cBuf[iOff+6]; // transparent color index
588
+ if ((cBuf[iOff+3] & 1) && (ucBackground == cBuf[iOff+6]))
589
+ ucHasTransparent = 1;
590
+ }
591
+ iOff += 2; /* skip to length */
592
+ iOff += (int)cBuf[iOff]; /* Skip the data block */
593
+ iOff++;
594
+ // block terminator or optional sub blocks
595
+ c = cBuf[iOff++]; /* Skip any sub-blocks */
596
+ while (c && iOff < (iDataAvailable - c))
597
+ {
598
+ iOff += (int)c;
599
+ c = cBuf[iOff++];
600
+ }
601
+ if (c != 0) // problem, we went past the end
602
+ {
603
+ iNumFrames--; // possible corrupt data; stop
604
+ goto gifpagesz;
605
+ }
606
+ break;
607
+ case 0x2c: /* Start of image data */
608
+ bExt = 0; /* Stop doing extension blocks */
609
+ break;
610
+ default:
611
+ /* Corrupt data, stop here */
612
+ iNumFrames--;
613
+ goto gifpagesz;
614
+ } // switch
615
+ } // while
616
+ if (iOff >= iDataAvailable) // problem
617
+ {
618
+ iNumFrames--; // possible corrupt data; stop
619
+ goto gifpagesz;
620
+ }
621
+ /* Start of image data */
622
+ c = cBuf[iOff+9]; /* Get the flags byte */
623
+ iOff += 10; /* Skip image position and size */
624
+ if (c & 0x80) /* Local color table */
625
+ {
626
+ c &= 7;
627
+ iOff += (2<<c)*3;
628
+ ucHasLocalColorTable = 1;
629
+ }
630
+ iOff++; /* Skip LZW code size byte */
631
+ c = cBuf[iOff++];
632
+ while (c) /* While there are more data blocks */
633
+ {
634
+ if (iOff > (3*FILE_BUF_SIZE/4) && iDataRemaining > 0) /* Near end of buffer, re-align */
635
+ {
636
+ lFileOff += iOff; /* adjust total file pointer */
637
+ iDataRemaining -= iOff;
638
+ iReadAmount = (iDataRemaining > FILE_BUF_SIZE) ? FILE_BUF_SIZE:iDataRemaining;
639
+ (*pPage->pfnSeek)(&pPage->GIFFile, lFileOff);
640
+ iDataAvailable = (*pPage->pfnRead)(&pPage->GIFFile, cBuf, iReadAmount);
641
+ iOff = 0; /* Start at beginning of buffer */
642
+ }
643
+ iOff += (int)c; /* Skip this data block */
644
+ if ((int)lFileOff + iOff > pPage->GIFFile.iSize) // past end of file, stop
645
+ {
646
+ iNumFrames--; // don't count this page
647
+ break; // last page is corrupted, don't use it
648
+ }
649
+ c = cBuf[iOff++]; /* Get length of next */
650
+ }
651
+ /* End of image data, check for more pages... */
652
+ if (((int)lFileOff + iOff > pPage->GIFFile.iSize) || cBuf[iOff] == 0x3b)
653
+ {
654
+ bDone = 1; /* End of file has been reached */
655
+ }
656
+ else /* More pages to scan */
657
+ {
658
+ iNumFrames++;
659
+ if (!pInfo) goto gifpagesz; //@ signal to stop on first frame
660
+ // read new page data starting at this offset
661
+ if (pPage->GIFFile.iSize > FILE_BUF_SIZE && iDataRemaining > 0) // since we didn't read the whole file in one shot
662
+ {
663
+ lFileOff += iOff; /* adjust total file pointer */
664
+ iDataRemaining -= iOff;
665
+ iReadAmount = (iDataRemaining > FILE_BUF_SIZE) ? FILE_BUF_SIZE : iDataRemaining;
666
+ (*pPage->pfnSeek)(&pPage->GIFFile, lFileOff);
667
+ iDataAvailable = (*pPage->pfnRead)(&pPage->GIFFile, cBuf, iReadAmount);
668
+ iOff = 0; /* Start at beginning of buffer */
669
+ }
670
+ }
671
+ } /* while !bDone */
672
+ gifpagesz:
673
+ if (pInfo) {
674
+ pInfo->iFrameCount = iNumFrames;
675
+ pInfo->iMaxDelay = iMaxDelay;
676
+ pInfo->iMinDelay = iMinDelay;
677
+ pInfo->iDuration = iTotalDelay;
678
+ pInfo->sGlobalColorTableSize = sGlobalColorTableSize;
679
+ pInfo->ucHasLocalColorTable = ucHasLocalColorTable;
680
+ pInfo->ucHasTransparent = ucHasTransparent;
681
+ }
682
+ return iNumFrames ? 1 : 0;
683
+ } /* GIFGetInfo() */
684
+
685
+ //
686
+ // Unpack more chunk data for decoding
687
+ // returns 1 to signify more data available for this image
688
+ // 0 indicates there is no more data
689
+ //
690
+ static int GIFGetMoreData(GIFIMAGE *pPage)
691
+ {
692
+ int iDelta = (pPage->iLZWSize - pPage->iLZWOff);
693
+ unsigned char c = 1;
694
+ // move any existing data down
695
+ if (pPage->bEndOfFrame || iDelta >= (LZW_BUF_SIZE - MAX_CHUNK_SIZE) || iDelta <= 0)
696
+ return 1; // frame is finished or buffer is already full; no need to read more data
697
+ if (pPage->iLZWOff != 0)
698
+ {
699
+ // NB: memcpy() fails on some systems because the src and dest ptrs overlap
700
+ // so copy the bytes in a simple loop to avoid problems
701
+ for (int i=0; i<pPage->iLZWSize - pPage->iLZWOff; i++) {
702
+ pPage->ucLZW[i] = pPage->ucLZW[i + pPage->iLZWOff];
703
+ }
704
+ pPage->iLZWSize -= pPage->iLZWOff;
705
+ pPage->iLZWOff = 0;
706
+ }
707
+ while (c && pPage->GIFFile.iPos < pPage->GIFFile.iSize && pPage->iLZWSize < (LZW_BUF_SIZE-MAX_CHUNK_SIZE))
708
+ {
709
+ (*pPage->pfnRead)(&pPage->GIFFile, &c, 1); // current length
710
+ (*pPage->pfnRead)(&pPage->GIFFile, &pPage->ucLZW[pPage->iLZWSize], c);
711
+ pPage->iLZWSize += c;
712
+ }
713
+ if (c == 0) // end of frame
714
+ pPage->bEndOfFrame = 1;
715
+ return (c != 0 && pPage->GIFFile.iPos < pPage->GIFFile.iSize); // more data available?
716
+ } /* GIFGetMoreData() */
717
+ #if 0
718
+ //
719
+ // Handle transparent pixels and disposal method
720
+ // Used only when a frame buffer is allocated
721
+ //
722
+ static void DrawNewPixels(GIFIMAGE *pPage, GIFDRAW *pDraw)
723
+ {
724
+ uint8_t *d, *s;
725
+ int x, iPitch = pPage->iCanvasWidth;
726
+
727
+ s = pDraw->pPixels;
728
+ d = &pPage->pFrameBuffer[pDraw->iX + (pDraw->y + pDraw->iY) * iPitch]; // dest pointer in our complete canvas buffer
729
+ if (pDraw->ucDisposalMethod == 2) // restore to background color
730
+ {
731
+ memset(d, pDraw->ucBackground, pDraw->iWidth);
732
+ }
733
+ // Apply the new pixels to the main image
734
+ if (pDraw->ucHasTransparency) // if transparency used
735
+ {
736
+ uint8_t c, ucTransparent = pDraw->ucTransparent;
737
+ for (x=0; x<pDraw->iWidth; x++)
738
+ {
739
+ c = *s++;
740
+ if (c != ucTransparent)
741
+ *d = c;
742
+ d++;
743
+ }
744
+ }
745
+ else
746
+ {
747
+ c_memcpy(d, s, pDraw->iWidth); // just overwrite the old pixels
748
+ }
749
+ } /* DrawNewPixels() */
750
+ //
751
+ // Convert current line of pixels through the palette
752
+ // to either RGB565 or RGB888 output
753
+ // Used only when a frame buffer has been allocated
754
+ //
755
+ static void ConvertNewPixels(GIFIMAGE *pPage, GIFDRAW *pDraw)
756
+ {
757
+ uint8_t *d, *s;
758
+ int x;
759
+
760
+ s = &pPage->pFrameBuffer[(pPage->iCanvasWidth * (pDraw->iY + pDraw->y)) + pDraw->iX];
761
+ d = &pPage->pFrameBuffer[pPage->iCanvasHeight * pPage->iCanvasWidth]; // point past bottom of frame buffer
762
+ if (pPage->ucPaletteType == GIF_PALETTE_RGB565)
763
+ {
764
+ uint16_t *pPal, *pu16;
765
+ pPal = (uint16_t *)pDraw->pPalette;
766
+ pu16 = (uint16_t *)d;
767
+ for (x=0; x<pPage->iWidth; x++)
768
+ {
769
+ *pu16++ = pPal[*s++]; // convert to RGB565 pixels
770
+ }
771
+ }
772
+ else
773
+ {
774
+ uint8_t *pPal;
775
+ int pixel;
776
+ pPal = (uint8_t *)pDraw->pPalette;
777
+ for (x=0; x<pPage->iWidth; x++)
778
+ {
779
+ pixel = *s++;
780
+ *d++ = pPal[(pixel * 3) + 0]; // convert to RGB888 pixels
781
+ *d++ = pPal[(pixel * 3) + 1];
782
+ *d++ = pPal[(pixel * 3) + 2];
783
+ }
784
+ }
785
+ } /* ConvertNewPixels() */
786
+ #endif
787
+
788
+ //
789
+ // GIFMakePels
790
+ //
791
+ static void GIFMakePels(GIFIMAGE *pPage, unsigned int code)
792
+ {
793
+ int iPixCount;
794
+ unsigned short *giftabs;
795
+ unsigned char *buf, *s, *pEnd, *gifpels;
796
+ unsigned char ucNeedMore = 0;
797
+ /* Copy this string of sequential pixels to output buffer */
798
+ // iPixCount = 0;
799
+ s = pPage->ucFileBuf + FILE_BUF_SIZE; /* Pixels will come out in reversed order */
800
+ buf = pPage->ucLineBuf + (pPage->iWidth - pPage->iXCount);
801
+ giftabs = pPage->usGIFTable;
802
+ gifpels = &pPage->ucGIFPixels[PIXEL_LAST];
803
+ while (code < LINK_UNUSED)
804
+ {
805
+ if (s == pPage->ucFileBuf) /* Houston, we have a problem */
806
+ {
807
+ return; /* Exit with error */
808
+ }
809
+ *(--s) = gifpels[code];
810
+ code = giftabs[code];
811
+ }
812
+ iPixCount = (int)(intptr_t)(pPage->ucFileBuf + FILE_BUF_SIZE - s);
813
+
814
+ while (iPixCount && pPage->iYCount > 0)
815
+ {
816
+ if (pPage->iXCount > iPixCount) /* Pixels fit completely on the line */
817
+ {
818
+ // memcpy(buf, s, iPixCount);
819
+ // buf += iPixCount;
820
+ pEnd = buf + iPixCount;
821
+ while (buf < pEnd)
822
+ {
823
+ *buf++ = *s++;
824
+ }
825
+ pPage->iXCount -= iPixCount;
826
+ // iPixCount = 0;
827
+ if (ucNeedMore)
828
+ GIFGetMoreData(pPage); // check if we need to read more LZW data every 4 lines
829
+ return;
830
+ }
831
+ else /* Pixels cross into next line */
832
+ {
833
+ GIFDRAW gd;
834
+ pEnd = buf + pPage->iXCount;
835
+ while (buf < pEnd)
836
+ {
837
+ *buf++ = *s++;
838
+ }
839
+ iPixCount -= pPage->iXCount;
840
+ pPage->iXCount = pPage->iWidth; /* Reset pixel count */
841
+ // Prepare GIDRAW structure for callback
842
+ gd.iX = pPage->iX;
843
+ gd.iY = pPage->iY;
844
+ gd.iWidth = pPage->iWidth;
845
+ gd.iHeight = pPage->iHeight;
846
+ gd.pPixels = pPage->ucLineBuf;
847
+ gd.pPalette = (pPage->bUseLocalPalette) ? pPage->pLocalPalette : pPage->pPalette;
848
+ // gd.pPalette24 = (uint8_t *)gd.pPalette; // just cast the pointer for RGB888
849
+ gd.y = pPage->iHeight - pPage->iYCount;
850
+ // Ugly logic to handle the interlaced line position, but it
851
+ // saves having to have another set of state variables
852
+ if (pPage->ucMap & 0x40) { // interlaced?
853
+ int height = pPage->iHeight-1;
854
+ if (gd.y > height / 2)
855
+ gd.y = gd.y * 2 - (height | 1);
856
+ else if (gd.y > height / 4)
857
+ gd.y = gd.y * 4 - ((height & ~1) | 2);
858
+ else if (gd.y > height / 8)
859
+ gd.y = gd.y * 8 - ((height & ~3) | 4);
860
+ else
861
+ gd.y = gd.y * 8;
862
+ }
863
+ gd.ucDisposalMethod = (pPage->ucGIFBits & 0x1c)>>2;
864
+ gd.ucTransparent = pPage->ucTransparent;
865
+ gd.ucHasTransparency = pPage->ucGIFBits & 1;
866
+ gd.ucBackground = pPage->ucBackground;
867
+ // if (pPage->pFrameBuffer) // update the frame buffer
868
+ // {
869
+ // DrawNewPixels(pPage, &gd);
870
+ // if (pPage->ucDrawType == GIF_DRAW_COOKED)
871
+ // {
872
+ // ConvertNewPixels(pPage, &gd); // prepare for output
873
+ // gd.pPixels = &pPage->pFrameBuffer[pPage->iCanvasWidth * pPage->iCanvasHeight];
874
+ // }
875
+ // }
876
+ (*pPage->pfnDraw)(&gd); // callback to handle this line
877
+ pPage->iYCount--;
878
+ buf = pPage->ucLineBuf;
879
+ if ((pPage->iYCount & 3) == 0) // since we support only small images...
880
+ ucNeedMore = 1;
881
+ }
882
+ } /* while */
883
+ if (ucNeedMore)
884
+ GIFGetMoreData(pPage); // check if we need to read more LZW data every 4 lines
885
+ return;
886
+ } /* GIFMakePels() */
887
+ //
888
+ // Macro to extract a variable length code
889
+ //
890
+ #define GET_CODE if (bitnum > (REGISTER_WIDTH - codesize)) { pImage->iLZWOff += (bitnum >> 3); \
891
+ bitnum &= 7; ulBits = INTELLONG(&p[pImage->iLZWOff]); } \
892
+ code = (unsigned short) (ulBits >> bitnum); /* Read a 32-bit chunk */ \
893
+ code &= sMask; bitnum += codesize;
894
+
895
+ //
896
+ // Decode LZW into an image
897
+ //
898
+ static int DecodeLZW(GIFIMAGE *pImage, int iOptions)
899
+ {
900
+ int i, bitnum;
901
+ unsigned short oldcode, codesize, nextcode, nextlim;
902
+ unsigned short *giftabs, cc, eoi;
903
+ signed short sMask;
904
+ unsigned char *gifpels, *p;
905
+ // int iStripSize;
906
+ //unsigned char **index;
907
+ uint32_t ulBits;
908
+ unsigned short code;
909
+ (void)iOptions; // not used for now
910
+ // if output can be used for string table, do it faster
911
+ // if (bGIF && (OutPage->cBitsperpixel == 8 && ((OutPage->iWidth & 3) == 0)))
912
+ // return PILFastLZW(InPage, OutPage, bGIF, iOptions);
913
+ p = pImage->ucLZW; // un-chunked LZW data
914
+ sMask = 0xffff << (pImage->ucCodeStart + 1);
915
+ sMask = 0xffff - sMask;
916
+ cc = (sMask >> 1) + 1; /* Clear code */
917
+ eoi = cc + 1;
918
+ giftabs = pImage->usGIFTable;
919
+ gifpels = pImage->ucGIFPixels;
920
+ pImage->iYCount = pImage->iHeight; // count down the lines
921
+ pImage->iXCount = pImage->iWidth;
922
+ bitnum = 0;
923
+ pImage->iLZWOff = 0; // Offset into compressed data
924
+ GIFGetMoreData(pImage); // Read some data to start
925
+
926
+ // Initialize code table
927
+ // this part only needs to be initialized once
928
+ for (i = 0; i < cc; i++)
929
+ {
930
+ gifpels[PIXEL_FIRST + i] = gifpels[PIXEL_LAST + i] = (unsigned short) i;
931
+ giftabs[i] = LINK_END;
932
+ }
933
+ init_codetable:
934
+ codesize = pImage->ucCodeStart + 1;
935
+ sMask = 0xffff << (pImage->ucCodeStart + 1);
936
+ sMask = 0xffff - sMask;
937
+ nextcode = cc + 2;
938
+ nextlim = (unsigned short) ((1 << codesize));
939
+ // This part of the table needs to be reset multiple times
940
+ memset(&giftabs[cc], LINK_UNUSED, (4096 - cc)*sizeof(short));
941
+ ulBits = INTELLONG(&p[pImage->iLZWOff]); // start by reading 4 bytes of LZW data
942
+ GET_CODE
943
+ if (code == cc) // we just reset the dictionary, so get another code
944
+ {
945
+ GET_CODE
946
+ }
947
+ oldcode = code;
948
+ GIFMakePels(pImage, code); // first code is output as the first pixel
949
+ // Main decode loop
950
+ while (code != eoi && pImage->iYCount > 0) // && y < pImage->iHeight+1) /* Loop through all lines of the image (or strip) */
951
+ {
952
+ GET_CODE
953
+ if (code == cc) /* Clear code?, and not first code */
954
+ goto init_codetable;
955
+ if (code != eoi)
956
+ {
957
+ if (nextcode < nextlim) // for deferred cc case, don't let it overwrite the last entry (fff)
958
+ {
959
+ giftabs[nextcode] = oldcode;
960
+ gifpels[PIXEL_FIRST + nextcode] = gifpels[PIXEL_FIRST + oldcode];
961
+ if (giftabs[code] == LINK_UNUSED) /* Old code */
962
+ gifpels[PIXEL_LAST + nextcode] = gifpels[PIXEL_FIRST + oldcode];
963
+ else
964
+ gifpels[PIXEL_LAST + nextcode] = gifpels[PIXEL_FIRST + code];
965
+ }
966
+ nextcode++;
967
+ if (nextcode >= nextlim && codesize < 12)
968
+ {
969
+ codesize++;
970
+ nextlim <<= 1;
971
+ sMask = (sMask << 1) | 1;
972
+ }
973
+ GIFMakePels(pImage, code);
974
+ oldcode = code;
975
+ }
976
+ } /* while not end of LZW code stream */
977
+ return 0;
978
+ //gif_forced_error:
979
+ // free(pImage->pPixels);
980
+ // pImage->pPixels = NULL;
981
+ // return -1;
982
+ } /* DecodeLZW() */