/**
  * Analyzes image colors and creates color map.
  */
 protected void AnalyzePixels()
 {
     int len = pixels.Length;
     int nPix = len / 3;
     indexedPixels = new byte[nPix];
     NeuQuant nq = new NeuQuant(pixels, len, sample);
     // initialize quantizer
     colorTab = nq.Process(); // create reduced palette
     // 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;
     //}
     // 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);
     }
 }