/// <summary> /// Gets an enumerable array of bytes representing each row of the image. /// </summary> /// <param name="image"> /// The <see cref="ImageBuffer"/> for storing and manipulating pixel information. /// </param> /// <param name="lookups"> /// The array of <see cref="Color32"/> containing indexed versions of the images colors. /// </param> /// <param name="alphaThreshold"> /// The alpha threshold. /// </param> /// <param name="paletteHistogram"> /// The palette histogram. /// </param> /// <returns> /// The enumerable list of <see cref="byte"/> representing each pixel. /// </returns> private static IEnumerable <byte[]> IndexedPixels(ImageBuffer image, Color32[] lookups, int alphaThreshold, PaletteColorHistory[] paletteHistogram) { byte[] lineIndexes = new byte[image.Image.Width]; PaletteLookup lookup = new PaletteLookup(lookups); // Determine the correct fallback color. byte fallback = lookups.Length < AlphaMax ? AlphaMin : AlphaMax; foreach (Color32[] pixelLine in image.PixelLines) { int length = pixelLine.Length; for (int i = 0; i < length; i++) { Color32 pixel = pixelLine[i]; byte bestMatch = fallback; if (pixel.A > alphaThreshold) { bestMatch = lookup.GetPaletteIndex(pixel); paletteHistogram[bestMatch].AddPixel(pixel); } lineIndexes[i] = bestMatch; } yield return(lineIndexes); } }
/// <summary> /// Gets an enumerable array of bytes representing each row of the image. /// </summary> /// <param name="image"> /// The <see cref="ImageBuffer"/> for storing and manipulating pixel information. /// </param> /// <param name="lookups"> /// The array of <see cref="Color32"/> containing indexed versions of the images colors. /// </param> /// <param name="alphaThreshold"> /// The alpha threshold. /// </param> /// <param name="paletteHistogram"> /// The palette histogram. /// </param> /// <returns> /// The enumerable list of <see cref="byte"/> representing each pixel. /// </returns> private static IEnumerable<byte[]> IndexedPixels(ImageBuffer image, Color32[] lookups, int alphaThreshold, PaletteColorHistory[] paletteHistogram) { byte[] lineIndexes = new byte[image.Image.Width]; PaletteLookup lookup = new PaletteLookup(lookups); // Determine the correct fallback color. byte fallback = lookups.Length < AlphaMax ? AlphaMin : AlphaMax; foreach (Color32[] pixelLine in image.PixelLines) { int length = pixelLine.Length; for (int i = 0; i < length; i++) { Color32 pixel = pixelLine[i]; byte bestMatch = fallback; if (pixel.A > alphaThreshold) { bestMatch = lookup.GetPaletteIndex(pixel); paletteHistogram[bestMatch].AddPixel(pixel); } lineIndexes[i] = bestMatch; } yield return lineIndexes; } }