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

jpeg_idct_16x8() private method

private jpeg_idct_16x8 ( 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_16x8(int component_index, short[] coef_block, int output_row, int output_col)
        {
            /* buffers data between passes */
            int[] workspace = new int[8 * 8];

            /* 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. */
            /* 8 - point IDCT kernel, cK represents sqrt(2) * cos(K * pi / 16). */

            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 * 2],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 2]);
                int 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;

                z2 = SLOW_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 0],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 0]);
                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;

                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 8 rows from work array, store into output array.
             * 16-point IDCT kernel, cK represents sqrt(2) * cos(K*pi/32).
             */
            workspaceIndex = 0;
            byte[] limit = m_cinfo.m_sample_range_limit;
            int limitOffset = m_cinfo.m_sampleRangeLimitOffset - RANGE_SUBSET;

            for (int ctr = 0; ctr < 8; ctr++)
            {
                /* Even part */

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

                int z1 = workspace[workspaceIndex + 4];
                int tmp1 = z1 * SLOW_INTEGER_FIX(1.306562965);      /* c4[16] = c2[8] */
                int tmp2 = z1 * SLOW_INTEGER_FIX_0_541196100;       /* c12[16] = c6[8] */

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

                z1 = workspace[workspaceIndex + 2];
                int z2 = workspace[workspaceIndex + 6];
                int z3 = z1 - z2;
                int z4 = z3 * SLOW_INTEGER_FIX(0.275899379);        /* c14[16] = c7[8] */
                z3 = z3 * SLOW_INTEGER_FIX(1.387039845);        /* c2[16] = c1[8] */

                tmp0 = z3 + z2 * SLOW_INTEGER_FIX_2_562915447;  /* (c6+c2)[16] = (c3+c1)[8] */
                tmp1 = z4 + z1 * SLOW_INTEGER_FIX_0_899976223;  /* (c6-c14)[16] = (c3-c7)[8] */
                tmp2 = z3 - z1 * SLOW_INTEGER_FIX(0.601344887); /* (c2-c10)[16] = (c1-c5)[8] */
                int tmp3 = z4 - z2 * SLOW_INTEGER_FIX(0.509795579); /* (c10-c14)[16] = (c5-c7)[8] */

                int tmp20 = tmp10 + tmp0;
                int tmp27 = tmp10 - tmp0;
                int tmp21 = tmp12 + tmp1;
                int tmp26 = tmp12 - tmp1;
                int tmp22 = tmp13 + tmp2;
                int tmp25 = tmp13 - tmp2;
                int tmp23 = tmp11 + tmp3;
                int tmp24 = tmp11 - tmp3;

                /* Odd part */

                z1 = workspace[workspaceIndex + 1];
                z2 = workspace[workspaceIndex + 3];
                z3 = workspace[workspaceIndex + 5];
                z4 = workspace[workspaceIndex + 7];

                tmp11 = z1 + z3;

                tmp1 = (z1 + z2) * SLOW_INTEGER_FIX(1.353318001);   /* c3 */
                tmp2 = tmp11 * SLOW_INTEGER_FIX(1.247225013);   /* c5 */
                tmp3 = (z1 + z4) * SLOW_INTEGER_FIX(1.093201867);   /* c7 */
                tmp10 = (z1 - z4) * SLOW_INTEGER_FIX(0.897167586);   /* c9 */
                tmp11 = tmp11 * SLOW_INTEGER_FIX(0.666655658);   /* c11 */
                tmp12 = (z1 - z2) * SLOW_INTEGER_FIX(0.410524528);   /* c13 */
                tmp0 = tmp1 + tmp2 + tmp3 - z1 * SLOW_INTEGER_FIX(2.286341144);        /* c7+c5+c3-c1 */
                tmp13 = tmp10 + tmp11 + tmp12 - z1 * SLOW_INTEGER_FIX(1.835730603);        /* c9+c11+c13-c15 */
                z1 = (z2 + z3) * SLOW_INTEGER_FIX(0.138617169);   /* c15 */
                tmp1 += z1 + z2 * SLOW_INTEGER_FIX(0.071888074);  /* c9+c11-c3-c15 */
                tmp2 += z1 - z3 * SLOW_INTEGER_FIX(1.125726048);  /* c5+c7+c15-c3 */
                z1 = (z3 - z2) * SLOW_INTEGER_FIX(1.407403738);   /* c1 */
                tmp11 += z1 - z3 * SLOW_INTEGER_FIX(0.766367282);  /* c1+c11-c9-c13 */
                tmp12 += z1 + z2 * SLOW_INTEGER_FIX(1.971951411);  /* c1+c5+c13-c7 */
                z2 += z4;
                z1 = z2 * (-SLOW_INTEGER_FIX(0.666655658));      /* -c11 */
                tmp1 += z1;
                tmp3 += z1 + z4 * SLOW_INTEGER_FIX(1.065388962);  /* c3+c11+c15-c7 */
                z2 = z2 * (-SLOW_INTEGER_FIX(1.247225013));      /* -c5 */
                tmp10 += z2 + z4 * SLOW_INTEGER_FIX(3.141271809);  /* c1+c5+c9-c13 */
                tmp12 += z2;
                z2 = (z3 + z4) * (-SLOW_INTEGER_FIX(1.353318001)); /* -c3 */
                tmp2 += z2;
                tmp3 += z2;
                z2 = (z4 - z3) * SLOW_INTEGER_FIX(0.410524528);   /* c13 */
                tmp10 += z2;
                tmp11 += z2;

                /* Final output stage */
                int currentOutRow = output_row + ctr;
                m_componentBuffer[currentOutRow][output_col + 0] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                        tmp20 + tmp0, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 15] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp20 - tmp0, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 1] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp21 + tmp1, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 14] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp21 - tmp1, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 2] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp22 + tmp2, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 13] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp22 - tmp2, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 3] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp23 + tmp3, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 12] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp23 - tmp3, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 4] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp24 + tmp10, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 11] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp24 - tmp10, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 5] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp25 + tmp11, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 10] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp25 - tmp11, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 6] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp26 + tmp12, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 9] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp26 - tmp12, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 7] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp27 + tmp13, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 8] = limit[limitOffset + JpegUtils.RIGHT_SHIFT(
                    tmp27 - tmp13, SLOW_INTEGER_CONST_BITS + SLOW_INTEGER_PASS1_BITS + 3) & RANGE_MASK];

                workspaceIndex += 8;		/* advance pointer to next row */
            }
        }