public static Image <TPixel> CompareToOriginal <TPixel>(
            this Image <TPixel> image,
            ITestImageProvider provider,
            ImageComparer comparer,
            IImageDecoder referenceDecoder = null)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            string path = TestImageProvider <TPixel> .GetFilePathOrNull(provider);

            if (path == null)
            {
                throw new InvalidOperationException("CompareToOriginal() works only with file providers!");
            }

            var testFile = TestFile.Create(path);

            referenceDecoder = referenceDecoder ?? TestEnvironment.GetReferenceDecoder(path);

            using (var original = Image.Load <TPixel>(testFile.Bytes, referenceDecoder))
            {
                comparer.VerifySimilarity(original, image);
            }

            return(image);
        }
        /// <summary>
        /// Loads the expected image with a reference decoder + compares it to <paramref name="image"/>.
        /// Also performs a debug save using <see cref="ImagingTestCaseUtility.SaveTestOutputFile{TPixel}"/>.
        /// </summary>
        internal static void VerifyEncoder <TPixel>(
            this Image <TPixel> image,
            ITestImageProvider provider,
            string extension,
            object testOutputDetails,
            IImageEncoder encoder,
            ImageComparer customComparer   = null,
            bool appendPixelTypeToFileName = true,
            string referenceImageExtension = null,
            IImageDecoder referenceDecoder = null)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            string actualOutputFile = provider.Utility.SaveTestOutputFile(
                image,
                extension,
                encoder,
                testOutputDetails,
                appendPixelTypeToFileName);

            referenceDecoder ??= TestEnvironment.GetReferenceDecoder(actualOutputFile);

            using (var encodedImage = Image.Load <TPixel>(actualOutputFile, referenceDecoder))
            {
                ImageComparer comparer = customComparer ?? ImageComparer.Exact;
                comparer.VerifySimilarity(encodedImage, image);
            }
        }
        /// <summary>
        /// Compares the image against the expected Reference output, throws an exception if the images are not similar enough.
        /// The output file should be named identically to the output produced by <see cref="DebugSave{TPixel}(Image{TPixel}, ITestImageProvider, object, string, bool)"/>.
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="image">The image which should be compared to the reference output.</param>
        /// <param name="comparer">The <see cref="ImageComparer"/> to use.</param>
        /// <param name="provider">The image provider.</param>
        /// <param name="testOutputDetails">Details to be concatenated to the test output file, describing the parameters of the test.</param>
        /// <param name="extension">The extension</param>
        /// <param name="grayscale">A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size.</param>
        /// <param name="appendPixelTypeToFileName">A boolean indicating whether to append the pixel type to the  output file name.</param>
        /// <param name="appendSourceFileOrDescription">A boolean indicating whether to append <see cref="ITestImageProvider.SourceFileOrDescription"/> to the test output file name.</param>
        /// <param name="decoder">A custom decoder.</param>
        /// <returns>The image.</returns>
        public static Image <TPixel> CompareToReferenceOutput <TPixel>(
            this Image <TPixel> image,
            ImageComparer comparer,
            ITestImageProvider provider,
            object testOutputDetails           = null,
            string extension                   = "png",
            bool grayscale                     = false,
            bool appendPixelTypeToFileName     = true,
            bool appendSourceFileOrDescription = true,
            IImageDecoder decoder              = null)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using (Image <TPixel> referenceImage = GetReferenceOutputImage <TPixel>(
                       provider,
                       testOutputDetails,
                       extension,
                       appendPixelTypeToFileName,
                       appendSourceFileOrDescription,
                       decoder))
            {
                comparer.VerifySimilarity(referenceImage, image);
            }

            return(image);
        }
        public static Image <TPixel> CompareFirstFrameToReferenceOutput <TPixel>(
            this Image <TPixel> image,
            ImageComparer comparer,
            ITestImageProvider provider,
            object testOutputDetails           = null,
            string extension                   = "png",
            bool grayscale                     = false,
            bool appendPixelTypeToFileName     = true,
            bool appendSourceFileOrDescription = true)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using (var firstFrameOnlyImage = new Image <TPixel>(image.Width, image.Height))
                using (Image <TPixel> referenceImage = GetReferenceOutputImage <TPixel>(
                           provider,
                           testOutputDetails,
                           extension,
                           appendPixelTypeToFileName,
                           appendSourceFileOrDescription))
                {
                    firstFrameOnlyImage.Frames.AddFrame(image.Frames.RootFrame);
                    firstFrameOnlyImage.Frames.RemoveFrame(0);

                    comparer.VerifySimilarity(referenceImage, firstFrameOnlyImage);
                }

            return(image);
        }
