jcdctmgr.c~
来自「JPEG Image compression using IJG standar」· C~ 代码 · 共 674 行 · 第 1/2 页
C~
674 行
/* * 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; DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no]; DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */ JDIMENSION bi; int i, j; /* JIE */ JCOEFPTR tmp0; sample_data += start_row; /* fold in the vertical offset once */ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) { /* Load data into workspace, applying unsigned->signed conversion */ { register DCTELEM *workspaceptr; register JSAMPROW elemptr; register int elemr; workspaceptr = workspace; for (elemr = 0; elemr < DCTSIZE; elemr++) { elemptr = sample_data[elemr] + start_col;#if DCTSIZE == 8 /* unroll the inner loop */ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;#else { register int elemc; for (elemc = DCTSIZE; elemc > 0; elemc--) { *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; } }#endif } } /* Perform the DCT */ (*do_dct) (workspace);/*******************//* Jie: test code *//*******************/ /*fprintf(stderr, "\nQuantization Table:\n"); for (i = 0; i<8; i++){ for (j = 0; j < 8; j ++) { fprintf(stderr, "%10d", divisors[i*8 + j]); } fprintf(stderr, "\n"); } */ if (first_blk == 1){ fprintf(stderr, "\nAfter forward DCT:(This is trueDCT * (Scale * 8)\n"); for (i = 0; i<8; i++){ for (j = 0; j < 8; j ++) { fprintf(stderr, "%10.2f", (float) workspace[i*8 + j]); } fprintf(stderr, "\n"); } } first_blk = 0; /* Quantize/descale the coefficients, and store into coef_blocks[] */ { register DCTELEM temp, qval; register int i; register JCOEFPTR output_ptr = coef_blocks[bi]; if (lossless_codec) { /* skip quantization if lossless is specified. */ for (i = 0; i < DCTSIZE2; i++) { output_ptr[i] = (JCOEF) workspace[i]; } } else { 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; } } } /*******************//* Jie: test code *//*******************/ /* fprintf(stderr, "\nAfter quantization: trueDCT/Q0)\n"); tmp0 = coef_blocks[0]; for (i = 0; i<8; i++){ for (j = 0; j < 8; j ++) { fprintf(stderr, "%10d", tmp0[i*8 + j]); } fprintf(stderr, "\n"); } */ }}#ifdef DCT_FLOAT_SUPPORTEDMETHODDEF(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. */{ int i, j;/* remove */ JCOEFPTR tmp0; /* 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; 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) { /* Load data into workspace, applying unsigned->signed conversion */ { register FAST_FLOAT *workspaceptr; register JSAMPROW elemptr; register int elemr; workspaceptr = workspace; for (elemr = 0; elemr < DCTSIZE; elemr++) { elemptr = sample_data[elemr] + start_col;#if DCTSIZE == 8 /* unroll the inner loop */ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);#else { register int elemc; for (elemc = DCTSIZE; elemc > 0; elemc--) { *workspaceptr++ = (FAST_FLOAT) (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE); } }#endif } } /* Perform the DCT */ (*do_dct) (workspace);/*******************//* Jie: test code *//*******************/ /*fprintf(stderr, "\nQuantization Table:\n"); for (i = 0; i<8; i++){ for (j = 0; j < 8; j ++) { fprintf(stderr, "%10.2f", 1 / divisors[i*8 + j]); } fprintf(stderr, "\n"); } fprintf(stderr, "\nAfter forward DCT:(This is trueDCT * (Scale * 8)\n"); for (i = 0; i<8; i++){ for (j = 0; j < 8; j ++) { fprintf(stderr, "%10.2f", (float) workspace[i*8 + j]); } fprintf(stderr, "\n"); } */ /* 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); } }/*******************//* Jie: test code *//*******************/ /* fprintf(stderr, "\nAfter quantization:(With Qfactor=50: divide by Q0 and 8Scale: get trueDCT/Q0)\n"); tmp0 = coef_blocks[0]; for (i = 0; i<8; i++){ for (j = 0; j < 8; j ++) { fprintf(stderr, "%10d", tmp0[i*8 + j]); } fprintf(stderr, "\n"); } */ }}#endif /* DCT_FLOAT_SUPPORTED *//* * Initialize FDCT manager. */GLOBAL(void)jinit_forward_dct (j_compress_ptr cinfo){ my_fdct_ptr fdct; int i; fdct = (my_fdct_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_fdct_controller)); cinfo->fdct = (struct jpeg_forward_dct *) fdct; fdct->pub.start_pass = start_pass_fdctmgr; switch (cinfo->dct_method) {#ifdef DCT_ISLOW_SUPPORTED case JDCT_ISLOW: fdct->pub.forward_DCT = forward_DCT; fdct->do_dct = jpeg_fdct_islow; break;#endif#ifdef DCT_IFAST_SUPPORTED case JDCT_IFAST: fdct->pub.forward_DCT = forward_DCT; fdct->do_dct = jpeg_fdct_ifast; break;#endif#ifdef DCT_FLOAT_SUPPORTED case JDCT_FLOAT: fdct->pub.forward_DCT = forward_DCT_float; fdct->do_float_dct = jpeg_fdct_float; break;#endif /* Jie 05/18/00*/#ifdef DCT_BIN_A1_SUPPORTED case JDCT_BIN_A1: fdct->pub.forward_DCT = forward_DCT; fdct->do_dct = jpeg_fdct_bin_a1; break;#endif#ifdef DCT_BIN_B1_SUPPORTED case JDCT_BIN_B1: fdct->pub.forward_DCT = forward_DCT; fdct->do_dct = jpeg_fdct_bin_b1; break;#endif#ifdef DCT_BIN_C1_SUPPORTED case JDCT_BIN_C1: fdct->pub.forward_DCT = forward_DCT; fdct->do_dct = jpeg_fdct_bin_c1; break;#endif#ifdef DCT_BIN_L1_SUPPORTED case JDCT_BIN_L1: fdct->pub.forward_DCT = forward_DCT; fdct->do_dct = jpeg_fdct_bin_l1; 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;#ifdef DCT_FLOAT_SUPPORTED fdct->float_divisors[i] = NULL;#endif }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?