Path: blob/master/src/java.desktop/share/native/libjavajpeg/jidctred.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jidctred.c6*7* Copyright (C) 1994-1998, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains inverse-DCT routines that produce reduced-size output:12* either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.13*14* The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)15* algorithm used in jidctint.c. We simply replace each 8-to-8 1-D IDCT step16* with an 8-to-4 step that produces the four averages of two adjacent outputs17* (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).18* These steps were derived by computing the corresponding values at the end19* of the normal LL&M code, then simplifying as much as possible.20*21* 1x1 is trivial: just take the DC coefficient divided by 8.22*23* See jidctint.c for additional comments.24*/2526#define JPEG_INTERNALS27#include "jinclude.h"28#include "jpeglib.h"29#include "jdct.h" /* Private declarations for DCT subsystem */3031#ifdef IDCT_SCALING_SUPPORTED323334/*35* This module is specialized to the case DCTSIZE = 8.36*/3738#if DCTSIZE != 839Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */40#endif414243/* Scaling is the same as in jidctint.c. */4445#if BITS_IN_JSAMPLE == 846#define CONST_BITS 1347#define PASS1_BITS 248#else49#define CONST_BITS 1350#define PASS1_BITS 1 /* lose a little precision to avoid overflow */51#endif5253/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus54* causing a lot of useless floating-point operations at run time.55* To get around this we use the following pre-calculated constants.56* If you change CONST_BITS you may want to add appropriate values.57* (With a reasonable C compiler, you can just rely on the FIX() macro...)58*/5960#if CONST_BITS == 1361#define FIX_0_211164243 ((INT32) 1730) /* FIX(0.211164243) */62#define FIX_0_509795579 ((INT32) 4176) /* FIX(0.509795579) */63#define FIX_0_601344887 ((INT32) 4926) /* FIX(0.601344887) */64#define FIX_0_720959822 ((INT32) 5906) /* FIX(0.720959822) */65#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */66#define FIX_0_850430095 ((INT32) 6967) /* FIX(0.850430095) */67#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */68#define FIX_1_061594337 ((INT32) 8697) /* FIX(1.061594337) */69#define FIX_1_272758580 ((INT32) 10426) /* FIX(1.272758580) */70#define FIX_1_451774981 ((INT32) 11893) /* FIX(1.451774981) */71#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */72#define FIX_2_172734803 ((INT32) 17799) /* FIX(2.172734803) */73#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */74#define FIX_3_624509785 ((INT32) 29692) /* FIX(3.624509785) */75#else76#define FIX_0_211164243 FIX(0.211164243)77#define FIX_0_509795579 FIX(0.509795579)78#define FIX_0_601344887 FIX(0.601344887)79#define FIX_0_720959822 FIX(0.720959822)80#define FIX_0_765366865 FIX(0.765366865)81#define FIX_0_850430095 FIX(0.850430095)82#define FIX_0_899976223 FIX(0.899976223)83#define FIX_1_061594337 FIX(1.061594337)84#define FIX_1_272758580 FIX(1.272758580)85#define FIX_1_451774981 FIX(1.451774981)86#define FIX_1_847759065 FIX(1.847759065)87#define FIX_2_172734803 FIX(2.172734803)88#define FIX_2_562915447 FIX(2.562915447)89#define FIX_3_624509785 FIX(3.624509785)90#endif919293/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.94* For 8-bit samples with the recommended scaling, all the variable95* and constant values involved are no more than 16 bits wide, so a96* 16x16->32 bit multiply can be used instead of a full 32x32 multiply.97* For 12-bit samples, a full 32-bit multiplication will be needed.98*/99100#if BITS_IN_JSAMPLE == 8101#define MULTIPLY(var,const) MULTIPLY16C16(var,const)102#else103#define MULTIPLY(var,const) ((var) * (const))104#endif105106107/* Dequantize a coefficient by multiplying it by the multiplier-table108* entry; produce an int result. In this module, both inputs and result109* are 16 bits or less, so either int or short multiply will work.110*/111112#define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval))113114115/*116* Perform dequantization and inverse DCT on one block of coefficients,117* producing a reduced-size 4x4 output block.118*/119120GLOBAL(void)121jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,122JCOEFPTR coef_block,123JSAMPARRAY output_buf, JDIMENSION output_col)124{125INT32 tmp0, tmp2, tmp10, tmp12;126INT32 z1, z2, z3, z4;127JCOEFPTR inptr;128ISLOW_MULT_TYPE * quantptr;129int * wsptr;130JSAMPROW outptr;131JSAMPLE *range_limit = IDCT_range_limit(cinfo);132int ctr;133int workspace[DCTSIZE*4]; /* buffers data between passes */134SHIFT_TEMPS135136/* Pass 1: process columns from input, store into work array. */137138inptr = coef_block;139quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;140wsptr = workspace;141for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {142/* Don't bother to process column 4, because second pass won't use it */143if (ctr == DCTSIZE-4)144continue;145if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&146inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&147inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {148/* AC terms all zero; we need not examine term 4 for 4x4 output */149int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;150151wsptr[DCTSIZE*0] = dcval;152wsptr[DCTSIZE*1] = dcval;153wsptr[DCTSIZE*2] = dcval;154wsptr[DCTSIZE*3] = dcval;155156continue;157}158159/* Even part */160161tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);162tmp0 <<= (CONST_BITS+1);163164z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);165z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);166167tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);168169tmp10 = tmp0 + tmp2;170tmp12 = tmp0 - tmp2;171172/* Odd part */173174z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);175z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);176z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);177z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);178179tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */180+ MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */181+ MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */182+ MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */183184tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */185+ MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */186+ MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */187+ MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */188189/* Final output stage */190191wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);192wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);193wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);194wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);195}196197/* Pass 2: process 4 rows from work array, store into output array. */198199wsptr = workspace;200for (ctr = 0; ctr < 4; ctr++) {201outptr = output_buf[ctr] + output_col;202/* It's not clear whether a zero row test is worthwhile here ... */203204#ifndef NO_ZERO_ROW_TEST205if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&206wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {207/* AC terms all zero */208JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)209& RANGE_MASK];210211outptr[0] = dcval;212outptr[1] = dcval;213outptr[2] = dcval;214outptr[3] = dcval;215216wsptr += DCTSIZE; /* advance pointer to next row */217continue;218}219#endif220221/* Even part */222223tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);224225tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)226+ MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);227228tmp10 = tmp0 + tmp2;229tmp12 = tmp0 - tmp2;230231/* Odd part */232233z1 = (INT32) wsptr[7];234z2 = (INT32) wsptr[5];235z3 = (INT32) wsptr[3];236z4 = (INT32) wsptr[1];237238tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */239+ MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */240+ MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */241+ MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */242243tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */244+ MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */245+ MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */246+ MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */247248/* Final output stage */249250outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,251CONST_BITS+PASS1_BITS+3+1)252& RANGE_MASK];253outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,254CONST_BITS+PASS1_BITS+3+1)255& RANGE_MASK];256outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,257CONST_BITS+PASS1_BITS+3+1)258& RANGE_MASK];259outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,260CONST_BITS+PASS1_BITS+3+1)261& RANGE_MASK];262263wsptr += DCTSIZE; /* advance pointer to next row */264}265}266267268/*269* Perform dequantization and inverse DCT on one block of coefficients,270* producing a reduced-size 2x2 output block.271*/272273GLOBAL(void)274jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,275JCOEFPTR coef_block,276JSAMPARRAY output_buf, JDIMENSION output_col)277{278INT32 tmp0, tmp10, z1;279JCOEFPTR inptr;280ISLOW_MULT_TYPE * quantptr;281int * wsptr;282JSAMPROW outptr;283JSAMPLE *range_limit = IDCT_range_limit(cinfo);284int ctr;285int workspace[DCTSIZE*2]; /* buffers data between passes */286SHIFT_TEMPS287288/* Pass 1: process columns from input, store into work array. */289290inptr = coef_block;291quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;292wsptr = workspace;293for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {294/* Don't bother to process columns 2,4,6 */295if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)296continue;297if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&298inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {299/* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */300int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;301302wsptr[DCTSIZE*0] = dcval;303wsptr[DCTSIZE*1] = dcval;304305continue;306}307308/* Even part */309310z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);311tmp10 = z1 << (CONST_BITS+2);312313/* Odd part */314315z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);316tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */317z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);318tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */319z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);320tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */321z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);322tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */323324/* Final output stage */325326wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);327wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);328}329330/* Pass 2: process 2 rows from work array, store into output array. */331332wsptr = workspace;333for (ctr = 0; ctr < 2; ctr++) {334outptr = output_buf[ctr] + output_col;335/* It's not clear whether a zero row test is worthwhile here ... */336337#ifndef NO_ZERO_ROW_TEST338if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {339/* AC terms all zero */340JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)341& RANGE_MASK];342343outptr[0] = dcval;344outptr[1] = dcval;345346wsptr += DCTSIZE; /* advance pointer to next row */347continue;348}349#endif350351/* Even part */352353tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);354355/* Odd part */356357tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */358+ MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */359+ MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */360+ MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */361362/* Final output stage */363364outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,365CONST_BITS+PASS1_BITS+3+2)366& RANGE_MASK];367outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,368CONST_BITS+PASS1_BITS+3+2)369& RANGE_MASK];370371wsptr += DCTSIZE; /* advance pointer to next row */372}373}374375376/*377* Perform dequantization and inverse DCT on one block of coefficients,378* producing a reduced-size 1x1 output block.379*/380381GLOBAL(void)382jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,383JCOEFPTR coef_block,384JSAMPARRAY output_buf, JDIMENSION output_col)385{386int dcval;387ISLOW_MULT_TYPE * quantptr;388JSAMPLE *range_limit = IDCT_range_limit(cinfo);389SHIFT_TEMPS390391/* We hardly need an inverse DCT routine for this: just take the392* average pixel value, which is one-eighth of the DC coefficient.393*/394quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;395dcval = DEQUANTIZE(coef_block[0], quantptr[0]);396dcval = (int) DESCALE((INT32) dcval, 3);397398output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];399}400401#endif /* IDCT_SCALING_SUPPORTED */402403404