Browse Source

Use using declarations to reduce nesting

pull/1090/head
Brian Popow 6 years ago
parent
commit
38454f8fde
  1. 137
      tests/ImageSharp.Tests/Advanced/AdvancedImageExtensionsTests.cs
  2. 42
      tests/ImageSharp.Tests/Common/StreamExtensionsTests.cs
  3. 192
      tests/ImageSharp.Tests/Drawing/DrawImageTests.cs
  4. 346
      tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
  5. 113
      tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs
  6. 14
      tests/ImageSharp.Tests/Formats/Bmp/BmpMetadataTests.cs
  7. 119
      tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs
  8. 74
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
  9. 162
      tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs
  10. 88
      tests/ImageSharp.Tests/Formats/Gif/GifMetadataTests.cs
  11. 12
      tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs
  12. 48
      tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs
  13. 80
      tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs
  14. 16
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs
  15. 88
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs
  16. 16
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Progressive.cs
  17. 34
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  18. 107
      tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs
  19. 58
      tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs
  20. 60
      tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs
  21. 20
      tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs
  22. 10
      tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs
  23. 46
      tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.cs
  24. 8
      tests/ImageSharp.Tests/Formats/Jpg/Utils/VerifyJpeg.cs
  25. 18
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.Chunks.cs
  26. 102
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
  27. 224
      tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs
  28. 185
      tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs
  29. 44
      tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs
  30. 120
      tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs
  31. 66
      tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs
  32. 28
      tests/ImageSharp.Tests/Formats/Tga/TgaTestUtils.cs
  33. 60
      tests/ImageSharp.Tests/Helpers/ParallelHelperTests.cs
  34. 18
      tests/ImageSharp.Tests/Helpers/RowIntervalTests.cs
  35. 190
      tests/ImageSharp.Tests/IO/DoubleBufferedStreamReaderTests.cs
  36. 116
      tests/ImageSharp.Tests/Image/ImageCloneTests.cs
  37. 30
      tests/ImageSharp.Tests/Image/ImageFrameCollectionTests.Generic.cs
  38. 81
      tests/ImageSharp.Tests/Image/ImageFrameCollectionTests.NonGeneric.cs
  39. 12
      tests/ImageSharp.Tests/Image/ImageRotationTests.cs
  40. 8
      tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs
  41. 40
      tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs
  42. 40
      tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs
  43. 40
      tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs
  44. 40
      tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_UseDefaultConfiguration.cs
  45. 28
      tests/ImageSharp.Tests/Image/ImageTests.Save.cs
  46. 88
      tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs
  47. 56
      tests/ImageSharp.Tests/Image/ImageTests.cs
  48. 106
      tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
  49. 112
      tests/ImageSharp.Tests/Memory/BufferAreaTests.cs
  50. 16
      tests/ImageSharp.Tests/Metadata/ImageMetadataTests.cs
  51. 70
      tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs
  52. 6
      tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifValueTests.cs
  53. 32
      tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffCompositorTests.cs
  54. 22
      tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.Rgba32OperationsTests.cs
  55. 8
      tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs
  56. 114
      tests/ImageSharp.Tests/Processing/Normalization/HistogramEqualizationTests.cs
  57. 60
      tests/ImageSharp.Tests/Processing/Processors/Binarization/BinaryDitherTests.cs
  58. 22
      tests/ImageSharp.Tests/Processing/Processors/Binarization/BinaryThresholdTest.cs
  59. 26
      tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs
  60. 40
      tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs
  61. 28
      tests/ImageSharp.Tests/Processing/Processors/Transforms/AutoOrientTests.cs
  62. 36
      tests/ImageSharp.Tests/Processing/Processors/Transforms/PadTest.cs
  63. 365
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
  64. 12
      tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateFlipTests.cs
  65. 102
      tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs
  66. 68
      tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs
  67. 22
      tests/ImageSharp.Tests/Processing/Transforms/TransformsHelpersTest.cs
  68. 28
      tests/ImageSharp.Tests/ProfilingBenchmarks/LoadResizeSaveProfilingBenchmarks.cs
  69. 10
      tests/ImageSharp.Tests/ProfilingBenchmarks/ResizeProfilingBenchmarks.cs
  70. 40
      tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs
  71. 176
      tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs
  72. 0
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/ITestImageProvider.cs
  73. 12
      tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs
  74. 64
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs
  75. 60
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs
  76. 48
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs
  77. 8
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs
  78. 62
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
  79. 104
      tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs
  80. 32
      tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs
  81. 78
      tests/ImageSharp.Tests/TestUtilities/Tests/SystemDrawingReferenceCodecTests.cs
  82. 60
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs
  83. 50
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

137
tests/ImageSharp.Tests/Advanced/AdvancedImageExtensionsTests.cs

@ -20,23 +20,20 @@ namespace SixLabors.ImageSharp.Tests.Advanced
public void WhenMemoryIsOwned<TPixel>(TestImageProvider<TPixel> provider) public void WhenMemoryIsOwned<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image0 = provider.GetImage()) using Image<TPixel> image0 = provider.GetImage();
{ var targetBuffer = new TPixel[image0.Width * image0.Height];
var targetBuffer = new TPixel[image0.Width * image0.Height];
// Act:
// Act: Memory<TPixel> memory = image0.GetPixelMemory();
Memory<TPixel> memory = image0.GetPixelMemory();
// Assert:
// Assert: Assert.Equal(image0.Width * image0.Height, memory.Length);
Assert.Equal(image0.Width * image0.Height, memory.Length); memory.Span.CopyTo(targetBuffer);
memory.Span.CopyTo(targetBuffer);
using Image<TPixel> image1 = provider.GetImage();
using (Image<TPixel> image1 = provider.GetImage())
{ // We are using a copy of the original image for assertion
// We are using a copy of the original image for assertion image1.ComparePixelBufferTo(targetBuffer);
image1.ComparePixelBufferTo(targetBuffer);
}
}
} }
[Theory] [Theory]
@ -45,27 +42,23 @@ namespace SixLabors.ImageSharp.Tests.Advanced
public void WhenMemoryIsConsumed<TPixel>(TestImageProvider<TPixel> provider) public void WhenMemoryIsConsumed<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image0 = provider.GetImage()) using Image<TPixel> image0 = provider.GetImage();
{ var targetBuffer = new TPixel[image0.Width * image0.Height];
var targetBuffer = new TPixel[image0.Width * image0.Height]; image0.GetPixelSpan().CopyTo(targetBuffer);
image0.GetPixelSpan().CopyTo(targetBuffer);
var managerOfExternalMemory = new TestMemoryManager<TPixel>(targetBuffer); var managerOfExternalMemory = new TestMemoryManager<TPixel>(targetBuffer);
Memory<TPixel> externalMemory = managerOfExternalMemory.Memory; Memory<TPixel> externalMemory = managerOfExternalMemory.Memory;
using (var image1 = Image.WrapMemory(externalMemory, image0.Width, image0.Height)) using var image1 = Image.WrapMemory(externalMemory, image0.Width, image0.Height);
{ Memory<TPixel> internalMemory = image1.GetPixelMemory();
Memory<TPixel> internalMemory = image1.GetPixelMemory(); Assert.Equal(targetBuffer.Length, internalMemory.Length);
Assert.Equal(targetBuffer.Length, internalMemory.Length); Assert.True(Unsafe.AreSame(ref targetBuffer[0], ref internalMemory.Span[0]));
Assert.True(Unsafe.AreSame(ref targetBuffer[0], ref internalMemory.Span[0]));
image0.ComparePixelBufferTo(internalMemory.Span); image0.ComparePixelBufferTo(internalMemory.Span);
}
// Make sure externalMemory works after destruction: // Make sure externalMemory works after destruction:
image0.ComparePixelBufferTo(externalMemory.Span); image0.ComparePixelBufferTo(externalMemory.Span);
}
} }
} }
@ -75,24 +68,21 @@ namespace SixLabors.ImageSharp.Tests.Advanced
public void GetPixelRowMemory<TPixel>(TestImageProvider<TPixel> provider) public void GetPixelRowMemory<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var targetBuffer = new TPixel[image.Width * image.Height];
// Act:
for (int y = 0; y < image.Height; y++)
{ {
var targetBuffer = new TPixel[image.Width * image.Height]; Memory<TPixel> rowMemory = image.GetPixelRowMemory(y);
rowMemory.Span.CopyTo(targetBuffer.AsSpan(image.Width * y));
}
// Act: // Assert:
for (int y = 0; y < image.Height; y++) using Image<TPixel> image1 = provider.GetImage();
{
Memory<TPixel> rowMemory = image.GetPixelRowMemory(y);
rowMemory.Span.CopyTo(targetBuffer.AsSpan(image.Width * y));
}
// Assert: // We are using a copy of the original image for assertion
using (Image<TPixel> image1 = provider.GetImage()) image1.ComparePixelBufferTo(targetBuffer);
{
// We are using a copy of the original image for assertion
image1.ComparePixelBufferTo(targetBuffer);
}
}
} }
[Theory] [Theory]
@ -101,24 +91,21 @@ namespace SixLabors.ImageSharp.Tests.Advanced
public void GetPixelRowSpan<TPixel>(TestImageProvider<TPixel> provider) public void GetPixelRowSpan<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var targetBuffer = new TPixel[image.Width * image.Height];
// Act:
for (int y = 0; y < image.Height; y++)
{ {
var targetBuffer = new TPixel[image.Width * image.Height]; Span<TPixel> rowMemory = image.GetPixelRowSpan(y);
rowMemory.CopyTo(targetBuffer.AsSpan(image.Width * y));
}
// Act: // Assert:
for (int y = 0; y < image.Height; y++) using Image<TPixel> image1 = provider.GetImage();
{
Span<TPixel> rowMemory = image.GetPixelRowSpan(y);
rowMemory.CopyTo(targetBuffer.AsSpan(image.Width * y));
}
// Assert: // We are using a copy of the original image for assertion
using (Image<TPixel> image1 = provider.GetImage()) image1.ComparePixelBufferTo(targetBuffer);
{
// We are using a copy of the original image for assertion
image1.ComparePixelBufferTo(targetBuffer);
}
}
} }
#pragma warning disable 0618 #pragma warning disable 0618
@ -128,21 +115,19 @@ namespace SixLabors.ImageSharp.Tests.Advanced
public unsafe void DangerousGetPinnableReference_CopyToBuffer<TPixel>(TestImageProvider<TPixel> provider) public unsafe void DangerousGetPinnableReference_CopyToBuffer<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var targetBuffer = new TPixel[image.Width * image.Height];
ref byte source = ref Unsafe.As<TPixel, byte>(ref targetBuffer[0]);
ref byte dest = ref Unsafe.As<TPixel, byte>(ref image.DangerousGetPinnableReferenceToPixelBuffer());
fixed (byte* targetPtr = &source)
fixed (byte* pixelBasePtr = &dest)
{ {
var targetBuffer = new TPixel[image.Width * image.Height]; uint dataSizeInBytes = (uint)(image.Width * image.Height * Unsafe.SizeOf<TPixel>());
Unsafe.CopyBlock(targetPtr, pixelBasePtr, dataSizeInBytes);
ref byte source = ref Unsafe.As<TPixel, byte>(ref targetBuffer[0]);
ref byte dest = ref Unsafe.As<TPixel, byte>(ref image.DangerousGetPinnableReferenceToPixelBuffer());
fixed (byte* targetPtr = &source)
fixed (byte* pixelBasePtr = &dest)
{
uint dataSizeInBytes = (uint)(image.Width * image.Height * Unsafe.SizeOf<TPixel>());
Unsafe.CopyBlock(targetPtr, pixelBasePtr, dataSizeInBytes);
}
image.ComparePixelBufferTo(targetBuffer);
} }
image.ComparePixelBufferTo(targetBuffer);
} }
} }
} }

42
tests/ImageSharp.Tests/Common/StreamExtensionsTests.cs

@ -15,51 +15,43 @@ namespace SixLabors.ImageSharp.Tests.Common
[InlineData(-1)] [InlineData(-1)]
public void Skip_CountZeroOrLower_PositionNotChanged(int count) public void Skip_CountZeroOrLower_PositionNotChanged(int count)
{ {
using (var memStream = new MemoryStream(5)) using var memStream = new MemoryStream(5);
{ memStream.Position = 4;
memStream.Position = 4; memStream.Skip(count);
memStream.Skip(count);
Assert.Equal(4, memStream.Position); Assert.Equal(4, memStream.Position);
}
} }
[Fact] [Fact]
public void Skip_SeekableStream_SeekIsCalled() public void Skip_SeekableStream_SeekIsCalled()
{ {
using (var seekableStream = new SeekableStream(4)) using var seekableStream = new SeekableStream(4);
{ seekableStream.Skip(4);
seekableStream.Skip(4);
Assert.Equal(4, seekableStream.Offset); Assert.Equal(4, seekableStream.Offset);
Assert.Equal(SeekOrigin.Current, seekableStream.Loc); Assert.Equal(SeekOrigin.Current, seekableStream.Loc);
}
} }
[Fact] [Fact]
public void Skip_NonSeekableStream_BytesAreRead() public void Skip_NonSeekableStream_BytesAreRead()
{ {
using (var nonSeekableStream = new NonSeekableStream()) using var nonSeekableStream = new NonSeekableStream();
{ nonSeekableStream.Skip(5);
nonSeekableStream.Skip(5);
Assert.Equal(3, nonSeekableStream.Counts.Count); Assert.Equal(3, nonSeekableStream.Counts.Count);
Assert.Equal(5, nonSeekableStream.Counts[0]); Assert.Equal(5, nonSeekableStream.Counts[0]);
Assert.Equal(3, nonSeekableStream.Counts[1]); Assert.Equal(3, nonSeekableStream.Counts[1]);
Assert.Equal(1, nonSeekableStream.Counts[2]); Assert.Equal(1, nonSeekableStream.Counts[2]);
}
} }
[Fact] [Fact]
public void Skip_EofStream_NoExceptionIsThrown() public void Skip_EofStream_NoExceptionIsThrown()
{ {
using (var eofStream = new EofStream(7)) using var eofStream = new EofStream(7);
{ eofStream.Skip(7);
eofStream.Skip(7);
Assert.Equal(0, eofStream.Position); Assert.Equal(0, eofStream.Position);
}
} }
private class SeekableStream : MemoryStream private class SeekableStream : MemoryStream

192
tests/ImageSharp.Tests/Drawing/DrawImageTests.cs

@ -34,24 +34,22 @@ namespace SixLabors.ImageSharp.Tests.Drawing
public void ImageBlendingMatchesSvgSpecExamples<TPixel>(TestImageProvider<TPixel> provider, PixelColorBlendingMode mode) public void ImageBlendingMatchesSvgSpecExamples<TPixel>(TestImageProvider<TPixel> provider, PixelColorBlendingMode mode)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> background = provider.GetImage()) using Image<TPixel> background = provider.GetImage();
using (var source = Image.Load<TPixel>(TestFile.Create(TestImages.Png.Ducky).Bytes)) using var source = Image.Load<TPixel>(TestFile.Create(TestImages.Png.Ducky).Bytes);
{ background.Mutate(x => x.DrawImage(source, mode, 1F));
background.Mutate(x => x.DrawImage(source, mode, 1F)); background.DebugSave(
background.DebugSave( provider,
provider, new { mode = mode },
new { mode = mode }, appendPixelTypeToFileName: false,
appendPixelTypeToFileName: false, appendSourceFileOrDescription: false);
appendSourceFileOrDescription: false);
var comparer = ImageComparer.TolerantPercentage(0.01F);
var comparer = ImageComparer.TolerantPercentage(0.01F); background.CompareToReferenceOutput(
background.CompareToReferenceOutput( comparer,
comparer, provider,
provider, new { mode = mode },
new { mode = mode }, appendPixelTypeToFileName: false,
appendPixelTypeToFileName: false, appendSourceFileOrDescription: false);
appendSourceFileOrDescription: false);
}
} }
[Theory] [Theory]
@ -73,28 +71,26 @@ namespace SixLabors.ImageSharp.Tests.Drawing
float opacity) float opacity)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
using (var blend = Image.Load<TPixel>(TestFile.Create(brushImage).Bytes)) using var blend = Image.Load<TPixel>(TestFile.Create(brushImage).Bytes);
var size = new Size(image.Width * 3 / 4, image.Height * 3 / 4);
var position = new Point(image.Width / 8, image.Height / 8);
blend.Mutate(x => x.Resize(size.Width, size.Height, KnownResamplers.Bicubic));
image.Mutate(x => x.DrawImage(blend, position, mode, opacity));
FormattableString testInfo = $"{System.IO.Path.GetFileNameWithoutExtension(brushImage)}-{mode}-{opacity}";
var encoder = new PngEncoder();
if (provider.PixelType == PixelTypes.Rgba64)
{ {
var size = new Size(image.Width * 3 / 4, image.Height * 3 / 4); encoder.BitDepth = PngBitDepth.Bit16;
var position = new Point(image.Width / 8, image.Height / 8);
blend.Mutate(x => x.Resize(size.Width, size.Height, KnownResamplers.Bicubic));
image.Mutate(x => x.DrawImage(blend, position, mode, opacity));
FormattableString testInfo = $"{System.IO.Path.GetFileNameWithoutExtension(brushImage)}-{mode}-{opacity}";
var encoder = new PngEncoder();
if (provider.PixelType == PixelTypes.Rgba64)
{
encoder.BitDepth = PngBitDepth.Bit16;
}
image.DebugSave(provider, testInfo, encoder: encoder);
image.CompareToReferenceOutput(
ImageComparer.TolerantPercentage(0.01f),
provider,
testInfo);
} }
image.DebugSave(provider, testInfo, encoder: encoder);
image.CompareToReferenceOutput(
ImageComparer.TolerantPercentage(0.01f),
provider,
testInfo);
} }
[Theory] [Theory]
@ -104,19 +100,17 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{ {
byte[] brushData = TestFile.Create(TestImages.Png.Ducky).Bytes; byte[] brushData = TestFile.Create(TestImages.Png.Ducky).Bytes;
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
using (Image brushImage = provider.PixelType == PixelTypes.Rgba32 using Image brushImage = provider.PixelType == PixelTypes.Rgba32
? (Image)Image.Load<Bgra32>(brushData) ? (Image)Image.Load<Bgra32>(brushData)
: Image.Load<Rgba32>(brushData)) : Image.Load<Rgba32>(brushData);
{ image.Mutate(c => c.DrawImage(brushImage, 0.5f));
image.Mutate(c => c.DrawImage(brushImage, 0.5f));
image.DebugSave(provider, appendSourceFileOrDescription: false);
image.DebugSave(provider, appendSourceFileOrDescription: false); image.CompareToReferenceOutput(
image.CompareToReferenceOutput( ImageComparer.TolerantPercentage(0.01f),
ImageComparer.TolerantPercentage(0.01f), provider,
provider, appendSourceFileOrDescription: false);
appendSourceFileOrDescription: false);
}
} }
[Theory] [Theory]
@ -126,25 +120,23 @@ namespace SixLabors.ImageSharp.Tests.Drawing
[WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, -25, -30)] [WithSolidFilledImages(100, 100, "White", PixelTypes.Rgba32, -25, -30)]
public void WorksWithDifferentLocations(TestImageProvider<Rgba32> provider, int x, int y) public void WorksWithDifferentLocations(TestImageProvider<Rgba32> provider, int x, int y)
{ {
using (Image<Rgba32> background = provider.GetImage()) using Image<Rgba32> background = provider.GetImage();
using (var overlay = new Image<Rgba32>(50, 50)) using var overlay = new Image<Rgba32>(50, 50);
{ overlay.GetPixelSpan().Fill(Rgba32.Black);
overlay.GetPixelSpan().Fill(Rgba32.Black);
background.Mutate(c => c.DrawImage(overlay, new Point(x, y), PixelColorBlendingMode.Normal, 1F));
background.Mutate(c => c.DrawImage(overlay, new Point(x, y), PixelColorBlendingMode.Normal, 1F));
background.DebugSave(
background.DebugSave( provider,
provider, testOutputDetails: $"{x}_{y}",
testOutputDetails: $"{x}_{y}", appendPixelTypeToFileName: false,
appendPixelTypeToFileName: false, appendSourceFileOrDescription: false);
appendSourceFileOrDescription: false);
background.CompareToReferenceOutput(
background.CompareToReferenceOutput( provider,
provider, testOutputDetails: $"{x}_{y}",
testOutputDetails: $"{x}_{y}", appendPixelTypeToFileName: false,
appendPixelTypeToFileName: false, appendSourceFileOrDescription: false);
appendSourceFileOrDescription: false);
}
} }
[Theory] [Theory]
@ -152,29 +144,27 @@ namespace SixLabors.ImageSharp.Tests.Drawing
public void DrawTransformed<TPixel>(TestImageProvider<TPixel> provider) public void DrawTransformed<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
using (var blend = Image.Load<TPixel>(TestFile.Create(TestImages.Bmp.Car).Bytes)) using var blend = Image.Load<TPixel>(TestFile.Create(TestImages.Bmp.Car).Bytes);
{ AffineTransformBuilder builder = new AffineTransformBuilder()
AffineTransformBuilder builder = new AffineTransformBuilder() .AppendRotationDegrees(45F)
.AppendRotationDegrees(45F) .AppendScale(new SizeF(.25F, .25F))
.AppendScale(new SizeF(.25F, .25F)) .AppendTranslation(new PointF(10, 10));
.AppendTranslation(new PointF(10, 10));
// Apply a background color so we can see the translation.
// Apply a background color so we can see the translation. blend.Mutate(x => x.Transform(builder));
blend.Mutate(x => x.Transform(builder)); blend.Mutate(x => x.BackgroundColor(Color.HotPink));
blend.Mutate(x => x.BackgroundColor(Color.HotPink));
// Lets center the matrix so we can tell whether any cut-off issues we may have belong to the drawing processor
// Lets center the matrix so we can tell whether any cut-off issues we may have belong to the drawing processor var position = new Point((image.Width - blend.Width) / 2, (image.Height - blend.Height) / 2);
var position = new Point((image.Width - blend.Width) / 2, (image.Height - blend.Height) / 2); image.Mutate(x => x.DrawImage(blend, position, .75F));
image.Mutate(x => x.DrawImage(blend, position, .75F));
image.DebugSave(provider, appendSourceFileOrDescription: false, appendPixelTypeToFileName: false);
image.DebugSave(provider, appendSourceFileOrDescription: false, appendPixelTypeToFileName: false); image.CompareToReferenceOutput(
image.CompareToReferenceOutput( ImageComparer.TolerantPercentage(0.002f),
ImageComparer.TolerantPercentage(0.002f), provider,
provider, appendSourceFileOrDescription: false,
appendSourceFileOrDescription: false, appendPixelTypeToFileName: false);
appendPixelTypeToFileName: false);
}
} }
[Theory] [Theory]
@ -184,17 +174,15 @@ namespace SixLabors.ImageSharp.Tests.Drawing
[WithSolidFilledImages(100, 100, 255, 255, 255, PixelTypes.Rgba32, -30, 130)] [WithSolidFilledImages(100, 100, 255, 255, 255, PixelTypes.Rgba32, -30, 130)]
public void NonOverlappingImageThrows(TestImageProvider<Rgba32> provider, int x, int y) public void NonOverlappingImageThrows(TestImageProvider<Rgba32> provider, int x, int y)
{ {
using (Image<Rgba32> background = provider.GetImage()) using Image<Rgba32> background = provider.GetImage();
using (var overlay = new Image<Rgba32>(Configuration.Default, 10, 10, Rgba32.Black)) using var overlay = new Image<Rgba32>(Configuration.Default, 10, 10, Rgba32.Black);
{ ImageProcessingException ex = Assert.Throws<ImageProcessingException>(Test);
ImageProcessingException ex = Assert.Throws<ImageProcessingException>(Test);
Assert.Contains("does not overlap", ex.ToString()); Assert.Contains("does not overlap", ex.ToString());
void Test() void Test()
{ {
background.Mutate(context => context.DrawImage(overlay, new Point(x, y), new GraphicsOptions())); background.Mutate(context => context.DrawImage(overlay, new Point(x, y), new GraphicsOptions()));
}
} }
} }
} }

346
tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs

@ -37,13 +37,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_MiscellaneousBitmaps<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_MiscellaneousBitmaps<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
image.DebugSave(provider);
if (TestEnvironment.IsWindows)
{ {
image.DebugSave(provider); image.CompareToOriginal(provider);
if (TestEnvironment.IsWindows)
{
image.CompareToOriginal(provider);
}
} }
} }
@ -52,11 +50,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeBitfields<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeBitfields<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -65,11 +61,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_Inverted<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_Inverted<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -78,11 +72,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_1Bit<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_1Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, new SystemDrawingReferenceDecoder());
image.CompareToOriginal(provider, new SystemDrawingReferenceDecoder());
}
} }
[Theory] [Theory]
@ -90,15 +82,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_4Bit<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_4Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider);
// The Magick Reference Decoder can not decode 4-Bit bitmaps, so only execute this on windows. // The Magick Reference Decoder can not decode 4-Bit bitmaps, so only execute this on windows.
if (TestEnvironment.IsWindows) if (TestEnvironment.IsWindows)
{ {
image.CompareToOriginal(provider); image.CompareToOriginal(provider);
}
} }
} }
@ -107,11 +97,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_8Bit<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_8Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -119,11 +107,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_16Bit<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_16Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -131,11 +117,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_32Bit<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_32Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -143,11 +127,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_32BitV4Header_Fast<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_32BitV4Header_Fast<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -157,15 +139,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_RunLengthEncoded_4Bit_WithDelta<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_RunLengthEncoded_4Bit_WithDelta<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.Black })) using Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.Black });
{ image.DebugSave(provider);
image.DebugSave(provider);
// The Magick Reference Decoder can not decode 4-Bit bitmaps, so only execute this on windows. // The Magick Reference Decoder can not decode 4-Bit bitmaps, so only execute this on windows.
if (TestEnvironment.IsWindows) if (TestEnvironment.IsWindows)
{ {
image.CompareToOriginal(provider); image.CompareToOriginal(provider);
}
} }
} }
@ -174,15 +154,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_RunLengthEncoded_4Bit<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_RunLengthEncoded_4Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.Black })) using Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.Black });
{ image.DebugSave(provider);
image.DebugSave(provider);
// The Magick Reference Decoder can not decode 4-Bit bitmaps, so only execute this on windows. // The Magick Reference Decoder can not decode 4-Bit bitmaps, so only execute this on windows.
if (TestEnvironment.IsWindows) if (TestEnvironment.IsWindows)
{ {
image.CompareToOriginal(provider); image.CompareToOriginal(provider);
}
} }
} }
@ -194,13 +172,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_RunLengthEncoded_8Bit_WithDelta_SystemDrawingRefDecoder<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_RunLengthEncoded_8Bit_WithDelta_SystemDrawingRefDecoder<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.Black })) using Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.Black });
image.DebugSave(provider);
if (TestEnvironment.IsWindows)
{ {
image.DebugSave(provider); image.CompareToOriginal(provider, new SystemDrawingReferenceDecoder());
if (TestEnvironment.IsWindows)
{
image.CompareToOriginal(provider, new SystemDrawingReferenceDecoder());
}
} }
} }
@ -210,11 +186,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_RunLengthEncoded_8Bit_WithDelta_MagickRefDecoder<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_RunLengthEncoded_8Bit_WithDelta_MagickRefDecoder<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.FirstColorOfPalette })) using Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.FirstColorOfPalette });
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, new MagickReferenceDecoder());
image.CompareToOriginal(provider, new MagickReferenceDecoder());
}
} }
[Theory] [Theory]
@ -223,11 +197,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_RunLengthEncoded_8Bit<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_RunLengthEncoded_8Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.FirstColorOfPalette })) using Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.FirstColorOfPalette });
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, new MagickReferenceDecoder());
image.CompareToOriginal(provider, new MagickReferenceDecoder());
}
} }
[Theory] [Theory]
@ -237,13 +209,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_RunLengthEncoded_24Bit<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_RunLengthEncoded_24Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.Black })) using Image<TPixel> image = provider.GetImage(new BmpDecoder { RleSkippedPixelHandling = RleSkippedPixelHandling.Black });
{ image.DebugSave(provider);
image.DebugSave(provider);
// TODO: Neither System.Drawing nor MagickReferenceDecoder decode this file. // TODO: Neither System.Drawing nor MagickReferenceDecoder decode this file.
// image.CompareToOriginal(provider); // image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -251,13 +221,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeAlphaBitfields<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeAlphaBitfields<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider);
// TODO: Neither System.Drawing nor MagickReferenceDecoder decode this file. // TODO: Neither System.Drawing nor MagickReferenceDecoder decode this file.
// image.CompareToOriginal(provider); // image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -265,11 +233,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeBitmap_WithAlphaChannel<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeBitmap_WithAlphaChannel<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, new MagickReferenceDecoder());
image.CompareToOriginal(provider, new MagickReferenceDecoder());
}
} }
[Theory] [Theory]
@ -277,17 +243,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeBitfields_WithUnusualBitmasks<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeBitfields_WithUnusualBitmasks<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider);
// Choosing large tolerance of 6.1 here, because for some reason with the MagickReferenceDecoder the alpha channel
// Choosing large tolerance of 6.1 here, because for some reason with the MagickReferenceDecoder the alpha channel // seems to be wrong. This bitmap has an alpha channel of two bits. In many cases this alpha channel has a value of 3,
// seems to be wrong. This bitmap has an alpha channel of two bits. In many cases this alpha channel has a value of 3, // which should be remapped to 255 for RGBA32, but the magick decoder has a value of 191 set.
// which should be remapped to 255 for RGBA32, but the magick decoder has a value of 191 set. // The total difference without the alpha channel is still: 0.0204%
// The total difference without the alpha channel is still: 0.0204% // Exporting the image as PNG with GIMP yields to the same result as the ImageSharp implementation.
// Exporting the image as PNG with GIMP yields to the same result as the ImageSharp implementation. image.CompareToOriginal(provider, ImageComparer.TolerantPercentage(6.1f), new MagickReferenceDecoder());
image.CompareToOriginal(provider, ImageComparer.TolerantPercentage(6.1f), new MagickReferenceDecoder());
}
} }
[Theory] [Theory]
@ -296,11 +260,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeBmpv2<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeBmpv2<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -308,11 +270,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeBmpv3<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeBmpv3<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -320,11 +280,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeLessThanFullPalette<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeLessThanFullPalette<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, new MagickReferenceDecoder());
image.CompareToOriginal(provider, new MagickReferenceDecoder());
}
} }
[Theory] [Theory]
@ -333,13 +291,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeOversizedPalette<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeOversizedPalette<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
image.DebugSave(provider);
if (TestEnvironment.IsWindows)
{ {
image.DebugSave(provider); image.CompareToOriginal(provider);
if (TestEnvironment.IsWindows)
{
image.CompareToOriginal(provider);
}
} }
} }
@ -375,11 +331,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeAdobeBmpv3<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeAdobeBmpv3<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, new MagickReferenceDecoder());
image.CompareToOriginal(provider, new MagickReferenceDecoder());
}
} }
[Theory] [Theory]
@ -387,11 +341,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeAdobeBmpv3_WithAlpha<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeAdobeBmpv3_WithAlpha<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, new MagickReferenceDecoder());
image.CompareToOriginal(provider, new MagickReferenceDecoder());
}
} }
[Theory] [Theory]
@ -399,11 +351,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeBmpv4<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeBmpv4<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -412,11 +362,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecodeBmpv5<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecodeBmpv5<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -424,11 +372,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_RespectsFileHeaderOffset<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_RespectsFileHeaderOffset<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -436,11 +382,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -448,11 +392,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode4BytePerEntryPalette<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode4BytePerEntryPalette<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider);
image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -471,12 +413,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void Identify_DetectsCorrectPixelType(string imagePath, int expectedPixelSize) public void Identify_DetectsCorrectPixelType(string imagePath, int expectedPixelSize)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ IImageInfo imageInfo = Image.Identify(stream);
IImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo);
Assert.NotNull(imageInfo); Assert.Equal(expectedPixelSize, imageInfo.PixelType?.BitsPerPixel);
Assert.Equal(expectedPixelSize, imageInfo.PixelType?.BitsPerPixel);
}
} }
[Theory] [Theory]
@ -491,13 +431,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void Identify_DetectsCorrectWidthAndHeight(string imagePath, int expectedWidth, int expectedHeight) public void Identify_DetectsCorrectWidthAndHeight(string imagePath, int expectedWidth, int expectedHeight)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ IImageInfo imageInfo = Image.Identify(stream);
IImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo);
Assert.NotNull(imageInfo); Assert.Equal(expectedWidth, imageInfo.Width);
Assert.Equal(expectedWidth, imageInfo.Width); Assert.Equal(expectedHeight, imageInfo.Height);
Assert.Equal(expectedHeight, imageInfo.Height);
}
} }
[Theory] [Theory]
@ -505,17 +443,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ var decoder = new BmpDecoder();
var decoder = new BmpDecoder(); using Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream);
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream)) ImageMetadata meta = image.Metadata;
{ Assert.Equal(xResolution, meta.HorizontalResolution);
ImageMetadata meta = image.Metadata; Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(xResolution, meta.HorizontalResolution); Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
} }
[Theory] [Theory]
@ -523,13 +457,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_Os2v2XShortHeader<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_Os2v2XShortHeader<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider);
// TODO: Neither System.Drawing or MagickReferenceDecoder can correctly decode this file. // TODO: Neither System.Drawing or MagickReferenceDecoder can correctly decode this file.
// image.CompareToOriginal(provider); // image.CompareToOriginal(provider);
}
} }
[Theory] [Theory]
@ -537,15 +469,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_Os2v2Header<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_Os2v2Header<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider);
// TODO: System.Drawing can not decode this image. MagickReferenceDecoder can decode it, // TODO: System.Drawing can not decode this image. MagickReferenceDecoder can decode it,
// but i think incorrectly. I have loaded the image with GIMP and exported as PNG. // but i think incorrectly. I have loaded the image with GIMP and exported as PNG.
// The results are the same as the image sharp implementation. // The results are the same as the image sharp implementation.
// image.CompareToOriginal(provider, new MagickReferenceDecoder()); // image.CompareToOriginal(provider, new MagickReferenceDecoder());
}
} }
[Theory] [Theory]
@ -561,13 +491,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void BmpDecoder_CanDecode_Os2BitmapArray<TPixel>(TestImageProvider<TPixel> provider) public void BmpDecoder_CanDecode_Os2BitmapArray<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new BmpDecoder())) using Image<TPixel> image = provider.GetImage(new BmpDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider);
// TODO: Neither System.Drawing or MagickReferenceDecoder can correctly decode this file. // TODO: Neither System.Drawing or MagickReferenceDecoder can correctly decode this file.
// image.CompareToOriginal(provider); // image.CompareToOriginal(provider);
}
} }
} }
} }

113
tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs

