From 34dd7839c1bbbbe6257d47c31bf38edc7455f175 Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Tue, 7 Jul 2026 13:49:40 +0200 Subject: [PATCH] Update bundled JPEG library to version 10 dated 25-Jan-2026. --- documentation/src/bundled-libs.dox | 4 +- jpeg/README | 12 +- jpeg/change.log | 17 + jpeg/fltk_jpeg_prefix.h | 4 +- jpeg/install.txt | 67 +- jpeg/jccolor.c | 7 +- jpeg/jcdctmgr.c | 103 ++- jpeg/jchuff.c | 8 +- jpeg/jcinit.c | 4 +- jpeg/jcmarker.c | 4 +- jpeg/jcparam.c | 5 +- jpeg/jctrans.c | 3 +- jpeg/jdcolor.c | 7 +- jpeg/jdct.h | 30 +- jpeg/jddctmgr.c | 72 +- jpeg/jdmarker.c | 4 +- jpeg/jdmaster.c | 4 +- jpeg/jerror.c | 13 +- jpeg/jerror.h | 6 +- jpeg/jfdctint.c | 956 ++++++++++++++++---------- jpeg/jidctfst.c | 128 ++-- jpeg/jidctint.c | 1032 +++++++++++++++++----------- jpeg/jmorecfg.h | 64 +- jpeg/jpeglib.h | 16 +- jpeg/jversion.h | 6 +- jpeg/usage.txt | 6 +- 26 files changed, 1597 insertions(+), 985 deletions(-) diff --git a/documentation/src/bundled-libs.dox b/documentation/src/bundled-libs.dox index c58d30713..8a80002cc 100644 --- a/documentation/src/bundled-libs.dox +++ b/documentation/src/bundled-libs.dox @@ -23,10 +23,10 @@ The nanosvg library is not affected. \section bundled-status Current status \code -Current versions of bundled libraries (as of Jan 20, 2026): +Current versions of bundled libraries (as of Jul 7, 2026): Library Version/git commit Release date FLTK Version -------------------------------------------------------------------------- - jpeg jpeg-9f 2024-01-14 1.4.0 + jpeg jpeg-10 2026-01-26 1.5.0 nanosvg 7aeda550a8 [1] 2023-12-02 1.4.0 png libpng-1.6.54 2026-01-12 1.5.0 zlib zlib-1.3.1 2024-01-22 1.4.0 diff --git a/jpeg/README b/jpeg/README index 0f4ee1522..c0a8593e2 100644 --- a/jpeg/README +++ b/jpeg/README @@ -1,7 +1,7 @@ The Independent JPEG Group's JPEG software ========================================== -README for release 9f of 14-Jan-2024 +README for release 10 of 25-Jan-2026 ==================================== This distribution contains the ninth public release of the Independent JPEG @@ -116,7 +116,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-2024, Thomas G. Lane, Guido Vollbeding. +This software is copyright (C) 1991-2026, Thomas G. Lane, Guido Vollbeding. All Rights Reserved except as specified below. Permission is hereby granted to use, copy, modify, and distribute this @@ -240,9 +240,9 @@ 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 in Windows-compatible "zip" archive format as -https://www.ijg.org/files/jpegsr9f.zip, and +https://www.ijg.org/files/jpegsr10.zip, and in Unix-compatible "tar.gz" archive format as -https://www.ijg.org/files/jpegsrc.v9f.tar.gz. +https://www.ijg.org/files/jpegsrc.v10.tar.gz. The JPEG FAQ (Frequently Asked Questions) article is a source of some general information about JPEG. @@ -366,9 +366,9 @@ is currently prepared and will be published later. TO DO ===== -Version 9 is the second release of a new generation JPEG standard +Version 10 is the third release of a new generation JPEG standard to overcome the limitations of the original JPEG specification, -and is the first true source reference JPEG codec. +and is a true source reference JPEG codec. More features are being prepared for coming releases... Please send bug reports, offers of help, etc. to jpeg-info@ijg.org. diff --git a/jpeg/change.log b/jpeg/change.log index eae011312..f5137f2cc 100644 --- a/jpeg/change.log +++ b/jpeg/change.log @@ -1,6 +1,23 @@ CHANGE LOG for Independent JPEG Group's JPEG software +Version 10 25-Jan-2026 +----------------------- + +jmorecfg.h: add JPEG_DATA_PRECISION parameter alongside and independent +of BITS_IN_JSAMPLE, enabling higher bit depth support with backward +compatibility and preparing the next standard for file interchange. + +Add lossless negate function in jpegtran. +Thank to Ulf-Dietrich Braumann for suggestion. + +Enable processing of images using reversible color tranform +with various bit depths by jpegtran. + +Add Windows 64 Bit (Modern) platform support in the C++Builder/RAD Studio +build, remove ARM (32-bit) platform support in the Visual Studio build. + + Version 9f 14-Jan-2024 ----------------------- diff --git a/jpeg/fltk_jpeg_prefix.h b/jpeg/fltk_jpeg_prefix.h index 834d90ce6..960bc0be2 100644 --- a/jpeg/fltk_jpeg_prefix.h +++ b/jpeg/fltk_jpeg_prefix.h @@ -27,8 +27,6 @@ #define _jinit_memory_mgr fltk__jinit_memory_mgr #define _jinit_merged_upsampler fltk__jinit_merged_upsampler #define _jinit_upsampler fltk__jinit_upsampler -#define _jpeg_CreateCompress fltk__jpeg_CreateCompress -#define _jpeg_CreateDecompress fltk__jpeg_CreateDecompress #define _jpeg_abort fltk__jpeg_abort #define _jpeg_abort_compress fltk__jpeg_abort_compress #define _jpeg_abort_decompress fltk__jpeg_abort_decompress @@ -41,6 +39,8 @@ #define _jpeg_consume_input fltk__jpeg_consume_input #define _jpeg_copy_critical_parameters fltk__jpeg_copy_critical_parameters #define _jpeg_core_output_dimensions fltk__jpeg_core_output_dimensions +#define _jpeg_CreateCompress fltk__jpeg_CreateCompress +#define _jpeg_CreateDecompress fltk__jpeg_CreateDecompress #define _jpeg_default_colorspace fltk__jpeg_default_colorspace #define _jpeg_default_qtables fltk__jpeg_default_qtables #define _jpeg_destroy fltk__jpeg_destroy diff --git a/jpeg/install.txt b/jpeg/install.txt index 9670a0444..5ca4a890a 100644 --- a/jpeg/install.txt +++ b/jpeg/install.txt @@ -1,6 +1,6 @@ INSTALLATION INSTRUCTIONS for the Independent JPEG Group's JPEG software -Copyright (C) 1991-2023, Thomas G. Lane, Guido Vollbeding. +Copyright (C) 1991-2025, 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. @@ -142,7 +142,7 @@ Makefile jconfig file System and/or compiler makefile.manx jconfig.manx Amiga, Manx Aztec C makefile.sas jconfig.sas Amiga, SAS C makeproj.mac jconfig.mac Apple Macintosh, Metrowerks CodeWarrior -makefile.xc jconfig.xc Apple Mac, Xcode 15 +makefile.xc jconfig.xc Apple Mac, Xcode 26 mak*jpeg.st jconfig.st Atari ST/Mega/STE/TT/Falcon, Pure C or Turbo C makefile.bcc jconfig.bcc MS-DOS or OS/2, Borland C makefile.dj jconfig.dj MS-DOS, DJGPP (Delorie's port of GNU C) @@ -151,15 +151,16 @@ makefile.wat jconfig.wat MS-DOS, OS/2, or Windows NT, Watcom C makefile.vc jconfig.vc Windows, MS Visual C++ makefile.vs jconfig.vc Windows, MS Visual C++ 6 Developer Studio make*.vc6 -makefile.vs jconfig.vc Windows, Visual Studio 2019-2022 version 16-17 +makefile.vs jconfig.vc Windows, Visual Studio 2019-2026 version 16-18 make*.v16 -makefile.b32 jconfig.vc Windows, C++Builder/RAD Studio 10.4-11 +makefile.b32 jconfig.vc Windows, C++Builder/RAD Studio 12 mak*jpeg.bcb makefile.b32 jconfig.vc Windows, Embarcadero C++ for Win32 (bcc32) makefile.c32 jconfig.vc Windows, Embarcadero C++ for Win32 (bcc32c) makefile.d32 makefile.x32 jconfig.vc Windows, Embarcadero C++ for Win32 (bcc32x) makefile.b64 jconfig.vc Windows, Embarcadero C++ for Win64 (bcc64) +makefile.x64 jconfig.vc Windows, Embarcadero C++ for Win64/COFF (bcc64x) makefile.mms jconfig.vms Digital VMS, with MMS software makefile.vms jconfig.vms Digital VMS, without MMS software @@ -1061,20 +1062,20 @@ To use: appropriate location on your path. -Microsoft Windows, Microsoft Visual Studio 2019-2022 version 16-17: +Microsoft Windows, Microsoft Visual Studio 2019-2026 version 16-18: We include makefiles that should work as project files in Visual Studio -2019-2022 version 16-17 or later. There is a library makefile that -builds the IJG library as a static Win32/x64/ARM/ARM64/ARM64EC library, +2019-2026 version 16-18 or later. There is a library makefile that +builds the IJG library as a static Win32/x64/ARM64/ARM64EC library, and application makefiles that build the sample applications as -Win32/x64/ARM/ARM64/ARM64EC console applications. (Even if you -only want the library, we recommend building the applications -so that you can run the self-test.) +Win32/x64/ARM64/ARM64EC console applications. (Even if you only +want the library, we recommend building the applications so that +you can run the self-test.) To use: 1. Ensure you’ve checked the item "Desktop development with C++" in the Workloads tab of Visual Studio Installer. - Open the Developer Command Prompt for VS 2019-2022, change to the + Open the Developer Command Prompt for VS 2019-2026, change to the source directory and execute the command line nmake /f makefile.vs setup-v16 This will move jconfig.vc to jconfig.h and makefiles to project files. @@ -1085,7 +1086,7 @@ To use: which allows to repeat the setup later. 2. Open the solution file jpeg.sln, build the library project. a) If you are using Visual Studio more recent than - 2022 version 17, you'll possibly get a message + 2026 version 18, you'll possibly get a message saying that the project files are being updated. b) If necessary, open the project properties and adapt the Windows Target Platform Version (or Windows SDK Version) @@ -1099,32 +1100,33 @@ To use: or Individual components tab). d) If you want to build x64 code, change the platform setting from Win32 to x64. You can build Win32 and x64 versions side by side. - e) If you want to build ARM/ARM64/ARM64EC code, change the platform - setting to ARM/ARM64/ARM64EC. Ensure you’ve installed the - ARM/ARM64/ARM64EC build tools in Visual Studio Installer + e) If you want to build ARM64/ARM64EC code, change the platform + setting to ARM64/ARM64EC. Ensure you’ve installed the + ARM64/ARM64EC build tools in Visual Studio Installer (Workloads tab Optional components or Individual components tab). - You can build Win32/x64/ARM/ARM64/ARM64EC versions side by side. + You can build Win32/x64/ARM64/ARM64EC versions side by side. 3. Open the solution file apps.sln, build the application projects. 4. To perform the self-test, execute the command line nmake /f makefile.vs test-32 for the Win32 build, or on a 64-bit system nmake /f makefile.vs test-64 - for the x64 build. - Note: test options for ARM code will be added - as soon as Windows on ARM reference devices are available. - To date there are no Windows on ARM reference devices available. + for the x64 build. On an ARM system use + nmake /f makefile.vs test-arm64 + for the ARM64 build, and + nmake /f makefile.vs test-arm64ec + for the ARM64EC build. 5. Move the application .exe files from the Release folder to an appropriate location on your path. -Microsoft Windows, Embarcadero C++Builder/RAD Studio 10.4-11: +Microsoft Windows, Embarcadero C++Builder/RAD Studio 12: We include makefiles that should work as project files in C++Builder/RAD -Studio 10.4-11 or later. There is a library makefile that builds the -IJG library as a static Win32/Win64 library, and application makefiles -that build the sample applications as Win32/Win64 console applications. -(Even if you only want the library, we recommend building the -applications so that you can run the self-test.) +Studio 12 or later. There is a library makefile that builds the +IJG library as a static Win32/Win64/Win64x library, and application +makefiles that build the sample applications as Win32/Win64/Win64x +console applications. (Even if you only want the library, we recommend +building the applications so that you can run the self-test.) To use: 1. Open the RAD Studio Command Prompt (or the standard Windows Command @@ -1139,22 +1141,25 @@ To use: This will create renamed copies of the files, which allows to repeat the setup later. (You can use one of the other makefiles .c32, - .d32, .x32, or .b64 instead of .b32.) + .d32, .x32, .b64, or .x64 instead of .b32.) 2. Open the project file jpeg.cbproj, build the library project. a) By default, the classic Borland compiler (bcc32) is used for the Win32 build. To enable the Clang-based compiler for a specific project, select Project > Options > C++ Compiler and deselect the Use classic Borland compiler option. - b) If you want to build Win64 code, change the platform setting - from Windows 32 Bit to Windows 64 Bit. - You can build Win32 and Win64 versions side by side. + b) If you want to build Win64/Win64x code, change the platform + setting from Windows 32 Bit to Windows 64 Bit/Windows 64 Bit + (Modern). + You can build Win32/Win64/Win64x versions side by side. 3. Open the project group file apps.groupproj, build the application projects. 4. To perform the self-test, execute the command line make -fmakefile.b32 test-32 for the Win32 build, or on a 64-bit system make -fmakefile.b32 test-64 - for the Win64 build. + for the Win64 build, and + make -fmakefile.b32 test-64x + for the Win64x build. 5. Move the application .exe files from the Release folder to an appropriate location on your path. diff --git a/jpeg/jccolor.c b/jpeg/jccolor.c index c028dd9db..696076aec 100644 --- a/jpeg/jccolor.c +++ b/jpeg/jccolor.c @@ -2,7 +2,7 @@ * jccolor.c * * Copyright (C) 1991-1996, Thomas G. Lane. - * Modified 2011-2023 by Guido Vollbeding. + * Modified 2011-2025 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. * @@ -476,8 +476,9 @@ jinit_color_converter (j_compress_ptr cinfo) /* Support color transform only for RGB colorspaces */ if (cinfo->color_transform && - cinfo->jpeg_color_space != JCS_RGB && - cinfo->jpeg_color_space != JCS_BG_RGB) + (cinfo->LSE_maxtrans != MAXJSAMPLE || + (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 */ diff --git a/jpeg/jcdctmgr.c b/jpeg/jcdctmgr.c index a48ccd814..6f4a90aed 100644 --- a/jpeg/jcdctmgr.c +++ b/jpeg/jcdctmgr.c @@ -2,7 +2,7 @@ * jcdctmgr.c * * Copyright (C) 1994-1996, Thomas G. Lane. - * Modified 2003-2020 by Guido Vollbeding. + * Modified 2003-2025 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. * @@ -50,18 +50,6 @@ typedef union { } 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. - */ -#ifdef DCT_ISLOW_SUPPORTED -#define PROVIDE_ISLOW_TABLES -#else -#ifdef DCT_SCALING_SUPPORTED -#define PROVIDE_ISLOW_TABLES -#endif -#endif - - /* * Perform forward DCT on one or more blocks of a component. * @@ -102,9 +90,9 @@ forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr, * 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. + * 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 @@ -185,7 +173,7 @@ start_pass_fdctmgr (j_compress_ptr cinfo) my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; int ci, qtblno, i; jpeg_component_info *compptr; - int method = 0; + J_DCT_METHOD method = JDCT_DEFAULT; JQUANT_TBL * qtbl; DCTELEM * dtbl; @@ -194,6 +182,13 @@ start_pass_fdctmgr (j_compress_ptr cinfo) /* Select the proper DCT routine for this component's scaling */ switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) { #ifdef DCT_SCALING_SUPPORTED +/* + * The current scaled-DCT routines require ISLOW-style divisor tables, + * so be sure to compile that code if either ISLOW or SCALING is requested. + */ +#ifndef PROVIDE_ISLOW_TABLES +#define PROVIDE_ISLOW_TABLES +#endif case ((1 << 8) + 1): fdct->do_dct[ci] = jpeg_fdct_1x1; method = JDCT_ISLOW; /* jfdctint uses islow-style table */ @@ -323,14 +318,34 @@ start_pass_fdctmgr (j_compress_ptr cinfo) switch (cinfo->dct_method) { #ifdef DCT_ISLOW_SUPPORTED case JDCT_ISLOW: +#ifndef PROVIDE_ISLOW_TABLES +#define PROVIDE_ISLOW_TABLES +#endif fdct->do_dct[ci] = jpeg_fdct_islow; method = JDCT_ISLOW; break; #endif #ifdef DCT_IFAST_SUPPORTED case JDCT_IFAST: +#if BITS_IN_JSAMPLE < JPEG_DATA_PRECISION || \ + BITS_IN_JSAMPLE > JPEG_DATA_PRECISION + 8 + /* + * Adjustment of divisor tables in JDCT_IFAST + * below doesn't work well in this condition. + * Use JDCT_ISLOW instead. + */ +#ifndef PROVIDE_ISLOW_TABLES +#define PROVIDE_ISLOW_TABLES +#endif + fdct->do_dct[ci] = jpeg_fdct_islow; + method = JDCT_ISLOW; +#else +#ifndef PROVIDE_IFAST_TABLES +#define PROVIDE_IFAST_TABLES +#endif fdct->do_dct[ci] = jpeg_fdct_ifast; method = JDCT_IFAST; +#endif break; #endif #ifdef DCT_FLOAT_SUPPORTED @@ -357,7 +372,7 @@ start_pass_fdctmgr (j_compress_ptr cinfo) switch (method) { #ifdef PROVIDE_ISLOW_TABLES case JDCT_ISLOW: - /* For LL&M IDCT method, divisors are equal to raw quantization + /* For LL&M FDCT method, divisors are equal to raw quantization * coefficients multiplied by 8 (to counteract scaling). */ dtbl = (DCTELEM *) compptr->dct_table; @@ -368,14 +383,15 @@ start_pass_fdctmgr (j_compress_ptr cinfo) fdct->pub.forward_DCT[ci] = forward_DCT; break; #endif -#ifdef DCT_IFAST_SUPPORTED +#ifdef PROVIDE_IFAST_TABLES case JDCT_IFAST: { - /* For AA&N IDCT method, divisors are equal to quantization + /* For AA&N FDCT 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. + * We apply a further scale factor of 8 + * with adjustment if necessary. */ #define CONST_BITS 14 static const INT16 aanscales[DCTSIZE2] = { @@ -392,11 +408,20 @@ start_pass_fdctmgr (j_compress_ptr cinfo) 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); + if (compptr->component_needed) { + for (i = 0; i < DCTSIZE2; i++) { + dtbl[i] = (DCTELEM) + DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], + (INT32) aanscales[i]), + CONST_BITS+JPEG_DATA_PRECISION-BITS_IN_JSAMPLE-4); + } + } else { + for (i = 0; i < DCTSIZE2; i++) { + dtbl[i] = (DCTELEM) + DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i], + (INT32) aanscales[i]), + CONST_BITS+JPEG_DATA_PRECISION-BITS_IN_JSAMPLE-3); + } } } fdct->pub.forward_DCT[ci] = forward_DCT; @@ -405,11 +430,12 @@ 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 + /* For float AA&N FDCT 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. + * We apply a further scale factor of 8 + * with adjustment if necessary. * What's actually stored is 1/divisor so that the inner loop can * use a multiplication rather than a division. */ @@ -419,6 +445,7 @@ start_pass_fdctmgr (j_compress_ptr cinfo) 1.0, 1.387039845, 1.306562965, 1.175875602, 1.0, 0.785694958, 0.541196100, 0.275899379 }; +#if BITS_IN_JSAMPLE == JPEG_DATA_PRECISION i = 0; for (row = 0; row < DCTSIZE; row++) { @@ -427,6 +454,26 @@ start_pass_fdctmgr (j_compress_ptr cinfo) (1.0 / ((double) qtbl->quantval[i] * aanscalefactor[row] * aanscalefactor[col] * (compptr->component_needed ? 16.0 : 8.0))); +#else + double extrafactor = compptr->component_needed ? 16.0 : 8.0; + + /* Adjust extra factor */ +#if BITS_IN_JSAMPLE < JPEG_DATA_PRECISION + i = JPEG_DATA_PRECISION - BITS_IN_JSAMPLE; + do { extrafactor *= 0.5; } while (--i); +#else + i = BITS_IN_JSAMPLE - JPEG_DATA_PRECISION; + do { extrafactor *= 2.0; } while (--i); +#endif + + 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] * + extrafactor)); +#endif i++; } } diff --git a/jpeg/jchuff.c b/jpeg/jchuff.c index 1f527b218..d12e3cf2c 100644 --- a/jpeg/jchuff.c +++ b/jpeg/jchuff.c @@ -2,7 +2,7 @@ * jchuff.c * * Copyright (C) 1991-1997, Thomas G. Lane. - * Modified 2006-2023 by Guido Vollbeding. + * Modified 2006-2025 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,9 +26,9 @@ /* The legal range of a DCT coefficient is - * -1024 .. +1023 for 8-bit sample data precision; - * -16384 .. +16383 for 12-bit sample data precision. - * Hence the magnitude should always fit in sample data precision + 2 bits. + * -1024 .. +1023 for 8-bit JPEG data precision; + * -16384 .. +16383 for 12-bit JPEG data precision. + * Hence the magnitude should always fit in JPEG data precision + 2 bits. */ /* Derived data constructed for each Huffman table */ diff --git a/jpeg/jcinit.c b/jpeg/jcinit.c index 2aea7ca2a..e38f203a9 100644 --- a/jpeg/jcinit.c +++ b/jpeg/jcinit.c @@ -2,7 +2,7 @@ * jcinit.c * * Copyright (C) 1991-1997, Thomas G. Lane. - * Modified 2003-2017 by Guido Vollbeding. + * Modified 2003-2025 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 @@ jinit_compress_master (j_compress_ptr cinfo) JDIMENSION jd_samplesperrow; /* For now, precision must match compiled-in value... */ - if (cinfo->data_precision != BITS_IN_JSAMPLE) + if (cinfo->data_precision != JPEG_DATA_PRECISION) ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); /* Sanity check on input image dimensions */ diff --git a/jpeg/jcmarker.c b/jpeg/jcmarker.c index 8874cd867..13db077d2 100644 --- a/jpeg/jcmarker.c +++ b/jpeg/jcmarker.c @@ -2,7 +2,7 @@ * jcmarker.c * * Copyright (C) 1991-1998, Thomas G. Lane. - * Modified 2003-2019 by Guido Vollbeding. + * Modified 2003-2025 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. * @@ -296,7 +296,7 @@ emit_lse_ict (j_compress_ptr cinfo) emit_2bytes(cinfo, 24); /* fixed length */ emit_byte(cinfo, 0x0D); /* ID inverse transform specification */ - emit_2bytes(cinfo, MAXJSAMPLE); /* MAXTRANS */ + emit_2bytes(cinfo, (int) cinfo->LSE_maxtrans); /* MAXTRANS */ emit_byte(cinfo, 3); /* Nt=3 */ emit_byte(cinfo, cinfo->comp_info[1].component_id); emit_byte(cinfo, cinfo->comp_info[0].component_id); diff --git a/jpeg/jcparam.c b/jpeg/jcparam.c index 261ae86ca..0929d48e6 100644 --- a/jpeg/jcparam.c +++ b/jpeg/jcparam.c @@ -2,7 +2,7 @@ * jcparam.c * * Copyright (C) 1991-1998, Thomas G. Lane. - * Modified 2003-2022 by Guido Vollbeding. + * Modified 2003-2025 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. * @@ -215,7 +215,7 @@ jpeg_set_defaults (j_compress_ptr cinfo) cinfo->scale_num = 1; /* 1:1 scaling */ cinfo->scale_denom = 1; - cinfo->data_precision = BITS_IN_JSAMPLE; + cinfo->data_precision = JPEG_DATA_PRECISION; /* Set up two quantization tables using default quality of 75 */ jpeg_set_quality(cinfo, 75, TRUE); /* Reset standard Huffman tables */ @@ -283,6 +283,7 @@ jpeg_set_defaults (j_compress_ptr cinfo) /* No color transform */ cinfo->color_transform = JCT_NONE; + cinfo->LSE_maxtrans = MAXJSAMPLE; /* Default LSE MAXTRANS value */ /* Choose JPEG colorspace based on input space, set defaults accordingly */ diff --git a/jpeg/jctrans.c b/jpeg/jctrans.c index 261dd2996..ec5ecf700 100644 --- a/jpeg/jctrans.c +++ b/jpeg/jctrans.c @@ -2,7 +2,7 @@ * jctrans.c * * Copyright (C) 1995-1998, Thomas G. Lane. - * Modified 2000-2020 by Guido Vollbeding. + * Modified 2000-2025 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. * @@ -91,6 +91,7 @@ jpeg_copy_critical_parameters (j_decompress_ptr srcinfo, * entropy coding mode dependent on image data precision. */ dstinfo->color_transform = srcinfo->color_transform; + dstinfo->LSE_maxtrans = srcinfo->LSE_maxtrans; jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space); dstinfo->data_precision = srcinfo->data_precision; dstinfo->arith_code = srcinfo->data_precision > 8 ? TRUE : FALSE; diff --git a/jpeg/jdcolor.c b/jpeg/jdcolor.c index 6b40fb534..4df3663e5 100644 --- a/jpeg/jdcolor.c +++ b/jpeg/jdcolor.c @@ -2,7 +2,7 @@ * jdcolor.c * * Copyright (C) 1991-1997, Thomas G. Lane. - * Modified 2011-2023 by Guido Vollbeding. + * Modified 2011-2025 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. * @@ -640,8 +640,9 @@ jinit_color_deconverter (j_decompress_ptr cinfo) /* Support color transform only for RGB colorspaces */ if (cinfo->color_transform && - cinfo->jpeg_color_space != JCS_RGB && - cinfo->jpeg_color_space != JCS_BG_RGB) + (cinfo->LSE_maxtrans != MAXJSAMPLE || + (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. diff --git a/jpeg/jdct.h b/jpeg/jdct.h index 0f251590c..e70856a3f 100644 --- a/jpeg/jdct.h +++ b/jpeg/jdct.h @@ -2,7 +2,7 @@ * jdct.h * * Copyright (C) 1994-1996, Thomas G. Lane. - * Modified 2002-2023 by Guido Vollbeding. + * Modified 2002-2025 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. * @@ -17,9 +17,9 @@ /* * A forward DCT routine is given a pointer to an input sample array and * a pointer to a work area of type DCTELEM[]; the DCT is to be performed - * in-place in that buffer. Type DCTELEM is int for 8-bit samples, INT32 - * for 12-bit samples. (NOTE: Floating-point DCT implementations use an - * array of type FAST_FLOAT, instead.) + * in-place in that buffer. Type DCTELEM is int or INT32, depending on + * bit depth parameters. (NOTE: Floating-point DCT implementations use + * an array of type FAST_FLOAT, instead.) * The input data is to be fetched from the sample array starting at a * specified column. (Any row offset needed will be applied to the array * pointer before it is passed to the FDCT code.) @@ -32,7 +32,20 @@ * Quantization of the output coefficients is done by jcdctmgr.c. */ -#if BITS_IN_JSAMPLE == 8 +/* Condition for FDCT: + * BITS_IN_JSAMPLE <= 10 && JPEG_DATA_PRECISION <= 10 + * Condition for int IDCT where DCTELEM is used (2x2, 1x1, 2x1, 1x2): + * JPEG_DATA_PRECISION + RANGE_BITS <= 12 + * [BITS_IN_JSAMPLE - 1 + RANGE_BITS + + * 3 - (BITS_IN_JSAMPLE - JPEG_DATA_PRECISION) <= 14] + * {3 - (BITS_IN_JSAMPLE - JPEG_DATA_PRECISION) = PASS2_BITS - PASS1_BITS} + * Condition for fast IDCT where DCTELEM is used: + * JPEG_DATA_PRECISION <= 10 && BITS_IN_JSAMPLE <= 13 && RANGE_BITS <= 2 + * [BITS_IN_JSAMPLE - 1 + RANGE_BITS + PASS2BITS <= 14] + * Combined for all: + */ + +#if BITS_IN_JSAMPLE <= 10 && JPEG_DATA_PRECISION <= 10 && RANGE_BITS <= 2 typedef int DCTELEM; /* 16 or 32 bits is fine */ #else typedef INT32 DCTELEM; /* must have 32 bits */ @@ -64,9 +77,10 @@ typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data, */ typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */ -#if BITS_IN_JSAMPLE == 8 +#if JPEG_DATA_PRECISION <= 10 && BITS_IN_JSAMPLE <= 13 typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */ -#define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */ +#define IFAST_SCALE_BITS (10 - JPEG_DATA_PRECISION) + /* fractional bits in scale factors */ #else typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */ #define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */ @@ -394,7 +408,7 @@ EXTERN(void) jpeg_idct_1x2 #ifdef RIGHT_SHIFT_IS_UNSIGNED #define ISHIFT_TEMPS DCTELEM ishift_temp; -#if BITS_IN_JSAMPLE == 8 +#if BITS_IN_JSAMPLE <= 10 && JPEG_DATA_PRECISION <= 10 && RANGE_BITS <= 2 #define DCTELEMBITS 16 /* DCTELEM may be 16 or 32 bits */ #else #define DCTELEMBITS 32 /* DCTELEM must be 32 bits */ diff --git a/jpeg/jddctmgr.c b/jpeg/jddctmgr.c index 9ecfbb510..542b95def 100644 --- a/jpeg/jddctmgr.c +++ b/jpeg/jddctmgr.c @@ -2,7 +2,7 @@ * jddctmgr.c * * Copyright (C) 1994-1996, Thomas G. Lane. - * Modified 2002-2013 by Guido Vollbeding. + * Modified 2002-2025 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. * @@ -46,8 +46,8 @@ typedef struct { /* This array contains the IDCT method code that each multiplier table * is currently set up for, or -1 if it's not yet set up. - * The actual multiplier tables are pointed to by dct_table in the - * per-component comp_info structures. + * The actual multiplier tables are pointed to by dct_table + * in the per-component comp_info structures. */ int cur_method[MAX_COMPONENTS]; } my_idct_controller; @@ -68,18 +68,6 @@ typedef union { } multiplier_table; -/* The current scaled-IDCT routines require ISLOW-style multiplier tables, - * so be sure to compile that code if either ISLOW or SCALING is requested. - */ -#ifdef DCT_ISLOW_SUPPORTED -#define PROVIDE_ISLOW_TABLES -#else -#ifdef IDCT_SCALING_SUPPORTED -#define PROVIDE_ISLOW_TABLES -#endif -#endif - - /* * Prepare for an output pass. * Here we select the proper IDCT routine for each component and build @@ -101,6 +89,13 @@ start_pass (j_decompress_ptr cinfo) /* Select the proper IDCT routine for this component's scaling */ switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) { #ifdef IDCT_SCALING_SUPPORTED +/* + * The current scaled-IDCT routines require ISLOW-style multiplier tables, + * so be sure to compile that code if either ISLOW or SCALING is requested. + */ +#ifndef PROVIDE_ISLOW_TABLES +#define PROVIDE_ISLOW_TABLES +#endif case ((1 << 8) + 1): method_ptr = jpeg_idct_1x1; method = JDCT_ISLOW; /* jidctint uses islow-style table */ @@ -230,6 +225,9 @@ start_pass (j_decompress_ptr cinfo) switch (cinfo->dct_method) { #ifdef DCT_ISLOW_SUPPORTED case JDCT_ISLOW: +#ifndef PROVIDE_ISLOW_TABLES +#define PROVIDE_ISLOW_TABLES +#endif method_ptr = jpeg_idct_islow; method = JDCT_ISLOW; break; @@ -248,21 +246,19 @@ start_pass (j_decompress_ptr cinfo) #endif default: ERREXIT(cinfo, JERR_NOT_COMPILED); - break; } break; default: ERREXIT2(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size); - break; } idct->pub.inverse_DCT[ci] = method_ptr; /* Create multiplier table from quant table. * However, we can skip this if the component is uninteresting * or if we already built the table. Also, if no quant table * has yet been saved for the component, we leave the - * multiplier table all-zero; we'll be reading zeroes from the - * coefficient controller's buffer anyway. + * multiplier table all-zero; we'll be reading zeroes + * from the coefficient controller's buffer anyway. */ if (! compptr->component_needed || idct->cur_method[ci] == method) continue; @@ -325,7 +321,8 @@ start_pass (j_decompress_ptr cinfo) * 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. + * We apply a further scale factor of 1/8 + * with adjustment if necessary. */ FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table; int row, col; @@ -333,13 +330,31 @@ start_pass (j_decompress_ptr cinfo) 1.0, 1.387039845, 1.306562965, 1.175875602, 1.0, 0.785694958, 0.541196100, 0.275899379 }; +#if JPEG_DATA_PRECISION == BITS_IN_JSAMPLE 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); + fmtbl[i] = (FLOAT_MULT_TYPE) ((double) qtbl->quantval[i] * + aanscalefactor[row] * aanscalefactor[col] * 0.125); +#else + double extrafactor = 0.125; + + /* Adjust extra factor */ +#if JPEG_DATA_PRECISION < BITS_IN_JSAMPLE + i = BITS_IN_JSAMPLE - JPEG_DATA_PRECISION; + do { extrafactor *= 2.0; } while (--i); +#else + i = JPEG_DATA_PRECISION - BITS_IN_JSAMPLE; + do { extrafactor *= 0.5; } while (--i); +#endif + + 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] * extrafactor); +#endif i++; } } @@ -348,7 +363,6 @@ start_pass (j_decompress_ptr cinfo) #endif default: ERREXIT(cinfo, JERR_NOT_COMPILED); - break; } } } @@ -365,18 +379,16 @@ jinit_inverse_dct (j_decompress_ptr cinfo) int ci; jpeg_component_info *compptr; - idct = (my_idct_ptr) - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, - SIZEOF(my_idct_controller)); + idct = (my_idct_ptr) (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, 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; ci++, compptr++) { /* 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)); + compptr->dct_table = (*cinfo->mem->alloc_small) + ((j_common_ptr) cinfo, JPOOL_IMAGE, 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; diff --git a/jpeg/jdmarker.c b/jpeg/jdmarker.c index c10fde605..f529e0db6 100644 --- a/jpeg/jdmarker.c +++ b/jpeg/jdmarker.c @@ -2,7 +2,7 @@ * jdmarker.c * * Copyright (C) 1991-1998, Thomas G. Lane. - * Modified 2009-2019 by Guido Vollbeding. + * Modified 2009-2025 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. * @@ -664,7 +664,7 @@ get_lse (j_decompress_ptr cinfo) if (tmp != 0x0D) /* ID inverse transform specification */ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker); INPUT_2BYTES(cinfo, tmp, return FALSE); - if (tmp != MAXJSAMPLE) goto bad; /* MAXTRANS */ + cinfo->LSE_maxtrans = (UINT16) tmp; /* MAXTRANS */ INPUT_BYTE(cinfo, tmp, return FALSE); if (tmp != 3) goto bad; /* Nt=3 */ INPUT_BYTE(cinfo, cid, return FALSE); diff --git a/jpeg/jdmaster.c b/jpeg/jdmaster.c index 3070b7bb4..4b7887104 100644 --- a/jpeg/jdmaster.c +++ b/jpeg/jdmaster.c @@ -2,7 +2,7 @@ * jdmaster.c * * Copyright (C) 1991-1997, Thomas G. Lane. - * Modified 2002-2020 by Guido Vollbeding. + * Modified 2002-2025 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. * @@ -267,7 +267,7 @@ master_selection (j_decompress_ptr cinfo) JDIMENSION jd_samplesperrow; /* For now, precision must match compiled-in value... */ - if (cinfo->data_precision != BITS_IN_JSAMPLE) + if (cinfo->data_precision != JPEG_DATA_PRECISION) ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); /* Initialize dimensions and other stuff */ diff --git a/jpeg/jerror.c b/jpeg/jerror.c index 7163af699..58db6ff0d 100644 --- a/jpeg/jerror.c +++ b/jpeg/jerror.c @@ -2,7 +2,7 @@ * jerror.c * * Copyright (C) 1991-1998, Thomas G. Lane. - * Modified 2012-2015 by Guido Vollbeding. + * Modified 2012-2025 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. * @@ -237,9 +237,18 @@ jpeg_std_error (struct jpeg_error_mgr * err) err->format_message = format_message; err->reset_error_mgr = reset_error_mgr; + err->msg_code = 0; /* may be useful as a flag for "no error" */ + err->msg_parm.i[0] = 0; /* initialize 8 int message parameters */ + err->msg_parm.i[1] = 0; + err->msg_parm.i[2] = 0; + err->msg_parm.i[3] = 0; + err->msg_parm.i[4] = 0; + err->msg_parm.i[5] = 0; + err->msg_parm.i[6] = 0; + err->msg_parm.i[7] = 0; + err->trace_level = 0; /* default = no tracing */ err->num_warnings = 0; /* no warnings emitted yet */ - err->msg_code = 0; /* may be useful as a flag for "no error" */ /* Initialize message table pointers */ err->jpeg_message_table = jpeg_std_message_table; diff --git a/jpeg/jerror.h b/jpeg/jerror.h index db608b9c0..b7a2d6063 100644 --- a/jpeg/jerror.h +++ b/jpeg/jerror.h @@ -2,7 +2,7 @@ * jerror.h * * Copyright (C) 1994-1997, Thomas G. Lane. - * Modified 1997-2018 by Guido Vollbeding. + * Modified 1997-2026 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. * @@ -130,7 +130,7 @@ JMESSAGE(JTRC_ADOBE, "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d") JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u") JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u") -JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x") +JMESSAGE(JTRC_DAC, "Define Arithmetic Conditioning 0x%02x: 0x%02x") JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x") JMESSAGE(JTRC_DQT, "Define Quantization Table %d precision %d") JMESSAGE(JTRC_DRI, "Define Restart Interval %u") @@ -156,7 +156,7 @@ JMESSAGE(JTRC_SMOOTH_NOTIMPL, "Smoothing not supported with nonstandard sampling ratios") JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d") JMESSAGE(JTRC_SOF_COMPONENT, " Component %d: %dhx%dv q=%d") -JMESSAGE(JTRC_SOI, "Start of Image") +JMESSAGE(JTRC_SOI, "Start Of Image") JMESSAGE(JTRC_SOS, "Start Of Scan: %d components") JMESSAGE(JTRC_SOS_COMPONENT, " Component %d: dc=%d ac=%d") JMESSAGE(JTRC_SOS_PARAMS, " Ss=%d, Se=%d, Ah=%d, Al=%d") diff --git a/jpeg/jfdctint.c b/jpeg/jfdctint.c index 05df4750b..023031aa9 100644 --- a/jpeg/jfdctint.c +++ b/jpeg/jfdctint.c @@ -2,7 +2,7 @@ * jfdctint.c * * Copyright (C) 1991-1996, Thomas G. Lane. - * Modification developed 2003-2018 by Guido Vollbeding. + * Modification developed 2003-2026 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,7 +26,7 @@ * We also provide FDCT routines with various input sample block sizes for * direct resolution reduction or enlargement and for direct resolving the * common 2x1 and 1x2 subsampling cases without additional resampling: NxN - * (N=1...16), 2NxN, and Nx2N (N=1...8) pixels for one 8x8 output DCT block. + * (N=1...16), 2NxN, and Nx2N (N=1...8) samples for one 8x8 output DCT block. * * For N<8 we fill the remaining block coefficients with zero. * For N>8 we apply a partial N-point FDCT on the input samples, computing @@ -75,33 +75,37 @@ * a problem to do in integer arithmetic. We multiply all the constants * by CONST_SCALE and convert them to integer constants (thus retaining * CONST_BITS bits of precision in the constants). After doing a - * multiplication we have to divide the product by CONST_SCALE, with proper - * rounding, to produce the correct output. This division can be done - * cheaply as a right shift of CONST_BITS bits. We postpone shifting - * as long as possible so that partial sums can be added together with - * full fractional precision. + * multiplication we have to divide the product by CONST_SCALE, with + * proper rounding, to produce the correct output. This division can + * be done cheaply as a right shift of CONST_BITS bits. We postpone + * shifting as long as possible so that partial sums can be added + * together with full fractional precision. * * The outputs of the first pass are scaled up by PASS1_BITS bits so that * they are represented to better-than-integral precision. These outputs - * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word - * with the recommended scaling. (For 12-bit sample data, the intermediate - * array is INT32 anyway.) + * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit + * word with the recommended scaling. (For higher bit depths, the + * intermediate array is INT32 anyway.) * - * To avoid overflow of the 32-bit intermediate results in pass 2, we must - * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis - * shows that the values given below are the most effective. + * To avoid overflow of the 32-bit intermediate results in pass 2, we + * must have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error + * analysis shows that the values given below are the most effective. */ -#if BITS_IN_JSAMPLE == 8 +#if BITS_IN_JSAMPLE <= 10 && JPEG_DATA_PRECISION <= 10 #define CONST_BITS 13 -#define PASS1_BITS 2 +#define PASS1_BITS (10 - BITS_IN_JSAMPLE) +#define PASS2_BITS (10 - JPEG_DATA_PRECISION) #else +#if BITS_IN_JSAMPLE <= 13 && JPEG_DATA_PRECISION <= 13 #define CONST_BITS 13 -#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ +#define PASS1_BITS (13 - BITS_IN_JSAMPLE) +#define PASS2_BITS (13 - JPEG_DATA_PRECISION) +#endif #endif -/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus - * causing a lot of useless floating-point operations at run time. +/* Some C compilers fail to reduce "FIX(constant)" at compile time, + * thus causing a lot of useless floating-point operations at run time. * To get around this we use the following pre-calculated constants. * If you change CONST_BITS you may want to add appropriate values. * (With a reasonable C compiler, you can just rely on the FIX() macro...) @@ -137,19 +141,37 @@ /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. - * For 8-bit samples with the recommended scaling, all the variable + * For up to 10-bit data with the recommended scaling, all the variable * and constant values involved are no more than 16 bits wide, so a * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. - * For 12-bit samples, a full 32-bit multiplication will be needed. + * For higher bit depths, a full 32-bit multiplication will be needed. */ -#if BITS_IN_JSAMPLE == 8 +#if BITS_IN_JSAMPLE <= 10 && JPEG_DATA_PRECISION <= 10 #define MULTIPLY(var,const) MULTIPLY16C16(var,const) #else #define MULTIPLY(var,const) ((var) * (const)) #endif +/* Pass 1 output: smart scale up. */ + +#if PASS1_BITS > 0 +#define PASS1_OUTPUT(x) (DCTELEM) ((x) << PASS1_BITS) +#else +#define PASS1_OUTPUT(x) (DCTELEM) (x) +#endif + + +/* Pass 2 output: smart scale down. */ + +#if PASS2_BITS > 0 +#define PASS2_OUTPUT(x) (DCTELEM) RIGHT_SHIFT(x, PASS2_BITS) +#else +#define PASS2_OUTPUT(x) (DCTELEM) (x) +#endif + + /* * Perform the forward DCT on one block of samples. */ @@ -195,8 +217,8 @@ jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) ((tmp10 + tmp11 - 8 * CENTERJSAMPLE) << PASS1_BITS); - dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS); + dataptr[0] = PASS1_OUTPUT(tmp10 + tmp11 - 8 * CENTERJSAMPLE); + dataptr[4] = PASS1_OUTPUT(tmp10 - tmp11); z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); /* c6 */ /* Add fudge factor here for final descale. */ @@ -246,8 +268,8 @@ 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. + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. * cK represents sqrt(2) * cos(K*pi/16). */ @@ -263,7 +285,15 @@ jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; /* Add fudge factor here for final descale. */ - tmp10 = tmp0 + tmp3 + (ONE << (PASS1_BITS-1)); +#if PASS2_BITS > 1 + tmp10 = tmp0 + tmp3 + (ONE << (PASS2_BITS-1)); +#else +#if PASS2_BITS > 0 + tmp10 = tmp0 + tmp3 + ONE; +#else + tmp10 = tmp0 + tmp3; +#endif +#endif tmp12 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp13 = tmp1 - tmp2; @@ -273,19 +303,19 @@ jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; tmp3 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; - dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp10 + tmp11, PASS1_BITS); - dataptr[DCTSIZE*4] = (DCTELEM) RIGHT_SHIFT(tmp10 - tmp11, PASS1_BITS); + dataptr[DCTSIZE*0] = PASS2_OUTPUT(tmp10 + tmp11); + dataptr[DCTSIZE*4] = PASS2_OUTPUT(tmp10 - tmp11); z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); /* c6 */ /* Add fudge factor here for final descale. */ - z1 += ONE << (CONST_BITS+PASS1_BITS-1); + z1 += ONE << (CONST_BITS+PASS2_BITS-1); dataptr[DCTSIZE*2] = (DCTELEM) RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*6] = (DCTELEM) RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); /* Odd part per figure 8 --- note paper omits factor of sqrt(2). * i0..i3 in the paper are tmp0..tmp3 here. @@ -296,7 +326,7 @@ jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ /* Add fudge factor here for final descale. */ - z1 += ONE << (CONST_BITS+PASS1_BITS-1); + z1 += ONE << (CONST_BITS+PASS2_BITS-1); tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* -c3+c5 */ tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ @@ -315,10 +345,10 @@ jpeg_fdct_islow (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) RIGHT_SHIFT(tmp1, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) RIGHT_SHIFT(tmp2, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) RIGHT_SHIFT(tmp3, CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -368,8 +398,7 @@ jpeg_fdct_7x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) z1 = tmp0 + tmp2; /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((z1 + tmp1 + tmp3 - 7 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[0] = PASS1_OUTPUT(z1 + tmp1 + tmp3 - 7 * CENTERJSAMPLE); tmp3 += tmp3; z1 -= tmp3; z1 -= tmp3; @@ -404,10 +433,10 @@ jpeg_fdct_7x7 (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. - * We must also scale the output by (8/7)**2 = 64/49, which we fold - * into the constant multipliers: + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. + * We must also scale the output by (8/7)**2 = 64/49, + * which we fold into the constant multipliers: * cK now represents sqrt(2) * cos(K*pi/14) * 64/49. */ @@ -427,20 +456,20 @@ 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+PASS2_BITS); tmp3 += tmp3; z1 -= tmp3; z1 -= tmp3; z1 = MULTIPLY(z1, FIX(0.461784020)); /* (c2+c6-c4)/2 */ z2 = MULTIPLY(tmp0 - tmp2, FIX(1.202428084)); /* (c2+c4-c6)/2 */ z3 = MULTIPLY(tmp1 - tmp2, FIX(0.411026446)); /* c6 */ - dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + z2 + z3, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + z2 + z3, CONST_BITS+PASS2_BITS); z1 -= z2; 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); - dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+PASS2_BITS); /* Odd part */ @@ -454,9 +483,9 @@ jpeg_fdct_7x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp0 += tmp3; tmp2 += tmp3 + MULTIPLY(tmp12, FIX(2.443531355)); /* c3+c1-c5 */ - dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+PASS1_BITS); - dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+PASS1_BITS); - dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -504,8 +533,7 @@ jpeg_fdct_6x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[3]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp10 + tmp11 - 6 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[0] = PASS1_OUTPUT(tmp10 + tmp11 - 6 * CENTERJSAMPLE); dataptr[2] = (DCTELEM) DESCALE(MULTIPLY(tmp12, FIX(1.224744871)), /* c2 */ CONST_BITS-PASS1_BITS); @@ -518,18 +546,24 @@ jpeg_fdct_6x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp10 = DESCALE(MULTIPLY(tmp0 + tmp2, FIX(0.366025404)), /* c5 */ CONST_BITS-PASS1_BITS); +#if PASS1_BITS > 0 dataptr[1] = (DCTELEM) (tmp10 + ((tmp0 + tmp1) << PASS1_BITS)); dataptr[3] = (DCTELEM) ((tmp0 - tmp1 - tmp2) << PASS1_BITS); dataptr[5] = (DCTELEM) (tmp10 + ((tmp2 - tmp1) << PASS1_BITS)); +#else + dataptr[1] = (DCTELEM) (tmp10 + tmp0 + tmp1); + dataptr[3] = (DCTELEM) (tmp0 - tmp1 - tmp2); + dataptr[5] = (DCTELEM) (tmp10 + tmp2 - tmp1); +#endif dataptr += DCTSIZE; /* advance pointer to next row */ } /* Pass 2: process columns. - * 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/6)**2 = 16/9, which we fold - * into the constant multipliers: + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. + * We must also scale the output by (8/6)**2 = 16/9, + * which we fold into the constant multipliers: * cK now represents sqrt(2) * cos(K*pi/12) * 16/9. */ @@ -550,13 +584,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+PASS2_BITS); dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(MULTIPLY(tmp12, FIX(2.177324216)), /* c2 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(1.257078722)), /* c4 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); /* Odd part */ @@ -564,13 +598,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+PASS2_BITS); dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(MULTIPLY(tmp0 - tmp1 - tmp2, FIX(1.777777778)), /* 16/9 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp10 + MULTIPLY(tmp2 - tmp1, FIX(1.777777778)), /* 16/9 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -642,8 +676,8 @@ jpeg_fdct_5x5 (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. + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. * We must also scale the output by (8/5)**2 = 64/25, which we partially * fold into the constant multipliers (other part was done in pass 1): * cK now represents sqrt(2) * cos(K*pi/10) * 32/25. @@ -665,12 +699,12 @@ 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+PASS2_BITS); tmp11 = MULTIPLY(tmp11, FIX(1.011928851)); /* (c2+c4)/2 */ tmp10 -= tmp2 << 2; tmp10 = MULTIPLY(tmp10, FIX(0.452548340)); /* (c2-c4)/2 */ - dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(tmp11 + tmp10, CONST_BITS+PASS1_BITS); - dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp11 - tmp10, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(tmp11 + tmp10, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp11 - tmp10, CONST_BITS+PASS2_BITS); /* Odd part */ @@ -678,10 +712,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+PASS2_BITS); dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.785601151)), /* c1+c3 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -746,8 +780,8 @@ 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. + * We apply the PASS2_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]. */ @@ -756,27 +790,35 @@ jpeg_fdct_4x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) /* Even part */ /* Add fudge factor here for final descale. */ - tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3] + (ONE << (PASS1_BITS-1)); +#if PASS2_BITS > 1 + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3] + (ONE << (PASS2_BITS-1)); +#else +#if PASS2_BITS > 0 + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3] + ONE; +#else + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3]; +#endif +#endif tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*2]; tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*3]; tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*2]; - dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp0 + tmp1, PASS1_BITS); - dataptr[DCTSIZE*2] = (DCTELEM) RIGHT_SHIFT(tmp0 - tmp1, PASS1_BITS); + dataptr[DCTSIZE*0] = PASS2_OUTPUT(tmp0 + tmp1); + dataptr[DCTSIZE*2] = PASS2_OUTPUT(tmp0 - tmp1); /* Odd part */ tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ /* Add fudge factor here for final descale. */ - tmp0 += ONE << (CONST_BITS+PASS1_BITS-1); + tmp0 += ONE << (CONST_BITS+PASS2_BITS-1); dataptr[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*3] = (DCTELEM) RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -835,8 +877,8 @@ jpeg_fdct_3x3 (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. + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. * We must also scale the output by (8/3)**2 = 64/9, which we partially * fold into the constant multipliers (other part was done in pass 1): * cK now represents sqrt(2) * cos(K*pi/6) * 16/9. @@ -853,16 +895,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+PASS2_BITS); dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(1.257078722)), /* c2 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); /* Odd part */ dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(MULTIPLY(tmp2, FIX(2.177324216)), /* c1 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -905,12 +947,23 @@ jpeg_fdct_2x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) /* Column 0 */ /* Apply unsigned->signed conversion. */ - data[DCTSIZE*0] = (tmp0 + tmp2 - 4 * CENTERJSAMPLE) << 4; - data[DCTSIZE*1] = (tmp0 - tmp2) << 4; + +#if PASS2_BITS < PASS1_BITS + 4 + data[DCTSIZE*0] = + (tmp0 + tmp2 - 4 * CENTERJSAMPLE) << (4+PASS1_BITS-PASS2_BITS); + data[DCTSIZE*1] = (tmp0 - tmp2) << (4+PASS1_BITS-PASS2_BITS); /* Column 1 */ - data[DCTSIZE*0+1] = (tmp1 + tmp3) << 4; - data[DCTSIZE*1+1] = (tmp1 - tmp3) << 4; + data[DCTSIZE*0+1] = (tmp1 + tmp3) << (4+PASS1_BITS-PASS2_BITS); + data[DCTSIZE*1+1] = (tmp1 - tmp3) << (4+PASS1_BITS-PASS2_BITS); +#else + data[DCTSIZE*0] = tmp0 + tmp2 - 4 * CENTERJSAMPLE; + data[DCTSIZE*1] = tmp0 - tmp2; + + /* Column 1 */ + data[DCTSIZE*0+1] = tmp1 + tmp3; + data[DCTSIZE*1+1] = tmp1 - tmp3; +#endif } @@ -931,10 +984,25 @@ jpeg_fdct_1x1 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION 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] = (dcval - CENTERJSAMPLE) << 6; + data[0] = (dcval - CENTERJSAMPLE) << (6+PASS1_BITS-PASS2_BITS); } +/* Pass 1 bits decrement scaling for block sizes 9, 10, 11. */ + +#if PASS1_BITS > 0 +#define PASS1_DECR (PASS1_BITS - 1) +#else +#define PASS1_DECR 0 +#endif + +#if PASS1_DECR > 0 +#define PASS1_OUTDEC(x) (DCTELEM) ((x) << PASS1_DECR) +#else +#define PASS1_OUTDEC(x) (DCTELEM) (x) +#endif + + /* * Perform the forward DCT on a 9x9 sample block. */ @@ -954,8 +1022,7 @@ jpeg_fdct_9x9 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) /* 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. + * furthermore, we scale the results by 2**PASS1_DECR. * cK represents sqrt(2) * cos(K*pi/18). */ @@ -980,35 +1047,38 @@ jpeg_fdct_9x9 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) z1 = tmp0 + tmp2 + tmp3; z2 = tmp1 + tmp4; /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) ((z1 + z2 - 9 * CENTERJSAMPLE) << 1); + dataptr[0] = PASS1_OUTDEC(z1 + z2 - 9 * CENTERJSAMPLE); dataptr[6] = (DCTELEM) DESCALE(MULTIPLY(z1 - z2 - z2, FIX(0.707106781)), /* c6 */ - CONST_BITS-1); + CONST_BITS-PASS1_DECR); 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-PASS1_DECR); dataptr[4] = (DCTELEM) DESCALE(MULTIPLY(tmp3 - tmp0, FIX(0.245575608)) /* c8 */ - + z1 - z2, CONST_BITS-1); + + z1 - z2, CONST_BITS-PASS1_DECR); /* Odd part */ dataptr[3] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp12 - tmp13, FIX(1.224744871)), /* c3 */ - CONST_BITS-1); + CONST_BITS-PASS1_DECR); tmp11 = MULTIPLY(tmp11, FIX(1.224744871)); /* c3 */ tmp0 = MULTIPLY(tmp10 + tmp12, FIX(0.909038955)); /* c5 */ tmp1 = MULTIPLY(tmp10 + tmp13, FIX(0.483689525)); /* c7 */ - dataptr[1] = (DCTELEM) DESCALE(tmp11 + tmp0 + tmp1, CONST_BITS-1); + dataptr[1] = (DCTELEM) + DESCALE(tmp11 + tmp0 + tmp1, CONST_BITS-PASS1_DECR); tmp2 = MULTIPLY(tmp12 - tmp13, FIX(1.392728481)); /* c1 */ - dataptr[5] = (DCTELEM) DESCALE(tmp0 - tmp11 - tmp2, CONST_BITS-1); - dataptr[7] = (DCTELEM) DESCALE(tmp1 - tmp11 + tmp2, CONST_BITS-1); + dataptr[5] = (DCTELEM) + DESCALE(tmp0 - tmp11 - tmp2, CONST_BITS-PASS1_DECR); + dataptr[7] = (DCTELEM) + DESCALE(tmp1 - tmp11 + tmp2, CONST_BITS-PASS1_DECR); ctr++; @@ -1021,9 +1091,10 @@ jpeg_fdct_9x9 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) } /* Pass 2: process columns. - * We leave the results scaled up by an overall factor of 8. + * We remove the PASS1_DECR scaling, but leave the results scaled up + * by an overall factor of 8. * We must also scale the output by (8/9)**2 = 64/81, which we partially - * fold into the constant multipliers and final/initial shifting: + * fold into the constant multipliers and final shifting: * cK now represents sqrt(2) * cos(K*pi/18) * 128/81. */ @@ -1047,38 +1118,38 @@ 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+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(MULTIPLY(z1 - z2 - z2, FIX(1.117403309)), /* c6 */ - CONST_BITS+2); + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); 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+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(MULTIPLY(tmp3 - tmp0, FIX(0.388070096)) /* c8 */ - + z1 - z2, CONST_BITS+2); + + z1 - z2, CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); /* Odd part */ dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp12 - tmp13, FIX(1.935399303)), /* c3 */ - CONST_BITS+2); + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); tmp11 = MULTIPLY(tmp11, FIX(1.935399303)); /* c3 */ tmp0 = MULTIPLY(tmp10 + tmp12, FIX(1.436506004)); /* c5 */ tmp1 = MULTIPLY(tmp10 + tmp13, FIX(0.764348879)); /* c7 */ - dataptr[DCTSIZE*1] = (DCTELEM) - DESCALE(tmp11 + tmp0 + tmp1, CONST_BITS+2); + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp11 + tmp0 + tmp1, + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); tmp2 = MULTIPLY(tmp12 - tmp13, FIX(2.200854883)); /* c1 */ - dataptr[DCTSIZE*5] = (DCTELEM) - DESCALE(tmp0 - tmp11 - tmp2, CONST_BITS+2); - dataptr[DCTSIZE*7] = (DCTELEM) - DESCALE(tmp1 - tmp11 + tmp2, CONST_BITS+2); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp0 - tmp11 - tmp2, + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp1 - tmp11 + tmp2, + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -1104,8 +1175,7 @@ jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) /* 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. + * furthermore, we scale the results by 2**PASS1_DECR. * cK represents sqrt(2) * cos(K*pi/20). */ @@ -1134,39 +1204,39 @@ jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[5]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp10 + tmp11 + tmp12 - 10 * CENTERJSAMPLE) << 1); + dataptr[0] = + PASS1_OUTDEC(tmp10 + tmp11 + tmp12 - 10 * CENTERJSAMPLE); tmp12 += tmp12; dataptr[4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.144122806)) - /* c4 */ MULTIPLY(tmp11 - tmp12, FIX(0.437016024)), /* c8 */ - CONST_BITS-1); + CONST_BITS-PASS1_DECR); 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-PASS1_DECR); dataptr[6] = (DCTELEM) DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.176250899)), /* c2+c6 */ - CONST_BITS-1); + CONST_BITS-PASS1_DECR); /* Odd part */ tmp10 = tmp0 + tmp4; tmp11 = tmp1 - tmp3; - dataptr[5] = (DCTELEM) ((tmp10 - tmp11 - tmp2) << 1); + dataptr[5] = PASS1_OUTDEC(tmp10 - tmp11 - tmp2); 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); + CONST_BITS-PASS1_DECR); tmp12 = MULTIPLY(tmp0 - tmp4, FIX(0.951056516)) - /* (c3+c7)/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; - dataptr[3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS-1); - dataptr[7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS-1); + dataptr[3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS-PASS1_DECR); + dataptr[7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS-PASS1_DECR); ctr++; @@ -1179,9 +1249,10 @@ jpeg_fdct_10x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) } /* Pass 2: process columns. - * We leave the results scaled up by an overall factor of 8. + * We remove the PASS1_DECR scaling, but leave the results scaled up + * by an overall factor of 8. * We must also scale the output by (8/10)**2 = 16/25, which we partially - * fold into the constant multipliers and final/initial shifting: + * fold into the constant multipliers and final shifting: * cK now represents sqrt(2) * cos(K*pi/20) * 32/25. */ @@ -1209,19 +1280,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+PASS1_DECR+1+PASS2_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+2); + CONST_BITS+PASS1_DECR+1+PASS2_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+2); + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.785601151)), /* c2+c6 */ - CONST_BITS+2); + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); /* Odd part */ @@ -1229,20 +1300,22 @@ 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+PASS1_DECR+1+PASS2_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+2); + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); tmp12 = MULTIPLY(tmp0 - tmp4, FIX(1.217352341)) - /* (c3+c7)/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 */ - dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS+2); - dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS+2); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(tmp12 + tmp13, CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) + DESCALE(tmp12 - tmp13, CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -1269,8 +1342,7 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) /* 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. + * furthermore, we scale the results by 2**PASS1_DECR. * cK represents sqrt(2) * cos(K*pi/22). */ @@ -1295,8 +1367,8 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp14 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[6]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5 - 11 * CENTERJSAMPLE) << 1); + dataptr[0] = + PASS1_OUTDEC(tmp0 + tmp1 + tmp2 + tmp3 + tmp4 + tmp5 - 11 * CENTERJSAMPLE); tmp5 += tmp5; tmp0 -= tmp5; tmp1 -= tmp5; @@ -1310,16 +1382,16 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) dataptr[2] = (DCTELEM) DESCALE(z1 + z2 - MULTIPLY(tmp3, FIX(1.018300590)) /* c2+c8-c6 */ - MULTIPLY(tmp4, FIX(1.390975730)), /* c4+c10 */ - CONST_BITS-1); + CONST_BITS-PASS1_DECR); 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); + CONST_BITS-PASS1_DECR); dataptr[6] = (DCTELEM) DESCALE(z1 + z3 - MULTIPLY(tmp0, FIX(1.620527200)) /* c2+c4-c6 */ - MULTIPLY(tmp2, FIX(0.788749120)), /* c8+c10 */ - CONST_BITS-1); + CONST_BITS-PASS1_DECR); /* Odd part */ @@ -1338,10 +1410,10 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp3 += tmp5 + tmp10 + MULTIPLY(tmp13, FIX(1.305598626)) /* c1+c5-c9-c7 */ - MULTIPLY(tmp14, FIX(1.286413905)); /* c3 */ - dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS-1); - dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS-1); - dataptr[5] = (DCTELEM) DESCALE(tmp2, CONST_BITS-1); - dataptr[7] = (DCTELEM) DESCALE(tmp3, CONST_BITS-1); + dataptr[1] = (DCTELEM) DESCALE(tmp0, CONST_BITS-PASS1_DECR); + dataptr[3] = (DCTELEM) DESCALE(tmp1, CONST_BITS-PASS1_DECR); + dataptr[5] = (DCTELEM) DESCALE(tmp2, CONST_BITS-PASS1_DECR); + dataptr[7] = (DCTELEM) DESCALE(tmp3, CONST_BITS-PASS1_DECR); ctr++; @@ -1354,9 +1426,10 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) } /* Pass 2: process columns. - * We leave the results scaled up by an overall factor of 8. + * We remove the PASS1_DECR scaling, but leave the results scaled up + * by an overall factor of 8. * We must also scale the output by (8/11)**2 = 64/121, which we partially - * fold into the constant multipliers and final/initial shifting: + * fold into the constant multipliers and final shifting: * cK now represents sqrt(2) * cos(K*pi/22) * 128/121. */ @@ -1381,7 +1454,7 @@ 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); + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); tmp5 += tmp5; tmp0 -= tmp5; tmp1 -= tmp5; @@ -1395,16 +1468,16 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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); + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); 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); + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); 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); + CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); /* Odd part */ @@ -1423,10 +1496,14 @@ jpeg_fdct_11x11 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp3 += tmp5 + tmp10 + MULTIPLY(tmp13, FIX(1.381129125)) /* c1+c5-c9-c7 */ - MULTIPLY(tmp14, FIX(1.360834544)); /* c3 */ - dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+2); - dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+2); - dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+2); - dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp3, CONST_BITS+2); + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp0, CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(tmp1, CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp2, CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) + DESCALE(tmp3, CONST_BITS+PASS1_DECR+1+PASS2_BITS-PASS1_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -1560,17 +1637,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+PASS2_BITS-PASS1_BITS); dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(MULTIPLY(tmp13 - tmp14 - tmp15, FIX(0.888888889)), /* 8/9 */ - CONST_BITS+1); + CONST_BITS+1+PASS2_BITS-PASS1_BITS); dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.088662108)), /* c4 */ - CONST_BITS+1); + CONST_BITS+1+PASS2_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+1); + CONST_BITS+1+PASS2_BITS-PASS1_BITS); /* Odd part */ @@ -1589,10 +1666,14 @@ jpeg_fdct_12x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.161389302)) /* c3 */ - 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); - dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+1); - dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+1); + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp10, CONST_BITS+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(tmp11, CONST_BITS+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp12, CONST_BITS+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) + DESCALE(tmp13, CONST_BITS+1+PASS2_BITS-PASS1_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -1740,7 +1821,7 @@ 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); + CONST_BITS+1+PASS2_BITS-PASS1_BITS); tmp6 += tmp6; tmp0 -= tmp6; tmp1 -= tmp6; @@ -1755,7 +1836,7 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) MULTIPLY(tmp3, FIX(0.129109289)) - /* c12 */ MULTIPLY(tmp4, FIX(0.608465700)) - /* c8 */ MULTIPLY(tmp5, FIX(0.948429952)), /* c4 */ - CONST_BITS+1); + CONST_BITS+1+PASS2_BITS-PASS1_BITS); 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 */ @@ -1763,8 +1844,10 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(z1 + z2, CONST_BITS+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*6] = (DCTELEM) + DESCALE(z1 - z2, CONST_BITS+1+PASS2_BITS-PASS1_BITS); /* Odd part */ @@ -1789,10 +1872,14 @@ jpeg_fdct_13x13 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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); - dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+1); - dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp3, CONST_BITS+1); + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp0, CONST_BITS+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(tmp1, CONST_BITS+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp2, CONST_BITS+1+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) + DESCALE(tmp3, CONST_BITS+1+PASS2_BITS-PASS1_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -1946,24 +2033,24 @@ 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); + CONST_BITS+1+PASS2_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+1); + CONST_BITS+1+PASS2_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+1); + CONST_BITS+1+PASS2_BITS-PASS1_BITS); dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.122795725)) /* c6+c10 */ - MULTIPLY(tmp16, FIX(0.900412262)), /* c2 */ - CONST_BITS+1); + CONST_BITS+1+PASS2_BITS-PASS1_BITS); /* Odd part */ @@ -1972,7 +2059,7 @@ jpeg_fdct_14x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(MULTIPLY(tmp0 - tmp10 + tmp3 - tmp11 - tmp6, FIX(0.653061224)), /* 32/49 */ - CONST_BITS+1); + CONST_BITS+1+PASS2_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 */ @@ -1982,18 +2069,18 @@ jpeg_fdct_14x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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); + CONST_BITS+1+PASS2_BITS-PASS1_BITS); tmp12 = MULTIPLY(tmp0 + tmp1, FIX(0.871740478)) + /* c3 */ 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); + CONST_BITS+1+PASS2_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+1); + CONST_BITS+1+PASS2_BITS-PASS1_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -2135,12 +2222,12 @@ 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+PASS2_BITS-PASS1_BITS); z3 += z3; dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(MULTIPLY(z1 - z3, FIX(1.301757503)) - /* c6 */ MULTIPLY(z2 - z3, FIX(0.497227121)), /* c12 */ - CONST_BITS+2); + CONST_BITS+2+PASS2_BITS-PASS1_BITS); tmp2 += ((tmp1 + tmp4) >> 1) - tmp7 - tmp7; z1 = MULTIPLY(tmp3 - tmp2, FIX(1.742091575)) - /* c2+c14 */ MULTIPLY(tmp6 - tmp2, FIX(2.546621957)); /* c4+c8 */ @@ -2150,8 +2237,10 @@ jpeg_fdct_15x15 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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); + dataptr[DCTSIZE*2] = (DCTELEM) + DESCALE(z1 + z3, CONST_BITS+2+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) + DESCALE(z2 + z3, CONST_BITS+2+PASS2_BITS-PASS1_BITS); /* Odd part */ @@ -2170,10 +2259,14 @@ jpeg_fdct_15x15 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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); - dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+2); - dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp3, CONST_BITS+2); + dataptr[DCTSIZE*1] = (DCTELEM) + DESCALE(tmp0, CONST_BITS+2+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) + DESCALE(tmp1, CONST_BITS+2+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) + DESCALE(tmp2, CONST_BITS+2+PASS2_BITS-PASS1_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) + DESCALE(tmp3, CONST_BITS+2+PASS2_BITS-PASS1_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -2238,8 +2331,8 @@ jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp7 = GETJSAMPLE(elemptr[7]) - GETJSAMPLE(elemptr[8]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp10 + tmp11 + tmp12 + tmp13 - 16 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[0] = + PASS1_OUTPUT(tmp10 + tmp11 + tmp12 + tmp13 - 16 * CENTERJSAMPLE); dataptr[4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */ MULTIPLY(tmp11 - tmp12, FIX_0_541196100), /* c12[16] = c6[8] */ @@ -2297,8 +2390,8 @@ jpeg_fdct_16x16 (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. + * We apply the PASS2_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). */ @@ -2336,11 +2429,11 @@ jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp7 = dataptr[DCTSIZE*7] - wsptr[DCTSIZE*0]; dataptr[DCTSIZE*0] = (DCTELEM) - DESCALE(tmp10 + tmp11 + tmp12 + tmp13, PASS1_BITS+2); + DESCALE(tmp10 + tmp11 + tmp12 + tmp13, PASS2_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); + CONST_BITS+PASS2_BITS+2); tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) + /* c14[16] = c7[8] */ MULTIPLY(tmp14 - tmp16, FIX(1.387039845)); /* c2[16] = c1[8] */ @@ -2348,11 +2441,11 @@ jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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); + CONST_BITS+PASS2_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); + CONST_BITS+PASS2_BITS+2); /* Odd part */ @@ -2378,10 +2471,10 @@ jpeg_fdct_16x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */ + 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); - dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+PASS1_BITS+2); - dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+PASS1_BITS+2); + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+PASS2_BITS+2); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+PASS2_BITS+2); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+PASS2_BITS+2); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+PASS2_BITS+2); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -2447,8 +2540,8 @@ jpeg_fdct_16x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp7 = GETJSAMPLE(elemptr[7]) - GETJSAMPLE(elemptr[8]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp10 + tmp11 + tmp12 + tmp13 - 16 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[0] = + PASS1_OUTPUT(tmp10 + tmp11 + tmp12 + tmp13 - 16 * CENTERJSAMPLE); dataptr[4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.306562965)) + /* c4[16] = c2[8] */ MULTIPLY(tmp11 - tmp12, FIX_0_541196100), /* c12[16] = c6[8] */ @@ -2499,8 +2592,8 @@ jpeg_fdct_16x8 (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. + * We apply the PASS2_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). */ @@ -2516,7 +2609,12 @@ jpeg_fdct_16x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; - tmp10 = tmp0 + tmp3; + /* Add fudge factor here for final descale. */ +#if PASS2_BITS > 0 + tmp10 = tmp0 + tmp3 + (ONE << PASS2_BITS); +#else + tmp10 = tmp0 + tmp3 + ONE; +#endif tmp12 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp13 = tmp1 - tmp2; @@ -2526,16 +2624,19 @@ jpeg_fdct_16x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; tmp3 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; - dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS+1); - dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS+1); + dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp10 + tmp11, PASS2_BITS+1); + dataptr[DCTSIZE*4] = (DCTELEM) RIGHT_SHIFT(tmp10 - tmp11, PASS2_BITS+1); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS+PASS2_BITS); - 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); + RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */ + CONST_BITS+PASS2_BITS+1); dataptr[DCTSIZE*6] = (DCTELEM) - DESCALE(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */ - CONST_BITS+PASS1_BITS+1); + RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */ + CONST_BITS+PASS2_BITS+1); /* Odd part per figure 8 --- note paper omits factor of sqrt(2). * i0..i3 in the paper are tmp0..tmp3 here. @@ -2544,28 +2645,31 @@ jpeg_fdct_16x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp12 = tmp0 + tmp2; tmp13 = tmp1 + tmp3; - z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ - tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* -c3+c5 */ - tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ + z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS+PASS2_BITS); + + tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* -c3+c5 */ + tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ tmp12 += z1; tmp13 += z1; - 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 */ + 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 */ + 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[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0, CONST_BITS+PASS2_BITS+1); + dataptr[DCTSIZE*3] = (DCTELEM) RIGHT_SHIFT(tmp1, CONST_BITS+PASS2_BITS+1); + dataptr[DCTSIZE*5] = (DCTELEM) RIGHT_SHIFT(tmp2, CONST_BITS+PASS2_BITS+1); + dataptr[DCTSIZE*7] = (DCTELEM) RIGHT_SHIFT(tmp3, CONST_BITS+PASS2_BITS+1); dataptr++; /* advance pointer to next column */ } @@ -2628,8 +2732,8 @@ jpeg_fdct_14x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp6 = GETJSAMPLE(elemptr[6]) - GETJSAMPLE(elemptr[7]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp10 + tmp11 + tmp12 + tmp13 - 14 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[0] = + PASS1_OUTPUT(tmp10 + tmp11 + tmp12 + tmp13 - 14 * CENTERJSAMPLE); tmp13 += tmp13; dataptr[4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp13, FIX(1.274162392)) + /* c4 */ @@ -2652,7 +2756,7 @@ jpeg_fdct_14x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp10 = tmp1 + tmp2; tmp11 = tmp5 - tmp4; - dataptr[7] = (DCTELEM) ((tmp0 - tmp10 + tmp3 - tmp11 - tmp6) << PASS1_BITS); + dataptr[7] = PASS1_OUTPUT(tmp0 - tmp10 + tmp3 - tmp11 - tmp6); tmp3 <<= CONST_BITS; tmp10 = MULTIPLY(tmp10, - FIX(0.158341681)); /* -c13 */ tmp11 = MULTIPLY(tmp11, FIX(1.405321284)); /* c1 */ @@ -2678,8 +2782,8 @@ jpeg_fdct_14x7 (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. + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. * We must also scale the output by (8/14)*(8/7) = 32/49, which we * partially fold into the constant multipliers and final shifting: * 7-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/14) * 64/49. @@ -2701,20 +2805,20 @@ 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+PASS2_BITS+1); tmp3 += tmp3; z1 -= tmp3; z1 -= tmp3; z1 = MULTIPLY(z1, FIX(0.461784020)); /* (c2+c6-c4)/2 */ z2 = MULTIPLY(tmp0 - tmp2, FIX(1.202428084)); /* (c2+c4-c6)/2 */ z3 = MULTIPLY(tmp1 - tmp2, FIX(0.411026446)); /* c6 */ - dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + z2 + z3, CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + z2 + z3, CONST_BITS+PASS2_BITS+1); z1 -= z2; 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); - dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+PASS1_BITS+1); + CONST_BITS+PASS2_BITS+1); + dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + z2, CONST_BITS+PASS2_BITS+1); /* Odd part */ @@ -2728,9 +2832,9 @@ jpeg_fdct_14x7 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp0 += tmp3; tmp2 += tmp3 + MULTIPLY(tmp12, FIX(2.443531355)); /* c3+c1-c5 */ - 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*1] = (DCTELEM) DESCALE(tmp0, CONST_BITS+PASS2_BITS+1); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp1, CONST_BITS+PASS2_BITS+1); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp2, CONST_BITS+PASS2_BITS+1); dataptr++; /* advance pointer to next column */ } @@ -2790,9 +2894,9 @@ jpeg_fdct_12x6 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp5 = GETJSAMPLE(elemptr[5]) - GETJSAMPLE(elemptr[6]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp10 + tmp11 + tmp12 - 12 * CENTERJSAMPLE) << PASS1_BITS); - dataptr[6] = (DCTELEM) ((tmp13 - tmp14 - tmp15) << PASS1_BITS); + dataptr[0] = + PASS1_OUTPUT(tmp10 + tmp11 + tmp12 - 12 * CENTERJSAMPLE); + dataptr[6] = PASS1_OUTPUT(tmp13 - tmp14 - tmp15); dataptr[4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.224744871)), /* c4 */ CONST_BITS-PASS1_BITS); @@ -2826,8 +2930,8 @@ jpeg_fdct_12x6 (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. + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. * We must also scale the output by (8/12)*(8/6) = 8/9, which we * partially fold into the constant multipliers and final shifting: * 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12) * 16/9. @@ -2850,13 +2954,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+PASS2_BITS+1); dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(MULTIPLY(tmp12, FIX(2.177324216)), /* c2 */ - CONST_BITS+PASS1_BITS+1); + CONST_BITS+PASS2_BITS+1); dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(1.257078722)), /* c4 */ - CONST_BITS+PASS1_BITS+1); + CONST_BITS+PASS2_BITS+1); /* Odd part */ @@ -2864,13 +2968,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+PASS2_BITS+1); dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(MULTIPLY(tmp0 - tmp1 - tmp2, FIX(1.777777778)), /* 16/9 */ - CONST_BITS+PASS1_BITS+1); + CONST_BITS+PASS2_BITS+1); dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp10 + MULTIPLY(tmp2 - tmp1, FIX(1.777777778)), /* 16/9 */ - CONST_BITS+PASS1_BITS+1); + CONST_BITS+PASS2_BITS+1); dataptr++; /* advance pointer to next column */ } @@ -2926,8 +3030,8 @@ jpeg_fdct_10x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp4 = GETJSAMPLE(elemptr[4]) - GETJSAMPLE(elemptr[5]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp10 + tmp11 + tmp12 - 10 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[0] = + PASS1_OUTPUT(tmp10 + tmp11 + tmp12 - 10 * CENTERJSAMPLE); tmp12 += tmp12; dataptr[4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.144122806)) - /* c4 */ @@ -2945,7 +3049,7 @@ jpeg_fdct_10x5 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp10 = tmp0 + tmp4; tmp11 = tmp1 - tmp3; - dataptr[5] = (DCTELEM) ((tmp10 - tmp11 - tmp2) << PASS1_BITS); + dataptr[5] = PASS1_OUTPUT(tmp10 - tmp11 - tmp2); tmp2 <<= CONST_BITS; dataptr[1] = (DCTELEM) DESCALE(MULTIPLY(tmp0, FIX(1.396802247)) + /* c1 */ @@ -2964,10 +3068,10 @@ jpeg_fdct_10x5 (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. - * We must also scale the output by (8/10)*(8/5) = 32/25, which we - * fold into the constant multipliers: + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. + * We must also scale the output by (8/10)*(8/5) = 32/25, + * which we fold into the constant multipliers: * 5-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/10) * 32/25. */ @@ -2987,12 +3091,12 @@ 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+PASS2_BITS); tmp11 = MULTIPLY(tmp11, FIX(1.011928851)); /* (c2+c4)/2 */ tmp10 -= tmp2 << 2; tmp10 = MULTIPLY(tmp10, FIX(0.452548340)); /* (c2-c4)/2 */ - dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(tmp11 + tmp10, CONST_BITS+PASS1_BITS); - dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp11 - tmp10, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(tmp11 + tmp10, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp11 - tmp10, CONST_BITS+PASS2_BITS); /* Odd part */ @@ -3000,10 +3104,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+PASS2_BITS); dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp10 - MULTIPLY(tmp1, FIX(2.785601151)), /* c1+c3 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -3113,8 +3217,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. + * We apply the PASS2_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) [refers to 8-point FDCT]. */ @@ -3124,27 +3228,35 @@ jpeg_fdct_8x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) /* Even part */ /* Add fudge factor here for final descale. */ - tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3] + (ONE << (PASS1_BITS-1)); +#if PASS2_BITS > 1 + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3] + (ONE << (PASS2_BITS-1)); +#else +#if PASS2_BITS > 0 + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3] + ONE; +#else + tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*3]; +#endif +#endif tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*2]; tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*3]; tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*2]; - dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp0 + tmp1, PASS1_BITS); - dataptr[DCTSIZE*2] = (DCTELEM) RIGHT_SHIFT(tmp0 - tmp1, PASS1_BITS); + dataptr[DCTSIZE*0] = PASS2_OUTPUT(tmp0 + tmp1); + dataptr[DCTSIZE*2] = PASS2_OUTPUT(tmp0 - tmp1); /* Odd part */ tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ /* Add fudge factor here for final descale. */ - tmp0 += ONE << (CONST_BITS+PASS1_BITS-1); + tmp0 += ONE << (CONST_BITS+PASS2_BITS-1); dataptr[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*3] = (DCTELEM) RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -3218,8 +3330,8 @@ jpeg_fdct_6x3 (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. + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. * We must also scale the output by (8/6)*(8/3) = 32/9, which we partially * fold into the constant multipliers (other part was done in pass 1): * 3-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/6) * 16/9. @@ -3236,16 +3348,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+PASS2_BITS); dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(MULTIPLY(tmp0 - tmp1 - tmp1, FIX(1.257078722)), /* c2 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); /* Odd part */ dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(MULTIPLY(tmp2, FIX(2.177324216)), /* c1 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -3266,6 +3378,9 @@ jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) INT32 z1, z2, z3; JSAMPROW elemptr; SHIFT_TEMPS +#if PASS2_BITS > PASS1_BITS + 3 + ISHIFT_TEMPS +#endif /* Pre-zero output coefficient block. */ MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); @@ -3284,6 +3399,15 @@ jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp4 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[3]); tmp5 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[2]); +#if PASS2_BITS > PASS1_BITS + 3 + /* Add fudge factor here for final downscale. */ +#if PASS2_BITS > PASS1_BITS + 4 + tmp4 += 1 << (PASS2_BITS-PASS1_BITS-3-1); +#else + tmp4 += 1; +#endif +#endif + tmp0 = tmp4 + tmp5; tmp2 = tmp4 - tmp5; @@ -3294,7 +3418,7 @@ jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) z1 = MULTIPLY(z2 + z3, FIX_0_541196100); /* c6 */ /* Add fudge factor here for final descale. */ - z1 += ONE << (CONST_BITS-3-1); + z1 += ONE << (CONST_BITS+PASS2_BITS-PASS1_BITS-3-1); tmp1 = z1 + MULTIPLY(z2, FIX_0_765366865); /* c2-c6 */ tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065); /* c2+c6 */ @@ -3325,20 +3449,49 @@ jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) /* Column 0 */ /* Apply unsigned->signed conversion. */ - data[DCTSIZE*0] = (tmp0 + tmp10 - 8 * CENTERJSAMPLE) << 3; - data[DCTSIZE*1] = (tmp0 - tmp10) << 3; - /* Column 1 */ - data[DCTSIZE*0+1] = (DCTELEM) RIGHT_SHIFT(tmp1 + tmp11, CONST_BITS-3); - data[DCTSIZE*1+1] = (DCTELEM) RIGHT_SHIFT(tmp1 - tmp11, CONST_BITS-3); +#if PASS2_BITS < PASS1_BITS + 3 + data[DCTSIZE*0] = + (tmp0 + tmp10 - 8 * CENTERJSAMPLE) << (3+PASS1_BITS-PASS2_BITS); + data[DCTSIZE*1] = (tmp0 - tmp10) << (3+PASS1_BITS-PASS2_BITS); /* Column 2 */ - data[DCTSIZE*0+2] = (tmp2 + tmp12) << 3; - data[DCTSIZE*1+2] = (tmp2 - tmp12) << 3; + data[DCTSIZE*0+2] = (tmp2 + tmp12) << (3+PASS1_BITS-PASS2_BITS); + data[DCTSIZE*1+2] = (tmp2 - tmp12) << (3+PASS1_BITS-PASS2_BITS); +#else +#if PASS2_BITS == PASS1_BITS + 3 + data[DCTSIZE*0] = tmp0 + tmp10 - 8 * CENTERJSAMPLE; + data[DCTSIZE*1] = tmp0 - tmp10; + + /* Column 2 */ + data[DCTSIZE*0+2] = tmp2 + tmp12; + data[DCTSIZE*1+2] = tmp2 - tmp12; +#else + data[DCTSIZE*0] = + IRIGHT_SHIFT(tmp0 + tmp10 - 8 * CENTERJSAMPLE, + PASS2_BITS-PASS1_BITS-3); + data[DCTSIZE*1] = + IRIGHT_SHIFT(tmp0 - tmp10, PASS2_BITS-PASS1_BITS-3); + + /* Column 2 */ + data[DCTSIZE*0+2] = + IRIGHT_SHIFT(tmp2 + tmp12, PASS2_BITS-PASS1_BITS-3); + data[DCTSIZE*1+2] = + IRIGHT_SHIFT(tmp2 - tmp12, PASS2_BITS-PASS1_BITS-3); +#endif +#endif + + /* Column 1 */ + data[DCTSIZE*0+1] = (DCTELEM) + RIGHT_SHIFT(tmp1 + tmp11, CONST_BITS+PASS2_BITS-PASS1_BITS-3); + data[DCTSIZE*1+1] = (DCTELEM) + RIGHT_SHIFT(tmp1 - tmp11, CONST_BITS+PASS2_BITS-PASS1_BITS-3); /* Column 3 */ - data[DCTSIZE*0+3] = (DCTELEM) RIGHT_SHIFT(tmp3 + tmp13, CONST_BITS-3); - data[DCTSIZE*1+3] = (DCTELEM) RIGHT_SHIFT(tmp3 - tmp13, CONST_BITS-3); + data[DCTSIZE*0+3] = (DCTELEM) + RIGHT_SHIFT(tmp3 + tmp13, CONST_BITS+PASS2_BITS-PASS1_BITS-3); + data[DCTSIZE*1+3] = (DCTELEM) + RIGHT_SHIFT(tmp3 - tmp13, CONST_BITS+PASS2_BITS-PASS1_BITS-3); } @@ -3369,11 +3522,12 @@ jpeg_fdct_2x1 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) /* Even part */ /* Apply unsigned->signed conversion. */ - data[0] = (tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 5; + data[0] = + (tmp0 + tmp1 - 2 * CENTERJSAMPLE) << (5+PASS1_BITS-PASS2_BITS); /* Odd part */ - data[1] = (tmp0 - tmp1) << 5; + data[1] = (tmp0 - tmp1) << (5+PASS1_BITS-PASS2_BITS); } @@ -3427,16 +3581,19 @@ jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp3 = GETJSAMPLE(elemptr[3]) - GETJSAMPLE(elemptr[4]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) ((tmp10 + tmp11 - 8 * CENTERJSAMPLE) << PASS1_BITS); - dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS); + dataptr[0] = PASS1_OUTPUT(tmp10 + tmp11 - 8 * CENTERJSAMPLE); + dataptr[4] = PASS1_OUTPUT(tmp10 - tmp11); + + z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); /* c6 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); - z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); /* c6 */ dataptr[2] = (DCTELEM) - DESCALE(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */ - CONST_BITS-PASS1_BITS); + RIGHT_SHIFT(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); + 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). * i0..i3 in the paper are tmp0..tmp3 here. @@ -3445,28 +3602,31 @@ jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp12 = tmp0 + tmp2; tmp13 = tmp1 + tmp3; - z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ - tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* -c3+c5 */ - tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ + z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ + /* Add fudge factor here for final descale. */ + z1 += ONE << (CONST_BITS-PASS1_BITS-1); + + tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* -c3+c5 */ + tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ tmp12 += z1; tmp13 += z1; - 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 */ + 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 */ + 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); + 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); ctr++; @@ -3479,8 +3639,8 @@ jpeg_fdct_8x16 (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. + * We apply the PASS2_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. * 16-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/32). */ @@ -3518,11 +3678,16 @@ jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp7 = dataptr[DCTSIZE*7] - wsptr[DCTSIZE*0]; dataptr[DCTSIZE*0] = (DCTELEM) - DESCALE(tmp10 + tmp11 + tmp12 + tmp13, PASS1_BITS+1); +#if PASS2_BITS > 0 + RIGHT_SHIFT(tmp10 + tmp11 + tmp12 + tmp13 + (ONE << PASS2_BITS), + PASS2_BITS+1); +#else + RIGHT_SHIFT(tmp10 + tmp11 + tmp12 + tmp13 + ONE, 1); +#endif 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); + CONST_BITS+PASS2_BITS+1); tmp10 = MULTIPLY(tmp17 - tmp15, FIX(0.275899379)) + /* c14[16] = c7[8] */ MULTIPLY(tmp14 - tmp16, FIX(1.387039845)); /* c2[16] = c1[8] */ @@ -3530,11 +3695,11 @@ jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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); + CONST_BITS+PASS2_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); + CONST_BITS+PASS2_BITS+1); /* Odd part */ @@ -3560,10 +3725,10 @@ jpeg_fdct_8x16 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp13 += tmp15 + tmp16 + MULTIPLY(tmp3, FIX(1.065388962)) /* c15+c3+c11-c7 */ + 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); - dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+PASS1_BITS+1); - dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+PASS1_BITS+1); + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+PASS2_BITS+1); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+PASS2_BITS+1); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+PASS2_BITS+1); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+PASS2_BITS+1); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -3617,8 +3782,7 @@ jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) z1 = tmp0 + tmp2; /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((z1 + tmp1 + tmp3 - 7 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[0] = PASS1_OUTPUT(z1 + tmp1 + tmp3 - 7 * CENTERJSAMPLE); tmp3 += tmp3; z1 -= tmp3; z1 -= tmp3; @@ -3660,10 +3824,10 @@ jpeg_fdct_7x14 (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. - * We must also scale the output by (8/7)*(8/14) = 32/49, which we - * fold into the constant multipliers: + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. + * We must also scale the output by (8/7)*(8/14) = 32/49, + * which we fold into the constant multipliers: * 14-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/28) * 32/49. */ @@ -3698,24 +3862,24 @@ 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); + CONST_BITS+PASS2_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); + CONST_BITS+PASS2_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); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(tmp10 - MULTIPLY(tmp15, FIX(1.122795725)) /* c6+c10 */ - MULTIPLY(tmp16, FIX(0.900412262)), /* c2 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); /* Odd part */ @@ -3724,7 +3888,7 @@ jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(MULTIPLY(tmp0 - tmp10 + tmp3 - tmp11 - tmp6, FIX(0.653061224)), /* 32/49 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); tmp3 = MULTIPLY(tmp3 , FIX(0.653061224)); /* 32/49 */ tmp10 = MULTIPLY(tmp10, - FIX(0.103406812)); /* -c13 */ tmp11 = MULTIPLY(tmp11, FIX(0.917760839)); /* c1 */ @@ -3734,18 +3898,18 @@ jpeg_fdct_7x14 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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); + CONST_BITS+PASS2_BITS); tmp12 = MULTIPLY(tmp0 + tmp1, FIX(0.871740478)) + /* c3 */ 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); + CONST_BITS+PASS2_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); + CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -3799,8 +3963,7 @@ jpeg_fdct_6x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp2 = GETJSAMPLE(elemptr[2]) - GETJSAMPLE(elemptr[3]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp10 + tmp11 - 6 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[0] = PASS1_OUTPUT(tmp10 + tmp11 - 6 * CENTERJSAMPLE); dataptr[2] = (DCTELEM) DESCALE(MULTIPLY(tmp12, FIX(1.224744871)), /* c2 */ CONST_BITS-PASS1_BITS); @@ -3813,9 +3976,15 @@ jpeg_fdct_6x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp10 = DESCALE(MULTIPLY(tmp0 + tmp2, FIX(0.366025404)), /* c5 */ CONST_BITS-PASS1_BITS); +#if PASS1_BITS > 0 dataptr[1] = (DCTELEM) (tmp10 + ((tmp0 + tmp1) << PASS1_BITS)); dataptr[3] = (DCTELEM) ((tmp0 - tmp1 - tmp2) << PASS1_BITS); dataptr[5] = (DCTELEM) (tmp10 + ((tmp2 - tmp1) << PASS1_BITS)); +#else + dataptr[1] = (DCTELEM) (tmp10 + tmp0 + tmp1); + dataptr[3] = (DCTELEM) (tmp0 - tmp1 - tmp2); + dataptr[5] = (DCTELEM) (tmp10 + tmp2 - tmp1); +#endif ctr++; @@ -3828,10 +3997,10 @@ jpeg_fdct_6x12 (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. - * We must also scale the output by (8/6)*(8/12) = 8/9, which we - * fold into the constant multipliers: + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. + * We must also scale the output by (8/6)*(8/12) = 8/9, + * which we fold into the constant multipliers: * 12-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/24) * 8/9. */ @@ -3863,17 +4032,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+PASS2_BITS); dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(MULTIPLY(tmp13 - tmp14 - tmp15, FIX(0.888888889)), /* 8/9 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp12, FIX(1.088662108)), /* c4 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_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); + CONST_BITS+PASS2_BITS); /* Odd part */ @@ -3892,10 +4061,10 @@ jpeg_fdct_6x12 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp11 = tmp15 + MULTIPLY(tmp0 - tmp3, FIX(1.161389302)) /* c3 */ - 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); - dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+PASS1_BITS); - dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp10, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp11, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp12, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp13, CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -3948,8 +4117,7 @@ jpeg_fdct_5x10 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp1 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[3]); /* Apply unsigned->signed conversion. */ - dataptr[0] = (DCTELEM) - ((tmp10 + tmp2 - 5 * CENTERJSAMPLE) << PASS1_BITS); + dataptr[0] = PASS1_OUTPUT(tmp10 + tmp2 - 5 * CENTERJSAMPLE); tmp11 = MULTIPLY(tmp11, FIX(0.790569415)); /* (c2+c4)/2 */ tmp10 -= tmp2 << 2; tmp10 = MULTIPLY(tmp10, FIX(0.353553391)); /* (c2-c4)/2 */ @@ -3978,10 +4146,10 @@ jpeg_fdct_5x10 (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. - * We must also scale the output by (8/5)*(8/10) = 32/25, which we - * fold into the constant multipliers: + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. + * We must also scale the output by (8/5)*(8/10) = 32/25, + * which we fold into the constant multipliers: * 10-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/20) * 32/25. */ @@ -4009,19 +4177,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+PASS2_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); + CONST_BITS+PASS2_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+PASS2_BITS); dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(tmp10 - MULTIPLY(tmp14, FIX(2.785601151)), /* c2+c6 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); /* Odd part */ @@ -4029,20 +4197,20 @@ 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+PASS2_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); + CONST_BITS+PASS2_BITS); tmp12 = MULTIPLY(tmp0 - tmp4, FIX(1.217352341)) - /* (c3+c7)/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 */ - dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS+PASS1_BITS); - dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS+PASS1_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp12 + tmp13, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp12 - tmp13, CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ wsptr++; /* advance pointer to next column */ @@ -4112,8 +4280,8 @@ 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. + * We apply the PASS2_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). */ @@ -4129,7 +4297,15 @@ jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; /* Add fudge factor here for final descale. */ - tmp10 = tmp0 + tmp3 + (ONE << (PASS1_BITS-1)); +#if PASS2_BITS > 1 + tmp10 = tmp0 + tmp3 + (ONE << (PASS2_BITS-1)); +#else +#if PASS2_BITS > 0 + tmp10 = tmp0 + tmp3 + ONE; +#else + tmp10 = tmp0 + tmp3; +#endif +#endif tmp12 = tmp0 - tmp3; tmp11 = tmp1 + tmp2; tmp13 = tmp1 - tmp2; @@ -4139,19 +4315,19 @@ jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp2 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; tmp3 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; - dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp10 + tmp11, PASS1_BITS); - dataptr[DCTSIZE*4] = (DCTELEM) RIGHT_SHIFT(tmp10 - tmp11, PASS1_BITS); + dataptr[DCTSIZE*0] = PASS2_OUTPUT(tmp10 + tmp11); + dataptr[DCTSIZE*4] = PASS2_OUTPUT(tmp10 - tmp11); z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100); /* c6 */ /* Add fudge factor here for final descale. */ - z1 += ONE << (CONST_BITS+PASS1_BITS-1); + z1 += ONE << (CONST_BITS+PASS2_BITS-1); dataptr[DCTSIZE*2] = (DCTELEM) RIGHT_SHIFT(z1 + MULTIPLY(tmp12, FIX_0_765366865), /* c2-c6 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*6] = (DCTELEM) RIGHT_SHIFT(z1 - MULTIPLY(tmp13, FIX_1_847759065), /* c2+c6 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); /* Odd part per figure 8 --- note paper omits factor of sqrt(2). * i0..i3 in the paper are tmp0..tmp3 here. @@ -4162,7 +4338,7 @@ jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) z1 = MULTIPLY(tmp12 + tmp13, FIX_1_175875602); /* c3 */ /* Add fudge factor here for final descale. */ - z1 += ONE << (CONST_BITS+PASS1_BITS-1); + z1 += ONE << (CONST_BITS+PASS2_BITS-1); tmp12 = MULTIPLY(tmp12, - FIX_0_390180644); /* -c3+c5 */ tmp13 = MULTIPLY(tmp13, - FIX_1_961570560); /* -c3-c5 */ @@ -4181,10 +4357,10 @@ jpeg_fdct_4x8 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) 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[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*3] = (DCTELEM) RIGHT_SHIFT(tmp1, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*5] = (DCTELEM) RIGHT_SHIFT(tmp2, CONST_BITS+PASS2_BITS); + dataptr[DCTSIZE*7] = (DCTELEM) RIGHT_SHIFT(tmp3, CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -4246,8 +4422,8 @@ jpeg_fdct_3x6 (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. + * We apply the PASS2_BITS scaling, but leave the + * results scaled up by an overall factor of 8. * We must also scale the output by (8/6)*(8/3) = 32/9, which we partially * fold into the constant multipliers (other part was done in pass 1): * 6-point FDCT kernel, cK represents sqrt(2) * cos(K*pi/12) * 16/9. @@ -4270,13 +4446,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+PASS2_BITS); dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(MULTIPLY(tmp12, FIX(2.177324216)), /* c2 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(MULTIPLY(tmp10 - tmp11 - tmp11, FIX(1.257078722)), /* c4 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); /* Odd part */ @@ -4284,13 +4460,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+PASS2_BITS); dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(MULTIPLY(tmp0 - tmp1 - tmp2, FIX(1.777777778)), /* 16/9 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp10 + MULTIPLY(tmp2 - tmp1, FIX(1.777777778)), /* 16/9 */ - CONST_BITS+PASS1_BITS); + CONST_BITS+PASS2_BITS); dataptr++; /* advance pointer to next column */ } @@ -4356,21 +4532,38 @@ jpeg_fdct_2x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*3]; tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*2]; - dataptr[DCTSIZE*0] = (DCTELEM) ((tmp0 + tmp1) << 3); - dataptr[DCTSIZE*2] = (DCTELEM) ((tmp0 - tmp1) << 3); +#if PASS2_BITS < PASS1_BITS + 3 + dataptr[DCTSIZE*0] = (DCTELEM) + ((tmp0 + tmp1) << (3+PASS1_BITS-PASS2_BITS)); + dataptr[DCTSIZE*2] = (DCTELEM) + ((tmp0 - tmp1) << (3+PASS1_BITS-PASS2_BITS)); +#else +#if PASS2_BITS == PASS1_BITS + 3 + dataptr[DCTSIZE*0] = (DCTELEM) (tmp0 + tmp1); + dataptr[DCTSIZE*2] = (DCTELEM) (tmp0 - tmp1); +#else + /* Add fudge factor for descale. */ + tmp0 += ONE << (PASS2_BITS-PASS1_BITS-3-1); + + dataptr[DCTSIZE*0] = (DCTELEM) + RIGHT_SHIFT(tmp0 + tmp1, PASS2_BITS-PASS1_BITS-3); + dataptr[DCTSIZE*2] = (DCTELEM) + RIGHT_SHIFT(tmp0 - tmp1, PASS2_BITS-PASS1_BITS-3); +#endif +#endif /* Odd part */ tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ - /* Add fudge factor here for final descale. */ - tmp0 += ONE << (CONST_BITS-3-1); + /* Add fudge factor for descale. */ + tmp0 += ONE << (CONST_BITS+PASS2_BITS-PASS1_BITS-3-1); dataptr[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ - CONST_BITS-3); + CONST_BITS+PASS2_BITS-PASS1_BITS-3); dataptr[DCTSIZE*3] = (DCTELEM) RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ - CONST_BITS-3); + CONST_BITS+PASS2_BITS-PASS1_BITS-3); dataptr++; /* advance pointer to next column */ } @@ -4404,11 +4597,12 @@ jpeg_fdct_1x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) tmp1 = GETJSAMPLE(sample_data[1][start_col]); /* Apply unsigned->signed conversion. */ - data[DCTSIZE*0] = (tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 5; + data[DCTSIZE*0] = + (tmp0 + tmp1 - 2 * CENTERJSAMPLE) << (5+PASS1_BITS-PASS2_BITS); /* Odd part */ - data[DCTSIZE*1] = (tmp0 - tmp1) << 5; + data[DCTSIZE*1] = (tmp0 - tmp1) << (5+PASS1_BITS-PASS2_BITS); } #endif /* DCT_SCALING_SUPPORTED */ diff --git a/jpeg/jidctfst.c b/jpeg/jidctfst.c index 1ac3e39cb..b5bfb597d 100644 --- a/jpeg/jidctfst.c +++ b/jpeg/jidctfst.c @@ -2,7 +2,7 @@ * jidctfst.c * * Copyright (C) 1994-1998, Thomas G. Lane. - * Modified 2015-2017 by Guido Vollbeding. + * Modified 2015-2025 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. * @@ -15,11 +15,11 @@ * a time). Direct algorithms are also available, but they are much more * complex and seem not to be any faster when reduced to code. * - * This implementation is based on Arai, Agui, and Nakajima's algorithm for - * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in - * Japanese, but the algorithm is described in the Pennebaker & Mitchell - * JPEG textbook (see REFERENCES section in file README). The following code - * is based directly on figure 4-8 in P&M. + * This implementation is based on Arai, Agui, and Nakajima's algorithm + * for scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is + * in Japanese, but the algorithm is described in the Pennebaker & Mitchell + * JPEG textbook (see REFERENCES section in file README). The following + * code is based directly on figure 4-8 in P&M. * While an 8-point DCT cannot be done in less than 11 multiplies, it is * possible to arrange the computation so that many of the multiplies are * simple scalings of the final outputs. These multiplies can then be @@ -28,9 +28,9 @@ * to be done in the DCT itself. * The primary disadvantage of this method is that with fixed-point math, * accuracy is lost due to imprecise representation of the scaled - * quantization values. The smaller the quantization table entry, the less - * precise the scaled value, so this implementation does worse with high- - * quality-setting files than with low-quality ones. + * quantization values. The smaller the quantization table entry, + * the less precise the scaled value, so this implementation does + * worse with high-quality-setting files than with low-quality ones. */ #define JPEG_INTERNALS @@ -55,17 +55,17 @@ * (right shift) multiplication products as soon as they are formed, * rather than carrying additional fractional bits into subsequent additions. * This compromises accuracy slightly, but it lets us save a few shifts. - * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples) - * everywhere except in the multiplications proper; this saves a good deal - * of work on 16-bit-int machines. + * More importantly, 16-bit arithmetic is then adequate (for up to 10-bit + * data) everywhere except in the multiplications proper; + * this saves a good deal of work on 16-bit-int machines. * * The dequantized coefficients are not integers because the AA&N scaling * factors have been incorporated. We represent them scaled up by PASS1_BITS, * so that the first and second IDCT rounds have the same input scaling. - * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to + * For up to 10-bit data, we choose IFAST_SCALE_BITS = PASS1_BITS so as to * avoid a descaling shift; this compromises accuracy rather drastically * for small quantization table entries, but it saves a lot of shifts. - * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway, + * For higher bit depths, there's no hope of using 16x16 multiplies anyway, * so we use a much larger scaling factor to preserve accuracy. * * A final compromise is to represent the multiplicative constants to only @@ -74,16 +74,20 @@ * are fewer one-bits in the constants). */ -#if BITS_IN_JSAMPLE == 8 +#if JPEG_DATA_PRECISION <= 10 && BITS_IN_JSAMPLE <= 13 #define CONST_BITS 8 -#define PASS1_BITS 2 +#define PASS1_BITS (10 - JPEG_DATA_PRECISION) +#define PASS2_BITS (13 - BITS_IN_JSAMPLE) #else +#if JPEG_DATA_PRECISION <= 13 && BITS_IN_JSAMPLE <= 16 #define CONST_BITS 8 -#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ +#define PASS1_BITS (13 - JPEG_DATA_PRECISION) +#define PASS2_BITS (16 - BITS_IN_JSAMPLE) +#endif #endif -/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus - * causing a lot of useless floating-point operations at run time. +/* Some C compilers fail to reduce "FIX(constant)" at compile time, + * thus causing a lot of useless floating-point operations at run time. * To get around this we use the following pre-calculated constants. * If you change CONST_BITS you may want to add appropriate values. * (With a reasonable C compiler, you can just rely on the FIX() macro...) @@ -102,9 +106,9 @@ #endif -/* We can gain a little more speed, with a further compromise in accuracy, - * by omitting the addition in a descaling shift. This yields an incorrectly - * rounded result half the time... +/* We can gain a little more speed, with a further compromise + * in accuracy, by omitting the addition in a descaling shift. + * This yields an incorrectly rounded result half the time... */ #ifndef USE_ACCURATE_ROUNDING @@ -113,20 +117,20 @@ #endif -/* Multiply a DCTELEM variable by an INT32 constant, and immediately - * descale to yield a DCTELEM result. +/* Multiply a DCTELEM variable by an INT32 constant, + * and immediately descale to yield a DCTELEM result. */ #define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS)) /* Dequantize a coefficient by multiplying it by the multiplier-table - * entry; produce a DCTELEM result. For 8-bit data a 16x16->16 - * multiplication will do. For 12-bit data, the multiplier table is - * declared INT32, so a 32-bit multiply will be used. + * entry; produce a DCTELEM result. For up to 10-bit data a 16x16->16 + * multiplication will do. For higher bit depths, the multiplier table + * is declared INT32, so a 32-bit multiply will be used. */ -#if BITS_IN_JSAMPLE == 8 +#if JPEG_DATA_PRECISION <= 10 && BITS_IN_JSAMPLE <= 13 #define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval)) #else #define DEQUANTIZE(coef,quantval) \ @@ -134,6 +138,16 @@ #endif +/* Final output conversion: scale down and range-limit. */ + +#if PASS2_BITS > 0 +#define FINAL_OUTPUT(x) \ + range_limit[(int) IRIGHT_SHIFT(x, PASS2_BITS) & RANGE_MASK] +#else +#define FINAL_OUTPUT(x) range_limit[(int) (x) & RANGE_MASK] +#endif + + /* * Perform dequantization and inverse DCT on one block of coefficients. * @@ -172,7 +186,7 @@ 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 && @@ -188,13 +202,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]); @@ -212,7 +226,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]); @@ -249,10 +263,10 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, 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. + * which is folded into the PASS2_BITS value. */ wsptr = workspace; @@ -260,9 +274,16 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, outptr = output_buf[ctr] + output_col; /* Add range center and fudge factor for final descale and range-limit. */ +#if PASS2_BITS > 1 z5 = (DCTELEM) wsptr[0] + - ((((DCTELEM) RANGE_CENTER) << (PASS1_BITS+3)) + - (1 << (PASS1_BITS+2))); + ((((DCTELEM) RANGE_CENTER) << PASS2_BITS) + (1 << (PASS2_BITS-1))); +#else +#if PASS2_BITS > 0 + z5 = (DCTELEM) wsptr[0] + ((((DCTELEM) RANGE_CENTER) << 1) + 1); +#else + z5 = (DCTELEM) wsptr[0] + (DCTELEM) RANGE_CENTER; +#endif +#endif /* 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 @@ -271,14 +292,13 @@ 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) { /* AC terms all zero */ - JSAMPLE dcval = range_limit[(int) IRIGHT_SHIFT(z5, PASS1_BITS+3) - & RANGE_MASK]; - + JSAMPLE dcval = FINAL_OUTPUT(z5); + outptr[0] = dcval; outptr[1] = dcval; outptr[2] = dcval; @@ -292,7 +312,7 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, continue; } #endif - + /* Even part */ tmp10 = z5 + (DCTELEM) wsptr[4]; @@ -325,24 +345,16 @@ jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, tmp5 = tmp11 - tmp6; tmp4 = tmp10 - tmp5; - /* Final output stage: scale down by a factor of 8 and range-limit */ + /* Final output stage: scale down and range-limit */ - 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]; + outptr[0] = FINAL_OUTPUT(tmp0 + tmp7); + outptr[7] = FINAL_OUTPUT(tmp0 - tmp7); + outptr[1] = FINAL_OUTPUT(tmp1 + tmp6); + outptr[6] = FINAL_OUTPUT(tmp1 - tmp6); + outptr[2] = FINAL_OUTPUT(tmp2 + tmp5); + outptr[5] = FINAL_OUTPUT(tmp2 - tmp5); + outptr[3] = FINAL_OUTPUT(tmp3 + tmp4); + outptr[4] = FINAL_OUTPUT(tmp3 - tmp4); wsptr += DCTSIZE; /* advance pointer to next row */ } diff --git a/jpeg/jidctint.c b/jpeg/jidctint.c index e30ec8c8a..de2c23fbe 100644 --- a/jpeg/jidctint.c +++ b/jpeg/jidctint.c @@ -2,7 +2,7 @@ * jidctint.c * * Copyright (C) 1991-1998, Thomas G. Lane. - * Modification developed 2002-2018 by Guido Vollbeding. + * Modification developed 2002-2026 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. * @@ -28,7 +28,7 @@ * We also provide IDCT routines with various output sample block sizes for * direct resolution reduction or enlargement and for direct resolving the * common 2x1 and 1x2 subsampling cases without additional resampling: NxN - * (N=1...16), 2NxN, and Nx2N (N=1...8) pixels for one 8x8 input DCT block. + * (N=1...16), 2NxN, and Nx2N (N=1...8) samples for one 8x8 input DCT block. * * For N<8 we simply take the corresponding low-frequency coefficients of * the 8x8 input DCT block and apply an NxN point IDCT on the sub-block @@ -80,33 +80,37 @@ * a problem to do in integer arithmetic. We multiply all the constants * by CONST_SCALE and convert them to integer constants (thus retaining * CONST_BITS bits of precision in the constants). After doing a - * multiplication we have to divide the product by CONST_SCALE, with proper - * rounding, to produce the correct output. This division can be done - * cheaply as a right shift of CONST_BITS bits. We postpone shifting - * as long as possible so that partial sums can be added together with - * full fractional precision. + * multiplication we have to divide the product by CONST_SCALE, with + * proper rounding, to produce the correct output. This division can + * be done cheaply as a right shift of CONST_BITS bits. We postpone + * shifting as long as possible so that partial sums can be added + * together with full fractional precision. * * The outputs of the first pass are scaled up by PASS1_BITS bits so that * they are represented to better-than-integral precision. These outputs - * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word - * with the recommended scaling. (To scale up 12-bit sample data further, an - * intermediate INT32 array would be needed.) + * require JPEG_DATA_PRECISION + PASS1_BITS + 3 bits; this fits in a + * 16-bit word with the recommended scaling. (To scale up higher bit + * depths further, an intermediate INT32 array would be needed.) * - * To avoid overflow of the 32-bit intermediate results in pass 2, we must - * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis - * shows that the values given below are the most effective. + * To avoid overflow of the 32-bit intermediate results in pass 2, we + * must have JPEG_DATA_PRECISION + CONST_BITS + PASS1_BITS <= 26. Error + * analysis shows that the values given below are the most effective. */ -#if BITS_IN_JSAMPLE == 8 +#if JPEG_DATA_PRECISION <= 10 && BITS_IN_JSAMPLE <= 13 #define CONST_BITS 13 -#define PASS1_BITS 2 +#define PASS1_BITS (10 - JPEG_DATA_PRECISION) +#define PASS2_BITS (13 - BITS_IN_JSAMPLE) #else +#if JPEG_DATA_PRECISION <= 13 && BITS_IN_JSAMPLE <= 16 #define CONST_BITS 13 -#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ +#define PASS1_BITS (13 - JPEG_DATA_PRECISION) +#define PASS2_BITS (16 - BITS_IN_JSAMPLE) +#endif #endif -/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus - * causing a lot of useless floating-point operations at run time. +/* Some C compilers fail to reduce "FIX(constant)" at compile time, + * thus causing a lot of useless floating-point operations at run time. * To get around this we use the following pre-calculated constants. * If you change CONST_BITS you may want to add appropriate values. * (With a reasonable C compiler, you can just rely on the FIX() macro...) @@ -142,13 +146,13 @@ /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. - * For 8-bit samples with the recommended scaling, all the variable + * For up to 10-bit data with the recommended scaling, all the variable * and constant values involved are no more than 16 bits wide, so a * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. - * For 12-bit samples, a full 32-bit multiplication will be needed. + * For higher bit depths, a full 32-bit multiplication will be needed. */ -#if BITS_IN_JSAMPLE == 8 +#if JPEG_DATA_PRECISION <= 10 && BITS_IN_JSAMPLE <= 13 #define MULTIPLY(var,const) MULTIPLY16C16(var,const) #else #define MULTIPLY(var,const) ((var) * (const)) @@ -163,6 +167,20 @@ #define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval)) +/* Pass 2 range center and fudge factor for final descale and range-limit. */ + +#if PASS2_BITS > 1 +#define PASS2_OFFSET \ + ((((INT32) RANGE_CENTER) << PASS2_BITS) + (ONE << (PASS2_BITS-1))) +#else +#if PASS2_BITS > 0 +#define PASS2_OFFSET ((((INT32) RANGE_CENTER) << 1) + ONE) +#else +#define PASS2_OFFSET (INT32) RANGE_CENTER +#endif +#endif + + /* * Perform dequantization and inverse DCT on one block of coefficients. * @@ -210,7 +228,11 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) { /* AC terms all zero */ +#if PASS1_BITS > 0 int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; +#else + int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); +#endif wsptr[DCTSIZE*0] = dcval; wsptr[DCTSIZE*1] = dcval; @@ -301,7 +323,7 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* 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. + * which is folded into the PASS2_BITS value. */ wsptr = workspace; @@ -309,9 +331,7 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, 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))); + z2 = (INT32) wsptr[0] + PASS2_OFFSET; /* 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 @@ -325,8 +345,12 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 && wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { /* AC terms all zero */ - JSAMPLE dcval = range_limit[(int) RIGHT_SHIFT(z2, PASS1_BITS+3) +#if PASS2_BITS > 0 + JSAMPLE dcval = range_limit[(int) RIGHT_SHIFT(z2, PASS2_BITS) & RANGE_MASK]; +#else + JSAMPLE dcval = range_limit[(int) z2 & RANGE_MASK]; +#endif outptr[0] = dcval; outptr[1] = dcval; @@ -347,9 +371,15 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, */ z3 = (INT32) wsptr[4]; + z2 <<= CONST_BITS; + z3 <<= CONST_BITS; +#if PASS2_BITS == 0 + /* Add fudge factor here for final descale. */ + z2 += ONE << (CONST_BITS-1); +#endif - tmp0 = (z2 + z3) << CONST_BITS; - tmp1 = (z2 - z3) << CONST_BITS; + tmp0 = z2 + z3; + tmp1 = z2 - z3; z2 = (INT32) wsptr[2]; z3 = (INT32) wsptr[6]; @@ -396,28 +426,28 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += DCTSIZE; /* advance pointer to next row */ @@ -514,10 +544,11 @@ jpeg_idct_7x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp13 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp13 += ONE << (CONST_BITS-1); +#endif z1 = (INT32) wsptr[2]; z2 = (INT32) wsptr[4]; @@ -552,25 +583,25 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 7; /* advance pointer to next row */ @@ -631,7 +662,11 @@ jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, tmp1 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */ tmp0 = tmp1 + ((z1 + z2) << CONST_BITS); tmp2 = tmp1 + ((z3 - z2) << CONST_BITS); +#if PASS1_BITS > 0 tmp1 = (z1 - z2 - z3) << PASS1_BITS; +#else + tmp1 = z1 - z2 - z3; +#endif /* Final output stage */ @@ -652,10 +687,11 @@ jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp0 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp0 += ONE << (CONST_BITS-1); +#endif tmp2 = (INT32) wsptr[4]; tmp10 = MULTIPLY(tmp2, FIX(0.707106781)); /* c4 */ tmp1 = tmp0 + tmp10; @@ -678,22 +714,22 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 6; /* advance pointer to next row */ @@ -773,10 +809,11 @@ jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp12 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp12 += ONE << (CONST_BITS-1); +#endif tmp0 = (INT32) wsptr[2]; tmp1 = (INT32) wsptr[4]; z1 = MULTIPLY(tmp0 + tmp1, FIX(0.790569415)); /* (c2+c4)/2 */ @@ -798,19 +835,19 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 5; /* advance pointer to next row */ @@ -853,8 +890,13 @@ 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]); +#if PASS1_BITS > 0 tmp10 = (tmp0 + tmp2) << PASS1_BITS; tmp12 = (tmp0 - tmp2) << PASS1_BITS; +#else + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; +#endif /* Odd part */ /* Same rotation as in the even part of the 8x8 LL&M IDCT */ @@ -887,13 +929,16 @@ jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp2 = (INT32) wsptr[2]; + tmp0 <<= CONST_BITS; + tmp2 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp0 += ONE << (CONST_BITS-1); +#endif - tmp10 = (tmp0 + tmp2) << CONST_BITS; - tmp12 = (tmp0 - tmp2) << CONST_BITS; + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; /* Odd part */ /* Same rotation as in the even part of the 8x8 LL&M IDCT */ @@ -908,16 +953,16 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 4; /* advance pointer to next row */ @@ -986,10 +1031,11 @@ jpeg_idct_3x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp0 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp0 += ONE << (CONST_BITS-1); +#endif tmp2 = (INT32) wsptr[2]; tmp12 = MULTIPLY(tmp2, FIX(0.707106781)); /* c2 */ tmp10 = tmp0 + tmp12; @@ -1003,13 +1049,13 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 3; /* advance pointer to next row */ @@ -1042,8 +1088,15 @@ 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 range center and fudge factor for final descale and range-limit. */ - tmp4 += (((DCTELEM) RANGE_CENTER) << 3) + (1 << 2); + +#if PASS2_BITS > PASS1_BITS + /* Add range center and fudge factor for final downscale and range-limit. */ +#if PASS2_BITS > PASS1_BITS + 1 + tmp4 += (((DCTELEM) RANGE_CENTER) << (PASS2_BITS-PASS1_BITS)) + + (1 << (PASS2_BITS-PASS1_BITS-1)); +#else + tmp4 += (((DCTELEM) RANGE_CENTER) << 1) + 1; +#endif tmp0 = tmp4 + tmp5; tmp2 = tmp4 - tmp5; @@ -1060,14 +1113,66 @@ jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Row 0 */ outptr = output_buf[0] + output_col; - outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp1, 3) & RANGE_MASK]; - outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp1, 3) & RANGE_MASK]; + outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp1, + PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; + outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp1, + PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; /* Row 1 */ outptr = output_buf[1] + output_col; - outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp2 + tmp3, 3) & RANGE_MASK]; - outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp2 - tmp3, 3) & RANGE_MASK]; + outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp2 + tmp3, + PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; + outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp2 - tmp3, + PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; +#else +#if PASS2_BITS == PASS1_BITS + tmp4 += (DCTELEM) RANGE_CENTER; /* add range center for final range-limit */ + + tmp0 = tmp4 + tmp5; + tmp2 = tmp4 - tmp5; + + /* Column 1 */ + tmp4 = DEQUANTIZE(coef_block[DCTSIZE*0+1], quantptr[DCTSIZE*0+1]); + tmp5 = DEQUANTIZE(coef_block[DCTSIZE*1+1], quantptr[DCTSIZE*1+1]); +#else + tmp4 <<= (PASS1_BITS-PASS2_BITS); /* upscale */ + tmp5 <<= (PASS1_BITS-PASS2_BITS); /* upscale */ + + tmp4 += (DCTELEM) RANGE_CENTER; /* add range center for final range-limit */ + + tmp0 = tmp4 + tmp5; + tmp2 = tmp4 - tmp5; + + /* Column 1 */ + tmp4 = DEQUANTIZE(coef_block[DCTSIZE*0+1], quantptr[DCTSIZE*0+1]); + tmp5 = DEQUANTIZE(coef_block[DCTSIZE*1+1], quantptr[DCTSIZE*1+1]); + + tmp4 <<= (PASS1_BITS-PASS2_BITS); /* upscale */ + tmp5 <<= (PASS1_BITS-PASS2_BITS); /* upscale */ +#endif + + tmp1 = tmp4 + tmp5; + tmp3 = tmp4 - tmp5; + + /* Pass 2: process 2 rows, store into output array. */ + + /* Row 0 */ + outptr = output_buf[0] + output_col; + + outptr[0] = range_limit[(int) (tmp0 + tmp1) & RANGE_MASK]; + outptr[1] = range_limit[(int) (tmp0 - tmp1) & RANGE_MASK]; + + /* Row 1 */ + outptr = output_buf[1] + output_col; + + outptr[0] = range_limit[(int) (tmp2 + tmp3) & RANGE_MASK]; + outptr[1] = range_limit[(int) (tmp2 - tmp3) & RANGE_MASK]; +#endif } @@ -1075,8 +1180,7 @@ jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, * Perform dequantization and inverse DCT on one block of coefficients, * producing a reduced-size 1x1 output block. * - * We hardly need an inverse DCT routine for this: just take the - * average pixel value, which is one-eighth of the DC coefficient. + * This is just a rescale of the DC coefficient. */ GLOBAL(void) @@ -1089,16 +1193,29 @@ jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr, JSAMPLE *range_limit = IDCT_range_limit(cinfo); 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]); - /* Add range center and fudge factor for descale and range-limit. */ - dcval += (((DCTELEM) RANGE_CENTER) << 3) + (1 << 2); + +#if PASS2_BITS > PASS1_BITS + /* Add range center and fudge factor for downscale and range-limit. */ +#if PASS2_BITS > PASS1_BITS + 1 + dcval += (((DCTELEM) RANGE_CENTER) << (PASS2_BITS-PASS1_BITS)) + + (1 << (PASS2_BITS-PASS1_BITS-1)); +#else + dcval += (((DCTELEM) RANGE_CENTER) << 1) + 1; +#endif output_buf[0][output_col] = - range_limit[(int) IRIGHT_SHIFT(dcval, 3) & RANGE_MASK]; + range_limit[(int) IRIGHT_SHIFT(dcval, PASS2_BITS-PASS1_BITS) & RANGE_MASK]; +#else +#if PASS2_BITS < PASS1_BITS + dcval <<= (PASS1_BITS-PASS2_BITS); /* upscale */ +#endif + + output_buf[0][output_col] = + range_limit[((int) dcval + RANGE_CENTER) & RANGE_MASK]; +#endif } @@ -1198,10 +1315,11 @@ jpeg_idct_9x9 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp0 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp0 += ONE << (CONST_BITS-1); +#endif z1 = (INT32) wsptr[2]; z2 = (INT32) wsptr[4]; @@ -1243,31 +1361,31 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -1355,7 +1473,11 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr, z2 = MULTIPLY(tmp11, FIX(0.587785252)); /* (c1-c9)/2 */ z4 = z5 - tmp12 - (tmp13 << (CONST_BITS - 1)); +#if PASS1_BITS > 0 tmp12 = (z1 - tmp13 - z3) << PASS1_BITS; +#else + tmp12 = z1 - tmp13 - z3; +#endif tmp11 = MULTIPLY(z1, FIX(1.260073511)) - z2 - z4; /* c3 */ tmp13 = MULTIPLY(z1, FIX(0.642039522)) - z2 + z4; /* c7 */ @@ -1383,10 +1505,11 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; z3 <<= CONST_BITS; +#if PASS2_BITS == 0 + z3 += ONE << (CONST_BITS-1); +#endif z4 = (INT32) wsptr[4]; z1 = MULTIPLY(z4, FIX(1.144122806)); /* c4 */ z2 = MULTIPLY(z4, FIX(0.437016024)); /* c8 */ @@ -1437,34 +1560,34 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -1578,10 +1701,11 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp10 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp10 += ONE << (CONST_BITS-1); +#endif z1 = (INT32) wsptr[2]; z2 = (INT32) wsptr[4]; @@ -1630,37 +1754,37 @@ jpeg_idct_11x11 (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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -1784,10 +1908,11 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; z3 <<= CONST_BITS; +#if PASS2_BITS == 0 + z3 += ONE << (CONST_BITS-1); +#endif z4 = (INT32) wsptr[4]; z4 = MULTIPLY(z4, FIX(1.224744871)); /* c4 */ @@ -1845,40 +1970,40 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -2007,10 +2132,11 @@ jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; z1 <<= CONST_BITS; +#if PASS2_BITS == 0 + z1 += ONE << (CONST_BITS-1); +#endif z2 = (INT32) wsptr[2]; z3 = (INT32) wsptr[4]; @@ -2072,43 +2198,43 @@ jpeg_idct_13x13 (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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -2207,7 +2333,11 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr, tmp14 += z4 + tmp13 - MULTIPLY(z3, FIX(1.6906431334)); /* c1+c9-c11 */ tmp15 += z4 + MULTIPLY(z2, FIX(0.674957567)); /* c1+c11-c5 */ +#if PASS1_BITS > 0 tmp13 = (z1 - z3) << PASS1_BITS; +#else + tmp13 = z1 - z3; +#endif /* Final output stage */ @@ -2236,10 +2366,11 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; z1 <<= CONST_BITS; +#if PASS2_BITS == 0 + z1 += ONE << (CONST_BITS-1); +#endif z4 = (INT32) wsptr[4]; z2 = MULTIPLY(z4, FIX(1.274162392)); /* c4 */ z3 = MULTIPLY(z4, FIX(0.314692123)); /* c12 */ @@ -2297,46 +2428,46 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp16, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp16, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -2470,10 +2601,11 @@ jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; z1 <<= CONST_BITS; +#if PASS2_BITS == 0 + z1 += ONE << (CONST_BITS-1); +#endif z2 = (INT32) wsptr[2]; z3 = (INT32) wsptr[4]; @@ -2538,49 +2670,49 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[14] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp16, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp16, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp27, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -2723,10 +2855,11 @@ jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp0 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp0 += ONE << (CONST_BITS-1); +#endif z1 = (INT32) wsptr[4]; tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */ @@ -2799,52 +2932,52 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[15] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[14] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp27 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp27 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -2900,7 +3033,11 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr, inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) { /* AC terms all zero */ +#if PASS1_BITS > 0 int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; +#else + int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); +#endif wsptr[DCTSIZE*0] = dcval; wsptr[DCTSIZE*1] = dcval; @@ -3000,10 +3137,11 @@ jpeg_idct_16x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp0 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp0 += ONE << (CONST_BITS-1); +#endif z1 = (INT32) wsptr[4]; tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */ @@ -3076,52 +3214,52 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[15] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[14] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp27 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp27 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -3220,10 +3358,11 @@ jpeg_idct_14x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; z1 <<= CONST_BITS; +#if PASS2_BITS == 0 + z1 += ONE << (CONST_BITS-1); +#endif z4 = (INT32) wsptr[4]; z2 = MULTIPLY(z4, FIX(1.274162392)); /* c4 */ z3 = MULTIPLY(z4, FIX(0.314692123)); /* c12 */ @@ -3281,46 +3420,46 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[13] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[12] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp26 + tmp16, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp26 - tmp16, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -3383,7 +3522,11 @@ jpeg_idct_12x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, tmp11 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */ tmp10 = tmp11 + ((z1 + z2) << CONST_BITS); tmp12 = tmp11 + ((z3 - z2) << CONST_BITS); +#if PASS1_BITS > 0 tmp11 = (z1 - z2 - z3) << PASS1_BITS; +#else + tmp11 = z1 - z2 - z3; +#endif /* Final output stage */ @@ -3406,10 +3549,11 @@ jpeg_idct_12x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; z3 <<= CONST_BITS; +#if PASS2_BITS == 0 + z3 += ONE << (CONST_BITS-1); +#endif z4 = (INT32) wsptr[4]; z4 = MULTIPLY(z4, FIX(1.224744871)); /* c4 */ @@ -3467,40 +3611,40 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[11] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[10] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp25 + tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp25 - tmp15, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -3584,10 +3728,11 @@ jpeg_idct_10x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; z3 <<= CONST_BITS; +#if PASS2_BITS == 0 + z3 += ONE << (CONST_BITS-1); +#endif z4 = (INT32) wsptr[4]; z1 = MULTIPLY(z4, FIX(1.144122806)); /* c4 */ z2 = MULTIPLY(z4, FIX(0.437016024)); /* c8 */ @@ -3638,34 +3783,34 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[9] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[8] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23 + tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp23 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp24 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp24 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 8; /* advance pointer to next row */ @@ -3711,8 +3856,13 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); tmp2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); +#if PASS1_BITS > 0 tmp10 = (tmp0 + tmp2) << PASS1_BITS; tmp12 = (tmp0 - tmp2) << PASS1_BITS; +#else + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; +#endif /* Odd part */ /* Same rotation as in the even part of the 8x8 LL&M IDCT */ @@ -3738,7 +3888,7 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* 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. + * which is folded into the PASS2_BITS value. * 8-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16). */ @@ -3751,13 +3901,17 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, */ /* 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))); + z2 = (INT32) wsptr[0] + PASS2_OFFSET; z3 = (INT32) wsptr[4]; + z2 <<= CONST_BITS; + z3 <<= CONST_BITS; +#if PASS2_BITS == 0 + /* Add fudge factor here for final descale. */ + z2 += ONE << (CONST_BITS-1); +#endif - tmp0 = (z2 + z3) << CONST_BITS; - tmp1 = (z2 - z3) << CONST_BITS; + tmp0 = z2 + z3; + tmp1 = z2 - z3; z2 = (INT32) wsptr[2]; z3 = (INT32) wsptr[6]; @@ -3804,28 +3958,28 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += DCTSIZE; /* advance pointer to next row */ @@ -3898,10 +4052,11 @@ jpeg_idct_6x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp0 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp0 += ONE << (CONST_BITS-1); +#endif tmp2 = (INT32) wsptr[4]; tmp10 = MULTIPLY(tmp2, FIX(0.707106781)); /* c4 */ tmp1 = tmp0 + tmp10; @@ -3924,22 +4079,22 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 6; /* advance pointer to next row */ @@ -4001,12 +4156,34 @@ jpeg_idct_4x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ - /* Add range center and fudge factor for final descale and range-limit. */ - tmp0 = wsptr[0] + ((((INT32) RANGE_CENTER) << 3) + (ONE << 2)); + tmp0 = wsptr[0]; tmp2 = wsptr[2]; - tmp10 = (tmp0 + tmp2) << CONST_BITS; - tmp12 = (tmp0 - tmp2) << CONST_BITS; + /* Add range center and fudge factor for final descale and range-limit. */ +#if PASS2_BITS > PASS1_BITS +#if PASS2_BITS > PASS1_BITS + 1 + tmp0 += (((INT32) RANGE_CENTER) << (PASS2_BITS-PASS1_BITS)) + + (ONE << (PASS2_BITS-PASS1_BITS-1)); +#else + tmp0 += (((INT32) RANGE_CENTER) << 1) + ONE; +#endif + tmp0 <<= CONST_BITS; +#else +#if PASS2_BITS == PASS1_BITS + tmp0 += (INT32) RANGE_CENTER; + tmp0 <<= CONST_BITS; + tmp0 += ONE << (CONST_BITS-1); +#else + tmp0 <<= CONST_BITS; + tmp0 += (((INT32) RANGE_CENTER) << (CONST_BITS+PASS2_BITS-PASS1_BITS)) + + (ONE << (CONST_BITS+PASS2_BITS-PASS1_BITS-1)); +#endif +#endif + + tmp2 <<= CONST_BITS; + + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; /* Odd part */ /* Same rotation as in the even part of the 8x8 LL&M IDCT */ @@ -4020,18 +4197,22 @@ 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]; - outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+3) - & RANGE_MASK]; - outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, - CONST_BITS+3) - & RANGE_MASK]; - outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, - CONST_BITS+3) - & RANGE_MASK]; + outptr[0] = + range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; + outptr[3] = + range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; + outptr[1] = + range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, + CONST_BITS+PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; + outptr[2] = + range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, + CONST_BITS+PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; wsptr += 4; /* advance pointer to next row */ } @@ -4066,8 +4247,6 @@ jpeg_idct_2x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ 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 */ @@ -4075,8 +4254,32 @@ jpeg_idct_2x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Final output stage */ - outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp1, 3) & RANGE_MASK]; - outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp1, 3) & RANGE_MASK]; +#if PASS2_BITS > PASS1_BITS + /* Add range center and fudge factor for downscale and range-limit. */ +#if PASS2_BITS > PASS1_BITS + 1 + tmp0 += (((DCTELEM) RANGE_CENTER) << (PASS2_BITS-PASS1_BITS)) + + (1 << (PASS2_BITS-PASS1_BITS-1)); +#else + tmp0 += (((DCTELEM) RANGE_CENTER) << 1) + 1; +#endif + + outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp1, + PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; + outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp1, + PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; +#else +#if PASS2_BITS < PASS1_BITS + tmp0 <<= (PASS1_BITS-PASS2_BITS); /* upscale */ + tmp1 <<= (PASS1_BITS-PASS2_BITS); /* upscale */ +#endif + + tmp0 += (DCTELEM) RANGE_CENTER; /* add range center for range-limit */ + + outptr[0] = range_limit[(int) (tmp0 + tmp1) & RANGE_MASK]; + outptr[1] = range_limit[(int) (tmp0 - tmp1) & RANGE_MASK]; +#endif } @@ -4209,7 +4412,7 @@ jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* 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. + * which is folded into the PASS2_BITS value. * 8-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/16). */ @@ -4222,13 +4425,17 @@ jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr, */ /* 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))); + z2 = (INT32) wsptr[0] + PASS2_OFFSET; z3 = (INT32) wsptr[4]; + z2 <<= CONST_BITS; + z3 <<= CONST_BITS; +#if PASS2_BITS == 0 + /* Add fudge factor here for final descale. */ + z2 += ONE << (CONST_BITS-1); +#endif - tmp0 = (z2 + z3) << CONST_BITS; - tmp1 = (z2 - z3) << CONST_BITS; + tmp0 = z2 + z3; + tmp1 = z2 - z3; z2 = (INT32) wsptr[2]; z3 = (INT32) wsptr[6]; @@ -4275,28 +4482,28 @@ jpeg_idct_8x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ outptr[0] = range_limit[(int) RIGHT_SHIFT(tmp10 + tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[7] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp3, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp1, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp13 + tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp13 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += DCTSIZE; /* advance pointer to next row */ @@ -4396,7 +4603,11 @@ jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr, tmp14 += z4 + tmp13 - MULTIPLY(z3, FIX(1.6906431334)); /* c1+c9-c11 */ tmp15 += z4 + MULTIPLY(z2, FIX(0.674957567)); /* c1+c11-c5 */ +#if PASS1_BITS > 0 tmp13 = (z1 - z3) << PASS1_BITS; +#else + tmp13 = z1 - z3; +#endif /* Final output stage */ @@ -4427,10 +4638,11 @@ jpeg_idct_7x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp23 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp23 += ONE << (CONST_BITS-1); +#endif z1 = (INT32) wsptr[2]; z2 = (INT32) wsptr[4]; @@ -4465,25 +4677,25 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[6] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp23, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 7; /* advance pointer to next row */ @@ -4610,10 +4822,11 @@ jpeg_idct_6x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp10 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp10 += ONE << (CONST_BITS-1); +#endif tmp12 = (INT32) wsptr[4]; tmp20 = MULTIPLY(tmp12, FIX(0.707106781)); /* c4 */ tmp11 = tmp10 + tmp20; @@ -4636,22 +4849,22 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[5] = range_limit[(int) RIGHT_SHIFT(tmp20 - tmp10, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp21 + tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp21 - tmp11, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp22 + tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp22 - tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 6; /* advance pointer to next row */ @@ -4740,7 +4953,11 @@ jpeg_idct_5x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr, z2 = MULTIPLY(tmp11, FIX(0.587785252)); /* (c1-c9)/2 */ z4 = z5 - tmp12 - (tmp13 << (CONST_BITS - 1)); +#if PASS1_BITS > 0 tmp12 = (z1 - tmp13 - z3) << PASS1_BITS; +#else + tmp12 = z1 - tmp13 - z3; +#endif tmp11 = MULTIPLY(z1, FIX(1.260073511)) - z2 - z4; /* c3 */ tmp13 = MULTIPLY(z1, FIX(0.642039522)) - z2 + z4; /* c7 */ @@ -4770,10 +4987,11 @@ jpeg_idct_5x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp12 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp12 += ONE << (CONST_BITS-1); +#endif tmp13 = (INT32) wsptr[2]; tmp14 = (INT32) wsptr[4]; z1 = MULTIPLY(tmp13 + tmp14, FIX(0.790569415)); /* (c2+c4)/2 */ @@ -4795,19 +5013,19 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[4] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp13, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp11 + tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp11 - tmp14, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 5; /* advance pointer to next row */ @@ -4863,7 +5081,11 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr, inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) { /* AC terms all zero */ +#if PASS1_BITS > 0 int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; +#else + int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); +#endif wsptr[4*0] = dcval; wsptr[4*1] = dcval; @@ -4964,13 +5186,16 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp2 = (INT32) wsptr[2]; + tmp0 <<= CONST_BITS; + tmp2 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp0 += ONE << (CONST_BITS-1); +#endif - tmp10 = (tmp0 + tmp2) << CONST_BITS; - tmp12 = (tmp0 - tmp2) << CONST_BITS; + tmp10 = tmp0 + tmp2; + tmp12 = tmp0 - tmp2; /* Odd part */ /* Same rotation as in the even part of the 8x8 LL&M IDCT */ @@ -4985,16 +5210,16 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[3] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp12 + tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp12 - tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 4; /* advance pointer to next row */ @@ -5056,7 +5281,11 @@ jpeg_idct_3x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, tmp1 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */ tmp0 = tmp1 + ((z1 + z2) << CONST_BITS); tmp2 = tmp1 + ((z3 - z2) << CONST_BITS); +#if PASS1_BITS > 0 tmp1 = (z1 - z2 - z3) << PASS1_BITS; +#else + tmp1 = z1 - z2 - z3; +#endif /* Final output stage */ @@ -5079,10 +5308,11 @@ jpeg_idct_3x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ /* 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 = (INT32) wsptr[0] + PASS2_OFFSET; tmp0 <<= CONST_BITS; +#if PASS2_BITS == 0 + tmp0 += ONE << (CONST_BITS-1); +#endif tmp2 = (INT32) wsptr[2]; tmp12 = MULTIPLY(tmp2, FIX(0.707106781)); /* c2 */ tmp10 = tmp0 + tmp12; @@ -5096,13 +5326,13 @@ 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) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[2] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp2, - CONST_BITS+PASS1_BITS+3) + CONST_BITS+PASS2_BITS) & RANGE_MASK]; wsptr += 3; /* advance pointer to next row */ @@ -5178,8 +5408,8 @@ jpeg_idct_2x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* 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))); + ((((INT32) RANGE_CENTER) << (CONST_BITS+PASS2_BITS-PASS1_BITS)) + + (ONE << (CONST_BITS+PASS2_BITS-PASS1_BITS-1))); /* Odd part */ @@ -5187,10 +5417,14 @@ 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]; - outptr[1] = range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, CONST_BITS+3) - & RANGE_MASK]; + outptr[0] = + range_limit[(int) RIGHT_SHIFT(tmp10 + tmp0, + CONST_BITS+PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; + outptr[1] = + range_limit[(int) RIGHT_SHIFT(tmp10 - tmp0, + CONST_BITS+PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; wsptr += 2; /* advance pointer to next row */ } @@ -5221,8 +5455,6 @@ jpeg_idct_1x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Even part */ 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 */ @@ -5230,10 +5462,34 @@ jpeg_idct_1x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, /* Final output stage */ +#if PASS2_BITS > PASS1_BITS + /* Add range center and fudge factor for downscale and range-limit. */ +#if PASS2_BITS > PASS1_BITS + 1 + tmp0 += (((DCTELEM) RANGE_CENTER) << (PASS2_BITS-PASS1_BITS)) + + (1 << (PASS2_BITS-PASS1_BITS-1)); +#else + tmp0 += (((DCTELEM) RANGE_CENTER) << 1) + 1; +#endif + output_buf[0][output_col] = - range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp1, 3) & RANGE_MASK]; + range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp1, PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; output_buf[1][output_col] = - range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp1, 3) & RANGE_MASK]; + range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp1, PASS2_BITS-PASS1_BITS) + & RANGE_MASK]; +#else +#if PASS2_BITS < PASS1_BITS + tmp0 <<= (PASS1_BITS-PASS2_BITS); /* upscale */ + tmp1 <<= (PASS1_BITS-PASS2_BITS); /* upscale */ +#endif + + tmp0 += (DCTELEM) RANGE_CENTER; /* add range center for range-limit */ + + output_buf[0][output_col] = + range_limit[(int) (tmp0 + tmp1) & RANGE_MASK]; + output_buf[1][output_col] = + range_limit[(int) (tmp0 - tmp1) & RANGE_MASK]; +#endif } #endif /* IDCT_SCALING_SUPPORTED */ diff --git a/jpeg/jmorecfg.h b/jpeg/jmorecfg.h index 4638d6af2..2a4591360 100644 --- a/jpeg/jmorecfg.h +++ b/jpeg/jmorecfg.h @@ -2,7 +2,7 @@ * jmorecfg.h * * Copyright (C) 1991-1997, Thomas G. Lane. - * Modified 1997-2022 by Guido Vollbeding. + * Modified 1997-2025 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. * @@ -12,26 +12,62 @@ */ +#define JPEG_DATA_PRECISION 8 /* see table below */ +#define BITS_IN_JSAMPLE JPEG_DATA_PRECISION /* see table below */ /* - * 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, 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. + * Most useful alternative for "HDR" (High Dynamic Range) application + * with backward compatibility for file interchange (see table below; + * move comment marks for selection): +#define BITS_IN_JSAMPLE 10 + */ +/* For still higher demands (see table below): +#define BITS_IN_JSAMPLE 11 + */ +/* or +#define BITS_IN_JSAMPLE 12 + */ + +/* | BITS_IN_JSAMPLE + * JPEG_DATA_PRECISION | read / write with full DCT up to lossless operation + * | exceptions see below + * ------------------------------------------------------------------------------- + * {[_8_]} | _8_ <9> <10> <11>* <12>~ + * [_9_] | <8> _9_ <10> <11> <12>* + * [_10_] | <8> <9> _10_ <11> <12> 13 * + * _11_ | <8> <9> <10> _11_ <12> 13 14 * + * _12_ | <8> <9> <10> <11> _12_ 13 14 15 * + * 13 | 8 9 10 11 12 13 14 15 16 * + * + * _x_ currently and previously implemented - default configuration + * newly implemented + * {x} current standard for file interchange - backward compatible + * [x] next standard for file interchange - common DCT implementation category + * * does not support GCC lossless (GCbCr lossless - requires 1 extra bit) + * ~ 1 bit precision loss - effective 11 bits precision (lossy) + * + * Since the DCT coefficients are 3 bits larger than sample values with normal DCT + * processing, it is possible to support sample values with up to 3 more bits than + * the nominal JPEG data precision parameter by adapted DCT processing with up to + * lossless operation. The generated JPEG files are fully interchangeable for the + * same JPEG data precision parameter. Another BITS_IN_JSAMPLE setting will just + * reconstruct an image with corresponding precision. + * + * A special case for JPEG data precision 8 with 12-bit sample size (4 more bits) + * is provided so that all previously available sample formats are now supported + * for file interchange with backward compatibility. + * If full-feature DCT up to lossless operation with up to 12-bit sample size is + * required, it is recommended to select JPEG data precision 10, because it falls + * in the same DCT implementation category with 8 and 9 which may be commonly + * supported at run-time as the next standard for file interchange. + * + * Remaining bit depths and variability at run-time may 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, 9, 10, 11, or 12 */ - /* * Maximum number of components (color channels) allowed in JPEG image. diff --git a/jpeg/jpeglib.h b/jpeg/jpeglib.h index e7e15ab2c..63d5aa2d6 100644 --- a/jpeg/jpeglib.h +++ b/jpeg/jpeglib.h @@ -2,7 +2,7 @@ * jpeglib.h * * Copyright (C) 1991-1998, Thomas G. Lane. - * Modified 2002-2022 by Guido Vollbeding. + * Modified 2002-2025 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. * @@ -34,12 +34,12 @@ extern "C" { #endif /* Version IDs for the JPEG library. - * Might be useful for tests like "#if JPEG_LIB_VERSION >= 90". + * Might be useful for tests like "#if JPEG_LIB_VERSION >= 100". */ -#define JPEG_LIB_VERSION 90 /* Compatibility version 9.0 */ -#define JPEG_LIB_VERSION_MAJOR 9 -#define JPEG_LIB_VERSION_MINOR 6 +#define JPEG_LIB_VERSION 100 /* Compatibility version 10.0 */ +#define JPEG_LIB_VERSION_MAJOR 10 +#define JPEG_LIB_VERSION_MINOR 0 /* Various constants determining the sizes of things. @@ -384,8 +384,9 @@ struct jpeg_compress_struct { UINT16 Y_density; /* Vertical pixel density */ boolean write_Adobe_marker; /* should an Adobe marker be written? */ - J_COLOR_TRANSFORM color_transform; /* Color transform identifier, writes LSE marker if nonzero */ + J_COLOR_TRANSFORM color_transform; + UINT16 LSE_maxtrans; /* LSE MAXTRANS value, usually = MAXJSAMPLE */ /* State variable: index of next scanline to be written to * jpeg_write_scanlines(). Application may use this to control its @@ -606,8 +607,9 @@ struct jpeg_decompress_struct { boolean saw_Adobe_marker; /* TRUE iff an Adobe APP14 marker was found */ UINT8 Adobe_transform; /* Color transform code from Adobe marker */ - J_COLOR_TRANSFORM color_transform; /* Color transform identifier derived from LSE marker, otherwise zero */ + J_COLOR_TRANSFORM color_transform; + UINT16 LSE_maxtrans; /* LSE MAXTRANS value, usually = MAXJSAMPLE */ boolean CCIR601_sampling; /* TRUE=first samples are cosited */ diff --git a/jpeg/jversion.h b/jpeg/jversion.h index df53ef5e5..7db7643aa 100644 --- a/jpeg/jversion.h +++ b/jpeg/jversion.h @@ -1,7 +1,7 @@ /* * jversion.h * - * Copyright (C) 1991-2024, Thomas G. Lane, Guido Vollbeding. + * Copyright (C) 1991-2026, 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 "9f 14-Jan-2024" +#define JVERSION "10 25-Jan-2026" -#define JCOPYRIGHT "Copyright (C) 2024, Thomas G. Lane, Guido Vollbeding" +#define JCOPYRIGHT "Copyright (C) 2026, Thomas G. Lane, Guido Vollbeding" diff --git a/jpeg/usage.txt b/jpeg/usage.txt index 92bbb41ff..4a532d37d 100644 --- a/jpeg/usage.txt +++ b/jpeg/usage.txt @@ -555,6 +555,10 @@ For example you may want to do jpegtran -rot 90 -perfect foo.jpg || djpeg foo.jpg | pnmflip -r90 | cjpeg to do a perfect rotation if available or an approximated one if not. +A lossless negation function can be used in case an image file contains +inverted component data: + -negate Negate image (invert component data). + We also offer a lossless-crop option, which discards data outside a given image region but losslessly preserves what is inside. Like the rotate and flip transforms, lossless crop is restricted by the current JPEG format: the @@ -591,7 +595,7 @@ region with repeated reflections of the outside area, instead of gray out. Another option is lossless-drop, which replaces data at a given image position by another image: - -drop +X+Y filename Drop another image + -drop +X+Y filename Drop another image. Both source images must have the same subsampling values. It is best if they also have the same quantization, otherwise quantization adaption occurs.