home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume34 / jpeg / part13 < prev    next >
Encoding:
Text File  |  1992-12-16  |  57.9 KB  |  1,914 lines

  1. Newsgroups: comp.sources.misc
  2. From: jpeg-info@uunet.uu.net (Independent JPEG Group)
  3. Subject:  v34i067:  jpeg - JPEG image compression, Part13/18
  4. Message-ID: <1992Dec17.164956.6658@sparky.imd.sterling.com>
  5. X-Md4-Signature: 394fc2e284504b61aa61a5fff123ef6e
  6. Date: Thu, 17 Dec 1992 16:49:56 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: jpeg-info@uunet.uu.net (Independent JPEG Group)
  10. Posting-number: Volume 34, Issue 67
  11. Archive-name: jpeg/part13
  12. Environment: UNIX, VMS, MS-DOS, Mac, Amiga, Atari, Cray
  13. Supersedes: jpeg: Volume 29, Issue 1-18
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then feed it
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # Contents:  ckconfig.c jdarith.c jdhuff.c jrdtarga.c jwrgif.c
  20. # Wrapped by kent@sparky on Wed Dec 16 20:52:29 1992
  21. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  22. echo If this archive is complete, you will see the following message:
  23. echo '          "shar: End of archive 13 (of 18)."'
  24. if test -f 'ckconfig.c' -a "${1}" != "-c" ; then 
  25.   echo shar: Will not clobber existing file \"'ckconfig.c'\"
  26. else
  27.   echo shar: Extracting \"'ckconfig.c'\" \(13021 characters\)
  28.   sed "s/^X//" >'ckconfig.c' <<'END_OF_FILE'
  29. X/*
  30. X * ckconfig.c
  31. X *
  32. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  33. X * This file is part of the Independent JPEG Group's software.
  34. X * For conditions of distribution and use, see the accompanying README file.
  35. X */
  36. X
  37. X/*
  38. X * This program is intended to help you determine how to configure the JPEG
  39. X * software for installation on a particular system.  The idea is to try to
  40. X * compile and execute this program.  If your compiler fails to compile the
  41. X * program, make changes as indicated in the comments below.  Once you can
  42. X * compile the program, run it, and it will tell you how to set the various
  43. X * switches in jconfig.h and in your Makefile.
  44. X *
  45. X * This could all be done automatically if we could assume we were on a Unix
  46. X * system, but we don't want to assume that, so you'll have to edit and
  47. X * recompile this program until it works.
  48. X *
  49. X * As a general rule, each time you try to compile this program,
  50. X * pay attention only to the *first* error message you get from the compiler.
  51. X * Many C compilers will issue lots of spurious error messages once they
  52. X * have gotten confused.  Go to the line indicated in the first error message,
  53. X * and read the comments preceding that line to see what to change.
  54. X *
  55. X * Almost all of the edits you may need to make to this program consist of
  56. X * changing a line that reads "#define SOME_SYMBOL" to "#undef SOME_SYMBOL",
  57. X * or vice versa.  This is called defining or undefining that symbol.
  58. X */
  59. X
  60. X
  61. X/* First we must see if your system has the include files we need.
  62. X * We start out with the assumption that your system follows the ANSI
  63. X * conventions for include files.  If you get any error in the next dozen
  64. X * lines, undefine INCLUDES_ARE_ANSI.
  65. X */
  66. X
  67. X#define INCLUDES_ARE_ANSI    /* replace 'define' by 'undef' if error here */
  68. X
  69. X#ifdef INCLUDES_ARE_ANSI    /* this will be skipped if you undef... */
  70. X#include <stdio.h>        /* If you ain't got this, you ain't got C. */
  71. X#ifdef __SASC            /* Amiga SAS C provides size_t in stddef.h. */
  72. X#include <stddef.h>        /* (They are wrong...) */
  73. X#endif
  74. X#include <string.h>        /* size_t might be here too. */
  75. Xtypedef size_t my_size_t;    /* The payoff: do we have size_t now? */
  76. X#include <stdlib.h>        /* Check other ANSI includes we use. */
  77. X#endif
  78. X
  79. X
  80. X/* If your system doesn't follow the ANSI conventions, we have to figure out
  81. X * what it does follow.  If you didn't get an error before this line, you can
  82. X * ignore everything down to "#define HAVE_ANSI_DEFINITIONS".
  83. X */
  84. X
  85. X#ifndef INCLUDES_ARE_ANSI    /* skip these tests if INCLUDES_ARE_ANSI */
  86. X
  87. X#include <stdio.h>        /* If you ain't got this, you ain't got C. */
  88. X
  89. X/* jinclude.h will try to include <sys/types.h> if you don't set
  90. X * INCLUDES_ARE_ANSI.  We need to test whether that include file is provided.
  91. X * If you get an error here, undefine HAVE_TYPES_H.
  92. X */
  93. X
  94. X#define HAVE_TYPES_H
  95. X
  96. X#ifdef HAVE_TYPES_H
  97. X#include <sys/types.h>
  98. X#endif
  99. X
  100. X/* We have to see if your string functions are defined by
  101. X * strings.h (BSD convention) or string.h (everybody else).
  102. X * We try the non-BSD convention first; define BSD if the compiler
  103. X * says it can't find string.h.
  104. X */
  105. X
  106. X#undef BSD
  107. X
  108. X#ifdef BSD
  109. X#include <strings.h>
  110. X#else
  111. X#include <string.h>
  112. X#endif
  113. X
  114. X/* Usually size_t is defined in stdio.h, sys/types.h, and/or string.h.
  115. X * If not, you'll get an error on the "typedef size_t my_size_t;" line below.
  116. X * In that case, you'll have to search through your system library to
  117. X * figure out which include file defines "size_t".  Look for a line that
  118. X * says "typedef something-or-other size_t;" (stddef.h and stdlib.h are
  119. X * good places to look first).  Then, change the line below that says
  120. X * "#include <someincludefile.h>" to instead include the file
  121. X * you found size_t in, and define NEED_SPECIAL_INCLUDE.
  122. X */
  123. X
  124. X#undef NEED_SPECIAL_INCLUDE    /* assume we DON'T need it, for starters */
  125. X
  126. X#ifdef NEED_SPECIAL_INCLUDE
  127. X#include <someincludefile.h>
  128. X#endif
  129. X
  130. Xtypedef size_t my_size_t;    /* The payoff: do we have size_t now? */
  131. X
  132. X
  133. X#endif /* INCLUDES_ARE_ANSI */
  134. X
  135. X
  136. X
  137. X/* The next question is whether your compiler supports ANSI-style function
  138. X * definitions.  You need to know this in order to choose between using
  139. X * makefile.ansi and using makefile.unix.
  140. X * The #define line below is set to assume you have ANSI function definitions.
  141. X * If you get an error in this group of lines, undefine HAVE_ANSI_DEFINITIONS.
  142. X */
  143. X
  144. X#define HAVE_ANSI_DEFINITIONS
  145. X
  146. X#ifdef HAVE_ANSI_DEFINITIONS
  147. Xint testfunction (int arg1, int * arg2); /* check prototypes */
  148. X
  149. Xstruct methods_struct {        /* check method-pointer declarations */
  150. X  int (*error_exit) (char *msgtext);
  151. X  int (*trace_message) (char *msgtext);
  152. X  int (*another_method) (void);
  153. X};
  154. X
  155. Xint testfunction (int arg1, int * arg2) /* check definitions */
  156. X{
  157. X  return arg2[arg1];
  158. X}
  159. X
  160. Xint testfunction1 (void)    /* check void arg list */
  161. X{
  162. X  return 0;
  163. X}
  164. X#endif
  165. X
  166. X
  167. X/* Now we want to find out if your compiler knows what "unsigned char" means.
  168. X * If you get an error on the "unsigned char un_char;" line,
  169. X * then undefine HAVE_UNSIGNED_CHAR.
  170. X */
  171. X
  172. X#define HAVE_UNSIGNED_CHAR
  173. X
  174. X#ifdef HAVE_UNSIGNED_CHAR
  175. Xunsigned char un_char;
  176. X#endif
  177. X
  178. X
  179. X/* Now we want to find out if your compiler knows what "unsigned short" means.
  180. X * If you get an error on the "unsigned short un_short;" line,
  181. X * then undefine HAVE_UNSIGNED_SHORT.
  182. X */
  183. X
  184. X#define HAVE_UNSIGNED_SHORT
  185. X
  186. X#ifdef HAVE_UNSIGNED_SHORT
  187. Xunsigned short un_short;
  188. X#endif
  189. X
  190. X
  191. X/* Now we want to find out if your compiler understands type "void".
  192. X * If you get an error anywhere in here, undefine HAVE_VOID.
  193. X */
  194. X
  195. X#define HAVE_VOID
  196. X
  197. X#ifdef HAVE_VOID
  198. Xtypedef void * void_ptr;    /* check void * */
  199. Xtypedef void (*void_func) ();    /* check ptr to function returning void */
  200. X
  201. Xvoid testfunction2 (arg1, arg2)    /* check void function result */
  202. X     void_ptr arg1;
  203. X     void_func arg2;
  204. X{
  205. X  char * locptr = (char *) arg1; /* check casting to and from void * */
  206. X  arg1 = (void *) locptr;
  207. X  (*arg2) (1, 2);        /* check call of fcn returning void */
  208. X}
  209. X#endif
  210. X
  211. X
  212. X/* Now we want to find out if your compiler knows what "const" means.
  213. X * If you get an error here, undefine HAVE_CONST.
  214. X */
  215. X
  216. X#define HAVE_CONST
  217. X
  218. X#ifdef HAVE_CONST
  219. Xstatic const int carray[3] = {1, 2, 3};
  220. X
  221. Xint testfunction3 (arg1)
  222. X     const int arg1;
  223. X{
  224. X  return carray[arg1];
  225. X}
  226. X#endif
  227. X
  228. X
  229. X
  230. X/************************************************************************
  231. X *  OK, that's it.  You should not have to change anything beyond this
  232. X *  point in order to compile and execute this program.  (You might get
  233. X *  some warnings, but you can ignore them.)
  234. X *  When you run the program, it will make a couple more tests that it
  235. X *  can do automatically, and then it will print out a summary of the changes
  236. X *  that you need to make to the makefile and jconfig.h.
  237. X ************************************************************************
  238. X */
  239. X
  240. X
  241. Xstatic int any_changes = 0;
  242. X
  243. Xint new_change ()
  244. X{
  245. X  if (! any_changes) {
  246. X    printf("\nMost of the changes recommended by this program can be made either\n");
  247. X    printf("by editing jconfig.h, or by adding -Dsymbol switches to the CFLAGS\n");
  248. X    printf("line in your Makefile.  (Some PC compilers expect /Dsymbol instead.)\n");
  249. X    printf("The CFLAGS method is simpler, but if your compiler doesn't support -D,\n");
  250. X    printf("then you must change jconfig.h.  Also, it's best to change jconfig.h\n");
  251. X    printf("if you plan to use the JPEG software as a library for other programs.\n");
  252. X    any_changes = 1;
  253. X  }
  254. X  printf("\n");            /* blank line before each problem report */
  255. X  return 0;
  256. X}
  257. X
  258. X
  259. Xint test_char_sign (arg)
  260. X     int arg;
  261. X{
  262. X  if (arg == 189) {        /* expected result for unsigned char */
  263. X    new_change();
  264. X    printf("You should add -DCHAR_IS_UNSIGNED to CFLAGS,\n");
  265. X    printf("or else remove the /* */ comment marks from the line\n");
  266. X    printf("/* #define CHAR_IS_UNSIGNED */  in jconfig.h.\n");
  267. X    printf("(Be sure to delete the space before the # character too.)\n");
  268. X  }
  269. X  else if (arg != -67) {    /* expected result for signed char */
  270. X    new_change();
  271. X    printf("Hmm, it seems 'char' is less than eight bits wide on your machine.\n");
  272. X    printf("I fear the JPEG software will not work at all.\n");
  273. X  }
  274. X  return 0;
  275. X}
  276. X
  277. X
  278. Xint test_shifting (arg)
  279. X     long arg;
  280. X/* See whether right-shift on a long is signed or not. */
  281. X{
  282. X  long res = arg >> 4;
  283. X
  284. X  if (res == 0x80817F4L) {    /* expected result for unsigned */
  285. X    new_change();
  286. X    printf("You must add -DRIGHT_SHIFT_IS_UNSIGNED to CFLAGS,\n");
  287. X    printf("or else remove the /* */ comment marks from the line\n");
  288. X    printf("/* #define RIGHT_SHIFT_IS_UNSIGNED */  in jconfig.h.\n");
  289. X  }
  290. X  else if (res != -0x7F7E80CL) { /* expected result for signed */
  291. X    new_change();
  292. X    printf("Right shift isn't acting as I expect it to.\n");
  293. X    printf("I fear the JPEG software will not work at all.\n");
  294. X  }
  295. X  return 0;
  296. X}
  297. X
  298. X
  299. Xint main (argc, argv)
  300. X     int argc;
  301. X     char ** argv;
  302. X{
  303. X  char signed_char_check = (char) (-67);
  304. X
  305. X  printf("Results of configuration check for Independent JPEG Group's software:\n");
  306. X  printf("\nIf there's not a specific makefile provided for your compiler,\n");
  307. X#ifdef HAVE_ANSI_DEFINITIONS
  308. X  printf("you should use makefile.ansi as the starting point for your Makefile.\n");
  309. X#else
  310. X  printf("you should use makefile.unix as the starting point for your Makefile.\n");
  311. X#endif
  312. X
  313. X  /* Check whether we have all the ANSI features, */
  314. X  /* and whether this agrees with __STDC__ being predefined. */
  315. X#ifdef __STDC__
  316. X#define HAVE_STDC    /* ANSI compilers won't allow redefining __STDC__ */
  317. X#endif
  318. X
  319. X#ifdef HAVE_ANSI_DEFINITIONS
  320. X#ifdef HAVE_UNSIGNED_CHAR
  321. X#ifdef HAVE_UNSIGNED_SHORT
  322. X#ifdef HAVE_CONST
  323. X#define HAVE_ALL_ANSI_FEATURES
  324. X#endif
  325. X#endif
  326. X#endif
  327. X#endif
  328. X
  329. X#ifdef HAVE_ALL_ANSI_FEATURES
  330. X#ifndef HAVE_STDC
  331. X  new_change();
  332. X  printf("Your compiler doesn't claim to be ANSI-compliant, but it is close enough\n");
  333. X  printf("for me.  Either add -DHAVE_STDC to CFLAGS, or add #define HAVE_STDC at the\n");
  334. X  printf("beginning of jconfig.h.\n");
  335. X#define HAVE_STDC
  336. X#endif
  337. X#else /* !HAVE_ALL_ANSI_FEATURES */
  338. X#ifdef HAVE_STDC
  339. X  new_change();
  340. X  printf("Your compiler claims to be ANSI-compliant, but it is lying!\n");
  341. X  printf("Delete the line  #define HAVE_STDC  near the beginning of jconfig.h.\n");
  342. X#undef HAVE_STDC
  343. X#endif
  344. X#endif /* HAVE_ALL_ANSI_FEATURES */
  345. X
  346. X#ifndef HAVE_STDC
  347. X
  348. X#ifdef HAVE_ANSI_DEFINITIONS
  349. X  new_change();
  350. X  printf("You should add -DPROTO to CFLAGS, or else take out the several\n");
  351. X  printf("#ifdef/#else/#endif lines surrounding #define PROTO in jconfig.h.\n");
  352. X  printf("(Leave only one #define PROTO line.)\n");
  353. X#endif
  354. X
  355. X#ifdef HAVE_UNSIGNED_CHAR
  356. X#ifdef HAVE_UNSIGNED_SHORT
  357. X  new_change();
  358. X  printf("You should add -DHAVE_UNSIGNED_CHAR and -DHAVE_UNSIGNED_SHORT\n");
  359. X  printf("to CFLAGS, or else take out the #ifdef HAVE_STDC/#endif lines\n");
  360. X  printf("surrounding #define HAVE_UNSIGNED_CHAR and #define HAVE_UNSIGNED_SHORT\n");
  361. X  printf("in jconfig.h.\n");
  362. X#else /* only unsigned char */
  363. X  new_change();
  364. X  printf("You should add -DHAVE_UNSIGNED_CHAR to CFLAGS,\n");
  365. X  printf("or else move #define HAVE_UNSIGNED_CHAR outside the\n");
  366. X  printf("#ifdef HAVE_STDC/#endif lines surrounding it in jconfig.h.\n");
  367. X#endif
  368. X#else /* !HAVE_UNSIGNED_CHAR */
  369. X#ifdef HAVE_UNSIGNED_SHORT
  370. X  new_change();
  371. X  printf("You should add -DHAVE_UNSIGNED_SHORT to CFLAGS,\n");
  372. X  printf("or else move #define HAVE_UNSIGNED_SHORT outside the\n");
  373. X  printf("#ifdef HAVE_STDC/#endif lines surrounding it in jconfig.h.\n");
  374. X#endif
  375. X#endif /* HAVE_UNSIGNED_CHAR */
  376. X
  377. X#ifdef HAVE_CONST
  378. X  new_change();
  379. X  printf("You should delete the  #define const  line from jconfig.h.\n");
  380. X#endif
  381. X
  382. X#endif /* HAVE_STDC */
  383. X
  384. X  test_char_sign((int) signed_char_check);
  385. X
  386. X  test_shifting(-0x7F7E80B1L);
  387. X
  388. X#ifndef HAVE_VOID
  389. X  new_change();
  390. X  printf("You should add -Dvoid=char to CFLAGS,\n");
  391. X  printf("or else remove the /* */ comment marks from the line\n");
  392. X  printf("/* #define void char */  in jconfig.h.\n");
  393. X  printf("(Be sure to delete the space before the # character too.)\n");
  394. X#endif
  395. X
  396. X#ifdef INCLUDES_ARE_ANSI
  397. X#ifndef __STDC__
  398. X  new_change();
  399. X  printf("You should add -DINCLUDES_ARE_ANSI to CFLAGS, or else add\n");
  400. X  printf("#define INCLUDES_ARE_ANSI at the beginning of jinclude.h (NOT jconfig.h).\n");
  401. X#endif
  402. X#else /* !INCLUDES_ARE_ANSI */
  403. X#ifdef __STDC__
  404. X  new_change();
  405. X  printf("You should add -DNONANSI_INCLUDES to CFLAGS, or else add\n");
  406. X  printf("#define NONANSI_INCLUDES at the beginning of jinclude.h (NOT jconfig.h).\n");
  407. X#endif
  408. X#ifdef NEED_SPECIAL_INCLUDE
  409. X  new_change();
  410. X  printf("In jinclude.h, change the line reading #include <sys/types.h>\n");
  411. X  printf("to instead include the file you found size_t in.\n");
  412. X#else /* !NEED_SPECIAL_INCLUDE */
  413. X#ifndef HAVE_TYPES_H
  414. X  new_change();
  415. X  printf("In jinclude.h, delete the line reading #include <sys/types.h>.\n");
  416. X#endif
  417. X#endif /* NEED_SPECIAL_INCLUDE */
  418. X#ifdef BSD
  419. X  new_change();
  420. X  printf("You should add -DBSD to CFLAGS, or else add\n");
  421. X  printf("#define BSD at the beginning of jinclude.h (NOT jconfig.h).\n");
  422. X#endif
  423. X#endif /* INCLUDES_ARE_ANSI */
  424. X
  425. X  if (any_changes) {
  426. X    printf("\nI think that's everything...\n");
  427. X  } else {
  428. X    printf("\nI think jconfig.h is OK as distributed.\n");
  429. X  }
  430. X
  431. X  return any_changes;
  432. X}
  433. END_OF_FILE
  434.   if test 13021 -ne `wc -c <'ckconfig.c'`; then
  435.     echo shar: \"'ckconfig.c'\" unpacked with wrong size!
  436.   fi
  437.   # end of 'ckconfig.c'
  438. fi
  439. if test -f 'jdarith.c' -a "${1}" != "-c" ; then 
  440.   echo shar: Will not clobber existing file \"'jdarith.c'\"
  441. else
  442.   echo shar: Extracting \"'jdarith.c'\" \(1147 characters\)
  443.   sed "s/^X//" >'jdarith.c' <<'END_OF_FILE'
  444. X/*
  445. X * jdarith.c
  446. X *
  447. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  448. X * This file is part of the Independent JPEG Group's software.
  449. X * For conditions of distribution and use, see the accompanying README file.
  450. X *
  451. X * This file contains arithmetic entropy decoding routines.
  452. X * These routines are invoked via the methods entropy_decode
  453. X * and entropy_decode_init/term.
  454. X */
  455. X
  456. X#include "jinclude.h"
  457. X
  458. X#ifdef D_ARITH_CODING_SUPPORTED
  459. X
  460. X
  461. X/*
  462. X * The arithmetic coding option of the JPEG standard specifies Q-coding,
  463. X * which is covered by patents held by IBM (and possibly AT&T and Mitsubishi).
  464. X * At this time it does not appear to be legal for the Independent JPEG
  465. X * Group to distribute software that implements arithmetic coding.
  466. X * We have therefore removed arithmetic coding support from the
  467. X * distributed source code.
  468. X *
  469. X * We're not happy about it either.
  470. X */
  471. X
  472. X
  473. X/*
  474. X * The method selection routine for arithmetic entropy decoding.
  475. X */
  476. X
  477. XGLOBAL void
  478. Xjseldarithmetic (decompress_info_ptr cinfo)
  479. X{
  480. X  if (cinfo->arith_code) {
  481. X    ERREXIT(cinfo->emethods, "Sorry, there are legal restrictions on arithmetic coding");
  482. X  }
  483. X}
  484. X
  485. X#endif /* D_ARITH_CODING_SUPPORTED */
  486. END_OF_FILE
  487.   if test 1147 -ne `wc -c <'jdarith.c'`; then
  488.     echo shar: \"'jdarith.c'\" unpacked with wrong size!
  489.   fi
  490.   # end of 'jdarith.c'
  491. fi
  492. if test -f 'jdhuff.c' -a "${1}" != "-c" ; then 
  493.   echo shar: Will not clobber existing file \"'jdhuff.c'\"
  494. else
  495.   echo shar: Extracting \"'jdhuff.c'\" \(12497 characters\)
  496.   sed "s/^X//" >'jdhuff.c' <<'END_OF_FILE'
  497. X/*
  498. X * jdhuff.c
  499. X *
  500. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  501. X * This file is part of the Independent JPEG Group's software.
  502. X * For conditions of distribution and use, see the accompanying README file.
  503. X *
  504. X * This file contains Huffman entropy decoding routines.
  505. X * These routines are invoked via the methods entropy_decode
  506. X * and entropy_decode_init/term.
  507. X */
  508. X
  509. X#include "jinclude.h"
  510. X
  511. X
  512. X/* Static variables to avoid passing 'round extra parameters */
  513. X
  514. Xstatic decompress_info_ptr dcinfo;
  515. X
  516. Xstatic INT32 get_buffer;    /* current bit-extraction buffer */
  517. Xstatic int bits_left;        /* # of unused bits in it */
  518. Xstatic boolean printed_eod;    /* flag to suppress multiple end-of-data msgs */
  519. X
  520. XLOCAL void
  521. Xfix_huff_tbl (HUFF_TBL * htbl)
  522. X/* Compute derived values for a Huffman table */
  523. X{
  524. X  int p, i, l, si;
  525. X  char huffsize[257];
  526. X  UINT16 huffcode[257];
  527. X  UINT16 code;
  528. X  
  529. X  /* Figure C.1: make table of Huffman code length for each symbol */
  530. X  /* Note that this is in code-length order. */
  531. X
  532. X  p = 0;
  533. X  for (l = 1; l <= 16; l++) {
  534. X    for (i = 1; i <= (int) htbl->bits[l]; i++)
  535. X      huffsize[p++] = (char) l;
  536. X  }
  537. X  huffsize[p] = 0;
  538. X  
  539. X  /* Figure C.2: generate the codes themselves */
  540. X  /* Note that this is in code-length order. */
  541. X  
  542. X  code = 0;
  543. X  si = huffsize[0];
  544. X  p = 0;
  545. X  while (huffsize[p]) {
  546. X    while (((int) huffsize[p]) == si) {
  547. X      huffcode[p++] = code;
  548. X      code++;
  549. X    }
  550. X    code <<= 1;
  551. X    si++;
  552. X  }
  553. X
  554. X  /* We don't bother to fill in the encoding tables ehufco[] and ehufsi[], */
  555. X  /* since they are not used for decoding. */
  556. X
  557. X  /* Figure F.15: generate decoding tables */
  558. X
  559. X  p = 0;
  560. X  for (l = 1; l <= 16; l++) {
  561. X    if (htbl->bits[l]) {
  562. X      htbl->valptr[l] = p;    /* huffval[] index of 1st sym of code len l */
  563. X      htbl->mincode[l] = huffcode[p]; /* minimum code of length l */
  564. X      p += htbl->bits[l];
  565. X      htbl->maxcode[l] = huffcode[p-1];    /* maximum code of length l */
  566. X    } else {
  567. X      htbl->maxcode[l] = -1;
  568. X    }
  569. X  }
  570. X  htbl->maxcode[17] = 0xFFFFFL;    /* ensures huff_DECODE terminates */
  571. X}
  572. X
  573. X
  574. X/*
  575. X * Code for extracting the next N bits from the input stream.
  576. X * (N never exceeds 15 for JPEG data.)
  577. X * This needs to go as fast as possible!
  578. X *
  579. X * We read source bytes into get_buffer and dole out bits as needed.
  580. X * If get_buffer already contains enough bits, they are fetched in-line
  581. X * by the macros get_bits() and get_bit().  When there aren't enough bits,
  582. X * fill_bit_buffer is called; it will attempt to fill get_buffer to the
  583. X * "high water mark", then extract the desired number of bits.  The idea,
  584. X * of course, is to minimize the function-call overhead cost of entering
  585. X * fill_bit_buffer.
  586. X * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
  587. X * of get_buffer to be used.  (On machines with wider words, an even larger
  588. X * buffer could be used.)  However, on some machines 32-bit shifts are
  589. X * relatively slow and take time proportional to the number of places shifted.
  590. X * (This is true with most PC compilers, for instance.)  In this case it may
  591. X * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
  592. X * average shift distance at the cost of more calls to fill_bit_buffer.
  593. X */
  594. X
  595. X#ifdef SLOW_SHIFT_32
  596. X#define MIN_GET_BITS  15    /* minimum allowable value */
  597. X#else
  598. X#define MIN_GET_BITS  25    /* max value for 32-bit get_buffer */
  599. X#endif
  600. X
  601. Xstatic const int bmask[16] =    /* bmask[n] is mask for n rightmost bits */
  602. X  { 0, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF,
  603. X    0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF };
  604. X
  605. X
  606. XLOCAL int
  607. Xfill_bit_buffer (int nbits)
  608. X/* Load up the bit buffer and do get_bits(nbits) */
  609. X{
  610. X  /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
  611. X  while (bits_left < MIN_GET_BITS) {
  612. X    register int c = JGETC(dcinfo);
  613. X    
  614. X    /* If it's 0xFF, check and discard stuffed zero byte */
  615. X    if (c == 0xFF) {
  616. X      int c2 = JGETC(dcinfo);
  617. X      if (c2 != 0) {
  618. X    /* Oops, it's actually a marker indicating end of compressed data. */
  619. X    /* Better put it back for use later */
  620. X    JUNGETC(c2,dcinfo);
  621. X    JUNGETC(c,dcinfo);
  622. X    /* There should be enough bits still left in the data segment; */
  623. X    /* if so, just break out of the while loop. */
  624. X    if (bits_left >= nbits)
  625. X      break;
  626. X    /* Uh-oh.  Report corrupted data to user and stuff zeroes into
  627. X     * the data stream, so we can produce some kind of image.
  628. X     * Note that this will be repeated for each byte demanded for the
  629. X     * rest of the segment; this is a bit slow but not unreasonably so.
  630. X     * The main thing is to avoid getting a zillion warnings, hence:
  631. X     */
  632. X    if (! printed_eod) {
  633. X      WARNMS(dcinfo->emethods, "Corrupt JPEG data: premature end of data segment");
  634. X      printed_eod = TRUE;
  635. X    }
  636. X    c = 0;            /* insert a zero byte into bit buffer */
  637. X      }
  638. X    }
  639. X
  640. X    /* OK, load c into get_buffer */
  641. X    get_buffer = (get_buffer << 8) | c;
  642. X    bits_left += 8;
  643. X  }
  644. X
  645. X  /* Having filled get_buffer, extract desired bits (this simplifies macros) */
  646. X  bits_left -= nbits;
  647. X  return ((int) (get_buffer >> bits_left)) & bmask[nbits];
  648. X}
  649. X
  650. X
  651. X/* Macros to make things go at some speed! */
  652. X/* NB: parameter to get_bits should be simple variable, not expression */
  653. X
  654. X#define get_bits(nbits) \
  655. X    (bits_left >= (nbits) ? \
  656. X     ((int) (get_buffer >> (bits_left -= (nbits)))) & bmask[nbits] : \
  657. X     fill_bit_buffer(nbits))
  658. X
  659. X#define get_bit() \
  660. X    (bits_left ? \
  661. X     ((int) (get_buffer >> (--bits_left))) & 1 : \
  662. X     fill_bit_buffer(1))
  663. X
  664. X
  665. X/* Figure F.16: extract next coded symbol from input stream */
  666. X  
  667. XINLINE
  668. XLOCAL int
  669. Xhuff_DECODE (HUFF_TBL * htbl)
  670. X{
  671. X  register int l;
  672. X  register INT32 code;
  673. X  
  674. X  code = get_bit();
  675. X  l = 1;
  676. X  while (code > htbl->maxcode[l]) {
  677. X    code = (code << 1) | get_bit();
  678. X    l++;
  679. X  }
  680. X
  681. X  /* With garbage input we may reach the sentinel value l = 17. */
  682. X
  683. X  if (l > 16) {
  684. X    WARNMS(dcinfo->emethods, "Corrupt JPEG data: bad Huffman code");
  685. X    return 0;            /* fake a zero as the safest result */
  686. X  }
  687. X
  688. X  return htbl->huffval[ htbl->valptr[l] + ((int) (code - htbl->mincode[l])) ];
  689. X}
  690. X
  691. X
  692. X/* Figure F.12: extend sign bit */
  693. X
  694. X#define huff_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
  695. X
  696. Xstatic const int extend_test[16] =   /* entry n is 2**(n-1) */
  697. X  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  698. X    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  699. X
  700. Xstatic const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
  701. X  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
  702. X    ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
  703. X    ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
  704. X    ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  705. X
  706. X
  707. X/*
  708. X * Initialize for a Huffman-compressed scan.
  709. X * This is invoked after reading the SOS marker.
  710. X */
  711. X
  712. XMETHODDEF void
  713. Xhuff_decoder_init (decompress_info_ptr cinfo)
  714. X{
  715. X  short ci;
  716. X  jpeg_component_info * compptr;
  717. X
  718. X  /* Initialize static variables */
  719. X  dcinfo = cinfo;
  720. X  bits_left = 0;
  721. X  printed_eod = FALSE;
  722. X
  723. X  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  724. X    compptr = cinfo->cur_comp_info[ci];
  725. X    /* Make sure requested tables are present */
  726. X    if (cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no] == NULL ||
  727. X    cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no] == NULL)
  728. X      ERREXIT(cinfo->emethods, "Use of undefined Huffman table");
  729. X    /* Compute derived values for Huffman tables */
  730. X    /* We may do this more than once for same table, but it's not a big deal */
  731. X    fix_huff_tbl(cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no]);
  732. X    fix_huff_tbl(cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
  733. X    /* Initialize DC predictions to 0 */
  734. X    cinfo->last_dc_val[ci] = 0;
  735. X  }
  736. X
  737. X  /* Initialize restart stuff */
  738. X  cinfo->restarts_to_go = cinfo->restart_interval;
  739. X  cinfo->next_restart_num = 0;
  740. X}
  741. X
  742. X
  743. X/*
  744. X * Check for a restart marker & resynchronize decoder.
  745. X */
  746. X
  747. XLOCAL void
  748. Xprocess_restart (decompress_info_ptr cinfo)
  749. X{
  750. X  int c, nbytes;
  751. X  short ci;
  752. X
  753. X  /* Throw away any unused bits remaining in bit buffer */
  754. X  nbytes = bits_left / 8;    /* count any full bytes loaded into buffer */
  755. X  bits_left = 0;
  756. X  printed_eod = FALSE;        /* next segment can get another warning */
  757. X
  758. X  /* Scan for next JPEG marker */
  759. X  do {
  760. X    do {            /* skip any non-FF bytes */
  761. X      nbytes++;
  762. X      c = JGETC(cinfo);
  763. X    } while (c != 0xFF);
  764. X    do {            /* skip any duplicate FFs */
  765. X      /* we don't increment nbytes here since extra FFs are legal */
  766. X      c = JGETC(cinfo);
  767. X    } while (c == 0xFF);
  768. X  } while (c == 0);        /* repeat if it was a stuffed FF/00 */
  769. X
  770. X  if (nbytes != 1)
  771. X    WARNMS2(cinfo->emethods,
  772. X        "Corrupt JPEG data: %d extraneous bytes before marker 0x%02x",
  773. X        nbytes-1, c);
  774. X
  775. X  if (c != (RST0 + cinfo->next_restart_num)) {
  776. X    /* Uh-oh, the restart markers have been messed up too. */
  777. X    /* Let the file-format module try to figure out how to resync. */
  778. X    (*cinfo->methods->resync_to_restart) (cinfo, c);
  779. X  } else
  780. X    TRACEMS1(cinfo->emethods, 2, "RST%d", cinfo->next_restart_num);
  781. X
  782. X  /* Re-initialize DC predictions to 0 */
  783. X  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  784. X    cinfo->last_dc_val[ci] = 0;
  785. X
  786. X  /* Update restart state */
  787. X  cinfo->restarts_to_go = cinfo->restart_interval;
  788. X  cinfo->next_restart_num = (cinfo->next_restart_num + 1) & 7;
  789. X}
  790. X
  791. X
  792. X/* ZAG[i] is the natural-order position of the i'th element of zigzag order.
  793. X * If the incoming data is corrupted, huff_decode_mcu could attempt to
  794. X * reference values beyond the end of the array.  To avoid a wild store,
  795. X * we put some extra zeroes after the real entries.
  796. X */
  797. X
  798. Xstatic const short ZAG[DCTSIZE2+16] = {
  799. X  0,  1,  8, 16,  9,  2,  3, 10,
  800. X 17, 24, 32, 25, 18, 11,  4,  5,
  801. X 12, 19, 26, 33, 40, 48, 41, 34,
  802. X 27, 20, 13,  6,  7, 14, 21, 28,
  803. X 35, 42, 49, 56, 57, 50, 43, 36,
  804. X 29, 22, 15, 23, 30, 37, 44, 51,
  805. X 58, 59, 52, 45, 38, 31, 39, 46,
  806. X 53, 60, 61, 54, 47, 55, 62, 63,
  807. X  0,  0,  0,  0,  0,  0,  0,  0, /* extra entries in case k>63 below */
  808. X  0,  0,  0,  0,  0,  0,  0,  0
  809. X};
  810. X
  811. X
  812. X/*
  813. X * Decode and return one MCU's worth of Huffman-compressed coefficients.
  814. X * This routine also handles quantization descaling and zigzag reordering
  815. X * of coefficient values.
  816. X *
  817. X * The i'th block of the MCU is stored into the block pointed to by
  818. X * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
  819. X * (Wholesale zeroing is usually a little faster than retail...)
  820. X */
  821. X
  822. XMETHODDEF void
  823. Xhuff_decode_mcu (decompress_info_ptr cinfo, JBLOCKROW *MCU_data)
  824. X{
  825. X  register int s, k, r;
  826. X  short blkn, ci;
  827. X  register JBLOCKROW block;
  828. X  register QUANT_TBL_PTR quanttbl;
  829. X  HUFF_TBL *dctbl;
  830. X  HUFF_TBL *actbl;
  831. X  jpeg_component_info * compptr;
  832. X
  833. X  /* Account for restart interval, process restart marker if needed */
  834. X  if (cinfo->restart_interval) {
  835. X    if (cinfo->restarts_to_go == 0)
  836. X      process_restart(cinfo);
  837. X    cinfo->restarts_to_go--;
  838. X  }
  839. X
  840. X  /* Outer loop handles each block in the MCU */
  841. X
  842. X  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  843. X    block = MCU_data[blkn];
  844. X    ci = cinfo->MCU_membership[blkn];
  845. X    compptr = cinfo->cur_comp_info[ci];
  846. X    quanttbl = cinfo->quant_tbl_ptrs[compptr->quant_tbl_no];
  847. X    actbl = cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no];
  848. X    dctbl = cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no];
  849. X
  850. X    /* Decode a single block's worth of coefficients */
  851. X
  852. X    /* Section F.2.2.1: decode the DC coefficient difference */
  853. X    s = huff_DECODE(dctbl);
  854. X    if (s) {
  855. X      r = get_bits(s);
  856. X      s = huff_EXTEND(r, s);
  857. X    }
  858. X
  859. X    /* Convert DC difference to actual value, update last_dc_val */
  860. X    s += cinfo->last_dc_val[ci];
  861. X    cinfo->last_dc_val[ci] = (JCOEF) s;
  862. X    /* Descale and output the DC coefficient (assumes ZAG[0] = 0) */
  863. X    (*block)[0] = (JCOEF) (((JCOEF) s) * quanttbl[0]);
  864. X    
  865. X    /* Section F.2.2.2: decode the AC coefficients */
  866. X    /* Since zero values are skipped, output area must be zeroed beforehand */
  867. X    for (k = 1; k < DCTSIZE2; k++) {
  868. X      r = huff_DECODE(actbl);
  869. X      
  870. X      s = r & 15;
  871. X      r = r >> 4;
  872. X      
  873. X      if (s) {
  874. X    k += r;
  875. X    r = get_bits(s);
  876. X    s = huff_EXTEND(r, s);
  877. X    /* Descale coefficient and output in natural (dezigzagged) order */
  878. X    (*block)[ZAG[k]] = (JCOEF) (((JCOEF) s) * quanttbl[k]);
  879. X      } else {
  880. X    if (r != 15)
  881. X      break;
  882. X    k += 15;
  883. X      }
  884. X    }
  885. X  }
  886. X}
  887. X
  888. X
  889. X/*
  890. X * Finish up at the end of a Huffman-compressed scan.
  891. X */
  892. X
  893. XMETHODDEF void
  894. Xhuff_decoder_term (decompress_info_ptr cinfo)
  895. X{
  896. X  /* No work needed */
  897. X}
  898. X
  899. X
  900. X/*
  901. X * The method selection routine for Huffman entropy decoding.
  902. X */
  903. X
  904. XGLOBAL void
  905. Xjseldhuffman (decompress_info_ptr cinfo)
  906. X{
  907. X  if (! cinfo->arith_code) {
  908. X    cinfo->methods->entropy_decode_init = huff_decoder_init;
  909. X    cinfo->methods->entropy_decode = huff_decode_mcu;
  910. X    cinfo->methods->entropy_decode_term = huff_decoder_term;
  911. X  }
  912. X}
  913. END_OF_FILE
  914.   if test 12497 -ne `wc -c <'jdhuff.c'`; then
  915.     echo shar: \"'jdhuff.c'\" unpacked with wrong size!
  916.   fi
  917.   # end of 'jdhuff.c'
  918. fi
  919. if test -f 'jrdtarga.c' -a "${1}" != "-c" ; then 
  920.   echo shar: Will not clobber existing file \"'jrdtarga.c'\"
  921. else
  922.   echo shar: Extracting \"'jrdtarga.c'\" \(13431 characters\)
  923.   sed "s/^X//" >'jrdtarga.c' <<'END_OF_FILE'
  924. X/*
  925. X * jrdtarga.c
  926. X *
  927. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  928. X * This file is part of the Independent JPEG Group's software.
  929. X * For conditions of distribution and use, see the accompanying README file.
  930. X *
  931. X * This file contains routines to read input images in Targa format.
  932. X *
  933. X * These routines may need modification for non-Unix environments or
  934. X * specialized applications.  As they stand, they assume input from
  935. X * an ordinary stdio stream.  They further assume that reading begins
  936. X * at the start of the file; input_init may need work if the
  937. X * user interface has already read some data (e.g., to determine that
  938. X * the file is indeed Targa format).
  939. X *
  940. X * These routines are invoked via the methods get_input_row
  941. X * and input_init/term.
  942. X *
  943. X * Based on code contributed by Lee Daniel Crocker.
  944. X */
  945. X
  946. X#include "jinclude.h"
  947. X
  948. X#ifdef TARGA_SUPPORTED
  949. X
  950. X
  951. X/* Macros to deal with unsigned chars as efficiently as compiler allows */
  952. X
  953. X#ifdef HAVE_UNSIGNED_CHAR
  954. Xtypedef unsigned char U_CHAR;
  955. X#define UCH(x)    ((int) (x))
  956. X#else /* !HAVE_UNSIGNED_CHAR */
  957. X#ifdef CHAR_IS_UNSIGNED
  958. Xtypedef char U_CHAR;
  959. X#define UCH(x)    ((int) (x))
  960. X#else
  961. Xtypedef char U_CHAR;
  962. X#define UCH(x)    ((int) (x) & 0xFF)
  963. X#endif
  964. X#endif /* HAVE_UNSIGNED_CHAR */
  965. X
  966. X
  967. X#define    ReadOK(file,buffer,len)    (JFREAD(file,buffer,len) == ((size_t) (len)))
  968. X
  969. X
  970. Xstatic JSAMPARRAY colormap;    /* Targa colormap (converted to my format) */
  971. X
  972. Xstatic big_sarray_ptr whole_image; /* Needed if funny input row order */
  973. Xstatic long current_row;    /* Current logical row number to read */
  974. X
  975. X/* Pointer to routine to extract next Targa pixel from input file */
  976. Xstatic void (*read_pixel) PP((compress_info_ptr cinfo));
  977. X
  978. X/* Result of read_pixel is delivered here: */
  979. Xstatic U_CHAR tga_pixel[4];
  980. X
  981. Xstatic int pixel_size;        /* Bytes per Targa pixel (1 to 4) */
  982. X
  983. X/* State info for reading RLE-coded pixels; both counts must be init to 0 */
  984. Xstatic int block_count;        /* # of pixels remaining in RLE block */
  985. Xstatic int dup_pixel_count;    /* # of times to duplicate previous pixel */
  986. X
  987. X/* This saves the correct pixel-row-expansion method for preload_image */
  988. Xstatic void (*get_pixel_row) PP((compress_info_ptr cinfo,
  989. X                 JSAMPARRAY pixel_row));
  990. X
  991. X
  992. X/* For expanding 5-bit pixel values to 8-bit with best rounding */
  993. X
  994. Xstatic const UINT8 c5to8bits[32] = {
  995. X    0,   8,  16,  24,  32,  41,  49,  57,
  996. X   65,  74,  82,  90,  98, 106, 115, 123,
  997. X  131, 139, 148, 156, 164, 172, 180, 189,
  998. X  197, 205, 213, 222, 230, 238, 246, 255
  999. X};
  1000. X
  1001. X
  1002. X
  1003. XLOCAL int
  1004. Xread_byte (compress_info_ptr cinfo)
  1005. X/* Read next byte from Targa file */
  1006. X{
  1007. X  register FILE *infile = cinfo->input_file;
  1008. X  register int c;
  1009. X
  1010. X  if ((c = getc(infile)) == EOF)
  1011. X    ERREXIT(cinfo->emethods, "Premature EOF in Targa file");
  1012. X  return c;
  1013. X}
  1014. X
  1015. X
  1016. XLOCAL void
  1017. Xread_colormap (compress_info_ptr cinfo, int cmaplen, int mapentrysize)
  1018. X/* Read the colormap from a Targa file */
  1019. X{
  1020. X  int i;
  1021. X
  1022. X  /* Presently only handles 24-bit BGR format */
  1023. X  if (mapentrysize != 24)
  1024. X    ERREXIT(cinfo->emethods, "Unsupported Targa colormap format");
  1025. X
  1026. X  for (i = 0; i < cmaplen; i++) {
  1027. X    colormap[2][i] = (JSAMPLE) read_byte(cinfo);
  1028. X    colormap[1][i] = (JSAMPLE) read_byte(cinfo);
  1029. X    colormap[0][i] = (JSAMPLE) read_byte(cinfo);
  1030. X  }
  1031. X}
  1032. X
  1033. X
  1034. X/*
  1035. X * read_pixel methods: get a single pixel from Targa file into tga_pixel[]
  1036. X */
  1037. X
  1038. XLOCAL void
  1039. Xread_non_rle_pixel (compress_info_ptr cinfo)
  1040. X/* Read one Targa pixel from the input file; no RLE expansion */
  1041. X{
  1042. X  register FILE * infile = cinfo->input_file;
  1043. X  register int i;
  1044. X
  1045. X  for (i = 0; i < pixel_size; i++) {
  1046. X    tga_pixel[i] = (U_CHAR) getc(infile);
  1047. X  }
  1048. X}
  1049. X
  1050. X
  1051. XLOCAL void
  1052. Xread_rle_pixel (compress_info_ptr cinfo)
  1053. X/* Read one Targa pixel from the input file, expanding RLE data as needed */
  1054. X{
  1055. X  register FILE * infile = cinfo->input_file;
  1056. X  register int i;
  1057. X
  1058. X  /* Duplicate previously read pixel? */
  1059. X  if (dup_pixel_count > 0) {
  1060. X    dup_pixel_count--;
  1061. X    return;
  1062. X  }
  1063. X
  1064. X  /* Time to read RLE block header? */
  1065. X  if (--block_count < 0) {    /* decrement pixels remaining in block */
  1066. X    i = read_byte(cinfo);
  1067. X    if (i & 0x80) {        /* Start of duplicate-pixel block? */
  1068. X      dup_pixel_count = i & 0x7F; /* number of duplications after this one */
  1069. X      block_count = 0;        /* then read new block header */
  1070. X    } else {
  1071. X      block_count = i & 0x7F;    /* number of pixels after this one */
  1072. X    }
  1073. X  }
  1074. X
  1075. X  /* Read next pixel */
  1076. X  for (i = 0; i < pixel_size; i++) {
  1077. X    tga_pixel[i] = (U_CHAR) getc(infile);
  1078. X  }
  1079. X}
  1080. X
  1081. X
  1082. X/*
  1083. X * Read one row of pixels.
  1084. X *
  1085. X * We provide several different versions depending on input file format.
  1086. X */
  1087. X
  1088. X
  1089. XMETHODDEF void
  1090. Xget_8bit_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1091. X/* This version is for reading 8-bit grayscale pixels */
  1092. X{
  1093. X  register JSAMPROW ptr0;
  1094. X  register long col;
  1095. X  
  1096. X  ptr0 = pixel_row[0];
  1097. X  for (col = cinfo->image_width; col > 0; col--) {
  1098. X    (*read_pixel) (cinfo);    /* Load next pixel into tga_pixel */
  1099. X    *ptr0++ = (JSAMPLE) UCH(tga_pixel[0]);
  1100. X  }
  1101. X}
  1102. X
  1103. XMETHODDEF void
  1104. Xget_8bit_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1105. X/* This version is for reading 8-bit colormap indexes */
  1106. X{
  1107. X  register int t;
  1108. X  register JSAMPROW ptr0, ptr1, ptr2;
  1109. X  register long col;
  1110. X  
  1111. X  ptr0 = pixel_row[0];
  1112. X  ptr1 = pixel_row[1];
  1113. X  ptr2 = pixel_row[2];
  1114. X  for (col = cinfo->image_width; col > 0; col--) {
  1115. X    (*read_pixel) (cinfo);    /* Load next pixel into tga_pixel */
  1116. X    t = UCH(tga_pixel[0]);
  1117. X    *ptr0++ = colormap[0][t];
  1118. X    *ptr1++ = colormap[1][t];
  1119. X    *ptr2++ = colormap[2][t];
  1120. X  }
  1121. X}
  1122. X
  1123. XMETHODDEF void
  1124. Xget_16bit_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1125. X/* This version is for reading 16-bit pixels */
  1126. X{
  1127. X  register int t;
  1128. X  register JSAMPROW ptr0, ptr1, ptr2;
  1129. X  register long col;
  1130. X  
  1131. X  ptr0 = pixel_row[0];
  1132. X  ptr1 = pixel_row[1];
  1133. X  ptr2 = pixel_row[2];
  1134. X  for (col = cinfo->image_width; col > 0; col--) {
  1135. X    (*read_pixel) (cinfo);    /* Load next pixel into tga_pixel */
  1136. X    t = UCH(tga_pixel[0]);
  1137. X    t += UCH(tga_pixel[1]) << 8;
  1138. X    /* We expand 5 bit data to 8 bit sample width.
  1139. X     * The format of the 16-bit (LSB first) input word is
  1140. X     *     xRRRRRGGGGGBBBBB
  1141. X     */
  1142. X    *ptr2++ = (JSAMPLE) c5to8bits[t & 0x1F];
  1143. X    t >>= 5;
  1144. X    *ptr1++ = (JSAMPLE) c5to8bits[t & 0x1F];
  1145. X    t >>= 5;
  1146. X    *ptr0++ = (JSAMPLE) c5to8bits[t & 0x1F];
  1147. X  }
  1148. X}
  1149. X
  1150. XMETHODDEF void
  1151. Xget_24bit_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1152. X/* This version is for reading 24-bit pixels */
  1153. X{
  1154. X  register JSAMPROW ptr0, ptr1, ptr2;
  1155. X  register long col;
  1156. X  
  1157. X  ptr0 = pixel_row[0];
  1158. X  ptr1 = pixel_row[1];
  1159. X  ptr2 = pixel_row[2];
  1160. X  for (col = cinfo->image_width; col > 0; col--) {
  1161. X    (*read_pixel) (cinfo);    /* Load next pixel into tga_pixel */
  1162. X    *ptr0++ = (JSAMPLE) UCH(tga_pixel[2]); /* convert BGR to RGB order */
  1163. X    *ptr1++ = (JSAMPLE) UCH(tga_pixel[1]);
  1164. X    *ptr2++ = (JSAMPLE) UCH(tga_pixel[0]);
  1165. X  }
  1166. X}
  1167. X
  1168. X/*
  1169. X * Targa also defines a 32-bit pixel format with order B,G,R,A.
  1170. X * We presently ignore the attribute byte, so the code for reading
  1171. X * these pixels is identical to the 24-bit routine above.
  1172. X * This works because the actual pixel length is only known to read_pixel.
  1173. X */
  1174. X
  1175. X#define get_32bit_row  get_24bit_row
  1176. X
  1177. X
  1178. X/*
  1179. X * This method is for re-reading the input data in standard top-down
  1180. X * row order.  The entire image has already been read into whole_image
  1181. X * with proper conversion of pixel format, but it's in a funny row order.
  1182. X */
  1183. X
  1184. XMETHODDEF void
  1185. Xget_memory_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1186. X{
  1187. X  JSAMPARRAY image_ptr;
  1188. X  long source_row;
  1189. X
  1190. X  /* Compute row of source that maps to current_row of normal order */
  1191. X  /* For now, assume image is bottom-up and not interlaced. */
  1192. X  /* NEEDS WORK to support interlaced images! */
  1193. X  source_row = cinfo->image_height - current_row - 1;
  1194. X
  1195. X  /* Fetch that row from virtual array */
  1196. X  image_ptr = (*cinfo->emethods->access_big_sarray)
  1197. X        (whole_image, source_row * cinfo->input_components, FALSE);
  1198. X
  1199. X  jcopy_sample_rows(image_ptr, 0, pixel_row, 0,
  1200. X            cinfo->input_components, cinfo->image_width);
  1201. X
  1202. X  current_row++;
  1203. X}
  1204. X
  1205. X
  1206. X/*
  1207. X * This method loads the image into whole_image during the first call on
  1208. X * get_input_row.  The get_input_row pointer is then adjusted to call
  1209. X * get_memory_row on subsequent calls.
  1210. X */
  1211. X
  1212. XMETHODDEF void
  1213. Xpreload_image (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  1214. X{
  1215. X  JSAMPARRAY image_ptr;
  1216. X  long row;
  1217. X
  1218. X  /* Read the data into a virtual array in input-file row order */
  1219. X  for (row = 0; row < cinfo->image_height; row++) {
  1220. X    (*cinfo->methods->progress_monitor) (cinfo, row, cinfo->image_height);
  1221. X    image_ptr = (*cinfo->emethods->access_big_sarray)
  1222. X            (whole_image, row * cinfo->input_components, TRUE);
  1223. X    (*get_pixel_row) (cinfo, image_ptr);
  1224. X  }
  1225. X  cinfo->completed_passes++;
  1226. X
  1227. X  /* Set up to read from the virtual array in unscrambled order */
  1228. X  cinfo->methods->get_input_row = get_memory_row;
  1229. X  current_row = 0;
  1230. X  /* And read the first row */
  1231. X  get_memory_row(cinfo, pixel_row);
  1232. X}
  1233. X
  1234. X
  1235. X/*
  1236. X * Read the file header; return image size and component count.
  1237. X */
  1238. X
  1239. XMETHODDEF void
  1240. Xinput_init (compress_info_ptr cinfo)
  1241. X{
  1242. X  U_CHAR targaheader[18];
  1243. X  int idlen, cmaptype, subtype, flags, interlace_type, components;
  1244. X  UINT16 width, height, maplen;
  1245. X  boolean is_bottom_up;
  1246. X
  1247. X#define GET_2B(offset)    ((unsigned int) UCH(targaheader[offset]) + \
  1248. X             (((unsigned int) UCH(targaheader[offset+1])) << 8))
  1249. X  
  1250. X  if (! ReadOK(cinfo->input_file, targaheader, 18))
  1251. X    ERREXIT(cinfo->emethods, "Unexpected end of file");
  1252. X
  1253. X  /* Pretend "15-bit" pixels are 16-bit --- we ignore attribute bit anyway */
  1254. X  if (targaheader[16] == 15)
  1255. X    targaheader[16] = 16;
  1256. X
  1257. X  idlen = UCH(targaheader[0]);
  1258. X  cmaptype = UCH(targaheader[1]);
  1259. X  subtype = UCH(targaheader[2]);
  1260. X  maplen = GET_2B(5);
  1261. X  width = GET_2B(12);
  1262. X  height = GET_2B(14);
  1263. X  pixel_size = UCH(targaheader[16]) >> 3;
  1264. X  flags = UCH(targaheader[17]);    /* Image Descriptor byte */
  1265. X
  1266. X  is_bottom_up = ((flags & 0x20) == 0);    /* bit 5 set => top-down */
  1267. X  interlace_type = flags >> 6;    /* bits 6/7 are interlace code */
  1268. X
  1269. X  if (cmaptype > 1 ||        /* cmaptype must be 0 or 1 */
  1270. X      pixel_size < 1 || pixel_size > 4 ||
  1271. X      (UCH(targaheader[16]) & 7) != 0 || /* bits/pixel must be multiple of 8 */
  1272. X      interlace_type != 0)    /* currently don't allow interlaced image */
  1273. X    ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
  1274. X  
  1275. X  if (subtype > 8) {
  1276. X    /* It's an RLE-coded file */
  1277. X    read_pixel = read_rle_pixel;
  1278. X    block_count = dup_pixel_count = 0;
  1279. X    subtype -= 8;
  1280. X  } else {
  1281. X    /* Non-RLE file */
  1282. X    read_pixel = read_non_rle_pixel;
  1283. X  }
  1284. X
  1285. X  /* Now should have subtype 1, 2, or 3 */
  1286. X  components = 3;        /* until proven different */
  1287. X  cinfo->in_color_space = CS_RGB;
  1288. X
  1289. X  switch (subtype) {
  1290. X  case 1:            /* colormapped image */
  1291. X    if (pixel_size == 1 && cmaptype == 1)
  1292. X      get_pixel_row = get_8bit_row;
  1293. X    else
  1294. X      ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
  1295. X    TRACEMS2(cinfo->emethods, 1, "%ux%u colormapped Targa image",
  1296. X         width, height);
  1297. X    break;
  1298. X  case 2:            /* RGB image */
  1299. X    switch (pixel_size) {
  1300. X    case 2:
  1301. X      get_pixel_row = get_16bit_row;
  1302. X      break;
  1303. X    case 3:
  1304. X      get_pixel_row = get_24bit_row;
  1305. X      break;
  1306. X    case 4:
  1307. X      get_pixel_row = get_32bit_row;
  1308. X      break;
  1309. X    default:
  1310. X      ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
  1311. X      break;
  1312. X    }
  1313. X    TRACEMS2(cinfo->emethods, 1, "%ux%u RGB Targa image",
  1314. X         width, height);
  1315. X    break;
  1316. X  case 3:            /* Grayscale image */
  1317. X    components = 1;
  1318. X    cinfo->in_color_space = CS_GRAYSCALE;
  1319. X    if (pixel_size == 1)
  1320. X      get_pixel_row = get_8bit_gray_row;
  1321. X    else
  1322. X      ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
  1323. X    TRACEMS2(cinfo->emethods, 1, "%ux%u grayscale Targa image",
  1324. X         width, height);
  1325. X    break;
  1326. X  default:
  1327. X    ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
  1328. X    break;
  1329. X  }
  1330. X
  1331. X  if (is_bottom_up) {
  1332. X    whole_image = (*cinfo->emethods->request_big_sarray)
  1333. X            ((long) width, (long) height * components,
  1334. X             (long) components);
  1335. X    cinfo->methods->get_input_row = preload_image;
  1336. X    cinfo->total_passes++;    /* count file reading as separate pass */
  1337. X  } else {
  1338. X    whole_image = NULL;
  1339. X    cinfo->methods->get_input_row = get_pixel_row;
  1340. X  }
  1341. X  
  1342. X  while (idlen--)        /* Throw away ID field */
  1343. X    (void) read_byte(cinfo);
  1344. X
  1345. X  if (maplen > 0) {
  1346. X    if (maplen > 256 || GET_2B(3) != 0)
  1347. X      ERREXIT(cinfo->emethods, "Colormap too large");
  1348. X    /* Allocate space to store the colormap */
  1349. X    colormap = (*cinfo->emethods->alloc_small_sarray)
  1350. X            ((long) maplen, 3L);
  1351. X    /* and read it from the file */
  1352. X    read_colormap(cinfo, (int) maplen, UCH(targaheader[7]));
  1353. X  } else {
  1354. X    if (cmaptype)        /* but you promised a cmap! */
  1355. X      ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
  1356. X    colormap = NULL;
  1357. X  }
  1358. X
  1359. X  cinfo->input_components = components;
  1360. X  cinfo->image_width = width;
  1361. X  cinfo->image_height = height;
  1362. X  cinfo->data_precision = 8;    /* always, even if 12-bit JSAMPLEs */
  1363. X}
  1364. X
  1365. X
  1366. X/*
  1367. X * Finish up at the end of the file.
  1368. X */
  1369. X
  1370. XMETHODDEF void
  1371. Xinput_term (compress_info_ptr cinfo)
  1372. X{
  1373. X  /* no work (we let free_all release the workspace) */
  1374. X}
  1375. X
  1376. X
  1377. X/*
  1378. X * The method selection routine for Targa format input.
  1379. X * Note that this must be called by the user interface before calling
  1380. X * jpeg_compress.  If multiple input formats are supported, the
  1381. X * user interface is responsible for discovering the file format and
  1382. X * calling the appropriate method selection routine.
  1383. X */
  1384. X
  1385. XGLOBAL void
  1386. Xjselrtarga (compress_info_ptr cinfo)
  1387. X{
  1388. X  cinfo->methods->input_init = input_init;
  1389. X  /* cinfo->methods->get_input_row is set by input_init */
  1390. X  cinfo->methods->input_term = input_term;
  1391. X}
  1392. X
  1393. X#endif /* TARGA_SUPPORTED */
  1394. END_OF_FILE
  1395.   if test 13431 -ne `wc -c <'jrdtarga.c'`; then
  1396.     echo shar: \"'jrdtarga.c'\" unpacked with wrong size!
  1397.   fi
  1398.   # end of 'jrdtarga.c'
  1399. fi
  1400. if test -f 'jwrgif.c' -a "${1}" != "-c" ; then 
  1401.   echo shar: Will not clobber existing file \"'jwrgif.c'\"
  1402. else
  1403.   echo shar: Extracting \"'jwrgif.c'\" \(14045 characters\)
  1404.   sed "s/^X//" >'jwrgif.c' <<'END_OF_FILE'
  1405. X/*
  1406. X * jwrgif.c
  1407. X *
  1408. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  1409. X * This file is part of the Independent JPEG Group's software.
  1410. X * For conditions of distribution and use, see the accompanying README file.
  1411. X *
  1412. X * This file contains routines to write output images in GIF format.
  1413. X *
  1414. X * These routines may need modification for non-Unix environments or
  1415. X * specialized applications.  As they stand, they assume output to
  1416. X * an ordinary stdio stream.
  1417. X *
  1418. X * These routines are invoked via the methods put_pixel_rows, put_color_map,
  1419. X * and output_init/term.
  1420. X */
  1421. X
  1422. X/*
  1423. X * This code is loosely based on ppmtogif from the PBMPLUS distribution
  1424. X * of Feb. 1991.  That file contains the following copyright notice:
  1425. X *    Based on GIFENCODE by David Rowley <mgardi@watdscu.waterloo.edu>.
  1426. X *    Lempel-Ziv compression based on "compress" by Spencer W. Thomas et al.
  1427. X *    Copyright (C) 1989 by Jef Poskanzer.
  1428. X *    Permission to use, copy, modify, and distribute this software and its
  1429. X *    documentation for any purpose and without fee is hereby granted, provided
  1430. X *    that the above copyright notice appear in all copies and that both that
  1431. X *    copyright notice and this permission notice appear in supporting
  1432. X *    documentation.  This software is provided "as is" without express or
  1433. X *    implied warranty.
  1434. X *
  1435. X * We are also required to state that
  1436. X *    "The Graphics Interchange Format(c) is the Copyright property of
  1437. X *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
  1438. X *    CompuServe Incorporated."
  1439. X */
  1440. X
  1441. X#include "jinclude.h"
  1442. X
  1443. X#ifdef GIF_SUPPORTED
  1444. X
  1445. X
  1446. Xstatic decompress_info_ptr dcinfo; /* to avoid passing to all functions */
  1447. X
  1448. X#define    MAX_LZW_BITS    12    /* maximum LZW code size (4096 symbols) */
  1449. X
  1450. Xtypedef INT16 code_int;        /* must hold -1 .. 2**MAX_LZW_BITS */
  1451. X
  1452. X#define LZW_TABLE_SIZE    ((code_int) 1 << MAX_LZW_BITS)
  1453. X
  1454. X#define HSIZE        5003    /* hash table size for 80% occupancy */
  1455. X
  1456. Xtypedef int hash_int;        /* must hold -2*HSIZE..2*HSIZE */
  1457. X
  1458. Xstatic int n_bits;        /* current number of bits/code */
  1459. Xstatic code_int maxcode;    /* maximum code, given n_bits */
  1460. X#define MAXCODE(n_bits)    (((code_int) 1 << (n_bits)) - 1)
  1461. X
  1462. Xstatic int init_bits;        /* initial n_bits ... restored after clear */
  1463. X
  1464. Xstatic code_int ClearCode;    /* clear code (doesn't change) */
  1465. Xstatic code_int EOFCode;    /* EOF code (ditto) */
  1466. X
  1467. Xstatic code_int free_code;    /* first not-yet-used symbol code */
  1468. X
  1469. X/*
  1470. X * The LZW hash table consists of three parallel arrays:
  1471. X *   hash_code[i]    code of symbol in slot i, or 0 if empty slot
  1472. X *   hash_prefix[i]    symbol's prefix code; undefined if empty slot
  1473. X *   hash_suffix[i]    symbol's suffix character; undefined if empty slot
  1474. X * where slot values (i) range from 0 to HSIZE-1.
  1475. X *
  1476. X * Algorithm:  use open addressing double hashing (no chaining) on the
  1477. X * prefix code / suffix character combination.  We do a variant of Knuth's
  1478. X * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
  1479. X * secondary probe.
  1480. X *
  1481. X * The hash tables are allocated from FAR heap space since they would use up
  1482. X * rather a lot of the near data space in a PC.
  1483. X */
  1484. X
  1485. Xstatic code_int FAR *hash_code;    /* => hash table of symbol codes */
  1486. Xstatic code_int FAR *hash_prefix; /* => hash table of prefix symbols */
  1487. Xstatic UINT8 FAR *hash_suffix;    /* => hash table of suffix bytes */
  1488. X
  1489. X
  1490. X/*
  1491. X * Routines to package compressed data bytes into GIF data blocks.
  1492. X * A data block consists of a count byte (1..255) and that many data bytes.
  1493. X */
  1494. X
  1495. Xstatic int bytesinpkt;        /* # of bytes in current packet */
  1496. Xstatic char packetbuf[256];    /* workspace for accumulating packet */
  1497. X
  1498. X
  1499. XLOCAL void
  1500. Xflush_packet (void)
  1501. X/* flush any accumulated data */
  1502. X{
  1503. X  if (bytesinpkt > 0) {        /* never write zero-length packet */
  1504. X    packetbuf[0] = (char) bytesinpkt++;
  1505. X    if (JFWRITE(dcinfo->output_file, packetbuf, bytesinpkt)
  1506. X    != (size_t) bytesinpkt)
  1507. X      ERREXIT(dcinfo->emethods, "Output file write error");
  1508. X    bytesinpkt = 0;
  1509. X  }
  1510. X}
  1511. X
  1512. X
  1513. X/* Add a character to current packet; flush to disk if necessary */
  1514. X#define CHAR_OUT(c)  \
  1515. X    { packetbuf[++bytesinpkt] = (char) (c);  \
  1516. X        if (bytesinpkt >= 255)  \
  1517. X          flush_packet();  \
  1518. X    }
  1519. X
  1520. X
  1521. X/* Routine to convert variable-width codes into a byte stream */
  1522. X
  1523. Xstatic INT32 cur_accum;        /* holds bits not yet output */
  1524. Xstatic int cur_bits;        /* # of bits in cur_accum */
  1525. X
  1526. X
  1527. XLOCAL void
  1528. Xoutput (code_int code)
  1529. X/* Emit a code of n_bits bits */
  1530. X/* Uses cur_accum and cur_bits to reblock into 8-bit bytes */
  1531. X{
  1532. X  cur_accum |= ((INT32) code) << cur_bits;
  1533. X  cur_bits += n_bits;
  1534. X
  1535. X  while (cur_bits >= 8) {
  1536. X    CHAR_OUT(cur_accum & 0xFF);
  1537. X    cur_accum >>= 8;
  1538. X    cur_bits -= 8;
  1539. X  }
  1540. X
  1541. X  /*
  1542. X   * If the next entry is going to be too big for the code size,
  1543. X   * then increase it, if possible.  We do this here to ensure
  1544. X   * that it's done in sync with the decoder's codesize increases.
  1545. X   */
  1546. X  if (free_code > maxcode) {
  1547. X    n_bits++;
  1548. X    if (n_bits == MAX_LZW_BITS)
  1549. X      maxcode = LZW_TABLE_SIZE;    /* free_code will never exceed this */
  1550. X    else
  1551. X      maxcode = MAXCODE(n_bits);
  1552. X  }
  1553. X}
  1554. X
  1555. X
  1556. X/* The LZW algorithm proper */
  1557. X
  1558. Xstatic code_int waiting_code;    /* symbol not yet output; may be extendable */
  1559. Xstatic boolean first_byte;    /* if TRUE, waiting_code is not valid */
  1560. X
  1561. X
  1562. XLOCAL void
  1563. Xclear_hash (void)
  1564. X/* Fill the hash table with empty entries */
  1565. X{
  1566. X  /* It's sufficient to zero hash_code[] */
  1567. X  jzero_far((void FAR *) hash_code, HSIZE * SIZEOF(code_int));
  1568. X}
  1569. X
  1570. X
  1571. XLOCAL void
  1572. Xclear_block (void)
  1573. X/* Reset compressor and issue a Clear code */
  1574. X{
  1575. X  clear_hash();            /* delete all the symbols */
  1576. X  free_code = ClearCode + 2;
  1577. X  output(ClearCode);        /* inform decoder */
  1578. X  n_bits = init_bits;        /* reset code size */
  1579. X  maxcode = MAXCODE(n_bits);
  1580. X}
  1581. X
  1582. X
  1583. XLOCAL void
  1584. Xcompress_init (int i_bits)
  1585. X/* Initialize LZW compressor */
  1586. X{
  1587. X  /* init all the static variables */
  1588. X  n_bits = init_bits = i_bits;
  1589. X  maxcode = MAXCODE(n_bits);
  1590. X  ClearCode = ((code_int) 1 << (init_bits - 1));
  1591. X  EOFCode = ClearCode + 1;
  1592. X  free_code = ClearCode + 2;
  1593. X  first_byte = TRUE;        /* no waiting symbol yet */
  1594. X  /* init output buffering vars */
  1595. X  bytesinpkt = 0;
  1596. X  cur_accum = 0;
  1597. X  cur_bits = 0;
  1598. X  /* clear hash table */
  1599. X  clear_hash();
  1600. X  /* GIF specifies an initial Clear code */
  1601. X  output(ClearCode);
  1602. X}
  1603. X
  1604. X
  1605. XLOCAL void
  1606. Xcompress_byte (int c)
  1607. X/* Accept and compress one 8-bit byte */
  1608. X{
  1609. X  register hash_int i;
  1610. X  register hash_int disp;
  1611. X
  1612. X  if (first_byte) {        /* need to initialize waiting_code */
  1613. X    waiting_code = c;
  1614. X    first_byte = FALSE;
  1615. X    return;
  1616. X  }
  1617. X
  1618. X  /* Probe hash table to see if a symbol exists for
  1619. X   * waiting_code followed by c.
  1620. X   * If so, replace waiting_code by that symbol and return.
  1621. X   */
  1622. X  i = ((hash_int) c << (MAX_LZW_BITS-8)) + waiting_code;
  1623. X  /* i is less than twice 2**MAX_LZW_BITS, therefore less than twice HSIZE */
  1624. X  if (i >= HSIZE)
  1625. X    i -= HSIZE;
  1626. X  
  1627. X  if (hash_code[i] != 0) {    /* is first probed slot empty? */
  1628. X    if (hash_prefix[i] == waiting_code && hash_suffix[i] == (UINT8) c) {
  1629. X      waiting_code = hash_code[i];
  1630. X      return;
  1631. X    }
  1632. X    if (i == 0)            /* secondary hash (after G. Knott) */
  1633. X      disp = 1;
  1634. X    else
  1635. X      disp = HSIZE - i;
  1636. X    while (1) {
  1637. X      i -= disp;
  1638. X      if (i < 0)
  1639. X    i += HSIZE;
  1640. X      if (hash_code[i] == 0)
  1641. X    break;            /* hit empty slot */
  1642. X      if (hash_prefix[i] == waiting_code && hash_suffix[i] == (UINT8) c) {
  1643. X    waiting_code = hash_code[i];
  1644. X    return;
  1645. X      }
  1646. X    }
  1647. X  }
  1648. X
  1649. X  /* here when hashtable[i] is an empty slot; desired symbol not in table */
  1650. X  output(waiting_code);
  1651. X  if (free_code < LZW_TABLE_SIZE) {
  1652. X    hash_code[i] = free_code++;    /* add symbol to hashtable */
  1653. X    hash_prefix[i] = waiting_code;
  1654. X    hash_suffix[i] = (UINT8) c;
  1655. X  } else
  1656. X    clear_block();
  1657. X  waiting_code = c;
  1658. X}
  1659. X
  1660. X
  1661. XLOCAL void
  1662. Xcompress_term (void)
  1663. X/* Clean up at end */
  1664. X{
  1665. X  /* Flush out the buffered code */
  1666. X  if (! first_byte)
  1667. X    output(waiting_code);
  1668. X  /* Send an EOF code */
  1669. X  output(EOFCode);
  1670. X  /* Flush the bit-packing buffer */
  1671. X  if (cur_bits > 0) {
  1672. X    CHAR_OUT(cur_accum & 0xFF);
  1673. X  }
  1674. X  /* Flush the packet buffer */
  1675. X  flush_packet();
  1676. X}
  1677. X
  1678. X
  1679. X/* GIF header construction */
  1680. X
  1681. X
  1682. XLOCAL void
  1683. Xput_word (UINT16 w)
  1684. X/* Emit a 16-bit word, LSB first */
  1685. X{
  1686. X  putc(w & 0xFF, dcinfo->output_file);
  1687. X  putc((w >> 8) & 0xFF, dcinfo->output_file);
  1688. X}
  1689. X
  1690. X
  1691. XLOCAL void
  1692. Xput_3bytes (int val)
  1693. X/* Emit 3 copies of same byte value --- handy subr for colormap construction */
  1694. X{
  1695. X  putc(val, dcinfo->output_file);
  1696. X  putc(val, dcinfo->output_file);
  1697. X  putc(val, dcinfo->output_file);
  1698. X}
  1699. X
  1700. X
  1701. XLOCAL void
  1702. Xemit_header (int num_colors, JSAMPARRAY colormap)
  1703. X/* Output the GIF file header, including color map */
  1704. X/* If colormap==NULL, synthesize a gray-scale colormap */
  1705. X{
  1706. X  int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte;
  1707. X  int cshift = dcinfo->data_precision - 8;
  1708. X  int i;
  1709. X
  1710. X  if (num_colors > 256)
  1711. X    ERREXIT(dcinfo->emethods, "GIF can only handle 256 colors");
  1712. X  /* Compute bits/pixel and related values */
  1713. X  BitsPerPixel = 1;
  1714. X  while (num_colors > (1 << BitsPerPixel))
  1715. X    BitsPerPixel++;
  1716. X  ColorMapSize = 1 << BitsPerPixel;
  1717. X  if (BitsPerPixel <= 1)
  1718. X    InitCodeSize = 2;
  1719. X  else
  1720. X    InitCodeSize = BitsPerPixel;
  1721. X  /*
  1722. X   * Write the GIF header.
  1723. X   * Note that we generate a plain GIF87 header for maximum compatibility.
  1724. X   */
  1725. X  putc('G', dcinfo->output_file);
  1726. X  putc('I', dcinfo->output_file);
  1727. X  putc('F', dcinfo->output_file);
  1728. X  putc('8', dcinfo->output_file);
  1729. X  putc('7', dcinfo->output_file);
  1730. X  putc('a', dcinfo->output_file);
  1731. X  /* Write the Logical Screen Descriptor */
  1732. X  put_word((UINT16) dcinfo->image_width);
  1733. X  put_word((UINT16) dcinfo->image_height);
  1734. X  FlagByte = 0x80;        /* Yes, there is a global color table */
  1735. X  FlagByte |= (BitsPerPixel-1) << 4; /* color resolution */
  1736. X  FlagByte |= (BitsPerPixel-1);    /* size of global color table */
  1737. X  putc(FlagByte, dcinfo->output_file);
  1738. X  putc(0, dcinfo->output_file);    /* Background color index */
  1739. X  putc(0, dcinfo->output_file);    /* Reserved in GIF87 (aspect ratio in GIF89) */
  1740. X  /* Write the Global Color Map */
  1741. X  /* If the color map is more than 8 bits precision, */
  1742. X  /* we reduce it to 8 bits by shifting */
  1743. X  for (i=0; i < ColorMapSize; i++) {
  1744. X    if (i < num_colors) {
  1745. X      if (colormap != NULL) {
  1746. X    if (dcinfo->out_color_space == CS_RGB) {
  1747. X      /* Normal case: RGB color map */
  1748. X      putc(GETJSAMPLE(colormap[0][i]) >> cshift, dcinfo->output_file);
  1749. X      putc(GETJSAMPLE(colormap[1][i]) >> cshift, dcinfo->output_file);
  1750. X      putc(GETJSAMPLE(colormap[2][i]) >> cshift, dcinfo->output_file);
  1751. X    } else {
  1752. X      /* Grayscale "color map": possible if quantizing grayscale image */
  1753. X      put_3bytes(GETJSAMPLE(colormap[0][i]) >> cshift);
  1754. X    }
  1755. X      } else {
  1756. X    /* Create a gray-scale map of num_colors values, range 0..255 */
  1757. X    put_3bytes((i * 255 + (num_colors-1)/2) / (num_colors-1));
  1758. X      }
  1759. X    } else {
  1760. X      /* fill out the map to a power of 2 */
  1761. X      put_3bytes(0);
  1762. X    }
  1763. X  }
  1764. X  /* Write image separator and Image Descriptor */
  1765. X  putc(',', dcinfo->output_file); /* separator */
  1766. X  put_word((UINT16) 0);        /* left/top offset */
  1767. X  put_word((UINT16) 0);
  1768. X  put_word((UINT16) dcinfo->image_width); /* image size */
  1769. X  put_word((UINT16) dcinfo->image_height);
  1770. X  /* flag byte: not interlaced, no local color map */
  1771. X  putc(0x00, dcinfo->output_file);
  1772. X  /* Write Initial Code Size byte */
  1773. X  putc(InitCodeSize, dcinfo->output_file);
  1774. X
  1775. X  /* Initialize for LZW compression of image data */
  1776. X  compress_init(InitCodeSize+1);
  1777. X}
  1778. X
  1779. X
  1780. X
  1781. X/*
  1782. X * Initialize for GIF output.
  1783. X */
  1784. X
  1785. XMETHODDEF void
  1786. Xoutput_init (decompress_info_ptr cinfo)
  1787. X{
  1788. X  dcinfo = cinfo;        /* save for use by local routines */
  1789. X  if (cinfo->final_out_comps != 1) /* safety check */
  1790. X    ERREXIT(cinfo->emethods, "GIF output got confused");
  1791. X  /* Allocate space for hash table */
  1792. X  hash_code = (code_int FAR *) (*cinfo->emethods->alloc_medium)
  1793. X                (HSIZE * SIZEOF(code_int));
  1794. X  hash_prefix = (code_int FAR *) (*cinfo->emethods->alloc_medium)
  1795. X                (HSIZE * SIZEOF(code_int));
  1796. X  hash_suffix = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
  1797. X                (HSIZE * SIZEOF(UINT8));
  1798. X  /*
  1799. X   * If we aren't quantizing, put_color_map won't be called,
  1800. X   * so emit the header now.  This only happens with gray scale output.
  1801. X   * (If we are quantizing, wait for the color map to be provided.)
  1802. X   */
  1803. X  if (! cinfo->quantize_colors)
  1804. X    emit_header(256, (JSAMPARRAY) NULL);
  1805. X}
  1806. X
  1807. X
  1808. X/*
  1809. X * Write the color map.
  1810. X */
  1811. X
  1812. XMETHODDEF void
  1813. Xput_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  1814. X{
  1815. X  emit_header(num_colors, colormap);
  1816. X}
  1817. X
  1818. X
  1819. X/*
  1820. X * Write some pixel data.
  1821. X */
  1822. X
  1823. XMETHODDEF void
  1824. Xput_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  1825. X        JSAMPIMAGE pixel_data)
  1826. X{
  1827. X  register JSAMPROW ptr;
  1828. X  register long col;
  1829. X  register long width = cinfo->image_width;
  1830. X  register int row;
  1831. X  
  1832. X  for (row = 0; row < num_rows; row++) {
  1833. X    ptr = pixel_data[0][row];
  1834. X    for (col = width; col > 0; col--) {
  1835. X      compress_byte(GETJSAMPLE(*ptr));
  1836. X      ptr++;
  1837. X    }
  1838. X  }
  1839. X}
  1840. X
  1841. X
  1842. X/*
  1843. X * Finish up at the end of the file.
  1844. X */
  1845. X
  1846. XMETHODDEF void
  1847. Xoutput_term (decompress_info_ptr cinfo)
  1848. X{
  1849. X  /* Flush LZW mechanism */
  1850. X  compress_term();
  1851. X  /* Write a zero-length data block to end the series */
  1852. X  putc(0, cinfo->output_file);
  1853. X  /* Write the GIF terminator mark */
  1854. X  putc(';', cinfo->output_file);
  1855. X  /* Make sure we wrote the output file OK */
  1856. X  fflush(cinfo->output_file);
  1857. X  if (ferror(cinfo->output_file))
  1858. X    ERREXIT(cinfo->emethods, "Output file write error");
  1859. X  /* Free space */
  1860. X  /* no work (we let free_all release the workspace) */
  1861. X}
  1862. X
  1863. X
  1864. X/*
  1865. X * The method selection routine for GIF format output.
  1866. X * This should be called from d_ui_method_selection if GIF output is wanted.
  1867. X */
  1868. X
  1869. XGLOBAL void
  1870. Xjselwgif (decompress_info_ptr cinfo)
  1871. X{
  1872. X  cinfo->methods->output_init = output_init;
  1873. X  cinfo->methods->put_color_map = put_color_map;
  1874. X  cinfo->methods->put_pixel_rows = put_pixel_rows;
  1875. X  cinfo->methods->output_term = output_term;
  1876. X
  1877. X  if (cinfo->out_color_space != CS_GRAYSCALE &&
  1878. X      cinfo->out_color_space != CS_RGB)
  1879. X    ERREXIT(cinfo->emethods, "GIF output must be grayscale or RGB");
  1880. X
  1881. X  /* Force quantization if color or if > 8 bits input */
  1882. X  if (cinfo->out_color_space == CS_RGB || cinfo->data_precision > 8) {
  1883. X    /* Force quantization to at most 256 colors */
  1884. X    cinfo->quantize_colors = TRUE;
  1885. X    if (cinfo->desired_number_of_colors > 256)
  1886. X      cinfo->desired_number_of_colors = 256;
  1887. X  }
  1888. X}
  1889. X
  1890. X#endif /* GIF_SUPPORTED */
  1891. END_OF_FILE
  1892.   if test 14045 -ne `wc -c <'jwrgif.c'`; then
  1893.     echo shar: \"'jwrgif.c'\" unpacked with wrong size!
  1894.   fi
  1895.   # end of 'jwrgif.c'
  1896. fi
  1897. echo shar: End of archive 13 \(of 18\).
  1898. cp /dev/null ark13isdone
  1899. MISSING=""
  1900. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ; do
  1901.     if test ! -f ark${I}isdone ; then
  1902.     MISSING="${MISSING} ${I}"
  1903.     fi
  1904. done
  1905. if test "${MISSING}" = "" ; then
  1906.     echo You have unpacked all 18 archives.
  1907.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1908. else
  1909.     echo You still must unpack the following archives:
  1910.     echo "        " ${MISSING}
  1911. fi
  1912. exit 0
  1913. exit 0 # Just in case...
  1914.