@ -54,22 +54,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
var options = new BmpEncoder(); var options = new BmpEncoder();
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{
input.Save(memStream, options); memStream.Position = 0;
using var output = Image.Load<Rgba32>(memStream);
memStream.Position = 0; ImageMetadata meta = output.Metadata;
using (var output = Image.Load<Rgba32>(memStream)) Assert.Equal(xResolution, meta.HorizontalResolution);
{ Assert.Equal(yResolution, meta.VerticalResolution);
ImageMetadata meta = output.Metadata; Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
}
} }
[Theory] [Theory]
@ -79,21 +73,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
var options = new BmpEncoder(); var options = new BmpEncoder();
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{
input.Save(memStream, options); memStream.Position = 0;
using var output = Image.Load<Rgba32>(memStream);
memStream.Position = 0; BmpMetadata meta = output.Metadata.GetBmpMetadata();
using (var output = Image.Load<Rgba32>(memStream))
{ Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel);
BmpMetadata meta = output.Metadata.GetBmpMetadata();
Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel);
}
}
}
} }
[Theory] [Theory]
@ -192,20 +180,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
return; return;
} }
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var encoder = new BmpEncoder
{ {
var encoder = new BmpEncoder BitsPerPixel = BmpBitsPerPixel.Pixel8,
{ Quantizer = new WuQuantizer(256)
BitsPerPixel = BmpBitsPerPixel.Pixel8, };
Quantizer = new WuQuantizer(256) string actualOutputFile = provider.Utility.SaveTestOutputFile(image, "bmp", encoder, appendPixelTypeToFileName: false);
}; IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile);
string actualOutputFile = provider.Utility.SaveTestOutputFile(image, "bmp", encoder, appendPixelTypeToFileName: false); using var referenceImage = Image.Load<TPixel>(actualOutputFile, referenceDecoder);
IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile); referenceImage.CompareToReferenceOutput(ImageComparer.TolerantPercentage(0.01f), provider, extension: "bmp", appendPixelTypeToFileName: false);
using (var referenceImage = Image.Load<TPixel>(actualOutputFile, referenceDecoder))
{
referenceImage.CompareToReferenceOutput(ImageComparer.TolerantPercentage(0.01f), provider, extension: "bmp", appendPixelTypeToFileName: false);
}
}
} }
[Theory] [Theory]
@ -218,20 +202,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
return; return;
} }
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var encoder = new BmpEncoder
{ {
var encoder = new BmpEncoder BitsPerPixel = BmpBitsPerPixel.Pixel8,
{ Quantizer = new OctreeQuantizer(256)
BitsPerPixel = BmpBitsPerPixel.Pixel8, };
Quantizer = new OctreeQuantizer(256) string actualOutputFile = provider.Utility.SaveTestOutputFile(image, "bmp", encoder, appendPixelTypeToFileName: false);
}; IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile);
string actualOutputFile = provider.Utility.SaveTestOutputFile(image, "bmp", encoder, appendPixelTypeToFileName: false); using var referenceImage = Image.Load<TPixel>(actualOutputFile, referenceDecoder);
IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile); referenceImage.CompareToReferenceOutput(ImageComparer.TolerantPercentage(0.01f), provider, extension: "bmp", appendPixelTypeToFileName: false);
using (var referenceImage = Image.Load<TPixel>(actualOutputFile, referenceDecoder))
{
referenceImage.CompareToReferenceOutput(ImageComparer.TolerantPercentage(0.01f), provider, extension: "bmp", appendPixelTypeToFileName: false);
}
}
} }
[Theory] [Theory]
@ -247,19 +227,18 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
ImageComparer customComparer = null) ImageComparer customComparer = null)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
// There is no alpha in bmp with less then 32 bits per pixels, so the reference image will be made opaque.
if (bitsPerPixel != BmpBitsPerPixel.Pixel32)
{ {
// There is no alpha in bmp with less then 32 bits per pixels, so the reference image will be made opaque. image.Mutate(c => c.MakeOpaque());
if (bitsPerPixel != BmpBitsPerPixel.Pixel32) }
{
image.Mutate(c => c.MakeOpaque());
}
var encoder = new BmpEncoder { BitsPerPixel = bitsPerPixel, SupportTransparency = supportTransparency }; var encoder = new BmpEncoder { BitsPerPixel = bitsPerPixel, SupportTransparency = supportTransparency };
// Does DebugSave & load reference CompareToReferenceInput(): // Does DebugSave & load reference CompareToReferenceInput():
image.VerifyEncoder(provider, "bmp", bitsPerPixel, encoder, customComparer); image.VerifyEncoder(provider, "bmp", bitsPerPixel, encoder, customComparer);
}
} }
} }
} }

14
tests/ImageSharp.Tests/Formats/Bmp/BmpMetadataTests.cs

@ -36,14 +36,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
public void Identify_DetectsCorrectBitmapInfoHeaderType(string imagePath, BmpInfoHeaderType expectedInfoHeaderType) public void Identify_DetectsCorrectBitmapInfoHeaderType(string imagePath, BmpInfoHeaderType expectedInfoHeaderType)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ IImageInfo imageInfo = Image.Identify(stream);
IImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo);
Assert.NotNull(imageInfo); BmpMetadata bitmapMetadata = imageInfo.Metadata.GetBmpMetadata();
BmpMetadata bitmapMetadata = imageInfo.Metadata.GetBmpMetadata(); Assert.NotNull(bitmapMetadata);
Assert.NotNull(bitmapMetadata); Assert.Equal(expectedInfoHeaderType, bitmapMetadata.InfoHeaderType);
Assert.Equal(expectedInfoHeaderType, bitmapMetadata.InfoHeaderType);
}
} }
} }
} }

119
tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

@ -23,12 +23,10 @@ namespace SixLabors.ImageSharp.Tests
public void ResolutionShouldChange<TPixel>(TestImageProvider<TPixel> provider) public void ResolutionShouldChange<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Metadata.VerticalResolution = 150;
image.Metadata.VerticalResolution = 150; image.Metadata.HorizontalResolution = 150;
image.Metadata.HorizontalResolution = 150; image.DebugSave(provider);
image.DebugSave(provider);
}
} }
[Fact] [Fact]
@ -38,11 +36,9 @@ namespace SixLabors.ImageSharp.Tests
foreach (TestFile file in Files) foreach (TestFile file in Files)
{ {
using (Image<Rgba32> image = file.CreateRgba32Image()) using Image<Rgba32> image = file.CreateRgba32Image();
{ string filename = path + "/" + file.FileNameWithoutExtension + ".txt";
string filename = path + "/" + file.FileNameWithoutExtension + ".txt"; File.WriteAllText(filename, image.ToBase64String(PngFormat.Instance));
File.WriteAllText(filename, image.ToBase64String(PngFormat.Instance));
}
} }
} }
@ -53,10 +49,8 @@ namespace SixLabors.ImageSharp.Tests
foreach (TestFile file in Files) foreach (TestFile file in Files)
{ {
using (Image<Rgba32> image = file.CreateRgba32Image()) using Image<Rgba32> image = file.CreateRgba32Image();
{ image.Save($"{path}/{file.FileName}");
image.Save($"{path}/{file.FileName}");
}
} }
} }
@ -100,27 +94,25 @@ namespace SixLabors.ImageSharp.Tests
foreach (TestFile file in Files) foreach (TestFile file in Files)
{ {
using (Image<Rgba32> image = file.CreateRgba32Image()) using Image<Rgba32> image = file.CreateRgba32Image();
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.bmp"))
{ {
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.bmp")) image.SaveAsBmp(output);
{ }
image.SaveAsBmp(output);
} using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.jpg"))
{
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.jpg")) image.SaveAsJpeg(output);
{ }
image.SaveAsJpeg(output);
} using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.png"))
{
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.png")) image.SaveAsPng(output);
{ }
image.SaveAsPng(output);
} using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.gif"))
{
using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.gif")) image.SaveAsGif(output);
{
image.SaveAsGif(output);
}
} }
} }
} }
@ -132,19 +124,14 @@ namespace SixLabors.ImageSharp.Tests
foreach (TestFile file in Files) foreach (TestFile file in Files)
{ {
byte[] serialized; using var image = Image.Load(file.Bytes, out IImageFormat mimeType);
using (var image = Image.Load(file.Bytes, out IImageFormat mimeType)) using var memoryStream = new MemoryStream();
using (var memoryStream = new MemoryStream()) image.Save(memoryStream, mimeType);
{ memoryStream.Flush();
image.Save(memoryStream, mimeType); byte[] serialized = memoryStream.ToArray();
memoryStream.Flush();
serialized = memoryStream.ToArray(); using var image2 = Image.Load<Rgba32>(serialized);
} image2.Save($"{path}/{file.FileName}");
using (var image2 = Image.Load<Rgba32>(serialized))
{
image2.Save($"{path}/{file.FileName}");
}
} }
} }
@ -170,25 +157,21 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(10, 100, "tga")] [InlineData(10, 100, "tga")]
public void CanIdentifyImageLoadedFromBytes(int width, int height, string extension) public void CanIdentifyImageLoadedFromBytes(int width, int height, string extension)
{ {
using (var image = Image.LoadPixelData(new Rgba32[width * height], width, height)) using var image = Image.LoadPixelData(new Rgba32[width * height], width, height);
{ using var memoryStream = new MemoryStream();
using (var memoryStream = new MemoryStream()) IImageFormat format = GetFormat(extension);
{ image.Save(memoryStream, format);
IImageFormat format = GetFormat(extension); memoryStream.Position = 0;
image.Save(memoryStream, format);
memoryStream.Position = 0;
IImageInfo imageInfo = Image.Identify(memoryStream); IImageInfo imageInfo = Image.Identify(memoryStream);
Assert.Equal(imageInfo.Width, width); Assert.Equal(imageInfo.Width, width);
Assert.Equal(imageInfo.Height, height); Assert.Equal(imageInfo.Height, height);
memoryStream.Position = 0; memoryStream.Position = 0;
imageInfo = Image.Identify(memoryStream, out IImageFormat detectedFormat); imageInfo = Image.Identify(memoryStream, out IImageFormat detectedFormat);
Assert.Equal(format, detectedFormat); Assert.Equal(format, detectedFormat);
}
}
} }
[Fact] [Fact]
@ -196,13 +179,11 @@ namespace SixLabors.ImageSharp.Tests
{ {
byte[] invalid = new byte[10]; byte[] invalid = new byte[10];
using (var memoryStream = new MemoryStream(invalid)) using var memoryStream = new MemoryStream(invalid);
{ IImageInfo imageInfo = Image.Identify(memoryStream, out IImageFormat format);
IImageInfo imageInfo = Image.Identify(memoryStream, out IImageFormat format);
Assert.Null(imageInfo); Assert.Null(imageInfo);
Assert.Null(format); Assert.Null(format);
}
} }
private static IImageFormat GetFormat(string format) private static IImageFormat GetFormat(string format)

74
tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs

@ -54,11 +54,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
public void Decode_VerifyAllFrames<TPixel>(TestImageProvider<TPixel> provider) public void Decode_VerifyAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.DebugSaveMultiFrame(provider);
image.DebugSaveMultiFrame(provider); image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact);
}
} }
[Fact] [Fact]
@ -70,15 +68,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
fixed (byte* data = testFile.Bytes.AsSpan(0, length)) fixed (byte* data = testFile.Bytes.AsSpan(0, length))
{ {
using (var stream = new UnmanagedMemoryStream(data, length)) using var stream = new UnmanagedMemoryStream(data, length);
{ var decoder = new GifDecoder();
var decoder = new GifDecoder();
using Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream);
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream)) Assert.Equal((200, 200), (image.Width, image.Height));
{
Assert.Equal((200, 200), (image.Width, image.Height));
}
}
} }
} }
@ -87,11 +81,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
public void GifDecoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void GifDecoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider);
image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider);
}
} }
[Theory] [Theory]
@ -104,12 +96,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
expectedFrameCount = 1; expectedFrameCount = 1;
} }
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ Assert.Equal(expectedFrameCount, image.Frames.Count);
Assert.Equal(expectedFrameCount, image.Frames.Count); image.DebugSave(provider);
image.DebugSave(provider); image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider);
image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider);
}
} }
[Theory] [Theory]
@ -117,10 +107,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
public void CanDecodeJustOneFrame<TPixel>(TestImageProvider<TPixel> provider) public void CanDecodeJustOneFrame<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new GifDecoder { DecodingMode = FrameDecodingMode.First })) using Image<TPixel> image = provider.GetImage(new GifDecoder { DecodingMode = FrameDecodingMode.First });
{ Assert.Equal(1, image.Frames.Count);
Assert.Equal(1, image.Frames.Count);
}
} }
[Theory] [Theory]
@ -128,10 +116,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
public void CanDecodeAllFrames<TPixel>(TestImageProvider<TPixel> provider) public void CanDecodeAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new GifDecoder { DecodingMode = FrameDecodingMode.All })) using Image<TPixel> image = provider.GetImage(new GifDecoder { DecodingMode = FrameDecodingMode.All });
{ Assert.True(image.Frames.Count > 1);
Assert.True(image.Frames.Count > 1);
}
} }
[Theory] [Theory]
@ -142,25 +128,21 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
public void DetectPixelSize(string imagePath, int expectedPixelSize) public void DetectPixelSize(string imagePath, int expectedPixelSize)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel);
Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel);
}
} }
[Fact] [Fact]
public void CanDecodeIntermingledImages() public void CanDecodeIntermingledImages()
{ {
using (var kumin1 = Image.Load(TestFile.Create(TestImages.Gif.Kumin).Bytes)) using var kumin1 = Image.Load(TestFile.Create(TestImages.Gif.Kumin).Bytes);
using (Image.Load(TestFile.Create(TestImages.Png.Icon).Bytes)) using var load = Image.Load(TestFile.Create(TestImages.Png.Icon).Bytes);
using (var kumin2 = Image.Load(TestFile.Create(TestImages.Gif.Kumin).Bytes)) using var kumin2 = Image.Load(TestFile.Create(TestImages.Gif.Kumin).Bytes);
for (int i = 0; i < kumin1.Frames.Count; i++)
{ {
for (int i = 0; i < kumin1.Frames.Count; i++) ImageFrame<Rgba32> first = kumin1.Frames[i];
{ ImageFrame<Rgba32> second = kumin2.Frames[i];
ImageFrame<Rgba32> first = kumin1.Frames[i]; first.ComparePixelBufferTo(second.GetPixelSpan());
ImageFrame<Rgba32> second = kumin2.Frames[i];
first.ComparePixelBufferTo(second.GetPixelSpan());
}
} }
} }
} }

162
tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs

@ -30,25 +30,21 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
public void EncodeGeneratedPatterns<TPixel>(TestImageProvider<TPixel> provider) public void EncodeGeneratedPatterns<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var encoder = new GifEncoder
{ {
var encoder = new GifEncoder // Use the palette quantizer without dithering to ensure results
{ // are consistent
// Use the palette quantizer without dithering to ensure results Quantizer = new WebSafePaletteQuantizer(false)
// are consistent };
Quantizer = new WebSafePaletteQuantizer(false)
}; // Always save as we need to compare the encoded output.
provider.Utility.SaveTestOutputFile(image, "gif", encoder);
// Always save as we need to compare the encoded output.
provider.Utility.SaveTestOutputFile(image, "gif", encoder);
}
// Compare encoded result // Compare encoded result
string path = provider.Utility.GetTestOutputFileName("gif", null, true); string path = provider.Utility.GetTestOutputFileName("gif", null, true);
using (var encoded = Image.Load<Rgba32>(path)) using var encoded = Image.Load<Rgba32>(path);
{ encoded.CompareToReferenceOutput(ValidatorComparer, provider, null, "gif");
encoded.CompareToReferenceOutput(ValidatorComparer, provider, null, "gif");
}
} }
[Theory] [Theory]
@ -58,22 +54,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
var options = new GifEncoder(); var options = new GifEncoder();
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{
input.Save(memStream, options); memStream.Position = 0;
using var output = Image.Load<Rgba32>(memStream);
memStream.Position = 0; ImageMetadata meta = output.Metadata;
using (var output = Image.Load<Rgba32>(memStream)) Assert.Equal(xResolution, meta.HorizontalResolution);
{ Assert.Equal(yResolution, meta.VerticalResolution);
ImageMetadata meta = output.Metadata; Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
}
} }
[Fact] [Fact]
@ -83,21 +73,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
var testFile = TestFile.Create(TestImages.Gif.Rings); var testFile = TestFile.Create(TestImages.Gif.Rings);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{
input.Save(memStream, options); memStream.Position = 0;
using var output = Image.Load<Rgba32>(memStream);
memStream.Position = 0; GifMetadata metadata = output.Metadata.GetGifMetadata();
using (var output = Image.Load<Rgba32>(memStream)) Assert.Equal(1, metadata.Comments.Count);
{ Assert.Equal("ImageSharp", metadata.Comments[0]);
GifMetadata metadata = output.Metadata.GetGifMetadata();
Assert.Equal(1, metadata.Comments.Count);
Assert.Equal("ImageSharp", metadata.Comments[0]);
}
}
}
} }
[Theory] [Theory]
@ -105,69 +89,65 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
public void EncodeGlobalPaletteReturnsSmallerFile<TPixel>(TestImageProvider<TPixel> provider) public void EncodeGlobalPaletteReturnsSmallerFile<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var encoder = new GifEncoder
{ {
var encoder = new GifEncoder ColorTableMode = GifColorTableMode.Global,
{ Quantizer = new OctreeQuantizer(false)
ColorTableMode = GifColorTableMode.Global, };
Quantizer = new OctreeQuantizer(false)
};
// Always save as we need to compare the encoded output. // Always save as we need to compare the encoded output.
provider.Utility.SaveTestOutputFile(image, "gif", encoder, "global"); provider.Utility.SaveTestOutputFile(image, "gif", encoder, "global");
encoder.ColorTableMode = GifColorTableMode.Local; encoder.ColorTableMode = GifColorTableMode.Local;
provider.Utility.SaveTestOutputFile(image, "gif", encoder, "local"); provider.Utility.SaveTestOutputFile(image, "gif", encoder, "local");
var fileInfoGlobal = new FileInfo(provider.Utility.GetTestOutputFileName("gif", "global")); var fileInfoGlobal = new FileInfo(provider.Utility.GetTestOutputFileName("gif", "global"));
var fileInfoLocal = new FileInfo(provider.Utility.GetTestOutputFileName("gif", "local")); var fileInfoLocal = new FileInfo(provider.Utility.GetTestOutputFileName("gif", "local"));
Assert.True(fileInfoGlobal.Length < fileInfoLocal.Length); Assert.True(fileInfoGlobal.Length < fileInfoLocal.Length);
}
} }
[Fact] [Fact]
public void NonMutatingEncodePreservesPaletteCount() public void NonMutatingEncodePreservesPaletteCount()
{ {
using (var inStream = new MemoryStream(TestFile.Create(TestImages.Gif.Leo).Bytes)) using var inStream = new MemoryStream(TestFile.Create(TestImages.Gif.Leo).Bytes);
using (var outStream = new MemoryStream()) using var outStream = new MemoryStream();
inStream.Position = 0;
var image = Image.Load<Rgba32>(inStream);
GifMetadata metaData = image.Metadata.GetGifMetadata();
GifFrameMetadata frameMetadata = image.Frames.RootFrame.Metadata.GetGifMetadata();
GifColorTableMode colorMode = metaData.ColorTableMode;
var encoder = new GifEncoder
{ {
inStream.Position = 0; ColorTableMode = colorMode,
Quantizer = new OctreeQuantizer(frameMetadata.ColorTableLength)
var image = Image.Load<Rgba32>(inStream); };
GifMetadata metaData = image.Metadata.GetGifMetadata();
GifFrameMetadata frameMetadata = image.Frames.RootFrame.Metadata.GetGifMetadata();
GifColorTableMode colorMode = metaData.ColorTableMode;
var encoder = new GifEncoder
{
ColorTableMode = colorMode,
Quantizer = new OctreeQuantizer(frameMetadata.ColorTableLength)
};
image.Save(outStream, encoder); image.Save(outStream, encoder);
outStream.Position = 0; outStream.Position = 0;
outStream.Position = 0; outStream.Position = 0;
var clone = Image.Load<Rgba32>(outStream); var clone = Image.Load<Rgba32>(outStream);
GifMetadata cloneMetadata = clone.Metadata.GetGifMetadata(); GifMetadata cloneMetadata = clone.Metadata.GetGifMetadata();
Assert.Equal(metaData.ColorTableMode, cloneMetadata.ColorTableMode); Assert.Equal(metaData.ColorTableMode, cloneMetadata.ColorTableMode);
// Gifiddle and Cyotek GifInfo say this image has 64 colors. // Gifiddle and Cyotek GifInfo say this image has 64 colors.
Assert.Equal(64, frameMetadata.ColorTableLength); Assert.Equal(64, frameMetadata.ColorTableLength);
for (int i = 0; i < image.Frames.Count; i++) for (int i = 0; i < image.Frames.Count; i++)
{ {
GifFrameMetadata ifm = image.Frames[i].Metadata.GetGifMetadata(); GifFrameMetadata ifm = image.Frames[i].Metadata.GetGifMetadata();
GifFrameMetadata cifm = clone.Frames[i].Metadata.GetGifMetadata(); GifFrameMetadata cifm = clone.Frames[i].Metadata.GetGifMetadata();
Assert.Equal(ifm.ColorTableLength, cifm.ColorTableLength);
Assert.Equal(ifm.FrameDelay, cifm.FrameDelay);
}
image.Dispose(); Assert.Equal(ifm.ColorTableLength, cifm.ColorTableLength);
clone.Dispose(); Assert.Equal(ifm.FrameDelay, cifm.FrameDelay);
} }
image.Dispose();
clone.Dispose();
} }
} }
} }

88
tests/ImageSharp.Tests/Formats/Gif/GifMetadataTests.cs

@ -57,12 +57,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
var testFile = TestFile.Create(TestImages.Gif.Rings); var testFile = TestFile.Create(TestImages.Gif.Rings);
using (Image<Rgba32> image = testFile.CreateRgba32Image(options)) using Image<Rgba32> image = testFile.CreateRgba32Image(options);
{ GifMetadata metadata = image.Metadata.GetGifMetadata();
GifMetadata metadata = image.Metadata.GetGifMetadata(); Assert.Equal(1, metadata.Comments.Count);
Assert.Equal(1, metadata.Comments.Count); Assert.Equal("ImageSharp", metadata.Comments[0]);
Assert.Equal("ImageSharp", metadata.Comments[0]);
}
} }
[Fact] [Fact]
@ -75,11 +73,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
var testFile = TestFile.Create(TestImages.Gif.Rings); var testFile = TestFile.Create(TestImages.Gif.Rings);
using (Image<Rgba32> image = testFile.CreateRgba32Image(options)) using Image<Rgba32> image = testFile.CreateRgba32Image(options);
{ GifMetadata metadata = image.Metadata.GetGifMetadata();
GifMetadata metadata = image.Metadata.GetGifMetadata(); Assert.Equal(0, metadata.Comments.Count);
Assert.Equal(0, metadata.Comments.Count);
}
} }
[Fact] [Fact]
@ -88,13 +84,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
var options = new GifDecoder(); var options = new GifDecoder();
var testFile = TestFile.Create(TestImages.Gif.LargeComment); var testFile = TestFile.Create(TestImages.Gif.LargeComment);
using (Image<Rgba32> image = testFile.CreateRgba32Image(options)) using Image<Rgba32> image = testFile.CreateRgba32Image(options);
{ GifMetadata metadata = image.Metadata.GetGifMetadata();
GifMetadata metadata = image.Metadata.GetGifMetadata(); Assert.Equal(2, metadata.Comments.Count);
Assert.Equal(2, metadata.Comments.Count); Assert.Equal(new string('c', 349), metadata.Comments[0]);
Assert.Equal(new string('c', 349), metadata.Comments[0]); Assert.Equal("ImageSharp", metadata.Comments[1]);
Assert.Equal("ImageSharp", metadata.Comments[1]);
}
} }
[Fact] [Fact]
@ -103,20 +97,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
var decoder = new GifDecoder(); var decoder = new GifDecoder();
var testFile = TestFile.Create(TestImages.Gif.LargeComment); var testFile = TestFile.Create(TestImages.Gif.LargeComment);
using (Image<Rgba32> input = testFile.CreateRgba32Image(decoder)) using Image<Rgba32> input = testFile.CreateRgba32Image(decoder);
using (var memoryStream = new MemoryStream()) using var memoryStream = new MemoryStream();
{ input.Save(memoryStream, new GifEncoder());
input.Save(memoryStream, new GifEncoder()); memoryStream.Position = 0;
memoryStream.Position = 0;
using Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, memoryStream);
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, memoryStream)) GifMetadata metadata = image.Metadata.GetGifMetadata();
{ Assert.Equal(2, metadata.Comments.Count);
GifMetadata metadata = image.Metadata.GetGifMetadata(); Assert.Equal(new string('c', 349), metadata.Comments[0]);
Assert.Equal(2, metadata.Comments.Count); Assert.Equal("ImageSharp", metadata.Comments[1]);
Assert.Equal(new string('c', 349), metadata.Comments[0]);
Assert.Equal("ImageSharp", metadata.Comments[1]);
}
}
} }
[Theory] [Theory]
@ -124,15 +114,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ var decoder = new GifDecoder();
var decoder = new GifDecoder(); IImageInfo image = decoder.Identify(Configuration.Default, stream);
IImageInfo image = decoder.Identify(Configuration.Default, stream); ImageMetadata meta = image.Metadata;
ImageMetadata meta = image.Metadata; Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(xResolution, meta.HorizontalResolution); Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(yResolution, meta.VerticalResolution); Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
} }
[Theory] [Theory]
@ -140,17 +128,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ var decoder = new GifDecoder();
var decoder = new GifDecoder(); using Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream);
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream)) ImageMetadata meta = image.Metadata;
{ Assert.Equal(xResolution, meta.HorizontalResolution);
ImageMetadata meta = image.Metadata; Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(xResolution, meta.HorizontalResolution); Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
} }
} }
} }

12
tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs

@ -128,14 +128,10 @@ namespace SixLabors.ImageSharp.Tests
public void DetectFormatAllocatesCleanBuffer() public void DetectFormatAllocatesCleanBuffer()
{ {
byte[] jpegImage; byte[] jpegImage;
using (var buffer = new MemoryStream()) using var buffer = new MemoryStream();
{ using var image = new Image<Rgba32>(100, 100);
using (var image = new Image<Rgba32>(100, 100)) image.SaveAsJpeg(buffer);
{ jpegImage = buffer.ToArray();
image.SaveAsJpeg(buffer);
jpegImage = buffer.ToArray();
}
}
byte[] invalidImage = { 1, 2, 3 }; byte[] invalidImage = { 1, 2, 3 };

48
tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs

@ -43,19 +43,17 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
{ {
Block8x8F block = CreateRandomFloatBlock(0, 100); Block8x8F block = CreateRandomFloatBlock(0, 100);
using (Buffer2D<float> buffer = Configuration.Default.MemoryAllocator.Allocate2D<float>(20, 20, AllocationOptions.Clean)) using Buffer2D<float> buffer = Configuration.Default.MemoryAllocator.Allocate2D<float>(20, 20, AllocationOptions.Clean);
{ BufferArea<float> area = buffer.GetArea(5, 10, 8, 8);
BufferArea<float> area = buffer.GetArea(5, 10, 8, 8); block.Copy1x1Scale(area);
block.Copy1x1Scale(area);
Assert.Equal(block[0, 0], buffer[5, 10]); Assert.Equal(block[0, 0], buffer[5, 10]);
Assert.Equal(block[1, 0], buffer[6, 10]); Assert.Equal(block[1, 0], buffer[6, 10]);
Assert.Equal(block[0, 1], buffer[5, 11]); Assert.Equal(block[0, 1], buffer[5, 11]);
Assert.Equal(block[0, 7], buffer[5, 17]); Assert.Equal(block[0, 7], buffer[5, 17]);
Assert.Equal(block[63], buffer[12, 17]); Assert.Equal(block[63], buffer[12, 17]);
VerifyAllZeroOutsideSubArea(buffer, 5, 10); VerifyAllZeroOutsideSubArea(buffer, 5, 10);
}
} }
[Theory] [Theory]
@ -71,27 +69,25 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var start = new Point(50, 50); var start = new Point(50, 50);
using (Buffer2D<float> buffer = Configuration.Default.MemoryAllocator.Allocate2D<float>(100, 100, AllocationOptions.Clean)) using Buffer2D<float> buffer = Configuration.Default.MemoryAllocator.Allocate2D<float>(100, 100, AllocationOptions.Clean);
{ BufferArea<float> area = buffer.GetArea(start.X, start.Y, 8 * horizontalFactor, 8 * verticalFactor);
BufferArea<float> area = buffer.GetArea(start.X, start.Y, 8 * horizontalFactor, 8 * verticalFactor); block.CopyTo(area, horizontalFactor, verticalFactor);
block.CopyTo(area, horizontalFactor, verticalFactor);
for (int y = 0; y < 8 * verticalFactor; y++) for (int y = 0; y < 8 * verticalFactor; y++)
{
for (int x = 0; x < 8 * horizontalFactor; x++)
{ {
for (int x = 0; x < 8 * horizontalFactor; x++) int yy = y / verticalFactor;
{ int xx = x / horizontalFactor;
int yy = y / verticalFactor;
int xx = x / horizontalFactor;
float expected = block[xx, yy]; float expected = block[xx, yy];
float actual = area[x, y]; float actual = area[x, y];
Assert.Equal(expected, actual); Assert.Equal(expected, actual);
}
} }
VerifyAllZeroOutsideSubArea(buffer, start.X, start.Y, horizontalFactor, verticalFactor);
} }
VerifyAllZeroOutsideSubArea(buffer, start.X, start.Y, horizontalFactor, verticalFactor);
} }
} }
} }

80
tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs

@ -38,23 +38,21 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
public void LoadAndStretchCorners_FromOrigo<TPixel>(TestImageProvider<TPixel> provider) public void LoadAndStretchCorners_FromOrigo<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> s = provider.GetImage()) using Image<TPixel> s = provider.GetImage();
{ var d = default(GenericBlock8x8<TPixel>);
var d = default(GenericBlock8x8<TPixel>); d.LoadAndStretchEdges(s.Frames.RootFrame, 0, 0);
d.LoadAndStretchEdges(s.Frames.RootFrame, 0, 0);
TPixel a = s.Frames.RootFrame[0, 0];
TPixel a = s.Frames.RootFrame[0, 0]; TPixel b = d[0, 0];
TPixel b = d[0, 0];
Assert.Equal(s[0, 0], d[0, 0]);
Assert.Equal(s[0, 0], d[0, 0]); Assert.Equal(s[1, 0], d[1, 0]);
Assert.Equal(s[1, 0], d[1, 0]); Assert.Equal(s[7, 0], d[7, 0]);
Assert.Equal(s[7, 0], d[7, 0]); Assert.Equal(s[0, 1], d[0, 1]);
Assert.Equal(s[0, 1], d[0, 1]); Assert.Equal(s[1, 1], d[1, 1]);
Assert.Equal(s[1, 1], d[1, 1]); Assert.Equal(s[7, 0], d[7, 0]);
Assert.Equal(s[7, 0], d[7, 0]); Assert.Equal(s[0, 7], d[0, 7]);
Assert.Equal(s[0, 7], d[0, 7]); Assert.Equal(s[7, 7], d[7, 7]);
Assert.Equal(s[7, 7], d[7, 7]);
}
} }
[Theory] [Theory]
@ -62,38 +60,36 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
public void LoadAndStretchCorners_WithOffset<TPixel>(TestImageProvider<TPixel> provider) public void LoadAndStretchCorners_WithOffset<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> s = provider.GetImage()) using Image<TPixel> s = provider.GetImage();
{ var d = default(GenericBlock8x8<TPixel>);
var d = default(GenericBlock8x8<TPixel>); d.LoadAndStretchEdges(s.Frames.RootFrame, 6, 7);
d.LoadAndStretchEdges(s.Frames.RootFrame, 6, 7);
Assert.Equal(s[6, 7], d[0, 0]); Assert.Equal(s[6, 7], d[0, 0]);
Assert.Equal(s[6, 8], d[0, 1]); Assert.Equal(s[6, 8], d[0, 1]);
Assert.Equal(s[7, 8], d[1, 1]); Assert.Equal(s[7, 8], d[1, 1]);
Assert.Equal(s[6, 9], d[0, 2]); Assert.Equal(s[6, 9], d[0, 2]);
Assert.Equal(s[6, 9], d[0, 3]); Assert.Equal(s[6, 9], d[0, 3]);
Assert.Equal(s[6, 9], d[0, 7]); Assert.Equal(s[6, 9], d[0, 7]);
Assert.Equal(s[7, 9], d[1, 2]); Assert.Equal(s[7, 9], d[1, 2]);
Assert.Equal(s[7, 9], d[1, 3]); Assert.Equal(s[7, 9], d[1, 3]);
Assert.Equal(s[7, 9], d[1, 7]); Assert.Equal(s[7, 9], d[1, 7]);
Assert.Equal(s[9, 9], d[3, 2]); Assert.Equal(s[9, 9], d[3, 2]);
Assert.Equal(s[9, 9], d[3, 3]); Assert.Equal(s[9, 9], d[3, 3]);
Assert.Equal(s[9, 9], d[3, 7]); Assert.Equal(s[9, 9], d[3, 7]);
Assert.Equal(s[9, 7], d[3, 0]); Assert.Equal(s[9, 7], d[3, 0]);
Assert.Equal(s[9, 7], d[4, 0]); Assert.Equal(s[9, 7], d[4, 0]);
Assert.Equal(s[9, 7], d[7, 0]); Assert.Equal(s[9, 7], d[7, 0]);
Assert.Equal(s[9, 9], d[3, 2]); Assert.Equal(s[9, 9], d[3, 2]);
Assert.Equal(s[9, 9], d[4, 2]); Assert.Equal(s[9, 9], d[4, 2]);
Assert.Equal(s[9, 9], d[7, 2]); Assert.Equal(s[9, 9], d[7, 2]);
Assert.Equal(s[9, 9], d[4, 3]); Assert.Equal(s[9, 9], d[4, 3]);
Assert.Equal(s[9, 9], d[7, 7]); Assert.Equal(s[9, 9], d[7, 7]);
}
} }
[Fact] [Fact]

16
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs

@ -20,16 +20,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
return; return;
} }
using (Image<TPixel> image = provider.GetImage(JpegDecoder)) using Image<TPixel> image = provider.GetImage(JpegDecoder);
{ image.DebugSave(provider);
image.DebugSave(provider);
provider.Utility.TestName = DecodeBaselineJpegOutputName; provider.Utility.TestName = DecodeBaselineJpegOutputName;
image.CompareToReferenceOutput( image.CompareToReferenceOutput(
this.GetImageComparer(provider), this.GetImageComparer(provider),
provider, provider,
appendPixelTypeToFileName: false); appendPixelTypeToFileName: false);
}
} }
[Theory] [Theory]

88
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs

