/** * 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 // map image pixels to new palette int k = 0; 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); } }
/** * 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; for (int i = 0; i < usedEntry.Length; i++) { usedEntry[i] = false; } 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 = nq.Map(transparent.B, transparent.G, transparent.R); //FindClosest(transparent); } }
/** * 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; 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); } }
private void CreatePalette(byte[] pixels, out byte[] pixelindexes, out byte[] colorpalette, out bool[] usedpalette, out int alphaindex) { if (this.HQMode) { int len = pixels.Length; int nPix = len / 3; NeuQuant nq = new NeuQuant(pixels, len, this.PaletteSample); pixelindexes = new byte[nPix]; usedpalette = new bool[256]; colorpalette = nq.Process(); // thy shal cast blackmagic upon us. // map image pixels to new palette int k = 0; for (int i = 0; i < nPix; i++) { int index = nq.Map(pixels[k++] & 0xff, pixels[k++] & 0xff, pixels[k++] & 0xff); usedpalette[index] = true; pixelindexes[i] = (byte)index; } } else { GifEncoderLQ lq = new GifEncoderLQ(); colorpalette = lq.GetColorBytes(); pixelindexes = new byte[pixels.Length / 3]; usedpalette = null; alphaindex = 0; // map image pixels to new palette for (int i = 0; i < pixelindexes.Length; i++) { long leastDistance = long.MaxValue; int result = 0; int r = pixels[i * 3 + 2]; int g = pixels[i * 3 + 1]; int b = pixels[i * 3 + 0]; for (int index = 0; index < 256; index++) { int ra = r - colorpalette[index * 3 + 0]; int rg = g - colorpalette[index * 3 + 1]; int rb = b - colorpalette[index * 3 + 2]; long distance = ra * ra + rg * rg + rb * rb; // if a difference is zero, we're good because it won't get better if (distance == 0) { result = index; break; } // if a difference is the best so far, stores it as our best candidate if (distance < leastDistance) { leastDistance = distance; result = index; } } pixelindexes[i] = (byte)result; } } alphaindex = this.AlphaColor != Color.Empty ? this.FindClosest(this.AlphaColor, colorpalette) : 0; }