BitMiracle.LibJpeg.Classic.Internal.jpeg_inverse_dct.jpeg_idct_islow C# (CSharp) Method

jpeg_idct_islow() private method

Perform dequantization and inverse DCT on one block of coefficients. NOTE: this code only copes with 8x8 DCTs. A slow-but-accurate integer implementation of the inverse DCT (Discrete Cosine Transform). In the IJG code, this routine must also perform dequantization of the input coefficients. A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT on each row (or vice versa, but it's more convenient to emit a row at a time). 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 IDCT step produces outputs which are a factor of sqrt(N) larger than the true IDCT 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 IDCT, because the y0 and y4 inputs need not be divided by sqrt(N). 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. (To scale up 12-bit sample data further, an intermediate int array would be needed.) 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.
private jpeg_idct_islow ( int component_index, short coef_block, int output_row, int output_col ) : void
component_index int
coef_block short
output_row int
output_col int
return void
        private void jpeg_idct_islow(int component_index, short[] coef_block, int output_row, int output_col)
        {
            /* buffers data between passes */
            int[] workspace = new int[JpegConstants.DCTSIZE2];

            /* Pass 1: process columns from input, store into work array. */
            /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
            /* furthermore, we scale the results by 2**SLOW_INTEGER_PASS1_BITS. */

            int coefBlockIndex = 0;

            int[] quantTable = m_dctTables[component_index].int_array;
            int quantTableIndex = 0;

            int workspaceIndex = 0;

            for (int ctr = JpegConstants.DCTSIZE; ctr > 0; ctr--)
            {
                /* Due to quantization, we will usually find that many of the input
                * coefficients are zero, especially the AC terms.  We can exploit this
                * by short-circuiting the IDCT calculation for any column in which all
                * the AC terms are zero.  In that case each output is equal to the
                * DC coefficient (with scale factor as needed).
                * With typical images and quantization tables, half or more of the
                * column DCT calculations can be simplified this way.
                */

                if (coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 1] == 0 &&
                    coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 2] == 0 &&
                    coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 3] == 0 &&
                    coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 4] == 0 &&
                    coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 5] == 0 &&
                    coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 6] == 0 &&
                    coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 7] == 0)
                {
                    /* AC terms all zero */
                    int dcval = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 0],
                        quantTable[quantTableIndex + JpegConstants.DCTSIZE * 0]) << SLOW_INTEGER_PASS1_BITS;

                    workspace[workspaceIndex + JpegConstants.DCTSIZE * 0] = dcval;
                    workspace[workspaceIndex + JpegConstants.DCTSIZE * 1] = dcval;
                    workspace[workspaceIndex + JpegConstants.DCTSIZE * 2] = dcval;
                    workspace[workspaceIndex + JpegConstants.DCTSIZE * 3] = dcval;
                    workspace[workspaceIndex + JpegConstants.DCTSIZE * 4] = dcval;
                    workspace[workspaceIndex + JpegConstants.DCTSIZE * 5] = dcval;
                    workspace[workspaceIndex + JpegConstants.DCTSIZE * 6] = dcval;
                    workspace[workspaceIndex + JpegConstants.DCTSIZE * 7] = dcval;

                    /* advance pointers to next column */
                    coefBlockIndex++;
                    quantTableIndex++;
                    workspaceIndex++;
                    continue;
                }

                /* Even part: reverse the even part of the forward DCT. */
                /* The rotator is c(-6). */

                int z2 = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 0],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 0]);
                int z3 = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 4],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 4]);

                z2 <<= SLOW_INTEGER_CONST_BITS;
                z3 <<= SLOW_INTEGER_CONST_BITS;
                /* Add fudge factor here for final descale. */
                z2 += 1 << (SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS - 1);

                int tmp0 = z2 + z3;
                int tmp1 = z2 - z3;

                z2 = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 2],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 2]);
                z3 = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 6],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 6]);

                int z1 = (z2 + z3) * SLOW_INTEGER_FIX_0_541196100;
                int tmp2 = z1 + z2 * SLOW_INTEGER_FIX_0_765366865;
                int tmp3 = z1 - z3 * SLOW_INTEGER_FIX_1_847759065;

                int tmp10 = tmp0 + tmp2;
                int tmp13 = tmp0 - tmp2;
                int tmp11 = tmp1 + tmp3;
                int tmp12 = tmp1 - tmp3;

                /* Odd part per figure 8; the matrix is unitary and hence its
                * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
                */

                tmp0 = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 7],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 7]);
                tmp1 = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 5],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 5]);
                tmp2 = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 3],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 3]);
                tmp3 = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 1],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 1]);

                z2 = tmp0 + tmp2;
                z3 = tmp1 + tmp3;

                z1 = (z2 + z3) * SLOW_INTEGER_FIX_1_175875602;       /*  c3 */
                z2 = z2 * (-SLOW_INTEGER_FIX_1_961570560);          /* -c3-c5 */
                z3 = z3 * (-SLOW_INTEGER_FIX_0_390180644);          /* -c3+c5 */
                z2 += z1;
                z3 += z1;

                z1 = (tmp0 + tmp3) * (-SLOW_INTEGER_FIX_0_899976223); /* -c3+c7 */
                tmp0 = tmp0 * SLOW_INTEGER_FIX_0_298631336;        /* -c1+c3+c5-c7 */
                tmp3 = tmp3 * SLOW_INTEGER_FIX_1_501321110;        /*  c1+c3-c5-c7 */
                tmp0 += z1 + z2;
                tmp3 += z1 + z3;

                z1 = (tmp1 + tmp2) * (-SLOW_INTEGER_FIX_2_562915447); /* -c1-c3 */
                tmp1 = tmp1 * SLOW_INTEGER_FIX_2_053119869;        /*  c1+c3-c5+c7 */
                tmp2 = tmp2 * SLOW_INTEGER_FIX_3_072711026;        /*  c1+c3+c5-c7 */
                tmp1 += z1 + z3;
                tmp2 += z1 + z2;

                /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */

                workspace[workspaceIndex + JpegConstants.DCTSIZE * 0] = JpegUtils.RIGHT_SHIFT(tmp10 + tmp3, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 7] = JpegUtils.RIGHT_SHIFT(tmp10 - tmp3, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 1] = JpegUtils.RIGHT_SHIFT(tmp11 + tmp2, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 6] = JpegUtils.RIGHT_SHIFT(tmp11 - tmp2, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 2] = JpegUtils.RIGHT_SHIFT(tmp12 + tmp1, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 5] = JpegUtils.RIGHT_SHIFT(tmp12 - tmp1, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 3] = JpegUtils.RIGHT_SHIFT(tmp13 + tmp0, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 4] = JpegUtils.RIGHT_SHIFT(tmp13 - tmp0, SLOW_INTEGER_CONST_BITS - SLOW_INTEGER_PASS1_BITS);

                /* advance pointers to next column */
                coefBlockIndex++;
                quantTableIndex++;
                workspaceIndex++;
            }

            /* Pass 2: process rows from work array, store into output array. */
            /* Note that we must descale the results by a factor of 8 == 2**3, */
            /* and also undo the SLOW_INTEGER_PASS1_BITS scaling. */

            workspaceIndex = 0;
            byte[] limit = m_cinfo.m_sample_range_limit;
            int limitOffset = m_cinfo.m_sampleRangeLimitOffset - RANGE_SUBSET;

            for (int ctr = 0; ctr < JpegConstants.DCTSIZE; ctr++)
            {
                /* Add range center and fudge factor for final descale and range-limit. */
                int z2 = workspace[workspaceIndex + 0] +
                    (RANGE_CENTER << (SLOW_INTEGER_PASS1_BITS + 3)) +
                    (1 << (SLOW_INTEGER_PASS1_BITS + 2));

                /* Rows of zeroes can be exploited in the same way as we did with columns.
                * However, the column calculation has created many nonzero AC terms, so
                * the simplification applies less often (typically 5% to 10% of the time).
                * On machines with very fast multiplication, it's possible that the
                * test takes more time than it's worth.  In that case this section
                * may be commented out.
                */
                int currentOutRow = output_row + ctr;
                if (workspace[workspaceIndex + 1] == 0 &&
                    workspace[workspaceIndex + 2] == 0 &&
                    workspace[workspaceIndex + 3] == 0 &&
                    workspace[workspaceIndex + 4] == 0 &&
                    workspace[workspaceIndex + 5] == 0 &&
                    workspace[workspaceIndex + 6] == 0 &&
                    workspace[workspaceIndex + 7] == 0)
                {
                    /* AC terms all zero */
                    byte dcval = limit[limitOffset + JpegUtils.RIGHT_SHIFT(z2, SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];

                    m_componentBuffer[currentOutRow][output_col + 0] = dcval;
                    m_componentBuffer[currentOutRow][output_col + 1] = dcval;
                    m_componentBuffer[currentOutRow][output_col + 2] = dcval;
                    m_componentBuffer[currentOutRow][output_col + 3] = dcval;
                    m_componentBuffer[currentOutRow][output_col + 4] = dcval;
                    m_componentBuffer[currentOutRow][output_col + 5] = dcval;
                    m_componentBuffer[currentOutRow][output_col + 6] = dcval;
                    m_componentBuffer[currentOutRow][output_col + 7] = dcval;

                    workspaceIndex += JpegConstants.DCTSIZE;       /* advance pointer to next row */
                    continue;
                }

                /* Even part: reverse the even part of the forward DCT. */
                /* The rotator is c(-6). */

                int z3 = workspace[workspaceIndex + 4];

                int tmp0 = (z2 + z3) << SLOW_INTEGER_CONST_BITS;
                int tmp1 = (z2 - z3) << SLOW_INTEGER_CONST_BITS;

                z2 = workspace[workspaceIndex + 2];
                z3 = workspace[workspaceIndex + 6];

                int z1 = (z2 + z3) * SLOW_INTEGER_FIX_0_541196100; /* c6 */
                int tmp2 = z1 + z2 * SLOW_INTEGER_FIX_0_765366865; /* c2-c6 */
                int tmp3 = z1 - z3 * SLOW_INTEGER_FIX_1_847759065; /* c2+c6 */

                int tmp10 = tmp0 + tmp2;
                int tmp13 = tmp0 - tmp2;
                int tmp11 = tmp1 + tmp3;
                int tmp12 = tmp1 - tmp3;

                /* Odd part per figure 8; the matrix is unitary and hence its
                * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
                */

                tmp0 = workspace[workspaceIndex + 7];
                tmp1 = workspace[workspaceIndex + 5];
                tmp2 = workspace[workspaceIndex + 3];
                tmp3 = workspace[workspaceIndex + 1];

                z2 = tmp0 + tmp2;
                z3 = tmp1 + tmp3;

                z1 = (z2 + z3) * SLOW_INTEGER_FIX_1_175875602;       /*  c3 */
                z2 = z2 * (-SLOW_INTEGER_FIX_1_961570560);          /* -c3-c5 */
                z3 = z3 * (-SLOW_INTEGER_FIX_0_390180644);          /* -c3+c5 */
                z2 += z1;
                z3 += z1;

                z1 = (tmp0 + tmp3) * (-SLOW_INTEGER_FIX_0_899976223); /* -c3+c7 */
                tmp0 = tmp0 * SLOW_INTEGER_FIX_0_298631336;        /* -c1+c3+c5-c7 */
                tmp3 = tmp3 * SLOW_INTEGER_FIX_1_501321110;        /*  c1+c3-c5-c7 */
                tmp0 += z1 + z2;
                tmp3 += z1 + z3;

                z1 = (tmp1 + tmp2) * (-SLOW_INTEGER_FIX_2_562915447); /* -c1-c3 */
                tmp1 = tmp1 * SLOW_INTEGER_FIX_2_053119869;        /*  c1+c3-c5+c7 */
                tmp2 = tmp2 * SLOW_INTEGER_FIX_3_072711026;        /*  c1+c3+c5-c7 */
                tmp1 += z1 + z3;
                tmp2 += z1 + z2;

                /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */

                m_componentBuffer[currentOutRow][output_col + 0] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(tmp10 + tmp3, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 7] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(tmp10 - tmp3, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 1] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(tmp11 + tmp2, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 6] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(tmp11 - tmp2, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 2] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(tmp12 + tmp1, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 5] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(tmp12 - tmp1, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 3] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(tmp13 + tmp0, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 4] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(tmp13 - tmp0, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];

                /* advance pointer to next row */
                workspaceIndex += JpegConstants.DCTSIZE;
            }
        }