@ -79,17 +79,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ var decoder = new JpegDecoder();
var decoder = new JpegDecoder(); using Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream);
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream)) ImageMetadata meta = image.Metadata;
{ Assert.Equal(xResolution, meta.HorizontalResolution);
ImageMetadata meta = image.Metadata; Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(xResolution, meta.HorizontalResolution); Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
} }
[Theory] [Theory]
@ -97,15 +93,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ var decoder = new JpegDecoder();
var decoder = new JpegDecoder(); IImageInfo image = decoder.Identify(Configuration.Default, stream);
IImageInfo image = decoder.Identify(Configuration.Default, stream); ImageMetadata meta = image.Metadata;
ImageMetadata meta = image.Metadata; Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(xResolution, meta.HorizontalResolution); Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(yResolution, meta.VerticalResolution); Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
} }
[Theory] [Theory]
@ -113,13 +107,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
public void Identify_VerifyQuality(string imagePath, int quality) public void Identify_VerifyQuality(string imagePath, int quality)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ var decoder = new JpegDecoder();
var decoder = new JpegDecoder(); IImageInfo image = decoder.Identify(Configuration.Default, stream);
IImageInfo image = decoder.Identify(Configuration.Default, stream); JpegMetadata meta = image.Metadata.GetJpegMetadata();
JpegMetadata meta = image.Metadata.GetJpegMetadata(); Assert.Equal(quality, meta.Quality);
Assert.Equal(quality, meta.Quality);
}
} }
[Theory] [Theory]
@ -127,28 +119,22 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
public void Decode_VerifyQuality(string imagePath, int quality) public void Decode_VerifyQuality(string imagePath, int quality)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ var decoder = new JpegDecoder();
var decoder = new JpegDecoder(); using Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream);
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream)) JpegMetadata meta = image.Metadata.GetJpegMetadata();
{ Assert.Equal(quality, meta.Quality);
JpegMetadata meta = image.Metadata.GetJpegMetadata();
Assert.Equal(quality, meta.Quality);
}
}
} }
private static void TestImageInfo(string imagePath, IImageDecoder decoder, bool useIdentify, Action<IImageInfo> test) private static void TestImageInfo(string imagePath, IImageDecoder decoder, bool useIdentify, Action<IImageInfo> test)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ IImageInfo imageInfo = useIdentify
IImageInfo imageInfo = useIdentify
? ((IImageInfoDetector)decoder).Identify(Configuration.Default, stream) ? ((IImageInfoDetector)decoder).Identify(Configuration.Default, stream)
: decoder.Decode<Rgba32>(Configuration.Default, stream); : decoder.Decode<Rgba32>(Configuration.Default, stream);
test(imageInfo); test(imageInfo);
}
} }
private static void TestMetadataImpl( private static void TestMetadataImpl(
@ -215,18 +201,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
// Snake.jpg has both Exif and ICC profiles defined: // Snake.jpg has both Exif and ICC profiles defined:
var testFile = TestFile.Create(TestImages.Jpeg.Baseline.Snake); var testFile = TestFile.Create(TestImages.Jpeg.Baseline.Snake);
using (Image<Rgba32> image = testFile.CreateRgba32Image(decoder)) using Image<Rgba32> image = testFile.CreateRgba32Image(decoder);
if (ignoreMetadata)
{ {
if (ignoreMetadata) Assert.Null(image.Metadata.ExifProfile);
{ Assert.Null(image.Metadata.IccProfile);
Assert.Null(image.Metadata.ExifProfile); }
Assert.Null(image.Metadata.IccProfile); else
} {
else Assert.NotNull(image.Metadata.ExifProfile);
{ Assert.NotNull(image.Metadata.IccProfile);
Assert.NotNull(image.Metadata.ExifProfile);
Assert.NotNull(image.Metadata.IccProfile);
}
} }
} }

16
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Progressive.cs

@ -22,16 +22,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
return; return;
} }
using (Image<TPixel> image = provider.GetImage(JpegDecoder)) using Image<TPixel> image = provider.GetImage(JpegDecoder);
{ image.DebugSave(provider);
image.DebugSave(provider);
provider.Utility.TestName = DecodeProgressiveJpegOutputName; provider.Utility.TestName = DecodeProgressiveJpegOutputName;
image.CompareToReferenceOutput( image.CompareToReferenceOutput(
this.GetImageComparer(provider), this.GetImageComparer(provider),
provider, provider,
appendPixelTypeToFileName: false); appendPixelTypeToFileName: false);
}
} }
} }
} }

34
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -69,16 +69,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
public void ParseStream_BasicPropertiesAreCorrect() public void ParseStream_BasicPropertiesAreCorrect()
{ {
byte[] bytes = TestFile.Create(TestImages.Jpeg.Progressive.Progress).Bytes; byte[] bytes = TestFile.Create(TestImages.Jpeg.Progressive.Progress).Bytes;
using (var ms = new MemoryStream(bytes)) using var ms = new MemoryStream(bytes);
{ var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder());
var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); decoder.ParseStream(ms);
decoder.ParseStream(ms);
// I don't know why these numbers are different. All I know is that the decoder works
// I don't know why these numbers are different. All I know is that the decoder works // and spectral data is exactly correct also.
// and spectral data is exactly correct also. // VerifyJpeg.VerifyComponentSizes3(decoder.Frame.Components, 43, 61, 22, 31, 22, 31);
// VerifyJpeg.VerifyComponentSizes3(decoder.Frame.Components, 43, 61, 22, 31, 22, 31); VerifyJpeg.VerifyComponentSizes3(decoder.Frame.Components, 44, 62, 22, 31, 22, 31);
VerifyJpeg.VerifyComponentSizes3(decoder.Frame.Components, 44, 62, 22, 31, 22, 31);
}
} }
public const string DecodeBaselineJpegOutputName = "DecodeBaselineJpeg"; public const string DecodeBaselineJpegOutputName = "DecodeBaselineJpeg";
@ -128,16 +126,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var comparer = ImageComparer.Tolerant(0, 0); var comparer = ImageComparer.Tolerant(0, 0);
using (Image<TPixel> expectedImage = provider.GetReferenceOutputImage<TPixel>(appendPixelTypeToFileName: false)) using Image<TPixel> expectedImage = provider.GetReferenceOutputImage<TPixel>(appendPixelTypeToFileName: false);
using (var pdfJsOriginalResult = Image.Load<Rgba32>(pdfJsOriginalResultPath)) using var pdfJsOriginalResult = Image.Load<Rgba32>(pdfJsOriginalResultPath);
using (var pdfJsPortResult = Image.Load<Rgba32>(sourceBytes, JpegDecoder)) using var pdfJsPortResult = Image.Load<Rgba32>(sourceBytes, JpegDecoder);
{ ImageSimilarityReport originalReport = comparer.CompareImagesOrFrames(expectedImage, pdfJsOriginalResult);
ImageSimilarityReport originalReport = comparer.CompareImagesOrFrames(expectedImage, pdfJsOriginalResult); ImageSimilarityReport portReport = comparer.CompareImagesOrFrames(expectedImage, pdfJsPortResult);
ImageSimilarityReport portReport = comparer.CompareImagesOrFrames(expectedImage, pdfJsPortResult);
this.Output.WriteLine($"Difference for PDF.js ORIGINAL: {originalReport.DifferencePercentageString}"); this.Output.WriteLine($"Difference for PDF.js ORIGINAL: {originalReport.DifferencePercentageString}");
this.Output.WriteLine($"Difference for PORT: {portReport.DifferencePercentageString}"); this.Output.WriteLine($"Difference for PORT: {portReport.DifferencePercentageString}");
}
} }
} }
} }

107
tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs

@ -47,20 +47,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var options = new JpegEncoder(); var options = new JpegEncoder();
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{
input.Save(memStream, options); memStream.Position = 0;
using var output = Image.Load<Rgba32>(memStream);
memStream.Position = 0; JpegMetadata meta = output.Metadata.GetJpegMetadata();
using (var output = Image.Load<Rgba32>(memStream)) Assert.Equal(quality, meta.Quality);
{
JpegMetadata meta = output.Metadata.GetJpegMetadata();
Assert.Equal(quality, meta.Quality);
}
}
}
} }
[Theory] [Theory]
@ -108,22 +102,21 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
int quality = 100) int quality = 100)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
// There is no alpha in Jpeg!
image.Mutate(c => c.MakeOpaque());
var encoder = new JpegEncoder
{ {
// There is no alpha in Jpeg! Subsample = subsample,
image.Mutate(c => c.MakeOpaque()); Quality = quality
};
string info = $"{subsample}-Q{quality}";
ImageComparer comparer = GetComparer(quality, subsample);
var encoder = new JpegEncoder // Does DebugSave & load reference CompareToReferenceInput():
{ image.VerifyEncoder(provider, "jpeg", info, encoder, comparer, referenceImageExtension: "png");
Subsample = subsample,
Quality = quality
};
string info = $"{subsample}-Q{quality}";
ImageComparer comparer = GetComparer(quality, subsample);
// Does DebugSave & load reference CompareToReferenceInput():
image.VerifyEncoder(provider, "jpeg", info, encoder, comparer, referenceImageExtension: "png");
}
} }
[Fact] [Fact]
@ -136,17 +129,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var testFile = TestFile.Create(TestImages.Jpeg.Baseline.Calliphora); var testFile = TestFile.Create(TestImages.Jpeg.Baseline.Calliphora);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
using (var memStream0 = new MemoryStream()) using var memStream0 = new MemoryStream();
using (var memStream1 = new MemoryStream()) using var memStream1 = new MemoryStream();
{ input.SaveAsJpeg(memStream0, options);
input.SaveAsJpeg(memStream0, options);
options.Quality = 1; options.Quality = 1;
input.SaveAsJpeg(memStream1, options); input.SaveAsJpeg(memStream1, options);
Assert.Equal(memStream0.ToArray(), memStream1.ToArray()); Assert.Equal(memStream0.ToArray(), memStream1.ToArray());
}
} }
[Fact] [Fact]
@ -159,17 +150,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var testFile = TestFile.Create(TestImages.Jpeg.Baseline.Calliphora); var testFile = TestFile.Create(TestImages.Jpeg.Baseline.Calliphora);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
using (var memStream0 = new MemoryStream()) using var memStream0 = new MemoryStream();
using (var memStream1 = new MemoryStream()) using var memStream1 = new MemoryStream();
{ input.SaveAsJpeg(memStream0, options);
input.SaveAsJpeg(memStream0, options);
options.Quality = 100; options.Quality = 100;
input.SaveAsJpeg(memStream1, options); input.SaveAsJpeg(memStream1, options);
Assert.NotEqual(memStream0.ToArray(), memStream1.ToArray()); Assert.NotEqual(memStream0.ToArray(), memStream1.ToArray());
}
} }
[Theory] [Theory]
@ -179,22 +168,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var options = new JpegEncoder(); var options = new JpegEncoder();
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{
input.Save(memStream, options); memStream.Position = 0;
using var output = Image.Load<Rgba32>(memStream);
memStream.Position = 0; ImageMetadata meta = output.Metadata;
using (var output = Image.Load<Rgba32>(memStream)) Assert.Equal(xResolution, meta.HorizontalResolution);
{ Assert.Equal(yResolution, meta.VerticalResolution);
ImageMetadata meta = output.Metadata; Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
}
} }
} }
} }

58
tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs

@ -34,10 +34,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
private static void SaveBuffer<TPixel>(JpegComponentPostProcessor cp, TestImageProvider<TPixel> provider) private static void SaveBuffer<TPixel>(JpegComponentPostProcessor cp, TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<Rgba32> image = cp.ColorBuffer.ToGrayscaleImage(1f / 255f)) using Image<Rgba32> image = cp.ColorBuffer.ToGrayscaleImage(1f / 255f);
{ image.DebugSave(provider, $"-C{cp.Component.Index}-");
image.DebugSave(provider, $"-C{cp.Component.Index}-");
}
} }
[Theory] [Theory]
@ -47,18 +45,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
string imageFile = provider.SourceFileOrDescription; string imageFile = provider.SourceFileOrDescription;
using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile)) using JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile);
using (var pp = new JpegImagePostProcessor(Configuration.Default, decoder)) using var pp = new JpegImagePostProcessor(Configuration.Default, decoder);
using (var imageFrame = new ImageFrame<Rgba32>(Configuration.Default, decoder.ImageWidth, decoder.ImageHeight)) using var imageFrame = new ImageFrame<Rgba32>(Configuration.Default, decoder.ImageWidth, decoder.ImageHeight);
{ pp.DoPostProcessorStep(imageFrame);
pp.DoPostProcessorStep(imageFrame);
JpegComponentPostProcessor[] cp = pp.ComponentProcessors; JpegComponentPostProcessor[] cp = pp.ComponentProcessors;
SaveBuffer(cp[0], provider); SaveBuffer(cp[0], provider);
SaveBuffer(cp[1], provider); SaveBuffer(cp[1], provider);
SaveBuffer(cp[2], provider); SaveBuffer(cp[2], provider);
}
} }
[Theory] [Theory]
@ -67,30 +63,26 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
string imageFile = provider.SourceFileOrDescription; string imageFile = provider.SourceFileOrDescription;
using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile)) using JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile);
using (var pp = new JpegImagePostProcessor(Configuration.Default, decoder)) using var pp = new JpegImagePostProcessor(Configuration.Default, decoder);
using (var image = new Image<Rgba32>(decoder.ImageWidth, decoder.ImageHeight)) using var image = new Image<Rgba32>(decoder.ImageWidth, decoder.ImageHeight);
{ pp.PostProcess(image.Frames.RootFrame);
pp.PostProcess(image.Frames.RootFrame);
image.DebugSave(provider); image.DebugSave(provider);
ImagingTestCaseUtility testUtil = provider.Utility; ImagingTestCaseUtility testUtil = provider.Utility;
testUtil.TestGroupName = nameof(JpegDecoderTests); testUtil.TestGroupName = nameof(JpegDecoderTests);
testUtil.TestName = JpegDecoderTests.DecodeBaselineJpegOutputName; testUtil.TestName = JpegDecoderTests.DecodeBaselineJpegOutputName;
using (Image<TPixel> referenceImage = using Image<TPixel> referenceImage =
provider.GetReferenceOutputImage<TPixel>(appendPixelTypeToFileName: false)) provider.GetReferenceOutputImage<TPixel>(appendPixelTypeToFileName: false);
{ ImageSimilarityReport report = ImageComparer.Exact.CompareImagesOrFrames(referenceImage, image);
ImageSimilarityReport report = ImageComparer.Exact.CompareImagesOrFrames(referenceImage, image);
this.Output.WriteLine($"*** {imageFile} ***"); this.Output.WriteLine($"*** {imageFile} ***");
this.Output.WriteLine($"Difference: {report.DifferencePercentageString}"); this.Output.WriteLine($"Difference: {report.DifferencePercentageString}");
// ReSharper disable once PossibleInvalidOperationException // ReSharper disable once PossibleInvalidOperationException
Assert.True(report.TotalNormalizedDifference.Value < 0.005f); Assert.True(report.TotalNormalizedDifference.Value < 0.005f);
}
}
} }
} }
} }

60
tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs

@ -32,28 +32,24 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
{ {
var expectedColorSpace = (JpegColorSpace)expectedColorSpaceValue; var expectedColorSpace = (JpegColorSpace)expectedColorSpaceValue;
using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile)) using JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile);
{ Assert.Equal(expectedColorSpace, decoder.ColorSpace);
Assert.Equal(expectedColorSpace, decoder.ColorSpace);
}
} }
[Fact] [Fact]
public void ComponentScalingIsCorrect_1ChannelJpeg() public void ComponentScalingIsCorrect_1ChannelJpeg()
{ {
using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(TestImages.Jpeg.Baseline.Jpeg400)) using JpegDecoderCore decoder = JpegFixture.ParseJpegStream(TestImages.Jpeg.Baseline.Jpeg400);
{ Assert.Equal(1, decoder.ComponentCount);
Assert.Equal(1, decoder.ComponentCount); Assert.Equal(1, decoder.Components.Length);
Assert.Equal(1, decoder.Components.Length);
Size expectedSizeInBlocks = decoder.ImageSizeInPixels.DivideRoundUp(8); Size expectedSizeInBlocks = decoder.ImageSizeInPixels.DivideRoundUp(8);
Assert.Equal(expectedSizeInBlocks, decoder.ImageSizeInMCU); Assert.Equal(expectedSizeInBlocks, decoder.ImageSizeInMCU);
var uniform1 = new Size(1, 1); var uniform1 = new Size(1, 1);
JpegComponent c0 = decoder.Components[0]; JpegComponent c0 = decoder.Components[0];
VerifyJpeg.VerifyComponent(c0, expectedSizeInBlocks, uniform1, uniform1); VerifyJpeg.VerifyComponent(c0, expectedSizeInBlocks, uniform1, uniform1);
}
} }
[Theory] [Theory]
@ -104,32 +100,30 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var fLuma = (Size)expectedLumaFactors; var fLuma = (Size)expectedLumaFactors;
var fChroma = (Size)expectedChromaFactors; var fChroma = (Size)expectedChromaFactors;
using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile)) using JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile);
{ Assert.Equal(componentCount, decoder.ComponentCount);
Assert.Equal(componentCount, decoder.ComponentCount); Assert.Equal(componentCount, decoder.Components.Length);
Assert.Equal(componentCount, decoder.Components.Length);
JpegComponent c0 = decoder.Components[0]; JpegComponent c0 = decoder.Components[0];
JpegComponent c1 = decoder.Components[1]; JpegComponent c1 = decoder.Components[1];
JpegComponent c2 = decoder.Components[2]; JpegComponent c2 = decoder.Components[2];
var uniform1 = new Size(1, 1); var uniform1 = new Size(1, 1);
Size expectedLumaSizeInBlocks = decoder.ImageSizeInMCU.MultiplyBy(fLuma); Size expectedLumaSizeInBlocks = decoder.ImageSizeInMCU.MultiplyBy(fLuma);
Size divisor = fLuma.DivideBy(fChroma); Size divisor = fLuma.DivideBy(fChroma);
Size expectedChromaSizeInBlocks = expectedLumaSizeInBlocks.DivideRoundUp(divisor); Size expectedChromaSizeInBlocks = expectedLumaSizeInBlocks.DivideRoundUp(divisor);
VerifyJpeg.VerifyComponent(c0, expectedLumaSizeInBlocks, fLuma, uniform1); VerifyJpeg.VerifyComponent(c0, expectedLumaSizeInBlocks, fLuma, uniform1);
VerifyJpeg.VerifyComponent(c1, expectedChromaSizeInBlocks, fChroma, divisor); VerifyJpeg.VerifyComponent(c1, expectedChromaSizeInBlocks, fChroma, divisor);
VerifyJpeg.VerifyComponent(c2, expectedChromaSizeInBlocks, fChroma, divisor); VerifyJpeg.VerifyComponent(c2, expectedChromaSizeInBlocks, fChroma, divisor);
if (componentCount == 4) if (componentCount == 4)
{ {
JpegComponent c3 = decoder.Components[2]; JpegComponent c3 = decoder.Components[2];
VerifyJpeg.VerifyComponent(c3, expectedLumaSizeInBlocks, fLuma, uniform1); VerifyJpeg.VerifyComponent(c3, expectedLumaSizeInBlocks, fLuma, uniform1);
}
} }
} }
} }

20
tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs

@ -50,13 +50,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes; byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes;
using (var ms = new MemoryStream(sourceBytes)) using var ms = new MemoryStream(sourceBytes);
{ decoder.ParseStream(ms);
decoder.ParseStream(ms);
var data = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder); var data = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder);
VerifyJpeg.SaveSpectralImage(provider, data); VerifyJpeg.SaveSpectralImage(provider, data);
}
} }
[Theory] [Theory]
@ -73,13 +71,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes; byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes;
using (var ms = new MemoryStream(sourceBytes)) using var ms = new MemoryStream(sourceBytes);
{ decoder.ParseStream(ms);
decoder.ParseStream(ms); var imageSharpData = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder);
var imageSharpData = LibJpegTools.SpectralData.LoadFromImageSharpDecoder(decoder);
this.VerifySpectralCorrectnessImpl(provider, imageSharpData); this.VerifySpectralCorrectnessImpl(provider, imageSharpData);
}
} }
private void VerifySpectralCorrectnessImpl<TPixel>( private void VerifySpectralCorrectnessImpl<TPixel>(

10
tests/ImageSharp.Tests/Formats/Jpg/Utils/JpegFixture.cs

@ -192,12 +192,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils
internal static JpegDecoderCore ParseJpegStream(string testFileName, bool metaDataOnly = false) internal static JpegDecoderCore ParseJpegStream(string testFileName, bool metaDataOnly = false)
{ {
byte[] bytes = TestFile.Create(testFileName).Bytes; byte[] bytes = TestFile.Create(testFileName).Bytes;
using (var ms = new MemoryStream(bytes)) using var ms = new MemoryStream(bytes);
{ var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder());
var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); decoder.ParseStream(ms, metaDataOnly);
decoder.ParseStream(ms, metaDataOnly); return decoder;
return decoder;
}
} }
} }
} }

46
tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.cs

@ -98,40 +98,38 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils
{ {
RunDumpJpegCoeffsTool(testFile.FullPath, coeffFileFullPath); RunDumpJpegCoeffsTool(testFile.FullPath, coeffFileFullPath);
using (var dumpStream = new FileStream(coeffFileFullPath, FileMode.Open)) using var dumpStream = new FileStream(coeffFileFullPath, FileMode.Open);
using (var rdr = new BinaryReader(dumpStream)) using var rdr = new BinaryReader(dumpStream);
int componentCount = rdr.ReadInt16();
var result = new ComponentData[componentCount];
for (int i = 0; i < componentCount; i++)
{ {
int componentCount = rdr.ReadInt16(); int widthInBlocks = rdr.ReadInt16();
var result = new ComponentData[componentCount]; int heightInBlocks = rdr.ReadInt16();
var resultComponent = new ComponentData(widthInBlocks, heightInBlocks, i);
result[i] = resultComponent;
}
for (int i = 0; i < componentCount; i++) var buffer = new byte[64 * sizeof(short)];
{
int widthInBlocks = rdr.ReadInt16();
int heightInBlocks = rdr.ReadInt16();
var resultComponent = new ComponentData(widthInBlocks, heightInBlocks, i);
result[i] = resultComponent;
}
var buffer = new byte[64 * sizeof(short)]; for (int i = 0; i < result.Length; i++)
{
ComponentData c = result[i];
for (int i = 0; i < result.Length; i++) for (int y = 0; y < c.HeightInBlocks; y++)
{ {
ComponentData c = result[i]; for (int x = 0; x < c.WidthInBlocks; x++)
for (int y = 0; y < c.HeightInBlocks; y++)
{ {
for (int x = 0; x < c.WidthInBlocks; x++) rdr.Read(buffer, 0, buffer.Length);
{
rdr.Read(buffer, 0, buffer.Length);
short[] block = MemoryMarshal.Cast<byte, short>(buffer.AsSpan()).ToArray(); short[] block = MemoryMarshal.Cast<byte, short>(buffer.AsSpan()).ToArray();
c.MakeBlock(block, y, x); c.MakeBlock(block, y, x);
}
} }
} }
return new SpectralData(result);
} }
return new SpectralData(result);
} }
finally finally
{ {

8
tests/ImageSharp.Tests/Formats/Jpg/Utils/VerifyJpeg.cs

@ -59,11 +59,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils
output?.WriteLine("Min: " + comp.MinVal); output?.WriteLine("Min: " + comp.MinVal);
output?.WriteLine("Max: " + comp.MaxVal); output?.WriteLine("Max: " + comp.MaxVal);
using (Image<Rgba32> image = comp.CreateGrayScaleImage()) using Image<Rgba32> image = comp.CreateGrayScaleImage();
{ string details = $"C{comp.Index}";
string details = $"C{comp.Index}"; image.DebugSave(provider, details, appendPixelTypeToFileName: false);
image.DebugSave(provider, details, appendPixelTypeToFileName: false);
}
} }
Image<Rgba32> fullImage = data.TryCreateRGBSpectralImage(); Image<Rgba32> fullImage = data.TryCreateRGBSpectralImage();

18
tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.Chunks.cs

@ -63,19 +63,17 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
{ {
string chunkName = GetChunkTypeName(chunkType); string chunkName = GetChunkTypeName(chunkType);
using (var memStream = new MemoryStream()) using var memStream = new MemoryStream();
{ WriteHeaderChunk(memStream);
WriteHeaderChunk(memStream); WriteChunk(memStream, chunkName);
WriteChunk(memStream, chunkName); WriteDataChunk(memStream);
WriteDataChunk(memStream);
var decoder = new PngDecoder(); var decoder = new PngDecoder();
ImageFormatException exception = ImageFormatException exception =
Assert.Throws<ImageFormatException>(() => decoder.Decode<Rgb24>(null, memStream)); Assert.Throws<ImageFormatException>(() => decoder.Decode<Rgb24>(null, memStream));
Assert.Equal($"CRC Error. PNG {chunkName} chunk is corrupt!", exception.Message); Assert.Equal($"CRC Error. PNG {chunkName} chunk is corrupt!", exception.Message);
}
} }
private static string GetChunkTypeName(uint value) private static string GetChunkTypeName(uint value)

102
tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

@ -87,23 +87,21 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decode<TPixel>(TestImageProvider<TPixel> provider) public void Decode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider);
// We don't have another x-plat reference decoder that can be compared for this image. // We don't have another x-plat reference decoder that can be compared for this image.
if (provider.Utility.SourceFileOrDescription == TestImages.Png.Bad.Issue1047_BadEndChunk) if (provider.Utility.SourceFileOrDescription == TestImages.Png.Bad.Issue1047_BadEndChunk)
{ {
if (TestEnvironment.IsWindows) if (TestEnvironment.IsWindows)
{
image.CompareToOriginal(provider, ImageComparer.Exact, (IImageDecoder)SystemDrawingReferenceDecoder.Instance);
}
}
else
{ {
image.CompareToOriginal(provider, ImageComparer.Exact); image.CompareToOriginal(provider, ImageComparer.Exact, (IImageDecoder)SystemDrawingReferenceDecoder.Instance);
} }
} }
else
{
image.CompareToOriginal(provider, ImageComparer.Exact);
}
} }
[Theory] [Theory]
@ -111,11 +109,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decode_Interlaced_ImageIsCorrect<TPixel>(TestImageProvider<TPixel> provider) public void Decode_Interlaced_ImageIsCorrect<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
} }
[Theory] [Theory]
@ -123,11 +119,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decode_48Bpp<TPixel>(TestImageProvider<TPixel> provider) public void Decode_48Bpp<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
} }
[Theory] [Theory]
@ -135,11 +129,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decode_64Bpp<TPixel>(TestImageProvider<TPixel> provider) public void Decode_64Bpp<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
} }
[Theory] [Theory]
@ -147,11 +139,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decoder_L8bitInterlaced<TPixel>(TestImageProvider<TPixel> provider) public void Decoder_L8bitInterlaced<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
} }
[Theory] [Theory]
@ -159,11 +149,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decode_L16Bit<TPixel>(TestImageProvider<TPixel> provider) public void Decode_L16Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
} }
[Theory] [Theory]
@ -171,11 +159,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decode_GrayAlpha16Bit<TPixel>(TestImageProvider<TPixel> provider) public void Decode_GrayAlpha16Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
} }
[Theory] [Theory]
@ -183,11 +169,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decoder_CanDecodeGrey8bitWithAlpha<TPixel>(TestImageProvider<TPixel> provider) public void Decoder_CanDecodeGrey8bitWithAlpha<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
} }
[Theory] [Theory]
@ -195,11 +179,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void Decoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
} }
[Theory] [Theory]
@ -213,10 +195,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Identify(string imagePath, int expectedPixelSize) public void Identify(string imagePath, int expectedPixelSize)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel);
Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel);
}
} }
[Theory] [Theory]
@ -227,11 +207,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
System.Exception ex = Record.Exception( System.Exception ex = Record.Exception(
() => () =>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact);
image.CompareToOriginal(provider, ImageComparer.Exact);
}
}); });
Assert.Null(ex); Assert.Null(ex);
} }

224
tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs

@ -230,26 +230,24 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void WritesFileMarker<TPixel>(TestImageProvider<TPixel> provider) public void WritesFileMarker<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
using (var ms = new MemoryStream()) using var ms = new MemoryStream();
image.Save(ms, new PngEncoder());
byte[] data = ms.ToArray().Take(8).ToArray();
byte[] expected =
{ {
image.Save(ms, new PngEncoder()); 0x89, // Set the high bit.
0x50, // P
byte[] data = ms.ToArray().Take(8).ToArray(); 0x4E, // N
byte[] expected = 0x47, // G
{ 0x0D, // Line ending CRLF
0x89, // Set the high bit. 0x0A, // Line ending CRLF
0x50, // P 0x1A, // EOF
0x4E, // N 0x0A // LF
0x47, // G };
0x0D, // Line ending CRLF
0x0A, // Line ending CRLF Assert.Equal(expected, data);
0x1A, // EOF
0x0A // LF
};
Assert.Equal(expected, data);
}
} }
[Theory] [Theory]
@ -259,22 +257,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
var options = new PngEncoder(); var options = new PngEncoder();
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{
input.Save(memStream, options); memStream.Position = 0;
using var output = Image.Load<Rgba32>(memStream);
memStream.Position = 0; ImageMetadata meta = output.Metadata;
using (var output = Image.Load<Rgba32>(memStream)) Assert.Equal(xResolution, meta.HorizontalResolution);
{ Assert.Equal(yResolution, meta.VerticalResolution);
ImageMetadata meta = output.Metadata; Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
}
} }
[Theory] [Theory]
@ -284,21 +276,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
var options = new PngEncoder(); var options = new PngEncoder();
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{
input.Save(memStream, options);
memStream.Position = 0; memStream.Position = 0;
using (var output = Image.Load<Rgba32>(memStream)) using var output = Image.Load<Rgba32>(memStream);
{ PngMetadata meta = output.Metadata.GetPngMetadata();
PngMetadata meta = output.Metadata.GetPngMetadata();
Assert.Equal(pngBitDepth, meta.BitDepth); Assert.Equal(pngBitDepth, meta.BitDepth);
}
}
}
} }
[Theory] [Theory]
@ -308,51 +294,45 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
var options = new PngEncoder(); var options = new PngEncoder();
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
PngMetadata inMeta = input.Metadata.GetPngMetadata();
Assert.True(inMeta.HasTransparency);
using var memStream = new MemoryStream();
input.Save(memStream, options);
memStream.Position = 0;
using var output = Image.Load<Rgba32>(memStream);
PngMetadata outMeta = output.Metadata.GetPngMetadata();
Assert.True(outMeta.HasTransparency);
switch (pngColorType)
{ {
PngMetadata inMeta = input.Metadata.GetPngMetadata(); case PngColorType.Grayscale:
Assert.True(inMeta.HasTransparency); if (pngBitDepth.Equals(PngBitDepth.Bit16))
{
using (var memStream = new MemoryStream()) Assert.True(outMeta.TransparentL16.HasValue);
{ Assert.Equal(inMeta.TransparentL16, outMeta.TransparentL16);
input.Save(memStream, options); }
memStream.Position = 0; else
using (var output = Image.Load<Rgba32>(memStream)) {
Assert.True(outMeta.TransparentL8.HasValue);
Assert.Equal(inMeta.TransparentL8, outMeta.TransparentL8);
}
break;
case PngColorType.Rgb:
if (pngBitDepth.Equals(PngBitDepth.Bit16))
{
Assert.True(outMeta.TransparentRgb48.HasValue);
Assert.Equal(inMeta.TransparentRgb48, outMeta.TransparentRgb48);
}
else
{ {
PngMetadata outMeta = output.Metadata.GetPngMetadata(); Assert.True(outMeta.TransparentRgb24.HasValue);
Assert.True(outMeta.HasTransparency); Assert.Equal(inMeta.TransparentRgb24, outMeta.TransparentRgb24);
switch (pngColorType)
{
case PngColorType.Grayscale:
if (pngBitDepth.Equals(PngBitDepth.Bit16))
{
Assert.True(outMeta.TransparentL16.HasValue);
Assert.Equal(inMeta.TransparentL16, outMeta.TransparentL16);
}
else
{
Assert.True(outMeta.TransparentL8.HasValue);
Assert.Equal(inMeta.TransparentL8, outMeta.TransparentL8);
}
break;
case PngColorType.Rgb:
if (pngBitDepth.Equals(PngBitDepth.Bit16))
{
Assert.True(outMeta.TransparentRgb48.HasValue);
Assert.Equal(inMeta.TransparentRgb48, outMeta.TransparentRgb48);
}
else
{
Assert.True(outMeta.TransparentRgb24.HasValue);
Assert.Equal(inMeta.TransparentRgb24, outMeta.TransparentRgb24);
}
break;
}
} }
}
break;
} }
} }
@ -372,41 +352,37 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
bool appendPngBitDepth = false) bool appendPngBitDepth = false)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var encoder = new PngEncoder
{ {
var encoder = new PngEncoder ColorType = pngColorType,
{ FilterMethod = pngFilterMethod,
ColorType = pngColorType, CompressionLevel = compressionLevel,
FilterMethod = pngFilterMethod, BitDepth = bitDepth,
CompressionLevel = compressionLevel, Quantizer = new WuQuantizer(paletteSize),
BitDepth = bitDepth, InterlaceMethod = interlaceMode
Quantizer = new WuQuantizer(paletteSize), };
InterlaceMethod = interlaceMode
}; string pngColorTypeInfo = appendPngColorType ? pngColorType.ToString() : string.Empty;
string pngFilterMethodInfo = appendPngFilterMethod ? pngFilterMethod.ToString() : string.Empty;
string pngColorTypeInfo = appendPngColorType ? pngColorType.ToString() : string.Empty; string compressionLevelInfo = appendCompressionLevel ? $"_C{compressionLevel}" : string.Empty;
string pngFilterMethodInfo = appendPngFilterMethod ? pngFilterMethod.ToString() : string.Empty; string paletteSizeInfo = appendPaletteSize ? $"_PaletteSize-{paletteSize}" : string.Empty;
string compressionLevelInfo = appendCompressionLevel ? $"_C{compressionLevel}" : string.Empty; string pngBitDepthInfo = appendPngBitDepth ? bitDepth.ToString() : string.Empty;
string paletteSizeInfo = appendPaletteSize ? $"_PaletteSize-{paletteSize}" : string.Empty; string pngInterlaceModeInfo = interlaceMode != PngInterlaceMode.None ? $"_{interlaceMode}" : string.Empty;
string pngBitDepthInfo = appendPngBitDepth ? bitDepth.ToString() : string.Empty;
string pngInterlaceModeInfo = interlaceMode != PngInterlaceMode.None ? $"_{interlaceMode}" : string.Empty; string debugInfo = $"{pngColorTypeInfo}{pngFilterMethodInfo}{compressionLevelInfo}{paletteSizeInfo}{pngBitDepthInfo}{pngInterlaceModeInfo}";
string debugInfo = $"{pngColorTypeInfo}{pngFilterMethodInfo}{compressionLevelInfo}{paletteSizeInfo}{pngBitDepthInfo}{pngInterlaceModeInfo}"; string actualOutputFile = provider.Utility.SaveTestOutputFile(image, "png", encoder, debugInfo, appendPixelType);
string actualOutputFile = provider.Utility.SaveTestOutputFile(image, "png", encoder, debugInfo, appendPixelType); // Compare to the Magick reference decoder.
IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile);
// Compare to the Magick reference decoder.
IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(actualOutputFile); // We compare using both our decoder and the reference decoder as pixel transformation
// occurs within the encoder itself leaving the input image unaffected.
// We compare using both our decoder and the reference decoder as pixel transformation // This means we are benefiting from testing our decoder also.
// occurs within the encoder itself leaving the input image unaffected. using var imageSharpImage = Image.Load<TPixel>(actualOutputFile, new PngDecoder());
// This means we are benefiting from testing our decoder also. using var referenceImage = Image.Load<TPixel>(actualOutputFile, referenceDecoder);
using (var imageSharpImage = Image.Load<TPixel>(actualOutputFile, new PngDecoder())) ImageComparer.Exact.VerifySimilarity(referenceImage, imageSharpImage);
using (var referenceImage = Image.Load<TPixel>(actualOutputFile, referenceDecoder))
{
ImageComparer.Exact.VerifySimilarity(referenceImage, imageSharpImage);
}
}
} }
} }
} }

