libnsgif.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * Copyright 2003 James Bursa <[email protected]>
  3. * Copyright 2004 John Tytgat <[email protected]>
  4. * Copyright 2004 Richard Wilson <[email protected]>
  5. * Copyright 2008 Sean Fox <[email protected]>
  6. *
  7. * This file is part of NetSurf's libnsgif, http://www.netsurf-browser.org/
  8. * Licenced under the MIT License,
  9. * http://www.opensource.org/licenses/mit-license.php
  10. */
  11. //#include <stdbool.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include "libnsgif.h"
  18. #ifdef NDEBUG
  19. # define LOG(x) ((void) 0)
  20. #else
  21. # define LOG(x) do { fprintf(stderr, x), fputc('\n', stderr); } while (0)
  22. #endif /* NDEBUG */
  23. /* READING GIF FILES
  24. =================
  25. The functions provided by this file allow for efficient progressive GIF
  26. decoding. Whilst the initialisation does not ensure that there is
  27. sufficient image data to complete the entire frame, it does ensure that
  28. the information provided is valid. Any subsequent attempts to decode an
  29. initialised GIF are guaranteed to succeed, and any bytes of the image
  30. not present are assumed to be totally transparent.
  31. To begin decoding a GIF, the 'gif' structure must be initialised with
  32. the 'gif_data' and 'buffer_size' set to their initial values. The
  33. 'buffer_position' should initially be 0, and will be internally updated
  34. as the decoding commences. The caller should then repeatedly call
  35. gif_initialise() with the structure until the function returns 1, or
  36. no more data is available.
  37. Once the initialisation has begun, the decoder completes the variables
  38. 'frame_count' and 'frame_count_partial'. The former being the total
  39. number of frames that have been successfully initialised, and the
  40. latter being the number of frames that a partial amount of data is
  41. available for. This assists the caller in managing the animation whilst
  42. decoding is continuing.
  43. To decode a frame, the caller must use gif_decode_frame() which updates
  44. the current 'frame_image' to reflect the desired frame. The required
  45. 'disposal_method' is also updated to reflect how the frame should be
  46. plotted. The caller must not assume that the current 'frame_image' will
  47. be valid between calls if initialisation is still occurring, and should
  48. either always request that the frame is decoded (no processing will
  49. occur if the 'decoded_frame' has not been invalidated by initialisation)
  50. or perform the check itself.
  51. It should be noted that gif_finalise() should always be called, even if
  52. no frames were initialised. Additionally, it is the responsibility of
  53. the caller to free 'gif_data'.
  54. [rjw] - Fri 2nd April 2004
  55. */
  56. /* TO-DO LIST
  57. =================
  58. + Plain text and comment extensions could be implemented if there is any
  59. interest in doing so.
  60. */
  61. /* Maximum colour table size
  62. */
  63. #define GIF_MAX_COLOURS 256
  64. /* Internal flag that the colour table needs to be processed
  65. */
  66. #define GIF_PROCESS_COLOURS 0xaa000000
  67. /* Internal flag that a frame is invalid/unprocessed
  68. */
  69. #define GIF_INVALID_FRAME -1
  70. /* Transparent colour
  71. */
  72. #define GIF_TRANSPARENT_COLOUR 0x00
  73. /* GIF Flags
  74. */
  75. #define GIF_FRAME_COMBINE 1
  76. #define GIF_FRAME_CLEAR 2
  77. #define GIF_FRAME_RESTORE 3
  78. #define GIF_FRAME_QUIRKS_RESTORE 4
  79. #define GIF_IMAGE_SEPARATOR 0x2c
  80. #define GIF_INTERLACE_MASK 0x40
  81. #define GIF_COLOUR_TABLE_MASK 0x80
  82. #define GIF_COLOUR_TABLE_SIZE_MASK 0x07
  83. #define GIF_EXTENSION_INTRODUCER 0x21
  84. #define GIF_EXTENSION_GRAPHIC_CONTROL 0xf9
  85. #define GIF_DISPOSAL_MASK 0x1c
  86. #define GIF_TRANSPARENCY_MASK 0x01
  87. #define GIF_EXTENSION_COMMENT 0xfe
  88. #define GIF_EXTENSION_PLAIN_TEXT 0x01
  89. #define GIF_EXTENSION_APPLICATION 0xff
  90. #define GIF_BLOCK_TERMINATOR 0x00
  91. #define GIF_TRAILER 0x3b
  92. /* Internal GIF routines
  93. */
  94. static gif_result gif_initialise_sprite(gif_animation *gif, unsigned int width, unsigned int height);
  95. static gif_result gif_initialise_frame(gif_animation *gif);
  96. static gif_result gif_initialise_frame_extensions(gif_animation *gif, const int frame);
  97. static gif_result gif_skip_frame_extensions(gif_animation *gif);
  98. static unsigned int gif_interlaced_line(int height, int y);
  99. /* Internal LZW routines
  100. */
  101. static void gif_init_LZW(gif_animation *gif);
  102. static bool gif_next_LZW(gif_animation *gif);
  103. static int gif_next_code(gif_animation *gif, int code_size);
  104. static const int maskTbl[16] = {0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f,
  105. 0x00ff, 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff};
  106. /** Initialises necessary gif_animation members.
  107. */
  108. void gif_create(gif_animation *gif, gif_bitmap_callback_vt *bitmap_callbacks) {
  109. memset(gif, 0, sizeof(gif_animation));
  110. gif->bitmap_callbacks = *bitmap_callbacks;
  111. gif->decoded_frame = GIF_INVALID_FRAME;
  112. }
  113. /** Initialises any workspace held by the animation and attempts to decode
  114. any information that hasn't already been decoded.
  115. If an error occurs, all previously decoded frames are retained.
  116. @return GIF_FRAME_DATA_ERROR for GIF frame data error
  117. GIF_INSUFFICIENT_FRAME_DATA for insufficient data to process
  118. any more frames
  119. GIF_INSUFFICIENT_MEMORY for memory error
  120. GIF_DATA_ERROR for GIF error
  121. GIF_INSUFFICIENT_DATA for insufficient data to do anything
  122. GIF_OK for successful decoding
  123. GIF_WORKING for successful decoding if more frames are expected
  124. */
  125. gif_result gif_initialise(gif_animation *gif, size_t size, unsigned char *data) {
  126. unsigned char *gif_data;
  127. unsigned int index;
  128. gif_result return_value;
  129. /* The GIF format is thoroughly documented; a full description
  130. * can be found at http://www.w3.org/Graphics/GIF/spec-gif89a.txt
  131. */
  132. /* Initialize values
  133. */
  134. gif->buffer_size = (unsigned int)size;
  135. gif->gif_data = data;
  136. /* Check for sufficient data to be a GIF (6-byte header + 7-byte logical screen descriptor)
  137. */
  138. if (gif->buffer_size < 13) return GIF_INSUFFICIENT_DATA;
  139. /* Get our current processing position
  140. */
  141. gif_data = gif->gif_data + gif->buffer_position;
  142. /* See if we should initialise the GIF
  143. */
  144. if (gif->buffer_position == 0) {
  145. /* We want everything to be NULL before we start so we've no chance
  146. of freeing bad pointers (paranoia)
  147. */
  148. gif->frame_image = NULL;
  149. gif->frames = NULL;
  150. gif->local_colour_table = NULL;
  151. gif->global_colour_table = NULL;
  152. /* The caller may have been lazy and not reset any values
  153. */
  154. gif->frame_count = 0;
  155. gif->frame_count_partial = 0;
  156. gif->decoded_frame = GIF_INVALID_FRAME;
  157. /* 6-byte GIF file header is:
  158. *
  159. * +0 3CHARS Signature ('GIF')
  160. * +3 3CHARS Version ('87a' or '89a')
  161. */
  162. if (strncmp((const char *) gif_data, "GIF", 3) != 0)
  163. return GIF_DATA_ERROR;
  164. gif_data += 3;
  165. /* Ensure GIF reports version 87a or 89a
  166. */
  167. /* if ((strncmp(gif_data, "87a", 3) != 0) &&
  168. (strncmp(gif_data, "89a", 3) != 0))
  169. LOG(("Unknown GIF format - proceeding anyway"));
  170. */ gif_data += 3;
  171. /* 7-byte Logical Screen Descriptor is:
  172. *
  173. * +0 SHORT Logical Screen Width
  174. * +2 SHORT Logical Screen Height
  175. * +4 CHAR __Packed Fields__
  176. * 1BIT Global Colour Table Flag
  177. * 3BITS Colour Resolution
  178. * 1BIT Sort Flag
  179. * 3BITS Size of Global Colour Table
  180. * +5 CHAR Background Colour Index
  181. * +6 CHAR Pixel Aspect Ratio
  182. */
  183. gif->width = gif_data[0] | (gif_data[1] << 8);
  184. gif->height = gif_data[2] | (gif_data[3] << 8);
  185. gif->global_colours = (gif_data[4] & GIF_COLOUR_TABLE_MASK);
  186. gif->colour_table_size = (2 << (gif_data[4] & GIF_COLOUR_TABLE_SIZE_MASK));
  187. gif->background_index = gif_data[5];
  188. gif->aspect_ratio = gif_data[6];
  189. gif->loop_count = 1;
  190. gif_data += 7;
  191. /* Some broken GIFs report the size as the screen size they were created in. As
  192. such, we detect for the common cases and set the sizes as 0 if they are found
  193. which results in the GIF being the maximum size of the frames.
  194. */
  195. if (((gif->width == 640) && (gif->height == 480)) ||
  196. ((gif->width == 640) && (gif->height == 512)) ||
  197. ((gif->width == 800) && (gif->height == 600)) ||
  198. ((gif->width == 1024) && (gif->height == 768)) ||
  199. ((gif->width == 1280) && (gif->height == 1024)) ||
  200. ((gif->width == 1600) && (gif->height == 1200)) ||
  201. ((gif->width == 0) || (gif->height == 0)) ||
  202. ((gif->width > 2048) || (gif->height > 2048))) {
  203. gif->width = 1;
  204. gif->height = 1;
  205. }
  206. /* Allocate some data irrespective of whether we've got any colour tables. We
  207. always get the maximum size in case a GIF is lying to us. It's far better
  208. to give the wrong colours than to trample over some memory somewhere.
  209. */
  210. gif->global_colour_table = (unsigned int *)calloc(GIF_MAX_COLOURS, sizeof(int));
  211. gif->local_colour_table = (unsigned int *)calloc(GIF_MAX_COLOURS, sizeof(int));
  212. if ((gif->global_colour_table == NULL) || (gif->local_colour_table == NULL)) {
  213. gif_finalise(gif);
  214. return GIF_INSUFFICIENT_MEMORY;
  215. }
  216. /* Set the first colour to a value that will never occur in reality so we
  217. know if we've processed it
  218. */
  219. gif->global_colour_table[0] = GIF_PROCESS_COLOURS;
  220. /* Check if the GIF has no frame data (13-byte header + 1-byte termination block)
  221. * Although generally useless, the GIF specification does not expressly prohibit this
  222. */
  223. if (gif->buffer_size == 14) {
  224. if (gif_data[0] == GIF_TRAILER)
  225. return GIF_OK;
  226. else
  227. return GIF_INSUFFICIENT_DATA;
  228. }
  229. /* Initialise enough workspace for 4 frames initially
  230. */
  231. if ((gif->frames = (gif_frame *)malloc(sizeof(gif_frame))) == NULL) {
  232. gif_finalise(gif);
  233. return GIF_INSUFFICIENT_MEMORY;
  234. }
  235. gif->frame_holders = 1;
  236. /* Initialise the sprite header
  237. */
  238. assert(gif->bitmap_callbacks.bitmap_create);
  239. if ((gif->frame_image = gif->bitmap_callbacks.bitmap_create(gif->width, gif->height)) == NULL) {
  240. gif_finalise(gif);
  241. return GIF_INSUFFICIENT_MEMORY;
  242. }
  243. /* Remember we've done this now
  244. */
  245. gif->buffer_position = (unsigned int)(gif_data - gif->gif_data);
  246. }
  247. /* Do the colour map if we haven't already. As the top byte is always 0xff or 0x00
  248. depending on the transparency we know if it's been filled in.
  249. */
  250. if (gif->global_colour_table[0] == GIF_PROCESS_COLOURS) {
  251. /* Check for a global colour map signified by bit 7
  252. */
  253. if (gif->global_colours) {
  254. if (gif->buffer_size < (gif->colour_table_size * 3 + 12)) {
  255. return GIF_INSUFFICIENT_DATA;
  256. }
  257. for (index = 0; index < gif->colour_table_size; index++) {
  258. /* Gif colour map contents are r,g,b.
  259. *
  260. * We want to pack them bytewise into the
  261. * colour table, such that the red component
  262. * is in byte 0 and the alpha component is in
  263. * byte 3.
  264. */
  265. unsigned char *entry = (unsigned char *) &gif->
  266. global_colour_table[index];
  267. entry[0] = gif_data[0]; /* r */
  268. entry[1] = gif_data[1]; /* g */
  269. entry[2] = gif_data[2]; /* b */
  270. entry[3] = 0xff; /* a */
  271. gif_data += 3;
  272. }
  273. gif->buffer_position = (unsigned int)(gif_data - gif->gif_data);
  274. } else {
  275. /* Create a default colour table with the first two colours as black and white
  276. */
  277. unsigned int *entry = gif->global_colour_table;
  278. entry[0] = 0x00000000;
  279. /* Force Alpha channel to opaque */
  280. ((unsigned char *) entry)[3] = 0xff;
  281. entry[1] = 0xffffffff;
  282. }
  283. }
  284. /* Repeatedly try to initialise frames
  285. */
  286. while ((return_value = gif_initialise_frame(gif)) == GIF_WORKING);
  287. /* If there was a memory error tell the caller
  288. */
  289. if ((return_value == GIF_INSUFFICIENT_MEMORY) ||
  290. (return_value == GIF_DATA_ERROR))
  291. return return_value;
  292. /* If we didn't have some frames then a GIF_INSUFFICIENT_DATA becomes a
  293. GIF_INSUFFICIENT_FRAME_DATA
  294. */
  295. if ((return_value == GIF_INSUFFICIENT_DATA) && (gif->frame_count_partial > 0))
  296. return GIF_INSUFFICIENT_FRAME_DATA;
  297. /* Return how many we got
  298. */
  299. return return_value;
  300. }
  301. /** Updates the sprite memory size
  302. @return GIF_INSUFFICIENT_MEMORY for a memory error
  303. GIF_OK for success
  304. */
  305. static gif_result gif_initialise_sprite(gif_animation *gif, unsigned int width, unsigned int height) {
  306. unsigned int max_width;
  307. unsigned int max_height;
  308. struct bitmap *buffer;
  309. /* Check if we've changed
  310. */
  311. if ((width <= gif->width) && (height <= gif->height))
  312. return GIF_OK;
  313. /* Get our maximum values
  314. */
  315. max_width = (width > gif->width) ? width : gif->width;
  316. max_height = (height > gif->height) ? height : gif->height;
  317. /* Allocate some more memory
  318. */
  319. assert(gif->bitmap_callbacks.bitmap_create);
  320. if ((buffer = gif->bitmap_callbacks.bitmap_create(max_width, max_height)) == NULL)
  321. return GIF_INSUFFICIENT_MEMORY;
  322. assert(gif->bitmap_callbacks.bitmap_destroy);
  323. gif->bitmap_callbacks.bitmap_destroy(gif->frame_image);
  324. gif->frame_image = buffer;
  325. gif->width = max_width;
  326. gif->height = max_height;
  327. /* Invalidate our currently decoded image
  328. */
  329. gif->decoded_frame = GIF_INVALID_FRAME;
  330. return GIF_OK;
  331. }
  332. /** Attempts to initialise the next frame
  333. @return GIF_INSUFFICIENT_DATA for insufficient data to do anything
  334. GIF_FRAME_DATA_ERROR for GIF frame data error
  335. GIF_INSUFFICIENT_MEMORY for insufficient memory to process
  336. GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame
  337. GIF_DATA_ERROR for GIF error (invalid frame header)
  338. GIF_OK for successful decoding
  339. GIF_WORKING for successful decoding if more frames are expected
  340. */
  341. static gif_result gif_initialise_frame(gif_animation *gif) {
  342. int frame;
  343. gif_frame *temp_buf;
  344. unsigned char *gif_data, *gif_end;
  345. int gif_bytes;
  346. unsigned int flags = 0;
  347. unsigned int width, height, offset_x, offset_y;
  348. unsigned int block_size, colour_table_size;
  349. bool first_image = true;
  350. gif_result return_value;
  351. /* Get the frame to decode and our data position
  352. */
  353. frame = gif->frame_count;
  354. /* Get our buffer position etc.
  355. */
  356. gif_data = (unsigned char *)(gif->gif_data + gif->buffer_position);
  357. gif_end = (unsigned char *)(gif->gif_data + gif->buffer_size);
  358. gif_bytes = (unsigned int)(gif_end - gif_data);
  359. /* Check if we've finished
  360. */
  361. if ((gif_bytes > 0) && (gif_data[0] == GIF_TRAILER)) return GIF_OK;
  362. /* Check if we have enough data
  363. * The shortest block of data is a 4-byte comment extension + 1-byte block terminator + 1-byte gif trailer
  364. */
  365. if (gif_bytes < 6) return GIF_INSUFFICIENT_DATA;
  366. /* We could theoretically get some junk data that gives us millions of frames, so
  367. we ensure that we don't have a silly number
  368. */
  369. if (frame > 4096) return GIF_FRAME_DATA_ERROR;
  370. /* Get some memory to store our pointers in etc.
  371. */
  372. if ((int)gif->frame_holders <= frame) {
  373. /* Allocate more memory
  374. */
  375. if ((temp_buf = (gif_frame *)realloc(gif->frames,
  376. (frame + 1) * sizeof(gif_frame))) == NULL)
  377. return GIF_INSUFFICIENT_MEMORY;
  378. gif->frames = temp_buf;
  379. gif->frame_holders = frame + 1;
  380. }
  381. /* Store our frame pointer. We would do it when allocating except we
  382. start off with one frame allocated so we can always use realloc.
  383. */
  384. gif->frames[frame].frame_pointer = gif->buffer_position;
  385. gif->frames[frame].display = false;
  386. gif->frames[frame].virgin = true;
  387. gif->frames[frame].disposal_method = 0;
  388. gif->frames[frame].transparency = false;
  389. gif->frames[frame].frame_delay = 100;
  390. gif->frames[frame].redraw_required = false;
  391. /* Invalidate any previous decoding we have of this frame
  392. */
  393. if (gif->decoded_frame == frame)
  394. gif->decoded_frame = GIF_INVALID_FRAME;
  395. /* We pretend to initialise the frames, but really we just skip over all
  396. the data contained within. This is all basically a cut down version of
  397. gif_decode_frame that doesn't have any of the LZW bits in it.
  398. */
  399. /* Initialise any extensions
  400. */
  401. gif->buffer_position = (unsigned int)(gif_data - gif->gif_data);
  402. if ((return_value = gif_initialise_frame_extensions(gif, frame)) != GIF_OK)
  403. return return_value;
  404. gif_data = (gif->gif_data + gif->buffer_position);
  405. gif_bytes = (unsigned int)(gif_end - gif_data);
  406. /* Check if we've finished
  407. */
  408. if ((gif_bytes = (unsigned int)(gif_end - gif_data)) < 1)
  409. return GIF_INSUFFICIENT_FRAME_DATA;
  410. else if (gif_data[0] == GIF_TRAILER) {
  411. gif->buffer_position = (unsigned int)(gif_data - gif->gif_data);
  412. gif->frame_count = frame + 1;
  413. return GIF_OK;
  414. }
  415. /* If we're not done, there should be an image descriptor
  416. */
  417. if (gif_data[0] != GIF_IMAGE_SEPARATOR) return GIF_FRAME_DATA_ERROR;
  418. /* Do some simple boundary checking
  419. */
  420. offset_x = gif_data[1] | (gif_data[2] << 8);
  421. offset_y = gif_data[3] | (gif_data[4] << 8);
  422. width = gif_data[5] | (gif_data[6] << 8);
  423. height = gif_data[7] | (gif_data[8] << 8);
  424. /* Set up the redraw characteristics. We have to check for extending the area
  425. due to multi-image frames.
  426. */
  427. if (!first_image) {
  428. if (gif->frames[frame].redraw_x > offset_x) {
  429. gif->frames[frame].redraw_width += (gif->frames[frame].redraw_x - offset_x);
  430. gif->frames[frame].redraw_x = offset_x;
  431. }
  432. if (gif->frames[frame].redraw_y > offset_y) {
  433. gif->frames[frame].redraw_height += (gif->frames[frame].redraw_y - offset_y);
  434. gif->frames[frame].redraw_y = offset_y;
  435. }
  436. if ((offset_x + width) > (gif->frames[frame].redraw_x + gif->frames[frame].redraw_width))
  437. gif->frames[frame].redraw_width = (offset_x + width) - gif->frames[frame].redraw_x;
  438. if ((offset_y + height) > (gif->frames[frame].redraw_y + gif->frames[frame].redraw_height))
  439. gif->frames[frame].redraw_height = (offset_y + height) - gif->frames[frame].redraw_y;
  440. } else {
  441. first_image = false;
  442. gif->frames[frame].redraw_x = offset_x;
  443. gif->frames[frame].redraw_y = offset_y;
  444. gif->frames[frame].redraw_width = width;
  445. gif->frames[frame].redraw_height = height;
  446. }
  447. /* if we are clearing the background then we need to redraw enough to cover the previous
  448. frame too
  449. */
  450. gif->frames[frame].redraw_required = ((gif->frames[frame].disposal_method == GIF_FRAME_CLEAR) ||
  451. (gif->frames[frame].disposal_method == GIF_FRAME_RESTORE));
  452. /* Boundary checking - shouldn't ever happen except with junk data
  453. */
  454. if (gif_initialise_sprite(gif, (offset_x + width), (offset_y + height)))
  455. return GIF_INSUFFICIENT_MEMORY;
  456. /* Decode the flags
  457. */
  458. flags = gif_data[9];
  459. colour_table_size = 2 << (flags & GIF_COLOUR_TABLE_SIZE_MASK);
  460. /* Move our data onwards and remember we've got a bit of this frame
  461. */
  462. gif_data += 10;
  463. gif_bytes = (unsigned int)(gif_end - gif_data);
  464. gif->frame_count_partial = frame + 1;
  465. /* Skip the local colour table
  466. */
  467. if (flags & GIF_COLOUR_TABLE_MASK) {
  468. gif_data += 3 * colour_table_size;
  469. if ((gif_bytes = (unsigned int)(gif_end - gif_data)) < 0)
  470. return GIF_INSUFFICIENT_FRAME_DATA;
  471. }
  472. /* Ensure we have a correct code size
  473. */
  474. if (gif_data[0] > GIF_MAX_LZW)
  475. return GIF_DATA_ERROR;
  476. /* Move our pointer to the actual image data
  477. */
  478. gif_data++;
  479. if (--gif_bytes < 0)
  480. return GIF_INSUFFICIENT_FRAME_DATA;
  481. /* Repeatedly skip blocks until we get a zero block or run out of data
  482. * These blocks of image data are processed later by gif_decode_frame()
  483. */
  484. block_size = 0;
  485. while (block_size != 1) {
  486. block_size = gif_data[0] + 1;
  487. /* Check if the frame data runs off the end of the file
  488. */
  489. if ((int)(gif_bytes - block_size) < 0) {
  490. /* Try to recover by signaling the end of the gif.
  491. * Once we get garbage data, there is no logical
  492. * way to determine where the next frame is.
  493. * It's probably better to partially load the gif
  494. * than not at all.
  495. */
  496. if (gif_bytes >= 2) {
  497. gif_data[0] = 0;
  498. gif_data[1] = GIF_TRAILER;
  499. gif_bytes = 1;
  500. ++gif_data;
  501. break;
  502. } else
  503. return GIF_INSUFFICIENT_FRAME_DATA;
  504. } else {
  505. gif_bytes -= block_size;
  506. gif_data += block_size;
  507. }
  508. }
  509. /* Add the frame and set the display flag
  510. */
  511. gif->buffer_position = (unsigned int)(gif_data - gif->gif_data);
  512. gif->frame_count = frame + 1;
  513. gif->frames[frame].display = true;
  514. /* Check if we've finished
  515. */
  516. if (gif_bytes < 1)
  517. return GIF_INSUFFICIENT_FRAME_DATA;
  518. else
  519. if (gif_data[0] == GIF_TRAILER) return GIF_OK;
  520. return GIF_WORKING;
  521. }
  522. /** Attempts to initialise the frame's extensions
  523. @return GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame
  524. GIF_OK for successful initialisation
  525. */
  526. static gif_result gif_initialise_frame_extensions(gif_animation *gif, const int frame) {
  527. unsigned char *gif_data, *gif_end;
  528. int gif_bytes;
  529. unsigned int block_size;
  530. /* Get our buffer position etc.
  531. */
  532. gif_data = (unsigned char *)(gif->gif_data + gif->buffer_position);
  533. gif_end = (unsigned char *)(gif->gif_data + gif->buffer_size);
  534. /* Initialise the extensions
  535. */
  536. while (gif_data[0] == GIF_EXTENSION_INTRODUCER) {
  537. ++gif_data;
  538. gif_bytes = (unsigned int)(gif_end - gif_data);
  539. /* Switch on extension label
  540. */
  541. switch(gif_data[0]) {
  542. /* 6-byte Graphic Control Extension is:
  543. *
  544. * +0 CHAR Graphic Control Label
  545. * +1 CHAR Block Size
  546. * +2 CHAR __Packed Fields__
  547. * 3BITS Reserved
  548. * 3BITS Disposal Method
  549. * 1BIT User Input Flag
  550. * 1BIT Transparent Color Flag
  551. * +3 SHORT Delay Time
  552. * +5 CHAR Transparent Color Index
  553. */
  554. case GIF_EXTENSION_GRAPHIC_CONTROL:
  555. if (gif_bytes < 6) return GIF_INSUFFICIENT_FRAME_DATA;
  556. gif->frames[frame].frame_delay = gif_data[3] | (gif_data[4] << 8);
  557. if (gif_data[2] & GIF_TRANSPARENCY_MASK) {
  558. gif->frames[frame].transparency = true;
  559. gif->frames[frame].transparency_index = gif_data[5];
  560. }
  561. gif->frames[frame].disposal_method = ((gif_data[2] & GIF_DISPOSAL_MASK) >> 2);
  562. /* I have encountered documentation and GIFs in the wild that use
  563. * 0x04 to restore the previous frame, rather than the officially
  564. * documented 0x03. I believe some (older?) software may even actually
  565. * export this way. We handle this as a type of "quirks" mode.
  566. */
  567. if (gif->frames[frame].disposal_method == GIF_FRAME_QUIRKS_RESTORE)
  568. gif->frames[frame].disposal_method = GIF_FRAME_RESTORE;
  569. gif_data += (2 + gif_data[1]);
  570. break;
  571. /* 14-byte+ Application Extension is:
  572. *
  573. * +0 CHAR Application Extension Label
  574. * +1 CHAR Block Size
  575. * +2 8CHARS Application Identifier
  576. * +10 3CHARS Appl. Authentication Code
  577. * +13 1-256 Application Data (Data sub-blocks)
  578. */
  579. case GIF_EXTENSION_APPLICATION:
  580. if (gif_bytes < 17) return GIF_INSUFFICIENT_FRAME_DATA;
  581. if ((gif_data[1] == 0x0b) &&
  582. (strncmp((const char *) gif_data + 2,
  583. "NETSCAPE2.0", 11) == 0) &&
  584. (gif_data[13] == 0x03) &&
  585. (gif_data[14] == 0x01)) {
  586. gif->loop_count = gif_data[15] | (gif_data[16] << 8);
  587. }
  588. gif_data += (2 + gif_data[1]);
  589. break;
  590. /* Move the pointer to the first data sub-block
  591. * Skip 1 byte for the extension label
  592. */
  593. case GIF_EXTENSION_COMMENT:
  594. ++gif_data;
  595. break;
  596. /* Move the pointer to the first data sub-block
  597. * Skip 2 bytes for the extension label and size fields
  598. * Skip the extension size itself
  599. */
  600. default:
  601. gif_data += (2 + gif_data[1]);
  602. }
  603. /* Repeatedly skip blocks until we get a zero block or run out of data
  604. * This data is ignored by this gif decoder
  605. */
  606. gif_bytes = (unsigned int)(gif_end - gif_data);
  607. block_size = 0;
  608. while (gif_data[0] != GIF_BLOCK_TERMINATOR) {
  609. block_size = gif_data[0] + 1;
  610. if ((gif_bytes -= block_size) < 0)
  611. return GIF_INSUFFICIENT_FRAME_DATA;
  612. gif_data += block_size;
  613. }
  614. ++gif_data;
  615. }
  616. /* Set buffer position and return
  617. */
  618. gif->buffer_position = (unsigned int)(gif_data - gif->gif_data);
  619. return GIF_OK;
  620. }
  621. /** Decodes a GIF frame.
  622. @return GIF_FRAME_DATA_ERROR for GIF frame data error
  623. GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame
  624. GIF_DATA_ERROR for GIF error (invalid frame header)
  625. GIF_INSUFFICIENT_DATA for insufficient data to do anything
  626. GIF_INSUFFICIENT_MEMORY for insufficient memory to process
  627. GIF_OK for successful decoding
  628. If a frame does not contain any image data, GIF_OK is returned and
  629. gif->current_error is set to GIF_FRAME_NO_DISPLAY
  630. */
  631. gif_result gif_decode_frame(gif_animation *gif, unsigned int frame) {
  632. unsigned int index = 0;
  633. unsigned char *gif_data, *gif_end;
  634. int gif_bytes;
  635. unsigned int width, height, offset_x, offset_y;
  636. unsigned int flags, colour_table_size, interlace;
  637. unsigned int *colour_table;
  638. unsigned int *frame_data = 0; // Set to 0 for no warnings
  639. unsigned int *frame_scanline;
  640. unsigned int save_buffer_position;
  641. gif_result return_value = GIF_OK;
  642. unsigned int x, y, decode_y, burst_bytes;
  643. int last_undisposed_frame = (frame - 1);
  644. register unsigned char colour;
  645. /* Ensure we have a frame to decode
  646. */
  647. if (frame >= gif->frame_count_partial)
  648. return GIF_INSUFFICIENT_DATA;
  649. /* Ensure this frame is supposed to be decoded
  650. */
  651. if (gif->frames[frame].display == false) {
  652. gif->current_error = GIF_FRAME_NO_DISPLAY;
  653. return GIF_OK;
  654. }
  655. if ((!gif->clear_image) && ((int)frame == gif->decoded_frame))
  656. return GIF_OK;
  657. /* Get the start of our frame data and the end of the GIF data
  658. */
  659. gif_data = gif->gif_data + gif->frames[frame].frame_pointer;
  660. gif_end = gif->gif_data + gif->buffer_size;
  661. gif_bytes = (unsigned int)(gif_end - gif_data);
  662. /* Check if we have enough data
  663. * The shortest block of data is a 10-byte image descriptor + 1-byte gif trailer
  664. */
  665. if (gif_bytes < 12) return GIF_INSUFFICIENT_FRAME_DATA;
  666. /* Save the buffer position
  667. */
  668. save_buffer_position = gif->buffer_position;
  669. gif->buffer_position = (unsigned int)(gif_data - gif->gif_data);
  670. /* Skip any extensions because we all ready processed them
  671. */
  672. if ((return_value = gif_skip_frame_extensions(gif)) != GIF_OK)
  673. goto gif_decode_frame_exit;
  674. gif_data = (gif->gif_data + gif->buffer_position);
  675. gif_bytes = (unsigned int)(gif_end - gif_data);
  676. /* Ensure we have enough data for the 10-byte image descriptor + 1-byte gif trailer
  677. */
  678. if (gif_bytes < 12) {
  679. return_value = GIF_INSUFFICIENT_FRAME_DATA;
  680. goto gif_decode_frame_exit;
  681. }
  682. /* 10-byte Image Descriptor is:
  683. *
  684. * +0 CHAR Image Separator (0x2c)
  685. * +1 SHORT Image Left Position
  686. * +3 SHORT Image Top Position
  687. * +5 SHORT Width
  688. * +7 SHORT Height
  689. * +9 CHAR __Packed Fields__
  690. * 1BIT Local Colour Table Flag
  691. * 1BIT Interlace Flag
  692. * 1BIT Sort Flag
  693. * 2BITS Reserved
  694. * 3BITS Size of Local Colour Table
  695. */
  696. if (gif_data[0] != GIF_IMAGE_SEPARATOR) {
  697. return_value = GIF_DATA_ERROR;
  698. goto gif_decode_frame_exit;
  699. }
  700. offset_x = gif_data[1] | (gif_data[2] << 8);
  701. offset_y = gif_data[3] | (gif_data[4] << 8);
  702. width = gif_data[5] | (gif_data[6] << 8);
  703. height = gif_data[7] | (gif_data[8] << 8);
  704. /* Boundary checking - shouldn't ever happen except unless the data has been
  705. modified since initialisation.
  706. */
  707. if ((offset_x + width > gif->width) || (offset_y + height > gif->height)) {
  708. return_value = GIF_DATA_ERROR;
  709. goto gif_decode_frame_exit;
  710. }
  711. /* Decode the flags
  712. */
  713. flags = gif_data[9];
  714. colour_table_size = 2 << (flags & GIF_COLOUR_TABLE_SIZE_MASK);
  715. interlace = flags & GIF_INTERLACE_MASK;
  716. /* Move our pointer to the colour table or image data (if no colour table is given)
  717. */
  718. gif_data += 10;
  719. gif_bytes = (unsigned int)(gif_end - gif_data);
  720. /* Set up the colour table
  721. */
  722. if (flags & GIF_COLOUR_TABLE_MASK) {
  723. if (gif_bytes < (int)(3 * colour_table_size)) {
  724. return_value = GIF_INSUFFICIENT_FRAME_DATA;
  725. goto gif_decode_frame_exit;
  726. }
  727. colour_table = gif->local_colour_table;
  728. if (!gif->clear_image) {
  729. for (index = 0; index < colour_table_size; index++) {
  730. /* Gif colour map contents are r,g,b.
  731. *
  732. * We want to pack them bytewise into the
  733. * colour table, such that the red component
  734. * is in byte 0 and the alpha component is in
  735. * byte 3.
  736. */
  737. unsigned char *entry =
  738. (unsigned char *) &colour_table[index];
  739. entry[0] = gif_data[0]; /* r */
  740. entry[1] = gif_data[1]; /* g */
  741. entry[2] = gif_data[2]; /* b */
  742. entry[3] = 0xff; /* a */
  743. gif_data += 3;
  744. }
  745. } else {
  746. gif_data += 3 * colour_table_size;
  747. }
  748. gif_bytes = (unsigned int)(gif_end - gif_data);
  749. } else {
  750. colour_table = gif->global_colour_table;
  751. }
  752. /* Check if we've finished
  753. */
  754. if (gif_bytes < 1) {
  755. return_value = GIF_INSUFFICIENT_FRAME_DATA;
  756. goto gif_decode_frame_exit;
  757. } else if (gif_data[0] == GIF_TRAILER) {
  758. return_value = GIF_OK;
  759. goto gif_decode_frame_exit;
  760. }
  761. /* Get the frame data
  762. */
  763. assert(gif->bitmap_callbacks.bitmap_get_buffer);
  764. frame_data = (void *)gif->bitmap_callbacks.bitmap_get_buffer(gif->frame_image);
  765. if (!frame_data)
  766. return GIF_INSUFFICIENT_MEMORY;
  767. /* If we are clearing the image we just clear, if not decode
  768. */
  769. if (!gif->clear_image) {
  770. /* Ensure we have enough data for a 1-byte LZW code size + 1-byte gif trailer
  771. */
  772. if (gif_bytes < 2) {
  773. return_value = GIF_INSUFFICIENT_FRAME_DATA;
  774. goto gif_decode_frame_exit;
  775. /* If we only have a 1-byte LZW code size + 1-byte gif trailer, we're finished
  776. */
  777. } else if ((gif_bytes == 2) && (gif_data[1] == GIF_TRAILER)) {
  778. return_value = GIF_OK;
  779. goto gif_decode_frame_exit;
  780. }
  781. /* If the previous frame's disposal method requires we restore the background
  782. * colour or this is the first frame, clear the frame data
  783. */
  784. if ((frame == 0) || (gif->decoded_frame == GIF_INVALID_FRAME)) {
  785. memset((char*)frame_data, GIF_TRANSPARENT_COLOUR, gif->width * gif->height * sizeof(int));
  786. gif->decoded_frame = frame;
  787. /* The line below would fill the image with its background color, but because GIFs support
  788. * transparency we likely wouldn't want to do that. */
  789. /* memset((char*)frame_data, colour_table[gif->background_index], gif->width * gif->height * sizeof(int)); */
  790. } else if ((frame != 0) && (gif->frames[frame - 1].disposal_method == GIF_FRAME_CLEAR)) {
  791. gif->clear_image = true;
  792. if ((return_value = gif_decode_frame(gif, (frame - 1))) != GIF_OK)
  793. goto gif_decode_frame_exit;
  794. gif->clear_image = false;
  795. /* If the previous frame's disposal method requires we restore the previous
  796. * image, find the last image set to "do not dispose" and get that frame data
  797. */
  798. } else if ((frame != 0) && (gif->frames[frame - 1].disposal_method == GIF_FRAME_RESTORE)) {
  799. while ((last_undisposed_frame != -1) && (gif->frames[last_undisposed_frame--].disposal_method == GIF_FRAME_RESTORE)) {
  800. }
  801. /* If we don't find one, clear the frame data
  802. */
  803. if (last_undisposed_frame == -1) {
  804. /* see notes above on transparency vs. background color */
  805. memset((char*)frame_data, GIF_TRANSPARENT_COLOUR, gif->width * gif->height * sizeof(int));
  806. } else {
  807. if ((return_value = gif_decode_frame(gif, last_undisposed_frame)) != GIF_OK)
  808. goto gif_decode_frame_exit;
  809. /* Get this frame's data
  810. */
  811. assert(gif->bitmap_callbacks.bitmap_get_buffer);
  812. frame_data = (void *)gif->bitmap_callbacks.bitmap_get_buffer(gif->frame_image);
  813. if (!frame_data)
  814. return GIF_INSUFFICIENT_MEMORY;
  815. }
  816. }
  817. gif->decoded_frame = frame;
  818. /* Initialise the LZW decoding
  819. */
  820. gif->set_code_size = gif_data[0];
  821. gif->buffer_position = (unsigned int)(gif_data - gif->gif_data + 1);
  822. /* Set our code variables
  823. */
  824. gif->code_size = gif->set_code_size + 1;
  825. gif->clear_code = (1 << gif->set_code_size);
  826. gif->end_code = gif->clear_code + 1;
  827. gif->max_code_size = gif->clear_code << 1;
  828. gif->max_code = gif->clear_code + 2;
  829. gif->curbit = gif->lastbit = 0;
  830. gif->last_byte = 2;
  831. gif->get_done = false;
  832. gif->direct = gif->buf;
  833. gif_init_LZW(gif);
  834. /* Decompress the data
  835. */
  836. for (y = 0; y < height; y++) {
  837. if (interlace)
  838. decode_y = gif_interlaced_line(height, y) + offset_y;
  839. else
  840. decode_y = y + offset_y;
  841. frame_scanline = frame_data + offset_x + (decode_y * gif->width);
  842. /* Rather than decoding pixel by pixel, we try to burst out streams
  843. of data to remove the need for end-of data checks every pixel.
  844. */
  845. x = width;
  846. while (x > 0) {
  847. burst_bytes = (unsigned int)(gif->stack_pointer - gif->stack);
  848. if (burst_bytes > 0) {
  849. if (burst_bytes > x)
  850. burst_bytes = x;
  851. x -= burst_bytes;
  852. while (burst_bytes-- > 0) {
  853. colour = *--gif->stack_pointer;
  854. if (((gif->frames[frame].transparency) &&
  855. (colour != gif->frames[frame].transparency_index)) ||
  856. (!gif->frames[frame].transparency))
  857. *frame_scanline = colour_table[colour];
  858. frame_scanline++;
  859. }
  860. } else {
  861. if (!gif_next_LZW(gif)) {
  862. /* Unexpected end of frame, try to recover
  863. */
  864. if (gif->current_error == GIF_END_OF_FRAME)
  865. return_value = GIF_OK;
  866. else
  867. return_value = gif->current_error;
  868. goto gif_decode_frame_exit;
  869. }
  870. }
  871. }
  872. }
  873. } else {
  874. /* Clear our frame
  875. */
  876. if (gif->frames[frame].disposal_method == GIF_FRAME_CLEAR) {
  877. for (y = 0; y < height; y++) {
  878. frame_scanline = frame_data + offset_x + ((offset_y + y) * gif->width);
  879. if (gif->frames[frame].transparency)
  880. memset(frame_scanline, GIF_TRANSPARENT_COLOUR, width * 4);
  881. else
  882. memset(frame_scanline, colour_table[gif->background_index], width * 4);
  883. }
  884. }
  885. }
  886. gif_decode_frame_exit:
  887. /* Check if we should test for optimisation
  888. */
  889. if (gif->frames[frame].virgin) {
  890. if (gif->bitmap_callbacks.bitmap_test_opaque)
  891. gif->frames[frame].opaque = gif->bitmap_callbacks.bitmap_test_opaque(gif->frame_image);
  892. else
  893. gif->frames[frame].opaque = false;
  894. gif->frames[frame].virgin = false;
  895. }
  896. if (gif->bitmap_callbacks.bitmap_set_opaque)
  897. gif->bitmap_callbacks.bitmap_set_opaque(gif->frame_image, gif->frames[frame].opaque);
  898. if (gif->bitmap_callbacks.bitmap_modified)
  899. gif->bitmap_callbacks.bitmap_modified(gif->frame_image);
  900. /* Restore the buffer position
  901. */
  902. gif->buffer_position = save_buffer_position;
  903. /* Success!
  904. */
  905. return return_value;
  906. }
  907. /** Skips the frame's extensions (which have been previously initialised)
  908. @return GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame
  909. GIF_OK for successful decoding
  910. */
  911. static gif_result gif_skip_frame_extensions(gif_animation *gif) {
  912. unsigned char *gif_data, *gif_end;
  913. int gif_bytes;
  914. unsigned int block_size;
  915. /* Get our buffer position etc.
  916. */
  917. gif_data = (unsigned char *)(gif->gif_data + gif->buffer_position);
  918. gif_end = (unsigned char *)(gif->gif_data + gif->buffer_size);
  919. gif_bytes = (unsigned int)(gif_end - gif_data);
  920. /* Skip the extensions
  921. */
  922. while (gif_data[0] == GIF_EXTENSION_INTRODUCER) {
  923. ++gif_data;
  924. /* Switch on extension label
  925. */
  926. switch(gif_data[0]) {
  927. /* Move the pointer to the first data sub-block
  928. * 1 byte for the extension label
  929. */
  930. case GIF_EXTENSION_COMMENT:
  931. ++gif_data;
  932. break;
  933. /* Move the pointer to the first data sub-block
  934. * 2 bytes for the extension label and size fields
  935. * Skip the extension size itself
  936. */
  937. default:
  938. gif_data += (2 + gif_data[1]);
  939. }
  940. /* Repeatedly skip blocks until we get a zero block or run out of data
  941. * This data is ignored by this gif decoder
  942. */
  943. gif_bytes = (unsigned int)(gif_end - gif_data);
  944. block_size = 0;
  945. while (gif_data[0] != GIF_BLOCK_TERMINATOR) {
  946. block_size = gif_data[0] + 1;
  947. if ((gif_bytes -= block_size) < 0)
  948. return GIF_INSUFFICIENT_FRAME_DATA;
  949. gif_data += block_size;
  950. }
  951. ++gif_data;
  952. }
  953. /* Set buffer position and return
  954. */
  955. gif->buffer_position = (unsigned int)(gif_data - gif->gif_data);
  956. return GIF_OK;
  957. }
  958. static unsigned int gif_interlaced_line(int height, int y) {
  959. if ((y << 3) < height) return (y << 3);
  960. y -= ((height + 7) >> 3);
  961. if ((y << 3) < (height - 4)) return (y << 3) + 4;
  962. y -= ((height + 3) >> 3);
  963. if ((y << 2) < (height - 2)) return (y << 2) + 2;
  964. y -= ((height + 1) >> 2);
  965. return (y << 1) + 1;
  966. }
  967. /* Releases any workspace held by the animation
  968. */
  969. void gif_finalise(gif_animation *gif) {
  970. /* Release all our memory blocks
  971. */
  972. if (gif->frame_image) {
  973. assert(gif->bitmap_callbacks.bitmap_destroy);
  974. gif->bitmap_callbacks.bitmap_destroy(gif->frame_image);
  975. }
  976. gif->frame_image = NULL;
  977. free(gif->frames);
  978. gif->frames = NULL;
  979. free(gif->local_colour_table);
  980. gif->local_colour_table = NULL;
  981. free(gif->global_colour_table);
  982. gif->global_colour_table = NULL;
  983. }
  984. /**
  985. * Initialise LZW decoding
  986. */
  987. void gif_init_LZW(gif_animation *gif) {
  988. int i;
  989. gif->current_error = 0;
  990. if (gif->clear_code >= (1 << GIF_MAX_LZW)) {
  991. gif->stack_pointer = gif->stack;
  992. gif->current_error = GIF_FRAME_DATA_ERROR;
  993. return;
  994. }
  995. /* initialise our table */
  996. memset(gif->table, 0x00, (1 << GIF_MAX_LZW) * 8);
  997. for (i = 0; i < gif->clear_code; ++i)
  998. gif->table[1][i] = i;
  999. /* update our LZW parameters */
  1000. gif->code_size = gif->set_code_size + 1;
  1001. gif->max_code_size = gif->clear_code << 1;
  1002. gif->max_code = gif->clear_code + 2;
  1003. gif->stack_pointer = gif->stack;
  1004. do {
  1005. gif->firstcode = gif->oldcode = gif_next_code(gif, gif->code_size);
  1006. } while (gif->firstcode == gif->clear_code);
  1007. *gif->stack_pointer++ =gif->firstcode;
  1008. }
  1009. static bool gif_next_LZW(gif_animation *gif) {
  1010. int code, incode;
  1011. int block_size;
  1012. int new_code;
  1013. code = gif_next_code(gif, gif->code_size);
  1014. if (code < 0) {
  1015. gif->current_error = code;
  1016. return false;
  1017. } else if (code == gif->clear_code) {
  1018. gif_init_LZW(gif);
  1019. return true;
  1020. } else if (code == gif->end_code) {
  1021. /* skip to the end of our data so multi-image GIFs work */
  1022. if (gif->zero_data_block) {
  1023. gif->current_error = GIF_FRAME_DATA_ERROR;
  1024. return false;
  1025. }
  1026. block_size = 0;
  1027. while (block_size != 1) {
  1028. block_size = gif->gif_data[gif->buffer_position] + 1;
  1029. gif->buffer_position += block_size;
  1030. }
  1031. gif->current_error = GIF_FRAME_DATA_ERROR;
  1032. return false;
  1033. }
  1034. incode = code;
  1035. if (code >= gif->max_code) {
  1036. *gif->stack_pointer++ = gif->firstcode;
  1037. code = gif->oldcode;
  1038. }
  1039. /* The following loop is the most important in the GIF decoding cycle as every
  1040. * single pixel passes through it.
  1041. *
  1042. * Note: our gif->stack is always big enough to hold a complete decompressed chunk. */
  1043. while (code >= gif->clear_code) {
  1044. *gif->stack_pointer++ = gif->table[1][code];
  1045. new_code = gif->table[0][code];
  1046. if (new_code < gif->clear_code) {
  1047. code = new_code;
  1048. break;
  1049. }
  1050. *gif->stack_pointer++ = gif->table[1][new_code];
  1051. code = gif->table[0][new_code];
  1052. if (code == new_code) {
  1053. gif->current_error = GIF_FRAME_DATA_ERROR;
  1054. return false;
  1055. }
  1056. }
  1057. *gif->stack_pointer++ = gif->firstcode = gif->table[1][code];
  1058. if ((code = gif->max_code) < (1 << GIF_MAX_LZW)) {
  1059. gif->table[0][code] = gif->oldcode;
  1060. gif->table[1][code] = gif->firstcode;
  1061. ++gif->max_code;
  1062. if ((gif->max_code >= gif->max_code_size) && (gif->max_code_size < (1 << GIF_MAX_LZW))) {
  1063. gif->max_code_size = gif->max_code_size << 1;
  1064. ++gif->code_size;
  1065. }
  1066. }
  1067. gif->oldcode = incode;
  1068. return true;
  1069. }
  1070. static int gif_next_code(gif_animation *gif, int code_size) {
  1071. int i, j, end, count, ret;
  1072. unsigned char *b;
  1073. (void)code_size;
  1074. end = gif->curbit + gif->code_size;
  1075. if (end >= gif->lastbit) {
  1076. if (gif->get_done)
  1077. return GIF_END_OF_FRAME;
  1078. gif->buf[0] = gif->direct[gif->last_byte - 2];
  1079. gif->buf[1] = gif->direct[gif->last_byte - 1];
  1080. /* get the next block */
  1081. gif->direct = gif->gif_data + gif->buffer_position;
  1082. gif->zero_data_block = ((count = gif->direct[0]) == 0);
  1083. if ((gif->buffer_position + count) >= gif->buffer_size)
  1084. return GIF_INSUFFICIENT_FRAME_DATA;
  1085. if (count == 0)
  1086. gif->get_done = true;
  1087. else {
  1088. gif->direct -= 1;
  1089. gif->buf[2] = gif->direct[2];
  1090. gif->buf[3] = gif->direct[3];
  1091. }
  1092. gif->buffer_position += count + 1;
  1093. /* update our variables */
  1094. gif->last_byte = 2 + count;
  1095. gif->curbit = (gif->curbit - gif->lastbit) + 16;
  1096. gif->lastbit = (2 + count) << 3;
  1097. end = gif->curbit + gif->code_size;
  1098. }
  1099. i = gif->curbit >> 3;
  1100. if (i < 2)
  1101. b = gif->buf;
  1102. else
  1103. b = gif->direct;
  1104. ret = b[i];
  1105. j = (end >> 3) - 1;
  1106. if (i <= j) {
  1107. ret |= (b[i + 1] << 8);
  1108. if (i < j)
  1109. ret |= (b[i + 2] << 16);
  1110. }
  1111. ret = (ret >> (gif->curbit % 8)) & maskTbl[gif->code_size];
  1112. gif->curbit += gif->code_size;
  1113. return ret;
  1114. }