The Independent JPEG Group's JPEG software v5
diff --git a/CHANGELOG b/CHANGELOG
deleted file mode 100644
index ccbc6ea..0000000
--- a/CHANGELOG
+++ /dev/null
@@ -1,146 +0,0 @@
-CHANGELOG for Independent JPEG Group's JPEG software
-
-
-Version 4A 18-Feb-93
---------------------
-
-Substantial speedup for grayscale output from color JPEG file (suppress
-processing of chrominance components).  Lesser speedups in Huffman decoding
-and in compression quantization.
-
-Can switch stdin/stdout to binary mode with either fdopen() or setmode();
-this allows use of one-file command line style on a wider range of systems.
-Also added -outfile switch so that makefile test scripts don't have to depend
-on the command line style.
-
-New makefile.icc for Code Builder; makefile.sas is updated for SAS C 6.x.
-
-Hook added to allow surrounding application to read and write COM (comment)
-blocks.
-
-Bugfixes (DOS only): jmemdos.c code for accessing expanded memory only worked
-if struct fields are packed on byte boundaries.  This is not true by default
-for Microsoft C.  Furthermore, Microsoft C needs an _fheapmin() call to clean
-up the far heap correctly.
-
-
-Version 4  10-Dec-92
---------------------
-
-Revised user interface: switches now use names instead of single letters.
-(Old switch letters are acceptable abbreviations of new switch names, EXCEPT
-for djpeg's old -g, -D, -1 switches.)  cjpeg has several new switches.
-
-Provision for smoothing the input image added to cjpeg.  This helps a lot with
-converting dithered GIFs to JPEG.
-
-Decoder upsampling now uses interpolation instead of pixel replication; this
-improves rendering of sharp colored edges.
-
-The decompressor will now try to continue after detecting an error in the
-compressed data, instead of just aborting.  If the input file has restart
-markers, full synchronization will usually be regained at the next undamaged
-restart marker.  (But you're still out of luck if any of the header markers
-are corrupt.)
-
-Substantial improvements in speed; DCT accuracy improved too.
-
-Numerous minor changes to improve portability.  egetopt.c, which was by far
-the worst portability problem, is gone altogether.
-
-A few bugfixes, sigh (mostly affecting DOS implementations only).
-Bugfix: on DOS machines, cjpeg -o would fail on grayscale input files.
-Bugfix: one-pass quantization to more than 64 color levels would fail on
-16-bit-int machines.  This could only happen with quantized grayscale output.
-
-A couple of changes affect code that calls the JPEG subroutine library:
-
-1. The parameter struct tag names are now capitalized (Compress_info_struct,
-Compress_methods_struct, Decompress_info_struct, Decompress_methods_struct,
-and External_methods_struct).  This makes it easier to live with brain-damaged
-compilers with short identifier lengths.  (All identifiers used in the JPEG
-code are now unique within the first 16 characters.)
-
-2. If you are not calling jselerror(), you need to initialize three new fields
-in the emethods structure, typically as follows:
-    e_methods.num_warnings = 0; /* no warnings emitted yet */
-    e_methods.first_warning_level = 0; /* display first corrupt-data warning */
-    e_methods.more_warning_level = 3; /* but suppress additional ones */
-These fields control handling of corrupt-data warnings.
-
-
-Version 3  17-Mar-92
---------------------
-
-Memory manager is finally capable of swapping to temp files.  There are
-separate versions of jmemsys.c for no temp files (same behavior as older
-versions), simple temp files with or without tmpfile(), and a DOS-specific
-version (including special code for EMS and XMS).  This is probably much more
-system-dependent than any of the older code; some bugs may surface here.
-
-Hooks added for user interface to install progress monitoring routine
-(percent-done bar, etc).  See comments with dummy progress_monitor
-routines in jcdeflts.c, jddeflts.c.
-
-Two-pass color quantization (finally!).  This is now the default method when
-quantizing; say '-1' to djpeg for quick-and-ugly 1-pass method.  There is
-a test file for checking 2-pass quantization and GIF output.
-
-Fixed bug in jcopy_block_row that broke cjpeg -o option and djpeg -b option
-on MSDOS machines.
-
-Miscellaneous small speedups; notably, DCT computation rearranged so that
-GCC "inline" feature is no longer needed for good code quality.
-
-File config.c renamed ckconfig.c to avoid name conflict with /etc/config
-on Unix systems.
-
-Added example.c to document usage of JPEG subroutines better.
-
-Memory manager now knows how to release all storage during error exit ---
-avoids memory leak when using JPEG as subroutines.  This implies a couple
-small changes to the subroutine interface: the old free_defaults subroutines
-are no longer needed, but if you have a replacement error_exit method then it
-must call the new free_all method.  Also, jselvirtmem renamed to jselmemmgr.
-
-Code for reading Targa files with 32-bit pixels was incorrect.
-
-Colorspace conversion slightly faster and more accurate; because of
-this, old "test" files will no longer match bit-for-bit.
-
-
-Version 2  13-Dec-91
---------------------
-
-Documentation improved a little --- there are man pages now.
-Installation instructions moved from README to a separate file SETUP.
-
-New program config.c is provided to help you get the configuration options
-right.  This should make installation a lot more foolproof.
-
-Sense of djpeg -D switch reversed: dithering is now ON by default.
-
-RLE image file support added (thanks to Mike Lijewski).
-
-Targa image file support added (thanks to Lee Crocker).
-
-PPM input now accepts all PPM and PGM files.
-
-Bug fix: on machines where 'int' is 16 bits, high-Q-setting JPEG files
-were not decoded correctly.
-
-Numerous changes to improve portability.  There should be few or no compiler
-warnings now.
-
-Makefiles cleaned up; defaults now appropriate for production use rather than
-debugging.
-
-Subroutine interface cleaned up.  If you wrote code based on version 1's
-jcmain/jdmain, you'll need to change it, but it should get a little shorter
-and simpler.
-
-
-Version 1   7-Oct-91
---------------------
-
-Initial public release.
diff --git a/README b/README
index 99c4d36..c882ffb 100644
--- a/README
+++ b/README
@@ -1,51 +1,65 @@
 The Independent JPEG Group's JPEG software
 ==========================================
 
-README for release 4A of 18-Feb-93
-==================================
+README for release 5 of 24-Sep-94
+=================================
 
-This distribution contains a BETA TEST release of the Independent JPEG
+This distribution contains the fifth public release of the Independent JPEG
 Group's free JPEG software.  You are welcome to redistribute this software and
 to use it for any purpose, subject to the conditions under LEGAL ISSUES, below.
 
-For installation instructions, see file SETUP.
-
-For usage instructions, see file USAGE (or the cjpeg.1 and djpeg.1 manual
-pages; but USAGE contains a "hints" section not found in the manual pages).
-Useful information can also be found in the JPEG FAQ (Frequently Asked
-Questions) article; see ARCHIVE LOCATIONS below to obtain the FAQ article.
-
-This software is still undergoing revision.  Updated versions may be obtained
-by FTP or UUCP to UUNET and other archive sites; see ARCHIVE LOCATIONS below
-for details.
-
 Serious users of this software (particularly those incorporating it into
 larger programs) should contact jpeg-info@uunet.uu.net to be added to our
 electronic mailing list.  Mailing list members are notified of updates and
 have a chance to participate in technical discussions, etc.
 
-This software is the work of Tom Lane, Philip Gladstone, Luis Ortiz, Lee
-Crocker, George Phillips, Ge' Weijers, and other members of the Independent
-JPEG Group.
+This software is the work of Tom Lane, Philip Gladstone, Luis Ortiz, Jim
+Boucher, Lee Crocker, George Phillips, Davide Rossi, Ge' Weijers, and other
+members of the Independent JPEG Group.
+
+IJG is not associated with the official ISO JPEG standards committee.
 
 
-DISCLAIMER
-==========
+DOCUMENTATION ROADMAP
+=====================
 
-THIS SOFTWARE IS NOT COMPLETE NOR FULLY DEBUGGED.  It is not guaranteed to be
-useful for anything, nor to be compatible with subsequent releases, nor to be
-an accurate implementation of the JPEG standard.  (See LEGAL ISSUES for even
-more disclaimers.)
+This file contains the following sections:
 
-Despite that, we believe that this software is pretty good, and if you find
-any problems with it, we'd like to know about them.  Please report problems
-by e-mail to jpeg-info@uunet.uu.net.
+OVERVIEW            General description of JPEG and the IJG software.
+LEGAL ISSUES        Copyright, lack of warranty, terms of distribution.
+REFERENCES          Where to learn more about JPEG.
+ARCHIVE LOCATIONS   Where to find newer versions of this software.
+RELATED SOFTWARE    Other stuff you should get.
+FILE FORMAT WARS    Software *not* to get.
+TO DO               Plans for future IJG releases.
+
+Other documentation files in the distribution are:
+
+User documentation:
+  install.doc       How to configure and install the IJG software.
+  usage.doc         Usage instructions for cjpeg, djpeg, rdjpgcom, wrjpgcom.
+  *.1               Unix-style man pages for programs (same info as usage.doc).
+  change.log        Version-to-version change highlights.
+Programmer and internal documentation:
+  libjpeg.doc       How to use the JPEG library in your own programs.
+  example.c         Sample code for calling the JPEG library.
+  structure.doc     Overview of the JPEG library's internal structure.
+  filelist.doc      Road map of IJG files.
+  coderules.doc     Coding style rules --- please read if you contribute code.
+
+Please read at least the files install.doc and usage.doc.  Useful information
+can also be found in the JPEG FAQ (Frequently Asked Questions) article.  See
+ARCHIVE LOCATIONS below to find out where to obtain the FAQ article.
+
+If you want to understand how the JPEG code works, we suggest reading one or
+more of the REFERENCES, then looking at the documentation files (in roughly
+the order listed) before diving into the code.
 
 
-WHAT'S HERE
-===========
+OVERVIEW
+========
 
-This distribution contains C software to implement JPEG image compression and
+This package contains C software to implement JPEG image compression and
 decompression.  JPEG (pronounced "jay-peg") is a standardized compression
 method for full-color and gray-scale images.  JPEG is intended for compressing
 "real-world" scenes; cartoons and other non-realistic images are not its
@@ -57,19 +71,27 @@
 For more details, see the references, or just experiment with various
 compression settings.
 
-The software implements JPEG baseline and extended-sequential compression
+We provide a set of library routines for reading and writing JPEG image files,
+plus two simple applications "cjpeg" and "djpeg", which use the library to
+perform conversion between JPEG and some other popular image file formats.
+The library is intended to be reused in other applications.
+
+This software implements JPEG baseline and extended-sequential compression
 processes.  Provision is made for supporting all variants of these processes,
 although some uncommon parameter settings aren't implemented yet.  For legal
 reasons, we are not distributing code for the arithmetic-coding process; see
 LEGAL ISSUES.  At present we have made no provision for supporting the
 progressive, hierarchical, or lossless processes defined in the standard.
+(Support for progressive mode may be offered in a future release.)
 
 In order to support file conversion and viewing software, we have included
 considerable functionality beyond the bare JPEG coding/decoding capability;
 for example, the color quantization modules are not strictly part of JPEG
 decoding, but they are essential for output to colormapped file formats or
-colormapped displays.  These extra functions can be compiled out if not
-required for a particular application.
+colormapped displays.  These extra functions can be compiled out of the
+library if not required for a particular application.  We have also included
+two simple applications for inserting and extracting textual comments in
+JFIF files.
 
 The emphasis in designing this software has been on achieving portability and
 flexibility, while also making it fast enough to be useful.  In particular,
@@ -78,258 +100,31 @@
 package will someday be industrial-strength code, much remains to be done in
 performance tuning and in improving the capabilities of individual modules.
 
-
-This software can be used on several levels:
-
-* As canned software for JPEG compression and decompression.  Just edit the
-  Makefile and configuration files as needed (see file SETUP), compile and go.
-  Members of the Independent JPEG Group will improve the out-of-the-box
-  functionality and speed as time goes on.
-
-* As the basis for other JPEG programs.  For example, you could incorporate
-  the decompressor into a general image viewing package by replacing the
-  output module with write-to-screen functions.  For an implementation on
-  specific hardware, you might want to replace some of the inner loops with
-  assembly code.  For a non-command-line-driven system, you might want a
-  different user interface.  (Members of the group will be producing Macintosh
-  and Amiga versions with more appropriate user interfaces, for example.)
-
-* As a toolkit for experimentation with JPEG and JPEG-like algorithms.  Most
-  of the individual decisions you might want to mess with are packaged up into
-  separate modules.  For example, the details of color-space conversion and
-  subsampling techniques are each localized in one compressor and one
-  decompressor module.  You'd probably also want to extend the user interface
-  to give you more detailed control over the JPEG compression parameters.
-
-In particular, we welcome the use of this software as a component of commercial
-products; no royalty is required.
-
-
-ARCHIVE LOCATIONS
-=================
-
-[Version 4A is a beta-test release and will not be publicly archived.
-The following paragraphs refer to the most recent official release.]
-
-The "official" archive site for this software is ftp.uu.net (Internet
-address 137.39.1.9 or 192.48.96.9).  The most recent released version can
-always be found there in directory graphics/jpeg.  This particular version
-will be archived as jpegsrc.v4.tar.Z.  If you are on the Internet, you can
-retrieve files from UUNET by anonymous FTP.  If you don't have FTP access,
-UUNET's archives are also available via UUCP; contact postmaster@uunet.uu.net
-for information on retrieving files that way.
-
-Numerous Internet sites maintain copies of the UUNET files; in particular,
-you can probably find a copy at any site that archives comp.sources.misc
-submissions.  However, only ftp.uu.net is guaranteed to have the latest
-official version.
-
-You can also obtain this software from CompuServe, in the GRAPHSUPPORT forum
-(GO PICS), library 15; this version will be file jpsrc4.zip.  Again,
-CompuServe is not guaranteed to have the very latest version.
-
-The JPEG FAQ (Frequently Asked Questions) article is a useful source of
-general information about JPEG.  It is updated constantly and therefore
-is not included in this distribution.  The FAQ is posted every two weeks
-to Usenet newsgroups comp.graphics, news.answers, and other groups.  You
-can always obtain the latest version from the news.answers archive at
-rtfm.mit.edu (18.172.1.27).  By FTP, fetch /pub/usenet/news.answers/jpeg-faq.
-If you don't have FTP, send e-mail to mail-server@rtfm.mit.edu with body
-"send usenet/news.answers/jpeg-faq".
-
-
-SUPPORTING SOFTWARE
-===================
-
-You will probably want Jef Poskanzer's PBMPLUS image software, which provides
-many useful operations on PPM-format image files.  In particular, it can
-convert PPM images to and from a wide range of other formats.  You can FTP
-this free software from export.lcs.mit.edu (contrib/pbmplus*.tar.Z) or
-ftp.ee.lbl.gov (pbmplus*.tar.Z).  Unfortunately PBMPLUS is not nearly as
-portable as the JPEG software is; you are likely to have difficulty making it
-work on any non-Unix machine.
-
-If you are using X Windows you might want to use the xv or xloadimage viewers
-to save yourself the trouble of converting PPM to some other format.  Both of
-these can be found in the contrib directory at export.lcs.mit.edu.  Actually,
-xv version 2.00 and up incorporates our software and thus can read and write
-JPEG files directly.  (NOTE: since xv internally reduces all images to 8
-bits/pixel, a JPEG file written by xv will not be very high quality; and xv
-cannot fully exploit a 24-bit display.  These problems are expected to go away
-in the next xv release, planned for early 1993.  In the meantime, use
-xloadimage for 24-bit displays.)
-
-For DOS machines, Lee Crocker's free Piclab program is a useful companion to
-the JPEG software.  The latest version, currently 1.91, is available by FTP
-from SIMTEL20 and its various mirror sites, file <msdos.graphics>piclb191.zip.
-CompuServe also has it, in the same library as the JPEG software.
-
-
-SOFTWARE THAT'S NO HELP AT ALL
-==============================
-
-Handmade Software's shareware PC program GIF2JPG produces files that are
-totally incompatible with our programs.  They use a proprietary format that is
-an amalgam of GIF and JPEG representations.  However, you can force GIF2JPG
-to produce compatible files with its -j switch, and their decompression
-program JPG2GIF can read our files (at least ones produced with our default
-option settings).
-
-Some commercial JPEG implementations are also incompatible as of this writing,
-especially programs released before summer 1991.  The root of the problem is
-that the ISO JPEG committee failed to specify a concrete file format.  Some
-vendors "filled in the blanks" on their own, creating proprietary formats that
-no one else could read.  (For example, none of the early commercial JPEG
-implementations for the Macintosh were able to exchange compressed files.)
-
-The file format we have adopted is called JFIF (see REFERENCES).  This format
-has been agreed to by a number of major commercial JPEG vendors, and we expect
-that it will become the de facto standard.  JFIF is a minimal representation;
-work is also going forward to incorporate JPEG compression into the TIFF 6.0
-standard, for use in "high end" applications that need to record a lot of
-additional data about an image.  We intend to support TIFF 6.0 in the future.
-We hope that these two formats will be sufficient and that other, incompatible
-JPEG file formats will not proliferate.
-
-Indeed, part of the reason for developing and releasing this free software is
-to help force rapid convergence to de facto standards for JPEG file formats.
-SUPPORT STANDARD, NON-PROPRIETARY FORMATS: demand JFIF or TIFF 6.0!
-
-
-USING JPEG AS A SUBROUTINE IN A LARGER PROGRAM
-==============================================
-
-You can readily incorporate the JPEG compression and decompression routines in
-a larger program.  The file example.c provides a skeleton of the interface
-routines you'll need for this purpose.  Essentially, you replace jcmain.c (for
-compression) and/or jdmain.c (for decompression) with your own code.  Note
-that the fewer JPEG options you allow the user to twiddle, the less code you
-need; all the default options are set up automatically.  (Alternately, if you
-know a lot about JPEG or have a special application, you may want to twiddle
-the default options even more extensively than jcmain/jdmain do.)
-
-Most likely, you will want the uncompressed image to come from memory (for
-compression) or go to memory or the screen (for decompression).  For this
-purpose you must provide image reading or writing routines that match the
-interface used by the image file I/O modules (jrdXXX or jwrXXX); again,
-example.c shows a skeleton of what is required.  In this situation, you
-won't need any of the non-JPEG image file I/O modules used by cjpeg and djpeg.
-
-By default, any error detected inside the JPEG routines will cause a message
-to be printed on stderr, followed by exit().  You can override this behavior
-by supplying your own message-printing and/or error-exit routines; again,
-example.c shows how.
-
-We recommend you create libjpeg.a as shown in the Makefile, then link that
-with your surrounding program.  (If your linker is at all reasonable, only the
-code you actually need will get loaded.)  Include the files jconfig.h and
-jpegdata.h in C files that need to call the JPEG routines.
-
-CAUTION: some people have tried to compile JPEG and their surrounding code
-with different compilers, e.g., cc for JPEG and c++ or gcc for the rest.  This
-is a Real Bad Move and you will deserve what happens to you if you try it.
-(Hint: the parameter structures can get laid out differently with no warning.)
-
-Read our "architecture" file for more info.  If it seems to you that the
-software structure doesn't accommodate what you want to do, please contact
-the authors.
-
-Beginning with version 3, we will endeavor to hold the interface described by
-example.c constant, so that you can plug in updated versions of the JPEG code
-just by recompiling.  However, we can't guarantee this, especially if you
-choose to twiddle any JPEG options not listed in example.c.  Check the
-CHANGELOG when installing any new version, and compare example.c against the
-prior version.  Recompile your calling software (don't just relink), as we may
-add or subtract fields in the parameter structures.
-
-
-REFERENCES
-==========
-
-We highly recommend reading one or more of these references before trying to
-understand the innards of any JPEG software.
-
-The best short technical introduction to the JPEG compression algorithm is
-	Wallace, Gregory K.  "The JPEG Still Picture Compression Standard",
-	Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
-(Adjacent articles in that issue discuss MPEG motion picture compression,
-applications of JPEG, and related topics.)  If you don't have the CACM issue
-handy, a PostScript file containing a revised version of the article is
-available at ftp.uu.net, graphics/jpeg/wallace.ps.Z.  The file (actually a
-preprint for an article to appear in IEEE Trans. Consumer Electronics) omits
-the sample images that appeared in CACM, but it includes corrections and some
-added material.  Note: the Wallace article is copyright ACM and IEEE, and it
-may not be used for commercial purposes.
-
-A somewhat less technical, more leisurely introduction to JPEG can be found in
-"The Data Compression Book" by Mark Nelson, published by M&T Books (Redwood
-City, CA), 1991, ISBN 1-55851-216-0.  This book provides good explanations and
-example C code for a multitude of compression methods including JPEG.  It is
-an excellent source if you are comfortable reading C code but don't know much
-about data compression in general.  The book's JPEG sample code is far from
-industrial-strength, but when you are ready to look at a full implementation,
-you've got one here...
-
-A new textbook about JPEG is "JPEG Still Image Data Compression Standard" by
-William B. Pennebaker and Joan L. Mitchell, published by Van Nostrand
-Reinhold, 1993, ISBN 0-442-01272-1.  Price US$59.95.  This book includes the
-complete text of the ISO JPEG standards (DIS 10918-1 and draft DIS 10918-2).
-This is by far the most complete exposition of JPEG in existence, and I highly
-recommend it.  If you read the entire book, you will probably know more about
-JPEG than I do.
-
-The JPEG standard itself is not available electronically; you must order a
-paper copy through ISO.  (Unless you are concerned about having a certified
-official copy, I recommend buying the Pennebaker and Mitchell book instead;
-it's much cheaper and includes a great deal of useful explanatory material.)
-In the US, copies of the standard may be ordered from ANSI Sales at (212)
-642-4900.  It's not cheap: as of 1992, Part 1 is $95 and Part 2 is $47, plus
-7% shipping/handling.  The standard is divided into two parts, Part 1 being
-the actual specification, while Part 2 covers compliance testing methods.
-As of early 1992, Part 1 has Draft International Standard status.  It is
-titled "Digital Compression and Coding of Continuous-tone Still Images, Part
-1: Requirements and guidelines" and has document number ISO/IEC DIS 10918-1.
-Part 2 is still at Committee Draft status.  It is titled "Digital Compression
-and Coding of Continuous-tone Still Images, Part 2: Compliance testing" and
-has document number ISO/IEC CD 10918-2.  (NOTE: I'm told that the final
-version of Part 2 will differ considerably from the CD draft.)
-
-The JPEG standard does not specify all details of an interchangeable file
-format.  For the omitted details we follow the "JFIF" conventions, revision
-1.02.  A copy of the JFIF spec is available from:
-	Literature Department
-	C-Cube Microsystems, Inc.
-	399A West Trimble Road
-	San Jose, CA  95131
-	(408) 944-6300
-A PostScript version of this document is available at ftp.uu.net, file
-graphics/jpeg/jfif.ps.Z.  It can also be obtained by e-mail from the C-Cube
-mail server, netlib@c3.pla.ca.us.  Send the message "send jfif_ps from jpeg"
-to the server to obtain the JFIF document; send the message "help" if you have
-trouble.
-
-The TIFF 6.0 file format specification can be obtained by FTP from sgi.com
-(192.48.153.1), file graphics/tiff/TIFF6.ps.Z; or you can order a printed copy
-from Aldus Corp. at (206) 628-6593.  It should be noted that the TIFF 6.0 spec
-of 3-June-92 has a number of serious problems in its JPEG features.  A
-clarification note will probably be needed to ensure that TIFF JPEG files are
-compatible across different implementations.  The IJG does not intend to
-support TIFF 6.0 until these problems are resolved.
-
-If you want to understand this implementation, start by reading the
-"architecture" documentation file.  Please read "codingrules" if you want to
-contribute any code.
+We welcome the use of this software as a component of commercial products.
+No royalty is required, but we do ask for an acknowledgement in product
+documentation, as described under LEGAL ISSUES.
 
 
 LEGAL ISSUES
 ============
 
+In plain English:
+
+1. We don't promise that this software works.  (But if you find any bugs,
+   please let us know!)
+2. You can use this software for whatever you want.  You don't have to pay us.
+3. You may not pretend that you wrote this software.  If you use it in a
+   program, you must acknowledge somewhere in your documentation that
+   you've used the IJG code.
+
+In legalese:
+
 The authors make NO WARRANTY or representation, either express or implied,
 with respect to this software, its quality, accuracy, merchantability, or
 fitness for a particular purpose.  This software is provided "AS IS", and you,
 its user, assume the entire risk as to its quality and accuracy.
 
-This software is copyright (C) 1991, 1992, Thomas G. Lane.
+This software is copyright (C) 1991, 1992, 1993, 1994, Thomas G. Lane.
 All Rights Reserved except as specified below.
 
 Permission is hereby granted to use, copy, modify, and distribute this
@@ -346,6 +141,10 @@
 full responsibility for any undesirable consequences; the authors accept
 NO LIABILITY for damages of any kind.
 
+These conditions apply to any software derived from or based on the IJG code,
+not just to the unmodified library.  If you use our work, you ought to
+acknowledge us.
+
 Permission is NOT granted for the use of any IJG author's name or company name
 in advertising or publicity relating to this software or products derived from
 it.  This software may be referred to only as "the Independent JPEG Group's
@@ -362,44 +161,189 @@
 by the usual distribution terms of the Free Software Foundation; principally,
 that you must include source code if you redistribute it.  (See the file
 ansi2knr.c for full details.)  However, since ansi2knr.c is not needed as part
-of any program generated from the JPEG code, this does not limit you more than
+of any program generated from the IJG code, this does not limit you more than
 the foregoing paragraphs do.
 
+The configuration script "configure" was produced by GNU Autoconf.  Again,
+the FSF copyright terms apply only to configure, not to the IJG code; and
+again, that does not limit your use of the object code.
 
 It appears that the arithmetic coding option of the JPEG spec is covered by
-patents owned by IBM and AT&T, as well as a pending Japanese patent of
-Mitsubishi.  Hence arithmetic coding cannot legally be used without obtaining
-one or more licenses.  For this reason, support for arithmetic coding has been
-removed from the free JPEG software.  (Since arithmetic coding provides only a
-marginal gain over the unpatented Huffman mode, it is unlikely that very many
-implementors will support it.  If you do obtain the necessary licenses,
-contact jpeg-info@uunet.uu.net for a copy of our arithmetic coding modules.)
+patents owned by IBM, AT&T, and Mitsubishi.  Hence arithmetic coding cannot
+legally be used without obtaining one or more licenses.  For this reason,
+support for arithmetic coding has been removed from the free JPEG software.
+(Since arithmetic coding provides only a marginal gain over the unpatented
+Huffman mode, it is unlikely that very many implementations will support it.)
 So far as we are aware, there are no patent restrictions on the remaining
 code.
 
-
 We are required to state that
     "The Graphics Interchange Format(c) is the Copyright property of
     CompuServe Incorporated.  GIF(sm) is a Service Mark property of
     CompuServe Incorporated."
 
 
+REFERENCES
+==========
+
+We highly recommend reading one or more of these references before trying to
+understand the innards of the JPEG software.
+
+The best short technical introduction to the JPEG compression algorithm is
+	Wallace, Gregory K.  "The JPEG Still Picture Compression Standard",
+	Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
+(Adjacent articles in that issue discuss MPEG motion picture compression,
+applications of JPEG, and related topics.)  If you don't have the CACM issue
+handy, a PostScript file containing a revised version of Wallace's article is
+available at ftp.uu.net, graphics/jpeg/wallace.ps.gz.  The file (actually a
+preprint for an article to appear in IEEE Trans. Consumer Electronics) omits
+the sample images that appeared in CACM, but it includes corrections and some
+added material.  Note: the Wallace article is copyright ACM and IEEE, and it
+may not be used for commercial purposes.
+
+A somewhat less technical, more leisurely introduction to JPEG can be found in
+"The Data Compression Book" by Mark Nelson, published by M&T Books (Redwood
+City, CA), 1991, ISBN 1-55851-216-0.  This book provides good explanations and
+example C code for a multitude of compression methods including JPEG.  It is
+an excellent source if you are comfortable reading C code but don't know much
+about data compression in general.  The book's JPEG sample code is far from
+industrial-strength, but when you are ready to look at a full implementation,
+you've got one here...
+
+The best full description of JPEG is the textbook "JPEG Still Image Data
+Compression Standard" by William B. Pennebaker and Joan L. Mitchell, published
+by Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.  Price US$59.95, 638 pp.
+The book includes the complete text of the ISO JPEG standards (DIS 10918-1
+and draft DIS 10918-2).  This is by far the most complete exposition of JPEG
+in existence, and we highly recommend it.
+
+The JPEG standard itself is not available electronically; you must order a
+paper copy through ISO.  (Unless you feel a need to own a certified official
+copy, we recommend buying the Pennebaker and Mitchell book instead; it's much
+cheaper and includes a great deal of useful explanatory material.)  In the US,
+copies of the standard may be ordered from ANSI Sales at (212) 642-4900, or
+from Global Engineering Documents at (800) 854-7179.  (ANSI doesn't take
+credit card orders, but Global does.)  It's not cheap: as of 1992, ANSI was
+charging $95 for Part 1 and $47 for Part 2, plus 7% shipping/handling.  The
+standard is divided into two parts, Part 1 being the actual specification,
+while Part 2 covers compliance testing methods.  Part 1 is titled "Digital
+Compression and Coding of Continuous-tone Still Images, Part 1: Requirements
+and guidelines" and has document number ISO/IEC IS 10918-1.  As of mid-1994,
+Part 2 is still at Draft International Standard status.  It is titled "Digital
+Compression and Coding of Continuous-tone Still Images, Part 2: Compliance
+testing" and has document number ISO/IEC DIS 10918-2.  (The document number
+will change to IS 10918-2 when final approval is obtained.)  A Part 3,
+covering extensions, is likely to appear in draft form in late 1994.
+
+The JPEG standard does not specify all details of an interchangeable file
+format.  For the omitted details we follow the "JFIF" conventions, revision
+1.02.  A copy of the JFIF spec is available from:
+	Literature Department
+	C-Cube Microsystems, Inc.
+	1778 McCarthy Blvd.
+	Milpitas, CA 95035
+	phone (408) 944-6300,  fax (408) 944-6314
+A PostScript version of this document is available at ftp.uu.net, file
+graphics/jpeg/jfif.ps.gz.  It can also be obtained by e-mail from the C-Cube
+mail server, netlib@c3.pla.ca.us.  Send the message "send jfif_ps from jpeg"
+to the server to obtain the JFIF document; send the message "help" if you have
+trouble.
+
+The TIFF 6.0 file format specification can be obtained by FTP from sgi.com
+(192.48.153.1), file graphics/tiff/TIFF6.ps.Z; or you can order a printed copy
+from Aldus Corp. at (206) 628-6593.  It should be noted that the TIFF 6.0 spec
+of 3-June-92 has a number of serious problems in its JPEG features.  A
+redesign effort is currently underway to correct these problems; it is
+expected to result in a new, incompatible, spec.  IJG intends to support the
+corrected version of TIFF when the new spec is issued.
+
+
+ARCHIVE LOCATIONS
+=================
+
+The "official" archive site for this software is ftp.uu.net (Internet
+address 192.48.96.9).  The most recent released version can always be found
+there in directory graphics/jpeg.  This particular version will be archived
+as graphics/jpeg/jpegsrc.v5.tar.gz.  If you are on the Internet, you
+can retrieve files from ftp.uu.net by standard anonymous FTP.  If you don't
+have FTP access, UUNET's archives are also available via UUCP; contact
+help@uunet.uu.net for information on retrieving files that way.
+
+Numerous Internet sites maintain copies of the UUNET files; in particular,
+you can probably find a copy at any site that archives comp.sources.misc
+submissions.  However, only ftp.uu.net is guaranteed to have the latest
+official version.
+
+You can also obtain this software from CompuServe, in the GRAPHSUPPORT forum
+(GO GRAPHSUP); this version will be file jpsrc5.zip in library 15.  Again,
+CompuServe is not guaranteed to have the very latest version.
+
+The JPEG FAQ (Frequently Asked Questions) article is a useful source of
+general information about JPEG.  It is updated constantly and therefore
+is not included in this distribution.  The FAQ is posted every two weeks
+to Usenet newsgroups comp.graphics, news.answers, and other groups.  You
+can always obtain the latest version from the news.answers archive at
+rtfm.mit.edu (18.181.0.24).  By FTP, fetch /pub/usenet/news.answers/jpeg-faq.
+If you don't have FTP, send e-mail to mail-server@rtfm.mit.edu with body
+"send usenet/news.answers/jpeg-faq".
+
+
+RELATED SOFTWARE
+================
+
+Numerous viewing and image manipulation programs now support JPEG.  (Quite a
+few of them use this library to do so.)  The JPEG FAQ described above lists
+some of the more popular free and shareware viewers, and tells where to
+obtain them on Internet.
+
+If you are on a Unix machine, we highly recommend Jef Poskanzer's free
+PBMPLUS image software, which provides many useful operations on PPM-format
+image files.  In particular, it can convert PPM images to and from a wide
+range of other formats.  You can obtain this package by FTP from ftp.x.org
+(contrib/pbmplus*.tar.Z) or ftp.ee.lbl.gov (pbmplus*.tar.Z).  There is also
+a newer update of this package called NETPBM, available from
+wuarchive.wustl.edu under directory /graphics/graphics/packages/NetPBM/.
+Unfortunately PBMPLUS/NETPBM is not nearly as portable as the IJG software
+is; you are likely to have difficulty making it work on any non-Unix machine.
+
+A different free JPEG implementation, written by the PVRG group at Stanford,
+is available from havefun.stanford.edu in directory pub/jpeg.  This program
+is designed for research and experimentation rather than production use;
+it is slower, harder to use, and less portable than the IJG code, but it
+implements a larger subset of the JPEG standard.  In particular, it supports
+lossless JPEG.
+
+
+FILE FORMAT WARS
+================
+
+Some JPEG programs produce files that are not compatible with our library.
+The root of the problem is that the ISO JPEG committee failed to specify a
+concrete file format.  Some vendors "filled in the blanks" on their own,
+creating proprietary formats that no one else could read.  (For example, none
+of the early commercial JPEG implementations for the Macintosh were able to
+exchange compressed files.)
+
+The file format we have adopted is called JFIF (see REFERENCES).  This format
+has been agreed to by a number of major commercial JPEG vendors, and it has
+become the de facto standard.  JFIF is a minimal or "low end" representation.
+Work is also going forward to incorporate JPEG compression into the TIFF
+standard, for use in "high end" applications that need to record a lot of
+additional data about an image.  We intend to support TIFF in the future.
+We hope that these two formats will be sufficient and that other,
+incompatible JPEG file formats will not proliferate.
+
+Indeed, part of the reason for developing and releasing this free software is
+to help force rapid convergence to de facto standards for JPEG file formats.
+SUPPORT STANDARD, NON-PROPRIETARY FORMATS: demand JFIF or TIFF/JPEG!
+
+
 TO DO
 =====
 
-The next major release will probably be a significant rewrite to allow use of
-this code in conjunction with Sam Leffler's free TIFF library (assuming the
-bugs in the TIFF 6.0 specification get resolved).
+In future versions, we are considering supporting progressive JPEG
+compression, the upcoming JPEG Part 3 extensions, and other improvements.
 
-Many of the modules need fleshing out to provide more complete
-implementations, or to provide faster paths for common cases.
-Speeding things up is still high on our priority list.
-
-We'd appreciate it if people would compile and check out the code on as wide a
-variety of systems as possible, and report any portability problems
-encountered (with solutions, if possible).  Checks of file compatibility with
-other JPEG implementations would also be of interest.  Finally, we would
-appreciate code profiles showing where the most time is spent, especially on
-unusual systems.
+As always, speeding things up is high on our priority list.
 
 Please send bug reports, offers of help, etc. to jpeg-info@uunet.uu.net.
diff --git a/SETUP b/SETUP
deleted file mode 100644
index 85f0184..0000000
--- a/SETUP
+++ /dev/null
@@ -1,550 +0,0 @@
-SETUP instructions for the Independent JPEG Group's JPEG software
-=================================================================
-
-This file explains how to configure and compile the JPEG software.  We have
-tried to make this software extremely portable and flexible, so that it can be
-adapted to almost any environment.  The downside of this decision is that the
-installation process is not very automatic; you will need at least a little
-familiarity with C programming and program build procedures for your system.
-
-This file contains general instructions, then sections of specific hints for
-certain systems.  You may save yourself considerable time if you scan the
-whole file before starting to do anything.
-
-Before installing the software you must unpack the distributed source code.
-Since you are reading this file, you have probably already succeeded in this
-task.  However, there is one potential trap if you are on a non-Unix system:
-you may need to convert these files to the local standard text file format
-(for example, if you are on MS-DOS you probably have to convert LF end-of-line
-to CR/LF).  If so, apply the conversion to all the files EXCEPT those whose
-names begin with "test".  The test files contain binary data; if you change
-them in any way then the self-test will give bad results.
-
-
-STEP 1: PREPARE A MAKEFILE
-==========================
-
-First, select a makefile and copy it to "Makefile" (or whatever your version
-of make uses as the default makefile name; for example, "makefile.mak" for
-old versions of Borland C).  We include several standard makefiles in the
-distribution:
-
-	makefile.ansi:	for Unix systems with ANSI-compatible C compilers.
-	makefile.unix:	for Unix systems with non-ANSI C compilers.
-	makefile.mc5:	for Microsoft C 5.x under MS-DOS.
-	makefile.mc6:	for Microsoft C 6.x and up under MS-DOS.
-	makefile.bcc:	for Borland C (Turbo C) under MS-DOS.
-	makefile.icc:	for Intel's Code Builder C under MS-DOS.
-	makefile.manx:	for Manx Aztec C on Amigas.
-	makefile.sas:	for SAS C on Amigas.
-	makcjpeg.st:	project file for Atari ST/STE/TT Pure C or Turbo C.
-	makdjpeg.st:	project file for Atari ST/STE/TT Pure C or Turbo C.
-	makljpeg.st:	project file for Atari ST/STE/TT Pure C or Turbo C.
-	makefile.mms:	for VAX/VMS systems with MMS.
-	makefile.vms:	for VAX/VMS systems without MMS.
-
-If you don't see a makefile for your system, we recommend starting from either
-makefile.ansi or makefile.unix, depending on whether your compiler accepts
-ANSI C or not.  Actually you should start with makefile.ansi whenever your
-compiler supports ANSI-style function definitions; you don't need full ANSI
-compatibility.  The difference between the two makefiles is that makefile.unix
-preprocesses the source code to convert function definitions to old-style C.
-(Our thanks to Peter Deutsch of Aladdin Enterprises for the ansi2knr program.)
-
-If you don't know whether your compiler supports ANSI-style function
-definitions, then take a look at ckconfig.c.  It is a test program that will
-help you figure out this fact, as well as some other facts you'll need in
-later steps.  You must compile and execute ckconfig.c by hand; the makefiles
-don't provide any support for this.  ckconfig.c may not compile the first try
-(in fact, the whole idea is for it to fail if anything is going to).  If you
-get compile errors, fix them by editing ckconfig.c according to the directions
-given in ckconfig.c.  Once you get it to run, select a makefile according to
-the advice it prints out, and make any other changes it recommends.
-
-Look over the selected Makefile and adjust options as needed.  In particular
-you may want to change the CC and CFLAGS definitions.  For instance, if you
-are using GCC, set CC=gcc.  If you had to use any compiler switches to get
-ckconfig.c to work, make sure the same switches are in CFLAGS.
-
-If you are on a system that doesn't use makefiles, you'll need to set up
-project files (or whatever you do use) to compile all the source files and
-link them into executable files cjpeg and djpeg.  See the file lists in any of
-the makefiles to find out which files go into each program.  As a last resort,
-you can make a batch script that just compiles everything and links it all
-together; makefile.vms is an example of this (it's for VMS systems that have
-no make-like utility).
-
-
-STEP 2: EDIT JCONFIG.H
-======================
-
-Look over jconfig.h and adjust #defines to reflect the properties of your
-system and C compiler.  If you prefer, you can usually leave jconfig.h
-unmodified and add -Dsymbol switches to the Makefile's CFLAGS definition.
-(This is already done if you used a compiler-specific makefile in step 1.)
-However, putting the switches in the Makefile is a bad idea if you are going
-to incorporate the JPEG software into other programs --- you'd need to include
-the same -D switches in the other programs' Makefiles.  Better to change
-jconfig.h.
-
-If you have an ANSI-compliant C compiler, no changes should be necessary
-except perhaps for RIGHT_SHIFT_IS_UNSIGNED and TWO_FILE_COMMANDLINE.  For
-older compilers other changes may be needed, depending on what ANSI features
-are supported.
-
-If you don't know enough about C programming to understand the questions in
-jconfig.h, then use ckconfig.c to figure out what to change.  (See description
-of ckconfig.c in step 1.)
-
-A note about TWO_FILE_COMMANDLINE: defining this selects the command line
-syntax in which the input and output files are both named on the command line.
-If it's not defined, the output image goes to standard output, and the input
-can optionally come from standard input.  You MUST use two-file style on any
-system that doesn't cope well with binary data fed through stdin/stdout; this
-is true for most MS-DOS compilers, for example.  If you're not on a Unix
-system, it's probably safest to assume you need two-file style.  (But if your
-compiler provides either the Posix-standard fdopen() library routine or a
-Microsoft-compatible setmode() routine, you can use the Unix command line
-style, by defining USE_FDOPEN or USE_SETMODE respectively.)
-
-
-STEP 3: SELECT SYSTEM-DEPENDENT FILES
-=====================================
-
-A few places in the JPEG software are so system-dependent that we have to
-provide several different implementations and let you select the one you need.
-
-The only system-dependent file in the current version is jmemsys.c.  This file
-controls use of temporary files for big images that won't fit in main memory.
-You'll notice there is no file named jmemsys.c in the distribution; you must
-select one of the provided versions and copy, rename, or link it to jmemsys.c.
-Here are the provided versions:
-
-	jmemansi.c	This is a reasonably portable version that should
-			work on most ANSI and near-ANSI C compilers.  It uses
-			the ANSI-standard library routine tmpfile(), which not
-			all non-ANSI systems have.  On some systems tmpfile()
-			may put the temporary file in a non-optimal location;
-			if you don't like what it does, use jmemname.c.
-
-	jmemname.c	This version constructs the temp file name by itself.
-			For anything except a Unix machine, you'll need to
-			configure the select_file_name() routine appropriately;
-			see the comments near the head of jmemname.c.
-			If you use this version, define NEED_SIGNAL_CATCHER
-			in jconfig.h or in the Makefile to make sure the temp
-			files are removed if the program is aborted.
-
-	jmemnobs.c	(That stands for No Backing Store :-).  This will
-			compile on almost any system, but it assumes you
-			have enough main memory or virtual memory to hold
-			the biggest images you need to work with.
-
-	jmemdos.c	This should be used in most MS-DOS installations; see
-			the system-specific notes about MS-DOS for more info.
-			IMPORTANT: if you use this, also copy jmemdos.h to
-			jmemsys.h, replacing the standard version.  ALSO,
-			include the assembly file jmemdosa.asm in the programs.
-			(This last is already done if you used one of the
-			supplied MS-DOS-specific makefiles.)
-
-If you have plenty of (real or virtual) main memory, just use jmemnobs.c.
-"Plenty" means at least ten bytes for every pixel in the largest images
-you plan to process, so a lot of systems don't meet this criterion.
-If yours doesn't, try jmemansi.c first.  If that doesn't compile, you'll have
-to use jmemname.c; be sure to adjust select_file_name() for local conditions.
-You may also need to change unlink() to remove() in close_backing_store().
-
-Except with jmemnobs.c, you need to adjust the #define DEFAULT_MAX_MEM to a
-reasonable value for your system (either by editing jmemsys.c, or by adding
-a -D switch to the Makefile).  This value limits the amount of data space the
-program will attempt to allocate.  Code and static data space isn't counted,
-so the actual memory needs for cjpeg or djpeg are typically 100 to 150Kb more
-than the max-memory setting.  Larger max-memory settings reduce the amount of
-I/O needed to process a large image, but too large a value can result in
-"insufficient memory" failures.  On most Unix machines (and other systems with
-virtual memory), just set DEFAULT_MAX_MEM to several million and forget it.
-At the other end of the spectrum, for MS-DOS machines you probably can't go
-much above 300K to 400K.  (On MS-DOS the value refers to conventional memory;
-extended/expanded memory is handled separately by jmemdos.c.)
-
-
-STEP 4: MAKE
-============
-
-Now you should be able to "make" the software.
-
-If you have trouble with missing system include files or inclusion of the
-wrong ones, look at jinclude.h (or use ckconfig.c, if you are not a C expert).
-
-If your compiler complains about big_sarray_control and big_barray_control
-being undefined structures, you should be able to shut it up by adding
--DINCOMPLETE_TYPES_BROKEN to CFLAGS (or add #define INCOMPLETE_TYPES_BROKEN
-to jconfig.h).  If you don't have a getenv() library routine, define NO_GETENV.
-
-There are a fair number of routines that do not use all of their parameters;
-some compilers will issue warnings about this, which you can ignore.  Any
-other warning deserves investigation.
-
-
-STEP 5: TEST
-============
-
-As a quick test of functionality we've included a small sample image in
-several forms:
-	testorig.jpg	A reduced section of the well-known Lenna picture.
-	testimg.ppm	The output of djpeg testorig.jpg
-	testimg.gif	The output of djpeg -gif testorig.jpg
-	testimg.jpg	The output of cjpeg testimg.ppm
-(The two .jpg files aren't identical since JPEG is lossy.)  If you can
-generate duplicates of the testimg.* files then you probably have working
-programs.
-
-With most of the makefiles, "make test" will perform the necessary
-comparisons.  If you started with makefile.ansi or makefile.unix, and you
-defined TWO_FILE_COMMANDLINE, then change the makefile's test script to use
-two-file syntax (i.e., delete the ">" character from the invocations of cjpeg
-and djpeg).  The other makefiles will work with either command-line syntax.
-
-If you're using a makefile that doesn't provide the test option, run djpeg and
-cjpeg by hand to generate testout.ppm, testout.gif, and testout.jpg, then
-compare these to testimg.* with whatever binary file comparison tool you have.
-The files should be bit-for-bit identical.
-
-If the cjpeg test run fails with "Missing Huffman code table entry", it's a
-good bet that you needed to define RIGHT_SHIFT_IS_UNSIGNED.  Go back to step 2
-and run ckconfig.c.  (This is a good plan for any other test failure, too.)
-
-If you are using Unix (one-file) command line style on a non-Unix system,
-it's a good idea to check that binary I/O through stdin/stdout actually works.
-You should get the same results from "djpeg <testorig.jpg >out.ppm" as from
-"djpeg -outfile out.ppm  testorig.jpg".  Note that the non-Unix makefiles use
-the latter style and therefore do not exercise stdin/stdout.  If this test
-fails, try recompiling jcmain.c & jdmain.c with USE_SETMODE and/or USE_FDOPEN.
-
-If your choice of jmemsys.c was anything other than jmemnobs.c, you should
-test that temporary-file usage works.  Try "djpeg -gif -max 0 testorig.jpg"
-and make sure its output matches testimg.gif.  If you have any really large
-images handy, try compressing them with -optimize and/or decompressing with
--gif to make sure your DEFAULT_MAX_MEM setting is not too large.
-
-NOTE: this is far from an exhaustive test of the JPEG software; some modules,
-such as 1-pass color quantization, are not exercised at all.  It's just a quick
-test to give you some confidence that you haven't missed something major.
-
-
-STEP 6: INSTALLATION
-====================
-
-Once you're done with the above steps, you can install the software by copying
-the executable files (cjpeg and djpeg) to wherever you normally install
-programs.  On Unix systems, you'll also want to put cjpeg.1 and djpeg.1 in the
-corresponding manual directory.  (The makefiles don't support this step since
-there's such a wide variety of installation procedures on different systems.)
-
-To learn to use the programs, read the file USAGE (or manual pages cjpeg(1)
-and djpeg(1) on Unix).  Note that the man pages cjpeg.1/djpeg.1 only describe
-the Unix-style command line syntax; if you want to use these files with a
-version that uses two-file command line syntax, you'll have to modify the text
-accordingly.  The USAGE file describes both styles.
-
-
-OPTIMIZATION
-============
-
-Unless you own a Cray, you'll probably be interested in making the JPEG
-software go as fast as possible.  This section covers some machine-dependent
-optimizations you may want to try.  We suggest that before trying any of this,
-you first get the basic installation to pass the self-test (step 5 above).
-Repeat the self-test after any optimization to make sure that you haven't
-broken anything.
-
-The JPEG DCT routines perform a lot of multiplications.  These multiplications
-must yield 32-bit results, but none of their input values are more than 16
-bits wide.  On many machines, notably the 680x0 and 80x86 CPUs, a 16x16=>32
-bit multiply instruction is faster than a full 32x32=>32 bit multiply.
-Unfortunately there is no portable way to specify such a multiplication in C,
-but some compilers can generate one when you use the right combination of
-casts.  See the MULTIPLY macro definitions in jfwddct.c and jrevdct.c.
-If your compiler makes "int" be 32 bits and "short" be 16 bits, defining
-SHORTxSHORT_32 is fairly likely to work.  When experimenting with alternate
-definitions, be sure to test not only whether the code still works (use the
-self-test step), but also whether it is actually faster --- on some compilers,
-alternate definitions may compute the right answer, yet be slower than the
-default.  Timing cjpeg on a large PPM input file is the best way to check
-this, as the DCT will be the largest fraction of the runtime in that mode.
-(Note: some of the distributed compiler-specific makefiles already contain
--D switches to select an appropriate MULTIPLY definition.)
-
-If access to "short" arrays is slow on your machine, it may be a win to define
-type DCTELEM as int rather than as JCOEF (which is normally defined as short).
-This will cause the DCT routines to operate on int arrays instead of short
-arrays.  If shorts are slow and you have lots of memory to burn, you might
-even make JCOEF itself be int.
-
-If your compiler can compile function calls in-line, make sure the INLINE
-macro in jconfig.h is defined as the keyword that marks a function
-inline-able.  Some compilers have a switch that tells the compiler to inline
-any function it thinks is profitable (e.g., -finline-functions for gcc).
-Enabling such a switch is likely to make the compiled code bigger but faster.
-
-In general, it's worth trying the maximum optimization level of your compiler,
-and experimenting with any optional optimizations such as loop unrolling.
-(Unfortunately, far too many compilers have optimizer bugs ... be prepared to
-back off if the code fails self-test.)  If you do any experimentation along
-these lines, please report the optimal settings to jpeg-info@uunet.uu.net so
-we can mention them in future releases.  Be sure to specify your machine and
-compiler version.
-
-
-OPTIONAL STUFF
-==============
-
-Progress monitor:
-
-If you like, you can #define PROGRESS_REPORT (in jconfig.h or in the Makefile)
-to enable display of percent-done progress reports.  The routines provided in
-jcmain.c/jdmain.c merely print percentages to stderr, but you can customize
-them to do something fancier.
-
-Utah RLE file format support:
-
-We distribute the software with support for RLE image files (Utah Raster
-Toolkit format) disabled, because the RLE support won't compile without the
-Utah library.  If you have URT version 3.0, you can enable RLE support as
-follows:
-	1.  #define RLE_SUPPORTED in jconfig.h or in the Makefile.
-	2.  Add a -I option to CFLAGS in the Makefile for the directory
-	    containing the URT .h files (typically the "include"
-	    subdirectory of the URT distribution).
-	3.  Add -L... -lrle to LDLIBS in the Makefile, where ... specifies
-	    the directory containing the URT "librle.a" file (typically the
-	    "lib" subdirectory of the URT distribution).
-
-JPEG library:
-
-If you want to incorporate the JPEG code as subroutines in a larger program,
-we recommend that you make libjpeg.a, then link that into your surrounding
-program.  See file README for more info.
-
-CAUTION: When you use the JPEG code as subroutines, we recommend that you make
-any required configuration changes by modifying jconfig.h, not by adding -D
-switches to the Makefile.  Otherwise you must be sure to provide the same -D
-switches when compiling any program that includes the JPEG .h files, to ensure
-that the parameter structures are interpreted the same way.  (This is only
-critical for the first few symbols mentioned in jconfig.h, down through
-NEED_FAR_POINTERS.)
-
-Removing code:
-
-If you need to make a smaller version of the JPEG software, some optional
-functions can be removed at compile time.  See the xxx_SUPPORTED #defines in
-jconfig.h.  If at all possible, we recommend that you leave in decoder support
-for all valid JPEG files, to ensure that you can read anyone's output.
-Restricting your encoder, or removing optional functions like block smoothing,
-won't hurt compatibility.  Taking out support for image file formats that you
-don't use is the most painless way to make the programs smaller.
-
-
-NOTES FOR SPECIFIC SYSTEMS
-==========================
-
-We welcome reports on changes needed for systems not mentioned here.
-Submit 'em to jpeg-info@uunet.uu.net.  Also, if ckconfig.c is wrong about
-how to configure the JPEG software for your system, please let us know.
-
-
-Amiga:
-
-Makefiles are provided for Manx Aztec C and SAS C.  I have also heard from
-people who have compiled with the free DICE compiler, using makefile.ansi as a
-starting point (set "CC= dcc" and "CFLAGS= -c -DAMIGA -DTWO_FILE_COMMANDLINE
--DNEED_SIGNAL_CATCHER" in the makefile).  For all compilers, we recommend you
-use jmemname.c as the system-dependent memory manager.  Assuming you have
--DAMIGA in the makefile, jmemname.c will put temporary files in JPEGTMP:.
-Change jmemname.c if you don't like this.
-
-
-Atari:
-
-The project files provided should work as-is with Pure C.  For Turbo C, change
-library filenames "PC..." to "TC..." in the project files for cjpeg.ttp and
-djpeg.ttp.  Don't forget to select a jmemsys.c file, see Step 3 (we recommend
-jmemansi.c).  Also adjust the DEFAULT_MAX_MEM setting --- you probably want it
-to be a couple hundred K less than your normal free memory.  Note that you
-must make jpeg.lib before making cjpeg.ttp or cjpeg.ttp.  You'll have to
-perform the self-test (Step 5) by hand.
-
-There is a bug in some older versions of the Turbo C library which causes the
-space used by temporary files created with "tmpfile()" not to be freed after
-an abnormal program exit.  If you check your disk afterwards, you will find
-cluster chains that are allocated but not used by a file.  This should not
-happen in cjpeg or djpeg, since we enable a signal catcher to explicitly close
-temp files before exiting.  But if you use the JPEG library with your own
-code, be sure to supply a signal catcher, or else use a different
-system-dependent memory manager.
-
-
-Cray:
-
-Should you be so fortunate as to be running JPEG on a Cray YMP, there is a
-compiler bug in Cray's Standard C versions prior to 3.1.  You'll need to
-insert a line reading "#pragma novector" just before the loop	
-    for (i = 1; i <= (int) htbl->bits[l]; i++)
-      huffsize[p++] = (char) l;
-in fix_huff_tbl (in V4A, line 42 of jchuff.c and line 39 of jdhuff.c).  The
-usual symptom of not adding this line is a core-dump.  See Cray's SPR 48222.
-
-
-HP/Apollo DOMAIN:
-
-With system release 10.4 or later, makefile.ansi should work OK.  If you have
-version 10.3.anything, you need to figure out whether you have the ANSI C
-compiler (version 6.7 or later) and whether you've installed the ANSI C
-include files (if so, the first line of <stdio.h> will mention ANSI C).
-If you have the ANSI C compiler but not the ANSI C include files, use
-makefile.ansi and add -DNONANSI_INCLUDES to CFLAGS.  If you have both,
-then makefile.ansi should work as is.  If neither, use makefile.unix.
-
-
-HP-UX:
-
-If you have HP-UX 7.05 or later with the "software development" C compiler,
-then you can use makefile.ansi.  Add "-Aa" to the CFLAGS line in the makefile
-to make the compiler work in ANSI mode.  If you have a pre-7.05 system, or if
-you are using the non-ANSI C compiler delivered with a minimum HP-UX 8.0
-system, then you must use makefile.unix (and do NOT add -Aa).  Also, adding
-"-lmalloc" to LDLIBS is recommended if you have libmalloc.a (it seems not to
-be present in minimum 8.0).
-
-On HP 9000 series 800 machines, the HP C compiler is buggy in revisions prior
-to A.08.07.  If you get complaints about "not a typedef name", you'll have to
-convert the code to K&R style (i.e., use makefile.unix).
-
-
-Macintosh MPW:
-
-We don't directly support MPW in the current release, but Larry Rosenstein
-reports that the JPEG code can be ported without very much trouble.  There's
-useful notes and conversion scripts in his kit for porting PBMPLUS to MPW.
-You can obtain the kit by FTP to ftp.apple.com, file /pub/lsr/pbmplus-port*.
-
-
-Macintosh Think C:
-
-You'll have to prepare project files for cjpeg and djpeg; we don't include
-those in the distribution since they are not text files.  The COBJECTS and
-DOBJECTS lists in makefile.unix show which files should be included in each
-project.  Also add the ANSI and Unix C libraries in a separate segment.  You
-may need to divide the JPEG files into more than one segment; you can do this
-pretty much as you please.
-
-If you have Think C version 5.0 you need not modify jconfig.h; instead you
-should turn on both the ANSI Settings and Language Extensions option buttons
-(so that both __STDC__ and THINK_C are predefined).  With version 4.0 you must
-edit jconfig.h.  (You can #define HAVE_STDC to do the right thing for all
-options except const; you must also #define const.)
-
-jcmain and jdmain are set up to provide the usual command-line interface
-by means of Think's ccommand() library routine.  A more Mac-like interface
-is in the works.
-
-
-MS-DOS, generic comments:
-
-The JPEG code is designed to be compiled with 80x86 "small" or "medium" memory
-models (i.e., data pointers are 16 bits unless explicitly declared "far"; code
-pointers can be either size).  You should be able to use small model to
-compile cjpeg or djpeg by itself, but you will probably have to go to medium
-model if you include the JPEG code in a larger application.  This shouldn't
-hurt performance much.  You *will* take a noticeable performance hit if you
-compile in a large-data memory model, and you should avoid "huge" model if at
-all possible.  Be sure that NEED_FAR_POINTERS is defined by jconfig.h or by
-the Makefile if you use a small-data model; be sure it is NOT defined if you
-use a large-data memory model.  (As distributed, jconfig.h defines
-NEED_FAR_POINTERS if MSDOS is defined.)
-
-The DOS-specific memory manager, jmemdos.c, should be used if possible.
-(Be sure to install jmemdos.h and jmemdosa.asm along with it.)  If you
-can't use jmemdos.c for some reason --- for example, because you don't have
-a Microsoft-compatible assembler to assemble jmemdosa.asm --- you'll have
-to fall back to jmemansi.c or jmemname.c.  IMPORTANT: if you use either of
-the latter two files, you will have to compile in a large-data memory model
-in order to get the right stdio library.  Too bad.
-
-None of the above advice applies if you are using a 386 flat-memory-space
-environment, such as DJGPP or Watcom C.  (And you should use one if you have
-it, as performance will be much better than 8086-compatible code!)  For
-flat-memory-space compilers, do NOT define NEED_FAR_POINTERS, and do NOT use
-jmemdos.c.  Use jmemnobs.c if the environment supplies adequate virtual
-memory, otherwise use jmemansi.c or jmemname.c.
-
-Most MS-DOS compilers treat stdin/stdout as text files, so you must use
-two-file command line style.  But if your compiler has either fdopen() or
-setmode(), you can use one-file style if you like.  To do this, define
-USE_FDOPEN or USE_SETMODE so that stdin/stdout will be set to binary mode.
-(USE_SETMODE seems to work with more DOS compilers than USE_FDOPEN.)  You
-should test that I/O through stdin/stdout produces the same results as I/O
-to explicitly named files... the "make test" procedures in the DOS-specific
-makefiles do NOT use stdin/stdout.
-
-If you add more switches to CFLAGS in the DOS-specific makefiles, you are
-likely to run up against DOS' 128-byte command line length limit.  In that
-case, remove some "-Dsymbol" switches from CFLAGS and instead put
-corresponding "#define symbol" lines at the head of jinclude.h.
-
-
-MS-DOS, Borland C:
-
-Be sure to convert all the source files to DOS text format (CR/LF newlines).
-Although Borland C will often work OK with unmodified Unix (LF newlines)
-source files, sometimes it will give bogus compile errors.
-"Illegal character '#'" is the most common such error.
-
-Some versions of Borland's MAKE erroneously display the warning message about
-creating jmemsys.c, even after you have done so.  If this happens to you,
-delete the four lines beginning with "jmemsys.c:" from the Makefile.
-
-If you want one-file command line style, define USE_SETMODE.  fdopen() does
-not work correctly.
-
-
-MS-DOS, DJGPP:
-
-Use makefile.ansi and jmemnobs.c, and put "-UMSDOS" in CFLAGS to undo the
-compiler's automatic definition of MSDOS.  Also put either "-DUSE_SETMODE" or
-"-DTWO_FILE_COMMANDLINE" in CFLAGS, depending on whether you prefer one-file
-or two-file command line style.  You'll also need to put the object-file lists
-into response files in order to circumvent DOS's 128-byte command line length
-limit at the final linking step.
-
-
-MS-DOS, Microsoft C:
-
-Old versions of MS C fail with an "out of macro expansion space" error
-because they can't cope with the macro TRACEMS8 (defined in jpegdata.h).
-If this happens to you, the easiest solution is to change TRACEMS8 to
-expand to nothing.  You'll lose the ability to dump out JPEG coefficient
-tables with djpeg -debug -debug, but at least you can compile.
-
-Original MS C 6.0 is buggy; it compiles incorrect code unless you turn off
-optimization (remove -O from CFLAGS).  That problem seems to have been fixed
-in 6.00A and later versions.  6.00A still generates a bogus "conditional
-expression is constant" warning in jrdppm.c, but the emitted code seems OK.
-
-If you want one-file command line style, define USE_SETMODE.  fdopen() does
-not work correctly, at least not in 6.00A.
-
-
-SGI:
-
-Use makefile.ansi, but set "AR2= ar -ts" rather than "AR2= ranlib".  Also
-make any changes recommended by ckconfig.c.
-
-
-Sun:
-
-Don't forget to add -DBSD to CFLAGS.  If you are using GCC on SunOS 4.0.1 or
-earlier, you will need to add -DNONANSI_INCLUDES to CFLAGS (your compiler may
-be ANSI, but your system include files aren't).  I've gotten conflicting
-reports on whether this is still necessary on SunOS 4.1 or later.
diff --git a/USAGE b/USAGE
deleted file mode 100644
index 76044f8..0000000
--- a/USAGE
+++ /dev/null
@@ -1,308 +0,0 @@
-USAGE instructions for the Independent JPEG Group's JPEG software
-=================================================================
-
-INTRODUCTION
-
-This distribution contains software to implement JPEG image compression and
-decompression.  JPEG (pronounced "jay-peg") is a standardized compression
-method for full-color and gray-scale images.  JPEG is designed to handle
-"real-world" scenes, for example scanned photographs.  Cartoons, line
-drawings, and other non-realistic images are not JPEG's strong suit; on this
-sort of material you may get poor image quality and/or little compression.
-
-JPEG is lossy, meaning that the output image is not necessarily identical to
-the input image.  Hence you should not use JPEG if you have to have identical
-output bits.  However, on typical real-world images, very good compression
-levels can be obtained with no visible change, and amazingly high compression
-is possible if you can tolerate a low-quality image.  You can trade off image
-quality against file size by adjusting the compressor's "quality" setting.
-
-This file describes usage of the standard programs "cjpeg" and "djpeg" that
-can be built directly from the distributed C code.  See the README file for
-hints on incorporating the JPEG software into other programs.
-
-If you are on a Unix machine you may prefer to read the Unix-style manual
-pages in files cjpeg.1 and djpeg.1.  But also see the HINTS section below,
-which is not present in either manual page.
-
-NOTE: the switch syntax has been redesigned since the v3 release of
-cjpeg/djpeg.  Switch names are now words instead of single letters.
-
-
-GENERAL USAGE
-
-We provide two programs, cjpeg to compress an image file into JPEG format,
-and djpeg to decompress a JPEG file back into a conventional image format.
-
-On Unix-like systems, you say:
-	cjpeg [switches] [imagefile] >jpegfile
-or
-	djpeg [switches] [jpegfile]  >imagefile
-The programs read the specified input file, or standard input if none is
-named.  They always write to standard output (with trace/error messages to
-standard error).  These conventions are handy for piping images between
-programs.
-
-On most non-Unix systems, you say:
-	cjpeg [switches] imagefile jpegfile
-or
-	djpeg [switches] jpegfile  imagefile
-i.e., both the input and output files are named on the command line.  This
-style is a little more foolproof, and it loses no functionality if you don't
-have pipes.  (You can get this style on Unix too, if you prefer, by defining
-TWO_FILE_COMMANDLINE when you compile the programs; see SETUP.)
-
-You can also say:
-	cjpeg [switches] -outfile jpegfile  imagefile
-or
-	djpeg [switches] -outfile imagefile  jpegfile
-This syntax works on all systems, so it is useful for scripts.
-
-The currently supported image file formats are: PPM (PBMPLUS color format),
-PGM (PBMPLUS gray-scale format), GIF, Targa, and RLE (Utah Raster Toolkit
-format).  (RLE is supported only if the URT library is available.)
-cjpeg recognizes the input image format automatically, with the exception
-of some Targa-format files.  You have to tell djpeg which format to generate.
-
-The only JPEG file format currently supported is the JFIF format.  Support for
-the TIFF 6.0 JPEG format will probably be added at some future date.
-
-All switch names may be abbreviated; for example, -grayscale may be written
--gray or -gr.  Most of the "basic" switches can be abbreviated to as little as
-one letter.  Upper and lower case are equivalent (-GIF is the same as -gif).
-British spellings are also accepted (e.g., -greyscale), though for brevity
-these are not mentioned below.
-
-
-CJPEG DETAILS
-
-The basic command line switches for cjpeg are:
-
-	-quality N	Scale quantization tables to adjust image quality.
-			Quality is 0 (worst) to 100 (best); default is 75.
-			(See below for more info.)
-
-	-grayscale	Create monochrome JPEG file from color input.
-			Be sure to use this switch when compressing a grayscale
-			GIF file, because cjpeg isn't bright enough to notice
-			whether a GIF file uses only shades of gray.  By
-			saying -grayscale, you'll get a smaller JPEG file that
-			takes less time to process.
-
-	-optimize	Perform optimization of entropy encoding parameters.
-			Without this, default encoding parameters are used.
-			-optimize usually makes the JPEG file a little smaller,
-			but cjpeg runs somewhat slower and needs much more
-			memory.  Image quality and speed of decompression are
-			unaffected by -optimize.
-
-	-targa		Input file is Targa format.  Targa files that contain
-			an "identification" field will not be automatically
-			recognized by cjpeg; for such files you must specify
-			-targa to make cjpeg treat the input as Targa format.
-
-The -quality switch lets you trade off compressed file size against quality of
-the reconstructed image: the higher the quality setting, the larger the JPEG
-file, and the closer the output image will be to the original input.  Normally
-you want to use the lowest quality setting (smallest file) that decompresses
-into something visually indistinguishable from the original image.  For this
-purpose the quality setting should be between 50 and 95; the default of 75 is
-often about right.  If you see defects at -quality 75, then go up 5 or 10
-counts at a time until you are happy with the output image.  (The optimal
-setting will vary from one image to another.)
-
--quality 100 will generate a quantization table of all 1's, eliminating loss
-in the quantization step (but there is still information loss in subsampling,
-as well as roundoff error).  This setting is mainly of interest for
-experimental purposes.  Quality values above about 95 are NOT recommended for
-normal use; the compressed file size goes up dramatically for hardly any gain
-in output image quality.
-
-In the other direction, quality values below 50 will produce very small files
-of low image quality.  Settings around 5 to 10 might be useful in preparing an
-index of a large image library, for example.  Try -quality 2 (or so) for some
-amusing Cubist effects.  (Note: quality values below about 25 generate 2-byte
-quantization tables, which are considered optional in the JPEG standard.
-cjpeg emits a warning message when you give such a quality value, because
-some commercial JPEG programs may be unable to decode the resulting file.)
-
-Switches for advanced users:
-
-	-maxmemory N	Set limit for amount of memory to use in processing
-			large images.  Value is in thousands of bytes, or
-			millions of bytes if "M" is attached to the number.
-			For example, -max 4m selects 4000000 bytes.  If more
-			space is needed, temporary files will be used.
-
-	-restart N	Emit a JPEG restart marker every N MCU rows, or every
-			N MCU blocks if "B" is attached to the number.
-			-restart 0 (the default) means no restart markers.
-
-	-smooth N	Smooth the input image to eliminate dithering noise.
-			N, ranging from 1 to 100, indicates the strength of
-			smoothing.  0 (the default) means no smoothing.
-
-	-verbose	Enable debug printout.  More -v's give more printout.
-	or  -debug	Also, version information is printed at startup.
-
-The -restart option inserts extra markers that allow a JPEG decoder to
-resynchronize after a transmission error.  Without restart markers, any damage
-to a compressed file will usually ruin the image from the point of the error
-to the end of the image; with restart markers, the damage is usually confined
-to the portion of the image up to the next restart marker.  Of course, the
-restart markers occupy extra space.  We recommend -restart 1 for images that
-will be transmitted across unreliable networks such as Usenet.
-
-The -smooth option filters the input to eliminate fine-scale noise.  This is
-often useful when converting GIF files to JPEG: a moderate smoothing factor of
-10 to 50 gets rid of dithering patterns in the input file, resulting in a
-smaller JPEG file and a better-looking image.  Too large a smoothing factor
-will visibly blur the image, however.
-
-Switches for wizards:
-
-	-arithmetic	Use arithmetic coding rather than Huffman coding.
-			(Not currently supported for legal reasons.)
-
-	-nointerleave	Generate noninterleaved JPEG file (not yet supported).
-
-	-qtables file	Use the quantization tables given in the specified
-			file.  The file should contain one to four tables
-			(64 values each) as plain text.  Comments preceded by
-			'#' may be included in the file.  The tables are
-			implicitly numbered 0,1,etc.  If -quality N is also
-			specified, the values in the file are scaled according
-			to cjpeg's quality scaling curve.
-
-	-sample HxV[,...]	Set JPEG sampling factors.  If you specify
-			fewer H/V pairs than there are components, the
-			remaining components are set to 1x1 sampling.  The
-			default setting is equivalent to "-sample 2x2".
-
-The "wizard" switches are intended for experimentation with JPEG.  If you
-don't know what you are doing, DON'T USE THEM.  You can easily produce files
-with worse image quality and/or poorer compression than you'll get from the
-default settings.  Furthermore, these switches should not be used when making
-files intended for general use, because not all JPEG implementations will
-support unusual JPEG parameter settings.
-
-
-DJPEG DETAILS
-
-The basic command line switches for djpeg are:
-
-	-colors N	Reduce image to at most N colors.  This reduces the
-	or -quantize N	number of colors used in the output image, so that it
-			can be displayed on a colormapped display or stored in
-			a colormapped file format.  For example, if you have
-			an 8-bit display, you'd need to reduce to 256 or fewer
-			colors.  (-colors is the recommended name, -quantize
-			is provided only for backwards compatibility.)
-
-	-gif		Select GIF output format.  Since GIF does not support
-			more than 256 colors, -colors 256 is assumed (unless
-			you specify a smaller number of colors).
-
-	-pnm		Select PBMPLUS (PPM/PGM) output format (this is the
-			default format).  PGM is emitted if the JPEG file is
-			gray-scale or if -grayscale is specified; otherwise
-			PPM is emitted.
-
-	-rle		Select RLE output format.  (Requires URT library.)
-
-	-targa		Select Targa output format.  Gray-scale format is
-			emitted if the JPEG file is gray-scale or if
-			-grayscale is specified; otherwise, colormapped format
-			is emitted if -colors is specified; otherwise, 24-bit
-			full-color format is emitted.
-
-Switches for advanced users:
-
-	-blocksmooth	Perform cross-block smoothing.  This is slow, quite
-			memory-intensive, and only seems to improve the image
-			at very low quality settings (-quality 10 to 20 or so).
-			At normal quality settings it may make things worse.
-
-	-grayscale	Force gray-scale output even if JPEG file is color.
-			Useful for viewing on monochrome displays.
-
-	-maxmemory N	Set limit for amount of memory to use in processing
-			large images.  Value is in thousands of bytes, or
-			millions of bytes if "M" is attached to the number.
-			For example, -max 4m selects 4000000 bytes.  If more
-			space is needed, temporary files will be used.
-
-	-nodither	Do not use dithering in color quantization.
-			By default, Floyd-Steinberg dithering is applied when
-			quantizing colors, but on some images dithering may
-			result in objectionable "graininess".  If that
-			happens, you can turn off dithering with -nodither.
-			-nodither is ignored unless you also say -colors N.
-
-	-onepass	Use one-pass instead of two-pass color quantization.
-			The one-pass method is faster and needs less memory,
-			but it produces a lower-quality image.  -onepass is
-			ignored unless you also say -colors N.  Also,
-			the one-pass method is always used for gray-scale
-			output (the two-pass method is no improvement then).
-
-	-verbose	Enable debug printout.  More -v's give more printout.
-	or  -debug	Also, version information is printed at startup.
-
-
-HINTS
-
-Color GIF files are not the ideal input for JPEG; JPEG is really intended for
-compressing full-color (24-bit) images.  In particular, don't try to convert
-cartoons, line drawings, and other images that have only a few distinct
-colors.  GIF works great on these, JPEG does not.  If you want to convert a
-GIF to JPEG, you should experiment with cjpeg's -quality and -smooth options
-to get a satisfactory conversion.  -smooth 10 or so is often helpful.
-
-Avoid running an image through a series of JPEG compression/decompression
-cycles.  Image quality loss will accumulate; after ten or so cycles the image
-may be noticeably worse than it was after one cycle.  It's best to use a
-lossless format while manipulating an image, then convert to JPEG format when
-you are ready to file the image away.
-
-The -optimize option to cjpeg is worth using when you are making a "final"
-version for posting or archiving.  It's also a win when you are using low
-quality settings to make very small JPEG files; the percentage improvement
-is often a lot more than it is on larger files.
-
-When making images to be posted on Usenet, we recommend using cjpeg's option
--restart 1.  This option limits the damage done to a compressed image by
-netnews transmission errors.
-
-The default memory usage limit (-maxmemory) is set when the software is
-compiled.  If you get an "insufficient memory" error, try specifying a smaller
--maxmemory value, even -maxmemory 0 to use the absolute minimum space.  You
-may want to recompile with a smaller default value if this happens often.
-
-On machines that have "environment" variables, you can define the environment
-variable JPEGMEM to set the default memory limit.  The value is specified as
-described for the -maxmemory switch.  JPEGMEM overrides the default value
-specified when the program was compiled, and itself is overridden by an
-explicit -maxmemory switch.
-
-On MS-DOS machines, -maxmemory is the amount of main (conventional) memory to
-use.  (Extended or expanded memory is also used if available.)  Most
-DOS-specific versions of this software do their own memory space estimation
-and do not need -maxmemory.
-
-djpeg with two-pass color quantization requires a good deal of memory; on
-MS-DOS machines it may run out of memory even with -maxmemory 0.  In that case
-you can still decompress, with some loss of image quality, by specifying
--onepass for one-pass quantization.
-
-If more space is needed than will fit in the available main memory (as
-determined by -maxmemory), temporary files will be used.  (MS-DOS versions
-will try to get extended or expanded memory first.)  The temporary files are
-often rather large: in typical cases they occupy three bytes per pixel, for
-example 3*800*600 = 1.44Mb for an 800x600 image.  If you don't have enough
-free disk space, leave out -optimize (for cjpeg) or specify -onepass (for
-djpeg).  On MS-DOS, the temporary files are created in the directory named by
-the TMP or TEMP environment variable, or in the current directory if neither
-of those exist.  Amiga implementations put the temp files in the directory
-named by JPEGTMP:, so be sure to assign JPEGTMP: to a disk partition with
-adequate free space.
diff --git a/ansi2knr.1 b/ansi2knr.1
new file mode 100644
index 0000000..434ce8f
--- /dev/null
+++ b/ansi2knr.1
@@ -0,0 +1,19 @@
+.TH ANSI2KNR 1 "31 December 1990" 
+.SH NAME
+ansi2knr \- convert ANSI C to Kernighan & Ritchie C
+.SH SYNOPSIS
+.I ansi2knr
+input_file output_file
+.SH DESCRIPTION
+If no output_file is supplied, output goes to stdout.
+.br
+There are no error messages.
+.sp
+.I ansi2knr
+recognizes functions by seeing a non-keyword identifier at the left margin, followed by a left parenthesis, with a right parenthesis as the last character on the line.  It will recognize a multi-line header if the last character on each line but the last is a left parenthesis or comma.  These algorithms ignore whitespace and comments, except that the function name must be the first thing on the line.
+.sp
+The following constructs will confuse it:
+.br
+     - Any other construct that starts at the left margin and follows the above syntax (such as a macro or function call).
+.br
+     - Macros that tinker with the syntax of the function header.
diff --git a/ansi2knr.c b/ansi2knr.c
index 13310d9..3924215 100644
--- a/ansi2knr.c
+++ b/ansi2knr.c
@@ -1,24 +1,26 @@
-/* Copyright (C) 1989, 1991 Aladdin Enterprises.  All rights reserved.
-   Distributed by Free Software Foundation, Inc.
+/* Copyright (C) 1989, 1991, 1993 Aladdin Enterprises. All rights reserved. */
 
-This file is part of Ghostscript.
+/* ansi2knr.c */
+/* Convert ANSI function declarations to K&R syntax */
 
-Ghostscript is distributed in the hope that it will be useful, but
+/*
+ansi2knr is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
 to anyone for the consequences of using it or for whether it serves any
 particular purpose or works at all, unless he says so in writing.  Refer
-to the Ghostscript General Public License for full details.
+to the GNU General Public License for full details.
 
 Everyone is granted permission to copy, modify and redistribute
-Ghostscript, but only under the conditions described in the Ghostscript
+ansi2knr, but only under the conditions described in the GNU
 General Public License.  A copy of this license is supposed to have been
-given to you along with Ghostscript so you can know your rights and
+given to you along with ansi2knr so you can know your rights and
 responsibilities.  It should be in a file named COPYING.  Among other
 things, the copyright notice and this notice must be preserved on all
-copies.  */
+copies.
+*/
 
 /*
----------- Here is the GhostScript file COPYING, referred to above ----------
+---------- Here is the GNU GPL file COPYING, referred to above ----------
 ----- These terms do NOT apply to the JPEG software itself; see README ------
 
 		    GHOSTSCRIPT GENERAL PUBLIC LICENSE
@@ -163,28 +165,26 @@
 PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) GHOSTSCRIPT, EVEN IF YOU
 HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM
 BY ANY OTHER PARTY.
+
 -------------------- End of file COPYING ------------------------------
 */
 
 
-/* ansi2knr.c */
-/* Convert ANSI function declarations to K&R syntax */
-
 #include <stdio.h>
 #include <ctype.h>
 
 #ifdef BSD
 #include <strings.h>
-#define strchr index
 #else
 #ifdef VMS
-	extern char *strcat(), *strchr(), *strcpy(), *strupr();
-	extern int strcmp(), strlen(), strncmp();
+	extern int strlen(), strncmp();
 #else
 #include <string.h>
 #endif
 #endif
 
+/* malloc and free should be declared in stdlib.h, */
+/* but if you've got a K&R compiler, they probably aren't. */
 #ifdef MSDOS
 #include <malloc.h>
 #else
@@ -198,27 +198,35 @@
 #endif
 
 /* Usage:
-	ansi2knr input_file output_file
+	ansi2knr input_file [output_file]
  * If no output_file is supplied, output goes to stdout.
  * There are no error messages.
  *
  * ansi2knr recognizes functions by seeing a non-keyword identifier
  * at the left margin, followed by a left parenthesis,
  * with a right parenthesis as the last character on the line.
- * It will recognize a multi-line header if the last character
- * on each line but the last is a left parenthesis or comma.
+ * It will recognize a multi-line header provided that the last character
+ * of the last line of the header is a right parenthesis,
+ * and no intervening line ends with a left brace or a semicolon.
  * These algorithms ignore whitespace and comments, except that
  * the function name must be the first thing on the line.
  * The following constructs will confuse it:
-	- Any other construct that starts at the left margin and
-	    follows the above syntax (such as a macro or function call).
-	- Macros that tinker with the syntax of the function header.
+ *	- Any other construct that starts at the left margin and
+ *	    follows the above syntax (such as a macro or function call).
+ *	- Macros that tinker with the syntax of the function header.
  */
 
 /* Scanning macros */
 #define isidchar(ch) (isalnum(ch) || (ch) == '_')
 #define isidfirstchar(ch) (isalpha(ch) || (ch) == '_')
 
+/* Forward references */
+char *skipspace();
+int writeblanks();
+int test1();
+int convert1();
+
+/* The main program */
 main(argc, argv)
     int argc;
     char *argv[];
@@ -324,9 +332,9 @@
 	switch ( *bend )
 	   {
 	case ')': contin = 1; break;
-	case '(':
-	case ',': contin = -1; break;
-	default: return 0;		/* not a function */
+	case '{':
+	case ';': return 0;		/* not a function */
+	default: contin = -1;
 	   }
 	while ( isidchar(*p) ) p++;
 	endfn = p;
@@ -362,13 +370,16 @@
 convert1(buf, out)
     char *buf;
     FILE *out;
-{	char *endfn = strchr(buf, '(') + 1;
+{	char *endfn;
 	register char *p;
 	char **breaks;
 	unsigned num_breaks = 2;	/* for testing */
 	char **btop;
 	char **bp;
 	char **ap;
+	/* Pre-ANSI implementations don't agree on whether strchr */
+	/* is called strchr or index, so we open-code it here. */
+	for ( endfn = buf; *(endfn++) != '('; ) ;
 top:	p = endfn;
 	breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
 	if ( breaks == 0 )
diff --git a/architecture b/architecture
deleted file mode 100644
index bf3385f..0000000
--- a/architecture
+++ /dev/null
@@ -1,1195 +0,0 @@
-
-	JPEG SYSTEM ARCHITECTURE		1-DEC-92
-
-
-This file provides an overview of the "architecture" of the portable JPEG
-software; that is, the functions of the various modules in the system and the
-interfaces between modules.  For more precise details about any data structure
-or calling convention, see the header files.
-
-Important note: when I say "module" I don't mean "a C function", which is what
-some people seem to think the term means.  A separate C source file is closer
-to the mark.  Also, it is frequently the case that several different modules
-present a common interface to callers; the term "object" or "method" refers to
-this common interface (see "Poor man's object-oriented programming", below).
-
-JPEG-specific terminology follows the JPEG standard:
-  A "component" means a color channel, e.g., Red or Luminance.
-  A "sample" is a pixel component value (i.e., one number in the image data).
-  A "coefficient" is a frequency coefficient (a DCT transform output number).
-  The term "block" refers to an 8x8 group of samples or coefficients.
-  "MCU" (minimum coded unit) is the same as "MDU" of the R8 draft; i.e., an
-	interleaved set of blocks of size determined by the sampling factors,
-	or a single block in a noninterleaved scan.
-
-
-*** System requirements ***
-
-We must support compression and decompression of both Huffman and
-arithmetic-coded JPEG files.  Any set of compression parameters allowed by the
-JPEG spec should be readable for decompression.  (We can be more restrictive
-about what formats we can generate.)  (Note: for legal reasons no arithmetic
-coding implementation is currently included in the publicly available sources.
-However, the architecture still supports it.)
-
-We need to be able to handle both raw JPEG files (more specifically, the JFIF
-format) and JPEG-in-TIFF (C-cubed's format, and perhaps Kodak's).  Even if we
-don't implement TIFF ourselves, other people will want to use our code for
-that.  This means that generation and scanning of the file header has to be
-separated out.
-
-Perhaps we should be prepared to support the JPEG lossless mode (also referred
-to in the spec as spatial DPCM coding).  A lot of people seem to believe they
-need this... whether they really do is debatable, but the customer is always
-right.  On the other hand, there will not be much sharable code between the
-lossless and lossy modes!  At best, a lossless program could be derived from
-parts of the lossy version.  For now we will only worry about the lossy mode.
-
-I see no real value in supporting the JPEG progressive modes (note that
-spectral selection and successive approximation are two different progressive
-modes).  These are only of interest when painting the decompressed image in
-real-time, which nobody is going to do with a pure software implementation.
-
-There is some value in supporting the hierarchical mode, which allows for
-successive frames of higher resolution.  This could be of use for including
-"thumbnail" representations.  However, this appears to add a lot more
-complexity than it is worth.
-
-A variety of uncompressed image file formats and user interfaces must be
-supported.  These aspects therefore have to be kept separate from the rest of
-the system.  A particularly important issue is whether color quantization of
-the output is needed (i.e., whether a colormap is used).  We should be able to
-support both adaptive quantization (which requires two or more passes over the
-image) and nonadaptive (quantization to a prespecified colormap, which can be
-done in one pass).
-
-Memory usage is an important concern, since we will port this code to 80x86
-and other limited-memory machines.  For large intermediate structures, we
-should be able to use either virtual memory or temporary files.
-
-It should be possible to build programs that handle compression only,
-decompression only, or both, without much duplicate or unused code in any
-version.  (In particular, a decompression-only version should have no extra
-baggage.)
-
-
-*** Compression overview ***
-
-The *logical* steps needed in (non-lossless) JPEG compression are:
-
-1. Conversion from incoming image format to a standardized internal form
-   (either RGB or grayscale).
-
-2. Color space conversion (e.g., RGB to YCbCr).  This is a null step for
-   grayscale (unless we support mapping color inputs to grayscale, which
-   would most easily be done here).  Gamma adjustment may also be needed here.
-
-3. Downsampling (reduction of number of samples in some color components).
-   This step operates independently on each color component.
-
-4. MCU extraction (creation of a single sequence of 8x8 sample blocks).
-   This step and the following ones are performed once for each scan
-   in the output JPEG file, i.e., once if making an interleaved file and more
-   than once for a noninterleaved file.
-   Note: both this step and the previous one must deal with edge conditions
-   for pictures that aren't a multiple of the MCU dimensions.  Alternately,
-   we could expand the picture to a multiple of an MCU before doing these
-   two steps.  (The latter seems better and has been adopted below.)
-
-5. DCT transformation of each 8x8 block.
-
-6. Quantization scaling and zigzag reordering of the elements in each 8x8
-   block.
-
-7. Huffman or arithmetic encoding of the transformed block sequence.
-
-8. Output of the JPEG file with whatever headers/markers are wanted.
-
-Of course, the actual implementation will combine some of these logical steps
-for efficiency.  The trick is to keep these logical functions as separate as
-possible without losing too much performance.
-
-In addition to these logical pipeline steps, we need various modules that
-aren't part of the data pipeline.  These are:
-
-A. Overall control (sequencing of other steps & management of data passing).
-
-B. User interface; this will determine the input and output files, and supply
-   values for some compression parameters.  Note that this module is highly
-   platform-dependent.
-
-C. Compression parameter selection: some parameters should be chosen
-   automatically rather than requiring the user to find a good value.
-   The prototype only does this for the back-end (Huffman or arithmetic)
-   parameters, but further in the future, more might be done.  A
-   straightforward approach to selection is to try several values; this
-   requires being able to repeatedly apply some portion of the pipeline and
-   inspect the results (without actually outputting them).  Probably only
-   entropy encoding parameters can reasonably be done this way; optimizing
-   earlier steps would require too much data to be reprocessed (not to mention
-   the problem of interactions between parameters for different steps).
-   What other facilities do we need to support automatic parameter selection?
-
-D. A memory management module to deal with small-memory machines.  This must
-   create the illusion of virtual memory for certain large data structures
-   (e.g., the downsampled image or the transformed coefficients).
-   The interface to this must be defined to minimize the overhead incurred,
-   especially on virtual-memory machines where the module won't do much.
-
-In many cases we can arrange things so that a data stream is produced in
-segments by one module and consumed by another without the need to hold it all
-in (virtual) memory.  This is obviously not possible for any data that must be
-scanned more than once, so it won't work everywhere.
-
-The major variable at this level of detail is whether the JPEG file is to be
-interleaved or not; that affects the order of processing so fundamentally that
-the central control module must know about it.  Some of the other modules may
-need to know it too.  It would simplify life if we didn't need to support
-noninterleaved images, but that is not reasonable.
-
-Many of these steps operate independently on each color component; the
-knowledge of how many components there are, and how they are interleaved,
-ought to be confined to the central control module.  (Color space conversion
-and MCU extraction probably have to know it too.)
-
-
-*** Decompression overview ***
-
-Decompression is roughly the inverse process from compression, but there are
-some additional steps needed to produce a good output image.
-
-The *logical* steps needed in (non-lossless) JPEG decompression are:
-
-1. Scanning of the JPEG file, decoding of headers/markers etc.
-
-2. Huffman or arithmetic decoding of the coefficient sequence.
-
-3. Quantization descaling and zigzag reordering of the elements in each 8x8
-   block.
-
-4. MCU disassembly (conversion of a possibly interleaved sequence of 8x8
-   blocks back to separate components in pixel map order).
-
-5. (Optional)  Cross-block smoothing per JPEG section K.8 or a similar
-   algorithm.  (Steps 5-8 operate independently on each component.)
-
-6. Inverse DCT transformation of each 8x8 block.
-
-7. Upsampling.  At this point a pixel image of the original dimensions
-   has been recreated.
-
-8. Post-upsampling smoothing.  This can be combined with upsampling,
-   by using a convolution-like calculation to generate each output pixel
-   directly from one or more input pixels.
-
-9. Cropping to the original pixel dimensions (throwing away duplicated
-   pixels at the edges).  It is most convenient to do this now, as the
-   preceding steps are simplified by not having to worry about odd picture
-   sizes.
-
-10. Color space reconversion (e.g., YCbCr to RGB).  This is a null step for
-    grayscale.  (Note that mapping a color JPEG to grayscale output is most
-    easily done in this step.)  Gamma adjustment may also be needed here.
-
-11. Color quantization (only if a colormapped output format is requested).
-    NOTE: it is probably preferable to perform quantization in the internal
-    (JPEG) colorspace rather than the output colorspace.  Doing it that way,
-    color conversion need only be applied to the colormap entries, not to
-    every pixel; and quantization gets to operate in a non-gamma-corrected
-    space.  But the internal space may not be suitable for some algorithms.
-    The system design is such that only the color quantizer module knows
-    whether color conversion happens before or after quantization.
-
-12. Writing of the desired image format.
-
-As before, some of these will be combined into single steps.  When dealing
-with a noninterleaved JPEG file, steps 2-9 will be performed once for each
-scan; the resulting data will need to be buffered up so that steps 10-12 can
-process all the color components together.
-
-The same auxiliary modules are needed as before, except for compression
-parameter selection.  Note that rerunning a pipeline stage should never be
-needed during decompression.  This may allow a simpler control module.  The
-user interface might also be simpler since it need not supply any compression
-parameters.
-
-As before, not all of these steps require the whole image to be stored.
-Actually, two-pass color quantization is the only step that logically requires
-this; everything else could be done a few raster lines at a time (at least for
-interleaved images).  We might want to make color quantization be a separate
-program because of this fact.
-
-Again, many of the steps should be able to work on one color component in
-ignorance of the other components.
-
-
-*** Implications of noninterleaved formats ***
-
-Much of the work can be done in a single pass if an interleaved JPEG file
-format is used.  With a noninterleaved JPEG file, separating or recombining
-the components will force use of virtual memory (on a small-memory machine,
-we probably would want one temp file per color component).
-
-If any of the image formats we read or write are noninterleaved, the opposite
-condition might apply: processing a noninterleaved JPEG file would be more
-efficient.  Offhand, though, I can't think of any popular image formats that
-work that way; besides the win would only come if the same color space were
-used in JPEG and non-JPEG files.  It's not worth the complexity to make the
-system design accommodate that case efficiently.
-
-An argument against interleaving is that it makes the decompressor need more
-memory for cross-block smoothing (since the minimum processable chunk of the
-image gets bigger).  With images more than 1000 pixels across, 80x86 machines
-are likely to have difficulty in handling this feature.
-
-Another argument against interleaving is that the noninterleaved format allows
-a wider range of sampling factors, since the limit of ten blocks per MCU no
-longer applies.  We could get around this by blithely ignoring the spec's
-limit of ten blocks, but that seems like a bad idea (especially since it makes
-the above problem worse).
-
-The upshot is that we need to support both interleaved and noninterleaved JPEG
-formats, since for any given machine and picture size one may be much more
-efficient than the other.  However, the non-JPEG format we convert to or from
-will be assumed to be an interleaved format (i.e., it produces or stores all
-the components of a pixel together).
-
-I do not think it is necessary for the compressor to be able to output
-partially-interleaved formats (multiple scans, some of which interleave a
-subset of the components).  However, the decompressor must be able to read
-such files to conform to the spec.
-
-
-*** Data formats ***
-
-Pipeline steps that work on pixel sample values will use the following data
-structure:
-
-    typedef something JSAMPLE;		a pixel component value, 0..MAXJSAMPLE
-    typedef JSAMPLE *JSAMPROW;		ptr to a row of samples
-    typedef JSAMPROW *JSAMPARRAY;	ptr to a list of rows
-    typedef JSAMPARRAY *JSAMPIMAGE;	ptr to a list of color-component arrays
-
-The basic element type JSAMPLE will be one of unsigned char, (signed) char, or
-unsigned short.  Unsigned short will be used if samples wider than 8 bits are
-to be supported (this is a compile-time option).  Otherwise, unsigned char is
-used if possible.  If the compiler only supports signed chars, then it is
-necessary to mask off the value when reading.  Thus, all reads of sample
-values should be coded as "GETJSAMPLE(value)", where the macro will be defined
-as "((value)&0xFF)" on signed-char machines and "(value)" elsewhere.
-
-With these conventions, JSAMPLE values can be assumed to be >= 0.  This should
-simplify correct rounding during downsampling, etc.  The JPEG draft's
-specification that sample values run from -128..127 will be accommodated by
-subtracting 128 just as the sample value is copied into the source array for
-the DCT step (this will be an array of signed shorts or longs).  Similarly,
-during decompression the output of the IDCT step will be immediately shifted
-back to 0..255.  (NB: different values are required when 12-bit samples are in
-use.  The code should be written in terms of MAXJSAMPLE and CENTERJSAMPLE,
-which will be #defined as 255 and 128 respectively in an 8-bit implementation,
-and as 4095 and 2048 in a 12-bit implementation.)
-
-On compilers that don't support "unsigned short", signed short can be used for
-a 12-bit implementation.  To support lossless coding (which allows up to
-16-bit data precision) masking with 0xFFFF in GETJSAMPLE might be necessary.
-(But if "int" is 16 bits then using "unsigned int" is the best solution.)
-
-Notice that we use a pointer per row, rather than a two-dimensional JSAMPLE
-array.  This choice costs only a small amount of memory and has several
-benefits:
-
-* Code using the data structure doesn't need to know the allocated width of
-the rows.  This will simplify edge expansion/compression, since we can work
-in an array that's wider than the logical picture width.
-
-* The rows forming a component array may be allocated at different times
-without extra copying.  This will simplify working a few scanlines at a time,
-especially in smoothing steps that need access to the previous and next rows.
-
-* Indexing doesn't require multiplication; this is a performance win on many
-machines.
-
-Note that each color component is stored in a separate array; we don't use the
-traditional structure in which the components of a pixel are stored together.
-This simplifies coding of steps that work on each component independently,
-because they don't need to know how many components there are.  Furthermore,
-we can read or write each component to a temp file independently, which is
-helpful when dealing with noninterleaved JPEG files.
-
-A specific sample value will be accessed by code such as
-	GETJSAMPLE(image[colorcomponent][row][col])
-where col is measured from the image left edge, but row is measured from the
-first sample row currently in memory.  Either of the first two indexings can
-be precomputed by copying the relevant pointer.
-
-
-Pipeline steps that work on frequency-coefficient values will use the
-following data structure:
-
-    typedef short JCOEF;		a 16-bit signed integer
-    typedef JCOEF JBLOCK[64];		an 8x8 block of coefficients
-    typedef JBLOCK *JBLOCKROW;		ptr to one horizontal row of 8x8 blocks
-    typedef JBLOCKROW *JBLOCKARRAY;	ptr to a list of such rows
-    typedef JBLOCKARRAY *JBLOCKIMAGE;	ptr to a list of color component arrays
-
-The underlying type is always a 16-bit signed integer (this is "short" on all
-machines of interest, but let's use the typedef name anyway).  These are
-grouped into 8x8 blocks (we should use #defines DCTSIZE and DCTSIZE2 rather
-than "8" and "64").  The contents of a block may be either in "natural" or
-zigzagged order, and may be true values or divided by the quantization
-coefficients, depending on where the block is in the pipeline.
-
-Notice that the allocation unit is now a row of 8x8 blocks, corresponding to
-eight rows of samples.  Otherwise the structure is much the same as for
-samples, and for the same reasons.
-
-On machines where malloc() can't handle a request bigger than 64Kb, this data
-structure limits us to rows of less than 512 JBLOCKs, which would be a picture
-width of 4000 pixels.  This seems an acceptable restriction.
-
-
-On 80x86 machines, the bottom-level pointer types (JSAMPROW and JBLOCKROW)
-must be declared as "far" pointers, but the upper levels can be "near"
-(implying that the pointer lists are allocated in the DS segment).
-To simplify sharing code, we'll have a #define symbol FAR, which expands to
-the "far" keyword when compiling on 80x86 machines and to nothing elsewhere.
-
-
-The data arrays used as input and output of the DCT transform subroutine will
-be declared using a separate typedef; they could be arrays of "short", "int"
-or "long" independently of the above choices.  This would depend on what is
-needed to make the compiler generate correct and efficient multiply/add code
-in the DCT inner loops.  No significant speed or memory penalty will be paid
-to have a different representation than is used in the main image storage
-arrays, since some additional value-by-value processing is done at the time of
-creation or extraction of the DCT data anyway (e.g., add/subtract 128).
-
-
-*** Poor man's object-oriented programming ***
-
-It should be pretty clear by now that we have a lot of quasi-independent
-steps, many of which have several possible behaviors.  To avoid cluttering the
-code with lots of switch statements, we'll use a simple form of object-style
-programming to separate out the different possibilities.
-
-For example, Huffman and arithmetic coding will be implemented as two separate
-modules that present the same external interface; at runtime, the calling code
-will access the proper module indirectly through an "object".
-
-We can get the limited features we need while staying within portable C.  The
-basic tool is a function pointer.  An "object" is just a struct containing one
-or more function pointer fields, each of which corresponds to a method name in
-real object-oriented languages.  During initialization we fill in the function
-pointers with references to whichever module we have determined we need to use
-in this run.  Then invocation of the module is done by indirecting through a
-function pointer; on most architectures this is no more expensive (and
-possibly cheaper) than a switch, which would be the only other way of making
-the required run-time choice.  The really significant benefit, of course, is
-keeping the source code clean and well structured.
-
-For example, the interface for entropy decoding (Huffman or arithmetic
-decoding) might look like this:
-
-	struct function_ptr_struct {
-		...
-		/* Entropy decoding methods */
-		void (*prepare_for_scan) ();
-		void (*get_next_mcu) ();
-		...
-		};
-
-	typedef struct function_ptr_struct * function_ptrs;
-
-The struct pointer is what will actually be passed around.  A call site might
-look like this:
-
-	some_function (function_ptrs fptrs)
-	    {
-		...
-		(*fptrs->get_next_mcu) (...);
-		...
-	    }
-
-(It might be worth inventing some specialized macros to hide the rather ugly
-syntax for method definition and call.)  Note that the caller doesn't know how
-many different get_next_mcu procedures there are, what their real names are,
-nor how to choose which one to call.
-
-An important benefit of this scheme is that it is easy to provide multiple
-versions of any method, each tuned to a particular case.  While a lot of
-precalculation might be done to select an optimal implementation of a method,
-the cost per invocation is constant.  For example, the MCU extraction step
-might have a "generic" method, plus one or more "hardwired" methods for the
-most popular sampling factors; the hardwired methods would be faster because
-they'd use straight-line code instead of for-loops.  The cost to determine
-which method to use is paid only once, at startup, and the selection criteria
-are hidden from the callers of the method.
-
-This plan differs a little bit from usual object-oriented structures, in that
-only one instance of each object class will exist during execution.  The
-reason for having the class structure is that on different runs we may create
-different instances (choose to execute different modules).
-
-To minimize the number of object pointers that have to be passed around, it
-will be easiest to have just a few big structs containing all the method
-pointers.  We'll actually use two such structs, one for "system-dependent"
-methods (memory allocation and error handling) and one for everything else.
-
-Because of this choice, it's best not to think of an "object" as a specific
-data structure.  Rather, an "object" is just a group of related methods.
-There would typically be one or more C modules (source files) providing
-concrete implementations of those methods.  You can think of the term
-"method" as denoting the common interface presented by some set of functions,
-and "object" as denoting a group of common method interfaces, or the total
-shared interface behavior of a group of modules.
-
-
-*** Data chunk sizes ***
-
-To make the cost of this object-oriented style really minimal, we should make
-sure that each method call does a fair amount of computation.  To do that we
-should pass large chunks of data around; for example, the colorspace
-conversion method should process much more than one pixel per call.
-
-For many steps, the most natural unit of data seems to be an "MCU row".
-This consists of one complete horizontal strip of the image, as high as an
-MCU.  In a noninterleaved scan, an MCU row is always eight samples high (when
-looking at samples) or one 8x8 block high (when looking at coefficients).  In
-an interleaved scan, an MCU row consists of all the data for one horizontal
-row of MCUs; this may be from one to four blocks high (eight to thirty-two
-samples) depending on the sampling factors.  The height and width of an MCU
-row may be different in each component.  (Note that the height and width of an
-MCU row changes at the downsampling and upsampling steps.  An unsubsampled
-image has the same size in each component.  The preceding statements apply to
-the downsampled dimensions.)
-
-For example, consider a 1024-pixel-wide image using (2h:2v)(1h:1v)(1h:1v)
-subsampling.  In the noninterleaved case, an MCU row of Y would contain 8x1024
-samples or the same number of frequency coefficients, so it would occupy
-8K bytes (samples) or 16K bytes (coefficients).  An MCU row of Cb or Cr would
-contain 8x512 samples and occupy half as much space.  In the interleaved case,
-an MCU row would contain 16x1024 Y samples, 8x512 Cb and 8x512 Cr samples, so
-a total of 24K (samples) or 48K (coefficients) would be needed.  This is a
-reasonable amount of data to expect to retain in memory at one time.  (Bear in
-mind that we'll usually need to have several MCU rows resident in memory at
-once, at the inputs and outputs to various pipeline steps.)
-
-The worst case is probably (2h:4v)(1h:1v)(1h:1v) interleaving (this uses 10
-blocks per MCU, which is the maximum allowed by the spec).  An MCU will then
-contain 32 sample rows worth of Y, so it would occupy 40K or 80K bytes for a
-1024-pixel-wide image.  The most memory-intensive step is probably cross-block
-smoothing, for which we'd need 3 MCU rows of coefficients as input and another
-one as output; that would be 320K of working storage.  Anything much larger
-would not fit in an 80x86 machine.  (To decompress wider pictures on an 80x86,
-we'll have to skip cross-block smoothing or else use temporary files.)
-
-This unit is thus a reasonable-sized chunk for passing through the pipeline.
-Of course, its major advantage is that it is a natural chunk size for the MCU
-assembly and disassembly steps to work with.
-
-For the entropy (Huffman or arithmetic) encoding/decoding steps, the most
-convenient chunk is a single MCU: one 8x8 block if not interleaved, three to
-ten such blocks if interleaved.  The advantage of this is that when handling
-interleaved data, the blocks have the same sequence of component membership on
-each call.  (For example, Y,Y,Y,Y,Cb,Cr when using (2h:2v)(1h:1v)(1h:1v)
-subsampling.)  The code needs to know component membership so that it can
-apply the right set of compression coefficients to each block.  A prebuilt
-array describing this membership can be used during each call.  This chunk
-size also makes it easy to handle restart intervals: just count off one MCU
-per call and reinitialize when the count reaches zero (restart intervals are
-specified in numbers of MCU).
-
-For similar reasons, one MCU is also the best chunk size for the frequency
-coefficient quantization and dequantization steps.
-
-For downsampling and upsampling, the best chunk size is to have each call
-transform Vk sample rows from or to Vmax sample rows (Vk = this component's
-vertical sampling factor, Vmax = largest vertical sampling factor).  There are
-eight such chunks in each MCU row.  Using a whole MCU row as the chunk size
-would reduce function call overhead a fraction, but would imply more buffering
-to provide context for cross-pixel smoothing.
-
-
-*** Compression object structure ***
-
-I propose the following set of objects for the compressor.  Here an "object"
-is the common interface for one or more modules having comparable functions.
-
-Most of these objects can be justified as information-hiding modules.
-I've indicated what information is private to each object/module.
-
-Note that in all cases, the caller of a method is expected to have allocated
-any storage needed for it to return its result.  (Typically this storage can
-be re-used in successive calls, so malloc'ing and free'ing once per call is
-not reasonable.)  Also, much of the context required (compression parameters,
-image size, etc) will be passed around in large common data structures, which
-aren't described here; see the header files.  Notice that any object that
-might need to allocate working storage receives an "init" and a "term" call;
-"term" should be careful to free all allocated storage so that the JPEG system
-can be used multiple times during a program run.  (For the same reason,
-depending on static initialization of variables is a no-no.  The only
-exception to the free-all-allocated-storage rule is that storage allocated for
-the entire processing of an image need not be explicitly freed, since the
-memory manager's free_all cleanup will free it.)
-
-1. Input file conversion to standardized form.  This provides these methods:
-	input_init: read the file header, report image size & component count.
-	get_input_row: read one pixel row, return it in our standard format.
-	input_term: finish up at the end.
-   In implementations that support multiple input formats, input_init could
-   set up an appropriate get_input_row method depending on the format it
-   finds.  Note that in most applications, the selection and opening of the
-   input file will be under the control of the user interface module; and
-   indeed the user interface may have already read the input header, so that
-   all that input_init may have to do is return previously saved values.  The
-   behind-the-scenes interaction between this object and the user interface is
-   not specified by this architecture.
-   (Hides format of input image and mechanism used to read it.  This code is
-   likely to vary considerably from one implementation to another.  Note that
-   the color space and number of color components of the source are not hidden;
-   but they are used only by the next object.)
-
-2. Gamma and color space conversion.  This provides three methods:
-	colorin_init: initialization.
-	get_sample_rows: read, convert, and return a specified number of pixel
-			 rows (not more than remain in the picture).
-	colorin_term: finish up at the end.
-   The most efficient approach seems to be for this object to call
-   get_input_row directly, rather than being passed the input data; that way,
-   any intermediate storage required can be local to this object.
-   (get_sample_rows might tell get_input_row to read directly into its own
-   output area and then convert in place; or it may do something different.
-   For example, conversion in place wouldn't work if it is changing the number
-   of color components.)  The output of this step is in the standardized
-   sample array format shown previously.
-   (Hides all knowledge of color space semantics and conversion.  Remaining
-   modules only need to know the number of JPEG components.)
-
-3. Edge expansion: needs only a single method.
-	edge_expand: Given an NxM sample array, expand to a desired size (a
-		     multiple of the MCU dimensions) by duplicating the last
-		     row or column.  Repeat for each component.
-   Expansion will occur in place, so the caller must have pre-allocated enough
-   storage.  (I'm assuming that it is easier and faster to do this expansion
-   than it is to worry about boundary conditions in the next two steps.
-   Notice that vertical expansion will occur only once, at the bottom of the
-   picture, so only horizontal expansion by a few pixels is speed-critical.)
-   (This doesn't really hide any information, so maybe it could be a simple
-   subroutine instead of a method.  Depends on whether we want to be able to
-   use alternative, optimized methods.)
-
-4. Downsampling: this will be applied to one component at a time.
-	downsample_init: initialize (precalculate convolution factors, for
-			 example).  This will be called once per scan.
-	downsample: Given a sample array, reduce it to a smaller number of
-		    samples using specified sampling factors.
-	downsample_term: clean up at the end of a scan.
-   If the current component has vertical sampling factor Vk and the largest
-   sampling factor is Vmax, then the input is always Vmax sample rows (whose
-   width is a multiple of Hmax) and the output is always Vk sample rows.
-   Vmax additional rows above and below the nominal input rows are also passed
-   for use by partial-pixel-averaging sampling methods.  (Is this necessary?)
-   At the top and bottom of the image, these extra rows are copies of the
-   first or last actual input row.
-   (This hides whether and how cross-pixel averaging occurs.)
-
-5. MCU extraction (creation of a single sequence of 8x8 sample blocks).
-	extract_init: initialize as needed.  This will be called once per scan.
-	extract_MCUs: convert a sample array to a sequence of MCUs.
-	extract_term: clean up at the end of a scan.
-   Given one or more MCU rows worth of image data, extract sample blocks in the
-   appropriate order; pass these off to subsequent steps one MCU at a time.
-   The input must be a multiple of the MCU dimensions.  It will probably be
-   most convenient for the DCT transform, frequency quantization, and zigzag
-   reordering of each block to be done as simple subroutines of this step.
-   Once a transformed MCU has been completed, it'll be passed off to a
-   method call, which will be passed as a parameter to extract_MCUs.
-   That routine might either encode and output the MCU immediately, or buffer
-   it up for later output if we want to do global optimization of the entropy
-   encoding coefficients.  Note: when outputting a noninterleaved file this
-   object will be called separately for each component.  Direct output could
-   be done for the first component, but the others would have to be buffered.
-   (Again, an object mainly on the grounds that multiple instantiations might
-   be useful.)
-
-6. DCT transformation of each 8x8 block.  This probably doesn't have to be a
-   full-fledged method, but just a plain subroutine that will be called by MCU
-   extraction.  One 8x8 block will be processed per call.
-
-7. Quantization scaling and zigzag reordering of the elements in each 8x8
-   block.  (This can probably be a plain subroutine called once per block by
-   MCU extraction; hard to see a need for multiple instantiations here.)
-
-8. Entropy encoding (Huffman or arithmetic).
-	entropy_encode_init: prepare for one scan.
-	entropy_encode: accepts an MCU's worth of quantized coefficients,
-			encodes and outputs them.
-	entropy_encode_term: finish up at end of a scan (dump any buffered
-			     bytes, for example).
-   The data output by this module will be sent to the entropy_output method
-   provided by the pipeline controller.  (It will probably be worth using
-   buffering to pass multiple bytes per call of the output method.)  The
-   output method could be just write_jpeg_data, but might also be a dummy
-   routine that counts output bytes (for use during cut-and-try coefficient
-   optimization).
-   (This hides which entropy encoding method is in use.)
-
-9. JPEG file header construction.  This will provide these methods:
-	write_file_header: output the initial header.
-	write_scan_header: output scan header (called once per component
-			   if noninterleaved mode).
-	write_jpeg_data: the actual data output method for the preceding step.
-	write_scan_trailer: finish up after one scan.
-	write_file_trailer: finish up at end of file.
-   Note that compressed data is passed to the write_jpeg_data method, in case
-   a simple fwrite isn't appropriate for some reason.
-   (This hides which variant JPEG file format is being written.  Also, the
-   actual mechanism for writing the file is private to this object and the
-   user interface.)
-
-10. Pipeline control.  This object will provide the "main loop" that invokes
-    all the pipeline objects.  Note that we will need several different main
-    loops depending on the situation (interleaved output or not, global
-    optimization of encoding parameters or not, etc).  This object will do
-    most of the memory allocation, since it will provide the working buffers
-    that are the inputs and outputs of the pipeline steps.
-    (An object mostly to support multiple instantiations; however, overall
-    memory management and sequencing of operations are known only here.)
-
-11. Overall control.  This module will provide at least two routines:
-	jpeg_compress: the main entry point to the compressor.
-	per_scan_method_selection: called by pipeline controllers for
-				   secondary method selection passes.
-    jpeg_compress is invoked from the user interface after the UI has selected
-    the input and output files and obtained values for all compression
-    parameters that aren't dynamically determined.  jpeg_compress performs
-    basic initialization (e.g., calculating the size of MCUs), does the
-    "global" method selection pass, and finally calls the selected pipeline
-    control object.  (Per-scan method selections will be invoked by the
-    pipeline controller.)
-    Note that jpeg_compress can't be a method since it is invoked prior to
-    method selection.
-
-12. User interface; this is the architecture's term for "the rest of the
-    application program", i.e., that which invokes the JPEG compressor.  In a
-    standalone JPEG compression program the UI need be little more than a C
-    main() routine and argument parsing code; but we can expect that the JPEG
-    compressor may be incorporated into complex graphics applications, wherein
-    the UI is much more complex.  Much of the UI will need to be written
-    afresh for each non-Unix-like platform the compressor is ported to.
-    The UI is expected to supply input and output files and values for all
-    non-automatically-chosen compression parameters.  (Hence defaults are
-    determined by the UI; we should provide helpful routines to fill in
-    the recommended defaults.)  The UI must also supply error handling
-    routines and some mechanism for trace messages.
-    (This module hides the user interface provided --- command line,
-    interactive, etc.  Except for error/message handling, the UI calls the
-    portable JPEG code, not the other way around.)
-
-13. (Optional) Compression parameter selection control.
-	entropy_optimize: given an array of MCUs ready to be fed to entropy
-			  encoding, find optimal encoding parameters.
-    The actual optimization algorithm ought to be separated out as an object,
-    even though a special pipeline control method will be needed.  (The
-    pipeline controller only has to understand that the output of extract_MCUs
-    must be built up as a virtual array rather than fed directly to entropy
-    encoding and output.  This pipeline behavior may also be useful for future
-    implementation of hierarchical modes, etc.)
-    To minimize the amount of control logic in the optimization module, the
-    pipeline control doesn't actually hand over big-array pointers, but rather
-    an "iterator": a function which knows how to scan the stored image.
-    (This hides the details of the parameter optimization algorithm.)
-
-    The present design doesn't allow for multiple passes at earlier points
-    in the pipeline, but allowing that would only require providing some
-    new pipeline control methods; nothing else need change.
-
-14. A memory management object.  This will provide methods to allocate "small"
-    things and "big" things.  Small things have to fit in memory and you get
-    back direct pointers (this could be handled by direct calls to malloc, but
-    it's cleaner not to assume malloc is the right routine).  "Big" things
-    mean buffered images for multiple passes, noninterleaved output, etc.
-    In this case the memory management object will give you room for a few MCU
-    rows and you have to ask for access to the next few; dumping and reloading
-    in a temporary file will go on behind the scenes.  (All big objects are
-    image arrays containing either samples or coefficients, and will be
-    scanned top-to-bottom some number of times, so we can apply this access
-    model easily.)  On a platform with virtual memory, the memory manager can
-    treat small and big things alike: just malloc up enough virtual memory for
-    the whole image, and let the operating system worry about swapping the
-    image to disk.
-
-    Most of the actual calls on the memory manager will be made from pipeline
-    control objects; changing any data item from "small" to "big" status would
-    require a new pipeline control object, since it will contain the logic to
-    ask for a new chunk of a big thing.  Thus, one way in which pipeline
-    controllers will vary is in which structures they treat as big.
-
-    The memory manager will need to be told roughly how much space is going to
-    be requested overall, so that it can figure out how big a buffer is safe
-    to allocate for a "big" object.  (If it happens that you are dealing with
-    a small image, you'd like to decide to keep it all in memory!)  The most
-    flexible way of doing this is to divide allocation of "big" objects into
-    two steps.  First, there will be one or more "request" calls that indicate
-    the desired object sizes; then an "instantiate" call causes the memory
-    manager to actually construct the objects.  The instantiation must occur
-    before the contents of any big object can be accessed.
-
-    For 80x86 CPUs, we would like the code to be compilable under small or
-    medium model, meaning that pointers are 16 bits unless explicitly declared
-    FAR.  Hence space allocated by the "small" allocator must fit into the
-    64Kb default data segment, along with stack space and global/static data.
-    For normal JPEG operations we seem to need only about 32Kb of such space,
-    so we are within the target (and have a reasonable slop for the needs of
-    a surrounding application program).  However, some color quantization
-    algorithms need 64Kb or more of all-in-memory space in order to create
-    color histograms.  For this purpose, we will also support "medium" size
-    things.  These are semantically the same as "small" things but are
-    referenced through FAR pointers.
-
-    The following methods will be needed:
-	alloc_small:	allocate an object of given size; use for any random
-			data that's not an image array.
-	free_small:	release same.
-	alloc_medium:	like alloc_small, but returns a FAR pointer.  Use for
-			any object bigger than a couple kilobytes.
-	free_medium:	release same.
-	alloc_small_sarray: construct an all-in-memory image sample array.
-	free_small_sarray:  release same.
-	alloc_small_barray,
-	free_small_barray:  ditto for block (coefficient) arrays.
-	request_big_sarray:  request a virtual image sample array.  The size
-			     of the in-memory buffer will be determined by the
-			     memory manager, but it will always be a multiple
-			     of the passed-in MCU height.
-	request_big_barray:  ditto for block (coefficient) arrays.
-	alloc_big_arrays:  instantiate all the big arrays previously requested.
-			   This call will also pass some info about future
-			   memory demands, so that the memory manager can
-			   figure out how much space to leave unallocated.
-	access_big_sarray: obtain access to a specified portion of a virtual
-			   image sample array.
-	free_big_sarray:   release a virtual sample array.
-	access_big_barray,
-	free_big_barray:   ditto for block (coefficient) arrays.
-	free_all:	   release any remaining storage.  This is called
-			   before normal or error termination; the main reason
-			   why it must exist is to ensure that any temporary
-			   files will be deleted upon error termination.
-
-    alloc_big_arrays will be called by the pipeline controller, which does
-    most of the memory allocation anyway.  The only reason for having separate
-    request calls is to allow some of the other modules to get big arrays.
-    The pipeline controller is required to give an upper bound on total future
-    small-array requests, so that this space can be discounted.  (A fairly
-    conservative estimate will be adequate.)  Future small-object requests
-    aren't counted; the memory manager has to use a slop factor for those.
-    10K or so seems to be sufficient.  (In an 80x86, small objects aren't an
-    issue anyway, since they don't compete for far-heap space.  "Medium"-size
-    objects will have to be counted separately.)
-
-    The distinction between sample and coefficient array routines is annoying,
-    but it has to be maintained for machines in which "char *" is represented
-    differently from "int *".  On byte-addressable machines some of these
-    methods could perhaps point to the same code.
-
-    The array routines will operate on only 2-D arrays (one component at a
-    time), since different components may require different-size arrays.
-
-    (This object hides the knowledge of whether virtual memory is available,
-    as well as the actual interface to OS and library support routines.)
-
-Note that any given implementation will presumably contain only one
-instantiation of input file header reading, overall control, user interface,
-and memory management.  Thus these could be called as simple subroutines,
-without bothering with an object indirection.  This is essential for overall
-control (which has to initialize the object structure); for consistency we
-will impose objectness on the other three.
-
-
-*** Decompression object structure ***
-
-I propose the following set of objects for decompression.  The general
-comments at the top of the compression object section also apply here.
-
-1. JPEG file scanning.  This will provide these methods:
-	read_file_header: read the file header, determine which variant
-			  JPEG format is in use, read everything through SOF.
-	read_scan_header: read scan header (up through SOS).  This is called
-			  after read_file_header and again after each scan;
-			  it returns TRUE if it finds SOS, FALSE if EOI.
-	read_jpeg_data: fetch data for entropy decoder.
-	resync_to_restart: try to recover from bogus data (see below).
-	read_scan_trailer: finish up after one scan, prepare for another call
-			   of read_scan_header (may be a no-op).
-	read_file_trailer: finish up at end of file (probably a no-op).
-   The entropy decoder must deal with restart markers, but all other JPEG
-   marker types will be handled in this object; useful data from the markers
-   will be extracted into data structures available to subsequent routines.
-   Note that on exit from read_file_header, only the SOF-marker data should be
-   assumed valid (image size, component IDs, sampling factors); other data
-   such as Huffman tables may not appear until after the SOF.  The overall
-   image size and colorspace can be determined after read_file_header, but not
-   whether or how the data is interleaved.  (This hides which variant JPEG
-   file format is being read.  In particular, for JPEG-in-TIFF the read_header
-   routines might not be scanning standard JPEG markers at all; they could
-   extract the data from TIFF tags.  The user interface will already have
-   opened the input file and possibly read part of the header before
-   read_file_header is called.)
-
-   When reading a file with a nonzero restart interval, the entropy decoder
-   expects to see a correct sequence of restart markers.  In some cases, these
-   markers may be synthesized by the file-format module (a TIFF reader might
-   do so, for example, using tile boundary pointers to determine where the
-   restart intervals fall).  If the incoming data is corrupted, the entropy
-   decoder will read as far as the next JPEG marker, which may or may not be
-   the expected next restart marker.  If it isn't, resync_to_restart is called
-   to try to locate a good place to resume reading.  We make this heuristic a
-   file-format-dependent operation since some file formats may have special
-   info that's not available to the entropy decoder (again, TIFF is an
-   example).  Note that resync_to_restart is NOT called at the end of a scan;
-   it is read_scan_trailer's responsibility to resync there.
-
-   NOTE: for JFIF/raw-JPEG file format, the read_jpeg_data routine is actually
-   supplied by the user interface; the jrdjfif module uses read_jpeg_data
-   internally to scan the input stream.  This makes it possible for the user
-   interface module to single-handedly implement special applications like
-   reading from a non-stdio source.  For JPEG-in-TIFF format, the need for
-   random access will make it impossible for this to work; hence the TIFF
-   header module will override the UI-supplied read_jpeg_data routine.
-   Non-stdio input from a TIFF file will require extensive surgery to the TIFF
-   header module, if indeed it is practical at all.
-
-2. Entropy (Huffman or arithmetic) decoding of the coefficient sequence.
-	entropy_decode_init: prepare for one scan.
-	entropy_decode: decodes and returns an MCU's worth of quantized
-			coefficients per call.
-	entropy_decode_term: finish up after a scan (may be a no-op).
-   This will read raw data by calling the read_jpeg_data method (I don't see
-   any reason to provide a further level of indirection).
-   (This hides which entropy encoding method is in use.)
-
-3. Quantization descaling and zigzag reordering of the elements in each 8x8
-   block.  This will be folded into entropy_decode for efficiency reasons:
-   many of the coefficients are zeroes, and this can be exploited most easily
-   within entropy_decode since the encoding explicitly skips zeroes.
-
-4. MCU disassembly (conversion of a possibly interleaved sequence of 8x8
-   blocks back to separate components in pixel map order).
-	disassemble_init: initialize.  This will be called once per scan.
-	disassemble_MCU:  Given an MCU's worth of dequantized blocks,
-			  distribute them into the proper locations in a
-			  coefficient image array.
-	disassemble_term: clean up at the end of a scan.
-   Probably this should be called once per MCU row and should call the
-   entropy decoder repeatedly to obtain the row's data.  The output is
-   always a multiple of an MCU's dimensions.
-   (An object on the grounds that multiple instantiations might be useful.)
-
-5. Cross-block smoothing per JPEG section K.8 or a similar algorithm.
-	smooth_coefficients: Given three block rows' worth of a single
-			     component, emit a smoothed equivalent of the
-			     middle row.  The "above" and "below" pointers
-			     may be NULL if at top/bottom of image.
-   The pipeline controller will do the necessary buffering to provide the
-   above/below context.  Smoothing will be optional since a good deal of
-   extra memory is needed to buffer the additional block rows.
-   (This object hides the details of the smoothing algorithm.)
-
-6. Inverse DCT transformation of each 8x8 block.
-	reverse_DCT: given an MCU row's worth of blocks, perform inverse
-		     DCT on each block and output the results into an array
-		     of samples.
-   We put this method into the jdmcu module for symmetry with the division of
-   labor in compression.  Note that the actual IDCT code is a separate source
-   file.
-
-7. Upsampling and smoothing: this will be applied to one component at a
-   time.  Note that cross-pixel smoothing, which was a separate step in the
-   prototype code, will now be performed simultaneously with expansion.
-	upsample_init: initialize (precalculate convolution factors, for
-		       example).  This will be called once per scan.
-	upsample: Given a sample array, enlarge it by specified sampling
-		  factors.
-	upsample_term: clean up at the end of a scan.
-   If the current component has vertical sampling factor Vk and the largest
-   sampling factor is Vmax, then the input is always Vk sample rows (whose
-   width is a multiple of Hk) and the output is always Vmax sample rows.
-   Vk additional rows above and below the nominal input rows are also passed
-   for use in cross-pixel smoothing.  At the top and bottom of the image,
-   these extra rows are copies of the first or last actual input row.
-   (This hides whether and how cross-pixel smoothing occurs.)
-
-8. Cropping to the original pixel dimensions (throwing away duplicated
-   pixels at the edges).  This won't be a separate object, just an
-   adjustment of the nominal image size in the pipeline controller.
-
-9. Color space reconversion and gamma adjustment.
-	colorout_init: initialization.  This will be passed the component
-		       data from read_file_header, and will determine the
-		       number of output components.
-	color_convert: convert a specified number of pixel rows.  Input and
-		       output are image arrays of same size but possibly
-		       different numbers of components.
-	colorout_term: cleanup (probably a no-op except for memory dealloc).
-   In practice will usually be given an MCU row's worth of pixel rows, except
-   at the bottom where a smaller number of rows may be left over.  Note that
-   this object works on all the components at once.
-   When quantizing colors, color_convert may be applied to the colormap
-   instead of actual pixel data.  color_convert is called by the color
-   quantizer in this case; the pipeline controller calls color_convert
-   directly only when not quantizing.
-   (Hides all knowledge of color space semantics and conversion.  Remaining
-   modules only need to know the number of JPEG and output components.)
-
-10. Color quantization (used only if a colormapped output format is requested).
-    We use two different strategies depending on whether one-pass (on-the-fly)
-    or two-pass quantization is requested.  Note that the two-pass interface
-    is actually designed to let the quantizer make any number of passes.
-	color_quant_init: initialization, allocate working memory.  In 1-pass
-			  quantization, should call put_color_map.
-	color_quantize: convert a specified number of pixel rows.  Input
-			and output are image arrays of same size, but input
-			is N coefficients and output is only one.  (Used only
-			in 1-pass quantization.)
-	color_quant_prescan: prescan a specified number of pixel rows in
-			     2-pass quantization.
-	color_quant_doit: perform multi-pass color quantization.  Input is a
-			  "big" sample image, output is via put_color_map and
-			  put_pixel_rows.  (Used only in 2-pass quantization.)
-	color_quant_term: cleanup (probably a no-op except for memory dealloc).
-    The input to the color quantizer is always in the unconverted colorspace;
-    its output colormap must be in the converted colorspace.  The quantizer
-    has the choice of which space to work in internally.  It must call
-    color_convert either on its input data or on the colormap it sends to the
-    output module.
-    For one-pass quantization the image is simply processed by color_quantize,
-    a few rows at a time.  For two-pass quantization, the pipeline controller
-    accumulates the output of steps 1-8 into a "big" sample image.  The
-    color_quant_prescan method is invoked during this process so that the
-    quantizer can accumulate statistics.  (If the input file has multiple
-    scans, the prescan may be done during the final scan or as a separate
-    pass.)  At the end of the image, color_quant_doit is called; it must
-    create and output a colormap, then rescan the "big" image and pass mapped
-    data to the output module.  Additional scans of the image could be made
-    before the output pass is done (in fact, prescan could be a no-op).
-    As with entropy parameter optimization, the pipeline controller actually
-    passes an iterator function rather than direct access to the big image.
-    (Hides color quantization algorithm.)
-
-11. Writing of the desired image format.
-	output_init: produce the file header given data from read_file_header.
-	put_color_map: output colormap, if any (called by color quantizer).
-		       If used, must be called before any pixel data is output.
-	put_pixel_rows: output image data in desired format.
-	output_term: finish up at the end.
-    The actual timing of I/O may differ from that suggested by the routine
-    names; for instance, writing of the file header may be delayed until
-    put_color_map time if the actual number of colors is needed in the header.
-    Also, the colormap is available to put_pixel_rows and output_term as well
-    as put_color_map.
-    Note that whether colormapping is needed will be determined by the user
-    interface object prior to method selection.  In implementations that
-    support multiple output formats, the actual output format will also be
-    determined by the user interface.
-    (Hides format of output image and mechanism used to write it.  Note that
-    several other objects know the color model used by the output format.
-    The actual mechanism for writing the file is private to this object and
-    the user interface.)
-
-12. Pipeline control.  This object will provide the "main loop" that invokes
-    all the pipeline objects.  Note that we will need several different main
-    loops depending on the situation (interleaved input or not, whether to
-    apply cross-block smoothing or not, etc).  We may want to divvy up the
-    pipeline controllers into two levels, one that retains control over the
-    whole file and one that is invoked per scan.
-    This object will do most of the memory allocation, since it will provide
-    the working buffers that are the inputs and outputs of the pipeline steps.
-    (An object mostly to support multiple instantiations; however, overall
-    memory management and sequencing of operations are known only here.)
-
-13. Overall control.  This module will provide at least two routines:
-	jpeg_decompress: the main entry point to the decompressor.
-	per_scan_method_selection: called by pipeline controllers for
-				   secondary method selection passes.
-    jpeg_decompress is invoked from the user interface after the UI has
-    selected the input and output files and obtained values for all
-    user-specified options (e.g., output file format, whether to do block
-    smoothing).  jpeg_decompress calls read_file_header, performs basic
-    initialization (e.g., calculating the size of MCUs), does the "global"
-    method selection pass, and finally calls the selected pipeline control
-    object.  (Per-scan method selections will be invoked by the pipeline
-    controller.)
-    Note that jpeg_decompress can't be a method since it is invoked prior to
-    method selection.
-
-14. User interface; this is the architecture's term for "the rest of the
-    application program", i.e., that which invokes the JPEG decompressor.
-    The UI is expected to supply input and output files and values for all
-    operational parameters.  The UI must also supply error handling routines.
-    (This module hides the user interface provided --- command line,
-    interactive, etc.  Except for error handling, the UI calls the portable
-    JPEG code, not the other way around.)
-
-15. A memory management object.  This will be identical to the memory
-    management for compression (and will be the same code, in combined
-    programs).  See above for details.
-
-
-*** Initial method selection ***
-
-The main ugliness in this design is the portion of startup that will select
-which of several instantiations should be used for each of the objects.  (For
-example, Huffman or arithmetic for entropy encoding; one of several pipeline
-controllers depending on interleaving, the size of the image, etc.)  It's not
-really desirable to have a single chunk of code that knows the names of all
-the possible instantiations and the conditions under which to select each one.
-
-The best approach seems to be to provide a selector function for each object
-(group of related method calls).  This function knows about each possible
-instantiation of its object and how to choose the right one; but it doesn't
-know about any other objects.
-
-Note that there will be several rounds of method selection: at initial startup,
-after overall compression parameters are determined (after the file header is
-read, if decompressing), and one in preparation for each scan (this occurs
-more than once if the file is noninterleaved).  Each object method will need
-to be clearly identified as to which round sets it up.
-
-
-*** Implications of DNL marker ***
-
-Some JPEG files may use a DNL marker to postpone definition of the image
-height (this would be useful for a fax-like scanner's output, for instance).
-In these files the SOF marker claims the image height is 0, and you only
-find out the true image height at the end of the first scan.
-
-We could handle these files as follows:
-1. Upon seeing zero image height, replace it by 65535 (the maximum allowed).
-2. When the DNL is found, update the image height in the global image
-   descriptor.
-This implies that pipeline control objects must avoid making copies of the
-image height, and must re-test for termination after each MCU row.  This is
-no big deal.
-
-In situations where image-size data structures are allocated, this approach
-will result in very inefficient use of virtual memory or
-much-larger-than-necessary temporary files.  This seems acceptable for
-something that probably won't be a mainstream usage.  People might have to
-forgo use of memory-hogging options (such as two-pass color quantization or
-noninterleaved JPEG files) if they want efficient conversion of such files.
-(One could improve efficiency by demanding a user-supplied upper bound for the
-height, less than 65536; in most cases it could be much less.)
-
-Alternately, we could insist that DNL-using files be preprocessed by a
-separate program that reads ahead to the DNL, then goes back and fixes the SOF
-marker.  This is a much simpler solution and is probably far more efficient.
-Even if one wants piped input, buffering the first scan of the JPEG file
-needs a lot smaller temp file than is implied by the maximum-height method.
-For this approach we'd simply treat DNL as a no-op in the decompressor (at
-most, check that it matches the SOF image height).
-
-We will not worry about making the compressor capable of outputting DNL.
-Something similar to the first scheme above could be applied if anyone ever
-wants to make that work.
-
-
-*** Memory manager internal structure ***
-
-The memory manager contains the most potential for system dependencies.
-To isolate system dependencies as much as possible, we have broken the
-memory manager into two parts.  There is a reasonably system-independent
-"front end" (jmemmgr.c) and a "back end" that contains only the code
-likely to change across systems.  All of the memory management methods
-outlined above are implemented by the front end.  The back end provides
-the following routines for use by the front end (none of these routines
-are known to the rest of the JPEG code):
-
-jmem_init, jmem_term	system-dependent initialization/shutdown
-
-jget_small, jfree_small	interface to malloc and free library routines
-
-jget_large, jfree_large	interface to FAR malloc/free in MS-DOS machines;
-			otherwise same as jget_small/jfree_small
-
-jmem_available		estimate available memory
-
-jopen_backing_store	create a backing-store object
-
-read_backing_store,	manipulate a backing store object
-write_backing_store,
-close_backing_store
-
-On some systems there will be more than one type of backing-store object
-(specifically, in MS-DOS a backing store file might be an area of extended
-memory as well as a disk file).  jopen_backing_store is responsible for
-choosing how to implement a given object.  The read/write/close routines
-are method pointers in the structure that describes a given object; this
-lets them be different for different object types.
-
-It may be necessary to ensure that backing store objects are explicitly
-released upon abnormal program termination.  (For example, MS-DOS won't free
-extended memory by itself.)  To support this, we will expect the main program
-or surrounding application to arrange to call the free_all method upon
-abnormal termination; this may require a SIGINT signal handler, for instance.
-(We don't want to have the system-dependent module install its own signal
-handler, because that would pre-empt the surrounding application's ability
-to control signal handling.)
-
-
-*** Notes for MS-DOS implementors ***
-
-The standalone cjpeg and djpeg applications can be compiled in "small" memory
-model, at least at the moment; as the code grows we may be forced to switch to
-"medium" model.  (Small = both code and data pointers are near by default;
-medium = far code pointers, near data pointers.)  Medium model will slow down
-calls through method pointers, but I don't think this will amount to any
-significant speed penalty.
-
-When integrating the JPEG code into a larger application, it's a good idea to
-stay with a small-data-space model if possible.  An 8K stack is much more than
-sufficient for the JPEG code, and its static data requirements are less than
-1K.  When executed, it will typically malloc about 10K-20K worth of near heap
-space (and lots of far heap, but that doesn't count in this calculation).
-This figure will vary depending on image size and other factors, but figuring
-30K should be more than sufficient.  Thus you have about 25K available for
-other modules' static data and near heap requirements before you need to go to
-a larger memory model.  The C library's static data will account for several K
-of this, but that still leaves a good deal for your needs.  (If you are tight
-on space, you could reduce JPEG_BUF_SIZE from 4K to 1K to save 3K of near heap
-space.)
-
-As the code is improved, we will endeavor to hold the near data requirements
-to the range given above.  This does imply that certain data structures will
-be allocated as FAR although they would fit in near space if we assumed the
-JPEG code is stand-alone.  (The LZW tables in jrdgif/jwrgif are examples.)
-To make an optimal implementation, you might want to move these structures
-back to near heap if you know there is sufficient space.
-
-FAR data space may also be a tight resource when you are dealing with large
-images.  The most memory-intensive case is decompression with two-pass color
-quantization.  This requires a 128Kb color histogram plus strip buffers
-amounting to about 150 bytes per column for typical sampling ratios (eg, about
-96000 bytes for a 640-pixel-wide image).  You may not be able to process wide
-images if you have large data structures of your own.
-
-
-*** Potential optimizations ***
-
-For colormapped input formats it might be worthwhile to merge the input file
-reading and the colorspace conversion steps; in other words, do the colorspace
-conversion by hacking up the colormap before inputting the image body, rather
-than doing the conversion on each pixel independently.  Not clear if this is
-worth the uglification involved.  In the above design for the compressor, only
-the colorspace conversion step ever sees the output of get_input_row, so this
-sort of thing could be done via private agreement between those two modules.
-
-Level shift from 0..255 to -128..127 may be done either during colorspace
-conversion, or at the moment of converting an 8x8 sample block into the format
-used by the DCT step (which will be signed short or long int).  This could be
-selectable by a compile-time flag, so that the intermediate steps can work on
-either signed or unsigned chars as samples, whichever is most easily handled
-by the platform.  However, making sure that rounding is done right will be a
-lot easier if we can assume positive values.  At the moment I think that
-benefit is worth the overhead of "& 0xFF" when reading out sample values on
-signed-char-only machines.
diff --git a/cderror.h b/cderror.h
new file mode 100644
index 0000000..6479a4d
--- /dev/null
+++ b/cderror.h
@@ -0,0 +1,135 @@
+/*
+ * cderror.h
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file defines the error and message codes for the cjpeg/djpeg
+ * applications.  These strings are not needed as part of the JPEG library
+ * proper.
+ * Edit this file to add new codes, or to translate the message strings to
+ * some other language.
+ */
+
+
+/* To define the enum list of message codes, include this file without
+ * defining JMAKE_MSG_TABLE.  To create the message string table, include it
+ * again with JMAKE_MSG_TABLE defined (this should be done in just one module).
+ */
+
+#ifdef JMAKE_MSG_TABLE
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define addon_message_table	cdMsgTable
+#endif
+
+const char * const addon_message_table[] = {
+
+#define JMESSAGE(code,string)	string ,
+
+#else /* not JMAKE_MSG_TABLE */
+
+typedef enum {
+
+#define JMESSAGE(code,string)	code ,
+
+#endif /* JMAKE_MSG_TABLE */
+
+JMESSAGE(JMSG_FIRSTADDONCODE=1000, NULL) /* Must be first entry! */
+
+#ifdef BMP_SUPPORTED
+JMESSAGE(JERR_BMP_BADCMAP, "Unsupported BMP colormap format")
+JMESSAGE(JERR_BMP_BADDEPTH, "Only 8- and 24-bit BMP files are supported")
+JMESSAGE(JERR_BMP_BADHEADER, "Invalid BMP file: bad header length")
+JMESSAGE(JERR_BMP_BADPLANES, "Invalid BMP file: biPlanes not equal to 1")
+JMESSAGE(JERR_BMP_COLORSPACE, "BMP output must be grayscale or RGB")
+JMESSAGE(JERR_BMP_COMPRESSED, "Sorry, compressed BMPs not yet supported")
+JMESSAGE(JERR_BMP_NOT, "Not a BMP file - does not start with BM")
+JMESSAGE(JTRC_BMP, "%ux%u 24-bit BMP image")
+JMESSAGE(JTRC_BMP_MAPPED, "%ux%u 8-bit colormapped BMP image")
+JMESSAGE(JTRC_BMP_OS2, "%ux%u 24-bit OS2 BMP image")
+JMESSAGE(JTRC_BMP_OS2_MAPPED, "%ux%u 8-bit colormapped OS2 BMP image")
+#endif /* BMP_SUPPORTED */
+
+#ifdef GIF_SUPPORTED
+JMESSAGE(JERR_GIF_BUG, "GIF output got confused")
+JMESSAGE(JERR_GIF_CODESIZE, "Bogus GIF codesize %d")
+JMESSAGE(JERR_GIF_COLORSPACE, "GIF output must be grayscale or RGB")
+JMESSAGE(JERR_GIF_IMAGENOTFOUND, "Too few images in GIF file")
+JMESSAGE(JERR_GIF_NOT, "Not a GIF file")
+JMESSAGE(JTRC_GIF, "%ux%ux%d GIF image")
+JMESSAGE(JTRC_GIF_BADVERSION,
+	 "Warning: unexpected GIF version number '%c%c%c'")
+JMESSAGE(JTRC_GIF_EXTENSION, "Ignoring GIF extension block of type 0x%02x")
+JMESSAGE(JTRC_GIF_NONSQUARE, "Caution: nonsquare pixels in input")
+JMESSAGE(JWRN_GIF_BADDATA, "Corrupt data in GIF file")
+JMESSAGE(JWRN_GIF_CHAR, "Bogus char 0x%02x in GIF file, ignoring")
+JMESSAGE(JWRN_GIF_ENDCODE, "Premature end of GIF image")
+JMESSAGE(JWRN_GIF_NOMOREDATA, "Ran out of GIF bits")
+#endif /* GIF_SUPPORTED */
+
+#ifdef PPM_SUPPORTED
+JMESSAGE(JERR_PPM_COLORSPACE, "PPM output must be grayscale or RGB")
+JMESSAGE(JERR_PPM_NONNUMERIC, "Nonnumeric data in PPM file")
+JMESSAGE(JERR_PPM_NOT, "Not a PPM file")
+JMESSAGE(JTRC_PGM, "%ux%u PGM image")
+JMESSAGE(JTRC_PGM_TEXT, "%ux%u text PGM image")
+JMESSAGE(JTRC_PPM, "%ux%u PPM image")
+JMESSAGE(JTRC_PPM_TEXT, "%ux%u text PPM image")
+#endif /* PPM_SUPPORTED */
+
+#ifdef RLE_SUPPORTED
+JMESSAGE(JERR_RLE_BADERROR, "Bogus error code from RLE library")
+JMESSAGE(JERR_RLE_COLORSPACE, "RLE output must be grayscale or RGB")
+JMESSAGE(JERR_RLE_DIMENSIONS, "Image dimensions (%ux%u) too large for RLE")
+JMESSAGE(JERR_RLE_EMPTY, "Empty RLE file")
+JMESSAGE(JERR_RLE_EOF, "Premature EOF in RLE header")
+JMESSAGE(JERR_RLE_MEM, "Insufficient memory for RLE header")
+JMESSAGE(JERR_RLE_NOT, "Not an RLE file")
+JMESSAGE(JERR_RLE_TOOMANYCHANNELS, "Cannot handle %d output channels for RLE")
+JMESSAGE(JERR_RLE_UNSUPPORTED, "Cannot handle this RLE setup")
+JMESSAGE(JTRC_RLE, "%ux%u full-color RLE file")
+JMESSAGE(JTRC_RLE_FULLMAP, "%ux%u full-color RLE file with map of length %d")
+JMESSAGE(JTRC_RLE_GRAY, "%ux%u grayscale RLE file")
+JMESSAGE(JTRC_RLE_MAPGRAY, "%ux%u grayscale RLE file with map of length %d")
+JMESSAGE(JTRC_RLE_MAPPED, "%ux%u colormapped RLE file with map of length %d")
+#endif /* RLE_SUPPORTED */
+
+#ifdef TARGA_SUPPORTED
+JMESSAGE(JERR_TGA_BADCMAP, "Unsupported Targa colormap format")
+JMESSAGE(JERR_TGA_BADPARMS, "Invalid or unsupported Targa file")
+JMESSAGE(JERR_TGA_COLORSPACE, "Targa output must be grayscale or RGB")
+JMESSAGE(JTRC_TGA, "%ux%u RGB Targa image")
+JMESSAGE(JTRC_TGA_GRAY, "%ux%u grayscale Targa image")
+JMESSAGE(JTRC_TGA_MAPPED, "%ux%u colormapped Targa image")
+#else
+JMESSAGE(JERR_TGA_NOTCOMP, "Targa support was not compiled")
+#endif /* TARGA_SUPPORTED */
+
+JMESSAGE(JERR_BAD_CMAP_FILE,
+	 "Color map file is invalid or of unsupported format")
+JMESSAGE(JERR_TOO_MANY_COLORS,
+	 "Output file format cannot handle %d colormap entries")
+JMESSAGE(JERR_UNGETC_FAILED, "ungetc failed")
+#ifdef TARGA_SUPPORTED
+JMESSAGE(JERR_UNKNOWN_FORMAT,
+	 "Unrecognized input file format --- perhaps you need -targa")
+#else
+JMESSAGE(JERR_UNKNOWN_FORMAT, "Unrecognized input file format")
+#endif
+JMESSAGE(JERR_UNSUPPORTED_FORMAT, "Unsupported output file format")
+
+#ifdef JMAKE_MSG_TABLE
+
+  NULL
+};
+
+#else /* not JMAKE_MSG_TABLE */
+
+  JMSG_LASTADDONCODE
+} ADDON_MESSAGE_CODE;
+
+#endif /* JMAKE_MSG_TABLE */
+
+#undef JMESSAGE
diff --git a/cdjpeg.h b/cdjpeg.h
new file mode 100644
index 0000000..35acc35
--- /dev/null
+++ b/cdjpeg.h
@@ -0,0 +1,124 @@
+/*
+ * cdjpeg.h
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains common declarations for the sample applications
+ * cjpeg and djpeg.  It is NOT used by the core JPEG library.
+ */
+
+#define JPEG_CJPEG_DJPEG	/* define proper options in jconfig.h */
+#define JPEG_INTERNAL_OPTIONS	/* cjpeg.c,djpeg.c need to see xxx_SUPPORTED */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jerror.h"		/* get library error codes too */
+#include "cderror.h"		/* get application-specific error codes */
+
+
+/*
+ * Object interface for cjpeg's source file decoding modules
+ */
+
+typedef struct cjpeg_source_struct * cjpeg_source_ptr;
+
+struct cjpeg_source_struct {
+  JMETHOD(void, start_input, (j_compress_ptr cinfo,
+			      cjpeg_source_ptr sinfo));
+  JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo,
+				       cjpeg_source_ptr sinfo));
+  JMETHOD(void, finish_input, (j_compress_ptr cinfo,
+			       cjpeg_source_ptr sinfo));
+
+  FILE *input_file;
+
+  JSAMPARRAY buffer;
+  JDIMENSION buffer_height;
+};
+
+
+/*
+ * Object interface for djpeg's output file encoding modules
+ */
+
+typedef struct djpeg_dest_struct * djpeg_dest_ptr;
+
+struct djpeg_dest_struct {
+  /* start_output is called after jpeg_start_decompress finishes.
+   * The color map will be ready at this time, if one is needed.
+   */
+  JMETHOD(void, start_output, (j_decompress_ptr cinfo,
+			       djpeg_dest_ptr dinfo));
+  /* Emit the specified number of pixel rows from the buffer. */
+  JMETHOD(void, put_pixel_rows, (j_decompress_ptr cinfo,
+				 djpeg_dest_ptr dinfo,
+				 JDIMENSION rows_supplied));
+  /* Finish up at the end of the image. */
+  JMETHOD(void, finish_output, (j_decompress_ptr cinfo,
+				djpeg_dest_ptr dinfo));
+
+  /* Target file spec; filled in by djpeg.c after object is created. */
+  FILE * output_file;
+
+  /* Output pixel-row buffer.  Created by module init or start_output.
+   * Width is cinfo->output_width * cinfo->output_components;
+   * height is buffer_height.
+   */
+  JSAMPARRAY buffer;
+  JDIMENSION buffer_height;
+};
+
+
+/*
+ * cjpeg/djpeg may need to perform extra passes to convert to or from
+ * the source/destination file format.  The JPEG library does not know
+ * about these passes, but we'd like them to be counted by the progress
+ * monitor.  We use an expanded progress monitor object to hold the
+ * additional pass count.
+ */
+
+struct cdjpeg_progress_mgr {
+  struct jpeg_progress_mgr pub;	/* fields known to JPEG library */
+  int completed_extra_passes;	/* extra passes completed */
+  int total_extra_passes;	/* total extra */
+  /* last printed percentage stored here to avoid multiple printouts */
+  int percent_done;
+};
+
+typedef struct cdjpeg_progress_mgr * cd_progress_ptr;
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jinit_read_bmp		jIRdBMP
+#define jinit_write_bmp		jIWrBMP
+#define jinit_read_gif		jIRdGIF
+#define jinit_write_gif		jIWrGIF
+#define jinit_read_ppm		jIRdPPM
+#define jinit_write_ppm		jIWrPPM
+#define jinit_read_rle		jIRdRLE
+#define jinit_write_rle		jIWrRLE
+#define jinit_read_targa	jIRdTarga
+#define jinit_write_targa	jIWrTarga
+#define read_color_map		RdCMap
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+/* Module selection routines for I/O modules. */
+
+EXTERN cjpeg_source_ptr jinit_read_bmp JPP((j_compress_ptr cinfo));
+EXTERN djpeg_dest_ptr jinit_write_bmp JPP((j_decompress_ptr cinfo,
+					   boolean is_os2));
+EXTERN cjpeg_source_ptr jinit_read_gif JPP((j_compress_ptr cinfo));
+EXTERN djpeg_dest_ptr jinit_write_gif JPP((j_decompress_ptr cinfo));
+EXTERN cjpeg_source_ptr jinit_read_ppm JPP((j_compress_ptr cinfo));
+EXTERN djpeg_dest_ptr jinit_write_ppm JPP((j_decompress_ptr cinfo));
+EXTERN cjpeg_source_ptr jinit_read_rle JPP((j_compress_ptr cinfo));
+EXTERN djpeg_dest_ptr jinit_write_rle JPP((j_decompress_ptr cinfo));
+EXTERN cjpeg_source_ptr jinit_read_targa JPP((j_compress_ptr cinfo));
+EXTERN djpeg_dest_ptr jinit_write_targa JPP((j_decompress_ptr cinfo));
+
+/* Other global routines */
+
+EXTERN void read_color_map JPP((j_decompress_ptr cinfo, FILE * infile));
diff --git a/change.log b/change.log
new file mode 100644
index 0000000..ac2bbea
--- /dev/null
+++ b/change.log
@@ -0,0 +1,39 @@
+CHANGE LOG for Independent JPEG Group's JPEG software
+
+
+Version 5  24-Sep-94
+--------------------
+
+Version 5 represents a nearly complete redesign and rewrite of the IJG
+software.  Major user-visible changes include:
+  * Automatic configuration simplifies installation for most Unix systems.
+  * A range of speed vs. image quality tradeoffs are supported.
+    This includes resizing of an image during decompression: scaling down
+    by a factor of 1/2, 1/4, or 1/8 is handled very efficiently.
+  * New programs rdjpgcom and wrjpgcom allow insertion and extraction
+    of text comments in a JPEG file.
+
+The application programmer's interface to the library has changed completely.
+Notable improvements include:
+  * We have eliminated the use of callback routines for handling the
+    uncompressed image data.  The application now sees the library as a
+    set of routines that it calls to read or write image data on a
+    scanline-by-scanline basis.
+  * The application image data is represented in a conventional interleaved-
+    pixel format, rather than as a separate array for each color channel.
+    This can save a copying step in many programs.
+  * The handling of compressed data has been cleaned up: the application can
+    supply routines to source or sink the compressed data.  It is possible to
+    suspend processing on source/sink buffer overrun, although this is not
+    supported in all operating modes.
+  * All static state has been eliminated from the library, so that multiple
+    instances of compression or decompression can be active concurrently.
+  * JPEG abbreviated datastream formats are supported, ie, quantization and
+    Huffman tables can be stored separately from the image data.
+  * And not only that, but the documentation of the library has improved
+    considerably!
+
+
+The last widely used release before the version 5 rewrite was version 4A of
+18-Feb-93.  Change logs before that point have been discarded, since they
+are not of much interest after the rewrite.
diff --git a/cjpeg.1 b/cjpeg.1
index c76a912..80c2110 100644
--- a/cjpeg.1
+++ b/cjpeg.1
@@ -1,46 +1,10 @@
-.TH CJPEG 1 "4 November 1992"
+.TH CJPEG 1 "30 August 1994"
 .SH NAME
 cjpeg \- compress an image file to a JPEG file
 .SH SYNOPSIS
 .B cjpeg
 [
-.BI \-quality " N"
-]
-[
-.B \-grayscale
-]
-[
-.B \-optimize
-]
-[
-.B \-targa
-]
-[
-.BI \-maxmemory " N"
-]
-[
-.BI \-restart " N"
-]
-[
-.BI \-smooth " N"
-]
-[
-.B \-verbose
-]
-[
-.B \-debug
-]
-[
-.B \-arithmetic
-]
-[
-.B \-nointerleave
-]
-[
-.BI \-qtables " file"
-]
-[
-.BI \-sample " HxV[,...]"
+.I options
 ]
 [
 .I filename
@@ -52,7 +16,7 @@
 compresses the named image file, or the standard input if no file is
 named, and produces a JPEG/JFIF file on the standard output.
 The currently supported input file formats are: PPM (PBMPLUS color
-format), PGM (PBMPLUS gray-scale format), GIF, Targa, and RLE (Utah Raster
+format), PGM (PBMPLUS gray-scale format), BMP, GIF, Targa, and RLE (Utah Raster
 Toolkit format).  (RLE is supported only if the URT library is available.)
 .SH OPTIONS
 All switch names may be abbreviated; for example,
@@ -104,6 +68,7 @@
 to make
 .B cjpeg
 treat the input as Targa format.
+For most Targa files, you won't need this switch.
 .PP
 The
 .B \-quality
@@ -136,16 +101,25 @@
 considered optional in the JPEG standard.
 .B cjpeg
 emits a warning message when you give such a quality value, because some
-commercial JPEG programs may be unable to decode the resulting file.)
+commercial JPEG programs may be unable to decode the resulting file.  Use
+.B \-baseline
+if you need to ensure compatibility at low quality values.)
 .PP
 Switches for advanced users:
 .TP
-.BI \-maxmemory " N"
-Set limit for amount of memory to use in processing large images.  Value is
-in thousands of bytes, or millions of bytes if "M" is attached to the
-number.  For example,
-.B \-max 4m
-selects 4000000 bytes.  If more space is needed, temporary files will be used.
+.B \-dct int
+Use integer DCT method (default).
+.TP
+.B \-dct fast
+Use fast integer DCT (less accurate).
+.TP
+.B \-dct float
+Use floating-point DCT method.
+The floating-point method is the most accurate, but will be the slowest unless
+your machine has very fast floating-point hardware.  Also note that results of
+the floating-point method may vary slightly across machines, while the integer
+methods should give the same results everywhere.  The fast integer method is
+much less accurate than the other two.
 .TP
 .BI \-restart " N"
 Emit a JPEG restart marker every N MCU rows, or every N MCU blocks if "B" is
@@ -157,6 +131,16 @@
 Smooth the input image to eliminate dithering noise.  N, ranging from 1 to
 100, indicates the strength of smoothing.  0 (the default) means no smoothing.
 .TP
+.BI \-maxmemory " N"
+Set limit for amount of memory to use in processing large images.  Value is
+in thousands of bytes, or millions of bytes if "M" is attached to the
+number.  For example,
+.B \-max 4m
+selects 4000000 bytes.  If more space is needed, temporary files will be used.
+.TP
+.BI \-outfile " name"
+Send output image to the named file, not to standard output.
+.TP
 .B \-verbose
 Enable debug printout.  More
 .BR \-v 's
@@ -191,6 +175,10 @@
 Use arithmetic coding rather than Huffman coding.  (Not currently
 supported for legal reasons.)
 .TP
+.B \-baseline
+Force a baseline JPEG file to be generated.  This clamps quantization values
+to 8 bits even at low quality settings.
+.TP
 .B \-nointerleave
 Generate noninterleaved JPEG file (not yet supported).
 .TP
@@ -199,11 +187,15 @@
 contain one to four tables (64 values each) as plain text.  Comments preceded
 by '#' may be included in the file.  The tables are implicitly numbered
 0,1,etc.  If
-.B \-quality
-N is also specified, the values in the file are scaled according to
+.BI \-quality " N"
+is also specified, the values in the file are scaled according to
 .BR cjpeg 's
 quality scaling curve.
 .TP
+.BI \-qslots " N[,...]"
+Select which quantization table to use for each color component.  By default,
+table 0 is used for luminance and table 1 for chrominance components.
+.TP
 .BI \-sample " HxV[,...]"
 Set JPEG sampling factors.  If you specify fewer H/V pairs than there are
 components, the remaining components are set to 1x1 sampling.  The default
@@ -224,6 +216,34 @@
 .I 60 foo.ppm
 .B >
 .I foo.jpg
+.SH HINTS
+Color GIF files are not the ideal input for JPEG; JPEG is really intended for
+compressing full-color (24-bit) images.  In particular, don't try to convert
+cartoons, line drawings, and other images that have only a few distinct
+colors.  GIF works great on these, JPEG does not.  If you want to convert a
+GIF to JPEG, you should experiment with
+.BR cjpeg 's
+.B \-quality
+and
+.B \-smooth
+options to get a satisfactory conversion.
+.B \-smooth 10
+or so is often helpful.
+.PP
+Avoid running an image through a series of JPEG compression/decompression
+cycles.  Image quality loss will accumulate; after ten or so cycles the image
+may be noticeably worse than it was after one cycle.  It's best to use a
+lossless format while manipulating an image, then convert to JPEG format when
+you are ready to file the image away.
+.PP
+The
+.B \-optimize
+option to
+.B cjpeg
+is worth using when you are making a "final" version for posting or archiving.
+It's also a win when you are using low quality settings to make very small
+JPEG files; the percentage improvement is often a lot more than it is on
+larger files.
 .SH ENVIRONMENT
 .TP
 .B JPEGMEM
@@ -236,7 +256,9 @@
 itself is overridden by an explicit
 .BR \-maxmemory .
 .SH SEE ALSO
-.BR djpeg (1)
+.BR djpeg (1),
+.BR rdjpgcom (1),
+.BR wrjpgcom (1)
 .br
 .BR ppm (5),
 .BR pgm (5)
@@ -246,12 +268,12 @@
 .SH AUTHOR
 Independent JPEG Group
 .SH BUGS
-Arithmetic coding and interleaved output not yet supported.
+Arithmetic coding is not supported for legal reasons.
 .PP
-Not all variants of Targa file format are supported.
+Not all variants of BMP and Targa file formats are supported.
 .PP
 The
-.B -targa
+.B \-targa
 switch is not a bug, it's a feature.  (It would be a bug if the Targa format
 designers had not been clueless.)
 .PP
diff --git a/cjpeg.c b/cjpeg.c
new file mode 100644
index 0000000..33347f3
--- /dev/null
+++ b/cjpeg.c
@@ -0,0 +1,877 @@
+/*
+ * cjpeg.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a command-line user interface for the JPEG compressor.
+ * It should work on any system with Unix- or MS-DOS-style command lines.
+ *
+ * Two different command line styles are permitted, depending on the
+ * compile-time switch TWO_FILE_COMMANDLINE:
+ *	cjpeg [options]  inputfile outputfile
+ *	cjpeg [options]  [inputfile]
+ * In the second style, output is always to standard output, which you'd
+ * normally redirect to a file or pipe to some other program.  Input is
+ * either from a named file or from standard input (typically redirected).
+ * The second style is convenient on Unix but is unhelpful on systems that
+ * don't support pipes.  Also, you MUST use the first style if your system
+ * doesn't do binary I/O to stdin/stdout.
+ * To simplify script writing, the "-outfile" switch is provided.  The syntax
+ *	cjpeg [options]  -outfile outputfile  inputfile
+ * works regardless of which command line style is used.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+#define JMAKE_MSG_TABLE
+#include "cderror.h"		/* create message string table */
+#include "jversion.h"		/* for version message */
+
+#include <ctype.h>		/* to declare isupper(), tolower() */
+#ifdef NEED_SIGNAL_CATCHER
+#include <signal.h>		/* to declare signal() */
+#endif
+#ifdef USE_SETMODE
+#include <fcntl.h>		/* to declare setmode()'s parameter macros */
+/* If you have setmode() but not <io.h>, just delete this line: */
+#include <io.h>			/* to declare setmode() */
+#endif
+
+#ifdef USE_CCOMMAND		/* command-line reader for Macintosh */
+#ifdef __MWERKS__
+#include <SIOUX.h>              /* Metrowerks declares it here */
+#endif
+#ifdef THINK_C
+#include <console.h>		/* Think declares it here */
+#endif
+#endif
+
+#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
+#define READ_BINARY	"r"
+#define WRITE_BINARY	"w"
+#else
+#define READ_BINARY	"rb"
+#define WRITE_BINARY	"wb"
+#endif
+
+#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
+#define EXIT_FAILURE  1
+#endif
+#ifndef EXIT_SUCCESS
+#ifdef VMS
+#define EXIT_SUCCESS  1		/* VMS is very nonstandard */
+#else
+#define EXIT_SUCCESS  0
+#endif
+#endif
+#ifndef EXIT_WARNING
+#ifdef VMS
+#define EXIT_WARNING  1		/* VMS is very nonstandard */
+#else
+#define EXIT_WARNING  2
+#endif
+#endif
+
+
+/*
+ * This routine determines what format the input file is,
+ * and selects the appropriate input-reading module.
+ *
+ * To determine which family of input formats the file belongs to,
+ * we may look only at the first byte of the file, since C does not
+ * guarantee that more than one character can be pushed back with ungetc.
+ * Looking at additional bytes would require one of these approaches:
+ *     1) assume we can fseek() the input file (fails for piped input);
+ *     2) assume we can push back more than one character (works in
+ *        some C implementations, but unportable);
+ *     3) provide our own buffering (breaks input readers that want to use
+ *        stdio directly, such as the RLE library);
+ * or  4) don't put back the data, and modify the input_init methods to assume
+ *        they start reading after the start of file (also breaks RLE library).
+ * #1 is attractive for MS-DOS but is untenable on Unix.
+ *
+ * The most portable solution for file types that can't be identified by their
+ * first byte is to make the user tell us what they are.  This is also the
+ * only approach for "raw" file types that contain only arbitrary values.
+ * We presently apply this method for Targa files.  Most of the time Targa
+ * files start with 0x00, so we recognize that case.  Potentially, however,
+ * a Targa file could start with any byte value (byte 0 is the length of the
+ * seldom-used ID field), so we provide a switch to force Targa input mode.
+ */
+
+static boolean is_targa;	/* records user -targa switch */
+
+
+LOCAL cjpeg_source_ptr
+select_file_type (j_compress_ptr cinfo, FILE * infile)
+{
+  int c;
+
+  if (is_targa) {
+#ifdef TARGA_SUPPORTED
+    return jinit_read_targa(cinfo);
+#else
+    ERREXIT(cinfo, JERR_TGA_NOTCOMP);
+#endif
+  }
+
+  if ((c = getc(infile)) == EOF)
+    ERREXIT(cinfo, JERR_INPUT_EMPTY);
+  if (ungetc(c, infile) == EOF)
+    ERREXIT(cinfo, JERR_UNGETC_FAILED);
+
+  switch (c) {
+#ifdef BMP_SUPPORTED
+  case 'B':
+    return jinit_read_bmp(cinfo);
+#endif
+#ifdef GIF_SUPPORTED
+  case 'G':
+    return jinit_read_gif(cinfo);
+#endif
+#ifdef PPM_SUPPORTED
+  case 'P':
+    return jinit_read_ppm(cinfo);
+#endif
+#ifdef RLE_SUPPORTED
+  case 'R':
+    return jinit_read_rle(cinfo);
+#endif
+#ifdef TARGA_SUPPORTED
+  case 0x00:
+    return jinit_read_targa(cinfo);
+#endif
+  default:
+    ERREXIT(cinfo, JERR_UNKNOWN_FORMAT);
+    break;
+  }
+
+  return NULL;			/* suppress compiler warnings */
+}
+
+
+/*
+ * Signal catcher to ensure that temporary files are removed before aborting.
+ * NB: for Amiga Manx C this is actually a global routine named _abort();
+ * we put "#define signal_catcher _abort" in jconfig.h.  Talk about bogus...
+ */
+
+#ifdef NEED_SIGNAL_CATCHER
+
+static j_common_ptr sig_cinfo;
+
+GLOBAL void
+signal_catcher (int signum)
+{
+  if (sig_cinfo != NULL) {
+    if (sig_cinfo->err != NULL) /* turn off trace output */
+      sig_cinfo->err->trace_level = 0;
+    jpeg_destroy(sig_cinfo);	/* clean up memory allocation & temp files */
+  }
+  exit(EXIT_FAILURE);
+}
+
+#endif
+
+
+/*
+ * Optional routine to display a percent-done figure on stderr.
+ */
+
+#ifdef PROGRESS_REPORT
+
+METHODDEF void
+progress_monitor (j_common_ptr cinfo)
+{
+  cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
+  int total_passes = prog->pub.total_passes + prog->total_extra_passes;
+  int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
+
+  if (percent_done != prog->percent_done) {
+    prog->percent_done = percent_done;
+    if (total_passes > 1) {
+      fprintf(stderr, "\rPass %d/%d: %3d%% ",
+	      prog->pub.completed_passes + prog->completed_extra_passes + 1,
+	      total_passes, percent_done);
+    } else {
+      fprintf(stderr, "\r %3d%% ", percent_done);
+    }
+    fflush(stderr);
+  }
+}
+
+#endif
+
+
+/*
+ * Argument-parsing code.
+ * The switch parser is designed to be useful with DOS-style command line
+ * syntax, ie, intermixed switches and file names, where only the switches
+ * to the left of a given file name affect processing of that file.
+ * The main program in this file doesn't actually use this capability...
+ */
+
+
+static const char * progname;	/* program name for error messages */
+static char * outfilename;	/* for -outfile switch */
+
+
+LOCAL void
+usage (void)
+/* complain about bad command line */
+{
+  fprintf(stderr, "usage: %s [switches] ", progname);
+#ifdef TWO_FILE_COMMANDLINE
+  fprintf(stderr, "inputfile outputfile\n");
+#else
+  fprintf(stderr, "[inputfile]\n");
+#endif
+
+  fprintf(stderr, "Switches (names may be abbreviated):\n");
+  fprintf(stderr, "  -quality N     Compression quality (0..100; 5-95 is useful range)\n");
+  fprintf(stderr, "  -grayscale     Create monochrome JPEG file\n");
+#ifdef ENTROPY_OPT_SUPPORTED
+  fprintf(stderr, "  -optimize      Optimize Huffman table (smaller file, but slow compression)\n");
+#endif
+#ifdef TARGA_SUPPORTED
+  fprintf(stderr, "  -targa         Input file is Targa format (usually not needed)\n");
+#endif
+  fprintf(stderr, "Switches for advanced users:\n");
+#ifdef DCT_ISLOW_SUPPORTED
+  fprintf(stderr, "  -dct int       Use integer DCT method%s\n",
+	  (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+  fprintf(stderr, "  -dct fast      Use fast integer DCT (less accurate)%s\n",
+	  (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+  fprintf(stderr, "  -dct float     Use floating-point DCT method%s\n",
+	  (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
+#endif
+  fprintf(stderr, "  -restart N     Set restart interval in rows, or in blocks with B\n");
+#ifdef INPUT_SMOOTHING_SUPPORTED
+  fprintf(stderr, "  -smooth N      Smooth dithered input (N=1..100 is strength)\n");
+#endif
+  fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
+  fprintf(stderr, "  -outfile name  Specify name for output file\n");
+  fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
+  fprintf(stderr, "Switches for wizards:\n");
+#ifdef C_ARITH_CODING_SUPPORTED
+  fprintf(stderr, "  -arithmetic    Use arithmetic coding\n");
+#endif
+  fprintf(stderr, "  -baseline      Force baseline output\n");
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+  fprintf(stderr, "  -nointerleave  Create noninterleaved JPEG file\n");
+#endif
+  fprintf(stderr, "  -qtables file  Use quantization tables given in file\n");
+  fprintf(stderr, "  -qslots N[,...]    Set component quantization tables\n");
+  fprintf(stderr, "  -sample HxV[,...]  Set component sampling factors\n");
+  exit(EXIT_FAILURE);
+}
+
+
+LOCAL boolean
+keymatch (char * arg, const char * keyword, int minchars)
+/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
+/* keyword is the constant keyword (must be lower case already), */
+/* minchars is length of minimum legal abbreviation. */
+{
+  register int ca, ck;
+  register int nmatched = 0;
+
+  while ((ca = *arg++) != '\0') {
+    if ((ck = *keyword++) == '\0')
+      return FALSE;		/* arg longer than keyword, no good */
+    if (isupper(ca))		/* force arg to lcase (assume ck is already) */
+      ca = tolower(ca);
+    if (ca != ck)
+      return FALSE;		/* no good */
+    nmatched++;			/* count matched characters */
+  }
+  /* reached end of argument; fail if it's too short for unique abbrev */
+  if (nmatched < minchars)
+    return FALSE;
+  return TRUE;			/* A-OK */
+}
+
+
+LOCAL int
+qt_getc (FILE * file)
+/* Read next char, skipping over any comments (# to end of line) */
+/* A comment/newline sequence is returned as a newline */
+{
+  register int ch;
+  
+  ch = getc(file);
+  if (ch == '#') {
+    do {
+      ch = getc(file);
+    } while (ch != '\n' && ch != EOF);
+  }
+  return ch;
+}
+
+
+LOCAL long
+read_qt_integer (FILE * file)
+/* Read an unsigned decimal integer from a quantization-table file */
+/* Swallows one trailing character after the integer */
+{
+  register int ch;
+  register long val;
+  
+  /* Skip any leading whitespace, detect EOF */
+  do {
+    ch = qt_getc(file);
+    if (ch == EOF)
+      return EOF;
+  } while (isspace(ch));
+  
+  if (! isdigit(ch)) {
+    fprintf(stderr, "%s: bogus data in quantization file\n", progname);
+    exit(EXIT_FAILURE);
+  }
+
+  val = ch - '0';
+  while (ch = qt_getc(file), isdigit(ch)) {
+    val *= 10;
+    val += ch - '0';
+  }
+  return val;
+}
+
+
+LOCAL void
+read_quant_tables (j_compress_ptr cinfo, char * filename, int scale_factor,
+		   boolean force_baseline)
+/* Read a set of quantization tables from the specified file.
+ * The file is plain ASCII text: decimal numbers with whitespace between.
+ * Comments preceded by '#' may be included in the file.
+ * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
+ * The tables are implicitly numbered 0,1,etc.
+ * NOTE: does not affect the qslots mapping, which will default to selecting
+ * table 0 for luminance (or primary) components, 1 for chrominance components.
+ * You must use -qslots if you want a different component->table mapping.
+ */
+{
+  /* ZIG[i] is the zigzag-order position of the i'th element of a DCT block */
+  /* read in natural order (left to right, top to bottom). */
+  static const int ZIG[DCTSIZE2] = {
+     0,  1,  5,  6, 14, 15, 27, 28,
+     2,  4,  7, 13, 16, 26, 29, 42,
+     3,  8, 12, 17, 25, 30, 41, 43,
+     9, 11, 18, 24, 31, 40, 44, 53,
+    10, 19, 23, 32, 39, 45, 52, 54,
+    20, 22, 33, 38, 46, 51, 55, 60,
+    21, 34, 37, 47, 50, 56, 59, 61,
+    35, 36, 48, 49, 57, 58, 62, 63
+    };
+  FILE * fp;
+  int tblno, i;
+  long val;
+  unsigned int table[DCTSIZE2];
+
+  if ((fp = fopen(filename, "r")) == NULL) {
+    fprintf(stderr, "%s: can't open %s\n", progname, filename);
+    exit(EXIT_FAILURE);
+  }
+  tblno = 0;
+
+  while ((val = read_qt_integer(fp)) != EOF) { /* read 1st element of table */
+    if (tblno >= NUM_QUANT_TBLS) {
+      fprintf(stderr, "%s: too many tables in file %s\n", progname, filename);
+      exit(EXIT_FAILURE);
+    }
+    table[0] = (unsigned int) val;
+    for (i = 1; i < DCTSIZE2; i++) {
+      if ((val = read_qt_integer(fp)) == EOF) {
+	fprintf(stderr, "%s: incomplete table in file %s\n", progname, filename);
+	exit(EXIT_FAILURE);
+      }
+      table[ZIG[i]] = (unsigned int) val;
+    }
+    jpeg_add_quant_table(cinfo, tblno, table, scale_factor, force_baseline);
+    tblno++;
+  }
+
+  fclose(fp);
+}
+
+
+LOCAL void
+set_quant_slots (j_compress_ptr cinfo, char *arg)
+/* Process a quantization-table-selectors parameter string, of the form
+ *     N[,N,...]
+ * If there are more components than parameters, the last value is replicated.
+ */
+{
+  int val = 0;			/* default table # */
+  int ci;
+  char ch;
+
+  for (ci = 0; ci < MAX_COMPONENTS; ci++) {
+    if (*arg) {
+      ch = ',';			/* if not set by sscanf, will be ',' */
+      if (sscanf(arg, "%d%c", &val, &ch) < 1)
+	usage();
+      if (ch != ',')
+	usage();		/* syntax check */
+      if (val < 0 || val >= NUM_QUANT_TBLS) {
+	fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
+		NUM_QUANT_TBLS-1);
+	exit(EXIT_FAILURE);
+      }
+      cinfo->comp_info[ci].quant_tbl_no = val;
+      while (*arg && *arg++ != ',') /* advance to next segment of arg string */
+	;
+    } else {
+      /* reached end of parameter, set remaining components to last table */
+      cinfo->comp_info[ci].quant_tbl_no = val;
+    }
+  }
+}
+
+
+LOCAL void
+set_sample_factors (j_compress_ptr cinfo, char *arg)
+/* Process a sample-factors parameter string, of the form
+ *     HxV[,HxV,...]
+ * If there are more components than parameters, "1x1" is assumed.
+ */
+{
+  int ci, val1, val2;
+  char ch1, ch2;
+
+  for (ci = 0; ci < MAX_COMPONENTS; ci++) {
+    if (*arg) {
+      ch2 = ',';		/* if not set by sscanf, will be ',' */
+      if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
+	usage();
+      if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',')
+	usage();		/* syntax check */
+      if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
+	fprintf(stderr, "JPEG sampling factors must be 1..4\n");
+	exit(EXIT_FAILURE);
+      }
+      cinfo->comp_info[ci].h_samp_factor = val1;
+      cinfo->comp_info[ci].v_samp_factor = val2;
+      while (*arg && *arg++ != ',') /* advance to next segment of arg string */
+	;
+    } else {
+      /* reached end of parameter, set remaining components to 1x1 sampling */
+      cinfo->comp_info[ci].h_samp_factor = 1;
+      cinfo->comp_info[ci].v_samp_factor = 1;
+    }
+  }
+}
+
+
+LOCAL int
+parse_switches (j_compress_ptr cinfo, int argc, char **argv,
+		int last_file_arg_seen, boolean for_real)
+/* Parse optional switches.
+ * Returns argv[] index of first file-name argument (== argc if none).
+ * Any file names with indexes <= last_file_arg_seen are ignored;
+ * they have presumably been processed in a previous iteration.
+ * (Pass 0 for last_file_arg_seen on the first or only iteration.)
+ * for_real is FALSE on the first (dummy) pass; we may skip any expensive
+ * processing.
+ */
+{
+  int argn;
+  char * arg;
+  int quality;			/* -quality parameter */
+  int q_scale_factor;		/* scaling percentage for -qtables */
+  boolean force_baseline;
+  char * qtablefile = NULL;	/* saves -qtables filename if any */
+  char * qslotsarg = NULL;	/* saves -qslots parm if any */
+  char * samplearg = NULL;	/* saves -sample parm if any */
+
+  /* Set up default JPEG parameters. */
+  /* Note that default -quality level need not, and does not,
+   * match the default scaling for an explicit -qtables argument.
+   */
+  quality = 75;			/* default -quality value */
+  q_scale_factor = 100;		/* default to no scaling for -qtables */
+  force_baseline = FALSE;	/* by default, allow 16-bit quantizers */
+  is_targa = FALSE;
+  outfilename = NULL;
+  cinfo->err->trace_level = 0;
+
+  /* Scan command line options, adjust parameters */
+
+  for (argn = 1; argn < argc; argn++) {
+    arg = argv[argn];
+    if (*arg != '-') {
+      /* Not a switch, must be a file name argument */
+      if (argn <= last_file_arg_seen) {
+	outfilename = NULL;	/* -outfile applies to just one input file */
+	continue;		/* ignore this name if previously processed */
+      }
+      break;			/* else done parsing switches */
+    }
+    arg++;			/* advance past switch marker character */
+
+    if (keymatch(arg, "arithmetic", 1)) {
+      /* Use arithmetic coding. */
+#ifdef C_ARITH_CODING_SUPPORTED
+      cinfo->arith_code = TRUE;
+#else
+      fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
+	      progname);
+      exit(EXIT_FAILURE);
+#endif
+
+    } else if (keymatch(arg, "baseline", 1)) {
+      /* Force baseline output (8-bit quantizer values). */
+      force_baseline = TRUE;
+
+    } else if (keymatch(arg, "dct", 2)) {
+      /* Select DCT algorithm. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (keymatch(argv[argn], "int", 1)) {
+	cinfo->dct_method = JDCT_ISLOW;
+      } else if (keymatch(argv[argn], "fast", 2)) {
+	cinfo->dct_method = JDCT_IFAST;
+      } else if (keymatch(argv[argn], "float", 2)) {
+	cinfo->dct_method = JDCT_FLOAT;
+      } else
+	usage();
+
+    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
+      /* Enable debug printouts. */
+      /* On first -d, print version identification */
+      static boolean printed_version = FALSE;
+
+      if (! printed_version) {
+	fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n",
+		JVERSION, JCOPYRIGHT);
+	printed_version = TRUE;
+      }
+      cinfo->err->trace_level++;
+
+    } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
+      /* Force a monochrome JPEG file to be generated. */
+      jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
+
+    } else if (keymatch(arg, "maxmemory", 3)) {
+      /* Maximum memory in Kb (or Mb with 'm'). */
+      long lval;
+      char ch = 'x';
+
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
+	usage();
+      if (ch == 'm' || ch == 'M')
+	lval *= 1000L;
+      cinfo->mem->max_memory_to_use = lval * 1000L;
+
+    } else if (keymatch(arg, "nointerleave", 3)) {
+      /* Create noninterleaved file. */
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+      cinfo->interleave = FALSE;
+#else
+      fprintf(stderr, "%s: sorry, multiple-scan support was not compiled\n",
+	      progname);
+      exit(EXIT_FAILURE);
+#endif
+
+    } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
+      /* Enable entropy parm optimization. */
+#ifdef ENTROPY_OPT_SUPPORTED
+      cinfo->optimize_coding = TRUE;
+#else
+      fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
+	      progname);
+      exit(EXIT_FAILURE);
+#endif
+
+    } else if (keymatch(arg, "outfile", 4)) {
+      /* Set output file name. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      outfilename = argv[argn];	/* save it away for later use */
+
+    } else if (keymatch(arg, "quality", 1)) {
+      /* Quality factor (quantization table scaling factor). */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (sscanf(argv[argn], "%d", &quality) != 1)
+	usage();
+      /* Change scale factor in case -qtables is present. */
+      q_scale_factor = jpeg_quality_scaling(quality);
+
+    } else if (keymatch(arg, "qslots", 2)) {
+      /* Quantization table slot numbers. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      qslotsarg = argv[argn];
+      /* Must delay setting qslots until after we have processed any
+       * colorspace-determining switches, since jpeg_set_colorspace sets
+       * default quant table numbers.
+       */
+
+    } else if (keymatch(arg, "qtables", 2)) {
+      /* Quantization tables fetched from file. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      qtablefile = argv[argn];
+      /* We postpone actually reading the file in case -quality comes later. */
+
+    } else if (keymatch(arg, "restart", 1)) {
+      /* Restart interval in MCU rows (or in MCUs with 'b'). */
+      long lval;
+      char ch = 'x';
+
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
+	usage();
+      if (lval < 0 || lval > 65535L)
+	usage();
+      if (ch == 'b' || ch == 'B') {
+	cinfo->restart_interval = (unsigned int) lval;
+	cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
+      } else {
+	cinfo->restart_in_rows = (int) lval;
+	/* restart_interval will be computed during startup */
+      }
+
+    } else if (keymatch(arg, "sample", 2)) {
+      /* Set sampling factors. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      samplearg = argv[argn];
+      /* Must delay setting sample factors until after we have processed any
+       * colorspace-determining switches, since jpeg_set_colorspace sets
+       * default sampling factors.
+       */
+
+    } else if (keymatch(arg, "smooth", 2)) {
+      /* Set input smoothing factor. */
+      int val;
+
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (sscanf(argv[argn], "%d", &val) != 1)
+	usage();
+      if (val < 0 || val > 100)
+	usage();
+      cinfo->smoothing_factor = val;
+
+    } else if (keymatch(arg, "targa", 1)) {
+      /* Input file is Targa format. */
+      is_targa = TRUE;
+
+    } else {
+      usage();			/* bogus switch */
+    }
+  }
+
+  /* Post-switch-scanning cleanup */
+
+  if (for_real) {
+
+    /* Set quantization tables for selected quality. */
+    /* Some or all may be overridden if -qtables is present. */
+    jpeg_set_quality(cinfo, quality, force_baseline);
+
+    if (qtablefile != NULL)	/* process -qtables if it was present */
+      read_quant_tables(cinfo, qtablefile, q_scale_factor, force_baseline);
+
+    if (qslotsarg != NULL)	/* process -qslots if it was present */
+      set_quant_slots(cinfo, qslotsarg);
+
+    if (samplearg != NULL)	/* process -sample if it was present */
+      set_sample_factors(cinfo, samplearg);
+
+  }
+
+  return argn;			/* return index of next arg (file name) */
+}
+
+
+/*
+ * The main program.
+ */
+
+GLOBAL int
+main (int argc, char **argv)
+{
+  struct jpeg_compress_struct cinfo;
+  struct jpeg_error_mgr jerr;
+#ifdef PROGRESS_REPORT
+  struct cdjpeg_progress_mgr progress;
+#endif
+  int file_index;
+  cjpeg_source_ptr src_mgr;
+  FILE * input_file;
+  FILE * output_file;
+  JDIMENSION num_scanlines;
+
+  /* On Mac, fetch a command line. */
+#ifdef USE_CCOMMAND
+  argc = ccommand(&argv);
+#endif
+
+  progname = argv[0];
+  if (progname == NULL || progname[0] == 0)
+    progname = "cjpeg";		/* in case C library doesn't provide it */
+
+  /* Initialize the JPEG compression object with default error handling. */
+  cinfo.err = jpeg_std_error(&jerr);
+  jpeg_create_compress(&cinfo);
+  /* Add some application-specific error messages (from cderror.h) */
+  jerr.addon_message_table = addon_message_table;
+  jerr.first_addon_message = JMSG_FIRSTADDONCODE;
+  jerr.last_addon_message = JMSG_LASTADDONCODE;
+
+  /* Now safe to enable signal catcher. */
+#ifdef NEED_SIGNAL_CATCHER
+  sig_cinfo = (j_common_ptr) &cinfo;
+  signal(SIGINT, signal_catcher);
+#ifdef SIGTERM			/* not all systems have SIGTERM */
+  signal(SIGTERM, signal_catcher);
+#endif
+#endif
+
+  /* Initialize JPEG parameters.
+   * Much of this may be overridden later.
+   * In particular, we don't yet know the input file's color space,
+   * but we need to provide some value for jpeg_set_defaults() to work.
+   */
+
+  cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
+  jpeg_set_defaults(&cinfo);
+
+  /* Scan command line to find file names.
+   * It is convenient to use just one switch-parsing routine, but the switch
+   * values read here are ignored; we will rescan the switches after opening
+   * the input file.
+   */
+
+  file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
+
+#ifdef TWO_FILE_COMMANDLINE
+  /* Must have either -outfile switch or explicit output file name */
+  if (outfilename == NULL) {
+    if (file_index != argc-2) {
+      fprintf(stderr, "%s: must name one input and one output file\n",
+	      progname);
+      usage();
+    }
+    outfilename = argv[file_index+1];
+  } else {
+    if (file_index != argc-1) {
+      fprintf(stderr, "%s: must name one input and one output file\n",
+	      progname);
+      usage();
+    }
+  }
+#else
+  /* Unix style: expect zero or one file name */
+  if (file_index < argc-1) {
+    fprintf(stderr, "%s: only one input file\n", progname);
+    usage();
+  }
+#endif /* TWO_FILE_COMMANDLINE */
+
+  /* Open the input file. */
+  if (file_index < argc) {
+    if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
+      exit(EXIT_FAILURE);
+    }
+  } else {
+    /* default input file is stdin */
+#ifdef USE_SETMODE		/* need to hack file mode? */
+    setmode(fileno(stdin), O_BINARY);
+#endif
+#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
+    if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open stdin\n", progname);
+      exit(EXIT_FAILURE);
+    }
+#else
+    input_file = stdin;
+#endif
+  }
+
+  /* Open the output file. */
+  if (outfilename != NULL) {
+    if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
+      exit(EXIT_FAILURE);
+    }
+  } else {
+    /* default output file is stdout */
+#ifdef USE_SETMODE		/* need to hack file mode? */
+    setmode(fileno(stdout), O_BINARY);
+#endif
+#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
+    if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open stdout\n", progname);
+      exit(EXIT_FAILURE);
+    }
+#else
+    output_file = stdout;
+#endif
+  }
+
+#ifdef PROGRESS_REPORT
+  /* Enable progress display, unless trace output is on */
+  if (jerr.trace_level == 0) {
+    progress.pub.progress_monitor = progress_monitor;
+    progress.completed_extra_passes = 0;
+    progress.total_extra_passes = 0;
+    progress.percent_done = -1;
+    cinfo.progress = &progress.pub;
+  }
+#endif
+
+  /* Figure out the input file format, and set up to read it. */
+  src_mgr = select_file_type(&cinfo, input_file);
+  src_mgr->input_file = input_file;
+
+  /* Read the input file header to obtain file size & colorspace. */
+  (*src_mgr->start_input) (&cinfo, src_mgr);
+
+  /* Now that we know input colorspace, fix colorspace-dependent defaults */
+  jpeg_default_colorspace(&cinfo);
+
+  /* Adjust default compression parameters by re-parsing the options */
+  file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
+
+  /* Specify data destination for compression */
+  jpeg_stdio_dest(&cinfo, output_file);
+
+  /* Start compressor */
+  jpeg_start_compress(&cinfo, TRUE);
+
+  /* Process data */
+  while (cinfo.next_scanline < cinfo.image_height) {
+    num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
+    (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
+  }
+
+  /* Finish compression and release memory */
+  (*src_mgr->finish_input) (&cinfo, src_mgr);
+  jpeg_finish_compress(&cinfo);
+  jpeg_destroy_compress(&cinfo);
+
+#ifdef PROGRESS_REPORT
+  /* Clear away progress display */
+  if (jerr.trace_level == 0) {
+    fprintf(stderr, "\r                \r");
+    fflush(stderr);
+  }
+#endif
+
+  /* All done. */
+  exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
+  return 0;			/* suppress no-return-value warnings */
+}
diff --git a/ckconfig.c b/ckconfig.c
index 687d2c6..34baf79 100644
--- a/ckconfig.c
+++ b/ckconfig.c
@@ -1,7 +1,7 @@
 /*
  * ckconfig.c
  *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  */
@@ -11,12 +11,8 @@
  * software for installation on a particular system.  The idea is to try to
  * compile and execute this program.  If your compiler fails to compile the
  * program, make changes as indicated in the comments below.  Once you can
- * compile the program, run it, and it will tell you how to set the various
- * switches in jconfig.h and in your Makefile.
- *
- * This could all be done automatically if we could assume we were on a Unix
- * system, but we don't want to assume that, so you'll have to edit and
- * recompile this program until it works.
+ * compile the program, run it, and it will produce a "jconfig.h" file for
+ * your system.
  *
  * As a general rule, each time you try to compile this program,
  * pay attention only to the *first* error message you get from the compiler.
@@ -31,66 +27,58 @@
 
 
 /* First we must see if your system has the include files we need.
- * We start out with the assumption that your system follows the ANSI
- * conventions for include files.  If you get any error in the next dozen
- * lines, undefine INCLUDES_ARE_ANSI.
+ * We start out with the assumption that your system has all the ANSI-standard
+ * include files.  If you get any error trying to include one of these files,
+ * undefine the corresponding HAVE_xxx symbol.
  */
 
-#define INCLUDES_ARE_ANSI	/* replace 'define' by 'undef' if error here */
-
-#ifdef INCLUDES_ARE_ANSI	/* this will be skipped if you undef... */
-#include <stdio.h>		/* If you ain't got this, you ain't got C. */
-#ifdef __SASC			/* Amiga SAS C provides size_t in stddef.h. */
-#include <stddef.h>		/* (They are wrong...) */
-#endif
-#include <string.h>		/* size_t might be here too. */
-typedef size_t my_size_t;	/* The payoff: do we have size_t now? */
-#include <stdlib.h>		/* Check other ANSI includes we use. */
+#define HAVE_STDDEF_H		/* replace 'define' by 'undef' if error here */
+#ifdef HAVE_STDDEF_H		/* next line will be skipped if you undef... */
+#include <stddef.h>
 #endif
 
-
-/* If your system doesn't follow the ANSI conventions, we have to figure out
- * what it does follow.  If you didn't get an error before this line, you can
- * ignore everything down to "#define HAVE_ANSI_DEFINITIONS".
- */
-
-#ifndef INCLUDES_ARE_ANSI	/* skip these tests if INCLUDES_ARE_ANSI */
+#define HAVE_STDLIB_H		/* same thing for stdlib.h */
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
 
 #include <stdio.h>		/* If you ain't got this, you ain't got C. */
 
-/* jinclude.h will try to include <sys/types.h> if you don't set
- * INCLUDES_ARE_ANSI.  We need to test whether that include file is provided.
- * If you get an error here, undefine HAVE_TYPES_H.
- */
-
-#define HAVE_TYPES_H
-
-#ifdef HAVE_TYPES_H
-#include <sys/types.h>
-#endif
-
 /* We have to see if your string functions are defined by
- * strings.h (BSD convention) or string.h (everybody else).
- * We try the non-BSD convention first; define BSD if the compiler
- * says it can't find string.h.
+ * strings.h (old BSD convention) or string.h (everybody else).
+ * We try the non-BSD convention first; define NEED_BSD_STRINGS
+ * if the compiler says it can't find string.h.
  */
 
-#undef BSD
+#undef NEED_BSD_STRINGS
 
-#ifdef BSD
+#ifdef NEED_BSD_STRINGS
 #include <strings.h>
 #else
 #include <string.h>
 #endif
 
-/* Usually size_t is defined in stdio.h, sys/types.h, and/or string.h.
- * If not, you'll get an error on the "typedef size_t my_size_t;" line below.
- * In that case, you'll have to search through your system library to
- * figure out which include file defines "size_t".  Look for a line that
- * says "typedef something-or-other size_t;" (stddef.h and stdlib.h are
- * good places to look first).  Then, change the line below that says
- * "#include <someincludefile.h>" to instead include the file
- * you found size_t in, and define NEED_SPECIAL_INCLUDE.
+/* On some systems (especially older Unix machines), type size_t is
+ * defined only in the include file <sys/types.h>.  If you get a failure
+ * on the size_t test below, try defining NEED_SYS_TYPES_H.
+ */
+
+#undef NEED_SYS_TYPES_H		/* start by assuming we don't need it */
+#ifdef NEED_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+
+/* Usually type size_t is defined in one of the include files we've included
+ * above.  If not, you'll get an error on the "typedef size_t my_size_t;" line.
+ * In that case, first try defining NEED_SYS_TYPES_H just above.
+ * If that doesn't work, you'll have to search through your system library
+ * to figure out which include file defines "size_t".  Look for a line that
+ * says "typedef something-or-other size_t;".  Then, change the line below
+ * that says "#include <someincludefile.h>" to instead include the file
+ * you found size_t in, and define NEED_SPECIAL_INCLUDE.  If you can't find
+ * type size_t anywhere, try replacing "#include <someincludefile.h>" with
+ * "typedef unsigned int size_t;".
  */
 
 #undef NEED_SPECIAL_INCLUDE	/* assume we DON'T need it, for starters */
@@ -102,20 +90,16 @@
 typedef size_t my_size_t;	/* The payoff: do we have size_t now? */
 
 
-#endif /* INCLUDES_ARE_ANSI */
-
-
-
 /* The next question is whether your compiler supports ANSI-style function
- * definitions.  You need to know this in order to choose between using
+ * prototypes.  You need to know this in order to choose between using
  * makefile.ansi and using makefile.unix.
- * The #define line below is set to assume you have ANSI function definitions.
- * If you get an error in this group of lines, undefine HAVE_ANSI_DEFINITIONS.
+ * The #define line below is set to assume you have ANSI function prototypes.
+ * If you get an error in this group of lines, undefine HAVE_PROTOTYPES.
  */
 
-#define HAVE_ANSI_DEFINITIONS
+#define HAVE_PROTOTYPES
 
-#ifdef HAVE_ANSI_DEFINITIONS
+#ifdef HAVE_PROTOTYPES
 int testfunction (int arg1, int * arg2); /* check prototypes */
 
 struct methods_struct {		/* check method-pointer declarations */
@@ -129,7 +113,7 @@
   return arg2[arg1];
 }
 
-int testfunction1 (void)	/* check void arg list */
+int test2function (void)	/* check void arg list */
 {
   return 0;
 }
@@ -167,12 +151,21 @@
 #define HAVE_VOID
 
 #ifdef HAVE_VOID
+/* Caution: a C++ compiler will insist on complete prototypes */
 typedef void * void_ptr;	/* check void * */
-typedef void (*void_func) ();	/* check ptr to function returning void */
+#ifdef HAVE_PROTOTYPES		/* check ptr to function returning void */
+typedef void (*void_func) (int a, int b);
+#else
+typedef void (*void_func) ();
+#endif
 
-void testfunction2 (arg1, arg2)	/* check void function result */
+#ifdef HAVE_PROTOTYPES		/* check void function result */
+void test3function (void_ptr arg1, void_func arg2)
+#else
+void test3function (arg1, arg2)
      void_ptr arg1;
      void_func arg2;
+#endif
 {
   char * locptr = (char *) arg1; /* check casting to and from void * */
   arg1 = (void *) locptr;
@@ -190,215 +183,220 @@
 #ifdef HAVE_CONST
 static const int carray[3] = {1, 2, 3};
 
-int testfunction3 (arg1)
+#ifdef HAVE_PROTOTYPES
+int test4function (const int arg1)
+#else
+int test4function (arg1)
      const int arg1;
+#endif
 {
   return carray[arg1];
 }
 #endif
 
 
+/* If you get an error or warning about this structure definition,
+ * define INCOMPLETE_TYPES_BROKEN.
+ */
+
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifndef INCOMPLETE_TYPES_BROKEN
+typedef struct undefined_structure * undef_struct_ptr;
+#endif
+
+
+/* If you get an error about duplicate names,
+ * define NEED_SHORT_EXTERNAL_NAMES.
+ */
+
+#undef NEED_SHORT_EXTERNAL_NAMES
+
+#ifndef NEED_SHORT_EXTERNAL_NAMES
+
+int possibly_duplicate_function ()
+{
+  return 0;
+}
+
+int possibly_dupli_function ()
+{
+  return 1;
+}
+
+#endif
+
+
 
 /************************************************************************
  *  OK, that's it.  You should not have to change anything beyond this
  *  point in order to compile and execute this program.  (You might get
  *  some warnings, but you can ignore them.)
  *  When you run the program, it will make a couple more tests that it
- *  can do automatically, and then it will print out a summary of the changes
- *  that you need to make to the makefile and jconfig.h.
+ *  can do automatically, and then it will create jconfig.h and print out
+ *  any additional suggestions it has.
  ************************************************************************
  */
 
 
-static int any_changes = 0;
-
-int new_change ()
-{
-  if (! any_changes) {
-    printf("\nMost of the changes recommended by this program can be made either\n");
-    printf("by editing jconfig.h, or by adding -Dsymbol switches to the CFLAGS\n");
-    printf("line in your Makefile.  (Some PC compilers expect /Dsymbol instead.)\n");
-    printf("The CFLAGS method is simpler, but if your compiler doesn't support -D,\n");
-    printf("then you must change jconfig.h.  Also, it's best to change jconfig.h\n");
-    printf("if you plan to use the JPEG software as a library for other programs.\n");
-    any_changes = 1;
-  }
-  printf("\n");			/* blank line before each problem report */
-  return 0;
-}
-
-
-int test_char_sign (arg)
+#ifdef HAVE_PROTOTYPES
+int is_char_signed (int arg)
+#else
+int is_char_signed (arg)
      int arg;
+#endif
 {
   if (arg == 189) {		/* expected result for unsigned char */
-    new_change();
-    printf("You should add -DCHAR_IS_UNSIGNED to CFLAGS,\n");
-    printf("or else remove the /* */ comment marks from the line\n");
-    printf("/* #define CHAR_IS_UNSIGNED */  in jconfig.h.\n");
-    printf("(Be sure to delete the space before the # character too.)\n");
+    return 0;			/* type char is unsigned */
   }
   else if (arg != -67) {	/* expected result for signed char */
-    new_change();
-    printf("Hmm, it seems 'char' is less than eight bits wide on your machine.\n");
-    printf("I fear the JPEG software will not work at all.\n");
+    printf("Hmm, it seems 'char' is not eight bits wide on your machine.\n");
+    printf("I fear the JPEG software will not work at all.\n\n");
   }
-  return 0;
+  return 1;			/* assume char is signed otherwise */
 }
 
 
-int test_shifting (arg)
+#ifdef HAVE_PROTOTYPES
+int is_shifting_signed (long arg)
+#else
+int is_shifting_signed (arg)
      long arg;
+#endif
 /* See whether right-shift on a long is signed or not. */
 {
   long res = arg >> 4;
 
-  if (res == 0x80817F4L) {	/* expected result for unsigned */
-    new_change();
-    printf("You must add -DRIGHT_SHIFT_IS_UNSIGNED to CFLAGS,\n");
-    printf("or else remove the /* */ comment marks from the line\n");
-    printf("/* #define RIGHT_SHIFT_IS_UNSIGNED */  in jconfig.h.\n");
+  if (res == -0x7F7E80CL) {	/* expected result for signed shift */
+    return 1;			/* right shift is signed */
   }
-  else if (res != -0x7F7E80CL) { /* expected result for signed */
-    new_change();
-    printf("Right shift isn't acting as I expect it to.\n");
-    printf("I fear the JPEG software will not work at all.\n");
+  /* see if unsigned-shift hack will fix it. */
+  /* we can't just test exact value since it depends on width of long... */
+  res |= (~0L) << (32-4);
+  if (res == -0x7F7E80CL) {	/* expected result now? */
+    return 0;			/* right shift is unsigned */
   }
-  return 0;
+  printf("Right shift isn't acting as I expect it to.\n");
+  printf("I fear the JPEG software will not work at all.\n\n");
+  return 0;			/* try it with unsigned anyway */
 }
 
 
+#ifdef HAVE_PROTOTYPES
+int main (int argc, char ** argv)
+#else
 int main (argc, argv)
      int argc;
      char ** argv;
+#endif
 {
   char signed_char_check = (char) (-67);
+  FILE *outfile;
 
-  printf("Results of configuration check for Independent JPEG Group's software:\n");
-  printf("\nIf there's not a specific makefile provided for your compiler,\n");
-#ifdef HAVE_ANSI_DEFINITIONS
-  printf("you should use makefile.ansi as the starting point for your Makefile.\n");
-#else
-  printf("you should use makefile.unix as the starting point for your Makefile.\n");
-#endif
-
-  /* Check whether we have all the ANSI features, */
-  /* and whether this agrees with __STDC__ being predefined. */
-#ifdef __STDC__
-#define HAVE_STDC	/* ANSI compilers won't allow redefining __STDC__ */
-#endif
-
-#ifdef HAVE_ANSI_DEFINITIONS
-#ifdef HAVE_UNSIGNED_CHAR
-#ifdef HAVE_UNSIGNED_SHORT
-#ifdef HAVE_CONST
-#define HAVE_ALL_ANSI_FEATURES
-#endif
-#endif
-#endif
-#endif
-
-#ifdef HAVE_ALL_ANSI_FEATURES
-#ifndef HAVE_STDC
-  new_change();
-  printf("Your compiler doesn't claim to be ANSI-compliant, but it is close enough\n");
-  printf("for me.  Either add -DHAVE_STDC to CFLAGS, or add #define HAVE_STDC at the\n");
-  printf("beginning of jconfig.h.\n");
-#define HAVE_STDC
-#endif
-#else /* !HAVE_ALL_ANSI_FEATURES */
-#ifdef HAVE_STDC
-  new_change();
-  printf("Your compiler claims to be ANSI-compliant, but it is lying!\n");
-  printf("Delete the line  #define HAVE_STDC  near the beginning of jconfig.h.\n");
-#undef HAVE_STDC
-#endif
-#endif /* HAVE_ALL_ANSI_FEATURES */
-
-#ifndef HAVE_STDC
-
-#ifdef HAVE_ANSI_DEFINITIONS
-  new_change();
-  printf("You should add -DPROTO to CFLAGS, or else take out the several\n");
-  printf("#ifdef/#else/#endif lines surrounding #define PROTO in jconfig.h.\n");
-  printf("(Leave only one #define PROTO line.)\n");
-#endif
-
-#ifdef HAVE_UNSIGNED_CHAR
-#ifdef HAVE_UNSIGNED_SHORT
-  new_change();
-  printf("You should add -DHAVE_UNSIGNED_CHAR and -DHAVE_UNSIGNED_SHORT\n");
-  printf("to CFLAGS, or else take out the #ifdef HAVE_STDC/#endif lines\n");
-  printf("surrounding #define HAVE_UNSIGNED_CHAR and #define HAVE_UNSIGNED_SHORT\n");
-  printf("in jconfig.h.\n");
-#else /* only unsigned char */
-  new_change();
-  printf("You should add -DHAVE_UNSIGNED_CHAR to CFLAGS,\n");
-  printf("or else move #define HAVE_UNSIGNED_CHAR outside the\n");
-  printf("#ifdef HAVE_STDC/#endif lines surrounding it in jconfig.h.\n");
-#endif
-#else /* !HAVE_UNSIGNED_CHAR */
-#ifdef HAVE_UNSIGNED_SHORT
-  new_change();
-  printf("You should add -DHAVE_UNSIGNED_SHORT to CFLAGS,\n");
-  printf("or else move #define HAVE_UNSIGNED_SHORT outside the\n");
-  printf("#ifdef HAVE_STDC/#endif lines surrounding it in jconfig.h.\n");
-#endif
-#endif /* HAVE_UNSIGNED_CHAR */
-
-#ifdef HAVE_CONST
-  new_change();
-  printf("You should delete the  #define const  line from jconfig.h.\n");
-#endif
-
-#endif /* HAVE_STDC */
-
-  test_char_sign((int) signed_char_check);
-
-  test_shifting(-0x7F7E80B1L);
-
-#ifndef HAVE_VOID
-  new_change();
-  printf("You should add -Dvoid=char to CFLAGS,\n");
-  printf("or else remove the /* */ comment marks from the line\n");
-  printf("/* #define void char */  in jconfig.h.\n");
-  printf("(Be sure to delete the space before the # character too.)\n");
-#endif
-
-#ifdef INCLUDES_ARE_ANSI
-#ifndef __STDC__
-  new_change();
-  printf("You should add -DINCLUDES_ARE_ANSI to CFLAGS, or else add\n");
-  printf("#define INCLUDES_ARE_ANSI at the beginning of jinclude.h (NOT jconfig.h).\n");
-#endif
-#else /* !INCLUDES_ARE_ANSI */
-#ifdef __STDC__
-  new_change();
-  printf("You should add -DNONANSI_INCLUDES to CFLAGS, or else add\n");
-  printf("#define NONANSI_INCLUDES at the beginning of jinclude.h (NOT jconfig.h).\n");
-#endif
-#ifdef NEED_SPECIAL_INCLUDE
-  new_change();
-  printf("In jinclude.h, change the line reading #include <sys/types.h>\n");
-  printf("to instead include the file you found size_t in.\n");
-#else /* !NEED_SPECIAL_INCLUDE */
-#ifndef HAVE_TYPES_H
-  new_change();
-  printf("In jinclude.h, delete the line reading #include <sys/types.h>.\n");
-#endif
-#endif /* NEED_SPECIAL_INCLUDE */
-#ifdef BSD
-  new_change();
-  printf("You should add -DBSD to CFLAGS, or else add\n");
-  printf("#define BSD at the beginning of jinclude.h (NOT jconfig.h).\n");
-#endif
-#endif /* INCLUDES_ARE_ANSI */
-
-  if (any_changes) {
-    printf("\nI think that's everything...\n");
-  } else {
-    printf("\nI think jconfig.h is OK as distributed.\n");
+  /* Attempt to write jconfig.h */
+  if ((outfile = fopen("jconfig.h", "w")) == NULL) {
+    printf("Failed to write jconfig.h\n");
+    return 1;
   }
 
-  return any_changes;
+  /* Write out all the info */
+  fprintf(outfile, "/* jconfig.h --- generated by ckconfig.c */\n");
+  fprintf(outfile, "/* see jconfig.doc for explanations */\n\n");
+#ifdef HAVE_PROTOTYPES
+  fprintf(outfile, "#define HAVE_PROTOTYPES\n");
+#else
+  fprintf(outfile, "#undef HAVE_PROTOTYPES\n");
+#endif
+#ifdef HAVE_UNSIGNED_CHAR
+  fprintf(outfile, "#define HAVE_UNSIGNED_CHAR\n");
+#else
+  fprintf(outfile, "#undef HAVE_UNSIGNED_CHAR\n");
+#endif
+#ifdef HAVE_UNSIGNED_SHORT
+  fprintf(outfile, "#define HAVE_UNSIGNED_SHORT\n");
+#else
+  fprintf(outfile, "#undef HAVE_UNSIGNED_SHORT\n");
+#endif
+#ifdef HAVE_VOID
+  fprintf(outfile, "/* #define void char */\n");
+#else
+  fprintf(outfile, "#define void char\n");
+#endif
+#ifdef HAVE_CONST
+  fprintf(outfile, "/* #define const */\n");
+#else
+  fprintf(outfile, "#define const\n");
+#endif
+  if (is_char_signed((int) signed_char_check))
+    fprintf(outfile, "#undef CHAR_IS_UNSIGNED\n");
+  else
+    fprintf(outfile, "#define CHAR_IS_UNSIGNED\n");
+#ifdef HAVE_STDDEF_H
+  fprintf(outfile, "#define HAVE_STDDEF_H\n");
+#else
+  fprintf(outfile, "#undef HAVE_STDDEF_H\n");
+#endif
+#ifdef HAVE_STDLIB_H
+  fprintf(outfile, "#define HAVE_STDLIB_H\n");
+#else
+  fprintf(outfile, "#undef HAVE_STDLIB_H\n");
+#endif
+#ifdef NEED_BSD_STRINGS
+  fprintf(outfile, "#define NEED_BSD_STRINGS\n");
+#else
+  fprintf(outfile, "#undef NEED_BSD_STRINGS\n");
+#endif
+#ifdef NEED_SYS_TYPES_H
+  fprintf(outfile, "#define NEED_SYS_TYPES_H\n");
+#else
+  fprintf(outfile, "#undef NEED_SYS_TYPES_H\n");
+#endif
+  fprintf(outfile, "#undef NEED_FAR_POINTERS\n");
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+  fprintf(outfile, "#define NEED_SHORT_EXTERNAL_NAMES\n");
+#else
+  fprintf(outfile, "#undef NEED_SHORT_EXTERNAL_NAMES\n");
+#endif
+#ifdef INCOMPLETE_TYPES_BROKEN
+  fprintf(outfile, "#define INCOMPLETE_TYPES_BROKEN\n");
+#else
+  fprintf(outfile, "#undef INCOMPLETE_TYPES_BROKEN\n");
+#endif
+  fprintf(outfile, "\n#ifdef JPEG_INTERNALS\n\n");
+  if (is_shifting_signed(-0x7F7E80B1L))
+    fprintf(outfile, "#undef RIGHT_SHIFT_IS_UNSIGNED\n");
+  else
+    fprintf(outfile, "#define RIGHT_SHIFT_IS_UNSIGNED\n");
+  fprintf(outfile, "\n#endif /* JPEG_INTERNALS */\n");
+  fprintf(outfile, "\n#ifdef JPEG_CJPEG_DJPEG\n\n");
+  fprintf(outfile, "#define BMP_SUPPORTED		/* BMP image file format */\n");
+  fprintf(outfile, "#define GIF_SUPPORTED		/* GIF image file format */\n");
+  fprintf(outfile, "#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */\n");
+  fprintf(outfile, "#undef RLE_SUPPORTED		/* Utah RLE image file format */\n");
+  fprintf(outfile, "#define TARGA_SUPPORTED		/* Targa image file format */\n\n");
+  fprintf(outfile, "#undef TWO_FILE_COMMANDLINE	/* You may need this on non-Unix systems */\n");
+  fprintf(outfile, "#undef NEED_SIGNAL_CATCHER	/* Define this if you use jmemname.c */\n");
+  fprintf(outfile, "#undef DONT_USE_B_MODE\n");
+  fprintf(outfile, "/* #define PROGRESS_REPORT */	/* optional */\n");
+  fprintf(outfile, "\n#endif /* JPEG_CJPEG_DJPEG */\n");
+
+  /* Close the jconfig.h file */
+  fclose(outfile);
+
+  /* User report */
+  printf("Configuration check for Independent JPEG Group's software done.\n");
+  printf("\nI have written the jconfig.h file for you.\n\n");
+#ifdef HAVE_PROTOTYPES
+  printf("You should use makefile.ansi as the starting point for your Makefile.\n");
+#else
+  printf("You should use makefile.unix as the starting point for your Makefile.\n");
+#endif
+
+#ifdef NEED_SPECIAL_INCLUDE
+  printf("\nYou'll need to change jconfig.h to include the system include file\n");
+  printf("that you found type size_t in, or add a direct definition of type\n");
+  printf("size_t if that's what you used.  Just add it to the end.\n");
+#endif
+
+  return 0;
 }
diff --git a/coderules.doc b/coderules.doc
new file mode 100644
index 0000000..b28034e
--- /dev/null
+++ b/coderules.doc
@@ -0,0 +1,118 @@
+IJG JPEG LIBRARY:  CODING RULES
+
+Copyright (C) 1991-1994, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+Since numerous people will be contributing code and bug fixes, it's important
+to establish a common coding style.  The goal of using similar coding styles
+is much more important than the details of just what that style is.
+
+In general we follow the recommendations of "Recommended C Style and Coding
+Standards" revision 6.1 (Cannon et al. as modified by Spencer, Keppel and
+Brader).  This document is available in the IJG FTP archive (see
+jpeg/doc/cstyle.ms.tbl.Z, or cstyle.txt.Z for those without nroff/tbl).
+
+Block comments should be laid out thusly:
+
+/*
+ *  Block comments in this style.
+ */
+
+We indent statements in K&R style, e.g.,
+	if (test) {
+	  then-part;
+	} else {
+	  else-part;
+	}
+with two spaces per indentation level.  (This indentation convention is
+handled automatically by GNU Emacs and many other text editors.)
+
+Multi-word names should be written in lower case with underscores, e.g.,
+multi_word_name (not multiWordName).  Preprocessor symbols and enum constants
+are similar but upper case (MULTI_WORD_NAME).  Names should be unique within
+the first fifteen characters.  (On some older systems, global names must be
+unique within six characters.  We accommodate this without cluttering the
+source code by using macros to substitute shorter names.)
+
+We use function prototypes everywhere; we rely on automatic source code
+transformation to feed prototype-less C compilers.  Transformation is done
+by the simple and portable tool 'ansi2knr.c' (courtesy of Ghostscript).
+ansi2knr is not very bright, so it imposes a format requirement on function
+declarations: the function name MUST BEGIN IN COLUMN 1.  Thus all functions
+should be written in the following style:
+
+LOCAL int *
+function_name (int a, char *b)
+{
+    code...
+}
+
+Note that each function definition is prefixed with GLOBAL, LOCAL, or
+METHODDEF.  These macros expand to "static" or nothing as appropriate.
+They provide a readable indication of the routine's usage and can readily be
+changed for special needs.  (For instance, all routines can be made global for
+use with debuggers or code profilers that require it.)
+
+ansi2knr does not transform method declarations (function pointers in
+structs).  We handle these with a macro JMETHOD, defined as
+	#ifdef HAVE_PROTOTYPES
+	#define JMETHOD(type,methodname,arglist)  type (*methodname) arglist
+	#else
+	#define JMETHOD(type,methodname,arglist)  type (*methodname) ()
+	#endif
+which is used like this:
+	struct function_pointers {
+	  JMETHOD(void, init_entropy_encoder, (int somearg, jparms *jp));
+	  JMETHOD(void, term_entropy_encoder, (void));
+	};
+Note the set of parentheses surrounding the parameter list.
+
+A similar solution is used for external function declarations (see the JPP
+macro).
+
+If the code is to work on non-ANSI compilers, we cannot rely on a prototype
+declaration to coerce actual parameters into the right types.  Therefore, use
+explicit casts on actual parameters whenever the actual parameter type is not
+identical to the formal parameter.  Beware of implicit conversions to "int".
+
+It seems there are some non-ANSI compilers in which the sizeof() operator
+is defined to return int, yet size_t is defined as long.  Needless to say,
+this is brain-damaged.  Always use the SIZEOF() macro in place of sizeof(),
+so that the result is guaranteed to be of type size_t.
+
+
+The JPEG library is intended to be used within larger programs.  Furthermore,
+we want it to be reentrant so that it can be used by applications that process
+multiple images concurrently.  The following rules support these requirements:
+
+1. Avoid direct use of file I/O, "malloc", error report printouts, etc;
+pass these through the common routines provided.
+
+2. Minimize global namespace pollution.  Functions should be declared static
+wherever possible.  (Note that our method-based calling conventions help this
+a lot: in many modules only the initialization function will ever need to be
+called directly, so only that function need be externally visible.)  All
+global function names should begin with "jpeg_", and should have an
+abbreviated name (unique in the first six characters) substituted by macro
+when NEED_SHORT_EXTERNAL_NAMES is set.
+
+3. Don't use global variables; anything that must be used in another module
+should be in the common data structures.
+
+4. Don't use static variables except for read-only constant tables.  Variables
+that should be private to a module can be placed into private structures (see
+the system architecture document, structure.doc).
+
+5. Source file names should begin with "j" for files that are part of the
+library proper; source files that are not part of the library, such as cjpeg.c
+and djpeg.c, do not begin with "j".  Keep source file names to eight
+characters (plus ".c" or ".h", etc) to make life easy for MS-DOSers.  Keep
+compression and decompression code in separate source files --- some
+applications may want only one half of the library.
+
+Note: these rules (particularly #4) are not followed religiously in the
+modules that are used in cjpeg/djpeg but are not part of the JPEG library
+proper.  Those modules are not really intended to be used in other
+applications.
diff --git a/codingrules b/codingrules
deleted file mode 100644
index fd6f700..0000000
--- a/codingrules
+++ /dev/null
@@ -1,99 +0,0 @@
-
-	JPEG SYSTEM CODING RULES		27-SEP-91
-
-Since numerous people will be contributing code and bug fixes, it's important
-to establish a common coding style.  The goal of using similar coding styles
-is much more important than the details of just what that style is.
-
-I suggest we follow the recommendations of "Recommended C Style and Coding
-Standards" revision 6.1 (Cannon et al. as modified by Spencer, Keppel and
-Brader).  I have placed a copy of this document in the jpeg FTP archive (see
-jpeg/doc/cstyle.ms.tbl.Z, or cstyle.txt.Z for those without nroff/tbl).
-
-Unless someone has a real strong objection, let's do block comments thusly:
-
-/*
- *  Block comments in this style.
- */
-
-and indent statements in K&R style, e.g.,
-
-	if (test) {
-	    then-part;
-	} else {
-	    else-part;
-	}
-
-I suggest that multi-word names be written in the style multi_word_name
-rather than multiWordName, but I am open to argument on this.
-
-
-I would like to use function prototypes everywhere, and rely on automatic
-source code transformation to feed non-ANSI C compilers.  The best tool
-I have so far found for this is 'ansi2knr.c', which is part of Ghostscript.
-ansi2knr is not very bright, so it imposes a format requirement on function
-declarations: the function name MUST BEGIN IN COLUMN 1.  Thus all functions
-should be written in the following style:
-
-static int *
-function_name (int a, char *b)
-{
-    code...
-}
-
-ansi2knr won't help with method declarations (function pointers in structs).
-I suggest we use a macro to declare method pointers, something like this:
-
-#ifdef PROTO
-#define METHOD(type,methodname,arglist)  type (*methodname) arglist
-#else
-#define METHOD(type,methodname,arglist)  type (*methodname) ()
-#endif
-
-which is used like this:
-
-struct function_pointers {
-	METHOD(void, init_entropy_encoder, (functptrs fptrs, jparms *jp));
-	METHOD(void, term_entropy_encoder, (void));
-};
-
-Note the set of parentheses surrounding the parameter list.
-
-A similar solution is used for external function declarations (see the PP
-macro in jpegdata.h).
-
-If the code is to work on non-ANSI compilers, you cannot rely on a prototype
-declaration to coerce actual parameters into the right types.  Therefore, use
-explicit casts on actual parameters whenever the actual parameter type is not
-identical to the formal parameter.  Beware of implicit conversions to "int".
-
-It seems there are some non-ANSI compilers in which the sizeof() operator
-is defined to return int, while size_t is defined as long.  Needless to say,
-this is brain-damaged.  Always use the SIZEOF() macro in place of sizeof(),
-so that the result is guaranteed to be of type size_t.
-
-
-We can expect that the JPEG compressor and decompressor will be incorporated
-into larger programs.  Therefore, the following rules are important:
-
-1. Avoid direct use of any file I/O, "malloc", error report printouts, etc;
-pass these through the common routines provided.
-
-2. Assume that the JPEG code may be invoked more than once per program run;
-therefore, do not rely on static initialization of variables, and be careful
-to release all allocated storage at the end of processing.
-
-3. Minimize global namespace pollution.  Functions should be declared static
-wherever possible.  (Note that our method-based calling conventions help this
-a lot: in many modules only the method-selector function will ever need to be
-called directly, so only that function need be externally visible.)  All
-global function names should begin with "j", and should be unique in the first
-six characters for portability reasons.
-Don't use global variables at all; anything that must be used in another
-module should be put into parameters (there'll be some large structs passed
-around for this purpose).
-
-4. Source file names should also begin with "j"; remember to keep them to
-eight characters (plus ".c" or ".h", etc) to make life easy for MS-DOSers.
-Do not put code for both compression and decompression into the same source
-file.
diff --git a/configure b/configure
new file mode 100755
index 0000000..0d62d68
--- /dev/null
+++ b/configure
@@ -0,0 +1,1168 @@
+#!/bin/sh
+# Guess values for system-dependent variables and create Makefiles.
+# Generated automatically using autoconf.
+# Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+# Usage: configure [--srcdir=DIR] [--host=HOST] [--gas] [--nfp]
+#        [--prefix=PREFIX] [--exec-prefix=PREFIX] [--with-PACKAGE[=VALUE]]
+# Ignores all args except --srcdir, --prefix, --exec-prefix, and
+# --with-PACKAGE[=VALUE] unless this script has special code to handle it.
+
+for arg
+do
+  # Handle --exec-prefix with a space before the argument.
+  if test x$next_exec_prefix = xyes; then exec_prefix=$arg; next_exec_prefix=
+  # Handle --host with a space before the argument.
+  elif test x$next_host = xyes; then next_host=
+  # Handle --prefix with a space before the argument.
+  elif test x$next_prefix = xyes; then prefix=$arg; next_prefix=
+  # Handle --srcdir with a space before the argument.
+  elif test x$next_srcdir = xyes; then srcdir=$arg; next_srcdir=
+  else
+    case $arg in
+     # For backward compatibility, recognize -exec-prefix and --exec_prefix.
+     -exec-prefix=* | --exec_prefix=* | --exec-prefix=* | --exec-prefi=* | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* | --exec=* | --exe=* | --ex=* | --e=*)
+	exec_prefix=`echo $arg | sed 's/[-a-z_]*=//'` ;;
+     -exec-prefix | --exec_prefix | --exec-prefix | --exec-prefi | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- | --exec | --exe | --ex | --e)
+	next_exec_prefix=yes ;;
+
+     -gas | --gas | --ga | --g) ;;
+
+     -host=* | --host=* | --hos=* | --ho=* | --h=*) ;;
+     -host | --host | --hos | --ho | --h)
+	next_host=yes ;;
+
+     -nfp | --nfp | --nf) ;;
+
+     -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
+	prefix=`echo $arg | sed 's/[-a-z_]*=//'` ;;
+     -prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
+	next_prefix=yes ;;
+
+     -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=* | --s=*)
+	srcdir=`echo $arg | sed 's/[-a-z_]*=//'` ;;
+     -srcdir | --srcdir | --srcdi | --srcd | --src | --sr | --s)
+	next_srcdir=yes ;;
+
+     -with-* | --with-*)
+       package=`echo $arg|sed -e 's/-*with-//' -e 's/=.*//'`
+       # Reject names that aren't valid shell variable names.
+       if test -n "`echo $package| sed 's/[-a-zA-Z0-9_]//g'`"; then
+         echo "configure: $package: invalid package name" >&2; exit 1
+       fi
+       package=`echo $package| sed 's/-/_/g'`
+       case "$arg" in
+         *=*) val="`echo $arg|sed 's/[^=]*=//'`" ;;
+         *) val=1 ;;
+       esac
+       eval "with_$package='$val'" ;;
+
+     -v | -verbose | --verbose | --verbos | --verbo | --verb | --ver | --ve | --v)
+       verbose=yes ;;
+
+     *=*)
+       varname=`echo $arg|sed -e 's/=.*//'`
+       # Reject names that aren't valid shell variable names.
+       if test -n "`echo $varname| sed 's/[a-zA-Z0-9_]//g'`"; then
+         echo "configure: $varname: invalid shell variable name" >&2; exit 1
+       fi
+       val="`echo $arg|sed 's/[^=]*=//'`"
+       test -n "$verbose" && echo "	setting shell variable $varname to $val"
+       eval "$varname='$val'"
+       eval "export $varname" ;;
+
+     *) ;;
+    esac
+  fi
+done
+
+trap 'rm -fr conftest* confdefs* core; exit 1' 1 3 15
+trap 'rm -f confdefs*' 0
+
+# NLS nuisances.
+# These must not be set unconditionally because not all systems understand
+# e.g. LANG=C (notably SCO).
+if test "${LC_ALL+set}" = 'set' ; then LC_ALL=C; export LC_ALL; fi
+if test "${LANG+set}"   = 'set' ; then LANG=C;   export LANG;   fi
+
+rm -f conftest* confdefs.h
+# AIX cpp loses on an empty file, so make sure it contains at least a newline.
+echo > confdefs.h
+compile='${CC-cc} $CFLAGS conftest.c -o conftest $LIBS >/dev/null 2>&1'
+
+# A filename unique to this package, relative to the directory that
+# configure is in, which we can look for to find out if srcdir is correct.
+unique_file=jcmaster.c
+
+# Find the source files, if location was not specified.
+if test -z "$srcdir"; then
+  srcdirdefaulted=yes
+  # Try the directory containing this script, then `..'.
+  prog=$0
+  confdir=`echo $prog|sed 's%/[^/][^/]*$%%'`
+  test "X$confdir" = "X$prog" && confdir=.
+  srcdir=$confdir
+  if test ! -r $srcdir/$unique_file; then
+    srcdir=..
+  fi
+fi
+if test ! -r $srcdir/$unique_file; then
+  if test x$srcdirdefaulted = xyes; then
+    echo "configure: Can not find sources in \`${confdir}' or \`..'." 1>&2
+  else
+    echo "configure: Can not find sources in \`${srcdir}'." 1>&2
+  fi
+  exit 1
+fi
+# Preserve a srcdir of `.' to avoid automounter screwups with pwd.
+# But we can't avoid them for `..', to make subdirectories work.
+case $srcdir in
+  .|/*|~*) ;;
+  *) srcdir=`cd $srcdir; pwd` ;; # Make relative path absolute.
+esac
+
+
+# Save the original args to write them into config.status later.
+configure_args="$*"
+
+
+if test -z "$CC"; then
+  # Extract the first word of `gcc', so it can be a program name with args.
+  set dummy gcc; word=$2
+  echo checking for $word
+  IFS="${IFS= 	}"; saveifs="$IFS"; IFS="${IFS}:"
+  for dir in $PATH; do
+    test -z "$dir" && dir=.
+    if test -f $dir/$word; then
+      CC="gcc"
+      break
+    fi
+  done
+  IFS="$saveifs"
+fi
+test -z "$CC" && CC="cc"
+test -n "$CC" && test -n "$verbose" && echo "	setting CC to $CC"
+
+# Find out if we are using GNU C, under whatever name.
+cat > conftest.c <<EOF
+#ifdef __GNUC__
+  yes
+#endif
+EOF
+${CC-cc} -E conftest.c > conftest.out 2>&1
+if egrep yes conftest.out >/dev/null 2>&1; then
+  GCC=1 # For later tests.
+fi
+rm -f conftest*
+
+echo checking how to run the C preprocessor
+if test -z "$CPP"; then
+  # This must be in double quotes, not single quotes, because CPP may get
+  # substituted into the Makefile and ``${CC-cc}'' will simply confuse
+  # make.  It must be expanded now.
+  CPP="${CC-cc} -E"
+  cat > conftest.c <<EOF
+#include "confdefs.h"
+#include <stdio.h>
+Syntax Error
+EOF
+err=`eval "($CPP conftest.c >/dev/null) 2>&1"`
+if test -z "$err"; then
+  :
+else
+  rm -rf conftest*
+  CPP=/lib/cpp
+fi
+rm -f conftest*
+fi
+test ".${verbose}" != "." && echo "	setting CPP to $CPP"
+
+echo checking whether cross-compiling
+# If we cannot run a trivial program, we must be cross compiling.
+cat > conftest.c <<EOF
+#include "confdefs.h"
+main(){exit(0);}
+EOF
+eval $compile
+if test -s conftest && (./conftest; exit) 2>/dev/null; then
+  :
+else
+  cross_compiling=1
+fi
+rm -fr conftest*
+
+echo checking for function prototypes
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+int testfunction (int arg1, int * arg2); /* check prototypes */
+struct methods_struct {		/* check method-pointer declarations */
+  int (*error_exit) (char *msgtext);
+  int (*trace_message) (char *msgtext);
+  int (*another_method) (void);
+};
+int testfunction (int arg1, int * arg2) /* check definitions */
+{ return arg2[arg1]; }
+int test2function (void)	/* check void arg list */
+{ return 0; }
+
+int main() { exit(0); }
+int t() {   }
+EOF
+if eval $compile; then
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" HAVE_PROTOTYPES to be empty
+echo "#define" HAVE_PROTOTYPES  >> confdefs.h
+DEFS="$DEFS -DHAVE_PROTOTYPES="
+SEDDEFS="${SEDDEFS}\${SEDdA}HAVE_PROTOTYPES\${SEDdB}HAVE_PROTOTYPES\${SEDdC}\${SEDdD}
+\${SEDuA}HAVE_PROTOTYPES\${SEDuB}HAVE_PROTOTYPES\${SEDuC}\${SEDuD}
+\${SEDeA}HAVE_PROTOTYPES\${SEDeB}HAVE_PROTOTYPES\${SEDeC}\${SEDeD}
+"
+}
+
+
+else
+  rm -rf conftest*
+  echo Your compiler does not seem to know about function prototypes.
+echo Perhaps it needs a special switch to enable ANSI C mode.
+echo If so, we recommend running configure like this:
+echo "   ./configure  CC='cc -switch'"
+echo where -switch is the proper switch.
+
+fi
+rm -f conftest*
+echo checking for stddef.h
+cat > conftest.c <<EOF
+#include "confdefs.h"
+#include <stddef.h>
+EOF
+err=`eval "($CPP conftest.c >/dev/null) 2>&1"`
+if test -z "$err"; then
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" HAVE_STDDEF_H to be empty
+echo "#define" HAVE_STDDEF_H  >> confdefs.h
+DEFS="$DEFS -DHAVE_STDDEF_H="
+SEDDEFS="${SEDDEFS}\${SEDdA}HAVE_STDDEF_H\${SEDdB}HAVE_STDDEF_H\${SEDdC}\${SEDdD}
+\${SEDuA}HAVE_STDDEF_H\${SEDuB}HAVE_STDDEF_H\${SEDuC}\${SEDuD}
+\${SEDeA}HAVE_STDDEF_H\${SEDeB}HAVE_STDDEF_H\${SEDeC}\${SEDeD}
+"
+}
+
+
+fi
+rm -f conftest*
+
+echo checking for stdlib.h
+cat > conftest.c <<EOF
+#include "confdefs.h"
+#include <stdlib.h>
+EOF
+err=`eval "($CPP conftest.c >/dev/null) 2>&1"`
+if test -z "$err"; then
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" HAVE_STDLIB_H to be empty
+echo "#define" HAVE_STDLIB_H  >> confdefs.h
+DEFS="$DEFS -DHAVE_STDLIB_H="
+SEDDEFS="${SEDDEFS}\${SEDdA}HAVE_STDLIB_H\${SEDdB}HAVE_STDLIB_H\${SEDdC}\${SEDdD}
+\${SEDuA}HAVE_STDLIB_H\${SEDuB}HAVE_STDLIB_H\${SEDuC}\${SEDuD}
+\${SEDeA}HAVE_STDLIB_H\${SEDeB}HAVE_STDLIB_H\${SEDeC}\${SEDeD}
+"
+}
+
+
+fi
+rm -f conftest*
+
+echo checking for string.h
+cat > conftest.c <<EOF
+#include "confdefs.h"
+#include <string.h>
+EOF
+err=`eval "($CPP conftest.c >/dev/null) 2>&1"`
+if test -z "$err"; then
+  :
+else
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" NEED_BSD_STRINGS to be empty
+echo "#define" NEED_BSD_STRINGS  >> confdefs.h
+DEFS="$DEFS -DNEED_BSD_STRINGS="
+SEDDEFS="${SEDDEFS}\${SEDdA}NEED_BSD_STRINGS\${SEDdB}NEED_BSD_STRINGS\${SEDdC}\${SEDdD}
+\${SEDuA}NEED_BSD_STRINGS\${SEDuB}NEED_BSD_STRINGS\${SEDuC}\${SEDuD}
+\${SEDeA}NEED_BSD_STRINGS\${SEDeB}NEED_BSD_STRINGS\${SEDeC}\${SEDeD}
+"
+}
+
+fi
+rm -f conftest*
+
+echo checking for size_t
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+#ifdef HAVE_STDDEF_H
+#include <stddef.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#include <stdio.h>
+#ifdef NEED_BSD_STRINGS
+#include <strings.h>
+#else
+#include <string.h>
+#endif
+typedef size_t my_size_t;
+
+int main() { exit(0); }
+int t() {  my_size_t foovar;  }
+EOF
+if eval $compile; then
+  :
+else
+  rm -rf conftest*
+  echo checking for sys/types.h
+cat > conftest.c <<EOF
+#include "confdefs.h"
+#include <sys/types.h>
+EOF
+err=`eval "($CPP conftest.c >/dev/null) 2>&1"`
+if test -z "$err"; then
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" NEED_SYS_TYPES_H to be empty
+echo "#define" NEED_SYS_TYPES_H  >> confdefs.h
+DEFS="$DEFS -DNEED_SYS_TYPES_H="
+SEDDEFS="${SEDDEFS}\${SEDdA}NEED_SYS_TYPES_H\${SEDdB}NEED_SYS_TYPES_H\${SEDdC}\${SEDdD}
+\${SEDuA}NEED_SYS_TYPES_H\${SEDuB}NEED_SYS_TYPES_H\${SEDuC}\${SEDuD}
+\${SEDeA}NEED_SYS_TYPES_H\${SEDeB}NEED_SYS_TYPES_H\${SEDeC}\${SEDeD}
+"
+}
+
+
+else
+  rm -rf conftest*
+  echo Type size_t is not defined in any of the usual places.
+echo Try putting '"typedef unsigned int size_t;"' in jconfig.h.
+
+fi
+rm -f conftest*
+
+fi
+rm -f conftest*
+echo checking for unsigned char
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+int main() { exit(0); }
+int t() {  unsigned char un_char;  }
+EOF
+if eval $compile; then
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" HAVE_UNSIGNED_CHAR to be empty
+echo "#define" HAVE_UNSIGNED_CHAR  >> confdefs.h
+DEFS="$DEFS -DHAVE_UNSIGNED_CHAR="
+SEDDEFS="${SEDDEFS}\${SEDdA}HAVE_UNSIGNED_CHAR\${SEDdB}HAVE_UNSIGNED_CHAR\${SEDdC}\${SEDdD}
+\${SEDuA}HAVE_UNSIGNED_CHAR\${SEDuB}HAVE_UNSIGNED_CHAR\${SEDuC}\${SEDuD}
+\${SEDeA}HAVE_UNSIGNED_CHAR\${SEDeB}HAVE_UNSIGNED_CHAR\${SEDeC}\${SEDeD}
+"
+}
+
+
+fi
+rm -f conftest*
+echo checking for unsigned short
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+int main() { exit(0); }
+int t() {  unsigned short un_short;  }
+EOF
+if eval $compile; then
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" HAVE_UNSIGNED_SHORT to be empty
+echo "#define" HAVE_UNSIGNED_SHORT  >> confdefs.h
+DEFS="$DEFS -DHAVE_UNSIGNED_SHORT="
+SEDDEFS="${SEDDEFS}\${SEDdA}HAVE_UNSIGNED_SHORT\${SEDdB}HAVE_UNSIGNED_SHORT\${SEDdC}\${SEDdD}
+\${SEDuA}HAVE_UNSIGNED_SHORT\${SEDuB}HAVE_UNSIGNED_SHORT\${SEDuC}\${SEDuD}
+\${SEDeA}HAVE_UNSIGNED_SHORT\${SEDeB}HAVE_UNSIGNED_SHORT\${SEDeC}\${SEDeD}
+"
+}
+
+
+fi
+rm -f conftest*
+echo checking for type void
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+/* Caution: a C++ compiler will insist on valid prototypes */
+typedef void * void_ptr;	/* check void * */
+#ifdef HAVE_PROTOTYPES		/* check ptr to function returning void */
+typedef void (*void_func) (int a, int b);
+#else
+typedef void (*void_func) ();
+#endif
+
+#ifdef HAVE_PROTOTYPES		/* check void function result */
+void test3function (void_ptr arg1, void_func arg2)
+#else
+void test3function (arg1, arg2)
+     void_ptr arg1;
+     void_func arg2;
+#endif
+{
+  char * locptr = (char *) arg1; /* check casting to and from void * */
+  arg1 = (void *) locptr;
+  (*arg2) (1, 2);		/* check call of fcn returning void */
+}
+
+int main() { exit(0); }
+int t() {   }
+EOF
+if eval $compile; then
+  :
+else
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" void to be char
+echo "#define" void char >> confdefs.h
+DEFS="$DEFS -Dvoid=char"
+SEDDEFS="${SEDDEFS}\${SEDdA}void\${SEDdB}void\${SEDdC}char\${SEDdD}
+\${SEDuA}void\${SEDuB}void\${SEDuC}char\${SEDuD}
+\${SEDeA}void\${SEDeB}void\${SEDeC}char\${SEDeD}
+"
+}
+
+fi
+rm -f conftest*
+prog='/* Ultrix mips cc rejects this.  */
+typedef int charset[2]; const charset x;
+/* SunOS 4.1.1 cc rejects this.  */
+char const *const *ccp;
+char **p;
+/* AIX XL C 1.02.0.0 rejects this.
+   It does not let you subtract one const X* pointer from another in an arm
+   of an if-expression whose if-part is not a constant expression */
+const char *g = "string";
+ccp = &g + (g ? g-g : 0);
+/* HPUX 7.0 cc rejects these. */
+++ccp;
+p = (char**) ccp;
+ccp = (char const *const *) p;
+{ /* SCO 3.2v4 cc rejects this.  */
+  char *t;
+  char const *s = 0 ? (char *) 0 : (char const *) 0;
+
+  *t++ = 0;
+}
+{ /* Someone thinks the Sun supposedly-ANSI compiler will reject this.  */
+  int x[] = {25,17};
+  const int *foo = &x[0];
+  ++foo;
+}
+{ /* Sun SC1.0 ANSI compiler rejects this -- but not the above. */
+  typedef const int *iptr;
+  iptr p = 0;
+  ++p;
+}
+{ /* AIX XL C 1.02.0.0 rejects this saying
+     "k.c", line 2.27: 1506-025 (S) Operand must be a modifiable lvalue. */
+  struct s { int j; const int *ap[3]; };
+  struct s *b; b->j = 5;
+}
+{ /* ULTRIX-32 V3.1 (Rev 9) vcc rejects this */
+  const int foo = 10;
+}'
+echo checking for lack of working const
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+int main() { exit(0); }
+int t() { $prog }
+EOF
+if eval $compile; then
+  :
+else
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" const to be empty
+echo "#define" const  >> confdefs.h
+DEFS="$DEFS -Dconst="
+SEDDEFS="${SEDDEFS}\${SEDdA}const\${SEDdB}const\${SEDdC}\${SEDdD}
+\${SEDuA}const\${SEDuB}const\${SEDuC}\${SEDuD}
+\${SEDeA}const\${SEDeB}const\${SEDeC}\${SEDeD}
+"
+}
+
+fi
+rm -f conftest*
+
+echo checking for inline
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+int main() { exit(0); }
+int t() { } inline int foo() { return 0; }
+int bar() { return foo(); }
+EOF
+if eval $compile; then
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" INLINE to be inline
+echo "#define" INLINE inline >> confdefs.h
+DEFS="$DEFS -DINLINE=inline"
+SEDDEFS="${SEDDEFS}\${SEDdA}INLINE\${SEDdB}INLINE\${SEDdC}inline\${SEDdD}
+\${SEDuA}INLINE\${SEDuB}INLINE\${SEDuC}inline\${SEDuD}
+\${SEDeA}INLINE\${SEDeB}INLINE\${SEDeC}inline\${SEDeD}
+"
+}
+
+
+else
+  rm -rf conftest*
+  cat > conftest.c <<EOF
+#include "confdefs.h"
+
+int main() { exit(0); }
+int t() { } __inline__ int foo() { return 0; }
+int bar() { return foo(); }
+EOF
+if eval $compile; then
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" INLINE to be __inline__
+echo "#define" INLINE __inline__ >> confdefs.h
+DEFS="$DEFS -DINLINE=__inline__"
+SEDDEFS="${SEDDEFS}\${SEDdA}INLINE\${SEDdB}INLINE\${SEDdC}__inline__\${SEDdD}
+\${SEDuA}INLINE\${SEDuB}INLINE\${SEDuC}__inline__\${SEDuD}
+\${SEDeA}INLINE\${SEDeB}INLINE\${SEDeC}__inline__\${SEDeD}
+"
+}
+
+
+else
+  rm -rf conftest*
+  cat > conftest.c <<EOF
+#include "confdefs.h"
+
+int main() { exit(0); }
+int t() { } __inline int foo() { return 0; }
+int bar() { return foo(); }
+EOF
+if eval $compile; then
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" INLINE to be __inline
+echo "#define" INLINE __inline >> confdefs.h
+DEFS="$DEFS -DINLINE=__inline"
+SEDDEFS="${SEDDEFS}\${SEDdA}INLINE\${SEDdB}INLINE\${SEDdC}__inline\${SEDdD}
+\${SEDuA}INLINE\${SEDuB}INLINE\${SEDuC}__inline\${SEDuD}
+\${SEDeA}INLINE\${SEDeB}INLINE\${SEDeC}__inline\${SEDeD}
+"
+}
+
+
+else
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" INLINE to be empty
+echo "#define" INLINE  >> confdefs.h
+DEFS="$DEFS -DINLINE="
+SEDDEFS="${SEDDEFS}\${SEDdA}INLINE\${SEDdB}INLINE\${SEDdC}\${SEDdD}
+\${SEDuA}INLINE\${SEDuB}INLINE\${SEDuC}\${SEDuD}
+\${SEDeA}INLINE\${SEDeB}INLINE\${SEDeC}\${SEDeD}
+"
+}
+
+fi
+rm -f conftest*
+
+fi
+rm -f conftest*
+
+fi
+rm -f conftest*
+echo checking for broken incomplete types
+cat > conftest.c <<EOF
+#include "confdefs.h"
+ typedef struct undefined_structure * undef_struct_ptr; 
+int main() { exit(0); }
+int t() {  }
+EOF
+if eval $compile; then
+  :
+else
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" INCOMPLETE_TYPES_BROKEN to be empty
+echo "#define" INCOMPLETE_TYPES_BROKEN  >> confdefs.h
+DEFS="$DEFS -DINCOMPLETE_TYPES_BROKEN="
+SEDDEFS="${SEDDEFS}\${SEDdA}INCOMPLETE_TYPES_BROKEN\${SEDdB}INCOMPLETE_TYPES_BROKEN\${SEDdC}\${SEDdD}
+\${SEDuA}INCOMPLETE_TYPES_BROKEN\${SEDuB}INCOMPLETE_TYPES_BROKEN\${SEDuC}\${SEDuD}
+\${SEDeA}INCOMPLETE_TYPES_BROKEN\${SEDeB}INCOMPLETE_TYPES_BROKEN\${SEDeC}\${SEDeD}
+"
+}
+
+fi
+rm -f conftest*
+echo checking for short external names
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+int possibly_duplicate_function () { return 0; }
+int possibly_dupli_function () { return 1; }
+
+int main() { exit(0); }
+int t() {   }
+EOF
+if eval $compile; then
+  :
+else
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" NEED_SHORT_EXTERNAL_NAMES to be empty
+echo "#define" NEED_SHORT_EXTERNAL_NAMES  >> confdefs.h
+DEFS="$DEFS -DNEED_SHORT_EXTERNAL_NAMES="
+SEDDEFS="${SEDDEFS}\${SEDdA}NEED_SHORT_EXTERNAL_NAMES\${SEDdB}NEED_SHORT_EXTERNAL_NAMES\${SEDdC}\${SEDdD}
+\${SEDuA}NEED_SHORT_EXTERNAL_NAMES\${SEDuB}NEED_SHORT_EXTERNAL_NAMES\${SEDuC}\${SEDuD}
+\${SEDeA}NEED_SHORT_EXTERNAL_NAMES\${SEDeB}NEED_SHORT_EXTERNAL_NAMES\${SEDeC}\${SEDeD}
+"
+}
+
+fi
+rm -f conftest*
+
+echo checking to see if char is signed
+if test -n "$cross_compiling"
+then
+  echo Assuming that char is signed on target machine.
+echo If it is unsigned, this will be a little bit inefficient.
+
+else
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+#ifdef HAVE_PROTOTYPES
+int is_char_signed (int arg)
+#else
+int is_char_signed (arg)
+     int arg;
+#endif
+{
+  if (arg == 189) {		/* expected result for unsigned char */
+    return 0;			/* type char is unsigned */
+  }
+  else if (arg != -67) {	/* expected result for signed char */
+    printf("Hmm, it seems 'char' is not eight bits wide on your machine.\n");
+    printf("I fear the JPEG software will not work at all.\n\n");
+  }
+  return 1;			/* assume char is signed otherwise */
+}
+char signed_char_check = (char) (-67);
+main() {
+  exit(is_char_signed((int) signed_char_check));
+}
+EOF
+eval $compile
+if test -s conftest && (./conftest; exit) 2>/dev/null; then
+  
+{
+test -n "$verbose" && \
+echo "	defining" CHAR_IS_UNSIGNED to be empty
+echo "#define" CHAR_IS_UNSIGNED  >> confdefs.h
+DEFS="$DEFS -DCHAR_IS_UNSIGNED="
+SEDDEFS="${SEDDEFS}\${SEDdA}CHAR_IS_UNSIGNED\${SEDdB}CHAR_IS_UNSIGNED\${SEDdC}\${SEDdD}
+\${SEDuA}CHAR_IS_UNSIGNED\${SEDuB}CHAR_IS_UNSIGNED\${SEDuC}\${SEDuD}
+\${SEDeA}CHAR_IS_UNSIGNED\${SEDeB}CHAR_IS_UNSIGNED\${SEDeC}\${SEDeD}
+"
+}
+
+
+fi
+fi
+rm -fr conftest*
+echo checking to see if right shift is signed
+if test -n "$cross_compiling"
+then
+  echo Assuming that right shift is signed on target machine.
+
+else
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+#ifdef HAVE_PROTOTYPES
+int is_shifting_signed (long arg)
+#else
+int is_shifting_signed (arg)
+     long arg;
+#endif
+/* See whether right-shift on a long is signed or not. */
+{
+  long res = arg >> 4;
+
+  if (res == -0x7F7E80CL) {	/* expected result for signed shift */
+    return 1;			/* right shift is signed */
+  }
+  /* see if unsigned-shift hack will fix it. */
+  /* we can't just test exact value since it depends on width of long... */
+  res |= (~0L) << (32-4);
+  if (res == -0x7F7E80CL) {	/* expected result now? */
+    return 0;			/* right shift is unsigned */
+  }
+  printf("Right shift isn't acting as I expect it to.\n");
+  printf("I fear the JPEG software will not work at all.\n\n");
+  return 0;			/* try it with unsigned anyway */
+}
+main() {
+  exit(is_shifting_signed(-0x7F7E80B1L));
+}
+EOF
+eval $compile
+if test -s conftest && (./conftest; exit) 2>/dev/null; then
+  
+{
+test -n "$verbose" && \
+echo "	defining" RIGHT_SHIFT_IS_UNSIGNED to be empty
+echo "#define" RIGHT_SHIFT_IS_UNSIGNED  >> confdefs.h
+DEFS="$DEFS -DRIGHT_SHIFT_IS_UNSIGNED="
+SEDDEFS="${SEDDEFS}\${SEDdA}RIGHT_SHIFT_IS_UNSIGNED\${SEDdB}RIGHT_SHIFT_IS_UNSIGNED\${SEDdC}\${SEDdD}
+\${SEDuA}RIGHT_SHIFT_IS_UNSIGNED\${SEDuB}RIGHT_SHIFT_IS_UNSIGNED\${SEDuC}\${SEDuD}
+\${SEDeA}RIGHT_SHIFT_IS_UNSIGNED\${SEDeB}RIGHT_SHIFT_IS_UNSIGNED\${SEDeC}\${SEDeD}
+"
+}
+
+
+fi
+fi
+rm -fr conftest*
+echo checking to see if fopen accepts b spec
+if test -n "$cross_compiling"
+then
+  echo Assuming that it does.
+
+else
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+#include <stdio.h>
+main() {
+  if (fopen("conftestdata", "wb") != NULL)
+    exit(0);
+  exit(1);
+}
+EOF
+eval $compile
+if test -s conftest && (./conftest; exit) 2>/dev/null; then
+  :
+else
+  
+{
+test -n "$verbose" && \
+echo "	defining" DONT_USE_B_MODE to be empty
+echo "#define" DONT_USE_B_MODE  >> confdefs.h
+DEFS="$DEFS -DDONT_USE_B_MODE="
+SEDDEFS="${SEDDEFS}\${SEDdA}DONT_USE_B_MODE\${SEDdB}DONT_USE_B_MODE\${SEDdC}\${SEDdD}
+\${SEDuA}DONT_USE_B_MODE\${SEDuB}DONT_USE_B_MODE\${SEDuC}\${SEDuD}
+\${SEDeA}DONT_USE_B_MODE\${SEDeB}DONT_USE_B_MODE\${SEDeC}\${SEDeD}
+"
+}
+
+fi
+fi
+rm -fr conftest*
+# Make sure to not get the incompatible SysV /etc/install and
+# /usr/sbin/install, which might be in PATH before a BSD-like install,
+# or the SunOS /usr/etc/install directory, or the AIX /bin/install,
+# or the AFS install, which mishandles nonexistent args, or
+# /usr/ucb/install on SVR4, which tries to use the nonexistent group
+# `staff'.  On most BSDish systems install is in /usr/bin, not /usr/ucb
+# anyway.  Sigh.
+if test "z${INSTALL}" = "z" ; then
+  echo checking for install
+  IFS="${IFS= 	}"; saveifs="$IFS"; IFS="${IFS}:"
+  for dir in $PATH; do
+    test -z "$dir" && dir=.
+    case $dir in
+    /etc|/usr/sbin|/usr/etc|/usr/afsws/bin|/usr/ucb) ;;
+    *)
+      if test -f $dir/installbsd; then
+	INSTALL="$dir/installbsd -c" # OSF1
+	INSTALL_PROGRAM='$(INSTALL)'
+	INSTALL_DATA='$(INSTALL) -m 644'
+	break
+      fi
+      if test -f $dir/install; then
+	if grep dspmsg $dir/install >/dev/null 2>&1; then
+	  : # AIX
+	else
+	  INSTALL="$dir/install -c"
+	  INSTALL_PROGRAM='$(INSTALL)'
+	  INSTALL_DATA='$(INSTALL) -m 644'
+	  break
+	fi
+      fi
+      ;;
+    esac
+  done
+  IFS="$saveifs"
+fi
+INSTALL=${INSTALL-cp}
+test -n "$verbose" && echo "	setting INSTALL to $INSTALL"
+INSTALL_PROGRAM=${INSTALL_PROGRAM-'$(INSTALL)'}
+test -n "$verbose" && echo "	setting INSTALL_PROGRAM to $INSTALL_PROGRAM"
+INSTALL_DATA=${INSTALL_DATA-'$(INSTALL)'}
+test -n "$verbose" && echo "	setting INSTALL_DATA to $INSTALL_DATA"
+
+if test -z "$RANLIB"; then
+  # Extract the first word of `ranlib', so it can be a program name with args.
+  set dummy ranlib; word=$2
+  echo checking for $word
+  IFS="${IFS= 	}"; saveifs="$IFS"; IFS="${IFS}:"
+  for dir in $PATH; do
+    test -z "$dir" && dir=.
+    if test -f $dir/$word; then
+      RANLIB="ranlib"
+      break
+    fi
+  done
+  IFS="$saveifs"
+fi
+test -z "$RANLIB" && RANLIB=":"
+test -n "$RANLIB" && test -n "$verbose" && echo "	setting RANLIB to $RANLIB"
+
+# check whether --with-maxmem was given
+withval="$with_maxmem"
+if test -n "$withval"; then
+  DEFAULTMAXMEM=`expr $withval \* 1048576`
+
+{
+test -n "$verbose" && \
+echo "	defining" DEFAULT_MAX_MEM to be ${DEFAULTMAXMEM}
+echo "#define" DEFAULT_MAX_MEM ${DEFAULTMAXMEM} >> confdefs.h
+DEFS="$DEFS -DDEFAULT_MAX_MEM=${DEFAULTMAXMEM}"
+SEDDEFS="${SEDDEFS}\${SEDdA}DEFAULT_MAX_MEM\${SEDdB}DEFAULT_MAX_MEM\${SEDdC}${DEFAULTMAXMEM}\${SEDdD}
+\${SEDuA}DEFAULT_MAX_MEM\${SEDuB}DEFAULT_MAX_MEM\${SEDuC}${DEFAULTMAXMEM}\${SEDuD}
+\${SEDeA}DEFAULT_MAX_MEM\${SEDeB}DEFAULT_MAX_MEM\${SEDeC}${DEFAULTMAXMEM}\${SEDeD}
+"
+}
+
+echo checking for 'tmpfile()'
+cat > conftest.c <<EOF
+#include "confdefs.h"
+#include <stdio.h>
+int main() { exit(0); }
+int t() {  FILE * tfile = tmpfile();  }
+EOF
+if eval $compile; then
+  rm -rf conftest*
+  MEMORYMGR="jmemansi.o"
+
+else
+  rm -rf conftest*
+  MEMORYMGR="jmemname.o"
+
+{
+test -n "$verbose" && \
+echo "	defining" NEED_SIGNAL_CATCHER to be empty
+echo "#define" NEED_SIGNAL_CATCHER  >> confdefs.h
+DEFS="$DEFS -DNEED_SIGNAL_CATCHER="
+SEDDEFS="${SEDDEFS}\${SEDdA}NEED_SIGNAL_CATCHER\${SEDdB}NEED_SIGNAL_CATCHER\${SEDdC}\${SEDdD}
+\${SEDuA}NEED_SIGNAL_CATCHER\${SEDuB}NEED_SIGNAL_CATCHER\${SEDuC}\${SEDuD}
+\${SEDeA}NEED_SIGNAL_CATCHER\${SEDeB}NEED_SIGNAL_CATCHER\${SEDeC}\${SEDeD}
+"
+}
+echo checking for 'mktemp()'
+cat > conftest.c <<EOF
+#include "confdefs.h"
+
+int main() { exit(0); }
+int t() {  char fname[80]; mktemp(fname);  }
+EOF
+if eval $compile; then
+  :
+else
+  rm -rf conftest*
+  
+{
+test -n "$verbose" && \
+echo "	defining" NO_MKTEMP to be empty
+echo "#define" NO_MKTEMP  >> confdefs.h
+DEFS="$DEFS -DNO_MKTEMP="
+SEDDEFS="${SEDDEFS}\${SEDdA}NO_MKTEMP\${SEDdB}NO_MKTEMP\${SEDdC}\${SEDdD}
+\${SEDuA}NO_MKTEMP\${SEDuB}NO_MKTEMP\${SEDuC}\${SEDuD}
+\${SEDeA}NO_MKTEMP\${SEDeB}NO_MKTEMP\${SEDeC}\${SEDeD}
+"
+}
+
+fi
+rm -f conftest*
+
+fi
+rm -f conftest*
+
+else
+  MEMORYMGR="jmemnobs.o"
+fi
+
+# Prepare to massage makefile.auto correctly.
+case "$DEFS" in
+  *HAVE_PROTOTYPES*)
+	ANSI2KNR=""
+	ISANSICOM="# "
+	;;
+  *)
+	ANSI2KNR="ansi2knr"
+	ISANSICOM=""
+	;;
+esac
+case "$DEFS" in
+  *NEED_BSD_STRINGS*)
+	ANSI2KNRFLAGS="-DBSD"
+	;;
+  *)
+	ANSI2KNRFLAGS=""
+	;;
+esac
+# Set default prefixes.
+if test -n "$prefix"; then
+  test -z "$exec_prefix" && exec_prefix='${prefix}'
+  prsub="s%^prefix\\([ 	]*\\)=\\([ 	]*\\).*$%prefix\\1=\\2$prefix%"
+fi
+if test -n "$exec_prefix"; then
+  prsub="$prsub
+s%^exec_prefix\\([ 	]*\\)=\\([ 	]*\\).*$%exec_prefix\\1=\\2$exec_prefix%"
+fi
+# Quote sed substitution magic chars in DEFS.
+cat >conftest.def <<EOF
+$DEFS
+EOF
+escape_ampersand_and_backslash='s%[&\\]%\\&%g'
+DEFS=`sed "$escape_ampersand_and_backslash" <conftest.def`
+rm -f conftest.def
+# Substitute for predefined variables.
+
+trap 'rm -f config.status; exit 1' 1 3 15
+echo creating config.status
+rm -f config.status
+cat > config.status <<EOF
+#!/bin/sh
+# Generated automatically by configure.
+# Run this file to recreate the current configuration.
+# This directory was configured as follows,
+# on host `(hostname || uname -n) 2>/dev/null | sed 1q`:
+#
+# $0 $configure_args
+
+for arg
+do
+  case "\$arg" in
+    -recheck | --recheck | --rechec | --reche | --rech | --rec | --re | --r)
+    echo running \${CONFIG_SHELL-/bin/sh} $0 $configure_args
+    exec \${CONFIG_SHELL-/bin/sh} $0 $configure_args ;;
+    *) echo "Usage: config.status --recheck" 2>&1; exit 1 ;;
+  esac
+done
+
+trap 'rm -fr Makefile jconfig.h conftest*; exit 1' 1 3 15
+CC='$CC'
+CPP='$CPP'
+INSTALL='$INSTALL'
+INSTALL_PROGRAM='$INSTALL_PROGRAM'
+INSTALL_DATA='$INSTALL_DATA'
+RANLIB='$RANLIB'
+MEMORYMGR='$MEMORYMGR'
+ANSI2KNR='$ANSI2KNR'
+ISANSICOM='$ISANSICOM'
+ANSI2KNRFLAGS='$ANSI2KNRFLAGS'
+LIBS='$LIBS'
+srcdir='$srcdir'
+prefix='$prefix'
+exec_prefix='$exec_prefix'
+prsub='$prsub'
+extrasub='$extrasub'
+EOF
+cat >> config.status <<\EOF
+
+top_srcdir=$srcdir
+
+CONFIG_FILES=${CONFIG_FILES-"Makefile"}
+for file in .. ${CONFIG_FILES}; do if test "x$file" != x..; then
+  srcdir=$top_srcdir
+  # Remove last slash and all that follows it.  Not all systems have dirname.
+  dir=`echo $file|sed 's%/[^/][^/]*$%%'`
+  if test "$dir" != "$file"; then
+    test "$top_srcdir" != . && srcdir=$top_srcdir/$dir
+    test ! -d $dir && mkdir $dir
+  fi
+  echo creating $file
+  rm -f $file
+  echo "# Generated automatically from `echo $file|sed 's|.*/||'`.auto by configure." > $file
+  sed -e "
+$prsub
+$extrasub
+s%@CC@%$CC%g
+s%@CPP@%$CPP%g
+s%@INSTALL@%$INSTALL%g
+s%@INSTALL_PROGRAM@%$INSTALL_PROGRAM%g
+s%@INSTALL_DATA@%$INSTALL_DATA%g
+s%@RANLIB@%$RANLIB%g
+s%@MEMORYMGR@%$MEMORYMGR%g
+s%@ANSI2KNR@%$ANSI2KNR%g
+s%@ISANSICOM@%$ISANSICOM%g
+s%@ANSI2KNRFLAGS@%$ANSI2KNRFLAGS%g
+s%@LIBS@%$LIBS%g
+s%@srcdir@%$srcdir%g
+s%@DEFS@%-DHAVE_CONFIG_H%" $top_srcdir/makefile.auto >> $file
+fi; done
+
+CONFIG_HEADERS=${CONFIG_HEADERS-"jconfig.h"}
+for file in .. ${CONFIG_HEADERS}; do if test "x$file" != x..; then
+echo creating $file
+
+# These sed commands are put into SEDDEFS when defining a macro.
+# They are broken into pieces to make the sed script easier to manage.
+# They are passed to sed as "A NAME B NAME C VALUE D", where NAME
+# is the cpp macro being defined and VALUE is the value it is being given.
+# Each defining turns into a single global substitution command.
+#
+# SEDd sets the value in "#define NAME VALUE" lines.
+SEDdA='s@^\([ 	]*\)#\([ 	]*define[ 	][ 	]*\)'
+SEDdB='\([ 	][ 	]*\)[^ 	]*@\1#\2'
+SEDdC='\3'
+SEDdD='@g'
+# SEDu turns "#undef NAME" with trailing blanks into "#define NAME VALUE".
+SEDuA='s@^\([ 	]*\)#\([ 	]*\)undef\([ 	][ 	]*\)'
+SEDuB='\([ 	]\)@\1#\2define\3'
+SEDuC=' '
+SEDuD='\4@g'
+# SEDe turns "#undef NAME" without trailing blanks into "#define NAME VALUE".
+SEDeA='s@^\([ 	]*\)#\([ 	]*\)undef\([ 	][ 	]*\)'
+SEDeB='$@\1#\2define\3'
+SEDeC=' '
+SEDeD='@g'
+rm -f conftest.sed
+EOF
+# Turn off quoting long enough to insert the sed commands.
+rm -f conftest.sh
+cat > conftest.sh <<EOF
+$SEDDEFS
+EOF
+
+# Break up $SEDDEFS (now in conftest.sh) because some shells have a limit
+# on the size of here documents.
+
+# Maximum number of lines to put in a single here document.
+maxshlines=9
+
+while :
+do
+  # wc gives bogus results for an empty file on some systems.
+  lines=`grep -c . conftest.sh`
+  if test -z "$lines" || test "$lines" -eq 0; then break; fi
+  rm -f conftest.s1 conftest.s2
+  sed ${maxshlines}q conftest.sh > conftest.s1 # Like head -20.
+  sed 1,${maxshlines}d conftest.sh > conftest.s2 # Like tail +21.
+  # Write a limited-size here document to append to conftest.sed.
+  echo 'cat >> conftest.sed <<CONFEOF' >> config.status
+  cat conftest.s1 >> config.status
+  echo 'CONFEOF' >> config.status
+  rm -f conftest.s1 conftest.sh
+  mv conftest.s2 conftest.sh
+done
+rm -f conftest.sh
+
+# Now back to your regularly scheduled config.status.
+cat >> config.status <<\EOF
+# This sed command replaces #undef's with comments.  This is necessary, for
+# example, in the case of _POSIX_SOURCE, which is predefined and required
+# on some systems where configure will not decide to define it in
+# jconfig.h.
+cat >> conftest.sed <<\CONFEOF
+CONFEOF
+rm -f conftest.h
+# Break up the sed commands because old seds have small limits.
+maxsedlines=20
+cp $top_srcdir/jconfig.auto conftest.h1
+while :
+do
+  lines=`grep -c . conftest.sed`
+  if test -z "$lines" || test "$lines" -eq 0; then break; fi
+  rm -f conftest.s1 conftest.s2 conftest.h2
+  sed ${maxsedlines}q conftest.sed > conftest.s1 # Like head -20.
+  sed 1,${maxsedlines}d conftest.sed > conftest.s2 # Like tail +21.
+  sed -f conftest.s1 < conftest.h1 > conftest.h2
+  rm -f conftest.s1 conftest.h1 conftest.sed
+  mv conftest.h2 conftest.h1
+  mv conftest.s2 conftest.sed
+done
+rm -f conftest.sed conftest.h
+echo "/* $file.  Generated automatically by configure.  */" > conftest.h
+cat conftest.h1 >> conftest.h
+rm -f conftest.h1
+if cmp -s $file conftest.h 2>/dev/null; then
+  # The file exists and we would not be changing it.
+  echo "$file is unchanged"
+  rm -f conftest.h
+else
+  rm -f $file
+  mv conftest.h $file
+fi
+fi; done
+
+
+
+exit 0
+EOF
+chmod +x config.status
+${CONFIG_SHELL-/bin/sh} config.status
+
diff --git a/djpeg.1 b/djpeg.1
index 7f45074..08f80c5 100644
--- a/djpeg.1
+++ b/djpeg.1
@@ -1,43 +1,10 @@
-.TH DJPEG 1 "17 February 1993"
+.TH DJPEG 1 "28 August 1994"
 .SH NAME
 djpeg \- decompress a JPEG file to an image file
 .SH SYNOPSIS
 .B djpeg
 [
-.BI \-colors " N"
-]
-[
-.B \-gif
-]
-[
-.B \-pnm
-]
-[
-.B \-rle
-]
-[
-.B \-targa
-]
-[
-.B \-blocksmooth
-]
-[
-.B \-grayscale
-]
-[
-.BI \-maxmemory " N"
-]
-[
-.B \-nodither
-]
-[
-.B \-onepass
-]
-[
-.B \-verbose
-]
-[
-.B \-debug
+.I options
 ]
 [
 .I filename
@@ -47,9 +14,9 @@
 .LP
 .B djpeg
 decompresses the named JPEG file, or the standard input if no file is named,
-and produces an image file on the standard output.  PBMPLUS (PPM/PGM), GIF,
-Targa, or RLE (Utah Raster Toolkit) output format can be selected.  (RLE is
-supported only if the URT library is available.)
+and produces an image file on the standard output.  PBMPLUS (PPM/PGM), BMP,
+GIF, Targa, or RLE (Utah Raster Toolkit) output format can be selected.
+(RLE is supported only if the URT library is available.)
 .SH OPTIONS
 All switch names may be abbreviated; for example,
 .B \-grayscale
@@ -82,11 +49,47 @@
 .B \-quantize
 is provided only for backwards compatibility.
 .TP
+.B \-fast
+Select recommended processing options for fast, low quality output.  (The
+default options are chosen for highest quality output.)  Currently, this is
+equivalent to \fB\-dct fast \-nosmooth \-onepass \-dither ordered\fR.
+.TP
+.B \-grayscale
+Force gray-scale output even if JPEG file is color.  Useful for viewing on
+monochrome displays; also,
+.B djpeg
+runs noticeably faster in this mode.
+.TP
+.BI \-scale " M/N"
+Scale the output image by a factor M/N.  Currently the scale factor must be
+1/1, 1/2, 1/4, or 1/8.  Scaling is handy if the image is larger than your
+screen; also,
+.B djpeg
+runs much faster when scaling down the output.
+.TP
+.B \-bmp
+Select BMP output format (Windows flavor).  8-bit colormapped format is
+emitted if
+.B \-colors
+or
+.B \-grayscale
+is specified, or if the JPEG file is gray-scale; otherwise, 24-bit full-color
+format is emitted.
+.TP
 .B \-gif
 Select GIF output format.  Since GIF does not support more than 256 colors,
 .B \-colors 256
 is assumed (unless you specify a smaller number of colors).
 .TP
+.B \-os2
+Select BMP output format (OS/2 1.x flavor).  8-bit colormapped format is
+emitted if
+.B \-colors
+or
+.B \-grayscale
+is specified, or if the JPEG file is gray-scale; otherwise, 24-bit full-color
+format is emitted.
+.TP
 .B \-pnm
 Select PBMPLUS (PPM/PGM) output format (this is the default format).
 PGM is emitted if the JPEG file is gray-scale or if
@@ -106,32 +109,48 @@
 .PP
 Switches for advanced users:
 .TP
-.B \-blocksmooth
-Perform cross-block smoothing.  This is slow, quite memory-intensive, and only
-seems to improve the image at very low quality settings (\fB\-quality\fR 10 to
-20 or so).  At normal quality settings it may make the image worse.
+.B \-dct int
+Use integer DCT method (default).
 .TP
-.B \-grayscale
-Force gray-scale output even if JPEG file is color.
-Useful for viewing on monochrome displays.
+.B \-dct fast
+Use fast integer DCT (less accurate).
 .TP
-.BI \-maxmemory " N"
-Set limit for amount of memory to use in processing large images.  Value is
-in thousands of bytes, or millions of bytes if "M" is attached to the
-number.  For example,
-.B \-max 4m
-selects 4000000 bytes.  If more space is needed, temporary files will be used.
+.B \-dct float
+Use floating-point DCT method.
+The floating-point method is the most accurate, but will be the slowest unless
+your machine has very fast floating-point hardware.  Also note that results of
+the floating-point method may vary slightly across machines, while the integer
+methods should give the same results everywhere.  The fast integer method is
+much less accurate than the other two.
 .TP
-.B \-nodither
-Do not use dithering in color quantization.  By default, Floyd-Steinberg
-dithering is applied when quantizing colors, but on some images dithering may
-result in objectionable "graininess".  If that happens, you can turn off
-dithering with
-.BR \-nodither .
-.B \-nodither
-is ignored unless you also say
+.B \-dither fs
+Use Floyd-Steinberg dithering in color quantization.
+.TP
+.B \-dither ordered
+Use ordered dithering in color quantization.
+.TP
+.B \-dither none
+Do not use dithering in color quantization.
+By default, Floyd-Steinberg dithering is applied when quantizing colors; this
+is slow but usually produces the best results.  Ordered dither is a compromise
+between speed and quality; no dithering is fast but usually looks awful.  Note
+that these switches have no effect unless color quantization is being done.
+Ordered dither is only available in
+.B \-onepass
+mode.
+.TP
+.BI \-map " file"
+Quantize to the colors used in the specified image file.  This is useful for
+producing multiple files with identical color maps, or for forcing a
+predefined set of colors to be used.  The
+.I file
+must be a GIF or PPM file. This option overrides
 .B \-colors
-.IR N .
+and
+.BR \-onepass .
+.TP
+.B \-nosmooth
+Use a faster, lower-quality upsampling routine.
 .TP
 .B \-onepass
 Use one-pass instead of two-pass color quantization.  The one-pass method is
@@ -143,6 +162,16 @@
 Also, the one-pass method is always used for gray-scale output (the two-pass
 method is no improvement then).
 .TP
+.BI \-maxmemory " N"
+Set limit for amount of memory to use in processing large images.  Value is
+in thousands of bytes, or millions of bytes if "M" is attached to the
+number.  For example,
+.B \-max 4m
+selects 4000000 bytes.  If more space is needed, temporary files will be used.
+.TP
+.BI \-outfile " name"
+Send output image to the named file, not to standard output.
+.TP
 .B \-verbose
 Enable debug printout.  More
 .BR \-v 's
@@ -160,6 +189,33 @@
 .I foo.jpg
 .B >
 .I foo.gif
+.SH HINTS
+To get a quick preview of an image, use the
+.B \-grayscale
+and/or
+.B \-scale
+switches.
+.B \-grayscale \-scale 1/8
+is the fastest case.
+.PP
+Several options are available that trade off image quality to gain speed.
+.B \-fast
+turns on the recommended settings.
+.PP
+.B \-dct fast
+and/or
+.B \-nosmooth
+gain speed at a small sacrifice in quality.
+When producing a color-quantized image,
+.B \-onepass \-dither ordered
+is fast but much lower quality than the default behavior.
+.B \-dither none
+may give acceptable results in two-pass mode, but is seldom tolerable in
+one-pass mode.
+.PP
+If you are fortunate enough to have very fast floating point hardware,
+.B \-dct float
+may be even faster than \fB\-dct fast\fR.
 .SH ENVIRONMENT
 .TP
 .B JPEGMEM
@@ -172,7 +228,9 @@
 itself is overridden by an explicit
 .BR \-maxmemory .
 .SH SEE ALSO
-.BR cjpeg (1)
+.BR cjpeg (1),
+.BR rdjpgcom (1),
+.BR wrjpgcom (1)
 .br
 .BR ppm (5),
 .BR pgm (5)
diff --git a/djpeg.c b/djpeg.c
new file mode 100644
index 0000000..0df3755
--- /dev/null
+++ b/djpeg.c
@@ -0,0 +1,736 @@
+/*
+ * djpeg.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a command-line user interface for the JPEG decompressor.
+ * It should work on any system with Unix- or MS-DOS-style command lines.
+ *
+ * Two different command line styles are permitted, depending on the
+ * compile-time switch TWO_FILE_COMMANDLINE:
+ *	djpeg [options]  inputfile outputfile
+ *	djpeg [options]  [inputfile]
+ * In the second style, output is always to standard output, which you'd
+ * normally redirect to a file or pipe to some other program.  Input is
+ * either from a named file or from standard input (typically redirected).
+ * The second style is convenient on Unix but is unhelpful on systems that
+ * don't support pipes.  Also, you MUST use the first style if your system
+ * doesn't do binary I/O to stdin/stdout.
+ * To simplify script writing, the "-outfile" switch is provided.  The syntax
+ *	djpeg [options]  -outfile outputfile  inputfile
+ * works regardless of which command line style is used.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+#define JMAKE_MSG_TABLE
+#include "cderror.h"		/* create message string table */
+#include "jversion.h"		/* for version message */
+
+#include <ctype.h>		/* to declare isupper(),tolower(),isprint() */
+#ifdef NEED_SIGNAL_CATCHER
+#include <signal.h>		/* to declare signal() */
+#endif
+#ifdef USE_SETMODE
+#include <fcntl.h>		/* to declare setmode()'s parameter macros */
+/* If you have setmode() but not <io.h>, just delete this line: */
+#include <io.h>			/* to declare setmode() */
+#endif
+
+#ifdef USE_CCOMMAND		/* command-line reader for Macintosh */
+#ifdef __MWERKS__
+#include <SIOUX.h>              /* Metrowerks declares it here */
+#endif
+#ifdef THINK_C
+#include <console.h>		/* Think declares it here */
+#endif
+#endif
+
+#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
+#define READ_BINARY	"r"
+#define WRITE_BINARY	"w"
+#else
+#define READ_BINARY	"rb"
+#define WRITE_BINARY	"wb"
+#endif
+
+#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
+#define EXIT_FAILURE  1
+#endif
+#ifndef EXIT_SUCCESS
+#ifdef VMS
+#define EXIT_SUCCESS  1		/* VMS is very nonstandard */
+#else
+#define EXIT_SUCCESS  0
+#endif
+#endif
+#ifndef EXIT_WARNING
+#ifdef VMS
+#define EXIT_WARNING  1		/* VMS is very nonstandard */
+#else
+#define EXIT_WARNING  2
+#endif
+#endif
+
+
+/*
+ * This list defines the known output image formats
+ * (not all of which need be supported by a given version).
+ * You can change the default output format by defining DEFAULT_FMT;
+ * indeed, you had better do so if you undefine PPM_SUPPORTED.
+ */
+
+typedef enum {
+	FMT_BMP,		/* BMP format (Windows flavor) */
+	FMT_GIF,		/* GIF format */
+	FMT_OS2,		/* BMP format (OS/2 flavor) */
+	FMT_PPM,		/* PPM/PGM (PBMPLUS formats) */
+	FMT_RLE,		/* RLE format */
+	FMT_TARGA,		/* Targa format */
+	FMT_TIFF		/* TIFF format */
+} IMAGE_FORMATS;
+
+#ifndef DEFAULT_FMT		/* so can override from CFLAGS in Makefile */
+#define DEFAULT_FMT	FMT_PPM
+#endif
+
+static IMAGE_FORMATS requested_fmt;
+
+
+/*
+ * Signal catcher to ensure that temporary files are removed before aborting.
+ * NB: for Amiga Manx C this is actually a global routine named _abort();
+ * we put "#define signal_catcher _abort" in jconfig.h.  Talk about bogus...
+ */
+
+#ifdef NEED_SIGNAL_CATCHER
+
+static j_common_ptr sig_cinfo;
+
+GLOBAL void
+signal_catcher (int signum)
+{
+  if (sig_cinfo != NULL) {
+    if (sig_cinfo->err != NULL) /* turn off trace output */
+      sig_cinfo->err->trace_level = 0;
+    jpeg_destroy(sig_cinfo);	/* clean up memory allocation & temp files */
+  }
+  exit(EXIT_FAILURE);
+}
+
+#endif
+
+
+/*
+ * Optional routine to display a percent-done figure on stderr.
+ */
+
+#ifdef PROGRESS_REPORT
+
+METHODDEF void
+progress_monitor (j_common_ptr cinfo)
+{
+  cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
+  int total_passes = prog->pub.total_passes + prog->total_extra_passes;
+  int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
+
+  if (percent_done != prog->percent_done) {
+    prog->percent_done = percent_done;
+    if (total_passes > 1) {
+      fprintf(stderr, "\rPass %d/%d: %3d%% ",
+	      prog->pub.completed_passes + prog->completed_extra_passes + 1,
+	      total_passes, percent_done);
+    } else {
+      fprintf(stderr, "\r %3d%% ", percent_done);
+    }
+    fflush(stderr);
+  }
+}
+
+#endif
+
+
+/*
+ * Argument-parsing code.
+ * The switch parser is designed to be useful with DOS-style command line
+ * syntax, ie, intermixed switches and file names, where only the switches
+ * to the left of a given file name affect processing of that file.
+ * The main program in this file doesn't actually use this capability...
+ */
+
+
+static const char * progname;	/* program name for error messages */
+static char * outfilename;	/* for -outfile switch */
+
+
+LOCAL void
+usage (void)
+/* complain about bad command line */
+{
+  fprintf(stderr, "usage: %s [switches] ", progname);
+#ifdef TWO_FILE_COMMANDLINE
+  fprintf(stderr, "inputfile outputfile\n");
+#else
+  fprintf(stderr, "[inputfile]\n");
+#endif
+
+  fprintf(stderr, "Switches (names may be abbreviated):\n");
+  fprintf(stderr, "  -colors N      Reduce image to no more than N colors\n");
+  fprintf(stderr, "  -fast          Fast, low-quality processing\n");
+  fprintf(stderr, "  -grayscale     Force grayscale output\n");
+#ifdef IDCT_SCALING_SUPPORTED
+  fprintf(stderr, "  -scale M/N     Scale output image by fraction M/N, eg, 1/8\n");
+#endif
+#ifdef BMP_SUPPORTED
+  fprintf(stderr, "  -bmp           Select BMP output format (Windows style)%s\n",
+	  (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));
+#endif
+#ifdef GIF_SUPPORTED
+  fprintf(stderr, "  -gif           Select GIF output format%s\n",
+	  (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));
+#endif
+#ifdef BMP_SUPPORTED
+  fprintf(stderr, "  -os2           Select BMP output format (OS/2 style)%s\n",
+	  (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));
+#endif
+#ifdef PPM_SUPPORTED
+  fprintf(stderr, "  -pnm           Select PBMPLUS (PPM/PGM) output format%s\n",
+	  (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));
+#endif
+#ifdef RLE_SUPPORTED
+  fprintf(stderr, "  -rle           Select Utah RLE output format%s\n",
+	  (DEFAULT_FMT == FMT_RLE ? " (default)" : ""));
+#endif
+#ifdef TARGA_SUPPORTED
+  fprintf(stderr, "  -targa         Select Targa output format%s\n",
+	  (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));
+#endif
+  fprintf(stderr, "Switches for advanced users:\n");
+#ifdef DCT_ISLOW_SUPPORTED
+  fprintf(stderr, "  -dct int       Use integer DCT method%s\n",
+	  (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+  fprintf(stderr, "  -dct fast      Use fast integer DCT (less accurate)%s\n",
+	  (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+  fprintf(stderr, "  -dct float     Use floating-point DCT method%s\n",
+	  (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
+#endif
+  fprintf(stderr, "  -dither fs     Use F-S dithering (default)\n");
+  fprintf(stderr, "  -dither none   Don't use dithering in quantization\n");
+  fprintf(stderr, "  -dither ordered  Use ordered dither (medium speed, quality)\n");
+#ifdef QUANT_2PASS_SUPPORTED
+  fprintf(stderr, "  -map FILE      Map to colors used in named image file\n");
+#endif
+  fprintf(stderr, "  -nosmooth      Don't use high-quality upsampling\n");
+#ifdef QUANT_1PASS_SUPPORTED
+  fprintf(stderr, "  -onepass       Use 1-pass quantization (fast, low quality)\n");
+#endif
+  fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
+  fprintf(stderr, "  -outfile name  Specify name for output file\n");
+  fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
+  exit(EXIT_FAILURE);
+}
+
+
+LOCAL boolean
+keymatch (char * arg, const char * keyword, int minchars)
+/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
+/* keyword is the constant keyword (must be lower case already), */
+/* minchars is length of minimum legal abbreviation. */
+{
+  register int ca, ck;
+  register int nmatched = 0;
+
+  while ((ca = *arg++) != '\0') {
+    if ((ck = *keyword++) == '\0')
+      return FALSE;		/* arg longer than keyword, no good */
+    if (isupper(ca))		/* force arg to lcase (assume ck is already) */
+      ca = tolower(ca);
+    if (ca != ck)
+      return FALSE;		/* no good */
+    nmatched++;			/* count matched characters */
+  }
+  /* reached end of argument; fail if it's too short for unique abbrev */
+  if (nmatched < minchars)
+    return FALSE;
+  return TRUE;			/* A-OK */
+}
+
+
+LOCAL int
+parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
+		int last_file_arg_seen, boolean for_real)
+/* Parse optional switches.
+ * Returns argv[] index of first file-name argument (== argc if none).
+ * Any file names with indexes <= last_file_arg_seen are ignored;
+ * they have presumably been processed in a previous iteration.
+ * (Pass 0 for last_file_arg_seen on the first or only iteration.)
+ * for_real is FALSE on the first (dummy) pass; we may skip any expensive
+ * processing.
+ */
+{
+  int argn;
+  char * arg;
+
+  /* Set up default JPEG parameters. */
+  requested_fmt = DEFAULT_FMT;	/* set default output file format */
+  outfilename = NULL;
+  cinfo->err->trace_level = 0;
+
+  /* Scan command line options, adjust parameters */
+
+  for (argn = 1; argn < argc; argn++) {
+    arg = argv[argn];
+    if (*arg != '-') {
+      /* Not a switch, must be a file name argument */
+      if (argn <= last_file_arg_seen) {
+	outfilename = NULL;	/* -outfile applies to just one input file */
+	continue;		/* ignore this name if previously processed */
+      }
+      break;			/* else done parsing switches */
+    }
+    arg++;			/* advance past switch marker character */
+
+    if (keymatch(arg, "bmp", 1)) {
+      /* BMP output format. */
+      requested_fmt = FMT_BMP;
+
+    } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
+	       keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
+      /* Do color quantization. */
+      int val;
+
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (sscanf(argv[argn], "%d", &val) != 1)
+	usage();
+      cinfo->desired_number_of_colors = val;
+      cinfo->quantize_colors = TRUE;
+
+    } else if (keymatch(arg, "dct", 2)) {
+      /* Select IDCT algorithm. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (keymatch(argv[argn], "int", 1)) {
+	cinfo->dct_method = JDCT_ISLOW;
+      } else if (keymatch(argv[argn], "fast", 2)) {
+	cinfo->dct_method = JDCT_IFAST;
+      } else if (keymatch(argv[argn], "float", 2)) {
+	cinfo->dct_method = JDCT_FLOAT;
+      } else
+	usage();
+
+    } else if (keymatch(arg, "dither", 2)) {
+      /* Select dithering algorithm. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (keymatch(argv[argn], "fs", 2)) {
+	cinfo->dither_mode = JDITHER_FS;
+      } else if (keymatch(argv[argn], "none", 2)) {
+	cinfo->dither_mode = JDITHER_NONE;
+      } else if (keymatch(argv[argn], "ordered", 2)) {
+	cinfo->dither_mode = JDITHER_ORDERED;
+      } else
+	usage();
+
+    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
+      /* Enable debug printouts. */
+      /* On first -d, print version identification */
+      static boolean printed_version = FALSE;
+
+      if (! printed_version) {
+	fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
+		JVERSION, JCOPYRIGHT);
+	printed_version = TRUE;
+      }
+      cinfo->err->trace_level++;
+
+    } else if (keymatch(arg, "fast", 1)) {
+      /* Select recommended processing options for quick-and-dirty output. */
+      cinfo->two_pass_quantize = FALSE;
+      cinfo->dither_mode = JDITHER_ORDERED;
+      if (! cinfo->quantize_colors) /* don't override an earlier -colors */
+	cinfo->desired_number_of_colors = 216;
+      cinfo->dct_method = JDCT_FASTEST;
+      cinfo->do_fancy_upsampling = FALSE;
+
+    } else if (keymatch(arg, "gif", 1)) {
+      /* GIF output format. */
+      requested_fmt = FMT_GIF;
+
+    } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
+      /* Force monochrome output. */
+      cinfo->out_color_space = JCS_GRAYSCALE;
+
+    } else if (keymatch(arg, "map", 3)) {
+      /* Quantize to a color map taken from an input file. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (for_real) {		/* too expensive to do twice! */
+#ifdef QUANT_2PASS_SUPPORTED	/* otherwise can't quantize to supplied map */
+	FILE * mapfile;
+
+	if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
+	  fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
+	  exit(EXIT_FAILURE);
+	}
+	read_color_map(cinfo, mapfile);
+	fclose(mapfile);
+	cinfo->quantize_colors = TRUE;
+#else
+	ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+      }
+
+    } else if (keymatch(arg, "maxmemory", 3)) {
+      /* Maximum memory in Kb (or Mb with 'm'). */
+      long lval;
+      char ch = 'x';
+
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
+	usage();
+      if (ch == 'm' || ch == 'M')
+	lval *= 1000L;
+      cinfo->mem->max_memory_to_use = lval * 1000L;
+
+    } else if (keymatch(arg, "nosmooth", 3)) {
+      /* Suppress fancy upsampling */
+      cinfo->do_fancy_upsampling = FALSE;
+
+    } else if (keymatch(arg, "onepass", 3)) {
+      /* Use fast one-pass quantization. */
+      cinfo->two_pass_quantize = FALSE;
+
+    } else if (keymatch(arg, "os2", 3)) {
+      /* BMP output format (OS/2 flavor). */
+      requested_fmt = FMT_OS2;
+
+    } else if (keymatch(arg, "outfile", 4)) {
+      /* Set output file name. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      outfilename = argv[argn];	/* save it away for later use */
+
+    } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
+      /* PPM/PGM output format. */
+      requested_fmt = FMT_PPM;
+
+    } else if (keymatch(arg, "rle", 1)) {
+      /* RLE output format. */
+      requested_fmt = FMT_RLE;
+
+    } else if (keymatch(arg, "scale", 1)) {
+      /* Scale the output image by a fraction M/N. */
+      if (++argn >= argc)	/* advance to next argument */
+	usage();
+      if (sscanf(argv[argn], "%d/%d",
+		 &cinfo->scale_num, &cinfo->scale_denom) != 2)
+	usage();
+
+    } else if (keymatch(arg, "targa", 1)) {
+      /* Targa output format. */
+      requested_fmt = FMT_TARGA;
+
+    } else {
+      usage();			/* bogus switch */
+    }
+  }
+
+  return argn;			/* return index of next arg (file name) */
+}
+
+
+/*
+ * Marker processor for COM markers.
+ * This replaces the library's built-in processor, which just skips the marker.
+ * We want to print out the marker as text, if possible.
+ * Note this code relies on a non-suspending data source.
+ */
+
+LOCAL unsigned int
+jpeg_getc (j_decompress_ptr cinfo)
+/* Read next byte */
+{
+  struct jpeg_source_mgr * datasrc = cinfo->src;
+
+  if (datasrc->bytes_in_buffer == 0) {
+    if (! (*datasrc->fill_input_buffer) (cinfo))
+      ERREXIT(cinfo, JERR_CANT_SUSPEND);
+  }
+  datasrc->bytes_in_buffer--;
+  return GETJOCTET(*datasrc->next_input_byte++);
+}
+
+
+METHODDEF boolean
+COM_handler (j_decompress_ptr cinfo)
+{
+  boolean traceit = (cinfo->err->trace_level >= 1);
+  INT32 length;
+  unsigned int ch;
+  unsigned int lastch = 0;
+
+  length = jpeg_getc(cinfo) << 8;
+  length += jpeg_getc(cinfo);
+  length -= 2;			/* discount the length word itself */
+
+  if (traceit)
+    fprintf(stderr, "Comment, length %ld:\n", (long) length);
+
+  while (--length >= 0) {
+    ch = jpeg_getc(cinfo);
+    if (traceit) {
+      /* Emit the character in a readable form.
+       * Nonprintables are converted to \nnn form,
+       * while \ is converted to \\.
+       * Newlines in CR, CR/LF, or LF form will be printed as one newline.
+       */
+      if (ch == '\r') {
+	fprintf(stderr, "\n");
+      } else if (ch == '\n') {
+	if (lastch != '\r')
+	  fprintf(stderr, "\n");
+      } else if (ch == '\\') {
+	fprintf(stderr, "\\\\");
+      } else if (isprint(ch)) {
+	putc(ch, stderr);
+      } else {
+	fprintf(stderr, "\\%03o", ch);
+      }
+      lastch = ch;
+    }
+  }
+
+  if (traceit)
+    fprintf(stderr, "\n");
+
+  return TRUE;
+}
+
+
+/*
+ * The main program.
+ */
+
+GLOBAL int
+main (int argc, char **argv)
+{
+  struct jpeg_decompress_struct cinfo;
+  struct jpeg_error_mgr jerr;
+#ifdef PROGRESS_REPORT
+  struct cdjpeg_progress_mgr progress;
+#endif
+  int file_index;
+  djpeg_dest_ptr dest_mgr = NULL;
+  FILE * input_file;
+  FILE * output_file;
+  JDIMENSION num_scanlines;
+
+  /* On Mac, fetch a command line. */
+#ifdef USE_CCOMMAND
+  argc = ccommand(&argv);
+#endif
+
+  progname = argv[0];
+  if (progname == NULL || progname[0] == 0)
+    progname = "djpeg";		/* in case C library doesn't provide it */
+
+  /* Initialize the JPEG decompression object with default error handling. */
+  cinfo.err = jpeg_std_error(&jerr);
+  jpeg_create_decompress(&cinfo);
+  /* Add some application-specific error messages (from cderror.h) */
+  jerr.addon_message_table = addon_message_table;
+  jerr.first_addon_message = JMSG_FIRSTADDONCODE;
+  jerr.last_addon_message = JMSG_LASTADDONCODE;
+  /* Insert custom COM marker processor. */
+  jpeg_set_marker_processor(&cinfo, JPEG_COM, COM_handler);
+
+  /* Now safe to enable signal catcher. */
+#ifdef NEED_SIGNAL_CATCHER
+  sig_cinfo = (j_common_ptr) &cinfo;
+  signal(SIGINT, signal_catcher);
+#ifdef SIGTERM			/* not all systems have SIGTERM */
+  signal(SIGTERM, signal_catcher);
+#endif
+#endif
+
+  /* Scan command line to find file names. */
+  /* It is convenient to use just one switch-parsing routine, but the switch
+   * values read here are ignored; we will rescan the switches after opening
+   * the input file.
+   * (Exception: tracing level set here controls verbosity for COM markers
+   * found during jpeg_read_header...)
+   */
+
+  file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
+
+#ifdef TWO_FILE_COMMANDLINE
+  /* Must have either -outfile switch or explicit output file name */
+  if (outfilename == NULL) {
+    if (file_index != argc-2) {
+      fprintf(stderr, "%s: must name one input and one output file\n",
+	      progname);
+      usage();
+    }
+    outfilename = argv[file_index+1];
+  } else {
+    if (file_index != argc-1) {
+      fprintf(stderr, "%s: must name one input and one output file\n",
+	      progname);
+      usage();
+    }
+  }
+#else
+  /* Unix style: expect zero or one file name */
+  if (file_index < argc-1) {
+    fprintf(stderr, "%s: only one input file\n", progname);
+    usage();
+  }
+#endif /* TWO_FILE_COMMANDLINE */
+
+  /* Open the input file. */
+  if (file_index < argc) {
+    if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
+      exit(EXIT_FAILURE);
+    }
+  } else {
+    /* default input file is stdin */
+#ifdef USE_SETMODE		/* need to hack file mode? */
+    setmode(fileno(stdin), O_BINARY);
+#endif
+#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
+    if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open stdin\n", progname);
+      exit(EXIT_FAILURE);
+    }
+#else
+    input_file = stdin;
+#endif
+  }
+
+  /* Open the output file. */
+  if (outfilename != NULL) {
+    if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
+      exit(EXIT_FAILURE);
+    }
+  } else {
+    /* default output file is stdout */
+#ifdef USE_SETMODE		/* need to hack file mode? */
+    setmode(fileno(stdout), O_BINARY);
+#endif
+#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
+    if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open stdout\n", progname);
+      exit(EXIT_FAILURE);
+    }
+#else
+    output_file = stdout;
+#endif
+  }
+
+#ifdef PROGRESS_REPORT
+  /* Enable progress display, unless trace output is on */
+  if (jerr.trace_level == 0) {
+    progress.pub.progress_monitor = progress_monitor;
+    progress.completed_extra_passes = 0;
+    progress.total_extra_passes = 0;
+    progress.percent_done = -1;
+    cinfo.progress = &progress.pub;
+  }
+#endif
+
+  /* Specify data source for decompression */
+  jpeg_stdio_src(&cinfo, input_file);
+
+  /* Read file header, set default decompression parameters */
+  (void) jpeg_read_header(&cinfo, TRUE);
+
+  /* Adjust default decompression parameters by re-parsing the options */
+  file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
+
+  /* Initialize the output module now to let it override any crucial
+   * option settings (for instance, GIF wants to force color quantization).
+   */
+  switch (requested_fmt) {
+#ifdef BMP_SUPPORTED
+  case FMT_BMP:
+    dest_mgr = jinit_write_bmp(&cinfo, FALSE);
+    break;
+  case FMT_OS2:
+    dest_mgr = jinit_write_bmp(&cinfo, TRUE);
+    break;
+#endif
+#ifdef GIF_SUPPORTED
+  case FMT_GIF:
+    dest_mgr = jinit_write_gif(&cinfo);
+    break;
+#endif
+#ifdef PPM_SUPPORTED
+  case FMT_PPM:
+    dest_mgr = jinit_write_ppm(&cinfo);
+    break;
+#endif
+#ifdef RLE_SUPPORTED
+  case FMT_RLE:
+    dest_mgr = jinit_write_rle(&cinfo);
+    break;
+#endif
+#ifdef TARGA_SUPPORTED
+  case FMT_TARGA:
+    dest_mgr = jinit_write_targa(&cinfo);
+    break;
+#endif
+  default:
+    ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
+    break;
+  }
+  dest_mgr->output_file = output_file;
+
+  /* Start decompressor */
+  jpeg_start_decompress(&cinfo);
+
+  /* Write output file header */
+  (*dest_mgr->start_output) (&cinfo, dest_mgr);
+
+  /* Process data */
+  while (cinfo.output_scanline < cinfo.output_height) {
+    num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
+					dest_mgr->buffer_height);
+    (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
+  }
+
+#ifdef PROGRESS_REPORT
+  /* Hack: count final pass as done in case finish_output does an extra pass.
+   * The library won't have updated completed_passes.
+   */
+  progress.pub.completed_passes = progress.pub.total_passes;
+#endif
+
+  /* Finish decompression and release memory.
+   * I must do it in this order because output module has allocated memory
+   * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
+   */
+  (*dest_mgr->finish_output) (&cinfo, dest_mgr);
+  jpeg_finish_decompress(&cinfo);
+  jpeg_destroy_decompress(&cinfo);
+
+#ifdef PROGRESS_REPORT
+  /* Clear away progress display */
+  if (jerr.trace_level == 0) {
+    fprintf(stderr, "\r                \r");
+    fflush(stderr);
+  }
+#endif
+
+  /* All done. */
+  exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
+  return 0;			/* suppress no-return-value warnings */
+}
diff --git a/example.c b/example.c
index 666dd6f..cb8c64a 100644
--- a/example.c
+++ b/example.c
@@ -1,31 +1,29 @@
 /*
  * example.c
  *
- * This file is not actually part of the JPEG software.  Rather, it provides
- * a skeleton that may be useful for constructing applications that use the
- * JPEG software as subroutines.  This code will NOT do anything useful as is.
+ * This file illustrates how to use the IJG code as a subroutine library
+ * to read or write JPEG image files.  You should look at this code in
+ * conjunction with the documentation file libjpeg.doc.
  *
- * This file illustrates how to use the JPEG code as a subroutine library
- * to read or write JPEG image files.  We assume here that you are not
- * merely interested in converting the image to yet another image file format
- * (if you are, you should be adding another I/O module to cjpeg/djpeg, not
- * constructing a new application).  Instead, we show how to pass the
- * decompressed image data into or out of routines that you provide.  For
- * example, a viewer program might use the JPEG decompressor together with
- * routines that write the decompressed image directly to a display.
+ * This code will not do anything useful as-is, but it may be helpful as a
+ * skeleton for constructing routines that call the JPEG library.  
  *
  * We present these routines in the same coding style used in the JPEG code
  * (ANSI function definitions, etc); but you are of course free to code your
  * routines in a different style if you prefer.
  */
 
+#include <stdio.h>
+
 /*
- * Include file for declaring JPEG data structures.
- * This file also includes some system headers like <stdio.h>;
- * if you prefer, you can include "jconfig.h" and "jpegdata.h" instead.
+ * Include file for users of JPEG library.
+ * You will need to have included system headers that define at least
+ * the typedefs FILE and size_t before you can include jpeglib.h.
+ * (stdio.h is sufficient on ANSI-conforming systems.)
+ * You may also wish to include "jerror.h".
  */
 
-#include "jinclude.h"
+#include "jpeglib.h"
 
 /*
  * <setjmp.h> is used for the optional error recovery mechanism shown in
@@ -45,587 +43,375 @@
 
 
 /*
- * To supply the image data for compression, you must define three routines
- * input_init, get_input_row, and input_term.  These routines will be called
- * from the JPEG compressor via function pointer values that you store in the
- * cinfo data structure; hence they need not be globally visible and the exact
- * names don't matter.  (In fact, the "METHODDEF" macro expands to "static" if
- * you use the unmodified JPEG include files.)
+ * IMAGE DATA FORMATS:
  *
- * The input file reading modules (jrdppm.c, jrdgif.c, jrdtarga.c, etc) may be
- * useful examples of what these routines should actually do, although each of
- * them is encrusted with a lot of specialized code for its own file format.
+ * The standard input image format is a rectangular array of pixels, with
+ * each pixel having the same number of "component" values (color channels).
+ * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
+ * If you are working with color data, then the color values for each pixel
+ * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
+ * RGB color.
+ *
+ * For this example, we'll assume that this data structure matches the way
+ * our application has stored the image in memory, so we can just pass a
+ * pointer to our image buffer.  In particular, let's say that the image is
+ * RGB color and is described by:
  */
 
-
-METHODDEF void
-input_init (compress_info_ptr cinfo)
-/* Initialize for input; return image size and component data. */
-{
-  /* This routine must return five pieces of information about the incoming
-   * image, and must do any setup needed for the get_input_row routine.
-   * The image information is returned in fields of the cinfo struct.
-   * (If you don't care about modularity, you could initialize these fields
-   * in the main JPEG calling routine, and make this routine be a no-op.)
-   * We show some example values here.
-   */
-  cinfo->image_width = 640;		/* width in pixels */
-  cinfo->image_height = 480;		/* height in pixels */
-  /* JPEG views an image as being a rectangular array of pixels, with each
-   * pixel having the same number of "component" values (color channels).
-   * You must specify how many components there are and the colorspace
-   * interpretation of the components.  Most applications will use RGB data or
-   * grayscale data.  If you want to use something else, you'll need to study
-   * and perhaps modify jcdeflts.c, jccolor.c, and jdcolor.c.
-   */
-  cinfo->input_components = 3;		/* or 1 for grayscale */
-  cinfo->in_color_space = CS_RGB;	/* or CS_GRAYSCALE for grayscale */
-  cinfo->data_precision = 8;		/* bits per pixel component value */
-  /* In the current JPEG software, data_precision must be set equal to
-   * BITS_IN_JSAMPLE, which is 8 unless you twiddle jconfig.h.  Future
-   * versions might allow you to say either 8 or 12 if compiled with
-   * 12-bit JSAMPLEs, or up to 16 in lossless mode.  In any case,
-   * it is up to you to scale incoming pixel values to the range
-   *   0 .. (1<<data_precision)-1.
-   * If your image data format is fixed at a byte per component,
-   * then saying "8" is probably the best long-term solution.
-   */
-}
+extern JSAMPLE * image_buffer;	/* Points to large array of R,G,B-order data */
+extern int image_height;	/* Number of rows in image */
+extern int image_width;		/* Number of columns in image */
 
 
 /*
- * This function is called repeatedly and must supply the next row of pixels
- * on each call.  The rows MUST be returned in top-to-bottom order if you want
- * your JPEG files to be compatible with everyone else's.  (If you cannot
- * readily read your data in that order, you'll need an intermediate array to
- * hold the image.  See jrdtarga.c or jrdrle.c for examples of handling
- * bottom-to-top source data using the JPEG code's portable mechanisms.)
- * The data is to be returned into a 2-D array of JSAMPLEs, indexed as
- *		JSAMPLE pixel_row[component][column]
- * where component runs from 0 to cinfo->input_components-1, and column runs
- * from 0 to cinfo->image_width-1 (column 0 is left edge of image).  Note that
- * this is actually an array of pointers to arrays rather than a true 2D array,
- * since C does not support variable-size multidimensional arrays.
- * JSAMPLE is typically typedef'd as "unsigned char".
- */
-
-
-METHODDEF void
-get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* Read next row of pixels into pixel_row[][] */
-{
-  /* This example shows how you might read RGB data (3 components)
-   * from an input file in which the data is stored 3 bytes per pixel
-   * in left-to-right, top-to-bottom order.
-   */
-  register FILE * infile = cinfo->input_file;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  for (col = 0; col < cinfo->image_width; col++) {
-    *ptr0++ = (JSAMPLE) getc(infile); /* red */
-    *ptr1++ = (JSAMPLE) getc(infile); /* green */
-    *ptr2++ = (JSAMPLE) getc(infile); /* blue */
-  }
-}
-
-
-METHODDEF void
-input_term (compress_info_ptr cinfo)
-/* Finish up at the end of the input */
-{
-  /* This termination routine will very often have no work to do, */
-  /* but you must provide it anyway. */
-  /* Note that the JPEG code will only call it during successful exit; */
-  /* if you want it called during error exit, you gotta do that yourself. */
-}
-
-
-/*
- * That's it for the routines that deal with reading the input image data.
- * Now we have overall control and parameter selection routines.
- */
-
-
-/*
- * This routine must determine what output JPEG file format is to be written,
- * and make any other compression parameter changes that are desirable.
- * This routine gets control after the input file header has been read
- * (i.e., right after input_init has been called).  You could combine its
- * functions into input_init, or even into the main control routine, but
- * if you have several different input_init routines, it's a definite win
- * to keep this separate.  You MUST supply this routine even if it's a no-op.
- */
-
-METHODDEF void
-c_ui_method_selection (compress_info_ptr cinfo)
-{
-  /* If the input is gray scale, generate a monochrome JPEG file. */
-  if (cinfo->in_color_space == CS_GRAYSCALE)
-    j_monochrome_default(cinfo);
-  /* For now, always select JFIF output format. */
-  jselwjfif(cinfo);
-}
-
-
-/*
- * OK, here is the main function that actually causes everything to happen.
- * We assume here that the target filename is supplied by the caller of this
- * routine, and that all JPEG compression parameters can be default values.
+ * Sample routine for JPEG compression.  We assume that the target file name
+ * and a compression quality factor are passed in.
  */
 
 GLOBAL void
-write_JPEG_file (char * filename)
+write_JPEG_file (char * filename, int quality)
 {
-  /* These three structs contain JPEG parameters and working data.
-   * They must survive for the duration of parameter setup and one
-   * call to jpeg_compress; typically, making them local data in the
-   * calling routine is the best strategy.
+  /* This struct contains the JPEG compression parameters and pointers to
+   * working space (which is allocated as needed by the JPEG library).
+   * It is possible to have several such structures, representing multiple
+   * compression/decompression processes, in existence at once.  We refer
+   * to any one struct (and its associated working data) as a "JPEG object".
    */
-  struct Compress_info_struct cinfo;
-  struct Compress_methods_struct c_methods;
-  struct External_methods_struct e_methods;
+  struct jpeg_compress_struct cinfo;
+  /* This struct represents a JPEG error handler.  It is declared separately
+   * because applications often want to supply a specialized error handler
+   * (see the second half of this file for an example).  But here we just
+   * take the easy way out and use the standard error handler, which will
+   * print a message on stderr and call exit() if compression fails.
+   */
+  struct jpeg_error_mgr jerr;
+  /* More stuff */
+  FILE * outfile;		/* target file */
+  JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
+  int row_stride;		/* physical row width in image buffer */
 
-  /* Initialize the system-dependent method pointers. */
-  cinfo.methods = &c_methods;	/* links to method structs */
-  cinfo.emethods = &e_methods;
-  /* Here we use the default JPEG error handler, which will just print
-   * an error message on stderr and call exit().  See the second half of
-   * this file for an example of more graceful error recovery.
-   */
-  jselerror(&e_methods);	/* select std error/trace message routines */
-  /* Here we use the standard memory manager provided with the JPEG code.
-   * In some cases you might want to replace the memory manager, or at
-   * least the system-dependent part of it, with your own code.
-   */
-  jselmemmgr(&e_methods);	/* select std memory allocation routines */
-  /* If the compressor requires full-image buffers (for entropy-coding
-   * optimization or a noninterleaved JPEG file), it will create temporary
-   * files for anything that doesn't fit within the maximum-memory setting.
-   * (Note that temp files are NOT needed if you use the default parameters.)
-   * You can change the default maximum-memory setting by changing
-   * e_methods.max_memory_to_use after jselmemmgr returns.
-   * On some systems you may also need to set up a signal handler to
-   * ensure that temporary files are deleted if the program is interrupted.
-   * (This is most important if you are on MS-DOS and use the jmemdos.c
-   * memory manager back end; it will try to grab extended memory for
-   * temp files, and that space will NOT be freed automatically.)
-   * See jcmain.c or jdmain.c for an example signal handler.
-   */
+  /* Step 1: allocate and initialize JPEG compression object */
 
-  /* Here, set up pointers to your own routines for input data handling
-   * and post-init parameter selection.
+  /* We have to set up the error handler first, in case the initialization
+   * step fails.  (Unlikely, but it could happen if you are out of memory.)
+   * This routine fills in the contents of struct jerr, and returns jerr's
+   * address which we place into the link field in cinfo.
    */
-  c_methods.input_init = input_init;
-  c_methods.get_input_row = get_input_row;
-  c_methods.input_term = input_term;
-  c_methods.c_ui_method_selection = c_ui_method_selection;
+  cinfo.err = jpeg_std_error(&jerr);
+  /* Now we can initialize the JPEG compression object. */
+  jpeg_create_compress(&cinfo);
 
-  /* Set up default JPEG parameters in the cinfo data structure. */
-  j_c_defaults(&cinfo, 75, FALSE);
-  /* Note: 75 is the recommended default quality level; you may instead pass
-   * a user-specified quality level.  Be aware that values below 25 will cause
-   * non-baseline JPEG files to be created (and a warning message to that
-   * effect to be emitted on stderr).  This won't bother our decoder, but some
-   * commercial JPEG implementations may choke on non-baseline JPEG files.
-   * If you want to force baseline compatibility, pass TRUE instead of FALSE.
-   * (If non-baseline files are fine, but you could do without that warning
-   * message, set e_methods.trace_level to -1.)
-   */
+  /* Step 2: specify data destination (eg, a file) */
+  /* Note: steps 2 and 3 can be done in either order. */
 
-  /* At this point you can modify the default parameters set by j_c_defaults
-   * as needed.  For a minimal implementation, you shouldn't need to change
-   * anything.  See jcmain.c for some examples of what you might change.
-   */
-
-  /* Select the input and output files.
-   * Note that cinfo.input_file is only used if your input reading routines
-   * use it; otherwise, you can just make it NULL.
+  /* Here we use the library-supplied code to send compressed data to a
+   * stdio stream.  You can also write your own code to do something else.
    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
    * requires it in order to write binary files.
    */
-
-  cinfo.input_file = NULL;	/* if no actual input file involved */
-
-  if ((cinfo.output_file = fopen(filename, "wb")) == NULL) {
+  if ((outfile = fopen(filename, "wb")) == NULL) {
     fprintf(stderr, "can't open %s\n", filename);
     exit(1);
   }
+  jpeg_stdio_dest(&cinfo, outfile);
 
-  /* Here we go! */
-  jpeg_compress(&cinfo);
+  /* Step 3: set parameters for compression */
 
-  /* That's it, son.  Nothin' else to do, except close files. */
-  /* Here we assume only the output file need be closed. */
-  fclose(cinfo.output_file);
-
-  /* Note: if you want to compress more than one image, we recommend you
-   * repeat this whole routine.  You MUST repeat the j_c_defaults()/alter
-   * parameters/jpeg_compress() sequence, as some data structures allocated
-   * in j_c_defaults are freed upon exit from jpeg_compress.
+  /* First we supply a description of the input image.
+   * Four fields of the cinfo struct must be filled in:
    */
+  cinfo.image_width = image_width; 	/* image width and height, in pixels */
+  cinfo.image_height = image_height;
+  cinfo.input_components = 3;		/* # of color components per pixel */
+  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
+  /* Now use the library's routine to set default compression parameters.
+   * (You must set at least cinfo.in_color_space before calling this,
+   * since the defaults depend on the source color space.)
+   */
+  jpeg_set_defaults(&cinfo);
+  /* Now you can set any non-default parameters you wish to.
+   * Here we just illustrate the use of quality (quantization table) scaling:
+   */
+  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
+
+  /* Step 4: Start compressor */
+
+  /* TRUE ensures that we will write a complete interchange-JPEG file.
+   * Pass TRUE unless you are very sure of what you're doing.
+   */
+  jpeg_start_compress(&cinfo, TRUE);
+
+  /* Step 5: while (scan lines remain to be written) */
+  /*           jpeg_write_scanlines(...); */
+
+  /* Here we use the library's state variable cinfo.next_scanline as the
+   * loop counter, so that we don't have to keep track ourselves.
+   * To keep things simple, we pass one scanline per call; you can pass
+   * more if you wish, though.
+   */
+  row_stride = image_width * 3;	/* JSAMPLEs per row in image_buffer */
+
+  while (cinfo.next_scanline < cinfo.image_height) {
+    row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
+    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
+  }
+
+  /* Step 6: Finish compression */
+
+  jpeg_finish_compress(&cinfo);
+  /* After finish_compress, we can close the output file. */
+  fclose(outfile);
+
+  /* Step 7: release JPEG compression object */
+
+  /* This is an important step since it will release a good deal of memory. */
+  jpeg_destroy_compress(&cinfo);
+
+  /* And we're done! */
 }
 
 
+/*
+ * SOME FINE POINTS:
+ *
+ * In the above loop, we ignored the return value of jpeg_write_scanlines,
+ * which is the number of scanlines actually written.  We could get away
+ * with this because we were only relying on the value of cinfo.next_scanline,
+ * which will be incremented correctly.  If you maintain additional loop
+ * variables then you should be careful to increment them properly.
+ * Actually, for output to a stdio stream you needn't worry, because
+ * then jpeg_write_scanlines will write all the lines passed (or else exit
+ * with a fatal error).  Partial writes can only occur if you use a data
+ * destination module that can demand suspension of the compressor.
+ * (If you don't know what that's for, you don't need it.)
+ *
+ * If the compressor requires full-image buffers (for entropy-coding
+ * optimization or a noninterleaved JPEG file), it will create temporary
+ * files for anything that doesn't fit within the maximum-memory setting.
+ * (Note that temp files are NOT needed if you use the default parameters.)
+ * On some systems you may need to set up a signal handler to ensure that
+ * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
+ *
+ * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG
+ * files to be compatible with everyone else's.  If you cannot readily read
+ * your data in that order, you'll need an intermediate array to hold the
+ * image.  See rdtarga.c or rdbmp.c for examples of handling bottom-to-top
+ * source data using the JPEG code's internal virtual-array mechanisms.
+ */
+
+
 
 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
 
 /* This half of the example shows how to read data from the JPEG decompressor.
- * It's a little more refined than the above in that we show how to do your
- * own error recovery.  If you don't care about that, you don't need these
- * next two routines.
- */
-
-
-/*
- * These routines replace the default trace/error routines included with the
- * JPEG code.  The example trace_message routine shown here is actually the
- * same as the standard one, but you could modify it if you don't want messages
- * sent to stderr.  The example error_exit routine is set up to return
- * control to read_JPEG_file() rather than calling exit().  You can use the
- * same routines for both compression and decompression error recovery.
- */
-
-/* These static variables are needed by the error routines. */
-static jmp_buf setjmp_buffer;	/* for return to caller */
-static external_methods_ptr emethods; /* needed for access to message_parm */
-
-
-/* This routine is used for any and all trace, debug, or error printouts
- * from the JPEG code.  The parameter is a printf format string; up to 8
- * integer data values for the format string have been stored in the
- * message_parm[] field of the external_methods struct.
- */
-
-METHODDEF void
-trace_message (const char *msgtext)
-{
-  fprintf(stderr, msgtext,
-	  emethods->message_parm[0], emethods->message_parm[1],
-	  emethods->message_parm[2], emethods->message_parm[3],
-	  emethods->message_parm[4], emethods->message_parm[5],
-	  emethods->message_parm[6], emethods->message_parm[7]);
-  fprintf(stderr, "\n");	/* there is no \n in the format string! */
-}
-
-/*
- * The error_exit() routine should not return to its caller.  The default
- * routine calls exit(), but here we assume that we want to return to
- * read_JPEG_file, which has set up a setjmp context for the purpose.
- * You should make sure that the free_all method is called, either within
- * error_exit or after the return to the outer-level routine.
- */
-
-METHODDEF void
-error_exit (const char *msgtext)
-{
-  trace_message(msgtext);	/* report the error message */
-  (*emethods->free_all) ();	/* clean up memory allocation & temp files */
-  longjmp(setjmp_buffer, 1);	/* return control to outer routine */
-}
-
-
-
-/*
- * To accept the image data from decompression, you must define four routines
- * output_init, put_color_map, put_pixel_rows, and output_term.
+ * It's a bit more refined than the above, in that we show:
+ *   (a) how to modify the JPEG library's standard error-reporting behavior;
+ *   (b) how to allocate workspace using the library's memory manager.
  *
- * You must understand the distinction between full color output mode
- * (N independent color components) and colormapped output mode (a single
- * output component representing an index into a color map).  You should use
- * colormapped mode to write to a colormapped display screen or output file.
- * Colormapped mode is also useful for reducing grayscale output to a small
- * number of gray levels: when using the 1-pass quantizer on grayscale data,
- * the colormap entries will be evenly spaced from 0 to MAX_JSAMPLE, so you
- * can regard the indexes as directly representing gray levels at reduced
- * precision.  In any other case, you should not depend on the colormap
- * entries having any particular order.
- * To get colormapped output, set cinfo->quantize_colors to TRUE and set
- * cinfo->desired_number_of_colors to the maximum number of entries in the
- * colormap.  This can be done either in your main routine or in
- * d_ui_method_selection.  For grayscale quantization, also set
- * cinfo->two_pass_quantize to FALSE to ensure the 1-pass quantizer is used
- * (presently this is the default, but it may not be so in the future).
- *
- * The output file writing modules (jwrppm.c, jwrgif.c, jwrtarga.c, etc) may be
- * useful examples of what these routines should actually do, although each of
- * them is encrusted with a lot of specialized code for its own file format.
- */
-
-
-METHODDEF void
-output_init (decompress_info_ptr cinfo)
-/* This routine should do any setup required */
-{
-  /* This routine can initialize for output based on the data passed in cinfo.
-   * Useful fields include:
-   *	image_width, image_height	Pretty obvious, I hope.
-   *	data_precision			bits per pixel value; typically 8.
-   *	out_color_space			output colorspace previously requested
-   *	color_out_comps			number of color components in same
-   *	final_out_comps			number of components actually output
-   * final_out_comps is 1 if quantize_colors is true, else it is equal to
-   * color_out_comps.
-   *
-   * If you have requested color quantization, the colormap is NOT yet set.
-   * You may wish to defer output initialization until put_color_map is called.
-   */
-}
-
-
-/*
- * This routine is called if and only if you have set cinfo->quantize_colors
- * to TRUE.  It is given the selected colormap and can complete any required
- * initialization.  This call will occur after output_init and before any
- * calls to put_pixel_rows.  Note that the colormap pointer is also placed
- * in a cinfo field, whence it can be used by put_pixel_rows or output_term.
- * num_colors will be less than or equal to desired_number_of_colors.
- *
- * The colormap data is supplied as a 2-D array of JSAMPLEs, indexed as
- *		JSAMPLE colormap[component][indexvalue]
- * where component runs from 0 to cinfo->color_out_comps-1, and indexvalue
- * runs from 0 to num_colors-1.  Note that this is actually an array of
- * pointers to arrays rather than a true 2D array, since C does not support
- * variable-size multidimensional arrays.
- * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
- * to be as portable as the JPEG code proper, you should always access JSAMPLE
- * values with the GETJSAMPLE() macro, which will do the right thing if the
- * machine has only signed chars.
- */
-
-METHODDEF void
-put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
-/* Write the color map */
-{
-  /* You need not provide this routine if you always set cinfo->quantize_colors
-   * FALSE; but a safer practice is to provide it and have it just print an
-   * error message, like this:
-   */
-  fprintf(stderr, "put_color_map called: there's a bug here somewhere!\n");
-}
-
-
-/*
- * This function is called repeatedly, with a few more rows of pixels supplied
- * on each call.  With the current JPEG code, some multiple of 8 rows will be
- * passed on each call except the last, but it is extremely bad form to depend
- * on this.  You CAN assume num_rows > 0.
- * The data is supplied in top-to-bottom row order (the standard order within
- * a JPEG file).  If you cannot readily use the data in that order, you'll
- * need an intermediate array to hold the image.  See jwrrle.c for an example
- * of outputting data in bottom-to-top order.
- *
- * The data is supplied as a 3-D array of JSAMPLEs, indexed as
- *		JSAMPLE pixel_data[component][row][column]
- * where component runs from 0 to cinfo->final_out_comps-1, row runs from 0 to
- * num_rows-1, and column runs from 0 to cinfo->image_width-1 (column 0 is
- * left edge of image).  Note that this is actually an array of pointers to
- * pointers to arrays rather than a true 3D array, since C does not support
- * variable-size multidimensional arrays.
- * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
- * to be as portable as the JPEG code proper, you should always access JSAMPLE
- * values with the GETJSAMPLE() macro, which will do the right thing if the
- * machine has only signed chars.
- *
- * If quantize_colors is true, then there is only one component, and its values
- * are indexes into the previously supplied colormap.  Otherwise the values
- * are actual data in your selected output colorspace.
- */
-
-
-METHODDEF void
-put_pixel_rows (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE pixel_data)
-/* Write some rows of output data */
-{
-  /* This example shows how you might write full-color RGB data (3 components)
-   * to an output file in which the data is stored 3 bytes per pixel.
-   */
-  register FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  register int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr0 = pixel_data[0][row];
-    ptr1 = pixel_data[1][row];
-    ptr2 = pixel_data[2][row];
-    for (col = 0; col < cinfo->image_width; col++) {
-      putc(GETJSAMPLE(*ptr0), outfile);	/* red */
-      ptr0++;
-      putc(GETJSAMPLE(*ptr1), outfile);	/* green */
-      ptr1++;
-      putc(GETJSAMPLE(*ptr2), outfile);	/* blue */
-      ptr2++;
-    }
-  }
-}
-
-
-METHODDEF void
-output_term (decompress_info_ptr cinfo)
-/* Finish up at the end of the output */
-{
-  /* This termination routine may not need to do anything. */
-  /* Note that the JPEG code will only call it during successful exit; */
-  /* if you want it called during error exit, you gotta do that yourself. */
-}
-
-
-/*
- * That's it for the routines that deal with writing the output image.
- * Now we have overall control and parameter selection routines.
+ * Just to make this example a little different from the first one, we'll
+ * assume that we do not intend to put the whole image into an in-memory
+ * buffer, but to send it line-by-line someplace else.  We need a one-
+ * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
+ * memory manager allocate it for us.  This approach is actually quite useful
+ * because we don't need to remember to deallocate the buffer separately: it
+ * will go away automatically when the JPEG object is cleaned up.
  */
 
 
 /*
- * This routine gets control after the JPEG file header has been read;
- * at this point the image size and colorspace are known.
- * The routine must determine what output routines are to be used, and make
- * any decompression parameter changes that are desirable.  For example,
- * if it is found that the JPEG file is grayscale, you might want to do
- * things differently than if it is color.  You can also delay setting
- * quantize_colors and associated options until this point. 
+ * ERROR HANDLING:
  *
- * j_d_defaults initializes out_color_space to CS_RGB.  If you want grayscale
- * output you should set out_color_space to CS_GRAYSCALE.  Note that you can
- * force grayscale output from a color JPEG file (though not vice versa).
+ * The JPEG library's standard error handler (jerror.c) is divided into
+ * several "methods" which you can override individually.  This lets you
+ * adjust the behavior without duplicating a lot of code, which you might
+ * have to update with each future release.
+ *
+ * Our example here shows how to override the "error_exit" method so that
+ * control is returned to the library's caller when a fatal error occurs,
+ * rather than calling exit() as the standard error_exit method does.
+ *
+ * We use C's setjmp/longjmp facility to return control.  This means that the
+ * routine which calls the JPEG library must first execute a setjmp() call to
+ * establish the return point.  We want the replacement error_exit to do a
+ * longjmp().  But we need to make the setjmp buffer accessible to the
+ * error_exit routine.  To do this, we make a private extension of the
+ * standard JPEG error handler object.  (If we were using C++, we'd say we
+ * were making a subclass of the regular error handler.)
+ *
+ * Here's the extended error handler struct:
+ */
+
+struct my_error_mgr {
+  struct jpeg_error_mgr pub;	/* "public" fields */
+
+  jmp_buf setjmp_buffer;	/* for return to caller */
+};
+
+typedef struct my_error_mgr * my_error_ptr;
+
+/*
+ * Here's the routine that will replace the standard error_exit method:
  */
 
 METHODDEF void
-d_ui_method_selection (decompress_info_ptr cinfo)
+my_error_exit (j_common_ptr cinfo)
 {
-  /* if grayscale input, force grayscale output; */
-  /* else leave the output colorspace as set by main routine. */
-  if (cinfo->jpeg_color_space == CS_GRAYSCALE)
-    cinfo->out_color_space = CS_GRAYSCALE;
+  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
+  my_error_ptr myerr = (my_error_ptr) cinfo->err;
 
-  /* select output routines */
-  cinfo->methods->output_init = output_init;
-  cinfo->methods->put_color_map = put_color_map;
-  cinfo->methods->put_pixel_rows = put_pixel_rows;
-  cinfo->methods->output_term = output_term;
+  /* Always display the message. */
+  /* We could postpone this until after returning, if we chose. */
+  (*cinfo->err->output_message) (cinfo);
+
+  /* Return control to the setjmp point */
+  longjmp(myerr->setjmp_buffer, 1);
 }
 
 
 /*
- * OK, here is the main function that actually causes everything to happen.
- * We assume here that the JPEG filename is supplied by the caller of this
- * routine, and that all decompression parameters can be default values.
- * The routine returns 1 if successful, 0 if not.
+ * Sample routine for JPEG decompression.  We assume that the source file name
+ * is passed in.  We want to return 1 on success, 0 on error.
  */
 
+
 GLOBAL int
 read_JPEG_file (char * filename)
 {
-  /* These three structs contain JPEG parameters and working data.
-   * They must survive for the duration of parameter setup and one
-   * call to jpeg_decompress; typically, making them local data in the
-   * calling routine is the best strategy.
+  /* This struct contains the JPEG decompression parameters and pointers to
+   * working space (which is allocated as needed by the JPEG library).
    */
-  struct Decompress_info_struct cinfo;
-  struct Decompress_methods_struct dc_methods;
-  struct External_methods_struct e_methods;
+  struct jpeg_decompress_struct cinfo;
+  /* We use our private extension JPEG error handler. */
+  struct my_error_mgr jerr;
+  /* More stuff */
+  FILE * infile;		/* source file */
+  JSAMPARRAY buffer;		/* Output row buffer */
+  int row_stride;		/* physical row width in output buffer */
 
-  /* Select the input and output files.
-   * In this example we want to open the input file before doing anything else,
+  /* In this example we want to open the input file before doing anything else,
    * so that the setjmp() error recovery below can assume the file is open.
-   * Note that cinfo.output_file is only used if your output handling routines
-   * use it; otherwise, you can just make it NULL.
    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
    * requires it in order to read binary files.
    */
 
-  if ((cinfo.input_file = fopen(filename, "rb")) == NULL) {
+  if ((infile = fopen(filename, "rb")) == NULL) {
     fprintf(stderr, "can't open %s\n", filename);
     return 0;
   }
 
-  cinfo.output_file = NULL;	/* if no actual output file involved */
+  /* Step 1: allocate and initialize JPEG decompression object */
 
-  /* Initialize the system-dependent method pointers. */
-  cinfo.methods = &dc_methods;	/* links to method structs */
-  cinfo.emethods = &e_methods;
-  /* Here we supply our own error handler; compare to use of standard error
-   * handler in the previous write_JPEG_file example.
-   */
-  emethods = &e_methods;	/* save struct addr for possible access */
-  e_methods.error_exit = error_exit; /* supply error-exit routine */
-  e_methods.trace_message = trace_message; /* supply trace-message routine */
-  e_methods.trace_level = 0;	/* default = no tracing */
-  e_methods.num_warnings = 0;	/* no warnings emitted yet */
-  e_methods.first_warning_level = 0; /* display first corrupt-data warning */
-  e_methods.more_warning_level = 3; /* but suppress additional ones */
-
-  /* prepare setjmp context for possible exit from error_exit */
-  if (setjmp(setjmp_buffer)) {
+  /* We set up the normal JPEG error routines, then override error_exit. */
+  cinfo.err = jpeg_std_error(&jerr.pub);
+  jerr.pub.error_exit = my_error_exit;
+  /* Establish the setjmp return context for my_error_exit to use. */
+  if (setjmp(jerr.setjmp_buffer)) {
     /* If we get here, the JPEG code has signaled an error.
-     * Memory allocation has already been cleaned up (see free_all call in
-     * error_exit), but we need to close the input file before returning.
-     * You might also need to close an output file, etc.
+     * We need to clean up the JPEG object, close the input file, and return.
      */
-    fclose(cinfo.input_file);
+    jpeg_destroy_decompress(&cinfo);
+    fclose(infile);
     return 0;
   }
+  /* Now we can initialize the JPEG decompression object. */
+  jpeg_create_decompress(&cinfo);
 
-  /* Here we use the standard memory manager provided with the JPEG code.
-   * In some cases you might want to replace the memory manager, or at
-   * least the system-dependent part of it, with your own code.
-   */
-  jselmemmgr(&e_methods);	/* select std memory allocation routines */
-  /* If the decompressor requires full-image buffers (for two-pass color
-   * quantization or a noninterleaved JPEG file), it will create temporary
-   * files for anything that doesn't fit within the maximum-memory setting.
-   * You can change the default maximum-memory setting by changing
-   * e_methods.max_memory_to_use after jselmemmgr returns.
-   * On some systems you may also need to set up a signal handler to
-   * ensure that temporary files are deleted if the program is interrupted.
-   * (This is most important if you are on MS-DOS and use the jmemdos.c
-   * memory manager back end; it will try to grab extended memory for
-   * temp files, and that space will NOT be freed automatically.)
-   * See jcmain.c or jdmain.c for an example signal handler.
+  /* Step 2: specify data source (eg, a file) */
+
+  jpeg_stdio_src(&cinfo, infile);
+
+  /* Step 3: read file parameters with jpeg_read_header() */
+
+  (void) jpeg_read_header(&cinfo, TRUE);
+  /* We can ignore the return value from jpeg_read_header since
+   *   (a) suspension is not possible with the stdio data source, and
+   *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
+   * See libjpeg.doc for more info.
    */
 
-  /* Here, set up the pointer to your own routine for post-header-reading
-   * parameter selection.  You could also initialize the pointers to the
-   * output data handling routines here, if they are not dependent on the
-   * image type.
-   */
-  dc_methods.d_ui_method_selection = d_ui_method_selection;
+  /* Step 4: set parameters for decompression */
 
-  /* Set up default decompression parameters. */
-  j_d_defaults(&cinfo, TRUE);
-  /* TRUE indicates that an input buffer should be allocated.
-   * In unusual cases you may want to allocate the input buffer yourself;
-   * see jddeflts.c for commentary.
+  /* In this example, we don't need to change any of the defaults set by
+   * jpeg_read_header(), so we do nothing here.
    */
 
-  /* At this point you can modify the default parameters set by j_d_defaults
-   * as needed; for example, you can request color quantization or force
-   * grayscale output.  See jdmain.c for examples of what you might change.
+  /* Step 5: Start decompressor */
+
+  jpeg_start_decompress(&cinfo);
+
+  /* We may need to do some setup of our own at this point before reading
+   * the data.  After jpeg_start_decompress() we have the correct scaled
+   * output image dimensions available, as well as the output colormap
+   * if we asked for color quantization.
+   * In this example, we need to make an output work buffer of the right size.
+   */ 
+  /* JSAMPLEs per row in output buffer */
+  row_stride = cinfo.output_width * cinfo.output_components;
+  /* Make a one-row-high sample array that will go away when done with image */
+  buffer = (*cinfo.mem->alloc_sarray)
+		((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
+
+  /* Step 6: while (scan lines remain to be read) */
+  /*           jpeg_read_scanlines(...); */
+
+  /* Here we use the library's state variable cinfo.output_scanline as the
+   * loop counter, so that we don't have to keep track ourselves.
+   */
+  while (cinfo.output_scanline < cinfo.output_height) {
+    (void) jpeg_read_scanlines(&cinfo, buffer, 1);
+    /* Assume put_scanline_someplace wants a pointer and sample count. */
+    put_scanline_someplace(buffer[0], row_stride);
+  }
+
+  /* Step 7: Finish decompression */
+
+  (void) jpeg_finish_decompress(&cinfo);
+  /* We can ignore the return value since suspension is not possible
+   * with the stdio data source.
    */
 
-  /* Set up to read a JFIF or baseline-JPEG file. */
-  /* This is the only JPEG file format currently supported. */
-  jselrjfif(&cinfo);
+  /* Step 8: Release JPEG decompression object */
 
-  /* Here we go! */
-  jpeg_decompress(&cinfo);
+  /* This is an important step since it will release a good deal of memory. */
+  jpeg_destroy_decompress(&cinfo);
 
-  /* That's it, son.  Nothin' else to do, except close files. */
-  /* Here we assume only the input file need be closed. */
-  fclose(cinfo.input_file);
-
-  /* You might want to test e_methods.num_warnings to see if bad data was
-   * detected.  In this example, we just blindly forge ahead.
+  /* After finish_decompress, we can close the input file.
+   * Here we postpone it until after no more JPEG errors are possible,
+   * so as to simplify the setjmp error logic above.  (Actually, I don't
+   * think that jpeg_destroy can do an error exit, but why assume anything...)
    */
-  return 1;			/* indicate success */
+  fclose(infile);
 
-  /* Note: if you want to decompress more than one image, we recommend you
-   * repeat this whole routine.  You MUST repeat the j_d_defaults()/alter
-   * parameters/jpeg_decompress() sequence, as some data structures allocated
-   * in j_d_defaults are freed upon exit from jpeg_decompress.
+  /* At this point you may want to check to see whether any corrupt-data
+   * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
    */
+
+  /* And we're done! */
+  return 1;
 }
+
+
+/*
+ * SOME FINE POINTS:
+ *
+ * In the above code, we ignored the return value of jpeg_read_scanlines,
+ * which is the number of scanlines actually read.  We could get away with
+ * this because we asked for only one line at a time and we weren't using
+ * a suspending data source.  See libjpeg.doc for more info.
+ *
+ * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
+ * we should have done it beforehand to ensure that the space would be
+ * counted against the JPEG max_memory setting.  In some systems the above
+ * code would risk an out-of-memory error.  However, in general we don't
+ * know the output image dimensions before jpeg_start_decompress(), unless we
+ * call jpeg_calc_output_dimensions().  See libjpeg.doc for more about this.
+ *
+ * Scanlines are returned in the same order as they appear in the JPEG file,
+ * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
+ * you can use one of the virtual arrays provided by the JPEG memory manager
+ * to invert the data.  See wrbmp.c for an example.
+ *
+ * As with compression, some operating modes may require temporary files.
+ * On some systems you may need to set up a signal handler to ensure that
+ * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
+ */
diff --git a/filelist.doc b/filelist.doc
new file mode 100644
index 0000000..0965b74
--- /dev/null
+++ b/filelist.doc
@@ -0,0 +1,185 @@
+IJG JPEG LIBRARY:  FILE LIST
+
+Copyright (C) 1994, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+Here is a road map to the files in the IJG JPEG distribution.  The
+distribution includes the JPEG library proper, plus two application
+programs ("cjpeg" and "djpeg") which use the library to convert JPEG
+files to and from some other popular image formats.  There are also
+two stand-alone applications, "rdjpgcom" and "wrjpgcom".
+
+
+THE JPEG LIBRARY
+================
+
+Include files:
+
+jpeglib.h	JPEG library's exported data and function declarations.
+jconfig.h	Configuration declarations.  Note: this file is not present
+		in the distribution; it is generated during installation.
+jmorecfg.h	Additional configuration declarations; need not be changed
+		for a standard installation.
+jerror.h	Declares JPEG library's error and trace message codes.
+jinclude.h	Central include file used by library's .c files.
+jpegint.h	JPEG library's internal data structures.
+jdct.h		Private declarations for forward & reverse DCT subsystems.
+jmemsys.h	Private declarations for memory management subsystem.
+jversion.h	Version information.
+
+Applications using the library should include jpeglib.h (which in turn
+includes jconfig.h and jmorecfg.h).  Optionally, jerror.h may be included
+if the application needs to reference individual JPEG error codes.  The
+other include files are intended for internal use and would not normally
+be included by an application program.  (cjpeg/djpeg do use jinclude.h,
+since its function is to improve portability of the whole IJG distribution.
+Most other applications will directly include the system include files they
+want, and hence won't need jinclude.h.)
+
+
+C source code files:
+
+These files contain most of the functions intended to be called directly by
+an application program:
+
+jcapi.c		Application program interface routines for compression.
+jdapi.c		Application program interface routines for decompression.
+jcomapi.c	Application program interface routines common to compression
+		and decompression.
+jcparam.c	Compression parameter setting helper routines.
+
+Compression side of the library:
+
+jcmaster.c	Master control: determines which other modules to use.
+jcmainct.c	Main buffer controller (preprocessor => JPEG compressor).
+jcprepct.c	Preprocessor buffer controller.
+jccoefct.c	Buffer controller for DCT coefficient buffer.
+jccolor.c	Color space conversion.
+jcsample.c	Downsampling.
+jcdctmgr.c	DCT manager (DCT implementation selection & control).
+jfdctint.c	Forward DCT using slow-but-accurate integer method.
+jfdctfst.c	Forward DCT using faster, less accurate integer method.
+jfdctflt.c	Forward DCT using floating-point arithmetic.
+jchuff.c	Huffman entropy coding.
+jcmarker.c	JPEG marker writing.
+jdatadst.c	Data destination manager for stdio output.
+
+Decompression side of the library:
+
+jdmaster.c	Master control: determines which other modules to use.
+jdmainct.c	Main buffer controller (JPEG decompressor => postprocessor).
+jdcoefct.c	Buffer controller for DCT coefficient buffer.
+jdpostct.c	Postprocessor buffer controller.
+jdmarker.c	JPEG marker reading.
+jdhuff.c	Huffman entropy decoding.
+jddctmgr.c	IDCT manager (IDCT implementation selection & control).
+jidctint.c	Inverse DCT using slow-but-accurate integer method.
+jidctfst.c	Inverse DCT using faster, less accurate integer method.
+jidctflt.c	Inverse DCT using floating-point arithmetic.
+jidctred.c	Inverse DCTs with reduced-size outputs.
+jdsample.c	Upsampling.
+jdcolor.c	Color space conversion.
+jdmerge.c	Merged upsampling/color conversion (faster, lower quality).
+jquant1.c	One-pass color quantization using a fixed-spacing colormap.
+jquant2.c	Two-pass color quantization using a custom-generated colormap.
+		Also handles one-pass quantization to an externally given map.
+jdatasrc.c	Data source manager for stdio input.
+
+Support files for both compression and decompression:
+
+jerror.c	Standard error handling routines (application replaceable).
+jmemmgr.c	System-independent (more or less) memory management code.
+jutils.c	Miscellaneous utility routines.
+
+jmemmgr.c relies on a system-dependent memory management module.  The IJG
+distribution includes the following implementations of the system-dependent
+module:
+
+jmemnobs.c	"No backing store": assumes adequate virtual memory exists.
+jmemansi.c	Makes temporary files with ANSI-standard routine tmpfile().
+jmemname.c	Makes temporary files with program-generated file names.
+jmemdos.c	Custom implementation for MS-DOS: knows about extended and
+		expanded memory as well as temporary files.
+
+Exactly one of the system-dependent modules should be configured into an
+installed JPEG library (see install.doc for hints about which one to use).
+On unusual systems you may find it worthwhile to make a special
+system-dependent memory manager.
+
+
+Non-C source code files:
+
+jmemdosa.asm	80x86 assembly code support for jmemdos.c; used only in
+		MS-DOS-specific configurations of the JPEG library.
+
+
+CJPEG/DJPEG
+===========
+
+Include files:
+
+cdjpeg.h	Declarations shared by cjpeg/djpeg modules.
+cderror.h	Additional error and trace message codes for cjpeg/djpeg.
+
+C source code files:
+
+cjpeg.c		Main program for cjpeg.
+djpeg.c		Main program for djpeg.
+rdcolmap.c	Code to read a colormap file for djpeg's "-map" option.
+
+Image file reader modules for cjpeg:
+
+rdbmp.c		BMP file input.
+rdgif.c		GIF file input.
+rdppm.c		PPM/PGM file input.
+rdrle.c		Utah RLE file input.
+rdtarga.c	Targa file input.
+
+Image file writer modules for djpeg:
+
+wrbmp.c		BMP file output.
+wrgif.c		GIF file output.
+wrppm.c		PPM/PGM file output.
+wrrle.c		Utah RLE file output.
+wrtarga.c	Targa file output.
+
+
+RDJPGCOM/WRJPGCOM
+=================
+
+C source code files:
+
+rdjpgcom.c	Stand-alone rdjpgcom application.
+wrjpgcom.c	Stand-alone wrjpgcom application.
+
+These programs do not depend on the IJG library.  They do use
+jconfig.h and jinclude.h, simply to improve portability.
+
+
+ADDITIONAL FILES
+================
+
+Documentation (see README for a guide to the documentation files):
+
+README		Master documentation file.
+*.doc		Other documentation files.
+*.1		Documentation in Unix man page format.
+change.log	Version-to-version change highlights.
+example.c	Sample code for calling JPEG library.
+
+Configuration/installation files and programs (see install.doc for more info):
+
+configure	Unix shell script to perform automatic configuration.
+ckconfig.c	Program to generate jconfig.h on non-Unix systems.
+jconfig.doc	Template for making jconfig.h by hand.
+makefile.*	Sample makefiles for particular systems.
+jconfig.*	Sample jconfig.h for particular systems.
+ansi2knr.c	De-ANSIfier for pre-ANSI C compilers (courtesy of
+		L. Peter Deutsch and Aladdin Enterprises).
+
+Test files (see install.doc for test procedure):
+
+test*.*		Source and comparison files for confidence test.
+		These are binary image files, NOT text files.
diff --git a/install.doc b/install.doc
new file mode 100644
index 0000000..289ce24
--- /dev/null
+++ b/install.doc
@@ -0,0 +1,745 @@
+INSTALLATION INSTRUCTIONS for the Independent JPEG Group's JPEG software
+
+Copyright (C) 1991-1994, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+This file explains how to configure and install the IJG software.  We have
+tried to make this software extremely portable and flexible, so that it can be
+adapted to almost any environment.  The downside of this decision is that the
+installation process is complicated.  We have provided shortcuts to simplify
+the task on common systems.  But in any case, you will need at least a little
+familiarity with C programming and program build procedures for your system.
+
+If you are only using this software as part of a larger program, the larger
+program's installation procedure may take care of configuring the IJG code.
+For example, Ghostscript's installation script will configure the IJG code.
+You don't need to read this file if you just want to compile Ghostscript.
+
+If you are on a Unix machine, you may not need to read this file at all.
+Try doing
+	./configure
+	make
+	make test
+If that doesn't complain, do
+	make install
+(better do "make -n install" first to see if the makefile will put the files
+where you want them).  Read further if you run into snags or want to customize
+the code for your system.
+
+
+TABLE OF CONTENTS
+-----------------
+
+Before you start
+Configuring the software:
+	using the automatic "configure" script
+	using one of the supplied jconfig and makefile files
+	by hand
+Building the software
+Testing the software
+Installing the software
+Optional stuff
+Optimization
+Hints for specific systems
+
+
+BEFORE YOU START
+================
+
+Before installing the software you must unpack the distributed source code.
+Since you are reading this file, you have probably already succeeded in this
+task.  However, there is a potential for error if you needed to convert the
+files to the local standard text file format (for example, if you are on
+MS-DOS you may have converted LF end-of-line to CR/LF).  You must apply
+such conversion to all the files EXCEPT those whose names begin with "test".
+The test files contain binary data; if you change them in any way then the
+self-test will give bad results.
+
+Please check the last section of this file to see if there are hints for the
+specific machine or compiler you are using.
+
+
+CONFIGURING THE SOFTWARE
+========================
+
+To configure the IJG code for your system, you need to create two files:
+  * jconfig.h: contains values for system-dependent #define symbols.
+  * Makefile: controls the compilation process.
+(On a non-Unix machine, you may create "project files" or some other
+substitute for a Makefile.  jconfig.h is needed in any environment.)
+
+We provide three different ways to generate these files:
+  * On a Unix system, you can just run the "configure" script.
+  * We provide sample jconfig files and makefiles for popular machines;
+    if your machine matches one of the samples, just copy the right sample
+    files to jconfig.h and Makefile.
+  * If all else fails, read the instructions below and make your own files.
+
+
+Configuring the software using the automatic "configure" script
+---------------------------------------------------------------
+
+If you are on a Unix machine, you can just type
+	./configure
+and let the configure script construct appropriate configuration files.
+If you're using "csh" on an old version of System V, you might need to type
+	sh configure
+instead to prevent csh from trying to execute configure itself.
+Expect configure to run for a few minutes, particularly on slower machines;
+it works by compiling a series of test programs.
+
+Configure was created with GNU Autoconf and it follows the usual conventions
+for GNU configure scripts.  It makes a few assumptions that you may want to
+override.  You can do this by providing optional switches to configure:
+
+* Configure will use gcc (GNU C compiler) if it's available, otherwise cc.
+To force a particular compiler to be selected, use the CC option, for example
+	./configure CC='cc'
+The same method can be used to include any unusual compiler switches.
+For example, on HP-UX you probably want to say
+	./configure CC='cc -Aa'
+to get HP's compiler to run in ANSI mode.
+
+* Configure will set up the makefile so that "make install" will install files
+into /usr/local/bin, /usr/local/man, etc.  You can specify an installation
+prefix other than "/usr/local" by giving configure the option "--prefix=PATH".
+
+* If you don't have a lot of swap space, you may need to enable the IJG
+software's internal virtual memory mechanism.  To do this, give the option
+"--with-maxmem=N" where N is the default maxmemory limit in megabytes.
+This is discussed in more detail under "Selecting a memory manager", below.
+You probably don't need to worry about this on reasonably-sized Unix machines,
+unless you plan to process very large images.
+
+Configure has some other features that are useful if you are cross-compiling
+or working in a network of multiple machine types; but if you need those
+features, you probably already know how to use them.
+
+
+Configuring the software using one of the supplied jconfig and makefile files
+-----------------------------------------------------------------------------
+
+If you have one of these systems, you can just use the provided configuration
+files:
+
+Makefile	jconfig file	System and/or compiler
+
+makefile.manx	jconfig.manx	Amiga, Manx Aztec C
+makefile.sas	jconfig.sas	Amiga, SAS C
+mak*jpeg.st	jconfig.st	Atari ST/STE/TT, Pure C or Turbo C
+makefile.bcc	jconfig.bcc	MS-DOS, Borland C (Turbo C)
+makefile.dj	jconfig.dj	MS-DOS, DJGPP (Delorie's port of GNU C)
+makefile.mc6	jconfig.mc6	MS-DOS, Microsoft C version 6.x and up
+makefile.mms	jconfig.vms	Digital VMS, with MMS software
+makefile.vms	jconfig.vms	Digital VMS, without MMS software
+
+Copy the proper jconfig file to jconfig.h and the makefile to Makefile
+(or whatever your system uses as the standard makefile name).  For the
+Atari, we provide three project files; see the Atari hints below.
+
+
+Configuring the software by hand
+--------------------------------
+
+First, generate a jconfig.h file.  If you are moderately familiar with C,
+the comments in jconfig.doc should be enough information to do this; just
+copy jconfig.doc to jconfig.h and edit it appropriately.  Otherwise, you may
+prefer to use the ckconfig.c program.  You will need to compile and execute
+ckconfig.c by hand --- we hope you know at least enough to do that.
+ckconfig.c may not compile the first try (in fact, the whole idea is for it
+to fail if anything is going to).  If you get compile errors, fix them by
+editing ckconfig.c according to the directions given in ckconfig.c.  Once
+you get it to run, it will write a suitable jconfig.h file, and will also
+print out some advice about which makefile to use.
+
+You may also want to look at the canned jconfig files, if there is one for a
+system similar to yours.
+
+Second, select a makefile and copy it to Makefile (or whatever your system
+uses as the standard makefile name).  The most generic makefiles we provide
+are
+	makefile.ansi:	if your C compiler supports function prototypes
+	makefile.unix:	if not.
+(You have function prototypes if ckconfig.c put "#define HAVE_PROTOTYPES"
+in jconfig.h.)  You may want to start from one of the other makefiles if
+there is one for a system similar to yours.
+
+Look over the selected Makefile and adjust options as needed.  In particular
+you may want to change the CC and CFLAGS definitions.  For instance, if you
+are using GCC, set CC=gcc.  If you had to use any compiler switches to get
+ckconfig.c to work, make sure the same switches are in CFLAGS.
+
+If you are on a system that doesn't use makefiles, you'll need to set up
+project files (or whatever you do use) to compile all the source files and
+link them into executable files cjpeg, djpeg, rdjpgcom, and wrjpgcom.  See
+the file lists in any of the makefiles to find out which files go into each
+program.  Note that the provided makefiles all make a "library" file libjpeg
+first, but you don't have to do that if you don't want to; the file lists
+identify which source files are actually needed for compression,
+decompression, or both.  As a last resort, you can make a batch script that
+just compiles everything and links it all together; makefile.vms is an
+example of this (it's for VMS systems that have no make-like utility).
+
+Here are comments about some specific configuration decisions you'll
+need to make:
+
+Command line style
+------------------
+
+cjpeg and djpeg can use a Unix-like command line style which supports
+redirection and piping, like this:
+	cjpeg inputfile >outputfile
+	cjpeg <inputfile >outputfile
+	source program | cjpeg >outputfile
+The simpler "two file" command line style is just
+	cjpeg inputfile outputfile
+You may prefer the two-file style, particularly if you don't have pipes.
+
+You MUST use two-file style on any system that doesn't cope well with binary
+data fed through stdin/stdout; this is true for some MS-DOS compilers, for
+example.  If you're not on a Unix system, it's safest to assume you need
+two-file style.  (But if your compiler provides either the Posix-standard
+fdopen() library routine or a Microsoft-compatible setmode() routine, you
+can safely use the Unix command line style, by defining USE_FDOPEN or
+USE_SETMODE respectively.)
+
+To use the two-file style, make jconfig.h say "#define TWO_FILE_COMMANDLINE".
+
+Selecting a memory manager
+--------------------------
+
+The IJG code is capable of working on images that are too big to fit in main
+memory; data is swapped out to temporary files as necessary.  However, the
+code to do this is rather system-dependent.  We provide four different
+memory managers:
+
+* jmemansi.c	This version uses the ANSI-standard library routine tmpfile(),
+		which not all non-ANSI systems have.  On some systems
+		tmpfile() may put the temporary file in a non-optimal
+		location; if you don't like what it does, use jmemname.c.
+
+* jmemname.c	This version creates named temporary files.  For anything
+		except a Unix machine, you'll need to configure the
+		select_file_name() routine appropriately; see the comments
+		near the head of jmemname.c.  If you use this version, define
+		NEED_SIGNAL_CATCHER in jconfig.h to make sure the temp files
+		are removed if the program is aborted.
+
+* jmemnobs.c	(That stands for No Backing Store :-).)  This will compile on
+		almost any system, but it assumes you have enough main memory
+		or virtual memory to hold the biggest images you work with.
+
+* jmemdos.c	This should be used with most 16-bit MS-DOS compilers.
+		See the system-specific notes about MS-DOS for more info.
+		IMPORTANT: if you use this, define USE_MSDOS_MEMMGR in
+		jconfig.h, and include the assembly file jmemdosa.asm in the
+		programs.  The supplied makefiles and jconfig files for
+		MS-DOS compilers already do both.
+
+To use a particular memory manager, change the SYSDEPMEM variable in your
+makefile to equal the corresponding object file name (for example, jmemansi.o
+or jmemansi.obj for jmemansi.c).
+
+If you have plenty of (real or virtual) main memory, just use jmemnobs.c.
+"Plenty" means about ten bytes for every pixel in the largest images
+you plan to process, so a lot of systems don't meet this criterion.
+If yours doesn't, try jmemansi.c first.  If that doesn't compile, you'll have
+to use jmemname.c; be sure to adjust select_file_name() for local conditions.
+You may also need to change unlink() to remove() in close_backing_store().
+
+Except with jmemnobs.c, you need to adjust the DEFAULT_MAX_MEM setting to a
+reasonable value for your system (either by adding a #define for
+DEFAULT_MAX_MEM to jconfig.h, or by adding a -D switch to the Makefile).
+This value limits the amount of data space the program will attempt to
+allocate.  Code and static data space isn't counted, so the actual memory
+needs for cjpeg or djpeg are typically 100 to 150Kb more than the max-memory
+setting.  Larger max-memory settings reduce the amount of I/O needed to
+process a large image, but too large a value can result in "insufficient
+memory" failures.  On most Unix machines (and other systems with virtual
+memory), just set DEFAULT_MAX_MEM to several million and forget it.  At the
+other end of the spectrum, for MS-DOS machines you probably can't go much
+above 300K to 400K.  (On MS-DOS the value refers to conventional memory only.
+Extended/expanded memory is handled separately by jmemdos.c.)
+
+
+BUILDING THE SOFTWARE
+=====================
+
+Now you should be able to compile the software.  Just say "make" (or
+whatever's necessary to start the compilation).  Have a cup of coffee.
+
+Here are some things that could go wrong:
+
+If your compiler complains about undefined structures, you should be able to
+shut it up by putting "#define INCOMPLETE_TYPES_BROKEN" in jconfig.h.
+
+If you have trouble with missing system include files or inclusion of the
+wrong ones, read jinclude.h.  This shouldn't happen if you used configure
+or ckconfig.c to set up jconfig.h.
+
+There are a fair number of routines that do not use all of their parameters;
+some compilers will issue warnings about this, which you can ignore.  There
+are also a few configuration checks that may give "unreachable code" warnings.
+Any other warning deserves investigation.
+
+If you don't have a getenv() library routine, define NO_GETENV.
+
+Also see the system-specific hints, below.
+
+
+TESTING THE SOFTWARE
+====================
+
+As a quick test of functionality we've included a small sample image in
+several forms:
+	testorig.jpg	Starting point for the djpeg tests.
+	testimg.ppm	The output of djpeg testorig.jpg
+	testimg.gif	The output of djpeg -gif testorig.jpg
+	testimg.jpg	The output of cjpeg testimg.ppm
+(The two .jpg files aren't identical since JPEG is lossy.)  If you can
+generate duplicates of the testimg.* files then you probably have working
+programs.
+
+With most of the makefiles, "make test" will perform the necessary
+comparisons.
+
+If you're using a makefile that doesn't provide the test option, run djpeg
+and cjpeg by hand to generate testout.ppm, testout.gif, and testout.jpg,
+then compare these to testimg.* with whatever binary file comparison tool
+you have.  The files should be bit-for-bit identical.
+
+If the programs complain "MAX_ALLOC_CHUNK is wrong, please fix", then you
+need to reduce MAX_ALLOC_CHUNK to a value that fits in type size_t.
+Try adding "#define MAX_ALLOC_CHUNK 65520L" to jconfig.h.  A less likely
+configuration error is "ALIGN_TYPE is wrong, please fix": defining ALIGN_TYPE
+as long should take care of that one.
+
+If the cjpeg test run fails with "Missing Huffman code table entry", it's a
+good bet that you needed to define RIGHT_SHIFT_IS_UNSIGNED.  Go back to the
+configuration step and run ckconfig.c.  (This is a good plan for any other
+test failure, too.)
+
+If you are using Unix (one-file) command line style on a non-Unix system,
+it's a good idea to check that binary I/O through stdin/stdout actually
+works.  You should get the same results from "djpeg <testorig.jpg >out.ppm"
+as from "djpeg -outfile out.ppm testorig.jpg".  Note that the makefiles all
+use the latter style and therefore do not exercise stdin/stdout!  If this
+check fails, try recompiling cjpeg.c and djpeg.c with USE_SETMODE or
+USE_FDOPEN.  If it still doesn't work, better use two-file style.
+(rdjpgcom.c and wrjpgcom.c will also need to be recompiled.)
+
+If you chose a memory manager other than jmemnobs.c, you should test that
+temporary-file usage works.  Try "djpeg -gif -max 0 testorig.jpg" and make
+sure its output matches testimg.gif.  If you have any really large images
+handy, try compressing them with -optimize and/or decompressing with -gif to
+make sure your DEFAULT_MAX_MEM setting is not too large.
+
+NOTE: this is far from an exhaustive test of the JPEG software; some modules,
+such as 1-pass color quantization, are not exercised at all.  It's just a
+quick test to give you some confidence that you haven't missed something
+major.
+
+
+INSTALLING THE SOFTWARE
+=======================
+
+Once you're done with the above steps, you can install the software by
+copying the executable files (cjpeg, djpeg, rdjpgcom, and wrjpgcom) to
+wherever you normally install programs.  On Unix systems, you'll also want
+to put the man pages (cjpeg.1, djpeg.1, rdjpgcom.1, wrjpgcom.1) in the
+man-page directory.  The canned makefiles don't support this step since
+there's such a wide variety of installation procedures on different systems.
+
+If you generated a Makefile with the "configure" script, you can just say
+	make install
+to install the programs and their man pages into the standard places.
+(You'll probably need to be root to do this.)  We recommend first saying
+	make -n install
+to see where configure thought the files should go.  You may need to edit
+the Makefile, particularly if your system's conventions for man page
+filenames don't match what configure expects.
+
+If you want to install the library file libjpeg.a and the include files j*.h
+(for use in compiling other programs besides cjpeg/djpeg), then say
+	make install-lib
+
+
+OPTIONAL STUFF
+==============
+
+Progress monitor:
+
+If you like, you can #define PROGRESS_REPORT (in jconfig.h) to enable display
+of percent-done progress reports.  The routines provided in cjpeg.c/djpeg.c
+merely print percentages to stderr, but you can customize them to do
+something fancier.
+
+Utah RLE file format support:
+
+We distribute the software with support for RLE image files (Utah Raster
+Toolkit format) disabled, because the RLE support won't compile without the
+Utah library.  If you have URT version 3.1 or later, you can enable RLE
+support as follows:
+	1.  #define RLE_SUPPORTED in jconfig.h.
+	2.  Add a -I option to CFLAGS in the Makefile for the directory
+	    containing the URT .h files (typically the "include"
+	    subdirectory of the URT distribution).
+	3.  Add -L... -lrle to LDLIBS in the Makefile, where ... specifies
+	    the directory containing the URT "librle.a" file (typically the
+	    "lib" subdirectory of the URT distribution).
+
+Removing code:
+
+If you need to make a smaller version of the JPEG software, some optional
+functions can be removed at compile time.  See the xxx_SUPPORTED #defines in
+jconfig.h and jmorecfg.h.  If at all possible, we recommend that you leave in
+decoder support for all valid JPEG files, to ensure that you can read anyone's
+output.  Taking out support for image file formats that you don't use is the
+most painless way to make the programs smaller.  Another possibility is to
+remove some of the DCT methods: in particular, the "IFAST" method may not be
+enough faster than the others to be worth keeping on your machine.  (If you
+do remove ISLOW or IFAST, be sure to redefine JDCT_DEFAULT or JDCT_FASTEST
+to a supported method, by adding a #define in jconfig.h.)
+
+
+OPTIMIZATION
+============
+
+Unless you own a Cray, you'll probably be interested in making the JPEG
+software go as fast as possible.  This section covers some machine-dependent
+optimizations you may want to try.  We suggest that before trying any of
+this, you first get the basic installation to pass the self-test step.
+Repeat the self-test after any optimization to make sure that you haven't
+broken anything.
+
+The integer DCT routines perform a lot of multiplications.  These
+multiplications must yield 32-bit results, but none of their input values
+are more than 16 bits wide.  On many machines, notably the 680x0 and 80x86
+CPUs, a 16x16=>32 bit multiply instruction is faster than a full 32x32=>32
+bit multiply.  Unfortunately there is no portable way to specify such a
+multiplication in C, but some compilers can generate one when you use the
+right combination of casts.  See the MULTIPLYxxx macro definitions in
+jdct.h.  If your compiler makes "int" be 32 bits and "short" be 16 bits,
+defining SHORTxSHORT_32 is fairly likely to work.  When experimenting with
+alternate definitions, be sure to test not only whether the code still works
+(use the self-test), but also whether it is actually faster --- on some
+compilers, alternate definitions may compute the right answer, yet be slower
+than the default.  Timing cjpeg on a large PPM input file is the best way to
+check this, as the DCT will be the largest fraction of the runtime in that
+mode.  (Note: some of the distributed compiler-specific jconfig files
+already contain #define switches to select appropriate MULTIPLYxxx
+definitions.)
+
+If your machine has sufficiently fast floating point hardware, you may find
+that the float DCT method is faster than the integer DCT methods, even
+after tweaking the integer multiply macros.  In that case you may want to
+make the float DCT be the default method.  (The only objection to this is
+that float DCT results may vary slightly across machines.)  To do that, add
+"#define JDCT_DEFAULT JDCT_FLOAT" to jconfig.h.  Even if you don't change
+the default, you should redefine JDCT_FASTEST, which is the method selected
+by djpeg's -fast switch.  Don't forget to update the documentation files
+(usage.doc and/or cjpeg.1, djpeg.1) to agree with what you've done.
+
+If access to "short" arrays is slow on your machine, it may be a win to
+define type JCOEF as int rather than short.  This will cost a good deal of
+memory though, particularly in some multi-pass modes, so don't do it unless
+you have memory to burn and short is REALLY slow.
+
+If your compiler can compile function calls in-line, make sure the INLINE
+macro in jmorecfg.h is defined as the keyword that marks a function
+inline-able.  Some compilers have a switch that tells the compiler to inline
+any function it thinks is profitable (e.g., -finline-functions for gcc).
+Enabling such a switch is likely to make the compiled code bigger but faster.
+
+In general, it's worth trying the maximum optimization level of your compiler,
+and experimenting with any optional optimizations such as loop unrolling.
+(Unfortunately, far too many compilers have optimizer bugs ... be prepared to
+back off if the code fails self-test.)  If you do any experimentation along
+these lines, please report the optimal settings to jpeg-info@uunet.uu.net so
+we can mention them in future releases.  Be sure to specify your machine and
+compiler version.
+
+
+HINTS FOR SPECIFIC SYSTEMS
+==========================
+
+We welcome reports on changes needed for systems not mentioned here.  Submit
+'em to jpeg-info@uunet.uu.net.  Also, if configure or ckconfig.c is wrong
+about how to configure the JPEG software for your system, please let us know.
+
+
+Acorn RISC OS:
+
+(Thanks to Simon Middleton for these hints on compiling with Desktop C.)
+After renaming the files according to Acorn conventions, take a copy of
+makefile.ansi, change all occurrences of 'libjpeg.a' to 'libjpeg.o' and
+change these definitions as indicated:
+
+CFLAGS= -throwback -IC: -Wn
+LDLIBS=C:o.Stubs
+SYSDEPMEM=jmemansi.o
+LN=Link
+AR=LibFile -c -o
+
+Also add a new line '.c.o:; $(cc) $< $(cflags) -c -o $@'.  Remove the
+lines '$(RM) libjpeg.o' and '$(AR2) libjpeg.o' and the 'jconfig.h'
+dependency section.
+
+Copy jconfig.doc to jconfig.h.  Edit jconfig.h to define TWO_FILE_COMMANDLINE
+and CHAR_IS_UNSIGNED.
+
+Run the makefile using !AMU not !Make.  If you want to use the 'clean' and
+'test' makefile entries then you will have to fiddle with the syntax a bit
+and rename the test files.
+
+
+Amiga:
+
+SAS C 6.50 reportedly is too buggy to compile the IJG code properly.
+A patch to update to 6.51 is available from SAS or AmiNet FTP sites.
+
+
+Atari ST/STE/TT:
+ 
+Copy the project files makcjpeg.st, makdjpeg.st, and makljpeg.st to cjpeg.prj,
+djpeg.prj, and libjpeg.prj respectively.  The project files should work as-is
+with Pure C.  For Turbo C, change library filenames "PC..." to "TC..." in
+cjpeg.prj and djpeg.prj.  Note that libjpeg.prj selects jmemansi.c as the
+recommended memory manager.  You'll probably want to adjust the
+DEFAULT_MAX_MEM setting --- you want it to be a couple hundred K less than
+your normal free memory.  Put "#define DEFAULT_MAX_MEM nnnn" into jconfig.h
+to do this.
+
+To use the 68881/68882 coprocessor for the floating point DCT, add the
+compiler option "-8" to the project files and replace PCFLTLIB.LIB with
+PC881LIB.LIB in cjpeg.prj and djpeg.prj.  Or if you don't have a
+coprocessor, you may prefer to remove the float DCT code by undefining
+DCT_FLOAT_SUPPORTED in jmorecfg.h (since without a coprocessor, the float
+code will be too slow to be useful).  In that case, you can delete
+PCFLTLIB.LIB from the project files.
+
+Note that you must make libjpeg.lib before making cjpeg.ttp or djpeg.ttp.
+You'll have to perform the self-test by hand.
+
+We haven't bothered to include project files for rdjpgcom and wrjpgcom.
+Those source files should just be compiled by themselves; they don't
+depend on the JPEG library.
+
+There is a bug in some older versions of the Turbo C library which causes the
+space used by temporary files created with "tmpfile()" not to be freed after
+an abnormal program exit.  If you check your disk afterwards, you will find
+cluster chains that are allocated but not used by a file.  This should not
+happen in cjpeg or djpeg, since we enable a signal catcher to explicitly close
+temp files before exiting.  But if you use the JPEG library with your own
+code, be sure to supply a signal catcher, or else use a different
+system-dependent memory manager.
+
+
+Cray:
+
+Should you be so fortunate as to be running JPEG on a Cray YMP, there is a
+compiler bug in old versions of Cray's Standard C (prior to 3.1).  If you
+still have an old compiler, you'll need to insert a line reading
+"#pragma novector" just before the loop	
+    for (i = 1; i <= (int) htbl->bits[l]; i++)
+      huffsize[p++] = (char) l;
+in fix_huff_tbl (in V5beta1, line 204 of jchuff.c and line 176 of jdhuff.c).
+[This bug may or may not still occur with the current IJG code, but it's
+probably a dead issue anyway...]
+
+
+HP-UX:
+
+If you have HP-UX 7.05 or later with the "software development" C compiler,
+you should run the compiler in ANSI mode.  If using the configure script,
+say
+	./configure CC='cc -Aa'
+(or -Ae if you prefer).  If configuring by hand, use makefile.ansi and add
+"-Aa" to the CFLAGS line in the makefile.
+
+If you have a pre-7.05 system, or if you are using the non-ANSI C compiler
+delivered with a minimum HP-UX system, then you must use makefile.unix
+(and do NOT add -Aa); or just run configure without the CC option.
+
+On HP 9000 series 800 machines, the HP C compiler is buggy in revisions prior
+to A.08.07.  If you get complaints about "not a typedef name", you'll have to
+use makefile.unix, or run configure without the CC option.
+
+
+Macintosh, MPW:
+
+We don't directly support MPW in the current release, but Larry Rosenstein
+ported an earlier version of the IJG code without very much trouble.  There's
+useful notes and conversion scripts in his kit for porting PBMPLUS to MPW.
+You can obtain the kit by FTP to ftp.apple.com, files /pub/lsr/pbmplus-port*.
+
+
+Macintosh, Metrowerks CodeWarrior:
+
+Metrowerks release DR2 has problems with the IJG code; don't use it.  Release
+DR3.5 or later should be OK.
+
+The command-line-style interface can be used by defining USE_CCOMMAND and
+TWO_FILE_COMMANDLINE (see next entry for more details).
+
+On 680x0 Macs, Metrowerks defines type "double" as a 10-byte IEEE extended
+float.  jmemmgr.c won't like this: it wants sizeof(ALIGN_TYPE) to be a power
+of 2.  Add "#define ALIGN_TYPE long" to jconfig.h to eliminate the complaint.
+
+
+Macintosh, Think C:
+
+The supplied user-interface files (cjpeg.c and djpeg.c) are set up to provide
+a Unix-style command line interface.  You can use this interface on the Mac
+by means of Think's ccommand() library routine.  However, a much better
+Mac-style user interface has been prepared by Jim Brunner.  You can obtain
+the additional source code needed for that user interface by FTP to
+sumex-aim.stanford.edu, file /info-mac/dev/src/jpeg-convert-c.hqx.  Jim's
+documentation also includes more detailed build instructions for Think C.
+(Jim is working on updating this code to work with v5 of the IJG library,
+but it wasn't ready as of v5 release time.  Should be out before too long.)
+
+If you want to build the minimal command line version, proceed as follows.
+You'll have to prepare project files for the programs; we don't include any
+in the distribution since they are not text files.  Use the file lists in
+any of the supplied makefiles as a guide.  Also add the ANSI and Unix C
+libraries in a separate segment.  You may need to divide the JPEG files into
+more than one segment; we recommend dividing compression and decompression
+modules.  Define USE_CCOMMAND in jconfig.h so that the ccommand() routine is
+called.  You must also define TWO_FILE_COMMANDLINE because stdin/stdout
+don't handle binary data correctly.
+
+On 680x0 Macs, Think C defines type "double" as a 12-byte IEEE extended float.
+jmemmgr.c won't like this: it wants sizeof(ALIGN_TYPE) to be a power of 2.
+Add "#define ALIGN_TYPE long" to jconfig.h to eliminate the complaint.
+
+
+MIPS R3000:
+
+MIPS's cc version 1.31 has a rather nasty optimization bug.  Don't use -O
+if you have that compiler version.  (Use "cc -V" to check the version.)
+Note that the R3000 chip is found in workstations from DEC and others.
+
+
+MS-DOS, generic comments for 16-bit compilers:
+
+The IJG code is designed to be compiled in 80x86 "small" or "medium" memory
+models (i.e., data pointers are 16 bits unless explicitly declared "far";
+code pointers can be either size).  You may be able to use small model to
+compile cjpeg or djpeg by itself, but you will probably have to use medium
+model for any larger application.  This won't make much difference in
+performance.  You *will* take a noticeable performance hit if you use a
+large-data memory model, and you should avoid "huge" model if at all
+possible.  Be sure that NEED_FAR_POINTERS is defined in jconfig.h if you use
+a small-data memory model; be sure it is NOT defined if you use a large-data
+model.  (The supplied makefiles and jconfig files for Borland and Microsoft C
+compile in medium model and define NEED_FAR_POINTERS.)
+
+The DOS-specific memory manager, jmemdos.c, should be used if possible.
+It needs some assembly-code routines which are in jmemdosa.asm; make sure
+your makefile assembles that file and includes it in the library.  If you
+don't have a suitable assembler, you can get pre-assembled object files for
+jmemdosa by FTP from ftp.uu.net: graphics/jpeg/jdosaobj.zip.
+
+When using jmemdos.c, jconfig.h must define USE_MSDOS_MEMMGR and must set
+MAX_ALLOC_CHUNK to less than 64K (65520L is a typical value).  If your
+C library's far-heap malloc() can't allocate blocks that large, reduce
+MAX_ALLOC_CHUNK to whatever it can handle.
+
+If you can't use jmemdos.c for some reason --- for example, because you
+don't have an assembler to assemble jmemdosa.asm --- you'll have to fall
+back to jmemansi.c or jmemname.c.  You'll probably still need to set
+MAX_ALLOC_CHUNK in jconfig.h, because most DOS C libraries won't malloc()
+more than 64K at a time.  IMPORTANT: if you use jmemansi.c or jmemname.c,
+you will have to compile in a large-data memory model in order to get the
+right stdio library.  Too bad.
+
+wrjpgcom needs to be compiled in large model, because it malloc()s a 64KB
+work area to hold the comment text.  If your C library's malloc can't
+handle that, reduce MAX_COM_LENGTH as necessary in wrjpgcom.c.
+
+Most MS-DOS compilers treat stdin/stdout as text files, so you must use
+two-file command line style.  But if your compiler has either fdopen() or
+setmode(), you can use one-file style if you like.  To do this, define
+USE_SETMODE or USE_FDOPEN so that stdin/stdout will be set to binary mode.
+(USE_SETMODE seems to work with more DOS compilers than USE_FDOPEN.)  You
+should test that I/O through stdin/stdout produces the same results as I/O
+to explicitly named files... the "make test" procedures in the supplied
+makefiles do NOT use stdin/stdout.
+
+
+MS-DOS, generic comments for 32-bit compilers:
+
+None of the above comments about memory models apply if you are using a
+32-bit flat-memory-space environment, such as DJGPP or Watcom C.  (And you
+should use one if you have it, as performance will be much better than
+8086-compatible code!)  For flat-memory-space compilers, do NOT define
+NEED_FAR_POINTERS, and do NOT use jmemdos.c.  Use jmemnobs.c if the
+environment supplies adequate virtual memory, otherwise use jmemansi.c or
+jmemname.c.
+
+You'll still need to be careful about binary I/O through stdin/stdout.
+See the last paragraph of the previous section.
+
+
+MS-DOS, Borland C:
+
+If you want one-file command line style, just undefine TWO_FILE_COMMANDLINE.
+jconfig.bcc includes #define USE_SETMODE.  (fdopen does not work correctly.)
+
+Be sure to convert all the source files to DOS text format (CR/LF newlines).
+Although Borland C will often work OK with unmodified Unix (LF newlines)
+source files, sometimes it will give bogus compile errors.
+"Illegal character '#'" is the most common such error.
+
+
+MS-DOS, DJGPP:
+
+Use a recent version of DJGPP (1.11 or better).  If you prefer two-file
+command line style, change the supplied jconfig.dj to define
+TWO_FILE_COMMANDLINE.  makefile.dj is set up to generate only COFF files
+(cjpeg, djpeg, etc) when you say make.  After testing, say "make exe" to
+make executables with stub.exe, or "make standalone" if you want executables
+that include go32.  You will probably need to tweak the makefile's pointer to
+go32.exe to do "make standalone".
+
+
+MS-DOS, Microsoft C:
+
+If you want one-file command line style, just undefine TWO_FILE_COMMANDLINE.
+jconfig.mc6 includes #define USE_SETMODE.  (fdopen does not work correctly.)
+
+Old versions of MS C fail with an "out of macro expansion space" error
+because they can't cope with the macro TRACEMS8 (defined in jerror.h).
+If this happens to you, the easiest solution is to change TRACEMS8 to
+expand to nothing.  You'll lose the ability to dump out JPEG coefficient
+tables with djpeg -debug -debug, but at least you can compile.
+
+Original MS C 6.0 is very buggy; it compiles incorrect code unless you turn
+off optimization entirely (remove -O from CFLAGS).  6.00A is better, but it
+still generates bad code if you enable loop optimizations (-Ol or -Ox).
+
+MS C 8.0 reportedly fails to compile jquant1.c if optimization is turned off
+(yes, off).
+
+
+SGI:
+
+Set "AR2= ar -ts" rather than "AR2= ranlib" in the Makefile.  If you are
+using configure, you should say
+	./configure RANLIB='ar -ts'
+
+
+VMS:
+
+On an Alpha/VMS system with MMS, be sure to use the "/Marco=Alpha=1"
+qualifier with MMS when building the JPEG package.
+
+VAX/VMS v5.5-1 may have problems with the test step of the build procedure
+reporting differences when it compares the original and test GIF and JPG
+images.  If the error points to the last block of the files, it is most
+likely bogus and may be safely ignored.  It seems to be because the files
+are Stream_LF and Backup/Compare has difficulty with the (presumably) null
+padded files.  This problem was not observed on VAX/VMS v6.1 or AXP/VMS v6.1.
diff --git a/jbsmooth.c b/jbsmooth.c
deleted file mode 100644
index 223143e..0000000
--- a/jbsmooth.c
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * jbsmooth.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains cross-block smoothing routines.
- * These routines are invoked via the smooth_coefficients method.
- */
-
-#include "jinclude.h"
-
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-
-
-/*
- * Cross-block coefficient smoothing.
- */
-
-METHODDEF void
-smooth_coefficients (decompress_info_ptr cinfo,
-		     jpeg_component_info *compptr,
-		     JBLOCKROW above,
-		     JBLOCKROW currow,
-		     JBLOCKROW below,
-		     JBLOCKROW output)
-{
-  QUANT_TBL_PTR Qptr = cinfo->quant_tbl_ptrs[compptr->quant_tbl_no];
-  long blocks_in_row = compptr->downsampled_width / DCTSIZE;
-  long col;
-
-  /* First, copy the block row as-is.
-   * This takes care of the first & last blocks in the row, the top/bottom
-   * special cases, and the higher-order coefficients in each block.
-   */
-  jcopy_block_row(currow, output, blocks_in_row);
-
-  /* Now apply the smoothing calculation, but not to any blocks on the
-   * edges of the image.
-   */
-
-  if (above != NULL && below != NULL) {
-    for (col = 1; col < blocks_in_row-1; col++) {
-
-      /* See section K.8 of the JPEG standard.
-       *
-       * As I understand it, this produces approximations
-       * for the low frequency AC components, based on the
-       * DC values of the block and its eight neighboring blocks.
-       * (Thus it can't be used for blocks on the image edges.)
-       */
-
-      /* The layout of these variables corresponds to text and figure in K.8 */
-      
-      JCOEF DC1, DC2, DC3;
-      JCOEF DC4, DC5, DC6;
-      JCOEF DC7, DC8, DC9;
-      
-      long       AC01, AC02;
-      long AC10, AC11;
-      long AC20;
-      
-      DC1 = above [col-1][0];
-      DC2 = above [col  ][0];
-      DC3 = above [col+1][0];
-      DC4 = currow[col-1][0];
-      DC5 = currow[col  ][0];
-      DC6 = currow[col+1][0];
-      DC7 = below [col-1][0];
-      DC8 = below [col  ][0];
-      DC9 = below [col+1][0];
-      
-#define DIVIDE_256(x)	x = ( (x) < 0 ? -((128-(x))/256) : ((x)+128)/256 )
-      
-      AC01 = (36 * (DC4 - DC6));
-      DIVIDE_256(AC01);
-      AC10 = (36 * (DC2 - DC8));
-      DIVIDE_256(AC10);
-      AC20 = (9 * (DC2 + DC8 - 2*DC5));
-      DIVIDE_256(AC20);
-      AC11 = (5 * ((DC1 - DC3) - (DC7 - DC9)));
-      DIVIDE_256(AC11);
-      AC02 = (9 * (DC4 + DC6 - 2*DC5));
-      DIVIDE_256(AC02);
-      
-      /* I think that this checks to see if the quantisation
-       * on the transmitting side would have produced this
-       * answer. If so, then we use our (hopefully better)
-       * estimate.
-       */
-
-#define ABS(x)	((x) < 0 ? -(x) : (x))
-
-#define COND_ASSIGN(_ac,_n,_z)   if ((ABS(output[col][_n] - (_ac))<<1) <= Qptr[_z]) output[col][_n] = (JCOEF) (_ac)
-
-      COND_ASSIGN(AC01,  1, 1);
-      COND_ASSIGN(AC02,  2, 5);
-      COND_ASSIGN(AC10,  8, 2);
-      COND_ASSIGN(AC11,  9, 4);
-      COND_ASSIGN(AC20, 16, 3);
-    }
-  }
-}
-
-
-/*
- * The method selection routine for cross-block smoothing.
- */
-
-GLOBAL void
-jselbsmooth (decompress_info_ptr cinfo)
-{
-  /* just one implementation for now */
-  cinfo->methods->smooth_coefficients = smooth_coefficients;
-}
-
-#endif /* BLOCK_SMOOTHING_SUPPORTED */
diff --git a/jcapi.c b/jcapi.c
new file mode 100644
index 0000000..d9ba37b
--- /dev/null
+++ b/jcapi.c
@@ -0,0 +1,369 @@
+/*
+ * jcapi.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains application interface code for the compression half of
+ * the JPEG library.  Most of the routines intended to be called directly by
+ * an application are in this file.  But also see jcparam.c for
+ * parameter-setup helper routines, and jcomapi.c for routines shared by
+ * compression and decompression.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Initialization of a JPEG compression object.
+ * The error manager must already be set up (in case memory manager fails).
+ */
+
+GLOBAL void
+jpeg_create_compress (j_compress_ptr cinfo)
+{
+  int i;
+
+  /* For debugging purposes, zero the whole master structure.
+   * But error manager pointer is already there, so save and restore it.
+   */
+  {
+    struct jpeg_error_mgr * err = cinfo->err;
+    MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
+    cinfo->err = err;
+  }
+  cinfo->is_decompressor = FALSE;
+
+  /* Initialize a memory manager instance for this object */
+  jinit_memory_mgr((j_common_ptr) cinfo);
+
+  /* Zero out pointers to permanent structures. */
+  cinfo->progress = NULL;
+  cinfo->dest = NULL;
+
+  cinfo->comp_info = NULL;
+
+  for (i = 0; i < NUM_QUANT_TBLS; i++)
+    cinfo->quant_tbl_ptrs[i] = NULL;
+
+  for (i = 0; i < NUM_HUFF_TBLS; i++) {
+    cinfo->dc_huff_tbl_ptrs[i] = NULL;
+    cinfo->ac_huff_tbl_ptrs[i] = NULL;
+  }
+
+  cinfo->input_gamma = 1.0;	/* in case application forgets */
+
+  /* OK, I'm ready */
+  cinfo->global_state = CSTATE_START;
+}
+
+
+/*
+ * Destruction of a JPEG compression object
+ */
+
+GLOBAL void
+jpeg_destroy_compress (j_compress_ptr cinfo)
+{
+  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Forcibly suppress or un-suppress all quantization and Huffman tables.
+ * Marks all currently defined tables as already written (if suppress)
+ * or not written (if !suppress).  This will control whether they get emitted
+ * by a subsequent jpeg_start_compress call.
+ *
+ * This routine is exported for use by applications that want to produce
+ * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
+ * since it is called by jpeg_start_compress, we put it here --- otherwise
+ * jcparam.o would be linked whether the application used it or not.
+ */
+
+GLOBAL void
+jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
+{
+  int i;
+  JQUANT_TBL * qtbl;
+  JHUFF_TBL * htbl;
+
+  for (i = 0; i < NUM_QUANT_TBLS; i++) {
+    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
+      qtbl->sent_table = suppress;
+  }
+
+  for (i = 0; i < NUM_HUFF_TBLS; i++) {
+    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
+      htbl->sent_table = suppress;
+    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
+      htbl->sent_table = suppress;
+  }
+}
+
+
+/*
+ * Compression initialization.
+ * Before calling this, all parameters and a data destination must be set up.
+ *
+ * We require a write_all_tables parameter as a failsafe check when writing
+ * multiple datastreams from the same compression object.  Since prior runs
+ * will have left all the tables marked sent_table=TRUE, a subsequent run
+ * would emit an abbreviated stream (no tables) by default.  This may be what
+ * is wanted, but for safety's sake it should not be the default behavior:
+ * programmers should have to make a deliberate choice to emit abbreviated
+ * images.  Therefore the documentation and examples should encourage people
+ * to pass write_all_tables=TRUE; then it will take active thought to do the
+ * wrong thing.
+ */
+
+GLOBAL void
+jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
+{
+  if (cinfo->global_state != CSTATE_START)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  if (write_all_tables)
+    jpeg_suppress_tables(cinfo, FALSE);	/* mark all tables to be written */
+
+  /* (Re)initialize error mgr and destination modules */
+  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+  (*cinfo->dest->init_destination) (cinfo);
+  /* Perform master selection of active modules */
+  jinit_master_compress(cinfo);
+  /* Set up for the first pass */
+  (*cinfo->master->prepare_for_pass) (cinfo);
+  /* Ready for application to drive first pass through jpeg_write_scanlines
+   * or jpeg_write_raw_data.
+   */
+  cinfo->next_scanline = 0;
+  cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
+}
+
+
+/*
+ * Write some scanlines of data to the JPEG compressor.
+ *
+ * The return value will be the number of lines actually written.
+ * This should be less than the supplied num_lines only in case that
+ * the data destination module has requested suspension of the compressor,
+ * or if more than image_height scanlines are passed in.
+ *
+ * Note: we warn about excess calls to jpeg_write_scanlines() since
+ * this likely signals an application programmer error.  However,
+ * excess scanlines passed in the last valid call are *silently* ignored,
+ * so that the application need not adjust num_lines for end-of-image
+ * when using a multiple-scanline buffer.
+ */
+
+GLOBAL JDIMENSION
+jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
+		      JDIMENSION num_lines)
+{
+  JDIMENSION row_ctr, rows_left;
+
+  if (cinfo->global_state != CSTATE_SCANNING)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+  if (cinfo->next_scanline >= cinfo->image_height)
+    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+
+  /* Call progress monitor hook if present */
+  if (cinfo->progress != NULL) {
+    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
+    cinfo->progress->pass_limit = (long) cinfo->image_height;
+    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+  }
+
+  /* Give master control module another chance if this is first call to
+   * jpeg_write_scanlines.  This lets output of the frame/scan headers be
+   * delayed so that application can write COM, etc, markers between
+   * jpeg_start_compress and jpeg_write_scanlines.
+   */
+  if (cinfo->master->call_pass_startup)
+    (*cinfo->master->pass_startup) (cinfo);
+
+  /* Ignore any extra scanlines at bottom of image. */
+  rows_left = cinfo->image_height - cinfo->next_scanline;
+  if (num_lines > rows_left)
+    num_lines = rows_left;
+
+  row_ctr = 0;
+  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
+  cinfo->next_scanline += row_ctr;
+  return row_ctr;
+}
+
+
+/*
+ * Alternate entry point to write raw data.
+ * Processes exactly one iMCU row per call.
+ */
+
+GLOBAL JDIMENSION
+jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
+		     JDIMENSION num_lines)
+{
+  JDIMENSION mcu_ctr, lines_per_MCU_row;
+
+  if (cinfo->global_state != CSTATE_RAW_OK)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+  if (cinfo->next_scanline >= cinfo->image_height) {
+    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+    return 0;
+  }
+
+  /* Call progress monitor hook if present */
+  if (cinfo->progress != NULL) {
+    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
+    cinfo->progress->pass_limit = (long) cinfo->image_height;
+    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+  }
+
+  /* Give master control module another chance if this is first call to
+   * jpeg_write_raw_data.  This lets output of the frame/scan headers be
+   * delayed so that application can write COM, etc, markers between
+   * jpeg_start_compress and jpeg_write_raw_data.
+   */
+  if (cinfo->master->call_pass_startup)
+    (*cinfo->master->pass_startup) (cinfo);
+
+  /* Verify that at least one iMCU row has been passed. */
+  lines_per_MCU_row = cinfo->max_v_samp_factor * DCTSIZE;
+  if (num_lines < lines_per_MCU_row)
+    ERREXIT(cinfo, JERR_BUFFER_SIZE);
+
+  /* Directly compress the row. */
+  mcu_ctr = 0;
+  (*cinfo->coef->compress_data) (cinfo, data, &mcu_ctr);
+  /* If compressor did not consume the whole row, then we must need to
+   * suspend processing; this is not currently supported.
+   */
+  if (mcu_ctr != cinfo->MCUs_per_row)
+    ERREXIT(cinfo, JERR_CANT_SUSPEND);
+
+  /* OK, we processed one iMCU row. */
+  cinfo->next_scanline += lines_per_MCU_row;
+  return lines_per_MCU_row;
+}
+
+
+/*
+ * Finish JPEG compression.
+ *
+ * If a multipass operating mode was selected, this may do a great deal of
+ * work including most of the actual output.
+ */
+
+GLOBAL void
+jpeg_finish_compress (j_compress_ptr cinfo)
+{
+  JDIMENSION iMCU_row, mcu_ctr;
+
+  if (cinfo->global_state != CSTATE_SCANNING && 
+      cinfo->global_state != CSTATE_RAW_OK)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+  if (cinfo->next_scanline < cinfo->image_height)
+    ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
+  /* Terminate first pass */
+  (*cinfo->master->finish_pass) (cinfo);
+  /* Perform any remaining passes */
+  while (! cinfo->master->is_last_pass) {
+    (*cinfo->master->prepare_for_pass) (cinfo);
+    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
+      if (cinfo->progress != NULL) {
+	cinfo->progress->pass_counter = (long) iMCU_row;
+	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
+	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+      }
+      /* We bypass the main controller and invoke coef controller directly;
+       * all work is being done from the coefficient buffer.
+       */
+      mcu_ctr = 0;
+      (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL, &mcu_ctr);
+      if (mcu_ctr != cinfo->MCUs_per_row)
+	ERREXIT(cinfo, JERR_CANT_SUSPEND);
+    }
+    (*cinfo->master->finish_pass) (cinfo);
+  }
+  /* Write EOI, do final cleanup */
+  (*cinfo->marker->write_file_trailer) (cinfo);
+  (*cinfo->dest->term_destination) (cinfo);
+  /* We can use jpeg_abort to release memory and reset global_state */
+  jpeg_abort((j_common_ptr) cinfo);
+}
+
+
+/*
+ * Write a special marker.
+ * This is only recommended for writing COM or APPn markers.
+ * Must be called after jpeg_start_compress() and before
+ * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
+ */
+
+GLOBAL void
+jpeg_write_marker (j_compress_ptr cinfo, int marker,
+		   const JOCTET *dataptr, unsigned int datalen)
+{
+  if (cinfo->next_scanline != 0 ||
+      (cinfo->global_state != CSTATE_SCANNING &&
+       cinfo->global_state != CSTATE_RAW_OK))
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  (*cinfo->marker->write_any_marker) (cinfo, marker, dataptr, datalen);
+}
+
+
+/*
+ * Alternate compression function: just write an abbreviated table file.
+ * Before calling this, all parameters and a data destination must be set up.
+ *
+ * To produce a pair of files containing abbreviated tables and abbreviated
+ * image data, one would proceed as follows:
+ *
+ *		initialize JPEG object
+ *		set JPEG parameters
+ *		set destination to table file
+ *		jpeg_write_tables(cinfo);
+ *		set destination to image file
+ *		jpeg_start_compress(cinfo, FALSE);
+ *		write data...
+ *		jpeg_finish_compress(cinfo);
+ *
+ * jpeg_write_tables has the side effect of marking all tables written
+ * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
+ * will not re-emit the tables unless it is passed write_all_tables=TRUE.
+ */
+
+GLOBAL void
+jpeg_write_tables (j_compress_ptr cinfo)
+{
+  if (cinfo->global_state != CSTATE_START)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  /* (Re)initialize error mgr and destination modules */
+  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+  (*cinfo->dest->init_destination) (cinfo);
+  /* Initialize the marker writer ... bit of a crock to do it here. */
+  jinit_marker_writer(cinfo);
+  /* Write them tables! */
+  (*cinfo->marker->write_tables_only) (cinfo);
+  /* And clean up. */
+  (*cinfo->dest->term_destination) (cinfo);
+  /* We can use jpeg_abort to release memory ... is this necessary? */
+  jpeg_abort((j_common_ptr) cinfo);
+}
+
+
+/*
+ * Abort processing of a JPEG compression operation,
+ * but don't destroy the object itself.
+ */
+
+GLOBAL void
+jpeg_abort_compress (j_compress_ptr cinfo)
+{
+  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
+}
diff --git a/jcarith.c b/jcarith.c
deleted file mode 100644
index 108a9fb..0000000
--- a/jcarith.c
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * jcarith.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains arithmetic entropy encoding routines.
- * These routines are invoked via the methods entropy_encode,
- * entropy_encode_init/term, and entropy_optimize.
- */
-
-#include "jinclude.h"
-
-#ifdef C_ARITH_CODING_SUPPORTED
-
-
-/*
- * The arithmetic coding option of the JPEG standard specifies Q-coding,
- * which is covered by patents held by IBM (and possibly AT&T and Mitsubishi).
- * At this time it does not appear to be legal for the Independent JPEG
- * Group to distribute software that implements arithmetic coding.
- * We have therefore removed arithmetic coding support from the
- * distributed source code.
- *
- * We're not happy about it either.
- */
-
-
-/*
- * The method selection routine for arithmetic entropy encoding.
- */
-
-GLOBAL void
-jselcarithmetic (compress_info_ptr cinfo)
-{
-  if (cinfo->arith_code) {
-    ERREXIT(cinfo->emethods, "Sorry, there are legal restrictions on arithmetic coding");
-  }
-}
-
-#endif /* C_ARITH_CODING_SUPPORTED */
diff --git a/jccoefct.c b/jccoefct.c
new file mode 100644
index 0000000..2ca1f37
--- /dev/null
+++ b/jccoefct.c
@@ -0,0 +1,414 @@
+/*
+ * jccoefct.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the coefficient buffer controller for compression.
+ * This controller is the top level of the JPEG compressor proper.
+ * The coefficient buffer lies between forward-DCT and entropy encoding steps.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* We use a full-image coefficient buffer when doing Huffman optimization,
+ * and also for writing multiple-scan JPEG files.  In all cases, the DCT
+ * step is run during the first pass, and subsequent passes need only read
+ * the buffered coefficients.
+ */
+#ifdef ENTROPY_OPT_SUPPORTED
+#define FULL_COEF_BUFFER_SUPPORTED
+#else
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+#define FULL_COEF_BUFFER_SUPPORTED
+#endif
+#endif
+
+
+/* Private buffer controller object */
+
+typedef struct {
+  struct jpeg_c_coef_controller pub; /* public fields */
+
+  JDIMENSION MCU_row_num;	/* keep track of MCU row # within image */
+
+  /* For single-pass compression, it's sufficient to buffer just one MCU
+   * (although this may prove a bit slow in practice).  We allocate a
+   * workspace of MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
+   * MCU constructed and sent.  (On 80x86, the workspace is FAR even though
+   * it's not really very big; this is to keep the module interfaces unchanged
+   * when a large coefficient buffer is necessary.)
+   * In multi-pass modes, this array points to the current MCU's blocks
+   * within the virtual arrays.
+   */
+  JBLOCKROW MCU_buffer[MAX_BLOCKS_IN_MCU];
+
+  /* In multi-pass modes, we need a virtual block array for each component. */
+  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
+} my_coef_controller;
+
+typedef my_coef_controller * my_coef_ptr;
+
+
+/* Forward declarations */
+METHODDEF void compress_data
+    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr));
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+METHODDEF void compress_first_pass
+    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr));
+METHODDEF void compress_output
+    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr));
+#endif
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF void
+start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+  coef->MCU_row_num = 0;
+
+  switch (pass_mode) {
+  case JBUF_PASS_THRU:
+    if (coef->whole_image[0] != NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    coef->pub.compress_data = compress_data;
+    break;
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+  case JBUF_SAVE_AND_PASS:
+    if (coef->whole_image[0] == NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    coef->pub.compress_data = compress_first_pass;
+    break;
+  case JBUF_CRANK_DEST:
+    if (coef->whole_image[0] == NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    coef->pub.compress_data = compress_output;
+    break;
+#endif
+  default:
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    break;
+  }
+}
+
+
+/*
+ * Process some data in the single-pass case.
+ * Up to one MCU row is processed (less if suspension is forced).
+ *
+ * NB: input_buf contains a plane for each component in image.
+ * For single pass, this is the same as the components in the scan.
+ */
+
+METHODDEF void
+compress_data (j_compress_ptr cinfo,
+	       JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr)
+{
+  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+  JDIMENSION MCU_col_num;	/* index of current MCU within row */
+  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
+  JDIMENSION last_MCU_row = cinfo->MCU_rows_in_scan - 1;
+  int blkn, bi, ci, yindex, blockcnt;
+  JDIMENSION ypos, xpos;
+  jpeg_component_info *compptr;
+
+  /* Loop to write as much as one whole MCU row */
+
+  for (MCU_col_num = *in_mcu_ctr; MCU_col_num <= last_MCU_col; MCU_col_num++) {
+    /* Determine where data comes from in input_buf and do the DCT thing.
+     * Each call on forward_DCT processes a horizontal row of DCT blocks
+     * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
+     * sequentially.  Dummy blocks at the right or bottom edge are filled in
+     * specially.  The data in them does not matter for image reconstruction,
+     * so we fill them with values that will encode to the smallest amount of
+     * data, viz: all zeroes in the AC entries, DC entries equal to previous
+     * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
+     */
+    blkn = 0;
+    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+      compptr = cinfo->cur_comp_info[ci];
+      blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+					      : compptr->last_col_width;
+      xpos = MCU_col_num * compptr->MCU_sample_width;
+      ypos = 0;
+      for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+	if (coef->MCU_row_num < last_MCU_row ||
+	    yindex < compptr->last_row_height) {
+	  (*cinfo->fdct->forward_DCT) (cinfo, compptr,
+				       input_buf[ci], coef->MCU_buffer[blkn],
+				       ypos, xpos, (JDIMENSION) blockcnt);
+	  if (blockcnt < compptr->MCU_width) {
+	    /* Create some dummy blocks at the right edge of the image. */
+	    jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
+		      (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
+	    for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
+	      coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
+	    }
+	  }
+	} else {
+	  /* Create a whole row of dummy blocks at the bottom of the image. */
+	  jzero_far((void FAR *) coef->MCU_buffer[blkn],
+		    compptr->MCU_width * SIZEOF(JBLOCK));
+	  for (bi = 0; bi < compptr->MCU_width; bi++) {
+	    coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
+	  }
+	}
+	blkn += compptr->MCU_width;
+	ypos += DCTSIZE;
+      }
+    }
+    /* Try to write the MCU.  In event of a suspension failure, we will
+     * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
+     */
+    if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer))
+      break;			/* suspension forced; exit loop */
+  }
+  if (MCU_col_num > last_MCU_col)
+    coef->MCU_row_num++;	/* advance if we finished the row */
+  *in_mcu_ctr = MCU_col_num;
+}
+
+
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+
+/*
+ * Process some data in the first pass of a multi-pass case.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the image.
+ * This amount of data is read from the source buffer, DCT'd and quantized,
+ * and saved into the virtual arrays.  We also generate suitable dummy blocks
+ * as needed at the right and lower edges.  (The dummy blocks are constructed
+ * in the virtual arrays, which have been padded appropriately.)  This makes
+ * it possible for subsequent passes not to worry about real vs. dummy blocks.
+ *
+ * We must also emit the data to the entropy encoder.  This is conveniently
+ * done by calling compress_output() after we've loaded the current strip
+ * of the virtual arrays.
+ *
+ * NB: input_buf contains a plane for each component in image.  All
+ * components are DCT'd and loaded into the virtual arrays in this pass.
+ * However, it may be that only a subset of the components are emitted to
+ * the entropy encoder during this first pass; be careful about looking
+ * at the scan-dependent variables (MCU dimensions, etc).
+ */
+
+METHODDEF void
+compress_first_pass (j_compress_ptr cinfo,
+		     JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr)
+{
+  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+  JDIMENSION last_MCU_row = cinfo->total_iMCU_rows - 1;
+  JDIMENSION blocks_across, MCUs_across, MCUindex;
+  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
+  JCOEF lastDC;
+  jpeg_component_info *compptr;
+  JBLOCKARRAY buffer;
+  JBLOCKROW thisblockrow, lastblockrow;
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    /* Align the virtual buffer for this component. */
+    buffer = (*cinfo->mem->access_virt_barray)
+      ((j_common_ptr) cinfo, coef->whole_image[ci],
+       coef->MCU_row_num * compptr->v_samp_factor, TRUE);
+    /* Count non-dummy DCT block rows in this iMCU row. */
+    if (coef->MCU_row_num < last_MCU_row)
+      block_rows = compptr->v_samp_factor;
+    else {
+      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+      if (block_rows == 0) block_rows = compptr->v_samp_factor;
+    }
+    blocks_across = compptr->width_in_blocks;
+    h_samp_factor = compptr->h_samp_factor;
+    /* Count number of dummy blocks to be added at the right margin. */
+    ndummy = (int) (blocks_across % h_samp_factor);
+    if (ndummy > 0)
+      ndummy = h_samp_factor - ndummy;
+    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
+     * on forward_DCT processes a complete horizontal row of DCT blocks.
+     */
+    for (block_row = 0; block_row < block_rows; block_row++) {
+      thisblockrow = buffer[block_row];
+      (*cinfo->fdct->forward_DCT) (cinfo, compptr,
+				   input_buf[ci], thisblockrow,
+				   (JDIMENSION) (block_row * DCTSIZE),
+				   (JDIMENSION) 0, blocks_across);
+      if (ndummy > 0) {
+	/* Create dummy blocks at the right edge of the image. */
+	thisblockrow += blocks_across; /* => first dummy block */
+	jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
+	lastDC = thisblockrow[-1][0];
+	for (bi = 0; bi < ndummy; bi++) {
+	  thisblockrow[bi][0] = lastDC;
+	}
+      }
+    }
+    /* If at end of image, create dummy block rows as needed.
+     * The tricky part here is that within each MCU, we want the DC values
+     * of the dummy blocks to match the last real block's DC value.
+     * This squeezes a few more bytes out of the resulting file...
+     */
+    if (coef->MCU_row_num == last_MCU_row) {
+      blocks_across += ndummy;	/* include lower right corner */
+      MCUs_across = blocks_across / h_samp_factor;
+      for (block_row = block_rows; block_row < compptr->v_samp_factor;
+	   block_row++) {
+	thisblockrow = buffer[block_row];
+	lastblockrow = buffer[block_row-1];
+	jzero_far((void FAR *) thisblockrow,
+		  (size_t) (blocks_across * SIZEOF(JBLOCK)));
+	for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
+	  lastDC = lastblockrow[h_samp_factor-1][0];
+	  for (bi = 0; bi < h_samp_factor; bi++) {
+	    thisblockrow[bi][0] = lastDC;
+	  }
+	  thisblockrow += h_samp_factor; /* advance to next MCU in row */
+	  lastblockrow += h_samp_factor;
+	}
+      }
+    }
+  }
+  /* NB: compress_output will increment MCU_row_num */
+
+  /* Emit data to the entropy encoder, sharing code with subsequent passes */
+  compress_output(cinfo, input_buf, in_mcu_ctr);
+}
+
+
+/*
+ * Process some data in subsequent passes of a multi-pass case.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the scan.
+ * The data is obtained from the virtual arrays and fed to the entropy coder.
+ *
+ * Note that output suspension is not supported during multi-pass operation,
+ * so the complete MCU row will always be emitted to the entropy encoder
+ * before returning.
+ *
+ * NB: input_buf is ignored; it is likely to be a NULL pointer.
+ */
+
+METHODDEF void
+compress_output (j_compress_ptr cinfo,
+		 JSAMPIMAGE input_buf, JDIMENSION *in_mcu_ctr)
+{
+  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+  JDIMENSION MCU_col_num;	/* index of current MCU within row */
+  int blkn, ci, xindex, yindex, yoffset, num_MCU_rows;
+  JDIMENSION remaining_rows, start_col;
+  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
+  JBLOCKROW buffer_ptr;
+  jpeg_component_info *compptr;
+
+  /* Align the virtual buffers for the components used in this scan.
+   * NB: during first pass, this is safe only because the buffers will
+   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
+   */
+  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+    compptr = cinfo->cur_comp_info[ci];
+    buffer[ci] = (*cinfo->mem->access_virt_barray)
+      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
+       coef->MCU_row_num * compptr->v_samp_factor, FALSE);
+  }
+
+  /* In an interleaved scan, we process exactly one MCU row.
+   * In a noninterleaved scan, we need to process v_samp_factor MCU rows,
+   * each of which contains a single block row.
+   */
+  if (cinfo->comps_in_scan == 1) {
+    compptr = cinfo->cur_comp_info[0];
+    num_MCU_rows = compptr->v_samp_factor;
+    /* but watch out for the bottom of the image */
+    remaining_rows = cinfo->MCU_rows_in_scan -
+		     coef->MCU_row_num * compptr->v_samp_factor;
+    if (remaining_rows < (JDIMENSION) num_MCU_rows)
+      num_MCU_rows = (int) remaining_rows;
+  } else {
+    num_MCU_rows = 1;
+  }
+
+  /* Loop to process one whole iMCU row */
+  for (yoffset = 0; yoffset < num_MCU_rows; yoffset++) {
+    for (MCU_col_num = 0; MCU_col_num < cinfo->MCUs_per_row; MCU_col_num++) {
+      /* Construct list of pointers to DCT blocks belonging to this MCU */
+      blkn = 0;			/* index of current DCT block within MCU */
+      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+	compptr = cinfo->cur_comp_info[ci];
+	start_col = MCU_col_num * compptr->MCU_width;
+	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
+	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
+	    coef->MCU_buffer[blkn++] = buffer_ptr++;
+	  }
+	}
+      }
+      /* Try to write the MCU. */
+      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
+	ERREXIT(cinfo, JERR_CANT_SUSPEND); /* not supported */
+      }
+    }
+  }
+
+  coef->MCU_row_num++;		/* advance to next iMCU row */
+  *in_mcu_ctr = cinfo->MCUs_per_row;
+}
+
+#endif /* FULL_COEF_BUFFER_SUPPORTED */
+
+
+/*
+ * Initialize coefficient buffer controller.
+ */
+
+GLOBAL void
+jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
+{
+  my_coef_ptr coef;
+  int ci, i;
+  jpeg_component_info *compptr;
+  JBLOCKROW buffer;
+
+  coef = (my_coef_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_coef_controller));
+  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
+  coef->pub.start_pass = start_pass_coef;
+
+  /* Create the coefficient buffer. */
+  if (need_full_buffer) {
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+    /* Allocate a full-image virtual array for each component, */
+    /* padded to a multiple of samp_factor DCT blocks in each direction. */
+    /* Note memmgr implicitly pads the vertical direction. */
+    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	 ci++, compptr++) {
+      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
+	((j_common_ptr) cinfo, JPOOL_IMAGE,
+	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+				(long) compptr->h_samp_factor),
+	 compptr->height_in_blocks,
+	 (JDIMENSION) compptr->v_samp_factor);
+    }
+#else
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+  } else {
+    /* We only need a single-MCU buffer. */
+    buffer = (JBLOCKROW)
+      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+    for (i = 0; i < MAX_BLOCKS_IN_MCU; i++) {
+      coef->MCU_buffer[i] = buffer + i;
+    }
+    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
+  }
+}
diff --git a/jccolor.c b/jccolor.c
index 0da5e1b..4ab3d7e 100644
--- a/jccolor.c
+++ b/jccolor.c
@@ -1,19 +1,28 @@
 /*
  * jccolor.c
  *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains input colorspace conversion routines.
- * These routines are invoked via the methods get_sample_rows
- * and colorin_init/term.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
 
 
-static JSAMPARRAY pixel_row;	/* Workspace for a pixel row in input format */
+/* Private subobject */
+
+typedef struct {
+  struct jpeg_color_converter pub; /* public fields */
+
+  /* Private state for RGB->YCC conversion */
+  INT32 * rgb_ycc_tab;		/* => table for RGB to YCbCr conversion */
+} my_color_converter;
+
+typedef my_color_converter * my_cconvert_ptr;
 
 
 /**************** RGB -> YCbCr conversion: most common case **************/
@@ -41,11 +50,7 @@
  * in the tables to save adding them separately in the inner loop.
  */
 
-#ifdef SIXTEEN_BIT_SAMPLES
-#define SCALEBITS	14	/* avoid overflow */
-#else
-#define SCALEBITS	16	/* speedier right-shift on some machines */
-#endif
+#define SCALEBITS	16	/* speediest right-shift on some machines */
 #define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
 #define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
 
@@ -55,7 +60,6 @@
  * machines (more than can hold all eight addresses, anyway).
  */
 
-static INT32 * rgb_ycc_tab;	/* => table for RGB to YCbCr conversion */
 #define R_Y_OFF		0			/* offset to R => Y section */
 #define G_Y_OFF		(1*(MAXJSAMPLE+1))	/* offset to G => Y section */
 #define B_Y_OFF		(2*(MAXJSAMPLE+1))	/* etc. */
@@ -69,21 +73,20 @@
 
 
 /*
- * Initialize for colorspace conversion.
+ * Initialize for RGB->YCC colorspace conversion.
  */
 
 METHODDEF void
-rgb_ycc_init (compress_info_ptr cinfo)
+rgb_ycc_start (j_compress_ptr cinfo)
 {
+  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+  INT32 * rgb_ycc_tab;
   INT32 i;
 
-  /* Allocate a workspace for the result of get_input_row. */
-  pixel_row = (*cinfo->emethods->alloc_small_sarray)
-		(cinfo->image_width, (long) cinfo->input_components);
-
   /* Allocate and fill in the conversion tables. */
-  rgb_ycc_tab = (INT32 *) (*cinfo->emethods->alloc_small)
-				(TABLE_SIZE * SIZEOF(INT32));
+  cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(TABLE_SIZE * SIZEOF(INT32)));
 
   for (i = 0; i <= MAXJSAMPLE; i++) {
     rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
@@ -102,40 +105,41 @@
 
 
 /*
- * Fetch some rows of pixels from get_input_row and convert to the
- * JPEG colorspace.
+ * Convert some rows of samples to the JPEG colorspace.
+ *
+ * Note that we change from the application's interleaved-pixel format
+ * to our internal noninterleaved, one-plane-per-component format.
+ * The input buffer is therefore three times as wide as the output buffer.
+ *
+ * A starting row offset is provided only for the output buffer.  The caller
+ * can easily adjust the passed input_buf value to accommodate any row
+ * offset required on that side.
  */
 
 METHODDEF void
-get_rgb_ycc_rows (compress_info_ptr cinfo,
-		  int rows_to_read, JSAMPIMAGE image_data)
+rgb_ycc_convert (j_compress_ptr cinfo,
+		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+		 JDIMENSION output_row, int num_rows)
 {
-#ifdef SIXTEEN_BIT_SAMPLES
-  register UINT16 r, g, b;
-#else
+  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   register int r, g, b;
-#endif
-  register INT32 * ctab = rgb_ycc_tab;
-  register JSAMPROW inptr0, inptr1, inptr2;
+  register INT32 * ctab = cconvert->rgb_ycc_tab;
+  register JSAMPROW inptr;
   register JSAMPROW outptr0, outptr1, outptr2;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
+  register JDIMENSION col;
+  JDIMENSION num_cols = cinfo->image_width;
 
-  for (row = 0; row < rows_to_read; row++) {
-    /* Read one row from the source file */
-    (*cinfo->methods->get_input_row) (cinfo, pixel_row);
-    /* Convert colorspace */
-    inptr0 = pixel_row[0];
-    inptr1 = pixel_row[1];
-    inptr2 = pixel_row[2];
-    outptr0 = image_data[0][row];
-    outptr1 = image_data[1][row];
-    outptr2 = image_data[2][row];
-    for (col = 0; col < width; col++) {
-      r = GETJSAMPLE(inptr0[col]);
-      g = GETJSAMPLE(inptr1[col]);
-      b = GETJSAMPLE(inptr2[col]);
+  while (--num_rows >= 0) {
+    inptr = *input_buf++;
+    outptr0 = output_buf[0][output_row];
+    outptr1 = output_buf[1][output_row];
+    outptr2 = output_buf[2][output_row];
+    output_row++;
+    for (col = 0; col < num_cols; col++) {
+      r = GETJSAMPLE(inptr[RGB_RED]);
+      g = GETJSAMPLE(inptr[RGB_GREEN]);
+      b = GETJSAMPLE(inptr[RGB_BLUE]);
+      inptr += RGB_PIXELSIZE;
       /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
        * must be too; we do not need an explicit range-limiting operation.
        * Hence the value being shifted is never negative, and we don't
@@ -162,46 +166,34 @@
 
 
 /*
- * Fetch some rows of pixels from get_input_row and convert to the
- * JPEG colorspace.
+ * Convert some rows of samples to the JPEG colorspace.
  * This version handles RGB->grayscale conversion, which is the same
  * as the RGB->Y portion of RGB->YCbCr.
- * We assume rgb_ycc_init has been called (we only use the Y tables).
+ * We assume rgb_ycc_start has been called (we only use the Y tables).
  */
 
 METHODDEF void
-get_rgb_gray_rows (compress_info_ptr cinfo,
-		   int rows_to_read, JSAMPIMAGE image_data)
+rgb_gray_convert (j_compress_ptr cinfo,
+		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+		  JDIMENSION output_row, int num_rows)
 {
-#ifdef SIXTEEN_BIT_SAMPLES
-  register UINT16 r, g, b;
-#else
+  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   register int r, g, b;
-#endif
-  register INT32 * ctab = rgb_ycc_tab;
-  register JSAMPROW inptr0, inptr1, inptr2;
+  register INT32 * ctab = cconvert->rgb_ycc_tab;
+  register JSAMPROW inptr;
   register JSAMPROW outptr;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
+  register JDIMENSION col;
+  JDIMENSION num_cols = cinfo->image_width;
 
-  for (row = 0; row < rows_to_read; row++) {
-    /* Read one row from the source file */
-    (*cinfo->methods->get_input_row) (cinfo, pixel_row);
-    /* Convert colorspace */
-    inptr0 = pixel_row[0];
-    inptr1 = pixel_row[1];
-    inptr2 = pixel_row[2];
-    outptr = image_data[0][row];
-    for (col = 0; col < width; col++) {
-      r = GETJSAMPLE(inptr0[col]);
-      g = GETJSAMPLE(inptr1[col]);
-      b = GETJSAMPLE(inptr2[col]);
-      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
-       * must be too; we do not need an explicit range-limiting operation.
-       * Hence the value being shifted is never negative, and we don't
-       * need the general RIGHT_SHIFT macro.
-       */
+  while (--num_rows >= 0) {
+    inptr = *input_buf++;
+    outptr = output_buf[0][output_row];
+    output_row++;
+    for (col = 0; col < num_cols; col++) {
+      r = GETJSAMPLE(inptr[RGB_RED]);
+      g = GETJSAMPLE(inptr[RGB_GREEN]);
+      b = GETJSAMPLE(inptr[RGB_BLUE]);
+      inptr += RGB_PIXELSIZE;
       /* Y */
       outptr[col] = (JSAMPLE)
 		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
@@ -212,150 +204,246 @@
 
 
 /*
- * Initialize for colorspace conversion.
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles Adobe-style CMYK->YCCK conversion,
+ * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
+ * conversion as above, while passing K (black) unchanged.
+ * We assume rgb_ycc_start has been called.
  */
 
 METHODDEF void
-colorin_init (compress_info_ptr cinfo)
+cmyk_ycck_convert (j_compress_ptr cinfo,
+		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+		   JDIMENSION output_row, int num_rows)
 {
-  /* Allocate a workspace for the result of get_input_row. */
-  pixel_row = (*cinfo->emethods->alloc_small_sarray)
-		(cinfo->image_width, (long) cinfo->input_components);
-}
+  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+  register int r, g, b;
+  register INT32 * ctab = cconvert->rgb_ycc_tab;
+  register JSAMPROW inptr;
+  register JSAMPROW outptr0, outptr1, outptr2, outptr3;
+  register JDIMENSION col;
+  JDIMENSION num_cols = cinfo->image_width;
 
-
-/*
- * Fetch some rows of pixels from get_input_row and convert to the
- * JPEG colorspace.
- * This version handles grayscale output with no conversion.
- * The source can be either plain grayscale or YCbCr (since Y == gray).
- */
-
-METHODDEF void
-get_grayscale_rows (compress_info_ptr cinfo,
-		    int rows_to_read, JSAMPIMAGE image_data)
-{
-  int row;
-
-  for (row = 0; row < rows_to_read; row++) {
-    /* Read one row from the source file */
-    (*cinfo->methods->get_input_row) (cinfo, pixel_row);
-    /* Convert colorspace (gamma mapping needed here) */
-    jcopy_sample_rows(pixel_row, 0, image_data[0], row,
-		      1, cinfo->image_width);
-  }
-}
-
-
-/*
- * Fetch some rows of pixels from get_input_row and convert to the
- * JPEG colorspace.
- * This version handles multi-component colorspaces without conversion.
- */
-
-METHODDEF void
-get_noconvert_rows (compress_info_ptr cinfo,
-		    int rows_to_read, JSAMPIMAGE image_data)
-{
-  int row, ci;
-
-  for (row = 0; row < rows_to_read; row++) {
-    /* Read one row from the source file */
-    (*cinfo->methods->get_input_row) (cinfo, pixel_row);
-    /* Convert colorspace (gamma mapping needed here) */
-    for (ci = 0; ci < cinfo->input_components; ci++) {
-      jcopy_sample_rows(pixel_row, ci, image_data[ci], row,
-			1, cinfo->image_width);
+  while (--num_rows >= 0) {
+    inptr = *input_buf++;
+    outptr0 = output_buf[0][output_row];
+    outptr1 = output_buf[1][output_row];
+    outptr2 = output_buf[2][output_row];
+    outptr3 = output_buf[3][output_row];
+    output_row++;
+    for (col = 0; col < num_cols; col++) {
+      r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
+      g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
+      b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
+      /* K passes through as-is */
+      outptr3[col] = inptr[3];	/* don't need GETJSAMPLE here */
+      inptr += 4;
+      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
+       * must be too; we do not need an explicit range-limiting operation.
+       * Hence the value being shifted is never negative, and we don't
+       * need the general RIGHT_SHIFT macro.
+       */
+      /* Y */
+      outptr0[col] = (JSAMPLE)
+		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+		 >> SCALEBITS);
+      /* Cb */
+      outptr1[col] = (JSAMPLE)
+		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
+		 >> SCALEBITS);
+      /* Cr */
+      outptr2[col] = (JSAMPLE)
+		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
+		 >> SCALEBITS);
     }
   }
 }
 
 
 /*
- * Finish up at the end of the file.
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles grayscale output with no conversion.
+ * The source can be either plain grayscale or YCbCr (since Y == gray).
  */
 
 METHODDEF void
-colorin_term (compress_info_ptr cinfo)
+grayscale_convert (j_compress_ptr cinfo,
+		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+		   JDIMENSION output_row, int num_rows)
 {
-  /* no work (we let free_all release the workspace) */
+  register JSAMPROW inptr;
+  register JSAMPROW outptr;
+  register JDIMENSION col;
+  JDIMENSION num_cols = cinfo->image_width;
+  int instride = cinfo->input_components;
+
+  while (--num_rows >= 0) {
+    inptr = *input_buf++;
+    outptr = output_buf[0][output_row];
+    output_row++;
+    for (col = 0; col < num_cols; col++) {
+      outptr[col] = inptr[0];	/* don't need GETJSAMPLE() here */
+      inptr += instride;
+    }
+  }
 }
 
 
 /*
- * The method selection routine for input colorspace conversion.
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles multi-component colorspaces without conversion.
+ * We assume input_components == num_components.
+ */
+
+METHODDEF void
+null_convert (j_compress_ptr cinfo,
+	      JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+	      JDIMENSION output_row, int num_rows)
+{
+  register JSAMPROW inptr;
+  register JSAMPROW outptr;
+  register JDIMENSION col;
+  register int ci;
+  int nc = cinfo->num_components;
+  JDIMENSION num_cols = cinfo->image_width;
+
+  while (--num_rows >= 0) {
+    /* It seems fastest to make a separate pass for each component. */
+    for (ci = 0; ci < nc; ci++) {
+      inptr = *input_buf;
+      outptr = output_buf[ci][output_row];
+      for (col = 0; col < num_cols; col++) {
+	outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
+	inptr += nc;
+      }
+    }
+    input_buf++;
+    output_row++;
+  }
+}
+
+
+/*
+ * Empty method for start_pass.
+ */
+
+METHODDEF void
+null_method (j_compress_ptr cinfo)
+{
+  /* no work needed */
+}
+
+
+/*
+ * Module initialization routine for input colorspace conversion.
  */
 
 GLOBAL void
-jselccolor (compress_info_ptr cinfo)
+jinit_color_converter (j_compress_ptr cinfo)
 {
+  my_cconvert_ptr cconvert;
+
+  cconvert = (my_cconvert_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_color_converter));
+  cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
+  /* set start_pass to null method until we find out differently */
+  cconvert->pub.start_pass = null_method;
+
   /* Make sure input_components agrees with in_color_space */
   switch (cinfo->in_color_space) {
-  case CS_GRAYSCALE:
+  case JCS_GRAYSCALE:
     if (cinfo->input_components != 1)
-      ERREXIT(cinfo->emethods, "Bogus input colorspace");
+      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
     break;
 
-  case CS_RGB:
-  case CS_YCbCr:
-  case CS_YIQ:
+  case JCS_RGB:
+#if RGB_PIXELSIZE != 3
+    if (cinfo->input_components != RGB_PIXELSIZE)
+      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+    break;
+#endif /* else share code with YCbCr */
+
+  case JCS_YCbCr:
     if (cinfo->input_components != 3)
-      ERREXIT(cinfo->emethods, "Bogus input colorspace");
+      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
     break;
 
-  case CS_CMYK:
+  case JCS_CMYK:
+  case JCS_YCCK:
     if (cinfo->input_components != 4)
-      ERREXIT(cinfo->emethods, "Bogus input colorspace");
+      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
     break;
 
-  default:
-    ERREXIT(cinfo->emethods, "Unsupported input colorspace");
+  default:			/* JCS_UNKNOWN can be anything */
+    if (cinfo->input_components < 1)
+      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
     break;
   }
 
-  /* Standard init/term methods (may override below) */
-  cinfo->methods->colorin_init = colorin_init;
-  cinfo->methods->colorin_term = colorin_term;
-
   /* Check num_components, set conversion method based on requested space */
   switch (cinfo->jpeg_color_space) {
-  case CS_GRAYSCALE:
+  case JCS_GRAYSCALE:
     if (cinfo->num_components != 1)
-      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
-    if (cinfo->in_color_space == CS_GRAYSCALE)
-      cinfo->methods->get_sample_rows = get_grayscale_rows;
-    else if (cinfo->in_color_space == CS_RGB) {
-      cinfo->methods->colorin_init = rgb_ycc_init;
-      cinfo->methods->get_sample_rows = get_rgb_gray_rows;
-    } else if (cinfo->in_color_space == CS_YCbCr)
-      cinfo->methods->get_sample_rows = get_grayscale_rows;
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+    if (cinfo->in_color_space == JCS_GRAYSCALE)
+      cconvert->pub.color_convert = grayscale_convert;
+    else if (cinfo->in_color_space == JCS_RGB) {
+      cconvert->pub.start_pass = rgb_ycc_start;
+      cconvert->pub.color_convert = rgb_gray_convert;
+    } else if (cinfo->in_color_space == JCS_YCbCr)
+      cconvert->pub.color_convert = grayscale_convert;
     else
-      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
     break;
 
-  case CS_YCbCr:
+  case JCS_RGB:
     if (cinfo->num_components != 3)
-      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
-    if (cinfo->in_color_space == CS_RGB) {
-      cinfo->methods->colorin_init = rgb_ycc_init;
-      cinfo->methods->get_sample_rows = get_rgb_ycc_rows;
-    } else if (cinfo->in_color_space == CS_YCbCr)
-      cinfo->methods->get_sample_rows = get_noconvert_rows;
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+    if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
+      cconvert->pub.color_convert = null_convert;
     else
-      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
     break;
 
-  case CS_CMYK:
+  case JCS_YCbCr:
+    if (cinfo->num_components != 3)
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+    if (cinfo->in_color_space == JCS_RGB) {
+      cconvert->pub.start_pass = rgb_ycc_start;
+      cconvert->pub.color_convert = rgb_ycc_convert;
+    } else if (cinfo->in_color_space == JCS_YCbCr)
+      cconvert->pub.color_convert = null_convert;
+    else
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    break;
+
+  case JCS_CMYK:
     if (cinfo->num_components != 4)
-      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
-    if (cinfo->in_color_space == CS_CMYK)
-      cinfo->methods->get_sample_rows = get_noconvert_rows;
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+    if (cinfo->in_color_space == JCS_CMYK)
+      cconvert->pub.color_convert = null_convert;
     else
-      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
     break;
 
-  default:
-    ERREXIT(cinfo->emethods, "Unsupported JPEG colorspace");
+  case JCS_YCCK:
+    if (cinfo->num_components != 4)
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+    if (cinfo->in_color_space == JCS_CMYK) {
+      cconvert->pub.start_pass = rgb_ycc_start;
+      cconvert->pub.color_convert = cmyk_ycck_convert;
+    } else if (cinfo->in_color_space == JCS_YCCK)
+      cconvert->pub.color_convert = null_convert;
+    else
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    break;
+
+  default:			/* allow null conversion of JCS_UNKNOWN */
+    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
+	cinfo->num_components != cinfo->input_components)
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    cconvert->pub.color_convert = null_convert;
     break;
   }
 }
diff --git a/jcdctmgr.c b/jcdctmgr.c
new file mode 100644
index 0000000..459c3d3
--- /dev/null
+++ b/jcdctmgr.c
@@ -0,0 +1,398 @@
+/*
+ * jcdctmgr.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the forward-DCT management logic.
+ * This code selects a particular DCT implementation to be used,
+ * and it performs related housekeeping chores including coefficient
+ * quantization.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h"		/* Private declarations for DCT subsystem */
+
+
+/* Private subobject for this module */
+
+typedef struct {
+  struct jpeg_forward_dct pub;	/* public fields */
+
+  /* Pointer to the DCT routine actually in use */
+  forward_DCT_method_ptr do_dct;
+
+  /* The actual post-DCT divisors --- not identical to the quant table
+   * entries, because of scaling (especially for an unnormalized DCT).
+   * Each table is given in zigzag order.
+   */
+  DCTELEM * divisors[NUM_QUANT_TBLS];
+
+#ifdef DCT_FLOAT_SUPPORTED
+  /* Same as above for the floating-point case. */
+  float_DCT_method_ptr do_float_dct;
+  FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
+#endif
+} my_fdct_controller;
+
+typedef my_fdct_controller * my_fdct_ptr;
+
+
+/* ZAG[i] is the natural-order position of the i'th element of zigzag order. */
+
+static const int ZAG[DCTSIZE2] = {
+  0,  1,  8, 16,  9,  2,  3, 10,
+ 17, 24, 32, 25, 18, 11,  4,  5,
+ 12, 19, 26, 33, 40, 48, 41, 34,
+ 27, 20, 13,  6,  7, 14, 21, 28,
+ 35, 42, 49, 56, 57, 50, 43, 36,
+ 29, 22, 15, 23, 30, 37, 44, 51,
+ 58, 59, 52, 45, 38, 31, 39, 46,
+ 53, 60, 61, 54, 47, 55, 62, 63
+};
+
+
+/*
+ * Initialize for a processing pass.
+ * Verify that all referenced Q-tables are present, and set up
+ * the divisor table for each one.
+ * In the current implementation, DCT of all components is done during
+ * the first pass, even if only some components will be output in the
+ * first scan.  Hence all components should be examined here.
+ */
+
+METHODDEF void
+start_pass_fdctmgr (j_compress_ptr cinfo)
+{
+  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
+  int ci, qtblno, i;
+  jpeg_component_info *compptr;
+  JQUANT_TBL * qtbl;
+  DCTELEM * dtbl;
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    qtblno = compptr->quant_tbl_no;
+    /* Make sure specified quantization table is present */
+    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
+	cinfo->quant_tbl_ptrs[qtblno] == NULL)
+      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
+    qtbl = cinfo->quant_tbl_ptrs[qtblno];
+    /* Compute divisors for this quant table */
+    /* We may do this more than once for same table, but it's not a big deal */
+    switch (cinfo->dct_method) {
+#ifdef DCT_ISLOW_SUPPORTED
+    case JDCT_ISLOW:
+      /* For LL&M IDCT method, divisors are equal to raw quantization
+       * coefficients multiplied by 8 (to counteract scaling).
+       */
+      if (fdct->divisors[qtblno] == NULL) {
+	fdct->divisors[qtblno] = (DCTELEM *)
+	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				      DCTSIZE2 * SIZEOF(DCTELEM));
+      }
+      dtbl = fdct->divisors[qtblno];
+      for (i = 0; i < DCTSIZE2; i++) {
+	dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
+      }
+      break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+    case JDCT_IFAST:
+      {
+	/* For AA&N IDCT method, divisors are equal to quantization
+	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
+	 *   scalefactor[0] = 1
+	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
+	 * We apply a further scale factor of 8.
+	 */
+#define CONST_BITS 14
+	static const INT16 aanscales[DCTSIZE2] = {
+	  /* precomputed values scaled up by 14 bits: in natural order */
+	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
+	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
+	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
+	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
+	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
+	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
+	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
+	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
+	};
+	SHIFT_TEMPS
+
+	if (fdct->divisors[qtblno] == NULL) {
+	  fdct->divisors[qtblno] = (DCTELEM *)
+	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+					DCTSIZE2 * SIZEOF(DCTELEM));
+	}
+	dtbl = fdct->divisors[qtblno];
+	for (i = 0; i < DCTSIZE2; i++) {
+	  dtbl[i] = (DCTELEM)
+	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
+				  (INT32) aanscales[ZAG[i]]),
+		    CONST_BITS-3);
+	}
+      }
+      break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+    case JDCT_FLOAT:
+      {
+	/* For float AA&N IDCT method, divisors are equal to quantization
+	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
+	 *   scalefactor[0] = 1
+	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
+	 * We apply a further scale factor of 8.
+	 * What's actually stored is 1/divisor so that the inner loop can
+	 * use a multiplication rather than a division.
+	 */
+	FAST_FLOAT * fdtbl;
+	int row, col;
+	static const double aanscalefactor[DCTSIZE] = {
+	  1.0, 1.387039845, 1.306562965, 1.175875602,
+	  1.0, 0.785694958, 0.541196100, 0.275899379
+	};
+
+	if (fdct->float_divisors[qtblno] == NULL) {
+	  fdct->float_divisors[qtblno] = (FAST_FLOAT *)
+	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+					DCTSIZE2 * SIZEOF(FAST_FLOAT));
+	}
+	fdtbl = fdct->float_divisors[qtblno];
+	for (i = 0; i < DCTSIZE2; i++) {
+	  row = ZAG[i] >> 3;
+	  col = ZAG[i] & 7;
+	  fdtbl[i] = (FAST_FLOAT)
+	    (1.0 / (((double) qtbl->quantval[i] *
+		     aanscalefactor[row] * aanscalefactor[col] * 8.0)));
+	}
+      }
+      break;
+#endif
+    default:
+      ERREXIT(cinfo, JERR_NOT_COMPILED);
+      break;
+    }
+  }
+}
+
+
+/*
+ * Perform forward DCT on one or more blocks of a component.
+ *
+ * The input samples are taken from the sample_data[] array starting at
+ * position start_row/start_col, and moving to the right for any additional
+ * blocks. The quantized, zigzagged coefficients are returned in coef_blocks[].
+ */
+
+METHODDEF void
+forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
+	     JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+	     JDIMENSION start_row, JDIMENSION start_col,
+	     JDIMENSION num_blocks)
+/* This version is used for integer DCT implementations. */
+{
+  /* This routine is heavily used, so it's worth coding it tightly. */
+  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
+  forward_DCT_method_ptr do_dct = fdct->do_dct;
+  DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
+  DCTELEM workspace[DCTSIZE2];	/* work area for FDCT subroutine */
+  JDIMENSION bi;
+
+  sample_data += start_row;	/* fold in the vertical offset once */
+
+  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
+    /* Load data into workspace, applying unsigned->signed conversion */
+    { register DCTELEM *workspaceptr;
+      register JSAMPROW elemptr;
+      register int elemr;
+
+      workspaceptr = workspace;
+      for (elemr = 0; elemr < DCTSIZE; elemr++) {
+	elemptr = sample_data[elemr] + start_col;
+#if DCTSIZE == 8		/* unroll the inner loop */
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+#else
+	{ register int elemc;
+	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
+	    *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	  }
+	}
+#endif
+      }
+    }
+
+    /* Perform the DCT */
+    (*do_dct) (workspace);
+
+    /* Quantize/descale the coefficients, and store into coef_blocks[] */
+    { register DCTELEM temp, qval;
+      register int i;
+      register JCOEFPTR output_ptr = coef_blocks[bi];
+
+      for (i = 0; i < DCTSIZE2; i++) {
+	qval = divisors[i];
+	temp = workspace[ZAG[i]];
+	/* Divide the coefficient value by qval, ensuring proper rounding.
+	 * Since C does not specify the direction of rounding for negative
+	 * quotients, we have to force the dividend positive for portability.
+	 *
+	 * In most files, at least half of the output values will be zero
+	 * (at default quantization settings, more like three-quarters...)
+	 * so we should ensure that this case is fast.  On many machines,
+	 * a comparison is enough cheaper than a divide to make a special test
+	 * a win.  Since both inputs will be nonnegative, we need only test
+	 * for a < b to discover whether a/b is 0.
+	 * If your machine's division is fast enough, define FAST_DIVIDE.
+	 */
+#ifdef FAST_DIVIDE
+#define DIVIDE_BY(a,b)	a /= b
+#else
+#define DIVIDE_BY(a,b)	if (a >= b) a /= b; else a = 0
+#endif
+	if (temp < 0) {
+	  temp = -temp;
+	  temp += qval>>1;	/* for rounding */
+	  DIVIDE_BY(temp, qval);
+	  temp = -temp;
+	} else {
+	  temp += qval>>1;	/* for rounding */
+	  DIVIDE_BY(temp, qval);
+	}
+	output_ptr[i] = (JCOEF) temp;
+      }
+    }
+  }
+}
+
+
+#ifdef DCT_FLOAT_SUPPORTED
+
+METHODDEF void
+forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
+		   JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+		   JDIMENSION start_row, JDIMENSION start_col,
+		   JDIMENSION num_blocks)
+/* This version is used for floating-point DCT implementations. */
+{
+  /* This routine is heavily used, so it's worth coding it tightly. */
+  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
+  float_DCT_method_ptr do_dct = fdct->do_float_dct;
+  FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
+  FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
+  JDIMENSION bi;
+
+  sample_data += start_row;	/* fold in the vertical offset once */
+
+  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
+    /* Load data into workspace, applying unsigned->signed conversion */
+    { register FAST_FLOAT *workspaceptr;
+      register JSAMPROW elemptr;
+      register int elemr;
+
+      workspaceptr = workspace;
+      for (elemr = 0; elemr < DCTSIZE; elemr++) {
+	elemptr = sample_data[elemr] + start_col;
+#if DCTSIZE == 8		/* unroll the inner loop */
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+#else
+	{ register int elemc;
+	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
+	    *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+	  }
+	}
+#endif
+      }
+    }
+
+    /* Perform the DCT */
+    (*do_dct) (workspace);
+
+    /* Quantize/descale the coefficients, and store into coef_blocks[] */
+    { register FAST_FLOAT temp;
+      register int i;
+      register JCOEFPTR output_ptr = coef_blocks[bi];
+
+      for (i = 0; i < DCTSIZE2; i++) {
+	/* Apply the quantization and scaling factor */
+	temp = workspace[ZAG[i]] * divisors[i];
+	/* Round to nearest integer.
+	 * Since C does not specify the direction of rounding for negative
+	 * quotients, we have to force the dividend positive for portability.
+	 * The maximum coefficient size is +-16K (for 12-bit data), so this
+	 * code should work for either 16-bit or 32-bit ints.
+	 */
+	output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
+      }
+    }
+  }
+}
+
+#endif /* DCT_FLOAT_SUPPORTED */
+
+
+/*
+ * Initialize FDCT manager.
+ */
+
+GLOBAL void
+jinit_forward_dct (j_compress_ptr cinfo)
+{
+  my_fdct_ptr fdct;
+  int i;
+
+  fdct = (my_fdct_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_fdct_controller));
+  cinfo->fdct = (struct jpeg_forward_dct *) fdct;
+  fdct->pub.start_pass = start_pass_fdctmgr;
+
+  switch (cinfo->dct_method) {
+#ifdef DCT_ISLOW_SUPPORTED
+  case JDCT_ISLOW:
+    fdct->pub.forward_DCT = forward_DCT;
+    fdct->do_dct = jpeg_fdct_islow;
+    break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+  case JDCT_IFAST:
+    fdct->pub.forward_DCT = forward_DCT;
+    fdct->do_dct = jpeg_fdct_ifast;
+    break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+  case JDCT_FLOAT:
+    fdct->pub.forward_DCT = forward_DCT_float;
+    fdct->do_float_dct = jpeg_fdct_float;
+    break;
+#endif
+  default:
+    ERREXIT(cinfo, JERR_NOT_COMPILED);
+    break;
+  }
+
+  /* Mark divisor tables unallocated */
+  for (i = 0; i < NUM_QUANT_TBLS; i++) {
+    fdct->divisors[i] = NULL;
+#ifdef DCT_FLOAT_SUPPORTED
+    fdct->float_divisors[i] = NULL;
+#endif
+  }
+}
diff --git a/jcdeflts.c b/jcdeflts.c
deleted file mode 100644
index eddba20..0000000
--- a/jcdeflts.c
+++ /dev/null
@@ -1,391 +0,0 @@
-/*
- * jcdeflts.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains optional default-setting code for the JPEG compressor.
- * User interfaces do not have to use this file, but those that don't use it
- * must know a lot more about the innards of the JPEG code.
- */
-
-#include "jinclude.h"
-
-
-/* Default do-nothing progress monitoring routine.
- * This can be overridden by a user interface that wishes to
- * provide progress monitoring; just set methods->progress_monitor
- * after j_c_defaults is done.  The routine will be called periodically
- * during the compression process.
- *
- * During any one pass, loopcounter increases from 0 up to (not including)
- * looplimit; the step size is not necessarily 1.  Both the step size and
- * the limit may differ between passes.  The expected total number of passes
- * is in cinfo->total_passes, and the number of passes already completed is
- * in cinfo->completed_passes.  Thus the fraction of work completed may be
- * estimated as
- *		completed_passes + (loopcounter/looplimit)
- *		------------------------------------------
- *				total_passes
- * ignoring the fact that the passes may not be equal amounts of work.
- */
-
-METHODDEF void
-progress_monitor (compress_info_ptr cinfo, long loopcounter, long looplimit)
-{
-  /* do nothing */
-}
-
-
-/*
- * Huffman table setup routines
- */
-
-LOCAL void
-add_huff_table (compress_info_ptr cinfo,
-		HUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
-/* Define a Huffman table */
-{
-  if (*htblptr == NULL)
-    *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
-  
-  MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
-  MEMCOPY((*htblptr)->huffval, val, SIZEOF((*htblptr)->huffval));
-
-  /* Initialize sent_table FALSE so table will be written to JPEG file.
-   * In an application where we are writing non-interchange JPEG files,
-   * it might be desirable to save space by leaving default Huffman tables
-   * out of the file.  To do that, just initialize sent_table = TRUE...
-   */
-
-  (*htblptr)->sent_table = FALSE;
-}
-
-
-LOCAL void
-std_huff_tables (compress_info_ptr cinfo)
-/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
-/* IMPORTANT: these are only valid for 8-bit data precision! */
-{
-  static const UINT8 dc_luminance_bits[17] =
-    { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
-  static const UINT8 dc_luminance_val[] =
-    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
-  
-  static const UINT8 dc_chrominance_bits[17] =
-    { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
-  static const UINT8 dc_chrominance_val[] =
-    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
-  
-  static const UINT8 ac_luminance_bits[17] =
-    { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
-  static const UINT8 ac_luminance_val[] =
-    { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
-      0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
-      0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
-      0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
-      0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
-      0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
-      0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
-      0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
-      0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
-      0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
-      0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
-      0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
-      0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
-      0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
-      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
-      0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
-      0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
-      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
-      0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
-      0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
-      0xf9, 0xfa };
-  
-  static const UINT8 ac_chrominance_bits[17] =
-    { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
-  static const UINT8 ac_chrominance_val[] =
-    { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
-      0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
-      0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
-      0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
-      0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
-      0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
-      0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
-      0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
-      0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
-      0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
-      0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
-      0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
-      0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
-      0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
-      0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
-      0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
-      0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
-      0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
-      0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
-      0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
-      0xf9, 0xfa };
-  
-  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
-		 dc_luminance_bits, dc_luminance_val);
-  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
-		 ac_luminance_bits, ac_luminance_val);
-  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
-		 dc_chrominance_bits, dc_chrominance_val);
-  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
-		 ac_chrominance_bits, ac_chrominance_val);
-}
-
-
-/*
- * Quantization table setup routines
- */
-
-GLOBAL void
-j_add_quant_table (compress_info_ptr cinfo, int which_tbl,
-		   const QUANT_VAL *basic_table, int scale_factor,
-		   boolean force_baseline)
-/* Define a quantization table equal to the basic_table times
- * a scale factor (given as a percentage).
- * If force_baseline is TRUE, the computed quantization table entries
- * are limited to 1..255 for JPEG baseline compatibility.
- */
-{
-  QUANT_TBL_PTR * qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
-  int i;
-  long temp;
-
-  if (*qtblptr == NULL)
-    *qtblptr = (QUANT_TBL_PTR) (*cinfo->emethods->alloc_small) (SIZEOF(QUANT_TBL));
-
-  for (i = 0; i < DCTSIZE2; i++) {
-    temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
-    /* limit the values to the valid range */
-    if (temp <= 0L) temp = 1L;
-#ifdef EIGHT_BIT_SAMPLES
-    if (temp > 32767L) temp = 32767L; /* QUANT_VALs are 'short' */
-#else
-    if (temp > 65535L) temp = 65535L; /* QUANT_VALs are 'UINT16' */
-#endif
-    if (force_baseline && temp > 255L)
-      temp = 255L;		/* limit to baseline range if requested */
-    (*qtblptr)[i] = (QUANT_VAL) temp;
-  }
-}
-
-
-GLOBAL int
-j_quality_scaling (int quality)
-/* Convert a user-specified quality rating to a percentage scaling factor
- * for an underlying quantization table, using our recommended scaling curve.
- * The input 'quality' factor should be 0 (terrible) to 100 (very good).
- */
-{
-  /* Safety limit on quality factor.  Convert 0 to 1 to avoid zero divide. */
-  if (quality <= 0) quality = 1;
-  if (quality > 100) quality = 100;
-
-  /* The basic table is used as-is (scaling 100) for a quality of 50.
-   * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
-   * note that at Q=100 the scaling is 0, which will cause j_add_quant_table
-   * to make all the table entries 1 (hence, no quantization loss).
-   * Qualities 1..50 are converted to scaling percentage 5000/Q.
-   */
-  if (quality < 50)
-    quality = 5000 / quality;
-  else
-    quality = 200 - quality*2;
-
-  return quality;
-}
-
-
-GLOBAL void
-j_set_quality (compress_info_ptr cinfo, int quality, boolean force_baseline)
-/* Set or change the 'quality' (quantization) setting, using default tables.
- * This is the standard quality-adjusting entry point for typical user
- * interfaces; only those who want detailed control over quantization tables
- * would use the preceding two routines directly.
- */
-{
-  /* This is the sample quantization table given in the JPEG spec section K.1,
-   * but expressed in zigzag order (as are all of our quant. tables).
-   * The spec says that the values given produce "good" quality, and
-   * when divided by 2, "very good" quality.  (These two settings are
-   * selected by quality=50 and quality=75 respectively.)
-   */
-  static const QUANT_VAL std_luminance_quant_tbl[DCTSIZE2] = {
-    16,  11,  12,  14,  12,  10,  16,  14,
-    13,  14,  18,  17,  16,  19,  24,  40,
-    26,  24,  22,  22,  24,  49,  35,  37,
-    29,  40,  58,  51,  61,  60,  57,  51,
-    56,  55,  64,  72,  92,  78,  64,  68,
-    87,  69,  55,  56,  80, 109,  81,  87,
-    95,  98, 103, 104, 103,  62,  77, 113,
-    121, 112, 100, 120,  92, 101, 103,  99
-    };
-  static const QUANT_VAL std_chrominance_quant_tbl[DCTSIZE2] = {
-    17,  18,  18,  24,  21,  24,  47,  26,
-    26,  47,  99,  66,  56,  66,  99,  99,
-    99,  99,  99,  99,  99,  99,  99,  99,
-    99,  99,  99,  99,  99,  99,  99,  99,
-    99,  99,  99,  99,  99,  99,  99,  99,
-    99,  99,  99,  99,  99,  99,  99,  99,
-    99,  99,  99,  99,  99,  99,  99,  99,
-    99,  99,  99,  99,  99,  99,  99,  99
-    };
-
-  /* Convert user 0-100 rating to percentage scaling */
-  quality = j_quality_scaling(quality);
-
-  /* Set up two quantization tables using the specified quality scaling */
-  j_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
-		    quality, force_baseline);
-  j_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
-		    quality, force_baseline);
-}
-
-
-
-/* Default parameter setup for compression.
- *
- * User interfaces that don't choose to use this routine must do their
- * own setup of all these parameters.  Alternately, you can call this
- * to establish defaults and then alter parameters selectively.  This
- * is the recommended approach since, if we add any new parameters,
- * your code will still work (they'll be set to reasonable defaults).
- *
- * See above for the meaning of the 'quality' and 'force_baseline' parameters.
- * Typically, the application's default quality setting will be passed to this
- * routine.  A later call on j_set_quality() can be used to change to a
- * user-specified quality setting.
- *
- * This routine sets up for a color image; to output a grayscale image,
- * do this first and call j_monochrome_default() afterwards.
- * (The latter can be called within c_ui_method_selection, so the
- * choice can depend on the input file header.)
- * Note that if you want a JPEG colorspace other than GRAYSCALE or YCbCr,
- * you should also change the component ID codes, and you should NOT emit
- * a JFIF header (set write_JFIF_header = FALSE).
- *
- * CAUTION: if you want to compress multiple images per run, it's necessary
- * to call j_c_defaults before *each* call to jpeg_compress, since subsidiary
- * structures like the Huffman tables are automatically freed during cleanup.
- */
-
-GLOBAL void
-j_c_defaults (compress_info_ptr cinfo, int quality, boolean force_baseline)
-/* NB: the external methods must already be set up. */
-{
-  short i;
-  jpeg_component_info * compptr;
-
-  /* Initialize pointers as needed to mark stuff unallocated. */
-  cinfo->comp_info = NULL;
-  for (i = 0; i < NUM_QUANT_TBLS; i++)
-    cinfo->quant_tbl_ptrs[i] = NULL;
-  for (i = 0; i < NUM_HUFF_TBLS; i++) {
-    cinfo->dc_huff_tbl_ptrs[i] = NULL;
-    cinfo->ac_huff_tbl_ptrs[i] = NULL;
-  }
-
-  cinfo->data_precision = BITS_IN_JSAMPLE; /* default; can be overridden by input_init */
-  cinfo->density_unit = 0;	/* Pixel size is unknown by default */
-  cinfo->X_density = 1;		/* Pixel aspect ratio is square by default */
-  cinfo->Y_density = 1;
-
-  cinfo->input_gamma = 1.0;	/* no gamma correction by default */
-
-  cinfo->write_JFIF_header = TRUE; /* write a JFIF marker */
-  cinfo->comment_text = NULL;	/* but no COM block */
-
-  /* Prepare three color components; first is luminance which is also usable */
-  /* for grayscale.  The others are assumed to be UV or similar chrominance. */
-  cinfo->jpeg_color_space = CS_YCbCr;
-  cinfo->num_components = 3;
-  cinfo->comp_info = (jpeg_component_info *)
-    (*cinfo->emethods->alloc_small) (4 * SIZEOF(jpeg_component_info));
-  /* Note: we allocate a 4-entry comp_info array so that user interface can
-   * easily change over to CMYK color space if desired.
-   */
-
-  compptr = &cinfo->comp_info[0];
-  compptr->component_index = 0;
-  compptr->component_id = 1;	/* JFIF specifies IDs 1,2,3 */
-  compptr->h_samp_factor = 2;	/* default to 2x2 subsamples of chrominance */
-  compptr->v_samp_factor = 2;
-  compptr->quant_tbl_no = 0;	/* use tables 0 for luminance */
-  compptr->dc_tbl_no = 0;
-  compptr->ac_tbl_no = 0;
-
-  compptr = &cinfo->comp_info[1];
-  compptr->component_index = 1;
-  compptr->component_id = 2;
-  compptr->h_samp_factor = 1;
-  compptr->v_samp_factor = 1;
-  compptr->quant_tbl_no = 1;	/* use tables 1 for chrominance */
-  compptr->dc_tbl_no = 1;
-  compptr->ac_tbl_no = 1;
-
-  compptr = &cinfo->comp_info[2];
-  compptr->component_index = 2;
-  compptr->component_id = 3;
-  compptr->h_samp_factor = 1;
-  compptr->v_samp_factor = 1;
-  compptr->quant_tbl_no = 1;	/* use tables 1 for chrominance */
-  compptr->dc_tbl_no = 1;
-  compptr->ac_tbl_no = 1;
-
-  /* Set up two quantization tables using the specified quality scaling */
-  j_set_quality(cinfo, quality, force_baseline);
-
-  /* Set up two Huffman tables in case user interface wants Huffman coding */
-  std_huff_tables(cinfo);
-
-  /* Initialize default arithmetic coding conditioning */
-  for (i = 0; i < NUM_ARITH_TBLS; i++) {
-    cinfo->arith_dc_L[i] = 0;
-    cinfo->arith_dc_U[i] = 1;
-    cinfo->arith_ac_K[i] = 5;
-  }
-
-  /* Use Huffman coding, not arithmetic coding, by default */
-  cinfo->arith_code = FALSE;
-
-  /* Color images are interleaved by default */
-  cinfo->interleave = TRUE;
-
-  /* By default, don't do extra passes to optimize entropy coding */
-  cinfo->optimize_coding = FALSE;
-
-  /* By default, use the simpler non-cosited sampling alignment */
-  cinfo->CCIR601_sampling = FALSE;
-
-  /* No input smoothing */
-  cinfo->smoothing_factor = 0;
-
-  /* No restart markers */
-  cinfo->restart_interval = 0;
-  cinfo->restart_in_rows = 0;
-
-  /* Install default do-nothing progress monitoring method. */
-  cinfo->methods->progress_monitor = progress_monitor;
-}
-
-
-
-GLOBAL void
-j_monochrome_default (compress_info_ptr cinfo)
-/* Change the j_c_defaults() values to emit a monochrome JPEG file. */
-{
-  jpeg_component_info * compptr;
-
-  cinfo->jpeg_color_space = CS_GRAYSCALE;
-  cinfo->num_components = 1;
-  /* Set single component to 1x1 subsampling */
-  compptr = &cinfo->comp_info[0];
-  compptr->h_samp_factor = 1;
-  compptr->v_samp_factor = 1;
-}
diff --git a/jcexpand.c b/jcexpand.c
deleted file mode 100644
index 6b42b71..0000000
--- a/jcexpand.c
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * jcexpand.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains image edge-expansion routines.
- * These routines are invoked via the edge_expand method.
- */
-
-#include "jinclude.h"
-
-
-/*
- * Expand an image so that it is a multiple of the MCU dimensions.
- * This is to be accomplished by duplicating the rightmost column
- * and/or bottommost row of pixels.  The image has not yet been
- * downsampled, so all components have the same dimensions.
- */
-
-METHODDEF void
-edge_expand (compress_info_ptr cinfo,
-	     long input_cols, int input_rows,
-	     long output_cols, int output_rows,
-	     JSAMPIMAGE image_data)
-{
-  /* Expand horizontally */
-  if (input_cols < output_cols) {
-    register JSAMPROW ptr;
-    register JSAMPLE pixval;
-    register long count;
-    register int row;
-    short ci;
-    long numcols = output_cols - input_cols;
-
-    for (ci = 0; ci < cinfo->num_components; ci++) {
-      for (row = 0; row < input_rows; row++) {
-	ptr = image_data[ci][row] + (input_cols-1);
-	pixval = GETJSAMPLE(*ptr++);
-	for (count = numcols; count > 0; count--)
-	  *ptr++ = pixval;
-      }
-    }
-  }
-
-  /* Expand vertically */
-  /* This happens only once at the bottom of the image, */
-  /* so it needn't be super-efficient */
-  if (input_rows < output_rows) {
-    register int row;
-    short ci;
-    JSAMPARRAY this_component;
-
-    for (ci = 0; ci < cinfo->num_components; ci++) {
-      this_component = image_data[ci];
-      for (row = input_rows; row < output_rows; row++) {
-	jcopy_sample_rows(this_component, input_rows-1, this_component, row,
-			  1, output_cols);
-      }
-    }
-  }
-}
-
-
-/*
- * The method selection routine for edge expansion.
- */
-
-GLOBAL void
-jselexpand (compress_info_ptr cinfo)
-{
-  /* just one implementation for now */
-  cinfo->methods->edge_expand = edge_expand;
-}
diff --git a/jchuff.c b/jchuff.c
index 9eb558a..9ddefc5 100644
--- a/jchuff.c
+++ b/jchuff.c
@@ -1,38 +1,200 @@
 /*
  * jchuff.c
  *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains Huffman entropy encoding routines.
- * These routines are invoked via the methods entropy_encode,
- * entropy_encode_init/term, and entropy_optimize.
+ *
+ * Much of the complexity here has to do with supporting output suspension.
+ * If the data destination module demands suspension, we want to be able to
+ * back up to the start of the current MCU.  To do this, we copy state
+ * variables into local working storage, and update them back to the
+ * permanent JPEG objects only upon successful completion of an MCU.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
 
 
-/* Static variables to avoid passing 'round extra parameters */
+/* Derived data constructed for each Huffman table */
 
-static compress_info_ptr cinfo;
+typedef struct {
+  unsigned int ehufco[256];	/* code for each symbol */
+  char ehufsi[256];		/* length of code for each symbol */
+  /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
+} C_DERIVED_TBL;
 
-static INT32 huff_put_buffer;	/* current bit-accumulation buffer */
-static int huff_put_bits;	/* # of bits now in it */
+/* Expanded entropy encoder object for Huffman encoding.
+ *
+ * The savable_state subrecord contains fields that change within an MCU,
+ * but must not be updated permanently until we complete the MCU.
+ */
 
-static char * output_buffer;	/* output buffer */
-static int bytes_in_buffer;
+typedef struct {
+  INT32 put_buffer;		/* current bit-accumulation buffer */
+  int put_bits;			/* # of bits now in it */
+  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
+} savable_state;
 
+/* This macro is to work around compilers with missing or broken
+ * structure assignment.  You'll need to fix this code if you have
+ * such a compiler and you change MAX_COMPS_IN_SCAN.
+ */
+
+#ifndef NO_STRUCT_ASSIGN
+#define ASSIGN_STATE(dest,src)  ((dest) = (src))
+#else
+#if MAX_COMPS_IN_SCAN == 4
+#define ASSIGN_STATE(dest,src)  \
+	((dest).put_buffer = (src).put_buffer, \
+	 (dest).put_bits = (src).put_bits, \
+	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
+	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
+	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
+	 (dest).last_dc_val[3] = (src).last_dc_val[3])
+#endif
+#endif
+
+
+typedef struct {
+  struct jpeg_entropy_encoder pub; /* public fields */
+
+  savable_state saved;		/* Bit buffer & DC state at start of MCU */
+
+  /* These fields are NOT loaded into local working state. */
+  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
+  int next_restart_num;		/* next restart number to write (0-7) */
+
+  /* Pointers to derived tables (these workspaces have image lifespan) */
+  C_DERIVED_TBL * dc_derived_tbls[NUM_HUFF_TBLS];
+  C_DERIVED_TBL * ac_derived_tbls[NUM_HUFF_TBLS];
+
+#ifdef ENTROPY_OPT_SUPPORTED	/* Statistics tables for optimization */
+  long * dc_count_ptrs[NUM_HUFF_TBLS];
+  long * ac_count_ptrs[NUM_HUFF_TBLS];
+#endif
+} huff_entropy_encoder;
+
+typedef huff_entropy_encoder * huff_entropy_ptr;
+
+/* Working state while writing an MCU.
+ * This struct contains all the fields that are needed by subroutines.
+ */
+
+typedef struct {
+  JOCTET * next_output_byte;	/* => next byte to write in buffer */
+  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
+  savable_state cur;		/* Current bit buffer & DC state */
+  j_compress_ptr cinfo;		/* dump_buffer needs access to this */
+} working_state;
+
+
+/* Forward declarations */
+METHODDEF boolean encode_mcu_huff JPP((j_compress_ptr cinfo,
+				       JBLOCKROW *MCU_data));
+METHODDEF void finish_pass_huff JPP((j_compress_ptr cinfo));
+#ifdef ENTROPY_OPT_SUPPORTED
+METHODDEF boolean encode_mcu_gather JPP((j_compress_ptr cinfo,
+					 JBLOCKROW *MCU_data));
+METHODDEF void finish_pass_gather JPP((j_compress_ptr cinfo));
+#endif
+LOCAL void fix_huff_tbl JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl,
+			     C_DERIVED_TBL ** pdtbl));
+
+
+/*
+ * Initialize for a Huffman-compressed scan.
+ * If gather_statistics is TRUE, we do not output anything during the scan,
+ * just count the Huffman symbols used and generate Huffman code tables.
+ */
+
+METHODDEF void
+start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
+{
+  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+  int ci, dctbl, actbl;
+  jpeg_component_info * compptr;
+
+  if (gather_statistics) {
+#ifdef ENTROPY_OPT_SUPPORTED
+    entropy->pub.encode_mcu = encode_mcu_gather;
+    entropy->pub.finish_pass = finish_pass_gather;
+#else
+    ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+  } else {
+    entropy->pub.encode_mcu = encode_mcu_huff;
+    entropy->pub.finish_pass = finish_pass_huff;
+  }
+
+  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+    compptr = cinfo->cur_comp_info[ci];
+    dctbl = compptr->dc_tbl_no;
+    actbl = compptr->ac_tbl_no;
+    /* Make sure requested tables are present */
+    /* (In gather mode, tables need not be allocated yet) */
+    if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS ||
+	(cinfo->dc_huff_tbl_ptrs[dctbl] == NULL && !gather_statistics))
+      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
+    if (actbl < 0 || actbl >= NUM_HUFF_TBLS ||
+	(cinfo->ac_huff_tbl_ptrs[actbl] == NULL && !gather_statistics))
+      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
+    if (gather_statistics) {
+#ifdef ENTROPY_OPT_SUPPORTED
+      /* Allocate and zero the statistics tables */
+      /* Note that gen_huff_coding expects 257 entries in each table! */
+      if (entropy->dc_count_ptrs[dctbl] == NULL)
+	entropy->dc_count_ptrs[dctbl] = (long *)
+	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				      257 * SIZEOF(long));
+      MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
+      if (entropy->ac_count_ptrs[actbl] == NULL)
+	entropy->ac_count_ptrs[actbl] = (long *)
+	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				      257 * SIZEOF(long));
+      MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
+#endif
+    } else {
+      /* Compute derived values for Huffman tables */
+      /* We may do this more than once for a table, but it's not expensive */
+      fix_huff_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[dctbl],
+		   & entropy->dc_derived_tbls[dctbl]);
+      fix_huff_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[actbl],
+		   & entropy->ac_derived_tbls[actbl]);
+    }
+    /* Initialize DC predictions to 0 */
+    entropy->saved.last_dc_val[ci] = 0;
+  }
+
+  /* Initialize bit buffer to empty */
+  entropy->saved.put_buffer = 0;
+  entropy->saved.put_bits = 0;
+
+  /* Initialize restart stuff */
+  entropy->restarts_to_go = cinfo->restart_interval;
+  entropy->next_restart_num = 0;
+}
 
 
 LOCAL void
-fix_huff_tbl (HUFF_TBL * htbl)
-/* Compute derived values for a Huffman table */
+fix_huff_tbl (j_compress_ptr cinfo, JHUFF_TBL * htbl, C_DERIVED_TBL ** pdtbl)
+/* Compute the derived values for a Huffman table */
 {
+  C_DERIVED_TBL *dtbl;
   int p, i, l, lastp, si;
   char huffsize[257];
-  UINT16 huffcode[257];
-  UINT16 code;
+  unsigned int huffcode[257];
+  unsigned int code;
+
+  /* Allocate a workspace if we haven't already done so. */
+  if (*pdtbl == NULL)
+    *pdtbl = (C_DERIVED_TBL *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(C_DERIVED_TBL));
+  dtbl = *pdtbl;
   
   /* Figure C.1: make table of Huffman code length for each symbol */
   /* Note that this is in code-length order. */
@@ -66,92 +228,103 @@
   /* Set any codeless symbols to have code length 0;
    * this allows emit_bits to detect any attempt to emit such symbols.
    */
-  MEMZERO(htbl->priv.enc.ehufsi, SIZEOF(htbl->priv.enc.ehufsi));
+  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
 
   for (p = 0; p < lastp; p++) {
-    htbl->priv.enc.ehufco[htbl->huffval[p]] = huffcode[p];
-    htbl->priv.enc.ehufsi[htbl->huffval[p]] = huffsize[p];
+    dtbl->ehufco[htbl->huffval[p]] = huffcode[p];
+    dtbl->ehufsi[htbl->huffval[p]] = huffsize[p];
   }
 }
 
 
 /* Outputting bytes to the file */
 
-LOCAL void
-flush_bytes (void)
+/* Emit a byte, taking 'action' if must suspend. */
+#define emit_byte(state,val,action)  \
+	{ *(state)->next_output_byte++ = (JOCTET) (val);  \
+	  if (--(state)->free_in_buffer == 0)  \
+	    if (! dump_buffer(state))  \
+	      { action; } }
+
+
+LOCAL boolean
+dump_buffer (working_state * state)
+/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
 {
-  if (bytes_in_buffer)
-    (*cinfo->methods->entropy_output) (cinfo, output_buffer, bytes_in_buffer);
-  bytes_in_buffer = 0;
+  struct jpeg_destination_mgr * dest = state->cinfo->dest;
+
+  if (! (*dest->empty_output_buffer) (state->cinfo))
+    return FALSE;
+  /* After a successful buffer dump, must reset buffer pointers */
+  state->next_output_byte = dest->next_output_byte;
+  state->free_in_buffer = dest->free_in_buffer;
+  return TRUE;
 }
 
 
-#define emit_byte(val)  \
-  MAKESTMT( if (bytes_in_buffer >= JPEG_BUF_SIZE) \
-	      flush_bytes(); \
-	    output_buffer[bytes_in_buffer++] = (char) (val); )
-
-
-
 /* Outputting bits to the file */
 
-/* Only the right 24 bits of huff_put_buffer are used; the valid bits are
+/* Only the right 24 bits of put_buffer are used; the valid bits are
  * left-justified in this part.  At most 16 bits can be passed to emit_bits
- * in one call, and we never retain more than 7 bits in huff_put_buffer
+ * in one call, and we never retain more than 7 bits in put_buffer
  * between calls, so 24 bits are sufficient.
  */
 
 INLINE
-LOCAL void
-emit_bits (UINT16 code, int size)
+LOCAL boolean
+emit_bits (working_state * state, unsigned int code, int size)
+/* Emit some bits; return TRUE if successful, FALSE if must suspend */
 {
   /* This routine is heavily used, so it's worth coding tightly. */
-  register INT32 put_buffer = code;
-  register int put_bits = huff_put_bits;
+  register INT32 put_buffer = (INT32) code;
+  register int put_bits = state->cur.put_bits;
 
   /* if size is 0, caller used an invalid Huffman table entry */
   if (size == 0)
-    ERREXIT(cinfo->emethods, "Missing Huffman code table entry");
+    ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
 
-  put_buffer &= (((INT32) 1) << size) - 1; /* Mask off any excess bits in code */
+  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
   
   put_bits += size;		/* new number of bits in buffer */
   
   put_buffer <<= 24 - put_bits; /* align incoming bits */
 
-  put_buffer |= huff_put_buffer; /* and merge with old buffer contents */
+  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
   
   while (put_bits >= 8) {
     int c = (int) ((put_buffer >> 16) & 0xFF);
     
-    emit_byte(c);
+    emit_byte(state, c, return FALSE);
     if (c == 0xFF) {		/* need to stuff a zero byte? */
-      emit_byte(0);
+      emit_byte(state, 0, return FALSE);
     }
     put_buffer <<= 8;
     put_bits -= 8;
   }
 
-  huff_put_buffer = put_buffer;	/* Update global variables */
-  huff_put_bits = put_bits;
+  state->cur.put_buffer = put_buffer; /* update state variables */
+  state->cur.put_bits = put_bits;
+
+  return TRUE;
 }
 
 
-LOCAL void
-flush_bits (void)
+LOCAL boolean
+flush_bits (working_state * state)
 {
-  emit_bits((UINT16) 0x7F, 7);	/* fill any partial byte with ones */
-  huff_put_buffer = 0;		/* and reset bit-buffer to empty */
-  huff_put_bits = 0;
+  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
+    return FALSE;
+  state->cur.put_buffer = 0;	/* and reset bit-buffer to empty */
+  state->cur.put_bits = 0;
+  return TRUE;
 }
 
 
-
 /* Encode a single block's worth of coefficients */
-/* Note that the DC coefficient has already been converted to a difference */
 
-LOCAL void
-encode_one_block (JBLOCK block, HUFF_TBL *dctbl, HUFF_TBL *actbl)
+LOCAL boolean
+encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
+		  C_DERIVED_TBL *dctbl, C_DERIVED_TBL *actbl)
 {
   register int temp, temp2;
   register int nbits;
@@ -159,7 +332,7 @@
   
   /* Encode the DC coefficient difference per section F.1.2.1 */
   
-  temp = temp2 = block[0];
+  temp = temp2 = block[0] - last_dc_val;
 
   if (temp < 0) {
     temp = -temp;		/* temp is abs value of input */
@@ -176,13 +349,15 @@
   }
   
   /* Emit the Huffman-coded symbol for the number of bits */
-  emit_bits(dctbl->priv.enc.ehufco[nbits], dctbl->priv.enc.ehufsi[nbits]);
+  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
+    return FALSE;
 
   /* Emit that number of bits of the value, if positive, */
   /* or the complement of its magnitude, if negative. */
   if (nbits)			/* emit_bits rejects calls with size 0 */
-    emit_bits((UINT16) temp2, nbits);
-  
+    if (! emit_bits(state, (unsigned int) temp2, nbits))
+      return FALSE;
+
   /* Encode the AC coefficients per section F.1.2.2 */
   
   r = 0;			/* r = run length of zeros */
@@ -193,7 +368,8 @@
     } else {
       /* if run length > 15, must emit special run-length-16 codes (0xF0) */
       while (r > 15) {
-	emit_bits(actbl->priv.enc.ehufco[0xF0], actbl->priv.enc.ehufsi[0xF0]);
+	if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
+	  return FALSE;
 	r -= 16;
       }
 
@@ -206,16 +382,18 @@
       
       /* Find the number of bits needed for the magnitude of the coefficient */
       nbits = 1;		/* there must be at least one 1 bit */
-      while (temp >>= 1)
+      while ((temp >>= 1))
 	nbits++;
       
       /* Emit Huffman symbol for run length / number of bits */
       i = (r << 4) + nbits;
-      emit_bits(actbl->priv.enc.ehufco[i], actbl->priv.enc.ehufsi[i]);
-      
+      if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
+	return FALSE;
+
       /* Emit that number of bits of the value, if positive, */
       /* or the complement of its magnitude, if negative. */
-      emit_bits((UINT16) temp2, nbits);
+      if (! emit_bits(state, (unsigned int) temp2, nbits))
+	return FALSE;
       
       r = 0;
     }
@@ -223,51 +401,10 @@
 
   /* If the last coef(s) were zero, emit an end-of-block code */
   if (r > 0)
-    emit_bits(actbl->priv.enc.ehufco[0], actbl->priv.enc.ehufsi[0]);
-}
+    if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
+      return FALSE;
 
-
-
-/*
- * Initialize for a Huffman-compressed scan.
- * This is invoked after writing the SOS marker.
- * The pipeline controller must establish the entropy_output method pointer
- * before calling this routine.
- */
-
-METHODDEF void
-huff_init (compress_info_ptr xinfo)
-{
-  short ci;
-  jpeg_component_info * compptr;
-
-  /* Initialize static variables */
-  cinfo = xinfo;
-  huff_put_buffer = 0;
-  huff_put_bits = 0;
-
-  /* Initialize the output buffer */
-  output_buffer = (char *) (*cinfo->emethods->alloc_small)
-				((size_t) JPEG_BUF_SIZE);
-  bytes_in_buffer = 0;
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
-    /* Make sure requested tables are present */
-    if (cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no] == NULL ||
-	cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no] == NULL)
-      ERREXIT(cinfo->emethods, "Use of undefined Huffman table");
-    /* Compute derived values for Huffman tables */
-    /* We may do this more than once for same table, but it's not a big deal */
-    fix_huff_tbl(cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no]);
-    fix_huff_tbl(cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
-    /* Initialize DC predictions to 0 */
-    cinfo->last_dc_val[ci] = 0;
-  }
-
-  /* Initialize restart stuff */
-  cinfo->restarts_to_go = cinfo->restart_interval;
-  cinfo->next_restart_num = 0;
+  return TRUE;
 }
 
 
@@ -275,24 +412,24 @@
  * Emit a restart marker & resynchronize predictions.
  */
 
-LOCAL void
-emit_restart (compress_info_ptr cinfo)
+LOCAL boolean
+emit_restart (working_state * state, int restart_num)
 {
-  short ci;
+  int ci;
 
-  flush_bits();
+  if (! flush_bits(state))
+    return FALSE;
 
-  emit_byte(0xFF);
-  emit_byte(RST0 + cinfo->next_restart_num);
+  emit_byte(state, 0xFF, return FALSE);
+  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
 
   /* Re-initialize DC predictions to 0 */
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
-    cinfo->last_dc_val[ci] = 0;
+  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
+    state->cur.last_dc_val[ci] = 0;
 
-  /* Update restart state */
-  cinfo->restarts_to_go = cinfo->restart_interval;
-  cinfo->next_restart_num++;
-  cinfo->next_restart_num &= 7;
+  /* The restart counter is not updated until we successfully write the MCU. */
+
+  return TRUE;
 }
 
 
@@ -300,31 +437,56 @@
  * Encode and output one MCU's worth of Huffman-compressed coefficients.
  */
 
-METHODDEF void
-huff_encode (compress_info_ptr cinfo, JBLOCK *MCU_data)
+METHODDEF boolean
+encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 {
-  short blkn, ci;
+  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+  working_state state;
+  int blkn, ci;
   jpeg_component_info * compptr;
-  JCOEF temp;
 
-  /* Account for restart interval, emit restart marker if needed */
+  /* Load up working state */
+  state.next_output_byte = cinfo->dest->next_output_byte;
+  state.free_in_buffer = cinfo->dest->free_in_buffer;
+  ASSIGN_STATE(state.cur, entropy->saved);
+  state.cinfo = cinfo;
+
+  /* Emit restart marker if needed */
   if (cinfo->restart_interval) {
-    if (cinfo->restarts_to_go == 0)
-      emit_restart(cinfo);
-    cinfo->restarts_to_go--;
+    if (entropy->restarts_to_go == 0)
+      if (! emit_restart(&state, entropy->next_restart_num))
+	return FALSE;
   }
 
+  /* Encode the MCU data blocks */
   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
     ci = cinfo->MCU_membership[blkn];
     compptr = cinfo->cur_comp_info[ci];
-    /* Convert DC value to difference, update last_dc_val */
-    temp = MCU_data[blkn][0];
-    MCU_data[blkn][0] -= cinfo->last_dc_val[ci];
-    cinfo->last_dc_val[ci] = temp;
-    encode_one_block(MCU_data[blkn],
-		     cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no],
-		     cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
+    if (! encode_one_block(&state,
+			   MCU_data[blkn][0], state.cur.last_dc_val[ci],
+			   entropy->dc_derived_tbls[compptr->dc_tbl_no],
+			   entropy->ac_derived_tbls[compptr->ac_tbl_no]))
+      return FALSE;
+    /* Update last_dc_val */
+    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
   }
+
+  /* Completed MCU, so update state */
+  cinfo->dest->next_output_byte = state.next_output_byte;
+  cinfo->dest->free_in_buffer = state.free_in_buffer;
+  ASSIGN_STATE(entropy->saved, state.cur);
+
+  /* Update restart-interval state too */
+  if (cinfo->restart_interval) {
+    if (entropy->restarts_to_go == 0) {
+      entropy->restarts_to_go = cinfo->restart_interval;
+      entropy->next_restart_num++;
+      entropy->next_restart_num &= 7;
+    }
+    entropy->restarts_to_go--;
+  }
+
+  return TRUE;
 }
 
 
@@ -333,18 +495,28 @@
  */
 
 METHODDEF void
-huff_term (compress_info_ptr cinfo)
+finish_pass_huff (j_compress_ptr cinfo)
 {
+  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+  working_state state;
+
+  /* Load up working state ... flush_bits needs it */
+  state.next_output_byte = cinfo->dest->next_output_byte;
+  state.free_in_buffer = cinfo->dest->free_in_buffer;
+  ASSIGN_STATE(state.cur, entropy->saved);
+  state.cinfo = cinfo;
+
   /* Flush out the last data */
-  flush_bits();
-  flush_bytes();
-  /* Release the I/O buffer */
-  (*cinfo->emethods->free_small) ((void *) output_buffer);
+  if (! flush_bits(&state))
+    ERREXIT(cinfo, JERR_CANT_SUSPEND);
+
+  /* Update state */
+  cinfo->dest->next_output_byte = state.next_output_byte;
+  cinfo->dest->free_in_buffer = state.free_in_buffer;
+  ASSIGN_STATE(entropy->saved, state.cur);
 }
 
 
-
-
 /*
  * Huffman coding optimization.
  *
@@ -353,24 +525,127 @@
  * count the number of uses of each symbol that is to be Huffman-coded.
  * (This process must agree with the code above.)  Then we build an
  * optimal Huffman coding tree for the observed counts.
+ *
+ * The JPEG standard requires Huffman codes to be no more than 16 bits long.
+ * If some symbols have a very small but nonzero probability, the Huffman tree
+ * must be adjusted to meet the code length restriction.  We currently use
+ * the adjustment method suggested in the JPEG spec.  This method is *not*
+ * optimal; it may not choose the best possible limited-length code.  But
+ * since the symbols involved are infrequently used, it's not clear that
+ * going to extra trouble is worthwhile.
  */
 
 #ifdef ENTROPY_OPT_SUPPORTED
 
 
-/* These are static so htest_one_block can find 'em */
-static long * dc_count_ptrs[NUM_HUFF_TBLS];
-static long * ac_count_ptrs[NUM_HUFF_TBLS];
-
+/* Process a single block's worth of coefficients */
 
 LOCAL void
-gen_huff_coding (compress_info_ptr cinfo, HUFF_TBL *htbl, long freq[])
-/* Generate the optimal coding for the given counts */
+htest_one_block (JCOEFPTR block, int last_dc_val,
+		 long dc_counts[], long ac_counts[])
+{
+  register int temp;
+  register int nbits;
+  register int k, r;
+  
+  /* Encode the DC coefficient difference per section F.1.2.1 */
+  
+  temp = block[0] - last_dc_val;
+  if (temp < 0)
+    temp = -temp;
+  
+  /* Find the number of bits needed for the magnitude of the coefficient */
+  nbits = 0;
+  while (temp) {
+    nbits++;
+    temp >>= 1;
+  }
+
+  /* Count the Huffman symbol for the number of bits */
+  dc_counts[nbits]++;
+  
+  /* Encode the AC coefficients per section F.1.2.2 */
+  
+  r = 0;			/* r = run length of zeros */
+  
+  for (k = 1; k < DCTSIZE2; k++) {
+    if ((temp = block[k]) == 0) {
+      r++;
+    } else {
+      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
+      while (r > 15) {
+	ac_counts[0xF0]++;
+	r -= 16;
+      }
+      
+      /* Find the number of bits needed for the magnitude of the coefficient */
+      if (temp < 0)
+	temp = -temp;
+      
+      /* Find the number of bits needed for the magnitude of the coefficient */
+      nbits = 1;		/* there must be at least one 1 bit */
+      while ((temp >>= 1))
+	nbits++;
+      
+      /* Count Huffman symbol for run length / number of bits */
+      ac_counts[(r << 4) + nbits]++;
+      
+      r = 0;
+    }
+  }
+
+  /* If the last coef(s) were zero, emit an end-of-block code */
+  if (r > 0)
+    ac_counts[0]++;
+}
+
+
+/*
+ * Trial-encode one MCU's worth of Huffman-compressed coefficients.
+ * No data is actually output, so no suspension return is possible.
+ */
+
+METHODDEF boolean
+encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+  int blkn, ci;
+  jpeg_component_info * compptr;
+
+  /* Take care of restart intervals if needed */
+  if (cinfo->restart_interval) {
+    if (entropy->restarts_to_go == 0) {
+      /* Re-initialize DC predictions to 0 */
+      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
+	entropy->saved.last_dc_val[ci] = 0;
+      /* Update restart state */
+      entropy->restarts_to_go = cinfo->restart_interval;
+    }
+    entropy->restarts_to_go--;
+  }
+
+  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+    ci = cinfo->MCU_membership[blkn];
+    compptr = cinfo->cur_comp_info[ci];
+    htest_one_block(MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
+		    entropy->dc_count_ptrs[compptr->dc_tbl_no],
+		    entropy->ac_count_ptrs[compptr->ac_tbl_no]);
+    entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
+  }
+
+  return TRUE;
+}
+
+
+/* Generate the optimal coding for the given counts, initialize htbl */
+
+LOCAL void
+gen_huff_coding (j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
 {
 #define MAX_CLEN 32		/* assumed maximum initial code length */
   UINT8 bits[MAX_CLEN+1];	/* bits[k] = # of symbols with code length k */
-  short codesize[257];		/* codesize[k] = code length of symbol k */
-  short others[257];		/* next symbol in current branch of tree */
+  int codesize[257];		/* codesize[k] = code length of symbol k */
+  int others[257];		/* next symbol in current branch of tree */
   int c1, c2;
   int p, i, j;
   long v;
@@ -383,7 +658,7 @@
     others[i] = -1;		/* init links to empty */
   
   freq[256] = 1;		/* make sure there is a nonzero count */
-  /* including the pseudo-symbol 256 in the Huffman procedure guarantees
+  /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
    * that no real symbol is given code-value of all ones, because 256
    * will be placed in the largest codeword category.
    */
@@ -444,7 +719,7 @@
       /* The JPEG standard seems to think that this can't happen, */
       /* but I'm paranoid... */
       if (codesize[i] > MAX_CLEN)
-	ERREXIT(cinfo->emethods, "Huffman code size table overflow");
+	ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
 
       bits[codesize[i]]++;
     }
@@ -495,176 +770,49 @@
       }
     }
   }
+
+  /* Set sent_table FALSE so updated table will be written to JPEG file. */
+  htbl->sent_table = FALSE;
 }
 
 
-/* Process a single block's worth of coefficients */
-/* Note that the DC coefficient has already been converted to a difference */
-
-LOCAL void
-htest_one_block (JBLOCK block, JCOEF block0,
-		 long dc_counts[], long ac_counts[])
-{
-  register INT32 temp;
-  register int nbits;
-  register int k, r;
-  
-  /* Encode the DC coefficient difference per section F.1.2.1 */
-  
-  /* Find the number of bits needed for the magnitude of the coefficient */
-  temp = block0;
-  if (temp < 0) temp = -temp;
-  
-  for (nbits = 0; temp; nbits++)
-    temp >>= 1;
-  
-  /* Count the Huffman symbol for the number of bits */
-  dc_counts[nbits]++;
-  
-  /* Encode the AC coefficients per section F.1.2.2 */
-  
-  r = 0;			/* r = run length of zeros */
-  
-  for (k = 1; k < DCTSIZE2; k++) {
-    if ((temp = block[k]) == 0) {
-      r++;
-    } else {
-      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
-      while (r > 15) {
-	ac_counts[0xF0]++;
-	r -= 16;
-      }
-      
-      /* Find the number of bits needed for the magnitude of the coefficient */
-      if (temp < 0) temp = -temp;
-      
-      for (nbits = 0; temp; nbits++)
-	temp >>= 1;
-      
-      /* Count Huffman symbol for run length / number of bits */
-      ac_counts[(r << 4) + nbits]++;
-      
-      r = 0;
-    }
-  }
-
-  /* If the last coef(s) were zero, emit an end-of-block code */
-  if (r > 0)
-    ac_counts[0]++;
-}
-
-
-
 /*
- * Trial-encode one MCU's worth of Huffman-compressed coefficients.
- */
-
-LOCAL void
-htest_encode (compress_info_ptr cinfo, JBLOCK *MCU_data)
-{
-  short blkn, ci;
-  jpeg_component_info * compptr;
-
-  /* Take care of restart intervals if needed */
-  if (cinfo->restart_interval) {
-    if (cinfo->restarts_to_go == 0) {
-      /* Re-initialize DC predictions to 0 */
-      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
-	cinfo->last_dc_val[ci] = 0;
-      /* Update restart state */
-      cinfo->restarts_to_go = cinfo->restart_interval;
-    }
-    cinfo->restarts_to_go--;
-  }
-
-  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
-    ci = cinfo->MCU_membership[blkn];
-    compptr = cinfo->cur_comp_info[ci];
-    /* NB: unlike the real entropy encoder, we may not change the input data */
-    htest_one_block(MCU_data[blkn],
-		    (JCOEF) (MCU_data[blkn][0] - cinfo->last_dc_val[ci]),
-		    dc_count_ptrs[compptr->dc_tbl_no],
-		    ac_count_ptrs[compptr->ac_tbl_no]);
-    cinfo->last_dc_val[ci] = MCU_data[blkn][0];
-  }
-}
-
-
-
-/*
- * Find the best coding parameters for a Huffman-coded scan.
- * When called, the scan data has already been converted to a sequence of
- * MCU groups of quantized coefficients, which are stored in a "big" array.
- * The source_method knows how to iterate through that array.
- * On return, the MCU data is unmodified, but the Huffman tables referenced
- * by the scan components may have been altered.
+ * Finish up a statistics-gathering pass and create the new Huffman tables.
  */
 
 METHODDEF void
-huff_optimize (compress_info_ptr cinfo, MCU_output_caller_ptr source_method)
-/* Optimize Huffman-coding parameters (Huffman symbol table) */
+finish_pass_gather (j_compress_ptr cinfo)
 {
-  int i, tbl;
-  HUFF_TBL **htblptr;
+  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+  int ci, dctbl, actbl;
+  jpeg_component_info * compptr;
+  JHUFF_TBL **htblptr;
+  boolean did_dc[NUM_HUFF_TBLS];
+  boolean did_ac[NUM_HUFF_TBLS];
 
-  /* Allocate and zero the count tables */
-  /* Note that gen_huff_coding expects 257 entries in each table! */
+  /* It's important not to apply gen_huff_coding more than once per table,
+   * because it clobbers the input frequency counts!
+   */
+  MEMZERO(did_dc, SIZEOF(did_dc));
+  MEMZERO(did_ac, SIZEOF(did_ac));
 
-  for (i = 0; i < NUM_HUFF_TBLS; i++) {
-    dc_count_ptrs[i] = NULL;
-    ac_count_ptrs[i] = NULL;
-  }
-
-  for (i = 0; i < cinfo->comps_in_scan; i++) {
-    /* Create DC table */
-    tbl = cinfo->cur_comp_info[i]->dc_tbl_no;
-    if (dc_count_ptrs[tbl] == NULL) {
-      dc_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small)
-					(257 * SIZEOF(long));
-      MEMZERO(dc_count_ptrs[tbl], 257 * SIZEOF(long));
-    }
-    /* Create AC table */
-    tbl = cinfo->cur_comp_info[i]->ac_tbl_no;
-    if (ac_count_ptrs[tbl] == NULL) {
-      ac_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small)
-					(257 * SIZEOF(long));
-      MEMZERO(ac_count_ptrs[tbl], 257 * SIZEOF(long));
-    }
-  }
-
-  /* Initialize DC predictions to 0 */
-  for (i = 0; i < cinfo->comps_in_scan; i++) {
-    cinfo->last_dc_val[i] = 0;
-  }
-  /* Initialize restart stuff */
-  cinfo->restarts_to_go = cinfo->restart_interval;
-
-  /* Scan the MCU data, count symbol uses */
-  (*source_method) (cinfo, htest_encode);
-
-  /* Now generate optimal Huffman tables */
-  for (tbl = 0; tbl < NUM_HUFF_TBLS; tbl++) {
-    if (dc_count_ptrs[tbl] != NULL) {
-      htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
+  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+    compptr = cinfo->cur_comp_info[ci];
+    dctbl = compptr->dc_tbl_no;
+    actbl = compptr->ac_tbl_no;
+    if (! did_dc[dctbl]) {
+      htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
       if (*htblptr == NULL)
-	*htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
-      /* Set sent_table FALSE so updated table will be written to JPEG file. */
-      (*htblptr)->sent_table = FALSE;
-      /* Compute the optimal Huffman encoding */
-      gen_huff_coding(cinfo, *htblptr, dc_count_ptrs[tbl]);
-      /* Release the count table */
-      (*cinfo->emethods->free_small) ((void *) dc_count_ptrs[tbl]);
+	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+      gen_huff_coding(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
+      did_dc[dctbl] = TRUE;
     }
-    if (ac_count_ptrs[tbl] != NULL) {
-      htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
+    if (! did_ac[actbl]) {
+      htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
       if (*htblptr == NULL)
-	*htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
-      /* Set sent_table FALSE so updated table will be written to JPEG file. */
-      (*htblptr)->sent_table = FALSE;
-      /* Compute the optimal Huffman encoding */
-      gen_huff_coding(cinfo, *htblptr, ac_count_ptrs[tbl]);
-      /* Release the count table */
-      (*cinfo->emethods->free_small) ((void *) ac_count_ptrs[tbl]);
+	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+      gen_huff_coding(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
+      did_ac[actbl] = TRUE;
     }
   }
 }
@@ -674,27 +822,26 @@
 
 
 /*
- * The method selection routine for Huffman entropy encoding.
+ * Module initialization routine for Huffman entropy encoding.
  */
 
 GLOBAL void
-jselchuffman (compress_info_ptr cinfo)
+jinit_huff_encoder (j_compress_ptr cinfo)
 {
-  if (! cinfo->arith_code) {
-    cinfo->methods->entropy_encode_init = huff_init;
-    cinfo->methods->entropy_encode = huff_encode;
-    cinfo->methods->entropy_encode_term = huff_term;
+  huff_entropy_ptr entropy;
+  int i;
+
+  entropy = (huff_entropy_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(huff_entropy_encoder));
+  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
+  entropy->pub.start_pass = start_pass_huff;
+
+  /* Mark tables unallocated */
+  for (i = 0; i < NUM_HUFF_TBLS; i++) {
+    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
 #ifdef ENTROPY_OPT_SUPPORTED
-    cinfo->methods->entropy_optimize = huff_optimize;
-    /* The standard Huffman tables are only valid for 8-bit data precision.
-     * If the precision is higher, force optimization on so that usable
-     * tables will be computed.  This test can be removed if default tables
-     * are supplied that are valid for the desired precision.
-     */
-    if (cinfo->data_precision > 8)
-      cinfo->optimize_coding = TRUE;
-    if (cinfo->optimize_coding)
-      cinfo->total_passes++;	/* one pass needed for entropy optimization */
+    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
 #endif
   }
 }
diff --git a/jcmain.c b/jcmain.c
deleted file mode 100644
index 03d24fc..0000000
--- a/jcmain.c
+++ /dev/null
@@ -1,733 +0,0 @@
-/*
- * jcmain.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains a command-line user interface for the JPEG compressor.
- * It should work on any system with Unix- or MS-DOS-style command lines.
- *
- * Two different command line styles are permitted, depending on the
- * compile-time switch TWO_FILE_COMMANDLINE:
- *	cjpeg [options]  inputfile outputfile
- *	cjpeg [options]  [inputfile]
- * In the second style, output is always to standard output, which you'd
- * normally redirect to a file or pipe to some other program.  Input is
- * either from a named file or from standard input (typically redirected).
- * The second style is convenient on Unix but is unhelpful on systems that
- * don't support pipes.  Also, you MUST use the first style if your system
- * doesn't do binary I/O to stdin/stdout.
- * To simplify script writing, the "-outfile" switch is provided.  The syntax
- *	cjpeg [options]  -outfile outputfile  inputfile
- * works regardless of which command line style is used.
- */
-
-#include "jinclude.h"
-#ifdef INCLUDES_ARE_ANSI
-#include <stdlib.h>		/* to declare exit() */
-#endif
-#include <ctype.h>		/* to declare isupper(), tolower() */
-#ifdef NEED_SIGNAL_CATCHER
-#include <signal.h>		/* to declare signal() */
-#endif
-#ifdef USE_SETMODE
-#include <fcntl.h>		/* to declare setmode() */
-#endif
-
-#ifdef THINK_C
-#include <console.h>		/* command-line reader for Macintosh */
-#endif
-
-#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
-#define READ_BINARY	"r"
-#define WRITE_BINARY	"w"
-#else
-#define READ_BINARY	"rb"
-#define WRITE_BINARY	"wb"
-#endif
-
-#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
-#define EXIT_FAILURE  1
-#endif
-#ifndef EXIT_SUCCESS
-#ifdef VMS
-#define EXIT_SUCCESS  1		/* VMS is very nonstandard */
-#else
-#define EXIT_SUCCESS  0
-#endif
-#endif
-
-
-#include "jversion.h"		/* for version message */
-
-
-/*
- * This routine determines what format the input file is,
- * and selects the appropriate input-reading module.
- *
- * To determine which family of input formats the file belongs to,
- * we may look only at the first byte of the file, since C does not
- * guarantee that more than one character can be pushed back with ungetc.
- * Looking at additional bytes would require one of these approaches:
- *     1) assume we can fseek() the input file (fails for piped input);
- *     2) assume we can push back more than one character (works in
- *        some C implementations, but unportable);
- *     3) provide our own buffering as is done in djpeg (breaks input readers
- *        that want to use stdio directly, such as the RLE library);
- * or  4) don't put back the data, and modify the input_init methods to assume
- *        they start reading after the start of file (also breaks RLE library).
- * #1 is attractive for MS-DOS but is untenable on Unix.
- *
- * The most portable solution for file types that can't be identified by their
- * first byte is to make the user tell us what they are.  This is also the
- * only approach for "raw" file types that contain only arbitrary values.
- * We presently apply this method for Targa files.  Most of the time Targa
- * files start with 0x00, so we recognize that case.  Potentially, however,
- * a Targa file could start with any byte value (byte 0 is the length of the
- * seldom-used ID field), so we provide a switch to force Targa input mode.
- */
-
-static boolean is_targa;	/* records user -targa switch */
-
-
-LOCAL void
-select_file_type (compress_info_ptr cinfo)
-{
-  int c;
-
-  if (is_targa) {
-#ifdef TARGA_SUPPORTED
-    jselrtarga(cinfo);
-#else
-    ERREXIT(cinfo->emethods, "Targa support was not compiled");
-#endif
-    return;
-  }
-
-  if ((c = getc(cinfo->input_file)) == EOF)
-    ERREXIT(cinfo->emethods, "Empty input file");
-
-  switch (c) {
-#ifdef GIF_SUPPORTED
-  case 'G':
-    jselrgif(cinfo);
-    break;
-#endif
-#ifdef PPM_SUPPORTED
-  case 'P':
-    jselrppm(cinfo);
-    break;
-#endif
-#ifdef RLE_SUPPORTED
-  case 'R':
-    jselrrle(cinfo);
-    break;
-#endif
-#ifdef TARGA_SUPPORTED
-  case 0x00:
-    jselrtarga(cinfo);
-    break;
-#endif
-  default:
-#ifdef TARGA_SUPPORTED
-    ERREXIT(cinfo->emethods, "Unrecognized input file format --- perhaps you need -targa");
-#else
-    ERREXIT(cinfo->emethods, "Unrecognized input file format");
-#endif
-    break;
-  }
-
-  if (ungetc(c, cinfo->input_file) == EOF)
-    ERREXIT(cinfo->emethods, "ungetc failed");
-}
-
-
-/*
- * This routine gets control after the input file header has been read.
- * It must determine what output JPEG file format is to be written,
- * and make any other compression parameter changes that are desirable.
- */
-
-METHODDEF void
-c_ui_method_selection (compress_info_ptr cinfo)
-{
-  /* If the input is gray scale, generate a monochrome JPEG file. */
-  if (cinfo->in_color_space == CS_GRAYSCALE)
-    j_monochrome_default(cinfo);
-  /* For now, always select JFIF output format. */
-#ifdef JFIF_SUPPORTED
-  jselwjfif(cinfo);
-#else
-  You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
-#endif
-}
-
-
-/*
- * Signal catcher to ensure that temporary files are removed before aborting.
- * NB: for Amiga Manx C this is actually a global routine named _abort();
- * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
- */
-
-#ifdef NEED_SIGNAL_CATCHER
-
-static external_methods_ptr emethods; /* for access to free_all */
-
-GLOBAL void
-signal_catcher (int signum)
-{
-  if (emethods != NULL) {
-    emethods->trace_level = 0;	/* turn off trace output */
-    (*emethods->free_all) ();	/* clean up memory allocation & temp files */
-  }
-  exit(EXIT_FAILURE);
-}
-
-#endif
-
-
-/*
- * Optional routine to display a percent-done figure on stderr.
- * See jcdeflts.c for explanation of the information used.
- */
-
-#ifdef PROGRESS_REPORT
-
-METHODDEF void
-progress_monitor (compress_info_ptr cinfo, long loopcounter, long looplimit)
-{
-  if (cinfo->total_passes > 1) {
-    fprintf(stderr, "\rPass %d/%d: %3d%% ",
-	    cinfo->completed_passes+1, cinfo->total_passes,
-	    (int) (loopcounter*100L/looplimit));
-  } else {
-    fprintf(stderr, "\r %3d%% ",
-	    (int) (loopcounter*100L/looplimit));
-  }
-  fflush(stderr);
-}
-
-#endif
-
-
-/*
- * Argument-parsing code.
- * The switch parser is designed to be useful with DOS-style command line
- * syntax, ie, intermixed switches and file names, where only the switches
- * to the left of a given file name affect processing of that file.
- * The main program in this file doesn't actually use this capability...
- */
-
-
-static char * progname;		/* program name for error messages */
-static char * outfilename;	/* for -outfile switch */
-
-
-LOCAL void
-usage (void)
-/* complain about bad command line */
-{
-  fprintf(stderr, "usage: %s [switches] ", progname);
-#ifdef TWO_FILE_COMMANDLINE
-  fprintf(stderr, "inputfile outputfile\n");
-#else
-  fprintf(stderr, "[inputfile]\n");
-#endif
-
-  fprintf(stderr, "Switches (names may be abbreviated):\n");
-  fprintf(stderr, "  -quality N     Compression quality (0..100; 5-95 is useful range)\n");
-  fprintf(stderr, "  -grayscale     Create monochrome JPEG file\n");
-#ifdef ENTROPY_OPT_SUPPORTED
-  fprintf(stderr, "  -optimize      Optimize Huffman table (smaller file, but slow compression)\n");
-#endif
-#ifdef TARGA_SUPPORTED
-  fprintf(stderr, "  -targa         Input file is Targa format (usually not needed)\n");
-#endif
-  fprintf(stderr, "Switches for advanced users:\n");
-  fprintf(stderr, "  -restart N     Set restart interval in rows, or in blocks with B\n");
-#ifdef INPUT_SMOOTHING_SUPPORTED
-  fprintf(stderr, "  -smooth N      Smooth dithered input (N=1..100 is strength)\n");
-#endif
-  fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
-  fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
-  fprintf(stderr, "Switches for wizards:\n");
-#ifdef C_ARITH_CODING_SUPPORTED
-  fprintf(stderr, "  -arithmetic    Use arithmetic coding\n");
-#endif
-#ifdef C_MULTISCAN_FILES_SUPPORTED
-  fprintf(stderr, "  -nointerleave  Create noninterleaved JPEG file\n");
-#endif
-  fprintf(stderr, "  -qtables file  Use quantization tables given in file\n");
-  fprintf(stderr, "  -sample HxV[,...]  Set JPEG sampling factors\n");
-  exit(EXIT_FAILURE);
-}
-
-
-LOCAL boolean
-keymatch (char * arg, const char * keyword, int minchars)
-/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
-/* keyword is the constant keyword (must be lower case already), */
-/* minchars is length of minimum legal abbreviation. */
-{
-  register int ca, ck;
-  register int nmatched = 0;
-
-  while ((ca = *arg++) != '\0') {
-    if ((ck = *keyword++) == '\0')
-      return FALSE;		/* arg longer than keyword, no good */
-    if (isupper(ca))		/* force arg to lcase (assume ck is already) */
-      ca = tolower(ca);
-    if (ca != ck)
-      return FALSE;		/* no good */
-    nmatched++;			/* count matched characters */
-  }
-  /* reached end of argument; fail if it's too short for unique abbrev */
-  if (nmatched < minchars)
-    return FALSE;
-  return TRUE;			/* A-OK */
-}
-
-
-LOCAL int
-qt_getc (FILE * file)
-/* Read next char, skipping over any comments (# to end of line) */
-/* A comment/newline sequence is returned as a newline */
-{
-  register int ch;
-  
-  ch = getc(file);
-  if (ch == '#') {
-    do {
-      ch = getc(file);
-    } while (ch != '\n' && ch != EOF);
-  }
-  return ch;
-}
-
-
-LOCAL long
-read_qt_integer (FILE * file)
-/* Read an unsigned decimal integer from a quantization-table file */
-/* Swallows one trailing character after the integer */
-{
-  register int ch;
-  register long val;
-  
-  /* Skip any leading whitespace, detect EOF */
-  do {
-    ch = qt_getc(file);
-    if (ch == EOF)
-      return EOF;
-  } while (isspace(ch));
-  
-  if (! isdigit(ch)) {
-    fprintf(stderr, "%s: bogus data in quantization file\n", progname);
-    exit(EXIT_FAILURE);
-  }
-
-  val = ch - '0';
-  while (ch = qt_getc(file), isdigit(ch)) {
-    val *= 10;
-    val += ch - '0';
-  }
-  return val;
-}
-
-
-LOCAL void
-read_quant_tables (compress_info_ptr cinfo, char * filename, int scale_factor)
-/* Read a set of quantization tables from the specified file.
- * The file is plain ASCII text: decimal numbers with whitespace between.
- * Comments preceded by '#' may be included in the file.
- * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
- * The tables are implicitly numbered 0,1,etc.
- */
-{
-  /* ZIG[i] is the zigzag-order position of the i'th element of a DCT block */
-  /* read in natural order (left to right, top to bottom). */
-  static const short ZIG[DCTSIZE2] = {
-     0,  1,  5,  6, 14, 15, 27, 28,
-     2,  4,  7, 13, 16, 26, 29, 42,
-     3,  8, 12, 17, 25, 30, 41, 43,
-     9, 11, 18, 24, 31, 40, 44, 53,
-    10, 19, 23, 32, 39, 45, 52, 54,
-    20, 22, 33, 38, 46, 51, 55, 60,
-    21, 34, 37, 47, 50, 56, 59, 61,
-    35, 36, 48, 49, 57, 58, 62, 63
-    };
-  FILE * fp;
-  int tblno, i;
-  long val;
-  QUANT_TBL table;
-
-  if ((fp = fopen(filename, "r")) == NULL) {
-    fprintf(stderr, "%s: can't open %s\n", progname, filename);
-    exit(EXIT_FAILURE);
-  }
-  tblno = 0;
-
-  while ((val = read_qt_integer(fp)) != EOF) { /* read 1st element of table */
-    if (tblno >= NUM_QUANT_TBLS) {
-      fprintf(stderr, "%s: too many tables in file %s\n", progname, filename);
-      exit(EXIT_FAILURE);
-    }
-    table[0] = (QUANT_VAL) val;
-    for (i = 1; i < DCTSIZE2; i++) {
-      if ((val = read_qt_integer(fp)) == EOF) {
-	fprintf(stderr, "%s: incomplete table in file %s\n", progname, filename);
-	exit(EXIT_FAILURE);
-      }
-      table[ZIG[i]] = (QUANT_VAL) val;
-    }
-    j_add_quant_table(cinfo, tblno, table, scale_factor, FALSE);
-    tblno++;
-  }
-
-  fclose(fp);
-}
-
-
-LOCAL void
-set_sample_factors (compress_info_ptr cinfo, char *arg)
-/* Process a sample-factors parameter string, of the form */
-/*     HxV[,HxV,...]    */
-{
-#define MAX_COMPONENTS 4	/* # of comp_info slots made by jcdeflts.c */
-  int ci, val1, val2;
-  char ch1, ch2;
-
-  for (ci = 0; ci < MAX_COMPONENTS; ci++) {
-    if (*arg) {
-      ch2 = ',';		/* if not set by sscanf, will be ',' */
-      if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
-	usage();
-      if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',')
-	usage();		/* syntax check */
-      if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
-	fprintf(stderr, "JPEG sampling factors must be 1..4\n");
-	exit(EXIT_FAILURE);
-      }
-      cinfo->comp_info[ci].h_samp_factor = val1;
-      cinfo->comp_info[ci].v_samp_factor = val2;
-      while (*arg && *arg++ != ',') /* advance to next segment of arg string */
-	;
-    } else {
-      /* reached end of parameter, set remaining components to 1x1 sampling */
-      cinfo->comp_info[ci].h_samp_factor = 1;
-      cinfo->comp_info[ci].v_samp_factor = 1;
-    }
-  }
-}
-
-
-LOCAL int
-parse_switches (compress_info_ptr cinfo, int last_file_arg_seen,
-		int argc, char **argv)
-/* Initialize cinfo with default switch settings, then parse option switches.
- * Returns argv[] index of first file-name argument (== argc if none).
- * Any file names with indexes <= last_file_arg_seen are ignored;
- * they have presumably been processed in a previous iteration.
- * (Pass 0 for last_file_arg_seen on the first or only iteration.)
- */
-{
-  int argn;
-  char * arg;
-  char * qtablefile = NULL;	/* saves -qtables filename if any */
-  int q_scale_factor = 100;	/* default to no scaling for -qtables */
-
-  /* (Re-)initialize the system-dependent error and memory managers. */
-  jselerror(cinfo->emethods);	/* error/trace message routines */
-  jselmemmgr(cinfo->emethods);	/* memory allocation routines */
-  cinfo->methods->c_ui_method_selection = c_ui_method_selection;
-
-  /* Now OK to enable signal catcher. */
-#ifdef NEED_SIGNAL_CATCHER
-  emethods = cinfo->emethods;
-#endif
-
-  /* Set up default JPEG parameters. */
-  /* Note that default -quality level here need not, and does not,
-   * match the default scaling for an explicit -qtables argument.
-   */
-  j_c_defaults(cinfo, 75, FALSE); /* default quality level = 75 */
-  is_targa = FALSE;
-  outfilename = NULL;
-
-  /* Scan command line options, adjust parameters */
-
-  for (argn = 1; argn < argc; argn++) {
-    arg = argv[argn];
-    if (*arg != '-') {
-      /* Not a switch, must be a file name argument */
-      if (argn <= last_file_arg_seen) {
-	outfilename = NULL;	/* -outfile applies to just one input file */
-	continue;		/* ignore this name if previously processed */
-      }
-      break;			/* else done parsing switches */
-    }
-    arg++;			/* advance past switch marker character */
-
-    if (keymatch(arg, "arithmetic", 1)) {
-      /* Use arithmetic coding. */
-#ifdef C_ARITH_CODING_SUPPORTED
-      cinfo->arith_code = TRUE;
-#else
-      fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
-	      progname);
-      exit(EXIT_FAILURE);
-#endif
-
-    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
-      /* Enable debug printouts. */
-      /* On first -d, print version identification */
-      if (last_file_arg_seen == 0 && cinfo->emethods->trace_level == 0)
-	fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n",
-		JVERSION, JCOPYRIGHT);
-      cinfo->emethods->trace_level++;
-
-    } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
-      /* Force a monochrome JPEG file to be generated. */
-      j_monochrome_default(cinfo);
-
-    } else if (keymatch(arg, "maxmemory", 1)) {
-      /* Maximum memory in Kb (or Mb with 'm'). */
-      long lval;
-      char ch = 'x';
-
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
-	usage();
-      if (ch == 'm' || ch == 'M')
-	lval *= 1000L;
-      cinfo->emethods->max_memory_to_use = lval * 1000L;
-
-    } else if (keymatch(arg, "nointerleave", 3)) {
-      /* Create noninterleaved file. */
-#ifdef C_MULTISCAN_FILES_SUPPORTED
-      cinfo->interleave = FALSE;
-#else
-      fprintf(stderr, "%s: sorry, multiple-scan support was not compiled\n",
-	      progname);
-      exit(EXIT_FAILURE);
-#endif
-
-    } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
-      /* Enable entropy parm optimization. */
-#ifdef ENTROPY_OPT_SUPPORTED
-      cinfo->optimize_coding = TRUE;
-#else
-      fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
-	      progname);
-      exit(EXIT_FAILURE);
-#endif
-
-    } else if (keymatch(arg, "outfile", 3)) {
-      /* Set output file name. */
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      outfilename = argv[argn];	/* save it away for later use */
-
-    } else if (keymatch(arg, "quality", 1)) {
-      /* Quality factor (quantization table scaling factor). */
-      int val;
-
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      if (sscanf(argv[argn], "%d", &val) != 1)
-	usage();
-      /* Set quantization tables (will be overridden if -qtables also given).
-       * Note: we make force_baseline FALSE.
-       * This means non-baseline JPEG files can be created with low Q values.
-       * To ensure only baseline files are generated, pass TRUE instead.
-       */
-      j_set_quality(cinfo, val, FALSE);
-      /* Change scale factor in case -qtables is present. */
-      q_scale_factor = j_quality_scaling(val);
-
-    } else if (keymatch(arg, "qtables", 2)) {
-      /* Quantization tables fetched from file. */
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      qtablefile = argv[argn];
-      /* we postpone actually reading the file in case -quality comes later */
-
-    } else if (keymatch(arg, "restart", 1)) {
-      /* Restart interval in MCU rows (or in MCUs with 'b'). */
-      long lval;
-      char ch = 'x';
-
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
-	usage();
-      if (lval < 0 || lval > 65535L)
-	usage();
-      if (ch == 'b' || ch == 'B')
-	cinfo->restart_interval = (UINT16) lval;
-      else
-	cinfo->restart_in_rows = (int) lval;
-
-    } else if (keymatch(arg, "sample", 2)) {
-      /* Set sampling factors. */
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      set_sample_factors(cinfo, argv[argn]);
-
-    } else if (keymatch(arg, "smooth", 2)) {
-      /* Set input smoothing factor. */
-      int val;
-
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      if (sscanf(argv[argn], "%d", &val) != 1)
-	usage();
-      if (val < 0 || val > 100)
-	usage();
-      cinfo->smoothing_factor = val;
-
-    } else if (keymatch(arg, "targa", 1)) {
-      /* Input file is Targa format. */
-      is_targa = TRUE;
-
-    } else {
-      usage();			/* bogus switch */
-    }
-  }
-
-  /* Post-switch-scanning cleanup */
-
-  if (qtablefile != NULL)	/* process -qtables if it was present */
-    read_quant_tables(cinfo, qtablefile, q_scale_factor);
-
-  return argn;			/* return index of next arg (file name) */
-}
-
-
-/*
- * The main program.
- */
-
-GLOBAL int
-main (int argc, char **argv)
-{
-  struct Compress_info_struct cinfo;
-  struct Compress_methods_struct c_methods;
-  struct External_methods_struct e_methods;
-  int file_index;
-
-  /* On Mac, fetch a command line. */
-#ifdef THINK_C
-  argc = ccommand(&argv);
-#endif
-
-  progname = argv[0];
-
-  /* Set up links to method structures. */
-  cinfo.methods = &c_methods;
-  cinfo.emethods = &e_methods;
-
-  /* Install, but don't yet enable signal catcher. */
-#ifdef NEED_SIGNAL_CATCHER
-  emethods = NULL;
-  signal(SIGINT, signal_catcher);
-#ifdef SIGTERM			/* not all systems have SIGTERM */
-  signal(SIGTERM, signal_catcher);
-#endif
-#endif
-
-  /* Scan command line: set up compression parameters, find file names. */
-
-  file_index = parse_switches(&cinfo, 0, argc, argv);
-
-#ifdef TWO_FILE_COMMANDLINE
-  /* Must have either -outfile switch or explicit output file name */
-  if (outfilename == NULL) {
-    if (file_index != argc-2) {
-      fprintf(stderr, "%s: must name one input and one output file\n",
-	      progname);
-      usage();
-    }
-    outfilename = argv[file_index+1];
-  } else {
-    if (file_index != argc-1) {
-      fprintf(stderr, "%s: must name one input and one output file\n",
-	      progname);
-      usage();
-    }
-  }
-#else
-  /* Unix style: expect zero or one file name */
-  if (file_index < argc-1) {
-    fprintf(stderr, "%s: only one input file\n", progname);
-    usage();
-  }
-#endif /* TWO_FILE_COMMANDLINE */
-
-  /* Open the input file. */
-  if (file_index < argc) {
-    if ((cinfo.input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
-      fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
-      exit(EXIT_FAILURE);
-    }
-  } else {
-    /* default input file is stdin */
-#ifdef USE_SETMODE		/* need to hack file mode? */
-    setmode(fileno(stdin), O_BINARY);
-#endif
-#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
-    if ((cinfo.input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
-      fprintf(stderr, "%s: can't open stdin\n", progname);
-      exit(EXIT_FAILURE);
-    }
-#else
-    cinfo.input_file = stdin;
-#endif
-  }
-
-  /* Open the output file. */
-  if (outfilename != NULL) {
-    if ((cinfo.output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
-      fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
-      exit(EXIT_FAILURE);
-    }
-  } else {
-    /* default output file is stdout */
-#ifdef USE_SETMODE		/* need to hack file mode? */
-    setmode(fileno(stdout), O_BINARY);
-#endif
-#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
-    if ((cinfo.output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
-      fprintf(stderr, "%s: can't open stdout\n", progname);
-      exit(EXIT_FAILURE);
-    }
-#else
-    cinfo.output_file = stdout;
-#endif
-  }
-
-  /* Figure out the input file format, and set up to read it. */
-  select_file_type(&cinfo);
-
-#ifdef PROGRESS_REPORT
-  /* Start up progress display, unless trace output is on */
-  if (e_methods.trace_level == 0)
-    c_methods.progress_monitor = progress_monitor;
-#endif
-
-  /* Do it to it! */
-  jpeg_compress(&cinfo);
-
-#ifdef PROGRESS_REPORT
-  /* Clear away progress display */
-  if (e_methods.trace_level == 0) {
-    fprintf(stderr, "\r                \r");
-    fflush(stderr);
-  }
-#endif
-
-  /* All done. */
-  exit(EXIT_SUCCESS);
-  return 0;			/* suppress no-return-value warnings */
-}
diff --git a/jcmainct.c b/jcmainct.c
new file mode 100644
index 0000000..5359268
--- /dev/null
+++ b/jcmainct.c
@@ -0,0 +1,298 @@
+/*
+ * jcmainct.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the main buffer controller for compression.
+ * The main buffer lies between the pre-processor and the JPEG
+ * compressor proper; it holds downsampled data in the JPEG colorspace.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Note: currently, there is no operating mode in which a full-image buffer
+ * is needed at this step.  If there were, that mode could not be used with
+ * "raw data" input, since this module is bypassed in that case.  However,
+ * we've left the code here for possible use in special applications.
+ */
+#undef FULL_MAIN_BUFFER_SUPPORTED
+
+
+/* Private buffer controller object */
+
+typedef struct {
+  struct jpeg_c_main_controller pub; /* public fields */
+
+  JDIMENSION cur_mcu_row;	/* number of current iMCU row */
+  JDIMENSION rowgroup_ctr;	/* counts row groups received in iMCU row */
+  JDIMENSION mcu_ctr;		/* counts MCUs output from current row */
+  boolean suspended;		/* remember if we suspended output */
+  J_BUF_MODE pass_mode;		/* current operating mode */
+
+  /* If using just a strip buffer, this points to the entire set of buffers
+   * (we allocate one for each component).  In the full-image case, this
+   * points to the currently accessible strips of the virtual arrays.
+   */
+  JSAMPARRAY buffer[MAX_COMPONENTS];
+
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+  /* If using full-image storage, this array holds pointers to virtual-array
+   * control blocks for each component.  Unused if not full-image storage.
+   */
+  jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
+#endif
+} my_main_controller;
+
+typedef my_main_controller * my_main_ptr;
+
+
+/* Forward declarations */
+METHODDEF void process_data_simple_main
+	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
+	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+METHODDEF void process_data_buffer_main
+	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
+	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
+#endif
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF void
+start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+  my_main_ptr main = (my_main_ptr) cinfo->main;
+
+  /* Do nothing in raw-data mode. */
+  if (cinfo->raw_data_in)
+    return;
+
+  main->cur_mcu_row = 0;	/* initialize counters */
+  main->rowgroup_ctr = 0;
+  main->mcu_ctr = 0;
+  main->suspended = FALSE;
+  main->pass_mode = pass_mode;	/* save mode for use by process_data */
+
+  switch (pass_mode) {
+  case JBUF_PASS_THRU:
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+    if (main->whole_image[0] != NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+    main->pub.process_data = process_data_simple_main;
+    break;
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+  case JBUF_SAVE_SOURCE:
+  case JBUF_CRANK_DEST:
+  case JBUF_SAVE_AND_PASS:
+    if (main->whole_image[0] == NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    main->pub.process_data = process_data_buffer_main;
+    break;
+#endif
+  default:
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    break;
+  }
+}
+
+
+/*
+ * Process some data.
+ * This routine handles the simple pass-through mode,
+ * where we have only a strip buffer.
+ */
+
+METHODDEF void
+process_data_simple_main (j_compress_ptr cinfo,
+			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+			  JDIMENSION in_rows_avail)
+{
+  my_main_ptr main = (my_main_ptr) cinfo->main;
+
+  while (main->cur_mcu_row < cinfo->total_iMCU_rows) {
+    /* Read input data if we haven't filled the main buffer yet */
+    if (main->rowgroup_ctr < DCTSIZE)
+      (*cinfo->prep->pre_process_data) (cinfo,
+					input_buf, in_row_ctr, in_rows_avail,
+					main->buffer, &main->rowgroup_ctr,
+					(JDIMENSION) DCTSIZE);
+
+    /* If we don't have a full iMCU row buffered, return to application for
+     * more data.  Note that preprocessor will always pad to fill the iMCU row
+     * at the bottom of the image.
+     */
+    if (main->rowgroup_ctr != DCTSIZE)
+      return;
+
+    /* Send the completed row to the compressor */
+    (*cinfo->coef->compress_data) (cinfo, main->buffer, &main->mcu_ctr);
+
+    /* If compressor did not consume the whole row, then we must need to
+     * suspend processing and return to the application.  In this situation
+     * we pretend we didn't yet consume the last input row; otherwise, if
+     * it happened to be the last row of the image, the application would
+     * think we were done.
+     */
+    if (main->mcu_ctr < cinfo->MCUs_per_row) {
+      if (! main->suspended) {
+	(*in_row_ctr)--;
+	main->suspended = TRUE;
+      }
+      return;
+    }
+    /* We did finish the row.  Undo our little suspension hack if a previous
+     * call suspended; then mark the main buffer empty.
+     */
+    if (main->suspended) {
+      (*in_row_ctr)++;
+      main->suspended = FALSE;
+    }
+    main->mcu_ctr = 0;
+    main->rowgroup_ctr = 0;
+    main->cur_mcu_row++;
+  }
+}
+
+
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+
+/*
+ * Process some data.
+ * This routine handles all of the modes that use a full-size buffer.
+ */
+
+METHODDEF void
+process_data_buffer_main (j_compress_ptr cinfo,
+			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+			  JDIMENSION in_rows_avail)
+{
+  my_main_ptr main = (my_main_ptr) cinfo->main;
+  int ci;
+  jpeg_component_info *compptr;
+  boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
+
+  while (main->cur_mcu_row < cinfo->total_iMCU_rows) {
+    /* Realign the virtual buffers if at the start of an iMCU row. */
+    if (main->rowgroup_ctr == 0) {
+      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	   ci++, compptr++) {
+	main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
+	  ((j_common_ptr) cinfo, main->whole_image[ci],
+	   main->cur_mcu_row * (compptr->v_samp_factor * DCTSIZE), writing);
+      }
+      /* In a read pass, pretend we just read some source data. */
+      if (! writing) {
+	*in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
+	main->rowgroup_ctr = DCTSIZE;
+      }
+    }
+
+    /* If a write pass, read input data until the current iMCU row is full. */
+    /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
+    if (writing) {
+      (*cinfo->prep->pre_process_data) (cinfo,
+					input_buf, in_row_ctr, in_rows_avail,
+					main->buffer, &main->rowgroup_ctr,
+					(JDIMENSION) DCTSIZE);
+      /* Return to application if we need more data to fill the iMCU row. */
+      if (main->rowgroup_ctr < DCTSIZE)
+	return;
+    }
+
+    /* Emit data, unless this is a sink-only pass. */
+    if (main->pass_mode != JBUF_SAVE_SOURCE) {
+      (*cinfo->coef->compress_data) (cinfo, main->buffer, &main->mcu_ctr);
+      /* If compressor did not consume the whole row, then we must need to
+       * suspend processing and return to the application.  In this situation
+       * we pretend we didn't yet consume the last input row; otherwise, if
+       * it happened to be the last row of the image, the application would
+       * think we were done.
+       */
+      if (main->mcu_ctr < cinfo->MCUs_per_row) {
+	if (! main->suspended) {
+	  (*in_row_ctr)--;
+	  main->suspended = TRUE;
+	}
+	return;
+      }
+      /* We did finish the row.  Undo our little suspension hack if a previous
+       * call suspended; then mark the main buffer empty.
+       */
+      if (main->suspended) {
+	(*in_row_ctr)++;
+	main->suspended = FALSE;
+      }
+    }
+
+    /* If get here, we are done with this iMCU row.  Mark buffer empty. */
+    main->mcu_ctr = 0;
+    main->rowgroup_ctr = 0;
+    main->cur_mcu_row++;
+  }
+}
+
+#endif /* FULL_MAIN_BUFFER_SUPPORTED */
+
+
+/*
+ * Initialize main buffer controller.
+ */
+
+GLOBAL void
+jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
+{
+  my_main_ptr main;
+  int ci;
+  jpeg_component_info *compptr;
+
+  main = (my_main_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_main_controller));
+  cinfo->main = (struct jpeg_c_main_controller *) main;
+  main->pub.start_pass = start_pass_main;
+
+  /* We don't need to create a buffer in raw-data mode. */
+  if (cinfo->raw_data_in)
+    return;
+
+  /* Create the buffer.  It holds downsampled data, so each component
+   * may be of a different size.
+   */
+  if (need_full_buffer) {
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+    /* Allocate a full-image virtual array for each component */
+    /* Note we implicitly pad the bottom to a multiple of the iMCU height */
+    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	 ci++, compptr++) {
+      main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
+	((j_common_ptr) cinfo, JPOOL_IMAGE,
+	 compptr->width_in_blocks * DCTSIZE,
+	 compptr->height_in_blocks * DCTSIZE,
+	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
+    }
+#else
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+  } else {
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+    main->whole_image[0] = NULL; /* flag for no virtual arrays */
+#endif
+    /* Allocate a strip buffer for each component */
+    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	 ci++, compptr++) {
+      main->buffer[ci] = (*cinfo->mem->alloc_sarray)
+	((j_common_ptr) cinfo, JPOOL_IMAGE,
+	 compptr->width_in_blocks * DCTSIZE,
+	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
+    }
+  }
+}
diff --git a/jcmarker.c b/jcmarker.c
new file mode 100644
index 0000000..5454658
--- /dev/null
+++ b/jcmarker.c
@@ -0,0 +1,605 @@
+/*
+ * jcmarker.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write JPEG datastream markers.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+typedef enum {			/* JPEG marker codes */
+  M_SOF0  = 0xc0,
+  M_SOF1  = 0xc1,
+  M_SOF2  = 0xc2,
+  M_SOF3  = 0xc3,
+  
+  M_SOF5  = 0xc5,
+  M_SOF6  = 0xc6,
+  M_SOF7  = 0xc7,
+  
+  M_JPG   = 0xc8,
+  M_SOF9  = 0xc9,
+  M_SOF10 = 0xca,
+  M_SOF11 = 0xcb,
+  
+  M_SOF13 = 0xcd,
+  M_SOF14 = 0xce,
+  M_SOF15 = 0xcf,
+  
+  M_DHT   = 0xc4,
+  
+  M_DAC   = 0xcc,
+  
+  M_RST0  = 0xd0,
+  M_RST1  = 0xd1,
+  M_RST2  = 0xd2,
+  M_RST3  = 0xd3,
+  M_RST4  = 0xd4,
+  M_RST5  = 0xd5,
+  M_RST6  = 0xd6,
+  M_RST7  = 0xd7,
+  
+  M_SOI   = 0xd8,
+  M_EOI   = 0xd9,
+  M_SOS   = 0xda,
+  M_DQT   = 0xdb,
+  M_DNL   = 0xdc,
+  M_DRI   = 0xdd,
+  M_DHP   = 0xde,
+  M_EXP   = 0xdf,
+  
+  M_APP0  = 0xe0,
+  M_APP1  = 0xe1,
+  M_APP2  = 0xe2,
+  M_APP3  = 0xe3,
+  M_APP4  = 0xe4,
+  M_APP5  = 0xe5,
+  M_APP6  = 0xe6,
+  M_APP7  = 0xe7,
+  M_APP8  = 0xe8,
+  M_APP9  = 0xe9,
+  M_APP10 = 0xea,
+  M_APP11 = 0xeb,
+  M_APP12 = 0xec,
+  M_APP13 = 0xed,
+  M_APP14 = 0xee,
+  M_APP15 = 0xef,
+  
+  M_JPG0  = 0xf0,
+  M_JPG13 = 0xfd,
+  M_COM   = 0xfe,
+  
+  M_TEM   = 0x01,
+  
+  M_ERROR = 0x100
+} JPEG_MARKER;
+
+
+/*
+ * Basic output routines.
+ *
+ * Note that we do not support suspension while writing a marker.
+ * Therefore, an application using suspension must ensure that there is
+ * enough buffer space for the initial markers (typ. 600-700 bytes) before
+ * calling jpeg_start_compress, and enough space to write the trailing EOI
+ * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
+ * modes are not supported at all with suspension, so those two are the only
+ * points where markers will be written.
+ */
+
+LOCAL void
+emit_byte (j_compress_ptr cinfo, int val)
+/* Emit a byte */
+{
+  struct jpeg_destination_mgr * dest = cinfo->dest;
+
+  *(dest->next_output_byte)++ = (JOCTET) val;
+  if (--dest->free_in_buffer == 0) {
+    if (! (*dest->empty_output_buffer) (cinfo))
+      ERREXIT(cinfo, JERR_CANT_SUSPEND);
+  }
+}
+
+
+LOCAL void
+emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
+/* Emit a marker code */
+{
+  emit_byte(cinfo, 0xFF);
+  emit_byte(cinfo, (int) mark);
+}
+
+
+LOCAL void
+emit_2bytes (j_compress_ptr cinfo, int value)
+/* Emit a 2-byte integer; these are always MSB first in JPEG files */
+{
+  emit_byte(cinfo, (value >> 8) & 0xFF);
+  emit_byte(cinfo, value & 0xFF);
+}
+
+
+/*
+ * Routines to write specific marker types.
+ */
+
+LOCAL int
+emit_dqt (j_compress_ptr cinfo, int index)
+/* Emit a DQT marker */
+/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
+{
+  JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
+  int prec;
+  int i;
+
+  if (qtbl == NULL)
+    ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
+
+  prec = 0;
+  for (i = 0; i < DCTSIZE2; i++) {
+    if (qtbl->quantval[i] > 255)
+      prec = 1;
+  }
+
+  if (! qtbl->sent_table) {
+    emit_marker(cinfo, M_DQT);
+
+    emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
+
+    emit_byte(cinfo, index + (prec<<4));
+
+    for (i = 0; i < DCTSIZE2; i++) {
+      if (prec)
+	emit_byte(cinfo, qtbl->quantval[i] >> 8);
+      emit_byte(cinfo, qtbl->quantval[i] & 0xFF);
+    }
+
+    qtbl->sent_table = TRUE;
+  }
+
+  return prec;
+}
+
+
+LOCAL void
+emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
+/* Emit a DHT marker */
+{
+  JHUFF_TBL * htbl;
+  int length, i;
+  
+  if (is_ac) {
+    htbl = cinfo->ac_huff_tbl_ptrs[index];
+    index += 0x10;		/* output index has AC bit set */
+  } else {
+    htbl = cinfo->dc_huff_tbl_ptrs[index];
+  }
+
+  if (htbl == NULL)
+    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
+  
+  if (! htbl->sent_table) {
+    emit_marker(cinfo, M_DHT);
+    
+    length = 0;
+    for (i = 1; i <= 16; i++)
+      length += htbl->bits[i];
+    
+    emit_2bytes(cinfo, length + 2 + 1 + 16);
+    emit_byte(cinfo, index);
+    
+    for (i = 1; i <= 16; i++)
+      emit_byte(cinfo, htbl->bits[i]);
+    
+    for (i = 0; i < length; i++)
+      emit_byte(cinfo, htbl->huffval[i]);
+    
+    htbl->sent_table = TRUE;
+  }
+}
+
+
+LOCAL void
+emit_dac (j_compress_ptr cinfo)
+/* Emit a DAC marker */
+/* Since the useful info is so small, we want to emit all the tables in */
+/* one DAC marker.  Therefore this routine does its own scan of the table. */
+{
+#ifdef C_ARITH_CODING_SUPPORTED
+  char dc_in_use[NUM_ARITH_TBLS];
+  char ac_in_use[NUM_ARITH_TBLS];
+  int length, i;
+  jpeg_component_info *compptr;
+  
+  for (i = 0; i < NUM_ARITH_TBLS; i++)
+    dc_in_use[i] = ac_in_use[i] = 0;
+  
+  for (i = 0; i < cinfo->comps_in_scan; i++) {
+    compptr = cinfo->cur_comp_info[i];
+    dc_in_use[compptr->dc_tbl_no] = 1;
+    ac_in_use[compptr->ac_tbl_no] = 1;
+  }
+  
+  length = 0;
+  for (i = 0; i < NUM_ARITH_TBLS; i++)
+    length += dc_in_use[i] + ac_in_use[i];
+  
+  emit_marker(cinfo, M_DAC);
+  
+  emit_2bytes(cinfo, length*2 + 2);
+  
+  for (i = 0; i < NUM_ARITH_TBLS; i++) {
+    if (dc_in_use[i]) {
+      emit_byte(cinfo, i);
+      emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
+    }
+    if (ac_in_use[i]) {
+      emit_byte(cinfo, i + 0x10);
+      emit_byte(cinfo, cinfo->arith_ac_K[i]);
+    }
+  }
+#endif /* C_ARITH_CODING_SUPPORTED */
+}
+
+
+LOCAL void
+emit_dri (j_compress_ptr cinfo)
+/* Emit a DRI marker */
+{
+  emit_marker(cinfo, M_DRI);
+  
+  emit_2bytes(cinfo, 4);	/* fixed length */
+
+  emit_2bytes(cinfo, (int) cinfo->restart_interval);
+}
+
+
+LOCAL void
+emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
+/* Emit a SOF marker */
+{
+  int ci;
+  jpeg_component_info *compptr;
+  
+  emit_marker(cinfo, code);
+  
+  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
+
+  /* Make sure image isn't bigger than SOF field can handle */
+  if ((long) cinfo->image_height > 65535L ||
+      (long) cinfo->image_width > 65535L)
+    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
+
+  emit_byte(cinfo, cinfo->data_precision);
+  emit_2bytes(cinfo, (int) cinfo->image_height);
+  emit_2bytes(cinfo, (int) cinfo->image_width);
+
+  emit_byte(cinfo, cinfo->num_components);
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    emit_byte(cinfo, compptr->component_id);
+    emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
+    emit_byte(cinfo, compptr->quant_tbl_no);
+  }
+}
+
+
+LOCAL void
+emit_sos (j_compress_ptr cinfo)
+/* Emit a SOS marker */
+{
+  int i;
+  jpeg_component_info *compptr;
+  
+  emit_marker(cinfo, M_SOS);
+  
+  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
+  
+  emit_byte(cinfo, cinfo->comps_in_scan);
+  
+  for (i = 0; i < cinfo->comps_in_scan; i++) {
+    compptr = cinfo->cur_comp_info[i];
+    emit_byte(cinfo, compptr->component_id);
+    emit_byte(cinfo, (compptr->dc_tbl_no << 4) + compptr->ac_tbl_no);
+  }
+
+  emit_byte(cinfo, 0);		/* Spectral selection start */
+  emit_byte(cinfo, DCTSIZE2-1);	/* Spectral selection end */
+  emit_byte(cinfo, 0);		/* Successive approximation */
+}
+
+
+LOCAL void
+emit_jfif_app0 (j_compress_ptr cinfo)
+/* Emit a JFIF-compliant APP0 marker */
+{
+  /*
+   * Length of APP0 block	(2 bytes)
+   * Block ID			(4 bytes - ASCII "JFIF")
+   * Zero byte			(1 byte to terminate the ID string)
+   * Version Major, Minor	(2 bytes - 0x01, 0x01)
+   * Units			(1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
+   * Xdpu			(2 bytes - dots per unit horizontal)
+   * Ydpu			(2 bytes - dots per unit vertical)
+   * Thumbnail X size		(1 byte)
+   * Thumbnail Y size		(1 byte)
+   */
+  
+  emit_marker(cinfo, M_APP0);
+  
+  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
+
+  emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
+  emit_byte(cinfo, 0x46);
+  emit_byte(cinfo, 0x49);
+  emit_byte(cinfo, 0x46);
+  emit_byte(cinfo, 0);
+  /* We currently emit version code 1.01 since we use no 1.02 features.
+   * This may avoid complaints from some older decoders.
+   */
+  emit_byte(cinfo, 1);		/* Major version */
+  emit_byte(cinfo, 1);		/* Minor version */
+  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
+  emit_2bytes(cinfo, (int) cinfo->X_density);
+  emit_2bytes(cinfo, (int) cinfo->Y_density);
+  emit_byte(cinfo, 0);		/* No thumbnail image */
+  emit_byte(cinfo, 0);
+}
+
+
+LOCAL void
+emit_adobe_app14 (j_compress_ptr cinfo)
+/* Emit an Adobe APP14 marker */
+{
+  /*
+   * Length of APP14 block	(2 bytes)
+   * Block ID			(5 bytes - ASCII "Adobe")
+   * Version Number		(2 bytes - currently 100)
+   * Flags0			(2 bytes - currently 0)
+   * Flags1			(2 bytes - currently 0)
+   * Color transform		(1 byte)
+   *
+   * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
+   * now in circulation seem to use Version = 100, so that's what we write.
+   *
+   * We write the color transform byte as 1 if the JPEG color space is
+   * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
+   * whether the encoder performed a transformation, which is pretty useless.
+   */
+  
+  emit_marker(cinfo, M_APP14);
+  
+  emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
+
+  emit_byte(cinfo, 0x41);	/* Identifier: ASCII "Adobe" */
+  emit_byte(cinfo, 0x64);
+  emit_byte(cinfo, 0x6F);
+  emit_byte(cinfo, 0x62);
+  emit_byte(cinfo, 0x65);
+  emit_2bytes(cinfo, 100);	/* Version */
+  emit_2bytes(cinfo, 0);	/* Flags0 */
+  emit_2bytes(cinfo, 0);	/* Flags1 */
+  switch (cinfo->jpeg_color_space) {
+  case JCS_YCbCr:
+    emit_byte(cinfo, 1);	/* Color transform = 1 */
+    break;
+  case JCS_YCCK:
+    emit_byte(cinfo, 2);	/* Color transform = 2 */
+    break;
+  default:
+    emit_byte(cinfo, 0);	/* Color transform = 0 */
+    break;
+  }
+}
+
+
+/*
+ * This routine is exported for possible use by applications.
+ * The intended use is to emit COM or APPn markers after calling
+ * jpeg_start_compress() and before the first jpeg_write_scanlines() call
+ * (hence, after write_file_header but before write_frame_header).
+ * Other uses are not guaranteed to produce desirable results.
+ */
+
+METHODDEF void
+write_any_marker (j_compress_ptr cinfo, int marker,
+		  const JOCTET *dataptr, unsigned int datalen)
+/* Emit an arbitrary marker with parameters */
+{
+  if (datalen <= (unsigned int) 65533) { /* safety check */
+    emit_marker(cinfo, (JPEG_MARKER) marker);
+  
+    emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
+
+    while (datalen--) {
+      emit_byte(cinfo, *dataptr);
+      dataptr++;
+    }
+  }
+}
+
+
+/*
+ * Write datastream header.
+ * This consists of an SOI and optional APPn markers.
+ * We recommend use of the JFIF marker, but not the Adobe marker,
+ * when using YCbCr or grayscale data.  The JFIF marker should NOT
+ * be used for any other JPEG colorspace.  The Adobe marker is helpful
+ * to distinguish RGB, CMYK, and YCCK colorspaces.
+ * Note that an application can write additional header markers after
+ * jpeg_start_decompress returns.
+ */
+
+METHODDEF void
+write_file_header (j_compress_ptr cinfo)
+{
+  emit_marker(cinfo, M_SOI);	/* first the SOI */
+
+  if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
+    emit_jfif_app0(cinfo);
+  if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
+    emit_adobe_app14(cinfo);
+}
+
+
+/*
+ * Write frame header.
+ * This consists of DQT and SOFn markers.
+ * Note that we do not emit the SOF until we have emitted the DQT(s).
+ * This avoids compatibility problems with incorrect implementations that
+ * try to error-check the quant table numbers as soon as they see the SOF.
+ */
+
+METHODDEF void
+write_frame_header (j_compress_ptr cinfo)
+{
+  int ci, prec;
+  boolean is_baseline;
+  jpeg_component_info *compptr;
+  
+  /* Emit DQT for each quantization table.
+   * Note that emit_dqt() suppresses any duplicate tables.
+   */
+  prec = 0;
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    prec += emit_dqt(cinfo, compptr->quant_tbl_no);
+  }
+  /* now prec is nonzero iff there are any 16-bit quant tables. */
+
+  /* Check for a non-baseline specification.
+   * Note we assume that Huffman table numbers won't be changed later.
+   */
+  is_baseline = TRUE;
+  if (cinfo->arith_code || (cinfo->data_precision != 8))
+    is_baseline = FALSE;
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
+      is_baseline = FALSE;
+  }
+  if (prec && is_baseline) {
+    is_baseline = FALSE;
+    /* If it's baseline except for quantizer size, warn the user */
+    TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
+  }
+
+  /* Emit the proper SOF marker */
+  if (cinfo->arith_code)
+    emit_sof(cinfo, M_SOF9);	/* SOF code for arithmetic coding */
+  else if (is_baseline)
+    emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
+  else
+    emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
+}
+
+
+/*
+ * Write scan header.
+ * This consists of DHT or DAC markers, optional DRI, and SOS.
+ * Compressed data will be written following the SOS.
+ */
+
+METHODDEF void
+write_scan_header (j_compress_ptr cinfo)
+{
+  int i;
+  jpeg_component_info *compptr;
+
+  if (cinfo->arith_code) {
+    /* Emit arith conditioning info.  We may have some duplication
+     * if the file has multiple scans, but it's so small it's hardly
+     * worth worrying about.
+     */
+    emit_dac(cinfo);
+  } else {
+    /* Emit Huffman tables.
+     * Note that emit_dht() suppresses any duplicate tables.
+     */
+    for (i = 0; i < cinfo->comps_in_scan; i++) {
+      compptr = cinfo->cur_comp_info[i];
+      emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
+      emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
+    }
+  }
+
+  /* Emit DRI if required --- note that DRI value could change for each scan.
+   * If it doesn't, a tiny amount of space is wasted in multiple-scan files.
+   * We assume DRI will never be nonzero for one scan and zero for a later one.
+   */
+  if (cinfo->restart_interval)
+    emit_dri(cinfo);
+
+  emit_sos(cinfo);
+}
+
+
+/*
+ * Write datastream trailer.
+ */
+
+METHODDEF void
+write_file_trailer (j_compress_ptr cinfo)
+{
+  emit_marker(cinfo, M_EOI);
+}
+
+
+/*
+ * Write an abbreviated table-specification datastream.
+ * This consists of SOI, DQT and DHT tables, and EOI.
+ * Any table that is defined and not marked sent_table = TRUE will be
+ * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
+ */
+
+METHODDEF void
+write_tables_only (j_compress_ptr cinfo)
+{
+  int i;
+
+  emit_marker(cinfo, M_SOI);
+
+  for (i = 0; i < NUM_QUANT_TBLS; i++) {
+    if (cinfo->quant_tbl_ptrs[i] != NULL)
+      (void) emit_dqt(cinfo, i);
+  }
+
+  if (! cinfo->arith_code) {
+    for (i = 0; i < NUM_HUFF_TBLS; i++) {
+      if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
+	emit_dht(cinfo, i, FALSE);
+      if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
+	emit_dht(cinfo, i, TRUE);
+    }
+  }
+
+  emit_marker(cinfo, M_EOI);
+}
+
+
+/*
+ * Initialize the marker writer module.
+ */
+
+GLOBAL void
+jinit_marker_writer (j_compress_ptr cinfo)
+{
+  /* Create the subobject */
+  cinfo->marker = (struct jpeg_marker_writer *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(struct jpeg_marker_writer));
+  /* Initialize method pointers */
+  cinfo->marker->write_any_marker = write_any_marker;
+  cinfo->marker->write_file_header = write_file_header;
+  cinfo->marker->write_frame_header = write_frame_header;
+  cinfo->marker->write_scan_header = write_scan_header;
+  cinfo->marker->write_file_trailer = write_file_trailer;
+  cinfo->marker->write_tables_only = write_tables_only;
+}
diff --git a/jcmaster.c b/jcmaster.c
index b192aae..d9b5c03 100644
--- a/jcmaster.c
+++ b/jcmaster.c
@@ -1,133 +1,387 @@
 /*
  * jcmaster.c
  *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
- * This file contains the main control for the JPEG compressor.
- * The system-dependent (user interface) code should call jpeg_compress()
- * after doing appropriate setup of the compress_info_struct parameter.
+ * This file contains master control logic for the JPEG compressor.
+ * These routines are concerned with selecting the modules to be executed
+ * and with determining the number of passes and the work to be done in each
+ * pass.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
 
 
-METHODDEF void
-c_per_scan_method_selection (compress_info_ptr cinfo)
-/* Central point for per-scan method selection */
-{
-  /* Edge expansion */
-  jselexpand(cinfo);
-  /* Downsampling of pixels */
-  jseldownsample(cinfo);
-  /* MCU extraction */
-  jselcmcu(cinfo);
-}
+/* Private state */
 
+typedef struct {
+  struct jpeg_comp_master pub;	/* public fields */
+
+  int pass_number;		/* eventually need more complex state... */
+} my_comp_master;
+
+typedef my_comp_master * my_master_ptr;
+
+
+/*
+ * Support routines that do various essential calculations.
+ */
 
 LOCAL void
-c_initial_method_selection (compress_info_ptr cinfo)
-/* Central point for initial method selection */
+initial_setup (j_compress_ptr cinfo)
+/* Do computations that are needed before master selection phase */
 {
-  /* Input image reading method selection is already done. */
-  /* So is output file header formatting (both are done by user interface). */
-
-  /* Gamma and color space conversion */
-  jselccolor(cinfo);
-  /* Entropy encoding: either Huffman or arithmetic coding. */
-#ifdef C_ARITH_CODING_SUPPORTED
-  jselcarithmetic(cinfo);
-#else
-  cinfo->arith_code = FALSE;	/* force Huffman mode */
-#endif
-  jselchuffman(cinfo);
-  /* Pipeline control */
-  jselcpipeline(cinfo);
-  /* Overall control (that's me!) */
-  cinfo->methods->c_per_scan_method_selection = c_per_scan_method_selection;
-}
-
-
-LOCAL void
-initial_setup (compress_info_ptr cinfo)
-/* Do computations that are needed before initial method selection */
-{
-  short ci;
+  int ci;
   jpeg_component_info *compptr;
+  long samplesperrow;
+  JDIMENSION jd_samplesperrow;
+
+  /* Sanity check on image dimensions */
+  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
+      || cinfo->num_components <= 0 || cinfo->input_components <= 0)
+    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
+
+  /* Make sure image isn't bigger than I can handle */
+  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
+      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
+    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
+
+  /* Width of an input scanline must be representable as JDIMENSION. */
+  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
+  jd_samplesperrow = (JDIMENSION) samplesperrow;
+  if ((long) jd_samplesperrow != samplesperrow)
+    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+
+  /* For now, precision must match compiled-in value... */
+  if (cinfo->data_precision != BITS_IN_JSAMPLE)
+    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
+
+  /* Check that number of components won't exceed internal array sizes */
+  if (cinfo->num_components > MAX_COMPONENTS)
+    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+	     MAX_COMPONENTS);
 
   /* Compute maximum sampling factors; check factor validity */
   cinfo->max_h_samp_factor = 1;
   cinfo->max_v_samp_factor = 1;
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    compptr = &cinfo->comp_info[ci];
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
 	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
-      ERREXIT(cinfo->emethods, "Bogus sampling factors");
+      ERREXIT(cinfo, JERR_BAD_SAMPLING);
     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
 				   compptr->h_samp_factor);
     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
 				   compptr->v_samp_factor);
-
   }
 
-  /* Compute logical downsampled dimensions of components */
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    compptr = &cinfo->comp_info[ci];
-    compptr->true_comp_width = (cinfo->image_width * compptr->h_samp_factor
-				+ cinfo->max_h_samp_factor - 1)
-				/ cinfo->max_h_samp_factor;
-    compptr->true_comp_height = (cinfo->image_height * compptr->v_samp_factor
-				 + cinfo->max_v_samp_factor - 1)
-				 / cinfo->max_v_samp_factor;
+  /* Compute dimensions of components */
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    /* For compression, we never do DCT scaling. */
+    compptr->DCT_scaled_size = DCTSIZE;
+    /* Size in DCT blocks */
+    compptr->width_in_blocks = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
+    compptr->height_in_blocks = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
+    /* Size in samples */
+    compptr->downsampled_width = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+		    (long) cinfo->max_h_samp_factor);
+    compptr->downsampled_height = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+		    (long) cinfo->max_v_samp_factor);
+    /* Mark component needed (this flag isn't actually used for compression) */
+    compptr->component_needed = TRUE;
+  }
+
+  /* Compute number of fully interleaved MCU rows (number of times that
+   * main controller will call coefficient controller).
+   */
+  cinfo->total_iMCU_rows = (JDIMENSION)
+    jdiv_round_up((long) cinfo->image_height,
+		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
+}
+
+
+LOCAL void
+per_scan_setup (j_compress_ptr cinfo)
+/* Do computations that are needed before processing a JPEG scan */
+/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
+{
+  int ci, mcublks, tmp;
+  jpeg_component_info *compptr;
+  
+  if (cinfo->comps_in_scan == 1) {
+    
+    /* Noninterleaved (single-component) scan */
+    compptr = cinfo->cur_comp_info[0];
+    
+    /* Overall image size in MCUs */
+    cinfo->MCUs_per_row = compptr->width_in_blocks;
+    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
+    
+    /* For noninterleaved scan, always one block per MCU */
+    compptr->MCU_width = 1;
+    compptr->MCU_height = 1;
+    compptr->MCU_blocks = 1;
+    compptr->MCU_sample_width = DCTSIZE;
+    compptr->last_col_width = 1;
+    compptr->last_row_height = 1;
+    
+    /* Prepare array describing MCU composition */
+    cinfo->blocks_in_MCU = 1;
+    cinfo->MCU_membership[0] = 0;
+    
+  } else {
+    
+    /* Interleaved (multi-component) scan */
+    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
+      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
+	       MAX_COMPS_IN_SCAN);
+    
+    /* Overall image size in MCUs */
+    cinfo->MCUs_per_row = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_width,
+		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
+    cinfo->MCU_rows_in_scan = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_height,
+		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
+    
+    cinfo->blocks_in_MCU = 0;
+    
+    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+      compptr = cinfo->cur_comp_info[ci];
+      /* Sampling factors give # of blocks of component in each MCU */
+      compptr->MCU_width = compptr->h_samp_factor;
+      compptr->MCU_height = compptr->v_samp_factor;
+      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
+      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
+      /* Figure number of non-dummy blocks in last MCU column & row */
+      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
+      if (tmp == 0) tmp = compptr->MCU_width;
+      compptr->last_col_width = tmp;
+      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
+      if (tmp == 0) tmp = compptr->MCU_height;
+      compptr->last_row_height = tmp;
+      /* Prepare array describing MCU composition */
+      mcublks = compptr->MCU_blocks;
+      if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
+	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
+      while (mcublks-- > 0) {
+	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
+      }
+    }
+    
+  }
+
+  /* Convert restart specified in rows to actual MCU count. */
+  /* Note that count must fit in 16 bits, so we provide limiting. */
+  if (cinfo->restart_in_rows > 0) {
+    long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
+    cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
   }
 }
 
 
 /*
- * This is the main entry point to the JPEG compressor.
+ * Master selection of compression modules.
+ * This is done once at the start of processing an image.  We determine
+ * which modules will be used and give them appropriate initialization calls.
  */
 
+LOCAL void
+master_selection (j_compress_ptr cinfo)
+{
+  my_master_ptr master = (my_master_ptr) cinfo->master;
+
+  initial_setup(cinfo);
+  master->pass_number = 0;
+
+  /* There's not a lot of smarts here right now, but it'll get more
+   * complicated when we have multiple implementations available...
+   */
+
+  /* Preprocessing */
+  if (! cinfo->raw_data_in) {
+    jinit_color_converter(cinfo);
+    jinit_downsampler(cinfo);
+    jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
+  }
+  /* Forward DCT */
+  jinit_forward_dct(cinfo);
+  /* Entropy encoding: either Huffman or arithmetic coding. */
+  if (cinfo->arith_code) {
+#ifdef C_ARITH_CODING_SUPPORTED
+    jinit_arith_encoder(cinfo);
+#else
+    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+#endif
+  } else
+    jinit_huff_encoder(cinfo);
+
+  /* For now, a full buffer is needed only for Huffman optimization. */
+  jinit_c_coef_controller(cinfo, cinfo->optimize_coding);
+  jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
+
+  jinit_marker_writer(cinfo);
+
+  /* We can now tell the memory manager to allocate virtual arrays. */
+  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+
+  /* Write the datastream header (SOI) immediately.
+   * Frame and scan headers are postponed till later.
+   * This lets application insert special markers after the SOI.
+   */
+  (*cinfo->marker->write_file_header) (cinfo);
+}
+
+
+/*
+ * Per-pass setup.
+ * This is called at the beginning of each pass.  We determine which modules
+ * will be active during this pass and give them appropriate start_pass calls.
+ * We also set is_last_pass to indicate whether any more passes will be
+ * required.
+ */
+
+METHODDEF void
+prepare_for_pass (j_compress_ptr cinfo)
+{
+  my_master_ptr master = (my_master_ptr) cinfo->master;
+  int ci;
+  int npasses;
+
+  /* ???? JUST A QUICK CROCK FOR NOW ??? */
+
+  /* For now, handle only single interleaved output scan; */
+  /* we support two passes for Huffman optimization. */
+
+  /* Prepare for single scan containing all components */
+  if (cinfo->num_components > MAX_COMPS_IN_SCAN)
+    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+	     MAX_COMPS_IN_SCAN);
+  cinfo->comps_in_scan = cinfo->num_components;
+  for (ci = 0; ci < cinfo->num_components; ci++) {
+    cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
+  }
+
+  per_scan_setup(cinfo);
+
+  if (! cinfo->optimize_coding) {
+    /* Standard single-pass case */
+    npasses = 1;
+    master->pub.call_pass_startup = TRUE;
+    master->pub.is_last_pass = TRUE;
+    if (! cinfo->raw_data_in) {
+      (*cinfo->cconvert->start_pass) (cinfo);
+      (*cinfo->downsample->start_pass) (cinfo);
+      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
+    }
+    (*cinfo->fdct->start_pass) (cinfo);
+    (*cinfo->entropy->start_pass) (cinfo, FALSE);
+    (*cinfo->coef->start_pass) (cinfo, JBUF_PASS_THRU);
+    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
+  } else {
+    npasses = 2;
+    switch (master->pass_number) {
+    case 0:
+      /* Huffman optimization: run all modules, gather statistics */
+      master->pub.call_pass_startup = FALSE;
+      master->pub.is_last_pass = FALSE;
+      if (! cinfo->raw_data_in) {
+	(*cinfo->cconvert->start_pass) (cinfo);
+	(*cinfo->downsample->start_pass) (cinfo);
+	(*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
+      }
+      (*cinfo->fdct->start_pass) (cinfo);
+      (*cinfo->entropy->start_pass) (cinfo, TRUE);
+      (*cinfo->coef->start_pass) (cinfo, JBUF_SAVE_AND_PASS);
+      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
+      break;
+    case 1:
+      /* Second pass: reread data from coefficient buffer */
+      master->pub.is_last_pass = TRUE;
+      (*cinfo->entropy->start_pass) (cinfo, FALSE);
+      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
+      /* We emit frame/scan headers now */
+      (*cinfo->marker->write_frame_header) (cinfo);
+      (*cinfo->marker->write_scan_header) (cinfo);
+      break;
+    }
+  }
+
+  /* Set up progress monitor's pass info if present */
+  if (cinfo->progress != NULL) {
+    cinfo->progress->completed_passes = master->pass_number;
+    cinfo->progress->total_passes = npasses;
+  }
+
+  master->pass_number++;
+}
+
+
+/*
+ * Special start-of-pass hook.
+ * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
+ * In single-pass processing, we need this hook because we don't want to
+ * write frame/scan headers during jpeg_start_compress; we want to let the
+ * application write COM markers etc. between jpeg_start_compress and the
+ * jpeg_write_scanlines loop.
+ * In multi-pass processing, this routine is not used.
+ */
+
+METHODDEF void
+pass_startup (j_compress_ptr cinfo)
+{
+  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
+
+  (*cinfo->marker->write_frame_header) (cinfo);
+  (*cinfo->marker->write_scan_header) (cinfo);
+}
+
+
+/*
+ * Finish up at end of pass.
+ */
+
+METHODDEF void
+finish_pass_master (j_compress_ptr cinfo)
+{
+  /* More complex logic later ??? */
+
+  /* The entropy coder needs an end-of-pass call, either to analyze
+   * statistics or to flush its output buffer.
+   */
+  (*cinfo->entropy->finish_pass) (cinfo);
+}
+
+
+/*
+ * Initialize master compression control.
+ * This creates my own subrecord and also performs the master selection phase,
+ * which causes other modules to create their subrecords.
+ */
 
 GLOBAL void
-jpeg_compress (compress_info_ptr cinfo)
+jinit_master_compress (j_compress_ptr cinfo)
 {
-  /* Init pass counts to 0 --- total_passes is adjusted in method selection */
-  cinfo->total_passes = 0;
-  cinfo->completed_passes = 0;
+  my_master_ptr master;
 
-  /* Read the input file header: determine image size & component count.
-   * NOTE: the user interface must have initialized the input_init method
-   * pointer (eg, by calling jselrppm) before calling me.
-   * The other file reading methods (get_input_row etc.) were probably
-   * set at the same time, but could be set up by input_init itself,
-   * or by c_ui_method_selection.
-   */
-  (*cinfo->methods->input_init) (cinfo);
+  master = (my_master_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(my_comp_master));
+  cinfo->master = (struct jpeg_comp_master *) master;
+  master->pub.prepare_for_pass = prepare_for_pass;
+  master->pub.pass_startup = pass_startup;
+  master->pub.finish_pass = finish_pass_master;
 
-  /* Give UI a chance to adjust compression parameters and select */
-  /* output file format based on results of input_init. */
-  (*cinfo->methods->c_ui_method_selection) (cinfo);
-
-  /* Now select methods for compression steps. */
-  initial_setup(cinfo);
-  c_initial_method_selection(cinfo);
-
-  /* Initialize the output file & other modules as needed */
-  /* (entropy_encoder is inited by pipeline controller) */
-
-  (*cinfo->methods->colorin_init) (cinfo);
-  (*cinfo->methods->write_file_header) (cinfo);
-
-  /* And let the pipeline controller do the rest. */
-  (*cinfo->methods->c_pipeline_controller) (cinfo);
-
-  /* Finish output file, release working storage, etc */
-  (*cinfo->methods->write_file_trailer) (cinfo);
-  (*cinfo->methods->colorin_term) (cinfo);
-  (*cinfo->methods->input_term) (cinfo);
-
-  (*cinfo->emethods->free_all) ();
-
-  /* My, that was easy, wasn't it? */
+  master_selection(cinfo);
 }
diff --git a/jcmcu.c b/jcmcu.c
deleted file mode 100644
index 524a54f..0000000
--- a/jcmcu.c
+++ /dev/null
@@ -1,228 +0,0 @@
-/*
- * jcmcu.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains MCU extraction routines and quantization scaling.
- * These routines are invoked via the extract_MCUs and
- * extract_init/term methods.
- */
-
-#include "jinclude.h"
-
-
-/*
- * If this file is compiled with -DDCT_ERR_STATS, it will reverse-DCT each
- * block and sum the total errors across the whole picture.  This provides
- * a convenient method of using real picture data to test the roundoff error
- * of a DCT algorithm.  DCT_ERR_STATS should *not* be defined for a production
- * compression program, since compression is much slower with it defined.
- * Also note that jrevdct.o must be linked into the compressor when this
- * switch is defined.
- */
-
-#ifdef DCT_ERR_STATS
-static int dcterrorsum;		/* these hold the error statistics */
-static int dcterrormax;
-static int dctcoefcount;	/* This will probably overflow on a 16-bit-int machine */
-#endif
-
-
-/* ZAG[i] is the natural-order position of the i'th element of zigzag order. */
-
-static const int ZAG[DCTSIZE2] = {
-  0,  1,  8, 16,  9,  2,  3, 10,
- 17, 24, 32, 25, 18, 11,  4,  5,
- 12, 19, 26, 33, 40, 48, 41, 34,
- 27, 20, 13,  6,  7, 14, 21, 28,
- 35, 42, 49, 56, 57, 50, 43, 36,
- 29, 22, 15, 23, 30, 37, 44, 51,
- 58, 59, 52, 45, 38, 31, 39, 46,
- 53, 60, 61, 54, 47, 55, 62, 63
-};
-
-
-LOCAL void
-extract_block (JSAMPARRAY input_data, int start_row, long start_col,
-	       JBLOCK output_data, QUANT_TBL_PTR quanttbl)
-/* Extract one 8x8 block from the specified location in the sample array; */
-/* perform forward DCT, quantization scaling, and zigzag reordering on it. */
-{
-  /* This routine is heavily used, so it's worth coding it tightly. */
-  DCTBLOCK block;
-#ifdef DCT_ERR_STATS
-  DCTBLOCK svblock;		/* saves input data for comparison */
-#endif
-
-  { register JSAMPROW elemptr;
-    register DCTELEM *localblkptr = block;
-    register int elemr;
-
-    for (elemr = DCTSIZE; elemr > 0; elemr--) {
-      elemptr = input_data[start_row++] + start_col;
-#if DCTSIZE == 8		/* unroll the inner loop */
-      *localblkptr++ = (DCTELEM) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-      *localblkptr++ = (DCTELEM) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-      *localblkptr++ = (DCTELEM) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-      *localblkptr++ = (DCTELEM) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-      *localblkptr++ = (DCTELEM) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-      *localblkptr++ = (DCTELEM) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-      *localblkptr++ = (DCTELEM) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-      *localblkptr++ = (DCTELEM) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-#else
-      { register int elemc;
-	for (elemc = DCTSIZE; elemc > 0; elemc--) {
-	  *localblkptr++ = (DCTELEM) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-	}
-      }
-#endif
-    }
-  }
-
-#ifdef DCT_ERR_STATS
-  MEMCOPY(svblock, block, SIZEOF(DCTBLOCK));
-#endif
-
-  j_fwd_dct(block);
-
-  { register JCOEF temp;
-    register QUANT_VAL qval;
-    register int i;
-
-    for (i = 0; i < DCTSIZE2; i++) {
-      qval = *quanttbl++;
-      temp = (JCOEF) block[ZAG[i]];
-      /* Divide the coefficient value by qval, ensuring proper rounding.
-       * Since C does not specify the direction of rounding for negative
-       * quotients, we have to force the dividend positive for portability.
-       *
-       * In most files, at least half of the output values will be zero
-       * (at default quantization settings, more like three-quarters...)
-       * so we should ensure that this case is fast.  On many machines,
-       * a comparison is enough cheaper than a divide to make a special test
-       * a win.  Since both inputs will be nonnegative, we need only test
-       * for a < b to discover whether a/b is 0.
-       * If your machine's division is fast enough, define FAST_DIVIDE.
-       */
-#ifdef FAST_DIVIDE
-#define DIVIDE_BY(a,b)	a /= b
-#else
-#define DIVIDE_BY(a,b)	(a >= b) ? (a /= b) : (a = 0)
-#endif
-      if (temp < 0) {
-	temp = -temp;
-	temp += qval>>1;	/* for rounding */
-	DIVIDE_BY(temp, qval);
-	temp = -temp;
-      } else {
-	temp += qval>>1;	/* for rounding */
-	DIVIDE_BY(temp, qval);
-      }
-      *output_data++ = temp;
-    }
-  }
-
-#ifdef DCT_ERR_STATS
-  j_rev_dct(block);
-
-  { register int diff;
-    register int i;
-
-    for (i = 0; i < DCTSIZE2; i++) {
-      diff = block[i] - svblock[i];
-      if (diff < 0) diff = -diff;
-      dcterrorsum += diff;
-      if (dcterrormax < diff) dcterrormax = diff;
-    }
-    dctcoefcount += DCTSIZE2;
-  }
-#endif
-}
-
-
-/*
- * Extract samples in MCU order, process & hand off to output_method.
- * The input is always exactly N MCU rows worth of data.
- */
-
-METHODDEF void
-extract_MCUs (compress_info_ptr cinfo,
-	      JSAMPIMAGE image_data,
-	      int num_mcu_rows,
-	      MCU_output_method_ptr output_method)
-{
-  JBLOCK MCU_data[MAX_BLOCKS_IN_MCU];
-  int mcurow;
-  long mcuindex;
-  short blkn, ci, xpos, ypos;
-  jpeg_component_info * compptr;
-  QUANT_TBL_PTR quant_ptr;
-
-  for (mcurow = 0; mcurow < num_mcu_rows; mcurow++) {
-    for (mcuindex = 0; mcuindex < cinfo->MCUs_per_row; mcuindex++) {
-      /* Extract data from the image array, DCT it, and quantize it */
-      blkn = 0;
-      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-	compptr = cinfo->cur_comp_info[ci];
-	quant_ptr = cinfo->quant_tbl_ptrs[compptr->quant_tbl_no];
-	for (ypos = 0; ypos < compptr->MCU_height; ypos++) {
-	  for (xpos = 0; xpos < compptr->MCU_width; xpos++) {
-	    extract_block(image_data[ci],
-			  (mcurow * compptr->MCU_height + ypos)*DCTSIZE,
-			  (mcuindex * compptr->MCU_width + xpos)*DCTSIZE,
-			  MCU_data[blkn], quant_ptr);
-	    blkn++;
-	  }
-	}
-      }
-      /* Send the MCU whereever the pipeline controller wants it to go */
-      (*output_method) (cinfo, MCU_data);
-    }
-  }
-}
-
-
-/*
- * Initialize for processing a scan.
- */
-
-METHODDEF void
-extract_init (compress_info_ptr cinfo)
-{
-  /* no work for now */
-#ifdef DCT_ERR_STATS
-  dcterrorsum = dcterrormax = dctcoefcount = 0;
-#endif
-}
-
-
-/*
- * Clean up after a scan.
- */
-
-METHODDEF void
-extract_term (compress_info_ptr cinfo)
-{
-  /* no work for now */
-#ifdef DCT_ERR_STATS
-  TRACEMS3(cinfo->emethods, 0, "DCT roundoff errors = %d/%d,  max = %d",
-	   dcterrorsum, dctcoefcount, dcterrormax);
-#endif
-}
-
-
-
-/*
- * The method selection routine for MCU extraction.
- */
-
-GLOBAL void
-jselcmcu (compress_info_ptr cinfo)
-{
-  /* just one implementation for now */
-  cinfo->methods->extract_init = extract_init;
-  cinfo->methods->extract_MCUs = extract_MCUs;
-  cinfo->methods->extract_term = extract_term;
-}
diff --git a/jcomapi.c b/jcomapi.c
new file mode 100644
index 0000000..c10903f
--- /dev/null
+++ b/jcomapi.c
@@ -0,0 +1,94 @@
+/*
+ * jcomapi.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains application interface routines that are used for both
+ * compression and decompression.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Abort processing of a JPEG compression or decompression operation,
+ * but don't destroy the object itself.
+ *
+ * For this, we merely clean up all the nonpermanent memory pools.
+ * Note that temp files (virtual arrays) are not allowed to belong to
+ * the permanent pool, so we will be able to close all temp files here.
+ * Closing a data source or destination, if necessary, is the application's
+ * responsibility.
+ */
+
+GLOBAL void
+jpeg_abort (j_common_ptr cinfo)
+{
+  int pool;
+
+  /* Releasing pools in reverse order might help avoid fragmentation
+   * with some (brain-damaged) malloc libraries.
+   */
+  for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
+    (*cinfo->mem->free_pool) (cinfo, pool);
+  }
+
+  /* Reset overall state for possible reuse of object */
+  cinfo->global_state = (cinfo->is_decompressor ? DSTATE_START : CSTATE_START);
+}
+
+
+/*
+ * Destruction of a JPEG object.
+ *
+ * Everything gets deallocated except the master jpeg_compress_struct itself
+ * and the error manager struct.  Both of these are supplied by the application
+ * and must be freed, if necessary, by the application.  (Often they are on
+ * the stack and so don't need to be freed anyway.)
+ * Closing a data source or destination, if necessary, is the application's
+ * responsibility.
+ */
+
+GLOBAL void
+jpeg_destroy (j_common_ptr cinfo)
+{
+  /* We need only tell the memory manager to release everything. */
+  /* NB: mem pointer is NULL if memory mgr failed to initialize. */
+  if (cinfo->mem != NULL)
+    (*cinfo->mem->self_destruct) (cinfo);
+  cinfo->mem = NULL;		/* be safe if jpeg_destroy is called twice */
+  cinfo->global_state = 0;	/* mark it destroyed */
+}
+
+
+/*
+ * Convenience routines for allocating quantization and Huffman tables.
+ * (Would jutils.c be a more reasonable place to put these?)
+ */
+
+GLOBAL JQUANT_TBL *
+jpeg_alloc_quant_table (j_common_ptr cinfo)
+{
+  JQUANT_TBL *tbl;
+
+  tbl = (JQUANT_TBL *)
+    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
+  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
+  return tbl;
+}
+
+
+GLOBAL JHUFF_TBL *
+jpeg_alloc_huff_table (j_common_ptr cinfo)
+{
+  JHUFF_TBL *tbl;
+
+  tbl = (JHUFF_TBL *)
+    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
+  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
+  return tbl;
+}
diff --git a/jconfig.auto b/jconfig.auto
new file mode 100644
index 0000000..eb565ea
--- /dev/null
+++ b/jconfig.auto
@@ -0,0 +1,44 @@
+/* jconfig.auto --- source file edited by configure script */
+/* see jconfig.doc for explanations */
+
+#undef HAVE_PROTOTYPES
+#undef HAVE_UNSIGNED_CHAR
+#undef HAVE_UNSIGNED_SHORT
+#undef void
+#undef const
+#undef CHAR_IS_UNSIGNED
+#undef HAVE_STDDEF_H
+#undef HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+/* Define this if you get warnings about undefined structures. */
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+#undef INLINE
+/* These are for configuring the JPEG memory manager. */
+#undef DEFAULT_MAX_MEM
+#undef NO_MKTEMP
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED		/* BMP image file format */
+#define GIF_SUPPORTED		/* GIF image file format */
+#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED		/* Utah RLE image file format */
+#define TARGA_SUPPORTED		/* Targa image file format */
+
+#undef TWO_FILE_COMMANDLINE
+#undef NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+
+/* Define this if you want percent-done progress reports from cjpeg/djpeg. */
+#undef PROGRESS_REPORT
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/jconfig.bcc b/jconfig.bcc
new file mode 100644
index 0000000..3271ee6
--- /dev/null
+++ b/jconfig.bcc
@@ -0,0 +1,44 @@
+/* jconfig.bcc --- jconfig.h for Borland C (Turbo C) on MS-DOS. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#define NEED_FAR_POINTERS	/* for small or medium memory model */
+#undef NEED_SHORT_EXTERNAL_NAMES
+#define INCOMPLETE_TYPES_BROKEN	/* suppress undefined-structure warnings */
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define USE_MSDOS_MEMMGR	/* Define this if you use jmemdos.c */
+
+#define MAX_ALLOC_CHUNK 65520L	/* Maximum request to malloc() */
+
+#define USE_FMEM		/* Borland has _fmemcpy() and _fmemset() */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED		/* BMP image file format */
+#define GIF_SUPPORTED		/* GIF image file format */
+#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED		/* Utah RLE image file format */
+#define TARGA_SUPPORTED		/* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE
+#define USE_SETMODE		/* Borland has setmode() */
+#define NEED_SIGNAL_CATCHER	/* Define this if you use jmemdos.c */
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT		/* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/jconfig.dj b/jconfig.dj
new file mode 100644
index 0000000..f759a9d
--- /dev/null
+++ b/jconfig.dj
@@ -0,0 +1,38 @@
+/* jconfig.dj --- jconfig.h for DJGPP (Delorie's GNU C port) on MS-DOS. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS	/* DJGPP uses flat 32-bit addressing */
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED		/* BMP image file format */
+#define GIF_SUPPORTED		/* GIF image file format */
+#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED		/* Utah RLE image file format */
+#define TARGA_SUPPORTED		/* Targa image file format */
+
+#undef TWO_FILE_COMMANDLINE	/* optional */
+#define USE_SETMODE		/* Needed to make one-file style work in DJGPP */
+#undef NEED_SIGNAL_CATCHER	/* Define this if you use jmemname.c */
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT		/* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/jconfig.doc b/jconfig.doc
new file mode 100644
index 0000000..c18d1c0
--- /dev/null
+++ b/jconfig.doc
@@ -0,0 +1,155 @@
+/*
+ * jconfig.doc
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file documents the configuration options that are required to
+ * customize the JPEG software for a particular system.
+ *
+ * The actual configuration options for a particular installation are stored
+ * in jconfig.h.  On many machines, jconfig.h can be generated automatically
+ * or copied from one of the "canned" jconfig files that we supply.  But if
+ * you need to generate a jconfig.h file by hand, this file tells you how.
+ *
+ * DO NOT EDIT THIS FILE --- IT WON'T ACCOMPLISH ANYTHING.
+ * EDIT A COPY NAMED JCONFIG.H.
+ */
+
+
+/*
+ * These symbols indicate the properties of your machine or compiler.
+ * #define the symbol if yes, #undef it if no.
+ */
+
+/* Does your compiler support function prototypes?
+ * (If not, you also need to use ansi2knr, see install.doc)
+ */
+#define HAVE_PROTOTYPES
+
+/* Does your compiler support the declaration "unsigned char" ?
+ * How about "unsigned short" ?
+ */
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+
+/* Define "void" as "char" if your compiler doesn't know about type void.
+ * NOTE: be sure to define void such that "void *" represents the most general
+ * pointer type, e.g., that returned by malloc().
+ */
+/* #define void char */
+
+/* Define "const" as empty if your compiler doesn't know the "const" keyword.
+ */
+/* #define const */
+
+/* Define this if an ordinary "char" type is unsigned.
+ * If you're not sure, leaving it undefined will work at some cost in speed.
+ * If you defined HAVE_UNSIGNED_CHAR then the speed difference is minimal.
+ */
+#undef CHAR_IS_UNSIGNED
+
+/* Define this if your system has an ANSI-conforming <stddef.h> file.
+ */
+#define HAVE_STDDEF_H
+
+/* Define this if your system has an ANSI-conforming <stdlib.h> file.
+ */
+#define HAVE_STDLIB_H
+
+/* Define this if your system does not have an ANSI/SysV <string.h>,
+ * but does have a BSD-style <strings.h>.
+ */
+#undef NEED_BSD_STRINGS
+
+/* Define this if your system does not provide typedef size_t in any of the
+ * ANSI-standard places (stddef.h, stdlib.h, or stdio.h), but places it in
+ * <sys/types.h> instead.
+ */
+#undef NEED_SYS_TYPES_H
+
+/* For 80x86 machines, you need to define NEED_FAR_POINTERS,
+ * unless you are using a large-data memory model or 80386 flat-memory mode.
+ * On less brain-damaged CPUs this symbol must not be defined.
+ * (Defining this symbol causes large data structures to be referenced through
+ * "far" pointers and to be allocated with a special version of malloc.)
+ */
+#undef NEED_FAR_POINTERS
+
+/* Define this if your linker needs global names to be unique in less
+ * than the first 15 characters.
+ */
+#undef NEED_SHORT_EXTERNAL_NAMES
+
+/* Although a real ANSI C compiler can deal perfectly well with pointers to
+ * unspecified structures (see "incomplete types" in the spec), a few pre-ANSI
+ * and pseudo-ANSI compilers get confused.  To keep one of these bozos happy,
+ * define INCOMPLETE_TYPES_BROKEN.  This is not recommended unless you
+ * actually get "missing structure definition" warnings or errors while
+ * compiling the JPEG code.
+ */
+#undef INCOMPLETE_TYPES_BROKEN
+
+
+/*
+ * The following options affect code selection within the JPEG library,
+ * but they don't need to be visible to applications using the library.
+ * To minimize application namespace pollution, the symbols won't be
+ * defined unless JPEG_INTERNALS has been defined.
+ */
+
+#ifdef JPEG_INTERNALS
+
+/* Define this if your compiler implements ">>" on signed values as a logical
+ * (unsigned) shift; leave it undefined if ">>" is a signed (arithmetic) shift,
+ * which is the normal and rational definition.
+ */
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+
+#endif /* JPEG_INTERNALS */
+
+
+/*
+ * The remaining options do not affect the JPEG library proper,
+ * but only the sample applications cjpeg/djpeg (see cjpeg.c, djpeg.c).
+ * Other applications can ignore these.
+ */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+/* These defines indicate which image (non-JPEG) file formats are allowed. */
+
+#define BMP_SUPPORTED		/* BMP image file format */
+#define GIF_SUPPORTED		/* GIF image file format */
+#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED		/* Utah RLE image file format */
+#define TARGA_SUPPORTED		/* Targa image file format */
+
+/* Define this if you want to name both input and output files on the command
+ * line, rather than using stdout and optionally stdin.  You MUST do this if
+ * your system can't cope with binary I/O to stdin/stdout.  See comments at
+ * head of cjpeg.c or djpeg.c.
+ */
+#undef TWO_FILE_COMMANDLINE
+
+/* Define this if your system needs explicit cleanup of temporary files.
+ * This is crucial under MS-DOS, where the temporary "files" may be areas
+ * of extended memory; on most other systems it's not as important.
+ */
+#undef NEED_SIGNAL_CATCHER
+
+/* By default, we open image files with fopen(...,"rb") or fopen(...,"wb").
+ * This is necessary on systems that distinguish text files from binary files,
+ * and is harmless on most systems that don't.  If you have one of the rare
+ * systems that complains about the "b" spec, define this symbol.
+ */
+#undef DONT_USE_B_MODE
+
+/* Define this if you want percent-done progress reports from cjpeg/djpeg.
+ */
+#undef PROGRESS_REPORT
+
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/jconfig.h b/jconfig.h
deleted file mode 100644
index 60ca005..0000000
--- a/jconfig.h
+++ /dev/null
@@ -1,360 +0,0 @@
-/*
- * jconfig.h
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains preprocessor declarations that help customize
- * the JPEG software for a particular application, machine, or compiler.
- * Edit these declarations as needed (or add -D flags to the Makefile).
- */
-
-
-/*
- * These symbols indicate the properties of your machine or compiler.
- * The conditional definitions given may do the right thing already,
- * but you'd best look them over closely, especially if your compiler
- * does not handle full ANSI C.  An ANSI-compliant C compiler should
- * provide all the necessary features; __STDC__ is supposed to be
- * predefined by such compilers.
- */
-
-/*
- * HAVE_STDC is tested below to see whether ANSI features are available.
- * We avoid testing __STDC__ directly for arcane reasons of portability.
- * (On some compilers, __STDC__ is only defined if a switch is given,
- * but the switch also disables machine-specific features we need to get at.
- * In that case, -DHAVE_STDC in the Makefile is a convenient solution.)
- */
-
-#ifdef __STDC__			/* if compiler claims to be ANSI, believe it */
-#define HAVE_STDC
-#endif
-
-
-/* Does your compiler support function prototypes? */
-/* (If not, you also need to use ansi2knr, see SETUP) */
-
-#ifdef HAVE_STDC		/* ANSI C compilers always have prototypes */
-#define PROTO
-#else
-#ifdef __cplusplus		/* So do C++ compilers */
-#define PROTO
-#endif
-#endif
-
-/* Does your compiler support the declaration "unsigned char" ? */
-/* How about "unsigned short" ? */
-
-#ifdef HAVE_STDC		/* ANSI C compilers must support both */
-#define HAVE_UNSIGNED_CHAR
-#define HAVE_UNSIGNED_SHORT
-#endif
-
-/* Define this if an ordinary "char" type is unsigned.
- * If you're not sure, leaving it undefined will work at some cost in speed.
- * If you defined HAVE_UNSIGNED_CHAR then it doesn't matter very much.
- */
-
-/* #define CHAR_IS_UNSIGNED */
-
-/* Define this if your compiler implements ">>" on signed values as a logical
- * (unsigned) shift; leave it undefined if ">>" is a signed (arithmetic) shift,
- * which is the normal and rational definition.
- */
-
-/* #define RIGHT_SHIFT_IS_UNSIGNED */
-
-/* Define "void" as "char" if your compiler doesn't know about type void.
- * NOTE: be sure to define void such that "void *" represents the most general
- * pointer type, e.g., that returned by malloc().
- */
-
-/* #define void char */
-
-/* Define const as empty if your compiler doesn't know the "const" keyword. */
-/* (Even if it does, defining const as empty won't break anything.) */
-
-#ifndef HAVE_STDC		/* ANSI C and C++ compilers should know it. */
-#ifndef __cplusplus
-#define const
-#endif
-#endif
-
-/* For 80x86 machines, you need to define NEED_FAR_POINTERS,
- * unless you are using a large-data memory model or 80386 flat-memory mode.
- * On less brain-damaged CPUs this symbol must not be defined.
- * (Defining this symbol causes large data structures to be referenced through
- * "far" pointers and to be allocated with a special version of malloc.)
- */
-
-#ifdef MSDOS
-#define NEED_FAR_POINTERS
-#endif
-
-
-/* The next three symbols only affect the system-dependent user interface
- * modules (jcmain.c, jdmain.c).  You can ignore these if you are supplying
- * your own user interface code.
- */
-
-/* Define this if you want to name both input and output files on the command
- * line, rather than using stdout and optionally stdin.  You MUST do this if
- * your system can't cope with binary I/O to stdin/stdout.  See comments at
- * head of jcmain.c or jdmain.c.
- */
-
-#ifdef MSDOS			/* two-file style is needed for PCs */
-#ifndef USE_FDOPEN		/* unless you have fdopen() or setmode() */
-#ifndef USE_SETMODE
-#define TWO_FILE_COMMANDLINE
-#endif
-#endif
-#endif
-#ifdef THINK_C			/* it's needed for Macintosh too */
-#define TWO_FILE_COMMANDLINE
-#endif
-
-/* Define this if your system needs explicit cleanup of temporary files.
- * This is crucial under MS-DOS, where the temporary "files" may be areas
- * of extended memory; on most other systems it's not as important.
- */
-
-#ifdef MSDOS
-#define NEED_SIGNAL_CATCHER
-#endif
-
-/* By default, we open image files with fopen(...,"rb") or fopen(...,"wb").
- * This is necessary on systems that distinguish text files from binary files,
- * and is harmless on most systems that don't.  If you have one of the rare
- * systems that complains about the "b" spec, define this symbol.
- */
-
-/* #define DONT_USE_B_MODE */
-
-
-/* If you're getting bored, that's the end of the symbols you HAVE to
- * worry about.  Go fix the makefile and compile.
- */
-
-
-/* If your compiler supports inline functions, define INLINE
- * as the inline keyword; otherwise define it as empty.
- */
-
-#ifdef __GNUC__			/* for instance, GNU C knows about inline */
-#define INLINE __inline__
-#endif
-#ifndef INLINE			/* default is to define it as empty */
-#define INLINE
-#endif
-
-/* On a few systems, type boolean and/or macros FALSE, TRUE may appear
- * in standard header files.  Or you may have conflicts with application-
- * specific header files that you want to include together with these files.
- * In that case you need only comment out these definitions.
- */
-
-typedef int boolean;
-#undef FALSE			/* in case these macros already exist */
-#undef TRUE
-#define FALSE	0		/* values of boolean */
-#define TRUE	1
-
-/* This defines the size of the I/O buffers for entropy compression
- * and decompression; you could reduce it if memory is tight.
- */
-
-#define JPEG_BUF_SIZE	4096 /* bytes */
-
-
-
-/* These symbols determine the JPEG functionality supported. */
-
-/*
- * These defines indicate whether to include various optional functions.
- * Undefining some of these symbols will produce a smaller but less capable
- * program file.  Note that you can leave certain source files out of the
- * compilation/linking process if you've #undef'd the corresponding symbols.
- * (You may HAVE to do that if your compiler doesn't like null source files.)
- */
-
-/* Arithmetic coding is unsupported for legal reasons.  Complaints to IBM. */
-
-/* Encoder capability options: */
-#undef  C_ARITH_CODING_SUPPORTED    /* Arithmetic coding back end? */
-#undef  C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files?  (NYI) */
-#define ENTROPY_OPT_SUPPORTED	    /* Optimization of entropy coding parms? */
-#define INPUT_SMOOTHING_SUPPORTED   /* Input image smoothing option? */
-/* Decoder capability options: */
-#undef  D_ARITH_CODING_SUPPORTED    /* Arithmetic coding back end? */
-#define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
-#define BLOCK_SMOOTHING_SUPPORTED   /* Block smoothing during decoding? */
-#define QUANT_1PASS_SUPPORTED	/* 1-pass color quantization? */
-#define QUANT_2PASS_SUPPORTED	/* 2-pass color quantization? */
-/* these defines indicate which JPEG file formats are allowed */
-#define JFIF_SUPPORTED		/* JFIF or "raw JPEG" files */
-#undef  JTIFF_SUPPORTED		/* JPEG-in-TIFF (not yet implemented) */
-/* these defines indicate which image (non-JPEG) file formats are allowed */
-#define GIF_SUPPORTED		/* GIF image file format */
-/* #define RLE_SUPPORTED */	/* RLE image file format (by default, no) */
-#define PPM_SUPPORTED		/* PPM/PGM image file format */
-#define TARGA_SUPPORTED		/* Targa image file format */
-#undef  TIFF_SUPPORTED		/* TIFF image file format (not yet impl.) */
-
-/* more capability options later, no doubt */
-
-
-/*
- * Define exactly one of these three symbols to indicate whether you want
- * 8-bit, 12-bit, or 16-bit sample (pixel component) values.  8-bit is the
- * default and is nearly always the right thing to use.  You can use 12-bit if
- * you need to support image formats with more than 8 bits of resolution in a
- * color value.  16-bit should only be used for the lossless JPEG mode (not
- * currently supported).  Note that 12- and 16-bit values take up twice as
- * much memory as 8-bit!
- * Note: if you select 12- or 16-bit precision, it is dangerous to turn off
- * ENTROPY_OPT_SUPPORTED.  The standard Huffman tables are only good for 8-bit
- * precision, so jchuff.c normally uses entropy optimization to compute
- * usable tables for higher precision.  If you don't want to do optimization,
- * you'll have to supply different default Huffman tables.
- */
-
-#define EIGHT_BIT_SAMPLES
-#undef  TWELVE_BIT_SAMPLES
-#undef  SIXTEEN_BIT_SAMPLES
-
-
-
-/*
- * The remaining definitions don't need to be hand-edited in most cases.
- * You may need to change these if you have a machine with unusual data
- * types; for example, "char" not 8 bits, "short" not 16 bits,
- * or "long" not 32 bits.  We don't care whether "int" is 16 or 32 bits,
- * but it had better be at least 16.
- */
-
-/* First define the representation of a single pixel element value. */
-
-#ifdef EIGHT_BIT_SAMPLES
-/* JSAMPLE should be the smallest type that will hold the values 0..255.
- * You can use a signed char by having GETJSAMPLE mask it with 0xFF.
- * If you have only signed chars, and you are more worried about speed than
- * memory usage, it might be a win to make JSAMPLE be short.
- */
-
-#ifdef HAVE_UNSIGNED_CHAR
-
-typedef unsigned char JSAMPLE;
-#define GETJSAMPLE(value)  (value)
-
-#else /* not HAVE_UNSIGNED_CHAR */
-#ifdef CHAR_IS_UNSIGNED
-
-typedef char JSAMPLE;
-#define GETJSAMPLE(value)  (value)
-
-#else /* not CHAR_IS_UNSIGNED */
-
-typedef char JSAMPLE;
-#define GETJSAMPLE(value)  ((value) & 0xFF)
-
-#endif /* CHAR_IS_UNSIGNED */
-#endif /* HAVE_UNSIGNED_CHAR */
-
-#define BITS_IN_JSAMPLE   8
-#define MAXJSAMPLE	255
-#define CENTERJSAMPLE	128
-
-#endif /* EIGHT_BIT_SAMPLES */
-
-
-#ifdef TWELVE_BIT_SAMPLES
-/* JSAMPLE should be the smallest type that will hold the values 0..4095. */
-/* On nearly all machines "short" will do nicely. */
-
-typedef short JSAMPLE;
-#define GETJSAMPLE(value)  (value)
-
-#define BITS_IN_JSAMPLE   12
-#define MAXJSAMPLE	4095
-#define CENTERJSAMPLE	2048
-
-#endif /* TWELVE_BIT_SAMPLES */
-
-
-#ifdef SIXTEEN_BIT_SAMPLES
-/* JSAMPLE should be the smallest type that will hold the values 0..65535. */
-
-#ifdef HAVE_UNSIGNED_SHORT
-
-typedef unsigned short JSAMPLE;
-#define GETJSAMPLE(value)  (value)
-
-#else /* not HAVE_UNSIGNED_SHORT */
-
-/* If int is 32 bits this'll be horrendously inefficient storage-wise.
- * But since we don't actually support 16-bit samples (ie lossless coding) yet,
- * I'm not going to worry about making a smarter definition ...
- */
-typedef unsigned int JSAMPLE;
-#define GETJSAMPLE(value)  (value)
-
-#endif /* HAVE_UNSIGNED_SHORT */
-
-#define BITS_IN_JSAMPLE    16
-#define MAXJSAMPLE	65535
-#define CENTERJSAMPLE	32768
-
-#endif /* SIXTEEN_BIT_SAMPLES */
-
-
-/* Here we define the representation of a DCT frequency coefficient.
- * This should be a signed 16-bit value; "short" is usually right.
- * It's important that this be exactly 16 bits, no more and no less;
- * more will cost you a BIG hit of memory, less will give wrong answers.
- */
-
-typedef short JCOEF;
-
-
-/* The remaining typedefs are used for various table entries and so forth.
- * They must be at least as wide as specified; but making them too big
- * won't cost a huge amount of memory, so we don't provide special
- * extraction code like we did for JSAMPLE.  (In other words, these
- * typedefs live at a different point on the speed/space tradeoff curve.)
- */
-
-/* UINT8 must hold at least the values 0..255. */
-
-#ifdef HAVE_UNSIGNED_CHAR
-typedef unsigned char UINT8;
-#else /* not HAVE_UNSIGNED_CHAR */
-#ifdef CHAR_IS_UNSIGNED
-typedef char UINT8;
-#else /* not CHAR_IS_UNSIGNED */
-typedef short UINT8;
-#endif /* CHAR_IS_UNSIGNED */
-#endif /* HAVE_UNSIGNED_CHAR */
-
-/* UINT16 must hold at least the values 0..65535. */
-
-#ifdef HAVE_UNSIGNED_SHORT
-typedef unsigned short UINT16;
-#else /* not HAVE_UNSIGNED_SHORT */
-typedef unsigned int UINT16;
-#endif /* HAVE_UNSIGNED_SHORT */
-
-/* INT16 must hold at least the values -32768..32767. */
-
-#ifndef XMD_H			/* X11/xmd.h correctly defines INT16 */
-typedef short INT16;
-#endif
-
-/* INT32 must hold signed 32-bit values; if your machine happens */
-/* to have 64-bit longs, you might want to change this. */
-
-#ifndef XMD_H			/* X11/xmd.h correctly defines INT32 */
-typedef long INT32;
-#endif
diff --git a/jconfig.manx b/jconfig.manx
new file mode 100644
index 0000000..6dd0d00
--- /dev/null
+++ b/jconfig.manx
@@ -0,0 +1,43 @@
+/* jconfig.manx --- jconfig.h for Amiga systems using Manx Aztec C ver 5.x. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define TEMP_DIRECTORY "JPEGTMP:"	/* recommended setting for Amiga */
+
+#define SHORTxSHORT_32		/* produces better DCT code with Aztec C */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED		/* BMP image file format */
+#define GIF_SUPPORTED		/* GIF image file format */
+#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED		/* Utah RLE image file format */
+#define TARGA_SUPPORTED		/* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE
+#define NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT		/* optional */
+
+#define signal_catcher _abort	/* hack for Aztec C naming requirements */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/jconfig.mc6 b/jconfig.mc6
new file mode 100644
index 0000000..c55082d
--- /dev/null
+++ b/jconfig.mc6
@@ -0,0 +1,52 @@
+/* jconfig.mc6 --- jconfig.h for Microsoft C on MS-DOS, version 6.00A & up. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#define NEED_FAR_POINTERS	/* for small or medium memory model */
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define USE_MSDOS_MEMMGR	/* Define this if you use jmemdos.c */
+
+#define MAX_ALLOC_CHUNK 65520L	/* Maximum request to malloc() */
+
+#define USE_FMEM		/* Microsoft has _fmemcpy() and _fmemset() */
+
+#define NEED_FHEAPMIN		/* far heap management routines are broken */
+
+#define SHORTxLCONST_32		/* enable compiler-specific DCT optimization */
+/* Note: the above define is known to improve the code with Microsoft C 6.00A.
+ * I do not know whether it is good for later compiler versions.
+ * Please report any info on this point to jpeg-info@uunet.uu.net.
+ */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED		/* BMP image file format */
+#define GIF_SUPPORTED		/* GIF image file format */
+#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED		/* Utah RLE image file format */
+#define TARGA_SUPPORTED		/* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE
+#define USE_SETMODE		/* Microsoft has setmode() */
+#define NEED_SIGNAL_CATCHER	/* Define this if you use jmemdos.c */
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT		/* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/jconfig.sas b/jconfig.sas
new file mode 100644
index 0000000..efdac22
--- /dev/null
+++ b/jconfig.sas
@@ -0,0 +1,43 @@
+/* jconfig.sas --- jconfig.h for Amiga systems using SAS C 6.0 and up. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define TEMP_DIRECTORY "JPEGTMP:"	/* recommended setting for Amiga */
+
+#define NO_MKTEMP		/* SAS C doesn't have mktemp() */
+
+#define SHORTxSHORT_32		/* produces better DCT code with SAS C */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED		/* BMP image file format */
+#define GIF_SUPPORTED		/* GIF image file format */
+#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED		/* Utah RLE image file format */
+#define TARGA_SUPPORTED		/* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE
+#define NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT		/* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/jconfig.st b/jconfig.st
new file mode 100644
index 0000000..ab3b5b4
--- /dev/null
+++ b/jconfig.st
@@ -0,0 +1,39 @@
+/* jconfig.st --- jconfig.h for Atari ST/STE/TT using Pure C or Turbo C. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+#define INCOMPLETE_TYPES_BROKEN	/* suppress undefined-structure warnings */
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#define ALIGN_TYPE  long	/* apparently double is a weird size? */
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED		/* BMP image file format */
+#define GIF_SUPPORTED		/* GIF image file format */
+#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED		/* Utah RLE image file format */
+#define TARGA_SUPPORTED		/* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE	/* optional -- undef if you like Unix style */
+#define NEED_SIGNAL_CATCHER	/* needed if you use jmemname.c */
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT		/* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/jconfig.vms b/jconfig.vms
new file mode 100644
index 0000000..55a6ffb
--- /dev/null
+++ b/jconfig.vms
@@ -0,0 +1,37 @@
+/* jconfig.vms --- jconfig.h for use on Digital VMS. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED		/* BMP image file format */
+#define GIF_SUPPORTED		/* GIF image file format */
+#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED		/* Utah RLE image file format */
+#define TARGA_SUPPORTED		/* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE	/* Needed on VMS */
+#undef NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT		/* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/jcparam.c b/jcparam.c
new file mode 100644
index 0000000..234aa56
--- /dev/null
+++ b/jcparam.c
@@ -0,0 +1,443 @@
+/*
+ * jcparam.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains optional default-setting code for the JPEG compressor.
+ * Applications do not have to use this file, but those that don't use it
+ * must know a lot more about the innards of the JPEG code.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Quantization table setup routines
+ */
+
+GLOBAL void
+jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
+		      const unsigned int *basic_table,
+		      int scale_factor, boolean force_baseline)
+/* Define a quantization table equal to the basic_table times
+ * a scale factor (given as a percentage).
+ * If force_baseline is TRUE, the computed quantization table entries
+ * are limited to 1..255 for JPEG baseline compatibility.
+ */
+{
+  JQUANT_TBL ** qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
+  int i;
+  long temp;
+
+  /* Safety check to ensure start_compress not called yet. */
+  if (cinfo->global_state != CSTATE_START)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  if (*qtblptr == NULL)
+    *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
+
+  for (i = 0; i < DCTSIZE2; i++) {
+    temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
+    /* limit the values to the valid range */
+    if (temp <= 0L) temp = 1L;
+    if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
+    if (force_baseline && temp > 255L)
+      temp = 255L;		/* limit to baseline range if requested */
+    (*qtblptr)->quantval[i] = (UINT16) temp;
+  }
+
+  /* Initialize sent_table FALSE so table will be written to JPEG file. */
+  (*qtblptr)->sent_table = FALSE;
+}
+
+
+GLOBAL void
+jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
+			 boolean force_baseline)
+/* Set or change the 'quality' (quantization) setting, using default tables
+ * and a straight percentage-scaling quality scale.  In most cases it's better
+ * to use jpeg_set_quality (below); this entry point is provided for
+ * applications that insist on a linear percentage scaling.
+ */
+{
+  /* This is the sample quantization table given in the JPEG spec section K.1,
+   * but expressed in zigzag order (as are all of our quant. tables).
+   * The spec says that the values given produce "good" quality, and
+   * when divided by 2, "very good" quality.
+   */
+  static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
+    16,  11,  12,  14,  12,  10,  16,  14,
+    13,  14,  18,  17,  16,  19,  24,  40,
+    26,  24,  22,  22,  24,  49,  35,  37,
+    29,  40,  58,  51,  61,  60,  57,  51,
+    56,  55,  64,  72,  92,  78,  64,  68,
+    87,  69,  55,  56,  80, 109,  81,  87,
+    95,  98, 103, 104, 103,  62,  77, 113,
+    121, 112, 100, 120,  92, 101, 103,  99
+    };
+  static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
+    17,  18,  18,  24,  21,  24,  47,  26,
+    26,  47,  99,  66,  56,  66,  99,  99,
+    99,  99,  99,  99,  99,  99,  99,  99,
+    99,  99,  99,  99,  99,  99,  99,  99,
+    99,  99,  99,  99,  99,  99,  99,  99,
+    99,  99,  99,  99,  99,  99,  99,  99,
+    99,  99,  99,  99,  99,  99,  99,  99,
+    99,  99,  99,  99,  99,  99,  99,  99
+    };
+
+  /* Set up two quantization tables using the specified scaling */
+  jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
+		       scale_factor, force_baseline);
+  jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
+		       scale_factor, force_baseline);
+}
+
+
+GLOBAL int
+jpeg_quality_scaling (int quality)
+/* Convert a user-specified quality rating to a percentage scaling factor
+ * for an underlying quantization table, using our recommended scaling curve.
+ * The input 'quality' factor should be 0 (terrible) to 100 (very good).
+ */
+{
+  /* Safety limit on quality factor.  Convert 0 to 1 to avoid zero divide. */
+  if (quality <= 0) quality = 1;
+  if (quality > 100) quality = 100;
+
+  /* The basic table is used as-is (scaling 100) for a quality of 50.
+   * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
+   * note that at Q=100 the scaling is 0, which will cause j_add_quant_table
+   * to make all the table entries 1 (hence, no quantization loss).
+   * Qualities 1..50 are converted to scaling percentage 5000/Q.
+   */
+  if (quality < 50)
+    quality = 5000 / quality;
+  else
+    quality = 200 - quality*2;
+
+  return quality;
+}
+
+
+GLOBAL void
+jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
+/* Set or change the 'quality' (quantization) setting, using default tables.
+ * This is the standard quality-adjusting entry point for typical user
+ * interfaces; only those who want detailed control over quantization tables
+ * would use the preceding three routines directly.
+ */
+{
+  /* Convert user 0-100 rating to percentage scaling */
+  quality = jpeg_quality_scaling(quality);
+
+  /* Set up standard quality tables */
+  jpeg_set_linear_quality(cinfo, quality, force_baseline);
+}
+
+
+/*
+ * Huffman table setup routines
+ */
+
+LOCAL void
+add_huff_table (j_compress_ptr cinfo,
+		JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
+/* Define a Huffman table */
+{
+  if (*htblptr == NULL)
+    *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+  
+  MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
+  MEMCOPY((*htblptr)->huffval, val, SIZEOF((*htblptr)->huffval));
+
+  /* Initialize sent_table FALSE so table will be written to JPEG file. */
+  (*htblptr)->sent_table = FALSE;
+}
+
+
+LOCAL void
+std_huff_tables (j_compress_ptr cinfo)
+/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
+/* IMPORTANT: these are only valid for 8-bit data precision! */
+{
+  static const UINT8 bits_dc_luminance[17] =
+    { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
+  static const UINT8 val_dc_luminance[] =
+    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
+  
+  static const UINT8 bits_dc_chrominance[17] =
+    { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
+  static const UINT8 val_dc_chrominance[] =
+    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
+  
+  static const UINT8 bits_ac_luminance[17] =
+    { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
+  static const UINT8 val_ac_luminance[] =
+    { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
+      0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
+      0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
+      0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
+      0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
+      0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
+      0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+      0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
+      0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
+      0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
+      0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
+      0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
+      0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
+      0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
+      0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
+      0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
+      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
+      0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
+      0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
+      0xf9, 0xfa };
+  
+  static const UINT8 bits_ac_chrominance[17] =
+    { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
+  static const UINT8 val_ac_chrominance[] =
+    { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
+      0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
+      0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
+      0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
+      0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
+      0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
+      0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
+      0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
+      0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
+      0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
+      0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
+      0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+      0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
+      0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
+      0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
+      0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
+      0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
+      0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
+      0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
+      0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
+      0xf9, 0xfa };
+  
+  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
+		 bits_dc_luminance, val_dc_luminance);
+  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
+		 bits_ac_luminance, val_ac_luminance);
+  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
+		 bits_dc_chrominance, val_dc_chrominance);
+  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
+		 bits_ac_chrominance, val_ac_chrominance);
+}
+
+
+/*
+ * Default parameter setup for compression.
+ *
+ * Applications that don't choose to use this routine must do their
+ * own setup of all these parameters.  Alternately, you can call this
+ * to establish defaults and then alter parameters selectively.  This
+ * is the recommended approach since, if we add any new parameters,
+ * your code will still work (they'll be set to reasonable defaults).
+ */
+
+GLOBAL void
+jpeg_set_defaults (j_compress_ptr cinfo)
+{
+  int i;
+
+  /* Safety check to ensure start_compress not called yet. */
+  if (cinfo->global_state != CSTATE_START)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  /* Allocate comp_info array large enough for maximum component count.
+   * Array is made permanent in case application wants to compress
+   * multiple images at same param settings.
+   */
+  if (cinfo->comp_info == NULL)
+    cinfo->comp_info = (jpeg_component_info *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+				  MAX_COMPONENTS * SIZEOF(jpeg_component_info));
+
+  /* Initialize everything not dependent on the color space */
+
+  cinfo->data_precision = BITS_IN_JSAMPLE;
+  /* Set up two quantization tables using default quality of 75 */
+  jpeg_set_quality(cinfo, 75, TRUE);
+  /* Set up two Huffman tables */
+  std_huff_tables(cinfo);
+
+  /* Initialize default arithmetic coding conditioning */
+  for (i = 0; i < NUM_ARITH_TBLS; i++) {
+    cinfo->arith_dc_L[i] = 0;
+    cinfo->arith_dc_U[i] = 1;
+    cinfo->arith_ac_K[i] = 5;
+  }
+
+  /* Expect normal source image, not raw downsampled data */
+  cinfo->raw_data_in = FALSE;
+
+  /* Use Huffman coding, not arithmetic coding, by default */
+  cinfo->arith_code = FALSE;
+
+  /* Color images are interleaved by default */
+  cinfo->interleave = TRUE;
+
+  /* By default, don't do extra passes to optimize entropy coding */
+  cinfo->optimize_coding = FALSE;
+  /* The standard Huffman tables are only valid for 8-bit data precision.
+   * If the precision is higher, force optimization on so that usable
+   * tables will be computed.  This test can be removed if default tables
+   * are supplied that are valid for the desired precision.
+   */
+  if (cinfo->data_precision > 8)
+    cinfo->optimize_coding = TRUE;
+
+  /* By default, use the simpler non-cosited sampling alignment */
+  cinfo->CCIR601_sampling = FALSE;
+
+  /* No input smoothing */
+  cinfo->smoothing_factor = 0;
+
+  /* DCT algorithm preference */
+  cinfo->dct_method = JDCT_DEFAULT;
+
+  /* No restart markers */
+  cinfo->restart_interval = 0;
+  cinfo->restart_in_rows = 0;
+
+  /* Fill in default JFIF marker parameters.  Note that whether the marker
+   * will actually be written is determined by jpeg_set_colorspace.
+   */
+  cinfo->density_unit = 0;	/* Pixel size is unknown by default */
+  cinfo->X_density = 1;		/* Pixel aspect ratio is square by default */
+  cinfo->Y_density = 1;
+
+  /* Choose JPEG colorspace based on input space, set defaults accordingly */
+
+  jpeg_default_colorspace(cinfo);
+}
+
+
+/*
+ * Select an appropriate JPEG colorspace for in_color_space.
+ */
+
+GLOBAL void
+jpeg_default_colorspace (j_compress_ptr cinfo)
+{
+  switch (cinfo->in_color_space) {
+  case JCS_GRAYSCALE:
+    jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
+    break;
+  case JCS_RGB:
+    jpeg_set_colorspace(cinfo, JCS_YCbCr);
+    break;
+  case JCS_YCbCr:
+    jpeg_set_colorspace(cinfo, JCS_YCbCr);
+    break;
+  case JCS_CMYK:
+    jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
+    break;
+  case JCS_YCCK:
+    jpeg_set_colorspace(cinfo, JCS_YCCK);
+    break;
+  case JCS_UNKNOWN:
+    jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
+    break;
+  default:
+    ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+  }
+}
+
+
+/*
+ * Set the JPEG colorspace, and choose colorspace-dependent default values.
+ */
+
+GLOBAL void
+jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
+{
+  jpeg_component_info * compptr;
+  int ci;
+
+#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
+  (compptr = &cinfo->comp_info[index], \
+   compptr->component_index = (index), \
+   compptr->component_id = (id), \
+   compptr->h_samp_factor = (hsamp), \
+   compptr->v_samp_factor = (vsamp), \
+   compptr->quant_tbl_no = (quant), \
+   compptr->dc_tbl_no = (dctbl), \
+   compptr->ac_tbl_no = (actbl) )
+
+  /* Safety check to ensure start_compress not called yet. */
+  if (cinfo->global_state != CSTATE_START)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
+   * tables 1 for chrominance components.
+   */
+
+  cinfo->jpeg_color_space = colorspace;
+
+  cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
+  cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
+
+  switch (colorspace) {
+  case JCS_GRAYSCALE:
+    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
+    cinfo->num_components = 1;
+    /* JFIF specifies component ID 1 */
+    SET_COMP(0, 1, 1,1, 0, 0,0);
+    break;
+  case JCS_RGB:
+    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
+    cinfo->num_components = 3;
+    SET_COMP(0, 'R', 1,1, 0, 0,0);
+    SET_COMP(1, 'G', 1,1, 0, 0,0);
+    SET_COMP(2, 'B', 1,1, 0, 0,0);
+    break;
+  case JCS_YCbCr:
+    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
+    cinfo->num_components = 3;
+    /* JFIF specifies component IDs 1,2,3 */
+    /* We default to 2x2 subsamples of chrominance */
+    SET_COMP(0, 1, 2,2, 0, 0,0);
+    SET_COMP(1, 2, 1,1, 1, 1,1);
+    SET_COMP(2, 3, 1,1, 1, 1,1);
+    break;
+  case JCS_CMYK:
+    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
+    cinfo->num_components = 4;
+    SET_COMP(0, 'C', 1,1, 0, 0,0);
+    SET_COMP(1, 'M', 1,1, 0, 0,0);
+    SET_COMP(2, 'Y', 1,1, 0, 0,0);
+    SET_COMP(3, 'K', 1,1, 0, 0,0);
+    break;
+  case JCS_YCCK:
+    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
+    cinfo->num_components = 4;
+    SET_COMP(0, 1, 2,2, 0, 0,0);
+    SET_COMP(1, 2, 1,1, 1, 1,1);
+    SET_COMP(2, 3, 1,1, 1, 1,1);
+    SET_COMP(3, 4, 2,2, 0, 0,0);
+    break;
+  case JCS_UNKNOWN:
+    cinfo->num_components = cinfo->input_components;
+    if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
+      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+	       MAX_COMPONENTS);
+    for (ci = 0; ci < cinfo->num_components; ci++) {
+      SET_COMP(ci, ci, 1,1, 0, 0,0);
+    }
+    break;
+  default:
+    ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+  }
+}
diff --git a/jcpipe.c b/jcpipe.c
deleted file mode 100644
index 6b64c4f..0000000
--- a/jcpipe.c
+++ /dev/null
@@ -1,736 +0,0 @@
-/*
- * jcpipe.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains compression pipeline controllers.
- * These routines are invoked via the c_pipeline_controller method.
- *
- * There are four basic pipeline controllers, one for each combination of:
- *	single-scan JPEG file (single component or fully interleaved)
- *  vs. multiple-scan JPEG file (noninterleaved or partially interleaved).
- *
- *	optimization of entropy encoding parameters
- *  vs. usage of default encoding parameters.
- *
- * Note that these conditions determine the needs for "big" arrays:
- * multiple scans imply a big array for splitting the color components;
- * entropy encoding optimization needs a big array for the MCU data.
- *
- * All but the simplest controller (single-scan, no optimization) can be
- * compiled out through configuration options, if you need to make a minimal
- * implementation.
- */
-
-#include "jinclude.h"
-
-
-/*
- * About the data structures:
- *
- * The processing chunk size for downsampling is referred to in this file as
- * a "row group": a row group is defined as Vk (v_samp_factor) sample rows of
- * any component after downsampling, or Vmax (max_v_samp_factor) unsubsampled
- * rows.  In an interleaved scan each MCU row contains exactly DCTSIZE row
- * groups of each component in the scan.  In a noninterleaved scan an MCU row
- * is one row of blocks, which might not be an integral number of row groups;
- * for convenience we use a buffer of the same size as in interleaved scans,
- * and process Vk MCU rows in each burst of downsampling.
- * To provide context for the downsampling step, we have to retain the last
- * two row groups of the previous MCU row while reading in the next MCU row
- * (or set of Vk MCU rows).  To do this without copying data about, we create
- * a rather strange data structure.  Exactly DCTSIZE+2 row groups of samples
- * are allocated, but we create two different sets of pointers to this array.
- * The second set swaps the last two pairs of row groups.  By working
- * alternately with the two sets of pointers, we can access the data in the
- * desired order.
- */
-
-
-
-/*
- * Utility routines: common code for pipeline controllers
- */
-
-LOCAL void
-interleaved_scan_setup (compress_info_ptr cinfo)
-/* Compute all derived info for an interleaved (multi-component) scan */
-/* On entry, cinfo->comps_in_scan and cinfo->cur_comp_info[] are set up */
-{
-  short ci, mcublks;
-  jpeg_component_info *compptr;
-
-  if (cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
-    ERREXIT(cinfo->emethods, "Too many components for interleaved scan");
-
-  cinfo->MCUs_per_row = (cinfo->image_width
-			 + cinfo->max_h_samp_factor*DCTSIZE - 1)
-			/ (cinfo->max_h_samp_factor*DCTSIZE);
-
-  cinfo->MCU_rows_in_scan = (cinfo->image_height
-			     + cinfo->max_v_samp_factor*DCTSIZE - 1)
-			    / (cinfo->max_v_samp_factor*DCTSIZE);
-  
-  cinfo->blocks_in_MCU = 0;
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
-    /* for interleaved scan, sampling factors give # of blocks per component */
-    compptr->MCU_width = compptr->h_samp_factor;
-    compptr->MCU_height = compptr->v_samp_factor;
-    compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
-    /* compute physical dimensions of component */
-    compptr->downsampled_width = jround_up(compptr->true_comp_width,
-					   (long) (compptr->MCU_width*DCTSIZE));
-    compptr->downsampled_height = jround_up(compptr->true_comp_height,
-					    (long) (compptr->MCU_height*DCTSIZE));
-    /* Sanity check */
-    if (compptr->downsampled_width !=
-	(cinfo->MCUs_per_row * (compptr->MCU_width*DCTSIZE)))
-      ERREXIT(cinfo->emethods, "I'm confused about the image width");
-    /* Prepare array describing MCU composition */
-    mcublks = compptr->MCU_blocks;
-    if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
-      ERREXIT(cinfo->emethods, "Sampling factors too large for interleaved scan");
-    while (mcublks-- > 0) {
-      cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
-    }
-  }
-
-  /* Convert restart specified in rows to actual MCU count. */
-  /* Note that count must fit in 16 bits, so we provide limiting. */
-  if (cinfo->restart_in_rows > 0) {
-    long nominal = cinfo->restart_in_rows * cinfo->MCUs_per_row;
-    cinfo->restart_interval = (UINT16) MIN(nominal, 65535L);
-  }
-
-  (*cinfo->methods->c_per_scan_method_selection) (cinfo);
-}
-
-
-LOCAL void
-noninterleaved_scan_setup (compress_info_ptr cinfo)
-/* Compute all derived info for a noninterleaved (single-component) scan */
-/* On entry, cinfo->comps_in_scan = 1 and cinfo->cur_comp_info[0] is set up */
-{
-  jpeg_component_info *compptr = cinfo->cur_comp_info[0];
-
-  /* for noninterleaved scan, always one block per MCU */
-  compptr->MCU_width = 1;
-  compptr->MCU_height = 1;
-  compptr->MCU_blocks = 1;
-  /* compute physical dimensions of component */
-  compptr->downsampled_width = jround_up(compptr->true_comp_width,
-					 (long) DCTSIZE);
-  compptr->downsampled_height = jround_up(compptr->true_comp_height,
-					  (long) DCTSIZE);
-
-  cinfo->MCUs_per_row = compptr->downsampled_width / DCTSIZE;
-  cinfo->MCU_rows_in_scan = compptr->downsampled_height / DCTSIZE;
-
-  /* Prepare array describing MCU composition */
-  cinfo->blocks_in_MCU = 1;
-  cinfo->MCU_membership[0] = 0;
-
-  /* Convert restart specified in rows to actual MCU count. */
-  /* Note that count must fit in 16 bits, so we provide limiting. */
-  if (cinfo->restart_in_rows > 0) {
-    long nominal = cinfo->restart_in_rows * cinfo->MCUs_per_row;
-    cinfo->restart_interval = (UINT16) MIN(nominal, 65535L);
-  }
-
-  (*cinfo->methods->c_per_scan_method_selection) (cinfo);
-}
-
-
-
-LOCAL void
-alloc_sampling_buffer (compress_info_ptr cinfo, JSAMPIMAGE fullsize_data[2],
-		       long fullsize_width)
-/* Create a pre-downsampling data buffer having the desired structure */
-/* (see comments at head of file) */
-{
-  short ci, vs, i;
-
-  vs = cinfo->max_v_samp_factor; /* row group height */
-
-  /* Get top-level space for array pointers */
-  fullsize_data[0] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
-				(cinfo->num_components * SIZEOF(JSAMPARRAY));
-  fullsize_data[1] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
-				(cinfo->num_components * SIZEOF(JSAMPARRAY));
-
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    /* Allocate the real storage */
-    fullsize_data[0][ci] = (*cinfo->emethods->alloc_small_sarray)
-				(fullsize_width,
-				(long) (vs * (DCTSIZE+2)));
-    /* Create space for the scrambled-order pointers */
-    fullsize_data[1][ci] = (JSAMPARRAY) (*cinfo->emethods->alloc_small)
-				(vs * (DCTSIZE+2) * SIZEOF(JSAMPROW));
-    /* Duplicate the first DCTSIZE-2 row groups */
-    for (i = 0; i < vs * (DCTSIZE-2); i++) {
-      fullsize_data[1][ci][i] = fullsize_data[0][ci][i];
-    }
-    /* Copy the last four row groups in swapped order */
-    for (i = 0; i < vs * 2; i++) {
-      fullsize_data[1][ci][vs*DCTSIZE + i] = fullsize_data[0][ci][vs*(DCTSIZE-2) + i];
-      fullsize_data[1][ci][vs*(DCTSIZE-2) + i] = fullsize_data[0][ci][vs*DCTSIZE + i];
-    }
-  }
-}
-
-
-#if 0				/* this routine not currently needed */
-
-LOCAL void
-free_sampling_buffer (compress_info_ptr cinfo, JSAMPIMAGE fullsize_data[2])
-/* Release a sampling buffer created by alloc_sampling_buffer */
-{
-  short ci;
-
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    /* Free the real storage */
-    (*cinfo->emethods->free_small_sarray) (fullsize_data[0][ci]);
-    /* Free the scrambled-order pointers */
-    (*cinfo->emethods->free_small) ((void *) fullsize_data[1][ci]);
-  }
-
-  /* Free the top-level space */
-  (*cinfo->emethods->free_small) ((void *) fullsize_data[0]);
-  (*cinfo->emethods->free_small) ((void *) fullsize_data[1]);
-}
-
-#endif
-
-
-LOCAL void
-downsample (compress_info_ptr cinfo,
-	    JSAMPIMAGE fullsize_data, JSAMPIMAGE sampled_data,
-	    long fullsize_width,
-	    short above, short current, short below, short out)
-/* Do downsampling of a single row group (of each component). */
-/* above, current, below are indexes of row groups in fullsize_data;      */
-/* out is the index of the target row group in sampled_data.              */
-/* Special case: above, below can be -1 to indicate top, bottom of image. */
-{
-  jpeg_component_info *compptr;
-  JSAMPARRAY above_ptr, below_ptr;
-  JSAMPROW dummy[MAX_SAMP_FACTOR]; /* for downsample expansion at top/bottom */
-  short ci, vs, i;
-
-  vs = cinfo->max_v_samp_factor; /* row group height */
-
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    compptr = & cinfo->comp_info[ci];
-
-    if (above >= 0)
-      above_ptr = fullsize_data[ci] + above * vs;
-    else {
-      /* Top of image: make a dummy above-context with copies of 1st row */
-      /* We assume current=0 in this case */
-      for (i = 0; i < vs; i++)
-	dummy[i] = fullsize_data[ci][0];
-      above_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
-    }
-
-    if (below >= 0)
-      below_ptr = fullsize_data[ci] + below * vs;
-    else {
-      /* Bot of image: make a dummy below-context with copies of last row */
-      for (i = 0; i < vs; i++)
-	dummy[i] = fullsize_data[ci][(current+1)*vs-1];
-      below_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
-    }
-
-    (*cinfo->methods->downsample[ci])
-		(cinfo, (int) ci,
-		 fullsize_width, (int) vs,
-		 compptr->downsampled_width, (int) compptr->v_samp_factor,
-		 above_ptr,
-		 fullsize_data[ci] + current * vs,
-		 below_ptr,
-		 sampled_data[ci] + out * compptr->v_samp_factor);
-  }
-}
-
-
-/* These variables are initialized by the pipeline controller for use by
- * MCU_output_catcher.
- * To avoid a lot of row-pointer overhead, we cram as many MCUs into each
- * row of whole_scan_MCUs as we can get without exceeding 32Kbytes per row.
- * NOTE: the "arbitrary" constant here must not exceed MAX_ALLOC_CHUNK
- * defined in jmemsys.h, which is 64K-epsilon in most DOS implementations.
- */
-
-#define MAX_WHOLE_ROW_BLOCKS	((int) (32768L / SIZEOF(JBLOCK))) /* max blocks/row */
-
-static big_barray_ptr whole_scan_MCUs; /* Big array for saving the MCUs */
-static int MCUs_in_big_row;	/* # of MCUs in each row of whole_scan_MCUs */
-static long next_whole_row;	/* next row to access in whole_scan_MCUs */
-static int next_MCU_index;	/* next MCU in current row */
-
-
-METHODDEF void
-MCU_output_catcher (compress_info_ptr cinfo, JBLOCK *MCU_data)
-/* Output method for siphoning off extract_MCUs output into a big array */
-{
-  static JBLOCKARRAY rowptr;
-
-  if (next_MCU_index >= MCUs_in_big_row) {
-    rowptr = (*cinfo->emethods->access_big_barray) (whole_scan_MCUs,
-						    next_whole_row, TRUE);
-    next_whole_row++;
-    next_MCU_index = 0;
-  }
-
-  /*
-   * note that on 80x86, the cast applied to MCU_data implies
-   * near to far pointer conversion.
-   */
-  jcopy_block_row((JBLOCKROW) MCU_data,
-		  rowptr[0] + next_MCU_index * cinfo->blocks_in_MCU,
-		  (long) cinfo->blocks_in_MCU);
-  next_MCU_index++;
-}
-
-
-METHODDEF void
-dump_scan_MCUs (compress_info_ptr cinfo, MCU_output_method_ptr output_method)
-/* Dump the MCUs saved in whole_scan_MCUs to the output method. */
-/* The method may be either the entropy encoder or some routine supplied */
-/* by the entropy optimizer. */
-{
-  /* On an 80x86 machine, the entropy encoder expects the passed data block
-   * to be in NEAR memory (for performance reasons), so we have to copy it
-   * back from the big array to a local array.  On less brain-damaged CPUs
-   * we needn't do that.
-   */
-#ifdef NEED_FAR_POINTERS
-  JBLOCK MCU_data[MAX_BLOCKS_IN_MCU];
-#endif
-  long mcurow, mcuindex, next_row;
-  int next_index;
-  JBLOCKARRAY rowptr = NULL;	/* init only to suppress compiler complaint */
-
-  next_row = 0;
-  next_index = MCUs_in_big_row;
-
-  for (mcurow = 0; mcurow < cinfo->MCU_rows_in_scan; mcurow++) {
-    (*cinfo->methods->progress_monitor) (cinfo, mcurow,
-					 cinfo->MCU_rows_in_scan);
-    for (mcuindex = 0; mcuindex < cinfo->MCUs_per_row; mcuindex++) {
-      if (next_index >= MCUs_in_big_row) {
-	rowptr = (*cinfo->emethods->access_big_barray) (whole_scan_MCUs,
-							next_row, FALSE);
-	next_row++;
-	next_index = 0;
-      }
-#ifdef NEED_FAR_POINTERS
-      jcopy_block_row(rowptr[0] + next_index * cinfo->blocks_in_MCU,
-		      (JBLOCKROW) MCU_data, /* casts near to far pointer! */
-		      (long) cinfo->blocks_in_MCU);
-      (*output_method) (cinfo, MCU_data);
-#else
-      (*output_method) (cinfo, rowptr[0] + next_index * cinfo->blocks_in_MCU);
-#endif
-      next_index++;
-    }
-  }
-
-  cinfo->completed_passes++;
-}
-
-
-
-/*
- * Compression pipeline controller used for single-scan files
- * with no optimization of entropy parameters.
- */
-
-METHODDEF void
-single_ccontroller (compress_info_ptr cinfo)
-{
-  int rows_in_mem;		/* # of sample rows in full-size buffers */
-  long fullsize_width;		/* # of samples per row in full-size buffers */
-  long cur_pixel_row;		/* counts # of pixel rows processed */
-  long mcu_rows_output;		/* # of MCU rows actually emitted */
-  int mcu_rows_per_loop;	/* # of MCU rows processed per outer loop */
-  /* Work buffer for pre-downsampling data (see comments at head of file) */
-  JSAMPIMAGE fullsize_data[2];
-  /* Work buffer for downsampled data */
-  JSAMPIMAGE sampled_data;
-  int rows_this_time;
-  short ci, whichss, i;
-
-  /* Prepare for single scan containing all components */
-  if (cinfo->num_components > MAX_COMPS_IN_SCAN)
-    ERREXIT(cinfo->emethods, "Too many components for interleaved scan");
-  cinfo->comps_in_scan = cinfo->num_components;
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
-  }
-  if (cinfo->comps_in_scan == 1) {
-    noninterleaved_scan_setup(cinfo);
-    /* Vk block rows constitute the same number of MCU rows */
-    mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
-  } else {
-    interleaved_scan_setup(cinfo);
-    /* in an interleaved scan, one MCU row contains Vk block rows */
-    mcu_rows_per_loop = 1;
-  }
-  cinfo->total_passes++;
-
-  /* Compute dimensions of full-size pixel buffers */
-  /* Note these are the same whether interleaved or not. */
-  rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
-  fullsize_width = jround_up(cinfo->image_width,
-			     (long) (cinfo->max_h_samp_factor * DCTSIZE));
-
-  /* Allocate working memory: */
-  /* fullsize_data is sample data before downsampling */
-  alloc_sampling_buffer(cinfo, fullsize_data, fullsize_width);
-  /* sampled_data is sample data after downsampling */
-  sampled_data = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
-				(cinfo->num_components * SIZEOF(JSAMPARRAY));
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    sampled_data[ci] = (*cinfo->emethods->alloc_small_sarray)
-			(cinfo->comp_info[ci].downsampled_width,
-			 (long) (cinfo->comp_info[ci].v_samp_factor * DCTSIZE));
-  }
-
-  /* Tell the memory manager to instantiate big arrays.
-   * We don't need any big arrays in this controller,
-   * but some other module (like the input file reader) may need one.
-   */
-  (*cinfo->emethods->alloc_big_arrays)
-	((long) 0,				/* no more small sarrays */
-	 (long) 0,				/* no more small barrays */
-	 (long) 0);				/* no more "medium" objects */
-
-  /* Initialize output file & do per-scan object init */
-
-  (*cinfo->methods->write_scan_header) (cinfo);
-  cinfo->methods->entropy_output = cinfo->methods->write_jpeg_data;
-  (*cinfo->methods->entropy_encode_init) (cinfo);
-  (*cinfo->methods->downsample_init) (cinfo);
-  (*cinfo->methods->extract_init) (cinfo);
-
-  /* Loop over input image: rows_in_mem pixel rows are processed per loop */
-
-  mcu_rows_output = 0;
-  whichss = 1;			/* arrange to start with fullsize_data[0] */
-
-  for (cur_pixel_row = 0; cur_pixel_row < cinfo->image_height;
-       cur_pixel_row += rows_in_mem) {
-    (*cinfo->methods->progress_monitor) (cinfo, cur_pixel_row,
-					 cinfo->image_height);
-
-    whichss ^= 1;		/* switch to other fullsize_data buffer */
-    
-    /* Obtain rows_this_time pixel rows and expand to rows_in_mem rows. */
-    /* Then we have exactly DCTSIZE row groups for downsampling. */   
-    rows_this_time = (int) MIN((long) rows_in_mem,
-			       cinfo->image_height - cur_pixel_row);
- 
-    (*cinfo->methods->get_sample_rows) (cinfo, rows_this_time,
-					fullsize_data[whichss]);
-    (*cinfo->methods->edge_expand) (cinfo,
-				    cinfo->image_width, rows_this_time,
-				    fullsize_width, rows_in_mem,
-				    fullsize_data[whichss]);
-    
-    /* Downsample the data (all components) */
-    /* First time through is a special case */
-    
-    if (cur_pixel_row) {
-      /* Downsample last row group of previous set */
-      downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-		 (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
-		 (short) (DCTSIZE-1));
-      /* and dump the previous set's downsampled data */
-      (*cinfo->methods->extract_MCUs) (cinfo, sampled_data, 
-				       mcu_rows_per_loop,
-				       cinfo->methods->entropy_encode);
-      mcu_rows_output += mcu_rows_per_loop;
-      /* Downsample first row group of this set */
-      downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-		 (short) (DCTSIZE+1), (short) 0, (short) 1,
-		 (short) 0);
-    } else {
-      /* Downsample first row group with dummy above-context */
-      downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-		 (short) (-1), (short) 0, (short) 1,
-		 (short) 0);
-    }
-    /* Downsample second through next-to-last row groups of this set */
-    for (i = 1; i <= DCTSIZE-2; i++) {
-      downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-		 (short) (i-1), (short) i, (short) (i+1),
-		 (short) i);
-    }
-  } /* end of outer loop */
-  
-  /* Downsample the last row group with dummy below-context */
-  /* Note whichss points to last buffer side used */
-  downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-	     (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
-	     (short) (DCTSIZE-1));
-  /* Dump the remaining data (may be less than full height if uninterleaved) */
-  (*cinfo->methods->extract_MCUs) (cinfo, sampled_data, 
-		(int) (cinfo->MCU_rows_in_scan - mcu_rows_output),
-		cinfo->methods->entropy_encode);
-
-  /* Finish output file */
-  (*cinfo->methods->extract_term) (cinfo);
-  (*cinfo->methods->downsample_term) (cinfo);
-  (*cinfo->methods->entropy_encode_term) (cinfo);
-  (*cinfo->methods->write_scan_trailer) (cinfo);
-  cinfo->completed_passes++;
-
-  /* Release working memory */
-  /* (no work -- we let free_all release what's needful) */
-}
-
-
-/*
- * Compression pipeline controller used for single-scan files
- * with optimization of entropy parameters.
- */
-
-#ifdef ENTROPY_OPT_SUPPORTED
-
-METHODDEF void
-single_eopt_ccontroller (compress_info_ptr cinfo)
-{
-  int rows_in_mem;		/* # of sample rows in full-size buffers */
-  long fullsize_width;		/* # of samples per row in full-size buffers */
-  long cur_pixel_row;		/* counts # of pixel rows processed */
-  long mcu_rows_output;		/* # of MCU rows actually emitted */
-  int mcu_rows_per_loop;	/* # of MCU rows processed per outer loop */
-  /* Work buffer for pre-downsampling data (see comments at head of file) */
-  JSAMPIMAGE fullsize_data[2];
-  /* Work buffer for downsampled data */
-  JSAMPIMAGE sampled_data;
-  int rows_this_time;
-  int blocks_in_big_row;
-  short ci, whichss, i;
-
-  /* Prepare for single scan containing all components */
-  if (cinfo->num_components > MAX_COMPS_IN_SCAN)
-    ERREXIT(cinfo->emethods, "Too many components for interleaved scan");
-  cinfo->comps_in_scan = cinfo->num_components;
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
-  }
-  if (cinfo->comps_in_scan == 1) {
-    noninterleaved_scan_setup(cinfo);
-    /* Vk block rows constitute the same number of MCU rows */
-    mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
-  } else {
-    interleaved_scan_setup(cinfo);
-    /* in an interleaved scan, one MCU row contains Vk block rows */
-    mcu_rows_per_loop = 1;
-  }
-  cinfo->total_passes += 2;	/* entropy encoder must add # passes it uses */
-
-  /* Compute dimensions of full-size pixel buffers */
-  /* Note these are the same whether interleaved or not. */
-  rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
-  fullsize_width = jround_up(cinfo->image_width,
-			     (long) (cinfo->max_h_samp_factor * DCTSIZE));
-
-  /* Allocate working memory: */
-  /* fullsize_data is sample data before downsampling */
-  alloc_sampling_buffer(cinfo, fullsize_data, fullsize_width);
-  /* sampled_data is sample data after downsampling */
-  sampled_data = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
-				(cinfo->num_components * SIZEOF(JSAMPARRAY));
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    sampled_data[ci] = (*cinfo->emethods->alloc_small_sarray)
-			(cinfo->comp_info[ci].downsampled_width,
-			 (long) (cinfo->comp_info[ci].v_samp_factor * DCTSIZE));
-  }
-
-  /* Figure # of MCUs to be packed in a row of whole_scan_MCUs */
-  MCUs_in_big_row = MAX_WHOLE_ROW_BLOCKS / cinfo->blocks_in_MCU;
-  blocks_in_big_row = MCUs_in_big_row * cinfo->blocks_in_MCU;
-
-  /* Request a big array: whole_scan_MCUs saves the MCU data for the scan */
-  whole_scan_MCUs = (*cinfo->emethods->request_big_barray)
-		((long) blocks_in_big_row,
-		 (long) (cinfo->MCUs_per_row * cinfo->MCU_rows_in_scan
-			 + MCUs_in_big_row-1) / MCUs_in_big_row,
-		 1L);		/* unit height is 1 row */
-
-  next_whole_row = 0;		/* init output ptr for MCU_output_catcher */
-  next_MCU_index = MCUs_in_big_row; /* forces access on first call! */
-
-  /* Tell the memory manager to instantiate big arrays */
-  (*cinfo->emethods->alloc_big_arrays)
-	((long) 0,				/* no more small sarrays */
-	 (long) 0,				/* no more small barrays */
-	 (long) 0);				/* no more "medium" objects */
-
-  /* Do per-scan object init */
-
-  (*cinfo->methods->downsample_init) (cinfo);
-  (*cinfo->methods->extract_init) (cinfo);
-
-  /* Loop over input image: rows_in_mem pixel rows are processed per loop */
-  /* MCU data goes into whole_scan_MCUs, not to the entropy encoder */
-
-  mcu_rows_output = 0;
-  whichss = 1;			/* arrange to start with fullsize_data[0] */
-
-  for (cur_pixel_row = 0; cur_pixel_row < cinfo->image_height;
-       cur_pixel_row += rows_in_mem) {
-    (*cinfo->methods->progress_monitor) (cinfo, cur_pixel_row,
-					 cinfo->image_height);
-
-    whichss ^= 1;		/* switch to other fullsize_data buffer */
-    
-    /* Obtain rows_this_time pixel rows and expand to rows_in_mem rows. */
-    /* Then we have exactly DCTSIZE row groups for downsampling. */   
-    rows_this_time = (int) MIN((long) rows_in_mem,
-			       cinfo->image_height - cur_pixel_row);
- 
-    (*cinfo->methods->get_sample_rows) (cinfo, rows_this_time,
-					fullsize_data[whichss]);
-    (*cinfo->methods->edge_expand) (cinfo,
-				    cinfo->image_width, rows_this_time,
-				    fullsize_width, rows_in_mem,
-				    fullsize_data[whichss]);
-    
-    /* Downsample the data (all components) */
-    /* First time through is a special case */
-    
-    if (cur_pixel_row) {
-      /* Downsample last row group of previous set */
-      downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-		 (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
-		 (short) (DCTSIZE-1));
-      /* and dump the previous set's downsampled data */
-      (*cinfo->methods->extract_MCUs) (cinfo, sampled_data, 
-				       mcu_rows_per_loop,
-				       MCU_output_catcher);
-      mcu_rows_output += mcu_rows_per_loop;
-      /* Downsample first row group of this set */
-      downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-		 (short) (DCTSIZE+1), (short) 0, (short) 1,
-		 (short) 0);
-    } else {
-      /* Downsample first row group with dummy above-context */
-      downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-		 (short) (-1), (short) 0, (short) 1,
-		 (short) 0);
-    }
-    /* Downsample second through next-to-last row groups of this set */
-    for (i = 1; i <= DCTSIZE-2; i++) {
-      downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-		 (short) (i-1), (short) i, (short) (i+1),
-		 (short) i);
-    }
-  } /* end of outer loop */
-  
-  /* Downsample the last row group with dummy below-context */
-  /* Note whichss points to last buffer side used */
-  downsample(cinfo, fullsize_data[whichss], sampled_data, fullsize_width,
-	     (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
-	     (short) (DCTSIZE-1));
-  /* Dump the remaining data (may be less than full height if uninterleaved) */
-  (*cinfo->methods->extract_MCUs) (cinfo, sampled_data, 
-		(int) (cinfo->MCU_rows_in_scan - mcu_rows_output),
-		MCU_output_catcher);
-
-  /* Clean up after that stuff, then find the optimal entropy parameters */
-
-  (*cinfo->methods->extract_term) (cinfo);
-  (*cinfo->methods->downsample_term) (cinfo);
-
-  cinfo->completed_passes++;
-
-  (*cinfo->methods->entropy_optimize) (cinfo, dump_scan_MCUs);
-
-  /* Emit scan to output file */
-  /* Note: we can't do write_scan_header until entropy parameters are set! */
-
-  (*cinfo->methods->write_scan_header) (cinfo);
-  cinfo->methods->entropy_output = cinfo->methods->write_jpeg_data;
-  (*cinfo->methods->entropy_encode_init) (cinfo);
-  dump_scan_MCUs(cinfo, cinfo->methods->entropy_encode);
-  (*cinfo->methods->entropy_encode_term) (cinfo);
-  (*cinfo->methods->write_scan_trailer) (cinfo);
-
-  /* Release working memory */
-  /* (no work -- we let free_all release what's needful) */
-}
-
-#endif /* ENTROPY_OPT_SUPPORTED */
-
-
-/*
- * Compression pipeline controller used for multiple-scan files
- * with no optimization of entropy parameters.
- */
-
-#ifdef C_MULTISCAN_FILES_SUPPORTED
-
-METHODDEF void
-multi_ccontroller (compress_info_ptr cinfo)
-{
-  ERREXIT(cinfo->emethods, "Not implemented yet");
-}
-
-#endif /* C_MULTISCAN_FILES_SUPPORTED */
-
-
-/*
- * Compression pipeline controller used for multiple-scan files
- * with optimization of entropy parameters.
- */
-
-#ifdef C_MULTISCAN_FILES_SUPPORTED
-#ifdef ENTROPY_OPT_SUPPORTED
-
-METHODDEF void
-multi_eopt_ccontroller (compress_info_ptr cinfo)
-{
-  ERREXIT(cinfo->emethods, "Not implemented yet");
-}
-
-#endif /* ENTROPY_OPT_SUPPORTED */
-#endif /* C_MULTISCAN_FILES_SUPPORTED */
-
-
-/*
- * The method selection routine for compression pipeline controllers.
- */
-
-GLOBAL void
-jselcpipeline (compress_info_ptr cinfo)
-{
-  if (cinfo->interleave || cinfo->num_components == 1) {
-    /* single scan needed */
-#ifdef ENTROPY_OPT_SUPPORTED
-    if (cinfo->optimize_coding)
-      cinfo->methods->c_pipeline_controller = single_eopt_ccontroller;
-    else
-#endif
-      cinfo->methods->c_pipeline_controller = single_ccontroller;
-  } else {
-    /* multiple scans needed */
-#ifdef C_MULTISCAN_FILES_SUPPORTED
-#ifdef ENTROPY_OPT_SUPPORTED
-    if (cinfo->optimize_coding)
-      cinfo->methods->c_pipeline_controller = multi_eopt_ccontroller;
-    else
-#endif
-      cinfo->methods->c_pipeline_controller = multi_ccontroller;
-#else
-    ERREXIT(cinfo->emethods, "Multiple-scan support was not compiled");
-#endif
-  }
-}
diff --git a/jcprepct.c b/jcprepct.c
new file mode 100644
index 0000000..7e60946
--- /dev/null
+++ b/jcprepct.c
@@ -0,0 +1,371 @@
+/*
+ * jcprepct.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the compression preprocessing controller.
+ * This controller manages the color conversion, downsampling,
+ * and edge expansion steps.
+ *
+ * Most of the complexity here is associated with buffering input rows
+ * as required by the downsampler.  See the comments at the head of
+ * jcsample.c for the downsampler's needs.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* At present, jcsample.c can request context rows only for smoothing.
+ * In the future, we might also need context rows for CCIR601 sampling
+ * or other more-complex downsampling procedures.  The code to support
+ * context rows should be compiled only if needed.
+ */
+#ifdef INPUT_SMOOTHING_SUPPORTED
+#define CONTEXT_ROWS_SUPPORTED
+#endif
+
+
+/*
+ * For the simple (no-context-row) case, we just need to buffer one
+ * row group's worth of pixels for the downsampling step.  At the bottom of
+ * the image, we pad to a full row group by replicating the last pixel row.
+ * The downsampler's last output row is then replicated if needed to pad
+ * out to a full iMCU row.
+ *
+ * When providing context rows, we must buffer three row groups' worth of
+ * pixels.  Three row groups are physically allocated, but the row pointer
+ * arrays are made five row groups high, with the extra pointers above and
+ * below "wrapping around" to point to the last and first real row groups.
+ * This allows the downsampler to access the proper context rows.
+ * At the top and bottom of the image, we create dummy context rows by
+ * copying the first or last real pixel row.  This copying could be avoided
+ * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
+ * trouble on the compression side.
+ */
+
+
+/* Private buffer controller object */
+
+typedef struct {
+  struct jpeg_c_prep_controller pub; /* public fields */
+
+  /* Downsampling input buffer.  This buffer holds color-converted data
+   * until we have enough to do a downsample step.
+   */
+  JSAMPARRAY color_buf[MAX_COMPONENTS];
+
+  JDIMENSION rows_to_go;	/* counts rows remaining in source image */
+  int next_buf_row;		/* index of next row to store in color_buf */
+
+#ifdef CONTEXT_ROWS_SUPPORTED	/* only needed for context case */
+  int this_row_group;		/* starting row index of group to process */
+  int next_buf_stop;		/* downsample when we reach this index */
+#endif
+} my_prep_controller;
+
+typedef my_prep_controller * my_prep_ptr;
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF void
+start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+
+  if (pass_mode != JBUF_PASS_THRU)
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+  /* Initialize total-height counter for detecting bottom of image */
+  prep->rows_to_go = cinfo->image_height;
+  /* Mark the conversion buffer empty */
+  prep->next_buf_row = 0;
+#ifdef CONTEXT_ROWS_SUPPORTED
+  /* Preset additional state variables for context mode.
+   * These aren't used in non-context mode, so we needn't test which mode.
+   */
+  prep->this_row_group = 0;
+  /* Set next_buf_stop to stop after two row groups have been read in. */
+  prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
+#endif
+}
+
+
+/*
+ * Expand an image vertically from height input_rows to height output_rows,
+ * by duplicating the bottom row.
+ */
+
+LOCAL void
+expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
+		    int input_rows, int output_rows)
+{
+  register int row;
+
+  for (row = input_rows; row < output_rows; row++) {
+    jcopy_sample_rows(image_data, input_rows-1, image_data, row,
+		      1, num_cols);
+  }
+}
+
+
+/*
+ * Process some data in the simple no-context case.
+ *
+ * Preprocessor output data is counted in "row groups".  A row group
+ * is defined to be v_samp_factor sample rows of each component.
+ * Downsampling will produce this much data from each max_v_samp_factor
+ * input rows.
+ */
+
+METHODDEF void
+pre_process_data (j_compress_ptr cinfo,
+		  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+		  JDIMENSION in_rows_avail,
+		  JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
+		  JDIMENSION out_row_groups_avail)
+{
+  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+  int numrows, ci;
+  JDIMENSION inrows;
+  jpeg_component_info * compptr;
+
+  while (*in_row_ctr < in_rows_avail &&
+	 *out_row_group_ctr < out_row_groups_avail) {
+    /* Do color conversion to fill the conversion buffer. */
+    inrows = in_rows_avail - *in_row_ctr;
+    numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
+    numrows = (int) MIN((JDIMENSION) numrows, inrows);
+    (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
+				       prep->color_buf,
+				       (JDIMENSION) prep->next_buf_row,
+				       numrows);
+    *in_row_ctr += numrows;
+    prep->next_buf_row += numrows;
+    prep->rows_to_go -= numrows;
+    /* If at bottom of image, pad to fill the conversion buffer. */
+    if (prep->rows_to_go == 0 &&
+	prep->next_buf_row < cinfo->max_v_samp_factor) {
+      for (ci = 0; ci < cinfo->num_components; ci++) {
+	expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
+			   prep->next_buf_row, cinfo->max_v_samp_factor);
+      }
+      prep->next_buf_row = cinfo->max_v_samp_factor;
+    }
+    /* If we've filled the conversion buffer, empty it. */
+    if (prep->next_buf_row == cinfo->max_v_samp_factor) {
+      (*cinfo->downsample->downsample) (cinfo,
+					prep->color_buf, (JDIMENSION) 0,
+					output_buf, *out_row_group_ctr);
+      prep->next_buf_row = 0;
+      (*out_row_group_ctr)++;
+    }
+    /* If at bottom of image, pad the output to a full iMCU height.
+     * Note we assume the caller is providing a one-iMCU-height output buffer!
+     */
+    if (prep->rows_to_go == 0 &&
+	*out_row_group_ctr < out_row_groups_avail) {
+      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	   ci++, compptr++) {
+	expand_bottom_edge(output_buf[ci],
+			   compptr->width_in_blocks * DCTSIZE,
+			   (int) (*out_row_group_ctr * compptr->v_samp_factor),
+			   (int) (out_row_groups_avail * compptr->v_samp_factor));
+      }
+      *out_row_group_ctr = out_row_groups_avail;
+      break;			/* can exit outer loop without test */
+    }
+  }
+}
+
+
+#ifdef CONTEXT_ROWS_SUPPORTED
+
+/*
+ * Process some data in the context case.
+ */
+
+METHODDEF void
+pre_process_context (j_compress_ptr cinfo,
+		     JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+		     JDIMENSION in_rows_avail,
+		     JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
+		     JDIMENSION out_row_groups_avail)
+{
+  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+  int numrows, ci;
+  int buf_height = cinfo->max_v_samp_factor * 3;
+  JDIMENSION inrows;
+  jpeg_component_info * compptr;
+
+  while (*out_row_group_ctr < out_row_groups_avail) {
+    if (*in_row_ctr < in_rows_avail) {
+      /* Do color conversion to fill the conversion buffer. */
+      inrows = in_rows_avail - *in_row_ctr;
+      numrows = prep->next_buf_stop - prep->next_buf_row;
+      numrows = (int) MIN((JDIMENSION) numrows, inrows);
+      (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
+					 prep->color_buf,
+					 (JDIMENSION) prep->next_buf_row,
+					 numrows);
+      /* Pad at top of image, if first time through */
+      if (prep->rows_to_go == cinfo->image_height) {
+	for (ci = 0; ci < cinfo->num_components; ci++) {
+	  int row;
+	  for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
+	    jcopy_sample_rows(prep->color_buf[ci], 0,
+			      prep->color_buf[ci], -row,
+			      1, cinfo->image_width);
+	  }
+	}
+      }
+      *in_row_ctr += numrows;
+      prep->next_buf_row += numrows;
+      prep->rows_to_go -= numrows;
+    } else {
+      /* Return for more data, unless we are at the bottom of the image. */
+      if (prep->rows_to_go != 0)
+	break;
+    }
+    /* If at bottom of image, pad to fill the conversion buffer. */
+    if (prep->rows_to_go == 0 &&
+	prep->next_buf_row < prep->next_buf_stop) {
+      for (ci = 0; ci < cinfo->num_components; ci++) {
+	expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
+			   prep->next_buf_row, prep->next_buf_stop);
+      }
+      prep->next_buf_row = prep->next_buf_stop;
+    }
+    /* If we've gotten enough data, downsample a row group. */
+    if (prep->next_buf_row == prep->next_buf_stop) {
+      (*cinfo->downsample->downsample) (cinfo,
+					prep->color_buf,
+					(JDIMENSION) prep->this_row_group,
+					output_buf, *out_row_group_ctr);
+      (*out_row_group_ctr)++;
+      /* Advance pointers with wraparound as necessary. */
+      prep->this_row_group += cinfo->max_v_samp_factor;
+      if (prep->this_row_group >= buf_height)
+	prep->this_row_group = 0;
+      if (prep->next_buf_row >= buf_height)
+	prep->next_buf_row = 0;
+      prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
+    }
+    /* If at bottom of image, pad the output to a full iMCU height.
+     * Note we assume the caller is providing a one-iMCU-height output buffer!
+     */
+    if (prep->rows_to_go == 0 &&
+	*out_row_group_ctr < out_row_groups_avail) {
+      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	   ci++, compptr++) {
+	expand_bottom_edge(output_buf[ci],
+			   compptr->width_in_blocks * DCTSIZE,
+			   (int) (*out_row_group_ctr * compptr->v_samp_factor),
+			   (int) (out_row_groups_avail * compptr->v_samp_factor));
+      }
+      *out_row_group_ctr = out_row_groups_avail;
+      break;			/* can exit outer loop without test */
+    }
+  }
+}
+
+
+/*
+ * Create the wrapped-around downsampling input buffer needed for context mode.
+ */
+
+LOCAL void
+create_context_buffer (j_compress_ptr cinfo)
+{
+  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+  int rgroup_height = cinfo->max_v_samp_factor;
+  int ci, i;
+  jpeg_component_info * compptr;
+  JSAMPARRAY true_buffer, fake_buffer;
+
+  /* Grab enough space for fake row pointers for all the components;
+   * we need five row groups' worth of pointers for each component.
+   */
+  fake_buffer = (JSAMPARRAY)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(cinfo->num_components * 5 * rgroup_height) *
+				SIZEOF(JSAMPROW));
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    /* Allocate the actual buffer space (3 row groups) for this component.
+     * We make the buffer wide enough to allow the downsampler to edge-expand
+     * horizontally within the buffer, if it so chooses.
+     */
+    true_buffer = (*cinfo->mem->alloc_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
+		      cinfo->max_h_samp_factor) / compptr->h_samp_factor),
+       (JDIMENSION) (3 * rgroup_height));
+    /* Copy true buffer row pointers into the middle of the fake row array */
+    MEMCOPY(fake_buffer + rgroup_height, true_buffer,
+	    3 * rgroup_height * SIZEOF(JSAMPROW));
+    /* Fill in the above and below wraparound pointers */
+    for (i = 0; i < rgroup_height; i++) {
+      fake_buffer[i] = true_buffer[2 * rgroup_height + i];
+      fake_buffer[4 * rgroup_height + i] = true_buffer[i];
+    }
+    prep->color_buf[ci] = fake_buffer + rgroup_height;
+    fake_buffer += 5 * rgroup_height; /* point to space for next component */
+  }
+}
+
+#endif /* CONTEXT_ROWS_SUPPORTED */
+
+
+/*
+ * Initialize preprocessing controller.
+ */
+
+GLOBAL void
+jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
+{
+  my_prep_ptr prep;
+  int ci;
+  jpeg_component_info * compptr;
+
+  if (need_full_buffer)		/* safety check */
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+  prep = (my_prep_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_prep_controller));
+  cinfo->prep = (struct jpeg_c_prep_controller *) prep;
+  prep->pub.start_pass = start_pass_prep;
+
+  /* Allocate the color conversion buffer.
+   * We make the buffer wide enough to allow the downsampler to edge-expand
+   * horizontally within the buffer, if it so chooses.
+   */
+  if (cinfo->downsample->need_context_rows) {
+    /* Set up to provide context rows */
+#ifdef CONTEXT_ROWS_SUPPORTED
+    prep->pub.pre_process_data = pre_process_context;
+    create_context_buffer(cinfo);
+#else
+    ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+  } else {
+    /* No context, just make it tall enough for one row group */
+    prep->pub.pre_process_data = pre_process_data;
+    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	 ci++, compptr++) {
+      prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
+	((j_common_ptr) cinfo, JPOOL_IMAGE,
+	 (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
+			cinfo->max_h_samp_factor) / compptr->h_samp_factor),
+	 (JDIMENSION) cinfo->max_v_samp_factor);
+    }
+  }
+}
diff --git a/jcsample.c b/jcsample.c
index 31a636b..bf0fb46 100644
--- a/jcsample.c
+++ b/jcsample.c
@@ -1,13 +1,28 @@
 /*
  * jcsample.c
  *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains downsampling routines.
- * These routines are invoked via the downsample and
- * downsample_init/term methods.
+ *
+ * Downsampling input data is counted in "row groups".  A row group
+ * is defined to be max_v_samp_factor pixel rows of each component,
+ * from which the downsampler produces v_samp_factor sample rows.
+ * A single row group is processed in each call to the downsampler module.
+ *
+ * The downsampler is responsible for edge-expansion of its output data
+ * to fill an integral number of DCT blocks horizontally.  The source buffer
+ * may be modified if it is helpful for this purpose (the source buffer is
+ * allocated wide enough to correspond to the desired output width).
+ * The caller (the prep controller) is responsible for vertical padding.
+ *
+ * The downsampler may request "context rows" by setting need_context_rows
+ * during startup.  In this case, the input arrays will contain at least
+ * one row group's worth of pixels above and below the passed-in data;
+ * the caller will create dummy rows at image top and bottom by replicating
+ * the first or last real pixel row.
  *
  * An excellent reference for image resampling is
  *   Digital Image Warping, George Wolberg, 1990.
@@ -30,55 +45,121 @@
  * Currently, smoothing is only supported for 2h2v sampling factors.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Pointer to routine to downsample a single component */
+typedef JMETHOD(void, downsample1_ptr,
+		(j_compress_ptr cinfo, jpeg_component_info * compptr,
+		 JSAMPARRAY input_data, JSAMPARRAY output_data));
+
+/* Private subobject */
+
+typedef struct {
+  struct jpeg_downsampler pub;	/* public fields */
+
+  /* Downsampling method pointers, one per component */
+  downsample1_ptr methods[MAX_COMPONENTS];
+} my_downsampler;
+
+typedef my_downsampler * my_downsample_ptr;
 
 
 /*
- * Initialize for downsampling a scan.
+ * Initialize for a downsampling pass.
  */
 
 METHODDEF void
-downsample_init (compress_info_ptr cinfo)
+start_pass_downsample (j_compress_ptr cinfo)
 {
   /* no work for now */
 }
 
 
 /*
+ * Expand a component horizontally from width input_cols to width output_cols,
+ * by duplicating the rightmost samples.
+ */
+
+LOCAL void
+expand_right_edge (JSAMPARRAY image_data, int num_rows,
+		   JDIMENSION input_cols, JDIMENSION output_cols)
+{
+  register JSAMPROW ptr;
+  register JSAMPLE pixval;
+  register int count;
+  int row;
+  int numcols = (int) (output_cols - input_cols);
+
+  if (numcols > 0) {
+    for (row = 0; row < num_rows; row++) {
+      ptr = image_data[row] + input_cols;
+      pixval = ptr[-1];		/* don't need GETJSAMPLE() here */
+      for (count = numcols; count > 0; count--)
+	*ptr++ = pixval;
+    }
+  }
+}
+
+
+/*
+ * Do downsampling for a whole row group (all components).
+ *
+ * In this version we simply downsample each component independently.
+ */
+
+METHODDEF void
+sep_downsample (j_compress_ptr cinfo,
+		JSAMPIMAGE input_buf, JDIMENSION in_row_index,
+		JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
+{
+  my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
+  int ci;
+  jpeg_component_info * compptr;
+  JSAMPARRAY in_ptr, out_ptr;
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    in_ptr = input_buf[ci] + in_row_index;
+    out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
+    (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
+  }
+}
+
+
+/*
  * Downsample pixel values of a single component.
+ * One row group is processed per call.
  * This version handles arbitrary integral sampling ratios, without smoothing.
  * Note that this version is not actually used for customary sampling ratios.
  */
 
 METHODDEF void
-int_downsample (compress_info_ptr cinfo, int which_component,
-		long input_cols, int input_rows,
-		long output_cols, int output_rows,
-		JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-		JSAMPARRAY output_data)
+int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+		JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
-  jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
   int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
-  long outcol, outcol_h;	/* outcol_h == outcol*h_expand */
+  JDIMENSION outcol, outcol_h;	/* outcol_h == outcol*h_expand */
+  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
   JSAMPROW inptr, outptr;
   INT32 outvalue;
 
-#ifdef DEBUG			/* for debugging pipeline controller */
-  if (output_rows != compptr->v_samp_factor ||
-      input_rows != cinfo->max_v_samp_factor ||
-      (output_cols % compptr->h_samp_factor) != 0 ||
-      (input_cols % cinfo->max_h_samp_factor) != 0 ||
-      input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
-    ERREXIT(cinfo->emethods, "Bogus downsample parameters");
-#endif
-
   h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
   v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
   numpix = h_expand * v_expand;
   numpix2 = numpix/2;
 
+  /* Expand input data enough to let all the output samples be generated
+   * by the standard loop.  Special-casing padded output would be more
+   * efficient.
+   */
+  expand_right_edge(input_data, cinfo->max_v_samp_factor,
+		    cinfo->image_width, output_cols * h_expand);
+
   inrow = 0;
-  for (outrow = 0; outrow < output_rows; outrow++) {
+  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
     outptr = output_data[outrow];
     for (outcol = 0, outcol_h = 0; outcol < output_cols;
 	 outcol++, outcol_h += h_expand) {
@@ -98,37 +179,60 @@
 
 /*
  * Downsample pixel values of a single component.
- * This version handles the common case of 2:1 horizontal and 1:1 vertical,
+ * This version handles the special case of a full-size component,
  * without smoothing.
  */
 
 METHODDEF void
-h2v1_downsample (compress_info_ptr cinfo, int which_component,
-		 long input_cols, int input_rows,
-		 long output_cols, int output_rows,
-		 JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-		 JSAMPARRAY output_data)
+fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+		     JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+  /* Copy the data */
+  jcopy_sample_rows(input_data, 0, output_data, 0,
+		    cinfo->max_v_samp_factor, cinfo->image_width);
+  /* Edge-expand */
+  expand_right_edge(output_data, cinfo->max_v_samp_factor,
+		    cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the common case of 2:1 horizontal and 1:1 vertical,
+ * without smoothing.
+ *
+ * A note about the "bias" calculations: when rounding fractional values to
+ * integer, we do not want to always round 0.5 up to the next integer.
+ * If we did that, we'd introduce a noticeable bias towards larger values.
+ * Instead, this code is arranged so that 0.5 will be rounded up or down at
+ * alternate pixel locations (a simple ordered dither pattern).
+ */
+
+METHODDEF void
+h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+		 JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
   int outrow;
-  long outcol;
+  JDIMENSION outcol;
+  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
   register JSAMPROW inptr, outptr;
+  register int bias;
 
-#ifdef DEBUG			/* for debugging pipeline controller */
-  jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
-  if (output_rows != compptr->v_samp_factor ||
-      input_rows != cinfo->max_v_samp_factor ||
-      (output_cols % compptr->h_samp_factor) != 0 ||
-      (input_cols % cinfo->max_h_samp_factor) != 0 ||
-      input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
-    ERREXIT(cinfo->emethods, "Bogus downsample parameters");
-#endif
+  /* Expand input data enough to let all the output samples be generated
+   * by the standard loop.  Special-casing padded output would be more
+   * efficient.
+   */
+  expand_right_edge(input_data, cinfo->max_v_samp_factor,
+		    cinfo->image_width, output_cols * 2);
 
-  for (outrow = 0; outrow < output_rows; outrow++) {
+  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
     outptr = output_data[outrow];
     inptr = input_data[outrow];
+    bias = 0;			/* bias = 0,1,0,1,... for successive samples */
     for (outcol = 0; outcol < output_cols; outcol++) {
       *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
-			      + 1) >> 1);
+			      + bias) >> 1);
+      bias ^= 1;		/* 0=>1, 1=>0 */
       inptr += 2;
     }
   }
@@ -142,35 +246,33 @@
  */
 
 METHODDEF void
-h2v2_downsample (compress_info_ptr cinfo, int which_component,
-		 long input_cols, int input_rows,
-		 long output_cols, int output_rows,
-		 JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-		 JSAMPARRAY output_data)
+h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+		 JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
   int inrow, outrow;
-  long outcol;
+  JDIMENSION outcol;
+  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
   register JSAMPROW inptr0, inptr1, outptr;
+  register int bias;
 
-#ifdef DEBUG			/* for debugging pipeline controller */
-  jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
-  if (output_rows != compptr->v_samp_factor ||
-      input_rows != cinfo->max_v_samp_factor ||
-      (output_cols % compptr->h_samp_factor) != 0 ||
-      (input_cols % cinfo->max_h_samp_factor) != 0 ||
-      input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
-    ERREXIT(cinfo->emethods, "Bogus downsample parameters");
-#endif
+  /* Expand input data enough to let all the output samples be generated
+   * by the standard loop.  Special-casing padded output would be more
+   * efficient.
+   */
+  expand_right_edge(input_data, cinfo->max_v_samp_factor,
+		    cinfo->image_width, output_cols * 2);
 
   inrow = 0;
-  for (outrow = 0; outrow < output_rows; outrow++) {
+  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
     outptr = output_data[outrow];
     inptr0 = input_data[inrow];
     inptr1 = input_data[inrow+1];
+    bias = 1;			/* bias = 1,2,1,2,... for successive samples */
     for (outcol = 0; outcol < output_cols; outcol++) {
       *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
 			      GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
-			      + 2) >> 2);
+			      + bias) >> 2);
+      bias ^= 3;		/* 1=>2, 2=>1 */
       inptr0 += 2; inptr1 += 2;
     }
     inrow += 2;
@@ -178,57 +280,30 @@
 }
 
 
-/*
- * Downsample pixel values of a single component.
- * This version handles the special case of a full-size component,
- * without smoothing.
- */
-
-METHODDEF void
-fullsize_downsample (compress_info_ptr cinfo, int which_component,
-		     long input_cols, int input_rows,
-		     long output_cols, int output_rows,
-		     JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-		     JSAMPARRAY output_data)
-{
-#ifdef DEBUG			/* for debugging pipeline controller */
-  if (input_cols != output_cols || input_rows != output_rows)
-    ERREXIT(cinfo->emethods, "Pipeline controller messed up");
-#endif
-
-  jcopy_sample_rows(input_data, 0, output_data, 0, output_rows, output_cols);
-}
-
-
 #ifdef INPUT_SMOOTHING_SUPPORTED
 
 /*
  * Downsample pixel values of a single component.
  * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
- * with smoothing.
+ * with smoothing.  One row of context is required.
  */
 
 METHODDEF void
-h2v2_smooth_downsample (compress_info_ptr cinfo, int which_component,
-			long input_cols, int input_rows,
-			long output_cols, int output_rows,
-			JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-			JSAMPARRAY output_data)
+h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+			JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
   int inrow, outrow;
-  long colctr;
+  JDIMENSION colctr;
+  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
   register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
   INT32 membersum, neighsum, memberscale, neighscale;
 
-#ifdef DEBUG			/* for debugging pipeline controller */
-  jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
-  if (output_rows != compptr->v_samp_factor ||
-      input_rows != cinfo->max_v_samp_factor ||
-      (output_cols % compptr->h_samp_factor) != 0 ||
-      (input_cols % cinfo->max_h_samp_factor) != 0 ||
-      input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
-    ERREXIT(cinfo->emethods, "Bogus downsample parameters");
-#endif
+  /* Expand input data enough to let all the output samples be generated
+   * by the standard loop.  Special-casing padded output would be more
+   * efficient.
+   */
+  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
+		    cinfo->image_width, output_cols * 2);
 
   /* We don't bother to form the individual "smoothed" input pixel values;
    * we can directly compute the output which is the average of the four
@@ -247,18 +322,12 @@
   neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
 
   inrow = 0;
-  for (outrow = 0; outrow < output_rows; outrow++) {
+  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
     outptr = output_data[outrow];
     inptr0 = input_data[inrow];
     inptr1 = input_data[inrow+1];
-    if (inrow == 0)
-      above_ptr = above[input_rows-1];
-    else
-      above_ptr = input_data[inrow-1];
-    if (inrow >= input_rows-2)
-      below_ptr = below[0];
-    else
-      below_ptr = input_data[inrow+2];
+    above_ptr = input_data[inrow-1];
+    below_ptr = input_data[inrow+2];
 
     /* Special case for first column: pretend column -1 is same as column 0 */
     membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
@@ -271,7 +340,7 @@
     neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
 		GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
     membersum = membersum * memberscale + neighsum * neighscale;
-    *outptr++ = (JSAMPLE) ((membersum + 32768L) >> 16);
+    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
     inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
 
     for (colctr = output_cols - 2; colctr > 0; colctr--) {
@@ -291,7 +360,7 @@
       /* form final output scaled up by 2^16 */
       membersum = membersum * memberscale + neighsum * neighscale;
       /* round, descale and output it */
-      *outptr++ = (JSAMPLE) ((membersum + 32768L) >> 16);
+      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
       inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
     }
 
@@ -306,7 +375,7 @@
     neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
 		GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
     membersum = membersum * memberscale + neighsum * neighscale;
-    *outptr = (JSAMPLE) ((membersum + 32768L) >> 16);
+    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
 
     inrow += 2;
   }
@@ -316,26 +385,26 @@
 /*
  * Downsample pixel values of a single component.
  * This version handles the special case of a full-size component,
- * with smoothing.
+ * with smoothing.  One row of context is required.
  */
 
 METHODDEF void
-fullsize_smooth_downsample (compress_info_ptr cinfo, int which_component,
-			    long input_cols, int input_rows,
-			    long output_cols, int output_rows,
-			    JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-			    JSAMPARRAY output_data)
+fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
+			    JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
   int outrow;
-  long colctr;
+  JDIMENSION colctr;
+  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
   register JSAMPROW inptr, above_ptr, below_ptr, outptr;
   INT32 membersum, neighsum, memberscale, neighscale;
   int colsum, lastcolsum, nextcolsum;
 
-#ifdef DEBUG			/* for debugging pipeline controller */
-  if (input_cols != output_cols || input_rows != output_rows)
-    ERREXIT(cinfo->emethods, "Pipeline controller messed up");
-#endif
+  /* Expand input data enough to let all the output samples be generated
+   * by the standard loop.  Special-casing padded output would be more
+   * efficient.
+   */
+  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
+		    cinfo->image_width, output_cols);
 
   /* Each of the eight neighbor pixels contributes a fraction SF to the
    * smoothed pixel, while the main pixel contributes (1-8*SF).  In order
@@ -346,17 +415,11 @@
   memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
   neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
 
-  for (outrow = 0; outrow < output_rows; outrow++) {
+  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
     outptr = output_data[outrow];
     inptr = input_data[outrow];
-    if (outrow == 0)
-      above_ptr = above[input_rows-1];
-    else
-      above_ptr = input_data[outrow-1];
-    if (outrow >= input_rows-1)
-      below_ptr = below[0];
-    else
-      below_ptr = input_data[outrow+1];
+    above_ptr = input_data[outrow-1];
+    below_ptr = input_data[outrow+1];
 
     /* Special case for first column */
     colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
@@ -366,7 +429,7 @@
 		 GETJSAMPLE(*inptr);
     neighsum = colsum + (colsum - membersum) + nextcolsum;
     membersum = membersum * memberscale + neighsum * neighscale;
-    *outptr++ = (JSAMPLE) ((membersum + 32768L) >> 16);
+    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
     lastcolsum = colsum; colsum = nextcolsum;
 
     for (colctr = output_cols - 2; colctr > 0; colctr--) {
@@ -376,7 +439,7 @@
 		   GETJSAMPLE(*inptr);
       neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
       membersum = membersum * memberscale + neighsum * neighscale;
-      *outptr++ = (JSAMPLE) ((membersum + 32768L) >> 16);
+      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
       lastcolsum = colsum; colsum = nextcolsum;
     }
 
@@ -384,7 +447,7 @@
     membersum = GETJSAMPLE(*inptr);
     neighsum = lastcolsum + (colsum - membersum) + colsum;
     membersum = membersum * memberscale + neighsum * neighscale;
-    *outptr = (JSAMPLE) ((membersum + 32768L) >> 16);
+    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
 
   }
 }
@@ -393,68 +456,64 @@
 
 
 /*
- * Clean up after a scan.
- */
-
-METHODDEF void
-downsample_term (compress_info_ptr cinfo)
-{
-  /* no work for now */
-}
-
-
-
-/*
- * The method selection routine for downsampling.
+ * Module initialization routine for downsampling.
  * Note that we must select a routine for each component.
  */
 
 GLOBAL void
-jseldownsample (compress_info_ptr cinfo)
+jinit_downsampler (j_compress_ptr cinfo)
 {
-  short ci;
+  my_downsample_ptr downsample;
+  int ci;
   jpeg_component_info * compptr;
   boolean smoothok = TRUE;
 
-  if (cinfo->CCIR601_sampling)
-    ERREXIT(cinfo->emethods, "CCIR601 downsampling not implemented yet");
+  downsample = (my_downsample_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_downsampler));
+  cinfo->downsample = (struct jpeg_downsampler *) downsample;
+  downsample->pub.start_pass = start_pass_downsample;
+  downsample->pub.downsample = sep_downsample;
+  downsample->pub.need_context_rows = FALSE;
 
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
+  if (cinfo->CCIR601_sampling)
+    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
+
+  /* Verify we can handle the sampling factors, and set up method pointers */
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
     if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
 	compptr->v_samp_factor == cinfo->max_v_samp_factor) {
 #ifdef INPUT_SMOOTHING_SUPPORTED
-      if (cinfo->smoothing_factor)
-	cinfo->methods->downsample[ci] = fullsize_smooth_downsample;
-      else
+      if (cinfo->smoothing_factor) {
+	downsample->methods[ci] = fullsize_smooth_downsample;
+	downsample->pub.need_context_rows = TRUE;
+      } else
 #endif
-	cinfo->methods->downsample[ci] = fullsize_downsample;
+	downsample->methods[ci] = fullsize_downsample;
     } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
-	     compptr->v_samp_factor == cinfo->max_v_samp_factor) {
+	       compptr->v_samp_factor == cinfo->max_v_samp_factor) {
       smoothok = FALSE;
-      cinfo->methods->downsample[ci] = h2v1_downsample;
+      downsample->methods[ci] = h2v1_downsample;
     } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
-	     compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
+	       compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
 #ifdef INPUT_SMOOTHING_SUPPORTED
-      if (cinfo->smoothing_factor)
-	cinfo->methods->downsample[ci] = h2v2_smooth_downsample;
-      else
+      if (cinfo->smoothing_factor) {
+	downsample->methods[ci] = h2v2_smooth_downsample;
+	downsample->pub.need_context_rows = TRUE;
+      } else
 #endif
-	cinfo->methods->downsample[ci] = h2v2_downsample;
+	downsample->methods[ci] = h2v2_downsample;
     } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
-	     (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
+	       (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
       smoothok = FALSE;
-      cinfo->methods->downsample[ci] = int_downsample;
+      downsample->methods[ci] = int_downsample;
     } else
-      ERREXIT(cinfo->emethods, "Fractional downsampling not implemented yet");
+      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
   }
 
 #ifdef INPUT_SMOOTHING_SUPPORTED
   if (cinfo->smoothing_factor && !smoothok)
-    TRACEMS(cinfo->emethods, 0,
-	    "Smoothing not supported with nonstandard sampling ratios");
+    TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
 #endif
-
-  cinfo->methods->downsample_init = downsample_init;
-  cinfo->methods->downsample_term = downsample_term;
 }
diff --git a/jdapi.c b/jdapi.c
new file mode 100644
index 0000000..b34b701
--- /dev/null
+++ b/jdapi.c
@@ -0,0 +1,438 @@
+/*
+ * jdapi.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains application interface code for the decompression half of
+ * the JPEG library.  Most of the routines intended to be called directly by
+ * an application are in this file.  But also see jcomapi.c for routines
+ * shared by compression and decompression.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Initialization of a JPEG decompression object.
+ * The error manager must already be set up (in case memory manager fails).
+ */
+
+GLOBAL void
+jpeg_create_decompress (j_decompress_ptr cinfo)
+{
+  int i;
+
+  /* For debugging purposes, zero the whole master structure.
+   * But error manager pointer is already there, so save and restore it.
+   */
+  {
+    struct jpeg_error_mgr * err = cinfo->err;
+    MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
+    cinfo->err = err;
+  }
+  cinfo->is_decompressor = TRUE;
+
+  /* Initialize a memory manager instance for this object */
+  jinit_memory_mgr((j_common_ptr) cinfo);
+
+  /* Zero out pointers to permanent structures. */
+  cinfo->progress = NULL;
+  cinfo->src = NULL;
+
+  for (i = 0; i < NUM_QUANT_TBLS; i++)
+    cinfo->quant_tbl_ptrs[i] = NULL;
+
+  for (i = 0; i < NUM_HUFF_TBLS; i++) {
+    cinfo->dc_huff_tbl_ptrs[i] = NULL;
+    cinfo->ac_huff_tbl_ptrs[i] = NULL;
+  }
+
+  cinfo->sample_range_limit = NULL;
+
+  /* Initialize marker processor so application can override methods
+   * for COM, APPn markers before calling jpeg_read_header.
+   */
+  cinfo->marker = NULL;
+  jinit_marker_reader(cinfo);
+
+  /* OK, I'm ready */
+  cinfo->global_state = DSTATE_START;
+}
+
+
+/*
+ * Destruction of a JPEG decompression object
+ */
+
+GLOBAL void
+jpeg_destroy_decompress (j_decompress_ptr cinfo)
+{
+  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Install a special processing method for COM or APPn markers.
+ */
+
+GLOBAL void
+jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
+			   jpeg_marker_parser_method routine)
+{
+  if (marker_code == JPEG_COM)
+    cinfo->marker->process_COM = routine;
+  else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
+    cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
+  else
+    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
+}
+
+
+/*
+ * Set default decompression parameters.
+ */
+
+LOCAL void
+default_decompress_parms (j_decompress_ptr cinfo)
+{
+  /* Guess the input colorspace, and set output colorspace accordingly. */
+  /* (Wish JPEG committee had provided a real way to specify this...) */
+  /* Note application may override our guesses. */
+  switch (cinfo->num_components) {
+  case 1:
+    cinfo->jpeg_color_space = JCS_GRAYSCALE;
+    cinfo->out_color_space = JCS_GRAYSCALE;
+    break;
+    
+  case 3:
+    if (cinfo->saw_JFIF_marker) {
+      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
+    } else if (cinfo->saw_Adobe_marker) {
+      switch (cinfo->Adobe_transform) {
+      case 0:
+	cinfo->jpeg_color_space = JCS_RGB;
+	break;
+      case 1:
+	cinfo->jpeg_color_space = JCS_YCbCr;
+	break;
+      default:
+	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
+	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
+	break;
+      }
+    } else {
+      /* Saw no special markers, try to guess from the component IDs */
+      int cid0 = cinfo->comp_info[0].component_id;
+      int cid1 = cinfo->comp_info[1].component_id;
+      int cid2 = cinfo->comp_info[2].component_id;
+
+      if (cid0 == 1 && cid1 == 2 && cid2 == 3)
+	cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
+      else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
+	cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
+      else {
+	TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
+	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
+      }
+    }
+    /* Always guess RGB is proper output colorspace. */
+    cinfo->out_color_space = JCS_RGB;
+    break;
+    
+  case 4:
+    if (cinfo->saw_Adobe_marker) {
+      switch (cinfo->Adobe_transform) {
+      case 0:
+	cinfo->jpeg_color_space = JCS_CMYK;
+	break;
+      case 2:
+	cinfo->jpeg_color_space = JCS_YCCK;
+	break;
+      default:
+	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
+	cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
+	break;
+      }
+    } else {
+      /* No special markers, assume straight CMYK. */
+      cinfo->jpeg_color_space = JCS_CMYK;
+    }
+    cinfo->out_color_space = JCS_CMYK;
+    break;
+    
+  default:
+    cinfo->jpeg_color_space = JCS_UNKNOWN;
+    cinfo->out_color_space = JCS_UNKNOWN;
+    break;
+  }
+
+  /* Set defaults for other decompression parameters. */
+  cinfo->scale_num = 1;		/* 1:1 scaling */
+  cinfo->scale_denom = 1;
+  cinfo->output_gamma = 1.0;
+  cinfo->raw_data_out = FALSE;
+  cinfo->quantize_colors = FALSE;
+  /* We set these in case application only sets quantize_colors. */
+  cinfo->two_pass_quantize = TRUE;
+  cinfo->dither_mode = JDITHER_FS;
+  cinfo->desired_number_of_colors = 256;
+  cinfo->colormap = NULL;
+  /* DCT algorithm preference */
+  cinfo->dct_method = JDCT_DEFAULT;
+  cinfo->do_fancy_upsampling = TRUE;
+}
+
+
+/*
+ * Decompression startup: read start of JPEG datastream to see what's there.
+ * Need only initialize JPEG object and supply a data source before calling.
+ *
+ * This routine will read as far as the first SOS marker (ie, actual start of
+ * compressed data), and will save all tables and parameters in the JPEG
+ * object.  It will also initialize the decompression parameters to default
+ * values, and finally return JPEG_HEADER_OK.  On return, the application may
+ * adjust the decompression parameters and then call jpeg_start_decompress.
+ * (Or, if the application only wanted to determine the image parameters,
+ * the data need not be decompressed.  In that case, call jpeg_abort or
+ * jpeg_destroy to release any temporary space.)
+ * If an abbreviated (tables only) datastream is presented, the routine will
+ * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
+ * re-use the JPEG object to read the abbreviated image datastream(s).
+ * It is unnecessary (but OK) to call jpeg_abort in this case.
+ * The JPEG_SUSPENDED return code only occurs if the data source module
+ * requests suspension of the decompressor.  In this case the application
+ * should load more source data and then re-call jpeg_read_header to resume
+ * processing.
+ * If a non-suspending data source is used and require_image is TRUE, then the
+ * return code need not be inspected since only JPEG_HEADER_OK is possible.
+ */
+
+GLOBAL int
+jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
+{
+  int retcode;
+
+  if (cinfo->global_state == DSTATE_START) {
+    /* First-time actions: reset appropriate modules */
+    (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+    (*cinfo->marker->reset_marker_reader) (cinfo);
+    (*cinfo->src->init_source) (cinfo);
+    cinfo->global_state = DSTATE_INHEADER;
+  } else if (cinfo->global_state != DSTATE_INHEADER) {
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+  }
+
+  retcode = (*cinfo->marker->read_markers) (cinfo);
+
+  switch (retcode) {
+  case JPEG_HEADER_OK:		/* Found SOS, prepare to decompress */
+    /* Set up default parameters based on header data */
+    default_decompress_parms(cinfo);
+    /* Set global state: ready for start_decompress */
+    cinfo->global_state = DSTATE_READY;
+    break;
+
+  case JPEG_HEADER_TABLES_ONLY:	/* Found EOI before any SOS */
+    if (cinfo->marker->saw_SOF)
+      ERREXIT(cinfo, JERR_SOF_NO_SOS);
+    if (require_image)		/* Complain if application wants an image */
+      ERREXIT(cinfo, JERR_NO_IMAGE);
+    /* We need not do any cleanup since only permanent storage (for DQT, DHT)
+     * has been allocated.
+     */
+    /* Set global state: ready for a new datastream */
+    cinfo->global_state = DSTATE_START;
+    break;
+
+  case JPEG_SUSPENDED:		/* Had to suspend before end of headers */
+    /* no work */
+    break;
+  }
+
+  return retcode;
+}
+
+
+/*
+ * Decompression initialization.
+ * jpeg_read_header must be completed before calling this.
+ *
+ * If a multipass operating mode was selected, this will do all but the
+ * last pass, and thus may take a great deal of time.
+ */
+
+GLOBAL void
+jpeg_start_decompress (j_decompress_ptr cinfo)
+{
+  JDIMENSION chunk_ctr, last_chunk_ctr;
+
+  if (cinfo->global_state != DSTATE_READY)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+  /* Perform master selection of active modules */
+  jinit_master_decompress(cinfo);
+  /* Do all but the final (output) pass, and set up for that one. */
+  for (;;) {
+    (*cinfo->master->prepare_for_pass) (cinfo);
+    if (cinfo->master->is_last_pass)
+      break;
+    chunk_ctr = 0;
+    while (chunk_ctr < cinfo->main->num_chunks) {
+      /* Call progress monitor hook if present */
+      if (cinfo->progress != NULL) {
+	cinfo->progress->pass_counter = (long) chunk_ctr;
+	cinfo->progress->pass_limit = (long) cinfo->main->num_chunks;
+	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+      }
+      /* Process some data */
+      last_chunk_ctr = chunk_ctr;
+      (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
+				    &chunk_ctr, (JDIMENSION) 0);
+      if (chunk_ctr == last_chunk_ctr) /* check for failure to make progress */
+	ERREXIT(cinfo, JERR_CANT_SUSPEND);
+    }
+    (*cinfo->master->finish_pass) (cinfo);
+  }
+  /* Ready for application to drive last pass through jpeg_read_scanlines
+   * or jpeg_read_raw_data.
+   */
+  cinfo->output_scanline = 0;
+  cinfo->global_state = (cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING);
+}
+
+
+/*
+ * Read some scanlines of data from the JPEG decompressor.
+ *
+ * The return value will be the number of lines actually read.
+ * This may be less than the number requested in several cases,
+ * including bottom of image, data source suspension, and operating
+ * modes that emit multiple scanlines at a time.
+ *
+ * Note: we warn about excess calls to jpeg_read_scanlines() since
+ * this likely signals an application programmer error.  However,
+ * an oversize buffer (max_lines > scanlines remaining) is not an error.
+ */
+
+GLOBAL JDIMENSION
+jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
+		     JDIMENSION max_lines)
+{
+  JDIMENSION row_ctr;
+
+  if (cinfo->global_state != DSTATE_SCANNING)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+  if (cinfo->output_scanline >= cinfo->output_height)
+    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+
+  /* Call progress monitor hook if present */
+  if (cinfo->progress != NULL) {
+    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
+    cinfo->progress->pass_limit = (long) cinfo->output_height;
+    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+  }
+
+  /* Process some data */
+  row_ctr = 0;
+  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
+  cinfo->output_scanline += row_ctr;
+  return row_ctr;
+}
+
+
+/*
+ * Alternate entry point to read raw data.
+ * Processes exactly one MCU row per call.
+ */
+
+GLOBAL JDIMENSION
+jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
+		    JDIMENSION max_lines)
+{
+  JDIMENSION lines_per_MCU_row;
+
+  if (cinfo->global_state != DSTATE_RAW_OK)
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+  if (cinfo->output_scanline >= cinfo->output_height) {
+    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+    return 0;
+  }
+
+  /* Call progress monitor hook if present */
+  if (cinfo->progress != NULL) {
+    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
+    cinfo->progress->pass_limit = (long) cinfo->output_height;
+    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+  }
+
+  /* Verify that at least one MCU row can be returned. */
+  lines_per_MCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
+  if (max_lines < lines_per_MCU_row)
+    ERREXIT(cinfo, JERR_BUFFER_SIZE);
+
+  /* Decompress directly into user's buffer. */
+  if (! (*cinfo->coef->decompress_data) (cinfo, data))
+    return 0;			/* suspension forced, can do nothing more */
+
+  /* OK, we processed one MCU row. */
+  cinfo->output_scanline += lines_per_MCU_row;
+  return lines_per_MCU_row;
+}
+
+
+/*
+ * Finish JPEG decompression.
+ *
+ * This will normally just verify the file trailer and release temp storage.
+ *
+ * Returns FALSE if suspended.  The return value need be inspected only if
+ * a suspending data source is used.
+ */
+
+GLOBAL boolean
+jpeg_finish_decompress (j_decompress_ptr cinfo)
+{
+  if (cinfo->global_state == DSTATE_SCANNING ||
+      cinfo->global_state == DSTATE_RAW_OK) {
+    /* Terminate final pass */
+    if (cinfo->output_scanline < cinfo->output_height)
+      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
+    (*cinfo->master->finish_pass) (cinfo);
+    cinfo->global_state = DSTATE_STOPPING;
+  } else if (cinfo->global_state != DSTATE_STOPPING) {
+    /* Repeat call after a suspension? */
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+  }
+  /* Check for EOI in source file, unless master control already read it */
+  if (! cinfo->master->eoi_processed) {
+    switch ((*cinfo->marker->read_markers) (cinfo)) {
+    case JPEG_HEADER_OK:	/* Found SOS!? */
+      ERREXIT(cinfo, JERR_EOI_EXPECTED);
+      break;
+    case JPEG_HEADER_TABLES_ONLY: /* Found EOI, A-OK */
+      break;
+    case JPEG_SUSPENDED:	/* Suspend, come back later */
+      return FALSE;
+    }
+  }
+  /* Do final cleanup */
+  (*cinfo->src->term_source) (cinfo);
+  /* We can use jpeg_abort to release memory and reset global_state */
+  jpeg_abort((j_common_ptr) cinfo);
+  return TRUE;
+}
+
+
+/*
+ * Abort processing of a JPEG decompression operation,
+ * but don't destroy the object itself.
+ */
+
+GLOBAL void
+jpeg_abort_decompress (j_decompress_ptr cinfo)
+{
+  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
+}
diff --git a/jdarith.c b/jdarith.c
deleted file mode 100644
index 7265f7e..0000000
--- a/jdarith.c
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * jdarith.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains arithmetic entropy decoding routines.
- * These routines are invoked via the methods entropy_decode
- * and entropy_decode_init/term.
- */
-
-#include "jinclude.h"
-
-#ifdef D_ARITH_CODING_SUPPORTED
-
-
-/*
- * The arithmetic coding option of the JPEG standard specifies Q-coding,
- * which is covered by patents held by IBM (and possibly AT&T and Mitsubishi).
- * At this time it does not appear to be legal for the Independent JPEG
- * Group to distribute software that implements arithmetic coding.
- * We have therefore removed arithmetic coding support from the
- * distributed source code.
- *
- * We're not happy about it either.
- */
-
-
-/*
- * The method selection routine for arithmetic entropy decoding.
- */
-
-GLOBAL void
-jseldarithmetic (decompress_info_ptr cinfo)
-{
-  if (cinfo->arith_code) {
-    ERREXIT(cinfo->emethods, "Sorry, there are legal restrictions on arithmetic coding");
-  }
-}
-
-#endif /* D_ARITH_CODING_SUPPORTED */
diff --git a/jdatadst.c b/jdatadst.c
new file mode 100644
index 0000000..08c4daf
--- /dev/null
+++ b/jdatadst.c
@@ -0,0 +1,151 @@
+/*
+ * jdatadst.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains compression data destination routines for the case of
+ * emitting JPEG data to a file (or any stdio stream).  While these routines
+ * are sufficient for most applications, some will want to use a different
+ * destination manager.
+ * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
+ * JOCTETs into 8-bit-wide elements on external storage.  If char is wider
+ * than 8 bits on your machine, you may need to do some tweaking.
+ */
+
+/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jerror.h"
+
+
+/* Expanded data destination object for stdio output */
+
+typedef struct {
+  struct jpeg_destination_mgr pub; /* public fields */
+
+  FILE * outfile;		/* target stream */
+  JOCTET * buffer;		/* start of buffer */
+} my_destination_mgr;
+
+typedef my_destination_mgr * my_dest_ptr;
+
+#define OUTPUT_BUF_SIZE  4096	/* choose an efficiently fwrite'able size */
+
+
+/*
+ * Initialize destination --- called by jpeg_start_compress
+ * before any data is actually written.
+ */
+
+METHODDEF void
+init_destination (j_compress_ptr cinfo)
+{
+  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
+
+  /* Allocate the output buffer --- it will be released when done with image */
+  dest->buffer = (JOCTET *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
+
+  dest->pub.next_output_byte = dest->buffer;
+  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
+}
+
+
+/*
+ * Empty the output buffer --- called whenever buffer fills up.
+ *
+ * In typical applications, this should write the entire output buffer
+ * (ignoring the current state of next_output_byte & free_in_buffer),
+ * reset the pointer & count to the start of the buffer, and return TRUE
+ * indicating that the buffer has been dumped.
+ *
+ * In applications that need to be able to suspend compression due to output
+ * overrun, a FALSE return indicates that the buffer cannot be emptied now.
+ * In this situation, the compressor will return to its caller (possibly with
+ * an indication that it has not accepted all the supplied scanlines).  The
+ * application should resume compression after it has made more room in the
+ * output buffer.  Note that there are substantial restrictions on the use of
+ * suspension --- see the documentation.
+ *
+ * When suspending, the compressor will back up to a convenient restart point
+ * (typically the start of the current MCU). next_output_byte & free_in_buffer
+ * indicate where the restart point will be if the current call returns FALSE.
+ * Data beyond this point will be regenerated after resumption, so do not
+ * write it out when emptying the buffer externally.
+ */
+
+METHODDEF boolean
+empty_output_buffer (j_compress_ptr cinfo)
+{
+  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
+
+  if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
+      (size_t) OUTPUT_BUF_SIZE)
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+
+  dest->pub.next_output_byte = dest->buffer;
+  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
+
+  return TRUE;
+}
+
+
+/*
+ * Terminate destination --- called by jpeg_finish_compress
+ * after all data has been written.  Usually needs to flush buffer.
+ *
+ * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
+ * application must deal with any cleanup that should happen even
+ * for error exit.
+ */
+
+METHODDEF void
+term_destination (j_compress_ptr cinfo)
+{
+  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
+  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
+
+  /* Write any data remaining in the buffer */
+  if (datacount > 0) {
+    if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
+      ERREXIT(cinfo, JERR_FILE_WRITE);
+  }
+  fflush(dest->outfile);
+  /* Make sure we wrote the output file OK */
+  if (ferror(dest->outfile))
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * Prepare for output to a stdio stream.
+ * The caller must have already opened the stream, and is responsible
+ * for closing it after finishing compression.
+ */
+
+GLOBAL void
+jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
+{
+  my_dest_ptr dest;
+
+  /* The destination object is made permanent so that multiple JPEG images
+   * can be written to the same file without re-executing jpeg_stdio_dest.
+   * This makes it dangerous to use this manager and a different destination
+   * manager serially with the same JPEG object, because their private object
+   * sizes may be different.  Caveat programmer.
+   */
+  if (cinfo->dest == NULL) {	/* first time for this JPEG object? */
+    cinfo->dest = (struct jpeg_destination_mgr *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+				  SIZEOF(my_destination_mgr));
+  }
+
+  dest = (my_dest_ptr) cinfo->dest;
+  dest->pub.init_destination = init_destination;
+  dest->pub.empty_output_buffer = empty_output_buffer;
+  dest->pub.term_destination = term_destination;
+  dest->outfile = outfile;
+}
diff --git a/jdatasrc.c b/jdatasrc.c
new file mode 100644
index 0000000..a37bdd2
--- /dev/null
+++ b/jdatasrc.c
@@ -0,0 +1,209 @@
+/*
+ * jdatasrc.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains decompression data source routines for the case of
+ * reading JPEG data from a file (or any stdio stream).  While these routines
+ * are sufficient for most applications, some will want to use a different
+ * source manager.
+ * IMPORTANT: we assume that fread() will correctly transcribe an array of
+ * JOCTETs from 8-bit-wide elements on external storage.  If char is wider
+ * than 8 bits on your machine, you may need to do some tweaking.
+ */
+
+/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jerror.h"
+
+
+/* Expanded data source object for stdio input */
+
+typedef struct {
+  struct jpeg_source_mgr pub;	/* public fields */
+
+  FILE * infile;		/* source stream */
+  JOCTET * buffer;		/* start of buffer */
+  boolean start_of_file;	/* have we gotten any data yet? */
+} my_source_mgr;
+
+typedef my_source_mgr * my_src_ptr;
+
+#define INPUT_BUF_SIZE  4096	/* choose an efficiently fread'able size */
+
+
+/*
+ * Initialize source --- called by jpeg_read_header
+ * before any data is actually read.
+ */
+
+METHODDEF void
+init_source (j_decompress_ptr cinfo)
+{
+  my_src_ptr src = (my_src_ptr) cinfo->src;
+
+  /* We reset the empty-input-file flag for each image,
+   * but we don't clear the input buffer.
+   * This is correct behavior for reading a series of images from one source.
+   */
+  src->start_of_file = TRUE;
+}
+
+
+/*
+ * Fill the input buffer --- called whenever buffer is emptied.
+ *
+ * In typical applications, this should read fresh data into the buffer
+ * (ignoring the current state of next_input_byte & bytes_in_buffer),
+ * reset the pointer & count to the start of the buffer, and return TRUE
+ * indicating that the buffer has been reloaded.  It is not necessary to
+ * fill the buffer entirely, only to obtain at least one more byte.
+ *
+ * There is no such thing as an EOF return.  If the end of the file has been
+ * reached, the routine has a choice of ERREXIT() or inserting fake data into
+ * the buffer.  In most cases, generating a warning message and inserting a
+ * fake EOI marker is the best course of action --- this will allow the
+ * decompressor to output however much of the image is there.  However,
+ * the resulting error message is misleading if the real problem is an empty
+ * input file, so we handle that case specially.
+ *
+ * In applications that need to be able to suspend compression due to input
+ * not being available yet, a FALSE return indicates that no more data can be
+ * obtained right now, but more may be forthcoming later.  In this situation,
+ * the decompressor will return to its caller (with an indication of the
+ * number of scanlines it has read, if any).  The application should resume
+ * decompression after it has loaded more data into the input buffer.  Note
+ * that there are substantial restrictions on the use of suspension --- see
+ * the documentation.
+ *
+ * When suspending, the decompressor will back up to a convenient restart point
+ * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
+ * indicate where the restart point will be if the current call returns FALSE.
+ * Data beyond this point must be rescanned after resumption, so move it to
+ * the front of the buffer rather than discarding it.
+ */
+
+METHODDEF boolean
+fill_input_buffer (j_decompress_ptr cinfo)
+{
+  my_src_ptr src = (my_src_ptr) cinfo->src;
+  size_t nbytes;
+
+  nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
+
+  if (nbytes <= 0) {
+    if (src->start_of_file)	/* Treat empty input file as fatal error */
+      ERREXIT(cinfo, JERR_INPUT_EMPTY);
+    WARNMS(cinfo, JWRN_JPEG_EOF);
+    /* Insert a fake EOI marker */
+    src->buffer[0] = (JOCTET) 0xFF;
+    src->buffer[1] = (JOCTET) JPEG_EOI;
+    nbytes = 2;
+  }
+
+  src->pub.next_input_byte = src->buffer;
+  src->pub.bytes_in_buffer = nbytes;
+  src->start_of_file = FALSE;
+
+  return TRUE;
+}
+
+
+/*
+ * Skip data --- used to skip over a potentially large amount of
+ * uninteresting data (such as an APPn marker).
+ *
+ * Writers of suspendable-input applications must note that skip_input_data
+ * is not granted the right to give a suspension return.  If the skip extends
+ * beyond the data currently in the buffer, the buffer can be marked empty so
+ * that the next read will cause a fill_input_buffer call that can suspend.
+ * Arranging for additional bytes to be discarded before reloading the input
+ * buffer is the application writer's problem.
+ */
+
+METHODDEF void
+skip_input_data (j_decompress_ptr cinfo, long num_bytes)
+{
+  my_src_ptr src = (my_src_ptr) cinfo->src;
+
+  /* Just a dumb implementation for now.  Could use fseek() except
+   * it doesn't work on pipes.  Not clear that being smart is worth
+   * any trouble anyway --- large skips are infrequent.
+   */
+  if (num_bytes > 0) {
+    while (num_bytes > (long) src->pub.bytes_in_buffer) {
+      num_bytes -= (long) src->pub.bytes_in_buffer;
+      (void) fill_input_buffer(cinfo);
+    }
+    src->pub.next_input_byte += (size_t) num_bytes;
+    src->pub.bytes_in_buffer -= (size_t) num_bytes;
+  }
+}
+
+
+/*
+ * An additional method that can be provided by data source modules is the
+ * resync_to_restart method for error recovery in the presence of RST markers.
+ * For the moment, this source module just uses the default resync method
+ * provided by the JPEG library.  That method assumes that no backtracking
+ * is possible.
+ */
+
+
+/*
+ * Terminate source --- called by jpeg_finish_decompress
+ * after all data has been read.  Often a no-op.
+ *
+ * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
+ * application must deal with any cleanup that should happen even
+ * for error exit.
+ */
+
+METHODDEF void
+term_source (j_decompress_ptr cinfo)
+{
+  /* no work necessary here */
+}
+
+
+/*
+ * Prepare for input from a stdio stream.
+ * The caller must have already opened the stream, and is responsible
+ * for closing it after finishing decompression.
+ */
+
+GLOBAL void
+jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
+{
+  my_src_ptr src;
+
+  /* The source object and input buffer are made permanent so that a series
+   * of JPEG images can be read from the same file by calling jpeg_stdio_src
+   * only before the first one.  (If we discarded the buffer at the end of
+   * one image, we'd likely lose the start of the next one.)
+   * This makes it unsafe to use this manager and a different source
+   * manager serially with the same JPEG object.  Caveat programmer.
+   */
+  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
+    cinfo->src = (struct jpeg_source_mgr *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+				  SIZEOF(my_source_mgr));
+    src = (my_src_ptr) cinfo->src;
+    src->buffer = (JOCTET *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+				  INPUT_BUF_SIZE * SIZEOF(JOCTET));
+  }
+
+  src = (my_src_ptr) cinfo->src;
+  src->pub.init_source = init_source;
+  src->pub.fill_input_buffer = fill_input_buffer;
+  src->pub.skip_input_data = skip_input_data;
+  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
+  src->pub.term_source = term_source;
+  src->infile = infile;
+  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
+  src->pub.next_input_byte = NULL; /* until buffer loaded */
+}
diff --git a/jdcoefct.c b/jdcoefct.c
new file mode 100644
index 0000000..19790c5
--- /dev/null
+++ b/jdcoefct.c
@@ -0,0 +1,359 @@
+/*
+ * jdcoefct.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the coefficient buffer controller for decompression.
+ * This controller is the top level of the JPEG decompressor proper.
+ * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private buffer controller object */
+
+typedef struct {
+  struct jpeg_d_coef_controller pub; /* public fields */
+
+  JDIMENSION MCU_col_num;	/* saves next MCU column to process */
+  JDIMENSION MCU_row_num;	/* keep track of MCU row # within image */
+
+  /* In single-pass modes without block smoothing, it's sufficient to buffer
+   * just one MCU (although this may prove a bit slow in practice).
+   * We allocate a workspace of MAX_BLOCKS_IN_MCU coefficient blocks,
+   * and let the entropy decoder write into that workspace each time.
+   * (On 80x86, the workspace is FAR even though it's not really very big;
+   * this is to keep the module interfaces unchanged when a large coefficient
+   * buffer is necessary.)
+   * In multi-pass modes, this array points to the current MCU's blocks
+   * within the virtual arrays.
+   */
+  JBLOCKROW MCU_buffer[MAX_BLOCKS_IN_MCU];
+
+  /* In multi-pass modes, we need a virtual block array for each component. */
+  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
+} my_coef_controller;
+
+typedef my_coef_controller * my_coef_ptr;
+
+
+/* Forward declarations */
+METHODDEF boolean decompress_data
+	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+METHODDEF boolean decompress_read
+	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+METHODDEF boolean decompress_output
+	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+#endif
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF void
+start_pass_coef (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+  coef->MCU_col_num = 0;
+  coef->MCU_row_num = 0;
+
+  switch (pass_mode) {
+  case JBUF_PASS_THRU:
+    if (coef->whole_image[0] != NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    coef->pub.decompress_data = decompress_data;
+    break;
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+  case JBUF_SAVE_SOURCE:
+    if (coef->whole_image[0] == NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    coef->pub.decompress_data = decompress_read;
+    break;
+  case JBUF_CRANK_DEST:
+    if (coef->whole_image[0] == NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    coef->pub.decompress_data = decompress_output;
+    break;
+#endif
+  default:
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    break;
+  }
+}
+
+
+/*
+ * Process some data in the single-pass case.
+ * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
+ * Returns TRUE if it completed a row, FALSE if not (suspension).
+ *
+ * NB: output_buf contains a plane for each component in image.
+ * For single pass, this is the same as the components in the scan.
+ */
+
+METHODDEF boolean
+decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
+{
+  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+  JDIMENSION MCU_col_num;	/* index of current MCU within row */
+  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
+  JDIMENSION last_MCU_row = cinfo->MCU_rows_in_scan - 1;
+  int blkn, ci, xindex, yindex, useful_width;
+  JSAMPARRAY output_ptr;
+  JDIMENSION start_col, output_col;
+  jpeg_component_info *compptr;
+  inverse_DCT_method_ptr inverse_DCT;
+
+  /* Loop to process as much as one whole MCU row */
+
+  for (MCU_col_num = coef->MCU_col_num; MCU_col_num <= last_MCU_col;
+       MCU_col_num++) {
+
+    /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
+    jzero_far((void FAR *) coef->MCU_buffer[0],
+	      (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
+    if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
+      /* Suspension forced; return with row unfinished */
+      coef->MCU_col_num = MCU_col_num; /* update my state */
+      return FALSE;
+    }
+
+    /* Determine where data should go in output_buf and do the IDCT thing.
+     * We skip dummy blocks at the right and bottom edges (but blkn gets
+     * incremented past them!).  Note the inner loop relies on having
+     * allocated the MCU_buffer[] blocks sequentially.
+     */
+    blkn = 0;			/* index of current DCT block within MCU */
+    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+      compptr = cinfo->cur_comp_info[ci];
+      /* Don't bother to IDCT an uninteresting component. */
+      if (! compptr->component_needed) {
+	blkn += compptr->MCU_blocks;
+	continue;
+      }
+      inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
+      useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+						  : compptr->last_col_width;
+      output_ptr = output_buf[ci];
+      start_col = MCU_col_num * compptr->MCU_sample_width;
+      for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+	if (coef->MCU_row_num < last_MCU_row ||
+	    yindex < compptr->last_row_height) {
+	  output_col = start_col;
+	  for (xindex = 0; xindex < useful_width; xindex++) {
+	    (*inverse_DCT) (cinfo, compptr,
+			    (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
+			    output_ptr, output_col);
+	    output_col += compptr->DCT_scaled_size;
+	  }
+	}
+	blkn += compptr->MCU_width;
+	output_ptr += compptr->DCT_scaled_size;
+      }
+    }
+  }
+
+  /* We finished the row successfully */
+  coef->MCU_col_num = 0;	/* prepare for next row */
+  coef->MCU_row_num++;
+  return TRUE;
+}
+
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+
+/*
+ * Process some data: handle an input pass for a multiple-scan file.
+ * We read the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the scan.
+ * No data is returned; we just stash it in the virtual arrays.
+ *
+ * Returns TRUE if it completed a row, FALSE if not (suspension).
+ * Currently, the suspension case is not supported.
+ */
+
+METHODDEF boolean
+decompress_read (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
+{
+  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+  JDIMENSION MCU_col_num;	/* index of current MCU within row */
+  int blkn, ci, xindex, yindex, yoffset, num_MCU_rows;
+  JDIMENSION total_width, remaining_rows, start_col;
+  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
+  JBLOCKROW buffer_ptr;
+  jpeg_component_info *compptr;
+
+  /* Align the virtual buffers for the components used in this scan. */
+  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+    compptr = cinfo->cur_comp_info[ci];
+    buffer[ci] = (*cinfo->mem->access_virt_barray)
+      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
+       coef->MCU_row_num * compptr->v_samp_factor, TRUE);
+    /* Entropy decoder expects buffer to be zeroed. */
+    total_width = (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+					 (long) compptr->h_samp_factor);
+    for (yindex = 0; yindex < compptr->v_samp_factor; yindex++) {
+      jzero_far((void FAR *) buffer[ci][yindex], 
+		(size_t) (total_width * SIZEOF(JBLOCK)));
+    }
+  }
+
+  /* In an interleaved scan, we process exactly one MCU row.
+   * In a noninterleaved scan, we need to process v_samp_factor MCU rows,
+   * each of which contains a single block row.
+   */
+  if (cinfo->comps_in_scan == 1) {
+    compptr = cinfo->cur_comp_info[0];
+    num_MCU_rows = compptr->v_samp_factor;
+    /* but watch out for the bottom of the image */
+    remaining_rows = cinfo->MCU_rows_in_scan -
+		     coef->MCU_row_num * compptr->v_samp_factor;
+    if (remaining_rows < (JDIMENSION) num_MCU_rows)
+      num_MCU_rows = (int) remaining_rows;
+  } else {
+    num_MCU_rows = 1;
+  }
+
+  /* Loop to process one whole iMCU row */
+  for (yoffset = 0; yoffset < num_MCU_rows; yoffset++) {
+    for (MCU_col_num = 0; MCU_col_num < cinfo->MCUs_per_row; MCU_col_num++) {
+      /* Construct list of pointers to DCT blocks belonging to this MCU */
+      blkn = 0;			/* index of current DCT block within MCU */
+      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+	compptr = cinfo->cur_comp_info[ci];
+	start_col = MCU_col_num * compptr->MCU_width;
+	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
+	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
+	    coef->MCU_buffer[blkn++] = buffer_ptr++;
+	  }
+	}
+      }
+      /* Try to fetch the MCU. */
+      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
+	ERREXIT(cinfo, JERR_CANT_SUSPEND); /* not supported */
+      }
+    }
+  }
+
+  coef->MCU_row_num++;
+  return TRUE;
+}
+
+
+/*
+ * Process some data: output from the virtual arrays after reading is done.
+ * Always emits one fully interleaved MCU row ("iMCU" row).
+ * Always returns TRUE --- suspension is not possible.
+ *
+ * NB: output_buf contains a plane for each component in image.
+ */
+
+METHODDEF boolean
+decompress_output (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
+{
+  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+  JDIMENSION last_MCU_row = cinfo->total_iMCU_rows - 1;
+  JDIMENSION block_num;
+  int ci, block_row, block_rows;
+  JBLOCKARRAY buffer;
+  JBLOCKROW buffer_ptr;
+  JSAMPARRAY output_ptr;
+  JDIMENSION output_col;
+  jpeg_component_info *compptr;
+  inverse_DCT_method_ptr inverse_DCT;
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    /* Don't bother to IDCT an uninteresting component. */
+    if (! compptr->component_needed)
+      continue;
+    /* Align the virtual buffer for this component. */
+    buffer = (*cinfo->mem->access_virt_barray)
+      ((j_common_ptr) cinfo, coef->whole_image[ci],
+       coef->MCU_row_num * compptr->v_samp_factor, FALSE);
+    /* Count non-dummy DCT block rows in this iMCU row. */
+    if (coef->MCU_row_num < last_MCU_row)
+      block_rows = compptr->v_samp_factor;
+    else {
+      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+      if (block_rows == 0) block_rows = compptr->v_samp_factor;
+    }
+    inverse_DCT = cinfo->idct->inverse_DCT[ci];
+    output_ptr = output_buf[ci];
+    /* Loop over all DCT blocks to be processed. */
+    for (block_row = 0; block_row < block_rows; block_row++) {
+      buffer_ptr = buffer[block_row];
+      output_col = 0;
+      for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
+	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
+			output_ptr, output_col);
+	buffer_ptr++;
+	output_col += compptr->DCT_scaled_size;
+      }
+      output_ptr += compptr->DCT_scaled_size;
+    }
+  }
+
+  coef->MCU_row_num++;
+  return TRUE;
+}
+
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+
+
+/*
+ * Initialize coefficient buffer controller.
+ */
+
+GLOBAL void
+jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
+{
+  my_coef_ptr coef;
+  int ci, i;
+  jpeg_component_info *compptr;
+  JBLOCKROW buffer;
+
+  coef = (my_coef_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_coef_controller));
+  cinfo->coef = (struct jpeg_d_coef_controller *) coef;
+  coef->pub.start_pass = start_pass_coef;
+
+  /* Create the coefficient buffer. */
+  if (need_full_buffer) {
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+    /* Allocate a full-image virtual array for each component, */
+    /* padded to a multiple of samp_factor DCT blocks in each direction. */
+    /* Note memmgr implicitly pads the vertical direction. */
+    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	 ci++, compptr++) {
+      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
+	((j_common_ptr) cinfo, JPOOL_IMAGE,
+	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+				(long) compptr->h_samp_factor),
+	 compptr->height_in_blocks,
+	 (JDIMENSION) compptr->v_samp_factor);
+    }
+#else
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+  } else {
+    /* We only need a single-MCU buffer. */
+    buffer = (JBLOCKROW)
+      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+    for (i = 0; i < MAX_BLOCKS_IN_MCU; i++) {
+      coef->MCU_buffer[i] = buffer + i;
+    }
+    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
+  }
+}
diff --git a/jdcolor.c b/jdcolor.c
index f651c04..d7d0c2a 100644
--- a/jdcolor.c
+++ b/jdcolor.c
@@ -1,16 +1,31 @@
 /*
  * jdcolor.c
  *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains output colorspace conversion routines.
- * These routines are invoked via the methods color_convert
- * and colorout_init/term.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private subobject */
+
+typedef struct {
+  struct jpeg_color_deconverter pub; /* public fields */
+
+  /* Private state for YCC->RGB conversion */
+  int * Cr_r_tab;		/* => table for Cr to R conversion */
+  int * Cb_b_tab;		/* => table for Cb to B conversion */
+  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
+  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
+} my_color_deconverter;
+
+typedef my_color_deconverter * my_cconvert_ptr;
 
 
 /**************** YCbCr -> RGB conversion: most common case **************/
@@ -42,91 +57,90 @@
  * together before rounding.
  */
 
-#ifdef SIXTEEN_BIT_SAMPLES
-#define SCALEBITS	14	/* avoid overflow */
-#else
-#define SCALEBITS	16	/* speedier right-shift on some machines */
-#endif
+#define SCALEBITS	16	/* speediest right-shift on some machines */
 #define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
 #define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
 
-static int * Cr_r_tab;		/* => table for Cr to R conversion */
-static int * Cb_b_tab;		/* => table for Cb to B conversion */
-static INT32 * Cr_g_tab;	/* => table for Cr to G conversion */
-static INT32 * Cb_g_tab;	/* => table for Cb to G conversion */
-
 
 /*
- * Initialize for colorspace conversion.
+ * Initialize for YCC->RGB colorspace conversion.
  */
 
 METHODDEF void
-ycc_rgb_init (decompress_info_ptr cinfo)
+ycc_rgb_start (j_decompress_ptr cinfo)
 {
+  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   INT32 i, x2;
   SHIFT_TEMPS
 
-  Cr_r_tab = (int *) (*cinfo->emethods->alloc_small)
-				((MAXJSAMPLE+1) * SIZEOF(int));
-  Cb_b_tab = (int *) (*cinfo->emethods->alloc_small)
-				((MAXJSAMPLE+1) * SIZEOF(int));
-  Cr_g_tab = (INT32 *) (*cinfo->emethods->alloc_small)
-				((MAXJSAMPLE+1) * SIZEOF(INT32));
-  Cb_g_tab = (INT32 *) (*cinfo->emethods->alloc_small)
-				((MAXJSAMPLE+1) * SIZEOF(INT32));
+  cconvert->Cr_r_tab = (int *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(MAXJSAMPLE+1) * SIZEOF(int));
+  cconvert->Cb_b_tab = (int *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(MAXJSAMPLE+1) * SIZEOF(int));
+  cconvert->Cr_g_tab = (INT32 *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(MAXJSAMPLE+1) * SIZEOF(INT32));
+  cconvert->Cb_g_tab = (INT32 *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(MAXJSAMPLE+1) * SIZEOF(INT32));
 
   for (i = 0; i <= MAXJSAMPLE; i++) {
     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
     /* The Cb or Cr value we are thinking of is x = i - MAXJSAMPLE/2 */
     x2 = 2*i - MAXJSAMPLE;	/* twice x */
     /* Cr=>R value is nearest int to 1.40200 * x */
-    Cr_r_tab[i] = (int)
+    cconvert->Cr_r_tab[i] = (int)
 		    RIGHT_SHIFT(FIX(1.40200/2) * x2 + ONE_HALF, SCALEBITS);
     /* Cb=>B value is nearest int to 1.77200 * x */
-    Cb_b_tab[i] = (int)
+    cconvert->Cb_b_tab[i] = (int)
 		    RIGHT_SHIFT(FIX(1.77200/2) * x2 + ONE_HALF, SCALEBITS);
     /* Cr=>G value is scaled-up -0.71414 * x */
-    Cr_g_tab[i] = (- FIX(0.71414/2)) * x2;
+    cconvert->Cr_g_tab[i] = (- FIX(0.71414/2)) * x2;
     /* Cb=>G value is scaled-up -0.34414 * x */
     /* We also add in ONE_HALF so that need not do it in inner loop */
-    Cb_g_tab[i] = (- FIX(0.34414/2)) * x2 + ONE_HALF;
+    cconvert->Cb_g_tab[i] = (- FIX(0.34414/2)) * x2 + ONE_HALF;
   }
 }
 
 
 /*
  * Convert some rows of samples to the output colorspace.
+ *
+ * Note that we change from noninterleaved, one-plane-per-component format
+ * to interleaved-pixel format.  The output buffer is therefore three times
+ * as wide as the input buffer.
+ * A starting row offset is provided only for the input buffer.  The caller
+ * can easily adjust the passed output_buf value to accommodate any row
+ * offset required on that side.
  */
 
 METHODDEF void
-ycc_rgb_convert (decompress_info_ptr cinfo, int num_rows, long num_cols,
-		 JSAMPIMAGE input_data, JSAMPIMAGE output_data)
+ycc_rgb_convert (j_decompress_ptr cinfo,
+		 JSAMPIMAGE input_buf, JDIMENSION input_row,
+		 JSAMPARRAY output_buf, int num_rows)
 {
-#ifdef SIXTEEN_BIT_SAMPLES
-  register INT32 y;
-  register UINT16 cb, cr;
-#else
+  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   register int y, cb, cr;
-#endif
+  register JSAMPROW outptr;
   register JSAMPROW inptr0, inptr1, inptr2;
-  register JSAMPROW outptr0, outptr1, outptr2;
-  register long col;
+  register JDIMENSION col;
+  JDIMENSION num_cols = cinfo->output_width;
   /* copy these pointers into registers if possible */
   register JSAMPLE * range_limit = cinfo->sample_range_limit;
-  register int * Crrtab = Cr_r_tab;
-  register int * Cbbtab = Cb_b_tab;
-  register INT32 * Crgtab = Cr_g_tab;
-  register INT32 * Cbgtab = Cb_g_tab;
-  int row;
+  register int * Crrtab = cconvert->Cr_r_tab;
+  register int * Cbbtab = cconvert->Cb_b_tab;
+  register INT32 * Crgtab = cconvert->Cr_g_tab;
+  register INT32 * Cbgtab = cconvert->Cb_g_tab;
   SHIFT_TEMPS
-  
-  for (row = 0; row < num_rows; row++) {
-    inptr0 = input_data[0][row];
-    inptr1 = input_data[1][row];
-    inptr2 = input_data[2][row];
-    outptr0 = output_data[0][row];
-    outptr1 = output_data[1][row];
-    outptr2 = output_data[2][row];
+
+  while (--num_rows >= 0) {
+    inptr0 = input_buf[0][input_row];
+    inptr1 = input_buf[1][input_row];
+    inptr2 = input_buf[2][input_row];
+    input_row++;
+    outptr = *output_buf++;
     for (col = 0; col < num_cols; col++) {
       y  = GETJSAMPLE(inptr0[col]);
       cb = GETJSAMPLE(inptr1[col]);
@@ -135,168 +149,226 @@
        * range-limiting would be unnecessary here; but due to possible
        * noise in the DCT/IDCT phase, we do need to apply range limits.
        */
-      outptr0[col] = range_limit[y + Crrtab[cr]];	/* red */
-      outptr1[col] = range_limit[y +			/* green */
-				 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
-						    SCALEBITS))];
-      outptr2[col] = range_limit[y + Cbbtab[cb]];	/* blue */
+      outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
+      outptr[RGB_GREEN] = range_limit[y +
+			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
+						 SCALEBITS))];
+      outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
+      outptr += RGB_PIXELSIZE;
     }
   }
 }
 
 
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-ycc_rgb_term (decompress_info_ptr cinfo)
-{
-  /* no work (we let free_all release the workspace) */
-}
-
-
 /**************** Cases other than YCbCr -> RGB **************/
 
 
 /*
- * Initialize for colorspace conversion.
+ * Color conversion for no colorspace change: just copy the data,
+ * converting from separate-planes to interleaved representation.
  */
 
 METHODDEF void
-null_init (decompress_info_ptr cinfo)
-/* colorout_init for cases where no setup is needed */
+null_convert (j_decompress_ptr cinfo,
+	      JSAMPIMAGE input_buf, JDIMENSION input_row,
+	      JSAMPARRAY output_buf, int num_rows)
 {
-  /* no work needed */
-}
+  register JSAMPROW inptr, outptr;
+  register JDIMENSION count;
+  register int num_components = cinfo->output_components;
+  JDIMENSION num_cols = cinfo->output_width;
+  int ci;
 
-
-/*
- * Color conversion for no colorspace change: just copy the data.
- */
-
-METHODDEF void
-null_convert (decompress_info_ptr cinfo, int num_rows, long num_cols,
-	      JSAMPIMAGE input_data, JSAMPIMAGE output_data)
-{
-  short ci;
-
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    jcopy_sample_rows(input_data[ci], 0, output_data[ci], 0,
-		      num_rows, num_cols);
+  while (--num_rows >= 0) {
+    for (ci = 0; ci < num_components; ci++) {
+      inptr = input_buf[ci][input_row];
+      outptr = output_buf[0] + ci;
+      for (count = num_cols; count > 0; count--) {
+	*outptr = *inptr++;	/* needn't bother with GETJSAMPLE() here */
+	outptr += num_components;
+      }
+    }
+    input_row++;
+    output_buf++;
   }
 }
 
 
 /*
  * Color conversion for grayscale: just copy the data.
- * This also works for YCbCr/YIQ -> grayscale conversion, in which
+ * This also works for YCbCr -> grayscale conversion, in which
  * we just copy the Y (luminance) component and ignore chrominance.
  */
 
 METHODDEF void
-grayscale_convert (decompress_info_ptr cinfo, int num_rows, long num_cols,
-		   JSAMPIMAGE input_data, JSAMPIMAGE output_data)
+grayscale_convert (j_decompress_ptr cinfo,
+		   JSAMPIMAGE input_buf, JDIMENSION input_row,
+		   JSAMPARRAY output_buf, int num_rows)
 {
-  jcopy_sample_rows(input_data[0], 0, output_data[0], 0,
-		    num_rows, num_cols);
+  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
+		    num_rows, cinfo->output_width);
 }
 
 
 /*
- * Finish up at the end of the file.
+ * Adobe-style YCCK->CMYK conversion.
+ * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
+ * conversion as above, while passing K (black) unchanged.
+ * We assume ycc_rgb_start has been called.
  */
 
 METHODDEF void
-null_term (decompress_info_ptr cinfo)
-/* colorout_term for cases where no teardown is needed */
+ycck_cmyk_convert (j_decompress_ptr cinfo,
+		   JSAMPIMAGE input_buf, JDIMENSION input_row,
+		   JSAMPARRAY output_buf, int num_rows)
+{
+  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+  register int y, cb, cr;
+  register JSAMPROW outptr;
+  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
+  register JDIMENSION col;
+  JDIMENSION num_cols = cinfo->output_width;
+  /* copy these pointers into registers if possible */
+  register JSAMPLE * range_limit = cinfo->sample_range_limit;
+  register int * Crrtab = cconvert->Cr_r_tab;
+  register int * Cbbtab = cconvert->Cb_b_tab;
+  register INT32 * Crgtab = cconvert->Cr_g_tab;
+  register INT32 * Cbgtab = cconvert->Cb_g_tab;
+  SHIFT_TEMPS
+
+  while (--num_rows >= 0) {
+    inptr0 = input_buf[0][input_row];
+    inptr1 = input_buf[1][input_row];
+    inptr2 = input_buf[2][input_row];
+    inptr3 = input_buf[3][input_row];
+    input_row++;
+    outptr = *output_buf++;
+    for (col = 0; col < num_cols; col++) {
+      y  = GETJSAMPLE(inptr0[col]);
+      cb = GETJSAMPLE(inptr1[col]);
+      cr = GETJSAMPLE(inptr2[col]);
+      /* Note: if the inputs were computed directly from RGB values,
+       * range-limiting would be unnecessary here; but due to possible
+       * noise in the DCT/IDCT phase, we do need to apply range limits.
+       */
+      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];	/* red */
+      outptr[1] = range_limit[MAXJSAMPLE - (y +			/* green */
+			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
+						 SCALEBITS)))];
+      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];	/* blue */
+      /* K passes through unchanged */
+      outptr[3] = inptr3[col];	/* don't need GETJSAMPLE here */
+      outptr += 4;
+    }
+  }
+}
+
+
+/*
+ * Empty method for start_pass.
+ */
+
+METHODDEF void
+null_method (j_decompress_ptr cinfo)
 {
   /* no work needed */
 }
 
 
-
 /*
- * The method selection routine for output colorspace conversion.
+ * Module initialization routine for output colorspace conversion.
  */
 
 GLOBAL void
-jseldcolor (decompress_info_ptr cinfo)
+jinit_color_deconverter (j_decompress_ptr cinfo)
 {
+  my_cconvert_ptr cconvert;
   int ci;
 
+  cconvert = (my_cconvert_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_color_deconverter));
+  cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
+  /* set start_pass to null method until we find out differently */
+  cconvert->pub.start_pass = null_method;
+
   /* Make sure num_components agrees with jpeg_color_space */
   switch (cinfo->jpeg_color_space) {
-  case CS_GRAYSCALE:
+  case JCS_GRAYSCALE:
     if (cinfo->num_components != 1)
-      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
     break;
 
-  case CS_RGB:
-  case CS_YCbCr:
-  case CS_YIQ:
+  case JCS_RGB:
+  case JCS_YCbCr:
     if (cinfo->num_components != 3)
-      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
     break;
 
-  case CS_CMYK:
+  case JCS_CMYK:
+  case JCS_YCCK:
     if (cinfo->num_components != 4)
-      ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
     break;
 
-  default:
-    ERREXIT(cinfo->emethods, "Unsupported JPEG colorspace");
+  default:			/* JCS_UNKNOWN can be anything */
+    if (cinfo->num_components < 1)
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
     break;
   }
 
-  /* Set color_out_comps and conversion method based on requested space. */
-  /* Also clear the component_needed flags for any unused components, */
-  /* so that earlier pipeline stages can avoid useless computation. */
+  /* Set out_color_components and conversion method based on requested space.
+   * Also clear the component_needed flags for any unused components,
+   * so that earlier pipeline stages can avoid useless computation.
+   */
 
   switch (cinfo->out_color_space) {
-  case CS_GRAYSCALE:
-    cinfo->color_out_comps = 1;
-    if (cinfo->jpeg_color_space == CS_GRAYSCALE ||
-	cinfo->jpeg_color_space == CS_YCbCr ||
-	cinfo->jpeg_color_space == CS_YIQ) {
-      cinfo->methods->color_convert = grayscale_convert;
-      cinfo->methods->colorout_init = null_init;
-      cinfo->methods->colorout_term = null_term;
+  case JCS_GRAYSCALE:
+    cinfo->out_color_components = 1;
+    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
+	cinfo->jpeg_color_space == JCS_YCbCr) {
+      cconvert->pub.color_convert = grayscale_convert;
       /* For color->grayscale conversion, only the Y (0) component is needed */
       for (ci = 1; ci < cinfo->num_components; ci++)
-	cinfo->cur_comp_info[ci]->component_needed = FALSE;
+	cinfo->comp_info[ci].component_needed = FALSE;
     } else
-      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
     break;
 
-  case CS_RGB:
-    cinfo->color_out_comps = 3;
-    if (cinfo->jpeg_color_space == CS_YCbCr) {
-      cinfo->methods->color_convert = ycc_rgb_convert;
-      cinfo->methods->colorout_init = ycc_rgb_init;
-      cinfo->methods->colorout_term = ycc_rgb_term;
-    } else if (cinfo->jpeg_color_space == CS_RGB) {
-      cinfo->methods->color_convert = null_convert;
-      cinfo->methods->colorout_init = null_init;
-      cinfo->methods->colorout_term = null_term;
+  case JCS_RGB:
+    cinfo->out_color_components = RGB_PIXELSIZE;
+    if (cinfo->jpeg_color_space == JCS_YCbCr) {
+      cconvert->pub.start_pass = ycc_rgb_start;
+      cconvert->pub.color_convert = ycc_rgb_convert;
+    } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
+      cconvert->pub.color_convert = null_convert;
     } else
-      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    break;
+
+  case JCS_CMYK:
+    cinfo->out_color_components = 4;
+    if (cinfo->jpeg_color_space == JCS_YCCK) {
+      cconvert->pub.start_pass = ycc_rgb_start;
+      cconvert->pub.color_convert = ycck_cmyk_convert;
+    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
+      cconvert->pub.color_convert = null_convert;
+    } else
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
     break;
 
   default:
-    /* Permit null conversion from CMYK or YCbCr to same output space */
+    /* Permit null conversion to same output space */
     if (cinfo->out_color_space == cinfo->jpeg_color_space) {
-      cinfo->color_out_comps = cinfo->num_components;
-      cinfo->methods->color_convert = null_convert;
-      cinfo->methods->colorout_init = null_init;
-      cinfo->methods->colorout_term = null_term;
+      cinfo->out_color_components = cinfo->num_components;
+      cconvert->pub.color_convert = null_convert;
     } else			/* unsupported non-null conversion */
-      ERREXIT(cinfo->emethods, "Unsupported color conversion request");
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
     break;
   }
 
   if (cinfo->quantize_colors)
-    cinfo->final_out_comps = 1;	/* single colormapped output component */
+    cinfo->output_components = 1; /* single colormapped output component */
   else
-    cinfo->final_out_comps = cinfo->color_out_comps;
+    cinfo->output_components = cinfo->out_color_components;
 }
diff --git a/jdct.h b/jdct.h
new file mode 100644
index 0000000..3ce790b
--- /dev/null
+++ b/jdct.h
@@ -0,0 +1,176 @@
+/*
+ * jdct.h
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This include file contains common declarations for the forward and
+ * inverse DCT modules.  These declarations are private to the DCT managers
+ * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms.
+ * The individual DCT algorithms are kept in separate files to ease 
+ * machine-dependent tuning (e.g., assembly coding).
+ */
+
+
+/*
+ * A forward DCT routine is given a pointer to a work area of type DCTELEM[];
+ * the DCT is to be performed in-place in that buffer.  Type DCTELEM is int
+ * for 8-bit samples, INT32 for 12-bit samples.  (NOTE: Floating-point DCT
+ * implementations use an array of type FAST_FLOAT, instead.)
+ * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE).
+ * The DCT outputs are returned scaled up by a factor of 8; they therefore
+ * have a range of +-8K for 8-bit data, +-128K for 12-bit data.  This
+ * convention improves accuracy in integer implementations and saves some
+ * work in floating-point ones.
+ * Quantization of the output coefficients is done by jcdctmgr.c.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+typedef int DCTELEM;		/* 16 or 32 bits is fine */
+#else
+typedef INT32 DCTELEM;		/* must have 32 bits */
+#endif
+
+typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data));
+typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data));
+
+
+/*
+ * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer
+ * to an output sample array.  The routine must dequantize the input data as
+ * well as perform the IDCT; for dequantization, it uses the multiplier table
+ * pointed to by compptr->dct_table.  The output data is to be placed into the
+ * sample array starting at a specified column.  (Any row offset needed will
+ * be applied to the array pointer before it is passed to the IDCT code.)
+ * Note that the number of samples emitted by the IDCT routine is
+ * DCT_scaled_size * DCT_scaled_size.
+ */
+
+/* typedef inverse_DCT_method_ptr is declared in jpegint.h */
+
+/*
+ * Each IDCT routine has its own ideas about the best dct_table element type.
+ */
+
+typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
+#if BITS_IN_JSAMPLE == 8
+typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
+#define IFAST_SCALE_BITS  2	/* fractional bits in scale factors */
+#else
+typedef INT32 IFAST_MULT_TYPE;	/* need 32 bits for scaled quantizers */
+#define IFAST_SCALE_BITS  13	/* fractional bits in scale factors */
+#endif
+typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
+
+
+/*
+ * Each IDCT routine is responsible for range-limiting its results and
+ * converting them to unsigned form (0..MAXJSAMPLE).  The raw outputs could
+ * be quite far out of range if the input data is corrupt, so a bulletproof
+ * range-limiting step is required.  We use a mask-and-table-lookup method
+ * to do the combined operations quickly.  See the comments with
+ * prepare_range_limit_table (in jdmaster.c) for more info.
+ */
+
+#define IDCT_range_limit(cinfo)  ((cinfo)->sample_range_limit + CENTERJSAMPLE)
+
+#define RANGE_MASK  (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_fdct_islow		jFDislow
+#define jpeg_fdct_ifast		jFDifast
+#define jpeg_fdct_float		jFDfloat
+#define jpeg_idct_islow		jRDislow
+#define jpeg_idct_ifast		jRDifast
+#define jpeg_idct_float		jRDfloat
+#define jpeg_idct_4x4		jRD4x4
+#define jpeg_idct_2x2		jRD2x2
+#define jpeg_idct_1x1		jRD1x1
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+/* Extern declarations for the forward and inverse DCT routines. */
+
+EXTERN void jpeg_fdct_islow JPP((DCTELEM * data));
+EXTERN void jpeg_fdct_ifast JPP((DCTELEM * data));
+EXTERN void jpeg_fdct_float JPP((FAST_FLOAT * data));
+
+EXTERN void jpeg_idct_islow
+    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN void jpeg_idct_ifast
+    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN void jpeg_idct_float
+    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN void jpeg_idct_4x4
+    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN void jpeg_idct_2x2
+    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN void jpeg_idct_1x1
+    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+
+
+/*
+ * Macros for handling fixed-point arithmetic; these are used by many
+ * but not all of the DCT/IDCT modules.
+ *
+ * All values are expected to be of type INT32.
+ * Fractional constants are scaled left by CONST_BITS bits.
+ * CONST_BITS is defined within each module using these macros,
+ * and may differ from one module to the next.
+ */
+
+#define ONE	((INT32) 1)
+#define CONST_SCALE (ONE << CONST_BITS)
+
+/* Convert a positive real constant to an integer scaled by CONST_SCALE.
+ * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
+ * thus causing a lot of useless floating-point operations at run time.
+ */
+
+#define FIX(x)	((INT32) ((x) * CONST_SCALE + 0.5))
+
+/* Descale and correctly round an INT32 value that's scaled by N bits.
+ * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
+ * the fudge factor is correct for either sign of X.
+ */
+
+#define DESCALE(x,n)  RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
+
+/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
+ * This macro is used only when the two inputs will actually be no more than
+ * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a
+ * full 32x32 multiply.  This provides a useful speedup on many machines.
+ * Unfortunately there is no way to specify a 16x16->32 multiply portably
+ * in C, but some C compilers will do the right thing if you provide the
+ * correct combination of casts.
+ */
+
+#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
+#define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT16) (const)))
+#endif
+#ifdef SHORTxLCONST_32		/* known to work with Microsoft C 6.0 */
+#define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT32) (const)))
+#endif
+
+#ifndef MULTIPLY16C16		/* default definition */
+#define MULTIPLY16C16(var,const)  ((var) * (const))
+#endif
+
+/* Same except both inputs are variables. */
+
+#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
+#define MULTIPLY16V16(var1,var2)  (((INT16) (var1)) * ((INT16) (var2)))
+#endif
+
+#ifndef MULTIPLY16V16		/* default definition */
+#define MULTIPLY16V16(var1,var2)  ((var1) * (var2))
+#endif
diff --git a/jddctmgr.c b/jddctmgr.c
new file mode 100644
index 0000000..0dd7716
--- /dev/null
+++ b/jddctmgr.c
@@ -0,0 +1,282 @@
+/*
+ * jddctmgr.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the inverse-DCT management logic.
+ * This code selects a particular IDCT implementation to be used,
+ * and it performs related housekeeping chores.  No code in this file
+ * is executed per IDCT step, only during pass setup.
+ *
+ * Note that the IDCT routines are responsible for performing coefficient
+ * dequantization as well as the IDCT proper.  This module sets up the
+ * dequantization multiplier table needed by the IDCT routine.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h"		/* Private declarations for DCT subsystem */
+
+
+/* Private subobject for this module */
+
+typedef struct {
+  struct jpeg_inverse_dct pub;	/* public fields */
+
+  /* Record the IDCT method type actually selected for each component */
+  J_DCT_METHOD real_method[MAX_COMPONENTS];
+} my_idct_controller;
+
+typedef my_idct_controller * my_idct_ptr;
+
+
+/* ZIG[i] is the zigzag-order position of the i'th element of a DCT block */
+/* read in natural order (left to right, top to bottom). */
+static const int ZIG[DCTSIZE2] = {
+     0,  1,  5,  6, 14, 15, 27, 28,
+     2,  4,  7, 13, 16, 26, 29, 42,
+     3,  8, 12, 17, 25, 30, 41, 43,
+     9, 11, 18, 24, 31, 40, 44, 53,
+    10, 19, 23, 32, 39, 45, 52, 54,
+    20, 22, 33, 38, 46, 51, 55, 60,
+    21, 34, 37, 47, 50, 56, 59, 61,
+    35, 36, 48, 49, 57, 58, 62, 63
+};
+
+
+/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
+ * so be sure to compile that code if either ISLOW or SCALING is requested.
+ */
+#ifdef DCT_ISLOW_SUPPORTED
+#define PROVIDE_ISLOW_TABLES
+#else
+#ifdef IDCT_SCALING_SUPPORTED
+#define PROVIDE_ISLOW_TABLES
+#endif
+#endif
+
+
+/*
+ * Initialize for an input scan.
+ *
+ * Verify that all referenced Q-tables are present, and set up
+ * the multiplier table for each one.
+ * With a multiple-scan JPEG file, this is called during each input scan,
+ * NOT during the final output pass where the IDCT is actually done.
+ * The purpose is to save away the current Q-table contents just in case
+ * the encoder changes tables between scans.  This decoder will dequantize
+ * any component using the Q-table which was current at the start of the
+ * first scan using that component.
+ */
+
+METHODDEF void
+start_input_pass (j_decompress_ptr cinfo)
+{
+  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
+  int ci, qtblno, i;
+  jpeg_component_info *compptr;
+  JQUANT_TBL * qtbl;
+
+  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+    compptr = cinfo->cur_comp_info[ci];
+    qtblno = compptr->quant_tbl_no;
+    /* Make sure specified quantization table is present */
+    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
+	cinfo->quant_tbl_ptrs[qtblno] == NULL)
+      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
+    qtbl = cinfo->quant_tbl_ptrs[qtblno];
+    /* Create multiplier table from quant table, unless we already did so. */
+    if (compptr->dct_table != NULL)
+      continue;
+    switch (idct->real_method[compptr->component_index]) {
+#ifdef PROVIDE_ISLOW_TABLES
+    case JDCT_ISLOW:
+      {
+	/* For LL&M IDCT method, multipliers are equal to raw quantization
+	 * coefficients, but are stored in natural order as ints.
+	 */
+	ISLOW_MULT_TYPE * ismtbl;
+	compptr->dct_table =
+	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				      DCTSIZE2 * SIZEOF(ISLOW_MULT_TYPE));
+	ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
+	for (i = 0; i < DCTSIZE2; i++) {
+	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[ZIG[i]];
+	}
+      }
+      break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+    case JDCT_IFAST:
+      {
+	/* For AA&N IDCT method, multipliers are equal to quantization
+	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
+	 *   scalefactor[0] = 1
+	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
+	 * For integer operation, the multiplier table is to be scaled by
+	 * IFAST_SCALE_BITS.  The multipliers are stored in natural order.
+	 */
+	IFAST_MULT_TYPE * ifmtbl;
+#define CONST_BITS 14
+	static const INT16 aanscales[DCTSIZE2] = {
+	  /* precomputed values scaled up by 14 bits */
+	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
+	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
+	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
+	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
+	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
+	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
+	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
+	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
+	};
+	SHIFT_TEMPS
+
+	compptr->dct_table =
+	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				      DCTSIZE2 * SIZEOF(IFAST_MULT_TYPE));
+	ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
+	for (i = 0; i < DCTSIZE2; i++) {
+	  ifmtbl[i] = (IFAST_MULT_TYPE)
+	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[ZIG[i]],
+				  (INT32) aanscales[i]),
+		    CONST_BITS-IFAST_SCALE_BITS);
+	}
+      }
+      break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+    case JDCT_FLOAT:
+      {
+	/* For float AA&N IDCT method, multipliers are equal to quantization
+	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
+	 *   scalefactor[0] = 1
+	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
+	 * The multipliers are stored in natural order.
+	 */
+	FLOAT_MULT_TYPE * fmtbl;
+	int row, col;
+	static const double aanscalefactor[DCTSIZE] = {
+	  1.0, 1.387039845, 1.306562965, 1.175875602,
+	  1.0, 0.785694958, 0.541196100, 0.275899379
+	};
+
+	compptr->dct_table =
+	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				      DCTSIZE2 * SIZEOF(FLOAT_MULT_TYPE));
+	fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
+	i = 0;
+	for (row = 0; row < DCTSIZE; row++) {
+	  for (col = 0; col < DCTSIZE; col++) {
+	    fmtbl[i] = (FLOAT_MULT_TYPE)
+	      ((double) qtbl->quantval[ZIG[i]] *
+	       aanscalefactor[row] * aanscalefactor[col]);
+	    i++;
+	  }
+	}
+      }
+      break;
+#endif
+    default:
+      ERREXIT(cinfo, JERR_NOT_COMPILED);
+      break;
+    }
+  }
+}
+
+
+/*
+ * Prepare for an output pass that will actually perform IDCTs.
+ *
+ * start_input_pass should already have been done for all components
+ * of interest; we need only verify that this is true.
+ * Note that uninteresting components are not required to have loaded tables.
+ * This allows the master controller to stop before reading the whole file
+ * if it has obtained the data for the interesting component(s).
+ */
+
+METHODDEF void
+start_output_pass (j_decompress_ptr cinfo)
+{
+  jpeg_component_info *compptr;
+  int ci;
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    if (! compptr->component_needed)
+      continue;
+    if (compptr->dct_table == NULL)
+      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, compptr->quant_tbl_no);
+  }
+}
+
+
+/*
+ * Initialize IDCT manager.
+ */
+
+GLOBAL void
+jinit_inverse_dct (j_decompress_ptr cinfo)
+{
+  my_idct_ptr idct;
+  int ci;
+  jpeg_component_info *compptr;
+
+  idct = (my_idct_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_idct_controller));
+  cinfo->idct = (struct jpeg_inverse_dct *) idct;
+  idct->pub.start_input_pass = start_input_pass;
+  idct->pub.start_output_pass = start_output_pass;
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    compptr->dct_table = NULL;	/* initialize tables to "not prepared" */
+    switch (compptr->DCT_scaled_size) {
+#ifdef IDCT_SCALING_SUPPORTED
+    case 1:
+      idct->pub.inverse_DCT[ci] = jpeg_idct_1x1;
+      idct->real_method[ci] = JDCT_ISLOW; /* jidctred uses islow-style table */
+      break;
+    case 2:
+      idct->pub.inverse_DCT[ci] = jpeg_idct_2x2;
+      idct->real_method[ci] = JDCT_ISLOW; /* jidctred uses islow-style table */
+      break;
+    case 4:
+      idct->pub.inverse_DCT[ci] = jpeg_idct_4x4;
+      idct->real_method[ci] = JDCT_ISLOW; /* jidctred uses islow-style table */
+      break;
+#endif
+    case DCTSIZE:
+      switch (cinfo->dct_method) {
+#ifdef DCT_ISLOW_SUPPORTED
+      case JDCT_ISLOW:
+	idct->pub.inverse_DCT[ci] = jpeg_idct_islow;
+	idct->real_method[ci] = JDCT_ISLOW;
+	break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+      case JDCT_IFAST:
+	idct->pub.inverse_DCT[ci] = jpeg_idct_ifast;
+	idct->real_method[ci] = JDCT_IFAST;
+	break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+      case JDCT_FLOAT:
+	idct->pub.inverse_DCT[ci] = jpeg_idct_float;
+	idct->real_method[ci] = JDCT_FLOAT;
+	break;
+#endif
+      default:
+	ERREXIT(cinfo, JERR_NOT_COMPILED);
+	break;
+      }
+      break;
+    default:
+      ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
+      break;
+    }
+  }
+}
diff --git a/jddeflts.c b/jddeflts.c
deleted file mode 100644
index b8f1229..0000000
--- a/jddeflts.c
+++ /dev/null
@@ -1,185 +0,0 @@
-/*
- * jddeflts.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains optional default-setting code for the JPEG decompressor.
- * User interfaces do not have to use this file, but those that don't use it
- * must know more about the innards of the JPEG code.
- */
-
-#include "jinclude.h"
-
-
-/* Default do-nothing progress monitoring routine.
- * This can be overridden by a user interface that wishes to
- * provide progress monitoring; just set methods->progress_monitor
- * after j_d_defaults is done.  The routine will be called periodically
- * during the decompression process.
- *
- * During any one pass, loopcounter increases from 0 up to (not including)
- * looplimit; the step size is not necessarily 1.  Both the step size and
- * the limit may differ between passes.  The expected total number of passes
- * is in cinfo->total_passes, and the number of passes already completed is
- * in cinfo->completed_passes.  Thus the fraction of work completed may be
- * estimated as
- *		completed_passes + (loopcounter/looplimit)
- *		------------------------------------------
- *				total_passes
- * ignoring the fact that the passes may not be equal amounts of work.
- *
- * When decompressing, the total_passes figure is an estimate that may be
- * on the high side; completed_passes will jump by more than one if some
- * passes are skipped.
- */
-
-METHODDEF void
-progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
-{
-  /* do nothing */
-}
-
-
-/* Default comment-block processing routine.
- * This can be overridden by an application that wishes to examine
- * COM blocks found in the JPEG file.  The default routine does nothing.
- * CAUTION: the comment processing routine MUST call JGETC() exactly
- * comment_length times to read the comment data, whether it intends
- * to do anything with the data or not!
- * Keep in mind that (a) there may be more than one COM block in a file;
- * (b) there's no guarantee that what's in the block is ASCII data.
- */
-
-METHODDEF void
-process_comment (decompress_info_ptr cinfo, long comment_length)
-{
-  while (comment_length-- > 0) {
-    (void) JGETC(cinfo);
-  }
-}
-
-
-/*
- * Reload the input buffer after it's been emptied, and return the next byte.
- * See the JGETC macro for calling conditions.  Note in particular that
- * read_jpeg_data may NOT return EOF.  If no more data is available, it must
- * exit via ERREXIT, or perhaps synthesize fake data (such as an RST marker).
- * In the present implementation, we insert an EOI marker; this might not be
- * appropriate for non-JFIF file formats, but it usually allows us to handle
- * a truncated JFIF file.
- *
- * This routine can be overridden by the system-dependent user interface,
- * in case the data source is not a stdio stream or some other special
- * condition applies.  Note, however, that this capability only applies for
- * JFIF or similar serial-access JPEG file formats.  The input file control
- * module for a random-access format such as TIFF/JPEG would most likely
- * override the read_jpeg_data method with its own routine.
- */
-
-METHODDEF int
-read_jpeg_data (decompress_info_ptr cinfo)
-{
-  cinfo->next_input_byte = cinfo->input_buffer + MIN_UNGET;
-
-  cinfo->bytes_in_buffer = (int) JFREAD(cinfo->input_file,
-					cinfo->next_input_byte,
-					JPEG_BUF_SIZE);
-  
-  if (cinfo->bytes_in_buffer <= 0) {
-    WARNMS(cinfo->emethods, "Premature EOF in JPEG file");
-    cinfo->next_input_byte[0] = (char) 0xFF;
-    cinfo->next_input_byte[1] = (char) 0xD9; /* EOI marker */
-    cinfo->bytes_in_buffer = 2;
-  }
-
-  return JGETC(cinfo);
-}
-
-
-
-/* Default parameter setup for decompression.
- *
- * User interfaces that don't choose to use this routine must do their
- * own setup of all these parameters.  Alternately, you can call this
- * to establish defaults and then alter parameters selectively.  This
- * is the recommended approach since, if we add any new parameters,
- * your code will still work (they'll be set to reasonable defaults).
- *
- * standard_buffering should be TRUE to cause an input buffer to be allocated
- * (the normal case); if FALSE, the user interface must provide a buffer.
- * This option is most useful in the case that the buffer must not be freed
- * at the end of an image.  (For example, when reading a sequence of images
- * from a single file, the remaining data in the buffer represents the
- * start of the next image and mustn't be discarded.)  To handle this,
- * allocate the input buffer yourself at startup, WITHOUT using alloc_small
- * (probably a direct call to malloc() instead).  Then pass FALSE on each
- * call to j_d_defaults to ensure the buffer state is not modified.
- *
- * If the source of the JPEG data is not a stdio stream, override the
- * read_jpeg_data method with your own routine after calling j_d_defaults.
- * You can still use the standard buffer if it's appropriate.
- *
- * CAUTION: if you want to decompress multiple images per run, it's necessary
- * to call j_d_defaults before *each* call to jpeg_decompress, since subsidiary
- * structures like the quantization tables are automatically freed during
- * cleanup.
- */
-
-GLOBAL void
-j_d_defaults (decompress_info_ptr cinfo, boolean standard_buffering)
-/* NB: the external methods must already be set up. */
-{
-  short i;
-
-  /* Initialize pointers as needed to mark stuff unallocated. */
-  /* Outer application may fill in default tables for abbreviated files... */
-  cinfo->comp_info = NULL;
-  for (i = 0; i < NUM_QUANT_TBLS; i++)
-    cinfo->quant_tbl_ptrs[i] = NULL;
-  for (i = 0; i < NUM_HUFF_TBLS; i++) {
-    cinfo->dc_huff_tbl_ptrs[i] = NULL;
-    cinfo->ac_huff_tbl_ptrs[i] = NULL;
-  }
-  cinfo->colormap = NULL;
-
-  /* Default to RGB output */
-  /* UI can override by changing out_color_space */
-  cinfo->out_color_space = CS_RGB;
-  cinfo->jpeg_color_space = CS_UNKNOWN;
-  /* Setting any other value in jpeg_color_space overrides heuristics in */
-  /* jrdjfif.c.  That might be useful when reading non-JFIF JPEG files, */
-  /* but ordinarily the UI shouldn't change it. */
-  
-  /* Default to no gamma correction of output */
-  cinfo->output_gamma = 1.0;
-  
-  /* Default to no color quantization */
-  cinfo->quantize_colors = FALSE;
-  /* but set reasonable default parameters for quantization, */
-  /* so that turning on quantize_colors is sufficient to do something useful */
-  cinfo->two_pass_quantize = TRUE;
-  cinfo->use_dithering = TRUE;
-  cinfo->desired_number_of_colors = 256;
-  
-  /* Default to no smoothing */
-  cinfo->do_block_smoothing = FALSE;
-  cinfo->do_pixel_smoothing = FALSE;
-  
-  /* Allocate memory for input buffer, unless outer application provides it. */
-  if (standard_buffering) {
-    cinfo->input_buffer = (char *) (*cinfo->emethods->alloc_small)
-					((size_t) (JPEG_BUF_SIZE + MIN_UNGET));
-    cinfo->bytes_in_buffer = 0;	/* initialize buffer to empty */
-  }
-
-  /* Install standard buffer-reloading method (outer code may override). */
-  cinfo->methods->read_jpeg_data = read_jpeg_data;
-
-  /* Install default do-nothing progress monitoring method. */
-  cinfo->methods->progress_monitor = progress_monitor;
-
-  /* Install default comment-block processing method. */
-  cinfo->methods->process_comment = process_comment;
-}
diff --git a/jdhuff.c b/jdhuff.c
index 3ac46cf..e92ad9a 100644
--- a/jdhuff.c
+++ b/jdhuff.c
@@ -1,35 +1,172 @@
 /*
  * jdhuff.c
  *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains Huffman entropy decoding routines.
- * These routines are invoked via the methods entropy_decode
- * and entropy_decode_init/term.
+ *
+ * Much of the complexity here has to do with supporting input suspension.
+ * If the data source module demands suspension, we want to be able to back
+ * up to the start of the current MCU.  To do this, we copy state variables
+ * into local working storage, and update them back to the permanent JPEG
+ * objects only upon successful completion of an MCU.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
 
 
-/* Static variables to avoid passing 'round extra parameters */
+/* Derived data constructed for each Huffman table */
 
-static decompress_info_ptr dcinfo;
+#define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */
 
-static INT32 get_buffer;	/* current bit-extraction buffer */
-static int bits_left;		/* # of unused bits in it */
-static boolean printed_eod;	/* flag to suppress multiple end-of-data msgs */
+typedef struct {
+  /* Basic tables: (element [0] of each array is unused) */
+  INT32 mincode[17];		/* smallest code of length k */
+  INT32 maxcode[18];		/* largest code of length k (-1 if none) */
+  /* (maxcode[17] is a sentinel to ensure huff_DECODE terminates) */
+  int valptr[17];		/* huffval[] index of 1st symbol of length k */
+
+  /* Back link to public Huffman table (needed only in slow_DECODE) */
+  JHUFF_TBL *pub;
+
+  /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
+   * the input data stream.  If the next Huffman code is no more
+   * than HUFF_LOOKAHEAD bits long, we can obtain its length and
+   * the corresponding symbol directly from these tables.
+   */
+  int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
+  UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
+} D_DERIVED_TBL;
+
+/* Expanded entropy decoder object for Huffman decoding.
+ *
+ * The savable_state subrecord contains fields that change within an MCU,
+ * but must not be updated permanently until we complete the MCU.
+ */
+
+typedef struct {
+  INT32 get_buffer;		/* current bit-extraction buffer */
+  int bits_left;		/* # of unused bits in it */
+  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
+} savable_state;
+
+/* This macro is to work around compilers with missing or broken
+ * structure assignment.  You'll need to fix this code if you have
+ * such a compiler and you change MAX_COMPS_IN_SCAN.
+ */
+
+#ifndef NO_STRUCT_ASSIGN
+#define ASSIGN_STATE(dest,src)  ((dest) = (src))
+#else
+#if MAX_COMPS_IN_SCAN == 4
+#define ASSIGN_STATE(dest,src)  \
+	((dest).get_buffer = (src).get_buffer, \
+	 (dest).bits_left = (src).bits_left, \
+	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
+	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
+	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
+	 (dest).last_dc_val[3] = (src).last_dc_val[3])
+#endif
+#endif
+
+
+typedef struct {
+  struct jpeg_entropy_decoder pub; /* public fields */
+
+  savable_state saved;		/* Bit buffer & DC state at start of MCU */
+
+  /* These fields are NOT loaded into local working state. */
+  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
+  boolean printed_eod;		/* flag to suppress extra end-of-data msgs */
+
+  /* Pointers to derived tables (these workspaces have image lifespan) */
+  D_DERIVED_TBL * dc_derived_tbls[NUM_HUFF_TBLS];
+  D_DERIVED_TBL * ac_derived_tbls[NUM_HUFF_TBLS];
+} huff_entropy_decoder;
+
+typedef huff_entropy_decoder * huff_entropy_ptr;
+
+/* Working state while scanning an MCU.
+ * This struct contains all the fields that are needed by subroutines.
+ */
+
+typedef struct {
+  int unread_marker;		/* nonzero if we have hit a marker */
+  const JOCTET * next_input_byte; /* => next byte to read from source */
+  size_t bytes_in_buffer;	/* # of bytes remaining in source buffer */
+  savable_state cur;		/* Current bit buffer & DC state */
+  j_decompress_ptr cinfo;	/* fill_bit_buffer needs access to this */
+} working_state;
+
+
+/* Forward declarations */
+LOCAL void fix_huff_tbl JPP((j_decompress_ptr cinfo, JHUFF_TBL * htbl,
+			     D_DERIVED_TBL ** pdtbl));
+
+
+/*
+ * Initialize for a Huffman-compressed scan.
+ */
+
+METHODDEF void
+start_pass_huff_decoder (j_decompress_ptr cinfo)
+{
+  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+  int ci, dctbl, actbl;
+  jpeg_component_info * compptr;
+
+  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+    compptr = cinfo->cur_comp_info[ci];
+    dctbl = compptr->dc_tbl_no;
+    actbl = compptr->ac_tbl_no;
+    /* Make sure requested tables are present */
+    if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS ||
+	cinfo->dc_huff_tbl_ptrs[dctbl] == NULL)
+      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
+    if (actbl < 0 || actbl >= NUM_HUFF_TBLS ||
+	cinfo->ac_huff_tbl_ptrs[actbl] == NULL)
+      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
+    /* Compute derived values for Huffman tables */
+    /* We may do this more than once for a table, but it's not expensive */
+    fix_huff_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[dctbl],
+		 & entropy->dc_derived_tbls[dctbl]);
+    fix_huff_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[actbl],
+		 & entropy->ac_derived_tbls[actbl]);
+    /* Initialize DC predictions to 0 */
+    entropy->saved.last_dc_val[ci] = 0;
+  }
+
+  /* Initialize private state variables */
+  entropy->saved.bits_left = 0;
+  entropy->printed_eod = FALSE;
+
+  /* Initialize restart counter */
+  entropy->restarts_to_go = cinfo->restart_interval;
+}
+
 
 LOCAL void
-fix_huff_tbl (HUFF_TBL * htbl)
-/* Compute derived values for a Huffman table */
+fix_huff_tbl (j_decompress_ptr cinfo, JHUFF_TBL * htbl, D_DERIVED_TBL ** pdtbl)
+/* Compute the derived values for a Huffman table */
 {
+  D_DERIVED_TBL *dtbl;
   int p, i, l, si;
   int lookbits, ctr;
   char huffsize[257];
-  UINT16 huffcode[257];
-  UINT16 code;
+  unsigned int huffcode[257];
+  unsigned int code;
+
+  /* Allocate a workspace if we haven't already done so. */
+  if (*pdtbl == NULL)
+    *pdtbl = (D_DERIVED_TBL *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(D_DERIVED_TBL));
+  dtbl = *pdtbl;
+  dtbl->pub = htbl;		/* fill in back link */
   
   /* Figure C.1: make table of Huffman code length for each symbol */
   /* Note that this is in code-length order. */
@@ -61,15 +198,15 @@
   p = 0;
   for (l = 1; l <= 16; l++) {
     if (htbl->bits[l]) {
-      htbl->priv.dec.valptr[l] = p; /* huffval[] index of 1st symbol of code length l */
-      htbl->priv.dec.mincode[l] = huffcode[p]; /* minimum code of length l */
+      dtbl->valptr[l] = p; /* huffval[] index of 1st symbol of code length l */
+      dtbl->mincode[l] = huffcode[p]; /* minimum code of length l */
       p += htbl->bits[l];
-      htbl->priv.dec.maxcode[l] = huffcode[p-1]; /* maximum code of length l */
+      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
     } else {
-      htbl->priv.dec.maxcode[l] = -1; /* -1 if no codes of this length */
+      dtbl->maxcode[l] = -1;	/* -1 if no codes of this length */
     }
   }
-  htbl->priv.dec.maxcode[17] = 0xFFFFFL; /* ensures huff_DECODE terminates */
+  dtbl->maxcode[17] = 0xFFFFFL; /* ensures huff_DECODE terminates */
 
   /* Compute lookahead tables to speed up decoding.
    * First we set all the table entries to 0, indicating "too long";
@@ -78,7 +215,7 @@
    * with that code.
    */
 
-  MEMZERO(htbl->priv.dec.look_nbits, SIZEOF(htbl->priv.dec.look_nbits));
+  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
 
   p = 0;
   for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
@@ -87,8 +224,8 @@
       /* Generate left-justified code followed by all possible bit sequences */
       lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
       for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
-	htbl->priv.dec.look_nbits[lookbits] = l;
-	htbl->priv.dec.look_sym[lookbits] = htbl->huffval[p];
+	dtbl->look_nbits[lookbits] = l;
+	dtbl->look_sym[lookbits] = htbl->huffval[p];
 	lookbits++;
       }
     }
@@ -107,13 +244,15 @@
  * bits, fill_bit_buffer is called; it will attempt to fill get_buffer to
  * the "high water mark" (not just to the number of bits needed; this reduces
  * the function-call overhead cost of entering fill_bit_buffer).
- * On return, fill_bit_buffer guarantees that get_buffer contains at least
- * the requested number of bits --- dummy zeroes are inserted if necessary.
+ * Note that fill_bit_buffer may return FALSE to indicate suspension.
+ * On TRUE return, fill_bit_buffer guarantees that get_buffer contains
+ * at least the requested number of bits --- dummy zeroes are inserted if
+ * necessary.
  *
  * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
  * of get_buffer to be used.  (On machines with wider words, an even larger
  * buffer could be used.)  However, on some machines 32-bit shifts are
- * relatively slow and take time proportional to the number of places shifted.
+ * quite slow and take time proportional to the number of places shifted.
  * (This is true with most PC compilers, for instance.)  In this case it may
  * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
  * average shift distance at the cost of more calls to fill_bit_buffer.
@@ -126,37 +265,70 @@
 #endif
 
 
-LOCAL void
-fill_bit_buffer (int nbits)
+LOCAL boolean
+fill_bit_buffer (working_state * state, int nbits)
 /* Load up the bit buffer to a depth of at least nbits */
 {
+  /* Copy heavily used state fields into locals (hopefully registers) */
+  register const JOCTET * next_input_byte = state->next_input_byte;
+  register size_t bytes_in_buffer = state->bytes_in_buffer;
+  register INT32 get_buffer = state->cur.get_buffer;
+  register int bits_left = state->cur.bits_left;
+  register int c;
+
   /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
   /* (It is assumed that no request will be for more than that many bits.) */
+
   while (bits_left < MIN_GET_BITS) {
-    register int c = JGETC(dcinfo);
-    
+    /* Attempt to read a byte */
+    if (state->unread_marker != 0)
+      goto no_more_data;	/* can't advance past a marker */
+
+    if (bytes_in_buffer == 0) {
+      if (! (*state->cinfo->src->fill_input_buffer) (state->cinfo))
+	return FALSE;
+      next_input_byte = state->cinfo->src->next_input_byte;
+      bytes_in_buffer = state->cinfo->src->bytes_in_buffer;
+    }
+    bytes_in_buffer--;
+    c = GETJOCTET(*next_input_byte++);
+
     /* If it's 0xFF, check and discard stuffed zero byte */
     if (c == 0xFF) {
-      int c2 = JGETC(dcinfo);
-      if (c2 != 0) {
+      do {
+	if (bytes_in_buffer == 0) {
+	  if (! (*state->cinfo->src->fill_input_buffer) (state->cinfo))
+	    return FALSE;
+	  next_input_byte = state->cinfo->src->next_input_byte;
+	  bytes_in_buffer = state->cinfo->src->bytes_in_buffer;
+	}
+	bytes_in_buffer--;
+	c = GETJOCTET(*next_input_byte++);
+      } while (c == 0xFF);
+
+      if (c == 0) {
+	/* Found FF/00, which represents an FF data byte */
+	c = 0xFF;
+      } else {
 	/* Oops, it's actually a marker indicating end of compressed data. */
 	/* Better put it back for use later */
-	JUNGETC(c2,dcinfo);
-	JUNGETC(c,dcinfo);
+	state->unread_marker = c;
+
+      no_more_data:
 	/* There should be enough bits still left in the data segment; */
-	/* if so, just break out of the while loop. */
+	/* if so, just break out of the outer while loop. */
 	if (bits_left >= nbits)
 	  break;
 	/* Uh-oh.  Report corrupted data to user and stuff zeroes into
 	 * the data stream, so that we can produce some kind of image.
 	 * Note that this will be repeated for each byte demanded for the
-	 * rest of the segment; this is a bit slow but not unreasonably so.
+	 * rest of the segment; this is slow but not unreasonably so.
 	 * The main thing is to avoid getting a zillion warnings, hence
 	 * we use a flag to ensure that only one warning appears.
 	 */
-	if (! printed_eod) {
-	  WARNMS(dcinfo->emethods, "Corrupt JPEG data: premature end of data segment");
-	  printed_eod = TRUE;
+	if (! ((huff_entropy_ptr) state->cinfo->entropy)->printed_eod) {
+	  WARNMS(state->cinfo, JWRN_HIT_MARKER);
+	  ((huff_entropy_ptr) state->cinfo->entropy)->printed_eod = TRUE;
 	}
 	c = 0;			/* insert a zero byte into bit buffer */
       }
@@ -166,31 +338,50 @@
     get_buffer = (get_buffer << 8) | c;
     bits_left += 8;
   }
+
+  /* Unload the local registers */
+  state->next_input_byte = next_input_byte;
+  state->bytes_in_buffer = bytes_in_buffer;
+  state->cur.get_buffer = get_buffer;
+  state->cur.bits_left = bits_left;
+
+  return TRUE;
 }
 
 
 /*
  * These macros provide the in-line portion of bit fetching.
- * Correct usage is:
- *	check_bit_buffer(n);		ensure there are N bits in get_buffer
- *      val = get_bits(n);		fetch N bits
- * The value n should be a simple variable, not an expression, because it
+ * Use check_bit_buffer to ensure there are N bits in get_buffer
+ * before using get_bits, peek_bits, or drop_bits.
+ *	check_bit_buffer(state,n,action);
+ *		Ensure there are N bits in get_buffer; if suspend, take action.
+ *      val = get_bits(state,n);
+ *		Fetch next N bits.
+ *      val = peek_bits(state,n);
+ *		Fetch next N bits without removing them from the buffer.
+ *	drop_bits(state,n);
+ *		Discard next N bits.
+ * The value N should be a simple variable, not an expression, because it
  * is evaluated multiple times.
- * peek_bits() fetches next N bits without removing them from the buffer.
  */
 
-#define check_bit_buffer(nbits) \
-	{ if (bits_left < (nbits))  fill_bit_buffer(nbits); }
+#define check_bit_buffer(state,nbits,action) \
+	{ if ((state).cur.bits_left < (nbits))  \
+	    if (! fill_bit_buffer(&(state), nbits))  \
+	      { action; } }
 
-#define get_bits(nbits) \
-	(((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))
+#define get_bits(state,nbits) \
+	(((int) ((state).cur.get_buffer >> ((state).cur.bits_left -= (nbits)))) & ((1<<(nbits))-1))
 
-#define peek_bits(nbits) \
-	(((int) (get_buffer >> (bits_left -  (nbits)))) & ((1<<(nbits))-1))
+#define peek_bits(state,nbits) \
+	(((int) ((state).cur.get_buffer >> ((state).cur.bits_left -  (nbits)))) & ((1<<(nbits))-1))
+
+#define drop_bits(state,nbits) \
+	((state).cur.bits_left -= (nbits))
 
 
 /*
- * Routines to extract next Huffman-coded symbol from input bit stream.
+ * Code for extracting next Huffman-coded symbol from input bit stream.
  * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
  * without looping.  Usually, more than 95% of the Huffman codes will be 8
  * or fewer bits long.  The few overlength codes are handled with a loop.
@@ -198,32 +389,37 @@
  * routine slow_DECODE is rarely entered and need not be inline code.
  *
  * Notes about the huff_DECODE macro:
- * 1. The first if-test is coded to call fill_bit_buffer only when necessary.
- * 2. If the lookahead succeeds, we need only decrement bits_left to remove
- *    the proper number of bits from get_buffer.
- * 3. If the lookahead table contains no entry, the next code must be
- *    more than HUFF_LOOKAHEAD bits long.
- * 4. Near the end of the data segment, we may fail to get enough bits
+ * 1. Near the end of the data segment, we may fail to get enough bits
  *    for a lookahead.  In that case, we do it the hard way.
+ * 2. If the lookahead table contains no entry, the next code must be
+ *    more than HUFF_LOOKAHEAD bits long.
+ * 3. slow_DECODE returns -1 if forced to suspend.
  */
 
-#define huff_DECODE(htbl,result) \
-{ register int nb, look;					\
-  if (bits_left >= HUFF_LOOKAHEAD ||				\
-      (fill_bit_buffer(0), bits_left >= HUFF_LOOKAHEAD)) {	\
-    look = peek_bits(HUFF_LOOKAHEAD);				\
-    if ((nb = htbl->priv.dec.look_nbits[look]) != 0) {		\
-      bits_left -= nb;						\
-      result = htbl->priv.dec.look_sym[look];			\
-    } else							\
-      result = slow_DECODE(htbl, HUFF_LOOKAHEAD+1);		\
-  } else							\
-    result = slow_DECODE(htbl, 1);				\
+#define huff_DECODE(result,state,htbl,donelabel) \
+{ if (state.cur.bits_left < HUFF_LOOKAHEAD) {  \
+    if (! fill_bit_buffer(&state, 0)) return FALSE;  \
+    if (state.cur.bits_left < HUFF_LOOKAHEAD) {  \
+      if ((result = slow_DECODE(&state, htbl, 1)) < 0) return FALSE;  \
+      goto donelabel;  \
+    }  \
+  }  \
+  { register int nb, look;  \
+    look = peek_bits(state, HUFF_LOOKAHEAD);  \
+    if ((nb = htbl->look_nbits[look]) != 0) {  \
+      drop_bits(state, nb);  \
+      result = htbl->look_sym[look];  \
+    } else {  \
+      if ((result = slow_DECODE(&state, htbl, HUFF_LOOKAHEAD+1)) < 0)  \
+	return FALSE;  \
+    }  \
+  }  \
+donelabel:;  \
 }
 
   
 LOCAL int
-slow_DECODE (HUFF_TBL * htbl, int min_bits)
+slow_DECODE (working_state * state, D_DERIVED_TBL * htbl, int min_bits)
 {
   register int l = min_bits;
   register INT32 code;
@@ -231,28 +427,28 @@
   /* huff_DECODE has determined that the code is at least min_bits */
   /* bits long, so fetch that many bits in one swoop. */
 
-  check_bit_buffer(l);
-  code = get_bits(l);
+  check_bit_buffer(*state, l, return -1);
+  code = get_bits(*state, l);
 
   /* Collect the rest of the Huffman code one bit at a time. */
   /* This is per Figure F.16 in the JPEG spec. */
 
-  while (code > htbl->priv.dec.maxcode[l]) {
+  while (code > htbl->maxcode[l]) {
     code <<= 1;
-    check_bit_buffer(1);
-    code |= get_bits(1);
+    check_bit_buffer(*state, 1, return -1);
+    code |= get_bits(*state, 1);
     l++;
   }
 
   /* With garbage input we may reach the sentinel value l = 17. */
 
   if (l > 16) {
-    WARNMS(dcinfo->emethods, "Corrupt JPEG data: bad Huffman code");
+    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
     return 0;			/* fake a zero as the safest result */
   }
 
-  return htbl->huffval[ htbl->priv.dec.valptr[l] +
-		        ((int) (code - htbl->priv.dec.mincode[l])) ];
+  return htbl->pub->huffval[ htbl->valptr[l] +
+			    ((int) (code - htbl->mincode[l])) ];
 }
 
 
@@ -282,87 +478,35 @@
 
 
 /*
- * Initialize for a Huffman-compressed scan.
- * This is invoked after reading the SOS marker.
- */
-
-METHODDEF void
-decoder_init (decompress_info_ptr cinfo)
-{
-  short ci;
-  jpeg_component_info * compptr;
-
-  /* Initialize static variables */
-  dcinfo = cinfo;
-  bits_left = 0;
-  printed_eod = FALSE;
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
-    /* Make sure requested tables are present */
-    if (cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no] == NULL ||
-	cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no] == NULL)
-      ERREXIT(cinfo->emethods, "Use of undefined Huffman table");
-    /* Compute derived values for Huffman tables */
-    /* We may do this more than once for same table, but it's not a big deal */
-    fix_huff_tbl(cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no]);
-    fix_huff_tbl(cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
-    /* Initialize DC predictions to 0 */
-    cinfo->last_dc_val[ci] = 0;
-  }
-
-  /* Initialize restart stuff */
-  cinfo->restarts_to_go = cinfo->restart_interval;
-  cinfo->next_restart_num = 0;
-}
-
-
-/*
  * Check for a restart marker & resynchronize decoder.
+ * Returns FALSE if must suspend.
  */
 
-LOCAL void
-process_restart (decompress_info_ptr cinfo)
+LOCAL boolean
+process_restart (j_decompress_ptr cinfo)
 {
-  int c, nbytes;
-  short ci;
+  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+  int ci;
 
-  /* Throw away any unused bits remaining in bit buffer */
-  nbytes = bits_left / 8;	/* count any full bytes loaded into buffer */
-  bits_left = 0;
-  printed_eod = FALSE;		/* next segment can get another warning */
+  /* Throw away any unused bits remaining in bit buffer; */
+  /* include any full bytes in next_marker's count of discarded bytes */
+  cinfo->marker->discarded_bytes += entropy->saved.bits_left / 8;
+  entropy->saved.bits_left = 0;
 
-  /* Scan for next JPEG marker */
-  do {
-    do {			/* skip any non-FF bytes */
-      nbytes++;
-      c = JGETC(cinfo);
-    } while (c != 0xFF);
-    do {			/* skip any duplicate FFs */
-      /* we don't increment nbytes here since extra FFs are legal */
-      c = JGETC(cinfo);
-    } while (c == 0xFF);
-  } while (c == 0);		/* repeat if it was a stuffed FF/00 */
-
-  if (nbytes != 1)
-    WARNMS2(cinfo->emethods,
-	    "Corrupt JPEG data: %d extraneous bytes before marker 0x%02x",
-	    nbytes-1, c);
-
-  if (c != (RST0 + cinfo->next_restart_num)) {
-    /* Uh-oh, the restart markers have been messed up too. */
-    /* Let the file-format module try to figure out how to resync. */
-    (*cinfo->methods->resync_to_restart) (cinfo, c);
-  } else
-    TRACEMS1(cinfo->emethods, 2, "RST%d", cinfo->next_restart_num);
+  /* Advance past the RSTn marker */
+  if (! (*cinfo->marker->read_restart_marker) (cinfo))
+    return FALSE;
 
   /* Re-initialize DC predictions to 0 */
   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
-    cinfo->last_dc_val[ci] = 0;
+    entropy->saved.last_dc_val[ci] = 0;
 
-  /* Update restart state */
-  cinfo->restarts_to_go = cinfo->restart_interval;
-  cinfo->next_restart_num = (cinfo->next_restart_num + 1) & 7;
+  /* Reset restart counter */
+  entropy->restarts_to_go = cinfo->restart_interval;
+
+  entropy->printed_eod = FALSE; /* next segment can get another warning */
+
+  return TRUE;
 }
 
 
@@ -372,7 +516,7 @@
  * we put some extra zeroes after the real entries.
  */
 
-static const short ZAG[DCTSIZE2+16] = {
+static const int ZAG[DCTSIZE2+16] = {
   0,  1,  8, 16,  9,  2,  3, 10,
  17, 24, 32, 25, 18, 11,  4,  5,
  12, 19, 26, 33, 40, 48, 41, 34,
@@ -388,104 +532,156 @@
 
 /*
  * Decode and return one MCU's worth of Huffman-compressed coefficients.
- * This routine also handles quantization descaling and zigzag reordering
- * of coefficient values.
+ * The coefficients are reordered from zigzag order into natural array order,
+ * but are not dequantized.
  *
  * The i'th block of the MCU is stored into the block pointed to by
  * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
  * (Wholesale zeroing is usually a little faster than retail...)
+ *
+ * Returns FALSE if data source requested suspension.  In that case no
+ * changes have been made to permanent state.  (Exception: some output
+ * coefficients may already have been assigned.  This is harmless for
+ * this module, but would not work for decoding progressive JPEG.)
  */
 
-METHODDEF void
-decode_mcu (decompress_info_ptr cinfo, JBLOCKROW *MCU_data)
+METHODDEF boolean
+decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
 {
+  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   register int s, k, r;
-  short blkn, ci;
-  register JBLOCKROW block;
-  register QUANT_TBL_PTR quanttbl;
-  HUFF_TBL *dctbl;
-  HUFF_TBL *actbl;
+  int blkn, ci;
+  JBLOCKROW block;
+  working_state state;
+  D_DERIVED_TBL * dctbl;
+  D_DERIVED_TBL * actbl;
   jpeg_component_info * compptr;
 
-  /* Account for restart interval, process restart marker if needed */
+  /* Process restart marker if needed; may have to suspend */
   if (cinfo->restart_interval) {
-    if (cinfo->restarts_to_go == 0)
-      process_restart(cinfo);
-    cinfo->restarts_to_go--;
+    if (entropy->restarts_to_go == 0)
+      if (! process_restart(cinfo))
+	return FALSE;
   }
 
+  /* Load up working state */
+  state.unread_marker = cinfo->unread_marker;
+  state.next_input_byte = cinfo->src->next_input_byte;
+  state.bytes_in_buffer = cinfo->src->bytes_in_buffer;
+  ASSIGN_STATE(state.cur, entropy->saved);
+  state.cinfo = cinfo;
+
   /* Outer loop handles each block in the MCU */
 
   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
     block = MCU_data[blkn];
     ci = cinfo->MCU_membership[blkn];
     compptr = cinfo->cur_comp_info[ci];
-    quanttbl = cinfo->quant_tbl_ptrs[compptr->quant_tbl_no];
-    actbl = cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no];
-    dctbl = cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no];
+    dctbl = entropy->dc_derived_tbls[compptr->dc_tbl_no];
+    actbl = entropy->ac_derived_tbls[compptr->ac_tbl_no];
 
     /* Decode a single block's worth of coefficients */
 
     /* Section F.2.2.1: decode the DC coefficient difference */
-    huff_DECODE(dctbl, s);
+    huff_DECODE(s, state, dctbl, label1);
     if (s) {
-      check_bit_buffer(s);
-      r = get_bits(s);
+      check_bit_buffer(state, s, return FALSE);
+      r = get_bits(state, s);
       s = huff_EXTEND(r, s);
     }
 
+    /* Shortcut if component's values are not interesting */
+    if (! compptr->component_needed)
+      goto skip_ACs;
+
     /* Convert DC difference to actual value, update last_dc_val */
-    s += cinfo->last_dc_val[ci];
-    cinfo->last_dc_val[ci] = (JCOEF) s;
-    /* Descale and output the DC coefficient (assumes ZAG[0] = 0) */
-    (*block)[0] = (JCOEF) (((JCOEF) s) * quanttbl[0]);
-    
-    /* Section F.2.2.2: decode the AC coefficients */
-    /* Since zero values are skipped, output area must be zeroed beforehand */
-    for (k = 1; k < DCTSIZE2; k++) {
-      huff_DECODE(actbl, s);
+    s += state.cur.last_dc_val[ci];
+    state.cur.last_dc_val[ci] = s;
+    /* Output the DC coefficient (assumes ZAG[0] = 0) */
+    (*block)[0] = (JCOEF) s;
+
+    /* Do we need to decode the AC coefficients for this component? */
+    if (compptr->DCT_scaled_size > 1) {
+
+      /* Section F.2.2.2: decode the AC coefficients */
+      /* Since zeroes are skipped, output area must be cleared beforehand */
+      for (k = 1; k < DCTSIZE2; k++) {
+	huff_DECODE(s, state, actbl, label2);
       
-      r = s >> 4;
-      s &= 15;
+	r = s >> 4;
+	s &= 15;
       
-      if (s) {
-	k += r;
-	check_bit_buffer(s);
-	r = get_bits(s);
-	s = huff_EXTEND(r, s);
-	/* Descale coefficient and output in natural (dezigzagged) order */
-	(*block)[ZAG[k]] = (JCOEF) (((JCOEF) s) * quanttbl[k]);
-      } else {
-	if (r != 15)
-	  break;
-	k += 15;
+	if (s) {
+	  k += r;
+	  check_bit_buffer(state, s, return FALSE);
+	  r = get_bits(state, s);
+	  s = huff_EXTEND(r, s);
+	  /* Output coefficient in natural (dezigzagged) order */
+	  (*block)[ZAG[k]] = (JCOEF) s;
+	} else {
+	  if (r != 15)
+	    break;
+	  k += 15;
+	}
       }
+
+    } else {
+skip_ACs:
+
+      /* Section F.2.2.2: decode the AC coefficients */
+      /* In this path we just discard the values */
+      for (k = 1; k < DCTSIZE2; k++) {
+	huff_DECODE(s, state, actbl, label3);
+      
+	r = s >> 4;
+	s &= 15;
+      
+	if (s) {
+	  k += r;
+	  check_bit_buffer(state, s, return FALSE);
+	  drop_bits(state, s);
+	} else {
+	  if (r != 15)
+	    break;
+	  k += 15;
+	}
+      }
+
     }
   }
+
+  /* Completed MCU, so update state */
+  cinfo->unread_marker = state.unread_marker;
+  cinfo->src->next_input_byte = state.next_input_byte;
+  cinfo->src->bytes_in_buffer = state.bytes_in_buffer;
+  ASSIGN_STATE(entropy->saved, state.cur);
+
+  /* Account for restart interval (no-op if not using restarts) */
+  entropy->restarts_to_go--;
+
+  return TRUE;
 }
 
 
 /*
- * Finish up at the end of a Huffman-compressed scan.
- */
-
-METHODDEF void
-decoder_term (decompress_info_ptr cinfo)
-{
-  /* No work needed */
-}
-
-
-/*
- * The method selection routine for Huffman entropy decoding.
+ * Module initialization routine for Huffman entropy decoding.
  */
 
 GLOBAL void
-jseldhuffman (decompress_info_ptr cinfo)
+jinit_huff_decoder (j_decompress_ptr cinfo)
 {
-  if (! cinfo->arith_code) {
-    cinfo->methods->entropy_decode_init = decoder_init;
-    cinfo->methods->entropy_decode = decode_mcu;
-    cinfo->methods->entropy_decode_term = decoder_term;
+  huff_entropy_ptr entropy;
+  int i;
+
+  entropy = (huff_entropy_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(huff_entropy_decoder));
+  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
+  entropy->pub.start_pass = start_pass_huff_decoder;
+  entropy->pub.decode_mcu = decode_mcu;
+
+  /* Mark tables unallocated */
+  for (i = 0; i < NUM_HUFF_TBLS; i++) {
+    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
   }
 }
diff --git a/jdmain.c b/jdmain.c
deleted file mode 100644
index efb93ea..0000000
--- a/jdmain.c
+++ /dev/null
@@ -1,514 +0,0 @@
-/*
- * jdmain.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains a command-line user interface for the JPEG decompressor.
- * It should work on any system with Unix- or MS-DOS-style command lines.
- *
- * Two different command line styles are permitted, depending on the
- * compile-time switch TWO_FILE_COMMANDLINE:
- *	djpeg [options]  inputfile outputfile
- *	djpeg [options]  [inputfile]
- * In the second style, output is always to standard output, which you'd
- * normally redirect to a file or pipe to some other program.  Input is
- * either from a named file or from standard input (typically redirected).
- * The second style is convenient on Unix but is unhelpful on systems that
- * don't support pipes.  Also, you MUST use the first style if your system
- * doesn't do binary I/O to stdin/stdout.
- * To simplify script writing, the "-outfile" switch is provided.  The syntax
- *	djpeg [options]  -outfile outputfile  inputfile
- * works regardless of which command line style is used.
- */
-
-#include "jinclude.h"
-#ifdef INCLUDES_ARE_ANSI
-#include <stdlib.h>		/* to declare exit() */
-#endif
-#include <ctype.h>		/* to declare isupper(), tolower() */
-#ifdef NEED_SIGNAL_CATCHER
-#include <signal.h>		/* to declare signal() */
-#endif
-#ifdef USE_SETMODE
-#include <fcntl.h>		/* to declare setmode() */
-#endif
-
-#ifdef THINK_C
-#include <console.h>		/* command-line reader for Macintosh */
-#endif
-
-#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
-#define READ_BINARY	"r"
-#define WRITE_BINARY	"w"
-#else
-#define READ_BINARY	"rb"
-#define WRITE_BINARY	"wb"
-#endif
-
-#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
-#define EXIT_FAILURE  1
-#endif
-#ifndef EXIT_SUCCESS
-#ifdef VMS
-#define EXIT_SUCCESS  1		/* VMS is very nonstandard */
-#else
-#define EXIT_SUCCESS  0
-#endif
-#endif
-
-
-#include "jversion.h"		/* for version message */
-
-
-/*
- * This list defines the known output image formats
- * (not all of which need be supported by a given version).
- * You can change the default output format by defining DEFAULT_FMT;
- * indeed, you had better do so if you undefine PPM_SUPPORTED.
- */
-
-typedef enum {
-	FMT_GIF,		/* GIF format */
-	FMT_PPM,		/* PPM/PGM (PBMPLUS formats) */
-	FMT_RLE,		/* RLE format */
-	FMT_TARGA,		/* Targa format */
-	FMT_TIFF		/* TIFF format */
-} IMAGE_FORMATS;
-
-#ifndef DEFAULT_FMT		/* so can override from CFLAGS in Makefile */
-#define DEFAULT_FMT	FMT_PPM
-#endif
-
-static IMAGE_FORMATS requested_fmt;
-
-
-/*
- * This routine gets control after the input file header has been read.
- * It must determine what output file format is to be written,
- * and make any other decompression parameter changes that are desirable.
- */
-
-METHODDEF void
-d_ui_method_selection (decompress_info_ptr cinfo)
-{
-  /* if grayscale or CMYK input, force similar output; */
-  /* else leave the output colorspace as set by options. */
-  if (cinfo->jpeg_color_space == CS_GRAYSCALE)
-    cinfo->out_color_space = CS_GRAYSCALE;
-  else if (cinfo->jpeg_color_space == CS_CMYK)
-    cinfo->out_color_space = CS_CMYK;
-
-  /* select output file format */
-  /* Note: jselwxxx routine may make additional parameter changes,
-   * such as forcing color quantization if it's a colormapped format.
-   */
-  switch (requested_fmt) {
-#ifdef GIF_SUPPORTED
-  case FMT_GIF:
-    jselwgif(cinfo);
-    break;
-#endif
-#ifdef PPM_SUPPORTED
-  case FMT_PPM:
-    jselwppm(cinfo);
-    break;
-#endif
-#ifdef RLE_SUPPORTED
-  case FMT_RLE:
-    jselwrle(cinfo);
-    break;
-#endif
-#ifdef TARGA_SUPPORTED
-  case FMT_TARGA:
-    jselwtarga(cinfo);
-    break;
-#endif
-  default:
-    ERREXIT(cinfo->emethods, "Unsupported output file format");
-    break;
-  }
-}
-
-
-/*
- * Signal catcher to ensure that temporary files are removed before aborting.
- * NB: for Amiga Manx C this is actually a global routine named _abort();
- * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
- */
-
-#ifdef NEED_SIGNAL_CATCHER
-
-static external_methods_ptr emethods; /* for access to free_all */
-
-GLOBAL void
-signal_catcher (int signum)
-{
-  if (emethods != NULL) {
-    emethods->trace_level = 0;	/* turn off trace output */
-    (*emethods->free_all) ();	/* clean up memory allocation & temp files */
-  }
-  exit(EXIT_FAILURE);
-}
-
-#endif
-
-
-/*
- * Optional routine to display a percent-done figure on stderr.
- * See jddeflts.c for explanation of the information used.
- */
-
-#ifdef PROGRESS_REPORT
-
-METHODDEF void
-progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
-{
-  if (cinfo->total_passes > 1) {
-    fprintf(stderr, "\rPass %d/%d: %3d%% ",
-	    cinfo->completed_passes+1, cinfo->total_passes,
-	    (int) (loopcounter*100L/looplimit));
-  } else {
-    fprintf(stderr, "\r %3d%% ",
-	    (int) (loopcounter*100L/looplimit));
-  }
-  fflush(stderr);
-}
-
-#endif
-
-
-/*
- * Argument-parsing code.
- * The switch parser is designed to be useful with DOS-style command line
- * syntax, ie, intermixed switches and file names, where only the switches
- * to the left of a given file name affect processing of that file.
- * The main program in this file doesn't actually use this capability...
- */
-
-
-static char * progname;		/* program name for error messages */
-static char * outfilename;	/* for -outfile switch */
-
-
-LOCAL void
-usage (void)
-/* complain about bad command line */
-{
-  fprintf(stderr, "usage: %s [switches] ", progname);
-#ifdef TWO_FILE_COMMANDLINE
-  fprintf(stderr, "inputfile outputfile\n");
-#else
-  fprintf(stderr, "[inputfile]\n");
-#endif
-
-  fprintf(stderr, "Switches (names may be abbreviated):\n");
-  fprintf(stderr, "  -colors N      Reduce image to no more than N colors\n");
-#ifdef GIF_SUPPORTED
-  fprintf(stderr, "  -gif           Select GIF output format\n");
-#endif
-#ifdef PPM_SUPPORTED
-  fprintf(stderr, "  -pnm           Select PBMPLUS (PPM/PGM) output format (default)\n");
-#endif
-  fprintf(stderr, "  -quantize N    Same as -colors N\n");
-#ifdef RLE_SUPPORTED
-  fprintf(stderr, "  -rle           Select Utah RLE output format\n");
-#endif
-#ifdef TARGA_SUPPORTED
-  fprintf(stderr, "  -targa         Select Targa output format\n");
-#endif
-  fprintf(stderr, "Switches for advanced users:\n");
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-  fprintf(stderr, "  -blocksmooth   Apply cross-block smoothing\n");
-#endif
-  fprintf(stderr, "  -grayscale     Force grayscale output\n");
-  fprintf(stderr, "  -nodither      Don't use dithering in quantization\n");
-#ifdef QUANT_1PASS_SUPPORTED
-  fprintf(stderr, "  -onepass       Use 1-pass quantization (fast, low quality)\n");
-#endif
-  fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
-  fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
-  exit(EXIT_FAILURE);
-}
-
-
-LOCAL boolean
-keymatch (char * arg, const char * keyword, int minchars)
-/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
-/* keyword is the constant keyword (must be lower case already), */
-/* minchars is length of minimum legal abbreviation. */
-{
-  register int ca, ck;
-  register int nmatched = 0;
-
-  while ((ca = *arg++) != '\0') {
-    if ((ck = *keyword++) == '\0')
-      return FALSE;		/* arg longer than keyword, no good */
-    if (isupper(ca))		/* force arg to lcase (assume ck is already) */
-      ca = tolower(ca);
-    if (ca != ck)
-      return FALSE;		/* no good */
-    nmatched++;			/* count matched characters */
-  }
-  /* reached end of argument; fail if it's too short for unique abbrev */
-  if (nmatched < minchars)
-    return FALSE;
-  return TRUE;			/* A-OK */
-}
-
-
-LOCAL int
-parse_switches (decompress_info_ptr cinfo, int last_file_arg_seen,
-		int argc, char **argv)
-/* Initialize cinfo with default switch settings, then parse option switches.
- * Returns argv[] index of first file-name argument (== argc if none).
- * Any file names with indexes <= last_file_arg_seen are ignored;
- * they have presumably been processed in a previous iteration.
- * (Pass 0 for last_file_arg_seen on the first or only iteration.)
- */
-{
-  int argn;
-  char * arg;
-
-  /* (Re-)initialize the system-dependent error and memory managers. */
-  jselerror(cinfo->emethods);	/* error/trace message routines */
-  jselmemmgr(cinfo->emethods);	/* memory allocation routines */
-  cinfo->methods->d_ui_method_selection = d_ui_method_selection;
-
-  /* Now OK to enable signal catcher. */
-#ifdef NEED_SIGNAL_CATCHER
-  emethods = cinfo->emethods;
-#endif
-
-  /* Set up default JPEG parameters. */
-  j_d_defaults(cinfo, TRUE);
-  requested_fmt = DEFAULT_FMT;	/* set default output file format */
-  outfilename = NULL;
-
-  /* Scan command line options, adjust parameters */
-
-  for (argn = 1; argn < argc; argn++) {
-    arg = argv[argn];
-    if (*arg != '-') {
-      /* Not a switch, must be a file name argument */
-      if (argn <= last_file_arg_seen) {
-	outfilename = NULL;	/* -outfile applies to just one input file */
-	continue;		/* ignore this name if previously processed */
-      }
-      break;			/* else done parsing switches */
-    }
-    arg++;			/* advance past switch marker character */
-
-    if (keymatch(arg, "blocksmooth", 1)) {
-      /* Enable cross-block smoothing. */
-      cinfo->do_block_smoothing = TRUE;
-
-    } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
-	       keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
-      /* Do color quantization. */
-      int val;
-
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      if (sscanf(argv[argn], "%d", &val) != 1)
-	usage();
-      cinfo->desired_number_of_colors = val;
-      cinfo->quantize_colors = TRUE;
-
-    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
-      /* Enable debug printouts. */
-      /* On first -d, print version identification */
-      if (last_file_arg_seen == 0 && cinfo->emethods->trace_level == 0)
-	fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
-		JVERSION, JCOPYRIGHT);
-      cinfo->emethods->trace_level++;
-
-    } else if (keymatch(arg, "gif", 1)) {
-      /* GIF output format. */
-      requested_fmt = FMT_GIF;
-
-    } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
-      /* Force monochrome output. */
-      cinfo->out_color_space = CS_GRAYSCALE;
-
-    } else if (keymatch(arg, "maxmemory", 1)) {
-      /* Maximum memory in Kb (or Mb with 'm'). */
-      long lval;
-      char ch = 'x';
-
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
-	usage();
-      if (ch == 'm' || ch == 'M')
-	lval *= 1000L;
-      cinfo->emethods->max_memory_to_use = lval * 1000L;
-
-    } else if (keymatch(arg, "nodither", 3)) {
-      /* Suppress dithering in color quantization. */
-      cinfo->use_dithering = FALSE;
-
-    } else if (keymatch(arg, "onepass", 1)) {
-      /* Use fast one-pass quantization. */
-      cinfo->two_pass_quantize = FALSE;
-
-    } else if (keymatch(arg, "outfile", 3)) {
-      /* Set output file name. */
-      if (++argn >= argc)	/* advance to next argument */
-	usage();
-      outfilename = argv[argn];	/* save it away for later use */
-
-    } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
-      /* PPM/PGM output format. */
-      requested_fmt = FMT_PPM;
-
-    } else if (keymatch(arg, "rle", 1)) {
-      /* RLE output format. */
-      requested_fmt = FMT_RLE;
-
-    } else if (keymatch(arg, "targa", 1)) {
-      /* Targa output format. */
-      requested_fmt = FMT_TARGA;
-
-    } else {
-      usage();			/* bogus switch */
-    }
-  }
-
-  return argn;			/* return index of next arg (file name) */
-}
-
-
-/*
- * The main program.
- */
-
-GLOBAL int
-main (int argc, char **argv)
-{
-  struct Decompress_info_struct cinfo;
-  struct Decompress_methods_struct dc_methods;
-  struct External_methods_struct e_methods;
-  int file_index;
-
-  /* On Mac, fetch a command line. */
-#ifdef THINK_C
-  argc = ccommand(&argv);
-#endif
-
-  progname = argv[0];
-
-  /* Set up links to method structures. */
-  cinfo.methods = &dc_methods;
-  cinfo.emethods = &e_methods;
-
-  /* Install, but don't yet enable signal catcher. */
-#ifdef NEED_SIGNAL_CATCHER
-  emethods = NULL;
-  signal(SIGINT, signal_catcher);
-#ifdef SIGTERM			/* not all systems have SIGTERM */
-  signal(SIGTERM, signal_catcher);
-#endif
-#endif
-
-  /* Scan command line: set up compression parameters, find file names. */
-
-  file_index = parse_switches(&cinfo, 0, argc, argv);
-
-#ifdef TWO_FILE_COMMANDLINE
-  /* Must have either -outfile switch or explicit output file name */
-  if (outfilename == NULL) {
-    if (file_index != argc-2) {
-      fprintf(stderr, "%s: must name one input and one output file\n",
-	      progname);
-      usage();
-    }
-    outfilename = argv[file_index+1];
-  } else {
-    if (file_index != argc-1) {
-      fprintf(stderr, "%s: must name one input and one output file\n",
-	      progname);
-      usage();
-    }
-  }
-#else
-  /* Unix style: expect zero or one file name */
-  if (file_index < argc-1) {
-    fprintf(stderr, "%s: only one input file\n", progname);
-    usage();
-  }
-#endif /* TWO_FILE_COMMANDLINE */
-
-  /* Open the input file. */
-  if (file_index < argc) {
-    if ((cinfo.input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
-      fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
-      exit(EXIT_FAILURE);
-    }
-  } else {
-    /* default input file is stdin */
-#ifdef USE_SETMODE		/* need to hack file mode? */
-    setmode(fileno(stdin), O_BINARY);
-#endif
-#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
-    if ((cinfo.input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
-      fprintf(stderr, "%s: can't open stdin\n", progname);
-      exit(EXIT_FAILURE);
-    }
-#else
-    cinfo.input_file = stdin;
-#endif
-  }
-
-  /* Open the output file. */
-  if (outfilename != NULL) {
-    if ((cinfo.output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
-      fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
-      exit(EXIT_FAILURE);
-    }
-  } else {
-    /* default output file is stdout */
-#ifdef USE_SETMODE		/* need to hack file mode? */
-    setmode(fileno(stdout), O_BINARY);
-#endif
-#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
-    if ((cinfo.output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
-      fprintf(stderr, "%s: can't open stdout\n", progname);
-      exit(EXIT_FAILURE);
-    }
-#else
-    cinfo.output_file = stdout;
-#endif
-  }
-  
-  /* Set up to read a JFIF or baseline-JPEG file. */
-  /* A smarter UI would inspect the first few bytes of the input file */
-  /* to determine its type. */
-#ifdef JFIF_SUPPORTED
-  jselrjfif(&cinfo);
-#else
-  You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
-#endif
-
-#ifdef PROGRESS_REPORT
-  /* Start up progress display, unless trace output is on */
-  if (e_methods.trace_level == 0)
-    dc_methods.progress_monitor = progress_monitor;
-#endif
-
-  /* Do it to it! */
-  jpeg_decompress(&cinfo);
-
-#ifdef PROGRESS_REPORT
-  /* Clear away progress display */
-  if (e_methods.trace_level == 0) {
-    fprintf(stderr, "\r                \r");
-    fflush(stderr);
-  }
-#endif
-
-  /* All done. */
-  exit(EXIT_SUCCESS);
-  return 0;			/* suppress no-return-value warnings */
-}
diff --git a/jdmainct.c b/jdmainct.c
new file mode 100644
index 0000000..f9abbad
--- /dev/null
+++ b/jdmainct.c
@@ -0,0 +1,531 @@
+/*
+ * jdmainct.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the main buffer controller for decompression.
+ * The main buffer lies between the JPEG decompressor proper and the
+ * post-processor; it holds downsampled data in the JPEG colorspace.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * In the current system design, the main buffer need never be a full-image
+ * buffer; any full-height buffers will be found inside the coefficient or
+ * postprocessing controllers.  Nonetheless, the main controller is not
+ * trivial.  Its responsibility is to provide context rows for upsampling/
+ * rescaling, and doing this in an efficient fashion is a bit tricky.
+ *
+ * Postprocessor input data is counted in "row groups".  A row group
+ * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
+ * sample rows of each component.  (We require DCT_scaled_size values to be
+ * chosen such that these numbers are integers.  In practice DCT_scaled_size
+ * values will likely be powers of two, so we actually have the stronger
+ * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
+ * Upsampling will typically produce max_v_samp_factor pixel rows from each
+ * row group (times any additional scale factor that the upsampler is
+ * applying).
+ *
+ * The coefficient controller will deliver data to us one iMCU row at a time;
+ * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
+ * exactly min_DCT_scaled_size row groups.  (This amount of data corresponds
+ * to one row of MCUs when the image is fully interleaved.)  Note that the
+ * number of sample rows varies across components, but the number of row
+ * groups does not.  Some garbage sample rows may be included in the last iMCU
+ * row at the bottom of the image.
+ *
+ * Depending on the vertical scaling algorithm used, the upsampler may need
+ * access to the sample row(s) above and below its current input row group.
+ * The upsampler is required to set need_context_rows TRUE at global selection
+ * time if so.  When need_context_rows is FALSE, this controller can simply
+ * obtain one iMCU row at a time from the coefficient controller and dole it
+ * out as row groups to the postprocessor.
+ *
+ * When need_context_rows is TRUE, this controller guarantees that the buffer
+ * passed to postprocessing contains at least one row group's worth of samples
+ * above and below the row group(s) being processed.  Note that the context
+ * rows "above" the first passed row group appear at negative row offsets in
+ * the passed buffer.  At the top and bottom of the image, the required
+ * context rows are manufactured by duplicating the first or last real sample
+ * row; this avoids having special cases in the upsampling inner loops.
+ *
+ * The amount of context is fixed at one row group just because that's a
+ * convenient number for this controller to work with.  The existing
+ * upsamplers really only need one sample row of context.  An upsampler
+ * supporting arbitrary output rescaling might wish for more than one row
+ * group of context when shrinking the image; tough, we don't handle that.
+ * (This is justified by the assumption that downsizing will be handled mostly
+ * by adjusting the DCT_scaled_size values, so that the actual scale factor at
+ * the upsample step needn't be much less than one.)
+ *
+ * To provide the desired context, we have to retain the last two row groups
+ * of one iMCU row while reading in the next iMCU row.  (The last row group
+ * can't be processed until we have another row group for its below-context,
+ * and so we have to save the next-to-last group too for its above-context.)
+ * We could do this most simply by copying data around in our buffer, but
+ * that'd be very slow.  We can avoid copying any data by creating a rather
+ * strange pointer structure.  Here's how it works.  We allocate a workspace
+ * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
+ * of row groups per iMCU row).  We create two sets of redundant pointers to
+ * the workspace.  Labeling the physical row groups 0 to M+1, the synthesized
+ * pointer lists look like this:
+ *                   M+1                          M-1
+ * master pointer --> 0         master pointer --> 0
+ *                    1                            1
+ *                   ...                          ...
+ *                   M-3                          M-3
+ *                   M-2                           M
+ *                   M-1                          M+1
+ *                    M                           M-2
+ *                   M+1                          M-1
+ *                    0                            0
+ * We read alternate iMCU rows using each master pointer; thus the last two
+ * row groups of the previous iMCU row remain un-overwritten in the workspace.
+ * The pointer lists are set up so that the required context rows appear to
+ * be adjacent to the proper places when we pass the pointer lists to the
+ * upsampler.
+ *
+ * The above pictures describe the normal state of the pointer lists.
+ * At top and bottom of the image, we diddle the pointer lists to duplicate
+ * the first or last sample row as necessary (this is cheaper than copying
+ * sample rows around).
+ *
+ * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1.  In that
+ * situation each iMCU row provides only one row group so the buffering logic
+ * must be different (eg, we must read two iMCU rows before we can emit the
+ * first row group).  For now, we simply do not support providing context
+ * rows when min_DCT_scaled_size is 1.  That combination seems unlikely to
+ * be worth providing --- if someone wants a 1/8th-size preview, they probably
+ * want it quick and dirty, so a context-free upsampler is sufficient.
+ */
+
+
+/* Private buffer controller object */
+
+typedef struct {
+  struct jpeg_d_main_controller pub; /* public fields */
+
+  /* Pointer to allocated workspace (M or M+2 row groups). */
+  JSAMPARRAY buffer[MAX_COMPONENTS];
+
+  boolean buffer_full;		/* Have we gotten an iMCU row from decoder? */
+  JDIMENSION rowgroup_ctr;	/* counts row groups output to postprocessor */
+
+  /* Remaining fields are only used in the context case. */
+
+  /* These are the master pointers to the funny-order pointer lists. */
+  JSAMPIMAGE xbuffer[2];	/* pointers to weird pointer lists */
+
+  int whichptr;			/* indicates which pointer set is now in use */
+  int context_state;		/* process_data state machine status */
+  JDIMENSION rowgroups_avail;	/* row groups available to postprocessor */
+  JDIMENSION iMCU_row_ctr;	/* counts iMCU rows to detect image top/bot */
+} my_main_controller;
+
+typedef my_main_controller * my_main_ptr;
+
+/* context_state values: */
+#define CTX_PREPARE_FOR_IMCU	0	/* need to prepare for MCU row */
+#define CTX_PROCESS_IMCU	1	/* feeding iMCU to postprocessor */
+#define CTX_POSTPONED_ROW	2	/* feeding postponed row group */
+
+
+/* Forward declarations */
+METHODDEF void process_data_simple_main
+	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+METHODDEF void process_data_context_main
+	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+METHODDEF void process_data_input_only
+	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+#endif
+#ifdef QUANT_2PASS_SUPPORTED
+METHODDEF void process_data_crank_post
+	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+#endif
+
+
+LOCAL void
+make_funny_pointers (j_decompress_ptr cinfo)
+/* Create the funny pointer lists discussed in the comments above.
+ * The actual workspace is already allocated (in main->buffer),
+ * we just have to make the curiously ordered lists.
+ */
+{
+  my_main_ptr main = (my_main_ptr) cinfo->main;
+  int ci, i, rgroup;
+  int M = cinfo->min_DCT_scaled_size;
+  jpeg_component_info *compptr;
+  JSAMPARRAY buf, xbuf0, xbuf1;
+
+  /* Get top-level space for component array pointers.
+   * We alloc both arrays with one call to save a few cycles.
+   */
+  main->xbuffer[0] = (JSAMPIMAGE)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
+  main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+      cinfo->min_DCT_scaled_size; /* height of a row group of component */
+    /* Get space for pointer lists --- M+4 row groups in each list.
+     * We alloc both pointer lists with one call to save a few cycles.
+     */
+    xbuf0 = (JSAMPARRAY)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
+    xbuf0 += rgroup;		/* want one row group at negative offsets */
+    main->xbuffer[0][ci] = xbuf0;
+    xbuf1 = xbuf0 + (rgroup * (M + 4));
+    main->xbuffer[1][ci] = xbuf1;
+    /* First copy the workspace pointers as-is */
+    buf = main->buffer[ci];
+    for (i = 0; i < rgroup * (M + 2); i++) {
+      xbuf0[i] = xbuf1[i] = buf[i];
+    }
+    /* In the second list, put the last four row groups in swapped order */
+    for (i = 0; i < rgroup * 2; i++) {
+      xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
+      xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
+    }
+    /* The wraparound pointers at top and bottom will be filled later
+     * (see set_wraparound_pointers, below).  Initially we want the "above"
+     * pointers to duplicate the first actual data line.  This only needs
+     * to happen in xbuffer[0].
+     */
+    for (i = 0; i < rgroup; i++) {
+      xbuf0[i - rgroup] = xbuf0[0];
+    }
+  }
+}
+
+
+LOCAL void
+set_wraparound_pointers (j_decompress_ptr cinfo)
+/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
+ * This changes the pointer list state from top-of-image to the normal state.
+ */
+{
+  my_main_ptr main = (my_main_ptr) cinfo->main;
+  int ci, i, rgroup;
+  int M = cinfo->min_DCT_scaled_size;
+  jpeg_component_info *compptr;
+  JSAMPARRAY xbuf0, xbuf1;
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+      cinfo->min_DCT_scaled_size; /* height of a row group of component */
+    xbuf0 = main->xbuffer[0][ci];
+    xbuf1 = main->xbuffer[1][ci];
+    for (i = 0; i < rgroup; i++) {
+      xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
+      xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
+      xbuf0[rgroup*(M+2) + i] = xbuf0[i];
+      xbuf1[rgroup*(M+2) + i] = xbuf1[i];
+    }
+  }
+}
+
+
+LOCAL void
+set_bottom_pointers (j_decompress_ptr cinfo)
+/* Change the pointer lists to duplicate the last sample row at the bottom
+ * of the image.  whichptr indicates which xbuffer holds the final iMCU row.
+ * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
+ */
+{
+  my_main_ptr main = (my_main_ptr) cinfo->main;
+  int ci, i, rgroup, iMCUheight, rows_left;
+  jpeg_component_info *compptr;
+  JSAMPARRAY xbuf;
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    /* Count sample rows in one iMCU row and in one row group */
+    iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
+    rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
+    /* Count nondummy sample rows remaining for this component */
+    rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
+    if (rows_left == 0) rows_left = iMCUheight;
+    /* Count nondummy row groups.  Should get same answer for each component,
+     * so we need only do it once.
+     */
+    if (ci == 0) {
+      main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
+    }
+    /* Duplicate the last real sample row rgroup*2 times; this pads out the
+     * last partial rowgroup and ensures at least one full rowgroup of context.
+     */
+    xbuf = main->xbuffer[main->whichptr][ci];
+    for (i = 0; i < rgroup * 2; i++) {
+      xbuf[rows_left + i] = xbuf[rows_left-1];
+    }
+  }
+}
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF void
+start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+  my_main_ptr main = (my_main_ptr) cinfo->main;
+
+  /* Processing chunks are output rows except in JBUF_CRANK_SOURCE mode. */
+  main->pub.num_chunks = cinfo->output_height;
+
+  switch (pass_mode) {
+  case JBUF_PASS_THRU:
+    /* Do nothing if raw-data mode. */
+    if (cinfo->raw_data_out)
+      return;
+    if (cinfo->upsample->need_context_rows) {
+      main->pub.process_data = process_data_context_main;
+      make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
+      main->whichptr = 0;	/* Read first iMCU row into xbuffer[0] */
+      main->context_state = CTX_PREPARE_FOR_IMCU;
+      main->iMCU_row_ctr = 0;
+    } else {
+      /* Simple case with no context needed */
+      main->pub.process_data = process_data_simple_main;
+    }
+    main->buffer_full = FALSE;	/* Mark buffer empty */
+    main->rowgroup_ctr = 0;
+    break;
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+  case JBUF_CRANK_SOURCE:
+    /* Reading a multi-scan file, just crank the decompressor */
+    main->pub.process_data = process_data_input_only;
+    /* decompressor needs to be called once for each (equivalent) iMCU row */
+    main->pub.num_chunks = cinfo->total_iMCU_rows;
+    break;
+#endif
+#ifdef QUANT_2PASS_SUPPORTED
+  case JBUF_CRANK_DEST:
+    /* For last pass of 2-pass quantization, just crank the postprocessor */
+    main->pub.process_data = process_data_crank_post;
+    break;
+#endif
+  default:
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    break;
+  }
+}
+
+
+/*
+ * Process some data.
+ * This handles the simple case where no context is required.
+ */
+
+METHODDEF void
+process_data_simple_main (j_decompress_ptr cinfo,
+			  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+			  JDIMENSION out_rows_avail)
+{
+  my_main_ptr main = (my_main_ptr) cinfo->main;
+  JDIMENSION rowgroups_avail;
+
+  /* Read input data if we haven't filled the main buffer yet */
+  if (! main->buffer_full) {
+    if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer))
+      return;			/* suspension forced, can do nothing more */
+    main->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
+  }
+
+  /* There are always min_DCT_scaled_size row groups in an iMCU row. */
+  rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
+  /* Note: at the bottom of the image, we may pass extra garbage row groups
+   * to the postprocessor.  The postprocessor has to check for bottom
+   * of image anyway (at row resolution), so no point in us doing it too.
+   */
+
+  /* Feed the postprocessor */
+  (*cinfo->post->post_process_data) (cinfo, main->buffer,
+				     &main->rowgroup_ctr, rowgroups_avail,
+				     output_buf, out_row_ctr, out_rows_avail);
+
+  /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
+  if (main->rowgroup_ctr >= rowgroups_avail) {
+    main->buffer_full = FALSE;
+    main->rowgroup_ctr = 0;
+  }
+}
+
+
+/*
+ * Process some data.
+ * This handles the case where context rows must be provided.
+ */
+
+METHODDEF void
+process_data_context_main (j_decompress_ptr cinfo,
+			   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+			   JDIMENSION out_rows_avail)
+{
+  my_main_ptr main = (my_main_ptr) cinfo->main;
+
+  /* Read input data if we haven't filled the main buffer yet */
+  if (! main->buffer_full) {
+    if (! (*cinfo->coef->decompress_data) (cinfo,
+					   main->xbuffer[main->whichptr]))
+      return;			/* suspension forced, can do nothing more */
+    main->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
+    main->iMCU_row_ctr++;	/* count rows received */
+  }
+
+  /* Postprocessor typically will not swallow all the input data it is handed
+   * in one call (due to filling the output buffer first).  Must be prepared
+   * to exit and restart.  This switch lets us keep track of how far we got.
+   * Note that each case falls through to the next on successful completion.
+   */
+  switch (main->context_state) {
+  case CTX_POSTPONED_ROW:
+    /* Call postprocessor using previously set pointers for postponed row */
+    (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
+			&main->rowgroup_ctr, main->rowgroups_avail,
+			output_buf, out_row_ctr, out_rows_avail);
+    if (main->rowgroup_ctr < main->rowgroups_avail)
+      return;			/* Need to suspend */
+    main->context_state = CTX_PREPARE_FOR_IMCU;
+    if (*out_row_ctr >= out_rows_avail)
+      return;			/* Postprocessor exactly filled output buf */
+    /*FALLTHROUGH*/
+  case CTX_PREPARE_FOR_IMCU:
+    /* Prepare to process first M-1 row groups of this iMCU row */
+    main->rowgroup_ctr = 0;
+    main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
+    /* Check for bottom of image: if so, tweak pointers to "duplicate"
+     * the last sample row, and adjust rowgroups_avail to ignore padding rows.
+     */
+    if (main->iMCU_row_ctr == cinfo->total_iMCU_rows)
+      set_bottom_pointers(cinfo);
+    main->context_state = CTX_PROCESS_IMCU;
+    /*FALLTHROUGH*/
+  case CTX_PROCESS_IMCU:
+    /* Call postprocessor using previously set pointers */
+    (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
+			&main->rowgroup_ctr, main->rowgroups_avail,
+			output_buf, out_row_ctr, out_rows_avail);
+    if (main->rowgroup_ctr < main->rowgroups_avail)
+      return;			/* Need to suspend */
+    /* After the first iMCU, change wraparound pointers to normal state */
+    if (main->iMCU_row_ctr == 1)
+      set_wraparound_pointers(cinfo);
+    /* Prepare to load new iMCU row using other xbuffer list */
+    main->whichptr ^= 1;	/* 0=>1 or 1=>0 */
+    main->buffer_full = FALSE;
+    /* Still need to process last row group of this iMCU row, */
+    /* which is saved at index M+1 of the other xbuffer */
+    main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
+    main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
+    main->context_state = CTX_POSTPONED_ROW;
+  }
+}
+
+
+/*
+ * Process some data.
+ * Initial passes in a multiple-scan file: just call the decompressor,
+ * which will save data in its internal buffer, but return nothing.
+ */
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+
+METHODDEF void
+process_data_input_only (j_decompress_ptr cinfo,
+			 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+			 JDIMENSION out_rows_avail)
+{
+  if (! (*cinfo->coef->decompress_data) (cinfo, (JSAMPIMAGE) NULL))
+    return;			/* suspension forced, can do nothing more */
+  *out_row_ctr += 1;		/* OK, we did one iMCU row */
+}
+
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+
+
+/*
+ * Process some data.
+ * Final pass of two-pass quantization: just call the postprocessor.
+ * Source data will be the postprocessor controller's internal buffer.
+ */
+
+#ifdef QUANT_2PASS_SUPPORTED
+
+METHODDEF void
+process_data_crank_post (j_decompress_ptr cinfo,
+			 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+			 JDIMENSION out_rows_avail)
+{
+  (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
+				     (JDIMENSION *) NULL, (JDIMENSION) 0,
+				     output_buf, out_row_ctr, out_rows_avail);
+}
+
+#endif /* QUANT_2PASS_SUPPORTED */
+
+
+/*
+ * Initialize main buffer controller.
+ */
+
+GLOBAL void
+jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
+{
+  my_main_ptr main;
+  int ci, rgroup, ngroups;
+  jpeg_component_info *compptr;
+
+  main = (my_main_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_main_controller));
+  cinfo->main = (struct jpeg_d_main_controller *) main;
+  main->pub.start_pass = start_pass_main;
+
+  if (need_full_buffer)		/* shouldn't happen */
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+  /* In raw-data mode, we don't need a workspace.  This module doesn't
+   * do anything useful in that mode, except pass calls through to the
+   * coef controller in CRANK_SOURCE mode (ie, reading a multiscan file).
+   */
+  if (cinfo->raw_data_out)
+    return;
+
+  /* Allocate the workspace.
+   * ngroups is the number of row groups we need.
+   */
+  if (cinfo->upsample->need_context_rows) {
+    if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
+      ERREXIT(cinfo, JERR_NOTIMPL);
+    ngroups = cinfo->min_DCT_scaled_size + 2;
+  } else {
+    ngroups = cinfo->min_DCT_scaled_size;
+  }
+
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+      cinfo->min_DCT_scaled_size; /* height of a row group of component */
+    main->buffer[ci] = (*cinfo->mem->alloc_sarray)
+			((j_common_ptr) cinfo, JPOOL_IMAGE,
+			 compptr->width_in_blocks * compptr->DCT_scaled_size,
+			 (JDIMENSION) (rgroup * ngroups));
+  }
+}
diff --git a/jdmarker.c b/jdmarker.c
new file mode 100644
index 0000000..d42d4b9
--- /dev/null
+++ b/jdmarker.c
@@ -0,0 +1,1052 @@
+/*
+ * jdmarker.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to decode JPEG datastream markers.
+ * Most of the complexity arises from our desire to support input
+ * suspension: if not all of the data for a marker is available,
+ * we must exit back to the application.  On resumption, we reprocess
+ * the marker.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+typedef enum {			/* JPEG marker codes */
+  M_SOF0  = 0xc0,
+  M_SOF1  = 0xc1,
+  M_SOF2  = 0xc2,
+  M_SOF3  = 0xc3,
+  
+  M_SOF5  = 0xc5,
+  M_SOF6  = 0xc6,
+  M_SOF7  = 0xc7,
+  
+  M_JPG   = 0xc8,
+  M_SOF9  = 0xc9,
+  M_SOF10 = 0xca,
+  M_SOF11 = 0xcb,
+  
+  M_SOF13 = 0xcd,
+  M_SOF14 = 0xce,
+  M_SOF15 = 0xcf,
+  
+  M_DHT   = 0xc4,
+  
+  M_DAC   = 0xcc,
+  
+  M_RST0  = 0xd0,
+  M_RST1  = 0xd1,
+  M_RST2  = 0xd2,
+  M_RST3  = 0xd3,
+  M_RST4  = 0xd4,
+  M_RST5  = 0xd5,
+  M_RST6  = 0xd6,
+  M_RST7  = 0xd7,
+  
+  M_SOI   = 0xd8,
+  M_EOI   = 0xd9,
+  M_SOS   = 0xda,
+  M_DQT   = 0xdb,
+  M_DNL   = 0xdc,
+  M_DRI   = 0xdd,
+  M_DHP   = 0xde,
+  M_EXP   = 0xdf,
+  
+  M_APP0  = 0xe0,
+  M_APP1  = 0xe1,
+  M_APP2  = 0xe2,
+  M_APP3  = 0xe3,
+  M_APP4  = 0xe4,
+  M_APP5  = 0xe5,
+  M_APP6  = 0xe6,
+  M_APP7  = 0xe7,
+  M_APP8  = 0xe8,
+  M_APP9  = 0xe9,
+  M_APP10 = 0xea,
+  M_APP11 = 0xeb,
+  M_APP12 = 0xec,
+  M_APP13 = 0xed,
+  M_APP14 = 0xee,
+  M_APP15 = 0xef,
+  
+  M_JPG0  = 0xf0,
+  M_JPG13 = 0xfd,
+  M_COM   = 0xfe,
+  
+  M_TEM   = 0x01,
+  
+  M_ERROR = 0x100
+} JPEG_MARKER;
+
+
+/*
+ * Macros for fetching data from the data source module.
+ *
+ * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
+ * the current restart point; we update them only when we have reached a
+ * suitable place to restart if a suspension occurs.
+ */
+
+/* Declare and initialize local copies of input pointer/count */
+#define INPUT_VARS(cinfo)  \
+	struct jpeg_source_mgr * datasrc = (cinfo)->src;  \
+	const JOCTET * next_input_byte = datasrc->next_input_byte;  \
+	size_t bytes_in_buffer = datasrc->bytes_in_buffer
+
+/* Unload the local copies --- do this only at a restart boundary */
+#define INPUT_SYNC(cinfo)  \
+	( datasrc->next_input_byte = next_input_byte,  \
+	  datasrc->bytes_in_buffer = bytes_in_buffer )
+
+/* Reload the local copies --- seldom used except in MAKE_BYTE_AVAIL */
+#define INPUT_RELOAD(cinfo)  \
+	( next_input_byte = datasrc->next_input_byte,  \
+	  bytes_in_buffer = datasrc->bytes_in_buffer )
+
+/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
+ * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
+ * but we must reload the local copies after a successful fill.
+ */
+#define MAKE_BYTE_AVAIL(cinfo,action)  \
+	if (bytes_in_buffer == 0) {  \
+	  if (! (*datasrc->fill_input_buffer) (cinfo))  \
+	    { action; }  \
+	  INPUT_RELOAD(cinfo);  \
+	}  \
+	bytes_in_buffer--
+
+/* Read a byte into variable V.
+ * If must suspend, take the specified action (typically "return FALSE").
+ */
+#define INPUT_BYTE(cinfo,V,action)  \
+	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
+		  V = GETJOCTET(*next_input_byte++); )
+
+/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
+ * V should be declared unsigned int or perhaps INT32.
+ */
+#define INPUT_2BYTES(cinfo,V,action)  \
+	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
+		  V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
+		  MAKE_BYTE_AVAIL(cinfo,action); \
+		  V += GETJOCTET(*next_input_byte++); )
+
+
+/*
+ * Routines to process JPEG markers.
+ *
+ * Entry condition: JPEG marker itself has been read and its code saved
+ *   in cinfo->unread_marker; input restart point is just after the marker.
+ *
+ * Exit: if return TRUE, have read and processed any parameters, and have
+ *   updated the restart point to point after the parameters.
+ *   If return FALSE, was forced to suspend before reaching end of
+ *   marker parameters; restart point has not been moved.  Same routine
+ *   will be called again after application supplies more input data.
+ *
+ * This approach to suspension assumes that all of a marker's parameters can
+ * fit into a single input bufferload.  This should hold for "normal"
+ * markers.  Some COM/APPn markers might have large parameter segments,
+ * but we use skip_input_data to get past those, and thereby put the problem
+ * on the source manager's shoulders.
+ *
+ * Note that we don't bother to avoid duplicate trace messages if a
+ * suspension occurs within marker parameters.  Other side effects
+ * require more care.
+ */
+
+
+LOCAL boolean
+get_soi (j_decompress_ptr cinfo)
+/* Process an SOI marker */
+{
+  int i;
+  
+  TRACEMS(cinfo, 1, JTRC_SOI);
+
+  if (cinfo->marker->saw_SOI)
+    ERREXIT(cinfo, JERR_SOI_DUPLICATE);
+
+  /* Reset all parameters that are defined to be reset by SOI */
+
+  for (i = 0; i < NUM_ARITH_TBLS; i++) {
+    cinfo->arith_dc_L[i] = 0;
+    cinfo->arith_dc_U[i] = 1;
+    cinfo->arith_ac_K[i] = 5;
+  }
+  cinfo->restart_interval = 0;
+
+  /* Set initial assumptions for colorspace etc */
+
+  cinfo->jpeg_color_space = JCS_UNKNOWN;
+  cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
+
+  cinfo->saw_JFIF_marker = FALSE;
+  cinfo->density_unit = 0;	/* set default JFIF APP0 values */
+  cinfo->X_density = 1;
+  cinfo->Y_density = 1;
+  cinfo->saw_Adobe_marker = FALSE;
+  cinfo->Adobe_transform = 0;
+
+  cinfo->marker->saw_SOI = TRUE;
+
+  return TRUE;
+}
+
+
+LOCAL boolean
+get_sof (j_decompress_ptr cinfo)
+/* Process a SOFn marker */
+{
+  INT32 length;
+  int c, ci;
+  jpeg_component_info * compptr;
+  INPUT_VARS(cinfo);
+
+  INPUT_2BYTES(cinfo, length, return FALSE);
+
+  INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
+  INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
+  INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
+  INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
+
+  length -= 8;
+
+  TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
+	   (int) cinfo->image_width, (int) cinfo->image_height,
+	   cinfo->num_components);
+
+  if (cinfo->marker->saw_SOF)
+    ERREXIT(cinfo, JERR_SOF_DUPLICATE);
+
+  /* We don't support files in which the image height is initially specified */
+  /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
+  /* might as well have a general sanity check. */
+  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
+      || cinfo->num_components <= 0)
+    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
+
+  /* Make sure image isn't bigger than I can handle */
+  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
+      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
+    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
+
+  /* For now, precision must match compiled-in value... */
+  if (cinfo->data_precision != BITS_IN_JSAMPLE)
+    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
+
+  /* Check that number of components won't exceed internal array sizes */
+  if (cinfo->num_components > MAX_COMPONENTS)
+    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+	     MAX_COMPONENTS);
+
+  if (length != (cinfo->num_components * 3))
+    ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+  if (cinfo->comp_info == NULL)	/* do only once, even if suspend */
+    cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
+			((j_common_ptr) cinfo, JPOOL_IMAGE,
+			 cinfo->num_components * SIZEOF(jpeg_component_info));
+  
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    compptr->component_index = ci;
+    INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
+    INPUT_BYTE(cinfo, c, return FALSE);
+    compptr->h_samp_factor = (c >> 4) & 15;
+    compptr->v_samp_factor = (c     ) & 15;
+    INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
+
+    TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
+	     compptr->component_id, compptr->h_samp_factor,
+	     compptr->v_samp_factor, compptr->quant_tbl_no);
+  }
+
+  cinfo->marker->saw_SOF = TRUE;
+
+  INPUT_SYNC(cinfo);
+  return TRUE;
+}
+
+
+LOCAL boolean
+get_sos (j_decompress_ptr cinfo)
+/* Process a SOS marker */
+{
+  INT32 length;
+  int i, ci, n, c, cc, ccc;
+  jpeg_component_info * compptr;
+  INPUT_VARS(cinfo);
+
+  if (! cinfo->marker->saw_SOF)
+    ERREXIT(cinfo, JERR_SOS_NO_SOF);
+
+  INPUT_2BYTES(cinfo, length, return FALSE);
+
+  INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
+
+  if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
+    ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+  TRACEMS1(cinfo, 1, JTRC_SOS, n);
+
+  cinfo->comps_in_scan = n;
+
+  /* Collect the component-spec parameters */
+
+  for (i = 0; i < n; i++) {
+    INPUT_BYTE(cinfo, cc, return FALSE);
+    INPUT_BYTE(cinfo, c, return FALSE);
+    
+    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	 ci++, compptr++) {
+      if (cc == compptr->component_id)
+	goto id_found;
+    }
+
+    ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
+
+  id_found:
+
+    cinfo->cur_comp_info[i] = compptr;
+    compptr->dc_tbl_no = (c >> 4) & 15;
+    compptr->ac_tbl_no = (c     ) & 15;
+    
+    TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
+	     compptr->dc_tbl_no, compptr->ac_tbl_no);
+  }
+
+  /* Collect the additional scan parameters Ss, Se, Ah/Al.
+   * Currently we just validate that they are right for sequential JPEG.
+   * This ought to be an error condition, but we make it a warning because
+   * there are some baseline files out there with all zeroes in these bytes.
+   * (Thank you, Logitech :-(.)
+   */
+  INPUT_BYTE(cinfo, c, return FALSE);
+  INPUT_BYTE(cinfo, cc, return FALSE);
+  INPUT_BYTE(cinfo, ccc, return FALSE);
+  if (c != 0 || cc != DCTSIZE2-1 || ccc != 0)
+    WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
+
+  /* Prepare to scan data & restart markers */
+  cinfo->marker->next_restart_num = 0;
+
+  INPUT_SYNC(cinfo);
+  return TRUE;
+}
+
+
+METHODDEF boolean
+get_app0 (j_decompress_ptr cinfo)
+/* Process an APP0 marker */
+{
+#define JFIF_LEN 14
+  INT32 length;
+  UINT8 b[JFIF_LEN];
+  int buffp;
+  INPUT_VARS(cinfo);
+
+  INPUT_2BYTES(cinfo, length, return FALSE);
+  length -= 2;
+
+  /* See if a JFIF APP0 marker is present */
+
+  if (length >= JFIF_LEN) {
+    for (buffp = 0; buffp < JFIF_LEN; buffp++)
+      INPUT_BYTE(cinfo, b[buffp], return FALSE);
+    length -= JFIF_LEN;
+
+    if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) {
+      /* Found JFIF APP0 marker: check version */
+      /* Major version must be 1 */
+      if (b[5] != 1)
+	ERREXIT2(cinfo, JERR_JFIF_MAJOR, b[5], b[6]);
+      /* Minor version should be 0..2, but try to process anyway if newer */
+      if (b[6] > 2)
+	TRACEMS2(cinfo, 1, JTRC_JFIF_MINOR, b[5], b[6]);
+      /* Save info */
+      cinfo->saw_JFIF_marker = TRUE;
+      cinfo->density_unit = b[7];
+      cinfo->X_density = (b[8] << 8) + b[9];
+      cinfo->Y_density = (b[10] << 8) + b[11];
+      TRACEMS3(cinfo, 1, JTRC_JFIF,
+	       cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
+      if (b[12] | b[13])
+	TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL, b[12], b[13]);
+      if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))
+	TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) length);
+    } else {
+      /* Start of APP0 does not match "JFIF" */
+      TRACEMS1(cinfo, 1, JTRC_APP0, (int) length + JFIF_LEN);
+    }
+  } else {
+    /* Too short to be JFIF marker */
+    TRACEMS1(cinfo, 1, JTRC_APP0, (int) length);
+  }
+
+  INPUT_SYNC(cinfo);
+  if (length > 0)		/* skip any remaining data -- could be lots */
+    (*cinfo->src->skip_input_data) (cinfo, (long) length);
+
+  return TRUE;
+}
+
+
+METHODDEF boolean
+get_app14 (j_decompress_ptr cinfo)
+/* Process an APP14 marker */
+{
+#define ADOBE_LEN 12
+  INT32 length;
+  UINT8 b[ADOBE_LEN];
+  int buffp;
+  unsigned int version, flags0, flags1, transform;
+  INPUT_VARS(cinfo);
+
+  INPUT_2BYTES(cinfo, length, return FALSE);
+  length -= 2;
+
+  /* See if an Adobe APP14 marker is present */
+
+  if (length >= ADOBE_LEN) {
+    for (buffp = 0; buffp < ADOBE_LEN; buffp++)
+      INPUT_BYTE(cinfo, b[buffp], return FALSE);
+    length -= ADOBE_LEN;
+
+    if (b[0]==0x41 && b[1]==0x64 && b[2]==0x6F && b[3]==0x62 && b[4]==0x65) {
+      /* Found Adobe APP14 marker */
+      version = (b[5] << 8) + b[6];
+      flags0 = (b[7] << 8) + b[8];
+      flags1 = (b[9] << 8) + b[10];
+      transform = b[11];
+      TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
+      cinfo->saw_Adobe_marker = TRUE;
+      cinfo->Adobe_transform = (UINT8) transform;
+    } else {
+      /* Start of APP14 does not match "Adobe" */
+      TRACEMS1(cinfo, 1, JTRC_APP14, (int) length + ADOBE_LEN);
+    }
+  } else {
+    /* Too short to be Adobe marker */
+    TRACEMS1(cinfo, 1, JTRC_APP14, (int) length);
+  }
+
+  INPUT_SYNC(cinfo);
+  if (length > 0)		/* skip any remaining data -- could be lots */
+    (*cinfo->src->skip_input_data) (cinfo, (long) length);
+
+  return TRUE;
+}
+
+
+LOCAL boolean
+get_dac (j_decompress_ptr cinfo)
+/* Process a DAC marker */
+{
+  INT32 length;
+  int index, val;
+  INPUT_VARS(cinfo);
+
+  INPUT_2BYTES(cinfo, length, return FALSE);
+  length -= 2;
+  
+  while (length > 0) {
+    INPUT_BYTE(cinfo, index, return FALSE);
+    INPUT_BYTE(cinfo, val, return FALSE);
+
+    length -= 2;
+
+    TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
+
+    if (index < 0 || index >= (2*NUM_ARITH_TBLS))
+      ERREXIT1(cinfo, JERR_DAC_INDEX, index);
+
+    if (index >= NUM_ARITH_TBLS) { /* define AC table */
+      cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
+    } else {			/* define DC table */
+      cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
+      cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
+      if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
+	ERREXIT1(cinfo, JERR_DAC_VALUE, val);
+    }
+  }
+
+  INPUT_SYNC(cinfo);
+  return TRUE;
+}
+
+
+LOCAL boolean
+get_dht (j_decompress_ptr cinfo)
+/* Process a DHT marker */
+{
+  INT32 length;
+  UINT8 bits[17];
+  UINT8 huffval[256];
+  int i, index, count;
+  JHUFF_TBL **htblptr;
+  INPUT_VARS(cinfo);
+
+  INPUT_2BYTES(cinfo, length, return FALSE);
+  length -= 2;
+  
+  while (length > 0) {
+    INPUT_BYTE(cinfo, index, return FALSE);
+
+    TRACEMS1(cinfo, 1, JTRC_DHT, index);
+      
+    bits[0] = 0;
+    count = 0;
+    for (i = 1; i <= 16; i++) {
+      INPUT_BYTE(cinfo, bits[i], return FALSE);
+      count += bits[i];
+    }
+
+    length -= 1 + 16;
+
+    TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
+	     bits[1], bits[2], bits[3], bits[4],
+	     bits[5], bits[6], bits[7], bits[8]);
+    TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
+	     bits[9], bits[10], bits[11], bits[12],
+	     bits[13], bits[14], bits[15], bits[16]);
+
+    if (count > 256 || ((INT32) count) > length)
+      ERREXIT(cinfo, JERR_DHT_COUNTS);
+
+    for (i = 0; i < count; i++)
+      INPUT_BYTE(cinfo, huffval[i], return FALSE);
+
+    length -= count;
+
+    if (index & 0x10) {		/* AC table definition */
+      index -= 0x10;
+      htblptr = &cinfo->ac_huff_tbl_ptrs[index];
+    } else {			/* DC table definition */
+      htblptr = &cinfo->dc_huff_tbl_ptrs[index];
+    }
+
+    if (index < 0 || index >= NUM_HUFF_TBLS)
+      ERREXIT1(cinfo, JERR_DHT_INDEX, index);
+
+    if (*htblptr == NULL)
+      *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+  
+    MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
+    MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
+  }
+
+  INPUT_SYNC(cinfo);
+  return TRUE;
+}
+
+
+LOCAL boolean
+get_dqt (j_decompress_ptr cinfo)
+/* Process a DQT marker */
+{
+  INT32 length;
+  int n, i, prec;
+  unsigned int tmp;
+  JQUANT_TBL *quant_ptr;
+  INPUT_VARS(cinfo);
+
+  INPUT_2BYTES(cinfo, length, return FALSE);
+  length -= 2;
+
+  while (length > 0) {
+    INPUT_BYTE(cinfo, n, return FALSE);
+    prec = n >> 4;
+    n &= 0x0F;
+
+    TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
+
+    if (n >= NUM_QUANT_TBLS)
+      ERREXIT1(cinfo, JERR_DQT_INDEX, n);
+      
+    if (cinfo->quant_tbl_ptrs[n] == NULL)
+      cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
+    quant_ptr = cinfo->quant_tbl_ptrs[n];
+
+    for (i = 0; i < DCTSIZE2; i++) {
+      if (prec)
+	INPUT_2BYTES(cinfo, tmp, return FALSE);
+      else
+	INPUT_BYTE(cinfo, tmp, return FALSE);
+      quant_ptr->quantval[i] = (UINT16) tmp;
+    }
+
+    for (i = 0; i < DCTSIZE2; i += 8) {
+      TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
+	       quant_ptr->quantval[i  ], quant_ptr->quantval[i+1],
+	       quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
+	       quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
+	       quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
+    }
+
+    length -= DCTSIZE2+1;
+    if (prec) length -= DCTSIZE2;
+  }
+
+  INPUT_SYNC(cinfo);
+  return TRUE;
+}
+
+
+LOCAL boolean
+get_dri (j_decompress_ptr cinfo)
+/* Process a DRI marker */
+{
+  INT32 length;
+  unsigned int tmp;
+  INPUT_VARS(cinfo);
+
+  INPUT_2BYTES(cinfo, length, return FALSE);
+  
+  if (length != 4)
+    ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+  INPUT_2BYTES(cinfo, tmp, return FALSE);
+
+  TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
+
+  cinfo->restart_interval = tmp;
+
+  INPUT_SYNC(cinfo);
+  return TRUE;
+}
+
+
+METHODDEF boolean
+skip_variable (j_decompress_ptr cinfo)
+/* Skip over an unknown or uninteresting variable-length marker */
+{
+  INT32 length;
+  INPUT_VARS(cinfo);
+
+  INPUT_2BYTES(cinfo, length, return FALSE);
+  
+  TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
+
+  INPUT_SYNC(cinfo);		/* do before skip_input_data */
+  (*cinfo->src->skip_input_data) (cinfo, (long) length - 2L);
+
+  return TRUE;
+}
+
+
+/*
+ * Find the next JPEG marker, save it in cinfo->unread_marker.
+ * Returns FALSE if had to suspend before reaching a marker;
+ * in that case cinfo->unread_marker is unchanged.
+ *
+ * Note that the result might not be a valid marker code,
+ * but it will never be 0 or FF.
+ */
+
+LOCAL boolean
+next_marker (j_decompress_ptr cinfo)
+{
+  int c;
+  INPUT_VARS(cinfo);
+
+  for (;;) {
+    INPUT_BYTE(cinfo, c, return FALSE);
+    /* Skip any non-FF bytes.
+     * This may look a bit inefficient, but it will not occur in a valid file.
+     * We sync after each discarded byte so that a suspending data source
+     * can discard the byte from its buffer.
+     */
+    while (c != 0xFF) {
+      cinfo->marker->discarded_bytes++;
+      INPUT_SYNC(cinfo);
+      INPUT_BYTE(cinfo, c, return FALSE);
+    }
+    /* This loop swallows any duplicate FF bytes.  Extra FFs are legal as
+     * pad bytes, so don't count them in discarded_bytes.  We assume there
+     * will not be so many consecutive FF bytes as to overflow a suspending
+     * data source's input buffer.
+     */
+    do {
+      INPUT_BYTE(cinfo, c, return FALSE);
+    } while (c == 0xFF);
+    if (c != 0)
+      break;			/* found a valid marker, exit loop */
+    /* Reach here if we found a stuffed-zero data sequence (FF/00).
+     * Discard it and loop back to try again.
+     */
+    cinfo->marker->discarded_bytes += 2;
+    INPUT_SYNC(cinfo);
+  }
+
+  if (cinfo->marker->discarded_bytes != 0) {
+    WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
+    cinfo->marker->discarded_bytes = 0;
+  }
+
+  cinfo->unread_marker = c;
+
+  INPUT_SYNC(cinfo);
+  return TRUE;
+}
+
+
+LOCAL boolean
+first_marker (j_decompress_ptr cinfo)
+/* Like next_marker, but used to obtain the initial SOI marker. */
+/* For this marker, we do not allow preceding garbage or fill; otherwise,
+ * we might well scan an entire input file before realizing it ain't JPEG.
+ * If an application wants to process non-JFIF files, it must seek to the
+ * SOI before calling the JPEG library.
+ */
+{
+  int c, c2;
+  INPUT_VARS(cinfo);
+
+  INPUT_BYTE(cinfo, c, return FALSE);
+  INPUT_BYTE(cinfo, c2, return FALSE);
+  if (c != 0xFF || c2 != (int) M_SOI)
+    ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
+
+  cinfo->unread_marker = c2;
+
+  INPUT_SYNC(cinfo);
+  return TRUE;
+}
+
+
+/*
+ * Read markers until SOS or EOI.
+ *
+ * Returns same codes as are defined for jpeg_read_header,
+ * but HEADER_OK and HEADER_TABLES_ONLY merely indicate which marker type
+ * stopped the scan --- they do not necessarily mean the file is valid.
+ */
+
+METHODDEF int
+read_markers (j_decompress_ptr cinfo)
+{
+  /* Outer loop repeats once for each marker. */
+  for (;;) {
+    /* Collect the marker proper, unless we already did. */
+    /* NB: first_marker() enforces the requirement that SOI appear first. */
+    if (cinfo->unread_marker == 0) {
+      if (! cinfo->marker->saw_SOI) {
+	if (! first_marker(cinfo))
+	  return JPEG_SUSPENDED;
+      } else {
+	if (! next_marker(cinfo))
+	  return JPEG_SUSPENDED;
+      }
+    }
+    /* At this point cinfo->unread_marker contains the marker code and the
+     * input point is just past the marker proper, but before any parameters.
+     * A suspension will cause us to return with this state still true.
+     */
+    switch (cinfo->unread_marker) {
+    case M_SOI:
+      if (! get_soi(cinfo))
+	return JPEG_SUSPENDED;
+      break;
+
+    case M_SOF0:		/* Baseline */
+    case M_SOF1:		/* Extended sequential, Huffman */
+      cinfo->arith_code = FALSE;
+      if (! get_sof(cinfo))
+	return JPEG_SUSPENDED;
+      break;
+
+    case M_SOF9:		/* Extended sequential, arithmetic */
+      cinfo->arith_code = TRUE;
+      if (! get_sof(cinfo))
+	return JPEG_SUSPENDED;
+      break;
+
+    /* Currently unsupported SOFn types */
+    case M_SOF2:		/* Progressive, Huffman */
+    case M_SOF3:		/* Lossless, Huffman */
+    case M_SOF5:		/* Differential sequential, Huffman */
+    case M_SOF6:		/* Differential progressive, Huffman */
+    case M_SOF7:		/* Differential lossless, Huffman */
+    case M_JPG:			/* Reserved for JPEG extensions */
+    case M_SOF10:		/* Progressive, arithmetic */
+    case M_SOF11:		/* Lossless, arithmetic */
+    case M_SOF13:		/* Differential sequential, arithmetic */
+    case M_SOF14:		/* Differential progressive, arithmetic */
+    case M_SOF15:		/* Differential lossless, arithmetic */
+      ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
+      break;
+
+    case M_SOS:
+      if (! get_sos(cinfo))
+	return JPEG_SUSPENDED;
+      cinfo->unread_marker = 0;	/* processed the marker */
+      return JPEG_HEADER_OK;	/* return value for SOS found */
+    
+    case M_EOI:
+      TRACEMS(cinfo, 1, JTRC_EOI);
+      cinfo->unread_marker = 0;	/* processed the marker */
+      return JPEG_HEADER_TABLES_ONLY; /* return value for EOI found */
+      
+    case M_DAC:
+      if (! get_dac(cinfo))
+	return JPEG_SUSPENDED;
+      break;
+      
+    case M_DHT:
+      if (! get_dht(cinfo))
+	return JPEG_SUSPENDED;
+      break;
+      
+    case M_DQT:
+      if (! get_dqt(cinfo))
+	return JPEG_SUSPENDED;
+      break;
+      
+    case M_DRI:
+      if (! get_dri(cinfo))
+	return JPEG_SUSPENDED;
+      break;
+      
+    case M_APP0:
+    case M_APP1:
+    case M_APP2:
+    case M_APP3:
+    case M_APP4:
+    case M_APP5:
+    case M_APP6:
+    case M_APP7:
+    case M_APP8:
+    case M_APP9:
+    case M_APP10:
+    case M_APP11:
+    case M_APP12:
+    case M_APP13:
+    case M_APP14:
+    case M_APP15:
+      if (! (*cinfo->marker->process_APPn[cinfo->unread_marker - (int) M_APP0]) (cinfo))
+	return JPEG_SUSPENDED;
+      break;
+      
+    case M_COM:
+      if (! (*cinfo->marker->process_COM) (cinfo))
+	return JPEG_SUSPENDED;
+      break;
+
+    case M_RST0:		/* these are all parameterless */
+    case M_RST1:
+    case M_RST2:
+    case M_RST3:
+    case M_RST4:
+    case M_RST5:
+    case M_RST6:
+    case M_RST7:
+    case M_TEM:
+      TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
+      break;
+
+    case M_DNL:			/* Ignore DNL ... perhaps the wrong thing */
+      if (! skip_variable(cinfo))
+	return JPEG_SUSPENDED;
+      break;
+
+    default:			/* must be DHP, EXP, JPGn, or RESn */
+      /* For now, we treat the reserved markers as fatal errors since they are
+       * likely to be used to signal incompatible JPEG Part 3 extensions.
+       * Once the JPEG 3 version-number marker is well defined, this code
+       * ought to change!
+       */
+      ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
+      break;
+    }
+    /* Successfully processed marker, so reset state variable */
+    cinfo->unread_marker = 0;
+  } /* end loop */
+}
+
+
+/*
+ * Read a restart marker, which is expected to appear next in the datastream;
+ * if the marker is not there, take appropriate recovery action.
+ * Returns FALSE if suspension is required.
+ *
+ * This is called by the entropy decoder after it has read an appropriate
+ * number of MCUs.  cinfo->unread_marker may be nonzero if the entropy decoder
+ * has already read a marker from the data source.  Under normal conditions
+ * cinfo->unread_marker will be reset to 0 before returning; if not reset,
+ * it holds a marker which the decoder will be unable to read past.
+ */
+
+METHODDEF boolean
+read_restart_marker (j_decompress_ptr cinfo)
+{
+  /* Obtain a marker unless we already did. */
+  /* Note that next_marker will complain if it skips any data. */
+  if (cinfo->unread_marker == 0) {
+    if (! next_marker(cinfo))
+      return FALSE;
+  }
+
+  if (cinfo->unread_marker ==
+      ((int) M_RST0 + cinfo->marker->next_restart_num)) {
+    /* Normal case --- swallow the marker and let entropy decoder continue */
+    TRACEMS1(cinfo, 2, JTRC_RST, cinfo->marker->next_restart_num);
+    cinfo->unread_marker = 0;
+  } else {
+    /* Uh-oh, the restart markers have been messed up. */
+    /* Let the data source manager determine how to resync. */
+    if (! (*cinfo->src->resync_to_restart) (cinfo))
+      return FALSE;
+  }
+
+  /* Update next-restart state */
+  cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
+
+  return TRUE;
+}
+
+
+/*
+ * This is the default resync_to_restart method for data source managers
+ * to use if they don't have any better approach.  Some data source managers
+ * may be able to back up, or may have additional knowledge about the data
+ * which permits a more intelligent recovery strategy; such managers would
+ * presumably supply their own resync method.
+ *
+ * read_restart_marker calls resync_to_restart if it finds a marker other than
+ * the restart marker it was expecting.  (This code is *not* used unless
+ * a nonzero restart interval has been declared.)  cinfo->unread_marker is
+ * the marker code actually found (might be anything, except 0 or FF).
+ * The desired restart marker is indicated by cinfo->marker->next_restart_num.
+ * This routine is supposed to apply whatever error recovery strategy seems
+ * appropriate in order to position the input stream to the next data segment.
+ * Note that cinfo->unread_marker is treated as a marker appearing before
+ * the current data-source input point; usually it should be reset to zero
+ * before returning.
+ * Returns FALSE if suspension is required.
+ *
+ * This implementation is substantially constrained by wanting to treat the
+ * input as a data stream; this means we can't back up.  Therefore, we have
+ * only the following actions to work with:
+ *   1. Simply discard the marker and let the entropy decoder resume at next
+ *      byte of file.
+ *   2. Read forward until we find another marker, discarding intervening
+ *      data.  (In theory we could look ahead within the current bufferload,
+ *      without having to discard data if we don't find the desired marker.
+ *      This idea is not implemented here, in part because it makes behavior
+ *      dependent on buffer size and chance buffer-boundary positions.)
+ *   3. Leave the marker unread (by failing to zero cinfo->unread_marker).
+ *      This will cause the entropy decoder to process an empty data segment,
+ *      inserting dummy zeroes, and then we will reprocess the marker.
+ *
+ * #2 is appropriate if we think the desired marker lies ahead, while #3 is
+ * appropriate if the found marker is a future restart marker (indicating
+ * that we have missed the desired restart marker, probably because it got
+ * corrupted).
+ * We apply #2 or #3 if the found marker is a restart marker no more than
+ * two counts behind or ahead of the expected one.  We also apply #2 if the
+ * found marker is not a legal JPEG marker code (it's certainly bogus data).
+ * If the found marker is a restart marker more than 2 counts away, we do #1
+ * (too much risk that the marker is erroneous; with luck we will be able to
+ * resync at some future point).
+ * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
+ * overrunning the end of a scan.  An implementation limited to single-scan
+ * files might find it better to apply #2 for markers other than EOI, since
+ * any other marker would have to be bogus data in that case.
+ */
+
+GLOBAL boolean
+jpeg_resync_to_restart (j_decompress_ptr cinfo)
+{
+  int marker = cinfo->unread_marker;
+  int desired = cinfo->marker->next_restart_num;
+  int action = 1;
+  
+  /* Always put up a warning. */
+  WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
+  
+  /* Outer loop handles repeated decision after scanning forward. */
+  for (;;) {
+    if (marker < (int) M_SOF0)
+      action = 2;		/* invalid marker */
+    else if (marker < (int) M_RST0 || marker > (int) M_RST7)
+      action = 3;		/* valid non-restart marker */
+    else {
+      if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
+	  marker == ((int) M_RST0 + ((desired+2) & 7)))
+	action = 3;		/* one of the next two expected restarts */
+      else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
+	       marker == ((int) M_RST0 + ((desired-2) & 7)))
+	action = 2;		/* a prior restart, so advance */
+      else
+	action = 1;		/* desired restart or too far away */
+    }
+    TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
+    switch (action) {
+    case 1:
+      /* Discard marker and let entropy decoder resume processing. */
+      cinfo->unread_marker = 0;
+      return TRUE;
+    case 2:
+      /* Scan to the next marker, and repeat the decision loop. */
+      if (! next_marker(cinfo))
+	return FALSE;
+      marker = cinfo->unread_marker;
+      break;
+    case 3:
+      /* Return without advancing past this marker. */
+      /* Entropy decoder will be forced to process an empty segment. */
+      return TRUE;
+    }
+  } /* end loop */
+}
+
+
+/*
+ * Reset marker processing state to begin a fresh datastream.
+ */
+
+METHODDEF void
+reset_marker_reader (j_decompress_ptr cinfo)
+{
+  cinfo->unread_marker = 0;	    /* no pending marker */
+  cinfo->marker->saw_SOI = FALSE;   /* set internal state too */
+  cinfo->marker->saw_SOF = FALSE;
+  cinfo->marker->discarded_bytes = 0;
+  cinfo->comp_info = NULL;	    /* until allocated by get_sof */
+}
+
+
+/*
+ * Initialize the marker reader module.
+ */
+
+GLOBAL void
+jinit_marker_reader (j_decompress_ptr cinfo)
+{
+  int i;
+
+  /* Create subobject in permanent pool */
+  if (cinfo->marker == NULL) {	/* first time for this JPEG object? */
+    cinfo->marker = (struct jpeg_marker_reader *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+				  SIZEOF(struct jpeg_marker_reader));
+  }
+  /* Initialize method pointers */
+  cinfo->marker->reset_marker_reader = reset_marker_reader;
+  cinfo->marker->read_markers = read_markers;
+  cinfo->marker->read_restart_marker = read_restart_marker;
+  cinfo->marker->process_COM = skip_variable;
+  for (i = 0; i < 16; i++)
+    cinfo->marker->process_APPn[i] = skip_variable;
+  cinfo->marker->process_APPn[0] = get_app0;
+  cinfo->marker->process_APPn[14] = get_app14;
+  /* Reset marker processing state */
+  reset_marker_reader(cinfo);
+}
diff --git a/jdmaster.c b/jdmaster.c
index a490d4d..ca579da 100644
--- a/jdmaster.c
+++ b/jdmaster.c
@@ -1,176 +1,648 @@
 /*
  * jdmaster.c
  *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
- * This file contains the main control for the JPEG decompressor.
- * The system-dependent (user interface) code should call jpeg_decompress()
- * after doing appropriate setup of the decompress_info_struct parameter.
+ * This file contains master control logic for the JPEG decompressor.
+ * These routines are concerned with selecting the modules to be executed
+ * and with determining the number of passes and the work to be done in each
+ * pass.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
 
 
-METHODDEF void
-d_per_scan_method_selection (decompress_info_ptr cinfo)
-/* Central point for per-scan method selection */
+/* Private state */
+
+typedef enum {
+	main_pass,		/* read and process a single-scan file */
+	preread_pass,		/* read one scan of a multi-scan file */
+	output_pass,		/* primary processing pass for multi-scan */
+	post_pass		/* optional post-pass for 2-pass quant. */
+} D_PASS_TYPE;
+
+typedef struct {
+  struct jpeg_decomp_master pub; /* public fields */
+
+  boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
+
+  D_PASS_TYPE pass_type;	/* the type of the current pass */
+
+  int pass_number;		/* # of passes completed */
+  int total_passes;		/* estimated total # of passes needed */
+
+  boolean need_post_pass;	/* are we using full two-pass quantization? */
+} my_decomp_master;
+
+typedef my_decomp_master * my_master_ptr;
+
+
+/*
+ * Determine whether merged upsample/color conversion should be used.
+ * CRUCIAL: this must match the actual capabilities of jdmerge.c!
+ */
+
+LOCAL boolean
+use_merged_upsample (j_decompress_ptr cinfo)
 {
-  /* MCU disassembly */
-  jseldmcu(cinfo);
-  /* Upsampling of pixels */
-  jselupsample(cinfo);
+#ifdef UPSAMPLE_MERGING_SUPPORTED
+  /* Merging is the equivalent of plain box-filter upsampling */
+  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
+    return FALSE;
+  /* jdmerge.c only supports YCC=>RGB color conversion */
+  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
+      cinfo->out_color_space != JCS_RGB ||
+      cinfo->out_color_components != RGB_PIXELSIZE)
+    return FALSE;
+  /* and it only handles 2h1v or 2h2v sampling ratios */
+  if (cinfo->comp_info[0].h_samp_factor != 2 ||
+      cinfo->comp_info[1].h_samp_factor != 1 ||
+      cinfo->comp_info[2].h_samp_factor != 1 ||
+      cinfo->comp_info[0].v_samp_factor >  2 ||
+      cinfo->comp_info[1].v_samp_factor != 1 ||
+      cinfo->comp_info[2].v_samp_factor != 1)
+    return FALSE;
+  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
+  if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
+      cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
+      cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
+    return FALSE;
+  /* ??? also need to test for upsample-time rescaling, when & if supported */
+  /* by golly, it'll work... */
+  return TRUE;
+#else
+  return FALSE;
+#endif
 }
 
 
-LOCAL void
-d_initial_method_selection (decompress_info_ptr cinfo)
-/* Central point for initial method selection (after reading file header) */
+/*
+ * Support routines that do various essential calculations.
+ *
+ * jpeg_calc_output_dimensions is exported for possible use by application.
+ * Hence it mustn't do anything that can't be done twice.
+ */
+
+GLOBAL void
+jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
+/* Do computations that are needed before master selection phase */
 {
-  /* JPEG file scanning method selection is already done. */
-  /* So is output file format selection (both are done by user interface). */
-
-  /* Gamma and color space conversion */
-  /* NB: this may change the component_needed flags */
-  jseldcolor(cinfo);
-
-  /* Color quantization selection rules */
-#ifdef QUANT_1PASS_SUPPORTED
-#ifdef QUANT_2PASS_SUPPORTED
-  /* We have both, check for conditions in which 1-pass should be used */
-  if (cinfo->num_components != 3 || cinfo->jpeg_color_space != CS_YCbCr)
-    cinfo->two_pass_quantize = FALSE; /* 2-pass only handles YCbCr input */
-  if (cinfo->out_color_space == CS_GRAYSCALE)
-    cinfo->two_pass_quantize = FALSE; /* Should use 1-pass for grayscale out */
-#else /* not QUANT_2PASS_SUPPORTED */
-  cinfo->two_pass_quantize = FALSE; /* only have 1-pass */
-#endif
-#else /* not QUANT_1PASS_SUPPORTED */
-#ifdef QUANT_2PASS_SUPPORTED
-  cinfo->two_pass_quantize = TRUE; /* only have 2-pass */
-#else /* not QUANT_2PASS_SUPPORTED */
-  if (cinfo->quantize_colors) {
-    ERREXIT(cinfo->emethods, "Color quantization was not compiled");
-  }
-#endif
-#endif
-
-#ifdef QUANT_1PASS_SUPPORTED
-  jsel1quantize(cinfo);
-#endif
-#ifdef QUANT_2PASS_SUPPORTED
-  jsel2quantize(cinfo);
-#endif
-
-  /* Cross-block smoothing */
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-  jselbsmooth(cinfo);
-#else
-  cinfo->do_block_smoothing = FALSE;
-#endif
-
-  /* Entropy decoding: either Huffman or arithmetic coding. */
-#ifdef D_ARITH_CODING_SUPPORTED
-  jseldarithmetic(cinfo);
-#else
-  if (cinfo->arith_code) {
-    ERREXIT(cinfo->emethods, "Arithmetic coding not supported");
-  }
-#endif
-  jseldhuffman(cinfo);
-
-  /* Pipeline control */
-  jseldpipeline(cinfo);
-  /* Overall control (that's me!) */
-  cinfo->methods->d_per_scan_method_selection = d_per_scan_method_selection;
-}
-
-
-LOCAL void
-initial_setup (decompress_info_ptr cinfo)
-/* Do computations that are needed before initial method selection */
-{
-  short ci;
+  int ci;
   jpeg_component_info *compptr;
 
   /* Compute maximum sampling factors; check factor validity */
   cinfo->max_h_samp_factor = 1;
   cinfo->max_v_samp_factor = 1;
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    compptr = &cinfo->comp_info[ci];
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
 	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
-      ERREXIT(cinfo->emethods, "Bogus sampling factors");
+      ERREXIT(cinfo, JERR_BAD_SAMPLING);
     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
 				   compptr->h_samp_factor);
     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
 				   compptr->v_samp_factor);
-
   }
 
-  /* Compute logical downsampled dimensions of components */
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    compptr = &cinfo->comp_info[ci];
-    compptr->true_comp_width = (cinfo->image_width * compptr->h_samp_factor
-				+ cinfo->max_h_samp_factor - 1)
-				/ cinfo->max_h_samp_factor;
-    compptr->true_comp_height = (cinfo->image_height * compptr->v_samp_factor
-				 + cinfo->max_v_samp_factor - 1)
-				 / cinfo->max_v_samp_factor;
+  /* Compute actual output image dimensions and DCT scaling choices. */
+#ifdef IDCT_SCALING_SUPPORTED
+  if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
+    /* Provide 1/8 scaling */
+    cinfo->output_width = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_width, 8L);
+    cinfo->output_height = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_height, 8L);
+    cinfo->min_DCT_scaled_size = 1;
+  } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
+    /* Provide 1/4 scaling */
+    cinfo->output_width = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_width, 4L);
+    cinfo->output_height = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_height, 4L);
+    cinfo->min_DCT_scaled_size = 2;
+  } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
+    /* Provide 1/2 scaling */
+    cinfo->output_width = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_width, 2L);
+    cinfo->output_height = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_height, 2L);
+    cinfo->min_DCT_scaled_size = 4;
+  } else {
+    /* Provide 1/1 scaling */
+    cinfo->output_width = cinfo->image_width;
+    cinfo->output_height = cinfo->image_height;
+    cinfo->min_DCT_scaled_size = DCTSIZE;
+  }
+  /* In selecting the actual DCT scaling for each component, we try to
+   * scale up the chroma components via IDCT scaling rather than upsampling.
+   * This saves time if the upsampler gets to use 1:1 scaling.
+   * Note this code assumes that the supported DCT scalings are powers of 2.
+   */
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    int ssize = cinfo->min_DCT_scaled_size;
+    while (ssize < DCTSIZE &&
+	   (compptr->h_samp_factor * ssize * 2 <=
+	    cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
+	   (compptr->v_samp_factor * ssize * 2 <=
+	    cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
+      ssize = ssize * 2;
+    }
+    compptr->DCT_scaled_size = ssize;
+  }
+#else /* !IDCT_SCALING_SUPPORTED */
+  /* Hardwire it to "no scaling" */
+  cinfo->output_width = cinfo->image_width;
+  cinfo->output_height = cinfo->image_height;
+  cinfo->min_DCT_scaled_size = DCTSIZE;
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    compptr->DCT_scaled_size = DCTSIZE;
+  }
+#endif /* IDCT_SCALING_SUPPORTED */
+
+  /* Report number of components in selected colorspace. */
+  /* Probably this should be in the color conversion module... */
+  switch (cinfo->out_color_space) {
+  case JCS_GRAYSCALE:
+    cinfo->out_color_components = 1;
+    break;
+  case JCS_RGB:
+#if RGB_PIXELSIZE != 3
+    cinfo->out_color_components = RGB_PIXELSIZE;
+    break;
+#endif /* else share code with YCbCr */
+  case JCS_YCbCr:
+    cinfo->out_color_components = 3;
+    break;
+  case JCS_CMYK:
+  case JCS_YCCK:
+    cinfo->out_color_components = 4;
+    break;
+  default:			/* else must be same colorspace as in file */
+    cinfo->out_color_components = cinfo->num_components;
+    break;
+  }
+  cinfo->output_components = (cinfo->quantize_colors ? 1 :
+			      cinfo->out_color_components);
+
+  /* See if upsampler will want to emit more than one row at a time */
+  if (use_merged_upsample(cinfo))
+    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
+  else
+    cinfo->rec_outbuf_height = 1;
+
+  /* Compute various sampling-related dimensions.
+   * Some of these are of interest to the application if it is dealing with
+   * "raw" (not upsampled) output, so we do the calculations here.
+   */
+
+  /* Compute dimensions of components */
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    /* Size in DCT blocks */
+    compptr->width_in_blocks = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
+    compptr->height_in_blocks = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
+    /* Size in samples, after IDCT scaling */
+    compptr->downsampled_width = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_width *
+		    (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
+		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
+    compptr->downsampled_height = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_height *
+		    (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
+		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
+    /* Mark component needed, until color conversion says otherwise */
+    compptr->component_needed = TRUE;
+  }
+
+  /* Compute number of fully interleaved MCU rows (number of times that
+   * main controller will call coefficient controller).
+   */
+  cinfo->total_iMCU_rows = (JDIMENSION)
+    jdiv_round_up((long) cinfo->image_height,
+		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
+}
+
+
+LOCAL void
+per_scan_setup (j_decompress_ptr cinfo)
+/* Do computations that are needed before processing a JPEG scan */
+/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
+{
+  int ci, mcublks, tmp;
+  jpeg_component_info *compptr;
+  
+  if (cinfo->comps_in_scan == 1) {
+    
+    /* Noninterleaved (single-component) scan */
+    compptr = cinfo->cur_comp_info[0];
+    
+    /* Overall image size in MCUs */
+    cinfo->MCUs_per_row = compptr->width_in_blocks;
+    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
+    
+    /* For noninterleaved scan, always one block per MCU */
+    compptr->MCU_width = 1;
+    compptr->MCU_height = 1;
+    compptr->MCU_blocks = 1;
+    compptr->MCU_sample_width = compptr->DCT_scaled_size;
+    compptr->last_col_width = 1;
+    compptr->last_row_height = 1;
+    
+    /* Prepare array describing MCU composition */
+    cinfo->blocks_in_MCU = 1;
+    cinfo->MCU_membership[0] = 0;
+    
+  } else {
+    
+    /* Interleaved (multi-component) scan */
+    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
+      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
+	       MAX_COMPS_IN_SCAN);
+    
+    /* Overall image size in MCUs */
+    cinfo->MCUs_per_row = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_width,
+		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
+    cinfo->MCU_rows_in_scan = (JDIMENSION)
+      jdiv_round_up((long) cinfo->image_height,
+		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
+    
+    cinfo->blocks_in_MCU = 0;
+    
+    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+      compptr = cinfo->cur_comp_info[ci];
+      /* Sampling factors give # of blocks of component in each MCU */
+      compptr->MCU_width = compptr->h_samp_factor;
+      compptr->MCU_height = compptr->v_samp_factor;
+      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
+      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
+      /* Figure number of non-dummy blocks in last MCU column & row */
+      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
+      if (tmp == 0) tmp = compptr->MCU_width;
+      compptr->last_col_width = tmp;
+      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
+      if (tmp == 0) tmp = compptr->MCU_height;
+      compptr->last_row_height = tmp;
+      /* Prepare array describing MCU composition */
+      mcublks = compptr->MCU_blocks;
+      if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
+	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
+      while (mcublks-- > 0) {
+	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
+      }
+    }
+    
   }
 }
 
 
 /*
- * This is the main entry point to the JPEG decompressor.
+ * Several decompression processes need to range-limit values to the range
+ * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
+ * due to noise introduced by quantization, roundoff error, etc.  These
+ * processes are inner loops and need to be as fast as possible.  On most
+ * machines, particularly CPUs with pipelines or instruction prefetch,
+ * a (subscript-check-less) C table lookup
+ *		x = sample_range_limit[x];
+ * is faster than explicit tests
+ *		if (x < 0)  x = 0;
+ *		else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
+ * These processes all use a common table prepared by the routine below.
+ *
+ * For most steps we can mathematically guarantee that the initial value
+ * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
+ * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
+ * limiting step (just after the IDCT), a wildly out-of-range value is 
+ * possible if the input data is corrupt.  To avoid any chance of indexing
+ * off the end of memory and getting a bad-pointer trap, we perform the
+ * post-IDCT limiting thus:
+ *		x = range_limit[x & MASK];
+ * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
+ * samples.  Under normal circumstances this is more than enough range and
+ * a correct output will be generated; with bogus input data the mask will
+ * cause wraparound, and we will safely generate a bogus-but-in-range output.
+ * For the post-IDCT step, we want to convert the data from signed to unsigned
+ * representation by adding CENTERJSAMPLE at the same time that we limit it.
+ * So the post-IDCT limiting table ends up looking like this:
+ *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
+ *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
+ *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
+ *   0,1,...,CENTERJSAMPLE-1
+ * Negative inputs select values from the upper half of the table after
+ * masking.
+ *
+ * We can save some space by overlapping the start of the post-IDCT table
+ * with the simpler range limiting table.  The post-IDCT table begins at
+ * sample_range_limit + CENTERJSAMPLE.
+ *
+ * Note that the table is allocated in near data space on PCs; it's small
+ * enough and used often enough to justify this.
  */
 
+LOCAL void
+prepare_range_limit_table (j_decompress_ptr cinfo)
+/* Allocate and fill in the sample_range_limit table */
+{
+  JSAMPLE * table;
+  int i;
+
+  table = (JSAMPLE *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+		(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
+  table += (MAXJSAMPLE+1);	/* allow negative subscripts of simple table */
+  cinfo->sample_range_limit = table;
+  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
+  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
+  /* Main part of "simple" table: limit[x] = x */
+  for (i = 0; i <= MAXJSAMPLE; i++)
+    table[i] = (JSAMPLE) i;
+  table += CENTERJSAMPLE;	/* Point to where post-IDCT table starts */
+  /* End of simple table, rest of first half of post-IDCT table */
+  for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
+    table[i] = MAXJSAMPLE;
+  /* Second half of post-IDCT table */
+  MEMZERO(table + (2 * (MAXJSAMPLE+1)),
+	  (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
+  MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
+	  cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
+}
+
+
+/*
+ * Master selection of decompression modules.
+ * This is done once at the start of processing an image.  We determine
+ * which modules will be used and give them appropriate initialization calls.
+ *
+ * Note that this is called only after jpeg_read_header has finished.
+ * We therefore know what is in the SOF and (first) SOS markers.
+ */
+
+LOCAL void
+master_selection (j_decompress_ptr cinfo)
+{
+  my_master_ptr master = (my_master_ptr) cinfo->master;
+  long samplesperrow;
+  JDIMENSION jd_samplesperrow;
+
+  /* Initialize dimensions and other stuff */
+  jpeg_calc_output_dimensions(cinfo);
+  prepare_range_limit_table(cinfo);
+
+  /* Width of an output scanline must be representable as JDIMENSION. */
+  samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
+  jd_samplesperrow = (JDIMENSION) samplesperrow;
+  if ((long) jd_samplesperrow != samplesperrow)
+    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+
+  /* Initialize my private state */
+  master->pub.eoi_processed = FALSE;
+  master->pass_number = 0;
+  master->need_post_pass = FALSE;
+  if (cinfo->comps_in_scan == cinfo->num_components) {
+    master->pass_type = main_pass;
+    master->total_passes = 1;
+  } else {
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+    master->pass_type = preread_pass;
+    /* Assume there is a separate scan for each component; */
+    /* if partially interleaved, we'll increment pass_number appropriately */
+    master->total_passes = cinfo->num_components + 1;
+#else
+    ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+  }
+  master->using_merged_upsample = use_merged_upsample(cinfo);
+
+  /* There's not a lot of smarts here right now, but it'll get more
+   * complicated when we have multiple implementations available...
+   */
+
+  /* Color quantizer selection */
+  if (cinfo->quantize_colors) {
+    if (cinfo->raw_data_out)
+      ERREXIT(cinfo, JERR_NOTIMPL);
+#ifdef QUANT_2PASS_SUPPORTED
+    /* 2-pass quantizer only works in 3-component color space.
+     * We use the "2-pass" code in a single pass if a colormap is given.
+     */
+    if (cinfo->out_color_components != 3)
+      cinfo->two_pass_quantize = FALSE;
+    else if (cinfo->colormap != NULL)
+      cinfo->two_pass_quantize = TRUE;
+#else
+    /* Force 1-pass quantize if we don't have 2-pass code compiled. */
+    cinfo->two_pass_quantize = FALSE;
+#endif
+
+    if (cinfo->two_pass_quantize) {
+#ifdef QUANT_2PASS_SUPPORTED
+      if (cinfo->colormap == NULL) {
+	master->need_post_pass = TRUE;
+	master->total_passes++;
+      }
+      jinit_2pass_quantizer(cinfo);
+#else
+      ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+    } else {
+#ifdef QUANT_1PASS_SUPPORTED
+      jinit_1pass_quantizer(cinfo);
+#else
+      ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+    }
+  }
+
+  /* Post-processing: in particular, color conversion first */
+  if (! cinfo->raw_data_out) {
+    if (master->using_merged_upsample) {
+#ifdef UPSAMPLE_MERGING_SUPPORTED
+      jinit_merged_upsampler(cinfo); /* does color conversion too */
+#else
+      ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+    } else {
+      jinit_color_deconverter(cinfo);
+      jinit_upsampler(cinfo);
+    }
+    jinit_d_post_controller(cinfo, master->need_post_pass);
+  }
+  /* Inverse DCT */
+  jinit_inverse_dct(cinfo);
+  /* Entropy decoding: either Huffman or arithmetic coding. */
+  if (cinfo->arith_code) {
+#ifdef D_ARITH_CODING_SUPPORTED
+    jinit_arith_decoder(cinfo);
+#else
+    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+#endif
+  } else
+    jinit_huff_decoder(cinfo);
+
+  jinit_d_coef_controller(cinfo, (master->pass_type == preread_pass));
+  jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
+  /* Note that main controller is initialized even in raw-data mode. */
+
+  /* We can now tell the memory manager to allocate virtual arrays. */
+  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+}
+
+
+/*
+ * Per-pass setup.
+ * This is called at the beginning of each pass.  We determine which modules
+ * will be active during this pass and give them appropriate start_pass calls.
+ * We also set is_last_pass to indicate whether any more passes will be
+ * required.
+ */
+
+METHODDEF void
+prepare_for_pass (j_decompress_ptr cinfo)
+{
+  my_master_ptr master = (my_master_ptr) cinfo->master;
+
+  switch (master->pass_type) {
+  case main_pass:
+    /* Set up to read and decompress single-scan file in one pass */
+    per_scan_setup(cinfo);
+    master->pub.is_last_pass = ! master->need_post_pass;
+    if (! cinfo->raw_data_out) {
+      if (! master->using_merged_upsample)
+	(*cinfo->cconvert->start_pass) (cinfo);
+      (*cinfo->upsample->start_pass) (cinfo);
+      if (cinfo->quantize_colors)
+	(*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
+      (*cinfo->post->start_pass) (cinfo,
+	    (master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
+    }
+    (*cinfo->idct->start_input_pass) (cinfo);
+    (*cinfo->idct->start_output_pass) (cinfo);
+    (*cinfo->entropy->start_pass) (cinfo);
+    (*cinfo->coef->start_pass) (cinfo, JBUF_PASS_THRU);
+    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
+    break;
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+  case preread_pass:
+    /* Read (another) scan of a multi-scan file */
+    per_scan_setup(cinfo);
+    master->pub.is_last_pass = FALSE;
+    (*cinfo->idct->start_input_pass) (cinfo);
+    (*cinfo->entropy->start_pass) (cinfo);
+    (*cinfo->coef->start_pass) (cinfo, JBUF_SAVE_SOURCE);
+    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_SOURCE);
+    break;
+  case output_pass:
+    /* All scans read, now do the IDCT and subsequent processing */
+    master->pub.is_last_pass = ! master->need_post_pass;
+    if (! cinfo->raw_data_out) {
+      if (! master->using_merged_upsample)
+	(*cinfo->cconvert->start_pass) (cinfo);
+      (*cinfo->upsample->start_pass) (cinfo);
+      if (cinfo->quantize_colors)
+	(*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
+      (*cinfo->post->start_pass) (cinfo,
+	    (master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
+    }
+    (*cinfo->idct->start_output_pass) (cinfo);
+    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
+    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
+    break;
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+#ifdef QUANT_2PASS_SUPPORTED
+  case post_pass:
+    /* Final pass of 2-pass quantization */
+    master->pub.is_last_pass = TRUE;
+    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
+    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
+    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
+    break;
+#endif /* QUANT_2PASS_SUPPORTED */
+  default:
+    ERREXIT(cinfo, JERR_NOT_COMPILED);
+  }
+
+  /* Set up progress monitor's pass info if present */
+  if (cinfo->progress != NULL) {
+    cinfo->progress->completed_passes = master->pass_number;
+    cinfo->progress->total_passes = master->total_passes;
+  }
+}
+
+
+/*
+ * Finish up at end of pass.
+ * In multi-scan mode, we must read next scan header and set the next
+ * pass_type correctly for prepare_for_pass.
+ */
+
+METHODDEF void
+finish_pass_master (j_decompress_ptr cinfo)
+{
+  my_master_ptr master = (my_master_ptr) cinfo->master;
+
+  switch (master->pass_type) {
+  case main_pass:
+  case output_pass:
+    if (cinfo->quantize_colors)
+      (*cinfo->cquantize->finish_pass) (cinfo);
+    master->pass_number++;
+    master->pass_type = post_pass; /* in case need_post_pass is true */
+    break;
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+  case preread_pass:
+    /* Count one pass done for each component in this scan */
+    master->pass_number += cinfo->comps_in_scan;
+    switch ((*cinfo->marker->read_markers) (cinfo)) {
+    case JPEG_HEADER_OK:	/* Found SOS, do another preread pass */
+      break;
+    case JPEG_HEADER_TABLES_ONLY: /* Found EOI, no more preread passes */
+      master->pub.eoi_processed = TRUE;
+      master->pass_type = output_pass;
+      break;
+    case JPEG_SUSPENDED:
+      ERREXIT(cinfo, JERR_CANT_SUSPEND);
+    }
+    break;
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+#ifdef QUANT_2PASS_SUPPORTED
+  case post_pass:
+    (*cinfo->cquantize->finish_pass) (cinfo);
+    /* there will be no more passes, don't bother to change state */
+    break;
+#endif /* QUANT_2PASS_SUPPORTED */
+  default:
+    ERREXIT(cinfo, JERR_NOT_COMPILED);
+  }
+}
+
+
+/*
+ * Initialize master decompression control.
+ * This creates my own subrecord and also performs the master selection phase,
+ * which causes other modules to create their subrecords.
+ */
 
 GLOBAL void
-jpeg_decompress (decompress_info_ptr cinfo)
+jinit_master_decompress (j_decompress_ptr cinfo)
 {
-  /* Init pass counts to 0 --- total_passes is adjusted in method selection */
-  cinfo->total_passes = 0;
-  cinfo->completed_passes = 0;
+  my_master_ptr master;
 
-  /* Read the JPEG file header markers; everything up through the first SOS
-   * marker is read now.  NOTE: the user interface must have initialized the
-   * read_file_header method pointer (eg, by calling jselrjfif or jselrtiff).
-   * The other file reading methods (read_scan_header etc.) were probably
-   * set at the same time, but could be set up by read_file_header itself.
-   */
-  (*cinfo->methods->read_file_header) (cinfo);
-  if (! ((*cinfo->methods->read_scan_header) (cinfo)))
-    ERREXIT(cinfo->emethods, "Empty JPEG file");
+  master = (my_master_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(my_decomp_master));
+  cinfo->master = (struct jpeg_decomp_master *) master;
+  master->pub.prepare_for_pass = prepare_for_pass;
+  master->pub.finish_pass = finish_pass_master;
 
-  /* Give UI a chance to adjust decompression parameters and select */
-  /* output file format based on info from file header. */
-  (*cinfo->methods->d_ui_method_selection) (cinfo);
-
-  /* Now select methods for decompression steps. */
-  initial_setup(cinfo);
-  d_initial_method_selection(cinfo);
-
-  /* Initialize the output file & other modules as needed */
-  /* (modules needing per-scan init are called by pipeline controller) */
-
-  (*cinfo->methods->output_init) (cinfo);
-  (*cinfo->methods->colorout_init) (cinfo);
-  if (cinfo->quantize_colors)
-    (*cinfo->methods->color_quant_init) (cinfo);
-
-  /* And let the pipeline controller do the rest. */
-  (*cinfo->methods->d_pipeline_controller) (cinfo);
-
-  /* Finish output file, release working storage, etc */
-  if (cinfo->quantize_colors)
-    (*cinfo->methods->color_quant_term) (cinfo);
-  (*cinfo->methods->colorout_term) (cinfo);
-  (*cinfo->methods->output_term) (cinfo);
-  (*cinfo->methods->read_file_trailer) (cinfo);
-
-  (*cinfo->emethods->free_all) ();
-
-  /* My, that was easy, wasn't it? */
+  master_selection(cinfo);
 }
diff --git a/jdmcu.c b/jdmcu.c
deleted file mode 100644
index 951bb7e..0000000
--- a/jdmcu.c
+++ /dev/null
@@ -1,215 +0,0 @@
-/*
- * jdmcu.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains MCU disassembly and IDCT control routines.
- * These routines are invoked via the disassemble_MCU, reverse_DCT, and
- * disassemble_init/term methods.
- */
-
-#include "jinclude.h"
-
-
-/*
- * Fetch one MCU row from entropy_decode, build coefficient array.
- * This version is used for noninterleaved (single-component) scans.
- */
-
-METHODDEF void
-disassemble_noninterleaved_MCU (decompress_info_ptr cinfo,
-				JBLOCKIMAGE image_data)
-{
-  JBLOCKROW MCU_data[1];
-  long mcuindex;
-
-  /* this is pretty easy since there is one component and one block per MCU */
-
-  /* Pre-zero the target area to speed up entropy decoder */
-  /* (we assume wholesale zeroing is faster than retail) */
-  jzero_far((void FAR *) image_data[0][0],
-	    (size_t) (cinfo->MCUs_per_row * SIZEOF(JBLOCK)));
-
-  for (mcuindex = 0; mcuindex < cinfo->MCUs_per_row; mcuindex++) {
-    /* Point to the proper spot in the image array for this MCU */
-    MCU_data[0] = image_data[0][0] + mcuindex;
-    /* Fetch the coefficient data */
-    (*cinfo->methods->entropy_decode) (cinfo, MCU_data);
-  }
-}
-
-
-/*
- * Fetch one MCU row from entropy_decode, build coefficient array.
- * This version is used for interleaved (multi-component) scans.
- */
-
-METHODDEF void
-disassemble_interleaved_MCU (decompress_info_ptr cinfo,
-			     JBLOCKIMAGE image_data)
-{
-  JBLOCKROW MCU_data[MAX_BLOCKS_IN_MCU];
-  long mcuindex;
-  short blkn, ci, xpos, ypos;
-  jpeg_component_info * compptr;
-  JBLOCKROW image_ptr;
-
-  /* Pre-zero the target area to speed up entropy decoder */
-  /* (we assume wholesale zeroing is faster than retail) */
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
-    for (ypos = 0; ypos < compptr->MCU_height; ypos++) {
-      jzero_far((void FAR *) image_data[ci][ypos],
-		(size_t) (cinfo->MCUs_per_row * compptr->MCU_width * SIZEOF(JBLOCK)));
-    }
-  }
-
-  for (mcuindex = 0; mcuindex < cinfo->MCUs_per_row; mcuindex++) {
-    /* Point to the proper spots in the image array for this MCU */
-    blkn = 0;
-    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-      compptr = cinfo->cur_comp_info[ci];
-      for (ypos = 0; ypos < compptr->MCU_height; ypos++) {
-	image_ptr = image_data[ci][ypos] + (mcuindex * compptr->MCU_width);
-	for (xpos = 0; xpos < compptr->MCU_width; xpos++) {
-	  MCU_data[blkn] = image_ptr;
-	  image_ptr++;
-	  blkn++;
-	}
-      }
-    }
-    /* Fetch the coefficient data */
-    (*cinfo->methods->entropy_decode) (cinfo, MCU_data);
-  }
-}
-
-
-/*
- * Perform inverse DCT on each block in an MCU row's worth of data;
- * output the results into a sample array starting at row start_row.
- * NB: start_row can only be nonzero when dealing with a single-component
- * scan; otherwise we'd have to pass different offsets for different
- * components, since the heights of interleaved MCU rows can vary.
- * But the pipeline controller logic is such that this is not necessary.
- */
-
-METHODDEF void
-reverse_DCT (decompress_info_ptr cinfo,
-	     JBLOCKIMAGE coeff_data, JSAMPIMAGE output_data, int start_row)
-{
-  DCTBLOCK block;
-  JBLOCKROW browptr;
-  JSAMPARRAY srowptr;
-  jpeg_component_info * compptr;
-  long blocksperrow, bi;
-  short numrows, ri;
-  short ci;
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
-    /* don't bother to IDCT an uninteresting component */
-    if (! compptr->component_needed)
-      continue;
-    /* calculate size of an MCU row in this component */
-    blocksperrow = compptr->downsampled_width / DCTSIZE;
-    numrows = compptr->MCU_height;
-    /* iterate through all blocks in MCU row */
-    for (ri = 0; ri < numrows; ri++) {
-      browptr = coeff_data[ci][ri];
-      srowptr = output_data[ci] + (ri * DCTSIZE + start_row);
-      for (bi = 0; bi < blocksperrow; bi++) {
-	/* copy the data into a local DCTBLOCK.  This allows for change of
-	 * representation (if DCTELEM != JCOEF).  On 80x86 machines it also
-	 * brings the data back from FAR storage to NEAR storage.
-	 */
-	{ register JCOEFPTR elemptr = browptr[bi];
-	  register DCTELEM *localblkptr = block;
-	  register int elem = DCTSIZE2;
-
-	  while (--elem >= 0)
-	    *localblkptr++ = (DCTELEM) *elemptr++;
-	}
-
-	j_rev_dct(block);	/* perform inverse DCT */
-
-	/* Output the data into the sample array.
-	 * Note change from signed to unsigned representation:
-	 * DCT calculation works with values +-CENTERJSAMPLE,
-	 * but sample arrays always hold 0..MAXJSAMPLE.
-	 * We have to do range-limiting because of quantization errors in the
-	 * DCT/IDCT phase.  We use the sample_range_limit[] table to do this
-	 * quickly; the CENTERJSAMPLE offset is folded into table indexing.
-	 */
-	{ register JSAMPROW elemptr;
-	  register DCTELEM *localblkptr = block;
-	  register JSAMPLE *range_limit = cinfo->sample_range_limit +
-						CENTERJSAMPLE;
-#if DCTSIZE != 8
-	  register int elemc;
-#endif
-	  register int elemr;
-
-	  for (elemr = 0; elemr < DCTSIZE; elemr++) {
-	    elemptr = srowptr[elemr] + (bi * DCTSIZE);
-#if DCTSIZE == 8		/* unroll the inner loop */
-	    *elemptr++ = range_limit[*localblkptr++];
-	    *elemptr++ = range_limit[*localblkptr++];
-	    *elemptr++ = range_limit[*localblkptr++];
-	    *elemptr++ = range_limit[*localblkptr++];
-	    *elemptr++ = range_limit[*localblkptr++];
-	    *elemptr++ = range_limit[*localblkptr++];
-	    *elemptr++ = range_limit[*localblkptr++];
-	    *elemptr++ = range_limit[*localblkptr++];
-#else
-	    for (elemc = DCTSIZE; elemc > 0; elemc--) {
-	      *elemptr++ = range_limit[*localblkptr++];
-	    }
-#endif
-	  }
-	}
-      }
-    }
-  }
-}
-
-
-/*
- * Initialize for processing a scan.
- */
-
-METHODDEF void
-disassemble_init (decompress_info_ptr cinfo)
-{
-  /* no work for now */
-}
-
-
-/*
- * Clean up after a scan.
- */
-
-METHODDEF void
-disassemble_term (decompress_info_ptr cinfo)
-{
-  /* no work for now */
-}
-
-
-
-/*
- * The method selection routine for MCU disassembly.
- */
-
-GLOBAL void
-jseldmcu (decompress_info_ptr cinfo)
-{
-  if (cinfo->comps_in_scan == 1)
-    cinfo->methods->disassemble_MCU = disassemble_noninterleaved_MCU;
-  else
-    cinfo->methods->disassemble_MCU = disassemble_interleaved_MCU;
-  cinfo->methods->reverse_DCT = reverse_DCT;
-  cinfo->methods->disassemble_init = disassemble_init;
-  cinfo->methods->disassemble_term = disassemble_term;
-}
diff --git a/jdmerge.c b/jdmerge.c
new file mode 100644
index 0000000..a0b9cae
--- /dev/null
+++ b/jdmerge.c
@@ -0,0 +1,389 @@
+/*
+ * jdmerge.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains code for merged upsampling/color conversion.
+ *
+ * This file combines functions from jdsample.c and jdcolor.c;
+ * read those files first to understand what's going on.
+ *
+ * When the chroma components are to be upsampled by simple replication
+ * (ie, box filtering), we can save some work in color conversion by
+ * calculating all the output pixels corresponding to a pair of chroma
+ * samples at one time.  In the conversion equations
+ *	R = Y           + K1 * Cr
+ *	G = Y + K2 * Cb + K3 * Cr
+ *	B = Y + K4 * Cb
+ * only the Y term varies among the group of pixels corresponding to a pair
+ * of chroma samples, so the rest of the terms can be calculated just once.
+ * At typical sampling ratios, this eliminates half or three-quarters of the
+ * multiplications needed for color conversion.
+ *
+ * This file currently provides implementations for the following cases:
+ *	YCbCr => RGB color conversion only.
+ *	Sampling ratios of 2h1v or 2h2v.
+ *	No scaling needed at upsample time.
+ *	Corner-aligned (non-CCIR601) sampling alignment.
+ * Other special cases could be added, but in most applications these are
+ * the only common cases.  (For uncommon cases we fall back on the more
+ * general code in jdsample.c and jdcolor.c.)
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+#ifdef UPSAMPLE_MERGING_SUPPORTED
+
+
+/* Private subobject */
+
+typedef struct {
+  struct jpeg_upsampler pub;	/* public fields */
+
+  /* Pointer to routine to do actual upsampling/conversion of one row group */
+  JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
+			   JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
+			   JSAMPARRAY output_buf));
+
+  /* Private state for YCC->RGB conversion */
+  int * Cr_r_tab;		/* => table for Cr to R conversion */
+  int * Cb_b_tab;		/* => table for Cb to B conversion */
+  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
+  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
+
+  /* For 2:1 vertical sampling, we produce two output rows at a time.
+   * We need a "spare" row buffer to hold the second output row if the
+   * application provides just a one-row buffer; we also use the spare
+   * to discard the dummy last row if the image height is odd.
+   */
+  JSAMPROW spare_row;
+  boolean spare_full;		/* T if spare buffer is occupied */
+
+  JDIMENSION out_row_width;	/* samples per output row */
+  JDIMENSION rows_to_go;	/* counts rows remaining in image */
+} my_upsampler;
+
+typedef my_upsampler * my_upsample_ptr;
+
+#define SCALEBITS	16	/* speediest right-shift on some machines */
+#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
+#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
+
+
+/*
+ * Initialize for an upsampling pass.
+ */
+
+METHODDEF void
+start_pass_merged_upsample (j_decompress_ptr cinfo)
+{
+  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+  INT32 i, x2;
+  SHIFT_TEMPS
+
+  /* Mark the spare buffer empty */
+  upsample->spare_full = FALSE;
+  /* Initialize total-height counter for detecting bottom of image */
+  upsample->rows_to_go = cinfo->output_height;
+
+  /* Initialize the YCC=>RGB conversion tables.
+   * This is taken directly from jdcolor.c; see that file for more info.
+   */
+  upsample->Cr_r_tab = (int *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(MAXJSAMPLE+1) * SIZEOF(int));
+  upsample->Cb_b_tab = (int *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(MAXJSAMPLE+1) * SIZEOF(int));
+  upsample->Cr_g_tab = (INT32 *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(MAXJSAMPLE+1) * SIZEOF(INT32));
+  upsample->Cb_g_tab = (INT32 *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(MAXJSAMPLE+1) * SIZEOF(INT32));
+
+  for (i = 0; i <= MAXJSAMPLE; i++) {
+    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
+    /* The Cb or Cr value we are thinking of is x = i - MAXJSAMPLE/2 */
+    x2 = 2*i - MAXJSAMPLE;	/* twice x */
+    /* Cr=>R value is nearest int to 1.40200 * x */
+    upsample->Cr_r_tab[i] = (int)
+		    RIGHT_SHIFT(FIX(1.40200/2) * x2 + ONE_HALF, SCALEBITS);
+    /* Cb=>B value is nearest int to 1.77200 * x */
+    upsample->Cb_b_tab[i] = (int)
+		    RIGHT_SHIFT(FIX(1.77200/2) * x2 + ONE_HALF, SCALEBITS);
+    /* Cr=>G value is scaled-up -0.71414 * x */
+    upsample->Cr_g_tab[i] = (- FIX(0.71414/2)) * x2;
+    /* Cb=>G value is scaled-up -0.34414 * x */
+    /* We also add in ONE_HALF so that need not do it in inner loop */
+    upsample->Cb_g_tab[i] = (- FIX(0.34414/2)) * x2 + ONE_HALF;
+  }
+}
+
+
+/*
+ * Control routine to do upsampling (and color conversion).
+ *
+ * The control routine just handles the row buffering considerations.
+ */
+
+METHODDEF void
+merged_2v_upsample (j_decompress_ptr cinfo,
+		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+		    JDIMENSION in_row_groups_avail,
+		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+		    JDIMENSION out_rows_avail)
+/* 2:1 vertical sampling case: may need a spare row. */
+{
+  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+  JSAMPROW work_ptrs[2];
+  JDIMENSION num_rows;		/* number of rows returned to caller */
+
+  if (upsample->spare_full) {
+    /* If we have a spare row saved from a previous cycle, just return it. */
+    jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
+		      1, upsample->out_row_width);
+    num_rows = 1;
+    upsample->spare_full = FALSE;
+  } else {
+    /* Figure number of rows to return to caller. */
+    num_rows = 2;
+    /* Not more than the distance to the end of the image. */
+    if (num_rows > upsample->rows_to_go)
+      num_rows = upsample->rows_to_go;
+    /* And not more than what the client can accept: */
+    out_rows_avail -= *out_row_ctr;
+    if (num_rows > out_rows_avail)
+      num_rows = out_rows_avail;
+    /* Create output pointer array for upsampler. */
+    work_ptrs[0] = output_buf[*out_row_ctr];
+    if (num_rows > 1) {
+      work_ptrs[1] = output_buf[*out_row_ctr + 1];
+    } else {
+      work_ptrs[1] = upsample->spare_row;
+      upsample->spare_full = TRUE;
+    }
+    /* Now do the upsampling. */
+    (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
+  }
+
+  /* Adjust counts */
+  *out_row_ctr += num_rows;
+  upsample->rows_to_go -= num_rows;
+  /* When the buffer is emptied, declare this input row group consumed */
+  if (! upsample->spare_full)
+    (*in_row_group_ctr)++;
+}
+
+
+METHODDEF void
+merged_1v_upsample (j_decompress_ptr cinfo,
+		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+		    JDIMENSION in_row_groups_avail,
+		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+		    JDIMENSION out_rows_avail)
+/* 1:1 vertical sampling case: much easier, never need a spare row. */
+{
+  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+
+  /* Just do the upsampling. */
+  (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
+			 output_buf + *out_row_ctr);
+  /* Adjust counts */
+  (*out_row_ctr)++;
+  (*in_row_group_ctr)++;
+}
+
+
+/*
+ * These are the routines invoked by the control routines to do
+ * the actual upsampling/conversion.  One row group is processed per call.
+ *
+ * Note: since we may be writing directly into application-supplied buffers,
+ * we have to be honest about the output width; we can't assume the buffer
+ * has been rounded up to an even width.
+ */
+
+
+/*
+ * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
+ */
+
+METHODDEF void
+h2v1_merged_upsample (j_decompress_ptr cinfo,
+		      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
+		      JSAMPARRAY output_buf)
+{
+  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+  register int y, cred, cgreen, cblue;
+  int cb, cr;
+  register JSAMPROW outptr;
+  JSAMPROW inptr0, inptr1, inptr2;
+  JDIMENSION col;
+  /* copy these pointers into registers if possible */
+  register JSAMPLE * range_limit = cinfo->sample_range_limit;
+  int * Crrtab = upsample->Cr_r_tab;
+  int * Cbbtab = upsample->Cb_b_tab;
+  INT32 * Crgtab = upsample->Cr_g_tab;
+  INT32 * Cbgtab = upsample->Cb_g_tab;
+  SHIFT_TEMPS
+
+  inptr0 = input_buf[0][in_row_group_ctr];
+  inptr1 = input_buf[1][in_row_group_ctr];
+  inptr2 = input_buf[2][in_row_group_ctr];
+  outptr = output_buf[0];
+  /* Loop for each pair of output pixels */
+  for (col = cinfo->output_width >> 1; col > 0; col--) {
+    /* Do the chroma part of the calculation */
+    cb = GETJSAMPLE(*inptr1++);
+    cr = GETJSAMPLE(*inptr2++);
+    cred = Crrtab[cr];
+    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+    cblue = Cbbtab[cb];
+    /* Fetch 2 Y values and emit 2 pixels */
+    y  = GETJSAMPLE(*inptr0++);
+    outptr[RGB_RED] =   range_limit[y + cred];
+    outptr[RGB_GREEN] = range_limit[y + cgreen];
+    outptr[RGB_BLUE] =  range_limit[y + cblue];
+    outptr += RGB_PIXELSIZE;
+    y  = GETJSAMPLE(*inptr0++);
+    outptr[RGB_RED] =   range_limit[y + cred];
+    outptr[RGB_GREEN] = range_limit[y + cgreen];
+    outptr[RGB_BLUE] =  range_limit[y + cblue];
+    outptr += RGB_PIXELSIZE;
+  }
+  /* If image width is odd, do the last output column separately */
+  if (cinfo->output_width & 1) {
+    cb = GETJSAMPLE(*inptr1);
+    cr = GETJSAMPLE(*inptr2);
+    cred = Crrtab[cr];
+    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+    cblue = Cbbtab[cb];
+    y  = GETJSAMPLE(*inptr0);
+    outptr[RGB_RED] =   range_limit[y + cred];
+    outptr[RGB_GREEN] = range_limit[y + cgreen];
+    outptr[RGB_BLUE] =  range_limit[y + cblue];
+  }
+}
+
+
+/*
+ * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
+ */
+
+METHODDEF void
+h2v2_merged_upsample (j_decompress_ptr cinfo,
+		      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
+		      JSAMPARRAY output_buf)
+{
+  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+  register int y, cred, cgreen, cblue;
+  int cb, cr;
+  register JSAMPROW outptr0, outptr1;
+  JSAMPROW inptr00, inptr01, inptr1, inptr2;
+  JDIMENSION col;
+  /* copy these pointers into registers if possible */
+  register JSAMPLE * range_limit = cinfo->sample_range_limit;
+  int * Crrtab = upsample->Cr_r_tab;
+  int * Cbbtab = upsample->Cb_b_tab;
+  INT32 * Crgtab = upsample->Cr_g_tab;
+  INT32 * Cbgtab = upsample->Cb_g_tab;
+  SHIFT_TEMPS
+
+  inptr00 = input_buf[0][in_row_group_ctr*2];
+  inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
+  inptr1 = input_buf[1][in_row_group_ctr];
+  inptr2 = input_buf[2][in_row_group_ctr];
+  outptr0 = output_buf[0];
+  outptr1 = output_buf[1];
+  /* Loop for each group of output pixels */
+  for (col = cinfo->output_width >> 1; col > 0; col--) {
+    /* Do the chroma part of the calculation */
+    cb = GETJSAMPLE(*inptr1++);
+    cr = GETJSAMPLE(*inptr2++);
+    cred = Crrtab[cr];
+    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+    cblue = Cbbtab[cb];
+    /* Fetch 4 Y values and emit 4 pixels */
+    y  = GETJSAMPLE(*inptr00++);
+    outptr0[RGB_RED] =   range_limit[y + cred];
+    outptr0[RGB_GREEN] = range_limit[y + cgreen];
+    outptr0[RGB_BLUE] =  range_limit[y + cblue];
+    outptr0 += RGB_PIXELSIZE;
+    y  = GETJSAMPLE(*inptr00++);
+    outptr0[RGB_RED] =   range_limit[y + cred];
+    outptr0[RGB_GREEN] = range_limit[y + cgreen];
+    outptr0[RGB_BLUE] =  range_limit[y + cblue];
+    outptr0 += RGB_PIXELSIZE;
+    y  = GETJSAMPLE(*inptr01++);
+    outptr1[RGB_RED] =   range_limit[y + cred];
+    outptr1[RGB_GREEN] = range_limit[y + cgreen];
+    outptr1[RGB_BLUE] =  range_limit[y + cblue];
+    outptr1 += RGB_PIXELSIZE;
+    y  = GETJSAMPLE(*inptr01++);
+    outptr1[RGB_RED] =   range_limit[y + cred];
+    outptr1[RGB_GREEN] = range_limit[y + cgreen];
+    outptr1[RGB_BLUE] =  range_limit[y + cblue];
+    outptr1 += RGB_PIXELSIZE;
+  }
+  /* If image width is odd, do the last output column separately */
+  if (cinfo->output_width & 1) {
+    cb = GETJSAMPLE(*inptr1);
+    cr = GETJSAMPLE(*inptr2);
+    cred = Crrtab[cr];
+    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+    cblue = Cbbtab[cb];
+    y  = GETJSAMPLE(*inptr00);
+    outptr0[RGB_RED] =   range_limit[y + cred];
+    outptr0[RGB_GREEN] = range_limit[y + cgreen];
+    outptr0[RGB_BLUE] =  range_limit[y + cblue];
+    y  = GETJSAMPLE(*inptr01);
+    outptr1[RGB_RED] =   range_limit[y + cred];
+    outptr1[RGB_GREEN] = range_limit[y + cgreen];
+    outptr1[RGB_BLUE] =  range_limit[y + cblue];
+  }
+}
+
+
+/*
+ * Module initialization routine for merged upsampling/color conversion.
+ *
+ * NB: this is called under the conditions determined by use_merged_upsample()
+ * in jdmaster.c.  That routine MUST correspond to the actual capabilities
+ * of this module; no safety checks are made here.
+ */
+
+GLOBAL void
+jinit_merged_upsampler (j_decompress_ptr cinfo)
+{
+  my_upsample_ptr upsample;
+
+  upsample = (my_upsample_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_upsampler));
+  cinfo->upsample = (struct jpeg_upsampler *) upsample;
+  upsample->pub.start_pass = start_pass_merged_upsample;
+  upsample->pub.need_context_rows = FALSE;
+
+  upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
+
+  if (cinfo->max_v_samp_factor == 2) {
+    upsample->pub.upsample = merged_2v_upsample;
+    upsample->upmethod = h2v2_merged_upsample;
+    /* Allocate a spare row buffer */
+    upsample->spare_row = (JSAMPROW)
+      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+		(size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
+  } else {
+    upsample->pub.upsample = merged_1v_upsample;
+    upsample->upmethod = h2v1_merged_upsample;
+    /* No spare row needed */
+    upsample->spare_row = NULL;
+  }
+}
+
+#endif /* UPSAMPLE_MERGING_SUPPORTED */
diff --git a/jdpipe.c b/jdpipe.c
deleted file mode 100644
index 26f1f69..0000000
--- a/jdpipe.c
+++ /dev/null
@@ -1,1056 +0,0 @@
-/*
- * jdpipe.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains decompression pipeline controllers.
- * These routines are invoked via the d_pipeline_controller method.
- *
- * There are two basic pipeline controllers.  The simpler one handles a
- * single-scan JPEG file (single component or fully interleaved) with no
- * color quantization or 1-pass quantization.  In this case, the file can
- * be processed in one top-to-bottom pass.  The more complex controller is
- * used when 2-pass color quantization is requested and/or the JPEG file
- * has multiple scans (noninterleaved or partially interleaved).  In this
- * case, the entire image must be buffered up in a "big" array.
- *
- * If you need to make a minimal implementation, the more complex controller
- * can be compiled out by disabling the appropriate configuration options.
- * We don't recommend this, since then you can't handle all legal JPEG files.
- */
-
-#include "jinclude.h"
-
-
-#ifdef D_MULTISCAN_FILES_SUPPORTED /* wish we could assume ANSI's defined() */
-#define NEED_COMPLEX_CONTROLLER
-#else
-#ifdef QUANT_2PASS_SUPPORTED
-#define NEED_COMPLEX_CONTROLLER
-#endif
-#endif
-
-
-/*
- * About the data structures:
- *
- * The processing chunk size for upsampling is referred to in this file as
- * a "row group": a row group is defined as Vk (v_samp_factor) sample rows of
- * any component while downsampled, or Vmax (max_v_samp_factor) unsubsampled
- * rows.  In an interleaved scan each MCU row contains exactly DCTSIZE row
- * groups of each component in the scan.  In a noninterleaved scan an MCU row
- * is one row of blocks, which might not be an integral number of row groups;
- * therefore, we read in Vk MCU rows to obtain the same amount of data as we'd
- * have in an interleaved scan.
- * To provide context for the upsampling step, we have to retain the last
- * two row groups of the previous MCU row while reading in the next MCU row
- * (or set of Vk MCU rows).  To do this without copying data about, we create
- * a rather strange data structure.  Exactly DCTSIZE+2 row groups of samples
- * are allocated, but we create two different sets of pointers to this array.
- * The second set swaps the last two pairs of row groups.  By working
- * alternately with the two sets of pointers, we can access the data in the
- * desired order.
- *
- * Cross-block smoothing also needs context above and below the "current" row.
- * Since this is an optional feature, I've implemented it in a way that is
- * much simpler but requires more than the minimum amount of memory.  We
- * simply allocate three extra MCU rows worth of coefficient blocks and use
- * them to "read ahead" one MCU row in the file.  For a typical 1000-pixel-wide
- * image with 2x2,1x1,1x1 sampling, each MCU row is about 50Kb; an 80x86
- * machine may be unable to apply cross-block smoothing to wider images.
- */
-
-
-/*
- * These variables are logically local to the pipeline controller,
- * but we make them static so that scan_big_image can use them
- * without having to pass them through the quantization routines.
- */
-
-static int rows_in_mem;		/* # of sample rows in full-size buffers */
-/* Work buffer for data being passed to output module. */
-/* This has color_out_comps components if not quantizing, */
-/* but only one component when quantizing. */
-static JSAMPIMAGE output_workspace;
-
-#ifdef NEED_COMPLEX_CONTROLLER
-/* Full-size image array holding upsampled, but not color-processed data. */
-static big_sarray_ptr *fullsize_image;
-static JSAMPIMAGE fullsize_ptrs; /* workspace for access_big_sarray() result */
-#endif
-
-
-/*
- * Utility routines: common code for pipeline controllers
- */
-
-LOCAL void
-interleaved_scan_setup (decompress_info_ptr cinfo)
-/* Compute all derived info for an interleaved (multi-component) scan */
-/* On entry, cinfo->comps_in_scan and cinfo->cur_comp_info[] are set up */
-{
-  short ci, mcublks;
-  jpeg_component_info *compptr;
-
-  if (cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
-    ERREXIT(cinfo->emethods, "Too many components for interleaved scan");
-
-  cinfo->MCUs_per_row = (cinfo->image_width
-			 + cinfo->max_h_samp_factor*DCTSIZE - 1)
-			/ (cinfo->max_h_samp_factor*DCTSIZE);
-
-  cinfo->MCU_rows_in_scan = (cinfo->image_height
-			     + cinfo->max_v_samp_factor*DCTSIZE - 1)
-			    / (cinfo->max_v_samp_factor*DCTSIZE);
-  
-  cinfo->blocks_in_MCU = 0;
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
-    /* for interleaved scan, sampling factors give # of blocks per component */
-    compptr->MCU_width = compptr->h_samp_factor;
-    compptr->MCU_height = compptr->v_samp_factor;
-    compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
-    /* compute physical dimensions of component */
-    compptr->downsampled_width = jround_up(compptr->true_comp_width,
-					   (long) (compptr->MCU_width*DCTSIZE));
-    compptr->downsampled_height = jround_up(compptr->true_comp_height,
-					    (long) (compptr->MCU_height*DCTSIZE));
-    /* Sanity check */
-    if (compptr->downsampled_width !=
-	(cinfo->MCUs_per_row * (compptr->MCU_width*DCTSIZE)))
-      ERREXIT(cinfo->emethods, "I'm confused about the image width");
-    /* Prepare array describing MCU composition */
-    mcublks = compptr->MCU_blocks;
-    if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
-      ERREXIT(cinfo->emethods, "Sampling factors too large for interleaved scan");
-    while (mcublks-- > 0) {
-      cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
-    }
-  }
-
-  (*cinfo->methods->d_per_scan_method_selection) (cinfo);
-}
-
-
-LOCAL void
-noninterleaved_scan_setup (decompress_info_ptr cinfo)
-/* Compute all derived info for a noninterleaved (single-component) scan */
-/* On entry, cinfo->comps_in_scan = 1 and cinfo->cur_comp_info[0] is set up */
-{
-  jpeg_component_info *compptr = cinfo->cur_comp_info[0];
-
-  /* for noninterleaved scan, always one block per MCU */
-  compptr->MCU_width = 1;
-  compptr->MCU_height = 1;
-  compptr->MCU_blocks = 1;
-  /* compute physical dimensions of component */
-  compptr->downsampled_width = jround_up(compptr->true_comp_width,
-					 (long) DCTSIZE);
-  compptr->downsampled_height = jround_up(compptr->true_comp_height,
-					  (long) DCTSIZE);
-
-  cinfo->MCUs_per_row = compptr->downsampled_width / DCTSIZE;
-  cinfo->MCU_rows_in_scan = compptr->downsampled_height / DCTSIZE;
-
-  /* Prepare array describing MCU composition */
-  cinfo->blocks_in_MCU = 1;
-  cinfo->MCU_membership[0] = 0;
-
-  (*cinfo->methods->d_per_scan_method_selection) (cinfo);
-}
-
-
-
-LOCAL JSAMPIMAGE
-alloc_sampimage (decompress_info_ptr cinfo,
-		 int num_comps, long num_rows, long num_cols)
-/* Allocate an in-memory sample image (all components same size) */
-{
-  JSAMPIMAGE image;
-  int ci;
-
-  image = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
-				(num_comps * SIZEOF(JSAMPARRAY));
-  for (ci = 0; ci < num_comps; ci++) {
-    image[ci] = (*cinfo->emethods->alloc_small_sarray) (num_cols, num_rows);
-  }
-  return image;
-}
-
-
-#if 0				/* this routine not currently needed */
-
-LOCAL void
-free_sampimage (decompress_info_ptr cinfo, JSAMPIMAGE image, int num_comps)
-/* Release a sample image created by alloc_sampimage */
-{
-  int ci;
-
-  for (ci = 0; ci < num_comps; ci++) {
-      (*cinfo->emethods->free_small_sarray) (image[ci]);
-  }
-  (*cinfo->emethods->free_small) ((void *) image);
-}
-
-#endif
-
-
-LOCAL JBLOCKIMAGE
-alloc_MCU_row (decompress_info_ptr cinfo)
-/* Allocate one MCU row's worth of coefficient blocks */
-{
-  JBLOCKIMAGE image;
-  int ci;
-
-  image = (JBLOCKIMAGE) (*cinfo->emethods->alloc_small)
-				(cinfo->comps_in_scan * SIZEOF(JBLOCKARRAY));
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    image[ci] = (*cinfo->emethods->alloc_small_barray)
-			(cinfo->cur_comp_info[ci]->downsampled_width / DCTSIZE,
-			 (long) cinfo->cur_comp_info[ci]->MCU_height);
-  }
-  return image;
-}
-
-
-#ifdef NEED_COMPLEX_CONTROLLER	/* not used by simple controller */
-
-LOCAL void
-free_MCU_row (decompress_info_ptr cinfo, JBLOCKIMAGE image)
-/* Release a coefficient block array created by alloc_MCU_row */
-{
-  int ci;
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    (*cinfo->emethods->free_small_barray) (image[ci]);
-  }
-  (*cinfo->emethods->free_small) ((void *) image);
-}
-
-#endif
-
-
-LOCAL void
-alloc_sampling_buffer (decompress_info_ptr cinfo, JSAMPIMAGE sampled_data[2])
-/* Create a downsampled-data buffer having the desired structure */
-/* (see comments at head of file) */
-{
-  short ci, vs, i;
-
-  /* Get top-level space for array pointers */
-  sampled_data[0] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
-				(cinfo->comps_in_scan * SIZEOF(JSAMPARRAY));
-  sampled_data[1] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
-				(cinfo->comps_in_scan * SIZEOF(JSAMPARRAY));
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    vs = cinfo->cur_comp_info[ci]->v_samp_factor; /* row group height */
-    /* Allocate the real storage */
-    sampled_data[0][ci] = (*cinfo->emethods->alloc_small_sarray)
-				(cinfo->cur_comp_info[ci]->downsampled_width,
-				(long) (vs * (DCTSIZE+2)));
-    /* Create space for the scrambled-order pointers */
-    sampled_data[1][ci] = (JSAMPARRAY) (*cinfo->emethods->alloc_small)
-				(vs * (DCTSIZE+2) * SIZEOF(JSAMPROW));
-    /* Duplicate the first DCTSIZE-2 row groups */
-    for (i = 0; i < vs * (DCTSIZE-2); i++) {
-      sampled_data[1][ci][i] = sampled_data[0][ci][i];
-    }
-    /* Copy the last four row groups in swapped order */
-    for (i = 0; i < vs * 2; i++) {
-      sampled_data[1][ci][vs*DCTSIZE + i] = sampled_data[0][ci][vs*(DCTSIZE-2) + i];
-      sampled_data[1][ci][vs*(DCTSIZE-2) + i] = sampled_data[0][ci][vs*DCTSIZE + i];
-    }
-  }
-}
-
-
-#ifdef NEED_COMPLEX_CONTROLLER	/* not used by simple controller */
-
-LOCAL void
-free_sampling_buffer (decompress_info_ptr cinfo, JSAMPIMAGE sampled_data[2])
-/* Release a sampling buffer created by alloc_sampling_buffer */
-{
-  short ci;
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    /* Free the real storage */
-    (*cinfo->emethods->free_small_sarray) (sampled_data[0][ci]);
-    /* Free the scrambled-order pointers */
-    (*cinfo->emethods->free_small) ((void *) sampled_data[1][ci]);
-  }
-
-  /* Free the top-level space */
-  (*cinfo->emethods->free_small) ((void *) sampled_data[0]);
-  (*cinfo->emethods->free_small) ((void *) sampled_data[1]);
-}
-
-#endif
-
-
-/*
- * Several decompression processes need to range-limit values to the range
- * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
- * due to noise introduced by quantization, roundoff error, etc.  These
- * processes are inner loops and need to be as fast as possible.  On most
- * machines, particularly CPUs with pipelines or instruction prefetch,
- * a (range-check-less) C table lookup
- *		x = sample_range_limit[x];
- * is faster than explicit tests
- *		if (x < 0)  x = 0;
- *		else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
- * These processes all use a common table prepared by the routine below.
- *
- * The table will work correctly for x within MAXJSAMPLE+1 of the legal
- * range.  This is a much wider range than is needed for most cases,
- * but the wide range is handy for color quantization.
- * Note that the table is allocated in near data space on PCs; it's small
- * enough and used often enough to justify this.
- */
-
-LOCAL void
-prepare_range_limit_table (decompress_info_ptr cinfo)
-/* Allocate and fill in the sample_range_limit table */
-{
-  JSAMPLE * table;
-  int i;
-
-  table = (JSAMPLE *) (*cinfo->emethods->alloc_small)
-			(3 * (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
-  cinfo->sample_range_limit = table + (MAXJSAMPLE+1);
-  for (i = 0; i <= MAXJSAMPLE; i++) {
-    table[i] = 0;			/* sample_range_limit[x] = 0 for x<0 */
-    table[i+(MAXJSAMPLE+1)] = (JSAMPLE) i;	/* sample_range_limit[x] = x */
-    table[i+(MAXJSAMPLE+1)*2] = MAXJSAMPLE;	/* x beyond MAXJSAMPLE */
-  }
-}
-
-
-LOCAL void
-duplicate_row (JSAMPARRAY image_data,
-	       long num_cols, int source_row, int num_rows)
-/* Duplicate the source_row at source_row+1 .. source_row+num_rows */
-/* This happens only at the bottom of the image, */
-/* so it needn't be super-efficient */
-{
-  register int row;
-
-  for (row = 1; row <= num_rows; row++) {
-    jcopy_sample_rows(image_data, source_row, image_data, source_row + row,
-		      1, num_cols);
-  }
-}
-
-
-LOCAL void
-expand (decompress_info_ptr cinfo,
-	JSAMPIMAGE sampled_data, JSAMPIMAGE fullsize_data,
-	long fullsize_width,
-	short above, short current, short below, short out)
-/* Do upsampling expansion of a single row group (of each component). */
-/* above, current, below are indexes of row groups in sampled_data;       */
-/* out is the index of the target row group in fullsize_data.             */
-/* Special case: above, below can be -1 to indicate top, bottom of image. */
-{
-  jpeg_component_info *compptr;
-  JSAMPARRAY above_ptr, below_ptr;
-  JSAMPROW dummy[MAX_SAMP_FACTOR]; /* for downsample expansion at top/bottom */
-  short ci, vs, i;
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
-    /* don't bother to upsample an uninteresting component */
-    if (! compptr->component_needed)
-      continue;
-
-    vs = compptr->v_samp_factor; /* row group height */
-
-    if (above >= 0)
-      above_ptr = sampled_data[ci] + above * vs;
-    else {
-      /* Top of image: make a dummy above-context with copies of 1st row */
-      /* We assume current=0 in this case */
-      for (i = 0; i < vs; i++)
-	dummy[i] = sampled_data[ci][0];
-      above_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
-    }
-
-    if (below >= 0)
-      below_ptr = sampled_data[ci] + below * vs;
-    else {
-      /* Bot of image: make a dummy below-context with copies of last row */
-      for (i = 0; i < vs; i++)
-	dummy[i] = sampled_data[ci][(current+1)*vs-1];
-      below_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
-    }
-
-    (*cinfo->methods->upsample[ci])
-		(cinfo, (int) ci,
-		 compptr->downsampled_width, (int) vs,
-		 fullsize_width, (int) cinfo->max_v_samp_factor,
-		 above_ptr,
-		 sampled_data[ci] + current * vs,
-		 below_ptr,
-		 fullsize_data[ci] + out * cinfo->max_v_samp_factor);
-  }
-}
-
-
-LOCAL void
-emit_1pass (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE fullsize_data,
-	    JSAMPARRAY dummy)
-/* Do color processing and output of num_rows full-size rows. */
-/* This is not used when doing 2-pass color quantization. */
-/* The dummy argument simply lets this be called via scan_big_image. */
-{
-  if (cinfo->quantize_colors) {
-    (*cinfo->methods->color_quantize) (cinfo, num_rows, fullsize_data,
-				       output_workspace[0]);
-  } else {
-    (*cinfo->methods->color_convert) (cinfo, num_rows, cinfo->image_width,
-				      fullsize_data, output_workspace);
-  }
-    
-  (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, output_workspace);
-}
-
-
-/*
- * Support routines for complex controller.
- */
-
-#ifdef NEED_COMPLEX_CONTROLLER
-
-METHODDEF void
-scan_big_image (decompress_info_ptr cinfo, quantize_method_ptr quantize_method)
-/* Apply quantize_method to entire image stored in fullsize_image[]. */
-/* This is the "iterator" routine used by the 2-pass color quantizer. */
-/* We also use it directly in some cases. */
-{
-  long pixel_rows_output;
-  short ci;
-
-  for (pixel_rows_output = 0; pixel_rows_output < cinfo->image_height;
-       pixel_rows_output += rows_in_mem) {
-    (*cinfo->methods->progress_monitor) (cinfo, pixel_rows_output,
-					 cinfo->image_height);
-    /* Realign the big buffers */
-    for (ci = 0; ci < cinfo->num_components; ci++) {
-      fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
-	(fullsize_image[ci], pixel_rows_output, FALSE);
-    }
-    /* Let the quantizer have its way with the data.
-     * Note that output_workspace is simply workspace for the quantizer;
-     * when it's ready to output, it must call put_pixel_rows itself.
-     */
-    (*quantize_method) (cinfo,
-			(int) MIN((long) rows_in_mem,
-				  cinfo->image_height - pixel_rows_output),
-			fullsize_ptrs, output_workspace[0]);
-  }
-
-  cinfo->completed_passes++;
-}
-
-#endif /* NEED_COMPLEX_CONTROLLER */
-
-
-/*
- * Support routines for cross-block smoothing.
- */
-
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-
-
-LOCAL void
-smooth_mcu_row (decompress_info_ptr cinfo,
-		JBLOCKIMAGE above, JBLOCKIMAGE input, JBLOCKIMAGE below,
-		JBLOCKIMAGE output)
-/* Apply cross-block smoothing to one MCU row's worth of coefficient blocks. */
-/* above,below are NULL if at top/bottom of image. */
-{
-  jpeg_component_info *compptr;
-  short ci, ri, last;
-  JBLOCKROW prev;
-
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
-    /* don't bother to smooth an uninteresting component */
-    if (! compptr->component_needed)
-      continue;
-
-    last = compptr->MCU_height - 1;
-
-    if (above == NULL)
-      prev = NULL;
-    else
-      prev = above[ci][last];
-
-    for (ri = 0; ri < last; ri++) {
-      (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
-				prev, input[ci][ri], input[ci][ri+1],
-				output[ci][ri]);
-      prev = input[ci][ri];
-    }
-
-    if (below == NULL)
-      (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
-				prev, input[ci][last], (JBLOCKROW) NULL,
-				output[ci][last]);
-    else
-      (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
-				prev, input[ci][last], below[ci][0],
-				output[ci][last]);
-  }
-}
-
-
-LOCAL void
-get_smoothed_row (decompress_info_ptr cinfo, JBLOCKIMAGE coeff_data,
-		  JBLOCKIMAGE bsmooth[3], int * whichb, long cur_mcu_row)
-/* Get an MCU row of coefficients, applying cross-block smoothing. */
-/* The output row is placed in coeff_data.  bsmooth and whichb hold */
-/* working state, and cur_row is needed to check for image top/bottom. */
-/* This routine just takes care of the buffering logic. */
-{
-  int prev, cur, next;
-  
-  /* Special case for top of image: need to pre-fetch a row & init whichb */
-  if (cur_mcu_row == 0) {
-    (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[0]);
-    if (cinfo->MCU_rows_in_scan > 1) {
-      (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[1]);
-      smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], bsmooth[1],
-		     coeff_data);
-    } else {
-      smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], (JBLOCKIMAGE) NULL,
-		     coeff_data);
-    }
-    *whichb = 1;		/* points to next bsmooth[] element to use */
-    return;
-  }
-  
-  cur = *whichb;		/* set up references */
-  prev = (cur == 0 ? 2 : cur - 1);
-  next = (cur == 2 ? 0 : cur + 1);
-  *whichb = next;		/* advance whichb for next time */
-  
-  /* Special case for bottom of image: don't read another row */
-  if (cur_mcu_row >= cinfo->MCU_rows_in_scan - 1) {
-    smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], (JBLOCKIMAGE) NULL,
-		   coeff_data);
-    return;
-  }
-  
-  /* Normal case: read ahead a new row, smooth the one I got before */
-  (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[next]);
-  smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], bsmooth[next],
-		 coeff_data);
-}
-
-
-#endif /* BLOCK_SMOOTHING_SUPPORTED */
-
-
-
-/*
- * Decompression pipeline controller used for single-scan files
- * without 2-pass color quantization.
- */
-
-METHODDEF void
-simple_dcontroller (decompress_info_ptr cinfo)
-{
-  long fullsize_width;		/* # of samples per row in full-size buffers */
-  long cur_mcu_row;		/* counts # of MCU rows processed */
-  long pixel_rows_output;	/* # of pixel rows actually emitted */
-  int mcu_rows_per_loop;	/* # of MCU rows processed per outer loop */
-  /* Work buffer for dequantized coefficients (IDCT input) */
-  JBLOCKIMAGE coeff_data;
-  /* Work buffer for cross-block smoothing input */
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-  JBLOCKIMAGE bsmooth[3];	/* this is optional */
-  int whichb;
-#endif
-  /* Work buffer for downsampled image data (see comments at head of file) */
-  JSAMPIMAGE sampled_data[2];
-  /* Work buffer for upsampled data */
-  JSAMPIMAGE fullsize_data;
-  int whichss, ri;
-  short i;
-
-  /* Compute dimensions of full-size pixel buffers */
-  /* Note these are the same whether interleaved or not. */
-  rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
-  fullsize_width = jround_up(cinfo->image_width,
-			     (long) (cinfo->max_h_samp_factor * DCTSIZE));
-
-  /* Prepare for single scan containing all components */
-  if (cinfo->comps_in_scan == 1) {
-    noninterleaved_scan_setup(cinfo);
-    /* Need to read Vk MCU rows to obtain Vk block rows */
-    mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
-  } else {
-    interleaved_scan_setup(cinfo);
-    /* in an interleaved scan, one MCU row provides Vk block rows */
-    mcu_rows_per_loop = 1;
-  }
-  cinfo->total_passes++;
-
-  /* Allocate working memory: */
-  prepare_range_limit_table(cinfo);
-  /* coeff_data holds a single MCU row of coefficient blocks */
-  coeff_data = alloc_MCU_row(cinfo);
-  /* if doing cross-block smoothing, need extra space for its input */
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-  if (cinfo->do_block_smoothing) {
-    bsmooth[0] = alloc_MCU_row(cinfo);
-    bsmooth[1] = alloc_MCU_row(cinfo);
-    bsmooth[2] = alloc_MCU_row(cinfo);
-  }
-#endif
-  /* sampled_data is sample data before upsampling */
-  alloc_sampling_buffer(cinfo, sampled_data);
-  /* fullsize_data is sample data after upsampling */
-  fullsize_data = alloc_sampimage(cinfo, (int) cinfo->num_components,
-				  (long) rows_in_mem, fullsize_width);
-  /* output_workspace is the color-processed data */
-  output_workspace = alloc_sampimage(cinfo, (int) cinfo->final_out_comps,
-				     (long) rows_in_mem, fullsize_width);
-
-  /* Tell the memory manager to instantiate big arrays.
-   * We don't need any big arrays in this controller,
-   * but some other module (like the output file writer) may need one.
-   */
-  (*cinfo->emethods->alloc_big_arrays)
-	((long) 0,				/* no more small sarrays */
-	 (long) 0,				/* no more small barrays */
-	 (long) 0);				/* no more "medium" objects */
-  /* NB: if quantizer needs any "medium" size objects, it must get them */
-  /* at color_quant_init time */
-
-  /* Initialize to read scan data */
-
-  (*cinfo->methods->entropy_decode_init) (cinfo);
-  (*cinfo->methods->upsample_init) (cinfo);
-  (*cinfo->methods->disassemble_init) (cinfo);
-
-  /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
-
-  pixel_rows_output = 0;
-  whichss = 1;			/* arrange to start with sampled_data[0] */
-
-  for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
-       cur_mcu_row += mcu_rows_per_loop) {
-    (*cinfo->methods->progress_monitor) (cinfo, cur_mcu_row,
-					 cinfo->MCU_rows_in_scan);
-
-    whichss ^= 1;		/* switch to other downsampled-data buffer */
-
-    /* Obtain v_samp_factor block rows of each component in the scan. */
-    /* This is a single MCU row if interleaved, multiple MCU rows if not. */
-    /* In the noninterleaved case there might be fewer than v_samp_factor */
-    /* block rows remaining; if so, pad with copies of the last pixel row */
-    /* so that upsampling doesn't have to treat it as a special case. */
-
-    for (ri = 0; ri < mcu_rows_per_loop; ri++) {
-      if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
-	/* OK to actually read an MCU row. */
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-	if (cinfo->do_block_smoothing)
-	  get_smoothed_row(cinfo, coeff_data,
-			   bsmooth, &whichb, cur_mcu_row + ri);
-	else
-#endif
-	  (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
-      
-	(*cinfo->methods->reverse_DCT) (cinfo, coeff_data,
-					sampled_data[whichss],
-					ri * DCTSIZE);
-      } else {
-	/* Need to pad out with copies of the last downsampled row. */
-	/* This can only happen if there is just one component. */
-	duplicate_row(sampled_data[whichss][0],
-		      cinfo->cur_comp_info[0]->downsampled_width,
-		      ri * DCTSIZE - 1, DCTSIZE);
-      }
-    }
-
-    /* Upsample the data */
-    /* First time through is a special case */
-
-    if (cur_mcu_row) {
-      /* Expand last row group of previous set */
-      expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
-	     (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
-	     (short) (DCTSIZE-1));
-      /* and dump the previous set's expanded data */
-      emit_1pass (cinfo, rows_in_mem, fullsize_data, (JSAMPARRAY) NULL);
-      pixel_rows_output += rows_in_mem;
-      /* Expand first row group of this set */
-      expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
-	     (short) (DCTSIZE+1), (short) 0, (short) 1,
-	     (short) 0);
-    } else {
-      /* Expand first row group with dummy above-context */
-      expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
-	     (short) (-1), (short) 0, (short) 1,
-	     (short) 0);
-    }
-    /* Expand second through next-to-last row groups of this set */
-    for (i = 1; i <= DCTSIZE-2; i++) {
-      expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
-	     (short) (i-1), (short) i, (short) (i+1),
-	     (short) i);
-    }
-  } /* end of outer loop */
-
-  /* Expand the last row group with dummy below-context */
-  /* Note whichss points to last buffer side used */
-  expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
-	 (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
-	 (short) (DCTSIZE-1));
-  /* and dump the remaining data (may be less than full height) */
-  emit_1pass (cinfo, (int) (cinfo->image_height - pixel_rows_output),
-	      fullsize_data, (JSAMPARRAY) NULL);
-
-  /* Clean up after the scan */
-  (*cinfo->methods->disassemble_term) (cinfo);
-  (*cinfo->methods->upsample_term) (cinfo);
-  (*cinfo->methods->entropy_decode_term) (cinfo);
-  (*cinfo->methods->read_scan_trailer) (cinfo);
-  cinfo->completed_passes++;
-
-  /* Verify that we've seen the whole input file */
-  if ((*cinfo->methods->read_scan_header) (cinfo))
-    WARNMS(cinfo->emethods, "Didn't expect more than one scan");
-
-  /* Release working memory */
-  /* (no work -- we let free_all release what's needful) */
-}
-
-
-/*
- * Decompression pipeline controller used for multiple-scan files
- * and/or 2-pass color quantization.
- *
- * The current implementation places the "big" buffer at the stage of
- * upsampled, non-color-processed data.  This is the only place that
- * makes sense when doing 2-pass quantization.  For processing multiple-scan
- * files without 2-pass quantization, it would be possible to develop another
- * controller that buffers the downsampled data instead, thus reducing the size
- * of the temp files (by about a factor of 2 in typical cases).  However,
- * our present upsampling logic is dependent on the assumption that
- * upsampling occurs during a scan, so it's much easier to do the
- * enlargement as the JPEG file is read.  This also simplifies life for the
- * memory manager, which would otherwise have to deal with overlapping
- * access_big_sarray() requests.
- * At present it appears that most JPEG files will be single-scan,
- * so it doesn't seem worthwhile to worry about this optimization.
- */
-
-#ifdef NEED_COMPLEX_CONTROLLER
-
-METHODDEF void
-complex_dcontroller (decompress_info_ptr cinfo)
-{
-  long fullsize_width;		/* # of samples per row in full-size buffers */
-  long cur_mcu_row;		/* counts # of MCU rows processed */
-  long pixel_rows_output;	/* # of pixel rows actually emitted */
-  int mcu_rows_per_loop;	/* # of MCU rows processed per outer loop */
-  /* Work buffer for dequantized coefficients (IDCT input) */
-  JBLOCKIMAGE coeff_data;
-  /* Work buffer for cross-block smoothing input */
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-  JBLOCKIMAGE bsmooth[3];	/* this is optional */
-  int whichb;
-#endif
-  /* Work buffer for downsampled image data (see comments at head of file) */
-  JSAMPIMAGE sampled_data[2];
-  int whichss, ri;
-  short ci, i;
-  boolean single_scan;
-
-  /* Compute dimensions of full-size pixel buffers */
-  /* Note these are the same whether interleaved or not. */
-  rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
-  fullsize_width = jround_up(cinfo->image_width,
-			     (long) (cinfo->max_h_samp_factor * DCTSIZE));
-
-  /* Allocate all working memory that doesn't depend on scan info */
-  prepare_range_limit_table(cinfo);
-  /* output_workspace is the color-processed data */
-  output_workspace = alloc_sampimage(cinfo, (int) cinfo->final_out_comps,
-				     (long) rows_in_mem, fullsize_width);
-
-  /* Get a big image: fullsize_image is sample data after upsampling. */
-  fullsize_image = (big_sarray_ptr *) (*cinfo->emethods->alloc_small)
-			(cinfo->num_components * SIZEOF(big_sarray_ptr));
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    fullsize_image[ci] = (*cinfo->emethods->request_big_sarray)
-			(fullsize_width,
-			 jround_up(cinfo->image_height, (long) rows_in_mem),
-			 (long) rows_in_mem);
-  }
-  /* Also get an area for pointers to currently accessible chunks */
-  fullsize_ptrs = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
-				(cinfo->num_components * SIZEOF(JSAMPARRAY));
-
-  /* Tell the memory manager to instantiate big arrays */
-  (*cinfo->emethods->alloc_big_arrays)
-	 /* extra sarray space is for downsampled-data buffers: */
-	((long) (fullsize_width			/* max width in samples */
-	 * cinfo->max_v_samp_factor*(DCTSIZE+2)	/* max height */
-	 * cinfo->num_components),		/* max components per scan */
-	 /* extra barray space is for MCU-row buffers: */
-	 (long) ((fullsize_width / DCTSIZE)	/* max width in blocks */
-	 * cinfo->max_v_samp_factor		/* max height */
-	 * cinfo->num_components		/* max components per scan */
-	 * (cinfo->do_block_smoothing ? 4 : 1)),/* how many of these we need */
-	 /* no extra "medium"-object space */
-	 (long) 0);
-  /* NB: if quantizer needs any "medium" size objects, it must get them */
-  /* at color_quant_init time */
-
-  /* If file is single-scan, we can do color quantization prescan on-the-fly
-   * during the scan (we must be doing 2-pass quantization, else this method
-   * would not have been selected).  If it is multiple scans, we have to make
-   * a separate pass after we've collected all the components.  (We could save
-   * some I/O by doing CQ prescan during the last scan, but the extra logic
-   * doesn't seem worth the trouble.)
-   */
-
-  single_scan = (cinfo->comps_in_scan == cinfo->num_components);
-
-  /* Account for passes needed (color quantizer adds its passes separately).
-   * If multiscan file, we guess that each component has its own scan,
-   * and increment completed_passes by the number of components in the scan.
-   */
-
-  if (single_scan)
-    cinfo->total_passes++;	/* the single scan */
-  else {
-    cinfo->total_passes += cinfo->num_components; /* guessed # of scans */
-    if (cinfo->two_pass_quantize)
-      cinfo->total_passes++;	/* account for separate CQ prescan pass */
-  }
-  if (! cinfo->two_pass_quantize)
-    cinfo->total_passes++;	/* count output pass unless quantizer does it */
-
-  /* Loop over scans in file */
-
-  do {
-    
-    /* Prepare for this scan */
-    if (cinfo->comps_in_scan == 1) {
-      noninterleaved_scan_setup(cinfo);
-      /* Need to read Vk MCU rows to obtain Vk block rows */
-      mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
-    } else {
-      interleaved_scan_setup(cinfo);
-      /* in an interleaved scan, one MCU row provides Vk block rows */
-      mcu_rows_per_loop = 1;
-    }
-    
-    /* Allocate scan-local working memory */
-    /* coeff_data holds a single MCU row of coefficient blocks */
-    coeff_data = alloc_MCU_row(cinfo);
-    /* if doing cross-block smoothing, need extra space for its input */
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-    if (cinfo->do_block_smoothing) {
-      bsmooth[0] = alloc_MCU_row(cinfo);
-      bsmooth[1] = alloc_MCU_row(cinfo);
-      bsmooth[2] = alloc_MCU_row(cinfo);
-    }
-#endif
-    /* sampled_data is sample data before upsampling */
-    alloc_sampling_buffer(cinfo, sampled_data);
-
-    /* line up the big buffers for components in this scan */
-    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-      fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
-	(fullsize_image[cinfo->cur_comp_info[ci]->component_index],
-	 (long) 0, TRUE);
-    }
-    
-    /* Initialize to read scan data */
-    
-    (*cinfo->methods->entropy_decode_init) (cinfo);
-    (*cinfo->methods->upsample_init) (cinfo);
-    (*cinfo->methods->disassemble_init) (cinfo);
-    
-    /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
-    
-    pixel_rows_output = 0;
-    whichss = 1;		/* arrange to start with sampled_data[0] */
-    
-    for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
-	 cur_mcu_row += mcu_rows_per_loop) {
-      (*cinfo->methods->progress_monitor) (cinfo, cur_mcu_row,
-					   cinfo->MCU_rows_in_scan);
-
-      whichss ^= 1;		/* switch to other downsampled-data buffer */
-
-      /* Obtain v_samp_factor block rows of each component in the scan. */
-      /* This is a single MCU row if interleaved, multiple MCU rows if not. */
-      /* In the noninterleaved case there might be fewer than v_samp_factor */
-      /* block rows remaining; if so, pad with copies of the last pixel row */
-      /* so that upsampling doesn't have to treat it as a special case. */
-      
-      for (ri = 0; ri < mcu_rows_per_loop; ri++) {
-	if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
-	  /* OK to actually read an MCU row. */
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-	  if (cinfo->do_block_smoothing)
-	    get_smoothed_row(cinfo, coeff_data,
-			     bsmooth, &whichb, cur_mcu_row + ri);
-	  else
-#endif
-	    (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
-	  
-	  (*cinfo->methods->reverse_DCT) (cinfo, coeff_data,
-					  sampled_data[whichss],
-					  ri * DCTSIZE);
-	} else {
-	  /* Need to pad out with copies of the last downsampled row. */
-	  /* This can only happen if there is just one component. */
-	  duplicate_row(sampled_data[whichss][0],
-			cinfo->cur_comp_info[0]->downsampled_width,
-			ri * DCTSIZE - 1, DCTSIZE);
-	}
-      }
-      
-      /* Upsample the data */
-      /* First time through is a special case */
-      
-      if (cur_mcu_row) {
-	/* Expand last row group of previous set */
-	expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
-	       (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
-	       (short) (DCTSIZE-1));
-	/* If single scan, can do color quantization prescan on-the-fly */
-	if (single_scan)
-	  (*cinfo->methods->color_quant_prescan) (cinfo, rows_in_mem,
-						  fullsize_ptrs,
-						  output_workspace[0]);
-	/* Realign the big buffers */
-	pixel_rows_output += rows_in_mem;
-	for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-	  fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
-	    (fullsize_image[cinfo->cur_comp_info[ci]->component_index],
-	     pixel_rows_output, TRUE);
-	}
-	/* Expand first row group of this set */
-	expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
-	       (short) (DCTSIZE+1), (short) 0, (short) 1,
-	       (short) 0);
-      } else {
-	/* Expand first row group with dummy above-context */
-	expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
-	       (short) (-1), (short) 0, (short) 1,
-	       (short) 0);
-      }
-      /* Expand second through next-to-last row groups of this set */
-      for (i = 1; i <= DCTSIZE-2; i++) {
-	expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
-	       (short) (i-1), (short) i, (short) (i+1),
-	       (short) i);
-      }
-    } /* end of loop over scan's data */
-    
-    /* Expand the last row group with dummy below-context */
-    /* Note whichss points to last buffer side used */
-    expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
-	   (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
-	   (short) (DCTSIZE-1));
-    /* If single scan, finish on-the-fly color quantization prescan */
-    if (single_scan)
-      (*cinfo->methods->color_quant_prescan) (cinfo,
-			(int) (cinfo->image_height - pixel_rows_output),
-			fullsize_ptrs, output_workspace[0]);
-    
-    /* Clean up after the scan */
-    (*cinfo->methods->disassemble_term) (cinfo);
-    (*cinfo->methods->upsample_term) (cinfo);
-    (*cinfo->methods->entropy_decode_term) (cinfo);
-    (*cinfo->methods->read_scan_trailer) (cinfo);
-    if (single_scan)
-      cinfo->completed_passes++;
-    else
-      cinfo->completed_passes += cinfo->comps_in_scan;
-
-    /* Release scan-local working memory */
-    free_MCU_row(cinfo, coeff_data);
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-    if (cinfo->do_block_smoothing) {
-      free_MCU_row(cinfo, bsmooth[0]);
-      free_MCU_row(cinfo, bsmooth[1]);
-      free_MCU_row(cinfo, bsmooth[2]);
-    }
-#endif
-    free_sampling_buffer(cinfo, sampled_data);
-    
-    /* Repeat if there is another scan */
-  } while ((!single_scan) && (*cinfo->methods->read_scan_header) (cinfo));
-
-  if (single_scan) {
-    /* If we expected just one scan, make SURE there's just one */
-    if ((*cinfo->methods->read_scan_header) (cinfo))
-      WARNMS(cinfo->emethods, "Didn't expect more than one scan");
-    /* We did the CQ prescan on-the-fly, so we are all set. */
-  } else {
-    /* For multiple-scan file, do the CQ prescan as a separate pass. */
-    /* The main reason why prescan is passed the output_workspace is */
-    /* so that we can use scan_big_image to call it... */
-    if (cinfo->two_pass_quantize)
-      scan_big_image(cinfo, cinfo->methods->color_quant_prescan);
-  }
-
-  /* Now that we've collected the data, do color processing and output */
-  if (cinfo->two_pass_quantize)
-    (*cinfo->methods->color_quant_doit) (cinfo, scan_big_image);
-  else
-    scan_big_image(cinfo, emit_1pass);
-
-  /* Release working memory */
-  /* (no work -- we let free_all release what's needful) */
-}
-
-#endif /* NEED_COMPLEX_CONTROLLER */
-
-
-/*
- * The method selection routine for decompression pipeline controllers.
- * Note that at this point we've already read the JPEG header and first SOS,
- * so we can tell whether the input is one scan or not.
- */
-
-GLOBAL void
-jseldpipeline (decompress_info_ptr cinfo)
-{
-  /* simplify subsequent tests on color quantization */
-  if (! cinfo->quantize_colors)
-    cinfo->two_pass_quantize = FALSE;
-  
-  if (cinfo->comps_in_scan == cinfo->num_components) {
-    /* It's a single-scan file */
-    if (cinfo->two_pass_quantize) {
-#ifdef NEED_COMPLEX_CONTROLLER
-      cinfo->methods->d_pipeline_controller = complex_dcontroller;
-#else
-      ERREXIT(cinfo->emethods, "2-pass quantization support was not compiled");
-#endif
-    } else
-      cinfo->methods->d_pipeline_controller = simple_dcontroller;
-  } else {
-    /* It's a multiple-scan file */
-#ifdef NEED_COMPLEX_CONTROLLER
-    cinfo->methods->d_pipeline_controller = complex_dcontroller;
-#else
-    ERREXIT(cinfo->emethods, "Multiple-scan support was not compiled");
-#endif
-  }
-}
diff --git a/jdpostct.c b/jdpostct.c
new file mode 100644
index 0000000..d6fa61a
--- /dev/null
+++ b/jdpostct.c
@@ -0,0 +1,275 @@
+/*
+ * jdpostct.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains the decompression postprocessing controller.
+ * This controller manages the upsampling, color conversion, and color
+ * quantization/reduction steps; specifically, it controls the buffering
+ * between upsample/color conversion and color quantization/reduction.
+ *
+ * If no color quantization/reduction is required, then this module has no
+ * work to do, and it just hands off to the upsample/color conversion code.
+ * An integrated upsample/convert/quantize process would replace this module
+ * entirely.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private buffer controller object */
+
+typedef struct {
+  struct jpeg_d_post_controller pub; /* public fields */
+
+  /* Color quantization source buffer: this holds output data from
+   * the upsample/color conversion step to be passed to the quantizer.
+   * For two-pass color quantization, we need a full-image buffer;
+   * for one-pass operation, a strip buffer is sufficient.
+   */
+  jvirt_sarray_ptr whole_image;	/* virtual array, or NULL if one-pass */
+  JSAMPARRAY buffer;		/* strip buffer, or current strip of virtual */
+  JDIMENSION strip_height;	/* buffer size in rows */
+  /* for two-pass mode only: */
+  JDIMENSION starting_row;	/* row # of first row in current strip */
+  JDIMENSION next_row;		/* index of next row to fill/empty in strip */
+} my_post_controller;
+
+typedef my_post_controller * my_post_ptr;
+
+
+/* Forward declarations */
+METHODDEF void post_process_1pass
+	JPP((j_decompress_ptr cinfo,
+	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+	     JDIMENSION in_row_groups_avail,
+	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+	     JDIMENSION out_rows_avail));
+#ifdef QUANT_2PASS_SUPPORTED
+METHODDEF void post_process_prepass
+	JPP((j_decompress_ptr cinfo,
+	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+	     JDIMENSION in_row_groups_avail,
+	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+	     JDIMENSION out_rows_avail));
+METHODDEF void post_process_2pass
+	JPP((j_decompress_ptr cinfo,
+	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+	     JDIMENSION in_row_groups_avail,
+	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+	     JDIMENSION out_rows_avail));
+#endif
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF void
+start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+  my_post_ptr post = (my_post_ptr) cinfo->post;
+
+  switch (pass_mode) {
+  case JBUF_PASS_THRU:
+    if (cinfo->quantize_colors) {
+      /* Single-pass processing with color quantization. */
+      post->pub.post_process_data = post_process_1pass;
+    } else {
+      /* For single-pass processing without color quantization,
+       * I have no work to do; just call the upsampler directly.
+       */
+      post->pub.post_process_data = cinfo->upsample->upsample;
+    }
+    break;
+#ifdef QUANT_2PASS_SUPPORTED
+  case JBUF_SAVE_AND_PASS:
+    /* First pass of 2-pass quantization */
+    if (post->whole_image == NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    post->pub.post_process_data = post_process_prepass;
+    break;
+  case JBUF_CRANK_DEST:
+    /* Second pass of 2-pass quantization */
+    if (post->whole_image == NULL)
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    post->pub.post_process_data = post_process_2pass;
+    break;
+#endif /* QUANT_2PASS_SUPPORTED */
+  default:
+    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+    break;
+  }
+  post->starting_row = post->next_row = 0;
+}
+
+
+/*
+ * Process some data in the one-pass (strip buffer) case.
+ * This is used for color precision reduction as well as one-pass quantization.
+ */
+
+METHODDEF void
+post_process_1pass (j_decompress_ptr cinfo,
+		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+		    JDIMENSION in_row_groups_avail,
+		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+		    JDIMENSION out_rows_avail)
+{
+  my_post_ptr post = (my_post_ptr) cinfo->post;
+  JDIMENSION num_rows, max_rows;
+
+  /* Fill the buffer, but not more than what we can dump out in one go. */
+  /* Note we rely on the upsampler to detect bottom of image. */
+  max_rows = out_rows_avail - *out_row_ctr;
+  if (max_rows > post->strip_height)
+    max_rows = post->strip_height;
+  num_rows = 0;
+  (*cinfo->upsample->upsample) (cinfo,
+		input_buf, in_row_group_ctr, in_row_groups_avail,
+		post->buffer, &num_rows, max_rows);
+  /* Quantize and emit data. */
+  (*cinfo->cquantize->color_quantize) (cinfo,
+		post->buffer, output_buf + *out_row_ctr, (int) num_rows);
+  *out_row_ctr += num_rows;
+}
+
+
+#ifdef QUANT_2PASS_SUPPORTED
+
+/*
+ * Process some data in the first pass of 2-pass quantization.
+ */
+
+METHODDEF void
+post_process_prepass (j_decompress_ptr cinfo,
+		      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+		      JDIMENSION in_row_groups_avail,
+		      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+		      JDIMENSION out_rows_avail)
+{
+  my_post_ptr post = (my_post_ptr) cinfo->post;
+  JDIMENSION old_next_row, num_rows;
+
+  /* Reposition virtual buffer if at start of strip. */
+  if (post->next_row == 0) {
+    post->buffer = (*cinfo->mem->access_virt_sarray)
+	((j_common_ptr) cinfo, post->whole_image, post->starting_row, TRUE);
+  }
+
+  /* Upsample some data (up to a strip height's worth). */
+  old_next_row = post->next_row;
+  (*cinfo->upsample->upsample) (cinfo,
+		input_buf, in_row_group_ctr, in_row_groups_avail,
+		post->buffer, &post->next_row, post->strip_height);
+
+  /* Allow quantizer to scan new data.  No data is emitted, */
+  /* but we advance out_row_ctr so outer loop can tell when we're done. */
+  if (post->next_row > old_next_row) {
+    num_rows = post->next_row - old_next_row;
+    (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
+					 (JSAMPARRAY) NULL, (int) num_rows);
+    *out_row_ctr += num_rows;
+  }
+
+  /* Advance if we filled the strip. */
+  if (post->next_row >= post->strip_height) {
+    post->starting_row += post->strip_height;
+    post->next_row = 0;
+  }
+}
+
+
+/*
+ * Process some data in the second pass of 2-pass quantization.
+ */
+
+METHODDEF void
+post_process_2pass (j_decompress_ptr cinfo,
+		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+		    JDIMENSION in_row_groups_avail,
+		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+		    JDIMENSION out_rows_avail)
+{
+  my_post_ptr post = (my_post_ptr) cinfo->post;
+  JDIMENSION num_rows, max_rows;
+
+  /* Reposition virtual buffer if at start of strip. */
+  if (post->next_row == 0) {
+    post->buffer = (*cinfo->mem->access_virt_sarray)
+	((j_common_ptr) cinfo, post->whole_image, post->starting_row, FALSE);
+  }
+
+  /* Determine number of rows to emit. */
+  num_rows = post->strip_height - post->next_row; /* available in strip */
+  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
+  if (num_rows > max_rows)
+    num_rows = max_rows;
+  /* We have to check bottom of image here, can't depend on upsampler. */
+  max_rows = cinfo->output_height - post->starting_row;
+  if (num_rows > max_rows)
+    num_rows = max_rows;
+
+  /* Quantize and emit data. */
+  (*cinfo->cquantize->color_quantize) (cinfo,
+		post->buffer + post->next_row, output_buf + *out_row_ctr,
+		(int) num_rows);
+  *out_row_ctr += num_rows;
+
+  /* Advance if we filled the strip. */
+  post->next_row += num_rows;
+  if (post->next_row >= post->strip_height) {
+    post->starting_row += post->strip_height;
+    post->next_row = 0;
+  }
+}
+
+#endif /* QUANT_2PASS_SUPPORTED */
+
+
+/*
+ * Initialize postprocessing controller.
+ */
+
+GLOBAL void
+jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
+{
+  my_post_ptr post;
+
+  post = (my_post_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_post_controller));
+  cinfo->post = (struct jpeg_d_post_controller *) post;
+  post->pub.start_pass = start_pass_dpost;
+  post->whole_image = NULL;	/* flag for no virtual arrays */
+
+  /* Create the quantization buffer, if needed */
+  if (cinfo->quantize_colors) {
+    /* The buffer strip height is max_v_samp_factor, which is typically
+     * an efficient number of rows for upsampling to return.
+     * (In the presence of output rescaling, we might want to be smarter?)
+     */
+    post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
+    if (need_full_buffer) {
+      /* Two-pass color quantization: need full-image storage. */
+#ifdef QUANT_2PASS_SUPPORTED
+      post->whole_image = (*cinfo->mem->request_virt_sarray)
+	((j_common_ptr) cinfo, JPOOL_IMAGE,
+	 cinfo->output_width * cinfo->out_color_components,
+	 cinfo->output_height, post->strip_height);
+#else
+      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif /* QUANT_2PASS_SUPPORTED */
+    } else {
+      /* One-pass color quantization: just make a strip buffer. */
+      post->buffer = (*cinfo->mem->alloc_sarray)
+	((j_common_ptr) cinfo, JPOOL_IMAGE,
+	 cinfo->output_width * cinfo->out_color_components,
+	 post->strip_height);
+    }
+  }
+}
diff --git a/jdsample.c b/jdsample.c
index 3f49e29..661e198 100644
--- a/jdsample.c
+++ b/jdsample.c
@@ -1,37 +1,181 @@
 /*
  * jdsample.c
  *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains upsampling routines.
- * These routines are invoked via the upsample and
- * upsample_init/term methods.
+ *
+ * Upsampling input data is counted in "row groups".  A row group
+ * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
+ * sample rows of each component.  Upsampling will normally produce
+ * max_v_samp_factor pixel rows from each row group (but this could vary
+ * if the upsampler is applying a scale factor of its own).
  *
  * An excellent reference for image resampling is
  *   Digital Image Warping, George Wolberg, 1990.
  *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Pointer to routine to upsample a single component */
+typedef JMETHOD(void, upsample1_ptr,
+		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
+		 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
+
+/* Private subobject */
+
+typedef struct {
+  struct jpeg_upsampler pub;	/* public fields */
+
+  /* Color conversion buffer.  When using separate upsampling and color
+   * conversion steps, this buffer holds one upsampled row group until it
+   * has been color converted and output.
+   * Note: we do not allocate any storage for component(s) which are full-size,
+   * ie do not need rescaling.  The corresponding entry of color_buf[] is
+   * simply set to point to the input data array, thereby avoiding copying.
+   */
+  JSAMPARRAY color_buf[MAX_COMPONENTS];
+
+  /* Per-component upsampling method pointers */
+  upsample1_ptr methods[MAX_COMPONENTS];
+
+  int next_row_out;		/* counts rows emitted from color_buf */
+  JDIMENSION rows_to_go;	/* counts rows remaining in image */
+
+  /* Height of an input row group for each component. */
+  int rowgroup_height[MAX_COMPONENTS];
+
+  /* These arrays save pixel expansion factors so that int_expand need not
+   * recompute them each time.  They are unused for other upsampling methods.
+   */
+  UINT8 h_expand[MAX_COMPONENTS];
+  UINT8 v_expand[MAX_COMPONENTS];
+} my_upsampler;
+
+typedef my_upsampler * my_upsample_ptr;
 
 
 /*
- * Initialize for upsampling a scan.
+ * Initialize for an upsampling pass.
  */
 
 METHODDEF void
-upsample_init (decompress_info_ptr cinfo)
+start_pass_upsample (j_decompress_ptr cinfo)
 {
-  /* no work for now */
+  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+
+  /* Mark the conversion buffer empty */
+  upsample->next_row_out = cinfo->max_v_samp_factor;
+  /* Initialize total-height counter for detecting bottom of image */
+  upsample->rows_to_go = cinfo->output_height;
 }
 
 
 /*
- * Upsample pixel values of a single component.
- * This version handles any integral sampling ratios.
+ * Control routine to do upsampling (and color conversion).
  *
+ * In this version we upsample each component independently.
+ * We upsample one row group into the conversion buffer, then apply
+ * color conversion a row at a time.
+ */
+
+METHODDEF void
+sep_upsample (j_decompress_ptr cinfo,
+	      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+	      JDIMENSION in_row_groups_avail,
+	      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+	      JDIMENSION out_rows_avail)
+{
+  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+  int ci;
+  jpeg_component_info * compptr;
+  JDIMENSION num_rows;
+
+  /* Fill the conversion buffer, if it's empty */
+  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
+    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+	 ci++, compptr++) {
+      /* Invoke per-component upsample method.  Notice we pass a POINTER
+       * to color_buf[ci], so that fullsize_upsample can change it.
+       */
+      (*upsample->methods[ci]) (cinfo, compptr,
+	input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
+	upsample->color_buf + ci);
+    }
+    upsample->next_row_out = 0;
+  }
+
+  /* Color-convert and emit rows */
+
+  /* How many we have in the buffer: */
+  num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
+  /* Not more than the distance to the end of the image.  Need this test
+   * in case the image height is not a multiple of max_v_samp_factor:
+   */
+  if (num_rows > upsample->rows_to_go) 
+    num_rows = upsample->rows_to_go;
+  /* And not more than what the client can accept: */
+  out_rows_avail -= *out_row_ctr;
+  if (num_rows > out_rows_avail)
+    num_rows = out_rows_avail;
+
+  (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
+				     (JDIMENSION) upsample->next_row_out,
+				     output_buf + *out_row_ctr,
+				     (int) num_rows);
+
+  /* Adjust counts */
+  *out_row_ctr += num_rows;
+  upsample->rows_to_go -= num_rows;
+  upsample->next_row_out += num_rows;
+  /* When the buffer is emptied, declare this input row group consumed */
+  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
+    (*in_row_group_ctr)++;
+}
+
+
+/*
+ * These are the routines invoked by sep_upsample to upsample pixel values
+ * of a single component.  One row group is processed per call.
+ */
+
+
+/*
+ * For full-size components, we just make color_buf[ci] point at the
+ * input buffer, and thus avoid copying any data.  Note that this is
+ * safe only because sep_upsample doesn't declare the input row group
+ * "consumed" until we are done color converting and emitting it.
+ */
+
+METHODDEF void
+fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+		   JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+  *output_data_ptr = input_data;
+}
+
+
+/*
+ * This is a no-op version used for "uninteresting" components.
+ * These components will not be referenced by color conversion.
+ */
+
+METHODDEF void
+noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+  *output_data_ptr = NULL;	/* safety check */
+}
+
+
+/*
+ * This version handles any integral sampling ratios.
  * This is not used for typical JPEG files, so it need not be fast.
  * Nor, for that matter, is it particularly accurate: the algorithm is
  * simple replication of the input pixel onto the corresponding output
@@ -42,81 +186,131 @@
  */
 
 METHODDEF void
-int_upsample (decompress_info_ptr cinfo, int which_component,
-	      long input_cols, int input_rows,
-	      long output_cols, int output_rows,
-	      JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-	      JSAMPARRAY output_data)
+int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	      JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
 {
-  jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
+  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+  JSAMPARRAY output_data = *output_data_ptr;
   register JSAMPROW inptr, outptr;
   register JSAMPLE invalue;
-  register short h_expand, h;
-  short v_expand, v;
+  register int h;
+  JSAMPROW outend;
+  int h_expand, v_expand;
   int inrow, outrow;
-  register long incol;
 
-#ifdef DEBUG			/* for debugging pipeline controller */
-  if (input_rows != compptr->v_samp_factor ||
-      output_rows != cinfo->max_v_samp_factor ||
-      (input_cols % compptr->h_samp_factor) != 0 ||
-      (output_cols % cinfo->max_h_samp_factor) != 0 ||
-      output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
-    ERREXIT(cinfo->emethods, "Bogus upsample parameters");
-#endif
+  h_expand = upsample->h_expand[compptr->component_index];
+  v_expand = upsample->v_expand[compptr->component_index];
 
-  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
-  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
-
-  outrow = 0;
-  for (inrow = 0; inrow < input_rows; inrow++) {
-    for (v = 0; v < v_expand; v++) {
-      inptr = input_data[inrow];
-      outptr = output_data[outrow++];
-      for (incol = 0; incol < input_cols; incol++) {
-	invalue = GETJSAMPLE(*inptr++);
-	for (h = 0; h < h_expand; h++) {
-	  *outptr++ = invalue;
-	}
+  inrow = outrow = 0;
+  while (outrow < cinfo->max_v_samp_factor) {
+    /* Generate one output row with proper horizontal expansion */
+    inptr = input_data[inrow];
+    outptr = output_data[outrow];
+    outend = outptr + cinfo->output_width;
+    while (outptr < outend) {
+      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
+      for (h = h_expand; h > 0; h--) {
+	*outptr++ = invalue;
       }
     }
+    /* Generate any additional output rows by duplicating the first one */
+    if (v_expand > 1) {
+      jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
+			v_expand-1, cinfo->output_width);
+    }
+    inrow++;
+    outrow += v_expand;
+  }
+}
+
+
+/*
+ * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
+ * It's still a box filter.
+ */
+
+METHODDEF void
+h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+  JSAMPARRAY output_data = *output_data_ptr;
+  register JSAMPROW inptr, outptr;
+  register JSAMPLE invalue;
+  JSAMPROW outend;
+  int inrow;
+
+  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
+    inptr = input_data[inrow];
+    outptr = output_data[inrow];
+    outend = outptr + cinfo->output_width;
+    while (outptr < outend) {
+      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
+      *outptr++ = invalue;
+      *outptr++ = invalue;
+    }
   }
 }
 
 
 /*
- * Upsample pixel values of a single component.
- * This version handles the common case of 2:1 horizontal and 1:1 vertical.
+ * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
+ * It's still a box filter.
+ */
+
+METHODDEF void
+h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+  JSAMPARRAY output_data = *output_data_ptr;
+  register JSAMPROW inptr, outptr;
+  register JSAMPLE invalue;
+  JSAMPROW outend;
+  int inrow, outrow;
+
+  inrow = outrow = 0;
+  while (outrow < cinfo->max_v_samp_factor) {
+    inptr = input_data[inrow];
+    outptr = output_data[outrow];
+    outend = outptr + cinfo->output_width;
+    while (outptr < outend) {
+      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
+      *outptr++ = invalue;
+      *outptr++ = invalue;
+    }
+    jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
+		      1, cinfo->output_width);
+    inrow++;
+    outrow += 2;
+  }
+}
+
+
+/*
+ * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
  *
  * The upsampling algorithm is linear interpolation between pixel centers,
  * also known as a "triangle filter".  This is a good compromise between
  * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
  * of the way between input pixel centers.
+ *
+ * A note about the "bias" calculations: when rounding fractional values to
+ * integer, we do not want to always round 0.5 up to the next integer.
+ * If we did that, we'd introduce a noticeable bias towards larger values.
+ * Instead, this code is arranged so that 0.5 will be rounded up or down at
+ * alternate pixel locations (a simple ordered dither pattern).
  */
 
 METHODDEF void
-h2v1_upsample (decompress_info_ptr cinfo, int which_component,
-	       long input_cols, int input_rows,
-	       long output_cols, int output_rows,
-	       JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-	       JSAMPARRAY output_data)
+h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+		     JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
 {
+  JSAMPARRAY output_data = *output_data_ptr;
   register JSAMPROW inptr, outptr;
   register int invalue;
+  register JDIMENSION colctr;
   int inrow;
-  register long colctr;
 
-#ifdef DEBUG			/* for debugging pipeline controller */
-  jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
-  if (input_rows != compptr->v_samp_factor ||
-      output_rows != cinfo->max_v_samp_factor ||
-      (input_cols % compptr->h_samp_factor) != 0 ||
-      (output_cols % cinfo->max_h_samp_factor) != 0 ||
-      output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
-    ERREXIT(cinfo->emethods, "Bogus upsample parameters");
-#endif
-
-  for (inrow = 0; inrow < input_rows; inrow++) {
+  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
     inptr = input_data[inrow];
     outptr = output_data[inrow];
     /* Special case for first column */
@@ -124,165 +318,161 @@
     *outptr++ = (JSAMPLE) invalue;
     *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
 
-    for (colctr = input_cols - 2; colctr > 0; colctr--) {
+    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
       /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
       invalue = GETJSAMPLE(*inptr++) * 3;
-      *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 2) >> 2);
+      *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
       *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
     }
 
     /* Special case for last column */
     invalue = GETJSAMPLE(*inptr);
-    *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 2) >> 2);
+    *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
     *outptr++ = (JSAMPLE) invalue;
   }
 }
 
 
 /*
- * Upsample pixel values of a single component.
- * This version handles the common case of 2:1 horizontal and 2:1 vertical.
+ * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
+ * Again a triangle filter; see comments for h2v1 case, above.
  *
- * The upsampling algorithm is linear interpolation between pixel centers,
- * also known as a "triangle filter".  This is a good compromise between
- * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
- * of the way between input pixel centers.
+ * It is OK for us to reference the adjacent input rows because we demanded
+ * context from the main buffer controller (see initialization code).
  */
 
 METHODDEF void
-h2v2_upsample (decompress_info_ptr cinfo, int which_component,
-	       long input_cols, int input_rows,
-	       long output_cols, int output_rows,
-	       JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-	       JSAMPARRAY output_data)
+h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+		     JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
 {
+  JSAMPARRAY output_data = *output_data_ptr;
   register JSAMPROW inptr0, inptr1, outptr;
-#ifdef EIGHT_BIT_SAMPLES
+#if BITS_IN_JSAMPLE == 8
   register int thiscolsum, lastcolsum, nextcolsum;
 #else
   register INT32 thiscolsum, lastcolsum, nextcolsum;
 #endif
+  register JDIMENSION colctr;
   int inrow, outrow, v;
-  register long colctr;
 
-#ifdef DEBUG			/* for debugging pipeline controller */
-  jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
-  if (input_rows != compptr->v_samp_factor ||
-      output_rows != cinfo->max_v_samp_factor ||
-      (input_cols % compptr->h_samp_factor) != 0 ||
-      (output_cols % cinfo->max_h_samp_factor) != 0 ||
-      output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
-    ERREXIT(cinfo->emethods, "Bogus upsample parameters");
-#endif
-
-  outrow = 0;
-  for (inrow = 0; inrow < input_rows; inrow++) {
+  inrow = outrow = 0;
+  while (outrow < cinfo->max_v_samp_factor) {
     for (v = 0; v < 2; v++) {
       /* inptr0 points to nearest input row, inptr1 points to next nearest */
       inptr0 = input_data[inrow];
-      if (v == 0) {		/* next nearest is row above */
-	if (inrow == 0)
-	  inptr1 = above[input_rows-1];
-	else
-	  inptr1 = input_data[inrow-1];
-      } else {			/* next nearest is row below */
-	if (inrow == input_rows-1)
-	  inptr1 = below[0];
-	else
-	  inptr1 = input_data[inrow+1];
-      }
+      if (v == 0)		/* next nearest is row above */
+	inptr1 = input_data[inrow-1];
+      else			/* next nearest is row below */
+	inptr1 = input_data[inrow+1];
       outptr = output_data[outrow++];
 
       /* Special case for first column */
       thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
       nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
       *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
-      *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 8) >> 4);
+      *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
       lastcolsum = thiscolsum; thiscolsum = nextcolsum;
 
-      for (colctr = input_cols - 2; colctr > 0; colctr--) {
+      for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
 	/* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
 	/* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
 	nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
 	*outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
-	*outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 8) >> 4);
+	*outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
 	lastcolsum = thiscolsum; thiscolsum = nextcolsum;
       }
 
       /* Special case for last column */
       *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
-      *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
+      *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
     }
+    inrow++;
   }
 }
 
 
 /*
- * Upsample pixel values of a single component.
- * This version handles the special case of a full-size component.
- */
-
-METHODDEF void
-fullsize_upsample (decompress_info_ptr cinfo, int which_component,
-		   long input_cols, int input_rows,
-		   long output_cols, int output_rows,
-		   JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
-		   JSAMPARRAY output_data)
-{
-#ifdef DEBUG			/* for debugging pipeline controller */
-  if (input_cols != output_cols || input_rows != output_rows)
-    ERREXIT(cinfo->emethods, "Pipeline controller messed up");
-#endif
-
-  jcopy_sample_rows(input_data, 0, output_data, 0, output_rows, output_cols);
-}
-
-
-
-/*
- * Clean up after a scan.
- */
-
-METHODDEF void
-upsample_term (decompress_info_ptr cinfo)
-{
-  /* no work for now */
-}
-
-
-
-/*
- * The method selection routine for upsampling.
- * Note that we must select a routine for each component.
+ * Module initialization routine for upsampling.
  */
 
 GLOBAL void
-jselupsample (decompress_info_ptr cinfo)
+jinit_upsampler (j_decompress_ptr cinfo)
 {
-  short ci;
+  my_upsample_ptr upsample;
+  int ci;
   jpeg_component_info * compptr;
+  boolean need_buffer, do_fancy;
+  int h_in_group, v_in_group, h_out_group, v_out_group;
 
-  if (cinfo->CCIR601_sampling)
-    ERREXIT(cinfo->emethods, "CCIR601 upsampling not implemented yet");
+  upsample = (my_upsample_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_upsampler));
+  cinfo->upsample = (struct jpeg_upsampler *) upsample;
+  upsample->pub.start_pass = start_pass_upsample;
+  upsample->pub.upsample = sep_upsample;
+  upsample->pub.need_context_rows = FALSE; /* until we find out differently */
 
-  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-    compptr = cinfo->cur_comp_info[ci];
-    if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
-	compptr->v_samp_factor == cinfo->max_v_samp_factor)
-      cinfo->methods->upsample[ci] = fullsize_upsample;
-    else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
-	     compptr->v_samp_factor == cinfo->max_v_samp_factor)
-      cinfo->methods->upsample[ci] = h2v1_upsample;
-    else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
-	     compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor)
-      cinfo->methods->upsample[ci] = h2v2_upsample;
-    else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
-	     (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0)
-      cinfo->methods->upsample[ci] = int_upsample;
-    else
-      ERREXIT(cinfo->emethods, "Fractional upsampling not implemented yet");
+  if (cinfo->CCIR601_sampling)	/* this isn't supported */
+    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
+
+  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
+   * so don't ask for it.
+   */
+  do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
+
+  /* Verify we can handle the sampling factors, select per-component methods,
+   * and create storage as needed.
+   */
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    /* Compute size of an "input group" after IDCT scaling.  This many samples
+     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
+     */
+    h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
+		 cinfo->min_DCT_scaled_size;
+    v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+		 cinfo->min_DCT_scaled_size;
+    h_out_group = cinfo->max_h_samp_factor;
+    v_out_group = cinfo->max_v_samp_factor;
+    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
+    need_buffer = TRUE;
+    if (! compptr->component_needed) {
+      /* Don't bother to upsample an uninteresting component. */
+      upsample->methods[ci] = noop_upsample;
+      need_buffer = FALSE;
+    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
+      /* Fullsize components can be processed without any work. */
+      upsample->methods[ci] = fullsize_upsample;
+      need_buffer = FALSE;
+    } else if (h_in_group * 2 == h_out_group &&
+	       v_in_group == v_out_group) {
+      /* Special cases for 2h1v upsampling */
+      if (do_fancy && compptr->downsampled_width > 2)
+	upsample->methods[ci] = h2v1_fancy_upsample;
+      else
+	upsample->methods[ci] = h2v1_upsample;
+    } else if (h_in_group * 2 == h_out_group &&
+	       v_in_group * 2 == v_out_group) {
+      /* Special cases for 2h2v upsampling */
+      if (do_fancy && compptr->downsampled_width > 2) {
+	upsample->methods[ci] = h2v2_fancy_upsample;
+	upsample->pub.need_context_rows = TRUE;
+      } else
+	upsample->methods[ci] = h2v2_upsample;
+    } else if ((h_out_group % h_in_group) == 0 &&
+	       (v_out_group % v_in_group) == 0) {
+      /* Generic integral-factors upsampling method */
+      upsample->methods[ci] = int_upsample;
+      upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
+      upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
+    } else
+      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
+    if (need_buffer) {
+      upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
+	((j_common_ptr) cinfo, JPOOL_IMAGE,
+	 (JDIMENSION) jround_up((long) cinfo->output_width,
+				(long) cinfo->max_h_samp_factor),
+	 (JDIMENSION) cinfo->max_v_samp_factor);
+    }
   }
-
-  cinfo->methods->upsample_init = upsample_init;
-  cinfo->methods->upsample_term = upsample_term;
 }
diff --git a/jerror.c b/jerror.c
index 0256f56..690a3d3 100644
--- a/jerror.c
+++ b/jerror.c
@@ -1,81 +1,211 @@
 /*
  * jerror.c
  *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains simple error-reporting and trace-message routines.
  * These are suitable for Unix-like systems and others where writing to
- * stderr is the right thing to do.  If the JPEG software is integrated
- * into a larger application, you may well need to replace these.
- *
- * The error_exit() routine should not return to its caller.  Within a
- * larger application, you might want to have it do a longjmp() to return
- * control to the outer user interface routine.  This should work since
- * the portable JPEG code doesn't use setjmp/longjmp.  You should make sure
- * that free_all is called either within error_exit or after the return to
- * the outer-level routine.
+ * stderr is the right thing to do.  Many applications will want to replace
+ * some or all of these routines.
  *
  * These routines are used by both the compression and decompression code.
  */
 
+/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
 #include "jinclude.h"
-#ifdef INCLUDES_ARE_ANSI
-#include <stdlib.h>		/* to declare exit() */
-#endif
+#include "jpeglib.h"
+#include "jversion.h"
+
+#include "jerror.h"		/* get error codes */
+#define JMAKE_MSG_TABLE
+#include "jerror.h"		/* create message string table */
 
 #ifndef EXIT_FAILURE		/* define exit() codes if not provided */
 #define EXIT_FAILURE  1
 #endif
 
 
-static external_methods_ptr methods; /* saved for access to message_parm, free_all */
-
+/*
+ * Error exit handler: must not return to caller.
+ *
+ * Applications may override this if they want to get control back after
+ * an error.  Typically one would longjmp somewhere instead of exiting.
+ * The setjmp buffer can be made a private field within an expanded error
+ * handler object.  Note that the info needed to generate an error message
+ * is stored in the error object, so you can generate the message now or
+ * later, at your convenience.
+ * You should make sure that the JPEG object is cleaned up (with jpeg_abort
+ * or jpeg_destroy) at some point.
+ */
 
 METHODDEF void
-trace_message (const char *msgtext)
+error_exit (j_common_ptr cinfo)
 {
-  fprintf(stderr, msgtext,
-	  methods->message_parm[0], methods->message_parm[1],
-	  methods->message_parm[2], methods->message_parm[3],
-	  methods->message_parm[4], methods->message_parm[5],
-	  methods->message_parm[6], methods->message_parm[7]);
-  fprintf(stderr, "\n");
-}
+  /* Always display the message */
+  (*cinfo->err->output_message) (cinfo);
 
+  /* Let the memory manager delete any temp files before we die */
+  jpeg_destroy(cinfo);
 
-METHODDEF void
-error_exit (const char *msgtext)
-{
-  (*methods->trace_message) (msgtext);
-  (*methods->free_all) ();	/* clean up memory allocation */
   exit(EXIT_FAILURE);
 }
 
 
 /*
- * The method selection routine for simple error handling.
- * The system-dependent setup routine should call this routine
- * to install the necessary method pointers in the supplied struct.
+ * Actual output of an error or trace message.
+ * Applications may override this method to send JPEG messages somewhere
+ * other than stderr.
  */
 
-GLOBAL void
-jselerror (external_methods_ptr emethods)
+METHODDEF void
+output_message (j_common_ptr cinfo)
 {
-  methods = emethods;		/* save struct addr for later access */
+  char buffer[JMSG_LENGTH_MAX];
 
-  emethods->error_exit = error_exit;
-  emethods->trace_message = trace_message;
+  /* Create the message */
+  (*cinfo->err->format_message) (cinfo, buffer);
 
-  emethods->trace_level = 0;	/* default = no tracing */
+  /* Send it to stderr, adding a newline */
+  fprintf(stderr, "%s\n", buffer);
+}
 
-  emethods->num_warnings = 0;	/* no warnings emitted yet */
-  /* By default, the first corrupt-data warning will be displayed,
-   * but additional ones will appear only if trace level is at least 3.
-   * A corrupt data file could generate many warnings, so it's a good idea
-   * to suppress additional messages except at high tracing levels.
-   */
-  emethods->first_warning_level = 0;
-  emethods->more_warning_level = 3;
+
+/*
+ * Decide whether to emit a trace or warning message.
+ * msg_level is one of:
+ *   -1: recoverable corrupt-data warning, may want to abort.
+ *    0: important advisory messages (always display to user).
+ *    1: first level of tracing detail.
+ *    2,3,...: successively more detailed tracing messages.
+ * An application might override this method if it wanted to abort on warnings
+ * or change the policy about which messages to display.
+ */
+
+METHODDEF void
+emit_message (j_common_ptr cinfo, int msg_level)
+{
+  struct jpeg_error_mgr * err = cinfo->err;
+
+  if (msg_level < 0) {
+    /* It's a warning message.  Since corrupt files may generate many warnings,
+     * the policy implemented here is to show only the first warning,
+     * unless trace_level >= 3.
+     */
+    if (err->num_warnings == 0 || err->trace_level >= 3)
+      (*err->output_message) (cinfo);
+    /* Always count warnings in num_warnings. */
+    err->num_warnings++;
+  } else {
+    /* It's a trace message.  Show it if trace_level >= msg_level. */
+    if (err->trace_level >= msg_level)
+      (*err->output_message) (cinfo);
+  }
+}
+
+
+/*
+ * Format a message string for the most recent JPEG error or message.
+ * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
+ * characters.  Note that no '\n' character is added to the string.
+ * Few applications should need to override this method.
+ */
+
+METHODDEF void
+format_message (j_common_ptr cinfo, char * buffer)
+{
+  struct jpeg_error_mgr * err = cinfo->err;
+  int msg_code = err->msg_code;
+  const char * msgtext = NULL;
+  const char * msgptr;
+  char ch;
+  boolean isstring;
+
+  /* Look up message string in proper table */
+  if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
+    msgtext = err->jpeg_message_table[msg_code];
+  } else if (err->addon_message_table != NULL &&
+	     msg_code >= err->first_addon_message &&
+	     msg_code <= err->last_addon_message) {
+    msgtext = err->addon_message_table[msg_code - err->first_addon_message];
+  }
+
+  /* Defend against bogus message number */
+  if (msgtext == NULL) {
+    err->msg_parm.i[0] = msg_code;
+    msgtext = err->jpeg_message_table[0];
+  }
+
+  /* Check for string parameter, as indicated by %s in the message text */
+  isstring = FALSE;
+  msgptr = msgtext;
+  while ((ch = *msgptr++) != '\0') {
+    if (ch == '%') {
+      if (*msgptr == 's') isstring = TRUE;
+      break;
+    }
+  }
+
+  /* Format the message into the passed buffer */
+  if (isstring)
+    sprintf(buffer, msgtext, err->msg_parm.s);
+  else
+    sprintf(buffer, msgtext,
+	    err->msg_parm.i[0], err->msg_parm.i[1],
+	    err->msg_parm.i[2], err->msg_parm.i[3],
+	    err->msg_parm.i[4], err->msg_parm.i[5],
+	    err->msg_parm.i[6], err->msg_parm.i[7]);
+}
+
+
+/*
+ * Reset error state variables at start of a new image.
+ * This is called during compression startup to reset trace/error
+ * processing to default state, without losing any application-specific
+ * method pointers.  An application might possibly want to override
+ * this method if it has additional error processing state.
+ */
+
+METHODDEF void
+reset_error_mgr (j_common_ptr cinfo)
+{
+  cinfo->err->num_warnings = 0;
+  /* trace_level is not reset since it is an application-supplied parameter */
+  cinfo->err->msg_code = 0;	/* may be useful as a flag for "no error" */
+}
+
+
+/*
+ * Fill in the standard error-handling methods in a jpeg_error_mgr object.
+ * Typical call is:
+ *	struct jpeg_compress_struct cinfo;
+ *	struct jpeg_error_mgr err;
+ *
+ *	cinfo.err = jpeg_std_error(&err);
+ * after which the application may override some of the methods.
+ */
+
+GLOBAL struct jpeg_error_mgr *
+jpeg_std_error (struct jpeg_error_mgr * err)
+{
+  err->error_exit = error_exit;
+  err->emit_message = emit_message;
+  err->output_message = output_message;
+  err->format_message = format_message;
+  err->reset_error_mgr = reset_error_mgr;
+
+  err->trace_level = 0;		/* default = no tracing */
+  err->num_warnings = 0;	/* no warnings emitted yet */
+  err->msg_code = 0;		/* may be useful as a flag for "no error" */
+
+  /* Initialize message table pointers */
+  err->jpeg_message_table = jpeg_message_table;
+  err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
+
+  err->addon_message_table = NULL;
+  err->first_addon_message = 0;	/* for safety */
+  err->last_addon_message = 0;
+
+  return err;
 }
diff --git a/jerror.h b/jerror.h
new file mode 100644
index 0000000..7918c38
--- /dev/null
+++ b/jerror.h
@@ -0,0 +1,264 @@
+/*
+ * jerror.h
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file defines the error and message codes for the JPEG library.
+ * Edit this file to add new codes, or to translate the message strings to
+ * some other language.
+ * A set of error-reporting macros are defined too.  Some applications using
+ * the JPEG library may wish to include this file to get the error codes
+ * and/or the macros.
+ */
+
+
+/* To define the enum list of message codes, include this file without
+ * defining JMAKE_MSG_TABLE.  To create the message string table, include it
+ * again with JMAKE_MSG_TABLE defined (this should be done in just one module).
+ */
+
+#ifdef JMAKE_MSG_TABLE
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_message_table	jMsgTable
+#endif
+
+const char * const jpeg_message_table[] = {
+
+#define JMESSAGE(code,string)	string ,
+
+#else /* not JMAKE_MSG_TABLE */
+
+typedef enum {
+
+#define JMESSAGE(code,string)	code ,
+
+#endif /* JMAKE_MSG_TABLE */
+
+JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */
+
+/* For maintenance convenience, list is alphabetical by message code name */
+JMESSAGE(JERR_ARITH_NOTIMPL,
+	 "Sorry, there are legal restrictions on arithmetic coding")
+JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
+JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
+JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
+JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
+JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
+JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace")
+JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace")
+JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length")
+JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan")
+JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d")
+JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d")
+JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
+JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
+JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access")
+JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small")
+JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here")
+JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet")
+JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d")
+JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request")
+JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d")
+JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x")
+JMESSAGE(JERR_DHT_COUNTS, "Bogus DHT counts")
+JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d")
+JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d")
+JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)")
+JMESSAGE(JERR_EMS_READ, "Read from EMS failed")
+JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed")
+JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
+JMESSAGE(JERR_FILE_READ, "Input file read error")
+JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
+JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
+JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow")
+JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
+JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
+JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")
+JMESSAGE(JERR_INPUT_EOF, "Premature end of input file")
+JMESSAGE(JERR_JFIF_MAJOR, "Unsupported JFIF revision number %d.%02d")
+JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
+JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
+JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
+JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
+JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
+JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined")
+JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x")
+JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)")
+JMESSAGE(JERR_QUANT_COMPONENTS,
+	 "Cannot quantize more than %d color components")
+JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors")
+JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors")
+JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers")
+JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker")
+JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x")
+JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers")
+JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF")
+JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s")
+JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file")
+JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file")
+JMESSAGE(JERR_TFILE_WRITE,
+	 "Write failed on temporary file --- out of disk space?")
+JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines")
+JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x")
+JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up")
+JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation")
+JMESSAGE(JERR_XMS_READ, "Read from XMS failed")
+JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed")
+JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT)
+JMESSAGE(JMSG_VERSION, JVERSION)
+JMESSAGE(JTRC_16BIT_TABLES,
+	 "Caution: quantization tables are too coarse for baseline JPEG")
+JMESSAGE(JTRC_ADOBE,
+	 "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
+JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u")
+JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u")
+JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x")
+JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x")
+JMESSAGE(JTRC_DQT, "Define Quantization Table %d  precision %d")
+JMESSAGE(JTRC_DRI, "Define Restart Interval %u")
+JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u")
+JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u")
+JMESSAGE(JTRC_EOI, "End Of Image")
+JMESSAGE(JTRC_HUFFBITS, "        %3d %3d %3d %3d %3d %3d %3d %3d")
+JMESSAGE(JTRC_JFIF, "JFIF APP0 marker, density %dx%d  %d")
+JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
+	 "Warning: thumbnail image size does not match data length %u")
+JMESSAGE(JTRC_JFIF_MINOR, "Warning: unknown JFIF revision number %d.%02d")
+JMESSAGE(JTRC_JFIF_THUMBNAIL, "    with %d x %d thumbnail image")
+JMESSAGE(JTRC_MISC_MARKER, "Skipping marker 0x%02x, length %u")
+JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
+JMESSAGE(JTRC_QUANTVALS, "        %4u %4u %4u %4u %4u %4u %4u %4u")
+JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors")
+JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors")
+JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization")
+JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d")
+JMESSAGE(JTRC_RST, "RST%d")
+JMESSAGE(JTRC_SMOOTH_NOTIMPL,
+	 "Smoothing not supported with nonstandard sampling ratios")
+JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d")
+JMESSAGE(JTRC_SOF_COMPONENT, "    Component %d: %dhx%dv q=%d")
+JMESSAGE(JTRC_SOI, "Start of Image")
+JMESSAGE(JTRC_SOS, "Start Of Scan: %d components")
+JMESSAGE(JTRC_SOS_COMPONENT, "    Component %d: dc=%d ac=%d")
+JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
+JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
+JMESSAGE(JTRC_UNKNOWN_IDS,
+	 "Unrecognized component IDs %d %d %d, assuming YCbCr")
+JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
+JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
+JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
+JMESSAGE(JWRN_EXTRANEOUS_DATA,
+	 "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
+JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment")
+JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code")
+JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
+JMESSAGE(JWRN_MUST_RESYNC,
+	 "Corrupt JPEG data: found marker 0x%02x instead of RST%d")
+JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
+JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")
+
+#ifdef JMAKE_MSG_TABLE
+
+  NULL
+};
+
+#else /* not JMAKE_MSG_TABLE */
+
+  JMSG_LASTMSGCODE
+} J_MESSAGE_CODE;
+
+#endif /* JMAKE_MSG_TABLE */
+
+#undef JMESSAGE
+
+
+#ifndef JMAKE_MSG_TABLE
+
+/* Macros to simplify using the error and trace message stuff */
+/* The first parameter is either type of cinfo pointer */
+
+/* Fatal errors (print message and exit) */
+#define ERREXIT(cinfo,code)  \
+  ((cinfo)->err->msg_code = (code), \
+   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT1(cinfo,code,p1)  \
+  ((cinfo)->err->msg_code = (code), \
+   (cinfo)->err->msg_parm.i[0] = (p1), \
+   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT2(cinfo,code,p1,p2)  \
+  ((cinfo)->err->msg_code = (code), \
+   (cinfo)->err->msg_parm.i[0] = (p1), \
+   (cinfo)->err->msg_parm.i[1] = (p2), \
+   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT3(cinfo,code,p1,p2,p3)  \
+  ((cinfo)->err->msg_code = (code), \
+   (cinfo)->err->msg_parm.i[0] = (p1), \
+   (cinfo)->err->msg_parm.i[1] = (p2), \
+   (cinfo)->err->msg_parm.i[2] = (p3), \
+   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT4(cinfo,code,p1,p2,p3,p4)  \
+  ((cinfo)->err->msg_code = (code), \
+   (cinfo)->err->msg_parm.i[0] = (p1), \
+   (cinfo)->err->msg_parm.i[1] = (p2), \
+   (cinfo)->err->msg_parm.i[2] = (p3), \
+   (cinfo)->err->msg_parm.i[3] = (p4), \
+   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXITS(cinfo,code,str)  \
+  ((cinfo)->err->msg_code = (code), \
+   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
+   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+
+#define MAKESTMT(stuff)		do { stuff } while (0)
+
+/* Nonfatal errors (we can keep going, but the data is probably corrupt) */
+#define WARNMS(cinfo,code)  \
+  ((cinfo)->err->msg_code = (code), \
+   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
+#define WARNMS1(cinfo,code,p1)  \
+  ((cinfo)->err->msg_code = (code), \
+   (cinfo)->err->msg_parm.i[0] = (p1), \
+   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
+#define WARNMS2(cinfo,code,p1,p2)  \
+  ((cinfo)->err->msg_code = (code), \
+   (cinfo)->err->msg_parm.i[0] = (p1), \
+   (cinfo)->err->msg_parm.i[1] = (p2), \
+   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
+
+/* Informational/debugging messages */
+#define TRACEMS(cinfo,lvl,code)  \
+  ((cinfo)->err->msg_code = (code), \
+   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+#define TRACEMS1(cinfo,lvl,code,p1)  \
+  ((cinfo)->err->msg_code = (code), \
+   (cinfo)->err->msg_parm.i[0] = (p1), \
+   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+#define TRACEMS2(cinfo,lvl,code,p1,p2)  \
+  ((cinfo)->err->msg_code = (code), \
+   (cinfo)->err->msg_parm.i[0] = (p1), \
+   (cinfo)->err->msg_parm.i[1] = (p2), \
+   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+#define TRACEMS3(cinfo,lvl,code,p1,p2,p3)  \
+  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \
+	   (cinfo)->err->msg_code = (code); \
+	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4)  \
+  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
+	   (cinfo)->err->msg_code = (code); \
+	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8)  \
+  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
+	   _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \
+	   (cinfo)->err->msg_code = (code); \
+	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMSS(cinfo,lvl,code,str)  \
+  ((cinfo)->err->msg_code = (code), \
+   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
+   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+
+#endif /* JMAKE_MSG_TABLE */
diff --git a/jfdctflt.c b/jfdctflt.c
new file mode 100644
index 0000000..21371eb
--- /dev/null
+++ b/jfdctflt.c
@@ -0,0 +1,168 @@
+/*
+ * jfdctflt.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a floating-point implementation of the
+ * forward DCT (Discrete Cosine Transform).
+ *
+ * This implementation should be more accurate than either of the integer
+ * DCT implementations.  However, it may not give the same results on all
+ * machines because of differences in roundoff behavior.  Speed will depend
+ * on the hardware's floating point capacity.
+ *
+ * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
+ * on each column.  Direct algorithms are also available, but they are
+ * much more complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README).  The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs.  These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with a fixed-point
+ * implementation, accuracy is lost due to imprecise representation of the
+ * scaled quantization values.  However, that problem does not arise if
+ * we use floating point arithmetic.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h"		/* Private declarations for DCT subsystem */
+
+#ifdef DCT_FLOAT_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/*
+ * Perform the forward DCT on one block of samples.
+ */
+
+GLOBAL void
+jpeg_fdct_float (FAST_FLOAT * data)
+{
+  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
+  FAST_FLOAT z1, z2, z3, z4, z5, z11, z13;
+  FAST_FLOAT *dataptr;
+  int ctr;
+
+  /* Pass 1: process rows. */
+
+  dataptr = data;
+  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+    tmp0 = dataptr[0] + dataptr[7];
+    tmp7 = dataptr[0] - dataptr[7];
+    tmp1 = dataptr[1] + dataptr[6];
+    tmp6 = dataptr[1] - dataptr[6];
+    tmp2 = dataptr[2] + dataptr[5];
+    tmp5 = dataptr[2] - dataptr[5];
+    tmp3 = dataptr[3] + dataptr[4];
+    tmp4 = dataptr[3] - dataptr[4];
+    
+    /* Even part */
+    
+    tmp10 = tmp0 + tmp3;	/* phase 2 */
+    tmp13 = tmp0 - tmp3;
+    tmp11 = tmp1 + tmp2;
+    tmp12 = tmp1 - tmp2;
+    
+    dataptr[0] = tmp10 + tmp11; /* phase 3 */
+    dataptr[4] = tmp10 - tmp11;
+    
+    z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
+    dataptr[2] = tmp13 + z1;	/* phase 5 */
+    dataptr[6] = tmp13 - z1;
+    
+    /* Odd part */
+
+    tmp10 = tmp4 + tmp5;	/* phase 2 */
+    tmp11 = tmp5 + tmp6;
+    tmp12 = tmp6 + tmp7;
+
+    /* The rotator is modified from fig 4-8 to avoid extra negations. */
+    z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
+    z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
+    z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
+    z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */
+
+    z11 = tmp7 + z3;		/* phase 5 */
+    z13 = tmp7 - z3;
+
+    dataptr[5] = z13 + z2;	/* phase 6 */
+    dataptr[3] = z13 - z2;
+    dataptr[1] = z11 + z4;
+    dataptr[7] = z11 - z4;
+
+    dataptr += DCTSIZE;		/* advance pointer to next row */
+  }
+
+  /* Pass 2: process columns. */
+
+  dataptr = data;
+  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+    tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
+    tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
+    tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
+    tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
+    tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
+    tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
+    tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
+    tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
+    
+    /* Even part */
+    
+    tmp10 = tmp0 + tmp3;	/* phase 2 */
+    tmp13 = tmp0 - tmp3;
+    tmp11 = tmp1 + tmp2;
+    tmp12 = tmp1 - tmp2;
+    
+    dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
+    dataptr[DCTSIZE*4] = tmp10 - tmp11;
+    
+    z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
+    dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
+    dataptr[DCTSIZE*6] = tmp13 - z1;
+    
+    /* Odd part */
+
+    tmp10 = tmp4 + tmp5;	/* phase 2 */
+    tmp11 = tmp5 + tmp6;
+    tmp12 = tmp6 + tmp7;
+
+    /* The rotator is modified from fig 4-8 to avoid extra negations. */
+    z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
+    z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
+    z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
+    z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */
+
+    z11 = tmp7 + z3;		/* phase 5 */
+    z13 = tmp7 - z3;
+
+    dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
+    dataptr[DCTSIZE*3] = z13 - z2;
+    dataptr[DCTSIZE*1] = z11 + z4;
+    dataptr[DCTSIZE*7] = z11 - z4;
+
+    dataptr++;			/* advance pointer to next column */
+  }
+}
+
+#endif /* DCT_FLOAT_SUPPORTED */
diff --git a/jfdctfst.c b/jfdctfst.c
new file mode 100644
index 0000000..a52d7b7
--- /dev/null
+++ b/jfdctfst.c
@@ -0,0 +1,224 @@
+/*
+ * jfdctfst.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a fast, not so accurate integer implementation of the
+ * forward DCT (Discrete Cosine Transform).
+ *
+ * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
+ * on each column.  Direct algorithms are also available, but they are
+ * much more complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README).  The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs.  These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with fixed-point math,
+ * accuracy is lost due to imprecise representation of the scaled
+ * quantization values.  The smaller the quantization table entry, the less
+ * precise the scaled value, so this implementation does worse with high-
+ * quality-setting files than with low-quality ones.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h"		/* Private declarations for DCT subsystem */
+
+#ifdef DCT_IFAST_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/* Scaling decisions are generally the same as in the LL&M algorithm;
+ * see jfdctint.c for more details.  However, we choose to descale
+ * (right shift) multiplication products as soon as they are formed,
+ * rather than carrying additional fractional bits into subsequent additions.
+ * This compromises accuracy slightly, but it lets us save a few shifts.
+ * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
+ * everywhere except in the multiplications proper; this saves a good deal
+ * of work on 16-bit-int machines.
+ *
+ * Again to save a few shifts, the intermediate results between pass 1 and
+ * pass 2 are not upscaled, but are represented only to integral precision.
+ *
+ * A final compromise is to represent the multiplicative constants to only
+ * 8 fractional bits, rather than 13.  This saves some shifting work on some
+ * machines, and may also reduce the cost of multiplication (since there
+ * are fewer one-bits in the constants).
+ */
+
+#define CONST_BITS  8
+
+
+/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
+ * causing a lot of useless floating-point operations at run time.
+ * To get around this we use the following pre-calculated constants.
+ * If you change CONST_BITS you may want to add appropriate values.
+ * (With a reasonable C compiler, you can just rely on the FIX() macro...)
+ */
+
+#if CONST_BITS == 8
+#define FIX_0_382683433  ((INT32)   98)		/* FIX(0.382683433) */
+#define FIX_0_541196100  ((INT32)  139)		/* FIX(0.541196100) */
+#define FIX_0_707106781  ((INT32)  181)		/* FIX(0.707106781) */
+#define FIX_1_306562965  ((INT32)  334)		/* FIX(1.306562965) */
+#else
+#define FIX_0_382683433  FIX(0.382683433)
+#define FIX_0_541196100  FIX(0.541196100)
+#define FIX_0_707106781  FIX(0.707106781)
+#define FIX_1_306562965  FIX(1.306562965)
+#endif
+
+
+/* We can gain a little more speed, with a further compromise in accuracy,
+ * by omitting the addition in a descaling shift.  This yields an incorrectly
+ * rounded result half the time...
+ */
+
+#ifndef USE_ACCURATE_ROUNDING
+#undef DESCALE
+#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
+#endif
+
+
+/* Multiply a DCTELEM variable by an INT32 constant, and immediately
+ * descale to yield a DCTELEM result.
+ */
+
+#define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
+
+
+/*
+ * Perform the forward DCT on one block of samples.
+ */
+
+GLOBAL void
+jpeg_fdct_ifast (DCTELEM * data)
+{
+  DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+  DCTELEM tmp10, tmp11, tmp12, tmp13;
+  DCTELEM z1, z2, z3, z4, z5, z11, z13;
+  DCTELEM *dataptr;
+  int ctr;
+  SHIFT_TEMPS
+
+  /* Pass 1: process rows. */
+
+  dataptr = data;
+  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+    tmp0 = dataptr[0] + dataptr[7];
+    tmp7 = dataptr[0] - dataptr[7];
+    tmp1 = dataptr[1] + dataptr[6];
+    tmp6 = dataptr[1] - dataptr[6];
+    tmp2 = dataptr[2] + dataptr[5];
+    tmp5 = dataptr[2] - dataptr[5];
+    tmp3 = dataptr[3] + dataptr[4];
+    tmp4 = dataptr[3] - dataptr[4];
+    
+    /* Even part */
+    
+    tmp10 = tmp0 + tmp3;	/* phase 2 */
+    tmp13 = tmp0 - tmp3;
+    tmp11 = tmp1 + tmp2;
+    tmp12 = tmp1 - tmp2;
+    
+    dataptr[0] = tmp10 + tmp11; /* phase 3 */
+    dataptr[4] = tmp10 - tmp11;
+    
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
+    dataptr[2] = tmp13 + z1;	/* phase 5 */
+    dataptr[6] = tmp13 - z1;
+    
+    /* Odd part */
+
+    tmp10 = tmp4 + tmp5;	/* phase 2 */
+    tmp11 = tmp5 + tmp6;
+    tmp12 = tmp6 + tmp7;
+
+    /* The rotator is modified from fig 4-8 to avoid extra negations. */
+    z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
+    z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
+    z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
+    z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
+
+    z11 = tmp7 + z3;		/* phase 5 */
+    z13 = tmp7 - z3;
+
+    dataptr[5] = z13 + z2;	/* phase 6 */
+    dataptr[3] = z13 - z2;
+    dataptr[1] = z11 + z4;
+    dataptr[7] = z11 - z4;
+
+    dataptr += DCTSIZE;		/* advance pointer to next row */
+  }
+
+  /* Pass 2: process columns. */
+
+  dataptr = data;
+  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+    tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
+    tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
+    tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
+    tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
+    tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
+    tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
+    tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
+    tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
+    
+    /* Even part */
+    
+    tmp10 = tmp0 + tmp3;	/* phase 2 */
+    tmp13 = tmp0 - tmp3;
+    tmp11 = tmp1 + tmp2;
+    tmp12 = tmp1 - tmp2;
+    
+    dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
+    dataptr[DCTSIZE*4] = tmp10 - tmp11;
+    
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
+    dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
+    dataptr[DCTSIZE*6] = tmp13 - z1;
+    
+    /* Odd part */
+
+    tmp10 = tmp4 + tmp5;	/* phase 2 */
+    tmp11 = tmp5 + tmp6;
+    tmp12 = tmp6 + tmp7;
+
+    /* The rotator is modified from fig 4-8 to avoid extra negations. */
+    z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
+    z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
+    z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
+    z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
+
+    z11 = tmp7 + z3;		/* phase 5 */
+    z13 = tmp7 - z3;
+
+    dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
+    dataptr[DCTSIZE*3] = z13 - z2;
+    dataptr[DCTSIZE*1] = z11 + z4;
+    dataptr[DCTSIZE*7] = z11 - z4;
+
+    dataptr++;			/* advance pointer to next column */
+  }
+}
+
+#endif /* DCT_IFAST_SUPPORTED */
diff --git a/jfwddct.c b/jfdctint.c
similarity index 81%
rename from jfwddct.c
rename to jfdctint.c
index 3254bce..7df0433 100644
--- a/jfwddct.c
+++ b/jfdctint.c
@@ -1,12 +1,16 @@
 /*
- * jfwddct.c
+ * jfdctint.c
  *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
- * This file contains the basic DCT (Discrete Cosine Transform)
- * transformation subroutine.
+ * This file contains a slow-but-accurate integer implementation of the
+ * forward DCT (Discrete Cosine Transform).
+ *
+ * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
+ * on each column.  Direct algorithms are also available, but they are
+ * much more complex and seem not to be any faster when reduced to code.
  *
  * This implementation is based on an algorithm described in
  *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
@@ -19,10 +23,16 @@
  * scaled fixed-point arithmetic, with a minimal number of shifts.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h"		/* Private declarations for DCT subsystem */
+
+#ifdef DCT_ISLOW_SUPPORTED
+
 
 /*
- * This routine is specialized to the case DCTSIZE = 8.
+ * This module is specialized to the case DCTSIZE = 8.
  */
 
 #if DCTSIZE != 8
@@ -31,10 +41,6 @@
 
 
 /*
- * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
- * on each column.  Direct algorithms are also available, but they are
- * much more complex and seem not to be any faster when reduced to code.
- *
  * The poop on this scaling stuff is as follows:
  *
  * Each 1-D DCT step produces outputs which are a factor of sqrt(N)
@@ -43,6 +49,8 @@
  * a simple right shift at the end of the algorithm.  The advantage of
  * this arrangement is that we save two multiplications per 1-D DCT,
  * because the y0 and y4 outputs need not be divided by sqrt(N).
+ * In the IJG code, this factor of 8 is removed by the quantization step
+ * (in jcdctmgr.c), NOT in this module.
  *
  * We have to do addition and subtraction of the integer inputs, which
  * is no problem, and multiplication by fractional constants, which is
@@ -58,30 +66,22 @@
  * The outputs of the first pass are scaled up by PASS1_BITS bits so that
  * they are represented to better-than-integral precision.  These outputs
  * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
- * with the recommended scaling.  (To scale up 12-bit sample data, an
- * intermediate INT32 array would be needed.)
+ * with the recommended scaling.  (For 12-bit sample data, the intermediate
+ * array is INT32 anyway.)
  *
  * To avoid overflow of the 32-bit intermediate results in pass 2, we must
- * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 25.  Error analysis
+ * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
  * shows that the values given below are the most effective.
  */
 
-#ifdef EIGHT_BIT_SAMPLES
+#if BITS_IN_JSAMPLE == 8
 #define CONST_BITS  13
 #define PASS1_BITS  2
 #else
 #define CONST_BITS  13
-#define PASS1_BITS  0		/* lose a little precision to avoid overflow */
+#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
 #endif
 
-#define ONE	((INT32) 1)
-
-#define CONST_SCALE (ONE << CONST_BITS)
-
-/* Convert a positive real constant to an integer scaled by CONST_SCALE. */
-
-#define FIX(x)	((INT32) ((x) * CONST_SCALE + 0.5))
-
 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  * causing a lot of useless floating-point operations at run time.
  * To get around this we use the following pre-calculated constants.
@@ -118,34 +118,16 @@
 #endif
 
 
-/* Descale and correctly round an INT32 value that's scaled by N bits.
- * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
- * the fudge factor is correct for either sign of X.
- */
-
-#define DESCALE(x,n)  RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
-
 /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  * For 8-bit samples with the recommended scaling, all the variable
  * and constant values involved are no more than 16 bits wide, so a
- * 16x16->32 bit multiply can be used instead of a full 32x32 multiply;
- * this provides a useful speedup on many machines.
- * There is no way to specify a 16x16->32 multiply in portable C, but
- * some C compilers will do the right thing if you provide the correct
- * combination of casts.
- * NB: for 12-bit samples, a full 32-bit multiplication will be needed.
+ * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
+ * For 12-bit samples, a full 32-bit multiplication will be needed.
  */
 
-#ifdef EIGHT_BIT_SAMPLES
-#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
-#define MULTIPLY(var,const)  (((INT16) (var)) * ((INT16) (const)))
-#endif
-#ifdef SHORTxLCONST_32		/* known to work with Microsoft C 6.0 */
-#define MULTIPLY(var,const)  (((INT16) (var)) * ((INT32) (const)))
-#endif
-#endif
-
-#ifndef MULTIPLY		/* default definition */
+#if BITS_IN_JSAMPLE == 8
+#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
+#else
 #define MULTIPLY(var,const)  ((var) * (const))
 #endif
 
@@ -155,13 +137,13 @@
  */
 
 GLOBAL void
-j_fwd_dct (DCTBLOCK data)
+jpeg_fdct_islow (DCTELEM * data)
 {
   INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
   INT32 tmp10, tmp11, tmp12, tmp13;
   INT32 z1, z2, z3, z4, z5;
-  register DCTELEM *dataptr;
-  int rowctr;
+  DCTELEM *dataptr;
+  int ctr;
   SHIFT_TEMPS
 
   /* Pass 1: process rows. */
@@ -169,7 +151,7 @@
   /* furthermore, we scale the results by 2**PASS1_BITS. */
 
   dataptr = data;
-  for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
+  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
     tmp0 = dataptr[0] + dataptr[7];
     tmp7 = dataptr[0] - dataptr[7];
     tmp1 = dataptr[1] + dataptr[6];
@@ -228,12 +210,13 @@
     dataptr += DCTSIZE;		/* advance pointer to next row */
   }
 
-  /* Pass 2: process columns. */
-  /* Note that we must descale the results by a factor of 8 == 2**3, */
-  /* and also undo the PASS1_BITS scaling. */
+  /* Pass 2: process columns.
+   * We remove the PASS1_BITS scaling, but leave the results scaled up
+   * by an overall factor of 8.
+   */
 
   dataptr = data;
-  for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
+  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
     tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
     tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
     tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
@@ -252,14 +235,14 @@
     tmp11 = tmp1 + tmp2;
     tmp12 = tmp1 - tmp2;
     
-    dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS+3);
-    dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS+3);
+    dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS);
+    dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS);
     
     z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
     dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
-					   CONST_BITS+PASS1_BITS+3);
+					   CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
-					   CONST_BITS+PASS1_BITS+3);
+					   CONST_BITS+PASS1_BITS);
     
     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
      * cK represents cos(K*pi/16).
@@ -285,14 +268,16 @@
     z4 += z5;
     
     dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3,
-					   CONST_BITS+PASS1_BITS+3);
+					   CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4,
-					   CONST_BITS+PASS1_BITS+3);
+					   CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3,
-					   CONST_BITS+PASS1_BITS+3);
+					   CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4,
-					   CONST_BITS+PASS1_BITS+3);
+					   CONST_BITS+PASS1_BITS);
     
     dataptr++;			/* advance pointer to next column */
   }
 }
+
+#endif /* DCT_ISLOW_SUPPORTED */
diff --git a/jidctflt.c b/jidctflt.c
new file mode 100644
index 0000000..847919e
--- /dev/null
+++ b/jidctflt.c
@@ -0,0 +1,241 @@
+/*
+ * jidctflt.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a floating-point implementation of the
+ * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
+ * must also perform dequantization of the input coefficients.
+ *
+ * This implementation should be more accurate than either of the integer
+ * IDCT implementations.  However, it may not give the same results on all
+ * machines because of differences in roundoff behavior.  Speed will depend
+ * on the hardware's floating point capacity.
+ *
+ * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
+ * on each row (or vice versa, but it's more convenient to emit a row at
+ * a time).  Direct algorithms are also available, but they are much more
+ * complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README).  The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs.  These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with a fixed-point
+ * implementation, accuracy is lost due to imprecise representation of the
+ * scaled quantization values.  However, that problem does not arise if
+ * we use floating point arithmetic.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h"		/* Private declarations for DCT subsystem */
+
+#ifdef DCT_FLOAT_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/* Dequantize a coefficient by multiplying it by the multiplier-table
+ * entry; produce a float result.
+ */
+
+#define DEQUANTIZE(coef,quantval)  (((FAST_FLOAT) (coef)) * (quantval))
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients.
+ */
+
+GLOBAL void
+jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+		 JCOEFPTR coef_block,
+		 JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
+  FAST_FLOAT z5, z10, z11, z12, z13;
+  JCOEFPTR inptr;
+  FLOAT_MULT_TYPE * quantptr;
+  FAST_FLOAT * wsptr;
+  JSAMPROW outptr;
+  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+  int ctr;
+  FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
+  SHIFT_TEMPS
+
+  /* Pass 1: process columns from input, store into work array. */
+
+  inptr = coef_block;
+  quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table;
+  wsptr = workspace;
+  for (ctr = DCTSIZE; ctr > 0; ctr--) {
+    /* Due to quantization, we will usually find that many of the input
+     * coefficients are zero, especially the AC terms.  We can exploit this
+     * by short-circuiting the IDCT calculation for any column in which all
+     * the AC terms are zero.  In that case each output is equal to the
+     * DC coefficient (with scale factor as needed).
+     * With typical images and quantization tables, half or more of the
+     * column DCT calculations can be simplified this way.
+     */
+    
+    if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
+	 inptr[DCTSIZE*4] | inptr[DCTSIZE*5] | inptr[DCTSIZE*6] |
+	 inptr[DCTSIZE*7]) == 0) {
+      /* AC terms all zero */
+      FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+      
+      wsptr[DCTSIZE*0] = dcval;
+      wsptr[DCTSIZE*1] = dcval;
+      wsptr[DCTSIZE*2] = dcval;
+      wsptr[DCTSIZE*3] = dcval;
+      wsptr[DCTSIZE*4] = dcval;
+      wsptr[DCTSIZE*5] = dcval;
+      wsptr[DCTSIZE*6] = dcval;
+      wsptr[DCTSIZE*7] = dcval;
+      
+      inptr++;			/* advance pointers to next column */
+      quantptr++;
+      wsptr++;
+      continue;
+    }
+    
+    /* Even part */
+
+    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+    tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+    tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
+    tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+    tmp10 = tmp0 + tmp2;	/* phase 3 */
+    tmp11 = tmp0 - tmp2;
+
+    tmp13 = tmp1 + tmp3;	/* phases 5-3 */
+    tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
+
+    tmp0 = tmp10 + tmp13;	/* phase 2 */
+    tmp3 = tmp10 - tmp13;
+    tmp1 = tmp11 + tmp12;
+    tmp2 = tmp11 - tmp12;
+    
+    /* Odd part */
+
+    tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+    tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+    tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+    tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+
+    z13 = tmp6 + tmp5;		/* phase 6 */
+    z10 = tmp6 - tmp5;
+    z11 = tmp4 + tmp7;
+    z12 = tmp4 - tmp7;
+
+    tmp7 = z11 + z13;		/* phase 5 */
+    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
+
+    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
+    tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
+    tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
+
+    tmp6 = tmp12 - tmp7;	/* phase 2 */
+    tmp5 = tmp11 - tmp6;
+    tmp4 = tmp10 + tmp5;
+
+    wsptr[DCTSIZE*0] = tmp0 + tmp7;
+    wsptr[DCTSIZE*7] = tmp0 - tmp7;
+    wsptr[DCTSIZE*1] = tmp1 + tmp6;
+    wsptr[DCTSIZE*6] = tmp1 - tmp6;
+    wsptr[DCTSIZE*2] = tmp2 + tmp5;
+    wsptr[DCTSIZE*5] = tmp2 - tmp5;
+    wsptr[DCTSIZE*4] = tmp3 + tmp4;
+    wsptr[DCTSIZE*3] = tmp3 - tmp4;
+
+    inptr++;			/* advance pointers to next column */
+    quantptr++;
+    wsptr++;
+  }
+  
+  /* Pass 2: process rows from work array, store into output array. */
+  /* Note that we must descale the results by a factor of 8 == 2**3. */
+
+  wsptr = workspace;
+  for (ctr = 0; ctr < DCTSIZE; ctr++) {
+    outptr = output_buf[ctr] + output_col;
+    /* Rows of zeroes can be exploited in the same way as we did with columns.
+     * However, the column calculation has created many nonzero AC terms, so
+     * the simplification applies less often (typically 5% to 10% of the time).
+     * And testing floats for zero is relatively expensive, so we don't bother.
+     */
+    
+    /* Even part */
+
+    tmp10 = wsptr[0] + wsptr[4];
+    tmp11 = wsptr[0] - wsptr[4];
+
+    tmp13 = wsptr[2] + wsptr[6];
+    tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
+
+    tmp0 = tmp10 + tmp13;
+    tmp3 = tmp10 - tmp13;
+    tmp1 = tmp11 + tmp12;
+    tmp2 = tmp11 - tmp12;
+
+    /* Odd part */
+
+    z13 = wsptr[5] + wsptr[3];
+    z10 = wsptr[5] - wsptr[3];
+    z11 = wsptr[1] + wsptr[7];
+    z12 = wsptr[1] - wsptr[7];
+
+    tmp7 = z11 + z13;
+    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
+
+    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
+    tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
+    tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
+
+    tmp6 = tmp12 - tmp7;
+    tmp5 = tmp11 - tmp6;
+    tmp4 = tmp10 + tmp5;
+
+    /* Final output stage: scale down by a factor of 8 and range-limit */
+
+    outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3)
+			    & RANGE_MASK];
+    outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3)
+			    & RANGE_MASK];
+    outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3)
+			    & RANGE_MASK];
+    outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3)
+			    & RANGE_MASK];
+    outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3)
+			    & RANGE_MASK];
+    outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3)
+			    & RANGE_MASK];
+    outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3)
+			    & RANGE_MASK];
+    outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3)
+			    & RANGE_MASK];
+    
+    wsptr += DCTSIZE;		/* advance pointer to next row */
+  }
+}
+
+#endif /* DCT_FLOAT_SUPPORTED */
diff --git a/jidctfst.c b/jidctfst.c
new file mode 100644
index 0000000..f13d14d
--- /dev/null
+++ b/jidctfst.c
@@ -0,0 +1,362 @@
+/*
+ * jidctfst.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a fast, not so accurate integer implementation of the
+ * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
+ * must also perform dequantization of the input coefficients.
+ *
+ * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
+ * on each row (or vice versa, but it's more convenient to emit a row at
+ * a time).  Direct algorithms are also available, but they are much more
+ * complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README).  The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs.  These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with fixed-point math,
+ * accuracy is lost due to imprecise representation of the scaled
+ * quantization values.  The smaller the quantization table entry, the less
+ * precise the scaled value, so this implementation does worse with high-
+ * quality-setting files than with low-quality ones.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h"		/* Private declarations for DCT subsystem */
+
+#ifdef DCT_IFAST_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/* Scaling decisions are generally the same as in the LL&M algorithm;
+ * see jidctint.c for more details.  However, we choose to descale
+ * (right shift) multiplication products as soon as they are formed,
+ * rather than carrying additional fractional bits into subsequent additions.
+ * This compromises accuracy slightly, but it lets us save a few shifts.
+ * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
+ * everywhere except in the multiplications proper; this saves a good deal
+ * of work on 16-bit-int machines.
+ *
+ * The dequantized coefficients are not integers because the AA&N scaling
+ * factors have been incorporated.  We represent them scaled up by PASS1_BITS,
+ * so that the first and second IDCT rounds have the same input scaling.
+ * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
+ * avoid a descaling shift; this compromises accuracy rather drastically
+ * for small quantization table entries, but it saves a lot of shifts.
+ * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
+ * so we use a much larger scaling factor to preserve accuracy.
+ *
+ * A final compromise is to represent the multiplicative constants to only
+ * 8 fractional bits, rather than 13.  This saves some shifting work on some
+ * machines, and may also reduce the cost of multiplication (since there
+ * are fewer one-bits in the constants).
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define CONST_BITS  8
+#define PASS1_BITS  2
+#else
+#define CONST_BITS  8
+#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
+#endif
+
+/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
+ * causing a lot of useless floating-point operations at run time.
+ * To get around this we use the following pre-calculated constants.
+ * If you change CONST_BITS you may want to add appropriate values.
+ * (With a reasonable C compiler, you can just rely on the FIX() macro...)
+ */
+
+#if CONST_BITS == 8
+#define FIX_1_082392200  ((INT32)  277)		/* FIX(1.082392200) */
+#define FIX_1_414213562  ((INT32)  362)		/* FIX(1.414213562) */
+#define FIX_1_847759065  ((INT32)  473)		/* FIX(1.847759065) */
+#define FIX_2_613125930  ((INT32)  669)		/* FIX(2.613125930) */
+#else
+#define FIX_1_082392200  FIX(1.082392200)
+#define FIX_1_414213562  FIX(1.414213562)
+#define FIX_1_847759065  FIX(1.847759065)
+#define FIX_2_613125930  FIX(2.613125930)
+#endif
+
+
+/* We can gain a little more speed, with a further compromise in accuracy,
+ * by omitting the addition in a descaling shift.  This yields an incorrectly
+ * rounded result half the time...
+ */
+
+#ifndef USE_ACCURATE_ROUNDING
+#undef DESCALE
+#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
+#endif
+
+
+/* Multiply a DCTELEM variable by an INT32 constant, and immediately
+ * descale to yield a DCTELEM result.
+ */
+
+#define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
+
+
+/* Dequantize a coefficient by multiplying it by the multiplier-table
+ * entry; produce a DCTELEM result.  For 8-bit data a 16x16->16
+ * multiplication will do.  For 12-bit data, the multiplier table is
+ * declared INT32, so a 32-bit multiply will be used.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define DEQUANTIZE(coef,quantval)  (((IFAST_MULT_TYPE) (coef)) * (quantval))
+#else
+#define DEQUANTIZE(coef,quantval)  \
+	DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
+#endif
+
+
+/* Like DESCALE, but applies to a DCTELEM and produces an int.
+ * We assume that int right shift is unsigned if INT32 right shift is.
+ */
+
+#ifdef RIGHT_SHIFT_IS_UNSIGNED
+#define ISHIFT_TEMPS	DCTELEM ishift_temp;
+#define IRIGHT_SHIFT(x,shft)  \
+	((ishift_temp = (x)) < 0 ? \
+	 (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (32-(shft))) : \
+	 (ishift_temp >> (shft)))
+#else
+#define ISHIFT_TEMPS
+#define IRIGHT_SHIFT(x,shft)	((x) >> (shft))
+#endif
+
+#ifdef USE_ACCURATE_ROUNDING
+#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
+#else
+#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT(x, n))
+#endif
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients.
+ */
+
+GLOBAL void
+jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+		 JCOEFPTR coef_block,
+		 JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+  DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+  DCTELEM tmp10, tmp11, tmp12, tmp13;
+  DCTELEM z5, z10, z11, z12, z13;
+  JCOEFPTR inptr;
+  IFAST_MULT_TYPE * quantptr;
+  int * wsptr;
+  JSAMPROW outptr;
+  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+  int ctr;
+  int workspace[DCTSIZE2];	/* buffers data between passes */
+  SHIFT_TEMPS			/* for DESCALE */
+  ISHIFT_TEMPS			/* for IDESCALE */
+
+  /* Pass 1: process columns from input, store into work array. */
+
+  inptr = coef_block;
+  quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
+  wsptr = workspace;
+  for (ctr = DCTSIZE; ctr > 0; ctr--) {
+    /* Due to quantization, we will usually find that many of the input
+     * coefficients are zero, especially the AC terms.  We can exploit this
+     * by short-circuiting the IDCT calculation for any column in which all
+     * the AC terms are zero.  In that case each output is equal to the
+     * DC coefficient (with scale factor as needed).
+     * With typical images and quantization tables, half or more of the
+     * column DCT calculations can be simplified this way.
+     */
+    
+    if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
+	 inptr[DCTSIZE*4] | inptr[DCTSIZE*5] | inptr[DCTSIZE*6] |
+	 inptr[DCTSIZE*7]) == 0) {
+      /* AC terms all zero */
+      int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+
+      wsptr[DCTSIZE*0] = dcval;
+      wsptr[DCTSIZE*1] = dcval;
+      wsptr[DCTSIZE*2] = dcval;
+      wsptr[DCTSIZE*3] = dcval;
+      wsptr[DCTSIZE*4] = dcval;
+      wsptr[DCTSIZE*5] = dcval;
+      wsptr[DCTSIZE*6] = dcval;
+      wsptr[DCTSIZE*7] = dcval;
+      
+      inptr++;			/* advance pointers to next column */
+      quantptr++;
+      wsptr++;
+      continue;
+    }
+    
+    /* Even part */
+
+    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+    tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+    tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
+    tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+    tmp10 = tmp0 + tmp2;	/* phase 3 */
+    tmp11 = tmp0 - tmp2;
+
+    tmp13 = tmp1 + tmp3;	/* phases 5-3 */
+    tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
+
+    tmp0 = tmp10 + tmp13;	/* phase 2 */
+    tmp3 = tmp10 - tmp13;
+    tmp1 = tmp11 + tmp12;
+    tmp2 = tmp11 - tmp12;
+    
+    /* Odd part */
+
+    tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+    tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+    tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+    tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+
+    z13 = tmp6 + tmp5;		/* phase 6 */
+    z10 = tmp6 - tmp5;
+    z11 = tmp4 + tmp7;
+    z12 = tmp4 - tmp7;
+
+    tmp7 = z11 + z13;		/* phase 5 */
+    tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
+
+    z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
+    tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
+    tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
+
+    tmp6 = tmp12 - tmp7;	/* phase 2 */
+    tmp5 = tmp11 - tmp6;
+    tmp4 = tmp10 + tmp5;
+
+    wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
+    wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
+    wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
+    wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
+    wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
+    wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
+    wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);
+    wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);
+
+    inptr++;			/* advance pointers to next column */
+    quantptr++;
+    wsptr++;
+  }
+  
+  /* Pass 2: process rows from work array, store into output array. */
+  /* Note that we must descale the results by a factor of 8 == 2**3, */
+  /* and also undo the PASS1_BITS scaling. */
+
+  wsptr = workspace;
+  for (ctr = 0; ctr < DCTSIZE; ctr++) {
+    outptr = output_buf[ctr] + output_col;
+    /* Rows of zeroes can be exploited in the same way as we did with columns.
+     * However, the column calculation has created many nonzero AC terms, so
+     * the simplification applies less often (typically 5% to 10% of the time).
+     * On machines with very fast multiplication, it's possible that the
+     * test takes more time than it's worth.  In that case this section
+     * may be commented out.
+     */
+    
+#ifndef NO_ZERO_ROW_TEST
+    if ((wsptr[1] | wsptr[2] | wsptr[3] | wsptr[4] | wsptr[5] | wsptr[6] |
+	 wsptr[7]) == 0) {
+      /* AC terms all zero */
+      JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
+				  & RANGE_MASK];
+      
+      outptr[0] = dcval;
+      outptr[1] = dcval;
+      outptr[2] = dcval;
+      outptr[3] = dcval;
+      outptr[4] = dcval;
+      outptr[5] = dcval;
+      outptr[6] = dcval;
+      outptr[7] = dcval;
+
+      wsptr += DCTSIZE;		/* advance pointer to next row */
+      continue;
+    }
+#endif
+    
+    /* Even part */
+
+    tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
+    tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);
+
+    tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
+    tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
+	    - tmp13;
+
+    tmp0 = tmp10 + tmp13;
+    tmp3 = tmp10 - tmp13;
+    tmp1 = tmp11 + tmp12;
+    tmp2 = tmp11 - tmp12;
+
+    /* Odd part */
+
+    z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
+    z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
+    z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
+    z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];
+
+    tmp7 = z11 + z13;		/* phase 5 */
+    tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
+
+    z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
+    tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
+    tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
+
+    tmp6 = tmp12 - tmp7;	/* phase 2 */
+    tmp5 = tmp11 - tmp6;
+    tmp4 = tmp10 + tmp5;
+
+    /* Final output stage: scale down by a factor of 8 and range-limit */
+
+    outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
+			    & RANGE_MASK];
+
+    wsptr += DCTSIZE;		/* advance pointer to next row */
+  }
+}
+
+#endif /* DCT_IFAST_SUPPORTED */
diff --git a/jidctint.c b/jidctint.c
new file mode 100644
index 0000000..f25b08d
--- /dev/null
+++ b/jidctint.c
@@ -0,0 +1,388 @@
+/*
+ * jidctint.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a slow-but-accurate integer implementation of the
+ * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
+ * must also perform dequantization of the input coefficients.
+ *
+ * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
+ * on each row (or vice versa, but it's more convenient to emit a row at
+ * a time).  Direct algorithms are also available, but they are much more
+ * complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on an algorithm described in
+ *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
+ *   Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
+ *   Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
+ * The primary algorithm described there uses 11 multiplies and 29 adds.
+ * We use their alternate method with 12 multiplies and 32 adds.
+ * The advantage of this method is that no data path contains more than one
+ * multiplication; this allows a very simple and accurate implementation in
+ * scaled fixed-point arithmetic, with a minimal number of shifts.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h"		/* Private declarations for DCT subsystem */
+
+#ifdef DCT_ISLOW_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/*
+ * The poop on this scaling stuff is as follows:
+ *
+ * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
+ * larger than the true IDCT outputs.  The final outputs are therefore
+ * a factor of N larger than desired; since N=8 this can be cured by
+ * a simple right shift at the end of the algorithm.  The advantage of
+ * this arrangement is that we save two multiplications per 1-D IDCT,
+ * because the y0 and y4 inputs need not be divided by sqrt(N).
+ *
+ * We have to do addition and subtraction of the integer inputs, which
+ * is no problem, and multiplication by fractional constants, which is
+ * a problem to do in integer arithmetic.  We multiply all the constants
+ * by CONST_SCALE and convert them to integer constants (thus retaining
+ * CONST_BITS bits of precision in the constants).  After doing a
+ * multiplication we have to divide the product by CONST_SCALE, with proper
+ * rounding, to produce the correct output.  This division can be done
+ * cheaply as a right shift of CONST_BITS bits.  We postpone shifting
+ * as long as possible so that partial sums can be added together with
+ * full fractional precision.
+ *
+ * The outputs of the first pass are scaled up by PASS1_BITS bits so that
+ * they are represented to better-than-integral precision.  These outputs
+ * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
+ * with the recommended scaling.  (To scale up 12-bit sample data further, an
+ * intermediate INT32 array would be needed.)
+ *
+ * To avoid overflow of the 32-bit intermediate results in pass 2, we must
+ * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
+ * shows that the values given below are the most effective.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define CONST_BITS  13
+#define PASS1_BITS  2
+#else
+#define CONST_BITS  13
+#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
+#endif
+
+/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
+ * causing a lot of useless floating-point operations at run time.
+ * To get around this we use the following pre-calculated constants.
+ * If you change CONST_BITS you may want to add appropriate values.
+ * (With a reasonable C compiler, you can just rely on the FIX() macro...)
+ */
+
+#if CONST_BITS == 13
+#define FIX_0_298631336  ((INT32)  2446)	/* FIX(0.298631336) */
+#define FIX_0_390180644  ((INT32)  3196)	/* FIX(0.390180644) */
+#define FIX_0_541196100  ((INT32)  4433)	/* FIX(0.541196100) */
+#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
+#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
+#define FIX_1_175875602  ((INT32)  9633)	/* FIX(1.175875602) */
+#define FIX_1_501321110  ((INT32)  12299)	/* FIX(1.501321110) */
+#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
+#define FIX_1_961570560  ((INT32)  16069)	/* FIX(1.961570560) */
+#define FIX_2_053119869  ((INT32)  16819)	/* FIX(2.053119869) */
+#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
+#define FIX_3_072711026  ((INT32)  25172)	/* FIX(3.072711026) */
+#else
+#define FIX_0_298631336  FIX(0.298631336)
+#define FIX_0_390180644  FIX(0.390180644)
+#define FIX_0_541196100  FIX(0.541196100)
+#define FIX_0_765366865  FIX(0.765366865)
+#define FIX_0_899976223  FIX(0.899976223)
+#define FIX_1_175875602  FIX(1.175875602)
+#define FIX_1_501321110  FIX(1.501321110)
+#define FIX_1_847759065  FIX(1.847759065)
+#define FIX_1_961570560  FIX(1.961570560)
+#define FIX_2_053119869  FIX(2.053119869)
+#define FIX_2_562915447  FIX(2.562915447)
+#define FIX_3_072711026  FIX(3.072711026)
+#endif
+
+
+/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
+ * For 8-bit samples with the recommended scaling, all the variable
+ * and constant values involved are no more than 16 bits wide, so a
+ * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
+ * For 12-bit samples, a full 32-bit multiplication will be needed.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
+#else
+#define MULTIPLY(var,const)  ((var) * (const))
+#endif
+
+
+/* Dequantize a coefficient by multiplying it by the multiplier-table
+ * entry; produce an int result.  In this module, both inputs and result
+ * are 16 bits or less, so either int or short multiply will work.
+ */
+
+#define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients.
+ */
+
+GLOBAL void
+jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+		 JCOEFPTR coef_block,
+		 JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+  INT32 tmp0, tmp1, tmp2, tmp3;
+  INT32 tmp10, tmp11, tmp12, tmp13;
+  INT32 z1, z2, z3, z4, z5;
+  JCOEFPTR inptr;
+  ISLOW_MULT_TYPE * quantptr;
+  int * wsptr;
+  JSAMPROW outptr;
+  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+  int ctr;
+  int workspace[DCTSIZE2];	/* buffers data between passes */
+  SHIFT_TEMPS
+
+  /* Pass 1: process columns from input, store into work array. */
+  /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
+  /* furthermore, we scale the results by 2**PASS1_BITS. */
+
+  inptr = coef_block;
+  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+  wsptr = workspace;
+  for (ctr = DCTSIZE; ctr > 0; ctr--) {
+    /* Due to quantization, we will usually find that many of the input
+     * coefficients are zero, especially the AC terms.  We can exploit this
+     * by short-circuiting the IDCT calculation for any column in which all
+     * the AC terms are zero.  In that case each output is equal to the
+     * DC coefficient (with scale factor as needed).
+     * With typical images and quantization tables, half or more of the
+     * column DCT calculations can be simplified this way.
+     */
+    
+    if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
+	 inptr[DCTSIZE*4] | inptr[DCTSIZE*5] | inptr[DCTSIZE*6] |
+	 inptr[DCTSIZE*7]) == 0) {
+      /* AC terms all zero */
+      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
+      
+      wsptr[DCTSIZE*0] = dcval;
+      wsptr[DCTSIZE*1] = dcval;
+      wsptr[DCTSIZE*2] = dcval;
+      wsptr[DCTSIZE*3] = dcval;
+      wsptr[DCTSIZE*4] = dcval;
+      wsptr[DCTSIZE*5] = dcval;
+      wsptr[DCTSIZE*6] = dcval;
+      wsptr[DCTSIZE*7] = dcval;
+      
+      inptr++;			/* advance pointers to next column */
+      quantptr++;
+      wsptr++;
+      continue;
+    }
+    
+    /* Even part: reverse the even part of the forward DCT. */
+    /* The rotator is sqrt(2)*c(-6). */
+    
+    z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+    z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+    
+    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
+    tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
+    tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
+    
+    z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+    z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
+
+    tmp0 = (z2 + z3) << CONST_BITS;
+    tmp1 = (z2 - z3) << CONST_BITS;
+    
+    tmp10 = tmp0 + tmp3;
+    tmp13 = tmp0 - tmp3;
+    tmp11 = tmp1 + tmp2;
+    tmp12 = tmp1 - tmp2;
+    
+    /* Odd part per figure 8; the matrix is unitary and hence its
+     * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
+     */
+    
+    tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+    tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+    tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+    tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+    
+    z1 = tmp0 + tmp3;
+    z2 = tmp1 + tmp2;
+    z3 = tmp0 + tmp2;
+    z4 = tmp1 + tmp3;
+    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+    
+    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+    
+    z3 += z5;
+    z4 += z5;
+    
+    tmp0 += z1 + z3;
+    tmp1 += z2 + z4;
+    tmp2 += z2 + z3;
+    tmp3 += z1 + z4;
+    
+    /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
+    
+    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
+    wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
+    wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
+    wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
+    wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
+    wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
+    wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
+    wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
+    
+    inptr++;			/* advance pointers to next column */
+    quantptr++;
+    wsptr++;
+  }
+  
+  /* Pass 2: process rows from work array, store into output array. */
+  /* Note that we must descale the results by a factor of 8 == 2**3, */
+  /* and also undo the PASS1_BITS scaling. */
+
+  wsptr = workspace;
+  for (ctr = 0; ctr < DCTSIZE; ctr++) {
+    outptr = output_buf[ctr] + output_col;
+    /* Rows of zeroes can be exploited in the same way as we did with columns.
+     * However, the column calculation has created many nonzero AC terms, so
+     * the simplification applies less often (typically 5% to 10% of the time).
+     * On machines with very fast multiplication, it's possible that the
+     * test takes more time than it's worth.  In that case this section
+     * may be commented out.
+     */
+    
+#ifndef NO_ZERO_ROW_TEST
+    if ((wsptr[1] | wsptr[2] | wsptr[3] | wsptr[4] | wsptr[5] | wsptr[6] |
+	 wsptr[7]) == 0) {
+      /* AC terms all zero */
+      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
+				  & RANGE_MASK];
+      
+      outptr[0] = dcval;
+      outptr[1] = dcval;
+      outptr[2] = dcval;
+      outptr[3] = dcval;
+      outptr[4] = dcval;
+      outptr[5] = dcval;
+      outptr[6] = dcval;
+      outptr[7] = dcval;
+
+      wsptr += DCTSIZE;		/* advance pointer to next row */
+      continue;
+    }
+#endif
+    
+    /* Even part: reverse the even part of the forward DCT. */
+    /* The rotator is sqrt(2)*c(-6). */
+    
+    z2 = (INT32) wsptr[2];
+    z3 = (INT32) wsptr[6];
+    
+    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
+    tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
+    tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
+    
+    tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS;
+    tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS;
+    
+    tmp10 = tmp0 + tmp3;
+    tmp13 = tmp0 - tmp3;
+    tmp11 = tmp1 + tmp2;
+    tmp12 = tmp1 - tmp2;
+    
+    /* Odd part per figure 8; the matrix is unitary and hence its
+     * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
+     */
+    
+    tmp0 = (INT32) wsptr[7];
+    tmp1 = (INT32) wsptr[5];
+    tmp2 = (INT32) wsptr[3];
+    tmp3 = (INT32) wsptr[1];
+    
+    z1 = tmp0 + tmp3;
+    z2 = tmp1 + tmp2;
+    z3 = tmp0 + tmp2;
+    z4 = tmp1 + tmp3;
+    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+    
+    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+    
+    z3 += z5;
+    z4 += z5;
+    
+    tmp0 += z1 + z3;
+    tmp1 += z2 + z4;
+    tmp2 += z2 + z3;
+    tmp3 += z1 + z4;
+    
+    /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
+    
+    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
+					  CONST_BITS+PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
+					  CONST_BITS+PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
+					  CONST_BITS+PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
+					  CONST_BITS+PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
+					  CONST_BITS+PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
+					  CONST_BITS+PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
+					  CONST_BITS+PASS1_BITS+3)
+			    & RANGE_MASK];
+    outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
+					  CONST_BITS+PASS1_BITS+3)
+			    & RANGE_MASK];
+    
+    wsptr += DCTSIZE;		/* advance pointer to next row */
+  }
+}
+
+#endif /* DCT_ISLOW_SUPPORTED */
diff --git a/jidctred.c b/jidctred.c
new file mode 100644
index 0000000..019c339
--- /dev/null
+++ b/jidctred.c
@@ -0,0 +1,397 @@
+/*
+ * jidctred.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains inverse-DCT routines that produce reduced-size output:
+ * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
+ *
+ * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
+ * algorithm used in jidctint.c.  We simply replace each 8-to-8 1-D IDCT step
+ * with an 8-to-4 step that produces the four averages of two adjacent outputs
+ * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
+ * These steps were derived by computing the corresponding values at the end
+ * of the normal LL&M code, then simplifying as much as possible.
+ *
+ * 1x1 is trivial: just take the DC coefficient divided by 8.
+ *
+ * See jidctint.c for additional comments.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h"		/* Private declarations for DCT subsystem */
+
+#ifdef IDCT_SCALING_SUPPORTED
+
+
+/*
+ * This module is specialized to the case DCTSIZE = 8.
+ */
+
+#if DCTSIZE != 8
+  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
+#endif
+
+
+/* Scaling is the same as in jidctint.c. */
+
+#if BITS_IN_JSAMPLE == 8
+#define CONST_BITS  13
+#define PASS1_BITS  2
+#else
+#define CONST_BITS  13
+#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
+#endif
+
+/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
+ * causing a lot of useless floating-point operations at run time.
+ * To get around this we use the following pre-calculated constants.
+ * If you change CONST_BITS you may want to add appropriate values.
+ * (With a reasonable C compiler, you can just rely on the FIX() macro...)
+ */
+
+#if CONST_BITS == 13
+#define FIX_0_211164243  ((INT32)  1730)	/* FIX(0.211164243) */
+#define FIX_0_509795579  ((INT32)  4176)	/* FIX(0.509795579) */
+#define FIX_0_601344887  ((INT32)  4926)	/* FIX(0.601344887) */
+#define FIX_0_720959822  ((INT32)  5906)	/* FIX(0.720959822) */
+#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
+#define FIX_0_850430095  ((INT32)  6967)	/* FIX(0.850430095) */
+#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
+#define FIX_1_061594337  ((INT32)  8697)	/* FIX(1.061594337) */
+#define FIX_1_272758580  ((INT32)  10426)	/* FIX(1.272758580) */
+#define FIX_1_451774981  ((INT32)  11893)	/* FIX(1.451774981) */
+#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
+#define FIX_2_172734803  ((INT32)  17799)	/* FIX(2.172734803) */
+#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
+#define FIX_3_624509785  ((INT32)  29692)	/* FIX(3.624509785) */
+#else
+#define FIX_0_211164243  FIX(0.211164243)
+#define FIX_0_509795579  FIX(0.509795579)
+#define FIX_0_601344887  FIX(0.601344887)
+#define FIX_0_720959822  FIX(0.720959822)
+#define FIX_0_765366865  FIX(0.765366865)
+#define FIX_0_850430095  FIX(0.850430095)
+#define FIX_0_899976223  FIX(0.899976223)
+#define FIX_1_061594337  FIX(1.061594337)
+#define FIX_1_272758580  FIX(1.272758580)
+#define FIX_1_451774981  FIX(1.451774981)
+#define FIX_1_847759065  FIX(1.847759065)
+#define FIX_2_172734803  FIX(2.172734803)
+#define FIX_2_562915447  FIX(2.562915447)
+#define FIX_3_624509785  FIX(3.624509785)
+#endif
+
+
+/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
+ * For 8-bit samples with the recommended scaling, all the variable
+ * and constant values involved are no more than 16 bits wide, so a
+ * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
+ * For 12-bit samples, a full 32-bit multiplication will be needed.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
+#else
+#define MULTIPLY(var,const)  ((var) * (const))
+#endif
+
+
+/* Dequantize a coefficient by multiplying it by the multiplier-table
+ * entry; produce an int result.  In this module, both inputs and result
+ * are 16 bits or less, so either int or short multiply will work.
+ */
+
+#define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients,
+ * producing a reduced-size 4x4 output block.
+ */
+
+GLOBAL void
+jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	       JCOEFPTR coef_block,
+	       JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+  INT32 tmp0, tmp2, tmp10, tmp12;
+  INT32 z1, z2, z3, z4;
+  JCOEFPTR inptr;
+  ISLOW_MULT_TYPE * quantptr;
+  int * wsptr;
+  JSAMPROW outptr;
+  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+  int ctr;
+  int workspace[DCTSIZE*4];	/* buffers data between passes */
+  SHIFT_TEMPS
+
+  /* Pass 1: process columns from input, store into work array. */
+
+  inptr = coef_block;
+  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+  wsptr = workspace;
+  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
+    /* Don't bother to process column 4, because second pass won't use it */
+    if (ctr == DCTSIZE-4)
+      continue;
+    if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
+	 inptr[DCTSIZE*5] | inptr[DCTSIZE*6] | inptr[DCTSIZE*7]) == 0) {
+      /* AC terms all zero; we need not examine term 4 for 4x4 output */
+      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
+      
+      wsptr[DCTSIZE*0] = dcval;
+      wsptr[DCTSIZE*1] = dcval;
+      wsptr[DCTSIZE*2] = dcval;
+      wsptr[DCTSIZE*3] = dcval;
+      
+      continue;
+    }
+    
+    /* Even part */
+    
+    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+    tmp0 <<= (CONST_BITS+1);
+    
+    z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+    z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+    tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
+    
+    tmp10 = tmp0 + tmp2;
+    tmp12 = tmp0 - tmp2;
+    
+    /* Odd part */
+    
+    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+    z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+    z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+    z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+    
+    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
+	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
+	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
+	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
+    
+    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
+	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
+	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
+	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
+
+    /* Final output stage */
+    
+    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
+    wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
+    wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
+    wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
+  }
+  
+  /* Pass 2: process 4 rows from work array, store into output array. */
+
+  wsptr = workspace;
+  for (ctr = 0; ctr < 4; ctr++) {
+    outptr = output_buf[ctr] + output_col;
+    /* It's not clear whether a zero row test is worthwhile here ... */
+
+#ifndef NO_ZERO_ROW_TEST
+    if ((wsptr[1] | wsptr[2] | wsptr[3] | wsptr[5] | wsptr[6] |
+	 wsptr[7]) == 0) {
+      /* AC terms all zero */
+      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
+				  & RANGE_MASK];
+      
+      outptr[0] = dcval;
+      outptr[1] = dcval;
+      outptr[2] = dcval;
+      outptr[3] = dcval;
+      
+      wsptr += DCTSIZE;		/* advance pointer to next row */
+      continue;
+    }
+#endif
+    
+    /* Even part */
+    
+    tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
+    
+    tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
+	 + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
+    
+    tmp10 = tmp0 + tmp2;
+    tmp12 = tmp0 - tmp2;
+    
+    /* Odd part */
+    
+    z1 = (INT32) wsptr[7];
+    z2 = (INT32) wsptr[5];
+    z3 = (INT32) wsptr[3];
+    z4 = (INT32) wsptr[1];
+    
+    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
+	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
+	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
+	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
+    
+    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
+	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
+	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
+	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
+
+    /* Final output stage */
+    
+    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
+					  CONST_BITS+PASS1_BITS+3+1)
+			    & RANGE_MASK];
+    outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
+					  CONST_BITS+PASS1_BITS+3+1)
+			    & RANGE_MASK];
+    outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
+					  CONST_BITS+PASS1_BITS+3+1)
+			    & RANGE_MASK];
+    outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
+					  CONST_BITS+PASS1_BITS+3+1)
+			    & RANGE_MASK];
+    
+    wsptr += DCTSIZE;		/* advance pointer to next row */
+  }
+}
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients,
+ * producing a reduced-size 2x2 output block.
+ */
+
+GLOBAL void
+jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	       JCOEFPTR coef_block,
+	       JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+  INT32 tmp0, tmp10, z1;
+  JCOEFPTR inptr;
+  ISLOW_MULT_TYPE * quantptr;
+  int * wsptr;
+  JSAMPROW outptr;
+  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+  int ctr;
+  int workspace[DCTSIZE*2];	/* buffers data between passes */
+  SHIFT_TEMPS
+
+  /* Pass 1: process columns from input, store into work array. */
+
+  inptr = coef_block;
+  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+  wsptr = workspace;
+  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
+    /* Don't bother to process columns 2,4,6 */
+    if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
+      continue;
+    if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*3] |
+	 inptr[DCTSIZE*5] | inptr[DCTSIZE*7]) == 0) {
+      /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
+      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
+      
+      wsptr[DCTSIZE*0] = dcval;
+      wsptr[DCTSIZE*1] = dcval;
+      
+      continue;
+    }
+    
+    /* Even part */
+    
+    z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+    tmp10 = z1 << (CONST_BITS+2);
+    
+    /* Odd part */
+
+    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+    tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
+    z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+    tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
+    z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+    tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
+    z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+    tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
+
+    /* Final output stage */
+    
+    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
+    wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
+  }
+  
+  /* Pass 2: process 2 rows from work array, store into output array. */
+
+  wsptr = workspace;
+  for (ctr = 0; ctr < 2; ctr++) {
+    outptr = output_buf[ctr] + output_col;
+    /* It's not clear whether a zero row test is worthwhile here ... */
+
+#ifndef NO_ZERO_ROW_TEST
+    if ((wsptr[1] | wsptr[3] | wsptr[5] | wsptr[7]) == 0) {
+      /* AC terms all zero */
+      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
+				  & RANGE_MASK];
+      
+      outptr[0] = dcval;
+      outptr[1] = dcval;
+      
+      wsptr += DCTSIZE;		/* advance pointer to next row */
+      continue;
+    }
+#endif
+    
+    /* Even part */
+    
+    tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
+    
+    /* Odd part */
+
+    tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
+	 + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
+	 + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
+	 + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
+
+    /* Final output stage */
+    
+    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
+					  CONST_BITS+PASS1_BITS+3+2)
+			    & RANGE_MASK];
+    outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
+					  CONST_BITS+PASS1_BITS+3+2)
+			    & RANGE_MASK];
+    
+    wsptr += DCTSIZE;		/* advance pointer to next row */
+  }
+}
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients,
+ * producing a reduced-size 1x1 output block.
+ */
+
+GLOBAL void
+jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+	       JCOEFPTR coef_block,
+	       JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+  int dcval;
+  ISLOW_MULT_TYPE * quantptr;
+  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+  SHIFT_TEMPS
+
+  /* We hardly need an inverse DCT routine for this: just take the
+   * average pixel value, which is one-eighth of the DC coefficient.
+   */
+  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+  dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
+  dcval = (int) DESCALE((INT32) dcval, 3);
+
+  output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
+}
+
+#endif /* IDCT_SCALING_SUPPORTED */
diff --git a/jinclude.h b/jinclude.h
index 8e8d185..0a4f151 100644
--- a/jinclude.h
+++ b/jinclude.h
@@ -1,53 +1,72 @@
 /*
  * jinclude.h
  *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
- * This is the central file that's #include'd by all the JPEG .c files.
- * Its purpose is to provide a single place to fix any problems with
- * including the wrong system include files.
- * You can edit these declarations if you use a system with nonstandard
- * system include files.
+ * This file exists to provide a single place to fix any problems with
+ * including the wrong system include files.  (Common problems are taken
+ * care of by the standard jconfig symbols, but on really weird systems
+ * you may have to edit this file.)
+ *
+ * NOTE: this file is NOT intended to be included by applications using the
+ * JPEG library.  Most applications need only include jpeglib.h.
  */
 
 
+/* Include auto-config file to find out which system include files we need. */
+
+#include "jconfig.h"		/* auto configuration options */
+#define JCONFIG_INCLUDED	/* so that jpeglib.h doesn't do it again */
+
 /*
- * Normally the __STDC__ macro can be taken as indicating that the system
- * include files conform to the ANSI C standard.  However, if you are running
- * GCC on a machine with non-ANSI system include files, that is not the case.
- * In that case change the following, or add -DNONANSI_INCLUDES to your CFLAGS.
+ * We need the NULL macro and size_t typedef.
+ * On an ANSI-conforming system it is sufficient to include <stddef.h>.
+ * Otherwise, we get them from <stdlib.h> or <stdio.h>; we may have to
+ * pull in <sys/types.h> as well.
+ * Note that the core JPEG library does not require <stdio.h>;
+ * only the default error handler and data source/destination modules do.
+ * But we must pull it in because of the references to FILE in jpeglib.h.
+ * You can remove those references if you want to compile without <stdio.h>.
  */
 
-#ifdef __STDC__
-#ifndef NONANSI_INCLUDES
-#define INCLUDES_ARE_ANSI	/* this is what's tested before including */
-#endif
+#ifdef HAVE_STDDEF_H
+#include <stddef.h>
 #endif
 
-/*
- * <stdio.h> is included to get the FILE typedef and NULL macro.
- * Note that the core portable-JPEG files do not actually do any I/O
- * using the stdio library; only the user interface, error handler,
- * and file reading/writing modules invoke any stdio functions.
- * (Well, we did cheat a bit in jmemmgr.c, but only if MEM_STATS is defined.)
- */
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef NEED_SYS_TYPES_H
+#include <sys/types.h>
+#endif
 
 #include <stdio.h>
 
 /*
- * We need the size_t typedef, which defines the parameter type of malloc().
- * In an ANSI-conforming implementation this is provided by <stdio.h>,
- * but on non-ANSI systems it's more likely to be in <sys/types.h>.
- * On some not-quite-ANSI systems you may find it in <stddef.h>.
+ * We need memory copying and zeroing functions, plus strncpy().
+ * ANSI and System V implementations declare these in <string.h>.
+ * BSD doesn't have the mem() functions, but it does have bcopy()/bzero().
+ * Some systems may declare memset and memcpy in <memory.h>.
+ *
+ * NOTE: we assume the size parameters to these functions are of type size_t.
+ * Change the casts in these macros if not!
  */
 
-#ifndef INCLUDES_ARE_ANSI	/* shouldn't need this if ANSI C */
-#include <sys/types.h>
-#endif
-#ifdef __SASC			/* Amiga SAS C provides it in stddef.h. */
-#include <stddef.h>
+#ifdef NEED_BSD_STRINGS
+
+#include <strings.h>
+#define MEMZERO(target,size)	bzero((void *)(target), (size_t)(size))
+#define MEMCOPY(dest,src,size)	bcopy((const void *)(src), (void *)(dest), (size_t)(size))
+
+#else /* not BSD, assume ANSI/SysV string lib */
+
+#include <string.h>
+#define MEMZERO(target,size)	memset((void *)(target), 0, (size_t)(size))
+#define MEMCOPY(dest,src,size)	memcpy((void *)(dest), (const void *)(src), (size_t)(size))
+
 #endif
 
 /*
@@ -58,12 +77,11 @@
  * we always use this SIZEOF() macro in place of using sizeof() directly.
  */
 
-#undef SIZEOF			/* in case you included X11/xmd.h */
 #define SIZEOF(object)	((size_t) sizeof(object))
 
 /*
- * fread() and fwrite() are always invoked through these macros.
- * On some systems you may need to twiddle the argument casts.
+ * The modules that use fread() and fwrite() always invoke them through
+ * these macros.  On some systems you may need to twiddle the argument casts.
  * CAUTION: argument order is different from underlying functions!
  */
 
@@ -71,36 +89,3 @@
   ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
 #define JFWRITE(file,buf,sizeofbuf)  \
   ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
-
-/*
- * We need the memcpy() and strcmp() functions, plus memory zeroing.
- * ANSI and System V implementations declare these in <string.h>.
- * BSD doesn't have the mem() functions, but it does have bcopy()/bzero().
- * Some systems may declare memset and memcpy in <memory.h>.
- *
- * NOTE: we assume the size parameters to these functions are of type size_t.
- * Change the casts in these macros if not!
- */
-
-#ifdef INCLUDES_ARE_ANSI
-#include <string.h>
-#define MEMZERO(target,size)	memset((void *)(target), 0, (size_t)(size))
-#define MEMCOPY(dest,src,size)	memcpy((void *)(dest), (const void *)(src), (size_t)(size))
-#else /* not ANSI */
-#ifdef BSD
-#include <strings.h>
-#define MEMZERO(target,size)	bzero((void *)(target), (size_t)(size))
-#define MEMCOPY(dest,src,size)	bcopy((const void *)(src), (void *)(dest), (size_t)(size))
-#else /* not BSD, assume Sys V or compatible */
-#include <string.h>
-#define MEMZERO(target,size)	memset((void *)(target), 0, (size_t)(size))
-#define MEMCOPY(dest,src,size)	memcpy((void *)(dest), (const void *)(src), (size_t)(size))
-#endif /* BSD */
-#endif /* ANSI */
-
-
-/* Now include the portable JPEG definition files. */
-
-#include "jconfig.h"
-
-#include "jpegdata.h"
diff --git a/jmemansi.c b/jmemansi.c
index 22c7d05..70010f9 100644
--- a/jmemansi.c
+++ b/jmemansi.c
@@ -1,7 +1,7 @@
 /*
- * jmemansi.c  (jmemsys.c)
+ * jmemansi.c
  *
- * Copyright (C) 1992, Thomas G. Lane.
+ * Copyright (C) 1992-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -12,14 +12,14 @@
  * is shoved onto the user.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
-#include "jmemsys.h"
+#include "jpeglib.h"
+#include "jmemsys.h"		/* import the system-dependent declarations */
 
-#ifdef INCLUDES_ARE_ANSI
-#include <stdlib.h>		/* to declare malloc(), free() */
-#else
-extern void * malloc PP((size_t size));
-extern void free PP((void *ptr));
+#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */
+extern void * malloc JPP((size_t size));
+extern void free JPP((void *ptr));
 #endif
 
 #ifndef SEEK_SET		/* pre-ANSI systems may not define this; */
@@ -27,34 +27,43 @@
 #endif
 
 
-static external_methods_ptr methods; /* saved for access to error_exit */
-
-static long total_used;		/* total memory requested so far */
-
-
 /*
  * Memory allocation and freeing are controlled by the regular library
  * routines malloc() and free().
  */
 
 GLOBAL void *
-jget_small (size_t sizeofobject)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
 {
-  total_used += sizeofobject;
   return (void *) malloc(sizeofobject);
 }
 
 GLOBAL void
-jfree_small (void * object)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
 {
   free(object);
 }
 
+
 /*
- * We assume NEED_FAR_POINTERS is not defined and so the separate entry points
- * jget_large, jfree_large are not needed.
+ * "Large" objects are treated the same as "small" ones.
+ * NB: although we include FAR keywords in the routine declarations,
+ * this file won't actually work in 80x86 small/medium model; at least,
+ * you probably won't be able to process useful-size images in only 64KB.
  */
 
+GLOBAL void FAR *
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
+{
+  return (void FAR *) malloc(sizeofobject);
+}
+
+GLOBAL void
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+  free(object);
+}
+
 
 /*
  * This routine computes the total memory space available for allocation.
@@ -69,46 +78,49 @@
 #endif
 
 GLOBAL long
-jmem_available (long min_bytes_needed, long max_bytes_needed)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+		    long max_bytes_needed, long already_allocated)
 {
-  return methods->max_memory_to_use - total_used;
+  return cinfo->mem->max_memory_to_use - already_allocated;
 }
 
 
 /*
  * Backing store (temporary file) management.
  * Backing store objects are only used when the value returned by
- * jmem_available is less than the total space needed.  You can dispense
+ * jpeg_mem_available is less than the total space needed.  You can dispense
  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  */
 
 
 METHODDEF void
-read_backing_store (backing_store_ptr info, void FAR * buffer_address,
+read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+		    void FAR * buffer_address,
 		    long file_offset, long byte_count)
 {
   if (fseek(info->temp_file, file_offset, SEEK_SET))
-    ERREXIT(methods, "fseek failed on temporary file");
+    ERREXIT(cinfo, JERR_TFILE_SEEK);
   if (JFREAD(info->temp_file, buffer_address, byte_count)
       != (size_t) byte_count)
-    ERREXIT(methods, "fread failed on temporary file");
+    ERREXIT(cinfo, JERR_TFILE_READ);
 }
 
 
 METHODDEF void
-write_backing_store (backing_store_ptr info, void FAR * buffer_address,
+write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+		     void FAR * buffer_address,
 		     long file_offset, long byte_count)
 {
   if (fseek(info->temp_file, file_offset, SEEK_SET))
-    ERREXIT(methods, "fseek failed on temporary file");
+    ERREXIT(cinfo, JERR_TFILE_SEEK);
   if (JFWRITE(info->temp_file, buffer_address, byte_count)
       != (size_t) byte_count)
-    ERREXIT(methods, "fwrite failed on temporary file --- out of disk space?");
+    ERREXIT(cinfo, JERR_TFILE_WRITE);
 }
 
 
 METHODDEF void
-close_backing_store (backing_store_ptr info)
+close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
 {
   fclose(info->temp_file);
   /* Since this implementation uses tmpfile() to create the file,
@@ -121,15 +133,16 @@
  * Initial opening of a backing-store object.
  *
  * This version uses tmpfile(), which constructs a suitable file name
- * behind the scenes.  We don't have to use temp_name[] at all;
+ * behind the scenes.  We don't have to use info->temp_name[] at all;
  * indeed, we can't even find out the actual name of the temp file.
  */
 
 GLOBAL void
-jopen_backing_store (backing_store_ptr info, long total_bytes_needed)
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+			 long total_bytes_needed)
 {
   if ((info->temp_file = tmpfile()) == NULL)
-    ERREXIT(methods, "Failed to create temporary file");
+    ERREXITS(cinfo, JERR_TFILE_CREATE, "");
   info->read_backing_store = read_backing_store;
   info->write_backing_store = write_backing_store;
   info->close_backing_store = close_backing_store;
@@ -138,20 +151,17 @@
 
 /*
  * These routines take care of any system-dependent initialization and
- * cleanup required.  Keep in mind that jmem_term may be called more than
- * once.
+ * cleanup required.
  */
 
-GLOBAL void
-jmem_init (external_methods_ptr emethods)
+GLOBAL long
+jpeg_mem_init (j_common_ptr cinfo)
 {
-  methods = emethods;		/* save struct addr for error exit access */
-  emethods->max_memory_to_use = DEFAULT_MAX_MEM;
-  total_used = 0;
+  return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
 }
 
 GLOBAL void
-jmem_term (void)
+jpeg_mem_term (j_common_ptr cinfo)
 {
   /* no work */
 }
diff --git a/jmemdos.c b/jmemdos.c
index 30f6a2e..4db8ec5 100644
--- a/jmemdos.c
+++ b/jmemdos.c
@@ -1,7 +1,7 @@
 /*
- * jmemdos.c  (jmemsys.c)
+ * jmemdos.c
  *
- * Copyright (C) 1992, Thomas G. Lane.
+ * Copyright (C) 1992-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -39,15 +39,15 @@
 #endif
 
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
-#include "jmemsys.h"
+#include "jpeglib.h"
+#include "jmemsys.h"		/* import the system-dependent declarations */
 
-#ifdef INCLUDES_ARE_ANSI
-#include <stdlib.h>		/* to declare malloc(), free(), getenv() */
-#else
-extern void * malloc PP((size_t size));
-extern void free PP((void *ptr));
-extern char * getenv PP((const char * name));
+#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare these */
+extern void * malloc JPP((size_t size));
+extern void free JPP((void *ptr));
+extern char * getenv JPP((const char * name));
 #endif
 
 #ifdef NEED_FAR_POINTERS
@@ -64,7 +64,12 @@
 #define far_free(x)	_ffree(x)
 #endif
 
-#endif
+#else /* not NEED_FAR_POINTERS */
+
+#define far_malloc(x)	malloc(x)
+#define far_free(x)	free(x)
+
+#endif /* NEED_FAR_POINTERS */
 
 #ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
 #define READ_BINARY	"r"
@@ -72,6 +77,10 @@
 #define READ_BINARY	"rb"
 #endif
 
+#if MAX_ALLOC_CHUNK >= 65535L	/* make sure jconfig.h got this right */
+  MAX_ALLOC_CHUNK should be less than 64K. /* deliberate syntax error */
+#endif
+
 
 /*
  * Declarations for assembly-language support routines (see jmemdosa.asm).
@@ -91,22 +100,17 @@
 	void far * ds_si;
       } EMScontext;
 
-EXTERN short far jdos_open PP((short far * handle, char far * filename));
-EXTERN short far jdos_close PP((short handle));
-EXTERN short far jdos_seek PP((short handle, long offset));
-EXTERN short far jdos_read PP((short handle, void far * buffer,
-			       unsigned short count));
-EXTERN short far jdos_write PP((short handle, void far * buffer,
+EXTERN short far jdos_open JPP((short far * handle, char far * filename));
+EXTERN short far jdos_close JPP((short handle));
+EXTERN short far jdos_seek JPP((short handle, long offset));
+EXTERN short far jdos_read JPP((short handle, void far * buffer,
 				unsigned short count));
-EXTERN void far jxms_getdriver PP((XMSDRIVER far *));
-EXTERN void far jxms_calldriver PP((XMSDRIVER, XMScontext far *));
-EXTERN short far jems_available PP((void));
-EXTERN void far jems_calldriver PP((EMScontext far *));
-
-
-static external_methods_ptr methods; /* saved for access to error_exit */
-
-static long total_used;		/* total FAR memory requested so far */
+EXTERN short far jdos_write JPP((short handle, void far * buffer,
+				 unsigned short count));
+EXTERN void far jxms_getdriver JPP((XMSDRIVER far *));
+EXTERN void far jxms_calldriver JPP((XMSDRIVER, XMScontext far *));
+EXTERN short far jems_available JPP((void));
+EXTERN void far jems_calldriver JPP((EMScontext far *));
 
 
 /*
@@ -155,43 +159,34 @@
  */
 
 GLOBAL void *
-jget_small (size_t sizeofobject)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
 {
-  /* near data space is NOT counted in total_used */
-#ifndef NEED_FAR_POINTERS
-  total_used += sizeofobject;
-#endif
   return (void *) malloc(sizeofobject);
 }
 
 GLOBAL void
-jfree_small (void * object)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
 {
   free(object);
 }
 
 
 /*
- * Far-memory allocation and freeing
+ * "Large" objects are allocated in far memory, if possible
  */
 
-#ifdef NEED_FAR_POINTERS
-
 GLOBAL void FAR *
-jget_large (size_t sizeofobject)
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
 {
-  total_used += sizeofobject;
   return (void FAR *) far_malloc(sizeofobject);
 }
 
 GLOBAL void
-jfree_large (void FAR * object)
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
 {
   far_free(object);
 }
 
-#endif
-
 
 /*
  * This routine computes the total memory space available for allocation.
@@ -206,16 +201,17 @@
 #endif
 
 GLOBAL long
-jmem_available (long min_bytes_needed, long max_bytes_needed)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+		    long max_bytes_needed, long already_allocated)
 {
-  return methods->max_memory_to_use - total_used;
+  return cinfo->mem->max_memory_to_use - already_allocated;
 }
 
 
 /*
  * Backing store (temporary file) management.
  * Backing store objects are only used when the value returned by
- * jmem_available is less than the total space needed.  You can dispense
+ * jpeg_mem_available is less than the total space needed.  You can dispense
  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  */
 
@@ -228,8 +224,9 @@
  *   2. Extended memory, accessed per the XMS V2.0 specification.
  *   3. Expanded memory, accessed per the LIM/EMS 4.0 specification.
  * You'll need copies of those specs to make sense of the related code.
- * The specs are available by Internet FTP from SIMTEL20 and its various
- * mirror sites; see microsoft/xms20.arc and info/limems41.zip.
+ * The specs are available by Internet FTP from the SIMTEL archives 
+ * (oak.oakland.edu and its various mirror sites).  See files
+ * pub/msdos/microsoft/xms20.arc and pub/msdos/info/limems41.zip.
  */
 
 
@@ -239,37 +236,39 @@
 
 
 METHODDEF void
-read_file_store (backing_store_ptr info, void FAR * buffer_address,
+read_file_store (j_common_ptr cinfo, backing_store_ptr info,
+		 void FAR * buffer_address,
 		 long file_offset, long byte_count)
 {
   if (jdos_seek(info->handle.file_handle, file_offset))
-    ERREXIT(methods, "seek failed on temporary file");
+    ERREXIT(cinfo, JERR_TFILE_SEEK);
   /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */
   if (byte_count > 65535L)	/* safety check */
-    ERREXIT(methods, "MAX_ALLOC_CHUNK should be less than 64K");
+    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
   if (jdos_read(info->handle.file_handle, buffer_address,
 		(unsigned short) byte_count))
-    ERREXIT(methods, "read failed on temporary file");
+    ERREXIT(cinfo, JERR_TFILE_READ);
 }
 
 
 METHODDEF void
-write_file_store (backing_store_ptr info, void FAR * buffer_address,
+write_file_store (j_common_ptr cinfo, backing_store_ptr info,
+		  void FAR * buffer_address,
 		  long file_offset, long byte_count)
 {
   if (jdos_seek(info->handle.file_handle, file_offset))
-    ERREXIT(methods, "seek failed on temporary file");
+    ERREXIT(cinfo, JERR_TFILE_SEEK);
   /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */
   if (byte_count > 65535L)	/* safety check */
-    ERREXIT(methods, "MAX_ALLOC_CHUNK should be less than 64K");
+    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
   if (jdos_write(info->handle.file_handle, buffer_address,
 		 (unsigned short) byte_count))
-    ERREXIT(methods, "write failed on temporary file --- out of disk space?");
+    ERREXIT(cinfo, JERR_TFILE_WRITE);
 }
 
 
 METHODDEF void
-close_file_store (backing_store_ptr info)
+close_file_store (j_common_ptr cinfo, backing_store_ptr info)
 {
   jdos_close(info->handle.file_handle);	/* close the file */
   remove(info->temp_name);	/* delete the file */
@@ -277,30 +276,27 @@
  * remove() is the ANSI-standard name for this function, but
  * unlink() was more common in pre-ANSI systems.
  */
-  TRACEMS1(methods, 1, "Closed DOS file %d", info->handle.file_handle);
+  TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
 }
 
 
 LOCAL boolean
-open_file_store (backing_store_ptr info, long total_bytes_needed)
+open_file_store (j_common_ptr cinfo, backing_store_ptr info,
+		 long total_bytes_needed)
 {
   short handle;
-  char tracemsg[TEMP_NAME_LENGTH+40];
 
   select_file_name(info->temp_name);
   if (jdos_open((short far *) & handle, (char far *) info->temp_name)) {
-    /* hack to get around TRACEMS' inability to handle string parameters */
-    sprintf(tracemsg, "Failed to create temporary file %s", info->temp_name);
-    ERREXIT(methods, tracemsg);	/* jopen_backing_store will fail anyway */
+    /* might as well exit since jpeg_open_backing_store will fail anyway */
+    ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
     return FALSE;
   }
   info->handle.file_handle = handle;
   info->read_backing_store = read_file_store;
   info->write_backing_store = write_file_store;
   info->close_backing_store = close_file_store;
-  /* hack to get around TRACEMS' inability to handle string parameters */
-  sprintf(tracemsg, "Opened DOS file %d  %s", handle, info->temp_name);
-  TRACEMS(methods, 1, tracemsg);
+  TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
   return TRUE;			/* succeeded */
 }
 
@@ -330,7 +326,8 @@
 
 
 METHODDEF void
-read_xms_store (backing_store_ptr info, void FAR * buffer_address,
+read_xms_store (j_common_ptr cinfo, backing_store_ptr info,
+		void FAR * buffer_address,
 		long file_offset, long byte_count)
 {
   XMScontext ctx;
@@ -351,10 +348,10 @@
   ctx.ax = 0x0b00;		/* EMB move */
   jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
   if (ctx.ax != 1)
-    ERREXIT(methods, "read from extended memory failed");
+    ERREXIT(cinfo, JERR_XMS_READ);
 
   if (ODD(byte_count)) {
-    read_xms_store(info, (void FAR *) endbuffer,
+    read_xms_store(cinfo, info, (void FAR *) endbuffer,
 		   file_offset + byte_count - 1L, 2L);
     ((char FAR *) buffer_address)[byte_count - 1L] = endbuffer[0];
   }
@@ -362,7 +359,8 @@
 
 
 METHODDEF void
-write_xms_store (backing_store_ptr info, void FAR * buffer_address,
+write_xms_store (j_common_ptr cinfo, backing_store_ptr info,
+		 void FAR * buffer_address,
 		 long file_offset, long byte_count)
 {
   XMScontext ctx;
@@ -383,33 +381,34 @@
   ctx.ax = 0x0b00;		/* EMB move */
   jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
   if (ctx.ax != 1)
-    ERREXIT(methods, "write to extended memory failed");
+    ERREXIT(cinfo, JERR_XMS_WRITE);
 
   if (ODD(byte_count)) {
-    read_xms_store(info, (void FAR *) endbuffer,
+    read_xms_store(cinfo, info, (void FAR *) endbuffer,
 		   file_offset + byte_count - 1L, 2L);
     endbuffer[0] = ((char FAR *) buffer_address)[byte_count - 1L];
-    write_xms_store(info, (void FAR *) endbuffer,
+    write_xms_store(cinfo, info, (void FAR *) endbuffer,
 		    file_offset + byte_count - 1L, 2L);
   }
 }
 
 
 METHODDEF void
-close_xms_store (backing_store_ptr info)
+close_xms_store (j_common_ptr cinfo, backing_store_ptr info)
 {
   XMScontext ctx;
 
   ctx.dx = info->handle.xms_handle;
   ctx.ax = 0x0a00;
   jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
-  TRACEMS1(methods, 1, "Freed XMS handle %u", info->handle.xms_handle);
+  TRACEMS1(cinfo, 1, JTRC_XMS_CLOSE, info->handle.xms_handle);
   /* we ignore any error return from the driver */
 }
 
 
 LOCAL boolean
-open_xms_store (backing_store_ptr info, long total_bytes_needed)
+open_xms_store (j_common_ptr cinfo, backing_store_ptr info,
+		long total_bytes_needed)
 {
   XMScontext ctx;
 
@@ -436,7 +435,7 @@
   info->read_backing_store = read_xms_store;
   info->write_backing_store = write_xms_store;
   info->close_backing_store = close_xms_store;
-  TRACEMS1(methods, 1, "Obtained XMS handle %u", ctx.dx);
+  TRACEMS1(cinfo, 1, JTRC_XMS_OPEN, ctx.dx);
   return TRUE;			/* succeeded */
 }
 
@@ -485,7 +484,8 @@
 
 
 METHODDEF void
-read_ems_store (backing_store_ptr info, void FAR * buffer_address,
+read_ems_store (j_common_ptr cinfo, backing_store_ptr info,
+		void FAR * buffer_address,
 		long file_offset, long byte_count)
 {
   EMScontext ctx;
@@ -504,12 +504,13 @@
   ctx.ax = 0x5700;		/* move memory region */
   jems_calldriver((EMScontext far *) & ctx);
   if (HIBYTE(ctx.ax) != 0)
-    ERREXIT(methods, "read from expanded memory failed");
+    ERREXIT(cinfo, JERR_EMS_READ);
 }
 
 
 METHODDEF void
-write_ems_store (backing_store_ptr info, void FAR * buffer_address,
+write_ems_store (j_common_ptr cinfo, backing_store_ptr info,
+		 void FAR * buffer_address,
 		 long file_offset, long byte_count)
 {
   EMScontext ctx;
@@ -528,25 +529,26 @@
   ctx.ax = 0x5700;		/* move memory region */
   jems_calldriver((EMScontext far *) & ctx);
   if (HIBYTE(ctx.ax) != 0)
-    ERREXIT(methods, "write to expanded memory failed");
+    ERREXIT(cinfo, JERR_EMS_WRITE);
 }
 
 
 METHODDEF void
-close_ems_store (backing_store_ptr info)
+close_ems_store (j_common_ptr cinfo, backing_store_ptr info)
 {
   EMScontext ctx;
 
   ctx.ax = 0x4500;
   ctx.dx = info->handle.ems_handle;
   jems_calldriver((EMScontext far *) & ctx);
-  TRACEMS1(methods, 1, "Freed EMS handle %u", info->handle.ems_handle);
+  TRACEMS1(cinfo, 1, JTRC_EMS_CLOSE, info->handle.ems_handle);
   /* we ignore any error return from the driver */
 }
 
 
 LOCAL boolean
-open_ems_store (backing_store_ptr info, long total_bytes_needed)
+open_ems_store (j_common_ptr cinfo, backing_store_ptr info,
+		long total_bytes_needed)
 {
   EMScontext ctx;
 
@@ -578,7 +580,7 @@
   info->read_backing_store = read_ems_store;
   info->write_backing_store = write_ems_store;
   info->close_backing_store = close_ems_store;
-  TRACEMS1(methods, 1, "Obtained EMS handle %u", ctx.dx);
+  TRACEMS1(cinfo, 1, JTRC_EMS_OPEN, ctx.dx);
   return TRUE;			/* succeeded */
 }
 
@@ -590,40 +592,38 @@
  */
 
 GLOBAL void
-jopen_backing_store (backing_store_ptr info, long total_bytes_needed)
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+			 long total_bytes_needed)
 {
   /* Try extended memory, then expanded memory, then regular file. */
 #if XMS_SUPPORTED
-  if (open_xms_store(info, total_bytes_needed))
+  if (open_xms_store(cinfo, info, total_bytes_needed))
     return;
 #endif
 #if EMS_SUPPORTED
-  if (open_ems_store(info, total_bytes_needed))
+  if (open_ems_store(cinfo, info, total_bytes_needed))
     return;
 #endif
-  if (open_file_store(info, total_bytes_needed))
+  if (open_file_store(cinfo, info, total_bytes_needed))
     return;
-  ERREXIT(methods, "Failed to create temporary file");
+  ERREXITS(cinfo, JERR_TFILE_CREATE, "");
 }
 
 
 /*
  * These routines take care of any system-dependent initialization and
- * cleanup required.  Keep in mind that jmem_term may be called more than
- * once.
+ * cleanup required.
  */
 
-GLOBAL void
-jmem_init (external_methods_ptr emethods)
+GLOBAL long
+jpeg_mem_init (j_common_ptr cinfo)
 {
-  methods = emethods;		/* save struct addr for error exit access */
-  emethods->max_memory_to_use = DEFAULT_MAX_MEM;
-  total_used = 0;
-  next_file_num = 0;
+  next_file_num = 0;		/* initialize temp file name generator */
+  return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
 }
 
 GLOBAL void
-jmem_term (void)
+jpeg_mem_term (j_common_ptr cinfo)
 {
   /* Microsoft C, at least in v6.00A, will not successfully reclaim freed
    * blocks of size > 32Kbytes unless we give it a kick in the rear, like so:
diff --git a/jmemdos.h b/jmemdos.h
deleted file mode 100644
index 215dea6..0000000
--- a/jmemdos.h
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * jmemdos.h  (jmemsys.h)
- *
- * Copyright (C) 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This include file defines the interface between the system-independent
- * and system-dependent portions of the JPEG memory manager.  (The system-
- * independent portion is jmemmgr.c; there are several different versions
- * of the system-dependent portion, and of this file for that matter.)
- *
- * This version is suitable for MS-DOS (80x86) implementations.
- */
-
-
-/*
- * These two functions are used to allocate and release small chunks of
- * memory (typically the total amount requested through jget_small is
- * no more than 20Kb or so).  Behavior should be the same as for the
- * standard library functions malloc and free; in particular, jget_small
- * returns NULL on failure.  On most systems, these ARE malloc and free.
- * On an 80x86 machine using small-data memory model, these manage near heap.
- */
-
-EXTERN void * jget_small PP((size_t sizeofobject));
-EXTERN void jfree_small PP((void * object));
-
-/*
- * These two functions are used to allocate and release large chunks of
- * memory (up to the total free space designated by jmem_available).
- * The interface is the same as above, except that on an 80x86 machine,
- * far pointers are used.  On other systems these ARE the same as above.
- */
-
-#ifdef NEED_FAR_POINTERS	/* typically not needed except on 80x86 */
-EXTERN void FAR * jget_large PP((size_t sizeofobject));
-EXTERN void jfree_large PP((void FAR * object));
-#else
-#define jget_large(sizeofobject)	jget_small(sizeofobject)
-#define jfree_large(object)		jfree_small(object)
-#endif
-
-/*
- * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
- * be requested in a single call on jget_large (and jget_small for that
- * matter, but that case should never come into play).  This macro is needed
- * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
- * On machines with flat address spaces, any large constant may be used here.
- */
-
-#define MAX_ALLOC_CHUNK		65440L	/* leave room for malloc overhead */
-
-/*
- * This routine computes the total space available for allocation by
- * jget_large.  If more space than this is needed, backing store will be used.
- * NOTE: any memory already allocated must not be counted.
- *
- * There is a minimum space requirement, corresponding to the minimum
- * feasible buffer sizes; jmemmgr.c will request that much space even if
- * jmem_available returns zero.  The maximum space needed, enough to hold
- * all working storage in memory, is also passed in case it is useful.
- *
- * It is OK for jmem_available to underestimate the space available (that'll
- * just lead to more backing-store access than is really necessary).
- * However, an overestimate will lead to failure.  Hence it's wise to subtract
- * a slop factor from the true available space, especially if jget_small space
- * comes from the same pool.  5% should be enough.
- *
- * On machines with lots of virtual memory, any large constant may be returned.
- * Conversely, zero may be returned to always use the minimum amount of memory.
- */
-
-EXTERN long jmem_available PP((long min_bytes_needed, long max_bytes_needed));
-
-
-/*
- * This structure holds whatever state is needed to access a single
- * backing-store object.  The read/write/close method pointers are called
- * by jmemmgr.c to manipulate the backing-store object; all other fields
- * are private to the system-dependent backing store routines.
- */
-
-#define TEMP_NAME_LENGTH   64	/* max length of a temporary file's name */
-
-typedef unsigned short XMSH;	/* type of extended-memory handles */
-typedef unsigned short EMSH;	/* type of expanded-memory handles */
-
-typedef union {
-	short file_handle;	/* DOS file handle if it's a temp file */
-	XMSH xms_handle;	/* handle if it's a chunk of XMS */
-	EMSH ems_handle;	/* handle if it's a chunk of EMS */
-      } handle_union;
-
-typedef struct backing_store_struct * backing_store_ptr;
-
-typedef struct backing_store_struct {
-	/* Methods for reading/writing/closing this backing-store object */
-	METHOD(void, read_backing_store, (backing_store_ptr info,
-					  void FAR * buffer_address,
-					  long file_offset, long byte_count));
-	METHOD(void, write_backing_store, (backing_store_ptr info,
-					   void FAR * buffer_address,
-					   long file_offset, long byte_count));
-	METHOD(void, close_backing_store, (backing_store_ptr info));
-	/* Private fields for system-dependent backing-store management */
-	/* For the MS-DOS environment, we need: */
-	handle_union handle;	/* reference to backing-store storage object */
-	char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
-      } backing_store_info;
-
-/*
- * Initial opening of a backing-store object.  This must fill in the
- * read/write/close pointers in the object.  The read/write routines
- * may take an error exit if the specified maximum file size is exceeded.
- * (If jmem_available always returns a large value, this routine can just
- * take an error exit.)
- */
-
-EXTERN void jopen_backing_store PP((backing_store_ptr info,
-				    long total_bytes_needed));
-
-
-/*
- * These routines take care of any system-dependent initialization and
- * cleanup required.  The system methods struct address should be saved
- * by jmem_init in case an error exit must be taken.  jmem_term may assume
- * that all requested memory has been freed and that all opened backing-
- * store objects have been closed.
- * NB: jmem_term may be called more than once, and must behave reasonably
- * if that happens.
- */
-
-EXTERN void jmem_init PP((external_methods_ptr emethods));
-EXTERN void jmem_term PP((void));
diff --git a/jmemmgr.c b/jmemmgr.c
index f41e847..76fb486 100644
--- a/jmemmgr.c
+++ b/jmemmgr.c
@@ -1,17 +1,17 @@
 /*
  * jmemmgr.c
  *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
- * This file provides the standard system-independent memory management
+ * This file contains the JPEG system-independent memory management
  * routines.  This code is usable across a wide variety of machines; most
  * of the system dependencies have been isolated in a separate file.
  * The major functions provided here are:
- *   * bookkeeping to allow all allocated memory to be freed upon exit;
+ *   * pool-based allocation and freeing of memory;
  *   * policy decisions about how to divide available memory among the
- *     various large arrays;
+ *     virtual arrays;
  *   * control logic for swapping virtual arrays between main memory and
  *     backing storage.
  * The separate system-dependent file provides the actual backing-storage
@@ -19,551 +19,490 @@
  * main memory to use.
  * This file is system-dependent in the sense that some of its functions
  * are unnecessary in some systems.  For example, if there is enough virtual
- * memory so that backing storage will never be used, much of the big-array
- * control logic could be removed.  (Of course, if you have that much memory
- * then you shouldn't care about a little bit of unused code...)
- *
- * These routines are invoked via the methods alloc_small, free_small,
- * alloc_medium, free_medium, alloc_small_sarray, free_small_sarray,
- * alloc_small_barray, free_small_barray, request_big_sarray,
- * request_big_barray, alloc_big_arrays, access_big_sarray, access_big_barray,
- * free_big_sarray, free_big_barray, and free_all.
+ * memory so that backing storage will never be used, much of the virtual
+ * array control logic could be removed.  (Of course, if you have that much
+ * memory then you shouldn't care about a little bit of unused code...)
  */
 
-#define AM_MEMORY_MANAGER	/* we define big_Xarray_control structs */
-
+#define JPEG_INTERNALS
+#define AM_MEMORY_MANAGER	/* we define jvirt_Xarray_control structs */
 #include "jinclude.h"
+#include "jpeglib.h"
 #include "jmemsys.h"		/* import the system-dependent declarations */
 
 #ifndef NO_GETENV
-#ifdef INCLUDES_ARE_ANSI
-#include <stdlib.h>		/* to declare getenv() */
-#else
-extern char * getenv PP((const char * name));
+#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare getenv() */
+extern char * getenv JPP((const char * name));
 #endif
 #endif
 
 
 /*
- * On many systems it is not necessary to distinguish alloc_small from
- * alloc_medium; the main case where they must be distinguished is when
- * FAR pointers are distinct from regular pointers.  However, you might
- * want to keep them separate if you have different system-dependent logic
- * for small and large memory requests (i.e., jget_small and jget_large
- * do different things).
- */
-
-#ifdef NEED_FAR_POINTERS
-#define NEED_ALLOC_MEDIUM	/* flags alloc_medium really exists */
-#endif
-
-
-/*
- * Many machines require storage alignment: longs must start on 4-byte
- * boundaries, doubles on 8-byte boundaries, etc.  On such machines, malloc()
- * always returns pointers that are multiples of the worst-case alignment
- * requirement, and we had better do so too.  This means the headers that
- * we tack onto allocated structures had better have length a multiple of
- * the alignment requirement.
- * There isn't any really portable way to determine the worst-case alignment
- * requirement.  In this code we assume that the alignment requirement is
- * multiples of sizeof(align_type).  Here we define align_type as double;
- * with this definition, the code will run on all machines known to me.
- * If your machine has lesser alignment needs, you can save a few bytes
- * by making align_type smaller.
- */
-
-typedef double align_type;
-
-
-/*
  * Some important notes:
  *   The allocation routines provided here must never return NULL.
  *   They should exit to error_exit if unsuccessful.
  *
  *   It's not a good idea to try to merge the sarray and barray routines,
  *   even though they are textually almost the same, because samples are
- *   usually stored as bytes while coefficients are shorts.  Thus, in machines
- *   where byte pointers have a different representation from word pointers,
- *   the resulting machine code could not be the same.
+ *   usually stored as bytes while coefficients are shorts or ints.  Thus,
+ *   in machines where byte pointers have a different representation from
+ *   word pointers, the resulting machine code could not be the same.
  */
 
 
-static external_methods_ptr methods; /* saved for access to error_exit */
+/*
+ * Many machines require storage alignment: longs must start on 4-byte
+ * boundaries, doubles on 8-byte boundaries, etc.  On such machines, malloc()
+ * always returns pointers that are multiples of the worst-case alignment
+ * requirement, and we had better do so too.
+ * There isn't any really portable way to determine the worst-case alignment
+ * requirement.  This module assumes that the alignment requirement is
+ * multiples of sizeof(ALIGN_TYPE).
+ * By default, we define ALIGN_TYPE as double.  This is necessary on some
+ * workstations (where doubles really do need 8-byte alignment) and will work
+ * fine on nearly everything.  If your machine has lesser alignment needs,
+ * you can save a few bytes by making ALIGN_TYPE smaller.
+ * The only place I know of where this will NOT work is certain Macintosh
+ * 680x0 compilers that define double as a 10-byte IEEE extended float.
+ * Doing 10-byte alignment is counterproductive because longwords won't be
+ * aligned well.  Put "#define ALIGN_TYPE long" in jconfig.h if you have
+ * such a compiler.
+ */
+
+#ifndef ALIGN_TYPE		/* so can override from jconfig.h */
+#define ALIGN_TYPE  double
+#endif
+
+
+/*
+ * We allocate objects from "pools", where each pool is gotten with a single
+ * request to jpeg_get_small() or jpeg_get_large().  There is no per-object
+ * overhead within a pool, except for alignment padding.  Each pool has a
+ * header with a link to the next pool of the same class.
+ * Small and large pool headers are identical except that the latter's
+ * link pointer must be FAR on 80x86 machines.
+ * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
+ * field.  This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
+ * of the alignment requirement of ALIGN_TYPE.
+ */
+
+typedef union small_pool_struct * small_pool_ptr;
+
+typedef union small_pool_struct {
+  struct {
+    small_pool_ptr next;	/* next in list of pools */
+    size_t bytes_used;		/* how many bytes already used within pool */
+    size_t bytes_left;		/* bytes still available in this pool */
+  } hdr;
+  ALIGN_TYPE dummy;		/* included in union to ensure alignment */
+} small_pool_hdr;
+
+typedef union large_pool_struct FAR * large_pool_ptr;
+
+typedef union large_pool_struct {
+  struct {
+    large_pool_ptr next;	/* next in list of pools */
+    size_t bytes_used;		/* how many bytes already used within pool */
+    size_t bytes_left;		/* bytes still available in this pool */
+  } hdr;
+  ALIGN_TYPE dummy;		/* included in union to ensure alignment */
+} large_pool_hdr;
+
+
+/*
+ * Here is the full definition of a memory manager object.
+ */
+
+typedef struct {
+  struct jpeg_memory_mgr pub;	/* public fields */
+
+  /* Each pool identifier (lifetime class) names a linked list of pools. */
+  small_pool_ptr small_list[JPOOL_NUMPOOLS];
+  large_pool_ptr large_list[JPOOL_NUMPOOLS];
+
+  /* Since we only have one lifetime class of virtual arrays, only one
+   * linked list is necessary (for each datatype).  Note that the virtual
+   * array control blocks being linked together are actually stored somewhere
+   * in the small-pool list.
+   */
+  jvirt_sarray_ptr virt_sarray_list;
+  jvirt_barray_ptr virt_barray_list;
+
+  /* This counts total space obtained from jpeg_get_small/large */
+  long total_space_allocated;
+
+  /* alloc_sarray and alloc_barray set this value for use by virtual
+   * array routines.
+   */
+  JDIMENSION last_rowsperchunk;	/* from most recent alloc_sarray/barray */
+} my_memory_mgr;
+
+typedef my_memory_mgr * my_mem_ptr;
+
+
+/*
+ * The control blocks for virtual arrays.
+ * Note that these blocks are allocated in the "small" pool area.
+ * System-dependent info for the associated backing store (if any) is hidden
+ * inside the backing_store_info struct.
+ */
+
+struct jvirt_sarray_control {
+  JSAMPARRAY mem_buffer;	/* => the in-memory buffer */
+  JDIMENSION rows_in_array;	/* total virtual array height */
+  JDIMENSION samplesperrow;	/* width of array (and of memory buffer) */
+  JDIMENSION unitheight;	/* # of rows accessed by access_virt_sarray */
+  JDIMENSION rows_in_mem;	/* height of memory buffer */
+  JDIMENSION rowsperchunk;	/* allocation chunk size in mem_buffer */
+  JDIMENSION cur_start_row;	/* first logical row # in the buffer */
+  boolean dirty;		/* do current buffer contents need written? */
+  boolean b_s_open;		/* is backing-store data valid? */
+  jvirt_sarray_ptr next;	/* link to next virtual sarray control block */
+  backing_store_info b_s_info;	/* System-dependent control info */
+};
+
+struct jvirt_barray_control {
+  JBLOCKARRAY mem_buffer;	/* => the in-memory buffer */
+  JDIMENSION rows_in_array;	/* total virtual array height */
+  JDIMENSION blocksperrow;	/* width of array (and of memory buffer) */
+  JDIMENSION unitheight;	/* # of rows accessed by access_virt_barray */
+  JDIMENSION rows_in_mem;	/* height of memory buffer */
+  JDIMENSION rowsperchunk;	/* allocation chunk size in mem_buffer */
+  JDIMENSION cur_start_row;	/* first logical row # in the buffer */
+  boolean dirty;		/* do current buffer contents need written? */
+  boolean b_s_open;		/* is backing-store data valid? */
+  jvirt_barray_ptr next;	/* link to next virtual barray control block */
+  backing_store_info b_s_info;	/* System-dependent control info */
+};
 
 
 #ifdef MEM_STATS		/* optional extra stuff for statistics */
 
-/* These macros are the assumed overhead per block for malloc().
- * They don't have to be accurate, but the printed statistics will be
- * off a little bit if they are not.
- */
-#define MALLOC_OVERHEAD  (SIZEOF(void *)) /* overhead for jget_small() */
-#define MALLOC_FAR_OVERHEAD  (SIZEOF(void FAR *)) /* for jget_large() */
-
-static long total_num_small = 0;	/* total # of small objects alloced */
-static long total_bytes_small = 0;	/* total bytes requested */
-static long cur_num_small = 0;		/* # currently alloced */
-static long max_num_small = 0;		/* max simultaneously alloced */
-
-#ifdef NEED_ALLOC_MEDIUM
-static long total_num_medium = 0;	/* total # of medium objects alloced */
-static long total_bytes_medium = 0;	/* total bytes requested */
-static long cur_num_medium = 0;		/* # currently alloced */
-static long max_num_medium = 0;		/* max simultaneously alloced */
-#endif
-
-static long total_num_sarray = 0;	/* total # of sarray objects alloced */
-static long total_bytes_sarray = 0;	/* total bytes requested */
-static long cur_num_sarray = 0;		/* # currently alloced */
-static long max_num_sarray = 0;		/* max simultaneously alloced */
-
-static long total_num_barray = 0;	/* total # of barray objects alloced */
-static long total_bytes_barray = 0;	/* total bytes requested */
-static long cur_num_barray = 0;		/* # currently alloced */
-static long max_num_barray = 0;		/* max simultaneously alloced */
-
-
 LOCAL void
-print_mem_stats (void)
+print_mem_stats (j_common_ptr cinfo, int pool_id)
 {
-  /* since this is only a debugging stub, we can cheat a little on the
-   * trace message mechanism... helpful 'cuz trace_message can't handle longs.
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+  small_pool_ptr shdr_ptr;
+  large_pool_ptr lhdr_ptr;
+
+  /* Since this is only a debugging stub, we can cheat a little by using
+   * fprintf directly rather than going through the trace message code.
+   * This is helpful because message parm array can't handle longs.
    */
-  fprintf(stderr, "total_num_small = %ld\n", total_num_small);
-  fprintf(stderr, "total_bytes_small = %ld\n", total_bytes_small);
-  if (cur_num_small)
-    fprintf(stderr, "cur_num_small = %ld\n", cur_num_small);
-  fprintf(stderr, "max_num_small = %ld\n", max_num_small);
-  
-#ifdef NEED_ALLOC_MEDIUM
-  fprintf(stderr, "total_num_medium = %ld\n", total_num_medium);
-  fprintf(stderr, "total_bytes_medium = %ld\n", total_bytes_medium);
-  if (cur_num_medium)
-    fprintf(stderr, "cur_num_medium = %ld\n", cur_num_medium);
-  fprintf(stderr, "max_num_medium = %ld\n", max_num_medium);
-#endif
-  
-  fprintf(stderr, "total_num_sarray = %ld\n", total_num_sarray);
-  fprintf(stderr, "total_bytes_sarray = %ld\n", total_bytes_sarray);
-  if (cur_num_sarray)
-    fprintf(stderr, "cur_num_sarray = %ld\n", cur_num_sarray);
-  fprintf(stderr, "max_num_sarray = %ld\n", max_num_sarray);
-  
-  fprintf(stderr, "total_num_barray = %ld\n", total_num_barray);
-  fprintf(stderr, "total_bytes_barray = %ld\n", total_bytes_barray);
-  if (cur_num_barray)
-    fprintf(stderr, "cur_num_barray = %ld\n", cur_num_barray);
-  fprintf(stderr, "max_num_barray = %ld\n", max_num_barray);
+  fprintf(stderr, "Freeing pool %d, total space = %ld\n",
+	  pool_id, mem->total_space_allocated);
+
+  for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
+       lhdr_ptr = lhdr_ptr->hdr.next) {
+    fprintf(stderr, "  Large chunk used %ld\n",
+	    (long) lhdr_ptr->hdr.bytes_used);
+  }
+
+  for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
+       shdr_ptr = shdr_ptr->hdr.next) {
+    fprintf(stderr, "  Small chunk used %ld free %ld\n",
+	    (long) shdr_ptr->hdr.bytes_used,
+	    (long) shdr_ptr->hdr.bytes_left);
+  }
 }
 
 #endif /* MEM_STATS */
 
 
 LOCAL void
-out_of_memory (int which)
+out_of_memory (j_common_ptr cinfo, int which)
 /* Report an out-of-memory error and stop execution */
 /* If we compiled MEM_STATS support, report alloc requests before dying */
 {
 #ifdef MEM_STATS
-  if (methods->trace_level <= 0) /* don't do it if free_all() will */
-    print_mem_stats();		/* print optional memory usage statistics */
+  cinfo->err->trace_level = 2;	/* force self_destruct to report stats */
 #endif
-  ERREXIT1(methods, "Insufficient memory (case %d)", which);
+  ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
 }
 
 
 /*
- * Management of "small" objects.
- * These are all-in-memory, and are in near-heap space on an 80x86.
+ * Allocation of "small" objects.
+ *
+ * For these, we use pooled storage.  When a new pool must be created,
+ * we try to get enough space for the current request plus a "slop" factor,
+ * where the slop will be the amount of leftover space in the new pool.
+ * The speed vs. space tradeoff is largely determined by the slop values.
+ * A different slop value is provided for each pool class (lifetime),
+ * and we also distinguish the first pool of a class from later ones.
+ * NOTE: the values given work fairly well on both 16- and 32-bit-int
+ * machines, but may be too small if longs are 64 bits or more.
  */
 
-typedef union small_struct * small_ptr;
+static const size_t first_pool_slop[JPOOL_NUMPOOLS] = 
+{
+	1600,			/* first PERMANENT pool */
+	16000			/* first IMAGE pool */
+};
 
-typedef union small_struct {
-	small_ptr next;		/* next in list of allocated objects */
-	align_type dummy;	/* ensures alignment of following storage */
-      } small_hdr;
+static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = 
+{
+	0,			/* additional PERMANENT pools */
+	5000			/* additional IMAGE pools */
+};
 
-static small_ptr small_list;	/* head of list */
+#define MIN_SLOP  50		/* greater than 0 to avoid futile looping */
 
 
 METHODDEF void *
-alloc_small (size_t sizeofobject)
+alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
 /* Allocate a "small" object */
 {
-  small_ptr result;
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+  small_pool_ptr hdr_ptr, prev_hdr_ptr;
+  char * data_ptr;
+  size_t odd_bytes, min_request, slop;
 
-  sizeofobject += SIZEOF(small_hdr); /* add space for header */
+  /* Check for unsatisfiable request (do now to ensure no overflow below) */
+  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
+    out_of_memory(cinfo, 1);	/* request exceeds malloc's ability */
 
-#ifdef MEM_STATS
-  total_num_small++;
-  total_bytes_small += sizeofobject + MALLOC_OVERHEAD;
-  cur_num_small++;
-  if (cur_num_small > max_num_small) max_num_small = cur_num_small;
-#endif
+  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
+  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
+  if (odd_bytes > 0)
+    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
 
-  result = (small_ptr) jget_small(sizeofobject);
-  if (result == NULL)
-    out_of_memory(1);
-
-  result->next = small_list;
-  small_list = result;
-  result++;			/* advance past header */
-
-  return (void *) result;
-}
-
-
-METHODDEF void
-free_small (void *ptr)
-/* Free a "small" object */
-{
-  small_ptr hdr;
-  small_ptr * llink;
-
-  hdr = (small_ptr) ptr;
-  hdr--;			/* point back to header */
-
-  /* Remove item from list -- linear search is fast enough */
-  llink = &small_list;
-  while (*llink != hdr) {
-    if (*llink == NULL)
-      ERREXIT(methods, "Bogus free_small request");
-    llink = &( (*llink)->next );
+  /* See if space is available in any existing pool */
+  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
+  prev_hdr_ptr = NULL;
+  hdr_ptr = mem->small_list[pool_id];
+  while (hdr_ptr != NULL) {
+    if (hdr_ptr->hdr.bytes_left >= sizeofobject)
+      break;			/* found pool with enough space */
+    prev_hdr_ptr = hdr_ptr;
+    hdr_ptr = hdr_ptr->hdr.next;
   }
-  *llink = hdr->next;
 
-  jfree_small((void *) hdr);
+  /* Time to make a new pool? */
+  if (hdr_ptr == NULL) {
+    /* min_request is what we need now, slop is what will be leftover */
+    min_request = sizeofobject + SIZEOF(small_pool_hdr);
+    if (prev_hdr_ptr == NULL)	/* first pool in class? */
+      slop = first_pool_slop[pool_id];
+    else
+      slop = extra_pool_slop[pool_id];
+    /* Don't ask for more than MAX_ALLOC_CHUNK */
+    if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
+      slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
+    /* Try to get space, if fail reduce slop and try again */
+    for (;;) {
+      hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
+      if (hdr_ptr != NULL)
+	break;
+      slop /= 2;
+      if (slop < MIN_SLOP)	/* give up when it gets real small */
+	out_of_memory(cinfo, 2); /* jpeg_get_small failed */
+    }
+    mem->total_space_allocated += min_request + slop;
+    /* Success, initialize the new pool header and add to end of list */
+    hdr_ptr->hdr.next = NULL;
+    hdr_ptr->hdr.bytes_used = 0;
+    hdr_ptr->hdr.bytes_left = sizeofobject + slop;
+    if (prev_hdr_ptr == NULL)	/* first pool in class? */
+      mem->small_list[pool_id] = hdr_ptr;
+    else
+      prev_hdr_ptr->hdr.next = hdr_ptr;
+  }
 
-#ifdef MEM_STATS
-  cur_num_small--;
-#endif
+  /* OK, allocate the object from the current pool */
+  data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
+  data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
+  hdr_ptr->hdr.bytes_used += sizeofobject;
+  hdr_ptr->hdr.bytes_left -= sizeofobject;
+
+  return (void *) data_ptr;
 }
 
 
 /*
- * Management of "medium-size" objects.
- * These are just like small objects except they are in the FAR heap.
+ * Allocation of "large" objects.
+ *
+ * The external semantics of these are the same as "small" objects,
+ * except that FAR pointers are used on 80x86.  However the pool
+ * management heuristics are quite different.  We assume that each
+ * request is large enough that it may as well be passed directly to
+ * jpeg_get_large; the pool management just links everything together
+ * so that we can free it all on demand.
+ * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
+ * structures.  The routines that create these structures (see below)
+ * deliberately bunch rows together to ensure a large request size.
  */
 
-#ifdef NEED_ALLOC_MEDIUM
-
-typedef union medium_struct FAR * medium_ptr;
-
-typedef union medium_struct {
-	medium_ptr next;	/* next in list of allocated objects */
-	align_type dummy;	/* ensures alignment of following storage */
-      } medium_hdr;
-
-static medium_ptr medium_list;	/* head of list */
-
-
 METHODDEF void FAR *
-alloc_medium (size_t sizeofobject)
-/* Allocate a "medium-size" object */
+alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
+/* Allocate a "large" object */
 {
-  medium_ptr result;
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+  large_pool_ptr hdr_ptr;
+  size_t odd_bytes;
 
-  sizeofobject += SIZEOF(medium_hdr); /* add space for header */
+  /* Check for unsatisfiable request (do now to ensure no overflow below) */
+  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
+    out_of_memory(cinfo, 3);	/* request exceeds malloc's ability */
 
-#ifdef MEM_STATS
-  total_num_medium++;
-  total_bytes_medium += sizeofobject + MALLOC_FAR_OVERHEAD;
-  cur_num_medium++;
-  if (cur_num_medium > max_num_medium) max_num_medium = cur_num_medium;
-#endif
+  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
+  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
+  if (odd_bytes > 0)
+    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
 
-  result = (medium_ptr) jget_large(sizeofobject);
-  if (result == NULL)
-    out_of_memory(2);
+  /* Always make a new pool */
+  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
 
-  result->next = medium_list;
-  medium_list = result;
-  result++;			/* advance past header */
+  hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
+					    SIZEOF(large_pool_hdr));
+  if (hdr_ptr == NULL)
+    out_of_memory(cinfo, 4);	/* jpeg_get_large failed */
+  mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
 
-  return (void FAR *) result;
+  /* Success, initialize the new pool header and add to list */
+  hdr_ptr->hdr.next = mem->large_list[pool_id];
+  /* We maintain space counts in each pool header for statistical purposes,
+   * even though they are not needed for allocation.
+   */
+  hdr_ptr->hdr.bytes_used = sizeofobject;
+  hdr_ptr->hdr.bytes_left = 0;
+  mem->large_list[pool_id] = hdr_ptr;
+
+  return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
 }
 
 
-METHODDEF void
-free_medium (void FAR *ptr)
-/* Free a "medium-size" object */
-{
-  medium_ptr hdr;
-  medium_ptr FAR * llink;
-
-  hdr = (medium_ptr) ptr;
-  hdr--;			/* point back to header */
-
-  /* Remove item from list -- linear search is fast enough */
-  llink = (medium_ptr FAR *) &medium_list;
-  while (*llink != hdr) {
-    if (*llink == NULL)
-      ERREXIT(methods, "Bogus free_medium request");
-    llink = &( (*llink)->next );
-  }
-  *llink = hdr->next;
-
-  jfree_large((void FAR *) hdr);
-
-#ifdef MEM_STATS
-  cur_num_medium--;
-#endif
-}
-
-#endif /* NEED_ALLOC_MEDIUM */
-
-
 /*
- * Management of "small" (all-in-memory) 2-D sample arrays.
+ * Creation of 2-D sample arrays.
  * The pointers are in near heap, the samples themselves in FAR heap.
- * The header structure is adjacent to the row pointers.
+ *
  * To minimize allocation overhead and to allow I/O of large contiguous
  * blocks, we allocate the sample rows in groups of as many rows as possible
  * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
- * Note that the big-array control routines, later in this file, know about
- * this chunking of rows ... and also how to get the rowsperchunk value!
+ * NB: the virtual array control routines, later in this file, know about
+ * this chunking of rows.  The rowsperchunk value is left in the mem manager
+ * object so that it can be saved away if this sarray is the workspace for
+ * a virtual array.
  */
 
-typedef struct small_sarray_struct * small_sarray_ptr;
-
-typedef struct small_sarray_struct {
-	small_sarray_ptr next;	/* next in list of allocated sarrays */
-	long numrows;		/* # of rows in this array */
-	long rowsperchunk;	/* max # of rows per allocation chunk */
-	JSAMPROW dummy;		/* ensures alignment of following storage */
-      } small_sarray_hdr;
-
-static small_sarray_ptr small_sarray_list; /* head of list */
-
-
 METHODDEF JSAMPARRAY
-alloc_small_sarray (long samplesperrow, long numrows)
-/* Allocate a "small" (all-in-memory) 2-D sample array */
+alloc_sarray (j_common_ptr cinfo, int pool_id,
+	      JDIMENSION samplesperrow, JDIMENSION numrows)
+/* Allocate a 2-D sample array */
 {
-  small_sarray_ptr hdr;
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
   JSAMPARRAY result;
   JSAMPROW workspace;
-  long rowsperchunk, currow, i;
-
-#ifdef MEM_STATS
-  total_num_sarray++;
-  cur_num_sarray++;
-  if (cur_num_sarray > max_num_sarray) max_num_sarray = cur_num_sarray;
-#endif
+  JDIMENSION rowsperchunk, currow, i;
+  long ltemp;
 
   /* Calculate max # of rows allowed in one allocation chunk */
-  rowsperchunk = MAX_ALLOC_CHUNK / (samplesperrow * SIZEOF(JSAMPLE));
-  if (rowsperchunk <= 0)
-      ERREXIT(methods, "Image too wide for this implementation");
+  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
+	  ((long) samplesperrow * SIZEOF(JSAMPLE));
+  if (ltemp <= 0)
+    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+  if (ltemp < (long) numrows)
+    rowsperchunk = (JDIMENSION) ltemp;
+  else
+    rowsperchunk = numrows;
+  mem->last_rowsperchunk = rowsperchunk;
 
-  /* Get space for header and row pointers; this is always "near" on 80x86 */
-  hdr = (small_sarray_ptr) alloc_small((size_t) (numrows * SIZEOF(JSAMPROW)
-						 + SIZEOF(small_sarray_hdr)));
+  /* Get space for row pointers (small object) */
+  result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
+				    (size_t) (numrows * SIZEOF(JSAMPROW)));
 
-  result = (JSAMPARRAY) (hdr+1); /* advance past header */
-
-  /* Insert into list now so free_all does right thing if I fail */
-  /* after allocating only some of the rows... */
-  hdr->next = small_sarray_list;
-  hdr->numrows = 0;
-  hdr->rowsperchunk = rowsperchunk;
-  small_sarray_list = hdr;
-
-  /* Get the rows themselves; on 80x86 these are "far" */
+  /* Get the rows themselves (large objects) */
   currow = 0;
   while (currow < numrows) {
     rowsperchunk = MIN(rowsperchunk, numrows - currow);
-#ifdef MEM_STATS
-    total_bytes_sarray += rowsperchunk * samplesperrow * SIZEOF(JSAMPLE)
-			  + MALLOC_FAR_OVERHEAD;
-#endif
-    workspace = (JSAMPROW) jget_large((size_t) (rowsperchunk * samplesperrow
-						* SIZEOF(JSAMPLE)));
-    if (workspace == NULL)
-      out_of_memory(3);
+    workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
+	(size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
+		  * SIZEOF(JSAMPLE)));
     for (i = rowsperchunk; i > 0; i--) {
       result[currow++] = workspace;
       workspace += samplesperrow;
     }
-    hdr->numrows = currow;
   }
 
   return result;
 }
 
 
-METHODDEF void
-free_small_sarray (JSAMPARRAY ptr)
-/* Free a "small" (all-in-memory) 2-D sample array */
-{
-  small_sarray_ptr hdr;
-  small_sarray_ptr * llink;
-  long i;
-
-  hdr = (small_sarray_ptr) ptr;
-  hdr--;			/* point back to header */
-
-  /* Remove item from list -- linear search is fast enough */
-  llink = &small_sarray_list;
-  while (*llink != hdr) {
-    if (*llink == NULL)
-      ERREXIT(methods, "Bogus free_small_sarray request");
-    llink = &( (*llink)->next );
-  }
-  *llink = hdr->next;
-
-  /* Free the rows themselves; on 80x86 these are "far" */
-  /* Note we only free the row-group headers! */
-  for (i = 0; i < hdr->numrows; i += hdr->rowsperchunk) {
-    jfree_large((void FAR *) ptr[i]);
-  }
-
-  /* Free header and row pointers */
-  free_small((void *) hdr);
-
-#ifdef MEM_STATS
-  cur_num_sarray--;
-#endif
-}
-
-
 /*
- * Management of "small" (all-in-memory) 2-D coefficient-block arrays.
+ * Creation of 2-D coefficient-block arrays.
  * This is essentially the same as the code for sample arrays, above.
  */
 
-typedef struct small_barray_struct * small_barray_ptr;
-
-typedef struct small_barray_struct {
-	small_barray_ptr next;	/* next in list of allocated barrays */
-	long numrows;		/* # of rows in this array */
-	long rowsperchunk;	/* max # of rows per allocation chunk */
-	JBLOCKROW dummy;	/* ensures alignment of following storage */
-      } small_barray_hdr;
-
-static small_barray_ptr small_barray_list; /* head of list */
-
-
 METHODDEF JBLOCKARRAY
-alloc_small_barray (long blocksperrow, long numrows)
-/* Allocate a "small" (all-in-memory) 2-D coefficient-block array */
+alloc_barray (j_common_ptr cinfo, int pool_id,
+	      JDIMENSION blocksperrow, JDIMENSION numrows)
+/* Allocate a 2-D coefficient-block array */
 {
-  small_barray_ptr hdr;
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
   JBLOCKARRAY result;
   JBLOCKROW workspace;
-  long rowsperchunk, currow, i;
-
-#ifdef MEM_STATS
-  total_num_barray++;
-  cur_num_barray++;
-  if (cur_num_barray > max_num_barray) max_num_barray = cur_num_barray;
-#endif
+  JDIMENSION rowsperchunk, currow, i;
+  long ltemp;
 
   /* Calculate max # of rows allowed in one allocation chunk */
-  rowsperchunk = MAX_ALLOC_CHUNK / (blocksperrow * SIZEOF(JBLOCK));
-  if (rowsperchunk <= 0)
-      ERREXIT(methods, "Image too wide for this implementation");
+  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
+	  ((long) blocksperrow * SIZEOF(JBLOCK));
+  if (ltemp <= 0)
+    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+  if (ltemp < (long) numrows)
+    rowsperchunk = (JDIMENSION) ltemp;
+  else
+    rowsperchunk = numrows;
+  mem->last_rowsperchunk = rowsperchunk;
 
-  /* Get space for header and row pointers; this is always "near" on 80x86 */
-  hdr = (small_barray_ptr) alloc_small((size_t) (numrows * SIZEOF(JBLOCKROW)
-						 + SIZEOF(small_barray_hdr)));
+  /* Get space for row pointers (small object) */
+  result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
+				     (size_t) (numrows * SIZEOF(JBLOCKROW)));
 
-  result = (JBLOCKARRAY) (hdr+1); /* advance past header */
-
-  /* Insert into list now so free_all does right thing if I fail */
-  /* after allocating only some of the rows... */
-  hdr->next = small_barray_list;
-  hdr->numrows = 0;
-  hdr->rowsperchunk = rowsperchunk;
-  small_barray_list = hdr;
-
-  /* Get the rows themselves; on 80x86 these are "far" */
+  /* Get the rows themselves (large objects) */
   currow = 0;
   while (currow < numrows) {
     rowsperchunk = MIN(rowsperchunk, numrows - currow);
-#ifdef MEM_STATS
-    total_bytes_barray += rowsperchunk * blocksperrow * SIZEOF(JBLOCK)
-			  + MALLOC_FAR_OVERHEAD;
-#endif
-    workspace = (JBLOCKROW) jget_large((size_t) (rowsperchunk * blocksperrow
-						 * SIZEOF(JBLOCK)));
-    if (workspace == NULL)
-      out_of_memory(4);
+    workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
+	(size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
+		  * SIZEOF(JBLOCK)));
     for (i = rowsperchunk; i > 0; i--) {
       result[currow++] = workspace;
       workspace += blocksperrow;
     }
-    hdr->numrows = currow;
   }
 
   return result;
 }
 
 
-METHODDEF void
-free_small_barray (JBLOCKARRAY ptr)
-/* Free a "small" (all-in-memory) 2-D coefficient-block array */
-{
-  small_barray_ptr hdr;
-  small_barray_ptr * llink;
-  long i;
-
-  hdr = (small_barray_ptr) ptr;
-  hdr--;			/* point back to header */
-
-  /* Remove item from list -- linear search is fast enough */
-  llink = &small_barray_list;
-  while (*llink != hdr) {
-    if (*llink == NULL)
-      ERREXIT(methods, "Bogus free_small_barray request");
-    llink = &( (*llink)->next );
-  }
-  *llink = hdr->next;
-
-  /* Free the rows themselves; on 80x86 these are "far" */
-  /* Note we only free the row-group headers! */
-  for (i = 0; i < hdr->numrows; i += hdr->rowsperchunk) {
-    jfree_large((void FAR *) ptr[i]);
-  }
-
-  /* Free header and row pointers */
-  free_small((void *) hdr);
-
-#ifdef MEM_STATS
-  cur_num_barray--;
-#endif
-}
-
-
-
 /*
- * About "big" array management:
+ * About virtual array management:
  *
- * To allow machines with limited memory to handle large images,
- * all processing in the JPEG system is done a few pixel or block rows
- * at a time.  The above "small" array routines are only used to allocate
- * strip buffers (as wide as the image, but just a few rows high).
+ * To allow machines with limited memory to handle large images, all
+ * processing in the JPEG system is done a few pixel or block rows at a time.
+ * The above "normal" array routines are only used to allocate strip buffers
+ * (as wide as the image, but just a few rows high).
  * In some cases multiple passes must be made over the data.  In these
- * cases the "big" array routines are used.  The array is still accessed
+ * cases the virtual array routines are used.  The array is still accessed
  * a strip at a time, but the memory manager must save the whole array
  * for repeated accesses.  The intended implementation is that there is
  * a strip buffer in memory (as high as is possible given the desired memory
  * limit), plus a backing file that holds the rest of the array.
  *
- * The request_big_array routines are told the total size of the image (in case
- * it is useful to know the total file size that will be needed).  They are
- * also given the unit height, which is the number of rows that will be
- * accessed at once; the in-memory buffer should be made a multiple of
- * this height for best efficiency.
+ * The request_virt_array routines are told the total size of the image and
+ * the unit height, which is the number of rows that will be accessed at once;
+ * the in-memory buffer should be made a multiple of this height for best
+ * efficiency.
  *
- * The request routines create control blocks (and may open backing files),
- * but they don't create the in-memory buffers.  This is postponed until
- * alloc_big_arrays is called.  At that time the total amount of space needed
- * is known (approximately, anyway), so free memory can be divided up fairly.
+ * The request routines create control blocks but not the in-memory buffers.
+ * That is postponed until realize_virt_arrays is called.  At that time the
+ * total amount of space needed is known (approximately, anyway), so free
+ * memory can be divided up fairly.
  *
- * The access_big_array routines are responsible for making a specific strip
+ * The access_virt_array routines are responsible for making a specific strip
  * area accessible (after reading or writing the backing file, if necessary).
  * Note that the access routines are told whether the caller intends to modify
  * the accessed strip; during a read-only pass this saves having to rewrite
@@ -575,135 +514,112 @@
  * formats that use bottom-to-top scan order will require bottom-to-top read
  * passes.  The memory manager need not support multiple write passes nor
  * funny write orders (meaning that rearranging rows must be handled while
- * reading data out of the big array, not while putting it in).
+ * reading data out of the virtual array, not while putting it in).  THIS WILL
+ * PROBABLY NEED TO CHANGE ... will need multiple write passes for progressive
+ * JPEG decoding.
  *
  * In current usage, the access requests are always for nonoverlapping strips;
  * that is, successive access start_row numbers always differ by exactly the
  * unitheight.  This allows fairly simple buffer dump/reload logic if the
- * in-memory buffer is made a multiple of the unitheight.  It would be
- * possible to keep downsampled rather than fullsize data in the "big" arrays,
- * thus reducing temp file size, if we supported overlapping strip access
- * (access requests differing by less than the unitheight).  At the moment
- * I don't believe this is worth the extra complexity.
+ * in-memory buffer is made a multiple of the unitheight.  The code below
+ * would work with overlapping access requests, but not very efficiently.
  */
 
 
-
-/* The control blocks for virtual arrays.
- * System-dependent info for the associated backing store is hidden inside
- * the backing_store_info struct.
- */
-
-struct big_sarray_control {
-	long rows_in_array;	/* total virtual array height */
-	long samplesperrow;	/* width of array (and of memory buffer) */
-	long unitheight;	/* # of rows accessed by access_big_sarray() */
-	JSAMPARRAY mem_buffer;	/* the in-memory buffer */
-	long rows_in_mem;	/* height of memory buffer */
-	long rowsperchunk;	/* allocation chunk size in mem_buffer */
-	long cur_start_row;	/* first logical row # in the buffer */
-	boolean dirty;		/* do current buffer contents need written? */
-	boolean b_s_open;	/* is backing-store data valid? */
-	big_sarray_ptr next;	/* link to next big sarray control block */
-	backing_store_info b_s_info; /* System-dependent control info */
-};
-
-static big_sarray_ptr big_sarray_list; /* head of list */
-
-struct big_barray_control {
-	long rows_in_array;	/* total virtual array height */
-	long blocksperrow;	/* width of array (and of memory buffer) */
-	long unitheight;	/* # of rows accessed by access_big_barray() */
-	JBLOCKARRAY mem_buffer;	/* the in-memory buffer */
-	long rows_in_mem;	/* height of memory buffer */
-	long rowsperchunk;	/* allocation chunk size in mem_buffer */
-	long cur_start_row;	/* first logical row # in the buffer */
-	boolean dirty;		/* do current buffer contents need written? */
-	boolean b_s_open;	/* is backing-store data valid? */
-	big_barray_ptr next;	/* link to next big barray control block */
-	backing_store_info b_s_info; /* System-dependent control info */
-};
-
-static big_barray_ptr big_barray_list; /* head of list */
-
-
-METHODDEF big_sarray_ptr
-request_big_sarray (long samplesperrow, long numrows, long unitheight)
-/* Request a "big" (virtual-memory) 2-D sample array */
+METHODDEF jvirt_sarray_ptr
+request_virt_sarray (j_common_ptr cinfo, int pool_id,
+		     JDIMENSION samplesperrow, JDIMENSION numrows,
+		     JDIMENSION unitheight)
+/* Request a virtual 2-D sample array */
 {
-  big_sarray_ptr result;
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+  jvirt_sarray_ptr result;
+
+  /* Only IMAGE-lifetime virtual arrays are currently supported */
+  if (pool_id != JPOOL_IMAGE)
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
+
+  /* Round array size up to a multiple of unitheight */
+  numrows = (JDIMENSION) jround_up((long) numrows, (long) unitheight);
 
   /* get control block */
-  result = (big_sarray_ptr) alloc_small(SIZEOF(struct big_sarray_control));
+  result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
+					  SIZEOF(struct jvirt_sarray_control));
 
+  result->mem_buffer = NULL;	/* marks array not yet realized */
   result->rows_in_array = numrows;
   result->samplesperrow = samplesperrow;
   result->unitheight = unitheight;
-  result->mem_buffer = NULL;	/* marks array not yet realized */
   result->b_s_open = FALSE;	/* no associated backing-store object */
-  result->next = big_sarray_list; /* add to list of big arrays */
-  big_sarray_list = result;
+  result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
+  mem->virt_sarray_list = result;
 
   return result;
 }
 
 
-METHODDEF big_barray_ptr
-request_big_barray (long blocksperrow, long numrows, long unitheight)
-/* Request a "big" (virtual-memory) 2-D coefficient-block array */
+METHODDEF jvirt_barray_ptr
+request_virt_barray (j_common_ptr cinfo, int pool_id,
+		     JDIMENSION blocksperrow, JDIMENSION numrows,
+		     JDIMENSION unitheight)
+/* Request a virtual 2-D coefficient-block array */
 {
-  big_barray_ptr result;
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+  jvirt_barray_ptr result;
+
+  /* Only IMAGE-lifetime virtual arrays are currently supported */
+  if (pool_id != JPOOL_IMAGE)
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
+
+  /* Round array size up to a multiple of unitheight */
+  numrows = (JDIMENSION) jround_up((long) numrows, (long) unitheight);
 
   /* get control block */
-  result = (big_barray_ptr) alloc_small(SIZEOF(struct big_barray_control));
+  result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
+					  SIZEOF(struct jvirt_barray_control));
 
+  result->mem_buffer = NULL;	/* marks array not yet realized */
   result->rows_in_array = numrows;
   result->blocksperrow = blocksperrow;
   result->unitheight = unitheight;
-  result->mem_buffer = NULL;	/* marks array not yet realized */
   result->b_s_open = FALSE;	/* no associated backing-store object */
-  result->next = big_barray_list; /* add to list of big arrays */
-  big_barray_list = result;
+  result->next = mem->virt_barray_list; /* add to list of virtual arrays */
+  mem->virt_barray_list = result;
 
   return result;
 }
 
 
 METHODDEF void
-alloc_big_arrays (long extra_small_samples, long extra_small_blocks,
-		  long extra_medium_space)
-/* Allocate the in-memory buffers for any unrealized "big" arrays */
-/* 'extra' values are upper bounds for total future small-array requests */
-/* and far-heap requests */
+realize_virt_arrays (j_common_ptr cinfo)
+/* Allocate the in-memory buffers for any unrealized virtual arrays */
 {
-  long total_extra_space = extra_small_samples * SIZEOF(JSAMPLE)
-			   + extra_small_blocks * SIZEOF(JBLOCK)
-			   + extra_medium_space;
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
   long space_per_unitheight, maximum_space, avail_mem;
   long unitheights, max_unitheights;
-  big_sarray_ptr sptr;
-  big_barray_ptr bptr;
+  jvirt_sarray_ptr sptr;
+  jvirt_barray_ptr bptr;
 
   /* Compute the minimum space needed (unitheight rows in each buffer)
    * and the maximum space needed (full image height in each buffer).
-   * These may be of use to the system-dependent jmem_available routine.
+   * These may be of use to the system-dependent jpeg_mem_available routine.
    */
   space_per_unitheight = 0;
-  maximum_space = total_extra_space;
-  for (sptr = big_sarray_list; sptr != NULL; sptr = sptr->next) {
+  maximum_space = 0;
+  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
     if (sptr->mem_buffer == NULL) { /* if not realized yet */
-      space_per_unitheight += sptr->unitheight *
-			      sptr->samplesperrow * SIZEOF(JSAMPLE);
-      maximum_space += sptr->rows_in_array *
-		       sptr->samplesperrow * SIZEOF(JSAMPLE);
+      space_per_unitheight += (long) sptr->unitheight *
+			      (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
+      maximum_space += (long) sptr->rows_in_array *
+		       (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
     }
   }
-  for (bptr = big_barray_list; bptr != NULL; bptr = bptr->next) {
+  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
     if (bptr->mem_buffer == NULL) { /* if not realized yet */
-      space_per_unitheight += bptr->unitheight *
-			      bptr->blocksperrow * SIZEOF(JBLOCK);
-      maximum_space += bptr->rows_in_array *
-		       bptr->blocksperrow * SIZEOF(JBLOCK);
+      space_per_unitheight += (long) bptr->unitheight *
+			      (long) bptr->blocksperrow * SIZEOF(JBLOCK);
+      maximum_space += (long) bptr->rows_in_array *
+		       (long) bptr->blocksperrow * SIZEOF(JBLOCK);
     }
   }
 
@@ -711,8 +627,8 @@
     return;			/* no unrealized arrays, no work */
 
   /* Determine amount of memory to actually use; this is system-dependent. */
-  avail_mem = jmem_available(space_per_unitheight + total_extra_space,
-			     maximum_space);
+  avail_mem = jpeg_mem_available(cinfo, space_per_unitheight, maximum_space,
+				 mem->total_space_allocated);
 
   /* If the maximum space needed is available, make all the buffers full
    * height; otherwise parcel it out with the same number of unitheights
@@ -721,9 +637,9 @@
   if (avail_mem >= maximum_space)
     max_unitheights = 1000000000L;
   else {
-    max_unitheights = (avail_mem - total_extra_space) / space_per_unitheight;
+    max_unitheights = avail_mem / space_per_unitheight;
     /* If there doesn't seem to be enough space, try to get the minimum
-     * anyway.  This allows a "stub" implementation of jmem_available().
+     * anyway.  This allows a "stub" implementation of jpeg_mem_available().
      */
     if (max_unitheights <= 0)
       max_unitheights = 1;
@@ -731,53 +647,47 @@
 
   /* Allocate the in-memory buffers and initialize backing store as needed. */
 
-  for (sptr = big_sarray_list; sptr != NULL; sptr = sptr->next) {
+  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
     if (sptr->mem_buffer == NULL) { /* if not realized yet */
-      unitheights = (sptr->rows_in_array + sptr->unitheight - 1L)
-		    / sptr->unitheight;
+      unitheights = ((long) sptr->rows_in_array - 1L) / sptr->unitheight + 1L;
       if (unitheights <= max_unitheights) {
 	/* This buffer fits in memory */
 	sptr->rows_in_mem = sptr->rows_in_array;
       } else {
 	/* It doesn't fit in memory, create backing store. */
-	sptr->rows_in_mem = max_unitheights * sptr->unitheight;
-	jopen_backing_store(& sptr->b_s_info,
-			    (long) (sptr->rows_in_array *
-				    sptr->samplesperrow * SIZEOF(JSAMPLE)));
+	sptr->rows_in_mem = (JDIMENSION) (max_unitheights * sptr->unitheight);
+	jpeg_open_backing_store(cinfo, & sptr->b_s_info,
+				(long) sptr->rows_in_array *
+				(long) sptr->samplesperrow *
+				(long) SIZEOF(JSAMPLE));
 	sptr->b_s_open = TRUE;
       }
-      sptr->mem_buffer = alloc_small_sarray(sptr->samplesperrow,
-					    sptr->rows_in_mem);
-      /* Reach into the small_sarray header and get the rowsperchunk field.
-       * Yes, I know, this is horrible coding practice.
-       */
-      sptr->rowsperchunk =
-	((small_sarray_ptr) sptr->mem_buffer)[-1].rowsperchunk;
+      sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
+				      sptr->samplesperrow, sptr->rows_in_mem);
+      sptr->rowsperchunk = mem->last_rowsperchunk;
       sptr->cur_start_row = 0;
       sptr->dirty = FALSE;
     }
   }
 
-  for (bptr = big_barray_list; bptr != NULL; bptr = bptr->next) {
+  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
     if (bptr->mem_buffer == NULL) { /* if not realized yet */
-      unitheights = (bptr->rows_in_array + bptr->unitheight - 1L)
-		    / bptr->unitheight;
+      unitheights = ((long) bptr->rows_in_array - 1L) / bptr->unitheight + 1L;
       if (unitheights <= max_unitheights) {
 	/* This buffer fits in memory */
 	bptr->rows_in_mem = bptr->rows_in_array;
       } else {
 	/* It doesn't fit in memory, create backing store. */
-	bptr->rows_in_mem = max_unitheights * bptr->unitheight;
-	jopen_backing_store(& bptr->b_s_info,
-			    (long) (bptr->rows_in_array *
-				    bptr->blocksperrow * SIZEOF(JBLOCK)));
+	bptr->rows_in_mem = (JDIMENSION) (max_unitheights * bptr->unitheight);
+	jpeg_open_backing_store(cinfo, & bptr->b_s_info,
+				(long) bptr->rows_in_array *
+				(long) bptr->blocksperrow *
+				(long) SIZEOF(JBLOCK));
 	bptr->b_s_open = TRUE;
       }
-      bptr->mem_buffer = alloc_small_barray(bptr->blocksperrow,
-					    bptr->rows_in_mem);
-      /* Reach into the small_barray header and get the rowsperchunk field. */
-      bptr->rowsperchunk =
-	((small_barray_ptr) bptr->mem_buffer)[-1].rowsperchunk;
+      bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
+				      bptr->blocksperrow, bptr->rows_in_mem);
+      bptr->rowsperchunk = mem->last_rowsperchunk;
       bptr->cur_start_row = 0;
       bptr->dirty = FALSE;
     }
@@ -786,28 +696,29 @@
 
 
 LOCAL void
-do_sarray_io (big_sarray_ptr ptr, boolean writing)
-/* Do backing store read or write of a "big" sample array */
+do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
+/* Do backing store read or write of a virtual sample array */
 {
   long bytesperrow, file_offset, byte_count, rows, i;
 
-  bytesperrow = ptr->samplesperrow * SIZEOF(JSAMPLE);
+  bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
   file_offset = ptr->cur_start_row * bytesperrow;
   /* Loop to read or write each allocation chunk in mem_buffer */
-  for (i = 0; i < ptr->rows_in_mem; i += ptr->rowsperchunk) {
+  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
     /* One chunk, but check for short chunk at end of buffer */
-    rows = MIN(ptr->rowsperchunk, ptr->rows_in_mem - i);
+    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
     /* Transfer no more than fits in file */
-    rows = MIN(rows, ptr->rows_in_array - (ptr->cur_start_row + i));
+    rows = MIN(rows, (long) ptr->rows_in_array -
+		    ((long) ptr->cur_start_row + i));
     if (rows <= 0)		/* this chunk might be past end of file! */
       break;
     byte_count = rows * bytesperrow;
     if (writing)
-      (*ptr->b_s_info.write_backing_store) (& ptr->b_s_info,
+      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
 					    (void FAR *) ptr->mem_buffer[i],
 					    file_offset, byte_count);
     else
-      (*ptr->b_s_info.read_backing_store) (& ptr->b_s_info,
+      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
 					   (void FAR *) ptr->mem_buffer[i],
 					   file_offset, byte_count);
     file_offset += byte_count;
@@ -816,28 +727,29 @@
 
 
 LOCAL void
-do_barray_io (big_barray_ptr ptr, boolean writing)
-/* Do backing store read or write of a "big" coefficient-block array */
+do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
+/* Do backing store read or write of a virtual coefficient-block array */
 {
   long bytesperrow, file_offset, byte_count, rows, i;
 
-  bytesperrow = ptr->blocksperrow * SIZEOF(JBLOCK);
+  bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
   file_offset = ptr->cur_start_row * bytesperrow;
   /* Loop to read or write each allocation chunk in mem_buffer */
-  for (i = 0; i < ptr->rows_in_mem; i += ptr->rowsperchunk) {
+  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
     /* One chunk, but check for short chunk at end of buffer */
-    rows = MIN(ptr->rowsperchunk, ptr->rows_in_mem - i);
+    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
     /* Transfer no more than fits in file */
-    rows = MIN(rows, ptr->rows_in_array - (ptr->cur_start_row + i));
+    rows = MIN(rows, (long) ptr->rows_in_array -
+		    ((long) ptr->cur_start_row + i));
     if (rows <= 0)		/* this chunk might be past end of file! */
       break;
     byte_count = rows * bytesperrow;
     if (writing)
-      (*ptr->b_s_info.write_backing_store) (& ptr->b_s_info,
+      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
 					    (void FAR *) ptr->mem_buffer[i],
 					    file_offset, byte_count);
     else
-      (*ptr->b_s_info.read_backing_store) (& ptr->b_s_info,
+      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
 					   (void FAR *) ptr->mem_buffer[i],
 					   file_offset, byte_count);
     file_offset += byte_count;
@@ -846,46 +758,51 @@
 
 
 METHODDEF JSAMPARRAY
-access_big_sarray (big_sarray_ptr ptr, long start_row, boolean writable)
-/* Access the part of a "big" sample array starting at start_row */
+access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
+		    JDIMENSION start_row, boolean writable)
+/* Access the part of a virtual sample array starting at start_row */
 /* and extending for ptr->unitheight rows.  writable is true if  */
 /* caller intends to modify the accessed area. */
 {
   /* debugging check */
-  if (start_row < 0 || start_row+ptr->unitheight > ptr->rows_in_array ||
-      ptr->mem_buffer == NULL)
-    ERREXIT(methods, "Bogus access_big_sarray request");
+  if (start_row >= ptr->rows_in_array || ptr->mem_buffer == NULL)
+    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
 
   /* Make the desired part of the virtual array accessible */
   if (start_row < ptr->cur_start_row ||
       start_row+ptr->unitheight > ptr->cur_start_row+ptr->rows_in_mem) {
     if (! ptr->b_s_open)
-      ERREXIT(methods, "Virtual array controller messed up");
+      ERREXIT(cinfo, JERR_VIRTUAL_BUG);
     /* Flush old buffer contents if necessary */
     if (ptr->dirty) {
-      do_sarray_io(ptr, TRUE);
+      do_sarray_io(cinfo, ptr, TRUE);
       ptr->dirty = FALSE;
     }
     /* Decide what part of virtual array to access.
      * Algorithm: if target address > current window, assume forward scan,
      * load starting at target address.  If target address < current window,
-     * assume backward scan, load so that target address is top of window.
+     * assume backward scan, load so that target area is top of window.
      * Note that when switching from forward write to forward read, will have
      * start_row = 0, so the limiting case applies and we load from 0 anyway.
      */
     if (start_row > ptr->cur_start_row) {
       ptr->cur_start_row = start_row;
     } else {
-      ptr->cur_start_row = start_row + ptr->unitheight - ptr->rows_in_mem;
-      if (ptr->cur_start_row < 0)
-	ptr->cur_start_row = 0;	/* don't fall off front end of file */
+      /* use long arithmetic here to avoid overflow & unsigned problems */
+      long ltemp;
+
+      ltemp = (long) start_row + (long) ptr->unitheight -
+	      (long) ptr->rows_in_mem;
+      if (ltemp < 0)
+	ltemp = 0;		/* don't fall off front end of file */
+      ptr->cur_start_row = (JDIMENSION) ltemp;
     }
     /* If reading, read in the selected part of the array. 
      * If we are writing, we need not pre-read the selected portion,
      * since the access sequence constraints ensure it would be garbage.
      */
     if (! writable) {
-      do_sarray_io(ptr, FALSE);
+      do_sarray_io(cinfo, ptr, FALSE);
     }
   }
   /* Flag the buffer dirty if caller will write in it */
@@ -897,46 +814,51 @@
 
 
 METHODDEF JBLOCKARRAY
-access_big_barray (big_barray_ptr ptr, long start_row, boolean writable)
-/* Access the part of a "big" coefficient-block array starting at start_row */
+access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
+		    JDIMENSION start_row, boolean writable)
+/* Access the part of a virtual block array starting at start_row */
 /* and extending for ptr->unitheight rows.  writable is true if  */
 /* caller intends to modify the accessed area. */
 {
   /* debugging check */
-  if (start_row < 0 || start_row+ptr->unitheight > ptr->rows_in_array ||
-      ptr->mem_buffer == NULL)
-    ERREXIT(methods, "Bogus access_big_barray request");
+  if (start_row >= ptr->rows_in_array || ptr->mem_buffer == NULL)
+    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
 
   /* Make the desired part of the virtual array accessible */
   if (start_row < ptr->cur_start_row ||
       start_row+ptr->unitheight > ptr->cur_start_row+ptr->rows_in_mem) {
     if (! ptr->b_s_open)
-      ERREXIT(methods, "Virtual array controller messed up");
+      ERREXIT(cinfo, JERR_VIRTUAL_BUG);
     /* Flush old buffer contents if necessary */
     if (ptr->dirty) {
-      do_barray_io(ptr, TRUE);
+      do_barray_io(cinfo, ptr, TRUE);
       ptr->dirty = FALSE;
     }
     /* Decide what part of virtual array to access.
      * Algorithm: if target address > current window, assume forward scan,
      * load starting at target address.  If target address < current window,
-     * assume backward scan, load so that target address is top of window.
+     * assume backward scan, load so that target area is top of window.
      * Note that when switching from forward write to forward read, will have
      * start_row = 0, so the limiting case applies and we load from 0 anyway.
      */
     if (start_row > ptr->cur_start_row) {
       ptr->cur_start_row = start_row;
     } else {
-      ptr->cur_start_row = start_row + ptr->unitheight - ptr->rows_in_mem;
-      if (ptr->cur_start_row < 0)
-	ptr->cur_start_row = 0;	/* don't fall off front end of file */
+      /* use long arithmetic here to avoid overflow & unsigned problems */
+      long ltemp;
+
+      ltemp = (long) start_row + (long) ptr->unitheight -
+	      (long) ptr->rows_in_mem;
+      if (ltemp < 0)
+	ltemp = 0;		/* don't fall off front end of file */
+      ptr->cur_start_row = (JDIMENSION) ltemp;
     }
     /* If reading, read in the selected part of the array. 
      * If we are writing, we need not pre-read the selected portion,
      * since the access sequence constraints ensure it would be garbage.
      */
     if (! writable) {
-      do_barray_io(ptr, FALSE);
+      do_barray_io(cinfo, ptr, FALSE);
     }
   }
   /* Flag the buffer dirty if caller will write in it */
@@ -947,139 +869,178 @@
 }
 
 
-METHODDEF void
-free_big_sarray (big_sarray_ptr ptr)
-/* Free a "big" (virtual-memory) 2-D sample array */
-{
-  big_sarray_ptr * llink;
-
-  /* Remove item from list -- linear search is fast enough */
-  llink = &big_sarray_list;
-  while (*llink != ptr) {
-    if (*llink == NULL)
-      ERREXIT(methods, "Bogus free_big_sarray request");
-    llink = &( (*llink)->next );
-  }
-  *llink = ptr->next;
-
-  if (ptr->b_s_open)		/* there may be no backing store */
-    (*ptr->b_s_info.close_backing_store) (& ptr->b_s_info);
-
-  if (ptr->mem_buffer != NULL)	/* just in case never realized */
-    free_small_sarray(ptr->mem_buffer);
-
-  free_small((void *) ptr);	/* free the control block too */
-}
-
-
-METHODDEF void
-free_big_barray (big_barray_ptr ptr)
-/* Free a "big" (virtual-memory) 2-D coefficient-block array */
-{
-  big_barray_ptr * llink;
-
-  /* Remove item from list -- linear search is fast enough */
-  llink = &big_barray_list;
-  while (*llink != ptr) {
-    if (*llink == NULL)
-      ERREXIT(methods, "Bogus free_big_barray request");
-    llink = &( (*llink)->next );
-  }
-  *llink = ptr->next;
-
-  if (ptr->b_s_open)		/* there may be no backing store */
-    (*ptr->b_s_info.close_backing_store) (& ptr->b_s_info);
-
-  if (ptr->mem_buffer != NULL)	/* just in case never realized */
-    free_small_barray(ptr->mem_buffer);
-
-  free_small((void *) ptr);	/* free the control block too */
-}
-
-
 /*
- * Cleanup: free anything that's been allocated since jselmemmgr().
+ * Release all objects belonging to a specified pool.
  */
 
 METHODDEF void
-free_all (void)
+free_pool (j_common_ptr cinfo, int pool_id)
 {
-  /* First free any open "big" arrays -- these may release small arrays */
-  while (big_sarray_list != NULL)
-    free_big_sarray(big_sarray_list);
-  while (big_barray_list != NULL)
-    free_big_barray(big_barray_list);
-  /* Free any open small arrays -- these may release small objects */
-  /* +1's are because we must pass a pointer to the data, not the header */
-  while (small_sarray_list != NULL)
-    free_small_sarray((JSAMPARRAY) (small_sarray_list + 1));
-  while (small_barray_list != NULL)
-    free_small_barray((JBLOCKARRAY) (small_barray_list + 1));
-  /* Free any remaining small objects */
-  while (small_list != NULL)
-    free_small((void *) (small_list + 1));
-#ifdef NEED_ALLOC_MEDIUM
-  while (medium_list != NULL)
-    free_medium((void FAR *) (medium_list + 1));
-#endif
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+  small_pool_ptr shdr_ptr;
+  large_pool_ptr lhdr_ptr;
+  size_t space_freed;
 
-  jmem_term();			/* system-dependent cleanup */
+  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
 
 #ifdef MEM_STATS
-  if (methods->trace_level > 0)
-    print_mem_stats();		/* print optional memory usage statistics */
+  if (cinfo->err->trace_level > 1)
+    print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
 #endif
+
+  /* If freeing IMAGE pool, close any virtual arrays first */
+  if (pool_id == JPOOL_IMAGE) {
+    jvirt_sarray_ptr sptr;
+    jvirt_barray_ptr bptr;
+
+    for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+      if (sptr->b_s_open) {	/* there may be no backing store */
+	sptr->b_s_open = FALSE;	/* prevent recursive close if error */
+	(*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
+      }
+    }
+    mem->virt_sarray_list = NULL;
+    for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+      if (bptr->b_s_open) {	/* there may be no backing store */
+	bptr->b_s_open = FALSE;	/* prevent recursive close if error */
+	(*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
+      }
+    }
+    mem->virt_barray_list = NULL;
+  }
+
+  /* Release large objects */
+  lhdr_ptr = mem->large_list[pool_id];
+  mem->large_list[pool_id] = NULL;
+
+  while (lhdr_ptr != NULL) {
+    large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
+    space_freed = lhdr_ptr->hdr.bytes_used +
+		  lhdr_ptr->hdr.bytes_left +
+		  SIZEOF(large_pool_hdr);
+    jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
+    mem->total_space_allocated -= space_freed;
+    lhdr_ptr = next_lhdr_ptr;
+  }
+
+  /* Release small objects */
+  shdr_ptr = mem->small_list[pool_id];
+  mem->small_list[pool_id] = NULL;
+
+  while (shdr_ptr != NULL) {
+    small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
+    space_freed = shdr_ptr->hdr.bytes_used +
+		  shdr_ptr->hdr.bytes_left +
+		  SIZEOF(small_pool_hdr);
+    jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
+    mem->total_space_allocated -= space_freed;
+    shdr_ptr = next_shdr_ptr;
+  }
 }
 
 
 /*
- * The method selection routine for virtual memory systems.
- * The system-dependent setup routine should call this routine
- * to install the necessary method pointers in the supplied struct.
+ * Close up shop entirely.
+ * Note that this cannot be called unless cinfo->mem is non-NULL.
+ */
+
+METHODDEF void
+self_destruct (j_common_ptr cinfo)
+{
+  int pool;
+
+  /* Close all backing store, release all memory.
+   * Releasing pools in reverse order might help avoid fragmentation
+   * with some (brain-damaged) malloc libraries.
+   */
+  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
+    free_pool(cinfo, pool);
+  }
+
+  /* Release the memory manager control block too. */
+  jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
+  cinfo->mem = NULL;		/* ensures I will be called only once */
+
+  jpeg_mem_term(cinfo);		/* system-dependent cleanup */
+}
+
+
+/*
+ * Memory manager initialization.
+ * When this is called, only the error manager pointer is valid in cinfo!
  */
 
 GLOBAL void
-jselmemmgr (external_methods_ptr emethods)
+jinit_memory_mgr (j_common_ptr cinfo)
 {
-  methods = emethods;		/* save struct addr for error exit access */
+  my_mem_ptr mem;
+  long max_to_use;
+  int pool;
+  size_t test_mac;
 
-  emethods->alloc_small = alloc_small;
-  emethods->free_small = free_small;
-#ifdef NEED_ALLOC_MEDIUM
-  emethods->alloc_medium = alloc_medium;
-  emethods->free_medium = free_medium;
-#else
-  emethods->alloc_medium = alloc_small;
-  emethods->free_medium = free_small;
-#endif
-  emethods->alloc_small_sarray = alloc_small_sarray;
-  emethods->free_small_sarray = free_small_sarray;
-  emethods->alloc_small_barray = alloc_small_barray;
-  emethods->free_small_barray = free_small_barray;
-  emethods->request_big_sarray = request_big_sarray;
-  emethods->request_big_barray = request_big_barray;
-  emethods->alloc_big_arrays = alloc_big_arrays;
-  emethods->access_big_sarray = access_big_sarray;
-  emethods->access_big_barray = access_big_barray;
-  emethods->free_big_sarray = free_big_sarray;
-  emethods->free_big_barray = free_big_barray;
-  emethods->free_all = free_all;
+  cinfo->mem = NULL;		/* for safety if init fails */
 
-  /* Initialize list headers to empty */
-  small_list = NULL;
-#ifdef NEED_ALLOC_MEDIUM
-  medium_list = NULL;
-#endif
-  small_sarray_list = NULL;
-  small_barray_list = NULL;
-  big_sarray_list = NULL;
-  big_barray_list = NULL;
+  /* Check for configuration errors.
+   * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
+   * doesn't reflect any real hardware alignment requirement.
+   * The test is a little tricky: for X>0, X and X-1 have no one-bits
+   * in common if and only if X is a power of 2, ie has only one one-bit.
+   * Some compilers may give an "unreachable code" warning here; ignore it.
+   */
+  if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
+    ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
+  /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
+   * a multiple of SIZEOF(ALIGN_TYPE).
+   * Again, an "unreachable code" warning may be ignored here.
+   * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
+   */
+  test_mac = (size_t) MAX_ALLOC_CHUNK;
+  if ((long) test_mac != MAX_ALLOC_CHUNK ||
+      (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
+    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
 
-  jmem_init(emethods);		/* system-dependent initialization */
+  max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
+
+  /* Attempt to allocate memory manager's control block */
+  mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
+
+  if (mem == NULL) {
+    jpeg_mem_term(cinfo);	/* system-dependent cleanup */
+    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
+  }
+
+  /* OK, fill in the method pointers */
+  mem->pub.alloc_small = alloc_small;
+  mem->pub.alloc_large = alloc_large;
+  mem->pub.alloc_sarray = alloc_sarray;
+  mem->pub.alloc_barray = alloc_barray;
+  mem->pub.request_virt_sarray = request_virt_sarray;
+  mem->pub.request_virt_barray = request_virt_barray;
+  mem->pub.realize_virt_arrays = realize_virt_arrays;
+  mem->pub.access_virt_sarray = access_virt_sarray;
+  mem->pub.access_virt_barray = access_virt_barray;
+  mem->pub.free_pool = free_pool;
+  mem->pub.self_destruct = self_destruct;
+
+  /* Initialize working state */
+  mem->pub.max_memory_to_use = max_to_use;
+
+  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
+    mem->small_list[pool] = NULL;
+    mem->large_list[pool] = NULL;
+  }
+  mem->virt_sarray_list = NULL;
+  mem->virt_barray_list = NULL;
+
+  mem->total_space_allocated = SIZEOF(my_memory_mgr);
+
+  /* Declare ourselves open for business */
+  cinfo->mem = & mem->pub;
 
   /* Check for an environment variable JPEGMEM; if found, override the
-   * default max_memory setting from jmem_init.  Note that a command line
-   * -m argument may again override this value.
+   * default max_memory setting from jpeg_mem_init.  Note that the
+   * surrounding application may again override this value.
    * If your system doesn't support getenv(), define NO_GETENV to disable
    * this feature.
    */
@@ -1087,13 +1048,12 @@
   { char * memenv;
 
     if ((memenv = getenv("JPEGMEM")) != NULL) {
-      long lval;
       char ch = 'x';
 
-      if (sscanf(memenv, "%ld%c", &lval, &ch) > 0) {
+      if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
 	if (ch == 'm' || ch == 'M')
-	  lval *= 1000L;
-	emethods->max_memory_to_use = lval * 1000L;
+	  max_to_use *= 1000L;
+	mem->pub.max_memory_to_use = max_to_use * 1000L;
       }
     }
   }
diff --git a/jmemname.c b/jmemname.c
index 170f968..f6c9562 100644
--- a/jmemname.c
+++ b/jmemname.c
@@ -1,7 +1,7 @@
 /*
- * jmemname.c  (jmemsys.c)
+ * jmemname.c
  *
- * Copyright (C) 1992, Thomas G. Lane.
+ * Copyright (C) 1992-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -12,14 +12,14 @@
  * is shoved onto the user.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
-#include "jmemsys.h"
+#include "jpeglib.h"
+#include "jmemsys.h"		/* import the system-dependent declarations */
 
-#ifdef INCLUDES_ARE_ANSI
-#include <stdlib.h>		/* to declare malloc(), free() */
-#else
-extern void * malloc PP((size_t size));
-extern void free PP((void *ptr));
+#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */
+extern void * malloc JPP((size_t size));
+extern void free JPP((void *ptr));
 #endif
 
 #ifndef SEEK_SET		/* pre-ANSI systems may not define this; */
@@ -35,11 +35,6 @@
 #endif
 
 
-static external_methods_ptr methods; /* saved for access to error_exit */
-
-static long total_used;		/* total memory requested so far */
-
-
 /*
  * Selection of a file name for a temporary file.
  * This is system-dependent!
@@ -50,14 +45,7 @@
  *      The default value is /usr/tmp, which is the conventional place for
  *      creating large temp files on Unix.  On other systems you'll probably
  *      want to change the file location.  You can do this by editing the
- *      #define, or by defining TEMP_DIRECTORY in CFLAGS in the Makefile.
- *      For example, you might say
- *          CFLAGS= ... '-DTEMP_DIRECTORY="/tmp/"'
- *      Note that double quotes are needed in the text of the macro.
- *      With most make systems you have to put single quotes around the
- *      -D construct to preserve the double quotes.
- *	(Amiga SAS C has trouble with ":" and such in command-line options,
- *	so we've put in a special case for the preferred Amiga temp directory.)
+ *      #define, or (preferred) by defining TEMP_DIRECTORY in jconfig.h.
  *
  *  2.  If you need to change the file name as well as its location,
  *      you can override the TEMP_FILE_NAME macro.  (Note that this is
@@ -68,23 +56,19 @@
  *      simultaneously won't select the same file names.  If your system
  *      doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
  *
- *  4.  You probably want to define NEED_SIGNAL_CATCHER so that jcmain/jdmain
+ *  4.  You probably want to define NEED_SIGNAL_CATCHER so that cjpeg.c/djpeg.c
  *      will cause the temp files to be removed if you stop the program early.
  */
 
-#ifndef TEMP_DIRECTORY		/* so can override from Makefile */
-#ifdef AMIGA
-#define TEMP_DIRECTORY  "JPEGTMP:"  /* recommended setting for Amiga */
-#else
+#ifndef TEMP_DIRECTORY		/* can override from jconfig.h or Makefile */
 #define TEMP_DIRECTORY  "/usr/tmp/" /* recommended setting for Unix */
 #endif
-#endif
 
 static int next_file_num;	/* to distinguish among several temp files */
 
 #ifdef NO_MKTEMP
 
-#ifndef TEMP_FILE_NAME		/* so can override from Makefile */
+#ifndef TEMP_FILE_NAME		/* can override from jconfig.h or Makefile */
 #define TEMP_FILE_NAME  "%sJPG%03d.TMP"
 #endif
 
@@ -106,7 +90,7 @@
 #else /* ! NO_MKTEMP */
 
 /* Note that mktemp() requires the initial filename to end in six X's */
-#ifndef TEMP_FILE_NAME		/* so can override from Makefile */
+#ifndef TEMP_FILE_NAME		/* can override from jconfig.h or Makefile */
 #define TEMP_FILE_NAME  "%sJPG%dXXXXXX"
 #endif
 
@@ -128,23 +112,37 @@
  */
 
 GLOBAL void *
-jget_small (size_t sizeofobject)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
 {
-  total_used += sizeofobject;
   return (void *) malloc(sizeofobject);
 }
 
 GLOBAL void
-jfree_small (void * object)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
 {
   free(object);
 }
 
+
 /*
- * We assume NEED_FAR_POINTERS is not defined and so the separate entry points
- * jget_large, jfree_large are not needed.
+ * "Large" objects are treated the same as "small" ones.
+ * NB: although we include FAR keywords in the routine declarations,
+ * this file won't actually work in 80x86 small/medium model; at least,
+ * you probably won't be able to process useful-size images in only 64KB.
  */
 
+GLOBAL void FAR *
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
+{
+  return (void FAR *) malloc(sizeofobject);
+}
+
+GLOBAL void
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+  free(object);
+}
+
 
 /*
  * This routine computes the total memory space available for allocation.
@@ -159,46 +157,49 @@
 #endif
 
 GLOBAL long
-jmem_available (long min_bytes_needed, long max_bytes_needed)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+		    long max_bytes_needed, long already_allocated)
 {
-  return methods->max_memory_to_use - total_used;
+  return cinfo->mem->max_memory_to_use - already_allocated;
 }
 
 
 /*
  * Backing store (temporary file) management.
  * Backing store objects are only used when the value returned by
- * jmem_available is less than the total space needed.  You can dispense
+ * jpeg_mem_available is less than the total space needed.  You can dispense
  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  */
 
 
 METHODDEF void
-read_backing_store (backing_store_ptr info, void FAR * buffer_address,
+read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+		    void FAR * buffer_address,
 		    long file_offset, long byte_count)
 {
   if (fseek(info->temp_file, file_offset, SEEK_SET))
-    ERREXIT(methods, "fseek failed on temporary file");
+    ERREXIT(cinfo, JERR_TFILE_SEEK);
   if (JFREAD(info->temp_file, buffer_address, byte_count)
       != (size_t) byte_count)
-    ERREXIT(methods, "fread failed on temporary file");
+    ERREXIT(cinfo, JERR_TFILE_READ);
 }
 
 
 METHODDEF void
-write_backing_store (backing_store_ptr info, void FAR * buffer_address,
+write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+		     void FAR * buffer_address,
 		     long file_offset, long byte_count)
 {
   if (fseek(info->temp_file, file_offset, SEEK_SET))
-    ERREXIT(methods, "fseek failed on temporary file");
+    ERREXIT(cinfo, JERR_TFILE_SEEK);
   if (JFWRITE(info->temp_file, buffer_address, byte_count)
       != (size_t) byte_count)
-    ERREXIT(methods, "fwrite failed on temporary file --- out of disk space?");
+    ERREXIT(cinfo, JERR_TFILE_WRITE);
 }
 
 
 METHODDEF void
-close_backing_store (backing_store_ptr info)
+close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
 {
   fclose(info->temp_file);	/* close the file */
   unlink(info->temp_name);	/* delete the file */
@@ -206,46 +207,42 @@
  * remove() is the ANSI-standard name for this function, but if
  * your system was ANSI you'd be using jmemansi.c, right?
  */
+  TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
 }
 
 
-GLOBAL void
-jopen_backing_store (backing_store_ptr info, long total_bytes_needed)
-{
-  char tracemsg[TEMP_NAME_LENGTH+40];
+/*
+ * Initial opening of a backing-store object.
+ */
 
+GLOBAL void
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+			 long total_bytes_needed)
+{
   select_file_name(info->temp_name);
-  if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL) {
-    /* hack to get around ERREXIT's inability to handle string parameters */
-    sprintf(tracemsg, "Failed to create temporary file %s", info->temp_name);
-    ERREXIT(methods, tracemsg);
-  }
+  if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL)
+    ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
   info->read_backing_store = read_backing_store;
   info->write_backing_store = write_backing_store;
   info->close_backing_store = close_backing_store;
-  /* hack to get around TRACEMS' inability to handle string parameters */
-  sprintf(tracemsg, "Using temp file %s", info->temp_name);
-  TRACEMS(methods, 1, tracemsg);
+  TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
 }
 
 
 /*
  * These routines take care of any system-dependent initialization and
- * cleanup required.  Keep in mind that jmem_term may be called more than
- * once.
+ * cleanup required.
  */
 
-GLOBAL void
-jmem_init (external_methods_ptr emethods)
+GLOBAL long
+jpeg_mem_init (j_common_ptr cinfo)
 {
-  methods = emethods;		/* save struct addr for error exit access */
-  emethods->max_memory_to_use = DEFAULT_MAX_MEM;
-  total_used = 0;
-  next_file_num = 0;
+  next_file_num = 0;		/* initialize temp file name generator */
+  return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
 }
 
 GLOBAL void
-jmem_term (void)
+jpeg_mem_term (j_common_ptr cinfo)
 {
   /* no work */
 }
diff --git a/jmemnobs.c b/jmemnobs.c
index 05e24f6..d758f40 100644
--- a/jmemnobs.c
+++ b/jmemnobs.c
@@ -1,7 +1,7 @@
 /*
- * jmemnobs.c  (jmemsys.c)
+ * jmemnobs.c
  *
- * Copyright (C) 1992, Thomas G. Lane.
+ * Copyright (C) 1992-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -15,42 +15,54 @@
  * Note that the max_memory_to_use option is ignored by this implementation.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
-#include "jmemsys.h"
+#include "jpeglib.h"
+#include "jmemsys.h"		/* import the system-dependent declarations */
 
-#ifdef INCLUDES_ARE_ANSI
-#include <stdlib.h>		/* to declare malloc(), free() */
-#else
-extern void * malloc PP((size_t size));
-extern void free PP((void *ptr));
+#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */
+extern void * malloc JPP((size_t size));
+extern void free JPP((void *ptr));
 #endif
 
 
-static external_methods_ptr methods; /* saved for access to error_exit */
-
-
 /*
  * Memory allocation and freeing are controlled by the regular library
  * routines malloc() and free().
  */
 
 GLOBAL void *
-jget_small (size_t sizeofobject)
+jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
 {
   return (void *) malloc(sizeofobject);
 }
 
 GLOBAL void
-jfree_small (void * object)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
 {
   free(object);
 }
 
+
 /*
- * We assume NEED_FAR_POINTERS is not defined and so the separate entry points
- * jget_large, jfree_large are not needed.
+ * "Large" objects are treated the same as "small" ones.
+ * NB: although we include FAR keywords in the routine declarations,
+ * this file won't actually work in 80x86 small/medium model; at least,
+ * you probably won't be able to process useful-size images in only 64KB.
  */
 
+GLOBAL void FAR *
+jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
+{
+  return (void FAR *) malloc(sizeofobject);
+}
+
+GLOBAL void
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+  free(object);
+}
+
 
 /*
  * This routine computes the total memory space available for allocation.
@@ -58,7 +70,8 @@
  */
 
 GLOBAL long
-jmem_available (long min_bytes_needed, long max_bytes_needed)
+jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
+		    long max_bytes_needed, long already_allocated)
 {
   return max_bytes_needed;
 }
@@ -66,31 +79,31 @@
 
 /*
  * Backing store (temporary file) management.
- * This should never be called and we just error out.
+ * Since jpeg_mem_available always promised the moon,
+ * this should never be called and we can just error out.
  */
 
 GLOBAL void
-jopen_backing_store (backing_store_ptr info, long total_bytes_needed)
+jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
+			 long total_bytes_needed)
 {
-  ERREXIT(methods, "Backing store not supported");
+  ERREXIT(cinfo, JERR_NO_BACKING_STORE);
 }
 
 
 /*
  * These routines take care of any system-dependent initialization and
- * cleanup required.  Keep in mind that jmem_term may be called more than
- * once.
+ * cleanup required.  Here, there isn't any.
  */
 
-GLOBAL void
-jmem_init (external_methods_ptr emethods)
+GLOBAL long
+jpeg_mem_init (j_common_ptr cinfo)
 {
-  methods = emethods;		/* save struct addr for error exit access */
-  emethods->max_memory_to_use = 0;
+  return 0;			/* just set max_memory_to_use to 0 */
 }
 
 GLOBAL void
-jmem_term (void)
+jpeg_mem_term (j_common_ptr cinfo)
 {
   /* no work */
 }
diff --git a/jmemsys.h b/jmemsys.h
index 1766a95..033d29a 100644
--- a/jmemsys.h
+++ b/jmemsys.h
@@ -1,78 +1,107 @@
 /*
  * jmemsys.h
  *
- * Copyright (C) 1992, Thomas G. Lane.
+ * Copyright (C) 1992-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This include file defines the interface between the system-independent
- * and system-dependent portions of the JPEG memory manager.  (The system-
- * independent portion is jmemmgr.c; there are several different versions
- * of the system-dependent portion, and of this file for that matter.)
+ * and system-dependent portions of the JPEG memory manager.  No other
+ * modules need include it.  (The system-independent portion is jmemmgr.c;
+ * there are several different versions of the system-dependent portion.)
  *
- * This is a "generic" skeleton that may need to be modified for particular
- * systems.  It should be usable as-is on the majority of non-MSDOS machines.
+ * This file works as-is for the system-dependent memory managers supplied
+ * in the IJG distribution.  You may need to modify it if you write a
+ * custom memory manager.  If system-dependent changes are needed in
+ * this file, the best method is to #ifdef them based on a configuration
+ * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR.
  */
 
 
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_get_small		jGetSmall
+#define jpeg_free_small		jFreeSmall
+#define jpeg_get_large		jGetLarge
+#define jpeg_free_large		jFreeLarge
+#define jpeg_mem_available	jMemAvail
+#define jpeg_open_backing_store	jOpenBackStore
+#define jpeg_mem_init		jMemInit
+#define jpeg_mem_term		jMemTerm
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
 /*
  * These two functions are used to allocate and release small chunks of
- * memory (typically the total amount requested through jget_small is
- * no more than 20Kb or so).  Behavior should be the same as for the
- * standard library functions malloc and free; in particular, jget_small
- * returns NULL on failure.  On most systems, these ARE malloc and free.
+ * memory.  (Typically the total amount requested through jpeg_get_small is
+ * no more than 20K or so; this will be requested in chunks of a few K each.)
+ * Behavior should be the same as for the standard library functions malloc
+ * and free; in particular, jpeg_get_small must return NULL on failure.
+ * On most systems, these ARE malloc and free.  jpeg_free_small is passed the
+ * size of the object being freed, just in case it's needed.
  * On an 80x86 machine using small-data memory model, these manage near heap.
  */
 
-EXTERN void * jget_small PP((size_t sizeofobject));
-EXTERN void jfree_small PP((void * object));
+EXTERN void * jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
+EXTERN void jpeg_free_small JPP((j_common_ptr cinfo, void * object,
+				 size_t sizeofobject));
 
 /*
  * These two functions are used to allocate and release large chunks of
- * memory (up to the total free space designated by jmem_available).
+ * memory (up to the total free space designated by jpeg_mem_available).
  * The interface is the same as above, except that on an 80x86 machine,
- * far pointers are used.  On other systems these ARE the same as above.
+ * far pointers are used.  On most other machines these are identical to
+ * the jpeg_get/free_small routines; but we keep them separate anyway,
+ * in case a different allocation strategy is desirable for large chunks.
  */
 
-#ifdef NEED_FAR_POINTERS	/* typically not needed except on 80x86 */
-EXTERN void FAR * jget_large PP((size_t sizeofobject));
-EXTERN void jfree_large PP((void FAR * object));
-#else
-#define jget_large(sizeofobject)	jget_small(sizeofobject)
-#define jfree_large(object)		jfree_small(object)
-#endif
+EXTERN void FAR * jpeg_get_large JPP((j_common_ptr cinfo,size_t sizeofobject));
+EXTERN void jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
+				 size_t sizeofobject));
 
 /*
  * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
- * be requested in a single call on jget_large (and jget_small for that
+ * be requested in a single call to jpeg_get_large (and jpeg_get_small for that
  * matter, but that case should never come into play).  This macro is needed
  * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
- * On machines with flat address spaces, any large constant may be used here.
+ * On those machines, we expect that jconfig.h will provide a proper value.
+ * On machines with 32-bit flat address spaces, any large constant may be used.
+ *
+ * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
+ * size_t and will be a multiple of sizeof(align_type).
  */
 
-#define MAX_ALLOC_CHUNK		1000000000L
+#ifndef MAX_ALLOC_CHUNK		/* may be overridden in jconfig.h */
+#define MAX_ALLOC_CHUNK  1000000000L
+#endif
 
 /*
- * This routine computes the total space available for allocation by
- * jget_large.  If more space than this is needed, backing store will be used.
- * NOTE: any memory already allocated must not be counted.
+ * This routine computes the total space still available for allocation by
+ * jpeg_get_large.  If more space than this is needed, backing store will be
+ * used.  NOTE: any memory already allocated must not be counted.
  *
  * There is a minimum space requirement, corresponding to the minimum
  * feasible buffer sizes; jmemmgr.c will request that much space even if
- * jmem_available returns zero.  The maximum space needed, enough to hold
+ * jpeg_mem_available returns zero.  The maximum space needed, enough to hold
  * all working storage in memory, is also passed in case it is useful.
+ * Finally, the total space already allocated is passed.  If no better
+ * method is available, cinfo->mem->max_memory_to_use - already_allocated
+ * is often a suitable calculation.
  *
- * It is OK for jmem_available to underestimate the space available (that'll
- * just lead to more backing-store access than is really necessary).
+ * It is OK for jpeg_mem_available to underestimate the space available
+ * (that'll just lead to more backing-store access than is really necessary).
  * However, an overestimate will lead to failure.  Hence it's wise to subtract
- * a slop factor from the true available space, especially if jget_small space
- * comes from the same pool.  5% should be enough.
+ * a slop factor from the true available space.  5% should be enough.
  *
  * On machines with lots of virtual memory, any large constant may be returned.
  * Conversely, zero may be returned to always use the minimum amount of memory.
  */
 
-EXTERN long jmem_available PP((long min_bytes_needed, long max_bytes_needed));
+EXTERN long jpeg_mem_available JPP((j_common_ptr cinfo,
+				    long min_bytes_needed,
+				    long max_bytes_needed,
+				    long already_allocated));
 
 
 /*
@@ -84,44 +113,70 @@
 
 #define TEMP_NAME_LENGTH   64	/* max length of a temporary file's name */
 
+#ifdef USE_MSDOS_MEMMGR		/* DOS-specific junk */
+
+typedef unsigned short XMSH;	/* type of extended-memory handles */
+typedef unsigned short EMSH;	/* type of expanded-memory handles */
+
+typedef union {
+  short file_handle;		/* DOS file handle if it's a temp file */
+  XMSH xms_handle;		/* handle if it's a chunk of XMS */
+  EMSH ems_handle;		/* handle if it's a chunk of EMS */
+} handle_union;
+
+#endif /* USE_MSDOS_MEMMGR */
+
 typedef struct backing_store_struct * backing_store_ptr;
 
 typedef struct backing_store_struct {
-	/* Methods for reading/writing/closing this backing-store object */
-	METHOD(void, read_backing_store, (backing_store_ptr info,
-					  void FAR * buffer_address,
-					  long file_offset, long byte_count));
-	METHOD(void, write_backing_store, (backing_store_ptr info,
-					   void FAR * buffer_address,
-					   long file_offset, long byte_count));
-	METHOD(void, close_backing_store, (backing_store_ptr info));
-	/* Private fields for system-dependent backing-store management */
-	/* For a typical implementation with temp files, we might need: */
-	FILE * temp_file;	/* stdio reference to temp file */
-	char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
-      } backing_store_info;
+  /* Methods for reading/writing/closing this backing-store object */
+  JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
+				     backing_store_ptr info,
+				     void FAR * buffer_address,
+				     long file_offset, long byte_count));
+  JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
+				      backing_store_ptr info,
+				      void FAR * buffer_address,
+				      long file_offset, long byte_count));
+  JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
+				      backing_store_ptr info));
+
+  /* Private fields for system-dependent backing-store management */
+#ifdef USE_MSDOS_MEMMGR
+  /* For the MS-DOS manager (jmemdos.c), we need: */
+  handle_union handle;		/* reference to backing-store storage object */
+  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
+#else
+  /* For a typical implementation with temp files, we need: */
+  FILE * temp_file;		/* stdio reference to temp file */
+  char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
+#endif
+} backing_store_info;
 
 /*
  * Initial opening of a backing-store object.  This must fill in the
  * read/write/close pointers in the object.  The read/write routines
  * may take an error exit if the specified maximum file size is exceeded.
- * (If jmem_available always returns a large value, this routine can just
- * take an error exit.)
+ * (If jpeg_mem_available always returns a large value, this routine can
+ * just take an error exit.)
  */
 
-EXTERN void jopen_backing_store PP((backing_store_ptr info,
-				    long total_bytes_needed));
+EXTERN void jpeg_open_backing_store JPP((j_common_ptr cinfo,
+					 backing_store_ptr info,
+					 long total_bytes_needed));
 
 
 /*
  * These routines take care of any system-dependent initialization and
- * cleanup required.  The system methods struct address should be saved
- * by jmem_init in case an error exit must be taken.  jmem_term may assume
- * that all requested memory has been freed and that all opened backing-
- * store objects have been closed.
- * NB: jmem_term may be called more than once, and must behave reasonably
- * if that happens.
+ * cleanup required.  jpeg_mem_init will be called before anything is
+ * allocated (and, therefore, nothing in cinfo is of use except the error
+ * manager pointer).  It should return a suitable default value for
+ * max_memory_to_use; this may subsequently be overridden by the surrounding
+ * application.  (Note that max_memory_to_use is only important if
+ * jpeg_mem_available chooses to consult it ... no one else will.)
+ * jpeg_mem_term may assume that all requested memory has been freed and that
+ * all opened backing-store objects have been closed.
  */
 
-EXTERN void jmem_init PP((external_methods_ptr emethods));
-EXTERN void jmem_term PP((void));
+EXTERN long jpeg_mem_init JPP((j_common_ptr cinfo));
+EXTERN void jpeg_mem_term JPP((j_common_ptr cinfo));
diff --git a/jmorecfg.h b/jmorecfg.h
new file mode 100644
index 0000000..b056da5
--- /dev/null
+++ b/jmorecfg.h
@@ -0,0 +1,339 @@
+/*
+ * jmorecfg.h
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains additional configuration options that customize the
+ * JPEG software for special applications or support machine-dependent
+ * optimizations.  Most users will not need to touch this file.
+ */
+
+
+/*
+ * Define BITS_IN_JSAMPLE as either
+ *   8   for 8-bit sample values (the usual setting)
+ *   12  for 12-bit sample values
+ * Only 8 and 12 are legal data precisions for lossy JPEG according to the
+ * JPEG standard, and the IJG code does not support anything else!
+ * We do not support run-time selection of data precision, sorry.
+ */
+
+#define BITS_IN_JSAMPLE  8	/* use 8 or 12 */
+
+
+/*
+ * Maximum number of components (color channels) allowed in JPEG image.
+ * To meet the letter of the JPEG spec, set this to 255.  However, darn
+ * few applications need more than 4 channels (maybe 5 for CMYK + alpha
+ * mask).  We recommend 10 as a reasonable compromise; use 4 if you are
+ * really short on memory.  (Each allowed component costs a hundred or so
+ * bytes of storage, whether actually used in an image or not.)
+ */
+
+#define MAX_COMPONENTS  10	/* maximum number of image components */
+
+
+/*
+ * Basic data types.
+ * You may need to change these if you have a machine with unusual data
+ * type sizes; for example, "char" not 8 bits, "short" not 16 bits,
+ * or "long" not 32 bits.  We don't care whether "int" is 16 or 32 bits,
+ * but it had better be at least 16.
+ */
+
+/* Representation of a single sample (pixel element value).
+ * We frequently allocate large arrays of these, so it's important to keep
+ * them small.  But if you have memory to burn and access to char or short
+ * arrays is very slow on your hardware, you might want to change these.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+/* JSAMPLE should be the smallest type that will hold the values 0..255.
+ * You can use a signed char by having GETJSAMPLE mask it with 0xFF.
+ */
+
+#ifdef HAVE_UNSIGNED_CHAR
+
+typedef unsigned char JSAMPLE;
+#define GETJSAMPLE(value)  ((int) (value))
+
+#else /* not HAVE_UNSIGNED_CHAR */
+
+typedef char JSAMPLE;
+#ifdef CHAR_IS_UNSIGNED
+#define GETJSAMPLE(value)  ((int) (value))
+#else
+#define GETJSAMPLE(value)  ((int) (value) & 0xFF)
+#endif /* CHAR_IS_UNSIGNED */
+
+#endif /* HAVE_UNSIGNED_CHAR */
+
+#define MAXJSAMPLE	255
+#define CENTERJSAMPLE	128
+
+#endif /* BITS_IN_JSAMPLE == 8 */
+
+
+#if BITS_IN_JSAMPLE == 12
+/* JSAMPLE should be the smallest type that will hold the values 0..4095.
+ * On nearly all machines "short" will do nicely.
+ */
+
+typedef short JSAMPLE;
+#define GETJSAMPLE(value)  ((int) (value))
+
+#define MAXJSAMPLE	4095
+#define CENTERJSAMPLE	2048
+
+#endif /* BITS_IN_JSAMPLE == 12 */
+
+
+/* Representation of a DCT frequency coefficient.
+ * This should be a signed value of at least 16 bits; "short" is usually OK.
+ * Again, we allocate large arrays of these, but you can change to int
+ * if you have memory to burn and "short" is really slow.
+ */
+
+typedef short JCOEF;
+
+
+/* Compressed datastreams are represented as arrays of JOCTET.
+ * These must be EXACTLY 8 bits wide, at least once they are written to
+ * external storage.  Note that when using the stdio data source/destination
+ * managers, this is also the data type passed to fread/fwrite.
+ */
+
+#ifdef HAVE_UNSIGNED_CHAR
+
+typedef unsigned char JOCTET;
+#define GETJOCTET(value)  (value)
+
+#else /* not HAVE_UNSIGNED_CHAR */
+
+typedef char JOCTET;
+#ifdef CHAR_IS_UNSIGNED
+#define GETJOCTET(value)  (value)
+#else
+#define GETJOCTET(value)  ((value) & 0xFF)
+#endif /* CHAR_IS_UNSIGNED */
+
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+/* These typedefs are used for various table entries and so forth.
+ * They must be at least as wide as specified; but making them too big
+ * won't cost a huge amount of memory, so we don't provide special
+ * extraction code like we did for JSAMPLE.  (In other words, these
+ * typedefs live at a different point on the speed/space tradeoff curve.)
+ */
+
+/* UINT8 must hold at least the values 0..255. */
+
+#ifdef HAVE_UNSIGNED_CHAR
+typedef unsigned char UINT8;
+#else /* not HAVE_UNSIGNED_CHAR */
+#ifdef CHAR_IS_UNSIGNED
+typedef char UINT8;
+#else /* not CHAR_IS_UNSIGNED */
+typedef short UINT8;
+#endif /* CHAR_IS_UNSIGNED */
+#endif /* HAVE_UNSIGNED_CHAR */
+
+/* UINT16 must hold at least the values 0..65535. */
+
+#ifdef HAVE_UNSIGNED_SHORT
+typedef unsigned short UINT16;
+#else /* not HAVE_UNSIGNED_SHORT */
+typedef unsigned int UINT16;
+#endif /* HAVE_UNSIGNED_SHORT */
+
+/* INT16 must hold at least the values -32768..32767. */
+
+#ifndef XMD_H			/* X11/xmd.h correctly defines INT16 */
+typedef short INT16;
+#endif
+
+/* INT32 must hold at least signed 32-bit values. */
+
+#ifndef XMD_H			/* X11/xmd.h correctly defines INT32 */
+typedef long INT32;
+#endif
+
+/* Datatype used for image dimensions.  The JPEG standard only supports
+ * images up to 64K*64K due to 16-bit fields in SOF markers.  Therefore
+ * "unsigned int" is sufficient on all machines.  However, if you need to
+ * handle larger images and you don't mind deviating from the spec, you
+ * can change this datatype.
+ */
+
+typedef unsigned int JDIMENSION;
+
+#define JPEG_MAX_DIMENSION  65500L  /* a tad under 64K to prevent overflows */
+
+
+/* These defines are used in all function definitions and extern declarations.
+ * You could modify them if you need to change function linkage conventions.
+ * Another application is to make all functions global for use with debuggers
+ * or code profilers that require it.
+ */
+
+#define METHODDEF static	/* a function called through method pointers */
+#define LOCAL	  static	/* a function used only in its module */
+#define GLOBAL			/* a function referenced thru EXTERNs */
+#define EXTERN	  extern	/* a reference to a GLOBAL function */
+
+
+/* Here is the pseudo-keyword for declaring pointers that must be "far"
+ * on 80x86 machines.  Most of the specialized coding for 80x86 is handled
+ * by just saying "FAR *" where such a pointer is needed.  In a few places
+ * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
+ */
+
+#ifdef NEED_FAR_POINTERS
+#define FAR  far
+#else
+#define FAR
+#endif
+
+
+/*
+ * On a few systems, type boolean and/or its values FALSE, TRUE may appear
+ * in standard header files.  Or you may have conflicts with application-
+ * specific header files that you want to include together with these files.
+ * Defining HAVE_BOOLEAN before including jpeglib.h should make it work.
+ */
+
+#ifndef HAVE_BOOLEAN
+typedef int boolean;
+#endif
+#ifndef FALSE			/* in case these macros already exist */
+#define FALSE	0		/* values of boolean */
+#endif
+#ifndef TRUE
+#define TRUE	1
+#endif
+
+
+/*
+ * The remaining options affect code selection within the JPEG library,
+ * but they don't need to be visible to most applications using the library.
+ * To minimize application namespace pollution, the symbols won't be
+ * defined unless JPEG_INTERNALS or JPEG_INTERNAL_OPTIONS has been defined.
+ */
+
+#ifdef JPEG_INTERNALS
+#define JPEG_INTERNAL_OPTIONS
+#endif
+
+#ifdef JPEG_INTERNAL_OPTIONS
+
+
+/*
+ * These defines indicate whether to include various optional functions.
+ * Undefining some of these symbols will produce a smaller but less capable
+ * library.  Note that you can leave certain source files out of the
+ * compilation/linking process if you've #undef'd the corresponding symbols.
+ * (You may HAVE to do that if your compiler doesn't like null source files.)
+ */
+
+/* Arithmetic coding is unsupported for legal reasons.  Complaints to IBM. */
+
+/* Capability options common to encoder and decoder: */
+
+#define DCT_ISLOW_SUPPORTED	/* slow but accurate integer algorithm */
+#define DCT_IFAST_SUPPORTED	/* faster, less accurate integer method */
+#define DCT_FLOAT_SUPPORTED	/* floating-point: accurate, fast on fast HW */
+
+/* Encoder capability options: */
+
+#undef  C_ARITH_CODING_SUPPORTED    /* Arithmetic coding back end? */
+#undef  C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files?  (NYI) */
+#define ENTROPY_OPT_SUPPORTED	    /* Optimization of entropy coding parms? */
+/* Note: if you selected 12-bit data precision, it is dangerous to turn off
+ * ENTROPY_OPT_SUPPORTED.  The standard Huffman tables are only good for 8-bit
+ * precision, so jchuff.c normally uses entropy optimization to compute
+ * usable tables for higher precision.  If you don't want to do optimization,
+ * you'll have to supply different default Huffman tables.
+ */
+#define INPUT_SMOOTHING_SUPPORTED   /* Input image smoothing option? */
+
+/* Decoder capability options: */
+
+#undef  D_ARITH_CODING_SUPPORTED    /* Arithmetic coding back end? */
+#define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
+#define IDCT_SCALING_SUPPORTED	    /* Output rescaling via IDCT? */
+#undef  UPSAMPLE_SCALING_SUPPORTED  /* Output rescaling at upsample stage? */
+#define UPSAMPLE_MERGING_SUPPORTED  /* Fast path for sloppy upsampling? */
+#define QUANT_1PASS_SUPPORTED	    /* 1-pass color quantization? */
+#define QUANT_2PASS_SUPPORTED	    /* 2-pass color quantization? */
+
+/* more capability options later, no doubt */
+
+
+/*
+ * Ordering of RGB data in scanlines passed to or from the application.
+ * If your application wants to deal with data in the order B,G,R, just
+ * change these macros.  You can also deal with formats such as R,G,B,X
+ * (one extra byte per pixel) by changing RGB_PIXELSIZE.  Note that changing
+ * the offsets will also change the order in which colormap data is organized.
+ * RESTRICTIONS:
+ * 1. The sample applications cjpeg,djpeg do NOT support modified RGB formats.
+ * 2. These macros only affect RGB<=>YCbCr color conversion, so they are not
+ *    useful if you are using JPEG color spaces other than YCbCr or grayscale.
+ * 3. The color quantizer modules will not behave desirably if RGB_PIXELSIZE
+ *    is not 3 (they don't understand about dummy color components!).  So you
+ *    can't use color quantization if you change that value.
+ */
+
+#define RGB_RED		0	/* Offset of Red in an RGB scanline element */
+#define RGB_GREEN	1	/* Offset of Green */
+#define RGB_BLUE	2	/* Offset of Blue */
+#define RGB_PIXELSIZE	3	/* JSAMPLEs per RGB scanline element */
+
+
+/* Definitions for speed-related optimizations. */
+
+
+/* If your compiler supports inline functions, define INLINE
+ * as the inline keyword; otherwise define it as empty.
+ */
+
+#ifndef INLINE
+#ifdef __GNUC__			/* for instance, GNU C knows about inline */
+#define INLINE __inline__
+#endif
+#ifndef INLINE
+#define INLINE			/* default is to define it as empty */
+#endif
+#endif
+
+
+/* On some machines (notably 68000 series) "int" is 32 bits, but multiplying
+ * two 16-bit shorts is faster than multiplying two ints.  Define MULTIPLIER
+ * as short on such a machine.  MULTIPLIER must be at least 16 bits wide.
+ */
+
+#ifndef MULTIPLIER
+#define MULTIPLIER  int		/* type for fastest integer multiply */
+#endif
+
+
+/* FAST_FLOAT should be either float or double, whichever is done faster
+ * by your compiler.  (Note that this type is only used in the floating point
+ * DCT routines, so it only matters if you've defined DCT_FLOAT_SUPPORTED.)
+ * Typically, float is faster in ANSI C compilers, while double is faster in
+ * pre-ANSI compilers (because they insist on converting to double anyway).
+ * The code below therefore chooses float if we have ANSI-style prototypes.
+ */
+
+#ifndef FAST_FLOAT
+#ifdef HAVE_PROTOTYPES
+#define FAST_FLOAT  float
+#else
+#define FAST_FLOAT  double
+#endif
+#endif
+
+#endif /* JPEG_INTERNAL_OPTIONS */
diff --git a/jpegdata.h b/jpegdata.h
deleted file mode 100644
index 8e0b886..0000000
--- a/jpegdata.h
+++ /dev/null
@@ -1,959 +0,0 @@
-/*
- * jpegdata.h
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file defines shared data structures for the various JPEG modules.
- */
-
-
-/*
- * You might need to change some of the following declarations if you are
- * using the JPEG software within a surrounding application program
- * or porting it to an unusual system.
- */
-
-
-/* If the source or destination of image data is not to be stdio streams,
- * these types may need work.  You can replace them with some kind of
- * pointer or indicator that is useful to you, or just ignore 'em.
- * Note that the user interface and the various jrdxxx/jwrxxx modules
- * will also need work for non-stdio input/output.
- */
-
-typedef FILE * JFILEREF;	/* source or dest of JPEG-compressed data */
-
-typedef FILE * IFILEREF;	/* source or dest of non-JPEG image data */
-
-
-/* These defines are used in all function definitions and extern declarations.
- * You could modify them if you need to change function linkage conventions,
- * as is shown below for use with C++.  Another application would be to make
- * all functions global for use with code profilers that require it.
- * NOTE: the C++ test does the right thing if you are reading this include
- * file in a C++ application to link to JPEG code that's been compiled with a
- * regular C compiler.  I'm not sure it works if you try to compile the JPEG
- * code with C++.
- */
-
-#define METHODDEF static	/* a function called through method pointers */
-#define LOCAL	  static	/* a function used only in its module */
-#define GLOBAL			/* a function referenced thru EXTERNs */
-#ifdef __cplusplus
-#define EXTERN	  extern "C"	/* a reference to a GLOBAL function */
-#else
-#define EXTERN	  extern	/* a reference to a GLOBAL function */
-#endif
-
-
-/* Here is the pseudo-keyword for declaring pointers that must be "far"
- * on 80x86 machines.  Most of the specialized coding for 80x86 is handled
- * by just saying "FAR *" where such a pointer is needed.  In a few places
- * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
- */
-
-#ifdef NEED_FAR_POINTERS
-#define FAR  far
-#else
-#define FAR
-#endif
-
-
-
-/* The remaining declarations are not system-dependent, we hope. */
-
-
-/*
- * NOTE: if you have an ancient, strict-K&R C compiler, it may choke on the
- * similarly-named fields in Compress_info_struct and Decompress_info_struct.
- * If this happens, you can get around it by rearranging the two structs so
- * that the similarly-named fields appear first and in the same order in
- * each struct.  Since such compilers are now pretty rare, we haven't done
- * this in the portable code, preferring to maintain a logical ordering.
- */
-
-
-
-/* This macro is used to declare a "method", that is, a function pointer. */
-/* We want to supply prototype parameters if the compiler can cope. */
-/* Note that the arglist parameter must be parenthesized! */
-
-#ifdef PROTO
-#define METHOD(type,methodname,arglist)  type (*methodname) arglist
-#else
-#define METHOD(type,methodname,arglist)  type (*methodname) ()
-#endif
-
-/* Forward references to lists of method pointers */
-typedef struct External_methods_struct * external_methods_ptr;
-typedef struct Compress_methods_struct * compress_methods_ptr;
-typedef struct Decompress_methods_struct * decompress_methods_ptr;
-
-
-/* Data structures for images containing either samples or coefficients. */
-/* Note that the topmost (leftmost) index is always color component. */
-/* On 80x86 machines, the image arrays are too big for near pointers, */
-/* but the pointer arrays can fit in near memory. */
-
-typedef JSAMPLE FAR *JSAMPROW;	/* ptr to one image row of pixel samples. */
-typedef JSAMPROW *JSAMPARRAY;	/* ptr to some rows (a 2-D sample array) */
-typedef JSAMPARRAY *JSAMPIMAGE;	/* a 3-D sample array: top index is color */
-
-
-#define DCTSIZE		8	/* The basic DCT block is 8x8 samples */
-#define DCTSIZE2	64	/* DCTSIZE squared; # of elements in a block */
-
-typedef JCOEF JBLOCK[DCTSIZE2];	/* one block of coefficients */
-typedef JBLOCK FAR *JBLOCKROW;	/* pointer to one row of coefficient blocks */
-typedef JBLOCKROW *JBLOCKARRAY;		/* a 2-D array of coefficient blocks */
-typedef JBLOCKARRAY *JBLOCKIMAGE;	/* a 3-D array of coefficient blocks */
-
-typedef JCOEF FAR *JCOEFPTR;	/* useful in a couple of places */
-
-
-/* The input and output data of the DCT transform subroutines are of
- * the following type, which need not be the same as JCOEF.
- * For example, on a machine with fast floating point, it might make sense
- * to recode the DCT routines to use floating point; then DCTELEM would be
- * 'float' or 'double'.
- */
-
-typedef JCOEF DCTELEM;
-typedef DCTELEM DCTBLOCK[DCTSIZE2];
-
-
-/* Types for JPEG compression parameters and working tables. */
-
-
-typedef enum {			/* defines known color spaces */
-	CS_UNKNOWN,		/* error/unspecified */
-	CS_GRAYSCALE,		/* monochrome (only 1 component) */
-	CS_RGB,			/* red/green/blue */
-	CS_YCbCr,		/* Y/Cb/Cr (also known as YUV) */
-	CS_YIQ,			/* Y/I/Q */
-	CS_CMYK			/* C/M/Y/K */
-} COLOR_SPACE;
-
-
-typedef struct {		/* Basic info about one component */
-  /* These values are fixed over the whole image */
-  /* For compression, they must be supplied by the user interface; */
-  /* for decompression, they are read from the SOF marker. */
-	short component_id;	/* identifier for this component (0..255) */
-	short component_index;	/* its index in SOF or cinfo->comp_info[] */
-	short h_samp_factor;	/* horizontal sampling factor (1..4) */
-	short v_samp_factor;	/* vertical sampling factor (1..4) */
-	short quant_tbl_no;	/* quantization table selector (0..3) */
-  /* These values may vary between scans */
-  /* For compression, they must be supplied by the user interface; */
-  /* for decompression, they are read from the SOS marker. */
-	short dc_tbl_no;	/* DC entropy table selector (0..3) */
-	short ac_tbl_no;	/* AC entropy table selector (0..3) */
-  /* These values are computed during compression or decompression startup */
-	long true_comp_width;	/* component's image width in samples */
-	long true_comp_height;	/* component's image height in samples */
-	/* the above are the logical dimensions of the downsampled image */
-  /* These values are computed before starting a scan of the component */
-	short MCU_width;	/* number of blocks per MCU, horizontally */
-	short MCU_height;	/* number of blocks per MCU, vertically */
-	short MCU_blocks;	/* MCU_width * MCU_height */
-	long downsampled_width;	/* image width in samples, after expansion */
-	long downsampled_height; /* image height in samples, after expansion */
-	/* the above are the true_comp_xxx values rounded up to multiples of */
-	/* the MCU dimensions; these are the working dimensions of the array */
-	/* as it is passed through the DCT or IDCT step.  NOTE: these values */
-	/* differ depending on whether the component is interleaved or not!! */
-  /* This flag is used only for decompression.  In cases where some of the */
-  /* components will be ignored (eg grayscale output from YCbCr image), */
-  /* we can skip IDCT etc. computations for the unused components. */
-	boolean component_needed; /* do we need the value of this component? */
-} jpeg_component_info;
-
-
-/* DCT coefficient quantization tables.
- * For 8-bit precision, 'INT16' should be good enough for quantization values;
- * for more precision, we go for the full 16 bits.  'INT16' provides a useful
- * speedup on many machines (multiplication & division of JCOEFs by
- * quantization values is a significant chunk of the runtime).
- * Note: the values in a QUANT_TBL are always given in zigzag order.
- */
-#ifdef EIGHT_BIT_SAMPLES
-typedef INT16 QUANT_VAL;	/* element of a quantization table */
-#else
-typedef UINT16 QUANT_VAL;	/* element of a quantization table */
-#endif
-typedef QUANT_VAL QUANT_TBL[DCTSIZE2];	/* A quantization table */
-typedef QUANT_VAL * QUANT_TBL_PTR;	/* pointer to same */
-
-
-/* Huffman coding tables.
- */
-
-#define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */
-
-typedef struct {
-  /* These two fields directly represent the contents of a JPEG DHT marker */
-	UINT8 bits[17];		/* bits[k] = # of symbols with codes of */
-				/* length k bits; bits[0] is unused */
-	UINT8 huffval[256];	/* The symbols, in order of incr code length */
-  /* This field is used only during compression.  It's initialized FALSE when
-   * the table is created, and set TRUE when it's been output to the file.
-   */
-	boolean sent_table;	/* TRUE when table has been output */
-  /* The remaining fields are computed from the above to allow more efficient
-   * coding and decoding.  These fields should be considered private to the
-   * Huffman compression & decompression modules.  We use a union since only
-   * one set of fields is needed at a time.
-   */
-	union {
-	  struct {		/* encoding tables: */
-	    UINT16 ehufco[256];	/* code for each symbol */
-	    char ehufsi[256];	/* length of code for each symbol */
-	  } enc;
-	  struct {		/* decoding tables: */
-	    /* Basic tables: (element [0] of each array is unused) */
-	    INT32 mincode[17];	/* smallest code of length k */
-	    INT32 maxcode[18];	/* largest code of length k (-1 if none) */
-	    /* (maxcode[17] is a sentinel to ensure huff_DECODE terminates) */
-	    int valptr[17];	/* huffval[] index of 1st symbol of length k */
-	    /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
-	     * the input data stream.  If the next Huffman code is no more
-	     * than HUFF_LOOKAHEAD bits long, we can obtain its length and
-	     * the corresponding symbol directly from these tables.
-	     */
-	    int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
-	    UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
-	  } dec;
-	} priv;
-} HUFF_TBL;
-
-
-#define NUM_QUANT_TBLS      4	/* quantization tables are numbered 0..3 */
-#define NUM_HUFF_TBLS       4	/* Huffman tables are numbered 0..3 */
-#define NUM_ARITH_TBLS      16	/* arith-coding tables are numbered 0..15 */
-#define MAX_COMPS_IN_SCAN   4	/* JPEG limit on # of components in one scan */
-#define MAX_SAMP_FACTOR     4	/* JPEG limit on sampling factors */
-#define MAX_BLOCKS_IN_MCU   10	/* JPEG limit on # of blocks in an MCU */
-
-
-/* Working data for compression */
-
-struct Compress_info_struct {
-/*
- * All of these fields shall be established by the user interface before
- * calling jpeg_compress, or by the input_init or c_ui_method_selection
- * methods.
- * Most parameters can be set to reasonable defaults by j_c_defaults.
- * Note that the UI must supply the storage for the main methods struct,
- * though it sets only a few of the methods there.
- */
-	compress_methods_ptr methods; /* Points to list of methods to use */
-
-	external_methods_ptr emethods; /* Points to list of methods to use */
-
-	IFILEREF input_file;	/* tells input routines where to read image */
-	JFILEREF output_file;	/* tells output routines where to write JPEG */
-
-	long image_width;	/* input image width */
-	long image_height;	/* input image height */
-	short input_components;	/* # of color components in input image */
-
-	short data_precision;	/* bits of precision in image data */
-
-	COLOR_SPACE in_color_space; /* colorspace of input file */
-	COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
-
-	double input_gamma;	/* image gamma of input file */
-
-	boolean write_JFIF_header; /* should a JFIF marker be written? */
-	/* These three values are not used by the JPEG code, only copied */
-	/* into the JFIF APP0 marker.  density_unit can be 0 for unknown, */
-	/* 1 for dots/inch, or 2 for dots/cm.  Note that the pixel aspect */
-	/* ratio is defined by X_density/Y_density even when density_unit=0. */
-	UINT8 density_unit;	/* JFIF code for pixel size units */
-	UINT16 X_density;	/* Horizontal pixel density */
-	UINT16 Y_density;	/* Vertical pixel density */
-
-	char * comment_text;	/* Text for COM block, or NULL for no COM */
-	/* note: JPEG library will not free() the comment string, */
-	/* unless you allocate it via alloc_small(). */
-
-	short num_components;	/* # of color components in JPEG image */
-	jpeg_component_info * comp_info;
-	/* comp_info[i] describes component that appears i'th in SOF */
-
-	QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
-	/* ptrs to coefficient quantization tables, or NULL if not defined */
-
-	HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
-	HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
-	/* ptrs to Huffman coding tables, or NULL if not defined */
-
-	UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arithmetic-coding tables */
-	UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arithmetic-coding tables */
-	UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arithmetic-coding tables */
-
-	boolean arith_code;	/* TRUE=arithmetic coding, FALSE=Huffman */
-	boolean interleave;	/* TRUE=interleaved output, FALSE=not */
-	boolean optimize_coding; /* TRUE=optimize entropy encoding parms */
-	boolean CCIR601_sampling; /* TRUE=first samples are cosited */
-	int smoothing_factor;	/* 1..100, or 0 for no input smoothing */
-
-	/* The restart interval can be specified in absolute MCUs by setting
-	 * restart_interval, or in MCU rows by setting restart_in_rows
-	 * (in which case the correct restart_interval will be figured
-	 * for each scan).
-	 */
-	UINT16 restart_interval;/* MCUs per restart interval, or 0 for no restart */
-	int restart_in_rows;	/* if > 0, MCU rows per restart interval */
-
-/*
- * These fields are computed during jpeg_compress startup
- */
-	short max_h_samp_factor; /* largest h_samp_factor */
-	short max_v_samp_factor; /* largest v_samp_factor */
-
-/*
- * These fields may be useful for progress monitoring
- */
-
-	int total_passes;	/* number of passes expected */
-	int completed_passes;	/* number of passes completed so far */
-
-/*
- * These fields are valid during any one scan
- */
-	short comps_in_scan;	/* # of JPEG components output this time */
-	jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
-	/* *cur_comp_info[i] describes component that appears i'th in SOS */
-
-	long MCUs_per_row;	/* # of MCUs across the image */
-	long MCU_rows_in_scan;	/* # of MCU rows in the image */
-
-	short blocks_in_MCU;	/* # of DCT blocks per MCU */
-	short MCU_membership[MAX_BLOCKS_IN_MCU];
-	/* MCU_membership[i] is index in cur_comp_info of component owning */
-	/* i'th block in an MCU */
-
-	/* these fields are private data for the entropy encoder */
-	JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
-	JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
-	UINT16 restarts_to_go;	/* MCUs left in this restart interval */
-	short next_restart_num;	/* # of next RSTn marker (0..7) */
-};
-
-typedef struct Compress_info_struct * compress_info_ptr;
-
-
-/* Working data for decompression */
-
-struct Decompress_info_struct {
-/*
- * These fields shall be established by the user interface before
- * calling jpeg_decompress.
- * Most parameters can be set to reasonable defaults by j_d_defaults.
- * Note that the UI must supply the storage for the main methods struct,
- * though it sets only a few of the methods there.
- */
-	decompress_methods_ptr methods; /* Points to list of methods to use */
-
-	external_methods_ptr emethods; /* Points to list of methods to use */
-
-	JFILEREF input_file;	/* tells input routines where to read JPEG */
-	IFILEREF output_file;	/* tells output routines where to write image */
-
-	/* these can be set at d_ui_method_selection time: */
-
-	COLOR_SPACE out_color_space; /* colorspace of output */
-
-	double output_gamma;	/* image gamma wanted in output */
-
-	boolean quantize_colors; /* T if output is a colormapped format */
-	/* the following are ignored if not quantize_colors: */
-	boolean two_pass_quantize;	/* use two-pass color quantization? */
-	boolean use_dithering;		/* want color dithering? */
-	int desired_number_of_colors;	/* max number of colors to use */
-
-	boolean do_block_smoothing; /* T = apply cross-block smoothing */
-	boolean do_pixel_smoothing; /* T = apply post-upsampling smoothing */
-
-/*
- * These fields are used for efficient buffering of data between read_jpeg_data
- * and the entropy decoding object.  By using a shared buffer, we avoid copying
- * data and eliminate the need for an "unget" operation at the end of a scan.
- * The actual source of the data is known only to read_jpeg_data; see the
- * JGETC macro, below.
- * Note: the user interface is expected to allocate the input_buffer and
- * initialize bytes_in_buffer to 0.  Also, for JFIF/raw-JPEG input, the UI
- * actually supplies the read_jpeg_data method.  This is all handled by
- * j_d_defaults in a typical implementation.
- */
-	char * input_buffer;	/* start of buffer (private to input code) */
-	char * next_input_byte;	/* => next byte to read from buffer */
-	int bytes_in_buffer;	/* # of bytes remaining in buffer */
-
-/*
- * These fields are set by read_file_header or read_scan_header
- */
-	long image_width;	/* overall image width */
-	long image_height;	/* overall image height */
-
-	short data_precision;	/* bits of precision in image data */
-
-	COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
-
-        /* These three values are not used by the JPEG code, merely copied */
-	/* from the JFIF APP0 marker (if any). */
-	UINT8 density_unit;	/* JFIF code for pixel size units */
-	UINT16 X_density;	/* Horizontal pixel density */
-	UINT16 Y_density;	/* Vertical pixel density */
-
-	short num_components;	/* # of color components in JPEG image */
-	jpeg_component_info * comp_info;
-	/* comp_info[i] describes component that appears i'th in SOF */
-
-	QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
-	/* ptrs to coefficient quantization tables, or NULL if not defined */
-
-	HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
-	HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
-	/* ptrs to Huffman coding tables, or NULL if not defined */
-
-	UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
-	UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
-	UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
-
-	boolean arith_code;	/* TRUE=arithmetic coding, FALSE=Huffman */
-	boolean CCIR601_sampling; /* TRUE=first samples are cosited */
-
-	UINT16 restart_interval;/* MCUs per restart interval, or 0 for no restart */
-
-/*
- * These fields are computed during jpeg_decompress startup
- */
-	short max_h_samp_factor; /* largest h_samp_factor */
-	short max_v_samp_factor; /* largest v_samp_factor */
-
-	short color_out_comps;	/* # of color components output by color_convert */
-				/* (need not match num_components) */
-	short final_out_comps;	/* # of color components sent to put_pixel_rows */
-	/* (1 when quantizing colors, else same as color_out_comps) */
-
-	JSAMPLE * sample_range_limit; /* table for fast range-limiting */
-
-/*
- * When quantizing colors, the color quantizer leaves a pointer to the output
- * colormap in these fields.  The colormap is valid from the time put_color_map
- * is called (must be before any put_pixel_rows calls) until shutdown (more
- * specifically, until free_all is called to release memory).
- */
-	int actual_number_of_colors; /* actual number of entries */
-	JSAMPARRAY colormap;	/* NULL if not valid */
-	/* map has color_out_comps rows * actual_number_of_colors columns */
-
-/*
- * These fields may be useful for progress monitoring
- */
-
-	int total_passes;	/* number of passes expected */
-	int completed_passes;	/* number of passes completed so far */
-
-/*
- * These fields are valid during any one scan
- */
-	short comps_in_scan;	/* # of JPEG components input this time */
-	jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
-	/* *cur_comp_info[i] describes component that appears i'th in SOS */
-
-	long MCUs_per_row;	/* # of MCUs across the image */
-	long MCU_rows_in_scan;	/* # of MCU rows in the image */
-
-	short blocks_in_MCU;	/* # of DCT blocks per MCU */
-	short MCU_membership[MAX_BLOCKS_IN_MCU];
-	/* MCU_membership[i] is index in cur_comp_info of component owning */
-	/* i'th block in an MCU */
-
-	/* these fields are private data for the entropy encoder */
-	JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
-	JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
-	UINT16 restarts_to_go;	/* MCUs left in this restart interval */
-	short next_restart_num;	/* # of next RSTn marker (0..7) */
-};
-
-typedef struct Decompress_info_struct * decompress_info_ptr;
-
-
-/* Macros for reading data from the decompression input buffer */
-
-#ifdef CHAR_IS_UNSIGNED
-#define JGETC(cinfo)	( --(cinfo)->bytes_in_buffer < 0 ? \
-			 (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
-			 (int) (*(cinfo)->next_input_byte++) )
-#else
-#define JGETC(cinfo)	( --(cinfo)->bytes_in_buffer < 0 ? \
-			 (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
-			 (int) (*(cinfo)->next_input_byte++) & 0xFF )
-#endif
-
-#define JUNGETC(ch,cinfo)  ((cinfo)->bytes_in_buffer++, \
-			    *(--((cinfo)->next_input_byte)) = (char) (ch))
-
-#define MIN_UNGET	4	/* may always do at least 4 JUNGETCs */
-
-
-/* A virtual image has a control block whose contents are private to the
- * memory manager module (and may differ between managers).  The rest of the
- * code only refers to virtual images by these pointer types, and never
- * dereferences the pointer.
- */
-
-typedef struct big_sarray_control * big_sarray_ptr;
-typedef struct big_barray_control * big_barray_ptr;
-
-/* Although a real ANSI C compiler can deal perfectly well with pointers to
- * unspecified structures (see "incomplete types" in the spec), a few pre-ANSI
- * and pseudo-ANSI compilers get confused.  To keep one of these bozos happy,
- * add -DINCOMPLETE_TYPES_BROKEN to CFLAGS in your Makefile.  Then we will
- * pseudo-define the structs as containing a single "dummy" field.
- * The memory managers #define AM_MEMORY_MANAGER before including this file,
- * so that they can make their own definitions of the structs.
- */
-
-#ifdef INCOMPLETE_TYPES_BROKEN
-#ifndef AM_MEMORY_MANAGER
-struct big_sarray_control { long dummy; };
-struct big_barray_control { long dummy; };
-#endif
-#endif
-
-
-/* Method types that need typedefs */
-
-typedef METHOD(void, MCU_output_method_ptr, (compress_info_ptr cinfo,
-					     JBLOCK *MCU_data));
-typedef METHOD(void, MCU_output_caller_ptr, (compress_info_ptr cinfo,
-					     MCU_output_method_ptr output_method));
-typedef METHOD(void, downsample_ptr, (compress_info_ptr cinfo,
-				      int which_component,
-				      long input_cols, int input_rows,
-				      long output_cols, int output_rows,
-				      JSAMPARRAY above,
-				      JSAMPARRAY input_data,
-				      JSAMPARRAY below,
-				      JSAMPARRAY output_data));
-typedef METHOD(void, upsample_ptr, (decompress_info_ptr cinfo,
-				    int which_component,
-				    long input_cols, int input_rows,
-				    long output_cols, int output_rows,
-				    JSAMPARRAY above,
-				    JSAMPARRAY input_data,
-				    JSAMPARRAY below,
-				    JSAMPARRAY output_data));
-typedef METHOD(void, quantize_method_ptr, (decompress_info_ptr cinfo,
-					   int num_rows,
-					   JSAMPIMAGE input_data,
-					   JSAMPARRAY output_workspace));
-typedef METHOD(void, quantize_caller_ptr, (decompress_info_ptr cinfo,
-					   quantize_method_ptr quantize_method));
-
-
-/* These structs contain function pointers for the various JPEG methods. */
-
-/* Routines to be provided by the surrounding application, rather than the
- * portable JPEG code proper.  These are the same for compression and
- * decompression.
- */
-
-struct External_methods_struct {
-	/* User interface: error exit and trace message routines */
-	/* NOTE: the string msgtext parameters will eventually be replaced
-	 * by an enumerated-type code so that non-English error messages
-	 * can be substituted easily.  This will not be done until all the
-	 * code is in place, so that we know what messages are needed.
-	 */
-	METHOD(void, error_exit, (const char *msgtext));
-	METHOD(void, trace_message, (const char *msgtext));
-
-	/* Working data for error/trace facility */
-	/* See macros below for the usage of these variables */
-	int trace_level;	/* level of detail of tracing messages */
-	/* Use level 0 for important warning messages (nonfatal errors) */
-	/* Use levels 1, 2, 3 for successively more detailed trace options */
-
-	/* For recoverable corrupt-data errors, we emit a warning message and
-	 * keep going.  A surrounding application can check for bad data by
-	 * seeing if num_warnings is nonzero at the end of processing.
-	 */
-	long num_warnings;	/* number of corrupt-data warnings */
-	int first_warning_level; /* trace level for first warning */
-	int more_warning_level;	/* trace level for subsequent warnings */
-
-	int message_parm[8];	/* store numeric parms for messages here */
-
-	/* Memory management */
-	/* NB: alloc routines never return NULL. They exit to */
-	/* error_exit if not successful. */
-	METHOD(void *, alloc_small, (size_t sizeofobject));
-	METHOD(void, free_small, (void *ptr));
-	METHOD(void FAR *, alloc_medium, (size_t sizeofobject));
-	METHOD(void, free_medium, (void FAR *ptr));
-	METHOD(JSAMPARRAY, alloc_small_sarray, (long samplesperrow,
-						long numrows));
-	METHOD(void, free_small_sarray, (JSAMPARRAY ptr));
-	METHOD(JBLOCKARRAY, alloc_small_barray, (long blocksperrow,
-						 long numrows));
-	METHOD(void, free_small_barray, (JBLOCKARRAY ptr));
-	METHOD(big_sarray_ptr, request_big_sarray, (long samplesperrow,
-						    long numrows,
-						    long unitheight));
-	METHOD(big_barray_ptr, request_big_barray, (long blocksperrow,
-						    long numrows,
-						    long unitheight));
-	METHOD(void, alloc_big_arrays, (long extra_small_samples,
-					long extra_small_blocks,
-					long extra_medium_space));
-	METHOD(JSAMPARRAY, access_big_sarray, (big_sarray_ptr ptr,
-					       long start_row,
-					       boolean writable));
-	METHOD(JBLOCKARRAY, access_big_barray, (big_barray_ptr ptr,
-						long start_row,
-						boolean writable));
-	METHOD(void, free_big_sarray, (big_sarray_ptr ptr));
-	METHOD(void, free_big_barray, (big_barray_ptr ptr));
-	METHOD(void, free_all, (void));
-
-	long max_memory_to_use;	/* maximum amount of memory to use */
-};
-
-/* Macros to simplify using the error and trace message stuff */
-/* The first parameter is generally cinfo->emethods */
-
-/* Fatal errors (print message and exit) */
-#define ERREXIT(emeth,msg)		((*(emeth)->error_exit) (msg))
-#define ERREXIT1(emeth,msg,p1)		((emeth)->message_parm[0] = (p1), \
-					 (*(emeth)->error_exit) (msg))
-#define ERREXIT2(emeth,msg,p1,p2)	((emeth)->message_parm[0] = (p1), \
-					 (emeth)->message_parm[1] = (p2), \
-					 (*(emeth)->error_exit) (msg))
-#define ERREXIT3(emeth,msg,p1,p2,p3)	((emeth)->message_parm[0] = (p1), \
-					 (emeth)->message_parm[1] = (p2), \
-					 (emeth)->message_parm[2] = (p3), \
-					 (*(emeth)->error_exit) (msg))
-#define ERREXIT4(emeth,msg,p1,p2,p3,p4) ((emeth)->message_parm[0] = (p1), \
-					 (emeth)->message_parm[1] = (p2), \
-					 (emeth)->message_parm[2] = (p3), \
-					 (emeth)->message_parm[3] = (p4), \
-					 (*(emeth)->error_exit) (msg))
-
-#define MAKESTMT(stuff)		do { stuff } while (0)
-
-/* Nonfatal errors (we'll keep going, but the data is probably corrupt) */
-/* Note that warning count is incremented as a side-effect! */
-#define WARNMS(emeth,msg)    \
-  MAKESTMT( if ((emeth)->trace_level >= ((emeth)->num_warnings++ ? \
-		(emeth)->more_warning_level : (emeth)->first_warning_level)){ \
-		(*(emeth)->trace_message) (msg); } )
-#define WARNMS1(emeth,msg,p1)    \
-  MAKESTMT( if ((emeth)->trace_level >= ((emeth)->num_warnings++ ? \
-		(emeth)->more_warning_level : (emeth)->first_warning_level)){ \
-		(emeth)->message_parm[0] = (p1); \
-		(*(emeth)->trace_message) (msg); } )
-#define WARNMS2(emeth,msg,p1,p2)    \
-  MAKESTMT( if ((emeth)->trace_level >= ((emeth)->num_warnings++ ? \
-		(emeth)->more_warning_level : (emeth)->first_warning_level)){ \
-		(emeth)->message_parm[0] = (p1); \
-		(emeth)->message_parm[1] = (p2); \
-		(*(emeth)->trace_message) (msg); } )
-
-/* Informational/debugging messages */
-#define TRACEMS(emeth,lvl,msg)    \
-  MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
-		(*(emeth)->trace_message) (msg); } )
-#define TRACEMS1(emeth,lvl,msg,p1)    \
-  MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
-		(emeth)->message_parm[0] = (p1); \
-		(*(emeth)->trace_message) (msg); } )
-#define TRACEMS2(emeth,lvl,msg,p1,p2)    \
-  MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
-		(emeth)->message_parm[0] = (p1); \
-		(emeth)->message_parm[1] = (p2); \
-		(*(emeth)->trace_message) (msg); } )
-#define TRACEMS3(emeth,lvl,msg,p1,p2,p3)    \
-  MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
-		int * _mp = (emeth)->message_parm; \
-		*_mp++ = (p1); *_mp++ = (p2); *_mp = (p3); \
-		(*(emeth)->trace_message) (msg); } )
-#define TRACEMS4(emeth,lvl,msg,p1,p2,p3,p4)    \
-  MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
-		int * _mp = (emeth)->message_parm; \
-		*_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp = (p4); \
-		(*(emeth)->trace_message) (msg); } )
-#define TRACEMS8(emeth,lvl,msg,p1,p2,p3,p4,p5,p6,p7,p8)    \
-  MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
-		int * _mp = (emeth)->message_parm; \
-		*_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp++ = (p4); \
-		*_mp++ = (p5); *_mp++ = (p6); *_mp++ = (p7); *_mp = (p8); \
-		(*(emeth)->trace_message) (msg); } )
-
-
-/* Methods used during JPEG compression. */
-
-struct Compress_methods_struct {
-	/* Hook for user interface to get control after input_init */
-	METHOD(void, c_ui_method_selection, (compress_info_ptr cinfo));
-	/* Hook for user interface to do progress monitoring */
-	METHOD(void, progress_monitor, (compress_info_ptr cinfo,
-					long loopcounter, long looplimit));
-	/* Input image reading & conversion to standard form */
-	METHOD(void, input_init, (compress_info_ptr cinfo));
-	METHOD(void, get_input_row, (compress_info_ptr cinfo,
-				     JSAMPARRAY pixel_row));
-	METHOD(void, input_term, (compress_info_ptr cinfo));
-	/* Color space and gamma conversion */
-	METHOD(void, colorin_init, (compress_info_ptr cinfo));
-	METHOD(void, get_sample_rows, (compress_info_ptr cinfo,
-				       int rows_to_read,
-				       JSAMPIMAGE image_data));
-	METHOD(void, colorin_term, (compress_info_ptr cinfo));
-	/* Expand picture data at edges */
-	METHOD(void, edge_expand, (compress_info_ptr cinfo,
-				   long input_cols, int input_rows,
-				   long output_cols, int output_rows,
-				   JSAMPIMAGE image_data));
-	/* Downsample pixel values of a single component */
-	/* There can be a different downsample method for each component */
-	METHOD(void, downsample_init, (compress_info_ptr cinfo));
-	downsample_ptr downsample[MAX_COMPS_IN_SCAN];
-	METHOD(void, downsample_term, (compress_info_ptr cinfo));
-	/* Extract samples in MCU order, process & hand off to output_method */
-	/* The input is always exactly N MCU rows worth of data */
-	METHOD(void, extract_init, (compress_info_ptr cinfo));
-	METHOD(void, extract_MCUs, (compress_info_ptr cinfo,
-				    JSAMPIMAGE image_data,
-				    int num_mcu_rows,
-				    MCU_output_method_ptr output_method));
-	METHOD(void, extract_term, (compress_info_ptr cinfo));
-	/* Entropy encoding parameter optimization */
-	METHOD(void, entropy_optimize, (compress_info_ptr cinfo,
-					MCU_output_caller_ptr source_method));
-	/* Entropy encoding */
-	METHOD(void, entropy_encode_init, (compress_info_ptr cinfo));
-	METHOD(void, entropy_encode, (compress_info_ptr cinfo,
-				      JBLOCK *MCU_data));
-	METHOD(void, entropy_encode_term, (compress_info_ptr cinfo));
-	/* JPEG file header construction */
-	METHOD(void, write_file_header, (compress_info_ptr cinfo));
-	METHOD(void, write_scan_header, (compress_info_ptr cinfo));
-	METHOD(void, write_jpeg_data, (compress_info_ptr cinfo,
-				       char *dataptr,
-				       int datacount));
-	METHOD(void, write_scan_trailer, (compress_info_ptr cinfo));
-	METHOD(void, write_file_trailer, (compress_info_ptr cinfo));
-	/* Pipeline control */
-	METHOD(void, c_pipeline_controller, (compress_info_ptr cinfo));
-	METHOD(void, entropy_output, (compress_info_ptr cinfo,
-				      char *dataptr,
-				      int datacount));
-	/* Overall control */
-	METHOD(void, c_per_scan_method_selection, (compress_info_ptr cinfo));
-};
-
-/* Methods used during JPEG decompression. */
-
-struct Decompress_methods_struct {
-	/* Hook for user interface to get control after reading file header */
-	METHOD(void, d_ui_method_selection, (decompress_info_ptr cinfo));
-	/* Hook for user interface to process comment blocks */
-	METHOD(void, process_comment, (decompress_info_ptr cinfo,
-				       long comment_length));
-	/* Hook for user interface to do progress monitoring */
-	METHOD(void, progress_monitor, (decompress_info_ptr cinfo,
-					long loopcounter, long looplimit));
-	/* JPEG file scanning */
-	METHOD(void, read_file_header, (decompress_info_ptr cinfo));
-	METHOD(boolean, read_scan_header, (decompress_info_ptr cinfo));
-	METHOD(int, read_jpeg_data, (decompress_info_ptr cinfo));
-	METHOD(void, resync_to_restart, (decompress_info_ptr cinfo,
-					 int marker));
-	METHOD(void, read_scan_trailer, (decompress_info_ptr cinfo));
-	METHOD(void, read_file_trailer, (decompress_info_ptr cinfo));
-	/* Entropy decoding */
-	METHOD(void, entropy_decode_init, (decompress_info_ptr cinfo));
-	METHOD(void, entropy_decode, (decompress_info_ptr cinfo,
-				      JBLOCKROW *MCU_data));
-	METHOD(void, entropy_decode_term, (decompress_info_ptr cinfo));
-	/* MCU disassembly: fetch MCUs from entropy_decode, build coef array */
-	/* The reverse_DCT step is in the same module for symmetry reasons */
-	METHOD(void, disassemble_init, (decompress_info_ptr cinfo));
-	METHOD(void, disassemble_MCU, (decompress_info_ptr cinfo,
-				       JBLOCKIMAGE image_data));
-	METHOD(void, reverse_DCT, (decompress_info_ptr cinfo,
-				   JBLOCKIMAGE coeff_data,
-				   JSAMPIMAGE output_data, int start_row));
-	METHOD(void, disassemble_term, (decompress_info_ptr cinfo));
-	/* Cross-block smoothing */
-	METHOD(void, smooth_coefficients, (decompress_info_ptr cinfo,
-					   jpeg_component_info *compptr,
-					   JBLOCKROW above,
-					   JBLOCKROW currow,
-					   JBLOCKROW below,
-					   JBLOCKROW output));
-	/* Upsample pixel values of a single component */
-	/* There can be a different upsample method for each component */
-	METHOD(void, upsample_init, (decompress_info_ptr cinfo));
-	upsample_ptr upsample[MAX_COMPS_IN_SCAN];
-	METHOD(void, upsample_term, (decompress_info_ptr cinfo));
-	/* Color space and gamma conversion */
-	METHOD(void, colorout_init, (decompress_info_ptr cinfo));
-	METHOD(void, color_convert, (decompress_info_ptr cinfo,
-				     int num_rows, long num_cols,
-				     JSAMPIMAGE input_data,
-				     JSAMPIMAGE output_data));
-	METHOD(void, colorout_term, (decompress_info_ptr cinfo));
-	/* Color quantization */
-	METHOD(void, color_quant_init, (decompress_info_ptr cinfo));
-	METHOD(void, color_quantize, (decompress_info_ptr cinfo,
-				      int num_rows,
-				      JSAMPIMAGE input_data,
-				      JSAMPARRAY output_data));
-	METHOD(void, color_quant_prescan, (decompress_info_ptr cinfo,
-					   int num_rows,
-					   JSAMPIMAGE image_data,
-					   JSAMPARRAY workspace));
-	METHOD(void, color_quant_doit, (decompress_info_ptr cinfo,
-					quantize_caller_ptr source_method));
-	METHOD(void, color_quant_term, (decompress_info_ptr cinfo));
-	/* Output image writing */
-	METHOD(void, output_init, (decompress_info_ptr cinfo));
-	METHOD(void, put_color_map, (decompress_info_ptr cinfo,
-				     int num_colors, JSAMPARRAY colormap));
-	METHOD(void, put_pixel_rows, (decompress_info_ptr cinfo,
-				      int num_rows,
-				      JSAMPIMAGE pixel_data));
-	METHOD(void, output_term, (decompress_info_ptr cinfo));
-	/* Pipeline control */
-	METHOD(void, d_pipeline_controller, (decompress_info_ptr cinfo));
-	/* Overall control */
-	METHOD(void, d_per_scan_method_selection, (decompress_info_ptr cinfo));
-};
-
-
-/* External declarations for routines that aren't called via method ptrs. */
-/* Note: use "j" as first char of names to minimize namespace pollution. */
-/* The PP macro hides prototype parameters from compilers that can't cope. */
-
-#ifdef PROTO
-#define PP(arglist)	arglist
-#else
-#define PP(arglist)	()
-#endif
-
-
-/* main entry for compression */
-EXTERN void jpeg_compress PP((compress_info_ptr cinfo));
-
-/* default parameter setup for compression */
-EXTERN void j_c_defaults PP((compress_info_ptr cinfo, int quality,
-			     boolean force_baseline));
-EXTERN void j_monochrome_default PP((compress_info_ptr cinfo));
-EXTERN void j_set_quality PP((compress_info_ptr cinfo, int quality,
-			      boolean force_baseline));
-/* advanced compression parameter setup aids */
-EXTERN void j_add_quant_table PP((compress_info_ptr cinfo, int which_tbl,
-				  const QUANT_VAL *basic_table,
-				  int scale_factor, boolean force_baseline));
-EXTERN int j_quality_scaling PP((int quality));
-
-/* main entry for decompression */
-EXTERN void jpeg_decompress PP((decompress_info_ptr cinfo));
-
-/* default parameter setup for decompression */
-EXTERN void j_d_defaults PP((decompress_info_ptr cinfo,
-			     boolean standard_buffering));
-
-/* forward DCT */
-EXTERN void j_fwd_dct PP((DCTBLOCK data));
-/* inverse DCT */
-EXTERN void j_rev_dct PP((DCTBLOCK data));
-
-/* utility routines in jutils.c */
-EXTERN long jround_up PP((long a, long b));
-EXTERN void jcopy_sample_rows PP((JSAMPARRAY input_array, int source_row,
-				  JSAMPARRAY output_array, int dest_row,
-				  int num_rows, long num_cols));
-EXTERN void jcopy_block_row PP((JBLOCKROW input_row, JBLOCKROW output_row,
-				long num_blocks));
-EXTERN void jzero_far PP((void FAR * target, size_t bytestozero));
-
-/* method selection routines for compression modules */
-EXTERN void jselcpipeline PP((compress_info_ptr cinfo)); /* jcpipe.c */
-EXTERN void jselchuffman PP((compress_info_ptr cinfo)); /* jchuff.c */
-EXTERN void jselcarithmetic PP((compress_info_ptr cinfo)); /* jcarith.c */
-EXTERN void jselexpand PP((compress_info_ptr cinfo)); /* jcexpand.c */
-EXTERN void jseldownsample PP((compress_info_ptr cinfo)); /* jcsample.c */
-EXTERN void jselcmcu PP((compress_info_ptr cinfo)); /* jcmcu.c */
-EXTERN void jselccolor PP((compress_info_ptr cinfo));	/* jccolor.c */
-/* The user interface should call one of these to select input format: */
-EXTERN void jselrgif PP((compress_info_ptr cinfo)); /* jrdgif.c */
-EXTERN void jselrppm PP((compress_info_ptr cinfo)); /* jrdppm.c */
-EXTERN void jselrrle PP((compress_info_ptr cinfo)); /* jrdrle.c */
-EXTERN void jselrtarga PP((compress_info_ptr cinfo)); /* jrdtarga.c */
-/* and one of these to select output header format: */
-EXTERN void jselwjfif PP((compress_info_ptr cinfo)); /* jwrjfif.c */
-
-/* method selection routines for decompression modules */
-EXTERN void jseldpipeline PP((decompress_info_ptr cinfo)); /* jdpipe.c */
-EXTERN void jseldhuffman PP((decompress_info_ptr cinfo)); /* jdhuff.c */
-EXTERN void jseldarithmetic PP((decompress_info_ptr cinfo)); /* jdarith.c */
-EXTERN void jseldmcu PP((decompress_info_ptr cinfo)); /* jdmcu.c */
-EXTERN void jselbsmooth PP((decompress_info_ptr cinfo)); /* jbsmooth.c */
-EXTERN void jselupsample PP((decompress_info_ptr cinfo)); /* jdsample.c */
-EXTERN void jseldcolor PP((decompress_info_ptr cinfo));	/* jdcolor.c */
-EXTERN void jsel1quantize PP((decompress_info_ptr cinfo)); /* jquant1.c */
-EXTERN void jsel2quantize PP((decompress_info_ptr cinfo)); /* jquant2.c */
-/* The user interface should call one of these to select input format: */
-EXTERN void jselrjfif PP((decompress_info_ptr cinfo)); /* jrdjfif.c */
-/* and one of these to select output image format: */
-EXTERN void jselwgif PP((decompress_info_ptr cinfo)); /* jwrgif.c */
-EXTERN void jselwppm PP((decompress_info_ptr cinfo)); /* jwrppm.c */
-EXTERN void jselwrle PP((decompress_info_ptr cinfo)); /* jwrrle.c */
-EXTERN void jselwtarga PP((decompress_info_ptr cinfo)); /* jwrtarga.c */
-
-/* method selection routines for system-dependent modules */
-EXTERN void jselerror PP((external_methods_ptr emethods)); /* jerror.c */
-EXTERN void jselmemmgr PP((external_methods_ptr emethods)); /* jmemmgr.c */
-
-
-/* We assume that right shift corresponds to signed division by 2 with
- * rounding towards minus infinity.  This is correct for typical "arithmetic
- * shift" instructions that shift in copies of the sign bit.  But some
- * C compilers implement >> with an unsigned shift.  For these machines you
- * must define RIGHT_SHIFT_IS_UNSIGNED.
- * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
- * It is only applied with constant shift counts.  SHIFT_TEMPS must be
- * included in the variables of any routine using RIGHT_SHIFT.
- */
-
-#ifdef RIGHT_SHIFT_IS_UNSIGNED
-#define SHIFT_TEMPS	INT32 shift_temp;
-#define RIGHT_SHIFT(x,shft)  \
-	((shift_temp = (x)) < 0 ? \
-	 (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
-	 (shift_temp >> (shft)))
-#else
-#define SHIFT_TEMPS
-#define RIGHT_SHIFT(x,shft)	((x) >> (shft))
-#endif
-
-
-/* Miscellaneous useful macros */
-
-#undef MAX
-#define MAX(a,b)	((a) > (b) ? (a) : (b))
-#undef MIN
-#define MIN(a,b)	((a) < (b) ? (a) : (b))
-
-
-#define RST0	0xD0		/* RST0 marker code */
diff --git a/jpegint.h b/jpegint.h
new file mode 100644
index 0000000..5ae3a9f
--- /dev/null
+++ b/jpegint.h
@@ -0,0 +1,361 @@
+/*
+ * jpegint.h
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file provides common declarations for the various JPEG modules.
+ * These declarations are considered internal to the JPEG library; most
+ * applications using the library shouldn't need to include this file.
+ */
+
+
+/* Declarations for both compression & decompression */
+
+typedef enum {			/* Operating modes for buffer controllers */
+	JBUF_PASS_THRU,		/* Plain stripwise operation */
+	JBUF_CRANK_SOURCE,	/* Run source subobject, no output expected */
+	/* Remaining modes require a full-image buffer to have been created */
+	JBUF_SAVE_SOURCE,	/* Run source subobject only, save output */
+	JBUF_CRANK_DEST,	/* Run dest subobject only, using saved data */
+	JBUF_SAVE_AND_PASS	/* Run both subobjects, save output */
+} J_BUF_MODE;
+
+/* Values of global_state field */
+#define CSTATE_START	100	/* after create_compress */
+#define CSTATE_SCANNING	101	/* start_compress done, write_scanlines OK */
+#define CSTATE_RAW_OK	102	/* start_compress done, write_raw_data OK */
+#define DSTATE_START	200	/* after create_decompress */
+#define DSTATE_INHEADER	201	/* read_header initialized but not done */
+#define DSTATE_READY	202	/* read_header done, found image */
+#define DSTATE_SCANNING	203	/* start_decompress done, read_scanlines OK */
+#define DSTATE_RAW_OK	204	/* start_decompress done, read_raw_data OK */
+#define DSTATE_STOPPING	205	/* done reading data, looking for EOI */
+
+
+/* Declarations for compression modules */
+
+/* Master control module */
+struct jpeg_comp_master {
+  JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
+  JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
+  JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
+
+  /* State variables made visible to other modules */
+  boolean call_pass_startup;	/* True if pass_startup must be called */
+  boolean is_last_pass;		/* True during last pass */
+};
+
+/* Main buffer control (downsampled-data buffer) */
+struct jpeg_c_main_controller {
+  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
+  JMETHOD(void, process_data, (j_compress_ptr cinfo,
+			       JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+			       JDIMENSION in_rows_avail));
+};
+
+/* Compression preprocessing (downsampling input buffer control) */
+struct jpeg_c_prep_controller {
+  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
+  JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
+				   JSAMPARRAY input_buf,
+				   JDIMENSION *in_row_ctr,
+				   JDIMENSION in_rows_avail,
+				   JSAMPIMAGE output_buf,
+				   JDIMENSION *out_row_group_ctr,
+				   JDIMENSION out_row_groups_avail));
+};
+
+/* Coefficient buffer control */
+struct jpeg_c_coef_controller {
+  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
+  JMETHOD(void, compress_data, (j_compress_ptr cinfo,
+				JSAMPIMAGE input_buf,
+				JDIMENSION *in_mcu_ctr));
+};
+
+/* Colorspace conversion */
+struct jpeg_color_converter {
+  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
+  JMETHOD(void, color_convert, (j_compress_ptr cinfo,
+				JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+				JDIMENSION output_row, int num_rows));
+};
+
+/* Downsampling */
+struct jpeg_downsampler {
+  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
+  JMETHOD(void, downsample, (j_compress_ptr cinfo,
+			     JSAMPIMAGE input_buf, JDIMENSION in_row_index,
+			     JSAMPIMAGE output_buf,
+			     JDIMENSION out_row_group_index));
+
+  boolean need_context_rows;	/* TRUE if need rows above & below */
+};
+
+/* Forward DCT (also controls coefficient quantization) */
+struct jpeg_forward_dct {
+  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
+  /* perhaps this should be an array??? */
+  JMETHOD(void, forward_DCT, (j_compress_ptr cinfo,
+			      jpeg_component_info * compptr,
+			      JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+			      JDIMENSION start_row, JDIMENSION start_col,
+			      JDIMENSION num_blocks));
+};
+
+/* Entropy encoding */
+struct jpeg_entropy_encoder {
+  JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
+  JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
+  JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
+};
+
+/* Marker writing */
+struct jpeg_marker_writer {
+  /* write_any_marker is exported for use by applications */
+  /* Probably only COM and APPn markers should be written */
+  JMETHOD(void, write_any_marker, (j_compress_ptr cinfo, int marker,
+				   const JOCTET *dataptr, unsigned int datalen));
+  JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
+  JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
+  JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
+  JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
+  JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
+};
+
+
+/* Declarations for decompression modules */
+
+/* Master control module */
+struct jpeg_decomp_master {
+  JMETHOD(void, prepare_for_pass, (j_decompress_ptr cinfo));
+  JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
+
+  /* State variables made visible to other modules */
+  boolean is_last_pass;		/* True during last pass */
+  boolean eoi_processed;	/* True if EOI marker already read */
+};
+
+/* Main buffer control (downsampled-data buffer) */
+struct jpeg_d_main_controller {
+  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
+  JMETHOD(void, process_data, (j_decompress_ptr cinfo,
+			       JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+			       JDIMENSION out_rows_avail));
+  /* During input-only passes, output_buf and out_rows_avail are ignored.
+   * out_row_ctr is incremented towards the limit num_chunks.
+   */
+  JDIMENSION num_chunks;	/* number of chunks to be processed in pass */
+};
+
+/* Coefficient buffer control */
+struct jpeg_d_coef_controller {
+  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
+  JMETHOD(boolean, decompress_data, (j_decompress_ptr cinfo,
+				     JSAMPIMAGE output_buf));
+};
+
+/* Decompression postprocessing (color quantization buffer control) */
+struct jpeg_d_post_controller {
+  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
+  JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
+				    JSAMPIMAGE input_buf,
+				    JDIMENSION *in_row_group_ctr,
+				    JDIMENSION in_row_groups_avail,
+				    JSAMPARRAY output_buf,
+				    JDIMENSION *out_row_ctr,
+				    JDIMENSION out_rows_avail));
+};
+
+/* Marker reading & parsing */
+struct jpeg_marker_reader {
+  JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
+  /* Read markers until SOS or EOI.
+   * Returns same codes as are defined for jpeg_read_header,
+   * but HEADER_OK and HEADER_TABLES_ONLY merely indicate which marker type
+   * stopped the scan --- further validation is needed to declare file OK.
+   */
+  JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
+  /* Read a restart marker --- exported for use by entropy decoder only */
+  jpeg_marker_parser_method read_restart_marker;
+  /* Application-overridable marker processing methods */
+  jpeg_marker_parser_method process_COM;
+  jpeg_marker_parser_method process_APPn[16];
+
+  /* State of marker reader --- nominally internal, but applications
+   * supplying COM or APPn handlers might like to know the state.
+   */
+  boolean saw_SOI;		/* found SOI? */
+  boolean saw_SOF;		/* found SOF? */
+  int next_restart_num;		/* next restart number expected (0-7) */
+  unsigned int discarded_bytes;	/* # of bytes skipped looking for a marker */
+};
+
+/* Entropy decoding */
+struct jpeg_entropy_decoder {
+  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+  JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
+				JBLOCKROW *MCU_data));
+};
+
+/* Inverse DCT (also performs dequantization) */
+typedef JMETHOD(void, inverse_DCT_method_ptr,
+		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
+		 JCOEFPTR coef_block,
+		 JSAMPARRAY output_buf, JDIMENSION output_col));
+
+struct jpeg_inverse_dct {
+  JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
+  JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
+  /* It is useful to allow each component to have a separate IDCT method. */
+  inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
+};
+
+/* Upsampling (note that upsampler must also call color converter) */
+struct jpeg_upsampler {
+  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+  JMETHOD(void, upsample, (j_decompress_ptr cinfo,
+			   JSAMPIMAGE input_buf,
+			   JDIMENSION *in_row_group_ctr,
+			   JDIMENSION in_row_groups_avail,
+			   JSAMPARRAY output_buf,
+			   JDIMENSION *out_row_ctr,
+			   JDIMENSION out_rows_avail));
+
+  boolean need_context_rows;	/* TRUE if need rows above & below */
+};
+
+/* Colorspace conversion */
+struct jpeg_color_deconverter {
+  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+  JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
+				JSAMPIMAGE input_buf, JDIMENSION input_row,
+				JSAMPARRAY output_buf, int num_rows));
+};
+
+/* Color quantization or color precision reduction */
+struct jpeg_color_quantizer {
+  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
+  JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
+				 JSAMPARRAY input_buf, JSAMPARRAY output_buf,
+				 int num_rows));
+  JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
+};
+
+
+/* Miscellaneous useful macros */
+
+#undef MAX
+#define MAX(a,b)	((a) > (b) ? (a) : (b))
+#undef MIN
+#define MIN(a,b)	((a) < (b) ? (a) : (b))
+
+
+/* We assume that right shift corresponds to signed division by 2 with
+ * rounding towards minus infinity.  This is correct for typical "arithmetic
+ * shift" instructions that shift in copies of the sign bit.  But some
+ * C compilers implement >> with an unsigned shift.  For these machines you
+ * must define RIGHT_SHIFT_IS_UNSIGNED.
+ * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
+ * It is only applied with constant shift counts.  SHIFT_TEMPS must be
+ * included in the variables of any routine using RIGHT_SHIFT.
+ */
+
+#ifdef RIGHT_SHIFT_IS_UNSIGNED
+#define SHIFT_TEMPS	INT32 shift_temp;
+#define RIGHT_SHIFT(x,shft)  \
+	((shift_temp = (x)) < 0 ? \
+	 (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
+	 (shift_temp >> (shft)))
+#else
+#define SHIFT_TEMPS
+#define RIGHT_SHIFT(x,shft)	((x) >> (shft))
+#endif
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jinit_master_compress	jICMaster
+#define jinit_c_main_controller	jICMainC
+#define jinit_c_prep_controller	jICPrepC
+#define jinit_c_coef_controller	jICCoefC
+#define jinit_color_converter	jICColor
+#define jinit_downsampler	jIDownsampler
+#define jinit_forward_dct	jIFDCT
+#define jinit_huff_encoder	jIHEncoder
+#define jinit_marker_writer	jIMWriter
+#define jinit_master_decompress	jIDMaster
+#define jinit_d_main_controller	jIDMainC
+#define jinit_d_coef_controller	jIDCoefC
+#define jinit_d_post_controller	jIDPostC
+#define jinit_marker_reader	jIMReader
+#define jinit_huff_decoder	jIHDecoder
+#define jinit_inverse_dct	jIIDCT
+#define jinit_upsampler		jIUpsampler
+#define jinit_color_deconverter	jIDColor
+#define jinit_1pass_quantizer	jI1Quant
+#define jinit_2pass_quantizer	jI2Quant
+#define jinit_merged_upsampler	jIMUpsampler
+#define jinit_memory_mgr	jIMemMgr
+#define jdiv_round_up		jDivRound
+#define jround_up		jRound
+#define jcopy_sample_rows	jCopySamples
+#define jcopy_block_row		jCopyBlocks
+#define jzero_far		jZeroFar
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/* Compression module initialization routines */
+EXTERN void jinit_master_compress JPP((j_compress_ptr cinfo));
+EXTERN void jinit_c_main_controller JPP((j_compress_ptr cinfo,
+					 boolean need_full_buffer));
+EXTERN void jinit_c_prep_controller JPP((j_compress_ptr cinfo,
+					 boolean need_full_buffer));
+EXTERN void jinit_c_coef_controller JPP((j_compress_ptr cinfo,
+					 boolean need_full_buffer));
+EXTERN void jinit_color_converter JPP((j_compress_ptr cinfo));
+EXTERN void jinit_downsampler JPP((j_compress_ptr cinfo));
+EXTERN void jinit_forward_dct JPP((j_compress_ptr cinfo));
+EXTERN void jinit_huff_encoder JPP((j_compress_ptr cinfo));
+EXTERN void jinit_marker_writer JPP((j_compress_ptr cinfo));
+/* Decompression module initialization routines */
+EXTERN void jinit_master_decompress JPP((j_decompress_ptr cinfo));
+EXTERN void jinit_d_main_controller JPP((j_decompress_ptr cinfo,
+					 boolean need_full_buffer));
+EXTERN void jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
+					 boolean need_full_buffer));
+EXTERN void jinit_d_post_controller JPP((j_decompress_ptr cinfo,
+					 boolean need_full_buffer));
+EXTERN void jinit_marker_reader JPP((j_decompress_ptr cinfo));
+EXTERN void jinit_huff_decoder JPP((j_decompress_ptr cinfo));
+EXTERN void jinit_inverse_dct JPP((j_decompress_ptr cinfo));
+EXTERN void jinit_upsampler JPP((j_decompress_ptr cinfo));
+EXTERN void jinit_color_deconverter JPP((j_decompress_ptr cinfo));
+EXTERN void jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
+EXTERN void jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
+EXTERN void jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
+/* Memory manager initialization */
+EXTERN void jinit_memory_mgr JPP((j_common_ptr cinfo));
+
+/* Utility routines in jutils.c */
+EXTERN long jdiv_round_up JPP((long a, long b));
+EXTERN long jround_up JPP((long a, long b));
+EXTERN void jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
+				   JSAMPARRAY output_array, int dest_row,
+				   int num_rows, JDIMENSION num_cols));
+EXTERN void jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
+				 JDIMENSION num_blocks));
+EXTERN void jzero_far JPP((void FAR * target, size_t bytestozero));
+
+
+/* Suppress undefined-structure complaints if necessary. */
+
+#ifdef INCOMPLETE_TYPES_BROKEN
+#ifndef AM_MEMORY_MANAGER	/* only jmemmgr.c defines these */
+struct jvirt_sarray_control { long dummy; };
+struct jvirt_barray_control { long dummy; };
+#endif
+#endif /* INCOMPLETE_TYPES_BROKEN */
diff --git a/jpeglib.h b/jpeglib.h
new file mode 100644
index 0000000..893f8b8
--- /dev/null
+++ b/jpeglib.h
@@ -0,0 +1,930 @@
+/*
+ * jpeglib.h
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file defines the application interface for the JPEG library.
+ * Most applications using the library need only include this file,
+ * and perhaps jerror.h if they want to know the exact error codes.
+ */
+
+/*
+ * First we include the configuration files that record how this
+ * installation of the JPEG library is set up.  jconfig.h can be
+ * generated automatically for many systems.  jmorecfg.h contains
+ * manual configuration options that most people need not worry about.
+ */
+
+#ifndef JCONFIG_INCLUDED	/* in case jinclude.h already did */
+#include "jconfig.h"		/* widely used configuration options */
+#endif
+#include "jmorecfg.h"		/* seldom changed options */
+
+
+/* Version ID for the JPEG library.
+ * Might be useful for tests like "#if JPEG_LIB_VERSION >= 60".
+ */
+
+#define JPEG_LIB_VERSION  50	/* Version 5.0 */
+
+
+/* Various constants determining the sizes of things.
+ * All of these are specified by the JPEG standard, so don't change them
+ * if you want to be compatible.
+ */
+
+#define DCTSIZE		    8	/* The basic DCT block is 8x8 samples */
+#define DCTSIZE2	    64	/* DCTSIZE squared; # of elements in a block */
+#define NUM_QUANT_TBLS      4	/* Quantization tables are numbered 0..3 */
+#define NUM_HUFF_TBLS       4	/* Huffman tables are numbered 0..3 */
+#define NUM_ARITH_TBLS      16	/* Arith-coding tables are numbered 0..15 */
+#define MAX_COMPS_IN_SCAN   4	/* JPEG limit on # of components in one scan */
+#define MAX_SAMP_FACTOR     4	/* JPEG limit on sampling factors */
+#define MAX_BLOCKS_IN_MCU   10	/* JPEG limit on # of blocks in an MCU */
+
+
+/* This macro is used to declare a "method", that is, a function pointer.
+ * We want to supply prototype parameters if the compiler can cope.
+ * Note that the arglist parameter must be parenthesized!
+ */
+
+#ifdef HAVE_PROTOTYPES
+#define JMETHOD(type,methodname,arglist)  type (*methodname) arglist
+#else
+#define JMETHOD(type,methodname,arglist)  type (*methodname) ()
+#endif
+
+
+/* Data structures for images (arrays of samples and of DCT coefficients).
+ * On 80x86 machines, the image arrays are too big for near pointers,
+ * but the pointer arrays can fit in near memory.
+ */
+
+typedef JSAMPLE FAR *JSAMPROW;	/* ptr to one image row of pixel samples. */
+typedef JSAMPROW *JSAMPARRAY;	/* ptr to some rows (a 2-D sample array) */
+typedef JSAMPARRAY *JSAMPIMAGE;	/* a 3-D sample array: top index is color */
+
+typedef JCOEF JBLOCK[DCTSIZE2];	/* one block of coefficients */
+typedef JBLOCK FAR *JBLOCKROW;	/* pointer to one row of coefficient blocks */
+typedef JBLOCKROW *JBLOCKARRAY;		/* a 2-D array of coefficient blocks */
+typedef JBLOCKARRAY *JBLOCKIMAGE;	/* a 3-D array of coefficient blocks */
+
+typedef JCOEF FAR *JCOEFPTR;	/* useful in a couple of places */
+
+
+/* Types for JPEG compression parameters and working tables. */
+
+
+/* DCT coefficient quantization tables. */
+
+typedef struct {
+  /* This field directly represents the contents of a JPEG DQT marker.
+   * Note: the values are always given in zigzag order.
+   */
+  UINT16 quantval[DCTSIZE2];	/* quantization step for each coefficient */
+  /* This field is used only during compression.  It's initialized FALSE when
+   * the table is created, and set TRUE when it's been output to the file.
+   * You could suppress output of a table by setting this to TRUE.
+   * (See jpeg_suppress_tables for an example.)
+   */
+  boolean sent_table;		/* TRUE when table has been output */
+} JQUANT_TBL;
+
+
+/* Huffman coding tables. */
+
+typedef struct {
+  /* These two fields directly represent the contents of a JPEG DHT marker */
+  UINT8 bits[17];		/* bits[k] = # of symbols with codes of */
+				/* length k bits; bits[0] is unused */
+  UINT8 huffval[256];		/* The symbols, in order of incr code length */
+  /* This field is used only during compression.  It's initialized FALSE when
+   * the table is created, and set TRUE when it's been output to the file.
+   * You could suppress output of a table by setting this to TRUE.
+   * (See jpeg_suppress_tables for an example.)
+   */
+  boolean sent_table;		/* TRUE when table has been output */
+} JHUFF_TBL;
+
+
+/* Basic info about one component (color channel). */
+
+typedef struct {
+  /* These values are fixed over the whole image. */
+  /* For compression, they must be supplied by parameter setup; */
+  /* for decompression, they are read from the SOF marker. */
+  int component_id;		/* identifier for this component (0..255) */
+  int component_index;		/* its index in SOF or cinfo->comp_info[] */
+  int h_samp_factor;		/* horizontal sampling factor (1..4) */
+  int v_samp_factor;		/* vertical sampling factor (1..4) */
+  int quant_tbl_no;		/* quantization table selector (0..3) */
+  /* These values may vary between scans. */
+  /* For compression, they must be supplied by parameter setup; */
+  /* for decompression, they are read from the SOS marker. */
+  int dc_tbl_no;		/* DC entropy table selector (0..3) */
+  int ac_tbl_no;		/* AC entropy table selector (0..3) */
+  
+  /* Remaining fields should be treated as private by applications. */
+  
+  /* These values are computed during compression or decompression startup: */
+  /* Component's size in DCT blocks.
+   * Any dummy blocks added to complete an MCU are not counted; therefore
+   * these values do not depend on whether a scan is interleaved or not.
+   */
+  JDIMENSION width_in_blocks;
+  JDIMENSION height_in_blocks;
+  /* Size of a DCT block in samples.  Always DCTSIZE for compression.
+   * For decompression this is the size of the output from one DCT block,
+   * reflecting any scaling we choose to apply during the IDCT step.
+   * Values of 1,2,4,8 are likely to be supported.  Note that different
+   * components may receive different IDCT scalings.
+   */
+  int DCT_scaled_size;
+  /* The downsampled dimensions are the component's actual, unpadded number
+   * of samples at the main buffer (preprocessing/compression interface), thus
+   * downsampled_width = ceil(image_width * Hi/Hmax)
+   * and similarly for height.  For decompression, IDCT scaling is included, so
+   * downsampled_width = ceil(image_width * Hi/Hmax * DCT_scaled_size/DCTSIZE)
+   */
+  JDIMENSION downsampled_width;	 /* actual width in samples */
+  JDIMENSION downsampled_height; /* actual height in samples */
+  /* This flag is used only for decompression.  In cases where some of the
+   * components will be ignored (eg grayscale output from YCbCr image),
+   * we can skip most computations for the unused components.
+   */
+  boolean component_needed;	/* do we need the value of this component? */
+
+  /* These values are computed before starting a scan of the component: */
+  int MCU_width;		/* number of blocks per MCU, horizontally */
+  int MCU_height;		/* number of blocks per MCU, vertically */
+  int MCU_blocks;		/* MCU_width * MCU_height */
+  int MCU_sample_width;		/* MCU width in samples, MCU_width*DCT_scaled_size */
+  int last_col_width;		/* # of non-dummy blocks across in last MCU */
+  int last_row_height;		/* # of non-dummy blocks down in last MCU */
+
+  /* Private per-component storage for DCT or IDCT subsystem. */
+  void * dct_table;
+} jpeg_component_info;
+
+
+/* Known color spaces. */
+
+typedef enum {
+	JCS_UNKNOWN,		/* error/unspecified */
+	JCS_GRAYSCALE,		/* monochrome */
+	JCS_RGB,		/* red/green/blue */
+	JCS_YCbCr,		/* Y/Cb/Cr (also known as YUV) */
+	JCS_CMYK,		/* C/M/Y/K */
+	JCS_YCCK		/* Y/Cb/Cr/K */
+} J_COLOR_SPACE;
+
+/* DCT/IDCT algorithm options. */
+
+typedef enum {
+	JDCT_ISLOW,		/* slow but accurate integer algorithm */
+	JDCT_IFAST,		/* faster, less accurate integer method */
+	JDCT_FLOAT		/* floating-point: accurate, fast on fast HW */
+} J_DCT_METHOD;
+
+#ifndef JDCT_DEFAULT		/* may be overridden in jconfig.h */
+#define JDCT_DEFAULT  JDCT_ISLOW
+#endif
+#ifndef JDCT_FASTEST		/* may be overridden in jconfig.h */
+#define JDCT_FASTEST  JDCT_IFAST
+#endif
+
+/* Dithering options for decompression. */
+
+typedef enum {
+	JDITHER_NONE,		/* no dithering */
+	JDITHER_ORDERED,	/* simple ordered dither */
+	JDITHER_FS		/* Floyd-Steinberg error diffusion dither */
+} J_DITHER_MODE;
+
+
+/* Common fields between JPEG compression and decompression master structs. */
+
+#define jpeg_common_fields \
+  struct jpeg_error_mgr * err;	/* Error handler module */\
+  struct jpeg_memory_mgr * mem;	/* Memory manager module */\
+  struct jpeg_progress_mgr * progress; /* Progress monitor, or NULL if none */\
+  boolean is_decompressor;	/* so common code can tell which is which */\
+  int global_state		/* for checking call sequence validity */
+
+/* Routines that are to be used by both halves of the library are declared
+ * to receive a pointer to this structure.  There are no actual instances of
+ * jpeg_common_struct, only of jpeg_compress_struct and jpeg_decompress_struct.
+ */
+struct jpeg_common_struct {
+  jpeg_common_fields;		/* Fields common to both master struct types */
+  /* Additional fields follow in an actual jpeg_compress_struct or
+   * jpeg_decompress_struct.  All three structs must agree on these
+   * initial fields!  (This would be a lot cleaner in C++.)
+   */
+};
+
+typedef struct jpeg_common_struct * j_common_ptr;
+typedef struct jpeg_compress_struct * j_compress_ptr;
+typedef struct jpeg_decompress_struct * j_decompress_ptr;
+
+
+/* Master record for a compression instance */
+
+struct jpeg_compress_struct {
+  jpeg_common_fields;		/* Fields shared with jpeg_decompress_struct */
+
+  /* Destination for compressed data */
+  struct jpeg_destination_mgr * dest;
+
+  /* Description of source image --- these fields must be filled in by
+   * outer application before starting compression.  in_color_space must
+   * be correct before you can even call jpeg_set_defaults().
+   */
+
+  JDIMENSION image_width;	/* input image width */
+  JDIMENSION image_height;	/* input image height */
+  int input_components;		/* # of color components in input image */
+  J_COLOR_SPACE in_color_space;	/* colorspace of input image */
+
+  double input_gamma;		/* image gamma of input image */
+
+  /* Compression parameters --- these fields must be set before calling
+   * jpeg_start_compress().  We recommend calling jpeg_set_defaults() to
+   * initialize everything to reasonable defaults, then changing anything
+   * the application specifically wants to change.  That way you won't get
+   * burnt when new parameters are added.  Also note that there are several
+   * helper routines to simplify changing parameters.
+   */
+
+  int data_precision;		/* bits of precision in image data */
+
+  int num_components;		/* # of color components in JPEG image */
+  J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
+
+  jpeg_component_info * comp_info;
+  /* comp_info[i] describes component that appears i'th in SOF */
+  
+  JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
+  /* ptrs to coefficient quantization tables, or NULL if not defined */
+  
+  JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
+  JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
+  /* ptrs to Huffman coding tables, or NULL if not defined */
+  
+  UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
+  UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
+  UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
+
+  boolean raw_data_in;		/* TRUE=caller supplies downsampled data */
+  boolean arith_code;		/* TRUE=arithmetic coding, FALSE=Huffman */
+  boolean interleave;		/* TRUE=interleaved output, FALSE=not */
+  boolean optimize_coding;	/* TRUE=optimize entropy encoding parms */
+  boolean CCIR601_sampling;	/* TRUE=first samples are cosited */
+  int smoothing_factor;		/* 1..100, or 0 for no input smoothing */
+  J_DCT_METHOD dct_method;	/* DCT algorithm selector */
+
+  /* The restart interval can be specified in absolute MCUs by setting
+   * restart_interval, or in MCU rows by setting restart_in_rows
+   * (in which case the correct restart_interval will be figured
+   * for each scan).
+   */
+  unsigned int restart_interval; /* MCUs per restart, or 0 for no restart */
+  int restart_in_rows;		/* if > 0, MCU rows per restart interval */
+
+  /* Parameters controlling emission of special markers. */
+
+  boolean write_JFIF_header;	/* should a JFIF marker be written? */
+  /* These three values are not used by the JPEG code, merely copied */
+  /* into the JFIF APP0 marker.  density_unit can be 0 for unknown, */
+  /* 1 for dots/inch, or 2 for dots/cm.  Note that the pixel aspect */
+  /* ratio is defined by X_density/Y_density even when density_unit=0. */
+  UINT8 density_unit;		/* JFIF code for pixel size units */
+  UINT16 X_density;		/* Horizontal pixel density */
+  UINT16 Y_density;		/* Vertical pixel density */
+  boolean write_Adobe_marker;	/* should an Adobe marker be written? */
+  
+  /* State variable: index of next scanline to be written to
+   * jpeg_write_scanlines().  Application may use this to control its
+   * processing loop, e.g., "while (next_scanline < image_height)".
+   */
+
+  JDIMENSION next_scanline;	/* 0 .. image_height-1  */
+
+  /* Remaining fields are known throughout compressor, but generally
+   * should not be touched by a surrounding application.
+   */
+
+  /*
+   * These fields are computed during compression startup
+   */
+  int max_h_samp_factor;	/* largest h_samp_factor */
+  int max_v_samp_factor;	/* largest v_samp_factor */
+
+  JDIMENSION total_iMCU_rows;	/* # of iMCU rows to be input to coef ctlr */
+  /* The coefficient controller receives data in units of MCU rows as defined
+   * for fully interleaved scans (whether the JPEG file is interleaved or not).
+   * There are v_samp_factor * DCTSIZE sample rows of each component in an
+   * "iMCU" (interleaved MCU) row.
+   */
+  
+  /*
+   * These fields are valid during any one scan.
+   * They describe the components and MCUs actually appearing in the scan.
+   */
+  int comps_in_scan;		/* # of JPEG components in this scan */
+  jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
+  /* *cur_comp_info[i] describes component that appears i'th in SOS */
+  
+  JDIMENSION MCUs_per_row;	/* # of MCUs across the image */
+  JDIMENSION MCU_rows_in_scan;	/* # of MCU rows in the image */
+  
+  int blocks_in_MCU;		/* # of DCT blocks per MCU */
+  int MCU_membership[MAX_BLOCKS_IN_MCU];
+  /* MCU_membership[i] is index in cur_comp_info of component owning */
+  /* i'th block in an MCU */
+
+  /*
+   * Links to compression subobjects (methods and private variables of modules)
+   */
+  struct jpeg_comp_master * master;
+  struct jpeg_c_main_controller * main;
+  struct jpeg_c_prep_controller * prep;
+  struct jpeg_c_coef_controller * coef;
+  struct jpeg_marker_writer * marker;
+  struct jpeg_color_converter * cconvert;
+  struct jpeg_downsampler * downsample;
+  struct jpeg_forward_dct * fdct;
+  struct jpeg_entropy_encoder * entropy;
+};
+
+
+/* Master record for a decompression instance */
+
+struct jpeg_decompress_struct {
+  jpeg_common_fields;		/* Fields shared with jpeg_compress_struct */
+
+  /* Source of compressed data */
+  struct jpeg_source_mgr * src;
+
+  /* Basic description of image --- filled in by jpeg_read_header(). */
+  /* Application may inspect these values to decide how to process image. */
+
+  JDIMENSION image_width;	/* nominal image width (from SOF marker) */
+  JDIMENSION image_height;	/* nominal image height */
+  int num_components;		/* # of color components in JPEG image */
+  J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
+
+  /* Decompression processing parameters --- these fields must be set before
+   * calling jpeg_start_decompress().  Note that jpeg_read_header() initializes
+   * them to default values.
+   */
+
+  J_COLOR_SPACE out_color_space; /* colorspace for output */
+
+  unsigned int scale_num, scale_denom; /* fraction by which to scale image */
+
+  double output_gamma;		/* image gamma wanted in output */
+
+  boolean raw_data_out;		/* TRUE=downsampled data wanted */
+
+  boolean quantize_colors;	/* TRUE=colormapped output wanted */
+  /* the following are ignored if not quantize_colors: */
+  boolean two_pass_quantize;	/* TRUE=use two-pass color quantization */
+  J_DITHER_MODE dither_mode;	/* type of color dithering to use */
+  int desired_number_of_colors;	/* max number of colors to use */
+
+  J_DCT_METHOD dct_method;	/* DCT algorithm selector */
+  boolean do_fancy_upsampling;	/* TRUE=apply fancy upsampling */
+
+  /* Description of actual output image that will be returned to application.
+   * These fields are computed by jpeg_start_decompress().
+   * You can also use jpeg_calc_output_dimensions() to determine these values
+   * in advance of calling jpeg_start_decompress().
+   */
+
+  JDIMENSION output_width;	/* scaled image width */
+  JDIMENSION output_height;	/* scaled image height */
+  int out_color_components;	/* # of color components in out_color_space */
+  int output_components;	/* # of color components returned */
+  /* output_components is 1 (a colormap index) when quantizing colors;
+   * otherwise it equals out_color_components.
+   */
+  int rec_outbuf_height;	/* min recommended height of scanline buffer */
+  /* If the buffer passed to jpeg_read_scanlines() is less than this many rows
+   * high, space and time will be wasted due to unnecessary data copying.
+   * Usually rec_outbuf_height will be 1 or 2, at most 4.
+   */
+
+  /* When quantizing colors, the output colormap is described by these fields.
+   * The application can supply a colormap by setting colormap non-NULL before
+   * calling jpeg_start_decompress; otherwise a colormap is created during
+   * jpeg_start_decompress.
+   * The map has out_color_components rows and actual_number_of_colors columns.
+   */
+  int actual_number_of_colors;	/* number of entries in use */
+  JSAMPARRAY colormap;		/* The color map as a 2-D pixel array */
+
+  /* State variable: index of next scaled scanline to be read from
+   * jpeg_read_scanlines().  Application may use this to control its
+   * processing loop, e.g., "while (output_scanline < output_height)".
+   */
+
+  JDIMENSION output_scanline;	/* 0 .. output_height-1  */
+
+  /* Internal JPEG parameters --- the application usually need not look at
+   * these fields.
+   */
+
+  /* Quantization and Huffman tables are carried forward across input
+   * datastreams when processing abbreviated JPEG datastreams.
+   */
+
+  JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
+  /* ptrs to coefficient quantization tables, or NULL if not defined */
+
+  JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
+  JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
+  /* ptrs to Huffman coding tables, or NULL if not defined */
+
+  /* These parameters are never carried across datastreams, since they
+   * are given in SOF/SOS markers or defined to be reset by SOI.
+   */
+
+  int data_precision;		/* bits of precision in image data */
+
+  jpeg_component_info * comp_info;
+  /* comp_info[i] describes component that appears i'th in SOF */
+
+  UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
+  UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
+  UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
+
+  boolean arith_code;		/* TRUE=arithmetic coding, FALSE=Huffman */
+
+  unsigned int restart_interval; /* MCUs per restart interval, or 0 for no restart */
+
+  /* These fields record data obtained from optional markers recognized by
+   * the JPEG library.
+   */
+  boolean saw_JFIF_marker;	/* TRUE iff a JFIF APP0 marker was found */
+  /* Data copied from JFIF marker: */
+  UINT8 density_unit;		/* JFIF code for pixel size units */
+  UINT16 X_density;		/* Horizontal pixel density */
+  UINT16 Y_density;		/* Vertical pixel density */
+  boolean saw_Adobe_marker;	/* TRUE iff an Adobe APP14 marker was found */
+  UINT8 Adobe_transform;	/* Color transform code from Adobe marker */
+
+  boolean CCIR601_sampling;	/* TRUE=first samples are cosited */
+
+  /* Remaining fields are known throughout decompressor, but generally
+   * should not be touched by a surrounding application.
+   */
+
+  /*
+   * These fields are computed during decompression startup
+   */
+  int max_h_samp_factor;	/* largest h_samp_factor */
+  int max_v_samp_factor;	/* largest v_samp_factor */
+
+  int min_DCT_scaled_size;	/* smallest DCT_scaled_size of any component */
+
+  JDIMENSION total_iMCU_rows;	/* # of iMCU rows to be output by coef ctlr */
+  /* The coefficient controller outputs data in units of MCU rows as defined
+   * for fully interleaved scans (whether the JPEG file is interleaved or not).
+   * There are v_samp_factor * DCT_scaled_size sample rows of each component
+   * in an "iMCU" (interleaved MCU) row.
+   */
+
+  JSAMPLE * sample_range_limit; /* table for fast range-limiting */
+
+  /*
+   * These fields are valid during any one scan.
+   * They describe the components and MCUs actually appearing in the scan.
+   */
+  int comps_in_scan;		/* # of JPEG components in this scan */
+  jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
+  /* *cur_comp_info[i] describes component that appears i'th in SOS */
+
+  JDIMENSION MCUs_per_row;	/* # of MCUs across the image */
+  JDIMENSION MCU_rows_in_scan;	/* # of MCU rows in the image */
+
+  int blocks_in_MCU;		/* # of DCT blocks per MCU */
+  int MCU_membership[MAX_BLOCKS_IN_MCU];
+  /* MCU_membership[i] is index in cur_comp_info of component owning */
+  /* i'th block in an MCU */
+
+  /* This field is shared between entropy decoder and marker parser.
+   * It is either zero or the code of a JPEG marker that has been
+   * read from the data source, but has not yet been processed.
+   */
+  int unread_marker;
+
+  /*
+   * Links to decompression subobjects (methods, private variables of modules)
+   */
+  struct jpeg_decomp_master * master;
+  struct jpeg_d_main_controller * main;
+  struct jpeg_d_coef_controller * coef;
+  struct jpeg_d_post_controller * post;
+  struct jpeg_marker_reader * marker;
+  struct jpeg_entropy_decoder * entropy;
+  struct jpeg_inverse_dct * idct;
+  struct jpeg_upsampler * upsample;
+  struct jpeg_color_deconverter * cconvert;
+  struct jpeg_color_quantizer * cquantize;
+};
+
+
+/* "Object" declarations for JPEG modules that may be supplied or called
+ * directly by the surrounding application.
+ * As with all objects in the JPEG library, these structs only define the
+ * publicly visible methods and state variables of a module.  Additional
+ * private fields may exist after the public ones.
+ */
+
+
+/* Error handler object */
+
+struct jpeg_error_mgr {
+  /* Error exit handler: does not return to caller */
+  JMETHOD(void, error_exit, (j_common_ptr cinfo));
+  /* Conditionally emit a trace or warning message */
+  JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level));
+  /* Routine that actually outputs a trace or error message */
+  JMETHOD(void, output_message, (j_common_ptr cinfo));
+  /* Format a message string for the most recent JPEG error or message */
+  JMETHOD(void, format_message, (j_common_ptr cinfo, char * buffer));
+#define JMSG_LENGTH_MAX  200	/* recommended size of format_message buffer */
+  /* Reset error state variables at start of a new image */
+  JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo));
+  
+  /* The message ID code and any parameters are saved here.
+   * A message can have one string parameter or up to 8 int parameters.
+   */
+  int msg_code;
+#define JMSG_STR_PARM_MAX  80
+  union {
+    int i[8];
+    char s[JMSG_STR_PARM_MAX];
+  } msg_parm;
+  
+  /* Standard state variables for error facility */
+  
+  int trace_level;		/* max msg_level that will be displayed */
+  
+  /* For recoverable corrupt-data errors, we emit a warning message,
+   * but keep going unless emit_message chooses to abort.  emit_message
+   * should count warnings in num_warnings.  The surrounding application
+   * can check for bad data by seeing if num_warnings is nonzero at the
+   * end of processing.
+   */
+  long num_warnings;		/* number of corrupt-data warnings */
+
+  /* These fields point to the table(s) of error message strings.
+   * An application can change the table pointer to switch to a different
+   * message list (typically, to change the language in which errors are
+   * reported).  Some applications may wish to add additional error codes
+   * that will be handled by the JPEG library error mechanism; the second
+   * table pointer is used for this purpose.
+   *
+   * First table includes all errors generated by JPEG library itself.
+   * Error code 0 is reserved for a "no such error string" message.
+   */
+  const char * const * jpeg_message_table; /* Library errors */
+  int last_jpeg_message;    /* Table contains strings 0..last_jpeg_message */
+  /* Second table can be added by application (see cjpeg/djpeg for example).
+   * It contains strings numbered first_addon_message..last_addon_message.
+   */
+  const char * const * addon_message_table; /* Non-library errors */
+  int first_addon_message;	/* code for first string in addon table */
+  int last_addon_message;	/* code for last string in addon table */
+};
+
+
+/* Progress monitor object */
+
+struct jpeg_progress_mgr {
+  JMETHOD(void, progress_monitor, (j_common_ptr cinfo));
+
+  long pass_counter;		/* work units completed in this pass */
+  long pass_limit;		/* total number of work units in this pass */
+  int completed_passes;		/* passes completed so far */
+  int total_passes;		/* total number of passes expected */
+};
+
+
+/* Data destination object for compression */
+
+struct jpeg_destination_mgr {
+  JOCTET * next_output_byte;	/* => next byte to write in buffer */
+  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
+
+  JMETHOD(void, init_destination, (j_compress_ptr cinfo));
+  JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo));
+  JMETHOD(void, term_destination, (j_compress_ptr cinfo));
+};
+
+
+/* Data source object for decompression */
+
+struct jpeg_source_mgr {
+  const JOCTET * next_input_byte; /* => next byte to read from buffer */
+  size_t bytes_in_buffer;	/* # of bytes remaining in buffer */
+
+  JMETHOD(void, init_source, (j_decompress_ptr cinfo));
+  JMETHOD(boolean, fill_input_buffer, (j_decompress_ptr cinfo));
+  JMETHOD(void, skip_input_data, (j_decompress_ptr cinfo, long num_bytes));
+  JMETHOD(boolean, resync_to_restart, (j_decompress_ptr cinfo));
+  JMETHOD(void, term_source, (j_decompress_ptr cinfo));
+};
+
+
+/* Memory manager object.
+ * Allocates "small" objects (a few K total), "large" objects (tens of K),
+ * and "really big" objects (virtual arrays with backing store if needed).
+ * The memory manager does not allow individual objects to be freed; rather,
+ * each created object is assigned to a pool, and whole pools can be freed
+ * at once.  This is faster and more convenient than remembering exactly what
+ * to free, especially where malloc()/free() are not too speedy.
+ * NB: alloc routines never return NULL.  They exit to error_exit if not
+ * successful.
+ */
+
+#define JPOOL_PERMANENT	0	/* lasts until master record is destroyed */
+#define JPOOL_IMAGE	1	/* lasts until done with image/datastream */
+#define JPOOL_NUMPOOLS	2
+
+typedef struct jvirt_sarray_control * jvirt_sarray_ptr;
+typedef struct jvirt_barray_control * jvirt_barray_ptr;
+
+
+struct jpeg_memory_mgr {
+  /* Method pointers */
+  JMETHOD(void *, alloc_small, (j_common_ptr cinfo, int pool_id,
+				size_t sizeofobject));
+  JMETHOD(void FAR *, alloc_large, (j_common_ptr cinfo, int pool_id,
+				     size_t sizeofobject));
+  JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id,
+				     JDIMENSION samplesperrow,
+				     JDIMENSION numrows));
+  JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id,
+				      JDIMENSION blocksperrow,
+				      JDIMENSION numrows));
+  JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo,
+						  int pool_id,
+						  JDIMENSION samplesperrow,
+						  JDIMENSION numrows,
+						  JDIMENSION unitheight));
+  JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo,
+						  int pool_id,
+						  JDIMENSION blocksperrow,
+						  JDIMENSION numrows,
+						  JDIMENSION unitheight));
+  JMETHOD(void, realize_virt_arrays, (j_common_ptr cinfo));
+  JMETHOD(JSAMPARRAY, access_virt_sarray, (j_common_ptr cinfo,
+					   jvirt_sarray_ptr ptr,
+					   JDIMENSION start_row,
+					   boolean writable));
+  JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo,
+					    jvirt_barray_ptr ptr,
+					    JDIMENSION start_row,
+					    boolean writable));
+  JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id));
+  JMETHOD(void, self_destruct, (j_common_ptr cinfo));
+
+  /* Limit on memory allocation for this JPEG object.  (Note that this is
+   * merely advisory, not a guaranteed maximum; it only affects the space
+   * used for virtual-array buffers.)  May be changed by outer application
+   * after creating the JPEG object.
+   */
+  long max_memory_to_use;
+};
+
+
+/* Routine signature for application-supplied marker processing methods.
+ * Need not pass marker code since it is stored in cinfo->unread_marker.
+ */
+typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));
+
+
+/* Declarations for routines called by application.
+ * The JPP macro hides prototype parameters from compilers that can't cope.
+ * Note JPP requires double parentheses.
+ */
+
+#ifdef HAVE_PROTOTYPES
+#define JPP(arglist)	arglist
+#else
+#define JPP(arglist)	()
+#endif
+
+
+/* Short forms of external names for systems with brain-damaged linkers.
+ * We shorten external names to be unique in the first six letters, which
+ * is good enough for all known systems.
+ * (If your compiler itself needs names to be unique in less than 15 
+ * characters, you are out of luck.  Get a better compiler.)
+ */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_std_error		jStdError
+#define jpeg_create_compress	jCreaCompress
+#define jpeg_create_decompress	jCreaDecompress
+#define jpeg_destroy_compress	jDestCompress
+#define jpeg_destroy_decompress	jDestDecompress
+#define jpeg_stdio_dest		jStdDest
+#define jpeg_stdio_src		jStdSrc
+#define jpeg_set_defaults	jSetDefaults
+#define jpeg_set_colorspace	jSetColorspace
+#define jpeg_default_colorspace	jDefColorspace
+#define jpeg_set_quality	jSetQuality
+#define jpeg_set_linear_quality	jSetLQuality
+#define jpeg_add_quant_table	jAddQuantTable
+#define jpeg_quality_scaling	jQualityScaling
+#define jpeg_suppress_tables	jSuppressTables
+#define jpeg_alloc_quant_table	jAlcQTable
+#define jpeg_alloc_huff_table	jAlcHTable
+#define jpeg_start_compress	jStrtCompress
+#define jpeg_write_scanlines	jWrtScanlines
+#define jpeg_finish_compress	jFinCompress
+#define jpeg_write_raw_data	jWrtRawData
+#define jpeg_write_marker	jWrtMarker
+#define jpeg_write_tables	jWrtTables
+#define jpeg_read_header	jReadHeader
+#define jpeg_start_decompress	jStrtDecompress
+#define jpeg_read_scanlines	jReadScanlines
+#define jpeg_finish_decompress	jFinDecompress
+#define jpeg_read_raw_data	jReadRawData
+#define jpeg_calc_output_dimensions	jCalcDimensions
+#define jpeg_set_marker_processor	jSetMarker
+#define jpeg_abort_compress	jAbrtCompress
+#define jpeg_abort_decompress	jAbrtDecompress
+#define jpeg_abort		jAbort
+#define jpeg_destroy		jDestroy
+#define jpeg_resync_to_restart	jResyncRestart
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/* Default error-management setup */
+EXTERN struct jpeg_error_mgr *jpeg_std_error JPP((struct jpeg_error_mgr *err));
+
+/* Initialization and destruction of JPEG compression objects */
+/* NB: you must set up the error-manager BEFORE calling jpeg_create_xxx */
+EXTERN void jpeg_create_compress JPP((j_compress_ptr cinfo));
+EXTERN void jpeg_create_decompress JPP((j_decompress_ptr cinfo));
+EXTERN void jpeg_destroy_compress JPP((j_compress_ptr cinfo));
+EXTERN void jpeg_destroy_decompress JPP((j_decompress_ptr cinfo));
+
+/* Standard data source and destination managers: stdio streams. */
+/* Caller is responsible for opening the file before and closing after. */
+EXTERN void jpeg_stdio_dest JPP((j_compress_ptr cinfo, FILE * outfile));
+EXTERN void jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile));
+
+/* Default parameter setup for compression */
+EXTERN void jpeg_set_defaults JPP((j_compress_ptr cinfo));
+/* Compression parameter setup aids */
+EXTERN void jpeg_set_colorspace JPP((j_compress_ptr cinfo,
+				     J_COLOR_SPACE colorspace));
+EXTERN void jpeg_default_colorspace JPP((j_compress_ptr cinfo));
+EXTERN void jpeg_set_quality JPP((j_compress_ptr cinfo, int quality,
+				  boolean force_baseline));
+EXTERN void jpeg_set_linear_quality JPP((j_compress_ptr cinfo,
+					 int scale_factor,
+					 boolean force_baseline));
+EXTERN void jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl,
+				      const unsigned int *basic_table,
+				      int scale_factor,
+				      boolean force_baseline));
+EXTERN int jpeg_quality_scaling JPP((int quality));
+EXTERN void jpeg_suppress_tables JPP((j_compress_ptr cinfo,
+				      boolean suppress));
+EXTERN JQUANT_TBL * jpeg_alloc_quant_table JPP((j_common_ptr cinfo));
+EXTERN JHUFF_TBL * jpeg_alloc_huff_table JPP((j_common_ptr cinfo));
+
+/* Main entry points for compression */
+EXTERN void jpeg_start_compress JPP((j_compress_ptr cinfo,
+				     boolean write_all_tables));
+EXTERN JDIMENSION jpeg_write_scanlines JPP((j_compress_ptr cinfo,
+					    JSAMPARRAY scanlines,
+					    JDIMENSION num_lines));
+EXTERN void jpeg_finish_compress JPP((j_compress_ptr cinfo));
+
+/* Replaces jpeg_write_scanlines when writing raw downsampled data. */
+EXTERN JDIMENSION jpeg_write_raw_data JPP((j_compress_ptr cinfo,
+					   JSAMPIMAGE data,
+					   JDIMENSION num_lines));
+
+/* Write a special marker.  See libjpeg.doc concerning safe usage. */
+EXTERN void jpeg_write_marker JPP((j_compress_ptr cinfo, int marker,
+				   const JOCTET *dataptr, unsigned int datalen));
+
+/* Alternate compression function: just write an abbreviated table file */
+EXTERN void jpeg_write_tables JPP((j_compress_ptr cinfo));
+
+/* Decompression startup: read start of JPEG datastream to see what's there */
+EXTERN int jpeg_read_header JPP((j_decompress_ptr cinfo,
+				 boolean require_image));
+/* Return value is one of: */
+#define JPEG_HEADER_OK		0 /* Found valid image datastream */
+#define JPEG_HEADER_TABLES_ONLY	1 /* Found valid table-specs-only datastream */
+#define JPEG_SUSPENDED		2 /* Had to suspend before end of headers */
+/* If you pass require_image = TRUE (normal case), you need not check for
+ * a TABLES_ONLY return code; an abbreviated file will cause an error exit.
+ * JPEG_SUSPENDED is only possible if you use a data source module that can
+ * give a suspension return (the stdio source module doesn't).
+ */
+
+/* Main entry points for decompression */
+EXTERN void jpeg_start_decompress JPP((j_decompress_ptr cinfo));
+EXTERN JDIMENSION jpeg_read_scanlines JPP((j_decompress_ptr cinfo,
+					   JSAMPARRAY scanlines,
+					   JDIMENSION max_lines));
+EXTERN boolean jpeg_finish_decompress JPP((j_decompress_ptr cinfo));
+
+/* Replaces jpeg_read_scanlines when reading raw downsampled data. */
+EXTERN JDIMENSION jpeg_read_raw_data JPP((j_decompress_ptr cinfo,
+					  JSAMPIMAGE data,
+					  JDIMENSION max_lines));
+
+/* Precalculate output dimensions for current decompression parameters. */
+EXTERN void jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo));
+
+/* Install a special processing method for COM or APPn markers. */
+EXTERN void jpeg_set_marker_processor JPP((j_decompress_ptr cinfo,
+					   int marker_code,
+					   jpeg_marker_parser_method routine));
+
+/* If you choose to abort compression or decompression before completing
+ * jpeg_finish_(de)compress, then you need to clean up to release memory,
+ * temporary files, etc.  You can just call jpeg_destroy_(de)compress
+ * if you're done with the JPEG object, but if you want to clean it up and
+ * reuse it, call this:
+ */
+EXTERN void jpeg_abort_compress JPP((j_compress_ptr cinfo));
+EXTERN void jpeg_abort_decompress JPP((j_decompress_ptr cinfo));
+
+/* Generic versions of jpeg_abort and jpeg_destroy that work on either
+ * flavor of JPEG object.  These may be more convenient in some places.
+ */
+EXTERN void jpeg_abort JPP((j_common_ptr cinfo));
+EXTERN void jpeg_destroy JPP((j_common_ptr cinfo));
+
+/* Default restart-marker-resync procedure for use by data source modules */
+EXTERN boolean jpeg_resync_to_restart JPP((j_decompress_ptr cinfo));
+
+
+/* These marker codes are exported since applications and data source modules
+ * are likely to want to use them.
+ */
+
+#define JPEG_RST0	0xD0	/* RST0 marker code */
+#define JPEG_EOI	0xD9	/* EOI marker code */
+#define JPEG_APP0	0xE0	/* APP0 marker code */
+#define JPEG_COM	0xFE	/* COM marker code */
+
+
+/* If we have a brain-damaged compiler that emits warnings (or worse, errors)
+ * for structure definitions that are never filled in, keep it quiet by
+ * supplying dummy definitions for the various substructures.
+ */
+
+#ifdef INCOMPLETE_TYPES_BROKEN
+#ifndef JPEG_INTERNALS		/* will be defined in jpegint.h */
+struct jvirt_sarray_control { long dummy; };
+struct jvirt_barray_control { long dummy; };
+struct jpeg_comp_master { long dummy; };
+struct jpeg_c_main_controller { long dummy; };
+struct jpeg_c_prep_controller { long dummy; };
+struct jpeg_c_coef_controller { long dummy; };
+struct jpeg_marker_writer { long dummy; };
+struct jpeg_color_converter { long dummy; };
+struct jpeg_downsampler { long dummy; };
+struct jpeg_forward_dct { long dummy; };
+struct jpeg_entropy_encoder { long dummy; };
+struct jpeg_decomp_master { long dummy; };
+struct jpeg_d_main_controller { long dummy; };
+struct jpeg_d_coef_controller { long dummy; };
+struct jpeg_d_post_controller { long dummy; };
+struct jpeg_marker_reader { long dummy; };
+struct jpeg_entropy_decoder { long dummy; };
+struct jpeg_inverse_dct { long dummy; };
+struct jpeg_upsampler { long dummy; };
+struct jpeg_color_deconverter { long dummy; };
+struct jpeg_color_quantizer { long dummy; };
+#endif /* JPEG_INTERNALS */
+#endif /* INCOMPLETE_TYPES_BROKEN */
+
+
+/*
+ * The JPEG library modules define JPEG_INTERNALS before including this file.
+ * The internal structure declarations are read only when that is true.
+ * Applications using the library should not include jpegint.h, but may wish
+ * to include jerror.h.
+ */
+
+#ifdef JPEG_INTERNALS
+#include "jpegint.h"		/* fetch private declarations */
+#include "jerror.h"		/* fetch error codes too */
+#endif
diff --git a/jquant1.c b/jquant1.c
index cfe03a1..a68f3e7 100644
--- a/jquant1.c
+++ b/jquant1.c
@@ -1,16 +1,18 @@
 /*
  * jquant1.c
  *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains 1-pass color quantization (color mapping) routines.
- * These routines are invoked via the methods color_quantize
- * and color_quant_init/term.
+ * These routines provide mapping to a fixed color map using equally spaced
+ * color values.  Optional Floyd-Steinberg or ordered dithering is available.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
 
 #ifdef QUANT_1PASS_SUPPORTED
 
@@ -22,24 +24,6 @@
  * quantizer is perfectly adequate.  Dithering is highly recommended with this
  * quantizer, though you can turn it off if you really want to.
  *
- * This implementation quantizes in the output colorspace.  This has a couple
- * of disadvantages: each pixel must be individually color-converted, and if
- * the color conversion includes gamma correction then quantization is done in
- * a nonlinear space, which is less desirable.  The major advantage is that
- * with the usual output color spaces (RGB, grayscale) an orthogonal grid of
- * representative colors can be used, thus permitting the very simple and fast
- * color lookup scheme used here.  The standard JPEG colorspace (YCbCr) cannot
- * be effectively handled this way, because only about a quarter of an
- * orthogonal grid would fall within the gamut of realizable colors.  Another
- * advantage is that when the user wants quantized grayscale output from a
- * color JPEG file, this quantizer can provide a high-quality result with no
- * special hacking.
- *
- * The gamma-correction problem could be eliminated by adjusting the grid
- * spacing to counteract the gamma correction applied by color_convert.
- * At this writing, gamma correction is not implemented by jdcolor, so
- * nothing is done here.
- *
  * In 1-pass quantization the colormap must be chosen in advance of seeing the
  * image.  We use a map consisting of all combinations of Ncolors[i] color
  * values for the i'th component.  The Ncolors[] values are chosen so that
@@ -58,25 +42,37 @@
  *    sum( colorindex[component-number][pixel-component-value] )
  * Aside from being fast, this scheme allows for variable spacing between
  * representative values with no additional lookup cost.
+ *
+ * If gamma correction has been applied in color conversion, it might be wise
+ * to adjust the color grid spacing so that the representative colors are
+ * equidistant in linear space.  At this writing, gamma correction is not
+ * implemented by jdcolor, so nothing is done here.
  */
 
 
-#define MAX_COMPONENTS 4	/* max components I can handle */
-
-static JSAMPARRAY colormap;	/* The actual color map */
-/* colormap[i][j] = value of i'th color component for output pixel value j */
-
-static JSAMPARRAY colorindex;	/* Precomputed mapping for speed */
-/* colorindex[i][j] = index of color closest to pixel value j in component i,
- * premultiplied as described above.  Since colormap indexes must fit into
- * JSAMPLEs, the entries of this array will too.
+/* Declarations for ordered dithering.
+ *
+ * We use a standard 4x4 ordered dither array.  The basic concept of ordered
+ * dithering is described in many references, for instance Dale Schumacher's
+ * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991).
+ * In place of Schumacher's comparisons against a "threshold" value, we add a
+ * "dither" value to the input pixel and then round the result to the nearest
+ * output value.  The dither value is equivalent to (0.5 - threshold) times
+ * the distance between output values.  For ordered dithering, we assume that
+ * the output colors are equally spaced; if not, results will probably be
+ * worse, since the dither may be too much or too little at a given point.
+ *
+ * The normal calculation would be to form pixel value + dither, range-limit
+ * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual.
+ * We can skip the separate range-limiting step by extending the colorindex
+ * table in both directions.
  */
 
-static JSAMPARRAY input_buffer;	/* color conversion workspace */
-/* Since our input data is presented in the JPEG colorspace, we have to call
- * color_convert to get it into the output colorspace.  input_buffer is a
- * one-row-high workspace for the result of color_convert.
- */
+#define ODITHER_SIZE  4		/* dimension of dither matrix */
+#define ODITHER_CELLS (4*4)	/* number of cells in dither matrix */
+#define ODITHER_MASK  3		/* mask for wrapping around dither counters */
+
+typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE];
 
 
 /* Declarations for Floyd-Steinberg dithering.
@@ -100,10 +96,10 @@
  * end saves us from special-casing the first and last pixels.
  *
  * Note: on a wide image, we might not have enough room in a PC's near data
- * segment to hold the error array; so it is allocated with alloc_medium.
+ * segment to hold the error array; so it is allocated with alloc_large.
  */
 
-#ifdef EIGHT_BIT_SAMPLES
+#if BITS_IN_JSAMPLE == 8
 typedef INT16 FSERROR;		/* 16 bits should be enough */
 typedef int LOCFSERROR;		/* use 'int' for calculation temps */
 #else
@@ -113,12 +109,34 @@
 
 typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */
 
-static FSERRPTR fserrors[MAX_COMPONENTS]; /* accumulated errors */
-static boolean on_odd_row;	/* flag to remember which row we are on */
+
+/* Private subobject */
+
+#define MAX_Q_COMPS 4		/* max components I can handle */
+
+typedef struct {
+  struct jpeg_color_quantizer pub; /* public fields */
+
+  JSAMPARRAY colorindex;	/* Precomputed mapping for speed */
+  /* colorindex[i][j] = index of color closest to pixel value j in component i,
+   * premultiplied as described above.  Since colormap indexes must fit into
+   * JSAMPLEs, the entries of this array will too.
+   */
+
+  /* Variables for ordered dithering */
+  int row_index;		/* cur row's vertical index in dither matrix */
+  ODITHER_MATRIX *odither;	/* one dither array per component */
+
+  /* Variables for Floyd-Steinberg dithering */
+  FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
+  boolean on_odd_row;		/* flag to remember which row we are on */
+} my_cquantizer;
+
+typedef my_cquantizer * my_cquantize_ptr;
 
 
 /*
- * Policy-making subroutines for color_quant_init: these routines determine
+ * Policy-making subroutines for create_colormap: these routines determine
  * the colormap to be used.  The rest of the module only assumes that the
  * colormap is orthogonal.
  *
@@ -133,16 +151,16 @@
 
 
 LOCAL int
-select_ncolors (decompress_info_ptr cinfo, int Ncolors[])
+select_ncolors (j_decompress_ptr cinfo, int Ncolors[])
 /* Determine allocation of desired colors to components, */
 /* and fill in Ncolors[] array to indicate choice. */
 /* Return value is total number of colors (product of Ncolors[] values). */
 {
-  int nc = cinfo->color_out_comps; /* number of color components */
+  int nc = cinfo->out_color_components; /* number of color components */
   int max_colors = cinfo->desired_number_of_colors;
-  int total_colors, iroot, i;
+  int total_colors, iroot, i, j;
   long temp;
-  boolean changed;
+  static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
 
   /* We can allocate at least the nc'th root of max_colors per component. */
   /* Compute floor(nc'th root of max_colors). */
@@ -157,82 +175,27 @@
 
   /* Must have at least 2 color values per component */
   if (iroot < 2)
-    ERREXIT1(cinfo->emethods, "Cannot quantize to fewer than %d colors",
-	     (int) temp);
+    ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp);
 
-  if (cinfo->out_color_space == CS_RGB && nc == 3) {
-    /* We provide a special policy for quantizing in RGB space.
-     * If 256 colors are requested, we allocate 8 red, 8 green, 4 blue levels;
-     * this corresponds to the common 3/3/2-bit scheme.  For other totals,
-     * the counts are set so that the number of colors allocated to each
-     * component are roughly in the proportion R 3, G 4, B 2.
-     * For low color counts, it's easier to hardwire the optimal choices
-     * than try to tweak the algorithm to generate them.
-     */
-    if (max_colors == 256) {
-      Ncolors[0] = 8;  Ncolors[1] = 8;  Ncolors[2] = 4;
-      return 256;
-    }
-    if (max_colors < 12) {
-      /* Fixed mapping for 8 colors */
-      Ncolors[0] = Ncolors[1] = Ncolors[2] = 2;
-    } else if (max_colors < 18) {
-      /* Fixed mapping for 12 colors */
-      Ncolors[0] = 2;  Ncolors[1] = 3;  Ncolors[2] = 2;
-    } else if (max_colors < 24) {
-      /* Fixed mapping for 18 colors */
-      Ncolors[0] = 3;  Ncolors[1] = 3;  Ncolors[2] = 2;
-    } else if (max_colors < 27) {
-      /* Fixed mapping for 24 colors */
-      Ncolors[0] = 3;  Ncolors[1] = 4;  Ncolors[2] = 2;
-    } else if (max_colors < 36) {
-      /* Fixed mapping for 27 colors */
-      Ncolors[0] = 3;  Ncolors[1] = 3;  Ncolors[2] = 3;
-    } else {
-      /* these weights are readily derived with a little algebra */
-      Ncolors[0] = (iroot * 266) >> 8; /* R weight is 1.0400 */
-      Ncolors[1] = (iroot * 355) >> 8; /* G weight is 1.3867 */
-      Ncolors[2] = (iroot * 177) >> 8; /* B weight is 0.6934 */
-    }
-    total_colors = Ncolors[0] * Ncolors[1] * Ncolors[2];
-    /* The above computation produces "floor" values, so we may be able to
-     * increment the count for one or more components without exceeding
-     * max_colors.  We try in the order B, G, R.
-     */
-    do {
-      changed = FALSE;
-      for (i = 2; i >= 0; i--) {
-	/* calculate new total_colors if Ncolors[i] is incremented */
-	temp = total_colors / Ncolors[i];
-	temp *= Ncolors[i]+1;	/* done in long arith to avoid oflo */
-	if (temp <= (long) max_colors) {
-	  Ncolors[i]++;		/* OK, apply the increment */
-	  total_colors = (int) temp;
-	  changed = TRUE;
-	}
-      }
-    } while (changed);		/* loop until no increment is possible */
-  } else {
-    /* For any colorspace besides RGB, treat all the components equally. */
-
-    /* Initialize to iroot color values for each component */
-    total_colors = 1;
-    for (i = 0; i < nc; i++) {
-      Ncolors[i] = iroot;
-      total_colors *= iroot;
-    }
-    /* We may be able to increment the count for one or more components without
-     * exceeding max_colors, though we know not all can be incremented.
-     */
-    for (i = 0; i < nc; i++) {
-      /* calculate new total_colors if Ncolors[i] is incremented */
-      temp = total_colors / Ncolors[i];
-      temp *= Ncolors[i]+1;	/* done in long arith to avoid oflo */
-      if (temp > (long) max_colors)
-	break;			/* won't fit, done */
-      Ncolors[i]++;		/* OK, apply the increment */
-      total_colors = (int) temp;
-    }
+  /* Initialize to iroot color values for each component */
+  total_colors = 1;
+  for (i = 0; i < nc; i++) {
+    Ncolors[i] = iroot;
+    total_colors *= iroot;
+  }
+  /* We may be able to increment the count for one or more components without
+   * exceeding max_colors, though we know not all can be incremented.
+   * In RGB colorspace, try to increment G first, then R, then B.
+   */
+  for (i = 0; i < nc; i++) {
+    j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i);
+    /* calculate new total_colors if Ncolors[j] is incremented */
+    temp = total_colors / Ncolors[j];
+    temp *= Ncolors[j]+1;	/* done in long arith to avoid oflo */
+    if (temp > (long) max_colors)
+      break;			/* won't fit, done */
+    Ncolors[j]++;		/* OK, apply the increment */
+    total_colors = (int) temp;
   }
 
   return total_colors;
@@ -240,7 +203,7 @@
 
 
 LOCAL int
-output_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
+output_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
 /* Return j'th output value, where j will range from 0 to maxj */
 /* The output values must fall in 0..MAXJSAMPLE in increasing order */
 {
@@ -254,7 +217,7 @@
 
 
 LOCAL int
-largest_input_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
+largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
 /* Return largest input value that should map to j'th output value */
 /* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
 {
@@ -264,50 +227,54 @@
 
 
 /*
- * Initialize for one-pass color quantization.
+ * Create the colormap and color index table.
+ * Also creates the ordered-dither tables, if required.
  */
 
-METHODDEF void
-color_quant_init (decompress_info_ptr cinfo)
+LOCAL void
+create_colormap (j_decompress_ptr cinfo)
 {
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  JSAMPARRAY colormap;		/* Created colormap */
+  JSAMPROW indexptr;
   int total_colors;		/* Number of distinct output colors */
-  int Ncolors[MAX_COMPONENTS];	/* # of values alloced to each component */
-  int i,j,k, nci, blksize, blkdist, ptr, val;
-
-  /* Make sure my internal arrays won't overflow */
-  if (cinfo->num_components > MAX_COMPONENTS ||
-      cinfo->color_out_comps > MAX_COMPONENTS)
-    ERREXIT1(cinfo->emethods, "Cannot quantize more than %d color components",
-	     MAX_COMPONENTS);
-  /* Make sure colormap indexes can be represented by JSAMPLEs */
-  if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
-    ERREXIT1(cinfo->emethods, "Cannot request more than %d quantized colors",
-	     MAXJSAMPLE+1);
+  int Ncolors[MAX_Q_COMPS];	/* # of values alloced to each component */
+  ODITHER_MATRIX *odither;
+  int i,j,k, nci, blksize, blkdist, ptr, val, pad;
 
   /* Select number of colors for each component */
   total_colors = select_ncolors(cinfo, Ncolors);
 
   /* Report selected color counts */
-  if (cinfo->color_out_comps == 3)
-    TRACEMS4(cinfo->emethods, 1, "Quantizing to %d = %d*%d*%d colors",
+  if (cinfo->out_color_components == 3)
+    TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
 	     total_colors, Ncolors[0], Ncolors[1], Ncolors[2]);
   else
-    TRACEMS1(cinfo->emethods, 1, "Quantizing to %d colors", total_colors);
+    TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);
+
+  /* For ordered dither, we pad the color index tables by MAXJSAMPLE in
+   * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE).
+   * This is not necessary in the other dithering modes.
+   */
+  pad = (cinfo->dither_mode == JDITHER_ORDERED) ? MAXJSAMPLE*2 : 0;
 
   /* Allocate and fill in the colormap and color index. */
   /* The colors are ordered in the map in standard row-major order, */
   /* i.e. rightmost (highest-indexed) color changes most rapidly. */
 
-  colormap = (*cinfo->emethods->alloc_small_sarray)
-		((long) total_colors, (long) cinfo->color_out_comps);
-  colorindex = (*cinfo->emethods->alloc_small_sarray)
-		((long) (MAXJSAMPLE+1), (long) cinfo->color_out_comps);
+  colormap = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components);
+  cquantize->colorindex = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     (JDIMENSION) (MAXJSAMPLE+1 + pad),
+     (JDIMENSION) cinfo->out_color_components);
 
   /* blksize is number of adjacent repeated entries for a component */
   /* blkdist is distance between groups of identical entries for a component */
   blkdist = total_colors;
 
-  for (i = 0; i < cinfo->color_out_comps; i++) {
+  for (i = 0; i < cinfo->out_color_components; i++) {
     /* fill in colormap entries for i'th color component */
     nci = Ncolors[i];		/* # of distinct values for this color */
     blksize = blkdist / nci;
@@ -323,94 +290,100 @@
     }
     blkdist = blksize;		/* blksize of this color is blkdist of next */
 
+    /* adjust colorindex pointers to provide padding at negative indexes. */
+    if (pad)
+      cquantize->colorindex[i] += MAXJSAMPLE;
+
     /* fill in colorindex entries for i'th color component */
     /* in loop, val = index of current output value, */
     /* and k = largest j that maps to current val */
+    indexptr = cquantize->colorindex[i];
     val = 0;
     k = largest_input_value(cinfo, i, 0, nci-1);
     for (j = 0; j <= MAXJSAMPLE; j++) {
       while (j > k)		/* advance val if past boundary */
 	k = largest_input_value(cinfo, i, ++val, nci-1);
       /* premultiply so that no multiplication needed in main processing */
-      colorindex[i][j] = (JSAMPLE) (val * blksize);
+      indexptr[j] = (JSAMPLE) (val * blksize);
     }
+    /* Pad at both ends if necessary */
+    if (pad)
+      for (j = 1; j <= MAXJSAMPLE; j++) {
+	indexptr[-j] = indexptr[0];
+	indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
+      }
   }
 
-  /* Pass the colormap to the output module. */
-  /* NB: the output module may continue to use the colormap until shutdown. */
+  /* Make the colormap available to the application. */
   cinfo->colormap = colormap;
   cinfo->actual_number_of_colors = total_colors;
-  (*cinfo->methods->put_color_map) (cinfo, total_colors, colormap);
 
-  /* Allocate workspace to hold one row of color-converted data */
-  input_buffer = (*cinfo->emethods->alloc_small_sarray)
-			(cinfo->image_width, (long) cinfo->color_out_comps);
-
-  /* Allocate Floyd-Steinberg workspace if necessary */
-  if (cinfo->use_dithering) {
-    size_t arraysize = (size_t) ((cinfo->image_width + 2L) * SIZEOF(FSERROR));
-
-    for (i = 0; i < cinfo->color_out_comps; i++) {
-      fserrors[i] = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
-      /* Initialize the propagated errors to zero. */
-      jzero_far((void FAR *) fserrors[i], arraysize);
+  if (cinfo->dither_mode == JDITHER_ORDERED) {
+    /* Allocate and fill in the ordered-dither tables. */
+    odither = (ODITHER_MATRIX *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+			cinfo->out_color_components * SIZEOF(ODITHER_MATRIX));
+    cquantize->odither = odither;
+    for (i = 0; i < cinfo->out_color_components; i++) {
+      nci = Ncolors[i];		/* # of distinct values for this color */
+      /* The inter-value distance for this color is MAXJSAMPLE/(nci-1).
+       * Hence the dither value for the matrix cell with fill order j
+       * (j=1..N) should be (N+1-2*j)/(2*(N+1)) * MAXJSAMPLE/(nci-1).
+       */
+      val = 2 * (ODITHER_CELLS + 1) * (nci - 1); /* denominator */
+      /* Macro is coded to ensure round towards zero despite C's
+       * lack of consistency in integer division...
+       */
+#define ODITHER_DIV(num,den)  ((num)<0 ? -((-(num))/(den)) : (num)/(den))
+#define ODITHER_VAL(j)  ODITHER_DIV((ODITHER_CELLS+1-2*j)*MAXJSAMPLE, val)
+      /* Traditional fill order for 4x4 dither; see Schumacher's figure 4. */
+      odither[0][0][0] = ODITHER_VAL(1);
+      odither[0][0][1] = ODITHER_VAL(9);
+      odither[0][0][2] = ODITHER_VAL(3);
+      odither[0][0][3] = ODITHER_VAL(11);
+      odither[0][1][0] = ODITHER_VAL(13);
+      odither[0][1][1] = ODITHER_VAL(5);
+      odither[0][1][2] = ODITHER_VAL(15);
+      odither[0][1][3] = ODITHER_VAL(7);
+      odither[0][2][0] = ODITHER_VAL(4);
+      odither[0][2][1] = ODITHER_VAL(12);
+      odither[0][2][2] = ODITHER_VAL(2);
+      odither[0][2][3] = ODITHER_VAL(10);
+      odither[0][3][0] = ODITHER_VAL(16);
+      odither[0][3][1] = ODITHER_VAL(8);
+      odither[0][3][2] = ODITHER_VAL(14);
+      odither[0][3][3] = ODITHER_VAL(6);
+      odither++;		/* advance to next matrix */
     }
-    on_odd_row = FALSE;
   }
 }
 
 
 /*
- * Subroutines for color conversion methods.
- */
-
-LOCAL void
-do_color_conversion (decompress_info_ptr cinfo, JSAMPIMAGE input_data, int row)
-/* Convert the indicated row of the input data into output colorspace */
-/* in input_buffer.  This requires a little trickery since color_convert */
-/* expects to deal with 3-D arrays; fortunately we can fake it out */
-/* at fairly low cost. */
-{
-  short ci;
-  JSAMPARRAY input_hack[MAX_COMPONENTS];
-  JSAMPARRAY output_hack[MAX_COMPONENTS];
-
-  /* create JSAMPIMAGE pointing at specified row of input_data */
-  for (ci = 0; ci < cinfo->num_components; ci++)
-    input_hack[ci] = input_data[ci] + row;
-  /* Create JSAMPIMAGE pointing at input_buffer */
-  for (ci = 0; ci < cinfo->color_out_comps; ci++)
-    output_hack[ci] = &(input_buffer[ci]);
-
-  (*cinfo->methods->color_convert) (cinfo, 1, cinfo->image_width,
-				    input_hack, output_hack);
-}
-
-
-/*
  * Map some rows of pixels to the output colormapped representation.
  */
 
 METHODDEF void
-color_quantize (decompress_info_ptr cinfo, int num_rows,
-		JSAMPIMAGE input_data, JSAMPARRAY output_data)
+color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+		JSAMPARRAY output_buf, int num_rows)
 /* General case, no dithering */
 {
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  JSAMPARRAY colorindex = cquantize->colorindex;
   register int pixcode, ci;
-  register JSAMPROW ptrout;
-  register long col;
+  register JSAMPROW ptrin, ptrout;
   int row;
-  long width = cinfo->image_width;
-  register int nc = cinfo->color_out_comps;  
+  JDIMENSION col;
+  JDIMENSION width = cinfo->output_width;
+  register int nc = cinfo->out_color_components;
 
   for (row = 0; row < num_rows; row++) {
-    do_color_conversion(cinfo, input_data, row);
-    ptrout = output_data[row];
-    for (col = 0; col < width; col++) {
+    ptrin = input_buf[row];
+    ptrout = output_buf[row];
+    for (col = width; col > 0; col--) {
       pixcode = 0;
       for (ci = 0; ci < nc; ci++) {
-	pixcode += GETJSAMPLE(colorindex[ci]
-			      [GETJSAMPLE(input_buffer[ci][col])]);
+	pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]);
       }
       *ptrout++ = (JSAMPLE) pixcode;
     }
@@ -419,29 +392,27 @@
 
 
 METHODDEF void
-color_quantize3 (decompress_info_ptr cinfo, int num_rows,
-		 JSAMPIMAGE input_data, JSAMPARRAY output_data)
-/* Fast path for color_out_comps==3, no dithering */
+color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+		 JSAMPARRAY output_buf, int num_rows)
+/* Fast path for out_color_components==3, no dithering */
 {
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
   register int pixcode;
-  register JSAMPROW ptr0, ptr1, ptr2, ptrout;
-  register long col;
+  register JSAMPROW ptrin, ptrout;
+  JSAMPROW colorindex0 = cquantize->colorindex[0];
+  JSAMPROW colorindex1 = cquantize->colorindex[1];
+  JSAMPROW colorindex2 = cquantize->colorindex[2];
   int row;
-  JSAMPROW colorindex0 = colorindex[0];
-  JSAMPROW colorindex1 = colorindex[1];
-  JSAMPROW colorindex2 = colorindex[2];
-  long width = cinfo->image_width;
+  JDIMENSION col;
+  JDIMENSION width = cinfo->output_width;
 
   for (row = 0; row < num_rows; row++) {
-    do_color_conversion(cinfo, input_data, row);
-    ptr0 = input_buffer[0];
-    ptr1 = input_buffer[1];
-    ptr2 = input_buffer[2];
-    ptrout = output_data[row];
+    ptrin = input_buf[row];
+    ptrout = output_buf[row];
     for (col = width; col > 0; col--) {
-      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptr0++)]);
-      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptr1++)]);
-      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptr2++)]);
+      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]);
+      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]);
+      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]);
       *ptrout++ = (JSAMPLE) pixcode;
     }
   }
@@ -449,10 +420,106 @@
 
 
 METHODDEF void
-color_quantize_dither (decompress_info_ptr cinfo, int num_rows,
-		       JSAMPIMAGE input_data, JSAMPARRAY output_data)
+quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+		     JSAMPARRAY output_buf, int num_rows)
+/* General case, with ordered dithering */
+{
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  register JSAMPROW input_ptr;
+  register JSAMPROW output_ptr;
+  JSAMPROW colorindex_ci;
+  int * dither;			/* points to active row of dither matrix */
+  int row_index, col_index;	/* current indexes into dither matrix */
+  int nc = cinfo->out_color_components;
+  int ci;
+  int row;
+  JDIMENSION col;
+  JDIMENSION width = cinfo->output_width;
+
+  for (row = 0; row < num_rows; row++) {
+    /* Initialize output values to 0 so can process components separately */
+    jzero_far((void FAR *) output_buf[row],
+	      (size_t) (width * SIZEOF(JSAMPLE)));
+    row_index = cquantize->row_index;
+    for (ci = 0; ci < nc; ci++) {
+      input_ptr = input_buf[row] + ci;
+      output_ptr = output_buf[row];
+      colorindex_ci = cquantize->colorindex[ci];
+      dither = cquantize->odither[ci][row_index];
+      col_index = 0;
+
+      for (col = width; col > 0; col--) {
+	/* Form pixel value + dither, range-limit to 0..MAXJSAMPLE,
+	 * select output value, accumulate into output code for this pixel.
+	 * Range-limiting need not be done explicitly, as we have extended
+	 * the colorindex table to produce the right answers for out-of-range
+	 * inputs.  The maximum dither is +- MAXJSAMPLE; this sets the
+	 * required amount of padding.
+	 */
+	*output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]];
+	input_ptr += nc;
+	output_ptr++;
+	col_index = (col_index + 1) & ODITHER_MASK;
+      }
+    }
+    /* Advance row index for next row */
+    row_index = (row_index + 1) & ODITHER_MASK;
+    cquantize->row_index = row_index;
+  }
+}
+
+
+METHODDEF void
+quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+		      JSAMPARRAY output_buf, int num_rows)
+/* Fast path for out_color_components==3, with ordered dithering */
+{
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  register int pixcode;
+  register JSAMPROW input_ptr;
+  register JSAMPROW output_ptr;
+  JSAMPROW colorindex0 = cquantize->colorindex[0];
+  JSAMPROW colorindex1 = cquantize->colorindex[1];
+  JSAMPROW colorindex2 = cquantize->colorindex[2];
+  int * dither0;		/* points to active row of dither matrix */
+  int * dither1;
+  int * dither2;
+  int row_index, col_index;	/* current indexes into dither matrix */
+  int row;
+  JDIMENSION col;
+  JDIMENSION width = cinfo->output_width;
+
+  for (row = 0; row < num_rows; row++) {
+    row_index = cquantize->row_index;
+    input_ptr = input_buf[row];
+    output_ptr = output_buf[row];
+    dither0 = cquantize->odither[0][row_index];
+    dither1 = cquantize->odither[1][row_index];
+    dither2 = cquantize->odither[2][row_index];
+    col_index = 0;
+
+    for (col = width; col > 0; col--) {
+      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) +
+					dither0[col_index]]);
+      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) +
+					dither1[col_index]]);
+      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) +
+					dither2[col_index]]);
+      *output_ptr++ = (JSAMPLE) pixcode;
+      col_index = (col_index + 1) & ODITHER_MASK;
+    }
+    row_index = (row_index + 1) & ODITHER_MASK;
+    cquantize->row_index = row_index;
+  }
+}
+
+
+METHODDEF void
+quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+		    JSAMPARRAY output_buf, int num_rows)
 /* General case, with Floyd-Steinberg dithering */
 {
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
   register LOCFSERROR cur;	/* current error or pixel value */
   LOCFSERROR belowerr;		/* error for pixel below cur */
   LOCFSERROR bpreverr;		/* error for below/prev col */
@@ -464,42 +531,44 @@
   JSAMPROW colorindex_ci;
   JSAMPROW colormap_ci;
   int pixcode;
+  int nc = cinfo->out_color_components;
   int dir;			/* 1 for left-to-right, -1 for right-to-left */
+  int dirnc;			/* dir * nc */
   int ci;
-  int nc = cinfo->color_out_comps;
   int row;
-  long col_counter;
-  long width = cinfo->image_width;
+  JDIMENSION col;
+  JDIMENSION width = cinfo->output_width;
   JSAMPLE *range_limit = cinfo->sample_range_limit;
   SHIFT_TEMPS
 
   for (row = 0; row < num_rows; row++) {
-    do_color_conversion(cinfo, input_data, row);
     /* Initialize output values to 0 so can process components separately */
-    jzero_far((void FAR *) output_data[row],
+    jzero_far((void FAR *) output_buf[row],
 	      (size_t) (width * SIZEOF(JSAMPLE)));
     for (ci = 0; ci < nc; ci++) {
-      input_ptr = input_buffer[ci];
-      output_ptr = output_data[row];
-      if (on_odd_row) {
+      input_ptr = input_buf[row] + ci;
+      output_ptr = output_buf[row];
+      if (cquantize->on_odd_row) {
 	/* work right to left in this row */
-	input_ptr += width - 1;	/* so point to rightmost pixel */
-	output_ptr += width - 1;
+	input_ptr += (width-1) * nc; /* so point to rightmost pixel */
+	output_ptr += width-1;
 	dir = -1;
-	errorptr = fserrors[ci] + (width+1); /* point to entry after last column */
+	dirnc = -nc;
+	errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
       } else {
 	/* work left to right in this row */
 	dir = 1;
-	errorptr = fserrors[ci]; /* point to entry before first real column */
+	dirnc = nc;
+	errorptr = cquantize->fserrors[ci]; /* => entry before first column */
       }
-      colorindex_ci = colorindex[ci];
-      colormap_ci = colormap[ci];
+      colorindex_ci = cquantize->colorindex[ci];
+      colormap_ci = cinfo->colormap[ci];
       /* Preset error values: no error propagated to first pixel from left */
       cur = 0;
       /* and no error propagated to row below yet */
       belowerr = bpreverr = 0;
 
-      for (col_counter = width; col_counter > 0; col_counter--) {
+      for (col = width; col > 0; col--) {
 	/* cur holds the error propagated from the previous pixel on the
 	 * current line.  Add the error propagated from the previous line
 	 * to form the complete error correction term for this pixel, and
@@ -538,7 +607,7 @@
 	 * to the next pixel on the current line, and all the errors for the
 	 * next line have been shifted over. We are therefore ready to move on.
 	 */
-	input_ptr += dir;	/* advance input ptr to next column */
+	input_ptr += dirnc;	/* advance input ptr to next column */
 	output_ptr += dir;	/* advance output ptr to next column */
 	errorptr += dir;	/* advance errorptr to current column */
       }
@@ -548,70 +617,94 @@
        */
       errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
     }
-    on_odd_row = (on_odd_row ? FALSE : TRUE);
+    cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
   }
 }
 
 
 /*
- * Finish up at the end of the file.
+ * Initialize for one-pass color quantization.
  */
 
 METHODDEF void
-color_quant_term (decompress_info_ptr cinfo)
+start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
 {
-  /* no work (we let free_all release the workspace) */
-  /* Note that we *mustn't* free the colormap before free_all, */
-  /* since output module may use it! */
+  /* no work in 1-pass case */
 }
 
 
 /*
- * Prescan some rows of pixels.
- * Not used in one-pass case.
+ * Finish up at the end of the pass.
  */
 
 METHODDEF void
-color_quant_prescan (decompress_info_ptr cinfo, int num_rows,
-		     JSAMPIMAGE image_data, JSAMPARRAY workspace)
+finish_pass_1_quant (j_decompress_ptr cinfo)
 {
-  ERREXIT(cinfo->emethods, "Should not get here!");
+  /* no work in 1-pass case */
 }
 
 
 /*
- * Do two-pass quantization.
- * Not used in one-pass case.
- */
-
-METHODDEF void
-color_quant_doit (decompress_info_ptr cinfo, quantize_caller_ptr source_method)
-{
-  ERREXIT(cinfo->emethods, "Should not get here!");
-}
-
-
-/*
- * The method selection routine for 1-pass color quantization.
+ * Module initialization routine for 1-pass color quantization.
  */
 
 GLOBAL void
-jsel1quantize (decompress_info_ptr cinfo)
+jinit_1pass_quantizer (j_decompress_ptr cinfo)
 {
-  if (! cinfo->two_pass_quantize) {
-    cinfo->methods->color_quant_init = color_quant_init;
-    if (cinfo->use_dithering) {
-      cinfo->methods->color_quantize = color_quantize_dither;
-    } else {
-      if (cinfo->color_out_comps == 3)
-	cinfo->methods->color_quantize = color_quantize3;
-      else
-	cinfo->methods->color_quantize = color_quantize;
+  my_cquantize_ptr cquantize;
+  size_t arraysize;
+  int i;
+
+  cquantize = (my_cquantize_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_cquantizer));
+  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
+  cquantize->pub.start_pass = start_pass_1_quant;
+  cquantize->pub.finish_pass = finish_pass_1_quant;
+
+  /* Make sure my internal arrays won't overflow */
+  if (cinfo->out_color_components > MAX_Q_COMPS)
+    ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
+  /* Make sure colormap indexes can be represented by JSAMPLEs */
+  if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
+    ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1);
+
+  /* Initialize for desired dithering mode. */
+  switch (cinfo->dither_mode) {
+  case JDITHER_NONE:
+    if (cinfo->out_color_components == 3)
+      cquantize->pub.color_quantize = color_quantize3;
+    else
+      cquantize->pub.color_quantize = color_quantize;
+    break;
+  case JDITHER_ORDERED:
+    if (cinfo->out_color_components == 3)
+      cquantize->pub.color_quantize = quantize3_ord_dither;
+    else
+      cquantize->pub.color_quantize = quantize_ord_dither;
+    cquantize->row_index = 0;	/* initialize state for ordered dither */
+    break;
+  case JDITHER_FS:
+    cquantize->pub.color_quantize = quantize_fs_dither;
+    cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
+    /* Allocate Floyd-Steinberg workspace if necessary. */
+    /* We do this now since it is FAR storage and may affect the memory */
+    /* manager's space calculations. */
+    arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
+    for (i = 0; i < cinfo->out_color_components; i++) {
+      cquantize->fserrors[i] = (FSERRPTR) (*cinfo->mem->alloc_large)
+	((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
+      /* Initialize the propagated errors to zero. */
+      jzero_far((void FAR *) cquantize->fserrors[i], arraysize);
     }
-    cinfo->methods->color_quant_prescan = color_quant_prescan;
-    cinfo->methods->color_quant_doit = color_quant_doit;
-    cinfo->methods->color_quant_term = color_quant_term;
+    break;
+  default:
+    ERREXIT(cinfo, JERR_NOT_COMPILED);
+    break;
   }
+
+  /* Create the colormap. */
+  create_colormap(cinfo);
 }
 
 #endif /* QUANT_1PASS_SUPPORTED */
diff --git a/jquant2.c b/jquant2.c
index 4f3b191..7984f58 100644
--- a/jquant2.c
+++ b/jquant2.c
@@ -1,16 +1,25 @@
 /*
  * jquant2.c
  *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains 2-pass color quantization (color mapping) routines.
- * These routines are invoked via the methods color_quant_prescan,
- * color_quant_doit, and color_quant_init/term.
+ * These routines provide selection of a custom color map for an image,
+ * followed by mapping of the image to that color map, with optional
+ * Floyd-Steinberg dithering.
+ * It is also possible to use just the second pass to map to an arbitrary
+ * externally-given color map.
+ *
+ * Note: ordered dithering is not supported, since there isn't any fast
+ * way to compute intercolor distances; it's unclear that ordered dither's
+ * fundamental assumptions even hold with an irregularly spaced color map.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
 
 #ifdef QUANT_2PASS_SUPPORTED
 
@@ -23,13 +32,16 @@
  *   Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
  *
  * In the first pass over the image, we accumulate a histogram showing the
- * usage count of each possible color.  (To keep the histogram to a reasonable
+ * usage count of each possible color.  To keep the histogram to a reasonable
  * size, we reduce the precision of the input; typical practice is to retain
  * 5 or 6 bits per color, so that 8 or 4 different input values are counted
- * in the same histogram cell.)  Next, the color-selection step begins with a
- * box representing the whole color space, and repeatedly splits the "largest"
- * remaining box until we have as many boxes as desired colors.  Then the mean
- * color in each remaining box becomes one of the possible output colors.
+ * in the same histogram cell.
+ *
+ * Next, the color-selection step begins with a box representing the whole
+ * color space, and repeatedly splits the "largest" remaining box until we
+ * have as many boxes as desired colors.  Then the mean color in each
+ * remaining box becomes one of the possible output colors.
+ * 
  * The second pass over the image maps each input pixel to the closest output
  * color (optionally after applying a Floyd-Steinberg dithering correction).
  * This mapping is logically trivial, but making it go fast enough requires
@@ -40,28 +52,57 @@
  * used here have proved out well in experimental comparisons, but better ones
  * may yet be found.
  *
- * The most significant difference between this quantizer and others is that
- * this one is intended to operate in YCbCr colorspace, rather than RGB space
- * as is usually done.  Actually we work in scaled YCbCr colorspace, where
- * Y distances are inflated by a factor of 2 relative to Cb or Cr distances.
- * The empirical evidence is that distances in this space correspond to
- * perceptual color differences more closely than do distances in RGB space;
- * and working in this space is inexpensive within a JPEG decompressor, since
- * the input data is already in YCbCr form.  (We could transform to an even
- * more perceptually linear space such as Lab or Luv, but that is very slow
- * and doesn't yield much better results than scaled YCbCr.)
+ * In earlier versions of the IJG code, this module quantized in YCbCr color
+ * space, processing the raw upsampled data without a color conversion step.
+ * This allowed the color conversion math to be done only once per colormap
+ * entry, not once per pixel.  However, that optimization precluded other
+ * useful optimizations (such as merging color conversion with upsampling)
+ * and it also interfered with desired capabilities such as quantizing to an
+ * externally-supplied colormap.  We have therefore abandoned that approach.
+ * The present code works in the post-conversion color space, typically RGB.
+ *
+ * To improve the visual quality of the results, we actually work in scaled
+ * RGB space, giving G distances more weight than R, and R in turn more than
+ * B.  To do everything in integer math, we must use integer scale factors.
+ * The 2/3/1 scale factors used here correspond loosely to the relative
+ * weights of the colors in the NTSC grayscale equation.
+ * If you want to use this code to quantize a non-RGB color space, you'll
+ * probably need to change these scale factors.
  */
 
-#define Y_SCALE 2		/* scale Y distances up by this much */
+#define R_SCALE 2		/* scale R distances by this much */
+#define G_SCALE 3		/* scale G distances by this much */
+#define B_SCALE 1		/* and B by this much */
 
-#define MAXNUMCOLORS  (MAXJSAMPLE+1) /* maximum size of colormap */
+/* Relabel R/G/B as components 0/1/2, respecting the RGB ordering defined
+ * in jmorecfg.h.  As the code stands, it will do the right thing for R,G,B
+ * and B,G,R orders.  If you define some other weird order in jmorecfg.h,
+ * you'll get compile errors until you extend this logic.  In that case
+ * you'll probably want to tweak the histogram sizes too.
+ */
+
+#if RGB_RED == 0
+#define C0_SCALE R_SCALE
+#endif
+#if RGB_BLUE == 0
+#define C0_SCALE B_SCALE
+#endif
+#if RGB_GREEN == 1
+#define C1_SCALE G_SCALE
+#endif
+#if RGB_RED == 2
+#define C2_SCALE R_SCALE
+#endif
+#if RGB_BLUE == 2
+#define C2_SCALE B_SCALE
+#endif
 
 
 /*
  * First we have the histogram data structure and routines for creating it.
  *
- * For work in YCbCr space, it is useful to keep more precision for Y than
- * for Cb or Cr.  We recommend keeping 6 bits for Y and 5 bits each for Cb/Cr.
+ * The number of bits of precision can be adjusted by changing these symbols.
+ * We recommend keeping 6 bits for G and 5 each for R and B.
  * If you have plenty of memory and cycles, 6 bits all around gives marginally
  * better results; if you are short of memory, 5 bits all around will save
  * some space but degrade the results.
@@ -77,119 +118,165 @@
  * Since the JPEG code is intended to run in small memory model on 80x86
  * machines, we can't just allocate the histogram in one chunk.  Instead
  * of a true 3-D array, we use a row of pointers to 2-D arrays.  Each
- * pointer corresponds to a Y value (typically 2^6 = 64 pointers) and
- * each 2-D array has 2^5^2 = 1024 or 2^6^2 = 4096 entries.  Note that
+ * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and
+ * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries.  Note that
  * on 80x86 machines, the pointer row is in near memory but the actual
  * arrays are in far memory (same arrangement as we use for image arrays).
  */
 
-#ifndef HIST_Y_BITS		/* so you can override from Makefile */
-#define HIST_Y_BITS  6		/* bits of precision in Y histogram */
-#endif
-#ifndef HIST_C_BITS		/* so you can override from Makefile */
-#define HIST_C_BITS  5		/* bits of precision in Cb/Cr histogram */
-#endif
+#define MAXNUMCOLORS  (MAXJSAMPLE+1) /* maximum size of colormap */
 
-#define HIST_Y_ELEMS  (1<<HIST_Y_BITS) /* # of elements along histogram axes */
-#define HIST_C_ELEMS  (1<<HIST_C_BITS)
-
-/* These are the amounts to shift an input value to get a histogram index.
- * For a combination 8/12 bit implementation, would need variables here...
+/* These will do the right thing for either R,G,B or B,G,R color order,
+ * but you may not like the results for other color orders.
  */
+#define HIST_C0_BITS  5		/* bits of precision in R/B histogram */
+#define HIST_C1_BITS  6		/* bits of precision in G histogram */
+#define HIST_C2_BITS  5		/* bits of precision in B/R histogram */
 
-#define Y_SHIFT  (BITS_IN_JSAMPLE-HIST_Y_BITS)
-#define C_SHIFT  (BITS_IN_JSAMPLE-HIST_C_BITS)
+/* Number of elements along histogram axes. */
+#define HIST_C0_ELEMS  (1<<HIST_C0_BITS)
+#define HIST_C1_ELEMS  (1<<HIST_C1_BITS)
+#define HIST_C2_ELEMS  (1<<HIST_C2_BITS)
+
+/* These are the amounts to shift an input value to get a histogram index. */
+#define C0_SHIFT  (BITS_IN_JSAMPLE-HIST_C0_BITS)
+#define C1_SHIFT  (BITS_IN_JSAMPLE-HIST_C1_BITS)
+#define C2_SHIFT  (BITS_IN_JSAMPLE-HIST_C2_BITS)
 
 
-typedef UINT16 histcell;	/* histogram cell; MUST be an unsigned type */
+typedef UINT16 histcell;	/* histogram cell; prefer an unsigned type */
 
 typedef histcell FAR * histptr;	/* for pointers to histogram cells */
 
-typedef histcell hist1d[HIST_C_ELEMS]; /* typedefs for the array */
-typedef hist1d FAR * hist2d;	/* type for the Y-level pointers */
+typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */
+typedef hist1d FAR * hist2d;	/* type for the 2nd-level pointers */
 typedef hist2d * hist3d;	/* type for top-level pointer */
 
-static hist3d histogram;	/* pointer to the histogram */
+
+/* Declarations for Floyd-Steinberg dithering.
+ *
+ * Errors are accumulated into the array fserrors[], at a resolution of
+ * 1/16th of a pixel count.  The error at a given pixel is propagated
+ * to its not-yet-processed neighbors using the standard F-S fractions,
+ *		...	(here)	7/16
+ *		3/16	5/16	1/16
+ * We work left-to-right on even rows, right-to-left on odd rows.
+ *
+ * We can get away with a single array (holding one row's worth of errors)
+ * by using it to store the current row's errors at pixel columns not yet
+ * processed, but the next row's errors at columns already processed.  We
+ * need only a few extra variables to hold the errors immediately around the
+ * current column.  (If we are lucky, those variables are in registers, but
+ * even if not, they're probably cheaper to access than array elements are.)
+ *
+ * The fserrors[] array has (#columns + 2) entries; the extra entry at
+ * each end saves us from special-casing the first and last pixels.
+ * Each entry is three values long, one value for each color component.
+ *
+ * Note: on a wide image, we might not have enough room in a PC's near data
+ * segment to hold the error array; so it is allocated with alloc_large.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+typedef INT16 FSERROR;		/* 16 bits should be enough */
+typedef int LOCFSERROR;		/* use 'int' for calculation temps */
+#else
+typedef INT32 FSERROR;		/* may need more than 16 bits */
+typedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
+#endif
+
+typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */
+
+
+/* Private subobject */
+
+typedef struct {
+  struct jpeg_color_quantizer pub; /* public fields */
+
+  /* Variables for accumulating image statistics */
+  hist3d histogram;		/* pointer to the histogram */
+
+  /* Variables for Floyd-Steinberg dithering */
+  FSERRPTR fserrors;		/* accumulated errors */
+  boolean on_odd_row;		/* flag to remember which row we are on */
+  int * error_limiter;		/* table for clamping the applied error */
+} my_cquantizer;
+
+typedef my_cquantizer * my_cquantize_ptr;
 
 
 /*
  * Prescan some rows of pixels.
  * In this module the prescan simply updates the histogram, which has been
- * initialized to zeroes by color_quant_init.
- * Note: workspace is probably not useful for this routine, but it is passed
- * anyway to allow some code sharing within the pipeline controller.
+ * initialized to zeroes by start_pass.
+ * An output_buf parameter is required by the method signature, but no data
+ * is actually output (in fact the buffer controller is probably passing a
+ * NULL pointer).
  */
 
 METHODDEF void
-color_quant_prescan (decompress_info_ptr cinfo, int num_rows,
-		     JSAMPIMAGE image_data, JSAMPARRAY workspace)
+prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
+		  JSAMPARRAY output_buf, int num_rows)
 {
-  register JSAMPROW ptr0, ptr1, ptr2;
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  register JSAMPROW ptr;
   register histptr histp;
-  register int c0, c1, c2;
+  register hist3d histogram = cquantize->histogram;
   int row;
-  long col;
-  long width = cinfo->image_width;
+  JDIMENSION col;
+  JDIMENSION width = cinfo->output_width;
 
   for (row = 0; row < num_rows; row++) {
-    ptr0 = image_data[0][row];
-    ptr1 = image_data[1][row];
-    ptr2 = image_data[2][row];
+    ptr = input_buf[row];
     for (col = width; col > 0; col--) {
       /* get pixel value and index into the histogram */
-      c0 = GETJSAMPLE(*ptr0++) >> Y_SHIFT;
-      c1 = GETJSAMPLE(*ptr1++) >> C_SHIFT;
-      c2 = GETJSAMPLE(*ptr2++) >> C_SHIFT;
-      histp = & histogram[c0][c1][c2];
+      histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
+			 [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
+			 [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
       /* increment, check for overflow and undo increment if so. */
-      /* We assume unsigned representation here! */
-      if (++(*histp) == 0)
+      if (++(*histp) <= 0)
 	(*histp)--;
+      ptr += 3;
     }
   }
 }
 
 
 /*
- * Now we have the really interesting routines: selection of a colormap
+ * Next we have the really interesting routines: selection of a colormap
  * given the completed histogram.
  * These routines work with a list of "boxes", each representing a rectangular
  * subset of the input color space (to histogram precision).
  */
 
 typedef struct {
-	/* The bounds of the box (inclusive); expressed as histogram indexes */
-	int c0min, c0max;
-	int c1min, c1max;
-	int c2min, c2max;
-	/* The number of nonzero histogram cells within this box */
-	long colorcount;
-      } box;
+  /* The bounds of the box (inclusive); expressed as histogram indexes */
+  int c0min, c0max;
+  int c1min, c1max;
+  int c2min, c2max;
+  /* The volume (actually 2-norm) of the box */
+  INT32 volume;
+  /* The number of nonzero histogram cells within this box */
+  long colorcount;
+} box;
+
 typedef box * boxptr;
 
-static boxptr boxlist;		/* array with room for desired # of boxes */
-static int numboxes;		/* number of boxes currently in boxlist */
-
-static JSAMPARRAY my_colormap;	/* the finished colormap (in YCbCr space) */
-
 
 LOCAL boxptr
-find_biggest_color_pop (void)
+find_biggest_color_pop (boxptr boxlist, int numboxes)
 /* Find the splittable box with the largest color population */
 /* Returns NULL if no splittable boxes remain */
 {
   register boxptr boxp;
   register int i;
-  register long max = 0;
+  register long maxc = 0;
   boxptr which = NULL;
   
   for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
-    if (boxp->colorcount > max) {
-      if (boxp->c0max > boxp->c0min || boxp->c1max > boxp->c1min ||
-	  boxp->c2max > boxp->c2min) {
-	which = boxp;
-	max = boxp->colorcount;
-      }
+    if (boxp->colorcount > maxc && boxp->volume > 0) {
+      which = boxp;
+      maxc = boxp->colorcount;
     }
   }
   return which;
@@ -197,33 +284,19 @@
 
 
 LOCAL boxptr
-find_biggest_volume (void)
+find_biggest_volume (boxptr boxlist, int numboxes)
 /* Find the splittable box with the largest (scaled) volume */
 /* Returns NULL if no splittable boxes remain */
 {
   register boxptr boxp;
   register int i;
-  register INT32 max = 0;
-  register INT32 norm, c0,c1,c2;
+  register INT32 maxv = 0;
   boxptr which = NULL;
   
-  /* We use 2-norm rather than real volume here.
-   * Some care is needed since the differences are expressed in
-   * histogram-cell units; if HIST_Y_BITS != HIST_C_BITS, we have to
-   * adjust the scaling to get the proper scaled-YCbCr-space distance.
-   * This code won't work right if HIST_Y_BITS < HIST_C_BITS,
-   * but that shouldn't ever be true.
-   * Note norm > 0 iff box is splittable, so need not check separately.
-   */
-  
   for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
-    c0 = (boxp->c0max - boxp->c0min) * Y_SCALE;
-    c1 = (boxp->c1max - boxp->c1min) << (HIST_Y_BITS-HIST_C_BITS);
-    c2 = (boxp->c2max - boxp->c2min) << (HIST_Y_BITS-HIST_C_BITS);
-    norm = c0*c0 + c1*c1 + c2*c2;
-    if (norm > max) {
+    if (boxp->volume > maxv) {
       which = boxp;
-      max = norm;
+      maxv = boxp->volume;
     }
   }
   return which;
@@ -231,13 +304,16 @@
 
 
 LOCAL void
-update_box (boxptr boxp)
+update_box (j_decompress_ptr cinfo, boxptr boxp)
 /* Shrink the min/max bounds of a box to enclose only nonzero elements, */
-/* and recompute its population */
+/* and recompute its volume and population */
 {
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  hist3d histogram = cquantize->histogram;
   histptr histp;
   int c0,c1,c2;
   int c0min,c0max,c1min,c1max,c2min,c2max;
+  INT32 dist0,dist1,dist2;
   long ccount;
   
   c0min = boxp->c0min;  c0max = boxp->c0max;
@@ -292,7 +368,7 @@
     for (c2 = c2min; c2 <= c2max; c2++)
       for (c0 = c0min; c0 <= c0max; c0++) {
 	histp = & histogram[c0][c1min][c2];
-	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C_ELEMS)
+	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
 	  if (*histp != 0) {
 	    boxp->c2min = c2min = c2;
 	    goto have_c2min;
@@ -303,13 +379,26 @@
     for (c2 = c2max; c2 >= c2min; c2--)
       for (c0 = c0min; c0 <= c0max; c0++) {
 	histp = & histogram[c0][c1min][c2];
-	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C_ELEMS)
+	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
 	  if (*histp != 0) {
 	    boxp->c2max = c2max = c2;
 	    goto have_c2max;
 	  }
       }
  have_c2max:
+
+  /* Update box volume.
+   * We use 2-norm rather than real volume here; this biases the method
+   * against making long narrow boxes, and it has the side benefit that
+   * a box is splittable iff norm > 0.
+   * Since the differences are expressed in histogram-cell units,
+   * we have to shift back to JSAMPLE units to get consistent distances;
+   * after which, we scale according to the selected distance scale factors.
+   */
+  dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
+  dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
+  dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
+  boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
   
   /* Now scan remaining volume of box and compute population */
   ccount = 0;
@@ -325,8 +414,9 @@
 }
 
 
-LOCAL void
-median_cut (int desired_colors)
+LOCAL int
+median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes,
+	    int desired_colors)
 /* Repeatedly select and split the largest box until we have enough boxes */
 {
   int n,lb;
@@ -334,12 +424,13 @@
   register boxptr b1,b2;
 
   while (numboxes < desired_colors) {
-    /* Select box to split */
-    /* Current algorithm: by population for first half, then by volume */
+    /* Select box to split.
+     * Current algorithm: by population for first half, then by volume.
+     */
     if (numboxes*2 <= desired_colors) {
-      b1 = find_biggest_color_pop();
+      b1 = find_biggest_color_pop(boxlist, numboxes);
     } else {
-      b1 = find_biggest_volume();
+      b1 = find_biggest_volume(boxlist, numboxes);
     }
     if (b1 == NULL)		/* no splittable boxes left! */
       break;
@@ -349,14 +440,23 @@
     b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
     /* Choose which axis to split the box on.
      * Current algorithm: longest scaled axis.
-     * See notes in find_biggest_volume about scaling...
+     * See notes in update_box about scaling distances.
      */
-    c0 = (b1->c0max - b1->c0min) * Y_SCALE;
-    c1 = (b1->c1max - b1->c1min) << (HIST_Y_BITS-HIST_C_BITS);
-    c2 = (b1->c2max - b1->c2min) << (HIST_Y_BITS-HIST_C_BITS);
-    cmax = c0; n = 0;
-    if (c1 > cmax) { cmax = c1; n = 1; }
+    c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
+    c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
+    c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
+    /* We want to break any ties in favor of green, then red, blue last.
+     * This code does the right thing for R,G,B or B,G,R color orders only.
+     */
+#if RGB_RED == 0
+    cmax = c1; n = 1;
+    if (c0 > cmax) { cmax = c0; n = 0; }
     if (c2 > cmax) { n = 2; }
+#else
+    cmax = c1; n = 1;
+    if (c2 > cmax) { cmax = c2; n = 2; }
+    if (c0 > cmax) { n = 0; }
+#endif
     /* Choose split point along selected axis, and update box bounds.
      * Current algorithm: split at halfway point.
      * (Since the box has been shrunk to minimum volume,
@@ -381,19 +481,22 @@
       break;
     }
     /* Update stats for boxes */
-    update_box(b1);
-    update_box(b2);
+    update_box(cinfo, b1);
+    update_box(cinfo, b2);
     numboxes++;
   }
+  return numboxes;
 }
 
 
 LOCAL void
-compute_color (boxptr boxp, int icolor)
-/* Compute representative color for a box, put it in my_colormap[icolor] */
+compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
+/* Compute representative color for a box, put it in colormap[icolor] */
 {
   /* Current algorithm: mean weighted by pixels (not colors) */
   /* Note it is important to get the rounding correct! */
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  hist3d histogram = cquantize->histogram;
   histptr histp;
   int c0,c1,c2;
   int c0min,c0max,c1min,c1max,c2min,c2max;
@@ -413,73 +516,48 @@
       for (c2 = c2min; c2 <= c2max; c2++) {
 	if ((count = *histp++) != 0) {
 	  total += count;
-	  c0total += ((c0 << Y_SHIFT) + ((1<<Y_SHIFT)>>1)) * count;
-	  c1total += ((c1 << C_SHIFT) + ((1<<C_SHIFT)>>1)) * count;
-	  c2total += ((c2 << C_SHIFT) + ((1<<C_SHIFT)>>1)) * count;
+	  c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
+	  c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
+	  c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
 	}
       }
     }
   
-  my_colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
-  my_colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
-  my_colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
+  cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
+  cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
+  cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
 }
 
 
 LOCAL void
-remap_colormap (decompress_info_ptr cinfo)
-/* Remap the internal colormap to the output colorspace */
-{
-  /* This requires a little trickery since color_convert expects to
-   * deal with 3-D arrays (a 2-D sample array for each component).
-   * We must promote the colormaps into one-row 3-D arrays.
-   */
-  short ci;
-  JSAMPARRAY input_hack[3];
-  JSAMPARRAY output_hack[10];	/* assume no more than 10 output components */
-
-  for (ci = 0; ci < 3; ci++)
-    input_hack[ci] = &(my_colormap[ci]);
-  for (ci = 0; ci < cinfo->color_out_comps; ci++)
-    output_hack[ci] = &(cinfo->colormap[ci]);
-
-  (*cinfo->methods->color_convert) (cinfo, 1,
-				    (long) cinfo->actual_number_of_colors,
-				    input_hack, output_hack);
-}
-
-
-LOCAL void
-select_colors (decompress_info_ptr cinfo)
+select_colors (j_decompress_ptr cinfo)
 /* Master routine for color selection */
 {
+  boxptr boxlist;
+  int numboxes;
   int desired = cinfo->desired_number_of_colors;
   int i;
 
   /* Allocate workspace for box list */
-  boxlist = (boxptr) (*cinfo->emethods->alloc_small) (desired * SIZEOF(box));
+  boxlist = (boxptr) (*cinfo->mem->alloc_small)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE, desired * SIZEOF(box));
   /* Initialize one box containing whole space */
   numboxes = 1;
   boxlist[0].c0min = 0;
-  boxlist[0].c0max = MAXJSAMPLE >> Y_SHIFT;
+  boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT;
   boxlist[0].c1min = 0;
-  boxlist[0].c1max = MAXJSAMPLE >> C_SHIFT;
+  boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT;
   boxlist[0].c2min = 0;
-  boxlist[0].c2max = MAXJSAMPLE >> C_SHIFT;
+  boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT;
   /* Shrink it to actually-used volume and set its statistics */
-  update_box(& boxlist[0]);
+  update_box(cinfo, & boxlist[0]);
   /* Perform median-cut to produce final box list */
-  median_cut(desired);
-  /* Compute the representative color for each box, fill my_colormap[] */
+  numboxes = median_cut(cinfo, boxlist, numboxes, desired);
+  /* Compute the representative color for each box, fill colormap */
   for (i = 0; i < numboxes; i++)
-    compute_color(& boxlist[i], i);
+    compute_color(cinfo, & boxlist[i], i);
   cinfo->actual_number_of_colors = numboxes;
-  /* Produce an output colormap in the desired output colorspace */
-  remap_colormap(cinfo);
-  TRACEMS1(cinfo->emethods, 1, "Selected %d colors for quantization",
-	   numboxes);
-  /* Done with the box list */
-  (*cinfo->emethods->free_small) ((void *) boxlist);
+  TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
 }
 
 
@@ -511,17 +589,13 @@
  * cell (because the inner loop has to be over cells, not colormap entries).
  * The work array elements have to be INT32s, so the work array would need
  * 256Kb at our recommended precision.  This is not feasible in DOS machines.
- * Another disadvantage of the brute force approach is that it computes
- * distances to every cell of the cubical histogram.  When working with YCbCr
- * input, only about a quarter of the cube represents realizable colors, so
- * many of the cells will never be used and filling them is wasted effort.
  *
  * To get around these problems, we apply Thomas' method to compute the
  * nearest colors for only the cells within a small subbox of the histogram.
  * The work array need be only as big as the subbox, so the memory usage
- * problem is solved.  A subbox is processed only when some cell in it is
- * referenced by the pass2 routines, so we will never bother with cells far
- * outside the realizable color volume.  An additional advantage of this
+ * problem is solved.  Furthermore, we need not fill subboxes that are never
+ * referenced in pass2; many images use only part of the color gamut, so a
+ * fair amount of work is saved.  An additional advantage of this
  * approach is that we can apply Heckbert's locality criterion to quickly
  * eliminate colormap entries that are far away from the subbox; typically
  * three-fourths of the colormap entries are rejected by Heckbert's criterion,
@@ -541,18 +615,18 @@
  */
 
 
-#ifndef BOX_Y_LOG		/* so you can override from Makefile */
-#define BOX_Y_LOG  (HIST_Y_BITS-3) /* log2(hist cells in update box, Y axis) */
-#endif
-#ifndef BOX_C_LOG		/* so you can override from Makefile */
-#define BOX_C_LOG  (HIST_C_BITS-3) /* log2(hist cells in update box, C axes) */
-#endif
+/* log2(histogram cells in update box) for each axis; this can be adjusted */
+#define BOX_C0_LOG  (HIST_C0_BITS-3)
+#define BOX_C1_LOG  (HIST_C1_BITS-3)
+#define BOX_C2_LOG  (HIST_C2_BITS-3)
 
-#define BOX_Y_ELEMS  (1<<BOX_Y_LOG) /* # of hist cells in update box */
-#define BOX_C_ELEMS  (1<<BOX_C_LOG)
+#define BOX_C0_ELEMS  (1<<BOX_C0_LOG) /* # of hist cells in update box */
+#define BOX_C1_ELEMS  (1<<BOX_C1_LOG)
+#define BOX_C2_ELEMS  (1<<BOX_C2_LOG)
 
-#define BOX_Y_SHIFT  (Y_SHIFT + BOX_Y_LOG)
-#define BOX_C_SHIFT  (C_SHIFT + BOX_C_LOG)
+#define BOX_C0_SHIFT  (C0_SHIFT + BOX_C0_LOG)
+#define BOX_C1_SHIFT  (C1_SHIFT + BOX_C1_LOG)
+#define BOX_C2_SHIFT  (C2_SHIFT + BOX_C2_LOG)
 
 
 /*
@@ -564,7 +638,7 @@
  */
 
 LOCAL int
-find_nearby_colors (decompress_info_ptr cinfo, int minc0, int minc1, int minc2,
+find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
 		    JSAMPLE colorlist[])
 /* Locate the colormap entries close enough to an update box to be candidates
  * for the nearest entry to some cell(s) in the update box.  The update box
@@ -588,11 +662,11 @@
    * Note that since ">>" rounds down, the "center" values may be closer to
    * min than to max; hence comparisons to them must be "<=", not "<".
    */
-  maxc0 = minc0 + ((1 << BOX_Y_SHIFT) - (1 << Y_SHIFT));
+  maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
   centerc0 = (minc0 + maxc0) >> 1;
-  maxc1 = minc1 + ((1 << BOX_C_SHIFT) - (1 << C_SHIFT));
+  maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
   centerc1 = (minc1 + maxc1) >> 1;
-  maxc2 = minc2 + ((1 << BOX_C_SHIFT) - (1 << C_SHIFT));
+  maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
   centerc2 = (minc2 + maxc2) >> 1;
 
   /* For each color in colormap, find:
@@ -602,75 +676,74 @@
    * Both of these can be found by considering only the corners of the box.
    * We save the minimum distance for each color in mindist[];
    * only the smallest maximum distance is of interest.
-   * Note we have to scale Y to get correct distance in scaled space.
    */
   minmaxdist = 0x7FFFFFFFL;
 
   for (i = 0; i < numcolors; i++) {
     /* We compute the squared-c0-distance term, then add in the other two. */
-    x = GETJSAMPLE(my_colormap[0][i]);
+    x = GETJSAMPLE(cinfo->colormap[0][i]);
     if (x < minc0) {
-      tdist = (x - minc0) * Y_SCALE;
+      tdist = (x - minc0) * C0_SCALE;
       min_dist = tdist*tdist;
-      tdist = (x - maxc0) * Y_SCALE;
+      tdist = (x - maxc0) * C0_SCALE;
       max_dist = tdist*tdist;
     } else if (x > maxc0) {
-      tdist = (x - maxc0) * Y_SCALE;
+      tdist = (x - maxc0) * C0_SCALE;
       min_dist = tdist*tdist;
-      tdist = (x - minc0) * Y_SCALE;
+      tdist = (x - minc0) * C0_SCALE;
       max_dist = tdist*tdist;
     } else {
       /* within cell range so no contribution to min_dist */
       min_dist = 0;
       if (x <= centerc0) {
-	tdist = (x - maxc0) * Y_SCALE;
+	tdist = (x - maxc0) * C0_SCALE;
 	max_dist = tdist*tdist;
       } else {
-	tdist = (x - minc0) * Y_SCALE;
+	tdist = (x - minc0) * C0_SCALE;
 	max_dist = tdist*tdist;
       }
     }
 
-    x = GETJSAMPLE(my_colormap[1][i]);
+    x = GETJSAMPLE(cinfo->colormap[1][i]);
     if (x < minc1) {
-      tdist = x - minc1;
+      tdist = (x - minc1) * C1_SCALE;
       min_dist += tdist*tdist;
-      tdist = x - maxc1;
+      tdist = (x - maxc1) * C1_SCALE;
       max_dist += tdist*tdist;
     } else if (x > maxc1) {
-      tdist = x - maxc1;
+      tdist = (x - maxc1) * C1_SCALE;
       min_dist += tdist*tdist;
-      tdist = x - minc1;
+      tdist = (x - minc1) * C1_SCALE;
       max_dist += tdist*tdist;
     } else {
       /* within cell range so no contribution to min_dist */
       if (x <= centerc1) {
-	tdist = x - maxc1;
+	tdist = (x - maxc1) * C1_SCALE;
 	max_dist += tdist*tdist;
       } else {
-	tdist = x - minc1;
+	tdist = (x - minc1) * C1_SCALE;
 	max_dist += tdist*tdist;
       }
     }
 
-    x = GETJSAMPLE(my_colormap[2][i]);
+    x = GETJSAMPLE(cinfo->colormap[2][i]);
     if (x < minc2) {
-      tdist = x - minc2;
+      tdist = (x - minc2) * C2_SCALE;
       min_dist += tdist*tdist;
-      tdist = x - maxc2;
+      tdist = (x - maxc2) * C2_SCALE;
       max_dist += tdist*tdist;
     } else if (x > maxc2) {
-      tdist = x - maxc2;
+      tdist = (x - maxc2) * C2_SCALE;
       min_dist += tdist*tdist;
-      tdist = x - minc2;
+      tdist = (x - minc2) * C2_SCALE;
       max_dist += tdist*tdist;
     } else {
       /* within cell range so no contribution to min_dist */
       if (x <= centerc2) {
-	tdist = x - maxc2;
+	tdist = (x - maxc2) * C2_SCALE;
 	max_dist += tdist*tdist;
       } else {
-	tdist = x - minc2;
+	tdist = (x - minc2) * C2_SCALE;
 	max_dist += tdist*tdist;
       }
     }
@@ -694,7 +767,7 @@
 
 
 LOCAL void
-find_best_colors (decompress_info_ptr cinfo, int minc0, int minc1, int minc2,
+find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
 		  int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
 /* Find the closest colormap entry for each cell in the update box,
  * given the list of candidate colors prepared by find_nearby_colors.
@@ -713,72 +786,74 @@
   register INT32 xx2;
   INT32 inc0, inc1, inc2;	/* initial values for increments */
   /* This array holds the distance to the nearest-so-far color for each cell */
-  INT32 bestdist[BOX_Y_ELEMS * BOX_C_ELEMS * BOX_C_ELEMS];
+  INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
 
   /* Initialize best-distance for each cell of the update box */
   bptr = bestdist;
-  for (i = BOX_Y_ELEMS*BOX_C_ELEMS*BOX_C_ELEMS-1; i >= 0; i--)
+  for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
     *bptr++ = 0x7FFFFFFFL;
   
   /* For each color selected by find_nearby_colors,
    * compute its distance to the center of each cell in the box.
    * If that's less than best-so-far, update best distance and color number.
-   * Note we have to scale Y to get correct distance in scaled space.
    */
   
   /* Nominal steps between cell centers ("x" in Thomas article) */
-#define STEP_Y  ((1 << Y_SHIFT) * Y_SCALE)
-#define STEP_C  (1 << C_SHIFT)
+#define STEP_C0  ((1 << C0_SHIFT) * C0_SCALE)
+#define STEP_C1  ((1 << C1_SHIFT) * C1_SCALE)
+#define STEP_C2  ((1 << C2_SHIFT) * C2_SCALE)
   
   for (i = 0; i < numcolors; i++) {
     icolor = GETJSAMPLE(colorlist[i]);
     /* Compute (square of) distance from minc0/c1/c2 to this color */
-    inc0 = (minc0 - (int) GETJSAMPLE(my_colormap[0][icolor])) * Y_SCALE;
+    inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE;
     dist0 = inc0*inc0;
-    inc1 = minc1 - (int) GETJSAMPLE(my_colormap[1][icolor]);
+    inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE;
     dist0 += inc1*inc1;
-    inc2 = minc2 - (int) GETJSAMPLE(my_colormap[2][icolor]);
+    inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE;
     dist0 += inc2*inc2;
     /* Form the initial difference increments */
-    inc0 = inc0 * (2 * STEP_Y) + STEP_Y * STEP_Y;
-    inc1 = inc1 * (2 * STEP_C) + STEP_C * STEP_C;
-    inc2 = inc2 * (2 * STEP_C) + STEP_C * STEP_C;
+    inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
+    inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
+    inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
     /* Now loop over all cells in box, updating distance per Thomas method */
     bptr = bestdist;
     cptr = bestcolor;
     xx0 = inc0;
-    for (ic0 = BOX_Y_ELEMS-1; ic0 >= 0; ic0--) {
+    for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
       dist1 = dist0;
       xx1 = inc1;
-      for (ic1 = BOX_C_ELEMS-1; ic1 >= 0; ic1--) {
+      for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
 	dist2 = dist1;
 	xx2 = inc2;
-	for (ic2 = BOX_C_ELEMS-1; ic2 >= 0; ic2--) {
+	for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
 	  if (dist2 < *bptr) {
 	    *bptr = dist2;
 	    *cptr = (JSAMPLE) icolor;
 	  }
 	  dist2 += xx2;
-	  xx2 += 2 * STEP_C * STEP_C;
+	  xx2 += 2 * STEP_C2 * STEP_C2;
 	  bptr++;
 	  cptr++;
 	}
 	dist1 += xx1;
-	xx1 += 2 * STEP_C * STEP_C;
+	xx1 += 2 * STEP_C1 * STEP_C1;
       }
       dist0 += xx0;
-      xx0 += 2 * STEP_Y * STEP_Y;
+      xx0 += 2 * STEP_C0 * STEP_C0;
     }
   }
 }
 
 
 LOCAL void
-fill_inverse_cmap (decompress_info_ptr cinfo, int c0, int c1, int c2)
+fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
 /* Fill the inverse-colormap entries in the update box that contains */
 /* histogram cell c0/c1/c2.  (Only that one cell MUST be filled, but */
 /* we can fill as many others as we wish.) */
 {
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  hist3d histogram = cquantize->histogram;
   int minc0, minc1, minc2;	/* lower left corner of update box */
   int ic0, ic1, ic2;
   register JSAMPLE * cptr;	/* pointer into bestcolor[] array */
@@ -787,20 +862,20 @@
   JSAMPLE colorlist[MAXNUMCOLORS];
   int numcolors;		/* number of candidate colors */
   /* This array holds the actually closest colormap index for each cell. */
-  JSAMPLE bestcolor[BOX_Y_ELEMS * BOX_C_ELEMS * BOX_C_ELEMS];
+  JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
 
   /* Convert cell coordinates to update box ID */
-  c0 >>= BOX_Y_LOG;
-  c1 >>= BOX_C_LOG;
-  c2 >>= BOX_C_LOG;
+  c0 >>= BOX_C0_LOG;
+  c1 >>= BOX_C1_LOG;
+  c2 >>= BOX_C2_LOG;
 
   /* Compute true coordinates of update box's origin corner.
    * Actually we compute the coordinates of the center of the corner
    * histogram cell, which are the lower bounds of the volume we care about.
    */
-  minc0 = (c0 << BOX_Y_SHIFT) + ((1 << Y_SHIFT) >> 1);
-  minc1 = (c1 << BOX_C_SHIFT) + ((1 << C_SHIFT) >> 1);
-  minc2 = (c2 << BOX_C_SHIFT) + ((1 << C_SHIFT) >> 1);
+  minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
+  minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
+  minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
   
   /* Determine which colormap entries are close enough to be candidates
    * for the nearest entry to some cell in the update box.
@@ -812,14 +887,14 @@
 		   bestcolor);
 
   /* Save the best color numbers (plus 1) in the main cache array */
-  c0 <<= BOX_Y_LOG;		/* convert ID back to base cell indexes */
-  c1 <<= BOX_C_LOG;
-  c2 <<= BOX_C_LOG;
+  c0 <<= BOX_C0_LOG;		/* convert ID back to base cell indexes */
+  c1 <<= BOX_C1_LOG;
+  c2 <<= BOX_C2_LOG;
   cptr = bestcolor;
-  for (ic0 = 0; ic0 < BOX_Y_ELEMS; ic0++) {
-    for (ic1 = 0; ic1 < BOX_C_ELEMS; ic1++) {
+  for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
+    for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
       cachep = & histogram[c0+ic0][c1+ic1][c2];
-      for (ic2 = 0; ic2 < BOX_C_ELEMS; ic2++) {
+      for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
 	*cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
       }
     }
@@ -828,37 +903,31 @@
 
 
 /*
- * These routines perform second-pass scanning of the image: map each pixel to
- * the proper colormap index, and output the indexes to the output file.
- *
- * output_workspace is a one-component array of pixel dimensions at least
- * as large as the input image strip; it can be used to hold the converted
- * pixels' colormap indexes.
+ * Map some rows of pixels to the output colormapped representation.
  */
 
 METHODDEF void
-pass2_nodither (decompress_info_ptr cinfo, int num_rows,
-		JSAMPIMAGE image_data, JSAMPARRAY output_workspace)
+pass2_no_dither (j_decompress_ptr cinfo,
+		 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
 /* This version performs no dithering */
 {
-  register JSAMPROW ptr0, ptr1, ptr2, outptr;
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  hist3d histogram = cquantize->histogram;
+  register JSAMPROW inptr, outptr;
   register histptr cachep;
   register int c0, c1, c2;
   int row;
-  long col;
-  long width = cinfo->image_width;
+  JDIMENSION col;
+  JDIMENSION width = cinfo->output_width;
 
-  /* Convert data to colormap indexes, which we save in output_workspace */
   for (row = 0; row < num_rows; row++) {
-    ptr0 = image_data[0][row];
-    ptr1 = image_data[1][row];
-    ptr2 = image_data[2][row];
-    outptr = output_workspace[row];
+    inptr = input_buf[row];
+    outptr = output_buf[row];
     for (col = width; col > 0; col--) {
       /* get pixel value and index into the cache */
-      c0 = GETJSAMPLE(*ptr0++) >> Y_SHIFT;
-      c1 = GETJSAMPLE(*ptr1++) >> C_SHIFT;
-      c2 = GETJSAMPLE(*ptr2++) >> C_SHIFT;
+      c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
+      c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
+      c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT;
       cachep = & histogram[c0][c1][c2];
       /* If we have not seen this color before, find nearest colormap entry */
       /* and update the cache */
@@ -868,94 +937,52 @@
       *outptr++ = (JSAMPLE) (*cachep - 1);
     }
   }
-  /* Emit converted rows to the output file */
-  (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, &output_workspace);
 }
 
 
-/* Declarations for Floyd-Steinberg dithering.
- *
- * Errors are accumulated into the array fserrors[], at a resolution of
- * 1/16th of a pixel count.  The error at a given pixel is propagated
- * to its not-yet-processed neighbors using the standard F-S fractions,
- *		...	(here)	7/16
- *		3/16	5/16	1/16
- * We work left-to-right on even rows, right-to-left on odd rows.
- *
- * We can get away with a single array (holding one row's worth of errors)
- * by using it to store the current row's errors at pixel columns not yet
- * processed, but the next row's errors at columns already processed.  We
- * need only a few extra variables to hold the errors immediately around the
- * current column.  (If we are lucky, those variables are in registers, but
- * even if not, they're probably cheaper to access than array elements are.)
- *
- * The fserrors[] array has (#columns + 2) entries; the extra entry at
- * each end saves us from special-casing the first and last pixels.
- * Each entry is three values long, one value for each color component.
- *
- * Note: on a wide image, we might not have enough room in a PC's near data
- * segment to hold the error array; so it is allocated with alloc_medium.
- */
-
-#ifdef EIGHT_BIT_SAMPLES
-typedef INT16 FSERROR;		/* 16 bits should be enough */
-typedef int LOCFSERROR;		/* use 'int' for calculation temps */
-#else
-typedef INT32 FSERROR;		/* may need more than 16 bits */
-typedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
-#endif
-
-typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */
-
-static FSERRPTR fserrors;	/* accumulated errors */
-static boolean on_odd_row;	/* flag to remember which row we are on */
-
-
 METHODDEF void
-pass2_dither (decompress_info_ptr cinfo, int num_rows,
-	      JSAMPIMAGE image_data, JSAMPARRAY output_workspace)
+pass2_fs_dither (j_decompress_ptr cinfo,
+		 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
 /* This version performs Floyd-Steinberg dithering */
 {
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  hist3d histogram = cquantize->histogram;
   register LOCFSERROR cur0, cur1, cur2;	/* current error or pixel value */
   LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
   LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
   register FSERRPTR errorptr;	/* => fserrors[] at column before current */
-  JSAMPROW ptr0, ptr1, ptr2;	/* => current input pixel */
+  JSAMPROW inptr;		/* => current input pixel */
   JSAMPROW outptr;		/* => current output pixel */
   histptr cachep;
   int dir;			/* +1 or -1 depending on direction */
-  int dir3;			/* 3*dir, for advancing errorptr */
+  int dir3;			/* 3*dir, for advancing inptr & errorptr */
   int row;
-  long col;
-  long width = cinfo->image_width;
+  JDIMENSION col;
+  JDIMENSION width = cinfo->output_width;
   JSAMPLE *range_limit = cinfo->sample_range_limit;
-  JSAMPROW colormap0 = my_colormap[0];
-  JSAMPROW colormap1 = my_colormap[1];
-  JSAMPROW colormap2 = my_colormap[2];
+  int *error_limit = cquantize->error_limiter;
+  JSAMPROW colormap0 = cinfo->colormap[0];
+  JSAMPROW colormap1 = cinfo->colormap[1];
+  JSAMPROW colormap2 = cinfo->colormap[2];
   SHIFT_TEMPS
 
-  /* Convert data to colormap indexes, which we save in output_workspace */
   for (row = 0; row < num_rows; row++) {
-    ptr0 = image_data[0][row];
-    ptr1 = image_data[1][row];
-    ptr2 = image_data[2][row];
-    outptr = output_workspace[row];
-    if (on_odd_row) {
+    inptr = input_buf[row];
+    outptr = output_buf[row];
+    if (cquantize->on_odd_row) {
       /* work right to left in this row */
-      ptr0 += width - 1;	/* so point to rightmost pixel */
-      ptr1 += width - 1;
-      ptr2 += width - 1;
-      outptr += width - 1;
+      inptr += (width-1) * 3;	/* so point to rightmost pixel */
+      outptr += width-1;
       dir = -1;
       dir3 = -3;
-      errorptr = fserrors + (width+1)*3; /* point to entry after last column */
-      on_odd_row = FALSE;	/* flip for next time */
+      errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
+      cquantize->on_odd_row = FALSE; /* flip for next time */
     } else {
       /* work left to right in this row */
       dir = 1;
       dir3 = 3;
-      errorptr = fserrors;	/* point to entry before first real column */
-      on_odd_row = TRUE;	/* flip for next time */
+      errorptr = cquantize->fserrors; /* => entry before first real column */
+      cquantize->on_odd_row = TRUE; /* flip for next time */
     }
     /* Preset error values: no error propagated to first pixel from left */
     cur0 = cur1 = cur2 = 0;
@@ -975,22 +1002,28 @@
       cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
       cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
       cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
-      /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
-       * The maximum error is +- MAXJSAMPLE; this sets the required size
-       * of the range_limit array.
+      /* Limit the error using transfer function set by init_error_limit.
+       * See comments with init_error_limit for rationale.
        */
-      cur0 += GETJSAMPLE(*ptr0);
-      cur1 += GETJSAMPLE(*ptr1);
-      cur2 += GETJSAMPLE(*ptr2);
+      cur0 = error_limit[cur0];
+      cur1 = error_limit[cur1];
+      cur2 = error_limit[cur2];
+      /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
+       * The maximum error is +- MAXJSAMPLE (or less with error limiting);
+       * this sets the required size of the range_limit array.
+       */
+      cur0 += GETJSAMPLE(inptr[0]);
+      cur1 += GETJSAMPLE(inptr[1]);
+      cur2 += GETJSAMPLE(inptr[2]);
       cur0 = GETJSAMPLE(range_limit[cur0]);
       cur1 = GETJSAMPLE(range_limit[cur1]);
       cur2 = GETJSAMPLE(range_limit[cur2]);
       /* Index into the cache with adjusted pixel value */
-      cachep = & histogram[cur0 >> Y_SHIFT][cur1 >> C_SHIFT][cur2 >> C_SHIFT];
+      cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
       /* If we have not seen this color before, find nearest colormap */
       /* entry and update the cache */
       if (*cachep == 0)
-	fill_inverse_cmap(cinfo, cur0>>Y_SHIFT, cur1>>C_SHIFT, cur2>>C_SHIFT);
+	fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
       /* Now emit the colormap index for this cell */
       { register int pixcode = *cachep - 1;
 	*outptr = (JSAMPLE) pixcode;
@@ -1034,9 +1067,7 @@
        * to the next pixel on the current line, and all the errors for the
        * next line have been shifted over.  We are therefore ready to move on.
        */
-      ptr0 += dir;		/* Advance pixel pointers to next column */
-      ptr1 += dir;
-      ptr2 += dir;
+      inptr += dir3;		/* Advance pixel pointers to next column */
       outptr += dir;
       errorptr += dir3;		/* advance errorptr to current column */
     }
@@ -1048,149 +1079,174 @@
     errorptr[1] = (FSERROR) bpreverr1;
     errorptr[2] = (FSERROR) bpreverr2;
   }
-  /* Emit converted rows to the output file */
-  (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, &output_workspace);
 }
 
 
 /*
- * Initialize for two-pass color quantization.
+ * Initialize the error-limiting transfer function (lookup table).
+ * The raw F-S error computation can potentially compute error values of up to
+ * +- MAXJSAMPLE.  But we want the maximum correction applied to a pixel to be
+ * much less, otherwise obviously wrong pixels will be created.  (Typical
+ * effects include weird fringes at color-area boundaries, isolated bright
+ * pixels in a dark area, etc.)  The standard advice for avoiding this problem
+ * is to ensure that the "corners" of the color cube are allocated as output
+ * colors; then repeated errors in the same direction cannot cause cascading
+ * error buildup.  However, that only prevents the error from getting
+ * completely out of hand; Aaron Giles reports that error limiting improves
+ * the results even with corner colors allocated.
+ * A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty
+ * well, but the smoother transfer function used below is even better.  Thanks
+ * to Aaron Giles for this idea.
  */
 
-METHODDEF void
-color_quant_init (decompress_info_ptr cinfo)
+LOCAL void
+init_error_limit (j_decompress_ptr cinfo)
+/* Allocate and fill in the error_limiter table */
 {
-  int i;
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  int * table;
+  int in, out;
 
-  /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
-  if (cinfo->desired_number_of_colors < 8)
-    ERREXIT(cinfo->emethods, "Cannot request less than 8 quantized colors");
-  /* Make sure colormap indexes can be represented by JSAMPLEs */
-  if (cinfo->desired_number_of_colors > MAXNUMCOLORS)
-    ERREXIT1(cinfo->emethods, "Cannot request more than %d quantized colors",
-	     MAXNUMCOLORS);
+  table = (int *) (*cinfo->mem->alloc_small)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int));
+  table += MAXJSAMPLE;		/* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
+  cquantize->error_limiter = table;
 
-  /* Allocate and zero the histogram */
-  histogram = (hist3d) (*cinfo->emethods->alloc_small)
-				(HIST_Y_ELEMS * SIZEOF(hist2d));
-  for (i = 0; i < HIST_Y_ELEMS; i++) {
-    histogram[i] = (hist2d) (*cinfo->emethods->alloc_medium)
-				(HIST_C_ELEMS*HIST_C_ELEMS * SIZEOF(histcell));
-    jzero_far((void FAR *) histogram[i],
-	      HIST_C_ELEMS*HIST_C_ELEMS * SIZEOF(histcell));
+#define STEPSIZE ((MAXJSAMPLE+1)/16)
+  /* Map errors 1:1 up to +- MAXJSAMPLE/16 */
+  out = 0;
+  for (in = 0; in < STEPSIZE; in++, out++) {
+    table[in] = out; table[-in] = -out;
   }
-
-  /* Allocate storage for the internal and external colormaps. */
-  /* We do this now since it is FAR storage and may affect the memory */
-  /* manager's space calculations. */
-  my_colormap = (*cinfo->emethods->alloc_small_sarray)
-			((long) cinfo->desired_number_of_colors,
-			 (long) 3);
-  cinfo->colormap = (*cinfo->emethods->alloc_small_sarray)
-			((long) cinfo->desired_number_of_colors,
-			 (long) cinfo->color_out_comps);
-
-  /* Allocate Floyd-Steinberg workspace if necessary */
-  /* This isn't needed until pass 2, but again it is FAR storage. */
-  if (cinfo->use_dithering) {
-    size_t arraysize = (size_t) ((cinfo->image_width + 2L) *
-				 (3 * SIZEOF(FSERROR)));
-
-    fserrors = (FSERRPTR) (*cinfo->emethods->alloc_medium) (arraysize);
-    /* Initialize the propagated errors to zero. */
-    jzero_far((void FAR *) fserrors, arraysize);
-    on_odd_row = FALSE;
+  /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
+  for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
+    table[in] = out; table[-in] = -out;
   }
-
-  /* Indicate number of passes needed, excluding the prescan pass. */
-  cinfo->total_passes++;	/* I always use one pass */
+  /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */
+  for (; in <= MAXJSAMPLE; in++) {
+    table[in] = out; table[-in] = -out;
+  }
+#undef STEPSIZE
 }
 
 
 /*
- * Perform two-pass quantization: rescan the image data and output the
- * converted data via put_color_map and put_pixel_rows.
- * The source_method is a routine that can scan the image data; it can
- * be called as many times as desired.  The processing routine called by
- * source_method has the same interface as color_quantize does in the
- * one-pass case, except it must call put_pixel_rows itself.  (This allows
- * me to use multiple passes in which earlier passes don't output anything.)
+ * Finish up at the end of each pass.
  */
 
 METHODDEF void
-color_quant_doit (decompress_info_ptr cinfo, quantize_caller_ptr source_method)
+finish_pass1 (j_decompress_ptr cinfo)
 {
-  int i;
-
-  /* Select the representative colors */
+  /* Select the representative colors and fill in cinfo->colormap */
   select_colors(cinfo);
-  /* Pass the external colormap to the output module. */
-  /* NB: the output module may continue to use the colormap until shutdown. */
-  (*cinfo->methods->put_color_map) (cinfo, cinfo->actual_number_of_colors,
-				    cinfo->colormap);
-  /* Re-zero the histogram so pass 2 can use it as nearest-color cache */
-  for (i = 0; i < HIST_Y_ELEMS; i++) {
-    jzero_far((void FAR *) histogram[i],
-	      HIST_C_ELEMS*HIST_C_ELEMS * SIZEOF(histcell));
+}
+
+
+METHODDEF void
+finish_pass2 (j_decompress_ptr cinfo)
+{
+  /* no work */
+}
+
+
+/*
+ * Initialize for each processing pass.
+ */
+
+METHODDEF void
+start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
+{
+  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
+  hist3d histogram = cquantize->histogram;
+  int i;
+
+  if (is_pre_scan) {
+    /* Set up method pointers */
+    cquantize->pub.color_quantize = prescan_quantize;
+    cquantize->pub.finish_pass = finish_pass1;
+  } else {
+    /* Set up method pointers */
+    if (cinfo->dither_mode == JDITHER_FS)
+      cquantize->pub.color_quantize = pass2_fs_dither;
+    else
+      cquantize->pub.color_quantize = pass2_no_dither;
+    cquantize->pub.finish_pass = finish_pass2;
   }
-  /* Perform pass 2 */
-  if (cinfo->use_dithering)
-    (*source_method) (cinfo, pass2_dither);
-  else
-    (*source_method) (cinfo, pass2_nodither);
+  /* Zero the histogram or inverse color map */
+  for (i = 0; i < HIST_C0_ELEMS; i++) {
+    jzero_far((void FAR *) histogram[i],
+	      HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
+  }
 }
 
 
 /*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-color_quant_term (decompress_info_ptr cinfo)
-{
-  /* no work (we let free_all release the histogram/cache and colormaps) */
-  /* Note that we *mustn't* free the external colormap before free_all, */
-  /* since output module may use it! */
-}
-
-
-/*
- * Map some rows of pixels to the output colormapped representation.
- * Not used in two-pass case.
- */
-
-METHODDEF void
-color_quantize (decompress_info_ptr cinfo, int num_rows,
-		JSAMPIMAGE input_data, JSAMPARRAY output_data)
-{
-  ERREXIT(cinfo->emethods, "Should not get here!");
-}
-
-
-/*
- * The method selection routine for 2-pass color quantization.
+ * Module initialization routine for 2-pass color quantization.
  */
 
 GLOBAL void
-jsel2quantize (decompress_info_ptr cinfo)
+jinit_2pass_quantizer (j_decompress_ptr cinfo)
 {
-  if (cinfo->two_pass_quantize) {
-    /* Make sure jdmaster didn't give me a case I can't handle */
-    if (cinfo->num_components != 3 || cinfo->jpeg_color_space != CS_YCbCr)
-      ERREXIT(cinfo->emethods, "2-pass quantization only handles YCbCr input");
-    cinfo->methods->color_quant_init = color_quant_init;
-    cinfo->methods->color_quant_prescan = color_quant_prescan;
-    cinfo->methods->color_quant_doit = color_quant_doit;
-    cinfo->methods->color_quant_term = color_quant_term;
-    cinfo->methods->color_quantize = color_quantize;
-    /* Quantized grayscale output is normally done by jquant1.c (which will do
-     * a much better job of it).  But if the program is configured with only
-     * 2-pass quantization, then I have to do the job.  In this situation,
-     * jseldcolor's clearing of the Cb/Cr component_needed flags is incorrect,
-     * because I will look at those components before conversion.
-     */
-    cinfo->cur_comp_info[1]->component_needed = TRUE;
-    cinfo->cur_comp_info[2]->component_needed = TRUE;
+  my_cquantize_ptr cquantize;
+  int i;
+
+  cquantize = (my_cquantize_ptr)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				SIZEOF(my_cquantizer));
+  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
+  cquantize->pub.start_pass = start_pass_2_quant;
+
+  /* Make sure jdmaster didn't give me a case I can't handle */
+  if (cinfo->out_color_components != 3)
+    ERREXIT(cinfo, JERR_NOTIMPL);
+
+  /* Only F-S dithering or no dithering is supported. */
+  /* If user asks for ordered dither, give him F-S. */
+  if (cinfo->dither_mode != JDITHER_NONE)
+    cinfo->dither_mode = JDITHER_FS;
+
+  /* Make sure color count is acceptable */
+  i = (cinfo->colormap != NULL) ? cinfo->actual_number_of_colors
+				: cinfo->desired_number_of_colors;
+  /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
+  if (i < 8)
+    ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
+  /* Make sure colormap indexes can be represented by JSAMPLEs */
+  if (i > MAXNUMCOLORS)
+    ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
+
+  /* Allocate the histogram/inverse colormap storage */
+  cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d));
+  for (i = 0; i < HIST_C0_ELEMS; i++) {
+    cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
+  }
+
+  /* Allocate storage for the completed colormap,
+   * unless it has been supplied by the application.
+   * We do this now since it is FAR storage and may affect
+   * the memory manager's space calculations.
+   */
+  if (cinfo->colormap == NULL) {
+    cinfo->colormap = (*cinfo->mem->alloc_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       (JDIMENSION) cinfo->desired_number_of_colors, (JDIMENSION) 3);
+  }
+
+  /* Allocate Floyd-Steinberg workspace if necessary. */
+  /* This isn't needed until pass 2, but again it is FAR storage. */
+  if (cinfo->dither_mode == JDITHER_FS) {
+    size_t arraysize = (size_t) ((cinfo->output_width + 2) *
+				 (3 * SIZEOF(FSERROR)));
+
+    cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
+    /* Initialize the propagated errors to zero. */
+    jzero_far((void FAR *) cquantize->fserrors, arraysize);
+    cquantize->on_odd_row = FALSE;
+    init_error_limit(cinfo);
   }
 }
 
diff --git a/jrdgif.c b/jrdgif.c
deleted file mode 100644
index 4629bf1..0000000
--- a/jrdgif.c
+++ /dev/null
@@ -1,630 +0,0 @@
-/*
- * jrdgif.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to read input images in GIF format.
- *
- * These routines may need modification for non-Unix environments or
- * specialized applications.  As they stand, they assume input from
- * an ordinary stdio stream.  They further assume that reading begins
- * at the start of the file; input_init may need work if the
- * user interface has already read some data (e.g., to determine that
- * the file is indeed GIF format).
- *
- * These routines are invoked via the methods get_input_row
- * and input_init/term.
- */
-
-/*
- * This code is loosely based on giftoppm from the PBMPLUS distribution
- * of Feb. 1991.  That file contains the following copyright notice:
- * +-------------------------------------------------------------------+
- * | Copyright 1990, David Koblas.                                     |
- * |   Permission to use, copy, modify, and distribute this software   |
- * |   and its documentation for any purpose and without fee is hereby |
- * |   granted, provided that the above copyright notice appear in all |
- * |   copies and that both that copyright notice and this permission  |
- * |   notice appear in supporting documentation.  This software is    |
- * |   provided "as is" without express or implied warranty.           |
- * +-------------------------------------------------------------------+
- *
- * We are also required to state that
- *    "The Graphics Interchange Format(c) is the Copyright property of
- *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
- *    CompuServe Incorporated."
- */
-
-#include "jinclude.h"
-
-#ifdef GIF_SUPPORTED
-
-
-#define	MAXCOLORMAPSIZE	256	/* max # of colors in a GIF colormap */
-#define NUMCOLORS	3	/* # of colors */
-#define CM_RED		0	/* color component numbers */
-#define CM_GREEN	1
-#define CM_BLUE		2
-
-static JSAMPARRAY colormap;	/* the colormap to use */
-/* colormap[i][j] = value of i'th color component for pixel value j */
-
-#define	MAX_LZW_BITS	12	/* maximum LZW code size */
-#define LZW_TABLE_SIZE	(1<<MAX_LZW_BITS) /* # of possible LZW symbols */
-
-/* Macros for extracting header data --- note we assume chars may be signed */
-
-#define LM_to_uint(a,b)		((((b)&0xFF) << 8) | ((a)&0xFF))
-
-#define BitSet(byte, bit)	((byte) & (bit))
-#define INTERLACE	0x40	/* mask for bit signifying interlaced image */
-#define COLORMAPFLAG	0x80	/* mask for bit signifying colormap presence */
-
-#define	ReadOK(file,buffer,len)	(JFREAD(file,buffer,len) == ((size_t) (len)))
-
-/* Static vars for GetCode and LZWReadByte */
-
-static char code_buf[256+4];	/* current input data block */
-static int last_byte;		/* # of bytes in code_buf */
-static int last_bit;		/* # of bits in code_buf */
-static int cur_bit;		/* next bit index to read */
-static boolean out_of_blocks;	/* TRUE if hit terminator data block */
-
-static int input_code_size;	/* codesize given in GIF file */
-static int clear_code,end_code; /* values for Clear and End codes */
-
-static int code_size;		/* current actual code size */
-static int limit_code;		/* 2^code_size */
-static int max_code;		/* first unused code value */
-static boolean first_time;	/* flags first call to LZWReadByte */
-
-/* LZW decompression tables:
- *   symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1)
- *   symbol_tail[K] = suffix byte   of any LZW symbol K (0..LZW_TABLE_SIZE-1)
- * Note that entries 0..end_code of the above tables are not used,
- * since those symbols represent raw bytes or special codes.
- *
- * The stack represents the not-yet-used expansion of the last LZW symbol.
- * In the worst case, a symbol could expand to as many bytes as there are
- * LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack.
- * (This is conservative since that number includes the raw-byte symbols.)
- *
- * The tables are allocated from FAR heap space since they would use up
- * rather a lot of the near data space in a PC.
- */
-
-static UINT16 FAR *symbol_head; /* => table of prefix symbols */
-static UINT8  FAR *symbol_tail; /* => table of suffix bytes */
-static UINT8  FAR *symbol_stack; /* stack for symbol expansions */
-static UINT8  FAR *sp;		/* stack pointer */
-
-/* Static state for interlaced image processing */
-
-static boolean is_interlaced;	/* TRUE if have interlaced image */
-static big_sarray_ptr interlaced_image;	/* full image in interlaced order */
-static long cur_row_number;	/* need to know actual row number */
-static long pass2_offset;	/* # of pixel rows in pass 1 */
-static long pass3_offset;	/* # of pixel rows in passes 1&2 */
-static long pass4_offset;	/* # of pixel rows in passes 1,2,3 */
-
-
-/* Forward declarations */
-METHODDEF void load_interlaced_image PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
-METHODDEF void get_interlaced_row PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
-
-
-
-LOCAL int
-ReadByte (compress_info_ptr cinfo)
-/* Read next byte from GIF file */
-{
-  register FILE * infile = cinfo->input_file;
-  int c;
-
-  if ((c = getc(infile)) == EOF)
-    ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
-  return c;
-}
-
-
-LOCAL int
-GetDataBlock (compress_info_ptr cinfo, char *buf)
-/* Read a GIF data block, which has a leading count byte */
-/* A zero-length block marks the end of a data block sequence */
-{
-  int count;
-
-  count = ReadByte(cinfo);
-  if (count > 0) {
-    if (! ReadOK(cinfo->input_file, buf, count))
-      ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
-  }
-  return count;
-}
-
-
-LOCAL void
-SkipDataBlocks (compress_info_ptr cinfo)
-/* Skip a series of data blocks, until a block terminator is found */
-{
-  char buf[256];
-
-  while (GetDataBlock(cinfo, buf) > 0)
-    /* skip */;
-}
-
-
-LOCAL void
-ReInitLZW (void)
-/* (Re)initialize LZW state; shared code for startup and Clear processing */
-{
-  code_size = input_code_size+1;
-  limit_code = clear_code << 1;	/* 2^code_size */
-  max_code = clear_code + 2;	/* first unused code value */
-  sp = symbol_stack;		/* init stack to empty */
-}
-
-
-LOCAL void
-InitLZWCode (void)
-/* Initialize for a series of LZWReadByte (and hence GetCode) calls */
-{
-  /* GetCode initialization */
-  last_byte = 2;		/* make safe to "recopy last two bytes" */
-  last_bit = 0;			/* nothing in the buffer */
-  cur_bit = 0;			/* force buffer load on first call */
-  out_of_blocks = FALSE;
-
-  /* LZWReadByte initialization */
-  clear_code = 1 << input_code_size; /* compute special code values */
-  end_code = clear_code + 1;	/* note that these do not change */
-  first_time = TRUE;
-  ReInitLZW();
-}
-
-
-LOCAL int
-GetCode (compress_info_ptr cinfo)
-/* Fetch the next code_size bits from the GIF data */
-/* We assume code_size is less than 16 */
-{
-  register INT32 accum;
-  int offs, ret, count;
-
-  if ( (cur_bit+code_size) > last_bit) {
-    /* Time to reload the buffer */
-    if (out_of_blocks) {
-      WARNMS(cinfo->emethods, "Ran out of GIF bits");
-      return end_code;		/* fake something useful */
-    }
-    /* preserve last two bytes of what we have -- assume code_size <= 16 */
-    code_buf[0] = code_buf[last_byte-2];
-    code_buf[1] = code_buf[last_byte-1];
-    /* Load more bytes; set flag if we reach the terminator block */
-    if ((count = GetDataBlock(cinfo, &code_buf[2])) == 0) {
-      out_of_blocks = TRUE;
-      WARNMS(cinfo->emethods, "Ran out of GIF bits");
-      return end_code;		/* fake something useful */
-    }
-    /* Reset counters */
-    cur_bit = (cur_bit - last_bit) + 16;
-    last_byte = 2 + count;
-    last_bit = last_byte * 8;
-  }
-
-  /* Form up next 24 bits in accum */
-  offs = cur_bit >> 3;		/* byte containing cur_bit */
-#ifdef CHAR_IS_UNSIGNED
-  accum = code_buf[offs+2];
-  accum <<= 8;
-  accum |= code_buf[offs+1];
-  accum <<= 8;
-  accum |= code_buf[offs];
-#else
-  accum = code_buf[offs+2] & 0xFF;
-  accum <<= 8;
-  accum |= code_buf[offs+1] & 0xFF;
-  accum <<= 8;
-  accum |= code_buf[offs] & 0xFF;
-#endif
-
-  /* Right-align cur_bit in accum, then mask off desired number of bits */
-  accum >>= (cur_bit & 7);
-  ret = ((int) accum) & ((1 << code_size) - 1);
-  
-  cur_bit += code_size;
-  return ret;
-}
-
-
-LOCAL int
-LZWReadByte (compress_info_ptr cinfo)
-/* Read an LZW-compressed byte */
-{
-  static int oldcode;		/* previous LZW symbol */
-  static int firstcode;		/* first byte of oldcode's expansion */
-  register int code;		/* current working code */
-  int incode;			/* saves actual input code */
-
-  /* First time, just eat the expected Clear code(s) and return next code, */
-  /* which is expected to be a raw byte. */
-  if (first_time) {
-    first_time = FALSE;
-    code = clear_code;		/* enables sharing code with Clear case */
-  } else {
-
-    /* If any codes are stacked from a previously read symbol, return them */
-    if (sp > symbol_stack)
-      return (int) *(--sp);
-
-    /* Time to read a new symbol */
-    code = GetCode(cinfo);
-
-  }
-
-  if (code == clear_code) {
-    /* Reinit static state, swallow any extra Clear codes, and */
-    /* return next code, which is expected to be a raw byte. */
-    ReInitLZW();
-    do {
-      code = GetCode(cinfo);
-    } while (code == clear_code);
-    if (code > clear_code) {	/* make sure it is a raw byte */
-      WARNMS(cinfo->emethods, "Corrupt data in GIF file");
-      code = 0;			/* use something valid */
-    }
-    firstcode = oldcode = code;	/* make firstcode, oldcode valid! */
-    return code;
-  }
-
-  if (code == end_code) {
-    /* Skip the rest of the image, unless GetCode already read terminator */
-    if (! out_of_blocks) {
-      SkipDataBlocks(cinfo);
-      out_of_blocks = TRUE;
-    }
-    /* Complain that there's not enough data */
-    WARNMS(cinfo->emethods, "Premature end of GIF image");
-    /* Pad data with 0's */
-    return 0;			/* fake something usable */
-  }
-
-  /* Got normal raw byte or LZW symbol */
-  incode = code;		/* save for a moment */
-  
-  if (code >= max_code) {	/* special case for not-yet-defined symbol */
-    /* code == max_code is OK; anything bigger is bad data */
-    if (code > max_code) {
-      WARNMS(cinfo->emethods, "Corrupt data in GIF file");
-      incode = 0;		/* prevent creation of loops in symbol table */
-    }
-    *sp++ = (UINT8) firstcode;	/* it will be defined as oldcode/firstcode */
-    code = oldcode;
-  }
-
-  /* If it's a symbol, expand it into the stack */
-  while (code >= clear_code) {
-    *sp++ = symbol_tail[code];	/* tail of symbol: a simple byte value */
-    code = symbol_head[code];	/* head of symbol: another LZW symbol */
-  }
-  /* At this point code just represents a raw byte */
-  firstcode = code;		/* save for possible future use */
-
-  /* If there's room in table, */
-  if ((code = max_code) < LZW_TABLE_SIZE) {
-    /* Define a new symbol = prev sym + head of this sym's expansion */
-    symbol_head[code] = oldcode;
-    symbol_tail[code] = (UINT8) firstcode;
-    max_code++;
-    /* Is it time to increase code_size? */
-    if ((max_code >= limit_code) && (code_size < MAX_LZW_BITS)) {
-      code_size++;
-      limit_code <<= 1;		/* keep equal to 2^code_size */
-    }
-  }
-  
-  oldcode = incode;		/* save last input symbol for future use */
-  return firstcode;		/* return first byte of symbol's expansion */
-}
-
-
-LOCAL void
-ReadColorMap (compress_info_ptr cinfo, int cmaplen, JSAMPARRAY cmap)
-/* Read a GIF colormap */
-{
-  int i;
-
-  for (i = 0; i < cmaplen; i++) {
-    cmap[CM_RED][i]   = (JSAMPLE) ReadByte(cinfo);
-    cmap[CM_GREEN][i] = (JSAMPLE) ReadByte(cinfo);
-    cmap[CM_BLUE][i]  = (JSAMPLE) ReadByte(cinfo);
-  }
-}
-
-
-LOCAL void
-DoExtension (compress_info_ptr cinfo)
-/* Process an extension block */
-/* Currently we ignore 'em all */
-{
-  int extlabel;
-
-  /* Read extension label byte */
-  extlabel = ReadByte(cinfo);
-  TRACEMS1(cinfo->emethods, 1, "Ignoring GIF extension block of type 0x%02x",
-	   extlabel);
-  /* Skip the data block(s) associated with the extension */
-  SkipDataBlocks(cinfo);
-}
-
-
-/*
- * Read the file header; return image size and component count.
- */
-
-METHODDEF void
-input_init (compress_info_ptr cinfo)
-{
-  char hdrbuf[10];		/* workspace for reading control blocks */
-  UINT16 width, height;		/* image dimensions */
-  int colormaplen, aspectRatio;
-  int c;
-
-  /* Allocate space to store the colormap */
-  colormap = (*cinfo->emethods->alloc_small_sarray)
-		((long) MAXCOLORMAPSIZE, (long) NUMCOLORS);
-
-  /* Read and verify GIF Header */
-  if (! ReadOK(cinfo->input_file, hdrbuf, 6))
-    ERREXIT(cinfo->emethods, "Not a GIF file");
-  if (hdrbuf[0] != 'G' || hdrbuf[1] != 'I' || hdrbuf[2] != 'F')
-    ERREXIT(cinfo->emethods, "Not a GIF file");
-  /* Check for expected version numbers.
-   * If unknown version, give warning and try to process anyway;
-   * this is per recommendation in GIF89a standard.
-   */
-  if ((hdrbuf[3] != '8' || hdrbuf[4] != '7' || hdrbuf[5] != 'a') &&
-      (hdrbuf[3] != '8' || hdrbuf[4] != '9' || hdrbuf[5] != 'a'))
-    TRACEMS3(cinfo->emethods, 1,
-	     "Warning: unexpected GIF version number '%c%c%c'",
-	     hdrbuf[3], hdrbuf[4], hdrbuf[5]);
-
-  /* Read and decipher Logical Screen Descriptor */
-  if (! ReadOK(cinfo->input_file, hdrbuf, 7))
-    ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
-  width = LM_to_uint(hdrbuf[0],hdrbuf[1]);
-  height = LM_to_uint(hdrbuf[2],hdrbuf[3]);
-  colormaplen = 2 << (hdrbuf[4] & 0x07);
-  /* we ignore the color resolution, sort flag, and background color index */
-  aspectRatio = hdrbuf[6] & 0xFF;
-  if (aspectRatio != 0 && aspectRatio != 49)
-    TRACEMS(cinfo->emethods, 1, "Warning: nonsquare pixels in input");
-
-  /* Read global colormap if header indicates it is present */
-  if (BitSet(hdrbuf[4], COLORMAPFLAG))
-    ReadColorMap(cinfo, colormaplen, colormap);
-
-  /* Scan until we reach start of desired image.
-   * We don't currently support skipping images, but could add it easily.
-   */
-  for (;;) {
-    c = ReadByte(cinfo);
-
-    if (c == ';')		/* GIF terminator?? */
-      ERREXIT(cinfo->emethods, "Too few images in GIF file");
-
-    if (c == '!') {		/* Extension */
-      DoExtension(cinfo);
-      continue;
-    }
-    
-    if (c != ',') {		/* Not an image separator? */
-      TRACEMS1(cinfo->emethods, 1, "Bogus input char 0x%02x, ignoring", c);
-      continue;
-    }
-
-    /* Read and decipher Local Image Descriptor */
-    if (! ReadOK(cinfo->input_file, hdrbuf, 9))
-      ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
-    /* we ignore top/left position info, also sort flag */
-    width = LM_to_uint(hdrbuf[4],hdrbuf[5]);
-    height = LM_to_uint(hdrbuf[6],hdrbuf[7]);
-    is_interlaced = BitSet(hdrbuf[8], INTERLACE);
-
-    /* Read local colormap if header indicates it is present */
-    /* Note: if we wanted to support skipping images, */
-    /* we'd need to skip rather than read colormap for ignored images */
-    if (BitSet(hdrbuf[8], COLORMAPFLAG)) {
-      colormaplen = 2 << (hdrbuf[8] & 0x07);
-      ReadColorMap(cinfo, colormaplen, colormap);
-    }
-
-    input_code_size = ReadByte(cinfo); /* get minimum-code-size byte */
-    if (input_code_size < 2 || input_code_size >= MAX_LZW_BITS)
-      ERREXIT1(cinfo->emethods, "Bogus codesize %d", input_code_size);
-
-    /* Reached desired image, so break out of loop */
-    /* If we wanted to skip this image, */
-    /* we'd call SkipDataBlocks and then continue the loop */
-    break;
-  }
-
-  /* Prepare to read selected image: first initialize LZW decompressor */
-  symbol_head = (UINT16 FAR *) (*cinfo->emethods->alloc_medium)
-				(LZW_TABLE_SIZE * SIZEOF(UINT16));
-  symbol_tail = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
-				(LZW_TABLE_SIZE * SIZEOF(UINT8));
-  symbol_stack = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
-				(LZW_TABLE_SIZE * SIZEOF(UINT8));
-  InitLZWCode();
-
-  /*
-   * If image is interlaced, we read it into a full-size sample array,
-   * decompressing as we go; then get_input_row selects rows from the
-   * sample array in the proper order.
-   */
-  if (is_interlaced) {
-    /* We request the big array now, but can't access it until the pipeline
-     * controller causes all the big arrays to be allocated.  Hence, the
-     * actual work of reading the image is postponed until the first call
-     * of get_input_row.
-     */
-    interlaced_image = (*cinfo->emethods->request_big_sarray)
-		((long) width, (long) height, 1L);
-    cinfo->methods->get_input_row = load_interlaced_image;
-    cinfo->total_passes++;	/* count file reading as separate pass */
-  }
-
-  /* Return info about the image. */
-  cinfo->input_components = NUMCOLORS;
-  cinfo->in_color_space = CS_RGB;
-  cinfo->image_width = width;
-  cinfo->image_height = height;
-  cinfo->data_precision = 8;	/* always, even if 12-bit JSAMPLEs */
-
-  TRACEMS3(cinfo->emethods, 1, "%ux%ux%d GIF image",
-	   (unsigned int) width, (unsigned int) height, colormaplen);
-}
-
-
-/*
- * Read one row of pixels.
- * This version is used for noninterlaced GIF images:
- * we read directly from the GIF file.
- */
-
-METHODDEF void
-get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-{
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  register int c;
-  
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  for (col = cinfo->image_width; col > 0; col--) {
-    c = LZWReadByte(cinfo);
-    *ptr0++ = colormap[CM_RED][c];
-    *ptr1++ = colormap[CM_GREEN][c];
-    *ptr2++ = colormap[CM_BLUE][c];
-  }
-}
-
-
-/*
- * Read one row of pixels.
- * This version is used for the first call on get_input_row when
- * reading an interlaced GIF file: we read the whole image into memory.
- */
-
-METHODDEF void
-load_interlaced_image (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-{
-  JSAMPARRAY image_ptr;
-  register JSAMPROW sptr;
-  register long col;
-  long row;
-
-  /* Read the interlaced image into the big array we've created. */
-  for (row = 0; row < cinfo->image_height; row++) {
-    (*cinfo->methods->progress_monitor) (cinfo, row, cinfo->image_height);
-    image_ptr = (*cinfo->emethods->access_big_sarray)
-			(interlaced_image, row, TRUE);
-    sptr = image_ptr[0];
-    for (col = cinfo->image_width; col > 0; col--) {
-      *sptr++ = (JSAMPLE) LZWReadByte(cinfo);
-    }
-  }
-  cinfo->completed_passes++;
-
-  /* Replace method pointer so subsequent calls don't come here. */
-  cinfo->methods->get_input_row = get_interlaced_row;
-  /* Initialize for get_interlaced_row, and perform first call on it. */
-  cur_row_number = 0;
-  pass2_offset = (cinfo->image_height + 7L) / 8L;
-  pass3_offset = pass2_offset + (cinfo->image_height + 3L) / 8L;
-  pass4_offset = pass3_offset + (cinfo->image_height + 1L) / 4L;
-
-  get_interlaced_row(cinfo, pixel_row);
-}
-
-
-/*
- * Read one row of pixels.
- * This version is used for interlaced GIF images:
- * we read from the big in-memory image.
- */
-
-METHODDEF void
-get_interlaced_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-{
-  JSAMPARRAY image_ptr;
-  register JSAMPROW sptr, ptr0, ptr1, ptr2;
-  register long col;
-  register int c;
-  long irow;
-
-  /* Figure out which row of interlaced image is needed, and access it. */
-  switch ((int) (cur_row_number & 7L)) {
-  case 0:			/* first-pass row */
-    irow = cur_row_number >> 3;
-    break;
-  case 4:			/* second-pass row */
-    irow = (cur_row_number >> 3) + pass2_offset;
-    break;
-  case 2:			/* third-pass row */
-  case 6:
-    irow = (cur_row_number >> 2) + pass3_offset;
-    break;
-  default:			/* fourth-pass row */
-    irow = (cur_row_number >> 1) + pass4_offset;
-    break;
-  }
-  image_ptr = (*cinfo->emethods->access_big_sarray)
-			(interlaced_image, irow, FALSE);
-  /* Scan the row, expand colormap, and output */
-  sptr = image_ptr[0];
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  for (col = cinfo->image_width; col > 0; col--) {
-    c = GETJSAMPLE(*sptr++);
-    *ptr0++ = colormap[CM_RED][c];
-    *ptr1++ = colormap[CM_GREEN][c];
-    *ptr2++ = colormap[CM_BLUE][c];
-  }
-  cur_row_number++;		/* for next time */
-}
-
-
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-input_term (compress_info_ptr cinfo)
-{
-  /* no work (we let free_all release the workspace) */
-}
-
-
-/*
- * The method selection routine for GIF format input.
- * Note that this must be called by the user interface before calling
- * jpeg_compress.  If multiple input formats are supported, the
- * user interface is responsible for discovering the file format and
- * calling the appropriate method selection routine.
- */
-
-GLOBAL void
-jselrgif (compress_info_ptr cinfo)
-{
-  cinfo->methods->input_init = input_init;
-  cinfo->methods->get_input_row = get_input_row; /* assume uninterlaced */
-  cinfo->methods->input_term = input_term;
-}
-
-#endif /* GIF_SUPPORTED */
diff --git a/jrdjfif.c b/jrdjfif.c
deleted file mode 100644
index b6bd57c..0000000
--- a/jrdjfif.c
+++ /dev/null
@@ -1,864 +0,0 @@
-/*
- * jrdjfif.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to decode standard JPEG file headers/markers.
- * This code will handle "raw JPEG" and JFIF-convention JPEG files.
- *
- * You can also use this module to decode a raw-JPEG or JFIF-standard data
- * stream that is embedded within a larger file.  To do that, you must
- * position the file to the JPEG SOI marker (0xFF/0xD8) that begins the
- * data sequence to be decoded.  If nothing better is possible, you can scan
- * the file until you see the SOI marker, then use JUNGETC to push it back.
- *
- * This module relies on the JGETC macro and the read_jpeg_data method (which
- * is provided by the user interface) to read from the JPEG data stream.
- * Therefore, this module is not dependent on any particular assumption about
- * the data source; it need not be a stdio stream at all.  (This fact does
- * NOT carry over to more complex JPEG file formats such as JPEG-in-TIFF;
- * those format control modules may well need to assume stdio input.)
- *
- * These routines are invoked via the methods read_file_header,
- * read_scan_header, read_jpeg_data, read_scan_trailer, and read_file_trailer.
- */
-
-#include "jinclude.h"
-
-#ifdef JFIF_SUPPORTED
-
-
-typedef enum {			/* JPEG marker codes */
-  M_SOF0  = 0xc0,
-  M_SOF1  = 0xc1,
-  M_SOF2  = 0xc2,
-  M_SOF3  = 0xc3,
-  
-  M_SOF5  = 0xc5,
-  M_SOF6  = 0xc6,
-  M_SOF7  = 0xc7,
-  
-  M_JPG   = 0xc8,
-  M_SOF9  = 0xc9,
-  M_SOF10 = 0xca,
-  M_SOF11 = 0xcb,
-  
-  M_SOF13 = 0xcd,
-  M_SOF14 = 0xce,
-  M_SOF15 = 0xcf,
-  
-  M_DHT   = 0xc4,
-  
-  M_DAC   = 0xcc,
-  
-  M_RST0  = 0xd0,
-  M_RST1  = 0xd1,
-  M_RST2  = 0xd2,
-  M_RST3  = 0xd3,
-  M_RST4  = 0xd4,
-  M_RST5  = 0xd5,
-  M_RST6  = 0xd6,
-  M_RST7  = 0xd7,
-  
-  M_SOI   = 0xd8,
-  M_EOI   = 0xd9,
-  M_SOS   = 0xda,
-  M_DQT   = 0xdb,
-  M_DNL   = 0xdc,
-  M_DRI   = 0xdd,
-  M_DHP   = 0xde,
-  M_EXP   = 0xdf,
-  
-  M_APP0  = 0xe0,
-  M_APP15 = 0xef,
-  
-  M_JPG0  = 0xf0,
-  M_JPG13 = 0xfd,
-  M_COM   = 0xfe,
-  
-  M_TEM   = 0x01,
-  
-  M_ERROR = 0x100
-} JPEG_MARKER;
-
-
-/*
- * Reload the input buffer after it's been emptied, and return the next byte.
- * This is exported for direct use by the entropy decoder.
- * See the JGETC macro for calling conditions.  Note in particular that
- * read_jpeg_data may NOT return EOF.  If no more data is available, it must
- * exit via ERREXIT, or perhaps synthesize fake data (such as an RST marker).
- * For error recovery purposes, synthesizing an EOI marker is probably best.
- *
- * For this header control module, read_jpeg_data is supplied by the
- * user interface.  However, header formats that require random access
- * to the input file would need to supply their own code.  This code is
- * left here to indicate what is required.
- */
-
-#if 0				/* not needed in this module */
-
-METHODDEF int
-read_jpeg_data (decompress_info_ptr cinfo)
-{
-  cinfo->next_input_byte = cinfo->input_buffer + MIN_UNGET;
-
-  cinfo->bytes_in_buffer = (int) JFREAD(cinfo->input_file,
-					cinfo->next_input_byte,
-					JPEG_BUF_SIZE);
-  
-  if (cinfo->bytes_in_buffer <= 0) {
-    WARNMS(cinfo->emethods, "Premature EOF in JPEG file");
-    cinfo->next_input_byte[0] = (char) 0xFF;
-    cinfo->next_input_byte[1] = (char) M_EOI;
-    cinfo->bytes_in_buffer = 2;
-  }
-
-  return JGETC(cinfo);
-}
-
-#endif
-
-
-/*
- * Routines to parse JPEG markers & save away the useful info.
- */
-
-
-LOCAL INT32
-get_2bytes (decompress_info_ptr cinfo)
-/* Get a 2-byte unsigned integer (e.g., a marker parameter length field) */
-{
-  INT32 a;
-  
-  a = JGETC(cinfo);
-  return (a << 8) + JGETC(cinfo);
-}
-
-
-LOCAL void
-skip_variable (decompress_info_ptr cinfo, int code)
-/* Skip over an unknown or uninteresting variable-length marker */
-{
-  INT32 length;
-  
-  length = get_2bytes(cinfo);
-  
-  TRACEMS2(cinfo->emethods, 1,
-	   "Skipping marker 0x%02x, length %u", code, (int) length);
-  
-  for (length -= 2; length > 0; length--)
-    (void) JGETC(cinfo);
-}
-
-
-LOCAL void
-get_dht (decompress_info_ptr cinfo)
-/* Process a DHT marker */
-{
-  INT32 length;
-  UINT8 bits[17];
-  UINT8 huffval[256];
-  int i, index, count;
-  HUFF_TBL **htblptr;
-  
-  length = get_2bytes(cinfo)-2;
-  
-  while (length > 0) {
-    index = JGETC(cinfo);
-
-    TRACEMS1(cinfo->emethods, 1, "Define Huffman Table 0x%02x", index);
-      
-    bits[0] = 0;
-    count = 0;
-    for (i = 1; i <= 16; i++) {
-      bits[i] = (UINT8) JGETC(cinfo);
-      count += bits[i];
-    }
-
-    length -= 1 + 16;
-
-    TRACEMS8(cinfo->emethods, 2, "        %3d %3d %3d %3d %3d %3d %3d %3d",
-	     bits[1], bits[2], bits[3], bits[4],
-	     bits[5], bits[6], bits[7], bits[8]);
-    TRACEMS8(cinfo->emethods, 2, "        %3d %3d %3d %3d %3d %3d %3d %3d",
-	     bits[9], bits[10], bits[11], bits[12],
-	     bits[13], bits[14], bits[15], bits[16]);
-
-    if (count > 256 || ((INT32) count) > length)
-      ERREXIT(cinfo->emethods, "Bogus DHT counts");
-
-    for (i = 0; i < count; i++)
-      huffval[i] = (UINT8) JGETC(cinfo);
-
-    length -= count;
-
-    if (index & 0x10) {		/* AC table definition */
-      index -= 0x10;
-      htblptr = &cinfo->ac_huff_tbl_ptrs[index];
-    } else {			/* DC table definition */
-      htblptr = &cinfo->dc_huff_tbl_ptrs[index];
-    }
-
-    if (index < 0 || index >= NUM_HUFF_TBLS)
-      ERREXIT1(cinfo->emethods, "Bogus DHT index %d", index);
-
-    if (*htblptr == NULL)
-      *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
-  
-    MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
-    MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
-    }
-}
-
-
-LOCAL void
-get_dac (decompress_info_ptr cinfo)
-/* Process a DAC marker */
-{
-  INT32 length;
-  int index, val;
-
-  length = get_2bytes(cinfo)-2;
-  
-  while (length > 0) {
-    index = JGETC(cinfo);
-    val = JGETC(cinfo);
-
-    TRACEMS2(cinfo->emethods, 1,
-	     "Define Arithmetic Table 0x%02x: 0x%02x", index, val);
-
-    if (index < 0 || index >= (2*NUM_ARITH_TBLS))
-      ERREXIT1(cinfo->emethods, "Bogus DAC index %d", index);
-
-    if (index >= NUM_ARITH_TBLS) { /* define AC table */
-      cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
-    } else {			/* define DC table */
-      cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
-      cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
-      if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
-	ERREXIT1(cinfo->emethods, "Bogus DAC value 0x%x", val);
-    }
-
-    length -= 2;
-  }
-}
-
-
-LOCAL void
-get_dqt (decompress_info_ptr cinfo)
-/* Process a DQT marker */
-{
-  INT32 length;
-  int n, i, prec;
-  UINT16 tmp;
-  QUANT_TBL_PTR quant_ptr;
-  
-  length = get_2bytes(cinfo) - 2;
-  
-  while (length > 0) {
-    n = JGETC(cinfo);
-    prec = n >> 4;
-    n &= 0x0F;
-
-    TRACEMS2(cinfo->emethods, 1,
-	     "Define Quantization Table %d  precision %d", n, prec);
-
-    if (n >= NUM_QUANT_TBLS)
-      ERREXIT1(cinfo->emethods, "Bogus table number %d", n);
-      
-    if (cinfo->quant_tbl_ptrs[n] == NULL)
-      cinfo->quant_tbl_ptrs[n] = (QUANT_TBL_PTR)
-	(*cinfo->emethods->alloc_small) (SIZEOF(QUANT_TBL));
-    quant_ptr = cinfo->quant_tbl_ptrs[n];
-
-    for (i = 0; i < DCTSIZE2; i++) {
-      tmp = JGETC(cinfo);
-      if (prec)
-	tmp = (tmp<<8) + JGETC(cinfo);
-      quant_ptr[i] = tmp;
-    }
-
-    for (i = 0; i < DCTSIZE2; i += 8) {
-      TRACEMS8(cinfo->emethods, 2, "        %4u %4u %4u %4u %4u %4u %4u %4u",
-	       quant_ptr[i  ], quant_ptr[i+1], quant_ptr[i+2], quant_ptr[i+3],
-	       quant_ptr[i+4], quant_ptr[i+5], quant_ptr[i+6], quant_ptr[i+7]);
-    }
-
-    length -= DCTSIZE2+1;
-    if (prec) length -= DCTSIZE2;
-  }
-}
-
-
-LOCAL void
-get_dri (decompress_info_ptr cinfo)
-/* Process a DRI marker */
-{
-  if (get_2bytes(cinfo) != 4)
-    ERREXIT(cinfo->emethods, "Bogus length in DRI");
-
-  cinfo->restart_interval = (UINT16) get_2bytes(cinfo);
-
-  TRACEMS1(cinfo->emethods, 1,
-	   "Define Restart Interval %u", cinfo->restart_interval);
-}
-
-
-LOCAL void
-get_app0 (decompress_info_ptr cinfo)
-/* Process an APP0 marker */
-{
-#define JFIF_LEN 14
-  INT32 length;
-  UINT8 b[JFIF_LEN];
-  int buffp;
-
-  length = get_2bytes(cinfo) - 2;
-
-  /* See if a JFIF APP0 marker is present */
-
-  if (length >= JFIF_LEN) {
-    for (buffp = 0; buffp < JFIF_LEN; buffp++)
-      b[buffp] = (UINT8) JGETC(cinfo);
-    length -= JFIF_LEN;
-
-    if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) {
-      /* Found JFIF APP0 marker: check version */
-      /* Major version must be 1 */
-      if (b[5] != 1)
-	ERREXIT2(cinfo->emethods, "Unsupported JFIF revision number %d.%02d",
-		 b[5], b[6]);
-      /* Minor version should be 0..2, but try to process anyway if newer */
-      if (b[6] > 2)
-	TRACEMS2(cinfo->emethods, 1, "Warning: unknown JFIF revision number %d.%02d",
-		 b[5], b[6]);
-      /* Save info */
-      cinfo->density_unit = b[7];
-      cinfo->X_density = (b[8] << 8) + b[9];
-      cinfo->Y_density = (b[10] << 8) + b[11];
-      /* Assume colorspace is YCbCr, unless UI has overridden me */
-      if (cinfo->jpeg_color_space == CS_UNKNOWN)
-	cinfo->jpeg_color_space = CS_YCbCr;
-      TRACEMS3(cinfo->emethods, 1, "JFIF APP0 marker, density %dx%d  %d",
-	       cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
-      if (b[12] | b[13])
-	TRACEMS2(cinfo->emethods, 1, "    with %d x %d thumbnail image",
-		 b[12], b[13]);
-      if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))
-	TRACEMS1(cinfo->emethods, 1,
-		 "Warning: thumbnail image size does not match data length %u",
-		 (int) length);
-    } else {
-      TRACEMS1(cinfo->emethods, 1, "Unknown APP0 marker (not JFIF), length %u",
-	       (int) length + JFIF_LEN);
-    }
-  } else {
-    TRACEMS1(cinfo->emethods, 1, "Short APP0 marker, length %u", (int) length);
-  }
-
-  while (--length >= 0)		/* skip any remaining data */
-    (void) JGETC(cinfo);
-}
-
-
-LOCAL void
-get_com (decompress_info_ptr cinfo)
-/* Process a COM marker */
-/* Actually we just pass this off to an application-supplied routine */
-{
-  INT32 length;
-  
-  length = get_2bytes(cinfo) - 2;
-  
-  TRACEMS1(cinfo->emethods, 1, "Comment, length %u", (int) length);
-  
-  (*cinfo->methods->process_comment) (cinfo, (long) length);
-}
-
-
-LOCAL void
-get_sof (decompress_info_ptr cinfo, int code)
-/* Process a SOFn marker */
-{
-  INT32 length;
-  short ci;
-  int c;
-  jpeg_component_info * compptr;
-  
-  length = get_2bytes(cinfo);
-  
-  cinfo->data_precision = JGETC(cinfo);
-  cinfo->image_height   = get_2bytes(cinfo);
-  cinfo->image_width    = get_2bytes(cinfo);
-  cinfo->num_components = JGETC(cinfo);
-
-  TRACEMS4(cinfo->emethods, 1,
-	   "Start Of Frame 0x%02x: width=%u, height=%u, components=%d",
-	   code, (int) cinfo->image_width, (int) cinfo->image_height,
-	   cinfo->num_components);
-
-  /* We don't support files in which the image height is initially specified */
-  /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
-  /* might as well have a general sanity check. */
-  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
-      || cinfo->num_components <= 0)
-    ERREXIT(cinfo->emethods, "Empty JPEG image (DNL not supported)");
-
-#ifdef EIGHT_BIT_SAMPLES
-  if (cinfo->data_precision != 8)
-    ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
-#endif
-#ifdef TWELVE_BIT_SAMPLES
-  if (cinfo->data_precision != 12) /* this needs more thought?? */
-    ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
-#endif
-#ifdef SIXTEEN_BIT_SAMPLES
-  if (cinfo->data_precision != 16) /* this needs more thought?? */
-    ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
-#endif
-
-  if (length != (cinfo->num_components * 3 + 8))
-    ERREXIT(cinfo->emethods, "Bogus SOF length");
-
-  cinfo->comp_info = (jpeg_component_info *) (*cinfo->emethods->alloc_small)
-			(cinfo->num_components * SIZEOF(jpeg_component_info));
-  
-  for (ci = 0; ci < cinfo->num_components; ci++) {
-    compptr = &cinfo->comp_info[ci];
-    compptr->component_index = ci;
-    compptr->component_id = JGETC(cinfo);
-    c = JGETC(cinfo);
-    compptr->h_samp_factor = (c >> 4) & 15;
-    compptr->v_samp_factor = (c     ) & 15;
-    compptr->quant_tbl_no  = JGETC(cinfo);
-    compptr->component_needed = TRUE; /* assume all components are wanted */
-
-    TRACEMS4(cinfo->emethods, 1, "    Component %d: %dhx%dv q=%d",
-	     compptr->component_id, compptr->h_samp_factor,
-	     compptr->v_samp_factor, compptr->quant_tbl_no);
-  }
-}
-
-
-LOCAL void
-get_sos (decompress_info_ptr cinfo)
-/* Process a SOS marker */
-{
-  INT32 length;
-  int i, ci, n, c, cc;
-  jpeg_component_info * compptr;
-  
-  length = get_2bytes(cinfo);
-  
-  n = JGETC(cinfo);  /* Number of components */
-  cinfo->comps_in_scan = n;
-  length -= 3;
-  
-  if (length != (n * 2 + 3) || n < 1 || n > MAX_COMPS_IN_SCAN)
-    ERREXIT(cinfo->emethods, "Bogus SOS length");
-
-  TRACEMS1(cinfo->emethods, 1, "Start Of Scan: %d components", n);
-  
-  for (i = 0; i < n; i++) {
-    cc = JGETC(cinfo);
-    c = JGETC(cinfo);
-    length -= 2;
-    
-    for (ci = 0; ci < cinfo->num_components; ci++)
-      if (cc == cinfo->comp_info[ci].component_id)
-	break;
-    
-    if (ci >= cinfo->num_components)
-      ERREXIT(cinfo->emethods, "Invalid component number in SOS");
-    
-    compptr = &cinfo->comp_info[ci];
-    cinfo->cur_comp_info[i] = compptr;
-    compptr->dc_tbl_no = (c >> 4) & 15;
-    compptr->ac_tbl_no = (c     ) & 15;
-    
-    TRACEMS3(cinfo->emethods, 1, "    c%d: [dc=%d ac=%d]", cc,
-	     compptr->dc_tbl_no, compptr->ac_tbl_no);
-  }
-  
-  while (length > 0) {
-    (void) JGETC(cinfo);
-    length--;
-  }
-}
-
-
-LOCAL void
-get_soi (decompress_info_ptr cinfo)
-/* Process an SOI marker */
-{
-  int i;
-  
-  TRACEMS(cinfo->emethods, 1, "Start of Image");
-
-  /* Reset all parameters that are defined to be reset by SOI */
-
-  for (i = 0; i < NUM_ARITH_TBLS; i++) {
-    cinfo->arith_dc_L[i] = 0;
-    cinfo->arith_dc_U[i] = 1;
-    cinfo->arith_ac_K[i] = 5;
-  }
-  cinfo->restart_interval = 0;
-
-  cinfo->density_unit = 0;	/* set default JFIF APP0 values */
-  cinfo->X_density = 1;
-  cinfo->Y_density = 1;
-
-  cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling */
-}
-
-
-LOCAL int
-next_marker (decompress_info_ptr cinfo)
-/* Find the next JPEG marker */
-/* Note that the output might not be a valid marker code, */
-/* but it will never be 0 or FF */
-{
-  int c, nbytes;
-
-  nbytes = 0;
-  do {
-    do {			/* skip any non-FF bytes */
-      nbytes++;
-      c = JGETC(cinfo);
-    } while (c != 0xFF);
-    do {			/* skip any duplicate FFs */
-      /* we don't increment nbytes here since extra FFs are legal */
-      c = JGETC(cinfo);
-    } while (c == 0xFF);
-  } while (c == 0);		/* repeat if it was a stuffed FF/00 */
-
-  if (nbytes != 1)
-    WARNMS2(cinfo->emethods,
-	    "Corrupt JPEG data: %d extraneous bytes before marker 0x%02x",
-	    nbytes-1, c);
-
-  return c;
-}
-
-
-LOCAL int
-process_tables (decompress_info_ptr cinfo)
-/* Scan and process JPEG markers that can appear in any order */
-/* Return when an SOI, EOI, SOFn, or SOS is found */
-{
-  int c;
-
-  for (;;) {
-    c = next_marker(cinfo);
-      
-    switch (c) {
-    case M_SOF0:
-    case M_SOF1:
-    case M_SOF2:
-    case M_SOF3:
-    case M_SOF5:
-    case M_SOF6:
-    case M_SOF7:
-    case M_JPG:
-    case M_SOF9:
-    case M_SOF10:
-    case M_SOF11:
-    case M_SOF13:
-    case M_SOF14:
-    case M_SOF15:
-    case M_SOI:
-    case M_EOI:
-    case M_SOS:
-      return c;
-      
-    case M_DHT:
-      get_dht(cinfo);
-      break;
-      
-    case M_DAC:
-      get_dac(cinfo);
-      break;
-      
-    case M_DQT:
-      get_dqt(cinfo);
-      break;
-      
-    case M_DRI:
-      get_dri(cinfo);
-      break;
-      
-    case M_APP0:
-      get_app0(cinfo);
-      break;
-      
-    case M_COM:
-      get_com(cinfo);
-      break;
-
-    case M_RST0:		/* these are all parameterless */
-    case M_RST1:
-    case M_RST2:
-    case M_RST3:
-    case M_RST4:
-    case M_RST5:
-    case M_RST6:
-    case M_RST7:
-    case M_TEM:
-      TRACEMS1(cinfo->emethods, 1, "Unexpected marker 0x%02x", c);
-      break;
-
-    default:	/* must be DNL, DHP, EXP, APPn, JPGn, or RESn */
-      skip_variable(cinfo, c);
-      break;
-    }
-  }
-}
-
-
-
-/*
- * Initialize and read the file header (everything through the SOF marker).
- */
-
-METHODDEF void
-read_file_header (decompress_info_ptr cinfo)
-{
-  int c;
-
-  /* Demand an SOI marker at the start of the file --- otherwise it's
-   * probably not a JPEG file at all.  If the user interface wants to support
-   * nonstandard headers in front of the SOI, it must skip over them itself
-   * before calling jpeg_decompress().
-   */
-  if (JGETC(cinfo) != 0xFF  ||  JGETC(cinfo) != (int) M_SOI)
-    ERREXIT(cinfo->emethods, "Not a JPEG file");
-
-  get_soi(cinfo);		/* OK, process SOI */
-
-  /* Process markers until SOF */
-  c = process_tables(cinfo);
-
-  switch (c) {
-  case M_SOF0:
-  case M_SOF1:
-    get_sof(cinfo, c);
-    cinfo->arith_code = FALSE;
-    break;
-      
-  case M_SOF9:
-    get_sof(cinfo, c);
-    cinfo->arith_code = TRUE;
-    break;
-
-  default:
-    ERREXIT1(cinfo->emethods, "Unsupported SOF marker type 0x%02x", c);
-    break;
-  }
-
-  /* Figure out what colorspace we have */
-  /* (too bad the JPEG committee didn't provide a real way to specify this) */
-
-  switch (cinfo->num_components) {
-  case 1:
-    cinfo->jpeg_color_space = CS_GRAYSCALE;
-    break;
-
-  case 3:
-    /* if we saw a JFIF marker, leave it set to YCbCr; */
-    /* also leave it alone if UI has provided a value */
-    if (cinfo->jpeg_color_space == CS_UNKNOWN) {
-      short cid0 = cinfo->comp_info[0].component_id;
-      short cid1 = cinfo->comp_info[1].component_id;
-      short cid2 = cinfo->comp_info[2].component_id;
-
-      if (cid0 == 1 && cid1 == 2 && cid2 == 3)
-	cinfo->jpeg_color_space = CS_YCbCr; /* assume it's JFIF w/out marker */
-      else if (cid0 == 1 && cid1 == 4 && cid2 == 5)
-	cinfo->jpeg_color_space = CS_YIQ; /* prototype's YIQ matrix */
-      else {
-	TRACEMS3(cinfo->emethods, 1,
-		 "Unrecognized component IDs %d %d %d, assuming YCbCr",
-		 cid0, cid1, cid2);
-	cinfo->jpeg_color_space = CS_YCbCr;
-      }
-    }
-    break;
-
-  case 4:
-    cinfo->jpeg_color_space = CS_CMYK;
-    break;
-
-  default:
-    cinfo->jpeg_color_space = CS_UNKNOWN;
-    break;
-  }
-}
-
-
-/*
- * Read the start of a scan (everything through the SOS marker).
- * Return TRUE if find SOS, FALSE if find EOI.
- */
-
-METHODDEF boolean
-read_scan_header (decompress_info_ptr cinfo)
-{
-  int c;
-  
-  /* Process markers until SOS or EOI */
-  c = process_tables(cinfo);
-  
-  switch (c) {
-  case M_SOS:
-    get_sos(cinfo);
-    return TRUE;
-    
-  case M_EOI:
-    TRACEMS(cinfo->emethods, 1, "End Of Image");
-    return FALSE;
-
-  default:
-    ERREXIT1(cinfo->emethods, "Unexpected marker 0x%02x", c);
-    break;
-  }
-  return FALSE;			/* keeps lint happy */
-}
-
-
-/*
- * The entropy decoder calls this routine if it finds a marker other than
- * the restart marker it was expecting.  (This code is *not* used unless
- * a nonzero restart interval has been declared.)  The passed parameter is
- * the marker code actually found (might be anything, except 0 or FF).
- * The desired restart marker is that indicated by cinfo->next_restart_num.
- * This routine is supposed to apply whatever error recovery strategy seems
- * appropriate in order to position the input stream to the next data segment.
- * For some file formats (eg, TIFF) extra information such as tile boundary
- * pointers may be available to help in this decision.
- *
- * This implementation is substantially constrained by wanting to treat the
- * input as a data stream; this means we can't back up.  (For instance, we
- * generally can't fseek() if the input is a Unix pipe.)  Therefore, we have
- * only the following actions to work with:
- *   1. Do nothing, let the entropy decoder resume at next byte of file.
- *   2. Read forward until we find another marker, discarding intervening
- *      data.  (In theory we could look ahead within the current bufferload,
- *      without having to discard data if we don't find the desired marker.
- *      This idea is not implemented here, in part because it makes behavior
- *      dependent on buffer size and chance buffer-boundary positions.)
- *   3. Push back the passed marker (with JUNGETC).  This will cause the
- *      entropy decoder to process an empty data segment, inserting dummy
- *      zeroes, and then re-read the marker we pushed back.
- * #2 is appropriate if we think the desired marker lies ahead, while #3 is
- * appropriate if the found marker is a future restart marker (indicating
- * that we have missed the desired restart marker, probably because it got
- * corrupted).
-
- * We apply #2 or #3 if the found marker is a restart marker no more than
- * two counts behind or ahead of the expected one.  We also apply #2 if the
- * found marker is not a legal JPEG marker code (it's certainly bogus data).
- * If the found marker is a restart marker more than 2 counts away, we do #1
- * (too much risk that the marker is erroneous; with luck we will be able to
- * resync at some future point).
- * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
- * overrunning the end of a scan.  An implementation limited to single-scan
- * files might find it better to apply #2 for markers other than EOI, since
- * any other marker would have to be bogus data in that case.
- */
-
-METHODDEF void
-resync_to_restart (decompress_info_ptr cinfo, int marker)
-{
-  int desired = cinfo->next_restart_num;
-  int action = 1;
-
-  /* Always put up a warning. */
-  WARNMS2(cinfo->emethods,
-	  "Corrupt JPEG data: found 0x%02x marker instead of RST%d",
-	  marker, desired);
-  /* Outer loop handles repeated decision after scanning forward. */
-  for (;;) {
-    if (marker < (int) M_SOF0)
-      action = 2;		/* invalid marker */
-    else if (marker < (int) M_RST0 || marker > (int) M_RST7)
-      action = 3;		/* valid non-restart marker */
-    else {
-      if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
-	  marker == ((int) M_RST0 + ((desired+2) & 7)))
-	action = 3;		/* one of the next two expected restarts */
-      else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
-	       marker == ((int) M_RST0 + ((desired-2) & 7)))
-	action = 2;		/* a prior restart, so advance */
-      else
-	action = 1;		/* desired restart or too far away */
-    }
-    TRACEMS2(cinfo->emethods, 4,
-	     "At marker 0x%02x, recovery action %d", marker, action);
-    switch (action) {
-    case 1:
-      /* Let entropy decoder resume processing. */
-      return;
-    case 2:
-      /* Scan to the next marker, and repeat the decision loop. */
-      marker = next_marker(cinfo);
-      break;
-    case 3:
-      /* Put back this marker & return. */
-      /* Entropy decoder will be forced to process an empty segment. */
-      JUNGETC(marker, cinfo);
-      JUNGETC(0xFF, cinfo);
-      return;
-    }
-  }
-}
-
-
-/*
- * Finish up after a compressed scan (series of read_jpeg_data calls);
- * prepare for another read_scan_header call.
- */
-
-METHODDEF void
-read_scan_trailer (decompress_info_ptr cinfo)
-{
-  /* no work needed */
-}
-
-
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-read_file_trailer (decompress_info_ptr cinfo)
-{
-  /* no work needed */
-}
-
-
-/*
- * The method selection routine for standard JPEG header reading.
- * Note that this must be called by the user interface before calling
- * jpeg_decompress.  When a non-JFIF file is to be decompressed (TIFF,
- * perhaps), the user interface must discover the file type and call
- * the appropriate method selection routine.
- */
-
-GLOBAL void
-jselrjfif (decompress_info_ptr cinfo)
-{
-  cinfo->methods->read_file_header = read_file_header;
-  cinfo->methods->read_scan_header = read_scan_header;
-  /* For JFIF/raw-JPEG format, the user interface supplies read_jpeg_data. */
-#if 0
-  cinfo->methods->read_jpeg_data = read_jpeg_data;
-#endif
-  cinfo->methods->resync_to_restart = resync_to_restart;
-  cinfo->methods->read_scan_trailer = read_scan_trailer;
-  cinfo->methods->read_file_trailer = read_file_trailer;
-}
-
-#endif /* JFIF_SUPPORTED */
diff --git a/jrdppm.c b/jrdppm.c
deleted file mode 100644
index b17dcf7..0000000
--- a/jrdppm.c
+++ /dev/null
@@ -1,459 +0,0 @@
-/*
- * jrdppm.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to read input images in PPM format.
- * The PBMPLUS library is NOT required to compile this software,
- * but it is highly useful as a set of PPM image manipulation programs.
- *
- * These routines may need modification for non-Unix environments or
- * specialized applications.  As they stand, they assume input from
- * an ordinary stdio stream.  They further assume that reading begins
- * at the start of the file; input_init may need work if the
- * user interface has already read some data (e.g., to determine that
- * the file is indeed PPM format).
- *
- * These routines are invoked via the methods get_input_row
- * and input_init/term.
- */
-
-#include "jinclude.h"
-
-#ifdef PPM_SUPPORTED
-
-
-/* Portions of this code are based on the PBMPLUS library, which is:
-**
-** Copyright (C) 1988 by Jef Poskanzer.
-**
-** Permission to use, copy, modify, and distribute this software and its
-** documentation for any purpose and without fee is hereby granted, provided
-** that the above copyright notice appear in all copies and that both that
-** copyright notice and this permission notice appear in supporting
-** documentation.  This software is provided "as is" without express or
-** implied warranty.
-*/
-
-
-/* Macros to deal with unsigned chars as efficiently as compiler allows */
-
-#ifdef HAVE_UNSIGNED_CHAR
-typedef unsigned char U_CHAR;
-#define UCH(x)	((int) (x))
-#else /* !HAVE_UNSIGNED_CHAR */
-#ifdef CHAR_IS_UNSIGNED
-typedef char U_CHAR;
-#define UCH(x)	((int) (x))
-#else
-typedef char U_CHAR;
-#define UCH(x)	((int) (x) & 0xFF)
-#endif
-#endif /* HAVE_UNSIGNED_CHAR */
-
-
-#define	ReadOK(file,buffer,len)	(JFREAD(file,buffer,len) == ((size_t) (len)))
-
-
-/*
- * On most systems, reading individual bytes with getc() is drastically less
- * efficient than buffering a row at a time with fread().  But we must
- * allocate the row buffer in near data space on PCs, because we are assuming
- * small-data memory model, wherein fread() can't reach far memory.  If you
- * need to process very wide images on a PC, you may have to use the getc()
- * approach.  In that case, define USE_GETC_INPUT.
- */
-
-#ifndef USE_GETC_INPUT
-static U_CHAR * row_buffer;	/* holds 1 pixel row's worth of raw input */
-#endif
-
-static JSAMPLE * rescale;	/* => maxval-remapping array, or NULL */
-
-
-LOCAL int
-pbm_getc (FILE * file)
-/* Read next char, skipping over any comments */
-/* A comment/newline sequence is returned as a newline */
-{
-  register int ch;
-  
-  ch = getc(file);
-  if (ch == '#') {
-    do {
-      ch = getc(file);
-    } while (ch != '\n' && ch != EOF);
-  }
-  return ch;
-}
-
-
-LOCAL unsigned int
-read_pbm_integer (compress_info_ptr cinfo)
-/* Read an unsigned decimal integer from the PPM file */
-/* Swallows one trailing character after the integer */
-/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
-/* This should not be a problem in practice. */
-{
-  register int ch;
-  register unsigned int val;
-  
-  /* Skip any leading whitespace */
-  do {
-    ch = pbm_getc(cinfo->input_file);
-    if (ch == EOF)
-      ERREXIT(cinfo->emethods, "Premature EOF in PPM file");
-  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
-  
-  if (ch < '0' || ch > '9')
-    ERREXIT(cinfo->emethods, "Bogus data in PPM file");
-  
-  val = ch - '0';
-  while ((ch = pbm_getc(cinfo->input_file)) >= '0' && ch <= '9') {
-    val *= 10;
-    val += ch - '0';
-  }
-  return val;
-}
-
-
-/*
- * Read one row of pixels.
- *
- * We provide several different versions depending on input file format.
- * In all cases, input is scaled to the size of JSAMPLE; it's possible that
- * when JSAMPLE is 12 bits, this would not really be desirable.
- *
- * Note that a really fast path is provided for reading raw files with
- * maxval = MAXJSAMPLE, which is the normal case (at least for 8-bit JSAMPLEs).
- */
-
-
-METHODDEF void
-get_text_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading text-format PGM files with any maxval */
-{
-  register JSAMPROW ptr0;
-  register unsigned int val;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  for (col = cinfo->image_width; col > 0; col--) {
-    val = read_pbm_integer(cinfo);
-    if (rescale != NULL)
-      val = rescale[val];
-    *ptr0++ = (JSAMPLE) val;
-  }
-}
-
-
-METHODDEF void
-get_text_rgb_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading text-format PPM files with any maxval */
-{
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register unsigned int val;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  for (col = cinfo->image_width; col > 0; col--) {
-    val = read_pbm_integer(cinfo);
-    if (rescale != NULL)
-      val = rescale[val];
-    *ptr0++ = (JSAMPLE) val;
-    val = read_pbm_integer(cinfo);
-    if (rescale != NULL)
-      val = rescale[val];
-    *ptr1++ = (JSAMPLE) val;
-    val = read_pbm_integer(cinfo);
-    if (rescale != NULL)
-      val = rescale[val];
-    *ptr2++ = (JSAMPLE) val;
-  }
-}
-
-
-#ifdef USE_GETC_INPUT
-
-
-METHODDEF void
-get_scaled_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading raw-format PGM files with any maxval */
-{
-  register FILE * infile = cinfo->input_file;
-  register JSAMPROW ptr0;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  for (col = cinfo->image_width; col > 0; col--) {
-    *ptr0++ = rescale[getc(infile)];
-  }
-}
-
-
-METHODDEF void
-get_scaled_rgb_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading raw-format PPM files with any maxval */
-{
-  register FILE * infile = cinfo->input_file;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  for (col = cinfo->image_width; col > 0; col--) {
-    *ptr0++ = rescale[getc(infile)];
-    *ptr1++ = rescale[getc(infile)];
-    *ptr2++ = rescale[getc(infile)];
-  }
-}
-
-
-METHODDEF void
-get_raw_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading raw-format PGM files with maxval = MAXJSAMPLE */
-{
-  register FILE * infile = cinfo->input_file;
-  register JSAMPROW ptr0;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  for (col = cinfo->image_width; col > 0; col--) {
-    *ptr0++ = (JSAMPLE) getc(infile);
-  }
-}
-
-
-METHODDEF void
-get_raw_rgb_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading raw-format PPM files with maxval = MAXJSAMPLE */
-{
-  register FILE * infile = cinfo->input_file;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  for (col = cinfo->image_width; col > 0; col--) {
-    *ptr0++ = (JSAMPLE) getc(infile);
-    *ptr1++ = (JSAMPLE) getc(infile);
-    *ptr2++ = (JSAMPLE) getc(infile);
-  }
-}
-
-
-#else /* use row buffering */
-
-
-METHODDEF void
-get_scaled_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading raw-format PGM files with any maxval */
-{
-  register JSAMPROW ptr0;
-  register U_CHAR * row_bufferptr;
-  register long col;
-  
-  if (! ReadOK(cinfo->input_file, row_buffer, cinfo->image_width))
-    ERREXIT(cinfo->emethods, "Premature EOF in PPM file");
-  ptr0 = pixel_row[0];
-  row_bufferptr = row_buffer;
-  for (col = cinfo->image_width; col > 0; col--) {
-    *ptr0++ = rescale[UCH(*row_bufferptr++)];
-  }
-}
-
-
-METHODDEF void
-get_scaled_rgb_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading raw-format PPM files with any maxval */
-{
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register U_CHAR * row_bufferptr;
-  register long col;
-  
-  if (! ReadOK(cinfo->input_file, row_buffer, 3 * cinfo->image_width))
-    ERREXIT(cinfo->emethods, "Premature EOF in PPM file");
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  row_bufferptr = row_buffer;
-  for (col = cinfo->image_width; col > 0; col--) {
-    *ptr0++ = rescale[UCH(*row_bufferptr++)];
-    *ptr1++ = rescale[UCH(*row_bufferptr++)];
-    *ptr2++ = rescale[UCH(*row_bufferptr++)];
-  }
-}
-
-
-METHODDEF void
-get_raw_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading raw-format PGM files with maxval = MAXJSAMPLE */
-{
-  register JSAMPROW ptr0;
-  register U_CHAR * row_bufferptr;
-  register long col;
-  
-  if (! ReadOK(cinfo->input_file, row_buffer, cinfo->image_width))
-    ERREXIT(cinfo->emethods, "Premature EOF in PPM file");
-  ptr0 = pixel_row[0];
-  row_bufferptr = row_buffer;
-  for (col = cinfo->image_width; col > 0; col--) {
-    *ptr0++ = (JSAMPLE) UCH(*row_bufferptr++);
-  }
-}
-
-
-METHODDEF void
-get_raw_rgb_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading raw-format PPM files with maxval = MAXJSAMPLE */
-{
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register U_CHAR * row_bufferptr;
-  register long col;
-  
-  if (! ReadOK(cinfo->input_file, row_buffer, 3 * cinfo->image_width))
-    ERREXIT(cinfo->emethods, "Premature EOF in PPM file");
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  row_bufferptr = row_buffer;
-  for (col = cinfo->image_width; col > 0; col--) {
-    *ptr0++ = (JSAMPLE) UCH(*row_bufferptr++);
-    *ptr1++ = (JSAMPLE) UCH(*row_bufferptr++);
-    *ptr2++ = (JSAMPLE) UCH(*row_bufferptr++);
-  }
-}
-
-
-#endif /* USE_GETC_INPUT */
-
-
-/*
- * Read the file header; return image size and component count.
- */
-
-METHODDEF void
-input_init (compress_info_ptr cinfo)
-{
-  int c;
-  unsigned int w, h, maxval;
-
-  if (getc(cinfo->input_file) != 'P')
-    ERREXIT(cinfo->emethods, "Not a PPM file");
-
-  c = getc(cinfo->input_file);	/* save format discriminator for a sec */
-
-  w = read_pbm_integer(cinfo);	/* while we fetch the header info */
-  h = read_pbm_integer(cinfo);
-  maxval = read_pbm_integer(cinfo);
-
-  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
-    ERREXIT(cinfo->emethods, "Not a PPM file");
-
-  switch (c) {
-  case '2':			/* it's a text-format PGM file */
-    cinfo->methods->get_input_row = get_text_gray_row;
-    cinfo->input_components = 1;
-    cinfo->in_color_space = CS_GRAYSCALE;
-    TRACEMS2(cinfo->emethods, 1, "%ux%u text PGM image", w, h);
-    break;
-
-  case '3':			/* it's a text-format PPM file */
-    cinfo->methods->get_input_row = get_text_rgb_row;
-    cinfo->input_components = 3;
-    cinfo->in_color_space = CS_RGB;
-    TRACEMS2(cinfo->emethods, 1, "%ux%u text PPM image", w, h);
-    break;
-
-  case '5':			/* it's a raw-format PGM file */
-    if (maxval == MAXJSAMPLE)
-      cinfo->methods->get_input_row = get_raw_gray_row;
-    else
-      cinfo->methods->get_input_row = get_scaled_gray_row;
-    cinfo->input_components = 1;
-    cinfo->in_color_space = CS_GRAYSCALE;
-#ifndef USE_GETC_INPUT
-    /* allocate space for row buffer: 1 byte/pixel */
-    row_buffer = (U_CHAR *) (*cinfo->emethods->alloc_small)
-			((size_t) (SIZEOF(U_CHAR) * (long) w));
-#endif
-    TRACEMS2(cinfo->emethods, 1, "%ux%u PGM image", w, h);
-    break;
-
-  case '6':			/* it's a raw-format PPM file */
-    if (maxval == MAXJSAMPLE)
-      cinfo->methods->get_input_row = get_raw_rgb_row;
-    else
-      cinfo->methods->get_input_row = get_scaled_rgb_row;
-    cinfo->input_components = 3;
-    cinfo->in_color_space = CS_RGB;
-#ifndef USE_GETC_INPUT
-    /* allocate space for row buffer: 3 bytes/pixel */
-    row_buffer = (U_CHAR *) (*cinfo->emethods->alloc_small)
-			((size_t) (3 * SIZEOF(U_CHAR) * (long) w));
-#endif
-    TRACEMS2(cinfo->emethods, 1, "%ux%u PPM image", w, h);
-    break;
-
-  default:
-    ERREXIT(cinfo->emethods, "Not a PPM file");
-    break;
-  }
-
-  /* Compute the rescaling array if necessary */
-  /* This saves per-pixel calculation */
-  if (maxval == MAXJSAMPLE)
-    rescale = NULL;		/* no rescaling required */
-  else {
-    INT32 val, half_maxval;
-
-    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
-    rescale = (JSAMPLE *) (*cinfo->emethods->alloc_small)
-			((size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE)));
-    half_maxval = maxval / 2;
-    for (val = 0; val <= (INT32) maxval; val++) {
-      /* The multiplication here must be done in 32 bits to avoid overflow */
-      rescale[val] = (JSAMPLE) ((val * MAXJSAMPLE + half_maxval) / maxval);
-    }
-  }
-
-  cinfo->image_width = w;
-  cinfo->image_height = h;
-  cinfo->data_precision = BITS_IN_JSAMPLE;
-}
-
-
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-input_term (compress_info_ptr cinfo)
-{
-  /* no work (we let free_all release the workspace) */
-}
-
-
-/*
- * The method selection routine for PPM format input.
- * Note that this must be called by the user interface before calling
- * jpeg_compress.  If multiple input formats are supported, the
- * user interface is responsible for discovering the file format and
- * calling the appropriate method selection routine.
- */
-
-GLOBAL void
-jselrppm (compress_info_ptr cinfo)
-{
-  cinfo->methods->input_init = input_init;
-  /* cinfo->methods->get_input_row is set by input_init */
-  cinfo->methods->input_term = input_term;
-}
-
-#endif /* PPM_SUPPORTED */
diff --git a/jrdrle.c b/jrdrle.c
deleted file mode 100644
index fe4779b..0000000
--- a/jrdrle.c
+++ /dev/null
@@ -1,366 +0,0 @@
-/*
- * jrdrle.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to read input images in Utah RLE format.
- * The Utah Raster Toolkit library is required (version 3.0).
- *
- * These routines may need modification for non-Unix environments or
- * specialized applications.  As they stand, they assume input from
- * an ordinary stdio stream.  They further assume that reading begins
- * at the start of the file; input_init may need work if the
- * user interface has already read some data (e.g., to determine that
- * the file is indeed RLE format).
- *
- * These routines are invoked via the methods get_input_row
- * and input_init/term.
- *
- * Based on code contributed by Mike Lijewski.
- */
-
-#include "jinclude.h"
-
-#ifdef RLE_SUPPORTED
-
-/* rle.h is provided by the Utah Raster Toolkit. */
-
-#include <rle.h>
-
-
-/*
- * load_image assumes that JSAMPLE has the same representation as rle_pixel,
- * to wit, "unsigned char".  Hence we can't cope with 12- or 16-bit samples.
- */
-
-#ifndef EIGHT_BIT_SAMPLES
-  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
-#endif
-
-
-/*
- * We support the following types of RLE files:
- *   
- *   GRAYSCALE   - 8 bits, no colormap
- *   PSEUDOCOLOR - 8 bits, colormap
- *   TRUECOLOR   - 24 bits, colormap
- *   DIRECTCOLOR - 24 bits, no colormap
- *
- * For now, we ignore any alpha channel in the image.
- */
-
-typedef enum { GRAYSCALE, PSEUDOCOLOR, TRUECOLOR, DIRECTCOLOR } rle_kind;
-
-static rle_kind visual;		/* actual type of input file */
-
-/*
- * Since RLE stores scanlines bottom-to-top, we have to invert the image
- * to conform to JPEG's top-to-bottom order.  To do this, we read the
- * incoming image into a virtual array on the first get_input_row call,
- * then fetch the required row from the virtual array on subsequent calls.
- */
-
-static big_sarray_ptr image;	/* single array for GRAYSCALE/PSEUDOCOLOR */
-static big_sarray_ptr red_channel; /* three arrays for TRUECOLOR/DIRECTCOLOR */
-static big_sarray_ptr green_channel;
-static big_sarray_ptr blue_channel;
-static long cur_row_number;	/* last row# read from virtual array */
-
-static rle_hdr header;		/* Input file information */
-static rle_map *colormap;	/* RLE colormap, if any */
-
-
-/*
- * Read the file header; return image size and component count.
- */
-
-METHODDEF void
-input_init (compress_info_ptr cinfo)
-{
-  long width, height;
-
-  /* Use RLE library routine to get the header info */
-  header.rle_file = cinfo->input_file;
-  switch (rle_get_setup(&header)) {
-  case RLE_SUCCESS:
-    /* A-OK */
-    break;
-  case RLE_NOT_RLE:
-    ERREXIT(cinfo->emethods, "Not an RLE file");
-    break;
-  case RLE_NO_SPACE:
-    ERREXIT(cinfo->emethods, "Insufficient memory for RLE header");
-    break;
-  case RLE_EMPTY:
-    ERREXIT(cinfo->emethods, "Empty RLE file");
-    break;
-  case RLE_EOF:
-    ERREXIT(cinfo->emethods, "Premature EOF in RLE header");
-    break;
-  default:
-    ERREXIT(cinfo->emethods, "Bogus RLE error code");
-    break;
-  }
-
-  /* Figure out what we have, set private vars and return values accordingly */
-  
-  width  = header.xmax - header.xmin + 1;
-  height = header.ymax - header.ymin + 1;
-  header.xmin = 0;		/* realign horizontally */
-  header.xmax = width-1;
-
-  cinfo->image_width      = width;
-  cinfo->image_height     = height;
-  cinfo->data_precision   = 8;  /* we can only handle 8 bit data */
-
-  if (header.ncolors == 1 && header.ncmap == 0) {
-    visual     = GRAYSCALE;
-    TRACEMS(cinfo->emethods, 1, "Gray-scale RLE file");
-  } else if (header.ncolors == 1 && header.ncmap == 3) {
-    visual     = PSEUDOCOLOR;
-    colormap   = header.cmap;
-    TRACEMS1(cinfo->emethods, 1, "Colormapped RLE file with map of length %d",
-	     1 << header.cmaplen);
-  } else if (header.ncolors == 3 && header.ncmap == 3) {
-    visual     = TRUECOLOR;
-    colormap   = header.cmap;
-    TRACEMS1(cinfo->emethods, 1, "Full-color RLE file with map of length %d",
-	     1 << header.cmaplen);
-  } else if (header.ncolors == 3 && header.ncmap == 0) {
-    visual     = DIRECTCOLOR;
-    TRACEMS(cinfo->emethods, 1, "Full-color RLE file");
-  } else
-    ERREXIT(cinfo->emethods, "Can't handle this RLE setup");
-  
-  switch (visual) {
-  case GRAYSCALE:
-    /* request one big array to hold the grayscale image */
-    image = (*cinfo->emethods->request_big_sarray) (width, height, 1L);
-    cinfo->in_color_space   = CS_GRAYSCALE;
-    cinfo->input_components = 1;
-    break;
-  case PSEUDOCOLOR:
-    /* request one big array to hold the pseudocolor image */
-    image = (*cinfo->emethods->request_big_sarray) (width, height, 1L);
-    cinfo->in_color_space   = CS_RGB;
-    cinfo->input_components = 3;
-    break;
-  case TRUECOLOR:
-  case DIRECTCOLOR:
-    /* request three big arrays to hold the RGB channels */
-    red_channel   = (*cinfo->emethods->request_big_sarray) (width, height, 1L);
-    green_channel = (*cinfo->emethods->request_big_sarray) (width, height, 1L);
-    blue_channel  = (*cinfo->emethods->request_big_sarray) (width, height, 1L);
-    cinfo->in_color_space   = CS_RGB;
-    cinfo->input_components = 3;
-    break;
-  }
-
-  cinfo->total_passes++;	/* count file reading as separate pass */
-}
-
-
-/*
- * Read one row of pixels.
- * These are called only after load_image has read the image into
- * the virtual array(s).
- */
-
-
-METHODDEF void
-get_grayscale_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This is used for GRAYSCALE images */
-{
-  JSAMPROW inputrows[1];	/* a pseudo JSAMPARRAY structure */
-
-  cur_row_number--;		/* work down in array */
-  
-  inputrows[0] = *((*cinfo->emethods->access_big_sarray)
-			(image, cur_row_number, FALSE));
-
-  jcopy_sample_rows(inputrows, 0, pixel_row, 0, 1, cinfo->image_width);
-}
-
-
-METHODDEF void
-get_pseudocolor_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This is used for PSEUDOCOLOR images */
-{
-  long col;
-  JSAMPROW image_ptr, ptr0, ptr1, ptr2;
-  int val;
-
-  cur_row_number--;		/* work down in array */
-  
-  image_ptr = *((*cinfo->emethods->access_big_sarray)
-		(image, cur_row_number, FALSE));
-
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  
-  for (col = cinfo->image_width; col > 0; col--) {
-    val = GETJSAMPLE(*image_ptr++);
-    *ptr0++ = colormap[val      ] >> 8;
-    *ptr1++ = colormap[val + 256] >> 8;
-    *ptr2++ = colormap[val + 512] >> 8;
-  }
-}
-
-
-METHODDEF void
-get_truecolor_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This is used for TRUECOLOR images */
-/* The colormap consists of 3 independent lookup tables */
-{
-  long col;
-  JSAMPROW red_ptr, green_ptr, blue_ptr, ptr0, ptr1, ptr2;
-  
-  cur_row_number--;		/* work down in array */
-  
-  red_ptr   = *((*cinfo->emethods->access_big_sarray)
-		(red_channel, cur_row_number, FALSE));
-  green_ptr = *((*cinfo->emethods->access_big_sarray)
-		(green_channel, cur_row_number, FALSE));
-  blue_ptr  = *((*cinfo->emethods->access_big_sarray)
-		(blue_channel, cur_row_number, FALSE));
-  
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  
-  for (col = cinfo->image_width; col > 0; col--) {
-    *ptr0++ = colormap[GETJSAMPLE(*red_ptr++)        ] >> 8;
-    *ptr1++ = colormap[GETJSAMPLE(*green_ptr++) + 256] >> 8;
-    *ptr2++ = colormap[GETJSAMPLE(*blue_ptr++)  + 512] >> 8;
-  }
-}
-
-
-METHODDEF void
-get_directcolor_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This is used for DIRECTCOLOR images */
-{
-  JSAMPROW inputrows[3];	/* a pseudo JSAMPARRAY structure */
-
-  cur_row_number--;		/* work down in array */
-  
-  inputrows[0] = *((*cinfo->emethods->access_big_sarray)
-			(red_channel, cur_row_number, FALSE));
-  inputrows[1] = *((*cinfo->emethods->access_big_sarray)
-			(green_channel, cur_row_number, FALSE));
-  inputrows[2] = *((*cinfo->emethods->access_big_sarray)
-			(blue_channel, cur_row_number, FALSE));
-
-  jcopy_sample_rows(inputrows, 0, pixel_row, 0, 3, cinfo->image_width);
-}
-
-
-/*
- * Load the color channels into separate arrays.  We have to do
- * this because RLE files start at the lower left while the JPEG standard
- * has them starting in the upper left.  This is called the first time
- * we want to get a row of input.  What we do is load the RLE data into
- * big arrays and then call the appropriate routine to read one row from
- * the big arrays.  We also change cinfo->methods->get_input_row so that
- * subsequent calls go straight to the row-reading routine.
- */
-
-METHODDEF void
-load_image (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-{
-  long row;
-  rle_pixel *rle_row[3];
-  
-  /* Read the RLE data into our virtual array(s).
-   * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
-   * and (b) we are not on a machine where FAR pointers differ from regular.
-   */
-  RLE_CLR_BIT(header, RLE_ALPHA); /* don't read the alpha channel */
-
-  switch (visual) {
-  case GRAYSCALE:
-  case PSEUDOCOLOR:
-    for (row = 0; row < cinfo->image_height; row++) {
-      (*cinfo->methods->progress_monitor) (cinfo, row, cinfo->image_height);
-      /*
-       * Read a row of the image directly into our big array.
-       * Too bad this doesn't seem to return any indication of errors :-(.
-       */
-      rle_row[0] = (rle_pixel *) *((*cinfo->emethods->access_big_sarray)
-					(image, row, TRUE));
-      rle_getrow(&header, rle_row);
-    }
-    break;
-  case TRUECOLOR:
-  case DIRECTCOLOR:
-    for (row = 0; row < cinfo->image_height; row++) {
-      (*cinfo->methods->progress_monitor) (cinfo, row, cinfo->image_height);
-      /*
-       * Read a row of the image directly into our big arrays.
-       * Too bad this doesn't seem to return any indication of errors :-(.
-       */
-      rle_row[0] = (rle_pixel *) *((*cinfo->emethods->access_big_sarray)
-					(red_channel, row, TRUE));
-      rle_row[1] = (rle_pixel *) *((*cinfo->emethods->access_big_sarray)
-					(green_channel, row, TRUE));
-      rle_row[2] = (rle_pixel *) *((*cinfo->emethods->access_big_sarray)
-					(blue_channel, row, TRUE));
-      rle_getrow(&header, rle_row);
-    }
-    break;
-  }
-  cinfo->completed_passes++;
-  
-  /* Set up to call proper row-extraction routine in future */
-  switch (visual) {
-  case GRAYSCALE:
-    cinfo->methods->get_input_row = get_grayscale_row;
-    break;
-  case PSEUDOCOLOR:
-    cinfo->methods->get_input_row = get_pseudocolor_row;
-    break;
-  case TRUECOLOR:
-    cinfo->methods->get_input_row = get_truecolor_row;
-    break;
-  case DIRECTCOLOR:
-    cinfo->methods->get_input_row = get_directcolor_row;
-    break;
-  }
-
-  /* And fetch the topmost (bottommost) row */
-  cur_row_number = cinfo->image_height;
-  (*cinfo->methods->get_input_row) (cinfo, pixel_row);   
-}
-
-
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-input_term (compress_info_ptr cinfo)
-{
-  /* no work (we let free_all release the workspace) */
-}
-
-
-/*
- * The method selection routine for RLE format input.
- * Note that this must be called by the user interface before calling
- * jpeg_compress.  If multiple input formats are supported, the
- * user interface is responsible for discovering the file format and
- * calling the appropriate method selection routine.
- */
-
-GLOBAL void
-jselrrle (compress_info_ptr cinfo)
-{
-  cinfo->methods->input_init    = input_init;
-  cinfo->methods->get_input_row = load_image; /* until first call */
-  cinfo->methods->input_term    = input_term;
-}
-
-#endif /* RLE_SUPPORTED */
diff --git a/jrdtarga.c b/jrdtarga.c
deleted file mode 100644
index aefc6ab..0000000
--- a/jrdtarga.c
+++ /dev/null
@@ -1,470 +0,0 @@
-/*
- * jrdtarga.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to read input images in Targa format.
- *
- * These routines may need modification for non-Unix environments or
- * specialized applications.  As they stand, they assume input from
- * an ordinary stdio stream.  They further assume that reading begins
- * at the start of the file; input_init may need work if the
- * user interface has already read some data (e.g., to determine that
- * the file is indeed Targa format).
- *
- * These routines are invoked via the methods get_input_row
- * and input_init/term.
- *
- * Based on code contributed by Lee Daniel Crocker.
- */
-
-#include "jinclude.h"
-
-#ifdef TARGA_SUPPORTED
-
-
-/* Macros to deal with unsigned chars as efficiently as compiler allows */
-
-#ifdef HAVE_UNSIGNED_CHAR
-typedef unsigned char U_CHAR;
-#define UCH(x)	((int) (x))
-#else /* !HAVE_UNSIGNED_CHAR */
-#ifdef CHAR_IS_UNSIGNED
-typedef char U_CHAR;
-#define UCH(x)	((int) (x))
-#else
-typedef char U_CHAR;
-#define UCH(x)	((int) (x) & 0xFF)
-#endif
-#endif /* HAVE_UNSIGNED_CHAR */
-
-
-#define	ReadOK(file,buffer,len)	(JFREAD(file,buffer,len) == ((size_t) (len)))
-
-
-static JSAMPARRAY colormap;	/* Targa colormap (converted to my format) */
-
-static big_sarray_ptr whole_image; /* Needed if funny input row order */
-static long current_row;	/* Current logical row number to read */
-
-/* Pointer to routine to extract next Targa pixel from input file */
-static void (*read_pixel) PP((compress_info_ptr cinfo));
-
-/* Result of read_pixel is delivered here: */
-static U_CHAR tga_pixel[4];
-
-static int pixel_size;		/* Bytes per Targa pixel (1 to 4) */
-
-/* State info for reading RLE-coded pixels; both counts must be init to 0 */
-static int block_count;		/* # of pixels remaining in RLE block */
-static int dup_pixel_count;	/* # of times to duplicate previous pixel */
-
-/* This saves the correct pixel-row-expansion method for preload_image */
-static void (*get_pixel_row) PP((compress_info_ptr cinfo,
-				 JSAMPARRAY pixel_row));
-
-
-/* For expanding 5-bit pixel values to 8-bit with best rounding */
-
-static const UINT8 c5to8bits[32] = {
-    0,   8,  16,  24,  32,  41,  49,  57,
-   65,  74,  82,  90,  98, 106, 115, 123,
-  131, 139, 148, 156, 164, 172, 180, 189,
-  197, 205, 213, 222, 230, 238, 246, 255
-};
-
-
-
-LOCAL int
-read_byte (compress_info_ptr cinfo)
-/* Read next byte from Targa file */
-{
-  register FILE *infile = cinfo->input_file;
-  register int c;
-
-  if ((c = getc(infile)) == EOF)
-    ERREXIT(cinfo->emethods, "Premature EOF in Targa file");
-  return c;
-}
-
-
-LOCAL void
-read_colormap (compress_info_ptr cinfo, int cmaplen, int mapentrysize)
-/* Read the colormap from a Targa file */
-{
-  int i;
-
-  /* Presently only handles 24-bit BGR format */
-  if (mapentrysize != 24)
-    ERREXIT(cinfo->emethods, "Unsupported Targa colormap format");
-
-  for (i = 0; i < cmaplen; i++) {
-    colormap[2][i] = (JSAMPLE) read_byte(cinfo);
-    colormap[1][i] = (JSAMPLE) read_byte(cinfo);
-    colormap[0][i] = (JSAMPLE) read_byte(cinfo);
-  }
-}
-
-
-/*
- * read_pixel methods: get a single pixel from Targa file into tga_pixel[]
- */
-
-LOCAL void
-read_non_rle_pixel (compress_info_ptr cinfo)
-/* Read one Targa pixel from the input file; no RLE expansion */
-{
-  register FILE * infile = cinfo->input_file;
-  register int i;
-
-  for (i = 0; i < pixel_size; i++) {
-    tga_pixel[i] = (U_CHAR) getc(infile);
-  }
-}
-
-
-LOCAL void
-read_rle_pixel (compress_info_ptr cinfo)
-/* Read one Targa pixel from the input file, expanding RLE data as needed */
-{
-  register FILE * infile = cinfo->input_file;
-  register int i;
-
-  /* Duplicate previously read pixel? */
-  if (dup_pixel_count > 0) {
-    dup_pixel_count--;
-    return;
-  }
-
-  /* Time to read RLE block header? */
-  if (--block_count < 0) {	/* decrement pixels remaining in block */
-    i = read_byte(cinfo);
-    if (i & 0x80) {		/* Start of duplicate-pixel block? */
-      dup_pixel_count = i & 0x7F; /* number of duplications after this one */
-      block_count = 0;		/* then read new block header */
-    } else {
-      block_count = i & 0x7F;	/* number of pixels after this one */
-    }
-  }
-
-  /* Read next pixel */
-  for (i = 0; i < pixel_size; i++) {
-    tga_pixel[i] = (U_CHAR) getc(infile);
-  }
-}
-
-
-/*
- * Read one row of pixels.
- *
- * We provide several different versions depending on input file format.
- */
-
-
-METHODDEF void
-get_8bit_gray_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading 8-bit grayscale pixels */
-{
-  register JSAMPROW ptr0;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  for (col = cinfo->image_width; col > 0; col--) {
-    (*read_pixel) (cinfo);	/* Load next pixel into tga_pixel */
-    *ptr0++ = (JSAMPLE) UCH(tga_pixel[0]);
-  }
-}
-
-METHODDEF void
-get_8bit_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading 8-bit colormap indexes */
-{
-  register int t;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  for (col = cinfo->image_width; col > 0; col--) {
-    (*read_pixel) (cinfo);	/* Load next pixel into tga_pixel */
-    t = UCH(tga_pixel[0]);
-    *ptr0++ = colormap[0][t];
-    *ptr1++ = colormap[1][t];
-    *ptr2++ = colormap[2][t];
-  }
-}
-
-METHODDEF void
-get_16bit_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading 16-bit pixels */
-{
-  register int t;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  for (col = cinfo->image_width; col > 0; col--) {
-    (*read_pixel) (cinfo);	/* Load next pixel into tga_pixel */
-    t = UCH(tga_pixel[0]);
-    t += UCH(tga_pixel[1]) << 8;
-    /* We expand 5 bit data to 8 bit sample width.
-     * The format of the 16-bit (LSB first) input word is
-     *     xRRRRRGGGGGBBBBB
-     */
-    *ptr2++ = (JSAMPLE) c5to8bits[t & 0x1F];
-    t >>= 5;
-    *ptr1++ = (JSAMPLE) c5to8bits[t & 0x1F];
-    t >>= 5;
-    *ptr0++ = (JSAMPLE) c5to8bits[t & 0x1F];
-  }
-}
-
-METHODDEF void
-get_24bit_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-/* This version is for reading 24-bit pixels */
-{
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  
-  ptr0 = pixel_row[0];
-  ptr1 = pixel_row[1];
-  ptr2 = pixel_row[2];
-  for (col = cinfo->image_width; col > 0; col--) {
-    (*read_pixel) (cinfo);	/* Load next pixel into tga_pixel */
-    *ptr0++ = (JSAMPLE) UCH(tga_pixel[2]); /* convert BGR to RGB order */
-    *ptr1++ = (JSAMPLE) UCH(tga_pixel[1]);
-    *ptr2++ = (JSAMPLE) UCH(tga_pixel[0]);
-  }
-}
-
-/*
- * Targa also defines a 32-bit pixel format with order B,G,R,A.
- * We presently ignore the attribute byte, so the code for reading
- * these pixels is identical to the 24-bit routine above.
- * This works because the actual pixel length is only known to read_pixel.
- */
-
-#define get_32bit_row  get_24bit_row
-
-
-/*
- * This method is for re-reading the input data in standard top-down
- * row order.  The entire image has already been read into whole_image
- * with proper conversion of pixel format, but it's in a funny row order.
- */
-
-METHODDEF void
-get_memory_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-{
-  JSAMPARRAY image_ptr;
-  long source_row;
-
-  /* Compute row of source that maps to current_row of normal order */
-  /* For now, assume image is bottom-up and not interlaced. */
-  /* NEEDS WORK to support interlaced images! */
-  source_row = cinfo->image_height - current_row - 1;
-
-  /* Fetch that row from virtual array */
-  image_ptr = (*cinfo->emethods->access_big_sarray)
-		(whole_image, source_row * cinfo->input_components, FALSE);
-
-  jcopy_sample_rows(image_ptr, 0, pixel_row, 0,
-		    cinfo->input_components, cinfo->image_width);
-
-  current_row++;
-}
-
-
-/*
- * This method loads the image into whole_image during the first call on
- * get_input_row.  The get_input_row pointer is then adjusted to call
- * get_memory_row on subsequent calls.
- */
-
-METHODDEF void
-preload_image (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
-{
-  JSAMPARRAY image_ptr;
-  long row;
-
-  /* Read the data into a virtual array in input-file row order */
-  for (row = 0; row < cinfo->image_height; row++) {
-    (*cinfo->methods->progress_monitor) (cinfo, row, cinfo->image_height);
-    image_ptr = (*cinfo->emethods->access_big_sarray)
-			(whole_image, row * cinfo->input_components, TRUE);
-    (*get_pixel_row) (cinfo, image_ptr);
-  }
-  cinfo->completed_passes++;
-
-  /* Set up to read from the virtual array in unscrambled order */
-  cinfo->methods->get_input_row = get_memory_row;
-  current_row = 0;
-  /* And read the first row */
-  get_memory_row(cinfo, pixel_row);
-}
-
-
-/*
- * Read the file header; return image size and component count.
- */
-
-METHODDEF void
-input_init (compress_info_ptr cinfo)
-{
-  U_CHAR targaheader[18];
-  int idlen, cmaptype, subtype, flags, interlace_type, components;
-  UINT16 width, height, maplen;
-  boolean is_bottom_up;
-
-#define GET_2B(offset)	((unsigned int) UCH(targaheader[offset]) + \
-			 (((unsigned int) UCH(targaheader[offset+1])) << 8))
-  
-  if (! ReadOK(cinfo->input_file, targaheader, 18))
-    ERREXIT(cinfo->emethods, "Unexpected end of file");
-
-  /* Pretend "15-bit" pixels are 16-bit --- we ignore attribute bit anyway */
-  if (targaheader[16] == 15)
-    targaheader[16] = 16;
-
-  idlen = UCH(targaheader[0]);
-  cmaptype = UCH(targaheader[1]);
-  subtype = UCH(targaheader[2]);
-  maplen = GET_2B(5);
-  width = GET_2B(12);
-  height = GET_2B(14);
-  pixel_size = UCH(targaheader[16]) >> 3;
-  flags = UCH(targaheader[17]);	/* Image Descriptor byte */
-
-  is_bottom_up = ((flags & 0x20) == 0);	/* bit 5 set => top-down */
-  interlace_type = flags >> 6;	/* bits 6/7 are interlace code */
-
-  if (cmaptype > 1 ||		/* cmaptype must be 0 or 1 */
-      pixel_size < 1 || pixel_size > 4 ||
-      (UCH(targaheader[16]) & 7) != 0 || /* bits/pixel must be multiple of 8 */
-      interlace_type != 0)	/* currently don't allow interlaced image */
-    ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
-  
-  if (subtype > 8) {
-    /* It's an RLE-coded file */
-    read_pixel = read_rle_pixel;
-    block_count = dup_pixel_count = 0;
-    subtype -= 8;
-  } else {
-    /* Non-RLE file */
-    read_pixel = read_non_rle_pixel;
-  }
-
-  /* Now should have subtype 1, 2, or 3 */
-  components = 3;		/* until proven different */
-  cinfo->in_color_space = CS_RGB;
-
-  switch (subtype) {
-  case 1:			/* colormapped image */
-    if (pixel_size == 1 && cmaptype == 1)
-      get_pixel_row = get_8bit_row;
-    else
-      ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
-    TRACEMS2(cinfo->emethods, 1, "%ux%u colormapped Targa image",
-	     width, height);
-    break;
-  case 2:			/* RGB image */
-    switch (pixel_size) {
-    case 2:
-      get_pixel_row = get_16bit_row;
-      break;
-    case 3:
-      get_pixel_row = get_24bit_row;
-      break;
-    case 4:
-      get_pixel_row = get_32bit_row;
-      break;
-    default:
-      ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
-      break;
-    }
-    TRACEMS2(cinfo->emethods, 1, "%ux%u RGB Targa image",
-	     width, height);
-    break;
-  case 3:			/* Grayscale image */
-    components = 1;
-    cinfo->in_color_space = CS_GRAYSCALE;
-    if (pixel_size == 1)
-      get_pixel_row = get_8bit_gray_row;
-    else
-      ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
-    TRACEMS2(cinfo->emethods, 1, "%ux%u grayscale Targa image",
-	     width, height);
-    break;
-  default:
-    ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
-    break;
-  }
-
-  if (is_bottom_up) {
-    whole_image = (*cinfo->emethods->request_big_sarray)
-			((long) width, (long) height * components,
-			 (long) components);
-    cinfo->methods->get_input_row = preload_image;
-    cinfo->total_passes++;	/* count file reading as separate pass */
-  } else {
-    whole_image = NULL;
-    cinfo->methods->get_input_row = get_pixel_row;
-  }
-  
-  while (idlen--)		/* Throw away ID field */
-    (void) read_byte(cinfo);
-
-  if (maplen > 0) {
-    if (maplen > 256 || GET_2B(3) != 0)
-      ERREXIT(cinfo->emethods, "Colormap too large");
-    /* Allocate space to store the colormap */
-    colormap = (*cinfo->emethods->alloc_small_sarray)
-			((long) maplen, 3L);
-    /* and read it from the file */
-    read_colormap(cinfo, (int) maplen, UCH(targaheader[7]));
-  } else {
-    if (cmaptype)		/* but you promised a cmap! */
-      ERREXIT(cinfo->emethods, "Invalid or unsupported Targa file");
-    colormap = NULL;
-  }
-
-  cinfo->input_components = components;
-  cinfo->image_width = width;
-  cinfo->image_height = height;
-  cinfo->data_precision = 8;	/* always, even if 12-bit JSAMPLEs */
-}
-
-
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-input_term (compress_info_ptr cinfo)
-{
-  /* no work (we let free_all release the workspace) */
-}
-
-
-/*
- * The method selection routine for Targa format input.
- * Note that this must be called by the user interface before calling
- * jpeg_compress.  If multiple input formats are supported, the
- * user interface is responsible for discovering the file format and
- * calling the appropriate method selection routine.
- */
-
-GLOBAL void
-jselrtarga (compress_info_ptr cinfo)
-{
-  cinfo->methods->input_init = input_init;
-  /* cinfo->methods->get_input_row is set by input_init */
-  cinfo->methods->input_term = input_term;
-}
-
-#endif /* TARGA_SUPPORTED */
diff --git a/jrevdct.c b/jrevdct.c
deleted file mode 100644
index 3203b7e..0000000
--- a/jrevdct.c
+++ /dev/null
@@ -1,369 +0,0 @@
-/*
- * jrevdct.c
- *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains the basic inverse-DCT transformation subroutine.
- *
- * This implementation is based on an algorithm described in
- *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
- *   Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
- *   Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
- * The primary algorithm described there uses 11 multiplies and 29 adds.
- * We use their alternate method with 12 multiplies and 32 adds.
- * The advantage of this method is that no data path contains more than one
- * multiplication; this allows a very simple and accurate implementation in
- * scaled fixed-point arithmetic, with a minimal number of shifts.
- */
-
-#include "jinclude.h"
-
-/*
- * This routine is specialized to the case DCTSIZE = 8.
- */
-
-#if DCTSIZE != 8
-  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
-#endif
-
-
-/*
- * A 2-D IDCT can be done by 1-D IDCT on each row followed by 1-D IDCT
- * on each column.  Direct algorithms are also available, but they are
- * much more complex and seem not to be any faster when reduced to code.
- *
- * The poop on this scaling stuff is as follows:
- *
- * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
- * larger than the true IDCT outputs.  The final outputs are therefore
- * a factor of N larger than desired; since N=8 this can be cured by
- * a simple right shift at the end of the algorithm.  The advantage of
- * this arrangement is that we save two multiplications per 1-D IDCT,
- * because the y0 and y4 inputs need not be divided by sqrt(N).
- *
- * We have to do addition and subtraction of the integer inputs, which
- * is no problem, and multiplication by fractional constants, which is
- * a problem to do in integer arithmetic.  We multiply all the constants
- * by CONST_SCALE and convert them to integer constants (thus retaining
- * CONST_BITS bits of precision in the constants).  After doing a
- * multiplication we have to divide the product by CONST_SCALE, with proper
- * rounding, to produce the correct output.  This division can be done
- * cheaply as a right shift of CONST_BITS bits.  We postpone shifting
- * as long as possible so that partial sums can be added together with
- * full fractional precision.
- *
- * The outputs of the first pass are scaled up by PASS1_BITS bits so that
- * they are represented to better-than-integral precision.  These outputs
- * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
- * with the recommended scaling.  (To scale up 12-bit sample data further, an
- * intermediate INT32 array would be needed.)
- *
- * To avoid overflow of the 32-bit intermediate results in pass 2, we must
- * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
- * shows that the values given below are the most effective.
- */
-
-#ifdef EIGHT_BIT_SAMPLES
-#define CONST_BITS  13
-#define PASS1_BITS  2
-#else
-#define CONST_BITS  13
-#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
-#endif
-
-#define ONE	((INT32) 1)
-
-#define CONST_SCALE (ONE << CONST_BITS)
-
-/* Convert a positive real constant to an integer scaled by CONST_SCALE. */
-
-#define FIX(x)	((INT32) ((x) * CONST_SCALE + 0.5))
-
-/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
- * causing a lot of useless floating-point operations at run time.
- * To get around this we use the following pre-calculated constants.
- * If you change CONST_BITS you may want to add appropriate values.
- * (With a reasonable C compiler, you can just rely on the FIX() macro...)
- */
-
-#if CONST_BITS == 13
-#define FIX_0_298631336  ((INT32)  2446)	/* FIX(0.298631336) */
-#define FIX_0_390180644  ((INT32)  3196)	/* FIX(0.390180644) */
-#define FIX_0_541196100  ((INT32)  4433)	/* FIX(0.541196100) */
-#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
-#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
-#define FIX_1_175875602  ((INT32)  9633)	/* FIX(1.175875602) */
-#define FIX_1_501321110  ((INT32)  12299)	/* FIX(1.501321110) */
-#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
-#define FIX_1_961570560  ((INT32)  16069)	/* FIX(1.961570560) */
-#define FIX_2_053119869  ((INT32)  16819)	/* FIX(2.053119869) */
-#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
-#define FIX_3_072711026  ((INT32)  25172)	/* FIX(3.072711026) */
-#else
-#define FIX_0_298631336  FIX(0.298631336)
-#define FIX_0_390180644  FIX(0.390180644)
-#define FIX_0_541196100  FIX(0.541196100)
-#define FIX_0_765366865  FIX(0.765366865)
-#define FIX_0_899976223  FIX(0.899976223)
-#define FIX_1_175875602  FIX(1.175875602)
-#define FIX_1_501321110  FIX(1.501321110)
-#define FIX_1_847759065  FIX(1.847759065)
-#define FIX_1_961570560  FIX(1.961570560)
-#define FIX_2_053119869  FIX(2.053119869)
-#define FIX_2_562915447  FIX(2.562915447)
-#define FIX_3_072711026  FIX(3.072711026)
-#endif
-
-
-/* Descale and correctly round an INT32 value that's scaled by N bits.
- * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
- * the fudge factor is correct for either sign of X.
- */
-
-#define DESCALE(x,n)  RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
-
-/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
- * For 8-bit samples with the recommended scaling, all the variable
- * and constant values involved are no more than 16 bits wide, so a
- * 16x16->32 bit multiply can be used instead of a full 32x32 multiply;
- * this provides a useful speedup on many machines.
- * There is no way to specify a 16x16->32 multiply in portable C, but
- * some C compilers will do the right thing if you provide the correct
- * combination of casts.
- * NB: for 12-bit samples, a full 32-bit multiplication will be needed.
- */
-
-#ifdef EIGHT_BIT_SAMPLES
-#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
-#define MULTIPLY(var,const)  (((INT16) (var)) * ((INT16) (const)))
-#endif
-#ifdef SHORTxLCONST_32		/* known to work with Microsoft C 6.0 */
-#define MULTIPLY(var,const)  (((INT16) (var)) * ((INT32) (const)))
-#endif
-#endif
-
-#ifndef MULTIPLY		/* default definition */
-#define MULTIPLY(var,const)  ((var) * (const))
-#endif
-
-
-/*
- * Perform the inverse DCT on one block of coefficients.
- */
-
-GLOBAL void
-j_rev_dct (DCTBLOCK data)
-{
-  INT32 tmp0, tmp1, tmp2, tmp3;
-  INT32 tmp10, tmp11, tmp12, tmp13;
-  INT32 z1, z2, z3, z4, z5;
-  register DCTELEM *dataptr;
-  int rowctr;
-  SHIFT_TEMPS
-
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-
-  dataptr = data;
-  for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
-    /* Due to quantization, we will usually find that many of the input
-     * coefficients are zero, especially the AC terms.  We can exploit this
-     * by short-circuiting the IDCT calculation for any row in which all
-     * the AC terms are zero.  In that case each output is equal to the
-     * DC coefficient (with scale factor as needed).
-     * With typical images and quantization tables, half or more of the
-     * row DCT calculations can be simplified this way.
-     */
-
-    if ((dataptr[1] | dataptr[2] | dataptr[3] | dataptr[4] |
-	 dataptr[5] | dataptr[6] | dataptr[7]) == 0) {
-      /* AC terms all zero */
-      DCTELEM dcval = (DCTELEM) (dataptr[0] << PASS1_BITS);
-      
-      dataptr[0] = dcval;
-      dataptr[1] = dcval;
-      dataptr[2] = dcval;
-      dataptr[3] = dcval;
-      dataptr[4] = dcval;
-      dataptr[5] = dcval;
-      dataptr[6] = dcval;
-      dataptr[7] = dcval;
-      
-      dataptr += DCTSIZE;	/* advance pointer to next row */
-      continue;
-    }
-
-    /* Even part: reverse the even part of the forward DCT. */
-    /* The rotator is sqrt(2)*c(-6). */
-
-    z2 = (INT32) dataptr[2];
-    z3 = (INT32) dataptr[6];
-
-    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
-    tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
-    tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
-
-    tmp0 = ((INT32) dataptr[0] + (INT32) dataptr[4]) << CONST_BITS;
-    tmp1 = ((INT32) dataptr[0] - (INT32) dataptr[4]) << CONST_BITS;
-
-    tmp10 = tmp0 + tmp3;
-    tmp13 = tmp0 - tmp3;
-    tmp11 = tmp1 + tmp2;
-    tmp12 = tmp1 - tmp2;
-    
-    /* Odd part per figure 8; the matrix is unitary and hence its
-     * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
-     */
-
-    tmp0 = (INT32) dataptr[7];
-    tmp1 = (INT32) dataptr[5];
-    tmp2 = (INT32) dataptr[3];
-    tmp3 = (INT32) dataptr[1];
-
-    z1 = tmp0 + tmp3;
-    z2 = tmp1 + tmp2;
-    z3 = tmp0 + tmp2;
-    z4 = tmp1 + tmp3;
-    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
-    
-    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
-    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
-    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
-    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
-    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
-    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
-    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
-    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
-    
-    z3 += z5;
-    z4 += z5;
-    
-    tmp0 += z1 + z3;
-    tmp1 += z2 + z4;
-    tmp2 += z2 + z3;
-    tmp3 += z1 + z4;
-
-    /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
-
-    dataptr[0] = (DCTELEM) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
-    dataptr[7] = (DCTELEM) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
-    dataptr[1] = (DCTELEM) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
-    dataptr[6] = (DCTELEM) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
-    dataptr[2] = (DCTELEM) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
-    dataptr[5] = (DCTELEM) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
-    dataptr[3] = (DCTELEM) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
-    dataptr[4] = (DCTELEM) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
-
-    dataptr += DCTSIZE;		/* advance pointer to next row */
-  }
-
-  /* Pass 2: process columns. */
-  /* Note that we must descale the results by a factor of 8 == 2**3, */
-  /* and also undo the PASS1_BITS scaling. */
-
-  dataptr = data;
-  for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
-    /* Columns of zeroes can be exploited in the same way as we did with rows.
-     * However, the row calculation has created many nonzero AC terms, so the
-     * simplification applies less often (typically 5% to 10% of the time).
-     * On machines with very fast multiplication, it's possible that the
-     * test takes more time than it's worth.  In that case this section
-     * may be commented out.
-     */
-
-#ifndef NO_ZERO_COLUMN_TEST
-    if ((dataptr[DCTSIZE*1] | dataptr[DCTSIZE*2] | dataptr[DCTSIZE*3] |
-	 dataptr[DCTSIZE*4] | dataptr[DCTSIZE*5] | dataptr[DCTSIZE*6] |
-	 dataptr[DCTSIZE*7]) == 0) {
-      /* AC terms all zero */
-      DCTELEM dcval = (DCTELEM) DESCALE((INT32) dataptr[0], PASS1_BITS+3);
-      
-      dataptr[DCTSIZE*0] = dcval;
-      dataptr[DCTSIZE*1] = dcval;
-      dataptr[DCTSIZE*2] = dcval;
-      dataptr[DCTSIZE*3] = dcval;
-      dataptr[DCTSIZE*4] = dcval;
-      dataptr[DCTSIZE*5] = dcval;
-      dataptr[DCTSIZE*6] = dcval;
-      dataptr[DCTSIZE*7] = dcval;
-      
-      dataptr++;		/* advance pointer to next column */
-      continue;
-    }
-#endif
-
-    /* Even part: reverse the even part of the forward DCT. */
-    /* The rotator is sqrt(2)*c(-6). */
-
-    z2 = (INT32) dataptr[DCTSIZE*2];
-    z3 = (INT32) dataptr[DCTSIZE*6];
-
-    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
-    tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
-    tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
-
-    tmp0 = ((INT32) dataptr[DCTSIZE*0] + (INT32) dataptr[DCTSIZE*4]) << CONST_BITS;
-    tmp1 = ((INT32) dataptr[DCTSIZE*0] - (INT32) dataptr[DCTSIZE*4]) << CONST_BITS;
-
-    tmp10 = tmp0 + tmp3;
-    tmp13 = tmp0 - tmp3;
-    tmp11 = tmp1 + tmp2;
-    tmp12 = tmp1 - tmp2;
-    
-    /* Odd part per figure 8; the matrix is unitary and hence its
-     * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
-     */
-
-    tmp0 = (INT32) dataptr[DCTSIZE*7];
-    tmp1 = (INT32) dataptr[DCTSIZE*5];
-    tmp2 = (INT32) dataptr[DCTSIZE*3];
-    tmp3 = (INT32) dataptr[DCTSIZE*1];
-
-    z1 = tmp0 + tmp3;
-    z2 = tmp1 + tmp2;
-    z3 = tmp0 + tmp2;
-    z4 = tmp1 + tmp3;
-    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
-    
-    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
-    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
-    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
-    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
-    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
-    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
-    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
-    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
-    
-    z3 += z5;
-    z4 += z5;
-    
-    tmp0 += z1 + z3;
-    tmp1 += z2 + z4;
-    tmp2 += z2 + z3;
-    tmp3 += z1 + z4;
-
-    /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
-
-    dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp3,
-					   CONST_BITS+PASS1_BITS+3);
-    dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp10 - tmp3,
-					   CONST_BITS+PASS1_BITS+3);
-    dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp11 + tmp2,
-					   CONST_BITS+PASS1_BITS+3);
-    dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(tmp11 - tmp2,
-					   CONST_BITS+PASS1_BITS+3);
-    dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(tmp12 + tmp1,
-					   CONST_BITS+PASS1_BITS+3);
-    dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12 - tmp1,
-					   CONST_BITS+PASS1_BITS+3);
-    dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp13 + tmp0,
-					   CONST_BITS+PASS1_BITS+3);
-    dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp13 - tmp0,
-					   CONST_BITS+PASS1_BITS+3);
-    
-    dataptr++;			/* advance pointer to next column */
-  }
-}
diff --git a/jutils.c b/jutils.c
index 7433e27..ef10174 100644
--- a/jutils.c
+++ b/jutils.c
@@ -1,7 +1,7 @@
 /*
  * jutils.c
  *
- * Copyright (C) 1991, 1992, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -11,14 +11,30 @@
  * a surrounding application.
  */
 
+#define JPEG_INTERNALS
 #include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Arithmetic utilities
+ */
+
+GLOBAL long
+jdiv_round_up (long a, long b)
+/* Compute a/b rounded up to next integer, ie, ceil(a/b) */
+/* Assumes a >= 0, b > 0 */
+{
+  return (a + b - 1L) / b;
+}
 
 
 GLOBAL long
 jround_up (long a, long b)
-/* Compute a rounded up to next multiple of b; a >= 0, b > 0 */
+/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
+/* Assumes a >= 0, b > 0 */
 {
-  a += b-1;
+  a += b - 1L;
   return a - (a % b);
 }
 
@@ -46,10 +62,10 @@
 GLOBAL void
 jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
 		   JSAMPARRAY output_array, int dest_row,
-		   int num_rows, long num_cols)
+		   int num_rows, JDIMENSION num_cols)
 /* Copy some rows of samples from one place to another.
  * num_rows rows are copied from input_array[source_row++]
- * to output_array[dest_row++]; these areas should not overlap.
+ * to output_array[dest_row++]; these areas may overlap for duplication.
  * The source and destination arrays must be at least as wide as num_cols.
  */
 {
@@ -57,7 +73,7 @@
 #ifdef FMEMCOPY
   register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
 #else
-  register long count;
+  register JDIMENSION count;
 #endif
   register int row;
 
@@ -78,7 +94,8 @@
 
 
 GLOBAL void
-jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row, long num_blocks)
+jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
+		 JDIMENSION num_blocks)
 /* Copy a row of coefficient blocks from one place to another. */
 {
 #ifdef FMEMCOPY
@@ -89,7 +106,7 @@
 
   inptr = (JCOEFPTR) input_row;
   outptr = (JCOEFPTR) output_row;
-  for (count = num_blocks * DCTSIZE2; count > 0; count--) {
+  for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
     *outptr++ = *inptr++;
   }
 #endif
diff --git a/jversion.h b/jversion.h
index 4946670..4bc1987 100644
--- a/jversion.h
+++ b/jversion.h
@@ -1,7 +1,7 @@
 /*
  * jversion.h
  *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -9,6 +9,6 @@
  */
 
 
-#define JVERSION	"4A 18-Feb-93"
+#define JVERSION	"5  24-Sep-94"
 
-#define JCOPYRIGHT	"Copyright (C) 1993, Thomas G. Lane"
+#define JCOPYRIGHT	"Copyright (C) 1994, Thomas G. Lane"
diff --git a/jwrgif.c b/jwrgif.c
deleted file mode 100644
index 5519d94..0000000
--- a/jwrgif.c
+++ /dev/null
@@ -1,486 +0,0 @@
-/*
- * jwrgif.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to write output images in GIF format.
- *
- * These routines may need modification for non-Unix environments or
- * specialized applications.  As they stand, they assume output to
- * an ordinary stdio stream.
- *
- * These routines are invoked via the methods put_pixel_rows, put_color_map,
- * and output_init/term.
- */
-
-/*
- * This code is loosely based on ppmtogif from the PBMPLUS distribution
- * of Feb. 1991.  That file contains the following copyright notice:
- *    Based on GIFENCODE by David Rowley <mgardi@watdscu.waterloo.edu>.
- *    Lempel-Ziv compression based on "compress" by Spencer W. Thomas et al.
- *    Copyright (C) 1989 by Jef Poskanzer.
- *    Permission to use, copy, modify, and distribute this software and its
- *    documentation for any purpose and without fee is hereby granted, provided
- *    that the above copyright notice appear in all copies and that both that
- *    copyright notice and this permission notice appear in supporting
- *    documentation.  This software is provided "as is" without express or
- *    implied warranty.
- *
- * We are also required to state that
- *    "The Graphics Interchange Format(c) is the Copyright property of
- *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
- *    CompuServe Incorporated."
- */
-
-#include "jinclude.h"
-
-#ifdef GIF_SUPPORTED
-
-
-static decompress_info_ptr dcinfo; /* to avoid passing to all functions */
-
-#define	MAX_LZW_BITS	12	/* maximum LZW code size (4096 symbols) */
-
-typedef INT16 code_int;		/* must hold -1 .. 2**MAX_LZW_BITS */
-
-#define LZW_TABLE_SIZE	((code_int) 1 << MAX_LZW_BITS)
-
-#define HSIZE		5003	/* hash table size for 80% occupancy */
-
-typedef int hash_int;		/* must hold -2*HSIZE..2*HSIZE */
-
-static int n_bits;		/* current number of bits/code */
-static code_int maxcode;	/* maximum code, given n_bits */
-#define MAXCODE(n_bits)	(((code_int) 1 << (n_bits)) - 1)
-
-static int init_bits;		/* initial n_bits ... restored after clear */
-
-static code_int ClearCode;	/* clear code (doesn't change) */
-static code_int EOFCode;	/* EOF code (ditto) */
-
-static code_int free_code;	/* first not-yet-used symbol code */
-
-/*
- * The LZW hash table consists of three parallel arrays:
- *   hash_code[i]	code of symbol in slot i, or 0 if empty slot
- *   hash_prefix[i]	symbol's prefix code; undefined if empty slot
- *   hash_suffix[i]	symbol's suffix character; undefined if empty slot
- * where slot values (i) range from 0 to HSIZE-1.
- *
- * Algorithm:  use open addressing double hashing (no chaining) on the
- * prefix code / suffix character combination.  We do a variant of Knuth's
- * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
- * secondary probe.
- *
- * The hash tables are allocated from FAR heap space since they would use up
- * rather a lot of the near data space in a PC.
- */
-
-static code_int FAR *hash_code;	/* => hash table of symbol codes */
-static code_int FAR *hash_prefix; /* => hash table of prefix symbols */
-static UINT8 FAR *hash_suffix;	/* => hash table of suffix bytes */
-
-
-/*
- * Routines to package compressed data bytes into GIF data blocks.
- * A data block consists of a count byte (1..255) and that many data bytes.
- */
-
-static int bytesinpkt;		/* # of bytes in current packet */
-static char packetbuf[256];	/* workspace for accumulating packet */
-
-
-LOCAL void
-flush_packet (void)
-/* flush any accumulated data */
-{
-  if (bytesinpkt > 0) {		/* never write zero-length packet */
-    packetbuf[0] = (char) bytesinpkt++;
-    if (JFWRITE(dcinfo->output_file, packetbuf, bytesinpkt)
-	!= (size_t) bytesinpkt)
-      ERREXIT(dcinfo->emethods, "Output file write error --- out of disk space?");
-    bytesinpkt = 0;
-  }
-}
-
-
-/* Add a character to current packet; flush to disk if necessary */
-#define CHAR_OUT(c)  \
-	{ packetbuf[++bytesinpkt] = (char) (c);  \
-	    if (bytesinpkt >= 255)  \
-	      flush_packet();  \
-	}
-
-
-/* Routine to convert variable-width codes into a byte stream */
-
-static INT32 cur_accum;		/* holds bits not yet output */
-static int cur_bits;		/* # of bits in cur_accum */
-
-
-LOCAL void
-output (code_int code)
-/* Emit a code of n_bits bits */
-/* Uses cur_accum and cur_bits to reblock into 8-bit bytes */
-{
-  cur_accum |= ((INT32) code) << cur_bits;
-  cur_bits += n_bits;
-
-  while (cur_bits >= 8) {
-    CHAR_OUT(cur_accum & 0xFF);
-    cur_accum >>= 8;
-    cur_bits -= 8;
-  }
-
-  /*
-   * If the next entry is going to be too big for the code size,
-   * then increase it, if possible.  We do this here to ensure
-   * that it's done in sync with the decoder's codesize increases.
-   */
-  if (free_code > maxcode) {
-    n_bits++;
-    if (n_bits == MAX_LZW_BITS)
-      maxcode = LZW_TABLE_SIZE;	/* free_code will never exceed this */
-    else
-      maxcode = MAXCODE(n_bits);
-  }
-}
-
-
-/* The LZW algorithm proper */
-
-static code_int waiting_code;	/* symbol not yet output; may be extendable */
-static boolean first_byte;	/* if TRUE, waiting_code is not valid */
-
-
-LOCAL void
-clear_hash (void)
-/* Fill the hash table with empty entries */
-{
-  /* It's sufficient to zero hash_code[] */
-  jzero_far((void FAR *) hash_code, HSIZE * SIZEOF(code_int));
-}
-
-
-LOCAL void
-clear_block (void)
-/* Reset compressor and issue a Clear code */
-{
-  clear_hash();			/* delete all the symbols */
-  free_code = ClearCode + 2;
-  output(ClearCode);		/* inform decoder */
-  n_bits = init_bits;		/* reset code size */
-  maxcode = MAXCODE(n_bits);
-}
-
-
-LOCAL void
-compress_init (int i_bits)
-/* Initialize LZW compressor */
-{
-  /* init all the static variables */
-  n_bits = init_bits = i_bits;
-  maxcode = MAXCODE(n_bits);
-  ClearCode = ((code_int) 1 << (init_bits - 1));
-  EOFCode = ClearCode + 1;
-  free_code = ClearCode + 2;
-  first_byte = TRUE;		/* no waiting symbol yet */
-  /* init output buffering vars */
-  bytesinpkt = 0;
-  cur_accum = 0;
-  cur_bits = 0;
-  /* clear hash table */
-  clear_hash();
-  /* GIF specifies an initial Clear code */
-  output(ClearCode);
-}
-
-
-LOCAL void
-compress_byte (int c)
-/* Accept and compress one 8-bit byte */
-{
-  register hash_int i;
-  register hash_int disp;
-
-  if (first_byte) {		/* need to initialize waiting_code */
-    waiting_code = c;
-    first_byte = FALSE;
-    return;
-  }
-
-  /* Probe hash table to see if a symbol exists for
-   * waiting_code followed by c.
-   * If so, replace waiting_code by that symbol and return.
-   */
-  i = ((hash_int) c << (MAX_LZW_BITS-8)) + waiting_code;
-  /* i is less than twice 2**MAX_LZW_BITS, therefore less than twice HSIZE */
-  if (i >= HSIZE)
-    i -= HSIZE;
-  
-  if (hash_code[i] != 0) {	/* is first probed slot empty? */
-    if (hash_prefix[i] == waiting_code && hash_suffix[i] == (UINT8) c) {
-      waiting_code = hash_code[i];
-      return;
-    }
-    if (i == 0)			/* secondary hash (after G. Knott) */
-      disp = 1;
-    else
-      disp = HSIZE - i;
-    while (1) {
-      i -= disp;
-      if (i < 0)
-	i += HSIZE;
-      if (hash_code[i] == 0)
-	break;			/* hit empty slot */
-      if (hash_prefix[i] == waiting_code && hash_suffix[i] == (UINT8) c) {
-	waiting_code = hash_code[i];
-	return;
-      }
-    }
-  }
-
-  /* here when hashtable[i] is an empty slot; desired symbol not in table */
-  output(waiting_code);
-  if (free_code < LZW_TABLE_SIZE) {
-    hash_code[i] = free_code++;	/* add symbol to hashtable */
-    hash_prefix[i] = waiting_code;
-    hash_suffix[i] = (UINT8) c;
-  } else
-    clear_block();
-  waiting_code = c;
-}
-
-
-LOCAL void
-compress_term (void)
-/* Clean up at end */
-{
-  /* Flush out the buffered code */
-  if (! first_byte)
-    output(waiting_code);
-  /* Send an EOF code */
-  output(EOFCode);
-  /* Flush the bit-packing buffer */
-  if (cur_bits > 0) {
-    CHAR_OUT(cur_accum & 0xFF);
-  }
-  /* Flush the packet buffer */
-  flush_packet();
-}
-
-
-/* GIF header construction */
-
-
-LOCAL void
-put_word (UINT16 w)
-/* Emit a 16-bit word, LSB first */
-{
-  putc(w & 0xFF, dcinfo->output_file);
-  putc((w >> 8) & 0xFF, dcinfo->output_file);
-}
-
-
-LOCAL void
-put_3bytes (int val)
-/* Emit 3 copies of same byte value --- handy subr for colormap construction */
-{
-  putc(val, dcinfo->output_file);
-  putc(val, dcinfo->output_file);
-  putc(val, dcinfo->output_file);
-}
-
-
-LOCAL void
-emit_header (int num_colors, JSAMPARRAY colormap)
-/* Output the GIF file header, including color map */
-/* If colormap==NULL, synthesize a gray-scale colormap */
-{
-  int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte;
-  int cshift = dcinfo->data_precision - 8;
-  int i;
-
-  if (num_colors > 256)
-    ERREXIT(dcinfo->emethods, "GIF can only handle 256 colors");
-  /* Compute bits/pixel and related values */
-  BitsPerPixel = 1;
-  while (num_colors > (1 << BitsPerPixel))
-    BitsPerPixel++;
-  ColorMapSize = 1 << BitsPerPixel;
-  if (BitsPerPixel <= 1)
-    InitCodeSize = 2;
-  else
-    InitCodeSize = BitsPerPixel;
-  /*
-   * Write the GIF header.
-   * Note that we generate a plain GIF87 header for maximum compatibility.
-   */
-  putc('G', dcinfo->output_file);
-  putc('I', dcinfo->output_file);
-  putc('F', dcinfo->output_file);
-  putc('8', dcinfo->output_file);
-  putc('7', dcinfo->output_file);
-  putc('a', dcinfo->output_file);
-  /* Write the Logical Screen Descriptor */
-  put_word((UINT16) dcinfo->image_width);
-  put_word((UINT16) dcinfo->image_height);
-  FlagByte = 0x80;		/* Yes, there is a global color table */
-  FlagByte |= (BitsPerPixel-1) << 4; /* color resolution */
-  FlagByte |= (BitsPerPixel-1);	/* size of global color table */
-  putc(FlagByte, dcinfo->output_file);
-  putc(0, dcinfo->output_file);	/* Background color index */
-  putc(0, dcinfo->output_file);	/* Reserved in GIF87 (aspect ratio in GIF89) */
-  /* Write the Global Color Map */
-  /* If the color map is more than 8 bits precision, */
-  /* we reduce it to 8 bits by shifting */
-  for (i=0; i < ColorMapSize; i++) {
-    if (i < num_colors) {
-      if (colormap != NULL) {
-	if (dcinfo->out_color_space == CS_RGB) {
-	  /* Normal case: RGB color map */
-	  putc(GETJSAMPLE(colormap[0][i]) >> cshift, dcinfo->output_file);
-	  putc(GETJSAMPLE(colormap[1][i]) >> cshift, dcinfo->output_file);
-	  putc(GETJSAMPLE(colormap[2][i]) >> cshift, dcinfo->output_file);
-	} else {
-	  /* Grayscale "color map": possible if quantizing grayscale image */
-	  put_3bytes(GETJSAMPLE(colormap[0][i]) >> cshift);
-	}
-      } else {
-	/* Create a gray-scale map of num_colors values, range 0..255 */
-	put_3bytes((i * 255 + (num_colors-1)/2) / (num_colors-1));
-      }
-    } else {
-      /* fill out the map to a power of 2 */
-      put_3bytes(0);
-    }
-  }
-  /* Write image separator and Image Descriptor */
-  putc(',', dcinfo->output_file); /* separator */
-  put_word((UINT16) 0);		/* left/top offset */
-  put_word((UINT16) 0);
-  put_word((UINT16) dcinfo->image_width); /* image size */
-  put_word((UINT16) dcinfo->image_height);
-  /* flag byte: not interlaced, no local color map */
-  putc(0x00, dcinfo->output_file);
-  /* Write Initial Code Size byte */
-  putc(InitCodeSize, dcinfo->output_file);
-
-  /* Initialize for LZW compression of image data */
-  compress_init(InitCodeSize+1);
-}
-
-
-
-/*
- * Initialize for GIF output.
- */
-
-METHODDEF void
-output_init (decompress_info_ptr cinfo)
-{
-  dcinfo = cinfo;		/* save for use by local routines */
-  if (cinfo->final_out_comps != 1) /* safety check */
-    ERREXIT(cinfo->emethods, "GIF output got confused");
-  /* Allocate space for hash table */
-  hash_code = (code_int FAR *) (*cinfo->emethods->alloc_medium)
-				(HSIZE * SIZEOF(code_int));
-  hash_prefix = (code_int FAR *) (*cinfo->emethods->alloc_medium)
-				(HSIZE * SIZEOF(code_int));
-  hash_suffix = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
-				(HSIZE * SIZEOF(UINT8));
-  /*
-   * If we aren't quantizing, put_color_map won't be called,
-   * so emit the header now.  This only happens with gray scale output.
-   * (If we are quantizing, wait for the color map to be provided.)
-   */
-  if (! cinfo->quantize_colors)
-    emit_header(256, (JSAMPARRAY) NULL);
-}
-
-
-/*
- * Write the color map.
- */
-
-METHODDEF void
-put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
-{
-  emit_header(num_colors, colormap);
-}
-
-
-/*
- * Write some pixel data.
- */
-
-METHODDEF void
-put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
-		JSAMPIMAGE pixel_data)
-{
-  register JSAMPROW ptr;
-  register long col;
-  register long width = cinfo->image_width;
-  register int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr = pixel_data[0][row];
-    for (col = width; col > 0; col--) {
-      compress_byte(GETJSAMPLE(*ptr));
-      ptr++;
-    }
-  }
-}
-
-
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-output_term (decompress_info_ptr cinfo)
-{
-  /* Flush LZW mechanism */
-  compress_term();
-  /* Write a zero-length data block to end the series */
-  putc(0, cinfo->output_file);
-  /* Write the GIF terminator mark */
-  putc(';', cinfo->output_file);
-  /* Make sure we wrote the output file OK */
-  fflush(cinfo->output_file);
-  if (ferror(cinfo->output_file))
-    ERREXIT(cinfo->emethods, "Output file write error --- out of disk space?");
-  /* Free space */
-  /* no work (we let free_all release the workspace) */
-}
-
-
-/*
- * The method selection routine for GIF format output.
- * This should be called from d_ui_method_selection if GIF output is wanted.
- */
-
-GLOBAL void
-jselwgif (decompress_info_ptr cinfo)
-{
-  cinfo->methods->output_init = output_init;
-  cinfo->methods->put_color_map = put_color_map;
-  cinfo->methods->put_pixel_rows = put_pixel_rows;
-  cinfo->methods->output_term = output_term;
-
-  if (cinfo->out_color_space != CS_GRAYSCALE &&
-      cinfo->out_color_space != CS_RGB)
-    ERREXIT(cinfo->emethods, "GIF output must be grayscale or RGB");
-
-  /* Force quantization if color or if > 8 bits input */
-  if (cinfo->out_color_space == CS_RGB || cinfo->data_precision > 8) {
-    /* Force quantization to at most 256 colors */
-    cinfo->quantize_colors = TRUE;
-    if (cinfo->desired_number_of_colors > 256)
-      cinfo->desired_number_of_colors = 256;
-  }
-}
-
-#endif /* GIF_SUPPORTED */
diff --git a/jwrjfif.c b/jwrjfif.c
deleted file mode 100644
index f3daba0..0000000
--- a/jwrjfif.c
+++ /dev/null
@@ -1,500 +0,0 @@
-/*
- * jwrjfif.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to write standard JPEG file headers/markers.
- * The file format created is a raw JPEG data stream with (optionally) an
- * APP0 marker per the JFIF spec.  This will handle baseline and
- * JFIF-convention JPEG files, although there is currently no provision
- * for inserting a thumbnail image in the JFIF header.
- *
- * These routines may need modification for non-Unix environments or
- * specialized applications.  As they stand, they assume output to
- * an ordinary stdio stream.  However, the changes to write to something
- * else are localized in the macros appearing just below.
- *
- * These routines are invoked via the methods write_file_header,
- * write_scan_header, write_jpeg_data, write_scan_trailer, and
- * write_file_trailer.
- */
-
-#include "jinclude.h"
-
-#ifdef JFIF_SUPPORTED
-
-
-/*
- * To output to something other than a stdio stream, you'd need to redefine
- * these macros.
- */
-
-/* Write a single byte */
-#define emit_byte(cinfo,x)  putc((x), cinfo->output_file)
-
-/* Write some bytes from a (char *) buffer */
-#define WRITE_BYTES(cinfo,dataptr,datacount)  \
-  { if (JFWRITE(cinfo->output_file, dataptr, datacount) \
-	!= (size_t) (datacount)) \
-      ERREXIT(cinfo->emethods, "Output file write error --- out of disk space?"); }
-
-/* Clean up and verify successful output */
-#define CHECK_OUTPUT(cinfo)  \
-  { fflush(cinfo->output_file); \
-    if (ferror(cinfo->output_file)) \
-      ERREXIT(cinfo->emethods, "Output file write error --- out of disk space?"); }
-
-
-/* End of stdio-specific code. */
-
-
-typedef enum {			/* JPEG marker codes */
-  M_SOF0  = 0xc0,
-  M_SOF1  = 0xc1,
-  M_SOF2  = 0xc2,
-  M_SOF3  = 0xc3,
-  
-  M_SOF5  = 0xc5,
-  M_SOF6  = 0xc6,
-  M_SOF7  = 0xc7,
-  
-  M_JPG   = 0xc8,
-  M_SOF9  = 0xc9,
-  M_SOF10 = 0xca,
-  M_SOF11 = 0xcb,
-  
-  M_SOF13 = 0xcd,
-  M_SOF14 = 0xce,
-  M_SOF15 = 0xcf,
-  
-  M_DHT   = 0xc4,
-  
-  M_DAC   = 0xcc,
-  
-  M_RST0  = 0xd0,
-  M_RST1  = 0xd1,
-  M_RST2  = 0xd2,
-  M_RST3  = 0xd3,
-  M_RST4  = 0xd4,
-  M_RST5  = 0xd5,
-  M_RST6  = 0xd6,
-  M_RST7  = 0xd7,
-  
-  M_SOI   = 0xd8,
-  M_EOI   = 0xd9,
-  M_SOS   = 0xda,
-  M_DQT   = 0xdb,
-  M_DNL   = 0xdc,
-  M_DRI   = 0xdd,
-  M_DHP   = 0xde,
-  M_EXP   = 0xdf,
-  
-  M_APP0  = 0xe0,
-  M_APP15 = 0xef,
-  
-  M_JPG0  = 0xf0,
-  M_JPG13 = 0xfd,
-  M_COM   = 0xfe,
-  
-  M_TEM   = 0x01,
-  
-  M_ERROR = 0x100
-} JPEG_MARKER;
-
-
-LOCAL void
-emit_marker (compress_info_ptr cinfo, JPEG_MARKER mark)
-/* Emit a marker code */
-{
-  emit_byte(cinfo, 0xFF);
-  emit_byte(cinfo, mark);
-}
-
-
-LOCAL void
-emit_2bytes (compress_info_ptr cinfo, int value)
-/* Emit a 2-byte integer; these are always MSB first in JPEG files */
-{
-  emit_byte(cinfo, (value >> 8) & 0xFF);
-  emit_byte(cinfo, value & 0xFF);
-}
-
-
-LOCAL int
-emit_dqt (compress_info_ptr cinfo, int index)
-/* Emit a DQT marker */
-/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
-{
-  QUANT_TBL_PTR data = cinfo->quant_tbl_ptrs[index];
-  int prec = 0;
-  int i;
-  
-  for (i = 0; i < DCTSIZE2; i++) {
-    if (data[i] > 255)
-      prec = 1;
-  }
-
-  emit_marker(cinfo, M_DQT);
-  
-  emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
-  
-  emit_byte(cinfo, index + (prec<<4));
-  
-  for (i = 0; i < DCTSIZE2; i++) {
-    if (prec)
-      emit_byte(cinfo, data[i] >> 8);
-    emit_byte(cinfo, data[i] & 0xFF);
-  }
-
-  return prec;
-}
-
-
-LOCAL void
-emit_dht (compress_info_ptr cinfo, int index, boolean is_ac)
-/* Emit a DHT marker */
-{
-  HUFF_TBL * htbl;
-  int length, i;
-  
-  if (is_ac) {
-    htbl = cinfo->ac_huff_tbl_ptrs[index];
-    index += 0x10;		/* output index has AC bit set */
-  } else {
-    htbl = cinfo->dc_huff_tbl_ptrs[index];
-  }
-
-  if (htbl == NULL)
-    ERREXIT1(cinfo->emethods, "Huffman table 0x%02x was not defined", index);
-  
-  if (! htbl->sent_table) {
-    emit_marker(cinfo, M_DHT);
-    
-    length = 0;
-    for (i = 1; i <= 16; i++)
-      length += htbl->bits[i];
-    
-    emit_2bytes(cinfo, length + 2 + 1 + 16);
-    emit_byte(cinfo, index);
-    
-    for (i = 1; i <= 16; i++)
-      emit_byte(cinfo, htbl->bits[i]);
-    
-    for (i = 0; i < length; i++)
-      emit_byte(cinfo, htbl->huffval[i]);
-    
-    htbl->sent_table = TRUE;
-  }
-}
-
-
-LOCAL void
-emit_dac (compress_info_ptr cinfo)
-/* Emit a DAC marker */
-/* Since the useful info is so small, we want to emit all the tables in */
-/* one DAC marker.  Therefore this routine does its own scan of the table. */
-{
-  char dc_in_use[NUM_ARITH_TBLS];
-  char ac_in_use[NUM_ARITH_TBLS];
-  int length, i;
-  
-  for (i = 0; i < NUM_ARITH_TBLS; i++)
-    dc_in_use[i] = ac_in_use[i] = 0;
-  
-  for (i = 0; i < cinfo->num_components; i++) {
-    dc_in_use[cinfo->comp_info[i].dc_tbl_no] = 1;
-    ac_in_use[cinfo->comp_info[i].ac_tbl_no] = 1;
-  }
-  
-  length = 0;
-  for (i = 0; i < NUM_ARITH_TBLS; i++)
-    length += dc_in_use[i] + ac_in_use[i];
-  
-  emit_marker(cinfo, M_DAC);
-  
-  emit_2bytes(cinfo, length*2 + 2);
-  
-  for (i = 0; i < NUM_ARITH_TBLS; i++) {
-    if (dc_in_use[i]) {
-      emit_byte(cinfo, i);
-      emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
-    }
-    if (ac_in_use[i]) {
-      emit_byte(cinfo, i + 0x10);
-      emit_byte(cinfo, cinfo->arith_ac_K[i]);
-    }
-  }
-}
-
-
-LOCAL void
-emit_dri (compress_info_ptr cinfo)
-/* Emit a DRI marker */
-{
-  emit_marker(cinfo, M_DRI);
-  
-  emit_2bytes(cinfo, 4);	/* fixed length */
-
-  emit_2bytes(cinfo, (int) cinfo->restart_interval);
-}
-
-
-LOCAL void
-emit_sof (compress_info_ptr cinfo, JPEG_MARKER code)
-/* Emit a SOF marker */
-{
-  int i;
-  
-  emit_marker(cinfo, code);
-  
-  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
-
-  if (cinfo->image_height > 65535L || cinfo->image_width > 65535L)
-    ERREXIT(cinfo->emethods, "Maximum image dimension for JFIF is 65535 pixels");
-
-  emit_byte(cinfo, cinfo->data_precision);
-  emit_2bytes(cinfo, (int) cinfo->image_height);
-  emit_2bytes(cinfo, (int) cinfo->image_width);
-
-  emit_byte(cinfo, cinfo->num_components);
-
-  for (i = 0; i < cinfo->num_components; i++) {
-    emit_byte(cinfo, cinfo->comp_info[i].component_id);
-    emit_byte(cinfo, (cinfo->comp_info[i].h_samp_factor << 4)
-		     + cinfo->comp_info[i].v_samp_factor);
-    emit_byte(cinfo, cinfo->comp_info[i].quant_tbl_no);
-  }
-}
-
-
-LOCAL void
-emit_sos (compress_info_ptr cinfo)
-/* Emit a SOS marker */
-{
-  int i;
-  
-  emit_marker(cinfo, M_SOS);
-  
-  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
-  
-  emit_byte(cinfo, cinfo->comps_in_scan);
-  
-  for (i = 0; i < cinfo->comps_in_scan; i++) {
-    emit_byte(cinfo, cinfo->cur_comp_info[i]->component_id);
-    emit_byte(cinfo, (cinfo->cur_comp_info[i]->dc_tbl_no << 4)
-		     + cinfo->cur_comp_info[i]->ac_tbl_no);
-  }
-
-  emit_byte(cinfo, 0);		/* Spectral selection start */
-  emit_byte(cinfo, DCTSIZE2-1);	/* Spectral selection end */
-  emit_byte(cinfo, 0);		/* Successive approximation */
-}
-
-
-LOCAL void
-emit_jfif_app0 (compress_info_ptr cinfo)
-/* Emit a JFIF-compliant APP0 marker */
-{
-  /*
-   * Length of APP0 block	(2 bytes)
-   * Block ID			(4 bytes - ASCII "JFIF")
-   * Zero byte			(1 byte to terminate the ID string)
-   * Version Major, Minor	(2 bytes - 0x01, 0x01)
-   * Units			(1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
-   * Xdpu			(2 bytes - dots per unit horizontal)
-   * Ydpu			(2 bytes - dots per unit vertical)
-   * Thumbnail X size		(1 byte)
-   * Thumbnail Y size		(1 byte)
-   */
-  
-  emit_marker(cinfo, M_APP0);
-  
-  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
-
-  emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
-  emit_byte(cinfo, 0x46);
-  emit_byte(cinfo, 0x49);
-  emit_byte(cinfo, 0x46);
-  emit_byte(cinfo, 0);
-  emit_byte(cinfo, 1);		/* Major version */
-  emit_byte(cinfo, 1);		/* Minor version */
-  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
-  emit_2bytes(cinfo, (int) cinfo->X_density);
-  emit_2bytes(cinfo, (int) cinfo->Y_density);
-  emit_byte(cinfo, 0);		/* No thumbnail image */
-  emit_byte(cinfo, 0);
-}
-
-
-LOCAL void
-emit_com (compress_info_ptr cinfo, char * dataptr, size_t datalen)
-/* Emit a COM marker */
-{
-  if ((unsigned) datalen <= (unsigned) 65533) {	/* safety check */
-    emit_marker(cinfo, M_COM);
-  
-    emit_2bytes(cinfo, (int) (datalen + 2)); /* length */
-
-    while (datalen--) {
-      emit_byte(cinfo, *dataptr);
-      dataptr++;
-    }
-  }
-}
-
-
-/*
- * Write the file header.
- */
-
-
-METHODDEF void
-write_file_header (compress_info_ptr cinfo)
-{
-  char qt_in_use[NUM_QUANT_TBLS];
-  int i, prec;
-  boolean is_baseline;
-  
-  emit_marker(cinfo, M_SOI);	/* first the SOI */
-
-  if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
-    emit_jfif_app0(cinfo);
-
-  if (cinfo->comment_text != NULL) /* and an optional COM block */
-    emit_com(cinfo, cinfo->comment_text,
-	     (size_t) (strlen(cinfo->comment_text)));
-
-  /* Emit DQT for each quantization table. */
-  /* Note that doing it here means we can't adjust the QTs on-the-fly. */
-  /* If we did want to do that, we'd have a problem with checking precision */
-  /* for the is_baseline determination. */
-
-  for (i = 0; i < NUM_QUANT_TBLS; i++)
-    qt_in_use[i] = 0;
-
-  for (i = 0; i < cinfo->num_components; i++)
-    qt_in_use[cinfo->comp_info[i].quant_tbl_no] = 1;
-
-  prec = 0;
-  for (i = 0; i < NUM_QUANT_TBLS; i++) {
-    if (qt_in_use[i])
-      prec += emit_dqt(cinfo, i);
-  }
-  /* now prec is nonzero iff there are any 16-bit quant tables. */
-
-  /* Check for a non-baseline specification. */
-  /* Note we assume that Huffman table numbers won't be changed later. */
-  is_baseline = TRUE;
-  if (cinfo->arith_code || (cinfo->data_precision != 8))
-    is_baseline = FALSE;
-  for (i = 0; i < cinfo->num_components; i++) {
-    if (cinfo->comp_info[i].dc_tbl_no > 1 || cinfo->comp_info[i].ac_tbl_no > 1)
-      is_baseline = FALSE;
-  }
-  if (prec && is_baseline) {
-    is_baseline = FALSE;
-    /* If it's baseline except for quantizer size, warn the user */
-    TRACEMS(cinfo->emethods, 0,
-	    "Caution: quantization tables are too coarse for baseline JPEG");
-  }
-
-
-  /* Emit the proper SOF marker */
-  if (cinfo->arith_code)
-    emit_sof(cinfo, M_SOF9);	/* SOF code for arithmetic coding */
-  else if (is_baseline)
-    emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
-  else
-    emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
-}
-
-
-/*
- * Write the start of a scan (everything through the SOS marker).
- */
-
-METHODDEF void
-write_scan_header (compress_info_ptr cinfo)
-{
-  int i;
-
-  if (cinfo->arith_code) {
-    /* Emit arith conditioning info.  We will have some duplication
-     * if the file has multiple scans, but it's so small it's hardly
-     * worth worrying about.
-     */
-    emit_dac(cinfo);
-  } else {
-    /* Emit Huffman tables.  Note that emit_dht takes care of
-     * suppressing duplicate tables.
-     */
-    for (i = 0; i < cinfo->comps_in_scan; i++) {
-      emit_dht(cinfo, cinfo->cur_comp_info[i]->dc_tbl_no, FALSE);
-      emit_dht(cinfo, cinfo->cur_comp_info[i]->ac_tbl_no, TRUE);
-    }
-  }
-
-  /* Emit DRI if required --- note that DRI value could change for each scan.
-   * If it doesn't, a tiny amount of space is wasted in multiple-scan files.
-   * We assume DRI will never be nonzero for one scan and zero for a later one.
-   */
-  if (cinfo->restart_interval)
-    emit_dri(cinfo);
-
-  emit_sos(cinfo);
-}
-
-
-/*
- * Write some bytes of compressed data within a scan.
- */
-
-METHODDEF void
-write_jpeg_data (compress_info_ptr cinfo, char *dataptr, int datacount)
-{
-  WRITE_BYTES(cinfo, dataptr, datacount);
-}
-
-
-/*
- * Finish up after a compressed scan (series of write_jpeg_data calls).
- */
-
-METHODDEF void
-write_scan_trailer (compress_info_ptr cinfo)
-{
-  /* no work needed in this format */
-}
-
-
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-write_file_trailer (compress_info_ptr cinfo)
-{
-  emit_marker(cinfo, M_EOI);
-  /* Make sure we wrote the output file OK */
-  CHECK_OUTPUT(cinfo);
-}
-
-
-/*
- * The method selection routine for standard JPEG header writing.
- * This should be called from c_ui_method_selection if appropriate.
- */
-
-GLOBAL void
-jselwjfif (compress_info_ptr cinfo)
-{
-  cinfo->methods->write_file_header = write_file_header;
-  cinfo->methods->write_scan_header = write_scan_header;
-  cinfo->methods->write_jpeg_data = write_jpeg_data;
-  cinfo->methods->write_scan_trailer = write_scan_trailer;
-  cinfo->methods->write_file_trailer = write_file_trailer;
-}
-
-#endif /* JFIF_SUPPORTED */
diff --git a/jwrppm.c b/jwrppm.c
deleted file mode 100644
index a6d10e3..0000000
--- a/jwrppm.c
+++ /dev/null
@@ -1,337 +0,0 @@
-/*
- * jwrppm.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to write output images in PPM/PGM format.
- *
- * These routines may need modification for non-Unix environments or
- * specialized applications.  As they stand, they assume output to
- * an ordinary stdio stream.
- *
- * These routines are invoked via the methods put_pixel_rows, put_color_map,
- * and output_init/term.
- */
-
-#include "jinclude.h"
-
-#ifdef PPM_SUPPORTED
-
-
-/*
- * Haven't yet got around to making this work with text-format output,
- * hence cannot handle pixels wider than 8 bits.
- */
-
-#ifndef EIGHT_BIT_SAMPLES
-  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
-#endif
-
-
-/*
- * On most systems, writing individual bytes with putc() is drastically less
- * efficient than buffering a row at a time for fwrite().  But we must
- * allocate the row buffer in near data space on PCs, because we are assuming
- * small-data memory model, wherein fwrite() can't reach far memory.  If you
- * need to process very wide images on a PC, you may have to use the putc()
- * approach.  Also, there are still a few systems around wherein fwrite() is
- * actually implemented as a putc() loop, in which case this buffer is a waste
- * of space.  So the putc() method can be used by defining USE_PUTC_OUTPUT.
- */
-
-#ifndef USE_PUTC_OUTPUT
-static char * row_buffer;	/* holds 1 pixel row's worth of output */
-#endif
-
-
-/*
- * Write the file header.
- */
-
-METHODDEF void
-output_init (decompress_info_ptr cinfo)
-{
-  if (cinfo->out_color_space == CS_GRAYSCALE) {
-    /* emit header for raw PGM format */
-    fprintf(cinfo->output_file, "P5\n%ld %ld\n%d\n",
-	    cinfo->image_width, cinfo->image_height, 255);
-#ifndef USE_PUTC_OUTPUT
-    /* allocate space for row buffer: 1 byte/pixel */
-    row_buffer = (char *) (*cinfo->emethods->alloc_small)
-			((size_t) (SIZEOF(char) * cinfo->image_width));
-#endif
-  } else if (cinfo->out_color_space == CS_RGB) {
-    /* emit header for raw PPM format */
-    fprintf(cinfo->output_file, "P6\n%ld %ld\n%d\n",
-	    cinfo->image_width, cinfo->image_height, 255);
-#ifndef USE_PUTC_OUTPUT
-    /* allocate space for row buffer: 3 bytes/pixel */
-    row_buffer = (char *) (*cinfo->emethods->alloc_small)
-			((size_t) (3 * SIZEOF(char) * cinfo->image_width));
-#endif
-  } else {
-    ERREXIT(cinfo->emethods, "PPM output must be grayscale or RGB");
-  }
-}
-
-
-/*
- * Write some pixel data.
- */
-
-#ifdef USE_PUTC_OUTPUT
-
-METHODDEF void
-put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
-		JSAMPIMAGE pixel_data)
-{
-  register FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr0 = pixel_data[0][row];
-    ptr1 = pixel_data[1][row];
-    ptr2 = pixel_data[2][row];
-    for (col = width; col > 0; col--) {
-      putc(GETJSAMPLE(*ptr0), outfile);
-      ptr0++;
-      putc(GETJSAMPLE(*ptr1), outfile);
-      ptr1++;
-      putc(GETJSAMPLE(*ptr2), outfile);
-      ptr2++;
-    }
-  }
-}
-
-METHODDEF void
-put_gray_rows (decompress_info_ptr cinfo, int num_rows,
-	       JSAMPIMAGE pixel_data)
-{
-  register FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr0;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr0 = pixel_data[0][row];
-    for (col = width; col > 0; col--) {
-      putc(GETJSAMPLE(*ptr0), outfile);
-      ptr0++;
-    }
-  }
-}
-
-#else /* use row buffering */
-
-METHODDEF void
-put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
-		JSAMPIMAGE pixel_data)
-{
-  FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register char * row_bufferptr;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr0 = pixel_data[0][row];
-    ptr1 = pixel_data[1][row];
-    ptr2 = pixel_data[2][row];
-    row_bufferptr = row_buffer;
-    for (col = width; col > 0; col--) {
-      *row_bufferptr++ = (char) GETJSAMPLE(*ptr0++);
-      *row_bufferptr++ = (char) GETJSAMPLE(*ptr1++);
-      *row_bufferptr++ = (char) GETJSAMPLE(*ptr2++);
-    }
-    (void) JFWRITE(outfile, row_buffer, 3*width);
-  }
-}
-
-METHODDEF void
-put_gray_rows (decompress_info_ptr cinfo, int num_rows,
-	       JSAMPIMAGE pixel_data)
-{
-  FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr0;
-  register char * row_bufferptr;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr0 = pixel_data[0][row];
-    row_bufferptr = row_buffer;
-    for (col = width; col > 0; col--) {
-      *row_bufferptr++ = (char) GETJSAMPLE(*ptr0++);
-    }
-    (void) JFWRITE(outfile, row_buffer, width);
-  }
-}
-
-#endif /* USE_PUTC_OUTPUT */
-
-
-/*
- * Write some pixel data when color quantization is in effect.
- */
-
-#ifdef USE_PUTC_OUTPUT
-
-METHODDEF void
-put_demapped_rgb (decompress_info_ptr cinfo, int num_rows,
-		  JSAMPIMAGE pixel_data)
-{
-  register FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr;
-  register JSAMPROW color_map0 = cinfo->colormap[0];
-  register JSAMPROW color_map1 = cinfo->colormap[1];
-  register JSAMPROW color_map2 = cinfo->colormap[2];
-  register int pixval;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr = pixel_data[0][row];
-    for (col = width; col > 0; col--) {
-      pixval = GETJSAMPLE(*ptr++);
-      putc(GETJSAMPLE(color_map0[pixval]), outfile);
-      putc(GETJSAMPLE(color_map1[pixval]), outfile);
-      putc(GETJSAMPLE(color_map2[pixval]), outfile);
-    }
-  }
-}
-
-METHODDEF void
-put_demapped_gray (decompress_info_ptr cinfo, int num_rows,
-		   JSAMPIMAGE pixel_data)
-{
-  register FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr;
-  register JSAMPROW color_map0 = cinfo->colormap[0];
-  register int pixval;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr = pixel_data[0][row];
-    for (col = width; col > 0; col--) {
-      pixval = GETJSAMPLE(*ptr++);
-      putc(GETJSAMPLE(color_map0[pixval]), outfile);
-    }
-  }
-}
-
-#else /* use row buffering */
-
-METHODDEF void
-put_demapped_rgb (decompress_info_ptr cinfo, int num_rows,
-		  JSAMPIMAGE pixel_data)
-{
-  FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr;
-  register char * row_bufferptr;
-  register JSAMPROW color_map0 = cinfo->colormap[0];
-  register JSAMPROW color_map1 = cinfo->colormap[1];
-  register JSAMPROW color_map2 = cinfo->colormap[2];
-  register int pixval;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr = pixel_data[0][row];
-    row_bufferptr = row_buffer;
-    for (col = width; col > 0; col--) {
-      pixval = GETJSAMPLE(*ptr++);
-      *row_bufferptr++ = (char) GETJSAMPLE(color_map0[pixval]);
-      *row_bufferptr++ = (char) GETJSAMPLE(color_map1[pixval]);
-      *row_bufferptr++ = (char) GETJSAMPLE(color_map2[pixval]);
-    }
-    (void) JFWRITE(outfile, row_buffer, 3*width);
-  }
-}
-
-METHODDEF void
-put_demapped_gray (decompress_info_ptr cinfo, int num_rows,
-		   JSAMPIMAGE pixel_data)
-{
-  FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr;
-  register char * row_bufferptr;
-  register JSAMPROW color_map0 = cinfo->colormap[0];
-  register int pixval;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr = pixel_data[0][row];
-    row_bufferptr = row_buffer;
-    for (col = width; col > 0; col--) {
-      pixval = GETJSAMPLE(*ptr++);
-      *row_bufferptr++ = (char) GETJSAMPLE(color_map0[pixval]);
-    }
-    (void) JFWRITE(outfile, row_buffer, width);
-  }
-}
-
-#endif /* USE_PUTC_OUTPUT */
-
-
-/*
- * Write the color map.
- * For PPM output, we just remember to demap the output data!
- */
-
-METHODDEF void
-put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
-{
-  if (cinfo->out_color_space == CS_RGB)
-    cinfo->methods->put_pixel_rows = put_demapped_rgb;
-  else
-    cinfo->methods->put_pixel_rows = put_demapped_gray;
-}
-
-
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-output_term (decompress_info_ptr cinfo)
-{
-  /* No work except to make sure we wrote the output file OK; */
-  /* we let free_all release any workspace */
-  fflush(cinfo->output_file);
-  if (ferror(cinfo->output_file))
-    ERREXIT(cinfo->emethods, "Output file write error --- out of disk space?");
-}
-
-
-/*
- * The method selection routine for PPM format output.
- * This should be called from d_ui_method_selection if PPM output is wanted.
- */
-
-GLOBAL void
-jselwppm (decompress_info_ptr cinfo)
-{
-  cinfo->methods->output_init = output_init;
-  cinfo->methods->put_color_map = put_color_map;
-  if (cinfo->out_color_space == CS_RGB)
-    cinfo->methods->put_pixel_rows = put_pixel_rows;
-  else
-    cinfo->methods->put_pixel_rows = put_gray_rows;
-  cinfo->methods->output_term = output_term;
-}
-
-#endif /* PPM_SUPPORTED */
diff --git a/jwrrle.c b/jwrrle.c
deleted file mode 100644
index 0c8718e..0000000
--- a/jwrrle.c
+++ /dev/null
@@ -1,232 +0,0 @@
-/*
- * jwrrle.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to write output images in RLE format.
- * The Utah Raster Toolkit library is required (version 3.0).
- *
- * These routines may need modification for non-Unix environments or
- * specialized applications.  As they stand, they assume output to
- * an ordinary stdio stream.
- *
- * These routines are invoked via the methods put_pixel_rows, put_color_map,
- * and output_init/term.
- *
- * Based on code contributed by Mike Lijewski.
- */
-
-#include "jinclude.h"
-
-#ifdef RLE_SUPPORTED
-
-/* rle.h is provided by the Utah Raster Toolkit. */
-
-#include <rle.h>
-
-
-/*
- * output_term assumes that JSAMPLE has the same representation as rle_pixel,
- * to wit, "unsigned char".  Hence we can't cope with 12- or 16-bit samples.
- */
-
-#ifndef EIGHT_BIT_SAMPLES
-  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
-#endif
-
-
-/*
- * Since RLE stores scanlines bottom-to-top, we have to invert the image
- * from JPEG's top-to-bottom order.  To do this, we save the outgoing data
- * in virtual array(s) during put_pixel_row calls, then actually emit the
- * RLE file during output_term.  We use one virtual array if the output is
- * grayscale or colormapped, more if it is full color.
- */
-
-#define MAX_CHANS	4	/* allow up to four color components */
-static big_sarray_ptr channels[MAX_CHANS]; /* Virtual arrays for saved data */
-
-static long cur_output_row;	/* next row# to write to virtual array(s) */
-
-
-/*
- * For now, if we emit an RLE color map then it is always 256 entries long,
- * though not all of the entries need be used.
- */
-
-#define CMAPBITS	8
-#define CMAPLENGTH	(1<<(CMAPBITS))
-
-static rle_map *output_colormap; /* RLE-style color map, or NULL if none */
-static int number_colors;	/* Number of colors actually used */
-
-
-/*
- * Write the file header.
- *
- * In this module it's easier to wait till output_term to actually write
- * anything; here we just request the big arrays we'll need.
- */
-
-METHODDEF void
-output_init (decompress_info_ptr cinfo)
-{
-  short ci;
-  
-  if (cinfo->final_out_comps > MAX_CHANS)
-    ERREXIT1(cinfo->emethods, "Cannot handle %d output channels for RLE",
-	     cinfo->final_out_comps);
-  
-  for (ci = 0; ci < cinfo->final_out_comps; ci++) {
-    channels[ci] = (*cinfo->emethods->request_big_sarray)
-			(cinfo->image_width, cinfo->image_height, 1L);
-  }
-  
-  output_colormap = NULL;	/* No output colormap as yet */
-  number_colors = 0;
-  cur_output_row = 0;		/* Start filling virtual arrays at row 0 */
-
-  cinfo->total_passes++;	/* count file writing as separate pass */
-}
-
-
-/*
- * Write some pixel data.
- *
- * This routine just saves the data away in virtual arrays.
- */
-
-METHODDEF void
-put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
-		JSAMPIMAGE pixel_data)
-{
-  JSAMPROW outputrow[1];	/* a pseudo JSAMPARRAY structure */
-  int row;
-  short ci;
-  
-  for (row = 0; row < num_rows; row++) {
-    for (ci = 0; ci < cinfo->final_out_comps; ci++) {
-      outputrow[0] = *((*cinfo->emethods->access_big_sarray)
-			(channels[ci], cur_output_row, TRUE));
-      jcopy_sample_rows(pixel_data[ci], row, outputrow, 0,
-			1, cinfo->image_width);
-    }
-    cur_output_row++;
-  }
-}
-
-
-/*
- * Write the color map.
- *
- *  For RLE output we just save the colormap for the output stage.
- */
-
-METHODDEF void
-put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
-{
-  size_t cmapsize;
-  short ci;
-  int i;
-
-  if (num_colors > CMAPLENGTH)
-    ERREXIT1(cinfo->emethods, "Cannot handle %d colormap entries for RLE",
-	     num_colors);
-
-  /* Allocate storage for RLE-style cmap, zero any extra entries */
-  cmapsize = cinfo->color_out_comps * CMAPLENGTH * SIZEOF(rle_map);
-  output_colormap = (rle_map *) (*cinfo->emethods->alloc_small) (cmapsize);
-  MEMZERO(output_colormap, cmapsize);
-
-  /* Save away data in RLE format --- note 8-bit left shift! */
-  /* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */
-  for (ci = 0; ci < cinfo->color_out_comps; ci++) {
-    for (i = 0; i < num_colors; i++) {
-      output_colormap[ci * CMAPLENGTH + i] = GETJSAMPLE(colormap[ci][i]) << 8;
-    }
-  }
-  number_colors = num_colors;
-}
-
-
-/*
- * Finish up at the end of the file.
- *
- * Here is where we really output the RLE file.
- */
-
-METHODDEF void
-output_term (decompress_info_ptr cinfo)
-{
-  rle_hdr header;		/* Output file information */
-  rle_pixel *output_rows[MAX_CHANS];
-  char cmapcomment[80];
-  short ci;
-  long row;
-
-  /* Initialize the header info */
-  MEMZERO(&header, SIZEOF(rle_hdr)); /* make sure all bits are 0 */
-  header.rle_file = cinfo->output_file;
-  header.xmin     = 0;
-  header.xmax     = cinfo->image_width  - 1;
-  header.ymin     = 0;
-  header.ymax     = cinfo->image_height - 1;
-  header.alpha    = 0;
-  header.ncolors  = cinfo->final_out_comps;
-  for (ci = 0; ci < cinfo->final_out_comps; ci++) {
-    RLE_SET_BIT(header, ci);
-  }
-  if (number_colors > 0) {
-    header.ncmap   = cinfo->color_out_comps;
-    header.cmaplen = CMAPBITS;
-    header.cmap    = output_colormap;
-    /* Add a comment to the output image with the true colormap length. */
-    sprintf(cmapcomment, "color_map_length=%d", number_colors);
-    rle_putcom(cmapcomment, &header);
-  }
-  /* Emit the RLE header and color map (if any) */
-  rle_put_setup(&header);
-
-  /* Now output the RLE data from our virtual array(s).
-   * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
-   * and (b) we are not on a machine where FAR pointers differ from regular.
-   */
-  for (row = cinfo->image_height-1; row >= 0; row--) {
-    (*cinfo->methods->progress_monitor) (cinfo, cinfo->image_height-row-1,
-					 cinfo->image_height);
-    for (ci = 0; ci < cinfo->final_out_comps; ci++) {
-      output_rows[ci] = (rle_pixel *) *((*cinfo->emethods->access_big_sarray)
-					(channels[ci], row, FALSE));
-    }
-    rle_putrow(output_rows, (int) cinfo->image_width, &header);
-  }
-  cinfo->completed_passes++;
-
-  /* Emit file trailer */
-  rle_puteof(&header);
-  fflush(cinfo->output_file);
-  if (ferror(cinfo->output_file))
-    ERREXIT(cinfo->emethods, "Output file write error --- out of disk space?");
-
-  /* Release memory */
-  /* no work (we let free_all release the workspace) */
-}
-
-
-/*
- * The method selection routine for RLE format output.
- * This should be called from d_ui_method_selection if RLE output is wanted.
- */
-
-GLOBAL void
-jselwrle (decompress_info_ptr cinfo)
-{
-  cinfo->methods->output_init    = output_init;
-  cinfo->methods->put_color_map  = put_color_map;
-  cinfo->methods->put_pixel_rows = put_pixel_rows;
-  cinfo->methods->output_term    = output_term;
-}
-
-#endif /* RLE_SUPPORTED */
diff --git a/jwrtarga.c b/jwrtarga.c
deleted file mode 100644
index 4c952cb..0000000
--- a/jwrtarga.c
+++ /dev/null
@@ -1,343 +0,0 @@
-/*
- * jwrtarga.c
- *
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
- * This file is part of the Independent JPEG Group's software.
- * For conditions of distribution and use, see the accompanying README file.
- *
- * This file contains routines to write output images in Targa format.
- *
- * These routines may need modification for non-Unix environments or
- * specialized applications.  As they stand, they assume output to
- * an ordinary stdio stream.
- *
- * These routines are invoked via the methods put_pixel_rows, put_color_map,
- * and output_init/term.
- *
- * Based on code contributed by Lee Daniel Crocker.
- */
-
-#include "jinclude.h"
-
-#ifdef TARGA_SUPPORTED
-
-
-/*
- * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
- * This is not yet implemented.
- */
-
-#ifndef EIGHT_BIT_SAMPLES
-  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
-#endif
-
-
-/*
- * On most systems, writing individual bytes with putc() is drastically less
- * efficient than buffering a row at a time for fwrite().  But we must
- * allocate the row buffer in near data space on PCs, because we are assuming
- * small-data memory model, wherein fwrite() can't reach far memory.  If you
- * need to process very wide images on a PC, you may have to use the putc()
- * approach.  Also, there are still a few systems around wherein fwrite() is
- * actually implemented as a putc() loop, in which case this buffer is a waste
- * of space.  So the putc() method can be used by defining USE_PUTC_OUTPUT.
- */
-
-#ifndef USE_PUTC_OUTPUT
-static char * row_buffer;	/* holds 1 pixel row's worth of output */
-#endif
-
-
-LOCAL void
-write_header (decompress_info_ptr cinfo, int num_colors)
-/* Create and write a Targa header */
-{
-  char targaheader[18];
-
-  /* Set unused fields of header to 0 */
-  MEMZERO(targaheader, SIZEOF(targaheader));
-
-  if (num_colors > 0) {
-    targaheader[1] = 1;		/* color map type 1 */
-    targaheader[5] = (char) (num_colors & 0xFF);
-    targaheader[6] = (char) (num_colors >> 8);
-    targaheader[7] = 24;	/* 24 bits per cmap entry */
-  }
-
-  targaheader[12] = (char) (cinfo->image_width & 0xFF);
-  targaheader[13] = (char) (cinfo->image_width >> 8);
-  targaheader[14] = (char) (cinfo->image_height & 0xFF);
-  targaheader[15] = (char) (cinfo->image_height >> 8);
-  targaheader[17] = 0x20;	/* Top-down, non-interlaced */
-
-  if (cinfo->out_color_space == CS_GRAYSCALE) {
-    targaheader[2] = 3;		/* image type = uncompressed gray-scale */
-    targaheader[16] = 8;	/* bits per pixel */
-  } else {			/* must be RGB */
-    if (num_colors > 0) {
-      targaheader[2] = 1;	/* image type = colormapped RGB */
-      targaheader[16] = 8;
-    } else {
-      targaheader[2] = 2;	/* image type = uncompressed RGB */
-      targaheader[16] = 24;
-    }
-  }
-
-  if (JFWRITE(cinfo->output_file, targaheader, 18) != (size_t) 18)
-    ERREXIT(cinfo->emethods, "Could not write Targa header");
-}
-
-
-/*
- * Write the file header.
- */
-
-METHODDEF void
-output_init (decompress_info_ptr cinfo)
-{
-  if (cinfo->out_color_space == CS_GRAYSCALE) {
-    /* Targa doesn't have a mapped grayscale format, so we will */
-    /* demap quantized gray output.  Never emit a colormap. */
-    write_header(cinfo, 0);
-#ifndef USE_PUTC_OUTPUT
-    /* allocate space for row buffer: 1 byte/pixel */
-    row_buffer = (char *) (*cinfo->emethods->alloc_small)
-			((size_t) (SIZEOF(char) * cinfo->image_width));
-#endif
-  } else if (cinfo->out_color_space == CS_RGB) {
-    /* For quantized output, defer writing header until put_color_map time. */
-    if (! cinfo->quantize_colors)
-      write_header(cinfo, 0);
-#ifndef USE_PUTC_OUTPUT
-    /* allocate space for row buffer: 3 bytes/pixel */
-    row_buffer = (char *) (*cinfo->emethods->alloc_small)
-			((size_t) (3 * SIZEOF(char) * cinfo->image_width));
-#endif
-  } else {
-    ERREXIT(cinfo->emethods, "Targa output must be grayscale or RGB");
-  }
-}
-
-
-/*
- * Write some pixel data.
- */
-
-#ifdef USE_PUTC_OUTPUT
-
-METHODDEF void
-put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
-		JSAMPIMAGE pixel_data)
-/* used for unquantized full-color output */
-{
-  register FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr0 = pixel_data[0][row];
-    ptr1 = pixel_data[1][row];
-    ptr2 = pixel_data[2][row];
-    for (col = width; col > 0; col--) {
-      putc(GETJSAMPLE(*ptr2), outfile); /* write in BGR order */
-      ptr2++;
-      putc(GETJSAMPLE(*ptr1), outfile);
-      ptr1++;
-      putc(GETJSAMPLE(*ptr0), outfile);
-      ptr0++;
-    }
-  }
-}
-
-METHODDEF void
-put_gray_rows (decompress_info_ptr cinfo, int num_rows,
-	       JSAMPIMAGE pixel_data)
-/* used for grayscale OR quantized color output */
-{
-  register FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr0;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr0 = pixel_data[0][row];
-    for (col = width; col > 0; col--) {
-      putc(GETJSAMPLE(*ptr0), outfile);
-      ptr0++;
-    }
-  }
-}
-
-#else /* use row buffering */
-
-METHODDEF void
-put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
-		JSAMPIMAGE pixel_data)
-/* used for unquantized full-color output */
-{
-  FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr0, ptr1, ptr2;
-  register char * row_bufferptr;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr0 = pixel_data[0][row];
-    ptr1 = pixel_data[1][row];
-    ptr2 = pixel_data[2][row];
-    row_bufferptr = row_buffer;
-    for (col = width; col > 0; col--) {
-      *row_bufferptr++ = (char) GETJSAMPLE(*ptr2++); /* BGR order */
-      *row_bufferptr++ = (char) GETJSAMPLE(*ptr1++);
-      *row_bufferptr++ = (char) GETJSAMPLE(*ptr0++);
-    }
-    (void) JFWRITE(outfile, row_buffer, 3*width);
-  }
-}
-
-METHODDEF void
-put_gray_rows (decompress_info_ptr cinfo, int num_rows,
-	       JSAMPIMAGE pixel_data)
-/* used for grayscale OR quantized color output */
-{
-  FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr0;
-  register char * row_bufferptr;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr0 = pixel_data[0][row];
-    row_bufferptr = row_buffer;
-    for (col = width; col > 0; col--) {
-      *row_bufferptr++ = (char) GETJSAMPLE(*ptr0++);
-    }
-    (void) JFWRITE(outfile, row_buffer, width);
-  }
-}
-
-#endif /* USE_PUTC_OUTPUT */
-
-
-/*
- * Write some demapped pixel data when color quantization is in effect.
- * For Targa, this is only applied to grayscale data.
- */
-
-#ifdef USE_PUTC_OUTPUT
-
-METHODDEF void
-put_demapped_gray (decompress_info_ptr cinfo, int num_rows,
-		   JSAMPIMAGE pixel_data)
-{
-  register FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr;
-  register JSAMPROW color_map0 = cinfo->colormap[0];
-  register int pixval;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr = pixel_data[0][row];
-    for (col = width; col > 0; col--) {
-      pixval = GETJSAMPLE(*ptr++);
-      putc(GETJSAMPLE(color_map0[pixval]), outfile);
-    }
-  }
-}
-
-#else /* use row buffering */
-
-METHODDEF void
-put_demapped_gray (decompress_info_ptr cinfo, int num_rows,
-		   JSAMPIMAGE pixel_data)
-{
-  FILE * outfile = cinfo->output_file;
-  register JSAMPROW ptr;
-  register char * row_bufferptr;
-  register JSAMPROW color_map0 = cinfo->colormap[0];
-  register int pixval;
-  register long col;
-  long width = cinfo->image_width;
-  int row;
-  
-  for (row = 0; row < num_rows; row++) {
-    ptr = pixel_data[0][row];
-    row_bufferptr = row_buffer;
-    for (col = width; col > 0; col--) {
-      pixval = GETJSAMPLE(*ptr++);
-      *row_bufferptr++ = (char) GETJSAMPLE(color_map0[pixval]);
-    }
-    (void) JFWRITE(outfile, row_buffer, width);
-  }
-}
-
-#endif /* USE_PUTC_OUTPUT */
-
-
-/*
- * Write the color map.
- */
-
-METHODDEF void
-put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
-{
-  register FILE * outfile = cinfo->output_file;
-  int i;
-
-  if (cinfo->out_color_space == CS_RGB) {
-    /* We only support 8-bit colormap indexes, so only 256 colors */
-    if (num_colors > 256)
-      ERREXIT(cinfo->emethods, "Too many colors for Targa output");
-    /* Time to write the header */
-    write_header(cinfo, num_colors);
-    /* Write the colormap.  Note Targa uses BGR byte order */
-    for (i = 0; i < num_colors; i++) {
-      putc(GETJSAMPLE(colormap[2][i]), outfile);
-      putc(GETJSAMPLE(colormap[1][i]), outfile);
-      putc(GETJSAMPLE(colormap[0][i]), outfile);
-    }
-  } else {
-    cinfo->methods->put_pixel_rows = put_demapped_gray;
-  }
-}
-
-
-/*
- * Finish up at the end of the file.
- */
-
-METHODDEF void
-output_term (decompress_info_ptr cinfo)
-{
-  /* No work except to make sure we wrote the output file OK */
-  fflush(cinfo->output_file);
-  if (ferror(cinfo->output_file))
-    ERREXIT(cinfo->emethods, "Output file write error --- out of disk space?");
-}
-
-
-/*
- * The method selection routine for Targa format output.
- * This should be called from d_ui_method_selection if Targa output is wanted.
- */
-
-GLOBAL void
-jselwtarga (decompress_info_ptr cinfo)
-{
-  cinfo->methods->output_init = output_init;
-  cinfo->methods->put_color_map = put_color_map;
-  if (cinfo->out_color_space == CS_GRAYSCALE || cinfo->quantize_colors)
-    cinfo->methods->put_pixel_rows = put_gray_rows;
-  else
-    cinfo->methods->put_pixel_rows = put_pixel_rows;
-  cinfo->methods->output_term = output_term;
-}
-
-#endif /* TARGA_SUPPORTED */
diff --git a/libjpeg.doc b/libjpeg.doc
new file mode 100644
index 0000000..83067d1
--- /dev/null
+++ b/libjpeg.doc
@@ -0,0 +1,2144 @@
+USING THE IJG JPEG LIBRARY
+
+Copyright (C) 1994, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+This file describes how to use the IJG JPEG library within an application
+program.  Read it if you want to write a program that uses the library.
+
+The file example.c provides heavily commented skeleton code for calling the
+JPEG library.  Also see jpeglib.h (the include file to be used by application
+programs) for full details about data structures and function parameter lists.
+The library source code, of course, is the ultimate reference.
+
+Note that there have been *major* changes from the application interface
+presented by IJG version 4 and earlier versions.  The old design had several
+inherent limitations, and it had accumulated a lot of cruft as we added
+features while trying to minimize application-interface changes.  We have
+sacrificed backward compatibility in the version 5 rewrite, but we think the
+improvements justify this.
+
+
+TABLE OF CONTENTS
+-----------------
+
+Overview:
+	Functions provided by the library
+	Outline of typical usage
+Basic library usage:
+	Data formats
+	Compression details
+	Decompression details
+	Mechanics of usage: include files, linking, etc
+Advanced features:
+	Compression parameter selection
+	Decompression parameter selection
+	Special color spaces
+	Error handling
+	Compressed data handling (source and destination managers)
+	I/O suspension
+	Abbreviated datastreams and multiple images
+	Special markers
+	Raw (downsampled) image data
+	Progress monitoring
+	Memory management
+	Library compile-time options
+	Portability considerations
+	Notes for MS-DOS implementors
+
+You should read at least the overview and basic usage sections before trying
+to program with the library.  The sections on advanced features can be read
+if and when you need them.
+
+
+OVERVIEW
+========
+
+Functions provided by the library
+---------------------------------
+
+The IJG JPEG library provides C code to read and write JPEG-compressed image
+files.  The surrounding application program receives or supplies image data a
+scanline at a time, using a straightforward uncompressed image format.  All
+details of color conversion and other preprocessing/postprocessing can be
+handled by the library.
+
+The library includes a substantial amount of code that is not covered by the
+JPEG standard but is necessary for typical applications of JPEG.  These
+functions preprocess the image before JPEG compression or postprocess it after
+decompression.  They include colorspace conversion, downsampling/upsampling,
+and color quantization.  The application indirectly selects use of this code
+by specifying the format in which it wishes to supply or receive image data.
+For example, if colormapped output is requested, then the decompression
+library automatically invokes color quantization.
+
+A wide range of quality vs. speed tradeoffs are possible in JPEG processing,
+and even more so in decompression postprocessing.  The decompression library
+provides multiple implementations that cover most of the useful tradeoffs,
+ranging from very-high-quality down to fast-preview operation.  On the
+compression side we have generally not provided low-quality choices, since
+compression is normally less time-critical.  It should be understood that the
+low-quality modes may not meet the JPEG standard's accuracy requirements;
+nonetheless, they are useful for viewers.
+
+A word about functions *not* provided by the library.  We handle a subset of
+the ISO JPEG standard; most baseline and extended-sequential JPEG processes
+are supported.  (Our subset includes all features now in common use.)
+Unsupported ISO options include:
+	* Progressive storage (may be supported in future versions)
+	* Hierarchical storage
+	* Lossless JPEG
+	* Arithmetic entropy coding (unsupported for legal reasons)
+	* DNL marker
+	* Nonintegral subsampling ratios
+We support both 8- and 12-bit data precision, but this is a compile-time
+choice rather than a run-time choice; hence it is difficult to use both
+precisions in a single application.
+
+By itself, the library handles only interchange JPEG datastreams --- in
+particular the widely used JFIF file format.  The library can be used by
+surrounding code to process interchange or abbreviated JPEG datastreams that
+are embedded in more complex file formats.  (For example, we anticipate that
+Sam Leffler's LIBTIFF library will use this code to support the revised TIFF
+JPEG format.)
+
+
+Outline of typical usage
+------------------------
+
+The rough outline of a JPEG compression operation is:
+
+	Allocate and initialize a JPEG compression object
+	Specify the destination for the compressed data (eg, a file)
+	Set parameters for compression, including image size & colorspace
+	jpeg_start_compress(...);
+	while (scan lines remain to be written)
+		jpeg_write_scanlines(...);
+	jpeg_finish_compress(...);
+	Release the JPEG compression object
+
+A JPEG compression object holds parameters and working state for the JPEG
+library.  We make creation/destruction of the object separate from starting
+or finishing compression of an image; the same object can be re-used for a
+series of image compression operations.  This makes it easy to re-use the
+same parameter settings for a sequence of images.  Re-use of a JPEG object
+also has important implications for processing abbreviated JPEG datastreams,
+as discussed later.
+
+The image data to be compressed is supplied to jpeg_write_scanlines() from
+in-memory buffers.  If the application is doing file-to-file compression,
+reading image data from the source file is the application's responsibility.
+The library emits compressed data by calling a "data destination manager",
+which typically will write the data into a file; but the application can
+provide its own destination manager to do something else.
+
+Similarly, the rough outline of a JPEG decompression operation is:
+
+	Allocate and initialize a JPEG decompression object
+	Specify the source of the compressed data (eg, a file)
+	Call jpeg_read_header() to obtain image info
+	Set parameters for decompression
+	jpeg_start_decompress(...);
+	while (scan lines remain to be read)
+		jpeg_read_scanlines(...);
+	jpeg_finish_decompress(...);
+	Release the JPEG decompression object
+
+This is comparable to the compression outline except that reading the
+datastream header is a separate step.  This is helpful because information
+about the image's size, colorspace, etc is available when the application
+selects decompression parameters.  For example, the application can choose an
+output scaling ratio that will fit the image into the available screen size.
+
+The decompression library obtains compressed data by calling a data source
+manager, which typically will read the data from a file; but other behaviors
+can be obtained with a custom source manager.  Decompressed data is delivered
+into in-memory buffers passed to jpeg_read_scanlines().
+
+It is possible to abort an incomplete compression or decompression operation
+by calling jpeg_abort(); or, if you do not need to retain the JPEG object,
+simply release it by calling jpeg_destroy().
+
+JPEG compression and decompression objects are two separate struct types.
+However, they share some common fields, and certain routines such as
+jpeg_destroy() can work on either type of object.
+
+The JPEG library has no static variables: all state is in the compression
+or decompression object.  Therefore it is possible to process multiple
+compression and decompression operations concurrently, using multiple JPEG
+objects.
+
+Both compression and decompression can be done in an incremental memory-to-
+memory fashion, if suitable source/destination managers are used.  However,
+there are some restrictions on the processing that can be done in this mode.
+See the section on "I/O suspension" for more details.
+
+
+BASIC LIBRARY USAGE
+===================
+
+Data formats
+------------
+
+Before diving into procedural details, it is helpful to understand the
+image data format that the JPEG library expects or returns.
+
+The standard input image format is a rectangular array of pixels, with each
+pixel having the same number of "component" values (color channels).  You
+must specify how many components there are and the colorspace interpretation
+of the components.  Most applications will use RGB data (three components
+per pixel) or grayscale data (one component per pixel).
+
+Note that there is no provision for colormapped input.  You can feed in a
+colormapped image by expanding it to full-color format.  However JPEG often
+doesn't work very well with colormapped source data, because of dithering
+noise.  This is discussed in more detail in the JPEG FAQ and the other
+references mentioned in the README file.
+
+Pixels are stored by scanlines, with each scanline running from left to
+right.  The component values for each pixel are adjacent in the row; for
+example, R,G,B,R,G,B,R,G,B,... for 24-bit RGB color.  Each scanline is an
+array of data type JSAMPLE --- which is typically "unsigned char", unless
+you've changed jmorecfg.h.  (You can also change the RGB pixel layout, say
+to B,G,R order, by modifying jmorecfg.h.  But see the restrictions listed in
+that file before doing so.)
+
+A 2-D array of pixels is formed by making a list of pointers to the starts of
+scanlines; so the scanlines need not be physically adjacent in memory.  Even
+if you process just one scanline at a time, you must make a one-element
+pointer array to serve this purpose.  Pointers to JSAMPLE rows are of type
+JSAMPROW, and the pointer to the pointer array is of type JSAMPARRAY.
+
+The library accepts or supplies one or more complete scanlines per call.
+It is not possible to process part of a row at a time.  Scanlines are always
+processed top-to-bottom.  You can process an entire image in one call if you
+have it all in memory, but usually it's simplest to process one scanline at
+a time.
+
+For best results, source data values should have the precision specified by
+BITS_IN_JSAMPLE (normally 8 bits).  For instance, if you choose to compress
+data that's only 6 bits/channel, you should left-justify each value in a
+byte before passing it to the compressor.  If you need to compress data
+that has more than 8 bits/channel, compile with BITS_IN_JSAMPLE = 12.
+(See "Library compile-time options", later.)
+
+The data format returned by the decompressor is the same in all details,
+except that colormapped data is supported.  If you request colormapped
+output then the returned data array contains a single JSAMPLE per pixel;
+its value is an index into a color map.  The color map is represented as
+a 2-D JSAMPARRAY in which each row holds the values of one color component,
+that is, colormap[i][j] is the value of the i'th color component for pixel
+value (map index) j.  Note that since the colormap indexes are stored in
+JSAMPLEs, the maximum number of colors is limited by the size of JSAMPLE
+(ie, at most 256 colors for an 8-bit JPEG library).
+
+
+Compression details
+-------------------
+
+Here we revisit the JPEG compression outline given in the overview.
+
+1. Allocate and initialize a JPEG compression object.
+
+A JPEG compression object is a "struct jpeg_compress_struct" (plus a bunch of
+subsidiary structures which are allocated via malloc(), but the application
+doesn't control those directly).  This struct can be just a local variable in
+the calling routine, if a single routine is going to execute the whole JPEG
+compression sequence.  Otherwise it can be static or allocated from malloc().
+
+You will also need a structure representing a JPEG error handler.  The part
+of this that the library cares about is a "struct jpeg_error_mgr".  If you
+are providing your own error handler, you'll typically want to embed the
+jpeg_error_mgr struct in a larger structure; this is discussed later under
+"Error handling".  For now we'll assume you are just using the default error
+handler.  The default error handler will print JPEG error/warning messages
+on stderr, and it will call exit() if a fatal error occurs.
+
+You must initialize the error handler structure, store a pointer to it into
+the JPEG object's "err" field, and then call jpeg_create_compress() to
+initialize the rest of the JPEG object.
+
+Typical code for this step, if you are using the default error handler, is
+
+	struct jpeg_compress_struct cinfo;
+	struct jpeg_error_mgr jerr;
+	...
+	cinfo.err = jpeg_std_error(&jerr);
+	jpeg_create_compress(&cinfo);
+
+jpeg_create_compress allocates a small amount of memory, so it could fail
+if you are out of memory.  In that case it will exit via the error handler;
+that's why the error handler must be initialized first.
+
+
+2. Specify the destination for the compressed data (eg, a file).
+
+As previously mentioned, the JPEG library delivers compressed data to a
+"data destination" module.  The library includes one data destination
+module which knows how to write to a stdio stream.  You can use your own
+destination module if you want to do something else, as discussed later.
+
+If you use the standard destination module, you must open the target stdio
+stream beforehand.  Typical code for this step looks like:
+
+	FILE * outfile;
+	...
+	if ((outfile = fopen(filename, "wb")) == NULL) {
+	    fprintf(stderr, "can't open %s\n", filename);
+	    exit(1);
+	}
+	jpeg_stdio_dest(&cinfo, outfile);
+
+where the last line invokes the standard destination module.
+
+WARNING: it is critical that the binary compressed data be delivered to the
+output file unchanged.  On non-Unix systems the stdio library may perform
+newline translation or otherwise corrupt binary data.  To suppress this
+behavior, you may need to use a "b" option to fopen (as shown above), or use
+setmode() or another routine to put the stdio stream in binary mode.  See
+cjpeg.c and djpeg.c for code that has been found to work on many systems.
+
+You can select the data destination after setting other parameters (step 3),
+if that's more convenient.  You may not change the destination between
+calling jpeg_start_compress() and jpeg_finish_compress().
+
+
+3. Set parameters for compression, including image size & colorspace.
+
+You must supply information about the source image by setting the following
+fields in the JPEG object (cinfo structure):
+
+	image_width		Width of image, in pixels
+	image_height		Height of image, in pixels
+	input_components	Number of color channels (samples per pixel)
+	in_color_space		Color space of source image
+
+The image dimensions are, hopefully, obvious.  JPEG supports image dimensions
+of 1 to 64K pixels in either direction.  The input color space is typically
+RGB or grayscale, and input_components is 3 or 1 accordingly.  (See "Special
+color spaces", later, for more info.)  The in_color_space field must be
+assigned one of the J_COLOR_SPACE enum constants, typically JCS_RGB or
+JCS_GRAYSCALE.
+
+JPEG has a large number of compression parameters that determine how the
+image is encoded.  Most applications don't need or want to know about all
+these parameters.  You can set all the parameters to reasonable defaults by
+calling jpeg_set_defaults(); then, if there are particular values you want
+to change, you can do so after that.  The "Compression parameter selection"
+section tells about all the parameters.
+
+You must set in_color_space correctly before calling jpeg_set_defaults(),
+because the defaults depend on the source image colorspace.  However the
+other three source image parameters need not be valid until you call
+jpeg_start_compress().  There's no harm in calling jpeg_set_defaults() more
+than once, if that happens to be convenient.
+
+Typical code for a 24-bit RGB source image is
+
+	cinfo.image_width = Width; 	/* image width and height, in pixels */
+	cinfo.image_height = Height;
+	cinfo.input_components = 3;	/* # of color components per pixel */
+	cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
+
+	jpeg_set_defaults(&cinfo);
+	/* Make optional parameter settings here */
+
+
+4. jpeg_start_compress(...);
+
+After you have established the data destination and set all the necessary
+source image info and other parameters, call jpeg_start_compress() to begin
+a compression cycle.  This will initialize internal state, allocate working
+storage, and emit the first few bytes of the JPEG datastream header.
+
+Typical code:
+
+	jpeg_start_compress(&cinfo, TRUE);
+
+The "TRUE" parameter ensures that a complete JPEG interchange datastream
+will be written.  This is appropriate in most cases.  If you think you might
+want to use an abbreviated datastream, read the section on abbreviated
+datastreams, below.
+
+Once you have called jpeg_start_compress(), you may not alter any JPEG
+parameters or other fields of the JPEG object until you have completed
+the compression cycle.
+
+
+5. while (scan lines remain to be written)
+	jpeg_write_scanlines(...);
+
+Now write all the required image data by calling jpeg_write_scanlines()
+one or more times.  You can pass one or more scanlines in each call, up
+to the total image height.  In most applications it is convenient to pass
+just one or a few scanlines at a time.  The expected format for the passed
+data is discussed under "Data formats", above.
+
+Image data should be written in top-to-bottom scanline order.  The JPEG spec
+contains some weasel wording about how top and bottom are application-defined
+terms (a curious interpretation of the English language...) but if you want
+your files to be compatible with everyone else's, you WILL use top-to-bottom
+order.  If the source data must be read in bottom-to-top order, you can use
+the JPEG library's virtual array mechanism to invert the data efficiently.
+Examples of this can be found in the sample application cjpeg.
+
+The library maintains a count of the number of scanlines written so far
+in the next_scanline field of the JPEG object.  Usually you can just use
+this variable as the loop counter, so that the loop test looks like
+"while (cinfo.next_scanline < cinfo.image_height)".
+
+Code for this step depends heavily on the way that you store the source data.
+example.c shows the following code for the case of a full-size 2-D source
+array containing 3-byte RGB pixels:
+
+	JSAMPROW row_pointer[1];	/* pointer to a single row */
+	int row_stride;			/* physical row width in buffer */
+
+	row_stride = image_width * 3;	/* JSAMPLEs per row in image_buffer */
+
+	while (cinfo.next_scanline < cinfo.image_height) {
+	    row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
+	    jpeg_write_scanlines(&cinfo, row_pointer, 1);
+	}
+
+jpeg_write_scanlines() returns the number of scanlines actually written.
+This will normally be equal to the number passed in, so you can usually
+ignore the return value.  It is different in just two cases:
+  * If you try to write more scanlines than the declared image height,
+    the additional scanlines are ignored.
+  * If you use a suspending data destination manager, output buffer overrun
+    will cause the compressor to return before accepting all the passed lines.
+    This feature is discussed under "I/O suspension", below.  The normal
+    stdio destination manager will NOT cause this to happen.
+In any case, the return value is the same as the change in the value of
+next_scanline.
+
+
+6. jpeg_finish_compress(...);
+
+After all the image data has been written, call jpeg_finish_compress() to
+complete the compression cycle.  This step is ESSENTIAL to ensure that the
+last bufferload of data is written to the data destination.
+jpeg_finish_compress() also releases working memory associated with the JPEG
+object.
+
+Typical code:
+
+	jpeg_finish_compress(&cinfo);
+
+If using the stdio destination manager, don't forget to close the output
+stdio stream if necessary.
+
+If you have requested a multi-pass operating mode, such as Huffman code
+optimization, jpeg_finish_compress() will perform the additional passes using
+data buffered by the first pass.  In this case jpeg_finish_compress() may take
+quite a while to complete.  With the default compression parameters, this will
+not happen.
+
+It is an error to call jpeg_finish_compress() before writing the necessary
+total number of scanlines.  If you wish to abort compression, call
+jpeg_abort() as discussed below.
+
+After completing a compression cycle, you may dispose of the JPEG object
+as discussed next, or you may use it to compress another image.  In that case
+return to step 2, 3, or 4 as appropriate.  If you do not change the
+destination manager, the new datastream will be written to the same target.
+If you do not change any JPEG parameters, the new datastream will be written
+with the same parameters as before.  Note that you can change the input image
+dimensions freely between cycles, but if you change the input colorspace, you
+should call jpeg_set_defaults() to adjust for the new colorspace; and then
+you'll need to repeat all of step 3.
+
+
+7. Release the JPEG compression object.
+
+When you are done with a JPEG compression object, destroy it by calling
+jpeg_destroy_compress().  This will free all subsidiary memory.  Or you can
+call jpeg_destroy() which works for either compression or decompression
+objects --- this may be more convenient if you are sharing code between
+compression and decompression cases.  (Actually, these routines are equivalent
+except for the declared type of the passed pointer.  To avoid gripes from
+ANSI C compilers, pass a j_common_ptr to jpeg_destroy().)
+
+If you allocated the jpeg_compress_struct structure from malloc(), freeing
+it is your responsibility --- jpeg_destroy() won't.  Ditto for the error
+handler structure.
+
+Typical code:
+
+	jpeg_destroy_compress(&cinfo);
+
+
+8. Aborting.
+
+If you decide to abort a compression cycle before finishing, you can clean up
+in either of two ways:
+
+* If you don't need the JPEG object any more, just call
+  jpeg_destroy_compress() or jpeg_destroy() to release memory.  This is
+  legitimate at any point after calling jpeg_create_compress() --- in fact,
+  it's safe even if jpeg_create_compress() fails.
+
+* If you want to re-use the JPEG object, call jpeg_abort_compress(), or
+  jpeg_abort() which works on both compression and decompression objects.
+  This will return the object to an idle state, releasing any working memory.
+  jpeg_abort() is allowed at any time after successful object creation.
+
+Note that cleaning up the data destination, if required, is your
+responsibility.
+
+
+Decompression details
+---------------------
+
+Here we revisit the JPEG decompression outline given in the overview.
+
+1. Allocate and initialize a JPEG decompression object.
+
+This is just like initialization for compression, as discussed above,
+except that the object is a "struct jpeg_decompress_struct" and you
+call jpeg_create_decompress().  Error handling is exactly the same.
+
+Typical code:
+
+	struct jpeg_decompress_struct cinfo;
+	struct jpeg_error_mgr jerr;
+	...
+	cinfo.err = jpeg_std_error(&jerr);
+	jpeg_create_decompress(&cinfo);
+
+(Both here and in the IJG code, we usually use variable name "cinfo" for
+both compression and decompression objects.)
+
+
+2. Specify the source of the compressed data (eg, a file).
+
+As previously mentioned, the JPEG library reads compressed data from a "data
+source" module.  The library includes one data source module which knows how
+to read from a stdio stream.  You can use your own source module if you want
+to do something else, as discussed later.
+
+If you use the standard source module, you must open the source stdio stream
+beforehand.  Typical code for this step looks like:
+
+	FILE * infile;
+	...
+	if ((infile = fopen(filename, "rb")) == NULL) {
+	    fprintf(stderr, "can't open %s\n", filename);
+	    exit(1);
+	}
+	jpeg_stdio_src(&cinfo, infile);
+
+where the last line invokes the standard source module.
+
+WARNING: it is critical that the binary compressed data be read unchanged.
+On non-Unix systems the stdio library may perform newline translation or
+otherwise corrupt binary data.  To suppress this behavior, you may need to use
+a "b" option to fopen (as shown above), or use setmode() or another routine to
+put the stdio stream in binary mode.  See cjpeg.c and djpeg.c for code that
+has been found to work on many systems.
+
+You may not change the data source between calling jpeg_read_header() and
+jpeg_finish_decompress().  If you wish to read a series of JPEG images from
+a single source file, you should repeat the jpeg_read_header() to
+jpeg_finish_decompress() sequence without reinitializing either the JPEG
+object or the data source module; this prevents buffered input data from
+being discarded.
+
+
+3. Call jpeg_read_header() to obtain image info.
+
+Typical code for this step is just
+
+	jpeg_read_header(&cinfo, TRUE);
+
+This will read the source datastream header markers, up to the beginning
+of the compressed data proper.  On return, the image dimensions and other
+info have been stored in the JPEG object.  The application may wish to
+consult this information before selecting decompression parameters.
+
+More complex code is necessary if
+  * A suspending data source is used --- in that case jpeg_read_header()
+    may return before it has read all the header data.  See "I/O suspension",
+    below.  The normal stdio source manager will NOT cause this to happen.
+  * Abbreviated JPEG files are to be processed --- see the section on
+    abbreviated datastreams.  Standard applications that deal only in
+    interchange JPEG files need not be concerned with this case either.
+
+It is permissible to stop at this point if you just wanted to find out the
+image dimensions and other header info for a JPEG file.  In that case,
+call jpeg_destroy() when you are done with the JPEG object, or call
+jpeg_abort() to return it to an idle state before selecting a new data
+source and reading another header.
+
+
+4. Set parameters for decompression.
+
+jpeg_read_header() sets appropriate default decompression parameters based on
+the properties of the image (in particular, its colorspace).  However, you
+may well want to alter these defaults before beginning the decompression.
+For example, the default is to produce full color output from a color file.
+If you want colormapped output you must ask for it.  Other options allow the
+returned image to be scaled and allow various speed/quality tradeoffs to be
+selected.  "Decompression parameter selection", below, gives details.
+
+If the defaults are appropriate, nothing need be done at this step.
+
+Note that all default values are set by each call to jpeg_read_header().
+If you reuse a decompression object, you cannot expect your parameter
+settings to be preserved across cycles, as you can for compression.
+You must adjust parameter values each time.
+
+
+5. jpeg_start_decompress(...);
+
+Once the parameter values are satisfactory, call jpeg_start_decompress() to
+begin decompression.  This will initialize internal state, allocate working
+memory, and prepare for returning data.
+
+Typical code is just
+
+	jpeg_start_decompress(&cinfo);
+
+If you have requested a multi-pass operating mode, such as 2-pass color
+quantization, jpeg_start_decompress() will do everything needed before data
+output can begin.  In this case jpeg_start_decompress() may take quite a while
+to complete.  With a single-scan (fully interleaved) JPEG file and default
+decompression parameters, this will not happen; jpeg_start_decompress() will
+return quickly.
+
+After this call, the final output image dimensions, including any requested
+scaling, are available in the JPEG object; so is the selected colormap, if
+colormapped output has been requested.  Useful fields include
+
+	output_width		image width and height, as scaled
+	output_height
+	out_color_components	# of color components in out_color_space
+	output_components	# of color components returned per pixel
+	colormap		the selected colormap, if any
+	actual_number_of_colors		number of entries in colormap
+
+output_components is 1 (a colormap index) when quantizing colors; otherwise it
+equals out_color_components.  It is the number of JSAMPLE values that will be
+emitted per pixel in the output arrays.
+
+Typically you will need to allocate data buffers to hold the incoming image.
+You will need output_width * output_components JSAMPLEs per scanline in your
+output buffer, and a total of output_height scanlines will be returned.
+
+Note: if you are using the JPEG library's internal memory manager to allocate
+data buffers (as djpeg does), then the manager's protocol requires that you
+request large buffers *before* calling jpeg_start_decompress().  This is a
+little tricky since the output_XXX fields are not normally valid then.  You
+can make them valid by calling jpeg_calc_output_dimensions() after setting the
+relevant parameters (scaling, output color space, and quantization flag).
+
+
+6. while (scan lines remain to be read)
+	jpeg_read_scanlines(...);
+
+Now you can read the decompressed image data by calling jpeg_read_scanlines()
+one or more times.  At each call, you pass in the maximum number of scanlines
+to be read (ie, the height of your working buffer); jpeg_read_scanlines()
+will return up to that many lines.  The return value is the number of lines
+actually read.  The format of the returned data is discussed under "Data
+formats", above.
+
+Image data is returned in top-to-bottom scanline order.  If you must write
+out the image in bottom-to-top order, you can use the JPEG library's virtual
+array mechanism to invert the data efficiently.  Examples of this can be
+found in the sample application djpeg.
+
+The library maintains a count of the number of scanlines returned so far
+in the output_scanline field of the JPEG object.  Usually you can just use
+this variable as the loop counter, so that the loop test looks like
+"while (cinfo.output_scanline < cinfo.output_height)".  (Note that the test
+should NOT be against image_height, unless you never use scaling.  The
+image_height field is the height of the original unscaled image.)
+
+If you don't use a suspending data source, it is safe to assume that
+jpeg_read_scanlines() reads at least one scanline per call, until the
+bottom of the image has been reached.  If you use a buffer larger than one
+scanline, it is NOT safe to assume that jpeg_read_scanlines() fills it.
+In any case, the return value is the same as the change in the value of
+output_scanline.
+
+
+7. jpeg_finish_decompress(...);
+
+After all the image data has been read, call jpeg_finish_decompress() to
+complete the decompression cycle.  This causes working memory associated
+with the JPEG object to be released.
+
+Typical code:
+
+	jpeg_finish_decompress(&cinfo);
+
+If using the stdio source manager, don't forget to close the source stdio
+stream if necessary.
+
+It is an error to call jpeg_finish_decompress() before reading the correct
+total number of scanlines.  If you wish to abort compression, call
+jpeg_abort() as discussed below.
+
+After completing a decompression cycle, you may dispose of the JPEG object as
+discussed next, or you may use it to decompress another image.  In that case
+return to step 2 or 3 as appropriate.  If you do not change the source
+manager, the next image will be read from the same source.
+
+
+8. Release the JPEG decompression object.
+
+When you are done with a JPEG decompression object, destroy it by calling
+jpeg_destroy_decompress() or jpeg_destroy().  The previous discussion of
+destroying compression objects applies here too.
+
+Typical code:
+
+	jpeg_destroy_decompress(&cinfo);
+
+
+9. Aborting.
+
+You can abort a decompression cycle by calling jpeg_destroy_decompress() or
+jpeg_destroy() if you don't need the JPEG object any more, or
+jpeg_abort_decompress() or jpeg_abort() if you want to reuse the object.
+The previous discussion of aborting compression cycles applies here too.
+
+
+Mechanics of usage: include files, linking, etc
+-----------------------------------------------
+
+Applications using the JPEG library should include the header file jpeglib.h
+to obtain declarations of data types and routines.  Before including
+jpeglib.h, include system headers that define at least the typedefs FILE and
+size_t.  On ANSI-conforming systems, including <stdio.h> is sufficient; on
+older Unix systems, you may need <sys/types.h> to define size_t.
+
+If the application needs to refer to individual JPEG library error codes, also
+include jerror.h to define those symbols.
+
+jpeglib.h indirectly includes the files jconfig.h and jmorecfg.h.  If you are
+installing the JPEG header files in a system directory, you will want to
+install all four files: jpeglib.h, jerror.h, jconfig.h, jmorecfg.h.
+
+The most convenient way to include the JPEG code into your executable program
+is to prepare a library file ("libjpeg.a", or a corresponding name on non-Unix
+machines) and reference it at your link step.  If you use only half of the
+library (only compression or only decompression), only that much code will be
+included from the library, unless your linker is hopelessly brain-damaged.
+The supplied makefiles build libjpeg.a automatically (see install.doc).
+
+On some systems your application may need to set up a signal handler to ensure
+that temporary files are deleted if the program is interrupted.  This is most
+critical if you are on MS-DOS and use the jmemdos.c memory manager back end;
+it will try to grab extended memory for temp files, and that space will NOT be
+freed automatically.  See cjpeg.c or djpeg.c for an example signal handler.
+
+It may be worth pointing out that the core JPEG library does not actually
+require the stdio library: only the default source/destination managers and
+error handler need it.  You can use the library in a stdio-less environment
+if you replace those modules and use jmemnobs.c (or another memory manager of
+your own devising).  More info about the minimum system library requirements
+may be found in jinclude.h.
+
+
+ADVANCED FEATURES
+=================
+
+Compression parameter selection
+-------------------------------
+
+This section describes all the optional parameters you can set for JPEG
+compression, as well as the "helper" routines provided to assist in this
+task.  Proper setting of some parameters requires detailed understanding
+of the JPEG standard; if you don't know what a parameter is for, it's best
+not to mess with it!  See REFERENCES in the README file for pointers to
+more info about JPEG.
+
+It's a good idea to call jpeg_set_defaults() first, even if you plan to set
+all the parameters; that way your code is more likely to work with future JPEG
+libraries that have additional parameters.  For the same reason, we recommend
+you use a helper routine where one is provided, in preference to twiddling
+cinfo fields directly.
+
+The helper routines are:
+
+jpeg_set_defaults (j_compress_ptr cinfo)
+	This routine sets all JPEG parameters to reasonable defaults, using
+	only the input image's color space (field in_color_space, which must
+	already be set in cinfo).  Many applications will only need to use
+	this routine and perhaps jpeg_set_quality().
+
+jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
+	Sets the JPEG file's colorspace (field jpeg_color_space) as specified,
+	and sets other color-space-dependent parameters appropriately.  See
+	"Special color spaces", below, before using this.  A large number of
+	parameters, including all per-component parameters, are set by this
+	routine; if you want to twiddle individual parameters you should call
+	jpeg_set_colorspace() before rather than after.
+
+jpeg_default_colorspace (j_compress_ptr cinfo)
+	Selects an appropriate JPEG colorspace based on cinfo->in_color_space,
+	and calls jpeg_set_colorspace().  This is actually a subroutine of
+	jpeg_set_defaults().  It's broken out in case you want to change
+	just the colorspace-dependent JPEG parameters.
+
+jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
+	Constructs JPEG quantization tables appropriate for the indicated
+	quality setting.  The quality value is expressed on the 0..100 scale
+	recommended by IJG (cjpeg's "-quality" switch uses this routine).
+	Note that the exact mapping from quality values to tables may change
+	in future IJG releases as more is learned about DCT quantization.
+	If the force_baseline parameter is TRUE, then the quantization table
+	entries are constrained to the range 1..255 for full JPEG baseline
+	compatibility.  In the current implementation, this only makes a
+	difference for quality settings below 25, and it effectively prevents
+	very small/low quality files from being generated.  The IJG decoder
+	is capable of reading the non-baseline files generated at low quality
+	settings when force_baseline is FALSE, but other decoders may not be.
+
+jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
+			 boolean force_baseline)
+	Same as jpeg_set_quality() except that the generated tables are the
+	sample tables given in the JPEC spec section K.1, multiplied by the
+	specified scale factor (which is expressed as a percentage; thus
+	scale_factor = 100 reproduces the spec's tables).  Note that larger
+	scale factors give lower quality.  This entry point is useful for
+	conforming to the Adobe PostScript DCT conventions, but we do not
+	recommend linear scaling as a user-visible quality scale otherwise.
+	force_baseline again constrains the computed table entries to 1..255.
+
+int jpeg_quality_scaling (int quality)
+	Converts a value on the IJG-recommended quality scale to a linear
+	scaling percentage.  Note that this routine may change or go away
+	in future releases --- IJG may choose to adopt a scaling method that
+	can't be expressed as a simple scalar multiplier, in which case the
+	premise of this routine collapses.  Caveat user.
+
+jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
+		      const unsigned int *basic_table,
+		      int scale_factor, boolean force_baseline));
+	Allows an arbitrary quantization table to be created.  which_tbl
+	indicates which table slot to fill.  basic_table points to an array
+	of 64 unsigned ints given in JPEG zigzag order.  These values are
+	multiplied by scale_factor/100 and then clamped to the range 1..65535
+	(or to 1..255 if force_baseline is TRUE).
+
+
+Compression parameters (cinfo fields) include:
+
+boolean optimize_coding
+	TRUE causes the compressor to compute optimal Huffman coding tables
+	for the image.  This requires an extra pass over the data and
+	therefore costs a good deal of space and time.  The default is
+	FALSE, which tells the compressor to use the supplied or default
+	Huffman tables.  In most cases optimal tables save only a few percent
+	of file size compared to the default tables.  Note that when this is
+	TRUE, you need not supply Huffman tables at all, and any you do
+	supply will be overwritten.
+
+int smoothing_factor
+	If non-zero, the input image is smoothed; the value should be 1 for
+	minimal smoothing to 100 for maximum smoothing.  Consult jcsample.c
+	for details of the smoothing algorithm.  The default is zero.
+
+J_DCT_METHOD dct_method
+	Selects the algorithm used for the DCT step.  Choices are:
+		JDCT_ISLOW: slow but accurate integer algorithm
+		JDCT_IFAST: faster, less accurate integer method
+		JDCT_FLOAT: floating-point method
+		JDCT_DEFAULT: default method (normally JDCT_ISLOW)
+		JDCT_FASTEST: fastest method (normally JDCT_IFAST)
+	The floating-point method is the most accurate, but may give slightly
+	different results on different machines due to varying roundoff
+	behavior.  The integer methods should give the same results on all
+	machines.  On machines with sufficiently fast FP hardware, the
+	floating-point method may also be the fastest.  The IFAST method is
+	considerably less accurate than the other two; its use is not
+	recommended if high quality is a concern.  JDCT_DEFAULT and
+	JDCT_FASTEST are macros configurable by each installation.
+
+unsigned int restart_interval
+int restart_in_rows
+	To emit restart markers in the JPEG file, set one of these nonzero.
+	Set restart_interval to specify the exact interval in MCU blocks.
+	Set restart_in_rows to specify the interval in MCU rows.  (If
+	restart_in_rows is not 0, then restart_interval is set after the
+	image width in MCUs is computed.)  Defaults are zero (no restarts).
+
+J_COLOR_SPACE jpeg_color_space
+int num_components
+	The JPEG color space and corresponding number of components; see
+	"Special color spaces", below, for more info.  We recommend using
+	jpeg_set_color_space() if you want to change these.
+
+boolean write_JFIF_header
+	If TRUE, a JFIF APP0 marker is emitted.  jpeg_set_defaults() and
+	jpeg_set_colorspace() set this TRUE if a JFIF-legal JPEG color space
+	(ie, YCbCr or grayscale) is selected, otherwise FALSE.
+
+UINT8 density_unit
+UINT16 X_density
+UINT16 Y_density
+	The resolution information to be written into the JFIF marker;
+	not used otherwise.  density_unit may be 0 for unknown,
+	1 for dots/inch, or 2 for dots/cm.  The default values are 0,1,1
+	indicating square pixels of unknown size.
+
+boolean write_Adobe_marker
+	If TRUE, an Adobe APP14 marker is emitted.  jpeg_set_defaults() and
+	jpeg_set_colorspace() set this TRUE if JPEG color space RGB, CMYK,
+	or YCCK is selected, otherwise FALSE.  It is generally a bad idea
+	to set both write_JFIF_header and write_Adobe_marker.  In fact,
+	you probably shouldn't change the default settings at all --- the
+	default behavior ensures that the JPEG file's color space can be
+	recognized by the decoder.
+
+JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]
+	Pointers to coefficient quantization tables, one per table slot,
+	or NULL if no table is defined for a slot.  Usually these should
+	be set via one of the above helper routines; jpeg_add_quant_table()
+	is general enough to define any quantization table.  The other
+	routines will set up table slot 0 for luminance quality and table
+	slot 1 for chrominance.
+
+JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]
+JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS]
+	Pointers to Huffman coding tables, one per table slot, or NULL if
+	no table is defined for a slot.  Slots 0 and 1 are filled with the
+	JPEG sample tables by jpeg_set_defaults().  If you need to allocate
+	more table structures, jpeg_alloc_huff_table() may be used.
+	Note that optimal Huffman tables can be computed for an image
+	by setting optimize_coding, as discussed above; there's seldom
+	any need to mess with providing your own Huffman tables.
+
+There are some additional cinfo fields which are not documented here
+because you currently can't change them; for example, you can't set
+arith_code TRUE because arithmetic coding is unsupported.
+
+
+Per-component parameters are stored in the struct cinfo.comp_info[i] for
+component number i.  Note that components here refer to components of the
+JPEG color space, *not* the source image color space.  A suitably large
+comp_info[] array is allocated by jpeg_set_defaults(); if you choose not
+to use that routine, it's up to you to allocate the array.
+
+int component_id
+	The one-byte identifier code to be recorded in the JPEG file for
+	this component.  For the standard color spaces, we recommend you
+	leave the default values alone.
+
+int h_samp_factor
+int v_samp_factor
+	Horizontal and vertical sampling factors for the component; must
+	be 1..4 according to the JPEG standard.  Note that larger sampling
+	factors indicate a higher-resolution component; many people find
+	this behavior quite unintuitive.  The default values are 2,2 for
+	luminance components and 1,1 for chrominance components, except
+	for grayscale where 1,1 is used.
+
+int quant_tbl_no
+	Quantization table number for component.  The default value is
+	0 for luminance components and 1 for chrominance components.
+
+int dc_tbl_no
+int ac_tbl_no
+	DC and AC entropy coding table numbers.  The default values are
+	0 for luminance components and 1 for chrominance components.
+
+int component_index
+	Must equal the component's index in comp_info[].
+
+
+Decompression parameter selection
+---------------------------------
+
+Decompression parameter selection is somewhat simpler than compression
+parameter selection, since all of the JPEG internal parameters are
+recorded in the source file and need not be supplied by the application.
+(Unless you are working with abbreviated files, in which case see
+"Abbreviated datastreams", below.)  Decompression parameters control
+the postprocessing done on the image to deliver it in a format suitable
+for the application's use.  Many of the parameters control speed/quality
+tradeoffs, in which faster decompression may be obtained at the price of
+a poorer-quality image.  The defaults select the highest quality (slowest)
+processing.
+
+The following fields in the JPEG object are set by jpeg_read_header() and
+may be useful to the application in choosing decompression parameters:
+
+JDIMENSION image_width			Width and height of image
+JDIMENSION image_height
+int num_components			Number of color components
+J_COLOR_SPACE jpeg_color_space		Colorspace of image
+boolean saw_JFIF_marker			TRUE if a JFIF APP0 marker was seen
+  UINT8 density_unit			Resolution data from JFIF marker
+  UINT16 X_density
+  UINT16 Y_density
+boolean saw_Adobe_marker		TRUE if an Adobe APP14 marker was seen
+  UINT8 Adobe_transform			Color transform code from Adobe marker
+
+The JPEG color space, unfortunately, is something of a guess since the JPEG
+standard proper does not provide a way to record it.  In practice most files
+adhere to the JFIF or Adobe conventions, and the decoder will recognize these
+correctly.  See "Special color spaces", below, for more info.
+
+
+The decompression parameters that determine the basic properties of the
+returned image are:
+
+J_COLOR_SPACE out_color_space
+	Output color space.  jpeg_read_header() sets an appropriate default
+	based on jpeg_color_space; typically it will be RGB or grayscale.
+	The application can change this field to request output in a different
+	colorspace.  For example, set it to JCS_GRAYSCALE to get grayscale
+	output from a color file.  (This is useful for previewing: grayscale
+	output is faster than full color since the color components need not
+	be processed.)  Note that not all possible color space transforms are
+	currently implemented; you may need to extend jdcolor.c if you want an
+	unusual conversion.
+
+unsigned int scale_num, scale_denom
+	Scale the image by the fraction scale_num/scale_denom.  Default is
+	1/1, or no scaling.  Currently, the only supported scaling ratios
+	are 1/1, 1/2, 1/4, and 1/8.  (The library design allows for arbitrary
+	scaling ratios but this is not likely to be implemented any time soon.)
+	Smaller scaling ratios permit significantly faster decoding since
+	fewer pixels need be processed and a simpler IDCT method can be used.
+
+boolean quantize_colors
+	If set TRUE, colormapped output will be delivered.  Default is FALSE,
+	meaning that full-color output will be delivered.
+
+The next three parameters are relevant only if quantize_colors is TRUE.
+
+int desired_number_of_colors
+	Maximum number of colors to use in generating a library-supplied color
+	map (the actual number of colors is returned in a different field).
+	Default 256.  Ignored when the application supplies its own color map.
+
+boolean two_pass_quantize
+	If TRUE, an extra pass over the image is made to select a custom color
+	map for the image.  This usually looks a lot better than the one-size-
+	fits-all colormap that is used otherwise.  Default is TRUE.  Ignored
+	when the application supplies its own color map.
+
+J_DITHER_MODE dither_mode
+	Selects color dithering method.  Supported values are:
+		JDITHER_NONE	no dithering: fast, very low quality
+		JDITHER_ORDERED	ordered dither: moderate speed and quality
+		JDITHER_FS	Floyd-Steinberg dither: slow, high quality
+	Default is JDITHER_FS.  (At present, ordered dither is implemented
+	only in the single-pass, standard-colormap case.  If you ask for
+	ordered dither when two_pass_quantize is TRUE or when you supply
+	an external color map, you'll get F-S dithering.)
+
+When quantize_colors is TRUE, the target color map is described by the next
+two fields.  colormap is set to NULL by jpeg_read_header().  The application
+can supply a color map by setting colormap non-NULL and setting
+actual_number_of_colors to the map size.  Otherwise, jpeg_start_decompress()
+selects a suitable color map and sets these two fields itself.
+[Implementation restriction: at present, an externally supplied colormap is
+only accepted for 3-component output color spaces.]
+
+JSAMPARRAY colormap
+	The color map, represented as a 2-D pixel array of out_color_components
+	rows and actual_number_of_colors columns.  Ignored if not quantizing.
+
+int actual_number_of_colors
+	The number of colors in the color map.
+
+Additional decompression parameters that the application may set include:
+
+J_DCT_METHOD dct_method
+	Selects the algorithm used for the DCT step.  Choices are the same
+	as described above for compression.
+
+boolean do_fancy_upsampling
+	If TRUE, do careful upsampling of chroma components.  If FALSE,
+	a faster but sloppier method is used.  Default is TRUE.  The visual
+	impact of the sloppier method is often very small.
+
+
+The output image dimensions are given by the following fields.  These are
+computed from the source image dimensions and the decompression parameters
+by jpeg_start_decompress().  You can also call jpeg_calc_output_dimensions()
+to obtain the values that will result from the current parameter settings.
+This can be useful if you are trying to pick a scaling ratio that will get
+close to a desired target size.  It's also important if you are using the
+JPEG library's memory manager to allocate output buffer space, because you
+are supposed to request such buffers *before* jpeg_start_decompress().
+
+JDIMENSION output_width		Actual dimensions of output image.
+JDIMENSION output_height
+int out_color_components	Number of color components in out_color_space.
+int output_components		Number of color components returned.
+int rec_outbuf_height		Recommended height of scanline buffer.
+
+When quantizing colors, output_components is 1, indicating a single color map
+index per pixel.  Otherwise it equals out_color_components.  The output arrays
+are required to be output_width * output_components JSAMPLEs wide.
+
+rec_outbuf_height is the recommended minimum height (in scanlines) of the
+buffer passed to jpeg_read_scanlines().  If the buffer is smaller, the
+library will still work, but time will be wasted due to unnecessary data
+copying.  In high-quality modes, rec_outbuf_height is always 1, but some
+faster, lower-quality modes set it to larger values (typically 2 to 4).
+If you are going to ask for a high-speed processing mode, you may as well
+go to the trouble of honoring rec_outbuf_height so as to avoid data copying.
+
+
+Special color spaces
+--------------------
+
+The JPEG standard itself is "color blind" and doesn't specify any particular
+color space.  It is customary to convert color data to a luminance/chrominance
+color space before compressing, since this permits greater compression.  The
+existing de-facto JPEG file format standards specify YCbCr or grayscale data
+(JFIF), or grayscale, RGB, YCbCr, CMYK, or YCCK (Adobe).  For special
+applications such as multispectral images, other color spaces can be used,
+but it must be understood that such files will be unportable.
+
+The JPEG library can handle the most common colorspace conversions (namely
+RGB <=> YCbCr and CMYK <=> YCCK).  It can also deal with data of an unknown
+color space, passing it through without conversion.  If you deal extensively
+with an unusual color space, you can easily extend the library to understand
+additional color spaces and perform appropriate conversions.
+
+For compression, the source data's color space is specified by field
+in_color_space.  This is transformed to the JPEG file's color space given
+by jpeg_color_space.  jpeg_set_defaults() chooses a reasonable JPEG color
+space depending on in_color_space, but you can override this by calling
+jpeg_set_colorspace().  Of course you must select a supported transformation.
+jccolor.c currently supports the following transformations:
+	RGB => YCbCr
+	RGB => GRAYSCALE
+	YCbCr => GRAYSCALE
+	CMYK => YCCK
+plus the null transforms: GRAYSCALE => GRAYSCALE, RGB => RGB,
+YCbCr => YCbCr, CMYK => CMYK, YCCK => YCCK, and UNKNOWN => UNKNOWN.
+
+The de-facto file format standards (JFIF and Adobe) specify APPn markers that
+indicate the color space of the JPEG file.  It is important to ensure that
+these are written correctly, or omitted if the JPEG file's color space is not
+one of the ones supported by the de-facto standards.  jpeg_set_colorspace()
+will set the compression parameters to include or omit the APPn markers
+properly, so long as it is told the truth about the JPEG color space.
+For example, if you are writing some random 3-component color space without
+conversion, don't try to fake out the library by setting in_color_space and
+jpeg_color_space to JCS_YCbCr; use JCS_UNKNOWN.  You may want to write an
+APPn marker of your own devising to identify the colorspace --- see "Special
+markers", below.
+
+When told that the color space is UNKNOWN, the library will default to using
+luminance-quality compression parameters for all color components.  You may
+well want to change these parameters.  See the source code for
+jpeg_set_colorspace(), in jcparam.c, for details.
+
+For decompression, the JPEG file's color space is given in jpeg_color_space,
+and this is transformed to the output color space out_color_space.
+jpeg_read_header's setting of jpeg_color_space can be relied on if the file
+conforms to JFIF or Adobe conventions, but otherwise it is no better than a
+guess.  If you know the JPEG file's color space for certain, you can override
+jpeg_read_header's guess by setting jpeg_color_space.  jpeg_read_header also
+selects a default output color space based on (its guess of) jpeg_color_space;
+set out_color_space to override this.  Again, you must select a supported
+transformation.  jdcolor.c currently supports
+	YCbCr => GRAYSCALE
+	YCbCr => RGB
+	YCCK => CMYK
+as well as the null transforms.
+
+The two-pass color quantizer, jquant2.c, is specialized to handle RGB data
+(it weights distances appropriately for RGB colors).  You'll need to modify
+the code if you want to use it for non-RGB output color spaces.  Note that
+jquant2.c is used to map to an application-supplied colormap as well as for
+the normal two-pass colormap selection process.
+
+CAUTION: it appears that Adobe Photoshop writes inverted data in CMYK JPEG
+files: 0 represents 100% ink coverage, rather than 0% ink as you'd expect.
+This is arguably a bug in Photoshop, but if you need to work with Photoshop
+CMYK files, you will have to deal with it in your application.  We cannot
+"fix" this in the library by inverting the data during the CMYK<=>YCCK
+transform, because that would break other applications, notably Ghostscript.
+Photoshop versions prior to 3.0 write EPS files containing JPEG-encoded CMYK
+data in the same inverted-YCCK representation used in bare JPEG files, but
+the surrounding PostScript code performs an inversion using the PS image
+operator.  I am told that Photoshop 3.0 will write uninverted YCCK in
+EPS/JPEG files, and will omit the PS-level inversion.  (But the data
+polarity used in bare JPEG files will not change in 3.0.)  In either case,
+the JPEG library must not invert the data itself, or else Ghostscript would
+read these EPS files incorrectly.
+
+
+Error handling
+--------------
+
+When the default error handler is used, any error detected inside the JPEG
+routines will cause a message to be printed on stderr, followed by exit().
+You can supply your own error handling routines to override this behavior
+and to control the treatment of nonfatal warnings and trace/debug messages.
+The file example.c illustrates the most common case, which is to have the
+application regain control after an error rather than exiting.
+
+The JPEG library never writes any message directly; it always goes through
+the error handling routines.  Three classes of messages are recognized:
+  * Fatal errors: the library cannot continue.
+  * Warnings: the library can continue, but the data is corrupt, and a
+    damaged output image is likely to result.
+  * Trace/informational messages.  These come with a trace level indicating
+    the importance of the message; you can control the verbosity of the
+    program by adjusting the maximum trace level that will be displayed.
+
+You may, if you wish, simply replace the entire JPEG error handling module
+(jerror.c) with your own code.  However, you can avoid code duplication by
+only replacing some of the routines depending on the behavior you need.
+This is accomplished by calling jpeg_std_error() as usual, but then overriding
+some of the method pointers in the jpeg_error_mgr struct, as illustrated by
+example.c.
+
+All of the error handling routines will receive a pointer to the JPEG object
+(a j_common_ptr which points to either a jpeg_compress_struct or a
+jpeg_decompress_struct; if you need to tell which, test the is_decompressor
+field).  This struct includes a pointer to the error manager struct in its
+"err" field.  Frequently, custom error handler routines will need to access
+additional data which is not known to the JPEG library or the standard error
+handler.  The most convenient way to do this is to embed either the JPEG
+object or the jpeg_error_mgr struct in a larger structure that contains
+additional fields; then casting the passed pointer provides access to the
+additional fields.  Again, see example.c for one way to do it.
+
+The individual methods that you might wish to override are:
+
+error_exit (j_common_ptr cinfo)
+	Receives control for a fatal error.  Information sufficient to
+	generate the error message has been stored in cinfo->err; call
+	output_message to display it.  Control must NOT return to the caller;
+	generally this routine will exit() or longjmp() somewhere.
+	Typically you would override this routine to get rid of the exit()
+	default behavior.  Note that if you continue processing, you should
+	clean up the JPEG object with jpeg_abort() or jpeg_destroy().
+
+output_message (j_common_ptr cinfo)
+	Actual output of any JPEG message.  Override this to send messages
+	somewhere other than stderr.  Note that this method does not know
+	how to generate a message, only where to send it.
+
+format_message (j_common_ptr cinfo, char * buffer)
+	Constructs a readable error message string based on the error info
+	stored in cinfo->err.  This method is called by output_message.  Few
+	applications should need to override this method.  One possible
+	reason for doing so is to implement dynamic switching of error message
+	language.
+
+emit_message (j_common_ptr cinfo, int msg_level)
+	Decide whether or not to emit a warning or trace message; if so,
+	calls output_message.  The main reason for overriding this method
+	would be to abort on warnings.  msg_level is -1 for warnings,
+	0 and up for trace messages.
+
+Only error_exit() and emit_message() are called from the rest of the JPEG
+library; the other two are internal to the error handler.
+
+The actual message texts are stored in an array of strings which is pointed to
+by the field err->jpeg_message_table.  The messages are numbered from 0 to
+err->last_jpeg_message, and it is these code numbers that are used in the
+JPEG library code.  You could replace the message texts (for instance, with
+messages in French or German) by changing the message table pointer.  See
+jerror.h for the default texts.  CAUTION: this table will almost certainly
+change or grow from one library version to the next.
+
+It may be useful for an application to add its own message texts that are
+handled by the same mechanism.  The error handler supports a second "add-on"
+message table for this purpose.  To define an addon table, set the pointer
+err->addon_message_table and the message numbers err->first_addon_message and
+err->last_addon_message.  If you number the addon messages beginning at 1000
+or so, you won't have to worry about conflicts with the library's built-in
+messages.  See the sample applications cjpeg/djpeg for an example of using
+addon messages (the addon messages are defined in cderror.h).
+
+Actual invocation of the error handler is done via macros defined in jerror.h:
+	ERREXITn(...)	for fatal errors
+	WARNMSn(...)	for corrupt-data warnings
+	TRACEMSn(...)	for trace and informational messages.
+These macros store the message code and any additional parameters into the
+error handler struct, then invoke the error_exit() or emit_message() method.
+The variants of each macro are for varying numbers of additional parameters.
+The additional parameters are inserted into the generated message using
+standard printf() format codes.
+
+See jerror.h and jerror.c for further details.
+
+
+Compressed data handling (source and destination managers)
+----------------------------------------------------------
+
+The JPEG compression library sends its compressed data to a "destination
+manager" module.  The default destination manager just writes the data to a
+stdio stream, but you can provide your own manager to do something else.
+Similarly, the decompression library calls a "source manager" to obtain the
+compressed data; you can provide your own source manager if you want the data
+to come from somewhere other than a stdio stream.
+
+In both cases, compressed data is processed a bufferload at a time: the
+destination or source manager provides a work buffer, and the library invokes
+the manager only when the buffer is filled or emptied.  (You could define a
+one-character buffer to force the manager to be invoked for each byte, but
+that would be rather inefficient.)  The buffer's size and location are
+controlled by the manager, not by the library.  For example, if you desired to
+decompress a JPEG datastream that was all in memory, you could just make the
+buffer pointer and length point to the original data in memory.  Then the
+buffer-reload procedure would be invoked only if the decompressor ran off the
+end of the datastream, which would indicate an erroneous datastream.
+
+The work buffer is defined as an array of datatype JOCTET, which is generally
+"char" or "unsigned char".  On a machine where char is not exactly 8 bits
+wide, you must define JOCTET as a wider data type and then modify the data
+source and destination modules to transcribe the work arrays into 8-bit units
+on external storage.
+
+A data destination manager struct contains a pointer and count defining the
+next byte to write in the work buffer and the remaining free space:
+
+	JOCTET * next_output_byte;  /* => next byte to write in buffer */
+	size_t free_in_buffer;      /* # of byte spaces remaining in buffer */
+
+The library increments the pointer and decrements the count until the buffer
+is filled.  The manager's empty_output_buffer method must reset the pointer
+and count.  The manager is expected to remember the buffer's starting address
+and total size in private fields not visible to the library.
+
+A data destination manager provides three methods:
+
+init_destination (j_compress_ptr cinfo)
+	Initialize destination.  This is called by jpeg_start_compress()
+	before any data is actually written.  It must initialize
+	next_output_byte and free_in_buffer.  free_in_buffer must be
+	initialized to a positive value.
+
+empty_output_buffer (j_compress_ptr cinfo)
+	This is called whenever the buffer has filled (free_in_buffer
+	reaches zero).  In typical applications, it should write out the
+	*entire* buffer (use the saved start address and buffer length;
+	ignore the current state of next_output_byte and free_in_buffer).
+	Then reset the pointer & count to the start of the buffer, and
+	return TRUE indicating that the buffer has been dumped.
+	free_in_buffer must be set to a positive value when TRUE is
+	returned.  A FALSE return should only be used when I/O suspension is
+	desired (this operating mode is discussed in the next section).
+
+term_destination (j_compress_ptr cinfo)
+	Terminate destination --- called by jpeg_finish_compress() after all
+	data has been written.  In most applications, this must flush any
+	data remaining in the buffer.  Use either next_output_byte or
+	free_in_buffer to determine how much data is in the buffer.
+
+term_destination() is NOT called by jpeg_abort() or jpeg_destroy().  If you
+want the destination manager to be cleaned up during an abort, you must do it
+yourself.
+
+You will also need code to create a jpeg_destination_mgr struct, fill in its
+method pointers, and insert a pointer to the struct into the "dest" field of
+the JPEG compression object.  This can be done in-line in your setup code if
+you like, but it's probably cleaner to provide a separate routine similar to
+the jpeg_stdio_dest() routine of the supplied destination manager.
+
+Decompression source managers follow a parallel design, but with some
+additional frammishes.  The source manager struct contains a pointer and count
+defining the next byte to read from the work buffer and the number of bytes
+remaining:
+
+	const JOCTET * next_input_byte; /* => next byte to read from buffer */
+	size_t bytes_in_buffer;         /* # of bytes remaining in buffer */
+
+The library increments the pointer and decrements the count until the buffer
+is emptied.  The manager's fill_input_buffer method must reset the pointer and
+count.  In most applications, the manager must remember the buffer's starting
+address and total size in private fields not visible to the library.
+
+A data source manager provides five methods:
+
+init_source (j_decompress_ptr cinfo)
+	Initialize source.  This is called by jpeg_read_header() before any
+	data is actually read.  Unlike init_destination(), it may leave
+	bytes_in_buffer set to 0 (in which case a fill_input_buffer() call
+	will occur immediately).
+
+fill_input_buffer (j_decompress_ptr cinfo)
+	This is called whenever bytes_in_buffer has reached zero and more
+	data is wanted.  In typical applications, it should read fresh data
+	into the buffer (ignoring the current state of next_input_byte and
+	bytes_in_buffer), reset the pointer & count to the start of the
+	buffer, and return TRUE indicating that the buffer has been reloaded.
+	It is not necessary to fill the buffer entirely, only to obtain at
+	least one more byte.  bytes_in_buffer MUST be set to a positive value
+	if TRUE is returned.  A FALSE return should only be used when I/O
+	suspension is desired (this mode is discussed in the next section).
+
+skip_input_data (j_decompress_ptr cinfo, long num_bytes)
+	Skip num_bytes worth of data.  The buffer pointer and count should
+	be advanced over num_bytes input bytes, refilling the buffer as
+	needed.  This is used to skip over a potentially large amount of
+	uninteresting data (such as an APPn marker).  In some applications
+	it may be possible to optimize away the reading of the skipped data,
+	but it's not clear that being smart is worth much trouble; large
+	skips are uncommon.  bytes_in_buffer may be zero on return.
+	A zero or negative skip count should be treated as a no-op.
+
+resync_to_restart (j_decompress_ptr cinfo)
+	This routine is called only when the decompressor has failed to find
+	a restart (RSTn) marker where one is expected.  Its mission is to
+	find a suitable point for resuming decompression.  For most
+	applications, we recommend that you just use the default resync
+	procedure, jpeg_resync_to_restart().  However, if you are able to back
+	up in the input data stream, or if you have a-priori knowledge about
+	the likely location of restart markers, you may be able to do better.
+	Read the read_restart_marker() and jpeg_resync_to_restart() routines
+	in jdmarker.c if you think you'd like to implement your own resync
+	procedure.
+
+term_source (j_decompress_ptr cinfo)
+	Terminate source --- called by jpeg_finish_decompress() after all
+	data has been read.  Often a no-op.
+
+For both fill_input_buffer() and skip_input_data(), there is no such thing
+as an EOF return.  If the end of the file has been reached, the routine has
+a choice of exiting via ERREXIT() or inserting fake data into the buffer.
+In most cases, generating a warning message and inserting a fake EOI marker
+is the best course of action --- this will allow the decompressor to output
+however much of the image is there.  In pathological cases, the decompressor
+may swallow the EOI and again demand data ... just keep feeding it fake EOIs.
+jdatasrc.c illustrates the recommended error recovery behavior.
+
+term_source() is NOT called by jpeg_abort() or jpeg_destroy().  If you want
+the source manager to be cleaned up during an abort, you must do it yourself.
+
+You will also need code to create a jpeg_source_mgr struct, fill in its method
+pointers, and insert a pointer to the struct into the "src" field of the JPEG
+decompression object.  This can be done in-line in your setup code if you
+like, but it's probably cleaner to provide a separate routine similar to the
+jpeg_stdio_src() routine of the supplied source manager.
+
+For more information, consult the stdio source and destination managers
+in jdatasrc.c and jdatadst.c.
+
+
+I/O suspension
+--------------
+
+Some applications need to use the JPEG library as an incremental memory-to-
+memory filter: when the compressed data buffer is filled or emptied, they want
+control to return to the outer loop, rather than expecting that the buffer can
+be flushed or reloaded within the data source/destination manager subroutine.
+The library supports this need by providing an "I/O suspension" mode, which we
+describe in this section.
+
+The I/O suspension mode is a limited solution: it works only in the simplest
+operating modes (namely single-pass processing of single-scan JPEG files), and
+it has several other restrictions which are documented below.  Furthermore,
+nothing is guaranteed about the maximum amount of time spent in any one call
+to the library, so a single-threaded application may still have response-time
+problems.  If you need multi-pass processing or guaranteed response time, we
+suggest you "bite the bullet" and implement a real multi-tasking capability.
+
+To use I/O suspension, cooperation is needed between the calling application
+and the data source or destination manager; you will always need a custom
+source/destination manager.  (Please read the previous section if you haven't
+already.)  The basic idea is that the empty_output_buffer() or
+fill_input_buffer() routine is a no-op, merely returning FALSE to indicate
+that it has done nothing.  Upon seeing this, the JPEG library suspends
+operation and returns to its caller.  The surrounding application is
+responsible for emptying or refilling the work buffer before calling the JPEG
+library again.
+
+Compression suspension:
+
+For compression suspension, use an empty_output_buffer() routine that
+returns FALSE; typically it will not do anything else.  This will cause the
+compressor to return to the caller of jpeg_write_scanlines(), with the
+return value indicating that not all the supplied scanlines have been
+accepted.  The application must make more room in the output buffer, adjust
+the buffer pointer/count appropriately, and then call jpeg_write_scanlines()
+again, pointing to the first unconsumed scanline.
+
+When forced to suspend, the compressor will backtrack to a convenient stopping
+point (usually the start of the current MCU); it will regenerate some output
+data when restarted.  Therefore, although empty_output_buffer() is only called
+when the buffer is filled, you should NOT dump out the entire buffer, only the
+data up to the current position of next_output_byte/free_in_buffer.  The data
+beyond that point will be regenerated after resumption.
+
+Because of the backtracking behavior, a good-size output buffer is essential
+for efficiency; you don't want the compressor to suspend often.  (In fact, an
+overly small buffer could lead to infinite looping, if a single MCU required
+more data than would fit in the buffer.)  We recommend a buffer of at least
+several Kbytes.  You may want to insert explicit code to ensure that you don't
+call jpeg_write_scanlines() unless there is a reasonable amount of space in
+the output buffer; in other words, flush the buffer before trying to compress
+more data.
+
+The JPEG compressor does not support suspension while it is trying to write
+JPEG markers at the beginning and end of the file.  This means that
+  * At the beginning of a compression operation, there must be enough free
+    space in the output buffer to hold the header markers (typically 600 or
+    so bytes).  The recommended buffer size is bigger than this anyway, so
+    this is not a problem as long as you start with an empty buffer.  However,
+    this restriction might catch you if you insert large special markers, such
+    as a JFIF thumbnail image.
+  * When you call jpeg_finish_compress(), there must be enough space in the
+    output buffer to emit any buffered data and the final EOI marker.  In the
+    current implementation, half a dozen bytes should suffice for this, but
+    for safety's sake we recommend ensuring that at least 100 bytes are free
+    before calling jpeg_finish_compress().
+Furthermore, since jpeg_finish_compress() cannot suspend, you cannot request
+multi-pass operating modes such as Huffman code optimization or multiple-scan
+output.  That would imply that a large amount of data would be written inside
+jpeg_finish_compress(), which would certainly trigger a buffer overrun.
+
+Decompression suspension:
+
+For decompression suspension, use a fill_input_buffer() routine that simply
+returns FALSE (except perhaps during error recovery, as discussed below).
+This will cause the decompressor to return to its caller with an indication
+that suspension has occurred.  This can happen at three places:
+  * jpeg_read_header(): will return JPEG_SUSPENDED.
+  * jpeg_read_scanlines(): will return the number of scanlines already
+	completed (possibly 0).
+  * jpeg_finish_decompress(): will return FALSE, rather than its usual TRUE.
+The surrounding application must recognize these cases, load more data into
+the input buffer, and repeat the call.  In the case of jpeg_read_scanlines(),
+adjust the passed pointers to reflect any scanlines successfully read.
+
+Just as with compression, the decompressor will typically backtrack to a
+convenient restart point before suspending.  The data beyond the current
+position of next_input_byte/bytes_in_buffer must NOT be discarded; it will
+be re-read upon resumption.  In most implementations, you'll need to shift
+this data down to the start of your work buffer and then load more data
+after it.  Again, this behavior means that a several-Kbyte work buffer is
+essential for decent performance; furthermore, you should load a reasonable
+amount of new data before resuming decompression.  (If you loaded, say,
+only one new byte each time around, you could waste a LOT of cycles.)
+
+The skip_input_data() source manager routine requires special care in a
+suspension scenario.  This routine is NOT granted the ability to suspend the
+decompressor; it can decrement bytes_in_buffer to zero, but no more.  If the
+requested skip distance exceeds the amount of data currently in the input
+buffer, then skip_input_data() must set bytes_in_buffer to zero and record the
+additional skip distance somewhere else.  The decompressor will immediately
+call fill_input_buffer(), which will return FALSE, which will cause a
+suspension return.  The surrounding application must then arrange to discard
+the right number of bytes before it resumes loading the input buffer.  (Yes,
+this design is rather baroque, but it avoids complexity in the far more common
+case where a non-suspending source manager is used.)
+
+If the input data has been exhausted, we recommend that you emit a warning
+and insert dummy EOI markers just as a non-suspending data source manager
+would do.  This can be handled either in the surrounding application logic or
+within fill_input_buffer(); the latter is probably more efficient.  If
+fill_input_buffer() knows that no more data is available, it can set the
+pointer/count to point to a dummy EOI marker and then return TRUE just as
+though it had read more data in a non-suspending situation.
+
+The decompressor does not support suspension within jpeg_start_decompress().
+This means that you cannot use suspension with any multi-pass processing mode
+(eg, two-pass color quantization or multiple-scan JPEG files).  In single-pass
+modes, jpeg_start_decompress() reads no data and thus need never suspend.
+
+The decompressor does not attempt to suspend within any JPEG marker; it will
+backtrack to the start of the marker.  Hence the input buffer must be large
+enough to hold the longest marker in the file.  We recommend at least a 2K
+buffer.  The buffer would need to be 64K to allow for arbitrary COM or APPn
+markers, but the decompressor does not actually try to read these; it just
+skips them by calling skip_input_data().  If you provide a special marker
+handling routine that does look at such markers, coping with buffer overflow
+is your problem.  Ordinary JPEG markers should normally not exceed a few
+hundred bytes each (DHT tables are typically the longest).  For robustness
+against damaged marker length counts, you may wish to insert a test in your
+application for the case that the input buffer is completely full and yet the
+decoder has suspended without consuming any data --- otherwise, if this
+situation did occur, it would lead to an endless loop.
+
+
+Abbreviated datastreams and multiple images
+-------------------------------------------
+
+A JPEG compression or decompression object can be reused to process multiple
+images.  This saves a small amount of time per image by eliminating the
+"create" and "destroy" operations, but that isn't the real purpose of the
+feature.  Rather, reuse of an object provides support for abbreviated JPEG
+datastreams.  Object reuse can also simplify processing a series of images in
+a single input or output file.  This section explains these features.
+
+A JPEG file normally contains several hundred bytes worth of quantization
+and Huffman tables.  In a situation where many images will be stored or
+transmitted with identical tables, this may represent an annoying overhead.
+The JPEG standard therefore permits tables to be omitted.  The standard
+defines three classes of JPEG datastreams:
+  * "Interchange" datastreams contain an image and all tables needed to decode
+     the image.  These are the usual kind of JPEG file.
+  * "Abbreviated image" datastreams contain an image, but are missing some or
+    all of the tables needed to decode that image.
+  * "Abbreviated table specification" (henceforth "tables-only") datastreams
+    contain only table specifications.
+To decode an abbreviated image, it is necessary to load the missing table(s)
+into the decoder beforehand.  This can be accomplished by reading a separate
+tables-only file.  A variant scheme uses a series of images in which the first
+image is an interchange (complete) datastream, while subsequent ones are
+abbreviated and rely on the tables loaded by the first image.  It is assumed
+that once the decoder has read a table, it will remember that table until a
+new definition for the same table number is encountered.
+
+It is the application designer's responsibility to figure out how to associate
+the correct tables with an abbreviated image.  While abbreviated datastreams
+can be useful in a closed environment, their use is strongly discouraged in
+any situation where data exchange with other applications might be needed.
+Caveat designer.
+
+The JPEG library provides support for reading and writing any combination of
+tables-only datastreams and abbreviated images.  In both compression and
+decompression objects, a quantization or Huffman table will be retained for
+the lifetime of the object, unless it is overwritten by a new table definition.
+
+
+To create abbreviated image datastreams, it is only necessary to tell the
+compressor not to emit some or all of the tables it is using.  Each
+quantization and Huffman table struct contains a boolean field "sent_table",
+which normally is initialized to FALSE.  For each table used by the image, the
+header-writing process emits the table and sets sent_table = TRUE unless it is
+already TRUE.  (In normal usage, this prevents outputting the same table
+definition multiple times, as would otherwise occur because the chroma
+components typically share tables.)  Thus, setting this field to TRUE before
+calling jpeg_start_compress() will prevent the table from being written at
+all.
+
+If you want to create a "pure" abbreviated image file containing no tables,
+just call "jpeg_suppress_tables(&cinfo, TRUE)" after constructing all the
+tables.  If you want to emit some but not all tables, you'll need to set the
+individual sent_table fields directly.
+
+To create an abbreviated image, you must also call jpeg_start_compress()
+with a second parameter of FALSE, not TRUE.  Otherwise jpeg_start_compress()
+will force all the sent_table fields to FALSE.  (This is a safety feature to
+prevent abbreviated images from being created accidentally.)
+
+To create a tables-only file, perform the same parameter setup that you
+normally would, but instead of calling jpeg_start_compress() and so on, call
+jpeg_write_tables(&cinfo).  This will write an abbreviated datastream
+containing only SOI, DQT and/or DHT markers, and EOI.  All the quantization
+and Huffman tables that are currently defined in the compression object will
+be emitted unless their sent_tables flag is already TRUE, and then all the
+sent_tables flags will be set TRUE.
+
+A sure-fire way to create matching tables-only and abbreviated image files
+is to proceed as follows:
+
+	create JPEG compression object
+	set JPEG parameters
+	set destination to tables-only file
+	jpeg_write_tables(&cinfo);
+	set destination to image file
+	jpeg_start_compress(&cinfo, FALSE);
+	write data...
+	jpeg_finish_compress(&cinfo);
+
+Since the JPEG parameters are not altered between writing the table file and
+the abbreviated image file, the same tables are sure to be used.  Of course,
+you can repeat the jpeg_start_compress() ... jpeg_finish_compress() sequence
+many times to produce many abbreviated image files matching the table file.
+
+You cannot suppress output of the computed Huffman tables when Huffman
+optimization is selected.  (If you could, there'd be no way to decode the
+image...)  Generally, you don't want to set optimize_coding = TRUE when
+you are trying to produce abbreviated files.
+
+In some cases you might want to compress an image using tables which are
+not stored in the application, but are defined in an interchange or
+tables-only file readable by the application.  This can be done by setting up
+a JPEG decompression object to read the specification file, then copying the
+tables into your compression object.
+
+
+To read abbreviated image files, you simply need to load the proper tables
+into the decompression object before trying to read the abbreviated image.
+If the proper tables are stored in the application program, you can just
+allocate the table structs and fill in their contents directly.  More commonly
+you'd want to read the tables from a tables-only file.  The jpeg_read_header()
+call is sufficient to read a tables-only file.  You must pass a second
+parameter of FALSE to indicate that you do not require an image to be present.
+Thus, the typical scenario is
+
+	create JPEG decompression object
+	set source to tables-only file
+	jpeg_read_header(&cinfo, FALSE);
+	set source to abbreviated image file
+	jpeg_read_header(&cinfo, TRUE);
+	set decompression parameters
+	jpeg_start_decompress(&cinfo);
+	read data...
+	jpeg_finish_decompress(&cinfo);
+
+In some cases, you may want to read a file without knowing whether it contains
+an image or just tables.  In that case, pass FALSE and check the return value
+from jpeg_read_header(): it will be JPEG_HEADER_OK if an image was found,
+JPEG_HEADER_TABLES_ONLY if only tables were found.  (A third return value,
+JPEG_SUSPENDED, is possible when using a suspending data source manager.)
+Note that jpeg_read_header() will not complain if you read an abbreviated
+image for which you haven't loaded the missing tables; the missing-table check
+occurs in jpeg_start_decompress().
+
+
+It is possible to read a series of images from a single source file by
+repeating the jpeg_read_header() ... jpeg_finish_decompress() sequence,
+without releasing/recreating the JPEG object or the data source module.
+(If you did reinitialize, any partial bufferload left in the data source
+buffer at the end of one image would be discarded, causing you to lose the
+start of the next image.)  When you use this method, stored tables are
+automatically carried forward, so some of the images can be abbreviated images
+that depend on tables from earlier images.
+
+If you intend to write a series of images into a single destination file,
+you might want to make a specialized data destination module that doesn't
+flush the output buffer at term_destination() time.  This would speed things
+up by some trifling amount.  Of course, you'd need to remember to flush the
+buffer after the last image.  You can make the later images be abbreviated
+ones by passing FALSE to jpeg_start_compress().
+
+
+Special markers
+---------------
+
+Some applications may need to insert or extract special data in the JPEG
+datastream.  The JPEG standard provides marker types "COM" (comment) and
+"APP0" through "APP15" (application) to hold application-specific data.
+Unfortunately, the use of these markers is not specified by the standard.
+COM markers are fairly widely used to hold user-supplied text.  The JFIF file
+format spec uses APP0 markers with specified initial strings to hold certain
+data.  Adobe applications use APP14 markers beginning with the string "Adobe"
+for miscellaneous data.  Other APPn markers are rarely seen, but might
+contain almost anything.
+
+If you wish to store user-supplied text, we recommend you use COM markers
+and place readable 7-bit ASCII text in them.  Newline conventions are not
+standardized --- expect to find LF (Unix style), CR/LF (DOS style), or CR
+(Mac style).  A robust COM reader should be able to cope with random binary
+garbage, including nulls, since some applications generate COM markers
+containing non-ASCII junk.  (But yours should not be one of them.)
+
+For program-supplied data, use an APPn marker, and be sure to begin it with an
+identifying string so that you can tell whether the marker is actually yours.
+It's probably best to avoid using APP0 or APP14 for any private markers.
+
+Keep in mind that at most 65533 bytes can be put into one marker, but you
+can have as many markers as you like.
+
+By default, the JPEG compression library will write a JFIF APP0 marker if the
+selected JPEG colorspace is grayscale or YCbCr, or an Adobe APP14 marker if
+the selected colorspace is RGB, CMYK, or YCCK.  You can disable this, but
+we don't recommend it.  The decompression library will recognize JFIF and
+Adobe markers and will set the JPEG colorspace properly when one is found.
+
+You can write special markers immediately following the datastream header by
+calling jpeg_write_marker() after jpeg_start_compress() and before the first
+call to jpeg_write_scanlines().  When you do this, the markers appear after
+the SOI and the JFIF APP0 and Adobe APP14 markers (if written), but before
+all else.  Write the marker type parameter as "JPEG_COM" for COM or
+"JPEG_APP0 + n" for APPn.  (Actually, jpeg_write_marker will let you write
+any marker type, but we don't recommend writing any other kinds of marker.)
+For example, to write a user comment string pointed to by comment_text:
+	jpeg_write_marker(cinfo, JPEG_COM, comment_text, strlen(comment_text));
+Or if you prefer to synthesize the marker byte sequence yourself, you can
+just cram it straight into the data destination module.
+
+For decompression, you can supply your own routine to process COM or APPn
+markers by calling jpeg_set_marker_processor().  Usually you'd call this
+after creating a decompression object and before calling jpeg_read_header(),
+because the markers of interest will normally be scanned by jpeg_read_header.
+Once you've supplied a routine, it will be used for the life of that
+decompression object.  A separate routine may be registered for COM and for
+each APPn marker code.
+
+A marker processor routine must have the signature
+	boolean jpeg_marker_parser_method (j_decompress_ptr cinfo)
+Although the marker code is not explicitly passed, the routine can find it
+in cinfo->unread_marker.  At the time of call, the marker proper has been
+read from the data source module.  The processor routine is responsible for
+reading the marker length word and the remaining parameter bytes, if any.
+Return TRUE to indicate success.  (FALSE should be returned only if you are
+using a suspending data source and it tells you to suspend.  See the standard
+marker processors in jdmarker.c for appropriate coding methods if you need to
+use a suspending data source.)
+
+If you override the default APP0 or APP14 processors, it is up to you to
+recognize JFIF and Adobe markers if you want colorspace recognition to occur
+properly.  We recommend copying and extending the default processors if you
+want to do that.
+
+A simple example of an external COM processor can be found in djpeg.c.
+
+
+Raw (downsampled) image data
+----------------------------
+
+Some applications need to supply already-downsampled image data to the JPEG
+compressor, or to receive raw downsampled data from the decompressor.  The
+library supports this requirement by allowing the application to write or
+read raw data, bypassing the normal preprocessing or postprocessing steps.
+The interface is different from the standard one and is somewhat harder to
+use.  If your interest is merely in bypassing color conversion, we recommend
+that you use the standard interface and simply set jpeg_color_space =
+in_color_space (or jpeg_color_space = out_color_space for decompression).
+The mechanism described in this section is necessary only to supply or
+receive downsampled image data, in which not all components have the same
+dimensions.
+
+
+To compress raw data, you must supply the data in the colorspace to be used
+in the JPEG file (please read the earlier section on Special color spaces)
+and downsampled to the sampling factors specified in the JPEG parameters.
+You must supply the data in the format used internally by the JPEG library,
+namely a JSAMPIMAGE array.  This is an array of pointers to two-dimensional
+arrays, each of type JSAMPARRAY.  Each 2-D array holds the values for one
+color component.  This structure is necessary since the components are of
+different sizes.  If the image dimensions are not a multiple of the MCU size,
+you must also pad the data correctly (usually, this is done by replicating
+the last column and/or row).  The data must be padded to a multiple of a DCT
+block in each component: that is, each downsampled row must contain a
+multiple of 8 valid samples, and there must be a multiple of 8 sample rows
+for each component.  (For applications such as conversion of digital TV
+images, the standard image size is usually a multiple of the DCT block size,
+so that no padding need actually be done.)
+
+The procedure for compression of raw data is basically the same as normal
+compression, except that you call jpeg_write_raw_data() in place of
+jpeg_write_scanlines().  Before calling jpeg_start_compress(), you must do
+the following:
+  * Set cinfo->raw_data_in to TRUE.  (It is set FALSE by jpeg_set_defaults().)
+    This notifies the library that you will be supplying raw data.
+  * Ensure jpeg_color_space is correct --- an explicit jpeg_set_colorspace()
+    call is a good idea.  Note that since color conversion is bypassed,
+    in_color_space is ignored, except that jpeg_set_defaults() uses it to
+    choose the default jpeg_color_space setting.
+  * Ensure the sampling factors, cinfo->comp_info[i].h_samp_factor and
+    cinfo->comp_info[i].v_samp_factor, are correct.  Since these indicate the
+    dimensions of the data you are supplying, it's wise to set them
+    explicitly, rather than assuming the library's defaults are what you want.
+
+To pass raw data to the library, call jpeg_write_raw_data() in place of
+jpeg_write_scanlines().  The two routines work similarly except that
+jpeg_write_raw_data takes a JSAMPIMAGE data array rather than JSAMPARRAY.
+The scanlines count passed to and returned from jpeg_write_raw_data is
+measured in terms of the component with the largest v_samp_factor.
+
+jpeg_write_raw_data() processes one MCU row per call, which is to say
+v_samp_factor*DCTSIZE sample rows of each component.  The passed num_lines
+value must be at least max_v_samp_factor*DCTSIZE, and the return value will
+be exactly that amount (or possibly some multiple of that amount, in future
+library versions).  This is true even on the last call at the bottom of the
+image; don't forget to pad your data as necessary.
+
+The required dimensions of the supplied data can be computed for each
+component as
+	cinfo->comp_info[i].width_in_blocks*DCTSIZE  samples per row
+	cinfo->comp_info[i].height_in_blocks*DCTSIZE rows in image
+after jpeg_start_compress() has initialized those fields.  If the valid data
+is smaller than this, it must be padded appropriately.  For some sampling
+factors and image sizes, additional dummy DCT blocks are inserted to make
+the image a multiple of the MCU dimensions.  The library creates such dummy
+blocks itself; it does not read them from your supplied data.  Therefore you
+need never pad by more than DCTSIZE samples.  An example may help here.
+Assume 2h2v downsampling of YCbCr data, that is
+	cinfo->comp_info[0].h_samp_factor = 2		for Y
+	cinfo->comp_info[0].v_samp_factor = 2
+	cinfo->comp_info[1].h_samp_factor = 1		for Cb
+	cinfo->comp_info[1].v_samp_factor = 1
+	cinfo->comp_info[2].h_samp_factor = 1		for Cr
+	cinfo->comp_info[2].v_samp_factor = 1
+and suppose that the nominal image dimensions (cinfo->image_width and
+cinfo->image_height) are 101x101 pixels.  Then jpeg_start_compress() will
+compute downsampled_width = 101 and width_in_blocks = 13 for Y,
+downsampled_width = 51 and width_in_blocks = 7 for Cb and Cr (and the same
+for the height fields).  You must pad the Y data to at least 13*8 = 104
+columns and rows, the Cb/Cr data to at least 7*8 = 56 columns and rows.  The
+MCU height is max_v_samp_factor = 2 DCT rows so you must pass at least 16
+scanlines on each call to jpeg_write_raw_data(), which is to say 16 actual
+sample rows of Y and 8 each of Cb and Cr.  A total of 7 MCU rows are needed,
+so you must pass a total of 7*16 = 112 "scanlines".  The last DCT block row
+of Y data is dummy, so it doesn't matter what you pass for it in the data
+arrays, but the scanlines count must total up to 112 so that all of the Cb
+and Cr data gets passed.
+
+Currently, output suspension is not supported with raw data output: an error
+will result if the data destination module tries to suspend.
+
+
+Decompression with raw data output implies bypassing all postprocessing:
+you cannot ask for color quantization, for instance.  More seriously, you must
+deal with the color space and sampling factors present in the incoming file.
+If your application only handles, say, 2h1v YCbCr data, you must check for
+and fail on other color spaces or other sampling factors.
+
+To obtain raw data output, set cinfo->raw_data_out = TRUE before
+jpeg_start_decompress() (it is set FALSE by jpeg_read_header()).  Be sure to
+verify that the color space and sampling factors are ones you can handle.
+Then call jpeg_read_raw_data() in place of jpeg_read_scanlines().  The
+decompression process is otherwise the same as usual.
+
+jpeg_read_raw_data() returns one MCU row per call, and thus you must pass a
+buffer of at least max_v_samp_factor*DCTSIZE scanlines (scanline counting is
+the same as for raw-data compression).  The buffer you pass must be large
+enough to hold the actual data plus padding to DCT-block boundaries.  As with
+compression, any entirely dummy DCT blocks are not processed so you need not
+allocate space for them, but the total scanline count includes them.  The
+above example of computing buffer dimensions for raw-data compression is
+equally valid for decompression.
+
+Input suspension is supported with raw-data decompression: if the data source
+module suspends, jpeg_read_raw_data() will return 0.
+
+
+Progress monitoring
+-------------------
+
+Some applications may need to regain control from the JPEG library every so
+often.  The typical use of this feature is to produce a percent-done bar or
+other progress display.  (For a simple example, see cjpeg.c or djpeg.c.)
+Although you do get control back frequently during the data-transferring pass
+(the jpeg_read_scanlines or jpeg_write_scanlines loop), any additional passes
+will occur inside jpeg_finish_compress or jpeg_start_decompress; those
+routines may take a long time to execute, and you don't get control back
+until they are done.
+
+You can define a progress-monitor routine which will be called periodically
+by the library.  No guarantees are made about how often this call will occur,
+so we don't recommend you use it for mouse tracking or anything like that.
+At present, a call will occur once per MCU row, scanline, or sample row
+group, whichever unit is convenient for the current processing mode; so the
+wider the image, the longer the time between calls.  (During the data
+transferring pass, only one call occurs per call of jpeg_read_scanlines or
+jpeg_write_scanlines, so don't pass a large number of scanlines at once if
+you want fine resolution in the progress count.)
+
+To establish a progress-monitor callback, create a struct jpeg_progress_mgr,
+fill in its progress_monitor field with a pointer to your callback routine,
+and set cinfo->progress to point to the struct.  The callback will be called
+whenever cinfo->progress is non-NULL.  (This pointer is set to NULL by
+jpeg_create_compress or jpeg_create_decompress; the library will not change
+it thereafter.  So if you allocate dynamic storage for the progress struct,
+make sure it will live as long as the JPEG object does.  Allocating from the
+JPEG memory manager with lifetime JPOOL_PERMANENT will work nicely.)  You
+can use the same callback routine for both compression and decompression.
+
+The jpeg_progress_mgr struct contains four fields which are set by the library:
+	long pass_counter;	/* work units completed in this pass */
+	long pass_limit;	/* total number of work units in this pass */
+	int completed_passes;	/* passes completed so far */
+	int total_passes;	/* total number of passes expected */
+During any one pass, pass_counter increases from 0 up to (not including)
+pass_limit; the step size is not necessarily 1.  Both the step size and the
+limit may differ from one pass to another.  The expected total number of
+passes is in total_passes, and the number of passes already completed is in
+completed_passes.  Thus the fraction of work completed may be estimated as
+		completed_passes + (pass_counter/pass_limit)
+		--------------------------------------------
+				total_passes
+ignoring the fact that the passes may not be equal amounts of work.
+
+When decompressing, the total_passes value is not trustworthy, because it
+depends on the number of scans in the JPEG file, which isn't always known in
+advance.  In the current implementation, completed_passes may jump by more
+than one when dealing with a multiple-scan input file.  About all that is
+really safe to assume is that when completed_passes = total_passes - 1, the
+current pass will be the last one.
+
+If you really need to use the callback mechanism for time-critical tasks
+like mouse tracking, you could insert additional calls inside some of the
+library's inner loops.
+
+
+Memory management
+-----------------
+
+This section covers some key facts about the JPEG library's built-in memory
+manager.  For more info, please read structure.doc's section about the memory
+manager, and consult the source code if necessary.
+
+All memory and temporary file allocation within the library is done via the
+memory manager.  If necessary, you can replace the "back end" of the memory
+manager to control allocation yourself (for example, if you don't want the
+library to use malloc() and free() for some reason).
+
+Some data is allocated "permanently" and will not be freed until the JPEG
+object is destroyed.  Most data is allocated "per image" and is freed by
+jpeg_finish_compress, jpeg_finish_decompress, or jpeg_abort.  You can call the
+memory manager yourself to allocate structures that will automatically be
+freed at these times.  Typical code for this is
+  ptr = (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, size);
+Use JPOOL_PERMANENT to get storage that lasts as long as the JPEG object.
+Use alloc_large instead of alloc_small for anything bigger than a few Kbytes.
+There are also alloc_sarray and alloc_barray routines that automatically
+build 2-D sample or block arrays.
+
+The library's minimum space requirements to process an image depend on the
+image's width, but not on its height, because the library ordinarily works
+with "strip" buffers that are as wide as the image but just a few rows high.
+Some operating modes (eg, two-pass color quantization) require full-image
+buffers.  Such buffers are treated as "virtual arrays": only the current strip
+need be in memory, and the rest can be swapped out to a temporary file.
+
+If you use the simplest memory manager back end (jmemnobs.c), then no
+temporary files are used; virtual arrays are simply malloc()'d.  Images bigger
+than memory can be processed only if your system supports virtual memory.
+The other memory manager back ends support temporary files of various flavors
+and thus work in machines without virtual memory.  They may also be useful on
+Unix machines if you need to process images that exceed available swap space.
+
+When using temporary files, the library will make the in-memory buffers for
+its virtual arrays just big enough to stay within a "maximum memory" setting.
+Your application can set this limit by setting cinfo->mem->max_memory_to_use
+after creating the JPEG object.  (Of course, there is still a minimum size for
+the buffers, so the max-memory setting is effective only if it is bigger than
+the minimum space needed.)  If you allocate any large structures yourself, you
+must allocate them before jpeg_start_compress() or jpeg_start_decompress() in
+order to have them counted against the max memory limit.  Also keep in mind
+that space allocated with alloc_small() is ignored, on the assumption that
+it's too small to be worth worrying about.
+
+If you use the jmemname.c or jmemdos.c memory manager back end, it is
+important to clean up the JPEG object properly to ensure that the temporary
+files get deleted.  (This is especially crucial with jmemdos.c, where the
+"temporary files" may be extended-memory segments; if they are not freed,
+DOS will require a reboot to recover the memory.)  Thus, with these memory
+managers, it's a good idea to provide a signal handler that will trap any
+early exit from your program.  The handler should call either jpeg_abort()
+or jpeg_destroy() for any active JPEG objects.  A handler is not needed with
+jmemnobs.c, and shouldn't be necessary with jmemansi.c either, since the C
+library is supposed to take care of deleting files made with tmpfile().
+
+
+Library compile-time options
+----------------------------
+
+A number of compile-time options are available by modifying jmorecfg.h.
+
+The JPEG standard provides for both the baseline 8-bit DCT process and
+a 12-bit DCT process.  12-bit lossy JPEG is supported if you define
+BITS_IN_JSAMPLE as 12 rather than 8.  Note that this causes JSAMPLE to be
+larger than a char, so it affects the surrounding application's image data.
+At present, a 12-bit library can handle *only* 12-bit images, not both
+precisions.  (If you need to include both 8- and 12-bit libraries in a
+single application, you could probably do it by defining
+NEED_SHORT_EXTERNAL_NAMES for just one of the copies.  You'd have to access
+the 8-bit and 12-bit copies from separate application source files.  This is
+untested ... if you try it, we'd like to hear whether it works!)
+
+The maximum number of components (color channels) in the image is determined
+by MAX_COMPONENTS.  The JPEG standard allows up to 255 components, but we
+expect that few applications will need more than four or so.
+
+On machines with unusual data type sizes, you may be able to improve
+performance or reduce memory space by tweaking the various typedefs in
+jmorecfg.h.  In particular, on some RISC CPUs, access to arrays of "short"s
+is quite slow; consider trading memory for speed by making JCOEF, INT16, and
+UINT16 be "int" or "unsigned int".  UINT8 is also a candidate to become int.
+You probably don't want to make JSAMPLE be int unless you have lots of memory
+to burn.
+
+You can reduce the size of the library by compiling out various optional
+functions.  To do this, undefine xxx_SUPPORTED symbols as necessary.
+
+
+Portability considerations
+--------------------------
+
+The JPEG library has been written to be extremely portable; the sample
+applications cjpeg and djpeg are slightly less so.  This section summarizes
+the design goals in this area.  (If you encounter any bugs that cause the
+library to be less portable than is claimed here, we'd appreciate hearing
+about them.)
+
+The code works fine on both ANSI and pre-ANSI C compilers, using any of the
+popular system include file setups, and some not-so-popular ones too.  See
+install.doc for configuration procedures.
+
+The code is not dependent on the exact sizes of the C data types.  As
+distributed, we make the assumptions that
+	char	is at least 8 bits wide
+	short	is at least 16 bits wide
+	int	is at least 16 bits wide
+	long	is at least 32 bits wide
+(These are the minimum requirements of the ANSI C standard.)  Wider types will
+work fine, although memory may be used inefficiently if char is much larger
+than 8 bits or short is much bigger than 16 bits.  The code should work
+equally well with 16- or 32-bit ints.
+
+In a system where these assumptions are not met, you may be able to make the
+code work by modifying the typedefs in jmorecfg.h.  However, you will probably
+have difficulty if int is less than 16 bits wide, since references to plain
+int abound in the code.
+
+char can be either signed or unsigned, although the code runs faster if an
+unsigned char type is available.  If char is wider than 8 bits, you will need
+to redefine JOCTET and/or provide custom data source/destination managers so
+that JOCTET represents exactly 8 bits of data on external storage.
+
+The JPEG library proper does not assume ASCII representation of characters.
+But some of the image file I/O modules in cjpeg/djpeg do have ASCII
+dependencies in file-header manipulation; so does cjpeg's select_file_type()
+routine.
+
+The JPEG library does not rely heavily on the C library.  In particular, C
+stdio is used only by the data source/destination modules and the error
+handler, all of which are application-replaceable.  (cjpeg/djpeg are more
+heavily dependent on stdio.)  malloc and free are called only from the memory
+manager "back end" module, so you can use a different memory allocator by
+replacing that one file.
+
+The code generally assumes that C names must be unique in the first 15
+characters.  However, global function names can be made unique in the
+first 6 characters by defining NEED_SHORT_EXTERNAL_NAMES.
+
+More info about porting the code may be gleaned by reading jconfig.doc,
+jmorecfg.h, and jinclude.h.
+
+
+Notes for MS-DOS implementors
+-----------------------------
+
+The IJG code is designed to work efficiently in 80x86 "small" or "medium"
+memory models (i.e., data pointers are 16 bits unless explicitly declared
+"far"; code pointers can be either size).  You may be able to use small
+model to compile cjpeg or djpeg by itself, but you will probably have to use
+medium model for any larger application.  This won't make much difference in
+performance.  You *will* take a noticeable performance hit if you use a
+large-data memory model (perhaps 10%-25%), and you should avoid "huge" model
+if at all possible.
+
+The JPEG library typically needs 2Kb-3Kb of stack space.  It will also
+malloc about 20K-30K of near heap space while executing (and lots of far
+heap, but that doesn't count in this calculation).  This figure will vary
+depending on selected operating mode, and to a lesser extent on image size.
+Thus you have perhaps 25K available for static data and other modules' near
+heap space before you need to go to a larger memory model.  The C library's
+static data will account for several K of this, but that still leaves a good
+deal for your needs.  (If you are tight on space, you could reduce the sizes
+of the I/O buffers allocated by jdatasrc.c and jdatadst.c, say from 4K to
+1K.)
+
+About 2K of the near heap space is "permanent" memory that will not be
+released until you destroy the JPEG object.  This is only an issue if you
+save a JPEG object between compression or decompression operations.
+
+Far data space may also be a tight resource when you are dealing with large
+images.  The most memory-intensive case is decompression with two-pass color
+quantization, or single-pass quantization to an externally supplied color
+map.  This requires a 128Kb color lookup table plus strip buffers amounting
+to about 50 bytes per column for typical sampling ratios (eg, about 32000
+bytes for a 640-pixel-wide image).  You may not be able to process wide
+images if you have large data structures of your own.
+
+Of course, all of these concerns vanish if you use a 32-bit flat-memory-model
+compiler, such as DJGPP or Watcom C.  We highly recommend flat model if you
+can use it; the JPEG library is significantly faster in flat model.
diff --git a/makcjpeg.st b/makcjpeg.st
index 4819efb..f524a8f 100644
--- a/makcjpeg.st
+++ b/makcjpeg.st
@@ -6,32 +6,30 @@
 ;
 ; To use this file, rename it to CJPEG.PRJ.
 ; If you are using Turbo C, change filenames beginning with "PC..." to "TC..."
-; Read SETUP instructions before trying to make the program!
+; Read installation instructions before trying to make the program!
 ;
 ;
 ;      * * * Output file * * *
 cjpeg.ttp
 ;
-;      * * * DEFINES * * * 
-.C[-DHAVE_STDC]
-.C[-DINCLUDES_ARE_ANSI]
-.C[-DNEED_SIGNAL_CATCHER]  ; It's safer to have a signal catcher.
-.C[-DTWO_FILE_COMMANDLINE] ; If you like the UNIX - style with
-                           ; I/O redirection, comment this line out.
-.C[-DPROGRESS_REPORT]      ; This enables the % progress indicator
-;
 ; * * * COMPILER OPTIONS * * *  
 .C[-P]        ; absolute calls
 .C[-M]        ; and no string merging, folks
-.C[-w-par]    ; no "parameter xxxx unused"
-.C[-DINCOMPLETE_TYPES_BROKEN] ; suppress warnings about undefined structures
-.C[-wsig]     ; warn if significant digits may be lost
 .C[-w-cln]    ; no "constant is long" warnings
+.C[-w-par]    ; no "parameter xxxx unused"
+.C[-w-rch]    ; no "unreachable code"
+.C[-wsig]     ; warn if significant digits may be lost
 =
 ; * * * * List of modules * * * * 
 PCSTART.O
-jcmain.c		(jinclude.h,jconfig.h,jpegdata.h,jversion.h)
-JPEG.LIB           ; built by LIB_JPEG.PRJ
-;PCFLTLIB.LIB      ; floating point library -- you shouldn't need this
+cjpeg.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h,jversion.h)
+rdppm.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdgif.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdtarga.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdbmp.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+rdrle.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+LIBJPEG.LIB        ; built by LIBJPEG.PRJ
+PCFLTLIB.LIB       ; floating point library
+; the float library can be omitted if you've turned off DCT_FLOAT_SUPPORTED
 PCSTDLIB.LIB       ; standard library
 PCEXTLIB.LIB       ; extended library
diff --git a/makdjpeg.st b/makdjpeg.st
index 7c407e6..2014153 100644
--- a/makdjpeg.st
+++ b/makdjpeg.st
@@ -6,32 +6,31 @@
 ;
 ; To use this file, rename it to DJPEG.PRJ.
 ; If you are using Turbo C, change filenames beginning with "PC..." to "TC..."
-; Read SETUP instructions before trying to make the program!
+; Read installation instructions before trying to make the program!
 ;
 ;
 ;      * * * Output file * * *
 djpeg.ttp
 ;
-;      * * * DEFINES * * * 
-.C[-DHAVE_STDC]
-.C[-DINCLUDES_ARE_ANSI]
-.C[-DNEED_SIGNAL_CATCHER]  ; It's safer to have a signal catcher.
-.C[-DTWO_FILE_COMMANDLINE] ; If you like the UNIX - style with
-                           ; I/O redirection, comment this line out.
-.C[-DPROGRESS_REPORT]      ; This enables the % progress indicator
-;
 ; * * * COMPILER OPTIONS * * *  
 .C[-P]        ; absolute calls
 .C[-M]        ; and no string merging, folks
-.C[-w-par]    ; no "parameter xxxx unused"
-.C[-DINCOMPLETE_TYPES_BROKEN] ; suppress warnings about undefined structures
-.C[-wsig]     ; warn if significant digits may be lost
 .C[-w-cln]    ; no "constant is long" warnings
+.C[-w-par]    ; no "parameter xxxx unused"
+.C[-w-rch]    ; no "unreachable code"
+.C[-wsig]     ; warn if significant digits may be lost
 =
 ; * * * * List of modules * * * * 
 PCSTART.O
-jdmain.c		(jinclude.h,jconfig.h,jpegdata.h,jversion.h)
-JPEG.LIB           ; built by LIB_JPEG.PRJ
-;PCFLTLIB.LIB      ; floating point library -- you shouldn't need this
+djpeg.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h,jversion.h)
+rdcolmap.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrppm.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrgif.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrtarga.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrbmp.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+wrrle.c	(cdjpeg.h,jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h,cderror.h)
+LIBJPEG.LIB        ; built by LIBJPEG.PRJ
+PCFLTLIB.LIB       ; floating point library
+; the float library can be omitted if you've turned off DCT_FLOAT_SUPPORTED
 PCSTDLIB.LIB       ; standard library
 PCEXTLIB.LIB       ; extended library
diff --git a/makefile.ansi b/makefile.ansi
index 86e3118..b69aca2 100644
--- a/makefile.ansi
+++ b/makefile.ansi
@@ -3,28 +3,27 @@
 # This makefile is suitable for Unix-like systems with ANSI-capable compilers.
 # If you have a non-ANSI compiler, makefile.unix is a better starting point.
 
-# Read SETUP instructions before saying "make" !!
+# Read installation instructions before saying "make" !!
 
 # The name of your C compiler:
 CC= cc
 
 # You may need to adjust these cc options:
 CFLAGS= -O
-# In particular:
-#   Add -DBSD if on a pure BSD system (see jinclude.h).
-#   Add -DMEM_STATS to enable gathering of memory usage statistics.
-#   You may also want to add -DTWO_FILE_COMMANDLINE or -D switches for other
-#   symbols listed in jconfig.h, if you prefer not to change jconfig.h.
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
 
 # Link-time cc options:
 LDFLAGS= 
 
 # To link any special libraries, add the necessary -l commands here.
-# In particular, on some versions of HP-UX (and probably other SysV-derived
-# systems) there is a faster alternate malloc(3) library that you can use
-# by adding "-lmalloc" to this line.
 LDLIBS= 
 
+# Put here the object file name for the correct system-dependent memory
+# manager file.  For Unix this is usually jmemnobs.o, but you may want
+# to use jmemansi.o or jmemname.o if you have limited swap space.
+SYSDEPMEM= jmemnobs.o
+
 # miscellaneous OS-dependent stuff
 # linker
 LN= $(CC)
@@ -35,123 +34,145 @@
 # second step in .a creation (use "touch" if not needed)
 AR2= ranlib
 
+# End of configurable options.
 
-# source files (independently compilable files)
-SOURCES= jbsmooth.c jcarith.c jccolor.c jcdeflts.c jcexpand.c jchuff.c \
-        jcmain.c jcmaster.c jcmcu.c jcpipe.c jcsample.c jdarith.c jdcolor.c \
-        jddeflts.c jdhuff.c jdmain.c jdmaster.c jdmcu.c jdpipe.c jdsample.c \
-        jerror.c jquant1.c jquant2.c jfwddct.c jrevdct.c jutils.c jmemmgr.c \
-        jrdjfif.c jrdgif.c jrdppm.c jrdrle.c jrdtarga.c jwrjfif.c jwrgif.c \
-        jwrppm.c jwrrle.c jwrtarga.c
-# virtual source files (not present in distribution file, see SETUP)
-VIRTSOURCES= jmemsys.c
-# system-dependent implementations of virtual source files
-SYSDEPFILES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemdos.h \
-        jmemdosa.asm
+
+# source files: JPEG library proper
+LIBSOURCES= jcapi.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcmainct.c \
+        jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c \
+        jdapi.c jdatasrc.c jdatadst.c jdcoefct.c jdcolor.c jddctmgr.c \
+        jdhuff.c jdmainct.c jdmarker.c jdmaster.c jdpostct.c jdsample.c \
+        jerror.c jutils.c jfdctfst.c jfdctflt.c jfdctint.c jidctfst.c \
+        jidctflt.c jidctint.c jidctred.c jquant1.c jquant2.c jdmerge.c \
+        jmemmgr.c jmemansi.c jmemname.c jmemnobs.c jmemdos.c
+# source files: cjpeg/djpeg applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c rdcolmap.c rdppm.c wrppm.c rdgif.c wrgif.c \
+        rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c rdjpgcom.c \
+        wrjpgcom.c
+SOURCES= $(LIBSOURCES) $(APPSOURCES)
 # files included by source files
-INCLUDES= jinclude.h jconfig.h jpegdata.h jversion.h jmemsys.h
+INCLUDES= jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h jpegint.h \
+        jpeglib.h jversion.h cdjpeg.h cderror.h
 # documentation, test, and support files
-DOCS= README SETUP USAGE CHANGELOG cjpeg.1 djpeg.1 architecture codingrules
-MAKEFILES= makefile.ansi makefile.unix makefile.manx makefile.sas \
-        makcjpeg.st makdjpeg.st makljpeg.st makefile.mc5 makefile.mc6 \
-        makefile.bcc makefile.icc makljpeg.icc makefile.mms makefile.vms \
-        makvms.opt
-OTHERFILES= ansi2knr.c ckconfig.c example.c
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 rdjpgcom.1 wrjpgcom.1 \
+        example.c libjpeg.doc structure.doc coderules.doc filelist.doc \
+        change.log
+MKFILES= configure makefile.auto makefile.ansi makefile.unix makefile.manx \
+        makefile.sas makcjpeg.st makdjpeg.st makljpeg.st makefile.bcc \
+        makefile.mc6 makefile.dj makefile.mms makefile.vms makvms.opt
+CONFIGFILES= jconfig.auto jconfig.manx jconfig.sas jconfig.st jconfig.bcc \
+        jconfig.mc6 jconfig.dj jconfig.vms
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
 TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
-DISTFILES= $(DOCS) $(MAKEFILES) $(SOURCES) $(SYSDEPFILES) $(INCLUDES) \
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
         $(OTHERFILES) $(TESTFILES)
-# objectfiles common to cjpeg and djpeg
-COMOBJECTS= jutils.o jerror.o jmemmgr.o jmemsys.o
-# compression objectfiles
-CLIBOBJECTS= jcmaster.o jcdeflts.o jcarith.o jccolor.o jcexpand.o jchuff.o \
-        jcmcu.o jcpipe.o jcsample.o jfwddct.o jwrjfif.o jrdgif.o jrdppm.o \
-        jrdrle.o jrdtarga.o
-COBJECTS= jcmain.o $(CLIBOBJECTS) $(COMOBJECTS)
-# decompression objectfiles
-DLIBOBJECTS= jdmaster.o jddeflts.o jbsmooth.o jdarith.o jdcolor.o jdhuff.o \
-        jdmcu.o jdpipe.o jdsample.o jquant1.o jquant2.o jrevdct.o jrdjfif.o \
-        jwrgif.o jwrppm.o jwrrle.o jwrtarga.o
-DOBJECTS= jdmain.o $(DLIBOBJECTS) $(COMOBJECTS)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapi.o jcparam.o jdatadst.o jcmaster.o jcmarker.o jcmainct.o \
+        jcprepct.o jccoefct.o jccolor.o jcsample.o jchuff.o jcdctmgr.o \
+        jfdctfst.o jfdctflt.o jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapi.o jdatasrc.o jdmaster.o jdmarker.o jdmainct.o jdcoefct.o \
+        jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+        jdhuff.o jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
 # These objectfiles are included in libjpeg.a
 LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for cjpeg and djpeg applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o
 
 
-all: cjpeg djpeg
-# By default, libjpeg.a is not built unless you explicitly request it.
-# You can add libjpeg.a to the line above if you want it built by default.
+all: libjpeg.a cjpeg djpeg rdjpgcom wrjpgcom
 
-
-cjpeg: $(COBJECTS)
-	$(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) $(LDLIBS)
-
-djpeg: $(DOBJECTS)
-	$(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) $(LDLIBS)
-
-# libjpeg.a is useful if you are including the JPEG software in a larger
-# program; you'd include it in your link, rather than the individual modules.
 libjpeg.a: $(LIBOBJECTS)
 	$(RM) libjpeg.a
 	$(AR) libjpeg.a  $(LIBOBJECTS)
 	$(AR2) libjpeg.a
 
-jmemsys.c:
-	echo You must select a system-dependent jmemsys.c file.
-	echo Please read the SETUP directions.
+cjpeg: $(COBJECTS) libjpeg.a
+	$(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.a $(LDLIBS)
+
+djpeg: $(DOBJECTS) libjpeg.a
+	$(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.a $(LDLIBS)
+
+rdjpgcom: rdjpgcom.o
+	$(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.o $(LDLIBS)
+
+wrjpgcom: wrjpgcom.o
+	$(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.o $(LDLIBS)
+
+jconfig.h: jconfig.doc
+	echo You must prepare a system-dependent jconfig.h file.
+	echo Please read the installation directions in install.doc.
 	exit 1
 
 clean:
-	$(RM) *.o cjpeg djpeg libjpeg.a core testout.*
-
-distribute:
-	$(RM) jpegsrc.tar*
-	tar cvf jpegsrc.tar $(DISTFILES)
-	compress -v jpegsrc.tar
+	$(RM) *.o cjpeg djpeg libjpeg.a rdjpgcom wrjpgcom core testout.*
 
 test: cjpeg djpeg
 	$(RM) testout.ppm testout.gif testout.jpg
-	./djpeg testorig.jpg >testout.ppm
-	./djpeg -gif testorig.jpg >testout.gif
-	./cjpeg testimg.ppm >testout.jpg
+	./djpeg -dct int -ppm -outfile testout.ppm  testorig.jpg
+	./djpeg -dct int -gif -outfile testout.gif  testorig.jpg
+	./cjpeg -dct int -outfile testout.jpg  testimg.ppm
 	cmp testimg.ppm testout.ppm
 	cmp testimg.gif testout.gif
 	cmp testimg.jpg testout.jpg
 
 
-jbsmooth.o : jbsmooth.c jinclude.h jconfig.h jpegdata.h 
-jcarith.o : jcarith.c jinclude.h jconfig.h jpegdata.h 
-jccolor.o : jccolor.c jinclude.h jconfig.h jpegdata.h 
-jcdeflts.o : jcdeflts.c jinclude.h jconfig.h jpegdata.h 
-jcexpand.o : jcexpand.c jinclude.h jconfig.h jpegdata.h 
-jchuff.o : jchuff.c jinclude.h jconfig.h jpegdata.h 
-jcmain.o : jcmain.c jinclude.h jconfig.h jpegdata.h jversion.h 
-jcmaster.o : jcmaster.c jinclude.h jconfig.h jpegdata.h 
-jcmcu.o : jcmcu.c jinclude.h jconfig.h jpegdata.h 
-jcpipe.o : jcpipe.c jinclude.h jconfig.h jpegdata.h 
-jcsample.o : jcsample.c jinclude.h jconfig.h jpegdata.h 
-jdarith.o : jdarith.c jinclude.h jconfig.h jpegdata.h 
-jdcolor.o : jdcolor.c jinclude.h jconfig.h jpegdata.h 
-jddeflts.o : jddeflts.c jinclude.h jconfig.h jpegdata.h 
-jdhuff.o : jdhuff.c jinclude.h jconfig.h jpegdata.h 
-jdmain.o : jdmain.c jinclude.h jconfig.h jpegdata.h jversion.h 
-jdmaster.o : jdmaster.c jinclude.h jconfig.h jpegdata.h 
-jdmcu.o : jdmcu.c jinclude.h jconfig.h jpegdata.h 
-jdpipe.o : jdpipe.c jinclude.h jconfig.h jpegdata.h 
-jdsample.o : jdsample.c jinclude.h jconfig.h jpegdata.h 
-jerror.o : jerror.c jinclude.h jconfig.h jpegdata.h 
-jquant1.o : jquant1.c jinclude.h jconfig.h jpegdata.h 
-jquant2.o : jquant2.c jinclude.h jconfig.h jpegdata.h 
-jfwddct.o : jfwddct.c jinclude.h jconfig.h jpegdata.h 
-jrevdct.o : jrevdct.c jinclude.h jconfig.h jpegdata.h 
-jutils.o : jutils.c jinclude.h jconfig.h jpegdata.h 
-jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpegdata.h jmemsys.h 
-jrdjfif.o : jrdjfif.c jinclude.h jconfig.h jpegdata.h 
-jrdgif.o : jrdgif.c jinclude.h jconfig.h jpegdata.h 
-jrdppm.o : jrdppm.c jinclude.h jconfig.h jpegdata.h 
-jrdrle.o : jrdrle.c jinclude.h jconfig.h jpegdata.h 
-jrdtarga.o : jrdtarga.c jinclude.h jconfig.h jpegdata.h 
-jwrjfif.o : jwrjfif.c jinclude.h jconfig.h jpegdata.h 
-jwrgif.o : jwrgif.c jinclude.h jconfig.h jpegdata.h 
-jwrppm.o : jwrppm.c jinclude.h jconfig.h jpegdata.h 
-jwrrle.o : jwrrle.c jinclude.h jconfig.h jpegdata.h 
-jwrtarga.o : jwrtarga.c jinclude.h jconfig.h jpegdata.h 
-jmemsys.o : jmemsys.c jinclude.h jconfig.h jpegdata.h jmemsys.h 
+jcapi.o : jcapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcprepct.o : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapi.o : jdapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatasrc.o : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatadst.o : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdpostct.o : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jutils.o : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jfdctfst.o : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctflt.o : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+rdcolmap.o : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdppm.o : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdjpgcom.o : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o : wrjpgcom.c jinclude.h jconfig.h
diff --git a/makefile.auto b/makefile.auto
new file mode 100644
index 0000000..20a81b7
--- /dev/null
+++ b/makefile.auto
@@ -0,0 +1,238 @@
+# Makefile for Independent JPEG Group's software
+
+# makefile.auto is edited by configure to produce a custom Makefile.
+
+# Read installation instructions before saying "make" !!
+
+# For compiling with source and object files in different directories.
+srcdir = @srcdir@
+VPATH = @srcdir@
+
+# Where to install the programs and man pages.
+prefix = /usr/local
+exec_prefix = $(prefix)
+bindir = $(exec_prefix)/bin
+libdir = $(exec_prefix)/lib
+includedir = $(prefix)/include
+mandir = $(prefix)/man/man1
+binprefix =
+manprefix =
+manext = 1
+
+# The name of your C compiler:
+CC= @CC@
+
+# You may need to adjust these cc options:
+CFLAGS= -O -I$(srcdir)
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+# However, any special defines for ansi2knr.c may be included here:
+ANSI2KNRFLAGS= @ANSI2KNRFLAGS@
+
+# Link-time cc options:
+LDFLAGS= 
+
+# To link any special libraries, add the necessary -l commands here.
+LDLIBS= 
+
+# Put here the object file name for the correct system-dependent memory
+# manager file.  For Unix this is usually jmemnobs.o, but you may want
+# to use jmemansi.o or jmemname.o if you have limited swap space.
+SYSDEPMEM= @MEMORYMGR@
+
+# miscellaneous OS-dependent stuff
+SHELL= /bin/sh
+# linker
+LN= $(CC)
+# file deletion command
+RM= rm -f
+# file rename command
+MV= mv
+# library (.a) file creation command
+AR= ar rc
+# second step in .a creation (use "touch" if not needed)
+AR2= @RANLIB@
+# installation program
+INSTALL= @INSTALL@
+INSTALL_PROGRAM= @INSTALL_PROGRAM@
+INSTALL_DATA= @INSTALL_DATA@
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapi.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcmainct.c \
+        jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c \
+        jdapi.c jdatasrc.c jdatadst.c jdcoefct.c jdcolor.c jddctmgr.c \
+        jdhuff.c jdmainct.c jdmarker.c jdmaster.c jdpostct.c jdsample.c \
+        jerror.c jutils.c jfdctfst.c jfdctflt.c jfdctint.c jidctfst.c \
+        jidctflt.c jidctint.c jidctred.c jquant1.c jquant2.c jdmerge.c \
+        jmemmgr.c jmemansi.c jmemname.c jmemnobs.c jmemdos.c
+# source files: cjpeg/djpeg applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c rdcolmap.c rdppm.c wrppm.c rdgif.c wrgif.c \
+        rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c rdjpgcom.c \
+        wrjpgcom.c
+SOURCES= $(LIBSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h jpegint.h \
+        jpeglib.h jversion.h cdjpeg.h cderror.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 rdjpgcom.1 wrjpgcom.1 \
+        example.c libjpeg.doc structure.doc coderules.doc filelist.doc \
+        change.log
+MKFILES= configure makefile.auto makefile.ansi makefile.unix makefile.manx \
+        makefile.sas makcjpeg.st makdjpeg.st makljpeg.st makefile.bcc \
+        makefile.mc6 makefile.dj makefile.mms makefile.vms makvms.opt
+CONFIGFILES= jconfig.auto jconfig.manx jconfig.sas jconfig.st jconfig.bcc \
+        jconfig.mc6 jconfig.dj jconfig.vms
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+        $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapi.o jcparam.o jdatadst.o jcmaster.o jcmarker.o jcmainct.o \
+        jcprepct.o jccoefct.o jccolor.o jcsample.o jchuff.o jcdctmgr.o \
+        jfdctfst.o jfdctflt.o jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapi.o jdatasrc.o jdmaster.o jdmarker.o jdmainct.o jdcoefct.o \
+        jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+        jdhuff.o jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
+# These objectfiles are included in libjpeg.a
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for cjpeg and djpeg applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o
+
+
+all: @ANSI2KNR@ libjpeg.a cjpeg djpeg rdjpgcom wrjpgcom
+
+# This rule causes ansi2knr to be invoked.
+@ISANSICOM@.c.o:
+@ISANSICOM@	./ansi2knr $(srcdir)/$*.c T$*.c
+@ISANSICOM@	$(CC) $(CFLAGS) -c T$*.c
+@ISANSICOM@	$(RM) T$*.c $*.o
+@ISANSICOM@	$(MV) T$*.o $*.o
+
+ansi2knr: ansi2knr.c
+	$(CC) $(CFLAGS) $(ANSI2KNRFLAGS) -o ansi2knr ansi2knr.c
+
+libjpeg.a: @ANSI2KNR@ $(LIBOBJECTS)
+	$(RM) libjpeg.a
+	$(AR) libjpeg.a  $(LIBOBJECTS)
+	$(AR2) libjpeg.a
+
+cjpeg: $(COBJECTS) libjpeg.a
+	$(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.a $(LDLIBS)
+
+djpeg: $(DOBJECTS) libjpeg.a
+	$(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.a $(LDLIBS)
+
+rdjpgcom: rdjpgcom.o
+	$(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.o $(LDLIBS)
+
+wrjpgcom: wrjpgcom.o
+	$(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.o $(LDLIBS)
+
+jconfig.h: jconfig.doc
+	echo You must prepare a system-dependent jconfig.h file.
+	echo Please read the installation directions in install.doc.
+	exit 1
+
+install: cjpeg djpeg rdjpgcom wrjpgcom
+	$(INSTALL_PROGRAM) cjpeg $(bindir)/$(binprefix)cjpeg
+	$(INSTALL_PROGRAM) djpeg $(bindir)/$(binprefix)djpeg
+	$(INSTALL_PROGRAM) rdjpgcom $(bindir)/$(binprefix)rdjpgcom
+	$(INSTALL_PROGRAM) wrjpgcom $(bindir)/$(binprefix)wrjpgcom
+	$(INSTALL_DATA) $(srcdir)/cjpeg.1 $(mandir)/$(manprefix)cjpeg.$(manext)
+	$(INSTALL_DATA) $(srcdir)/djpeg.1 $(mandir)/$(manprefix)djpeg.$(manext)
+	$(INSTALL_DATA) $(srcdir)/rdjpgcom.1 $(mandir)/$(manprefix)rdjpgcom.$(manext)
+	$(INSTALL_DATA) $(srcdir)/wrjpgcom.1 $(mandir)/$(manprefix)wrjpgcom.$(manext)
+
+install-lib: libjpeg.a jconfig.h
+	$(INSTALL_DATA) libjpeg.a $(libdir)/$(binprefix)libjpeg.a
+	$(INSTALL_DATA) jconfig.h $(includedir)/jconfig.h
+	$(INSTALL_DATA) $(srcdir)/jpeglib.h $(includedir)/jpeglib.h
+	$(INSTALL_DATA) $(srcdir)/jmorecfg.h $(includedir)/jmorecfg.h
+	$(INSTALL_DATA) $(srcdir)/jerror.h $(includedir)/jerror.h
+
+clean:
+	$(RM) *.o cjpeg djpeg libjpeg.a rdjpgcom wrjpgcom ansi2knr core testout.*
+
+distribute:
+	$(RM) jpegsrc.tar*
+	tar cvf jpegsrc.tar $(DISTFILES)
+	compress -v jpegsrc.tar
+
+test: cjpeg djpeg
+	$(RM) testout.ppm testout.gif testout.jpg
+	./djpeg -dct int -ppm -outfile testout.ppm  testorig.jpg
+	./djpeg -dct int -gif -outfile testout.gif  testorig.jpg
+	./cjpeg -dct int -outfile testout.jpg  testimg.ppm
+	cmp testimg.ppm testout.ppm
+	cmp testimg.gif testout.gif
+	cmp testimg.jpg testout.jpg
+
+check: test
+
+# GNU Make likes to know which target names are not really files to be made:
+.PHONY: all install install-lib clean distribute test check
+
+
+jcapi.o : jcapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcprepct.o : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapi.o : jdapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatasrc.o : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatadst.o : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdpostct.o : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jutils.o : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jfdctfst.o : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctflt.o : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+rdcolmap.o : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdppm.o : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdjpgcom.o : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o : wrjpgcom.c jinclude.h jconfig.h
diff --git a/makefile.bcc b/makefile.bcc
index e404fb8..d76b229 100644
--- a/makefile.bcc
+++ b/makefile.bcc
@@ -8,110 +8,124 @@
 # like 1.01, are so buggy that you may as well forget it.)
 # Thanks to Tom Wright and Ge' Weijers for this file.
 
-# Read SETUP instructions before saying "make" !!
+# Read installation instructions before saying "make" !!
 
 # The name of your C compiler:
 CC= bcc
 
 # You may need to adjust these cc options:
-CFLAGS= -DHAVE_STDC -DINCLUDES_ARE_ANSI \
-	-ms -DMSDOS -DUSE_FMEM -DINCOMPLETE_TYPES_BROKEN -w-par -O2
-# -DHAVE_STDC -DINCLUDES_ARE_ANSI enable ANSI-C features (we DON'T want -A)
-# -ms selects small memory model for most efficient code
-# -DMSDOS enables DOS-specific code
-# -DUSE_FMEM indicates we have _fmemcpy() and _fmemset()
-# -DINCOMPLETE_TYPES_BROKEN suppresses bogus warning about undefined structures
+CFLAGS= -mm -w-par -O2
+# -mm selects medium memory model (near data, far code pointers)
 # -w-par suppresses warnings about unused function parameters
 # -O2 enables full code optimization (for pre-3.0 Borland C++, use -O -G -Z)
-# If you run up against DOS' 128-character limit on command line length,
-# you can get rid of some of the -D switches by adding equivalent #define
-# commands to the head of jinclude.h.
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
 
 # Link-time cc options:
-LDFLAGS= -ms
+LDFLAGS= -mm
 # memory model option here must match CFLAGS!
 
+# Put here the object file name for the correct system-dependent memory
+# manager file.  For DOS, we recommend jmemdos.c and jmemdosa.asm.
+# If you change this, you'll need to modify the linker response file
+# name list, below, by hand!
+SYSDEPMEM= jmemdos.obj jmemdosa.obj
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapi.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcmainct.c \
+        jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c \
+        jdapi.c jdatasrc.c jdatadst.c jdcoefct.c jdcolor.c jddctmgr.c \
+        jdhuff.c jdmainct.c jdmarker.c jdmaster.c jdpostct.c jdsample.c \
+        jerror.c jutils.c jfdctfst.c jfdctflt.c jfdctint.c jidctfst.c \
+        jidctflt.c jidctint.c jidctred.c jquant1.c jquant2.c jdmerge.c \
+        jmemmgr.c jmemansi.c jmemname.c jmemnobs.c jmemdos.c
+# source files: cjpeg/djpeg applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c rdcolmap.c rdppm.c wrppm.c rdgif.c wrgif.c \
+        rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c rdjpgcom.c \
+        wrjpgcom.c
+SOURCES= $(LIBSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h jpegint.h \
+        jpeglib.h jversion.h cdjpeg.h cderror.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 rdjpgcom.1 wrjpgcom.1 \
+        example.c libjpeg.doc structure.doc coderules.doc filelist.doc \
+        change.log
+MKFILES= configure makefile.auto makefile.ansi makefile.unix makefile.manx \
+        makefile.sas makcjpeg.st makdjpeg.st makljpeg.st makefile.bcc \
+        makefile.mc6 makefile.dj makefile.mms makefile.vms makvms.opt
+CONFIGFILES= jconfig.auto jconfig.manx jconfig.sas jconfig.st jconfig.bcc \
+        jconfig.mc6 jconfig.dj jconfig.vms
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+        $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.obj jutils.obj jerror.obj jmemmgr.obj $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapi.obj jcparam.obj jdatadst.obj jcmaster.obj jcmarker.obj \
+        jcmainct.obj jcprepct.obj jccoefct.obj jccolor.obj jcsample.obj \
+        jchuff.obj jcdctmgr.obj jfdctfst.obj jfdctflt.obj jfdctint.obj
+# decompression library object files
+DLIBOBJECTS= jdapi.obj jdatasrc.obj jdmaster.obj jdmarker.obj jdmainct.obj \
+        jdcoefct.obj jdpostct.obj jddctmgr.obj jidctfst.obj jidctflt.obj \
+        jidctint.obj jidctred.obj jdhuff.obj jdsample.obj jdcolor.obj \
+        jquant1.obj jquant2.obj jdmerge.obj
+# These objectfiles are included in libjpeg.lib
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for cjpeg and djpeg applications (excluding library files)
+COBJECTS= cjpeg.obj rdppm.obj rdgif.obj rdtarga.obj rdrle.obj rdbmp.obj
+DOBJECTS= djpeg.obj wrppm.obj wrgif.obj wrtarga.obj wrrle.obj wrbmp.obj \
+        rdcolmap.obj
+
 # need linker response file because file list > 128 chars
 RFILE= libjpeg.ans
 
 
-# source files (independently compilable files)
-SOURCES= jbsmooth.c jcarith.c jccolor.c jcdeflts.c jcexpand.c jchuff.c \
-        jcmain.c jcmaster.c jcmcu.c jcpipe.c jcsample.c jdarith.c jdcolor.c \
-        jddeflts.c jdhuff.c jdmain.c jdmaster.c jdmcu.c jdpipe.c jdsample.c \
-        jerror.c jquant1.c jquant2.c jfwddct.c jrevdct.c jutils.c jmemmgr.c \
-        jrdjfif.c jrdgif.c jrdppm.c jrdrle.c jrdtarga.c jwrjfif.c jwrgif.c \
-        jwrppm.c jwrrle.c jwrtarga.c
-# virtual source files (not present in distribution file, see SETUP)
-VIRTSOURCES= jmemsys.c
-# system-dependent implementations of virtual source files
-SYSDEPFILES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemdos.h \
-        jmemdosa.asm
-# files included by source files
-INCLUDES= jinclude.h jconfig.h jpegdata.h jversion.h jmemsys.h
-# documentation, test, and support files
-DOCS= README SETUP USAGE CHANGELOG cjpeg.1 djpeg.1 architecture codingrules
-MAKEFILES= makefile.ansi makefile.unix makefile.manx makefile.sas \
-        makcjpeg.st makdjpeg.st makljpeg.st makefile.mc5 makefile.mc6 \
-        makefile.bcc makefile.icc makljpeg.icc makefile.mms makefile.vms \
-        makvms.opt
-OTHERFILES= ansi2knr.c ckconfig.c example.c
-TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
-DISTFILES= $(DOCS) $(MAKEFILES) $(SOURCES) $(SYSDEPFILES) $(INCLUDES) \
-        $(OTHERFILES) $(TESTFILES)
-# objectfiles common to cjpeg and djpeg
-COMOBJECTS= jutils.obj jerror.obj jmemmgr.obj jmemsys.obj jmemdosa.obj
-# compression objectfiles
-CLIBOBJECTS= jcmaster.obj jcdeflts.obj jcarith.obj jccolor.obj jcexpand.obj \
-        jchuff.obj jcmcu.obj jcpipe.obj jcsample.obj jfwddct.obj \
-        jwrjfif.obj jrdgif.obj jrdppm.obj jrdrle.obj jrdtarga.obj
-COBJECTS= jcmain.obj $(CLIBOBJECTS) $(COMOBJECTS)
-# decompression objectfiles
-DLIBOBJECTS= jdmaster.obj jddeflts.obj jbsmooth.obj jdarith.obj jdcolor.obj \
-        jdhuff.obj jdmcu.obj jdpipe.obj jdsample.obj jquant1.obj \
-        jquant2.obj jrevdct.obj jrdjfif.obj jwrgif.obj jwrppm.obj \
-        jwrrle.obj jwrtarga.obj
-DOBJECTS= jdmain.obj $(DLIBOBJECTS) $(COMOBJECTS)
-# These objectfiles are included in libjpeg.lib
-LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+all: libjpeg.lib cjpeg.exe djpeg.exe rdjpgcom.exe wrjpgcom.exe
 
-
-all: cjpeg.exe djpeg.exe
-
-
-# libjpeg.lib is useful if you are including the JPEG software in a larger
-# program; you'd include it in your link, rather than the individual modules.
 libjpeg.lib: $(LIBOBJECTS) $(RFILE)
-    del libjpeg.lib
-    tlib @$(RFILE)
+	del libjpeg.lib
+	tlib @$(RFILE)
 
-# linker response file for same
+# linker response file for building libjpeg.lib
 $(RFILE) : Makefile
-    del $(RFILE)
-    echo libjpeg.lib & >$(RFILE)
-    echo +jcmaster.obj +jcdeflts.obj +jcarith.obj +jccolor.obj & >>$(RFILE)
-    echo +jcexpand.obj +jchuff.obj +jcmcu.obj +jcpipe.obj & >>$(RFILE)
-    echo +jcsample.obj +jfwddct.obj +jwrjfif.obj +jrdgif.obj & >>$(RFILE)
-    echo +jrdppm.obj +jrdrle.obj +jrdtarga.obj +jdmaster.obj & >>$(RFILE)
-    echo +jddeflts.obj +jbsmooth.obj +jdarith.obj +jdcolor.obj & >>$(RFILE)
-    echo +jdhuff.obj +jdmcu.obj +jdpipe.obj +jdsample.obj & >>$(RFILE)
-    echo +jquant1.obj +jquant2.obj +jrevdct.obj +jrdjfif.obj & >>$(RFILE)
-    echo +jwrgif.obj +jwrppm.obj +jwrrle.obj +jwrtarga.obj & >>$(RFILE)
-    echo +jutils.obj +jerror.obj +jmemmgr.obj +jmemsys.obj & >>$(RFILE)
-    echo +jmemdosa.obj >>$(RFILE)
+	del $(RFILE)
+	echo libjpeg.lib & >$(RFILE)
+	echo +jcapi.obj +jcparam.obj +jdatadst.obj +jcmaster.obj & >>$(RFILE)
+	echo +jcmarker.obj +jcmainct.obj +jcprepct.obj & >>$(RFILE)
+	echo +jccoefct.obj +jccolor.obj +jcsample.obj +jchuff.obj & >>$(RFILE)
+	echo +jcdctmgr.obj +jfdctfst.obj +jfdctflt.obj & >>$(RFILE)
+	echo +jfdctint.obj +jdapi.obj +jdatasrc.obj +jdmaster.obj & >>$(RFILE)
+	echo +jdmarker.obj +jdmainct.obj +jdcoefct.obj & >>$(RFILE)
+	echo +jdpostct.obj +jddctmgr.obj +jidctfst.obj & >>$(RFILE)
+	echo +jidctflt.obj +jidctint.obj +jidctred.obj +jdhuff.obj & >>$(RFILE)
+	echo +jdsample.obj +jdcolor.obj +jquant1.obj +jquant2.obj & >>$(RFILE)
+	echo +jdmerge.obj +jcomapi.obj +jutils.obj +jerror.obj & >>$(RFILE)
+	echo +jmemmgr.obj +jmemdos.obj +jmemdosa.obj >>$(RFILE)
 
-cjpeg.exe: jcmain.obj libjpeg.lib
-	$(CC) $(LDFLAGS) -ecjpeg.exe jcmain.obj libjpeg.lib
+cjpeg.exe: $(COBJECTS) libjpeg.lib
+	$(CC) $(LDFLAGS) -ecjpeg.exe $(COBJECTS) libjpeg.lib
 
-djpeg.exe: jdmain.obj libjpeg.lib
-	$(CC) $(LDFLAGS) -edjpeg.exe jdmain.obj libjpeg.lib
+djpeg.exe: $(DOBJECTS) libjpeg.lib
+	$(CC) $(LDFLAGS) -edjpeg.exe $(DOBJECTS) libjpeg.lib
+
+rdjpgcom.exe: rdjpgcom.c
+	$(CC) -ms -O rdjpgcom.c
+
+# wrjpgcom needs large model so it can malloc a 64K chunk
+wrjpgcom.exe: wrjpgcom.c
+	$(CC) -ml -O wrjpgcom.c
 
 .c.obj:
 	$(CC) $(CFLAGS) -c $<
 
-jmemsys.c:
-	echo You must select a system-dependent jmemsys.c file.
-	echo Please read the SETUP directions.
+jconfig.h: jconfig.doc
+	echo You must prepare a system-dependent jconfig.h file.
+	echo Please read the installation directions in install.doc.
 	exit 1
 
 clean:
@@ -119,55 +133,75 @@
 	del libjpeg.lib
 	del cjpeg.exe
 	del djpeg.exe
+	del rdjpgcom.exe
+	del wrjpgcom.exe
 	del testout.*
 
-test:
+test: cjpeg.exe djpeg.exe
 	del testout.*
-	djpeg -outfile testout.ppm  testorig.jpg
-	djpeg -gif -outfile testout.gif  testorig.jpg
-	cjpeg -outfile testout.jpg  testimg.ppm
+	djpeg -dct int -ppm -outfile testout.ppm  testorig.jpg
+	djpeg -dct int -gif -outfile testout.gif  testorig.jpg
+	cjpeg -dct int -outfile testout.jpg  testimg.ppm
 	fc /b testimg.ppm testout.ppm
 	fc /b testimg.gif testout.gif
 	fc /b testimg.jpg testout.jpg
 
 
-jbsmooth.obj : jbsmooth.c jinclude.h jconfig.h jpegdata.h
-jcarith.obj : jcarith.c jinclude.h jconfig.h jpegdata.h
-jccolor.obj : jccolor.c jinclude.h jconfig.h jpegdata.h
-jcdeflts.obj : jcdeflts.c jinclude.h jconfig.h jpegdata.h
-jcexpand.obj : jcexpand.c jinclude.h jconfig.h jpegdata.h
-jchuff.obj : jchuff.c jinclude.h jconfig.h jpegdata.h
-jcmain.obj : jcmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-jcmaster.obj : jcmaster.c jinclude.h jconfig.h jpegdata.h
-jcmcu.obj : jcmcu.c jinclude.h jconfig.h jpegdata.h
-jcpipe.obj : jcpipe.c jinclude.h jconfig.h jpegdata.h
-jcsample.obj : jcsample.c jinclude.h jconfig.h jpegdata.h
-jdarith.obj : jdarith.c jinclude.h jconfig.h jpegdata.h
-jdcolor.obj : jdcolor.c jinclude.h jconfig.h jpegdata.h
-jddeflts.obj : jddeflts.c jinclude.h jconfig.h jpegdata.h
-jdhuff.obj : jdhuff.c jinclude.h jconfig.h jpegdata.h
-jdmain.obj : jdmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-jdmaster.obj : jdmaster.c jinclude.h jconfig.h jpegdata.h
-jdmcu.obj : jdmcu.c jinclude.h jconfig.h jpegdata.h
-jdpipe.obj : jdpipe.c jinclude.h jconfig.h jpegdata.h
-jdsample.obj : jdsample.c jinclude.h jconfig.h jpegdata.h
-jerror.obj : jerror.c jinclude.h jconfig.h jpegdata.h
-jquant1.obj : jquant1.c jinclude.h jconfig.h jpegdata.h
-jquant2.obj : jquant2.c jinclude.h jconfig.h jpegdata.h
-jfwddct.obj : jfwddct.c jinclude.h jconfig.h jpegdata.h
-jrevdct.obj : jrevdct.c jinclude.h jconfig.h jpegdata.h
-jutils.obj : jutils.c jinclude.h jconfig.h jpegdata.h
-jmemmgr.obj : jmemmgr.c jinclude.h jconfig.h jpegdata.h jmemsys.h
-jrdjfif.obj : jrdjfif.c jinclude.h jconfig.h jpegdata.h
-jrdgif.obj : jrdgif.c jinclude.h jconfig.h jpegdata.h
-jrdppm.obj : jrdppm.c jinclude.h jconfig.h jpegdata.h
-jrdrle.obj : jrdrle.c jinclude.h jconfig.h jpegdata.h
-jrdtarga.obj : jrdtarga.c jinclude.h jconfig.h jpegdata.h
-jwrjfif.obj : jwrjfif.c jinclude.h jconfig.h jpegdata.h
-jwrgif.obj : jwrgif.c jinclude.h jconfig.h jpegdata.h
-jwrppm.obj : jwrppm.c jinclude.h jconfig.h jpegdata.h
-jwrrle.obj : jwrrle.c jinclude.h jconfig.h jpegdata.h
-jwrtarga.obj : jwrtarga.c jinclude.h jconfig.h jpegdata.h
-jmemsys.obj : jmemsys.c jinclude.h jconfig.h jpegdata.h jmemsys.h
+jcapi.obj : jcapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.obj : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.obj : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.obj : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.obj : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.obj : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.obj : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.obj : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.obj : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.obj : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcprepct.obj : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.obj : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapi.obj : jdapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatasrc.obj : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatadst.obj : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.obj : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.obj : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.obj : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.obj : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.obj : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.obj : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.obj : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdpostct.obj : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.obj : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.obj : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jutils.obj : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jfdctfst.obj : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctflt.obj : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.obj : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.obj : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.obj : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.obj : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.obj : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.obj : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.obj : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.obj : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.obj : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.obj : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.obj : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.obj : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.obj : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.obj : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.obj : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+rdcolmap.obj : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdppm.obj : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.obj : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.obj : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.obj : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.obj : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.obj : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.obj : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.obj : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.obj : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.obj : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdjpgcom.obj : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.obj : wrjpgcom.c jinclude.h jconfig.h
 jmemdosa.obj : jmemdosa.asm
 	tasm /mx jmemdosa.asm
diff --git a/makefile.dj b/makefile.dj
new file mode 100644
index 0000000..3988f80
--- /dev/null
+++ b/makefile.dj
@@ -0,0 +1,200 @@
+# Makefile for Independent JPEG Group's software
+
+# This makefile is for DJGPP (Delorie's GNU C port) on MS-DOS.
+# Say "make exe" to get stub-style .exe's, or
+# "make standalone" to get standalone .exe's.
+
+# Read installation instructions before saying "make" !!
+
+# To do "make standalone", you'll need to be sure this points to go32.exe:
+GO32= d:/djgpp/bin/go32.exe
+
+# The name of your C compiler:
+CC= gcc
+
+# You may need to adjust these cc options:
+CFLAGS= -O2 -Wall
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+
+# Link-time cc options:
+LDFLAGS= 
+
+# To link any special libraries, add the necessary -l commands here.
+LDLIBS= 
+
+# Put here the object file name for the correct system-dependent memory
+# manager file.  For DJGPP this is usually jmemnobs.o, but you could
+# use jmemname.o if you want to use named temp files instead of swap space.
+SYSDEPMEM= jmemnobs.o
+
+# miscellaneous OS-dependent stuff
+# linker
+LN= $(CC)
+# file deletion command
+RM= del
+# library (.a) file creation command
+AR= ar rc
+# second step in .a creation (use "touch" if not needed)
+AR2= ranlib
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapi.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcmainct.c \
+        jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c \
+        jdapi.c jdatasrc.c jdatadst.c jdcoefct.c jdcolor.c jddctmgr.c \
+        jdhuff.c jdmainct.c jdmarker.c jdmaster.c jdpostct.c jdsample.c \
+        jerror.c jutils.c jfdctfst.c jfdctflt.c jfdctint.c jidctfst.c \
+        jidctflt.c jidctint.c jidctred.c jquant1.c jquant2.c jdmerge.c \
+        jmemmgr.c jmemansi.c jmemname.c jmemnobs.c jmemdos.c
+# source files: cjpeg/djpeg applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c rdcolmap.c rdppm.c wrppm.c rdgif.c wrgif.c \
+        rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c rdjpgcom.c \
+        wrjpgcom.c
+SOURCES= $(LIBSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h jpegint.h \
+        jpeglib.h jversion.h cdjpeg.h cderror.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 rdjpgcom.1 wrjpgcom.1 \
+        example.c libjpeg.doc structure.doc coderules.doc filelist.doc \
+        change.log
+MKFILES= configure makefile.auto makefile.ansi makefile.unix makefile.manx \
+        makefile.sas makcjpeg.st makdjpeg.st makljpeg.st makefile.bcc \
+        makefile.mc6 makefile.dj makefile.mms makefile.vms makvms.opt
+CONFIGFILES= jconfig.auto jconfig.manx jconfig.sas jconfig.st jconfig.bcc \
+        jconfig.mc6 jconfig.dj jconfig.vms
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+        $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapi.o jcparam.o jdatadst.o jcmaster.o jcmarker.o jcmainct.o \
+        jcprepct.o jccoefct.o jccolor.o jcsample.o jchuff.o jcdctmgr.o \
+        jfdctfst.o jfdctflt.o jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapi.o jdatasrc.o jdmaster.o jdmarker.o jdmainct.o jdcoefct.o \
+        jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+        jdhuff.o jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
+# These objectfiles are included in libjpeg.a
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for cjpeg and djpeg applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o
+
+
+all: libjpeg.a cjpeg djpeg rdjpgcom wrjpgcom
+
+libjpeg.a: $(LIBOBJECTS)
+	$(RM) libjpeg.a
+	$(AR) libjpeg.a  $(LIBOBJECTS)
+	$(AR2) libjpeg.a
+
+cjpeg: $(COBJECTS) libjpeg.a
+	$(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.a $(LDLIBS)
+
+djpeg: $(DOBJECTS) libjpeg.a
+	$(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.a $(LDLIBS)
+
+rdjpgcom: rdjpgcom.o
+	$(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.o $(LDLIBS)
+
+wrjpgcom: wrjpgcom.o
+	$(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.o $(LDLIBS)
+
+exe: cjpeg djpeg rdjpgcom wrjpgcom
+	coff2exe cjpeg
+	coff2exe djpeg
+	coff2exe rdjpgcom
+	coff2exe wrjpgcom
+
+standalone: cjpeg djpeg rdjpgcom wrjpgcom
+	coff2exe -s $(GO32) cjpeg
+	coff2exe -s $(GO32) djpeg
+	coff2exe -s $(GO32) rdjpgcom
+	coff2exe -s $(GO32) wrjpgcom
+
+jconfig.h: jconfig.doc
+	echo You must prepare a system-dependent jconfig.h file.
+	echo Please read the installation directions in install.doc.
+	exit 1
+
+clean:
+	$(RM) *.o
+	$(RM) cjpeg
+	$(RM) djpeg
+	$(RM) rdjpgcom
+	$(RM) wrjpgcom
+	$(RM) libjpeg.a
+	$(RM) testout.*
+
+test: cjpeg djpeg
+	$(RM) testout.*
+	go32 djpeg -dct int -ppm -outfile testout.ppm  testorig.jpg
+	go32 djpeg -dct int -gif -outfile testout.gif  testorig.jpg
+	go32 cjpeg -dct int -outfile testout.jpg  testimg.ppm
+	fc /b testimg.ppm testout.ppm
+	fc /b testimg.gif testout.gif
+	fc /b testimg.jpg testout.jpg
+
+
+jcapi.o : jcapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcprepct.o : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapi.o : jdapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatasrc.o : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatadst.o : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdpostct.o : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jutils.o : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jfdctfst.o : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctflt.o : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+rdcolmap.o : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdppm.o : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdjpgcom.o : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o : wrjpgcom.c jinclude.h jconfig.h
diff --git a/makefile.icc b/makefile.icc
deleted file mode 100644
index 1f0d6fc..0000000
--- a/makefile.icc
+++ /dev/null
@@ -1,141 +0,0 @@
-# Makefile for Independent JPEG Group's software
-
-# This makefile is for Intel's Code Builder 32-bit DOS/Windows compiler.
-# Thanks to Bailey Brown.
-
-# Read SETUP instructions before saying "make" !!
-
-# The name of your C compiler:
-CC= icc
-
-# You may want to adjust these cc options:
-CFLAGS= /O3 /Ll /DTWO_FILE_COMMANDLINE
-
-# Link-time cc options:
-LDFLAGS= /F /xregion=4M
-# /F removes floating-point support, which we don't need.
-# /xregionsize sets virtual memory region size.
-
-# To link any special libraries, add the necessary -l commands here.
-LDLIBS= 
-
-# miscellaneous OS-dependent stuff
-# linker
-LN= $(CC)
-# library (.lib) file creation command
-AR= lib32
-
-
-# source files (independently compilable files)
-SOURCES= jbsmooth.c jcarith.c jccolor.c jcdeflts.c jcexpand.c jchuff.c \
-        jcmain.c jcmaster.c jcmcu.c jcpipe.c jcsample.c jdarith.c jdcolor.c \
-        jddeflts.c jdhuff.c jdmain.c jdmaster.c jdmcu.c jdpipe.c jdsample.c \
-        jerror.c jquant1.c jquant2.c jfwddct.c jrevdct.c jutils.c jmemmgr.c \
-        jrdjfif.c jrdgif.c jrdppm.c jrdrle.c jrdtarga.c jwrjfif.c jwrgif.c \
-        jwrppm.c jwrrle.c jwrtarga.c
-# virtual source files (not present in distribution file, see SETUP)
-VIRTSOURCES= jmemsys.c
-# system-dependent implementations of virtual source files
-SYSDEPFILES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemdos.h \
-        jmemdosa.asm
-# files included by source files
-INCLUDES= jinclude.h jconfig.h jpegdata.h jversion.h jmemsys.h
-# documentation, test, and support files
-DOCS= README SETUP USAGE CHANGELOG cjpeg.1 djpeg.1 architecture codingrules
-MAKEFILES= makefile.ansi makefile.unix makefile.manx makefile.sas \
-        makcjpeg.st makdjpeg.st makljpeg.st makefile.mc5 makefile.mc6 \
-        makefile.bcc makefile.icc makljpeg.icc makefile.mms makefile.vms \
-        makvms.opt
-OTHERFILES= ansi2knr.c ckconfig.c example.c
-TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
-DISTFILES= $(DOCS) $(MAKEFILES) $(SOURCES) $(SYSDEPFILES) $(INCLUDES) \
-        $(OTHERFILES) $(TESTFILES)
-# objectfiles common to cjpeg and djpeg
-COMOBJECTS= jutils.obj jerror.obj jmemmgr.obj jmemsys.obj
-# compression objectfiles
-CLIBOBJECTS= jcmaster.obj jcdeflts.obj jcarith.obj jccolor.obj jcexpand.obj \
-        jchuff.obj jcmcu.obj jcpipe.obj jcsample.obj jfwddct.obj \
-        jwrjfif.obj jrdgif.obj jrdppm.obj jrdrle.obj jrdtarga.obj
-COBJECTS= jcmain.obj $(CLIBOBJECTS) $(COMOBJECTS)
-# decompression objectfiles
-DLIBOBJECTS= jdmaster.obj jddeflts.obj jbsmooth.obj jdarith.obj jdcolor.obj \
-        jdhuff.obj jdmcu.obj jdpipe.obj jdsample.obj jquant1.obj \
-        jquant2.obj jrevdct.obj jrdjfif.obj jwrgif.obj jwrppm.obj \
-        jwrrle.obj jwrtarga.obj
-DOBJECTS= jdmain.obj $(DLIBOBJECTS) $(COMOBJECTS)
-# These objectfiles are included in libjpeg.lib
-LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
-
-
-all: cjpeg.exe djpeg.exe
-# By default, libjpeg.lib is not built unless you explicitly request it.
-# You can add libjpeg.lib to the line above if you want it built by default.
-
-
-cjpeg.exe: $(COBJECTS)
-	echo $(COBJECTS) >cjpeg.rsp
-	$(LN) $(LDFLAGS) /ecjpeg.exe @cjpeg.rsp $(LDLIBS)
-
-djpeg.exe: $(DOBJECTS)
-	echo $(DOBJECTS) >djpeg.rsp
-	$(LN) $(LDFLAGS) /edjpeg.exe @djpeg.rsp $(LDLIBS)
-
-# libjpeg.lib is useful if you are including the JPEG software in a larger
-# program; you'd include it in your link, rather than the individual modules.
-libjpeg.lib: $(LIBOBJECTS)
-	del libjpeg.lib
-	$(AR) libjpeg.lib  nobackup batch <makljpeg.icc
-
-jmemsys.c:
-	echo You must select a system-dependent jmemsys.c file.
-	echo Please read the SETUP directions.
-	exit 1
-
-test: cjpeg.exe djpeg.exe
-        del testout.*
-	djpeg -outfile testout.ppm  testorig.jpg
-	djpeg -gif -outfile testout.gif  testorig.jpg
-	cjpeg -outfile testout.jpg  testimg.ppm
-        fc /b testimg.ppm testout.ppm
-        fc /b testimg.gif testout.gif
-        fc /b testimg.jpg testout.jpg
-
-
-jbsmooth.obj : jbsmooth.c jinclude.h jconfig.h jpegdata.h
-jcarith.obj : jcarith.c jinclude.h jconfig.h jpegdata.h
-jccolor.obj : jccolor.c jinclude.h jconfig.h jpegdata.h
-jcdeflts.obj : jcdeflts.c jinclude.h jconfig.h jpegdata.h
-jcexpand.obj : jcexpand.c jinclude.h jconfig.h jpegdata.h
-jchuff.obj : jchuff.c jinclude.h jconfig.h jpegdata.h
-jcmain.obj : jcmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-jcmaster.obj : jcmaster.c jinclude.h jconfig.h jpegdata.h
-jcmcu.obj : jcmcu.c jinclude.h jconfig.h jpegdata.h
-jcpipe.obj : jcpipe.c jinclude.h jconfig.h jpegdata.h
-jcsample.obj : jcsample.c jinclude.h jconfig.h jpegdata.h
-jdarith.obj : jdarith.c jinclude.h jconfig.h jpegdata.h
-jdcolor.obj : jdcolor.c jinclude.h jconfig.h jpegdata.h
-jddeflts.obj : jddeflts.c jinclude.h jconfig.h jpegdata.h
-jdhuff.obj : jdhuff.c jinclude.h jconfig.h jpegdata.h
-jdmain.obj : jdmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-jdmaster.obj : jdmaster.c jinclude.h jconfig.h jpegdata.h
-jdmcu.obj : jdmcu.c jinclude.h jconfig.h jpegdata.h
-jdpipe.obj : jdpipe.c jinclude.h jconfig.h jpegdata.h
-jdsample.obj : jdsample.c jinclude.h jconfig.h jpegdata.h
-jerror.obj : jerror.c jinclude.h jconfig.h jpegdata.h
-jquant1.obj : jquant1.c jinclude.h jconfig.h jpegdata.h
-jquant2.obj : jquant2.c jinclude.h jconfig.h jpegdata.h
-jfwddct.obj : jfwddct.c jinclude.h jconfig.h jpegdata.h
-jrevdct.obj : jrevdct.c jinclude.h jconfig.h jpegdata.h
-jutils.obj : jutils.c jinclude.h jconfig.h jpegdata.h
-jmemmgr.obj : jmemmgr.c jinclude.h jconfig.h jpegdata.h jmemsys.h
-jrdjfif.obj : jrdjfif.c jinclude.h jconfig.h jpegdata.h
-jrdgif.obj : jrdgif.c jinclude.h jconfig.h jpegdata.h
-jrdppm.obj : jrdppm.c jinclude.h jconfig.h jpegdata.h
-jrdrle.obj : jrdrle.c jinclude.h jconfig.h jpegdata.h
-jrdtarga.obj : jrdtarga.c jinclude.h jconfig.h jpegdata.h
-jwrjfif.obj : jwrjfif.c jinclude.h jconfig.h jpegdata.h
-jwrgif.obj : jwrgif.c jinclude.h jconfig.h jpegdata.h
-jwrppm.obj : jwrppm.c jinclude.h jconfig.h jpegdata.h
-jwrrle.obj : jwrrle.c jinclude.h jconfig.h jpegdata.h
-jwrtarga.obj : jwrtarga.c jinclude.h jconfig.h jpegdata.h
-jmemsys.obj : jmemsys.c jinclude.h jconfig.h jpegdata.h jmemsys.h
diff --git a/makefile.manx b/makefile.manx
index e13d70e..28773b3 100644
--- a/makefile.manx
+++ b/makefile.manx
@@ -1,23 +1,21 @@
 # Makefile for Independent JPEG Group's software
 
 # This makefile is for Amiga systems using Manx Aztec C ver 5.x.
-# Use jmemname.c as the system-dependent memory manager.
 # Thanks to D.J. James (djjames@cup.portal.com) for this version.
 
-# Read SETUP instructions before saying "make" !!
+# Read installation instructions before saying "make" !!
 
 # The name of your C compiler:
 CC= cc
 
 # You may need to adjust these cc options:
-# uncomment for generic 68000 code (will work on any Amiga)
+# Uncomment for generic 68000 code (will work on any Amiga)
 ARCHFLAGS= -sn
 
-# uncomment for 68020/68030 code (faster, but won't run on 68000 CPU)
+# Uncomment for 68020/68030 code (faster, but won't run on 68000 CPU)
 #ARCHFLAGS= -c2
 
-CFLAGS= -MC -MD -DAMIGA -DTWO_FILE_COMMANDLINE -DNEED_SIGNAL_CATCHER \
-	-Dsignal_catcher=_abort -DSHORTxSHORT_32 $(ARCHFLAGS) -spfam -r4
+CFLAGS= -MC -MD $(ARCHFLAGS) -spfam -r4
 
 # Link-time cc options:
 LDFLAGS= -g
@@ -25,6 +23,10 @@
 # To link any special libraries, add the necessary -l commands here.
 LDLIBS= -lml -lcl
 
+# Put here the object file name for the correct system-dependent memory
+# manager file.  For Amiga we recommend jmemname.o.
+SYSDEPMEM= jmemname.o
+
 # miscellaneous OS-dependent stuff
 # linker
 LN= ln
@@ -33,122 +35,144 @@
 # library (.lib) file creation command
 AR= lb
 
+# End of configurable options.
 
-# source files (independently compilable files)
-SOURCES= jbsmooth.c jcarith.c jccolor.c jcdeflts.c jcexpand.c jchuff.c \
-        jcmain.c jcmaster.c jcmcu.c jcpipe.c jcsample.c jdarith.c jdcolor.c \
-        jddeflts.c jdhuff.c jdmain.c jdmaster.c jdmcu.c jdpipe.c jdsample.c \
-        jerror.c jquant1.c jquant2.c jfwddct.c jrevdct.c jutils.c jmemmgr.c \
-        jrdjfif.c jrdgif.c jrdppm.c jrdrle.c jrdtarga.c jwrjfif.c jwrgif.c \
-        jwrppm.c jwrrle.c jwrtarga.c
-# virtual source files (not present in distribution file, see SETUP)
-VIRTSOURCES= jmemsys.c
-# system-dependent implementations of virtual source files
-SYSDEPFILES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemdos.h \
-        jmemdosa.asm
+
+# source files: JPEG library proper
+LIBSOURCES= jcapi.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcmainct.c \
+        jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c \
+        jdapi.c jdatasrc.c jdatadst.c jdcoefct.c jdcolor.c jddctmgr.c \
+        jdhuff.c jdmainct.c jdmarker.c jdmaster.c jdpostct.c jdsample.c \
+        jerror.c jutils.c jfdctfst.c jfdctflt.c jfdctint.c jidctfst.c \
+        jidctflt.c jidctint.c jidctred.c jquant1.c jquant2.c jdmerge.c \
+        jmemmgr.c jmemansi.c jmemname.c jmemnobs.c jmemdos.c
+# source files: cjpeg/djpeg applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c rdcolmap.c rdppm.c wrppm.c rdgif.c wrgif.c \
+        rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c rdjpgcom.c \
+        wrjpgcom.c
+SOURCES= $(LIBSOURCES) $(APPSOURCES)
 # files included by source files
-INCLUDES= jinclude.h jconfig.h jpegdata.h jversion.h jmemsys.h
+INCLUDES= jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h jpegint.h \
+        jpeglib.h jversion.h cdjpeg.h cderror.h
 # documentation, test, and support files
-DOCS= README SETUP USAGE CHANGELOG cjpeg.1 djpeg.1 architecture codingrules
-MAKEFILES= makefile.ansi makefile.unix makefile.manx makefile.sas \
-        makcjpeg.st makdjpeg.st makljpeg.st makefile.mc5 makefile.mc6 \
-        makefile.bcc makefile.icc makljpeg.icc makefile.mms makefile.vms \
-        makvms.opt
-OTHERFILES= ansi2knr.c ckconfig.c example.c
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 rdjpgcom.1 wrjpgcom.1 \
+        example.c libjpeg.doc structure.doc coderules.doc filelist.doc \
+        change.log
+MKFILES= configure makefile.auto makefile.ansi makefile.unix makefile.manx \
+        makefile.sas makcjpeg.st makdjpeg.st makljpeg.st makefile.bcc \
+        makefile.mc6 makefile.dj makefile.mms makefile.vms makvms.opt
+CONFIGFILES= jconfig.auto jconfig.manx jconfig.sas jconfig.st jconfig.bcc \
+        jconfig.mc6 jconfig.dj jconfig.vms
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
 TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
-DISTFILES= $(DOCS) $(MAKEFILES) $(SOURCES) $(SYSDEPFILES) $(INCLUDES) \
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
         $(OTHERFILES) $(TESTFILES)
-# objectfiles common to cjpeg and djpeg
-COMOBJECTS= jutils.o jerror.o jmemmgr.o jmemsys.o
-# compression objectfiles
-CLIBOBJECTS= jcmaster.o jcdeflts.o jcarith.o jccolor.o jcexpand.o jchuff.o \
-        jcmcu.o jcpipe.o jcsample.o jfwddct.o jwrjfif.o jrdgif.o jrdppm.o \
-        jrdrle.o jrdtarga.o
-COBJECTS= jcmain.o $(CLIBOBJECTS) $(COMOBJECTS)
-# decompression objectfiles
-DLIBOBJECTS= jdmaster.o jddeflts.o jbsmooth.o jdarith.o jdcolor.o jdhuff.o \
-        jdmcu.o jdpipe.o jdsample.o jquant1.o jquant2.o jrevdct.o jrdjfif.o \
-        jwrgif.o jwrppm.o jwrrle.o jwrtarga.o
-DOBJECTS= jdmain.o $(DLIBOBJECTS) $(COMOBJECTS)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapi.o jcparam.o jdatadst.o jcmaster.o jcmarker.o jcmainct.o \
+        jcprepct.o jccoefct.o jccolor.o jcsample.o jchuff.o jcdctmgr.o \
+        jfdctfst.o jfdctflt.o jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapi.o jdatasrc.o jdmaster.o jdmarker.o jdmainct.o jdcoefct.o \
+        jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+        jdhuff.o jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
 # These objectfiles are included in libjpeg.lib
 LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for cjpeg and djpeg applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o
 
 
-all: cjpeg djpeg
-# By default, libjpeg.lib is not built unless you explicitly request it.
-# You can add libjpeg.lib to the line above if you want it built by default.
+all: libjpeg.lib cjpeg djpeg rdjpgcom wrjpgcom
 
-
-cjpeg: $(COBJECTS)
-	$(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) $(LDLIBS)
-
-djpeg: $(DOBJECTS)
-	$(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) $(LDLIBS)
-
-# libjpeg.lib is useful if you are including the JPEG software in a larger
-# program; you'd include it in your link, rather than the individual modules.
 libjpeg.lib: $(LIBOBJECTS)
 	-$(RM) libjpeg.lib
 	$(AR) libjpeg.lib  $(LIBOBJECTS)
 
-jmemsys.c:
-	echo You must select a system-dependent jmemsys.c file.
-	echo Please read the SETUP directions.
+cjpeg: $(COBJECTS) libjpeg.lib
+	$(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.lib $(LDLIBS)
+
+djpeg: $(DOBJECTS) libjpeg.lib
+	$(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.lib $(LDLIBS)
+
+rdjpgcom: rdjpgcom.o
+	$(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.o $(LDLIBS)
+
+wrjpgcom: wrjpgcom.o
+	$(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.o $(LDLIBS)
+
+jconfig.h: jconfig.doc
+	echo You must prepare a system-dependent jconfig.h file.
+	echo Please read the installation directions in install.doc.
 	exit 1
 
 clean:
-	-$(RM) *.o cjpeg djpeg libjpeg.lib core testout.*
-
-distribute:
-	-$(RM) jpegsrc.tar*
-	tar cvf jpegsrc.tar $(DISTFILES)
-	compress -v jpegsrc.tar
+	-$(RM) *.o cjpeg djpeg libjpeg.lib rdjpgcom wrjpgcom core testout.*
 
 test: cjpeg djpeg
 	-$(RM) testout.ppm testout.gif testout.jpg
-	djpeg -outfile testout.ppm  testorig.jpg
-	djpeg -gif -outfile testout.gif  testorig.jpg
-	cjpeg -outfile testout.jpg  testimg.ppm
+	djpeg -dct int -ppm -outfile testout.ppm  testorig.jpg
+	djpeg -dct int -gif -outfile testout.gif  testorig.jpg
+	cjpeg -dct int -outfile testout.jpg  testimg.ppm
 	cmp testimg.ppm testout.ppm
 	cmp testimg.gif testout.gif
 	cmp testimg.jpg testout.jpg
 
 
-jbsmooth.o : jbsmooth.c jinclude.h jconfig.h jpegdata.h 
-jcarith.o : jcarith.c jinclude.h jconfig.h jpegdata.h 
-jccolor.o : jccolor.c jinclude.h jconfig.h jpegdata.h 
-jcdeflts.o : jcdeflts.c jinclude.h jconfig.h jpegdata.h 
-jcexpand.o : jcexpand.c jinclude.h jconfig.h jpegdata.h 
-jchuff.o : jchuff.c jinclude.h jconfig.h jpegdata.h 
-jcmain.o : jcmain.c jinclude.h jconfig.h jpegdata.h jversion.h 
-jcmaster.o : jcmaster.c jinclude.h jconfig.h jpegdata.h 
-jcmcu.o : jcmcu.c jinclude.h jconfig.h jpegdata.h 
-jcpipe.o : jcpipe.c jinclude.h jconfig.h jpegdata.h 
-jcsample.o : jcsample.c jinclude.h jconfig.h jpegdata.h 
-jdarith.o : jdarith.c jinclude.h jconfig.h jpegdata.h 
-jdcolor.o : jdcolor.c jinclude.h jconfig.h jpegdata.h 
-jddeflts.o : jddeflts.c jinclude.h jconfig.h jpegdata.h 
-jdhuff.o : jdhuff.c jinclude.h jconfig.h jpegdata.h 
-jdmain.o : jdmain.c jinclude.h jconfig.h jpegdata.h jversion.h 
-jdmaster.o : jdmaster.c jinclude.h jconfig.h jpegdata.h 
-jdmcu.o : jdmcu.c jinclude.h jconfig.h jpegdata.h 
-jdpipe.o : jdpipe.c jinclude.h jconfig.h jpegdata.h 
-jdsample.o : jdsample.c jinclude.h jconfig.h jpegdata.h 
-jerror.o : jerror.c jinclude.h jconfig.h jpegdata.h 
-jquant1.o : jquant1.c jinclude.h jconfig.h jpegdata.h 
-jquant2.o : jquant2.c jinclude.h jconfig.h jpegdata.h 
-jfwddct.o : jfwddct.c jinclude.h jconfig.h jpegdata.h 
-jrevdct.o : jrevdct.c jinclude.h jconfig.h jpegdata.h 
-jutils.o : jutils.c jinclude.h jconfig.h jpegdata.h 
-jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpegdata.h jmemsys.h 
-jrdjfif.o : jrdjfif.c jinclude.h jconfig.h jpegdata.h 
-jrdgif.o : jrdgif.c jinclude.h jconfig.h jpegdata.h 
-jrdppm.o : jrdppm.c jinclude.h jconfig.h jpegdata.h 
-jrdrle.o : jrdrle.c jinclude.h jconfig.h jpegdata.h 
-jrdtarga.o : jrdtarga.c jinclude.h jconfig.h jpegdata.h 
-jwrjfif.o : jwrjfif.c jinclude.h jconfig.h jpegdata.h 
-jwrgif.o : jwrgif.c jinclude.h jconfig.h jpegdata.h 
-jwrppm.o : jwrppm.c jinclude.h jconfig.h jpegdata.h 
-jwrrle.o : jwrrle.c jinclude.h jconfig.h jpegdata.h 
-jwrtarga.o : jwrtarga.c jinclude.h jconfig.h jpegdata.h 
-jmemsys.o : jmemsys.c jinclude.h jconfig.h jpegdata.h jmemsys.h 
+jcapi.o : jcapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcprepct.o : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapi.o : jdapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatasrc.o : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatadst.o : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdpostct.o : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jutils.o : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jfdctfst.o : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctflt.o : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+rdcolmap.o : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdppm.o : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdjpgcom.o : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o : wrjpgcom.c jinclude.h jconfig.h
diff --git a/makefile.mc5 b/makefile.mc5
deleted file mode 100644
index 4335797..0000000
--- a/makefile.mc5
+++ /dev/null
@@ -1,162 +0,0 @@
-# Makefile for Independent JPEG Group's software
-
-# This makefile is for Microsoft C for MS-DOS, version 5.x.
-
-# Read SETUP instructions before saying "make" !!
-
-# Microsoft's brain-damaged version of make uses nonstandard syntax (a blank
-# line is needed to terminate a command list) and it simply scans the rules
-# in order, rather than doing a true dependency-tree walk.  Furthermore,
-# expanded command lines can't exceed 128 chars (this is a DOS bug, not
-# make's fault); so we can't just name all the objectfiles in the link steps.
-# Instead we shove each objectfile into a library as it is made, and link
-# from the library.  The objectfiles are also kept separately as timestamps.
-
-# You may need to adjust these cc options:
-CFLAGS=  /AS /I. /W3 /Oail /Gs   # NB: /Gs turns off stack oflo checks
-LDFLAGS= /Fm /F 2000             # /F hhhh  sets stack size (in hex)
-# In particular:
-#   Add /DMSDOS if your compiler doesn't automatically #define MSDOS.
-#   Add /DMEM_STATS to enable gathering of memory usage statistics.
-# You might also want to add /G2 if you have an 80286, etc.
-
-
-# source files (independently compilable files)
-SOURCES= jbsmooth.c jcarith.c jccolor.c jcdeflts.c jcexpand.c jchuff.c \
-        jcmain.c jcmaster.c jcmcu.c jcpipe.c jcsample.c jdarith.c jdcolor.c \
-        jddeflts.c jdhuff.c jdmain.c jdmaster.c jdmcu.c jdpipe.c jdsample.c \
-        jerror.c jquant1.c jquant2.c jfwddct.c jrevdct.c jutils.c jmemmgr.c \
-        jrdjfif.c jrdgif.c jrdppm.c jrdrle.c jrdtarga.c jwrjfif.c jwrgif.c \
-        jwrppm.c jwrrle.c jwrtarga.c
-# virtual source files (not present in distribution file, see SETUP)
-VIRTSOURCES= jmemsys.c
-# system-dependent implementations of virtual source files
-SYSDEPFILES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemdos.h \
-        jmemdosa.asm
-# files included by source files
-INCLUDES= jinclude.h jconfig.h jpegdata.h jversion.h jmemsys.h
-# documentation, test, and support files
-DOCS= README SETUP USAGE CHANGELOG cjpeg.1 djpeg.1 architecture codingrules
-MAKEFILES= makefile.ansi makefile.unix makefile.manx makefile.sas \
-        makcjpeg.st makdjpeg.st makljpeg.st makefile.mc5 makefile.mc6 \
-        makefile.bcc makefile.icc makljpeg.icc makefile.mms makefile.vms \
-        makvms.opt
-OTHERFILES= ansi2knr.c ckconfig.c example.c
-TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
-DISTFILES= $(DOCS) $(MAKEFILES) $(SOURCES) $(SYSDEPFILES) $(INCLUDES) \
-        $(OTHERFILES) $(TESTFILES)
-# objectfiles common to cjpeg and djpeg
-COMOBJECTS= jutils.obj jerror.obj jmemmgr.obj jmemsys.obj jmemdosa.obj
-# compression objectfiles
-CLIBOBJECTS= jcmaster.obj jcdeflts.obj jcarith.obj jccolor.obj jcexpand.obj \
-        jchuff.obj jcmcu.obj jcpipe.obj jcsample.obj jfwddct.obj \
-        jwrjfif.obj jrdgif.obj jrdppm.obj jrdrle.obj jrdtarga.obj
-COBJECTS= jcmain.obj $(CLIBOBJECTS) $(COMOBJECTS)
-# decompression objectfiles
-DLIBOBJECTS= jdmaster.obj jddeflts.obj jbsmooth.obj jdarith.obj jdcolor.obj \
-        jdhuff.obj jdmcu.obj jdpipe.obj jdsample.obj jquant1.obj \
-        jquant2.obj jrevdct.obj jrdjfif.obj jwrgif.obj jwrppm.obj \
-        jwrrle.obj jwrtarga.obj
-DOBJECTS= jdmain.obj $(DLIBOBJECTS) $(COMOBJECTS)
-# These objectfiles are included in libjpeg.lib
-LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
-
-
-# inference rule used for all compilations except jcmain.c, jdmain.c
-# notice that objectfile is also inserted into libjpeg.lib
-.c.obj:
-	cl $(CFLAGS) /c $*.c
-	lib libjpeg -+$*.obj;
-
-# inference rule for assembly code
-.asm.obj:
-	masm /mx $*;
-	lib libjpeg -+$*.obj;
-
-
-jbsmooth.obj : jbsmooth.c jinclude.h jconfig.h jpegdata.h
-
-jcarith.obj : jcarith.c jinclude.h jconfig.h jpegdata.h
-
-jccolor.obj : jccolor.c jinclude.h jconfig.h jpegdata.h
-
-jcdeflts.obj : jcdeflts.c jinclude.h jconfig.h jpegdata.h
-
-jcexpand.obj : jcexpand.c jinclude.h jconfig.h jpegdata.h
-
-jchuff.obj : jchuff.c jinclude.h jconfig.h jpegdata.h
-
-jcmain.obj : jcmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-	cl $(CFLAGS) /c $*.c
-
-jcmaster.obj : jcmaster.c jinclude.h jconfig.h jpegdata.h
-
-jcmcu.obj : jcmcu.c jinclude.h jconfig.h jpegdata.h
-
-jcpipe.obj : jcpipe.c jinclude.h jconfig.h jpegdata.h
-
-jcsample.obj : jcsample.c jinclude.h jconfig.h jpegdata.h
-
-jdarith.obj : jdarith.c jinclude.h jconfig.h jpegdata.h
-
-jdcolor.obj : jdcolor.c jinclude.h jconfig.h jpegdata.h
-
-jddeflts.obj : jddeflts.c jinclude.h jconfig.h jpegdata.h
-
-jdhuff.obj : jdhuff.c jinclude.h jconfig.h jpegdata.h
-
-jdmain.obj : jdmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-	cl $(CFLAGS) /c $*.c
-
-jdmaster.obj : jdmaster.c jinclude.h jconfig.h jpegdata.h
-
-jdmcu.obj : jdmcu.c jinclude.h jconfig.h jpegdata.h
-
-jdpipe.obj : jdpipe.c jinclude.h jconfig.h jpegdata.h
-
-jdsample.obj : jdsample.c jinclude.h jconfig.h jpegdata.h
-
-jerror.obj : jerror.c jinclude.h jconfig.h jpegdata.h
-
-jquant1.obj : jquant1.c jinclude.h jconfig.h jpegdata.h
-
-jquant2.obj : jquant2.c jinclude.h jconfig.h jpegdata.h
-
-jfwddct.obj : jfwddct.c jinclude.h jconfig.h jpegdata.h
-
-jrevdct.obj : jrevdct.c jinclude.h jconfig.h jpegdata.h
-
-jutils.obj : jutils.c jinclude.h jconfig.h jpegdata.h
-
-jmemmgr.obj : jmemmgr.c jinclude.h jconfig.h jpegdata.h jmemsys.h
-
-jrdjfif.obj : jrdjfif.c jinclude.h jconfig.h jpegdata.h
-
-jrdgif.obj : jrdgif.c jinclude.h jconfig.h jpegdata.h
-
-jrdppm.obj : jrdppm.c jinclude.h jconfig.h jpegdata.h
-
-jrdrle.obj : jrdrle.c jinclude.h jconfig.h jpegdata.h
-
-jrdtarga.obj : jrdtarga.c jinclude.h jconfig.h jpegdata.h
-
-jwrjfif.obj : jwrjfif.c jinclude.h jconfig.h jpegdata.h
-
-jwrgif.obj : jwrgif.c jinclude.h jconfig.h jpegdata.h
-
-jwrppm.obj : jwrppm.c jinclude.h jconfig.h jpegdata.h
-
-jwrrle.obj : jwrrle.c jinclude.h jconfig.h jpegdata.h
-
-jwrtarga.obj : jwrtarga.c jinclude.h jconfig.h jpegdata.h
-
-jmemsys.obj : jmemsys.c jinclude.h jconfig.h jpegdata.h jmemsys.h
-
-jmemdosa.obj : jmemdosa.asm
-
-
-cjpeg.exe: $(COBJECTS)
-	cl /Fecjpeg.exe jcmain.obj libjpeg.lib $(LDFLAGS)
-
-djpeg.exe: $(DOBJECTS)
-	cl /Fedjpeg.exe jdmain.obj libjpeg.lib $(LDFLAGS)
diff --git a/makefile.mc6 b/makefile.mc6
index eca1c07..8b800b7 100644
--- a/makefile.mc6
+++ b/makefile.mc6
@@ -4,108 +4,117 @@
 # Use NMAKE, not Microsoft's brain-damaged MAKE.
 # Thanks to Alan Wright and Chris Turner of Olivetti Research Ltd.
 
-# Read SETUP instructions before saying "nmake" !!
+# Read installation instructions before saying "nmake" !!
 
-# compiler flags. -D gives a #define to the sources:
-#       -AS             small memory model (or use -AM for medium model)
-#       -Ox             maximum safe optimisation
-#       -W3             warning level 3
-#       -Za             ANSI conformance, defines __STDC__ but undefines far
-#                       and near, so we DON'T use it.
-#       -DHAVE_STDC     indicate we do have all the ANSI language features
-#       -DINCLUDES_ARE_ANSI    and all the ANSI include files.
-#       -DMSDOS         we are on an MSDOS machine
-#       -DUSE_FMEM      we have _fmemcpy() and _fmemset()
-#       -DNEED_FHEAPMIN        our heap management routines are broken
-#       -DSHORTxLCONST_32      enables compiler-specific multiply optimization
-#       -DMEM_STATS     enable memory usage statistics (optional)
+# You may need to adjust these compiler options:
+CFLAGS = -AM -Oecigt -Gs -W3
+# -AM medium memory model (or use -AS for small model, if you remove features)
+# -Oecigt -Gs  maximum safe optimisation (-Ol has bugs in MSC 6.00A)
+# -W3 warning level 3
 # You might also want to add -G2 if you have an 80286, etc.
-# If you run up against DOS' 128-character limit on command line length,
-# you can get rid of some of the -D switches by adding equivalent #define
-# commands to the head of jinclude.h.
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
 
-CFLAGS = -AS -Ox -W3 -DHAVE_STDC -DINCLUDES_ARE_ANSI -DMSDOS -DUSE_FMEM -DNEED_FHEAPMIN -DSHORTxLCONST_32
+# Put here the object file name for the correct system-dependent memory
+# manager file.  For DOS, we recommend jmemdos.c and jmemdosa.asm.
+# If you change this, you'll need to modify the linker response file
+# name list, below, by hand!
+SYSDEPMEM= jmemdos.obj jmemdosa.obj
+
+# End of configurable options.
+
+
+# source files: JPEG library proper
+LIBSOURCES= jcapi.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcmainct.c \
+        jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c \
+        jdapi.c jdatasrc.c jdatadst.c jdcoefct.c jdcolor.c jddctmgr.c \
+        jdhuff.c jdmainct.c jdmarker.c jdmaster.c jdpostct.c jdsample.c \
+        jerror.c jutils.c jfdctfst.c jfdctflt.c jfdctint.c jidctfst.c \
+        jidctflt.c jidctint.c jidctred.c jquant1.c jquant2.c jdmerge.c \
+        jmemmgr.c jmemansi.c jmemname.c jmemnobs.c jmemdos.c
+# source files: cjpeg/djpeg applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c rdcolmap.c rdppm.c wrppm.c rdgif.c wrgif.c \
+        rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c rdjpgcom.c \
+        wrjpgcom.c
+SOURCES= $(LIBSOURCES) $(APPSOURCES)
+# files included by source files
+INCLUDES= jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h jpegint.h \
+        jpeglib.h jversion.h cdjpeg.h cderror.h
+# documentation, test, and support files
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 rdjpgcom.1 wrjpgcom.1 \
+        example.c libjpeg.doc structure.doc coderules.doc filelist.doc \
+        change.log
+MKFILES= configure makefile.auto makefile.ansi makefile.unix makefile.manx \
+        makefile.sas makcjpeg.st makdjpeg.st makljpeg.st makefile.bcc \
+        makefile.mc6 makefile.dj makefile.mms makefile.vms makvms.opt
+CONFIGFILES= jconfig.auto jconfig.manx jconfig.sas jconfig.st jconfig.bcc \
+        jconfig.mc6 jconfig.dj jconfig.vms
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
+TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
+        $(OTHERFILES) $(TESTFILES)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.obj jutils.obj jerror.obj jmemmgr.obj $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapi.obj jcparam.obj jdatadst.obj jcmaster.obj jcmarker.obj \
+        jcmainct.obj jcprepct.obj jccoefct.obj jccolor.obj jcsample.obj \
+        jchuff.obj jcdctmgr.obj jfdctfst.obj jfdctflt.obj jfdctint.obj
+# decompression library object files
+DLIBOBJECTS= jdapi.obj jdatasrc.obj jdmaster.obj jdmarker.obj jdmainct.obj \
+        jdcoefct.obj jdpostct.obj jddctmgr.obj jidctfst.obj jidctflt.obj \
+        jidctint.obj jidctred.obj jdhuff.obj jdsample.obj jdcolor.obj \
+        jquant1.obj jquant2.obj jdmerge.obj
+# These objectfiles are included in libjpeg.lib
+LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for cjpeg and djpeg applications (excluding library files)
+COBJECTS= cjpeg.obj rdppm.obj rdgif.obj rdtarga.obj rdrle.obj rdbmp.obj
+DOBJECTS= djpeg.obj wrppm.obj wrgif.obj wrtarga.obj wrrle.obj wrbmp.obj \
+        rdcolmap.obj
 
 # need linker response file because file list > 128 chars
 RFILE = libjpeg.ans
 
 
-# source files (independently compilable files)
-SOURCES= jbsmooth.c jcarith.c jccolor.c jcdeflts.c jcexpand.c jchuff.c \
-        jcmain.c jcmaster.c jcmcu.c jcpipe.c jcsample.c jdarith.c jdcolor.c \
-        jddeflts.c jdhuff.c jdmain.c jdmaster.c jdmcu.c jdpipe.c jdsample.c \
-        jerror.c jquant1.c jquant2.c jfwddct.c jrevdct.c jutils.c jmemmgr.c \
-        jrdjfif.c jrdgif.c jrdppm.c jrdrle.c jrdtarga.c jwrjfif.c jwrgif.c \
-        jwrppm.c jwrrle.c jwrtarga.c
-# virtual source files (not present in distribution file, see SETUP)
-VIRTSOURCES= jmemsys.c
-# system-dependent implementations of virtual source files
-SYSDEPFILES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemdos.h \
-        jmemdosa.asm
-# files included by source files
-INCLUDES= jinclude.h jconfig.h jpegdata.h jversion.h jmemsys.h
-# documentation, test, and support files
-DOCS= README SETUP USAGE CHANGELOG cjpeg.1 djpeg.1 architecture codingrules
-MAKEFILES= makefile.ansi makefile.unix makefile.manx makefile.sas \
-        makcjpeg.st makdjpeg.st makljpeg.st makefile.mc5 makefile.mc6 \
-        makefile.bcc makefile.icc makljpeg.icc makefile.mms makefile.vms \
-        makvms.opt
-OTHERFILES= ansi2knr.c ckconfig.c example.c
-TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
-DISTFILES= $(DOCS) $(MAKEFILES) $(SOURCES) $(SYSDEPFILES) $(INCLUDES) \
-        $(OTHERFILES) $(TESTFILES)
-# objectfiles common to cjpeg and djpeg
-COMOBJECTS= jutils.obj jerror.obj jmemmgr.obj jmemsys.obj jmemdosa.obj
-# compression objectfiles
-CLIBOBJECTS= jcmaster.obj jcdeflts.obj jcarith.obj jccolor.obj jcexpand.obj \
-        jchuff.obj jcmcu.obj jcpipe.obj jcsample.obj jfwddct.obj \
-        jwrjfif.obj jrdgif.obj jrdppm.obj jrdrle.obj jrdtarga.obj
-COBJECTS= jcmain.obj $(CLIBOBJECTS) $(COMOBJECTS)
-# decompression objectfiles
-DLIBOBJECTS= jdmaster.obj jddeflts.obj jbsmooth.obj jdarith.obj jdcolor.obj \
-        jdhuff.obj jdmcu.obj jdpipe.obj jdsample.obj jquant1.obj \
-        jquant2.obj jrevdct.obj jrdjfif.obj jwrgif.obj jwrppm.obj \
-        jwrrle.obj jwrtarga.obj
-DOBJECTS= jdmain.obj $(DLIBOBJECTS) $(COMOBJECTS)
-# These objectfiles are included in libjpeg.lib
-LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+all: libjpeg.lib cjpeg.exe djpeg.exe rdjpgcom.exe wrjpgcom.exe
 
-
-all: cjpeg.exe djpeg.exe
-
-
-# libjpeg.lib is useful if you are including the JPEG software in a larger
-# program; you'd include it in your link, rather than the individual modules.
 libjpeg.lib: $(LIBOBJECTS) $(RFILE)
-    del libjpeg.lib
-    lib @$(RFILE) ;
+	del libjpeg.lib
+	lib @$(RFILE) ;
 
-# linker response file for same
+# linker response file for building libjpeg.lib
 $(RFILE) : Makefile
-    del $(RFILE)
-    echo libjpeg.lib >$(RFILE)
+	del $(RFILE)
+	echo libjpeg.lib >$(RFILE)
 # silly want-to-create-it prompt:
-    echo y >>$(RFILE)
-    echo +jcmaster.obj +jcdeflts.obj +jcarith.obj +jccolor.obj & >>$(RFILE)
-    echo +jcexpand.obj +jchuff.obj +jcmcu.obj +jcpipe.obj & >>$(RFILE)
-    echo +jcsample.obj +jfwddct.obj +jwrjfif.obj +jrdgif.obj & >>$(RFILE)
-    echo +jrdppm.obj +jrdrle.obj +jrdtarga.obj +jdmaster.obj & >>$(RFILE)
-    echo +jddeflts.obj +jbsmooth.obj +jdarith.obj +jdcolor.obj & >>$(RFILE)
-    echo +jdhuff.obj +jdmcu.obj +jdpipe.obj +jdsample.obj & >>$(RFILE)
-    echo +jquant1.obj +jquant2.obj +jrevdct.obj +jrdjfif.obj & >>$(RFILE)
-    echo +jwrgif.obj +jwrppm.obj +jwrrle.obj +jwrtarga.obj & >>$(RFILE)
-    echo +jutils.obj +jerror.obj +jmemmgr.obj +jmemsys.obj & >>$(RFILE)
-    echo +jmemdosa.obj >>$(RFILE)
+	echo y >>$(RFILE)
+	echo +jcapi.obj +jcparam.obj +jdatadst.obj +jcmaster.obj & >>$(RFILE)
+	echo +jcmarker.obj +jcmainct.obj +jcprepct.obj & >>$(RFILE)
+	echo +jccoefct.obj +jccolor.obj +jcsample.obj +jchuff.obj & >>$(RFILE)
+	echo +jcdctmgr.obj +jfdctfst.obj +jfdctflt.obj & >>$(RFILE)
+	echo +jfdctint.obj +jdapi.obj +jdatasrc.obj +jdmaster.obj & >>$(RFILE)
+	echo +jdmarker.obj +jdmainct.obj +jdcoefct.obj & >>$(RFILE)
+	echo +jdpostct.obj +jddctmgr.obj +jidctfst.obj & >>$(RFILE)
+	echo +jidctflt.obj +jidctint.obj +jidctred.obj +jdhuff.obj & >>$(RFILE)
+	echo +jdsample.obj +jdcolor.obj +jquant1.obj +jquant2.obj & >>$(RFILE)
+	echo +jdmerge.obj +jcomapi.obj +jutils.obj +jerror.obj & >>$(RFILE)
+	echo +jmemmgr.obj +jmemdos.obj +jmemdosa.obj >>$(RFILE)
 
-cjpeg.exe: jcmain.obj libjpeg.lib
-    link /STACK:4096 /EXEPACK jcmain.obj, cjpeg.exe, , libjpeg.lib, ;
+cjpeg.exe: $(COBJECTS) libjpeg.lib
+	link /STACK:4096 /EXEPACK $(COBJECTS), cjpeg.exe, , libjpeg.lib, ;
 
-djpeg.exe: jdmain.obj libjpeg.lib
-    link /STACK:4096 /EXEPACK jdmain.obj, djpeg.exe, , libjpeg.lib, ;
+djpeg.exe: $(DOBJECTS) libjpeg.lib
+	link /STACK:4096 /EXEPACK $(DOBJECTS), djpeg.exe, , libjpeg.lib, ;
 
-jmemsys.c:
-	echo You must select a system-dependent jmemsys.c file.
-	echo Please read the SETUP directions.
+rdjpgcom.exe: rdjpgcom.c
+	$(CC) -AS -O -W3 rdjpgcom.c
+
+# wrjpgcom needs large model so it can malloc a 64K chunk
+wrjpgcom.exe: wrjpgcom.c
+	$(CC) -AL -O -W3 wrjpgcom.c
+
+jconfig.h: jconfig.doc
+	echo You must prepare a system-dependent jconfig.h file.
+	echo Please read the installation directions in install.doc.
 	exit 1
 
 clean:
@@ -113,55 +122,75 @@
 	del libjpeg.lib
 	del cjpeg.exe
 	del djpeg.exe
+	del rdjpgcom.exe
+	del wrjpgcom.exe
 	del testout.*
 
-test:
-        del testout.*
-	djpeg -outfile testout.ppm  testorig.jpg
-	djpeg -gif -outfile testout.gif  testorig.jpg
-	cjpeg -outfile testout.jpg  testimg.ppm
-        fc /b testimg.ppm testout.ppm
-        fc /b testimg.gif testout.gif
-        fc /b testimg.jpg testout.jpg
+test: cjpeg.exe djpeg.exe
+	del testout.*
+	djpeg -dct int -ppm -outfile testout.ppm  testorig.jpg
+	djpeg -dct int -gif -outfile testout.gif  testorig.jpg
+	cjpeg -dct int -outfile testout.jpg  testimg.ppm
+	fc /b testimg.ppm testout.ppm
+	fc /b testimg.gif testout.gif
+	fc /b testimg.jpg testout.jpg
 
 
-jbsmooth.obj : jbsmooth.c jinclude.h jconfig.h jpegdata.h
-jcarith.obj : jcarith.c jinclude.h jconfig.h jpegdata.h
-jccolor.obj : jccolor.c jinclude.h jconfig.h jpegdata.h
-jcdeflts.obj : jcdeflts.c jinclude.h jconfig.h jpegdata.h
-jcexpand.obj : jcexpand.c jinclude.h jconfig.h jpegdata.h
-jchuff.obj : jchuff.c jinclude.h jconfig.h jpegdata.h
-jcmain.obj : jcmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-jcmaster.obj : jcmaster.c jinclude.h jconfig.h jpegdata.h
-jcmcu.obj : jcmcu.c jinclude.h jconfig.h jpegdata.h
-jcpipe.obj : jcpipe.c jinclude.h jconfig.h jpegdata.h
-jcsample.obj : jcsample.c jinclude.h jconfig.h jpegdata.h
-jdarith.obj : jdarith.c jinclude.h jconfig.h jpegdata.h
-jdcolor.obj : jdcolor.c jinclude.h jconfig.h jpegdata.h
-jddeflts.obj : jddeflts.c jinclude.h jconfig.h jpegdata.h
-jdhuff.obj : jdhuff.c jinclude.h jconfig.h jpegdata.h
-jdmain.obj : jdmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-jdmaster.obj : jdmaster.c jinclude.h jconfig.h jpegdata.h
-jdmcu.obj : jdmcu.c jinclude.h jconfig.h jpegdata.h
-jdpipe.obj : jdpipe.c jinclude.h jconfig.h jpegdata.h
-jdsample.obj : jdsample.c jinclude.h jconfig.h jpegdata.h
-jerror.obj : jerror.c jinclude.h jconfig.h jpegdata.h
-jquant1.obj : jquant1.c jinclude.h jconfig.h jpegdata.h
-jquant2.obj : jquant2.c jinclude.h jconfig.h jpegdata.h
-jfwddct.obj : jfwddct.c jinclude.h jconfig.h jpegdata.h
-jrevdct.obj : jrevdct.c jinclude.h jconfig.h jpegdata.h
-jutils.obj : jutils.c jinclude.h jconfig.h jpegdata.h
-jmemmgr.obj : jmemmgr.c jinclude.h jconfig.h jpegdata.h jmemsys.h
-jrdjfif.obj : jrdjfif.c jinclude.h jconfig.h jpegdata.h
-jrdgif.obj : jrdgif.c jinclude.h jconfig.h jpegdata.h
-jrdppm.obj : jrdppm.c jinclude.h jconfig.h jpegdata.h
-jrdrle.obj : jrdrle.c jinclude.h jconfig.h jpegdata.h
-jrdtarga.obj : jrdtarga.c jinclude.h jconfig.h jpegdata.h
-jwrjfif.obj : jwrjfif.c jinclude.h jconfig.h jpegdata.h
-jwrgif.obj : jwrgif.c jinclude.h jconfig.h jpegdata.h
-jwrppm.obj : jwrppm.c jinclude.h jconfig.h jpegdata.h
-jwrrle.obj : jwrrle.c jinclude.h jconfig.h jpegdata.h
-jwrtarga.obj : jwrtarga.c jinclude.h jconfig.h jpegdata.h
-jmemsys.obj : jmemsys.c jinclude.h jconfig.h jpegdata.h jmemsys.h
+jcapi.obj : jcapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.obj : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.obj : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.obj : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.obj : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.obj : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.obj : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.obj : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.obj : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.obj : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcprepct.obj : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.obj : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapi.obj : jdapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatasrc.obj : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatadst.obj : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.obj : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.obj : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.obj : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.obj : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.obj : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.obj : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.obj : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdpostct.obj : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.obj : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.obj : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jutils.obj : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jfdctfst.obj : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctflt.obj : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.obj : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.obj : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.obj : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.obj : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.obj : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.obj : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.obj : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.obj : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.obj : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.obj : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.obj : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.obj : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.obj : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.obj : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.obj : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+rdcolmap.obj : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdppm.obj : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.obj : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.obj : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.obj : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.obj : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.obj : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.obj : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.obj : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.obj : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.obj : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdjpgcom.obj : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.obj : wrjpgcom.c jinclude.h jconfig.h
 jmemdosa.obj : jmemdosa.asm
-    masm /mx $*;
+	masm /mx $*;
diff --git a/makefile.mms b/makefile.mms
index 57818b6..0866c60 100644
--- a/makefile.mms
+++ b/makefile.mms
@@ -1,93 +1,113 @@
 # Makefile for Independent JPEG Group's software
 
-# This makefile is for use with MMS on VAX/VMS systems.
+# This makefile is for use with MMS on Digital VMS systems.
 # Thanks to Rick Dyson (dyson@iowasp.physics.uiowa.edu)
 # and Tim Bell (tbell@netcom.com) for their help.
 
-# Read SETUP instructions before saying "MMS" !!
+# Read installation instructions before saying "MMS" !!
 
-CFLAGS= $(CFLAGS) /NoDebug /Optimize /Define = (TWO_FILE_COMMANDLINE,HAVE_STDC,INCLUDES_ARE_ANSI)
-OPT= Sys$Disk:[]MAKVMS.OPT
+# You may need to adjust these cc options:
+CFLAGS= $(CFLAGS) /NoDebug /Optimize
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via /Define switches here.
+.ifdef ALPHA
+OPT=
+.else
+OPT= ,Sys$Disk:[]MAKVMS.OPT/Option
+.endif
+
+# Put here the object file name for the correct system-dependent memory
+# manager file.  For Unix this is usually jmemnobs.o, but you may want
+# to use jmemansi.o or jmemname.o if you have limited swap space.
+SYSDEPMEM= jmemnobs.obj
+
+# End of configurable options.
 
 
-# source files (independently compilable files)
-SOURCES= jbsmooth.c jcarith.c jccolor.c jcdeflts.c jcexpand.c jchuff.c \
-        jcmain.c jcmaster.c jcmcu.c jcpipe.c jcsample.c jdarith.c jdcolor.c \
-        jddeflts.c jdhuff.c jdmain.c jdmaster.c jdmcu.c jdpipe.c jdsample.c \
-        jerror.c jquant1.c jquant2.c jfwddct.c jrevdct.c jutils.c jmemmgr.c \
-        jrdjfif.c jrdgif.c jrdppm.c jrdrle.c jrdtarga.c jwrjfif.c jwrgif.c \
-        jwrppm.c jwrrle.c jwrtarga.c
-# virtual source files (not present in distribution file, see SETUP)
-VIRTSOURCES= jmemsys.c
-# system-dependent implementations of virtual source files
-SYSDEPFILES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemdos.h \
-        jmemdosa.asm
+# source files: JPEG library proper
+LIBSOURCES= jcapi.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcmainct.c \
+        jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c \
+        jdapi.c jdatasrc.c jdatadst.c jdcoefct.c jdcolor.c jddctmgr.c \
+        jdhuff.c jdmainct.c jdmarker.c jdmaster.c jdpostct.c jdsample.c \
+        jerror.c jutils.c jfdctfst.c jfdctflt.c jfdctint.c jidctfst.c \
+        jidctflt.c jidctint.c jidctred.c jquant1.c jquant2.c jdmerge.c \
+        jmemmgr.c jmemansi.c jmemname.c jmemnobs.c jmemdos.c
+# source files: cjpeg/djpeg applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c rdcolmap.c rdppm.c wrppm.c rdgif.c wrgif.c \
+        rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c rdjpgcom.c \
+        wrjpgcom.c
+SOURCES= $(LIBSOURCES) $(APPSOURCES)
 # files included by source files
-INCLUDES= jinclude.h jconfig.h jpegdata.h jversion.h jmemsys.h
+INCLUDES= jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h jpegint.h \
+        jpeglib.h jversion.h cdjpeg.h cderror.h
 # documentation, test, and support files
-DOCS= README SETUP USAGE CHANGELOG cjpeg.1 djpeg.1 architecture codingrules
-MAKEFILES= makefile.ansi makefile.unix makefile.manx makefile.sas \
-        makcjpeg.st makdjpeg.st makljpeg.st makefile.mc5 makefile.mc6 \
-        makefile.bcc makefile.icc makljpeg.icc makefile.mms makefile.vms \
-        makvms.opt
-OTHERFILES= ansi2knr.c ckconfig.c example.c
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 rdjpgcom.1 wrjpgcom.1 \
+        example.c libjpeg.doc structure.doc coderules.doc filelist.doc \
+        change.log
+MKFILES= configure makefile.auto makefile.ansi makefile.unix makefile.manx \
+        makefile.sas makcjpeg.st makdjpeg.st makljpeg.st makefile.bcc \
+        makefile.mc6 makefile.dj makefile.mms makefile.vms makvms.opt
+CONFIGFILES= jconfig.auto jconfig.manx jconfig.sas jconfig.st jconfig.bcc \
+        jconfig.mc6 jconfig.dj jconfig.vms
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
 TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
-DISTFILES= $(DOCS) $(MAKEFILES) $(SOURCES) $(SYSDEPFILES) $(INCLUDES) \
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
         $(OTHERFILES) $(TESTFILES)
-# objectfiles common to cjpeg and djpeg
-COMOBJECTS= jutils.obj jerror.obj jmemmgr.obj jmemsys.obj
-# compression objectfiles
-CLIBOBJECTS= jcmaster.obj jcdeflts.obj jcarith.obj jccolor.obj jcexpand.obj \
-        jchuff.obj jcmcu.obj jcpipe.obj jcsample.obj jfwddct.obj \
-        jwrjfif.obj jrdgif.obj jrdppm.obj jrdrle.obj jrdtarga.obj
-COBJECTS= jcmain.obj $(CLIBOBJECTS) $(COMOBJECTS)
-# decompression objectfiles
-DLIBOBJECTS= jdmaster.obj jddeflts.obj jbsmooth.obj jdarith.obj jdcolor.obj \
-        jdhuff.obj jdmcu.obj jdpipe.obj jdsample.obj jquant1.obj \
-        jquant2.obj jrevdct.obj jrdjfif.obj jwrgif.obj jwrppm.obj \
-        jwrrle.obj jwrtarga.obj
-DOBJECTS= jdmain.obj $(DLIBOBJECTS) $(COMOBJECTS)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.obj jutils.obj jerror.obj jmemmgr.obj $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapi.obj jcparam.obj jdatadst.obj jcmaster.obj jcmarker.obj \
+        jcmainct.obj jcprepct.obj jccoefct.obj jccolor.obj jcsample.obj \
+        jchuff.obj jcdctmgr.obj jfdctfst.obj jfdctflt.obj jfdctint.obj
+# decompression library object files
+DLIBOBJECTS= jdapi.obj jdatasrc.obj jdmaster.obj jdmarker.obj jdmainct.obj \
+        jdcoefct.obj jdpostct.obj jddctmgr.obj jidctfst.obj jidctflt.obj \
+        jidctint.obj jidctred.obj jdhuff.obj jdsample.obj jdcolor.obj \
+        jquant1.obj jquant2.obj jdmerge.obj
 # These objectfiles are included in libjpeg.olb
 LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for cjpeg and djpeg applications (excluding library files)
+COBJECTS= cjpeg.obj rdppm.obj rdgif.obj rdtarga.obj rdrle.obj rdbmp.obj
+DOBJECTS= djpeg.obj wrppm.obj wrgif.obj wrtarga.obj wrrle.obj wrbmp.obj \
+        rdcolmap.obj
 # objectfile lists with commas --- what a crock
-COBJLIST= jcmain.obj,jcmaster.obj,jcdeflts.obj,jcarith.obj,jccolor.obj,\
-          jcexpand.obj,jchuff.obj,jcmcu.obj,jcpipe.obj,jcsample.obj,\
-          jfwddct.obj,jwrjfif.obj,jrdgif.obj,jrdppm.obj,jrdrle.obj,\
-          jrdtarga.obj,jutils.obj,jerror.obj,jmemmgr.obj,jmemsys.obj
-DOBJLIST= jdmain.obj,jdmaster.obj,jddeflts.obj,jbsmooth.obj,jdarith.obj,\
-          jdcolor.obj,jdhuff.obj,jdmcu.obj,jdpipe.obj,jdsample.obj,\
-          jquant1.obj,jquant2.obj,jrevdct.obj,jrdjfif.obj,jwrgif.obj,\
-          jwrppm.obj,jwrrle.obj,jwrtarga.obj,jutils.obj,jerror.obj,\
-          jmemmgr.obj,jmemsys.obj
-LIBOBJLIST= jcmaster.obj,jcdeflts.obj,jcarith.obj,jccolor.obj,jcexpand.obj,\
-          jchuff.obj,jcmcu.obj,jcpipe.obj,jcsample.obj,jfwddct.obj,\
-          jwrjfif.obj,jrdgif.obj,jrdppm.obj,jrdrle.obj,jrdtarga.obj,\
-          jdmaster.obj,jddeflts.obj,jbsmooth.obj,jdarith.obj,jdcolor.obj,\
-          jdhuff.obj,jdmcu.obj,jdpipe.obj,jdsample.obj,jquant1.obj,\
-          jquant2.obj,jrevdct.obj,jrdjfif.obj,jwrgif.obj,jwrppm.obj,\
-          jwrrle.obj,jwrtarga.obj,jutils.obj,jerror.obj,jmemmgr.obj,\
-          jmemsys.obj
+COBJLIST= cjpeg.obj,rdppm.obj,rdgif.obj,rdtarga.obj,rdrle.obj,rdbmp.obj
+DOBJLIST= djpeg.obj,wrppm.obj,wrgif.obj,wrtarga.obj,wrrle.obj,wrbmp.obj,\
+          rdcolmap.obj
+LIBOBJLIST= jcapi.obj,jcparam.obj,jdatadst.obj,jcmaster.obj,jcmarker.obj,\
+          jcmainct.obj,jcprepct.obj,jccoefct.obj,jccolor.obj,jcsample.obj,\
+          jchuff.obj,jcdctmgr.obj,jfdctfst.obj,jfdctflt.obj,jfdctint.obj,\
+          jdapi.obj,jdatasrc.obj,jdmaster.obj,jdmarker.obj,jdmainct.obj,\
+          jdcoefct.obj,jdpostct.obj,jddctmgr.obj,jidctfst.obj,jidctflt.obj,\
+          jidctint.obj,jidctred.obj,jdhuff.obj,jdsample.obj,jdcolor.obj,\
+          jquant1.obj,jquant2.obj,jdmerge.obj,jcomapi.obj,jutils.obj,\
+          jerror.obj,jmemmgr.obj,$(SYSDEPMEM)
 
 
 .first
-	@ Define Sys Sys$Library
+	@- Define /NoLog Sys Sys$Library
 
-# By default, libjpeg.olb is not built unless you explicitly request it.
-# You can add libjpeg.olb to the next line if you want it built by default.
-ALL : cjpeg.exe djpeg.exe
+ALL : libjpeg.olb cjpeg.exe djpeg.exe rdjpgcom.exe wrjpgcom.exe
 	@ Continue
 
-cjpeg.exe : $(COBJECTS)
-	$(LINK) $(LFLAGS) /Executable = cjpeg.exe $(COBJLIST),$(OPT)/Option
-
-djpeg.exe : $(DOBJECTS)
-	$(LINK) $(LFLAGS) /Executable = djpeg.exe $(DOBJLIST),$(OPT)/Option
-
-# libjpeg.olb is useful if you are including the JPEG software in a larger
-# program; you'd include it in your link, rather than the individual modules.
 libjpeg.olb : $(LIBOBJECTS)
 	Library /Create libjpeg.olb $(LIBOBJLIST)
 
+cjpeg.exe : $(COBJECTS) libjpeg.olb
+	$(LINK) $(LFLAGS) /Executable = cjpeg.exe $(COBJLIST),libjpeg.olb/Library$(OPT)
+
+djpeg.exe : $(DOBJECTS) libjpeg.olb
+	$(LINK) $(LFLAGS) /Executable = djpeg.exe $(DOBJLIST),libjpeg.olb/Library$(OPT)
+
+rdjpgcom.exe : rdjpgcom.obj
+	$(LINK) $(LFLAGS) /Executable = rdjpgcom.exe rdjpgcom.obj$(OPT)
+
+wrjpgcom.exe : wrjpgcom.obj
+	$(LINK) $(LFLAGS) /Executable = wrjpgcom.exe wrjpgcom.obj$(OPT)
+
+jconfig.h : jconfig.vms
+	@- Copy jconfig.vms jconfig.h
+
 clean :
 	@- Set Protection = Owner:RWED *.*;-1
 	@- Set Protection = Owner:RWED *.OBJ
@@ -95,49 +115,67 @@
 	- Delete /NoLog /NoConfirm *.OBJ;
 
 test : cjpeg.exe djpeg.exe
-	mcr sys$disk:[]djpeg      testorig.jpg testout.ppm
-	mcr sys$disk:[]djpeg -gif testorig.jpg testout.gif
-	mcr sys$disk:[]cjpeg      testimg.ppm testout.jpg
+	mcr sys$disk:[]djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+	mcr sys$disk:[]djpeg -dct int -gif -outfile testout.gif testorig.jpg
+	mcr sys$disk:[]cjpeg -dct int      -outfile testout.jpg testimg.ppm
 	- Backup /Compare/Log	  testimg.ppm testout.ppm
 	- Backup /Compare/Log	  testimg.gif testout.gif
 	- Backup /Compare/Log	  testimg.jpg testout.jpg
 
 
-jbsmooth.obj : jbsmooth.c jinclude.h jconfig.h jpegdata.h
-jcarith.obj : jcarith.c jinclude.h jconfig.h jpegdata.h
-jccolor.obj : jccolor.c jinclude.h jconfig.h jpegdata.h
-jcdeflts.obj : jcdeflts.c jinclude.h jconfig.h jpegdata.h
-jcexpand.obj : jcexpand.c jinclude.h jconfig.h jpegdata.h
-jchuff.obj : jchuff.c jinclude.h jconfig.h jpegdata.h
-jcmain.obj : jcmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-jcmaster.obj : jcmaster.c jinclude.h jconfig.h jpegdata.h
-jcmcu.obj : jcmcu.c jinclude.h jconfig.h jpegdata.h
-jcpipe.obj : jcpipe.c jinclude.h jconfig.h jpegdata.h
-jcsample.obj : jcsample.c jinclude.h jconfig.h jpegdata.h
-jdarith.obj : jdarith.c jinclude.h jconfig.h jpegdata.h
-jdcolor.obj : jdcolor.c jinclude.h jconfig.h jpegdata.h
-jddeflts.obj : jddeflts.c jinclude.h jconfig.h jpegdata.h
-jdhuff.obj : jdhuff.c jinclude.h jconfig.h jpegdata.h
-jdmain.obj : jdmain.c jinclude.h jconfig.h jpegdata.h jversion.h
-jdmaster.obj : jdmaster.c jinclude.h jconfig.h jpegdata.h
-jdmcu.obj : jdmcu.c jinclude.h jconfig.h jpegdata.h
-jdpipe.obj : jdpipe.c jinclude.h jconfig.h jpegdata.h
-jdsample.obj : jdsample.c jinclude.h jconfig.h jpegdata.h
-jerror.obj : jerror.c jinclude.h jconfig.h jpegdata.h
-jquant1.obj : jquant1.c jinclude.h jconfig.h jpegdata.h
-jquant2.obj : jquant2.c jinclude.h jconfig.h jpegdata.h
-jfwddct.obj : jfwddct.c jinclude.h jconfig.h jpegdata.h
-jrevdct.obj : jrevdct.c jinclude.h jconfig.h jpegdata.h
-jutils.obj : jutils.c jinclude.h jconfig.h jpegdata.h
-jmemmgr.obj : jmemmgr.c jinclude.h jconfig.h jpegdata.h jmemsys.h
-jrdjfif.obj : jrdjfif.c jinclude.h jconfig.h jpegdata.h
-jrdgif.obj : jrdgif.c jinclude.h jconfig.h jpegdata.h
-jrdppm.obj : jrdppm.c jinclude.h jconfig.h jpegdata.h
-jrdrle.obj : jrdrle.c jinclude.h jconfig.h jpegdata.h
-jrdtarga.obj : jrdtarga.c jinclude.h jconfig.h jpegdata.h
-jwrjfif.obj : jwrjfif.c jinclude.h jconfig.h jpegdata.h
-jwrgif.obj : jwrgif.c jinclude.h jconfig.h jpegdata.h
-jwrppm.obj : jwrppm.c jinclude.h jconfig.h jpegdata.h
-jwrrle.obj : jwrrle.c jinclude.h jconfig.h jpegdata.h
-jwrtarga.obj : jwrtarga.c jinclude.h jconfig.h jpegdata.h
-jmemsys.obj : jmemsys.c jinclude.h jconfig.h jpegdata.h jmemsys.h
+jcapi.obj : jcapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.obj : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.obj : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.obj : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.obj : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.obj : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.obj : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.obj : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.obj : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.obj : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcprepct.obj : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.obj : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapi.obj : jdapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatasrc.obj : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatadst.obj : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.obj : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.obj : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.obj : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.obj : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.obj : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.obj : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.obj : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdpostct.obj : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.obj : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.obj : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jutils.obj : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jfdctfst.obj : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctflt.obj : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.obj : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.obj : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.obj : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.obj : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.obj : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.obj : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.obj : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.obj : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.obj : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.obj : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.obj : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.obj : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.obj : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.obj : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.obj : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+rdcolmap.obj : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdppm.obj : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.obj : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.obj : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.obj : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.obj : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.obj : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.obj : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.obj : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.obj : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.obj : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdjpgcom.obj : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.obj : wrjpgcom.c jinclude.h jconfig.h
diff --git a/makefile.sas b/makefile.sas
index 7da8570..f9b852c 100644
--- a/makefile.sas
+++ b/makefile.sas
@@ -3,12 +3,12 @@
 # This makefile is for Amiga systems using SAS C 6.0 and up.
 # Thanks to Ed Hanway, Mark Rinfret, and Jim Zepeda.
 
-# Read SETUP instructions before saying "make" !!
-# Use jmemname.c as the system-dependent memory manager.
+# Read installation instructions before saying "make" !!
 
 # The name of your C compiler:
 CC= sc
 
+# You may need to adjust these cc options:
 # Uncomment the following lines for generic 680x0 version
 ARCHFLAGS= cpu=any
 SUFFIX=
@@ -17,21 +17,23 @@
 #ARCHFLAGS= cpu=68030
 #SUFFIX=.030
 
-# You may need to adjust these cc options:
 CFLAGS= nostackcheck data=near parms=register optimize $(ARCHFLAGS) \
-	ignore=104 ignore=304 ignore=306 \
-	define HAVE_STDC define INCLUDES_ARE_ANSI \
-	define AMIGA define TWO_FILE_COMMANDLINE \
-	define NO_MKTEMP define NEED_SIGNAL_CATCHER define SHORTxSHORT_32
+	ignore=104 ignore=304 ignore=306
 # ignore=104 disables warnings for mismatched const qualifiers
 # ignore=304 disables warnings for variables being optimized out
 # ignore=306 disables warnings for the inlining of functions
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via define switches here.
 
 # Link-time cc options:
 LDFLAGS= SC SD ND BATCH
 
 # To link any special libraries, add the necessary commands here.
-LDLIBS= LIB LIB:scm.lib LIB:sc.lib
+LDLIBS= LIB:scm.lib LIB:sc.lib
+
+# Put here the object file name for the correct system-dependent memory
+# manager file.  For Amiga we recommend jmemname.o.
+SYSDEPMEM= jmemname.o
 
 # miscellaneous OS-dependent stuff
 # linker
@@ -41,132 +43,165 @@
 # library (.lib) file creation command
 AR= oml
 
+# End of configurable options.
 
-# source files (independently compilable files)
-SOURCES= jbsmooth.c jcarith.c jccolor.c jcdeflts.c jcexpand.c jchuff.c \
-        jcmain.c jcmaster.c jcmcu.c jcpipe.c jcsample.c jdarith.c jdcolor.c \
-        jddeflts.c jdhuff.c jdmain.c jdmaster.c jdmcu.c jdpipe.c jdsample.c \
-        jerror.c jquant1.c jquant2.c jfwddct.c jrevdct.c jutils.c jmemmgr.c \
-        jrdjfif.c jrdgif.c jrdppm.c jrdrle.c jrdtarga.c jwrjfif.c jwrgif.c \
-        jwrppm.c jwrrle.c jwrtarga.c
-# virtual source files (not present in distribution file, see SETUP)
-VIRTSOURCES= jmemsys.c
-# system-dependent implementations of virtual source files
-SYSDEPFILES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemdos.h \
-        jmemdosa.asm
+
+# source files: JPEG library proper
+LIBSOURCES= jcapi.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcmainct.c \
+        jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c \
+        jdapi.c jdatasrc.c jdatadst.c jdcoefct.c jdcolor.c jddctmgr.c \
+        jdhuff.c jdmainct.c jdmarker.c jdmaster.c jdpostct.c jdsample.c \
+        jerror.c jutils.c jfdctfst.c jfdctflt.c jfdctint.c jidctfst.c \
+        jidctflt.c jidctint.c jidctred.c jquant1.c jquant2.c jdmerge.c \
+        jmemmgr.c jmemansi.c jmemname.c jmemnobs.c jmemdos.c
+# source files: cjpeg/djpeg applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c rdcolmap.c rdppm.c wrppm.c rdgif.c wrgif.c \
+        rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c rdjpgcom.c \
+        wrjpgcom.c
+SOURCES= $(LIBSOURCES) $(APPSOURCES)
 # files included by source files
-INCLUDES= jinclude.h jconfig.h jpegdata.h jversion.h jmemsys.h
+INCLUDES= jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h jpegint.h \
+        jpeglib.h jversion.h cdjpeg.h cderror.h
 # documentation, test, and support files
-DOCS= README SETUP USAGE CHANGELOG cjpeg.1 djpeg.1 architecture codingrules
-MAKEFILES= makefile.ansi makefile.unix makefile.manx makefile.sas \
-        makcjpeg.st makdjpeg.st makljpeg.st makefile.mc5 makefile.mc6 \
-        makefile.bcc makefile.icc makljpeg.icc makefile.mms makefile.vms \
-        makvms.opt
-OTHERFILES= ansi2knr.c ckconfig.c example.c
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 rdjpgcom.1 wrjpgcom.1 \
+        example.c libjpeg.doc structure.doc coderules.doc filelist.doc \
+        change.log
+MKFILES= configure makefile.auto makefile.ansi makefile.unix makefile.manx \
+        makefile.sas makcjpeg.st makdjpeg.st makljpeg.st makefile.bcc \
+        makefile.mc6 makefile.dj makefile.mms makefile.vms makvms.opt
+CONFIGFILES= jconfig.auto jconfig.manx jconfig.sas jconfig.st jconfig.bcc \
+        jconfig.mc6 jconfig.dj jconfig.vms
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
 TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
-DISTFILES= $(DOCS) $(MAKEFILES) $(SOURCES) $(SYSDEPFILES) $(INCLUDES) \
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
         $(OTHERFILES) $(TESTFILES)
-# objectfiles common to cjpeg and djpeg
-COMOBJECTS= jutils.o jerror.o jmemmgr.o jmemsys.o
-# compression objectfiles
-CLIBOBJECTS= jcmaster.o jcdeflts.o jcarith.o jccolor.o jcexpand.o jchuff.o \
-        jcmcu.o jcpipe.o jcsample.o jfwddct.o jwrjfif.o jrdgif.o jrdppm.o \
-        jrdrle.o jrdtarga.o
-COBJECTS= jcmain.o $(CLIBOBJECTS) $(COMOBJECTS)
-# decompression objectfiles
-DLIBOBJECTS= jdmaster.o jddeflts.o jbsmooth.o jdarith.o jdcolor.o jdhuff.o \
-        jdmcu.o jdpipe.o jdsample.o jquant1.o jquant2.o jrevdct.o jrdjfif.o \
-        jwrgif.o jwrppm.o jwrrle.o jwrtarga.o
-DOBJECTS= jdmain.o $(DLIBOBJECTS) $(COMOBJECTS)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapi.o jcparam.o jdatadst.o jcmaster.o jcmarker.o jcmainct.o \
+        jcprepct.o jccoefct.o jccolor.o jcsample.o jchuff.o jcdctmgr.o \
+        jfdctfst.o jfdctflt.o jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapi.o jdatasrc.o jdmaster.o jdmarker.o jdmainct.o jdcoefct.o \
+        jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+        jdhuff.o jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
 # These objectfiles are included in libjpeg.lib
 LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for cjpeg and djpeg applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o
 
 
-all: cjpeg$(SUFFIX) djpeg$(SUFFIX)
-# By default, libjpeg.lib is not built unless you explicitly request it.
-# You can add libjpeg.lib to the line above if you want it built by default.
+all: libjpeg.lib cjpeg$(SUFFIX) djpeg$(SUFFIX) rdjpgcom$(SUFFIX) wrjpgcom$(SUFFIX)
 
-
-cjpeg$(SUFFIX): $(COBJECTS)
-	$(LN) <WITH <
-$(LDFLAGS)
-TO cjpeg$(SUFFIX)
-FROM LIB:c.o $(COBJECTS)
-$(LDLIBS)
-<
-
-djpeg$(SUFFIX): $(DOBJECTS)
-	$(LN) <WITH <
-$(LDFLAGS)
-TO djpeg$(SUFFIX)
-FROM LIB:c.o $(DOBJECTS)
-$(LDLIBS)
-<
-
-# libjpeg.lib is useful if you are including the JPEG software in a larger
-# program; you'd include it in your link, rather than the individual modules.
 libjpeg.lib: $(LIBOBJECTS)
 	-$(RM) libjpeg.lib
 	$(AR) libjpeg.lib r $(LIBOBJECTS)
 
-jmemsys.c:
-	echo You must select a system-dependent jmemsys.c file.
-	echo Please read the SETUP directions.
+cjpeg$(SUFFIX): $(COBJECTS) libjpeg.lib
+	$(LN) <WITH <
+$(LDFLAGS)
+TO cjpeg$(SUFFIX)
+FROM LIB:c.o $(COBJECTS)
+LIB libjpeg.lib $(LDLIBS)
+<
+
+djpeg$(SUFFIX): $(DOBJECTS) libjpeg.lib
+	$(LN) <WITH <
+$(LDFLAGS)
+TO djpeg$(SUFFIX)
+FROM LIB:c.o $(DOBJECTS)
+LIB libjpeg.lib $(LDLIBS)
+<
+
+rdjpgcom$(SUFFIX): rdjpgcom.o
+	$(LN) <WITH <
+$(LDFLAGS)
+TO rdjpgcom$(SUFFIX)
+FROM LIB:c.o rdjpgcom.o
+LIB $(LDLIBS)
+<
+
+wrjpgcom$(SUFFIX): wrjpgcom.o
+	$(LN) <WITH <
+$(LDFLAGS)
+TO wrjpgcom$(SUFFIX)
+FROM LIB:c.o wrjpgcom.o
+LIB $(LDLIBS)
+<
+
+jconfig.h: jconfig.doc
+	echo You must prepare a system-dependent jconfig.h file.
+	echo Please read the installation directions in install.doc.
 	exit 1
 
 clean:
 	-$(RM) *.o cjpeg djpeg cjpeg.030 djpeg.030 libjpeg.lib core testout.*
-
-distribute:
-	-$(RM) jpegsrc.tar*
-	tar cvf jpegsrc.tar $(DISTFILES)
-	compress -v jpegsrc.tar
+	-$(RM) rdjpgcom wrjpgcom rdjpgcom.030 wrjpgcom.030
 
 test: cjpeg djpeg
 	-$(RM) testout.ppm testout.gif testout.jpg
-	djpeg -outfile testout.ppm  testorig.jpg
-	djpeg -gif -outfile testout.gif  testorig.jpg
-	cjpeg -outfile testout.jpg  testimg.ppm
+	djpeg -dct int -ppm -outfile testout.ppm  testorig.jpg
+	djpeg -dct int -gif -outfile testout.gif  testorig.jpg
+	cjpeg -dct int -outfile testout.jpg  testimg.ppm
 	cmp testimg.ppm testout.ppm
 	cmp testimg.gif testout.gif
 	cmp testimg.jpg testout.jpg
 
 
-jbsmooth.o : jbsmooth.c jinclude.h jconfig.h jpegdata.h 
-jcarith.o : jcarith.c jinclude.h jconfig.h jpegdata.h 
-jccolor.o : jccolor.c jinclude.h jconfig.h jpegdata.h 
-jcdeflts.o : jcdeflts.c jinclude.h jconfig.h jpegdata.h 
-jcexpand.o : jcexpand.c jinclude.h jconfig.h jpegdata.h 
-jchuff.o : jchuff.c jinclude.h jconfig.h jpegdata.h 
-jcmain.o : jcmain.c jinclude.h jconfig.h jpegdata.h jversion.h 
-jcmaster.o : jcmaster.c jinclude.h jconfig.h jpegdata.h 
-jcmcu.o : jcmcu.c jinclude.h jconfig.h jpegdata.h 
-jcpipe.o : jcpipe.c jinclude.h jconfig.h jpegdata.h 
-jcsample.o : jcsample.c jinclude.h jconfig.h jpegdata.h 
-jdarith.o : jdarith.c jinclude.h jconfig.h jpegdata.h 
-jdcolor.o : jdcolor.c jinclude.h jconfig.h jpegdata.h 
-jddeflts.o : jddeflts.c jinclude.h jconfig.h jpegdata.h 
-jdhuff.o : jdhuff.c jinclude.h jconfig.h jpegdata.h 
-jdmain.o : jdmain.c jinclude.h jconfig.h jpegdata.h jversion.h 
-jdmaster.o : jdmaster.c jinclude.h jconfig.h jpegdata.h 
-jdmcu.o : jdmcu.c jinclude.h jconfig.h jpegdata.h 
-jdpipe.o : jdpipe.c jinclude.h jconfig.h jpegdata.h 
-jdsample.o : jdsample.c jinclude.h jconfig.h jpegdata.h 
-jerror.o : jerror.c jinclude.h jconfig.h jpegdata.h 
-jquant1.o : jquant1.c jinclude.h jconfig.h jpegdata.h 
-jquant2.o : jquant2.c jinclude.h jconfig.h jpegdata.h 
-jfwddct.o : jfwddct.c jinclude.h jconfig.h jpegdata.h 
-jrevdct.o : jrevdct.c jinclude.h jconfig.h jpegdata.h 
-jutils.o : jutils.c jinclude.h jconfig.h jpegdata.h 
-jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpegdata.h jmemsys.h 
-jrdjfif.o : jrdjfif.c jinclude.h jconfig.h jpegdata.h 
-jrdgif.o : jrdgif.c jinclude.h jconfig.h jpegdata.h 
-jrdppm.o : jrdppm.c jinclude.h jconfig.h jpegdata.h 
-jrdrle.o : jrdrle.c jinclude.h jconfig.h jpegdata.h 
-jrdtarga.o : jrdtarga.c jinclude.h jconfig.h jpegdata.h 
-jwrjfif.o : jwrjfif.c jinclude.h jconfig.h jpegdata.h 
-jwrgif.o : jwrgif.c jinclude.h jconfig.h jpegdata.h 
-jwrppm.o : jwrppm.c jinclude.h jconfig.h jpegdata.h 
-jwrrle.o : jwrrle.c jinclude.h jconfig.h jpegdata.h 
-jwrtarga.o : jwrtarga.c jinclude.h jconfig.h jpegdata.h 
-jmemsys.o : jmemsys.c jinclude.h jconfig.h jpegdata.h jmemsys.h 
+jcapi.o : jcapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcprepct.o : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapi.o : jdapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatasrc.o : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatadst.o : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdpostct.o : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jutils.o : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jfdctfst.o : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctflt.o : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+rdcolmap.o : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdppm.o : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdjpgcom.o : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o : wrjpgcom.c jinclude.h jconfig.h
diff --git a/makefile.unix b/makefile.unix
index 6e6699b..a2bfd55 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -3,170 +3,190 @@
 # This makefile is suitable for Unix-like systems with non-ANSI compilers.
 # If you have an ANSI compiler, makefile.ansi is a better starting point.
 
-# Read SETUP instructions before saying "make" !!
+# Read installation instructions before saying "make" !!
 
 # The name of your C compiler:
 CC= cc
 
 # You may need to adjust these cc options:
 CFLAGS= -O
-# In particular:
-#   Add -DBSD if on a pure BSD system (see jinclude.h).
-#   Add -DVMS if on a VMS system (see ansi2knr.c).
-#   Add -DMSDOS if on an MSDOS system (see ansi2knr.c).
-#   Add -DMEM_STATS to enable gathering of memory usage statistics.
-#   You may also want to add -DTWO_FILE_COMMANDLINE or -D switches for other
-#   symbols listed in jconfig.h, if you prefer not to change jconfig.h.
+# Generally, we recommend defining any configuration symbols in jconfig.h,
+# NOT via -D switches here.
+# However, any special defines for ansi2knr.c may be included here:
+ANSI2KNRFLAGS= 
 
 # Link-time cc options:
 LDFLAGS= 
 
 # To link any special libraries, add the necessary -l commands here.
-# In particular, on some versions of HP-UX (and probably other SysV-derived
-# systems) there is a faster alternate malloc(3) library that you can use
-# by adding "-lmalloc" to this line.
 LDLIBS= 
 
+# Put here the object file name for the correct system-dependent memory
+# manager file.  For Unix this is usually jmemnobs.o, but you may want
+# to use jmemansi.o or jmemname.o if you have limited swap space.
+SYSDEPMEM= jmemnobs.o
+
 # miscellaneous OS-dependent stuff
 # linker
 LN= $(CC)
 # file deletion command
 RM= rm -f
+# file rename command
+MV= mv
 # library (.a) file creation command
 AR= ar rc
 # second step in .a creation (use "touch" if not needed)
 AR2= ranlib
 
+# End of configurable options.
 
-# source files (independently compilable files)
-SOURCES= jbsmooth.c jcarith.c jccolor.c jcdeflts.c jcexpand.c jchuff.c \
-        jcmain.c jcmaster.c jcmcu.c jcpipe.c jcsample.c jdarith.c jdcolor.c \
-        jddeflts.c jdhuff.c jdmain.c jdmaster.c jdmcu.c jdpipe.c jdsample.c \
-        jerror.c jquant1.c jquant2.c jfwddct.c jrevdct.c jutils.c jmemmgr.c \
-        jrdjfif.c jrdgif.c jrdppm.c jrdrle.c jrdtarga.c jwrjfif.c jwrgif.c \
-        jwrppm.c jwrrle.c jwrtarga.c
-# virtual source files (not present in distribution file, see SETUP)
-VIRTSOURCES= jmemsys.c
-# system-dependent implementations of virtual source files
-SYSDEPFILES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemdos.h \
-        jmemdosa.asm
+
+# source files: JPEG library proper
+LIBSOURCES= jcapi.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcmainct.c \
+        jcmarker.c jcmaster.c jcomapi.c jcparam.c jcprepct.c jcsample.c \
+        jdapi.c jdatasrc.c jdatadst.c jdcoefct.c jdcolor.c jddctmgr.c \
+        jdhuff.c jdmainct.c jdmarker.c jdmaster.c jdpostct.c jdsample.c \
+        jerror.c jutils.c jfdctfst.c jfdctflt.c jfdctint.c jidctfst.c \
+        jidctflt.c jidctint.c jidctred.c jquant1.c jquant2.c jdmerge.c \
+        jmemmgr.c jmemansi.c jmemname.c jmemnobs.c jmemdos.c
+# source files: cjpeg/djpeg applications, also rdjpgcom/wrjpgcom
+APPSOURCES= cjpeg.c djpeg.c rdcolmap.c rdppm.c wrppm.c rdgif.c wrgif.c \
+        rdtarga.c wrtarga.c rdbmp.c wrbmp.c rdrle.c wrrle.c rdjpgcom.c \
+        wrjpgcom.c
+SOURCES= $(LIBSOURCES) $(APPSOURCES)
 # files included by source files
-INCLUDES= jinclude.h jconfig.h jpegdata.h jversion.h jmemsys.h
+INCLUDES= jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h jpegint.h \
+        jpeglib.h jversion.h cdjpeg.h cderror.h
 # documentation, test, and support files
-DOCS= README SETUP USAGE CHANGELOG cjpeg.1 djpeg.1 architecture codingrules
-MAKEFILES= makefile.ansi makefile.unix makefile.manx makefile.sas \
-        makcjpeg.st makdjpeg.st makljpeg.st makefile.mc5 makefile.mc6 \
-        makefile.bcc makefile.icc makljpeg.icc makefile.mms makefile.vms \
-        makvms.opt
-OTHERFILES= ansi2knr.c ckconfig.c example.c
+DOCS= README install.doc usage.doc cjpeg.1 djpeg.1 rdjpgcom.1 wrjpgcom.1 \
+        example.c libjpeg.doc structure.doc coderules.doc filelist.doc \
+        change.log
+MKFILES= configure makefile.auto makefile.ansi makefile.unix makefile.manx \
+        makefile.sas makcjpeg.st makdjpeg.st makljpeg.st makefile.bcc \
+        makefile.mc6 makefile.dj makefile.mms makefile.vms makvms.opt
+CONFIGFILES= jconfig.auto jconfig.manx jconfig.sas jconfig.st jconfig.bcc \
+        jconfig.mc6 jconfig.dj jconfig.vms
+OTHERFILES= jconfig.doc ckconfig.c ansi2knr.c ansi2knr.1 jmemdosa.asm
 TESTFILES= testorig.jpg testimg.ppm testimg.gif testimg.jpg
-DISTFILES= $(DOCS) $(MAKEFILES) $(SOURCES) $(SYSDEPFILES) $(INCLUDES) \
+DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \
         $(OTHERFILES) $(TESTFILES)
-# objectfiles common to cjpeg and djpeg
-COMOBJECTS= jutils.o jerror.o jmemmgr.o jmemsys.o
-# compression objectfiles
-CLIBOBJECTS= jcmaster.o jcdeflts.o jcarith.o jccolor.o jcexpand.o jchuff.o \
-        jcmcu.o jcpipe.o jcsample.o jfwddct.o jwrjfif.o jrdgif.o jrdppm.o \
-        jrdrle.o jrdtarga.o
-COBJECTS= jcmain.o $(CLIBOBJECTS) $(COMOBJECTS)
-# decompression objectfiles
-DLIBOBJECTS= jdmaster.o jddeflts.o jbsmooth.o jdarith.o jdcolor.o jdhuff.o \
-        jdmcu.o jdpipe.o jdsample.o jquant1.o jquant2.o jrevdct.o jrdjfif.o \
-        jwrgif.o jwrppm.o jwrrle.o jwrtarga.o
-DOBJECTS= jdmain.o $(DLIBOBJECTS) $(COMOBJECTS)
+# library object files common to compression and decompression
+COMOBJECTS= jcomapi.o jutils.o jerror.o jmemmgr.o $(SYSDEPMEM)
+# compression library object files
+CLIBOBJECTS= jcapi.o jcparam.o jdatadst.o jcmaster.o jcmarker.o jcmainct.o \
+        jcprepct.o jccoefct.o jccolor.o jcsample.o jchuff.o jcdctmgr.o \
+        jfdctfst.o jfdctflt.o jfdctint.o
+# decompression library object files
+DLIBOBJECTS= jdapi.o jdatasrc.o jdmaster.o jdmarker.o jdmainct.o jdcoefct.o \
+        jdpostct.o jddctmgr.o jidctfst.o jidctflt.o jidctint.o jidctred.o \
+        jdhuff.o jdsample.o jdcolor.o jquant1.o jquant2.o jdmerge.o
 # These objectfiles are included in libjpeg.a
 LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS)
+# object files for cjpeg and djpeg applications (excluding library files)
+COBJECTS= cjpeg.o rdppm.o rdgif.o rdtarga.o rdrle.o rdbmp.o
+DOBJECTS= djpeg.o wrppm.o wrgif.o wrtarga.o wrrle.o wrbmp.o rdcolmap.o
 
 
-all: ansi2knr cjpeg djpeg
-# By default, libjpeg.a is not built unless you explicitly request it.
-# You can add libjpeg.a to the line above if you want it built by default.
+all: ansi2knr libjpeg.a cjpeg djpeg rdjpgcom wrjpgcom
 
-
-# This rule causes ansi2knr to be invoked.  If you use this makefile,
-# make sure PROTO is not defined by jconfig.h.
-
+# This rule causes ansi2knr to be invoked.
 .c.o:
-	./ansi2knr $*.c tmpansi.c
-	$(CC) $(CFLAGS) -c tmpansi.c
-	mv tmpansi.o $*.o
-	$(RM) tmpansi.c
+	./ansi2knr $*.c T$*.c
+	$(CC) $(CFLAGS) -c T$*.c
+	$(RM) T$*.c $*.o
+	$(MV) T$*.o $*.o
 
 ansi2knr: ansi2knr.c
-	$(CC) $(CFLAGS) -o ansi2knr ansi2knr.c
+	$(CC) $(CFLAGS) $(ANSI2KNRFLAGS) -o ansi2knr ansi2knr.c
 
-
-cjpeg: ansi2knr $(COBJECTS)
-	$(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) $(LDLIBS)
-
-djpeg: ansi2knr $(DOBJECTS)
-	$(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) $(LDLIBS)
-
-# libjpeg.a is useful if you are including the JPEG software in a larger
-# program; you'd include it in your link, rather than the individual modules.
 libjpeg.a: ansi2knr $(LIBOBJECTS)
 	$(RM) libjpeg.a
 	$(AR) libjpeg.a  $(LIBOBJECTS)
 	$(AR2) libjpeg.a
 
-jmemsys.c:
-	echo You must select a system-dependent jmemsys.c file.
-	echo Please read the SETUP directions.
+cjpeg: ansi2knr $(COBJECTS) libjpeg.a
+	$(LN) $(LDFLAGS) -o cjpeg $(COBJECTS) libjpeg.a $(LDLIBS)
+
+djpeg: ansi2knr $(DOBJECTS) libjpeg.a
+	$(LN) $(LDFLAGS) -o djpeg $(DOBJECTS) libjpeg.a $(LDLIBS)
+
+rdjpgcom: rdjpgcom.o
+	$(LN) $(LDFLAGS) -o rdjpgcom rdjpgcom.o $(LDLIBS)
+
+wrjpgcom: wrjpgcom.o
+	$(LN) $(LDFLAGS) -o wrjpgcom wrjpgcom.o $(LDLIBS)
+
+jconfig.h: jconfig.doc
+	echo You must prepare a system-dependent jconfig.h file.
+	echo Please read the installation directions in install.doc.
 	exit 1
 
 clean:
-	$(RM) *.o cjpeg djpeg libjpeg.a ansi2knr core tmpansi.* testout.*
-
-distribute:
-	$(RM) jpegsrc.tar*
-	tar cvf jpegsrc.tar $(DISTFILES)
-	compress -v jpegsrc.tar
+	$(RM) *.o cjpeg djpeg libjpeg.a rdjpgcom wrjpgcom ansi2knr core testout.*
 
 test: cjpeg djpeg
 	$(RM) testout.ppm testout.gif testout.jpg
-	./djpeg testorig.jpg >testout.ppm
-	./djpeg -gif testorig.jpg >testout.gif
-	./cjpeg testimg.ppm >testout.jpg
+	./djpeg -dct int -ppm -outfile testout.ppm  testorig.jpg
+	./djpeg -dct int -gif -outfile testout.gif  testorig.jpg
+	./cjpeg -dct int -outfile testout.jpg  testimg.ppm
 	cmp testimg.ppm testout.ppm
 	cmp testimg.gif testout.gif
 	cmp testimg.jpg testout.jpg
 
 
-jbsmooth.o : jbsmooth.c jinclude.h jconfig.h jpegdata.h 
-jcarith.o : jcarith.c jinclude.h jconfig.h jpegdata.h 
-jccolor.o : jccolor.c jinclude.h jconfig.h jpegdata.h 
-jcdeflts.o : jcdeflts.c jinclude.h jconfig.h jpegdata.h 
-jcexpand.o : jcexpand.c jinclude.h jconfig.h jpegdata.h 
-jchuff.o : jchuff.c jinclude.h jconfig.h jpegdata.h 
-jcmain.o : jcmain.c jinclude.h jconfig.h jpegdata.h jversion.h 
-jcmaster.o : jcmaster.c jinclude.h jconfig.h jpegdata.h 
-jcmcu.o : jcmcu.c jinclude.h jconfig.h jpegdata.h 
-jcpipe.o : jcpipe.c jinclude.h jconfig.h jpegdata.h 
-jcsample.o : jcsample.c jinclude.h jconfig.h jpegdata.h 
-jdarith.o : jdarith.c jinclude.h jconfig.h jpegdata.h 
-jdcolor.o : jdcolor.c jinclude.h jconfig.h jpegdata.h 
-jddeflts.o : jddeflts.c jinclude.h jconfig.h jpegdata.h 
-jdhuff.o : jdhuff.c jinclude.h jconfig.h jpegdata.h 
-jdmain.o : jdmain.c jinclude.h jconfig.h jpegdata.h jversion.h 
-jdmaster.o : jdmaster.c jinclude.h jconfig.h jpegdata.h 
-jdmcu.o : jdmcu.c jinclude.h jconfig.h jpegdata.h 
-jdpipe.o : jdpipe.c jinclude.h jconfig.h jpegdata.h 
-jdsample.o : jdsample.c jinclude.h jconfig.h jpegdata.h 
-jerror.o : jerror.c jinclude.h jconfig.h jpegdata.h 
-jquant1.o : jquant1.c jinclude.h jconfig.h jpegdata.h 
-jquant2.o : jquant2.c jinclude.h jconfig.h jpegdata.h 
-jfwddct.o : jfwddct.c jinclude.h jconfig.h jpegdata.h 
-jrevdct.o : jrevdct.c jinclude.h jconfig.h jpegdata.h 
-jutils.o : jutils.c jinclude.h jconfig.h jpegdata.h 
-jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpegdata.h jmemsys.h 
-jrdjfif.o : jrdjfif.c jinclude.h jconfig.h jpegdata.h 
-jrdgif.o : jrdgif.c jinclude.h jconfig.h jpegdata.h 
-jrdppm.o : jrdppm.c jinclude.h jconfig.h jpegdata.h 
-jrdrle.o : jrdrle.c jinclude.h jconfig.h jpegdata.h 
-jrdtarga.o : jrdtarga.c jinclude.h jconfig.h jpegdata.h 
-jwrjfif.o : jwrjfif.c jinclude.h jconfig.h jpegdata.h 
-jwrgif.o : jwrgif.c jinclude.h jconfig.h jpegdata.h 
-jwrppm.o : jwrppm.c jinclude.h jconfig.h jpegdata.h 
-jwrrle.o : jwrrle.c jinclude.h jconfig.h jpegdata.h 
-jwrtarga.o : jwrtarga.c jinclude.h jconfig.h jpegdata.h 
-jmemsys.o : jmemsys.c jinclude.h jconfig.h jpegdata.h jmemsys.h 
+jcapi.o : jcapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccoefct.o : jccoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jccolor.o : jccolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcdctmgr.o : jcdctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jchuff.o : jchuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmainct.o : jcmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmarker.o : jcmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcmaster.o : jcmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcomapi.o : jcomapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcparam.o : jcparam.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcprepct.o : jcprepct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jcsample.o : jcsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdapi.o : jdapi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdatasrc.o : jdatasrc.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdatadst.o : jdatadst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h
+jdcoefct.o : jdcoefct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdcolor.o : jdcolor.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jddctmgr.o : jddctmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jdhuff.o : jdhuff.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmainct.o : jdmainct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmarker.o : jdmarker.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmaster.o : jdmaster.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdpostct.o : jdpostct.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdsample.o : jdsample.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jerror.o : jerror.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jversion.h jerror.h
+jutils.o : jutils.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jfdctfst.o : jfdctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctflt.o : jfdctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jfdctint.o : jfdctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctfst.o : jidctfst.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctflt.o : jidctflt.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctint.o : jidctint.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jidctred.o : jidctred.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jdct.h
+jquant1.o : jquant1.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jquant2.o : jquant2.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jdmerge.o : jdmerge.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h
+jmemmgr.o : jmemmgr.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemansi.o : jmemansi.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemname.o : jmemname.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemnobs.o : jmemnobs.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+jmemdos.o : jmemdos.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h jmemsys.h
+cjpeg.o : cjpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+djpeg.o : djpeg.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h jversion.h
+rdcolmap.o : rdcolmap.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdppm.o : rdppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrppm.o : wrppm.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdgif.o : rdgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrgif.o : wrgif.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdtarga.o : rdtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrtarga.o : wrtarga.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdbmp.o : rdbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrbmp.o : wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdrle.o : rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+wrrle.o : wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h
+rdjpgcom.o : rdjpgcom.c jinclude.h jconfig.h
+wrjpgcom.o : wrjpgcom.c jinclude.h jconfig.h
diff --git a/makefile.vms b/makefile.vms
index 243c6bb..e654999 100644
--- a/makefile.vms
+++ b/makefile.vms
@@ -1,72 +1,102 @@
 $! Makefile for Independent JPEG Group's software
 $!
-$! This is a command procedure for use on VAX/VMS systems that do not have MMS.
+$! This is a command procedure for Digital VMS systems that do not have MMS.
 $! It builds the JPEG software by brute force, recompiling everything whether
 $! or not it is necessary.  It then runs the basic self-test.
 $! Thanks to Rick Dyson (dyson@iowasp.physics.uiowa.edu)
 $! and Tim Bell (tbell@netcom.com) for their help.
 $!
-$! Read SETUP instructions before running this!!
+$! Read installation instructions before running this!!
 $!
-$ DoCompile := CC /NoDebug /Optimize /Define = (TWO_FILE_COMMANDLINE,HAVE_STDC,INCLUDES_ARE_ANSI)
+$ If F$GetSyi ("HW_MODEL") .gt. 1023 
+$   Then
+$       OPT = ""
+$   Else
+$       OPT = ",Sys$Disk:[]makvms.opt/Option"
+$ EndIf
+$ 
+$ DoCompile := CC /NoDebug /Optimize
 $!
-$ DoCompile jcmain.c
-$ DoCompile jdmain.c
+$ DoCompile jcapi.c
+$ DoCompile jcparam.c
+$ DoCompile jdatadst.c
 $ DoCompile jcmaster.c
-$ DoCompile jcdeflts.c
-$ DoCompile jcarith.c
+$ DoCompile jcmarker.c
+$ DoCompile jcmainct.c
+$ DoCompile jcprepct.c
+$ DoCompile jccoefct.c
 $ DoCompile jccolor.c
-$ DoCompile jcexpand.c
-$ DoCompile jchuff.c
-$ DoCompile jcmcu.c
-$ DoCompile jcpipe.c
 $ DoCompile jcsample.c
-$ DoCompile jfwddct.c
-$ DoCompile jwrjfif.c
-$ DoCompile jrdgif.c
-$ DoCompile jrdppm.c
-$ DoCompile jrdrle.c
-$ DoCompile jrdtarga.c
+$ DoCompile jchuff.c
+$ DoCompile jcdctmgr.c
+$ DoCompile jfdctfst.c
+$ DoCompile jfdctflt.c
+$ DoCompile jfdctint.c
+$ DoCompile jdapi.c
+$ DoCompile jdatasrc.c
 $ DoCompile jdmaster.c
-$ DoCompile jddeflts.c
-$ DoCompile jbsmooth.c
-$ DoCompile jdarith.c
-$ DoCompile jdcolor.c
+$ DoCompile jdmarker.c
+$ DoCompile jdmainct.c
+$ DoCompile jdcoefct.c
+$ DoCompile jdpostct.c
+$ DoCompile jddctmgr.c
+$ DoCompile jidctfst.c
+$ DoCompile jidctflt.c
+$ DoCompile jidctint.c
+$ DoCompile jidctred.c
 $ DoCompile jdhuff.c
-$ DoCompile jdmcu.c
-$ DoCompile jdpipe.c
 $ DoCompile jdsample.c
+$ DoCompile jdcolor.c
 $ DoCompile jquant1.c
 $ DoCompile jquant2.c
-$ DoCompile jrevdct.c
-$ DoCompile jrdjfif.c
-$ DoCompile jwrgif.c
-$ DoCompile jwrppm.c
-$ DoCompile jwrrle.c
-$ DoCompile jwrtarga.c
+$ DoCompile jdmerge.c
+$ DoCompile jcomapi.c
 $ DoCompile jutils.c
 $ DoCompile jerror.c
 $ DoCompile jmemmgr.c
-$ DoCompile jmemsys.c
+$ DoCompile jmemnobs.c
 $!
-$ Library /Create libjpeg.olb  jcmaster.obj,jcdeflts.obj,jcarith.obj, -
-          jccolor.obj,jcexpand.obj,jchuff.obj,jcmcu.obj,jcpipe.obj, -
-          jcsample.obj,jfwddct.obj,jwrjfif.obj,jrdgif.obj,jrdppm.obj, -
-          jrdrle.obj,jrdtarga.obj,jdmaster.obj,jddeflts.obj,jbsmooth.obj, -
-          jdarith.obj,jdcolor.obj,jdhuff.obj,jdmcu.obj,jdpipe.obj, -
-          jdsample.obj,jquant1.obj,jquant2.obj,jrevdct.obj,jrdjfif.obj, -
-          jwrgif.obj,jwrppm.obj,jwrrle.obj,jwrtarga.obj,jutils.obj, -
-          jerror.obj,jmemmgr.obj,jmemsys.obj
+$ Library /Create libjpeg.olb  jcapi.obj,jcparam.obj,jdatadst.obj, -
+          jcmaster.obj,jcmarker.obj,jcmainct.obj,jcprepct.obj,jccoefct.obj, -
+          jccolor.obj,jcsample.obj,jchuff.obj,jcdctmgr.obj,jfdctfst.obj, -
+          jfdctflt.obj,jfdctint.obj,jdapi.obj,jdatasrc.obj,jdmaster.obj, -
+          jdmarker.obj,jdmainct.obj,jdcoefct.obj,jdpostct.obj,jddctmgr.obj, -
+          jidctfst.obj,jidctflt.obj,jidctint.obj,jidctred.obj,jdhuff.obj, -
+          jdsample.obj,jdcolor.obj,jquant1.obj,jquant2.obj,jdmerge.obj, -
+          jcomapi.obj,jutils.obj,jerror.obj,jmemmgr.obj,jmemnobs.obj
 $!
-$ Link /Executable = cjpeg.exe  jcmain.obj,libjpeg.olb/Library,Sys$Disk:[]MAKVMS.OPT/Option
+$ DoCompile cjpeg.c
+$ DoCompile rdppm.c
+$ DoCompile rdgif.c
+$ DoCompile rdtarga.c
+$ DoCompile rdrle.c
+$ DoCompile rdbmp.c
 $!
-$ Link /Executable = djpeg.exe  jdmain.obj,libjpeg.olb/Library,Sys$Disk:[]MAKVMS.OPT/Option
+$ Link /Executable = cjpeg.exe  cjpeg.obj,rdppm.obj,rdgif.obj,rdtarga.obj, -
+          rdrle.obj,rdbmp.obj,libjpeg.olb/Library'OPT'
+$!
+$ DoCompile djpeg.c
+$ DoCompile wrppm.c
+$ DoCompile wrgif.c
+$ DoCompile wrtarga.c
+$ DoCompile wrrle.c
+$ DoCompile wrbmp.c
+$ DoCompile rdcolmap.c
+$!
+$ Link /Executable = djpeg.exe  djpeg.obj,wrppm.obj,wrgif.obj,wrtarga.obj, -
+          wrrle.obj,wrbmp.obj,rdcolmap.obj,libjpeg.olb/Library'OPT'
+$!
+$ DoCompile rdjpgcom.c
+$ Link /Executable = rdjpgcom.exe  rdjpgcom.obj'OPT'
+$!
+$ DoCompile wrjpgcom.c
+$ Link /Executable = wrjpgcom.exe  wrjpgcom.obj'OPT'
 $!
 $! Run the self-test
 $!
-$ mcr sys$disk:[]djpeg      testorig.jpg testout.ppm
-$ mcr sys$disk:[]djpeg -gif testorig.jpg testout.gif
-$ mcr sys$disk:[]cjpeg      testimg.ppm testout.jpg
+$ mcr sys$disk:[]djpeg -dct int -ppm -outfile testout.ppm testorig.jpg
+$ mcr sys$disk:[]djpeg -dct int -gif -outfile testout.gif testorig.jpg
+$ mcr sys$disk:[]cjpeg -dct int      -outfile testout.jpg testimg.ppm
 $ Backup /Compare/Log testimg.ppm testout.ppm
 $ Backup /Compare/Log testimg.gif testout.gif
 $ Backup /Compare/Log testimg.jpg testout.jpg
diff --git a/makljpeg.icc b/makljpeg.icc
deleted file mode 100644
index 1c34db8..0000000
--- a/makljpeg.icc
+++ /dev/null
@@ -1,38 +0,0 @@
-add jcmaster.obj
-add jcdeflts.obj
-add jcarith.obj
-add jccolor.obj
-add jcexpand.obj
-add jchuff.obj
-add jcmcu.obj
-add jcpipe.obj
-add jcsample.obj
-add jfwddct.obj
-add jwrjfif.obj
-add jrdgif.obj
-add jrdppm.obj
-add jrdrle.obj
-add jrdtarga.obj
-add jdmaster.obj
-add jddeflts.obj
-add jbsmooth.obj
-add jdarith.obj
-add jdcolor.obj
-add jdhuff.obj
-add jdmcu.obj
-add jdpipe.obj
-add jdsample.obj
-add jquant1.obj
-add jquant2.obj
-add jrevdct.obj
-add jrdjfif.obj
-add jwrgif.obj
-add jwrppm.obj
-add jwrrle.obj
-add jwrtarga.obj
-add jutils.obj
-add jerror.obj
-add jmemmgr.obj
-add jmemsys.obj
-update
-quit
diff --git a/makljpeg.st b/makljpeg.st
index cec75ce..a8dc41a 100644
--- a/makljpeg.st
+++ b/makljpeg.st
@@ -4,60 +4,58 @@
 ; Thanks to Frank Moehle (Frank.Moehle@arbi.informatik.uni-oldenburg.de)
 ; and to Dr. B. Setzepfandt (bernd@gina.uni-muenster.de).
 ;
-; To use this file, rename it to LIB_JPEG.PRJ.
-; Read SETUP instructions before trying to make the library!
+; To use this file, rename it to LIBJPEG.PRJ.
+; Read installation instructions before trying to make the program!
 ;
 ;
 ;      * * * Output file * * *
-jpeg.lib
-;
-;      * * * DEFINES * * * 
-.C[-DHAVE_STDC]
-.C[-DINCLUDES_ARE_ANSI]
+libjpeg.lib
 ;
 ; * * * COMPILER OPTIONS * * *  
 .C[-P]        ; absolute calls
 .C[-M]        ; and no string merging, folks
-.C[-w-par]    ; no "parameter xxxx unused"
-.C[-DINCOMPLETE_TYPES_BROKEN] ; suppress warnings about undefined structures
-.C[-wsig]     ; warn if significant digits may be lost
 .C[-w-cln]    ; no "constant is long" warnings
+.C[-w-par]    ; no "parameter xxxx unused"
+.C[-w-rch]    ; no "unreachable code"
+.C[-wsig]     ; warn if significant digits may be lost
 .L[-J]        ; link new Obj-format (so we get a library)
 =
 ; * * * * List of modules * * * * 
-jbsmooth.c		(jinclude.h,jconfig.h,jpegdata.h)
-jcarith.c		(jinclude.h,jconfig.h,jpegdata.h)
-jccolor.c		(jinclude.h,jconfig.h,jpegdata.h)
-jcdeflts.c		(jinclude.h,jconfig.h,jpegdata.h)
-jcexpand.c		(jinclude.h,jconfig.h,jpegdata.h)
-jchuff.c		(jinclude.h,jconfig.h,jpegdata.h)
-jcmaster.c		(jinclude.h,jconfig.h,jpegdata.h)
-jcmcu.c		(jinclude.h,jconfig.h,jpegdata.h)
-jcpipe.c		(jinclude.h,jconfig.h,jpegdata.h)
-jcsample.c		(jinclude.h,jconfig.h,jpegdata.h)
-jdarith.c		(jinclude.h,jconfig.h,jpegdata.h)
-jdcolor.c		(jinclude.h,jconfig.h,jpegdata.h)
-jddeflts.c		(jinclude.h,jconfig.h,jpegdata.h)
-jdhuff.c		(jinclude.h,jconfig.h,jpegdata.h)
-jdmaster.c		(jinclude.h,jconfig.h,jpegdata.h)
-jdmcu.c		(jinclude.h,jconfig.h,jpegdata.h)
-jdpipe.c		(jinclude.h,jconfig.h,jpegdata.h)
-jdsample.c		(jinclude.h,jconfig.h,jpegdata.h)
-jerror.c		(jinclude.h,jconfig.h,jpegdata.h)
-jquant1.c		(jinclude.h,jconfig.h,jpegdata.h)
-jquant2.c		(jinclude.h,jconfig.h,jpegdata.h)
-jfwddct.c		(jinclude.h,jconfig.h,jpegdata.h)
-jrevdct.c		(jinclude.h,jconfig.h,jpegdata.h)
-jutils.c		(jinclude.h,jconfig.h,jpegdata.h)
-jmemmgr.c		(jinclude.h,jconfig.h,jpegdata.h,jmemsys.h)
-jrdjfif.c		(jinclude.h,jconfig.h,jpegdata.h)
-jrdgif.c		(jinclude.h,jconfig.h,jpegdata.h)
-jrdppm.c		(jinclude.h,jconfig.h,jpegdata.h)
-jrdrle.c		(jinclude.h,jconfig.h,jpegdata.h)
-jrdtarga.c		(jinclude.h,jconfig.h,jpegdata.h)
-jwrjfif.c		(jinclude.h,jconfig.h,jpegdata.h)
-jwrgif.c		(jinclude.h,jconfig.h,jpegdata.h)
-jwrppm.c		(jinclude.h,jconfig.h,jpegdata.h)
-jwrrle.c		(jinclude.h,jconfig.h,jpegdata.h)
-jwrtarga.c		(jinclude.h,jconfig.h,jpegdata.h)
-jmemsys.c		(jinclude.h,jconfig.h,jpegdata.h,jmemsys.h)
+jcapi.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jccoefct.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jccolor.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcdctmgr.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jchuff.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcmainct.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcmarker.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcmaster.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcomapi.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcparam.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcprepct.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jcsample.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdapi.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdatasrc.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h)
+jdatadst.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jerror.h)
+jdcoefct.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdcolor.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jddctmgr.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jdhuff.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdmainct.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdmarker.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdmaster.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdpostct.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdsample.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jerror.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jversion.h,jerror.h)
+jutils.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jfdctfst.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jfdctflt.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jfdctint.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jidctfst.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jidctflt.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jidctint.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jidctred.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jdct.h)
+jquant1.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jquant2.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jdmerge.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h)
+jmemmgr.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jmemsys.h)
+jmemansi.c	(jinclude.h,jconfig.h,jpeglib.h,jmorecfg.h,jpegint.h,jerror.h,jmemsys.h)
diff --git a/makvms.opt b/makvms.opt
index eafc4c9..675e8fe 100644
--- a/makvms.opt
+++ b/makvms.opt
@@ -1,3 +1,4 @@
-! a pointer to the VAX/VMS C Run-Time Shareable Library
-! This file is needed by makefile.mms and makefile.vms
+! A pointer to the VAX/VMS C Run-Time Shareable Library.
+! This file is needed by makefile.mms and makefile.vms,
+! but only for the older VAX C compiler.  DEC C does not need it.
 Sys$Library:VAXCRTL.EXE /Share
diff --git a/rdbmp.c b/rdbmp.c
new file mode 100644
index 0000000..f91849e
--- /dev/null
+++ b/rdbmp.c
@@ -0,0 +1,421 @@
+/*
+ * rdbmp.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in Microsoft "BMP"
+ * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors).
+ * Currently, only 8-bit and 24-bit images are supported, not 1-bit or
+ * 4-bit (feeding such low-depth images into JPEG would be silly anyway).
+ * Also, we don't support RLE-compressed files.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume input from
+ * an ordinary stdio stream.  They further assume that reading begins
+ * at the start of the file; start_input may need work if the
+ * user interface has already read some data (e.g., to determine that
+ * the file is indeed BMP format).
+ *
+ * This code contributed by James Arthur Boucher.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef BMP_SUPPORTED
+
+
+/* Macros to deal with unsigned chars as efficiently as compiler allows */
+
+#ifdef HAVE_UNSIGNED_CHAR
+typedef unsigned char U_CHAR;
+#define UCH(x)	((int) (x))
+#else /* !HAVE_UNSIGNED_CHAR */
+#ifdef CHAR_IS_UNSIGNED
+typedef char U_CHAR;
+#define UCH(x)	((int) (x))
+#else
+typedef char U_CHAR;
+#define UCH(x)	((int) (x) & 0xFF)
+#endif
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+#define	ReadOK(file,buffer,len)	(JFREAD(file,buffer,len) == ((size_t) (len)))
+
+
+/* Private version of data source object */
+
+typedef struct _bmp_source_struct * bmp_source_ptr;
+
+typedef struct _bmp_source_struct {
+  struct cjpeg_source_struct pub; /* public fields */
+
+  j_compress_ptr cinfo;		/* back link saves passing separate parm */
+
+  JSAMPARRAY colormap;		/* BMP colormap (converted to my format) */
+
+  jvirt_sarray_ptr whole_image;	/* Needed to reverse row order */
+  JDIMENSION source_row;	/* Current source row number */
+  JDIMENSION row_width;		/* Physical width of scanlines in file */
+
+  int bits_per_pixel;		/* remembers 8- or 24-bit format */
+} bmp_source_struct;
+
+
+LOCAL int
+read_byte (bmp_source_ptr sinfo)
+/* Read next byte from BMP file */
+{
+  register FILE *infile = sinfo->pub.input_file;
+  register int c;
+
+  if ((c = getc(infile)) == EOF)
+    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
+  return c;
+}
+
+
+LOCAL void
+read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
+/* Read the colormap from a BMP file */
+{
+  int i;
+
+  switch (mapentrysize) {
+  case 3:
+    /* BGR format (occurs in OS/2 files) */
+    for (i = 0; i < cmaplen; i++) {
+      sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
+      sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
+      sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
+    }
+    break;
+  case 4:
+    /* BGR0 format (occurs in MS Windows files) */
+    for (i = 0; i < cmaplen; i++) {
+      sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
+      sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
+      sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
+      (void) read_byte(sinfo);
+    }
+    break;
+  default:
+    ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
+    break;
+  }
+}
+
+
+/*
+ * Read one row of pixels.
+ * The image has been read into the whole_image array, but is otherwise
+ * unprocessed.  We must read it out in top-to-bottom row order, and if
+ * it is an 8-bit image, we must expand colormapped pixels to 24bit format.
+ */
+
+METHODDEF JDIMENSION
+get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 8-bit colormap indexes */
+{
+  bmp_source_ptr source = (bmp_source_ptr) sinfo;
+  register JSAMPARRAY colormap = source->colormap;
+  JSAMPARRAY image_ptr;
+  register int t;
+  register JSAMPROW inptr, outptr;
+  register JDIMENSION col;
+
+  /* Fetch next row from virtual array */
+  source->source_row--;
+  image_ptr = (*cinfo->mem->access_virt_sarray)
+    ((j_common_ptr) cinfo, source->whole_image, source->source_row, FALSE);
+
+  /* Expand the colormap indexes to real data */
+  inptr = image_ptr[0];
+  outptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    t = GETJSAMPLE(*inptr++);
+    *outptr++ = colormap[0][t];	/* can omit GETJSAMPLE() safely */
+    *outptr++ = colormap[1][t];
+    *outptr++ = colormap[2][t];
+  }
+
+  return 1;
+}
+
+
+METHODDEF JDIMENSION
+get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 24-bit pixels */
+{
+  bmp_source_ptr source = (bmp_source_ptr) sinfo;
+  JSAMPARRAY image_ptr;
+  register JSAMPROW inptr, outptr;
+  register JDIMENSION col;
+
+  /* Fetch next row from virtual array */
+  source->source_row--;
+  image_ptr = (*cinfo->mem->access_virt_sarray)
+    ((j_common_ptr) cinfo, source->whole_image, source->source_row, FALSE);
+
+  /* Transfer data.  Note source values are in BGR order
+   * (even though Microsoft's own documents say the opposite).
+   */
+  inptr = image_ptr[0];
+  outptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    outptr[2] = *inptr++;	/* can omit GETJSAMPLE() safely */
+    outptr[1] = *inptr++;
+    outptr[0] = *inptr++;
+    outptr += 3;
+  }
+
+  return 1;
+}
+
+
+/*
+ * This method loads the image into whole_image during the first call on
+ * get_pixel_rows.  The get_pixel_rows pointer is then adjusted to call
+ * get_8bit_row or get_24bit_row on subsequent calls.
+ */
+
+METHODDEF JDIMENSION
+preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  bmp_source_ptr source = (bmp_source_ptr) sinfo;
+  register FILE *infile = source->pub.input_file;
+  register int c;
+  register JSAMPROW out_ptr;
+  JSAMPARRAY image_ptr;
+  JDIMENSION row, col;
+  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+
+  /* Read the data into a virtual array in input-file row order. */
+  for (row = 0; row < cinfo->image_height; row++) {
+    if (progress != NULL) {
+      progress->pub.pass_counter = (long) row;
+      progress->pub.pass_limit = (long) cinfo->image_height;
+      (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+    }
+    image_ptr = (*cinfo->mem->access_virt_sarray)
+      ((j_common_ptr) cinfo, source->whole_image, row, TRUE);
+    out_ptr = image_ptr[0];
+    for (col = source->row_width; col > 0; col--) {
+      /* inline copy of read_byte() for speed */
+      if ((c = getc(infile)) == EOF)
+	ERREXIT(cinfo, JERR_INPUT_EOF);
+      *out_ptr++ = (JSAMPLE) c;
+    }
+  }
+  if (progress != NULL)
+    progress->completed_extra_passes++;
+
+  /* Set up to read from the virtual array in top-to-bottom order */
+  switch (source->bits_per_pixel) {
+  case 8:
+    source->pub.get_pixel_rows = get_8bit_row;
+    break;
+  case 24:
+    source->pub.get_pixel_rows = get_24bit_row;
+    break;
+  default:
+    ERREXIT(cinfo, JERR_BMP_BADDEPTH);
+  }
+  source->source_row = cinfo->image_height;
+
+  /* And read the first row */
+  return (*source->pub.get_pixel_rows) (cinfo, sinfo);
+}
+
+
+/*
+ * Read the file header; return image size and component count.
+ */
+
+METHODDEF void
+start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  bmp_source_ptr source = (bmp_source_ptr) sinfo;
+  U_CHAR bmpfileheader[14];
+  U_CHAR bmpinfoheader[64];
+#define GET_2B(array,offset)  ((unsigned int) UCH(array[offset]) + \
+			       (((unsigned int) UCH(array[offset+1])) << 8))
+#define GET_4B(array,offset)  ((INT32) UCH(array[offset]) + \
+			       (((INT32) UCH(array[offset+1])) << 8) + \
+			       (((INT32) UCH(array[offset+2])) << 16) + \
+			       (((INT32) UCH(array[offset+3])) << 24))
+  INT32 headerSize;
+  INT32 biWidth = 0;		/* initialize to avoid compiler warning */
+  INT32 biHeight = 0;
+  unsigned int biPlanes;
+  INT32 biCompression;
+  INT32 biXPelsPerMeter,biYPelsPerMeter;
+  INT32 biClrUsed = 0;
+  int mapentrysize = 0;		/* 0 indicates no colormap */
+  JDIMENSION row_width;
+
+  /* Read and verify the bitmap file header */
+  if (! ReadOK(source->pub.input_file, bmpfileheader, 14))
+    ERREXIT(cinfo, JERR_INPUT_EOF);
+  if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */
+    ERREXIT(cinfo, JERR_BMP_NOT);
+  /* We ignore the remaining fileheader fields */
+
+  /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
+   * or 64 bytes (OS/2 2.x).  Check the first 4 bytes to find out which.
+   */
+  if (! ReadOK(source->pub.input_file, bmpinfoheader, 4))
+    ERREXIT(cinfo, JERR_INPUT_EOF);
+  headerSize = (INT32) GET_4B(bmpinfoheader,0);
+  if (headerSize < 12 || headerSize > 64)
+    ERREXIT(cinfo, JERR_BMP_BADHEADER);
+  if (! ReadOK(source->pub.input_file, bmpinfoheader+4, headerSize-4))
+    ERREXIT(cinfo, JERR_INPUT_EOF);
+
+  switch ((int) headerSize) {
+  case 12:
+    /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
+    biWidth = (INT32) GET_2B(bmpinfoheader,4);
+    biHeight = (INT32) GET_2B(bmpinfoheader,6);
+    biPlanes = GET_2B(bmpinfoheader,8);
+    source->bits_per_pixel = (int) GET_2B(bmpinfoheader,10);
+
+    switch (source->bits_per_pixel) {
+    case 8:			/* colormapped image */
+      mapentrysize = 3;		/* OS/2 uses RGBTRIPLE colormap */
+      TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight);
+      break;
+    case 24:			/* RGB image */
+      TRACEMS2(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight);
+      break;
+    default:
+      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
+      break;
+    }
+    if (biPlanes != 1)
+      ERREXIT(cinfo, JERR_BMP_BADPLANES);
+    break;
+  case 40:
+  case 64:
+    /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
+    /* or OS/2 2.x header, which has additional fields that we ignore */
+    biWidth = GET_4B(bmpinfoheader,4);
+    biHeight = GET_4B(bmpinfoheader,8);
+    biPlanes = GET_2B(bmpinfoheader,12);
+    source->bits_per_pixel = (int) GET_2B(bmpinfoheader,14);
+    biCompression = GET_4B(bmpinfoheader,16);
+    biXPelsPerMeter = GET_4B(bmpinfoheader,24);
+    biYPelsPerMeter = GET_4B(bmpinfoheader,28);
+    biClrUsed = GET_4B(bmpinfoheader,32);
+    /* biSizeImage, biClrImportant fields are ignored */
+
+    switch (source->bits_per_pixel) {
+    case 8:			/* colormapped image */
+      mapentrysize = 4;		/* Windows uses RGBQUAD colormap */
+      TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, (int) biWidth, (int) biHeight);
+      break;
+    case 24:			/* RGB image */
+      TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight);
+      break;
+    default:
+      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
+      break;
+    }
+    if (biPlanes != 1)
+      ERREXIT(cinfo, JERR_BMP_BADPLANES);
+    if (biCompression != 0)
+      ERREXIT(cinfo, JERR_BMP_COMPRESSED);
+
+    if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
+      /* Set JFIF density parameters from the BMP data */
+      cinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */
+      cinfo->Y_density = (UINT16) (biYPelsPerMeter/100);
+      cinfo->density_unit = 2;	/* dots/cm */
+    }
+    break;
+  default:
+    ERREXIT(cinfo, JERR_BMP_BADHEADER);
+    break;
+  }
+
+  /* Read the colormap, if any */
+  if (mapentrysize > 0) {
+    if (biClrUsed <= 0)
+      biClrUsed = 256;		/* assume it's 256 */
+    else if (biClrUsed > 256)
+      ERREXIT(cinfo, JERR_BMP_BADCMAP);
+    /* Allocate space to store the colormap */
+    source->colormap = (*cinfo->mem->alloc_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       (JDIMENSION) biClrUsed, (JDIMENSION) 3);
+    /* and read it from the file */
+    read_colormap(source, (int) biClrUsed, mapentrysize);
+  }
+
+  /* Compute row width in file, including padding to 4-byte boundary */
+  if (source->bits_per_pixel == 24)
+    row_width = (JDIMENSION) (biWidth * 3);
+  else
+    row_width = (JDIMENSION) biWidth;
+  while ((row_width & 3) != 0) row_width++;
+  source->row_width = row_width;
+
+  /* Allocate space for inversion array, prepare for preload pass */
+  source->whole_image = (*cinfo->mem->request_virt_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     row_width, (JDIMENSION) biHeight, (JDIMENSION) 1);
+  source->pub.get_pixel_rows = preload_image;
+  if (cinfo->progress != NULL) {
+    cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+    progress->total_extra_passes++; /* count file input as separate pass */
+  }
+
+  /* Allocate one-row buffer for returned data */
+  source->pub.buffer = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     (JDIMENSION) (biWidth * 3), (JDIMENSION) 1);
+  source->pub.buffer_height = 1;
+
+  cinfo->in_color_space = JCS_RGB;
+  cinfo->input_components = 3;
+  cinfo->data_precision = 8;
+  cinfo->image_width = (JDIMENSION) biWidth;
+  cinfo->image_height = (JDIMENSION) biHeight;
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF void
+finish_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  /* no work */
+}
+
+
+/*
+ * The module selection routine for BMP format input.
+ */
+
+GLOBAL cjpeg_source_ptr
+jinit_read_bmp (j_compress_ptr cinfo)
+{
+  bmp_source_ptr source;
+
+  /* Create module interface object */
+  source = (bmp_source_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(bmp_source_struct));
+  source->cinfo = cinfo;	/* make back link for subroutines */
+  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
+  source->pub.start_input = start_input_bmp;
+  source->pub.finish_input = finish_input_bmp;
+
+  return (cjpeg_source_ptr) source;
+}
+
+#endif /* BMP_SUPPORTED */
diff --git a/rdcolmap.c b/rdcolmap.c
new file mode 100644
index 0000000..54f5c1d
--- /dev/null
+++ b/rdcolmap.c
@@ -0,0 +1,253 @@
+/*
+ * rdcolmap.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file implements djpeg's "-map file" switch.  It reads a source image
+ * and constructs a colormap to be supplied to the JPEG decompressor.
+ *
+ * Currently, these file formats are supported for the map file:
+ *   GIF: the contents of the GIF's global colormap are used.
+ *   PPM (either text or raw flavor): the entire file is read and
+ *      each unique pixel value is entered in the map.
+ * Note that reading a large PPM file will be horrendously slow.
+ * Typically, a PPM-format map file should contain just one pixel
+ * of each desired color.  Such a file can be extracted from an
+ * ordinary image PPM file with ppmtomap(1).
+ *
+ * Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not
+ * currently implemented.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef QUANT_2PASS_SUPPORTED	/* otherwise can't quantize to supplied map */
+
+/* Portions of this code are based on the PBMPLUS library, which is:
+**
+** Copyright (C) 1988 by Jef Poskanzer.
+**
+** Permission to use, copy, modify, and distribute this software and its
+** documentation for any purpose and without fee is hereby granted, provided
+** that the above copyright notice appear in all copies and that both that
+** copyright notice and this permission notice appear in supporting
+** documentation.  This software is provided "as is" without express or
+** implied warranty.
+*/
+
+
+/*
+ * Add a (potentially) new color to the color map.
+ */
+
+LOCAL void
+add_map_entry (j_decompress_ptr cinfo, int R, int G, int B)
+{
+  JSAMPROW colormap0 = cinfo->colormap[0];
+  JSAMPROW colormap1 = cinfo->colormap[1];
+  JSAMPROW colormap2 = cinfo->colormap[2];
+  int ncolors = cinfo->actual_number_of_colors;
+  int index;
+
+  /* Check for duplicate color. */
+  for (index = 0; index < ncolors; index++) {
+    if (GETJSAMPLE(colormap0[index]) == R &&
+	GETJSAMPLE(colormap1[index]) == G &&
+	GETJSAMPLE(colormap2[index]) == B)
+      return;			/* color is already in map */
+  }
+
+  /* Check for map overflow. */
+  if (ncolors >= (MAXJSAMPLE+1))
+    ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, (MAXJSAMPLE+1));
+
+  /* OK, add color to map. */
+  colormap0[ncolors] = (JSAMPLE) R;
+  colormap1[ncolors] = (JSAMPLE) G;
+  colormap2[ncolors] = (JSAMPLE) B;
+  cinfo->actual_number_of_colors++;
+}
+
+
+/*
+ * Extract color map from a GIF file.
+ */
+
+LOCAL void
+read_gif_map (j_decompress_ptr cinfo, FILE * infile)
+{
+  int header[13];
+  int i, colormaplen;
+  int R, G, B;
+
+  /* Initial 'G' has already been read by read_color_map */
+  /* Read the rest of the GIF header and logical screen descriptor */
+  for (i = 1; i < 13; i++) {
+    if ((header[i] = getc(infile)) == EOF)
+      ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+  }
+
+  /* Verify GIF Header */
+  if (header[1] != 'I' || header[2] != 'F')
+    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+
+  /* There must be a global color map. */
+  if ((header[10] & 0x80) == 0)
+    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+
+  /* OK, fetch it. */
+  colormaplen = 2 << (header[10] & 0x07);
+
+  for (i = 0; i < colormaplen; i++) {
+    R = getc(infile);
+    G = getc(infile);
+    B = getc(infile);
+    if (R == EOF || G == EOF || B == EOF)
+      ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+    add_map_entry(cinfo,
+		  R << (BITS_IN_JSAMPLE-8),
+		  G << (BITS_IN_JSAMPLE-8),
+		  B << (BITS_IN_JSAMPLE-8));
+  }
+}
+
+
+/* Support routines for reading PPM */
+
+
+LOCAL int
+pbm_getc (FILE * infile)
+/* Read next char, skipping over any comments */
+/* A comment/newline sequence is returned as a newline */
+{
+  register int ch;
+  
+  ch = getc(infile);
+  if (ch == '#') {
+    do {
+      ch = getc(infile);
+    } while (ch != '\n' && ch != EOF);
+  }
+  return ch;
+}
+
+
+LOCAL unsigned int
+read_pbm_integer (j_decompress_ptr cinfo, FILE * infile)
+/* Read an unsigned decimal integer from the PPM file */
+/* Swallows one trailing character after the integer */
+/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
+/* This should not be a problem in practice. */
+{
+  register int ch;
+  register unsigned int val;
+  
+  /* Skip any leading whitespace */
+  do {
+    ch = pbm_getc(infile);
+    if (ch == EOF)
+      ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
+  
+  if (ch < '0' || ch > '9')
+    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
+  
+  val = ch - '0';
+  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
+    val *= 10;
+    val += ch - '0';
+  }
+  return val;
+}
+
+
+/*
+ * Extract color map from a PPM file.
+ */
+
+LOCAL void
+read_ppm_map (j_decompress_ptr cinfo, FILE * infile)
+{
+  int c;
+  unsigned int w, h, maxval, row, col;
+  int R, G, B;
+
+  /* Initial 'P' has already been read by read_color_map */
+  c = getc(infile);		/* save format discriminator for a sec */
+
+  /* while we fetch the remaining header info */
+  w = read_pbm_integer(cinfo, infile);
+  h = read_pbm_integer(cinfo, infile);
+  maxval = read_pbm_integer(cinfo, infile);
+
+  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
+    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+
+  /* For now, we don't support rescaling from an unusual maxval. */
+  if (maxval != (unsigned int) MAXJSAMPLE)
+    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+
+  switch (c) {
+  case '3':			/* it's a text-format PPM file */
+    for (row = 0; row < h; row++) {
+      for (col = 0; col < w; col++) {
+	R = read_pbm_integer(cinfo, infile);
+	G = read_pbm_integer(cinfo, infile);
+	B = read_pbm_integer(cinfo, infile);
+	add_map_entry(cinfo, R, G, B);
+      }
+    }
+    break;
+
+  case '6':			/* it's a raw-format PPM file */
+    for (row = 0; row < h; row++) {
+      for (col = 0; col < w; col++) {
+	R = pbm_getc(infile);
+	G = pbm_getc(infile);
+	B = pbm_getc(infile);
+	if (R == EOF || G == EOF || B == EOF)
+	  ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+	add_map_entry(cinfo, R, G, B);
+      }
+    }
+    break;
+
+  default:
+    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+    break;
+  }
+}
+
+
+/*
+ * Main entry point from djpeg.c.
+ *  Input: opened input file (from file name argument on command line).
+ *  Output: colormap and actual_number_of_colors fields are set in cinfo.
+ */
+
+GLOBAL void
+read_color_map (j_decompress_ptr cinfo, FILE * infile)
+{
+  /* Allocate space for a color map of maximum supported size. */
+  cinfo->colormap = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     (JDIMENSION) (MAXJSAMPLE+1), (JDIMENSION) 3);
+  cinfo->actual_number_of_colors = 0; /* initialize map to empty */
+
+  /* Read first byte to determine file format */
+  switch (getc(infile)) {
+  case 'G':
+    read_gif_map(cinfo, infile);
+    break;
+  case 'P':
+    read_ppm_map(cinfo, infile);
+    break;
+  default:
+    ERREXIT(cinfo, JERR_BAD_CMAP_FILE);
+    break;
+  }
+}
+
+#endif /* QUANT_2PASS_SUPPORTED */
diff --git a/rdgif.c b/rdgif.c
new file mode 100644
index 0000000..b886602
--- /dev/null
+++ b/rdgif.c
@@ -0,0 +1,671 @@
+/*
+ * rdgif.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in GIF format.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume input from
+ * an ordinary stdio stream.  They further assume that reading begins
+ * at the start of the file; input_init may need work if the
+ * user interface has already read some data (e.g., to determine that
+ * the file is indeed GIF format).
+ */
+
+/*
+ * This code is loosely based on giftoppm from the PBMPLUS distribution
+ * of Feb. 1991.  That file contains the following copyright notice:
+ * +-------------------------------------------------------------------+
+ * | Copyright 1990, David Koblas.                                     |
+ * |   Permission to use, copy, modify, and distribute this software   |
+ * |   and its documentation for any purpose and without fee is hereby |
+ * |   granted, provided that the above copyright notice appear in all |
+ * |   copies and that both that copyright notice and this permission  |
+ * |   notice appear in supporting documentation.  This software is    |
+ * |   provided "as is" without express or implied warranty.           |
+ * +-------------------------------------------------------------------+
+ *
+ * We are also required to state that
+ *    "The Graphics Interchange Format(c) is the Copyright property of
+ *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
+ *    CompuServe Incorporated."
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef GIF_SUPPORTED
+
+
+#define	MAXCOLORMAPSIZE	256	/* max # of colors in a GIF colormap */
+#define NUMCOLORS	3	/* # of colors */
+#define CM_RED		0	/* color component numbers */
+#define CM_GREEN	1
+#define CM_BLUE		2
+
+#define	MAX_LZW_BITS	12	/* maximum LZW code size */
+#define LZW_TABLE_SIZE	(1<<MAX_LZW_BITS) /* # of possible LZW symbols */
+
+/* Macros for extracting header data --- note we assume chars may be signed */
+
+#define LM_to_uint(a,b)		((((b)&0xFF) << 8) | ((a)&0xFF))
+
+#define BitSet(byte, bit)	((byte) & (bit))
+#define INTERLACE	0x40	/* mask for bit signifying interlaced image */
+#define COLORMAPFLAG	0x80	/* mask for bit signifying colormap presence */
+
+#define	ReadOK(file,buffer,len)	(JFREAD(file,buffer,len) == ((size_t) (len)))
+
+/* LZW decompression tables look like this:
+ *   symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1)
+ *   symbol_tail[K] = suffix byte   of any LZW symbol K (0..LZW_TABLE_SIZE-1)
+ * Note that entries 0..end_code of the above tables are not used,
+ * since those symbols represent raw bytes or special codes.
+ *
+ * The stack represents the not-yet-used expansion of the last LZW symbol.
+ * In the worst case, a symbol could expand to as many bytes as there are
+ * LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack.
+ * (This is conservative since that number includes the raw-byte symbols.)
+ *
+ * The tables are allocated from FAR heap space since they would use up
+ * rather a lot of the near data space in a PC.
+ */
+
+
+/* Private version of data source object */
+
+typedef struct {
+  struct cjpeg_source_struct pub; /* public fields */
+
+  j_compress_ptr cinfo;		/* back link saves passing separate parm */
+
+  JSAMPARRAY colormap;		/* GIF colormap (converted to my format) */
+
+  /* State for GetCode and LZWReadByte */
+  char code_buf[256+4];		/* current input data block */
+  int last_byte;		/* # of bytes in code_buf */
+  int last_bit;			/* # of bits in code_buf */
+  int cur_bit;			/* next bit index to read */
+  boolean out_of_blocks;	/* TRUE if hit terminator data block */
+
+  int input_code_size;		/* codesize given in GIF file */
+  int clear_code,end_code;	/* values for Clear and End codes */
+
+  int code_size;		/* current actual code size */
+  int limit_code;		/* 2^code_size */
+  int max_code;			/* first unused code value */
+  boolean first_time;		/* flags first call to LZWReadByte */
+
+  /* Private state for LZWReadByte */
+  int oldcode;			/* previous LZW symbol */
+  int firstcode;		/* first byte of oldcode's expansion */
+
+  /* LZW symbol table and expansion stack */
+  UINT16 FAR *symbol_head;	/* => table of prefix symbols */
+  UINT8  FAR *symbol_tail;	/* => table of suffix bytes */
+  UINT8  FAR *symbol_stack;	/* => stack for symbol expansions */
+  UINT8  FAR *sp;		/* stack pointer */
+
+  /* State for interlaced image processing */
+  boolean is_interlaced;	/* TRUE if have interlaced image */
+  jvirt_sarray_ptr interlaced_image; /* full image in interlaced order */
+  JDIMENSION cur_row_number;	/* need to know actual row number */
+  JDIMENSION pass2_offset;	/* # of pixel rows in pass 1 */
+  JDIMENSION pass3_offset;	/* # of pixel rows in passes 1&2 */
+  JDIMENSION pass4_offset;	/* # of pixel rows in passes 1,2,3 */
+} gif_source_struct;
+
+typedef gif_source_struct * gif_source_ptr;
+
+
+/* Forward declarations */
+METHODDEF JDIMENSION get_pixel_rows
+	JPP((j_compress_ptr cinfo, cjpeg_source_ptr sinfo));
+METHODDEF JDIMENSION load_interlaced_image
+	JPP((j_compress_ptr cinfo, cjpeg_source_ptr sinfo));
+METHODDEF JDIMENSION get_interlaced_row
+	JPP((j_compress_ptr cinfo, cjpeg_source_ptr sinfo));
+
+
+LOCAL int
+ReadByte (gif_source_ptr sinfo)
+/* Read next byte from GIF file */
+{
+  register FILE * infile = sinfo->pub.input_file;
+  int c;
+
+  if ((c = getc(infile)) == EOF)
+    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
+  return c;
+}
+
+
+LOCAL int
+GetDataBlock (gif_source_ptr sinfo, char *buf)
+/* Read a GIF data block, which has a leading count byte */
+/* A zero-length block marks the end of a data block sequence */
+{
+  int count;
+
+  count = ReadByte(sinfo);
+  if (count > 0) {
+    if (! ReadOK(sinfo->pub.input_file, buf, count))
+      ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
+  }
+  return count;
+}
+
+
+LOCAL void
+SkipDataBlocks (gif_source_ptr sinfo)
+/* Skip a series of data blocks, until a block terminator is found */
+{
+  char buf[256];
+
+  while (GetDataBlock(sinfo, buf) > 0)
+    /* skip */;
+}
+
+
+LOCAL void
+ReInitLZW (gif_source_ptr sinfo)
+/* (Re)initialize LZW state; shared code for startup and Clear processing */
+{
+  sinfo->code_size = sinfo->input_code_size + 1;
+  sinfo->limit_code = sinfo->clear_code << 1;	/* 2^code_size */
+  sinfo->max_code = sinfo->clear_code + 2;	/* first unused code value */
+  sinfo->sp = sinfo->symbol_stack;		/* init stack to empty */
+}
+
+
+LOCAL void
+InitLZWCode (gif_source_ptr sinfo)
+/* Initialize for a series of LZWReadByte (and hence GetCode) calls */
+{
+  /* GetCode initialization */
+  sinfo->last_byte = 2;		/* make safe to "recopy last two bytes" */
+  sinfo->last_bit = 0;		/* nothing in the buffer */
+  sinfo->cur_bit = 0;		/* force buffer load on first call */
+  sinfo->out_of_blocks = FALSE;
+
+  /* LZWReadByte initialization: */
+  /* compute special code values (note that these do not change later) */
+  sinfo->clear_code = 1 << sinfo->input_code_size;
+  sinfo->end_code = sinfo->clear_code + 1;
+  sinfo->first_time = TRUE;
+  ReInitLZW(sinfo);
+}
+
+
+LOCAL int
+GetCode (gif_source_ptr sinfo)
+/* Fetch the next code_size bits from the GIF data */
+/* We assume code_size is less than 16 */
+{
+  register INT32 accum;
+  int offs, ret, count;
+
+  while ( (sinfo->cur_bit + sinfo->code_size) > sinfo->last_bit) {
+    /* Time to reload the buffer */
+    if (sinfo->out_of_blocks) {
+      WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
+      return sinfo->end_code;	/* fake something useful */
+    }
+    /* preserve last two bytes of what we have -- assume code_size <= 16 */
+    sinfo->code_buf[0] = sinfo->code_buf[sinfo->last_byte-2];
+    sinfo->code_buf[1] = sinfo->code_buf[sinfo->last_byte-1];
+    /* Load more bytes; set flag if we reach the terminator block */
+    if ((count = GetDataBlock(sinfo, &sinfo->code_buf[2])) == 0) {
+      sinfo->out_of_blocks = TRUE;
+      WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
+      return sinfo->end_code;	/* fake something useful */
+    }
+    /* Reset counters */
+    sinfo->cur_bit = (sinfo->cur_bit - sinfo->last_bit) + 16;
+    sinfo->last_byte = 2 + count;
+    sinfo->last_bit = sinfo->last_byte * 8;
+  }
+
+  /* Form up next 24 bits in accum */
+  offs = sinfo->cur_bit >> 3;	/* byte containing cur_bit */
+#ifdef CHAR_IS_UNSIGNED
+  accum = sinfo->code_buf[offs+2];
+  accum <<= 8;
+  accum |= sinfo->code_buf[offs+1];
+  accum <<= 8;
+  accum |= sinfo->code_buf[offs];
+#else
+  accum = sinfo->code_buf[offs+2] & 0xFF;
+  accum <<= 8;
+  accum |= sinfo->code_buf[offs+1] & 0xFF;
+  accum <<= 8;
+  accum |= sinfo->code_buf[offs] & 0xFF;
+#endif
+
+  /* Right-align cur_bit in accum, then mask off desired number of bits */
+  accum >>= (sinfo->cur_bit & 7);
+  ret = ((int) accum) & ((1 << sinfo->code_size) - 1);
+  
+  sinfo->cur_bit += sinfo->code_size;
+  return ret;
+}
+
+
+LOCAL int
+LZWReadByte (gif_source_ptr sinfo)
+/* Read an LZW-compressed byte */
+{
+  register int code;		/* current working code */
+  int incode;			/* saves actual input code */
+
+  /* First time, just eat the expected Clear code(s) and return next code, */
+  /* which is expected to be a raw byte. */
+  if (sinfo->first_time) {
+    sinfo->first_time = FALSE;
+    code = sinfo->clear_code;	/* enables sharing code with Clear case */
+  } else {
+
+    /* If any codes are stacked from a previously read symbol, return them */
+    if (sinfo->sp > sinfo->symbol_stack)
+      return (int) *(-- sinfo->sp);
+
+    /* Time to read a new symbol */
+    code = GetCode(sinfo);
+
+  }
+
+  if (code == sinfo->clear_code) {
+    /* Reinit state, swallow any extra Clear codes, and */
+    /* return next code, which is expected to be a raw byte. */
+    ReInitLZW(sinfo);
+    do {
+      code = GetCode(sinfo);
+    } while (code == sinfo->clear_code);
+    if (code > sinfo->clear_code) { /* make sure it is a raw byte */
+      WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
+      code = 0;			/* use something valid */
+    }
+    /* make firstcode, oldcode valid! */
+    sinfo->firstcode = sinfo->oldcode = code;
+    return code;
+  }
+
+  if (code == sinfo->end_code) {
+    /* Skip the rest of the image, unless GetCode already read terminator */
+    if (! sinfo->out_of_blocks) {
+      SkipDataBlocks(sinfo);
+      sinfo->out_of_blocks = TRUE;
+    }
+    /* Complain that there's not enough data */
+    WARNMS(sinfo->cinfo, JWRN_GIF_ENDCODE);
+    /* Pad data with 0's */
+    return 0;			/* fake something usable */
+  }
+
+  /* Got normal raw byte or LZW symbol */
+  incode = code;		/* save for a moment */
+  
+  if (code >= sinfo->max_code) { /* special case for not-yet-defined symbol */
+    /* code == max_code is OK; anything bigger is bad data */
+    if (code > sinfo->max_code) {
+      WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
+      incode = 0;		/* prevent creation of loops in symbol table */
+    }
+    /* this symbol will be defined as oldcode/firstcode */
+    *(sinfo->sp++) = (UINT8) sinfo->firstcode;
+    code = sinfo->oldcode;
+  }
+
+  /* If it's a symbol, expand it into the stack */
+  while (code >= sinfo->clear_code) {
+    *(sinfo->sp++) = sinfo->symbol_tail[code]; /* tail is a byte value */
+    code = sinfo->symbol_head[code]; /* head is another LZW symbol */
+  }
+  /* At this point code just represents a raw byte */
+  sinfo->firstcode = code;	/* save for possible future use */
+
+  /* If there's room in table, */
+  if ((code = sinfo->max_code) < LZW_TABLE_SIZE) {
+    /* Define a new symbol = prev sym + head of this sym's expansion */
+    sinfo->symbol_head[code] = sinfo->oldcode;
+    sinfo->symbol_tail[code] = (UINT8) sinfo->firstcode;
+    sinfo->max_code++;
+    /* Is it time to increase code_size? */
+    if ((sinfo->max_code >= sinfo->limit_code) &&
+	(sinfo->code_size < MAX_LZW_BITS)) {
+      sinfo->code_size++;
+      sinfo->limit_code <<= 1;	/* keep equal to 2^code_size */
+    }
+  }
+  
+  sinfo->oldcode = incode;	/* save last input symbol for future use */
+  return sinfo->firstcode;	/* return first byte of symbol's expansion */
+}
+
+
+LOCAL void
+ReadColorMap (gif_source_ptr sinfo, int cmaplen, JSAMPARRAY cmap)
+/* Read a GIF colormap */
+{
+  int i;
+
+  for (i = 0; i < cmaplen; i++) {
+    cmap[CM_RED][i]   = (JSAMPLE) ReadByte(sinfo);
+    cmap[CM_GREEN][i] = (JSAMPLE) ReadByte(sinfo);
+    cmap[CM_BLUE][i]  = (JSAMPLE) ReadByte(sinfo);
+  }
+}
+
+
+LOCAL void
+DoExtension (gif_source_ptr sinfo)
+/* Process an extension block */
+/* Currently we ignore 'em all */
+{
+  int extlabel;
+
+  /* Read extension label byte */
+  extlabel = ReadByte(sinfo);
+  TRACEMS1(sinfo->cinfo, 1, JTRC_GIF_EXTENSION, extlabel);
+  /* Skip the data block(s) associated with the extension */
+  SkipDataBlocks(sinfo);
+}
+
+
+/*
+ * Read the file header; return image size and component count.
+ */
+
+METHODDEF void
+start_input_gif (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  gif_source_ptr source = (gif_source_ptr) sinfo;
+  char hdrbuf[10];		/* workspace for reading control blocks */
+  unsigned int width, height;	/* image dimensions */
+  int colormaplen, aspectRatio;
+  int c;
+
+  /* Allocate space to store the colormap */
+  source->colormap = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     (JDIMENSION) MAXCOLORMAPSIZE, (JDIMENSION) NUMCOLORS);
+
+  /* Read and verify GIF Header */
+  if (! ReadOK(source->pub.input_file, hdrbuf, 6))
+    ERREXIT(cinfo, JERR_GIF_NOT);
+  if (hdrbuf[0] != 'G' || hdrbuf[1] != 'I' || hdrbuf[2] != 'F')
+    ERREXIT(cinfo, JERR_GIF_NOT);
+  /* Check for expected version numbers.
+   * If unknown version, give warning and try to process anyway;
+   * this is per recommendation in GIF89a standard.
+   */
+  if ((hdrbuf[3] != '8' || hdrbuf[4] != '7' || hdrbuf[5] != 'a') &&
+      (hdrbuf[3] != '8' || hdrbuf[4] != '9' || hdrbuf[5] != 'a'))
+    TRACEMS3(cinfo, 1, JTRC_GIF_BADVERSION, hdrbuf[3], hdrbuf[4], hdrbuf[5]);
+
+  /* Read and decipher Logical Screen Descriptor */
+  if (! ReadOK(source->pub.input_file, hdrbuf, 7))
+    ERREXIT(cinfo, JERR_INPUT_EOF);
+  width = LM_to_uint(hdrbuf[0],hdrbuf[1]);
+  height = LM_to_uint(hdrbuf[2],hdrbuf[3]);
+  colormaplen = 2 << (hdrbuf[4] & 0x07);
+  /* we ignore the color resolution, sort flag, and background color index */
+  aspectRatio = hdrbuf[6] & 0xFF;
+  if (aspectRatio != 0 && aspectRatio != 49)
+    TRACEMS(cinfo, 1, JTRC_GIF_NONSQUARE);
+
+  /* Read global colormap if header indicates it is present */
+  if (BitSet(hdrbuf[4], COLORMAPFLAG))
+    ReadColorMap(source, colormaplen, source->colormap);
+
+  /* Scan until we reach start of desired image.
+   * We don't currently support skipping images, but could add it easily.
+   */
+  for (;;) {
+    c = ReadByte(source);
+
+    if (c == ';')		/* GIF terminator?? */
+      ERREXIT(cinfo, JERR_GIF_IMAGENOTFOUND);
+
+    if (c == '!') {		/* Extension */
+      DoExtension(source);
+      continue;
+    }
+    
+    if (c != ',') {		/* Not an image separator? */
+      WARNMS1(cinfo, JWRN_GIF_CHAR, c);
+      continue;
+    }
+
+    /* Read and decipher Local Image Descriptor */
+    if (! ReadOK(source->pub.input_file, hdrbuf, 9))
+      ERREXIT(cinfo, JERR_INPUT_EOF);
+    /* we ignore top/left position info, also sort flag */
+    width = LM_to_uint(hdrbuf[4],hdrbuf[5]);
+    height = LM_to_uint(hdrbuf[6],hdrbuf[7]);
+    source->is_interlaced = BitSet(hdrbuf[8], INTERLACE);
+
+    /* Read local colormap if header indicates it is present */
+    /* Note: if we wanted to support skipping images, */
+    /* we'd need to skip rather than read colormap for ignored images */
+    if (BitSet(hdrbuf[8], COLORMAPFLAG)) {
+      colormaplen = 2 << (hdrbuf[8] & 0x07);
+      ReadColorMap(source, colormaplen, source->colormap);
+    }
+
+    source->input_code_size = ReadByte(source); /* get min-code-size byte */
+    if (source->input_code_size < 2 || source->input_code_size >= MAX_LZW_BITS)
+      ERREXIT1(cinfo, JERR_GIF_CODESIZE, source->input_code_size);
+
+    /* Reached desired image, so break out of loop */
+    /* If we wanted to skip this image, */
+    /* we'd call SkipDataBlocks and then continue the loop */
+    break;
+  }
+
+  /* Prepare to read selected image: first initialize LZW decompressor */
+  source->symbol_head = (UINT16 FAR *)
+    (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				LZW_TABLE_SIZE * SIZEOF(UINT16));
+  source->symbol_tail = (UINT8 FAR *)
+    (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				LZW_TABLE_SIZE * SIZEOF(UINT8));
+  source->symbol_stack = (UINT8 FAR *)
+    (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				LZW_TABLE_SIZE * SIZEOF(UINT8));
+  InitLZWCode(source);
+
+  /*
+   * If image is interlaced, we read it into a full-size sample array,
+   * decompressing as we go; then get_interlaced_row selects rows from the
+   * sample array in the proper order.
+   */
+  if (source->is_interlaced) {
+    /* We request the virtual array now, but can't access it until virtual
+     * arrays have been allocated.  Hence, the actual work of reading the
+     * image is postponed until the first call to get_pixel_rows.
+     */
+    source->interlaced_image = (*cinfo->mem->request_virt_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       (JDIMENSION) width, (JDIMENSION) height, (JDIMENSION) 1);
+    if (cinfo->progress != NULL) {
+      cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+      progress->total_extra_passes++; /* count file input as separate pass */
+    }
+    source->pub.get_pixel_rows = load_interlaced_image;
+  } else {
+    source->pub.get_pixel_rows = get_pixel_rows;
+  }
+
+  /* Create compressor input buffer. */
+  source->pub.buffer = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     (JDIMENSION) width * NUMCOLORS, (JDIMENSION) 1);
+  source->pub.buffer_height = 1;
+
+  /* Return info about the image. */
+  cinfo->in_color_space = JCS_RGB;
+  cinfo->input_components = NUMCOLORS;
+  cinfo->data_precision = 8;
+  cinfo->image_width = width;
+  cinfo->image_height = height;
+
+  TRACEMS3(cinfo, 1, JTRC_GIF, width, height, colormaplen);
+}
+
+
+/*
+ * Read one row of pixels.
+ * This version is used for noninterlaced GIF images:
+ * we read directly from the GIF file.
+ */
+
+METHODDEF JDIMENSION
+get_pixel_rows (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  gif_source_ptr source = (gif_source_ptr) sinfo;
+  register int c;
+  register JSAMPROW ptr;
+  register JDIMENSION col;
+  register JSAMPARRAY colormap = source->colormap;
+  
+  ptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    c = LZWReadByte(source);
+    *ptr++ = colormap[CM_RED][c];
+    *ptr++ = colormap[CM_GREEN][c];
+    *ptr++ = colormap[CM_BLUE][c];
+  }
+  return 1;
+}
+
+
+/*
+ * Read one row of pixels.
+ * This version is used for the first call on get_pixel_rows when
+ * reading an interlaced GIF file: we read the whole image into memory.
+ */
+
+METHODDEF JDIMENSION
+load_interlaced_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  gif_source_ptr source = (gif_source_ptr) sinfo;
+  JSAMPARRAY image_ptr;
+  register JSAMPROW sptr;
+  register JDIMENSION col;
+  JDIMENSION row;
+  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+
+  /* Read the interlaced image into the virtual array we've created. */
+  for (row = 0; row < cinfo->image_height; row++) {
+    if (progress != NULL) {
+      progress->pub.pass_counter = (long) row;
+      progress->pub.pass_limit = (long) cinfo->image_height;
+      (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+    }
+    image_ptr = (*cinfo->mem->access_virt_sarray)
+      ((j_common_ptr) cinfo, source->interlaced_image, row, TRUE);
+    sptr = image_ptr[0];
+    for (col = cinfo->image_width; col > 0; col--) {
+      *sptr++ = (JSAMPLE) LZWReadByte(source);
+    }
+  }
+  if (progress != NULL)
+    progress->completed_extra_passes++;
+
+  /* Replace method pointer so subsequent calls don't come here. */
+  source->pub.get_pixel_rows = get_interlaced_row;
+  /* Initialize for get_interlaced_row, and perform first call on it. */
+  source->cur_row_number = 0;
+  source->pass2_offset = (cinfo->image_height + 7) / 8;
+  source->pass3_offset = source->pass2_offset + (cinfo->image_height + 3) / 8;
+  source->pass4_offset = source->pass3_offset + (cinfo->image_height + 1) / 4;
+
+  return get_interlaced_row(cinfo, sinfo);
+}
+
+
+/*
+ * Read one row of pixels.
+ * This version is used for interlaced GIF images:
+ * we read from the virtual array.
+ */
+
+METHODDEF JDIMENSION
+get_interlaced_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  gif_source_ptr source = (gif_source_ptr) sinfo;
+  JSAMPARRAY image_ptr;
+  register int c;
+  register JSAMPROW sptr, ptr;
+  register JDIMENSION col;
+  register JSAMPARRAY colormap = source->colormap;
+  JDIMENSION irow;
+
+  /* Figure out which row of interlaced image is needed, and access it. */
+  switch ((int) (source->cur_row_number & 7)) {
+  case 0:			/* first-pass row */
+    irow = source->cur_row_number >> 3;
+    break;
+  case 4:			/* second-pass row */
+    irow = (source->cur_row_number >> 3) + source->pass2_offset;
+    break;
+  case 2:			/* third-pass row */
+  case 6:
+    irow = (source->cur_row_number >> 2) + source->pass3_offset;
+    break;
+  default:			/* fourth-pass row */
+    irow = (source->cur_row_number >> 1) + source->pass4_offset;
+    break;
+  }
+  image_ptr = (*cinfo->mem->access_virt_sarray)
+    ((j_common_ptr) cinfo, source->interlaced_image, irow, FALSE);
+  /* Scan the row, expand colormap, and output */
+  sptr = image_ptr[0];
+  ptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    c = GETJSAMPLE(*sptr++);
+    *ptr++ = colormap[CM_RED][c];
+    *ptr++ = colormap[CM_GREEN][c];
+    *ptr++ = colormap[CM_BLUE][c];
+  }
+  source->cur_row_number++;	/* for next time */
+  return 1;
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF void
+finish_input_gif (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  /* no work */
+}
+
+
+/*
+ * The module selection routine for GIF format input.
+ */
+
+GLOBAL cjpeg_source_ptr
+jinit_read_gif (j_compress_ptr cinfo)
+{
+  gif_source_ptr source;
+
+  /* Create module interface object */
+  source = (gif_source_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(gif_source_struct));
+  source->cinfo = cinfo;	/* make back link for subroutines */
+  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
+  source->pub.start_input = start_input_gif;
+  source->pub.finish_input = finish_input_gif;
+
+  return (cjpeg_source_ptr) source;
+}
+
+#endif /* GIF_SUPPORTED */
diff --git a/rdjpgcom.1 b/rdjpgcom.1
new file mode 100644
index 0000000..d237bb8
--- /dev/null
+++ b/rdjpgcom.1
@@ -0,0 +1,44 @@
+.TH RDJPGCOM 1 "8 July 1994"
+.SH NAME
+rdjpgcom \- display text comments from a JPEG file
+.SH SYNOPSIS
+.B rdjpgcom
+[
+.B \-verbose
+]
+[
+.I filename
+]
+.LP
+.SH DESCRIPTION
+.LP
+.B rdjpgcom
+reads the named JPEG/JFIF file, or the standard input if no file is named,
+and prints any text comments found in the file on the standard output.
+.PP
+The JPEG standard allows "comment" (COM) blocks to occur within a JPEG file.
+Although the standard doesn't actually define what COM blocks are for, they
+are widely used to hold user-supplied text strings.  This lets you add
+annotations, titles, index terms, etc to your JPEG files, and later retrieve
+them as text.  COM blocks do not interfere with the image stored in the JPEG
+file.  The maximum size of a COM block is 64K, but you can have as many of
+them as you like in one JPEG file.
+.SH OPTIONS
+.TP
+.B \-verbose
+Causes
+.B rdjpgcom
+to also display the JPEG image dimensions.
+.PP
+Switch names may be abbreviated, and are not case sensitive.
+.SH HINTS
+.B rdjpgcom
+does not depend on the IJG JPEG library.  Its source code is intended as an
+illustration of the minimum amount of code required to parse a JPEG file
+header correctly.
+.SH SEE ALSO
+.BR cjpeg (1),
+.BR djpeg (1),
+.BR wrjpgcom (1)
+.SH AUTHOR
+Independent JPEG Group
diff --git a/rdjpgcom.c b/rdjpgcom.c
new file mode 100644
index 0000000..d121dc9
--- /dev/null
+++ b/rdjpgcom.c
@@ -0,0 +1,475 @@
+/*
+ * rdjpgcom.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a very simple stand-alone application that displays
+ * the text in COM (comment) markers in a JFIF file.
+ * This may be useful as an example of the minimum logic needed to parse
+ * JPEG markers.
+ */
+
+#define JPEG_CJPEG_DJPEG	/* to get the command-line config symbols */
+#include "jinclude.h"		/* get auto-config symbols, <stdio.h> */
+
+#include <ctype.h>		/* to declare isupper(), tolower() */
+#ifdef USE_SETMODE
+#include <fcntl.h>		/* to declare setmode()'s parameter macros */
+/* If you have setmode() but not <io.h>, just delete this line: */
+#include <io.h>			/* to declare setmode() */
+#endif
+
+#ifdef USE_CCOMMAND		/* command-line reader for Macintosh */
+#ifdef __MWERKS__
+#include <SIOUX.h>              /* Metrowerks declares it here */
+#endif
+#ifdef THINK_C
+#include <console.h>		/* Think declares it here */
+#endif
+#endif
+
+#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
+#define READ_BINARY	"r"
+#else
+#define READ_BINARY	"rb"
+#endif
+
+#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
+#define EXIT_FAILURE  1
+#endif
+#ifndef EXIT_SUCCESS
+#ifdef VMS
+#define EXIT_SUCCESS  1		/* VMS is very nonstandard */
+#else
+#define EXIT_SUCCESS  0
+#endif
+#endif
+
+
+/*
+ * These macros are used to read the input file.
+ * To reuse this code in another application, you might need to change these.
+ */
+
+static FILE * infile;		/* input JPEG file */
+
+/* Return next input byte, or EOF if no more */
+#define NEXTBYTE()  getc(infile)
+
+
+/* Error exit handler */
+#define ERREXIT(msg)  (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
+
+
+/* Read one byte, testing for EOF */
+static int
+read_1_byte (void)
+{
+  int c;
+
+  c = NEXTBYTE();
+  if (c == EOF)
+    ERREXIT("Premature EOF in JPEG file");
+  return c;
+}
+
+/* Read 2 bytes, convert to unsigned int */
+/* All 2-byte quantities in JPEG markers are MSB first */
+static unsigned int
+read_2_bytes (void)
+{
+  int c1, c2;
+
+  c1 = NEXTBYTE();
+  if (c1 == EOF)
+    ERREXIT("Premature EOF in JPEG file");
+  c2 = NEXTBYTE();
+  if (c2 == EOF)
+    ERREXIT("Premature EOF in JPEG file");
+  return (((unsigned int) c1) << 8) + ((unsigned int) c2);
+}
+
+
+/*
+ * JPEG markers consist of one or more 0xFF bytes, followed by a marker
+ * code byte (which is not an FF).  Here are the marker codes of interest
+ * in this program.  (See jdmarker.c for a more complete list.)
+ */
+
+#define M_SOF0  0xC0		/* Start Of Frame N */
+#define M_SOF1  0xC1		/* N indicates which compression process */
+#define M_SOF2  0xC2		/* Only SOF0 and SOF1 are now in common use */
+#define M_SOF3  0xC3
+#define M_SOF5  0xC5
+#define M_SOF6  0xC6
+#define M_SOF7  0xC7
+#define M_SOF9  0xC9
+#define M_SOF10 0xCA
+#define M_SOF11 0xCB
+#define M_SOF13 0xCD
+#define M_SOF14 0xCE
+#define M_SOF15 0xCF
+#define M_SOI   0xD8		/* Start Of Image (beginning of datastream) */
+#define M_EOI   0xD9		/* End Of Image (end of datastream) */
+#define M_SOS   0xDA		/* Start Of Scan (begins compressed data) */
+#define M_COM   0xFE		/* COMment */
+
+
+/*
+ * Find the next JPEG marker and return its marker code.
+ * We expect at least one FF byte, possibly more if the compressor used FFs
+ * to pad the file.
+ * There could also be non-FF garbage between markers.  The treatment of such
+ * garbage is unspecified; we choose to skip over it but emit a warning msg.
+ * NB: this routine must not be used after seeing SOS marker, since it will
+ * not deal correctly with FF/00 sequences in the compressed image data...
+ */
+
+static int
+next_marker (void)
+{
+  int c;
+  int discarded_bytes = 0;
+
+  /* Find 0xFF byte; count and skip any non-FFs. */
+  c = read_1_byte();
+  while (c != 0xFF) {
+    discarded_bytes++;
+    c = read_1_byte();
+  }
+  /* Get marker code byte, swallowing any duplicate FF bytes.  Extra FFs
+   * are legal as pad bytes, so don't count them in discarded_bytes.
+   */
+  do {
+    c = read_1_byte();
+  } while (c == 0xFF);
+
+  if (discarded_bytes != 0) {
+    fprintf(stderr, "Warning: garbage data found in JPEG file\n");
+  }
+
+  return c;
+}
+
+
+/*
+ * Read the initial marker, which should be SOI.
+ * For a JFIF file, the first two bytes of the file should be literally
+ * 0xFF M_SOI.  To be more general, we could use next_marker, but if the
+ * input file weren't actually JPEG at all, next_marker might read the whole
+ * file and then return a misleading error message...
+ */
+
+static int
+first_marker (void)
+{
+  int c1, c2;
+
+  c1 = NEXTBYTE();
+  c2 = NEXTBYTE();
+  if (c1 != 0xFF || c2 != M_SOI)
+    ERREXIT("Not a JPEG file");
+  return c2;
+}
+
+
+/*
+ * Most types of marker are followed by a variable-length parameter segment.
+ * This routine skips over the parameters for any marker we don't otherwise
+ * want to process.
+ * Note that we MUST skip the parameter segment explicitly in order not to
+ * be fooled by 0xFF bytes that might appear within the parameter segment;
+ * such bytes do NOT introduce new markers.
+ */
+
+static void
+skip_variable (void)
+/* Skip over an unknown or uninteresting variable-length marker */
+{
+  unsigned int length;
+
+  /* Get the marker parameter length count */
+  length = read_2_bytes();
+  /* Length includes itself, so must be at least 2 */
+  if (length < 2)
+    ERREXIT("Erroneous JPEG marker length");
+  length -= 2;
+  /* Skip over the remaining bytes */
+  while (length > 0) {
+    (void) read_1_byte();
+    length--;
+  }
+}
+
+
+/*
+ * Process a COM marker.
+ * We want to print out the marker contents as legible text;
+ * we must guard against random junk and varying newline representations.
+ */
+
+static void
+process_COM (void)
+{
+  unsigned int length;
+  int ch;
+  int lastch = 0;
+
+  /* Get the marker parameter length count */
+  length = read_2_bytes();
+  /* Length includes itself, so must be at least 2 */
+  if (length < 2)
+    ERREXIT("Erroneous JPEG marker length");
+  length -= 2;
+
+  while (length > 0) {
+    ch = read_1_byte();
+    /* Emit the character in a readable form.
+     * Nonprintables are converted to \nnn form,
+     * while \ is converted to \\.
+     * Newlines in CR, CR/LF, or LF form will be printed as one newline.
+     */
+    if (ch == '\r') {
+      printf("\n");
+    } else if (ch == '\n') {
+      if (lastch != '\r')
+	printf("\n");
+    } else if (ch == '\\') {
+      printf("\\\\");
+    } else if (isprint(ch)) {
+      putc(ch, stdout);
+    } else {
+      printf("\\%03o", ch);
+    }
+    lastch = ch;
+    length--;
+  }
+  printf("\n");
+}
+
+
+/*
+ * Process a SOFn marker.
+ * This code is only needed if you want to know the image dimensions...
+ */
+
+static void
+process_SOFn (int marker)
+{
+  unsigned int length;
+  unsigned int image_height, image_width;
+  int data_precision, num_components;
+  const char * process;
+  int ci, c1, c2, c3;
+
+  length = read_2_bytes();	/* usual parameter length count */
+
+  data_precision = read_1_byte();
+  image_height = read_2_bytes();
+  image_width = read_2_bytes();
+  num_components = read_1_byte();
+
+  switch (marker) {
+  case M_SOF0:	process = "Baseline";  break;
+  case M_SOF1:	process = "Extended sequential";  break;
+  case M_SOF2:	process = "Progressive";  break;
+  case M_SOF3:	process = "Lossless";  break;
+  case M_SOF5:	process = "Differential sequential";  break;
+  case M_SOF6:	process = "Differential progressive";  break;
+  case M_SOF7:	process = "Differential lossless";  break;
+  case M_SOF9:	process = "Extended sequential, arithmetic coding";  break;
+  case M_SOF10:	process = "Progressive, arithmetic coding";  break;
+  case M_SOF11:	process = "Lossless, arithmetic coding";  break;
+  case M_SOF13:	process = "Differential sequential, arithmetic coding";  break;
+  case M_SOF14:	process = "Differential progressive, arithmetic coding"; break;
+  case M_SOF15:	process = "Differential lossless, arithmetic coding";  break;
+  default:	process = "Unknown";  break;
+  }
+
+  printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
+	 image_width, image_height, num_components, data_precision);
+  printf("JPEG process: %s\n", process);
+
+  if (length != (unsigned int) (8 + num_components * 3))
+    ERREXIT("Bogus SOF marker length");
+
+  for (ci = 0; ci < num_components; ci++) {
+    c1 = read_1_byte();		/* Component ID code */
+    c2 = read_1_byte();		/* H, V sampling factors */
+    c3 = read_1_byte();		/* Quant table number */
+  }
+}
+
+
+/*
+ * Parse the marker stream until SOS or EOI is seen;
+ * display any COM markers.
+ * While the companion program wrjpgcom will always insert COM markers before
+ * SOFn, other implementations might not, so we scan to SOS before stopping.
+ * If we were only interested in the image dimensions, we would stop at SOFn.
+ * (Conversely, if we only cared about COM markers, there would be no need
+ * for special code to handle SOFn; we could treat it like other markers.)
+ */
+
+static int
+scan_JPEG_header (int verbose)
+{
+  int marker;
+
+  /* Expect SOI at start of file */
+  if (first_marker() != M_SOI)
+    ERREXIT("Expected SOI marker first");
+
+  /* Scan miscellaneous markers until we reach SOS. */
+  for (;;) {
+    marker = next_marker();
+    switch (marker) {
+    case M_SOF0:		/* Baseline */
+    case M_SOF1:		/* Extended sequential, Huffman */
+    case M_SOF2:		/* Progressive, Huffman */
+    case M_SOF3:		/* Lossless, Huffman */
+    case M_SOF5:		/* Differential sequential, Huffman */
+    case M_SOF6:		/* Differential progressive, Huffman */
+    case M_SOF7:		/* Differential lossless, Huffman */
+    case M_SOF9:		/* Extended sequential, arithmetic */
+    case M_SOF10:		/* Progressive, arithmetic */
+    case M_SOF11:		/* Lossless, arithmetic */
+    case M_SOF13:		/* Differential sequential, arithmetic */
+    case M_SOF14:		/* Differential progressive, arithmetic */
+    case M_SOF15:		/* Differential lossless, arithmetic */
+      if (verbose)
+	process_SOFn(marker);
+      else
+	skip_variable();
+      break;
+
+    case M_SOS:			/* stop before hitting compressed data */
+      return marker;
+
+    case M_EOI:			/* in case it's a tables-only JPEG stream */
+      return marker;
+
+    case M_COM:
+      process_COM();
+      break;
+
+    default:			/* Anything else just gets skipped */
+      skip_variable();		/* we assume it has a parameter count... */
+      break;
+    }
+  } /* end loop */
+}
+
+
+/* Command line parsing code */
+
+static const char * progname;	/* program name for error messages */
+
+
+static void
+usage (void)
+/* complain about bad command line */
+{
+  fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n");
+
+  fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname);
+
+  fprintf(stderr, "Switches (names may be abbreviated):\n");
+  fprintf(stderr, "  -verbose    Also display dimensions of JPEG image\n");
+
+  exit(EXIT_FAILURE);
+}
+
+
+static int
+keymatch (char * arg, const char * keyword, int minchars)
+/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
+/* keyword is the constant keyword (must be lower case already), */
+/* minchars is length of minimum legal abbreviation. */
+{
+  register int ca, ck;
+  register int nmatched = 0;
+
+  while ((ca = *arg++) != '\0') {
+    if ((ck = *keyword++) == '\0')
+      return 0;			/* arg longer than keyword, no good */
+    if (isupper(ca))		/* force arg to lcase (assume ck is already) */
+      ca = tolower(ca);
+    if (ca != ck)
+      return 0;			/* no good */
+    nmatched++;			/* count matched characters */
+  }
+  /* reached end of argument; fail if it's too short for unique abbrev */
+  if (nmatched < minchars)
+    return 0;
+  return 1;			/* A-OK */
+}
+
+
+/*
+ * The main program.
+ */
+
+int
+main (int argc, char **argv)
+{
+  int argn;
+  char * arg;
+  int verbose = 0;
+
+  /* On Mac, fetch a command line. */
+#ifdef USE_CCOMMAND
+  argc = ccommand(&argv);
+#endif
+
+  progname = argv[0];
+  if (progname == NULL || progname[0] == 0)
+    progname = "rdjpgcom";	/* in case C library doesn't provide it */
+
+  /* Parse switches, if any */
+  for (argn = 1; argn < argc; argn++) {
+    arg = argv[argn];
+    if (arg[0] != '-')
+      break;			/* not switch, must be file name */
+    arg++;			/* advance over '-' */
+    if (keymatch(arg, "verbose", 1)) {
+      verbose++;
+    } else
+      usage();
+  }
+
+  /* Open the input file. */
+  /* Unix style: expect zero or one file name */
+  if (argn < argc-1) {
+    fprintf(stderr, "%s: only one input file\n", progname);
+    usage();
+  }
+  if (argn < argc) {
+    if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
+      exit(EXIT_FAILURE);
+    }
+  } else {
+    /* default input file is stdin */
+#ifdef USE_SETMODE		/* need to hack file mode? */
+    setmode(fileno(stdin), O_BINARY);
+#endif
+#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
+    if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open stdin\n", progname);
+      exit(EXIT_FAILURE);
+    }
+#else
+    infile = stdin;
+#endif
+  }
+
+  /* Scan the JPEG headers. */
+  (void) scan_JPEG_header(verbose);
+
+  /* All done. */
+  exit(EXIT_SUCCESS);
+  return 0;			/* suppress no-return-value warnings */
+}
diff --git a/rdppm.c b/rdppm.c
new file mode 100644
index 0000000..6aa8e6f
--- /dev/null
+++ b/rdppm.c
@@ -0,0 +1,384 @@
+/*
+ * rdppm.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in PPM/PGM format.
+ * The PBMPLUS library is NOT required to compile this software
+ * (but it is highly useful as a set of PPM image manipulation programs).
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume input from
+ * an ordinary stdio stream.  They further assume that reading begins
+ * at the start of the file; start_input may need work if the
+ * user interface has already read some data (e.g., to determine that
+ * the file is indeed PPM format).
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef PPM_SUPPORTED
+
+
+/* Portions of this code are based on the PBMPLUS library, which is:
+**
+** Copyright (C) 1988 by Jef Poskanzer.
+**
+** Permission to use, copy, modify, and distribute this software and its
+** documentation for any purpose and without fee is hereby granted, provided
+** that the above copyright notice appear in all copies and that both that
+** copyright notice and this permission notice appear in supporting
+** documentation.  This software is provided "as is" without express or
+** implied warranty.
+*/
+
+
+/* Macros to deal with unsigned chars as efficiently as compiler allows */
+
+#ifdef HAVE_UNSIGNED_CHAR
+typedef unsigned char U_CHAR;
+#define UCH(x)	((int) (x))
+#else /* !HAVE_UNSIGNED_CHAR */
+#ifdef CHAR_IS_UNSIGNED
+typedef char U_CHAR;
+#define UCH(x)	((int) (x))
+#else
+typedef char U_CHAR;
+#define UCH(x)	((int) (x) & 0xFF)
+#endif
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+#define	ReadOK(file,buffer,len)	(JFREAD(file,buffer,len) == ((size_t) (len)))
+
+
+/*
+ * On most systems, reading individual bytes with getc() is drastically less
+ * efficient than buffering a row at a time with fread().  On PCs, we must
+ * allocate the buffer in near data space, because we are assuming small-data
+ * memory model, wherein fread() can't reach far memory.  If you need to
+ * process very wide images on a PC, you might have to compile in large-memory
+ * model, or else replace fread() with a getc() loop --- which will be much
+ * slower.
+ */
+
+
+/* Private version of data source object */
+
+typedef struct {
+  struct cjpeg_source_struct pub; /* public fields */
+
+  U_CHAR *iobuffer;		/* non-FAR pointer to I/O buffer */
+  JSAMPROW pixrow;		/* FAR pointer to same */
+  JDIMENSION buffer_width;	/* width of one row */
+  JSAMPLE *rescale;		/* => maxval-remapping array, or NULL */
+} ppm_source_struct;
+
+typedef ppm_source_struct * ppm_source_ptr;
+
+
+LOCAL int
+pbm_getc (FILE * infile)
+/* Read next char, skipping over any comments */
+/* A comment/newline sequence is returned as a newline */
+{
+  register int ch;
+  
+  ch = getc(infile);
+  if (ch == '#') {
+    do {
+      ch = getc(infile);
+    } while (ch != '\n' && ch != EOF);
+  }
+  return ch;
+}
+
+
+LOCAL unsigned int
+read_pbm_integer (j_compress_ptr cinfo, FILE * infile)
+/* Read an unsigned decimal integer from the PPM file */
+/* Swallows one trailing character after the integer */
+/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
+/* This should not be a problem in practice. */
+{
+  register int ch;
+  register unsigned int val;
+  
+  /* Skip any leading whitespace */
+  do {
+    ch = pbm_getc(infile);
+    if (ch == EOF)
+      ERREXIT(cinfo, JERR_INPUT_EOF);
+  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
+  
+  if (ch < '0' || ch > '9')
+    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
+  
+  val = ch - '0';
+  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
+    val *= 10;
+    val += ch - '0';
+  }
+  return val;
+}
+
+
+/*
+ * Read one row of pixels.
+ *
+ * We provide several different versions depending on input file format.
+ * In all cases, input is scaled to the size of JSAMPLE.
+ *
+ * Note that a really fast path is provided for reading raw files with
+ * maxval = MAXJSAMPLE, which is the normal case (at least for 8-bit JSAMPLEs).
+ */
+
+
+METHODDEF JDIMENSION
+get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading text-format PGM files with any maxval */
+{
+  ppm_source_ptr source = (ppm_source_ptr) sinfo;
+  FILE * infile = source->pub.input_file;
+  register JSAMPROW ptr;
+  register JSAMPLE *rescale = source->rescale;
+  JDIMENSION col;
+  
+  ptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
+  }
+  return 1;
+}
+
+
+METHODDEF JDIMENSION
+get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading text-format PPM files with any maxval */
+{
+  ppm_source_ptr source = (ppm_source_ptr) sinfo;
+  FILE * infile = source->pub.input_file;
+  register JSAMPROW ptr;
+  register JSAMPLE *rescale = source->rescale;
+  JDIMENSION col;
+  
+  ptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
+    *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
+    *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
+  }
+  return 1;
+}
+
+
+METHODDEF JDIMENSION
+get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading raw-format PGM files with any maxval */
+{
+  ppm_source_ptr source = (ppm_source_ptr) sinfo;
+  register JSAMPROW ptr;
+  register U_CHAR * bufferptr;
+  register JSAMPLE *rescale = source->rescale;
+  JDIMENSION col;
+
+  if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
+    ERREXIT(cinfo, JERR_INPUT_EOF);
+  ptr = source->pub.buffer[0];
+  bufferptr = source->iobuffer;
+  for (col = cinfo->image_width; col > 0; col--) {
+    *ptr++ = rescale[UCH(*bufferptr++)];
+  }
+  return 1;
+}
+
+
+METHODDEF JDIMENSION
+get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading raw-format PPM files with any maxval */
+{
+  ppm_source_ptr source = (ppm_source_ptr) sinfo;
+  register JSAMPROW ptr;
+  register U_CHAR * bufferptr;
+  register JSAMPLE *rescale = source->rescale;
+  JDIMENSION col;
+
+  if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
+    ERREXIT(cinfo, JERR_INPUT_EOF);
+  ptr = source->pub.buffer[0];
+  bufferptr = source->iobuffer;
+  for (col = cinfo->image_width; col > 0; col--) {
+    *ptr++ = rescale[UCH(*bufferptr++)];
+    *ptr++ = rescale[UCH(*bufferptr++)];
+    *ptr++ = rescale[UCH(*bufferptr++)];
+  }
+  return 1;
+}
+
+
+METHODDEF JDIMENSION
+get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading raw-format files with maxval = MAXJSAMPLE. */
+/* In this case we just read right into the JSAMPLE buffer! */
+/* Note that same code works for PPM and PGM files. */
+{
+  ppm_source_ptr source = (ppm_source_ptr) sinfo;
+
+  if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
+    ERREXIT(cinfo, JERR_INPUT_EOF);
+  return 1;
+}
+
+
+/*
+ * Read the file header; return image size and component count.
+ */
+
+METHODDEF void
+start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  ppm_source_ptr source = (ppm_source_ptr) sinfo;
+  int c;
+  unsigned int w, h, maxval;
+  boolean can_use_raw;
+
+  if (getc(source->pub.input_file) != 'P')
+    ERREXIT(cinfo, JERR_PPM_NOT);
+
+  c = getc(source->pub.input_file); /* save format discriminator for a sec */
+
+  /* while we fetch the remaining header info */
+  w = read_pbm_integer(cinfo, source->pub.input_file);
+  h = read_pbm_integer(cinfo, source->pub.input_file);
+  maxval = read_pbm_integer(cinfo, source->pub.input_file);
+
+  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
+    ERREXIT(cinfo, JERR_PPM_NOT);
+
+  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
+  cinfo->image_width = (JDIMENSION) w;
+  cinfo->image_height = (JDIMENSION) h;
+
+  /* Raw PPM/PGM matches JSAMPLE representation iff: */
+  can_use_raw = (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR));
+
+  switch (c) {
+  case '2':			/* it's a text-format PGM file */
+    cinfo->input_components = 1;
+    cinfo->in_color_space = JCS_GRAYSCALE;
+    TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
+    source->pub.get_pixel_rows = get_text_gray_row;
+    can_use_raw = FALSE;	/* force a rescale array to be made */
+    break;
+
+  case '3':			/* it's a text-format PPM file */
+    cinfo->input_components = 3;
+    cinfo->in_color_space = JCS_RGB;
+    TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
+    source->pub.get_pixel_rows = get_text_rgb_row;
+    can_use_raw = FALSE;	/* force a rescale array to be made */
+    break;
+
+  case '5':			/* it's a raw-format PGM file */
+    cinfo->input_components = 1;
+    cinfo->in_color_space = JCS_GRAYSCALE;
+    TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
+    if (can_use_raw)
+      source->pub.get_pixel_rows = get_raw_row;
+    else
+      source->pub.get_pixel_rows = get_scaled_gray_row;
+    /* allocate space for I/O buffer: 1 byte/pixel */
+    source->iobuffer = (U_CHAR *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  (size_t) (SIZEOF(U_CHAR) * (size_t) w));
+    break;
+
+  case '6':			/* it's a raw-format PPM file */
+    cinfo->input_components = 3;
+    cinfo->in_color_space = JCS_RGB;
+    TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
+    if (can_use_raw)
+      source->pub.get_pixel_rows = get_raw_row;
+    else
+      source->pub.get_pixel_rows = get_scaled_rgb_row;
+    /* allocate space for I/O buffer: 3 bytes/pixel */
+    source->iobuffer = (U_CHAR *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  (size_t) (3 * SIZEOF(U_CHAR) * (size_t) w));
+    break;
+
+  default:
+    ERREXIT(cinfo, JERR_PPM_NOT);
+    break;
+  }
+
+  /* Create compressor input buffer. */
+  source->buffer_width = (JDIMENSION) w * cinfo->input_components;
+  if (can_use_raw) {
+    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
+    /* Synthesize a JSAMPARRAY pointer structure */
+    /* Cast here implies near->far pointer conversion on PCs */
+    source->pixrow = (JSAMPROW) source->iobuffer;
+    source->pub.buffer = & source->pixrow;
+    source->pub.buffer_height = 1;
+  } else {
+    /* Need to translate anyway, so make a separate sample buffer. */
+    source->pub.buffer = (*cinfo->mem->alloc_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       source->buffer_width, (JDIMENSION) 1);
+    source->pub.buffer_height = 1;
+  }
+
+  /* Compute the rescaling array if required (we use it for all but raw) */
+  if (can_use_raw) {
+    source->rescale = NULL;	/* no rescaling required */
+  } else {
+    INT32 val, half_maxval;
+
+    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
+    source->rescale = (JSAMPLE *)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  (size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE)));
+    half_maxval = maxval / 2;
+    for (val = 0; val <= (INT32) maxval; val++) {
+      /* The multiplication here must be done in 32 bits to avoid overflow */
+      source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval);
+    }
+  }
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF void
+finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  /* no work */
+}
+
+
+/*
+ * The module selection routine for PPM format input.
+ */
+
+GLOBAL cjpeg_source_ptr
+jinit_read_ppm (j_compress_ptr cinfo)
+{
+  ppm_source_ptr source;
+
+  /* Create module interface object */
+  source = (ppm_source_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(ppm_source_struct));
+  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
+  source->pub.start_input = start_input_ppm;
+  source->pub.finish_input = finish_input_ppm;
+
+  return (cjpeg_source_ptr) source;
+}
+
+#endif /* PPM_SUPPORTED */
diff --git a/rdrle.c b/rdrle.c
new file mode 100644
index 0000000..b58eb75
--- /dev/null
+++ b/rdrle.c
@@ -0,0 +1,384 @@
+/*
+ * rdrle.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in Utah RLE format.
+ * The Utah Raster Toolkit library is required (version 3.1 or later).
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume input from
+ * an ordinary stdio stream.  They further assume that reading begins
+ * at the start of the file; start_input may need work if the
+ * user interface has already read some data (e.g., to determine that
+ * the file is indeed RLE format).
+ *
+ * Based on code contributed by Mike Lijewski,
+ * with updates from Robert Hutchinson.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef RLE_SUPPORTED
+
+/* rle.h is provided by the Utah Raster Toolkit. */
+
+#include <rle.h>
+
+/*
+ * We assume that JSAMPLE has the same representation as rle_pixel,
+ * to wit, "unsigned char".  Hence we can't cope with 12- or 16-bit samples.
+ */
+
+#if BITS_IN_JSAMPLE != 8
+  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
+#endif
+
+/*
+ * We support the following types of RLE files:
+ *   
+ *   GRAYSCALE   - 8 bits, no colormap
+ *   MAPPEDGRAY  - 8 bits, 1 channel colomap
+ *   PSEUDOCOLOR - 8 bits, 3 channel colormap
+ *   TRUECOLOR   - 24 bits, 3 channel colormap
+ *   DIRECTCOLOR - 24 bits, no colormap
+ *
+ * For now, we ignore any alpha channel in the image.
+ */
+
+typedef enum
+  { GRAYSCALE, MAPPEDGRAY, PSEUDOCOLOR, TRUECOLOR, DIRECTCOLOR } rle_kind;
+
+
+/*
+ * Since RLE stores scanlines bottom-to-top, we have to invert the image
+ * to conform to JPEG's top-to-bottom order.  To do this, we read the
+ * incoming image into a virtual array on the first get_pixel_rows call,
+ * then fetch the required row from the virtual array on subsequent calls.
+ */
+
+typedef struct _rle_source_struct * rle_source_ptr;
+
+typedef struct _rle_source_struct {
+  struct cjpeg_source_struct pub; /* public fields */
+
+  rle_kind visual;              /* actual type of input file */
+  jvirt_sarray_ptr image;       /* virtual array to hold the image */
+  rle_hdr header;               /* Input file information */
+  rle_pixel** rle_row;          /* holds a row returned by rle_getrow() */
+
+} rle_source_struct;
+
+
+/*
+ * Read the file header; return image size and component count.
+ */
+
+METHODDEF void
+start_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  rle_source_ptr source = (rle_source_ptr) sinfo;
+  JDIMENSION width, height;
+#ifdef PROGRESS_REPORT
+  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+#endif
+
+  /* Use RLE library routine to get the header info */
+  source->header = *rle_hdr_init(NULL);
+  source->header.rle_file = source->pub.input_file;
+  switch (rle_get_setup(&(source->header))) {
+  case RLE_SUCCESS:
+    /* A-OK */
+    break;
+  case RLE_NOT_RLE:
+    ERREXIT(cinfo, JERR_RLE_NOT);
+    break;
+  case RLE_NO_SPACE:
+    ERREXIT(cinfo, JERR_RLE_MEM);
+    break;
+  case RLE_EMPTY:
+    ERREXIT(cinfo, JERR_RLE_EMPTY);
+    break;
+  case RLE_EOF:
+    ERREXIT(cinfo, JERR_RLE_EOF);
+    break;
+  default:
+    ERREXIT(cinfo, JERR_RLE_BADERROR);
+    break;
+  }
+
+  /* Figure out what we have, set private vars and return values accordingly */
+  
+  width  = source->header.xmax - source->header.xmin + 1;
+  height = source->header.ymax - source->header.ymin + 1;
+  source->header.xmin = 0;		/* realign horizontally */
+  source->header.xmax = width-1;
+
+  cinfo->image_width      = width;
+  cinfo->image_height     = height;
+  cinfo->data_precision   = 8;  /* we can only handle 8 bit data */
+
+  if (source->header.ncolors == 1 && source->header.ncmap == 0) {
+    source->visual     = GRAYSCALE;
+    TRACEMS2(cinfo, 1, JTRC_RLE_GRAY, width, height);
+  } else if (source->header.ncolors == 1 && source->header.ncmap == 1) {
+    source->visual     = MAPPEDGRAY;
+    TRACEMS3(cinfo, 1, JTRC_RLE_MAPGRAY, width, height,
+             1 << source->header.cmaplen);
+  } else if (source->header.ncolors == 1 && source->header.ncmap == 3) {
+    source->visual     = PSEUDOCOLOR;
+    TRACEMS3(cinfo, 1, JTRC_RLE_MAPPED, width, height,
+	     1 << source->header.cmaplen);
+  } else if (source->header.ncolors == 3 && source->header.ncmap == 3) {
+    source->visual     = TRUECOLOR;
+    TRACEMS3(cinfo, 1, JTRC_RLE_FULLMAP, width, height,
+	     1 << source->header.cmaplen);
+  } else if (source->header.ncolors == 3 && source->header.ncmap == 0) {
+    source->visual     = DIRECTCOLOR;
+    TRACEMS2(cinfo, 1, JTRC_RLE, width, height);
+  } else
+    ERREXIT(cinfo, JERR_RLE_UNSUPPORTED);
+  
+  if (source->visual == GRAYSCALE || source->visual == MAPPEDGRAY) {
+    cinfo->in_color_space   = JCS_GRAYSCALE;
+    cinfo->input_components = 1;
+  } else {
+    cinfo->in_color_space   = JCS_RGB;
+    cinfo->input_components = 3;
+  }
+
+  /*
+   * A place to hold each scanline while it's converted.
+   * (GRAYSCALE scanlines don't need converting)
+   */
+  if (source->visual != GRAYSCALE) {
+    source->rle_row = (rle_pixel**) (*cinfo->mem->alloc_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       (JDIMENSION) width, (JDIMENSION) cinfo->input_components);
+  }
+
+  /* request a virtual array to hold the image */
+  source->image = (*cinfo->mem->request_virt_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     (JDIMENSION) (width * source->header.ncolors),
+     (JDIMENSION) height, (JDIMENSION) 1);
+
+#ifdef PROGRESS_REPORT
+  if (progress != NULL) {
+    /* count file input as separate pass */
+    progress->total_extra_passes++;
+  }
+#endif
+
+  source->pub.buffer_height = 1;
+}
+
+
+/*
+ * Read one row of pixels.
+ * Called only after load_image has read the image into the virtual array.
+ * Used for GRAYSCALE, MAPPEDGRAY, TRUECOLOR, and DIRECTCOLOR images.
+ */
+
+METHODDEF JDIMENSION
+get_rle_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  rle_source_ptr source = (rle_source_ptr) sinfo;
+
+  source->pub.buffer = (*cinfo->mem->access_virt_sarray)
+    ((j_common_ptr) cinfo, source->image, cinfo->next_scanline, FALSE);
+
+  return 1;
+}
+
+/*
+ * Read one row of pixels.
+ * Called only after load_image has read the image into the virtual array.
+ * Used for PSEUDOCOLOR images.
+ */
+
+METHODDEF JDIMENSION
+get_pseudocolor_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  rle_source_ptr source = (rle_source_ptr) sinfo;
+  JSAMPROW src_row, dest_row;
+  JDIMENSION col;
+  rle_map *colormap;
+  int val;
+
+  colormap = source->header.cmap;
+  dest_row = source->pub.buffer[0];
+  src_row = * (*cinfo->mem->access_virt_sarray)
+    ((j_common_ptr) cinfo, source->image, cinfo->next_scanline, FALSE);
+
+  for (col = cinfo->image_width; col > 0; col--) {
+    val = GETJSAMPLE(*src_row++);
+    *dest_row++ = colormap[val      ] >> 8;
+    *dest_row++ = colormap[val + 256] >> 8;
+    *dest_row++ = colormap[val + 512] >> 8;
+  }
+
+  return 1;
+}
+
+
+/*
+ * Load the image into a virtual array.  We have to do this because RLE
+ * files start at the lower left while the JPEG standard has them starting
+ * in the upper left.  This is called the first time we want to get a row
+ * of input.  What we do is load the RLE data into the array and then call
+ * the appropriate routine to read one row from the array.  Before returning,
+ * we set source->pub.get_pixel_rows so that subsequent calls go straight to
+ * the appropriate row-reading routine.
+ */
+
+METHODDEF JDIMENSION
+load_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  rle_source_ptr source = (rle_source_ptr) sinfo;
+  JDIMENSION row, col;
+  JSAMPROW  scanline, red_ptr, green_ptr, blue_ptr;
+  rle_pixel **rle_row;
+  rle_map *colormap;
+  char channel;
+#ifdef PROGRESS_REPORT
+  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+#endif
+
+  colormap = source->header.cmap;
+  rle_row = source->rle_row;
+  row = cinfo->image_height;
+
+  /* Read the RLE data into our virtual array.
+   * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
+   * and (b) we are not on a machine where FAR pointers differ from regular.
+   */
+  RLE_CLR_BIT(source->header, RLE_ALPHA); /* don't read the alpha channel */
+
+#ifdef PROGRESS_REPORT
+  if (progress != NULL) {
+    progress->pub.pass_limit = cinfo->image_height;
+    progress->pub.pass_counter = 0;
+    (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+  }
+#endif
+
+  switch (source->visual) {
+
+  case GRAYSCALE:
+  case PSEUDOCOLOR:
+    while (row--) {
+      rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
+         ((j_common_ptr) cinfo, source->image, row, TRUE);
+      rle_getrow(&source->header, rle_row);
+#ifdef PROGRESS_REPORT
+      if (progress != NULL) {
+        progress->pub.pass_counter++;
+        (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+      }
+#endif
+    }
+    break;
+
+  case MAPPEDGRAY:
+  case TRUECOLOR:
+    while (row--) {
+      scanline = * (*cinfo->mem->access_virt_sarray)
+        ((j_common_ptr) cinfo, source->image, row, TRUE);
+      rle_row = source->rle_row;
+      rle_getrow(&source->header, rle_row);
+
+      for (col = 0; col < cinfo->image_width; col++) {
+        for (channel = 0; channel < source->header.ncolors; channel++) {
+          *scanline++ = (JSAMPLE)
+            colormap[GETJSAMPLE(rle_row[channel][col]) + 256 * channel] >> 8;
+        }
+      }
+
+#ifdef PROGRESS_REPORT
+      if (progress != NULL) {
+        progress->pub.pass_counter++;
+        (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+      }
+#endif
+    }
+    break;
+
+  case DIRECTCOLOR:
+    while (row--) {
+      scanline = * (*cinfo->mem->access_virt_sarray)
+        ((j_common_ptr) cinfo, source->image, row, TRUE);
+      rle_getrow(&source->header, rle_row);
+
+      red_ptr   = rle_row[0];
+      green_ptr = rle_row[1];
+      blue_ptr  = rle_row[2];
+
+      for (col = cinfo->image_width; col > 0; col--) {
+        *scanline++ = *red_ptr++;
+        *scanline++ = *green_ptr++;
+        *scanline++ = *blue_ptr++;
+      }
+
+#ifdef PROGRESS_REPORT
+      if (progress != NULL) {
+        progress->pub.pass_counter++;
+        (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+      }
+#endif
+    }
+  }
+
+#ifdef PROGRESS_REPORT
+  if (progress != NULL)
+    progress->completed_extra_passes++;
+#endif
+
+  /* Set up to call proper row-extraction routine in future */
+  if (source->visual == PSEUDOCOLOR) {
+    source->pub.buffer = source->rle_row;
+    source->pub.get_pixel_rows = get_pseudocolor_row;
+  } else {
+    source->pub.get_pixel_rows = get_rle_row;
+  }
+
+  /* And fetch the topmost (bottommost) row */
+  return (*source->pub.get_pixel_rows) (cinfo, sinfo);   
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF void
+finish_input_rle (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  /* no work */
+}
+
+
+/*
+ * The module selection routine for RLE format input.
+ */
+
+GLOBAL cjpeg_source_ptr
+jinit_read_rle (j_compress_ptr cinfo)
+{
+  rle_source_ptr source;
+
+  /* Create module interface object */
+  source = (rle_source_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+                                  SIZEOF(rle_source_struct));
+  /* Fill in method ptrs */
+  source->pub.start_input = start_input_rle;
+  source->pub.finish_input = finish_input_rle;
+  source->pub.get_pixel_rows = load_image;
+
+  return (cjpeg_source_ptr) source;
+}
+
+#endif /* RLE_SUPPORTED */
diff --git a/rdtarga.c b/rdtarga.c
new file mode 100644
index 0000000..8c58138
--- /dev/null
+++ b/rdtarga.c
@@ -0,0 +1,499 @@
+/*
+ * rdtarga.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to read input images in Targa format.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume input from
+ * an ordinary stdio stream.  They further assume that reading begins
+ * at the start of the file; start_input may need work if the
+ * user interface has already read some data (e.g., to determine that
+ * the file is indeed Targa format).
+ *
+ * Based on code contributed by Lee Daniel Crocker.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef TARGA_SUPPORTED
+
+
+/* Macros to deal with unsigned chars as efficiently as compiler allows */
+
+#ifdef HAVE_UNSIGNED_CHAR
+typedef unsigned char U_CHAR;
+#define UCH(x)	((int) (x))
+#else /* !HAVE_UNSIGNED_CHAR */
+#ifdef CHAR_IS_UNSIGNED
+typedef char U_CHAR;
+#define UCH(x)	((int) (x))
+#else
+typedef char U_CHAR;
+#define UCH(x)	((int) (x) & 0xFF)
+#endif
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+#define	ReadOK(file,buffer,len)	(JFREAD(file,buffer,len) == ((size_t) (len)))
+
+
+/* Private version of data source object */
+
+typedef struct _tga_source_struct * tga_source_ptr;
+
+typedef struct _tga_source_struct {
+  struct cjpeg_source_struct pub; /* public fields */
+
+  j_compress_ptr cinfo;		/* back link saves passing separate parm */
+
+  JSAMPARRAY colormap;		/* Targa colormap (converted to my format) */
+
+  jvirt_sarray_ptr whole_image;	/* Needed if funny input row order */
+  JDIMENSION current_row;	/* Current logical row number to read */
+
+  /* Pointer to routine to extract next Targa pixel from input file */
+  JMETHOD(void, read_pixel, (tga_source_ptr sinfo));
+
+  /* Result of read_pixel is delivered here: */
+  U_CHAR tga_pixel[4];
+
+  int pixel_size;		/* Bytes per Targa pixel (1 to 4) */
+
+  /* State info for reading RLE-coded pixels; both counts must be init to 0 */
+  int block_count;		/* # of pixels remaining in RLE block */
+  int dup_pixel_count;		/* # of times to duplicate previous pixel */
+
+  /* This saves the correct pixel-row-expansion method for preload_image */
+  JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo,
+				       cjpeg_source_ptr sinfo));
+} tga_source_struct;
+
+
+/* For expanding 5-bit pixel values to 8-bit with best rounding */
+
+static const UINT8 c5to8bits[32] = {
+    0,   8,  16,  24,  32,  41,  49,  57,
+   65,  74,  82,  90,  98, 106, 115, 123,
+  131, 139, 148, 156, 164, 172, 180, 189,
+  197, 205, 213, 222, 230, 238, 246, 255
+};
+
+
+
+LOCAL int
+read_byte (tga_source_ptr sinfo)
+/* Read next byte from Targa file */
+{
+  register FILE *infile = sinfo->pub.input_file;
+  register int c;
+
+  if ((c = getc(infile)) == EOF)
+    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
+  return c;
+}
+
+
+LOCAL void
+read_colormap (tga_source_ptr sinfo, int cmaplen, int mapentrysize)
+/* Read the colormap from a Targa file */
+{
+  int i;
+
+  /* Presently only handles 24-bit BGR format */
+  if (mapentrysize != 24)
+    ERREXIT(sinfo->cinfo, JERR_TGA_BADCMAP);
+
+  for (i = 0; i < cmaplen; i++) {
+    sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
+    sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
+    sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
+  }
+}
+
+
+/*
+ * read_pixel methods: get a single pixel from Targa file into tga_pixel[]
+ */
+
+LOCAL void
+read_non_rle_pixel (tga_source_ptr sinfo)
+/* Read one Targa pixel from the input file; no RLE expansion */
+{
+  register FILE *infile = sinfo->pub.input_file;
+  register int i;
+
+  for (i = 0; i < sinfo->pixel_size; i++) {
+    sinfo->tga_pixel[i] = (U_CHAR) getc(infile);
+  }
+}
+
+
+LOCAL void
+read_rle_pixel (tga_source_ptr sinfo)
+/* Read one Targa pixel from the input file, expanding RLE data as needed */
+{
+  register FILE *infile = sinfo->pub.input_file;
+  register int i;
+
+  /* Duplicate previously read pixel? */
+  if (sinfo->dup_pixel_count > 0) {
+    sinfo->dup_pixel_count--;
+    return;
+  }
+
+  /* Time to read RLE block header? */
+  if (--sinfo->block_count < 0) { /* decrement pixels remaining in block */
+    i = read_byte(sinfo);
+    if (i & 0x80) {		/* Start of duplicate-pixel block? */
+      sinfo->dup_pixel_count = i & 0x7F; /* number of dups after this one */
+      sinfo->block_count = 0;	/* then read new block header */
+    } else {
+      sinfo->block_count = i & 0x7F; /* number of pixels after this one */
+    }
+  }
+
+  /* Read next pixel */
+  for (i = 0; i < sinfo->pixel_size; i++) {
+    sinfo->tga_pixel[i] = (U_CHAR) getc(infile);
+  }
+}
+
+
+/*
+ * Read one row of pixels.
+ *
+ * We provide several different versions depending on input file format.
+ */
+
+
+METHODDEF JDIMENSION
+get_8bit_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 8-bit grayscale pixels */
+{
+  tga_source_ptr source = (tga_source_ptr) sinfo;
+  register JSAMPROW ptr;
+  register JDIMENSION col;
+  
+  ptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
+    *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]);
+  }
+  return 1;
+}
+
+METHODDEF JDIMENSION
+get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 8-bit colormap indexes */
+{
+  tga_source_ptr source = (tga_source_ptr) sinfo;
+  register int t;
+  register JSAMPROW ptr;
+  register JDIMENSION col;
+  register JSAMPARRAY colormap = source->colormap;
+
+  ptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
+    t = UCH(source->tga_pixel[0]);
+    *ptr++ = colormap[0][t];
+    *ptr++ = colormap[1][t];
+    *ptr++ = colormap[2][t];
+  }
+  return 1;
+}
+
+METHODDEF JDIMENSION
+get_16bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 16-bit pixels */
+{
+  tga_source_ptr source = (tga_source_ptr) sinfo;
+  register int t;
+  register JSAMPROW ptr;
+  register JDIMENSION col;
+  
+  ptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
+    t = UCH(source->tga_pixel[0]);
+    t += UCH(source->tga_pixel[1]) << 8;
+    /* We expand 5 bit data to 8 bit sample width.
+     * The format of the 16-bit (LSB first) input word is
+     *     xRRRRRGGGGGBBBBB
+     */
+    ptr[2] = (JSAMPLE) c5to8bits[t & 0x1F];
+    t >>= 5;
+    ptr[1] = (JSAMPLE) c5to8bits[t & 0x1F];
+    t >>= 5;
+    ptr[0] = (JSAMPLE) c5to8bits[t & 0x1F];
+    ptr += 3;
+  }
+  return 1;
+}
+
+METHODDEF JDIMENSION
+get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+/* This version is for reading 24-bit pixels */
+{
+  tga_source_ptr source = (tga_source_ptr) sinfo;
+  register JSAMPROW ptr;
+  register JDIMENSION col;
+  
+  ptr = source->pub.buffer[0];
+  for (col = cinfo->image_width; col > 0; col--) {
+    (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
+    *ptr++ = (JSAMPLE) UCH(source->tga_pixel[2]); /* change BGR to RGB order */
+    *ptr++ = (JSAMPLE) UCH(source->tga_pixel[1]);
+    *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]);
+  }
+  return 1;
+}
+
+/*
+ * Targa also defines a 32-bit pixel format with order B,G,R,A.
+ * We presently ignore the attribute byte, so the code for reading
+ * these pixels is identical to the 24-bit routine above.
+ * This works because the actual pixel length is only known to read_pixel.
+ */
+
+#define get_32bit_row  get_24bit_row
+
+
+/*
+ * This method is for re-reading the input data in standard top-down
+ * row order.  The entire image has already been read into whole_image
+ * with proper conversion of pixel format, but it's in a funny row order.
+ */
+
+METHODDEF JDIMENSION
+get_memory_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  tga_source_ptr source = (tga_source_ptr) sinfo;
+  JDIMENSION source_row;
+
+  /* Compute row of source that maps to current_row of normal order */
+  /* For now, assume image is bottom-up and not interlaced. */
+  /* NEEDS WORK to support interlaced images! */
+  source_row = cinfo->image_height - source->current_row - 1;
+
+  /* Fetch that row from virtual array */
+  source->pub.buffer = (*cinfo->mem->access_virt_sarray)
+    ((j_common_ptr) cinfo, source->whole_image, source_row, FALSE);
+
+  source->current_row++;
+  return 1;
+}
+
+
+/*
+ * This method loads the image into whole_image during the first call on
+ * get_pixel_rows.  The get_pixel_rows pointer is then adjusted to call
+ * get_memory_row on subsequent calls.
+ */
+
+METHODDEF JDIMENSION
+preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  tga_source_ptr source = (tga_source_ptr) sinfo;
+  JDIMENSION row;
+  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+
+  /* Read the data into a virtual array in input-file row order. */
+  for (row = 0; row < cinfo->image_height; row++) {
+    if (progress != NULL) {
+      progress->pub.pass_counter = (long) row;
+      progress->pub.pass_limit = (long) cinfo->image_height;
+      (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+    }
+    source->pub.buffer = (*cinfo->mem->access_virt_sarray)
+      ((j_common_ptr) cinfo, source->whole_image, row, TRUE);
+    (*source->get_pixel_rows) (cinfo, sinfo);
+  }
+  if (progress != NULL)
+    progress->completed_extra_passes++;
+
+  /* Set up to read from the virtual array in unscrambled order */
+  source->pub.get_pixel_rows = get_memory_row;
+  source->current_row = 0;
+  /* And read the first row */
+  return get_memory_row(cinfo, sinfo);
+}
+
+
+/*
+ * Read the file header; return image size and component count.
+ */
+
+METHODDEF void
+start_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  tga_source_ptr source = (tga_source_ptr) sinfo;
+  U_CHAR targaheader[18];
+  int idlen, cmaptype, subtype, flags, interlace_type, components;
+  unsigned int width, height, maplen;
+  boolean is_bottom_up;
+
+#define GET_2B(offset)	((unsigned int) UCH(targaheader[offset]) + \
+			 (((unsigned int) UCH(targaheader[offset+1])) << 8))
+
+  if (! ReadOK(source->pub.input_file, targaheader, 18))
+    ERREXIT(cinfo, JERR_INPUT_EOF);
+
+  /* Pretend "15-bit" pixels are 16-bit --- we ignore attribute bit anyway */
+  if (targaheader[16] == 15)
+    targaheader[16] = 16;
+
+  idlen = UCH(targaheader[0]);
+  cmaptype = UCH(targaheader[1]);
+  subtype = UCH(targaheader[2]);
+  maplen = GET_2B(5);
+  width = GET_2B(12);
+  height = GET_2B(14);
+  source->pixel_size = UCH(targaheader[16]) >> 3;
+  flags = UCH(targaheader[17]);	/* Image Descriptor byte */
+
+  is_bottom_up = ((flags & 0x20) == 0);	/* bit 5 set => top-down */
+  interlace_type = flags >> 6;	/* bits 6/7 are interlace code */
+
+  if (cmaptype > 1 ||		/* cmaptype must be 0 or 1 */
+      source->pixel_size < 1 || source->pixel_size > 4 ||
+      (UCH(targaheader[16]) & 7) != 0 || /* bits/pixel must be multiple of 8 */
+      interlace_type != 0)	/* currently don't allow interlaced image */
+    ERREXIT(cinfo, JERR_TGA_BADPARMS);
+  
+  if (subtype > 8) {
+    /* It's an RLE-coded file */
+    source->read_pixel = read_rle_pixel;
+    source->block_count = source->dup_pixel_count = 0;
+    subtype -= 8;
+  } else {
+    /* Non-RLE file */
+    source->read_pixel = read_non_rle_pixel;
+  }
+
+  /* Now should have subtype 1, 2, or 3 */
+  components = 3;		/* until proven different */
+  cinfo->in_color_space = JCS_RGB;
+
+  switch (subtype) {
+  case 1:			/* Colormapped image */
+    if (source->pixel_size == 1 && cmaptype == 1)
+      source->get_pixel_rows = get_8bit_row;
+    else
+      ERREXIT(cinfo, JERR_TGA_BADPARMS);
+    TRACEMS2(cinfo, 1, JTRC_TGA_MAPPED, width, height);
+    break;
+  case 2:			/* RGB image */
+    switch (source->pixel_size) {
+    case 2:
+      source->get_pixel_rows = get_16bit_row;
+      break;
+    case 3:
+      source->get_pixel_rows = get_24bit_row;
+      break;
+    case 4:
+      source->get_pixel_rows = get_32bit_row;
+      break;
+    default:
+      ERREXIT(cinfo, JERR_TGA_BADPARMS);
+      break;
+    }
+    TRACEMS2(cinfo, 1, JTRC_TGA, width, height);
+    break;
+  case 3:			/* Grayscale image */
+    components = 1;
+    cinfo->in_color_space = JCS_GRAYSCALE;
+    if (source->pixel_size == 1)
+      source->get_pixel_rows = get_8bit_gray_row;
+    else
+      ERREXIT(cinfo, JERR_TGA_BADPARMS);
+    TRACEMS2(cinfo, 1, JTRC_TGA_GRAY, width, height);
+    break;
+  default:
+    ERREXIT(cinfo, JERR_TGA_BADPARMS);
+    break;
+  }
+
+  if (is_bottom_up) {
+    /* Create a virtual array to buffer the upside-down image. */
+    source->whole_image = (*cinfo->mem->request_virt_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       (JDIMENSION) width * components, (JDIMENSION) height, (JDIMENSION) 1);
+    if (cinfo->progress != NULL) {
+      cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+      progress->total_extra_passes++; /* count file input as separate pass */
+    }
+    /* source->pub.buffer will point to the virtual array. */
+    source->pub.buffer_height = 1; /* in case anyone looks at it */
+    source->pub.get_pixel_rows = preload_image;
+  } else {
+    /* Don't need a virtual array, but do need a one-row input buffer. */
+    source->whole_image = NULL;
+    source->pub.buffer = (*cinfo->mem->alloc_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       (JDIMENSION) width * components, (JDIMENSION) 1);
+    source->pub.buffer_height = 1;
+    source->pub.get_pixel_rows = source->get_pixel_rows;
+  }
+  
+  while (idlen--)		/* Throw away ID field */
+    (void) read_byte(source);
+
+  if (maplen > 0) {
+    if (maplen > 256 || GET_2B(3) != 0)
+      ERREXIT(cinfo, JERR_TGA_BADCMAP);
+    /* Allocate space to store the colormap */
+    source->colormap = (*cinfo->mem->alloc_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) maplen, (JDIMENSION) 3);
+    /* and read it from the file */
+    read_colormap(source, (int) maplen, UCH(targaheader[7]));
+  } else {
+    if (cmaptype)		/* but you promised a cmap! */
+      ERREXIT(cinfo, JERR_TGA_BADPARMS);
+    source->colormap = NULL;
+  }
+
+  cinfo->input_components = components;
+  cinfo->data_precision = 8;
+  cinfo->image_width = width;
+  cinfo->image_height = height;
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF void
+finish_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
+{
+  /* no work */
+}
+
+
+/*
+ * The module selection routine for Targa format input.
+ */
+
+GLOBAL cjpeg_source_ptr
+jinit_read_targa (j_compress_ptr cinfo)
+{
+  tga_source_ptr source;
+
+  /* Create module interface object */
+  source = (tga_source_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(tga_source_struct));
+  source->cinfo = cinfo;	/* make back link for subroutines */
+  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
+  source->pub.start_input = start_input_tga;
+  source->pub.finish_input = finish_input_tga;
+
+  return (cjpeg_source_ptr) source;
+}
+
+#endif /* TARGA_SUPPORTED */
diff --git a/structure.doc b/structure.doc
new file mode 100644
index 0000000..30d5828
--- /dev/null
+++ b/structure.doc
@@ -0,0 +1,862 @@
+IJG JPEG LIBRARY:  SYSTEM ARCHITECTURE
+
+Copyright (C) 1991-1994, Thomas G. Lane.
+This file is part of the Independent JPEG Group's software.
+For conditions of distribution and use, see the accompanying README file.
+
+
+This file provides an overview of the architecture of the IJG JPEG software;
+that is, the functions of the various modules in the system and the interfaces
+between modules.  For more precise details about any data structure or calling
+convention, see the include files and comments in the source code.
+
+We assume that the reader is already somewhat familiar with the JPEG standard.
+The README file includes references for learning about JPEG.  The file
+libjpeg.doc describes the library from the viewpoint of an application
+programmer using the library; it's best to read that file before this one.
+Also, the file coderules.doc describes the coding style conventions we use.
+
+In this document, JPEG-specific terminology follows the JPEG standard:
+  A "component" means a color channel, e.g., Red or Luminance.
+  A "sample" is a single component value (i.e., one number in the image data).
+  A "coefficient" is a frequency coefficient (a DCT transform output number).
+  A "block" is an 8x8 group of samples or coefficients.
+  An "MCU" (minimum coded unit) is an interleaved set of blocks of size
+	determined by the sampling factors, or a single block in a
+	noninterleaved scan.
+We do not use the terms "pixel" and "sample" interchangeably.  When we say
+pixel, we mean an element of the full-size image, while a sample is an element
+of the downsampled image.  Thus the number of samples may vary across
+components while the number of pixels does not.  (This terminology is not used
+rigorously throughout the code, but it is used in places where confusion would
+otherwise result.)
+
+
+*** System features ***
+
+The IJG distribution contains two parts:
+  * A subroutine library for JPEG compression and decompression.
+  * cjpeg/djpeg, two simple applications that use the library to transform
+    JFIF JPEG files to and from several other image formats.
+cjpeg/djpeg are of no great intellectual complexity: they merely add a simple
+command-line user interface and I/O routines for several uncompressed image
+formats.  This document concentrates on the library itself.
+
+We desire the library to be capable of supporting all JPEG baseline and
+extended sequential DCT processes.  Progressive processes are also allowed
+for in the system architecture, although they are not likely to be
+implemented very soon.  Hierarchical processes are not supported.
+
+The library does not support the lossless (spatial) JPEG process.  Lossless
+JPEG shares little or no code with lossy JPEG, and would normally be used
+without the extensive pre- and post-processing provided by this library.
+We feel that lossless JPEG is better handled by a separate library.
+
+Within these limits, any set of compression parameters allowed by the JPEG
+spec should be readable for decompression.  (We can be more restrictive about
+what formats we can generate.)  Although the system design allows for all
+parameter values, some uncommon settings are not yet implemented and may
+never be; nonintegral sampling ratios are the prime example.  Furthermore,
+we treat 8-bit vs. 12-bit data precision as a compile-time switch, not a
+run-time option, because most machines can store 8-bit pixels much more
+compactly than 12-bit.
+
+For legal reasons, JPEG arithmetic coding is not currently supported, but
+extending the library to include it would be straightforward.
+
+By itself, the library handles only interchange JPEG datastreams --- in
+particular the widely used JFIF file format.  The library can be used by
+surrounding code to process interchange or abbreviated JPEG datastreams that
+are embedded in more complex file formats.  (For example, we anticipate that
+Sam Leffler's TIFF library will use this code to support the revised TIFF
+JPEG format.)
+
+The library includes a substantial amount of code that is not covered by the
+JPEG standard but is necessary for typical applications of JPEG.  These
+functions preprocess the image before JPEG compression or postprocess it after
+decompression.  They include colorspace conversion, downsampling/upsampling,
+and color quantization.  This code can be omitted if not needed.
+
+A wide range of quality vs. speed tradeoffs are possible in JPEG processing,
+and even more so in decompression postprocessing.  The decompression library
+provides multiple implementations that cover most of the useful tradeoffs,
+ranging from very-high-quality down to fast-preview operation.  On the
+compression side we have generally not provided low-quality choices, since
+compression is normally less time-critical.  It should be understood that the
+low-quality modes may not meet the JPEG standard's accuracy requirements;
+nonetheless, they are useful for viewers.
+
+
+*** Portability issues ***
+
+Portability is an essential requirement for the library.  The key portability
+issues that show up at the level of system architecture are:
+
+1.  Memory usage.  We want the code to be able to run on PC-class machines
+with limited memory.  Images should therefore be processed sequentially (in
+strips), to avoid holding the whole image in memory at once.  Where a
+full-image buffer is necessary, we should be able to use either virtual memory
+or temporary files.
+
+2.  Near/far pointer distinction.  To run efficiently on 80x86 machines, the
+code should distinguish "small" objects (kept in near data space) from
+"large" ones (kept in far data space).  This is an annoying restriction, but
+fortunately it does not impact code quality for less brain-damaged machines,
+and the source code clutter turns out to be minimal with sufficient use of
+pointer typedefs.
+
+3. Data precision.  We assume that "char" is at least 8 bits, "short" and
+"int" at least 16, "long" at least 32.  The code will work fine with larger
+data sizes, although memory may be used inefficiently in some cases.  However,
+the JPEG compressed datastream must ultimately appear on external storage as a
+sequence of 8-bit bytes if it is to conform to the standard.  This may pose a
+problem on machines where char is wider than 8 bits.  The library represents
+compressed data as an array of values of typedef JOCTET.  If no data type
+exactly 8 bits wide is available, custom data source and data destination
+modules must be written to unpack and pack the chosen JOCTET datatype into
+8-bit external representation.
+
+
+*** System overview ***
+
+The compressor and decompressor are each divided into two main sections:
+the JPEG compressor or decompressor proper, and the preprocessing or
+postprocessing functions.  The interface between these two sections is the
+image data that the official JPEG spec regards as its input or output: this
+data is in the colorspace to be used for compression, and it is downsampled
+to the sampling factors to be used.  The preprocessing and postprocessing
+steps are responsible for converting a normal image representation to or from
+this form.  (Those few applications that want to deal with YCbCr downsampled
+data can skip the preprocessing or postprocessing step.)
+
+Looking more closely, the compressor library contains the following main
+elements:
+
+  Preprocessing:
+    * Color space conversion (e.g., RGB to YCbCr).  This step may also
+      provide gamma adjustment.
+    * Edge expansion and downsampling.  Optionally, this step can do simple
+      smoothing --- this is often helpful for low-quality source data.
+  JPEG proper:
+    * MCU assembly, DCT, quantization.
+    * Entropy coding (Huffman or arithmetic).
+
+In addition to these modules we need overall control, marker generation,
+and support code (memory management & error handling).  There is also a
+module responsible for physically writing the output data --- typically
+this is just an interface to fwrite(), but some applications may need to
+do something else with the data.
+
+The decompressor library contains the following main elements:
+
+  JPEG proper:
+    * Entropy decoding (Huffman or arithmetic).
+    * Dequantization, inverse DCT, MCU disassembly.
+  Postprocessing:
+    * Upsampling.  Optionally, this step may be able to do more general
+      rescaling of the image.
+    * Color space conversion (e.g., YCbCr to RGB).  This step may also
+      provide gamma adjustment.
+    * Optional color quantization (e.g., reduction to 256 colors).
+    * Optional color precision reduction (e.g., 24-bit to 15-bit color).
+      [Not implemented in v5.]
+
+We also need overall control, marker parsing, and a data source module.
+The support code (memory management & error handling) can be shared with
+the compression half of the library.
+
+There may be several implementations of each of these elements, particularly
+in the decompressor, where a wide range of speed/quality tradeoffs is very
+useful.  It must be understood that some of the best speedups involve
+merging adjacent steps in the pipeline.  For example, upsampling, color space
+conversion, and color quantization might all be done at once when using a
+low-quality ordered-dither technique.  The system architecture is designed to
+allow such merging where appropriate.
+
+
+Note: it is convenient to regard edge expansion (padding to block boundaries)
+as a preprocessing/postprocessing function, even though the JPEG spec includes
+it in compression/decompression.  We do this because downsampling/upsampling
+can be simplified a little if they work on padded data: it's not necessary to
+have special cases at the right and bottom edges.  Therefore the interface
+buffer is always an integral number of blocks wide and high, and we expect
+compression preprocessing to pad the source data properly.  Padding will occur
+only to the next block (8-sample) boundary.  In an interleaved-scan situation,
+additional dummy blocks may be used to fill out MCUs, but the MCU assembly and
+disassembly logic will create or discard these blocks internally.  (This is
+advantageous for speed reasons, since we avoid DCTing the dummy blocks.
+It also permits a small reduction in file size, because the compressor can
+choose dummy block contents so as to minimize their size in compressed form.)
+Applications that wish to deal directly with the downsampled data must provide
+similar buffering and padding for odd-sized images.
+
+
+*** Poor man's object-oriented programming ***
+
+It should be clear by now that we have a lot of quasi-independent processing
+steps, many of which have several possible behaviors.  To avoid cluttering the
+code with lots of switch statements, we use a simple form of object-style
+programming to separate out the different possibilities.
+
+For example, two different color quantization algorithms could be implemented
+as two separate modules that present the same external interface; at runtime,
+the calling code will access the proper module indirectly through an "object".
+
+We can get the limited features we need while staying within portable C.
+The basic tool is a function pointer.  An "object" is just a struct
+containing one or more function pointer fields, each of which corresponds to
+a method name in real object-oriented languages.  During initialization we
+fill in the function pointers with references to whichever module we have
+determined we need to use in this run.  Then invocation of the module is done
+by indirecting through a function pointer; on most machines this is no more
+expensive than a switch statement, which would be the only other way of
+making the required run-time choice.  The really significant benefit, of
+course, is keeping the source code clean and well structured.
+
+We can also arrange to have private storage that varies between different
+implementations of the same kind of object.  We do this by making all the
+module-specific object structs be separately allocated entities, which will
+be accessed via pointers in the master compression or decompression struct.
+The "public" fields or methods for a given kind of object are specified by
+a commonly known struct.  But a module's initialization code can allocate
+a larger struct that contains the common struct as its first member, plus
+additional private fields.  With appropriate pointer casting, the module's
+internal functions can access these private fields.  (For a simple example,
+see jdatadst.c, which implements the external interface specified by struct
+jpeg_destination_mgr, but adds extra fields.)
+
+(Of course this would all be a lot easier if we were using C++, but we are
+not yet prepared to assume that everyone has a C++ compiler.)
+
+An important benefit of this scheme is that it is easy to provide multiple
+versions of any method, each tuned to a particular case.  While a lot of
+precalculation might be done to select an optimal implementation of a method,
+the cost per invocation is constant.  For example, the upsampling step might
+have a "generic" method, plus one or more "hardwired" methods for the most
+popular sampling factors; the hardwired methods would be faster because they'd
+use straight-line code instead of for-loops.  The cost to determine which
+method to use is paid only once, at startup, and the selection criteria are
+hidden from the callers of the method.
+
+This plan differs a little bit from usual object-oriented structures, in that
+only one instance of each object class will exist during execution.  The
+reason for having the class structure is that on different runs we may create
+different instances (choose to execute different modules).  You can think of
+the term "method" as denoting the common interface presented by a particular
+set of interchangeable functions, and "object" as denoting a group of related
+methods, or the total shared interface behavior of a group of modules.
+
+
+*** Overall control structure ***
+
+We previously mentioned the need for overall control logic in the compression
+and decompression libraries.  In IJG implementations prior to v5, overall
+control was mostly provided by "pipeline control" modules, which proved to be
+large, unwieldy, and hard to understand.  To improve the situation, the
+control logic has been subdivided into multiple modules.  The control modules
+consist of:
+
+1. Master control for module selection and initialization.  This has two
+responsibilities:
+
+   1A.  Startup initialization at the beginning of image processing.
+        The individual processing modules to be used in this run are selected
+        and given initialization calls.
+
+   1B.  Per-pass control.  This determines how many passes will be performed
+        and calls each active processing module to configure itself
+        appropriately at the beginning of each pass.  End-of-pass processing,
+	where necessary, is also invoked from the master control module.
+
+   Method selection is partially distributed, in that a particular processing
+   module may contain several possible implementations of a particular method,
+   which it will select among when given its initialization call.  The master
+   control code need only be concerned with decisions that affect more than
+   one module.
+ 
+2. Data buffering control.  A separate control module exists for each
+   inter-processing-step data buffer.  This module is responsible for
+   invoking the processing steps that write or read that data buffer.
+
+Each buffer controller sees the world as follows:
+
+input data => processing step A => buffer => processing step B => output data
+                      |              |               |
+              ------------------ controller ------------------
+
+The controller knows the dataflow requirements of steps A and B: how much data
+they want to accept in one chunk and how much they output in one chunk.  Its
+function is to manage its buffer and call A and B at the proper times.
+
+A data buffer control module may itself be viewed as a processing step by a
+higher-level control module; thus the control modules form a binary tree with
+elementary processing steps at the leaves of the tree.
+
+The control modules are objects.  A considerable amount of flexibility can
+be had by replacing implementations of a control module.  For example:
+* Merging of adjacent steps in the pipeline is done by replacing a control
+  module and its pair of processing-step modules with a single processing-
+  step module.  (Hence the possible merges are determined by the tree of
+  control modules.)
+* In some processing modes, a given interstep buffer need only be a "strip"
+  buffer large enough to accommodate the desired data chunk sizes.  In other
+  modes, a full-image buffer is needed and several passes are required.
+  The control module determines which kind of buffer is used and manipulates
+  virtual array buffers as needed.  One or both processing steps may be
+  unaware of the multi-pass behavior.
+
+In theory, we might be able to make all of the data buffer controllers
+interchangeable and provide just one set of implementations for all.  In
+practice, each one contains considerable special-case processing for its
+particular job.  The buffer controller concept should be regarded as an
+overall system structuring principle, not as a complete description of the
+task performed by any one controller.
+
+
+*** Compression object structure ***
+
+Here is a sketch of the logical structure of the JPEG compression library:
+
+                                                 |-- Colorspace conversion
+                  |-- Preprocessing controller --|
+                  |                              |-- Downsampling
+Main controller --|
+                  |                            |-- Forward DCT, quantize
+                  |-- Coefficient controller --|
+                                               |-- Entropy encoding
+
+This sketch also describes the flow of control (subroutine calls) during
+typical image data processing.  Each of the components shown in the diagram is
+an "object" which may have several different implementations available.  One
+or more source code files contain the actual implementation(s) of each object.
+
+The objects shown above are:
+
+* Main controller: buffer controller for the subsampled-data buffer, which
+  holds the preprocessed input data.  This controller invokes preprocessing to
+  fill the subsampled-data buffer, and JPEG compression to empty it.  There is
+  usually no need for a full-image buffer here; a strip buffer is adequate.
+
+* Preprocessing controller: buffer controller for the downsampling input data
+  buffer, which lies between colorspace conversion and downsampling.  Note
+  that a unified conversion/downsampling module would probably replace this
+  controller entirely.
+
+* Colorspace conversion: converts application image data into the desired
+  JPEG color space; also changes the data from pixel-interleaved layout to
+  separate component planes.  Processes one pixel row at a time.
+
+* Downsampling: performs reduction of chroma components as required.
+  Optionally may perform pixel-level smoothing as well.  Processes a "row
+  group" at a time, where a row group is defined as Vmax pixel rows of each
+  component before downsampling, and Vk sample rows afterwards (remember Vk
+  differs across components).  Some downsampling or smoothing algorithms may
+  require context rows above and below the current row group; the
+  preprocessing controller is responsible for supplying these rows via proper
+  buffering.  The downsampler is responsible for edge expansion at the right
+  edge (i.e., extending each sample row to a multiple of 8 samples); but the
+  preprocessing controller is responsible for vertical edge expansion (i.e.,
+  duplicating the bottom sample row as needed to make a multiple of 8 rows).
+
+* Coefficient controller: buffer controller for the DCT-coefficient data.
+  This controller handles MCU assembly, including insertion of dummy DCT
+  blocks when needed at the right or bottom edge.  When performing
+  Huffman-code optimization or emitting a multiscan JPEG file, this
+  controller is responsible for buffering the full image.
+
+* Forward DCT and quantization: Perform DCT, quantize, and emit coefficients
+  in zigzag block order.  Works on one or more DCT blocks at a time.
+
+* Entropy encoding: Perform Huffman or arithmetic entropy coding and emit the
+  coded data to the data destination module.  Works on one MCU per call.
+
+In addition to the above objects, the compression library includes these
+objects:
+
+* Master control: determines the number of passes required, controls overall
+  and per-pass initialization of the other modules.
+
+* Marker writing: generates JPEG markers (except for RSTn, which is emitted
+  by the entropy encoder when needed).
+
+* Data destination manager: writes the output JPEG datastream to its final
+  destination (e.g., a file).  The destination manager supplied with the
+  library knows how to write to a stdio stream; for other behaviors, the
+  surrounding application may provide its own destination manager.
+
+* Memory manager: allocates and releases memory, controls virtual arrays
+  (with backing store management, where required).
+
+* Error handler: performs formatting and output of error and trace messages;
+  determines handling of nonfatal errors.  The surrounding application may
+  override some or all of this object's methods to change error handling.
+
+* Progress monitor: supports output of "percent-done" progress reports.
+  This object represents an optional callback to the surrounding application:
+  if wanted, it must be supplied by the application.
+
+The error handler, destination manager, and progress monitor objects are
+defined as separate objects in order to simplify application-specific
+customization of the JPEG library.  A surrounding application may override
+individual methods or supply its own all-new implementation of one of these
+objects.  The object interfaces for these objects are therefore treated as
+part of the application interface of the library, whereas the other objects
+are internal to the library.
+
+The error handler and memory manager are shared by JPEG compression and
+decompression; the progress monitor, if used, may be shared as well.
+
+
+*** Decompression object structure ***
+
+Here is a sketch of the logical structure of the JPEG decompression library:
+
+                                               |-- Entropy decoding
+                  |-- Coefficient controller --|
+                  |                            |-- Dequantize, Inverse DCT
+Main controller --|
+                  |                               |-- Upsampling
+                  |-- Postprocessing controller --|   |-- Colorspace conversion
+                                                  |-- Color quantization
+                                                  |-- Color precision reduction
+
+As before, this diagram also represents typical control flow.  The objects
+shown are:
+
+* Main controller: buffer controller for the subsampled-data buffer, which
+  holds the output of JPEG decompression proper.  This controller's primary
+  task is to feed the postprocessing procedure.  Some upsampling algorithms
+  may require context rows above and below the current row group; when this
+  is true, the main controller is responsible for managing its buffer so as
+  to make context rows available.  In the current design, the main buffer is
+  always a strip buffer; a full-image buffer is never required.
+
+* Coefficient controller: buffer controller for the DCT-coefficient data.
+  This controller handles MCU disassembly, including deletion of any dummy
+  DCT blocks at the right or bottom edge.  When reading a multiscan JPEG
+  file, this controller is responsible for buffering the full image.
+  (Buffering DCT coefficients, rather than samples, is necessary to support
+  progressive JPEG.)  The equivalent of one fully interleaved MCU row of
+  subsampled data is processed per call, even when the source JPEG file is
+  noninterleaved.
+
+* Entropy decoding: Read coded data from the data source module and perform
+  Huffman or arithmetic entropy decoding.  Works on one MCU per call.
+
+* Dequantization and inverse DCT: like it says.  Note that the coefficients
+  buffered by the coefficient controller have NOT been dequantized; we
+  merge dequantization and inverse DCT into a single step for speed reasons.
+  When scaled-down output is asked for, simplified DCT algorithms may be used
+  that emit only 1x1, 2x2, or 4x4 samples per DCT block, not the full 8x8.
+  Works on one DCT block at a time.
+
+* Postprocessing controller: buffer controller for the color quantization
+  input buffer, when quantization is in use.  (Without quantization, this
+  controller just calls the upsampler.)  For two-pass quantization, this
+  controller is responsible for buffering the full-image data.
+
+* Upsampling: restores chroma components to full size.  (May support more
+  general output rescaling, too.  Note that if undersized DCT outputs have
+  been emitted by the DCT module, this module must adjust so that properly
+  sized outputs are created.)  Works on one row group at a time.  This module
+  also calls the color conversion module, so its top level is effectively a
+  buffer controller for the upsampling->color conversion buffer.  However, in
+  all but the highest-quality operating modes, upsampling and color
+  conversion are likely to be merged into a single step.
+
+* Colorspace conversion: convert from JPEG color space to output color space,
+  and change data layout from separate component planes to pixel-interleaved.
+  Works on one pixel row at a time.
+
+* Color quantization: reduce the data to colormapped form, using either an
+  externally specified colormap or an internally generated one.  This module
+  is not used for full-color output.  Works on one pixel row at a time; may
+  require two passes to generate a color map.  Note that the output will
+  always be a single component representing colormap indexes.  In the current
+  design, the output values are JSAMPLEs, so an 8-bit compilation cannot
+  quantize to more than 256 colors.  This is unlikely to be a problem in
+  practice.
+
+* Color reduction: this module handles color precision reduction, e.g.,
+  generating 15-bit color (5 bits/primary) from JPEG's 24-bit output.
+  Not quite clear yet how this should be handled... should we merge it with
+  colorspace conversion???
+
+Note that some high-speed operating modes might condense the entire
+postprocessing sequence to a single module (upsample, color convert, and
+quantize in one step).
+
+In addition to the above objects, the decompression library includes these
+objects:
+
+* Master control: determines the number of passes required, controls overall
+  and per-pass initialization of the other modules.
+
+* Marker reading: decodes JPEG markers (except for RSTn).
+
+* Data source manager: supplies the input JPEG datastream.  The source
+  manager supplied with the library knows how to read from a stdio stream;
+  for other behaviors, the surrounding application may provide its own source
+  manager.
+
+* Memory manager: same as for compression library.
+
+* Error handler: same as for compression library.
+
+* Progress monitor: same as for compression library.
+
+As with compression, the data source manager, error handler, and progress
+monitor are candidates for replacement by a surrounding application.
+
+
+*** Data formats ***
+
+Arrays of pixel sample values use the following data structure:
+
+    typedef something JSAMPLE;		a pixel component value, 0..MAXJSAMPLE
+    typedef JSAMPLE *JSAMPROW;		ptr to a row of samples
+    typedef JSAMPROW *JSAMPARRAY;	ptr to a list of rows
+    typedef JSAMPARRAY *JSAMPIMAGE;	ptr to a list of color-component arrays
+
+The basic element type JSAMPLE will typically be one of unsigned char,
+(signed) char, or short.  Short will be used if samples wider than 8 bits are
+to be supported (this is a compile-time option).  Otherwise, unsigned char is
+used if possible.  If the compiler only supports signed chars, then it is
+necessary to mask off the value when reading.  Thus, all reads of JSAMPLE
+values must be coded as "GETJSAMPLE(value)", where the macro will be defined
+as "((value) & 0xFF)" on signed-char machines and "((int) (value))" elsewhere.
+
+With these conventions, JSAMPLE values can be assumed to be >= 0.  This helps
+simplify correct rounding during downsampling, etc.  The JPEG standard's
+specification that sample values run from -128..127 is accommodated by
+subtracting 128 just as the sample value is copied into the source array for
+the DCT step (this will be an array of signed ints).  Similarly, during
+decompression the output of the IDCT step will be immediately shifted back to
+0..255.  (NB: different values are required when 12-bit samples are in use.
+The code is written in terms of MAXJSAMPLE and CENTERJSAMPLE, which will be
+defined as 255 and 128 respectively in an 8-bit implementation, and as 4095
+and 2048 in a 12-bit implementation.)
+
+We use a pointer per row, rather than a two-dimensional JSAMPLE array.  This
+choice costs only a small amount of memory and has several benefits:
+* Code using the data structure doesn't need to know the allocated width of
+  the rows.  This simplifies edge expansion/compression, since we can work
+  in an array that's wider than the logical picture width.
+* Indexing doesn't require multiplication; this is a performance win on many
+  machines.
+* Arrays with more than 64K total elements can be supported even on machines
+  where malloc() cannot allocate chunks larger than 64K.
+* The rows forming a component array may be allocated at different times
+  without extra copying.  This trick allows some speedups in smoothing steps
+  that need access to the previous and next rows.
+
+Note that each color component is stored in a separate array; we don't use the
+traditional layout in which the components of a pixel are stored together.
+This simplifies coding of modules that work on each component independently,
+because they don't need to know how many components there are.  Furthermore,
+we can read or write each component to a temporary file independently, which
+is helpful when dealing with noninterleaved JPEG files.
+
+In general, a specific sample value is accessed by code such as
+	GETJSAMPLE(image[colorcomponent][row][col])
+where col is measured from the image left edge, but row is measured from the
+first sample row currently in memory.  Either of the first two indexings can
+be precomputed by copying the relevant pointer.
+
+
+Since most image-processing applications prefer to work on images in which
+the components of a pixel are stored together, the data passed to or from the
+surrounding application uses the traditional convention: a single pixel is
+represented by N consecutive JSAMPLE values, and an image row is an array of
+(# of color components)*(image width) JSAMPLEs.  One or more rows of data can
+be represented by a pointer of type JSAMPARRAY in this scheme.  This scheme is
+converted to component-wise storage inside the JPEG library.  (Applications
+that want to skip JPEG preprocessing or postprocessing will have to contend
+with component-wise storage.)
+
+
+Arrays of DCT-coefficient values use the following data structure:
+
+    typedef short JCOEF;		a 16-bit signed integer
+    typedef JCOEF JBLOCK[DCTSIZE2];	an 8x8 block of coefficients
+    typedef JBLOCK *JBLOCKROW;		ptr to one horizontal row of 8x8 blocks
+    typedef JBLOCKROW *JBLOCKARRAY;	ptr to a list of such rows
+    typedef JBLOCKARRAY *JBLOCKIMAGE;	ptr to a list of color component arrays
+
+The underlying type is at least a 16-bit signed integer; while "short" is big
+enough on all machines of interest, on some machines it is preferable to use
+"int" for speed reasons, despite the storage cost.  Coefficients are grouped
+into 8x8 blocks (but we always use #defines DCTSIZE and DCTSIZE2 rather than
+"8" and "64").  The contents of a block may be either in "natural" or
+zigzagged order, and may be true values or divided by the quantization
+coefficients, depending on where the block is in the processing pipeline.
+
+Notice that the allocation unit is now a row of 8x8 blocks, corresponding to
+eight rows of samples.  Otherwise the structure is much the same as for
+samples, and for the same reasons.
+
+On machines where malloc() can't handle a request bigger than 64Kb, this data
+structure limits us to rows of less than 512 JBLOCKs, or a picture width of
+4000+ pixels.  This seems an acceptable restriction.
+
+
+On 80x86 machines, the bottom-level pointer types (JSAMPROW and JBLOCKROW)
+must be declared as "far" pointers, but the upper levels can be "near"
+(implying that the pointer lists are allocated in the DS segment).
+We use a #define symbol FAR, which expands to the "far" keyword when
+compiling on 80x86 machines and to nothing elsewhere.
+
+
+*** Suspendable processing ***
+
+In some applications it is desirable to use the JPEG library as an
+incremental, memory-to-memory filter.  In this situation the data source or
+destination may be a limited-size buffer, and we can't rely on being able to
+empty or refill the buffer at arbitrary times.  Instead the application would
+like to have control return from the library at buffer overflow/underrun, and
+then resume compression or decompression at a later time.
+
+This scenario is supported for simple cases, namely, single-pass processing
+of single-scan JPEG files.  (For anything more complex, we recommend that the
+application "bite the bullet" and develop real multitasking capability.)  The
+libjpeg.doc file goes into more detail about the usage and limitations of
+this capability; here we address the implications for library structure.
+
+The essence of the problem is that the entropy codec (coder or decoder) must
+be prepared to stop at arbitrary times.  In turn, the controllers that call
+the entropy codec must be able to stop before having produced or consumed all
+the data that they normally would handle in one call.  That part is reasonably
+straightforward: we make the controller call interfaces include "progress
+counters" which indicate the number of data chunks successfully processed, and
+we require callers to test the counter rather than just assume all of the data
+was processed.
+
+Rather than trying to restart at an arbitrary point, the current Huffman
+codecs are designed to restart at the beginning of the current MCU after a
+suspension due to buffer overflow/underrun.  At the start of each call, the
+codec's internal state is loaded from permanent storage (in the JPEG object
+structures) into local variables.  On successful completion of the MCU, the
+permanent state is updated.  (This copying is not very expensive, and may even
+lead to *improved* performance if the local variables can be registerized.)
+If a suspension occurs, the codec simply returns without updating the state,
+thus effectively reverting to the start of the MCU.  Note that this implies
+leaving some data unprocessed in the source/destination buffer (ie, the
+compressed partial MCU).  The data source/destination module interfaces are
+specified so as to make this possible.  This also implies that the data buffer
+must be large enough to hold a worst-case compressed MCU; a couple thousand
+bytes should be enough.
+
+This design would probably not work for an arithmetic codec, since its
+modifiable state is quite large and couldn't be copied cheaply.  Instead it
+would have to suspend and resume exactly at the point of the buffer end.
+Also, a progressive JPEG decoder would have some problems with having already
+updated the output DCT coefficient buffer, since progressive decoding depends
+on the prior state of the coefficient buffer.  This case might also have to be
+handled by exact restart.  Currently I expect that IJG will just not support
+suspendable operation in these cases (when and if we implement them at all).
+
+The JPEG marker reader is designed to cope with suspension at an arbitrary
+point.  It does so by backing up to the start of the marker parameter segment,
+so the data buffer must be big enough to hold the largest marker of interest.
+Again, a couple KB should be adequate.  (A special "skip" convention is used
+to bypass COM and APPn markers, so these can be larger than the buffer size
+without causing problems; otherwise a 64K buffer would be needed in the worst
+case.)
+
+The JPEG marker writer currently does *not* cope with suspension.  I feel that
+this is not necessary; it is much easier simply to require the application to
+ensure there is enough buffer space before starting.  (An empty 2K buffer is
+more than sufficient for the header markers; and ensuring there are a dozen or
+two bytes available before calling jpeg_finish_compress() will suffice for the
+trailer.)  Again, this would not work for writing multi-scan JPEG files, but
+we simply do not intend to support that capability with suspension.
+
+
+*** Memory manager services ***
+
+The JPEG library's memory manager controls allocation and deallocation of
+memory, and it manages large "virtual" data arrays on machines where the
+operating system does not provide virtual memory.  Note that the same
+memory manager serves both compression and decompression operations.
+
+In all cases, allocated objects are tied to a particular compression or
+decompression master record, and they will be released when that master
+record is destroyed.
+
+The memory manager does not provide explicit deallocation of objects.
+Instead, objects are created in "pools" of free storage, and a whole pool
+can be freed at once.  This approach helps prevent storage-leak bugs, and
+it speeds up operations whenever malloc/free are slow (as they often are).
+The pools can be regarded as lifetime identifiers for objects.  Two
+pools/lifetimes are defined:
+  * JPOOL_PERMANENT	lasts until master record is destroyed
+  * JPOOL_IMAGE		lasts until done with image (JPEG datastream)
+Permanent lifetime is used for parameters and tables that should be carried
+across from one datastream to another; this includes all application-visible
+parameters.  Image lifetime is used for everything else.  (A third lifetime,
+JPOOL_PASS = one processing pass, was originally planned.  However it was
+dropped as not being worthwhile.  The actual usage patterns are such that the
+peak memory usage would be about the same anyway; and having per-pass storage
+substantially complicates the virtual memory allocation rules --- see below.)
+
+The memory manager deals with three kinds of object:
+1. "Small" objects.  Typically these require no more than 10K-20K total.
+2. "Large" objects.  These may require tens to hundreds of K depending on
+   image size.  Semantically they behave the same as small objects, but we
+   distinguish them for two reasons:
+     * On MS-DOS machines, large objects are referenced by FAR pointers,
+       small objects by NEAR pointers.
+     * Pool allocation heuristics may differ for large and small objects.
+   Note that individual "large" objects cannot exceed the size allowed by
+   type size_t, which may be 64K or less on some machines.
+3. "Virtual" objects.  These are large 2-D arrays of JSAMPLEs or JBLOCKs
+   (typically large enough for the entire image being processed).  The
+   memory manager provides stripwise access to these arrays.  On machines
+   without virtual memory, the rest of the array may be swapped out to a
+   temporary file.
+
+(Note: JSAMPARRAY and JBLOCKARRAY data structures are a combination of large
+objects for the data proper and small objects for the row pointers.  For
+convenience and speed, the memory manager provides single routines to create
+these structures.  Similarly, virtual arrays include a small control block
+and a JSAMPARRAY or JBLOCKARRAY working buffer, all created with one call.)
+
+In the present implementation, virtual arrays are only permitted to have image
+lifespan.  (Permanent lifespan would not be reasonable, and pass lifespan is
+not very useful since a virtual array's raison d'etre is to store data for
+multiple passes through the image.)  We also expect that only "small" objects
+will be given permanent lifespan, though this restriction is not required by
+the memory manager.
+
+In a non-virtual-memory machine, some performance benefit can be gained by
+making the in-memory buffers for virtual arrays be as large as possible.
+(For small images, the buffers might fit entirely in memory, so blind
+swapping would be very wasteful.)  The memory manager will adjust the height
+of the buffers to fit within a prespecified maximum memory usage.  In order
+to do this in a reasonably optimal fashion, the manager needs to allocate all
+of the virtual arrays at once.  Therefore, there isn't a one-step allocation
+routine for virtual arrays; instead, there is a "request" routine that simply
+allocates the control block, and a "realize" routine (called just once) that
+determines space allocation and creates all of the actual buffers.  The
+realize routine must allow for space occupied by non-virtual large objects.
+(We don't bother to factor in the space needed for small objects, on the
+grounds that it isn't worth the trouble.)
+
+To support all this, we establish the following protocol for doing business
+with the memory manager:
+  1. Modules must request virtual arrays (which may have only image lifespan)
+     during the global selection phase, i.e., in their jinit_xxx routines.
+  2. All "large" objects (including JSAMPARRAYs and JBLOCKARRAYs) must also be
+     allocated at global selection time.
+  3. realize_virt_arrays will be called at the completion of global selection.
+     The above conventions ensure that sufficient information is available
+     for it to choose a good size for virtual array buffers.
+Small objects of any lifespan may be allocated at any time.  We expect that
+the total space used for small objects will be small enough to be negligible
+in the realize_virt_arrays computation.
+
+In a virtual-memory machine, we simply pretend that the available space is
+infinite, thus causing realize_virt_arrays to decide that it can allocate all
+the virtual arrays as full-size in-memory buffers.  The overhead of the
+virtual-array access protocol is very small when no swapping occurs.
+
+
+*** Memory manager internal structure ***
+
+To isolate system dependencies as much as possible, we have broken the
+memory manager into two parts.  There is a reasonably system-independent
+"front end" (jmemmgr.c) and a "back end" that contains only the code
+likely to change across systems.  All of the memory management methods
+outlined above are implemented by the front end.  The back end provides
+the following routines for use by the front end (none of these routines
+are known to the rest of the JPEG code):
+
+jpeg_mem_init, jpeg_mem_term	system-dependent initialization/shutdown
+
+jpeg_get_small, jpeg_free_small	interface to malloc and free library routines
+				(or their equivalents)
+
+jpeg_get_large, jpeg_free_large	interface to FAR malloc/free in MSDOS machines;
+				else usually the same as
+				jpeg_get_small/jpeg_free_small
+
+jpeg_mem_available		estimate available memory
+
+jpeg_open_backing_store		create a backing-store object
+
+read_backing_store,		manipulate a backing-store object
+write_backing_store,
+close_backing_store
+
+On some systems there will be more than one type of backing-store object
+(specifically, in MS-DOS a backing store file might be an area of extended
+memory as well as a disk file).  jpeg_open_backing_store is responsible for
+choosing how to implement a given object.  The read/write/close routines
+are method pointers in the structure that describes a given object; this
+lets them be different for different object types.
+
+It may be necessary to ensure that backing store objects are explicitly
+released upon abnormal program termination.  For example, MS-DOS won't free
+extended memory by itself.  To support this, we will expect the main program
+or surrounding application to arrange to call self_destruct (typically via
+jpeg_destroy) upon abnormal termination.  This may require a SIGINT signal
+handler or equivalent.  We don't want to have the back end module install its
+own signal handler, because that would pre-empt the surrounding application's
+ability to control signal handling.
+
+The IJG distribution includes several memory manager back end implementations.
+Usually the same back end should be suitable for all applications on a given
+system, but it is possible for an application to supply its own back end at
+need.
+
+
+*** Implications of DNL marker ***
+
+Some JPEG files may use a DNL marker to postpone definition of the image
+height (this would be useful for a fax-like scanner's output, for instance).
+In these files the SOF marker claims the image height is 0, and you only
+find out the true image height at the end of the first scan.
+
+We could read these files as follows:
+1. Upon seeing zero image height, replace it by 65535 (the maximum allowed).
+2. When the DNL is found, update the image height in the global image
+   descriptor.
+This implies that control modules must avoid making copies of the image
+height, and must re-test for termination after each MCU row.  This would
+be easy enough to do.
+
+In cases where image-size data structures are allocated, this approach will
+result in very inefficient use of virtual memory or much-larger-than-necessary
+temporary files.  This seems acceptable for something that probably won't be a
+mainstream usage.  People might have to forgo use of memory-hogging options
+(such as two-pass color quantization or noninterleaved JPEG files) if they
+want efficient conversion of such files.  (One could improve efficiency by
+demanding a user-supplied upper bound for the height, less than 65536; in most
+cases it could be much less.)
+
+The standard also permits the SOF marker to overestimate the image height,
+with a DNL to give the true, smaller height at the end of the first scan.
+This would solve the space problems if the overestimate wasn't too great.
+However, it implies that you don't even know whether DNL will be used.
+
+This leads to a couple of very serious objections:
+1. Testing for a DNL marker must occur in the inner loop of the decompressor's
+   Huffman decoder; this implies a speed penalty whether the feature is used
+   or not.
+2. There is no way to hide the last-minute change in image height from an
+   application using the decoder.  Thus *every* application using the IJG
+   library would suffer a complexity penalty whether it cared about DNL or
+   not.
+We currently do not support DNL because of these problems.
+
+A different approach is to insist that DNL-using files be preprocessed by a
+separate program that reads ahead to the DNL, then goes back and fixes the SOF
+marker.  This is a much simpler solution and is probably far more efficient.
+Even if one wants piped input, buffering the first scan of the JPEG file needs
+a lot smaller temp file than is implied by the maximum-height method.  For
+this approach we'd simply treat DNL as a no-op in the decompressor (at most,
+check that it matches the SOF image height).
+
+We will not worry about making the compressor capable of outputting DNL.
+Something similar to the first scheme above could be applied if anyone ever
+wants to make that work.
diff --git a/testimg.gif b/testimg.gif
index 1744b40..8c14b8d 100644
--- a/testimg.gif
+++ b/testimg.gif
Binary files differ
diff --git a/testimg.jpg b/testimg.jpg
index e88cb2e..acf32dc 100644
--- a/testimg.jpg
+++ b/testimg.jpg
Binary files differ
diff --git a/testimg.ppm b/testimg.ppm
index 1a936c4..5464834 100644
--- a/testimg.ppm
+++ b/testimg.ppm
Binary files differ
diff --git a/testorig.jpg b/testorig.jpg
index db9a62f..58d908b 100644
--- a/testorig.jpg
+++ b/testorig.jpg
Binary files differ
diff --git a/usage.doc b/usage.doc
new file mode 100644
index 0000000..3fc061f
--- /dev/null
+++ b/usage.doc
@@ -0,0 +1,449 @@
+USAGE instructions for the Independent JPEG Group's JPEG software
+=================================================================
+
+This file describes usage of the JPEG conversion programs cjpeg and djpeg,
+as well as the utility programs rdjpgcom and wrjpgcom.  (See the other
+documentation files if you wish to use the JPEG library within your own
+programs.)
+
+If you are on a Unix machine you may prefer to read the Unix-style manual
+pages in files cjpeg.1, djpeg.1, rdjpgcom.1, wrjpgcom.1.
+
+
+INTRODUCTION
+
+These programs implement JPEG image compression and decompression.  JPEG
+(pronounced "jay-peg") is a standardized compression method for full-color
+and gray-scale images.  JPEG is designed to handle "real-world" scenes,
+for example scanned photographs.  Cartoons, line drawings, and other
+non-realistic images are not JPEG's strong suit; on that sort of material
+you may get poor image quality and/or little compression.
+
+JPEG is lossy, meaning that the output image is not necessarily identical to
+the input image.  Hence you should not use JPEG if you have to have identical
+output bits.  However, on typical real-world images, very good compression
+levels can be obtained with no visible change, and amazingly high compression
+is possible if you can tolerate a low-quality image.  You can trade off image
+quality against file size by adjusting the compressor's "quality" setting.
+
+
+GENERAL USAGE
+
+We provide two programs, cjpeg to compress an image file into JPEG format,
+and djpeg to decompress a JPEG file back into a conventional image format.
+
+On Unix-like systems, you say:
+	cjpeg [switches] [imagefile] >jpegfile
+or
+	djpeg [switches] [jpegfile]  >imagefile
+The programs read the specified input file, or standard input if none is
+named.  They always write to standard output (with trace/error messages to
+standard error).  These conventions are handy for piping images between
+programs.
+
+On most non-Unix systems, you say:
+	cjpeg [switches] imagefile jpegfile
+or
+	djpeg [switches] jpegfile  imagefile
+i.e., both the input and output files are named on the command line.  This
+style is a little more foolproof, and it loses no functionality if you don't
+have pipes.  (You can get this style on Unix too, if you prefer, by defining
+TWO_FILE_COMMANDLINE when you compile the programs; see install.doc.)
+
+You can also say:
+	cjpeg [switches] -outfile jpegfile  imagefile
+or
+	djpeg [switches] -outfile imagefile  jpegfile
+This syntax works on all systems, so it is useful for scripts.
+
+The currently supported image file formats are: PPM (PBMPLUS color format),
+PGM (PBMPLUS gray-scale format), BMP, GIF, Targa, and RLE (Utah Raster Toolkit
+format).  (RLE is supported only if the URT library is available.)
+cjpeg recognizes the input image format automatically, with the exception
+of some Targa-format files.  You have to tell djpeg which format to generate.
+
+JPEG files are in the defacto standard JFIF file format.  There are other,
+less widely used JPEG-based file formats, but we don't support them.
+
+All switch names may be abbreviated; for example, -grayscale may be written
+-gray or -gr.  Most of the "basic" switches can be abbreviated to as little as
+one letter.  Upper and lower case are equivalent (-GIF is the same as -gif).
+British spellings are also accepted (e.g., -greyscale), though for brevity
+these are not mentioned below.
+
+
+CJPEG DETAILS
+
+The basic command line switches for cjpeg are:
+
+	-quality N	Scale quantization tables to adjust image quality.
+			Quality is 0 (worst) to 100 (best); default is 75.
+			(See below for more info.)
+
+	-grayscale	Create monochrome JPEG file from color input.
+			Be sure to use this switch when compressing a grayscale
+			GIF file, because cjpeg isn't bright enough to notice
+			whether a GIF file uses only shades of gray.  By
+			saying -grayscale, you'll get a smaller JPEG file that
+			takes less time to process.
+
+	-optimize	Perform optimization of entropy encoding parameters.
+			Without this, default encoding parameters are used.
+			-optimize usually makes the JPEG file a little smaller,
+			but cjpeg runs somewhat slower and needs much more
+			memory.  Image quality and speed of decompression are
+			unaffected by -optimize.
+
+	-targa		Input file is Targa format.  Targa files that contain
+			an "identification" field will not be automatically
+			recognized by cjpeg; for such files you must specify
+			-targa to make cjpeg treat the input as Targa format.
+			For most Targa files, you won't need this switch.
+
+The -quality switch lets you trade off compressed file size against quality of
+the reconstructed image: the higher the quality setting, the larger the JPEG
+file, and the closer the output image will be to the original input.  Normally
+you want to use the lowest quality setting (smallest file) that decompresses
+into something visually indistinguishable from the original image.  For this
+purpose the quality setting should be between 50 and 95; the default of 75 is
+often about right.  If you see defects at -quality 75, then go up 5 or 10
+counts at a time until you are happy with the output image.  (The optimal
+setting will vary from one image to another.)
+
+-quality 100 will generate a quantization table of all 1's, eliminating loss
+in the quantization step (but there is still information loss in subsampling,
+as well as roundoff error).  This setting is mainly of interest for
+experimental purposes.  Quality values above about 95 are NOT recommended for
+normal use; the compressed file size goes up dramatically for hardly any gain
+in output image quality.
+
+In the other direction, quality values below 50 will produce very small files
+of low image quality.  Settings around 5 to 10 might be useful in preparing an
+index of a large image library, for example.  Try -quality 2 (or so) for some
+amusing Cubist effects.  (Note: quality values below about 25 generate 2-byte
+quantization tables, which are considered optional in the JPEG standard.
+cjpeg emits a warning message when you give such a quality value, because
+some commercial JPEG programs may be unable to decode the resulting file.
+Use -baseline if you need to ensure compatibility at low quality values.)
+
+Switches for advanced users:
+
+	-dct int	Use integer DCT method (default).
+	-dct fast	Use fast integer DCT (less accurate).
+	-dct float	Use floating-point DCT method.
+			The floating-point method is the most accurate, but
+			will be the slowest unless your machine has very fast
+			floating-point hardware.  Also note that results of
+			the floating-point method may vary slightly across
+			machines, while the integer methods should give the
+			same results everywhere.  The fast integer method is
+			much less accurate than the other two.
+
+	-restart N	Emit a JPEG restart marker every N MCU rows, or every
+			N MCU blocks if "B" is attached to the number.
+			-restart 0 (the default) means no restart markers.
+
+	-smooth N	Smooth the input image to eliminate dithering noise.
+			N, ranging from 1 to 100, indicates the strength of
+			smoothing.  0 (the default) means no smoothing.
+
+	-maxmemory N	Set limit for amount of memory to use in processing
+			large images.  Value is in thousands of bytes, or
+			millions of bytes if "M" is attached to the number.
+			For example, -max 4m selects 4000000 bytes.  If more
+			space is needed, temporary files will be used.
+
+	-verbose	Enable debug printout.  More -v's give more printout.
+	or  -debug	Also, version information is printed at startup.
+
+The -restart option inserts extra markers that allow a JPEG decoder to
+resynchronize after a transmission error.  Without restart markers, any damage
+to a compressed file will usually ruin the image from the point of the error
+to the end of the image; with restart markers, the damage is usually confined
+to the portion of the image up to the next restart marker.  Of course, the
+restart markers occupy extra space.  We recommend -restart 1 for images that
+will be transmitted across unreliable networks such as Usenet.
+
+The -smooth option filters the input to eliminate fine-scale noise.  This is
+often useful when converting GIF files to JPEG: a moderate smoothing factor of
+10 to 50 gets rid of dithering patterns in the input file, resulting in a
+smaller JPEG file and a better-looking image.  Too large a smoothing factor
+will visibly blur the image, however.
+
+Switches for wizards:
+
+	-arithmetic	Use arithmetic coding rather than Huffman coding.
+			(Not currently supported for legal reasons.)
+
+	-baseline	Force a baseline JPEG file to be generated.  This
+			clamps quantization values to 8 bits even at low
+			quality settings.
+
+	-nointerleave	Generate noninterleaved JPEG file (not yet supported).
+
+	-qtables file	Use the quantization tables given in the specified
+			file.  The file should contain one to four tables
+			(64 values each) as plain text.  Comments preceded by
+			'#' may be included in the file.  The tables are
+			implicitly numbered 0,1,etc.  If -quality N is also
+			specified, the values in the file are scaled according
+			to cjpeg's quality scaling curve.
+
+	-qslots N[,...] Select which quantization table to use for each color
+			component.  By default, table 0 is used for luminance
+			and table 1 for chrominance components.
+
+	-sample HxV[,...]	Set JPEG sampling factors.  If you specify
+			fewer H/V pairs than there are components, the
+			remaining components are set to 1x1 sampling.  The
+			default setting is equivalent to "-sample 2x2".
+
+The "wizard" switches are intended for experimentation with JPEG.  If you
+don't know what you are doing, DON'T USE THEM.  You can easily produce files
+with worse image quality and/or poorer compression than you'll get from the
+default settings.  Furthermore, these switches should not be used when making
+files intended for general use, because not all JPEG implementations will
+support unusual JPEG parameter settings.
+
+
+DJPEG DETAILS
+
+The basic command line switches for djpeg are:
+
+	-colors N	Reduce image to at most N colors.  This reduces the
+	or -quantize N	number of colors used in the output image, so that it
+			can be displayed on a colormapped display or stored in
+			a colormapped file format.  For example, if you have
+			an 8-bit display, you'd need to reduce to 256 or fewer
+			colors.  (-colors is the recommended name, -quantize
+			is provided only for backwards compatibility.)
+
+	-fast		Select recommended processing options for fast, low
+			quality output.  (The default options are chosen for
+			highest quality output.)  Currently, this is equivalent
+			to "-dct fast -nosmooth -onepass -dither ordered".
+
+	-grayscale	Force gray-scale output even if JPEG file is color.
+			Useful for viewing on monochrome displays; also,
+			djpeg runs noticeably faster in this mode.
+
+	-scale M/N	Scale the output image by a factor M/N.  Currently
+			the scale factor must be 1/1, 1/2, 1/4, or 1/8.
+			Scaling is handy if the image is larger than your
+			screen; also, djpeg runs much faster when scaling
+			down the output.
+
+	-bmp		Select BMP output format (Windows flavor).  8-bit
+			colormapped format is emitted if -colors or -grayscale
+			is specified, or if the JPEG file is gray-scale;
+			otherwise, 24-bit full-color format is emitted.
+
+	-gif		Select GIF output format.  Since GIF does not support
+			more than 256 colors, -colors 256 is assumed (unless
+			you specify a smaller number of colors).  If you
+			specify -fast, the default number of colors is 216.
+
+	-os2		Select BMP output format (OS/2 1.x flavor).  8-bit
+			colormapped format is emitted if -colors or -grayscale
+			is specified, or if the JPEG file is gray-scale;
+			otherwise, 24-bit full-color format is emitted.
+
+	-pnm		Select PBMPLUS (PPM/PGM) output format (this is the
+			default format).  PGM is emitted if the JPEG file is
+			gray-scale or if -grayscale is specified; otherwise
+			PPM is emitted.
+
+	-rle		Select RLE output format.  (Requires URT library.)
+
+	-targa		Select Targa output format.  Gray-scale format is
+			emitted if the JPEG file is gray-scale or if
+			-grayscale is specified; otherwise, colormapped format
+			is emitted if -colors is specified; otherwise, 24-bit
+			full-color format is emitted.
+
+Switches for advanced users:
+
+	-dct int	Use integer DCT method (default).
+	-dct fast	Use fast integer DCT (less accurate).
+	-dct float	Use floating-point DCT method.
+			The floating-point method is the most accurate, but
+			will be the slowest unless your machine has very fast
+			floating-point hardware.  Also note that results of
+			the floating-point method may vary slightly across
+			machines, while the integer methods should give the
+			same results everywhere.  The fast integer method is
+			much less accurate than the other two.
+
+	-dither fs	Use Floyd-Steinberg dithering in color quantization.
+	-dither ordered	Use ordered dithering in color quantization.
+	-dither none	Do not use dithering in color quantization.
+			By default, Floyd-Steinberg dithering is applied when
+			quantizing colors; this is slow but usually produces
+			the best results.  Ordered dither is a compromise
+			between speed and quality; no dithering is fast but
+			usually looks awful.  Note that these switches have
+			no effect unless color quantization is being done.
+			Ordered dither is only available in -onepass mode.
+
+	-map FILE	Quantize to the colors used in the specified image
+			file.  This is useful for producing multiple files
+			with identical color maps, or for forcing a predefined
+			set of colors to be used.  The FILE must be a GIF
+			or PPM file.  This option overrides -colors and
+			-onepass.
+
+	-nosmooth	Use a faster, lower-quality upsampling routine.
+
+	-onepass	Use one-pass instead of two-pass color quantization.
+			The one-pass method is faster and needs less memory,
+			but it produces a lower-quality image.  -onepass is
+			ignored unless you also say -colors N.  Also,
+			the one-pass method is always used for gray-scale
+			output (the two-pass method is no improvement then).
+
+	-maxmemory N	Set limit for amount of memory to use in processing
+			large images.  Value is in thousands of bytes, or
+			millions of bytes if "M" is attached to the number.
+			For example, -max 4m selects 4000000 bytes.  If more
+			space is needed, temporary files will be used.
+
+	-verbose	Enable debug printout.  More -v's give more printout.
+	or  -debug	Also, version information is printed at startup.
+
+
+HINTS FOR CJPEG
+
+Color GIF files are not the ideal input for JPEG; JPEG is really intended for
+compressing full-color (24-bit) images.  In particular, don't try to convert
+cartoons, line drawings, and other images that have only a few distinct
+colors.  GIF works great on these, JPEG does not.  If you want to convert a
+GIF to JPEG, you should experiment with cjpeg's -quality and -smooth options
+to get a satisfactory conversion.  -smooth 10 or so is often helpful.
+
+Avoid running an image through a series of JPEG compression/decompression
+cycles.  Image quality loss will accumulate; after ten or so cycles the image
+may be noticeably worse than it was after one cycle.  It's best to use a
+lossless format while manipulating an image, then convert to JPEG format when
+you are ready to file the image away.
+
+The -optimize option to cjpeg is worth using when you are making a "final"
+version for posting or archiving.  It's also a win when you are using low
+quality settings to make very small JPEG files; the percentage improvement
+is often a lot more than it is on larger files.
+
+
+HINTS FOR DJPEG
+
+To get a quick preview of an image, use the -grayscale and/or -scale switches.
+"-grayscale -scale 1/8" is the fastest case.
+
+Several options are available that trade off image quality to gain speed.
+"-fast" turns on the recommended settings.
+
+"-dct fast" and/or "-nosmooth" gain speed at a small sacrifice in quality.
+When producing a color-quantized image, "-onepass -dither ordered" is fast but
+much lower quality than the default behavior.  "-dither none" may give
+acceptable results in two-pass mode, but is seldom tolerable in one-pass mode.
+
+If you are fortunate enough to have very fast floating point hardware,
+"-dct float" may be even faster than "-dct fast".
+
+Two-pass color quantization requires a good deal of memory; on MS-DOS machines
+it may run out of memory even with -maxmemory 0.  In that case you can still
+decompress, with some loss of image quality, by specifying -onepass for
+one-pass quantization.
+
+
+HINTS FOR BOTH PROGRAMS
+
+If more space is needed than will fit in the available main memory (as
+determined by -maxmemory), temporary files will be used.  (MS-DOS versions
+will try to get extended or expanded memory first.)  The temporary files are
+often rather large: in typical cases they occupy three bytes per pixel, for
+example 3*800*600 = 1.44Mb for an 800x600 image.  If you don't have enough
+free disk space, leave out -optimize (for cjpeg) or specify -onepass (for
+djpeg).
+
+On MS-DOS, the temporary files are created in the directory named by the TMP
+or TEMP environment variable, or in the current directory if neither of those
+exist.  Amiga implementations put the temp files in the directory named by
+JPEGTMP:, so be sure to assign JPEGTMP: to a disk partition with adequate free
+space.
+
+The default memory usage limit (-maxmemory) is set when the software is
+compiled.  If you get an "insufficient memory" error, try specifying a smaller
+-maxmemory value, even -maxmemory 0 to use the absolute minimum space.  You
+may want to recompile with a smaller default value if this happens often.
+
+On machines that have "environment" variables, you can define the environment
+variable JPEGMEM to set the default memory limit.  The value is specified as
+described for the -maxmemory switch.  JPEGMEM overrides the default value
+specified when the program was compiled, and itself is overridden by an
+explicit -maxmemory switch.
+
+On MS-DOS machines, -maxmemory is the amount of main (conventional) memory to
+use.  (Extended or expanded memory is also used if available.)  Most
+DOS-specific versions of this software do their own memory space estimation
+and do not need you to specify -maxmemory.
+
+
+THE COMMENT UTILITIES
+
+The JPEG standard allows "comment" (COM) blocks to occur within a JPEG file.
+Although the standard doesn't actually define what COM blocks are for, they
+are widely used to hold user-supplied text strings.  This lets you add
+annotations, titles, index terms, etc to your JPEG files, and later retrieve
+them as text.  COM blocks do not interfere with the image stored in the JPEG
+file.  The maximum size of a COM block is 64K, but you can have as many of
+them as you like in one JPEG file.
+
+We provide two utility programs to display COM block contents and add COM
+blocks to a JPEG file.
+
+rdjpgcom searches a JPEG file and prints the contents of any COM blocks on
+standard output.  The command line syntax is
+	rdjpgcom [-verbose] [inputfilename]
+The switch "-verbose" (or just "-v") causes rdjpgcom to also display the JPEG
+image dimensions.  If you omit the input file name from the command line,
+the JPEG file is read from standard input.  (This may not work on some
+operating systems, if binary data can't be read from stdin.)
+
+wrjpgcom adds a COM block, containing text you provide, to a JPEG file.
+Ordinarily, the COM block is added after any existing COM blocks, but you
+can delete the old COM blocks if you wish.  wrjpgcom produces a new JPEG
+file; it does not modify the input file.  DO NOT try to overwrite the input
+file by directing wrjpgcom's output back into it; on most systems this will
+just destroy your file.
+
+The command line syntax for wrjpgcom is similar to cjpeg's.  On Unix-like
+systems, it is
+	wrjpgcom [switches] [inputfilename]
+The output file is written to standard output.  The input file comes from
+the named file, or from standard input if no input file is named.
+
+On most non-Unix systems, the syntax is
+	wrjpgcom [switches] inputfilename outputfilename
+where both input and output file names must be given explicitly.
+
+wrjpgcom understands three switches:
+	-replace		 Delete any existing COM blocks from the file.
+	-comment "Comment text"	 Supply new COM text on command line.
+        -cfile name		 Read text for new COM block from named file.
+(Switch names can be abbreviated.)  If you have only one line of comment text
+to add, you can provide it on the command line with -comment.  The comment
+text must be surrounded with quotes so that it is treated as a single
+argument.  Longer comments can be read from a text file.
+
+If you give neither -comment nor -cfile, then wrjpgcom will read the comment
+text from standard input.  (In this case an input image file name MUST be
+supplied, so that the source JPEG file comes from somewhere else.)  You can
+enter multiple lines, up to 64KB worth.  Type an end-of-file indicator
+(usually control-D or control-Z) to terminate the comment text entry.
+
+wrjpgcom will not add a COM block if the provided comment string is empty.
+Therefore -replace -comment "" can be used to delete all COM blocks from a
+file.
+
+These utility programs do not depend on the IJG JPEG library.  In
+particular, the source code for rdjpgcom is intended as an illustration of
+the minimum amount of code required to parse a JPEG file header correctly.
diff --git a/wrbmp.c b/wrbmp.c
new file mode 100644
index 0000000..06970c6
--- /dev/null
+++ b/wrbmp.c
@@ -0,0 +1,440 @@
+/*
+ * wrbmp.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in Microsoft "BMP"
+ * format (MS Windows 3.x and OS/2 1.x flavors).
+ * Either 8-bit colormapped or 24-bit full-color format can be written.
+ * No compression is supported.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume output to
+ * an ordinary stdio stream.
+ *
+ * This code contributed by James Arthur Boucher.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef BMP_SUPPORTED
+
+
+/*
+ * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
+ * This is not yet implemented.
+ */
+
+#if BITS_IN_JSAMPLE != 8
+  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
+#endif
+
+/*
+ * Since BMP stores scanlines bottom-to-top, we have to invert the image
+ * from JPEG's top-to-bottom order.  To do this, we save the outgoing data
+ * in a virtual array during put_pixel_row calls, then actually emit the
+ * BMP file during finish_output.  The virtual array contains one JSAMPLE per
+ * pixel if the output is grayscale or colormapped, three if it is full color.
+ */
+
+/* Private version of data destination object */
+
+typedef struct {
+  struct djpeg_dest_struct pub;	/* public fields */
+
+  boolean is_os2;		/* saves the OS2 format request flag */
+
+  jvirt_sarray_ptr whole_image;	/* needed to reverse row order */
+  JDIMENSION data_width;	/* JSAMPLEs per row */
+  JDIMENSION row_width;		/* physical width of one row in the BMP file */
+  int pad_bytes;		/* number of padding bytes needed per row */
+  JDIMENSION cur_output_row;	/* next row# to write to virtual array */
+} bmp_dest_struct;
+
+typedef bmp_dest_struct * bmp_dest_ptr;
+
+
+/* Forward declarations */
+LOCAL void write_colormap
+	JPP((j_decompress_ptr cinfo, bmp_dest_ptr dest,
+	     int map_colors, int map_entry_size));
+
+
+/*
+ * Write some pixel data.
+ * In this module rows_supplied will always be 1.
+ */
+
+METHODDEF void
+put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+		JDIMENSION rows_supplied)
+/* This version is for writing 24-bit pixels */
+{
+  bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
+  JSAMPARRAY image_ptr;
+  register JSAMPROW inptr, outptr;
+  register JDIMENSION col;
+  int pad;
+
+  /* Access next row in virtual array */
+  image_ptr = (*cinfo->mem->access_virt_sarray)
+    ((j_common_ptr) cinfo, dest->whole_image, dest->cur_output_row, TRUE);
+  dest->cur_output_row++;
+
+  /* Transfer data.  Note destination values must be in BGR order
+   * (even though Microsoft's own documents say the opposite).
+   */
+  inptr = dest->pub.buffer[0];
+  outptr = image_ptr[0];
+  for (col = cinfo->output_width; col > 0; col--) {
+    outptr[2] = *inptr++;	/* can omit GETJSAMPLE() safely */
+    outptr[1] = *inptr++;
+    outptr[0] = *inptr++;
+    outptr += 3;
+  }
+
+  /* Zero out the pad bytes. */
+  pad = dest->pad_bytes;
+  while (--pad >= 0)
+    *outptr++ = 0;
+}
+
+METHODDEF void
+put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+	       JDIMENSION rows_supplied)
+/* This version is for grayscale OR quantized color output */
+{
+  bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
+  JSAMPARRAY image_ptr;
+  register JSAMPROW inptr, outptr;
+  register JDIMENSION col;
+  int pad;
+
+  /* Access next row in virtual array */
+  image_ptr = (*cinfo->mem->access_virt_sarray)
+    ((j_common_ptr) cinfo, dest->whole_image, dest->cur_output_row, TRUE);
+  dest->cur_output_row++;
+
+  /* Transfer data. */
+  inptr = dest->pub.buffer[0];
+  outptr = image_ptr[0];
+  for (col = cinfo->output_width; col > 0; col--) {
+    *outptr++ = *inptr++;	/* can omit GETJSAMPLE() safely */
+  }
+
+  /* Zero out the pad bytes. */
+  pad = dest->pad_bytes;
+  while (--pad >= 0)
+    *outptr++ = 0;
+}
+
+
+/*
+ * Startup: normally writes the file header.
+ * In this module we may as well postpone everything until finish_output.
+ */
+
+METHODDEF void
+start_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  /* no work here */
+}
+
+
+/*
+ * Finish up at the end of the file.
+ *
+ * Here is where we really output the BMP file.
+ *
+ * First, routines to write the Windows and OS/2 variants of the file header.
+ */
+
+LOCAL void
+write_bmp_header (j_decompress_ptr cinfo, bmp_dest_ptr dest)
+/* Write a Windows-style BMP file header, including colormap if needed */
+{
+  char bmpfileheader[14];
+  char bmpinfoheader[40];
+#define PUT_2B(array,offset,value)  \
+	(array[offset] = (char) ((value) & 0xFF), \
+	 array[offset+1] = (char) (((value) >> 8) & 0xFF))
+#define PUT_4B(array,offset,value)  \
+	(array[offset] = (char) ((value) & 0xFF), \
+	 array[offset+1] = (char) (((value) >> 8) & 0xFF), \
+	 array[offset+2] = (char) (((value) >> 16) & 0xFF), \
+	 array[offset+3] = (char) (((value) >> 24) & 0xFF))
+  INT32 headersize, bfSize;
+  int bits_per_pixel, cmap_entries;
+
+  /* Compute colormap size and total file size */
+  if (cinfo->out_color_space == JCS_RGB) {
+    if (cinfo->quantize_colors) {
+      /* Colormapped RGB */
+      bits_per_pixel = 8;
+      cmap_entries = 256;
+    } else {
+      /* Unquantized, full color RGB */
+      bits_per_pixel = 24;
+      cmap_entries = 0;
+    }
+  } else {
+    /* Grayscale output.  We need to fake a 256-entry colormap. */
+    bits_per_pixel = 8;
+    cmap_entries = 256;
+  }
+  /* File size */
+  headersize = 14 + 40 + cmap_entries * 4; /* Header and colormap */
+  bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height;
+  
+  /* Set unused fields of header to 0 */
+  MEMZERO(bmpfileheader, SIZEOF(bmpfileheader));
+  MEMZERO(bmpinfoheader, SIZEOF(bmpinfoheader));
+
+  /* Fill the file header */
+  bmpfileheader[0] = 0x42;	/* first 2 bytes are ASCII 'B', 'M' */
+  bmpfileheader[1] = 0x4D;
+  PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
+  /* we leave bfReserved1 & bfReserved2 = 0 */
+  PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
+
+  /* Fill the info header (Microsoft calls this a BITMAPINFOHEADER) */
+  PUT_2B(bmpinfoheader, 0, 40);	/* biSize */
+  PUT_4B(bmpinfoheader, 4, cinfo->output_width); /* biWidth */
+  PUT_4B(bmpinfoheader, 8, cinfo->output_height); /* biHeight */
+  PUT_2B(bmpinfoheader, 12, 1);	/* biPlanes - must be 1 */
+  PUT_2B(bmpinfoheader, 14, bits_per_pixel); /* biBitCount */
+  /* we leave biCompression = 0, for none */
+  /* we leave biSizeImage = 0; this is correct for uncompressed data */
+  if (cinfo->density_unit == 2) { /* if have density in dots/cm, then */
+    PUT_4B(bmpinfoheader, 24, (INT32) (cinfo->X_density*100)); /* XPels/M */
+    PUT_4B(bmpinfoheader, 28, (INT32) (cinfo->Y_density*100)); /* XPels/M */
+  }
+  PUT_2B(bmpinfoheader, 32, cmap_entries); /* biClrUsed */
+  /* we leave biClrImportant = 0 */
+
+  if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14)
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+  if (JFWRITE(dest->pub.output_file, bmpinfoheader, 40) != (size_t) 40)
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+
+  if (cmap_entries > 0)
+    write_colormap(cinfo, dest, cmap_entries, 4);
+}
+
+
+LOCAL void
+write_os2_header (j_decompress_ptr cinfo, bmp_dest_ptr dest)
+/* Write an OS2-style BMP file header, including colormap if needed */
+{
+  char bmpfileheader[14];
+  char bmpcoreheader[12];
+  INT32 headersize, bfSize;
+  int bits_per_pixel, cmap_entries;
+
+  /* Compute colormap size and total file size */
+  if (cinfo->out_color_space == JCS_RGB) {
+    if (cinfo->quantize_colors) {
+      /* Colormapped RGB */
+      bits_per_pixel = 8;
+      cmap_entries = 256;
+    } else {
+      /* Unquantized, full color RGB */
+      bits_per_pixel = 24;
+      cmap_entries = 0;
+    }
+  } else {
+    /* Grayscale output.  We need to fake a 256-entry colormap. */
+    bits_per_pixel = 8;
+    cmap_entries = 256;
+  }
+  /* File size */
+  headersize = 14 + 12 + cmap_entries * 3; /* Header and colormap */
+  bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height;
+  
+  /* Set unused fields of header to 0 */
+  MEMZERO(bmpfileheader, SIZEOF(bmpfileheader));
+  MEMZERO(bmpcoreheader, SIZEOF(bmpcoreheader));
+
+  /* Fill the file header */
+  bmpfileheader[0] = 0x42;	/* first 2 bytes are ASCII 'B', 'M' */
+  bmpfileheader[1] = 0x4D;
+  PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
+  /* we leave bfReserved1 & bfReserved2 = 0 */
+  PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
+
+  /* Fill the info header (Microsoft calls this a BITMAPCOREHEADER) */
+  PUT_2B(bmpcoreheader, 0, 12);	/* bcSize */
+  PUT_2B(bmpcoreheader, 4, cinfo->output_width); /* bcWidth */
+  PUT_2B(bmpcoreheader, 6, cinfo->output_height); /* bcHeight */
+  PUT_2B(bmpcoreheader, 8, 1);	/* bcPlanes - must be 1 */
+  PUT_2B(bmpcoreheader, 10, bits_per_pixel); /* bcBitCount */
+
+  if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14)
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+  if (JFWRITE(dest->pub.output_file, bmpcoreheader, 12) != (size_t) 12)
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+
+  if (cmap_entries > 0)
+    write_colormap(cinfo, dest, cmap_entries, 3);
+}
+
+
+/*
+ * Write the colormap.
+ * Windows uses BGR0 map entries; OS/2 uses BGR entries.
+ */
+
+LOCAL void
+write_colormap (j_decompress_ptr cinfo, bmp_dest_ptr dest,
+		int map_colors, int map_entry_size)
+{
+  JSAMPARRAY colormap = cinfo->colormap;
+  int num_colors = cinfo->actual_number_of_colors;
+  FILE * outfile = dest->pub.output_file;
+  int i;
+
+  if (colormap != NULL) {
+    if (cinfo->out_color_components == 3) {
+      /* Normal case with RGB colormap */
+      for (i = 0; i < num_colors; i++) {
+	putc(GETJSAMPLE(colormap[2][i]), outfile);
+	putc(GETJSAMPLE(colormap[1][i]), outfile);
+	putc(GETJSAMPLE(colormap[0][i]), outfile);
+	if (map_entry_size == 4)
+	  putc(0, outfile);
+      }
+    } else {
+      /* Grayscale colormap (only happens with grayscale quantization) */
+      for (i = 0; i < num_colors; i++) {
+	putc(GETJSAMPLE(colormap[0][i]), outfile);
+	putc(GETJSAMPLE(colormap[0][i]), outfile);
+	putc(GETJSAMPLE(colormap[0][i]), outfile);
+	if (map_entry_size == 4)
+	  putc(0, outfile);
+      }
+    }
+  } else {
+    /* If no colormap, must be grayscale data.  Generate a linear "map". */
+    for (i = 0; i < 256; i++) {
+      putc(i, outfile);
+      putc(i, outfile);
+      putc(i, outfile);
+      if (map_entry_size == 4)
+	putc(0, outfile);
+    }
+  }
+  /* Pad colormap with zeros to ensure specified number of colormap entries */ 
+  if (i > map_colors)
+    ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, i);
+  for (; i < map_colors; i++) {
+    putc(0, outfile);
+    putc(0, outfile);
+    putc(0, outfile);
+    if (map_entry_size == 4)
+      putc(0, outfile);
+  }
+}
+
+
+METHODDEF void
+finish_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
+  register FILE * outfile = dest->pub.output_file;
+  JSAMPARRAY image_ptr;
+  register JSAMPROW data_ptr;
+  JDIMENSION row;
+  register JDIMENSION col;
+  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+
+  /* Write the header and colormap */
+  if (dest->is_os2)
+    write_os2_header(cinfo, dest);
+  else
+    write_bmp_header(cinfo, dest);
+
+  /* Write the file body from our virtual array */
+  for (row = cinfo->output_height; row > 0; row--) {
+    if (progress != NULL) {
+      progress->pub.pass_counter = (long) (cinfo->output_height - row);
+      progress->pub.pass_limit = (long) cinfo->output_height;
+      (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+    }
+    image_ptr = (*cinfo->mem->access_virt_sarray)
+      ((j_common_ptr) cinfo, dest->whole_image, row-1, FALSE);
+    data_ptr = image_ptr[0];
+    for (col = dest->row_width; col > 0; col--) {
+      putc(GETJSAMPLE(*data_ptr), outfile);
+      data_ptr++;
+    }
+  }
+  if (progress != NULL)
+    progress->completed_extra_passes++;
+
+  /* Make sure we wrote the output file OK */
+  fflush(outfile);
+  if (ferror(outfile))
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for BMP format output.
+ */
+
+GLOBAL djpeg_dest_ptr
+jinit_write_bmp (j_decompress_ptr cinfo, boolean is_os2)
+{
+  bmp_dest_ptr dest;
+  JDIMENSION row_width;
+
+  /* Create module interface object, fill in method pointers */
+  dest = (bmp_dest_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(bmp_dest_struct));
+  dest->pub.start_output = start_output_bmp;
+  dest->pub.finish_output = finish_output_bmp;
+  dest->is_os2 = is_os2;
+
+  if (cinfo->out_color_space == JCS_GRAYSCALE) {
+    dest->pub.put_pixel_rows = put_gray_rows;
+  } else if (cinfo->out_color_space == JCS_RGB) {
+    if (cinfo->quantize_colors)
+      dest->pub.put_pixel_rows = put_gray_rows;
+    else
+      dest->pub.put_pixel_rows = put_pixel_rows;
+  } else {
+    ERREXIT(cinfo, JERR_BMP_COLORSPACE);
+  }
+
+  /* Calculate output image dimensions so we can allocate space */
+  jpeg_calc_output_dimensions(cinfo);
+
+  /* Determine width of rows in the BMP file (padded to 4-byte boundary). */
+  row_width = cinfo->output_width * cinfo->output_components;
+  dest->data_width = row_width;
+  while ((row_width & 3) != 0) row_width++;
+  dest->row_width = row_width;
+  dest->pad_bytes = (int) (row_width - dest->data_width);
+
+  /* Allocate space for inversion array, prepare for write pass */
+  dest->whole_image = (*cinfo->mem->request_virt_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     row_width, cinfo->output_height, (JDIMENSION) 1);
+  dest->cur_output_row = 0;
+  if (cinfo->progress != NULL) {
+    cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+    progress->total_extra_passes++; /* count file input as separate pass */
+  }
+
+  /* Create decompressor output buffer. */
+  dest->pub.buffer = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE, row_width, (JDIMENSION) 1);
+  dest->pub.buffer_height = 1;
+
+  return (djpeg_dest_ptr) dest;
+}
+
+#endif /* BMP_SUPPORTED */
diff --git a/wrgif.c b/wrgif.c
new file mode 100644
index 0000000..d61e24d
--- /dev/null
+++ b/wrgif.c
@@ -0,0 +1,500 @@
+/*
+ * wrgif.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in GIF format.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume output to
+ * an ordinary stdio stream.
+ */
+
+/*
+ * This code is loosely based on ppmtogif from the PBMPLUS distribution
+ * of Feb. 1991.  That file contains the following copyright notice:
+ *    Based on GIFENCODE by David Rowley <mgardi@watdscu.waterloo.edu>.
+ *    Lempel-Ziv compression based on "compress" by Spencer W. Thomas et al.
+ *    Copyright (C) 1989 by Jef Poskanzer.
+ *    Permission to use, copy, modify, and distribute this software and its
+ *    documentation for any purpose and without fee is hereby granted, provided
+ *    that the above copyright notice appear in all copies and that both that
+ *    copyright notice and this permission notice appear in supporting
+ *    documentation.  This software is provided "as is" without express or
+ *    implied warranty.
+ *
+ * We are also required to state that
+ *    "The Graphics Interchange Format(c) is the Copyright property of
+ *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
+ *    CompuServe Incorporated."
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef GIF_SUPPORTED
+
+
+#define	MAX_LZW_BITS	12	/* maximum LZW code size (4096 symbols) */
+
+typedef INT16 code_int;		/* must hold -1 .. 2**MAX_LZW_BITS */
+
+#define LZW_TABLE_SIZE	((code_int) 1 << MAX_LZW_BITS)
+
+#define HSIZE		5003	/* hash table size for 80% occupancy */
+
+typedef int hash_int;		/* must hold -2*HSIZE..2*HSIZE */
+
+#define MAXCODE(n_bits)	(((code_int) 1 << (n_bits)) - 1)
+
+
+/*
+ * The LZW hash table consists of two parallel arrays:
+ *   hash_code[i]	code of symbol in slot i, or 0 if empty slot
+ *   hash_value[i]	symbol's value; undefined if empty slot
+ * where slot values (i) range from 0 to HSIZE-1.  The symbol value is
+ * its prefix symbol's code concatenated with its suffix character.
+ *
+ * Algorithm:  use open addressing double hashing (no chaining) on the
+ * prefix code / suffix character combination.  We do a variant of Knuth's
+ * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
+ * secondary probe.
+ *
+ * The hash_value[] table is allocated from FAR heap space since it would
+ * use up rather a lot of the near data space in a PC.
+ */
+
+typedef INT32 hash_entry;	/* must hold (code_int<<8) | byte */
+
+#define HASH_ENTRY(prefix,suffix)  ((((hash_entry) (prefix)) << 8) | (suffix))
+
+
+/* Private version of data destination object */
+
+typedef struct {
+  struct djpeg_dest_struct pub;	/* public fields */
+
+  j_decompress_ptr cinfo;	/* back link saves passing separate parm */
+
+  /* State for packing variable-width codes into a bitstream */
+  int n_bits;			/* current number of bits/code */
+  code_int maxcode;		/* maximum code, given n_bits */
+  int init_bits;		/* initial n_bits ... restored after clear */
+  INT32 cur_accum;		/* holds bits not yet output */
+  int cur_bits;			/* # of bits in cur_accum */
+
+  /* LZW string construction */
+  code_int waiting_code;	/* symbol not yet output; may be extendable */
+  boolean first_byte;		/* if TRUE, waiting_code is not valid */
+
+  /* State for LZW code assignment */
+  code_int ClearCode;		/* clear code (doesn't change) */
+  code_int EOFCode;		/* EOF code (ditto) */
+  code_int free_code;		/* first not-yet-used symbol code */
+
+  /* LZW hash table */
+  code_int *hash_code;		/* => hash table of symbol codes */
+  hash_entry FAR *hash_value;	/* => hash table of symbol values */
+
+  /* GIF data packet construction buffer */
+  int bytesinpkt;		/* # of bytes in current packet */
+  char packetbuf[256];		/* workspace for accumulating packet */
+
+} gif_dest_struct;
+
+typedef gif_dest_struct * gif_dest_ptr;
+
+
+/*
+ * Routines to package compressed data bytes into GIF data blocks.
+ * A data block consists of a count byte (1..255) and that many data bytes.
+ */
+
+LOCAL void
+flush_packet (gif_dest_ptr dinfo)
+/* flush any accumulated data */
+{
+  if (dinfo->bytesinpkt > 0) {	/* never write zero-length packet */
+    dinfo->packetbuf[0] = (char) dinfo->bytesinpkt++;
+    if (JFWRITE(dinfo->pub.output_file, dinfo->packetbuf, dinfo->bytesinpkt)
+	!= (size_t) dinfo->bytesinpkt)
+      ERREXIT(dinfo->cinfo, JERR_FILE_WRITE);
+    dinfo->bytesinpkt = 0;
+  }
+}
+
+
+/* Add a character to current packet; flush to disk if necessary */
+#define CHAR_OUT(dinfo,c)  \
+	{ (dinfo)->packetbuf[++(dinfo)->bytesinpkt] = (char) (c);  \
+	    if ((dinfo)->bytesinpkt >= 255)  \
+	      flush_packet(dinfo);  \
+	}
+
+
+/* Routine to convert variable-width codes into a byte stream */
+
+LOCAL void
+output (gif_dest_ptr dinfo, code_int code)
+/* Emit a code of n_bits bits */
+/* Uses cur_accum and cur_bits to reblock into 8-bit bytes */
+{
+  dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits;
+  dinfo->cur_bits += dinfo->n_bits;
+
+  while (dinfo->cur_bits >= 8) {
+    CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
+    dinfo->cur_accum >>= 8;
+    dinfo->cur_bits -= 8;
+  }
+
+  /*
+   * If the next entry is going to be too big for the code size,
+   * then increase it, if possible.  We do this here to ensure
+   * that it's done in sync with the decoder's codesize increases.
+   */
+  if (dinfo->free_code > dinfo->maxcode) {
+    dinfo->n_bits++;
+    if (dinfo->n_bits == MAX_LZW_BITS)
+      dinfo->maxcode = LZW_TABLE_SIZE; /* free_code will never exceed this */
+    else
+      dinfo->maxcode = MAXCODE(dinfo->n_bits);
+  }
+}
+
+
+/* The LZW algorithm proper */
+
+
+LOCAL void
+clear_hash (gif_dest_ptr dinfo)
+/* Fill the hash table with empty entries */
+{
+  /* It's sufficient to zero hash_code[] */
+  MEMZERO(dinfo->hash_code, HSIZE * SIZEOF(code_int));
+}
+
+
+LOCAL void
+clear_block (gif_dest_ptr dinfo)
+/* Reset compressor and issue a Clear code */
+{
+  clear_hash(dinfo);			/* delete all the symbols */
+  dinfo->free_code = dinfo->ClearCode + 2;
+  output(dinfo, dinfo->ClearCode);	/* inform decoder */
+  dinfo->n_bits = dinfo->init_bits;	/* reset code size */
+  dinfo->maxcode = MAXCODE(dinfo->n_bits);
+}
+
+
+LOCAL void
+compress_init (gif_dest_ptr dinfo, int i_bits)
+/* Initialize LZW compressor */
+{
+  /* init all the state variables */
+  dinfo->n_bits = dinfo->init_bits = i_bits;
+  dinfo->maxcode = MAXCODE(dinfo->n_bits);
+  dinfo->ClearCode = ((code_int) 1 << (i_bits - 1));
+  dinfo->EOFCode = dinfo->ClearCode + 1;
+  dinfo->free_code = dinfo->ClearCode + 2;
+  dinfo->first_byte = TRUE;	/* no waiting symbol yet */
+  /* init output buffering vars */
+  dinfo->bytesinpkt = 0;
+  dinfo->cur_accum = 0;
+  dinfo->cur_bits = 0;
+  /* clear hash table */
+  clear_hash(dinfo);
+  /* GIF specifies an initial Clear code */
+  output(dinfo, dinfo->ClearCode);
+}
+
+
+LOCAL void
+compress_byte (gif_dest_ptr dinfo, int c)
+/* Accept and compress one 8-bit byte */
+{
+  register hash_int i;
+  register hash_int disp;
+  register hash_entry probe_value;
+
+  if (dinfo->first_byte) {	/* need to initialize waiting_code */
+    dinfo->waiting_code = c;
+    dinfo->first_byte = FALSE;
+    return;
+  }
+
+  /* Probe hash table to see if a symbol exists for
+   * waiting_code followed by c.
+   * If so, replace waiting_code by that symbol and return.
+   */
+  i = ((hash_int) c << (MAX_LZW_BITS-8)) + dinfo->waiting_code;
+  /* i is less than twice 2**MAX_LZW_BITS, therefore less than twice HSIZE */
+  if (i >= HSIZE)
+    i -= HSIZE;
+
+  probe_value = HASH_ENTRY(dinfo->waiting_code, c);
+  
+  if (dinfo->hash_code[i] != 0) { /* is first probed slot empty? */
+    if (dinfo->hash_value[i] == probe_value) {
+      dinfo->waiting_code = dinfo->hash_code[i];
+      return;
+    }
+    if (i == 0)			/* secondary hash (after G. Knott) */
+      disp = 1;
+    else
+      disp = HSIZE - i;
+    for (;;) {
+      i -= disp;
+      if (i < 0)
+	i += HSIZE;
+      if (dinfo->hash_code[i] == 0)
+	break;			/* hit empty slot */
+      if (dinfo->hash_value[i] == probe_value) {
+	dinfo->waiting_code = dinfo->hash_code[i];
+	return;
+      }
+    }
+  }
+
+  /* here when hashtable[i] is an empty slot; desired symbol not in table */
+  output(dinfo, dinfo->waiting_code);
+  if (dinfo->free_code < LZW_TABLE_SIZE) {
+    dinfo->hash_code[i] = dinfo->free_code++; /* add symbol to hashtable */
+    dinfo->hash_value[i] = probe_value;
+  } else
+    clear_block(dinfo);
+  dinfo->waiting_code = c;
+}
+
+
+LOCAL void
+compress_term (gif_dest_ptr dinfo)
+/* Clean up at end */
+{
+  /* Flush out the buffered code */
+  if (! dinfo->first_byte)
+    output(dinfo, dinfo->waiting_code);
+  /* Send an EOF code */
+  output(dinfo, dinfo->EOFCode);
+  /* Flush the bit-packing buffer */
+  if (dinfo->cur_bits > 0) {
+    CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
+  }
+  /* Flush the packet buffer */
+  flush_packet(dinfo);
+}
+
+
+/* GIF header construction */
+
+
+LOCAL void
+put_word (gif_dest_ptr dinfo, unsigned int w)
+/* Emit a 16-bit word, LSB first */
+{
+  putc(w & 0xFF, dinfo->pub.output_file);
+  putc((w >> 8) & 0xFF, dinfo->pub.output_file);
+}
+
+
+LOCAL void
+put_3bytes (gif_dest_ptr dinfo, int val)
+/* Emit 3 copies of same byte value --- handy subr for colormap construction */
+{
+  putc(val, dinfo->pub.output_file);
+  putc(val, dinfo->pub.output_file);
+  putc(val, dinfo->pub.output_file);
+}
+
+
+LOCAL void
+emit_header (gif_dest_ptr dinfo, int num_colors, JSAMPARRAY colormap)
+/* Output the GIF file header, including color map */
+/* If colormap==NULL, synthesize a gray-scale colormap */
+{
+  int BitsPerPixel, ColorMapSize, InitCodeSize, FlagByte;
+  int cshift = dinfo->cinfo->data_precision - 8;
+  int i;
+
+  if (num_colors > 256)
+    ERREXIT1(dinfo->cinfo, JERR_TOO_MANY_COLORS, num_colors);
+  /* Compute bits/pixel and related values */
+  BitsPerPixel = 1;
+  while (num_colors > (1 << BitsPerPixel))
+    BitsPerPixel++;
+  ColorMapSize = 1 << BitsPerPixel;
+  if (BitsPerPixel <= 1)
+    InitCodeSize = 2;
+  else
+    InitCodeSize = BitsPerPixel;
+  /*
+   * Write the GIF header.
+   * Note that we generate a plain GIF87 header for maximum compatibility.
+   */
+  putc('G', dinfo->pub.output_file);
+  putc('I', dinfo->pub.output_file);
+  putc('F', dinfo->pub.output_file);
+  putc('8', dinfo->pub.output_file);
+  putc('7', dinfo->pub.output_file);
+  putc('a', dinfo->pub.output_file);
+  /* Write the Logical Screen Descriptor */
+  put_word(dinfo, (unsigned int) dinfo->cinfo->output_width);
+  put_word(dinfo, (unsigned int) dinfo->cinfo->output_height);
+  FlagByte = 0x80;		/* Yes, there is a global color table */
+  FlagByte |= (BitsPerPixel-1) << 4; /* color resolution */
+  FlagByte |= (BitsPerPixel-1);	/* size of global color table */
+  putc(FlagByte, dinfo->pub.output_file);
+  putc(0, dinfo->pub.output_file); /* Background color index */
+  putc(0, dinfo->pub.output_file); /* Reserved (aspect ratio in GIF89) */
+  /* Write the Global Color Map */
+  /* If the color map is more than 8 bits precision, */
+  /* we reduce it to 8 bits by shifting */
+  for (i=0; i < ColorMapSize; i++) {
+    if (i < num_colors) {
+      if (colormap != NULL) {
+	if (dinfo->cinfo->out_color_space == JCS_RGB) {
+	  /* Normal case: RGB color map */
+	  putc(GETJSAMPLE(colormap[0][i]) >> cshift, dinfo->pub.output_file);
+	  putc(GETJSAMPLE(colormap[1][i]) >> cshift, dinfo->pub.output_file);
+	  putc(GETJSAMPLE(colormap[2][i]) >> cshift, dinfo->pub.output_file);
+	} else {
+	  /* Grayscale "color map": possible if quantizing grayscale image */
+	  put_3bytes(dinfo, GETJSAMPLE(colormap[0][i]) >> cshift);
+	}
+      } else {
+	/* Create a gray-scale map of num_colors values, range 0..255 */
+	put_3bytes(dinfo, (i * 255 + (num_colors-1)/2) / (num_colors-1));
+      }
+    } else {
+      /* fill out the map to a power of 2 */
+      put_3bytes(dinfo, 0);
+    }
+  }
+  /* Write image separator and Image Descriptor */
+  putc(',', dinfo->pub.output_file); /* separator */
+  put_word(dinfo, 0);		/* left/top offset */
+  put_word(dinfo, 0);
+  put_word(dinfo, (unsigned int) dinfo->cinfo->output_width); /* image size */
+  put_word(dinfo, (unsigned int) dinfo->cinfo->output_height);
+  /* flag byte: not interlaced, no local color map */
+  putc(0x00, dinfo->pub.output_file);
+  /* Write Initial Code Size byte */
+  putc(InitCodeSize, dinfo->pub.output_file);
+
+  /* Initialize for LZW compression of image data */
+  compress_init(dinfo, InitCodeSize+1);
+}
+
+
+/*
+ * Startup: write the file header.
+ */
+
+METHODDEF void
+start_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  gif_dest_ptr dest = (gif_dest_ptr) dinfo;
+
+  if (cinfo->quantize_colors)
+    emit_header(dest, cinfo->actual_number_of_colors, cinfo->colormap);
+  else
+    emit_header(dest, 256, (JSAMPARRAY) NULL);
+}
+
+
+/*
+ * Write some pixel data.
+ * In this module rows_supplied will always be 1.
+ */
+
+METHODDEF void
+put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+		JDIMENSION rows_supplied)
+{
+  gif_dest_ptr dest = (gif_dest_ptr) dinfo;
+  register JSAMPROW ptr;
+  register JDIMENSION col;
+
+  ptr = dest->pub.buffer[0];
+  for (col = cinfo->output_width; col > 0; col--) {
+    compress_byte(dest, GETJSAMPLE(*ptr++));
+  }
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF void
+finish_output_gif (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  gif_dest_ptr dest = (gif_dest_ptr) dinfo;
+
+  /* Flush LZW mechanism */
+  compress_term(dest);
+  /* Write a zero-length data block to end the series */
+  putc(0, dest->pub.output_file);
+  /* Write the GIF terminator mark */
+  putc(';', dest->pub.output_file);
+  /* Make sure we wrote the output file OK */
+  fflush(dest->pub.output_file);
+  if (ferror(dest->pub.output_file))
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for GIF format output.
+ */
+
+GLOBAL djpeg_dest_ptr
+jinit_write_gif (j_decompress_ptr cinfo)
+{
+  gif_dest_ptr dest;
+
+  /* Create module interface object, fill in method pointers */
+  dest = (gif_dest_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(gif_dest_struct));
+  dest->cinfo = cinfo;		/* make back link for subroutines */
+  dest->pub.start_output = start_output_gif;
+  dest->pub.put_pixel_rows = put_pixel_rows;
+  dest->pub.finish_output = finish_output_gif;
+
+  if (cinfo->out_color_space != JCS_GRAYSCALE &&
+      cinfo->out_color_space != JCS_RGB)
+    ERREXIT(cinfo, JERR_GIF_COLORSPACE);
+
+  /* Force quantization if color or if > 8 bits input */
+  if (cinfo->out_color_space != JCS_GRAYSCALE || cinfo->data_precision > 8) {
+    /* Force quantization to at most 256 colors */
+    cinfo->quantize_colors = TRUE;
+    if (cinfo->desired_number_of_colors > 256)
+      cinfo->desired_number_of_colors = 256;
+  }
+
+  /* Calculate output image dimensions so we can allocate space */
+  jpeg_calc_output_dimensions(cinfo);
+
+  if (cinfo->output_components != 1) /* safety check: just one component? */
+    ERREXIT(cinfo, JERR_GIF_BUG);
+
+  /* Create decompressor output buffer. */
+  dest->pub.buffer = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE, cinfo->output_width, (JDIMENSION) 1);
+  dest->pub.buffer_height = 1;
+
+  /* Allocate space for hash table */
+  dest->hash_code = (code_int *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				HSIZE * SIZEOF(code_int));
+  dest->hash_value = (hash_entry FAR *)
+    (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				HSIZE * SIZEOF(hash_entry));
+
+  return (djpeg_dest_ptr) dest;
+}
+
+#endif /* GIF_SUPPORTED */
diff --git a/wrjpgcom.1 b/wrjpgcom.1
new file mode 100644
index 0000000..71ec4de
--- /dev/null
+++ b/wrjpgcom.1
@@ -0,0 +1,102 @@
+.TH WRJPGCOM 1 "30 August 1994"
+.SH NAME
+wrjpgcom \- insert text comments into a JPEG file
+.SH SYNOPSIS
+.B wrjpgcom
+[
+.B \-replace
+]
+[
+.BI \-comment " text"
+]
+[
+.BI \-cfile " name"
+]
+[
+.I filename
+]
+.LP
+.SH DESCRIPTION
+.LP
+.B wrjpgcom
+reads the named JPEG/JFIF file, or the standard input if no file is named,
+and generates a new JPEG/JFIF file on standard output.  A comment block is
+added to the file.
+.PP
+The JPEG standard allows "comment" (COM) blocks to occur within a JPEG file.
+Although the standard doesn't actually define what COM blocks are for, they
+are widely used to hold user-supplied text strings.  This lets you add
+annotations, titles, index terms, etc to your JPEG files, and later retrieve
+them as text.  COM blocks do not interfere with the image stored in the JPEG
+file.  The maximum size of a COM block is 64K, but you can have as many of
+them as you like in one JPEG file.
+.PP
+.B wrjpgcom
+adds a COM block, containing text you provide, to a JPEG file.
+Ordinarily, the COM block is added after any existing COM blocks; but you
+can delete the old COM blocks if you wish.
+.SH OPTIONS
+Switch names may be abbreviated, and are not case sensitive.
+.TP
+.B \-replace
+Delete any existing COM blocks from the file.
+.TP
+.BI \-comment " text"
+Supply text for new COM block on command line.
+.TP
+.BI \-cfile " name"
+Read text for new COM block from named file.
+.PP
+If you have only one line of comment text to add, you can provide it on the
+command line with
+.BR \-comment .
+The comment text must be surrounded with quotes so that it is treated as a
+single argument.  Longer comments can be read from a text file.
+.PP
+If you give neither
+.B \-comment
+nor
+.BR \-cfile ,
+then
+.B wrjpgcom
+will read the comment text from standard input.  (In this case an input image
+file name MUST be supplied, so that the source JPEG file comes from somewhere
+else.)  You can enter multiple lines, up to 64KB worth.  Type an end-of-file
+indicator (usually control-D) to terminate the comment text entry.
+.PP
+.B wrjpgcom
+will not add a COM block if the provided comment string is empty.  Therefore
+\fB\-replace \-comment ""\fR can be used to delete all COM blocks from a file.
+.SH EXAMPLES
+.LP
+Add a short comment to in.jpg, producing out.jpg:
+.IP
+.B wrjpgcom \-c
+\fI"View of my back yard" in.jpg
+.B >
+.I out.jpg
+.PP
+Attach a long comment previously stored in comment.txt:
+.IP
+.B wrjpgcom
+.I in.jpg
+.B <
+.I comment.txt
+.B >
+.I out.jpg
+.PP
+or equivalently
+.IP
+.B wrjpgcom
+.B -cfile
+.I comment.txt
+.B <
+.I in.jpg
+.B >
+.I out.jpg
+.SH SEE ALSO
+.BR cjpeg (1),
+.BR djpeg (1),
+.BR rdjpgcom (1)
+.SH AUTHOR
+Independent JPEG Group
diff --git a/wrjpgcom.c b/wrjpgcom.c
new file mode 100644
index 0000000..0b25d69
--- /dev/null
+++ b/wrjpgcom.c
@@ -0,0 +1,574 @@
+/*
+ * wrjpgcom.c
+ *
+ * Copyright (C) 1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains a very simple stand-alone application that inserts
+ * user-supplied text as a COM (comment) marker in a JFIF file.
+ * This may be useful as an example of the minimum logic needed to parse
+ * JPEG markers.
+ */
+
+#define JPEG_CJPEG_DJPEG	/* to get the command-line config symbols */
+#include "jinclude.h"		/* get auto-config symbols, <stdio.h> */
+
+#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc() */
+extern void * malloc ();
+#endif
+#include <ctype.h>		/* to declare isupper(), tolower() */
+#ifdef USE_SETMODE
+#include <fcntl.h>		/* to declare setmode()'s parameter macros */
+/* If you have setmode() but not <io.h>, just delete this line: */
+#include <io.h>			/* to declare setmode() */
+#endif
+
+#ifdef USE_CCOMMAND		/* command-line reader for Macintosh */
+#ifdef __MWERKS__
+#include <SIOUX.h>              /* Metrowerks declares it here */
+#endif
+#ifdef THINK_C
+#include <console.h>		/* Think declares it here */
+#endif
+#endif
+
+#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
+#define READ_BINARY	"r"
+#define WRITE_BINARY	"w"
+#else
+#define READ_BINARY	"rb"
+#define WRITE_BINARY	"wb"
+#endif
+
+#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
+#define EXIT_FAILURE  1
+#endif
+#ifndef EXIT_SUCCESS
+#ifdef VMS
+#define EXIT_SUCCESS  1		/* VMS is very nonstandard */
+#else
+#define EXIT_SUCCESS  0
+#endif
+#endif
+
+/* Reduce this value if your malloc() can't allocate blocks up to 64K.
+ * On DOS, compiling in large model is usually a better solution.
+ */
+
+#ifndef MAX_COM_LENGTH
+#define MAX_COM_LENGTH 65000	/* must be < 65534 in any case */
+#endif
+
+
+/*
+ * These macros are used to read the input file and write the output file.
+ * To reuse this code in another application, you might need to change these.
+ */
+
+static FILE * infile;		/* input JPEG file */
+
+/* Return next input byte, or EOF if no more */
+#define NEXTBYTE()  getc(infile)
+
+static FILE * outfile;		/* output JPEG file */
+
+/* Emit an output byte */
+#define PUTBYTE(x)  putc((x), outfile)
+
+
+/* Error exit handler */
+#define ERREXIT(msg)  (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
+
+
+/* Read one byte, testing for EOF */
+static int
+read_1_byte (void)
+{
+  int c;
+
+  c = NEXTBYTE();
+  if (c == EOF)
+    ERREXIT("Premature EOF in JPEG file");
+  return c;
+}
+
+/* Read 2 bytes, convert to unsigned int */
+/* All 2-byte quantities in JPEG markers are MSB first */
+static unsigned int
+read_2_bytes (void)
+{
+  int c1, c2;
+
+  c1 = NEXTBYTE();
+  if (c1 == EOF)
+    ERREXIT("Premature EOF in JPEG file");
+  c2 = NEXTBYTE();
+  if (c2 == EOF)
+    ERREXIT("Premature EOF in JPEG file");
+  return (((unsigned int) c1) << 8) + ((unsigned int) c2);
+}
+
+
+/* Routines to write data to output file */
+
+static void
+write_1_byte (int c)
+{
+  PUTBYTE(c);
+}
+
+static void
+write_2_bytes (unsigned int val)
+{
+  PUTBYTE((val >> 8) & 0xFF);
+  PUTBYTE(val & 0xFF);
+}
+
+static void
+write_marker (int marker)
+{
+  PUTBYTE(0xFF);
+  PUTBYTE(marker);
+}
+
+static void
+copy_rest_of_file (void)
+{
+  int c;
+
+  while ((c = NEXTBYTE()) != EOF)
+    PUTBYTE(c);
+}
+
+
+/*
+ * JPEG markers consist of one or more 0xFF bytes, followed by a marker
+ * code byte (which is not an FF).  Here are the marker codes of interest
+ * in this program.  (See jdmarker.c for a more complete list.)
+ */
+
+#define M_SOF0  0xC0		/* Start Of Frame N */
+#define M_SOF1  0xC1		/* N indicates which compression process */
+#define M_SOF2  0xC2		/* Only SOF0 and SOF1 are now in common use */
+#define M_SOF3  0xC3
+#define M_SOF5  0xC5
+#define M_SOF6  0xC6
+#define M_SOF7  0xC7
+#define M_SOF9  0xC9
+#define M_SOF10 0xCA
+#define M_SOF11 0xCB
+#define M_SOF13 0xCD
+#define M_SOF14 0xCE
+#define M_SOF15 0xCF
+#define M_SOI   0xD8		/* Start Of Image (beginning of datastream) */
+#define M_EOI   0xD9		/* End Of Image (end of datastream) */
+#define M_SOS   0xDA		/* Start Of Scan (begins compressed data) */
+#define M_COM   0xFE		/* COMment */
+
+
+/*
+ * Find the next JPEG marker and return its marker code.
+ * We expect at least one FF byte, possibly more if the compressor used FFs
+ * to pad the file.  (Padding FFs will NOT be replicated in the output file.)
+ * There could also be non-FF garbage between markers.  The treatment of such
+ * garbage is unspecified; we choose to skip over it but emit a warning msg.
+ * NB: this routine must not be used after seeing SOS marker, since it will
+ * not deal correctly with FF/00 sequences in the compressed image data...
+ */
+
+static int
+next_marker (void)
+{
+  int c;
+  int discarded_bytes = 0;
+
+  /* Find 0xFF byte; count and skip any non-FFs. */
+  c = read_1_byte();
+  while (c != 0xFF) {
+    discarded_bytes++;
+    c = read_1_byte();
+  }
+  /* Get marker code byte, swallowing any duplicate FF bytes.  Extra FFs
+   * are legal as pad bytes, so don't count them in discarded_bytes.
+   */
+  do {
+    c = read_1_byte();
+  } while (c == 0xFF);
+
+  if (discarded_bytes != 0) {
+    fprintf(stderr, "Warning: garbage data found in JPEG file\n");
+  }
+
+  return c;
+}
+
+
+/*
+ * Read the initial marker, which should be SOI.
+ * For a JFIF file, the first two bytes of the file should be literally
+ * 0xFF M_SOI.  To be more general, we could use next_marker, but if the
+ * input file weren't actually JPEG at all, next_marker might read the whole
+ * file and then return a misleading error message...
+ */
+
+static int
+first_marker (void)
+{
+  int c1, c2;
+
+  c1 = NEXTBYTE();
+  c2 = NEXTBYTE();
+  if (c1 != 0xFF || c2 != M_SOI)
+    ERREXIT("Not a JPEG file");
+  return c2;
+}
+
+
+/*
+ * Most types of marker are followed by a variable-length parameter segment.
+ * This routine skips over the parameters for any marker we don't otherwise
+ * want to process.
+ * Note that we MUST skip the parameter segment explicitly in order not to
+ * be fooled by 0xFF bytes that might appear within the parameter segment;
+ * such bytes do NOT introduce new markers.
+ */
+
+static void
+copy_variable (void)
+/* Copy an unknown or uninteresting variable-length marker */
+{
+  unsigned int length;
+
+  /* Get the marker parameter length count */
+  length = read_2_bytes();
+  write_2_bytes(length);
+  /* Length includes itself, so must be at least 2 */
+  if (length < 2)
+    ERREXIT("Erroneous JPEG marker length");
+  length -= 2;
+  /* Skip over the remaining bytes */
+  while (length > 0) {
+    write_1_byte(read_1_byte());
+    length--;
+  }
+}
+
+static void
+skip_variable (void)
+/* Skip over an unknown or uninteresting variable-length marker */
+{
+  unsigned int length;
+
+  /* Get the marker parameter length count */
+  length = read_2_bytes();
+  /* Length includes itself, so must be at least 2 */
+  if (length < 2)
+    ERREXIT("Erroneous JPEG marker length");
+  length -= 2;
+  /* Skip over the remaining bytes */
+  while (length > 0) {
+    (void) read_1_byte();
+    length--;
+  }
+}
+
+
+/*
+ * Parse the marker stream until SOFn or EOI is seen;
+ * copy data to output, but discard COM markers unless keep_COM is true.
+ */
+
+static int
+scan_JPEG_header (int keep_COM)
+{
+  int marker;
+
+  /* Expect SOI at start of file */
+  if (first_marker() != M_SOI)
+    ERREXIT("Expected SOI marker first");
+  write_marker(M_SOI);
+
+  /* Scan miscellaneous markers until we reach SOFn. */
+  for (;;) {
+    marker = next_marker();
+    switch (marker) {
+    case M_SOF0:		/* Baseline */
+    case M_SOF1:		/* Extended sequential, Huffman */
+    case M_SOF2:		/* Progressive, Huffman */
+    case M_SOF3:		/* Lossless, Huffman */
+    case M_SOF5:		/* Differential sequential, Huffman */
+    case M_SOF6:		/* Differential progressive, Huffman */
+    case M_SOF7:		/* Differential lossless, Huffman */
+    case M_SOF9:		/* Extended sequential, arithmetic */
+    case M_SOF10:		/* Progressive, arithmetic */
+    case M_SOF11:		/* Lossless, arithmetic */
+    case M_SOF13:		/* Differential sequential, arithmetic */
+    case M_SOF14:		/* Differential progressive, arithmetic */
+    case M_SOF15:		/* Differential lossless, arithmetic */
+      return marker;
+
+    case M_SOS:			/* should not see compressed data before SOF */
+      ERREXIT("SOS without prior SOFn");
+      break;
+
+    case M_EOI:			/* in case it's a tables-only JPEG stream */
+      return marker;
+
+    case M_COM:			/* Existing COM: conditionally discard */
+      if (keep_COM) {
+	write_marker(marker);
+	copy_variable();
+      } else {
+	skip_variable();
+      }
+      break;
+
+    default:			/* Anything else just gets copied */
+      write_marker(marker);
+      copy_variable();		/* we assume it has a parameter count... */
+      break;
+    }
+  } /* end loop */
+}
+
+
+/* Command line parsing code */
+
+static const char * progname;	/* program name for error messages */
+
+
+static void
+usage (void)
+/* complain about bad command line */
+{
+  fprintf(stderr, "wrjpgcom inserts a textual comment in a JPEG file.\n");
+  fprintf(stderr, "You can add to or replace any existing comment(s).\n");
+
+  fprintf(stderr, "Usage: %s [switches] ", progname);
+#ifdef TWO_FILE_COMMANDLINE
+  fprintf(stderr, "inputfile outputfile\n");
+#else
+  fprintf(stderr, "[inputfile]\n");
+#endif
+
+  fprintf(stderr, "Switches (names may be abbreviated):\n");
+  fprintf(stderr, "  -replace         Delete any existing comments\n");
+  fprintf(stderr, "  -comment \"text\"  Insert comment with given text\n");
+  fprintf(stderr, "  -cfile name      Read comment from named file\n");
+  fprintf(stderr, "Notice that you must put quotes around the comment text\n");
+  fprintf(stderr, "when you use -comment.\n");
+  fprintf(stderr, "If you do not give either -comment or -cfile on the command line,\n");
+  fprintf(stderr, "then the comment text is read from standard input.\n");
+  fprintf(stderr, "It can be multiple lines, up to %u characters total.\n",
+	  (unsigned int) MAX_COM_LENGTH);
+#ifndef TWO_FILE_COMMANDLINE
+  fprintf(stderr, "You must specify an input JPEG file name when supplying\n");
+  fprintf(stderr, "comment text from standard input.\n");
+#endif
+
+  exit(EXIT_FAILURE);
+}
+
+
+static int
+keymatch (char * arg, const char * keyword, int minchars)
+/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
+/* keyword is the constant keyword (must be lower case already), */
+/* minchars is length of minimum legal abbreviation. */
+{
+  register int ca, ck;
+  register int nmatched = 0;
+
+  while ((ca = *arg++) != '\0') {
+    if ((ck = *keyword++) == '\0')
+      return 0;			/* arg longer than keyword, no good */
+    if (isupper(ca))		/* force arg to lcase (assume ck is already) */
+      ca = tolower(ca);
+    if (ca != ck)
+      return 0;			/* no good */
+    nmatched++;			/* count matched characters */
+  }
+  /* reached end of argument; fail if it's too short for unique abbrev */
+  if (nmatched < minchars)
+    return 0;
+  return 1;			/* A-OK */
+}
+
+
+/*
+ * The main program.
+ */
+
+int
+main (int argc, char **argv)
+{
+  int argn;
+  char * arg;
+  int keep_COM = 1;
+  char * comment_arg = NULL;
+  FILE * comment_file = NULL;
+  unsigned int comment_length = 0;
+  int marker;
+
+  /* On Mac, fetch a command line. */
+#ifdef USE_CCOMMAND
+  argc = ccommand(&argv);
+#endif
+
+  progname = argv[0];
+  if (progname == NULL || progname[0] == 0)
+    progname = "wrjpgcom";	/* in case C library doesn't provide it */
+
+  /* Parse switches, if any */
+  for (argn = 1; argn < argc; argn++) {
+    arg = argv[argn];
+    if (arg[0] != '-')
+      break;			/* not switch, must be file name */
+    arg++;			/* advance over '-' */
+    if (keymatch(arg, "replace", 1)) {
+      keep_COM = 0;
+    } else if (keymatch(arg, "cfile", 2)) {
+      if (++argn >= argc) usage();
+      if ((comment_file = fopen(argv[argn], "r")) == NULL) {
+	fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
+	exit(EXIT_FAILURE);
+      }
+    } else if (keymatch(arg, "comment", 1)) {
+      if (++argn >= argc) usage();
+      comment_arg = argv[argn];
+      /* If the comment text starts with '"', then we are probably running
+       * under MS-DOG and must parse out the quoted string ourselves.  Sigh.
+       */
+      if (comment_arg[0] == '"') {
+	comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
+	if (comment_arg == NULL)
+	  ERREXIT("Insufficient memory");
+	strcpy(comment_arg, argv[argn]+1);
+	for (;;) {
+	  comment_length = strlen(comment_arg);
+	  if (comment_length > 0 && comment_arg[comment_length-1] == '"') {
+	    comment_arg[comment_length-1] = '\0'; /* zap terminating quote */
+	    break;
+	  }
+	  if (++argn >= argc)
+	    ERREXIT("Missing ending quote mark");
+	  strcat(comment_arg, " ");
+	  strcat(comment_arg, argv[argn]);
+	}
+      }
+      comment_length = strlen(comment_arg);
+    } else
+      usage();
+  }
+
+  /* Cannot use both -comment and -cfile. */
+  if (comment_arg != NULL && comment_file != NULL)
+    usage();
+  /* If there is neither -comment nor -cfile, we will read the comment text
+   * from stdin; in this case there MUST be an input JPEG file name.
+   */
+  if (comment_arg == NULL && comment_file == NULL && argn >= argc)
+    usage();
+
+  /* Open the input file. */
+  if (argn < argc) {
+    if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
+      exit(EXIT_FAILURE);
+    }
+  } else {
+    /* default input file is stdin */
+#ifdef USE_SETMODE		/* need to hack file mode? */
+    setmode(fileno(stdin), O_BINARY);
+#endif
+#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
+    if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
+      fprintf(stderr, "%s: can't open stdin\n", progname);
+      exit(EXIT_FAILURE);
+    }
+#else
+    infile = stdin;
+#endif
+  }
+
+  /* Open the output file. */
+#ifdef TWO_FILE_COMMANDLINE
+  /* Must have explicit output file name */
+  if (argn != argc-2) {
+    fprintf(stderr, "%s: must name one input and one output file\n",
+	    progname);
+    usage();
+  }
+  if ((outfile = fopen(argv[argn+1], WRITE_BINARY)) == NULL) {
+    fprintf(stderr, "%s: can't open %s\n", progname, argv[argn+1]);
+    exit(EXIT_FAILURE);
+  }
+#else
+  /* Unix style: expect zero or one file name */
+  if (argn < argc-1) {
+    fprintf(stderr, "%s: only one input file\n", progname);
+    usage();
+  }
+  /* default output file is stdout */
+#ifdef USE_SETMODE		/* need to hack file mode? */
+  setmode(fileno(stdout), O_BINARY);
+#endif
+#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
+  if ((outfile = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
+    fprintf(stderr, "%s: can't open stdout\n", progname);
+    exit(EXIT_FAILURE);
+  }
+#else
+  outfile = stdout;
+#endif
+#endif /* TWO_FILE_COMMANDLINE */
+
+  /* Collect comment text from comment_file or stdin, if necessary */
+  if (comment_arg == NULL) {
+    FILE * src_file;
+    int c;
+
+    comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
+    if (comment_arg == NULL)
+      ERREXIT("Insufficient memory");
+    comment_length = 0;
+    src_file = (comment_file != NULL ? comment_file : stdin);
+    while ((c = getc(src_file)) != EOF) {
+      if (comment_length >= (unsigned int) MAX_COM_LENGTH) {
+	fprintf(stderr, "Comment text may not exceed %u bytes\n",
+		(unsigned int) MAX_COM_LENGTH);
+	exit(EXIT_FAILURE);
+      }
+      comment_arg[comment_length++] = (char) c;
+    }
+    if (comment_file != NULL)
+      fclose(comment_file);
+  }
+
+  /* Copy JPEG headers until SOFn marker;
+   * we will insert the new comment marker just before SOFn.
+   * This (a) causes the new comment to appear after, rather than before,
+   * existing comments; and (b) ensures that comments come after any JFIF
+   * or JFXX markers, as required by the JFIF specification.
+   */
+  marker = scan_JPEG_header(keep_COM);
+  /* Insert the new COM marker, but only if nonempty text has been supplied */
+  if (comment_length > 0) {
+    write_marker(M_COM);
+    write_2_bytes(comment_length + 2);
+    while (comment_length > 0) {
+      write_1_byte(*comment_arg++);
+      comment_length--;
+    }
+  }
+  /* Duplicate the remainder of the source file.
+   * Note that any COM markers occuring after SOF will not be touched.
+   */
+  write_marker(marker);
+  copy_rest_of_file();
+
+  /* All done. */
+  exit(EXIT_SUCCESS);
+  return 0;			/* suppress no-return-value warnings */
+}
diff --git a/wrppm.c b/wrppm.c
new file mode 100644
index 0000000..4f7a2cb
--- /dev/null
+++ b/wrppm.c
@@ -0,0 +1,248 @@
+/*
+ * wrppm.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in PPM/PGM format.
+ * The PBMPLUS library is NOT required to compile this software
+ * (but it is highly useful as a set of PPM image manipulation programs).
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume output to
+ * an ordinary stdio stream.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef PPM_SUPPORTED
+
+
+/*
+ * Currently, this code only knows how to write raw PPM or PGM format,
+ * which can be no more than 8 bits/sample.  As an expedient for testing
+ * 12-bit JPEG mode, we support writing 12-bit data to an 8-bit file by
+ * downscaling the values.  Of course this implies loss of precision.
+ * (When the core library supports data precision reduction, a cleaner
+ * implementation will be to ask for that instead.)
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define DOWNSCALE(x)  (x)
+#else
+#define DOWNSCALE(x)  ((x) >> (BITS_IN_JSAMPLE-8))
+#endif
+
+
+/*
+ * When JSAMPLE is the same size as char, we can just fwrite() the
+ * decompressed data to the PPM or PGM file.  On PCs, in order to make this
+ * work the output buffer must be allocated in near data space, because we are
+ * assuming small-data memory model wherein fwrite() can't reach far memory.
+ * If you need to process very wide images on a PC, you might have to compile
+ * in large-memory model, or else replace fwrite() with a putc() loop ---
+ * which will be much slower.
+ */
+
+
+/* Private version of data destination object */
+
+typedef struct {
+  struct djpeg_dest_struct pub;	/* public fields */
+
+  /* Usually these two pointers point to the same place: */
+  char *iobuffer;		/* fwrite's I/O buffer */
+  JSAMPROW pixrow;		/* decompressor output buffer */
+
+  JDIMENSION buffer_width;	/* width of one row */
+} ppm_dest_struct;
+
+typedef ppm_dest_struct * ppm_dest_ptr;
+
+
+/*
+ * Write some pixel data.
+ * In this module rows_supplied will always be 1.
+ *
+ * put_pixel_rows handles the "normal" 8-bit case where the decompressor
+ * output buffer is physically the same as the fwrite buffer.
+ */
+
+METHODDEF void
+put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+		JDIMENSION rows_supplied)
+{
+  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+
+  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * This code is used when we have to copy the data because JSAMPLE is not
+ * the same size as char.  Typically this only happens in 12-bit mode.
+ */
+
+METHODDEF void
+copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+		 JDIMENSION rows_supplied)
+{
+  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+  register char * bufferptr;
+  register JSAMPROW ptr;
+  register JDIMENSION col;
+
+  ptr = dest->pub.buffer[0];
+  bufferptr = dest->iobuffer;
+  for (col = dest->buffer_width; col > 0; col--) {
+    *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(*ptr++));
+  }
+  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * Write some pixel data when color quantization is in effect.
+ * We have to demap the color index values to straight data.
+ */
+
+METHODDEF void
+put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+		  JDIMENSION rows_supplied)
+{
+  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+  register char * bufferptr;
+  register int pixval;
+  register JSAMPROW ptr;
+  register JSAMPROW color_map0 = cinfo->colormap[0];
+  register JSAMPROW color_map1 = cinfo->colormap[1];
+  register JSAMPROW color_map2 = cinfo->colormap[2];
+  register JDIMENSION col;
+
+  ptr = dest->pub.buffer[0];
+  bufferptr = dest->iobuffer;
+  for (col = cinfo->output_width; col > 0; col--) {
+    pixval = GETJSAMPLE(*ptr++);
+    *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(color_map0[pixval]));
+    *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(color_map1[pixval]));
+    *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(color_map2[pixval]));
+  }
+  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+METHODDEF void
+put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+		   JDIMENSION rows_supplied)
+{
+  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+  register char * bufferptr;
+  register JSAMPROW ptr;
+  register JSAMPROW color_map = cinfo->colormap[0];
+  register JDIMENSION col;
+
+  ptr = dest->pub.buffer[0];
+  bufferptr = dest->iobuffer;
+  for (col = cinfo->output_width; col > 0; col--) {
+    *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
+  }
+  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * Startup: write the file header.
+ */
+
+METHODDEF void
+start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
+
+  /* Emit file header */
+  switch (cinfo->out_color_space) {
+  case JCS_GRAYSCALE:
+    /* emit header for raw PGM format */
+    fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
+	    (long) cinfo->output_width, (long) cinfo->output_height, 255);
+    break;
+  case JCS_RGB:
+    /* emit header for raw PPM format */
+    fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
+	    (long) cinfo->output_width, (long) cinfo->output_height, 255);
+    break;
+  default:
+    ERREXIT(cinfo, JERR_PPM_COLORSPACE);
+  }
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF void
+finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  /* Make sure we wrote the output file OK */
+  fflush(dinfo->output_file);
+  if (ferror(dinfo->output_file))
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for PPM format output.
+ */
+
+GLOBAL djpeg_dest_ptr
+jinit_write_ppm (j_decompress_ptr cinfo)
+{
+  ppm_dest_ptr dest;
+
+  /* Create module interface object, fill in method pointers */
+  dest = (ppm_dest_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(ppm_dest_struct));
+  dest->pub.start_output = start_output_ppm;
+  dest->pub.finish_output = finish_output_ppm;
+
+  /* Calculate output image dimensions so we can allocate space */
+  jpeg_calc_output_dimensions(cinfo);
+
+  /* Create physical I/O buffer.  Note we make this near on a PC. */
+  dest->buffer_width = cinfo->output_width * cinfo->out_color_components;
+  dest->iobuffer = (char *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(size_t) (dest->buffer_width * SIZEOF(char)));
+
+  if (cinfo->quantize_colors || SIZEOF(JSAMPLE) != SIZEOF(char)) {
+    /* When quantizing, we need an output buffer for colormap indexes
+     * that's separate from the physical I/O buffer.  We also need a
+     * separate buffer if JSAMPLE and char are not the same size.
+     */
+    dest->pub.buffer = (*cinfo->mem->alloc_sarray)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE,
+       cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
+    dest->pub.buffer_height = 1;
+    if (! cinfo->quantize_colors)
+      dest->pub.put_pixel_rows = copy_pixel_rows;
+    else if (cinfo->out_color_space == JCS_GRAYSCALE)
+      dest->pub.put_pixel_rows = put_demapped_gray;
+    else
+      dest->pub.put_pixel_rows = put_demapped_rgb;
+  } else {
+    /* We will fwrite() directly from decompressor output buffer. */
+    /* Synthesize a JSAMPARRAY pointer structure */
+    /* Cast here implies near->far pointer conversion on PCs */
+    dest->pixrow = (JSAMPROW) dest->iobuffer;
+    dest->pub.buffer = & dest->pixrow;
+    dest->pub.buffer_height = 1;
+    dest->pub.put_pixel_rows = put_pixel_rows;
+  }
+
+  return (djpeg_dest_ptr) dest;
+}
+
+#endif /* PPM_SUPPORTED */
diff --git a/wrrle.c b/wrrle.c
new file mode 100644
index 0000000..c0dae01
--- /dev/null
+++ b/wrrle.c
@@ -0,0 +1,302 @@
+/*
+ * wrrle.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in RLE format.
+ * The Utah Raster Toolkit library is required (version 3.1 or later).
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume output to
+ * an ordinary stdio stream.
+ *
+ * Based on code contributed by Mike Lijewski,
+ * with updates from Robert Hutchinson.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef RLE_SUPPORTED
+
+/* rle.h is provided by the Utah Raster Toolkit. */
+
+#include <rle.h>
+
+/*
+ * We assume that JSAMPLE has the same representation as rle_pixel,
+ * to wit, "unsigned char".  Hence we can't cope with 12- or 16-bit samples.
+ */
+
+#if BITS_IN_JSAMPLE != 8
+  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
+#endif
+
+
+/*
+ * Since RLE stores scanlines bottom-to-top, we have to invert the image
+ * from JPEG's top-to-bottom order.  To do this, we save the outgoing data
+ * in a virtual array during put_pixel_row calls, then actually emit the
+ * RLE file during finish_output.
+ */
+
+
+/*
+ * For now, if we emit an RLE color map then it is always 256 entries long,
+ * though not all of the entries need be used.
+ */
+
+#define CMAPBITS	8
+#define CMAPLENGTH	(1<<(CMAPBITS))
+
+typedef struct {
+  struct djpeg_dest_struct pub; /* public fields */
+
+  jvirt_sarray_ptr image;	/* virtual array to store the output image */
+  rle_map *colormap;	 	/* RLE-style color map, or NULL if none */
+  rle_pixel **rle_row;		/* To pass rows to rle_putrow() */
+
+} rle_dest_struct;
+
+typedef rle_dest_struct * rle_dest_ptr;
+
+/* Forward declarations */
+METHODDEF void rle_put_pixel_rows
+    JPP((j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+	 JDIMENSION rows_supplied));
+
+
+/*
+ * Write the file header.
+ *
+ * In this module it's easier to wait till finish_output to write anything.
+ */
+
+METHODDEF void
+start_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  rle_dest_ptr dest = (rle_dest_ptr) dinfo;
+  size_t cmapsize;
+  int i, ci;
+#ifdef PROGRESS_REPORT
+  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+#endif
+
+  /*
+   * Make sure the image can be stored in RLE format.
+   *
+   * - RLE stores image dimensions as *signed* 16 bit integers.  JPEG
+   *   uses unsigned, so we have to check the width.
+   *
+   * - Colorspace is expected to be grayscale or RGB.
+   *
+   * - The number of channels (components) is expected to be 1 (grayscale/
+   *   pseudocolor) or 3 (truecolor/directcolor).
+   *   (could be 2 or 4 if using an alpha channel, but we aren't)
+   */
+
+  if (cinfo->output_width > 32767 || cinfo->output_height > 32767)
+    ERREXIT2(cinfo, JERR_RLE_DIMENSIONS, cinfo->output_width, 
+	     cinfo->output_height);
+
+  if (cinfo->out_color_space != JCS_GRAYSCALE &&
+      cinfo->out_color_space != JCS_RGB)
+    ERREXIT(cinfo, JERR_RLE_COLORSPACE);
+
+  if (cinfo->output_components != 1 && cinfo->output_components != 3)
+    ERREXIT1(cinfo, JERR_RLE_TOOMANYCHANNELS, cinfo->num_components);
+
+  /* Convert colormap, if any, to RLE format. */
+
+  dest->colormap = NULL;
+
+  if (cinfo->quantize_colors) {
+    /* Allocate storage for RLE-style cmap, zero any extra entries */
+    cmapsize = cinfo->out_color_components * CMAPLENGTH * SIZEOF(rle_map);
+    dest->colormap = (rle_map *) (*cinfo->mem->alloc_small)
+      ((j_common_ptr) cinfo, JPOOL_IMAGE, cmapsize);
+    MEMZERO(dest->colormap, cmapsize);
+
+    /* Save away data in RLE format --- note 8-bit left shift! */
+    /* Shifting would need adjustment for JSAMPLEs wider than 8 bits. */
+    for (ci = 0; ci < cinfo->out_color_components; ci++) {
+      for (i = 0; i < cinfo->actual_number_of_colors; i++) {
+        dest->colormap[ci * CMAPLENGTH + i] =
+          GETJSAMPLE(cinfo->colormap[ci][i]) << 8;
+      }
+    }
+  }
+
+  /* Set the output buffer to the first row */
+  dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
+    ((j_common_ptr) cinfo, dest->image, (JDIMENSION) 0, TRUE);
+  dest->pub.buffer_height = 1;
+
+  dest->pub.put_pixel_rows = rle_put_pixel_rows;
+
+#ifdef PROGRESS_REPORT
+  if (progress != NULL) {
+    progress->total_extra_passes++;  /* count file writing as separate pass */
+  }
+#endif
+}
+
+
+/*
+ * Write some pixel data.
+ *
+ * This routine just saves the data away in a virtual array.
+ */
+
+METHODDEF void
+rle_put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+		    JDIMENSION rows_supplied)
+{
+  rle_dest_ptr dest = (rle_dest_ptr) dinfo;
+
+  if (cinfo->output_scanline < cinfo->output_height) {
+    dest->pub.buffer = (*cinfo->mem->access_virt_sarray)
+      ((j_common_ptr) cinfo, dest->image, cinfo->output_scanline, TRUE);
+  }
+}
+
+/*
+ * Finish up at the end of the file.
+ *
+ * Here is where we really output the RLE file.
+ */
+
+METHODDEF void
+finish_output_rle (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  rle_dest_ptr dest = (rle_dest_ptr) dinfo;
+  rle_hdr header;		/* Output file information */
+  rle_pixel **rle_row, *red, *green, *blue;
+  JSAMPROW output_row;
+  char cmapcomment[80];
+  int row, col;
+  int ci;
+#ifdef PROGRESS_REPORT
+  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
+#endif
+
+  /* Initialize the header info */
+  header = *rle_hdr_init(NULL);
+  header.rle_file = dest->pub.output_file;
+  header.xmin     = 0;
+  header.xmax     = cinfo->output_width  - 1;
+  header.ymin     = 0;
+  header.ymax     = cinfo->output_height - 1;
+  header.alpha    = 0;
+  header.ncolors  = cinfo->output_components;
+  for (ci = 0; ci < cinfo->output_components; ci++) {
+    RLE_SET_BIT(header, ci);
+  }
+  if (cinfo->quantize_colors) {
+    header.ncmap   = cinfo->out_color_components;
+    header.cmaplen = CMAPBITS;
+    header.cmap    = dest->colormap;
+    /* Add a comment to the output image with the true colormap length. */
+    sprintf(cmapcomment, "color_map_length=%d", cinfo->actual_number_of_colors);
+    rle_putcom(cmapcomment, &header);
+  }
+
+  /* Emit the RLE header and color map (if any) */
+  rle_put_setup(&header);
+
+  /* Now output the RLE data from our virtual array.
+   * We assume here that (a) rle_pixel is represented the same as JSAMPLE,
+   * and (b) we are not on a machine where FAR pointers differ from regular.
+   */
+
+#ifdef PROGRESS_REPORT
+  if (progress != NULL) {
+    progress->pub.pass_limit = cinfo->output_height;
+    progress->pub.pass_counter = 0;
+    (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+  }
+#endif
+
+  if (cinfo->output_components == 1) {
+    for (row = cinfo->output_height-1; row >= 0; row--) {
+      rle_row = (rle_pixel **) (*cinfo->mem->access_virt_sarray)
+        ((j_common_ptr) cinfo, dest->image, (JDIMENSION) row, FALSE);
+      rle_putrow(rle_row, (int) cinfo->output_width, &header);
+#ifdef PROGRESS_REPORT
+      if (progress != NULL) {
+        progress->pub.pass_counter++;
+        (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+      }
+#endif
+    }
+  } else {
+    for (row = cinfo->output_height-1; row >= 0; row--) {
+      rle_row = (rle_pixel **) dest->rle_row;
+      output_row = * (*cinfo->mem->access_virt_sarray)
+        ((j_common_ptr) cinfo, dest->image, (JDIMENSION) row, FALSE);
+      red = rle_row[0];
+      green = rle_row[1];
+      blue = rle_row[2];
+      for (col = cinfo->output_width; col > 0; col--) {
+        *red++ = GETJSAMPLE(*output_row++);
+        *green++ = GETJSAMPLE(*output_row++);
+        *blue++ = GETJSAMPLE(*output_row++);
+      }
+      rle_putrow(rle_row, (int) cinfo->output_width, &header);
+#ifdef PROGRESS_REPORT
+      if (progress != NULL) {
+        progress->pub.pass_counter++;
+        (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
+      }
+#endif
+    }
+  }
+
+#ifdef PROGRESS_REPORT
+  if (progress != NULL)
+    progress->completed_extra_passes++;
+#endif
+
+  /* Emit file trailer */
+  rle_puteof(&header);
+  fflush(dest->pub.output_file);
+  if (ferror(dest->pub.output_file))
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for RLE format output.
+ */
+
+GLOBAL djpeg_dest_ptr
+jinit_write_rle (j_decompress_ptr cinfo)
+{
+  rle_dest_ptr dest;
+
+  /* Create module interface object, fill in method pointers */
+  dest = (rle_dest_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+                                  SIZEOF(rle_dest_struct));
+  dest->pub.start_output = start_output_rle;
+  dest->pub.finish_output = finish_output_rle;
+
+  /* Calculate output image dimensions so we can allocate space */
+  jpeg_calc_output_dimensions(cinfo);
+
+  /* Allocate a work array for output to the RLE library. */
+  dest->rle_row = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     cinfo->output_width, (JDIMENSION) cinfo->output_components);
+
+  /* Allocate a virtual array to hold the image. */
+  dest->image = (*cinfo->mem->request_virt_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE,
+     (JDIMENSION) (cinfo->output_width * cinfo->output_components),
+     cinfo->output_height, (JDIMENSION) 1);
+
+  return (djpeg_dest_ptr) dest;
+}
+
+#endif /* RLE_SUPPORTED */
diff --git a/wrtarga.c b/wrtarga.c
new file mode 100644
index 0000000..3698712
--- /dev/null
+++ b/wrtarga.c
@@ -0,0 +1,253 @@
+/*
+ * wrtarga.c
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file contains routines to write output images in Targa format.
+ *
+ * These routines may need modification for non-Unix environments or
+ * specialized applications.  As they stand, they assume output to
+ * an ordinary stdio stream.
+ *
+ * Based on code contributed by Lee Daniel Crocker.
+ */
+
+#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
+
+#ifdef TARGA_SUPPORTED
+
+
+/*
+ * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
+ * This is not yet implemented.
+ */
+
+#if BITS_IN_JSAMPLE != 8
+  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
+#endif
+
+/*
+ * The output buffer needs to be writable by fwrite().  On PCs, we must
+ * allocate the buffer in near data space, because we are assuming small-data
+ * memory model, wherein fwrite() can't reach far memory.  If you need to
+ * process very wide images on a PC, you might have to compile in large-memory
+ * model, or else replace fwrite() with a putc() loop --- which will be much
+ * slower.
+ */
+
+
+/* Private version of data destination object */
+
+typedef struct {
+  struct djpeg_dest_struct pub;	/* public fields */
+
+  char *iobuffer;		/* physical I/O buffer */
+  JDIMENSION buffer_width;	/* width of one row */
+} tga_dest_struct;
+
+typedef tga_dest_struct * tga_dest_ptr;
+
+
+LOCAL void
+write_header (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
+/* Create and write a Targa header */
+{
+  char targaheader[18];
+
+  /* Set unused fields of header to 0 */
+  MEMZERO(targaheader, SIZEOF(targaheader));
+
+  if (num_colors > 0) {
+    targaheader[1] = 1;		/* color map type 1 */
+    targaheader[5] = (char) (num_colors & 0xFF);
+    targaheader[6] = (char) (num_colors >> 8);
+    targaheader[7] = 24;	/* 24 bits per cmap entry */
+  }
+
+  targaheader[12] = (char) (cinfo->output_width & 0xFF);
+  targaheader[13] = (char) (cinfo->output_width >> 8);
+  targaheader[14] = (char) (cinfo->output_height & 0xFF);
+  targaheader[15] = (char) (cinfo->output_height >> 8);
+  targaheader[17] = 0x20;	/* Top-down, non-interlaced */
+
+  if (cinfo->out_color_space == JCS_GRAYSCALE) {
+    targaheader[2] = 3;		/* image type = uncompressed gray-scale */
+    targaheader[16] = 8;	/* bits per pixel */
+  } else {			/* must be RGB */
+    if (num_colors > 0) {
+      targaheader[2] = 1;	/* image type = colormapped RGB */
+      targaheader[16] = 8;
+    } else {
+      targaheader[2] = 2;	/* image type = uncompressed RGB */
+      targaheader[16] = 24;
+    }
+  }
+
+  if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t) 18)
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * Write some pixel data.
+ * In this module rows_supplied will always be 1.
+ */
+
+METHODDEF void
+put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+		JDIMENSION rows_supplied)
+/* used for unquantized full-color output */
+{
+  tga_dest_ptr dest = (tga_dest_ptr) dinfo;
+  register JSAMPROW inptr;
+  register char * outptr;
+  register JDIMENSION col;
+
+  inptr = dest->pub.buffer[0];
+  outptr = dest->iobuffer;
+  for (col = cinfo->output_width; col > 0; col--) {
+    outptr[0] = (char) GETJSAMPLE(inptr[2]); /* RGB to BGR order */
+    outptr[1] = (char) GETJSAMPLE(inptr[1]);
+    outptr[2] = (char) GETJSAMPLE(inptr[0]);
+    inptr += 3, outptr += 3;
+  }
+  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+METHODDEF void
+put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+	       JDIMENSION rows_supplied)
+/* used for grayscale OR quantized color output */
+{
+  tga_dest_ptr dest = (tga_dest_ptr) dinfo;
+  register JSAMPROW inptr;
+  register char * outptr;
+  register JDIMENSION col;
+
+  inptr = dest->pub.buffer[0];
+  outptr = dest->iobuffer;
+  for (col = cinfo->output_width; col > 0; col--) {
+    *outptr++ = (char) GETJSAMPLE(*inptr++);
+  }
+  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * Write some demapped pixel data when color quantization is in effect.
+ * For Targa, this is only applied to grayscale data.
+ */
+
+METHODDEF void
+put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
+		   JDIMENSION rows_supplied)
+{
+  tga_dest_ptr dest = (tga_dest_ptr) dinfo;
+  register JSAMPROW inptr;
+  register char * outptr;
+  register JSAMPROW color_map0 = cinfo->colormap[0];
+  register JDIMENSION col;
+
+  inptr = dest->pub.buffer[0];
+  outptr = dest->iobuffer;
+  for (col = cinfo->output_width; col > 0; col--) {
+    *outptr++ = (char) GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
+  }
+  (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
+}
+
+
+/*
+ * Startup: write the file header.
+ */
+
+METHODDEF void
+start_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  tga_dest_ptr dest = (tga_dest_ptr) dinfo;
+  int num_colors, i;
+  FILE *outfile;
+
+  if (cinfo->out_color_space == JCS_GRAYSCALE) {
+    /* Targa doesn't have a mapped grayscale format, so we will */
+    /* demap quantized gray output.  Never emit a colormap. */
+    write_header(cinfo, dinfo, 0);
+    if (cinfo->quantize_colors)
+      dest->pub.put_pixel_rows = put_demapped_gray;
+    else
+      dest->pub.put_pixel_rows = put_gray_rows;
+  } else if (cinfo->out_color_space == JCS_RGB) {
+    if (cinfo->quantize_colors) {
+      /* We only support 8-bit colormap indexes, so only 256 colors */
+      num_colors = cinfo->actual_number_of_colors;
+      if (num_colors > 256)
+	ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
+      write_header(cinfo, dinfo, num_colors);
+      /* Write the colormap.  Note Targa uses BGR byte order */
+      outfile = dest->pub.output_file;
+      for (i = 0; i < num_colors; i++) {
+	putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
+	putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
+	putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
+      }
+      dest->pub.put_pixel_rows = put_gray_rows;
+    } else {
+      write_header(cinfo, dinfo, 0);
+      dest->pub.put_pixel_rows = put_pixel_rows;
+    }
+  } else {
+    ERREXIT(cinfo, JERR_TGA_COLORSPACE);
+  }
+}
+
+
+/*
+ * Finish up at the end of the file.
+ */
+
+METHODDEF void
+finish_output_tga (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
+{
+  /* Make sure we wrote the output file OK */
+  fflush(dinfo->output_file);
+  if (ferror(dinfo->output_file))
+    ERREXIT(cinfo, JERR_FILE_WRITE);
+}
+
+
+/*
+ * The module selection routine for Targa format output.
+ */
+
+GLOBAL djpeg_dest_ptr
+jinit_write_targa (j_decompress_ptr cinfo)
+{
+  tga_dest_ptr dest;
+
+  /* Create module interface object, fill in method pointers */
+  dest = (tga_dest_ptr)
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				  SIZEOF(tga_dest_struct));
+  dest->pub.start_output = start_output_tga;
+  dest->pub.finish_output = finish_output_tga;
+
+  /* Calculate output image dimensions so we can allocate space */
+  jpeg_calc_output_dimensions(cinfo);
+
+  /* Create I/O buffer.  Note we make this near on a PC. */
+  dest->buffer_width = cinfo->output_width * cinfo->output_components;
+  dest->iobuffer = (char *)
+    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+				(size_t) (dest->buffer_width * SIZEOF(char)));
+
+  /* Create decompressor output buffer. */
+  dest->pub.buffer = (*cinfo->mem->alloc_sarray)
+    ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION) 1);
+  dest->pub.buffer_height = 1;
+
+  return (djpeg_dest_ptr) dest;
+}
+
+#endif /* TARGA_SUPPORTED */