Browse Source

introduced CoreCompat.SystemDrawing as reference utility for tests, implemented ToSystemDrawingBitmap()

af/merge-core
Anton Firszov 9 years ago
parent
commit
30912e6974
  1. 10
      tests/ImageSharp.Tests/ImageComparer.cs
  2. 1
      tests/ImageSharp.Tests/ImageSharp.Tests.csproj
  3. 65
      tests/ImageSharp.Tests/TestUtilities/Integration/IntegrationTestUtils.cs
  4. 21
      tests/ImageSharp.Tests/TestUtilities/Integration/ReferenceEncoder.cs
  5. 33
      tests/ImageSharp.Tests/TestUtilities/Tests/IntegrationTestUtilsTests.cs

10
tests/ImageSharp.Tests/ImageComparer.cs

@ -72,9 +72,13 @@ namespace ImageSharp.Tests
/// This is a sampling factor we sample a grid of average pixels <paramref name="scalingFactor"/> width by <paramref name="scalingFactor"/> high
/// The default undefined value is <see cref="DefaultScalingFactor"/>
/// </param>
public static void CheckSimilarity<TPixelA, TPixelB>(Image<TPixelA> expected, Image<TPixelB> actual, float imageTheshold = DefaultImageThreshold, byte segmentThreshold = DefaultSegmentThreshold, int scalingFactor = DefaultScalingFactor)
where TPixelA : struct, IPixel<TPixelA>
where TPixelB : struct, IPixel<TPixelB>
public static void CheckSimilarity<TPixelA, TPixelB>(
Image<TPixelA> expected,
Image<TPixelB> actual,
float imageTheshold = DefaultImageThreshold,
byte segmentThreshold = DefaultSegmentThreshold,
int scalingFactor = DefaultScalingFactor)
where TPixelA : struct, IPixel<TPixelA> where TPixelB : struct, IPixel<TPixelB>
{
float percentage = expected.PercentageDifference(actual, segmentThreshold, scalingFactor);

1
tests/ImageSharp.Tests/ImageSharp.Tests.csproj

@ -10,6 +10,7 @@
<None Include="PixelFormats\PixelOperationsTests.Blender.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CoreCompat.System.Drawing" Version="1.0.0-beta006" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Moq" Version="4.7.1" />
<PackageReference Include="xunit" Version="2.3.0-beta3-build3705" />

65
tests/ImageSharp.Tests/TestUtilities/Integration/IntegrationTestUtils.cs

@ -0,0 +1,65 @@
namespace ImageSharp.Tests.TestUtilities.Integration
{
using System;
using System.Drawing.Imaging;
using ImageSharp.Memory;
using ImageSharp.PixelFormats;
public static class IntegrationTestUtils
{
// TODO: It would be nice to have this method in PixelOperations<T>
private static void ToArgb32<TPixel>(Span<TPixel> source, Span<Argb32> dest)
where TPixel : struct, IPixel<TPixel>
{
int length = source.Length;
Guard.MustBeSizedAtLeast(dest, length, nameof(dest));
using (var rgbaBuffer = new Buffer<Rgba32>(length))
{
PixelOperations<TPixel>.Instance.ToRgba32(source, rgbaBuffer, length);
for (int i = 0; i < length; i++)
{
ref Rgba32 s = ref rgbaBuffer[i];
ref Argb32 d = ref dest[i];
d.PackFromRgba32(s);
}
}
}
internal static unsafe System.Drawing.Bitmap ToSystemDrawingBitmap<TPixel>(Image<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
int w = image.Width;
int h = image.Height;
var resultBitmap = new System.Drawing.Bitmap(w, h, PixelFormat.Format32bppArgb);
var fullRect = new System.Drawing.Rectangle(0, 0, w, h);
BitmapData data = resultBitmap.LockBits(fullRect, ImageLockMode.ReadWrite, resultBitmap.PixelFormat);
byte* destPtrBase = (byte*)data.Scan0;
long destRowByteCount= data.Stride;
long sourceRowByteCount = w * sizeof(Argb32);
using (var workBuffer = new Buffer<Argb32>(w))
{
var sourcePtr = (Argb32*) workBuffer.Pin();
for (int y = 0; y < h; y++)
{
Span<TPixel> row = image.GetRowSpan(y);
ToArgb32(row, workBuffer);
byte* destPtr = destPtrBase + data.Stride * y;
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
}
}
resultBitmap.UnlockBits(data);
return resultBitmap;
}
}
}

21
tests/ImageSharp.Tests/TestUtilities/Integration/ReferenceEncoder.cs

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ImageSharp.Tests.TestUtilities.Integration
{
using System.IO;
using ImageSharp.Formats;
using ImageSharp.PixelFormats;
public class ReferenceEncoder : IImageEncoder
{
public void Encode<TPixel>(Image<TPixel> image, Stream stream, IEncoderOptions options)
where TPixel : struct, IPixel<TPixel>
{
System.Drawing.Bitmap sdBitmap = IntegrationTestUtils.ToSystemDrawingBitmap(image);
throw new NotImplementedException();
}
}
}

33
tests/ImageSharp.Tests/TestUtilities/Tests/IntegrationTestUtilsTests.cs

@ -0,0 +1,33 @@
namespace ImageSharp.Tests
{
using ImageSharp.PixelFormats;
using ImageSharp.Tests.TestUtilities.Integration;
using Xunit;
using Xunit.Abstractions;
public class IntegrationTestUtilsTests
{
private ITestOutputHelper Output { get; }
public IntegrationTestUtilsTests(ITestOutputHelper output)
{
this.Output = output;
}
[Theory]
[WithTestPatternImages(20, 20, PixelTypes.Rgba32 | PixelTypes.Bgra32)]
public void ToSystemDrawingBitmap<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
using (System.Drawing.Bitmap sdBitmap = IntegrationTestUtils.ToSystemDrawingBitmap(image))
{
string fileName = provider.Utility.GetTestOutputFileName("png");
sdBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
}
}
Loading…
Cancel
Save