private bool compressDataImpl(byte[][][] input_buf)
{
int last_MCU_col = m_cinfo.m_MCUs_per_row - 1;
int last_iMCU_row = m_cinfo.m_total_iMCU_rows - 1;
/* Loop to write as much as one whole iMCU row */
for (int yoffset = m_MCU_vert_offset; yoffset < m_MCU_rows_per_iMCU_row; yoffset++)
{
for (int MCU_col_num = m_mcu_ctr; MCU_col_num <= last_MCU_col; MCU_col_num++)
{
/* Determine where data comes from in input_buf and do the DCT thing.
* Each call on forward_DCT processes a horizontal row of DCT blocks
* as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
* sequentially. Dummy blocks at the right or bottom edge are filled in
* specially. The data in them does not matter for image reconstruction,
* so we fill them with values that will encode to the smallest amount of
* data, viz: all zeroes in the AC entries, DC entries equal to previous
* block's DC value. (Thanks to Thomas Kinsman for this idea.)
*/
int blkn = 0;
for (int ci = 0; ci < m_cinfo.m_comps_in_scan; ci++)
{
jpeg_component_info componentInfo = m_cinfo.Component_info[m_cinfo.m_cur_comp_info[ci]];
jpeg_forward_dct.forward_DCT_ptr forward_DCT = m_cinfo.m_fdct.forward_DCT[componentInfo.Component_index];
int blockcnt = (MCU_col_num < last_MCU_col) ? componentInfo.MCU_width : componentInfo.last_col_width;
int xpos = MCU_col_num * componentInfo.MCU_sample_width;
int ypos = yoffset * componentInfo.DCT_v_scaled_size;
for (int yindex = 0; yindex < componentInfo.MCU_height; yindex++)
{
if (m_iMCU_row_num < last_iMCU_row || yoffset + yindex < componentInfo.last_row_height)
{
forward_DCT(componentInfo, input_buf[componentInfo.Component_index],
m_MCU_buffer[blkn], ypos, xpos, blockcnt);
if (blockcnt < componentInfo.MCU_width)
{
/* Create some dummy blocks at the right edge of the image. */
for (int i = 0; i < (componentInfo.MCU_width - blockcnt); i++)
Array.Clear(m_MCU_buffer[blkn + blockcnt][i].data, 0, m_MCU_buffer[blkn + blockcnt][i].data.Length);
for (int bi = blockcnt; bi < componentInfo.MCU_width; bi++)
m_MCU_buffer[blkn + bi][0][0] = m_MCU_buffer[blkn + bi - 1][0][0];
}
}
else
{
/* Create a row of dummy blocks at the bottom of the image. */
for (int i = 0; i < componentInfo.MCU_width; i++)
Array.Clear(m_MCU_buffer[blkn][i].data, 0, m_MCU_buffer[blkn][i].data.Length);
for (int bi = 0; bi < componentInfo.MCU_width; bi++)
m_MCU_buffer[blkn + bi][0][0] = m_MCU_buffer[blkn - 1][0][0];
}
blkn += componentInfo.MCU_width;
ypos += componentInfo.DCT_v_scaled_size;
}
}
/* Try to write the MCU. In event of a suspension failure, we will
* re-DCT the MCU on restart (a bit inefficient, could be fixed...)
*/
if (!m_cinfo.m_entropy.encode_mcu(m_MCU_buffer))
{
/* Suspension forced; update state counters and exit */
m_MCU_vert_offset = yoffset;
m_mcu_ctr = MCU_col_num;
return false;
}
}
/* Completed an MCU row, but perhaps not an iMCU row */
m_mcu_ctr = 0;
}
/* Completed the iMCU row, advance counters for next one */
m_iMCU_row_num++;
start_iMCU_row();
return true;
}