Beispiel #5
0
        private static void TestPngEncoderCore <TPixel>(
            TestImageProvider <TPixel> provider,
            PngColorType pngColorType,
            PngFilterMethod pngFilterMethod,
            int compressionLevel        = 6,
            int paletteSize             = 255,
            bool appendPngColorType     = false,
            bool appendPngFilterMethod  = false,
            bool appendPixelType        = false,
            bool appendCompressionLevel = false,
            bool appendPaletteSize      = false)
            where TPixel : struct, IPixel <TPixel>
        {
            using (Image <TPixel> image = provider.GetImage())
            {
                if (!HasAlpha(pngColorType))
                {
                    image.Mutate(c => c.MakeOpaque());
                }

                var encoder = new PngEncoder
                {
                    PngColorType     = pngColorType,
                    PngFilterMethod  = pngFilterMethod,
                    CompressionLevel = compressionLevel,
                    Quantizer        = new WuQuantizer(paletteSize)
                };

                string pngColorTypeInfo     = appendPngColorType ? pngColorType.ToString() : string.Empty;
                string pngFilterMethodInfo  = appendPngFilterMethod ? pngFilterMethod.ToString() : string.Empty;
                string compressionLevelInfo = appendCompressionLevel ? $"_C{compressionLevel}" : string.Empty;
                string paletteSizeInfo      = appendPaletteSize ? $"_PaletteSize-{paletteSize}" : string.Empty;
                string debugInfo            = $"{pngColorTypeInfo}{pngFilterMethodInfo}{compressionLevelInfo}{paletteSizeInfo}";
                //string referenceInfo = $"{pngColorTypeInfo}";

                // Does DebugSave & load reference CompareToReferenceInput():
                string actualOutputFile = ((ITestImageProvider)provider).Utility.SaveTestOutputFile(image, "png", encoder, debugInfo, appendPixelType);

                if (TestEnvironment.IsMono)
                {
                    // There are bugs in mono's System.Drawing implementation, reference decoders are not always reliable!
                    return;
                }

                IImageDecoder referenceDecoder    = TestEnvironment.GetReferenceDecoder(actualOutputFile);
                string        referenceOutputFile = ((ITestImageProvider)provider).Utility.GetReferenceOutputFileName("png", debugInfo, appendPixelType, true);

                using (var actualImage = Image.Load <TPixel>(actualOutputFile, referenceDecoder))
                    using (var referenceImage = Image.Load <TPixel>(referenceOutputFile, referenceDecoder))
                    {
                        float paletteToleranceHack = 80f / paletteSize;
                        paletteToleranceHack = paletteToleranceHack * paletteToleranceHack;
                        ImageComparer comparer = pngColorType == PngColorType.Palette
                                                 ? ImageComparer.Tolerant(ToleranceThresholdForPaletteEncoder * paletteToleranceHack)
                                                 : ImageComparer.Exact;

                        comparer.VerifySimilarity(referenceImage, actualImage);
                    }
            }
        }
        public static Image <TPixel> CompareToOriginal <TPixel>(
            this Image <TPixel> image,
            ITestImageProvider provider,
            ImageComparer comparer)
            where TPixel : struct, IPixel <TPixel>
        {
            string path = TestImageProvider <TPixel> .GetFilePathOrNull(provider);

            if (path == null)
            {
                throw new InvalidOperationException("CompareToOriginal() works only with file providers!");
            }

            var testFile = TestFile.Create(path);

            IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(path);
            IImageFormat  format           = TestEnvironment.GetImageFormat(path);
            IImageDecoder defaultDecoder   = Configuration.Default.FindDecoder(format);

            //if (referenceDecoder.GetType() == defaultDecoder.GetType())
            //{
            //    throw new InvalidOperationException($"Can't use CompareToOriginal(): no actual reference decoder registered for {format.Name}");
            //}

            using (var original = Image.Load <TPixel>(testFile.Bytes, referenceDecoder))
            {
                comparer.VerifySimilarity(original, image);
            }

            return(image);
        }
        public unsafe void Issue1640 <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            if (!TestEnvironment.Is64BitProcess)
            {
                return;
            }

            // https://github.com/SixLabors/ImageSharp/discussions/1640
            // Test using isolated memory to ensure clean buffers for reference
            provider.Configuration = Configuration.CreateDefaultInstance();
            var options = new HistogramEqualizationOptions
            {
                Method          = HistogramEqualizationMethod.AdaptiveTileInterpolation,
                LuminanceLevels = 4096,
                ClipHistogram   = false,
                ClipLimit       = 350,
                NumberOfTiles   = 8
            };

            using Image <TPixel> image           = provider.GetImage();
            using Image <TPixel> referenceResult = image.Clone(ctx =>
            {
                ctx.HistogramEqualization(options);
                ctx.Resize(image.Width / 4, image.Height / 4, KnownResamplers.Bicubic);
            });

            using Image <TPixel> processed = image.Clone(ctx =>
            {
                ctx.HistogramEqualization(options);
                ctx.Resize(image.Width / 4, image.Height / 4, KnownResamplers.Bicubic);
            });

            ValidatorComparer.VerifySimilarity(referenceResult, processed);
        }
