private void create_colorindex()
{
/* For ordered dither, we pad the color index tables by MAXJSAMPLE in
* each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE).
* This is not necessary in the other dithering modes. However, we
* flag whether it was done in case user changes dithering mode.
*/
int pad;
if (m_cinfo.m_dither_mode == J_DITHER_MODE.JDITHER_ORDERED)
{
pad = JpegConstants.MAXJSAMPLE * 2;
m_is_padded = true;
}
else
{
pad = 0;
m_is_padded = false;
}
m_colorindex = jpeg_common_struct.AllocJpegSamples(JpegConstants.MAXJSAMPLE + 1 + pad, m_cinfo.m_out_color_components);
m_colorindexOffset = new int[m_cinfo.m_out_color_components];
/* blksize is number of adjacent repeated entries for a component */
int blksize = m_sv_actual;
for (int i = 0; i < m_cinfo.m_out_color_components; i++)
{
/* fill in colorindex entries for i'th color component */
int nci = m_Ncolors[i]; /* # of distinct values for this color */
blksize = blksize / nci;
/* adjust colorindex pointers to provide padding at negative indexes. */
if (pad != 0)
m_colorindexOffset[i] += JpegConstants.MAXJSAMPLE;
/* in loop, val = index of current output value, */
/* and k = largest j that maps to current val */
int val = 0;
int k = largest_input_value(0, nci - 1);
for (int j = 0; j <= JpegConstants.MAXJSAMPLE; j++)
{
while (j > k)
{
/* advance val if past boundary */
k = largest_input_value(++val, nci - 1);
}
/* premultiply so that no multiplication needed in main processing */
m_colorindex[i][m_colorindexOffset[i] + j] = (byte)(val * blksize);
}
/* Pad at both ends if necessary */
if (pad != 0)
{
for (int j = 1; j <= JpegConstants.MAXJSAMPLE; j++)
{
m_colorindex[i][m_colorindexOffset[i] + -j] = m_colorindex[i][m_colorindexOffset[i]];
m_colorindex[i][m_colorindexOffset[i] + JpegConstants.MAXJSAMPLE + j] = m_colorindex[i][m_colorindexOffset[i] + JpegConstants.MAXJSAMPLE];
}
}
}
}