コード例 #1
0
 /// <summary>
 ///     Converts the image to black and white
 /// </summary>
 public void ConvertImageToBlackAndWhite()
 {
     Painter.ConvertToBlackAndWhite(this.SourcePixels, (int)this.ImageWidth, (int)this.ImageHeight);
 }
コード例 #2
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;
            }
        }