diff options
Diffstat (limited to 'jcdctmgr.c')
-rw-r--r-- | jcdctmgr.c | 458 |
1 files changed, 197 insertions, 261 deletions
@@ -41,6 +41,132 @@ typedef struct { typedef my_fdct_controller * my_fdct_ptr; +/* 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. + * + * The input samples are taken from the sample_data[] array starting at + * position start_row/start_col, and moving to the right for any additional + * blocks. The quantized coefficients are returned in coef_blocks[]. + */ + +METHODDEF(void) +forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY sample_data, JBLOCKROW coef_blocks, + JDIMENSION start_row, JDIMENSION start_col, + JDIMENSION num_blocks) +/* This version is used for integer DCT implementations. */ +{ + /* This routine is heavily used, so it's worth coding it tightly. */ + my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; + forward_DCT_method_ptr do_dct = fdct->do_dct[compptr->component_index]; + DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no]; + DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */ + JDIMENSION bi; + + sample_data += start_row; /* fold in the vertical offset once */ + + for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) { + /* Perform the DCT */ + (*do_dct) (workspace, sample_data, start_col); + + /* Quantize/descale the coefficients, and store into coef_blocks[] */ + { register DCTELEM temp, qval; + register int i; + register JCOEFPTR output_ptr = coef_blocks[bi]; + + for (i = 0; i < DCTSIZE2; i++) { + qval = divisors[i]; + temp = workspace[i]; + /* Divide the coefficient value by qval, ensuring proper rounding. + * Since C does not specify the direction of rounding for negative + * quotients, we have to force the dividend positive for portability. + * + * In most files, at least half of the output values will be zero + * (at default quantization settings, more like three-quarters...) + * so we should ensure that this case is fast. On many machines, + * a comparison is enough cheaper than a divide to make a special test + * a win. Since both inputs will be nonnegative, we need only test + * for a < b to discover whether a/b is 0. + * If your machine's division is fast enough, define FAST_DIVIDE. + */ +#ifdef FAST_DIVIDE +#define DIVIDE_BY(a,b) a /= b +#else +#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0 +#endif + if (temp < 0) { + temp = -temp; + temp += qval>>1; /* for rounding */ + DIVIDE_BY(temp, qval); + temp = -temp; + } else { + temp += qval>>1; /* for rounding */ + DIVIDE_BY(temp, qval); + } + output_ptr[i] = (JCOEF) temp; + } + } + } +} + + +#ifdef DCT_FLOAT_SUPPORTED + +METHODDEF(void) +forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr, + JSAMPARRAY sample_data, JBLOCKROW coef_blocks, + JDIMENSION start_row, JDIMENSION start_col, + JDIMENSION num_blocks) +/* This version is used for floating-point DCT implementations. */ +{ + /* This routine is heavily used, so it's worth coding it tightly. */ + my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; + float_DCT_method_ptr do_dct = fdct->do_float_dct[compptr->component_index]; + FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no]; + FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */ + JDIMENSION bi; + + sample_data += start_row; /* fold in the vertical offset once */ + + for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) { + /* Perform the DCT */ + (*do_dct) (workspace, sample_data, start_col); + + /* Quantize/descale the coefficients, and store into coef_blocks[] */ + { register FAST_FLOAT temp; + register int i; + register JCOEFPTR output_ptr = coef_blocks[bi]; + + for (i = 0; i < DCTSIZE2; i++) { + /* Apply the quantization and scaling factor */ + temp = workspace[i] * divisors[i]; + /* Round to nearest integer. + * Since C does not specify the direction of rounding for negative + * quotients, we have to force the dividend positive for portability. + * The maximum coefficient size is +-16K (for 12-bit data), so this + * code should work for either 16-bit or 32-bit ints. + */ + output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384); + } + } + } +} + +#endif /* DCT_FLOAT_SUPPORTED */ + + /* * Initialize for a processing pass. * Verify that all referenced Q-tables are present, and set up @@ -56,6 +182,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; JQUANT_TBL * qtbl; DCTELEM * dtbl; @@ -65,190 +192,128 @@ start_pass_fdctmgr (j_compress_ptr cinfo) switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) { #ifdef DCT_SCALING_SUPPORTED case ((1 << 8) + 1): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_1x1; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_1x1; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((2 << 8) + 2): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_2x2; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_2x2; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((3 << 8) + 3): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_3x3; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_3x3; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((4 << 8) + 4): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_4x4; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_4x4; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((5 << 8) + 5): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_5x5; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_5x5; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((6 << 8) + 6): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_6x6; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_6x6; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((7 << 8) + 7): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_7x7; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_7x7; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((9 << 8) + 9): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_9x9; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_9x9; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((10 << 8) + 10): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_10x10; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_10x10; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((11 << 8) + 11): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_11x11; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_11x11; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((12 << 8) + 12): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_12x12; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_12x12; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((13 << 8) + 13): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_13x13; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_13x13; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((14 << 8) + 14): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_14x14; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_14x14; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((15 << 8) + 15): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_15x15; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_15x15; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((16 << 8) + 16): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_16x16; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_16x16; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((16 << 8) + 8): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_16x8; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_16x8; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((14 << 8) + 7): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_14x7; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_14x7; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((12 << 8) + 6): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_12x6; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_12x6; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((10 << 8) + 5): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_10x5; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_10x5; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((8 << 8) + 4): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_8x4; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_8x4; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((6 << 8) + 3): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_6x3; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_6x3; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((4 << 8) + 2): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_4x2; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_4x2; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((2 << 8) + 1): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_2x1; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_2x1; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((8 << 8) + 16): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_8x16; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_8x16; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((7 << 8) + 14): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_7x14; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_7x14; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((6 << 8) + 12): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_6x12; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_6x12; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((5 << 8) + 10): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_5x10; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_5x10; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((4 << 8) + 8): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_4x8; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_4x8; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((3 << 8) + 6): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_3x6; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_3x6; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((2 << 8) + 4): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_2x4; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_2x4; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; case ((1 << 8) + 2): - if (cinfo->dct_method == JDCT_ISLOW) - fdct->do_dct[ci] = jpeg_fdct_1x2; - else - ERREXIT(cinfo, JERR_NOT_COMPILED); + fdct->do_dct[ci] = jpeg_fdct_1x2; + method = JDCT_ISLOW; /* jfdctint uses islow-style table */ break; #endif case ((DCTSIZE << 8) + DCTSIZE): @@ -256,16 +321,19 @@ start_pass_fdctmgr (j_compress_ptr cinfo) #ifdef DCT_ISLOW_SUPPORTED case JDCT_ISLOW: fdct->do_dct[ci] = jpeg_fdct_islow; + method = JDCT_ISLOW; break; #endif #ifdef DCT_IFAST_SUPPORTED case JDCT_IFAST: fdct->do_dct[ci] = jpeg_fdct_ifast; + method = JDCT_IFAST; break; #endif #ifdef DCT_FLOAT_SUPPORTED case JDCT_FLOAT: fdct->do_float_dct[ci] = jpeg_fdct_float; + method = JDCT_FLOAT; break; #endif default: @@ -286,8 +354,8 @@ start_pass_fdctmgr (j_compress_ptr cinfo) qtbl = cinfo->quant_tbl_ptrs[qtblno]; /* Compute divisors for this quant table */ /* We may do this more than once for same table, but it's not a big deal */ - switch (cinfo->dct_method) { -#ifdef DCT_ISLOW_SUPPORTED + switch (method) { +#ifdef PROVIDE_ISLOW_TABLES case JDCT_ISLOW: /* For LL&M IDCT method, divisors are equal to raw quantization * coefficients multiplied by 8 (to counteract scaling). @@ -301,6 +369,7 @@ start_pass_fdctmgr (j_compress_ptr cinfo) for (i = 0; i < DCTSIZE2; i++) { dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3; } + fdct->pub.forward_DCT[ci] = forward_DCT; break; #endif #ifdef DCT_IFAST_SUPPORTED @@ -339,6 +408,7 @@ start_pass_fdctmgr (j_compress_ptr cinfo) CONST_BITS-3); } } + fdct->pub.forward_DCT[ci] = forward_DCT; break; #endif #ifdef DCT_FLOAT_SUPPORTED @@ -375,6 +445,7 @@ start_pass_fdctmgr (j_compress_ptr cinfo) } } } + fdct->pub.forward_DCT[ci] = forward_DCT_float; break; #endif default: @@ -386,120 +457,6 @@ start_pass_fdctmgr (j_compress_ptr cinfo) /* - * Perform forward DCT on one or more blocks of a component. - * - * The input samples are taken from the sample_data[] array starting at - * position start_row/start_col, and moving to the right for any additional - * blocks. The quantized coefficients are returned in coef_blocks[]. - */ - -METHODDEF(void) -forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr, - JSAMPARRAY sample_data, JBLOCKROW coef_blocks, - JDIMENSION start_row, JDIMENSION start_col, - JDIMENSION num_blocks) -/* This version is used for integer DCT implementations. */ -{ - /* This routine is heavily used, so it's worth coding it tightly. */ - my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; - forward_DCT_method_ptr do_dct = fdct->do_dct[compptr->component_index]; - DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no]; - DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */ - JDIMENSION bi; - - sample_data += start_row; /* fold in the vertical offset once */ - - for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) { - /* Perform the DCT */ - (*do_dct) (workspace, sample_data, start_col); - - /* Quantize/descale the coefficients, and store into coef_blocks[] */ - { register DCTELEM temp, qval; - register int i; - register JCOEFPTR output_ptr = coef_blocks[bi]; - - for (i = 0; i < DCTSIZE2; i++) { - qval = divisors[i]; - temp = workspace[i]; - /* Divide the coefficient value by qval, ensuring proper rounding. - * Since C does not specify the direction of rounding for negative - * quotients, we have to force the dividend positive for portability. - * - * In most files, at least half of the output values will be zero - * (at default quantization settings, more like three-quarters...) - * so we should ensure that this case is fast. On many machines, - * a comparison is enough cheaper than a divide to make a special test - * a win. Since both inputs will be nonnegative, we need only test - * for a < b to discover whether a/b is 0. - * If your machine's division is fast enough, define FAST_DIVIDE. - */ -#ifdef FAST_DIVIDE -#define DIVIDE_BY(a,b) a /= b -#else -#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0 -#endif - if (temp < 0) { - temp = -temp; - temp += qval>>1; /* for rounding */ - DIVIDE_BY(temp, qval); - temp = -temp; - } else { - temp += qval>>1; /* for rounding */ - DIVIDE_BY(temp, qval); - } - output_ptr[i] = (JCOEF) temp; - } - } - } -} - - -#ifdef DCT_FLOAT_SUPPORTED - -METHODDEF(void) -forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr, - JSAMPARRAY sample_data, JBLOCKROW coef_blocks, - JDIMENSION start_row, JDIMENSION start_col, - JDIMENSION num_blocks) -/* This version is used for floating-point DCT implementations. */ -{ - /* This routine is heavily used, so it's worth coding it tightly. */ - my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct; - float_DCT_method_ptr do_dct = fdct->do_float_dct[compptr->component_index]; - FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no]; - FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */ - JDIMENSION bi; - - sample_data += start_row; /* fold in the vertical offset once */ - - for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) { - /* Perform the DCT */ - (*do_dct) (workspace, sample_data, start_col); - - /* Quantize/descale the coefficients, and store into coef_blocks[] */ - { register FAST_FLOAT temp; - register int i; - register JCOEFPTR output_ptr = coef_blocks[bi]; - - for (i = 0; i < DCTSIZE2; i++) { - /* Apply the quantization and scaling factor */ - temp = workspace[i] * divisors[i]; - /* Round to nearest integer. - * Since C does not specify the direction of rounding for negative - * quotients, we have to force the dividend positive for portability. - * The maximum coefficient size is +-16K (for 12-bit data), so this - * code should work for either 16-bit or 32-bit ints. - */ - output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384); - } - } - } -} - -#endif /* DCT_FLOAT_SUPPORTED */ - - -/* * Initialize FDCT manager. */ @@ -515,27 +472,6 @@ jinit_forward_dct (j_compress_ptr cinfo) cinfo->fdct = (struct jpeg_forward_dct *) fdct; fdct->pub.start_pass = start_pass_fdctmgr; - switch (cinfo->dct_method) { -#ifdef DCT_ISLOW_SUPPORTED - case JDCT_ISLOW: - fdct->pub.forward_DCT = forward_DCT; - break; -#endif -#ifdef DCT_IFAST_SUPPORTED - case JDCT_IFAST: - fdct->pub.forward_DCT = forward_DCT; - break; -#endif -#ifdef DCT_FLOAT_SUPPORTED - case JDCT_FLOAT: - fdct->pub.forward_DCT = forward_DCT_float; - break; -#endif - default: - ERREXIT(cinfo, JERR_NOT_COMPILED); - break; - } - /* Mark divisor tables unallocated */ for (i = 0; i < NUM_QUANT_TBLS; i++) { fdct->divisors[i] = NULL; |