void ReadFrom(Stream s) { using (var br = new BinaryReader(s)) { // Hashes var hashCount = br.ReadInt32(); m_HashToFileMap = new Dictionary <Hash128, string>(); for (var i = 0; i < hashCount; ++i) { var hash = Hash128.Parse(br.ReadString()); var value = br.ReadString(); m_HashToFileMap[hash] = value; } // Images data var imageDataCount = br.ReadInt32(); imagesData = new List <ImageData>(imageDataCount); m_AssetPathToIndex = new Dictionary <string, int>(); for (var i = 0; i < imageDataCount; ++i) { var guid = Hash128.Parse(br.ReadString()); var imageData = new ImageData(guid); var nbColor = br.ReadInt32(); imageData.bestColors = new ColorInfo[nbColor]; for (var j = 0; j < nbColor; ++j) { var colorInfo = new ColorInfo(); colorInfo.color = br.ReadUInt32(); colorInfo.ratio = br.ReadDouble(); imageData.bestColors[j] = colorInfo; } var nbShades = br.ReadInt32(); imageData.bestShades = new ColorInfo[nbShades]; for (var j = 0; j < nbShades; ++j) { var colorInfo = new ColorInfo(); colorInfo.color = br.ReadUInt32(); colorInfo.ratio = br.ReadDouble(); imageData.bestShades[j] = colorInfo; } ReadHistogram(imageData.histogram.valuesR, br); ReadHistogram(imageData.histogram.valuesG, br); ReadHistogram(imageData.histogram.valuesB, br); imagesData.Add(imageData); var assetPath = m_HashToFileMap[guid]; m_AssetPathToIndex.Add(assetPath, i); } } }
public static void ComputeBestColorsAndHistogram(Color32[] pixels, ColorInfo[] bestColors, ColorInfo[] bestShades, Histogram histogram) { var nbPixels = pixels.Length; var colorMap = new Dictionary <uint, long>(); var rgbClusters = new RGBClusters(); foreach (var pixel in pixels) { var pixelValue = ColorToInt(pixel); histogram.AddPixel(pixel); if (!colorMap.ContainsKey(pixelValue)) { colorMap.Add(pixelValue, 0); } ++colorMap[pixelValue]; rgbClusters.AddColor(pixel); } histogram.Normalize(nbPixels); // Get the best colors var orderedColors = colorMap.ToList(); // Order in reverse order so highest count is first orderedColors.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value)); var bestOrderedColors = orderedColors.Take(5).ToList(); var bestClusters = rgbClusters.GetBestClusters(5).ToList(); for (var i = bestOrderedColors.Count; i < 5; ++i) { bestOrderedColors.Add(new KeyValuePair <uint, long>(0, 0)); } for (var i = 0; i < 5; ++i) { bestColors[i] = new ColorInfo { color = bestOrderedColors[i].Key, ratio = bestOrderedColors[i].Value / (double)nbPixels }; bestShades[i] = new ColorInfo { color = ColorToInt(bestClusters[i].average), ratio = bestClusters[i].count / (double)nbPixels }; } }