rdgif.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. * rdgif.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1997, Thomas G. Lane.
  6. * Modified 2019 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2021-2022, D. R. Commander.
  9. * For conditions of distribution and use, see the accompanying README.ijg
  10. * file.
  11. *
  12. * This file contains routines to read input images in GIF format.
  13. *
  14. * These routines may need modification for non-Unix environments or
  15. * specialized applications. As they stand, they assume input from
  16. * an ordinary stdio stream. They further assume that reading begins
  17. * at the start of the file; start_input may need work if the
  18. * user interface has already read some data (e.g., to determine that
  19. * the file is indeed GIF format).
  20. */
  21. /*
  22. * This code is loosely based on giftoppm from the PBMPLUS distribution
  23. * of Feb. 1991. That file contains the following copyright notice:
  24. * +-------------------------------------------------------------------+
  25. * | Copyright 1990, David Koblas. |
  26. * | Permission to use, copy, modify, and distribute this software |
  27. * | and its documentation for any purpose and without fee is hereby |
  28. * | granted, provided that the above copyright notice appear in all |
  29. * | copies and that both that copyright notice and this permission |
  30. * | notice appear in supporting documentation. This software is |
  31. * | provided "as is" without express or implied warranty. |
  32. * +-------------------------------------------------------------------+
  33. */
  34. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  35. #ifdef GIF_SUPPORTED
  36. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  37. typedef unsigned char U_CHAR;
  38. #define UCH(x) ((int)(x))
  39. #define ReadOK(file, buffer, len) \
  40. (fread(buffer, 1, len, file) == ((size_t)(len)))
  41. #define MAXCOLORMAPSIZE 256 /* max # of colors in a GIF colormap */
  42. #define NUMCOLORS 3 /* # of colors */
  43. #define CM_RED 0 /* color component numbers */
  44. #define CM_GREEN 1
  45. #define CM_BLUE 2
  46. #define MAX_LZW_BITS 12 /* maximum LZW code size */
  47. #define LZW_TABLE_SIZE (1 << MAX_LZW_BITS) /* # of possible LZW symbols */
  48. /* Macros for extracting header data --- note we assume chars may be signed */
  49. #define LM_to_uint(array, offset) \
  50. ((unsigned int)UCH(array[offset]) + \
  51. (((unsigned int)UCH(array[offset + 1])) << 8))
  52. #define BitSet(byte, bit) ((byte) & (bit))
  53. #define INTERLACE 0x40 /* mask for bit signifying interlaced image */
  54. #define COLORMAPFLAG 0x80 /* mask for bit signifying colormap presence */
  55. /*
  56. * LZW decompression tables look like this:
  57. * symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  58. * symbol_tail[K] = suffix byte of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  59. * Note that entries 0..end_code of the above tables are not used,
  60. * since those symbols represent raw bytes or special codes.
  61. *
  62. * The stack represents the not-yet-used expansion of the last LZW symbol.
  63. * In the worst case, a symbol could expand to as many bytes as there are
  64. * LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack.
  65. * (This is conservative since that number includes the raw-byte symbols.)
  66. */
  67. /* Private version of data source object */
  68. typedef struct {
  69. struct cjpeg_source_struct pub; /* public fields */
  70. j_compress_ptr cinfo; /* back link saves passing separate parm */
  71. JSAMPARRAY colormap; /* GIF colormap (converted to my format) */
  72. /* State for GetCode and LZWReadByte */
  73. U_CHAR code_buf[256 + 4]; /* current input data block */
  74. int last_byte; /* # of bytes in code_buf */
  75. int last_bit; /* # of bits in code_buf */
  76. int cur_bit; /* next bit index to read */
  77. boolean first_time; /* flags first call to GetCode */
  78. boolean out_of_blocks; /* TRUE if hit terminator data block */
  79. int input_code_size; /* codesize given in GIF file */
  80. int clear_code, end_code; /* values for Clear and End codes */
  81. int code_size; /* current actual code size */
  82. int limit_code; /* 2^code_size */
  83. int max_code; /* first unused code value */
  84. /* Private state for LZWReadByte */
  85. int oldcode; /* previous LZW symbol */
  86. int firstcode; /* first byte of oldcode's expansion */
  87. /* LZW symbol table and expansion stack */
  88. UINT16 *symbol_head; /* => table of prefix symbols */
  89. UINT8 *symbol_tail; /* => table of suffix bytes */
  90. UINT8 *symbol_stack; /* => stack for symbol expansions */
  91. UINT8 *sp; /* stack pointer */
  92. /* State for interlaced image processing */
  93. boolean is_interlaced; /* TRUE if have interlaced image */
  94. jvirt_sarray_ptr interlaced_image; /* full image in interlaced order */
  95. JDIMENSION cur_row_number; /* need to know actual row number */
  96. JDIMENSION pass2_offset; /* # of pixel rows in pass 1 */
  97. JDIMENSION pass3_offset; /* # of pixel rows in passes 1&2 */
  98. JDIMENSION pass4_offset; /* # of pixel rows in passes 1,2,3 */
  99. } gif_source_struct;
  100. typedef gif_source_struct *gif_source_ptr;
  101. /* Forward declarations */
  102. METHODDEF(JDIMENSION) get_pixel_rows(j_compress_ptr cinfo,
  103. cjpeg_source_ptr sinfo);
  104. METHODDEF(JDIMENSION) load_interlaced_image(j_compress_ptr cinfo,
  105. cjpeg_source_ptr sinfo);
  106. METHODDEF(JDIMENSION) get_interlaced_row(j_compress_ptr cinfo,
  107. cjpeg_source_ptr sinfo);
  108. LOCAL(int)
  109. ReadByte(gif_source_ptr sinfo)
  110. /* Read next byte from GIF file */
  111. {
  112. register FILE *infile = sinfo->pub.input_file;
  113. register int c;
  114. if ((c = getc(infile)) == EOF)
  115. ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
  116. return c;
  117. }
  118. LOCAL(int)
  119. GetDataBlock(gif_source_ptr sinfo, U_CHAR *buf)
  120. /* Read a GIF data block, which has a leading count byte */
  121. /* A zero-length block marks the end of a data block sequence */
  122. {
  123. int count;
  124. count = ReadByte(sinfo);
  125. if (count > 0) {
  126. if (!ReadOK(sinfo->pub.input_file, buf, count))
  127. ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
  128. }
  129. return count;
  130. }
  131. LOCAL(void)
  132. SkipDataBlocks(gif_source_ptr sinfo)
  133. /* Skip a series of data blocks, until a block terminator is found */
  134. {
  135. U_CHAR buf[256];
  136. while (GetDataBlock(sinfo, buf) > 0)
  137. /* skip */;
  138. }
  139. LOCAL(void)
  140. ReInitLZW(gif_source_ptr sinfo)
  141. /* (Re)initialize LZW state; shared code for startup and Clear processing */
  142. {
  143. sinfo->code_size = sinfo->input_code_size + 1;
  144. sinfo->limit_code = sinfo->clear_code << 1; /* 2^code_size */
  145. sinfo->max_code = sinfo->clear_code + 2; /* first unused code value */
  146. sinfo->sp = sinfo->symbol_stack; /* init stack to empty */
  147. }
  148. LOCAL(void)
  149. InitLZWCode(gif_source_ptr sinfo)
  150. /* Initialize for a series of LZWReadByte (and hence GetCode) calls */
  151. {
  152. /* GetCode initialization */
  153. sinfo->last_byte = 2; /* make safe to "recopy last two bytes" */
  154. sinfo->code_buf[0] = 0;
  155. sinfo->code_buf[1] = 0;
  156. sinfo->last_bit = 0; /* nothing in the buffer */
  157. sinfo->cur_bit = 0; /* force buffer load on first call */
  158. sinfo->first_time = TRUE;
  159. sinfo->out_of_blocks = FALSE;
  160. /* LZWReadByte initialization: */
  161. /* compute special code values (note that these do not change later) */
  162. sinfo->clear_code = 1 << sinfo->input_code_size;
  163. sinfo->end_code = sinfo->clear_code + 1;
  164. ReInitLZW(sinfo);
  165. }
  166. LOCAL(int)
  167. GetCode(gif_source_ptr sinfo)
  168. /* Fetch the next code_size bits from the GIF data */
  169. /* We assume code_size is less than 16 */
  170. {
  171. register int accum;
  172. int offs, count;
  173. while (sinfo->cur_bit + sinfo->code_size > sinfo->last_bit) {
  174. /* Time to reload the buffer */
  175. /* First time, share code with Clear case */
  176. if (sinfo->first_time) {
  177. sinfo->first_time = FALSE;
  178. return sinfo->clear_code;
  179. }
  180. if (sinfo->out_of_blocks) {
  181. WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
  182. return sinfo->end_code; /* fake something useful */
  183. }
  184. /* preserve last two bytes of what we have -- assume code_size <= 16 */
  185. sinfo->code_buf[0] = sinfo->code_buf[sinfo->last_byte-2];
  186. sinfo->code_buf[1] = sinfo->code_buf[sinfo->last_byte-1];
  187. /* Load more bytes; set flag if we reach the terminator block */
  188. if ((count = GetDataBlock(sinfo, &sinfo->code_buf[2])) == 0) {
  189. sinfo->out_of_blocks = TRUE;
  190. WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
  191. return sinfo->end_code; /* fake something useful */
  192. }
  193. /* Reset counters */
  194. sinfo->cur_bit = (sinfo->cur_bit - sinfo->last_bit) + 16;
  195. sinfo->last_byte = 2 + count;
  196. sinfo->last_bit = sinfo->last_byte * 8;
  197. }
  198. /* Form up next 24 bits in accum */
  199. offs = sinfo->cur_bit >> 3; /* byte containing cur_bit */
  200. accum = UCH(sinfo->code_buf[offs + 2]);
  201. accum <<= 8;
  202. accum |= UCH(sinfo->code_buf[offs + 1]);
  203. accum <<= 8;
  204. accum |= UCH(sinfo->code_buf[offs]);
  205. /* Right-align cur_bit in accum, then mask off desired number of bits */
  206. accum >>= (sinfo->cur_bit & 7);
  207. sinfo->cur_bit += sinfo->code_size;
  208. return accum & ((1 << sinfo->code_size) - 1);
  209. }
  210. LOCAL(int)
  211. LZWReadByte(gif_source_ptr sinfo)
  212. /* Read an LZW-compressed byte */
  213. {
  214. register int code; /* current working code */
  215. int incode; /* saves actual input code */
  216. /* If any codes are stacked from a previously read symbol, return them */
  217. if (sinfo->sp > sinfo->symbol_stack)
  218. return (int)(*(--sinfo->sp));
  219. /* Time to read a new symbol */
  220. code = GetCode(sinfo);
  221. if (code == sinfo->clear_code) {
  222. /* Reinit state, swallow any extra Clear codes, and */
  223. /* return next code, which is expected to be a raw byte. */
  224. ReInitLZW(sinfo);
  225. do {
  226. code = GetCode(sinfo);
  227. } while (code == sinfo->clear_code);
  228. if (code > sinfo->clear_code) { /* make sure it is a raw byte */
  229. WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
  230. code = 0; /* use something valid */
  231. }
  232. /* make firstcode, oldcode valid! */
  233. sinfo->firstcode = sinfo->oldcode = code;
  234. return code;
  235. }
  236. if (code == sinfo->end_code) {
  237. /* Skip the rest of the image, unless GetCode already read terminator */
  238. if (!sinfo->out_of_blocks) {
  239. SkipDataBlocks(sinfo);
  240. sinfo->out_of_blocks = TRUE;
  241. }
  242. /* Complain that there's not enough data */
  243. WARNMS(sinfo->cinfo, JWRN_GIF_ENDCODE);
  244. /* Pad data with 0's */
  245. return 0; /* fake something usable */
  246. }
  247. /* Got normal raw byte or LZW symbol */
  248. incode = code; /* save for a moment */
  249. if (code >= sinfo->max_code) { /* special case for not-yet-defined symbol */
  250. /* code == max_code is OK; anything bigger is bad data */
  251. if (code > sinfo->max_code) {
  252. WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
  253. incode = 0; /* prevent creation of loops in symbol table */
  254. }
  255. /* this symbol will be defined as oldcode/firstcode */
  256. *(sinfo->sp++) = (UINT8)sinfo->firstcode;
  257. code = sinfo->oldcode;
  258. }
  259. /* If it's a symbol, expand it into the stack */
  260. while (code >= sinfo->clear_code) {
  261. *(sinfo->sp++) = sinfo->symbol_tail[code]; /* tail is a byte value */
  262. code = sinfo->symbol_head[code]; /* head is another LZW symbol */
  263. }
  264. /* At this point code just represents a raw byte */
  265. sinfo->firstcode = code; /* save for possible future use */
  266. /* If there's room in table... */
  267. if ((code = sinfo->max_code) < LZW_TABLE_SIZE) {
  268. /* Define a new symbol = prev sym + head of this sym's expansion */
  269. sinfo->symbol_head[code] = (UINT16)sinfo->oldcode;
  270. sinfo->symbol_tail[code] = (UINT8)sinfo->firstcode;
  271. sinfo->max_code++;
  272. /* Is it time to increase code_size? */
  273. if (sinfo->max_code >= sinfo->limit_code &&
  274. sinfo->code_size < MAX_LZW_BITS) {
  275. sinfo->code_size++;
  276. sinfo->limit_code <<= 1; /* keep equal to 2^code_size */
  277. }
  278. }
  279. sinfo->oldcode = incode; /* save last input symbol for future use */
  280. return sinfo->firstcode; /* return first byte of symbol's expansion */
  281. }
  282. LOCAL(void)
  283. ReadColorMap(gif_source_ptr sinfo, int cmaplen, JSAMPARRAY cmap)
  284. /* Read a GIF colormap */
  285. {
  286. int i, gray = 1;
  287. for (i = 0; i < cmaplen; i++) {
  288. #if BITS_IN_JSAMPLE == 8
  289. #define UPSCALE(x) (x)
  290. #else
  291. #define UPSCALE(x) ((x) << (BITS_IN_JSAMPLE - 8))
  292. #endif
  293. cmap[CM_RED][i] = (JSAMPLE)UPSCALE(ReadByte(sinfo));
  294. cmap[CM_GREEN][i] = (JSAMPLE)UPSCALE(ReadByte(sinfo));
  295. cmap[CM_BLUE][i] = (JSAMPLE)UPSCALE(ReadByte(sinfo));
  296. if (cmap[CM_RED][i] != cmap[CM_GREEN][i] ||
  297. cmap[CM_GREEN][i] != cmap[CM_BLUE][i])
  298. gray = 0;
  299. }
  300. if (sinfo->cinfo->in_color_space == JCS_RGB && gray) {
  301. sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
  302. sinfo->cinfo->input_components = 1;
  303. }
  304. }
  305. LOCAL(void)
  306. DoExtension(gif_source_ptr sinfo)
  307. /* Process an extension block */
  308. /* Currently we ignore 'em all */
  309. {
  310. int extlabel;
  311. /* Read extension label byte */
  312. extlabel = ReadByte(sinfo);
  313. TRACEMS1(sinfo->cinfo, 1, JTRC_GIF_EXTENSION, extlabel);
  314. /* Skip the data block(s) associated with the extension */
  315. SkipDataBlocks(sinfo);
  316. }
  317. /*
  318. * Read the file header; return image size and component count.
  319. */
  320. METHODDEF(void)
  321. start_input_gif(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  322. {
  323. gif_source_ptr source = (gif_source_ptr)sinfo;
  324. U_CHAR hdrbuf[10]; /* workspace for reading control blocks */
  325. unsigned int width, height; /* image dimensions */
  326. int colormaplen, aspectRatio;
  327. int c;
  328. /* Read and verify GIF Header */
  329. if (!ReadOK(source->pub.input_file, hdrbuf, 6))
  330. ERREXIT(cinfo, JERR_GIF_NOT);
  331. if (hdrbuf[0] != 'G' || hdrbuf[1] != 'I' || hdrbuf[2] != 'F')
  332. ERREXIT(cinfo, JERR_GIF_NOT);
  333. /* Check for expected version numbers.
  334. * If unknown version, give warning and try to process anyway;
  335. * this is per recommendation in GIF89a standard.
  336. */
  337. if ((hdrbuf[3] != '8' || hdrbuf[4] != '7' || hdrbuf[5] != 'a') &&
  338. (hdrbuf[3] != '8' || hdrbuf[4] != '9' || hdrbuf[5] != 'a'))
  339. TRACEMS3(cinfo, 1, JTRC_GIF_BADVERSION, hdrbuf[3], hdrbuf[4], hdrbuf[5]);
  340. /* Read and decipher Logical Screen Descriptor */
  341. if (!ReadOK(source->pub.input_file, hdrbuf, 7))
  342. ERREXIT(cinfo, JERR_INPUT_EOF);
  343. width = LM_to_uint(hdrbuf, 0);
  344. height = LM_to_uint(hdrbuf, 2);
  345. if (width == 0 || height == 0)
  346. ERREXIT(cinfo, JERR_GIF_EMPTY);
  347. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  348. if (sinfo->max_pixels &&
  349. (unsigned long long)width * height > sinfo->max_pixels)
  350. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  351. #endif
  352. /* we ignore the color resolution, sort flag, and background color index */
  353. aspectRatio = UCH(hdrbuf[6]);
  354. if (aspectRatio != 0 && aspectRatio != 49)
  355. TRACEMS(cinfo, 1, JTRC_GIF_NONSQUARE);
  356. /* Allocate space to store the colormap */
  357. source->colormap = (*cinfo->mem->alloc_sarray)
  358. ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)MAXCOLORMAPSIZE,
  359. (JDIMENSION)NUMCOLORS);
  360. colormaplen = 0; /* indicate initialization */
  361. /* Read global colormap if header indicates it is present */
  362. if (BitSet(hdrbuf[4], COLORMAPFLAG)) {
  363. colormaplen = 2 << (hdrbuf[4] & 0x07);
  364. ReadColorMap(source, colormaplen, source->colormap);
  365. }
  366. /* Scan until we reach start of desired image.
  367. * We don't currently support skipping images, but could add it easily.
  368. */
  369. for (;;) {
  370. c = ReadByte(source);
  371. if (c == ';') /* GIF terminator?? */
  372. ERREXIT(cinfo, JERR_GIF_IMAGENOTFOUND);
  373. if (c == '!') { /* Extension */
  374. DoExtension(source);
  375. continue;
  376. }
  377. if (c != ',') { /* Not an image separator? */
  378. WARNMS1(cinfo, JWRN_GIF_CHAR, c);
  379. continue;
  380. }
  381. /* Read and decipher Local Image Descriptor */
  382. if (!ReadOK(source->pub.input_file, hdrbuf, 9))
  383. ERREXIT(cinfo, JERR_INPUT_EOF);
  384. /* we ignore top/left position info, also sort flag */
  385. width = LM_to_uint(hdrbuf, 4);
  386. height = LM_to_uint(hdrbuf, 6);
  387. if (width == 0 || height == 0)
  388. ERREXIT(cinfo, JERR_GIF_EMPTY);
  389. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  390. if (sinfo->max_pixels &&
  391. (unsigned long long)width * height > sinfo->max_pixels)
  392. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  393. #endif
  394. source->is_interlaced = (BitSet(hdrbuf[8], INTERLACE) != 0);
  395. /* Read local colormap if header indicates it is present */
  396. /* Note: if we wanted to support skipping images, */
  397. /* we'd need to skip rather than read colormap for ignored images */
  398. if (BitSet(hdrbuf[8], COLORMAPFLAG)) {
  399. colormaplen = 2 << (hdrbuf[8] & 0x07);
  400. ReadColorMap(source, colormaplen, source->colormap);
  401. }
  402. source->input_code_size = ReadByte(source); /* get min-code-size byte */
  403. if (source->input_code_size < 2 || source->input_code_size > 8)
  404. ERREXIT1(cinfo, JERR_GIF_CODESIZE, source->input_code_size);
  405. /* Reached desired image, so break out of loop */
  406. /* If we wanted to skip this image, */
  407. /* we'd call SkipDataBlocks and then continue the loop */
  408. break;
  409. }
  410. /* Prepare to read selected image: first initialize LZW decompressor */
  411. source->symbol_head = (UINT16 *)
  412. (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  413. LZW_TABLE_SIZE * sizeof(UINT16));
  414. source->symbol_tail = (UINT8 *)
  415. (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  416. LZW_TABLE_SIZE * sizeof(UINT8));
  417. source->symbol_stack = (UINT8 *)
  418. (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  419. LZW_TABLE_SIZE * sizeof(UINT8));
  420. InitLZWCode(source);
  421. /*
  422. * If image is interlaced, we read it into a full-size sample array,
  423. * decompressing as we go; then get_interlaced_row selects rows from the
  424. * sample array in the proper order.
  425. */
  426. if (source->is_interlaced) {
  427. /* We request the virtual array now, but can't access it until virtual
  428. * arrays have been allocated. Hence, the actual work of reading the
  429. * image is postponed until the first call to get_pixel_rows.
  430. */
  431. source->interlaced_image = (*cinfo->mem->request_virt_sarray)
  432. ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
  433. (JDIMENSION)width, (JDIMENSION)height, (JDIMENSION)1);
  434. if (cinfo->progress != NULL) {
  435. cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
  436. progress->total_extra_passes++; /* count file input as separate pass */
  437. }
  438. source->pub.get_pixel_rows = load_interlaced_image;
  439. } else {
  440. source->pub.get_pixel_rows = get_pixel_rows;
  441. }
  442. if (cinfo->in_color_space != JCS_GRAYSCALE) {
  443. cinfo->in_color_space = JCS_RGB;
  444. cinfo->input_components = NUMCOLORS;
  445. }
  446. /* Create compressor input buffer. */
  447. source->pub.buffer = (*cinfo->mem->alloc_sarray)
  448. ((j_common_ptr)cinfo, JPOOL_IMAGE,
  449. (JDIMENSION)width * cinfo->input_components, (JDIMENSION)1);
  450. source->pub.buffer_height = 1;
  451. /* Pad colormap for safety. */
  452. for (c = colormaplen; c < source->clear_code; c++) {
  453. source->colormap[CM_RED][c] =
  454. source->colormap[CM_GREEN][c] =
  455. source->colormap[CM_BLUE][c] = CENTERJSAMPLE;
  456. }
  457. /* Return info about the image. */
  458. cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
  459. cinfo->image_width = width;
  460. cinfo->image_height = height;
  461. TRACEMS3(cinfo, 1, JTRC_GIF, width, height, colormaplen);
  462. }
  463. /*
  464. * Read one row of pixels.
  465. * This version is used for noninterlaced GIF images:
  466. * we read directly from the GIF file.
  467. */
  468. METHODDEF(JDIMENSION)
  469. get_pixel_rows(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  470. {
  471. gif_source_ptr source = (gif_source_ptr)sinfo;
  472. register int c;
  473. register JSAMPROW ptr;
  474. register JDIMENSION col;
  475. register JSAMPARRAY colormap = source->colormap;
  476. ptr = source->pub.buffer[0];
  477. if (cinfo->in_color_space == JCS_GRAYSCALE) {
  478. for (col = cinfo->image_width; col > 0; col--) {
  479. c = LZWReadByte(source);
  480. *ptr++ = colormap[CM_RED][c];
  481. }
  482. } else {
  483. for (col = cinfo->image_width; col > 0; col--) {
  484. c = LZWReadByte(source);
  485. *ptr++ = colormap[CM_RED][c];
  486. *ptr++ = colormap[CM_GREEN][c];
  487. *ptr++ = colormap[CM_BLUE][c];
  488. }
  489. }
  490. return 1;
  491. }
  492. /*
  493. * Read one row of pixels.
  494. * This version is used for the first call on get_pixel_rows when
  495. * reading an interlaced GIF file: we read the whole image into memory.
  496. */
  497. METHODDEF(JDIMENSION)
  498. load_interlaced_image(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  499. {
  500. gif_source_ptr source = (gif_source_ptr)sinfo;
  501. register JSAMPROW sptr;
  502. register JDIMENSION col;
  503. JDIMENSION row;
  504. cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
  505. /* Read the interlaced image into the virtual array we've created. */
  506. for (row = 0; row < cinfo->image_height; row++) {
  507. if (progress != NULL) {
  508. progress->pub.pass_counter = (long)row;
  509. progress->pub.pass_limit = (long)cinfo->image_height;
  510. (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
  511. }
  512. sptr = *(*cinfo->mem->access_virt_sarray)
  513. ((j_common_ptr)cinfo, source->interlaced_image, row, (JDIMENSION)1,
  514. TRUE);
  515. for (col = cinfo->image_width; col > 0; col--) {
  516. *sptr++ = (JSAMPLE)LZWReadByte(source);
  517. }
  518. }
  519. if (progress != NULL)
  520. progress->completed_extra_passes++;
  521. /* Replace method pointer so subsequent calls don't come here. */
  522. source->pub.get_pixel_rows = get_interlaced_row;
  523. /* Initialize for get_interlaced_row, and perform first call on it. */
  524. source->cur_row_number = 0;
  525. source->pass2_offset = (cinfo->image_height + 7) / 8;
  526. source->pass3_offset = source->pass2_offset + (cinfo->image_height + 3) / 8;
  527. source->pass4_offset = source->pass3_offset + (cinfo->image_height + 1) / 4;
  528. return get_interlaced_row(cinfo, sinfo);
  529. }
  530. /*
  531. * Read one row of pixels.
  532. * This version is used for interlaced GIF images:
  533. * we read from the virtual array.
  534. */
  535. METHODDEF(JDIMENSION)
  536. get_interlaced_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  537. {
  538. gif_source_ptr source = (gif_source_ptr)sinfo;
  539. register int c;
  540. register JSAMPROW sptr, ptr;
  541. register JDIMENSION col;
  542. register JSAMPARRAY colormap = source->colormap;
  543. JDIMENSION irow;
  544. /* Figure out which row of interlaced image is needed, and access it. */
  545. switch ((int)(source->cur_row_number & 7)) {
  546. case 0: /* first-pass row */
  547. irow = source->cur_row_number >> 3;
  548. break;
  549. case 4: /* second-pass row */
  550. irow = (source->cur_row_number >> 3) + source->pass2_offset;
  551. break;
  552. case 2: /* third-pass row */
  553. case 6:
  554. irow = (source->cur_row_number >> 2) + source->pass3_offset;
  555. break;
  556. default: /* fourth-pass row */
  557. irow = (source->cur_row_number >> 1) + source->pass4_offset;
  558. }
  559. sptr = *(*cinfo->mem->access_virt_sarray)
  560. ((j_common_ptr)cinfo, source->interlaced_image, irow, (JDIMENSION)1,
  561. FALSE);
  562. /* Scan the row, expand colormap, and output */
  563. ptr = source->pub.buffer[0];
  564. if (cinfo->in_color_space == JCS_GRAYSCALE) {
  565. for (col = cinfo->image_width; col > 0; col--) {
  566. c = *sptr++;
  567. *ptr++ = colormap[CM_RED][c];
  568. }
  569. } else {
  570. for (col = cinfo->image_width; col > 0; col--) {
  571. c = *sptr++;
  572. *ptr++ = colormap[CM_RED][c];
  573. *ptr++ = colormap[CM_GREEN][c];
  574. *ptr++ = colormap[CM_BLUE][c];
  575. }
  576. }
  577. source->cur_row_number++; /* for next time */
  578. return 1;
  579. }
  580. /*
  581. * Finish up at the end of the file.
  582. */
  583. METHODDEF(void)
  584. finish_input_gif(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  585. {
  586. /* no work */
  587. }
  588. /*
  589. * The module selection routine for GIF format input.
  590. */
  591. GLOBAL(cjpeg_source_ptr)
  592. jinit_read_gif(j_compress_ptr cinfo)
  593. {
  594. gif_source_ptr source;
  595. /* Create module interface object */
  596. source = (gif_source_ptr)
  597. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  598. sizeof(gif_source_struct));
  599. source->cinfo = cinfo; /* make back link for subroutines */
  600. /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  601. source->pub.start_input = start_input_gif;
  602. source->pub.finish_input = finish_input_gif;
  603. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  604. source->pub.max_pixels = 0;
  605. #endif
  606. return (cjpeg_source_ptr)source;
  607. }
  608. #endif /* GIF_SUPPORTED */