From 7c53bc17850be15b98a77daa0dddb7b00a029213 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 22 Jul 2018 19:54:37 +0200 Subject: [PATCH] made WrapMemory() public + test case demonstrating using Image.WrapMemory() to draw over a System.Drawing.Bitmap instance --- src/ImageSharp/Image.WrapMemory.cs | 4 +- src/ImageSharp/PixelFormats/Bgra32.cs | 1 + .../Image/ImageTests.WrapMemory.cs | 74 +++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Image.WrapMemory.cs b/src/ImageSharp/Image.WrapMemory.cs index 3ccd809f4..7f0c4ae2e 100644 --- a/src/ImageSharp/Image.WrapMemory.cs +++ b/src/ImageSharp/Image.WrapMemory.cs @@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp /// The height of the memory image /// The /// An instance - internal static Image WrapMemory( + public static Image WrapMemory( Configuration config, Memory pixelMemory, int width, @@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp /// The width of the memory image /// The height of the memory image /// An instance - internal static Image WrapMemory( + public static Image WrapMemory( Memory pixelMemory, int width, int height) diff --git a/src/ImageSharp/PixelFormats/Bgra32.cs b/src/ImageSharp/PixelFormats/Bgra32.cs index f951be881..14b2da07c 100644 --- a/src/ImageSharp/PixelFormats/Bgra32.cs +++ b/src/ImageSharp/PixelFormats/Bgra32.cs @@ -11,6 +11,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// /// Packed pixel type containing four 8-bit unsigned normalized values ranging from 0 to 255. /// The color components are stored in blue, green, red, and alpha order (least significant to most significant byte). + /// The format is binary compatible with System.Drawing.Imaging.PixelFormat.Format32bppArgb /// /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. /// diff --git a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs index 252dbc869..57f757176 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs @@ -2,7 +2,14 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Buffers; +using System.Drawing; +using System.Drawing.Imaging; + +using SixLabors.ImageSharp.PixelFormats; using SixLabors.Memory; +using SixLabors.Shapes; +using SixLabors.ImageSharp.Processing; using Xunit; // ReSharper disable InconsistentNaming @@ -12,6 +19,73 @@ namespace SixLabors.ImageSharp.Tests { public class WrapMemory { + class BitmapMemoryManager : MemoryManager + { + private System.Drawing.Bitmap bitmap; + + private BitmapData bmpData; + + private int length; + + public BitmapMemoryManager(Bitmap bitmap) + { + if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) + { + throw new ArgumentException("bitmap.PixelFormat != PixelFormat.Format32bppArgb", nameof(bitmap)); + } + + this.bitmap = bitmap; + var rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height); + this.bmpData = bitmap.LockBits(rectangle, ImageLockMode.ReadWrite, bitmap.PixelFormat); + this.length = bitmap.Width * bitmap.Height; + } + + protected override void Dispose(bool disposing) + { + this.bitmap.UnlockBits(this.bmpData); + } + + public override unsafe Span GetSpan() + { + void* ptr = (void*) this.bmpData.Scan0; + return new Span(ptr, this.length); + } + + public override unsafe MemoryHandle Pin(int elementIndex = 0) + { + void* ptr = (void*)this.bmpData.Scan0; + return new MemoryHandle(ptr); + } + + public override void Unpin() + { + } + } + + [Fact] + public void WrapSystemDrawingBitmap() + { + using (var bmp = new Bitmap(51, 23)) + { + using (var memoryManager = new BitmapMemoryManager(bmp)) + { + Memory memory = memoryManager.Memory; + Bgra32 bg = NamedColors.Red; + Bgra32 fg = NamedColors.Green; + + using (var image = Image.WrapMemory(memory, bmp.Width, bmp.Height)) + { + image.Mutate(c => c.Fill(bg).Fill(fg, new RectangularPolygon(10, 10, 10, 10))); + } + } + + string fn = System.IO.Path.Combine( + TestEnvironment.ActualOutputDirectoryFullPath, + "WrapSystemDrawingBitmap.bmp"); + + bmp.Save(fn, ImageFormat.Bmp); + } + } } } } \ No newline at end of file