Пример #1
0
        public Image <Rgba32> RenderAsGif(int width = 320, int height = 120, int margin = 7)
        {
            using (var image = new Image <Rgba32>(width, height))
            {
                int barwidth = width / 5;
                for (int arrayIndex = 0; arrayIndex < _bitArray.Count; arrayIndex++)
                {
                    for (byte cl = 1; cl >= 0; cl--)
                    {
                        // Weird flex move that sets the 5th bit first to 1 then to 0
                        var bits = this._bitArray[arrayIndex];
                        bits[0] = cl;

                        // Build Frame
                        using (var frame = new Image <Rgba32>(width, height))
                        {
                            for (int byteIndex = 0; byteIndex < 5; byteIndex++)
                            {
                                var color  = _bitArray[arrayIndex][byteIndex] == 1 ? Color.White :Color.Black;
                                var bounds = new CoreRectangle(byteIndex * barwidth + margin, margin, barwidth - 2 * margin, height - 2 * margin);
                                frame.Mutate(x => x.Fill(color, bounds));
                            }
                            image.Frames.AddFrame(frame.Frames[0]);
                        }
                    }
                }
                return(image);
            }
        }
Пример #2
0
        public Image <Rgba32> RenderAsGif(int width = 320, int height = 120)
        {
            using (var image = new Image <Rgba32>(width, height))
            {
                int margin = 7, barwidth = width / 5;
                for (int i = 0; i < bitarray.Count; i++)
                {
                    for (int cl = 1; cl >= 0; cl--)
                    {
                        int[] bits = this.bitarray[i];
                        bits[0] = cl;
                        using (var frame = new Image <Rgba32>(width, height))
                        {
                            for (int b = 0; b < 5; b++)
                            {
                                var color  = bitarray[i][b] == 1 ? Color.White :Color.Black;
                                var bounds = new CoreRectangle(b * barwidth + margin, margin, barwidth - 2 * margin, height - 2 * margin);
                                frame.Mutate(x => x.Fill(color, bounds));
                            }
                            image.Frames.AddFrame(frame.Frames[0]);
                        }
                    }
                }

                return(image);
            }
        }
Пример #3
0
        public static void ImageRegionIsColor(this Assert assert, SixLabors.ImageSharp.Rectangle region, Rgb24 expectedColor, Image <Rgb24> actualImage, double tolerance = 0.0)
        {
            //var width = region.Width;
            var area = region.Width * region.Height;

            var red     = new int[area];
            var green   = new int[area];
            var blue    = new int[area];
            var indices = new HashSet <int>();

            for (var x = region.Left; x < region.Right; x++)
            {
                for (var y = region.Top; y < region.Bottom; y++)
                {
                    var color = actualImage[x, y];

                    var i      = x - region.Left;
                    var j      = y - region.Top;
                    var index0 = (i * region.Height) + j;
                    if (indices.Contains(index0))
                    {
                        Debugger.Break();
                    }

                    indices.Add(index0);
                    red[index0]   = color.R;
                    green[index0] = color.G;
                    blue[index0]  = color.B;
                }
            }

            var averageRed   = red.Average();
            var averageBlue  = blue.Average();
            var averageGreen = green.Average();

            Assert.IsTrue(
                Math.Abs(averageRed - expectedColor.R) <= tolerance &&
                Math.Abs(averageGreen - expectedColor.G) <= tolerance &&
                Math.Abs(averageBlue - expectedColor.B) <= tolerance,
                $"Region {region} is not expected color {expectedColor} - actual averages: R={averageRed:F20}, G={averageGreen:F20}, B={averageBlue:F20}");
        }
Пример #4
0
        public static void ImageContainsExpected <T>(this Assert assert, Image <T> expectedImage, SixLabors.ImageSharp.Point expectedLocation, Image <T> actualImage, double tolerance = 0.0, string message = "")
            where T : struct, IPixel <T>
        {
            var regionToCheck = new SixLabors.ImageSharp.Rectangle(expectedLocation, expectedImage.Size());

            Assert.IsTrue(actualImage.Bounds().Contains(regionToCheck));

            var(normalizedDifference, differences) = CompareImage(expectedImage, actualImage, regionToCheck);

            if (normalizedDifference > tolerance)
            {
                var deltaStrings = differences.Take(10).Select(x => x.ToString()).FormatList();
                var assertReason =
                    $@"Images are not equal - total delta {normalizedDifference} is not less than tolerance {tolerance}.
Difference are:
{deltaStrings}
(and {differences.Count} more..)

{message}";
                Assert.Fail(assertReason);
            }
        }
Пример #5
0
 /// <summary>
 /// Pixelates an image with the given pixel size.
 /// </summary>
 /// <typeparam name="TPixel">The pixel format.</typeparam>
 /// <param name="source">The image this method extends.</param>
 /// <param name="size">The size of the pixels.</param>
 /// <param name="rectangle">
 /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
 /// </param>
 /// <returns>The <see cref="Image{TPixel}"/>.</returns>
 public static IImageProcessingContext <TPixel> Pixelate <TPixel>(this IImageProcessingContext <TPixel> source, int size, Rectangle rectangle)
     where TPixel : struct, IPixel <TPixel>
 => source.ApplyProcessor(new PixelateProcessor <TPixel>(size), rectangle);