Browse Source

ReferencePngDecoder

af/merge-core
Anton Firszov 9 years ago
parent
commit
d96392e0d2
  1. 10
      tests/ImageSharp.Sandbox46/Tests/TestUtilities/Integration/ReferencePngEncoder.cs
  2. 54
      tests/ImageSharp.Tests/TestUtilities/Integration/IntegrationTestUtils.cs
  3. 31
      tests/ImageSharp.Tests/TestUtilities/Integration/ReferencePngDecoder.cs
  4. 24
      tests/ImageSharp.Tests/TestUtilities/Integration/ReferencePngEncoder.cs
  5. 39
      tests/ImageSharp.Tests/TestUtilities/Tests/IntegrationTestUtilsTests.cs

10
tests/ImageSharp.Tests/TestUtilities/Integration/ReferenceEncoder.cs → tests/ImageSharp.Sandbox46/Tests/TestUtilities/Integration/ReferencePngEncoder.cs

@ -9,13 +9,17 @@ namespace ImageSharp.Tests.TestUtilities.Integration
using ImageSharp.Formats;
using ImageSharp.PixelFormats;
public class ReferenceEncoder : IImageEncoder
public class ReferencePngEncoder : IImageEncoder
{
public static ReferencePngEncoder Instance { get; } = new ReferencePngEncoder();
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();
using (System.Drawing.Bitmap sdBitmap = IntegrationTestUtils.ToSystemDrawingBitmap(image))
{
sdBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
}

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

@ -29,6 +29,60 @@
}
}
private static void FromArgb32<TPixel>(Span<Argb32> source, Span<TPixel> dest)
where TPixel : struct, IPixel<TPixel>
{
int length = source.Length;
Guard.MustBeSizedAtLeast(dest, length, nameof(dest));
using (var rgbaBuffer = new Buffer<Rgba32>(length))
{
PixelOperations<Argb32>.Instance.ToRgba32(source, rgbaBuffer, length);
for (int i = 0; i < length; i++)
{
ref Rgba32 s = ref rgbaBuffer[i];
ref TPixel d = ref dest[i];
d.PackFromRgba32(s);
}
}
}
internal static unsafe Image<TPixel> FromSystemDrawingBitmap<TPixel>(System.Drawing.Bitmap bmp)
where TPixel : struct, IPixel<TPixel>
{
int w = bmp.Width;
int h = bmp.Height;
var fullRect = new System.Drawing.Rectangle(0, 0, w, h);
BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
byte* sourcePtrBase = (byte*)data.Scan0;
long sourceRowByteCount = data.Stride;
long destRowByteCount = w * sizeof(Argb32);
var image = new Image<TPixel>(w, h);
using (var workBuffer = new Buffer<Argb32>(w))
{
var destPtr = (Argb32*)workBuffer.Pin();
for (int y = 0; y < h; y++)
{
Span<TPixel> row = image.GetRowSpan(y);
byte* sourcePtr = sourcePtrBase + data.Stride * y;
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
FromArgb32(workBuffer, row);
}
}
return image;
}
internal static unsafe System.Drawing.Bitmap ToSystemDrawingBitmap<TPixel>(Image<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{

31
tests/ImageSharp.Tests/TestUtilities/Integration/ReferencePngDecoder.cs

@ -0,0 +1,31 @@
namespace ImageSharp.Tests.TestUtilities.Integration
{
using System;
using System.IO;
using ImageSharp.Formats;
using ImageSharp.PixelFormats;
public class ReferencePngDecoder : IImageDecoder
{
public static ReferencePngDecoder Instance { get; } = new ReferencePngDecoder();
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream, IDecoderOptions options)
where TPixel : struct, IPixel<TPixel>
{
using (var sdBitmap = new System.Drawing.Bitmap(stream))
{
if (!sdBitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
{
throw new Exception("Reference image should be a Png!");
}
if (sdBitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
{
throw new Exception("Reference image pixel format should be PixelFormat.Format32bppArgb!");
}
return IntegrationTestUtils.FromSystemDrawingBitmap<TPixel>(sdBitmap);
}
}
}
}

24
tests/ImageSharp.Tests/TestUtilities/Integration/ReferencePngEncoder.cs

@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Text;
namespace ImageSharp.Tests.TestUtilities.Integration
{
using System.IO;
using ImageSharp.Formats;
using ImageSharp.PixelFormats;
public class ReferencePngEncoder : IImageEncoder
{
public static ReferencePngEncoder Instance { get; } = new ReferencePngEncoder();
public void Encode<TPixel>(Image<TPixel> image, Stream stream, IEncoderOptions options)
where TPixel : struct, IPixel<TPixel>
{
using (System.Drawing.Bitmap sdBitmap = IntegrationTestUtils.ToSystemDrawingBitmap(image))
{
sdBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
}

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

@ -29,5 +29,44 @@ namespace ImageSharp.Tests
}
}
}
[Theory]
[WithBlankImages(1, 1, PixelTypes.Rgba32 | PixelTypes.Bgra32)]
public void FromSystemDrawingBitmap<TPixel>(TestImageProvider<TPixel> dummyProvider)
where TPixel : struct, IPixel<TPixel>
{
string path = TestFile.GetPath(TestImages.Png.Splash);
using (var sdBitmap = new System.Drawing.Bitmap(path))
{
using (Image<TPixel> image = IntegrationTestUtils.FromSystemDrawingBitmap<TPixel>(sdBitmap))
{
image.DebugSave(dummyProvider);
}
}
}
[Theory]
[WithBlankImages(1, 1, PixelTypes.Rgba32 | PixelTypes.Bgra32)]
public void OpenWithReferenceDecoder<TPixel>(TestImageProvider<TPixel> dummyProvider)
where TPixel : struct, IPixel<TPixel>
{
string path = TestFile.GetPath(TestImages.Png.Splash);
using (Image<TPixel> image = Image.Load<TPixel>(path, ReferencePngDecoder.Instance))
{
image.DebugSave(dummyProvider);
}
}
[Theory]
[WithTestPatternImages(20, 20, PixelTypes.Rgba32 | PixelTypes.Argb32)]
public void SaveWithReferenceEncoder<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
provider.Utility.SaveTestOutputFile(image, "png", ReferencePngEncoder.Instance);
}
}
}
}
Loading…
Cancel
Save