private void initial_setup()
{
/* Make sure image isn't bigger than I can handle */
if (m_cinfo.m_image_height > JpegConstants.JPEG_MAX_DIMENSION ||
m_cinfo.m_image_width > JpegConstants.JPEG_MAX_DIMENSION)
{
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_IMAGE_TOO_BIG, (int)JpegConstants.JPEG_MAX_DIMENSION);
}
/* Only 8 to 12 bits data precision are supported for DCT based JPEG */
if (m_cinfo.m_data_precision < 8 || m_cinfo.m_data_precision > 12)
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_BAD_PRECISION, m_cinfo.m_data_precision);
/* Check that number of components won't exceed internal array sizes */
if (m_cinfo.m_num_components > JpegConstants.MAX_COMPONENTS)
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_COMPONENT_COUNT, m_cinfo.m_num_components, JpegConstants.MAX_COMPONENTS);
/* Compute maximum sampling factors; check factor validity */
m_cinfo.m_max_h_samp_factor = 1;
m_cinfo.m_max_v_samp_factor = 1;
for (int ci = 0; ci < m_cinfo.m_num_components; ci++)
{
if (m_cinfo.Comp_info[ci].H_samp_factor <= 0 || m_cinfo.Comp_info[ci].H_samp_factor > JpegConstants.MAX_SAMP_FACTOR ||
m_cinfo.Comp_info[ci].V_samp_factor <= 0 || m_cinfo.Comp_info[ci].V_samp_factor > JpegConstants.MAX_SAMP_FACTOR)
{
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_BAD_SAMPLING);
}
m_cinfo.m_max_h_samp_factor = Math.Max(m_cinfo.m_max_h_samp_factor, m_cinfo.Comp_info[ci].H_samp_factor);
m_cinfo.m_max_v_samp_factor = Math.Max(m_cinfo.m_max_v_samp_factor, m_cinfo.Comp_info[ci].V_samp_factor);
}
/* Derive block_size, natural_order, and lim_Se */
if (m_cinfo.is_baseline || (m_cinfo.m_progressive_mode && m_cinfo.m_comps_in_scan != 0))
{
/* no pseudo SOS marker */
m_cinfo.block_size = JpegConstants.DCTSIZE;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
}
else
{
switch (m_cinfo.m_Se)
{
case (1 * 1 - 1):
m_cinfo.block_size = 1;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order; /* not needed */
m_cinfo.lim_Se = m_cinfo.m_Se;
break;
case (2 * 2 - 1):
m_cinfo.block_size = 2;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order2;
m_cinfo.lim_Se = m_cinfo.m_Se;
break;
case (3 * 3 - 1):
m_cinfo.block_size = 3;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order3;
m_cinfo.lim_Se = m_cinfo.m_Se;
break;
case (4 * 4 - 1):
m_cinfo.block_size = 4;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order4;
m_cinfo.lim_Se = m_cinfo.m_Se;
break;
case (5 * 5 - 1):
m_cinfo.block_size = 5;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order5;
m_cinfo.lim_Se = m_cinfo.m_Se;
break;
case (6 * 6 - 1):
m_cinfo.block_size = 6;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order6;
m_cinfo.lim_Se = m_cinfo.m_Se;
break;
case (7 * 7 - 1):
m_cinfo.block_size = 7;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order7;
m_cinfo.lim_Se = m_cinfo.m_Se;
break;
case (8 * 8 - 1):
m_cinfo.block_size = 8;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
break;
case (9 * 9 - 1):
m_cinfo.block_size = 9;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
break;
case (10 * 10 - 1):
m_cinfo.block_size = 10;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
break;
case (11 * 11 - 1):
m_cinfo.block_size = 11;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
break;
case (12 * 12 - 1):
m_cinfo.block_size = 12;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
break;
case (13 * 13 - 1):
m_cinfo.block_size = 13;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
break;
case (14 * 14 - 1):
m_cinfo.block_size = 14;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
break;
case (15 * 15 - 1):
m_cinfo.block_size = 15;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
break;
case (16 * 16 - 1):
m_cinfo.block_size = 16;
m_cinfo.natural_order = JpegUtils.jpeg_natural_order;
m_cinfo.lim_Se = JpegConstants.DCTSIZE2 - 1;
break;
default:
m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_BAD_PROGRESSION,
m_cinfo.m_Ss, m_cinfo.m_Se, m_cinfo.m_Ah, m_cinfo.m_Al);
break;
}
}
/* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size.
* In the full decompressor,
* this will be overridden by jpeg_calc_output_dimensions in jdmaster.c;
* but in the transcoder,
* jpeg_calc_output_dimensions is not used, so we must do it here.
*/
m_cinfo.min_DCT_h_scaled_size = m_cinfo.block_size;
m_cinfo.min_DCT_v_scaled_size = m_cinfo.block_size;
/* Compute dimensions of components */
for (int ci = 0; ci < m_cinfo.m_num_components; ci++)
{
jpeg_component_info compptr = m_cinfo.Comp_info[ci];
compptr.DCT_h_scaled_size = m_cinfo.block_size;
compptr.DCT_v_scaled_size = m_cinfo.block_size;
/* Size in DCT blocks */
compptr.Width_in_blocks = (int)JpegUtils.jdiv_round_up(
m_cinfo.m_image_width * compptr.H_samp_factor,
m_cinfo.m_max_h_samp_factor * m_cinfo.block_size);
compptr.height_in_blocks = (int)JpegUtils.jdiv_round_up(
m_cinfo.m_image_height * compptr.V_samp_factor,
m_cinfo.m_max_v_samp_factor * m_cinfo.block_size);
/* downsampled_width and downsampled_height will also be overridden by
* jpeg_decomp_master if we are doing full decompression. The transcoder library
* doesn't use these values, but the calling application might.
*/
/* Size in samples */
compptr.downsampled_width = (int)JpegUtils.jdiv_round_up(
m_cinfo.m_image_width * compptr.H_samp_factor,
m_cinfo.m_max_h_samp_factor);
compptr.downsampled_height = (int)JpegUtils.jdiv_round_up(
m_cinfo.m_image_height * compptr.V_samp_factor,
m_cinfo.m_max_v_samp_factor);
/* Mark component needed, until color conversion says otherwise */
compptr.component_needed = true;
/* Mark no quantization table yet saved for component */
compptr.quant_table = null;
}
/* Compute number of fully interleaved MCU rows. */
m_cinfo.m_total_iMCU_rows = (int)JpegUtils.jdiv_round_up(
m_cinfo.m_image_height, m_cinfo.m_max_v_samp_factor * m_cinfo.block_size);
/* Decide whether file contains multiple scans */
if (m_cinfo.m_comps_in_scan < m_cinfo.m_num_components || m_cinfo.m_progressive_mode)
m_cinfo.m_inputctl.m_has_multiple_scans = true;
else
m_cinfo.m_inputctl.m_has_multiple_scans = false;
}