185
tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs

@ -53,21 +53,19 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decoder_CanReadTextData<TPixel>(TestImageProvider<TPixel> provider) public void Decoder_CanReadTextData<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance); Assert.Contains(meta.TextData, m => m.Keyword.Equals("Comment") && m.Value.Equals("comment"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Comment") && m.Value.Equals("comment")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("Author") && m.Value.Equals("ImageSharp"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Author") && m.Value.Equals("ImageSharp")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("Copyright") && m.Value.Equals("ImageSharp"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Copyright") && m.Value.Equals("ImageSharp")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("Title") && m.Value.Equals("unittest"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Title") && m.Value.Equals("unittest")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("Description") && m.Value.Equals("compressed-text"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Description") && m.Value.Equals("compressed-text")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("International") && m.Value.Equals("'e', mu'tlheghvam, ghaH yu'") && m.LanguageTag.Equals("x-klingon") && m.TranslatedKeyword.Equals("warning"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("International") && m.Value.Equals("'e', mu'tlheghvam, ghaH yu'") && m.LanguageTag.Equals("x-klingon") && m.TranslatedKeyword.Equals("warning")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("International2") && m.Value.Equals("ИМАГЕШАРП") && m.LanguageTag.Equals("rus"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("International2") && m.Value.Equals("ИМАГЕШАРП") && m.LanguageTag.Equals("rus")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("CompressedInternational") && m.Value.Equals("la plume de la mante") && m.LanguageTag.Equals("fra") && m.TranslatedKeyword.Equals("foobar"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("CompressedInternational") && m.Value.Equals("la plume de la mante") && m.LanguageTag.Equals("fra") && m.TranslatedKeyword.Equals("foobar")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("CompressedInternational2") && m.Value.Equals("這是一個考驗") && m.LanguageTag.Equals("chinese"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("CompressedInternational2") && m.Value.Equals("這是一個考驗") && m.LanguageTag.Equals("chinese")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("NoLang") && m.Value.Equals("this text chunk is missing a language tag"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("NoLang") && m.Value.Equals("this text chunk is missing a language tag")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("NoTranslatedKeyword") && m.Value.Equals("dieser chunk hat kein übersetztes Schlüßelwort"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("NoTranslatedKeyword") && m.Value.Equals("dieser chunk hat kein übersetztes Schlüßelwort"));
}
} }
[Theory] [Theory]
@ -76,28 +74,24 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
var decoder = new PngDecoder(); var decoder = new PngDecoder();
using (Image<TPixel> input = provider.GetImage(decoder)) using Image<TPixel> input = provider.GetImage(decoder);
using (var memoryStream = new MemoryStream()) using var memoryStream = new MemoryStream();
{ input.Save(memoryStream, new PngEncoder());
input.Save(memoryStream, new PngEncoder());
memoryStream.Position = 0;
memoryStream.Position = 0; using Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, memoryStream);
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, memoryStream)) PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
{ Assert.Contains(meta.TextData, m => m.Keyword.Equals("Comment") && m.Value.Equals("comment"));
PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance); Assert.Contains(meta.TextData, m => m.Keyword.Equals("Author") && m.Value.Equals("ImageSharp"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Comment") && m.Value.Equals("comment")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("Copyright") && m.Value.Equals("ImageSharp"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Author") && m.Value.Equals("ImageSharp")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("Title") && m.Value.Equals("unittest"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Copyright") && m.Value.Equals("ImageSharp")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("Description") && m.Value.Equals("compressed-text"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Title") && m.Value.Equals("unittest")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("International") && m.Value.Equals("'e', mu'tlheghvam, ghaH yu'") && m.LanguageTag.Equals("x-klingon") && m.TranslatedKeyword.Equals("warning"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("Description") && m.Value.Equals("compressed-text")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("International2") && m.Value.Equals("ИМАГЕШАРП") && m.LanguageTag.Equals("rus"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("International") && m.Value.Equals("'e', mu'tlheghvam, ghaH yu'") && m.LanguageTag.Equals("x-klingon") && m.TranslatedKeyword.Equals("warning")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("CompressedInternational") && m.Value.Equals("la plume de la mante") && m.LanguageTag.Equals("fra") && m.TranslatedKeyword.Equals("foobar"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("International2") && m.Value.Equals("ИМАГЕШАРП") && m.LanguageTag.Equals("rus")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("CompressedInternational2") && m.Value.Equals("這是一個考驗") && m.LanguageTag.Equals("chinese"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("CompressedInternational") && m.Value.Equals("la plume de la mante") && m.LanguageTag.Equals("fra") && m.TranslatedKeyword.Equals("foobar")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("NoLang") && m.Value.Equals("this text chunk is missing a language tag"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("CompressedInternational2") && m.Value.Equals("這是一個考驗") && m.LanguageTag.Equals("chinese")); Assert.Contains(meta.TextData, m => m.Keyword.Equals("NoTranslatedKeyword") && m.Value.Equals("dieser chunk hat kein übersetztes Schlüßelwort"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("NoLang") && m.Value.Equals("this text chunk is missing a language tag"));
Assert.Contains(meta.TextData, m => m.Keyword.Equals("NoTranslatedKeyword") && m.Value.Equals("dieser chunk hat kein übersetztes Schlüßelwort"));
}
}
} }
[Theory] [Theory]
@ -105,16 +99,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decoder_IgnoresInvalidTextData<TPixel>(TestImageProvider<TPixel> provider) public void Decoder_IgnoresInvalidTextData<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new PngDecoder())) using Image<TPixel> image = provider.GetImage(new PngDecoder());
{ PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance); Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("leading space"));
Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("leading space")); Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("trailing space"));
Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("trailing space")); Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("space"));
Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("space")); Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("empty"));
Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("empty")); Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("invalid characters"));
Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("invalid characters")); Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("too large"));
Assert.DoesNotContain(meta.TextData, m => m.Value.Equals("too large"));
}
} }
[Theory] [Theory]
@ -123,30 +115,27 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
var decoder = new PngDecoder(); var decoder = new PngDecoder();
using (Image<TPixel> input = provider.GetImage(decoder)) using Image<TPixel> input = provider.GetImage(decoder);
using (var memoryStream = new MemoryStream()) using var memoryStream = new MemoryStream();
// This will be a zTXt chunk.
var expectedText = new PngTextData("large-text", new string('c', 100), string.Empty, string.Empty);
// This will be a iTXt chunk.
var expectedTextNoneLatin = new PngTextData("large-text-non-latin", new string('Ф', 100), "language-tag", "translated-keyword");
PngMetadata inputMetadata = input.Metadata.GetFormatMetadata(PngFormat.Instance);
inputMetadata.TextData.Add(expectedText);
inputMetadata.TextData.Add(expectedTextNoneLatin);
input.Save(memoryStream, new PngEncoder
{ {
// This will be a zTXt chunk. TextCompressionThreshold = 50
var expectedText = new PngTextData("large-text", new string('c', 100), string.Empty, string.Empty); });
// This will be a iTXt chunk. memoryStream.Position = 0;
var expectedTextNoneLatin = new PngTextData("large-text-non-latin", new string('Ф', 100), "language-tag", "translated-keyword"); using Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, memoryStream);
PngMetadata inputMetadata = input.Metadata.GetFormatMetadata(PngFormat.Instance); PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
inputMetadata.TextData.Add(expectedText); Assert.Contains(meta.TextData, m => m.Equals(expectedText));
inputMetadata.TextData.Add(expectedTextNoneLatin); Assert.Contains(meta.TextData, m => m.Equals(expectedTextNoneLatin));
input.Save(memoryStream, new PngEncoder
{
TextCompressionThreshold = 50
});
memoryStream.Position = 0;
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, memoryStream))
{
PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
Assert.Contains(meta.TextData, m => m.Equals(expectedText));
Assert.Contains(meta.TextData, m => m.Equals(expectedTextNoneLatin));
}
}
} }
[Fact] [Fact]
@ -159,15 +148,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
var testFile = TestFile.Create(TestImages.Png.Blur); var testFile = TestFile.Create(TestImages.Png.Blur);
using (Image<Rgba32> image = testFile.CreateRgba32Image(options)) using Image<Rgba32> image = testFile.CreateRgba32Image(options);
{ PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
Assert.Equal(1, meta.TextData.Count); Assert.Equal(1, meta.TextData.Count);
Assert.Equal("Software", meta.TextData[0].Keyword); Assert.Equal("Software", meta.TextData[0].Keyword);
Assert.Equal("paint.net 4.0.6", meta.TextData[0].Value); Assert.Equal("paint.net 4.0.6", meta.TextData[0].Value);
Assert.Equal(0.4545d, meta.Gamma, precision: 4); Assert.Equal(0.4545d, meta.Gamma, precision: 4);
}
} }
[Fact] [Fact]
@ -180,11 +167,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
var testFile = TestFile.Create(TestImages.Png.Blur); var testFile = TestFile.Create(TestImages.Png.Blur);
using (Image<Rgba32> image = testFile.CreateRgba32Image(options)) using Image<Rgba32> image = testFile.CreateRgba32Image(options);
{ PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance); Assert.Equal(0, meta.TextData.Count);
Assert.Equal(0, meta.TextData.Count);
}
} }
[Theory] [Theory]
@ -192,17 +177,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ var decoder = new PngDecoder();
var decoder = new PngDecoder(); using Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream);
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream)) ImageMetadata meta = image.Metadata;
{ Assert.Equal(xResolution, meta.HorizontalResolution);
ImageMetadata meta = image.Metadata; Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(xResolution, meta.HorizontalResolution); Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
}
} }
[Theory] [Theory]
@ -210,15 +191,13 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
{ {
var testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false)) using var stream = new MemoryStream(testFile.Bytes, false);
{ var decoder = new PngDecoder();
var decoder = new PngDecoder(); IImageInfo image = decoder.Identify(Configuration.Default, stream);
IImageInfo image = decoder.Identify(Configuration.Default, stream); ImageMetadata meta = image.Metadata;
ImageMetadata meta = image.Metadata; Assert.Equal(xResolution, meta.HorizontalResolution);
Assert.Equal(xResolution, meta.HorizontalResolution); Assert.Equal(yResolution, meta.VerticalResolution);
Assert.Equal(yResolution, meta.VerticalResolution); Assert.Equal(resolutionUnit, meta.ResolutionUnits);
Assert.Equal(resolutionUnit, meta.ResolutionUnits);
}
} }
} }
} }

44
tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs

