/// <summary>
/// Analyzes image colors and creates color map.
/// </summary>
private void AnalyzePixels()
{
int len = _pixels.Length;
int nPix = len / 3;
_indexedPixels = new byte[nPix];
//Initialize quantizer.
var nq = new NeuQuant(_pixels, len, _sample);
//Create reduced palette.
_colorTab = nq.Process();
#region BGR to RGB
for (int i = 0; i < _colorTab.Length; i += 3)
{
//Only swap Red with Blue. Green stays.
byte temp = _colorTab[i];
_colorTab[i] = _colorTab[i + 2];
_colorTab[i + 2] = temp;
_usedEntry[i / 3] = false;
}
#endregion
//Map image pixels to new palette.
int k = 0;
_usedEntry = new bool[256];
for (int i = 0; i < nPix; i++)
{
int index = nq.Map(
_pixels[k++],
_pixels[k++],
_pixels[k++]);
_usedEntry[index] = true;
_indexedPixels[i] = (byte)index;
}
_colorDepth = 8;
_palSize = 7;
//Get closest match to transparent color if specified.
if (_transparent != Color.Empty)
{
_transIndex = nq.Map(_transparent.B, _transparent.G, _transparent.R);
}
_pixels = null;
}