/// <summary> /// Multiply a DCTELEM variable by an int constant, and immediately /// descale to yield a DCTELEM result. /// </summary> private static int FAST_INTEGER_MULTIPLY(int var, int c) { #if !USE_ACCURATE_ROUNDING return(JpegUtils.RIGHT_SHIFT((var) * (c), FAST_INTEGER_CONST_BITS)); #else return(JpegUtils.DESCALE((var) * (c), FAST_INTEGER_CONST_BITS)); #endif }
/// <summary> /// Perform the forward DCT on one block of samples. /// NOTE: this code only copes with 8x8 DCTs. /// /// A slow-but-accurate integer implementation of the /// forward DCT (Discrete Cosine Transform). /// /// A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT /// on each column. 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 an algorithm described in /// C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT /// Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics, /// Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991. /// The primary algorithm described there uses 11 multiplies and 29 adds. /// We use their alternate method with 12 multiplies and 32 adds. /// The advantage of this method is that no data path contains more than one /// multiplication; this allows a very simple and accurate implementation in /// scaled fixed-point arithmetic, with a minimal number of shifts. /// /// The poop on this scaling stuff is as follows: /// /// Each 1-D DCT step produces outputs which are a factor of sqrt(N) /// larger than the true DCT outputs. The final outputs are therefore /// a factor of N larger than desired; since N=8 this can be cured by /// a simple right shift at the end of the algorithm. The advantage of /// this arrangement is that we save two multiplications per 1-D DCT, /// because the y0 and y4 outputs need not be divided by sqrt(N). /// In the IJG code, this factor of 8 is removed by the quantization /// step, NOT here. /// /// We have to do addition and subtraction of the integer inputs, which /// is no problem, and multiplication by fractional constants, which is /// a problem to do in integer arithmetic. We multiply all the constants /// by CONST_SCALE and convert them to integer constants (thus retaining /// SLOW_INTEGER_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 SLOW_INTEGER_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 SLOW_INTEGER_PASS1_BITS bits so that /// they are represented to better-than-integral precision. These outputs /// require BITS_IN_JSAMPLE + SLOW_INTEGER_PASS1_BITS + 3 bits; this fits in a 16-bit word /// with the recommended scaling. (For 12-bit sample data, the intermediate /// array is int anyway.) /// /// To avoid overflow of the 32-bit intermediate results in pass 2, we must /// have BITS_IN_JSAMPLE + SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS <= 26. Error analysis /// shows that the values given below are the most effective. /// </summary> private static void jpeg_fdct_islow(int[] data) { /* Pass 1: process rows. */ /* Note results are scaled up by sqrt(8) compared to a true DCT; */ /* furthermore, we scale the results by 2**SLOW_INTEGER_PASS1_BITS. */ int dataIndex = 0; for (int ctr = JpegConstants.DCTSIZE - 1; ctr >= 0; ctr--) { int tmp0 = data[dataIndex + 0] + data[dataIndex + 7]; int tmp7 = data[dataIndex + 0] - data[dataIndex + 7]; int tmp1 = data[dataIndex + 1] + data[dataIndex + 6]; int tmp6 = data[dataIndex + 1] - data[dataIndex + 6]; int tmp2 = data[dataIndex + 2] + data[dataIndex + 5]; int tmp5 = data[dataIndex + 2] - data[dataIndex + 5]; int tmp3 = data[dataIndex + 3] + data[dataIndex + 4]; int tmp4 = data[dataIndex + 3] - data[dataIndex + 4]; /* Even part per LL&M figure 1 --- note that published figure is faulty; * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". */ int tmp10 = tmp0 + tmp3; int tmp13 = tmp0 - tmp3; int tmp11 = tmp1 + tmp2; int tmp12 = tmp1 - tmp2; data[dataIndex + 0] = (tmp10 + tmp11) << SLOW_INTEGER_PASS1_BITS; data[dataIndex + 4] = (tmp10 - tmp11) << SLOW_INTEGER_PASS1_BITS; int z1 = (tmp12 + tmp13) * SLOW_INTEGER_FIX_0_541196100; data[dataIndex + 2] = JpegUtils.DESCALE(z1 + tmp13 * SLOW_INTEGER_FIX_0_765366865, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS); data[dataIndex + 6] = JpegUtils.DESCALE(z1 + tmp12 * (-SLOW_INTEGER_FIX_1_847759065), SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS); /* Odd part per figure 8 --- note paper omits factor of sqrt(2). * cK represents cos(K*pi/16). * i0..i3 in the paper are tmp4..tmp7 here. */ z1 = tmp4 + tmp7; int z2 = tmp5 + tmp6; int z3 = tmp4 + tmp6; int z4 = tmp5 + tmp7; int z5 = (z3 + z4) * SLOW_INTEGER_FIX_1_175875602; /* sqrt(2) * c3 */ tmp4 = tmp4 * SLOW_INTEGER_FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */ tmp5 = tmp5 * SLOW_INTEGER_FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */ tmp6 = tmp6 * SLOW_INTEGER_FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */ tmp7 = tmp7 * SLOW_INTEGER_FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */ z1 = z1 * (-SLOW_INTEGER_FIX_0_899976223); /* sqrt(2) * (c7-c3) */ z2 = z2 * (-SLOW_INTEGER_FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ z3 = z3 * (-SLOW_INTEGER_FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ z4 = z4 * (-SLOW_INTEGER_FIX_0_390180644); /* sqrt(2) * (c5-c3) */ z3 += z5; z4 += z5; data[dataIndex + 7] = JpegUtils.DESCALE(tmp4 + z1 + z3, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS); data[dataIndex + 5] = JpegUtils.DESCALE(tmp5 + z2 + z4, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS); data[dataIndex + 3] = JpegUtils.DESCALE(tmp6 + z2 + z3, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS); data[dataIndex + 1] = JpegUtils.DESCALE(tmp7 + z1 + z4, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS); dataIndex += JpegConstants.DCTSIZE; /* advance pointer to next row */ } /* Pass 2: process columns. * We remove the SLOW_INTEGER_PASS1_BITS scaling, but leave the results scaled up * by an overall factor of 8. */ dataIndex = 0; for (int ctr = JpegConstants.DCTSIZE - 1; ctr >= 0; ctr--) { int tmp0 = data[dataIndex + JpegConstants.DCTSIZE * 0] + data[dataIndex + JpegConstants.DCTSIZE * 7]; int tmp7 = data[dataIndex + JpegConstants.DCTSIZE * 0] - data[dataIndex + JpegConstants.DCTSIZE * 7]; int tmp1 = data[dataIndex + JpegConstants.DCTSIZE * 1] + data[dataIndex + JpegConstants.DCTSIZE * 6]; int tmp6 = data[dataIndex + JpegConstants.DCTSIZE * 1] - data[dataIndex + JpegConstants.DCTSIZE * 6]; int tmp2 = data[dataIndex + JpegConstants.DCTSIZE * 2] + data[dataIndex + JpegConstants.DCTSIZE * 5]; int tmp5 = data[dataIndex + JpegConstants.DCTSIZE * 2] - data[dataIndex + JpegConstants.DCTSIZE * 5]; int tmp3 = data[dataIndex + JpegConstants.DCTSIZE * 3] + data[dataIndex + JpegConstants.DCTSIZE * 4]; int tmp4 = data[dataIndex + JpegConstants.DCTSIZE * 3] - data[dataIndex + JpegConstants.DCTSIZE * 4]; /* Even part per LL&M figure 1 --- note that published figure is faulty; * rotator "sqrt(2)*c1" should be "sqrt(2)*c6". */ int tmp10 = tmp0 + tmp3; int tmp13 = tmp0 - tmp3; int tmp11 = tmp1 + tmp2; int tmp12 = tmp1 - tmp2; data[dataIndex + JpegConstants.DCTSIZE * 0] = JpegUtils.DESCALE(tmp10 + tmp11, SLOW_INTEGER_PASS1_BITS); data[dataIndex + JpegConstants.DCTSIZE * 4] = JpegUtils.DESCALE(tmp10 - tmp11, SLOW_INTEGER_PASS1_BITS); int z1 = (tmp12 + tmp13) * SLOW_INTEGER_FIX_0_541196100; data[dataIndex + JpegConstants.DCTSIZE * 2] = JpegUtils.DESCALE(z1 + tmp13 * SLOW_INTEGER_FIX_0_765366865, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS); data[dataIndex + JpegConstants.DCTSIZE * 6] = JpegUtils.DESCALE(z1 + tmp12 * (-SLOW_INTEGER_FIX_1_847759065), SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS); /* Odd part per figure 8 --- note paper omits factor of sqrt(2). * cK represents cos(K*pi/16). * i0..i3 in the paper are tmp4..tmp7 here. */ z1 = tmp4 + tmp7; int z2 = tmp5 + tmp6; int z3 = tmp4 + tmp6; int z4 = tmp5 + tmp7; int z5 = (z3 + z4) * SLOW_INTEGER_FIX_1_175875602; /* sqrt(2) * c3 */ tmp4 = tmp4 * SLOW_INTEGER_FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */ tmp5 = tmp5 * SLOW_INTEGER_FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */ tmp6 = tmp6 * SLOW_INTEGER_FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */ tmp7 = tmp7 * SLOW_INTEGER_FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */ z1 = z1 * (-SLOW_INTEGER_FIX_0_899976223); /* sqrt(2) * (c7-c3) */ z2 = z2 * (-SLOW_INTEGER_FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ z3 = z3 * (-SLOW_INTEGER_FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ z4 = z4 * (-SLOW_INTEGER_FIX_0_390180644); /* sqrt(2) * (c5-c3) */ z3 += z5; z4 += z5; data[dataIndex + JpegConstants.DCTSIZE * 7] = JpegUtils.DESCALE(tmp4 + z1 + z3, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS); data[dataIndex + JpegConstants.DCTSIZE * 5] = JpegUtils.DESCALE(tmp5 + z2 + z4, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS); data[dataIndex + JpegConstants.DCTSIZE * 3] = JpegUtils.DESCALE(tmp6 + z2 + z3, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS); data[dataIndex + JpegConstants.DCTSIZE * 1] = JpegUtils.DESCALE(tmp7 + z1 + z4, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS); dataIndex++; /* advance pointer to next column */ } }
/// <summary> /// Initialize for a processing pass. /// Verify that all referenced Q-tables are present, and set up /// the divisor table for each one. /// In the current implementation, DCT of all components is done during /// the first pass, even if only some components will be output in the /// first scan. Hence all components should be examined here. /// </summary> public virtual void start_pass() { for (int ci = 0; ci < m_cinfo.m_num_components; ci++) { int qtblno = m_cinfo.Component_info[ci].Quant_tbl_no; /* Make sure specified quantization table is present */ if (qtblno < 0 || qtblno >= JpegConstants.NUM_QUANT_TBLS || m_cinfo.m_quant_tbl_ptrs[qtblno] == null) { m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NO_QUANT_TABLE, qtblno); } JQUANT_TBL qtbl = m_cinfo.m_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 */ int i = 0; switch (m_cinfo.m_dct_method) { case J_DCT_METHOD.JDCT_ISLOW: /* For LL&M IDCT method, divisors are equal to raw quantization * coefficients multiplied by 8 (to counteract scaling). */ if (m_divisors[qtblno] == null) { m_divisors[qtblno] = new int [JpegConstants.DCTSIZE2]; } for (i = 0; i < JpegConstants.DCTSIZE2; i++) { m_divisors[qtblno][i] = ((int)qtbl.quantval[i]) << 3; } break; case J_DCT_METHOD.JDCT_IFAST: if (m_divisors[qtblno] == null) { m_divisors[qtblno] = new int [JpegConstants.DCTSIZE2]; } for (i = 0; i < JpegConstants.DCTSIZE2; i++) { m_divisors[qtblno][i] = JpegUtils.DESCALE((int)qtbl.quantval[i] * (int)aanscales[i], CONST_BITS - 3); } break; case J_DCT_METHOD.JDCT_FLOAT: if (m_float_divisors[qtblno] == null) { m_float_divisors[qtblno] = new float [JpegConstants.DCTSIZE2]; } float[] fdtbl = m_float_divisors[qtblno]; i = 0; for (int row = 0; row < JpegConstants.DCTSIZE; row++) { for (int col = 0; col < JpegConstants.DCTSIZE; col++) { fdtbl[i] = (float)(1.0 / (((double)qtbl.quantval[i] * aanscalefactor[row] * aanscalefactor[col] * 8.0))); i++; } } break; default: m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_NOT_COMPILED); break; } } }