@ -18,19 +18,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
// does saving a file then reopening mean both files are identical??? // does saving a file then reopening mean both files are identical???
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
using (var ms = new MemoryStream()) using var ms = new MemoryStream();
{
// image.Save(provider.Utility.GetTestOutputFileName("bmp"));
image.Save(ms, new PngEncoder());
ms.Position = 0;
using (var img2 = Image.Load<Rgba32>(ms, new PngDecoder()))
{
ImageComparer.Tolerant().VerifySimilarity(image, img2);
// img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder()); // image.Save(provider.Utility.GetTestOutputFileName("bmp"));
} image.Save(ms, new PngEncoder());
} ms.Position = 0;
using var img2 = Image.Load<Rgba32>(ms, new PngDecoder());
ImageComparer.Tolerant().VerifySimilarity(image, img2);
// img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder());
} }
/* JJS: Disabled for now as the decoder now correctly decodes the full pixel components if the /* JJS: Disabled for now as the decoder now correctly decodes the full pixel components if the
@ -103,20 +100,17 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
// does saving a file then reopening mean both files are identical??? // does saving a file then reopening mean both files are identical???
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
using (var ms = new MemoryStream()) using var ms = new MemoryStream();
{
// image.Save(provider.Utility.GetTestOutputFileName("png"));
image.Mutate(x => x.Resize(100, 100));
// image.Save(provider.Utility.GetTestOutputFileName("png", "resize")); // image.Save(provider.Utility.GetTestOutputFileName("png"));
image.Save(ms, new PngEncoder()); image.Mutate(x => x.Resize(100, 100));
ms.Position = 0;
using (var img2 = Image.Load<Rgba32>(ms, new PngDecoder())) // image.Save(provider.Utility.GetTestOutputFileName("png", "resize"));
{ image.Save(ms, new PngEncoder());
ImageComparer.Tolerant().VerifySimilarity(image, img2); ms.Position = 0;
} using var img2 = Image.Load<Rgba32>(ms, new PngDecoder());
} ImageComparer.Tolerant().VerifySimilarity(image, img2);
} }
} }
} }

120
tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs

@ -18,11 +18,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_Uncompressed_MonoChrome<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_Uncompressed_MonoChrome<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -30,11 +28,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_Uncompressed_15Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_Uncompressed_15Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -42,11 +38,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_RunLengthEncoded_15Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_15Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -54,11 +48,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_Uncompressed_16Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_Uncompressed_16Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -66,11 +58,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_RunLengthEncoded_WithPalette_16Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_WithPalette_16Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -78,11 +68,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_Uncompressed_24Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_Uncompressed_24Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -90,11 +78,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_RunLengthEncoded_WithTopLeftOrigin_24Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_WithTopLeftOrigin_24Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -102,11 +88,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_Palette_WithTopLeftOrigin_24Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_Palette_WithTopLeftOrigin_24Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -114,11 +98,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_Uncompressed_32Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_Uncompressed_32Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -126,11 +108,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_RunLengthEncoded_MonoChrome<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_MonoChrome<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -138,11 +118,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_RunLengthEncoded_16Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_16Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -150,11 +128,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_RunLengthEncoded_24Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_24Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -162,11 +138,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_RunLengthEncoded_32Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_RunLengthEncoded_32Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -174,11 +148,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_WithPalette_16Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_WithPalette_16Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
[Theory] [Theory]
@ -186,11 +158,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public void TgaDecoder_CanDecode_WithPalette_24Bit<TPixel>(TestImageProvider<TPixel> provider) public void TgaDecoder_CanDecode_WithPalette_24Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage(new TgaDecoder())) using Image<TPixel> image = provider.GetImage(new TgaDecoder());
{ image.DebugSave(provider);
image.DebugSave(provider); TgaTestUtils.CompareWithReferenceDecoder(provider, image);
TgaTestUtils.CompareWithReferenceDecoder(provider, image);
}
} }
} }
} }

66
tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs

@ -37,20 +37,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
{ {
var options = new TgaEncoder(); var options = new TgaEncoder();
TestFile testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{ memStream.Position = 0;
input.Save(memStream, options); using var output = Image.Load<Rgba32>(memStream);
memStream.Position = 0; TgaMetadata meta = output.Metadata.GetTgaMetadata();
using (Image<Rgba32> output = Image.Load<Rgba32>(memStream)) Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel);
{
TgaMetadata meta = output.Metadata.GetTgaMetadata();
Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel);
}
}
}
} }
[Theory] [Theory]
@ -62,20 +56,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
Compression = TgaCompression.RunLength Compression = TgaCompression.RunLength
}; };
TestFile testFile = TestFile.Create(imagePath); var testFile = TestFile.Create(imagePath);
using (Image<Rgba32> input = testFile.CreateRgba32Image()) using Image<Rgba32> input = testFile.CreateRgba32Image();
{ using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) input.Save(memStream, options);
{ memStream.Position = 0;
input.Save(memStream, options); using var output = Image.Load<Rgba32>(memStream);
memStream.Position = 0; TgaMetadata meta = output.Metadata.GetTgaMetadata();
using (var output = Image.Load<Rgba32>(memStream)) Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel);
{
TgaMetadata meta = output.Metadata.GetTgaMetadata();
Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel);
}
}
}
} }
[Theory] [Theory]
@ -130,20 +118,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
float compareTolerance = 0.01f) float compareTolerance = 0.01f)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ var encoder = new TgaEncoder { BitsPerPixel = bitsPerPixel, Compression = compression };
var encoder = new TgaEncoder { BitsPerPixel = bitsPerPixel, Compression = compression };
using var memStream = new MemoryStream();
using (var memStream = new MemoryStream()) image.Save(memStream, encoder);
{ memStream.Position = 0;
image.Save(memStream, encoder); using var encodedImage = (Image<TPixel>)Image.Load(memStream);
memStream.Position = 0; TgaTestUtils.CompareWithReferenceDecoder(provider, encodedImage, useExactComparer, compareTolerance);
using (var encodedImage = (Image<TPixel>)Image.Load(memStream))
{
TgaTestUtils.CompareWithReferenceDecoder(provider, encodedImage, useExactComparer, compareTolerance);
}
}
}
} }
} }
} }

28
tests/ImageSharp.Tests/Formats/Tga/TgaTestUtils.cs

@ -42,24 +42,22 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
public static Image<TPixel> DecodeWithMagick<TPixel>(Configuration configuration, FileInfo fileInfo) public static Image<TPixel> DecodeWithMagick<TPixel>(Configuration configuration, FileInfo fileInfo)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (var magickImage = new MagickImage(fileInfo)) using var magickImage = new MagickImage(fileInfo);
{ var result = new Image<TPixel>(configuration, magickImage.Width, magickImage.Height);
var result = new Image<TPixel>(configuration, magickImage.Width, magickImage.Height); Span<TPixel> resultPixels = result.GetPixelSpan();
Span<TPixel> resultPixels = result.GetPixelSpan();
using (IPixelCollection pixels = magickImage.GetPixelsUnsafe())
{
byte[] data = pixels.ToByteArray(PixelMapping.RGBA);
PixelOperations<TPixel>.Instance.FromRgba32Bytes( using (IPixelCollection pixels = magickImage.GetPixelsUnsafe())
configuration, {
data, byte[] data = pixels.ToByteArray(PixelMapping.RGBA);
resultPixels,
resultPixels.Length);
}
return result; PixelOperations<TPixel>.Instance.FromRgba32Bytes(
configuration,
data,
resultPixels,
resultPixels.Length);
} }
return result;
} }
} }
} }

60
tests/ImageSharp.Tests/Helpers/ParallelHelperTests.cs

@ -297,43 +297,41 @@ namespace SixLabors.ImageSharp.Tests.Helpers
{ {
MemoryAllocator memoryAllocator = Configuration.Default.MemoryAllocator; MemoryAllocator memoryAllocator = Configuration.Default.MemoryAllocator;
using (Buffer2D<Point> expected = memoryAllocator.Allocate2D<Point>(bufferWidth, bufferHeight, AllocationOptions.Clean)) using Buffer2D<Point> expected = memoryAllocator.Allocate2D<Point>(bufferWidth, bufferHeight, AllocationOptions.Clean);
using (Buffer2D<Point> actual = memoryAllocator.Allocate2D<Point>(bufferWidth, bufferHeight, AllocationOptions.Clean)) using Buffer2D<Point> actual = memoryAllocator.Allocate2D<Point>(bufferWidth, bufferHeight, AllocationOptions.Clean);
{ var rect = new Rectangle(rectX, rectY, rectWidth, rectHeight);
var rect = new Rectangle(rectX, rectY, rectWidth, rectHeight);
void FillRow(int y, Buffer2D<Point> buffer) void FillRow(int y, Buffer2D<Point> buffer)
{
for (int x = rect.Left; x < rect.Right; x++)
{ {
for (int x = rect.Left; x < rect.Right; x++) buffer[x, y] = new Point(x, y);
{
buffer[x, y] = new Point(x, y);
}
} }
}
// Fill Expected data: // Fill Expected data:
for (int y = rectY; y < rect.Bottom; y++) for (int y = rectY; y < rect.Bottom; y++)
{ {
FillRow(y, expected); FillRow(y, expected);
} }
// Fill actual data using IterateRows: // Fill actual data using IterateRows:
var settings = new ParallelExecutionSettings(maxDegreeOfParallelism, memoryAllocator); var settings = new ParallelExecutionSettings(maxDegreeOfParallelism, memoryAllocator);
ParallelHelper.IterateRows( ParallelHelper.IterateRows(
rect, rect,
settings, settings,
rows => rows =>
{ {
this.output.WriteLine(rows.ToString()); this.output.WriteLine(rows.ToString());
for (int y = rows.Min; y < rows.Max; y++) for (int y = rows.Min; y < rows.Max; y++)
{ {
FillRow(y, actual); FillRow(y, actual);
} }
}); });
// Assert: // Assert:
TestImageExtensions.CompareBuffers(expected.GetSpan(), actual.GetSpan()); TestImageExtensions.CompareBuffers(expected.GetSpan(), actual.GetSpan());
}
} }
[Theory] [Theory]

18
tests/ImageSharp.Tests/Helpers/RowIntervalTests.cs

@ -19,20 +19,18 @@ namespace SixLabors.ImageSharp.Tests.Helpers
[InlineData(10, 20, 0, 1)] [InlineData(10, 20, 0, 1)]
public void GetMultiRowSpan(int width, int height, int min, int max) public void GetMultiRowSpan(int width, int height, int min, int max)
{ {
using (Buffer2D<int> buffer = Configuration.Default.MemoryAllocator.Allocate2D<int>(width, height)) using Buffer2D<int> buffer = Configuration.Default.MemoryAllocator.Allocate2D<int>(width, height);
{ var rows = new RowInterval(min, max);
var rows = new RowInterval(min, max);
Span<int> span = buffer.GetMultiRowSpan(rows); Span<int> span = buffer.GetMultiRowSpan(rows);
ref int expected0 = ref buffer.GetSpan()[min * width]; ref int expected0 = ref buffer.GetSpan()[min * width];
int expectedLength = (max - min) * width; int expectedLength = (max - min) * width;
ref int actual0 = ref span[0]; ref int actual0 = ref span[0];
Assert.Equal(span.Length, expectedLength); Assert.Equal(span.Length, expectedLength);
Assert.True(Unsafe.AreSame(ref expected0, ref actual0)); Assert.True(Unsafe.AreSame(ref expected0, ref actual0));
}
} }
[Fact] [Fact]

190
tests/ImageSharp.Tests/IO/DoubleBufferedStreamReaderTests.cs

@ -16,64 +16,58 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact] [Fact]
public void DoubleBufferedStreamReaderCanReadSingleByteFromOrigin() public void DoubleBufferedStreamReaderCanReadSingleByteFromOrigin()
{ {
using (MemoryStream stream = this.CreateTestStream()) using MemoryStream stream = this.CreateTestStream();
{ byte[] expected = stream.ToArray();
byte[] expected = stream.ToArray(); var reader = new DoubleBufferedStreamReader(this.allocator, stream);
var reader = new DoubleBufferedStreamReader(this.allocator, stream);
Assert.Equal(expected[0], reader.ReadByte()); Assert.Equal(expected[0], reader.ReadByte());
// We've read a whole chunk but increment by 1 in our reader. // We've read a whole chunk but increment by 1 in our reader.
Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength); Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength);
Assert.Equal(1, reader.Position); Assert.Equal(1, reader.Position);
}
} }
[Fact] [Fact]
public void DoubleBufferedStreamReaderCanReadSingleByteFromOffset() public void DoubleBufferedStreamReaderCanReadSingleByteFromOffset()
{ {
using (MemoryStream stream = this.CreateTestStream()) using MemoryStream stream = this.CreateTestStream();
{ byte[] expected = stream.ToArray();
byte[] expected = stream.ToArray(); const int offset = 5;
const int offset = 5; var reader = new DoubleBufferedStreamReader(this.allocator, stream);
var reader = new DoubleBufferedStreamReader(this.allocator, stream); reader.Position = offset;
reader.Position = offset;
Assert.Equal(expected[offset], reader.ReadByte()); Assert.Equal(expected[offset], reader.ReadByte());
// We've read a whole chunk but increment by 1 in our reader. // We've read a whole chunk but increment by 1 in our reader.
Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength + offset); Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength + offset);
Assert.Equal(offset + 1, reader.Position); Assert.Equal(offset + 1, reader.Position);
}
} }
[Fact] [Fact]
public void DoubleBufferedStreamReaderCanReadSubsequentSingleByteCorrectly() public void DoubleBufferedStreamReaderCanReadSubsequentSingleByteCorrectly()
{ {
using (MemoryStream stream = this.CreateTestStream()) using MemoryStream stream = this.CreateTestStream();
byte[] expected = stream.ToArray();
var reader = new DoubleBufferedStreamReader(this.allocator, stream);
for (int i = 0; i < expected.Length; i++)
{ {
byte[] expected = stream.ToArray(); Assert.Equal(expected[i], reader.ReadByte());
var reader = new DoubleBufferedStreamReader(this.allocator, stream); Assert.Equal(i + 1, reader.Position);
for (int i = 0; i < expected.Length; i++) if (i < DoubleBufferedStreamReader.ChunkLength)
{ {
Assert.Equal(expected[i], reader.ReadByte()); Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength);
Assert.Equal(i + 1, reader.Position); }
else if (i >= DoubleBufferedStreamReader.ChunkLength && i < DoubleBufferedStreamReader.ChunkLength * 2)
if (i < DoubleBufferedStreamReader.ChunkLength) {
{ // We should have advanced to the second chunk now.
Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength); Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 2);
} }
else if (i >= DoubleBufferedStreamReader.ChunkLength && i < DoubleBufferedStreamReader.ChunkLength * 2) else
{ {
// We should have advanced to the second chunk now. // We should have advanced to the third chunk now.
Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 2); Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 3);
}
else
{
// We should have advanced to the third chunk now.
Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 3);
}
} }
} }
} }
@ -81,53 +75,49 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact] [Fact]
public void DoubleBufferedStreamReaderCanReadMultipleBytesFromOrigin() public void DoubleBufferedStreamReaderCanReadMultipleBytesFromOrigin()
{ {
using (MemoryStream stream = this.CreateTestStream()) using MemoryStream stream = this.CreateTestStream();
{ var buffer = new byte[2];
var buffer = new byte[2]; byte[] expected = stream.ToArray();
byte[] expected = stream.ToArray(); var reader = new DoubleBufferedStreamReader(this.allocator, stream);
var reader = new DoubleBufferedStreamReader(this.allocator, stream);
Assert.Equal(2, reader.Read(buffer, 0, 2));
Assert.Equal(2, reader.Read(buffer, 0, 2)); Assert.Equal(expected[0], buffer[0]);
Assert.Equal(expected[0], buffer[0]); Assert.Equal(expected[1], buffer[1]);
Assert.Equal(expected[1], buffer[1]);
// We've read a whole chunk but increment by the buffer length in our reader.
// We've read a whole chunk but increment by the buffer length in our reader. Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength);
Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength); Assert.Equal(buffer.Length, reader.Position);
Assert.Equal(buffer.Length, reader.Position);
}
} }
[Fact] [Fact]
public void DoubleBufferedStreamReaderCanReadSubsequentMultipleByteCorrectly() public void DoubleBufferedStreamReaderCanReadSubsequentMultipleByteCorrectly()
{ {
using (MemoryStream stream = this.CreateTestStream()) using MemoryStream stream = this.CreateTestStream();
var buffer = new byte[2];
byte[] expected = stream.ToArray();
var reader = new DoubleBufferedStreamReader(this.allocator, stream);
for (int i = 0, o = 0; i < expected.Length / 2; i++, o += 2)
{ {
var buffer = new byte[2]; Assert.Equal(2, reader.Read(buffer, 0, 2));
byte[] expected = stream.ToArray(); Assert.Equal(expected[o], buffer[0]);
var reader = new DoubleBufferedStreamReader(this.allocator, stream); Assert.Equal(expected[o + 1], buffer[1]);
Assert.Equal(o + 2, reader.Position);
for (int i = 0, o = 0; i < expected.Length / 2; i++, o += 2) int offset = i * 2;
if (offset < DoubleBufferedStreamReader.ChunkLength)
{ {
Assert.Equal(2, reader.Read(buffer, 0, 2)); Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength);
Assert.Equal(expected[o], buffer[0]); }
Assert.Equal(expected[o + 1], buffer[1]); else if (offset >= DoubleBufferedStreamReader.ChunkLength && offset < DoubleBufferedStreamReader.ChunkLength * 2)
Assert.Equal(o + 2, reader.Position); {
// We should have advanced to the second chunk now.
int offset = i * 2; Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 2);
if (offset < DoubleBufferedStreamReader.ChunkLength) }
{ else
Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength); {
} // We should have advanced to the third chunk now.
else if (offset >= DoubleBufferedStreamReader.ChunkLength && offset < DoubleBufferedStreamReader.ChunkLength * 2) Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 3);
{
// We should have advanced to the second chunk now.
Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 2);
}
else
{
// We should have advanced to the third chunk now.
Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 3);
}
} }
} }
} }
@ -135,33 +125,31 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact] [Fact]
public void DoubleBufferedStreamReaderCanSkip() public void DoubleBufferedStreamReaderCanSkip()
{ {
using (MemoryStream stream = this.CreateTestStream()) using MemoryStream stream = this.CreateTestStream();
{ byte[] expected = stream.ToArray();
byte[] expected = stream.ToArray(); var reader = new DoubleBufferedStreamReader(this.allocator, stream);
var reader = new DoubleBufferedStreamReader(this.allocator, stream);
int skip = 50; int skip = 50;
int plusOne = 1; int plusOne = 1;
int skip2 = DoubleBufferedStreamReader.ChunkLength; int skip2 = DoubleBufferedStreamReader.ChunkLength;
// Skip // Skip
reader.Skip(skip); reader.Skip(skip);
Assert.Equal(skip, reader.Position); Assert.Equal(skip, reader.Position);
Assert.Equal(stream.Position, reader.Position); Assert.Equal(stream.Position, reader.Position);
// Read // Read
Assert.Equal(expected[skip], reader.ReadByte()); Assert.Equal(expected[skip], reader.ReadByte());
// Skip Again // Skip Again
reader.Skip(skip2); reader.Skip(skip2);
// First Skip + First Read + Second Skip // First Skip + First Read + Second Skip
int position = skip + plusOne + skip2; int position = skip + plusOne + skip2;
Assert.Equal(position, reader.Position); Assert.Equal(position, reader.Position);
Assert.Equal(stream.Position, reader.Position); Assert.Equal(stream.Position, reader.Position);
Assert.Equal(expected[position], reader.ReadByte()); Assert.Equal(expected[position], reader.ReadByte());
}
} }
private MemoryStream CreateTestStream() private MemoryStream CreateTestStream()

116
tests/ImageSharp.Tests/Image/ImageCloneTests.cs

@ -32,24 +32,22 @@ namespace SixLabors.ImageSharp.Tests
[WithTestPatternImage(9, 9, PixelTypes.Rgba32)] [WithTestPatternImage(9, 9, PixelTypes.Rgba32)]
public void CloneAs_ToBgra32(TestImageProvider<Rgba32> provider) public void CloneAs_ToBgra32(TestImageProvider<Rgba32> provider)
{ {
using (Image<Rgba32> image = provider.GetImage()) using Image<Rgba32> image = provider.GetImage();
using (Image<Bgra32> clone = image.CloneAs<Bgra32>()) using Image<Bgra32> clone = image.CloneAs<Bgra32>();
for (int y = 0; y < image.Height; y++)
{ {
for (int y = 0; y < image.Height; y++) Span<Rgba32> row = image.GetPixelRowSpan(y);
Span<Bgra32> rowClone = clone.GetPixelRowSpan(y);
for (int x = 0; x < image.Width; x++)
{ {
Span<Rgba32> row = image.GetPixelRowSpan(y); Rgba32 expected = row[x];
Span<Bgra32> rowClone = clone.GetPixelRowSpan(y); Bgra32 actual = rowClone[x];
for (int x = 0; x < image.Width; x++) Assert.Equal(expected.R, actual.R);
{ Assert.Equal(expected.G, actual.G);
Rgba32 expected = row[x]; Assert.Equal(expected.B, actual.B);
Bgra32 actual = rowClone[x]; Assert.Equal(expected.A, actual.A);
Assert.Equal(expected.R, actual.R);
Assert.Equal(expected.G, actual.G);
Assert.Equal(expected.B, actual.B);
Assert.Equal(expected.A, actual.A);
}
} }
} }
} }
@ -58,23 +56,21 @@ namespace SixLabors.ImageSharp.Tests
[WithTestPatternImage(9, 9, PixelTypes.Rgba32)] [WithTestPatternImage(9, 9, PixelTypes.Rgba32)]
public void CloneAs_ToBgr24(TestImageProvider<Rgba32> provider) public void CloneAs_ToBgr24(TestImageProvider<Rgba32> provider)
{ {
using (Image<Rgba32> image = provider.GetImage()) using Image<Rgba32> image = provider.GetImage();
using (Image<Bgr24> clone = image.CloneAs<Bgr24>()) using Image<Bgr24> clone = image.CloneAs<Bgr24>();
for (int y = 0; y < image.Height; y++)
{ {
for (int y = 0; y < image.Height; y++) Span<Rgba32> row = image.GetPixelRowSpan(y);
Span<Bgr24> rowClone = clone.GetPixelRowSpan(y);
for (int x = 0; x < image.Width; x++)
{ {
Span<Rgba32> row = image.GetPixelRowSpan(y); Rgba32 expected = row[x];
Span<Bgr24> rowClone = clone.GetPixelRowSpan(y); Bgr24 actual = rowClone[x];
for (int x = 0; x < image.Width; x++) Assert.Equal(expected.R, actual.R);
{ Assert.Equal(expected.G, actual.G);
Rgba32 expected = row[x]; Assert.Equal(expected.B, actual.B);
Bgr24 actual = rowClone[x];
Assert.Equal(expected.R, actual.R);
Assert.Equal(expected.G, actual.G);
Assert.Equal(expected.B, actual.B);
}
} }
} }
} }
@ -83,24 +79,22 @@ namespace SixLabors.ImageSharp.Tests
[WithTestPatternImage(9, 9, PixelTypes.Rgba32)] [WithTestPatternImage(9, 9, PixelTypes.Rgba32)]
public void CloneAs_ToArgb32(TestImageProvider<Rgba32> provider) public void CloneAs_ToArgb32(TestImageProvider<Rgba32> provider)
{ {
using (Image<Rgba32> image = provider.GetImage()) using Image<Rgba32> image = provider.GetImage();
using (Image<Argb32> clone = image.CloneAs<Argb32>()) using Image<Argb32> clone = image.CloneAs<Argb32>();
for (int y = 0; y < image.Height; y++)
{ {
for (int y = 0; y < image.Height; y++) Span<Rgba32> row = image.GetPixelRowSpan(y);
Span<Argb32> rowClone = clone.GetPixelRowSpan(y);
for (int x = 0; x < image.Width; x++)
{ {
Span<Rgba32> row = image.GetPixelRowSpan(y); Rgba32 expected = row[x];
Span<Argb32> rowClone = clone.GetPixelRowSpan(y); Argb32 actual = rowClone[x];
for (int x = 0; x < image.Width; x++) Assert.Equal(expected.R, actual.R);
{ Assert.Equal(expected.G, actual.G);
Rgba32 expected = row[x]; Assert.Equal(expected.B, actual.B);
Argb32 actual = rowClone[x]; Assert.Equal(expected.A, actual.A);
Assert.Equal(expected.R, actual.R);
Assert.Equal(expected.G, actual.G);
Assert.Equal(expected.B, actual.B);
Assert.Equal(expected.A, actual.A);
}
} }
} }
} }
@ -109,23 +103,21 @@ namespace SixLabors.ImageSharp.Tests
[WithTestPatternImage(9, 9, PixelTypes.Rgba32)] [WithTestPatternImage(9, 9, PixelTypes.Rgba32)]
public void CloneAs_ToRgb24(TestImageProvider<Rgba32> provider) public void CloneAs_ToRgb24(TestImageProvider<Rgba32> provider)
{ {
using (Image<Rgba32> image = provider.GetImage()) using Image<Rgba32> image = provider.GetImage();
using (Image<Rgb24> clone = image.CloneAs<Rgb24>()) using Image<Rgb24> clone = image.CloneAs<Rgb24>();
for (int y = 0; y < image.Height; y++)
{ {
for (int y = 0; y < image.Height; y++) Span<Rgba32> row = image.GetPixelRowSpan(y);
Span<Rgb24> rowClone = clone.GetPixelRowSpan(y);
for (int x = 0; x < image.Width; x++)
{ {
Span<Rgba32> row = image.GetPixelRowSpan(y); Rgba32 expected = row[x];
Span<Rgb24> rowClone = clone.GetPixelRowSpan(y); Rgb24 actual = rowClone[x];
for (int x = 0; x < image.Width; x++) Assert.Equal(expected.R, actual.R);
{ Assert.Equal(expected.G, actual.G);
Rgba32 expected = row[x]; Assert.Equal(expected.B, actual.B);
Rgb24 actual = rowClone[x];
Assert.Equal(expected.R, actual.R);
Assert.Equal(expected.G, actual.G);
Assert.Equal(expected.B, actual.B);
}
} }
} }
} }

30
tests/ImageSharp.Tests/Image/ImageFrameCollectionTests.Generic.cs

@ -192,15 +192,11 @@ namespace SixLabors.ImageSharp.Tests
public void CloneFrame<TPixel>(TestImageProvider<TPixel> provider) public void CloneFrame<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> img = provider.GetImage()) using Image<TPixel> img = provider.GetImage();
{ img.Frames.AddFrame(new ImageFrame<TPixel>(Configuration.Default, 10, 10)); // add a frame anyway
img.Frames.AddFrame(new ImageFrame<TPixel>(Configuration.Default, 10, 10)); // add a frame anyway using Image<TPixel> cloned = img.Frames.CloneFrame(0);
using (Image<TPixel> cloned = img.Frames.CloneFrame(0)) Assert.Equal(2, img.Frames.Count);
{ cloned.ComparePixelBufferTo(img.GetPixelSpan());
Assert.Equal(2, img.Frames.Count);
cloned.ComparePixelBufferTo(img.GetPixelSpan());
}
}
} }
[Theory] [Theory]
@ -208,17 +204,13 @@ namespace SixLabors.ImageSharp.Tests
public void ExtractFrame<TPixel>(TestImageProvider<TPixel> provider) public void ExtractFrame<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> img = provider.GetImage()) using Image<TPixel> img = provider.GetImage();
{ var sourcePixelData = img.GetPixelSpan().ToArray();
var sourcePixelData = img.GetPixelSpan().ToArray();
img.Frames.AddFrame(new ImageFrame<TPixel>(Configuration.Default, 10, 10)); img.Frames.AddFrame(new ImageFrame<TPixel>(Configuration.Default, 10, 10));
using (Image<TPixel> cloned = img.Frames.ExportFrame(0)) using Image<TPixel> cloned = img.Frames.ExportFrame(0);
{ Assert.Equal(1, img.Frames.Count);
Assert.Equal(1, img.Frames.Count); cloned.ComparePixelBufferTo(sourcePixelData);
cloned.ComparePixelBufferTo(sourcePixelData);
}
}
} }
[Fact] [Fact]

81
tests/ImageSharp.Tests/Image/ImageFrameCollectionTests.NonGeneric.cs

@ -148,20 +148,16 @@ namespace SixLabors.ImageSharp.Tests
public void CloneFrame<TPixel>(TestImageProvider<TPixel> provider) public void CloneFrame<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> img = provider.GetImage()) using Image<TPixel> img = provider.GetImage();
{ ImageFrameCollection nonGenericFrameCollection = img.Frames;
ImageFrameCollection nonGenericFrameCollection = img.Frames;
nonGenericFrameCollection.AddFrame(new ImageFrame<TPixel>(Configuration.Default, 10, 10)); // add a frame anyway nonGenericFrameCollection.AddFrame(new ImageFrame<TPixel>(Configuration.Default, 10, 10)); // add a frame anyway
using (Image cloned = nonGenericFrameCollection.CloneFrame(0)) using Image cloned = nonGenericFrameCollection.CloneFrame(0);
{ Assert.Equal(2, img.Frames.Count);
Assert.Equal(2, img.Frames.Count);
var expectedClone = (Image<TPixel>)cloned; var expectedClone = (Image<TPixel>)cloned;
expectedClone.ComparePixelBufferTo(img.GetPixelSpan()); expectedClone.ComparePixelBufferTo(img.GetPixelSpan());
}
}
} }
[Theory] [Theory]
@ -169,21 +165,17 @@ namespace SixLabors.ImageSharp.Tests
public void ExtractFrame<TPixel>(TestImageProvider<TPixel> provider) public void ExtractFrame<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> img = provider.GetImage()) using Image<TPixel> img = provider.GetImage();
{ var sourcePixelData = img.GetPixelSpan().ToArray();
var sourcePixelData = img.GetPixelSpan().ToArray();
ImageFrameCollection nonGenericFrameCollection = img.Frames; ImageFrameCollection nonGenericFrameCollection = img.Frames;
nonGenericFrameCollection.AddFrame(new ImageFrame<TPixel>(Configuration.Default, 10, 10)); nonGenericFrameCollection.AddFrame(new ImageFrame<TPixel>(Configuration.Default, 10, 10));
using (Image cloned = nonGenericFrameCollection.ExportFrame(0)) using Image cloned = nonGenericFrameCollection.ExportFrame(0);
{ Assert.Equal(1, img.Frames.Count);
Assert.Equal(1, img.Frames.Count);
var expectedClone = (Image<TPixel>)cloned; var expectedClone = (Image<TPixel>)cloned;
expectedClone.ComparePixelBufferTo(sourcePixelData); expectedClone.ComparePixelBufferTo(sourcePixelData);
}
}
} }
[Fact] [Fact]
@ -270,39 +262,34 @@ namespace SixLabors.ImageSharp.Tests
public void ConstructGif_FromDifferentPixelTypes<TPixel>(TestImageProvider<TPixel> provider) public void ConstructGif_FromDifferentPixelTypes<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image source = provider.GetImage()) using Image source = provider.GetImage();
using (var dest = new Image<TPixel>(source.GetConfiguration(), source.Width, source.Height)) using var dest = new Image<TPixel>(source.GetConfiguration(), source.Width, source.Height);
{
// Giphy.gif has 5 frames
ImportFrameAs<Bgra32>(source.Frames, dest.Frames, 0);
ImportFrameAs<Argb32>(source.Frames, dest.Frames, 1);
ImportFrameAs<Rgba64>(source.Frames, dest.Frames, 2);
ImportFrameAs<Rgba32>(source.Frames, dest.Frames, 3);
ImportFrameAs<Bgra32>(source.Frames, dest.Frames, 4);
// Drop the original empty root frame: // Giphy.gif has 5 frames
dest.Frames.RemoveFrame(0); ImportFrameAs<Bgra32>(source.Frames, dest.Frames, 0);
ImportFrameAs<Argb32>(source.Frames, dest.Frames, 1);
ImportFrameAs<Rgba64>(source.Frames, dest.Frames, 2);
ImportFrameAs<Rgba32>(source.Frames, dest.Frames, 3);
ImportFrameAs<Bgra32>(source.Frames, dest.Frames, 4);
dest.DebugSave(provider, appendSourceFileOrDescription: false, extension: "gif"); // Drop the original empty root frame:
dest.CompareToOriginal(provider); dest.Frames.RemoveFrame(0);
for (int i = 0; i < 5; i++) dest.DebugSave(provider, appendSourceFileOrDescription: false, extension: "gif");
{ dest.CompareToOriginal(provider);
CompareGifMetadata(source.Frames[i], dest.Frames[i]);
} for (int i = 0; i < 5; i++)
{
CompareGifMetadata(source.Frames[i], dest.Frames[i]);
} }
} }
private static void ImportFrameAs<TPixel>(ImageFrameCollection source, ImageFrameCollection destination, int index) private static void ImportFrameAs<TPixel>(ImageFrameCollection source, ImageFrameCollection destination, int index)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image temp = source.CloneFrame(index)) using Image temp = source.CloneFrame(index);
{ using Image<TPixel> temp2 = temp.CloneAs<TPixel>();
using (Image<TPixel> temp2 = temp.CloneAs<TPixel>()) destination.AddFrame(temp2.Frames.RootFrame);
{
destination.AddFrame(temp2.Frames.RootFrame);
}
}
} }
private static void CompareGifMetadata(ImageFrame a, ImageFrame b) private static void CompareGifMetadata(ImageFrame a, ImageFrame b)

12
tests/ImageSharp.Tests/Image/ImageRotationTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -48,12 +48,10 @@ namespace SixLabors.ImageSharp.Tests
private static (Size original, Size rotated) Rotate(int angle) private static (Size original, Size rotated) Rotate(int angle)
{ {
var file = TestFile.Create(TestImages.Bmp.Car); var file = TestFile.Create(TestImages.Bmp.Car);
using (var image = Image.Load<Rgba32>(file.FullPath)) using var image = Image.Load<Rgba32>(file.FullPath);
{ Size original = image.Size();
Size original = image.Size(); image.Mutate(x => x.Rotate(angle));
image.Mutate(x => x.Rotate(angle)); return (original, image.Size());
return (original, image.Size());
}
} }
} }
} }

8
tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs

@ -71,11 +71,9 @@ namespace SixLabors.ImageSharp.Tests
[Fact] [Fact]
public void FromStream_GlobalConfiguration() public void FromStream_GlobalConfiguration()
{ {
using (var stream = new MemoryStream(this.ActualImageBytes)) using var stream = new MemoryStream(this.ActualImageBytes);
{ IImageFormat type = Image.DetectFormat(stream);
IImageFormat type = Image.DetectFormat(stream); Assert.Equal(ExpectedGlobalFormat, type);
Assert.Equal(ExpectedGlobalFormat, type);
}
} }
[Fact] [Fact]

40
tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
@ -18,17 +18,15 @@ namespace SixLabors.ImageSharp.Tests
{ {
Rgba32[] data = { Rgba32.Black, Rgba32.White, Rgba32.White, Rgba32.Black, }; Rgba32[] data = { Rgba32.Black, Rgba32.White, Rgba32.White, Rgba32.Black, };
using (Image<Rgba32> img = useSpan using Image<Rgba32> img = useSpan
? Image.LoadPixelData<Rgba32>(data.AsSpan(), 2, 2) ? Image.LoadPixelData<Rgba32>(data.AsSpan(), 2, 2)
: Image.LoadPixelData<Rgba32>(data, 2, 2)) : Image.LoadPixelData<Rgba32>(data, 2, 2);
{ Assert.NotNull(img);
Assert.NotNull(img); Assert.Equal(Rgba32.Black, img[0, 0]);
Assert.Equal(Rgba32.Black, img[0, 0]); Assert.Equal(Rgba32.White, img[0, 1]);
Assert.Equal(Rgba32.White, img[0, 1]);
Assert.Equal(Rgba32.White, img[1, 0]); Assert.Equal(Rgba32.White, img[1, 0]);
Assert.Equal(Rgba32.Black, img[1, 1]); Assert.Equal(Rgba32.Black, img[1, 1]);
}
} }
[Theory] [Theory]
@ -43,18 +41,16 @@ namespace SixLabors.ImageSharp.Tests
255, 255, 255, 255, // 1,0 255, 255, 255, 255, // 1,0
0, 0, 0, 255, // 1,1 0, 0, 0, 255, // 1,1
}; };
using (Image<Rgba32> img = useSpan using Image<Rgba32> img = useSpan
? Image.LoadPixelData<Rgba32>(data.AsSpan(), 2, 2) ? Image.LoadPixelData<Rgba32>(data.AsSpan(), 2, 2)
: Image.LoadPixelData<Rgba32>(data, 2, 2)) : Image.LoadPixelData<Rgba32>(data, 2, 2);
{ Assert.NotNull(img);
Assert.NotNull(img); Assert.Equal(Rgba32.Black, img[0, 0]);
Assert.Equal(Rgba32.Black, img[0, 0]); Assert.Equal(Rgba32.White, img[0, 1]);
Assert.Equal(Rgba32.White, img[0, 1]);
Assert.Equal(Rgba32.White, img[1, 0]); Assert.Equal(Rgba32.White, img[1, 0]);
Assert.Equal(Rgba32.Black, img[1, 1]); Assert.Equal(Rgba32.Black, img[1, 1]);
}
} }
} }
} }
} }

40
tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs

@ -26,57 +26,45 @@ namespace SixLabors.ImageSharp.Tests
[Fact] [Fact]
public void Path_Specific() public void Path_Specific()
{ {
using (var img = Image.Load<Rgba32>(this.Path)) using var img = Image.Load<Rgba32>(this.Path);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Fact] [Fact]
public void Path_Agnostic() public void Path_Agnostic()
{ {
using (var img = Image.Load(this.Path)) using var img = Image.Load(this.Path);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Fact] [Fact]
public void Path_Decoder_Specific() public void Path_Decoder_Specific()
{ {
using (var img = Image.Load<Rgba32>(this.Path, new BmpDecoder())) using var img = Image.Load<Rgba32>(this.Path, new BmpDecoder());
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Fact] [Fact]
public void Path_Decoder_Agnostic() public void Path_Decoder_Agnostic()
{ {
using (var img = Image.Load(this.Path, new BmpDecoder())) using var img = Image.Load(this.Path, new BmpDecoder());
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Fact] [Fact]
public void Path_OutFormat_Specific() public void Path_OutFormat_Specific()
{ {
using (var img = Image.Load<Rgba32>(this.Path, out IImageFormat format)) using var img = Image.Load<Rgba32>(this.Path, out IImageFormat format);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img); Assert.IsType<BmpFormat>(format);
Assert.IsType<BmpFormat>(format);
}
} }
[Fact] [Fact]
public void Path_OutFormat_Agnostic() public void Path_OutFormat_Agnostic()
{ {
using (var img = Image.Load(this.Path, out IImageFormat format)) using var img = Image.Load(this.Path, out IImageFormat format);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img); Assert.IsType<BmpFormat>(format);
Assert.IsType<BmpFormat>(format);
}
} }
[Fact] [Fact]

40
tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs

@ -30,10 +30,8 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(true)] [InlineData(true)]
public void Bytes_Specific(bool useSpan) public void Bytes_Specific(bool useSpan)
{ {
using (var img = useSpan ? Image.Load<Rgba32>(ByteSpan) : Image.Load<Rgba32>(ByteArray)) using var img = useSpan ? Image.Load<Rgba32>(ByteSpan) : Image.Load<Rgba32>(ByteArray);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Theory] [Theory]
@ -41,10 +39,8 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(true)] [InlineData(true)]
public void Bytes_Agnostic(bool useSpan) public void Bytes_Agnostic(bool useSpan)
{ {
using (var img = useSpan ? Image.Load(ByteSpan) : Image.Load(ByteArray)) using var img = useSpan ? Image.Load(ByteSpan) : Image.Load(ByteArray);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Theory] [Theory]
@ -52,10 +48,8 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(true)] [InlineData(true)]
public void Bytes_Decoder_Specific(bool useSpan) public void Bytes_Decoder_Specific(bool useSpan)
{ {
using (var img = useSpan ? Image.Load<Rgba32>(ByteSpan, new BmpDecoder()) : Image.Load<Rgba32>(ByteArray, new BmpDecoder())) using var img = useSpan ? Image.Load<Rgba32>(ByteSpan, new BmpDecoder()) : Image.Load<Rgba32>(ByteArray, new BmpDecoder());
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Theory] [Theory]
@ -63,10 +57,8 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(true)] [InlineData(true)]
public void Bytes_Decoder_Agnostic(bool useSpan) public void Bytes_Decoder_Agnostic(bool useSpan)
{ {
using (var img = useSpan ? Image.Load(ByteSpan, new BmpDecoder()) : Image.Load(ByteArray, new BmpDecoder())) using var img = useSpan ? Image.Load(ByteSpan, new BmpDecoder()) : Image.Load(ByteArray, new BmpDecoder());
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Theory] [Theory]
@ -75,11 +67,9 @@ namespace SixLabors.ImageSharp.Tests
public void Bytes_OutFormat_Specific(bool useSpan) public void Bytes_OutFormat_Specific(bool useSpan)
{ {
IImageFormat format; IImageFormat format;
using (var img = useSpan ? Image.Load<Rgba32>(ByteSpan, out format) : Image.Load<Rgba32>(ByteArray, out format)) using var img = useSpan ? Image.Load<Rgba32>(ByteSpan, out format) : Image.Load<Rgba32>(ByteArray, out format);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img); Assert.IsType<BmpFormat>(format);
Assert.IsType<BmpFormat>(format);
}
} }
[Theory] [Theory]
@ -88,11 +78,9 @@ namespace SixLabors.ImageSharp.Tests
public void Bytes_OutFormat_Agnostic(bool useSpan) public void Bytes_OutFormat_Agnostic(bool useSpan)
{ {
IImageFormat format; IImageFormat format;
using (var img = useSpan ? Image.Load(ByteSpan, out format) : Image.Load(ByteArray, out format)) using var img = useSpan ? Image.Load(ByteSpan, out format) : Image.Load(ByteArray, out format);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img); Assert.IsType<BmpFormat>(format);
Assert.IsType<BmpFormat>(format);
}
} }
} }
} }

40
tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_UseDefaultConfiguration.cs

@ -29,57 +29,45 @@ namespace SixLabors.ImageSharp.Tests
[Fact] [Fact]
public void Stream_Specific() public void Stream_Specific()
{ {
using (var img = Image.Load<Rgba32>(this.Stream)) using var img = Image.Load<Rgba32>(this.Stream);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Fact] [Fact]
public void Stream_Agnostic() public void Stream_Agnostic()
{ {
using (var img = Image.Load(this.Stream)) using var img = Image.Load(this.Stream);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Fact] [Fact]
public void Stream_OutFormat_Specific() public void Stream_OutFormat_Specific()
{ {
using (var img = Image.Load<Rgba32>(this.Stream, out IImageFormat format)) using var img = Image.Load<Rgba32>(this.Stream, out IImageFormat format);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img); Assert.IsType<BmpFormat>(format);
Assert.IsType<BmpFormat>(format);
}
} }
[Fact] [Fact]
public void Stream_Decoder_Specific() public void Stream_Decoder_Specific()
{ {
using (var img = Image.Load<Rgba32>(this.Stream, new BmpDecoder())) using var img = Image.Load<Rgba32>(this.Stream, new BmpDecoder());
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Fact] [Fact]
public void Stream_Decoder_Agnostic() public void Stream_Decoder_Agnostic()
{ {
using (var img = Image.Load(this.Stream, new BmpDecoder())) using var img = Image.Load(this.Stream, new BmpDecoder());
{ VerifyDecodedImage(img);
VerifyDecodedImage(img);
}
} }
[Fact] [Fact]
public void Stream_OutFormat_Agnostic() public void Stream_OutFormat_Agnostic()
{ {
using (var img = Image.Load(this.Stream, out IImageFormat format)) using var img = Image.Load(this.Stream, out IImageFormat format);
{ VerifyDecodedImage(img);
VerifyDecodedImage(img); Assert.IsType<BmpFormat>(format);
Assert.IsType<BmpFormat>(format);
}
} }
public void Dispose() public void Dispose()

28
tests/ImageSharp.Tests/Image/ImageTests.Save.cs

@ -44,12 +44,10 @@ namespace SixLabors.ImageSharp.Tests
Assert.Throws<NotSupportedException>( Assert.Throws<NotSupportedException>(
() => () =>
{ {
using (var image = new Image<Rgba32>(10, 10)) using var image = new Image<Rgba32>(10, 10);
{ image.Save(file);
image.Save(file); });
}
});
} }
[Fact] [Fact]
@ -58,15 +56,11 @@ namespace SixLabors.ImageSharp.Tests
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageTests)); string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageTests));
string file = System.IO.Path.Combine(dir, "SetEncoding.dat"); string file = System.IO.Path.Combine(dir, "SetEncoding.dat");
using (var image = new Image<Rgba32>(10, 10)) using var image = new Image<Rgba32>(10, 10);
{ image.Save(file, new PngEncoder());
image.Save(file, new PngEncoder());
}
using (Image.Load(file, out var mime)) using var load = Image.Load(file, out var mime);
{ Assert.Equal("image/png", mime.DefaultMimeType);
Assert.Equal("image/png", mime.DefaultMimeType);
}
} }
[Fact] [Fact]
@ -75,10 +69,8 @@ namespace SixLabors.ImageSharp.Tests
var image = new Image<Rgba32>(5, 5); var image = new Image<Rgba32>(5, 5);
image.Dispose(); image.Dispose();
IImageEncoder encoder = Mock.Of<IImageEncoder>(); IImageEncoder encoder = Mock.Of<IImageEncoder>();
using (var stream = new MemoryStream()) using var stream = new MemoryStream();
{ Assert.Throws<ObjectDisposedException>(() => image.Save(stream, encoder));
Assert.Throws<ObjectDisposedException>(() => image.Save(stream, encoder));
}
} }
} }
} }

88
tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs

@ -88,14 +88,12 @@ namespace SixLabors.ImageSharp.Tests
var array = new Rgba32[25]; var array = new Rgba32[25];
var memory = new Memory<Rgba32>(array); var memory = new Memory<Rgba32>(array);
using (var image = Image.WrapMemory(cfg, memory, 5, 5, metaData)) using var image = Image.WrapMemory(cfg, memory, 5, 5, metaData);
{ ref Rgba32 pixel0 = ref image.GetPixelSpan()[0];
ref Rgba32 pixel0 = ref image.GetPixelSpan()[0]; Assert.True(Unsafe.AreSame(ref array[0], ref pixel0));
Assert.True(Unsafe.AreSame(ref array[0], ref pixel0));
Assert.Equal(cfg, image.GetConfiguration()); Assert.Equal(cfg, image.GetConfiguration());
Assert.Equal(metaData, image.Metadata); Assert.Equal(metaData, image.Metadata);
}
} }
[Fact] [Fact]
@ -106,33 +104,27 @@ namespace SixLabors.ImageSharp.Tests
return; return;
} }
using (var bmp = new Bitmap(51, 23)) using var bmp = new Bitmap(51, 23);
using var memoryManager = new BitmapMemoryManager(bmp);
Memory<Bgra32> memory = memoryManager.Memory;
Bgra32 bg = Color.Red;
Bgra32 fg = Color.Green;
using var image = Image.WrapMemory(memory, bmp.Width, bmp.Height);
Assert.Equal(memory, image.GetPixelMemory());
image.GetPixelSpan().Fill(bg);
for (var i = 10; i < 20; i++)
{ {
using (var memoryManager = new BitmapMemoryManager(bmp)) image.GetPixelRowSpan(i).Slice(10, 10).Fill(fg);
{ }
Memory<Bgra32> memory = memoryManager.Memory;
Bgra32 bg = Color.Red;
Bgra32 fg = Color.Green;
using (var image = Image.WrapMemory(memory, bmp.Width, bmp.Height))
{
Assert.Equal(memory, image.GetPixelMemory());
image.GetPixelSpan().Fill(bg);
for (var i = 10; i < 20; i++)
{
image.GetPixelRowSpan(i).Slice(10, 10).Fill(fg);
}
}
Assert.False(memoryManager.IsDisposed);
}
string fn = System.IO.Path.Combine( Assert.False(memoryManager.IsDisposed);
TestEnvironment.ActualOutputDirectoryFullPath,
$"{nameof(this.WrapSystemDrawingBitmap_WhenObserved)}.bmp");
bmp.Save(fn, ImageFormat.Bmp); string fn = System.IO.Path.Combine(
} TestEnvironment.ActualOutputDirectoryFullPath,
$"{nameof(this.WrapSystemDrawingBitmap_WhenObserved)}.bmp");
bmp.Save(fn, ImageFormat.Bmp);
} }
[Fact] [Fact]
@ -143,31 +135,29 @@ namespace SixLabors.ImageSharp.Tests
return; return;
} }
using (var bmp = new Bitmap(51, 23)) using var bmp = new Bitmap(51, 23);
var memoryManager = new BitmapMemoryManager(bmp);
Bgra32 bg = Color.Red;
Bgra32 fg = Color.Green;
using (var image = Image.WrapMemory(memoryManager, bmp.Width, bmp.Height))
{ {
var memoryManager = new BitmapMemoryManager(bmp); Assert.Equal(memoryManager.Memory, image.GetPixelMemory());
Bgra32 bg = Color.Red;
Bgra32 fg = Color.Green;
using (var image = Image.WrapMemory(memoryManager, bmp.Width, bmp.Height)) image.GetPixelSpan().Fill(bg);
for (var i = 10; i < 20; i++)
{ {
Assert.Equal(memoryManager.Memory, image.GetPixelMemory()); image.GetPixelRowSpan(i).Slice(10, 10).Fill(fg);
image.GetPixelSpan().Fill(bg);
for (var i = 10; i < 20; i++)
{
image.GetPixelRowSpan(i).Slice(10, 10).Fill(fg);
}
} }
}
Assert.True(memoryManager.IsDisposed); Assert.True(memoryManager.IsDisposed);
string fn = System.IO.Path.Combine( string fn = System.IO.Path.Combine(
TestEnvironment.ActualOutputDirectoryFullPath, TestEnvironment.ActualOutputDirectoryFullPath,
$"{nameof(this.WrapSystemDrawingBitmap_WhenOwned)}.bmp"); $"{nameof(this.WrapSystemDrawingBitmap_WhenOwned)}.bmp");
bmp.Save(fn, ImageFormat.Bmp); bmp.Save(fn, ImageFormat.Bmp);
}
} }
private static bool ShouldSkipBitmapTest => private static bool ShouldSkipBitmapTest =>

56
tests/ImageSharp.Tests/Image/ImageTests.cs

@ -21,15 +21,13 @@ namespace SixLabors.ImageSharp.Tests
[Fact] [Fact]
public void Width_Height() public void Width_Height()
{ {
using (var image = new Image<Rgba32>(11, 23)) using var image = new Image<Rgba32>(11, 23);
{ Assert.Equal(11, image.Width);
Assert.Equal(11, image.Width); Assert.Equal(23, image.Height);
Assert.Equal(23, image.Height); Assert.Equal(11 * 23, image.GetPixelSpan().Length);
Assert.Equal(11 * 23, image.GetPixelSpan().Length); image.ComparePixelBufferTo(default(Rgba32));
image.ComparePixelBufferTo(default(Rgba32));
Assert.Equal(Configuration.Default, image.GetConfiguration()); Assert.Equal(Configuration.Default, image.GetConfiguration());
}
} }
[Fact] [Fact]
@ -37,15 +35,13 @@ namespace SixLabors.ImageSharp.Tests
{ {
Configuration configuration = Configuration.Default.Clone(); Configuration configuration = Configuration.Default.Clone();
using (var image = new Image<Rgba32>(configuration, 11, 23)) using var image = new Image<Rgba32>(configuration, 11, 23);
{ Assert.Equal(11, image.Width);
Assert.Equal(11, image.Width); Assert.Equal(23, image.Height);
Assert.Equal(23, image.Height); Assert.Equal(11 * 23, image.GetPixelSpan().Length);
Assert.Equal(11 * 23, image.GetPixelSpan().Length); image.ComparePixelBufferTo(default(Rgba32));
image.ComparePixelBufferTo(default(Rgba32));
Assert.Equal(configuration, image.GetConfiguration()); Assert.Equal(configuration, image.GetConfiguration());
}
} }
[Fact] [Fact]
@ -54,15 +50,13 @@ namespace SixLabors.ImageSharp.Tests
Configuration configuration = Configuration.Default.Clone(); Configuration configuration = Configuration.Default.Clone();
Rgba32 color = Rgba32.Aquamarine; Rgba32 color = Rgba32.Aquamarine;
using (var image = new Image<Rgba32>(configuration, 11, 23, color)) using var image = new Image<Rgba32>(configuration, 11, 23, color);
{ Assert.Equal(11, image.Width);
Assert.Equal(11, image.Width); Assert.Equal(23, image.Height);
Assert.Equal(23, image.Height); Assert.Equal(11 * 23, image.GetPixelSpan().Length);
Assert.Equal(11 * 23, image.GetPixelSpan().Length); image.ComparePixelBufferTo(color);
image.ComparePixelBufferTo(color);
Assert.Equal(configuration, image.GetConfiguration()); Assert.Equal(configuration, image.GetConfiguration());
}
} }
[Fact] [Fact]
@ -74,15 +68,13 @@ namespace SixLabors.ImageSharp.Tests
configuration.MemoryAllocator = new TestMemoryAllocator(dirtyValue); configuration.MemoryAllocator = new TestMemoryAllocator(dirtyValue);
var metadata = new ImageMetadata(); var metadata = new ImageMetadata();
using (var image = Image.CreateUninitialized<L8>(configuration, 21, 22, metadata)) using var image = Image.CreateUninitialized<L8>(configuration, 21, 22, metadata);
{ Assert.Equal(21, image.Width);
Assert.Equal(21, image.Width); Assert.Equal(22, image.Height);
Assert.Equal(22, image.Height); Assert.Same(configuration, image.GetConfiguration());
Assert.Same(configuration, image.GetConfiguration()); Assert.Same(metadata, image.Metadata);
Assert.Same(metadata, image.Metadata);
Assert.Equal(dirtyValue, image[5, 5].PackedValue); Assert.Equal(dirtyValue, image[5, 5].PackedValue);
}
} }
} }
} }

106
tests/ImageSharp.Tests/Memory/Buffer2DTests.cs

@ -37,24 +37,20 @@ namespace SixLabors.ImageSharp.Tests.Memory
[InlineData(1025, 17)] [InlineData(1025, 17)]
public void Construct(int width, int height) public void Construct(int width, int height)
{ {
using (Buffer2D<TestStructs.Foo> buffer = this.MemoryAllocator.Allocate2D<TestStructs.Foo>(width, height)) using Buffer2D<TestStructs.Foo> buffer = this.MemoryAllocator.Allocate2D<TestStructs.Foo>(width, height);
{ Assert.Equal(width, buffer.Width);
Assert.Equal(width, buffer.Width); Assert.Equal(height, buffer.Height);
Assert.Equal(height, buffer.Height); Assert.Equal(width * height, buffer.GetMemory().Length);
Assert.Equal(width * height, buffer.GetMemory().Length);
}
} }
[Fact] [Fact]
public void CreateClean() public void CreateClean()
{ {
using (Buffer2D<int> buffer = this.MemoryAllocator.Allocate2D<int>(42, 42, AllocationOptions.Clean)) using Buffer2D<int> buffer = this.MemoryAllocator.Allocate2D<int>(42, 42, AllocationOptions.Clean);
Span<int> span = buffer.GetSpan();
for (int j = 0; j < span.Length; j++)
{ {
Span<int> span = buffer.GetSpan(); Assert.Equal(0, span[j]);
for (int j = 0; j < span.Length; j++)
{
Assert.Equal(0, span[j]);
}
} }
} }
@ -64,14 +60,12 @@ namespace SixLabors.ImageSharp.Tests.Memory
[InlineData(17, 42, 41)] [InlineData(17, 42, 41)]
public void GetRowSpanY(int width, int height, int y) public void GetRowSpanY(int width, int height, int y)
{ {
using (Buffer2D<TestStructs.Foo> buffer = this.MemoryAllocator.Allocate2D<TestStructs.Foo>(width, height)) using Buffer2D<TestStructs.Foo> buffer = this.MemoryAllocator.Allocate2D<TestStructs.Foo>(width, height);
{ Span<TestStructs.Foo> span = buffer.GetRowSpan(y);
Span<TestStructs.Foo> span = buffer.GetRowSpan(y);
// Assert.Equal(width * y, span.Start); // Assert.Equal(width * y, span.Start);
Assert.Equal(width, span.Length); Assert.Equal(width, span.Length);
Assert.SpanPointsTo(span, buffer.MemorySource.MemoryOwner, width * y); Assert.SpanPointsTo(span, buffer.MemorySource.MemoryOwner, width * y);
}
} }
[Theory] [Theory]
@ -80,35 +74,31 @@ namespace SixLabors.ImageSharp.Tests.Memory
[InlineData(99, 88, 98, 87)] [InlineData(99, 88, 98, 87)]
public void Indexer(int width, int height, int x, int y) public void Indexer(int width, int height, int x, int y)
{ {
using (Buffer2D<TestStructs.Foo> buffer = this.MemoryAllocator.Allocate2D<TestStructs.Foo>(width, height)) using Buffer2D<TestStructs.Foo> buffer = this.MemoryAllocator.Allocate2D<TestStructs.Foo>(width, height);
{ Span<TestStructs.Foo> span = buffer.MemorySource.GetSpan();
Span<TestStructs.Foo> span = buffer.MemorySource.GetSpan();
ref TestStructs.Foo actual = ref buffer[x, y]; ref TestStructs.Foo actual = ref buffer[x, y];
ref TestStructs.Foo expected = ref span[(y * width) + x]; ref TestStructs.Foo expected = ref span[(y * width) + x];
Assert.True(Unsafe.AreSame(ref expected, ref actual)); Assert.True(Unsafe.AreSame(ref expected, ref actual));
}
} }
[Fact] [Fact]
public void SwapOrCopyContent() public void SwapOrCopyContent()
{ {
using (Buffer2D<int> a = this.MemoryAllocator.Allocate2D<int>(10, 5)) using Buffer2D<int> a = this.MemoryAllocator.Allocate2D<int>(10, 5);
using (Buffer2D<int> b = this.MemoryAllocator.Allocate2D<int>(3, 7)) using Buffer2D<int> b = this.MemoryAllocator.Allocate2D<int>(3, 7);
{ IMemoryOwner<int> aa = a.MemorySource.MemoryOwner;
IMemoryOwner<int> aa = a.MemorySource.MemoryOwner; IMemoryOwner<int> bb = b.MemorySource.MemoryOwner;
IMemoryOwner<int> bb = b.MemorySource.MemoryOwner;
Buffer2D<int>.SwapOrCopyContent(a, b); Buffer2D<int>.SwapOrCopyContent(a, b);
Assert.Equal(bb, a.MemorySource.MemoryOwner); Assert.Equal(bb, a.MemorySource.MemoryOwner);
Assert.Equal(aa, b.MemorySource.MemoryOwner); Assert.Equal(aa, b.MemorySource.MemoryOwner);
Assert.Equal(new Size(3, 7), a.Size()); Assert.Equal(new Size(3, 7), a.Size());
Assert.Equal(new Size(10, 5), b.Size()); Assert.Equal(new Size(10, 5), b.Size());
}
} }
[Theory] [Theory]
@ -121,21 +111,19 @@ namespace SixLabors.ImageSharp.Tests.Memory
public void CopyColumns(int width, int height, int startIndex, int destIndex, int columnCount) public void CopyColumns(int width, int height, int startIndex, int destIndex, int columnCount)
{ {
var rnd = new Random(123); var rnd = new Random(123);
using (Buffer2D<float> b = this.MemoryAllocator.Allocate2D<float>(width, height)) using Buffer2D<float> b = this.MemoryAllocator.Allocate2D<float>(width, height);
{ rnd.RandomFill(b.GetSpan(), 0, 1);
rnd.RandomFill(b.GetSpan(), 0, 1);
b.CopyColumns(startIndex, destIndex, columnCount); b.CopyColumns(startIndex, destIndex, columnCount);
for (int y = 0; y < b.Height; y++) for (int y = 0; y < b.Height; y++)
{ {
Span<float> row = b.GetRowSpan(y); Span<float> row = b.GetRowSpan(y);
Span<float> s = row.Slice(startIndex, columnCount); Span<float> s = row.Slice(startIndex, columnCount);
Span<float> d = row.Slice(destIndex, columnCount); Span<float> d = row.Slice(destIndex, columnCount);
Xunit.Assert.True(s.SequenceEqual(d)); Xunit.Assert.True(s.SequenceEqual(d));
}
} }
} }
@ -143,22 +131,20 @@ namespace SixLabors.ImageSharp.Tests.Memory
public void CopyColumns_InvokeMultipleTimes() public void CopyColumns_InvokeMultipleTimes()
{ {
var rnd = new Random(123); var rnd = new Random(123);
using (Buffer2D<float> b = this.MemoryAllocator.Allocate2D<float>(100, 100)) using Buffer2D<float> b = this.MemoryAllocator.Allocate2D<float>(100, 100);
{ rnd.RandomFill(b.GetSpan(), 0, 1);
rnd.RandomFill(b.GetSpan(), 0, 1);
b.CopyColumns(0, 50, 22); b.CopyColumns(0, 50, 22);
b.CopyColumns(0, 50, 22); b.CopyColumns(0, 50, 22);
for (int y = 0; y < b.Height; y++) for (int y = 0; y < b.Height; y++)
{ {
Span<float> row = b.GetRowSpan(y); Span<float> row = b.GetRowSpan(y);
Span<float> s = row.Slice(0, 22); Span<float> s = row.Slice(0, 22);
Span<float> d = row.Slice(50, 22); Span<float> d = row.Slice(50, 22);
Xunit.Assert.True(s.SequenceEqual(d)); Xunit.Assert.True(s.SequenceEqual(d));
}
} }
} }
} }

112
tests/ImageSharp.Tests/Memory/BufferAreaTests.cs

@ -13,14 +13,12 @@ namespace SixLabors.ImageSharp.Tests.Memory
[Fact] [Fact]
public void Construct() public void Construct()
{ {
using (var buffer = Configuration.Default.MemoryAllocator.Allocate2D<int>(10, 20)) using var buffer = Configuration.Default.MemoryAllocator.Allocate2D<int>(10, 20);
{ var rectangle = new Rectangle(3, 2, 5, 6);
var rectangle = new Rectangle(3, 2, 5, 6); var area = new BufferArea<int>(buffer, rectangle);
var area = new BufferArea<int>(buffer, rectangle);
Assert.Equal(buffer, area.DestinationBuffer); Assert.Equal(buffer, area.DestinationBuffer);
Assert.Equal(rectangle, area.Rectangle); Assert.Equal(rectangle, area.Rectangle);
}
} }
private static Buffer2D<int> CreateTestBuffer(int w, int h) private static Buffer2D<int> CreateTestBuffer(int w, int h)
@ -42,16 +40,14 @@ namespace SixLabors.ImageSharp.Tests.Memory
[InlineData(5, 4, 3, 2)] [InlineData(5, 4, 3, 2)]
public void Indexer(int rx, int ry, int x, int y) public void Indexer(int rx, int ry, int x, int y)
{ {
using (Buffer2D<int> buffer = CreateTestBuffer(20, 30)) using Buffer2D<int> buffer = CreateTestBuffer(20, 30);
{ var r = new Rectangle(rx, ry, 5, 6);
var r = new Rectangle(rx, ry, 5, 6);
BufferArea<int> area = buffer.GetArea(r); BufferArea<int> area = buffer.GetArea(r);
int value = area[x, y]; int value = area[x, y];
int expected = ((ry + y) * 100) + rx + x; int expected = ((ry + y) * 100) + rx + x;
Assert.Equal(expected, value); Assert.Equal(expected, value);
}
} }
[Theory] [Theory]
@ -59,89 +55,79 @@ namespace SixLabors.ImageSharp.Tests.Memory
[InlineData(5, 4, 3, 6, 5)] [InlineData(5, 4, 3, 6, 5)]
public void GetRowSpan(int rx, int ry, int y, int w, int h) public void GetRowSpan(int rx, int ry, int y, int w, int h)
{ {
using (Buffer2D<int> buffer = CreateTestBuffer(20, 30)) using Buffer2D<int> buffer = CreateTestBuffer(20, 30);
{ var r = new Rectangle(rx, ry, w, h);
var r = new Rectangle(rx, ry, w, h);
BufferArea<int> area = buffer.GetArea(r); BufferArea<int> area = buffer.GetArea(r);
Span<int> span = area.GetRowSpan(y); Span<int> span = area.GetRowSpan(y);
Assert.Equal(w, span.Length); Assert.Equal(w, span.Length);
for (int i = 0; i < w; i++) for (int i = 0; i < w; i++)
{ {
int expected = ((ry + y) * 100) + rx + i; int expected = ((ry + y) * 100) + rx + i;
int value = span[i]; int value = span[i];
Assert.Equal(expected, value); Assert.Equal(expected, value);
}
} }
} }
[Fact] [Fact]
public void GetSubArea() public void GetSubArea()
{ {
using (Buffer2D<int> buffer = CreateTestBuffer(20, 30)) using Buffer2D<int> buffer = CreateTestBuffer(20, 30);
{ BufferArea<int> area0 = buffer.GetArea(6, 8, 10, 10);
BufferArea<int> area0 = buffer.GetArea(6, 8, 10, 10);
BufferArea<int> area1 = area0.GetSubArea(4, 4, 5, 5); BufferArea<int> area1 = area0.GetSubArea(4, 4, 5, 5);
var expectedRect = new Rectangle(10, 12, 5, 5); var expectedRect = new Rectangle(10, 12, 5, 5);
Assert.Equal(buffer, area1.DestinationBuffer); Assert.Equal(buffer, area1.DestinationBuffer);
Assert.Equal(expectedRect, area1.Rectangle); Assert.Equal(expectedRect, area1.Rectangle);
int value00 = (12 * 100) + 10; int value00 = (12 * 100) + 10;
Assert.Equal(value00, area1[0, 0]); Assert.Equal(value00, area1[0, 0]);
}
} }
[Fact] [Fact]
public void DangerousGetPinnableReference() public void DangerousGetPinnableReference()
{ {
using (Buffer2D<int> buffer = CreateTestBuffer(20, 30)) using Buffer2D<int> buffer = CreateTestBuffer(20, 30);
{ BufferArea<int> area0 = buffer.GetArea(6, 8, 10, 10);
BufferArea<int> area0 = buffer.GetArea(6, 8, 10, 10);
ref int r = ref area0.GetReferenceToOrigin(); ref int r = ref area0.GetReferenceToOrigin();
int expected = buffer[6, 8]; int expected = buffer[6, 8];
Assert.Equal(expected, r); Assert.Equal(expected, r);
}
} }
[Fact] [Fact]
public void Clear_FullArea() public void Clear_FullArea()
{ {
using (Buffer2D<int> buffer = CreateTestBuffer(22, 13)) using Buffer2D<int> buffer = CreateTestBuffer(22, 13);
{ buffer.GetArea().Clear();
buffer.GetArea().Clear(); Span<int> fullSpan = buffer.GetSpan();
Span<int> fullSpan = buffer.GetSpan(); Assert.True(fullSpan.SequenceEqual(new int[fullSpan.Length]));
Assert.True(fullSpan.SequenceEqual(new int[fullSpan.Length]));
}
} }
[Fact] [Fact]
public void Clear_SubArea() public void Clear_SubArea()
{ {
using (Buffer2D<int> buffer = CreateTestBuffer(20, 30)) using Buffer2D<int> buffer = CreateTestBuffer(20, 30);
{ BufferArea<int> area = buffer.GetArea(5, 5, 10, 10);
BufferArea<int> area = buffer.GetArea(5, 5, 10, 10); area.Clear();
area.Clear();
Assert.NotEqual(0, buffer[4, 4]); Assert.NotEqual(0, buffer[4, 4]);
Assert.NotEqual(0, buffer[15, 15]); Assert.NotEqual(0, buffer[15, 15]);
Assert.Equal(0, buffer[5, 5]); Assert.Equal(0, buffer[5, 5]);
Assert.Equal(0, buffer[14, 14]); Assert.Equal(0, buffer[14, 14]);
for (int y = area.Rectangle.Y; y < area.Rectangle.Bottom; y++) for (int y = area.Rectangle.Y; y < area.Rectangle.Bottom; y++)
{ {
Span<int> span = buffer.GetRowSpan(y).Slice(area.Rectangle.X, area.Width); Span<int> span = buffer.GetRowSpan(y).Slice(area.Rectangle.X, area.Width);
Assert.True(span.SequenceEqual(new int[area.Width])); Assert.True(span.SequenceEqual(new int[area.Width]));
}
} }
} }
} }

16
tests/ImageSharp.Tests/Metadata/ImageMetadataTests.cs

@ -91,17 +91,15 @@ namespace SixLabors.ImageSharp.Tests.Metadata
exifProfile.SetValue(ExifTag.XResolution, new Rational(200)); exifProfile.SetValue(ExifTag.XResolution, new Rational(200));
exifProfile.SetValue(ExifTag.YResolution, new Rational(300)); exifProfile.SetValue(ExifTag.YResolution, new Rational(300));
using (var image = new Image<Rgba32>(1, 1)) using var image = new Image<Rgba32>(1, 1);
{ image.Metadata.ExifProfile = exifProfile;
image.Metadata.ExifProfile = exifProfile; image.Metadata.HorizontalResolution = 400;
image.Metadata.HorizontalResolution = 400; image.Metadata.VerticalResolution = 500;
image.Metadata.VerticalResolution = 500;
image.Metadata.SyncProfiles(); image.Metadata.SyncProfiles();
Assert.Equal(400, ((Rational)image.Metadata.ExifProfile.GetValue(ExifTag.XResolution).Value).ToDouble()); Assert.Equal(400, ((Rational)image.Metadata.ExifProfile.GetValue(ExifTag.XResolution).Value).ToDouble());
Assert.Equal(500, ((Rational)image.Metadata.ExifProfile.GetValue(ExifTag.YResolution).Value).ToDouble()); Assert.Equal(500, ((Rational)image.Metadata.ExifProfile.GetValue(ExifTag.YResolution).Value).ToDouble());
}
} }
} }
} }

70
tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs

@ -95,42 +95,40 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(TestImageWriteFormat.Png)] [InlineData(TestImageWriteFormat.Png)]
public void WriteFraction(TestImageWriteFormat imageFormat) public void WriteFraction(TestImageWriteFormat imageFormat)
{ {
using (var memStream = new MemoryStream()) using var memStream = new MemoryStream();
{ double exposureTime = 1.0 / 1600;
double exposureTime = 1.0 / 1600;
ExifProfile profile = GetExifProfile(); ExifProfile profile = GetExifProfile();
profile.SetValue(ExifTag.ExposureTime, new Rational(exposureTime)); profile.SetValue(ExifTag.ExposureTime, new Rational(exposureTime));
var image = new Image<Rgba32>(1, 1); var image = new Image<Rgba32>(1, 1);
image.Metadata.ExifProfile = profile; image.Metadata.ExifProfile = profile;
image = WriteAndRead(image, imageFormat); image = WriteAndRead(image, imageFormat);
profile = image.Metadata.ExifProfile; profile = image.Metadata.ExifProfile;
Assert.NotNull(profile); Assert.NotNull(profile);
IExifValue<Rational> value = profile.GetValue(ExifTag.ExposureTime); IExifValue<Rational> value = profile.GetValue(ExifTag.ExposureTime);
Assert.NotNull(value); Assert.NotNull(value);
Assert.NotEqual(exposureTime, value.Value.ToDouble()); Assert.NotEqual(exposureTime, value.Value.ToDouble());
memStream.Position = 0; memStream.Position = 0;
profile = GetExifProfile(); profile = GetExifProfile();
profile.SetValue(ExifTag.ExposureTime, new Rational(exposureTime, true)); profile.SetValue(ExifTag.ExposureTime, new Rational(exposureTime, true));
image.Metadata.ExifProfile = profile; image.Metadata.ExifProfile = profile;
image = WriteAndRead(image, imageFormat); image = WriteAndRead(image, imageFormat);
profile = image.Metadata.ExifProfile; profile = image.Metadata.ExifProfile;
Assert.NotNull(profile); Assert.NotNull(profile);
value = profile.GetValue(ExifTag.ExposureTime); value = profile.GetValue(ExifTag.ExposureTime);
Assert.Equal(exposureTime, value.Value.ToDouble()); Assert.Equal(exposureTime, value.Value.ToDouble());
image.Dispose(); image.Dispose();
}
} }
[Theory] [Theory]
@ -454,26 +452,22 @@ namespace SixLabors.ImageSharp.Tests
private static Image<Rgba32> WriteAndReadJpeg(Image<Rgba32> image) private static Image<Rgba32> WriteAndReadJpeg(Image<Rgba32> image)
{ {
using (var memStream = new MemoryStream()) using var memStream = new MemoryStream();
{ image.SaveAsJpeg(memStream);
image.SaveAsJpeg(memStream); image.Dispose();
image.Dispose();
memStream.Position = 0; memStream.Position = 0;
return Image.Load<Rgba32>(memStream); return Image.Load<Rgba32>(memStream);
}
} }
private static Image<Rgba32> WriteAndReadPng(Image<Rgba32> image) private static Image<Rgba32> WriteAndReadPng(Image<Rgba32> image)
{ {
using (var memStream = new MemoryStream()) using var memStream = new MemoryStream();
{ image.SaveAsPng(memStream);
image.SaveAsPng(memStream); image.Dispose();
image.Dispose();
memStream.Position = 0; memStream.Position = 0;
return Image.Load<Rgba32>(memStream); return Image.Load<Rgba32>(memStream);
}
} }
private static void TestProfile(ExifProfile profile) private static void TestProfile(ExifProfile profile)

6
tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifValueTests.cs

@ -13,10 +13,8 @@ namespace SixLabors.ImageSharp.Tests
public ExifValueTests() public ExifValueTests()
{ {
using (Image<Rgba32> image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateRgba32Image()) using Image<Rgba32> image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateRgba32Image();
{ this.profile = image.Metadata.ExifProfile;
this.profile = image.Metadata.ExifProfile;
}
} }
private IExifValue<string> GetExifValue() private IExifValue<string> GetExifValue()

32
tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffCompositorTests.cs

@ -33,28 +33,24 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelBlenders
public void PorterDuffOutputIsCorrect(TestImageProvider<Rgba32> provider, PixelAlphaCompositionMode mode) public void PorterDuffOutputIsCorrect(TestImageProvider<Rgba32> provider, PixelAlphaCompositionMode mode)
{ {
var srcFile = TestFile.Create(TestImages.Png.PDSrc); var srcFile = TestFile.Create(TestImages.Png.PDSrc);
using (Image<Rgba32> src = srcFile.CreateRgba32Image()) using Image<Rgba32> src = srcFile.CreateRgba32Image();
using (Image<Rgba32> dest = provider.GetImage()) using Image<Rgba32> dest = provider.GetImage();
var options = new GraphicsOptions
{ {
var options = new GraphicsOptions Antialias = false,
{ AlphaCompositionMode = mode
Antialias = false, };
AlphaCompositionMode = mode
};
using (Image<Rgba32> res = dest.Clone(x => x.DrawImage(src, options)))
{
string combinedMode = mode.ToString();
if (combinedMode != "Src" && combinedMode.StartsWith("Src")) using Image<Rgba32> res = dest.Clone(x => x.DrawImage(src, options));
{ string combinedMode = mode.ToString();
combinedMode = combinedMode.Substring(3);
}
res.DebugSave(provider, combinedMode); if (combinedMode != "Src" && combinedMode.StartsWith("Src"))
res.CompareToReferenceOutput(provider, combinedMode); {
} combinedMode = combinedMode.Substring(3);
} }
res.DebugSave(provider, combinedMode);
res.CompareToReferenceOutput(provider, combinedMode);
} }
} }
} }

22
tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.Rgba32OperationsTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System.Buffers; using System.Buffers;
@ -30,17 +30,15 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations
const int times = 200000; const int times = 200000;
const int count = 1024; const int count = 1024;
using (IMemoryOwner<Rgba32> source = Configuration.Default.MemoryAllocator.Allocate<Rgba32>(count)) using IMemoryOwner<Rgba32> source = Configuration.Default.MemoryAllocator.Allocate<Rgba32>(count);
using (IMemoryOwner<Vector4> dest = Configuration.Default.MemoryAllocator.Allocate<Vector4>(count)) using IMemoryOwner<Vector4> dest = Configuration.Default.MemoryAllocator.Allocate<Vector4>(count);
{ this.Measure(
this.Measure( times,
times, () => PixelOperations<Rgba32>.Instance.ToVector4(
() => PixelOperations<Rgba32>.Instance.ToVector4( this.Configuration,
this.Configuration, source.GetSpan(),
source.GetSpan(), dest.GetSpan()));
dest.GetSpan()));
}
} }
} }
} }
} }

8
tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs

@ -997,11 +997,9 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations
where TSource : struct where TSource : struct
where TDest : struct where TDest : struct
{ {
using (var buffers = new TestBuffers<TSource, TDest>(source, expected)) using var buffers = new TestBuffers<TSource, TDest>(source, expected);
{ action(buffers.SourceBuffer, buffers.ActualDestBuffer);
action(buffers.SourceBuffer, buffers.ActualDestBuffer); buffers.Verify();
buffers.Verify();
}
} }
internal static Vector4[] CreateVector4TestData(int length, RefAction<Vector4> vectorModifier = null) internal static Vector4[] CreateVector4TestData(int length, RefAction<Vector4> vectorModifier = null)

114
tests/ImageSharp.Tests/Processing/Normalization/HistogramEqualizationTests.cs

@ -32,19 +32,18 @@ namespace SixLabors.ImageSharp.Tests.Processing.Normalization
70, 87, 69, 68, 65, 73, 78, 90 70, 87, 69, 68, 65, 73, 78, 90
}; };
using (var image = new Image<Rgba32>(8, 8)) using var image = new Image<Rgba32>(8, 8);
for (int y = 0; y < 8; y++)
{ {
for (int y = 0; y < 8; y++) for (int x = 0; x < 8; x++)
{ {
for (int x = 0; x < 8; x++) byte luminance = pixels[(y * 8) + x];
{ image[x, y] = new Rgba32(luminance, luminance, luminance);
byte luminance = pixels[(y * 8) + x];
image[x, y] = new Rgba32(luminance, luminance, luminance);
}
} }
}
var expected = new byte[] var expected = new byte[]
{ {
0, 12, 53, 32, 146, 53, 174, 53, 0, 12, 53, 32, 146, 53, 174, 53,
57, 32, 12, 227, 219, 202, 32, 154, 57, 32, 12, 227, 219, 202, 32, 154,
65, 85, 93, 239, 251, 227, 65, 158, 65, 85, 93, 239, 251, 227, 65, 158,
@ -53,24 +52,23 @@ namespace SixLabors.ImageSharp.Tests.Processing.Normalization
117, 190, 36, 190, 178, 93, 20, 170, 117, 190, 36, 190, 178, 93, 20, 170,
130, 202, 73, 20, 12, 53, 85, 194, 130, 202, 73, 20, 12, 53, 85, 194,
146, 206, 130, 117, 85, 166, 182, 215 146, 206, 130, 117, 85, 166, 182, 215
}; };
// Act // Act
image.Mutate(x => x.HistogramEqualization(new HistogramEqualizationOptions image.Mutate(x => x.HistogramEqualization(new HistogramEqualizationOptions
{ {
LuminanceLevels = luminanceLevels LuminanceLevels = luminanceLevels
})); }));
// Assert // Assert
for (int y = 0; y < 8; y++) for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{ {
for (int x = 0; x < 8; x++) Rgba32 actual = image[x, y];
{ Assert.Equal(expected[(y * 8) + x], actual.R);
Rgba32 actual = image[x, y]; Assert.Equal(expected[(y * 8) + x], actual.G);
Assert.Equal(expected[(y * 8) + x], actual.R); Assert.Equal(expected[(y * 8) + x], actual.B);
Assert.Equal(expected[(y * 8) + x], actual.G);
Assert.Equal(expected[(y * 8) + x], actual.B);
}
} }
} }
} }
@ -80,19 +78,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Normalization
public void Adaptive_SlidingWindow_15Tiles_WithClipping<TPixel>(TestImageProvider<TPixel> provider) public void Adaptive_SlidingWindow_15Tiles_WithClipping<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var options = new HistogramEqualizationOptions
{ {
var options = new HistogramEqualizationOptions Method = HistogramEqualizationMethod.AdaptiveSlidingWindow,
{ LuminanceLevels = 256,
Method = HistogramEqualizationMethod.AdaptiveSlidingWindow, ClipHistogram = true,
LuminanceLevels = 256, NumberOfTiles = 15
ClipHistogram = true, };
NumberOfTiles = 15 image.Mutate(x => x.HistogramEqualization(options));
}; image.DebugSave(provider);
image.Mutate(x => x.HistogramEqualization(options)); image.CompareToReferenceOutput(ValidatorComparer, provider);
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -100,19 +96,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Normalization
public void Adaptive_TileInterpolation_10Tiles_WithClipping<TPixel>(TestImageProvider<TPixel> provider) public void Adaptive_TileInterpolation_10Tiles_WithClipping<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var options = new HistogramEqualizationOptions
{ {
var options = new HistogramEqualizationOptions Method = HistogramEqualizationMethod.AdaptiveTileInterpolation,
{ LuminanceLevels = 256,
Method = HistogramEqualizationMethod.AdaptiveTileInterpolation, ClipHistogram = true,
LuminanceLevels = 256, NumberOfTiles = 10
ClipHistogram = true, };
NumberOfTiles = 10 image.Mutate(x => x.HistogramEqualization(options));
}; image.DebugSave(provider);
image.Mutate(x => x.HistogramEqualization(options)); image.CompareToReferenceOutput(ValidatorComparer, provider);
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
/// <summary> /// <summary>
@ -127,20 +121,18 @@ namespace SixLabors.ImageSharp.Tests.Processing.Normalization
public void Issue984<TPixel>(TestImageProvider<TPixel> provider) public void Issue984<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var options = new HistogramEqualizationOptions()
{ {
var options = new HistogramEqualizationOptions() Method = HistogramEqualizationMethod.AdaptiveTileInterpolation,
{ LuminanceLevels = 256,
Method = HistogramEqualizationMethod.AdaptiveTileInterpolation, ClipHistogram = true,
LuminanceLevels = 256, ClipLimit = 5,
ClipHistogram = true, NumberOfTiles = 10
ClipLimit = 5, };
NumberOfTiles = 10 image.Mutate(x => x.HistogramEqualization(options));
}; image.DebugSave(provider);
image.Mutate(x => x.HistogramEqualization(options)); image.CompareToReferenceOutput(ValidatorComparer, provider);
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
} }
} }

60
tests/ImageSharp.Tests/Processing/Processors/Binarization/BinaryDitherTests.cs

@ -52,11 +52,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization
public void BinaryDitherFilter_WorksWithAllDitherers<TPixel>(TestImageProvider<TPixel> provider, string name, IOrderedDither ditherer) public void BinaryDitherFilter_WorksWithAllDitherers<TPixel>(TestImageProvider<TPixel> provider, string name, IOrderedDither ditherer)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.BinaryDither(ditherer));
image.Mutate(x => x.BinaryDither(ditherer)); image.DebugSave(provider, name);
image.DebugSave(provider, name);
}
} }
[Theory] [Theory]
@ -65,11 +63,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization
public void DiffusionFilter_WorksWithAllErrorDiffusers<TPixel>(TestImageProvider<TPixel> provider, string name, IErrorDiffuser diffuser) public void DiffusionFilter_WorksWithAllErrorDiffusers<TPixel>(TestImageProvider<TPixel> provider, string name, IErrorDiffuser diffuser)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.BinaryDiffuse(diffuser, .5F));
image.Mutate(x => x.BinaryDiffuse(diffuser, .5F)); image.DebugSave(provider, name);
image.DebugSave(provider, name);
}
} }
[Theory] [Theory]
@ -77,11 +73,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization
public void BinaryDitherFilter_ShouldNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void BinaryDitherFilter_ShouldNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.BinaryDither(DefaultDitherer));
image.Mutate(x => x.BinaryDither(DefaultDitherer)); image.DebugSave(provider);
image.DebugSave(provider);
}
} }
[Theory] [Theory]
@ -89,11 +83,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization
public void DiffusionFilter_ShouldNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void DiffusionFilter_ShouldNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.BinaryDiffuse(DefaultErrorDiffuser, 0.5f));
image.Mutate(x => x.BinaryDiffuse(DefaultErrorDiffuser, 0.5f)); image.DebugSave(provider);
image.DebugSave(provider);
}
} }
[Theory] [Theory]
@ -101,16 +93,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization
public void ApplyDitherFilterInBox<TPixel>(TestImageProvider<TPixel> provider) public void ApplyDitherFilterInBox<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> source = provider.GetImage()) using Image<TPixel> source = provider.GetImage();
using (Image<TPixel> image = source.Clone()) using Image<TPixel> image = source.Clone();
{ var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.Mutate(x => x.BinaryDither(DefaultDitherer, bounds)); image.Mutate(x => x.BinaryDither(DefaultDitherer, bounds));
image.DebugSave(provider); image.DebugSave(provider);
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds); ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
}
} }
[Theory] [Theory]
@ -118,16 +108,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization
public void ApplyDiffusionFilterInBox<TPixel>(TestImageProvider<TPixel> provider) public void ApplyDiffusionFilterInBox<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> source = provider.GetImage()) using Image<TPixel> source = provider.GetImage();
using (Image<TPixel> image = source.Clone()) using Image<TPixel> image = source.Clone();
{ var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.Mutate(x => x.BinaryDiffuse(DefaultErrorDiffuser, .5F, bounds)); image.Mutate(x => x.BinaryDiffuse(DefaultErrorDiffuser, .5F, bounds));
image.DebugSave(provider); image.DebugSave(provider);
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds); ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
}
} }
} }
} }

22
tests/ImageSharp.Tests/Processing/Processors/Binarization/BinaryThresholdTest.cs

@ -32,11 +32,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization
public void ImageShouldApplyBinaryThresholdFilter<TPixel>(TestImageProvider<TPixel> provider, float value) public void ImageShouldApplyBinaryThresholdFilter<TPixel>(TestImageProvider<TPixel> provider, float value)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.BinaryThreshold(value));
image.Mutate(x => x.BinaryThreshold(value)); image.DebugSave(provider, value);
image.DebugSave(provider, value);
}
} }
[Theory] [Theory]
@ -44,16 +42,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization
public void ImageShouldApplyBinaryThresholdInBox<TPixel>(TestImageProvider<TPixel> provider, float value) public void ImageShouldApplyBinaryThresholdInBox<TPixel>(TestImageProvider<TPixel> provider, float value)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> source = provider.GetImage()) using Image<TPixel> source = provider.GetImage();
using (var image = source.Clone()) using var image = source.Clone();
{ var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.Mutate(x => x.BinaryThreshold(value, bounds)); image.Mutate(x => x.BinaryThreshold(value, bounds));
image.DebugSave(provider, value); image.DebugSave(provider, value);
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds); ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
}
} }
} }
} }

26
tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs

@ -56,23 +56,19 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
} }
// Make sure the kernel components are the same // Make sure the kernel components are the same
using (var image = new Image<Rgb24>(1, 1)) using var image = new Image<Rgb24>(1, 1);
Configuration configuration = image.GetConfiguration();
var definition = new BokehBlurProcessor(10, BokehBlurProcessor.DefaultComponents, BokehBlurProcessor.DefaultGamma);
using var processor = (BokehBlurProcessor<Rgb24>)definition.CreatePixelSpecificProcessor(configuration, image, image.Bounds());
Assert.Equal(components.Count, processor.Kernels.Count);
foreach ((Complex64[] a, Complex64[] b) in components.Zip(processor.Kernels, (a, b) => (a, b)))
{ {
Configuration configuration = image.GetConfiguration(); Span<Complex64> spanA = a.AsSpan(), spanB = b.AsSpan();
var definition = new BokehBlurProcessor(10, BokehBlurProcessor.DefaultComponents, BokehBlurProcessor.DefaultGamma); Assert.Equal(spanA.Length, spanB.Length);
using (var processor = (BokehBlurProcessor<Rgb24>)definition.CreatePixelSpecificProcessor(configuration, image, image.Bounds())) for (int i = 0; i < spanA.Length; i++)
{ {
Assert.Equal(components.Count, processor.Kernels.Count); Assert.True(Math.Abs(Math.Abs(spanA[i].Real) - Math.Abs(spanB[i].Real)) < 0.0001f);
foreach ((Complex64[] a, Complex64[] b) in components.Zip(processor.Kernels, (a, b) => (a, b))) Assert.True(Math.Abs(Math.Abs(spanA[i].Imaginary) - Math.Abs(spanB[i].Imaginary)) < 0.0001f);
{
Span<Complex64> spanA = a.AsSpan(), spanB = b.AsSpan();
Assert.Equal(spanA.Length, spanB.Length);
for (int i = 0; i < spanA.Length; i++)
{
Assert.True(Math.Abs(Math.Abs(spanA[i].Real) - Math.Abs(spanB[i].Real)) < 0.0001f);
Assert.True(Math.Abs(Math.Abs(spanA[i].Imaginary) - Math.Abs(spanB[i].Imaginary)) < 0.0001f);
}
}
} }
} }
} }

40
tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs

@ -57,12 +57,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
public void DetectEdges_WorksWithAllFilters<TPixel>(TestImageProvider<TPixel> provider, EdgeDetectionOperators detector) public void DetectEdges_WorksWithAllFilters<TPixel>(TestImageProvider<TPixel> provider, EdgeDetectionOperators detector)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.DetectEdges(detector));
image.Mutate(x => x.DetectEdges(detector)); image.DebugSave(provider, detector.ToString());
image.DebugSave(provider, detector.ToString()); image.CompareToReferenceOutput(ValidatorComparer, provider, detector.ToString());
image.CompareToReferenceOutput(ValidatorComparer, provider, detector.ToString());
}
} }
[Theory] [Theory]
@ -70,12 +68,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
public void DetectEdges_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void DetectEdges_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.DetectEdges());
image.Mutate(x => x.DetectEdges()); image.DebugSave(provider);
image.DebugSave(provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -83,11 +79,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
public void DetectEdges_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider) public void DetectEdges_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.DetectEdges());
image.Mutate(x => x.DetectEdges()); image.DebugSave(provider, extension: "gif");
image.DebugSave(provider, extension: "gif");
}
} }
[Theory] [Theory]
@ -95,14 +89,12 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
public void DetectEdges_InBox<TPixel>(TestImageProvider<TPixel> provider) public void DetectEdges_InBox<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.Mutate(x => x.DetectEdges(bounds)); image.Mutate(x => x.DetectEdges(bounds));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
} }
} }

28
tests/ImageSharp.Tests/Processing/Processors/Transforms/AutoOrientTests.cs

@ -44,15 +44,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void AutoOrient_WorksForAllExifOrientations<TPixel>(TestImageProvider<TPixel> provider, ushort orientation) public void AutoOrient_WorksForAllExifOrientations<TPixel>(TestImageProvider<TPixel> provider, ushort orientation)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Metadata.ExifProfile = new ExifProfile();
image.Metadata.ExifProfile = new ExifProfile(); image.Metadata.ExifProfile.SetValue(ExifTag.Orientation, orientation);
image.Metadata.ExifProfile.SetValue(ExifTag.Orientation, orientation);
image.Mutate(x => x.AutoOrient()); image.Mutate(x => x.AutoOrient());
image.DebugSave(provider, orientation, appendPixelTypeToFileName: false); image.DebugSave(provider, orientation, appendPixelTypeToFileName: false);
image.CompareToReferenceOutput(provider, orientation, appendPixelTypeToFileName: false); image.CompareToReferenceOutput(provider, orientation, appendPixelTypeToFileName: false);
}
} }
[Theory] [Theory]
@ -80,14 +78,12 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
ulong orientationCode = BitConverter.ToUInt64(orientationCodeData, 0); ulong orientationCode = BitConverter.ToUInt64(orientationCodeData, 0);
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
using (Image<TPixel> reference = image.Clone()) using Image<TPixel> reference = image.Clone();
{ image.Metadata.ExifProfile = new ExifProfile(bytes);
image.Metadata.ExifProfile = new ExifProfile(bytes); image.Mutate(x => x.AutoOrient());
image.Mutate(x => x.AutoOrient()); image.DebugSave(provider, $"{dataType}-{orientationCode}", appendPixelTypeToFileName: false);
image.DebugSave(provider, $"{dataType}-{orientationCode}", appendPixelTypeToFileName: false); ImageComparer.Exact.VerifySimilarity(image, reference);
ImageComparer.Exact.VerifySimilarity(image, reference);
}
} }
} }
} }

36
tests/ImageSharp.Tests/Processing/Processors/Transforms/PadTest.cs

@ -19,18 +19,16 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ImageShouldPad<TPixel>(TestImageProvider<TPixel> provider) public void ImageShouldPad<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.Pad(image.Width + 50, image.Height + 50));
image.Mutate(x => x.Pad(image.Width + 50, image.Height + 50)); image.DebugSave(provider);
image.DebugSave(provider);
// Check pixels are empty // Check pixels are empty
for (int y = 0; y < 25; y++) for (int y = 0; y < 25; y++)
{
for (int x = 0; x < 25; x++)
{ {
for (int x = 0; x < 25; x++) Assert.Equal(default, image[x, y]);
{
Assert.Equal(default, image[x, y]);
}
} }
} }
} }
@ -42,18 +40,16 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
{ {
var color = Color.Red; var color = Color.Red;
TPixel expected = color.ToPixel<TPixel>(); TPixel expected = color.ToPixel<TPixel>();
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.Pad(image.Width + 50, image.Height + 50, color));
image.Mutate(x => x.Pad(image.Width + 50, image.Height + 50, color)); image.DebugSave(provider);
image.DebugSave(provider);
// Check pixels are filled // Check pixels are filled
for (int y = 0; y < 25; y++) for (int y = 0; y < 25; y++)
{
for (int x = 0; x < 25; x++)
{ {
for (int x = 0; x < 25; x++) Assert.Equal(expected, image[x, y]);
{
Assert.Equal(expected, image[x, y]);
}
} }
} }
} }

365
tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

@ -45,15 +45,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
{ {
var filePath = TestFile.GetInputFileFullPath(TestImages.Jpeg.Baseline.Calliphora); var filePath = TestFile.GetInputFileFullPath(TestImages.Jpeg.Baseline.Calliphora);
using (var image = Image.Load(filePath)) using var image = Image.Load(filePath);
{ image.Mutate(x => x.Resize(image.Size() / 2));
image.Mutate(x => x.Resize(image.Size() / 2)); string path = System.IO.Path.Combine(
string path = System.IO.Path.Combine( TestEnvironment.CreateOutputDirectory(nameof(ResizeTests)),
TestEnvironment.CreateOutputDirectory(nameof(ResizeTests)), nameof(this.Resize_PixelAgnostic) + ".png");
nameof(this.Resize_PixelAgnostic) + ".png");
image.Save(path); image.Save(path);
}
} }
[Theory(Skip = "Debug only, enable manually")] [Theory(Skip = "Debug only, enable manually")]
@ -70,11 +68,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
provider.Configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInKilobytes * 1024; provider.Configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInKilobytes * 1024;
using (var image = provider.GetImage()) using var image = provider.GetImage();
{ image.Mutate(x => x.Resize(destSize, destSize));
image.Mutate(x => x.Resize(destSize, destSize)); image.DebugSave(provider, appendPixelTypeToFileName: false);
image.DebugSave(provider, appendPixelTypeToFileName: false);
}
} }
[Theory] [Theory]
@ -88,14 +84,12 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
// [WithBasicTestPatternImages(15, 12, PixelTypes.Rgba32, 2, 3, 1, 2)] means: // [WithBasicTestPatternImages(15, 12, PixelTypes.Rgba32, 2, 3, 1, 2)] means:
// resizing: (15, 12) -> (10, 6) // resizing: (15, 12) -> (10, 6)
// kernel dimensions: (3, 4) // kernel dimensions: (3, 4)
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ var destSize = new Size(image.Width * wN / wD, image.Height * hN / hD);
var destSize = new Size(image.Width * wN / wD, image.Height * hN / hD); image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false));
image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false)); FormattableString outputInfo = $"({wN}÷{wD},{hN}÷{hD})";
FormattableString outputInfo = $"({wN}÷{wD},{hN}÷{hD})"; image.DebugSave(provider, outputInfo, appendPixelTypeToFileName: false);
image.DebugSave(provider, outputInfo, appendPixelTypeToFileName: false); image.CompareToReferenceOutput(provider, outputInfo, appendPixelTypeToFileName: false);
image.CompareToReferenceOutput(provider, outputInfo, appendPixelTypeToFileName: false);
}
} }
private static readonly int SizeOfVector4 = Unsafe.SizeOf<Vector4>(); private static readonly int SizeOfVector4 = Unsafe.SizeOf<Vector4>();
@ -113,47 +107,43 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
int workingBufferLimitInRows) int workingBufferLimitInRows)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image0 = provider.GetImage()) using Image<TPixel> image0 = provider.GetImage();
{ Size destSize = image0.Size() / 4;
Size destSize = image0.Size() / 4;
var configuration = Configuration.CreateDefaultInstance();
int workingBufferSizeHintInBytes = workingBufferLimitInRows * destSize.Width * SizeOfVector4;
var allocator = new TestMemoryAllocator();
configuration.MemoryAllocator = allocator;
configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInBytes;
var verticalKernelMap = ResizeKernelMap.Calculate(
KnownResamplers.Bicubic,
destSize.Height,
image0.Height,
Configuration.Default.MemoryAllocator);
int minimumWorkerAllocationInBytes = verticalKernelMap.MaxDiameter * 2 * destSize.Width * SizeOfVector4;
verticalKernelMap.Dispose();
using Image<TPixel> image = image0.Clone(configuration);
image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false));
image.DebugSave(
provider,
testOutputDetails: workingBufferLimitInRows,
appendPixelTypeToFileName: false);
image.CompareToReferenceOutput(
ImageComparer.TolerantPercentage(0.001f),
provider,
testOutputDetails: workingBufferLimitInRows,
appendPixelTypeToFileName: false);
Configuration configuration = Configuration.CreateDefaultInstance(); Assert.NotEmpty(allocator.AllocationLog);
int workingBufferSizeHintInBytes = workingBufferLimitInRows * destSize.Width * SizeOfVector4; int maxAllocationSize = allocator.AllocationLog.Where(
TestMemoryAllocator allocator = new TestMemoryAllocator(); e => e.ElementType == typeof(Vector4)).Max(e => e.LengthInBytes);
configuration.MemoryAllocator = allocator;
configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInBytes;
var verticalKernelMap = ResizeKernelMap.Calculate( Assert.True(maxAllocationSize <= Math.Max(workingBufferSizeHintInBytes, minimumWorkerAllocationInBytes));
KnownResamplers.Bicubic,
destSize.Height,
image0.Height,
Configuration.Default.MemoryAllocator);
int minimumWorkerAllocationInBytes = verticalKernelMap.MaxDiameter * 2 * destSize.Width * SizeOfVector4;
verticalKernelMap.Dispose();
using (Image<TPixel> image = image0.Clone(configuration))
{
image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false));
image.DebugSave(
provider,
testOutputDetails: workingBufferLimitInRows,
appendPixelTypeToFileName: false);
image.CompareToReferenceOutput(
ImageComparer.TolerantPercentage(0.001f),
provider,
testOutputDetails: workingBufferLimitInRows,
appendPixelTypeToFileName: false);
Assert.NotEmpty(allocator.AllocationLog);
int maxAllocationSize = allocator.AllocationLog.Where(
e => e.ElementType == typeof(Vector4)).Max(e => e.LengthInBytes);
Assert.True(maxAllocationSize <= Math.Max(workingBufferSizeHintInBytes, minimumWorkerAllocationInBytes));
}
}
} }
[Theory] [Theory]
@ -161,13 +151,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void Resize_Compand<TPixel>(TestImageProvider<TPixel> provider) public void Resize_Compand<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.Resize(image.Size() / 2, true));
image.Mutate(x => x.Resize(image.Size() / 2, true));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -190,13 +178,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void Resize_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider) public void Resize_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, KnownResamplers.Bicubic));
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, KnownResamplers.Bicubic));
// Comparer fights decoder with gif-s. Could not use CompareToReferenceOutput here :( // Comparer fights decoder with gif-s. Could not use CompareToReferenceOutput here :(
image.DebugSave(provider, extension: "gif"); image.DebugSave(provider, extension: "gif");
}
} }
[Theory] [Theory]
@ -212,16 +198,12 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void Resize_ThrowsForWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider) public void Resize_ThrowsForWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image0 = provider.GetImage()) using Image<TPixel> image0 = provider.GetImage();
{ var mmg = TestMemoryManager<TPixel>.CreateAsCopyOf(image0.GetPixelSpan());
var mmg = TestMemoryManager<TPixel>.CreateAsCopyOf(image0.GetPixelSpan());
using (var image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height)) using var image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height);
{ Assert.ThrowsAny<Exception>(
Assert.ThrowsAny<Exception>( () => { image1.Mutate(x => x.Resize(image0.Width / 2, image0.Height / 2, true)); });
() => { image1.Mutate(x => x.Resize(image0.Width / 2, image0.Height / 2, true)); });
}
}
} }
[Theory] [Theory]
@ -328,27 +310,25 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeFromSourceRectangle<TPixel>(TestImageProvider<TPixel> provider) public void ResizeFromSourceRectangle<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ var sourceRectangle = new Rectangle(
var sourceRectangle = new Rectangle( image.Width / 8,
image.Width / 8, image.Height / 8,
image.Height / 8, image.Width / 4,
image.Width / 4, image.Height / 4);
image.Height / 4); var destRectangle = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
var destRectangle = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
image.Mutate(
image.Mutate( x => x.Resize(
x => x.Resize( image.Width,
image.Width, image.Height,
image.Height, KnownResamplers.Bicubic,
KnownResamplers.Bicubic, sourceRectangle,
sourceRectangle, destRectangle,
destRectangle, false));
false));
image.DebugSave(provider);
image.DebugSave(provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -356,13 +336,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeHeightAndKeepAspect<TPixel>(TestImageProvider<TPixel> provider) public void ResizeHeightAndKeepAspect<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.Resize(0, image.Height / 3, false));
image.Mutate(x => x.Resize(0, image.Height / 3, false));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -370,12 +348,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeHeightCannotKeepAspectKeepsOnePixel<TPixel>(TestImageProvider<TPixel> provider) public void ResizeHeightCannotKeepAspectKeepsOnePixel<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.Resize(0, 5));
image.Mutate(x => x.Resize(0, 5)); Assert.Equal(1, image.Width);
Assert.Equal(1, image.Width); Assert.Equal(5, image.Height);
Assert.Equal(5, image.Height);
}
} }
[Theory] [Theory]
@ -383,13 +359,11 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeWidthAndKeepAspect<TPixel>(TestImageProvider<TPixel> provider) public void ResizeWidthAndKeepAspect<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.Resize(image.Width / 3, 0, false));
image.Mutate(x => x.Resize(image.Width / 3, 0, false));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -397,12 +371,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeWidthCannotKeepAspectKeepsOnePixel<TPixel>(TestImageProvider<TPixel> provider) public void ResizeWidthCannotKeepAspectKeepsOnePixel<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.Resize(5, 0));
image.Mutate(x => x.Resize(5, 0)); Assert.Equal(5, image.Width);
Assert.Equal(5, image.Width); Assert.Equal(1, image.Height);
Assert.Equal(1, image.Height);
}
} }
[Theory] [Theory]
@ -410,19 +382,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeWithBoxPadMode<TPixel>(TestImageProvider<TPixel> provider) public void ResizeWithBoxPadMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var options = new ResizeOptions
{ {
var options = new ResizeOptions Size = new Size(image.Width + 200, image.Height + 200),
{ Mode = ResizeMode.BoxPad
Size = new Size(image.Width + 200, image.Height + 200), };
Mode = ResizeMode.BoxPad
};
image.Mutate(x => x.Resize(options)); image.Mutate(x => x.Resize(options));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -430,15 +400,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeWithCropHeightMode<TPixel>(TestImageProvider<TPixel> provider) public void ResizeWithCropHeightMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ var options = new ResizeOptions { Size = new Size(image.Width, image.Height / 2) };
var options = new ResizeOptions { Size = new Size(image.Width, image.Height / 2) };
image.Mutate(x => x.Resize(options)); image.Mutate(x => x.Resize(options));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -446,15 +414,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeWithCropWidthMode<TPixel>(TestImageProvider<TPixel> provider) public void ResizeWithCropWidthMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ var options = new ResizeOptions { Size = new Size(image.Width / 2, image.Height) };
var options = new ResizeOptions { Size = new Size(image.Width / 2, image.Height) };
image.Mutate(x => x.Resize(options)); image.Mutate(x => x.Resize(options));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -462,19 +428,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void CanResizeLargeImageWithCropMode<TPixel>(TestImageProvider<TPixel> provider) public void CanResizeLargeImageWithCropMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var options = new ResizeOptions
{ {
var options = new ResizeOptions Size = new Size(480, 600),
{ Mode = ResizeMode.Crop
Size = new Size(480, 600), };
Mode = ResizeMode.Crop
};
image.Mutate(x => x.Resize(options)); image.Mutate(x => x.Resize(options));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -482,15 +446,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeWithMaxMode<TPixel>(TestImageProvider<TPixel> provider) public void ResizeWithMaxMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ var options = new ResizeOptions { Size = new Size(300, 300), Mode = ResizeMode.Max };
var options = new ResizeOptions { Size = new Size(300, 300), Mode = ResizeMode.Max };
image.Mutate(x => x.Resize(options)); image.Mutate(x => x.Resize(options));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -498,21 +460,19 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeWithMinMode<TPixel>(TestImageProvider<TPixel> provider) public void ResizeWithMinMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var options = new ResizeOptions
{ {
var options = new ResizeOptions Size = new Size(
{ (int)Math.Round(image.Width * .75F),
Size = new Size( (int)Math.Round(image.Height * .95F)),
(int)Math.Round(image.Width * .75F), Mode = ResizeMode.Min
(int)Math.Round(image.Height * .95F)), };
Mode = ResizeMode.Min
}; image.Mutate(x => x.Resize(options));
image.Mutate(x => x.Resize(options)); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -520,19 +480,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeWithPadMode<TPixel>(TestImageProvider<TPixel> provider) public void ResizeWithPadMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var options = new ResizeOptions
{ {
var options = new ResizeOptions Size = new Size(image.Width + 200, image.Height),
{ Mode = ResizeMode.Pad
Size = new Size(image.Width + 200, image.Height), };
Mode = ResizeMode.Pad
};
image.Mutate(x => x.Resize(options)); image.Mutate(x => x.Resize(options));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -540,19 +498,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void ResizeWithStretchMode<TPixel>(TestImageProvider<TPixel> provider) public void ResizeWithStretchMode<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
var options = new ResizeOptions
{ {
var options = new ResizeOptions Size = new Size(image.Width / 2, image.Height),
{ Mode = ResizeMode.Stretch
Size = new Size(image.Width / 2, image.Height), };
Mode = ResizeMode.Stretch
};
image.Mutate(x => x.Resize(options)); image.Mutate(x => x.Resize(options));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -568,11 +524,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
return; return;
} }
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{
// Don't bother saving, we're testing the EXIF metadata updates. // Don't bother saving, we're testing the EXIF metadata updates.
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2)); image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2));
}
} }
} }
} }

12
tests/ImageSharp.Tests/Processing/Processors/Transforms/RotateFlipTests.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -29,11 +29,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void RotateFlip<TPixel>(TestImageProvider<TPixel> provider, RotateMode rotateType, FlipMode flipType) public void RotateFlip<TPixel>(TestImageProvider<TPixel> provider, RotateMode rotateType, FlipMode flipType)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.Mutate(x => x.RotateFlip(rotateType, flipType));
image.Mutate(x => x.RotateFlip(rotateType, flipType)); image.DebugSave(provider, string.Join("_", rotateType, flipType));
image.DebugSave(provider, string.Join("_", rotateType, flipType));
}
} }
} }
} }

102
tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs

@ -81,16 +81,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
IResampler resampler = GetResampler(resamplerName); IResampler resampler = GetResampler(resamplerName);
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ AffineTransformBuilder builder = new AffineTransformBuilder()
AffineTransformBuilder builder = new AffineTransformBuilder() .AppendRotationDegrees(30);
.AppendRotationDegrees(30);
image.Mutate(c => c.Transform(builder, resampler)); image.Mutate(c => c.Transform(builder, resampler));
image.DebugSave(provider, resamplerName); image.DebugSave(provider, resamplerName);
VerifyAllPixelsAreWhiteOrTransparent(image); VerifyAllPixelsAreWhiteOrTransparent(image);
}
} }
[Theory] [Theory]
@ -104,22 +102,20 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
float ty) float ty)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.DebugSave(provider, $"_original");
image.DebugSave(provider, $"_original"); AffineTransformBuilder builder = new AffineTransformBuilder()
AffineTransformBuilder builder = new AffineTransformBuilder() .AppendRotationDegrees(angleDeg)
.AppendRotationDegrees(angleDeg) .AppendScale(new SizeF(sx, sy))
.AppendScale(new SizeF(sx, sy)) .AppendTranslation(new PointF(tx, ty));
.AppendTranslation(new PointF(tx, ty));
this.PrintMatrix(builder.BuildMatrix(image.Size())); this.PrintMatrix(builder.BuildMatrix(image.Size()));
image.Mutate(i => i.Transform(builder, KnownResamplers.Bicubic)); image.Mutate(i => i.Transform(builder, KnownResamplers.Bicubic));
FormattableString testOutputDetails = $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})"; FormattableString testOutputDetails = $"R({angleDeg})_S({sx},{sy})_T({tx},{ty})";
image.DebugSave(provider, testOutputDetails); image.DebugSave(provider, testOutputDetails);
image.CompareToReferenceOutput(ValidatorComparer, provider, testOutputDetails); image.CompareToReferenceOutput(ValidatorComparer, provider, testOutputDetails);
}
} }
[Theory] [Theory]
@ -127,18 +123,16 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
public void Transform_RotateScale_ManuallyCentered<TPixel>(TestImageProvider<TPixel> provider, float angleDeg, float s) public void Transform_RotateScale_ManuallyCentered<TPixel>(TestImageProvider<TPixel> provider, float angleDeg, float s)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ AffineTransformBuilder builder = new AffineTransformBuilder()
AffineTransformBuilder builder = new AffineTransformBuilder() .AppendRotationDegrees(angleDeg)
.AppendRotationDegrees(angleDeg) .AppendScale(new SizeF(s, s));
.AppendScale(new SizeF(s, s));
image.Mutate(i => i.Transform(builder, KnownResamplers.Bicubic)); image.Mutate(i => i.Transform(builder, KnownResamplers.Bicubic));
FormattableString testOutputDetails = $"R({angleDeg})_S({s})"; FormattableString testOutputDetails = $"R({angleDeg})_S({s})";
image.DebugSave(provider, testOutputDetails); image.DebugSave(provider, testOutputDetails);
image.CompareToReferenceOutput(ValidatorComparer, provider, testOutputDetails); image.CompareToReferenceOutput(ValidatorComparer, provider, testOutputDetails);
}
} }
public static readonly TheoryData<int, int, int, int> Transform_IntoRectangle_Data = public static readonly TheoryData<int, int, int, int> Transform_IntoRectangle_Data =
@ -163,17 +157,15 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
{ {
var rectangle = new Rectangle(48, 0, 48, 24); var rectangle = new Rectangle(48, 0, 48, 24);
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.DebugSave(provider, $"_original");
image.DebugSave(provider, $"_original"); AffineTransformBuilder builder = new AffineTransformBuilder()
AffineTransformBuilder builder = new AffineTransformBuilder() .AppendScale(new SizeF(2, 1.5F));
.AppendScale(new SizeF(2, 1.5F));
image.Mutate(i => i.Transform(rectangle, builder, KnownResamplers.Spline)); image.Mutate(i => i.Transform(rectangle, builder, KnownResamplers.Spline));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -183,16 +175,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
{ {
var rectangle = new Rectangle(0, 24, 48, 24); var rectangle = new Rectangle(0, 24, 48, 24);
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ AffineTransformBuilder builder = new AffineTransformBuilder()
AffineTransformBuilder builder = new AffineTransformBuilder() .AppendScale(new SizeF(1F, 2F));
.AppendScale(new SizeF(1F, 2F));
image.Mutate(i => i.Transform(rectangle, builder, KnownResamplers.Spline)); image.Mutate(i => i.Transform(rectangle, builder, KnownResamplers.Spline));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider); image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
@ -201,17 +191,15 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
IResampler sampler = GetResampler(resamplerName); IResampler sampler = GetResampler(resamplerName);
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ AffineTransformBuilder builder = new AffineTransformBuilder()
AffineTransformBuilder builder = new AffineTransformBuilder() .AppendRotationDegrees(50)
.AppendRotationDegrees(50) .AppendScale(new SizeF(.6F, .6F));
.AppendScale(new SizeF(.6F, .6F));
image.Mutate(i => i.Transform(builder, sampler)); image.Mutate(i => i.Transform(builder, sampler));
image.DebugSave(provider, resamplerName); image.DebugSave(provider, resamplerName);
image.CompareToReferenceOutput(ValidatorComparer, provider, resamplerName); image.CompareToReferenceOutput(ValidatorComparer, provider, resamplerName);
}
} }
private static IResampler GetResampler(string name) private static IResampler GetResampler(string name)

68
tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs

@ -64,16 +64,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
IResampler sampler = GetResampler(resamplerName); IResampler sampler = GetResampler(resamplerName);
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder()
ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder() .AppendTaper(TaperSide.Right, TaperCorner.Both, .5F);
.AppendTaper(TaperSide.Right, TaperCorner.Both, .5F);
image.Mutate(i => i.Transform(builder, sampler)); image.Mutate(i => i.Transform(builder, sampler));
image.DebugSave(provider, resamplerName); image.DebugSave(provider, resamplerName);
image.CompareToReferenceOutput(ValidatorComparer, provider, resamplerName); image.CompareToReferenceOutput(ValidatorComparer, provider, resamplerName);
}
} }
[Theory] [Theory]
@ -81,17 +79,15 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
public void Transform_WithTaperMatrix<TPixel>(TestImageProvider<TPixel> provider, TaperSide taperSide, TaperCorner taperCorner) public void Transform_WithTaperMatrix<TPixel>(TestImageProvider<TPixel> provider, TaperSide taperSide, TaperCorner taperCorner)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder()
ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder() .AppendTaper(taperSide, taperCorner, .5F);
.AppendTaper(taperSide, taperCorner, .5F);
image.Mutate(i => i.Transform(builder)); image.Mutate(i => i.Transform(builder));
FormattableString testOutputDetails = $"{taperSide}-{taperCorner}"; FormattableString testOutputDetails = $"{taperSide}-{taperCorner}";
image.DebugSave(provider, testOutputDetails); image.DebugSave(provider, testOutputDetails);
image.CompareFirstFrameToReferenceOutput(TolerantComparer, provider, testOutputDetails); image.CompareFirstFrameToReferenceOutput(TolerantComparer, provider, testOutputDetails);
}
} }
[Theory] [Theory]
@ -104,19 +100,17 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
// This test matches the output described in the example at // This test matches the output described in the example at
// https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/transforms/non-affine // https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/transforms/non-affine
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ Matrix4x4 matrix = Matrix4x4.Identity;
Matrix4x4 matrix = Matrix4x4.Identity; matrix.M14 = 0.01F;
matrix.M14 = 0.01F;
ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder() ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder()
.AppendMatrix(matrix); .AppendMatrix(matrix);
image.Mutate(i => i.Transform(builder)); image.Mutate(i => i.Transform(builder));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(TolerantComparer, provider); image.CompareToReferenceOutput(TolerantComparer, provider);
}
} }
[Theory] [Theory]
@ -126,24 +120,22 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
{ {
// https://jsfiddle.net/dFrHS/545/ // https://jsfiddle.net/dFrHS/545/
// https://github.com/SixLabors/ImageSharp/issues/787 // https://github.com/SixLabors/ImageSharp/issues/787
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{
#pragma warning disable SA1117 // Parameters should be on same line or separate lines #pragma warning disable SA1117 // Parameters should be on same line or separate lines
var matrix = new Matrix4x4( var matrix = new Matrix4x4(
0.260987f, -0.434909f, 0, -0.0022184f, 0.260987f, -0.434909f, 0, -0.0022184f,
0.373196f, 0.949882f, 0, -0.000312129f, 0.373196f, 0.949882f, 0, -0.000312129f,
0, 0, 1, 0, 0, 0, 1, 0,
52, 165, 0, 1); 52, 165, 0, 1);
#pragma warning restore SA1117 // Parameters should be on same line or separate lines #pragma warning restore SA1117 // Parameters should be on same line or separate lines
ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder() ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder()
.AppendMatrix(matrix); .AppendMatrix(matrix);
image.Mutate(i => i.Transform(builder)); image.Mutate(i => i.Transform(builder));
image.DebugSave(provider); image.DebugSave(provider);
image.CompareToReferenceOutput(TolerantComparer, provider); image.CompareToReferenceOutput(TolerantComparer, provider);
}
} }
private static IResampler GetResampler(string name) private static IResampler GetResampler(string name)

22
tests/ImageSharp.Tests/Processing/Transforms/TransformsHelpersTest.cs

@ -15,21 +15,19 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms
{ {
int xy = 1; int xy = 1;
using (var img = new Image<A8>(xy, xy)) using var img = new Image<A8>(xy, xy);
{ var profile = new ExifProfile();
var profile = new ExifProfile(); img.Metadata.ExifProfile = profile;
img.Metadata.ExifProfile = profile; profile.SetValue(ExifTag.PixelXDimension, xy + ushort.MaxValue);
profile.SetValue(ExifTag.PixelXDimension, xy + ushort.MaxValue); profile.SetValue(ExifTag.PixelYDimension, xy + ushort.MaxValue);
profile.SetValue(ExifTag.PixelYDimension, xy + ushort.MaxValue);
Assert.Equal(ExifDataType.Long, profile.GetValue(ExifTag.PixelXDimension).DataType); Assert.Equal(ExifDataType.Long, profile.GetValue(ExifTag.PixelXDimension).DataType);
Assert.Equal(ExifDataType.Long, profile.GetValue(ExifTag.PixelYDimension).DataType); Assert.Equal(ExifDataType.Long, profile.GetValue(ExifTag.PixelYDimension).DataType);
TransformProcessorHelpers.UpdateDimensionalMetadata(img); TransformProcessorHelpers.UpdateDimensionalMetadata(img);
Assert.Equal(ExifDataType.Short, profile.GetValue(ExifTag.PixelXDimension).DataType); Assert.Equal(ExifDataType.Short, profile.GetValue(ExifTag.PixelXDimension).DataType);
Assert.Equal(ExifDataType.Short, profile.GetValue(ExifTag.PixelYDimension).DataType); Assert.Equal(ExifDataType.Short, profile.GetValue(ExifTag.PixelYDimension).DataType);
}
} }
} }
} }

28
tests/ImageSharp.Tests/ProfilingBenchmarks/LoadResizeSaveProfilingBenchmarks.cs

@ -26,21 +26,19 @@ namespace SixLabors.ImageSharp.Tests.ProfilingBenchmarks
byte[] imageBytes = TestFile.Create(imagePath).Bytes; byte[] imageBytes = TestFile.Create(imagePath).Bytes;
using (var ms = new MemoryStream()) using var ms = new MemoryStream();
{ this.Measure(
this.Measure( 30,
30, () =>
() => {
{ using (var image = Image.Load(configuration, imageBytes))
using (var image = Image.Load(configuration, imageBytes)) {
{ image.Mutate(x => x.Resize(image.Size() / 4));
image.Mutate(x => x.Resize(image.Size() / 4)); image.SaveAsJpeg(ms);
image.SaveAsJpeg(ms); }
}
ms.Seek(0, SeekOrigin.Begin);
ms.Seek(0, SeekOrigin.Begin); });
});
}
} }
} }
} }

10
tests/ImageSharp.Tests/ProfilingBenchmarks/ResizeProfilingBenchmarks.cs

@ -29,12 +29,10 @@ namespace SixLabors.ImageSharp.Tests.ProfilingBenchmarks
this.Measure( this.Measure(
this.ExecutionCount, this.ExecutionCount,
() => () =>
{ {
using (var image = new Image<Rgba32>(this.configuration, width, height)) using var image = new Image<Rgba32>(this.configuration, width, height);
{ image.Mutate(x => x.Resize(width / 5, height / 5));
image.Mutate(x => x.Resize(width / 5, height / 5)); });
}
});
} }
} }
} }

40
tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs

@ -41,20 +41,18 @@ namespace SixLabors.ImageSharp.Tests
bool dither) bool dither)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ Assert.True(image[0, 0].Equals(default(TPixel)));
Assert.True(image[0, 0].Equals(default(TPixel)));
var quantizer = new OctreeQuantizer(dither); var quantizer = new OctreeQuantizer(dither);
foreach (ImageFrame<TPixel> frame in image.Frames) foreach (ImageFrame<TPixel> frame in image.Frames)
{ {
IQuantizedFrame<TPixel> quantized = IQuantizedFrame<TPixel> quantized =
quantizer.CreateFrameQuantizer<TPixel>(this.Configuration).QuantizeFrame(frame); quantizer.CreateFrameQuantizer<TPixel>(this.Configuration).QuantizeFrame(frame);
int index = this.GetTransparentIndex(quantized); int index = this.GetTransparentIndex(quantized);
Assert.Equal(index, quantized.GetPixelSpan()[0]); Assert.Equal(index, quantized.GetPixelSpan()[0]);
}
} }
} }
@ -64,20 +62,18 @@ namespace SixLabors.ImageSharp.Tests
public void WuQuantizerYieldsCorrectTransparentPixel<TPixel>(TestImageProvider<TPixel> provider, bool dither) public void WuQuantizerYieldsCorrectTransparentPixel<TPixel>(TestImageProvider<TPixel> provider, bool dither)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ Assert.True(image[0, 0].Equals(default(TPixel)));
Assert.True(image[0, 0].Equals(default(TPixel)));
var quantizer = new WuQuantizer(dither); var quantizer = new WuQuantizer(dither);
foreach (ImageFrame<TPixel> frame in image.Frames) foreach (ImageFrame<TPixel> frame in image.Frames)
{ {
IQuantizedFrame<TPixel> quantized = IQuantizedFrame<TPixel> quantized =
quantizer.CreateFrameQuantizer<TPixel>(this.Configuration).QuantizeFrame(frame); quantizer.CreateFrameQuantizer<TPixel>(this.Configuration).QuantizeFrame(frame);
int index = this.GetTransparentIndex(quantized); int index = this.GetTransparentIndex(quantized);
Assert.Equal(index, quantized.GetPixelSpan()[0]); Assert.Equal(index, quantized.GetPixelSpan()[0]);
}
} }
} }

176
tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs

@ -17,15 +17,13 @@ namespace SixLabors.ImageSharp.Tests.Quantization
Configuration config = Configuration.Default; Configuration config = Configuration.Default;
var quantizer = new WuQuantizer(false); var quantizer = new WuQuantizer(false);
using (var image = new Image<Rgba32>(config, 1, 1, Rgba32.Black)) using var image = new Image<Rgba32>(config, 1, 1, Rgba32.Black);
using (IQuantizedFrame<Rgba32> result = quantizer.CreateFrameQuantizer<Rgba32>(config).QuantizeFrame(image.Frames[0])) using IQuantizedFrame<Rgba32> result = quantizer.CreateFrameQuantizer<Rgba32>(config).QuantizeFrame(image.Frames[0]);
{ Assert.Equal(1, result.Palette.Length);
Assert.Equal(1, result.Palette.Length); Assert.Equal(1, result.GetPixelSpan().Length);
Assert.Equal(1, result.GetPixelSpan().Length);
Assert.Equal(Rgba32.Black, result.Palette.Span[0]); Assert.Equal(Rgba32.Black, result.Palette.Span[0]);
Assert.Equal(0, result.GetPixelSpan()[0]); Assert.Equal(0, result.GetPixelSpan()[0]);
}
} }
[Fact] [Fact]
@ -34,15 +32,13 @@ namespace SixLabors.ImageSharp.Tests.Quantization
Configuration config = Configuration.Default; Configuration config = Configuration.Default;
var quantizer = new WuQuantizer(false); var quantizer = new WuQuantizer(false);
using (var image = new Image<Rgba32>(config, 1, 1, default(Rgba32))) using var image = new Image<Rgba32>(config, 1, 1, default(Rgba32));
using (IQuantizedFrame<Rgba32> result = quantizer.CreateFrameQuantizer<Rgba32>(config).QuantizeFrame(image.Frames[0])) using IQuantizedFrame<Rgba32> result = quantizer.CreateFrameQuantizer<Rgba32>(config).QuantizeFrame(image.Frames[0]);
{ Assert.Equal(1, result.Palette.Length);
Assert.Equal(1, result.Palette.Length); Assert.Equal(1, result.GetPixelSpan().Length);
Assert.Equal(1, result.GetPixelSpan().Length);
Assert.Equal(default, result.Palette.Span[0]); Assert.Equal(default, result.Palette.Span[0]);
Assert.Equal(0, result.GetPixelSpan()[0]); Assert.Equal(0, result.GetPixelSpan()[0]);
}
} }
[Fact] [Fact]
@ -63,46 +59,42 @@ namespace SixLabors.ImageSharp.Tests.Quantization
[Fact] [Fact]
public void Palette256() public void Palette256()
{ {
using (var image = new Image<Rgba32>(1, 256)) using var image = new Image<Rgba32>(1, 256);
for (int i = 0; i < 256; i++)
{ {
for (int i = 0; i < 256; i++) byte r = (byte)((i % 4) * 85);
{ byte g = (byte)(((i / 4) % 4) * 85);
byte r = (byte)((i % 4) * 85); byte b = (byte)(((i / 16) % 4) * 85);
byte g = (byte)(((i / 4) % 4) * 85); byte a = (byte)((i / 64) * 85);
byte b = (byte)(((i / 16) % 4) * 85);
byte a = (byte)((i / 64) * 85);
image[0, i] = new Rgba32(r, g, b, a); image[0, i] = new Rgba32(r, g, b, a);
} }
Configuration config = Configuration.Default; Configuration config = Configuration.Default;
var quantizer = new WuQuantizer(false); var quantizer = new WuQuantizer(false);
using (IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(config)) using IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(config);
using (IQuantizedFrame<Rgba32> result = frameQuantizer.QuantizeFrame(image.Frames[0])) using IQuantizedFrame<Rgba32> result = frameQuantizer.QuantizeFrame(image.Frames[0]);
Assert.Equal(256, result.Palette.Length);
Assert.Equal(256, result.GetPixelSpan().Length);
var actualImage = new Image<Rgba32>(1, 256);
ReadOnlySpan<Rgba32> paletteSpan = result.Palette.Span;
int paletteCount = result.Palette.Length - 1;
for (int y = 0; y < actualImage.Height; y++)
{
Span<Rgba32> row = actualImage.GetPixelRowSpan(y);
ReadOnlySpan<byte> quantizedPixelSpan = result.GetPixelSpan();
int yy = y * actualImage.Width;
for (int x = 0; x < actualImage.Width; x++)
{ {
Assert.Equal(256, result.Palette.Length); int i = x + yy;
Assert.Equal(256, result.GetPixelSpan().Length); row[x] = paletteSpan[Math.Min(paletteCount, quantizedPixelSpan[i])];
var actualImage = new Image<Rgba32>(1, 256);
ReadOnlySpan<Rgba32> paletteSpan = result.Palette.Span;
int paletteCount = result.Palette.Length - 1;
for (int y = 0; y < actualImage.Height; y++)
{
Span<Rgba32> row = actualImage.GetPixelRowSpan(y);
ReadOnlySpan<byte> quantizedPixelSpan = result.GetPixelSpan();
int yy = y * actualImage.Width;
for (int x = 0; x < actualImage.Width; x++)
{
int i = x + yy;
row[x] = paletteSpan[Math.Min(paletteCount, quantizedPixelSpan[i])];
}
}
Assert.True(image.GetPixelSpan().SequenceEqual(actualImage.GetPixelSpan()));
} }
} }
Assert.True(image.GetPixelSpan().SequenceEqual(actualImage.GetPixelSpan()));
} }
[Theory] [Theory]
@ -111,63 +103,55 @@ namespace SixLabors.ImageSharp.Tests.Quantization
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
// See https://github.com/SixLabors/ImageSharp/issues/866 // See https://github.com/SixLabors/ImageSharp/issues/866
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ Configuration config = Configuration.Default;
Configuration config = Configuration.Default; var quantizer = new WuQuantizer(false);
var quantizer = new WuQuantizer(false); using IFrameQuantizer<TPixel> frameQuantizer = quantizer.CreateFrameQuantizer<TPixel>(config);
using (IFrameQuantizer<TPixel> frameQuantizer = quantizer.CreateFrameQuantizer<TPixel>(config)) using IQuantizedFrame<TPixel> result = frameQuantizer.QuantizeFrame(image.Frames[0]);
using (IQuantizedFrame<TPixel> result = frameQuantizer.QuantizeFrame(image.Frames[0])) Assert.Equal(48, result.Palette.Length);
{
Assert.Equal(48, result.Palette.Length);
}
}
} }
private static void TestScale(Func<byte, Rgba32> pixelBuilder) private static void TestScale(Func<byte, Rgba32> pixelBuilder)
{ {
using (var image = new Image<Rgba32>(1, 256)) using var image = new Image<Rgba32>(1, 256);
using (var expectedImage = new Image<Rgba32>(1, 256)) using var expectedImage = new Image<Rgba32>(1, 256);
using (var actualImage = new Image<Rgba32>(1, 256)) using var actualImage = new Image<Rgba32>(1, 256);
for (int i = 0; i < 256; i++)
{ {
for (int i = 0; i < 256; i++) byte c = (byte)i;
{ image[0, i] = pixelBuilder.Invoke(c);
byte c = (byte)i; }
image[0, i] = pixelBuilder.Invoke(c);
}
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
{ {
byte c = (byte)((i & ~7) + 4); byte c = (byte)((i & ~7) + 4);
expectedImage[0, i] = pixelBuilder.Invoke(c); expectedImage[0, i] = pixelBuilder.Invoke(c);
} }
Configuration config = Configuration.Default;
var quantizer = new WuQuantizer(false);
Configuration config = Configuration.Default; using IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(config);
var quantizer = new WuQuantizer(false); using IQuantizedFrame<Rgba32> result = frameQuantizer.QuantizeFrame(image.Frames[0]);
Assert.Equal(4 * 8, result.Palette.Length);
Assert.Equal(256, result.GetPixelSpan().Length);
using (IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(config)) ReadOnlySpan<Rgba32> paletteSpan = result.Palette.Span;
using (IQuantizedFrame<Rgba32> result = frameQuantizer.QuantizeFrame(image.Frames[0])) int paletteCount = result.Palette.Length - 1;
for (int y = 0; y < actualImage.Height; y++)
{
Span<Rgba32> row = actualImage.GetPixelRowSpan(y);
ReadOnlySpan<byte> quantizedPixelSpan = result.GetPixelSpan();
int yy = y * actualImage.Width;
for (int x = 0; x < actualImage.Width; x++)
{ {
Assert.Equal(4 * 8, result.Palette.Length); int i = x + yy;
Assert.Equal(256, result.GetPixelSpan().Length); row[x] = paletteSpan[Math.Min(paletteCount, quantizedPixelSpan[i])];
ReadOnlySpan<Rgba32> paletteSpan = result.Palette.Span;
int paletteCount = result.Palette.Length - 1;
for (int y = 0; y < actualImage.Height; y++)
{
Span<Rgba32> row = actualImage.GetPixelRowSpan(y);
ReadOnlySpan<byte> quantizedPixelSpan = result.GetPixelSpan();
int yy = y * actualImage.Width;
for (int x = 0; x < actualImage.Width; x++)
{
int i = x + yy;
row[x] = paletteSpan[Math.Min(paletteCount, quantizedPixelSpan[i])];
}
}
} }
Assert.True(expectedImage.GetPixelSpan().SequenceEqual(actualImage.GetPixelSpan()));
} }
Assert.True(expectedImage.GetPixelSpan().SequenceEqual(actualImage.GetPixelSpan()));
} }
} }
} }

0
tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs → tests/ImageSharp.Tests/TestUtilities/ImageProviders/ITestImageProvider.cs

12
tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

@ -223,14 +223,10 @@ namespace SixLabors.ImageSharp.Tests
for (int i = 0; i < image.Frames.Count; i++) for (int i = 0; i < image.Frames.Count; i++)
{ {
using (Image<TPixel> frameImage = image.Frames.CloneFrame(i)) using Image<TPixel> frameImage = image.Frames.CloneFrame(i);
{ string filePath = files[i];
string filePath = files[i]; using FileStream stream = File.OpenWrite(filePath);
using (FileStream stream = File.OpenWrite(filePath)) frameImage.Save(stream, encoder);
{
frameImage.Save(stream, encoder);
}
}
} }
return files; return files;

64
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs

@ -20,42 +20,38 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream) public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (var magickImage = new MagickImage(stream)) 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(PixelMapping.RGBA);
PixelOperations<TPixel>.Instance.FromRgba32Bytes(
configuration,
data,
resultPixels,
resultPixels.Length);
}
else if (magickImage.Depth == 16)
{ {
var result = new Image<TPixel>(configuration, magickImage.Width, magickImage.Height); ushort[] data = pixels.ToShortArray(PixelMapping.RGBA);
Span<TPixel> resultPixels = result.GetPixelSpan(); Span<byte> bytes = MemoryMarshal.Cast<ushort, byte>(data.AsSpan());
using (IPixelCollection pixels = magickImage.GetPixelsUnsafe()) PixelOperations<TPixel>.Instance.FromRgba64Bytes(
{ configuration,
if (magickImage.Depth == 8) bytes,
{ resultPixels,
byte[] data = pixels.ToByteArray(PixelMapping.RGBA); resultPixels.Length);
PixelOperations<TPixel>.Instance.FromRgba32Bytes(
configuration,
data,
resultPixels,
resultPixels.Length);
}
else if (magickImage.Depth == 16)
{
ushort[] data = pixels.ToShortArray(PixelMapping.RGBA);
Span<byte> bytes = MemoryMarshal.Cast<ushort, byte>(data.AsSpan());
PixelOperations<TPixel>.Instance.FromRgba64Bytes(
configuration,
bytes,
resultPixels,
resultPixels.Length);
}
else
{
throw new InvalidOperationException();
}
}
return result;
} }
else
{
throw new InvalidOperationException();
}
return result;
} }
public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream); public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream);

60
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs

@ -49,22 +49,20 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
Configuration configuration = image.GetConfiguration(); Configuration configuration = image.GetConfiguration();
using (IMemoryOwner<Bgra32> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgra32>(w)) using IMemoryOwner<Bgra32> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgra32>(w);
fixed (Bgra32* destPtr = &workBuffer.GetReference())
{ {
fixed (Bgra32* destPtr = &workBuffer.GetReference()) for (int y = 0; y < h; y++)
{ {
for (int y = 0; y < h; y++) Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
{
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y); byte* sourcePtr = sourcePtrBase + (data.Stride * y);
byte* sourcePtr = sourcePtrBase + (data.Stride * y); Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
PixelOperations<TPixel>.Instance.FromBgra32(
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount); configuration,
PixelOperations<TPixel>.Instance.FromBgra32( workBuffer.GetSpan().Slice(0, w),
configuration, row);
workBuffer.GetSpan().Slice(0, w),
row);
}
} }
} }
} }
@ -108,19 +106,17 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
Configuration configuration = image.GetConfiguration(); Configuration configuration = image.GetConfiguration();
using (IMemoryOwner<Bgr24> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgr24>(w)) using IMemoryOwner<Bgr24> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgr24>(w);
fixed (Bgr24* destPtr = &workBuffer.GetReference())
{ {
fixed (Bgr24* destPtr = &workBuffer.GetReference()) for (int y = 0; y < h; y++)
{ {
for (int y = 0; y < h; y++) Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
{
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
byte* sourcePtr = sourcePtrBase + (data.Stride * y); byte* sourcePtr = sourcePtrBase + (data.Stride * y);
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount); Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
PixelOperations<TPixel>.Instance.FromBgr24(configuration, workBuffer.GetSpan().Slice(0, w), row); PixelOperations<TPixel>.Instance.FromBgr24(configuration, workBuffer.GetSpan().Slice(0, w), row);
}
} }
} }
} }
@ -149,18 +145,16 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
long destRowByteCount = data.Stride; long destRowByteCount = data.Stride;
long sourceRowByteCount = w * sizeof(Bgra32); long sourceRowByteCount = w * sizeof(Bgra32);
using (IMemoryOwner<Bgra32> workBuffer = image.GetConfiguration().MemoryAllocator.Allocate<Bgra32>(w)) using IMemoryOwner<Bgra32> workBuffer = image.GetConfiguration().MemoryAllocator.Allocate<Bgra32>(w);
fixed (Bgra32* sourcePtr = &workBuffer.GetReference())
{ {
fixed (Bgra32* sourcePtr = &workBuffer.GetReference()) for (int y = 0; y < h; y++)
{ {
for (int y = 0; y < h; y++) Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
{ PixelOperations<TPixel>.Instance.ToBgra32(configuration, row, workBuffer.GetSpan());
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y); byte* destPtr = destPtrBase + (data.Stride * y);
PixelOperations<TPixel>.Instance.ToBgra32(configuration, row, workBuffer.GetSpan());
byte* destPtr = destPtrBase + (data.Stride * y); Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
}
} }
} }
} }

48
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs

@ -16,40 +16,32 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream) public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (var sourceBitmap = new System.Drawing.Bitmap(stream)) using var sourceBitmap = new System.Drawing.Bitmap(stream);
if (sourceBitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
{ {
if (sourceBitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) return SystemDrawingBridge.From32bppArgbSystemDrawingBitmap<TPixel>(sourceBitmap);
{
return SystemDrawingBridge.From32bppArgbSystemDrawingBitmap<TPixel>(sourceBitmap);
}
using (var convertedBitmap = new System.Drawing.Bitmap(
sourceBitmap.Width,
sourceBitmap.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using (var g = System.Drawing.Graphics.FromImage(convertedBitmap))
{
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.DrawImage(sourceBitmap, 0, 0, sourceBitmap.Width, sourceBitmap.Height);
}
return SystemDrawingBridge.From32bppArgbSystemDrawingBitmap<TPixel>(convertedBitmap);
}
} }
using var convertedBitmap = new System.Drawing.Bitmap(
sourceBitmap.Width,
sourceBitmap.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using var g = System.Drawing.Graphics.FromImage(convertedBitmap);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.DrawImage(sourceBitmap, 0, 0, sourceBitmap.Width, sourceBitmap.Height);
return SystemDrawingBridge.From32bppArgbSystemDrawingBitmap<TPixel>(convertedBitmap);
} }
public IImageInfo Identify(Configuration configuration, Stream stream) public IImageInfo Identify(Configuration configuration, Stream stream)
{ {
using (var sourceBitmap = new System.Drawing.Bitmap(stream)) using var sourceBitmap = new System.Drawing.Bitmap(stream);
{ var pixelType = new PixelTypeInfo(System.Drawing.Image.GetPixelFormatSize(sourceBitmap.PixelFormat));
var pixelType = new PixelTypeInfo(System.Drawing.Image.GetPixelFormatSize(sourceBitmap.PixelFormat)); return new ImageInfo(pixelType, sourceBitmap.Width, sourceBitmap.Height, new ImageMetadata());
return new ImageInfo(pixelType, sourceBitmap.Width, sourceBitmap.Height, new ImageMetadata());
}
} }
public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream); public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream);

8
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceEncoder.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System.Drawing.Imaging; using System.Drawing.Imaging;
@ -25,10 +25,8 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
public void Encode<TPixel>(Image<TPixel> image, Stream stream) public void Encode<TPixel>(Image<TPixel> image, Stream stream)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (System.Drawing.Bitmap sdBitmap = SystemDrawingBridge.To32bppArgbSystemDrawingBitmap(image)) using System.Drawing.Bitmap sdBitmap = SystemDrawingBridge.To32bppArgbSystemDrawingBitmap(image);
{ sdBitmap.Save(stream, this.imageFormat);
sdBitmap.Save(stream, this.imageFormat);
}
} }
} }
} }

62
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -287,19 +287,17 @@ namespace SixLabors.ImageSharp.Tests
bool appendSourceFileOrDescription = true) bool appendSourceFileOrDescription = true)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (var firstFrameOnlyImage = new Image<TPixel>(image.Width, image.Height)) using var firstFrameOnlyImage = new Image<TPixel>(image.Width, image.Height);
using (Image<TPixel> referenceImage = GetReferenceOutputImage<TPixel>( using Image<TPixel> referenceImage = GetReferenceOutputImage<TPixel>(
provider, provider,
testOutputDetails, testOutputDetails,
extension, extension,
appendPixelTypeToFileName, appendPixelTypeToFileName,
appendSourceFileOrDescription)) appendSourceFileOrDescription);
{ firstFrameOnlyImage.Frames.AddFrame(image.Frames.RootFrame);
firstFrameOnlyImage.Frames.AddFrame(image.Frames.RootFrame); firstFrameOnlyImage.Frames.RemoveFrame(0);
firstFrameOnlyImage.Frames.RemoveFrame(0);
comparer.VerifySimilarity(referenceImage, firstFrameOnlyImage); comparer.VerifySimilarity(referenceImage, firstFrameOnlyImage);
}
return image; return image;
} }
@ -405,13 +403,11 @@ namespace SixLabors.ImageSharp.Tests
bool appendPixelTypeToFileName = true) bool appendPixelTypeToFileName = true)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> referenceImage = provider.GetReferenceOutputImage<TPixel>( using Image<TPixel> referenceImage = provider.GetReferenceOutputImage<TPixel>(
testOutputDetails, testOutputDetails,
extension, extension,
appendPixelTypeToFileName)) appendPixelTypeToFileName);
{ return comparer.CompareImages(referenceImage, image);
return comparer.CompareImages(referenceImage, image);
}
} }
public static Image<TPixel> ComparePixelBufferTo<TPixel>( public static Image<TPixel> ComparePixelBufferTo<TPixel>(
@ -556,23 +552,21 @@ namespace SixLabors.ImageSharp.Tests
bool appendSourceFileOrDescription = true) bool appendSourceFileOrDescription = true)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ operation(image);
operation(image);
image.DebugSave(
image.DebugSave( provider,
provider, testOutputDetails,
testOutputDetails, appendPixelTypeToFileName: appendPixelTypeToFileName,
appendPixelTypeToFileName: appendPixelTypeToFileName, appendSourceFileOrDescription: appendSourceFileOrDescription);
appendSourceFileOrDescription: appendSourceFileOrDescription);
image.CompareToReferenceOutput(
image.CompareToReferenceOutput( comparer,
comparer, provider,
provider, testOutputDetails,
testOutputDetails, appendPixelTypeToFileName: appendPixelTypeToFileName,
appendPixelTypeToFileName: appendPixelTypeToFileName, appendSourceFileOrDescription: appendSourceFileOrDescription);
appendSourceFileOrDescription: appendSourceFileOrDescription);
}
} }
/// <summary> /// <summary>
@ -660,11 +654,9 @@ namespace SixLabors.ImageSharp.Tests
referenceDecoder = referenceDecoder ?? TestEnvironment.GetReferenceDecoder(actualOutputFile); referenceDecoder = referenceDecoder ?? TestEnvironment.GetReferenceDecoder(actualOutputFile);
using (var actualImage = Image.Load<TPixel>(actualOutputFile, referenceDecoder)) using var actualImage = Image.Load<TPixel>(actualOutputFile, referenceDecoder);
{ ImageComparer comparer = customComparer ?? ImageComparer.Exact;
ImageComparer comparer = customComparer ?? ImageComparer.Exact; comparer.VerifySimilarity(actualImage, image);
comparer.VerifySimilarity(actualImage, image);
}
} }
internal static Image<Rgba32> ToGrayscaleImage(this Buffer2D<float> buffer, float scale) internal static Image<Rgba32> ToGrayscaleImage(this Buffer2D<float> buffer, float scale)

104
tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs

@ -34,14 +34,10 @@ namespace SixLabors.ImageSharp.Tests
int pixelThreshold) int pixelThreshold)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ using Image<TPixel> clone = image.Clone();
using (Image<TPixel> clone = image.Clone()) var comparer = ImageComparer.Tolerant(imageThreshold, pixelThreshold);
{ comparer.VerifySimilarity(image, clone);
var comparer = ImageComparer.Tolerant(imageThreshold, pixelThreshold);
comparer.VerifySimilarity(image, clone);
}
}
} }
[Theory] [Theory]
@ -49,16 +45,12 @@ namespace SixLabors.ImageSharp.Tests
public void TolerantImageComparer_ApprovesSimilarityBelowTolerance<TPixel>(TestImageProvider<TPixel> provider) public void TolerantImageComparer_ApprovesSimilarityBelowTolerance<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ using Image<TPixel> clone = image.Clone();
using (Image<TPixel> clone = image.Clone()) ImagingTestCaseUtility.ModifyPixel(clone, 0, 0, 1);
{
ImagingTestCaseUtility.ModifyPixel(clone, 0, 0, 1);
var comparer = ImageComparer.Tolerant(); var comparer = ImageComparer.Tolerant();
comparer.VerifySimilarity(image, clone); comparer.VerifySimilarity(image, clone);
}
}
} }
[Theory] [Theory]
@ -66,22 +58,18 @@ namespace SixLabors.ImageSharp.Tests
public void TolerantImageComparer_DoesNotApproveSimilarityAboveTolerance<TPixel>(TestImageProvider<TPixel> provider) public void TolerantImageComparer_DoesNotApproveSimilarityAboveTolerance<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ using Image<TPixel> clone = image.Clone();
using (Image<TPixel> clone = image.Clone()) byte perChannelChange = 20;
{ ImagingTestCaseUtility.ModifyPixel(clone, 3, 1, perChannelChange);
byte perChannelChange = 20;
ImagingTestCaseUtility.ModifyPixel(clone, 3, 1, perChannelChange);
var comparer = ImageComparer.Tolerant(); var comparer = ImageComparer.Tolerant();
ImageDifferenceIsOverThresholdException ex = Assert.ThrowsAny<ImageDifferenceIsOverThresholdException>( ImageDifferenceIsOverThresholdException ex = Assert.ThrowsAny<ImageDifferenceIsOverThresholdException>(
() => comparer.VerifySimilarity(image, clone)); () => comparer.VerifySimilarity(image, clone));
PixelDifference diff = ex.Reports.Single().Differences.Single(); PixelDifference diff = ex.Reports.Single().Differences.Single();
Assert.Equal(new Point(3, 1), diff.Position); Assert.Equal(new Point(3, 1), diff.Position);
}
}
} }
[Theory] [Theory]
@ -89,18 +77,14 @@ namespace SixLabors.ImageSharp.Tests
public void TolerantImageComparer_TestPerPixelThreshold<TPixel>(TestImageProvider<TPixel> provider) public void TolerantImageComparer_TestPerPixelThreshold<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ using Image<TPixel> clone = image.Clone();
using (Image<TPixel> clone = image.Clone()) ImagingTestCaseUtility.ModifyPixel(clone, 0, 0, 1);
{ ImagingTestCaseUtility.ModifyPixel(clone, 1, 0, 1);
ImagingTestCaseUtility.ModifyPixel(clone, 0, 0, 1); ImagingTestCaseUtility.ModifyPixel(clone, 2, 0, 1);
ImagingTestCaseUtility.ModifyPixel(clone, 1, 0, 1);
ImagingTestCaseUtility.ModifyPixel(clone, 2, 0, 1); var comparer = ImageComparer.Tolerant(perPixelManhattanThreshold: 257 * 3);
comparer.VerifySimilarity(image, clone);
var comparer = ImageComparer.Tolerant(perPixelManhattanThreshold: 257 * 3);
comparer.VerifySimilarity(image, clone);
}
}
} }
[Theory] [Theory]
@ -109,19 +93,15 @@ namespace SixLabors.ImageSharp.Tests
public void VerifySimilarity_ThrowsOnSizeMismatch<TPixel>(TestImageProvider<TPixel> provider, int w, int h) public void VerifySimilarity_ThrowsOnSizeMismatch<TPixel>(TestImageProvider<TPixel> provider, int w, int h)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ using Image<TPixel> clone = image.Clone(ctx => ctx.Resize(w, h));
using (Image<TPixel> clone = image.Clone(ctx => ctx.Resize(w, h))) ImageDimensionsMismatchException ex = Assert.ThrowsAny<ImageDimensionsMismatchException>(
() =>
{ {
ImageDimensionsMismatchException ex = Assert.ThrowsAny<ImageDimensionsMismatchException>( ImageComparer comparer = Mock.Of<ImageComparer>();
() => comparer.VerifySimilarity(image, clone);
{ });
ImageComparer comparer = Mock.Of<ImageComparer>(); this.Output.WriteLine(ex.Message);
comparer.VerifySimilarity(image, clone);
});
this.Output.WriteLine(ex.Message);
}
}
} }
[Theory] [Theory]
@ -129,18 +109,14 @@ namespace SixLabors.ImageSharp.Tests
public void VerifySimilarity_WhenAnImageFrameIsDifferent_Reports<TPixel>(TestImageProvider<TPixel> provider) public void VerifySimilarity_WhenAnImageFrameIsDifferent_Reports<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ using Image<TPixel> clone = image.Clone();
using (Image<TPixel> clone = image.Clone()) ImagingTestCaseUtility.ModifyPixel(clone.Frames[0], 42, 43, 1);
{
ImagingTestCaseUtility.ModifyPixel(clone.Frames[0], 42, 43, 1);
IEnumerable<ImageSimilarityReport> reports = ImageComparer.Exact.CompareImages(image, clone); IEnumerable<ImageSimilarityReport> reports = ImageComparer.Exact.CompareImages(image, clone);
PixelDifference difference = reports.Single().Differences.Single(); PixelDifference difference = reports.Single().Differences.Single();
Assert.Equal(new Point(42, 43), difference.Position); Assert.Equal(new Point(42, 43), difference.Position);
}
}
} }
[Theory] [Theory]

32
tests/ImageSharp.Tests/TestUtilities/Tests/MagickReferenceCodecTests.cs

@ -39,17 +39,15 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests
ImageComparer comparer = ImageComparer.Exact; ImageComparer comparer = ImageComparer.Exact;
using (var mImage = Image.Load<TPixel>(path, magickDecoder)) using var mImage = Image.Load<TPixel>(path, magickDecoder);
using (var sdImage = Image.Load<TPixel>(path, sdDecoder)) using var sdImage = Image.Load<TPixel>(path, sdDecoder);
{ ImageSimilarityReport<TPixel, TPixel> report = comparer.CompareImagesOrFrames(mImage, sdImage);
ImageSimilarityReport<TPixel, TPixel> report = comparer.CompareImagesOrFrames(mImage, sdImage);
mImage.DebugSave(dummyProvider); mImage.DebugSave(dummyProvider);
if (TestEnvironment.IsWindows) if (TestEnvironment.IsWindows)
{ {
Assert.True(report.IsEmpty); Assert.True(report.IsEmpty);
}
} }
} }
@ -70,17 +68,15 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests
// 1020 == 4 * 255 (Equivalent to manhattan distance of 1+1+1+1=4 in Rgba32 space) // 1020 == 4 * 255 (Equivalent to manhattan distance of 1+1+1+1=4 in Rgba32 space)
var comparer = ImageComparer.TolerantPercentage(1, 1020); var comparer = ImageComparer.TolerantPercentage(1, 1020);
using (var mImage = Image.Load<TPixel>(path, magickDecoder)) using var mImage = Image.Load<TPixel>(path, magickDecoder);
using (var sdImage = Image.Load<TPixel>(path, sdDecoder)) using var sdImage = Image.Load<TPixel>(path, sdDecoder);
{ ImageSimilarityReport<TPixel, TPixel> report = comparer.CompareImagesOrFrames(mImage, sdImage);
ImageSimilarityReport<TPixel, TPixel> report = comparer.CompareImagesOrFrames(mImage, sdImage);
mImage.DebugSave(dummyProvider); mImage.DebugSave(dummyProvider);
if (TestEnvironment.IsWindows) if (TestEnvironment.IsWindows)
{ {
Assert.True(report.IsEmpty); Assert.True(report.IsEmpty);
}
} }
} }
} }

78
tests/ImageSharp.Tests/TestUtilities/Tests/SystemDrawingReferenceCodecTests.cs

@ -26,14 +26,10 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests
public void To32bppArgbSystemDrawingBitmap<TPixel>(TestImageProvider<TPixel> provider) public void To32bppArgbSystemDrawingBitmap<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ using System.Drawing.Bitmap sdBitmap = SystemDrawingBridge.To32bppArgbSystemDrawingBitmap(image);
using (System.Drawing.Bitmap sdBitmap = SystemDrawingBridge.To32bppArgbSystemDrawingBitmap(image)) string fileName = provider.Utility.GetTestOutputFileName("png");
{ sdBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
string fileName = provider.Utility.GetTestOutputFileName("png");
sdBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
} }
[Theory] [Theory]
@ -43,28 +39,22 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests
{ {
string path = TestFile.GetInputFileFullPath(TestImages.Png.Splash); string path = TestFile.GetInputFileFullPath(TestImages.Png.Splash);
using (var sdBitmap = new System.Drawing.Bitmap(path)) using var sdBitmap = new System.Drawing.Bitmap(path);
{ using Image<TPixel> image = SystemDrawingBridge.From32bppArgbSystemDrawingBitmap<TPixel>(sdBitmap);
using (Image<TPixel> image = SystemDrawingBridge.From32bppArgbSystemDrawingBitmap<TPixel>(sdBitmap)) image.DebugSave(dummyProvider);
{
image.DebugSave(dummyProvider);
}
}
} }
private static string SavePng<TPixel>(TestImageProvider<TPixel> provider, PngColorType pngColorType) private static string SavePng<TPixel>(TestImageProvider<TPixel> provider, PngColorType pngColorType)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> sourceImage = provider.GetImage()) using Image<TPixel> sourceImage = provider.GetImage();
if (pngColorType != PngColorType.RgbWithAlpha)
{ {
if (pngColorType != PngColorType.RgbWithAlpha) sourceImage.Mutate(c => c.MakeOpaque());
{
sourceImage.Mutate(c => c.MakeOpaque());
}
var encoder = new PngEncoder { ColorType = pngColorType };
return provider.Utility.SaveTestOutputFile(sourceImage, "png", encoder);
} }
var encoder = new PngEncoder { ColorType = pngColorType };
return provider.Utility.SaveTestOutputFile(sourceImage, "png", encoder);
} }
[Theory] [Theory]
@ -79,15 +69,11 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests
string path = SavePng(provider, PngColorType.RgbWithAlpha); string path = SavePng(provider, PngColorType.RgbWithAlpha);
using (var sdBitmap = new System.Drawing.Bitmap(path)) using var sdBitmap = new System.Drawing.Bitmap(path);
{ using Image<TPixel> original = provider.GetImage();
using (Image<TPixel> original = provider.GetImage()) using Image<TPixel> resaved = SystemDrawingBridge.From32bppArgbSystemDrawingBitmap<TPixel>(sdBitmap);
using (Image<TPixel> resaved = SystemDrawingBridge.From32bppArgbSystemDrawingBitmap<TPixel>(sdBitmap)) ImageComparer comparer = ImageComparer.Exact;
{ comparer.VerifySimilarity(original, resaved);
ImageComparer comparer = ImageComparer.Exact;
comparer.VerifySimilarity(original, resaved);
}
}
} }
[Theory] [Theory]
@ -97,17 +83,11 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests
{ {
string path = SavePng(provider, PngColorType.Rgb); string path = SavePng(provider, PngColorType.Rgb);
using (Image<TPixel> original = provider.GetImage()) using Image<TPixel> original = provider.GetImage();
{ using var sdBitmap = new System.Drawing.Bitmap(path);
using (var sdBitmap = new System.Drawing.Bitmap(path)) using Image<TPixel> resaved = SystemDrawingBridge.From24bppRgbSystemDrawingBitmap<TPixel>(sdBitmap);
{ ImageComparer comparer = ImageComparer.Exact;
using (Image<TPixel> resaved = SystemDrawingBridge.From24bppRgbSystemDrawingBitmap<TPixel>(sdBitmap)) comparer.VerifySimilarity(original, resaved);
{
ImageComparer comparer = ImageComparer.Exact;
comparer.VerifySimilarity(original, resaved);
}
}
}
} }
[Theory] [Theory]
@ -116,10 +96,8 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
string path = TestFile.GetInputFileFullPath(TestImages.Png.Splash); string path = TestFile.GetInputFileFullPath(TestImages.Png.Splash);
using (var image = Image.Load<TPixel>(path, SystemDrawingReferenceDecoder.Instance)) using var image = Image.Load<TPixel>(path, SystemDrawingReferenceDecoder.Instance);
{ image.DebugSave(dummyProvider);
image.DebugSave(dummyProvider);
}
} }
[Theory] [Theory]
@ -127,10 +105,8 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests
public void SaveWithReferenceEncoder<TPixel>(TestImageProvider<TPixel> provider) public void SaveWithReferenceEncoder<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ provider.Utility.SaveTestOutputFile(image, "png", SystemDrawingReferenceEncoder.Png);
provider.Utility.SaveTestOutputFile(image, "png", SystemDrawingReferenceEncoder.Png);
}
} }
} }
} }

60
tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs

@ -20,10 +20,8 @@ namespace SixLabors.ImageSharp.Tests
TestImageProvider<TPixel> provider) TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.CompareToReferenceOutput(provider);
image.CompareToReferenceOutput(provider);
}
} }
[Theory] [Theory]
@ -32,10 +30,8 @@ namespace SixLabors.ImageSharp.Tests
TestImageProvider<TPixel> provider) TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ Assert.ThrowsAny<Exception>(() => image.CompareToReferenceOutput(provider));
Assert.ThrowsAny<Exception>(() => image.CompareToReferenceOutput(provider));
}
} }
[Theory] [Theory]
@ -44,11 +40,9 @@ namespace SixLabors.ImageSharp.Tests
TestImageProvider<TPixel> provider) TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ image.DebugSave(provider, appendPixelTypeToFileName: false);
image.DebugSave(provider, appendPixelTypeToFileName: false); image.CompareToReferenceOutput(provider, appendPixelTypeToFileName: false);
image.CompareToReferenceOutput(provider, appendPixelTypeToFileName: false);
}
} }
[Theory] [Theory]
@ -56,10 +50,8 @@ namespace SixLabors.ImageSharp.Tests
public void CompareToReferenceOutput_WhenReferenceFileMissing_Throws<TPixel>(TestImageProvider<TPixel> provider) public void CompareToReferenceOutput_WhenReferenceFileMissing_Throws<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ Assert.ThrowsAny<Exception>(() => image.CompareToReferenceOutput(provider));
Assert.ThrowsAny<Exception>(() => image.CompareToReferenceOutput(provider));
}
} }
[Theory] [Theory]
@ -67,13 +59,9 @@ namespace SixLabors.ImageSharp.Tests
public void CompareToOriginal_WhenSimilar<TPixel>(TestImageProvider<TPixel> provider) public void CompareToOriginal_WhenSimilar<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ using Image<TPixel> clone = image.Clone();
using (Image<TPixel> clone = image.Clone()) clone.CompareToOriginal(provider, ImageComparer.Exact);
{
clone.CompareToOriginal(provider, ImageComparer.Exact);
}
}
} }
[Theory] [Theory]
@ -81,15 +69,13 @@ namespace SixLabors.ImageSharp.Tests
public void CompareToOriginal_WhenDifferent_Throws<TPixel>(TestImageProvider<TPixel> provider) public void CompareToOriginal_WhenDifferent_Throws<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ ImagingTestCaseUtility.ModifyPixel(image, 3, 1, 1);
ImagingTestCaseUtility.ModifyPixel(image, 3, 1, 1);
Assert.ThrowsAny<ImageDifferenceIsOverThresholdException>(() => Assert.ThrowsAny<ImageDifferenceIsOverThresholdException>(() =>
{ {
image.CompareToOriginal(provider, ImageComparer.Exact); image.CompareToOriginal(provider, ImageComparer.Exact);
}); });
}
} }
[Theory] [Theory]
@ -97,13 +83,11 @@ namespace SixLabors.ImageSharp.Tests
public void CompareToOriginal_WhenInputIsNotFromFile_Throws<TPixel>(TestImageProvider<TPixel> provider) public void CompareToOriginal_WhenInputIsNotFromFile_Throws<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
Assert.ThrowsAny<Exception>(() =>
{ {
Assert.ThrowsAny<Exception>(() => image.CompareToOriginal(provider, Mock.Of<ImageComparer>());
{ });
image.CompareToOriginal(provider, Mock.Of<ImageComparer>());
});
}
} }
} }
} }

50
tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

@ -179,16 +179,14 @@ namespace SixLabors.ImageSharp.Tests
public void SaveTestOutputFileMultiFrame<TPixel>(TestImageProvider<TPixel> provider) public void SaveTestOutputFileMultiFrame<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ string[] files = provider.Utility.SaveTestOutputFileMultiFrame(image);
string[] files = provider.Utility.SaveTestOutputFileMultiFrame(image);
Assert.True(files.Length > 2); Assert.True(files.Length > 2);
foreach (string path in files) foreach (string path in files)
{ {
this.Output.WriteLine(path); this.Output.WriteLine(path);
Assert.True(File.Exists(path)); Assert.True(File.Exists(path));
}
} }
} }
@ -199,10 +197,8 @@ namespace SixLabors.ImageSharp.Tests
public void Use_WithBasicTestPatternImages<TPixel>(TestImageProvider<TPixel> provider) public void Use_WithBasicTestPatternImages<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> img = provider.GetImage()) using Image<TPixel> img = provider.GetImage();
{ img.DebugSave(provider);
img.DebugSave(provider);
}
} }
[Theory] [Theory]
@ -238,15 +234,13 @@ namespace SixLabors.ImageSharp.Tests
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Assert.NotNull(provider.Utility.SourceFileOrDescription); Assert.NotNull(provider.Utility.SourceFileOrDescription);
using (Image<TPixel> img = provider.GetImage()) using Image<TPixel> img = provider.GetImage();
{ Assert.True(img.Width * img.Height > 0);
Assert.True(img.Width * img.Height > 0);
Assert.Equal(123, yo); Assert.Equal(123, yo);
string fn = provider.Utility.GetTestOutputFileName("jpg"); string fn = provider.Utility.GetTestOutputFileName("jpg");
this.Output.WriteLine(fn); this.Output.WriteLine(fn);
}
} }
[Theory] [Theory]
@ -263,10 +257,8 @@ namespace SixLabors.ImageSharp.Tests
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Assert.NotNull(provider.Utility.SourceFileOrDescription); Assert.NotNull(provider.Utility.SourceFileOrDescription);
using (Image<TPixel> image = provider.GetImage()) using Image<TPixel> image = provider.GetImage();
{ provider.Utility.SaveTestOutputFile(image, "png");
provider.Utility.SaveTestOutputFile(image, "png");
}
} }
[Theory] [Theory]
@ -334,12 +326,10 @@ namespace SixLabors.ImageSharp.Tests
var customConfiguration = Configuration.CreateDefaultInstance(); var customConfiguration = Configuration.CreateDefaultInstance();
provider.Configuration = customConfiguration; provider.Configuration = customConfiguration;
using (Image<TPixel> image2 = provider.GetImage()) using Image<TPixel> image2 = provider.GetImage();
using (Image<TPixel> image3 = provider.GetImage()) using Image<TPixel> image3 = provider.GetImage();
{ Assert.Same(customConfiguration, image2.GetConfiguration());
Assert.Same(customConfiguration, image2.GetConfiguration()); Assert.Same(customConfiguration, image3.GetConfiguration());
Assert.Same(customConfiguration, image3.GetConfiguration());
}
} }
} }

Loading…
Cancel
Save