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;
}
}