示例#1
0
 public ColorClusteredBitmap(string originalImagePath, string convertedImagePath, WeightedColorSet histogram)
 {
     OriginalImagePath  = originalImagePath;
     ConvertedImagePath = convertedImagePath;
     Histogram          = histogram;
     Colors             = histogram.Colors.Select(c => c.Color.ToWindowsColor()).ToArray();
     ColorWeights       = histogram.Colors.Select(c => c.PixelCount).ToArray();
 }
示例#2
0
        private async void PopulateImagesAsync(string originalImages, string computedImages)
        {
            string[] originalFilePaths  = Directory.GetFiles(originalImages, "*.png").Concat(Directory.GetFiles(originalImages, "*.jpg")).ToArray();
            string[] computedFilesPaths = Directory.GetFiles(computedImages, "*.png");

            Tuple <string, string>[] joinedPaths = originalFilePaths.Join(computedFilesPaths, o => Path.GetFileNameWithoutExtension(o), i => GetPrefix(i), (o, i) => Tuple.Create(o, i)).ToArray();

            ColorClusteredBitmap[] bitmaps = new ColorClusteredBitmap[originalFilePaths.Length];

            int progress = 0;
            var timer    = InitializeProgress(joinedPaths.Length, () => progress);

            long totalWidths  = 0;
            long totalHeights = 0;

            await Task.Run(() =>
            {
                Parallel.For(0, joinedPaths.Length, i =>
                {
                    string histogramFile = Path.Combine(Path.GetDirectoryName(joinedPaths[i].Item2), "colorHistograms", $"{Path.GetFileNameWithoutExtension(joinedPaths[i].Item2)}.json");
                    if (File.Exists(histogramFile))
                    {
                        WeightedColorSet set = JsonConvert.DeserializeObject <WeightedColorSet>(File.ReadAllText(histogramFile));
                        bitmaps[i]           = new ColorClusteredBitmap(joinedPaths[i].Item1, joinedPaths[i].Item2, set);

                        Interlocked.Add(ref totalWidths, bitmaps[i].Histogram.PixelWidth);
                        Interlocked.Add(ref totalHeights, bitmaps[i].Histogram.PixelHeight);
                    }

                    Interlocked.Increment(ref progress);
                });
            });

            FinishProgress(timer);

            foreach (ColorClusteredBitmap result in bitmaps.Where(b => b != null))
            {
                sources.Add(result);
            }

            averageWidth       = (int)(totalWidths / sources.Count);
            averageHeight      = (int)(totalHeights / sources.Count);
            averageAspectRatio = (float)totalWidths / totalHeights;
        }