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

  1. Newsgroups: comp.sources.misc
  2. From: jpeg-info@uunet.uu.net (Independent JPEG Group)
  3. Subject:  v29i014:  jpeg - JPEG image compression, Part14/18
  4. Message-ID: <1992Mar25.145342.661@sparky.imd.sterling.com>
  5. X-Md4-Signature: ec731e9ff6b64867112d360b27ad0e4e
  6. Date: Wed, 25 Mar 1992 14:53:42 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 14
  11. Archive-name: jpeg/part14
  12. Environment: UNIX, VMS, MS-DOS, Mac, Amiga, Cray
  13.  
  14. #! /bin/sh
  15. # into a shell via "sh file" or similar.  To overwrite existing files,
  16. # type "sh file -c".
  17. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  18. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  19. # Contents:  egetopt.c jdhuff.c jfwddct.c jinclude.h jmemdosa.asm
  20. #   jmemname.c jrevdct.c
  21. # Wrapped by kent@sparky on Mon Mar 23 16:02:53 1992
  22. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  23. echo If this archive is complete, you will see the following message:
  24. echo '          "shar: End of archive 14 (of 18)."'
  25. if test -f 'egetopt.c' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'egetopt.c'\"
  27. else
  28.   echo shar: Extracting \"'egetopt.c'\" \(7226 characters\)
  29.   sed "s/^X//" >'egetopt.c' <<'END_OF_FILE'
  30. X/*
  31. X * egetopt.c -- Extended 'getopt'.
  32. X *
  33. X * A while back, a public-domain version of getopt() was posted to the
  34. X * net.  A bit later, a gentleman by the name of Keith Bostic made some
  35. X * enhancements and reposted it.
  36. X *
  37. X * In recent weeks (i.e., early-to-mid 1988) there's been some
  38. X * heated discussion in comp.lang.c about the merits and drawbacks
  39. X * of getopt(), especially with regard to its handling of '?'.
  40. X *
  41. X * In light of this, I have taken Mr. Bostic's public-domain getopt()
  42. X * and have made some changes that I hope will be considered to be
  43. X * improvements.  I call this routine 'egetopt' ("Extended getopt").
  44. X * The default behavior of this routine is the same as that of getopt(),
  45. X * but it has some optional features that make it more useful.  These
  46. X * options are controlled by the settings of some global variables.
  47. X * By not setting any of these extra global variables, you will have
  48. X * the same functionality as getopt(), which should satisfy those
  49. X * purists who believe getopt() is perfect and can never be improved.
  50. X * If, on the other hand, you are someone who isn't satisfied with the
  51. X * status quo, egetopt() may very well give you the added capabilities
  52. X * you want.
  53. X *
  54. X * Look at the enclosed README file for a description of egetopt()'s
  55. X * new features.
  56. X *
  57. X * The code was originally posted to the net as getopt.c by ...
  58. X *
  59. X *    Keith Bostic
  60. X *    ARPA: keith@seismo 
  61. X *    UUCP: seismo!keith
  62. X *
  63. X * Current version: added enhancements and comments, reformatted code.
  64. X *
  65. X *    Lloyd Zusman
  66. X *    Master Byte Software
  67. X *    Los Gatos, California
  68. X *    Internet:    ljz@fx.com
  69. X *    UUCP:        ...!ames!fxgrp!ljz
  70. X *
  71. X *        May, 1988
  72. X *
  73. X * Modified for use in free JPEG code:
  74. X *
  75. X *    Ed Hanway
  76. X *    UUCP:    uunet!sisd!jeh
  77. X *
  78. X *    October, 1991
  79. X */
  80. X
  81. X/* The original egetopt.c was written not to need stdio.h.
  82. X * For the JPEG code this is an unnecessary and unportable assumption.
  83. X * Also, we make all the variables and routines "static" to avoid
  84. X * possible conflicts with a system-library version of getopt.
  85. X *
  86. X * In the JPEG code, this file is compiled by #including it in jcmain.c
  87. X * or jdmain.c.  Since ANSI2KNR does not process include files, we can't
  88. X * rely on it to convert function definitions to K&R style.  Hence we
  89. X * provide both styles of function header with an explicit #ifdef PROTO (ick).
  90. X */
  91. X
  92. X#define GVAR static        /* make empty to export these variables */
  93. X
  94. X/*
  95. X * None of these constants are referenced in the executable portion of
  96. X * the code ... their sole purpose is to initialize global variables.
  97. X */
  98. X#define BADCH        (int)'?'
  99. X#define NEEDSEP        (int)':'
  100. X#define MAYBESEP    (int)'\0'
  101. X#define EMSG        ""
  102. X#define START        "-"
  103. X
  104. X/*
  105. X * Here are all the pertinent global variables.
  106. X */
  107. XGVAR int opterr = 1;        /* if true, output error message */
  108. XGVAR int optind = 1;        /* index into parent argv vector */
  109. XGVAR int optopt;        /* character checked for validity */
  110. XGVAR int optbad = BADCH;    /* character returned on error */
  111. XGVAR int optchar = 0;        /* character that begins returned option */
  112. XGVAR int optneed = NEEDSEP;    /* flag for mandatory argument */
  113. XGVAR int optmaybe = MAYBESEP;    /* flag for optional argument */
  114. XGVAR const char *optarg;    /* argument associated with option */
  115. XGVAR const char *optstart = START; /* list of characters that start options */
  116. X
  117. X
  118. X/*
  119. X * Macros.
  120. X */
  121. X
  122. X/*
  123. X * Conditionally print out an error message and return (depends on the
  124. X * setting of 'opterr').
  125. X */
  126. X#define TELL(S)    { \
  127. X    if (opterr) \
  128. X        fprintf(stderr, "%s%s%c\n", *nargv, (S), optopt); \
  129. X    return (optbad); \
  130. X}
  131. X
  132. X/*
  133. X * This works similarly to index() and strchr().  I include it so that you
  134. X * don't need to be concerned as to which one your system has.
  135. X */
  136. X
  137. X#ifdef PROTO
  138. XLOCAL const char *
  139. X_sindex (const char *string, int ch)
  140. X#else
  141. XLOCAL const char *
  142. X_sindex (string, ch)
  143. X     const char *string;
  144. X     int ch;
  145. X#endif
  146. X{
  147. X    if (string != NULL) {
  148. X        for (; *string != '\0'; ++string) {
  149. X            if (*string == (char)ch) {
  150. X                return (string);
  151. X            }
  152. X        }
  153. X    }
  154. X
  155. X    return (NULL);
  156. X}
  157. X
  158. X/*
  159. X * Here it is:
  160. X */
  161. X
  162. X#ifdef PROTO
  163. XLOCAL int
  164. Xegetopt (int nargc, char **nargv, const char *ostr)
  165. X#else
  166. XLOCAL int
  167. Xegetopt (nargc, nargv, ostr)
  168. X     int nargc;
  169. X     char **nargv;
  170. X     const char *ostr;
  171. X#endif
  172. X{
  173. X    static const char *place = EMSG; /* option letter processing */
  174. X    register const char *oli;     /* option letter list index */
  175. X    register const char *osi = NULL; /* option start list index */
  176. X
  177. X    if (nargv == (char **)NULL) {
  178. X        return (EOF);
  179. X    }
  180. X
  181. X    if (nargc <= optind || nargv[optind] == NULL) {
  182. X        return (EOF);
  183. X    }
  184. X
  185. X    if (place == NULL) {
  186. X        place = EMSG;
  187. X    }
  188. X
  189. X    /*
  190. X     * Update scanning pointer.
  191. X     */
  192. X    if (*place == '\0') {
  193. X        place = nargv[optind];
  194. X        if (place == NULL) {
  195. X            return (EOF);
  196. X        }
  197. X        osi = _sindex(optstart, *place);
  198. X        if (osi != NULL) {
  199. X            optchar = (int)*osi;
  200. X        }
  201. X        if (optind >= nargc || osi == NULL || *++place == '\0') {
  202. X                return (EOF);
  203. X        }
  204. X
  205. X        /*
  206. X         * Two adjacent, identical flag characters were found.
  207. X         * This takes care of "--", for example.
  208. X         */
  209. X        if (*place == place[-1]) {
  210. X            ++optind;
  211. X            return (EOF);
  212. X        }
  213. X    }
  214. X
  215. X    /*
  216. X     * If the option is a separator or the option isn't in the list,
  217. X     * we've got an error.
  218. X     */
  219. X    optopt = (int)*place++;
  220. X    oli = _sindex(ostr, optopt);
  221. X    if (optopt == optneed || optopt == optmaybe || oli == NULL) {
  222. X        /*
  223. X         * If we're at the end of the current argument, bump the
  224. X         * argument index.
  225. X         */
  226. X        if (*place == '\0') {
  227. X            ++optind;
  228. X        }
  229. X        TELL(": illegal option -- ");    /* byebye */
  230. X    }
  231. X
  232. X    /*
  233. X     * If there is no argument indicator, then we don't even try to
  234. X     * return an argument.
  235. X     */
  236. X    ++oli;
  237. X    if (*oli == '\0' || (*oli != optneed && *oli != optmaybe)) {
  238. X        /*
  239. X         * If we're at the end of the current argument, bump the
  240. X         * argument index.
  241. X         */
  242. X        if (*place == '\0') {
  243. X            ++optind;
  244. X        }
  245. X        optarg = NULL;
  246. X    }
  247. X    /*
  248. X     * If we're here, there's an argument indicator.  It's handled
  249. X     * differently depending on whether it's a mandatory or an
  250. X     * optional argument.
  251. X     */
  252. X    else {
  253. X        /*
  254. X         * If there's no white space, use the rest of the
  255. X         * string as the argument.  In this case, it doesn't
  256. X         * matter if the argument is mandatory or optional.
  257. X         */
  258. X        if (*place != '\0') {
  259. X            optarg = place;
  260. X        }
  261. X        /*
  262. X         * If we're here, there's whitespace after the option.
  263. X         *
  264. X         * Is it a mandatory argument?  If so, return the
  265. X         * next command-line argument if there is one.
  266. X         */
  267. X        else if (*oli == optneed) {
  268. X            /*
  269. X             * If we're at the end of the argument list, there
  270. X             * isn't an argument and hence we have an error.
  271. X             * Otherwise, make 'optarg' point to the argument.
  272. X             */
  273. X            if (nargc <= ++optind) {
  274. X                place = EMSG;
  275. X                TELL(": option requires an argument -- ");
  276. X            }
  277. X            else {
  278. X                optarg = nargv[optind];
  279. X            }
  280. X        }
  281. X        /*
  282. X         * If we're here it must have been an optional argument.
  283. X         */
  284. X        else {
  285. X            if (nargc <= ++optind) {
  286. X                place = EMSG;
  287. X                optarg = NULL;
  288. X            }
  289. X            else {
  290. X                optarg = nargv[optind];
  291. X                if (optarg == NULL) {
  292. X                    place = EMSG;
  293. X                }
  294. X                /*
  295. X                 * If the next item begins with a flag
  296. X                 * character, we treat it like a new
  297. X                 * argument.  This is accomplished by
  298. X                 * decrementing 'optind' and returning
  299. X                 * a null argument.
  300. X                 */
  301. X                else if (_sindex(optstart, *optarg) != NULL) {
  302. X                    --optind;
  303. X                    optarg = NULL;
  304. X                }
  305. X            }
  306. X        }
  307. X        place = EMSG;
  308. X        ++optind;
  309. X    }
  310. X
  311. X    /*
  312. X     * Return option letter.
  313. X     */
  314. X    return (optopt);
  315. X}
  316. END_OF_FILE
  317.   if test 7226 -ne `wc -c <'egetopt.c'`; then
  318.     echo shar: \"'egetopt.c'\" unpacked with wrong size!
  319.   fi
  320.   # end of 'egetopt.c'
  321. fi
  322. if test -f 'jdhuff.c' -a "${1}" != "-c" ; then 
  323.   echo shar: Will not clobber existing file \"'jdhuff.c'\"
  324. else
  325.   echo shar: Extracting \"'jdhuff.c'\" \(7712 characters\)
  326.   sed "s/^X//" >'jdhuff.c' <<'END_OF_FILE'
  327. X/*
  328. X * jdhuff.c
  329. X *
  330. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  331. X * This file is part of the Independent JPEG Group's software.
  332. X * For conditions of distribution and use, see the accompanying README file.
  333. X *
  334. X * This file contains Huffman entropy decoding routines.
  335. X * These routines are invoked via the methods entropy_decode
  336. X * and entropy_decoder_init/term.
  337. X */
  338. X
  339. X#include "jinclude.h"
  340. X
  341. X
  342. X/* Static variables to avoid passing 'round extra parameters */
  343. X
  344. Xstatic decompress_info_ptr dcinfo;
  345. X
  346. Xstatic INT32 get_buffer;    /* current bit-extraction buffer */
  347. Xstatic int bits_left;        /* # of unused bits in it */
  348. X
  349. X
  350. XLOCAL void
  351. Xfix_huff_tbl (HUFF_TBL * htbl)
  352. X/* Compute derived values for a Huffman table */
  353. X{
  354. X  int p, i, l, si;
  355. X  char huffsize[257];
  356. X  UINT16 huffcode[257];
  357. X  UINT16 code;
  358. X  
  359. X  /* Figure C.1: make table of Huffman code length for each symbol */
  360. X  /* Note that this is in code-length order. */
  361. X
  362. X  p = 0;
  363. X  for (l = 1; l <= 16; l++) {
  364. X    for (i = 1; i <= (int) htbl->bits[l]; i++)
  365. X      huffsize[p++] = (char) l;
  366. X  }
  367. X  huffsize[p] = 0;
  368. X  
  369. X  /* Figure C.2: generate the codes themselves */
  370. X  /* Note that this is in code-length order. */
  371. X  
  372. X  code = 0;
  373. X  si = huffsize[0];
  374. X  p = 0;
  375. X  while (huffsize[p]) {
  376. X    while (((int) huffsize[p]) == si) {
  377. X      huffcode[p++] = code;
  378. X      code++;
  379. X    }
  380. X    code <<= 1;
  381. X    si++;
  382. X  }
  383. X
  384. X  /* We don't bother to fill in the encoding tables ehufco[] and ehufsi[], */
  385. X  /* since they are not used for decoding. */
  386. X
  387. X  /* Figure F.15: generate decoding tables */
  388. X
  389. X  p = 0;
  390. X  for (l = 1; l <= 16; l++) {
  391. X    if (htbl->bits[l]) {
  392. X      htbl->valptr[l] = p;    /* huffval[] index of 1st sym of code len l */
  393. X      htbl->mincode[l] = huffcode[p]; /* minimum code of length l */
  394. X      p += htbl->bits[l];
  395. X      htbl->maxcode[l] = huffcode[p-1];    /* maximum code of length l */
  396. X    } else {
  397. X      htbl->maxcode[l] = -1;
  398. X    }
  399. X  }
  400. X  htbl->maxcode[17] = 0xFFFFFL;    /* ensures huff_DECODE terminates */
  401. X}
  402. X
  403. X
  404. X/* Extract the next N bits from the input stream (N <= 15) */
  405. X
  406. XLOCAL int
  407. Xget_bits (int nbits)
  408. X{
  409. X  int result;
  410. X  
  411. X  while (nbits > bits_left) {
  412. X    int c = JGETC(dcinfo);
  413. X    
  414. X    get_buffer <<= 8;
  415. X    get_buffer |= c;
  416. X    bits_left += 8;
  417. X    /* If it's 0xFF, check and discard stuffed zero byte */
  418. X    if (c == 0xff) {
  419. X      c = JGETC(dcinfo);  /* Byte stuffing */
  420. X      if (c != 0)
  421. X    ERREXIT1(dcinfo->emethods,
  422. X         "Unexpected marker 0x%02x in compressed data", c);
  423. X    }
  424. X  }
  425. X  
  426. X  bits_left -= nbits;
  427. X  result = ((int) (get_buffer >> bits_left)) & ((1 << nbits) - 1);
  428. X  return result;
  429. X}
  430. X
  431. X/* Macro to make things go at some speed! */
  432. X
  433. X#define get_bit()    (bits_left ? \
  434. X             ((int) (get_buffer >> (--bits_left))) & 1 : \
  435. X             get_bits(1))
  436. X
  437. X
  438. X/* Figure F.16: extract next coded symbol from input stream */
  439. X  
  440. XLOCAL int
  441. Xhuff_DECODE (HUFF_TBL * htbl)
  442. X{
  443. X  int l, p;
  444. X  INT32 code;
  445. X  
  446. X  code = get_bit();
  447. X  l = 1;
  448. X  while (code > htbl->maxcode[l]) {
  449. X    code = (code << 1) + get_bit();
  450. X    l++;
  451. X  }
  452. X
  453. X  /* With garbage input we may reach the sentinel value l = 17. */
  454. X
  455. X  if (l > 16) {
  456. X    ERREXIT(dcinfo->emethods, "Corrupted data in JPEG file");
  457. X  }
  458. X
  459. X  p = (int) (htbl->valptr[l] + (code - htbl->mincode[l]));
  460. X  
  461. X  return (int) htbl->huffval[p];
  462. X}
  463. X
  464. X
  465. X/* Figure F.12: extend sign bit */
  466. X
  467. X/* NB: on some compilers this will only work for s > 0 */
  468. X
  469. X#define huff_EXTEND(x, s)    ((x) < (1 << ((s)-1)) ? \
  470. X                 (x) + (-1 << (s)) + 1 : \
  471. X                 (x))
  472. X
  473. X
  474. X/* Decode a single block's worth of coefficients */
  475. X/* Note that only the difference is returned for the DC coefficient */
  476. X
  477. XLOCAL void
  478. Xdecode_one_block (JBLOCK block, HUFF_TBL *dctbl, HUFF_TBL *actbl)
  479. X{
  480. X  int s, k, r, n;
  481. X
  482. X  /* zero out the coefficient block */
  483. X
  484. X  MEMZERO((void *) block, SIZEOF(JBLOCK));
  485. X  
  486. X  /* Section F.2.2.1: decode the DC coefficient difference */
  487. X
  488. X  s = huff_DECODE(dctbl);
  489. X  if (s) {
  490. X    r = get_bits(s);
  491. X    s = huff_EXTEND(r, s);
  492. X  }
  493. X  block[0] = s;
  494. X
  495. X  /* Section F.2.2.2: decode the AC coefficients */
  496. X  
  497. X  for (k = 1; k < DCTSIZE2; k++) {
  498. X    r = huff_DECODE(actbl);
  499. X    
  500. X    s = r & 15;
  501. X    n = r >> 4;
  502. X    
  503. X    if (s) {
  504. X      k += n;
  505. X      r = get_bits(s);
  506. X      block[k] = huff_EXTEND(r, s);
  507. X    } else {
  508. X      if (n != 15)
  509. X    break;
  510. X      k += 15;
  511. X    }
  512. X  }
  513. X}
  514. X
  515. X
  516. X/*
  517. X * Initialize for a Huffman-compressed scan.
  518. X * This is invoked after reading the SOS marker.
  519. X */
  520. X
  521. XMETHODDEF void
  522. Xhuff_decoder_init (decompress_info_ptr cinfo)
  523. X{
  524. X  short ci;
  525. X  jpeg_component_info * compptr;
  526. X
  527. X  /* Initialize static variables */
  528. X  dcinfo = cinfo;
  529. X  bits_left = 0;
  530. X
  531. X  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  532. X    compptr = cinfo->cur_comp_info[ci];
  533. X    /* Make sure requested tables are present */
  534. X    if (cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no] == NULL ||
  535. X    cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no] == NULL)
  536. X      ERREXIT(cinfo->emethods, "Use of undefined Huffman table");
  537. X    /* Compute derived values for Huffman tables */
  538. X    /* We may do this more than once for same table, but it's not a big deal */
  539. X    fix_huff_tbl(cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no]);
  540. X    fix_huff_tbl(cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
  541. X    /* Initialize DC predictions to 0 */
  542. X    cinfo->last_dc_val[ci] = 0;
  543. X  }
  544. X
  545. X  /* Initialize restart stuff */
  546. X  cinfo->restarts_to_go = cinfo->restart_interval;
  547. X  cinfo->next_restart_num = 0;
  548. X}
  549. X
  550. X
  551. X/*
  552. X * Check for a restart marker & resynchronize decoder.
  553. X */
  554. X
  555. XLOCAL void
  556. Xprocess_restart (decompress_info_ptr cinfo)
  557. X{
  558. X  int c, nbytes;
  559. X  short ci;
  560. X
  561. X  /* Throw away any partial unread byte */
  562. X  bits_left = 0;
  563. X
  564. X  /* Scan for next JPEG marker */
  565. X  nbytes = 0;
  566. X  do {
  567. X    do {            /* skip any non-FF bytes */
  568. X      nbytes++;
  569. X      c = JGETC(cinfo);
  570. X    } while (c != 0xFF);
  571. X    do {            /* skip any duplicate FFs */
  572. X      nbytes++;
  573. X      c = JGETC(cinfo);
  574. X    } while (c == 0xFF);
  575. X  } while (c == 0);        /* repeat if it was a stuffed FF/00 */
  576. X
  577. X  if (c != (RST0 + cinfo->next_restart_num))
  578. X    ERREXIT2(cinfo->emethods, "Found 0x%02x marker instead of RST%d",
  579. X         c, cinfo->next_restart_num);
  580. X
  581. X  if (nbytes != 2)
  582. X    TRACEMS2(cinfo->emethods, 1, "Skipped %d bytes before RST%d",
  583. X         nbytes-2, cinfo->next_restart_num);
  584. X  else
  585. X    TRACEMS1(cinfo->emethods, 2, "RST%d", cinfo->next_restart_num);
  586. X
  587. X  /* Re-initialize DC predictions to 0 */
  588. X  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  589. X    cinfo->last_dc_val[ci] = 0;
  590. X
  591. X  /* Update restart state */
  592. X  cinfo->restarts_to_go = cinfo->restart_interval;
  593. X  cinfo->next_restart_num++;
  594. X  cinfo->next_restart_num &= 7;
  595. X}
  596. X
  597. X
  598. X/*
  599. X * Decode and return one MCU's worth of Huffman-compressed coefficients.
  600. X */
  601. X
  602. XMETHODDEF void
  603. Xhuff_decode (decompress_info_ptr cinfo, JBLOCK *MCU_data)
  604. X{
  605. X  short blkn, ci;
  606. X  jpeg_component_info * compptr;
  607. X
  608. X  /* Account for restart interval, process restart marker if needed */
  609. X  if (cinfo->restart_interval) {
  610. X    if (cinfo->restarts_to_go == 0)
  611. X      process_restart(cinfo);
  612. X    cinfo->restarts_to_go--;
  613. X  }
  614. X
  615. X  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  616. X    ci = cinfo->MCU_membership[blkn];
  617. X    compptr = cinfo->cur_comp_info[ci];
  618. X    decode_one_block(MCU_data[blkn],
  619. X             cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no],
  620. X             cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
  621. X    /* Convert DC difference to actual value, update last_dc_val */
  622. X    MCU_data[blkn][0] += cinfo->last_dc_val[ci];
  623. X    cinfo->last_dc_val[ci] = MCU_data[blkn][0];
  624. X  }
  625. X}
  626. X
  627. X
  628. X/*
  629. X * Finish up at the end of a Huffman-compressed scan.
  630. X */
  631. X
  632. XMETHODDEF void
  633. Xhuff_decoder_term (decompress_info_ptr cinfo)
  634. X{
  635. X  /* No work needed */
  636. X}
  637. X
  638. X
  639. X/*
  640. X * The method selection routine for Huffman entropy decoding.
  641. X */
  642. X
  643. XGLOBAL void
  644. Xjseldhuffman (decompress_info_ptr cinfo)
  645. X{
  646. X  if (! cinfo->arith_code) {
  647. X    cinfo->methods->entropy_decoder_init = huff_decoder_init;
  648. X    cinfo->methods->entropy_decode = huff_decode;
  649. X    cinfo->methods->entropy_decoder_term = huff_decoder_term;
  650. X  }
  651. X}
  652. END_OF_FILE
  653.   if test 7712 -ne `wc -c <'jdhuff.c'`; then
  654.     echo shar: \"'jdhuff.c'\" unpacked with wrong size!
  655.   fi
  656.   # end of 'jdhuff.c'
  657. fi
  658. if test -f 'jfwddct.c' -a "${1}" != "-c" ; then 
  659.   echo shar: Will not clobber existing file \"'jfwddct.c'\"
  660. else
  661.   echo shar: Extracting \"'jfwddct.c'\" \(7246 characters\)
  662.   sed "s/^X//" >'jfwddct.c' <<'END_OF_FILE'
  663. X/*
  664. X * jfwddct.c
  665. X *
  666. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  667. X * This file is part of the Independent JPEG Group's software.
  668. X * For conditions of distribution and use, see the accompanying README file.
  669. X *
  670. X * This file contains the basic DCT (Discrete Cosine Transform)
  671. X * transformation subroutine.
  672. X *
  673. X * This implementation is based on Appendix A.2 of the book
  674. X * "Discrete Cosine Transform---Algorithms, Advantages, Applications"
  675. X * by K.R. Rao and P. Yip  (Academic Press, Inc, London, 1990).
  676. X * It uses scaled fixed-point arithmetic instead of floating point.
  677. X */
  678. X
  679. X#include "jinclude.h"
  680. X
  681. X/*
  682. X * This routine is specialized to the case DCTSIZE = 8.
  683. X */
  684. X
  685. X#if DCTSIZE != 8
  686. X  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  687. X#endif
  688. X
  689. X
  690. X/* The poop on this scaling stuff is as follows:
  691. X *
  692. X * We have to do addition and subtraction of the integer inputs, which
  693. X * is no problem, and multiplication by fractional constants, which is
  694. X * a problem to do in integer arithmetic.  We multiply all the constants
  695. X * by DCT_SCALE and convert them to integer constants (thus retaining
  696. X * LG2_DCT_SCALE bits of precision in the constants).  After doing a
  697. X * multiplication we have to divide the product by DCT_SCALE, with proper
  698. X * rounding, to produce the correct output.  The division can be implemented
  699. X * cheaply as a right shift of LG2_DCT_SCALE bits.  The DCT equations also
  700. X * specify an additional division by 2 on the final outputs; this can be
  701. X * folded into the right-shift by shifting one more bit (see UNFIXH).
  702. X *
  703. X * If you are planning to recode this in assembler, you might want to set
  704. X * LG2_DCT_SCALE to 15.  This loses a bit of precision, but then all the
  705. X * multiplications are between 16-bit quantities (given 8-bit JSAMPLEs!)
  706. X * so you could use a signed 16x16=>32 bit multiply instruction instead of
  707. X * full 32x32 multiply.  Unfortunately there's no way to describe such a
  708. X * multiply portably in C, so we've gone for the extra bit of accuracy here.
  709. X */
  710. X
  711. X#ifdef EIGHT_BIT_SAMPLES
  712. X#define LG2_DCT_SCALE 16
  713. X#else
  714. X#define LG2_DCT_SCALE 15    /* lose a little precision to avoid overflow */
  715. X#endif
  716. X
  717. X#define ONE    ((INT32) 1)
  718. X
  719. X#define DCT_SCALE (ONE << LG2_DCT_SCALE)
  720. X
  721. X/* In some places we shift the inputs left by a couple more bits, */
  722. X/* so that they can be added to fractional results without too much */
  723. X/* loss of precision. */
  724. X#define LG2_OVERSCALE 2
  725. X#define OVERSCALE  (ONE << LG2_OVERSCALE)
  726. X#define OVERSHIFT(x)  ((x) <<= LG2_OVERSCALE)
  727. X
  728. X/* Scale a fractional constant by DCT_SCALE */
  729. X#define FIX(x)    ((INT32) ((x) * DCT_SCALE + 0.5))
  730. X
  731. X/* Scale a fractional constant by DCT_SCALE/OVERSCALE */
  732. X/* Such a constant can be multiplied with an overscaled input */
  733. X/* to produce something that's scaled by DCT_SCALE */
  734. X#define FIXO(x)  ((INT32) ((x) * DCT_SCALE / OVERSCALE + 0.5))
  735. X
  736. X/* Descale and correctly round a value that's scaled by DCT_SCALE */
  737. X#define UNFIX(x)   RIGHT_SHIFT((x) + (ONE << (LG2_DCT_SCALE-1)), LG2_DCT_SCALE)
  738. X
  739. X/* Same with an additional division by 2, ie, correctly rounded UNFIX(x/2) */
  740. X#define UNFIXH(x)  RIGHT_SHIFT((x) + (ONE << LG2_DCT_SCALE), LG2_DCT_SCALE+1)
  741. X
  742. X/* Take a value scaled by DCT_SCALE and round to integer scaled by OVERSCALE */
  743. X#define UNFIXO(x)  RIGHT_SHIFT((x) + (ONE << (LG2_DCT_SCALE-1-LG2_OVERSCALE)),\
  744. X                   LG2_DCT_SCALE-LG2_OVERSCALE)
  745. X
  746. X/* Here are the constants we need */
  747. X/* SIN_i_j is sine of i*pi/j, scaled by DCT_SCALE */
  748. X/* COS_i_j is cosine of i*pi/j, scaled by DCT_SCALE */
  749. X
  750. X#define SIN_1_4 FIX(0.707106781)
  751. X#define COS_1_4 SIN_1_4
  752. X
  753. X#define SIN_1_8 FIX(0.382683432)
  754. X#define COS_1_8 FIX(0.923879533)
  755. X#define SIN_3_8 COS_1_8
  756. X#define COS_3_8 SIN_1_8
  757. X
  758. X#define SIN_1_16 FIX(0.195090322)
  759. X#define COS_1_16 FIX(0.980785280)
  760. X#define SIN_7_16 COS_1_16
  761. X#define COS_7_16 SIN_1_16
  762. X
  763. X#define SIN_3_16 FIX(0.555570233)
  764. X#define COS_3_16 FIX(0.831469612)
  765. X#define SIN_5_16 COS_3_16
  766. X#define COS_5_16 SIN_3_16
  767. X
  768. X/* OSIN_i_j is sine of i*pi/j, scaled by DCT_SCALE/OVERSCALE */
  769. X/* OCOS_i_j is cosine of i*pi/j, scaled by DCT_SCALE/OVERSCALE */
  770. X
  771. X#define OSIN_1_4 FIXO(0.707106781)
  772. X#define OCOS_1_4 OSIN_1_4
  773. X
  774. X#define OSIN_1_8 FIXO(0.382683432)
  775. X#define OCOS_1_8 FIXO(0.923879533)
  776. X#define OSIN_3_8 OCOS_1_8
  777. X#define OCOS_3_8 OSIN_1_8
  778. X
  779. X#define OSIN_1_16 FIXO(0.195090322)
  780. X#define OCOS_1_16 FIXO(0.980785280)
  781. X#define OSIN_7_16 OCOS_1_16
  782. X#define OCOS_7_16 OSIN_1_16
  783. X
  784. X#define OSIN_3_16 FIXO(0.555570233)
  785. X#define OCOS_3_16 FIXO(0.831469612)
  786. X#define OSIN_5_16 OCOS_3_16
  787. X#define OCOS_5_16 OSIN_3_16
  788. X
  789. X
  790. X/*
  791. X * Perform the forward DCT on one block of samples.
  792. X *
  793. X * A 2-D DCT can be done by 1-D DCT on each row
  794. X * followed by 1-D DCT on each column.
  795. X */
  796. X
  797. XGLOBAL void
  798. Xj_fwd_dct (DCTBLOCK data)
  799. X{
  800. X  int pass, rowctr;
  801. X  register DCTELEM *inptr, *outptr;
  802. X  DCTBLOCK workspace;
  803. X
  804. X  /* Each iteration of the inner loop performs one 8-point 1-D DCT.
  805. X   * It reads from a *row* of the input matrix and stores into a *column*
  806. X   * of the output matrix.  In the first pass, we read from the data[] array
  807. X   * and store into the local workspace[].  In the second pass, we read from
  808. X   * the workspace[] array and store into data[], thus performing the
  809. X   * equivalent of a columnar DCT pass with no variable array indexing.
  810. X   */
  811. X
  812. X  inptr = data;            /* initialize pointers for first pass */
  813. X  outptr = workspace;
  814. X  for (pass = 1; pass >= 0; pass--) {
  815. X    for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
  816. X      /* many tmps have nonoverlapping lifetime -- flashy register colourers
  817. X       * should be able to do this lot very well
  818. X       */
  819. X      INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  820. X      INT32 tmp10, tmp11, tmp12, tmp13;
  821. X      INT32 tmp14, tmp15, tmp16, tmp17;
  822. X      INT32 tmp25, tmp26;
  823. X      SHIFT_TEMPS
  824. X
  825. X      tmp0 = inptr[7] + inptr[0];
  826. X      tmp1 = inptr[6] + inptr[1];
  827. X      tmp2 = inptr[5] + inptr[2];
  828. X      tmp3 = inptr[4] + inptr[3];
  829. X      tmp4 = inptr[3] - inptr[4];
  830. X      tmp5 = inptr[2] - inptr[5];
  831. X      tmp6 = inptr[1] - inptr[6];
  832. X      tmp7 = inptr[0] - inptr[7];
  833. X      
  834. X      tmp10 = tmp3 + tmp0;
  835. X      tmp11 = tmp2 + tmp1;
  836. X      tmp12 = tmp1 - tmp2;
  837. X      tmp13 = tmp0 - tmp3;
  838. X      
  839. X      outptr[        0] = (DCTELEM) UNFIXH((tmp10 + tmp11) * SIN_1_4);
  840. X      outptr[DCTSIZE*4] = (DCTELEM) UNFIXH((tmp10 - tmp11) * COS_1_4);
  841. X      
  842. X      outptr[DCTSIZE*2] = (DCTELEM) UNFIXH(tmp13*COS_1_8 + tmp12*SIN_1_8);
  843. X      outptr[DCTSIZE*6] = (DCTELEM) UNFIXH(tmp13*SIN_1_8 - tmp12*COS_1_8);
  844. X      
  845. X      tmp16 = UNFIXO((tmp6 + tmp5) * SIN_1_4);
  846. X      tmp15 = UNFIXO((tmp6 - tmp5) * COS_1_4);
  847. X      
  848. X      OVERSHIFT(tmp4);
  849. X      OVERSHIFT(tmp7);
  850. X      
  851. X      /* tmp4, tmp7, tmp15, tmp16 are overscaled by OVERSCALE */
  852. X      
  853. X      tmp14 = tmp4 + tmp15;
  854. X      tmp25 = tmp4 - tmp15;
  855. X      tmp26 = tmp7 - tmp16;
  856. X      tmp17 = tmp7 + tmp16;
  857. X      
  858. X      outptr[DCTSIZE  ] = (DCTELEM) UNFIXH(tmp17*OCOS_1_16 + tmp14*OSIN_1_16);
  859. X      outptr[DCTSIZE*7] = (DCTELEM) UNFIXH(tmp17*OCOS_7_16 - tmp14*OSIN_7_16);
  860. X      outptr[DCTSIZE*5] = (DCTELEM) UNFIXH(tmp26*OCOS_5_16 + tmp25*OSIN_5_16);
  861. X      outptr[DCTSIZE*3] = (DCTELEM) UNFIXH(tmp26*OCOS_3_16 - tmp25*OSIN_3_16);
  862. X
  863. X      inptr += DCTSIZE;        /* advance inptr to next row */
  864. X      outptr++;            /* advance outptr to next column */
  865. X    }
  866. X    /* end of pass; in case it was pass 1, set up for pass 2 */
  867. X    inptr = workspace;
  868. X    outptr = data;
  869. X  }
  870. X}
  871. END_OF_FILE
  872.   if test 7246 -ne `wc -c <'jfwddct.c'`; then
  873.     echo shar: \"'jfwddct.c'\" unpacked with wrong size!
  874.   fi
  875.   # end of 'jfwddct.c'
  876. fi
  877. if test -f 'jinclude.h' -a "${1}" != "-c" ; then 
  878.   echo shar: Will not clobber existing file \"'jinclude.h'\"
  879. else
  880.   echo shar: Extracting \"'jinclude.h'\" \(3579 characters\)
  881.   sed "s/^X//" >'jinclude.h' <<'END_OF_FILE'
  882. X/*
  883. X * jinclude.h
  884. X *
  885. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  886. X * This file is part of the Independent JPEG Group's software.
  887. X * For conditions of distribution and use, see the accompanying README file.
  888. X *
  889. X * This is the central file that's #include'd by all the JPEG .c files.
  890. X * Its purpose is to provide a single place to fix any problems with
  891. X * including the wrong system include files.
  892. X * You can edit these declarations if you use a system with nonstandard
  893. X * system include files.
  894. X */
  895. X
  896. X
  897. X/*
  898. X * Normally the __STDC__ macro can be taken as indicating that the system
  899. X * include files conform to the ANSI C standard.  However, if you are running
  900. X * GCC on a machine with non-ANSI system include files, that is not the case.
  901. X * In that case change the following, or add -DNONANSI_INCLUDES to your CFLAGS.
  902. X */
  903. X
  904. X#ifdef __STDC__
  905. X#ifndef NONANSI_INCLUDES
  906. X#define INCLUDES_ARE_ANSI    /* this is what's tested before including */
  907. X#endif
  908. X#endif
  909. X
  910. X/*
  911. X * <stdio.h> is included to get the FILE typedef and NULL macro.
  912. X * Note that the core portable-JPEG files do not actually do any I/O
  913. X * using the stdio library; only the user interface, error handler,
  914. X * and file reading/writing modules invoke any stdio functions.
  915. X * (Well, we did cheat a bit in jmemmgr.c, but only if MEM_STATS is defined.)
  916. X */
  917. X
  918. X#include <stdio.h>
  919. X
  920. X/*
  921. X * We need the size_t typedef, which defines the parameter type of malloc().
  922. X * In an ANSI-conforming implementation this is provided by <stdio.h>,
  923. X * but on non-ANSI systems it's more likely to be in <sys/types.h>.
  924. X * On some not-quite-ANSI systems you may find it in <stddef.h>.
  925. X */
  926. X
  927. X#ifndef INCLUDES_ARE_ANSI    /* shouldn't need this if ANSI C */
  928. X#include <sys/types.h>
  929. X#endif
  930. X#ifdef __SASC            /* Amiga SAS C provides it in stddef.h. */
  931. X#include <stddef.h>
  932. X#endif
  933. X
  934. X/*
  935. X * In ANSI C, and indeed any rational implementation, size_t is also the
  936. X * type returned by sizeof().  However, it seems there are some irrational
  937. X * implementations out there, in which sizeof() returns an int even though
  938. X * size_t is defined as long or unsigned long.  To ensure consistent results
  939. X * we always use this SIZEOF() macro in place of using sizeof() directly.
  940. X */
  941. X
  942. X#undef SIZEOF            /* in case you included X11/xmd.h */
  943. X#define SIZEOF(object)    ((size_t) sizeof(object))
  944. X
  945. X/*
  946. X * fread() and fwrite() are always invoked through these macros.
  947. X * On some systems you may need to twiddle the argument casts.
  948. X * CAUTION: argument order is different from underlying functions!
  949. X */
  950. X
  951. X#define JFREAD(file,buf,sizeofbuf)  \
  952. X  ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
  953. X#define JFWRITE(file,buf,sizeofbuf)  \
  954. X  ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
  955. X
  956. X/*
  957. X * We need the memcpy() and strcmp() functions, plus memory zeroing.
  958. X * ANSI and System V implementations declare these in <string.h>.
  959. X * BSD doesn't have the mem() functions, but it does have bcopy()/bzero().
  960. X * NOTE: we assume the size parameters to these functions are of type size_t.
  961. X * Insert casts in these macros if not!
  962. X */
  963. X
  964. X#ifdef INCLUDES_ARE_ANSI
  965. X#include <string.h>
  966. X#define MEMZERO(voidptr,size)    memset((voidptr), 0, (size))
  967. X#else /* not ANSI */
  968. X#ifdef BSD
  969. X#include <strings.h>
  970. X#define MEMZERO(voidptr,size)    bzero((voidptr), (size))
  971. X#define memcpy(dest,src,size)    bcopy((src), (dest), (size))
  972. X#else /* not BSD, assume Sys V or compatible */
  973. X#include <string.h>
  974. X#define MEMZERO(voidptr,size)    memset((voidptr), 0, (size))
  975. X#endif /* BSD */
  976. X#endif /* ANSI */
  977. X
  978. X
  979. X/* Now include the portable JPEG definition files. */
  980. X
  981. X#include "jconfig.h"
  982. X
  983. X#include "jpegdata.h"
  984. END_OF_FILE
  985.   if test 3579 -ne `wc -c <'jinclude.h'`; then
  986.     echo shar: \"'jinclude.h'\" unpacked with wrong size!
  987.   fi
  988.   # end of 'jinclude.h'
  989. fi
  990. if test -f 'jmemdosa.asm' -a "${1}" != "-c" ; then 
  991.   echo shar: Will not clobber existing file \"'jmemdosa.asm'\"
  992. else
  993.   echo shar: Extracting \"'jmemdosa.asm'\" \(8314 characters\)
  994.   sed "s/^X//" >'jmemdosa.asm' <<'END_OF_FILE'
  995. X;
  996. X; jmemdosa.asm
  997. X;
  998. X; Copyright (C) 1992, Thomas G. Lane.
  999. X; This file is part of the Independent JPEG Group's software.
  1000. X; For conditions of distribution and use, see the accompanying README file.
  1001. X;
  1002. X; This file contains low-level interface routines to support the MS-DOS
  1003. X; backing store manager (jmemdos.c).  Routines are provided to access disk
  1004. X; files through direct DOS calls, and to access XMS and EMS drivers.
  1005. X;
  1006. X; This file should assemble with Microsoft's MASM or any compatible
  1007. X; assembler (including Borland's Turbo Assembler).  If you haven't got
  1008. X; a compatible assembler, better fall back to jmemansi.c or jmemname.c.
  1009. X;
  1010. X; To minimize dependence on the C compiler's register usage conventions,
  1011. X; we save and restore all 8086 registers, even though most compilers only
  1012. X; require SI,DI,DS to be preserved.  Also, we use only 16-bit-wide return
  1013. X; values, which everybody returns in AX.
  1014. X;
  1015. X; Based on code contributed by Ge' Weijers.
  1016. X;
  1017. X
  1018. XJMEMDOSA_TXT    segment byte public 'CODE'
  1019. X
  1020. X        assume    cs:JMEMDOSA_TXT
  1021. X
  1022. X        public    _jdos_open
  1023. X        public    _jdos_close
  1024. X        public    _jdos_seek
  1025. X        public    _jdos_read
  1026. X        public    _jdos_write
  1027. X        public    _jxms_getdriver
  1028. X        public    _jxms_calldriver
  1029. X        public    _jems_available
  1030. X        public    _jems_calldriver
  1031. X
  1032. X;
  1033. X; short far jdos_open (short far * handle, char far * filename)
  1034. X;
  1035. X; Create and open a temporary file
  1036. X;
  1037. X_jdos_open    proc    far
  1038. X        push    bp            ; linkage
  1039. X        mov     bp,sp
  1040. X        push    si            ; save all registers for safety
  1041. X        push    di
  1042. X        push    bx
  1043. X        push    cx
  1044. X        push    dx
  1045. X        push    es
  1046. X        push    ds
  1047. X        mov    cx,0            ; normal file attributes
  1048. X        lds    dx,dword ptr [bp+10]    ; get filename pointer
  1049. X        mov    ah,3ch            ; create file
  1050. X        int    21h
  1051. X        jc    open_err        ; if failed, return error code
  1052. X        lds    bx,dword ptr [bp+6]    ; get handle pointer
  1053. X        mov    word ptr [bx],ax    ; save the handle
  1054. X        xor    ax,ax            ; return zero for OK
  1055. Xopen_err:    pop    ds            ; restore registers and exit
  1056. X        pop    es
  1057. X        pop    dx
  1058. X        pop    cx
  1059. X        pop    bx
  1060. X        pop    di
  1061. X        pop    si
  1062. X        pop     bp
  1063. X        ret
  1064. X_jdos_open    endp
  1065. X
  1066. X
  1067. X;
  1068. X; short far jdos_close (short handle)
  1069. X;
  1070. X; Close the file handle
  1071. X;
  1072. X_jdos_close    proc    far
  1073. X        push    bp            ; linkage
  1074. X        mov     bp,sp
  1075. X        push    si            ; save all registers for safety
  1076. X        push    di
  1077. X        push    bx
  1078. X        push    cx
  1079. X        push    dx
  1080. X        push    es
  1081. X        push    ds
  1082. X        mov    bx,word ptr [bp+6]    ; file handle
  1083. X        mov    ah,3eh            ; close file
  1084. X        int    21h
  1085. X        jc    close_err        ; if failed, return error code
  1086. X        xor    ax,ax            ; return zero for OK
  1087. Xclose_err:    pop    ds            ; restore registers and exit
  1088. X        pop    es
  1089. X        pop    dx
  1090. X        pop    cx
  1091. X        pop    bx
  1092. X        pop    di
  1093. X        pop    si
  1094. X        pop     bp
  1095. X        ret
  1096. X_jdos_close    endp
  1097. X
  1098. X
  1099. X;
  1100. X; short far jdos_seek (short handle, long offset)
  1101. X;
  1102. X; Set file position
  1103. X;
  1104. X_jdos_seek    proc    far
  1105. X        push    bp            ; linkage
  1106. X        mov     bp,sp
  1107. X        push    si            ; save all registers for safety
  1108. X        push    di
  1109. X        push    bx
  1110. X        push    cx
  1111. X        push    dx
  1112. X        push    es
  1113. X        push    ds
  1114. X        mov    bx,word ptr [bp+6]    ; file handle
  1115. X        mov    dx,word ptr [bp+8]    ; LS offset
  1116. X        mov    cx,word ptr [bp+10]    ; MS offset
  1117. X        mov    ax,4200h        ; absolute seek
  1118. X        int    21h
  1119. X        jc    seek_err        ; if failed, return error code
  1120. X        xor    ax,ax            ; return zero for OK
  1121. Xseek_err:    pop    ds            ; restore registers and exit
  1122. X        pop    es
  1123. X        pop    dx
  1124. X        pop    cx
  1125. X        pop    bx
  1126. X        pop    di
  1127. X        pop    si
  1128. X        pop     bp
  1129. X        ret
  1130. X_jdos_seek    endp
  1131. X
  1132. X
  1133. X;
  1134. X; short far jdos_read (short handle, void far * buffer, unsigned short count)
  1135. X;
  1136. X; Read from file
  1137. X;
  1138. X_jdos_read    proc    far
  1139. X        push    bp            ; linkage
  1140. X        mov     bp,sp
  1141. X        push    si            ; save all registers for safety
  1142. X        push    di
  1143. X        push    bx
  1144. X        push    cx
  1145. X        push    dx
  1146. X        push    es
  1147. X        push    ds
  1148. X        mov    bx,word ptr [bp+6]    ; file handle
  1149. X        lds    dx,dword ptr [bp+8]    ; buffer address
  1150. X        mov    cx,word ptr [bp+12]    ; number of bytes
  1151. X        mov    ah,3fh            ; read file
  1152. X        int    21h
  1153. X        jc    read_err        ; if failed, return error code
  1154. X        cmp    ax,word ptr [bp+12]    ; make sure all bytes were read
  1155. X        je    read_ok
  1156. X        mov    ax,1            ; else return 1 for not OK
  1157. X        jmp    short read_err
  1158. Xread_ok:    xor    ax,ax            ; return zero for OK
  1159. Xread_err:    pop    ds            ; restore registers and exit
  1160. X        pop    es
  1161. X        pop    dx
  1162. X        pop    cx
  1163. X        pop    bx
  1164. X        pop    di
  1165. X        pop    si
  1166. X        pop     bp
  1167. X        ret
  1168. X_jdos_read    endp
  1169. X
  1170. X
  1171. X;
  1172. X; short far jdos_write (short handle, void far * buffer, unsigned short count)
  1173. X;
  1174. X; Write to file
  1175. X;
  1176. X_jdos_write    proc    far
  1177. X        push    bp            ; linkage
  1178. X        mov     bp,sp
  1179. X        push    si            ; save all registers for safety
  1180. X        push    di
  1181. X        push    bx
  1182. X        push    cx
  1183. X        push    dx
  1184. X        push    es
  1185. X        push    ds
  1186. X        mov    bx,word ptr [bp+6]    ; file handle
  1187. X        lds    dx,dword ptr [bp+8]    ; buffer address
  1188. X        mov    cx,word ptr [bp+12]    ; number of bytes
  1189. X        mov    ah,40h            ; write file
  1190. X        int    21h
  1191. X        jc    write_err        ; if failed, return error code
  1192. X        cmp    ax,word ptr [bp+12]    ; make sure all bytes written
  1193. X        je    write_ok
  1194. X        mov    ax,1            ; else return 1 for not OK
  1195. X        jmp    short write_err
  1196. Xwrite_ok:    xor    ax,ax            ; return zero for OK
  1197. Xwrite_err:    pop    ds            ; restore registers and exit
  1198. X        pop    es
  1199. X        pop    dx
  1200. X        pop    cx
  1201. X        pop    bx
  1202. X        pop    di
  1203. X        pop    si
  1204. X        pop     bp
  1205. X        ret
  1206. X_jdos_write    endp
  1207. X
  1208. X
  1209. X;
  1210. X; void far jxms_getdriver (XMSDRIVER far *)
  1211. X;
  1212. X; Get the address of the XMS driver, or NULL if not available
  1213. X;
  1214. X_jxms_getdriver    proc    far
  1215. X        push    bp            ; linkage
  1216. X        mov     bp,sp
  1217. X        push    si            ; save all registers for safety
  1218. X        push    di
  1219. X        push    bx
  1220. X        push    cx
  1221. X        push    dx
  1222. X        push    es
  1223. X        push    ds
  1224. X        mov     ax,4300h        ; call multiplex interrupt with
  1225. X        int    2fh            ; a magic cookie, hex 4300
  1226. X        cmp     al,80h            ; AL should contain hex 80
  1227. X        je    xmsavail
  1228. X        xor     dx,dx            ; no XMS driver available
  1229. X        xor     ax,ax            ; return a nil pointer
  1230. X        jmp    short xmsavail_done
  1231. Xxmsavail:    mov     ax,4310h        ; fetch driver address with
  1232. X        int    2fh            ; another magic cookie
  1233. X        mov     dx,es            ; copy address to dx:ax
  1234. X        mov     ax,bx
  1235. Xxmsavail_done:    les     bx,dword ptr [bp+6]    ; get pointer to return value
  1236. X        mov    word ptr es:[bx],ax
  1237. X        mov    word ptr es:[bx+2],dx
  1238. X        pop    ds            ; restore registers and exit
  1239. X        pop    es
  1240. X        pop    dx
  1241. X        pop    cx
  1242. X        pop    bx
  1243. X        pop    di
  1244. X        pop    si
  1245. X        pop    bp
  1246. X        ret
  1247. X_jxms_getdriver    endp
  1248. X
  1249. X
  1250. X;
  1251. X; void far jxms_calldriver (XMSDRIVER, XMScontext far *)
  1252. X;
  1253. X; The XMScontext structure contains values for the AX,DX,BX,SI,DS registers.
  1254. X; These are loaded, the XMS call is performed, and the new values of the
  1255. X; AX,DX,BX registers are written back to the context structure.
  1256. X;
  1257. X_jxms_calldriver     proc    far
  1258. X        push    bp            ; linkage
  1259. X        mov     bp,sp
  1260. X        push    si            ; save all registers for safety
  1261. X        push    di
  1262. X        push    bx
  1263. X        push    cx
  1264. X        push    dx
  1265. X        push    es
  1266. X        push    ds
  1267. X        les     bx,dword ptr [bp+10]    ; get XMScontext pointer
  1268. X        mov     ax,word ptr es:[bx]    ; load registers
  1269. X        mov     dx,word ptr es:[bx+2]
  1270. X        mov     si,word ptr es:[bx+6]
  1271. X        mov     ds,word ptr es:[bx+8]
  1272. X        mov     bx,word ptr es:[bx+4]
  1273. X        call    dword ptr [bp+6]    ; call the driver
  1274. X        mov    cx,bx            ; save returned BX for a sec
  1275. X        les     bx,dword ptr [bp+10]    ; get XMScontext pointer
  1276. X        mov     word ptr es:[bx],ax    ; put back ax,dx,bx
  1277. X        mov     word ptr es:[bx+2],dx
  1278. X        mov     word ptr es:[bx+4],cx
  1279. X        pop    ds            ; restore registers and exit
  1280. X        pop    es
  1281. X        pop    dx
  1282. X        pop    cx
  1283. X        pop    bx
  1284. X        pop    di
  1285. X        pop    si
  1286. X        pop     bp
  1287. X        ret
  1288. X_jxms_calldriver     endp
  1289. X
  1290. X
  1291. X;
  1292. X; short far jems_available (void)
  1293. X;
  1294. X; Have we got an EMS driver? (this comes straight from the EMS 4.0 specs)
  1295. X;
  1296. X_jems_available    proc    far
  1297. X        push    si            ; save all registers for safety
  1298. X        push    di
  1299. X        push    bx
  1300. X        push    cx
  1301. X        push    dx
  1302. X        push    es
  1303. X        push    ds
  1304. X        mov    ax,3567h        ; get interrupt vector 67h
  1305. X        int    21h
  1306. X        push    cs
  1307. X        pop    ds
  1308. X        mov    di,000ah        ; check offs 10 in returned seg
  1309. X        lea    si,ASCII_device_name    ; against literal string
  1310. X        mov    cx,8
  1311. X        cld
  1312. X        repe cmpsb
  1313. X        jne    no_ems
  1314. X        mov    ax,1            ; match, it's there
  1315. X        jmp    short avail_done
  1316. Xno_ems:        xor    ax,ax            ; it's not there
  1317. Xavail_done:    pop    ds            ; restore registers and exit
  1318. X        pop    es
  1319. X        pop    dx
  1320. X        pop    cx
  1321. X        pop    bx
  1322. X        pop    di
  1323. X        pop    si
  1324. X        ret
  1325. X
  1326. XASCII_device_name    db    "EMMXXXX0"
  1327. X
  1328. X_jems_available    endp
  1329. X
  1330. X
  1331. X;
  1332. X; void far jems_calldriver (EMScontext far *)
  1333. X;
  1334. X; The EMScontext structure contains values for the AX,DX,BX,SI,DS registers.
  1335. X; These are loaded, the EMS trap is performed, and the new values of the
  1336. X; AX,DX,BX registers are written back to the context structure.
  1337. X;
  1338. X_jems_calldriver    proc far
  1339. X        push    bp            ; linkage
  1340. X        mov     bp,sp
  1341. X        push    si            ; save all registers for safety
  1342. X        push    di
  1343. X        push    bx
  1344. X        push    cx
  1345. X        push    dx
  1346. X        push    es
  1347. X        push    ds
  1348. X        les     bx,dword ptr [bp+6]    ; get EMScontext pointer
  1349. X        mov     ax,word ptr es:[bx]    ; load registers
  1350. X        mov     dx,word ptr es:[bx+2]
  1351. X        mov     si,word ptr es:[bx+6]
  1352. X        mov     ds,word ptr es:[bx+8]
  1353. X        mov     bx,word ptr es:[bx+4]
  1354. X        int    67h            ; call the EMS driver
  1355. X        mov    cx,bx            ; save returned BX for a sec
  1356. X        les     bx,dword ptr [bp+6]    ; get EMScontext pointer
  1357. X        mov     word ptr es:[bx],ax    ; put back ax,dx,bx
  1358. X        mov     word ptr es:[bx+2],dx
  1359. X        mov     word ptr es:[bx+4],cx
  1360. X        pop    ds            ; restore registers and exit
  1361. X        pop    es
  1362. X        pop    dx
  1363. X        pop    cx
  1364. X        pop    bx
  1365. X        pop    di
  1366. X        pop    si
  1367. X        pop     bp
  1368. X        ret
  1369. X_jems_calldriver    endp
  1370. X
  1371. XJMEMDOSA_TXT    ends
  1372. X
  1373. X        end
  1374. END_OF_FILE
  1375.   if test 8314 -ne `wc -c <'jmemdosa.asm'`; then
  1376.     echo shar: \"'jmemdosa.asm'\" unpacked with wrong size!
  1377.   fi
  1378.   # end of 'jmemdosa.asm'
  1379. fi
  1380. if test -f 'jmemname.c' -a "${1}" != "-c" ; then 
  1381.   echo shar: Will not clobber existing file \"'jmemname.c'\"
  1382. else
  1383.   echo shar: Extracting \"'jmemname.c'\" \(7643 characters\)
  1384.   sed "s/^X//" >'jmemname.c' <<'END_OF_FILE'
  1385. X/*
  1386. X * jmemname.c  (jmemsys.c)
  1387. X *
  1388. X * Copyright (C) 1992, Thomas G. Lane.
  1389. X * This file is part of the Independent JPEG Group's software.
  1390. X * For conditions of distribution and use, see the accompanying README file.
  1391. X *
  1392. X * This file provides a generic implementation of the system-dependent
  1393. X * portion of the JPEG memory manager.  This implementation assumes that
  1394. X * you must explicitly construct a name for each temp file.
  1395. X * Also, the problem of determining the amount of memory available
  1396. X * is shoved onto the user.
  1397. X */
  1398. X
  1399. X#include "jinclude.h"
  1400. X#include "jmemsys.h"
  1401. X
  1402. X#ifdef INCLUDES_ARE_ANSI
  1403. X#include <stdlib.h>        /* to declare malloc(), free() */
  1404. X#else
  1405. Xextern void * malloc PP((size_t size));
  1406. Xextern void free PP((void *ptr));
  1407. X#endif
  1408. X
  1409. X#ifndef SEEK_SET        /* pre-ANSI systems may not define this; */
  1410. X#define SEEK_SET  0        /* if not, assume 0 is correct */
  1411. X#endif
  1412. X
  1413. X#ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  1414. X#define READ_BINARY    "r"
  1415. X#define RW_BINARY    "w+"
  1416. X#else
  1417. X#define READ_BINARY    "rb"
  1418. X#define RW_BINARY    "w+b"
  1419. X#endif
  1420. X
  1421. X
  1422. Xstatic external_methods_ptr methods; /* saved for access to error_exit */
  1423. X
  1424. Xstatic long total_used;        /* total memory requested so far */
  1425. X
  1426. X
  1427. X/*
  1428. X * Selection of a file name for a temporary file.
  1429. X * This is system-dependent!
  1430. X *
  1431. X * The code as given is suitable for most Unix systems, and it is easily
  1432. X * modified for most non-Unix systems.  Some notes:
  1433. X *  1.  The temp file is created in the directory named by TEMP_DIRECTORY.
  1434. X *      The default value is /usr/tmp, which is the conventional place for
  1435. X *      creating large temp files on Unix.  On other systems you'll probably
  1436. X *      want to change the file location.  You can do this by editing the
  1437. X *      #define, or by defining TEMP_DIRECTORY in CFLAGS in the Makefile.
  1438. X *      For example, you might say
  1439. X *          CFLAGS= ... '-DTEMP_DIRECTORY="/tmp/"'
  1440. X *      Note that double quotes are needed in the text of the macro.
  1441. X *      With most make systems you have to put single quotes around the
  1442. X *      -D construct to preserve the double quotes.
  1443. X *    (Amiga SAS C has trouble with ":" and such in command-line options,
  1444. X *    so we've put in a special case for the preferred Amiga temp directory.)
  1445. X *
  1446. X *  2.  If you need to change the file name as well as its location,
  1447. X *      you can override the TEMP_FILE_NAME macro.  (Note that this is
  1448. X *      actually a printf format string; it must contain %s and %d.)
  1449. X *      Few people should need to do this.
  1450. X *
  1451. X *  3.  mktemp() is used to ensure that multiple processes running
  1452. X *      simultaneously won't select the same file names.  If your system
  1453. X *      doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
  1454. X *
  1455. X *  4.  You probably want to define NEED_SIGNAL_CATCHER so that jcmain/jdmain
  1456. X *      will cause the temp files to be removed if you stop the program early.
  1457. X */
  1458. X
  1459. X#ifndef TEMP_DIRECTORY        /* so can override from Makefile */
  1460. X#ifdef AMIGA
  1461. X#define TEMP_DIRECTORY  "JPEGTMP:"  /* recommended setting for Amiga */
  1462. X#else
  1463. X#define TEMP_DIRECTORY  "/usr/tmp/" /* recommended setting for Unix */
  1464. X#endif
  1465. X#endif
  1466. X
  1467. Xstatic int next_file_num;    /* to distinguish among several temp files */
  1468. X
  1469. X#ifdef NO_MKTEMP
  1470. X
  1471. X#ifndef TEMP_FILE_NAME        /* so can override from Makefile */
  1472. X#define TEMP_FILE_NAME  "%sJPG%03d.TMP"
  1473. X#endif
  1474. X
  1475. XLOCAL void
  1476. Xselect_file_name (char * fname)
  1477. X{
  1478. X  FILE * tfile;
  1479. X
  1480. X  /* Keep generating file names till we find one that's not in use */
  1481. X  for (;;) {
  1482. X    next_file_num++;        /* advance counter */
  1483. X    sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
  1484. X    if ((tfile = fopen(fname, READ_BINARY)) == NULL)
  1485. X      break;
  1486. X    fclose(tfile);        /* oops, it's there; close tfile & try again */
  1487. X  }
  1488. X}
  1489. X
  1490. X#else /* ! NO_MKTEMP */
  1491. X
  1492. X/* Note that mktemp() requires the initial filename to end in six X's */
  1493. X#ifndef TEMP_FILE_NAME        /* so can override from Makefile */
  1494. X#define TEMP_FILE_NAME  "%sJPG%dXXXXXX"
  1495. X#endif
  1496. X
  1497. XLOCAL void
  1498. Xselect_file_name (char * fname)
  1499. X{
  1500. X  next_file_num++;        /* advance counter */
  1501. X  sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
  1502. X  mktemp(fname);        /* make sure file name is unique */
  1503. X  /* mktemp replaces the trailing XXXXXX with a unique string of characters */
  1504. X}
  1505. X
  1506. X#endif /* NO_MKTEMP */
  1507. X
  1508. X
  1509. X/*
  1510. X * Memory allocation and freeing are controlled by the regular library
  1511. X * routines malloc() and free().
  1512. X */
  1513. X
  1514. XGLOBAL void *
  1515. Xjget_small (size_t sizeofobject)
  1516. X{
  1517. X  total_used += sizeofobject;
  1518. X  return (void *) malloc(sizeofobject);
  1519. X}
  1520. X
  1521. XGLOBAL void
  1522. Xjfree_small (void * object)
  1523. X{
  1524. X  free(object);
  1525. X}
  1526. X
  1527. X/*
  1528. X * We assume NEED_FAR_POINTERS is not defined and so the separate entry points
  1529. X * jget_large, jfree_large are not needed.
  1530. X */
  1531. X
  1532. X
  1533. X/*
  1534. X * This routine computes the total memory space available for allocation.
  1535. X * It's impossible to do this in a portable way; our current solution is
  1536. X * to make the user tell us (with a default value set at compile time).
  1537. X * If you can actually get the available space, it's a good idea to subtract
  1538. X * a slop factor of 5% or so.
  1539. X */
  1540. X
  1541. X#ifndef DEFAULT_MAX_MEM        /* so can override from makefile */
  1542. X#define DEFAULT_MAX_MEM        1000000L /* default: one megabyte */
  1543. X#endif
  1544. X
  1545. XGLOBAL long
  1546. Xjmem_available (long min_bytes_needed, long max_bytes_needed)
  1547. X{
  1548. X  return methods->max_memory_to_use - total_used;
  1549. X}
  1550. X
  1551. X
  1552. X/*
  1553. X * Backing store (temporary file) management.
  1554. X * Backing store objects are only used when the value returned by
  1555. X * jmem_available is less than the total space needed.  You can dispense
  1556. X * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  1557. X */
  1558. X
  1559. X
  1560. XMETHODDEF void
  1561. Xread_backing_store (backing_store_ptr info, void FAR * buffer_address,
  1562. X            long file_offset, long byte_count)
  1563. X{
  1564. X  if (fseek(info->temp_file, file_offset, SEEK_SET))
  1565. X    ERREXIT(methods, "fseek failed on temporary file");
  1566. X  if (JFREAD(info->temp_file, buffer_address, byte_count)
  1567. X      != (size_t) byte_count)
  1568. X    ERREXIT(methods, "fread failed on temporary file");
  1569. X}
  1570. X
  1571. X
  1572. XMETHODDEF void
  1573. Xwrite_backing_store (backing_store_ptr info, void FAR * buffer_address,
  1574. X             long file_offset, long byte_count)
  1575. X{
  1576. X  if (fseek(info->temp_file, file_offset, SEEK_SET))
  1577. X    ERREXIT(methods, "fseek failed on temporary file");
  1578. X  if (JFWRITE(info->temp_file, buffer_address, byte_count)
  1579. X      != (size_t) byte_count)
  1580. X    ERREXIT(methods, "fwrite failed on temporary file --- out of disk space?");
  1581. X}
  1582. X
  1583. X
  1584. XMETHODDEF void
  1585. Xclose_backing_store (backing_store_ptr info)
  1586. X{
  1587. X  fclose(info->temp_file);    /* close the file */
  1588. X  unlink(info->temp_name);    /* delete the file */
  1589. X/* If your system doesn't have unlink(), use remove() instead.
  1590. X * remove() is the ANSI-standard name for this function, but if
  1591. X * your system was ANSI you'd be using jmemansi.c, right?
  1592. X */
  1593. X}
  1594. X
  1595. X
  1596. XGLOBAL void
  1597. Xjopen_backing_store (backing_store_ptr info, long total_bytes_needed)
  1598. X{
  1599. X  char tracemsg[TEMP_NAME_LENGTH+40];
  1600. X
  1601. X  select_file_name(info->temp_name);
  1602. X  if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL)
  1603. X    ERREXIT(methods, "Failed to create temporary file");
  1604. X  info->read_backing_store = read_backing_store;
  1605. X  info->write_backing_store = write_backing_store;
  1606. X  info->close_backing_store = close_backing_store;
  1607. X  /* hack to get around TRACEMS' inability to handle string parameters */
  1608. X  sprintf(tracemsg, "Using temp file %s", info->temp_name);
  1609. X  TRACEMS(methods, 1, tracemsg);
  1610. X}
  1611. X
  1612. X
  1613. X/*
  1614. X * These routines take care of any system-dependent initialization and
  1615. X * cleanup required.  Keep in mind that jmem_term may be called more than
  1616. X * once.
  1617. X */
  1618. X
  1619. XGLOBAL void
  1620. Xjmem_init (external_methods_ptr emethods)
  1621. X{
  1622. X  methods = emethods;        /* save struct addr for error exit access */
  1623. X  emethods->max_memory_to_use = DEFAULT_MAX_MEM;
  1624. X  total_used = 0;
  1625. X  next_file_num = 0;
  1626. X}
  1627. X
  1628. XGLOBAL void
  1629. Xjmem_term (void)
  1630. X{
  1631. X  /* no work */
  1632. X}
  1633. END_OF_FILE
  1634.   if test 7643 -ne `wc -c <'jmemname.c'`; then
  1635.     echo shar: \"'jmemname.c'\" unpacked with wrong size!
  1636.   fi
  1637.   # end of 'jmemname.c'
  1638. fi
  1639. if test -f 'jrevdct.c' -a "${1}" != "-c" ; then 
  1640.   echo shar: Will not clobber existing file \"'jrevdct.c'\"
  1641. else
  1642.   echo shar: Extracting \"'jrevdct.c'\" \(7547 characters\)
  1643.   sed "s/^X//" >'jrevdct.c' <<'END_OF_FILE'
  1644. X/*
  1645. X * jrevdct.c
  1646. X *
  1647. X * Copyright (C) 1991, 1992, Thomas G. Lane.
  1648. X * This file is part of the Independent JPEG Group's software.
  1649. X * For conditions of distribution and use, see the accompanying README file.
  1650. X *
  1651. X * This file contains the basic inverse-DCT transformation subroutine.
  1652. X *
  1653. X * This implementation is based on Appendix A.2 of the book
  1654. X * "Discrete Cosine Transform---Algorithms, Advantages, Applications"
  1655. X * by K.R. Rao and P. Yip  (Academic Press, Inc, London, 1990).
  1656. X * It uses scaled fixed-point arithmetic instead of floating point.
  1657. X */
  1658. X
  1659. X#include "jinclude.h"
  1660. X
  1661. X/*
  1662. X * This routine is specialized to the case DCTSIZE = 8.
  1663. X */
  1664. X
  1665. X#if DCTSIZE != 8
  1666. X  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  1667. X#endif
  1668. X
  1669. X
  1670. X/* The poop on this scaling stuff is as follows:
  1671. X *
  1672. X * We have to do addition and subtraction of the integer inputs, which
  1673. X * is no problem, and multiplication by fractional constants, which is
  1674. X * a problem to do in integer arithmetic.  We multiply all the constants
  1675. X * by DCT_SCALE and convert them to integer constants (thus retaining
  1676. X * LG2_DCT_SCALE bits of precision in the constants).  After doing a
  1677. X * multiplication we have to divide the product by DCT_SCALE, with proper
  1678. X * rounding, to produce the correct output.  The division can be implemented
  1679. X * cheaply as a right shift of LG2_DCT_SCALE bits.  The DCT equations also
  1680. X * specify an additional division by 2 on the final outputs; this can be
  1681. X * folded into the right-shift by shifting one more bit (see UNFIXH).
  1682. X *
  1683. X * If you are planning to recode this in assembler, you might want to set
  1684. X * LG2_DCT_SCALE to 15.  This loses a bit of precision, but then all the
  1685. X * multiplications are between 16-bit quantities (given 8-bit JSAMPLEs!)
  1686. X * so you could use a signed 16x16=>32 bit multiply instruction instead of
  1687. X * full 32x32 multiply.  Unfortunately there's no way to describe such a
  1688. X * multiply portably in C, so we've gone for the extra bit of accuracy here.
  1689. X */
  1690. X
  1691. X#ifdef EIGHT_BIT_SAMPLES
  1692. X#define LG2_DCT_SCALE 16
  1693. X#else
  1694. X#define LG2_DCT_SCALE 15    /* lose a little precision to avoid overflow */
  1695. X#endif
  1696. X
  1697. X#define ONE    ((INT32) 1)
  1698. X
  1699. X#define DCT_SCALE (ONE << LG2_DCT_SCALE)
  1700. X
  1701. X/* In some places we shift the inputs left by a couple more bits, */
  1702. X/* so that they can be added to fractional results without too much */
  1703. X/* loss of precision. */
  1704. X#define LG2_OVERSCALE 2
  1705. X#define OVERSCALE  (ONE << LG2_OVERSCALE)
  1706. X#define OVERSHIFT(x)  ((x) <<= LG2_OVERSCALE)
  1707. X
  1708. X/* Scale a fractional constant by DCT_SCALE */
  1709. X#define FIX(x)    ((INT32) ((x) * DCT_SCALE + 0.5))
  1710. X
  1711. X/* Scale a fractional constant by DCT_SCALE/OVERSCALE */
  1712. X/* Such a constant can be multiplied with an overscaled input */
  1713. X/* to produce something that's scaled by DCT_SCALE */
  1714. X#define FIXO(x)  ((INT32) ((x) * DCT_SCALE / OVERSCALE + 0.5))
  1715. X
  1716. X/* Descale and correctly round a value that's scaled by DCT_SCALE */
  1717. X#define UNFIX(x)   RIGHT_SHIFT((x) + (ONE << (LG2_DCT_SCALE-1)), LG2_DCT_SCALE)
  1718. X
  1719. X/* Same with an additional division by 2, ie, correctly rounded UNFIX(x/2) */
  1720. X#define UNFIXH(x)  RIGHT_SHIFT((x) + (ONE << LG2_DCT_SCALE), LG2_DCT_SCALE+1)
  1721. X
  1722. X/* Take a value scaled by DCT_SCALE and round to integer scaled by OVERSCALE */
  1723. X#define UNFIXO(x)  RIGHT_SHIFT((x) + (ONE << (LG2_DCT_SCALE-1-LG2_OVERSCALE)),\
  1724. X                   LG2_DCT_SCALE-LG2_OVERSCALE)
  1725. X
  1726. X/* Here are the constants we need */
  1727. X/* SIN_i_j is sine of i*pi/j, scaled by DCT_SCALE */
  1728. X/* COS_i_j is cosine of i*pi/j, scaled by DCT_SCALE */
  1729. X
  1730. X#define SIN_1_4 FIX(0.707106781)
  1731. X#define COS_1_4 SIN_1_4
  1732. X
  1733. X#define SIN_1_8 FIX(0.382683432)
  1734. X#define COS_1_8 FIX(0.923879533)
  1735. X#define SIN_3_8 COS_1_8
  1736. X#define COS_3_8 SIN_1_8
  1737. X
  1738. X#define SIN_1_16 FIX(0.195090322)
  1739. X#define COS_1_16 FIX(0.980785280)
  1740. X#define SIN_7_16 COS_1_16
  1741. X#define COS_7_16 SIN_1_16
  1742. X
  1743. X#define SIN_3_16 FIX(0.555570233)
  1744. X#define COS_3_16 FIX(0.831469612)
  1745. X#define SIN_5_16 COS_3_16
  1746. X#define COS_5_16 SIN_3_16
  1747. X
  1748. X/* OSIN_i_j is sine of i*pi/j, scaled by DCT_SCALE/OVERSCALE */
  1749. X/* OCOS_i_j is cosine of i*pi/j, scaled by DCT_SCALE/OVERSCALE */
  1750. X
  1751. X#define OSIN_1_4 FIXO(0.707106781)
  1752. X#define OCOS_1_4 OSIN_1_4
  1753. X
  1754. X#define OSIN_1_8 FIXO(0.382683432)
  1755. X#define OCOS_1_8 FIXO(0.923879533)
  1756. X#define OSIN_3_8 OCOS_1_8
  1757. X#define OCOS_3_8 OSIN_1_8
  1758. X
  1759. X#define OSIN_1_16 FIXO(0.195090322)
  1760. X#define OCOS_1_16 FIXO(0.980785280)
  1761. X#define OSIN_7_16 OCOS_1_16
  1762. X#define OCOS_7_16 OSIN_1_16
  1763. X
  1764. X#define OSIN_3_16 FIXO(0.555570233)
  1765. X#define OCOS_3_16 FIXO(0.831469612)
  1766. X#define OSIN_5_16 OCOS_3_16
  1767. X#define OCOS_5_16 OSIN_3_16
  1768. X
  1769. X
  1770. X/*
  1771. X * Perform the inverse DCT on one block of coefficients.
  1772. X *
  1773. X * A 2-D IDCT can be done by 1-D IDCT on each row
  1774. X * followed by 1-D IDCT on each column.
  1775. X */
  1776. X
  1777. XGLOBAL void
  1778. Xj_rev_dct (DCTBLOCK data)
  1779. X{
  1780. X  int pass, rowctr;
  1781. X  register DCTELEM *inptr, *outptr;
  1782. X  DCTBLOCK workspace;
  1783. X
  1784. X  /* Each iteration of the inner loop performs one 8-point 1-D IDCT.
  1785. X   * It reads from a *row* of the input matrix and stores into a *column*
  1786. X   * of the output matrix.  In the first pass, we read from the data[] array
  1787. X   * and store into the local workspace[].  In the second pass, we read from
  1788. X   * the workspace[] array and store into data[], thus performing the
  1789. X   * equivalent of a columnar IDCT pass with no variable array indexing.
  1790. X   */
  1791. X
  1792. X  inptr = data;            /* initialize pointers for first pass */
  1793. X  outptr = workspace;
  1794. X  for (pass = 1; pass >= 0; pass--) {
  1795. X    for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
  1796. X      /* many tmps have nonoverlapping lifetime -- flashy register colourers
  1797. X       * should be able to do this lot very well
  1798. X       */
  1799. X      INT32 in0, in1, in2, in3, in4, in5, in6, in7;
  1800. X      INT32 tmp10, tmp11, tmp12, tmp13;
  1801. X      INT32 tmp20, tmp21, tmp22, tmp23;
  1802. X      INT32 tmp30, tmp31;
  1803. X      INT32 tmp40, tmp41, tmp42, tmp43;
  1804. X      INT32 tmp50, tmp51, tmp52, tmp53;
  1805. X      SHIFT_TEMPS
  1806. X    
  1807. X      in0 = inptr[0];
  1808. X      in1 = inptr[1];
  1809. X      in2 = inptr[2];
  1810. X      in3 = inptr[3];
  1811. X      in4 = inptr[4];
  1812. X      in5 = inptr[5];
  1813. X      in6 = inptr[6];
  1814. X      in7 = inptr[7];
  1815. X      
  1816. X      /* These values are scaled by DCT_SCALE */
  1817. X      
  1818. X      tmp10 = (in0 + in4) * COS_1_4;
  1819. X      tmp11 = (in0 - in4) * COS_1_4;
  1820. X      tmp12 = in2 * SIN_1_8 - in6 * COS_1_8;
  1821. X      tmp13 = in6 * SIN_1_8 + in2 * COS_1_8;
  1822. X      
  1823. X      tmp20 = tmp10 + tmp13;
  1824. X      tmp21 = tmp11 + tmp12;
  1825. X      tmp22 = tmp11 - tmp12;
  1826. X      tmp23 = tmp10 - tmp13;
  1827. X      
  1828. X      /* These values are scaled by OVERSCALE */
  1829. X      
  1830. X      tmp30 = UNFIXO((in3 + in5) * COS_1_4);
  1831. X      tmp31 = UNFIXO((in3 - in5) * COS_1_4);
  1832. X      
  1833. X      OVERSHIFT(in1);
  1834. X      OVERSHIFT(in7);
  1835. X      
  1836. X      tmp40 = in1 + tmp30;
  1837. X      tmp41 = in7 + tmp31;
  1838. X      tmp42 = in1 - tmp30;
  1839. X      tmp43 = in7 - tmp31;
  1840. X      
  1841. X      /* And these are scaled by DCT_SCALE */
  1842. X      
  1843. X      tmp50 = tmp40 * OCOS_1_16 + tmp41 * OSIN_1_16;
  1844. X      tmp51 = tmp40 * OSIN_1_16 - tmp41 * OCOS_1_16;
  1845. X      tmp52 = tmp42 * OCOS_5_16 + tmp43 * OSIN_5_16;
  1846. X      tmp53 = tmp42 * OSIN_5_16 - tmp43 * OCOS_5_16;
  1847. X      
  1848. X      outptr[        0] = (DCTELEM) UNFIXH(tmp20 + tmp50);
  1849. X      outptr[DCTSIZE  ] = (DCTELEM) UNFIXH(tmp21 + tmp53);
  1850. X      outptr[DCTSIZE*2] = (DCTELEM) UNFIXH(tmp22 + tmp52);
  1851. X      outptr[DCTSIZE*3] = (DCTELEM) UNFIXH(tmp23 + tmp51);
  1852. X      outptr[DCTSIZE*4] = (DCTELEM) UNFIXH(tmp23 - tmp51);
  1853. X      outptr[DCTSIZE*5] = (DCTELEM) UNFIXH(tmp22 - tmp52);
  1854. X      outptr[DCTSIZE*6] = (DCTELEM) UNFIXH(tmp21 - tmp53);
  1855. X      outptr[DCTSIZE*7] = (DCTELEM) UNFIXH(tmp20 - tmp50);
  1856. X      
  1857. X      inptr += DCTSIZE;        /* advance inptr to next row */
  1858. X      outptr++;            /* advance outptr to next column */
  1859. X    }
  1860. X    /* end of pass; in case it was pass 1, set up for pass 2 */
  1861. X    inptr = workspace;
  1862. X    outptr = data;
  1863. X  }
  1864. X}
  1865. END_OF_FILE
  1866.   if test 7547 -ne `wc -c <'jrevdct.c'`; then
  1867.     echo shar: \"'jrevdct.c'\" unpacked with wrong size!
  1868.   fi
  1869.   # end of 'jrevdct.c'
  1870. fi
  1871. echo shar: End of archive 14 \(of 18\).
  1872. cp /dev/null ark14isdone
  1873. MISSING=""
  1874. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ; do
  1875.     if test ! -f ark${I}isdone ; then
  1876.     MISSING="${MISSING} ${I}"
  1877.     fi
  1878. done
  1879. if test "${MISSING}" = "" ; then
  1880.     echo You have unpacked all 18 archives.
  1881.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1882. else
  1883.     echo You still must unpack the following archives:
  1884.     echo "        " ${MISSING}
  1885. fi
  1886. exit 0
  1887. exit 0 # Just in case...
  1888.