Ejemplo n.º 1
0
        public override ImageSimilarityReport <TPixelA, TPixelB> CompareImages <TPixelA, TPixelB>(Image <TPixelA> expected, Image <TPixelB> actual)
        {
            if (expected.Size() != actual.Size())
            {
                throw new InvalidOperationException("Calling ImageComparer is invalid when dimensions mismatch!");
            }

            int width = actual.Width;

            // TODO: Comparing through Rgba64 may not robust enough because of the existence of super high precision pixel types.

            var aBuffer = new Vector4[width];
            var bBuffer = new Vector4[width];

            float totalDifference = 0F;

            var differences   = new List <PixelDifference>();
            var configuration = new ImageSharp.Configuration();

            for (int y = 0; y < actual.Height; y++)
            {
                Span <TPixelA> aSpan = expected.GetPixelRowSpan(y);
                Span <TPixelB> bSpan = actual.GetPixelRowSpan(y);

                PixelOperations <TPixelA> .Instance.ToVector4(configuration, aSpan, aBuffer);

                PixelOperations <TPixelB> .Instance.ToVector4(configuration, bSpan, bBuffer);

                for (int x = 0; x < width; x++)
                {
                    float d = GetManhattanDistanceInRgbaSpace(ref aBuffer[x], ref bBuffer[x]);

                    if (d > this.PerPixelManhattanThreshold)
                    {
                        var diff = new PixelDifference(new Point(x, y), aBuffer[x], bBuffer[x]);
                        differences.Add(diff);

                        totalDifference += d;
                    }
                }
            }

            float normalizedDifference = totalDifference / (actual.Width * (float)actual.Height);

            normalizedDifference /= 4F * 65535F;

            if (normalizedDifference > this.ImageThreshold)
            {
                return(new ImageSimilarityReport <TPixelA, TPixelB>(expected, actual, differences, normalizedDifference));
            }
            else
            {
                return(ImageSimilarityReport <TPixelA, TPixelB> .Empty);
            }
        }
Ejemplo n.º 2
0
        public override ImageSimilarityReport <TPixelA, TPixelB> CompareImages <TPixelA, TPixelB>(
            Image <TPixelA> expected,
            Image <TPixelB> actual)
        {
            if (expected.Size() != actual.Size())
            {
                throw new InvalidOperationException("Calling ImageComparer is invalid when dimensions mismatch!");
            }

            int width = actual.Width;

            // TODO: Comparing through Rgba64 may not be robust enough because of the existence of super high precision pixel types.

            var aBuffer = new Vector4[width];
            var bBuffer = new Vector4[width];

            var differences = new List <PixelDifference>();

            ImageSharp.Configuration configuration = expected.GetConfiguration();

            for (int y = 0; y < actual.Height; y++)
            {
                Span <TPixelA> aSpan = expected.GetPixelRowSpan(y);
                Span <TPixelB> bSpan = actual.GetPixelRowSpan(y);

                PixelOperations <TPixelA> .Instance.ToVector4(configuration, aSpan, aBuffer);

                PixelOperations <TPixelB> .Instance.ToVector4(configuration, bSpan, bBuffer);

                for (int x = 0; x < width; x++)
                {
                    Vector4 aPixel = aBuffer[x];
                    Vector4 bPixel = bBuffer[x];

                    if (aPixel != bPixel)
                    {
                        var diff = new PixelDifference(new Point(x, y), aPixel, bPixel);
                        differences.Add(diff);
                    }
                }
            }

            return(new ImageSimilarityReport <TPixelA, TPixelB>(expected, actual, differences));
        }