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

jpeg_idct_float() private method

Perform dequantization and inverse DCT on one block of coefficients. NOTE: this code only copes with 8x8 DCTs. A floating-point implementation of the inverse DCT (Discrete Cosine Transform). In the IJG code, this routine must also perform dequantization of the input coefficients. This implementation should be more accurate than either of the integer IDCT implementations. However, it may not give the same results on all machines because of differences in roundoff behavior. Speed will depend on the hardware's floating point capacity. 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 Arai, Agui, and Nakajima's algorithm for scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in Japanese, but the algorithm is described in the Pennebaker & Mitchell JPEG textbook (see REFERENCES section in file README). The following code is based directly on figure 4-8 in P&M. While an 8-point DCT cannot be done in less than 11 multiplies, it is possible to arrange the computation so that many of the multiplies are simple scalings of the final outputs. These multiplies can then be folded into the multiplications or divisions by the JPEG quantization table entries. The AA&N method leaves only 5 multiplies and 29 adds to be done in the DCT itself. The primary disadvantage of this method is that with a fixed-point implementation, accuracy is lost due to imprecise representation of the scaled quantization values. However, that problem does not arise if we use floating point arithmetic.
private jpeg_idct_float ( 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_float(int component_index, short[] coef_block, int output_row, int output_col)
        {
            /* buffers data between passes */
            float[] workspace = new float[JpegConstants.DCTSIZE2];

            /* Pass 1: process columns from input, store into work array. */

            int coefBlockIndex = 0;
            int workspaceIndex = 0;

            float[] quantTable = m_dctTables[component_index].float_array;
            int quantTableIndex = 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 */
                    float dcval = FLOAT_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 0],
                        quantTable[quantTableIndex + JpegConstants.DCTSIZE * 0]);

                    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;

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

                /* Even part */

                float tmp0 = FLOAT_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 0],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 0]);
                float tmp1 = FLOAT_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 2],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 2]);
                float tmp2 = FLOAT_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 4],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 4]);
                float tmp3 = FLOAT_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 6],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 6]);

                float tmp10 = tmp0 + tmp2;    /* phase 3 */
                float tmp11 = tmp0 - tmp2;

                float tmp13 = tmp1 + tmp3;    /* phases 5-3 */
                float tmp12 = (tmp1 - tmp3) * 1.414213562f - tmp13; /* 2*c4 */

                tmp0 = tmp10 + tmp13;   /* phase 2 */
                tmp3 = tmp10 - tmp13;
                tmp1 = tmp11 + tmp12;
                tmp2 = tmp11 - tmp12;

                /* Odd part */

                float tmp4 = FLOAT_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 1],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 1]);
                float tmp5 = FLOAT_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 3],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 3]);
                float tmp6 = FLOAT_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 5],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 5]);
                float tmp7 = FLOAT_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 7],
                    quantTable[quantTableIndex + JpegConstants.DCTSIZE * 7]);

                float z13 = tmp6 + tmp5;      /* phase 6 */
                float z10 = tmp6 - tmp5;
                float z11 = tmp4 + tmp7;
                float z12 = tmp4 - tmp7;

                tmp7 = z11 + z13;       /* phase 5 */
                tmp11 = (z11 - z13) * 1.414213562f; /* 2*c4 */

                float z5 = (z10 + z12) * 1.847759065f; /* 2*c2 */
                tmp10 = z5 - z12 * 1.082392200f; /* 2*(c2-c6) */
                tmp12 = z5 - z10 * 2.613125930f; /* 2*(c2+c6) */

                tmp6 = tmp12 - tmp7;    /* phase 2 */
                tmp5 = tmp11 - tmp6;
                tmp4 = tmp10 - tmp5;

                workspace[workspaceIndex + JpegConstants.DCTSIZE * 0] = tmp0 + tmp7;
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 7] = tmp0 - tmp7;
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 1] = tmp1 + tmp6;
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 6] = tmp1 - tmp6;
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 2] = tmp2 + tmp5;
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 5] = tmp2 - tmp5;
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 3] = tmp3 + tmp4;
                workspace[workspaceIndex + JpegConstants.DCTSIZE * 4] = tmp3 - tmp4;

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

            /* Pass 2: process rows from work array, store into output array. */
            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++)
            {
                /* 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).
                * And testing floats for zero is relatively expensive, so we don't bother.
                */

                /* Even part */

                /* Prepare range-limit and float->int conversion */
                float z5 = workspace[workspaceIndex + 0] + (RANGE_CENTER + 0.5f);
                float tmp10 = z5 + workspace[workspaceIndex + 4];
                float tmp11 = z5 - workspace[workspaceIndex + 4];

                float tmp13 = workspace[workspaceIndex + 2] + workspace[workspaceIndex + 6];
                float tmp12 = (workspace[workspaceIndex + 2] - workspace[workspaceIndex + 6]) * 1.414213562f - tmp13; /* 2*c4 */

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

                /* Odd part */

                float z13 = workspace[workspaceIndex + 5] + workspace[workspaceIndex + 3];
                float z10 = workspace[workspaceIndex + 5] - workspace[workspaceIndex + 3];
                float z11 = workspace[workspaceIndex + 1] + workspace[workspaceIndex + 7];
                float z12 = workspace[workspaceIndex + 1] - workspace[workspaceIndex + 7];

                float tmp7 = z11 + z13;     /* phase 5 */
                tmp11 = (z11 - z13) * 1.414213562f; /* 2*c4 */

                z5 = (z10 + z12) * 1.847759065f; /* 2*c2 */
                tmp10 = z5 - z12 * 1.082392200f; /* 2*(c2-c6) */
                tmp12 = z5 - z10 * 2.613125930f; /* 2*(c2+c6) */

                float tmp6 = tmp12 - tmp7;      /* phase 2 */
                float tmp5 = tmp11 - tmp6;
                float tmp4 = tmp10 - tmp5;

                /* Final output stage: float->int conversion and range-limit */
                int currentOutRow = output_row + ctr;
                m_componentBuffer[currentOutRow][output_col + 0] = limit[limitOffset + (int)(tmp0 + tmp7) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 7] = limit[limitOffset + (int)(tmp0 - tmp7) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 1] = limit[limitOffset + (int)(tmp1 + tmp6) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 6] = limit[limitOffset + (int)(tmp1 - tmp6) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 2] = limit[limitOffset + (int)(tmp2 + tmp5) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 5] = limit[limitOffset + (int)(tmp2 - tmp5) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 3] = limit[limitOffset + (int)(tmp3 + tmp4) & RANGE_MASK];
                m_componentBuffer[currentOutRow][output_col + 4] = limit[limitOffset + (int)(tmp3 - tmp4) & RANGE_MASK];

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