public void TestBorderAndBackground()
    {
        var backgroundAndBorder = Style(new StyleDefinition()
        {
            BackgroundColor = PackedLinearColorA.FromHex("#666666"),
            BorderColor     = PackedLinearColorA.FromHex("#AAAAAA"),
            BorderWidth     = 2
        });
        var backgroundOnly = Style(new StyleDefinition()
        {
            BackgroundColor = PackedLinearColorA.FromHex("#666666"),
            BorderColor     = PackedLinearColorA.FromHex("#AAAAAA")
        });
        var borderOnly = Style(new StyleDefinition()
        {
            BorderColor = PackedLinearColorA.FromHex("#AAAAAA"),
            BorderWidth = 1
        });
        var thickBorder = Style(new StyleDefinition()
        {
            BorderColor = PackedLinearColorA.FromHex("#AAAAAA"),
            BorderWidth = 4
        });

        Device.BeginDraw();
        Device.TextEngine.RenderBackgroundAndBorder(25, 75, 100, 50, backgroundAndBorder);
        Device.TextEngine.RenderBackgroundAndBorder(25, 275, 100, 50, borderOnly);
        Device.TextEngine.RenderBackgroundAndBorder(225, 275, 100, 50, thickBorder);
        Device.TextEngine.RenderBackgroundAndBorder(225, 75, 100, 50, backgroundOnly);
        Device.EndDraw();

        var screenshot = TakeScreenshot();

        ImageComparison.AssertImagesEqual(screenshot, "Core/GFX/TextRendering/BackgroundAndBorder.png");
    }
    public void CanStartAndReachMainMenu()
    {
        UiSystems.MainMenu.Show(MainMenuPage.MainMenu);

        Game.RenderFrame();
        ImageComparison.AssertImagesEqual(Game.TakeScreenshot(), "Game/MainMenu.png");
    }
        public void NonJpgFilesAreIgnored()
        {
            ImageComparison c = new ImageComparison();
            DirectoryInfo directoryInfo = new DirectoryInfo(@".\TestImages");

            var result = c.Compare(new ReadOnlyCollection<FileInfo>(directoryInfo.GetFiles().Where(fi => fi.Name.StartsWith("TestFile")).ToList()));

            Assert.True(result.Keys.Count(f => f.Extension.EndsWith("gif")) == 0);
        }
        public void NoMatchesReturnsEmptyDictionary()
        {
            ImageComparison c = new ImageComparison();
            DirectoryInfo directoryInfo = new DirectoryInfo(@".\TestImages");

            var result = c.Compare(new ReadOnlyCollection<FileInfo>(directoryInfo.GetFiles().Where(fi => !fi.Name.Contains("Copy")).ToList()));

            Assert.True(result.Keys.Count == 0);
        }
    public void TestVeryLongChainLightning()
    {
        Device.BeginDraw();
        _renderer.Render(Viewport.Camera, 0, 0, CameraCenter, CameraCenter + new Vector3(0, 0, 10000));
        Device.EndDraw();

        using var screenshot = TakeScreenshot();
        ImageComparison.AssertImagesEqual(screenshot, "Core/Systems/Vfx/ChainLightningVeryLong.png");
    }
    private void RenderAndCompare(TextLayout layout, string refName)
    {
        Device.BeginDraw();
        Device.TextEngine.RenderTextLayout(200, 200, layout);
        Device.EndDraw();

        var screenshot = TakeScreenshot();

        ImageComparison.AssertImagesEqual(screenshot, refName);
    }
        public void SingleMatchReturnsOneResultInDictionaryList()
        {
            ImageComparison c = new ImageComparison();
            DirectoryInfo directoryInfo = new DirectoryInfo(@".\TestImages");

            var result = c.Compare(new ReadOnlyCollection<FileInfo>(directoryInfo.GetFiles().Where(fi => fi.Name.StartsWith("TestFileB")).ToList()));

            Assert.Equal("TestFileB.jpg", result.Keys.ElementAt(0).Name);
            Assert.True(result[result.Keys.ElementAt(0)].Count == 1);
            Assert.Equal("TestFileBCopy1.jpg", result[result.Keys.ElementAt(0)].First().Name);
        }
        public void NMatchesReturnsNMinusOneResultsInDictionaryList()
        {
            ImageComparison c = new ImageComparison();
            DirectoryInfo directoryInfo = new DirectoryInfo(@".\TestImages");

            var result = c.Compare(new ReadOnlyCollection<FileInfo>(directoryInfo.GetFiles().Where(fi => fi.Name.StartsWith("TestFileC")).ToList()));

            Assert.Equal("TestFileC.jpg", result.Keys.ElementAt(0).Name);
            Assert.True(result[result.Keys.ElementAt(0)].Count == 2);
            Assert.Equal("TestFileCCopy1.jpg", result[result.Keys.ElementAt(0)][0].Name);
            Assert.Equal("TestFileCCopy2.jpg", result[result.Keys.ElementAt(0)][1].Name);
        }
    public void TestHeightDifferenceChainLightning()
    {
        var a = CameraCenter - new Vector3(200, -25, 0);
        var b = CameraCenter;
        var c = CameraCenter + new Vector3(200, 25, 0);

        Device.BeginDraw();
        _renderer.Render(Viewport.Camera, 0, 0, a, b);
        _renderer.Render(Viewport.Camera, 0, 250, b, c);
        Device.EndDraw();

        using var screenshot = TakeScreenshot();
        MarkWorldPositions(screenshot, a, b, c);
        ImageComparison.AssertImagesEqual(screenshot, "Core/Systems/Vfx/ChainLightningHeightDifference.png");
    }
        public void MultipleSourceMatches()
        {
            ImageComparison c = new ImageComparison();
            DirectoryInfo directoryInfo = new DirectoryInfo(@".\TestImages");

            var result = c.Compare(new ReadOnlyCollection<FileInfo>(directoryInfo.GetFiles().Where(fi => fi.Name.StartsWith("TestFile")).ToList()));

            Assert.Equal("TestFileB.jpg", result.Keys.ElementAt(0).Name);
            Assert.True(result[result.Keys.ElementAt(0)].Count == 1);
            Assert.Equal("TestFileBCopy1.jpg", result[result.Keys.ElementAt(0)].First().Name);

            Assert.Equal("TestFileC.jpg", result.Keys.ElementAt(1).Name);
            Assert.True(result[result.Keys.ElementAt(1)].Count == 2);
            Assert.Equal("TestFileCCopy1.jpg", result[result.Keys.ElementAt(1)][0].Name);
            Assert.Equal("TestFileCCopy2.jpg", result[result.Keys.ElementAt(1)][1].Name);
        }
