📄 jidctred.c
字号:
/*
* jidctred.c
*
* Copyright (C) 1994-1998, Thomas G. Lane.
* [Changed 2002 by Guido Vollbeding]
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains inverse-DCT routines that produce reduced-size output:
* either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
*
* This implementation is based on 4x4 point, 2x2 point, or 1x1 point IDCTs.
* We simply take the corresponding low-frequency coefficients of the 8x8
* input DCT block and apply a 4x4 point, 2x2 point, or 1x1 point IDCT on
* the sub-block to yield the downscaled outputs.
* This can be seen as direct low-pass downsampling from the DCT domain
* point of view rather than the usual spatial domain point of view,
* yielding significant computational savings and results at least
* as good as common bilinear (averaging) spatial downsampling.
*
* 1x1 is trivial: just take the DC coefficient divided by 8.
*
* See jidctint.c for additional comments.
*/
#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
#include "jdct.h" /* Private declarations for DCT subsystem */
#ifdef IDCT_SCALING_SUPPORTED
/*
* This module is specialized to the case DCTSIZE = 8.
*/
#if DCTSIZE != 8
Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
#endif
/* Scaling is the same as in jidctint.c. */
#if BITS_IN_JSAMPLE == 8
#define CONST_BITS 13
#define PASS1_BITS 2
#else
#define CONST_BITS 13
#define PASS1_BITS 1 /* lose a little precision to avoid overflow */
#endif
/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
* causing a lot of useless floating-point operations at run time.
* To get around this we use the following pre-calculated constants.
* If you change CONST_BITS you may want to add appropriate values.
* (With a reasonable C compiler, you can just rely on the FIX() macro...)
*/
#if CONST_BITS == 13
#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */
#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
#else
#define FIX_0_541196100 FIX(0.541196100)
#define FIX_0_765366865 FIX(0.765366865)
#define FIX_1_847759065 FIX(1.847759065)
#endif
/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
* For 8-bit samples with the recommended scaling, all the variable
* and constant values involved are no more than 16 bits wide, so a
* 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
* For 12-bit samples, a full 32-bit multiplication will be needed.
*/
#if BITS_IN_JSAMPLE == 8
#define MULTIPLY(var,const) MULTIPLY16C16(var,const)
#else
#define MULTIPLY(var,const) ((var) * (const))
#endif
/* Dequantize a coefficient by multiplying it by the multiplier-table
* entry; produce an int result. In this module, both inputs and result
* are 16 bits or less, so either int or short multiply will work.
*/
#define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval))
/*
* Perform dequantization and inverse DCT on one block of coefficients,
* producing a reduced-size 4x4 output block.
*/
GLOBAL(void)
jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
JCOEFPTR coef_block,
JSAMPARRAY output_buf, JDIMENSION output_col)
{
INT32 tmp0, tmp2, tmp10, tmp12;
INT32 z1, z2, z3;
JCOEFPTR inptr;
ISLOW_MULT_TYPE * quantptr;
int * wsptr;
JSAMPROW outptr;
JSAMPLE *range_limit = IDCT_range_limit(cinfo);
int ctr;
int workspace[4*4]; /* buffers data between passes */
SHIFT_TEMPS
/* Pass 1: process columns from input, store into work array. */
inptr = coef_block;
quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
wsptr = workspace;
for (ctr = 0; ctr < 4; ctr++, inptr++, quantptr++, wsptr++) {
/* Even part */
tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
tmp10 = (tmp0 + tmp2) << PASS1_BITS;
tmp12 = (tmp0 - tmp2) << PASS1_BITS;
/* Odd part */
/* Same rotation as in the even part of the 8x8 LL&M IDCT */
z2 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
tmp0 = DESCALE(z1 + MULTIPLY(z3, - FIX_1_847759065), CONST_BITS-PASS1_BITS);
tmp2 = DESCALE(z1 + MULTIPLY(z2, FIX_0_765366865), CONST_BITS-PASS1_BITS);
/* Final output stage */
wsptr[4*0] = (int) (tmp10 + tmp2);
wsptr[4*3] = (int) (tmp10 - tmp2);
wsptr[4*1] = (int) (tmp12 + tmp0);
wsptr[4*2] = (int) (tmp12 - tmp0);
}
/* Pass 2: process 4 rows from work array, store into output array. */
wsptr = workspace;
for (ctr = 0; ctr < 4; ctr++) {
outptr = output_buf[ctr] + output_col;
/* Even part */
tmp0 = (INT32) wsptr[0];
tmp2 = (INT32) wsptr[2];
tmp10 = (tmp0 + tmp2) << CONST_BITS;
tmp12 = (tmp0 - tmp2) << CONST_BITS;
/* Odd part */
/* Same rotation as in the even part of the 8x8 LL&M IDCT */
z2 = (INT32) wsptr[1];
z3 = (INT32) wsptr[3];
z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
tmp0 = z1 + MULTIPLY(z3, - FIX_1_847759065);
tmp2 = z1 + MULTIPLY(z2, FIX_0_765366865);
/* Final output stage */
outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
CONST_BITS+PASS1_BITS+3)
& RANGE_MASK];
outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
CONST_BITS+PASS1_BITS+3)
& RANGE_MASK];
outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
CONST_BITS+PASS1_BITS+3)
& RANGE_MASK];
outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
CONST_BITS+PASS1_BITS+3)
& RANGE_MASK];
wsptr += 4; /* advance pointer to next row */
}
}
/*
* Perform dequantization and inverse DCT on one block of coefficients,
* producing a reduced-size 2x2 output block.
*/
GLOBAL(void)
jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
JCOEFPTR coef_block,
JSAMPARRAY output_buf, JDIMENSION output_col)
{
INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
ISLOW_MULT_TYPE * quantptr;
JSAMPROW outptr;
JSAMPLE *range_limit = IDCT_range_limit(cinfo);
SHIFT_TEMPS
/* Pass 1: process columns from input. */
quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
/* Column 0 */
tmp4 = DEQUANTIZE(coef_block[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp5 = DEQUANTIZE(coef_block[DCTSIZE*1], quantptr[DCTSIZE*1]);
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]);
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) DESCALE(tmp0 + tmp1, 3)
& RANGE_MASK];
outptr[1] = range_limit[(int) DESCALE(tmp0 - tmp1, 3)
& RANGE_MASK];
/* Row 1 */
outptr = output_buf[1] + output_col;
outptr[0] = range_limit[(int) DESCALE(tmp2 + tmp3, 3)
& RANGE_MASK];
outptr[1] = range_limit[(int) DESCALE(tmp2 - tmp3, 3)
& RANGE_MASK];
}
/*
* Perform dequantization and inverse DCT on one block of coefficients,
* producing a reduced-size 1x1 output block.
*/
GLOBAL(void)
jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
JCOEFPTR coef_block,
JSAMPARRAY output_buf, JDIMENSION output_col)
{
int dcval;
ISLOW_MULT_TYPE * quantptr;
JSAMPLE *range_limit = IDCT_range_limit(cinfo);
SHIFT_TEMPS
/* We hardly need an inverse DCT routine for this: just take the
* average pixel value, which is one-eighth of the DC coefficient.
*/
quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
dcval = (int) DESCALE((INT32) dcval, 3);
output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
}
#endif /* IDCT_SCALING_SUPPORTED */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -