3rdparty: update libjpeg 9b
authorSuleyman TURKMEN <sturkmen@hotmail.com>
Sun, 22 Oct 2017 10:06:42 +0000 (13:06 +0300)
committerAlexander Alekhin <alexander.alekhin@intel.com>
Thu, 14 Dec 2017 11:49:31 +0000 (14:49 +0300)
55 files changed:
3rdparty/libjpeg/README
3rdparty/libjpeg/change.log
3rdparty/libjpeg/jcapimin.c
3rdparty/libjpeg/jcapistd.c
3rdparty/libjpeg/jcarith.c
3rdparty/libjpeg/jccoefct.c
3rdparty/libjpeg/jccolor.c
3rdparty/libjpeg/jcdctmgr.c
3rdparty/libjpeg/jchuff.c
3rdparty/libjpeg/jcinit.c
3rdparty/libjpeg/jcmainct.c
3rdparty/libjpeg/jcmarker.c
3rdparty/libjpeg/jcmaster.c
3rdparty/libjpeg/jconfig.h
3rdparty/libjpeg/jcparam.c
3rdparty/libjpeg/jcprepct.c
3rdparty/libjpeg/jcsample.c
3rdparty/libjpeg/jctrans.c
3rdparty/libjpeg/jdapimin.c
3rdparty/libjpeg/jdapistd.c
3rdparty/libjpeg/jdarith.c
3rdparty/libjpeg/jdatadst.c
3rdparty/libjpeg/jdatasrc.c
3rdparty/libjpeg/jdcoefct.c
3rdparty/libjpeg/jdcolor.c
3rdparty/libjpeg/jdct.h
3rdparty/libjpeg/jddctmgr.c
3rdparty/libjpeg/jdhuff.c
3rdparty/libjpeg/jdinput.c
3rdparty/libjpeg/jdmainct.c
3rdparty/libjpeg/jdmarker.c
3rdparty/libjpeg/jdmaster.c
3rdparty/libjpeg/jdmerge.c
3rdparty/libjpeg/jdpostct.c
3rdparty/libjpeg/jdsample.c
3rdparty/libjpeg/jdtrans.c
3rdparty/libjpeg/jerror.c
3rdparty/libjpeg/jerror.h
3rdparty/libjpeg/jfdctflt.c
3rdparty/libjpeg/jfdctfst.c
3rdparty/libjpeg/jfdctint.c
3rdparty/libjpeg/jidctflt.c
3rdparty/libjpeg/jidctfst.c
3rdparty/libjpeg/jidctint.c
3rdparty/libjpeg/jmemansi.c
3rdparty/libjpeg/jmemmgr.c
3rdparty/libjpeg/jmemnobs.c
3rdparty/libjpeg/jmemsys.h
3rdparty/libjpeg/jmorecfg.h
3rdparty/libjpeg/jpegint.h
3rdparty/libjpeg/jpeglib.h
3rdparty/libjpeg/jquant1.c
3rdparty/libjpeg/jquant2.c
3rdparty/libjpeg/jutils.c
3rdparty/libjpeg/jversion.h

index fc5ca5d..4c8e82e 100644 (file)
@@ -1,8 +1,8 @@
 The Independent JPEG Group's JPEG software
 ==========================================
 
-README for release 9 of 13-Jan-2013
-===================================
+README for release 9b of 17-Jan-2016
+====================================
 
 This distribution contains the ninth public release of the Independent JPEG
 Group's free JPEG software.  You are welcome to redistribute this software and
@@ -14,7 +14,7 @@ Julian Minguillon, Luis Ortiz, George Phillips, Davide Rossi, Ge' Weijers,
 and other members of the Independent JPEG Group.
 
 IJG is not affiliated with the ISO/IEC JTC1/SC29/WG1 standards committee
-(also known as JPEG, together with ITU-T SG16).
+(previously known as JPEG, together with ITU-T SG16).
 
 
 DOCUMENTATION ROADMAP
@@ -60,7 +60,7 @@ OVERVIEW
 
 This package contains C software to implement JPEG image encoding, decoding,
 and transcoding.  JPEG (pronounced "jay-peg") is a standardized compression
-method for full-color and gray-scale images.
+method for full-color and grayscale images.
 
 This software implements JPEG baseline, extended-sequential, and progressive
 compression processes.  Provision is made for supporting all variants of these
@@ -115,7 +115,7 @@ 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-2013, Thomas G. Lane, Guido Vollbeding.
+This software is copyright (C) 1991-2016, Thomas G. Lane, Guido Vollbeding.
 All Rights Reserved except as specified below.
 
 Permission is hereby granted to use, copy, modify, and distribute this
@@ -153,16 +153,11 @@ ltmain.sh).  Another support script, install-sh, is copyright by X Consortium
 but is also freely distributable.
 
 The IJG distribution formerly included code to read and write GIF files.
-To avoid entanglement with the Unisys LZW patent, GIF reading support has
-been removed altogether, and the GIF writer has been simplified to produce
-"uncompressed GIFs".  This technique does not use the LZW algorithm; the
-resulting GIF files are larger than usual, but are readable by all standard
-GIF decoders.
-
-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."
+To avoid entanglement with the Unisys LZW patent (now expired), GIF reading
+support has been removed altogether, and the GIF writer has been simplified
+to produce "uncompressed GIFs".  This technique does not use the LZW
+algorithm; the resulting GIF files are larger than usual, but are readable
+by all standard GIF decoders.
 
 
 REFERENCES
@@ -176,8 +171,8 @@ The best short technical introduction to the JPEG compression algorithm is
        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 http://www.ijg.org/files/wallace.ps.gz.  The file (actually
+handy, a PDF file containing a revised version of Wallace's article is
+available at http://www.ijg.org/files/Wallace.JPEG.pdf.  The file (actually
 a preprint for an article that appeared 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,
@@ -225,14 +220,13 @@ WG1 N 6080 with title "JPEG 9 Lossless Coding", June/July 2012, Paris,
 France.
 
 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.  JFIF 1.02 has been adopted as an Ecma International Technical Report
-and thus received a formal publication status.  It is available as a free
-download in PDF format from
-http://www.ecma-international.org/publications/techreports/E-TR-098.htm.
-A PostScript version of the JFIF document is available at
-http://www.ijg.org/files/jfif.ps.gz.  There is also a plain text version at
-http://www.ijg.org/files/jfif.txt.gz, but it is missing the figures.
+format.  For the omitted details we follow the "JFIF" conventions, version 2.
+JFIF version 1 has been adopted as Recommendation ITU-T T.871 (05/2011) :
+Information technology - Digital compression and coding of continuous-tone
+still images: JPEG File Interchange Format (JFIF).  It is available as a
+free download in PDF file format from http://www.itu.int/rec/T-REC-T.871.
+A PDF file of the older JFIF document is available at
+http://www.w3.org/Graphics/JPEG/jfif3.pdf.
 
 The TIFF 6.0 file format specification can be obtained by FTP from
 ftp://ftp.sgi.com/graphics/tiff/TIFF6.ps.gz.  The JPEG incorporation scheme
@@ -252,8 +246,8 @@ ARCHIVE LOCATIONS
 The "official" archive site for this software is www.ijg.org.
 The most recent released version can always be found there in
 directory "files".  This particular version will be archived as
-http://www.ijg.org/files/jpegsrc.v9.tar.gz, and in Windows-compatible
-"zip" archive format as http://www.ijg.org/files/jpegsr9.zip.
+http://www.ijg.org/files/jpegsrc.v9b.tar.gz, and in Windows-compatible
+"zip" archive format as http://www.ijg.org/files/jpegsr9b.zip.
 
 The JPEG FAQ (Frequently Asked Questions) article is a source of some
 general information about JPEG.
@@ -280,7 +274,7 @@ Thank to Thomas Wiegand and Gary Sullivan for inviting me to the
 Joint Video Team (MPEG & ITU) meeting in Geneva, Switzerland.
 
 Thank to Thomas Richter and Daniel Lee for inviting me to the
-ISO/IEC JTC1/SC29/WG1 (also known as JPEG, together with ITU-T SG16)
+ISO/IEC JTC1/SC29/WG1 (previously known as JPEG, together with ITU-T SG16)
 meeting in Berlin, Germany.
 
 Thank to John Korejwa and Massimo Ballerini for inviting me to
@@ -306,10 +300,10 @@ design and development of this singular software package.
 FILE FORMAT WARS
 ================
 
-The ISO/IEC JTC1/SC29/WG1 standards committee (also known as JPEG, together
-with ITU-T SG16) currently promotes different formats containing the name
-"JPEG" which is misleading because these formats are incompatible with
-original DCT-based JPEG and are based on faulty technologies.
+The ISO/IEC JTC1/SC29/WG1 standards committee (previously known as JPEG,
+together with ITU-T SG16) currently promotes different formats containing
+the name "JPEG" which is misleading because these formats are incompatible
+with original DCT-based JPEG and are based on faulty technologies.
 IJG therefore does not and will not support such momentary mistakes
 (see REFERENCES).
 There exist also distributions under the name "OpenJPEG" promoting such
@@ -322,9 +316,13 @@ Don't use an incompatible file format!
 (In any case, our decoder will remain capable of reading existing JPEG
 image files indefinitely.)
 
-Furthermore, the ISO committee pretends to be "responsible for the popular
-JPEG" in their public reports which is not true because they don't respond to
-actual requirements for the maintenance of the original JPEG specification.
+The ISO committee pretends to be "responsible for the popular JPEG" in their
+public reports which is not true because they don't respond to actual
+requirements for the maintenance of the original JPEG specification.
+Furthermore, the ISO committee pretends to "ensure interoperability" with
+their standards which is not true because their "standards" support only
+application-specific and proprietary use cases and contain mathematically
+incorrect code.
 
 There are currently different distributions in circulation containing the
 name "libjpeg" which is misleading because they don't have the features and
@@ -332,19 +330,46 @@ are incompatible with formats supported by actual IJG libjpeg distributions.
 One of those fakes is released by members of the ISO committee and just uses
 the name of libjpeg for misdirection of people, similar to the abuse of the
 name JPEG as described above, while having nothing in common with actual IJG
-libjpeg distributions.
-The other one claims to be a "derivative" or "fork" of the original libjpeg
-and violates the license conditions as described under LEGAL ISSUES above.
-We have no sympathy for the release of misleading and illegal distributions
-derived from obsolete code bases.
+libjpeg distributions and containing mathematically incorrect code.
+The other one claims to be a "derivative" or "fork" of the original libjpeg,
+but violates the license conditions as described under LEGAL ISSUES above
+and violates basic C programming properties.
+We have no sympathy for the release of misleading, incorrect and illegal
+distributions derived from obsolete code bases.
 Don't use an obsolete code base!
 
+According to the UCC (Uniform Commercial Code) law, IJG has the lawful and
+legal right to foreclose on certain standardization bodies and other
+institutions or corporations that knowingly perform substantial and
+systematic deceptive acts and practices, fraud, theft, and damaging of the
+value of the people of this planet without their knowing, willing and
+intentional consent.
+The titles, ownership, and rights of these institutions and all their assets
+are now duly secured and held in trust for the free people of this planet.
+People of the planet, on every country, may have a financial interest in
+the assets of these former principals, agents, and beneficiaries of the
+foreclosed institutions and corporations.
+IJG asserts what is: that each man, woman, and child has unalienable value
+and rights granted and deposited in them by the Creator and not any one of
+the people is subordinate to any artificial principality, corporate fiction
+or the special interest of another without their appropriate knowing,
+willing and intentional consent made by contract or accommodation agreement.
+IJG expresses that which already was.
+The people have already determined and demanded that public administration
+entities, national governments, and their supporting judicial systems must
+be fully transparent, accountable, and liable.
+IJG has secured the value for all concerned free people of the planet.
+
+A partial list of foreclosed institutions and corporations ("Hall of Shame")
+is currently prepared and will be published later.
+
 
 TO DO
 =====
 
 Version 9 is the second release of a new generation JPEG standard
-to overcome the limitations of the original JPEG specification.
+to overcome the limitations of the original JPEG specification,
+and is the first true source reference JPEG codec.
 More features are being prepared for coming releases...
 
 Please send bug reports, offers of help, etc. to jpeg-info@jpegclub.org.
index a1d94ff..b8439f4 100644 (file)
@@ -1,6 +1,56 @@
 CHANGE LOG for Independent JPEG Group's JPEG software
 
 
+Version 9b  17-Jan-2016
+-----------------------
+
+Improvements and optimizations in DCT and color calculations.
+Normalize range limit array composition and access pattern.
+Thank to Sia Furler and Maddie Ziegler for inspiration.
+
+Use merged upsample with scaled DCT sizes larger than 8.
+Thank to Taylor Hatala for inspiration.
+
+Check for excessive comment lengths in argument parsing in wrjpgcom.c.
+Thank to Julian Cohen for hint.
+
+Add makefile.b32 for use with Borland C++ 32-bit (bcc32).
+Thank to Joe Slater for contribution.
+
+Document 'f' specifier for jpegtran -crop specification.
+Thank to Michele Martone for suggestion.
+
+Use defined value from header instead of hardwired number in rdswitch.c.
+Thank to Robert Sprowson for hint.
+
+
+Version 9a  19-Jan-2014
+-----------------------
+
+Add support for wide gamut color spaces (JFIF version 2).
+Improve clarity and accuracy in color conversion modules.
+Note: Requires rebuild of test images.
+
+Extend the bit depth support to all values from 8 to 12
+(BITS_IN_JSAMPLE configuration option in jmorecfg.h).
+jpegtran now supports N bits sample data precision with all N from 8 to 12
+in a single instance.  Thank to Roland Fassauer for inspiration.
+
+Try to resolve issues with new boolean type definition.
+Thank also to v4hn for suggestion.
+
+Enable option to use default Huffman tables for lossless compression
+(for hardware solution), and in this case improve lossless RGB compression
+with reversible color transform.  Thank to Benny Alexandar for hint.
+
+Extend the entropy decoding structure, so that extraneous bytes between
+compressed scan data and following marker can be reported correctly.
+Thank to Nigel Tao for hint.
+
+Add jpegtran -wipe option and extension for -crop.
+Thank to Andrew Senior, David Clunie, and Josef Schmid for suggestion.
+
+
 Version 9  13-Jan-2013
 ----------------------
 
index c4e637b..639ce86 100644 (file)
@@ -37,8 +37,8 @@ jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
   if (version != JPEG_LIB_VERSION)
     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
   if (structsize != SIZEOF(struct jpeg_compress_struct))
-    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
-             (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
+    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
+            (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
 
   /* For debugging purposes, we zero the whole master structure.
    * But the application has already set the err pointer, and may have set
@@ -169,15 +169,15 @@ jpeg_finish_compress (j_compress_ptr cinfo)
     (*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);
+       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.
        */
       if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
-        ERREXIT(cinfo, JERR_CANT_SUSPEND);
+       ERREXIT(cinfo, JERR_CANT_SUSPEND);
     }
     (*cinfo->master->finish_pass) (cinfo);
   }
@@ -198,7 +198,7 @@ jpeg_finish_compress (j_compress_ptr cinfo)
 
 GLOBAL(void)
 jpeg_write_marker (j_compress_ptr cinfo, int marker,
-                   const JOCTET *dataptr, unsigned int datalen)
+                  const JOCTET *dataptr, unsigned int datalen)
 {
   JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
 
index 34a2ea4..0917afa 100644 (file)
@@ -2,6 +2,7 @@
  * jcapistd.c
  *
  * Copyright (C) 1994-1996, Thomas G. Lane.
+ * Modified 2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -75,7 +76,7 @@ jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
 
 GLOBAL(JDIMENSION)
 jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
-                      JDIMENSION num_lines)
+                     JDIMENSION num_lines)
 {
   JDIMENSION row_ctr, rows_left;
 
@@ -118,7 +119,7 @@ jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
 
 GLOBAL(JDIMENSION)
 jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
-                     JDIMENSION num_lines)
+                    JDIMENSION num_lines)
 {
   JDIMENSION lines_per_iMCU_row;
 
@@ -145,7 +146,7 @@ jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
     (*cinfo->master->pass_startup) (cinfo);
 
   /* Verify that at least one iMCU row has been passed. */
-  lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
+  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size;
   if (num_lines < lines_per_iMCU_row)
     ERREXIT(cinfo, JERR_BUFFER_SIZE);
 
index d68b670..a64190e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * jcarith.c
  *
- * Developed 1997-2012 by Guido Vollbeding.
+ * Developed 1997-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -103,9 +103,9 @@ typedef arith_entropy_encoder * arith_entropy_ptr;
 #ifdef RIGHT_SHIFT_IS_UNSIGNED
 #define ISHIFT_TEMPS   int ishift_temp;
 #define IRIGHT_SHIFT(x,shft)  \
-        ((ishift_temp = (x)) < 0 ? \
-         (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
-         (ishift_temp >> (shft)))
+       ((ishift_temp = (x)) < 0 ? \
+        (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
+        (ishift_temp >> (shft)))
 #else
 #define ISHIFT_TEMPS
 #define IRIGHT_SHIFT(x,shft)   ((x) >> (shft))
@@ -149,11 +149,11 @@ finish_pass (j_compress_ptr cinfo)
     /* One final overflow has to be handled */
     if (e->buffer >= 0) {
       if (e->zc)
-        do emit_byte(0x00, cinfo);
-        while (--e->zc);
+       do emit_byte(0x00, cinfo);
+       while (--e->zc);
       emit_byte(e->buffer + 1, cinfo);
       if (e->buffer + 1 == 0xFF)
-        emit_byte(0x00, cinfo);
+       emit_byte(0x00, cinfo);
     }
     e->zc += e->sc;  /* carry-over converts stacked 0xFF bytes to 0x00 */
     e->sc = 0;
@@ -162,17 +162,17 @@ finish_pass (j_compress_ptr cinfo)
       ++e->zc;
     else if (e->buffer >= 0) {
       if (e->zc)
-        do emit_byte(0x00, cinfo);
-        while (--e->zc);
+       do emit_byte(0x00, cinfo);
+       while (--e->zc);
       emit_byte(e->buffer, cinfo);
     }
     if (e->sc) {
       if (e->zc)
-        do emit_byte(0x00, cinfo);
-        while (--e->zc);
+       do emit_byte(0x00, cinfo);
+       while (--e->zc);
       do {
-        emit_byte(0xFF, cinfo);
-        emit_byte(0x00, cinfo);
+       emit_byte(0xFF, cinfo);
+       emit_byte(0x00, cinfo);
       } while (--e->sc);
     }
   }
@@ -187,7 +187,7 @@ finish_pass (j_compress_ptr cinfo)
     if (e->c & 0x7F800L) {
       emit_byte((e->c >> 11) & 0xFF, cinfo);
       if (((e->c >> 11) & 0xFF) == 0xFF)
-        emit_byte(0x00, cinfo);
+       emit_byte(0x00, cinfo);
     }
   }
 }
@@ -216,7 +216,7 @@ finish_pass (j_compress_ptr cinfo)
  */
 
 LOCAL(void)
-arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
+arith_encode (j_compress_ptr cinfo, unsigned char *st, int val) 
 {
   register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
   register unsigned char nl, nm;
@@ -266,43 +266,43 @@ arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
       /* Another byte is ready for output */
       temp = e->c >> 19;
       if (temp > 0xFF) {
-        /* Handle overflow over all stacked 0xFF bytes */
-        if (e->buffer >= 0) {
-          if (e->zc)
-            do emit_byte(0x00, cinfo);
-            while (--e->zc);
-          emit_byte(e->buffer + 1, cinfo);
-          if (e->buffer + 1 == 0xFF)
-            emit_byte(0x00, cinfo);
-        }
-        e->zc += e->sc;  /* carry-over converts stacked 0xFF bytes to 0x00 */
-        e->sc = 0;
-        /* Note: The 3 spacer bits in the C register guarantee
-         * that the new buffer byte can't be 0xFF here
-         * (see page 160 in the P&M JPEG book). */
-        e->buffer = temp & 0xFF;  /* new output byte, might overflow later */
+       /* Handle overflow over all stacked 0xFF bytes */
+       if (e->buffer >= 0) {
+         if (e->zc)
+           do emit_byte(0x00, cinfo);
+           while (--e->zc);
+         emit_byte(e->buffer + 1, cinfo);
+         if (e->buffer + 1 == 0xFF)
+           emit_byte(0x00, cinfo);
+       }
+       e->zc += e->sc;  /* carry-over converts stacked 0xFF bytes to 0x00 */
+       e->sc = 0;
+       /* Note: The 3 spacer bits in the C register guarantee
+        * that the new buffer byte can't be 0xFF here
+        * (see page 160 in the P&M JPEG book). */
+       e->buffer = temp & 0xFF;  /* new output byte, might overflow later */
       } else if (temp == 0xFF) {
-        ++e->sc;  /* stack 0xFF byte (which might overflow later) */
+       ++e->sc;  /* stack 0xFF byte (which might overflow later) */
       } else {
-        /* Output all stacked 0xFF bytes, they will not overflow any more */
-        if (e->buffer == 0)
-          ++e->zc;
-        else if (e->buffer >= 0) {
-          if (e->zc)
-            do emit_byte(0x00, cinfo);
-            while (--e->zc);
-          emit_byte(e->buffer, cinfo);
-        }
-        if (e->sc) {
-          if (e->zc)
-            do emit_byte(0x00, cinfo);
-            while (--e->zc);
-          do {
-            emit_byte(0xFF, cinfo);
-            emit_byte(0x00, cinfo);
-          } while (--e->sc);
-        }
-        e->buffer = temp & 0xFF;  /* new output byte (can still overflow) */
+       /* Output all stacked 0xFF bytes, they will not overflow any more */
+       if (e->buffer == 0)
+         ++e->zc;
+       else if (e->buffer >= 0) {
+         if (e->zc)
+           do emit_byte(0x00, cinfo);
+           while (--e->zc);
+         emit_byte(e->buffer, cinfo);
+       }
+       if (e->sc) {
+         if (e->zc)
+           do emit_byte(0x00, cinfo);
+           while (--e->zc);
+         do {
+           emit_byte(0xFF, cinfo);
+           emit_byte(0x00, cinfo);
+         } while (--e->sc);
+       }
+       e->buffer = temp & 0xFF;  /* new output byte (can still overflow) */
       }
       e->c &= 0x7FFFFL;
       e->ct += 8;
@@ -362,7 +362,6 @@ METHODDEF(boolean)
 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 {
   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
-  JBLOCKROW block;
   unsigned char *st;
   int blkn, ci, tbl;
   int v, v2, m;
@@ -381,14 +380,13 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
   /* Encode the MCU data blocks */
   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
-    block = MCU_data[blkn];
     ci = cinfo->MCU_membership[blkn];
     tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
 
     /* Compute the DC value after the required point transform by Al.
      * This is simply an arithmetic right shift.
      */
-    m = IRIGHT_SHIFT((int) ((*block)[0]), cinfo->Al);
+    m = IRIGHT_SHIFT((int) (MCU_data[blkn][0][0]), cinfo->Al);
 
     /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */
 
@@ -405,38 +403,38 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
       /* Figure F.6: Encoding nonzero value v */
       /* Figure F.7: Encoding the sign of v */
       if (v > 0) {
-        arith_encode(cinfo, st + 1, 0);        /* Table F.4: SS = S0 + 1 */
-        st += 2;                       /* Table F.4: SP = S0 + 2 */
-        entropy->dc_context[ci] = 4;   /* small positive diff category */
+       arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */
+       st += 2;                        /* Table F.4: SP = S0 + 2 */
+       entropy->dc_context[ci] = 4;    /* small positive diff category */
       } else {
-        v = -v;
-        arith_encode(cinfo, st + 1, 1);        /* Table F.4: SS = S0 + 1 */
-        st += 3;                       /* Table F.4: SN = S0 + 3 */
-        entropy->dc_context[ci] = 8;   /* small negative diff category */
+       v = -v;
+       arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */
+       st += 3;                        /* Table F.4: SN = S0 + 3 */
+       entropy->dc_context[ci] = 8;    /* small negative diff category */
       }
       /* Figure F.8: Encoding the magnitude category of v */
       m = 0;
       if (v -= 1) {
-        arith_encode(cinfo, st, 1);
-        m = 1;
-        v2 = v;
-        st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
-        while (v2 >>= 1) {
-          arith_encode(cinfo, st, 1);
-          m <<= 1;
-          st += 1;
-        }
+       arith_encode(cinfo, st, 1);
+       m = 1;
+       v2 = v;
+       st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
+       while (v2 >>= 1) {
+         arith_encode(cinfo, st, 1);
+         m <<= 1;
+         st += 1;
+       }
       }
       arith_encode(cinfo, st, 0);
       /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
       if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
-        entropy->dc_context[ci] = 0;   /* zero diff category */
+       entropy->dc_context[ci] = 0;    /* zero diff category */
       else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
-        entropy->dc_context[ci] += 8;  /* large diff category */
+       entropy->dc_context[ci] += 8;   /* large diff category */
       /* Figure F.9: Encoding the magnitude bit pattern of v */
       st += 14;
       while (m >>= 1)
-        arith_encode(cinfo, st, (m & v) ? 1 : 0);
+       arith_encode(cinfo, st, (m & v) ? 1 : 0);
     }
   }
 
@@ -453,11 +451,11 @@ METHODDEF(boolean)
 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 {
   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
+  const int * natural_order;
   JBLOCKROW block;
   unsigned char *st;
   int tbl, k, ke;
   int v, v2, m;
-  const int * natural_order;
 
   /* Emit restart marker if needed */
   if (cinfo->restart_interval) {
@@ -499,18 +497,18 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
     arith_encode(cinfo, st, 0);                /* EOB decision */
     for (;;) {
       if ((v = (*block)[natural_order[++k]]) >= 0) {
-        if (v >>= cinfo->Al) {
-          arith_encode(cinfo, st + 1, 1);
-          arith_encode(cinfo, entropy->fixed_bin, 0);
-          break;
-        }
+       if (v >>= cinfo->Al) {
+         arith_encode(cinfo, st + 1, 1);
+         arith_encode(cinfo, entropy->fixed_bin, 0);
+         break;
+       }
       } else {
-        v = -v;
-        if (v >>= cinfo->Al) {
-          arith_encode(cinfo, st + 1, 1);
-          arith_encode(cinfo, entropy->fixed_bin, 1);
-          break;
-        }
+       v = -v;
+       if (v >>= cinfo->Al) {
+         arith_encode(cinfo, st + 1, 1);
+         arith_encode(cinfo, entropy->fixed_bin, 1);
+         break;
+       }
       }
       arith_encode(cinfo, st + 1, 0);
       st += 3;
@@ -523,15 +521,15 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
       m = 1;
       v2 = v;
       if (v2 >>= 1) {
-        arith_encode(cinfo, st, 1);
-        m <<= 1;
-        st = entropy->ac_stats[tbl] +
-             (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
-        while (v2 >>= 1) {
-          arith_encode(cinfo, st, 1);
-          m <<= 1;
-          st += 1;
-        }
+       arith_encode(cinfo, st, 1);
+       m <<= 1;
+       st = entropy->ac_stats[tbl] +
+            (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
+       while (v2 >>= 1) {
+         arith_encode(cinfo, st, 1);
+         m <<= 1;
+         st += 1;
+       }
       }
     }
     arith_encode(cinfo, st, 0);
@@ -552,6 +550,8 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
 /*
  * MCU encoding for DC successive approximation refinement scan.
+ * Note: we assume such scans can be multi-component,
+ * although the spec is not very clear on the point.
  */
 
 METHODDEF(boolean)
@@ -593,11 +593,11 @@ METHODDEF(boolean)
 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 {
   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
+  const int * natural_order;
   JBLOCKROW block;
   unsigned char *st;
   int tbl, k, ke, kex;
   int v;
-  const int * natural_order;
 
   /* Emit restart marker if needed */
   if (cinfo->restart_interval) {
@@ -649,26 +649,26 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
       arith_encode(cinfo, st, 0);      /* EOB decision */
     for (;;) {
       if ((v = (*block)[natural_order[++k]]) >= 0) {
-        if (v >>= cinfo->Al) {
-          if (v >> 1)                  /* previously nonzero coef */
-            arith_encode(cinfo, st + 2, (v & 1));
-          else {                       /* newly nonzero coef */
-            arith_encode(cinfo, st + 1, 1);
-            arith_encode(cinfo, entropy->fixed_bin, 0);
-          }
-          break;
-        }
+       if (v >>= cinfo->Al) {
+         if (v >> 1)                   /* previously nonzero coef */
+           arith_encode(cinfo, st + 2, (v & 1));
+         else {                        /* newly nonzero coef */
+           arith_encode(cinfo, st + 1, 1);
+           arith_encode(cinfo, entropy->fixed_bin, 0);
+         }
+         break;
+       }
       } else {
-        v = -v;
-        if (v >>= cinfo->Al) {
-          if (v >> 1)                  /* previously nonzero coef */
-            arith_encode(cinfo, st + 2, (v & 1));
-          else {                       /* newly nonzero coef */
-            arith_encode(cinfo, st + 1, 1);
-            arith_encode(cinfo, entropy->fixed_bin, 1);
-          }
-          break;
-        }
+       v = -v;
+       if (v >>= cinfo->Al) {
+         if (v >> 1)                   /* previously nonzero coef */
+           arith_encode(cinfo, st + 2, (v & 1));
+         else {                        /* newly nonzero coef */
+           arith_encode(cinfo, st + 1, 1);
+           arith_encode(cinfo, entropy->fixed_bin, 1);
+         }
+         break;
+       }
       }
       arith_encode(cinfo, st + 1, 0);
       st += 3;
@@ -692,12 +692,13 @@ METHODDEF(boolean)
 encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 {
   arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
-  jpeg_component_info * compptr;
+  const int * natural_order;
   JBLOCKROW block;
   unsigned char *st;
-  int blkn, ci, tbl, k, ke;
+  int tbl, k, ke;
   int v, v2, m;
-  const int * natural_order;
+  int blkn, ci;
+  jpeg_component_info * compptr;
 
   /* Emit restart marker if needed */
   if (cinfo->restart_interval) {
@@ -735,38 +736,38 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
       /* Figure F.6: Encoding nonzero value v */
       /* Figure F.7: Encoding the sign of v */
       if (v > 0) {
-        arith_encode(cinfo, st + 1, 0);        /* Table F.4: SS = S0 + 1 */
-        st += 2;                       /* Table F.4: SP = S0 + 2 */
-        entropy->dc_context[ci] = 4;   /* small positive diff category */
+       arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */
+       st += 2;                        /* Table F.4: SP = S0 + 2 */
+       entropy->dc_context[ci] = 4;    /* small positive diff category */
       } else {
-        v = -v;
-        arith_encode(cinfo, st + 1, 1);        /* Table F.4: SS = S0 + 1 */
-        st += 3;                       /* Table F.4: SN = S0 + 3 */
-        entropy->dc_context[ci] = 8;   /* small negative diff category */
+       v = -v;
+       arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */
+       st += 3;                        /* Table F.4: SN = S0 + 3 */
+       entropy->dc_context[ci] = 8;    /* small negative diff category */
       }
       /* Figure F.8: Encoding the magnitude category of v */
       m = 0;
       if (v -= 1) {
-        arith_encode(cinfo, st, 1);
-        m = 1;
-        v2 = v;
-        st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
-        while (v2 >>= 1) {
-          arith_encode(cinfo, st, 1);
-          m <<= 1;
-          st += 1;
-        }
+       arith_encode(cinfo, st, 1);
+       m = 1;
+       v2 = v;
+       st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
+       while (v2 >>= 1) {
+         arith_encode(cinfo, st, 1);
+         m <<= 1;
+         st += 1;
+       }
       }
       arith_encode(cinfo, st, 0);
       /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
       if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
-        entropy->dc_context[ci] = 0;   /* zero diff category */
+       entropy->dc_context[ci] = 0;    /* zero diff category */
       else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
-        entropy->dc_context[ci] += 8;  /* large diff category */
+       entropy->dc_context[ci] += 8;   /* large diff category */
       /* Figure F.9: Encoding the magnitude bit pattern of v */
       st += 14;
       while (m >>= 1)
-        arith_encode(cinfo, st, (m & v) ? 1 : 0);
+       arith_encode(cinfo, st, (m & v) ? 1 : 0);
     }
 
     /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
@@ -784,42 +785,42 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
       st = entropy->ac_stats[tbl] + 3 * k;
       arith_encode(cinfo, st, 0);      /* EOB decision */
       while ((v = (*block)[natural_order[++k]]) == 0) {
-        arith_encode(cinfo, st + 1, 0);
-        st += 3;
+       arith_encode(cinfo, st + 1, 0);
+       st += 3;
       }
       arith_encode(cinfo, st + 1, 1);
       /* Figure F.6: Encoding nonzero value v */
       /* Figure F.7: Encoding the sign of v */
       if (v > 0) {
-        arith_encode(cinfo, entropy->fixed_bin, 0);
+       arith_encode(cinfo, entropy->fixed_bin, 0);
       } else {
-        v = -v;
-        arith_encode(cinfo, entropy->fixed_bin, 1);
+       v = -v;
+       arith_encode(cinfo, entropy->fixed_bin, 1);
       }
       st += 2;
       /* Figure F.8: Encoding the magnitude category of v */
       m = 0;
       if (v -= 1) {
-        arith_encode(cinfo, st, 1);
-        m = 1;
-        v2 = v;
-        if (v2 >>= 1) {
-          arith_encode(cinfo, st, 1);
-          m <<= 1;
-          st = entropy->ac_stats[tbl] +
-               (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
-          while (v2 >>= 1) {
-            arith_encode(cinfo, st, 1);
-            m <<= 1;
-            st += 1;
-          }
-        }
+       arith_encode(cinfo, st, 1);
+       m = 1;
+       v2 = v;
+       if (v2 >>= 1) {
+         arith_encode(cinfo, st, 1);
+         m <<= 1;
+         st = entropy->ac_stats[tbl] +
+              (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
+         while (v2 >>= 1) {
+           arith_encode(cinfo, st, 1);
+           m <<= 1;
+           st += 1;
+         }
+       }
       }
       arith_encode(cinfo, st, 0);
       /* Figure F.9: Encoding the magnitude bit pattern of v */
       st += 14;
       while (m >>= 1)
-        arith_encode(cinfo, st, (m & v) ? 1 : 0);
+       arith_encode(cinfo, st, (m & v) ? 1 : 0);
     }
     /* Encode EOB decision only if k < cinfo->lim_Se */
     if (k < cinfo->lim_Se) {
@@ -856,14 +857,14 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
   if (cinfo->progressive_mode) {
     if (cinfo->Ah == 0) {
       if (cinfo->Ss == 0)
-        entropy->pub.encode_mcu = encode_mcu_DC_first;
+       entropy->pub.encode_mcu = encode_mcu_DC_first;
       else
-        entropy->pub.encode_mcu = encode_mcu_AC_first;
+       entropy->pub.encode_mcu = encode_mcu_AC_first;
     } else {
       if (cinfo->Ss == 0)
-        entropy->pub.encode_mcu = encode_mcu_DC_refine;
+       entropy->pub.encode_mcu = encode_mcu_DC_refine;
       else
-        entropy->pub.encode_mcu = encode_mcu_AC_refine;
+       entropy->pub.encode_mcu = encode_mcu_AC_refine;
     }
   } else
     entropy->pub.encode_mcu = encode_mcu;
@@ -875,10 +876,10 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
     if (cinfo->Ss == 0 && cinfo->Ah == 0) {
       tbl = compptr->dc_tbl_no;
       if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
-        ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
+       ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
       if (entropy->dc_stats[tbl] == NULL)
-        entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
-          ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
+       entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
+         ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
       MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
       /* Initialize DC predictions to 0 */
       entropy->last_dc_val[ci] = 0;
@@ -888,15 +889,15 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
     if (cinfo->Se) {
       tbl = compptr->ac_tbl_no;
       if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
-        ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
+       ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
       if (entropy->ac_stats[tbl] == NULL)
-        entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
-          ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
+       entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
+         ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
       MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
 #ifdef CALCULATE_SPECTRAL_CONDITIONING
       if (cinfo->progressive_mode)
-        /* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */
-        cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4);
+       /* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */
+       cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4);
 #endif
     }
   }
@@ -927,7 +928,7 @@ jinit_arith_encoder (j_compress_ptr cinfo)
 
   entropy = (arith_entropy_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(arith_entropy_encoder));
+                               SIZEOF(arith_entropy_encoder));
   cinfo->entropy = &entropy->pub;
   entropy->pub.start_pass = start_pass;
   entropy->pub.finish_pass = finish_pass;
index 2f0a489..924a703 100644 (file)
@@ -156,7 +156,7 @@ compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
        yoffset++) {
     for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
-         MCU_col_num++) {
+        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
@@ -168,48 +168,48 @@ compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
        */
       blkn = 0;
       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
-        compptr = cinfo->cur_comp_info[ci];
-        forward_DCT = cinfo->fdct->forward_DCT[compptr->component_index];
-        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
-                                                : compptr->last_col_width;
-        xpos = MCU_col_num * compptr->MCU_sample_width;
-        ypos = yoffset * compptr->DCT_v_scaled_size;
-        /* ypos == (yoffset+yindex) * DCTSIZE */
-        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
-          if (coef->iMCU_row_num < last_iMCU_row ||
-              yoffset+yindex < compptr->last_row_height) {
-            (*forward_DCT) (cinfo, compptr,
-                            input_buf[compptr->component_index],
-                            coef->MCU_buffer[blkn],
-                            ypos, xpos, (JDIMENSION) blockcnt);
-            if (blockcnt < compptr->MCU_width) {
-              /* Create some dummy blocks at the right edge of the image. */
-              FMEMZERO((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 row of dummy blocks at the bottom of the image. */
-            FMEMZERO((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 += compptr->DCT_v_scaled_size;
-        }
+       compptr = cinfo->cur_comp_info[ci];
+       forward_DCT = cinfo->fdct->forward_DCT[compptr->component_index];
+       blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+                                               : compptr->last_col_width;
+       xpos = MCU_col_num * compptr->MCU_sample_width;
+       ypos = yoffset * compptr->DCT_v_scaled_size;
+       /* ypos == (yoffset+yindex) * DCTSIZE */
+       for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+         if (coef->iMCU_row_num < last_iMCU_row ||
+             yoffset+yindex < compptr->last_row_height) {
+           (*forward_DCT) (cinfo, compptr,
+                           input_buf[compptr->component_index],
+                           coef->MCU_buffer[blkn],
+                           ypos, xpos, (JDIMENSION) blockcnt);
+           if (blockcnt < compptr->MCU_width) {
+             /* Create some dummy blocks at the right edge of the image. */
+             FMEMZERO((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 row of dummy blocks at the bottom of the image. */
+           FMEMZERO((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 += compptr->DCT_v_scaled_size;
+       }
       }
       /* 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)) {
-        /* Suspension forced; update state counters and exit */
-        coef->MCU_vert_offset = yoffset;
-        coef->mcu_ctr = MCU_col_num;
-        return FALSE;
+       /* Suspension forced; update state counters and exit */
+       coef->MCU_vert_offset = yoffset;
+       coef->mcu_ctr = MCU_col_num;
+       return FALSE;
       }
     }
     /* Completed an MCU row, but perhaps not an iMCU row */
@@ -286,16 +286,16 @@ compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
     for (block_row = 0; block_row < block_rows; block_row++) {
       thisblockrow = buffer[block_row];
       (*forward_DCT) (cinfo, compptr, input_buf[ci], thisblockrow,
-                      (JDIMENSION) (block_row * compptr->DCT_v_scaled_size),
-                      (JDIMENSION) 0, blocks_across);
+                     (JDIMENSION) (block_row * compptr->DCT_v_scaled_size),
+                     (JDIMENSION) 0, blocks_across);
       if (ndummy > 0) {
-        /* Create dummy blocks at the right edge of the image. */
-        thisblockrow += blocks_across; /* => first dummy block */
-        FMEMZERO((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
-        lastDC = thisblockrow[-1][0];
-        for (bi = 0; bi < ndummy; bi++) {
-          thisblockrow[bi][0] = lastDC;
-        }
+       /* Create dummy blocks at the right edge of the image. */
+       thisblockrow += blocks_across; /* => first dummy block */
+       FMEMZERO((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.
@@ -307,19 +307,19 @@ compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
       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];
-        FMEMZERO((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;
-        }
+          block_row++) {
+       thisblockrow = buffer[block_row];
+       lastblockrow = buffer[block_row-1];
+       FMEMZERO((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;
+       }
       }
     }
   }
@@ -369,25 +369,25 @@ compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
        yoffset++) {
     for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
-         MCU_col_num++) {
+        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++;
-          }
-        }
+       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)) {
-        /* Suspension forced; update state counters and exit */
-        coef->MCU_vert_offset = yoffset;
-        coef->mcu_ctr = MCU_col_num;
-        return FALSE;
+       /* Suspension forced; update state counters and exit */
+       coef->MCU_vert_offset = yoffset;
+       coef->mcu_ctr = MCU_col_num;
+       return FALSE;
       }
     }
     /* Completed an MCU row, but perhaps not an iMCU row */
@@ -413,7 +413,7 @@ jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
 
   coef = (my_coef_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_coef_controller));
+                               SIZEOF(my_coef_controller));
   cinfo->coef = (struct jpeg_c_coef_controller *) coef;
   coef->pub.start_pass = start_pass_coef;
 
@@ -426,14 +426,14 @@ jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
     jpeg_component_info *compptr;
 
     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
-         ci++, compptr++) {
+        ci++, compptr++) {
       coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
-        ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
-         (JDIMENSION) jround_up((long) compptr->width_in_blocks,
-                                (long) compptr->h_samp_factor),
-         (JDIMENSION) jround_up((long) compptr->height_in_blocks,
-                                (long) compptr->v_samp_factor),
-         (JDIMENSION) compptr->v_samp_factor);
+       ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+        (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+                               (long) compptr->h_samp_factor),
+        (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+                               (long) compptr->v_samp_factor),
+        (JDIMENSION) compptr->v_samp_factor);
     }
 #else
     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
@@ -445,7 +445,7 @@ jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
 
     buffer = (JBLOCKROW)
       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+                                 C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
     for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
       coef->MCU_buffer[i] = buffer + i;
     }
index bfb2580..f6b4a49 100644 (file)
@@ -2,7 +2,7 @@
  * jccolor.c
  *
  * Copyright (C) 1991-1996, Thomas G. Lane.
- * Modified 2011-2012 by Guido Vollbeding.
+ * Modified 2011-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -29,13 +29,25 @@ typedef my_color_converter * my_cconvert_ptr;
 /**************** RGB -> YCbCr conversion: most common case **************/
 
 /*
- * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
- * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
- * The conversion equations to be implemented are therefore
- *     Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
- *     Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE
- *     Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE
- * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
+ * YCbCr is defined per Recommendation ITU-R BT.601-7 (03/2011),
+ * previously known as Recommendation CCIR 601-1, except that Cb and Cr
+ * are normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
+ * sRGB (standard RGB color space) is defined per IEC 61966-2-1:1999.
+ * sYCC (standard luma-chroma-chroma color space with extended gamut)
+ * is defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex F.
+ * bg-sRGB and bg-sYCC (big gamut standard color spaces)
+ * are defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex G.
+ * Note that the derived conversion coefficients given in some of these
+ * documents are imprecise.  The general conversion equations are
+ *     Y  = Kr * R + (1 - Kr - Kb) * G + Kb * B
+ *     Cb = 0.5 * (B - Y) / (1 - Kb)
+ *     Cr = 0.5 * (R - Y) / (1 - Kr)
+ * With Kr = 0.299 and Kb = 0.114 (derived according to SMPTE RP 177-1993
+ * from the 1953 FCC NTSC primaries and CIE Illuminant C),
+ * the conversion equations to be implemented are therefore
+ *     Y  =  0.299 * R + 0.587 * G + 0.114 * B
+ *     Cb = -0.168735892 * R - 0.331264108 * G + 0.5 * B + CENTERJSAMPLE
+ *     Cr =  0.5 * R - 0.418687589 * G - 0.081312411 * B + CENTERJSAMPLE
  * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
  * rather than CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and
  * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
@@ -49,9 +61,9 @@ typedef my_color_converter * my_cconvert_ptr;
  * For even more speed, we avoid doing any multiplications in the inner loop
  * by precalculating the constants times R,G,B for all possible values.
  * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
- * for 12-bit samples it is still acceptable.  It's not very reasonable for
- * 16-bit samples, but if you want lossless storage you shouldn't be changing
- * colorspace anyway.
+ * for 9-bit to 12-bit samples it is still acceptable.  It's not very
+ * reasonable for 16-bit samples, but if you want lossless storage you
+ * shouldn't be changing colorspace anyway.
  * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
  * in the tables to save adding them separately in the inner loop.
  */
@@ -93,24 +105,24 @@ rgb_ycc_start (j_compress_ptr cinfo)
   /* Allocate and fill in the conversion tables. */
   cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (TABLE_SIZE * SIZEOF(INT32)));
+                               (TABLE_SIZE * SIZEOF(INT32)));
 
   for (i = 0; i <= MAXJSAMPLE; i++) {
-    rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
-    rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
-    rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;
-    rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
-    rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
+    rgb_ycc_tab[i+R_Y_OFF] = FIX(0.299) * i;
+    rgb_ycc_tab[i+G_Y_OFF] = FIX(0.587) * i;
+    rgb_ycc_tab[i+B_Y_OFF] = FIX(0.114) * i   + ONE_HALF;
+    rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.168735892)) * i;
+    rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.331264108)) * i;
     /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
      * This ensures that the maximum output will round to MAXJSAMPLE
      * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
      */
-    rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
+    rgb_ycc_tab[i+B_CB_OFF] = FIX(0.5) * i    + CBCR_OFFSET + ONE_HALF-1;
 /*  B=>Cb and R=>Cr tables are the same
-    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
+    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.5) * i    + CBCR_OFFSET + ONE_HALF-1;
 */
-    rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
-    rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
+    rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.418687589)) * i;
+    rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.081312411)) * i;
   }
 }
 
@@ -129,8 +141,8 @@ rgb_ycc_start (j_compress_ptr cinfo)
 
 METHODDEF(void)
 rgb_ycc_convert (j_compress_ptr cinfo,
-                 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
-                 JDIMENSION output_row, int num_rows)
+                JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+                JDIMENSION output_row, int num_rows)
 {
   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   register INT32 * ctab = cconvert->rgb_ycc_tab;
@@ -157,16 +169,16 @@ rgb_ycc_convert (j_compress_ptr cinfo,
        */
       /* Y */
       outptr0[col] = (JSAMPLE)
-                ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
-                 >> SCALEBITS);
+               ((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);
+               ((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);
+               ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
+                >> SCALEBITS);
       inptr += RGB_PIXELSIZE;
     }
   }
@@ -185,8 +197,8 @@ rgb_ycc_convert (j_compress_ptr cinfo,
 
 METHODDEF(void)
 rgb_gray_convert (j_compress_ptr cinfo,
-                  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
-                  JDIMENSION output_row, int num_rows)
+                 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+                 JDIMENSION output_row, int num_rows)
 {
   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   register INT32 * ctab = cconvert->rgb_ycc_tab;
@@ -205,8 +217,8 @@ rgb_gray_convert (j_compress_ptr cinfo,
       b = GETJSAMPLE(inptr[RGB_BLUE]);
       /* Y */
       outptr[col] = (JSAMPLE)
-                ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
-                 >> SCALEBITS);
+               ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+                >> SCALEBITS);
       inptr += RGB_PIXELSIZE;
     }
   }
@@ -223,8 +235,8 @@ rgb_gray_convert (j_compress_ptr cinfo,
 
 METHODDEF(void)
 cmyk_ycck_convert (j_compress_ptr cinfo,
-                   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
-                   JDIMENSION output_row, int num_rows)
+                  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+                  JDIMENSION output_row, int num_rows)
 {
   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   register INT32 * ctab = cconvert->rgb_ycc_tab;
@@ -254,16 +266,16 @@ cmyk_ycck_convert (j_compress_ptr cinfo,
        */
       /* Y */
       outptr0[col] = (JSAMPLE)
-                ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
-                 >> SCALEBITS);
+               ((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);
+               ((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);
+               ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
+                >> SCALEBITS);
       inptr += 4;
     }
   }
@@ -274,12 +286,15 @@ cmyk_ycck_convert (j_compress_ptr cinfo,
  * Convert some rows of samples to the JPEG colorspace.
  * [R,G,B] to [R-G,G,B-G] conversion with modulo calculation
  * (forward reversible color transform).
+ * This can be seen as an adaption of the general RGB->YCbCr
+ * conversion equation with Kr = Kb = 0, while replacing the
+ * normalization by modulo calculation.
  */
 
 METHODDEF(void)
 rgb_rgb1_convert (j_compress_ptr cinfo,
-                  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
-                  JDIMENSION output_row, int num_rows)
+                 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+                 JDIMENSION output_row, int num_rows)
 {
   register int r, g, b;
   register JSAMPROW inptr;
@@ -312,13 +327,13 @@ rgb_rgb1_convert (j_compress_ptr cinfo,
 /*
  * 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).
+ * The source can be either plain grayscale or YCC (since Y == gray).
  */
 
 METHODDEF(void)
 grayscale_convert (j_compress_ptr cinfo,
-                   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
-                   JDIMENSION output_row, int num_rows)
+                  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+                  JDIMENSION output_row, int num_rows)
 {
   int instride = cinfo->input_components;
   register JSAMPROW inptr;
@@ -345,8 +360,8 @@ grayscale_convert (j_compress_ptr cinfo,
 
 METHODDEF(void)
 rgb_convert (j_compress_ptr cinfo,
-             JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
-             JDIMENSION output_row, int num_rows)
+            JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+            JDIMENSION output_row, int num_rows)
 {
   register JSAMPROW inptr;
   register JSAMPROW outptr0, outptr1, outptr2;
@@ -378,8 +393,8 @@ rgb_convert (j_compress_ptr cinfo,
 
 METHODDEF(void)
 null_convert (j_compress_ptr cinfo,
-              JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
-              JDIMENSION output_row, int num_rows)
+             JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+             JDIMENSION output_row, int num_rows)
 {
   int ci;
   register int nc = cinfo->num_components;
@@ -394,8 +409,8 @@ null_convert (j_compress_ptr cinfo,
       inptr = input_buf[0] + ci;
       outptr = output_buf[ci][output_row];
       for (col = 0; col < num_cols; col++) {
-        *outptr++ = *inptr;    /* don't need GETJSAMPLE() here */
-        inptr += nc;
+       *outptr++ = *inptr;     /* don't need GETJSAMPLE() here */
+       inptr += nc;
       }
     }
     input_buf++;
@@ -426,7 +441,7 @@ jinit_color_converter (j_compress_ptr cinfo)
 
   cconvert = (my_cconvert_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_color_converter));
+                               SIZEOF(my_color_converter));
   cinfo->cconvert = &cconvert->pub;
   /* set start_pass to null method until we find out differently */
   cconvert->pub.start_pass = null_method;
@@ -439,11 +454,13 @@ jinit_color_converter (j_compress_ptr cinfo)
     break;
 
   case JCS_RGB:
+  case JCS_BG_RGB:
     if (cinfo->input_components != RGB_PIXELSIZE)
       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
     break;
 
   case JCS_YCbCr:
+  case JCS_BG_YCC:
     if (cinfo->input_components != 3)
       ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
     break;
@@ -460,8 +477,10 @@ jinit_color_converter (j_compress_ptr cinfo)
     break;
   }
 
-  /* Support color transform only for RGB colorspace */
-  if (cinfo->color_transform && cinfo->jpeg_color_space != JCS_RGB)
+  /* Support color transform only for RGB colorspaces */
+  if (cinfo->color_transform &&
+      cinfo->jpeg_color_space != JCS_RGB &&
+      cinfo->jpeg_color_space != JCS_BG_RGB)
     ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
 
   /* Check num_components, set conversion method based on requested space */
@@ -469,30 +488,35 @@ jinit_color_converter (j_compress_ptr cinfo)
   case JCS_GRAYSCALE:
     if (cinfo->num_components != 1)
       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
-    if (cinfo->in_color_space == JCS_GRAYSCALE ||
-        cinfo->in_color_space == JCS_YCbCr)
+    switch (cinfo->in_color_space) {
+    case JCS_GRAYSCALE:
+    case JCS_YCbCr:
+    case JCS_BG_YCC:
       cconvert->pub.color_convert = grayscale_convert;
-    else if (cinfo->in_color_space == JCS_RGB) {
+      break;
+    case JCS_RGB:
       cconvert->pub.start_pass = rgb_ycc_start;
       cconvert->pub.color_convert = rgb_gray_convert;
-    } else
+      break;
+    default:
       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    }
     break;
 
   case JCS_RGB:
+  case JCS_BG_RGB:
     if (cinfo->num_components != 3)
       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
-    if (cinfo->in_color_space == JCS_RGB) {
+    if (cinfo->in_color_space == cinfo->jpeg_color_space) {
       switch (cinfo->color_transform) {
       case JCT_NONE:
-        cconvert->pub.color_convert = rgb_convert;
-        break;
+       cconvert->pub.color_convert = rgb_convert;
+       break;
       case JCT_SUBTRACT_GREEN:
-        cconvert->pub.color_convert = rgb_rgb1_convert;
-        break;
+       cconvert->pub.color_convert = rgb_rgb1_convert;
+       break;
       default:
-        ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
-        break;
+       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
       }
     } else
       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
@@ -501,13 +525,48 @@ jinit_color_converter (j_compress_ptr cinfo)
   case JCS_YCbCr:
     if (cinfo->num_components != 3)
       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
-    if (cinfo->in_color_space == JCS_RGB) {
+    switch (cinfo->in_color_space) {
+    case JCS_RGB:
       cconvert->pub.start_pass = rgb_ycc_start;
       cconvert->pub.color_convert = rgb_ycc_convert;
-    } else if (cinfo->in_color_space == JCS_YCbCr)
+      break;
+    case JCS_YCbCr:
       cconvert->pub.color_convert = null_convert;
-    else
+      break;
+    default:
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    }
+    break;
+
+  case JCS_BG_YCC:
+    if (cinfo->num_components != 3)
+      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+    switch (cinfo->in_color_space) {
+    case JCS_RGB:
+      /* For conversion from normal RGB input to BG_YCC representation,
+       * the Cb/Cr values are first computed as usual, and then
+       * quantized further after DCT processing by a factor of
+       * 2 in reference to the nominal quantization factor.
+       */
+      /* need quantization scale by factor of 2 after DCT */
+      cinfo->comp_info[1].component_needed = TRUE;
+      cinfo->comp_info[2].component_needed = TRUE;
+      /* compute normal YCC first */
+      cconvert->pub.start_pass = rgb_ycc_start;
+      cconvert->pub.color_convert = rgb_ycc_convert;
+      break;
+    case JCS_YCbCr:
+      /* need quantization scale by factor of 2 after DCT */
+      cinfo->comp_info[1].component_needed = TRUE;
+      cinfo->comp_info[2].component_needed = TRUE;
+      /*FALLTHROUGH*/
+    case JCS_BG_YCC:
+      /* Pass through for BG_YCC input */
+      cconvert->pub.color_convert = null_convert;
+      break;
+    default:
       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    }
     break;
 
   case JCS_CMYK:
@@ -522,18 +581,22 @@ jinit_color_converter (j_compress_ptr cinfo)
   case JCS_YCCK:
     if (cinfo->num_components != 4)
       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
-    if (cinfo->in_color_space == JCS_CMYK) {
+    switch (cinfo->in_color_space) {
+    case JCS_CMYK:
       cconvert->pub.start_pass = rgb_ycc_start;
       cconvert->pub.color_convert = cmyk_ycck_convert;
-    } else if (cinfo->in_color_space == JCS_YCCK)
+      break;
+    case JCS_YCCK:
       cconvert->pub.color_convert = null_convert;
-    else
+      break;
+    default:
       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)
+       cinfo->num_components != cinfo->input_components)
       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
     cconvert->pub.color_convert = null_convert;
     break;
index ec90b9a..fafab91 100644 (file)
@@ -2,6 +2,7 @@
  * jcdctmgr.c
  *
  * Copyright (C) 1994-1996, Thomas G. Lane.
+ * Modified 2003-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -25,22 +26,30 @@ typedef struct {
   /* Pointer to the DCT routine actually in use */
   forward_DCT_method_ptr do_dct[MAX_COMPONENTS];
 
-  /* 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 normal array 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[MAX_COMPONENTS];
-  FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
 #endif
 } my_fdct_controller;
 
 typedef my_fdct_controller * my_fdct_ptr;
 
 
+/* The allocated post-DCT divisor tables -- big enough for any
+ * supported variant and not identical to the quant table entries,
+ * because of scaling (especially for an unnormalized DCT) --
+ * are pointed to by dct_table in the per-component comp_info
+ * structures.  Each table is given in normal array order.
+ */
+
+typedef union {
+  DCTELEM int_array[DCTSIZE2];
+#ifdef DCT_FLOAT_SUPPORTED
+  FAST_FLOAT float_array[DCTSIZE2];
+#endif
+} divisor_table;
+
+
 /* The current scaled-DCT routines require ISLOW-style divisor tables,
  * so be sure to compile that code if either ISLOW or SCALING is requested.
  */
@@ -63,15 +72,15 @@ typedef my_fdct_controller * my_fdct_ptr;
 
 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)
+            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[compptr->component_index];
-  DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
+  DCTELEM * divisors = (DCTELEM *) compptr->dct_table;
   DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
   JDIMENSION bi;
 
@@ -87,35 +96,35 @@ forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
       register JCOEFPTR output_ptr = coef_blocks[bi];
 
       for (i = 0; i < DCTSIZE2; i++) {
-        qval = divisors[i];
-        temp = workspace[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.
-         */
+       qval = divisors[i];
+       temp = workspace[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;
+       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;
       }
     }
   }
@@ -126,15 +135,15 @@ forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
 
 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)
+                  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[compptr->component_index];
-  FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
+  FAST_FLOAT * divisors = (FAST_FLOAT *) compptr->dct_table;
   FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
   JDIMENSION bi;
 
@@ -150,15 +159,15 @@ forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
       register JCOEFPTR output_ptr = coef_blocks[bi];
 
       for (i = 0; i < DCTSIZE2; i++) {
-        /* Apply the quantization and scaling factor */
-        temp = workspace[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);
+       /* Apply the quantization and scaling factor */
+       temp = workspace[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);
       }
     }
   }
@@ -320,54 +329,49 @@ start_pass_fdctmgr (j_compress_ptr cinfo)
       switch (cinfo->dct_method) {
 #ifdef DCT_ISLOW_SUPPORTED
       case JDCT_ISLOW:
-        fdct->do_dct[ci] = jpeg_fdct_islow;
-        method = JDCT_ISLOW;
-        break;
+       fdct->do_dct[ci] = jpeg_fdct_islow;
+       method = JDCT_ISLOW;
+       break;
 #endif
 #ifdef DCT_IFAST_SUPPORTED
       case JDCT_IFAST:
-        fdct->do_dct[ci] = jpeg_fdct_ifast;
-        method = JDCT_IFAST;
-        break;
+       fdct->do_dct[ci] = jpeg_fdct_ifast;
+       method = JDCT_IFAST;
+       break;
 #endif
 #ifdef DCT_FLOAT_SUPPORTED
       case JDCT_FLOAT:
-        fdct->do_float_dct[ci] = jpeg_fdct_float;
-        method = JDCT_FLOAT;
-        break;
+       fdct->do_float_dct[ci] = jpeg_fdct_float;
+       method = JDCT_FLOAT;
+       break;
 #endif
       default:
-        ERREXIT(cinfo, JERR_NOT_COMPILED);
-        break;
+       ERREXIT(cinfo, JERR_NOT_COMPILED);
+       break;
       }
       break;
     default:
       ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
-               compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
+              compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
       break;
     }
     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)
+       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 */
+    /* Create divisor table from quant table */
     switch (method) {
 #ifdef PROVIDE_ISLOW_TABLES
     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];
+      dtbl = (DCTELEM *) compptr->dct_table;
       for (i = 0; i < DCTSIZE2; i++) {
-        dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
+       dtbl[i] =
+         ((DCTELEM) qtbl->quantval[i]) << (compptr->component_needed ? 4 : 3);
       }
       fdct->pub.forward_DCT[ci] = forward_DCT;
       break;
@@ -375,38 +379,33 @@ start_pass_fdctmgr (j_compress_ptr cinfo)
 #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.
-         */
+       /* 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 */
-          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[i]),
-                    CONST_BITS-3);
-        }
+       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
+
+       dtbl = (DCTELEM *) compptr->dct_table;
+       for (i = 0; i < DCTSIZE2; i++) {
+         dtbl[i] = (DCTELEM)
+           DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
+                                 (INT32) aanscales[i]),
+                   compptr->component_needed ? CONST_BITS-4 : CONST_BITS-3);
+       }
       }
       fdct->pub.forward_DCT[ci] = forward_DCT;
       break;
@@ -414,36 +413,31 @@ start_pass_fdctmgr (j_compress_ptr cinfo)
 #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];
-        i = 0;
-        for (row = 0; row < DCTSIZE; row++) {
-          for (col = 0; col < DCTSIZE; col++) {
-            fdtbl[i] = (FAST_FLOAT)
-              (1.0 / (((double) qtbl->quantval[i] *
-                       aanscalefactor[row] * aanscalefactor[col] * 8.0)));
-            i++;
-          }
-        }
+       /* 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 = (FAST_FLOAT *) compptr->dct_table;
+       int row, col;
+       static const double aanscalefactor[DCTSIZE] = {
+         1.0, 1.387039845, 1.306562965, 1.175875602,
+         1.0, 0.785694958, 0.541196100, 0.275899379
+       };
+
+       i = 0;
+       for (row = 0; row < DCTSIZE; row++) {
+         for (col = 0; col < DCTSIZE; col++) {
+           fdtbl[i] = (FAST_FLOAT)
+             (1.0 / ((double) qtbl->quantval[i] *
+                     aanscalefactor[row] * aanscalefactor[col] *
+                     (compptr->component_needed ? 16.0 : 8.0)));
+           i++;
+         }
+       }
       }
       fdct->pub.forward_DCT[ci] = forward_DCT_float;
       break;
@@ -464,19 +458,20 @@ GLOBAL(void)
 jinit_forward_dct (j_compress_ptr cinfo)
 {
   my_fdct_ptr fdct;
-  int i;
+  int ci;
+  jpeg_component_info *compptr;
 
   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;
+                               SIZEOF(my_fdct_controller));
+  cinfo->fdct = &fdct->pub;
   fdct->pub.start_pass = start_pass_fdctmgr;
 
-  /* 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
+  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+       ci++, compptr++) {
+    /* Allocate a divisor table for each component */
+    compptr->dct_table =
+      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+                                 SIZEOF(divisor_table));
   }
 }
index b705af1..d1313f6 100644 (file)
@@ -2,7 +2,7 @@
  * jchuff.c
  *
  * Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 2006-2009 by Guido Vollbeding.
+ * Modified 2006-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -68,12 +68,12 @@ typedef struct {
 #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])
+       ((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
 
@@ -143,9 +143,9 @@ typedef struct {
 #ifdef RIGHT_SHIFT_IS_UNSIGNED
 #define ISHIFT_TEMPS   int ishift_temp;
 #define IRIGHT_SHIFT(x,shft)  \
-        ((ishift_temp = (x)) < 0 ? \
-         (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
-         (ishift_temp >> (shft)))
+       ((ishift_temp = (x)) < 0 ? \
+        (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
+        (ishift_temp >> (shft)))
 #else
 #define ISHIFT_TEMPS
 #define IRIGHT_SHIFT(x,shft)   ((x) >> (shft))
@@ -159,7 +159,7 @@ typedef struct {
 
 LOCAL(void)
 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
-                         c_derived_tbl ** pdtbl)
+                        c_derived_tbl ** pdtbl)
 {
   JHUFF_TBL *htbl;
   c_derived_tbl *dtbl;
@@ -184,9 +184,9 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
   if (*pdtbl == NULL)
     *pdtbl = (c_derived_tbl *)
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  SIZEOF(c_derived_tbl));
+                                 SIZEOF(c_derived_tbl));
   dtbl = *pdtbl;
-
+  
   /* Figure C.1: make table of Huffman code length for each symbol */
 
   p = 0;
@@ -199,7 +199,7 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
   }
   huffsize[p] = 0;
   lastp = p;
-
+  
   /* Figure C.2: generate the codes themselves */
   /* We also validate that the counts represent a legal Huffman code tree. */
 
@@ -219,7 +219,7 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
     code <<= 1;
     si++;
   }
-
+  
   /* Figure C.3: generate encoding tables */
   /* These are code and size indexed by symbol value */
 
@@ -253,16 +253,16 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
 
 /* Emit a byte, taking 'action' if must suspend. */
 #define emit_byte_s(state,val,action)  \
-        { *(state)->next_output_byte++ = (JOCTET) (val);  \
-          if (--(state)->free_in_buffer == 0)  \
-            if (! dump_buffer_s(state))  \
-              { action; } }
+       { *(state)->next_output_byte++ = (JOCTET) (val);  \
+         if (--(state)->free_in_buffer == 0)  \
+           if (! dump_buffer_s(state))  \
+             { action; } }
 
 /* Emit a byte */
 #define emit_byte_e(entropy,val)  \
-        { *(entropy)->next_output_byte++ = (JOCTET) (val);  \
-          if (--(entropy)->free_in_buffer == 0)  \
-            dump_buffer_e(entropy); }
+       { *(entropy)->next_output_byte++ = (JOCTET) (val);  \
+         if (--(entropy)->free_in_buffer == 0)  \
+           dump_buffer_e(entropy); }
 
 
 LOCAL(boolean)
@@ -308,20 +308,23 @@ emit_bits_s (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 = (INT32) code;
-  register int put_bits = state->cur.put_bits;
+  register INT32 put_buffer;
+  register int put_bits;
 
   /* if size is 0, caller used an invalid Huffman table entry */
   if (size == 0)
     ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
 
-  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
+  /* mask off any extra bits in code */
+  put_buffer = ((INT32) code) & ((((INT32) 1) << size) - 1);
 
-  put_bits += size;            /* new number of bits in buffer */
+  /* new number of bits in buffer */
+  put_bits = size + state->cur.put_bits;
 
   put_buffer <<= 24 - put_bits; /* align incoming bits */
 
-  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
+  /* and merge with old buffer contents */
+  put_buffer |= state->cur.put_buffer;
 
   while (put_bits >= 8) {
     int c = (int) ((put_buffer >> 16) & 0xFF);
@@ -347,8 +350,8 @@ emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size)
 /* Emit some bits, unless we are in gather mode */
 {
   /* This routine is heavily used, so it's worth coding tightly. */
-  register INT32 put_buffer = (INT32) code;
-  register int put_bits = entropy->saved.put_bits;
+  register INT32 put_buffer;
+  register int put_bits;
 
   /* if size is 0, caller used an invalid Huffman table entry */
   if (size == 0)
@@ -357,9 +360,11 @@ emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size)
   if (entropy->gather_statistics)
     return;                    /* do nothing if we're only getting stats */
 
-  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
+  /* mask off any extra bits in code */
+  put_buffer = ((INT32) code) & ((((INT32) 1) << size) - 1);
 
-  put_bits += size;            /* new number of bits in buffer */
+  /* new number of bits in buffer */
+  put_bits = size + entropy->saved.put_bits;
 
   put_buffer <<= 24 - put_bits; /* align incoming bits */
 
@@ -438,7 +443,7 @@ emit_ac_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
 
 LOCAL(void)
 emit_buffered_bits (huff_entropy_ptr entropy, char * bufstart,
-                    unsigned int nbits)
+                   unsigned int nbits)
 {
   if (entropy->gather_statistics)
     return;                    /* no real work */
@@ -543,10 +548,7 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   register int temp, temp2;
   register int nbits;
-  int blkn, ci;
-  int Al = cinfo->Al;
-  JBLOCKROW block;
-  jpeg_component_info * compptr;
+  int blkn, ci, tbl;
   ISHIFT_TEMPS
 
   entropy->next_output_byte = cinfo->dest->next_output_byte;
@@ -559,21 +561,20 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
   /* Encode the MCU data blocks */
   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
-    block = MCU_data[blkn];
     ci = cinfo->MCU_membership[blkn];
-    compptr = cinfo->cur_comp_info[ci];
+    tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
 
     /* Compute the DC value after the required point transform by Al.
      * This is simply an arithmetic right shift.
      */
-    temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
+    temp = IRIGHT_SHIFT((int) (MCU_data[blkn][0][0]), cinfo->Al);
 
     /* DC differences are figured on the point-transformed values. */
-    temp = temp2 - entropy->saved.last_dc_val[ci];
-    entropy->saved.last_dc_val[ci] = temp2;
+    temp2 = temp - entropy->saved.last_dc_val[ci];
+    entropy->saved.last_dc_val[ci] = temp;
 
     /* Encode the DC coefficient difference per section G.1.2.1 */
-    temp2 = temp;
+    temp = temp2;
     if (temp < 0) {
       temp = -temp;            /* temp is abs value of input */
       /* For a negative input, want temp2 = bitwise complement of abs(input) */
@@ -594,7 +595,7 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
       ERREXIT(cinfo, JERR_BAD_DCT_COEF);
 
     /* Count/emit the Huffman-coded symbol for the number of bits */
-    emit_dc_symbol(entropy, compptr->dc_tbl_no, nbits);
+    emit_dc_symbol(entropy, tbl, nbits);
 
     /* Emit that number of bits of the value, if positive, */
     /* or the complement of its magnitude, if negative. */
@@ -628,12 +629,12 @@ METHODDEF(boolean)
 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 {
   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+  const int * natural_order;
+  JBLOCKROW block;
   register int temp, temp2;
   register int nbits;
   register int r, k;
   int Se, Al;
-  const int * natural_order;
-  JBLOCKROW block;
 
   entropy->next_output_byte = cinfo->dest->next_output_byte;
   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
@@ -651,9 +652,9 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
   block = MCU_data[0];
 
   /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
-
+  
   r = 0;                       /* r = run length of zeros */
-
+   
   for (k = cinfo->Ss; k <= Se; k++) {
     if ((temp = (*block)[natural_order[k]]) == 0) {
       r++;
@@ -731,18 +732,15 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
 /*
  * MCU encoding for DC successive approximation refinement scan.
- * Note: we assume such scans can be multi-component, although the spec
- * is not very clear on the point.
+ * Note: we assume such scans can be multi-component,
+ * although the spec is not very clear on the point.
  */
 
 METHODDEF(boolean)
 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 {
   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
-  register int temp;
-  int blkn;
-  int Al = cinfo->Al;
-  JBLOCKROW block;
+  int Al, blkn;
 
   entropy->next_output_byte = cinfo->dest->next_output_byte;
   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
@@ -752,13 +750,12 @@ encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
     if (entropy->restarts_to_go == 0)
       emit_restart_e(entropy, entropy->next_restart_num);
 
+  Al = cinfo->Al;
+
   /* Encode the MCU data blocks */
   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
-    block = MCU_data[blkn];
-
     /* We simply emit the Al'th bit of the DC coefficient value. */
-    temp = (*block)[0];
-    emit_bits_e(entropy, (unsigned int) (temp >> Al), 1);
+    emit_bits_e(entropy, (unsigned int) (MCU_data[blkn][0][0] >> Al), 1);
   }
 
   cinfo->dest->next_output_byte = entropy->next_output_byte;
@@ -786,14 +783,14 @@ METHODDEF(boolean)
 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 {
   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+  const int * natural_order;
+  JBLOCKROW block;
   register int temp;
   register int r, k;
+  int Se, Al;
   int EOB;
   char *BR_buffer;
   unsigned int BR;
-  int Se, Al;
-  const int * natural_order;
-  JBLOCKROW block;
   int absvalues[DCTSIZE2];
 
   entropy->next_output_byte = cinfo->dest->next_output_byte;
@@ -830,7 +827,7 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
   }
 
   /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
-
+  
   r = 0;                       /* r = run length of zeros */
   BR = 0;                      /* BR = count of buffered bits added now */
   BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
@@ -914,11 +911,11 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
 LOCAL(boolean)
 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
-                  c_derived_tbl *dctbl, c_derived_tbl *actbl)
+                 c_derived_tbl *dctbl, c_derived_tbl *actbl)
 {
   register int temp, temp2;
   register int nbits;
-  register int k, r, i;
+  register int r, k;
   int Se = state->cinfo->lim_Se;
   const int * natural_order = state->cinfo->natural_order;
 
@@ -960,40 +957,40 @@ encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
   r = 0;                       /* r = run length of zeros */
 
   for (k = 1; k <= Se; k++) {
-    if ((temp = block[natural_order[k]]) == 0) {
+    if ((temp2 = block[natural_order[k]]) == 0) {
       r++;
     } else {
       /* if run length > 15, must emit special run-length-16 codes (0xF0) */
       while (r > 15) {
-        if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
-          return FALSE;
-        r -= 16;
+       if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
+         return FALSE;
+       r -= 16;
       }
 
-      temp2 = temp;
+      temp = temp2;
       if (temp < 0) {
-        temp = -temp;          /* temp is abs value of input */
-        /* This code assumes we are on a two's complement machine */
-        temp2--;
+       temp = -temp;           /* temp is abs value of input */
+       /* This code assumes we are on a two's complement machine */
+       temp2--;
       }
 
       /* 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++;
+       nbits++;
       /* Check for out-of-range coefficient values */
       if (nbits > MAX_COEF_BITS)
-        ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
+       ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
 
       /* Emit Huffman symbol for run length / number of bits */
-      i = (r << 4) + nbits;
-      if (! emit_bits_s(state, actbl->ehufco[i], actbl->ehufsi[i]))
-        return FALSE;
+      temp = (r << 4) + nbits;
+      if (! emit_bits_s(state, actbl->ehufco[temp], actbl->ehufsi[temp]))
+       return FALSE;
 
       /* Emit that number of bits of the value, if positive, */
       /* or the complement of its magnitude, if negative. */
       if (! emit_bits_s(state, (unsigned int) temp2, nbits))
-        return FALSE;
+       return FALSE;
 
       r = 0;
     }
@@ -1030,7 +1027,7 @@ encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
   if (cinfo->restart_interval) {
     if (entropy->restarts_to_go == 0)
       if (! emit_restart_s(&state, entropy->next_restart_num))
-        return FALSE;
+       return FALSE;
   }
 
   /* Encode the MCU data blocks */
@@ -1038,9 +1035,9 @@ encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
     ci = cinfo->MCU_membership[blkn];
     compptr = cinfo->cur_comp_info[ci];
     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]))
+                          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];
@@ -1120,11 +1117,11 @@ finish_pass_huff (j_compress_ptr cinfo)
 
 LOCAL(void)
 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
-                 long dc_counts[], long ac_counts[])
+                long dc_counts[], long ac_counts[])
 {
   register int temp;
   register int nbits;
-  register int k, r;
+  register int r, k;
   int Se = cinfo->lim_Se;
   const int * natural_order = cinfo->natural_order;
 
@@ -1159,21 +1156,21 @@ htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
     } else {
       /* if run length > 15, must emit special run-length-16 codes (0xF0) */
       while (r > 15) {
-        ac_counts[0xF0]++;
-        r -= 16;
+       ac_counts[0xF0]++;
+       r -= 16;
       }
 
       /* Find the number of bits needed for the magnitude of the coefficient */
       if (temp < 0)
-        temp = -temp;
+       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++;
+       nbits++;
       /* Check for out-of-range coefficient values */
       if (nbits > MAX_COEF_BITS)
-        ERREXIT(cinfo, JERR_BAD_DCT_COEF);
+       ERREXIT(cinfo, JERR_BAD_DCT_COEF);
 
       /* Count Huffman symbol for run length / number of bits */
       ac_counts[(r << 4) + nbits]++;
@@ -1205,7 +1202,7 @@ encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
     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;
+       entropy->saved.last_dc_val[ci] = 0;
       /* Update restart state */
       entropy->restarts_to_go = cinfo->restart_interval;
     }
@@ -1216,8 +1213,8 @@ encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
     ci = cinfo->MCU_membership[blkn];
     compptr = cinfo->cur_comp_info[ci];
     htest_one_block(cinfo, 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->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];
   }
 
@@ -1269,7 +1266,7 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
   MEMZERO(codesize, SIZEOF(codesize));
   for (i = 0; i < 257; i++)
     others[i] = -1;            /* init links to empty */
-
+  
   freq[256] = 1;               /* make sure 256 has a nonzero count */
   /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
    * that no real symbol is given code-value of all ones, because 256
@@ -1285,8 +1282,8 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
     v = 1000000000L;
     for (i = 0; i <= 256; i++) {
       if (freq[i] && freq[i] <= v) {
-        v = freq[i];
-        c1 = i;
+       v = freq[i];
+       c1 = i;
       }
     }
 
@@ -1296,15 +1293,15 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
     v = 1000000000L;
     for (i = 0; i <= 256; i++) {
       if (freq[i] && freq[i] <= v && i != c1) {
-        v = freq[i];
-        c2 = i;
+       v = freq[i];
+       c2 = i;
       }
     }
 
     /* Done if we've merged everything into one frequency */
     if (c2 < 0)
       break;
-
+    
     /* Else merge the two counts/trees */
     freq[c1] += freq[c2];
     freq[c2] = 0;
@@ -1315,9 +1312,9 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
       c1 = others[c1];
       codesize[c1]++;
     }
-
+    
     others[c1] = c2;           /* chain c2 onto c1's tree branch */
-
+    
     /* Increment the codesize of everything in c2's tree branch */
     codesize[c2]++;
     while (others[c2] >= 0) {
@@ -1332,7 +1329,7 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
       /* The JPEG standard seems to think that this can't happen, */
       /* but I'm paranoid... */
       if (codesize[i] > MAX_CLEN)
-        ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
+       ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
 
       bits[codesize[i]]++;
     }
@@ -1348,13 +1345,13 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
    * shortest nonzero BITS entry is converted into a prefix for two code words
    * one bit longer.
    */
-
+  
   for (i = MAX_CLEN; i > 16; i--) {
     while (bits[i] > 0) {
       j = i - 2;               /* find length of new prefix to be used */
       while (bits[j] == 0)
-        j--;
-
+       j--;
+      
       bits[i] -= 2;            /* remove two symbols */
       bits[i-1]++;             /* one goes in this length */
       bits[j+1] += 2;          /* two new symbols in this length */
@@ -1366,10 +1363,10 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
   while (bits[i] == 0)         /* find largest codelength still in use */
     i--;
   bits[i]--;
-
+  
   /* Return final symbol counts (only for lengths 0..16) */
   MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
-
+  
   /* Return a list of the symbols sorted by code length */
   /* It's not real clear to me why we don't need to consider the codelength
    * changes made above, but the JPEG spec seems to think this works.
@@ -1378,8 +1375,8 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
   for (i = 1; i <= MAX_CLEN; i++) {
     for (j = 0; j <= 255; j++) {
       if (codesize[j] == i) {
-        htbl->huffval[p] = (UINT8) j;
-        p++;
+       htbl->huffval[p] = (UINT8) j;
+       p++;
       }
     }
   }
@@ -1419,22 +1416,22 @@ finish_pass_gather (j_compress_ptr cinfo)
     if (cinfo->Ss == 0 && cinfo->Ah == 0) {
       tbl = compptr->dc_tbl_no;
       if (! did_dc[tbl]) {
-        htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
-        if (*htblptr == NULL)
-          *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
-        jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
-        did_dc[tbl] = TRUE;
+       htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
+       if (*htblptr == NULL)
+         *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+       jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
+       did_dc[tbl] = TRUE;
       }
     }
     /* AC needs no table when not present */
     if (cinfo->Se) {
       tbl = compptr->ac_tbl_no;
       if (! did_ac[tbl]) {
-        htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
-        if (*htblptr == NULL)
-          *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
-        jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
-        did_ac[tbl] = TRUE;
+       htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
+       if (*htblptr == NULL)
+         *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+       jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
+       did_ac[tbl] = TRUE;
       }
     }
   }
@@ -1468,19 +1465,19 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
     /* Select execution routine */
     if (cinfo->Ah == 0) {
       if (cinfo->Ss == 0)
-        entropy->pub.encode_mcu = encode_mcu_DC_first;
+       entropy->pub.encode_mcu = encode_mcu_DC_first;
       else
-        entropy->pub.encode_mcu = encode_mcu_AC_first;
+       entropy->pub.encode_mcu = encode_mcu_AC_first;
     } else {
       if (cinfo->Ss == 0)
-        entropy->pub.encode_mcu = encode_mcu_DC_refine;
+       entropy->pub.encode_mcu = encode_mcu_DC_refine;
       else {
-        entropy->pub.encode_mcu = encode_mcu_AC_refine;
-        /* AC refinement needs a correction bit buffer */
-        if (entropy->bit_buffer == NULL)
-          entropy->bit_buffer = (char *)
-            (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                        MAX_CORR_BITS * SIZEOF(char));
+       entropy->pub.encode_mcu = encode_mcu_AC_refine;
+       /* AC refinement needs a correction bit buffer */
+       if (entropy->bit_buffer == NULL)
+         entropy->bit_buffer = (char *)
+           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+                                       MAX_CORR_BITS * SIZEOF(char));
       }
     }
 
@@ -1501,22 +1498,22 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
     if (cinfo->Ss == 0 && cinfo->Ah == 0) {
       tbl = compptr->dc_tbl_no;
       if (gather_statistics) {
-        /* Check for invalid table index */
-        /* (make_c_derived_tbl does this in the other path) */
-        if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
-          ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
-        /* Allocate and zero the statistics tables */
-        /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
-        if (entropy->dc_count_ptrs[tbl] == NULL)
-          entropy->dc_count_ptrs[tbl] = (long *)
-            (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                        257 * SIZEOF(long));
-        MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
+       /* Check for invalid table index */
+       /* (make_c_derived_tbl does this in the other path) */
+       if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
+         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
+       /* Allocate and zero the statistics tables */
+       /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
+       if (entropy->dc_count_ptrs[tbl] == NULL)
+         entropy->dc_count_ptrs[tbl] = (long *)
+           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+                                       257 * SIZEOF(long));
+       MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
       } else {
-        /* Compute derived values for Huffman tables */
-        /* We may do this more than once for a table, but it's not expensive */
-        jpeg_make_c_derived_tbl(cinfo, TRUE, tbl,
-                                & entropy->dc_derived_tbls[tbl]);
+       /* Compute derived values for Huffman tables */
+       /* We may do this more than once for a table, but it's not expensive */
+       jpeg_make_c_derived_tbl(cinfo, TRUE, tbl,
+                               & entropy->dc_derived_tbls[tbl]);
       }
       /* Initialize DC predictions to 0 */
       entropy->saved.last_dc_val[ci] = 0;
@@ -1525,16 +1522,16 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
     if (cinfo->Se) {
       tbl = compptr->ac_tbl_no;
       if (gather_statistics) {
-        if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
-          ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
-        if (entropy->ac_count_ptrs[tbl] == NULL)
-          entropy->ac_count_ptrs[tbl] = (long *)
-            (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                        257 * SIZEOF(long));
-        MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
+       if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
+         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
+       if (entropy->ac_count_ptrs[tbl] == NULL)
+         entropy->ac_count_ptrs[tbl] = (long *)
+           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+                                       257 * SIZEOF(long));
+       MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
       } else {
-        jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
-                                & entropy->ac_derived_tbls[tbl]);
+       jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
+                               & entropy->ac_derived_tbls[tbl]);
       }
     }
   }
@@ -1561,8 +1558,8 @@ jinit_huff_encoder (j_compress_ptr cinfo)
 
   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;
+                               SIZEOF(huff_entropy_encoder));
+  cinfo->entropy = &entropy->pub;
   entropy->pub.start_pass = start_pass_huff;
 
   /* Mark tables unallocated */
index d4f5e3a..1e13e34 100644 (file)
@@ -2,6 +2,7 @@
  * jcinit.c
  *
  * Copyright (C) 1991-1997, Thomas G. Lane.
+ * Modified 2003-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
 GLOBAL(void)
 jinit_compress_master (j_compress_ptr cinfo)
 {
+  long samplesperrow;
+  JDIMENSION jd_samplesperrow;
+
+  /* For now, precision must match compiled-in value... */
+  if (cinfo->data_precision != BITS_IN_JSAMPLE)
+    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
+
+  /* Sanity check on image dimensions */
+  if (cinfo->image_height <= 0 || cinfo->image_width <= 0 ||
+      cinfo->input_components <= 0)
+    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
+
+  /* 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);
+
   /* Initialize master control (includes parameter checking/processing) */
   jinit_c_master_control(cinfo, FALSE /* full compression */);
 
@@ -49,7 +68,7 @@ jinit_compress_master (j_compress_ptr cinfo)
 
   /* Need a full-image coefficient buffer in any multi-pass mode. */
   jinit_c_coef_controller(cinfo,
-                (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
+               (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
   jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
 
   jinit_marker_writer(cinfo);
index 897b737..39b9790 100644 (file)
@@ -53,12 +53,12 @@ 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));
+       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));
+       JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
+            JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
 #endif
 
 
@@ -112,8 +112,8 @@ start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
 
 METHODDEF(void)
 process_data_simple_main (j_compress_ptr cinfo,
-                          JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
-                          JDIMENSION in_rows_avail)
+                         JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+                         JDIMENSION in_rows_avail)
 {
   my_main_ptr mainp = (my_main_ptr) cinfo->main;
 
@@ -121,9 +121,9 @@ process_data_simple_main (j_compress_ptr cinfo,
     /* Read input data if we haven't filled the main buffer yet */
     if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
       (*cinfo->prep->pre_process_data) (cinfo,
-                                        input_buf, in_row_ctr, in_rows_avail,
-                                        mainp->buffer, &mainp->rowgroup_ctr,
-                                        (JDIMENSION) cinfo->min_DCT_v_scaled_size);
+                                       input_buf, in_row_ctr, in_rows_avail,
+                                       mainp->buffer, &mainp->rowgroup_ctr,
+                                       (JDIMENSION) cinfo->min_DCT_v_scaled_size);
 
     /* 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
@@ -141,8 +141,8 @@ process_data_simple_main (j_compress_ptr cinfo,
        * think we were done.
        */
       if (! mainp->suspended) {
-        (*in_row_ctr)--;
-        mainp->suspended = TRUE;
+       (*in_row_ctr)--;
+       mainp->suspended = TRUE;
       }
       return;
     }
@@ -168,8 +168,8 @@ process_data_simple_main (j_compress_ptr cinfo,
 
 METHODDEF(void)
 process_data_buffer_main (j_compress_ptr cinfo,
-                          JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
-                          JDIMENSION in_rows_avail)
+                         JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+                         JDIMENSION in_rows_avail)
 {
   my_main_ptr mainp = (my_main_ptr) cinfo->main;
   int ci;
@@ -180,18 +180,18 @@ process_data_buffer_main (j_compress_ptr cinfo,
     /* Realign the virtual buffers if at the start of an iMCU row. */
     if (mainp->rowgroup_ctr == 0) {
       for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
-           ci++, compptr++) {
-        mainp->buffer[ci] = (*cinfo->mem->access_virt_sarray)
-          ((j_common_ptr) cinfo, mainp->whole_image[ci], mainp->cur_iMCU_row *
-           ((JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size)),
-           (JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size),
-           writing);
+          ci++, compptr++) {
+       mainp->buffer[ci] = (*cinfo->mem->access_virt_sarray)
+         ((j_common_ptr) cinfo, mainp->whole_image[ci], mainp->cur_iMCU_row *
+          ((JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size)),
+          (JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size),
+          writing);
       }
       /* In a read pass, pretend we just read some source data. */
       if (! writing) {
-        *in_row_ctr += (JDIMENSION)
-          (cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size);
-        mainp->rowgroup_ctr = (JDIMENSION) cinfo->min_DCT_v_scaled_size;
+       *in_row_ctr += (JDIMENSION)
+         (cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size);
+       mainp->rowgroup_ctr = (JDIMENSION) cinfo->min_DCT_v_scaled_size;
       }
     }
 
@@ -199,35 +199,35 @@ process_data_buffer_main (j_compress_ptr cinfo,
     /* 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,
-                                        mainp->buffer, &mainp->rowgroup_ctr,
-                                        (JDIMENSION) cinfo->min_DCT_v_scaled_size);
+                                       input_buf, in_row_ctr, in_rows_avail,
+                                       mainp->buffer, &mainp->rowgroup_ctr,
+                                       (JDIMENSION) cinfo->min_DCT_v_scaled_size);
       /* Return to application if we need more data to fill the iMCU row. */
       if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
-        return;
+       return;
     }
 
     /* Emit data, unless this is a sink-only pass. */
     if (mainp->pass_mode != JBUF_SAVE_SOURCE) {
       if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
-        /* 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 (! mainp->suspended) {
-          (*in_row_ctr)--;
-          mainp->suspended = TRUE;
-        }
-        return;
+       /* 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 (! mainp->suspended) {
+         (*in_row_ctr)--;
+         mainp->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 (mainp->suspended) {
-        (*in_row_ctr)++;
-        mainp->suspended = FALSE;
+       (*in_row_ctr)++;
+       mainp->suspended = FALSE;
       }
     }
 
@@ -253,7 +253,7 @@ jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
 
   mainp = (my_main_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_main_controller));
+                               SIZEOF(my_main_controller));
   cinfo->main = &mainp->pub;
   mainp->pub.start_pass = start_pass_main;
 
@@ -269,14 +269,14 @@ jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
     /* Allocate a full-image virtual array for each component */
     /* Note we pad the bottom to a multiple of the iMCU height */
     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
-         ci++, compptr++) {
+        ci++, compptr++) {
       mainp->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
-        ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
-         compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
-         ((JDIMENSION) jround_up((long) compptr->height_in_blocks,
-                                 (long) compptr->v_samp_factor)) *
-         ((JDIMENSION) cinfo->min_DCT_v_scaled_size),
-         (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
+       ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+        compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
+        ((JDIMENSION) jround_up((long) compptr->height_in_blocks,
+                                (long) compptr->v_samp_factor)) *
+        ((JDIMENSION) cinfo->min_DCT_v_scaled_size),
+        (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
     }
 #else
     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
@@ -287,11 +287,11 @@ jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
 #endif
     /* Allocate a strip buffer for each component */
     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
-         ci++, compptr++) {
+        ci++, compptr++) {
       mainp->buffer[ci] = (*cinfo->mem->alloc_sarray)
-        ((j_common_ptr) cinfo, JPOOL_IMAGE,
-         compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
-         (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
+       ((j_common_ptr) cinfo, JPOOL_IMAGE,
+        compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
+        (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
     }
   }
 }
index 711a3b2..ca2bb39 100644 (file)
@@ -2,7 +2,7 @@
  * jcmarker.c
  *
  * Copyright (C) 1991-1998, Thomas G. Lane.
- * Modified 2003-2012 by Guido Vollbeding.
+ * Modified 2003-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -172,7 +172,7 @@ emit_dqt (j_compress_ptr cinfo, int index)
       /* The table entries must be emitted in zigzag order. */
       unsigned int qval = qtbl->quantval[cinfo->natural_order[i]];
       if (prec)
-        emit_byte(cinfo, (int) (qval >> 8));
+       emit_byte(cinfo, (int) (qval >> 8));
       emit_byte(cinfo, (int) (qval & 0xFF));
     }
 
@@ -189,7 +189,7 @@ emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
 {
   JHUFF_TBL * htbl;
   int length, i;
-
+  
   if (is_ac) {
     htbl = cinfo->ac_huff_tbl_ptrs[index];
     index += 0x10;             /* output index has AC bit set */
@@ -199,23 +199,23 @@ emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
 
   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;
   }
 }
@@ -257,12 +257,12 @@ emit_dac (j_compress_ptr cinfo)
 
     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));
+       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]);
+       emit_byte(cinfo, i + 0x10);
+       emit_byte(cinfo, cinfo->arith_ac_K[i]);
       }
     }
   }
@@ -275,7 +275,7 @@ 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);
@@ -292,7 +292,7 @@ emit_lse_ict (j_compress_ptr cinfo)
     ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
 
   emit_marker(cinfo, M_JPG8);
-
+  
   emit_2bytes(cinfo, 24);      /* fixed length */
 
   emit_byte(cinfo, 0x0D);      /* ID inverse transform specification */
@@ -319,9 +319,9 @@ emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
 {
   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 */
@@ -350,13 +350,13 @@ emit_sos (j_compress_ptr cinfo)
 {
   int i, td, ta;
   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);
@@ -384,9 +384,9 @@ emit_pseudo_sos (j_compress_ptr cinfo)
 /* Emit a pseudo SOS marker */
 {
   emit_marker(cinfo, M_SOS);
-
+  
   emit_2bytes(cinfo, 2 + 1 + 3); /* length */
-
+  
   emit_byte(cinfo, 0); /* Ns */
 
   emit_byte(cinfo, 0); /* Ss */
@@ -410,9 +410,9 @@ emit_jfif_app0 (j_compress_ptr cinfo)
    * 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" */
@@ -449,9 +449,9 @@ emit_adobe_app14 (j_compress_ptr cinfo)
    * 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" */
@@ -508,8 +508,8 @@ write_marker_byte (j_compress_ptr cinfo, int val)
  * 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
+ * when using YCbCr or grayscale data.  The JFIF marker is also used
+ * for other standard JPEG colorspaces.  The Adobe marker is helpful
  * to distinguish RGB, CMYK, and YCCK colorspaces.
  * Note that an application can write additional header markers after
  * jpeg_start_compress returns.
@@ -547,7 +547,7 @@ 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.
    */
@@ -567,9 +567,9 @@ write_frame_header (j_compress_ptr cinfo)
   } else {
     is_baseline = TRUE;
     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
-         ci++, compptr++) {
+        ci++, compptr++) {
       if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
-        is_baseline = FALSE;
+       is_baseline = FALSE;
     }
     if (prec && is_baseline) {
       is_baseline = FALSE;
@@ -630,10 +630,10 @@ write_scan_header (j_compress_ptr cinfo)
       compptr = cinfo->cur_comp_info[i];
       /* DC needs no table for refinement scan */
       if (cinfo->Ss == 0 && cinfo->Ah == 0)
-        emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
+       emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
       /* AC needs no table when not present */
       if (cinfo->Se)
-        emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
+       emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
     }
   }
 
@@ -682,9 +682,9 @@ write_tables_only (j_compress_ptr cinfo)
   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);
+       emit_dht(cinfo, i, FALSE);
       if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
-        emit_dht(cinfo, i, TRUE);
+       emit_dht(cinfo, i, TRUE);
     }
   }
 
@@ -704,7 +704,7 @@ jinit_marker_writer (j_compress_ptr cinfo)
   /* Create the subobject */
   marker = (my_marker_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_marker_writer));
+                               SIZEOF(my_marker_writer));
   cinfo->marker = &marker->pub;
   /* Initialize method pointers */
   marker->pub.write_file_header = write_file_header;
index a34865d..2a8ae63 100644 (file)
@@ -2,13 +2,13 @@
  * jcmaster.c
  *
  * Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 2003-2011 by Guido Vollbeding.
+ * Modified 2003-2013 by Guido Vollbeding.
  * 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 master control logic for the JPEG compressor.
  * These routines are concerned with parameter validation, initial setup,
- * and inter-pass control (determining the number of passes and the work
+ * and inter-pass control (determining the number of passes and the work 
  * to be done in each pass).
  */
 
@@ -20,9 +20,9 @@
 /* Private state */
 
 typedef enum {
-        main_pass,             /* input data, also do first output step */
-        huff_opt_pass,         /* Huffman code optimization pass */
-        output_pass            /* data output pass */
+       main_pass,              /* input data, also do first output step */
+       huff_opt_pass,          /* Huffman code optimization pass */
+       output_pass             /* data output pass */
 } c_pass_type;
 
 typedef struct {
@@ -210,7 +210,7 @@ jpeg_calc_trans_dimensions (j_compress_ptr cinfo)
 {
   if (cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size)
     ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
-             cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size);
+            cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size);
 
   cinfo->block_size = cinfo->min_DCT_h_scaled_size;
 }
@@ -222,8 +222,6 @@ initial_setup (j_compress_ptr cinfo, boolean transcode_only)
 {
   int ci, ssize;
   jpeg_component_info *compptr;
-  long samplesperrow;
-  JDIMENSION jd_samplesperrow;
 
   if (transcode_only)
     jpeg_calc_trans_dimensions(cinfo);
@@ -251,7 +249,7 @@ initial_setup (j_compress_ptr cinfo, boolean transcode_only)
 
   /* Sanity check on image dimensions */
   if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 ||
-      cinfo->num_components <= 0 || cinfo->input_components <= 0)
+      cinfo->num_components <= 0)
     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
 
   /* Make sure image isn't bigger than I can handle */
@@ -259,20 +257,14 @@ initial_setup (j_compress_ptr cinfo, boolean transcode_only)
       (long) cinfo->jpeg_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)
+  /* Only 8 to 12 bits data precision are supported for DCT based JPEG */
+  if (cinfo->data_precision < 8 || cinfo->data_precision > 12)
     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);
+            MAX_COMPONENTS);
 
   /* Compute maximum sampling factors; check factor validity */
   cinfo->max_h_samp_factor = 1;
@@ -280,12 +272,12 @@ initial_setup (j_compress_ptr cinfo, boolean transcode_only)
   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)
+       compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
       ERREXIT(cinfo, JERR_BAD_SAMPLING);
     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
-                                   compptr->h_samp_factor);
+                                  compptr->h_samp_factor);
     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
-                                   compptr->v_samp_factor);
+                                  compptr->v_samp_factor);
   }
 
   /* Compute dimensions of components */
@@ -301,8 +293,8 @@ initial_setup (j_compress_ptr cinfo, boolean transcode_only)
     ssize = 1;
 #ifdef DCT_SCALING_SUPPORTED
     while (cinfo->min_DCT_h_scaled_size * ssize <=
-           (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
-           (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
+          (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
+          (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
       ssize = ssize * 2;
     }
 #endif
@@ -310,8 +302,8 @@ initial_setup (j_compress_ptr cinfo, boolean transcode_only)
     ssize = 1;
 #ifdef DCT_SCALING_SUPPORTED
     while (cinfo->min_DCT_v_scaled_size * ssize <=
-           (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
-           (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
+          (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
+          (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
       ssize = ssize * 2;
     }
 #endif
@@ -319,28 +311,30 @@ initial_setup (j_compress_ptr cinfo, boolean transcode_only)
 
     /* We don't support DCT ratios larger than 2. */
     if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
-        compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
+       compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
     else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
-        compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
+       compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
 
     /* Size in DCT blocks */
     compptr->width_in_blocks = (JDIMENSION)
       jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
-                    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
+                   (long) (cinfo->max_h_samp_factor * cinfo->block_size));
     compptr->height_in_blocks = (JDIMENSION)
       jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
-                    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
+                   (long) (cinfo->max_v_samp_factor * cinfo->block_size));
     /* Size in samples */
     compptr->downsampled_width = (JDIMENSION)
       jdiv_round_up((long) cinfo->jpeg_width *
-                    (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
-                    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
+                   (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
+                   (long) (cinfo->max_h_samp_factor * cinfo->block_size));
     compptr->downsampled_height = (JDIMENSION)
       jdiv_round_up((long) cinfo->jpeg_height *
-                    (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
-                    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
-    /* Mark component needed (this flag isn't actually used for compression) */
-    compptr->component_needed = TRUE;
+                   (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
+                   (long) (cinfo->max_v_samp_factor * cinfo->block_size));
+    /* Don't need quantization scale after DCT,
+     * until color conversion says otherwise.
+     */
+    compptr->component_needed = FALSE;
   }
 
   /* Compute number of fully interleaved MCU rows (number of times that
@@ -348,7 +342,7 @@ initial_setup (j_compress_ptr cinfo, boolean transcode_only)
    */
   cinfo->total_iMCU_rows = (JDIMENSION)
     jdiv_round_up((long) cinfo->jpeg_height,
-                  (long) (cinfo->max_v_samp_factor * cinfo->block_size));
+                 (long) (cinfo->max_v_samp_factor * cinfo->block_size));
 }
 
 
@@ -381,15 +375,15 @@ validate_script (j_compress_ptr cinfo)
 #ifdef C_PROGRESSIVE_SUPPORTED
     cinfo->progressive_mode = TRUE;
     last_bitpos_ptr = & last_bitpos[0][0];
-    for (ci = 0; ci < cinfo->num_components; ci++)
+    for (ci = 0; ci < cinfo->num_components; ci++) 
       for (coefi = 0; coefi < DCTSIZE2; coefi++)
-        *last_bitpos_ptr++ = -1;
+       *last_bitpos_ptr++ = -1;
 #else
     ERREXIT(cinfo, JERR_NOT_COMPILED);
 #endif
   } else {
     cinfo->progressive_mode = FALSE;
-    for (ci = 0; ci < cinfo->num_components; ci++)
+    for (ci = 0; ci < cinfo->num_components; ci++) 
       component_sent[ci] = FALSE;
   }
 
@@ -401,10 +395,10 @@ validate_script (j_compress_ptr cinfo)
     for (ci = 0; ci < ncomps; ci++) {
       thisi = scanptr->component_index[ci];
       if (thisi < 0 || thisi >= cinfo->num_components)
-        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
+       ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
       /* Components must appear in SOF order within each scan */
       if (ci > 0 && thisi <= scanptr->component_index[ci-1])
-        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
+       ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
     }
     /* Validate progression parameters */
     Ss = scanptr->Ss;
@@ -426,43 +420,43 @@ validate_script (j_compress_ptr cinfo)
 #define MAX_AH_AL 13
 #endif
       if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
-          Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
-        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+         Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
+       ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
       if (Ss == 0) {
-        if (Se != 0)           /* DC and AC together not OK */
-          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+       if (Se != 0)            /* DC and AC together not OK */
+         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
       } else {
-        if (ncomps != 1)       /* AC scans must be for only one component */
-          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+       if (ncomps != 1)        /* AC scans must be for only one component */
+         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
       }
       for (ci = 0; ci < ncomps; ci++) {
-        last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
-        if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
-          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
-        for (coefi = Ss; coefi <= Se; coefi++) {
-          if (last_bitpos_ptr[coefi] < 0) {
-            /* first scan of this coefficient */
-            if (Ah != 0)
-              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
-          } else {
-            /* not first scan */
-            if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
-              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
-          }
-          last_bitpos_ptr[coefi] = Al;
-        }
+       last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
+       if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
+         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+       for (coefi = Ss; coefi <= Se; coefi++) {
+         if (last_bitpos_ptr[coefi] < 0) {
+           /* first scan of this coefficient */
+           if (Ah != 0)
+             ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+         } else {
+           /* not first scan */
+           if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
+             ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+         }
+         last_bitpos_ptr[coefi] = Al;
+       }
       }
 #endif
     } else {
       /* For sequential JPEG, all progression parameters must be these: */
       if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
-        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+       ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
       /* Make sure components are not sent twice */
       for (ci = 0; ci < ncomps; ci++) {
-        thisi = scanptr->component_index[ci];
-        if (component_sent[thisi])
-          ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
-        component_sent[thisi] = TRUE;
+       thisi = scanptr->component_index[ci];
+       if (component_sent[thisi])
+         ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
+       component_sent[thisi] = TRUE;
       }
     }
   }
@@ -477,13 +471,13 @@ validate_script (j_compress_ptr cinfo)
      */
     for (ci = 0; ci < cinfo->num_components; ci++) {
       if (last_bitpos[ci][0] < 0)
-        ERREXIT(cinfo, JERR_MISSING_DATA);
+       ERREXIT(cinfo, JERR_MISSING_DATA);
     }
 #endif
   } else {
     for (ci = 0; ci < cinfo->num_components; ci++) {
       if (! component_sent[ci])
-        ERREXIT(cinfo, JERR_MISSING_DATA);
+       ERREXIT(cinfo, JERR_MISSING_DATA);
     }
   }
 }
@@ -539,7 +533,7 @@ select_scan_parameters (j_compress_ptr cinfo)
     cinfo->comps_in_scan = scanptr->comps_in_scan;
     for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
       cinfo->cur_comp_info[ci] =
-        &cinfo->comp_info[scanptr->component_index[ci]];
+       &cinfo->comp_info[scanptr->component_index[ci]];
     }
     if (cinfo->progressive_mode) {
       cinfo->Ss = scanptr->Ss;
@@ -555,7 +549,7 @@ select_scan_parameters (j_compress_ptr cinfo)
     /* Prepare for single sequential-JPEG scan containing all components */
     if (cinfo->num_components > MAX_COMPS_IN_SCAN)
       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
-               MAX_COMPS_IN_SCAN);
+              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];
@@ -575,16 +569,16 @@ per_scan_setup (j_compress_ptr cinfo)
 {
   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;
@@ -597,28 +591,28 @@ per_scan_setup (j_compress_ptr cinfo)
     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
     if (tmp == 0) tmp = compptr->v_samp_factor;
     compptr->last_row_height = tmp;
-
+    
     /* 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);
-
+              MAX_COMPS_IN_SCAN);
+    
     /* Overall image size in MCUs */
     cinfo->MCUs_per_row = (JDIMENSION)
       jdiv_round_up((long) cinfo->jpeg_width,
-                    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
+                   (long) (cinfo->max_h_samp_factor * cinfo->block_size));
     cinfo->MCU_rows_in_scan = (JDIMENSION)
       jdiv_round_up((long) cinfo->jpeg_height,
-                    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
-
+                   (long) (cinfo->max_v_samp_factor * cinfo->block_size));
+    
     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 */
@@ -636,12 +630,12 @@ per_scan_setup (j_compress_ptr cinfo)
       /* Prepare array describing MCU composition */
       mcublks = compptr->MCU_blocks;
       if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
-        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
+       ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
       while (mcublks-- > 0) {
-        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
+       cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
       }
     }
-
+    
   }
 
   /* Convert restart specified in rows to actual MCU count. */
@@ -681,8 +675,8 @@ prepare_for_pass (j_compress_ptr cinfo)
     (*cinfo->fdct->start_pass) (cinfo);
     (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
     (*cinfo->coef->start_pass) (cinfo,
-                                (master->total_passes > 1 ?
-                                 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
+                               (master->total_passes > 1 ?
+                                JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
     if (cinfo->optimize_coding) {
       /* No immediate data output; postpone writing frame/scan headers */
@@ -810,8 +804,8 @@ jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
 
   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;
+                                 SIZEOF(my_comp_master));
+  cinfo->master = &master->pub;
   master->pub.prepare_for_pass = prepare_for_pass;
   master->pub.pass_startup = pass_startup;
   master->pub.finish_pass = finish_pass_master;
@@ -833,10 +827,14 @@ jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
     cinfo->num_scans = 1;
   }
 
-  if ((cinfo->progressive_mode || cinfo->block_size < DCTSIZE) &&
-      !cinfo->arith_code)                      /*  TEMPORARY HACK ??? */
-    /* assume default tables no good for progressive or downscale mode */
-    cinfo->optimize_coding = TRUE;
+  if (cinfo->optimize_coding)
+    cinfo->arith_code = FALSE; /* disable arithmetic coding */
+  else if (! cinfo->arith_code &&
+          (cinfo->progressive_mode ||
+           (cinfo->block_size > 1 && cinfo->block_size < DCTSIZE)))
+    /* TEMPORARY HACK ??? */
+    /* assume default tables no good for progressive or reduced AC mode */
+    cinfo->optimize_coding = TRUE; /* force Huffman optimization */
 
   /* Initialize my private state */
   if (transcode_only) {
index 7f8e271..e9d33e5 100644 (file)
+/* jconfig.vc --- jconfig.h for Microsoft Visual C++ on Windows 9x or NT. */
+/* This file also works for Borland C++ 32-bit (bcc32) on Windows 9x or NT. */
+/* see jconfig.txt for explanations */
+
 #define HAVE_PROTOTYPES
 #define HAVE_UNSIGNED_CHAR
 #define HAVE_UNSIGNED_SHORT
-
-/* 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.
- */
+/* #define void char */
+/* #define const */
 #undef CHAR_IS_UNSIGNED
-
-#if defined __MINGW__ || defined __MINGW32__ || (!defined WIN32 && !defined _WIN32)
-/* 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
-#endif
-
-/* 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_FAR_POINTERS       /* we presume a 32-bit flat memory model */
 #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
 
-/* Define "boolean" as unsigned char, not int, on Windows systems.
- */
-#ifdef _WIN32
+/* Define "boolean" as unsigned char, not enum, per Windows custom */
 #ifndef __RPCNDR_H__           /* don't conflict if rpcndr.h already read */
 typedef unsigned char boolean;
 #endif
-#define HAVE_BOOLEAN           /* prevent jmorecfg.h from redefining it */
+#ifndef FALSE                  /* in case these macros already exist */
+#define FALSE  0               /* values of boolean */
 #endif
+#ifndef TRUE
+#define TRUE   1
+#endif
+#define HAVE_BOOLEAN           /* prevent jmorecfg.h from redefining it */
 
 
-/*
- * 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
 
-/* These are for configuring the JPEG memory manager. */
-#define DEFAULT_MAX_MEM 1073741824 /*1Gb*/
-
-#if !defined WIN32 && !defined _WIN32
-#define INLINE __inline__
-#undef NO_MKTEMP
-#endif
-
 #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.
- */
-#if defined WIN32 || defined _WIN32
 #define TWO_FILE_COMMANDLINE   /* optional */
 #define USE_SETMODE            /* Microsoft has setmode() */
-#else
-#undef 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.
- */
 #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
-
+#undef PROGRESS_REPORT         /* optional */
 
 #endif /* JPEG_CJPEG_DJPEG */
index a99a0d4..4b2bee2 100644 (file)
@@ -2,7 +2,7 @@
  * jcparam.c
  *
  * Copyright (C) 1991-1998, Thomas G. Lane.
- * Modified 2003-2012 by Guido Vollbeding.
+ * Modified 2003-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -22,8 +22,8 @@
 
 GLOBAL(void)
 jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
-                      const unsigned int *basic_table,
-                      int scale_factor, boolean force_baseline)
+                     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
@@ -96,15 +96,15 @@ jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline)
 {
   /* Set up two quantization tables using the specified scaling */
   jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
-                       cinfo->q_scale_factor[0], force_baseline);
+                      cinfo->q_scale_factor[0], force_baseline);
   jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
-                       cinfo->q_scale_factor[1], force_baseline);
+                      cinfo->q_scale_factor[1], force_baseline);
 }
 
 
 GLOBAL(void)
 jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
-                         boolean force_baseline)
+                        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
@@ -113,9 +113,9 @@ jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
 {
   /* Set up two quantization tables using the specified scaling */
   jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
-                       scale_factor, force_baseline);
+                      scale_factor, force_baseline);
   jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
-                       scale_factor, force_baseline);
+                      scale_factor, force_baseline);
 }
 
 
@@ -167,7 +167,7 @@ jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
 
 LOCAL(void)
 add_huff_table (j_compress_ptr cinfo,
-                JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
+               JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
 /* Define a Huffman table */
 {
   int nsymbols, len;
@@ -204,12 +204,12 @@ std_huff_tables (j_compress_ptr cinfo)
     { /* 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[] =
@@ -234,7 +234,7 @@ std_huff_tables (j_compress_ptr cinfo)
       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[] =
@@ -259,15 +259,15 @@ std_huff_tables (j_compress_ptr cinfo)
       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);
+                bits_dc_luminance, val_dc_luminance);
   add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
-                 bits_ac_luminance, val_ac_luminance);
+                bits_ac_luminance, val_ac_luminance);
   add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
-                 bits_dc_chrominance, val_dc_chrominance);
+                bits_dc_chrominance, val_dc_chrominance);
   add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
-                 bits_ac_chrominance, val_ac_chrominance);
+                bits_ac_chrominance, val_ac_chrominance);
 }
 
 
@@ -297,7 +297,7 @@ jpeg_set_defaults (j_compress_ptr cinfo)
   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));
+                                 MAX_COMPONENTS * SIZEOF(jpeg_component_info));
 
   /* Initialize everything not dependent on the color space */
 
@@ -323,18 +323,17 @@ jpeg_set_defaults (j_compress_ptr cinfo)
   /* 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;
+  /* The standard Huffman tables are only valid for 8-bit data precision.
+   * If the precision is higher, use arithmetic coding.
+   * (Alternatively, using Huffman coding would be possible with forcing
+   * optimization on so that usable tables will be computed, or by
+   * supplying default tables that are valid for the desired precision.)
+   * Otherwise, use Huffman coding by default.
+   */
+  cinfo->arith_code = cinfo->data_precision > 8 ? TRUE : FALSE;
 
   /* 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;
@@ -360,6 +359,9 @@ jpeg_set_defaults (j_compress_ptr cinfo)
    * JFIF_minor_version to 2.  We could probably get away with just defaulting
    * to 1.02, but there may still be some decoders in use that will complain
    * about that; saying 1.01 should minimize compatibility problems.
+   *
+   * For wide gamut colorspaces (BG_RGB and BG_YCC), the major version will be
+   * overridden by jpeg_set_colorspace and set to 2.
    */
   cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
   cinfo->JFIF_minor_version = 1;
@@ -384,6 +386,9 @@ GLOBAL(void)
 jpeg_default_colorspace (j_compress_ptr cinfo)
 {
   switch (cinfo->in_color_space) {
+  case JCS_UNKNOWN:
+    jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
+    break;
   case JCS_GRAYSCALE:
     jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
     break;
@@ -399,8 +404,12 @@ jpeg_default_colorspace (j_compress_ptr cinfo)
   case JCS_YCCK:
     jpeg_set_colorspace(cinfo, JCS_YCCK);
     break;
-  case JCS_UNKNOWN:
-    jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
+  case JCS_BG_RGB:
+    /* No translation for now -- conversion to BG_YCC not yet supportet */
+    jpeg_set_colorspace(cinfo, JCS_BG_RGB);
+    break;
+  case JCS_BG_YCC:
+    jpeg_set_colorspace(cinfo, JCS_BG_YCC);
     break;
   default:
     ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
@@ -441,29 +450,40 @@ jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
   cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
 
   switch (colorspace) {
+  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;
   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);
+    SET_COMP(0, 0x01, 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, 0x52 /* 'R' */, 1,1, 0, 0,0);
-    SET_COMP(1, 0x47 /* 'G' */, 1,1, 0,
-                cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
-                cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);
-    SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
+    SET_COMP(0, 0x52 /* 'R' */, 1,1, 0,
+               cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
+               cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);
+    SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
+    SET_COMP(2, 0x42 /* 'B' */, 1,1, 0,
+               cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
+               cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 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);
+    SET_COMP(0, 0x01, 2,2, 0, 0,0);
+    SET_COMP(1, 0x02, 1,1, 1, 1,1);
+    SET_COMP(2, 0x03, 1,1, 1, 1,1);
     break;
   case JCS_CMYK:
     cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
@@ -476,19 +496,33 @@ jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
   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);
+    SET_COMP(0, 0x01, 2,2, 0, 0,0);
+    SET_COMP(1, 0x02, 1,1, 1, 1,1);
+    SET_COMP(2, 0x03, 1,1, 1, 1,1);
+    SET_COMP(3, 0x04, 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);
-    }
+  case JCS_BG_RGB:
+    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
+    cinfo->JFIF_major_version = 2;   /* Set JFIF major version = 2 */
+    cinfo->num_components = 3;
+    /* Add offset 0x20 to the normal R/G/B component IDs */
+    SET_COMP(0, 0x72 /* 'r' */, 1,1, 0,
+               cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
+               cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);
+    SET_COMP(1, 0x67 /* 'g' */, 1,1, 0, 0,0);
+    SET_COMP(2, 0x62 /* 'b' */, 1,1, 0,
+               cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
+               cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);
+    break;
+  case JCS_BG_YCC:
+    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
+    cinfo->JFIF_major_version = 2;   /* Set JFIF major version = 2 */
+    cinfo->num_components = 3;
+    /* Add offset 0x20 to the normal Cb/Cr component IDs */
+    /* We default to 2x2 subsamples of chrominance */
+    SET_COMP(0, 0x01, 2,2, 0, 0,0);
+    SET_COMP(1, 0x22, 1,1, 1, 1,1);
+    SET_COMP(2, 0x23, 1,1, 1, 1,1);
     break;
   default:
     ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
@@ -500,7 +534,7 @@ jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
 
 LOCAL(jpeg_scan_info *)
 fill_a_scan (jpeg_scan_info * scanptr, int ci,
-             int Ss, int Se, int Ah, int Al)
+            int Ss, int Se, int Ah, int Al)
 /* Support routine: generate one scan for specified component */
 {
   scanptr->comps_in_scan = 1;
@@ -515,7 +549,7 @@ fill_a_scan (jpeg_scan_info * scanptr, int ci,
 
 LOCAL(jpeg_scan_info *)
 fill_scans (jpeg_scan_info * scanptr, int ncomps,
-            int Ss, int Se, int Ah, int Al)
+           int Ss, int Se, int Ah, int Al)
 /* Support routine: generate one scan for each component */
 {
   int ci;
@@ -572,8 +606,10 @@ jpeg_simple_progression (j_compress_ptr cinfo)
     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
 
   /* Figure space needed for script.  Calculation must match code below! */
-  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
-    /* Custom script for YCbCr color images. */
+  if (ncomps == 3 &&
+      (cinfo->jpeg_color_space == JCS_YCbCr ||
+       cinfo->jpeg_color_space == JCS_BG_YCC)) {
+    /* Custom script for YCC color images. */
     nscans = 10;
   } else {
     /* All-purpose script for other color spaces. */
@@ -588,20 +624,22 @@ jpeg_simple_progression (j_compress_ptr cinfo)
    * multiple compressions without changing the settings.  To avoid a memory
    * leak if jpeg_simple_progression is called repeatedly for the same JPEG
    * object, we try to re-use previously allocated space, and we allocate
-   * enough space to handle YCbCr even if initially asked for grayscale.
+   * enough space to handle YCC even if initially asked for grayscale.
    */
   if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
     cinfo->script_space_size = MAX(nscans, 10);
     cinfo->script_space = (jpeg_scan_info *)
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
-                        cinfo->script_space_size * SIZEOF(jpeg_scan_info));
+                       cinfo->script_space_size * SIZEOF(jpeg_scan_info));
   }
   scanptr = cinfo->script_space;
   cinfo->scan_info = scanptr;
   cinfo->num_scans = nscans;
 
-  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
-    /* Custom script for YCbCr color images. */
+  if (ncomps == 3 &&
+      (cinfo->jpeg_color_space == JCS_YCbCr ||
+       cinfo->jpeg_color_space == JCS_BG_YCC)) {
+    /* Custom script for YCC color images. */
     /* Initial DC scan */
     scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
     /* Initial AC scan: get some luma data out in a hurry */
index 1b4e14e..be44cc4 100644 (file)
@@ -104,13 +104,13 @@ start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
 
 LOCAL(void)
 expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
-                    int input_rows, int output_rows)
+                   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);
+                     1, num_cols);
   }
 }
 
@@ -126,10 +126,10 @@ expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
 
 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)
+                 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;
@@ -137,32 +137,32 @@ pre_process_data (j_compress_ptr cinfo,
   jpeg_component_info * compptr;
 
   while (*in_row_ctr < in_rows_avail &&
-         *out_row_group_ctr < out_row_groups_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);
+                                      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) {
+       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);
+       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->color_buf, (JDIMENSION) 0,
+                                       output_buf, *out_row_group_ctr);
       prep->next_buf_row = 0;
       (*out_row_group_ctr)++;
     }
@@ -170,15 +170,15 @@ pre_process_data (j_compress_ptr cinfo,
      * 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) {
+       *out_row_group_ctr < out_row_groups_avail) {
       for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
-           ci++, compptr++) {
-        numrows = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
-                  cinfo->min_DCT_v_scaled_size;
-        expand_bottom_edge(output_buf[ci],
-                           compptr->width_in_blocks * compptr->DCT_h_scaled_size,
-                           (int) (*out_row_group_ctr * numrows),
-                           (int) (out_row_groups_avail * numrows));
+          ci++, compptr++) {
+       numrows = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
+                 cinfo->min_DCT_v_scaled_size;
+       expand_bottom_edge(output_buf[ci],
+                          compptr->width_in_blocks * compptr->DCT_h_scaled_size,
+                          (int) (*out_row_group_ctr * numrows),
+                          (int) (out_row_groups_avail * numrows));
       }
       *out_row_group_ctr = out_row_groups_avail;
       break;                   /* can exit outer loop without test */
@@ -195,10 +195,10 @@ pre_process_data (j_compress_ptr cinfo,
 
 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)
+                    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;
@@ -212,19 +212,19 @@ pre_process_context (j_compress_ptr cinfo,
       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);
+                                        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);
-          }
-        }
+       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;
@@ -232,29 +232,29 @@ pre_process_context (j_compress_ptr cinfo,
     } else {
       /* Return for more data, unless we are at the bottom of the image. */
       if (prep->rows_to_go != 0)
-        break;
+       break;
       /* When at bottom of image, pad to fill the conversion buffer. */
       if (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;
+       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);
+                                       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;
+       prep->this_row_group = 0;
       if (prep->next_buf_row >= buf_height)
-        prep->next_buf_row = 0;
+       prep->next_buf_row = 0;
       prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
     }
   }
@@ -279,8 +279,8 @@ create_context_buffer (j_compress_ptr cinfo)
    */
   fake_buffer = (JSAMPARRAY)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (cinfo->num_components * 5 * rgroup_height) *
-                                SIZEOF(JSAMPROW));
+                               (cinfo->num_components * 5 * rgroup_height) *
+                               SIZEOF(JSAMPROW));
 
   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
        ci++, compptr++) {
@@ -291,12 +291,12 @@ create_context_buffer (j_compress_ptr cinfo)
     true_buffer = (*cinfo->mem->alloc_sarray)
       ((j_common_ptr) cinfo, JPOOL_IMAGE,
        (JDIMENSION) (((long) compptr->width_in_blocks *
-                      cinfo->min_DCT_h_scaled_size *
-                      cinfo->max_h_samp_factor) / compptr->h_samp_factor),
+                     cinfo->min_DCT_h_scaled_size *
+                     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));
+           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];
@@ -326,7 +326,7 @@ jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
 
   prep = (my_prep_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_prep_controller));
+                               SIZEOF(my_prep_controller));
   cinfo->prep = (struct jpeg_c_prep_controller *) prep;
   prep->pub.start_pass = start_pass_prep;
 
@@ -346,13 +346,13 @@ jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
     /* 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++) {
+        ci++, compptr++) {
       prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
-        ((j_common_ptr) cinfo, JPOOL_IMAGE,
-         (JDIMENSION) (((long) compptr->width_in_blocks *
-                        cinfo->min_DCT_h_scaled_size *
-                        cinfo->max_h_samp_factor) / compptr->h_samp_factor),
-         (JDIMENSION) cinfo->max_v_samp_factor);
+       ((j_common_ptr) cinfo, JPOOL_IMAGE,
+        (JDIMENSION) (((long) compptr->width_in_blocks *
+                       cinfo->min_DCT_h_scaled_size *
+                       cinfo->max_h_samp_factor) / compptr->h_samp_factor),
+        (JDIMENSION) cinfo->max_v_samp_factor);
     }
   }
 }
index 306bfd5..4d36f85 100644 (file)
@@ -52,8 +52,8 @@
 
 /* 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));
+               (j_compress_ptr cinfo, jpeg_component_info * compptr,
+                JSAMPARRAY input_data, JSAMPARRAY output_data));
 
 /* Private subobject */
 
@@ -94,7 +94,7 @@ start_pass_downsample (j_compress_ptr cinfo)
 
 LOCAL(void)
 expand_right_edge (JSAMPARRAY image_data, int num_rows,
-                   JDIMENSION input_cols, JDIMENSION output_cols)
+                  JDIMENSION input_cols, JDIMENSION output_cols)
 {
   register JSAMPROW ptr;
   register JSAMPLE pixval;
@@ -107,7 +107,7 @@ expand_right_edge (JSAMPARRAY image_data, int num_rows,
       ptr = image_data[row] + input_cols;
       pixval = ptr[-1];                /* don't need GETJSAMPLE() here */
       for (count = numcols; count > 0; count--)
-        *ptr++ = pixval;
+       *ptr++ = pixval;
     }
   }
 }
@@ -121,8 +121,8 @@ expand_right_edge (JSAMPARRAY image_data, int num_rows,
 
 METHODDEF(void)
 sep_downsample (j_compress_ptr cinfo,
-                JSAMPIMAGE input_buf, JDIMENSION in_row_index,
-                JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
+               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;
@@ -133,7 +133,7 @@ sep_downsample (j_compress_ptr cinfo,
        ci++, compptr++) {
     in_ptr = input_buf[ci] + in_row_index;
     out_ptr = output_buf[ci] +
-              (out_row_group_index * downsample->rowgroup_height[ci]);
+             (out_row_group_index * downsample->rowgroup_height[ci]);
     (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
   }
 }
@@ -148,7 +148,7 @@ sep_downsample (j_compress_ptr cinfo,
 
 METHODDEF(void)
 int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
-                JSAMPARRAY input_data, JSAMPARRAY output_data)
+               JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
   my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
   int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
@@ -167,19 +167,19 @@ int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
    * efficient.
    */
   expand_right_edge(input_data, cinfo->max_v_samp_factor,
-                    cinfo->image_width, output_cols * h_expand);
+                   cinfo->image_width, output_cols * h_expand);
 
   inrow = outrow = 0;
   while (inrow < cinfo->max_v_samp_factor) {
     outptr = output_data[outrow];
     for (outcol = 0, outcol_h = 0; outcol < output_cols;
-         outcol++, outcol_h += h_expand) {
+        outcol++, outcol_h += h_expand) {
       outvalue = 0;
       for (v = 0; v < v_expand; v++) {
-        inptr = input_data[inrow+v] + outcol_h;
-        for (h = 0; h < h_expand; h++) {
-          outvalue += (INT32) GETJSAMPLE(*inptr++);
-        }
+       inptr = input_data[inrow+v] + outcol_h;
+       for (h = 0; h < h_expand; h++) {
+         outvalue += (INT32) GETJSAMPLE(*inptr++);
+       }
       }
       *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
     }
@@ -197,14 +197,14 @@ int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
 
 METHODDEF(void)
 fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
-                     JSAMPARRAY input_data, JSAMPARRAY output_data)
+                    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);
+                   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 * compptr->DCT_h_scaled_size);
+                   compptr->width_in_blocks * compptr->DCT_h_scaled_size);
 }
 
 
@@ -222,7 +222,7 @@ fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
 
 METHODDEF(void)
 h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
-                 JSAMPARRAY input_data, JSAMPARRAY output_data)
+                JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
   int inrow;
   JDIMENSION outcol;
@@ -235,7 +235,7 @@ h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
    * efficient.
    */
   expand_right_edge(input_data, cinfo->max_v_samp_factor,
-                    cinfo->image_width, output_cols * 2);
+                   cinfo->image_width, output_cols * 2);
 
   for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
     outptr = output_data[inrow];
@@ -243,7 +243,7 @@ h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
     bias = 0;                  /* bias = 0,1,0,1,... for successive samples */
     for (outcol = 0; outcol < output_cols; outcol++) {
       *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
-                              + bias) >> 1);
+                             + bias) >> 1);
       bias ^= 1;               /* 0=>1, 1=>0 */
       inptr += 2;
     }
@@ -259,7 +259,7 @@ h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
 
 METHODDEF(void)
 h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
-                 JSAMPARRAY input_data, JSAMPARRAY output_data)
+                JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
   int inrow, outrow;
   JDIMENSION outcol;
@@ -272,7 +272,7 @@ h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
    * efficient.
    */
   expand_right_edge(input_data, cinfo->max_v_samp_factor,
-                    cinfo->image_width, output_cols * 2);
+                   cinfo->image_width, output_cols * 2);
 
   inrow = outrow = 0;
   while (inrow < cinfo->max_v_samp_factor) {
@@ -282,8 +282,8 @@ h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
     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])
-                              + bias) >> 2);
+                             GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
+                             + bias) >> 2);
       bias ^= 3;               /* 1=>2, 2=>1 */
       inptr0 += 2; inptr1 += 2;
     }
@@ -303,7 +303,7 @@ h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
 
 METHODDEF(void)
 h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
-                        JSAMPARRAY input_data, JSAMPARRAY output_data)
+                       JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
   int inrow, outrow;
   JDIMENSION colctr;
@@ -316,7 +316,7 @@ h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
    * efficient.
    */
   expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
-                    cinfo->image_width, output_cols * 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
@@ -344,14 +344,14 @@ h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Special case for first column: pretend column -1 is same as column 0 */
     membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
-                GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
+               GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
     neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
-               GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
-               GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
-               GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
+              GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
+              GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
+              GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
     neighsum += neighsum;
     neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
-                GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
+               GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
     membersum = membersum * memberscale + neighsum * neighscale;
     *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
     inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
@@ -359,17 +359,17 @@ h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
     for (colctr = output_cols - 2; colctr > 0; colctr--) {
       /* sum of pixels directly mapped to this output element */
       membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
-                  GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
+                 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
       /* sum of edge-neighbor pixels */
       neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
-                 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
-                 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
-                 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
+                GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
+                GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
+                GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
       /* The edge-neighbors count twice as much as corner-neighbors */
       neighsum += neighsum;
       /* Add in the corner-neighbors */
       neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
-                  GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
+                 GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
       /* form final output scaled up by 2^16 */
       membersum = membersum * memberscale + neighsum * neighscale;
       /* round, descale and output it */
@@ -379,14 +379,14 @@ h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Special case for last column */
     membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
-                GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
+               GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
     neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
-               GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
-               GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
-               GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
+              GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
+              GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
+              GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
     neighsum += neighsum;
     neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
-                GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
+               GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
     membersum = membersum * memberscale + neighsum * neighscale;
     *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
 
@@ -404,7 +404,7 @@ h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
 
 METHODDEF(void)
 fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
-                            JSAMPARRAY input_data, JSAMPARRAY output_data)
+                           JSAMPARRAY input_data, JSAMPARRAY output_data)
 {
   int inrow;
   JDIMENSION colctr;
@@ -418,7 +418,7 @@ fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
    * efficient.
    */
   expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
-                    cinfo->image_width, output_cols);
+                   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
@@ -437,10 +437,10 @@ fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
 
     /* Special case for first column */
     colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
-             GETJSAMPLE(*inptr);
+            GETJSAMPLE(*inptr);
     membersum = GETJSAMPLE(*inptr++);
     nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
-                 GETJSAMPLE(*inptr);
+                GETJSAMPLE(*inptr);
     neighsum = colsum + (colsum - membersum) + nextcolsum;
     membersum = membersum * memberscale + neighsum * neighscale;
     *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
@@ -450,7 +450,7 @@ fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
       membersum = GETJSAMPLE(*inptr++);
       above_ptr++; below_ptr++;
       nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
-                   GETJSAMPLE(*inptr);
+                  GETJSAMPLE(*inptr);
       neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
       membersum = membersum * memberscale + neighsum * neighscale;
       *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
@@ -485,7 +485,7 @@ jinit_downsampler (j_compress_ptr cinfo)
 
   downsample = (my_downsample_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_downsampler));
+                               SIZEOF(my_downsampler));
   cinfo->downsample = (struct jpeg_downsampler *) downsample;
   downsample->pub.start_pass = start_pass_downsample;
   downsample->pub.downsample = sep_downsample;
@@ -501,35 +501,35 @@ jinit_downsampler (j_compress_ptr cinfo)
      * are to be converted from max_h_samp_factor * max_v_samp_factor pixels.
      */
     h_out_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) /
-                  cinfo->min_DCT_h_scaled_size;
+                 cinfo->min_DCT_h_scaled_size;
     v_out_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
-                  cinfo->min_DCT_v_scaled_size;
+                 cinfo->min_DCT_v_scaled_size;
     h_in_group = cinfo->max_h_samp_factor;
     v_in_group = cinfo->max_v_samp_factor;
     downsample->rowgroup_height[ci] = v_out_group; /* save for use later */
     if (h_in_group == h_out_group && v_in_group == v_out_group) {
 #ifdef INPUT_SMOOTHING_SUPPORTED
       if (cinfo->smoothing_factor) {
-        downsample->methods[ci] = fullsize_smooth_downsample;
-        downsample->pub.need_context_rows = TRUE;
+       downsample->methods[ci] = fullsize_smooth_downsample;
+       downsample->pub.need_context_rows = TRUE;
       } else
 #endif
-        downsample->methods[ci] = fullsize_downsample;
+       downsample->methods[ci] = fullsize_downsample;
     } else if (h_in_group == h_out_group * 2 &&
-               v_in_group == v_out_group) {
+              v_in_group == v_out_group) {
       smoothok = FALSE;
       downsample->methods[ci] = h2v1_downsample;
     } else if (h_in_group == h_out_group * 2 &&
-               v_in_group == v_out_group * 2) {
+              v_in_group == v_out_group * 2) {
 #ifdef INPUT_SMOOTHING_SUPPORTED
       if (cinfo->smoothing_factor) {
-        downsample->methods[ci] = h2v2_smooth_downsample;
-        downsample->pub.need_context_rows = TRUE;
+       downsample->methods[ci] = h2v2_smooth_downsample;
+       downsample->pub.need_context_rows = TRUE;
       } else
 #endif
-        downsample->methods[ci] = h2v2_downsample;
+       downsample->methods[ci] = h2v2_downsample;
     } else if ((h_in_group % h_out_group) == 0 &&
-               (v_in_group % v_out_group) == 0) {
+              (v_in_group % v_out_group) == 0) {
       smoothok = FALSE;
       downsample->methods[ci] = int_downsample;
       downsample->h_expand[ci] = (UINT8) (h_in_group / h_out_group);
index 68a686b..7cd077e 100644 (file)
@@ -2,7 +2,7 @@
  * jctrans.c
  *
  * Copyright (C) 1995-1998, Thomas G. Lane.
- * Modified 2000-2012 by Guido Vollbeding.
+ * Modified 2000-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -18,9 +18,9 @@
 
 /* Forward declarations */
 LOCAL(void) transencode_master_selection
-        JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
+       JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
 LOCAL(void) transencode_coef_controller
-        JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
+       JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
 
 
 /*
@@ -62,7 +62,7 @@ jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
 
 GLOBAL(void)
 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
-                               j_compress_ptr dstinfo)
+                              j_compress_ptr dstinfo)
 {
   JQUANT_TBL ** qtblptr;
   jpeg_component_info *incomp, *outcomp;
@@ -97,10 +97,10 @@ jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
     if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
       qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
       if (*qtblptr == NULL)
-        *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
+       *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
       MEMCOPY((*qtblptr)->quantval,
-              srcinfo->quant_tbl_ptrs[tblno]->quantval,
-              SIZEOF((*qtblptr)->quantval));
+             srcinfo->quant_tbl_ptrs[tblno]->quantval,
+             SIZEOF((*qtblptr)->quantval));
       (*qtblptr)->sent_table = FALSE;
     }
   }
@@ -110,7 +110,7 @@ jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
   dstinfo->num_components = srcinfo->num_components;
   if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
     ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
-             MAX_COMPONENTS);
+            MAX_COMPONENTS);
   for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
        ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
     outcomp->component_id = incomp->component_id;
@@ -123,14 +123,14 @@ jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
      */
     tblno = outcomp->quant_tbl_no;
     if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
-        srcinfo->quant_tbl_ptrs[tblno] == NULL)
+       srcinfo->quant_tbl_ptrs[tblno] == NULL)
       ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
     slot_quant = srcinfo->quant_tbl_ptrs[tblno];
     c_quant = incomp->quant_table;
     if (c_quant != NULL) {
       for (coefi = 0; coefi < DCTSIZE2; coefi++) {
-        if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
-          ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
+       if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
+         ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
       }
     }
     /* Note: we do not copy the source's entropy table assignments;
@@ -143,10 +143,10 @@ jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
    * if the application chooses to copy JFIF 1.02 extension markers from
    * the source file, we need to copy the version to make sure we don't
    * emit a file that has 1.02 extensions but a claimed version of 1.01.
-   * We will *not*, however, copy version info from mislabeled "2.01" files.
    */
   if (srcinfo->saw_JFIF_marker) {
-    if (srcinfo->JFIF_major_version == 1) {
+    if (srcinfo->JFIF_major_version == 1 ||
+       srcinfo->JFIF_major_version == 2) {
       dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
       dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
     }
@@ -164,7 +164,7 @@ jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
 
 LOCAL(void)
 transencode_master_selection (j_compress_ptr cinfo,
-                              jvirt_barray_ptr * coef_arrays)
+                             jvirt_barray_ptr * coef_arrays)
 {
   /* Initialize master control (includes parameter checking/processing) */
   jinit_c_master_control(cinfo, TRUE /* transcode only */);
@@ -298,44 +298,44 @@ compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
        yoffset++) {
     for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
-         MCU_col_num++) {
+        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;
-        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
-                                                : compptr->last_col_width;
-        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
-          if (coef->iMCU_row_num < last_iMCU_row ||
-              yindex+yoffset < compptr->last_row_height) {
-            /* Fill in pointers to real blocks in this row */
-            buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
-            for (xindex = 0; xindex < blockcnt; xindex++)
-              MCU_buffer[blkn++] = buffer_ptr++;
-          } else {
-            /* At bottom of image, need a whole row of dummy blocks */
-            xindex = 0;
-          }
-          /* Fill in any dummy blocks needed in this row.
-           * Dummy blocks are filled in the same way as in jccoefct.c:
-           * all zeroes in the AC entries, DC entries equal to previous
-           * block's DC value.  The init routine has already zeroed the
-           * AC entries, so we need only set the DC entries correctly.
-           */
-          for (; xindex < compptr->MCU_width; xindex++) {
-            MCU_buffer[blkn] = coef->dummy_buffer[blkn];
-            MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
-            blkn++;
-          }
-        }
+       compptr = cinfo->cur_comp_info[ci];
+       start_col = MCU_col_num * compptr->MCU_width;
+       blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+                                               : compptr->last_col_width;
+       for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+         if (coef->iMCU_row_num < last_iMCU_row ||
+             yindex+yoffset < compptr->last_row_height) {
+           /* Fill in pointers to real blocks in this row */
+           buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
+           for (xindex = 0; xindex < blockcnt; xindex++)
+             MCU_buffer[blkn++] = buffer_ptr++;
+         } else {
+           /* At bottom of image, need a whole row of dummy blocks */
+           xindex = 0;
+         }
+         /* Fill in any dummy blocks needed in this row.
+          * Dummy blocks are filled in the same way as in jccoefct.c:
+          * all zeroes in the AC entries, DC entries equal to previous
+          * block's DC value.  The init routine has already zeroed the
+          * AC entries, so we need only set the DC entries correctly.
+          */
+         for (; xindex < compptr->MCU_width; xindex++) {
+           MCU_buffer[blkn] = coef->dummy_buffer[blkn];
+           MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
+           blkn++;
+         }
+       }
       }
       /* Try to write the MCU. */
       if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
-        /* Suspension forced; update state counters and exit */
-        coef->MCU_vert_offset = yoffset;
-        coef->mcu_ctr = MCU_col_num;
-        return FALSE;
+       /* Suspension forced; update state counters and exit */
+       coef->MCU_vert_offset = yoffset;
+       coef->mcu_ctr = MCU_col_num;
+       return FALSE;
       }
     }
     /* Completed an MCU row, but perhaps not an iMCU row */
@@ -358,7 +358,7 @@ compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
 
 LOCAL(void)
 transencode_coef_controller (j_compress_ptr cinfo,
-                             jvirt_barray_ptr * coef_arrays)
+                            jvirt_barray_ptr * coef_arrays)
 {
   my_coef_ptr coef;
   JBLOCKROW buffer;
@@ -366,7 +366,7 @@ transencode_coef_controller (j_compress_ptr cinfo,
 
   coef = (my_coef_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_coef_controller));
+                               SIZEOF(my_coef_controller));
   cinfo->coef = &coef->pub;
   coef->pub.start_pass = start_pass_coef;
   coef->pub.compress_data = compress_output;
@@ -377,7 +377,7 @@ transencode_coef_controller (j_compress_ptr cinfo,
   /* Allocate and pre-zero space for dummy DCT blocks. */
   buffer = (JBLOCKROW)
     (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+                               C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
   FMEMZERO((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
   for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
     coef->dummy_buffer[i] = buffer + i;
index e90e879..a6e0dd9 100644 (file)
@@ -2,7 +2,7 @@
  * jdapimin.c
  *
  * Copyright (C) 1994-1998, Thomas G. Lane.
- * Modified 2009 by Guido Vollbeding.
+ * Modified 2009-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -37,8 +37,8 @@ jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
   if (version != JPEG_LIB_VERSION)
     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
   if (structsize != SIZEOF(struct jpeg_decompress_struct))
-    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
-             (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
+    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
+            (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
 
   /* For debugging purposes, we zero the whole master structure.
    * But the application has already set the err pointer, and may have set
@@ -114,63 +114,66 @@ jpeg_abort_decompress (j_decompress_ptr cinfo)
 LOCAL(void)
 default_decompress_parms (j_decompress_ptr cinfo)
 {
+  int cid0, cid1, cid2;
+
   /* 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) {
+    cid0 = cinfo->comp_info[0].component_id;
+    cid1 = cinfo->comp_info[1].component_id;
+    cid2 = cinfo->comp_info[2].component_id;
+
+    /* First try to guess from the component IDs */
+    if      (cid0 == 0x01 && cid1 == 0x02 && cid2 == 0x03)
+      cinfo->jpeg_color_space = JCS_YCbCr;
+    else if (cid0 == 0x01 && cid1 == 0x22 && cid2 == 0x23)
+      cinfo->jpeg_color_space = JCS_BG_YCC;
+    else if (cid0 == 0x52 && cid1 == 0x47 && cid2 == 0x42)
+      cinfo->jpeg_color_space = JCS_RGB;       /* ASCII 'R', 'G', 'B' */
+    else if (cid0 == 0x72 && cid1 == 0x67 && cid2 == 0x62)
+      cinfo->jpeg_color_space = JCS_BG_RGB;    /* ASCII 'r', 'g', 'b' */
+    else if (cinfo->saw_JFIF_marker)
+      cinfo->jpeg_color_space = JCS_YCbCr;     /* assume it's YCbCr */
+    else if (cinfo->saw_Adobe_marker) {
       switch (cinfo->Adobe_transform) {
       case 0:
-        cinfo->jpeg_color_space = JCS_RGB;
-        break;
+       cinfo->jpeg_color_space = JCS_RGB;
+       break;
       case 1:
-        cinfo->jpeg_color_space = JCS_YCbCr;
-        break;
+       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;
+       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 */
-      }
+      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;
+       cinfo->jpeg_color_space = JCS_CMYK;
+       break;
       case 2:
-        cinfo->jpeg_color_space = JCS_YCCK;
-        break;
+       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;
+       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. */
@@ -178,7 +181,7 @@ default_decompress_parms (j_decompress_ptr cinfo)
     }
     cinfo->out_color_space = JCS_CMYK;
     break;
-
+    
   default:
     cinfo->jpeg_color_space = JCS_UNKNOWN;
     cinfo->out_color_space = JCS_UNKNOWN;
index d4d9b0f..7f3a78b 100644 (file)
@@ -2,6 +2,7 @@
  * jdapistd.c
  *
  * Copyright (C) 1994-1996, Thomas G. Lane.
+ * Modified 2002-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -52,24 +53,24 @@ jpeg_start_decompress (j_decompress_ptr cinfo)
     if (cinfo->inputctl->has_multiple_scans) {
 #ifdef D_MULTISCAN_FILES_SUPPORTED
       for (;;) {
-        int retcode;
-        /* Call progress monitor hook if present */
-        if (cinfo->progress != NULL)
-          (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
-        /* Absorb some more input */
-        retcode = (*cinfo->inputctl->consume_input) (cinfo);
-        if (retcode == JPEG_SUSPENDED)
-          return FALSE;
-        if (retcode == JPEG_REACHED_EOI)
-          break;
-        /* Advance progress counter if appropriate */
-        if (cinfo->progress != NULL &&
-            (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
-          if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
-            /* jdmaster underestimated number of scans; ratchet up one scan */
-            cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
-          }
-        }
+       int retcode;
+       /* Call progress monitor hook if present */
+       if (cinfo->progress != NULL)
+         (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+       /* Absorb some more input */
+       retcode = (*cinfo->inputctl->consume_input) (cinfo);
+       if (retcode == JPEG_SUSPENDED)
+         return FALSE;
+       if (retcode == JPEG_REACHED_EOI)
+         break;
+       /* Advance progress counter if appropriate */
+       if (cinfo->progress != NULL &&
+           (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
+         if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
+           /* jdmaster underestimated number of scans; ratchet up one scan */
+           cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
+         }
+       }
       }
 #else
       ERREXIT(cinfo, JERR_NOT_COMPILED);
@@ -108,16 +109,16 @@ output_pass_setup (j_decompress_ptr cinfo)
       JDIMENSION last_scanline;
       /* 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);
+       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 */
       last_scanline = cinfo->output_scanline;
       (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
-                                    &cinfo->output_scanline, (JDIMENSION) 0);
+                                   &cinfo->output_scanline, (JDIMENSION) 0);
       if (cinfo->output_scanline == last_scanline)
-        return FALSE;          /* No progress made, must suspend */
+       return FALSE;           /* No progress made, must suspend */
     }
     /* Finish up dummy pass, and set up for another one */
     (*cinfo->master->finish_output_pass) (cinfo);
@@ -150,7 +151,7 @@ output_pass_setup (j_decompress_ptr cinfo)
 
 GLOBAL(JDIMENSION)
 jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
-                     JDIMENSION max_lines)
+                    JDIMENSION max_lines)
 {
   JDIMENSION row_ctr;
 
@@ -183,7 +184,7 @@ jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
 
 GLOBAL(JDIMENSION)
 jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
-                    JDIMENSION max_lines)
+                   JDIMENSION max_lines)
 {
   JDIMENSION lines_per_iMCU_row;
 
@@ -264,7 +265,7 @@ jpeg_finish_output (j_decompress_ptr cinfo)
   }
   /* Read markers looking for SOS or EOI */
   while (cinfo->input_scan_number <= cinfo->output_scan_number &&
-         ! cinfo->inputctl->eoi_reached) {
+        ! cinfo->inputctl->eoi_reached) {
     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
       return FALSE;            /* Suspend, come back later */
   }
index 2e0d51a..5533c07 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * jdarith.c
  *
- * Developed 1997-2012 by Guido Vollbeding.
+ * Developed 1997-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -94,7 +94,7 @@ get_byte (j_decompress_ptr cinfo)
  * (instead of fixed) with the bit shift counter CT.
  * Thus, we also need only one (variable instead of
  * fixed size) shift for the LPS/MPS decision, and
- * we can get away with any renormalization update
+ * we can do away with any renormalization update
  * of C (except for new data insertion, of course).
  *
  * I've also introduced a new scheme for accessing
@@ -115,32 +115,32 @@ arith_decode (j_decompress_ptr cinfo, unsigned char *st)
     if (--e->ct < 0) {
       /* Need to fetch next data byte */
       if (cinfo->unread_marker)
-        data = 0;              /* stuff zero data */
+       data = 0;               /* stuff zero data */
       else {
-        data = get_byte(cinfo);        /* read next input byte */
-        if (data == 0xFF) {    /* zero stuff or marker code */
-          do data = get_byte(cinfo);
-          while (data == 0xFF);        /* swallow extra 0xFF bytes */
-          if (data == 0)
-            data = 0xFF;       /* discard stuffed zero byte */
-          else {
-            /* Note: Different from the Huffman decoder, hitting
-             * a marker while processing the compressed data
-             * segment is legal in arithmetic coding.
-             * The convention is to supply zero data
-             * then until decoding is complete.
-             */
-            cinfo->unread_marker = data;
-            data = 0;
-          }
-        }
+       data = get_byte(cinfo); /* read next input byte */
+       if (data == 0xFF) {     /* zero stuff or marker code */
+         do data = get_byte(cinfo);
+         while (data == 0xFF); /* swallow extra 0xFF bytes */
+         if (data == 0)
+           data = 0xFF;        /* discard stuffed zero byte */
+         else {
+           /* Note: Different from the Huffman decoder, hitting
+            * a marker while processing the compressed data
+            * segment is legal in arithmetic coding.
+            * The convention is to supply zero data
+            * then until decoding is complete.
+            */
+           cinfo->unread_marker = data;
+           data = 0;
+         }
+       }
       }
       e->c = (e->c << 8) | data; /* insert data into C register */
       if ((e->ct += 8) < 0)     /* update bit shift counter */
-        /* Need more initial bytes */
-        if (++e->ct == 0)
-          /* Got 2 initial bytes -> re-init A and exit loop */
-          e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
+       /* Need more initial bytes */
+       if (++e->ct == 0)
+         /* Got 2 initial bytes -> re-init A and exit loop */
+         e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
     }
     e->a <<= 1;
   }
@@ -207,7 +207,7 @@ process_restart (j_decompress_ptr cinfo)
       entropy->dc_context[ci] = 0;
     }
     if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
-        (cinfo->progressive_mode && cinfo->Ss)) {
+       (cinfo->progressive_mode && cinfo->Ss)) {
       MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
     }
   }
@@ -278,28 +278,28 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       st += 2; st += sign;
       /* Figure F.23: Decoding the magnitude category of v */
       if ((m = arith_decode(cinfo, st)) != 0) {
-        st = entropy->dc_stats[tbl] + 20;      /* Table F.4: X1 = 20 */
-        while (arith_decode(cinfo, st)) {
-          if ((m <<= 1) == 0x8000) {
-            WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
-            entropy->ct = -1;                  /* magnitude overflow */
-            return TRUE;
-          }
-          st += 1;
-        }
+       st = entropy->dc_stats[tbl] + 20;       /* Table F.4: X1 = 20 */
+       while (arith_decode(cinfo, st)) {
+         if ((m <<= 1) == 0x8000) {
+           WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
+           entropy->ct = -1;                   /* magnitude overflow */
+           return TRUE;
+         }
+         st += 1;
+       }
       }
       /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
       if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
-        entropy->dc_context[ci] = 0;              /* zero diff category */
+       entropy->dc_context[ci] = 0;               /* zero diff category */
       else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
-        entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
+       entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
       else
-        entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
+       entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
       v = m;
       /* Figure F.24: Decoding the magnitude bit pattern of v */
       st += 14;
       while (m >>= 1)
-        if (arith_decode(cinfo, st)) v |= m;
+       if (arith_decode(cinfo, st)) v |= m;
       v += 1; if (sign) v = -v;
       entropy->last_dc_val[ci] += v;
     }
@@ -354,9 +354,9 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       if (arith_decode(cinfo, st + 1)) break;
       st += 3;
       if (k >= cinfo->Se) {
-        WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
-        entropy->ct = -1;                      /* spectral overflow */
-        return TRUE;
+       WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
+       entropy->ct = -1;                       /* spectral overflow */
+       return TRUE;
       }
     }
     /* Figure F.21: Decoding nonzero value v */
@@ -366,17 +366,17 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
     /* Figure F.23: Decoding the magnitude category of v */
     if ((m = arith_decode(cinfo, st)) != 0) {
       if (arith_decode(cinfo, st)) {
-        m <<= 1;
-        st = entropy->ac_stats[tbl] +
-             (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
-        while (arith_decode(cinfo, st)) {
-          if ((m <<= 1) == 0x8000) {
-            WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
-            entropy->ct = -1;                  /* magnitude overflow */
-            return TRUE;
-          }
-          st += 1;
-        }
+       m <<= 1;
+       st = entropy->ac_stats[tbl] +
+            (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
+       while (arith_decode(cinfo, st)) {
+         if ((m <<= 1) == 0x8000) {
+           WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
+           entropy->ct = -1;                   /* magnitude overflow */
+           return TRUE;
+         }
+         st += 1;
+       }
       }
     }
     v = m;
@@ -395,6 +395,8 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
 
 /*
  * MCU decoding for DC successive approximation refinement scan.
+ * Note: we assume such scans can be multi-component,
+ * although the spec is not very clear on the point.
  */
 
 METHODDEF(boolean)
@@ -473,26 +475,26 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
     for (;;) {
       thiscoef = *block + natural_order[++k];
       if (*thiscoef) {                         /* previously nonzero coef */
-        if (arith_decode(cinfo, st + 2)) {
-          if (*thiscoef < 0)
-            *thiscoef += m1;
-          else
-            *thiscoef += p1;
-        }
-        break;
+       if (arith_decode(cinfo, st + 2)) {
+         if (*thiscoef < 0)
+           *thiscoef += m1;
+         else
+           *thiscoef += p1;
+       }
+       break;
       }
       if (arith_decode(cinfo, st + 1)) {       /* newly nonzero coef */
-        if (arith_decode(cinfo, entropy->fixed_bin))
-          *thiscoef = m1;
-        else
-          *thiscoef = p1;
-        break;
+       if (arith_decode(cinfo, entropy->fixed_bin))
+         *thiscoef = m1;
+       else
+         *thiscoef = p1;
+       break;
       }
       st += 3;
       if (k >= cinfo->Se) {
-        WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
-        entropy->ct = -1;                      /* spectral overflow */
-        return TRUE;
+       WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
+       entropy->ct = -1;                       /* spectral overflow */
+       return TRUE;
       }
     }
   } while (k < cinfo->Se);
@@ -551,28 +553,28 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       st += 2; st += sign;
       /* Figure F.23: Decoding the magnitude category of v */
       if ((m = arith_decode(cinfo, st)) != 0) {
-        st = entropy->dc_stats[tbl] + 20;      /* Table F.4: X1 = 20 */
-        while (arith_decode(cinfo, st)) {
-          if ((m <<= 1) == 0x8000) {
-            WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
-            entropy->ct = -1;                  /* magnitude overflow */
-            return TRUE;
-          }
-          st += 1;
-        }
+       st = entropy->dc_stats[tbl] + 20;       /* Table F.4: X1 = 20 */
+       while (arith_decode(cinfo, st)) {
+         if ((m <<= 1) == 0x8000) {
+           WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
+           entropy->ct = -1;                   /* magnitude overflow */
+           return TRUE;
+         }
+         st += 1;
+       }
       }
       /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
       if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
-        entropy->dc_context[ci] = 0;              /* zero diff category */
+       entropy->dc_context[ci] = 0;               /* zero diff category */
       else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
-        entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
+       entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
       else
-        entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
+       entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
       v = m;
       /* Figure F.24: Decoding the magnitude bit pattern of v */
       st += 14;
       while (m >>= 1)
-        if (arith_decode(cinfo, st)) v |= m;
+       if (arith_decode(cinfo, st)) v |= m;
       v += 1; if (sign) v = -v;
       entropy->last_dc_val[ci] += v;
     }
@@ -590,14 +592,14 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       st = entropy->ac_stats[tbl] + 3 * k;
       if (arith_decode(cinfo, st)) break;      /* EOB flag */
       for (;;) {
-        k++;
-        if (arith_decode(cinfo, st + 1)) break;
-        st += 3;
-        if (k >= cinfo->lim_Se) {
-          WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
-          entropy->ct = -1;                    /* spectral overflow */
-          return TRUE;
-        }
+       k++;
+       if (arith_decode(cinfo, st + 1)) break;
+       st += 3;
+       if (k >= cinfo->lim_Se) {
+         WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
+         entropy->ct = -1;                     /* spectral overflow */
+         return TRUE;
+       }
       }
       /* Figure F.21: Decoding nonzero value v */
       /* Figure F.22: Decoding the sign of v */
@@ -605,25 +607,25 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       st += 2;
       /* Figure F.23: Decoding the magnitude category of v */
       if ((m = arith_decode(cinfo, st)) != 0) {
-        if (arith_decode(cinfo, st)) {
-          m <<= 1;
-          st = entropy->ac_stats[tbl] +
-               (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
-          while (arith_decode(cinfo, st)) {
-            if ((m <<= 1) == 0x8000) {
-              WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
-              entropy->ct = -1;                        /* magnitude overflow */
-              return TRUE;
-            }
-            st += 1;
-          }
-        }
+       if (arith_decode(cinfo, st)) {
+         m <<= 1;
+         st = entropy->ac_stats[tbl] +
+              (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
+         while (arith_decode(cinfo, st)) {
+           if ((m <<= 1) == 0x8000) {
+             WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
+             entropy->ct = -1;                 /* magnitude overflow */
+             return TRUE;
+           }
+           st += 1;
+         }
+       }
       }
       v = m;
       /* Figure F.24: Decoding the magnitude bit pattern of v */
       st += 14;
       while (m >>= 1)
-        if (arith_decode(cinfo, st)) v |= m;
+       if (arith_decode(cinfo, st)) v |= m;
       v += 1; if (sign) v = -v;
       (*block)[natural_order[k]] = (JCOEF) v;
     } while (k < cinfo->lim_Se);
@@ -648,24 +650,24 @@ start_pass (j_decompress_ptr cinfo)
     /* Validate progressive scan parameters */
     if (cinfo->Ss == 0) {
       if (cinfo->Se != 0)
-        goto bad;
+       goto bad;
     } else {
       /* need not check Ss/Se < 0 since they came from unsigned bytes */
       if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
-        goto bad;
+       goto bad;
       /* AC scans may have only one component */
       if (cinfo->comps_in_scan != 1)
-        goto bad;
+       goto bad;
     }
     if (cinfo->Ah != 0) {
       /* Successive approximation refinement scan: must have Al = Ah-1. */
       if (cinfo->Ah-1 != cinfo->Al)
-        goto bad;
+       goto bad;
     }
     if (cinfo->Al > 13) {      /* need not check for < 0 */
       bad:
       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
-               cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
+              cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
     }
     /* Update progression status, and verify that scan order is legal.
      * Note that inter-scan inconsistencies are treated as warnings
@@ -675,32 +677,32 @@ start_pass (j_decompress_ptr cinfo)
       int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
       int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
       if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
-        WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
+       WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
       for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
-        int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
-        if (cinfo->Ah != expected)
-          WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
-        coef_bit_ptr[coefi] = cinfo->Al;
+       int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
+       if (cinfo->Ah != expected)
+         WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
+       coef_bit_ptr[coefi] = cinfo->Al;
       }
     }
     /* Select MCU decoding routine */
     if (cinfo->Ah == 0) {
       if (cinfo->Ss == 0)
-        entropy->pub.decode_mcu = decode_mcu_DC_first;
+       entropy->pub.decode_mcu = decode_mcu_DC_first;
       else
-        entropy->pub.decode_mcu = decode_mcu_AC_first;
+       entropy->pub.decode_mcu = decode_mcu_AC_first;
     } else {
       if (cinfo->Ss == 0)
-        entropy->pub.decode_mcu = decode_mcu_DC_refine;
+       entropy->pub.decode_mcu = decode_mcu_DC_refine;
       else
-        entropy->pub.decode_mcu = decode_mcu_AC_refine;
+       entropy->pub.decode_mcu = decode_mcu_AC_refine;
     }
   } else {
     /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
      * This ought to be an error condition, but we make it a warning.
      */
     if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
-        (cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se))
+       (cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se))
       WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
     /* Select MCU decoding routine */
     entropy->pub.decode_mcu = decode_mcu;
@@ -712,23 +714,23 @@ start_pass (j_decompress_ptr cinfo)
     if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
       tbl = compptr->dc_tbl_no;
       if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
-        ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
+       ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
       if (entropy->dc_stats[tbl] == NULL)
-        entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
-          ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
+       entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
+         ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
       MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
       /* Initialize DC predictions to 0 */
       entropy->last_dc_val[ci] = 0;
       entropy->dc_context[ci] = 0;
     }
     if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
-        (cinfo->progressive_mode && cinfo->Ss)) {
+       (cinfo->progressive_mode && cinfo->Ss)) {
       tbl = compptr->ac_tbl_no;
       if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
-        ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
+       ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
       if (entropy->ac_stats[tbl] == NULL)
-        entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
-          ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
+       entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
+         ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
       MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
     }
   }
@@ -744,6 +746,17 @@ start_pass (j_decompress_ptr cinfo)
 
 
 /*
+ * Finish up at the end of an arithmetic-compressed scan.
+ */
+
+METHODDEF(void)
+finish_pass (j_decompress_ptr cinfo)
+{
+  /* no work necessary here */
+}
+
+
+/*
  * Module initialization routine for arithmetic entropy decoding.
  */
 
@@ -755,9 +768,10 @@ jinit_arith_decoder (j_decompress_ptr cinfo)
 
   entropy = (arith_entropy_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(arith_entropy_decoder));
+                               SIZEOF(arith_entropy_decoder));
   cinfo->entropy = &entropy->pub;
   entropy->pub.start_pass = start_pass;
+  entropy->pub.finish_pass = finish_pass;
 
   /* Mark tables unallocated */
   for (i = 0; i < NUM_ARITH_TBLS; i++) {
@@ -773,10 +787,10 @@ jinit_arith_decoder (j_decompress_ptr cinfo)
     int *coef_bit_ptr, ci;
     cinfo->coef_bits = (int (*)[DCTSIZE2])
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  cinfo->num_components*DCTSIZE2*SIZEOF(int));
+                                 cinfo->num_components*DCTSIZE2*SIZEOF(int));
     coef_bit_ptr = & cinfo->coef_bits[0][0];
-    for (ci = 0; ci < cinfo->num_components; ci++)
+    for (ci = 0; ci < cinfo->num_components; ci++) 
       for (i = 0; i < DCTSIZE2; i++)
-        *coef_bit_ptr++ = -1;
+       *coef_bit_ptr++ = -1;
   }
 }
index 4997f47..5c8681c 100644 (file)
@@ -68,7 +68,7 @@ init_destination (j_compress_ptr cinfo)
   /* 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));
+                                 OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
 
   dest->pub.next_output_byte = dest->buffer;
   dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
@@ -206,7 +206,7 @@ jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
   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));
+                                 SIZEOF(my_destination_mgr));
   }
 
   dest = (my_dest_ptr) cinfo->dest;
@@ -233,7 +233,7 @@ jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
 
 GLOBAL(void)
 jpeg_mem_dest (j_compress_ptr cinfo,
-               unsigned char ** outbuffer, unsigned long * outsize)
+              unsigned char ** outbuffer, unsigned long * outsize)
 {
   my_mem_dest_ptr dest;
 
@@ -246,7 +246,7 @@ jpeg_mem_dest (j_compress_ptr cinfo,
   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_mem_destination_mgr));
+                                 SIZEOF(my_mem_destination_mgr));
   }
 
   dest = (my_mem_dest_ptr) cinfo->dest;
index 5e417cc..2a27cfe 100644 (file)
@@ -2,7 +2,7 @@
  * jdatasrc.c
  *
  * Copyright (C) 1994-1996, Thomas G. Lane.
- * Modified 2009-2011 by Guido Vollbeding.
+ * Modified 2009-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -221,11 +221,11 @@ jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
   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));
+                                 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));
+                                 INPUT_BUF_SIZE * SIZEOF(JOCTET));
   }
 
   src = (my_src_ptr) cinfo->src;
@@ -247,7 +247,7 @@ jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
 
 GLOBAL(void)
 jpeg_mem_src (j_decompress_ptr cinfo,
-              unsigned char * inbuffer, unsigned long insize)
+             const unsigned char * inbuffer, unsigned long insize)
 {
   struct jpeg_source_mgr * src;
 
@@ -261,7 +261,7 @@ jpeg_mem_src (j_decompress_ptr cinfo,
   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(struct jpeg_source_mgr));
+                                 SIZEOF(struct jpeg_source_mgr));
   }
 
   src = cinfo->src;
@@ -271,5 +271,5 @@ jpeg_mem_src (j_decompress_ptr cinfo,
   src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
   src->term_source = term_source;
   src->bytes_in_buffer = (size_t) insize;
-  src->next_input_byte = (JOCTET *) inbuffer;
+  src->next_input_byte = (const JOCTET *) inbuffer;
 }
index 93066a2..ed02fc3 100644 (file)
@@ -64,15 +64,15 @@ typedef my_coef_controller * my_coef_ptr;
 
 /* Forward declarations */
 METHODDEF(int) decompress_onepass
-        JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+       JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
 #ifdef D_MULTISCAN_FILES_SUPPORTED
 METHODDEF(int) decompress_data
-        JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+       JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
 #endif
 #ifdef BLOCK_SMOOTHING_SUPPORTED
 LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
 METHODDEF(int) decompress_smooth_data
-        JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+       JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
 #endif
 
 
@@ -161,16 +161,16 @@ decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
        yoffset++) {
     for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
-         MCU_col_num++) {
+        MCU_col_num++) {
       /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
       if (cinfo->lim_Se)       /* can bypass in DC only case */
-        FMEMZERO((void FAR *) coef->MCU_buffer[0],
-                 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
+       FMEMZERO((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; update state counters and exit */
-        coef->MCU_vert_offset = yoffset;
-        coef->MCU_ctr = MCU_col_num;
-        return JPEG_SUSPENDED;
+       /* Suspension forced; update state counters and exit */
+       coef->MCU_vert_offset = yoffset;
+       coef->MCU_ctr = MCU_col_num;
+       return JPEG_SUSPENDED;
       }
       /* 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
@@ -179,32 +179,32 @@ decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
        */
       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[compptr->component_index] +
-          yoffset * compptr->DCT_v_scaled_size;
-        start_col = MCU_col_num * compptr->MCU_sample_width;
-        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
-          if (cinfo->input_iMCU_row < last_iMCU_row ||
-              yoffset+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_h_scaled_size;
-            }
-          }
-          blkn += compptr->MCU_width;
-          output_ptr += compptr->DCT_v_scaled_size;
-        }
+       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[compptr->component_index] +
+         yoffset * compptr->DCT_v_scaled_size;
+       start_col = MCU_col_num * compptr->MCU_sample_width;
+       for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+         if (cinfo->input_iMCU_row < last_iMCU_row ||
+             yoffset+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_h_scaled_size;
+           }
+         }
+         blkn += compptr->MCU_width;
+         output_ptr += compptr->DCT_v_scaled_size;
+       }
       }
     }
     /* Completed an MCU row, but perhaps not an iMCU row */
@@ -270,25 +270,25 @@ consume_data (j_decompress_ptr cinfo)
   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
        yoffset++) {
     for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
-         MCU_col_num++) {
+        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++;
-          }
-        }
+       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)) {
-        /* Suspension forced; update state counters and exit */
-        coef->MCU_vert_offset = yoffset;
-        coef->MCU_ctr = MCU_col_num;
-        return JPEG_SUSPENDED;
+       /* Suspension forced; update state counters and exit */
+       coef->MCU_vert_offset = yoffset;
+       coef->MCU_ctr = MCU_col_num;
+       return JPEG_SUSPENDED;
       }
     }
     /* Completed an MCU row, but perhaps not an iMCU row */
@@ -329,8 +329,8 @@ decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
 
   /* Force some input to be done if we are getting ahead of the input. */
   while (cinfo->input_scan_number < cinfo->output_scan_number ||
-         (cinfo->input_scan_number == cinfo->output_scan_number &&
-          cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
+        (cinfo->input_scan_number == cinfo->output_scan_number &&
+         cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
     if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
       return JPEG_SUSPENDED;
   }
@@ -361,10 +361,10 @@ decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
       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_h_scaled_size;
+       (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
+                       output_ptr, output_col);
+       buffer_ptr++;
+       output_col += compptr->DCT_h_scaled_size;
       }
       output_ptr += compptr->DCT_v_scaled_size;
     }
@@ -421,8 +421,8 @@ smoothing_ok (j_decompress_ptr cinfo)
   if (coef->coef_bits_latch == NULL)
     coef->coef_bits_latch = (int *)
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  cinfo->num_components *
-                                  (SAVED_COEFS * SIZEOF(int)));
+                                 cinfo->num_components *
+                                 (SAVED_COEFS * SIZEOF(int)));
   coef_bits_latch = coef->coef_bits_latch;
 
   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
@@ -432,11 +432,11 @@ smoothing_ok (j_decompress_ptr cinfo)
       return FALSE;
     /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
     if (qtable->quantval[0] == 0 ||
-        qtable->quantval[Q01_POS] == 0 ||
-        qtable->quantval[Q10_POS] == 0 ||
-        qtable->quantval[Q20_POS] == 0 ||
-        qtable->quantval[Q11_POS] == 0 ||
-        qtable->quantval[Q02_POS] == 0)
+       qtable->quantval[Q01_POS] == 0 ||
+       qtable->quantval[Q10_POS] == 0 ||
+       qtable->quantval[Q20_POS] == 0 ||
+       qtable->quantval[Q11_POS] == 0 ||
+       qtable->quantval[Q02_POS] == 0)
       return FALSE;
     /* DC values must be at least partly known for all components. */
     coef_bits = cinfo->coef_bits[ci];
@@ -446,7 +446,7 @@ smoothing_ok (j_decompress_ptr cinfo)
     for (coefi = 1; coefi <= 5; coefi++) {
       coef_bits_latch[coefi] = coef_bits[coefi];
       if (coef_bits[coefi] != 0)
-        smoothing_useful = TRUE;
+       smoothing_useful = TRUE;
     }
     coef_bits_latch += SAVED_COEFS;
   }
@@ -482,7 +482,7 @@ decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
 
   /* Force some input to be done if we are getting ahead of the input. */
   while (cinfo->input_scan_number <= cinfo->output_scan_number &&
-         ! cinfo->inputctl->eoi_reached) {
+        ! cinfo->inputctl->eoi_reached) {
     if (cinfo->input_scan_number == cinfo->output_scan_number) {
       /* If input is working on current scan, we ordinarily want it to
        * have completed the current row.  But if input scan is DC,
@@ -491,7 +491,7 @@ decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
        */
       JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
       if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
-        break;
+       break;
     }
     if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
       return JPEG_SUSPENDED;
@@ -519,15 +519,15 @@ decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
     if (cinfo->output_iMCU_row > 0) {
       access_rows += compptr->v_samp_factor; /* prior iMCU row too */
       buffer = (*cinfo->mem->access_virt_barray)
-        ((j_common_ptr) cinfo, coef->whole_image[ci],
-         (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
-         (JDIMENSION) access_rows, FALSE);
+       ((j_common_ptr) cinfo, coef->whole_image[ci],
+        (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
+        (JDIMENSION) access_rows, FALSE);
       buffer += compptr->v_samp_factor;        /* point to current iMCU row */
       first_row = FALSE;
     } else {
       buffer = (*cinfo->mem->access_virt_barray)
-        ((j_common_ptr) cinfo, coef->whole_image[ci],
-         (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
+       ((j_common_ptr) cinfo, coef->whole_image[ci],
+        (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
       first_row = TRUE;
     }
     /* Fetch component-dependent info */
@@ -545,13 +545,13 @@ decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
     for (block_row = 0; block_row < block_rows; block_row++) {
       buffer_ptr = buffer[block_row];
       if (first_row && block_row == 0)
-        prev_block_row = buffer_ptr;
+       prev_block_row = buffer_ptr;
       else
-        prev_block_row = buffer[block_row-1];
+       prev_block_row = buffer[block_row-1];
       if (last_row && block_row == block_rows-1)
-        next_block_row = buffer_ptr;
+       next_block_row = buffer_ptr;
       else
-        next_block_row = buffer[block_row+1];
+       next_block_row = buffer[block_row+1];
       /* We fetch the surrounding DC values using a sliding-register approach.
        * Initialize all nine here so as to do the right thing on narrow pics.
        */
@@ -561,102 +561,102 @@ decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
       output_col = 0;
       last_block_column = compptr->width_in_blocks - 1;
       for (block_num = 0; block_num <= last_block_column; block_num++) {
-        /* Fetch current DCT block into workspace so we can modify it. */
-        jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
-        /* Update DC values */
-        if (block_num < last_block_column) {
-          DC3 = (int) prev_block_row[1][0];
-          DC6 = (int) buffer_ptr[1][0];
-          DC9 = (int) next_block_row[1][0];
-        }
-        /* Compute coefficient estimates per K.8.
-         * An estimate is applied only if coefficient is still zero,
-         * and is not known to be fully accurate.
-         */
-        /* AC01 */
-        if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
-          num = 36 * Q00 * (DC4 - DC6);
-          if (num >= 0) {
-            pred = (int) (((Q01<<7) + num) / (Q01<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-          } else {
-            pred = (int) (((Q01<<7) - num) / (Q01<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-            pred = -pred;
-          }
-          workspace[1] = (JCOEF) pred;
-        }
-        /* AC10 */
-        if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
-          num = 36 * Q00 * (DC2 - DC8);
-          if (num >= 0) {
-            pred = (int) (((Q10<<7) + num) / (Q10<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-          } else {
-            pred = (int) (((Q10<<7) - num) / (Q10<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-            pred = -pred;
-          }
-          workspace[8] = (JCOEF) pred;
-        }
-        /* AC20 */
-        if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
-          num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
-          if (num >= 0) {
-            pred = (int) (((Q20<<7) + num) / (Q20<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-          } else {
-            pred = (int) (((Q20<<7) - num) / (Q20<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-            pred = -pred;
-          }
-          workspace[16] = (JCOEF) pred;
-        }
-        /* AC11 */
-        if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
-          num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
-          if (num >= 0) {
-            pred = (int) (((Q11<<7) + num) / (Q11<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-          } else {
-            pred = (int) (((Q11<<7) - num) / (Q11<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-            pred = -pred;
-          }
-          workspace[9] = (JCOEF) pred;
-        }
-        /* AC02 */
-        if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
-          num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
-          if (num >= 0) {
-            pred = (int) (((Q02<<7) + num) / (Q02<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-          } else {
-            pred = (int) (((Q02<<7) - num) / (Q02<<8));
-            if (Al > 0 && pred >= (1<<Al))
-              pred = (1<<Al)-1;
-            pred = -pred;
-          }
-          workspace[2] = (JCOEF) pred;
-        }
-        /* OK, do the IDCT */
-        (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
-                        output_ptr, output_col);
-        /* Advance for next column */
-        DC1 = DC2; DC2 = DC3;
-        DC4 = DC5; DC5 = DC6;
-        DC7 = DC8; DC8 = DC9;
-        buffer_ptr++, prev_block_row++, next_block_row++;
-        output_col += compptr->DCT_h_scaled_size;
+       /* Fetch current DCT block into workspace so we can modify it. */
+       jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
+       /* Update DC values */
+       if (block_num < last_block_column) {
+         DC3 = (int) prev_block_row[1][0];
+         DC6 = (int) buffer_ptr[1][0];
+         DC9 = (int) next_block_row[1][0];
+       }
+       /* Compute coefficient estimates per K.8.
+        * An estimate is applied only if coefficient is still zero,
+        * and is not known to be fully accurate.
+        */
+       /* AC01 */
+       if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
+         num = 36 * Q00 * (DC4 - DC6);
+         if (num >= 0) {
+           pred = (int) (((Q01<<7) + num) / (Q01<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+         } else {
+           pred = (int) (((Q01<<7) - num) / (Q01<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+           pred = -pred;
+         }
+         workspace[1] = (JCOEF) pred;
+       }
+       /* AC10 */
+       if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
+         num = 36 * Q00 * (DC2 - DC8);
+         if (num >= 0) {
+           pred = (int) (((Q10<<7) + num) / (Q10<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+         } else {
+           pred = (int) (((Q10<<7) - num) / (Q10<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+           pred = -pred;
+         }
+         workspace[8] = (JCOEF) pred;
+       }
+       /* AC20 */
+       if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
+         num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
+         if (num >= 0) {
+           pred = (int) (((Q20<<7) + num) / (Q20<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+         } else {
+           pred = (int) (((Q20<<7) - num) / (Q20<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+           pred = -pred;
+         }
+         workspace[16] = (JCOEF) pred;
+       }
+       /* AC11 */
+       if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
+         num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
+         if (num >= 0) {
+           pred = (int) (((Q11<<7) + num) / (Q11<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+         } else {
+           pred = (int) (((Q11<<7) - num) / (Q11<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+           pred = -pred;
+         }
+         workspace[9] = (JCOEF) pred;
+       }
+       /* AC02 */
+       if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
+         num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
+         if (num >= 0) {
+           pred = (int) (((Q02<<7) + num) / (Q02<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+         } else {
+           pred = (int) (((Q02<<7) - num) / (Q02<<8));
+           if (Al > 0 && pred >= (1<<Al))
+             pred = (1<<Al)-1;
+           pred = -pred;
+         }
+         workspace[2] = (JCOEF) pred;
+       }
+       /* OK, do the IDCT */
+       (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
+                       output_ptr, output_col);
+       /* Advance for next column */
+       DC1 = DC2; DC2 = DC3;
+       DC4 = DC5; DC5 = DC6;
+       DC7 = DC8; DC8 = DC9;
+       buffer_ptr++, prev_block_row++, next_block_row++;
+       output_col += compptr->DCT_h_scaled_size;
       }
       output_ptr += compptr->DCT_v_scaled_size;
     }
@@ -681,7 +681,7 @@ jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
 
   coef = (my_coef_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_coef_controller));
+                               SIZEOF(my_coef_controller));
   cinfo->coef = (struct jpeg_d_coef_controller *) coef;
   coef->pub.start_input_pass = start_input_pass;
   coef->pub.start_output_pass = start_output_pass;
@@ -699,20 +699,20 @@ jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
     jpeg_component_info *compptr;
 
     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
-         ci++, compptr++) {
+        ci++, compptr++) {
       access_rows = compptr->v_samp_factor;
 #ifdef BLOCK_SMOOTHING_SUPPORTED
       /* If block smoothing could be used, need a bigger window */
       if (cinfo->progressive_mode)
-        access_rows *= 3;
+       access_rows *= 3;
 #endif
       coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
-        ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
-         (JDIMENSION) jround_up((long) compptr->width_in_blocks,
-                                (long) compptr->h_samp_factor),
-         (JDIMENSION) jround_up((long) compptr->height_in_blocks,
-                                (long) compptr->v_samp_factor),
-         (JDIMENSION) access_rows);
+       ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
+        (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+                               (long) compptr->h_samp_factor),
+        (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+                               (long) compptr->v_samp_factor),
+        (JDIMENSION) access_rows);
     }
     coef->pub.consume_data = consume_data;
     coef->pub.decompress_data = decompress_data;
@@ -727,13 +727,13 @@ jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
 
     buffer = (JBLOCKROW)
       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+                                 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
     for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
       coef->MCU_buffer[i] = buffer + i;
     }
     if (cinfo->lim_Se == 0)    /* DC only case: want to bypass later */
       FMEMZERO((void FAR *) buffer,
-               (size_t) (D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)));
+              (size_t) (D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)));
     coef->pub.consume_data = dummy_consume_data;
     coef->pub.decompress_data = decompress_onepass;
     coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
index 83c17a2..29c30fa 100644 (file)
@@ -2,7 +2,7 @@
  * jdcolor.c
  *
  * Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 2011-2012 by Guido Vollbeding.
+ * Modified 2011-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -19,7 +19,7 @@
 typedef struct {
   struct jpeg_color_deconverter pub; /* public fields */
 
-  /* Private state for YCC->RGB conversion */
+  /* Private state for YCbCr->RGB and BG_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 */
@@ -32,22 +32,44 @@ typedef struct {
 typedef my_color_deconverter * my_cconvert_ptr;
 
 
-/**************** YCbCr -> RGB conversion: most common case **************/
-/****************   RGB -> Y   conversion: less common case **************/
+/***************  YCbCr -> RGB conversion: most common case **************/
+/*************** BG_YCC -> RGB conversion: less common case **************/
+/***************    RGB -> Y   conversion: less common case **************/
 
 /*
- * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
- * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
- * The conversion equations to be implemented are therefore
+ * YCbCr is defined per Recommendation ITU-R BT.601-7 (03/2011),
+ * previously known as Recommendation CCIR 601-1, except that Cb and Cr
+ * are normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
+ * sRGB (standard RGB color space) is defined per IEC 61966-2-1:1999.
+ * sYCC (standard luma-chroma-chroma color space with extended gamut)
+ * is defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex F.
+ * bg-sRGB and bg-sYCC (big gamut standard color spaces)
+ * are defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex G.
+ * Note that the derived conversion coefficients given in some of these
+ * documents are imprecise.  The general conversion equations are
  *
- *     R = Y                + 1.40200 * Cr
- *     G = Y - 0.34414 * Cb - 0.71414 * Cr
- *     B = Y + 1.77200 * Cb
+ *     R = Y + K * (1 - Kr) * Cr
+ *     G = Y - K * (Kb * (1 - Kb) * Cb + Kr * (1 - Kr) * Cr) / (1 - Kr - Kb)
+ *     B = Y + K * (1 - Kb) * Cb
  *
- *     Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
+ *     Y = Kr * R + (1 - Kr - Kb) * G + Kb * B
+ *
+ * With Kr = 0.299 and Kb = 0.114 (derived according to SMPTE RP 177-1993
+ * from the 1953 FCC NTSC primaries and CIE Illuminant C), K = 2 for sYCC,
+ * the conversion equations to be implemented are therefore
+ *
+ *     R = Y + 1.402 * Cr
+ *     G = Y - 0.344136286 * Cb - 0.714136286 * Cr
+ *     B = Y + 1.772 * Cb
+ *
+ *     Y = 0.299 * R + 0.587 * G + 0.114 * B
  *
  * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
- * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
+ * For bg-sYCC, with K = 4, the equations are
+ *
+ *     R = Y + 2.804 * Cr
+ *     G = Y - 0.688272572 * Cb - 1.428272572 * Cr
+ *     B = Y + 3.544 * Cb
  *
  * To avoid floating-point arithmetic, we represent the fractional constants
  * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
@@ -58,9 +80,9 @@ typedef my_color_deconverter * my_cconvert_ptr;
  * For even more speed, we avoid doing any multiplications in the inner loop
  * by precalculating the constants times Cb and Cr for all possible values.
  * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
- * for 12-bit samples it is still acceptable.  It's not very reasonable for
- * 16-bit samples, but if you want lossless storage you shouldn't be changing
- * colorspace anyway.
+ * for 9-bit to 12-bit samples it is still acceptable.  It's not very
+ * reasonable for 16-bit samples, but if you want lossless storage you
+ * shouldn't be changing colorspace anyway.
  * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
  * values for the G calculation are left scaled up, since we must add them
  * together before rounding.
@@ -84,11 +106,12 @@ typedef my_color_deconverter * my_cconvert_ptr;
 
 
 /*
- * Initialize tables for YCC->RGB colorspace conversion.
+ * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
  */
 
 LOCAL(void)
 build_ycc_rgb_table (j_decompress_ptr cinfo)
+/* Normal case, sYCC */
 {
   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   int i;
@@ -97,31 +120,71 @@ build_ycc_rgb_table (j_decompress_ptr cinfo)
 
   cconvert->Cr_r_tab = (int *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (MAXJSAMPLE+1) * SIZEOF(int));
+                               (MAXJSAMPLE+1) * SIZEOF(int));
   cconvert->Cb_b_tab = (int *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (MAXJSAMPLE+1) * SIZEOF(int));
+                               (MAXJSAMPLE+1) * SIZEOF(int));
   cconvert->Cr_g_tab = (INT32 *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (MAXJSAMPLE+1) * SIZEOF(INT32));
+                               (MAXJSAMPLE+1) * SIZEOF(INT32));
   cconvert->Cb_g_tab = (INT32 *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (MAXJSAMPLE+1) * SIZEOF(INT32));
+                               (MAXJSAMPLE+1) * SIZEOF(INT32));
 
   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
-    /* Cr=>R value is nearest int to 1.40200 * x */
+    /* Cr=>R value is nearest int to 1.402 * x */
     cconvert->Cr_r_tab[i] = (int)
-                    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
-    /* Cb=>B value is nearest int to 1.77200 * x */
+                   RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
+    /* Cb=>B value is nearest int to 1.772 * x */
     cconvert->Cb_b_tab[i] = (int)
-                    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
-    /* Cr=>G value is scaled-up -0.71414 * x */
-    cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
-    /* Cb=>G value is scaled-up -0.34414 * x */
+                   RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
+    /* Cr=>G value is scaled-up -0.714136286 * x */
+    cconvert->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
+    /* Cb=>G value is scaled-up -0.344136286 * x */
     /* We also add in ONE_HALF so that need not do it in inner loop */
-    cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
+    cconvert->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
+  }
+}
+
+
+LOCAL(void)
+build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
+/* Wide gamut case, bg-sYCC */
+{
+  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+  int i;
+  INT32 x;
+  SHIFT_TEMPS
+
+  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, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
+    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
+    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
+    /* Cr=>R value is nearest int to 2.804 * x */
+    cconvert->Cr_r_tab[i] = (int)
+                   RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
+    /* Cb=>B value is nearest int to 3.544 * x */
+    cconvert->Cb_b_tab[i] = (int)
+                   RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
+    /* Cr=>G value is scaled-up -1.428272572 * x */
+    cconvert->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
+    /* Cb=>G value is scaled-up -0.688272572 * x */
+    /* We also add in ONE_HALF so that need not do it in inner loop */
+    cconvert->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
   }
 }
 
@@ -139,8 +202,8 @@ build_ycc_rgb_table (j_decompress_ptr cinfo)
 
 METHODDEF(void)
 ycc_rgb_convert (j_decompress_ptr cinfo,
-                 JSAMPIMAGE input_buf, JDIMENSION input_row,
-                 JSAMPARRAY output_buf, int num_rows)
+                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;
@@ -166,19 +229,21 @@ ycc_rgb_convert (j_decompress_ptr cinfo,
       y  = GETJSAMPLE(inptr0[col]);
       cb = GETJSAMPLE(inptr1[col]);
       cr = GETJSAMPLE(inptr2[col]);
-      /* Range-limiting is essential due to noise introduced by DCT losses. */
-      outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
+      /* Range-limiting is essential due to noise introduced by DCT losses,
+       * for extended gamut (sYCC) and wide gamut (bg-sYCC) encodings.
+       */
+      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]];
+                             ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
+                                                SCALEBITS))];
+      outptr[RGB_BLUE]  = range_limit[y + Cbbtab[cb]];
       outptr += RGB_PIXELSIZE;
     }
   }
 }
 
 
-/**************** Cases other than YCbCr -> RGB **************/
+/**************** Cases other than YCC -> RGB ****************/
 
 
 /*
@@ -195,12 +260,12 @@ build_rgb_y_table (j_decompress_ptr cinfo)
   /* Allocate and fill in the conversion tables. */
   cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (TABLE_SIZE * SIZEOF(INT32)));
+                               (TABLE_SIZE * SIZEOF(INT32)));
 
   for (i = 0; i <= MAXJSAMPLE; i++) {
-    rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
-    rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
-    rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
+    rgb_y_tab[i+R_Y_OFF] = FIX(0.299) * i;
+    rgb_y_tab[i+G_Y_OFF] = FIX(0.587) * i;
+    rgb_y_tab[i+B_Y_OFF] = FIX(0.114) * i + ONE_HALF;
   }
 }
 
@@ -211,8 +276,8 @@ build_rgb_y_table (j_decompress_ptr cinfo)
 
 METHODDEF(void)
 rgb_gray_convert (j_decompress_ptr cinfo,
-                  JSAMPIMAGE input_buf, JDIMENSION input_row,
-                  JSAMPARRAY output_buf, int num_rows)
+                 JSAMPIMAGE input_buf, JDIMENSION input_row,
+                 JSAMPARRAY output_buf, int num_rows)
 {
   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   register INT32 * ctab = cconvert->rgb_y_tab;
@@ -234,8 +299,8 @@ rgb_gray_convert (j_decompress_ptr cinfo,
       b = GETJSAMPLE(inptr2[col]);
       /* Y */
       outptr[col] = (JSAMPLE)
-                ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
-                 >> SCALEBITS);
+               ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+                >> SCALEBITS);
     }
   }
 }
@@ -244,12 +309,15 @@ rgb_gray_convert (j_decompress_ptr cinfo,
 /*
  * [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
  * (inverse color transform).
+ * This can be seen as an adaption of the general YCbCr->RGB
+ * conversion equation with Kr = Kb = 0, while replacing the
+ * normalization by modulo calculation.
  */
 
 METHODDEF(void)
 rgb1_rgb_convert (j_decompress_ptr cinfo,
-                  JSAMPIMAGE input_buf, JDIMENSION input_row,
-                  JSAMPARRAY output_buf, int num_rows)
+                 JSAMPIMAGE input_buf, JDIMENSION input_row,
+                 JSAMPARRAY output_buf, int num_rows)
 {
   register int r, g, b;
   register JSAMPROW outptr;
@@ -286,8 +354,8 @@ rgb1_rgb_convert (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 rgb1_gray_convert (j_decompress_ptr cinfo,
-                   JSAMPIMAGE input_buf, JDIMENSION input_row,
-                   JSAMPARRAY output_buf, int num_rows)
+                  JSAMPIMAGE input_buf, JDIMENSION input_row,
+                  JSAMPARRAY output_buf, int num_rows)
 {
   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   register INT32 * ctab = cconvert->rgb_y_tab;
@@ -314,8 +382,8 @@ rgb1_gray_convert (j_decompress_ptr cinfo,
       b = (b + g - CENTERJSAMPLE) & MAXJSAMPLE;
       /* Y */
       outptr[col] = (JSAMPLE)
-                ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
-                 >> SCALEBITS);
+               ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+                >> SCALEBITS);
     }
   }
 }
@@ -328,8 +396,8 @@ rgb1_gray_convert (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 rgb_convert (j_decompress_ptr cinfo,
-             JSAMPIMAGE input_buf, JDIMENSION input_row,
-             JSAMPARRAY output_buf, int num_rows)
+            JSAMPIMAGE input_buf, JDIMENSION input_row,
+            JSAMPARRAY output_buf, int num_rows)
 {
   register JSAMPROW outptr;
   register JSAMPROW inptr0, inptr1, inptr2;
@@ -360,8 +428,8 @@ rgb_convert (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 null_convert (j_decompress_ptr cinfo,
-              JSAMPIMAGE input_buf, JDIMENSION input_row,
-              JSAMPARRAY output_buf, int num_rows)
+             JSAMPIMAGE input_buf, JDIMENSION input_row,
+             JSAMPARRAY output_buf, int num_rows)
 {
   int ci;
   register int nc = cinfo->num_components;
@@ -375,8 +443,8 @@ null_convert (j_decompress_ptr cinfo,
       inptr = input_buf[ci][input_row];
       outptr = output_buf[0] + ci;
       for (col = 0; col < num_cols; col++) {
-        *outptr = *inptr++;    /* needn't bother with GETJSAMPLE() here */
-        outptr += nc;
+       *outptr = *inptr++;     /* needn't bother with GETJSAMPLE() here */
+       outptr += nc;
       }
     }
     input_row++;
@@ -387,17 +455,17 @@ null_convert (j_decompress_ptr cinfo,
 
 /*
  * Color conversion for grayscale: just copy the data.
- * This also works for YCbCr -> grayscale conversion, in which
+ * This also works for YCC -> grayscale conversion, in which
  * we just copy the Y (luminance) component and ignore chrominance.
  */
 
 METHODDEF(void)
 grayscale_convert (j_decompress_ptr cinfo,
-                   JSAMPIMAGE input_buf, JDIMENSION input_row,
-                   JSAMPARRAY output_buf, int num_rows)
+                  JSAMPIMAGE input_buf, JDIMENSION input_row,
+                  JSAMPARRAY output_buf, int num_rows)
 {
   jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
-                    num_rows, cinfo->output_width);
+                   num_rows, cinfo->output_width);
 }
 
 
@@ -409,8 +477,8 @@ grayscale_convert (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 gray_rgb_convert (j_decompress_ptr cinfo,
-                  JSAMPIMAGE input_buf, JDIMENSION input_row,
-                  JSAMPARRAY output_buf, int num_rows)
+                 JSAMPIMAGE input_buf, JDIMENSION input_row,
+                 JSAMPARRAY output_buf, int num_rows)
 {
   register JSAMPROW outptr;
   register JSAMPROW inptr;
@@ -438,8 +506,8 @@ gray_rgb_convert (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 ycck_cmyk_convert (j_decompress_ptr cinfo,
-                   JSAMPIMAGE input_buf, JDIMENSION input_row,
-                   JSAMPARRAY output_buf, int num_rows)
+                  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;
@@ -466,11 +534,13 @@ ycck_cmyk_convert (j_decompress_ptr cinfo,
       y  = GETJSAMPLE(inptr0[col]);
       cb = GETJSAMPLE(inptr1[col]);
       cr = GETJSAMPLE(inptr2[col]);
-      /* Range-limiting is essential due to noise introduced by DCT losses. */
+      /* Range-limiting is essential due to noise introduced by DCT losses,
+       * and for extended gamut encodings (sYCC).
+       */
       outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];  /* red */
       outptr[1] = range_limit[MAXJSAMPLE - (y +                        /* green */
-                              ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
-                                                 SCALEBITS)))];
+                             ((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 */
@@ -503,7 +573,7 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
 
   cconvert = (my_cconvert_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_color_deconverter));
+                               SIZEOF(my_color_deconverter));
   cinfo->cconvert = &cconvert->pub;
   cconvert->pub.start_pass = start_pass_dcolor;
 
@@ -516,6 +586,8 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
 
   case JCS_RGB:
   case JCS_YCbCr:
+  case JCS_BG_RGB:
+  case JCS_BG_YCC:
     if (cinfo->num_components != 3)
       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
     break;
@@ -532,8 +604,10 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
     break;
   }
 
-  /* Support color transform only for RGB colorspace */
-  if (cinfo->color_transform && cinfo->jpeg_color_space != JCS_RGB)
+  /* Support color transform only for RGB colorspaces */
+  if (cinfo->color_transform &&
+      cinfo->jpeg_color_space != JCS_RGB &&
+      cinfo->jpeg_color_space != JCS_BG_RGB)
     ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
 
   /* Set out_color_components and conversion method based on requested space.
@@ -544,47 +618,76 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
   switch (cinfo->out_color_space) {
   case JCS_GRAYSCALE:
     cinfo->out_color_components = 1;
-    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
-        cinfo->jpeg_color_space == JCS_YCbCr) {
+    switch (cinfo->jpeg_color_space) {
+    case JCS_GRAYSCALE:
+    case JCS_YCbCr:
+    case JCS_BG_YCC:
       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->comp_info[ci].component_needed = FALSE;
-    } else if (cinfo->jpeg_color_space == JCS_RGB) {
+       cinfo->comp_info[ci].component_needed = FALSE;
+      break;
+    case JCS_RGB:
       switch (cinfo->color_transform) {
       case JCT_NONE:
-        cconvert->pub.color_convert = rgb_gray_convert;
-        break;
+       cconvert->pub.color_convert = rgb_gray_convert;
+       break;
       case JCT_SUBTRACT_GREEN:
-        cconvert->pub.color_convert = rgb1_gray_convert;
-        break;
+       cconvert->pub.color_convert = rgb1_gray_convert;
+       break;
       default:
-        ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
-        break;
+       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
       }
       build_rgb_y_table(cinfo);
-    } else
+      break;
+    default:
       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    }
     break;
 
   case JCS_RGB:
     cinfo->out_color_components = RGB_PIXELSIZE;
-    if (cinfo->jpeg_color_space == JCS_YCbCr) {
+    switch (cinfo->jpeg_color_space) {
+    case JCS_GRAYSCALE:
+      cconvert->pub.color_convert = gray_rgb_convert;
+      break;
+    case JCS_YCbCr:
       cconvert->pub.color_convert = ycc_rgb_convert;
       build_ycc_rgb_table(cinfo);
-    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
-      cconvert->pub.color_convert = gray_rgb_convert;
-    } else if (cinfo->jpeg_color_space == JCS_RGB) {
+      break;
+    case JCS_BG_YCC:
+      cconvert->pub.color_convert = ycc_rgb_convert;
+      build_bg_ycc_rgb_table(cinfo);
+      break;
+    case JCS_RGB:
       switch (cinfo->color_transform) {
       case JCT_NONE:
-        cconvert->pub.color_convert = rgb_convert;
-        break;
+       cconvert->pub.color_convert = rgb_convert;
+       break;
       case JCT_SUBTRACT_GREEN:
-        cconvert->pub.color_convert = rgb1_rgb_convert;
-        break;
+       cconvert->pub.color_convert = rgb1_rgb_convert;
+       break;
       default:
-        ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
-        break;
+       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+      }
+      break;
+    default:
+      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    }
+    break;
+
+  case JCS_BG_RGB:
+    cinfo->out_color_components = RGB_PIXELSIZE;
+    if (cinfo->jpeg_color_space == JCS_BG_RGB) {
+      switch (cinfo->color_transform) {
+      case JCT_NONE:
+       cconvert->pub.color_convert = rgb_convert;
+       break;
+      case JCT_SUBTRACT_GREEN:
+       cconvert->pub.color_convert = rgb1_rgb_convert;
+       break;
+      default:
+       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
       }
     } else
       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
@@ -592,13 +695,17 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
 
   case JCS_CMYK:
     cinfo->out_color_components = 4;
-    if (cinfo->jpeg_color_space == JCS_YCCK) {
+    switch (cinfo->jpeg_color_space) {
+    case JCS_YCCK:
       cconvert->pub.color_convert = ycck_cmyk_convert;
       build_ycc_rgb_table(cinfo);
-    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
+      break;
+    case JCS_CMYK:
       cconvert->pub.color_convert = null_convert;
-    } else
+      break;
+    default:
       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+    }
     break;
 
   default:
index 8240f4e..5d0fe83 100644 (file)
@@ -2,13 +2,14 @@
  * jdct.h
  *
  * Copyright (C) 1994-1996, Thomas G. Lane.
+ * Modified 2002-2015 by Guido Vollbeding.
  * 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
+ * The individual DCT algorithms are kept in separate files to ease 
  * machine-dependent tuning (e.g., assembly coding).
  */
 
@@ -38,11 +39,11 @@ typedef INT32 DCTELEM;              /* must have 32 bits */
 #endif
 
 typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data,
-                                               JSAMPARRAY sample_data,
-                                               JDIMENSION start_col));
+                                              JSAMPARRAY sample_data,
+                                              JDIMENSION start_col));
 typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data,
-                                             JSAMPARRAY sample_data,
-                                             JDIMENSION start_col));
+                                            JSAMPARRAY sample_data,
+                                            JDIMENSION start_col));
 
 
 /*
@@ -78,13 +79,16 @@ typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
  * 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.
+ * to do the combined operations quickly, assuming that MAXJSAMPLE+1
+ * is a power of 2.  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 */
+#define RANGE_CENTER  (MAXJSAMPLE * 2 + 2)
+#define RANGE_SUBSET  (RANGE_CENTER - CENTERJSAMPLE)
+
+#define IDCT_range_limit(cinfo)  ((cinfo)->sample_range_limit - RANGE_SUBSET)
 
 
 /* Short forms of external names for systems with brain-damaged linkers. */
@@ -233,106 +237,106 @@ EXTERN(void) jpeg_fdct_1x2
 
 EXTERN(void) jpeg_idct_islow
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        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));
+        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));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_7x7
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_6x6
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_5x5
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        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));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_3x3
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        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));
+        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));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_9x9
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_10x10
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_11x11
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_12x12
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_13x13
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_14x14
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_15x15
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_16x16
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_16x8
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_14x7
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_12x6
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_10x5
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_8x4
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_6x3
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_4x2
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_2x1
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_8x16
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_7x14
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_6x12
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_5x10
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_4x8
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_3x6
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_2x4
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 EXTERN(void) jpeg_idct_1x2
     JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
-         JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+        JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
 
 
 /*
@@ -391,3 +395,23 @@ EXTERN(void) jpeg_idct_1x2
 #ifndef MULTIPLY16V16          /* default definition */
 #define MULTIPLY16V16(var1,var2)  ((var1) * (var2))
 #endif
+
+/* Like RIGHT_SHIFT, but applies to a DCTELEM.
+ * We assume that int right shift is unsigned if INT32 right shift is.
+ */
+
+#ifdef RIGHT_SHIFT_IS_UNSIGNED
+#define ISHIFT_TEMPS   DCTELEM ishift_temp;
+#if BITS_IN_JSAMPLE == 8
+#define DCTELEMBITS  16                /* DCTELEM may be 16 or 32 bits */
+#else
+#define DCTELEMBITS  32                /* DCTELEM must be 32 bits */
+#endif
+#define IRIGHT_SHIFT(x,shft)  \
+    ((ishift_temp = (x)) < 0 ? \
+     (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
+     (ishift_temp >> (shft)))
+#else
+#define ISHIFT_TEMPS
+#define IRIGHT_SHIFT(x,shft)   ((x) >> (shft))
+#endif
index ce49314..9ecfbb5 100644 (file)
@@ -2,7 +2,7 @@
  * jddctmgr.c
  *
  * Copyright (C) 1994-1996, Thomas G. Lane.
- * Modified 2002-2010 by Guido Vollbeding.
+ * Modified 2002-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -230,30 +230,30 @@ start_pass (j_decompress_ptr cinfo)
       switch (cinfo->dct_method) {
 #ifdef DCT_ISLOW_SUPPORTED
       case JDCT_ISLOW:
-        method_ptr = jpeg_idct_islow;
-        method = JDCT_ISLOW;
-        break;
+       method_ptr = jpeg_idct_islow;
+       method = JDCT_ISLOW;
+       break;
 #endif
 #ifdef DCT_IFAST_SUPPORTED
       case JDCT_IFAST:
-        method_ptr = jpeg_idct_ifast;
-        method = JDCT_IFAST;
-        break;
+       method_ptr = jpeg_idct_ifast;
+       method = JDCT_IFAST;
+       break;
 #endif
 #ifdef DCT_FLOAT_SUPPORTED
       case JDCT_FLOAT:
-        method_ptr = jpeg_idct_float;
-        method = JDCT_FLOAT;
-        break;
+       method_ptr = jpeg_idct_float;
+       method = JDCT_FLOAT;
+       break;
 #endif
       default:
-        ERREXIT(cinfo, JERR_NOT_COMPILED);
-        break;
+       ERREXIT(cinfo, JERR_NOT_COMPILED);
+       break;
       }
       break;
     default:
       ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
-               compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
+              compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
       break;
     }
     idct->pub.inverse_DCT[ci] = method_ptr;
@@ -274,75 +274,75 @@ start_pass (j_decompress_ptr cinfo)
 #ifdef PROVIDE_ISLOW_TABLES
     case JDCT_ISLOW:
       {
-        /* For LL&M IDCT method, multipliers are equal to raw quantization
-         * coefficients, but are stored as ints to ensure access efficiency.
-         */
-        ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
-        for (i = 0; i < DCTSIZE2; i++) {
-          ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
-        }
+       /* For LL&M IDCT method, multipliers are equal to raw quantization
+        * coefficients, but are stored as ints to ensure access efficiency.
+        */
+       ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
+       for (i = 0; i < DCTSIZE2; i++) {
+         ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[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.
-         */
-        IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
+       /* 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.
+        */
+       IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
 #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
+       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
 
-        for (i = 0; i < DCTSIZE2; i++) {
-          ifmtbl[i] = (IFAST_MULT_TYPE)
-            DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
-                                  (INT32) aanscales[i]),
-                    CONST_BITS-IFAST_SCALE_BITS);
-        }
+       for (i = 0; i < DCTSIZE2; i++) {
+         ifmtbl[i] = (IFAST_MULT_TYPE)
+           DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[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
-         * We apply a further scale factor of 1/8.
-         */
-        FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
-        int row, col;
-        static const double aanscalefactor[DCTSIZE] = {
-          1.0, 1.387039845, 1.306562965, 1.175875602,
-          1.0, 0.785694958, 0.541196100, 0.275899379
-        };
+       /* 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
+        * We apply a further scale factor of 1/8.
+        */
+       FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
+       int row, col;
+       static const double aanscalefactor[DCTSIZE] = {
+         1.0, 1.387039845, 1.306562965, 1.175875602,
+         1.0, 0.785694958, 0.541196100, 0.275899379
+       };
 
-        i = 0;
-        for (row = 0; row < DCTSIZE; row++) {
-          for (col = 0; col < DCTSIZE; col++) {
-            fmtbl[i] = (FLOAT_MULT_TYPE)
-              ((double) qtbl->quantval[i] *
-               aanscalefactor[row] * aanscalefactor[col] * 0.125);
-            i++;
-          }
-        }
+       i = 0;
+       for (row = 0; row < DCTSIZE; row++) {
+         for (col = 0; col < DCTSIZE; col++) {
+           fmtbl[i] = (FLOAT_MULT_TYPE)
+             ((double) qtbl->quantval[i] *
+              aanscalefactor[row] * aanscalefactor[col] * 0.125);
+           i++;
+         }
+       }
       }
       break;
 #endif
@@ -367,8 +367,8 @@ jinit_inverse_dct (j_decompress_ptr cinfo)
 
   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;
+                               SIZEOF(my_idct_controller));
+  cinfo->idct = &idct->pub;
   idct->pub.start_pass = start_pass;
 
   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
@@ -376,7 +376,7 @@ jinit_inverse_dct (j_decompress_ptr cinfo)
     /* Allocate and pre-zero a multiplier table for each component */
     compptr->dct_table =
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  SIZEOF(multiplier_table));
+                                 SIZEOF(multiplier_table));
     MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
     /* Mark multiplier table not yet set up for any method */
     idct->cur_method[ci] = -1;
index e76c0d0..6920e20 100644 (file)
@@ -2,7 +2,7 @@
  * jdhuff.c
  *
  * Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 2006-2012 by Guido Vollbeding.
+ * Modified 2006-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -97,22 +97,22 @@ typedef struct {            /* Bitreading working state within an MCU */
 
 /* Macros to declare and load/save bitread local variables. */
 #define BITREAD_STATE_VARS  \
-        register bit_buf_type get_buffer;  \
-        register int bits_left;  \
-        bitread_working_state br_state
+       register bit_buf_type get_buffer;  \
+       register int bits_left;  \
+       bitread_working_state br_state
 
 #define BITREAD_LOAD_STATE(cinfop,permstate)  \
-        br_state.cinfo = cinfop; \
-        br_state.next_input_byte = cinfop->src->next_input_byte; \
-        br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
-        get_buffer = permstate.get_buffer; \
-        bits_left = permstate.bits_left;
+       br_state.cinfo = cinfop; \
+       br_state.next_input_byte = cinfop->src->next_input_byte; \
+       br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
+       get_buffer = permstate.get_buffer; \
+       bits_left = permstate.bits_left;
 
 #define BITREAD_SAVE_STATE(cinfop,permstate)  \
-        cinfop->src->next_input_byte = br_state.next_input_byte; \
-        cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
-        permstate.get_buffer = get_buffer; \
-        permstate.bits_left = bits_left
+       cinfop->src->next_input_byte = br_state.next_input_byte; \
+       cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
+       permstate.get_buffer = get_buffer; \
+       permstate.bits_left = bits_left
 
 /*
  * These macros provide the in-line portion of bit fetching.
@@ -133,19 +133,19 @@ typedef struct {          /* Bitreading working state within an MCU */
  */
 
 #define CHECK_BIT_BUFFER(state,nbits,action) \
-        { if (bits_left < (nbits)) {  \
-            if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \
-              { action; }  \
-            get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
+       { if (bits_left < (nbits)) {  \
+           if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \
+             { action; }  \
+           get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
 
 #define GET_BITS(nbits) \
-        (((int) (get_buffer >> (bits_left -= (nbits)))) & BIT_MASK(nbits))
+       (((int) (get_buffer >> (bits_left -= (nbits)))) & BIT_MASK(nbits))
 
 #define PEEK_BITS(nbits) \
-        (((int) (get_buffer >> (bits_left -  (nbits)))) & BIT_MASK(nbits))
+       (((int) (get_buffer >> (bits_left -  (nbits)))) & BIT_MASK(nbits))
 
 #define DROP_BITS(nbits) \
-        (bits_left -= (nbits))
+       (bits_left -= (nbits))
 
 
 /*
@@ -182,7 +182,7 @@ typedef struct {            /* Bitreading working state within an MCU */
     nb = HUFF_LOOKAHEAD+1; \
 slowlabel: \
     if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
-        { failaction; } \
+       { failaction; } \
     get_buffer = state.get_buffer; bits_left = state.bits_left; \
   } \
 }
@@ -210,11 +210,11 @@ typedef struct {
 #else
 #if MAX_COMPS_IN_SCAN == 4
 #define ASSIGN_STATE(dest,src)  \
-        ((dest).EOBRUN = (src).EOBRUN, \
-         (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])
+       ((dest).EOBRUN = (src).EOBRUN, \
+        (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
 
@@ -321,7 +321,7 @@ static const int jpeg_zigzag_order2[2][2] = {
 
 LOCAL(void)
 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
-                         d_derived_tbl ** pdtbl)
+                        d_derived_tbl ** pdtbl)
 {
   JHUFF_TBL *htbl;
   d_derived_tbl *dtbl;
@@ -347,10 +347,10 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
   if (*pdtbl == NULL)
     *pdtbl = (d_derived_tbl *)
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  SIZEOF(d_derived_tbl));
+                                 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 */
 
   p = 0;
@@ -363,10 +363,10 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
   }
   huffsize[p] = 0;
   numsymbols = p;
-
+  
   /* Figure C.2: generate the codes themselves */
   /* We also validate that the counts represent a legal Huffman code tree. */
-
+  
   code = 0;
   si = huffsize[0];
   p = 0;
@@ -417,9 +417,9 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
       /* 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--) {
-        dtbl->look_nbits[lookbits] = l;
-        dtbl->look_sym[lookbits] = htbl->huffval[p];
-        lookbits++;
+       dtbl->look_nbits[lookbits] = l;
+       dtbl->look_sym[lookbits] = htbl->huffval[p];
+       lookbits++;
       }
     }
   }
@@ -434,7 +434,7 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
     for (i = 0; i < numsymbols; i++) {
       int sym = htbl->huffval[i];
       if (sym < 0 || sym > 15)
-        ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
     }
   }
 }
@@ -463,8 +463,8 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
 
 LOCAL(boolean)
 jpeg_fill_bit_buffer (bitread_working_state * state,
-                      register bit_buf_type get_buffer, register int bits_left,
-                      int nbits)
+                     register bit_buf_type get_buffer, register int bits_left,
+                     int nbits)
 /* Load up the bit buffer to a depth of at least nbits */
 {
   /* Copy heavily used state fields into locals (hopefully registers) */
@@ -482,48 +482,48 @@ jpeg_fill_bit_buffer (bitread_working_state * state,
 
       /* Attempt to read a byte */
       if (bytes_in_buffer == 0) {
-        if (! (*cinfo->src->fill_input_buffer) (cinfo))
-          return FALSE;
-        next_input_byte = cinfo->src->next_input_byte;
-        bytes_in_buffer = cinfo->src->bytes_in_buffer;
+       if (! (*cinfo->src->fill_input_buffer) (cinfo))
+         return FALSE;
+       next_input_byte = cinfo->src->next_input_byte;
+       bytes_in_buffer = 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) {
-        /* Loop here to discard any padding FF's on terminating marker,
-         * so that we can save a valid unread_marker value.  NOTE: we will
-         * accept multiple FF's followed by a 0 as meaning a single FF data
-         * byte.  This data pattern is not valid according to the standard.
-         */
-        do {
-          if (bytes_in_buffer == 0) {
-            if (! (*cinfo->src->fill_input_buffer) (cinfo))
-              return FALSE;
-            next_input_byte = cinfo->src->next_input_byte;
-            bytes_in_buffer = 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.
-           * Save the marker code for later use.
-           * Fine point: it might appear that we should save the marker into
-           * bitread working state, not straight into permanent state.  But
-           * once we have hit a marker, we cannot need to suspend within the
-           * current MCU, because we will read no more bytes from the data
-           * source.  So it is OK to update permanent state right away.
-           */
-          cinfo->unread_marker = c;
-          /* See if we need to insert some fake zero bits. */
-          goto no_more_bytes;
-        }
+       /* Loop here to discard any padding FF's on terminating marker,
+        * so that we can save a valid unread_marker value.  NOTE: we will
+        * accept multiple FF's followed by a 0 as meaning a single FF data
+        * byte.  This data pattern is not valid according to the standard.
+        */
+       do {
+         if (bytes_in_buffer == 0) {
+           if (! (*cinfo->src->fill_input_buffer) (cinfo))
+             return FALSE;
+           next_input_byte = cinfo->src->next_input_byte;
+           bytes_in_buffer = 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.
+          * Save the marker code for later use.
+          * Fine point: it might appear that we should save the marker into
+          * bitread working state, not straight into permanent state.  But
+          * once we have hit a marker, we cannot need to suspend within the
+          * current MCU, because we will read no more bytes from the data
+          * source.  So it is OK to update permanent state right away.
+          */
+         cinfo->unread_marker = c;
+         /* See if we need to insert some fake zero bits. */
+         goto no_more_bytes;
+       }
       }
 
       /* OK, load c into get_buffer */
@@ -543,8 +543,8 @@ jpeg_fill_bit_buffer (bitread_working_state * state,
        * appears per data segment.
        */
       if (! ((huff_entropy_ptr) cinfo->entropy)->insufficient_data) {
-        WARNMS(cinfo, JWRN_HIT_MARKER);
-        ((huff_entropy_ptr) cinfo->entropy)->insufficient_data = TRUE;
+       WARNMS(cinfo, JWRN_HIT_MARKER);
+       ((huff_entropy_ptr) cinfo->entropy)->insufficient_data = TRUE;
       }
       /* Fill the buffer with zero bits */
       get_buffer <<= MIN_GET_BITS - bits_left;
@@ -590,8 +590,8 @@ static const int bmask[16] =        /* bmask[n] is mask for n rightmost bits */
 
 LOCAL(int)
 jpeg_huff_decode (bitread_working_state * state,
-                  register bit_buf_type get_buffer, register int bits_left,
-                  d_derived_tbl * htbl, int min_bits)
+                 register bit_buf_type get_buffer, register int bits_left,
+                 d_derived_tbl * htbl, int min_bits)
 {
   register int l = min_bits;
   register INT32 code;
@@ -628,6 +628,22 @@ jpeg_huff_decode (bitread_working_state * state,
 
 
 /*
+ * Finish up at the end of a Huffman-compressed scan.
+ */
+
+METHODDEF(void)
+finish_pass_huff (j_decompress_ptr cinfo)
+{
+  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+
+  /* 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->bitstate.bits_left / 8;
+  entropy->bitstate.bits_left = 0;
+}
+
+
+/*
  * Check for a restart marker & resynchronize decoder.
  * Returns FALSE if must suspend.
  */
@@ -638,10 +654,7 @@ process_restart (j_decompress_ptr cinfo)
   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   int ci;
 
-  /* 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->bitstate.bits_left / 8;
-  entropy->bitstate.bits_left = 0;
+  finish_pass_huff(cinfo);
 
   /* Advance past the RSTn marker */
   if (! (*cinfo->marker->read_restart_marker) (cinfo))
@@ -671,7 +684,7 @@ process_restart (j_decompress_ptr cinfo)
 /*
  * Huffman MCU decoding.
  * Each of these routines decodes and returns one MCU's worth of
- * Huffman-compressed coefficients.
+ * Huffman-compressed coefficients. 
  * The coefficients are reordered from zigzag order into natural array order,
  * but are not dequantized.
  *
@@ -693,7 +706,7 @@ process_restart (j_decompress_ptr cinfo)
 
 METHODDEF(boolean)
 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
-{
+{   
   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   int Al = cinfo->Al;
   register int s, r;
@@ -708,7 +721,7 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   if (cinfo->restart_interval) {
     if (entropy->restarts_to_go == 0)
       if (! process_restart(cinfo))
-        return FALSE;
+       return FALSE;
   }
 
   /* If we've run out of data, just leave the MCU set to zeroes.
@@ -733,9 +746,9 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       /* Section F.2.2.1: decode the DC coefficient difference */
       HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
       if (s) {
-        CHECK_BIT_BUFFER(br_state, s, return FALSE);
-        r = GET_BITS(s);
-        s = HUFF_EXTEND(r, s);
+       CHECK_BIT_BUFFER(br_state, s, return FALSE);
+       r = GET_BITS(s);
+       s = HUFF_EXTEND(r, s);
       }
 
       /* Convert DC difference to actual value, update last_dc_val */
@@ -764,7 +777,7 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
 
 METHODDEF(boolean)
 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
-{
+{   
   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   register int s, k, r;
   unsigned int EOBRUN;
@@ -778,7 +791,7 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   if (cinfo->restart_interval) {
     if (entropy->restarts_to_go == 0)
       if (! process_restart(cinfo))
-        return FALSE;
+       return FALSE;
   }
 
   /* If we've run out of data, just leave the MCU set to zeroes.
@@ -805,29 +818,29 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       tbl = entropy->ac_derived_tbl;
 
       for (k = cinfo->Ss; k <= Se; k++) {
-        HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
-        r = s >> 4;
-        s &= 15;
-        if (s) {
-          k += r;
-          CHECK_BIT_BUFFER(br_state, s, return FALSE);
-          r = GET_BITS(s);
-          s = HUFF_EXTEND(r, s);
-          /* Scale and output coefficient in natural (dezigzagged) order */
-          (*block)[natural_order[k]] = (JCOEF) (s << Al);
-        } else {
-          if (r != 15) {       /* EOBr, run length is 2^r + appended bits */
-            if (r) {           /* EOBr, r > 0 */
-              EOBRUN = 1 << r;
-              CHECK_BIT_BUFFER(br_state, r, return FALSE);
-              r = GET_BITS(r);
-              EOBRUN += r;
-              EOBRUN--;                /* this band is processed at this moment */
-            }
-            break;             /* force end-of-band */
-          }
-          k += 15;             /* ZRL: skip 15 zeroes in band */
-        }
+       HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
+       r = s >> 4;
+       s &= 15;
+       if (s) {
+         k += r;
+         CHECK_BIT_BUFFER(br_state, s, return FALSE);
+         r = GET_BITS(s);
+         s = HUFF_EXTEND(r, s);
+         /* Scale and output coefficient in natural (dezigzagged) order */
+         (*block)[natural_order[k]] = (JCOEF) (s << Al);
+       } else {
+         if (r != 15) {        /* EOBr, run length is 2^r + appended bits */
+           if (r) {            /* EOBr, r > 0 */
+             EOBRUN = 1 << r;
+             CHECK_BIT_BUFFER(br_state, r, return FALSE);
+             r = GET_BITS(r);
+             EOBRUN += r;
+             EOBRUN--;         /* this band is processed at this moment */
+           }
+           break;              /* force end-of-band */
+         }
+         k += 15;              /* ZRL: skip 15 zeroes in band */
+       }
       }
 
       BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
@@ -846,24 +859,22 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
 
 /*
  * MCU decoding for DC successive approximation refinement scan.
- * Note: we assume such scans can be multi-component, although the spec
- * is not very clear on the point.
+ * Note: we assume such scans can be multi-component,
+ * although the spec is not very clear on the point.
  */
 
 METHODDEF(boolean)
 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
-{
+{   
   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
-  int p1 = 1 << cinfo->Al;     /* 1 in the bit position being coded */
-  int blkn;
-  JBLOCKROW block;
+  int p1, blkn;
   BITREAD_STATE_VARS;
 
   /* Process restart marker if needed; may have to suspend */
   if (cinfo->restart_interval) {
     if (entropy->restarts_to_go == 0)
       if (! process_restart(cinfo))
-        return FALSE;
+       return FALSE;
   }
 
   /* Not worth the cycles to check insufficient_data here,
@@ -873,15 +884,15 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   /* Load up working state */
   BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
 
+  p1 = 1 << cinfo->Al;         /* 1 in the bit position being coded */
+
   /* Outer loop handles each block in the MCU */
 
   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
-    block = MCU_data[blkn];
-
     /* Encoded data is simply the next bit of the two's-complement DC value */
     CHECK_BIT_BUFFER(br_state, 1, return FALSE);
     if (GET_BITS(1))
-      (*block)[0] |= p1;
+      MCU_data[blkn][0][0] |= p1;
     /* Note: since we use |=, repeating the assignment later is safe */
   }
 
@@ -901,7 +912,7 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
 
 METHODDEF(boolean)
 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
-{
+{   
   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   register int s, k, r;
   unsigned int EOBRUN;
@@ -918,7 +929,7 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   if (cinfo->restart_interval) {
     if (entropy->restarts_to_go == 0)
       if (! process_restart(cinfo))
-        return FALSE;
+       return FALSE;
   }
 
   /* If we've run out of data, don't modify the MCU.
@@ -951,59 +962,59 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
 
     if (EOBRUN == 0) {
       do {
-        HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
-        r = s >> 4;
-        s &= 15;
-        if (s) {
-          if (s != 1)          /* size of new coef should always be 1 */
-            WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
-          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
-          if (GET_BITS(1))
-            s = p1;            /* newly nonzero coef is positive */
-          else
-            s = m1;            /* newly nonzero coef is negative */
-        } else {
-          if (r != 15) {
-            EOBRUN = 1 << r;   /* EOBr, run length is 2^r + appended bits */
-            if (r) {
-              CHECK_BIT_BUFFER(br_state, r, goto undoit);
-              r = GET_BITS(r);
-              EOBRUN += r;
-            }
-            break;             /* rest of block is handled by EOB logic */
-          }
-          /* note s = 0 for processing ZRL */
-        }
-        /* Advance over already-nonzero coefs and r still-zero coefs,
-         * appending correction bits to the nonzeroes.  A correction bit is 1
-         * if the absolute value of the coefficient must be increased.
-         */
-        do {
-          thiscoef = *block + natural_order[k];
-          if (*thiscoef) {
-            CHECK_BIT_BUFFER(br_state, 1, goto undoit);
-            if (GET_BITS(1)) {
-              if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
-                if (*thiscoef >= 0)
-                  *thiscoef += p1;
-                else
-                  *thiscoef += m1;
-              }
-            }
-          } else {
-            if (--r < 0)
-              break;           /* reached target zero coefficient */
-          }
-          k++;
-        } while (k <= Se);
-        if (s) {
-          int pos = natural_order[k];
-          /* Output newly nonzero coefficient */
-          (*block)[pos] = (JCOEF) s;
-          /* Remember its position in case we have to suspend */
-          newnz_pos[num_newnz++] = pos;
-        }
-        k++;
+       HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
+       r = s >> 4;
+       s &= 15;
+       if (s) {
+         if (s != 1)           /* size of new coef should always be 1 */
+           WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
+         CHECK_BIT_BUFFER(br_state, 1, goto undoit);
+         if (GET_BITS(1))
+           s = p1;             /* newly nonzero coef is positive */
+         else
+           s = m1;             /* newly nonzero coef is negative */
+       } else {
+         if (r != 15) {
+           EOBRUN = 1 << r;    /* EOBr, run length is 2^r + appended bits */
+           if (r) {
+             CHECK_BIT_BUFFER(br_state, r, goto undoit);
+             r = GET_BITS(r);
+             EOBRUN += r;
+           }
+           break;              /* rest of block is handled by EOB logic */
+         }
+         /* note s = 0 for processing ZRL */
+       }
+       /* Advance over already-nonzero coefs and r still-zero coefs,
+        * appending correction bits to the nonzeroes.  A correction bit is 1
+        * if the absolute value of the coefficient must be increased.
+        */
+       do {
+         thiscoef = *block + natural_order[k];
+         if (*thiscoef) {
+           CHECK_BIT_BUFFER(br_state, 1, goto undoit);
+           if (GET_BITS(1)) {
+             if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
+               if (*thiscoef >= 0)
+                 *thiscoef += p1;
+               else
+                 *thiscoef += m1;
+             }
+           }
+         } else {
+           if (--r < 0)
+             break;            /* reached target zero coefficient */
+         }
+         k++;
+       } while (k <= Se);
+       if (s) {
+         int pos = natural_order[k];
+         /* Output newly nonzero coefficient */
+         (*block)[pos] = (JCOEF) s;
+         /* Remember its position in case we have to suspend */
+         newnz_pos[num_newnz++] = pos;
+       }
+       k++;
       } while (k <= Se);
     }
 
@@ -1014,19 +1025,19 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
        * if the absolute value of the coefficient must be increased.
        */
       do {
-        thiscoef = *block + natural_order[k];
-        if (*thiscoef) {
-          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
-          if (GET_BITS(1)) {
-            if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
-              if (*thiscoef >= 0)
-                *thiscoef += p1;
-              else
-                *thiscoef += m1;
-            }
-          }
-        }
-        k++;
+       thiscoef = *block + natural_order[k];
+       if (*thiscoef) {
+         CHECK_BIT_BUFFER(br_state, 1, goto undoit);
+         if (GET_BITS(1)) {
+           if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
+             if (*thiscoef >= 0)
+               *thiscoef += p1;
+             else
+               *thiscoef += m1;
+           }
+         }
+       }
+       k++;
       } while (k <= Se);
       /* Count one block completed in EOB run */
       EOBRUN--;
@@ -1069,7 +1080,7 @@ decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   if (cinfo->restart_interval) {
     if (entropy->restarts_to_go == 0)
       if (! process_restart(cinfo))
-        return FALSE;
+       return FALSE;
   }
 
   /* If we've run out of data, just leave the MCU set to zeroes.
@@ -1102,66 +1113,66 @@ decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       k = 1;
       coef_limit = entropy->coef_limit[blkn];
       if (coef_limit) {
-        /* Convert DC difference to actual value, update last_dc_val */
-        if (s) {
-          CHECK_BIT_BUFFER(br_state, s, return FALSE);
-          r = GET_BITS(s);
-          s = HUFF_EXTEND(r, s);
-        }
-        ci = cinfo->MCU_membership[blkn];
-        s += state.last_dc_val[ci];
-        state.last_dc_val[ci] = s;
-        /* Output the DC coefficient */
-        (*block)[0] = (JCOEF) s;
-
-        /* Section F.2.2.2: decode the AC coefficients */
-        /* Since zeroes are skipped, output area must be cleared beforehand */
-        for (; k < coef_limit; k++) {
-          HUFF_DECODE(s, br_state, htbl, return FALSE, label2);
-
-          r = s >> 4;
-          s &= 15;
-
-          if (s) {
-            k += r;
-            CHECK_BIT_BUFFER(br_state, s, return FALSE);
-            r = GET_BITS(s);
-            s = HUFF_EXTEND(r, s);
-            /* Output coefficient in natural (dezigzagged) order.
-             * Note: the extra entries in natural_order[] will save us
-             * if k > Se, which could happen if the data is corrupted.
-             */
-            (*block)[natural_order[k]] = (JCOEF) s;
-          } else {
-            if (r != 15)
-              goto EndOfBlock;
-            k += 15;
-          }
-        }
+       /* Convert DC difference to actual value, update last_dc_val */
+       if (s) {
+         CHECK_BIT_BUFFER(br_state, s, return FALSE);
+         r = GET_BITS(s);
+         s = HUFF_EXTEND(r, s);
+       }
+       ci = cinfo->MCU_membership[blkn];
+       s += state.last_dc_val[ci];
+       state.last_dc_val[ci] = s;
+       /* Output the DC coefficient */
+       (*block)[0] = (JCOEF) s;
+
+       /* Section F.2.2.2: decode the AC coefficients */
+       /* Since zeroes are skipped, output area must be cleared beforehand */
+       for (; k < coef_limit; k++) {
+         HUFF_DECODE(s, br_state, htbl, return FALSE, label2);
+
+         r = s >> 4;
+         s &= 15;
+
+         if (s) {
+           k += r;
+           CHECK_BIT_BUFFER(br_state, s, return FALSE);
+           r = GET_BITS(s);
+           s = HUFF_EXTEND(r, s);
+           /* Output coefficient in natural (dezigzagged) order.
+            * Note: the extra entries in natural_order[] will save us
+            * if k > Se, which could happen if the data is corrupted.
+            */
+           (*block)[natural_order[k]] = (JCOEF) s;
+         } else {
+           if (r != 15)
+             goto EndOfBlock;
+           k += 15;
+         }
+       }
       } else {
-        if (s) {
-          CHECK_BIT_BUFFER(br_state, s, return FALSE);
-          DROP_BITS(s);
-        }
+       if (s) {
+         CHECK_BIT_BUFFER(br_state, s, return FALSE);
+         DROP_BITS(s);
+       }
       }
 
       /* Section F.2.2.2: decode the AC coefficients */
       /* In this path we just discard the values */
       for (; k <= Se; k++) {
-        HUFF_DECODE(s, br_state, htbl, return FALSE, label3);
-
-        r = s >> 4;
-        s &= 15;
-
-        if (s) {
-          k += r;
-          CHECK_BIT_BUFFER(br_state, s, return FALSE);
-          DROP_BITS(s);
-        } else {
-          if (r != 15)
-            break;
-          k += 15;
-        }
+       HUFF_DECODE(s, br_state, htbl, return FALSE, label3);
+
+       r = s >> 4;
+       s &= 15;
+
+       if (s) {
+         k += r;
+         CHECK_BIT_BUFFER(br_state, s, return FALSE);
+         DROP_BITS(s);
+       } else {
+         if (r != 15)
+           break;
+         k += 15;
+       }
       }
 
       EndOfBlock: ;
@@ -1196,7 +1207,7 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   if (cinfo->restart_interval) {
     if (entropy->restarts_to_go == 0)
       if (! process_restart(cinfo))
-        return FALSE;
+       return FALSE;
   }
 
   /* If we've run out of data, just leave the MCU set to zeroes.
@@ -1226,66 +1237,66 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
       k = 1;
       coef_limit = entropy->coef_limit[blkn];
       if (coef_limit) {
-        /* Convert DC difference to actual value, update last_dc_val */
-        if (s) {
-          CHECK_BIT_BUFFER(br_state, s, return FALSE);
-          r = GET_BITS(s);
-          s = HUFF_EXTEND(r, s);
-        }
-        ci = cinfo->MCU_membership[blkn];
-        s += state.last_dc_val[ci];
-        state.last_dc_val[ci] = s;
-        /* Output the DC coefficient */
-        (*block)[0] = (JCOEF) s;
-
-        /* Section F.2.2.2: decode the AC coefficients */
-        /* Since zeroes are skipped, output area must be cleared beforehand */
-        for (; k < coef_limit; k++) {
-          HUFF_DECODE(s, br_state, htbl, return FALSE, label2);
-
-          r = s >> 4;
-          s &= 15;
-
-          if (s) {
-            k += r;
-            CHECK_BIT_BUFFER(br_state, s, return FALSE);
-            r = GET_BITS(s);
-            s = HUFF_EXTEND(r, s);
-            /* Output coefficient in natural (dezigzagged) order.
-             * Note: the extra entries in jpeg_natural_order[] will save us
-             * if k >= DCTSIZE2, which could happen if the data is corrupted.
-             */
-            (*block)[jpeg_natural_order[k]] = (JCOEF) s;
-          } else {
-            if (r != 15)
-              goto EndOfBlock;
-            k += 15;
-          }
-        }
+       /* Convert DC difference to actual value, update last_dc_val */
+       if (s) {
+         CHECK_BIT_BUFFER(br_state, s, return FALSE);
+         r = GET_BITS(s);
+         s = HUFF_EXTEND(r, s);
+       }
+       ci = cinfo->MCU_membership[blkn];
+       s += state.last_dc_val[ci];
+       state.last_dc_val[ci] = s;
+       /* Output the DC coefficient */
+       (*block)[0] = (JCOEF) s;
+
+       /* Section F.2.2.2: decode the AC coefficients */
+       /* Since zeroes are skipped, output area must be cleared beforehand */
+       for (; k < coef_limit; k++) {
+         HUFF_DECODE(s, br_state, htbl, return FALSE, label2);
+
+         r = s >> 4;
+         s &= 15;
+
+         if (s) {
+           k += r;
+           CHECK_BIT_BUFFER(br_state, s, return FALSE);
+           r = GET_BITS(s);
+           s = HUFF_EXTEND(r, s);
+           /* Output coefficient in natural (dezigzagged) order.
+            * Note: the extra entries in jpeg_natural_order[] will save us
+            * if k >= DCTSIZE2, which could happen if the data is corrupted.
+            */
+           (*block)[jpeg_natural_order[k]] = (JCOEF) s;
+         } else {
+           if (r != 15)
+             goto EndOfBlock;
+           k += 15;
+         }
+       }
       } else {
-        if (s) {
-          CHECK_BIT_BUFFER(br_state, s, return FALSE);
-          DROP_BITS(s);
-        }
+       if (s) {
+         CHECK_BIT_BUFFER(br_state, s, return FALSE);
+         DROP_BITS(s);
+       }
       }
 
       /* Section F.2.2.2: decode the AC coefficients */
       /* In this path we just discard the values */
       for (; k < DCTSIZE2; k++) {
-        HUFF_DECODE(s, br_state, htbl, return FALSE, label3);
-
-        r = s >> 4;
-        s &= 15;
-
-        if (s) {
-          k += r;
-          CHECK_BIT_BUFFER(br_state, s, return FALSE);
-          DROP_BITS(s);
-        } else {
-          if (r != 15)
-            break;
-          k += 15;
-        }
+       HUFF_DECODE(s, br_state, htbl, return FALSE, label3);
+
+       r = s >> 4;
+       s &= 15;
+
+       if (s) {
+         k += r;
+         CHECK_BIT_BUFFER(br_state, s, return FALSE);
+         DROP_BITS(s);
+       } else {
+         if (r != 15)
+           break;
+         k += 15;
+       }
       }
 
       EndOfBlock: ;
@@ -1318,19 +1329,19 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
     /* Validate progressive scan parameters */
     if (cinfo->Ss == 0) {
       if (cinfo->Se != 0)
-        goto bad;
+       goto bad;
     } else {
       /* need not check Ss/Se < 0 since they came from unsigned bytes */
       if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
-        goto bad;
+       goto bad;
       /* AC scans may have only one component */
       if (cinfo->comps_in_scan != 1)
-        goto bad;
+       goto bad;
     }
     if (cinfo->Ah != 0) {
       /* Successive approximation refinement scan: must have Al = Ah-1. */
       if (cinfo->Ah-1 != cinfo->Al)
-        goto bad;
+       goto bad;
     }
     if (cinfo->Al > 13) {      /* need not check for < 0 */
       /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
@@ -1341,7 +1352,7 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
        */
       bad:
       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
-               cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
+              cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
     }
     /* Update progression status, and verify that scan order is legal.
      * Note that inter-scan inconsistencies are treated as warnings
@@ -1351,26 +1362,26 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
       int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
       int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
       if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
-        WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
+       WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
       for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
-        int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
-        if (cinfo->Ah != expected)
-          WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
-        coef_bit_ptr[coefi] = cinfo->Al;
+       int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
+       if (cinfo->Ah != expected)
+         WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
+       coef_bit_ptr[coefi] = cinfo->Al;
       }
     }
 
     /* Select MCU decoding routine */
     if (cinfo->Ah == 0) {
       if (cinfo->Ss == 0)
-        entropy->pub.decode_mcu = decode_mcu_DC_first;
+       entropy->pub.decode_mcu = decode_mcu_DC_first;
       else
-        entropy->pub.decode_mcu = decode_mcu_AC_first;
+       entropy->pub.decode_mcu = decode_mcu_AC_first;
     } else {
       if (cinfo->Ss == 0)
-        entropy->pub.decode_mcu = decode_mcu_DC_refine;
+       entropy->pub.decode_mcu = decode_mcu_DC_refine;
       else
-        entropy->pub.decode_mcu = decode_mcu_AC_refine;
+       entropy->pub.decode_mcu = decode_mcu_AC_refine;
     }
 
     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
@@ -1379,17 +1390,17 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
        * We may build same derived table more than once, but it's not expensive.
        */
       if (cinfo->Ss == 0) {
-        if (cinfo->Ah == 0) {  /* DC refinement needs no table */
-          tbl = compptr->dc_tbl_no;
-          jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
-                                  & entropy->derived_tbls[tbl]);
-        }
+       if (cinfo->Ah == 0) {   /* DC refinement needs no table */
+         tbl = compptr->dc_tbl_no;
+         jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
+                                 & entropy->derived_tbls[tbl]);
+       }
       } else {
-        tbl = compptr->ac_tbl_no;
-        jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
-                                & entropy->derived_tbls[tbl]);
-        /* remember the single active table */
-        entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
+       tbl = compptr->ac_tbl_no;
+       jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
+                               & entropy->derived_tbls[tbl]);
+       /* remember the single active table */
+       entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
       }
       /* Initialize DC predictions to 0 */
       entropy->saved.last_dc_val[ci] = 0;
@@ -1403,8 +1414,8 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
      * there are some baseline files out there with all zeroes in these bytes.
      */
     if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
-        ((cinfo->is_baseline || cinfo->Se < DCTSIZE2) &&
-        cinfo->Se != cinfo->lim_Se))
+       ((cinfo->is_baseline || cinfo->Se < DCTSIZE2) &&
+       cinfo->Se != cinfo->lim_Se))
       WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
 
     /* Select MCU decoding routine */
@@ -1425,11 +1436,11 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
       /* We may do this more than once for a table, but it's not expensive */
       tbl = compptr->dc_tbl_no;
       jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
-                              & entropy->dc_derived_tbls[tbl]);
+                             & entropy->dc_derived_tbls[tbl]);
       if (cinfo->lim_Se) {     /* AC needs no table when not present */
-        tbl = compptr->ac_tbl_no;
-        jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
-                                & entropy->ac_derived_tbls[tbl]);
+       tbl = compptr->ac_tbl_no;
+       jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
+                               & entropy->ac_derived_tbls[tbl]);
       }
       /* Initialize DC predictions to 0 */
       entropy->saved.last_dc_val[ci] = 0;
@@ -1444,50 +1455,50 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
       entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
       /* Decide whether we really care about the coefficient values */
       if (compptr->component_needed) {
-        ci = compptr->DCT_v_scaled_size;
-        i = compptr->DCT_h_scaled_size;
-        switch (cinfo->lim_Se) {
-        case (1*1-1):
-          entropy->coef_limit[blkn] = 1;
-          break;
-        case (2*2-1):
-          if (ci <= 0 || ci > 2) ci = 2;
-          if (i <= 0 || i > 2) i = 2;
-          entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order2[ci - 1][i - 1];
-          break;
-        case (3*3-1):
-          if (ci <= 0 || ci > 3) ci = 3;
-          if (i <= 0 || i > 3) i = 3;
-          entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order3[ci - 1][i - 1];
-          break;
-        case (4*4-1):
-          if (ci <= 0 || ci > 4) ci = 4;
-          if (i <= 0 || i > 4) i = 4;
-          entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order4[ci - 1][i - 1];
-          break;
-        case (5*5-1):
-          if (ci <= 0 || ci > 5) ci = 5;
-          if (i <= 0 || i > 5) i = 5;
-          entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order5[ci - 1][i - 1];
-          break;
-        case (6*6-1):
-          if (ci <= 0 || ci > 6) ci = 6;
-          if (i <= 0 || i > 6) i = 6;
-          entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order6[ci - 1][i - 1];
-          break;
-        case (7*7-1):
-          if (ci <= 0 || ci > 7) ci = 7;
-          if (i <= 0 || i > 7) i = 7;
-          entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order7[ci - 1][i - 1];
-          break;
-        default:
-          if (ci <= 0 || ci > 8) ci = 8;
-          if (i <= 0 || i > 8) i = 8;
-          entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1];
-          break;
-        }
+       ci = compptr->DCT_v_scaled_size;
+       i = compptr->DCT_h_scaled_size;
+       switch (cinfo->lim_Se) {
+       case (1*1-1):
+         entropy->coef_limit[blkn] = 1;
+         break;
+       case (2*2-1):
+         if (ci <= 0 || ci > 2) ci = 2;
+         if (i <= 0 || i > 2) i = 2;
+         entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order2[ci - 1][i - 1];
+         break;
+       case (3*3-1):
+         if (ci <= 0 || ci > 3) ci = 3;
+         if (i <= 0 || i > 3) i = 3;
+         entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order3[ci - 1][i - 1];
+         break;
+       case (4*4-1):
+         if (ci <= 0 || ci > 4) ci = 4;
+         if (i <= 0 || i > 4) i = 4;
+         entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order4[ci - 1][i - 1];
+         break;
+       case (5*5-1):
+         if (ci <= 0 || ci > 5) ci = 5;
+         if (i <= 0 || i > 5) i = 5;
+         entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order5[ci - 1][i - 1];
+         break;
+       case (6*6-1):
+         if (ci <= 0 || ci > 6) ci = 6;
+         if (i <= 0 || i > 6) i = 6;
+         entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order6[ci - 1][i - 1];
+         break;
+       case (7*7-1):
+         if (ci <= 0 || ci > 7) ci = 7;
+         if (i <= 0 || i > 7) i = 7;
+         entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order7[ci - 1][i - 1];
+         break;
+       default:
+         if (ci <= 0 || ci > 8) ci = 8;
+         if (i <= 0 || i > 8) i = 8;
+         entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1];
+         break;
+       }
       } else {
-        entropy->coef_limit[blkn] = 0;
+       entropy->coef_limit[blkn] = 0;
       }
     }
   }
@@ -1514,20 +1525,21 @@ jinit_huff_decoder (j_decompress_ptr cinfo)
 
   entropy = (huff_entropy_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(huff_entropy_decoder));
+                               SIZEOF(huff_entropy_decoder));
   cinfo->entropy = &entropy->pub;
   entropy->pub.start_pass = start_pass_huff_decoder;
+  entropy->pub.finish_pass = finish_pass_huff;
 
   if (cinfo->progressive_mode) {
     /* Create progression status table */
     int *coef_bit_ptr, ci;
     cinfo->coef_bits = (int (*)[DCTSIZE2])
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  cinfo->num_components*DCTSIZE2*SIZEOF(int));
+                                 cinfo->num_components*DCTSIZE2*SIZEOF(int));
     coef_bit_ptr = & cinfo->coef_bits[0][0];
     for (ci = 0; ci < cinfo->num_components; ci++)
       for (i = 0; i < DCTSIZE2; i++)
-        *coef_bit_ptr++ = -1;
+       *coef_bit_ptr++ = -1;
 
     /* Mark derived tables unallocated */
     for (i = 0; i < NUM_HUFF_TBLS; i++) {
index 137b5c6..0199553 100644 (file)
@@ -2,7 +2,7 @@
  * jdinput.c
  *
  * Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 2002-2009 by Guido Vollbeding.
+ * Modified 2002-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -196,7 +196,7 @@ jpeg_core_output_dimensions (j_decompress_ptr cinfo)
   /* Hardwire it to "no scaling" */
   cinfo->output_width = cinfo->image_width;
   cinfo->output_height = cinfo->image_height;
-  /* jdinput.c has already initialized DCT_scaled_size,
+  /* initial_setup has already initialized DCT_scaled_size,
    * and has computed unscaled downsampled_width and downsampled_height.
    */
 
@@ -216,14 +216,14 @@ initial_setup (j_decompress_ptr cinfo)
       (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)
+  /* Only 8 to 12 bits data precision are supported for DCT based JPEG */
+  if (cinfo->data_precision < 8 || cinfo->data_precision > 12)
     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);
+            MAX_COMPONENTS);
 
   /* Compute maximum sampling factors; check factor validity */
   cinfo->max_h_samp_factor = 1;
@@ -231,12 +231,12 @@ initial_setup (j_decompress_ptr cinfo)
   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)
+       compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
       ERREXIT(cinfo, JERR_BAD_SAMPLING);
     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
-                                   compptr->h_samp_factor);
+                                  compptr->h_samp_factor);
     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
-                                   compptr->v_samp_factor);
+                                  compptr->v_samp_factor);
   }
 
   /* Derive block_size, natural_order, and lim_Se */
@@ -329,7 +329,7 @@ initial_setup (j_decompress_ptr cinfo)
       break;
     default:
       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
-               cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
+              cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
       break;
     }
 
@@ -350,10 +350,10 @@ initial_setup (j_decompress_ptr cinfo)
     /* 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 * cinfo->block_size));
+                   (long) (cinfo->max_h_samp_factor * cinfo->block_size));
     compptr->height_in_blocks = (JDIMENSION)
       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
-                    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
+                   (long) (cinfo->max_v_samp_factor * cinfo->block_size));
     /* downsampled_width and downsampled_height will also be overridden by
      * jdmaster.c if we are doing full decompression.  The transcoder library
      * doesn't use these values, but the calling application might.
@@ -361,10 +361,10 @@ initial_setup (j_decompress_ptr cinfo)
     /* 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);
+                   (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);
+                   (long) cinfo->max_v_samp_factor);
     /* Mark component needed, until color conversion says otherwise */
     compptr->component_needed = TRUE;
     /* Mark no quantization table yet saved for component */
@@ -374,7 +374,7 @@ initial_setup (j_decompress_ptr cinfo)
   /* Compute number of fully interleaved MCU rows. */
   cinfo->total_iMCU_rows = (JDIMENSION)
     jdiv_round_up((long) cinfo->image_height,
-                  (long) (cinfo->max_v_samp_factor * cinfo->block_size));
+                 (long) (cinfo->max_v_samp_factor * cinfo->block_size));
 
   /* Decide whether file contains multiple scans */
   if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
@@ -391,16 +391,16 @@ per_scan_setup (j_decompress_ptr cinfo)
 {
   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;
@@ -413,28 +413,28 @@ per_scan_setup (j_decompress_ptr cinfo)
     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
     if (tmp == 0) tmp = compptr->v_samp_factor;
     compptr->last_row_height = tmp;
-
+    
     /* 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);
-
+              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 * cinfo->block_size));
+                   (long) (cinfo->max_h_samp_factor * cinfo->block_size));
     cinfo->MCU_rows_in_scan = (JDIMENSION)
       jdiv_round_up((long) cinfo->image_height,
-                    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
-
+                   (long) (cinfo->max_v_samp_factor * cinfo->block_size));
+    
     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 */
@@ -452,12 +452,12 @@ per_scan_setup (j_decompress_ptr cinfo)
       /* Prepare array describing MCU composition */
       mcublks = compptr->MCU_blocks;
       if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
-        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
+       ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
       while (mcublks-- > 0) {
-        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
+       cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
       }
     }
-
+    
   }
 }
 
@@ -498,12 +498,12 @@ latch_quant_tables (j_decompress_ptr cinfo)
     /* Make sure specified quantization table is present */
     qtblno = compptr->quant_tbl_no;
     if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
-        cinfo->quant_tbl_ptrs[qtblno] == NULL)
+       cinfo->quant_tbl_ptrs[qtblno] == NULL)
       ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
     /* OK, save away the quantization table */
     qtbl = (JQUANT_TBL *)
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  SIZEOF(JQUANT_TBL));
+                                 SIZEOF(JQUANT_TBL));
     MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
     compptr->quant_table = qtbl;
   }
@@ -537,6 +537,7 @@ start_input_pass (j_decompress_ptr cinfo)
 METHODDEF(void)
 finish_input_pass (j_decompress_ptr cinfo)
 {
+  (*cinfo->entropy->finish_pass) (cinfo);
   cinfo->inputctl->consume_input = consume_markers;
 }
 
@@ -570,36 +571,36 @@ consume_markers (j_decompress_ptr cinfo)
     switch (val) {
     case JPEG_REACHED_SOS:     /* Found SOS */
       if (inputctl->inheaders) { /* 1st SOS */
-        if (inputctl->inheaders == 1)
-          initial_setup(cinfo);
-        if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */
-          inputctl->inheaders = 2;
-          break;
-        }
-        inputctl->inheaders = 0;
-        /* Note: start_input_pass must be called by jdmaster.c
-         * before any more input can be consumed.  jdapimin.c is
-         * responsible for enforcing this sequencing.
-         */
+       if (inputctl->inheaders == 1)
+         initial_setup(cinfo);
+       if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */
+         inputctl->inheaders = 2;
+         break;
+       }
+       inputctl->inheaders = 0;
+       /* Note: start_input_pass must be called by jdmaster.c
+        * before any more input can be consumed.  jdapimin.c is
+        * responsible for enforcing this sequencing.
+        */
       } else {                 /* 2nd or later SOS marker */
-        if (! inputctl->pub.has_multiple_scans)
-          ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
-        if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */
-          break;
-        start_input_pass(cinfo);
+       if (! inputctl->pub.has_multiple_scans)
+         ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
+       if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */
+         break;
+       start_input_pass(cinfo);
       }
       return val;
     case JPEG_REACHED_EOI:     /* Found EOI */
       inputctl->pub.eoi_reached = TRUE;
       if (inputctl->inheaders) { /* Tables-only datastream, apparently */
-        if (cinfo->marker->saw_SOF)
-          ERREXIT(cinfo, JERR_SOF_NO_SOS);
+       if (cinfo->marker->saw_SOF)
+         ERREXIT(cinfo, JERR_SOF_NO_SOS);
       } else {
-        /* Prevent infinite loop in coef ctlr's decompress_data routine
-         * if user set output_scan_number larger than number of scans.
-         */
-        if (cinfo->output_scan_number > cinfo->input_scan_number)
-          cinfo->output_scan_number = cinfo->input_scan_number;
+       /* Prevent infinite loop in coef ctlr's decompress_data routine
+        * if user set output_scan_number larger than number of scans.
+        */
+       if (cinfo->output_scan_number > cinfo->input_scan_number)
+         cinfo->output_scan_number = cinfo->input_scan_number;
       }
       return val;
     case JPEG_SUSPENDED:
@@ -645,8 +646,8 @@ jinit_input_controller (j_decompress_ptr cinfo)
   /* Create subobject in permanent pool */
   inputctl = (my_inputctl_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
-                                SIZEOF(my_input_controller));
-  cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
+                               SIZEOF(my_input_controller));
+  cinfo->inputctl = &inputctl->pub;
   /* Initialize method pointers */
   inputctl->pub.consume_input = consume_markers;
   inputctl->pub.reset_input_controller = reset_input_controller;
index 21829bf..52091fb 100644 (file)
@@ -142,15 +142,15 @@ typedef my_main_controller * my_main_ptr;
 
 /* Forward declarations */
 METHODDEF(void) process_data_simple_main
-        JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
-             JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+       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));
+       JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+            JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
 #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));
+       JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+            JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
 #endif
 
 
@@ -171,7 +171,7 @@ alloc_funny_pointers (j_decompress_ptr cinfo)
    */
   mainp->xbuffer[0] = (JSAMPIMAGE)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
+                               cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
   mainp->xbuffer[1] = mainp->xbuffer[0] + cinfo->num_components;
 
   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
@@ -183,7 +183,7 @@ alloc_funny_pointers (j_decompress_ptr cinfo)
      */
     xbuf = (JSAMPARRAY)
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                  2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
+                                 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
     xbuf += rgroup;            /* want one row group at negative offsets */
     mainp->xbuffer[0][ci] = xbuf;
     xbuf += rgroup * (M + 4);
@@ -344,8 +344,8 @@ start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
 
 METHODDEF(void)
 process_data_simple_main (j_decompress_ptr cinfo,
-                          JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
-                          JDIMENSION out_rows_avail)
+                         JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+                         JDIMENSION out_rows_avail)
 {
   my_main_ptr mainp = (my_main_ptr) cinfo->main;
   JDIMENSION rowgroups_avail;
@@ -366,8 +366,8 @@ process_data_simple_main (j_decompress_ptr cinfo,
 
   /* Feed the postprocessor */
   (*cinfo->post->post_process_data) (cinfo, mainp->buffer,
-                                     &mainp->rowgroup_ctr, rowgroups_avail,
-                                     output_buf, out_row_ctr, out_rows_avail);
+                                    &mainp->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 (mainp->rowgroup_ctr >= rowgroups_avail) {
@@ -384,15 +384,15 @@ process_data_simple_main (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 process_data_context_main (j_decompress_ptr cinfo,
-                           JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
-                           JDIMENSION out_rows_avail)
+                          JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+                          JDIMENSION out_rows_avail)
 {
   my_main_ptr mainp = (my_main_ptr) cinfo->main;
 
   /* Read input data if we haven't filled the main buffer yet */
   if (! mainp->buffer_full) {
     if (! (*cinfo->coef->decompress_data) (cinfo,
-                                           mainp->xbuffer[mainp->whichptr]))
+                                          mainp->xbuffer[mainp->whichptr]))
       return;                  /* suspension forced, can do nothing more */
     mainp->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
     mainp->iMCU_row_ctr++;     /* count rows received */
@@ -407,8 +407,8 @@ process_data_context_main (j_decompress_ptr cinfo,
   case CTX_POSTPONED_ROW:
     /* Call postprocessor using previously set pointers for postponed row */
     (*cinfo->post->post_process_data) (cinfo, mainp->xbuffer[mainp->whichptr],
-                        &mainp->rowgroup_ctr, mainp->rowgroups_avail,
-                        output_buf, out_row_ctr, out_rows_avail);
+                       &mainp->rowgroup_ctr, mainp->rowgroups_avail,
+                       output_buf, out_row_ctr, out_rows_avail);
     if (mainp->rowgroup_ctr < mainp->rowgroups_avail)
       return;                  /* Need to suspend */
     mainp->context_state = CTX_PREPARE_FOR_IMCU;
@@ -429,8 +429,8 @@ process_data_context_main (j_decompress_ptr cinfo,
   case CTX_PROCESS_IMCU:
     /* Call postprocessor using previously set pointers */
     (*cinfo->post->post_process_data) (cinfo, mainp->xbuffer[mainp->whichptr],
-                        &mainp->rowgroup_ctr, mainp->rowgroups_avail,
-                        output_buf, out_row_ctr, out_rows_avail);
+                       &mainp->rowgroup_ctr, mainp->rowgroups_avail,
+                       output_buf, out_row_ctr, out_rows_avail);
     if (mainp->rowgroup_ctr < mainp->rowgroups_avail)
       return;                  /* Need to suspend */
     /* After the first iMCU, change wraparound pointers to normal state */
@@ -458,12 +458,12 @@ process_data_context_main (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 process_data_crank_post (j_decompress_ptr cinfo,
-                         JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
-                         JDIMENSION out_rows_avail)
+                        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);
+                                    (JDIMENSION *) NULL, (JDIMENSION) 0,
+                                    output_buf, out_row_ctr, out_rows_avail);
 }
 
 #endif /* QUANT_2PASS_SUPPORTED */
@@ -482,7 +482,7 @@ jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
 
   mainp = (my_main_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_main_controller));
+                               SIZEOF(my_main_controller));
   cinfo->main = &mainp->pub;
   mainp->pub.start_pass = start_pass_main;
 
index d3fe514..3fbe5c1 100644 (file)
@@ -2,7 +2,7 @@
  * jdmarker.c
  *
  * Copyright (C) 1991-1998, Thomas G. Lane.
- * Modified 2009-2012 by Guido Vollbeding.
+ * Modified 2009-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -119,49 +119,49 @@ typedef my_marker_reader * my_marker_ptr;
 
 /* 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
+       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 )
+       ( datasrc->next_input_byte = next_input_byte,  \
+         datasrc->bytes_in_buffer = bytes_in_buffer )
 
 /* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
 #define INPUT_RELOAD(cinfo)  \
-        ( next_input_byte = datasrc->next_input_byte,  \
-          bytes_in_buffer = datasrc->bytes_in_buffer )
+       ( 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);  \
-        }
+       if (bytes_in_buffer == 0) {  \
+         if (! (*datasrc->fill_input_buffer) (cinfo))  \
+           { action; }  \
+         INPUT_RELOAD(cinfo);  \
+       }
 
 /* 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); \
-                  bytes_in_buffer--; \
-                  V = GETJOCTET(*next_input_byte++); )
+       MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
+                 bytes_in_buffer--; \
+                 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); \
-                  bytes_in_buffer--; \
-                  V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
-                  MAKE_BYTE_AVAIL(cinfo,action); \
-                  bytes_in_buffer--; \
-                  V += GETJOCTET(*next_input_byte++); )
+       MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
+                 bytes_in_buffer--; \
+                 V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
+                 MAKE_BYTE_AVAIL(cinfo,action); \
+                 bytes_in_buffer--; \
+                 V += GETJOCTET(*next_input_byte++); )
 
 
 /*
@@ -200,7 +200,7 @@ get_soi (j_decompress_ptr cinfo)
 /* Process an SOI marker */
 {
   int i;
-
+  
   TRACEMS(cinfo, 1, JTRC_SOI);
 
   if (cinfo->marker->saw_SOI)
@@ -238,7 +238,7 @@ get_soi (j_decompress_ptr cinfo)
 
 LOCAL(boolean)
 get_sof (j_decompress_ptr cinfo, boolean is_baseline, boolean is_prog,
-         boolean is_arith)
+        boolean is_arith)
 /* Process a SOFn marker */
 {
   INT32 length;
@@ -260,8 +260,8 @@ get_sof (j_decompress_ptr cinfo, boolean is_baseline, boolean is_prog,
   length -= 8;
 
   TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
-           (int) cinfo->image_width, (int) cinfo->image_height,
-           cinfo->num_components);
+          (int) cinfo->image_width, (int) cinfo->image_height,
+          cinfo->num_components);
 
   if (cinfo->marker->saw_SOF)
     ERREXIT(cinfo, JERR_SOF_DUPLICATE);
@@ -269,8 +269,8 @@ get_sof (j_decompress_ptr cinfo, boolean is_baseline, boolean is_prog,
   /* 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)
+  if (cinfo->image_height <= 0 || cinfo->image_width <= 0 ||
+      cinfo->num_components <= 0)
     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
 
   if (length != (cinfo->num_components * 3))
@@ -278,8 +278,8 @@ get_sof (j_decompress_ptr cinfo, boolean is_baseline, boolean is_prog,
 
   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));
+                       ((j_common_ptr) cinfo, JPOOL_IMAGE,
+                        cinfo->num_components * SIZEOF(jpeg_component_info));
 
   for (ci = 0; ci < cinfo->num_components; ci++) {
     INPUT_BYTE(cinfo, c, return FALSE);
@@ -289,14 +289,14 @@ get_sof (j_decompress_ptr cinfo, boolean is_baseline, boolean is_prog,
     /* max id seen so far + 1. */
     for (i = 0, compptr = cinfo->comp_info; i < ci; i++, compptr++) {
       if (c == compptr->component_id) {
-        compptr = cinfo->comp_info;
-        c = compptr->component_id;
-        compptr++;
-        for (i = 1; i < ci; i++, compptr++) {
-          if (compptr->component_id > c) c = compptr->component_id;
-        }
-        c++;
-        break;
+       compptr = cinfo->comp_info;
+       c = compptr->component_id;
+       compptr++;
+       for (i = 1; i < ci; i++, compptr++) {
+         if (compptr->component_id > c) c = compptr->component_id;
+       }
+       c++;
+       break;
       }
     }
     compptr->component_id = c;
@@ -307,8 +307,8 @@ get_sof (j_decompress_ptr cinfo, boolean is_baseline, boolean is_prog,
     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);
+            compptr->component_id, compptr->h_samp_factor,
+            compptr->v_samp_factor, compptr->quant_tbl_no);
   }
 
   cinfo->marker->saw_SOF = TRUE;
@@ -350,22 +350,25 @@ get_sos (j_decompress_ptr cinfo)
 
     /* Detect the case where component id's are not unique, and, if so, */
     /* create a fake component id using the same logic as in get_sof.   */
+    /* Note:  This also ensures that all of the SOF components are      */
+    /* referenced in the single scan case, which prevents access to     */
+    /* uninitialized memory in later decoding stages. */
     for (ci = 0; ci < i; ci++) {
       if (c == cinfo->cur_comp_info[ci]->component_id) {
-        c = cinfo->cur_comp_info[0]->component_id;
-        for (ci = 1; ci < i; ci++) {
-          compptr = cinfo->cur_comp_info[ci];
-          if (compptr->component_id > c) c = compptr->component_id;
-        }
-        c++;
-        break;
+       c = cinfo->cur_comp_info[0]->component_id;
+       for (ci = 1; ci < i; ci++) {
+         compptr = cinfo->cur_comp_info[ci];
+         if (compptr->component_id > c) c = compptr->component_id;
+       }
+       c++;
+       break;
       }
     }
 
     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
-         ci++, compptr++) {
+        ci++, compptr++) {
       if (c == compptr->component_id)
-        goto id_found;
+       goto id_found;
     }
 
     ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, c);
@@ -378,7 +381,7 @@ get_sos (j_decompress_ptr cinfo)
     compptr->ac_tbl_no = (c     ) & 15;
 
     TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, compptr->component_id,
-             compptr->dc_tbl_no, compptr->ac_tbl_no);
+            compptr->dc_tbl_no, compptr->ac_tbl_no);
   }
 
   /* Collect the additional scan parameters Ss, Se, Ah/Al. */
@@ -391,7 +394,7 @@ get_sos (j_decompress_ptr cinfo)
   cinfo->Al = (c     ) & 15;
 
   TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
-           cinfo->Ah, cinfo->Al);
+          cinfo->Ah, cinfo->Al);
 
   /* Prepare to scan data & restart markers */
   cinfo->marker->next_restart_num = 0;
@@ -416,7 +419,7 @@ get_dac (j_decompress_ptr cinfo)
 
   INPUT_2BYTES(cinfo, length, return FALSE);
   length -= 2;
-
+  
   while (length > 0) {
     INPUT_BYTE(cinfo, index, return FALSE);
     INPUT_BYTE(cinfo, val, return FALSE);
@@ -434,7 +437,7 @@ get_dac (j_decompress_ptr cinfo)
       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);
+       ERREXIT1(cinfo, JERR_DAC_VALUE, val);
     }
   }
 
@@ -465,12 +468,12 @@ get_dht (j_decompress_ptr cinfo)
 
   INPUT_2BYTES(cinfo, length, return FALSE);
   length -= 2;
-
+  
   while (length > 16) {
     INPUT_BYTE(cinfo, index, return FALSE);
 
     TRACEMS1(cinfo, 1, JTRC_DHT, index);
-
+      
     bits[0] = 0;
     count = 0;
     for (i = 1; i <= 16; i++) {
@@ -481,11 +484,11 @@ get_dht (j_decompress_ptr cinfo)
     length -= 1 + 16;
 
     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
-             bits[1], bits[2], bits[3], bits[4],
-             bits[5], bits[6], bits[7], bits[8]);
+            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]);
+            bits[9], bits[10], bits[11], bits[12],
+            bits[13], bits[14], bits[15], bits[16]);
 
     /* Here we just do minimal validation of the counts to avoid walking
      * off the end of our table space.  jdhuff.c will check more carefully.
@@ -493,6 +496,8 @@ get_dht (j_decompress_ptr cinfo)
     if (count > 256 || ((INT32) count) > length)
       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
 
+    MEMZERO(huffval, SIZEOF(huffval)); /* pre-zero array for later copy */
+
     for (i = 0; i < count; i++)
       INPUT_BYTE(cinfo, huffval[i], return FALSE);
 
@@ -510,7 +515,7 @@ get_dht (j_decompress_ptr cinfo)
 
     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));
   }
@@ -547,29 +552,29 @@ get_dqt (j_decompress_ptr cinfo)
 
     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];
 
     if (prec) {
       if (length < DCTSIZE2 * 2) {
-        /* Initialize full table for safety. */
-        for (i = 0; i < DCTSIZE2; i++) {
-          quant_ptr->quantval[i] = 1;
-        }
-        count = length >> 1;
+       /* Initialize full table for safety. */
+       for (i = 0; i < DCTSIZE2; i++) {
+         quant_ptr->quantval[i] = 1;
+       }
+       count = length >> 1;
       } else
-        count = DCTSIZE2;
+       count = DCTSIZE2;
     } else {
       if (length < DCTSIZE2) {
-        /* Initialize full table for safety. */
-        for (i = 0; i < DCTSIZE2; i++) {
-          quant_ptr->quantval[i] = 1;
-        }
-        count = length;
+       /* Initialize full table for safety. */
+       for (i = 0; i < DCTSIZE2; i++) {
+         quant_ptr->quantval[i] = 1;
+       }
+       count = length;
       } else
-        count = DCTSIZE2;
+       count = DCTSIZE2;
     }
 
     switch (count) {
@@ -584,20 +589,20 @@ get_dqt (j_decompress_ptr cinfo)
 
     for (i = 0; i < count; i++) {
       if (prec)
-        INPUT_2BYTES(cinfo, tmp, return FALSE);
+       INPUT_2BYTES(cinfo, tmp, return FALSE);
       else
-        INPUT_BYTE(cinfo, tmp, return FALSE);
+       INPUT_BYTE(cinfo, tmp, return FALSE);
       /* We convert the zigzag-order table to natural array order. */
       quant_ptr->quantval[natural_order[i]] = (UINT16) tmp;
     }
 
     if (cinfo->err->trace_level >= 2) {
       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]);
+       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]);
       }
     }
 
@@ -622,7 +627,7 @@ get_dri (j_decompress_ptr cinfo)
   INPUT_VARS(cinfo);
 
   INPUT_2BYTES(cinfo, length, return FALSE);
-
+  
   if (length != 4)
     ERREXIT(cinfo, JERR_BAD_LENGTH);
 
@@ -713,7 +718,7 @@ get_lse (j_decompress_ptr cinfo)
 
 LOCAL(void)
 examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
-              unsigned int datalen, INT32 remaining)
+             unsigned int datalen, INT32 remaining)
 /* Examine first few bytes from an APP0.
  * Take appropriate action if it is a JFIF marker.
  * datalen is # of bytes at data[], remaining is length of rest of marker data.
@@ -735,25 +740,26 @@ examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
     cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
     cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
     /* Check version.
-     * Major version must be 1, anything else signals an incompatible change.
+     * Major version must be 1 or 2, anything else signals an incompatible
+     * change.
      * (We used to treat this as an error, but now it's a nonfatal warning,
      * because some bozo at Hijaak couldn't read the spec.)
      * Minor version should be 0..2, but process anyway if newer.
      */
-    if (cinfo->JFIF_major_version != 1)
+    if (cinfo->JFIF_major_version != 1 && cinfo->JFIF_major_version != 2)
       WARNMS2(cinfo, JWRN_JFIF_MAJOR,
-              cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
+             cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
     /* Generate trace messages */
     TRACEMS5(cinfo, 1, JTRC_JFIF,
-             cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
-             cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
+            cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
+            cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
     /* Validate thumbnail dimensions and issue appropriate messages */
     if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
       TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
-               GETJOCTET(data[12]), GETJOCTET(data[13]));
+              GETJOCTET(data[12]), GETJOCTET(data[13]));
     totallen -= APP0_DATA_LEN;
     if (totallen !=
-        ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
+       ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
       TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
   } else if (datalen >= 6 &&
       GETJOCTET(data[0]) == 0x4A &&
@@ -777,7 +783,7 @@ examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
       break;
     default:
       TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
-               GETJOCTET(data[5]), (int) totallen);
+              GETJOCTET(data[5]), (int) totallen);
       break;
     }
   } else {
@@ -789,7 +795,7 @@ examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
 
 LOCAL(void)
 examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
-               unsigned int datalen, INT32 remaining)
+              unsigned int datalen, INT32 remaining)
 /* Examine first few bytes from an APP14.
  * Take appropriate action if it is an Adobe marker.
  * datalen is # of bytes at data[], remaining is length of rest of marker data.
@@ -885,15 +891,15 @@ save_marker (j_decompress_ptr cinfo)
       /* figure out how much we want to save */
       unsigned int limit;
       if (cinfo->unread_marker == (int) M_COM)
-        limit = marker->length_limit_COM;
+       limit = marker->length_limit_COM;
       else
-        limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
+       limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
       if ((unsigned int) length < limit)
-        limit = (unsigned int) length;
+       limit = (unsigned int) length;
       /* allocate and initialize the marker item */
       cur_marker = (jpeg_saved_marker_ptr)
-        (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                    SIZEOF(struct jpeg_marker_struct) + limit);
+       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+                                   SIZEOF(struct jpeg_marker_struct) + limit);
       cur_marker->next = NULL;
       cur_marker->marker = (UINT8) cinfo->unread_marker;
       cur_marker->original_length = (unsigned int) length;
@@ -937,7 +943,7 @@ save_marker (j_decompress_ptr cinfo)
     } else {
       jpeg_saved_marker_ptr prev = cinfo->marker_list;
       while (prev->next != NULL)
-        prev = prev->next;
+       prev = prev->next;
       prev->next = cur_marker;
     }
     /* Reset pointer & calc remaining data length */
@@ -957,7 +963,7 @@ save_marker (j_decompress_ptr cinfo)
     break;
   default:
     TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
-             (int) (data_length + length));
+            (int) (data_length + length));
     break;
   }
 
@@ -981,7 +987,7 @@ skip_variable (j_decompress_ptr cinfo)
 
   INPUT_2BYTES(cinfo, length, return FALSE);
   length -= 2;
-
+  
   TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
 
   INPUT_SYNC(cinfo);           /* do before skip_input_data */
@@ -1093,11 +1099,11 @@ read_markers (j_decompress_ptr cinfo)
     /* 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;
+       if (! first_marker(cinfo))
+         return JPEG_SUSPENDED;
       } else {
-        if (! next_marker(cinfo))
-          return JPEG_SUSPENDED;
+       if (! next_marker(cinfo))
+         return JPEG_SUSPENDED;
       }
     }
     /* At this point cinfo->unread_marker contains the marker code and the
@@ -1107,32 +1113,32 @@ read_markers (j_decompress_ptr cinfo)
     switch (cinfo->unread_marker) {
     case M_SOI:
       if (! get_soi(cinfo))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_SOF0:               /* Baseline */
       if (! get_sof(cinfo, TRUE, FALSE, FALSE))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_SOF1:               /* Extended sequential, Huffman */
       if (! get_sof(cinfo, FALSE, FALSE, FALSE))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_SOF2:               /* Progressive, Huffman */
       if (! get_sof(cinfo, FALSE, TRUE, FALSE))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_SOF9:               /* Extended sequential, arithmetic */
       if (! get_sof(cinfo, FALSE, FALSE, TRUE))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_SOF10:              /* Progressive, arithmetic */
       if (! get_sof(cinfo, FALSE, TRUE, TRUE))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     /* Currently unsupported SOFn types */
@@ -1150,7 +1156,7 @@ read_markers (j_decompress_ptr cinfo)
 
     case M_SOS:
       if (! get_sos(cinfo))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       cinfo->unread_marker = 0;        /* processed the marker */
       return JPEG_REACHED_SOS;
 
@@ -1161,27 +1167,27 @@ read_markers (j_decompress_ptr cinfo)
 
     case M_DAC:
       if (! get_dac(cinfo))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_DHT:
       if (! get_dht(cinfo))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_DQT:
       if (! get_dqt(cinfo))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_DRI:
       if (! get_dri(cinfo))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_JPG8:
       if (! get_lse(cinfo))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_APP0:
@@ -1201,13 +1207,13 @@ read_markers (j_decompress_ptr cinfo)
     case M_APP14:
     case M_APP15:
       if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
-                cinfo->unread_marker - (int) M_APP0]) (cinfo))
-        return JPEG_SUSPENDED;
+               cinfo->unread_marker - (int) M_APP0]) (cinfo))
+       return JPEG_SUSPENDED;
       break;
 
     case M_COM:
       if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     case M_RST0:               /* these are all parameterless */
@@ -1224,7 +1230,7 @@ read_markers (j_decompress_ptr cinfo)
 
     case M_DNL:                        /* Ignore DNL ... perhaps the wrong thing */
       if (! skip_variable(cinfo))
-        return JPEG_SUSPENDED;
+       return JPEG_SUSPENDED;
       break;
 
     default:                   /* must be DHP, EXP, JPGn, or RESn */
@@ -1273,7 +1279,7 @@ read_restart_marker (j_decompress_ptr cinfo)
     /* 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,
-                                            cinfo->marker->next_restart_num))
+                                           cinfo->marker->next_restart_num))
       return FALSE;
   }
 
@@ -1338,10 +1344,10 @@ jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
 {
   int marker = cinfo->unread_marker;
   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)
@@ -1350,13 +1356,13 @@ jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
       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 */
+         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 */
+              marker == ((int) M_RST0 + ((desired-2) & 7)))
+       action = 2;             /* a prior restart, so advance */
       else
-        action = 1;            /* desired restart or too far away */
+       action = 1;             /* desired restart or too far away */
     }
     TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
     switch (action) {
@@ -1367,7 +1373,7 @@ jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
     case 2:
       /* Scan to the next marker, and repeat the decision loop. */
       if (! next_marker(cinfo))
-        return FALSE;
+       return FALSE;
       marker = cinfo->unread_marker;
       break;
     case 3:
@@ -1412,7 +1418,7 @@ jinit_marker_reader (j_decompress_ptr cinfo)
   /* Create subobject in permanent pool */
   marker = (my_marker_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
-                                SIZEOF(my_marker_reader));
+                               SIZEOF(my_marker_reader));
   cinfo->marker = &marker->pub;
   /* Initialize public method pointers */
   marker->pub.reset_marker_reader = reset_marker_reader;
@@ -1443,7 +1449,7 @@ jinit_marker_reader (j_decompress_ptr cinfo)
 
 GLOBAL(void)
 jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
-                   unsigned int length_limit)
+                  unsigned int length_limit)
 {
   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
   long maxlength;
@@ -1492,7 +1498,7 @@ jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
 
 GLOBAL(void)
 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
-                           jpeg_marker_parser_method routine)
+                          jpeg_marker_parser_method routine)
 {
   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 
index 6c9b166..ab95090 100644 (file)
@@ -2,7 +2,7 @@
  * jdmaster.c
  *
  * Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 2002-2011 by Guido Vollbeding.
+ * Modified 2002-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -45,13 +45,26 @@ LOCAL(boolean)
 use_merged_upsample (j_decompress_ptr cinfo)
 {
 #ifdef UPSAMPLE_MERGING_SUPPORTED
-  /* Merging is the equivalent of plain box-filter upsampling */
-  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
+  /* Merging is the equivalent of plain box-filter upsampling. */
+  /* The following condition is only needed if fancy shall select
+   * a different upsampling method.  In our current implementation
+   * fancy only affects the DCT scaling, thus we can use fancy
+   * upsampling and merged upsample simultaneously, in particular
+   * with scaled DCT sizes larger than the default DCTSIZE.
+   */
+#if 0
+  if (cinfo->do_fancy_upsampling)
+    return FALSE;
+#endif
+  if (cinfo->CCIR601_sampling)
     return FALSE;
   /* jdmerge.c only supports YCC=>RGB color conversion */
-  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
+  if ((cinfo->jpeg_color_space != JCS_YCbCr &&
+       cinfo->jpeg_color_space != JCS_BG_YCC) ||
+      cinfo->num_components != 3 ||
       cinfo->out_color_space != JCS_RGB ||
-      cinfo->out_color_components != RGB_PIXELSIZE)
+      cinfo->out_color_components != RGB_PIXELSIZE ||
+      cinfo->color_transform)
     return FALSE;
   /* and it only handles 2h1v or 2h2v sampling ratios */
   if (cinfo->comp_info[0].h_samp_factor != 2 ||
@@ -113,24 +126,24 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
        ci++, compptr++) {
     int ssize = 1;
     while (cinfo->min_DCT_h_scaled_size * ssize <=
-           (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
-           (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
+          (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
+          (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
       ssize = ssize * 2;
     }
     compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
     ssize = 1;
     while (cinfo->min_DCT_v_scaled_size * ssize <=
-           (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
-           (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
+          (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
+          (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
       ssize = ssize * 2;
     }
     compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
 
     /* We don't support IDCT ratios larger than 2. */
     if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
-        compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
+       compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
     else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
-        compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
+       compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
   }
 
   /* Recompute downsampled dimensions of components;
@@ -141,12 +154,12 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
     /* Size in samples, after IDCT scaling */
     compptr->downsampled_width = (JDIMENSION)
       jdiv_round_up((long) cinfo->image_width *
-                    (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
-                    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
+                   (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
+                   (long) (cinfo->max_h_samp_factor * cinfo->block_size));
     compptr->downsampled_height = (JDIMENSION)
       jdiv_round_up((long) cinfo->image_height *
-                    (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
-                    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
+                   (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
+                   (long) (cinfo->max_v_samp_factor * cinfo->block_size));
   }
 
 #endif /* IDCT_SCALING_SUPPORTED */
@@ -158,9 +171,11 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
     cinfo->out_color_components = 1;
     break;
   case JCS_RGB:
+  case JCS_BG_RGB:
     cinfo->out_color_components = RGB_PIXELSIZE;
     break;
   case JCS_YCbCr:
+  case JCS_BG_YCC:
     cinfo->out_color_components = 3;
     break;
   case JCS_CMYK:
@@ -172,7 +187,7 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
     break;
   }
   cinfo->output_components = (cinfo->quantize_colors ? 1 :
-                              cinfo->out_color_components);
+                             cinfo->out_color_components);
 
   /* See if upsampler will want to emit more than one row at a time */
   if (use_merged_upsample(cinfo))
@@ -196,30 +211,20 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
  * 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
+ * of x is within 2*(MAXJSAMPLE+1) of the legal range, so a table running
+ * from -2*(MAXJSAMPLE+1) to 3*MAXJSAMPLE+2 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];
+ *             x = (sample_range_limit - SUBSET)[(x + CENTER) & 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.
+ * This is accomplished with SUBSET = CENTER - CENTERJSAMPLE.
  *
  * Note that the table is allocated in near data space on PCs; it's small
  * enough and used often enough to justify this.
@@ -234,23 +239,17 @@ prepare_range_limit_table (j_decompress_ptr cinfo)
 
   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 */
+                               5 * (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
+  /* First segment of range limit table: limit[x] = 0 for x < 0 */
+  MEMZERO(table, 2 * (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
+  table += 2 * (MAXJSAMPLE+1); /* allow negative subscripts of 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 */
+  /* Main part of range limit 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++)
+  /* End of range limit table: limit[x] = MAXJSAMPLE for x > MAXJSAMPLE */
+  for (; i < 3 * (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));
 }
 
 
@@ -273,10 +272,19 @@ master_selection (j_decompress_ptr cinfo)
   long samplesperrow;
   JDIMENSION jd_samplesperrow;
 
+  /* For now, precision must match compiled-in value... */
+  if (cinfo->data_precision != BITS_IN_JSAMPLE)
+    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
+
   /* Initialize dimensions and other stuff */
   jpeg_calc_output_dimensions(cinfo);
   prepare_range_limit_table(cinfo);
 
+  /* Sanity check on image dimensions */
+  if (cinfo->output_height <= 0 || cinfo->output_width <= 0 ||
+      cinfo->out_color_components <= 0)
+    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
+
   /* Width of an output scanline must be representable as JDIMENSION. */
   samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
   jd_samplesperrow = (JDIMENSION) samplesperrow;
@@ -427,24 +435,24 @@ prepare_for_output_pass (j_decompress_ptr cinfo)
     if (cinfo->quantize_colors && cinfo->colormap == NULL) {
       /* Select new quantization method */
       if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
-        cinfo->cquantize = master->quantizer_2pass;
-        master->pub.is_dummy_pass = TRUE;
+       cinfo->cquantize = master->quantizer_2pass;
+       master->pub.is_dummy_pass = TRUE;
       } else if (cinfo->enable_1pass_quant) {
-        cinfo->cquantize = master->quantizer_1pass;
+       cinfo->cquantize = master->quantizer_1pass;
       } else {
-        ERREXIT(cinfo, JERR_MODE_CHANGE);
+       ERREXIT(cinfo, JERR_MODE_CHANGE);
       }
     }
     (*cinfo->idct->start_pass) (cinfo);
     (*cinfo->coef->start_output_pass) (cinfo);
     if (! cinfo->raw_data_out) {
       if (! master->using_merged_upsample)
-        (*cinfo->cconvert->start_pass) (cinfo);
+       (*cinfo->cconvert->start_pass) (cinfo);
       (*cinfo->upsample->start_pass) (cinfo);
       if (cinfo->quantize_colors)
-        (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
+       (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
       (*cinfo->post->start_pass) (cinfo,
-            (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
+           (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
       (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
     }
   }
@@ -453,7 +461,7 @@ prepare_for_output_pass (j_decompress_ptr cinfo)
   if (cinfo->progress != NULL) {
     cinfo->progress->completed_passes = master->pass_number;
     cinfo->progress->total_passes = master->pass_number +
-                                    (master->pub.is_dummy_pass ? 2 : 1);
+                                   (master->pub.is_dummy_pass ? 2 : 1);
     /* In buffered-image mode, we assume one more output pass if EOI not
      * yet reached, but no more passes if EOI has been reached.
      */
@@ -520,8 +528,8 @@ jinit_master_decompress (j_decompress_ptr cinfo)
 
   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;
+                                 SIZEOF(my_decomp_master));
+  cinfo->master = &master->pub;
   master->pub.prepare_for_output_pass = prepare_for_output_pass;
   master->pub.finish_output_pass = finish_output_pass;
 
index 170bed1..192da58 100644 (file)
@@ -2,6 +2,7 @@
  * jdmerge.c
  *
  * Copyright (C) 1994-1996, Thomas G. Lane.
+ * Modified 2013-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -23,7 +24,7 @@
  * multiplications needed for color conversion.
  *
  * This file currently provides implementations for the following cases:
- *     YCbCr => RGB color conversion only.
+ *     YCC => RGB color conversion only (YCbCr or BG_YCC).
  *     Sampling ratios of 2h1v or 2h2v.
  *     No scaling needed at upsample time.
  *     Corner-aligned (non-CCIR601) sampling alignment.
@@ -46,8 +47,8 @@ typedef struct {
 
   /* 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));
+                          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 */
@@ -75,12 +76,13 @@ typedef my_upsampler * my_upsample_ptr;
 
 
 /*
- * Initialize tables for YCC->RGB colorspace conversion.
+ * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
  * This is taken directly from jdcolor.c; see that file for more info.
  */
 
 LOCAL(void)
 build_ycc_rgb_table (j_decompress_ptr cinfo)
+/* Normal case, sYCC */
 {
   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
   int i;
@@ -89,31 +91,71 @@ build_ycc_rgb_table (j_decompress_ptr cinfo)
 
   upsample->Cr_r_tab = (int *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (MAXJSAMPLE+1) * SIZEOF(int));
+                               (MAXJSAMPLE+1) * SIZEOF(int));
   upsample->Cb_b_tab = (int *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (MAXJSAMPLE+1) * SIZEOF(int));
+                               (MAXJSAMPLE+1) * SIZEOF(int));
   upsample->Cr_g_tab = (INT32 *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (MAXJSAMPLE+1) * SIZEOF(INT32));
+                               (MAXJSAMPLE+1) * SIZEOF(INT32));
   upsample->Cb_g_tab = (INT32 *)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                (MAXJSAMPLE+1) * SIZEOF(INT32));
+                               (MAXJSAMPLE+1) * SIZEOF(INT32));
 
   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
-    /* Cr=>R value is nearest int to 1.40200 * x */
+    /* Cr=>R value is nearest int to 1.402 * x */
     upsample->Cr_r_tab[i] = (int)
-                    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
-    /* Cb=>B value is nearest int to 1.77200 * x */
+                   RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
+    /* Cb=>B value is nearest int to 1.772 * x */
     upsample->Cb_b_tab[i] = (int)
-                    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
-    /* Cr=>G value is scaled-up -0.71414 * x */
-    upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
-    /* Cb=>G value is scaled-up -0.34414 * x */
+                   RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
+    /* Cr=>G value is scaled-up -0.714136286 * x */
+    upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
+    /* Cb=>G value is scaled-up -0.344136286 * x */
     /* We also add in ONE_HALF so that need not do it in inner loop */
-    upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
+    upsample->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
+  }
+}
+
+
+LOCAL(void)
+build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
+/* Wide gamut case, bg-sYCC */
+{
+  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+  int i;
+  INT32 x;
+  SHIFT_TEMPS
+
+  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, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
+    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
+    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
+    /* Cr=>R value is nearest int to 2.804 * x */
+    upsample->Cr_r_tab[i] = (int)
+                   RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
+    /* Cb=>B value is nearest int to 3.544 * x */
+    upsample->Cb_b_tab[i] = (int)
+                   RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
+    /* Cr=>G value is scaled-up -1.428272572 * x */
+    upsample->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
+    /* Cb=>G value is scaled-up -0.688272572 * x */
+    /* We also add in ONE_HALF so that need not do it in inner loop */
+    upsample->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
   }
 }
 
@@ -142,10 +184,10 @@ start_pass_merged_upsample (j_decompress_ptr cinfo)
 
 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)
+                   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;
@@ -155,7 +197,7 @@ merged_2v_upsample (j_decompress_ptr cinfo,
   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);
+                     1, upsample->out_row_width);
     num_rows = 1;
     upsample->spare_full = FALSE;
   } else {
@@ -191,17 +233,17 @@ merged_2v_upsample (j_decompress_ptr cinfo,
 
 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)
+                   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);
+                        output_buf + *out_row_ctr);
   /* Adjust counts */
   (*out_row_ctr)++;
   (*in_row_group_ctr)++;
@@ -224,8 +266,8 @@ merged_1v_upsample (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 h2v1_merged_upsample (j_decompress_ptr cinfo,
-                      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
-                      JSAMPARRAY output_buf)
+                     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;
@@ -250,32 +292,32 @@ h2v1_merged_upsample (j_decompress_ptr cinfo,
     /* Do the chroma part of the calculation */
     cb = GETJSAMPLE(*inptr1++);
     cr = GETJSAMPLE(*inptr2++);
-    cred = Crrtab[cr];
+    cred   = Crrtab[cr];
     cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
-    cblue = Cbbtab[cb];
+    cblue  = Cbbtab[cb];
     /* Fetch 2 Y values and emit 2 pixels */
     y  = GETJSAMPLE(*inptr0++);
-    outptr[RGB_RED] =   range_limit[y + cred];
+    outptr[RGB_RED]   = range_limit[y + cred];
     outptr[RGB_GREEN] = range_limit[y + cgreen];
-    outptr[RGB_BLUE]  range_limit[y + cblue];
+    outptr[RGB_BLUE]  = range_limit[y + cblue];
     outptr += RGB_PIXELSIZE;
     y  = GETJSAMPLE(*inptr0++);
-    outptr[RGB_RED] =   range_limit[y + cred];
+    outptr[RGB_RED]   = range_limit[y + cred];
     outptr[RGB_GREEN] = range_limit[y + cgreen];
-    outptr[RGB_BLUE]  range_limit[y + cblue];
+    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];
+    cred   = Crrtab[cr];
     cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
-    cblue = Cbbtab[cb];
+    cblue  = Cbbtab[cb];
     y  = GETJSAMPLE(*inptr0);
-    outptr[RGB_RED] =   range_limit[y + cred];
+    outptr[RGB_RED]   = range_limit[y + cred];
     outptr[RGB_GREEN] = range_limit[y + cgreen];
-    outptr[RGB_BLUE]  range_limit[y + cblue];
+    outptr[RGB_BLUE]  = range_limit[y + cblue];
   }
 }
 
@@ -286,8 +328,8 @@ h2v1_merged_upsample (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 h2v2_merged_upsample (j_decompress_ptr cinfo,
-                      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
-                      JSAMPARRAY output_buf)
+                     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;
@@ -314,46 +356,46 @@ h2v2_merged_upsample (j_decompress_ptr cinfo,
     /* Do the chroma part of the calculation */
     cb = GETJSAMPLE(*inptr1++);
     cr = GETJSAMPLE(*inptr2++);
-    cred = Crrtab[cr];
+    cred   = Crrtab[cr];
     cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
-    cblue = Cbbtab[cb];
+    cblue  = Cbbtab[cb];
     /* Fetch 4 Y values and emit 4 pixels */
     y  = GETJSAMPLE(*inptr00++);
-    outptr0[RGB_RED] =   range_limit[y + cred];
+    outptr0[RGB_RED]   = range_limit[y + cred];
     outptr0[RGB_GREEN] = range_limit[y + cgreen];
-    outptr0[RGB_BLUE]  range_limit[y + cblue];
+    outptr0[RGB_BLUE]  = range_limit[y + cblue];
     outptr0 += RGB_PIXELSIZE;
     y  = GETJSAMPLE(*inptr00++);
-    outptr0[RGB_RED] =   range_limit[y + cred];
+    outptr0[RGB_RED]   = range_limit[y + cred];
     outptr0[RGB_GREEN] = range_limit[y + cgreen];
-    outptr0[RGB_BLUE]  range_limit[y + cblue];
+    outptr0[RGB_BLUE]  = range_limit[y + cblue];
     outptr0 += RGB_PIXELSIZE;
     y  = GETJSAMPLE(*inptr01++);
-    outptr1[RGB_RED] =   range_limit[y + cred];
+    outptr1[RGB_RED]   = range_limit[y + cred];
     outptr1[RGB_GREEN] = range_limit[y + cgreen];
-    outptr1[RGB_BLUE]  range_limit[y + cblue];
+    outptr1[RGB_BLUE]  = range_limit[y + cblue];
     outptr1 += RGB_PIXELSIZE;
     y  = GETJSAMPLE(*inptr01++);
-    outptr1[RGB_RED] =   range_limit[y + cred];
+    outptr1[RGB_RED]   = range_limit[y + cred];
     outptr1[RGB_GREEN] = range_limit[y + cgreen];
-    outptr1[RGB_BLUE]  range_limit[y + cblue];
+    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];
+    cred   = Crrtab[cr];
     cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
-    cblue = Cbbtab[cb];
+    cblue  = Cbbtab[cb];
     y  = GETJSAMPLE(*inptr00);
-    outptr0[RGB_RED] =   range_limit[y + cred];
+    outptr0[RGB_RED]   = range_limit[y + cred];
     outptr0[RGB_GREEN] = range_limit[y + cgreen];
-    outptr0[RGB_BLUE]  range_limit[y + cblue];
+    outptr0[RGB_BLUE]  = range_limit[y + cblue];
     y  = GETJSAMPLE(*inptr01);
-    outptr1[RGB_RED] =   range_limit[y + cred];
+    outptr1[RGB_RED]   = range_limit[y + cred];
     outptr1[RGB_GREEN] = range_limit[y + cgreen];
-    outptr1[RGB_BLUE]  range_limit[y + cblue];
+    outptr1[RGB_BLUE]  = range_limit[y + cblue];
   }
 }
 
@@ -373,8 +415,8 @@ jinit_merged_upsampler (j_decompress_ptr cinfo)
 
   upsample = (my_upsample_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_upsampler));
-  cinfo->upsample = (struct jpeg_upsampler *) upsample;
+                               SIZEOF(my_upsampler));
+  cinfo->upsample = &upsample->pub;
   upsample->pub.start_pass = start_pass_merged_upsample;
   upsample->pub.need_context_rows = FALSE;
 
@@ -386,7 +428,7 @@ jinit_merged_upsampler (j_decompress_ptr cinfo)
     /* 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)));
+               (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
   } else {
     upsample->pub.upsample = merged_1v_upsample;
     upsample->upmethod = h2v1_merged_upsample;
@@ -394,7 +436,10 @@ jinit_merged_upsampler (j_decompress_ptr cinfo)
     upsample->spare_row = NULL;
   }
 
-  build_ycc_rgb_table(cinfo);
+  if (cinfo->jpeg_color_space == JCS_BG_YCC)
+    build_bg_ycc_rgb_table(cinfo);
+  else
+    build_ycc_rgb_table(cinfo);
 }
 
 #endif /* UPSAMPLE_MERGING_SUPPORTED */
index 48215ef..571563d 100644 (file)
@@ -44,24 +44,24 @@ 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));
+       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));
+       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));
+       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
 
 
@@ -84,9 +84,9 @@ start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
        * allocate a strip buffer.  Use the virtual-array buffer as workspace.
        */
       if (post->buffer == NULL) {
-        post->buffer = (*cinfo->mem->access_virt_sarray)
-          ((j_common_ptr) cinfo, post->whole_image,
-           (JDIMENSION) 0, post->strip_height, TRUE);
+       post->buffer = (*cinfo->mem->access_virt_sarray)
+         ((j_common_ptr) cinfo, post->whole_image,
+          (JDIMENSION) 0, post->strip_height, TRUE);
       }
     } else {
       /* For single-pass processing without color quantization,
@@ -124,10 +124,10 @@ start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
 
 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)
+                   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;
@@ -139,11 +139,11 @@ post_process_1pass (j_decompress_ptr cinfo,
     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);
+               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);
+               post->buffer, output_buf + *out_row_ctr, (int) num_rows);
   *out_row_ctr += num_rows;
 }
 
@@ -156,10 +156,10 @@ post_process_1pass (j_decompress_ptr cinfo,
 
 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)
+                     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;
@@ -167,22 +167,22 @@ post_process_prepass (j_decompress_ptr cinfo,
   /* 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, post->strip_height, TRUE);
+       ((j_common_ptr) cinfo, post->whole_image,
+        post->starting_row, post->strip_height, 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);
+               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);
+                                        (JSAMPARRAY) NULL, (int) num_rows);
     *out_row_ctr += num_rows;
   }
 
@@ -200,10 +200,10 @@ post_process_prepass (j_decompress_ptr cinfo,
 
 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)
+                   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;
@@ -211,8 +211,8 @@ post_process_2pass (j_decompress_ptr cinfo,
   /* 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, post->strip_height, FALSE);
+       ((j_common_ptr) cinfo, post->whole_image,
+        post->starting_row, post->strip_height, FALSE);
   }
 
   /* Determine number of rows to emit. */
@@ -227,8 +227,8 @@ post_process_2pass (j_decompress_ptr cinfo,
 
   /* Quantize and emit data. */
   (*cinfo->cquantize->color_quantize) (cinfo,
-                post->buffer + post->next_row, output_buf + *out_row_ctr,
-                (int) num_rows);
+               post->buffer + post->next_row, output_buf + *out_row_ctr,
+               (int) num_rows);
   *out_row_ctr += num_rows;
 
   /* Advance if we filled the strip. */
@@ -253,7 +253,7 @@ jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
 
   post = (my_post_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_post_controller));
+                               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 */
@@ -271,20 +271,20 @@ jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
       /* We round up the number of rows to a multiple of the strip height. */
 #ifdef QUANT_2PASS_SUPPORTED
       post->whole_image = (*cinfo->mem->request_virt_sarray)
-        ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
-         cinfo->output_width * cinfo->out_color_components,
-         (JDIMENSION) jround_up((long) cinfo->output_height,
-                                (long) post->strip_height),
-         post->strip_height);
+       ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+        cinfo->output_width * cinfo->out_color_components,
+        (JDIMENSION) jround_up((long) cinfo->output_height,
+                               (long) post->strip_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);
+       ((j_common_ptr) cinfo, JPOOL_IMAGE,
+        cinfo->output_width * cinfo->out_color_components,
+        post->strip_height);
     }
   }
 }
index 41dc50e..fd9907e 100644 (file)
@@ -2,7 +2,7 @@
  * jdsample.c
  *
  * Copyright (C) 1991-1996, Thomas G. Lane.
- * Modified 2002-2008 by Guido Vollbeding.
+ * Modified 2002-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -26,8 +26,8 @@
 
 /* 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));
+               (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+                JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
 
 /* Private subobject */
 
@@ -88,10 +88,10 @@ start_pass_upsample (j_decompress_ptr cinfo)
 
 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)
+             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;
@@ -101,13 +101,13 @@ sep_upsample (j_decompress_ptr cinfo,
   /* 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++) {
+        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);
+       input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
+       upsample->color_buf + ci);
     }
     upsample->next_row_out = 0;
   }
@@ -119,7 +119,7 @@ sep_upsample (j_decompress_ptr cinfo,
   /* 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)
+  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;
@@ -127,9 +127,9 @@ sep_upsample (j_decompress_ptr cinfo,
     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);
+                                    (JDIMENSION) upsample->next_row_out,
+                                    output_buf + *out_row_ctr,
+                                    (int) num_rows);
 
   /* Adjust counts */
   *out_row_ctr += num_rows;
@@ -156,7 +156,7 @@ sep_upsample (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                   JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+                  JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
 {
   *output_data_ptr = input_data;
 }
@@ -169,7 +169,7 @@ fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 METHODDEF(void)
 noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+              JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
 {
   *output_data_ptr = NULL;     /* safety check */
 }
@@ -188,7 +188,7 @@ noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 METHODDEF(void)
 int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-              JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+             JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
 {
   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
   JSAMPARRAY output_data = *output_data_ptr;
@@ -211,13 +211,13 @@ int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     while (outptr < outend) {
       invalue = *inptr++;      /* don't need GETJSAMPLE() here */
       for (h = h_expand; h > 0; h--) {
-        *outptr++ = invalue;
+       *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);
+                       v_expand-1, cinfo->output_width);
     }
     inrow++;
     outrow += v_expand;
@@ -232,7 +232,7 @@ int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 METHODDEF(void)
 h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+              JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
 {
   JSAMPARRAY output_data = *output_data_ptr;
   register JSAMPROW inptr, outptr;
@@ -260,7 +260,7 @@ h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 METHODDEF(void)
 h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+              JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
 {
   JSAMPARRAY output_data = *output_data_ptr;
   register JSAMPROW inptr, outptr;
@@ -279,7 +279,7 @@ h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
       *outptr++ = invalue;
     }
     jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
-                      1, cinfo->output_width);
+                     1, cinfo->output_width);
     inrow++;
     outrow += 2;
   }
@@ -296,13 +296,12 @@ jinit_upsampler (j_decompress_ptr cinfo)
   my_upsample_ptr upsample;
   int ci;
   jpeg_component_info * compptr;
-  boolean need_buffer;
   int h_in_group, v_in_group, h_out_group, v_out_group;
 
   upsample = (my_upsample_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_upsampler));
-  cinfo->upsample = (struct jpeg_upsampler *) upsample;
+                               SIZEOF(my_upsampler));
+  cinfo->upsample = &upsample->pub;
   upsample->pub.start_pass = start_pass_upsample;
   upsample->pub.upsample = sep_upsample;
   upsample->pub.need_context_rows = FALSE; /* until we find out differently */
@@ -319,43 +318,41 @@ jinit_upsampler (j_decompress_ptr cinfo)
      * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
      */
     h_in_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) /
-                 cinfo->min_DCT_h_scaled_size;
+                cinfo->min_DCT_h_scaled_size;
     v_in_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
-                 cinfo->min_DCT_v_scaled_size;
+                cinfo->min_DCT_v_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) {
+      continue;                /* don't need to allocate buffer */
+    }
+    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) {
+      continue;                /* don't need to allocate buffer */
+    }
+    if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
       /* Special case for 2h1v upsampling */
       upsample->methods[ci] = h2v1_upsample;
     } else if (h_in_group * 2 == h_out_group &&
-               v_in_group * 2 == v_out_group) {
+              v_in_group * 2 == v_out_group) {
       /* Special case for 2h2v upsampling */
       upsample->methods[ci] = h2v2_upsample;
     } else if ((h_out_group % h_in_group) == 0 &&
-               (v_out_group % v_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);
-    }
+    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);
   }
 }
index 8639739..22dd47f 100644 (file)
@@ -56,20 +56,20 @@ jpeg_read_coefficients (j_decompress_ptr cinfo)
       int retcode;
       /* Call progress monitor hook if present */
       if (cinfo->progress != NULL)
-        (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+       (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
       /* Absorb some more input */
       retcode = (*cinfo->inputctl->consume_input) (cinfo);
       if (retcode == JPEG_SUSPENDED)
-        return NULL;
+       return NULL;
       if (retcode == JPEG_REACHED_EOI)
-        break;
+       break;
       /* Advance progress counter if appropriate */
       if (cinfo->progress != NULL &&
-          (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
-        if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
-          /* startup underestimated number of scans; ratchet up one scan */
-          cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
-        }
+         (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
+       if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
+         /* startup underestimated number of scans; ratchet up one scan */
+         cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
+       }
       }
     }
     /* Set state so that jpeg_finish_decompress does the right thing */
index d1329ed..7163af6 100644 (file)
@@ -2,7 +2,7 @@
  * jerror.c
  *
  * Copyright (C) 1991-1998, Thomas G. Lane.
- * Modified 2012 by Guido Vollbeding.
+ * Modified 2012-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * These routines are used by both the compression and decompression code.
  */
 
+#ifdef USE_WINDOWS_MESSAGEBOX
+#include <windows.h>
+#endif
+
 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
 #include "jinclude.h"
 #include "jpeglib.h"
 #include "jversion.h"
 #include "jerror.h"
 
-#ifdef USE_WINDOWS_MESSAGEBOX
-#include <windows.h>
-#endif
-
 #ifndef EXIT_FAILURE           /* define exit() codes if not provided */
 #define EXIT_FAILURE  1
 #endif
@@ -106,7 +106,7 @@ output_message (j_common_ptr cinfo)
 #ifdef USE_WINDOWS_MESSAGEBOX
   /* Display it in a message dialog box */
   MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
-             MB_OK | MB_ICONERROR);
+            MB_OK | MB_ICONERROR);
 #else
   /* Send it to stderr, adding a newline */
   fprintf(stderr, "%s\n", buffer);
@@ -168,8 +168,8 @@ format_message (j_common_ptr cinfo, char * buffer)
   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) {
+            msg_code >= err->first_addon_message &&
+            msg_code <= err->last_addon_message) {
     msgtext = err->addon_message_table[msg_code - err->first_addon_message];
   }
 
@@ -194,10 +194,10 @@ format_message (j_common_ptr cinfo, char * buffer)
     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]);
+           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]);
 }
 
 
index 5d1db58..a4b661f 100644 (file)
@@ -48,25 +48,25 @@ JMESSAGE(JERR_BAD_CROP_SPEC, "Invalid crop request")
 JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
 JMESSAGE(JERR_BAD_DCTSIZE, "DCT scaled block size %dx%d not supported")
 JMESSAGE(JERR_BAD_DROP_SAMPLING,
-         "Component index %d: mismatching sampling ratio %d:%d, %d:%d, %c")
+        "Component index %d: mismatching sampling ratio %d:%d, %d:%d, %c")
 JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
 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_LIB_VERSION,
-         "Wrong JPEG library version: library is %d, caller expects %d")
+        "Wrong JPEG library version: library is %d, caller expects %d")
 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_PROGRESSION,
-         "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
+        "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
 JMESSAGE(JERR_BAD_PROG_SCRIPT,
-         "Invalid progressive parameters at scan script entry %d")
+        "Invalid progressive parameters at scan script entry %d")
 JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
 JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d")
 JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
 JMESSAGE(JERR_BAD_STRUCT_SIZE,
-         "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
+        "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
 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")
@@ -90,7 +90,7 @@ 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_MISMATCHED_QUANT_TABLE,
-         "Cannot transcode due to multiple use of quantization table %d")
+        "Cannot transcode due to multiple use of quantization table %d")
 JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
 JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
 JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
@@ -103,7 +103,7 @@ 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")
+        "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_BEFORE, "Invalid JPEG file structure: %s before SOF")
@@ -115,7 +115,7 @@ 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?")
+        "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")
@@ -125,9 +125,9 @@ 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")
+        "Caution: quantization tables are too coarse for baseline JPEG")
 JMESSAGE(JTRC_ADOBE,
-         "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
+        "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")
@@ -140,9 +140,9 @@ JMESSAGE(JTRC_EOI, "End Of Image")
 JMESSAGE(JTRC_HUFFBITS, "        %3d %3d %3d %3d %3d %3d %3d %3d")
 JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d  %d")
 JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
-         "Warning: thumbnail image size does not match data length %u")
+        "Warning: thumbnail image size does not match data length %u")
 JMESSAGE(JTRC_JFIF_EXTENSION,
-         "JFIF extension marker: type 0x%02x, length %u")
+        "JFIF extension marker: type 0x%02x, length %u")
 JMESSAGE(JTRC_JFIF_THUMBNAIL, "    with %d x %d thumbnail image")
 JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u")
 JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
@@ -153,7 +153,7 @@ 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")
+        "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")
@@ -163,27 +163,27 @@ JMESSAGE(JTRC_SOS_PARAMS, "  Ss=%d, Se=%d, Ah=%d, Al=%d")
 JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
 JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
 JMESSAGE(JTRC_THUMB_JPEG,
-         "JFIF extension marker: JPEG-compressed thumbnail image, length %u")
+        "JFIF extension marker: JPEG-compressed thumbnail image, length %u")
 JMESSAGE(JTRC_THUMB_PALETTE,
-         "JFIF extension marker: palette thumbnail image, length %u")
+        "JFIF extension marker: palette thumbnail image, length %u")
 JMESSAGE(JTRC_THUMB_RGB,
-         "JFIF extension marker: RGB thumbnail image, length %u")
+        "JFIF extension marker: RGB thumbnail image, length %u")
 JMESSAGE(JTRC_UNKNOWN_IDS,
-         "Unrecognized component IDs %d %d %d, assuming YCbCr")
+        "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_ARITH_BAD_CODE, "Corrupt JPEG data: bad arithmetic code")
 JMESSAGE(JWRN_BOGUS_PROGRESSION,
-         "Inconsistent progression sequence for component %d coefficient %d")
+        "Inconsistent progression sequence for component %d coefficient %d")
 JMESSAGE(JWRN_EXTRANEOUS_DATA,
-         "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
+        "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_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
 JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
 JMESSAGE(JWRN_MUST_RESYNC,
-         "Corrupt JPEG data: found marker 0x%02x instead of RST%d")
+        "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")
 
@@ -276,26 +276,26 @@ JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")
    (*(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)); )
+          _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)); )
+          _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 TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5)  \
   MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
-           _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
-           _mp[4] = (p5); \
-           (cinfo)->err->msg_code = (code); \
-           (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+          _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
+          _mp[4] = (p5); \
+          (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)); )
+          _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), \
index 74d0d86..0ebc186 100644 (file)
@@ -2,7 +2,7 @@
  * jfdctflt.c
  *
  * Copyright (C) 1994-1996, Thomas G. Lane.
- * Modified 2003-2009 by Guido Vollbeding.
+ * Modified 2003-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -54,6 +54,8 @@
 
 /*
  * Perform the forward DCT on one block of samples.
+ *
+ * cK represents cos(K*pi/16).
  */
 
 GLOBAL(void)
@@ -89,7 +91,7 @@ jpeg_fdct_float (FAST_FLOAT * data, JSAMPARRAY sample_data, JDIMENSION start_col
     tmp11 = tmp1 + tmp2;
     tmp12 = tmp1 - tmp2;
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = tmp10 + tmp11 - 8 * CENTERJSAMPLE; /* phase 3 */
     dataptr[4] = tmp10 - tmp11;
 
index 8cad5f2..d779f78 100644 (file)
@@ -2,7 +2,7 @@
  * jfdctfst.c
  *
  * Copyright (C) 1994-1996, Thomas G. Lane.
- * Modified 2003-2009 by Guido Vollbeding.
+ * Modified 2003-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
 
 /*
  * Perform the forward DCT on one block of samples.
+ *
+ * cK represents cos(K*pi/16).
  */
 
 GLOBAL(void)
@@ -145,7 +147,7 @@ jpeg_fdct_ifast (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp11 = tmp1 + tmp2;
     tmp12 = tmp1 - tmp2;
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = tmp10 + tmp11 - 8 * CENTERJSAMPLE; /* phase 3 */
     dataptr[4] = tmp10 - tmp11;
 
index 5077c95..7ed42e5 100644 (file)
@@ -2,7 +2,7 @@
  * jfdctint.c
  *
  * Copyright (C) 1991-1996, Thomas G. Lane.
- * Modification developed 2003-2009 by Guido Vollbeding.
+ * Modification developed 2003-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -165,16 +165,18 @@ jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * cK represents sqrt(2) * cos(K*pi/16).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < DCTSIZE; ctr++) {
     elemptr = sample_data[ctr] + start_col;
 
     /* Even part per LL&M figure 1 --- note that published figure is faulty;
-     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+     * rotator "c1" should be "c6".
      */
 
     tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[7]);
@@ -192,51 +194,53 @@ jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[5]);
     tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM) ((tmp10 + tmp11 - 8 * CENTERJSAMPLE) << PASS1_BITS);
     dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);
 
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);       /* c6 */
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS-PASS1_BITS-1);
-    dataptr[2] = (DCTELEM) RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865),
-                                       CONST_BITS-PASS1_BITS);
-    dataptr[6] = (DCTELEM) RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065),
-                                       CONST_BITS-PASS1_BITS);
+
+    dataptr[2] = (DCTELEM)
+      RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */
+                 CONST_BITS-PASS1_BITS);
+    dataptr[6] = (DCTELEM)
+      RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */
+                 CONST_BITS-PASS1_BITS);
 
     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
-     * cK represents sqrt(2) * cos(K*pi/16).
      * i0..i3 in the paper are tmp0..tmp3 here.
      */
 
-    tmp10 = tmp0 + tmp3;
-    tmp11 = tmp1 + tmp2;
     tmp12 = tmp0 + tmp2;
     tmp13 = tmp1 + tmp3;
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /*  c3 */
+
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602);       /*  c3 */
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS-PASS1_BITS-1);
 
-    tmp0  = MULTIPLY(tmp0,    FIX_1_501321110);    /*  c1+c3-c5-c7 */
-    tmp1  = MULTIPLY(tmp1,    FIX_3_072711026);    /*  c1+c3+c5-c7 */
-    tmp2  = MULTIPLY(tmp2,    FIX_2_053119869);    /*  c1+c3-c5+c7 */
-    tmp3  = MULTIPLY(tmp3,    FIX_0_298631336);    /* -c1+c3+c5-c7 */
-    tmp10 = MULTIPLY(tmp10, - FIX_0_899976223);    /*  c7-c3 */
-    tmp11 = MULTIPLY(tmp11, - FIX_2_562915447);    /* -c1-c3 */
-    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);    /*  c5-c3 */
-    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);    /* -c3-c5 */
-
+    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);          /* -c3+c5 */
+    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);          /* -c3-c5 */
     tmp12 += z1;
     tmp13 += z1;
 
-    dataptr[1] = (DCTELEM)
-      RIGHT_SHIFT(tmp0 + tmp10 + tmp12, CONST_BITS-PASS1_BITS);
-    dataptr[3] = (DCTELEM)
-      RIGHT_SHIFT(tmp1 + tmp11 + tmp13, CONST_BITS-PASS1_BITS);
-    dataptr[5] = (DCTELEM)
-      RIGHT_SHIFT(tmp2 + tmp11 + tmp12, CONST_BITS-PASS1_BITS);
-    dataptr[7] = (DCTELEM)
-      RIGHT_SHIFT(tmp3 + tmp10 + tmp13, CONST_BITS-PASS1_BITS);
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223);       /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_1_501321110);              /*  c1+c3-c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_0_298631336);              /* -c1+c3+c5-c7 */
+    tmp0 += z1 + tmp12;
+    tmp3 += z1 + tmp13;
+
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447);       /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_3_072711026);              /*  c1+c3+c5-c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_2_053119869);              /*  c1+c3-c5+c7 */
+    tmp1 += z1 + tmp13;
+    tmp2 += z1 + tmp12;
+
+    dataptr[1] = (DCTELEM) RIGHT_SHIFT(tmp0, CONST_BITS-PASS1_BITS);
+    dataptr[3] = (DCTELEM) RIGHT_SHIFT(tmp1, CONST_BITS-PASS1_BITS);
+    dataptr[5] = (DCTELEM) RIGHT_SHIFT(tmp2, CONST_BITS-PASS1_BITS);
+    dataptr[7] = (DCTELEM) RIGHT_SHIFT(tmp3, CONST_BITS-PASS1_BITS);
 
     dataptr += DCTSIZE;                /* advance pointer to next row */
   }
@@ -244,12 +248,13 @@ jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pass 2: process columns.
    * We remove the PASS1_BITS scaling, but leave the results scaled up
    * by an overall factor of 8.
+   * cK represents sqrt(2) * cos(K*pi/16).
    */
 
   dataptr = data;
   for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
     /* Even part per LL&M figure 1 --- note that published figure is faulty;
-     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+     * rotator "c1" should be "c6".
      */
 
     tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
@@ -271,47 +276,49 @@ jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp10 + tmp11, PASS1_BITS);
     dataptr[DCTSIZE*4] = (DCTELEM) RIGHT_SHIFT(tmp10 - tmp11, PASS1_BITS);
 
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);       /* c6 */
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS+PASS1_BITS-1);
+
     dataptr[DCTSIZE*2] = (DCTELEM)
-      RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), CONST_BITS+PASS1_BITS);
+      RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */
+                 CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*6] = (DCTELEM)
-      RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), CONST_BITS+PASS1_BITS);
+      RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */
+                 CONST_BITS+PASS1_BITS);
 
     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
-     * cK represents sqrt(2) * cos(K*pi/16).
      * i0..i3 in the paper are tmp0..tmp3 here.
      */
 
-    tmp10 = tmp0 + tmp3;
-    tmp11 = tmp1 + tmp2;
     tmp12 = tmp0 + tmp2;
     tmp13 = tmp1 + tmp3;
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /*  c3 */
+
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602);       /*  c3 */
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS+PASS1_BITS-1);
 
-    tmp0  = MULTIPLY(tmp0,    FIX_1_501321110);    /*  c1+c3-c5-c7 */
-    tmp1  = MULTIPLY(tmp1,    FIX_3_072711026);    /*  c1+c3+c5-c7 */
-    tmp2  = MULTIPLY(tmp2,    FIX_2_053119869);    /*  c1+c3-c5+c7 */
-    tmp3  = MULTIPLY(tmp3,    FIX_0_298631336);    /* -c1+c3+c5-c7 */
-    tmp10 = MULTIPLY(tmp10, - FIX_0_899976223);    /*  c7-c3 */
-    tmp11 = MULTIPLY(tmp11, - FIX_2_562915447);    /* -c1-c3 */
-    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);    /*  c5-c3 */
-    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);    /* -c3-c5 */
-
+    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);          /* -c3+c5 */
+    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);          /* -c3-c5 */
     tmp12 += z1;
     tmp13 += z1;
 
-    dataptr[DCTSIZE*1] = (DCTELEM)
-      RIGHT_SHIFT(tmp0 + tmp10 + tmp12, CONST_BITS+PASS1_BITS);
-    dataptr[DCTSIZE*3] = (DCTELEM)
-      RIGHT_SHIFT(tmp1 + tmp11 + tmp13, CONST_BITS+PASS1_BITS);
-    dataptr[DCTSIZE*5] = (DCTELEM)
-      RIGHT_SHIFT(tmp2 + tmp11 + tmp12, CONST_BITS+PASS1_BITS);
-    dataptr[DCTSIZE*7] = (DCTELEM)
-      RIGHT_SHIFT(tmp3 + tmp10 + tmp13, CONST_BITS+PASS1_BITS);
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223);       /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_1_501321110);              /*  c1+c3-c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_0_298631336);              /* -c1+c3+c5-c7 */
+    tmp0 += z1 + tmp12;
+    tmp3 += z1 + tmp13;
+
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447);       /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_3_072711026);              /*  c1+c3+c5-c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_2_053119869);              /*  c1+c3-c5+c7 */
+    tmp1 += z1 + tmp13;
+    tmp2 += z1 + tmp12;
+
+    dataptr[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0, CONST_BITS+PASS1_BITS);
+    dataptr[DCTSIZE*3] = (DCTELEM) RIGHT_SHIFT(tmp1, CONST_BITS+PASS1_BITS);
+    dataptr[DCTSIZE*5] = (DCTELEM) RIGHT_SHIFT(tmp2, CONST_BITS+PASS1_BITS);
+    dataptr[DCTSIZE*7] = (DCTELEM) RIGHT_SHIFT(tmp3, CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -338,10 +345,11 @@ jpeg_fdct_7x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* cK represents sqrt(2) * cos(K*pi/14). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * cK represents sqrt(2) * cos(K*pi/14).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 7; ctr++) {
@@ -359,7 +367,7 @@ jpeg_fdct_7x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp12 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[4]);
 
     z1 = tmp0 + tmp2;
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((z1 + tmp1 + tmp3 - 7 * CENTERJSAMPLE) << PASS1_BITS);
     tmp3 += tmp3;
@@ -373,7 +381,7 @@ jpeg_fdct_7x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     z2 = MULTIPLY(tmp0 - tmp1, FIX(0.881747734));       /* c4 */
     dataptr[4] = (DCTELEM)
       DESCALE(z2 + z3 - MULTIPLY(tmp1 - tmp3, FIX(0.707106781)), /* c2+c6-c4 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
     dataptr[6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS-PASS1_BITS);
 
     /* Odd part */
@@ -419,7 +427,7 @@ jpeg_fdct_7x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     z1 = tmp0 + tmp2;
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(z1 + tmp1 + tmp3, FIX(1.306122449)), /* 64/49 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     tmp3 += tmp3;
     z1 -= tmp3;
     z1 -= tmp3;
@@ -431,7 +439,7 @@ jpeg_fdct_7x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     z2 = MULTIPLY(tmp0 - tmp1, FIX(1.151670509));       /* c4 */
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(z2 + z3 - MULTIPLY(tmp1 - tmp3, FIX(0.923568041)), /* c2+c6-c4 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+PASS1_BITS);
 
     /* Odd part */
@@ -472,10 +480,11 @@ jpeg_fdct_6x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* cK represents sqrt(2) * cos(K*pi/12). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * cK represents sqrt(2) * cos(K*pi/12).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 6; ctr++) {
@@ -494,20 +503,20 @@ jpeg_fdct_6x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[4]);
     tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[3]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 - 6 * CENTERJSAMPLE) << PASS1_BITS);
     dataptr[2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp12, FIX(1.224744871)),                 /* c2 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(0.707106781)), /* c4 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
 
     /* Odd part */
 
     tmp10 = DESCALE(MULTIPLY(tmp0 + tmp2, FIX(0.366025404)),     /* c5 */
-                    CONST_BITS-PASS1_BITS);
+                   CONST_BITS-PASS1_BITS);
 
     dataptr[1] = (DCTELEM) (tmp10 + ((tmp0 + tmp1) << PASS1_BITS));
     dataptr[3] = (DCTELEM) ((tmp0 - tmp1 - tmp2) << PASS1_BITS);
@@ -541,13 +550,13 @@ jpeg_fdct_6x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp11, FIX(1.777777778)),         /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp12, FIX(2.177324216)),                 /* c2 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(1.257078722)), /* c4 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     /* Odd part */
 
@@ -555,13 +564,13 @@ jpeg_fdct_6x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp0 + tmp1, FIX(1.777777778)),   /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*3] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 - tmp1 - tmp2, FIX(1.777777778)),    /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*5] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp2 - tmp1, FIX(1.777777778)),   /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -585,12 +594,13 @@ jpeg_fdct_5x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* We scale the results further by 2 as part of output adaption */
-  /* scaling for different DCT size. */
-  /* cK represents sqrt(2) * cos(K*pi/10). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * We scale the results further by 2 as part of output adaption
+   * scaling for different DCT size.
+   * cK represents sqrt(2) * cos(K*pi/10).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 5; ctr++) {
@@ -608,7 +618,7 @@ jpeg_fdct_5x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[4]);
     tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[3]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp2 - 5 * CENTERJSAMPLE) << (PASS1_BITS+1));
     tmp11 = MULTIPLY(tmp11, FIX(0.790569415));          /* (c2+c4)/2 */
@@ -623,10 +633,10 @@ jpeg_fdct_5x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[1] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp0, FIX(0.513743148)), /* c1-c3 */
-              CONST_BITS-PASS1_BITS-1);
+             CONST_BITS-PASS1_BITS-1);
     dataptr[3] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.176250899)), /* c1+c3 */
-              CONST_BITS-PASS1_BITS-1);
+             CONST_BITS-PASS1_BITS-1);
 
     dataptr += DCTSIZE;                /* advance pointer to next row */
   }
@@ -655,7 +665,7 @@ jpeg_fdct_5x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp2, FIX(1.28)),        /* 32/25 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     tmp11 = MULTIPLY(tmp11, FIX(1.011928851));          /* (c2+c4)/2 */
     tmp10 -= tmp2 << 2;
     tmp10 = MULTIPLY(tmp10, FIX(0.452548340));          /* (c2-c4)/2 */
@@ -668,10 +678,10 @@ jpeg_fdct_5x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp0, FIX(0.657591230)), /* c1-c3 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*3] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.785601151)), /* c1+c3 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -695,11 +705,12 @@ jpeg_fdct_4x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* We must also scale the output by (8/4)**2 = 2**2, which we add here. */
-  /* cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT]. */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * We must also scale the output by (8/4)**2 = 2**2, which we add here.
+   * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT].
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 4; ctr++) {
@@ -713,7 +724,7 @@ jpeg_fdct_4x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[3]);
     tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[2]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp0 + tmp1 - 4 * CENTERJSAMPLE) << (PASS1_BITS+2));
     dataptr[2] = (DCTELEM) ((tmp0 - tmp1) << (PASS1_BITS+2));
@@ -726,10 +737,10 @@ jpeg_fdct_4x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[1] = (DCTELEM)
       RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */
-                  CONST_BITS-PASS1_BITS-2);
+                 CONST_BITS-PASS1_BITS-2);
     dataptr[3] = (DCTELEM)
       RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */
-                  CONST_BITS-PASS1_BITS-2);
+                 CONST_BITS-PASS1_BITS-2);
 
     dataptr += DCTSIZE;                /* advance pointer to next row */
   }
@@ -737,6 +748,7 @@ jpeg_fdct_4x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pass 2: process columns.
    * We remove the PASS1_BITS scaling, but leave the results scaled up
    * by an overall factor of 8.
+   * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT].
    */
 
   dataptr = data;
@@ -761,10 +773,10 @@ jpeg_fdct_4x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */
-                  CONST_BITS+PASS1_BITS);
+                 CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*3] = (DCTELEM)
       RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */
-                  CONST_BITS+PASS1_BITS);
+                 CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -787,12 +799,13 @@ jpeg_fdct_3x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* We scale the results further by 2**2 as part of output adaption */
-  /* scaling for different DCT size. */
-  /* cK represents sqrt(2) * cos(K*pi/6). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * We scale the results further by 2**2 as part of output adaption
+   * scaling for different DCT size.
+   * cK represents sqrt(2) * cos(K*pi/6).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 3; ctr++) {
@@ -805,18 +818,18 @@ jpeg_fdct_3x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     tmp2 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[2]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp0 + tmp1 - 3 * CENTERJSAMPLE) << (PASS1_BITS+2));
     dataptr[2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(0.707106781)), /* c2 */
-              CONST_BITS-PASS1_BITS-2);
+             CONST_BITS-PASS1_BITS-2);
 
     /* Odd part */
 
     dataptr[1] = (DCTELEM)
       DESCALE(MULTIPLY(tmp2, FIX(1.224744871)),               /* c1 */
-              CONST_BITS-PASS1_BITS-2);
+             CONST_BITS-PASS1_BITS-2);
 
     dataptr += DCTSIZE;                /* advance pointer to next row */
   }
@@ -840,16 +853,16 @@ jpeg_fdct_3x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 + tmp1, FIX(1.777777778)),        /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(1.257078722)), /* c2 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     /* Odd part */
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(MULTIPLY(tmp2, FIX(2.177324216)),               /* c1 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -863,14 +876,15 @@ jpeg_fdct_3x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 GLOBAL(void)
 jpeg_fdct_2x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 {
-  INT32 tmp0, tmp1, tmp2, tmp3;
+  DCTELEM tmp0, tmp1, tmp2, tmp3;
   JSAMPROW elemptr;
 
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT. */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT.
+   */
 
   /* Row 0 */
   elemptr = sample_data[0] + start_col;
@@ -890,13 +904,13 @@ jpeg_fdct_2x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
    */
 
   /* Column 0 */
-  /* Apply unsigned->signed conversion */
-  data[DCTSIZE*0] = (DCTELEM) ((tmp0 + tmp2 - 4 * CENTERJSAMPLE) << 4);
-  data[DCTSIZE*1] = (DCTELEM) ((tmp0 - tmp2) << 4);
+  /* Apply unsigned->signed conversion. */
+  data[DCTSIZE*0] = (tmp0 + tmp2 - 4 * CENTERJSAMPLE) << 4;
+  data[DCTSIZE*1] = (tmp0 - tmp2) << 4;
 
   /* Column 1 */
-  data[DCTSIZE*0+1] = (DCTELEM) ((tmp1 + tmp3) << 4);
-  data[DCTSIZE*1+1] = (DCTELEM) ((tmp1 - tmp3) << 4);
+  data[DCTSIZE*0+1] = (tmp1 + tmp3) << 4;
+  data[DCTSIZE*1+1] = (tmp1 - tmp3) << 4;
 }
 
 
@@ -907,14 +921,17 @@ jpeg_fdct_2x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 GLOBAL(void)
 jpeg_fdct_1x1 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 {
+  DCTELEM dcval;
+
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
+  dcval = GETJSAMPLE(sample_data[0][start_col]);
+
   /* We leave the result scaled up by an overall factor of 8. */
   /* We must also scale the output by (8/1)**2 = 2**6. */
-  /* Apply unsigned->signed conversion */
-  data[0] = (DCTELEM)
-    ((GETJSAMPLE(sample_data[0][start_col]) - CENTERJSAMPLE) << 6);
+  /* Apply unsigned->signed conversion. */
+  data[0] = (dcval - CENTERJSAMPLE) << 6;
 }
 
 
@@ -935,11 +952,12 @@ jpeg_fdct_9x9 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* we scale the results further by 2 as part of output adaption */
-  /* scaling for different DCT size. */
-  /* cK represents sqrt(2) * cos(K*pi/18). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * we scale the results further by 2 as part of output adaption
+   * scaling for different DCT size.
+   * cK represents sqrt(2) * cos(K*pi/18).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -961,25 +979,25 @@ jpeg_fdct_9x9 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     z1 = tmp0 + tmp2 + tmp3;
     z2 = tmp1 + tmp4;
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM) ((z1 + z2 - 9 * CENTERJSAMPLE) << 1);
     dataptr[6] = (DCTELEM)
       DESCALE(MULTIPLY(z1 - z2 - z2, FIX(0.707106781)),  /* c6 */
-              CONST_BITS-1);
+             CONST_BITS-1);
     z1 = MULTIPLY(tmp0 - tmp2, FIX(1.328926049));        /* c2 */
     z2 = MULTIPLY(tmp1 - tmp4 - tmp4, FIX(0.707106781)); /* c6 */
     dataptr[2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp2 - tmp3, FIX(1.083350441))    /* c4 */
-              + z1 + z2, CONST_BITS-1);
+             + z1 + z2, CONST_BITS-1);
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp3 - tmp0, FIX(0.245575608))    /* c8 */
-              + z1 - z2, CONST_BITS-1);
+             + z1 - z2, CONST_BITS-1);
 
     /* Odd part */
 
     dataptr[3] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12 - tmp13, FIX(1.224744871)), /* c3 */
-              CONST_BITS-1);
+             CONST_BITS-1);
 
     tmp11 = MULTIPLY(tmp11, FIX(1.224744871));        /* c3 */
     tmp0 = MULTIPLY(tmp10 + tmp12, FIX(0.909038955)); /* c5 */
@@ -996,7 +1014,7 @@ jpeg_fdct_9x9 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     if (ctr != DCTSIZE) {
       if (ctr == 9)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -1029,24 +1047,24 @@ jpeg_fdct_9x9 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     z2 = tmp1 + tmp4;
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(z1 + z2, FIX(1.580246914)),       /* 128/81 */
-              CONST_BITS+2);
+             CONST_BITS+2);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(MULTIPLY(z1 - z2 - z2, FIX(1.117403309)),  /* c6 */
-              CONST_BITS+2);
+             CONST_BITS+2);
     z1 = MULTIPLY(tmp0 - tmp2, FIX(2.100031287));        /* c2 */
     z2 = MULTIPLY(tmp1 - tmp4 - tmp4, FIX(1.117403309)); /* c6 */
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp2 - tmp3, FIX(1.711961190))    /* c4 */
-              + z1 + z2, CONST_BITS+2);
+             + z1 + z2, CONST_BITS+2);
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp3 - tmp0, FIX(0.388070096))    /* c8 */
-              + z1 - z2, CONST_BITS+2);
+             + z1 - z2, CONST_BITS+2);
 
     /* Odd part */
 
     dataptr[DCTSIZE*3] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12 - tmp13, FIX(1.935399303)), /* c3 */
-              CONST_BITS+2);
+             CONST_BITS+2);
 
     tmp11 = MULTIPLY(tmp11, FIX(1.935399303));        /* c3 */
     tmp0 = MULTIPLY(tmp10 + tmp12, FIX(1.436506004)); /* c5 */
@@ -1084,11 +1102,12 @@ jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* we scale the results further by 2 as part of output adaption */
-  /* scaling for different DCT size. */
-  /* cK represents sqrt(2) * cos(K*pi/20). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * we scale the results further by 2 as part of output adaption
+   * scaling for different DCT size.
+   * cK represents sqrt(2) * cos(K*pi/20).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -1114,21 +1133,21 @@ jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[6]);
     tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[5]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 + tmp12 - 10 * CENTERJSAMPLE) << 1);
     tmp12 += tmp12;
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.144122806)) - /* c4 */
-              MULTIPLY(tmp11 - tmp12, FIX(0.437016024)),  /* c8 */
-              CONST_BITS-1);
+             MULTIPLY(tmp11 - tmp12, FIX(0.437016024)),  /* c8 */
+             CONST_BITS-1);
     tmp10 = MULTIPLY(tmp13 + tmp14, FIX(0.831253876));    /* c6 */
     dataptr[2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp13, FIX(0.513743148)),  /* c2-c6 */
-              CONST_BITS-1);
+             CONST_BITS-1);
     dataptr[6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.176250899)),  /* c2+c6 */
-              CONST_BITS-1);
+             CONST_BITS-1);
 
     /* Odd part */
 
@@ -1138,14 +1157,14 @@ jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp2 <<= CONST_BITS;
     dataptr[1] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0, FIX(1.396802247)) +          /* c1 */
-              MULTIPLY(tmp1, FIX(1.260073511)) + tmp2 +   /* c3 */
-              MULTIPLY(tmp3, FIX(0.642039522)) +          /* c7 */
-              MULTIPLY(tmp4, FIX(0.221231742)),           /* c9 */
-              CONST_BITS-1);
+             MULTIPLY(tmp1, FIX(1.260073511)) + tmp2 +   /* c3 */
+             MULTIPLY(tmp3, FIX(0.642039522)) +          /* c7 */
+             MULTIPLY(tmp4, FIX(0.221231742)),           /* c9 */
+             CONST_BITS-1);
     tmp12 = MULTIPLY(tmp0 - tmp4, FIX(0.951056516)) -     /* (c3+c7)/2 */
-            MULTIPLY(tmp1 + tmp3, FIX(0.587785252));      /* (c1-c9)/2 */
+           MULTIPLY(tmp1 + tmp3, FIX(0.587785252));      /* (c1-c9)/2 */
     tmp13 = MULTIPLY(tmp10 + tmp11, FIX(0.309016994)) +   /* (c3-c7)/2 */
-            (tmp11 << (CONST_BITS - 1)) - tmp2;
+           (tmp11 << (CONST_BITS - 1)) - tmp2;
     dataptr[3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS-1);
     dataptr[7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS-1);
 
@@ -1153,7 +1172,7 @@ jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     if (ctr != DCTSIZE) {
       if (ctr == 10)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -1190,19 +1209,19 @@ jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12, FIX(1.28)), /* 32/25 */
-              CONST_BITS+2);
+             CONST_BITS+2);
     tmp12 += tmp12;
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.464477191)) - /* c4 */
-              MULTIPLY(tmp11 - tmp12, FIX(0.559380511)),  /* c8 */
-              CONST_BITS+2);
+             MULTIPLY(tmp11 - tmp12, FIX(0.559380511)),  /* c8 */
+             CONST_BITS+2);
     tmp10 = MULTIPLY(tmp13 + tmp14, FIX(1.064004961));    /* c6 */
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp13, FIX(0.657591230)),  /* c2-c6 */
-              CONST_BITS+2);
+             CONST_BITS+2);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.785601151)),  /* c2+c6 */
-              CONST_BITS+2);
+             CONST_BITS+2);
 
     /* Odd part */
 
@@ -1210,18 +1229,18 @@ jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp11 = tmp1 - tmp3;
     dataptr[DCTSIZE*5] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp11 - tmp2, FIX(1.28)),  /* 32/25 */
-              CONST_BITS+2);
+             CONST_BITS+2);
     tmp2 = MULTIPLY(tmp2, FIX(1.28));                     /* 32/25 */
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0, FIX(1.787906876)) +          /* c1 */
-              MULTIPLY(tmp1, FIX(1.612894094)) + tmp2 +   /* c3 */
-              MULTIPLY(tmp3, FIX(0.821810588)) +          /* c7 */
-              MULTIPLY(tmp4, FIX(0.283176630)),           /* c9 */
-              CONST_BITS+2);
+             MULTIPLY(tmp1, FIX(1.612894094)) + tmp2 +   /* c3 */
+             MULTIPLY(tmp3, FIX(0.821810588)) +          /* c7 */
+             MULTIPLY(tmp4, FIX(0.283176630)),           /* c9 */
+             CONST_BITS+2);
     tmp12 = MULTIPLY(tmp0 - tmp4, FIX(1.217352341)) -     /* (c3+c7)/2 */
-            MULTIPLY(tmp1 + tmp3, FIX(0.752365123));      /* (c1-c9)/2 */
+           MULTIPLY(tmp1 + tmp3, FIX(0.752365123));      /* (c1-c9)/2 */
     tmp13 = MULTIPLY(tmp10 + tmp11, FIX(0.395541753)) +   /* (c3-c7)/2 */
-            MULTIPLY(tmp11, FIX(0.64)) - tmp2;            /* 16/25 */
+           MULTIPLY(tmp11, FIX(0.64)) - tmp2;            /* 16/25 */
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS+2);
     dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS+2);
 
@@ -1248,11 +1267,12 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* we scale the results further by 2 as part of output adaption */
-  /* scaling for different DCT size. */
-  /* cK represents sqrt(2) * cos(K*pi/22). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * we scale the results further by 2 as part of output adaption
+   * scaling for different DCT size.
+   * cK represents sqrt(2) * cos(K*pi/22).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -1274,7 +1294,7 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp13 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[7]);
     tmp14 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[6]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5 - 11 * CENTERJSAMPLE) << 1);
     tmp5 += tmp5;
@@ -1284,22 +1304,22 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp3 -= tmp5;
     tmp4 -= tmp5;
     z1 = MULTIPLY(tmp0 + tmp3, FIX(1.356927976)) +       /* c2 */
-         MULTIPLY(tmp2 + tmp4, FIX(0.201263574));        /* c10 */
+        MULTIPLY(tmp2 + tmp4, FIX(0.201263574));        /* c10 */
     z2 = MULTIPLY(tmp1 - tmp3, FIX(0.926112931));        /* c6 */
     z3 = MULTIPLY(tmp0 - tmp1, FIX(1.189712156));        /* c4 */
     dataptr[2] = (DCTELEM)
       DESCALE(z1 + z2 - MULTIPLY(tmp3, FIX(1.018300590)) /* c2+c8-c6 */
-              - MULTIPLY(tmp4, FIX(1.390975730)),        /* c4+c10 */
-              CONST_BITS-1);
+             - MULTIPLY(tmp4, FIX(1.390975730)),        /* c4+c10 */
+             CONST_BITS-1);
     dataptr[4] = (DCTELEM)
       DESCALE(z2 + z3 + MULTIPLY(tmp1, FIX(0.062335650)) /* c4-c6-c10 */
-              - MULTIPLY(tmp2, FIX(1.356927976))         /* c2 */
-              + MULTIPLY(tmp4, FIX(0.587485545)),        /* c8 */
-              CONST_BITS-1);
+             - MULTIPLY(tmp2, FIX(1.356927976))         /* c2 */
+             + MULTIPLY(tmp4, FIX(0.587485545)),        /* c8 */
+             CONST_BITS-1);
     dataptr[6] = (DCTELEM)
       DESCALE(z1 + z3 - MULTIPLY(tmp0, FIX(1.620527200)) /* c2+c4-c6 */
-              - MULTIPLY(tmp2, FIX(0.788749120)),        /* c8+c10 */
-              CONST_BITS-1);
+             - MULTIPLY(tmp2, FIX(0.788749120)),        /* c8+c10 */
+             CONST_BITS-1);
 
     /* Odd part */
 
@@ -1307,16 +1327,16 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp2 = MULTIPLY(tmp10 + tmp12, FIX(1.068791298));    /* c5 */
     tmp3 = MULTIPLY(tmp10 + tmp13, FIX(0.764581576));    /* c7 */
     tmp0 = tmp1 + tmp2 + tmp3 - MULTIPLY(tmp10, FIX(1.719967871)) /* c7+c5+c3-c1 */
-           + MULTIPLY(tmp14, FIX(0.398430003));          /* c9 */
+          + MULTIPLY(tmp14, FIX(0.398430003));          /* c9 */
     tmp4 = MULTIPLY(tmp11 + tmp12, - FIX(0.764581576));  /* -c7 */
     tmp5 = MULTIPLY(tmp11 + tmp13, - FIX(1.399818907));  /* -c1 */
     tmp1 += tmp4 + tmp5 + MULTIPLY(tmp11, FIX(1.276416582)) /* c9+c7+c1-c3 */
-            - MULTIPLY(tmp14, FIX(1.068791298));         /* c5 */
+           - MULTIPLY(tmp14, FIX(1.068791298));         /* c5 */
     tmp10 = MULTIPLY(tmp12 + tmp13, FIX(0.398430003));   /* c9 */
     tmp2 += tmp4 + tmp10 - MULTIPLY(tmp12, FIX(1.989053629)) /* c9+c5+c3-c7 */
-            + MULTIPLY(tmp14, FIX(1.399818907));         /* c1 */
+           + MULTIPLY(tmp14, FIX(1.399818907));         /* c1 */
     tmp3 += tmp5 + tmp10 + MULTIPLY(tmp13, FIX(1.305598626)) /* c1+c5-c9-c7 */
-            - MULTIPLY(tmp14, FIX(1.286413905));         /* c3 */
+           - MULTIPLY(tmp14, FIX(1.286413905));         /* c3 */
 
     dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS-1);
     dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS-1);
@@ -1327,7 +1347,7 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     if (ctr != DCTSIZE) {
       if (ctr == 11)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -1360,8 +1380,8 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5,
-                       FIX(1.057851240)),                /* 128/121 */
-              CONST_BITS+2);
+                      FIX(1.057851240)),                /* 128/121 */
+             CONST_BITS+2);
     tmp5 += tmp5;
     tmp0 -= tmp5;
     tmp1 -= tmp5;
@@ -1369,22 +1389,22 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp3 -= tmp5;
     tmp4 -= tmp5;
     z1 = MULTIPLY(tmp0 + tmp3, FIX(1.435427942)) +       /* c2 */
-         MULTIPLY(tmp2 + tmp4, FIX(0.212906922));        /* c10 */
+        MULTIPLY(tmp2 + tmp4, FIX(0.212906922));        /* c10 */
     z2 = MULTIPLY(tmp1 - tmp3, FIX(0.979689713));        /* c6 */
     z3 = MULTIPLY(tmp0 - tmp1, FIX(1.258538479));        /* c4 */
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(z1 + z2 - MULTIPLY(tmp3, FIX(1.077210542)) /* c2+c8-c6 */
-              - MULTIPLY(tmp4, FIX(1.471445400)),        /* c4+c10 */
-              CONST_BITS+2);
+             - MULTIPLY(tmp4, FIX(1.471445400)),        /* c4+c10 */
+             CONST_BITS+2);
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(z2 + z3 + MULTIPLY(tmp1, FIX(0.065941844)) /* c4-c6-c10 */
-              - MULTIPLY(tmp2, FIX(1.435427942))         /* c2 */
-              + MULTIPLY(tmp4, FIX(0.621472312)),        /* c8 */
-              CONST_BITS+2);
+             - MULTIPLY(tmp2, FIX(1.435427942))         /* c2 */
+             + MULTIPLY(tmp4, FIX(0.621472312)),        /* c8 */
+             CONST_BITS+2);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(z1 + z3 - MULTIPLY(tmp0, FIX(1.714276708)) /* c2+c4-c6 */
-              - MULTIPLY(tmp2, FIX(0.834379234)),        /* c8+c10 */
-              CONST_BITS+2);
+             - MULTIPLY(tmp2, FIX(0.834379234)),        /* c8+c10 */
+             CONST_BITS+2);
 
     /* Odd part */
 
@@ -1392,16 +1412,16 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp2 = MULTIPLY(tmp10 + tmp12, FIX(1.130622199));    /* c5 */
     tmp3 = MULTIPLY(tmp10 + tmp13, FIX(0.808813568));    /* c7 */
     tmp0 = tmp1 + tmp2 + tmp3 - MULTIPLY(tmp10, FIX(1.819470145)) /* c7+c5+c3-c1 */
-           + MULTIPLY(tmp14, FIX(0.421479672));          /* c9 */
+          + MULTIPLY(tmp14, FIX(0.421479672));          /* c9 */
     tmp4 = MULTIPLY(tmp11 + tmp12, - FIX(0.808813568));  /* -c7 */
     tmp5 = MULTIPLY(tmp11 + tmp13, - FIX(1.480800167));  /* -c1 */
     tmp1 += tmp4 + tmp5 + MULTIPLY(tmp11, FIX(1.350258864)) /* c9+c7+c1-c3 */
-            - MULTIPLY(tmp14, FIX(1.130622199));         /* c5 */
+           - MULTIPLY(tmp14, FIX(1.130622199));         /* c5 */
     tmp10 = MULTIPLY(tmp12 + tmp13, FIX(0.421479672));   /* c9 */
     tmp2 += tmp4 + tmp10 - MULTIPLY(tmp12, FIX(2.104122847)) /* c9+c5+c3-c7 */
-            + MULTIPLY(tmp14, FIX(1.480800167));         /* c1 */
+           + MULTIPLY(tmp14, FIX(1.480800167));         /* c1 */
     tmp3 += tmp5 + tmp10 + MULTIPLY(tmp13, FIX(1.381129125)) /* c1+c5-c9-c7 */
-            - MULTIPLY(tmp14, FIX(1.360834544));         /* c3 */
+           - MULTIPLY(tmp14, FIX(1.360834544));         /* c3 */
 
     dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+2);
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+2);
@@ -1430,9 +1450,10 @@ jpeg_fdct_12x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT. */
-  /* cK represents sqrt(2) * cos(K*pi/24). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT.
+   * cK represents sqrt(2) * cos(K*pi/24).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -1462,15 +1483,15 @@ jpeg_fdct_12x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[7]);
     tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[6]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM) (tmp10 + tmp11 + tmp12 - 12 * CENTERJSAMPLE);
     dataptr[6] = (DCTELEM) (tmp13 - tmp14 - tmp15);
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.224744871)), /* c4 */
-              CONST_BITS);
+             CONST_BITS);
     dataptr[2] = (DCTELEM)
       DESCALE(tmp14 - tmp15 + MULTIPLY(tmp13 + tmp15, FIX(1.366025404)), /* c2 */
-              CONST_BITS);
+             CONST_BITS);
 
     /* Odd part */
 
@@ -1480,14 +1501,14 @@ jpeg_fdct_12x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.121971054));   /* c5 */
     tmp13 = MULTIPLY(tmp0 + tmp3, FIX(0.860918669));   /* c7 */
     tmp10 = tmp12 + tmp13 + tmp14 - MULTIPLY(tmp0, FIX(0.580774953)) /* c5+c7-c1 */
-            + MULTIPLY(tmp5, FIX(0.184591911));        /* c11 */
+           + MULTIPLY(tmp5, FIX(0.184591911));        /* c11 */
     tmp11 = MULTIPLY(tmp2 + tmp3, - FIX(0.184591911)); /* -c11 */
     tmp12 += tmp11 - tmp15 - MULTIPLY(tmp2, FIX(2.339493912)) /* c1+c5-c11 */
-            + MULTIPLY(tmp5, FIX(0.860918669));        /* c7 */
+           + MULTIPLY(tmp5, FIX(0.860918669));        /* c7 */
     tmp13 += tmp11 - tmp14 + MULTIPLY(tmp3, FIX(0.725788011)) /* c1+c11-c7 */
-            - MULTIPLY(tmp5, FIX(1.121971054));        /* c5 */
+           - MULTIPLY(tmp5, FIX(1.121971054));        /* c5 */
     tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.306562965)) /* c3 */
-            - MULTIPLY(tmp2 + tmp5, FIX_0_541196100);  /* c9 */
+           - MULTIPLY(tmp2 + tmp5, FIX_0_541196100);  /* c9 */
 
     dataptr[1] = (DCTELEM) DESCALE(tmp10, CONST_BITS);
     dataptr[3] = (DCTELEM) DESCALE(tmp11, CONST_BITS);
@@ -1498,7 +1519,7 @@ jpeg_fdct_12x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     if (ctr != DCTSIZE) {
       if (ctr == 12)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -1539,17 +1560,17 @@ jpeg_fdct_12x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12, FIX(0.888888889)), /* 8/9 */
-              CONST_BITS+1);
+             CONST_BITS+1);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(MULTIPLY(tmp13 - tmp14 - tmp15, FIX(0.888888889)), /* 8/9 */
-              CONST_BITS+1);
+             CONST_BITS+1);
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.088662108)),         /* c4 */
-              CONST_BITS+1);
+             CONST_BITS+1);
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp14 - tmp15, FIX(0.888888889)) +        /* 8/9 */
-              MULTIPLY(tmp13 + tmp15, FIX(1.214244803)),         /* c2 */
-              CONST_BITS+1);
+             MULTIPLY(tmp13 + tmp15, FIX(1.214244803)),         /* c2 */
+             CONST_BITS+1);
 
     /* Odd part */
 
@@ -1559,14 +1580,14 @@ jpeg_fdct_12x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp12 = MULTIPLY(tmp0 + tmp2, FIX(0.997307603));   /* c5 */
     tmp13 = MULTIPLY(tmp0 + tmp3, FIX(0.765261039));   /* c7 */
     tmp10 = tmp12 + tmp13 + tmp14 - MULTIPLY(tmp0, FIX(0.516244403)) /* c5+c7-c1 */
-            + MULTIPLY(tmp5, FIX(0.164081699));        /* c11 */
+           + MULTIPLY(tmp5, FIX(0.164081699));        /* c11 */
     tmp11 = MULTIPLY(tmp2 + tmp3, - FIX(0.164081699)); /* -c11 */
     tmp12 += tmp11 - tmp15 - MULTIPLY(tmp2, FIX(2.079550144)) /* c1+c5-c11 */
-            + MULTIPLY(tmp5, FIX(0.765261039));        /* c7 */
+           + MULTIPLY(tmp5, FIX(0.765261039));        /* c7 */
     tmp13 += tmp11 - tmp14 + MULTIPLY(tmp3, FIX(0.645144899)) /* c1+c11-c7 */
-            - MULTIPLY(tmp5, FIX(0.997307603));        /* c5 */
+           - MULTIPLY(tmp5, FIX(0.997307603));        /* c5 */
     tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.161389302)) /* c3 */
-            - MULTIPLY(tmp2 + tmp5, FIX(0.481063200)); /* c9 */
+           - MULTIPLY(tmp2 + tmp5, FIX(0.481063200)); /* c9 */
 
     dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+1);
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+1);
@@ -1596,9 +1617,10 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT. */
-  /* cK represents sqrt(2) * cos(K*pi/26). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT.
+   * cK represents sqrt(2) * cos(K*pi/26).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -1622,7 +1644,7 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp14 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[8]);
     tmp15 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[7]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       (tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5 + tmp6 - 13 * CENTERJSAMPLE);
     tmp6 += tmp6;
@@ -1634,18 +1656,18 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp5 -= tmp6;
     dataptr[2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0, FIX(1.373119086)) +   /* c2 */
-              MULTIPLY(tmp1, FIX(1.058554052)) +   /* c6 */
-              MULTIPLY(tmp2, FIX(0.501487041)) -   /* c10 */
-              MULTIPLY(tmp3, FIX(0.170464608)) -   /* c12 */
-              MULTIPLY(tmp4, FIX(0.803364869)) -   /* c8 */
-              MULTIPLY(tmp5, FIX(1.252223920)),    /* c4 */
-              CONST_BITS);
+             MULTIPLY(tmp1, FIX(1.058554052)) +   /* c6 */
+             MULTIPLY(tmp2, FIX(0.501487041)) -   /* c10 */
+             MULTIPLY(tmp3, FIX(0.170464608)) -   /* c12 */
+             MULTIPLY(tmp4, FIX(0.803364869)) -   /* c8 */
+             MULTIPLY(tmp5, FIX(1.252223920)),    /* c4 */
+             CONST_BITS);
     z1 = MULTIPLY(tmp0 - tmp2, FIX(1.155388986)) - /* (c4+c6)/2 */
-         MULTIPLY(tmp3 - tmp4, FIX(0.435816023)) - /* (c2-c10)/2 */
-         MULTIPLY(tmp1 - tmp5, FIX(0.316450131));  /* (c8-c12)/2 */
+        MULTIPLY(tmp3 - tmp4, FIX(0.435816023)) - /* (c2-c10)/2 */
+        MULTIPLY(tmp1 - tmp5, FIX(0.316450131));  /* (c8-c12)/2 */
     z2 = MULTIPLY(tmp0 + tmp2, FIX(0.096834934)) - /* (c4-c6)/2 */
-         MULTIPLY(tmp3 + tmp4, FIX(0.937303064)) + /* (c2+c10)/2 */
-         MULTIPLY(tmp1 + tmp5, FIX(0.486914739));  /* (c8+c12)/2 */
+        MULTIPLY(tmp3 + tmp4, FIX(0.937303064)) + /* (c2+c10)/2 */
+        MULTIPLY(tmp1 + tmp5, FIX(0.486914739));  /* (c8+c12)/2 */
 
     dataptr[4] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS);
     dataptr[6] = (DCTELEM) DESCALE(z1 - z2, CONST_BITS);
@@ -1655,23 +1677,23 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp1 = MULTIPLY(tmp10 + tmp11, FIX(1.322312651));   /* c3 */
     tmp2 = MULTIPLY(tmp10 + tmp12, FIX(1.163874945));   /* c5 */
     tmp3 = MULTIPLY(tmp10 + tmp13, FIX(0.937797057)) +  /* c7 */
-           MULTIPLY(tmp14 + tmp15, FIX(0.338443458));   /* c11 */
+          MULTIPLY(tmp14 + tmp15, FIX(0.338443458));   /* c11 */
     tmp0 = tmp1 + tmp2 + tmp3 -
-           MULTIPLY(tmp10, FIX(2.020082300)) +          /* c3+c5+c7-c1 */
-           MULTIPLY(tmp14, FIX(0.318774355));           /* c9-c11 */
+          MULTIPLY(tmp10, FIX(2.020082300)) +          /* c3+c5+c7-c1 */
+          MULTIPLY(tmp14, FIX(0.318774355));           /* c9-c11 */
     tmp4 = MULTIPLY(tmp14 - tmp15, FIX(0.937797057)) -  /* c7 */
-           MULTIPLY(tmp11 + tmp12, FIX(0.338443458));   /* c11 */
+          MULTIPLY(tmp11 + tmp12, FIX(0.338443458));   /* c11 */
     tmp5 = MULTIPLY(tmp11 + tmp13, - FIX(1.163874945)); /* -c5 */
     tmp1 += tmp4 + tmp5 +
-            MULTIPLY(tmp11, FIX(0.837223564)) -         /* c5+c9+c11-c3 */
-            MULTIPLY(tmp14, FIX(2.341699410));          /* c1+c7 */
+           MULTIPLY(tmp11, FIX(0.837223564)) -         /* c5+c9+c11-c3 */
+           MULTIPLY(tmp14, FIX(2.341699410));          /* c1+c7 */
     tmp6 = MULTIPLY(tmp12 + tmp13, - FIX(0.657217813)); /* -c9 */
     tmp2 += tmp4 + tmp6 -
-            MULTIPLY(tmp12, FIX(1.572116027)) +         /* c1+c5-c9-c11 */
-            MULTIPLY(tmp15, FIX(2.260109708));          /* c3+c7 */
+           MULTIPLY(tmp12, FIX(1.572116027)) +         /* c1+c5-c9-c11 */
+           MULTIPLY(tmp15, FIX(2.260109708));          /* c3+c7 */
     tmp3 += tmp5 + tmp6 +
-            MULTIPLY(tmp13, FIX(2.205608352)) -         /* c3+c5+c9-c7 */
-            MULTIPLY(tmp15, FIX(1.742345811));          /* c1+c11 */
+           MULTIPLY(tmp13, FIX(2.205608352)) -         /* c3+c5+c9-c7 */
+           MULTIPLY(tmp15, FIX(1.742345811));          /* c1+c11 */
 
     dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS);
     dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS);
@@ -1682,7 +1704,7 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     if (ctr != DCTSIZE) {
       if (ctr == 13)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -1717,8 +1739,8 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5 + tmp6,
-                       FIX(0.757396450)),          /* 128/169 */
-              CONST_BITS+1);
+                      FIX(0.757396450)),          /* 128/169 */
+             CONST_BITS+1);
     tmp6 += tmp6;
     tmp0 -= tmp6;
     tmp1 -= tmp6;
@@ -1728,18 +1750,18 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp5 -= tmp6;
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0, FIX(1.039995521)) +   /* c2 */
-              MULTIPLY(tmp1, FIX(0.801745081)) +   /* c6 */
-              MULTIPLY(tmp2, FIX(0.379824504)) -   /* c10 */
-              MULTIPLY(tmp3, FIX(0.129109289)) -   /* c12 */
-              MULTIPLY(tmp4, FIX(0.608465700)) -   /* c8 */
-              MULTIPLY(tmp5, FIX(0.948429952)),    /* c4 */
-              CONST_BITS+1);
+             MULTIPLY(tmp1, FIX(0.801745081)) +   /* c6 */
+             MULTIPLY(tmp2, FIX(0.379824504)) -   /* c10 */
+             MULTIPLY(tmp3, FIX(0.129109289)) -   /* c12 */
+             MULTIPLY(tmp4, FIX(0.608465700)) -   /* c8 */
+             MULTIPLY(tmp5, FIX(0.948429952)),    /* c4 */
+             CONST_BITS+1);
     z1 = MULTIPLY(tmp0 - tmp2, FIX(0.875087516)) - /* (c4+c6)/2 */
-         MULTIPLY(tmp3 - tmp4, FIX(0.330085509)) - /* (c2-c10)/2 */
-         MULTIPLY(tmp1 - tmp5, FIX(0.239678205));  /* (c8-c12)/2 */
+        MULTIPLY(tmp3 - tmp4, FIX(0.330085509)) - /* (c2-c10)/2 */
+        MULTIPLY(tmp1 - tmp5, FIX(0.239678205));  /* (c8-c12)/2 */
     z2 = MULTIPLY(tmp0 + tmp2, FIX(0.073342435)) - /* (c4-c6)/2 */
-         MULTIPLY(tmp3 + tmp4, FIX(0.709910013)) + /* (c2+c10)/2 */
-         MULTIPLY(tmp1 + tmp5, FIX(0.368787494));  /* (c8+c12)/2 */
+        MULTIPLY(tmp3 + tmp4, FIX(0.709910013)) + /* (c2+c10)/2 */
+        MULTIPLY(tmp1 + tmp5, FIX(0.368787494));  /* (c8+c12)/2 */
 
     dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+1);
     dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 - z2, CONST_BITS+1);
@@ -1749,23 +1771,23 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp1 = MULTIPLY(tmp10 + tmp11, FIX(1.001514908));   /* c3 */
     tmp2 = MULTIPLY(tmp10 + tmp12, FIX(0.881514751));   /* c5 */
     tmp3 = MULTIPLY(tmp10 + tmp13, FIX(0.710284161)) +  /* c7 */
-           MULTIPLY(tmp14 + tmp15, FIX(0.256335874));   /* c11 */
+          MULTIPLY(tmp14 + tmp15, FIX(0.256335874));   /* c11 */
     tmp0 = tmp1 + tmp2 + tmp3 -
-           MULTIPLY(tmp10, FIX(1.530003162)) +          /* c3+c5+c7-c1 */
-           MULTIPLY(tmp14, FIX(0.241438564));           /* c9-c11 */
+          MULTIPLY(tmp10, FIX(1.530003162)) +          /* c3+c5+c7-c1 */
+          MULTIPLY(tmp14, FIX(0.241438564));           /* c9-c11 */
     tmp4 = MULTIPLY(tmp14 - tmp15, FIX(0.710284161)) -  /* c7 */
-           MULTIPLY(tmp11 + tmp12, FIX(0.256335874));   /* c11 */
+          MULTIPLY(tmp11 + tmp12, FIX(0.256335874));   /* c11 */
     tmp5 = MULTIPLY(tmp11 + tmp13, - FIX(0.881514751)); /* -c5 */
     tmp1 += tmp4 + tmp5 +
-            MULTIPLY(tmp11, FIX(0.634110155)) -         /* c5+c9+c11-c3 */
-            MULTIPLY(tmp14, FIX(1.773594819));          /* c1+c7 */
+           MULTIPLY(tmp11, FIX(0.634110155)) -         /* c5+c9+c11-c3 */
+           MULTIPLY(tmp14, FIX(1.773594819));          /* c1+c7 */
     tmp6 = MULTIPLY(tmp12 + tmp13, - FIX(0.497774438)); /* -c9 */
     tmp2 += tmp4 + tmp6 -
-            MULTIPLY(tmp12, FIX(1.190715098)) +         /* c1+c5-c9-c11 */
-            MULTIPLY(tmp15, FIX(1.711799069));          /* c3+c7 */
+           MULTIPLY(tmp12, FIX(1.190715098)) +         /* c1+c5-c9-c11 */
+           MULTIPLY(tmp15, FIX(1.711799069));          /* c3+c7 */
     tmp3 += tmp5 + tmp6 +
-            MULTIPLY(tmp13, FIX(1.670519935)) -         /* c3+c5+c9-c7 */
-            MULTIPLY(tmp15, FIX(1.319646532));          /* c1+c11 */
+           MULTIPLY(tmp13, FIX(1.670519935)) -         /* c3+c5+c9-c7 */
+           MULTIPLY(tmp15, FIX(1.319646532));          /* c1+c11 */
 
     dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+1);
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+1);
@@ -1794,9 +1816,10 @@ jpeg_fdct_14x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT. */
-  /* cK represents sqrt(2) * cos(K*pi/28). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT.
+   * cK represents sqrt(2) * cos(K*pi/28).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -1828,26 +1851,26 @@ jpeg_fdct_14x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[8]);
     tmp6 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[7]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       (tmp10 + tmp11 + tmp12 + tmp13 - 14 * CENTERJSAMPLE);
     tmp13 += tmp13;
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.274162392)) + /* c4 */
-              MULTIPLY(tmp11 - tmp13, FIX(0.314692123)) - /* c12 */
-              MULTIPLY(tmp12 - tmp13, FIX(0.881747734)),  /* c8 */
-              CONST_BITS);
+             MULTIPLY(tmp11 - tmp13, FIX(0.314692123)) - /* c12 */
+             MULTIPLY(tmp12 - tmp13, FIX(0.881747734)),  /* c8 */
+             CONST_BITS);
 
     tmp10 = MULTIPLY(tmp14 + tmp15, FIX(1.105676686));    /* c6 */
 
     dataptr[2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp14, FIX(0.273079590))   /* c2-c6 */
-              + MULTIPLY(tmp16, FIX(0.613604268)),        /* c10 */
-              CONST_BITS);
+             + MULTIPLY(tmp16, FIX(0.613604268)),        /* c10 */
+             CONST_BITS);
     dataptr[6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.719280954))   /* c6+c10 */
-              - MULTIPLY(tmp16, FIX(1.378756276)),        /* c2 */
-              CONST_BITS);
+             - MULTIPLY(tmp16, FIX(1.378756276)),        /* c2 */
+             CONST_BITS);
 
     /* Odd part */
 
@@ -1859,27 +1882,27 @@ jpeg_fdct_14x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp11 = MULTIPLY(tmp11, FIX(1.405321284));            /* c1 */
     tmp10 += tmp11 - tmp3;
     tmp11 = MULTIPLY(tmp0 + tmp2, FIX(1.197448846)) +     /* c5 */
-            MULTIPLY(tmp4 + tmp6, FIX(0.752406978));      /* c9 */
+           MULTIPLY(tmp4 + tmp6, FIX(0.752406978));      /* c9 */
     dataptr[5] = (DCTELEM)
       DESCALE(tmp10 + tmp11 - MULTIPLY(tmp2, FIX(2.373959773)) /* c3+c5-c13 */
-              + MULTIPLY(tmp4, FIX(1.119999435)),         /* c1+c11-c9 */
-              CONST_BITS);
+             + MULTIPLY(tmp4, FIX(1.119999435)),         /* c1+c11-c9 */
+             CONST_BITS);
     tmp12 = MULTIPLY(tmp0 + tmp1, FIX(1.334852607)) +     /* c3 */
-            MULTIPLY(tmp5 - tmp6, FIX(0.467085129));      /* c11 */
+           MULTIPLY(tmp5 - tmp6, FIX(0.467085129));      /* c11 */
     dataptr[3] = (DCTELEM)
       DESCALE(tmp10 + tmp12 - MULTIPLY(tmp1, FIX(0.424103948)) /* c3-c9-c13 */
-              - MULTIPLY(tmp5, FIX(3.069855259)),         /* c1+c5+c11 */
-              CONST_BITS);
+             - MULTIPLY(tmp5, FIX(3.069855259)),         /* c1+c5+c11 */
+             CONST_BITS);
     dataptr[1] = (DCTELEM)
       DESCALE(tmp11 + tmp12 + tmp3 + tmp6 -
-              MULTIPLY(tmp0 + tmp6, FIX(1.126980169)),    /* c3+c5-c1 */
-              CONST_BITS);
+             MULTIPLY(tmp0 + tmp6, FIX(1.126980169)),    /* c3+c5-c1 */
+             CONST_BITS);
 
     ctr++;
 
     if (ctr != DCTSIZE) {
       if (ctr == 14)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -1922,25 +1945,25 @@ jpeg_fdct_14x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12 + tmp13,
-                       FIX(0.653061224)),                 /* 32/49 */
-              CONST_BITS+1);
+                      FIX(0.653061224)),                 /* 32/49 */
+             CONST_BITS+1);
     tmp13 += tmp13;
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp13, FIX(0.832106052)) + /* c4 */
-              MULTIPLY(tmp11 - tmp13, FIX(0.205513223)) - /* c12 */
-              MULTIPLY(tmp12 - tmp13, FIX(0.575835255)),  /* c8 */
-              CONST_BITS+1);
+             MULTIPLY(tmp11 - tmp13, FIX(0.205513223)) - /* c12 */
+             MULTIPLY(tmp12 - tmp13, FIX(0.575835255)),  /* c8 */
+             CONST_BITS+1);
 
     tmp10 = MULTIPLY(tmp14 + tmp15, FIX(0.722074570));    /* c6 */
 
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp14, FIX(0.178337691))   /* c2-c6 */
-              + MULTIPLY(tmp16, FIX(0.400721155)),        /* c10 */
-              CONST_BITS+1);
+             + MULTIPLY(tmp16, FIX(0.400721155)),        /* c10 */
+             CONST_BITS+1);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.122795725))   /* c6+c10 */
-              - MULTIPLY(tmp16, FIX(0.900412262)),        /* c2 */
-              CONST_BITS+1);
+             - MULTIPLY(tmp16, FIX(0.900412262)),        /* c2 */
+             CONST_BITS+1);
 
     /* Odd part */
 
@@ -1948,29 +1971,29 @@ jpeg_fdct_14x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp11 = tmp5 - tmp4;
     dataptr[DCTSIZE*7] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 - tmp10 + tmp3 - tmp11 - tmp6,
-                       FIX(0.653061224)),                 /* 32/49 */
-              CONST_BITS+1);
+                      FIX(0.653061224)),                 /* 32/49 */
+             CONST_BITS+1);
     tmp3  = MULTIPLY(tmp3 , FIX(0.653061224));            /* 32/49 */
     tmp10 = MULTIPLY(tmp10, - FIX(0.103406812));          /* -c13 */
     tmp11 = MULTIPLY(tmp11, FIX(0.917760839));            /* c1 */
     tmp10 += tmp11 - tmp3;
     tmp11 = MULTIPLY(tmp0 + tmp2, FIX(0.782007410)) +     /* c5 */
-            MULTIPLY(tmp4 + tmp6, FIX(0.491367823));      /* c9 */
+           MULTIPLY(tmp4 + tmp6, FIX(0.491367823));      /* c9 */
     dataptr[DCTSIZE*5] = (DCTELEM)
       DESCALE(tmp10 + tmp11 - MULTIPLY(tmp2, FIX(1.550341076)) /* c3+c5-c13 */
-              + MULTIPLY(tmp4, FIX(0.731428202)),         /* c1+c11-c9 */
-              CONST_BITS+1);
+             + MULTIPLY(tmp4, FIX(0.731428202)),         /* c1+c11-c9 */
+             CONST_BITS+1);
     tmp12 = MULTIPLY(tmp0 + tmp1, FIX(0.871740478)) +     /* c3 */
-            MULTIPLY(tmp5 - tmp6, FIX(0.305035186));      /* c11 */
+           MULTIPLY(tmp5 - tmp6, FIX(0.305035186));      /* c11 */
     dataptr[DCTSIZE*3] = (DCTELEM)
       DESCALE(tmp10 + tmp12 - MULTIPLY(tmp1, FIX(0.276965844)) /* c3-c9-c13 */
-              - MULTIPLY(tmp5, FIX(2.004803435)),         /* c1+c5+c11 */
-              CONST_BITS+1);
+             - MULTIPLY(tmp5, FIX(2.004803435)),         /* c1+c5+c11 */
+             CONST_BITS+1);
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(tmp11 + tmp12 + tmp3
-              - MULTIPLY(tmp0, FIX(0.735987049))          /* c3+c5-c1 */
-              - MULTIPLY(tmp6, FIX(0.082925825)),         /* c9-c11-c13 */
-              CONST_BITS+1);
+             - MULTIPLY(tmp0, FIX(0.735987049))          /* c3+c5-c1 */
+             - MULTIPLY(tmp6, FIX(0.082925825)),         /* c9-c11-c13 */
+             CONST_BITS+1);
 
     dataptr++;                 /* advance pointer to next column */
     wsptr++;                   /* advance pointer to next column */
@@ -1995,9 +2018,10 @@ jpeg_fdct_15x15 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT. */
-  /* cK represents sqrt(2) * cos(K*pi/30). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT.
+   * cK represents sqrt(2) * cos(K*pi/30).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -2026,21 +2050,21 @@ jpeg_fdct_15x15 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     z1 = tmp0 + tmp4 + tmp5;
     z2 = tmp1 + tmp3 + tmp6;
     z3 = tmp2 + tmp7;
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM) (z1 + z2 + z3 - 15 * CENTERJSAMPLE);
     z3 += z3;
     dataptr[6] = (DCTELEM)
       DESCALE(MULTIPLY(z1 - z3, FIX(1.144122806)) - /* c6 */
-              MULTIPLY(z2 - z3, FIX(0.437016024)),  /* c12 */
-              CONST_BITS);
+             MULTIPLY(z2 - z3, FIX(0.437016024)),  /* c12 */
+             CONST_BITS);
     tmp2 += ((tmp1 + tmp4) >> 1) - tmp7 - tmp7;
     z1 = MULTIPLY(tmp3 - tmp2, FIX(1.531135173)) -  /* c2+c14 */
          MULTIPLY(tmp6 - tmp2, FIX(2.238241955));   /* c4+c8 */
     z2 = MULTIPLY(tmp5 - tmp2, FIX(0.798468008)) -  /* c8-c14 */
-         MULTIPLY(tmp0 - tmp2, FIX(0.091361227));   /* c2-c4 */
+        MULTIPLY(tmp0 - tmp2, FIX(0.091361227));   /* c2-c4 */
     z3 = MULTIPLY(tmp0 - tmp3, FIX(1.383309603)) +  /* c2 */
-         MULTIPLY(tmp6 - tmp5, FIX(0.946293579)) +  /* c8 */
-         MULTIPLY(tmp1 - tmp4, FIX(0.790569415));   /* (c6+c12)/2 */
+        MULTIPLY(tmp6 - tmp5, FIX(0.946293579)) +  /* c8 */
+        MULTIPLY(tmp1 - tmp4, FIX(0.790569415));   /* (c6+c12)/2 */
 
     dataptr[2] = (DCTELEM) DESCALE(z1 + z3, CONST_BITS);
     dataptr[4] = (DCTELEM) DESCALE(z2 + z3, CONST_BITS);
@@ -2048,19 +2072,19 @@ jpeg_fdct_15x15 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     /* Odd part */
 
     tmp2 = MULTIPLY(tmp10 - tmp12 - tmp13 + tmp15 + tmp16,
-                    FIX(1.224744871));                         /* c5 */
+                   FIX(1.224744871));                         /* c5 */
     tmp1 = MULTIPLY(tmp10 - tmp14 - tmp15, FIX(1.344997024)) + /* c3 */
-           MULTIPLY(tmp11 - tmp13 - tmp16, FIX(0.831253876));  /* c9 */
+          MULTIPLY(tmp11 - tmp13 - tmp16, FIX(0.831253876));  /* c9 */
     tmp12 = MULTIPLY(tmp12, FIX(1.224744871));                 /* c5 */
     tmp4 = MULTIPLY(tmp10 - tmp16, FIX(1.406466353)) +         /* c1 */
-           MULTIPLY(tmp11 + tmp14, FIX(1.344997024)) +         /* c3 */
-           MULTIPLY(tmp13 + tmp15, FIX(0.575212477));          /* c11 */
+          MULTIPLY(tmp11 + tmp14, FIX(1.344997024)) +         /* c3 */
+          MULTIPLY(tmp13 + tmp15, FIX(0.575212477));          /* c11 */
     tmp0 = MULTIPLY(tmp13, FIX(0.475753014)) -                 /* c7-c11 */
-           MULTIPLY(tmp14, FIX(0.513743148)) +                 /* c3-c9 */
-           MULTIPLY(tmp16, FIX(1.700497885)) + tmp4 + tmp12;   /* c1+c13 */
+          MULTIPLY(tmp14, FIX(0.513743148)) +                 /* c3-c9 */
+          MULTIPLY(tmp16, FIX(1.700497885)) + tmp4 + tmp12;   /* c1+c13 */
     tmp3 = MULTIPLY(tmp10, - FIX(0.355500862)) -               /* -(c1-c7) */
-           MULTIPLY(tmp11, FIX(2.176250899)) -                 /* c3+c9 */
-           MULTIPLY(tmp15, FIX(0.869244010)) + tmp4 - tmp12;   /* c11+c13 */
+          MULTIPLY(tmp11, FIX(2.176250899)) -                 /* c3+c9 */
+          MULTIPLY(tmp15, FIX(0.869244010)) + tmp4 - tmp12;   /* c11+c13 */
 
     dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS);
     dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS);
@@ -2071,7 +2095,7 @@ jpeg_fdct_15x15 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     if (ctr != DCTSIZE) {
       if (ctr == 15)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -2111,20 +2135,20 @@ jpeg_fdct_15x15 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     z3 = tmp2 + tmp7;
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(z1 + z2 + z3, FIX(1.137777778)), /* 256/225 */
-              CONST_BITS+2);
+             CONST_BITS+2);
     z3 += z3;
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(MULTIPLY(z1 - z3, FIX(1.301757503)) - /* c6 */
-              MULTIPLY(z2 - z3, FIX(0.497227121)),  /* c12 */
-              CONST_BITS+2);
+             MULTIPLY(z2 - z3, FIX(0.497227121)),  /* c12 */
+             CONST_BITS+2);
     tmp2 += ((tmp1 + tmp4) >> 1) - tmp7 - tmp7;
     z1 = MULTIPLY(tmp3 - tmp2, FIX(1.742091575)) -  /* c2+c14 */
          MULTIPLY(tmp6 - tmp2, FIX(2.546621957));   /* c4+c8 */
     z2 = MULTIPLY(tmp5 - tmp2, FIX(0.908479156)) -  /* c8-c14 */
-         MULTIPLY(tmp0 - tmp2, FIX(0.103948774));   /* c2-c4 */
+        MULTIPLY(tmp0 - tmp2, FIX(0.103948774));   /* c2-c4 */
     z3 = MULTIPLY(tmp0 - tmp3, FIX(1.573898926)) +  /* c2 */
-         MULTIPLY(tmp6 - tmp5, FIX(1.076671805)) +  /* c8 */
-         MULTIPLY(tmp1 - tmp4, FIX(0.899492312));   /* (c6+c12)/2 */
+        MULTIPLY(tmp6 - tmp5, FIX(1.076671805)) +  /* c8 */
+        MULTIPLY(tmp1 - tmp4, FIX(0.899492312));   /* (c6+c12)/2 */
 
     dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + z3, CONST_BITS+2);
     dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(z2 + z3, CONST_BITS+2);
@@ -2132,19 +2156,19 @@ jpeg_fdct_15x15 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     /* Odd part */
 
     tmp2 = MULTIPLY(tmp10 - tmp12 - tmp13 + tmp15 + tmp16,
-                    FIX(1.393487498));                         /* c5 */
+                   FIX(1.393487498));                         /* c5 */
     tmp1 = MULTIPLY(tmp10 - tmp14 - tmp15, FIX(1.530307725)) + /* c3 */
-           MULTIPLY(tmp11 - tmp13 - tmp16, FIX(0.945782187));  /* c9 */
+          MULTIPLY(tmp11 - tmp13 - tmp16, FIX(0.945782187));  /* c9 */
     tmp12 = MULTIPLY(tmp12, FIX(1.393487498));                 /* c5 */
     tmp4 = MULTIPLY(tmp10 - tmp16, FIX(1.600246161)) +         /* c1 */
-           MULTIPLY(tmp11 + tmp14, FIX(1.530307725)) +         /* c3 */
-           MULTIPLY(tmp13 + tmp15, FIX(0.654463974));          /* c11 */
+          MULTIPLY(tmp11 + tmp14, FIX(1.530307725)) +         /* c3 */
+          MULTIPLY(tmp13 + tmp15, FIX(0.654463974));          /* c11 */
     tmp0 = MULTIPLY(tmp13, FIX(0.541301207)) -                 /* c7-c11 */
-           MULTIPLY(tmp14, FIX(0.584525538)) +                 /* c3-c9 */
-           MULTIPLY(tmp16, FIX(1.934788705)) + tmp4 + tmp12;   /* c1+c13 */
+          MULTIPLY(tmp14, FIX(0.584525538)) +                 /* c3-c9 */
+          MULTIPLY(tmp16, FIX(1.934788705)) + tmp4 + tmp12;   /* c1+c13 */
     tmp3 = MULTIPLY(tmp10, - FIX(0.404480980)) -               /* -(c1-c7) */
-           MULTIPLY(tmp11, FIX(2.476089912)) -                 /* c3+c9 */
-           MULTIPLY(tmp15, FIX(0.989006518)) + tmp4 - tmp12;   /* c11+c13 */
+          MULTIPLY(tmp11, FIX(2.476089912)) -                 /* c3+c9 */
+          MULTIPLY(tmp15, FIX(0.989006518)) + tmp4 - tmp12;   /* c11+c13 */
 
     dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+2);
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+2);
@@ -2173,10 +2197,11 @@ jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* cK represents sqrt(2) * cos(K*pi/32). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * cK represents sqrt(2) * cos(K*pi/32).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -2212,49 +2237,49 @@ jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp6 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[9]);
     tmp7 = GETJSAMPLE(elemptr[7]) - GETJSAMPLE(elemptr[8]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 + tmp12 + tmp13 - 16 * CENTERJSAMPLE) << PASS1_BITS);
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */
-              MULTIPLY(tmp11 - tmp12, FIX_0_541196100),   /* c12[16] = c6[8] */
-              CONST_BITS-PASS1_BITS);
+             MULTIPLY(tmp11 - tmp12, FIX_0_541196100),   /* c12[16] = c6[8] */
+             CONST_BITS-PASS1_BITS);
 
     tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) +   /* c14[16] = c7[8] */
-            MULTIPLY(tmp14 - tmp16, FIX(1.387039845));    /* c2[16] = c1[8] */
+           MULTIPLY(tmp14 - tmp16, FIX(1.387039845));    /* c2[16] = c1[8] */
 
     dataptr[2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp15, FIX(1.451774982))   /* c6+c14 */
-              + MULTIPLY(tmp16, FIX(2.172734804)),        /* c2+c10 */
-              CONST_BITS-PASS1_BITS);
+             + MULTIPLY(tmp16, FIX(2.172734804)),        /* c2+c10 */
+             CONST_BITS-PASS1_BITS);
     dataptr[6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp14, FIX(0.211164243))   /* c2-c6 */
-              - MULTIPLY(tmp17, FIX(1.061594338)),        /* c10+c14 */
-              CONST_BITS-PASS1_BITS);
+             - MULTIPLY(tmp17, FIX(1.061594338)),        /* c10+c14 */
+             CONST_BITS-PASS1_BITS);
 
     /* Odd part */
 
     tmp11 = MULTIPLY(tmp0 + tmp1, FIX(1.353318001)) +         /* c3 */
-            MULTIPLY(tmp6 - tmp7, FIX(0.410524528));          /* c13 */
+           MULTIPLY(tmp6 - tmp7, FIX(0.410524528));          /* c13 */
     tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.247225013)) +         /* c5 */
-            MULTIPLY(tmp5 + tmp7, FIX(0.666655658));          /* c11 */
+           MULTIPLY(tmp5 + tmp7, FIX(0.666655658));          /* c11 */
     tmp13 = MULTIPLY(tmp0 + tmp3, FIX(1.093201867)) +         /* c7 */
-            MULTIPLY(tmp4 - tmp7, FIX(0.897167586));          /* c9 */
+           MULTIPLY(tmp4 - tmp7, FIX(0.897167586));          /* c9 */
     tmp14 = MULTIPLY(tmp1 + tmp2, FIX(0.138617169)) +         /* c15 */
-            MULTIPLY(tmp6 - tmp5, FIX(1.407403738));          /* c1 */
+           MULTIPLY(tmp6 - tmp5, FIX(1.407403738));          /* c1 */
     tmp15 = MULTIPLY(tmp1 + tmp3, - FIX(0.666655658)) +       /* -c11 */
-            MULTIPLY(tmp4 + tmp6, - FIX(1.247225013));        /* -c5 */
+           MULTIPLY(tmp4 + tmp6, - FIX(1.247225013));        /* -c5 */
     tmp16 = MULTIPLY(tmp2 + tmp3, - FIX(1.353318001)) +       /* -c3 */
-            MULTIPLY(tmp5 - tmp4, FIX(0.410524528));          /* c13 */
+           MULTIPLY(tmp5 - tmp4, FIX(0.410524528));          /* c13 */
     tmp10 = tmp11 + tmp12 + tmp13 -
-            MULTIPLY(tmp0, FIX(2.286341144)) +                /* c7+c5+c3-c1 */
-            MULTIPLY(tmp7, FIX(0.779653625));                 /* c15+c13-c11+c9 */
+           MULTIPLY(tmp0, FIX(2.286341144)) +                /* c7+c5+c3-c1 */
+           MULTIPLY(tmp7, FIX(0.779653625));                 /* c15+c13-c11+c9 */
     tmp11 += tmp14 + tmp15 + MULTIPLY(tmp1, FIX(0.071888074)) /* c9-c3-c15+c11 */
-             - MULTIPLY(tmp6, FIX(1.663905119));              /* c7+c13+c1-c5 */
+            - MULTIPLY(tmp6, FIX(1.663905119));              /* c7+c13+c1-c5 */
     tmp12 += tmp14 + tmp16 - MULTIPLY(tmp2, FIX(1.125726048)) /* c7+c5+c15-c3 */
-             + MULTIPLY(tmp5, FIX(1.227391138));              /* c9-c11+c1-c13 */
+            + MULTIPLY(tmp5, FIX(1.227391138));              /* c9-c11+c1-c13 */
     tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */
-             + MULTIPLY(tmp4, FIX(2.167985692));              /* c1+c13+c5-c9 */
+            + MULTIPLY(tmp4, FIX(2.167985692));              /* c1+c13+c5-c9 */
 
     dataptr[1] = (DCTELEM) DESCALE(tmp10, CONST_BITS-PASS1_BITS);
     dataptr[3] = (DCTELEM) DESCALE(tmp11, CONST_BITS-PASS1_BITS);
@@ -2265,7 +2290,7 @@ jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     if (ctr != DCTSIZE) {
       if (ctr == DCTSIZE * 2)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -2275,6 +2300,7 @@ jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
    * We remove the PASS1_BITS scaling, but leave the results scaled up
    * by an overall factor of 8.
    * We must also scale the output by (8/16)**2 = 1/2**2.
+   * cK represents sqrt(2) * cos(K*pi/32).
    */
 
   dataptr = data;
@@ -2313,44 +2339,44 @@ jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
       DESCALE(tmp10 + tmp11 + tmp12 + tmp13, PASS1_BITS+2);
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */
-              MULTIPLY(tmp11 - tmp12, FIX_0_541196100),   /* c12[16] = c6[8] */
-              CONST_BITS+PASS1_BITS+2);
+             MULTIPLY(tmp11 - tmp12, FIX_0_541196100),   /* c12[16] = c6[8] */
+             CONST_BITS+PASS1_BITS+2);
 
     tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) +   /* c14[16] = c7[8] */
-            MULTIPLY(tmp14 - tmp16, FIX(1.387039845));    /* c2[16] = c1[8] */
+           MULTIPLY(tmp14 - tmp16, FIX(1.387039845));    /* c2[16] = c1[8] */
 
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp15, FIX(1.451774982))   /* c6+c14 */
-              + MULTIPLY(tmp16, FIX(2.172734804)),        /* c2+10 */
-              CONST_BITS+PASS1_BITS+2);
+             + MULTIPLY(tmp16, FIX(2.172734804)),        /* c2+10 */
+             CONST_BITS+PASS1_BITS+2);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp14, FIX(0.211164243))   /* c2-c6 */
-              - MULTIPLY(tmp17, FIX(1.061594338)),        /* c10+c14 */
-              CONST_BITS+PASS1_BITS+2);
+             - MULTIPLY(tmp17, FIX(1.061594338)),        /* c10+c14 */
+             CONST_BITS+PASS1_BITS+2);
 
     /* Odd part */
 
     tmp11 = MULTIPLY(tmp0 + tmp1, FIX(1.353318001)) +         /* c3 */
-            MULTIPLY(tmp6 - tmp7, FIX(0.410524528));          /* c13 */
+           MULTIPLY(tmp6 - tmp7, FIX(0.410524528));          /* c13 */
     tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.247225013)) +         /* c5 */
-            MULTIPLY(tmp5 + tmp7, FIX(0.666655658));          /* c11 */
+           MULTIPLY(tmp5 + tmp7, FIX(0.666655658));          /* c11 */
     tmp13 = MULTIPLY(tmp0 + tmp3, FIX(1.093201867)) +         /* c7 */
-            MULTIPLY(tmp4 - tmp7, FIX(0.897167586));          /* c9 */
+           MULTIPLY(tmp4 - tmp7, FIX(0.897167586));          /* c9 */
     tmp14 = MULTIPLY(tmp1 + tmp2, FIX(0.138617169)) +         /* c15 */
-            MULTIPLY(tmp6 - tmp5, FIX(1.407403738));          /* c1 */
+           MULTIPLY(tmp6 - tmp5, FIX(1.407403738));          /* c1 */
     tmp15 = MULTIPLY(tmp1 + tmp3, - FIX(0.666655658)) +       /* -c11 */
-            MULTIPLY(tmp4 + tmp6, - FIX(1.247225013));        /* -c5 */
+           MULTIPLY(tmp4 + tmp6, - FIX(1.247225013));        /* -c5 */
     tmp16 = MULTIPLY(tmp2 + tmp3, - FIX(1.353318001)) +       /* -c3 */
-            MULTIPLY(tmp5 - tmp4, FIX(0.410524528));          /* c13 */
+           MULTIPLY(tmp5 - tmp4, FIX(0.410524528));          /* c13 */
     tmp10 = tmp11 + tmp12 + tmp13 -
-            MULTIPLY(tmp0, FIX(2.286341144)) +                /* c7+c5+c3-c1 */
-            MULTIPLY(tmp7, FIX(0.779653625));                 /* c15+c13-c11+c9 */
+           MULTIPLY(tmp0, FIX(2.286341144)) +                /* c7+c5+c3-c1 */
+           MULTIPLY(tmp7, FIX(0.779653625));                 /* c15+c13-c11+c9 */
     tmp11 += tmp14 + tmp15 + MULTIPLY(tmp1, FIX(0.071888074)) /* c9-c3-c15+c11 */
-             - MULTIPLY(tmp6, FIX(1.663905119));              /* c7+c13+c1-c5 */
+            - MULTIPLY(tmp6, FIX(1.663905119));              /* c7+c13+c1-c5 */
     tmp12 += tmp14 + tmp16 - MULTIPLY(tmp2, FIX(1.125726048)) /* c7+c5+c15-c3 */
-             + MULTIPLY(tmp5, FIX(1.227391138));              /* c9-c11+c1-c13 */
+            + MULTIPLY(tmp5, FIX(1.227391138));              /* c9-c11+c1-c13 */
     tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */
-             + MULTIPLY(tmp4, FIX(2.167985692));              /* c1+c13+c5-c9 */
+            + MULTIPLY(tmp4, FIX(2.167985692));              /* c1+c13+c5-c9 */
 
     dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+PASS1_BITS+2);
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+PASS1_BITS+2);
@@ -2380,10 +2406,11 @@ jpeg_fdct_16x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* 16-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/32). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * 16-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/32).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -2419,49 +2446,49 @@ jpeg_fdct_16x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp6 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[9]);
     tmp7 = GETJSAMPLE(elemptr[7]) - GETJSAMPLE(elemptr[8]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 + tmp12 + tmp13 - 16 * CENTERJSAMPLE) << PASS1_BITS);
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */
-              MULTIPLY(tmp11 - tmp12, FIX_0_541196100),   /* c12[16] = c6[8] */
-              CONST_BITS-PASS1_BITS);
+             MULTIPLY(tmp11 - tmp12, FIX_0_541196100),   /* c12[16] = c6[8] */
+             CONST_BITS-PASS1_BITS);
 
     tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) +   /* c14[16] = c7[8] */
-            MULTIPLY(tmp14 - tmp16, FIX(1.387039845));    /* c2[16] = c1[8] */
+           MULTIPLY(tmp14 - tmp16, FIX(1.387039845));    /* c2[16] = c1[8] */
 
     dataptr[2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp15, FIX(1.451774982))   /* c6+c14 */
-              + MULTIPLY(tmp16, FIX(2.172734804)),        /* c2+c10 */
-              CONST_BITS-PASS1_BITS);
+             + MULTIPLY(tmp16, FIX(2.172734804)),        /* c2+c10 */
+             CONST_BITS-PASS1_BITS);
     dataptr[6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp14, FIX(0.211164243))   /* c2-c6 */
-              - MULTIPLY(tmp17, FIX(1.061594338)),        /* c10+c14 */
-              CONST_BITS-PASS1_BITS);
+             - MULTIPLY(tmp17, FIX(1.061594338)),        /* c10+c14 */
+             CONST_BITS-PASS1_BITS);
 
     /* Odd part */
 
     tmp11 = MULTIPLY(tmp0 + tmp1, FIX(1.353318001)) +         /* c3 */
-            MULTIPLY(tmp6 - tmp7, FIX(0.410524528));          /* c13 */
+           MULTIPLY(tmp6 - tmp7, FIX(0.410524528));          /* c13 */
     tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.247225013)) +         /* c5 */
-            MULTIPLY(tmp5 + tmp7, FIX(0.666655658));          /* c11 */
+           MULTIPLY(tmp5 + tmp7, FIX(0.666655658));          /* c11 */
     tmp13 = MULTIPLY(tmp0 + tmp3, FIX(1.093201867)) +         /* c7 */
-            MULTIPLY(tmp4 - tmp7, FIX(0.897167586));          /* c9 */
+           MULTIPLY(tmp4 - tmp7, FIX(0.897167586));          /* c9 */
     tmp14 = MULTIPLY(tmp1 + tmp2, FIX(0.138617169)) +         /* c15 */
-            MULTIPLY(tmp6 - tmp5, FIX(1.407403738));          /* c1 */
+           MULTIPLY(tmp6 - tmp5, FIX(1.407403738));          /* c1 */
     tmp15 = MULTIPLY(tmp1 + tmp3, - FIX(0.666655658)) +       /* -c11 */
-            MULTIPLY(tmp4 + tmp6, - FIX(1.247225013));        /* -c5 */
+           MULTIPLY(tmp4 + tmp6, - FIX(1.247225013));        /* -c5 */
     tmp16 = MULTIPLY(tmp2 + tmp3, - FIX(1.353318001)) +       /* -c3 */
-            MULTIPLY(tmp5 - tmp4, FIX(0.410524528));          /* c13 */
+           MULTIPLY(tmp5 - tmp4, FIX(0.410524528));          /* c13 */
     tmp10 = tmp11 + tmp12 + tmp13 -
-            MULTIPLY(tmp0, FIX(2.286341144)) +                /* c7+c5+c3-c1 */
-            MULTIPLY(tmp7, FIX(0.779653625));                 /* c15+c13-c11+c9 */
+           MULTIPLY(tmp0, FIX(2.286341144)) +                /* c7+c5+c3-c1 */
+           MULTIPLY(tmp7, FIX(0.779653625));                 /* c15+c13-c11+c9 */
     tmp11 += tmp14 + tmp15 + MULTIPLY(tmp1, FIX(0.071888074)) /* c9-c3-c15+c11 */
-             - MULTIPLY(tmp6, FIX(1.663905119));              /* c7+c13+c1-c5 */
+            - MULTIPLY(tmp6, FIX(1.663905119));              /* c7+c13+c1-c5 */
     tmp12 += tmp14 + tmp16 - MULTIPLY(tmp2, FIX(1.125726048)) /* c7+c5+c15-c3 */
-             + MULTIPLY(tmp5, FIX(1.227391138));              /* c9-c11+c1-c13 */
+            + MULTIPLY(tmp5, FIX(1.227391138));              /* c9-c11+c1-c13 */
     tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */
-             + MULTIPLY(tmp4, FIX(2.167985692));              /* c1+c13+c5-c9 */
+            + MULTIPLY(tmp4, FIX(2.167985692));              /* c1+c13+c5-c9 */
 
     dataptr[1] = (DCTELEM) DESCALE(tmp10, CONST_BITS-PASS1_BITS);
     dataptr[3] = (DCTELEM) DESCALE(tmp11, CONST_BITS-PASS1_BITS);
@@ -2475,12 +2502,13 @@ jpeg_fdct_16x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
    * We remove the PASS1_BITS scaling, but leave the results scaled up
    * by an overall factor of 8.
    * We must also scale the output by 8/16 = 1/2.
+   * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
    */
 
   dataptr = data;
   for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
     /* Even part per LL&M figure 1 --- note that published figure is faulty;
-     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+     * rotator "c1" should be "c6".
      */
 
     tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
@@ -2501,43 +2529,43 @@ jpeg_fdct_16x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS+1);
     dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS+1);
 
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
-    dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, FIX_0_765366865),
-                                           CONST_BITS+PASS1_BITS+1);
-    dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 - MULTIPLY(tmp13, FIX_1_847759065),
-                                           CONST_BITS+PASS1_BITS+1);
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);   /* c6 */
+    dataptr[DCTSIZE*2] = (DCTELEM)
+      DESCALE(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */
+             CONST_BITS+PASS1_BITS+1);
+    dataptr[DCTSIZE*6] = (DCTELEM)
+      DESCALE(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */
+             CONST_BITS+PASS1_BITS+1);
 
     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
-     * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
      * i0..i3 in the paper are tmp0..tmp3 here.
      */
 
-    tmp10 = tmp0 + tmp3;
-    tmp11 = tmp1 + tmp2;
     tmp12 = tmp0 + tmp2;
     tmp13 = tmp1 + tmp3;
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /*  c3 */
-
-    tmp0  = MULTIPLY(tmp0,    FIX_1_501321110);    /*  c1+c3-c5-c7 */
-    tmp1  = MULTIPLY(tmp1,    FIX_3_072711026);    /*  c1+c3+c5-c7 */
-    tmp2  = MULTIPLY(tmp2,    FIX_2_053119869);    /*  c1+c3-c5+c7 */
-    tmp3  = MULTIPLY(tmp3,    FIX_0_298631336);    /* -c1+c3+c5-c7 */
-    tmp10 = MULTIPLY(tmp10, - FIX_0_899976223);    /*  c7-c3 */
-    tmp11 = MULTIPLY(tmp11, - FIX_2_562915447);    /* -c1-c3 */
-    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);    /*  c5-c3 */
-    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);    /* -c3-c5 */
 
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602);   /*  c3 */
+    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);      /* -c3+c5 */
+    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);      /* -c3-c5 */
     tmp12 += z1;
     tmp13 += z1;
 
-    dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0 + tmp10 + tmp12,
-                                           CONST_BITS+PASS1_BITS+1);
-    dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1 + tmp11 + tmp13,
-                                           CONST_BITS+PASS1_BITS+1);
-    dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2 + tmp11 + tmp12,
-                                           CONST_BITS+PASS1_BITS+1);
-    dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp3 + tmp10 + tmp13,
-                                           CONST_BITS+PASS1_BITS+1);
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223);   /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_1_501321110);          /*  c1+c3-c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_0_298631336);          /* -c1+c3+c5-c7 */
+    tmp0 += z1 + tmp12;
+    tmp3 += z1 + tmp13;
+
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447);   /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_3_072711026);          /*  c1+c3+c5-c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_2_053119869);          /*  c1+c3-c5+c7 */
+    tmp1 += z1 + tmp13;
+    tmp2 += z1 + tmp12;
+
+    dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+PASS1_BITS+1);
+    dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+PASS1_BITS+1);
+    dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+PASS1_BITS+1);
+    dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp3, CONST_BITS+PASS1_BITS+1);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -2564,10 +2592,11 @@ jpeg_fdct_14x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Zero bottom row of output coefficient block. */
   MEMZERO(&data[DCTSIZE*7], SIZEOF(DCTELEM) * DCTSIZE);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* 14-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/28). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * 14-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/28).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 7; ctr++) {
@@ -2598,26 +2627,26 @@ jpeg_fdct_14x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[8]);
     tmp6 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[7]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 + tmp12 + tmp13 - 14 * CENTERJSAMPLE) << PASS1_BITS);
     tmp13 += tmp13;
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.274162392)) + /* c4 */
-              MULTIPLY(tmp11 - tmp13, FIX(0.314692123)) - /* c12 */
-              MULTIPLY(tmp12 - tmp13, FIX(0.881747734)),  /* c8 */
-              CONST_BITS-PASS1_BITS);
+             MULTIPLY(tmp11 - tmp13, FIX(0.314692123)) - /* c12 */
+             MULTIPLY(tmp12 - tmp13, FIX(0.881747734)),  /* c8 */
+             CONST_BITS-PASS1_BITS);
 
     tmp10 = MULTIPLY(tmp14 + tmp15, FIX(1.105676686));    /* c6 */
 
     dataptr[2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp14, FIX(0.273079590))   /* c2-c6 */
-              + MULTIPLY(tmp16, FIX(0.613604268)),        /* c10 */
-              CONST_BITS-PASS1_BITS);
+             + MULTIPLY(tmp16, FIX(0.613604268)),        /* c10 */
+             CONST_BITS-PASS1_BITS);
     dataptr[6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.719280954))   /* c6+c10 */
-              - MULTIPLY(tmp16, FIX(1.378756276)),        /* c2 */
-              CONST_BITS-PASS1_BITS);
+             - MULTIPLY(tmp16, FIX(1.378756276)),        /* c2 */
+             CONST_BITS-PASS1_BITS);
 
     /* Odd part */
 
@@ -2629,21 +2658,21 @@ jpeg_fdct_14x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp11 = MULTIPLY(tmp11, FIX(1.405321284));            /* c1 */
     tmp10 += tmp11 - tmp3;
     tmp11 = MULTIPLY(tmp0 + tmp2, FIX(1.197448846)) +     /* c5 */
-            MULTIPLY(tmp4 + tmp6, FIX(0.752406978));      /* c9 */
+           MULTIPLY(tmp4 + tmp6, FIX(0.752406978));      /* c9 */
     dataptr[5] = (DCTELEM)
       DESCALE(tmp10 + tmp11 - MULTIPLY(tmp2, FIX(2.373959773)) /* c3+c5-c13 */
-              + MULTIPLY(tmp4, FIX(1.119999435)),         /* c1+c11-c9 */
-              CONST_BITS-PASS1_BITS);
+             + MULTIPLY(tmp4, FIX(1.119999435)),         /* c1+c11-c9 */
+             CONST_BITS-PASS1_BITS);
     tmp12 = MULTIPLY(tmp0 + tmp1, FIX(1.334852607)) +     /* c3 */
-            MULTIPLY(tmp5 - tmp6, FIX(0.467085129));      /* c11 */
+           MULTIPLY(tmp5 - tmp6, FIX(0.467085129));      /* c11 */
     dataptr[3] = (DCTELEM)
       DESCALE(tmp10 + tmp12 - MULTIPLY(tmp1, FIX(0.424103948)) /* c3-c9-c13 */
-              - MULTIPLY(tmp5, FIX(3.069855259)),         /* c1+c5+c11 */
-              CONST_BITS-PASS1_BITS);
+             - MULTIPLY(tmp5, FIX(3.069855259)),         /* c1+c5+c11 */
+             CONST_BITS-PASS1_BITS);
     dataptr[1] = (DCTELEM)
       DESCALE(tmp11 + tmp12 + tmp3 + tmp6 -
-              MULTIPLY(tmp0 + tmp6, FIX(1.126980169)),    /* c3+c5-c1 */
-              CONST_BITS-PASS1_BITS);
+             MULTIPLY(tmp0 + tmp6, FIX(1.126980169)),    /* c3+c5-c1 */
+             CONST_BITS-PASS1_BITS);
 
     dataptr += DCTSIZE;                /* advance pointer to next row */
   }
@@ -2672,7 +2701,7 @@ jpeg_fdct_14x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     z1 = tmp0 + tmp2;
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(z1 + tmp1 + tmp3, FIX(1.306122449)), /* 64/49 */
-              CONST_BITS+PASS1_BITS+1);
+             CONST_BITS+PASS1_BITS+1);
     tmp3 += tmp3;
     z1 -= tmp3;
     z1 -= tmp3;
@@ -2684,7 +2713,7 @@ jpeg_fdct_14x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     z2 = MULTIPLY(tmp0 - tmp1, FIX(1.151670509));       /* c4 */
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(z2 + z3 - MULTIPLY(tmp1 - tmp3, FIX(0.923568041)), /* c2+c6-c4 */
-              CONST_BITS+PASS1_BITS+1);
+             CONST_BITS+PASS1_BITS+1);
     dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+PASS1_BITS+1);
 
     /* Odd part */
@@ -2727,10 +2756,11 @@ jpeg_fdct_12x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Zero 2 bottom rows of output coefficient block. */
   MEMZERO(&data[DCTSIZE*6], SIZEOF(DCTELEM) * DCTSIZE * 2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* 12-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/24). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * 12-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/24).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 6; ctr++) {
@@ -2759,16 +2789,16 @@ jpeg_fdct_12x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[7]);
     tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[6]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 + tmp12 - 12 * CENTERJSAMPLE) << PASS1_BITS);
     dataptr[6] = (DCTELEM) ((tmp13 - tmp14 - tmp15) << PASS1_BITS);
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.224744871)), /* c4 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
     dataptr[2] = (DCTELEM)
       DESCALE(tmp14 - tmp15 + MULTIPLY(tmp13 + tmp15, FIX(1.366025404)), /* c2 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
 
     /* Odd part */
 
@@ -2778,14 +2808,14 @@ jpeg_fdct_12x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.121971054));   /* c5 */
     tmp13 = MULTIPLY(tmp0 + tmp3, FIX(0.860918669));   /* c7 */
     tmp10 = tmp12 + tmp13 + tmp14 - MULTIPLY(tmp0, FIX(0.580774953)) /* c5+c7-c1 */
-            + MULTIPLY(tmp5, FIX(0.184591911));        /* c11 */
+           + MULTIPLY(tmp5, FIX(0.184591911));        /* c11 */
     tmp11 = MULTIPLY(tmp2 + tmp3, - FIX(0.184591911)); /* -c11 */
     tmp12 += tmp11 - tmp15 - MULTIPLY(tmp2, FIX(2.339493912)) /* c1+c5-c11 */
-            + MULTIPLY(tmp5, FIX(0.860918669));        /* c7 */
+           + MULTIPLY(tmp5, FIX(0.860918669));        /* c7 */
     tmp13 += tmp11 - tmp14 + MULTIPLY(tmp3, FIX(0.725788011)) /* c1+c11-c7 */
-            - MULTIPLY(tmp5, FIX(1.121971054));        /* c5 */
+           - MULTIPLY(tmp5, FIX(1.121971054));        /* c5 */
     tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.306562965)) /* c3 */
-            - MULTIPLY(tmp2 + tmp5, FIX_0_541196100);  /* c9 */
+           - MULTIPLY(tmp2 + tmp5, FIX_0_541196100);  /* c9 */
 
     dataptr[1] = (DCTELEM) DESCALE(tmp10, CONST_BITS-PASS1_BITS);
     dataptr[3] = (DCTELEM) DESCALE(tmp11, CONST_BITS-PASS1_BITS);
@@ -2820,13 +2850,13 @@ jpeg_fdct_12x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp11, FIX(1.777777778)),         /* 16/9 */
-              CONST_BITS+PASS1_BITS+1);
+             CONST_BITS+PASS1_BITS+1);
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp12, FIX(2.177324216)),                 /* c2 */
-              CONST_BITS+PASS1_BITS+1);
+             CONST_BITS+PASS1_BITS+1);
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(1.257078722)), /* c4 */
-              CONST_BITS+PASS1_BITS+1);
+             CONST_BITS+PASS1_BITS+1);
 
     /* Odd part */
 
@@ -2834,13 +2864,13 @@ jpeg_fdct_12x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp0 + tmp1, FIX(1.777777778)),   /* 16/9 */
-              CONST_BITS+PASS1_BITS+1);
+             CONST_BITS+PASS1_BITS+1);
     dataptr[DCTSIZE*3] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 - tmp1 - tmp2, FIX(1.777777778)),    /* 16/9 */
-              CONST_BITS+PASS1_BITS+1);
+             CONST_BITS+PASS1_BITS+1);
     dataptr[DCTSIZE*5] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp2 - tmp1, FIX(1.777777778)),   /* 16/9 */
-              CONST_BITS+PASS1_BITS+1);
+             CONST_BITS+PASS1_BITS+1);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -2866,10 +2896,11 @@ jpeg_fdct_10x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Zero 3 bottom rows of output coefficient block. */
   MEMZERO(&data[DCTSIZE*5], SIZEOF(DCTELEM) * DCTSIZE * 3);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* 10-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/20). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * 10-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/20).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 5; ctr++) {
@@ -2894,21 +2925,21 @@ jpeg_fdct_10x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[6]);
     tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[5]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 + tmp12 - 10 * CENTERJSAMPLE) << PASS1_BITS);
     tmp12 += tmp12;
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.144122806)) - /* c4 */
-              MULTIPLY(tmp11 - tmp12, FIX(0.437016024)),  /* c8 */
-              CONST_BITS-PASS1_BITS);
+             MULTIPLY(tmp11 - tmp12, FIX(0.437016024)),  /* c8 */
+             CONST_BITS-PASS1_BITS);
     tmp10 = MULTIPLY(tmp13 + tmp14, FIX(0.831253876));    /* c6 */
     dataptr[2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp13, FIX(0.513743148)),  /* c2-c6 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
     dataptr[6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.176250899)),  /* c2+c6 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
 
     /* Odd part */
 
@@ -2918,14 +2949,14 @@ jpeg_fdct_10x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp2 <<= CONST_BITS;
     dataptr[1] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0, FIX(1.396802247)) +          /* c1 */
-              MULTIPLY(tmp1, FIX(1.260073511)) + tmp2 +   /* c3 */
-              MULTIPLY(tmp3, FIX(0.642039522)) +          /* c7 */
-              MULTIPLY(tmp4, FIX(0.221231742)),           /* c9 */
-              CONST_BITS-PASS1_BITS);
+             MULTIPLY(tmp1, FIX(1.260073511)) + tmp2 +   /* c3 */
+             MULTIPLY(tmp3, FIX(0.642039522)) +          /* c7 */
+             MULTIPLY(tmp4, FIX(0.221231742)),           /* c9 */
+             CONST_BITS-PASS1_BITS);
     tmp12 = MULTIPLY(tmp0 - tmp4, FIX(0.951056516)) -     /* (c3+c7)/2 */
-            MULTIPLY(tmp1 + tmp3, FIX(0.587785252));      /* (c1-c9)/2 */
+           MULTIPLY(tmp1 + tmp3, FIX(0.587785252));      /* (c1-c9)/2 */
     tmp13 = MULTIPLY(tmp10 + tmp11, FIX(0.309016994)) +   /* (c3-c7)/2 */
-            (tmp11 << (CONST_BITS - 1)) - tmp2;
+           (tmp11 << (CONST_BITS - 1)) - tmp2;
     dataptr[3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS-PASS1_BITS);
     dataptr[7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS-PASS1_BITS);
 
@@ -2956,7 +2987,7 @@ jpeg_fdct_10x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp2, FIX(1.28)),        /* 32/25 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     tmp11 = MULTIPLY(tmp11, FIX(1.011928851));          /* (c2+c4)/2 */
     tmp10 -= tmp2 << 2;
     tmp10 = MULTIPLY(tmp10, FIX(0.452548340));          /* (c2-c4)/2 */
@@ -2969,10 +3000,10 @@ jpeg_fdct_10x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp0, FIX(0.657591230)), /* c1-c3 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*3] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.785601151)), /* c1+c3 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -2999,17 +3030,19 @@ jpeg_fdct_8x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Zero 4 bottom rows of output coefficient block. */
   MEMZERO(&data[DCTSIZE*4], SIZEOF(DCTELEM) * DCTSIZE * 4);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* We must also scale the output by 8/4 = 2, which we add here. */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * We must also scale the output by 8/4 = 2, which we add here.
+   * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 4; ctr++) {
     elemptr = sample_data[ctr] + start_col;
 
     /* Even part per LL&M figure 1 --- note that published figure is faulty;
-     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+     * rotator "c1" should be "c6".
      */
 
     tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[7]);
@@ -3027,52 +3060,54 @@ jpeg_fdct_8x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[5]);
     tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 - 8 * CENTERJSAMPLE) << (PASS1_BITS+1));
     dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << (PASS1_BITS+1));
 
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);       /* c6 */
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS-PASS1_BITS-2);
-    dataptr[2] = (DCTELEM) RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865),
-                                       CONST_BITS-PASS1_BITS-1);
-    dataptr[6] = (DCTELEM) RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065),
-                                       CONST_BITS-PASS1_BITS-1);
+
+    dataptr[2] = (DCTELEM)
+      RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */
+                 CONST_BITS-PASS1_BITS-1);
+    dataptr[6] = (DCTELEM)
+      RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */
+                 CONST_BITS-PASS1_BITS-1);
 
     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
-     * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
      * i0..i3 in the paper are tmp0..tmp3 here.
      */
 
-    tmp10 = tmp0 + tmp3;
-    tmp11 = tmp1 + tmp2;
     tmp12 = tmp0 + tmp2;
     tmp13 = tmp1 + tmp3;
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /*  c3 */
+
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602);       /*  c3 */
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS-PASS1_BITS-2);
 
-    tmp0  = MULTIPLY(tmp0,    FIX_1_501321110);    /*  c1+c3-c5-c7 */
-    tmp1  = MULTIPLY(tmp1,    FIX_3_072711026);    /*  c1+c3+c5-c7 */
-    tmp2  = MULTIPLY(tmp2,    FIX_2_053119869);    /*  c1+c3-c5+c7 */
-    tmp3  = MULTIPLY(tmp3,    FIX_0_298631336);    /* -c1+c3+c5-c7 */
-    tmp10 = MULTIPLY(tmp10, - FIX_0_899976223);    /*  c7-c3 */
-    tmp11 = MULTIPLY(tmp11, - FIX_2_562915447);    /* -c1-c3 */
-    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);    /*  c5-c3 */
-    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);    /* -c3-c5 */
-
+    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);          /* -c3+c5 */
+    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);          /* -c3-c5 */
     tmp12 += z1;
     tmp13 += z1;
 
-    dataptr[1] = (DCTELEM)
-      RIGHT_SHIFT(tmp0 + tmp10 + tmp12, CONST_BITS-PASS1_BITS-1);
-    dataptr[3] = (DCTELEM)
-      RIGHT_SHIFT(tmp1 + tmp11 + tmp13, CONST_BITS-PASS1_BITS-1);
-    dataptr[5] = (DCTELEM)
-      RIGHT_SHIFT(tmp2 + tmp11 + tmp12, CONST_BITS-PASS1_BITS-1);
-    dataptr[7] = (DCTELEM)
-      RIGHT_SHIFT(tmp3 + tmp10 + tmp13, CONST_BITS-PASS1_BITS-1);
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223);       /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_1_501321110);              /*  c1+c3-c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_0_298631336);              /* -c1+c3+c5-c7 */
+    tmp0 += z1 + tmp12;
+    tmp3 += z1 + tmp13;
+
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447);       /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_3_072711026);              /*  c1+c3+c5-c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_2_053119869);              /*  c1+c3-c5+c7 */
+    tmp1 += z1 + tmp13;
+    tmp2 += z1 + tmp12;
+
+    dataptr[1] = (DCTELEM) RIGHT_SHIFT(tmp0, CONST_BITS-PASS1_BITS-1);
+    dataptr[3] = (DCTELEM) RIGHT_SHIFT(tmp1, CONST_BITS-PASS1_BITS-1);
+    dataptr[5] = (DCTELEM) RIGHT_SHIFT(tmp2, CONST_BITS-PASS1_BITS-1);
+    dataptr[7] = (DCTELEM) RIGHT_SHIFT(tmp3, CONST_BITS-PASS1_BITS-1);
 
     dataptr += DCTSIZE;                /* advance pointer to next row */
   }
@@ -3080,7 +3115,8 @@ jpeg_fdct_8x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pass 2: process columns.
    * We remove the PASS1_BITS scaling, but leave the results scaled up
    * by an overall factor of 8.
-   * 4-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
+   * 4-point FDCT kernel,
+   * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT].
    */
 
   dataptr = data;
@@ -3099,16 +3135,16 @@ jpeg_fdct_8x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     /* Odd part */
 
-    tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100);   /* c6 */
+    tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100);       /* c6 */
     /* Add fudge factor here for final descale. */
     tmp0 += ONE << (CONST_BITS+PASS1_BITS-1);
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */
-                  CONST_BITS+PASS1_BITS);
+                 CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*3] = (DCTELEM)
       RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */
-                  CONST_BITS+PASS1_BITS);
+                 CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -3134,12 +3170,13 @@ jpeg_fdct_6x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* We scale the results further by 2 as part of output adaption */
-  /* scaling for different DCT size. */
-  /* 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * We scale the results further by 2 as part of output adaption
+   * scaling for different DCT size.
+   * 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 3; ctr++) {
@@ -3158,20 +3195,20 @@ jpeg_fdct_6x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[4]);
     tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[3]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 - 6 * CENTERJSAMPLE) << (PASS1_BITS+1));
     dataptr[2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp12, FIX(1.224744871)),                 /* c2 */
-              CONST_BITS-PASS1_BITS-1);
+             CONST_BITS-PASS1_BITS-1);
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(0.707106781)), /* c4 */
-              CONST_BITS-PASS1_BITS-1);
+             CONST_BITS-PASS1_BITS-1);
 
     /* Odd part */
 
     tmp10 = DESCALE(MULTIPLY(tmp0 + tmp2, FIX(0.366025404)),     /* c5 */
-                    CONST_BITS-PASS1_BITS-1);
+                   CONST_BITS-PASS1_BITS-1);
 
     dataptr[1] = (DCTELEM) (tmp10 + ((tmp0 + tmp1) << (PASS1_BITS+1)));
     dataptr[3] = (DCTELEM) ((tmp0 - tmp1 - tmp2) << (PASS1_BITS+1));
@@ -3199,16 +3236,16 @@ jpeg_fdct_6x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 + tmp1, FIX(1.777777778)),        /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(1.257078722)), /* c2 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     /* Odd part */
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(MULTIPLY(tmp2, FIX(2.177324216)),               /* c1 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -3234,12 +3271,13 @@ jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* We must also scale the output by (8/4)*(8/2) = 2**3, which we add here. */
-  /* 4-point FDCT kernel, */
-  /* cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT]. */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * We must also scale the output by (8/4)*(8/2) = 2**3, which we add here.
+   * 4-point FDCT kernel,
+   * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT].
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 2; ctr++) {
@@ -3253,7 +3291,7 @@ jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[3]);
     tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[2]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp0 + tmp1 - 4 * CENTERJSAMPLE) << (PASS1_BITS+3));
     dataptr[2] = (DCTELEM) ((tmp0 - tmp1) << (PASS1_BITS+3));
@@ -3266,10 +3304,10 @@ jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[1] = (DCTELEM)
       RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */
-                  CONST_BITS-PASS1_BITS-3);
+                 CONST_BITS-PASS1_BITS-3);
     dataptr[3] = (DCTELEM)
       RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */
-                  CONST_BITS-PASS1_BITS-3);
+                 CONST_BITS-PASS1_BITS-3);
 
     dataptr += DCTSIZE;                /* advance pointer to next row */
   }
@@ -3307,7 +3345,7 @@ jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 GLOBAL(void)
 jpeg_fdct_2x1 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 {
-  INT32 tmp0, tmp1;
+  DCTELEM tmp0, tmp1;
   JSAMPROW elemptr;
 
   /* Pre-zero output coefficient block. */
@@ -3323,11 +3361,13 @@ jpeg_fdct_2x1 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
    */
 
   /* Even part */
-  /* Apply unsigned->signed conversion */
-  data[0] = (DCTELEM) ((tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 5);
+
+  /* Apply unsigned->signed conversion. */
+  data[0] = (tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 5;
 
   /* Odd part */
-  data[1] = (DCTELEM) ((tmp0 - tmp1) << 5);
+
+  data[1] = (tmp0 - tmp1) << 5;
 }
 
 
@@ -3350,9 +3390,11 @@ jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   int ctr;
   SHIFT_TEMPS
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -3360,7 +3402,7 @@ jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     elemptr = sample_data[ctr] + start_col;
 
     /* Even part per LL&M figure 1 --- note that published figure is faulty;
-     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+     * rotator "c1" should be "c6".
      */
 
     tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[7]);
@@ -3378,49 +3420,53 @@ jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[5]);
     tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM) ((tmp10 + tmp11 - 8 * CENTERJSAMPLE) << PASS1_BITS);
     dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);
 
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
-    dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, FIX_0_765366865),
-                                   CONST_BITS-PASS1_BITS);
-    dataptr[6] = (DCTELEM) DESCALE(z1 - MULTIPLY(tmp13, FIX_1_847759065),
-                                   CONST_BITS-PASS1_BITS);
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);   /* c6 */
+    dataptr[2] = (DCTELEM)
+      DESCALE(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */
+             CONST_BITS-PASS1_BITS);
+    dataptr[6] = (DCTELEM)
+      DESCALE(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */
+             CONST_BITS-PASS1_BITS);
 
     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
-     * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
      * i0..i3 in the paper are tmp0..tmp3 here.
      */
 
-    tmp10 = tmp0 + tmp3;
-    tmp11 = tmp1 + tmp2;
     tmp12 = tmp0 + tmp2;
     tmp13 = tmp1 + tmp3;
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /*  c3 */
-
-    tmp0  = MULTIPLY(tmp0,    FIX_1_501321110);    /*  c1+c3-c5-c7 */
-    tmp1  = MULTIPLY(tmp1,    FIX_3_072711026);    /*  c1+c3+c5-c7 */
-    tmp2  = MULTIPLY(tmp2,    FIX_2_053119869);    /*  c1+c3-c5+c7 */
-    tmp3  = MULTIPLY(tmp3,    FIX_0_298631336);    /* -c1+c3+c5-c7 */
-    tmp10 = MULTIPLY(tmp10, - FIX_0_899976223);    /*  c7-c3 */
-    tmp11 = MULTIPLY(tmp11, - FIX_2_562915447);    /* -c1-c3 */
-    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);    /*  c5-c3 */
-    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);    /* -c3-c5 */
 
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602);   /*  c3 */
+    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);      /* -c3+c5 */
+    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);      /* -c3-c5 */
     tmp12 += z1;
     tmp13 += z1;
 
-    dataptr[1] = (DCTELEM) DESCALE(tmp0 + tmp10 + tmp12, CONST_BITS-PASS1_BITS);
-    dataptr[3] = (DCTELEM) DESCALE(tmp1 + tmp11 + tmp13, CONST_BITS-PASS1_BITS);
-    dataptr[5] = (DCTELEM) DESCALE(tmp2 + tmp11 + tmp12, CONST_BITS-PASS1_BITS);
-    dataptr[7] = (DCTELEM) DESCALE(tmp3 + tmp10 + tmp13, CONST_BITS-PASS1_BITS);
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223);   /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_1_501321110);          /*  c1+c3-c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_0_298631336);          /* -c1+c3+c5-c7 */
+    tmp0 += z1 + tmp12;
+    tmp3 += z1 + tmp13;
+
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447);   /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_3_072711026);          /*  c1+c3+c5-c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_2_053119869);          /*  c1+c3-c5+c7 */
+    tmp1 += z1 + tmp13;
+    tmp2 += z1 + tmp12;
+
+    dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS-PASS1_BITS);
+    dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS-PASS1_BITS);
+    dataptr[5] = (DCTELEM) DESCALE(tmp2, CONST_BITS-PASS1_BITS);
+    dataptr[7] = (DCTELEM) DESCALE(tmp3, CONST_BITS-PASS1_BITS);
 
     ctr++;
 
     if (ctr != DCTSIZE) {
       if (ctr == DCTSIZE * 2)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -3469,44 +3515,44 @@ jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
       DESCALE(tmp10 + tmp11 + tmp12 + tmp13, PASS1_BITS+1);
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */
-              MULTIPLY(tmp11 - tmp12, FIX_0_541196100),   /* c12[16] = c6[8] */
-              CONST_BITS+PASS1_BITS+1);
+             MULTIPLY(tmp11 - tmp12, FIX_0_541196100),   /* c12[16] = c6[8] */
+             CONST_BITS+PASS1_BITS+1);
 
     tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) +   /* c14[16] = c7[8] */
-            MULTIPLY(tmp14 - tmp16, FIX(1.387039845));    /* c2[16] = c1[8] */
+           MULTIPLY(tmp14 - tmp16, FIX(1.387039845));    /* c2[16] = c1[8] */
 
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp15, FIX(1.451774982))   /* c6+c14 */
-              + MULTIPLY(tmp16, FIX(2.172734804)),        /* c2+c10 */
-              CONST_BITS+PASS1_BITS+1);
+             + MULTIPLY(tmp16, FIX(2.172734804)),        /* c2+c10 */
+             CONST_BITS+PASS1_BITS+1);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp14, FIX(0.211164243))   /* c2-c6 */
-              - MULTIPLY(tmp17, FIX(1.061594338)),        /* c10+c14 */
-              CONST_BITS+PASS1_BITS+1);
+             - MULTIPLY(tmp17, FIX(1.061594338)),        /* c10+c14 */
+             CONST_BITS+PASS1_BITS+1);
 
     /* Odd part */
 
     tmp11 = MULTIPLY(tmp0 + tmp1, FIX(1.353318001)) +         /* c3 */
-            MULTIPLY(tmp6 - tmp7, FIX(0.410524528));          /* c13 */
+           MULTIPLY(tmp6 - tmp7, FIX(0.410524528));          /* c13 */
     tmp12 = MULTIPLY(tmp0 + tmp2, FIX(1.247225013)) +         /* c5 */
-            MULTIPLY(tmp5 + tmp7, FIX(0.666655658));          /* c11 */
+           MULTIPLY(tmp5 + tmp7, FIX(0.666655658));          /* c11 */
     tmp13 = MULTIPLY(tmp0 + tmp3, FIX(1.093201867)) +         /* c7 */
-            MULTIPLY(tmp4 - tmp7, FIX(0.897167586));          /* c9 */
+           MULTIPLY(tmp4 - tmp7, FIX(0.897167586));          /* c9 */
     tmp14 = MULTIPLY(tmp1 + tmp2, FIX(0.138617169)) +         /* c15 */
-            MULTIPLY(tmp6 - tmp5, FIX(1.407403738));          /* c1 */
+           MULTIPLY(tmp6 - tmp5, FIX(1.407403738));          /* c1 */
     tmp15 = MULTIPLY(tmp1 + tmp3, - FIX(0.666655658)) +       /* -c11 */
-            MULTIPLY(tmp4 + tmp6, - FIX(1.247225013));        /* -c5 */
+           MULTIPLY(tmp4 + tmp6, - FIX(1.247225013));        /* -c5 */
     tmp16 = MULTIPLY(tmp2 + tmp3, - FIX(1.353318001)) +       /* -c3 */
-            MULTIPLY(tmp5 - tmp4, FIX(0.410524528));          /* c13 */
+           MULTIPLY(tmp5 - tmp4, FIX(0.410524528));          /* c13 */
     tmp10 = tmp11 + tmp12 + tmp13 -
-            MULTIPLY(tmp0, FIX(2.286341144)) +                /* c7+c5+c3-c1 */
-            MULTIPLY(tmp7, FIX(0.779653625));                 /* c15+c13-c11+c9 */
+           MULTIPLY(tmp0, FIX(2.286341144)) +                /* c7+c5+c3-c1 */
+           MULTIPLY(tmp7, FIX(0.779653625));                 /* c15+c13-c11+c9 */
     tmp11 += tmp14 + tmp15 + MULTIPLY(tmp1, FIX(0.071888074)) /* c9-c3-c15+c11 */
-             - MULTIPLY(tmp6, FIX(1.663905119));              /* c7+c13+c1-c5 */
+            - MULTIPLY(tmp6, FIX(1.663905119));              /* c7+c13+c1-c5 */
     tmp12 += tmp14 + tmp16 - MULTIPLY(tmp2, FIX(1.125726048)) /* c7+c5+c15-c3 */
-             + MULTIPLY(tmp5, FIX(1.227391138));              /* c9-c11+c1-c13 */
+            + MULTIPLY(tmp5, FIX(1.227391138));              /* c9-c11+c1-c13 */
     tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */
-             + MULTIPLY(tmp4, FIX(2.167985692));              /* c1+c13+c5-c9 */
+            + MULTIPLY(tmp4, FIX(2.167985692));              /* c1+c13+c5-c9 */
 
     dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+PASS1_BITS+1);
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+PASS1_BITS+1);
@@ -3541,10 +3587,11 @@ jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* 7-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/14). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * 7-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/14).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -3563,7 +3610,7 @@ jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp12 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[4]);
 
     z1 = tmp0 + tmp2;
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((z1 + tmp1 + tmp3 - 7 * CENTERJSAMPLE) << PASS1_BITS);
     tmp3 += tmp3;
@@ -3577,7 +3624,7 @@ jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     z2 = MULTIPLY(tmp0 - tmp1, FIX(0.881747734));       /* c4 */
     dataptr[4] = (DCTELEM)
       DESCALE(z2 + z3 - MULTIPLY(tmp1 - tmp3, FIX(0.707106781)), /* c2+c6-c4 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
     dataptr[6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS-PASS1_BITS);
 
     /* Odd part */
@@ -3600,7 +3647,7 @@ jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     if (ctr != DCTSIZE) {
       if (ctr == 14)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -3644,25 +3691,25 @@ jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12 + tmp13,
-                       FIX(0.653061224)),                 /* 32/49 */
-              CONST_BITS+PASS1_BITS);
+                      FIX(0.653061224)),                 /* 32/49 */
+             CONST_BITS+PASS1_BITS);
     tmp13 += tmp13;
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp13, FIX(0.832106052)) + /* c4 */
-              MULTIPLY(tmp11 - tmp13, FIX(0.205513223)) - /* c12 */
-              MULTIPLY(tmp12 - tmp13, FIX(0.575835255)),  /* c8 */
-              CONST_BITS+PASS1_BITS);
+             MULTIPLY(tmp11 - tmp13, FIX(0.205513223)) - /* c12 */
+             MULTIPLY(tmp12 - tmp13, FIX(0.575835255)),  /* c8 */
+             CONST_BITS+PASS1_BITS);
 
     tmp10 = MULTIPLY(tmp14 + tmp15, FIX(0.722074570));    /* c6 */
 
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp14, FIX(0.178337691))   /* c2-c6 */
-              + MULTIPLY(tmp16, FIX(0.400721155)),        /* c10 */
-              CONST_BITS+PASS1_BITS);
+             + MULTIPLY(tmp16, FIX(0.400721155)),        /* c10 */
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.122795725))   /* c6+c10 */
-              - MULTIPLY(tmp16, FIX(0.900412262)),        /* c2 */
-              CONST_BITS+PASS1_BITS);
+             - MULTIPLY(tmp16, FIX(0.900412262)),        /* c2 */
+             CONST_BITS+PASS1_BITS);
 
     /* Odd part */
 
@@ -3670,29 +3717,29 @@ jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp11 = tmp5 - tmp4;
     dataptr[DCTSIZE*7] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 - tmp10 + tmp3 - tmp11 - tmp6,
-                       FIX(0.653061224)),                 /* 32/49 */
-              CONST_BITS+PASS1_BITS);
+                      FIX(0.653061224)),                 /* 32/49 */
+             CONST_BITS+PASS1_BITS);
     tmp3  = MULTIPLY(tmp3 , FIX(0.653061224));            /* 32/49 */
     tmp10 = MULTIPLY(tmp10, - FIX(0.103406812));          /* -c13 */
     tmp11 = MULTIPLY(tmp11, FIX(0.917760839));            /* c1 */
     tmp10 += tmp11 - tmp3;
     tmp11 = MULTIPLY(tmp0 + tmp2, FIX(0.782007410)) +     /* c5 */
-            MULTIPLY(tmp4 + tmp6, FIX(0.491367823));      /* c9 */
+           MULTIPLY(tmp4 + tmp6, FIX(0.491367823));      /* c9 */
     dataptr[DCTSIZE*5] = (DCTELEM)
       DESCALE(tmp10 + tmp11 - MULTIPLY(tmp2, FIX(1.550341076)) /* c3+c5-c13 */
-              + MULTIPLY(tmp4, FIX(0.731428202)),         /* c1+c11-c9 */
-              CONST_BITS+PASS1_BITS);
+             + MULTIPLY(tmp4, FIX(0.731428202)),         /* c1+c11-c9 */
+             CONST_BITS+PASS1_BITS);
     tmp12 = MULTIPLY(tmp0 + tmp1, FIX(0.871740478)) +     /* c3 */
-            MULTIPLY(tmp5 - tmp6, FIX(0.305035186));      /* c11 */
+           MULTIPLY(tmp5 - tmp6, FIX(0.305035186));      /* c11 */
     dataptr[DCTSIZE*3] = (DCTELEM)
       DESCALE(tmp10 + tmp12 - MULTIPLY(tmp1, FIX(0.276965844)) /* c3-c9-c13 */
-              - MULTIPLY(tmp5, FIX(2.004803435)),         /* c1+c5+c11 */
-              CONST_BITS+PASS1_BITS);
+             - MULTIPLY(tmp5, FIX(2.004803435)),         /* c1+c5+c11 */
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(tmp11 + tmp12 + tmp3
-              - MULTIPLY(tmp0, FIX(0.735987049))          /* c3+c5-c1 */
-              - MULTIPLY(tmp6, FIX(0.082925825)),         /* c9-c11-c13 */
-              CONST_BITS+PASS1_BITS);
+             - MULTIPLY(tmp0, FIX(0.735987049))          /* c3+c5-c1 */
+             - MULTIPLY(tmp6, FIX(0.082925825)),         /* c9-c11-c13 */
+             CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
     wsptr++;                   /* advance pointer to next column */
@@ -3721,10 +3768,11 @@ jpeg_fdct_6x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -3744,20 +3792,20 @@ jpeg_fdct_6x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[4]);
     tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[3]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp11 - 6 * CENTERJSAMPLE) << PASS1_BITS);
     dataptr[2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp12, FIX(1.224744871)),                 /* c2 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
     dataptr[4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(0.707106781)), /* c4 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
 
     /* Odd part */
 
     tmp10 = DESCALE(MULTIPLY(tmp0 + tmp2, FIX(0.366025404)),     /* c5 */
-                    CONST_BITS-PASS1_BITS);
+                   CONST_BITS-PASS1_BITS);
 
     dataptr[1] = (DCTELEM) (tmp10 + ((tmp0 + tmp1) << PASS1_BITS));
     dataptr[3] = (DCTELEM) ((tmp0 - tmp1 - tmp2) << PASS1_BITS);
@@ -3767,7 +3815,7 @@ jpeg_fdct_6x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     if (ctr != DCTSIZE) {
       if (ctr == 12)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -3809,17 +3857,17 @@ jpeg_fdct_6x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12, FIX(0.888888889)), /* 8/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(MULTIPLY(tmp13 - tmp14 - tmp15, FIX(0.888888889)), /* 8/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.088662108)),         /* c4 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp14 - tmp15, FIX(0.888888889)) +        /* 8/9 */
-              MULTIPLY(tmp13 + tmp15, FIX(1.214244803)),         /* c2 */
-              CONST_BITS+PASS1_BITS);
+             MULTIPLY(tmp13 + tmp15, FIX(1.214244803)),         /* c2 */
+             CONST_BITS+PASS1_BITS);
 
     /* Odd part */
 
@@ -3829,14 +3877,14 @@ jpeg_fdct_6x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp12 = MULTIPLY(tmp0 + tmp2, FIX(0.997307603));   /* c5 */
     tmp13 = MULTIPLY(tmp0 + tmp3, FIX(0.765261039));   /* c7 */
     tmp10 = tmp12 + tmp13 + tmp14 - MULTIPLY(tmp0, FIX(0.516244403)) /* c5+c7-c1 */
-            + MULTIPLY(tmp5, FIX(0.164081699));        /* c11 */
+           + MULTIPLY(tmp5, FIX(0.164081699));        /* c11 */
     tmp11 = MULTIPLY(tmp2 + tmp3, - FIX(0.164081699)); /* -c11 */
     tmp12 += tmp11 - tmp15 - MULTIPLY(tmp2, FIX(2.079550144)) /* c1+c5-c11 */
-            + MULTIPLY(tmp5, FIX(0.765261039));        /* c7 */
+           + MULTIPLY(tmp5, FIX(0.765261039));        /* c7 */
     tmp13 += tmp11 - tmp14 + MULTIPLY(tmp3, FIX(0.645144899)) /* c1+c11-c7 */
-            - MULTIPLY(tmp5, FIX(0.997307603));        /* c5 */
+           - MULTIPLY(tmp5, FIX(0.997307603));        /* c5 */
     tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.161389302)) /* c3 */
-            - MULTIPLY(tmp2 + tmp5, FIX(0.481063200)); /* c9 */
+           - MULTIPLY(tmp2 + tmp5, FIX(0.481063200)); /* c9 */
 
     dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+PASS1_BITS);
@@ -3870,10 +3918,11 @@ jpeg_fdct_5x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* 5-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/10). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * 5-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/10).
+   */
 
   dataptr = data;
   ctr = 0;
@@ -3892,7 +3941,7 @@ jpeg_fdct_5x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp0 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[4]);
     tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[3]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp10 + tmp2 - 5 * CENTERJSAMPLE) << PASS1_BITS);
     tmp11 = MULTIPLY(tmp11, FIX(0.790569415));          /* (c2+c4)/2 */
@@ -3907,16 +3956,16 @@ jpeg_fdct_5x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[1] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp0, FIX(0.513743148)), /* c1-c3 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
     dataptr[3] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.176250899)), /* c1+c3 */
-              CONST_BITS-PASS1_BITS);
+             CONST_BITS-PASS1_BITS);
 
     ctr++;
 
     if (ctr != DCTSIZE) {
       if (ctr == 10)
-        break;                 /* Done. */
+       break;                  /* Done. */
       dataptr += DCTSIZE;      /* advance pointer to next row */
     } else
       dataptr = workspace;     /* switch pointer to extended workspace */
@@ -3954,19 +4003,19 @@ jpeg_fdct_5x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp11 + tmp12, FIX(1.28)), /* 32/25 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     tmp12 += tmp12;
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.464477191)) - /* c4 */
-              MULTIPLY(tmp11 - tmp12, FIX(0.559380511)),  /* c8 */
-              CONST_BITS+PASS1_BITS);
+             MULTIPLY(tmp11 - tmp12, FIX(0.559380511)),  /* c8 */
+             CONST_BITS+PASS1_BITS);
     tmp10 = MULTIPLY(tmp13 + tmp14, FIX(1.064004961));    /* c6 */
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp13, FIX(0.657591230)),  /* c2-c6 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*6] = (DCTELEM)
       DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.785601151)),  /* c2+c6 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     /* Odd part */
 
@@ -3974,18 +4023,18 @@ jpeg_fdct_5x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp11 = tmp1 - tmp3;
     dataptr[DCTSIZE*5] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp11 - tmp2, FIX(1.28)),  /* 32/25 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     tmp2 = MULTIPLY(tmp2, FIX(1.28));                     /* 32/25 */
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0, FIX(1.787906876)) +          /* c1 */
-              MULTIPLY(tmp1, FIX(1.612894094)) + tmp2 +   /* c3 */
-              MULTIPLY(tmp3, FIX(0.821810588)) +          /* c7 */
-              MULTIPLY(tmp4, FIX(0.283176630)),           /* c9 */
-              CONST_BITS+PASS1_BITS);
+             MULTIPLY(tmp1, FIX(1.612894094)) + tmp2 +   /* c3 */
+             MULTIPLY(tmp3, FIX(0.821810588)) +          /* c7 */
+             MULTIPLY(tmp4, FIX(0.283176630)),           /* c9 */
+             CONST_BITS+PASS1_BITS);
     tmp12 = MULTIPLY(tmp0 - tmp4, FIX(1.217352341)) -     /* (c3+c7)/2 */
-            MULTIPLY(tmp1 + tmp3, FIX(0.752365123));      /* (c1-c9)/2 */
+           MULTIPLY(tmp1 + tmp3, FIX(0.752365123));      /* (c1-c9)/2 */
     tmp13 = MULTIPLY(tmp10 + tmp11, FIX(0.395541753)) +   /* (c3-c7)/2 */
-            MULTIPLY(tmp11, FIX(0.64)) - tmp2;            /* 16/25 */
+           MULTIPLY(tmp11, FIX(0.64)) - tmp2;            /* 16/25 */
     dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS+PASS1_BITS);
 
@@ -4015,11 +4064,13 @@ jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* We must also scale the output by 8/4 = 2, which we add here. */
-  /* 4-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * We must also scale the output by 8/4 = 2, which we add here.
+   * 4-point FDCT kernel,
+   * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT].
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < DCTSIZE; ctr++) {
@@ -4033,7 +4084,7 @@ jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[3]);
     tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[2]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp0 + tmp1 - 4 * CENTERJSAMPLE) << (PASS1_BITS+1));
     dataptr[2] = (DCTELEM) ((tmp0 - tmp1) << (PASS1_BITS+1));
@@ -4046,10 +4097,10 @@ jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[1] = (DCTELEM)
       RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */
-                  CONST_BITS-PASS1_BITS-1);
+                 CONST_BITS-PASS1_BITS-1);
     dataptr[3] = (DCTELEM)
       RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */
-                  CONST_BITS-PASS1_BITS-1);
+                 CONST_BITS-PASS1_BITS-1);
 
     dataptr += DCTSIZE;                /* advance pointer to next row */
   }
@@ -4057,12 +4108,13 @@ jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pass 2: process columns.
    * We remove the PASS1_BITS scaling, but leave the results scaled up
    * by an overall factor of 8.
+   * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
    */
 
   dataptr = data;
   for (ctr = 0; ctr < 4; ctr++) {
     /* Even part per LL&M figure 1 --- note that published figure is faulty;
-     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+     * rotator "c1" should be "c6".
      */
 
     tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
@@ -4084,47 +4136,49 @@ jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp10 + tmp11, PASS1_BITS);
     dataptr[DCTSIZE*4] = (DCTELEM) RIGHT_SHIFT(tmp10 - tmp11, PASS1_BITS);
 
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);       /* c6 */
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS+PASS1_BITS-1);
+
     dataptr[DCTSIZE*2] = (DCTELEM)
-      RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), CONST_BITS+PASS1_BITS);
+      RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */
+                 CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*6] = (DCTELEM)
-      RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), CONST_BITS+PASS1_BITS);
+      RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */
+                 CONST_BITS+PASS1_BITS);
 
     /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
-     * 8-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
      * i0..i3 in the paper are tmp0..tmp3 here.
      */
 
-    tmp10 = tmp0 + tmp3;
-    tmp11 = tmp1 + tmp2;
     tmp12 = tmp0 + tmp2;
     tmp13 = tmp1 + tmp3;
-    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /*  c3 */
+
+    z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602);       /*  c3 */
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS+PASS1_BITS-1);
 
-    tmp0  = MULTIPLY(tmp0,    FIX_1_501321110);    /*  c1+c3-c5-c7 */
-    tmp1  = MULTIPLY(tmp1,    FIX_3_072711026);    /*  c1+c3+c5-c7 */
-    tmp2  = MULTIPLY(tmp2,    FIX_2_053119869);    /*  c1+c3-c5+c7 */
-    tmp3  = MULTIPLY(tmp3,    FIX_0_298631336);    /* -c1+c3+c5-c7 */
-    tmp10 = MULTIPLY(tmp10, - FIX_0_899976223);    /*  c7-c3 */
-    tmp11 = MULTIPLY(tmp11, - FIX_2_562915447);    /* -c1-c3 */
-    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);    /*  c5-c3 */
-    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);    /* -c3-c5 */
-
+    tmp12 = MULTIPLY(tmp12, - FIX_0_390180644);          /* -c3+c5 */
+    tmp13 = MULTIPLY(tmp13, - FIX_1_961570560);          /* -c3-c5 */
     tmp12 += z1;
     tmp13 += z1;
 
-    dataptr[DCTSIZE*1] = (DCTELEM)
-      RIGHT_SHIFT(tmp0 + tmp10 + tmp12, CONST_BITS+PASS1_BITS);
-    dataptr[DCTSIZE*3] = (DCTELEM)
-      RIGHT_SHIFT(tmp1 + tmp11 + tmp13, CONST_BITS+PASS1_BITS);
-    dataptr[DCTSIZE*5] = (DCTELEM)
-      RIGHT_SHIFT(tmp2 + tmp11 + tmp12, CONST_BITS+PASS1_BITS);
-    dataptr[DCTSIZE*7] = (DCTELEM)
-      RIGHT_SHIFT(tmp3 + tmp10 + tmp13, CONST_BITS+PASS1_BITS);
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223);       /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_1_501321110);              /*  c1+c3-c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_0_298631336);              /* -c1+c3+c5-c7 */
+    tmp0 += z1 + tmp12;
+    tmp3 += z1 + tmp13;
+
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447);       /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_3_072711026);              /*  c1+c3+c5-c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_2_053119869);              /*  c1+c3-c5+c7 */
+    tmp1 += z1 + tmp13;
+    tmp2 += z1 + tmp12;
+
+    dataptr[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0, CONST_BITS+PASS1_BITS);
+    dataptr[DCTSIZE*3] = (DCTELEM) RIGHT_SHIFT(tmp1, CONST_BITS+PASS1_BITS);
+    dataptr[DCTSIZE*5] = (DCTELEM) RIGHT_SHIFT(tmp2, CONST_BITS+PASS1_BITS);
+    dataptr[DCTSIZE*7] = (DCTELEM) RIGHT_SHIFT(tmp3, CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -4150,12 +4204,13 @@ jpeg_fdct_3x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
-  /* furthermore, we scale the results by 2**PASS1_BITS. */
-  /* We scale the results further by 2 as part of output adaption */
-  /* scaling for different DCT size. */
-  /* 3-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/6). */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT;
+   * furthermore, we scale the results by 2**PASS1_BITS.
+   * We scale the results further by 2 as part of output adaption
+   * scaling for different DCT size.
+   * 3-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/6).
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 6; ctr++) {
@@ -4168,18 +4223,18 @@ jpeg_fdct_3x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     tmp2 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[2]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM)
       ((tmp0 + tmp1 - 3 * CENTERJSAMPLE) << (PASS1_BITS+1));
     dataptr[2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(0.707106781)), /* c2 */
-              CONST_BITS-PASS1_BITS-1);
+             CONST_BITS-PASS1_BITS-1);
 
     /* Odd part */
 
     dataptr[1] = (DCTELEM)
       DESCALE(MULTIPLY(tmp2, FIX(1.224744871)),               /* c1 */
-              CONST_BITS-PASS1_BITS-1);
+             CONST_BITS-PASS1_BITS-1);
 
     dataptr += DCTSIZE;                /* advance pointer to next row */
   }
@@ -4209,13 +4264,13 @@ jpeg_fdct_3x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*0] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 + tmp11, FIX(1.777777778)),         /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*2] = (DCTELEM)
       DESCALE(MULTIPLY(tmp12, FIX(2.177324216)),                 /* c2 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*4] = (DCTELEM)
       DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(1.257078722)), /* c4 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     /* Odd part */
 
@@ -4223,13 +4278,13 @@ jpeg_fdct_3x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp0 + tmp1, FIX(1.777777778)),   /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*3] = (DCTELEM)
       DESCALE(MULTIPLY(tmp0 - tmp1 - tmp2, FIX(1.777777778)),    /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
     dataptr[DCTSIZE*5] = (DCTELEM)
       DESCALE(tmp10 + MULTIPLY(tmp2 - tmp1, FIX(1.777777778)),   /* 16/9 */
-              CONST_BITS+PASS1_BITS);
+             CONST_BITS+PASS1_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -4255,9 +4310,10 @@ jpeg_fdct_2x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  /* Pass 1: process rows. */
-  /* Note results are scaled up by sqrt(8) compared to a true DCT. */
-  /* We must also scale the output by (8/2)*(8/4) = 2**3, which we add here. */
+  /* Pass 1: process rows.
+   * Note results are scaled up by sqrt(8) compared to a true DCT.
+   * We must also scale the output by (8/2)*(8/4) = 2**3, which we add here.
+   */
 
   dataptr = data;
   for (ctr = 0; ctr < 4; ctr++) {
@@ -4268,7 +4324,7 @@ jpeg_fdct_2x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
     tmp0 = GETJSAMPLE(elemptr[0]);
     tmp1 = GETJSAMPLE(elemptr[1]);
 
-    /* Apply unsigned->signed conversion */
+    /* Apply unsigned->signed conversion. */
     dataptr[0] = (DCTELEM) ((tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 3);
 
     /* Odd part */
@@ -4305,10 +4361,10 @@ jpeg_fdct_2x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 
     dataptr[DCTSIZE*1] = (DCTELEM)
       RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */
-                  CONST_BITS);
+                 CONST_BITS);
     dataptr[DCTSIZE*3] = (DCTELEM)
       RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */
-                  CONST_BITS);
+                 CONST_BITS);
 
     dataptr++;                 /* advance pointer to next column */
   }
@@ -4324,24 +4380,29 @@ jpeg_fdct_2x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 GLOBAL(void)
 jpeg_fdct_1x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
 {
-  INT32 tmp0, tmp1;
+  DCTELEM tmp0, tmp1;
 
   /* Pre-zero output coefficient block. */
   MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
 
-  tmp0 = GETJSAMPLE(sample_data[0][start_col]);
-  tmp1 = GETJSAMPLE(sample_data[1][start_col]);
+  /* Pass 1: empty. */
 
-  /* We leave the results scaled up by an overall factor of 8.
+  /* Pass 2: process columns.
+   * We leave the results scaled up by an overall factor of 8.
    * We must also scale the output by (8/1)*(8/2) = 2**5.
    */
 
   /* Even part */
-  /* Apply unsigned->signed conversion */
-  data[DCTSIZE*0] = (DCTELEM) ((tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 5);
+
+  tmp0 = GETJSAMPLE(sample_data[0][start_col]);
+  tmp1 = GETJSAMPLE(sample_data[1][start_col]);
+
+  /* Apply unsigned->signed conversion. */
+  data[DCTSIZE*0] = (tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 5;
 
   /* Odd part */
-  data[DCTSIZE*1] = (DCTELEM) ((tmp0 - tmp1) << 5);
+
+  data[DCTSIZE*1] = (tmp0 - tmp1) << 5;
 }
 
 #endif /* DCT_SCALING_SUPPORTED */
index 63e9ddc..c7e832a 100644 (file)
@@ -2,7 +2,7 @@
  * jidctflt.c
  *
  * Copyright (C) 1994-1998, Thomas G. Lane.
- * Modified 2010 by Guido Vollbeding.
+ * Modified 2010-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
 
 /*
  * Perform dequantization and inverse DCT on one block of coefficients.
+ *
+ * cK represents cos(K*pi/16).
  */
 
 GLOBAL(void)
 jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                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;
@@ -77,7 +79,7 @@ jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   FLOAT_MULT_TYPE * quantptr;
   FAST_FLOAT * wsptr;
   JSAMPROW outptr;
-  JSAMPLE *range_limit = cinfo->sample_range_limit;
+  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
   int ctr;
   FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
 
@@ -97,9 +99,9 @@ jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
      */
 
     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
-        inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
-        inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
-        inptr[DCTSIZE*7] == 0) {
+       inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+       inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+       inptr[DCTSIZE*7] == 0) {
       /* AC terms all zero */
       FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
 
@@ -186,13 +188,14 @@ jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Apply signed->unsigned and prepare float->int conversion */
-    z5 = wsptr[0] + ((FAST_FLOAT) CENTERJSAMPLE + (FAST_FLOAT) 0.5);
+    /* Prepare range-limit and float->int conversion */
+    z5 = wsptr[0] + (((FAST_FLOAT) RANGE_CENTER) + ((FAST_FLOAT) 0.5));
     tmp10 = z5 + wsptr[4];
     tmp11 = z5 - wsptr[4];
 
     tmp13 = wsptr[2] + wsptr[6];
-    tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
+    tmp12 = (wsptr[2] - wsptr[6]) *
+             ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
 
     tmp0 = tmp10 + tmp13;
     tmp3 = tmp10 - tmp13;
@@ -206,27 +209,27 @@ jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     z11 = wsptr[1] + wsptr[7];
     z12 = wsptr[1] - wsptr[7];
 
-    tmp7 = z11 + z13;
-    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
+    tmp7 = z11 + z13;          /* phase 5 */
+    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
 
     z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
     tmp10 = z5 - z12 * ((FAST_FLOAT) 1.082392200); /* 2*(c2-c6) */
     tmp12 = z5 - z10 * ((FAST_FLOAT) 2.613125930); /* 2*(c2+c6) */
 
-    tmp6 = tmp12 - tmp7;
+    tmp6 = tmp12 - tmp7;       /* phase 2 */
     tmp5 = tmp11 - tmp6;
     tmp4 = tmp10 - tmp5;
 
     /* Final output stage: float->int conversion and range-limit */
 
-    outptr[0] = range_limit[((int) (tmp0 + tmp7)) & RANGE_MASK];
-    outptr[7] = range_limit[((int) (tmp0 - tmp7)) & RANGE_MASK];
-    outptr[1] = range_limit[((int) (tmp1 + tmp6)) & RANGE_MASK];
-    outptr[6] = range_limit[((int) (tmp1 - tmp6)) & RANGE_MASK];
-    outptr[2] = range_limit[((int) (tmp2 + tmp5)) & RANGE_MASK];
-    outptr[5] = range_limit[((int) (tmp2 - tmp5)) & RANGE_MASK];
-    outptr[3] = range_limit[((int) (tmp3 + tmp4)) & RANGE_MASK];
-    outptr[4] = range_limit[((int) (tmp3 - tmp4)) & RANGE_MASK];
+    outptr[0] = range_limit[(int) (tmp0 + tmp7) & RANGE_MASK];
+    outptr[7] = range_limit[(int) (tmp0 - tmp7) & RANGE_MASK];
+    outptr[1] = range_limit[(int) (tmp1 + tmp6) & RANGE_MASK];
+    outptr[6] = range_limit[(int) (tmp1 - tmp6) & RANGE_MASK];
+    outptr[2] = range_limit[(int) (tmp2 + tmp5) & RANGE_MASK];
+    outptr[5] = range_limit[(int) (tmp2 - tmp5) & RANGE_MASK];
+    outptr[3] = range_limit[(int) (tmp3 + tmp4) & RANGE_MASK];
+    outptr[4] = range_limit[(int) (tmp3 - tmp4) & RANGE_MASK];
 
     wsptr += DCTSIZE;          /* advance pointer to next row */
   }
index 4012251..474cc45 100644 (file)
@@ -2,6 +2,7 @@
  * jidctfst.c
  *
  * Copyright (C) 1994-1998, Thomas G. Lane.
+ * Modified 2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
 #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;
-#if BITS_IN_JSAMPLE == 8
-#define DCTELEMBITS  16                /* DCTELEM may be 16 or 32 bits */
-#else
-#define DCTELEMBITS  32                /* DCTELEM must be 32 bits */
-#endif
-#define IRIGHT_SHIFT(x,shft)  \
-    ((ishift_temp = (x)) < 0 ? \
-     (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(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))
+       DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
 #endif
 
 
 /*
  * Perform dequantization and inverse DCT on one block of coefficients.
+ *
+ * cK represents cos(K*pi/16).
  */
 
 GLOBAL(void)
 jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
   DCTELEM tmp10, tmp11, tmp12, tmp13;
@@ -180,7 +156,7 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   int ctr;
   int workspace[DCTSIZE2];     /* buffers data between passes */
   SHIFT_TEMPS                  /* for DESCALE */
-  ISHIFT_TEMPS                 /* for IDESCALE */
+  ISHIFT_TEMPS                 /* for IRIGHT_SHIFT */
 
   /* Pass 1: process columns from input, store into work array. */
 
@@ -196,11 +172,11 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
      * With typical images and quantization tables, half or more of the
      * column DCT calculations can be simplified this way.
      */
-
+    
     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
-        inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
-        inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
-        inptr[DCTSIZE*7] == 0) {
+       inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+       inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+       inptr[DCTSIZE*7] == 0) {
       /* AC terms all zero */
       int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
 
@@ -212,13 +188,13 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
       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]);
@@ -236,7 +212,7 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp3 = tmp10 - tmp13;
     tmp1 = tmp11 + tmp12;
     tmp2 = tmp11 - tmp12;
-
+    
     /* Odd part */
 
     tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
@@ -253,12 +229,12 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     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) */
+    tmp10 = z5 - MULTIPLY(z12, FIX_1_082392200); /* 2*(c2-c6) */
+    tmp12 = z5 - MULTIPLY(z10, FIX_2_613125930); /* 2*(c2+c6) */
 
     tmp6 = tmp12 - tmp7;       /* phase 2 */
     tmp5 = tmp11 - tmp6;
-    tmp4 = tmp10 + tmp5;
+    tmp4 = tmp10 - tmp5;
 
     wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
     wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
@@ -266,21 +242,28 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     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);
+    wsptr[DCTSIZE*3] = (int) (tmp3 + tmp4);
+    wsptr[DCTSIZE*4] = (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. */
+  
+  /* 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;
+
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z5 = (DCTELEM) wsptr[0] +
+          ((((DCTELEM) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (1 << (PASS1_BITS+2)));
+
     /* 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).
@@ -288,14 +271,14 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
      * 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] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
-        wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
+       wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
       /* AC terms all zero */
-      JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
-                                  & RANGE_MASK];
-
+      JSAMPLE dcval = range_limit[(int) IRIGHT_SHIFT(z5, PASS1_BITS+3)
+                                 & RANGE_MASK];
+      
       outptr[0] = dcval;
       outptr[1] = dcval;
       outptr[2] = dcval;
@@ -309,15 +292,15 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
       continue;
     }
 #endif
-
+    
     /* Even part */
 
-    tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
-    tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);
+    tmp10 = z5 + (DCTELEM) wsptr[4];
+    tmp11 = z5 - (DCTELEM) wsptr[4];
 
-    tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
-    tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
-            - tmp13;
+    tmp13 = (DCTELEM) wsptr[2] + (DCTELEM) wsptr[6];
+    tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6],
+                    FIX_1_414213562) - tmp13; /* 2*c4 */
 
     tmp0 = tmp10 + tmp13;
     tmp3 = tmp10 - tmp13;
@@ -335,31 +318,31 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     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) */
+    tmp10 = z5 - MULTIPLY(z12, FIX_1_082392200); /* 2*(c2-c6) */
+    tmp12 = z5 - MULTIPLY(z10, FIX_2_613125930); /* 2*(c2+c6) */
 
     tmp6 = tmp12 - tmp7;       /* phase 2 */
     tmp5 = tmp11 - tmp6;
-    tmp4 = tmp10 + tmp5;
+    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];
+    outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp7, PASS1_BITS+3)
+                           & RANGE_MASK];
+    outptr[7] = range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp7, PASS1_BITS+3)
+                           & RANGE_MASK];
+    outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp1 + tmp6, PASS1_BITS+3)
+                           & RANGE_MASK];
+    outptr[6] = range_limit[(int) IRIGHT_SHIFT(tmp1 - tmp6, PASS1_BITS+3)
+                           & RANGE_MASK];
+    outptr[2] = range_limit[(int) IRIGHT_SHIFT(tmp2 + tmp5, PASS1_BITS+3)
+                           & RANGE_MASK];
+    outptr[5] = range_limit[(int) IRIGHT_SHIFT(tmp2 - tmp5, PASS1_BITS+3)
+                           & RANGE_MASK];
+    outptr[3] = range_limit[(int) IRIGHT_SHIFT(tmp3 + tmp4, PASS1_BITS+3)
+                           & RANGE_MASK];
+    outptr[4] = range_limit[(int) IRIGHT_SHIFT(tmp3 - tmp4, PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += DCTSIZE;          /* advance pointer to next row */
   }
index 64c6f97..de233ec 100644 (file)
@@ -2,7 +2,7 @@
  * jidctint.c
  *
  * Copyright (C) 1991-1998, Thomas G. Lane.
- * Modification developed 2002-2009 by Guido Vollbeding.
+ * Modification developed 2002-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
 
 /*
  * Perform dequantization and inverse DCT on one block of coefficients.
+ *
+ * cK represents sqrt(2) * cos(K*pi/16).
  */
 
 GLOBAL(void)
 jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp3;
   INT32 tmp10, tmp11, tmp12, tmp13;
@@ -184,9 +186,10 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   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. */
+  /* 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;
@@ -202,9 +205,9 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
      */
 
     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
-        inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
-        inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
-        inptr[DCTSIZE*7] == 0) {
+       inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+       inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+       inptr[DCTSIZE*7] == 0) {
       /* AC terms all zero */
       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
 
@@ -223,15 +226,9 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
       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(z2, FIX_0_765366865);
-    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);
+    /* Even part: reverse the even part of the forward DCT.
+     * The rotator is c(-6).
+     */
 
     z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
     z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
@@ -243,6 +240,13 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp0 = z2 + z3;
     tmp1 = z2 - z3;
 
+    z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+    z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);       /* c6 */
+    tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865);     /* c2-c6 */
+    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);     /* c2+c6 */
+
     tmp10 = tmp0 + tmp2;
     tmp13 = tmp0 - tmp2;
     tmp11 = tmp1 + tmp3;
@@ -260,21 +264,21 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     z2 = tmp0 + tmp2;
     z3 = tmp1 + tmp3;
 
-    z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */
-    z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
-    z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+    z1 = MULTIPLY(z2 + z3, FIX_1_175875602);       /*  c3 */
+    z2 = MULTIPLY(z2, - FIX_1_961570560);          /* -c3-c5 */
+    z3 = MULTIPLY(z3, - FIX_0_390180644);          /* -c3+c5 */
     z2 += z1;
     z3 += z1;
 
-    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
-    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
-    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_0_298631336);        /* -c1+c3+c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_1_501321110);        /*  c1+c3-c5-c7 */
     tmp0 += z1 + z2;
     tmp3 += z1 + z3;
 
-    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
-    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
-    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_2_053119869);        /*  c1+c3-c5+c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_3_072711026);        /*  c1+c3+c5-c7 */
     tmp1 += z1 + z3;
     tmp2 += z1 + z2;
 
@@ -294,13 +298,20 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     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. */
+  /* 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;
+
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z2 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
+
     /* 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).
@@ -311,10 +322,10 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 #ifndef NO_ZERO_ROW_TEST
     if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
-        wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
+       wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
       /* AC terms all zero */
-      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
-                                  & RANGE_MASK];
+      JSAMPLE dcval = range_limit[(int) RIGHT_SHIFT(z2, PASS1_BITS+3)
+                                 & RANGE_MASK];
 
       outptr[0] = dcval;
       outptr[1] = dcval;
@@ -330,23 +341,22 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     }
 #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(z2, FIX_0_765366865);
-    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);
+    /* Even part: reverse the even part of the forward DCT.
+     * The rotator is c(-6).
+     */
 
-    /* Add fudge factor here for final descale. */
-    z2 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
     z3 = (INT32) wsptr[4];
 
     tmp0 = (z2 + z3) << CONST_BITS;
     tmp1 = (z2 - z3) << CONST_BITS;
 
+    z2 = (INT32) wsptr[2];
+    z3 = (INT32) wsptr[6];
+
+    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);       /* c6 */
+    tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865);     /* c2-c6 */
+    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);     /* c2+c6 */
+
     tmp10 = tmp0 + tmp2;
     tmp13 = tmp0 - tmp2;
     tmp11 = tmp1 + tmp3;
@@ -364,50 +374,50 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     z2 = tmp0 + tmp2;
     z3 = tmp1 + tmp3;
 
-    z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */
-    z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
-    z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+    z1 = MULTIPLY(z2 + z3, FIX_1_175875602);       /*  c3 */
+    z2 = MULTIPLY(z2, - FIX_1_961570560);          /* -c3-c5 */
+    z3 = MULTIPLY(z3, - FIX_0_390180644);          /* -c3+c5 */
     z2 += z1;
     z3 += z1;
 
-    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
-    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
-    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_0_298631336);        /* -c1+c3+c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_1_501321110);        /*  c1+c3-c5-c7 */
     tmp0 += z1 + z2;
     tmp3 += z1 + z3;
 
-    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
-    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
-    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_2_053119869);        /*  c1+c3-c5+c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_3_072711026);        /*  c1+c3+c5-c7 */
     tmp1 += z1 + z3;
     tmp2 += z1 + z2;
 
     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp3,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp3,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += DCTSIZE;          /* advance pointer to next row */
   }
@@ -426,8 +436,8 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_7x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp10, tmp11, tmp12, tmp13;
   INT32 z1, z2, z3;
@@ -502,8 +512,10 @@ jpeg_idct_7x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp13 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp13 = (INT32) wsptr[0] +
+             ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+              (ONE << (PASS1_BITS+2)));
     tmp13 <<= CONST_BITS;
 
     z1 = (INT32) wsptr[2];
@@ -539,26 +551,26 @@ jpeg_idct_7x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 7;                /* advance pointer to next row */
   }
@@ -575,8 +587,8 @@ jpeg_idct_7x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp10, tmp11, tmp12;
   INT32 z1, z2, z3;
@@ -638,8 +650,10 @@ jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = (INT32) wsptr[0] +
+            ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+             (ONE << (PASS1_BITS+2)));
     tmp0 <<= CONST_BITS;
     tmp2 = (INT32) wsptr[4];
     tmp10 = MULTIPLY(tmp2, FIX(0.707106781));   /* c4 */
@@ -663,23 +677,23 @@ jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 6;                /* advance pointer to next row */
   }
@@ -696,8 +710,8 @@ jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp10, tmp11, tmp12;
   INT32 z1, z2, z3;
@@ -757,8 +771,10 @@ jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp12 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp12 = (INT32) wsptr[0] +
+             ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+              (ONE << (PASS1_BITS+2)));
     tmp12 <<= CONST_BITS;
     tmp0 = (INT32) wsptr[2];
     tmp1 = (INT32) wsptr[4];
@@ -781,20 +797,20 @@ jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 5;                /* advance pointer to next row */
   }
@@ -811,8 +827,8 @@ jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp2, tmp10, tmp12;
   INT32 z1, z2, z3;
@@ -835,7 +851,7 @@ jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
     tmp2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
-
+    
     tmp10 = (tmp0 + tmp2) << PASS1_BITS;
     tmp12 = (tmp0 - tmp2) << PASS1_BITS;
 
@@ -849,9 +865,9 @@ jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS-PASS1_BITS-1);
     tmp0 = RIGHT_SHIFT(z1 + MULTIPLY(z2, FIX_0_765366865), /* c2-c6 */
-                       CONST_BITS-PASS1_BITS);
+                      CONST_BITS-PASS1_BITS);
     tmp2 = RIGHT_SHIFT(z1 - MULTIPLY(z3, FIX_1_847759065), /* c2+c6 */
-                       CONST_BITS-PASS1_BITS);
+                      CONST_BITS-PASS1_BITS);
 
     /* Final output stage */
 
@@ -869,8 +885,10 @@ jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = (INT32) wsptr[0] +
+            ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+             (ONE << (PASS1_BITS+2)));
     tmp2 = (INT32) wsptr[2];
 
     tmp10 = (tmp0 + tmp2) << CONST_BITS;
@@ -889,17 +907,17 @@ jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 4;                /* advance pointer to next row */
   }
@@ -916,8 +934,8 @@ jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_3x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp2, tmp10, tmp12;
   JCOEFPTR inptr;
@@ -966,8 +984,10 @@ jpeg_idct_3x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = (INT32) wsptr[0] +
+            ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+             (ONE << (PASS1_BITS+2)));
     tmp0 <<= CONST_BITS;
     tmp2 = (INT32) wsptr[2];
     tmp12 = MULTIPLY(tmp2, FIX(0.707106781)); /* c2 */
@@ -982,14 +1002,14 @@ jpeg_idct_3x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 3;                /* advance pointer to next row */
   }
@@ -1005,14 +1025,14 @@ jpeg_idct_3x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
-  INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
+  DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
   ISLOW_MULT_TYPE * quantptr;
   JSAMPROW outptr;
   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
-  SHIFT_TEMPS
+  ISHIFT_TEMPS
 
   /* Pass 1: process columns from input. */
 
@@ -1021,8 +1041,8 @@ jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Column 0 */
   tmp4 = DEQUANTIZE(coef_block[DCTSIZE*0], quantptr[DCTSIZE*0]);
   tmp5 = DEQUANTIZE(coef_block[DCTSIZE*1], quantptr[DCTSIZE*1]);
-  /* Add fudge factor here for final descale. */
-  tmp4 += ONE << 2;
+  /* Add range center and fudge factor for final descale and range-limit. */
+  tmp4 += (((DCTELEM) RANGE_CENTER) << 3) + (1 << 2);
 
   tmp0 = tmp4 + tmp5;
   tmp2 = tmp4 - tmp5;
@@ -1039,14 +1059,14 @@ jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Row 0 */
   outptr = output_buf[0] + output_col;
 
-  outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp0 + tmp1, 3) & RANGE_MASK];
-  outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp0 - tmp1, 3) & RANGE_MASK];
+  outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp1, 3) & RANGE_MASK];
+  outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp1, 3) & RANGE_MASK];
 
   /* Row 1 */
   outptr = output_buf[1] + output_col;
 
-  outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp2 + tmp3, 3) & RANGE_MASK];
-  outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp2 - tmp3, 3) & RANGE_MASK];
+  outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp2 + tmp3, 3) & RANGE_MASK];
+  outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp2 - tmp3, 3) & RANGE_MASK];
 }
 
 
@@ -1060,20 +1080,24 @@ jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
-  int dcval;
+  DCTELEM dcval;
   ISLOW_MULT_TYPE * quantptr;
   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
-  SHIFT_TEMPS
+  ISHIFT_TEMPS
 
   /* 1x1 is trivial: just take the DC coefficient divided by 8. */
+
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+
   dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
-  dcval = (int) DESCALE((INT32) dcval, 3);
+  /* Add range center and fudge factor for descale and range-limit. */
+  dcval += (((DCTELEM) RANGE_CENTER) << 3) + (1 << 2);
 
-  output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
+  output_buf[0][output_col] =
+    range_limit[(int) IRIGHT_SHIFT(dcval, 3) & RANGE_MASK];
 }
 
 
@@ -1087,8 +1111,8 @@ jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_9x9 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13, tmp14;
   INT32 z1, z2, z3, z4;
@@ -1172,8 +1196,10 @@ jpeg_idct_9x9 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = (INT32) wsptr[0] +
+            ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+             (ONE << (PASS1_BITS+2)));
     tmp0 <<= CONST_BITS;
 
     z1 = (INT32) wsptr[2];
@@ -1216,32 +1242,32 @@ jpeg_idct_9x9 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp3,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp3,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp14,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -1258,8 +1284,8 @@ jpeg_idct_9x9 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24;
@@ -1292,7 +1318,7 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp11 = z3 - z2;
 
     tmp22 = RIGHT_SHIFT(z3 - ((z1 - z2) << 1),   /* c0 = (c4-c8)*2 */
-                        CONST_BITS-PASS1_BITS);
+                       CONST_BITS-PASS1_BITS);
 
     z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
     z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
@@ -1355,8 +1381,10 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z3 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z3 <<= CONST_BITS;
     z4 = (INT32) wsptr[4];
     z1 = MULTIPLY(z4, FIX(1.144122806));         /* c4 */
@@ -1408,35 +1436,35 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -1453,8 +1481,8 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25;
@@ -1492,13 +1520,13 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     z4 -= z2;
     tmp25 = tmp10 + MULTIPLY(z4, FIX(1.356927976));  /* c2 */
     tmp21 = tmp20 + tmp23 + tmp25 -
-            MULTIPLY(z2, FIX(1.821790775));          /* c2+c4+c10-c6 */
+           MULTIPLY(z2, FIX(1.821790775));          /* c2+c4+c10-c6 */
     tmp20 += tmp25 + MULTIPLY(z3, FIX(2.115825087)); /* c4+c6 */
     tmp23 += tmp25 - MULTIPLY(z1, FIX(1.513598477)); /* c6+c8 */
     tmp24 += tmp25;
     tmp22 = tmp24 - MULTIPLY(z3, FIX(0.788749120));  /* c8+c10 */
     tmp24 += MULTIPLY(z2, FIX(1.944413522)) -        /* c2+c8 */
-             MULTIPLY(z1, FIX(1.390975730));         /* c4+c10 */
+            MULTIPLY(z1, FIX(1.390975730));         /* c4+c10 */
     tmp25 = tmp10 - MULTIPLY(z4, FIX(1.414213562));  /* c0 */
 
     /* Odd part */
@@ -1514,7 +1542,7 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp12 = MULTIPLY(z1 + z3, FIX(0.670361295));         /* c5-c9 */
     tmp13 = tmp14 + MULTIPLY(z1 + z4, FIX(0.366151574)); /* c7-c9 */
     tmp10 = tmp11 + tmp12 + tmp13 -
-            MULTIPLY(z1, FIX(0.923107866));              /* c7+c5+c3-c1-2*c9 */
+           MULTIPLY(z1, FIX(0.923107866));              /* c7+c5+c3-c1-2*c9 */
     z1    = tmp14 - MULTIPLY(z2 + z3, FIX(1.163011579)); /* c7+c9 */
     tmp11 += z1 + MULTIPLY(z2, FIX(2.073276588));        /* c1+c7+3*c9-c3 */
     tmp12 += z1 - MULTIPLY(z3, FIX(1.192193623));        /* c3+c5-c7-c9 */
@@ -1522,8 +1550,8 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp11 += z1;
     tmp13 += z1 + MULTIPLY(z4, FIX(2.102458632));        /* c1+c5+c9-c7 */
     tmp14 += MULTIPLY(z2, - FIX(1.467221301)) +          /* -(c5+c9) */
-             MULTIPLY(z3, FIX(1.001388905)) -            /* c1-c9 */
-             MULTIPLY(z4, FIX(1.684843907));             /* c3+c9 */
+            MULTIPLY(z3, FIX(1.001388905)) -            /* c1-c9 */
+            MULTIPLY(z4, FIX(1.684843907));             /* c3+c9 */
 
     /* Final output stage */
 
@@ -1548,8 +1576,10 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp10 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp10 = (INT32) wsptr[0] +
+             ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+              (ONE << (PASS1_BITS+2)));
     tmp10 <<= CONST_BITS;
 
     z1 = (INT32) wsptr[2];
@@ -1563,13 +1593,13 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     z4 -= z2;
     tmp25 = tmp10 + MULTIPLY(z4, FIX(1.356927976));  /* c2 */
     tmp21 = tmp20 + tmp23 + tmp25 -
-            MULTIPLY(z2, FIX(1.821790775));          /* c2+c4+c10-c6 */
+           MULTIPLY(z2, FIX(1.821790775));          /* c2+c4+c10-c6 */
     tmp20 += tmp25 + MULTIPLY(z3, FIX(2.115825087)); /* c4+c6 */
     tmp23 += tmp25 - MULTIPLY(z1, FIX(1.513598477)); /* c6+c8 */
     tmp24 += tmp25;
     tmp22 = tmp24 - MULTIPLY(z3, FIX(0.788749120));  /* c8+c10 */
     tmp24 += MULTIPLY(z2, FIX(1.944413522)) -        /* c2+c8 */
-             MULTIPLY(z1, FIX(1.390975730));         /* c4+c10 */
+            MULTIPLY(z1, FIX(1.390975730));         /* c4+c10 */
     tmp25 = tmp10 - MULTIPLY(z4, FIX(1.414213562));  /* c0 */
 
     /* Odd part */
@@ -1585,7 +1615,7 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp12 = MULTIPLY(z1 + z3, FIX(0.670361295));         /* c5-c9 */
     tmp13 = tmp14 + MULTIPLY(z1 + z4, FIX(0.366151574)); /* c7-c9 */
     tmp10 = tmp11 + tmp12 + tmp13 -
-            MULTIPLY(z1, FIX(0.923107866));              /* c7+c5+c3-c1-2*c9 */
+           MULTIPLY(z1, FIX(0.923107866));              /* c7+c5+c3-c1-2*c9 */
     z1    = tmp14 - MULTIPLY(z2 + z3, FIX(1.163011579)); /* c7+c9 */
     tmp11 += z1 + MULTIPLY(z2, FIX(2.073276588));        /* c1+c7+3*c9-c3 */
     tmp12 += z1 - MULTIPLY(z3, FIX(1.192193623));        /* c3+c5-c7-c9 */
@@ -1593,44 +1623,44 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp11 += z1;
     tmp13 += z1 + MULTIPLY(z4, FIX(2.102458632));        /* c1+c5+c9-c7 */
     tmp14 += MULTIPLY(z2, - FIX(1.467221301)) +          /* -(c5+c9) */
-             MULTIPLY(z3, FIX(1.001388905)) -            /* c1-c9 */
-             MULTIPLY(z4, FIX(1.684843907));             /* c3+c9 */
+            MULTIPLY(z3, FIX(1.001388905)) -            /* c1-c9 */
+            MULTIPLY(z4, FIX(1.684843907));             /* c3+c9 */
 
     /* Final output stage */
 
     outptr[0]  = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[1]  = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[9]  = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[2]  = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[8]  = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[3]  = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[7]  = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[4]  = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[6]  = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[5]  = range_limit[(int) RIGHT_SHIFT(tmp25,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -1647,8 +1677,8 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25;
@@ -1720,7 +1750,7 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp12 += tmp13 + tmp14 - MULTIPLY(z3, FIX(1.478575242)); /* c1+c5-c7-c11 */
     tmp13 += tmp15 - tmp11 + MULTIPLY(z4, FIX(1.586706681)); /* c1+c11 */
     tmp15 += tmp14 - MULTIPLY(z1, FIX(0.676326758)) -        /* c7-c11 */
-             MULTIPLY(z4, FIX(1.982889723));                 /* c5+c7 */
+            MULTIPLY(z4, FIX(1.982889723));                 /* c5+c7 */
 
     z1 -= z4;
     z2 -= z3;
@@ -1752,8 +1782,10 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z3 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z3 <<= CONST_BITS;
 
     z4 = (INT32) wsptr[4];
@@ -1801,7 +1833,7 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp12 += tmp13 + tmp14 - MULTIPLY(z3, FIX(1.478575242)); /* c1+c5-c7-c11 */
     tmp13 += tmp15 - tmp11 + MULTIPLY(z4, FIX(1.586706681)); /* c1+c11 */
     tmp15 += tmp14 - MULTIPLY(z1, FIX(0.676326758)) -        /* c7-c11 */
-             MULTIPLY(z4, FIX(1.982889723));                 /* c5+c7 */
+            MULTIPLY(z4, FIX(1.982889723));                 /* c5+c7 */
 
     z1 -= z4;
     z2 -= z3;
@@ -1812,41 +1844,41 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0]  = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[1]  = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[2]  = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[9]  = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[3]  = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[8]  = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[4]  = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[7]  = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[5]  = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[6]  = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -1863,8 +1895,8 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26;
@@ -1930,7 +1962,7 @@ jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp15 = z1 + z4;
     tmp13 = MULTIPLY(tmp15, FIX(0.937797057));       /* c7 */
     tmp10 = tmp11 + tmp12 + tmp13 -
-            MULTIPLY(z1, FIX(2.020082300));          /* c7+c5+c3-c1 */
+           MULTIPLY(z1, FIX(2.020082300));          /* c7+c5+c3-c1 */
     tmp14 = MULTIPLY(z2 + z3, - FIX(0.338443458));   /* -c11 */
     tmp11 += tmp14 + MULTIPLY(z2, FIX(0.837223564)); /* c5+c9+c11-c3 */
     tmp12 += tmp14 - MULTIPLY(z3, FIX(1.572116027)); /* c1+c5-c9-c11 */
@@ -1942,11 +1974,11 @@ jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp13 += tmp14;
     tmp15 = MULTIPLY(tmp15, FIX(0.338443458));       /* c11 */
     tmp14 = tmp15 + MULTIPLY(z1, FIX(0.318774355)) - /* c9-c11 */
-            MULTIPLY(z2, FIX(0.466105296));          /* c1-c7 */
+           MULTIPLY(z2, FIX(0.466105296));          /* c1-c7 */
     z1    = MULTIPLY(z3 - z2, FIX(0.937797057));     /* c7 */
     tmp14 += z1;
     tmp15 += z1 + MULTIPLY(z3, FIX(0.384515595)) -   /* c3-c7 */
-             MULTIPLY(z4, FIX(1.742345811));         /* c1+c11 */
+            MULTIPLY(z4, FIX(1.742345811));         /* c1+c11 */
 
     /* Final output stage */
 
@@ -1973,8 +2005,10 @@ jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z1 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z1 <<= CONST_BITS;
 
     z2 = (INT32) wsptr[2];
@@ -2016,7 +2050,7 @@ jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp15 = z1 + z4;
     tmp13 = MULTIPLY(tmp15, FIX(0.937797057));       /* c7 */
     tmp10 = tmp11 + tmp12 + tmp13 -
-            MULTIPLY(z1, FIX(2.020082300));          /* c7+c5+c3-c1 */
+           MULTIPLY(z1, FIX(2.020082300));          /* c7+c5+c3-c1 */
     tmp14 = MULTIPLY(z2 + z3, - FIX(0.338443458));   /* -c11 */
     tmp11 += tmp14 + MULTIPLY(z2, FIX(0.837223564)); /* c5+c9+c11-c3 */
     tmp12 += tmp14 - MULTIPLY(z3, FIX(1.572116027)); /* c1+c5-c9-c11 */
@@ -2028,53 +2062,53 @@ jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp13 += tmp14;
     tmp15 = MULTIPLY(tmp15, FIX(0.338443458));       /* c11 */
     tmp14 = tmp15 + MULTIPLY(z1, FIX(0.318774355)) - /* c9-c11 */
-            MULTIPLY(z2, FIX(0.466105296));          /* c1-c7 */
+           MULTIPLY(z2, FIX(0.466105296));          /* c1-c7 */
     z1    = MULTIPLY(z3 - z2, FIX(0.937797057));     /* c7 */
     tmp14 += z1;
     tmp15 += z1 + MULTIPLY(z3, FIX(0.384515595)) -   /* c3-c7 */
-             MULTIPLY(z4, FIX(1.742345811));         /* c1+c11 */
+            MULTIPLY(z4, FIX(1.742345811));         /* c1+c11 */
 
     /* Final output stage */
 
     outptr[0]  = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[1]  = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[2]  = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[3]  = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[9]  = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[4]  = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[8]  = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[5]  = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[7]  = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[6]  = range_limit[(int) RIGHT_SHIFT(tmp26,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -2091,8 +2125,8 @@ jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26;
@@ -2128,7 +2162,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp12 = z1 - z4;
 
     tmp23 = RIGHT_SHIFT(z1 - ((z2 + z3 - z4) << 1), /* c0 = (c4+c12-c8)*2 */
-                        CONST_BITS-PASS1_BITS);
+                       CONST_BITS-PASS1_BITS);
 
     z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
     z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
@@ -2138,7 +2172,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp13 = z3 + MULTIPLY(z1, FIX(0.273079590)); /* c2-c6 */
     tmp14 = z3 - MULTIPLY(z2, FIX(1.719280954)); /* c6+c10 */
     tmp15 = MULTIPLY(z1, FIX(0.613604268)) -     /* c10 */
-            MULTIPLY(z2, FIX(1.378756276));      /* c2 */
+           MULTIPLY(z2, FIX(1.378756276));      /* c2 */
 
     tmp20 = tmp10 + tmp13;
     tmp26 = tmp10 - tmp13;
@@ -2200,8 +2234,10 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z1 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z1 <<= CONST_BITS;
     z4 = (INT32) wsptr[4];
     z2 = MULTIPLY(z4, FIX(1.274162392));         /* c4 */
@@ -2222,7 +2258,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp13 = z3 + MULTIPLY(z1, FIX(0.273079590)); /* c2-c6 */
     tmp14 = z3 - MULTIPLY(z2, FIX(1.719280954)); /* c6+c10 */
     tmp15 = MULTIPLY(z1, FIX(0.613604268)) -     /* c10 */
-            MULTIPLY(z2, FIX(1.378756276));      /* c2 */
+           MULTIPLY(z2, FIX(1.378756276));      /* c2 */
 
     tmp20 = tmp10 + tmp13;
     tmp26 = tmp10 - tmp13;
@@ -2260,47 +2296,47 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0]  = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[1]  = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[2]  = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[3]  = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[4]  = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[9]  = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[5]  = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[8]  = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[6]  = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp16,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[7]  = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp16,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -2317,8 +2353,8 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27;
@@ -2432,8 +2468,10 @@ jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z1 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z1 <<= CONST_BITS;
 
     z2 = (INT32) wsptr[2];
@@ -2499,50 +2537,50 @@ jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0]  = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[14] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[1]  = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[2]  = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[3]  = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[4]  = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[5]  = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[9]  = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[6]  = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp16,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[8]  = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp16,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[7]  = range_limit[(int) RIGHT_SHIFT(tmp27,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -2559,8 +2597,8 @@ jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                 JCOEFPTR coef_block,
-                 JSAMPARRAY output_buf, JDIMENSION output_col)
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27;
@@ -2632,9 +2670,9 @@ jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp11 = MULTIPLY(tmp11,   FIX(0.666655658));   /* c11 */
     tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528));   /* c13 */
     tmp0  = tmp1 + tmp2 + tmp3 -
-            MULTIPLY(z1, FIX(2.286341144));        /* c7+c5+c3-c1 */
+           MULTIPLY(z1, FIX(2.286341144));        /* c7+c5+c3-c1 */
     tmp13 = tmp10 + tmp11 + tmp12 -
-            MULTIPLY(z1, FIX(1.835730603));        /* c9+c11+c13-c15 */
+           MULTIPLY(z1, FIX(1.835730603));        /* c9+c11+c13-c15 */
     z1    = MULTIPLY(z2 + z3, FIX(0.138617169));   /* c15 */
     tmp1  += z1 + MULTIPLY(z2, FIX(0.071888074));  /* c9+c11-c3-c15 */
     tmp2  += z1 - MULTIPLY(z3, FIX(1.125726048));  /* c5+c7+c15-c3 */
@@ -2683,8 +2721,10 @@ jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = (INT32) wsptr[0] +
+            ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+             (ONE << (PASS1_BITS+2)));
     tmp0 <<= CONST_BITS;
 
     z1 = (INT32) wsptr[4];
@@ -2732,9 +2772,9 @@ jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp11 = MULTIPLY(tmp11,   FIX(0.666655658));   /* c11 */
     tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528));   /* c13 */
     tmp0  = tmp1 + tmp2 + tmp3 -
-            MULTIPLY(z1, FIX(2.286341144));        /* c7+c5+c3-c1 */
+           MULTIPLY(z1, FIX(2.286341144));        /* c7+c5+c3-c1 */
     tmp13 = tmp10 + tmp11 + tmp12 -
-            MULTIPLY(z1, FIX(1.835730603));        /* c9+c11+c13-c15 */
+           MULTIPLY(z1, FIX(1.835730603));        /* c9+c11+c13-c15 */
     z1    = MULTIPLY(z2 + z3, FIX(0.138617169));   /* c15 */
     tmp1  += z1 + MULTIPLY(z2, FIX(0.071888074));  /* c9+c11-c3-c15 */
     tmp2  += z1 - MULTIPLY(z3, FIX(1.125726048));  /* c5+c7+c15-c3 */
@@ -2758,53 +2798,53 @@ jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0]  = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp0,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[15] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp0,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[1]  = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp1,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[14] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp1,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[2]  = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp2,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp2,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[3]  = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp3,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp3,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[4]  = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[5]  = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[6]  = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[9]  = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[7]  = range_limit[(int) RIGHT_SHIFT(tmp27 + tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[8]  = range_limit[(int) RIGHT_SHIFT(tmp27 - tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -2820,8 +2860,8 @@ jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                JCOEFPTR coef_block,
-                JSAMPARRAY output_buf, JDIMENSION output_col)
+               JCOEFPTR coef_block,
+               JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27;
@@ -2835,9 +2875,11 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   int workspace[8*8];  /* 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. */
+  /* 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.
+   * 8-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
+   */
 
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
@@ -2853,9 +2895,9 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
      */
 
     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
-        inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
-        inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
-        inptr[DCTSIZE*7] == 0) {
+       inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+       inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+       inptr[DCTSIZE*7] == 0) {
       /* AC terms all zero */
       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
 
@@ -2874,15 +2916,16 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
       continue;
     }
 
-    /* Even part: reverse the even part of the forward DCT. */
-    /* The rotator is sqrt(2)*c(-6). */
+    /* Even part: reverse the even part of the forward DCT.
+     * The rotator is 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(z2, FIX_0_765366865);
-    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);
+    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);       /* c6 */
+    tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865);     /* c2-c6 */
+    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);     /* c2+c6 */
 
     z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
     z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
@@ -2911,21 +2954,21 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     z2 = tmp0 + tmp2;
     z3 = tmp1 + tmp3;
 
-    z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */
-    z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
-    z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+    z1 = MULTIPLY(z2 + z3, FIX_1_175875602);       /*  c3 */
+    z2 = MULTIPLY(z2, - FIX_1_961570560);          /* -c3-c5 */
+    z3 = MULTIPLY(z3, - FIX_0_390180644);          /* -c3+c5 */
     z2 += z1;
     z3 += z1;
 
-    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
-    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
-    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_0_298631336);        /* -c1+c3+c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_1_501321110);        /*  c1+c3-c5-c7 */
     tmp0 += z1 + z2;
     tmp3 += z1 + z3;
 
-    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
-    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
-    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_2_053119869);        /*  c1+c3-c5+c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_3_072711026);        /*  c1+c3+c5-c7 */
     tmp1 += z1 + z3;
     tmp2 += z1 + z2;
 
@@ -2948,14 +2991,17 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 2: process 8 rows from work array, store into output array.
    * 16-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/32).
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 8; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = (INT32) wsptr[0] +
+            ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+             (ONE << (PASS1_BITS+2)));
     tmp0 <<= CONST_BITS;
 
     z1 = (INT32) wsptr[4];
@@ -3003,9 +3049,9 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp11 = MULTIPLY(tmp11,   FIX(0.666655658));   /* c11 */
     tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528));   /* c13 */
     tmp0  = tmp1 + tmp2 + tmp3 -
-            MULTIPLY(z1, FIX(2.286341144));        /* c7+c5+c3-c1 */
+           MULTIPLY(z1, FIX(2.286341144));        /* c7+c5+c3-c1 */
     tmp13 = tmp10 + tmp11 + tmp12 -
-            MULTIPLY(z1, FIX(1.835730603));        /* c9+c11+c13-c15 */
+           MULTIPLY(z1, FIX(1.835730603));        /* c9+c11+c13-c15 */
     z1    = MULTIPLY(z2 + z3, FIX(0.138617169));   /* c15 */
     tmp1  += z1 + MULTIPLY(z2, FIX(0.071888074));  /* c9+c11-c3-c15 */
     tmp2  += z1 - MULTIPLY(z3, FIX(1.125726048));  /* c5+c7+c15-c3 */
@@ -3029,53 +3075,53 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0]  = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp0,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[15] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp0,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[1]  = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp1,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[14] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp1,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[2]  = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp2,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp2,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[3]  = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp3,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp3,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[4]  = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[5]  = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[6]  = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[9]  = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[7]  = range_limit[(int) RIGHT_SHIFT(tmp27 + tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[8]  = range_limit[(int) RIGHT_SHIFT(tmp27 - tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -3091,8 +3137,8 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_14x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                JCOEFPTR coef_block,
-                JSAMPARRAY output_buf, JDIMENSION output_col)
+               JCOEFPTR coef_block,
+               JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26;
@@ -3109,6 +3155,7 @@ jpeg_idct_14x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 1: process columns from input, store into work array.
    * 7-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/14).
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -3164,14 +3211,17 @@ jpeg_idct_14x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 2: process 7 rows from work array, store into output array.
    * 14-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/28).
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 7; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z1 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z1 <<= CONST_BITS;
     z4 = (INT32) wsptr[4];
     z2 = MULTIPLY(z4, FIX(1.274162392));         /* c4 */
@@ -3192,7 +3242,7 @@ jpeg_idct_14x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp13 = z3 + MULTIPLY(z1, FIX(0.273079590)); /* c2-c6 */
     tmp14 = z3 - MULTIPLY(z2, FIX(1.719280954)); /* c6+c10 */
     tmp15 = MULTIPLY(z1, FIX(0.613604268)) -     /* c10 */
-            MULTIPLY(z2, FIX(1.378756276));      /* c2 */
+           MULTIPLY(z2, FIX(1.378756276));      /* c2 */
 
     tmp20 = tmp10 + tmp13;
     tmp26 = tmp10 - tmp13;
@@ -3230,47 +3280,47 @@ jpeg_idct_14x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0]  = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[1]  = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[2]  = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[3]  = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[4]  = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[9]  = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[5]  = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[8]  = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[6]  = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp16,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[7]  = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp16,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -3286,8 +3336,8 @@ jpeg_idct_14x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_12x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                JCOEFPTR coef_block,
-                JSAMPARRAY output_buf, JDIMENSION output_col)
+               JCOEFPTR coef_block,
+               JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25;
@@ -3304,6 +3354,7 @@ jpeg_idct_12x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 1: process columns from input, store into work array.
    * 6-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/12).
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -3346,14 +3397,17 @@ jpeg_idct_12x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 2: process 6 rows from work array, store into output array.
    * 12-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/24).
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 6; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z3 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z3 <<= CONST_BITS;
 
     z4 = (INT32) wsptr[4];
@@ -3401,7 +3455,7 @@ jpeg_idct_12x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp12 += tmp13 + tmp14 - MULTIPLY(z3, FIX(1.478575242)); /* c1+c5-c7-c11 */
     tmp13 += tmp15 - tmp11 + MULTIPLY(z4, FIX(1.586706681)); /* c1+c11 */
     tmp15 += tmp14 - MULTIPLY(z1, FIX(0.676326758)) -        /* c7-c11 */
-             MULTIPLY(z4, FIX(1.982889723));                 /* c5+c7 */
+            MULTIPLY(z4, FIX(1.982889723));                 /* c5+c7 */
 
     z1 -= z4;
     z2 -= z3;
@@ -3412,41 +3466,41 @@ jpeg_idct_12x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0]  = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[1]  = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[2]  = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[9]  = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[3]  = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[8]  = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[4]  = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[7]  = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[5]  = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
     outptr[6]  = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15,
-                                               CONST_BITS+PASS1_BITS+3)
-                             & RANGE_MASK];
+                                              CONST_BITS+PASS1_BITS+3)
+                            & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -3462,8 +3516,8 @@ jpeg_idct_12x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_10x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                JCOEFPTR coef_block,
-                JSAMPARRAY output_buf, JDIMENSION output_col)
+               JCOEFPTR coef_block,
+               JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24;
@@ -3480,6 +3534,7 @@ jpeg_idct_10x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 1: process columns from input, store into work array.
    * 5-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/10).
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -3520,14 +3575,17 @@ jpeg_idct_10x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 2: process 5 rows from work array, store into output array.
    * 10-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/20).
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 5; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z3 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z3 <<= CONST_BITS;
     z4 = (INT32) wsptr[4];
     z1 = MULTIPLY(z4, FIX(1.144122806));         /* c4 */
@@ -3579,35 +3637,35 @@ jpeg_idct_10x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 8;                /* advance pointer to next row */
   }
@@ -3623,8 +3681,8 @@ jpeg_idct_10x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp3;
   INT32 tmp10, tmp11, tmp12, tmp13;
@@ -3639,8 +3697,10 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   SHIFT_TEMPS
 
   /* Pass 1: process columns from input, store into work array.
-   * 4-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
+   * 4-point IDCT kernel,
+   * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point IDCT].
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -3663,9 +3723,9 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Add fudge factor here for final descale. */
     z1 += ONE << (CONST_BITS-PASS1_BITS-1);
     tmp0 = RIGHT_SHIFT(z1 + MULTIPLY(z2, FIX_0_765366865), /* c2-c6 */
-                       CONST_BITS-PASS1_BITS);
+                      CONST_BITS-PASS1_BITS);
     tmp2 = RIGHT_SHIFT(z1 - MULTIPLY(z3, FIX_1_847759065), /* c2+c6 */
-                       CONST_BITS-PASS1_BITS);
+                      CONST_BITS-PASS1_BITS);
 
     /* Final output stage */
 
@@ -3675,31 +3735,36 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     wsptr[8*2] = (int) (tmp12 - tmp2);
   }
 
-  /* 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. */
+  /* 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.
+   * 8-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
+   */
 
   wsptr = workspace;
   for (ctr = 0; ctr < 4; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
-    /* 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(z2, FIX_0_765366865);
-    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);
+    /* Even part: reverse the even part of the forward DCT.
+     * The rotator is c(-6).
+     */
 
-    /* Add fudge factor here for final descale. */
-    z2 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z2 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z3 = (INT32) wsptr[4];
 
     tmp0 = (z2 + z3) << CONST_BITS;
     tmp1 = (z2 - z3) << CONST_BITS;
 
+    z2 = (INT32) wsptr[2];
+    z3 = (INT32) wsptr[6];
+
+    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);       /* c6 */
+    tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865);     /* c2-c6 */
+    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);     /* c2+c6 */
+
     tmp10 = tmp0 + tmp2;
     tmp13 = tmp0 - tmp2;
     tmp11 = tmp1 + tmp3;
@@ -3717,50 +3782,50 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     z2 = tmp0 + tmp2;
     z3 = tmp1 + tmp3;
 
-    z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */
-    z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
-    z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+    z1 = MULTIPLY(z2 + z3, FIX_1_175875602);       /*  c3 */
+    z2 = MULTIPLY(z2, - FIX_1_961570560);          /* -c3-c5 */
+    z3 = MULTIPLY(z3, - FIX_0_390180644);          /* -c3+c5 */
     z2 += z1;
     z3 += z1;
 
-    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
-    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
-    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_0_298631336);        /* -c1+c3+c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_1_501321110);        /*  c1+c3-c5-c7 */
     tmp0 += z1 + z2;
     tmp3 += z1 + z3;
 
-    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
-    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
-    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_2_053119869);        /*  c1+c3-c5+c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_3_072711026);        /*  c1+c3+c5-c7 */
     tmp1 += z1 + z3;
     tmp2 += z1 + z2;
 
     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp3,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp3,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += DCTSIZE;          /* advance pointer to next row */
   }
@@ -3776,8 +3841,8 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_6x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp10, tmp11, tmp12;
   INT32 z1, z2, z3;
@@ -3793,6 +3858,7 @@ jpeg_idct_6x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 1: process columns from input, store into work array.
    * 3-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/6).
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -3819,18 +3885,21 @@ jpeg_idct_6x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     wsptr[6*2] = (int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS-PASS1_BITS);
     wsptr[6*1] = (int) RIGHT_SHIFT(tmp2, CONST_BITS-PASS1_BITS);
   }
-
+  
   /* Pass 2: process 3 rows from work array, store into output array.
    * 6-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/12).
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 3; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = (INT32) wsptr[0] +
+            ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+             (ONE << (PASS1_BITS+2)));
     tmp0 <<= CONST_BITS;
     tmp2 = (INT32) wsptr[4];
     tmp10 = MULTIPLY(tmp2, FIX(0.707106781));   /* c4 */
@@ -3854,23 +3923,23 @@ jpeg_idct_6x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 6;                /* advance pointer to next row */
   }
@@ -3886,8 +3955,8 @@ jpeg_idct_6x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_4x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp2, tmp10, tmp12;
   INT32 z1, z2, z3;
@@ -3924,14 +3993,15 @@ jpeg_idct_4x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
    * 4-point IDCT kernel,
    * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point IDCT].
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 2; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = wsptr[0] + (ONE << 2);
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = wsptr[0] + ((((INT32) RANGE_CENTER) << 3) + (ONE << 2));
     tmp2 = wsptr[2];
 
     tmp10 = (tmp0 + tmp2) << CONST_BITS;
@@ -3950,17 +4020,17 @@ jpeg_idct_4x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2,
-                                              CONST_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2,
-                                              CONST_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 4;                /* advance pointer to next row */
   }
@@ -3976,14 +4046,14 @@ jpeg_idct_4x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_2x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
-  INT32 tmp0, tmp10;
+  DCTELEM tmp0, tmp1;
   ISLOW_MULT_TYPE * quantptr;
   JSAMPROW outptr;
   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
-  SHIFT_TEMPS
+  ISHIFT_TEMPS
 
   /* Pass 1: empty. */
 
@@ -3994,18 +4064,18 @@ jpeg_idct_2x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
   /* Even part */
 
-  tmp10 = DEQUANTIZE(coef_block[0], quantptr[0]);
-  /* Add fudge factor here for final descale. */
-  tmp10 += ONE << 2;
+  tmp0 = DEQUANTIZE(coef_block[0], quantptr[0]);
+  /* Add range center and fudge factor for final descale and range-limit. */
+  tmp0 += (((DCTELEM) RANGE_CENTER) << 3) + (1 << 2);
 
   /* Odd part */
 
-  tmp0 = DEQUANTIZE(coef_block[1], quantptr[1]);
+  tmp1 = DEQUANTIZE(coef_block[1], quantptr[1]);
 
   /* Final output stage */
 
-  outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, 3) & RANGE_MASK];
-  outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, 3) & RANGE_MASK];
+  outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp1, 3) & RANGE_MASK];
+  outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp1, 3) & RANGE_MASK];
 }
 
 
@@ -4018,8 +4088,8 @@ jpeg_idct_2x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                JCOEFPTR coef_block,
-                JSAMPARRAY output_buf, JDIMENSION output_col)
+               JCOEFPTR coef_block,
+               JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26, tmp27;
@@ -4036,6 +4106,7 @@ jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 1: process columns from input, store into work array.
    * 16-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/32).
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -4092,9 +4163,9 @@ jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp11 = MULTIPLY(tmp11,   FIX(0.666655658));   /* c11 */
     tmp12 = MULTIPLY(z1 - z2, FIX(0.410524528));   /* c13 */
     tmp0  = tmp1 + tmp2 + tmp3 -
-            MULTIPLY(z1, FIX(2.286341144));        /* c7+c5+c3-c1 */
+           MULTIPLY(z1, FIX(2.286341144));        /* c7+c5+c3-c1 */
     tmp13 = tmp10 + tmp11 + tmp12 -
-            MULTIPLY(z1, FIX(1.835730603));        /* c9+c11+c13-c15 */
+           MULTIPLY(z1, FIX(1.835730603));        /* c9+c11+c13-c15 */
     z1    = MULTIPLY(z2 + z3, FIX(0.138617169));   /* c15 */
     tmp1  += z1 + MULTIPLY(z2, FIX(0.071888074));  /* c9+c11-c3-c15 */
     tmp2  += z1 - MULTIPLY(z3, FIX(1.125726048));  /* c5+c7+c15-c3 */
@@ -4135,31 +4206,36 @@ jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     wsptr[8*8]  = (int) RIGHT_SHIFT(tmp27 - tmp13, CONST_BITS-PASS1_BITS);
   }
 
-  /* 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. */
+  /* 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.
+   * 8-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
+   */
 
   wsptr = workspace;
   for (ctr = 0; ctr < 16; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
-    /* 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(z2, FIX_0_765366865);
-    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);
+    /* Even part: reverse the even part of the forward DCT.
+     * The rotator is c(-6).
+     */
 
-    /* Add fudge factor here for final descale. */
-    z2 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    z2 = (INT32) wsptr[0] +
+          ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+           (ONE << (PASS1_BITS+2)));
     z3 = (INT32) wsptr[4];
 
     tmp0 = (z2 + z3) << CONST_BITS;
     tmp1 = (z2 - z3) << CONST_BITS;
 
+    z2 = (INT32) wsptr[2];
+    z3 = (INT32) wsptr[6];
+
+    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);       /* c6 */
+    tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865);     /* c2-c6 */
+    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);     /* c2+c6 */
+
     tmp10 = tmp0 + tmp2;
     tmp13 = tmp0 - tmp2;
     tmp11 = tmp1 + tmp3;
@@ -4177,50 +4253,50 @@ jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     z2 = tmp0 + tmp2;
     z3 = tmp1 + tmp3;
 
-    z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */
-    z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
-    z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+    z1 = MULTIPLY(z2 + z3, FIX_1_175875602);       /*  c3 */
+    z2 = MULTIPLY(z2, - FIX_1_961570560);          /* -c3-c5 */
+    z3 = MULTIPLY(z3, - FIX_0_390180644);          /* -c3+c5 */
     z2 += z1;
     z3 += z1;
 
-    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
-    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
-    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_0_298631336);        /* -c1+c3+c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_1_501321110);        /*  c1+c3-c5-c7 */
     tmp0 += z1 + z2;
     tmp3 += z1 + z3;
 
-    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
-    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
-    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_2_053119869);        /*  c1+c3-c5+c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_3_072711026);        /*  c1+c3+c5-c7 */
     tmp1 += z1 + z3;
     tmp2 += z1 + z2;
 
     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp3,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp3,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp1,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += DCTSIZE;          /* advance pointer to next row */
   }
@@ -4236,8 +4312,8 @@ jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                JCOEFPTR coef_block,
-                JSAMPARRAY output_buf, JDIMENSION output_col)
+               JCOEFPTR coef_block,
+               JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25, tmp26;
@@ -4254,6 +4330,7 @@ jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 1: process columns from input, store into work array.
    * 14-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/28).
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -4274,7 +4351,7 @@ jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp12 = z1 - z4;
 
     tmp23 = RIGHT_SHIFT(z1 - ((z2 + z3 - z4) << 1), /* c0 = (c4+c12-c8)*2 */
-                        CONST_BITS-PASS1_BITS);
+                       CONST_BITS-PASS1_BITS);
 
     z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
     z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
@@ -4284,7 +4361,7 @@ jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp13 = z3 + MULTIPLY(z1, FIX(0.273079590)); /* c2-c6 */
     tmp14 = z3 - MULTIPLY(z2, FIX(1.719280954)); /* c6+c10 */
     tmp15 = MULTIPLY(z1, FIX(0.613604268)) -     /* c10 */
-            MULTIPLY(z2, FIX(1.378756276));      /* c2 */
+           MULTIPLY(z2, FIX(1.378756276));      /* c2 */
 
     tmp20 = tmp10 + tmp13;
     tmp26 = tmp10 - tmp13;
@@ -4341,14 +4418,17 @@ jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 2: process 14 rows from work array, store into output array.
    * 7-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/14).
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 14; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp23 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp23 = (INT32) wsptr[0] +
+             ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+              (ONE << (PASS1_BITS+2)));
     tmp23 <<= CONST_BITS;
 
     z1 = (INT32) wsptr[2];
@@ -4384,26 +4464,26 @@ jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 7;                /* advance pointer to next row */
   }
@@ -4419,8 +4499,8 @@ jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_6x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                JCOEFPTR coef_block,
-                JSAMPARRAY output_buf, JDIMENSION output_col)
+               JCOEFPTR coef_block,
+               JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14, tmp15;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24, tmp25;
@@ -4437,6 +4517,7 @@ jpeg_idct_6x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 1: process columns from input, store into work array.
    * 12-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/24).
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -4493,7 +4574,7 @@ jpeg_idct_6x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp12 += tmp13 + tmp14 - MULTIPLY(z3, FIX(1.478575242)); /* c1+c5-c7-c11 */
     tmp13 += tmp15 - tmp11 + MULTIPLY(z4, FIX(1.586706681)); /* c1+c11 */
     tmp15 += tmp14 - MULTIPLY(z1, FIX(0.676326758)) -        /* c7-c11 */
-             MULTIPLY(z4, FIX(1.982889723));                 /* c5+c7 */
+            MULTIPLY(z4, FIX(1.982889723));                 /* c5+c7 */
 
     z1 -= z4;
     z2 -= z3;
@@ -4520,14 +4601,17 @@ jpeg_idct_6x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 2: process 12 rows from work array, store into output array.
    * 6-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/12).
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 12; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp10 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp10 = (INT32) wsptr[0] +
+             ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+              (ONE << (PASS1_BITS+2)));
     tmp10 <<= CONST_BITS;
     tmp12 = (INT32) wsptr[4];
     tmp20 = MULTIPLY(tmp12, FIX(0.707106781));   /* c4 */
@@ -4551,23 +4635,23 @@ jpeg_idct_6x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp20 + tmp10,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 6;                /* advance pointer to next row */
   }
@@ -4583,8 +4667,8 @@ jpeg_idct_6x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_5x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-                JCOEFPTR coef_block,
-                JSAMPARRAY output_buf, JDIMENSION output_col)
+               JCOEFPTR coef_block,
+               JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp10, tmp11, tmp12, tmp13, tmp14;
   INT32 tmp20, tmp21, tmp22, tmp23, tmp24;
@@ -4601,6 +4685,7 @@ jpeg_idct_5x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 1: process columns from input, store into work array.
    * 10-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/20).
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -4618,7 +4703,7 @@ jpeg_idct_5x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     tmp11 = z3 - z2;
 
     tmp22 = RIGHT_SHIFT(z3 - ((z1 - z2) << 1),   /* c0 = (c4-c8)*2 */
-                        CONST_BITS-PASS1_BITS);
+                       CONST_BITS-PASS1_BITS);
 
     z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
     z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
@@ -4676,14 +4761,17 @@ jpeg_idct_5x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 2: process 10 rows from work array, store into output array.
    * 5-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/10).
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 10; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp12 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp12 = (INT32) wsptr[0] +
+             ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+              (ONE << (PASS1_BITS+2)));
     tmp12 <<= CONST_BITS;
     tmp13 = (INT32) wsptr[2];
     tmp14 = (INT32) wsptr[4];
@@ -4706,20 +4794,20 @@ jpeg_idct_5x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp13,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp13,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp14,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp14,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 5;                /* advance pointer to next row */
   }
@@ -4735,8 +4823,8 @@ jpeg_idct_5x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp3;
   INT32 tmp10, tmp11, tmp12, tmp13;
@@ -4750,9 +4838,11 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   int workspace[4*8];  /* 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. */
+  /* 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.
+   * 8-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
+   */
 
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
@@ -4768,9 +4858,9 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
      */
 
     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
-        inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
-        inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
-        inptr[DCTSIZE*7] == 0) {
+       inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+       inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+       inptr[DCTSIZE*7] == 0) {
       /* AC terms all zero */
       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
 
@@ -4789,15 +4879,16 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
       continue;
     }
 
-    /* Even part: reverse the even part of the forward DCT. */
-    /* The rotator is sqrt(2)*c(-6). */
+    /* Even part: reverse the even part of the forward DCT.
+     * The rotator is 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(z2, FIX_0_765366865);
-    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);
+    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);       /* c6 */
+    tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865);     /* c2-c6 */
+    tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065);     /* c2+c6 */
 
     z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
     z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
@@ -4826,21 +4917,21 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     z2 = tmp0 + tmp2;
     z3 = tmp1 + tmp3;
 
-    z1 = MULTIPLY(z2 + z3, FIX_1_175875602); /* sqrt(2) * c3 */
-    z2 = MULTIPLY(z2, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
-    z3 = MULTIPLY(z3, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+    z1 = MULTIPLY(z2 + z3, FIX_1_175875602);       /*  c3 */
+    z2 = MULTIPLY(z2, - FIX_1_961570560);          /* -c3-c5 */
+    z3 = MULTIPLY(z3, - FIX_0_390180644);          /* -c3+c5 */
     z2 += z1;
     z3 += z1;
 
-    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
-    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
-    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+    z1 = MULTIPLY(tmp0 + tmp3, - FIX_0_899976223); /* -c3+c7 */
+    tmp0 = MULTIPLY(tmp0, FIX_0_298631336);        /* -c1+c3+c5-c7 */
+    tmp3 = MULTIPLY(tmp3, FIX_1_501321110);        /*  c1+c3-c5-c7 */
     tmp0 += z1 + z2;
     tmp3 += z1 + z3;
 
-    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
-    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
-    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+    z1 = MULTIPLY(tmp1 + tmp2, - FIX_2_562915447); /* -c1-c3 */
+    tmp1 = MULTIPLY(tmp1, FIX_2_053119869);        /*  c1+c3-c5+c7 */
+    tmp2 = MULTIPLY(tmp2, FIX_3_072711026);        /*  c1+c3+c5-c7 */
     tmp1 += z1 + z3;
     tmp2 += z1 + z2;
 
@@ -4861,16 +4952,20 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   }
 
   /* Pass 2: process 8 rows from work array, store into output array.
-   * 4-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16).
+   * 4-point IDCT kernel,
+   * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point IDCT].
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 8; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = (INT32) wsptr[0] +
+            ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+             (ONE << (PASS1_BITS+2)));
     tmp2 = (INT32) wsptr[2];
 
     tmp10 = (tmp0 + tmp2) << CONST_BITS;
@@ -4889,17 +4984,17 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 4;                /* advance pointer to next row */
   }
@@ -4915,8 +5010,8 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_3x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp1, tmp2, tmp10, tmp11, tmp12;
   INT32 z1, z2, z3;
@@ -4932,6 +5027,7 @@ jpeg_idct_3x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 1: process columns from input, store into work array.
    * 6-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/12).
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -4974,14 +5070,17 @@ jpeg_idct_3x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   /* Pass 2: process 6 rows from work array, store into output array.
    * 3-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/6).
    */
+
   wsptr = workspace;
   for (ctr = 0; ctr < 6; ctr++) {
     outptr = output_buf[ctr] + output_col;
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp0 = (INT32) wsptr[0] +
+            ((((INT32) RANGE_CENTER) << (PASS1_BITS+3)) +
+             (ONE << (PASS1_BITS+2)));
     tmp0 <<= CONST_BITS;
     tmp2 = (INT32) wsptr[2];
     tmp12 = MULTIPLY(tmp2, FIX(0.707106781)); /* c2 */
@@ -4996,14 +5095,14 @@ jpeg_idct_3x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp2,
-                                              CONST_BITS+PASS1_BITS+3)
-                            & RANGE_MASK];
+                                             CONST_BITS+PASS1_BITS+3)
+                           & RANGE_MASK];
 
     wsptr += 3;                /* advance pointer to next row */
   }
@@ -5019,8 +5118,8 @@ jpeg_idct_3x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_2x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
   INT32 tmp0, tmp2, tmp10, tmp12;
   INT32 z1, z2, z3;
@@ -5037,6 +5136,7 @@ jpeg_idct_2x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
    * 4-point IDCT kernel,
    * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point IDCT].
    */
+
   inptr = coef_block;
   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
   wsptr = workspace;
@@ -5075,8 +5175,10 @@ jpeg_idct_2x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
     /* Even part */
 
-    /* Add fudge factor here for final descale. */
-    tmp10 = wsptr[0] + (ONE << (CONST_BITS+2));
+    /* Add range center and fudge factor for final descale and range-limit. */
+    tmp10 = wsptr[0] +
+             ((((INT32) RANGE_CENTER) << (CONST_BITS+3)) +
+              (ONE << (CONST_BITS+2)));
 
     /* Odd part */
 
@@ -5085,9 +5187,9 @@ jpeg_idct_2x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
     /* Final output stage */
 
     outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, CONST_BITS+3)
-                            & RANGE_MASK];
+                           & RANGE_MASK];
     outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS+3)
-                            & RANGE_MASK];
+                           & RANGE_MASK];
 
     wsptr += 2;                /* advance pointer to next row */
   }
@@ -5103,13 +5205,13 @@ jpeg_idct_2x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
 GLOBAL(void)
 jpeg_idct_1x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
-               JCOEFPTR coef_block,
-               JSAMPARRAY output_buf, JDIMENSION output_col)
+              JCOEFPTR coef_block,
+              JSAMPARRAY output_buf, JDIMENSION output_col)
 {
-  INT32 tmp0, tmp10;
+  DCTELEM tmp0, tmp1;
   ISLOW_MULT_TYPE * quantptr;
   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
-  SHIFT_TEMPS
+  ISHIFT_TEMPS
 
   /* Process 1 column from input, store into output array. */
 
@@ -5117,20 +5219,20 @@ jpeg_idct_1x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
 
   /* Even part */
 
-  tmp10 = DEQUANTIZE(coef_block[DCTSIZE*0], quantptr[DCTSIZE*0]);
-  /* Add fudge factor here for final descale. */
-  tmp10 += ONE << 2;
+  tmp0 = DEQUANTIZE(coef_block[DCTSIZE*0], quantptr[DCTSIZE*0]);
+  /* Add range center and fudge factor for final descale and range-limit. */
+  tmp0 += (((DCTELEM) RANGE_CENTER) << 3) + (1 << 2);
 
   /* Odd part */
 
-  tmp0 = DEQUANTIZE(coef_block[DCTSIZE*1], quantptr[DCTSIZE*1]);
+  tmp1 = DEQUANTIZE(coef_block[DCTSIZE*1], quantptr[DCTSIZE*1]);
 
   /* Final output stage */
 
-  output_buf[0][output_col] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, 3)
-                                          & RANGE_MASK];
-  output_buf[1][output_col] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, 3)
-                                          & RANGE_MASK];
+  output_buf[0][output_col] =
+    range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp1, 3) & RANGE_MASK];
+  output_buf[1][output_col] =
+    range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp1, 3) & RANGE_MASK];
 }
 
 #endif /* IDCT_SCALING_SUPPORTED */
index 7049735..2d93e49 100644 (file)
@@ -79,7 +79,7 @@ jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
 
 GLOBAL(long)
 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
-                    long max_bytes_needed, long already_allocated)
+                   long max_bytes_needed, long already_allocated)
 {
   return cinfo->mem->max_memory_to_use - already_allocated;
 }
@@ -95,8 +95,8 @@ jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
 
 METHODDEF(void)
 read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
-                    void FAR * buffer_address,
-                    long file_offset, long byte_count)
+                   void FAR * buffer_address,
+                   long file_offset, long byte_count)
 {
   if (fseek(info->temp_file, file_offset, SEEK_SET))
     ERREXIT(cinfo, JERR_TFILE_SEEK);
@@ -108,8 +108,8 @@ read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
 
 METHODDEF(void)
 write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
-                     void FAR * buffer_address,
-                     long file_offset, long byte_count)
+                    void FAR * buffer_address,
+                    long file_offset, long byte_count)
 {
   if (fseek(info->temp_file, file_offset, SEEK_SET))
     ERREXIT(cinfo, JERR_TFILE_SEEK);
@@ -139,7 +139,7 @@ close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
 
 GLOBAL(void)
 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
-                         long total_bytes_needed)
+                        long total_bytes_needed)
 {
   if ((info->temp_file = tmpfile()) == NULL)
     ERREXITS(cinfo, JERR_TFILE_CREATE, "");
index ecbb2fa..0a137cd 100644 (file)
@@ -195,19 +195,19 @@ print_mem_stats (j_common_ptr cinfo, int pool_id)
    * This is helpful because message parm array can't handle longs.
    */
   fprintf(stderr, "Freeing pool %d, total space = %ld\n",
-          pool_id, mem->total_space_allocated);
+         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);
+           (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);
+           (long) shdr_ptr->hdr.bytes_used,
+           (long) shdr_ptr->hdr.bytes_left);
   }
 }
 
@@ -239,16 +239,16 @@ out_of_memory (j_common_ptr cinfo, int which)
  * machines, but may be too small if longs are 64 bits or more.
  */
 
-static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
+static const size_t first_pool_slop[JPOOL_NUMPOOLS] = 
 {
-        1600,                  /* first PERMANENT pool */
-        16000                  /* first IMAGE pool */
+       1600,                   /* first PERMANENT pool */
+       16000                   /* first IMAGE pool */
 };
 
-static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
+static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = 
 {
-        0,                     /* additional PERMANENT pools */
-        5000                   /* additional IMAGE pools */
+       0,                      /* additional PERMANENT pools */
+       5000                    /* additional IMAGE pools */
 };
 
 #define MIN_SLOP  50           /* greater than 0 to avoid futile looping */
@@ -299,10 +299,10 @@ alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
     for (;;) {
       hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
       if (hdr_ptr != NULL)
-        break;
+       break;
       slop /= 2;
       if (slop < MIN_SLOP)     /* give up when it gets real small */
-        out_of_memory(cinfo, 2); /* jpeg_get_small failed */
+       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 */
@@ -361,7 +361,7 @@ alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */
 
   hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
-                                            SIZEOF(large_pool_hdr));
+                                           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);
@@ -394,7 +394,7 @@ alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
 
 METHODDEF(JSAMPARRAY)
 alloc_sarray (j_common_ptr cinfo, int pool_id,
-              JDIMENSION samplesperrow, JDIMENSION numrows)
+             JDIMENSION samplesperrow, JDIMENSION numrows)
 /* Allocate a 2-D sample array */
 {
   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
@@ -405,7 +405,7 @@ alloc_sarray (j_common_ptr cinfo, int pool_id,
 
   /* Calculate max # of rows allowed in one allocation chunk */
   ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
-          ((long) samplesperrow * SIZEOF(JSAMPLE));
+         ((long) samplesperrow * SIZEOF(JSAMPLE));
   if (ltemp <= 0)
     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
   if (ltemp < (long) numrows)
@@ -416,15 +416,15 @@ alloc_sarray (j_common_ptr cinfo, int pool_id,
 
   /* Get space for row pointers (small object) */
   result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
-                                    (size_t) (numrows * SIZEOF(JSAMPROW)));
+                                   (size_t) (numrows * SIZEOF(JSAMPROW)));
 
   /* Get the rows themselves (large objects) */
   currow = 0;
   while (currow < numrows) {
     rowsperchunk = MIN(rowsperchunk, numrows - currow);
     workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
-        (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
-                  * SIZEOF(JSAMPLE)));
+       (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
+                 * SIZEOF(JSAMPLE)));
     for (i = rowsperchunk; i > 0; i--) {
       result[currow++] = workspace;
       workspace += samplesperrow;
@@ -442,7 +442,7 @@ alloc_sarray (j_common_ptr cinfo, int pool_id,
 
 METHODDEF(JBLOCKARRAY)
 alloc_barray (j_common_ptr cinfo, int pool_id,
-              JDIMENSION blocksperrow, JDIMENSION numrows)
+             JDIMENSION blocksperrow, JDIMENSION numrows)
 /* Allocate a 2-D coefficient-block array */
 {
   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
@@ -453,7 +453,7 @@ alloc_barray (j_common_ptr cinfo, int pool_id,
 
   /* Calculate max # of rows allowed in one allocation chunk */
   ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
-          ((long) blocksperrow * SIZEOF(JBLOCK));
+         ((long) blocksperrow * SIZEOF(JBLOCK));
   if (ltemp <= 0)
     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
   if (ltemp < (long) numrows)
@@ -464,15 +464,15 @@ alloc_barray (j_common_ptr cinfo, int pool_id,
 
   /* Get space for row pointers (small object) */
   result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
-                                     (size_t) (numrows * SIZEOF(JBLOCKROW)));
+                                    (size_t) (numrows * SIZEOF(JBLOCKROW)));
 
   /* Get the rows themselves (large objects) */
   currow = 0;
   while (currow < numrows) {
     rowsperchunk = MIN(rowsperchunk, numrows - currow);
     workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
-        (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
-                  * SIZEOF(JBLOCK)));
+       (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
+                 * SIZEOF(JBLOCK)));
     for (i = rowsperchunk; i > 0; i--) {
       result[currow++] = workspace;
       workspace += blocksperrow;
@@ -522,8 +522,8 @@ alloc_barray (j_common_ptr cinfo, int pool_id,
 
 METHODDEF(jvirt_sarray_ptr)
 request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
-                     JDIMENSION samplesperrow, JDIMENSION numrows,
-                     JDIMENSION maxaccess)
+                    JDIMENSION samplesperrow, JDIMENSION numrows,
+                    JDIMENSION maxaccess)
 /* Request a virtual 2-D sample array */
 {
   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
@@ -535,7 +535,7 @@ request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
 
   /* get control block */
   result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
-                                          SIZEOF(struct jvirt_sarray_control));
+                                         SIZEOF(struct jvirt_sarray_control));
 
   result->mem_buffer = NULL;   /* marks array not yet realized */
   result->rows_in_array = numrows;
@@ -552,8 +552,8 @@ request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
 
 METHODDEF(jvirt_barray_ptr)
 request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
-                     JDIMENSION blocksperrow, JDIMENSION numrows,
-                     JDIMENSION maxaccess)
+                    JDIMENSION blocksperrow, JDIMENSION numrows,
+                    JDIMENSION maxaccess)
 /* Request a virtual 2-D coefficient-block array */
 {
   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
@@ -565,7 +565,7 @@ request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
 
   /* get control block */
   result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
-                                          SIZEOF(struct jvirt_barray_control));
+                                         SIZEOF(struct jvirt_barray_control));
 
   result->mem_buffer = NULL;   /* marks array not yet realized */
   result->rows_in_array = numrows;
@@ -599,17 +599,17 @@ realize_virt_arrays (j_common_ptr cinfo)
   for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
     if (sptr->mem_buffer == NULL) { /* if not realized yet */
       space_per_minheight += (long) sptr->maxaccess *
-                             (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
+                            (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
       maximum_space += (long) sptr->rows_in_array *
-                       (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
+                      (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
     }
   }
   for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
     if (bptr->mem_buffer == NULL) { /* if not realized yet */
       space_per_minheight += (long) bptr->maxaccess *
-                             (long) bptr->blocksperrow * SIZEOF(JBLOCK);
+                            (long) bptr->blocksperrow * SIZEOF(JBLOCK);
       maximum_space += (long) bptr->rows_in_array *
-                       (long) bptr->blocksperrow * SIZEOF(JBLOCK);
+                      (long) bptr->blocksperrow * SIZEOF(JBLOCK);
     }
   }
 
@@ -618,7 +618,7 @@ realize_virt_arrays (j_common_ptr cinfo)
 
   /* Determine amount of memory to actually use; this is system-dependent. */
   avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
-                                 mem->total_space_allocated);
+                                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 minheights
@@ -641,19 +641,19 @@ realize_virt_arrays (j_common_ptr cinfo)
     if (sptr->mem_buffer == NULL) { /* if not realized yet */
       minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
       if (minheights <= max_minheights) {
-        /* This buffer fits in memory */
-        sptr->rows_in_mem = sptr->rows_in_array;
+       /* 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 = (JDIMENSION) (max_minheights * sptr->maxaccess);
-        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;
+       /* It doesn't fit in memory, create backing store. */
+       sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
+       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_sarray(cinfo, JPOOL_IMAGE,
-                                      sptr->samplesperrow, sptr->rows_in_mem);
+                                     sptr->samplesperrow, sptr->rows_in_mem);
       sptr->rowsperchunk = mem->last_rowsperchunk;
       sptr->cur_start_row = 0;
       sptr->first_undef_row = 0;
@@ -665,19 +665,19 @@ realize_virt_arrays (j_common_ptr cinfo)
     if (bptr->mem_buffer == NULL) { /* if not realized yet */
       minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
       if (minheights <= max_minheights) {
-        /* This buffer fits in memory */
-        bptr->rows_in_mem = bptr->rows_in_array;
+       /* 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 = (JDIMENSION) (max_minheights * bptr->maxaccess);
-        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;
+       /* It doesn't fit in memory, create backing store. */
+       bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
+       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_barray(cinfo, JPOOL_IMAGE,
-                                      bptr->blocksperrow, bptr->rows_in_mem);
+                                     bptr->blocksperrow, bptr->rows_in_mem);
       bptr->rowsperchunk = mem->last_rowsperchunk;
       bptr->cur_start_row = 0;
       bptr->first_undef_row = 0;
@@ -709,12 +709,12 @@ do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
     byte_count = rows * bytesperrow;
     if (writing)
       (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
-                                            (void FAR *) ptr->mem_buffer[i],
-                                            file_offset, byte_count);
+                                           (void FAR *) ptr->mem_buffer[i],
+                                           file_offset, byte_count);
     else
       (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
-                                           (void FAR *) ptr->mem_buffer[i],
-                                           file_offset, byte_count);
+                                          (void FAR *) ptr->mem_buffer[i],
+                                          file_offset, byte_count);
     file_offset += byte_count;
   }
 }
@@ -742,12 +742,12 @@ do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
     byte_count = rows * bytesperrow;
     if (writing)
       (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
-                                            (void FAR *) ptr->mem_buffer[i],
-                                            file_offset, byte_count);
+                                           (void FAR *) ptr->mem_buffer[i],
+                                           file_offset, byte_count);
     else
       (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
-                                           (void FAR *) ptr->mem_buffer[i],
-                                           file_offset, byte_count);
+                                          (void FAR *) ptr->mem_buffer[i],
+                                          file_offset, byte_count);
     file_offset += byte_count;
   }
 }
@@ -755,8 +755,8 @@ do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
 
 METHODDEF(JSAMPARRAY)
 access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
-                    JDIMENSION start_row, JDIMENSION num_rows,
-                    boolean writable)
+                   JDIMENSION start_row, JDIMENSION num_rows,
+                   boolean writable)
 /* Access the part of a virtual sample array starting at start_row */
 /* and extending for num_rows rows.  writable is true if  */
 /* caller intends to modify the accessed area. */
@@ -794,7 +794,7 @@ access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
 
       ltemp = (long) end_row - (long) ptr->rows_in_mem;
       if (ltemp < 0)
-        ltemp = 0;             /* don't fall off front end of file */
+       ltemp = 0;              /* don't fall off front end of file */
       ptr->cur_start_row = (JDIMENSION) ltemp;
     }
     /* Read in the selected part of the array.
@@ -810,7 +810,7 @@ access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
   if (ptr->first_undef_row < end_row) {
     if (ptr->first_undef_row < start_row) {
       if (writable)            /* writer skipped over a section of array */
-        ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
       undef_row = start_row;   /* but reader is allowed to read ahead */
     } else {
       undef_row = ptr->first_undef_row;
@@ -822,12 +822,12 @@ access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
       undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
       end_row -= ptr->cur_start_row;
       while (undef_row < end_row) {
-        FMEMZERO((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
-        undef_row++;
+       FMEMZERO((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
+       undef_row++;
       }
     } else {
       if (! writable)          /* reader looking at undefined data */
-        ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
     }
   }
   /* Flag the buffer dirty if caller will write in it */
@@ -840,8 +840,8 @@ access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
 
 METHODDEF(JBLOCKARRAY)
 access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
-                    JDIMENSION start_row, JDIMENSION num_rows,
-                    boolean writable)
+                   JDIMENSION start_row, JDIMENSION num_rows,
+                   boolean writable)
 /* Access the part of a virtual block array starting at start_row */
 /* and extending for num_rows rows.  writable is true if  */
 /* caller intends to modify the accessed area. */
@@ -879,7 +879,7 @@ access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
 
       ltemp = (long) end_row - (long) ptr->rows_in_mem;
       if (ltemp < 0)
-        ltemp = 0;             /* don't fall off front end of file */
+       ltemp = 0;              /* don't fall off front end of file */
       ptr->cur_start_row = (JDIMENSION) ltemp;
     }
     /* Read in the selected part of the array.
@@ -895,7 +895,7 @@ access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
   if (ptr->first_undef_row < end_row) {
     if (ptr->first_undef_row < start_row) {
       if (writable)            /* writer skipped over a section of array */
-        ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
       undef_row = start_row;   /* but reader is allowed to read ahead */
     } else {
       undef_row = ptr->first_undef_row;
@@ -907,12 +907,12 @@ access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
       undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
       end_row -= ptr->cur_start_row;
       while (undef_row < end_row) {
-        FMEMZERO((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
-        undef_row++;
+       FMEMZERO((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
+       undef_row++;
       }
     } else {
       if (! writable)          /* reader looking at undefined data */
-        ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
     }
   }
   /* Flag the buffer dirty if caller will write in it */
@@ -950,15 +950,15 @@ free_pool (j_common_ptr cinfo, int pool_id)
 
     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);
+       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);
+       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;
@@ -971,8 +971,8 @@ free_pool (j_common_ptr cinfo, int pool_id)
   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);
+                 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;
@@ -985,8 +985,8 @@ free_pool (j_common_ptr cinfo, int pool_id)
   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);
+                 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;
@@ -1108,9 +1108,9 @@ jinit_memory_mgr (j_common_ptr cinfo)
       char ch = 'x';
 
       if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
-        if (ch == 'm' || ch == 'M')
-          max_to_use *= 1000L;
-        mem->pub.max_memory_to_use = max_to_use * 1000L;
+       if (ch == 'm' || ch == 'M')
+         max_to_use *= 1000L;
+       mem->pub.max_memory_to_use = max_to_use * 1000L;
       }
     }
   }
index 3963805..eb8c337 100644 (file)
@@ -71,7 +71,7 @@ jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
 
 GLOBAL(long)
 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
-                    long max_bytes_needed, long already_allocated)
+                   long max_bytes_needed, long already_allocated)
 {
   return max_bytes_needed;
 }
@@ -85,7 +85,7 @@ jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
 
 GLOBAL(void)
 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
-                         long total_bytes_needed)
+                        long total_bytes_needed)
 {
   ERREXIT(cinfo, JERR_NO_BACKING_STORE);
 }
index 6cfea6a..6c3c6d3 100644 (file)
@@ -46,7 +46,7 @@
 
 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));
+                                 size_t sizeofobject));
 
 /*
  * These two functions are used to allocate and release large chunks of
@@ -58,9 +58,9 @@ EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,
  */
 
 EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,
-                                       size_t sizeofobject));
+                                      size_t sizeofobject));
 EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
-                                  size_t sizeofobject));
+                                 size_t sizeofobject));
 
 /*
  * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
@@ -101,9 +101,9 @@ EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
  */
 
 EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo,
-                                     long min_bytes_needed,
-                                     long max_bytes_needed,
-                                     long already_allocated));
+                                    long min_bytes_needed,
+                                    long max_bytes_needed,
+                                    long already_allocated));
 
 
 /*
@@ -139,15 +139,15 @@ typedef struct backing_store_struct * backing_store_ptr;
 typedef struct backing_store_struct {
   /* 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));
+                                    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));
+                                     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));
+                                     backing_store_ptr info));
 
   /* Private fields for system-dependent backing-store management */
 #ifdef USE_MSDOS_MEMMGR
@@ -178,8 +178,8 @@ typedef struct backing_store_struct {
  */
 
 EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,
-                                          backing_store_ptr info,
-                                          long total_bytes_needed));
+                                         backing_store_ptr info,
+                                         long total_bytes_needed));
 
 
 /*
index 2407edb..679d68b 100644 (file)
@@ -2,7 +2,7 @@
  * jmorecfg.h
  *
  * Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 1997-2012 by Guido Vollbeding.
+ * Modified 1997-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
 /*
  * Define BITS_IN_JSAMPLE as either
  *   8   for 8-bit sample values (the usual setting)
+ *   9   for 9-bit sample values
+ *   10  for 10-bit sample values
+ *   11  for 11-bit sample values
  *   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.
+ * Only 8, 9, 10, 11, and 12 bits sample data precision are supported for
+ * full-feature DCT processing.  Further depths up to 16-bit may be added
+ * later for the lossless modes of operation.
+ * Run-time selection and conversion of data precision will be added later
+ * and are currently not supported, sorry.
+ * Exception:  The transcoding part (jpegtran) supports all settings in a
+ * single instance, since it operates on the level of DCT coefficients and
+ * not sample values.  The DCT coefficients are of the same type (16 bits)
+ * in all cases (see below).
  */
 
-#define BITS_IN_JSAMPLE  8     /* use 8 or 12 */
+#define BITS_IN_JSAMPLE  8     /* use 8, 9, 10, 11, or 12 */
 
 
 /*
@@ -77,6 +86,48 @@ typedef char JSAMPLE;
 #endif /* BITS_IN_JSAMPLE == 8 */
 
 
+#if BITS_IN_JSAMPLE == 9
+/* JSAMPLE should be the smallest type that will hold the values 0..511.
+ * On nearly all machines "short" will do nicely.
+ */
+
+typedef short JSAMPLE;
+#define GETJSAMPLE(value)  ((int) (value))
+
+#define MAXJSAMPLE     511
+#define CENTERJSAMPLE  256
+
+#endif /* BITS_IN_JSAMPLE == 9 */
+
+
+#if BITS_IN_JSAMPLE == 10
+/* JSAMPLE should be the smallest type that will hold the values 0..1023.
+ * On nearly all machines "short" will do nicely.
+ */
+
+typedef short JSAMPLE;
+#define GETJSAMPLE(value)  ((int) (value))
+
+#define MAXJSAMPLE     1023
+#define CENTERJSAMPLE  512
+
+#endif /* BITS_IN_JSAMPLE == 10 */
+
+
+#if BITS_IN_JSAMPLE == 11
+/* JSAMPLE should be the smallest type that will hold the values 0..2047.
+ * On nearly all machines "short" will do nicely.
+ */
+
+typedef short JSAMPLE;
+#define GETJSAMPLE(value)  ((int) (value))
+
+#define MAXJSAMPLE     2047
+#define CENTERJSAMPLE  1024
+
+#endif /* BITS_IN_JSAMPLE == 11 */
+
+
 #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.
@@ -252,7 +303,10 @@ typedef void noreturn_t;
  * Defining HAVE_BOOLEAN before including jpeglib.h should make it work.
  */
 
-#ifdef HAVE_BOOLEAN
+#ifndef HAVE_BOOLEAN
+#if defined FALSE || defined TRUE || defined QGLOBAL_H
+/* Qt3 defines FALSE and TRUE as "const" variables in qglobal.h */
+typedef int boolean;
 #ifndef FALSE                  /* in case these macros already exist */
 #define FALSE  0               /* values of boolean */
 #endif
@@ -262,6 +316,7 @@ typedef void noreturn_t;
 #else
 typedef enum { FALSE = 0, TRUE = 1 } boolean;
 #endif
+#endif
 
 
 /*
@@ -299,11 +354,12 @@ typedef enum { FALSE = 0, TRUE = 1 } boolean;
 #define C_PROGRESSIVE_SUPPORTED            /* Progressive JPEG? (Requires MULTISCAN)*/
 #define DCT_SCALING_SUPPORTED      /* Input rescaling via DCT? (Requires DCT_ISLOW)*/
 #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.
+/* Note: if you selected more than 8-bit data precision, it is dangerous to
+ * turn off ENTROPY_OPT_SUPPORTED.  The standard Huffman tables are only
+ * good for 8-bit precision, so arithmetic coding is recommended for higher
+ * precision.  The Huffman encoder normally uses entropy optimization to
+ * compute usable tables for higher precision.  Otherwise, you'll have to
+ * supply different default Huffman tables.
  * The exact same statements apply for progressive JPEG: the default tables
  * don't work for progressive mode.  (This may get fixed, however.)
  */
@@ -314,7 +370,7 @@ typedef enum { FALSE = 0, TRUE = 1 } boolean;
 #define D_ARITH_CODING_SUPPORTED    /* Arithmetic coding back end? */
 #define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
 #define D_PROGRESSIVE_SUPPORTED            /* Progressive JPEG? (Requires MULTISCAN)*/
-#define IDCT_SCALING_SUPPORTED     /* Output rescaling via IDCT? */
+#define IDCT_SCALING_SUPPORTED     /* Output rescaling via IDCT? (Requires DCT_ISLOW)*/
 #define SAVE_MARKERS_SUPPORTED     /* jpeg_save_markers() needed? */
 #define BLOCK_SMOOTHING_SUPPORTED   /* Block smoothing? (Progressive only) */
 #undef  UPSAMPLE_SCALING_SUPPORTED  /* Output rescaling at upsample stage? */
index e037926..18bb887 100644 (file)
@@ -2,7 +2,7 @@
  * jpegint.h
  *
  * Copyright (C) 1991-1997, Thomas G. Lane.
- * Modified 1997-2011 by Guido Vollbeding.
+ * Modified 1997-2013 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
 /* Declarations for both compression & decompression */
 
 typedef enum {                 /* Operating modes for buffer controllers */
-        JBUF_PASS_THRU,                /* Plain stripwise operation */
-        /* 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 */
+       JBUF_PASS_THRU,         /* Plain stripwise operation */
+       /* 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 (jdapi.c has some dependencies on ordering!) */
@@ -57,54 +57,54 @@ struct jpeg_comp_master {
 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));
+                              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));
+                                  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(boolean, compress_data, (j_compress_ptr cinfo,
-                                   JSAMPIMAGE input_buf));
+                                  JSAMPIMAGE input_buf));
 };
 
 /* 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));
+                               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));
+                            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) */
 typedef JMETHOD(void, forward_DCT_ptr,
-                (j_compress_ptr cinfo, jpeg_component_info * compptr,
-                 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
-                 JDIMENSION start_row, JDIMENSION start_col,
-                 JDIMENSION num_blocks));
+               (j_compress_ptr cinfo, jpeg_component_info * compptr,
+                JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+                JDIMENSION start_row, JDIMENSION start_col,
+                JDIMENSION num_blocks));
 
 struct jpeg_forward_dct {
   JMETHOD(void, start_pass, (j_compress_ptr cinfo));
@@ -129,7 +129,7 @@ struct jpeg_marker_writer {
   /* These routines are exported to allow insertion of extra markers */
   /* Probably only COM and APPn markers should be written this way */
   JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
-                                      unsigned int datalen));
+                                     unsigned int datalen));
   JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
 };
 
@@ -161,8 +161,8 @@ struct jpeg_input_controller {
 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));
+                              JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+                              JDIMENSION out_rows_avail));
 };
 
 /* Coefficient buffer control */
@@ -171,7 +171,7 @@ struct jpeg_d_coef_controller {
   JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
   JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
   JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
-                                 JSAMPIMAGE output_buf));
+                                JSAMPIMAGE output_buf));
   /* Pointer to array of coefficient virtual arrays, or NULL if none */
   jvirt_barray_ptr *coef_arrays;
 };
@@ -180,12 +180,12 @@ struct jpeg_d_coef_controller {
 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));
+                                   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 */
@@ -211,15 +211,15 @@ struct jpeg_marker_reader {
 /* 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));
+  JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo, JBLOCKROW *MCU_data));
+  JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
 };
 
 /* 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));
+               (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+                JCOEFPTR coef_block,
+                JSAMPARRAY output_buf, JDIMENSION output_col));
 
 struct jpeg_inverse_dct {
   JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
@@ -231,12 +231,12 @@ struct jpeg_inverse_dct {
 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));
+                          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 */
 };
@@ -245,16 +245,16 @@ struct jpeg_upsampler {
 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));
+                               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));
+                                JSAMPARRAY input_buf, JSAMPARRAY output_buf,
+                                int num_rows));
   JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
   JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
 };
@@ -281,9 +281,9 @@ struct jpeg_color_quantizer {
 #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)))
+       ((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))
@@ -359,13 +359,13 @@ EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
 /* Compression module initialization routines */
 EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
 EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
-                                         boolean transcode_only));
+                                        boolean transcode_only));
 EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
-                                          boolean need_full_buffer));
+                                         boolean need_full_buffer));
 EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
-                                          boolean need_full_buffer));
+                                         boolean need_full_buffer));
 EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
-                                          boolean need_full_buffer));
+                                         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));
@@ -375,11 +375,11 @@ 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));
+                                         boolean need_full_buffer));
 EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
-                                          boolean need_full_buffer));
+                                         boolean need_full_buffer));
 EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
-                                          boolean need_full_buffer));
+                                         boolean need_full_buffer));
 EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
 EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
 EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
@@ -397,10 +397,10 @@ EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
 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));
+                                   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));
+                                 JDIMENSION num_blocks));
 /* Constant tables in jutils.c */
 #if 0                          /* This table is not actually needed in v6a */
 extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
index 3e58864..939b50b 100644 (file)
@@ -2,7 +2,7 @@
  * jpeglib.h
  *
  * Copyright (C) 1991-1998, Thomas G. Lane.
- * Modified 2002-2012 by Guido Vollbeding.
+ * Modified 2002-2015 by Guido Vollbeding.
  * This file is part of the Independent JPEG Group's software.
  * For conditions of distribution and use, see the accompanying README file.
  *
@@ -39,12 +39,12 @@ extern "C" {
 
 #define JPEG_LIB_VERSION        90     /* Compatibility version 9.0 */
 #define JPEG_LIB_VERSION_MAJOR  9
-#define JPEG_LIB_VERSION_MINOR  0
+#define JPEG_LIB_VERSION_MINOR  2
 
 
 /* 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.
+ * 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 coefficients */
@@ -109,7 +109,7 @@ typedef struct {
 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 */
+                               /* 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.
@@ -137,9 +137,9 @@ typedef struct {
   /* The decompressor output side may not use these variables. */
   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
@@ -157,16 +157,21 @@ typedef struct {
   /* The downsampled dimensions are the component's actual, unpadded number
    * of samples at the main buffer (preprocessing/compression interface);
    * DCT scaling is included, so
-   * downsampled_width = ceil(image_width * Hi/Hmax * DCT_h_scaled_size/DCTSIZE)
+   * downsampled_width =
+   *   ceil(image_width * Hi/Hmax * DCT_h_scaled_size/block_size)
    * and similarly for height.
    */
   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.
+  /* 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.
+   * For compression, some of the components will need further quantization
+   * scale by factor of 2 after DCT (eg BG_YCC output from normal RGB input).
+   * The field is first set TRUE for decompression, FALSE for compression
+   * in initial_setup, and then adapted in color conversion setup.
    */
-  boolean component_needed;    /* do we need the value of this component? */
+  boolean component_needed;
 
   /* These values are computed before starting a scan of the component. */
   /* The decompressor output side may not use these variables. */
@@ -213,27 +218,29 @@ struct jpeg_marker_struct {
 /* 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 */
+       JCS_UNKNOWN,            /* error/unspecified */
+       JCS_GRAYSCALE,          /* monochrome */
+       JCS_RGB,                /* red/green/blue, standard RGB (sRGB) */
+       JCS_YCbCr,              /* Y/Cb/Cr (also known as YUV), standard YCC */
+       JCS_CMYK,               /* C/M/Y/K */
+       JCS_YCCK,               /* Y/Cb/Cr/K */
+       JCS_BG_RGB,             /* big gamut red/green/blue, bg-sRGB */
+       JCS_BG_YCC              /* big gamut Y/Cb/Cr, bg-sYCC */
 } J_COLOR_SPACE;
 
 /* Supported color transforms. */
 
 typedef enum {
-        JCT_NONE           = 0,
-        JCT_SUBTRACT_GREEN = 1
+       JCT_NONE           = 0,
+       JCT_SUBTRACT_GREEN = 1
 } J_COLOR_TRANSFORM;
 
 /* 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 */
+       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 */
@@ -246,9 +253,9 @@ typedef enum {
 /* Dithering options for decompression. */
 
 typedef enum {
-        JDITHER_NONE,          /* no dithering */
-        JDITHER_ORDERED,       /* simple ordered dither */
-        JDITHER_FS             /* Floyd-Steinberg error diffusion dither */
+       JDITHER_NONE,           /* no dithering */
+       JDITHER_ORDERED,        /* simple ordered dither */
+       JDITHER_FS              /* Floyd-Steinberg error diffusion dither */
 } J_DITHER_MODE;
 
 
@@ -407,7 +414,7 @@ struct jpeg_compress_struct {
    * 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.
@@ -415,10 +422,10 @@ struct jpeg_compress_struct {
   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[C_MAX_BLOCKS_IN_MCU];
   /* MCU_membership[i] is index in cur_comp_info of component owning */
@@ -704,7 +711,7 @@ struct jpeg_error_mgr {
 #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.
    */
@@ -714,11 +721,11 @@ struct jpeg_error_mgr {
     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
@@ -808,38 +815,38 @@ 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));
+                               size_t sizeofobject));
   JMETHOD(void FAR *, alloc_large, (j_common_ptr cinfo, int pool_id,
-                                     size_t sizeofobject));
+                                    size_t sizeofobject));
   JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id,
-                                     JDIMENSION samplesperrow,
-                                     JDIMENSION numrows));
+                                    JDIMENSION samplesperrow,
+                                    JDIMENSION numrows));
   JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id,
-                                      JDIMENSION blocksperrow,
-                                      JDIMENSION numrows));
+                                     JDIMENSION blocksperrow,
+                                     JDIMENSION numrows));
   JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo,
-                                                  int pool_id,
-                                                  boolean pre_zero,
-                                                  JDIMENSION samplesperrow,
-                                                  JDIMENSION numrows,
-                                                  JDIMENSION maxaccess));
+                                                 int pool_id,
+                                                 boolean pre_zero,
+                                                 JDIMENSION samplesperrow,
+                                                 JDIMENSION numrows,
+                                                 JDIMENSION maxaccess));
   JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo,
-                                                  int pool_id,
-                                                  boolean pre_zero,
-                                                  JDIMENSION blocksperrow,
-                                                  JDIMENSION numrows,
-                                                  JDIMENSION maxaccess));
+                                                 int pool_id,
+                                                 boolean pre_zero,
+                                                 JDIMENSION blocksperrow,
+                                                 JDIMENSION numrows,
+                                                 JDIMENSION maxaccess));
   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,
-                                           JDIMENSION num_rows,
-                                           boolean writable));
+                                          jvirt_sarray_ptr ptr,
+                                          JDIMENSION start_row,
+                                          JDIMENSION num_rows,
+                                          boolean writable));
   JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo,
-                                            jvirt_barray_ptr ptr,
-                                            JDIMENSION start_row,
-                                            JDIMENSION num_rows,
-                                            boolean writable));
+                                           jvirt_barray_ptr ptr,
+                                           JDIMENSION start_row,
+                                           JDIMENSION num_rows,
+                                           boolean writable));
   JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id));
   JMETHOD(void, self_destruct, (j_common_ptr cinfo));
 
@@ -876,7 +883,7 @@ typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));
 /* 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
+ * (If your compiler itself needs names to be unique in less than 15 
  * characters, you are out of luck.  Get a better compiler.)
  */
 
@@ -939,7 +946,7 @@ typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));
 
 /* Default error-management setup */
 EXTERN(struct jpeg_error_mgr *) jpeg_std_error
-        JPP((struct jpeg_error_mgr * err));
+       JPP((struct jpeg_error_mgr * err));
 
 /* Initialization of JPEG compression objects.
  * jpeg_create_compress() and jpeg_create_decompress() are the exported
@@ -950,14 +957,14 @@ EXTERN(struct jpeg_error_mgr *) jpeg_std_error
  */
 #define jpeg_create_compress(cinfo) \
     jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
-                        (size_t) sizeof(struct jpeg_compress_struct))
+                       (size_t) sizeof(struct jpeg_compress_struct))
 #define jpeg_create_decompress(cinfo) \
     jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
-                          (size_t) sizeof(struct jpeg_decompress_struct))
+                         (size_t) sizeof(struct jpeg_decompress_struct))
 EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo,
-                                      int version, size_t structsize));
+                                     int version, size_t structsize));
 EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo,
-                                        int version, size_t structsize));
+                                       int version, size_t structsize));
 /* Destruction of JPEG compression objects */
 EXTERN(void) jpeg_destroy_compress JPP((j_compress_ptr cinfo));
 EXTERN(void) jpeg_destroy_decompress JPP((j_decompress_ptr cinfo));
@@ -969,42 +976,42 @@ EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile));
 
 /* Data source and destination managers: memory buffers. */
 EXTERN(void) jpeg_mem_dest JPP((j_compress_ptr cinfo,
-                               unsigned char ** outbuffer,
-                               unsigned long * outsize));
+                              unsigned char ** outbuffer,
+                              unsigned long * outsize));
 EXTERN(void) jpeg_mem_src JPP((j_decompress_ptr cinfo,
-                              unsigned char * inbuffer,
-                              unsigned long insize));
+                             const unsigned char * inbuffer,
+                             unsigned long insize));
 
 /* 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));
+                                     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));
+                                  boolean force_baseline));
 EXTERN(void) jpeg_set_linear_quality JPP((j_compress_ptr cinfo,
-                                          int scale_factor,
-                                          boolean force_baseline));
+                                         int scale_factor,
+                                         boolean force_baseline));
 EXTERN(void) jpeg_default_qtables JPP((j_compress_ptr cinfo,
-                                       boolean force_baseline));
+                                      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));
+                                      const unsigned int *basic_table,
+                                      int scale_factor,
+                                      boolean force_baseline));
 EXTERN(int) jpeg_quality_scaling JPP((int quality));
 EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo));
 EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo,
-                                       boolean suppress));
+                                      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));
+                                     boolean write_all_tables));
 EXTERN(JDIMENSION) jpeg_write_scanlines JPP((j_compress_ptr cinfo,
-                                             JSAMPARRAY scanlines,
-                                             JDIMENSION num_lines));
+                                            JSAMPARRAY scanlines,
+                                            JDIMENSION num_lines));
 EXTERN(void) jpeg_finish_compress JPP((j_compress_ptr cinfo));
 
 /* Precalculate JPEG dimensions for current compression parameters. */
@@ -1012,25 +1019,25 @@ EXTERN(void) jpeg_calc_jpeg_dimensions 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));
+                                           JSAMPIMAGE data,
+                                           JDIMENSION num_lines));
 
 /* Write a special marker.  See libjpeg.txt concerning safe usage. */
 EXTERN(void) jpeg_write_marker
-        JPP((j_compress_ptr cinfo, int marker,
-             const JOCTET * dataptr, unsigned int datalen));
+       JPP((j_compress_ptr cinfo, int marker,
+            const JOCTET * dataptr, unsigned int datalen));
 /* Same, but piecemeal. */
 EXTERN(void) jpeg_write_m_header
-        JPP((j_compress_ptr cinfo, int marker, unsigned int datalen));
+       JPP((j_compress_ptr cinfo, int marker, unsigned int datalen));
 EXTERN(void) jpeg_write_m_byte
-        JPP((j_compress_ptr cinfo, int val));
+       JPP((j_compress_ptr cinfo, int val));
 
 /* 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));
+                                 boolean require_image));
 /* Return value is one of: */
 #define JPEG_SUSPENDED         0 /* Suspended due to lack of input data */
 #define JPEG_HEADER_OK         1 /* Found valid image datastream */
@@ -1044,19 +1051,19 @@ EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo,
 /* Main entry points for decompression */
 EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo));
 EXTERN(JDIMENSION) jpeg_read_scanlines JPP((j_decompress_ptr cinfo,
-                                            JSAMPARRAY scanlines,
-                                            JDIMENSION max_lines));
+                                           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));
+                                          JSAMPIMAGE data,
+                                          JDIMENSION max_lines));
 
 /* Additional entry points for buffered-image mode. */
 EXTERN(boolean) jpeg_has_multiple_scans JPP((j_decompress_ptr cinfo));
 EXTERN(boolean) jpeg_start_output JPP((j_decompress_ptr cinfo,
-                                       int scan_number));
+                                      int scan_number));
 EXTERN(boolean) jpeg_finish_output JPP((j_decompress_ptr cinfo));
 EXTERN(boolean) jpeg_input_complete JPP((j_decompress_ptr cinfo));
 EXTERN(void) jpeg_new_colormap JPP((j_decompress_ptr cinfo));
@@ -1074,20 +1081,20 @@ EXTERN(void) jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo));
 
 /* Control saving of COM and APPn markers into marker_list. */
 EXTERN(void) jpeg_save_markers
-        JPP((j_decompress_ptr cinfo, int marker_code,
-             unsigned int length_limit));
+       JPP((j_decompress_ptr cinfo, int marker_code,
+            unsigned int length_limit));
 
 /* 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));
+       JPP((j_decompress_ptr cinfo, int marker_code,
+            jpeg_marker_parser_method routine));
 
 /* Read or write raw DCT coefficients --- useful for lossless transcoding. */
 EXTERN(jvirt_barray_ptr *) jpeg_read_coefficients JPP((j_decompress_ptr cinfo));
 EXTERN(void) jpeg_write_coefficients JPP((j_compress_ptr cinfo,
-                                          jvirt_barray_ptr * coef_arrays));
+                                         jvirt_barray_ptr * coef_arrays));
 EXTERN(void) jpeg_copy_critical_parameters JPP((j_decompress_ptr srcinfo,
-                                                j_compress_ptr dstinfo));
+                                               j_compress_ptr dstinfo));
 
 /* If you choose to abort compression or decompression before completing
  * jpeg_finish_(de)compress, then you need to clean up to release memory,
@@ -1106,7 +1113,7 @@ 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,
-                                            int desired));
+                                           int desired));
 
 
 /* These marker codes are exported since applications and data source modules
index 8753a36..9d11f70 100644 (file)
@@ -231,7 +231,7 @@ select_ncolors (j_decompress_ptr cinfo, int Ncolors[])
       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 with this pass */
+       break;                  /* won't fit, done with this pass */
       Ncolors[j]++;            /* OK, apply the increment */
       total_colors = (int) temp;
       changed = TRUE;
@@ -284,8 +284,8 @@ create_colormap (j_decompress_ptr cinfo)
   /* Report selected color counts */
   if (cinfo->out_color_components == 3)
     TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
-             total_colors, cquantize->Ncolors[0],
-             cquantize->Ncolors[1], cquantize->Ncolors[2]);
+            total_colors, cquantize->Ncolors[0],
+            cquantize->Ncolors[1], cquantize->Ncolors[2]);
   else
     TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);
 
@@ -310,9 +310,9 @@ create_colormap (j_decompress_ptr cinfo)
       val = output_value(cinfo, i, j, nci-1);
       /* Fill in all colormap entries that have this value of this component */
       for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
-        /* fill in blksize entries beginning at ptr */
-        for (k = 0; k < blksize; k++)
-          colormap[i][ptr+k] = (JSAMPLE) val;
+       /* fill in blksize entries beginning at ptr */
+       for (k = 0; k < blksize; k++)
+         colormap[i][ptr+k] = (JSAMPLE) val;
       }
     }
     blkdist = blksize;         /* blksize of this color is blkdist of next */
@@ -374,15 +374,15 @@ create_colorindex (j_decompress_ptr cinfo)
     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);
+       k = largest_input_value(cinfo, i, ++val, nci-1);
       /* premultiply so that no multiplication needed in main processing */
       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];
+       indexptr[-j] = indexptr[0];
+       indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
       }
   }
 }
@@ -402,7 +402,7 @@ make_odither_array (j_decompress_ptr cinfo, int ncolors)
 
   odither = (ODITHER_MATRIX_PTR)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(ODITHER_MATRIX));
+                               SIZEOF(ODITHER_MATRIX));
   /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1).
    * Hence the dither value for the matrix cell with fill order f
    * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1).
@@ -412,7 +412,7 @@ make_odither_array (j_decompress_ptr cinfo, int ncolors)
   for (j = 0; j < ODITHER_SIZE; j++) {
     for (k = 0; k < ODITHER_SIZE; k++) {
       num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k])))
-            * MAXJSAMPLE;
+           * MAXJSAMPLE;
       /* Ensure round towards zero despite C's lack of consistency
        * about rounding negative values in integer division...
        */
@@ -425,7 +425,7 @@ make_odither_array (j_decompress_ptr cinfo, int ncolors)
 
 /*
  * Create the ordered-dither tables.
- * Components having the same number of representative colors may
+ * Components having the same number of representative colors may 
  * share a dither table.
  */
 
@@ -441,8 +441,8 @@ create_odither_tables (j_decompress_ptr cinfo)
     odither = NULL;            /* search for matching prior component */
     for (j = 0; j < i; j++) {
       if (nci == cquantize->Ncolors[j]) {
-        odither = cquantize->odither[j];
-        break;
+       odither = cquantize->odither[j];
+       break;
       }
     }
     if (odither == NULL)       /* need a new table? */
@@ -458,7 +458,7 @@ create_odither_tables (j_decompress_ptr cinfo)
 
 METHODDEF(void)
 color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
-                JSAMPARRAY output_buf, int num_rows)
+               JSAMPARRAY output_buf, int num_rows)
 /* General case, no dithering */
 {
   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
@@ -476,7 +476,7 @@ color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
     for (col = width; col > 0; col--) {
       pixcode = 0;
       for (ci = 0; ci < nc; ci++) {
-        pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]);
+       pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]);
       }
       *ptrout++ = (JSAMPLE) pixcode;
     }
@@ -486,7 +486,7 @@ color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
 
 METHODDEF(void)
 color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
-                 JSAMPARRAY output_buf, int num_rows)
+                JSAMPARRAY output_buf, int num_rows)
 /* Fast path for out_color_components==3, no dithering */
 {
   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
@@ -514,7 +514,7 @@ color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
 
 METHODDEF(void)
 quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
-                     JSAMPARRAY output_buf, int num_rows)
+                    JSAMPARRAY output_buf, int num_rows)
 /* General case, with ordered dithering */
 {
   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
@@ -532,7 +532,7 @@ quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
   for (row = 0; row < num_rows; row++) {
     /* Initialize output values to 0 so can process components separately */
     FMEMZERO((void FAR *) output_buf[row],
-             (size_t) (width * SIZEOF(JSAMPLE)));
+            (size_t) (width * SIZEOF(JSAMPLE)));
     row_index = cquantize->row_index;
     for (ci = 0; ci < nc; ci++) {
       input_ptr = input_buf[row] + ci;
@@ -542,17 +542,17 @@ quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
       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;
+       /* 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 */
@@ -564,7 +564,7 @@ quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
 
 METHODDEF(void)
 quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
-                      JSAMPARRAY output_buf, int num_rows)
+                     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;
@@ -593,11 +593,11 @@ quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
 
     for (col = width; col > 0; col--) {
       pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) +
-                                        dither0[col_index]]);
+                                       dither0[col_index]]);
       pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) +
-                                        dither1[col_index]]);
+                                       dither1[col_index]]);
       pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) +
-                                        dither2[col_index]]);
+                                       dither2[col_index]]);
       *output_ptr++ = (JSAMPLE) pixcode;
       col_index = (col_index + 1) & ODITHER_MASK;
     }
@@ -609,7 +609,7 @@ quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
 
 METHODDEF(void)
 quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
-                    JSAMPARRAY output_buf, int num_rows)
+                   JSAMPARRAY output_buf, int num_rows)
 /* General case, with Floyd-Steinberg dithering */
 {
   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
@@ -637,22 +637,22 @@ quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
   for (row = 0; row < num_rows; row++) {
     /* Initialize output values to 0 so can process components separately */
     FMEMZERO((void FAR *) output_buf[row],
-             (size_t) (width * SIZEOF(JSAMPLE)));
+            (size_t) (width * SIZEOF(JSAMPLE)));
     for (ci = 0; ci < nc; ci++) {
       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) * nc; /* so point to rightmost pixel */
-        output_ptr += width-1;
-        dir = -1;
-        dirnc = -nc;
-        errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
+       /* work right to left in this row */
+       input_ptr += (width-1) * nc; /* so point to rightmost pixel */
+       output_ptr += width-1;
+       dir = -1;
+       dirnc = -nc;
+       errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
       } else {
-        /* work left to right in this row */
-        dir = 1;
-        dirnc = nc;
-        errorptr = cquantize->fserrors[ci]; /* => entry before first column */
+       /* work left to right in this row */
+       dir = 1;
+       dirnc = nc;
+       errorptr = cquantize->fserrors[ci]; /* => entry before first column */
       }
       colorindex_ci = cquantize->colorindex[ci];
       colormap_ci = cquantize->sv_colormap[ci];
@@ -662,47 +662,47 @@ quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
       belowerr = bpreverr = 0;
 
       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
-         * round the error term (which is expressed * 16) to an integer.
-         * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
-         * for either sign of the error value.
-         * Note: errorptr points to *previous* column's array entry.
-         */
-        cur = RIGHT_SHIFT(cur + errorptr[dir] + 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.
-         */
-        cur += GETJSAMPLE(*input_ptr);
-        cur = GETJSAMPLE(range_limit[cur]);
-        /* Select output value, accumulate into output code for this pixel */
-        pixcode = GETJSAMPLE(colorindex_ci[cur]);
-        *output_ptr += (JSAMPLE) pixcode;
-        /* Compute actual representation error at this pixel */
-        /* Note: we can do this even though we don't have the final */
-        /* pixel code, because the colormap is orthogonal. */
-        cur -= GETJSAMPLE(colormap_ci[pixcode]);
-        /* Compute error fractions to be propagated to adjacent pixels.
-         * Add these into the running sums, and simultaneously shift the
-         * next-line error sums left by 1 column.
-         */
-        bnexterr = cur;
-        delta = cur * 2;
-        cur += delta;          /* form error * 3 */
-        errorptr[0] = (FSERROR) (bpreverr + cur);
-        cur += delta;          /* form error * 5 */
-        bpreverr = belowerr + cur;
-        belowerr = bnexterr;
-        cur += delta;          /* form error * 7 */
-        /* At this point cur contains the 7/16 error value to be propagated
-         * 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 += dirnc;    /* advance input ptr to next column */
-        output_ptr += dir;     /* advance output ptr to next column */
-        errorptr += dir;       /* advance errorptr to current column */
+       /* 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
+        * round the error term (which is expressed * 16) to an integer.
+        * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
+        * for either sign of the error value.
+        * Note: errorptr points to *previous* column's array entry.
+        */
+       cur = RIGHT_SHIFT(cur + errorptr[dir] + 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.
+        */
+       cur += GETJSAMPLE(*input_ptr);
+       cur = GETJSAMPLE(range_limit[cur]);
+       /* Select output value, accumulate into output code for this pixel */
+       pixcode = GETJSAMPLE(colorindex_ci[cur]);
+       *output_ptr += (JSAMPLE) pixcode;
+       /* Compute actual representation error at this pixel */
+       /* Note: we can do this even though we don't have the final */
+       /* pixel code, because the colormap is orthogonal. */
+       cur -= GETJSAMPLE(colormap_ci[pixcode]);
+       /* Compute error fractions to be propagated to adjacent pixels.
+        * Add these into the running sums, and simultaneously shift the
+        * next-line error sums left by 1 column.
+        */
+       bnexterr = cur;
+       delta = cur * 2;
+       cur += delta;           /* form error * 3 */
+       errorptr[0] = (FSERROR) (bpreverr + cur);
+       cur += delta;           /* form error * 5 */
+       bpreverr = belowerr + cur;
+       belowerr = bnexterr;
+       cur += delta;           /* form error * 7 */
+       /* At this point cur contains the 7/16 error value to be propagated
+        * 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 += dirnc;     /* advance input ptr to next column */
+       output_ptr += dir;      /* advance output ptr to next column */
+       errorptr += dir;        /* advance errorptr to current column */
       }
       /* Post-loop cleanup: we must unload the final error value into the
        * final fserrors[] entry.  Note we need not unload belowerr because
@@ -825,7 +825,7 @@ jinit_1pass_quantizer (j_decompress_ptr cinfo)
 
   cquantize = (my_cquantize_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_cquantizer));
+                               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;
index 2ac3f84..38fc2af 100644 (file)
@@ -42,7 +42,7 @@
  * 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
@@ -223,7 +223,7 @@ typedef my_cquantizer * my_cquantize_ptr;
 
 METHODDEF(void)
 prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
-                  JSAMPARRAY output_buf, int num_rows)
+                 JSAMPARRAY output_buf, int num_rows)
 {
   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
   register JSAMPROW ptr;
@@ -238,11 +238,11 @@ prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
     for (col = width; col > 0; col--) {
       /* get pixel value and index into the histogram */
       histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
-                         [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
-                         [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
+                        [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
+                        [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
       /* increment, check for overflow and undo increment if so. */
       if (++(*histp) <= 0)
-        (*histp)--;
+       (*histp)--;
       ptr += 3;
     }
   }
@@ -265,9 +265,9 @@ typedef struct {
   INT32 volume;
   /* The number of nonzero histogram cells within this box */
   long colorcount;
-} _box;
+} box;
 
-typedef _box * boxptr;
+typedef box * boxptr;
 
 
 LOCAL(boxptr)
@@ -279,7 +279,7 @@ find_biggest_color_pop (boxptr boxlist, int numboxes)
   register int i;
   register long maxc = 0;
   boxptr which = NULL;
-
+  
   for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
     if (boxp->colorcount > maxc && boxp->volume > 0) {
       which = boxp;
@@ -299,7 +299,7 @@ find_biggest_volume (boxptr boxlist, int numboxes)
   register int i;
   register INT32 maxv = 0;
   boxptr which = NULL;
-
+  
   for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
     if (boxp->volume > maxv) {
       which = boxp;
@@ -322,75 +322,75 @@ update_box (j_decompress_ptr cinfo, boxptr boxp)
   int c0min,c0max,c1min,c1max,c2min,c2max;
   INT32 dist0,dist1,dist2;
   long ccount;
-
+  
   c0min = boxp->c0min;  c0max = boxp->c0max;
   c1min = boxp->c1min;  c1max = boxp->c1max;
   c2min = boxp->c2min;  c2max = boxp->c2max;
-
+  
   if (c0max > c0min)
     for (c0 = c0min; c0 <= c0max; c0++)
       for (c1 = c1min; c1 <= c1max; c1++) {
-        histp = & histogram[c0][c1][c2min];
-        for (c2 = c2min; c2 <= c2max; c2++)
-          if (*histp++ != 0) {
-            boxp->c0min = c0min = c0;
-            goto have_c0min;
-          }
+       histp = & histogram[c0][c1][c2min];
+       for (c2 = c2min; c2 <= c2max; c2++)
+         if (*histp++ != 0) {
+           boxp->c0min = c0min = c0;
+           goto have_c0min;
+         }
       }
  have_c0min:
   if (c0max > c0min)
     for (c0 = c0max; c0 >= c0min; c0--)
       for (c1 = c1min; c1 <= c1max; c1++) {
-        histp = & histogram[c0][c1][c2min];
-        for (c2 = c2min; c2 <= c2max; c2++)
-          if (*histp++ != 0) {
-            boxp->c0max = c0max = c0;
-            goto have_c0max;
-          }
+       histp = & histogram[c0][c1][c2min];
+       for (c2 = c2min; c2 <= c2max; c2++)
+         if (*histp++ != 0) {
+           boxp->c0max = c0max = c0;
+           goto have_c0max;
+         }
       }
  have_c0max:
   if (c1max > c1min)
     for (c1 = c1min; c1 <= c1max; c1++)
       for (c0 = c0min; c0 <= c0max; c0++) {
-        histp = & histogram[c0][c1][c2min];
-        for (c2 = c2min; c2 <= c2max; c2++)
-          if (*histp++ != 0) {
-            boxp->c1min = c1min = c1;
-            goto have_c1min;
-          }
+       histp = & histogram[c0][c1][c2min];
+       for (c2 = c2min; c2 <= c2max; c2++)
+         if (*histp++ != 0) {
+           boxp->c1min = c1min = c1;
+           goto have_c1min;
+         }
       }
  have_c1min:
   if (c1max > c1min)
     for (c1 = c1max; c1 >= c1min; c1--)
       for (c0 = c0min; c0 <= c0max; c0++) {
-        histp = & histogram[c0][c1][c2min];
-        for (c2 = c2min; c2 <= c2max; c2++)
-          if (*histp++ != 0) {
-            boxp->c1max = c1max = c1;
-            goto have_c1max;
-          }
+       histp = & histogram[c0][c1][c2min];
+       for (c2 = c2min; c2 <= c2max; c2++)
+         if (*histp++ != 0) {
+           boxp->c1max = c1max = c1;
+           goto have_c1max;
+         }
       }
  have_c1max:
   if (c2max > c2min)
     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_C2_ELEMS)
-          if (*histp != 0) {
-            boxp->c2min = c2min = c2;
-            goto have_c2min;
-          }
+       histp = & histogram[c0][c1min][c2];
+       for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
+         if (*histp != 0) {
+           boxp->c2min = c2min = c2;
+           goto have_c2min;
+         }
       }
  have_c2min:
   if (c2max > c2min)
     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_C2_ELEMS)
-          if (*histp != 0) {
-            boxp->c2max = c2max = c2;
-            goto have_c2max;
-          }
+       histp = & histogram[c0][c1min][c2];
+       for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
+         if (*histp != 0) {
+           boxp->c2max = c2max = c2;
+           goto have_c2max;
+         }
       }
  have_c2max:
 
@@ -406,16 +406,16 @@ update_box (j_decompress_ptr cinfo, boxptr boxp)
   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;
   for (c0 = c0min; c0 <= c0max; c0++)
     for (c1 = c1min; c1 <= c1max; c1++) {
       histp = & histogram[c0][c1][c2min];
       for (c2 = c2min; c2 <= c2max; c2++, histp++)
-        if (*histp != 0) {
-          ccount++;
-        }
+       if (*histp != 0) {
+         ccount++;
+       }
     }
   boxp->colorcount = ccount;
 }
@@ -423,7 +423,7 @@ update_box (j_decompress_ptr cinfo, boxptr boxp)
 
 LOCAL(int)
 median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes,
-            int desired_colors)
+           int desired_colors)
 /* Repeatedly select and split the largest box until we have enough boxes */
 {
   int n,lb;
@@ -512,24 +512,24 @@ compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
   long c0total = 0;
   long c1total = 0;
   long c2total = 0;
-
+  
   c0min = boxp->c0min;  c0max = boxp->c0max;
   c1min = boxp->c1min;  c1max = boxp->c1max;
   c2min = boxp->c2min;  c2max = boxp->c2max;
-
+  
   for (c0 = c0min; c0 <= c0max; c0++)
     for (c1 = c1min; c1 <= c1max; c1++) {
       histp = & histogram[c0][c1][c2min];
       for (c2 = c2min; c2 <= c2max; c2++) {
-        if ((count = *histp++) != 0) {
-          total += 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;
-        }
+       if ((count = *histp++) != 0) {
+         total += 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;
+       }
       }
     }
-
+  
   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);
@@ -546,7 +546,7 @@ select_colors (j_decompress_ptr cinfo, int desired_colors)
 
   /* Allocate workspace for box list */
   boxlist = (boxptr) (*cinfo->mem->alloc_small)
-    ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(_box));
+    ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box));
   /* Initialize one box containing whole space */
   numboxes = 1;
   boxlist[0].c0min = 0;
@@ -645,7 +645,7 @@ select_colors (j_decompress_ptr cinfo, int desired_colors)
 
 LOCAL(int)
 find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
-                    JSAMPLE colorlist[])
+                   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
  * is specified by the center coordinates of its first cell.  The number of
@@ -702,11 +702,11 @@ find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
       /* within cell range so no contribution to min_dist */
       min_dist = 0;
       if (x <= centerc0) {
-        tdist = (x - maxc0) * C0_SCALE;
-        max_dist = tdist*tdist;
+       tdist = (x - maxc0) * C0_SCALE;
+       max_dist = tdist*tdist;
       } else {
-        tdist = (x - minc0) * C0_SCALE;
-        max_dist = tdist*tdist;
+       tdist = (x - minc0) * C0_SCALE;
+       max_dist = tdist*tdist;
       }
     }
 
@@ -724,11 +724,11 @@ find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
     } else {
       /* within cell range so no contribution to min_dist */
       if (x <= centerc1) {
-        tdist = (x - maxc1) * C1_SCALE;
-        max_dist += tdist*tdist;
+       tdist = (x - maxc1) * C1_SCALE;
+       max_dist += tdist*tdist;
       } else {
-        tdist = (x - minc1) * C1_SCALE;
-        max_dist += tdist*tdist;
+       tdist = (x - minc1) * C1_SCALE;
+       max_dist += tdist*tdist;
       }
     }
 
@@ -746,11 +746,11 @@ find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
     } else {
       /* within cell range so no contribution to min_dist */
       if (x <= centerc2) {
-        tdist = (x - maxc2) * C2_SCALE;
-        max_dist += tdist*tdist;
+       tdist = (x - maxc2) * C2_SCALE;
+       max_dist += tdist*tdist;
       } else {
-        tdist = (x - minc2) * C2_SCALE;
-        max_dist += tdist*tdist;
+       tdist = (x - minc2) * C2_SCALE;
+       max_dist += tdist*tdist;
       }
     }
 
@@ -774,7 +774,7 @@ find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
 
 LOCAL(void)
 find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
-                  int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
+                 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.
  * Return the indexes of the closest entries in the bestcolor[] array.
@@ -798,17 +798,17 @@ find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
   bptr = bestdist;
   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.
    */
-
+  
   /* Nominal steps between cell centers ("x" in Thomas article) */
 #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 */
@@ -830,20 +830,20 @@ find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
       dist1 = dist0;
       xx1 = inc1;
       for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
-        dist2 = dist1;
-        xx2 = inc2;
-        for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
-          if (dist2 < *bptr) {
-            *bptr = dist2;
-            *cptr = (JSAMPLE) icolor;
-          }
-          dist2 += xx2;
-          xx2 += 2 * STEP_C2 * STEP_C2;
-          bptr++;
-          cptr++;
-        }
-        dist1 += xx1;
-        xx1 += 2 * STEP_C1 * STEP_C1;
+       dist2 = dist1;
+       xx2 = inc2;
+       for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
+         if (dist2 < *bptr) {
+           *bptr = dist2;
+           *cptr = (JSAMPLE) icolor;
+         }
+         dist2 += xx2;
+         xx2 += 2 * STEP_C2 * STEP_C2;
+         bptr++;
+         cptr++;
+       }
+       dist1 += xx1;
+       xx1 += 2 * STEP_C1 * STEP_C1;
       }
       dist0 += xx0;
       xx0 += 2 * STEP_C0 * STEP_C0;
@@ -882,7 +882,7 @@ fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
   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.
    */
@@ -890,7 +890,7 @@ fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
 
   /* Determine the actually nearest colors. */
   find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
-                   bestcolor);
+                  bestcolor);
 
   /* Save the best color numbers (plus 1) in the main cache array */
   c0 <<= BOX_C0_LOG;           /* convert ID back to base cell indexes */
@@ -901,7 +901,7 @@ fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
     for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
       cachep = & histogram[c0+ic0][c1+ic1][c2];
       for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
-        *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
+       *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
       }
     }
   }
@@ -914,7 +914,7 @@ fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
 
 METHODDEF(void)
 pass2_no_dither (j_decompress_ptr cinfo,
-                 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
+                JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
 /* This version performs no dithering */
 {
   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
@@ -938,7 +938,7 @@ pass2_no_dither (j_decompress_ptr cinfo,
       /* If we have not seen this color before, find nearest colormap entry */
       /* and update the cache */
       if (*cachep == 0)
-        fill_inverse_cmap(cinfo, c0,c1,c2);
+       fill_inverse_cmap(cinfo, c0,c1,c2);
       /* Now emit the colormap index for this cell */
       *outptr++ = (JSAMPLE) (*cachep - 1);
     }
@@ -948,7 +948,7 @@ pass2_no_dither (j_decompress_ptr cinfo,
 
 METHODDEF(void)
 pass2_fs_dither (j_decompress_ptr cinfo,
-                 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
+                JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
 /* This version performs Floyd-Steinberg dithering */
 {
   my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
@@ -1029,14 +1029,14 @@ pass2_fs_dither (j_decompress_ptr cinfo,
       /* If we have not seen this color before, find nearest colormap */
       /* entry and update the cache */
       if (*cachep == 0)
-        fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_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;
-        /* Compute representation error for this pixel */
-        cur0 -= GETJSAMPLE(colormap0[pixcode]);
-        cur1 -= GETJSAMPLE(colormap1[pixcode]);
-        cur2 -= GETJSAMPLE(colormap2[pixcode]);
+       *outptr = (JSAMPLE) pixcode;
+       /* Compute representation error for this pixel */
+       cur0 -= GETJSAMPLE(colormap0[pixcode]);
+       cur1 -= GETJSAMPLE(colormap1[pixcode]);
+       cur2 -= GETJSAMPLE(colormap2[pixcode]);
       }
       /* Compute error fractions to be propagated to adjacent pixels.
        * Add these into the running sums, and simultaneously shift the
@@ -1044,30 +1044,30 @@ pass2_fs_dither (j_decompress_ptr cinfo,
        */
       { register LOCFSERROR bnexterr, delta;
 
-        bnexterr = cur0;       /* Process component 0 */
-        delta = cur0 * 2;
-        cur0 += delta;         /* form error * 3 */
-        errorptr[0] = (FSERROR) (bpreverr0 + cur0);
-        cur0 += delta;         /* form error * 5 */
-        bpreverr0 = belowerr0 + cur0;
-        belowerr0 = bnexterr;
-        cur0 += delta;         /* form error * 7 */
-        bnexterr = cur1;       /* Process component 1 */
-        delta = cur1 * 2;
-        cur1 += delta;         /* form error * 3 */
-        errorptr[1] = (FSERROR) (bpreverr1 + cur1);
-        cur1 += delta;         /* form error * 5 */
-        bpreverr1 = belowerr1 + cur1;
-        belowerr1 = bnexterr;
-        cur1 += delta;         /* form error * 7 */
-        bnexterr = cur2;       /* Process component 2 */
-        delta = cur2 * 2;
-        cur2 += delta;         /* form error * 3 */
-        errorptr[2] = (FSERROR) (bpreverr2 + cur2);
-        cur2 += delta;         /* form error * 5 */
-        bpreverr2 = belowerr2 + cur2;
-        belowerr2 = bnexterr;
-        cur2 += delta;         /* form error * 7 */
+       bnexterr = cur0;        /* Process component 0 */
+       delta = cur0 * 2;
+       cur0 += delta;          /* form error * 3 */
+       errorptr[0] = (FSERROR) (bpreverr0 + cur0);
+       cur0 += delta;          /* form error * 5 */
+       bpreverr0 = belowerr0 + cur0;
+       belowerr0 = bnexterr;
+       cur0 += delta;          /* form error * 7 */
+       bnexterr = cur1;        /* Process component 1 */
+       delta = cur1 * 2;
+       cur1 += delta;          /* form error * 3 */
+       errorptr[1] = (FSERROR) (bpreverr1 + cur1);
+       cur1 += delta;          /* form error * 5 */
+       bpreverr1 = belowerr1 + cur1;
+       belowerr1 = bnexterr;
+       cur1 += delta;          /* form error * 7 */
+       bnexterr = cur2;        /* Process component 2 */
+       delta = cur2 * 2;
+       cur2 += delta;          /* form error * 3 */
+       errorptr[2] = (FSERROR) (bpreverr2 + cur2);
+       cur2 += delta;          /* form error * 5 */
+       bpreverr2 = belowerr2 + cur2;
+       belowerr2 = bnexterr;
+       cur2 += delta;          /* form error * 7 */
       }
       /* At this point curN contains the 7/16 error value to be propagated
        * to the next pixel on the current line, and all the errors for the
@@ -1198,16 +1198,16 @@ start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
 
     if (cinfo->dither_mode == JDITHER_FS) {
       size_t arraysize = (size_t) ((cinfo->output_width + 2) *
-                                   (3 * SIZEOF(FSERROR)));
+                                  (3 * SIZEOF(FSERROR)));
       /* Allocate Floyd-Steinberg workspace if we didn't already. */
       if (cquantize->fserrors == NULL)
-        cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
-          ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
+       cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
+         ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
       /* Initialize the propagated errors to zero. */
       FMEMZERO((void FAR *) cquantize->fserrors, arraysize);
       /* Make the error-limit table if we didn't already. */
       if (cquantize->error_limiter == NULL)
-        init_error_limit(cinfo);
+       init_error_limit(cinfo);
       cquantize->on_odd_row = FALSE;
     }
 
@@ -1216,7 +1216,7 @@ start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
   if (cquantize->needs_zeroed) {
     for (i = 0; i < HIST_C0_ELEMS; i++) {
       FMEMZERO((void FAR *) histogram[i],
-               HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
+              HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
     }
     cquantize->needs_zeroed = FALSE;
   }
@@ -1249,7 +1249,7 @@ jinit_2pass_quantizer (j_decompress_ptr cinfo)
 
   cquantize = (my_cquantize_ptr)
     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
-                                SIZEOF(my_cquantizer));
+                               SIZEOF(my_cquantizer));
   cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
   cquantize->pub.start_pass = start_pass_2_quant;
   cquantize->pub.new_color_map = new_color_map_2_quant;
index 49a8ce2..5b16b6d 100644 (file)
@@ -175,8 +175,8 @@ jzero_far (void FAR * target, size_t bytestozero)
 
 GLOBAL(void)
 jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
-                   JSAMPARRAY output_array, int dest_row,
-                   int num_rows, JDIMENSION num_cols)
+                  JSAMPARRAY output_array, int dest_row,
+                  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 may overlap for duplication.
@@ -209,7 +209,7 @@ jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
 
 GLOBAL(void)
 jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
-                 JDIMENSION num_blocks)
+                JDIMENSION num_blocks)
 /* Copy a row of coefficient blocks from one place to another. */
 {
 #ifdef FMEMCOPY
index 232085f..0740b31 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * jversion.h
  *
- * Copyright (C) 1991-2013, Thomas G. Lane, Guido Vollbeding.
+ * Copyright (C) 1991-2016, Thomas G. Lane, Guido Vollbeding.
  * 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       "9  13-Jan-2013"
+#define JVERSION       "9b  17-Jan-2016"
 
-#define JCOPYRIGHT     "Copyright (C) 2013, Thomas G. Lane, Guido Vollbeding"
+#define JCOPYRIGHT     "Copyright (C) 2016, Thomas G. Lane, Guido Vollbeding"