Exemple #1
0
        public void Diffmask(string pathPic1, string pathPic2, int expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage)
        {
            using (var fileStreamDifferenceMask = File.Create("differenceMask.png"))
                using (var maskImage = ImageSharpCompare.CalcDiffMaskImage(pathPic1, pathPic2))
                {
                    SixLabors.ImageSharp.ImageExtensions.SaveAsPng(maskImage, fileStreamDifferenceMask);
                }

            var maskedDiff = ImageSharpCompare.CalcDiff(pathPic1, pathPic2, "differenceMask.png");

            Assert.That(maskedDiff.AbsoluteError, Is.EqualTo(expectedAbsoluteError), "AbsoluteError");
            Assert.That(maskedDiff.MeanError, Is.EqualTo(expectedMeanError), "MeanError");
            Assert.That(maskedDiff.PixelErrorCount, Is.EqualTo(expectedPixelErrorCount), "PixelErrorCount");
            Assert.That(maskedDiff.PixelErrorPercentage, Is.EqualTo(expectedPixelErrorPercentage), "PixelErrorPercentage");
        }
        public void DiffmaskSteams(string pathPic1, string pathPic2, int expectedMeanError, int expectedAbsoluteError, int expectedPixelErrorCount, double expectedPixelErrorPercentage)
        {
            using var pic1 = new FileStream(pathPic1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            using var pic2 = new FileStream(pathPic2, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            using var maskImage = ImageSharpCompare.CalcDiffMaskImage(pic1, pic2);

            pic1.Position = 0;
            pic2.Position = 0;

            var maskedDiff = ImageSharpCompare.CalcDiff(pic1, pic2, maskImage);

            Assert.That(maskedDiff.AbsoluteError, Is.EqualTo(expectedAbsoluteError), "AbsoluteError");
            Assert.That(maskedDiff.MeanError, Is.EqualTo(expectedMeanError), "MeanError");
            Assert.That(maskedDiff.PixelErrorCount, Is.EqualTo(expectedPixelErrorCount), "PixelErrorCount");
            Assert.That(maskedDiff.PixelErrorPercentage, Is.EqualTo(expectedPixelErrorPercentage), "PixelErrorPercentage");
        }
    public static void AssertImagesEqual <TPixel>(Image <TPixel> actualImage,
                                                  string referenceName) where TPixel : unmanaged, IPixel <TPixel>
    {
        var imageBasename = TestContext.CurrentContext.Test.FullName.Replace("OpenTemple.Tests.", "")
                            + Path.GetFileNameWithoutExtension(referenceName);
        var actualPath = imageBasename + "_actual.png";

        actualImage.Save(actualPath);

        var expectedPath = TestData.GetPath(referenceName);

        if (!File.Exists(expectedPath))
        {
            File.Copy(actualPath, expectedPath);
            throw new NUnitException("Reference file didn't exist: " + expectedPath
                                     + " copied actual to its location");
        }

        using var expectedImage   = Image.Load(expectedPath);
        using var expectedImage32 = expectedImage.CloneAs <Rgba32>();
        using var actualImage32   = actualImage.CloneAs <Rgba32>();

        var imageDiff = ImageSharpCompare.CalcDiff(actualImage32, expectedImage32);

        if (imageDiff.PixelErrorCount > 0)
        {
            // Re-save the expected image so it is next to the actual and difference
            File.Delete(imageBasename + "_expected.png");
            File.Copy(expectedPath, imageBasename + "_expected.png");

            // Calculate the difference as an image
            var visualDifference = imageBasename + "_difference.png";
            using (var maskImage = ImageSharpCompare.CalcDiffMaskImage(actualImage32, expectedImage32))
                maskImage.SaveAsPng(visualDifference);

            TestContext.AddTestAttachment(actualPath, "Actual image");
            TestContext.AddTestAttachment(imageBasename + "_expected.png", "Expected image");
            TestContext.AddTestAttachment(visualDifference, "Visual difference");
            Assert.AreEqual(0, imageDiff.PixelErrorCount, "Images are different, see "
                            + actualPath + " and " + visualDifference);
        }
    }