diff --git a/tests/ImageSharp.Tests/TestUtilities/Integration/ReferenceEncoder.cs b/tests/ImageSharp.Sandbox46/Tests/TestUtilities/Integration/ReferencePngEncoder.cs similarity index 52% rename from tests/ImageSharp.Tests/TestUtilities/Integration/ReferenceEncoder.cs rename to tests/ImageSharp.Sandbox46/Tests/TestUtilities/Integration/ReferencePngEncoder.cs index 60c21971b8..806cb05be7 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Integration/ReferenceEncoder.cs +++ b/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(Image image, Stream stream, IEncoderOptions options) where TPixel : struct, IPixel { - 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); + } } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/Integration/IntegrationTestUtils.cs b/tests/ImageSharp.Tests/TestUtilities/Integration/IntegrationTestUtils.cs index f4a125733e..53986e64a6 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Integration/IntegrationTestUtils.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Integration/IntegrationTestUtils.cs @@ -29,6 +29,60 @@ } } + private static void FromArgb32(Span source, Span dest) + where TPixel : struct, IPixel + { + int length = source.Length; + Guard.MustBeSizedAtLeast(dest, length, nameof(dest)); + + using (var rgbaBuffer = new Buffer(length)) + { + PixelOperations.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 FromSystemDrawingBitmap(System.Drawing.Bitmap bmp) + where TPixel : struct, IPixel + { + 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(w, h); + + using (var workBuffer = new Buffer(w)) + { + var destPtr = (Argb32*)workBuffer.Pin(); + for (int y = 0; y < h; y++) + { + Span 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(Image image) where TPixel : struct, IPixel { diff --git a/tests/ImageSharp.Tests/TestUtilities/Integration/ReferencePngDecoder.cs b/tests/ImageSharp.Tests/TestUtilities/Integration/ReferencePngDecoder.cs new file mode 100644 index 0000000000..f2ac69fd4d --- /dev/null +++ b/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 Decode(Configuration configuration, Stream stream, IDecoderOptions options) + where TPixel : struct, IPixel + { + 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(sdBitmap); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Integration/ReferencePngEncoder.cs b/tests/ImageSharp.Tests/TestUtilities/Integration/ReferencePngEncoder.cs new file mode 100644 index 0000000000..b206197db6 --- /dev/null +++ b/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(Image image, Stream stream, IEncoderOptions options) + where TPixel : struct, IPixel + { + using (System.Drawing.Bitmap sdBitmap = IntegrationTestUtils.ToSystemDrawingBitmap(image)) + { + sdBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); + } + } + } +} diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/IntegrationTestUtilsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/IntegrationTestUtilsTests.cs index abb7f1262d..d1ceef03ba 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/IntegrationTestUtilsTests.cs +++ b/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(TestImageProvider dummyProvider) + where TPixel : struct, IPixel + { + string path = TestFile.GetPath(TestImages.Png.Splash); + + using (var sdBitmap = new System.Drawing.Bitmap(path)) + { + using (Image image = IntegrationTestUtils.FromSystemDrawingBitmap(sdBitmap)) + { + image.DebugSave(dummyProvider); + } + } + } + + [Theory] + [WithBlankImages(1, 1, PixelTypes.Rgba32 | PixelTypes.Bgra32)] + public void OpenWithReferenceDecoder(TestImageProvider dummyProvider) + where TPixel : struct, IPixel + { + string path = TestFile.GetPath(TestImages.Png.Splash); + using (Image image = Image.Load(path, ReferencePngDecoder.Instance)) + { + image.DebugSave(dummyProvider); + } + } + + [Theory] + [WithTestPatternImages(20, 20, PixelTypes.Rgba32 | PixelTypes.Argb32)] + public void SaveWithReferenceEncoder(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + provider.Utility.SaveTestOutputFile(image, "png", ReferencePngEncoder.Instance); + } + } } } \ No newline at end of file