rdgif.c 22 KB

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