Beispiel #8
0
        private static void TestPngEncoderCore <TPixel>(
            TestImageProvider <TPixel> provider,
            PngColorType pngColorType,
            int compressionLevel        = 6,
            int paletteSize             = 0,
            bool appendPngColorType     = false,
            bool appendPixelType        = false,
            bool appendCompressionLevel = false,
            bool appendPaletteSize      = false)
            where TPixel : struct, IPixel <TPixel>
        {
            using (Image <TPixel> image = provider.GetImage())
            {
                if (!HasAlpha(pngColorType))
                {
                    image.Mutate(c => c.MakeOpaque());
                }

                var encoder = new PngEncoder
                {
                    PngColorType     = pngColorType,
                    CompressionLevel = compressionLevel,
                    PaletteSize      = paletteSize
                };

                string pngColorTypeInfo     = appendPngColorType ? pngColorType.ToString() : "";
                string compressionLevelInfo = appendCompressionLevel ? $"_C{compressionLevel}" : "";
                string paletteSizeInfo      = appendPaletteSize ? $"_PaletteSize-{paletteSize}" : "";
                string debugInfo            = $"{pngColorTypeInfo}{compressionLevelInfo}{paletteSizeInfo}";
                //string referenceInfo = $"{pngColorTypeInfo}";

                // Does DebugSave & load reference CompareToReferenceInput():
                string actualOutputFile = ((ITestImageProvider)provider).Utility.SaveTestOutputFile(image, "png", encoder, debugInfo, appendPixelType);

                IImageDecoder referenceDecoder    = TestEnvironment.GetReferenceDecoder(actualOutputFile);
                string        referenceOutputFile = ((ITestImageProvider)provider).Utility.GetReferenceOutputFileName("png", debugInfo, appendPixelType);

                using (var actualImage = Image.Load <TPixel>(actualOutputFile, referenceDecoder))
                    using (var referenceImage = Image.Load <TPixel>(referenceOutputFile, referenceDecoder))
                    {
                        ImageComparer comparer = pngColorType == PngColorType.Palette
                                                 ? ImageComparer.Tolerant(ToleranceThresholdForPaletteEncoder)
                                                 : ImageComparer.Exact;

                        comparer.VerifySimilarity(referenceImage, actualImage);
                    }
            }
        }
 public void VerifySimilarity_ThrowsOnSizeMismatch <TPixel>(TestImageProvider <TPixel> provider, int w, int h)
     where TPixel : struct, IPixel <TPixel>
 {
     using (Image <TPixel> image = provider.GetImage())
     {
         using (Image <TPixel> clone = image.Clone(ctx => ctx.Resize(w, h)))
         {
             ImageDimensionsMismatchException ex = Assert.ThrowsAny <ImageDimensionsMismatchException>(
                 () =>
             {
                 ImageComparer comparer = Mock.Of <ImageComparer>();
                 comparer.VerifySimilarity(image, clone);
             });
             this.Output.WriteLine(ex.Message);
         }
     }
 }
