Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/ImageSharp/Advanced/AotCompilerTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Comparison;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Ani;
using SixLabors.ImageSharp.Formats.Bmp;
Expand Down Expand Up @@ -158,6 +160,7 @@ private static void Seed<TPixel>()
AotCompilePixelMaps<TPixel>();
AotCompileDithers<TPixel>();
AotCompileMemoryManagers<TPixel>();
AotCompileImageComparison<TPixel>();

_ = Unsafe.SizeOf<TPixel>();

Expand Down Expand Up @@ -700,6 +703,24 @@ private static void AotCompileMemoryManagers<TPixel>()
AotCompileMemoryManager<TPixel, SimpleGcMemoryAllocator>();
}

/// <summary>
/// This method pre-seeds the image comparison in the AoT compiler.
/// </summary>
/// <remarks>
/// The comparison is seeded for the same pixel type on both sides. Comparing different pixel
/// types is resolved at run time by the visitor dispatch and follows the same discovery
/// limits as other cross-type generic APIs.
/// </remarks>
/// <typeparam name="TPixel">The pixel format.</typeparam>
[Preserve]
private static void AotCompileImageComparison<TPixel>()
where TPixel : unmanaged, IPixel<TPixel>
{
ImageFrame<TPixel> frame = default;
_ = ImageComparisonEngine.CompareFrames<TPixel, TPixel>(0, frame, frame, 0, 0);
_ = ImageComparisonEngine.CreateDifferenceImage<TPixel, TPixel>(frame, frame);
}

/// <summary>
/// This method pre-seeds the <see cref="MemoryAllocator" /> in the AoT compiler.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Globalization;
using System.Text;

namespace SixLabors.ImageSharp.Comparison.Exceptions;

/// <summary>
/// The exception that is thrown when the difference of a compared image or frame is over the
/// threshold of the comparer.
/// </summary>
public class ImageDifferenceIsOverThresholdException : ImagesSimilarityException
{
/// <summary>
/// Initializes a new instance of the <see cref="ImageDifferenceIsOverThresholdException"/> class.
/// </summary>
/// <param name="reports">The reports of the frames which are over the threshold.</param>
public ImageDifferenceIsOverThresholdException(IReadOnlyList<ImageSimilarityReport> reports)
: base("Image difference is over threshold!" + StringifyReports(reports))
=> this.Reports = reports;

/// <summary>
/// Gets the reports of the frames which are over the threshold.
/// </summary>
public IReadOnlyList<ImageSimilarityReport> Reports { get; }

private static string StringifyReports(IReadOnlyList<ImageSimilarityReport> reports)
{
StringBuilder sb = new();
sb.Append(Environment.NewLine);

foreach (ImageSimilarityReport report in reports)
{
sb.AppendFormat(CultureInfo.InvariantCulture, "Report ImageFrame {0}: ", report.Index)
.Append(report)
.Append(Environment.NewLine);
}

return sb.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.ImageSharp.Comparison.Exceptions;

/// <summary>
/// The exception that is thrown when the dimensions of the compared images or frames do not match.
/// </summary>
public class ImageDimensionsMismatchException : ImagesSimilarityException
{
/// <summary>
/// Initializes a new instance of the <see cref="ImageDimensionsMismatchException"/> class.
/// </summary>
/// <param name="expectedSize">The expected size.</param>
/// <param name="actualSize">The actual size.</param>
public ImageDimensionsMismatchException(Size expectedSize, Size actualSize)
: base($"The image dimensions {actualSize} do not match the expected {expectedSize}!")
{
this.ExpectedSize = expectedSize;
this.ActualSize = actualSize;
}

/// <summary>
/// Gets the expected size of the compared images or frames.
/// </summary>
public Size ExpectedSize { get; }

/// <summary>
/// Gets the actual size of the compared images or frames.
/// </summary>
public Size ActualSize { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.ImageSharp.Comparison.Exceptions;

/// <summary>
/// The exception that is thrown when the number of frames of the compared images does not match.
/// </summary>
public class ImageFramesMismatchException : ImagesSimilarityException
{
/// <summary>
/// Initializes a new instance of the <see cref="ImageFramesMismatchException"/> class.
/// </summary>
/// <param name="expectedCount">The expected frame count.</param>
/// <param name="actualCount">The actual frame count.</param>
public ImageFramesMismatchException(int expectedCount, int actualCount)
: base($"The image frame count {actualCount} does not match the expected {expectedCount}!")
{
this.ExpectedCount = expectedCount;
this.ActualCount = actualCount;
}

/// <summary>
/// Gets the expected frame count of the compared images.
/// </summary>
public int ExpectedCount { get; }

/// <summary>
/// Gets the actual frame count of the compared images.
/// </summary>
public int ActualCount { get; }
}
22 changes: 22 additions & 0 deletions src/ImageSharp/Comparison/Exceptions/ImagesSimilarityException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System;

namespace SixLabors.ImageSharp.Comparison.Exceptions;

/// <summary>
/// The exception that is thrown when the similarity between two images does not meet the
/// requirements of the comparison.
/// </summary>
public class ImagesSimilarityException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="ImagesSimilarityException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public ImagesSimilarityException(string message)
: base(message)
{
}
}
Loading