private void jpeg_idct_ifast(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. */
int coefBlockIndex = 0;
int workspaceIndex = 0;
int[] quantTable = m_dctTables[component_index].int_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 */
int dcval = FAST_INTEGER_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;
/* advance pointers to next column */
coefBlockIndex++;
quantTableIndex++;
workspaceIndex++;
continue;
}
/* Even part */
int tmp0 = FAST_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 0],
quantTable[quantTableIndex + JpegConstants.DCTSIZE * 0]);
int tmp1 = FAST_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 2],
quantTable[quantTableIndex + JpegConstants.DCTSIZE * 2]);
int tmp2 = FAST_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 4],
quantTable[quantTableIndex + JpegConstants.DCTSIZE * 4]);
int tmp3 = FAST_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 6],
quantTable[quantTableIndex + JpegConstants.DCTSIZE * 6]);
int tmp10 = tmp0 + tmp2; /* phase 3 */
int tmp11 = tmp0 - tmp2;
int tmp13 = tmp1 + tmp3; /* phases 5-3 */
int tmp12 = FAST_INTEGER_MULTIPLY(tmp1 - tmp3, FAST_INTEGER_FIX_1_414213562) - tmp13; /* 2*c4 */
tmp0 = tmp10 + tmp13; /* phase 2 */
tmp3 = tmp10 - tmp13;
tmp1 = tmp11 + tmp12;
tmp2 = tmp11 - tmp12;
/* Odd part */
int tmp4 = FAST_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 1],
quantTable[quantTableIndex + JpegConstants.DCTSIZE * 1]);
int tmp5 = FAST_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 3],
quantTable[quantTableIndex + JpegConstants.DCTSIZE * 3]);
int tmp6 = FAST_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 5],
quantTable[quantTableIndex + JpegConstants.DCTSIZE * 5]);
int tmp7 = FAST_INTEGER_DEQUANTIZE(coef_block[coefBlockIndex + JpegConstants.DCTSIZE * 7],
quantTable[quantTableIndex + JpegConstants.DCTSIZE * 7]);
int z13 = tmp6 + tmp5; /* phase 6 */
int z10 = tmp6 - tmp5;
int z11 = tmp4 + tmp7;
int z12 = tmp4 - tmp7;
tmp7 = z11 + z13; /* phase 5 */
tmp11 = FAST_INTEGER_MULTIPLY(z11 - z13, FAST_INTEGER_FIX_1_414213562); /* 2*c4 */
int z5 = FAST_INTEGER_MULTIPLY(z10 + z12, FAST_INTEGER_FIX_1_847759065); /* 2*c2 */
tmp10 = z5 - FAST_INTEGER_MULTIPLY(z12, FAST_INTEGER_FIX_1_082392200); /* 2*(c2-c6) */
tmp12 = z5 - FAST_INTEGER_MULTIPLY(z10, FAST_INTEGER_FIX_2_613125930); /* 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;
/* 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 FAST_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++)
{
int currentOutRow = output_row + ctr;
/* Add range center and fudge factor for final descale and range-limit. */
int z5 = workspace[workspaceIndex + 0] +
((RANGE_CENTER << (FAST_INTEGER_PASS1_BITS + 3)) +
(1 << (FAST_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.
*/
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 + FAST_INTEGER_IRIGHT_SHIFT(z5, FAST_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;
/* advance pointer to next row */
workspaceIndex += JpegConstants.DCTSIZE;
continue;
}
/* Even part */
int tmp10 = z5 + workspace[workspaceIndex + 4];
int tmp11 = z5 - workspace[workspaceIndex + 4];
int tmp13 = workspace[workspaceIndex + 2] + workspace[workspaceIndex + 6];
int tmp12 = FAST_INTEGER_MULTIPLY(workspace[workspaceIndex + 2] - workspace[workspaceIndex + 6],
FAST_INTEGER_FIX_1_414213562) - tmp13; /* 2*c4 */
int tmp0 = tmp10 + tmp13;
int tmp3 = tmp10 - tmp13;
int tmp1 = tmp11 + tmp12;
int tmp2 = tmp11 - tmp12;
/* Odd part */
int z13 = workspace[workspaceIndex + 5] + workspace[workspaceIndex + 3];
int z10 = workspace[workspaceIndex + 5] - workspace[workspaceIndex + 3];
int z11 = workspace[workspaceIndex + 1] + workspace[workspaceIndex + 7];
int z12 = workspace[workspaceIndex + 1] - workspace[workspaceIndex + 7];
int tmp7 = z11 + z13; /* phase 5 */
tmp11 = FAST_INTEGER_MULTIPLY(z11 - z13, FAST_INTEGER_FIX_1_414213562); /* 2*c4 */
z5 = FAST_INTEGER_MULTIPLY(z10 + z12, FAST_INTEGER_FIX_1_847759065); /* 2*c2 */
tmp10 = z5 - FAST_INTEGER_MULTIPLY(z12, FAST_INTEGER_FIX_1_082392200); /* 2*(c2-c6) */
tmp12 = z5 - FAST_INTEGER_MULTIPLY(z10, FAST_INTEGER_FIX_2_613125930); /* 2*(c2+c6) */
int tmp6 = tmp12 - tmp7; /* phase 2 */
int tmp5 = tmp11 - tmp6;
int tmp4 = tmp10 - tmp5;
/* Final output stage: scale down by a factor of 8 and range-limit */
m_componentBuffer[currentOutRow][output_col + 0] = limit[limitOffset + FAST_INTEGER_IRIGHT_SHIFT(tmp0 + tmp7, FAST_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
m_componentBuffer[currentOutRow][output_col + 7] = limit[limitOffset + FAST_INTEGER_IRIGHT_SHIFT(tmp0 - tmp7, FAST_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
m_componentBuffer[currentOutRow][output_col + 1] = limit[limitOffset + FAST_INTEGER_IRIGHT_SHIFT(tmp1 + tmp6, FAST_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
m_componentBuffer[currentOutRow][output_col + 6] = limit[limitOffset + FAST_INTEGER_IRIGHT_SHIFT(tmp1 - tmp6, FAST_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
m_componentBuffer[currentOutRow][output_col + 2] = limit[limitOffset + FAST_INTEGER_IRIGHT_SHIFT(tmp2 + tmp5, FAST_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
m_componentBuffer[currentOutRow][output_col + 5] = limit[limitOffset + FAST_INTEGER_IRIGHT_SHIFT(tmp2 - tmp5, FAST_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
m_componentBuffer[currentOutRow][output_col + 3] = limit[limitOffset + FAST_INTEGER_IRIGHT_SHIFT(tmp3 + tmp4, FAST_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
m_componentBuffer[currentOutRow][output_col + 4] = limit[limitOffset + FAST_INTEGER_IRIGHT_SHIFT(tmp3 - tmp4, FAST_INTEGER_PASS1_BITS + 3) & RANGE_MASK];
/* advance pointer to next row */
workspaceIndex += JpegConstants.DCTSIZE;
}
}