/// <summary>
/// Analyzes image colors and creates color map.
/// </summary>
private void AnalyzePixels()
{
int len = _pixels.Length;
int nPix = len / 3;
_indexedPixels = new byte[nPix];
var nq = new NeuQuant(_pixels, len, _sample);
//Initialize quantizer.
_colorTab = nq.Process(); //Create reduced palette
#region Not used
// convert map from BGR to RGB
//for (int i = 0; i < colorTab.Length; i += 3)
//{
// 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]; //Here is the fix from the internet, codeproject.
for (int i = 0; i < nPix; i++)
{
int index = nq.Map(_pixels[k++] & 0xff,
_pixels[k++] & 0xff,
_pixels[k++] & 0xff);
_usedEntry[index] = true;
_indexedPixels[i] = (byte)index;
}
_pixels = null;
_colorDepth = 8;
_palSize = 7;
//Get closest match to transparent color if specified.
if (_transparent != Color.Empty)
{
//_transIndex = FindClosest(_transparent);
_transIndex = nq.Map(_transparent.B, _transparent.G, _transparent.R);
}
}