/** * 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); } }
/** * Initiates writing of a GIF file with the specified name. * * @param file String containing output file name. * @return false if open or initial write failed. */ /*public bool Start(String file) * { * bool ok = true; * try * { * // bw = new BinaryWriter( new MemoryStream( file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None ) ); * fs = new MemoryStream( file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None ); * ok = Start(fs); * closeStream = true; * } * catch (IOException e) * { * ok = false; * } * return started = ok; * }*/ /** * 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); /*int index = * nq.Map(pixels[k+2] & 0xff, * pixels[k+1] & 0xff, * pixels[k] & 0xff); * k += 3;*/ 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 < 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; }