Esempio n. 11
0
        public static void Main(string[] args)
        {
            IImageCompare _imageComparison = new ImageComparison("SampleImages");

            //If you want to crop an image from image then you can call below function
            //Crop image
            //_imageComparison.CropImage("cat_1.jpg", 10, 20, 200, 200);


            //Comparison of same image
            Console.WriteLine("1. Comparison of same images");
            _imageComparison.CompareImages("cat_1.jpg", "cat_1.jpg");

            Console.WriteLine("-------------------------------------------------------------");

            //Comparison of crop image
            Console.WriteLine("2. Comparison of crop images");
            _imageComparison.CompareImages("cat_1.jpg", "cat_2.jpg");
            _imageComparison.CompareImages("cat_1.jpg", "cat_3.jpg");
            _imageComparison.CompareImages("cat_1.jpg", "cat_4.jpg");
            _imageComparison.CompareImages("cat_1.jpg", "cat_6.jpg");

            Console.WriteLine("-------------------------------------------------------------");

            //Comparison of big and short sizes images
            Console.WriteLine("3. Comparison of big and short sizes images");
            _imageComparison.CompareImages("cat_1.jpg", "cat_5.jpg");

            Console.WriteLine("-------------------------------------------------------------");



            //Comparison of mostly common images
            Console.WriteLine("4. Comparison of mostly common images");

            _imageComparison.CompareImages("img_one.png", "img_two.png");
            _imageComparison.CompareImages("lab200.jpg", "lab100.jpg");

            Console.WriteLine("-------------------------------------------------------------");



            Console.WriteLine("Any key to end...");
            Console.ReadKey();
        }
