// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. using System; using System.Collections.Generic; using System.Linq; using System.Text; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison { public class ImageSimilarityReport { protected ImageSimilarityReport( object expectedImage, object actualImage, IEnumerable differences, float? totalNormalizedDifference = null) { this.ExpectedImage = expectedImage; this.ActualImage = actualImage; this.TotalNormalizedDifference = totalNormalizedDifference; this.Differences = differences.ToArray(); } public object ExpectedImage { get; } public object ActualImage { get; } // TODO: This should not be a nullable value! public float? TotalNormalizedDifference { get; } public string DifferencePercentageString { get { if (!this.TotalNormalizedDifference.HasValue) { return "?"; } else if (this.TotalNormalizedDifference == 0) { return "0%"; } else { return $"{this.TotalNormalizedDifference.Value * 100:0.0000}%"; } } } public PixelDifference[] Differences { get; } public bool IsEmpty => this.Differences.Length == 0; public override string ToString() { return this.IsEmpty ? "[SimilarImages]" : this.PrintDifference(); } private string PrintDifference() { var sb = new StringBuilder(); if (this.TotalNormalizedDifference.HasValue) { sb.AppendLine(); sb.AppendLine($"Total difference: {this.DifferencePercentageString}"); } int max = Math.Min(5, this.Differences.Length); for (int i = 0; i < max; i++) { sb.Append(this.Differences[i]); if (i < max - 1) { sb.AppendFormat(";{0}", Environment.NewLine); } } if (this.Differences.Length >= 5) { sb.Append("..."); } return sb.ToString(); } } public class ImageSimilarityReport : ImageSimilarityReport where TPixelA : unmanaged, IPixel where TPixelB : unmanaged, IPixel { public ImageSimilarityReport( ImageFrame expectedImage, ImageFrame actualImage, IEnumerable differences, float? totalNormalizedDifference = null) : base(expectedImage, actualImage, differences, totalNormalizedDifference) { } public static ImageSimilarityReport Empty => new ImageSimilarityReport(null, null, Enumerable.Empty(), 0f); public new ImageFrame ExpectedImage => (ImageFrame)base.ExpectedImage; public new ImageFrame ActualImage => (ImageFrame)base.ActualImage; } }