Browse Source

made WrapMemory() public + test case demonstrating using Image.WrapMemory() to draw over a System.Drawing.Bitmap instance

af/merge-core
Anton Firszov 8 years ago
parent
commit
7c53bc1785
  1. 4
      src/ImageSharp/Image.WrapMemory.cs
  2. 1
      src/ImageSharp/PixelFormats/Bgra32.cs
  3. 74
      tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs

4
src/ImageSharp/Image.WrapMemory.cs

@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp
/// <param name="height">The height of the memory image</param>
/// <param name="metaData">The <see cref="ImageMetaData"/></param>
/// <returns>An <see cref="Image{TPixel}"/> instance</returns>
internal static Image<TPixel> WrapMemory<TPixel>(
public static Image<TPixel> WrapMemory<TPixel>(
Configuration config,
Memory<TPixel> pixelMemory,
int width,
@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp
/// <param name="width">The width of the memory image</param>
/// <param name="height">The height of the memory image</param>
/// <returns>An <see cref="Image{TPixel}"/> instance</returns>
internal static Image<TPixel> WrapMemory<TPixel>(
public static Image<TPixel> WrapMemory<TPixel>(
Memory<TPixel> pixelMemory,
int width,
int height)

1
src/ImageSharp/PixelFormats/Bgra32.cs

@ -11,6 +11,7 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <summary>
/// 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
/// <para>
/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form.
/// </para>

74
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<Bgra32>
{
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<Bgra32> GetSpan()
{
void* ptr = (void*) this.bmpData.Scan0;
return new Span<Bgra32>(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<Bgra32> memory = memoryManager.Memory;
Bgra32 bg = NamedColors<Bgra32>.Red;
Bgra32 fg = NamedColors<Bgra32>.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);
}
}
}
}
}
Loading…
Cancel
Save