Exemplo n.º 1
0
        private Image getRandomImage(int[] indexes, ImagePalette palette, Image aboveImage, Collection <Image> previous,
                                     Collection <Image> disqualified, ICollection <Image> cycleRemoved)
        {
            Collection <Image> images;
            var averageColor  = Painter.GetAverageColor(this.SourcePixels, indexes);
            var imagesToAvoid = disqualified.Concat(cycleRemoved).ToList();

            var randomSelectionSize = 8;

            var disqualifiedSize = 6;

            if (aboveImage != null)
            {
                imagesToAvoid.Add(aboveImage);
                images = palette.FindMultipleImagesClosestToColor(averageColor, randomSelectionSize, imagesToAvoid);
                imagesToAvoid.RemoveAt(imagesToAvoid.Count - 1);
            }
            else
            {
                images = palette.FindMultipleImagesClosestToColor(averageColor, randomSelectionSize, imagesToAvoid);
            }

            if (disqualified.Count > disqualifiedSize)
            {
                disqualified.RemoveAt(0);
            }

            var imageToUse = this.chooseRandom(images);

            disqualified.Add(imageToUse);
            previous.Add(imageToUse);
            return(imageToUse);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Image" /> class.
        /// </summary>
        /// <param name="pixels">The pixels.</param>
        /// <param name="decoder">The decoder.</param>
        /// <param name="thumbnail">The thumbnail.</param>
        public Image(byte[] pixels, BitmapDecoder decoder, BitmapImage thumbnail)
        {
            this.SourcePixels = pixels;
            this.Decoder      = decoder;
            this.Thumbnail    = thumbnail;

            this.AverageColor = Painter.GetAverageColor(pixels);
        }
Exemplo n.º 3
0
        private void addImageToAverageColorDictionary(Image image)
        {
            var color = Painter.GetAverageColor(image.SourcePixels);

            if (!this.AverageColorDictionary.ContainsKey(color))
            {
                this.AverageColorDictionary.Add(color, new Collection <Image>());
            }

            this.AverageColorDictionary[color].Add(image);
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Creates a picture mosaic with the specified block size and image palette
        /// </summary>
        /// <param name="blockSize"> the block size </param>
        /// <param name="palette"> the palette to use</param>
        /// <param name="juxtaposition"> whether to juxtaposition the picture to prevent patterns</param>
        /// ///
        /// <param name="cycle">
        ///     whether to cycle through available photos to ensure no photo used used more that once more than
        ///     any other
        /// </param>
        public async Task CreatePictureMosaic(int blockSize, ImagePalette palette, bool juxtaposition, bool cycle)
        {
            if (palette.AverageColorDictionary.Count == 0)
            {
                throw new ArgumentException("There must be a minimum of one picture in the palette.", nameof(palette));
            }

            if (juxtaposition && cycle && palette.AverageColorDictionary.Count < 1)
            {
                throw new ArgumentException(
                          "There must be at least two pictures in the palette to both cycle thorough images and prevent juxtaposition",
                          nameof(palette));
            }

            var   colors          = palette.AverageColorDictionary;
            var   cycleRemoved    = new Collection <Image>();
            var   disqualified    = new Collection <Image>();
            var   previous        = new Collection <Image>();
            var   tasks           = new Collection <Task>();
            var   aboveImageIndex = 0;
            var   imageByteWidth  = (int)this.ImageWidth * 4;
            Image aboveImage      = null;

            foreach (var index in this.getBlockStartingPoints(blockSize))
            {
                var indexes = IndexMapper.Box(index, blockSize, (int)this.ImageWidth, (int)this.ImageHeight);
                IndexMapper.ConvertEachIndexToMatchOffset(indexes, 4);
                var averageColor = Painter.GetAverageColor(this.SourcePixels, indexes);

                Image imageToUse = null;

                if (juxtaposition)
                {
                    if (index > imageByteWidth)
                    {
                        aboveImage = previous[aboveImageIndex];
                        aboveImageIndex++;
                    }

                    imageToUse = this.getRandomImage(indexes, palette, aboveImage, previous, disqualified,
                                                     cycleRemoved);
                }

                if (cycle)
                {
                    if (imageToUse == null)
                    {
                        imageToUse = findClosestMatch(colors, averageColor, cycleRemoved);
                    }

                    cycleRemoved.Add(imageToUse);

                    if (cycleRemoved.Count == colors.Count)
                    {
                        cycleRemoved.Clear();
                    }
                }

                if (imageToUse == null)
                {
                    imageToUse = findClosestMatch(colors, averageColor, cycleRemoved);
                }

                var task = await Task.Factory.StartNew(async() =>
                {
                    await imageToUse.ResizeImage(blockSize);
                    Painter.FillBlockWithPicture(this.SourcePixels, imageToUse.ModifiedPixels, indexes);
                });

                tasks.Add(task);
            }

            foreach (var task in tasks)
            {
                await task;
            }
        }