home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume29 / jpeg / part11 < prev    next >
Text File  |  1992-04-03  |  55KB  |  1,771 lines

  1. Newsgroups: comp.sources.misc
  2. From: jpeg-info@uunet.uu.net (Independent JPEG Group)
  3. Subject:  REPOST: v29i011:  jpeg - JPEG image compression, Part11/18
  4. Message-ID: <1992Mar28.211945.29008@sparky.imd.sterling.com>
  5. X-Md4-Signature: 66459c7452d01d21bea1bd244039f9d4
  6. Date: Sat, 28 Mar 1992 21:19:45 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: jpeg-info@uunet.uu.net (Independent JPEG Group)
  10. Posting-number: Volume 29, Issue 11
  11. Archive-name: jpeg/part11
  12. Environment: UNIX, VMS, MS-DOS, Mac, Amiga, Cray
  13.  
  14. [ Reposted due to a propagation problem.  -Kent+ ]
  15.  
  16. #! /bin/sh
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  20. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  21. # Contents:  jchuff.c jcmain.c jrdgif.c makljpeg.cf
  22. # Wrapped by kent@sparky on Mon Mar 23 16:02:49 1992
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. echo If this archive is complete, you will see the following message:
  25. echo '          "shar: End of archive 11 (of 18)."'
  26. if test -f 'jchuff.c' -a "${1}" != "-c" ; then 
  27.   echo shar: Will not clobber existing file \"'jchuff.c'\"
  28. else
  29.   echo shar: Extracting \"'jchuff.c'\" \(20071 characters\)
  30.   sed "s/^X//" >'jchuff.c' <<'END_OF_FILE'
  31. X/*
  32. X * jchuff.c
  33. X *
  34. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  35. X * This file is part of the Independent JPEG Group's software.
  36. X * For conditions of distribution and use, see the accompanying README file.
  37. X *
  38. X * This file contains Huffman entropy encoding routines.
  39. X * These routines are invoked via the methods entropy_encode,
  40. X * entropy_encoder_init/term, and entropy_optimize.
  41. X */
  42. X
  43. X#include "jinclude.h"
  44. X
  45. X
  46. X/* Static variables to avoid passing 'round extra parameters */
  47. X
  48. Xstatic compress_info_ptr cinfo;
  49. X
  50. Xstatic INT32 huff_put_buffer;    /* current bit-accumulation buffer */
  51. Xstatic int huff_put_bits;    /* # of bits now in it */
  52. X
  53. Xstatic char * output_buffer;    /* output buffer */
  54. Xstatic int bytes_in_buffer;
  55. X
  56. X
  57. X
  58. XLOCAL void
  59. Xfix_huff_tbl (HUFF_TBL * htbl)
  60. X/* Compute derived values for a Huffman table */
  61. X{
  62. X  int p, i, l, lastp, si;
  63. X  char huffsize[257];
  64. X  UINT16 huffcode[257];
  65. X  UINT16 code;
  66. X  
  67. X  /* Figure C.1: make table of Huffman code length for each symbol */
  68. X  /* Note that this is in code-length order. */
  69. X
  70. X  p = 0;
  71. X  for (l = 1; l <= 16; l++) {
  72. X    for (i = 1; i <= (int) htbl->bits[l]; i++)
  73. X      huffsize[p++] = (char) l;
  74. X  }
  75. X  huffsize[p] = 0;
  76. X  lastp = p;
  77. X  
  78. X  /* Figure C.2: generate the codes themselves */
  79. X  /* Note that this is in code-length order. */
  80. X  
  81. X  code = 0;
  82. X  si = huffsize[0];
  83. X  p = 0;
  84. X  while (huffsize[p]) {
  85. X    while (((int) huffsize[p]) == si) {
  86. X      huffcode[p++] = code;
  87. X      code++;
  88. X    }
  89. X    code <<= 1;
  90. X    si++;
  91. X  }
  92. X  
  93. X  /* Figure C.3: generate encoding tables */
  94. X  /* These are code and size indexed by symbol value */
  95. X
  96. X  /* Set any codeless symbols to have code length 0;
  97. X   * this allows emit_bits to detect any attempt to emit such symbols.
  98. X   */
  99. X  MEMZERO((void *) htbl->ehufsi, SIZEOF(htbl->ehufsi));
  100. X
  101. X  for (p = 0; p < lastp; p++) {
  102. X    htbl->ehufco[htbl->huffval[p]] = huffcode[p];
  103. X    htbl->ehufsi[htbl->huffval[p]] = huffsize[p];
  104. X  }
  105. X  
  106. X  /* We don't bother to fill in the decoding tables mincode[], maxcode[], */
  107. X  /* and valptr[], since they are not used for encoding. */
  108. X}
  109. X
  110. X
  111. X/* Outputting bytes to the file */
  112. X
  113. XLOCAL void
  114. Xflush_bytes (void)
  115. X{
  116. X  if (bytes_in_buffer)
  117. X    (*cinfo->methods->entropy_output) (cinfo, output_buffer, bytes_in_buffer);
  118. X  bytes_in_buffer = 0;
  119. X}
  120. X
  121. X
  122. X#define emit_byte(val)  \
  123. X  MAKESTMT( if (bytes_in_buffer >= JPEG_BUF_SIZE) \
  124. X          flush_bytes(); \
  125. X        output_buffer[bytes_in_buffer++] = (char) (val); )
  126. X
  127. X
  128. X
  129. X/* Outputting bits to the file */
  130. X
  131. X/* Only the right 24 bits of huff_put_buffer are used; the valid bits are
  132. X * left-justified in this part.  At most 16 bits can be passed to emit_bits
  133. X * in one call, and we never retain more than 7 bits in huff_put_buffer
  134. X * between calls, so 24 bits are sufficient.
  135. X */
  136. X
  137. XLOCAL void
  138. Xemit_bits (UINT16 code, int size)
  139. X{
  140. X  /* This routine is heavily used, so it's worth coding tightly. */
  141. X  register INT32 put_buffer = code;
  142. X  register int put_bits = huff_put_bits;
  143. X
  144. X  /* if size is 0, caller used an invalid Huffman table entry */
  145. X  if (size == 0)
  146. X    ERREXIT(cinfo->emethods, "Missing Huffman code table entry");
  147. X
  148. X  put_buffer &= (((INT32) 1) << size) - 1; /* Mask off any excess bits in code */
  149. X  
  150. X  put_bits += size;        /* new number of bits in buffer */
  151. X  
  152. X  put_buffer <<= 24 - put_bits; /* align incoming bits */
  153. X
  154. X  put_buffer |= huff_put_buffer; /* and merge with old buffer contents */
  155. X  
  156. X  while (put_bits >= 8) {
  157. X    int c = (int) ((put_buffer >> 16) & 0xFF);
  158. X    
  159. X    emit_byte(c);
  160. X    if (c == 0xFF) {        /* need to stuff a zero byte? */
  161. X      emit_byte(0);
  162. X    }
  163. X    put_buffer <<= 8;
  164. X    put_bits -= 8;
  165. X  }
  166. X
  167. X  huff_put_buffer = put_buffer;    /* Update global variables */
  168. X  huff_put_bits = put_bits;
  169. X}
  170. X
  171. X
  172. XLOCAL void
  173. Xflush_bits (void)
  174. X{
  175. X  emit_bits((UINT16) 0x7F, 7);    /* fill any partial byte with ones */
  176. X  huff_put_buffer = 0;        /* and reset bit-buffer to empty */
  177. X  huff_put_bits = 0;
  178. X}
  179. X
  180. X
  181. X
  182. X/* Encode a single block's worth of coefficients */
  183. X/* Note that the DC coefficient has already been converted to a difference */
  184. X
  185. XLOCAL void
  186. Xencode_one_block (JBLOCK block, HUFF_TBL *dctbl, HUFF_TBL *actbl)
  187. X{
  188. X  register int temp, temp2;
  189. X  register int nbits;
  190. X  register int k, r, i;
  191. X  
  192. X  /* Encode the DC coefficient difference per section F.1.2.1 */
  193. X  
  194. X  temp = temp2 = block[0];
  195. X
  196. X  if (temp < 0) {
  197. X    temp = -temp;        /* temp is abs value of input */
  198. X    /* For a negative input, want temp2 = bitwise complement of abs(input) */
  199. X    /* This code assumes we are on a two's complement machine */
  200. X    temp2--;
  201. X  }
  202. X  
  203. X  /* Find the number of bits needed for the magnitude of the coefficient */
  204. X  nbits = 0;
  205. X  while (temp) {
  206. X    nbits++;
  207. X    temp >>= 1;
  208. X  }
  209. X  
  210. X  /* Emit the Huffman-coded symbol for the number of bits */
  211. X  emit_bits(dctbl->ehufco[nbits], dctbl->ehufsi[nbits]);
  212. X
  213. X  /* Emit that number of bits of the value, if positive, */
  214. X  /* or the complement of its magnitude, if negative. */
  215. X  if (nbits)            /* emit_bits rejects calls with size 0 */
  216. X    emit_bits((UINT16) temp2, nbits);
  217. X  
  218. X  /* Encode the AC coefficients per section F.1.2.2 */
  219. X  
  220. X  r = 0;            /* r = run length of zeros */
  221. X  
  222. X  for (k = 1; k < DCTSIZE2; k++) {
  223. X    if ((temp = block[k]) == 0) {
  224. X      r++;
  225. X    } else {
  226. X      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  227. X      while (r > 15) {
  228. X    emit_bits(actbl->ehufco[0xF0], actbl->ehufsi[0xF0]);
  229. X    r -= 16;
  230. X      }
  231. X
  232. X      temp2 = temp;
  233. X      if (temp < 0) {
  234. X    temp = -temp;        /* temp is abs value of input */
  235. X    /* This code assumes we are on a two's complement machine */
  236. X    temp2--;
  237. X      }
  238. X      
  239. X      /* Find the number of bits needed for the magnitude of the coefficient */
  240. X      nbits = 1;        /* there must be at least one 1 bit */
  241. X      while (temp >>= 1)
  242. X    nbits++;
  243. X      
  244. X      /* Emit Huffman symbol for run length / number of bits */
  245. X      i = (r << 4) + nbits;
  246. X      emit_bits(actbl->ehufco[i], actbl->ehufsi[i]);
  247. X      
  248. X      /* Emit that number of bits of the value, if positive, */
  249. X      /* or the complement of its magnitude, if negative. */
  250. X      emit_bits((UINT16) temp2, nbits);
  251. X      
  252. X      r = 0;
  253. X    }
  254. X  }
  255. X
  256. X  /* If the last coef(s) were zero, emit an end-of-block code */
  257. X  if (r > 0)
  258. X    emit_bits(actbl->ehufco[0], actbl->ehufsi[0]);
  259. X}
  260. X
  261. X
  262. X
  263. X/*
  264. X * Initialize for a Huffman-compressed scan.
  265. X * This is invoked after writing the SOS marker.
  266. X * The pipeline controller must establish the entropy_output method pointer
  267. X * before calling this routine.
  268. X */
  269. X
  270. XMETHODDEF void
  271. Xhuff_init (compress_info_ptr xinfo)
  272. X{
  273. X  short ci;
  274. X  jpeg_component_info * compptr;
  275. X
  276. X  /* Initialize static variables */
  277. X  cinfo = xinfo;
  278. X  huff_put_buffer = 0;
  279. X  huff_put_bits = 0;
  280. X
  281. X  /* Initialize the output buffer */
  282. X  output_buffer = (char *) (*cinfo->emethods->alloc_small)
  283. X                ((size_t) JPEG_BUF_SIZE);
  284. X  bytes_in_buffer = 0;
  285. X
  286. X  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  287. X    compptr = cinfo->cur_comp_info[ci];
  288. X    /* Make sure requested tables are present */
  289. X    if (cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no] == NULL ||
  290. X    cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no] == NULL)
  291. X      ERREXIT(cinfo->emethods, "Use of undefined Huffman table");
  292. X    /* Compute derived values for Huffman tables */
  293. X    /* We may do this more than once for same table, but it's not a big deal */
  294. X    fix_huff_tbl(cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no]);
  295. X    fix_huff_tbl(cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
  296. X    /* Initialize DC predictions to 0 */
  297. X    cinfo->last_dc_val[ci] = 0;
  298. X  }
  299. X
  300. X  /* Initialize restart stuff */
  301. X  cinfo->restarts_to_go = cinfo->restart_interval;
  302. X  cinfo->next_restart_num = 0;
  303. X}
  304. X
  305. X
  306. X/*
  307. X * Emit a restart marker & resynchronize predictions.
  308. X */
  309. X
  310. XLOCAL void
  311. Xemit_restart (compress_info_ptr cinfo)
  312. X{
  313. X  short ci;
  314. X
  315. X  flush_bits();
  316. X
  317. X  emit_byte(0xFF);
  318. X  emit_byte(RST0 + cinfo->next_restart_num);
  319. X
  320. X  /* Re-initialize DC predictions to 0 */
  321. X  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  322. X    cinfo->last_dc_val[ci] = 0;
  323. X
  324. X  /* Update restart state */
  325. X  cinfo->restarts_to_go = cinfo->restart_interval;
  326. X  cinfo->next_restart_num++;
  327. X  cinfo->next_restart_num &= 7;
  328. X}
  329. X
  330. X
  331. X/*
  332. X * Encode and output one MCU's worth of Huffman-compressed coefficients.
  333. X */
  334. X
  335. XMETHODDEF void
  336. Xhuff_encode (compress_info_ptr cinfo, JBLOCK *MCU_data)
  337. X{
  338. X  short blkn, ci;
  339. X  jpeg_component_info * compptr;
  340. X  JCOEF temp;
  341. X
  342. X  /* Account for restart interval, emit restart marker if needed */
  343. X  if (cinfo->restart_interval) {
  344. X    if (cinfo->restarts_to_go == 0)
  345. X      emit_restart(cinfo);
  346. X    cinfo->restarts_to_go--;
  347. X  }
  348. X
  349. X  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  350. X    ci = cinfo->MCU_membership[blkn];
  351. X    compptr = cinfo->cur_comp_info[ci];
  352. X    /* Convert DC value to difference, update last_dc_val */
  353. X    temp = MCU_data[blkn][0];
  354. X    MCU_data[blkn][0] -= cinfo->last_dc_val[ci];
  355. X    cinfo->last_dc_val[ci] = temp;
  356. X    encode_one_block(MCU_data[blkn],
  357. X             cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no],
  358. X             cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
  359. X  }
  360. X}
  361. X
  362. X
  363. X/*
  364. X * Finish up at the end of a Huffman-compressed scan.
  365. X */
  366. X
  367. XMETHODDEF void
  368. Xhuff_term (compress_info_ptr cinfo)
  369. X{
  370. X  /* Flush out the last data */
  371. X  flush_bits();
  372. X  flush_bytes();
  373. X  /* Release the I/O buffer */
  374. X  (*cinfo->emethods->free_small) ((void *) output_buffer);
  375. X}
  376. X
  377. X
  378. X
  379. X
  380. X/*
  381. X * Huffman coding optimization.
  382. X *
  383. X * This actually is optimization, in the sense that we find the best possible
  384. X * Huffman table(s) for the given data.  We first scan the supplied data and
  385. X * count the number of uses of each symbol that is to be Huffman-coded.
  386. X * (This process must agree with the code above.)  Then we build an
  387. X * optimal Huffman coding tree for the observed counts.
  388. X */
  389. X
  390. X#ifdef ENTROPY_OPT_SUPPORTED
  391. X
  392. X
  393. X/* These are static so htest_one_block can find 'em */
  394. Xstatic long * dc_count_ptrs[NUM_HUFF_TBLS];
  395. Xstatic long * ac_count_ptrs[NUM_HUFF_TBLS];
  396. X
  397. X
  398. XLOCAL void
  399. Xgen_huff_coding (compress_info_ptr cinfo, HUFF_TBL *htbl, long freq[])
  400. X/* Generate the optimal coding for the given counts */
  401. X{
  402. X#define MAX_CLEN 32        /* assumed maximum initial code length */
  403. X  UINT8 bits[MAX_CLEN+1];    /* bits[k] = # of symbols with code length k */
  404. X  short codesize[257];        /* codesize[k] = code length of symbol k */
  405. X  short others[257];        /* next symbol in current branch of tree */
  406. X  int c1, c2;
  407. X  int p, i, j;
  408. X  long v;
  409. X
  410. X  /* This algorithm is explained in section K.2 of the JPEG standard */
  411. X
  412. X  MEMZERO((void *) bits, SIZEOF(bits));
  413. X  MEMZERO((void *) codesize, SIZEOF(codesize));
  414. X  for (i = 0; i < 257; i++)
  415. X    others[i] = -1;        /* init links to empty */
  416. X  
  417. X  freq[256] = 1;        /* make sure there is a nonzero count */
  418. X  /* including the pseudo-symbol 256 in the Huffman procedure guarantees
  419. X   * that no real symbol is given code-value of all ones, because 256
  420. X   * will be placed in the largest codeword category.
  421. X   */
  422. X
  423. X  /* Huffman's basic algorithm to assign optimal code lengths to symbols */
  424. X
  425. X  for (;;) {
  426. X    /* Find the smallest nonzero frequency, set c1 = its symbol */
  427. X    /* In case of ties, take the larger symbol number */
  428. X    c1 = -1;
  429. X    v = 1000000000L;
  430. X    for (i = 0; i <= 256; i++) {
  431. X      if (freq[i] && freq[i] <= v) {
  432. X    v = freq[i];
  433. X    c1 = i;
  434. X      }
  435. X    }
  436. X
  437. X    /* Find the next smallest nonzero frequency, set c2 = its symbol */
  438. X    /* In case of ties, take the larger symbol number */
  439. X    c2 = -1;
  440. X    v = 1000000000L;
  441. X    for (i = 0; i <= 256; i++) {
  442. X      if (freq[i] && freq[i] <= v && i != c1) {
  443. X    v = freq[i];
  444. X    c2 = i;
  445. X      }
  446. X    }
  447. X
  448. X    /* Done if we've merged everything into one frequency */
  449. X    if (c2 < 0)
  450. X      break;
  451. X    
  452. X    /* Else merge the two counts/trees */
  453. X    freq[c1] += freq[c2];
  454. X    freq[c2] = 0;
  455. X
  456. X    /* Increment the codesize of everything in c1's tree branch */
  457. X    codesize[c1]++;
  458. X    while (others[c1] >= 0) {
  459. X      c1 = others[c1];
  460. X      codesize[c1]++;
  461. X    }
  462. X    
  463. X    others[c1] = c2;        /* chain c2 onto c1's tree branch */
  464. X    
  465. X    /* Increment the codesize of everything in c2's tree branch */
  466. X    codesize[c2]++;
  467. X    while (others[c2] >= 0) {
  468. X      c2 = others[c2];
  469. X      codesize[c2]++;
  470. X    }
  471. X  }
  472. X
  473. X  /* Now count the number of symbols of each code length */
  474. X  for (i = 0; i <= 256; i++) {
  475. X    if (codesize[i]) {
  476. X      /* The JPEG standard seems to think that this can't happen, */
  477. X      /* but I'm paranoid... */
  478. X      if (codesize[i] > MAX_CLEN)
  479. X    ERREXIT(cinfo->emethods, "Huffman code size table overflow");
  480. X
  481. X      bits[codesize[i]]++;
  482. X    }
  483. X  }
  484. X
  485. X  /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
  486. X   * Huffman procedure assigned any such lengths, we must adjust the coding.
  487. X   * Here is what the JPEG spec says about how this next bit works:
  488. X   * Since symbols are paired for the longest Huffman code, the symbols are
  489. X   * removed from this length category two at a time.  The prefix for the pair
  490. X   * (which is one bit shorter) is allocated to one of the pair; then,
  491. X   * skipping the BITS entry for that prefix length, a code word from the next
  492. X   * shortest nonzero BITS entry is converted into a prefix for two code words
  493. X   * one bit longer.
  494. X   */
  495. X  
  496. X  for (i = MAX_CLEN; i > 16; i--) {
  497. X    while (bits[i] > 0) {
  498. X      j = i - 2;        /* find length of new prefix to be used */
  499. X      while (bits[j] == 0)
  500. X    j--;
  501. X      
  502. X      bits[i] -= 2;        /* remove two symbols */
  503. X      bits[i-1]++;        /* one goes in this length */
  504. X      bits[j+1] += 2;        /* two new symbols in this length */
  505. X      bits[j]--;        /* symbol of this length is now a prefix */
  506. X    }
  507. X  }
  508. X
  509. X  /* Remove the count for the pseudo-symbol 256 from the largest codelength */
  510. X  while (bits[i] == 0)        /* find largest codelength still in use */
  511. X    i--;
  512. X  bits[i]--;
  513. X  
  514. X  /* Return final symbol counts (only for lengths 0..16) */
  515. X  memcpy((void *) htbl->bits, (void *) bits, SIZEOF(htbl->bits));
  516. X  
  517. X  /* Return a list of the symbols sorted by code length */
  518. X  /* It's not real clear to me why we don't need to consider the codelength
  519. X   * changes made above, but the JPEG spec seems to think this works.
  520. X   */
  521. X  p = 0;
  522. X  for (i = 1; i <= MAX_CLEN; i++) {
  523. X    for (j = 0; j <= 255; j++) {
  524. X      if (codesize[j] == i) {
  525. X    htbl->huffval[p] = (UINT8) j;
  526. X    p++;
  527. X      }
  528. X    }
  529. X  }
  530. X}
  531. X
  532. X
  533. X/* Process a single block's worth of coefficients */
  534. X/* Note that the DC coefficient has already been converted to a difference */
  535. X
  536. XLOCAL void
  537. Xhtest_one_block (JBLOCK block, JCOEF block0,
  538. X         long dc_counts[], long ac_counts[])
  539. X{
  540. X  register INT32 temp;
  541. X  register int nbits;
  542. X  register int k, r;
  543. X  
  544. X  /* Encode the DC coefficient difference per section F.1.2.1 */
  545. X  
  546. X  /* Find the number of bits needed for the magnitude of the coefficient */
  547. X  temp = block0;
  548. X  if (temp < 0) temp = -temp;
  549. X  
  550. X  for (nbits = 0; temp; nbits++)
  551. X    temp >>= 1;
  552. X  
  553. X  /* Count the Huffman symbol for the number of bits */
  554. X  dc_counts[nbits]++;
  555. X  
  556. X  /* Encode the AC coefficients per section F.1.2.2 */
  557. X  
  558. X  r = 0;            /* r = run length of zeros */
  559. X  
  560. X  for (k = 1; k < DCTSIZE2; k++) {
  561. X    if ((temp = block[k]) == 0) {
  562. X      r++;
  563. X    } else {
  564. X      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  565. X      while (r > 15) {
  566. X    ac_counts[0xF0]++;
  567. X    r -= 16;
  568. X      }
  569. X      
  570. X      /* Find the number of bits needed for the magnitude of the coefficient */
  571. X      if (temp < 0) temp = -temp;
  572. X      
  573. X      for (nbits = 0; temp; nbits++)
  574. X    temp >>= 1;
  575. X      
  576. X      /* Count Huffman symbol for run length / number of bits */
  577. X      ac_counts[(r << 4) + nbits]++;
  578. X      
  579. X      r = 0;
  580. X    }
  581. X  }
  582. X
  583. X  /* If the last coef(s) were zero, emit an end-of-block code */
  584. X  if (r > 0)
  585. X    ac_counts[0]++;
  586. X}
  587. X
  588. X
  589. X
  590. X/*
  591. X * Trial-encode one MCU's worth of Huffman-compressed coefficients.
  592. X */
  593. X
  594. XLOCAL void
  595. Xhtest_encode (compress_info_ptr cinfo, JBLOCK *MCU_data)
  596. X{
  597. X  short blkn, ci;
  598. X  jpeg_component_info * compptr;
  599. X
  600. X  /* Take care of restart intervals if needed */
  601. X  if (cinfo->restart_interval) {
  602. X    if (cinfo->restarts_to_go == 0) {
  603. X      /* Re-initialize DC predictions to 0 */
  604. X      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  605. X    cinfo->last_dc_val[ci] = 0;
  606. X      /* Update restart state */
  607. X      cinfo->restarts_to_go = cinfo->restart_interval;
  608. X    }
  609. X    cinfo->restarts_to_go--;
  610. X  }
  611. X
  612. X  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  613. X    ci = cinfo->MCU_membership[blkn];
  614. X    compptr = cinfo->cur_comp_info[ci];
  615. X    /* NB: unlike the real entropy encoder, we may not change the input data */
  616. X    htest_one_block(MCU_data[blkn],
  617. X            (JCOEF) (MCU_data[blkn][0] - cinfo->last_dc_val[ci]),
  618. X            dc_count_ptrs[compptr->dc_tbl_no],
  619. X            ac_count_ptrs[compptr->ac_tbl_no]);
  620. X    cinfo->last_dc_val[ci] = MCU_data[blkn][0];
  621. X  }
  622. X}
  623. X
  624. X
  625. X
  626. X/*
  627. X * Find the best coding parameters for a Huffman-coded scan.
  628. X * When called, the scan data has already been converted to a sequence of
  629. X * MCU groups of quantized coefficients, which are stored in a "big" array.
  630. X * The source_method knows how to iterate through that array.
  631. X * On return, the MCU data is unmodified, but the Huffman tables referenced
  632. X * by the scan components may have been altered.
  633. X */
  634. X
  635. XMETHODDEF void
  636. Xhuff_optimize (compress_info_ptr cinfo, MCU_output_caller_ptr source_method)
  637. X/* Optimize Huffman-coding parameters (Huffman symbol table) */
  638. X{
  639. X  int i, tbl;
  640. X  HUFF_TBL **htblptr;
  641. X
  642. X  /* Allocate and zero the count tables */
  643. X  /* Note that gen_huff_coding expects 257 entries in each table! */
  644. X
  645. X  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  646. X    dc_count_ptrs[i] = NULL;
  647. X    ac_count_ptrs[i] = NULL;
  648. X  }
  649. X
  650. X  for (i = 0; i < cinfo->comps_in_scan; i++) {
  651. X    /* Create DC table */
  652. X    tbl = cinfo->cur_comp_info[i]->dc_tbl_no;
  653. X    if (dc_count_ptrs[tbl] == NULL) {
  654. X      dc_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small)
  655. X                    (257 * SIZEOF(long));
  656. X      MEMZERO((void *) dc_count_ptrs[tbl], 257 * SIZEOF(long));
  657. X    }
  658. X    /* Create AC table */
  659. X    tbl = cinfo->cur_comp_info[i]->ac_tbl_no;
  660. X    if (ac_count_ptrs[tbl] == NULL) {
  661. X      ac_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small)
  662. X                    (257 * SIZEOF(long));
  663. X      MEMZERO((void *) ac_count_ptrs[tbl], 257 * SIZEOF(long));
  664. X    }
  665. X  }
  666. X
  667. X  /* Initialize DC predictions to 0 */
  668. X  for (i = 0; i < cinfo->comps_in_scan; i++) {
  669. X    cinfo->last_dc_val[i] = 0;
  670. X  }
  671. X  /* Initialize restart stuff */
  672. X  cinfo->restarts_to_go = cinfo->restart_interval;
  673. X
  674. X  /* Scan the MCU data, count symbol uses */
  675. X  (*source_method) (cinfo, htest_encode);
  676. X
  677. X  /* Now generate optimal Huffman tables */
  678. X  for (tbl = 0; tbl < NUM_HUFF_TBLS; tbl++) {
  679. X    if (dc_count_ptrs[tbl] != NULL) {
  680. X      htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
  681. X      if (*htblptr == NULL)
  682. X    *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
  683. X      /* Set sent_table FALSE so updated table will be written to JPEG file. */
  684. X      (*htblptr)->sent_table = FALSE;
  685. X      /* Compute the optimal Huffman encoding */
  686. X      gen_huff_coding(cinfo, *htblptr, dc_count_ptrs[tbl]);
  687. X      /* Release the count table */
  688. X      (*cinfo->emethods->free_small) ((void *) dc_count_ptrs[tbl]);
  689. X    }
  690. X    if (ac_count_ptrs[tbl] != NULL) {
  691. X      htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
  692. X      if (*htblptr == NULL)
  693. X    *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
  694. X      /* Set sent_table FALSE so updated table will be written to JPEG file. */
  695. X      (*htblptr)->sent_table = FALSE;
  696. X      /* Compute the optimal Huffman encoding */
  697. X      gen_huff_coding(cinfo, *htblptr, ac_count_ptrs[tbl]);
  698. X      /* Release the count table */
  699. X      (*cinfo->emethods->free_small) ((void *) ac_count_ptrs[tbl]);
  700. X    }
  701. X  }
  702. X}
  703. X
  704. X
  705. X#endif /* ENTROPY_OPT_SUPPORTED */
  706. X
  707. X
  708. X/*
  709. X * The method selection routine for Huffman entropy encoding.
  710. X */
  711. X
  712. XGLOBAL void
  713. Xjselchuffman (compress_info_ptr cinfo)
  714. X{
  715. X  if (! cinfo->arith_code) {
  716. X    cinfo->methods->entropy_encoder_init = huff_init;
  717. X    cinfo->methods->entropy_encode = huff_encode;
  718. X    cinfo->methods->entropy_encoder_term = huff_term;
  719. X#ifdef ENTROPY_OPT_SUPPORTED
  720. X    cinfo->methods->entropy_optimize = huff_optimize;
  721. X    /* The standard Huffman tables are only valid for 8-bit data precision.
  722. X     * If the precision is higher, force optimization on so that usable
  723. X     * tables will be computed.  This test can be removed if default tables
  724. X     * are supplied that are valid for the desired precision.
  725. X     */
  726. X    if (cinfo->data_precision > 8)
  727. X      cinfo->optimize_coding = TRUE;
  728. X    if (cinfo->optimize_coding)
  729. X      cinfo->total_passes++;    /* one pass needed for entropy optimization */
  730. X#endif
  731. X  }
  732. X}
  733. END_OF_FILE
  734.   if test 20071 -ne `wc -c <'jchuff.c'`; then
  735.     echo shar: \"'jchuff.c'\" unpacked with wrong size!
  736.   fi
  737.   # end of 'jchuff.c'
  738. fi
  739. if test -f 'jcmain.c' -a "${1}" != "-c" ; then 
  740.   echo shar: Will not clobber existing file \"'jcmain.c'\"
  741. else
  742.   echo shar: Extracting \"'jcmain.c'\" \(9991 characters\)
  743.   sed "s/^X//" >'jcmain.c' <<'END_OF_FILE'
  744. X/*
  745. X * jcmain.c
  746. X *
  747. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  748. X * This file is part of the Independent JPEG Group's software.
  749. X * For conditions of distribution and use, see the accompanying README file.
  750. X *
  751. X * This file contains a trivial test user interface for the JPEG compressor.
  752. X * It should work on any system with Unix- or MS-DOS-style command lines.
  753. X *
  754. X * Two different command line styles are permitted, depending on the
  755. X * compile-time switch TWO_FILE_COMMANDLINE:
  756. X *    cjpeg [options]  inputfile outputfile
  757. X *    cjpeg [options]  [inputfile]
  758. X * In the second style, output is always to standard output, which you'd
  759. X * normally redirect to a file or pipe to some other program.  Input is
  760. X * either from a named file or from standard input (typically redirected).
  761. X * The second style is convenient on Unix but is unhelpful on systems that
  762. X * don't support pipes.  Also, you MUST use the first style if your system
  763. X * doesn't do binary I/O to stdin/stdout.
  764. X */
  765. X
  766. X#include "jinclude.h"
  767. X#ifdef INCLUDES_ARE_ANSI
  768. X#include <stdlib.h>        /* to declare exit() */
  769. X#endif
  770. X#ifdef NEED_SIGNAL_CATCHER
  771. X#include <signal.h>        /* to declare signal() */
  772. X#endif
  773. X
  774. X#ifdef THINK_C
  775. X#include <console.h>        /* command-line reader for Macintosh */
  776. X#endif
  777. X
  778. X#ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  779. X#define READ_BINARY    "r"
  780. X#define WRITE_BINARY    "w"
  781. X#else
  782. X#define READ_BINARY    "rb"
  783. X#define WRITE_BINARY    "wb"
  784. X#endif
  785. X
  786. X#ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  787. X#define EXIT_FAILURE  1
  788. X#endif
  789. X#ifndef EXIT_SUCCESS
  790. X#ifdef VMS
  791. X#define EXIT_SUCCESS  1        /* VMS is very nonstandard */
  792. X#else
  793. X#define EXIT_SUCCESS  0
  794. X#endif
  795. X#endif
  796. X
  797. X
  798. X#include "jversion.h"        /* for version message */
  799. X
  800. X
  801. X/*
  802. X * PD version of getopt(3).
  803. X */
  804. X
  805. X#include "egetopt.c"
  806. X
  807. X
  808. X/*
  809. X * This routine determines what format the input file is,
  810. X * and selects the appropriate input-reading module.
  811. X *
  812. X * To determine which family of input formats the file belongs to,
  813. X * we may look only at the first byte of the file, since C does not
  814. X * guarantee that more than one character can be pushed back with ungetc.
  815. X * Looking at additional bytes would require one of these approaches:
  816. X *     1) assume we can fseek() the input file (fails for piped input);
  817. X *     2) assume we can push back more than one character (works in
  818. X *        some C implementations, but unportable);
  819. X *     3) provide our own buffering as is done in djpeg (breaks input readers
  820. X *        that want to use stdio directly, such as the RLE library);
  821. X * or  4) don't put back the data, and modify the input_init methods to assume
  822. X *        they start reading after the start of file (also breaks RLE library).
  823. X * #1 is attractive for MS-DOS but is untenable on Unix.
  824. X *
  825. X * The most portable solution for file types that can't be identified by their
  826. X * first byte is to make the user tell us what they are.  This is also the
  827. X * only approach for "raw" file types that contain only arbitrary values.
  828. X * We presently apply this method for Targa files.  Most of the time Targa
  829. X * files start with 0x00, so we recognize that case.  Potentially, however,
  830. X * a Targa file could start with any byte value (byte 0 is the length of the
  831. X * seldom-used ID field), so we accept a -T switch to force Targa input mode.
  832. X */
  833. X
  834. Xstatic boolean is_targa;    /* records user -T switch */
  835. X
  836. X
  837. XLOCAL void
  838. Xselect_file_type (compress_info_ptr cinfo)
  839. X{
  840. X  int c;
  841. X
  842. X  if (is_targa) {
  843. X#ifdef TARGA_SUPPORTED
  844. X    jselrtarga(cinfo);
  845. X#else
  846. X    ERREXIT(cinfo->emethods, "Targa support was not compiled");
  847. X#endif
  848. X    return;
  849. X  }
  850. X
  851. X  if ((c = getc(cinfo->input_file)) == EOF)
  852. X    ERREXIT(cinfo->emethods, "Empty input file");
  853. X
  854. X  switch (c) {
  855. X#ifdef GIF_SUPPORTED
  856. X  case 'G':
  857. X    jselrgif(cinfo);
  858. X    break;
  859. X#endif
  860. X#ifdef PPM_SUPPORTED
  861. X  case 'P':
  862. X    jselrppm(cinfo);
  863. X    break;
  864. X#endif
  865. X#ifdef RLE_SUPPORTED
  866. X  case 'R':
  867. X    jselrrle(cinfo);
  868. X    break;
  869. X#endif
  870. X#ifdef TARGA_SUPPORTED
  871. X  case 0x00:
  872. X    jselrtarga(cinfo);
  873. X    break;
  874. X#endif
  875. X  default:
  876. X#ifdef TARGA_SUPPORTED
  877. X    ERREXIT(cinfo->emethods, "Unrecognized input file format --- did you forget -T ?");
  878. X#else
  879. X    ERREXIT(cinfo->emethods, "Unrecognized input file format");
  880. X#endif
  881. X    break;
  882. X  }
  883. X
  884. X  if (ungetc(c, cinfo->input_file) == EOF)
  885. X    ERREXIT(cinfo->emethods, "ungetc failed");
  886. X}
  887. X
  888. X
  889. X/*
  890. X * This routine gets control after the input file header has been read.
  891. X * It must determine what output JPEG file format is to be written,
  892. X * and make any other compression parameter changes that are desirable.
  893. X */
  894. X
  895. XMETHODDEF void
  896. Xc_ui_method_selection (compress_info_ptr cinfo)
  897. X{
  898. X  /* If the input is gray scale, generate a monochrome JPEG file. */
  899. X  if (cinfo->in_color_space == CS_GRAYSCALE)
  900. X    j_monochrome_default(cinfo);
  901. X  /* For now, always select JFIF output format. */
  902. X#ifdef JFIF_SUPPORTED
  903. X  jselwjfif(cinfo);
  904. X#else
  905. X  You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  906. X#endif
  907. X}
  908. X
  909. X
  910. X/*
  911. X * Signal catcher to ensure that temporary files are removed before aborting.
  912. X * NB: for Amiga Manx C this is actually a global routine named _abort();
  913. X * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
  914. X */
  915. X
  916. X#ifdef NEED_SIGNAL_CATCHER
  917. X
  918. Xstatic external_methods_ptr emethods; /* for access to free_all */
  919. X
  920. XGLOBAL void
  921. Xsignal_catcher (int signum)
  922. X{
  923. X  emethods->trace_level = 0;    /* turn off trace output */
  924. X  (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  925. X  exit(EXIT_FAILURE);
  926. X}
  927. X
  928. X#endif
  929. X
  930. X
  931. XLOCAL void
  932. Xusage (char * progname)
  933. X/* complain about bad command line */
  934. X{
  935. X  fprintf(stderr, "usage: %s ", progname);
  936. X  fprintf(stderr, "[-Q quality 0..100] [-o] [-T] [-I] [-a] [-d] [-m mem]");
  937. X#ifdef TWO_FILE_COMMANDLINE
  938. X  fprintf(stderr, " inputfile outputfile\n");
  939. X#else
  940. X  fprintf(stderr, " [inputfile]\n");
  941. X#endif
  942. X  exit(EXIT_FAILURE);
  943. X}
  944. X
  945. X
  946. X/*
  947. X * The main program.
  948. X */
  949. X
  950. XGLOBAL int
  951. Xmain (int argc, char **argv)
  952. X{
  953. X  struct compress_info_struct cinfo;
  954. X  struct compress_methods_struct c_methods;
  955. X  struct external_methods_struct e_methods;
  956. X  int c;
  957. X
  958. X  /* On Mac, fetch a command line. */
  959. X#ifdef THINK_C
  960. X  argc = ccommand(&argv);
  961. X#endif
  962. X
  963. X  /* Initialize the system-dependent method pointers. */
  964. X  cinfo.methods = &c_methods;
  965. X  cinfo.emethods = &e_methods;
  966. X  jselerror(&e_methods);    /* error/trace message routines */
  967. X  jselmemmgr(&e_methods);    /* memory allocation routines */
  968. X  c_methods.c_ui_method_selection = c_ui_method_selection;
  969. X
  970. X  /* Now OK to enable signal catcher. */
  971. X#ifdef NEED_SIGNAL_CATCHER
  972. X  emethods = &e_methods;
  973. X  signal(SIGINT, signal_catcher);
  974. X#ifdef SIGTERM            /* not all systems have SIGTERM */
  975. X  signal(SIGTERM, signal_catcher);
  976. X#endif
  977. X#endif
  978. X
  979. X  /* Set up default JPEG parameters. */
  980. X  j_c_defaults(&cinfo, 75, FALSE); /* default quality level = 75 */
  981. X  is_targa = FALSE;
  982. X
  983. X  /* Scan command line options, adjust parameters */
  984. X  
  985. X  while ((c = egetopt(argc, argv, "IQ:Taom:d")) != EOF)
  986. X    switch (c) {
  987. X    case 'I':            /* Create noninterleaved file. */
  988. X#ifdef MULTISCAN_FILES_SUPPORTED
  989. X      cinfo.interleave = FALSE;
  990. X#else
  991. X      fprintf(stderr, "%s: sorry, multiple-scan support was not compiled\n",
  992. X          argv[0]);
  993. X      exit(EXIT_FAILURE);
  994. X#endif
  995. X      break;
  996. X    case 'Q':            /* Quality factor. */
  997. X      { int val;
  998. X    if (optarg == NULL)
  999. X      usage(argv[0]);
  1000. X    if (sscanf(optarg, "%d", &val) != 1)
  1001. X      usage(argv[0]);
  1002. X    /* Note: for now, we make force_baseline FALSE.
  1003. X     * This means non-baseline JPEG files can be created with low Q values.
  1004. X     * To ensure only baseline files are generated, pass TRUE instead.
  1005. X     */
  1006. X    j_set_quality(&cinfo, val, FALSE);
  1007. X      }
  1008. X      break;
  1009. X    case 'T':            /* Input file is Targa format. */
  1010. X      is_targa = TRUE;
  1011. X      break;
  1012. X    case 'a':            /* Use arithmetic coding. */
  1013. X#ifdef ARITH_CODING_SUPPORTED
  1014. X      cinfo.arith_code = TRUE;
  1015. X#else
  1016. X      fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
  1017. X          argv[0]);
  1018. X      exit(EXIT_FAILURE);
  1019. X#endif
  1020. X      break;
  1021. X    case 'o':            /* Enable entropy parm optimization. */
  1022. X#ifdef ENTROPY_OPT_SUPPORTED
  1023. X      cinfo.optimize_coding = TRUE;
  1024. X#else
  1025. X      fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
  1026. X          argv[0]);
  1027. X      exit(EXIT_FAILURE);
  1028. X#endif
  1029. X      break;
  1030. X    case 'm':            /* Maximum memory in Kb (or Mb with 'm'). */
  1031. X      { long lval;
  1032. X    char ch = 'x';
  1033. X
  1034. X    if (optarg == NULL)
  1035. X      usage(argv[0]);
  1036. X    if (sscanf(optarg, "%ld%c", &lval, &ch) < 1)
  1037. X      usage(argv[0]);
  1038. X    if (ch == 'm' || ch == 'M')
  1039. X      lval *= 1000L;
  1040. X    e_methods.max_memory_to_use = lval * 1000L;
  1041. X      }
  1042. X      break;
  1043. X    case 'd':            /* Debugging. */
  1044. X      e_methods.trace_level++;
  1045. X      break;
  1046. X    case '?':
  1047. X    default:
  1048. X      usage(argv[0]);
  1049. X      break;
  1050. X    }
  1051. X
  1052. X  /* If -d appeared, print version identification */
  1053. X  if (e_methods.trace_level > 0)
  1054. X    fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n",
  1055. X        JVERSION, JCOPYRIGHT);
  1056. X
  1057. X  /* Select the input and output files */
  1058. X
  1059. X#ifdef TWO_FILE_COMMANDLINE
  1060. X
  1061. X  if (optind != argc-2) {
  1062. X    fprintf(stderr, "%s: must name one input and one output file\n", argv[0]);
  1063. X    usage(argv[0]);
  1064. X  }
  1065. X  if ((cinfo.input_file = fopen(argv[optind], READ_BINARY)) == NULL) {
  1066. X    fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  1067. X    exit(EXIT_FAILURE);
  1068. X  }
  1069. X  if ((cinfo.output_file = fopen(argv[optind+1], WRITE_BINARY)) == NULL) {
  1070. X    fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind+1]);
  1071. X    exit(EXIT_FAILURE);
  1072. X  }
  1073. X
  1074. X#else /* not TWO_FILE_COMMANDLINE -- use Unix style */
  1075. X
  1076. X  cinfo.input_file = stdin;    /* default input file */
  1077. X  cinfo.output_file = stdout;    /* always the output file */
  1078. X
  1079. X  if (optind < argc-1) {
  1080. X    fprintf(stderr, "%s: only one input file\n", argv[0]);
  1081. X    usage(argv[0]);
  1082. X  }
  1083. X  if (optind < argc) {
  1084. X    if ((cinfo.input_file = fopen(argv[optind], READ_BINARY)) == NULL) {
  1085. X      fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  1086. X      exit(EXIT_FAILURE);
  1087. X    }
  1088. X  }
  1089. X
  1090. X#endif /* TWO_FILE_COMMANDLINE */
  1091. X
  1092. X  /* Figure out the input file format, and set up to read it. */
  1093. X  select_file_type(&cinfo);
  1094. X
  1095. X  /* Do it to it! */
  1096. X  jpeg_compress(&cinfo);
  1097. X
  1098. X  /* All done. */
  1099. X  exit(EXIT_SUCCESS);
  1100. X  return 0;            /* suppress no-return-value warnings */
  1101. X}
  1102. END_OF_FILE
  1103.   if test 9991 -ne `wc -c <'jcmain.c'`; then
  1104.     echo shar: \"'jcmain.c'\" unpacked with wrong size!
  1105.   fi
  1106.   # end of 'jcmain.c'
  1107. fi
  1108. if test -f 'jrdgif.c' -a "${1}" != "-c" ; then 
  1109.   echo shar: Will not clobber existing file \"'jrdgif.c'\"
  1110. else
  1111.   echo shar: Extracting \"'jrdgif.c'\" \(19830 characters\)
  1112.   sed "s/^X//" >'jrdgif.c' <<'END_OF_FILE'
  1113. X/*
  1114. X * jrdgif.c
  1115. X *
  1116. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  1117. X * This file is part of the Independent JPEG Group's software.
  1118. X * For conditions of distribution and use, see the accompanying README file.
  1119. X *
  1120. X * This file contains routines to read input images in GIF format.
  1121. X *
  1122. X * These routines may need modification for non-Unix environments or
  1123. X * specialized applications.  As they stand, they assume input from
  1124. X * an ordinary stdio stream.  They further assume that reading begins
  1125. X * at the start of the file; input_init may need work if the
  1126. X * user interface has already read some data (e.g., to determine that
  1127. X * the file is indeed GIF format).
  1128. X *
  1129. X * These routines are invoked via the methods get_input_row
  1130. X * and input_init/term.
  1131. X */
  1132. X
  1133. X/*
  1134. X * This code is loosely based on giftoppm from the PBMPLUS distribution
  1135. X * of Feb. 1991.  That file contains the following copyright notice:
  1136. X * +-------------------------------------------------------------------+
  1137. X * | Copyright 1990, David Koblas.                                     |
  1138. X * |   Permission to use, copy, modify, and distribute this software   |
  1139. X * |   and its documentation for any purpose and without fee is hereby |
  1140. X * |   granted, provided that the above copyright notice appear in all |
  1141. X * |   copies and that both that copyright notice and this permission  |
  1142. X * |   notice appear in supporting documentation.  This software is    |
  1143. X * |   provided "as is" without express or implied warranty.           |
  1144. X * +-------------------------------------------------------------------+
  1145. X *
  1146. X * We are also required to state that
  1147. X *    "The Graphics Interchange Format(c) is the Copyright property of
  1148. X *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
  1149. X *    CompuServe Incorporated."
  1150. X */
  1151. X
  1152. X#include "jinclude.h"
  1153. X
  1154. X#ifdef GIF_SUPPORTED
  1155. X
  1156. X
  1157. X#define    MAXCOLORMAPSIZE    256    /* max # of colors in a GIF colormap */
  1158. X#define NUMCOLORS    3    /* # of colors */
  1159. X#define CM_RED        0    /* color component numbers */
  1160. X#define CM_GREEN    1
  1161. X#define CM_BLUE        2
  1162. X
  1163. Xstatic JSAMPARRAY colormap;    /* the colormap to use */
  1164. X/* colormap[i][j] = value of i'th color component for pixel value j */
  1165. X
  1166. X#define    MAX_LZW_BITS    12    /* maximum LZW code size */
  1167. X#define LZW_TABLE_SIZE    (1<<MAX_LZW_BITS) /* # of possible LZW symbols */
  1168. X
  1169. X/* Macros for extracting header data --- note we assume chars may be signed */
  1170. X
  1171. X#define LM_to_uint(a,b)        ((((b)&0xFF) << 8) | ((a)&0xFF))
  1172. X
  1173. X#define BitSet(byte, bit)    ((byte) & (bit))
  1174. X#define INTERLACE    0x40    /* mask for bit signifying interlaced image */
  1175. X#define COLORMAPFLAG    0x80    /* mask for bit signifying colormap presence */
  1176. X
  1177. X#define    ReadOK(file,buffer,len)    (JFREAD(file,buffer,len) == ((size_t) (len)))
  1178. X
  1179. X/* Static vars for GetCode and LZWReadByte */
  1180. X
  1181. Xstatic char code_buf[256+4];    /* current input data block */
  1182. Xstatic int last_byte;        /* # of bytes in code_buf */
  1183. Xstatic int last_bit;        /* # of bits in code_buf */
  1184. Xstatic int cur_bit;        /* next bit index to read */
  1185. Xstatic boolean out_of_blocks;    /* TRUE if hit terminator data block */
  1186. X
  1187. Xstatic int input_code_size;    /* codesize given in GIF file */
  1188. Xstatic int clear_code,end_code; /* values for Clear and End codes */
  1189. X
  1190. Xstatic int code_size;        /* current actual code size */
  1191. Xstatic int limit_code;        /* 2^code_size */
  1192. Xstatic int max_code;        /* first unused code value */
  1193. Xstatic boolean first_time;    /* flags first call to LZWReadByte */
  1194. X
  1195. X/* LZW decompression tables:
  1196. X *   symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  1197. X *   symbol_tail[K] = suffix byte   of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  1198. X * Note that entries 0..end_code of the above tables are not used,
  1199. X * since those symbols represent raw bytes or special codes.
  1200. X *
  1201. X * The stack represents the not-yet-used expansion of the last LZW symbol.
  1202. X * In the worst case, a symbol could expand to as many bytes as there are
  1203. X * LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack.
  1204. X * (This is conservative since that number includes the raw-byte symbols.)
  1205. X *
  1206. X * The tables are allocated from FAR heap space since they would use up
  1207. X * rather a lot of the near data space in a PC.
  1208. X */
  1209. X
  1210. Xstatic UINT16 FAR *symbol_head; /* => table of prefix symbols */
  1211. Xstatic UINT8  FAR *symbol_tail; /* => table of suffix bytes */
  1212. Xstatic UINT8  FAR *symbol_stack; /* stack for symbol expansions */
  1213. Xstatic UINT8  FAR *sp;        /* stack pointer */
  1214. X
  1215. X/* Static state for interlaced image processing */
  1216. X
  1217. Xstatic boolean is_interlaced;    /* TRUE if have interlaced image */
  1218. Xstatic big_sarray_ptr interlaced_image;    /* full image in interlaced order */
  1219. Xstatic long cur_row_number;    /* need to know actual row number */
  1220. Xstatic long pass2_offset;    /* # of pixel rows in pass 1 */
  1221. Xstatic long pass3_offset;    /* # of pixel rows in passes 1&2 */
  1222. Xstatic long pass4_offset;    /* # of pixel rows in passes 1,2,3 */
  1223. X
  1224. X
  1225. X/* Forward declarations */
  1226. XMETHODDEF void load_interlaced_image PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  1227. XMETHODDEF void get_interlaced_row PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  1228. X
  1229. X
  1230. X
  1231. XLOCAL int
  1232. XReadByte (compress_info_ptr cinfo)
  1233. X/* Read next byte from GIF file */
  1234. X{
  1235. X  register FILE * infile = cinfo->input_file;
  1236. X  int c;
  1237. X
  1238. X  if ((c = getc(infile)) == EOF)
  1239. X    ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  1240. X  return c;
  1241. X}
  1242. X
  1243. X
  1244. XLOCAL int
  1245. XGetDataBlock (compress_info_ptr cinfo, char *buf)
  1246. X/* Read a GIF data block, which has a leading count byte */
  1247. X/* A zero-length block marks the end of a data block sequence */
  1248. X{
  1249. X  int count;
  1250. X
  1251. X  count = ReadByte(cinfo);
  1252. X  if (count > 0) {
  1253. X    if (! ReadOK(cinfo->input_file, buf, count))
  1254. X      ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  1255. X  }
  1256. X  return count;
  1257. X}
  1258. X
  1259. X
  1260. XLOCAL void
  1261. XSkipDataBlocks (compress_info_ptr cinfo)
  1262. X/* Skip a series of data blocks, until a block terminator is found */
  1263. X{
  1264. X  char buf[256];
  1265. X
  1266. X  while (GetDataBlock(cinfo, buf) > 0)
  1267. X    /* skip */;
  1268. X}
  1269. X
  1270. X
  1271. XLOCAL void
  1272. XReInitLZW (void)
  1273. X/* (Re)initialize LZW state; shared code for startup and Clear processing */
  1274. X{
  1275. X  code_size = input_code_size+1;
  1276. X  limit_code = clear_code << 1;    /* 2^code_size */
  1277. X  max_code = clear_code + 2;    /* first unused code value */
  1278. X  sp = symbol_stack;        /* init stack to empty */
  1279. X}
  1280. X
  1281. X
  1282. XLOCAL void
  1283. XInitLZWCode (void)
  1284. X/* Initialize for a series of LZWReadByte (and hence GetCode) calls */
  1285. X{
  1286. X  /* GetCode initialization */
  1287. X  last_byte = 2;        /* make safe to "recopy last two bytes" */
  1288. X  last_bit = 0;            /* nothing in the buffer */
  1289. X  cur_bit = 0;            /* force buffer load on first call */
  1290. X  out_of_blocks = FALSE;
  1291. X
  1292. X  /* LZWReadByte initialization */
  1293. X  clear_code = 1 << input_code_size; /* compute special code values */
  1294. X  end_code = clear_code + 1;    /* note that these do not change */
  1295. X  first_time = TRUE;
  1296. X  ReInitLZW();
  1297. X}
  1298. X
  1299. X
  1300. XLOCAL int
  1301. XGetCode (compress_info_ptr cinfo)
  1302. X/* Fetch the next code_size bits from the GIF data */
  1303. X/* We assume code_size is less than 16 */
  1304. X{
  1305. X  register INT32 accum;
  1306. X  int offs, ret, count;
  1307. X
  1308. X  if ( (cur_bit+code_size) > last_bit) {
  1309. X    /* Time to reload the buffer */
  1310. X    if (out_of_blocks) {
  1311. X      TRACEMS(cinfo->emethods, 1, "Ran out of GIF bits");
  1312. X      return end_code;        /* fake something useful */
  1313. X    }
  1314. X    /* preserve last two bytes of what we have -- assume code_size <= 16 */
  1315. X    code_buf[0] = code_buf[last_byte-2];
  1316. X    code_buf[1] = code_buf[last_byte-1];
  1317. X    /* Load more bytes; set flag if we reach the terminator block */
  1318. X    if ((count = GetDataBlock(cinfo, &code_buf[2])) == 0) {
  1319. X      out_of_blocks = TRUE;
  1320. X      TRACEMS(cinfo->emethods, 1, "Ran out of GIF bits");
  1321. X      return end_code;        /* fake something useful */
  1322. X    }
  1323. X    /* Reset counters */
  1324. X    cur_bit = (cur_bit - last_bit) + 16;
  1325. X    last_byte = 2 + count;
  1326. X    last_bit = last_byte * 8;
  1327. X  }
  1328. X
  1329. X  /* Form up next 24 bits in accum */
  1330. X  offs = cur_bit >> 3;        /* byte containing cur_bit */
  1331. X#ifdef CHAR_IS_UNSIGNED
  1332. X  accum = code_buf[offs+2];
  1333. X  accum <<= 8;
  1334. X  accum |= code_buf[offs+1];
  1335. X  accum <<= 8;
  1336. X  accum |= code_buf[offs];
  1337. X#else
  1338. X  accum = code_buf[offs+2] & 0xFF;
  1339. X  accum <<= 8;
  1340. X  accum |= code_buf[offs+1] & 0xFF;
  1341. X  accum <<= 8;
  1342. X  accum |= code_buf[offs] & 0xFF;
  1343. X#endif
  1344. X
  1345. X  /* Right-align cur_bit in accum, then mask off desired number of bits */
  1346. X  accum >>= (cur_bit & 7);
  1347. X  ret = ((int) accum) & ((1 << code_size) - 1);
  1348. X  
  1349. X  cur_bit += code_size;
  1350. X  return ret;
  1351. X}
  1352. X
  1353. X
  1354. XLOCAL int
  1355. XLZWReadByte (compress_info_ptr cinfo)
  1356. X/* Read an LZW-compressed byte */
  1357. X{
  1358. X  static int oldcode;        /* previous LZW symbol */
  1359. X  static int firstcode;        /* first byte of oldcode's expansion */
  1360. X  register int code;        /* current working code */
  1361. X  int incode;            /* saves actual input code */
  1362. X
  1363. X  /* First time, just eat the expected Clear code(s) and return next code, */
  1364. X  /* which is assumed to be a raw byte. */
  1365. X  if (first_time) {
  1366. X    first_time = FALSE;
  1367. X    do {
  1368. X      code = GetCode(cinfo);
  1369. X    } while (code == clear_code);
  1370. X    firstcode = oldcode = code;    /* make firstcode, oldcode valid! */
  1371. X    return code;
  1372. X  }
  1373. X
  1374. X  /* If any codes are stacked from a previously read symbol, return them */
  1375. X  if (sp > symbol_stack)
  1376. X    return (int) *(--sp);
  1377. X
  1378. X  code = GetCode(cinfo);
  1379. X
  1380. X  if (code == clear_code) {
  1381. X    /* Reinit static state, swallow any extra Clear codes, and return */
  1382. X    ReInitLZW();
  1383. X    do {
  1384. X      code = GetCode(cinfo);
  1385. X    } while (code == clear_code);
  1386. X    firstcode = oldcode = code; /* gotta reinit these too */
  1387. X    return code;
  1388. X  }
  1389. X
  1390. X  if (code == end_code) {
  1391. X    /* Skip the rest of the image, unless GetCode already read terminator */
  1392. X    if (! out_of_blocks)
  1393. X      SkipDataBlocks(cinfo);
  1394. X    return -1;
  1395. X  }
  1396. X
  1397. X  /* Normal raw byte or LZW symbol */
  1398. X  incode = code;        /* save for a moment */
  1399. X  
  1400. X  if (code >= max_code) {    /* special case for not-yet-defined symbol */
  1401. X    *sp++ = (UINT8) firstcode;    /* it will be defined as oldcode/firstcode */
  1402. X    code = oldcode;
  1403. X  }
  1404. X
  1405. X  /* If it's a symbol, expand it into the stack */
  1406. X  while (code >= clear_code) {
  1407. X    *sp++ = symbol_tail[code];    /* tail of symbol: a simple byte value */
  1408. X    code = symbol_head[code];    /* head of symbol: another LZW symbol */
  1409. X  }
  1410. X  /* At this point code just represents a raw byte */
  1411. X  firstcode = code;        /* save for possible future use */
  1412. X
  1413. X  /* If there's room in table, */
  1414. X  if ((code = max_code) < LZW_TABLE_SIZE) {
  1415. X    /* Define a new symbol = prev sym + head of this sym's expansion */
  1416. X    symbol_head[code] = oldcode;
  1417. X    symbol_tail[code] = (UINT8) firstcode;
  1418. X    max_code++;
  1419. X    /* Is it time to increase code_size? */
  1420. X    if ((max_code >= limit_code) && (code_size < MAX_LZW_BITS)) {
  1421. X      code_size++;
  1422. X      limit_code <<= 1;        /* keep equal to 2^code_size */
  1423. X    }
  1424. X  }
  1425. X  
  1426. X  oldcode = incode;        /* save last input symbol for future use */
  1427. X  return firstcode;        /* return first byte of symbol's expansion */
  1428. X}
  1429. X
  1430. X
  1431. XLOCAL void
  1432. XReadColorMap (compress_info_ptr cinfo, int cmaplen, JSAMPARRAY cmap)
  1433. X/* Read a GIF colormap */
  1434. X{
  1435. X  int i;
  1436. X
  1437. X  for (i = 0; i < cmaplen; i++) {
  1438. X    cmap[CM_RED][i]   = (JSAMPLE) ReadByte(cinfo);
  1439. X    cmap[CM_GREEN][i] = (JSAMPLE) ReadByte(cinfo);
  1440. X    cmap[CM_BLUE][i]  = (JSAMPLE) ReadByte(cinfo);
  1441. X  }
  1442. X}
  1443. X
  1444. X
  1445. XLOCAL void
  1446. XDoExtension (compress_info_ptr cinfo)
  1447. X/* Process an extension block */
  1448. X/* Currently we ignore 'em all */
  1449. X{
  1450. X  int extlabel;
  1451. X
  1452. X  /* Read extension label byte */
  1453. X  extlabel = ReadByte(cinfo);
  1454. X  TRACEMS1(cinfo->emethods, 1, "Ignoring GIF extension block of type 0x%02x",
  1455. X       extlabel);
  1456. X  /* Skip the data block(s) associated with the extension */
  1457. X  SkipDataBlocks(cinfo);
  1458. X}
  1459. X
  1460. X
  1461. X/*
  1462. X * Read the file header; return image size and component count.
  1463. X */
  1464. X
  1465. XMETHODDEF void
  1466. Xinput_init (compress_info_ptr cinfo)
  1467. X{
  1468. X  char hdrbuf[10];        /* workspace for reading control blocks */
  1469. X  UINT16 width, height;        /* image dimensions */
  1470. X  int colormaplen, aspectRatio;
  1471. X  int c;
  1472. X
  1473. X  /* Allocate space to store the colormap */
  1474. X  colormap = (*cinfo->emethods->alloc_small_sarray)
  1475. X        ((long) MAXCOLORMAPSIZE, (long) NUMCOLORS);
  1476. X
  1477. X  /* Read and verify GIF Header */
  1478. X  if (! ReadOK(cinfo->input_file, hdrbuf, 6))
  1479. X    ERREXIT(cinfo->emethods, "Not a GIF file");
  1480. X  if (strncmp(hdrbuf, "GIF", 3) != 0)
  1481. X    ERREXIT(cinfo->emethods, "Not a GIF file");
  1482. X  /* Check for expected version numbers.
  1483. X   * If unknown version, give warning and try to process anyway;
  1484. X   * this is per recommendation in GIF89a standard.
  1485. X   */
  1486. X  if ((strncmp(hdrbuf+3, "87a", 3) != 0) &&
  1487. X      (strncmp(hdrbuf+3, "89a", 3) != 0))
  1488. X    TRACEMS3(cinfo->emethods, 1,
  1489. X         "Warning: unexpected GIF version number '%c%c%c'",
  1490. X         hdrbuf[3], hdrbuf[4], hdrbuf[5]);
  1491. X
  1492. X  /* Read and decipher Logical Screen Descriptor */
  1493. X  if (! ReadOK(cinfo->input_file, hdrbuf, 7))
  1494. X    ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  1495. X  width = LM_to_uint(hdrbuf[0],hdrbuf[1]);
  1496. X  height = LM_to_uint(hdrbuf[2],hdrbuf[3]);
  1497. X  colormaplen = 2 << (hdrbuf[4] & 0x07);
  1498. X  /* we ignore the color resolution, sort flag, and background color index */
  1499. X  aspectRatio = hdrbuf[6] & 0xFF;
  1500. X  if (aspectRatio != 0 && aspectRatio != 49)
  1501. X    TRACEMS(cinfo->emethods, 1, "Warning: nonsquare pixels in input");
  1502. X
  1503. X  /* Read global colormap if header indicates it is present */
  1504. X  if (BitSet(hdrbuf[4], COLORMAPFLAG))
  1505. X    ReadColorMap(cinfo, colormaplen, colormap);
  1506. X
  1507. X  /* Scan until we reach start of desired image.
  1508. X   * We don't currently support skipping images, but could add it easily.
  1509. X   */
  1510. X  for (;;) {
  1511. X    c = ReadByte(cinfo);
  1512. X
  1513. X    if (c == ';')        /* GIF terminator?? */
  1514. X      ERREXIT(cinfo->emethods, "Too few images in GIF file");
  1515. X
  1516. X    if (c == '!') {        /* Extension */
  1517. X      DoExtension(cinfo);
  1518. X      continue;
  1519. X    }
  1520. X    
  1521. X    if (c != ',') {        /* Not an image separator? */
  1522. X      TRACEMS1(cinfo->emethods, 1, "Bogus input char 0x%02x, ignoring", c);
  1523. X      continue;
  1524. X    }
  1525. X
  1526. X    /* Read and decipher Local Image Descriptor */
  1527. X    if (! ReadOK(cinfo->input_file, hdrbuf, 9))
  1528. X      ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  1529. X    /* we ignore top/left position info, also sort flag */
  1530. X    width = LM_to_uint(hdrbuf[4],hdrbuf[5]);
  1531. X    height = LM_to_uint(hdrbuf[6],hdrbuf[7]);
  1532. X    is_interlaced = BitSet(hdrbuf[8], INTERLACE);
  1533. X    colormaplen = 2 << (hdrbuf[8] & 0x07);
  1534. X
  1535. X    /* Read local colormap if header indicates it is present */
  1536. X    /* Note: if we wanted to support skipping images, */
  1537. X    /* we'd need to skip rather than read colormap for ignored images */
  1538. X    if (BitSet(hdrbuf[8], COLORMAPFLAG))
  1539. X      ReadColorMap(cinfo, colormaplen, colormap);
  1540. X
  1541. X    input_code_size = ReadByte(cinfo); /* get minimum-code-size byte */
  1542. X    if (input_code_size < 2 || input_code_size >= MAX_LZW_BITS)
  1543. X      ERREXIT1(cinfo->emethods, "Bogus codesize %d", input_code_size);
  1544. X
  1545. X    /* Reached desired image, so break out of loop */
  1546. X    /* If we wanted to skip this image, */
  1547. X    /* we'd call SkipDataBlocks and then continue the loop */
  1548. X    break;
  1549. X  }
  1550. X
  1551. X  /* Prepare to read selected image: first initialize LZW decompressor */
  1552. X  symbol_head = (UINT16 FAR *) (*cinfo->emethods->alloc_medium)
  1553. X                (LZW_TABLE_SIZE * SIZEOF(UINT16));
  1554. X  symbol_tail = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
  1555. X                (LZW_TABLE_SIZE * SIZEOF(UINT8));
  1556. X  symbol_stack = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
  1557. X                (LZW_TABLE_SIZE * SIZEOF(UINT8));
  1558. X  InitLZWCode();
  1559. X
  1560. X  /*
  1561. X   * If image is interlaced, we read it into a full-size sample array,
  1562. X   * decompressing as we go; then get_input_row selects rows from the
  1563. X   * sample array in the proper order.
  1564. X   */
  1565. X  if (is_interlaced) {
  1566. X    /* We request the big array now, but can't access it until the pipeline
  1567. X     * controller causes all the big arrays to be allocated.  Hence, the
  1568. X     * actual work of reading the image is postponed until the first call
  1569. X     * of get_input_row.
  1570. X     */
  1571. X    interlaced_image = (*cinfo->emethods->request_big_sarray)
  1572. X        ((long) width, (long) height, 1L);
  1573. X    cinfo->methods->get_input_row = load_interlaced_image;
  1574. X    cinfo->total_passes++;    /* count file reading as separate pass */
  1575. X  }
  1576. X
  1577. X  /* Return info about the image. */
  1578. X  cinfo->input_components = NUMCOLORS;
  1579. X  cinfo->in_color_space = CS_RGB;
  1580. X  cinfo->image_width = width;
  1581. X  cinfo->image_height = height;
  1582. X  cinfo->data_precision = 8;    /* always, even if 12-bit JSAMPLEs */
  1583. X}
  1584. X
  1585. X
  1586. X/*
  1587. X * Read one row of pixels.
  1588. X * This version is used for noninterlaced GIF images:
  1589. X * we read directly from the GIF file.
  1590. X */
  1591. X
  1592. XMETHODDEF void
  1593. Xget_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1594. X{
  1595. X  register JSAMPROW ptr0, ptr1, ptr2;
  1596. X  register long col;
  1597. X  register int c;
  1598. X  
  1599. X  ptr0 = pixel_row[0];
  1600. X  ptr1 = pixel_row[1];
  1601. X  ptr2 = pixel_row[2];
  1602. X  for (col = cinfo->image_width; col > 0; col--) {
  1603. X    if ((c = LZWReadByte(cinfo)) < 0)
  1604. X      ERREXIT(cinfo->emethods, "Premature end of GIF image");
  1605. X    *ptr0++ = colormap[CM_RED][c];
  1606. X    *ptr1++ = colormap[CM_GREEN][c];
  1607. X    *ptr2++ = colormap[CM_BLUE][c];
  1608. X  }
  1609. X}
  1610. X
  1611. X
  1612. X/*
  1613. X * Read one row of pixels.
  1614. X * This version is used for the first call on get_input_row when
  1615. X * reading an interlaced GIF file: we read the whole image into memory.
  1616. X */
  1617. X
  1618. XMETHODDEF void
  1619. Xload_interlaced_image (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1620. X{
  1621. X  JSAMPARRAY image_ptr;
  1622. X  register JSAMPROW sptr;
  1623. X  register long col;
  1624. X  register int c;
  1625. X  long row;
  1626. X
  1627. X  /* Read the interlaced image into the big array we've created. */
  1628. X  for (row = 0; row < cinfo->image_height; row++) {
  1629. X    (*cinfo->methods->progress_monitor) (cinfo, row, cinfo->image_height);
  1630. X    image_ptr = (*cinfo->emethods->access_big_sarray)
  1631. X            (interlaced_image, row, TRUE);
  1632. X    sptr = image_ptr[0];
  1633. X    for (col = cinfo->image_width; col > 0; col--) {
  1634. X      if ((c = LZWReadByte(cinfo)) < 0)
  1635. X    ERREXIT(cinfo->emethods, "Premature end of GIF image");
  1636. X      *sptr++ = (JSAMPLE) c;
  1637. X    }
  1638. X  }
  1639. X  cinfo->completed_passes++;
  1640. X
  1641. X  /* Replace method pointer so subsequent calls don't come here. */
  1642. X  cinfo->methods->get_input_row = get_interlaced_row;
  1643. X  /* Initialize for get_interlaced_row, and perform first call on it. */
  1644. X  cur_row_number = 0;
  1645. X  pass2_offset = (cinfo->image_height + 7L) / 8L;
  1646. X  pass3_offset = pass2_offset + (cinfo->image_height + 3L) / 8L;
  1647. X  pass4_offset = pass3_offset + (cinfo->image_height + 1L) / 4L;
  1648. X
  1649. X  get_interlaced_row(cinfo, pixel_row);
  1650. X}
  1651. X
  1652. X
  1653. X/*
  1654. X * Read one row of pixels.
  1655. X * This version is used for interlaced GIF images:
  1656. X * we read from the big in-memory image.
  1657. X */
  1658. X
  1659. XMETHODDEF void
  1660. Xget_interlaced_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1661. X{
  1662. X  JSAMPARRAY image_ptr;
  1663. X  register JSAMPROW sptr, ptr0, ptr1, ptr2;
  1664. X  register long col;
  1665. X  register int c;
  1666. X  long irow;
  1667. X
  1668. X  /* Figure out which row of interlaced image is needed, and access it. */
  1669. X  switch ((int) (cur_row_number & 7L)) {
  1670. X  case 0:            /* first-pass row */
  1671. X    irow = cur_row_number >> 3;
  1672. X    break;
  1673. X  case 4:            /* second-pass row */
  1674. X    irow = (cur_row_number >> 3) + pass2_offset;
  1675. X    break;
  1676. X  case 2:            /* third-pass row */
  1677. X  case 6:
  1678. X    irow = (cur_row_number >> 2) + pass3_offset;
  1679. X    break;
  1680. X  default:            /* fourth-pass row */
  1681. X    irow = (cur_row_number >> 1) + pass4_offset;
  1682. X    break;
  1683. X  }
  1684. X  image_ptr = (*cinfo->emethods->access_big_sarray)
  1685. X            (interlaced_image, irow, FALSE);
  1686. X  /* Scan the row, expand colormap, and output */
  1687. X  sptr = image_ptr[0];
  1688. X  ptr0 = pixel_row[0];
  1689. X  ptr1 = pixel_row[1];
  1690. X  ptr2 = pixel_row[2];
  1691. X  for (col = cinfo->image_width; col > 0; col--) {
  1692. X    c = GETJSAMPLE(*sptr++);
  1693. X    *ptr0++ = colormap[CM_RED][c];
  1694. X    *ptr1++ = colormap[CM_GREEN][c];
  1695. X    *ptr2++ = colormap[CM_BLUE][c];
  1696. X  }
  1697. X  cur_row_number++;        /* for next time */
  1698. X}
  1699. X
  1700. X
  1701. X/*
  1702. X * Finish up at the end of the file.
  1703. X */
  1704. X
  1705. XMETHODDEF void
  1706. Xinput_term (compress_info_ptr cinfo)
  1707. X{
  1708. X  /* no work (we let free_all release the workspace) */
  1709. X}
  1710. X
  1711. X
  1712. X/*
  1713. X * The method selection routine for GIF format input.
  1714. X * Note that this must be called by the user interface before calling
  1715. X * jpeg_compress.  If multiple input formats are supported, the
  1716. X * user interface is responsible for discovering the file format and
  1717. X * calling the appropriate method selection routine.
  1718. X */
  1719. X
  1720. XGLOBAL void
  1721. Xjselrgif (compress_info_ptr cinfo)
  1722. X{
  1723. X  cinfo->methods->input_init = input_init;
  1724. X  cinfo->methods->get_input_row = get_input_row; /* assume uninterlaced */
  1725. X  cinfo->methods->input_term = input_term;
  1726. X}
  1727. X
  1728. X#endif /* GIF_SUPPORTED */
  1729. END_OF_FILE
  1730.   if test 19830 -ne `wc -c <'jrdgif.c'`; then
  1731.     echo shar: \"'jrdgif.c'\" unpacked with wrong size!
  1732.   fi
  1733.   # end of 'jrdgif.c'
  1734. fi
  1735. if test -f 'makljpeg.cf' -a "${1}" != "-c" ; then 
  1736.   echo shar: Will not clobber existing file \"'makljpeg.cf'\"
  1737. else
  1738.   echo shar: Extracting \"'makljpeg.cf'\" \(439 characters\)
  1739.   sed "s/^X//" >'makljpeg.cf' <<'END_OF_FILE'
  1740. Xjcmaster.mix,jcdeflts.mix,jcarith.mix,jccolor.mix,jcexpand.mix,jchuff.mix
  1741. Xjcmcu.mix,jcpipe.mix,jcsample.mix,jfwddct.mix,jwrjfif.mix,jrdgif.mix
  1742. Xjrdppm.mix,jrdrle.mix,jrdtarga.mix,jdmaster.mix,jddeflts.mix,jbsmooth.mix
  1743. Xjdarith.mix,jdcolor.mix,jdhuff.mix,jdmcu.mix,jdpipe.mix,jdsample.mix
  1744. Xjquant1.mix,jquant2.mix,jrevdct.mix,jrdjfif.mix,jwrgif.mix,jwrppm.mix
  1745. Xjwrrle.mix,jwrtarga.mix,jutils.mix,jerror.mix,jmemmgr.mix,jmemsys.mix
  1746. Xjmemdosa.mix
  1747. END_OF_FILE
  1748.   if test 439 -ne `wc -c <'makljpeg.cf'`; then
  1749.     echo shar: \"'makljpeg.cf'\" unpacked with wrong size!
  1750.   fi
  1751.   # end of 'makljpeg.cf'
  1752. fi
  1753. echo shar: End of archive 11 \(of 18\).
  1754. cp /dev/null ark11isdone
  1755. MISSING=""
  1756. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ; do
  1757.     if test ! -f ark${I}isdone ; then
  1758.     MISSING="${MISSING} ${I}"
  1759.     fi
  1760. done
  1761. if test "${MISSING}" = "" ; then
  1762.     echo You have unpacked all 18 archives.
  1763.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1764. else
  1765.     echo You still must unpack the following archives:
  1766.     echo "        " ${MISSING}
  1767. fi
  1768. exit 0
  1769. exit 0 # Just in case...
  1770. exit 0 # Just in case...
  1771.