Beispiel #10
0
        public void From24bppRgbSystemDrawingBitmap <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            string path = SavePng(provider, PngColorType.Rgb);

            using (Image <TPixel> original = provider.GetImage())
            {
                using (var sdBitmap = new System.Drawing.Bitmap(path))
                {
                    using (Image <TPixel> resaved = SystemDrawingBridge.From24bppRgbSystemDrawingBitmap <TPixel>(sdBitmap))
                    {
                        ImageComparer comparer = ImageComparer.Exact;
                        comparer.VerifySimilarity(original, resaved);
                    }
                }
            }
        }
        public void FromFromRgb24SystemDrawingBitmap2 <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : struct, IPixel <TPixel>
        {
            string path = SavePng(provider, PngColorType.Rgb);

            using (Image <TPixel> original = provider.GetImage())
            {
                original.Mutate(c => c.MakeOpaque());
                using (var sdBitmap = new System.Drawing.Bitmap(path))
                {
                    using (Image <TPixel> resaved = SystemDrawingBridge.FromFromRgb24SystemDrawingBitmap <TPixel>(sdBitmap))
                    {
                        resaved.Mutate(c => c.MakeOpaque());
                        ImageComparer comparer = ImageComparer.Exact;
                        comparer.VerifySimilarity(original, resaved);
                    }
                }
            }
        }
Beispiel #12
0
        public void From32bppArgbSystemDrawingBitmap2 <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            if (TestEnvironment.IsLinux)
            {
                return;
            }

            string path = SavePng(provider, PngColorType.RgbWithAlpha);

            using (var sdBitmap = new System.Drawing.Bitmap(path))
            {
                using (Image <TPixel> original = provider.GetImage())
                    using (Image <TPixel> resaved = SystemDrawingBridge.From32bppArgbSystemDrawingBitmap <TPixel>(sdBitmap))
                    {
                        ImageComparer comparer = ImageComparer.Exact;
                        comparer.VerifySimilarity(original, resaved);
                    }
            }
        }
        public static Image <TPixel> CompareToReferenceOutputMultiFrame <TPixel>(
            this Image <TPixel> image,
            ITestImageProvider provider,
            ImageComparer comparer,
            object testOutputDetails       = null,
            string extension               = "png",
            bool appendPixelTypeToFileName = true)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using (Image <TPixel> referenceImage = GetReferenceOutputImageMultiFrame <TPixel>(
                       provider,
                       image.Frames.Count,
                       testOutputDetails,
                       extension,
                       appendPixelTypeToFileName))
            {
                comparer.VerifySimilarity(referenceImage, image);
            }

            return(image);
        }
Beispiel #14
0
        internal static void RunBufferCapacityLimitProcessorTest <TPixel>(
            this TestImageProvider <TPixel> provider,
            int bufferCapacityInPixelRows,
            Action <IImageProcessingContext> process,
            object testOutputDetails = null,
            ImageComparer comparer   = null)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            comparer ??= ImageComparer.Exact;
            using Image <TPixel> expected = provider.GetImage();
            int width = expected.Width;

            expected.Mutate(process);

            var allocator = ArrayPoolMemoryAllocator.CreateDefault();

            provider.Configuration.MemoryAllocator = allocator;
            allocator.BufferCapacityInBytes        = bufferCapacityInPixelRows * width * Unsafe.SizeOf <TPixel>();

            using Image <TPixel> actual = provider.GetImage();
            actual.Mutate(process);
            comparer.VerifySimilarity(expected, actual);
        }