Esempio n. 12
0
    public void TestRasterization()
    {
        const int dimension = 192;
        var       buffer    = new byte[dimension * dimension];

        Span <Vector2> vertices = stackalloc Vector2[3]
        {
            new Vector2(5.5f, 10.2f),
            new Vector2(5.5f + 16, 10.2f + 5.0f),
            new Vector2(5.5f - 5, 10.2f + 5.0f)
        };

        TriangleRasterizer.Rasterize(dimension, dimension, buffer, vertices, 0xFF);

        using var image = Image.LoadPixelData <L8>(buffer, dimension, dimension);
        ImageComparison.AssertImagesEqual(image, "FogOfWar/tesselated_expected.png");
    }
}
Esempio n. 13
0
        static void RunTest(IEnumerable <string> files, bool verbose, int threading)
        {
            SimisProvider provider;

            try {
                provider = new SimisProvider(Path.GetDirectoryName(Application.ExecutablePath) + @"\Resources");
            } catch (FileException ex) {
                Console.WriteLine(ex.ToString());
                return;
            }

            var totalCount     = new TestFormatCount();
            var supportedCount = new TestFormatCount();
            var formatCounts   = new Dictionary <string, TestFormatCount>();
            var timeStart      = DateTime.Now;

            Func <SimisJinxFormat, TestFormatCount> GetFormatFor = (simisFormat) => {
                var formatName = simisFormat.Name;
                if (!formatCounts.ContainsKey(formatName))
                {
                    formatCounts[formatName] = new TestFormatCount()
                    {
                        FormatName = formatName, SortKey = formatName
                    };
                }
                return(formatCounts[formatName]);
            };

            Func <string, ProcessFileResults> ProcessFile = (file) => {
                if (verbose && (threading > 1))
                {
                    lock (formatCounts) {
                        Console.WriteLine(String.Format("[Thread {0}] {1}", Thread.CurrentThread.ManagedThreadId, file));
                    }
                }

                var       result       = new ProcessFileResults();
                var       formatCount  = new TestFormatCount();
                var       fileProvider = provider.GetForPath(file);
                SimisFile newFile      = null;
                Stream    readStream   = new UnclosableStream(new BufferedInMemoryStream(File.OpenRead(file)));
                Stream    saveStream   = new UnclosableStream(new MemoryStream());

                {
                    result.Total = true;
                    try {
                        using (var reader = SimisReader.FromStream(readStream, fileProvider)) {
                            var readerJinx = reader as SimisJinxReader;
                            var readerAce  = reader as SimisAceReader;
                            if (readerJinx != null)
                            {
                                readerJinx.ReadToken();
                                if (readerJinx.JinxStreamFormat == null)
                                {
                                    return(result);
                                }
                                result.JinxStreamFormat = readerJinx.JinxStreamFormat;
                            }
                            else if (readerAce != null)
                            {
                                if (fileProvider.Formats.FirstOrDefault() == null)
                                {
                                    return(result);
                                }
                                result.JinxStreamFormat = fileProvider.Formats.First();
                            }
                            else
                            {
                                return(result);
                            }
                        }
                    } catch (ReaderException) {
                        return(result);
                    }
                    readStream.Position = 0;
                }

                // First, read the file in.
                try {
                    try {
                        newFile = new SimisFile(readStream, fileProvider);
                    } catch (Exception e) {
                        throw new FileException(file, e);
                    }
                    result.ReadSuccess = true;
                } catch (FileException ex) {
                    if (verbose)
                    {
                        lock (formatCounts) {
                            Console.WriteLine("Read: " + ex + "\n");
                        }
                    }
                    return(result);
                }

                // Second, write the file out into memory.
                try {
                    try {
                        newFile.Write(saveStream);
                    } catch (Exception e) {
                        throw new FileException(file, e);
                    }
                    // WriteSuccess is delayed until after the comparison. We won't claim write support without comparison support.
                } catch (FileException ex) {
                    if (verbose)
                    {
                        lock (formatCounts) {
                            Console.WriteLine("Write: " + ex + "\n");
                        }
                    }
                    return(result);
                }

                // Third, verify that the output is the same as the input.
                readStream.Seek(0, SeekOrigin.Begin);
                saveStream.Seek(0, SeekOrigin.Begin);
                var readReader = new BinaryReader(new SimisTestableStream(readStream), newFile.StreamIsBinary ? ByteEncoding.Encoding : Encoding.Unicode);
                var saveReader = new BinaryReader(new SimisTestableStream(saveStream), newFile.StreamIsBinary ? ByteEncoding.Encoding : Encoding.Unicode);
                var isDXTACE   = (result.JinxStreamFormat.Extension == "ace") && ((newFile.Ace.Format & 0x10) != 0);
                var readChars  = readReader.ReadChars((int)readReader.BaseStream.Length);
                var saveChars  = saveReader.ReadChars((int)saveReader.BaseStream.Length);
                var charBytes  = newFile.StreamIsBinary ? 1 : 2;
                var charMin    = Math.Min(readChars.Length, saveChars.Length);
                for (var i = 0; i < charMin; i++)
                {
                    if (isDXTACE && (i > 168))
                    {
                        break;
                    }
                    if (readChars[i] != saveChars[i])
                    {
                        readReader.BaseStream.Position = charBytes * (i + 1);
                        saveReader.BaseStream.Position = charBytes * (i + 1);
                        var readEx = new ReaderException(readReader, newFile.StreamIsBinary, charBytes, "");
                        var saveEx = new ReaderException(saveReader, newFile.StreamIsBinary, charBytes, "");
                        if (verbose)
                        {
                            lock (formatCounts) {
                                Console.WriteLine("Compare: " + String.Format(CultureInfo.CurrentCulture, "{0}\n\nFile character {1:N0} does not match: {2:X4} vs {3:X4}.\n\n{4}{5}\n", file, charBytes * i, readChars[i], saveChars[i], readEx.ToString(), saveEx.ToString()));
                            }
                        }
                        return(result);
                    }
                }
                if ((result.JinxStreamFormat.Extension == "ace") && ((newFile.Ace.Format & 0x10) != 0))
                {
                    // DXT images are a massive pain because it is a lossy compression.
                    saveStream.Seek(0, SeekOrigin.Begin);
                    var saveOutput = new SimisFile(saveStream, fileProvider);
                    Debug.Assert(saveOutput.Ace != null);
                    Debug.Assert(saveOutput.Ace.Format == newFile.Ace.Format);

                    try {
                        if (newFile.Ace.Width != saveOutput.Ace.Width)
                        {
                            throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE width expected {0}; got {1}.", newFile.Ace.Width, saveOutput.Ace.Width));
                        }
                        if (newFile.Ace.Height != saveOutput.Ace.Height)
                        {
                            throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE height expected {0}; got {1}.", newFile.Ace.Height, saveOutput.Ace.Height));
                        }
                        if (newFile.Ace.Unknown7 != saveOutput.Ace.Unknown7)
                        {
                            throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE unknown7 expected {0}; got {1}.", newFile.Ace.Unknown7, saveOutput.Ace.Unknown7));
                        }
                        if (newFile.Ace.Creator != saveOutput.Ace.Creator)
                        {
                            throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE creator expected {0}; got {1}.", newFile.Ace.Creator, saveOutput.Ace.Creator));
                        }
                        var newFileChannels  = String.Join(",", newFile.Ace.Channel.Select(c => c.Type.ToString() + ":" + c.Size).ToArray());
                        var saveFileChannels = String.Join(",", saveOutput.Ace.Channel.Select(c => c.Type.ToString() + ":" + c.Size).ToArray());
                        if (newFileChannels != saveFileChannels)
                        {
                            throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE channels expected {0}; got {1}.", newFileChannels, saveFileChannels));
                        }
                        if (newFile.Ace.Image.Count != saveOutput.Ace.Image.Count)
                        {
                            throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE image count expected {0}; got {1}.", newFile.Ace.Image.Count, saveOutput.Ace.Image.Count));
                        }

                        var errors = new List <double>();
                        for (var i = 0; i < newFile.Ace.Image.Count; i++)
                        {
                            if (newFile.Ace.Image[i].Width != saveOutput.Ace.Image[i].Width)
                            {
                                throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE image {2} width expected {0}; got {1}.", newFile.Ace.Image[i].Width, saveOutput.Ace.Image[i].Width, i));
                            }
                            if (newFile.Ace.Image[i].Height != saveOutput.Ace.Image[i].Height)
                            {
                                throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE image {2} height expected {0}; got {1}.", newFile.Ace.Image[i].Height, saveOutput.Ace.Image[i].Height, i));
                            }
                            errors.Add(ImageComparison.GetRootMeanSquareError(newFile.Ace.Image[i].ImageColor, saveOutput.Ace.Image[i].ImageColor, newFile.Ace.Width, newFile.Ace.Height));
                            errors.Add(ImageComparison.GetRootMeanSquareError(newFile.Ace.Image[i].ImageMask, saveOutput.Ace.Image[i].ImageMask, newFile.Ace.Width, newFile.Ace.Height));
                        }

                        // Any error over 10.0 is considered a fail.
                        var maxError = 10.0;
                        if (errors.Max() > maxError)
                        {
                            throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "Image RMS (root mean square) errors are too high; highest: {2,5:F1} > {0,5:F1}; all: {1}.", maxError, String.Join(", ", errors.Select(e => e.ToString("F1").PadLeft(5)).ToArray()), errors.Max()));
                        }
                    } catch (InvalidDataException ex) {
                        if (verbose)
                        {
                            lock (formatCounts) {
                                Console.WriteLine("Compare: " + String.Format(CultureInfo.CurrentCulture, "{0}\n\n{1}\n", file, ex.Message));
                            }
                        }
                        return(result);
                    }
                }
                else
                {
                    if (readChars.Length != saveChars.Length)
                    {
                        readReader.BaseStream.Position = charBytes * charMin;
                        saveReader.BaseStream.Position = charBytes * charMin;
                        var readEx = new ReaderException(readReader, newFile.StreamIsBinary, 0, "");
                        var saveEx = new ReaderException(saveReader, newFile.StreamIsBinary, 0, "");
                        if (verbose)
                        {
                            lock (formatCounts) {
                                Console.WriteLine("Compare: " + String.Format(CultureInfo.CurrentCulture, "{0}\n\nFile and stream length do not match: {1:N0} vs {2:N0}.\n\n{3}{4}\n", file, readReader.BaseStream.Length, saveReader.BaseStream.Length, readEx.ToString(), saveEx.ToString()));
                            }
                        }
                        return(result);
                    }
                }

                // It all worked!
                result.WriteSuccess = true;
                return(result);
            };

            if (threading > 1)
            {
                var filesEnumerator = files.GetEnumerator();
                var filesFinished   = false;
                var threads         = new List <Thread>(threading);
                for (var i = 0; i < threading; i++)
                {
                    threads.Add(new Thread(() => {
                        var file    = "";
                        var results = new List <ProcessFileResults>();
                        while (true)
                        {
                            lock (filesEnumerator) {
                                if (filesFinished || !filesEnumerator.MoveNext())
                                {
                                    filesFinished = true;
                                    break;
                                }
                                file = filesEnumerator.Current;
                            }
                            results.Add(ProcessFile(file));
                        }
                        lock (totalCount) {
                            foreach (var result in results)
                            {
                                if (result.Total)
                                {
                                    totalCount.Total++;
                                }
                                if (result.ReadSuccess)
                                {
                                    totalCount.ReadSuccess++;
                                }
                                if (result.WriteSuccess)
                                {
                                    totalCount.WriteSuccess++;
                                }
                                if (result.JinxStreamFormat != null)
                                {
                                    var formatCount = GetFormatFor(result.JinxStreamFormat);
                                    if (result.Total)
                                    {
                                        supportedCount.Total++;
                                    }
                                    if (result.ReadSuccess)
                                    {
                                        supportedCount.ReadSuccess++;
                                    }
                                    if (result.WriteSuccess)
                                    {
                                        supportedCount.WriteSuccess++;
                                    }
                                    if (result.Total)
                                    {
                                        formatCount.Total++;
                                    }
                                    if (result.ReadSuccess)
                                    {
                                        formatCount.ReadSuccess++;
                                    }
                                    if (result.WriteSuccess)
                                    {
                                        formatCount.WriteSuccess++;
                                    }
                                }
                            }
                        }
                    }));
                }
                foreach (var thread in threads)
                {
                    thread.Start();
                }
                foreach (var thread in threads)
                {
                    thread.Join();
                }
            }
            else
            {
                foreach (var file in files)
                {
                    var result = ProcessFile(file);
                    if (result.Total)
                    {
                        totalCount.Total++;
                    }
                    if (result.ReadSuccess)
                    {
                        totalCount.ReadSuccess++;
                    }
                    if (result.WriteSuccess)
                    {
                        totalCount.WriteSuccess++;
                    }
                    if (result.JinxStreamFormat != null)
                    {
                        var formatCount = GetFormatFor(result.JinxStreamFormat);
                        if (result.Total)
                        {
                            supportedCount.Total++;
                        }
                        if (result.ReadSuccess)
                        {
                            supportedCount.ReadSuccess++;
                        }
                        if (result.WriteSuccess)
                        {
                            supportedCount.WriteSuccess++;
                        }
                        if (result.Total)
                        {
                            formatCount.Total++;
                        }
                        if (result.ReadSuccess)
                        {
                            formatCount.ReadSuccess++;
                        }
                        if (result.WriteSuccess)
                        {
                            formatCount.WriteSuccess++;
                        }
                    }
                }
            }

            supportedCount.FormatName = "(Total supported files of " + totalCount.Total + ")";
            supportedCount.SortKey    = "ZZZ";
            formatCounts[""]          = supportedCount;

            var outFormat = "{0,-40:S} {1,1:S}{2,-7:D} {3,1:S}{4,-7:D} {5,1:S}{6,-7:D}";

            Console.WriteLine(String.Format(CultureInfo.CurrentCulture, outFormat, "Format Name", "", "Total", "", "Read", "", "Write"));
            Console.WriteLine(String.Empty.PadLeft(69, '='));
            foreach (var formatCount in formatCounts.OrderBy(kvp => kvp.Value.SortKey).Select(kvp => kvp.Value))
            {
                Console.WriteLine(String.Format(CultureInfo.CurrentCulture, outFormat,
                                                formatCount.FormatName,
                                                "", formatCount.Total,
                                                formatCount.Total == formatCount.ReadSuccess ? "*" : "", formatCount.ReadSuccess,
                                                formatCount.Total == formatCount.WriteSuccess ? "*" : formatCount.ReadSuccess == formatCount.WriteSuccess ? "+" : "", formatCount.WriteSuccess));
            }
        }