mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
78 changed files with 677 additions and 764 deletions
@ -1,46 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System.Collections.Generic; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Common.Extensions |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Encapsulates a series of time saving extension methods to the <see cref="T:System.Collections.Generic.List"/> class.
|
|
||||
/// </summary>
|
|
||||
internal static class ListExtensions |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Inserts an item at the given index automatically expanding the capacity if required.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="T">The type of object within the list</typeparam>
|
|
||||
/// <param name="list">The list</param>
|
|
||||
/// <param name="index">The index</param>
|
|
||||
/// <param name="item">The item to insert</param>
|
|
||||
public static void SafeInsert<T>(this List<T> list, int index, T item) |
|
||||
{ |
|
||||
if (index >= list.Count) |
|
||||
{ |
|
||||
list.Add(item); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
list[index] = item; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Removes the last element from a list and returns that element. This method changes the length of the list.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="T">The type of object within the list</typeparam>
|
|
||||
/// <param name="list">The list</param>
|
|
||||
/// <returns>The last element in the specified sequence.</returns>
|
|
||||
public static T Pop<T>(this List<T> list) |
|
||||
{ |
|
||||
int last = list.Count - 1; |
|
||||
T item = list[last]; |
|
||||
list.RemoveAt(last); |
|
||||
return item; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,53 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.MetaData.Profiles.Icc |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Represents the ICC profile version number.
|
||||
|
/// </summary>
|
||||
|
public readonly struct IccVersion : IEquatable<IccVersion> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="IccVersion"/> struct.
|
||||
|
/// </summary>
|
||||
|
/// <param name="major">The major version number.</param>
|
||||
|
/// <param name="minor">The minor version number.</param>
|
||||
|
/// <param name="patch">The patch version number.</param>
|
||||
|
public IccVersion(int major, int minor, int patch) |
||||
|
{ |
||||
|
this.Major = major; |
||||
|
this.Minor = minor; |
||||
|
this.Patch = patch; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the major version number.
|
||||
|
/// </summary>
|
||||
|
public int Major { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the minor version number.
|
||||
|
/// </summary>
|
||||
|
public int Minor { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the patch number.
|
||||
|
/// </summary>
|
||||
|
public int Patch { get; } |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public bool Equals(IccVersion other) => |
||||
|
this.Major == other.Major && |
||||
|
this.Minor == other.Minor && |
||||
|
this.Patch == other.Patch; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override string ToString() |
||||
|
{ |
||||
|
return string.Join(".", this.Major, this.Minor, this.Patch); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,127 @@ |
|||||
|
using System.Buffers.Binary; |
||||
|
using System.IO; |
||||
|
using System.Text; |
||||
|
|
||||
|
using SixLabors.ImageSharp.Formats.Png; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
using Xunit; |
||||
|
// ReSharper disable InconsistentNaming
|
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Formats.Png |
||||
|
{ |
||||
|
public partial class PngDecoderTests |
||||
|
{ |
||||
|
// Contains the png marker, IHDR and pHYs chunks of a 1x1 pixel 32bit png 1 a single black pixel.
|
||||
|
private static readonly byte[] Raw1X1PngIhdrAndpHYs = |
||||
|
{ |
||||
|
// PNG Identifier
|
||||
|
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, |
||||
|
|
||||
|
// IHDR
|
||||
|
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, |
||||
|
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, |
||||
|
0x00, 0x00, 0x00, |
||||
|
// IHDR CRC
|
||||
|
0x90, 0x77, 0x53, 0xDE, |
||||
|
|
||||
|
// pHYS
|
||||
|
0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, |
||||
|
0x00, 0x0E, 0xC3, 0x00, 0x00, 0x0E, 0xC3, 0x01, |
||||
|
// pHYS CRC
|
||||
|
0xC7, 0x6F, 0xA8, 0x64 |
||||
|
}; |
||||
|
|
||||
|
// Contains the png marker, IDAT and IEND chunks of a 1x1 pixel 32bit png 1 a single black pixel.
|
||||
|
private static readonly byte[] Raw1X1PngIdatAndIend = |
||||
|
{ |
||||
|
// IDAT
|
||||
|
0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x18, |
||||
|
0x57, 0x63, 0x60, 0x60, 0x60, 0x00, 0x00, 0x00, 0x04, |
||||
|
0x00, 0x01, |
||||
|
|
||||
|
// IDAT CRC
|
||||
|
0x5C, 0xCD, 0xFF, 0x69, |
||||
|
|
||||
|
// IEND
|
||||
|
0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, |
||||
|
|
||||
|
// IEND CRC
|
||||
|
0xAE, 0x42, 0x60, 0x82 |
||||
|
}; |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData((uint)PngChunkType.Header)] // IHDR
|
||||
|
[InlineData((uint)PngChunkType.Palette)] // PLTE
|
||||
|
// [InlineData(PngChunkTypes.Data)] //TODO: Figure out how to test this
|
||||
|
[InlineData((uint)PngChunkType.End)] // IEND
|
||||
|
public void Decode_IncorrectCRCForCriticalChunk_ExceptionIsThrown(uint chunkType) |
||||
|
{ |
||||
|
string chunkName = GetChunkTypeName(chunkType); |
||||
|
|
||||
|
using (var memStream = new MemoryStream()) |
||||
|
{ |
||||
|
WriteHeaderChunk(memStream); |
||||
|
WriteChunk(memStream, chunkName); |
||||
|
WriteDataChunk(memStream); |
||||
|
|
||||
|
var decoder = new PngDecoder(); |
||||
|
|
||||
|
ImageFormatException exception = |
||||
|
Assert.Throws<ImageFormatException>(() => decoder.Decode<Rgb24>(null, memStream)); |
||||
|
|
||||
|
Assert.Equal($"CRC Error. PNG {chunkName} chunk is corrupt!", exception.Message); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData((uint)PngChunkType.Gamma)] // gAMA
|
||||
|
[InlineData((uint)PngChunkType.PaletteAlpha)] // tRNS
|
||||
|
[InlineData( |
||||
|
(uint)PngChunkType.Physical)] // pHYs: It's ok to test physical as we don't throw for duplicate chunks.
|
||||
|
//[InlineData(PngChunkTypes.Text)] //TODO: Figure out how to test this
|
||||
|
public void Decode_IncorrectCRCForNonCriticalChunk_ExceptionIsThrown(uint chunkType) |
||||
|
{ |
||||
|
string chunkName = GetChunkTypeName(chunkType); |
||||
|
|
||||
|
using (var memStream = new MemoryStream()) |
||||
|
{ |
||||
|
WriteHeaderChunk(memStream); |
||||
|
WriteChunk(memStream, chunkName); |
||||
|
WriteDataChunk(memStream); |
||||
|
|
||||
|
var decoder = new PngDecoder(); |
||||
|
decoder.Decode<Rgb24>(null, memStream); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static string GetChunkTypeName(uint value) |
||||
|
{ |
||||
|
byte[] data = new byte[4]; |
||||
|
|
||||
|
BinaryPrimitives.WriteUInt32BigEndian(data, value); |
||||
|
|
||||
|
return Encoding.ASCII.GetString(data); |
||||
|
} |
||||
|
|
||||
|
private static void WriteHeaderChunk(MemoryStream memStream) |
||||
|
{ |
||||
|
// Writes a 1x1 32bit png header chunk containing a single black pixel
|
||||
|
memStream.Write(Raw1X1PngIhdrAndpHYs, 0, Raw1X1PngIhdrAndpHYs.Length); |
||||
|
} |
||||
|
|
||||
|
private static void WriteChunk(MemoryStream memStream, string chunkName) |
||||
|
{ |
||||
|
memStream.Write(new byte[] { 0, 0, 0, 1 }, 0, 4); |
||||
|
memStream.Write(Encoding.GetEncoding("ASCII").GetBytes(chunkName), 0, 4); |
||||
|
memStream.Write(new byte[] { 0, 0, 0, 0, 0 }, 0, 5); |
||||
|
} |
||||
|
|
||||
|
private static void WriteDataChunk(MemoryStream memStream) |
||||
|
{ |
||||
|
// Writes a 1x1 32bit png data chunk containing a single black pixel
|
||||
|
memStream.Write(Raw1X1PngIdatAndIend, 0, Raw1X1PngIdatAndIend.Length); |
||||
|
memStream.Position = 0; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,53 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.IO; |
||||
|
using System.Runtime.InteropServices; |
||||
|
|
||||
|
using ImageMagick; |
||||
|
|
||||
|
using SixLabors.ImageSharp.Advanced; |
||||
|
using SixLabors.ImageSharp.Formats; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs |
||||
|
{ |
||||
|
public class MagickReferenceDecoder : IImageDecoder |
||||
|
{ |
||||
|
public static MagickReferenceDecoder Instance { get; } = new MagickReferenceDecoder(); |
||||
|
|
||||
|
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
using (var magickImage = new MagickImage(stream)) |
||||
|
{ |
||||
|
var result = new Image<TPixel>(configuration, magickImage.Width, magickImage.Height); |
||||
|
Span<TPixel> resultPixels = result.GetPixelSpan(); |
||||
|
|
||||
|
using (IPixelCollection pixels = magickImage.GetPixelsUnsafe()) |
||||
|
{ |
||||
|
if (magickImage.Depth == 8) |
||||
|
{ |
||||
|
byte[] data = pixels.ToByteArray("RGBA"); |
||||
|
|
||||
|
PixelOperations<TPixel>.Instance.PackFromRgba32Bytes(data, resultPixels, resultPixels.Length); |
||||
|
} |
||||
|
else if (magickImage.Depth == 16) |
||||
|
{ |
||||
|
ushort[] data = pixels.ToShortArray("RGBA"); |
||||
|
Span<byte> bytes = MemoryMarshal.Cast<ushort, byte>(data.AsSpan()); |
||||
|
|
||||
|
PixelOperations<TPixel>.Instance.PackFromRgba64Bytes(bytes, resultPixels, resultPixels.Length); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
throw new InvalidOperationException(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,89 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using Xunit; |
||||
|
// ReSharper disable InconsistentNaming
|
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests |
||||
|
{ |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; |
||||
|
|
||||
|
using Xunit.Abstractions; |
||||
|
|
||||
|
public class MagickReferenceCodecTests |
||||
|
{ |
||||
|
public MagickReferenceCodecTests(ITestOutputHelper output) |
||||
|
{ |
||||
|
this.Output = output; |
||||
|
} |
||||
|
|
||||
|
private ITestOutputHelper Output { get; } |
||||
|
|
||||
|
public const PixelTypes PixelTypesToTest32 = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.Rgb24; |
||||
|
|
||||
|
public const PixelTypes PixelTypesToTest64 = |
||||
|
PixelTypes.Rgba32 | PixelTypes.Rgb24 | PixelTypes.Rgba64 | PixelTypes.Rgb48; |
||||
|
|
||||
|
public const PixelTypes PixelTypesToTest48 = |
||||
|
PixelTypes.Rgba32 | PixelTypes.Rgba64 | PixelTypes.Rgb48; |
||||
|
|
||||
|
[Theory] |
||||
|
[WithBlankImages(1, 1, PixelTypesToTest32, TestImages.Png.Splash)] |
||||
|
[WithBlankImages(1, 1, PixelTypesToTest32, TestImages.Png.Indexed)] |
||||
|
public void MagickDecode_8BitDepthImage_IsEquivalentTo_SystemDrawingResult<TPixel>(TestImageProvider<TPixel> dummyProvider, string testImage) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
string path = TestFile.GetInputFileFullPath(testImage); |
||||
|
|
||||
|
var magickDecoder = new MagickReferenceDecoder(); |
||||
|
var sdDecoder = new SystemDrawingReferenceDecoder(); |
||||
|
|
||||
|
ImageComparer comparer = ImageComparer.Exact; |
||||
|
|
||||
|
using (var mImage = Image.Load<TPixel>(path, magickDecoder)) |
||||
|
using (var sdImage = Image.Load<TPixel>(path, sdDecoder)) |
||||
|
{ |
||||
|
ImageSimilarityReport<TPixel, TPixel> report = comparer.CompareImagesOrFrames(mImage, sdImage); |
||||
|
|
||||
|
mImage.DebugSave(dummyProvider); |
||||
|
|
||||
|
if (TestEnvironment.IsWindows) |
||||
|
{ |
||||
|
Assert.True(report.IsEmpty); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[WithBlankImages(1, 1, PixelTypesToTest64, TestImages.Png.Rgba64Bpp)] |
||||
|
[WithBlankImages(1, 1, PixelTypesToTest48, TestImages.Png.Rgb48Bpp)] |
||||
|
[WithBlankImages(1, 1, PixelTypesToTest48, TestImages.Png.Rgb48BppInterlaced)] |
||||
|
[WithBlankImages(1, 1, PixelTypesToTest48, TestImages.Png.Rgb48BppTrans)] |
||||
|
public void MagickDecode_16BitDepthImage_IsApproximatelyEquivalentTo_SystemDrawingResult<TPixel>(TestImageProvider<TPixel> dummyProvider, string testImage) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
string path = TestFile.GetInputFileFullPath(testImage); |
||||
|
|
||||
|
var magickDecoder = new MagickReferenceDecoder(); |
||||
|
var sdDecoder = new SystemDrawingReferenceDecoder(); |
||||
|
|
||||
|
// 1020 == 4 * 255 (Equivalent to manhattan distance of 1+1+1+1=4 in Rgba32 space)
|
||||
|
var comparer = ImageComparer.TolerantPercentage(1, 1020); |
||||
|
|
||||
|
using (var mImage = Image.Load<TPixel>(path, magickDecoder)) |
||||
|
using (var sdImage = Image.Load<TPixel>(path, sdDecoder)) |
||||
|
{ |
||||
|
ImageSimilarityReport<TPixel, TPixel> report = comparer.CompareImagesOrFrames(mImage, sdImage); |
||||
|
|
||||
|
mImage.DebugSave(dummyProvider); |
||||
|
|
||||
|
if (TestEnvironment.IsWindows) |
||||
|
{ |
||||
|
Assert.True(report.IsEmpty); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,96 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
using SixLabors.ImageSharp.Formats; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; |
||||
|
|
||||
|
using Xunit; |
||||
|
using Xunit.Abstractions; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests |
||||
|
{ |
||||
|
public class ReferenceDecoderBenchmarks |
||||
|
{ |
||||
|
private ITestOutputHelper Output { get; } |
||||
|
|
||||
|
public const string SkipBenchmarks = |
||||
|
#if false
|
||||
|
"Benchmark, enable manually!"; |
||||
|
#else
|
||||
|
null; |
||||
|
#endif
|
||||
|
|
||||
|
public const int DefaultExecutionCount = 50; |
||||
|
|
||||
|
public static readonly string[] PngBenchmarkFiles = |
||||
|
{ |
||||
|
TestImages.Png.CalliphoraPartial, |
||||
|
TestImages.Png.Kaboom, |
||||
|
TestImages.Png.Bike, |
||||
|
TestImages.Png.Splash, |
||||
|
TestImages.Png.SplashInterlaced |
||||
|
}; |
||||
|
|
||||
|
public static readonly string[] BmpBenchmarkFiles = |
||||
|
{ |
||||
|
TestImages.Bmp.NegHeight, |
||||
|
TestImages.Bmp.Car, |
||||
|
TestImages.Bmp.V5Header |
||||
|
}; |
||||
|
|
||||
|
public ReferenceDecoderBenchmarks(ITestOutputHelper output) |
||||
|
{ |
||||
|
this.Output = output; |
||||
|
} |
||||
|
|
||||
|
[Theory(Skip = SkipBenchmarks)] |
||||
|
[WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32)] |
||||
|
public void BenchmarkMagickPngDecoder<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
this.BenckmarkDecoderImpl(PngBenchmarkFiles, new MagickReferenceDecoder(), $@"Magick Decode Png"); |
||||
|
} |
||||
|
|
||||
|
[Theory(Skip = SkipBenchmarks)] |
||||
|
[WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32)] |
||||
|
public void BenchmarkSystemDrawingPngDecoder<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
this.BenckmarkDecoderImpl(PngBenchmarkFiles, new SystemDrawingReferenceDecoder(), $@"System.Drawing Decode Png"); |
||||
|
} |
||||
|
|
||||
|
[Theory(Skip = SkipBenchmarks)] |
||||
|
[WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32)] |
||||
|
public void BenchmarkMagickBmpDecoder<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
this.BenckmarkDecoderImpl(BmpBenchmarkFiles, new MagickReferenceDecoder(), $@"Magick Decode Bmp"); |
||||
|
} |
||||
|
|
||||
|
[Theory(Skip = SkipBenchmarks)] |
||||
|
[WithFile(TestImages.Png.Kaboom, PixelTypes.Rgba32)] |
||||
|
public void BenchmarkSystemDrawingBmpDecoder<TPixel>(TestImageProvider<TPixel> provider) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
this.BenckmarkDecoderImpl(BmpBenchmarkFiles, new SystemDrawingReferenceDecoder(), $@"System.Drawing Decode Bmp"); |
||||
|
} |
||||
|
|
||||
|
private void BenckmarkDecoderImpl(IEnumerable<string> testFiles, IImageDecoder decoder, string info, int times = DefaultExecutionCount) |
||||
|
{ |
||||
|
var measure = new MeasureFixture(this.Output); |
||||
|
measure.Measure(times, |
||||
|
() => |
||||
|
{ |
||||
|
foreach (string testFile in testFiles) |
||||
|
{ |
||||
|
Image<Rgba32> image = TestFile.Create(testFile).CreateImage(decoder); |
||||
|
image.Dispose(); |
||||
|
} |
||||
|
}, |
||||
|
info); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue