|
|
|
@ -2,6 +2,7 @@ |
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Drawing; |
|
|
|
using System.Drawing.Imaging; |
|
|
|
|
|
|
|
using SixLabors.ImageSharp.Advanced; |
|
|
|
@ -10,26 +11,34 @@ using SixLabors.ImageSharp.PixelFormats; |
|
|
|
|
|
|
|
namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Provides methods to convert to/from System.Drawing bitmaps.
|
|
|
|
/// </summary>
|
|
|
|
public static class SystemDrawingBridge |
|
|
|
{ |
|
|
|
internal static unsafe Image<TPixel> FromFromArgb32SystemDrawingBitmap<TPixel>(System.Drawing.Bitmap bmp) |
|
|
|
/// <summary>
|
|
|
|
/// Returns an image from the given System.Drawing bitmap.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="bmp">The input bitmap.</param>
|
|
|
|
/// <exception cref="ArgumentException">Thrown if the image pixel format is not of type <see cref="PixelFormat.Format32bppArgb"/></exception>
|
|
|
|
internal static unsafe Image<TPixel> From32bppArgbSystemDrawingBitmap<TPixel>(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); |
|
|
|
var fullRect = new Rectangle(0, 0, w, h); |
|
|
|
|
|
|
|
if (bmp.PixelFormat != PixelFormat.Format32bppArgb) |
|
|
|
{ |
|
|
|
throw new ArgumentException($"FromFromArgb32SystemDrawingBitmap(): pixel format should be Argb32!", nameof(bmp)); |
|
|
|
throw new ArgumentException($"{nameof(From32bppArgbSystemDrawingBitmap)} : pixel format should be {PixelFormat.Format32bppArgb}!", nameof(bmp)); |
|
|
|
} |
|
|
|
|
|
|
|
BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat); |
|
|
|
byte* sourcePtrBase = (byte*)data.Scan0; |
|
|
|
|
|
|
|
long sourceRowByteCount = data.Stride; |
|
|
|
long destRowByteCount = w * sizeof(Argb32); |
|
|
|
long destRowByteCount = w * sizeof(Bgra32); |
|
|
|
|
|
|
|
var image = new Image<TPixel>(w, h); |
|
|
|
|
|
|
|
@ -41,7 +50,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs |
|
|
|
{ |
|
|
|
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y); |
|
|
|
|
|
|
|
byte* sourcePtr = sourcePtrBase + data.Stride * y; |
|
|
|
byte* sourcePtr = sourcePtrBase + (data.Stride * y); |
|
|
|
|
|
|
|
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount); |
|
|
|
PixelOperations<TPixel>.Instance.PackFromBgra32(workBuffer.GetSpan(), row, row.Length); |
|
|
|
@ -53,19 +62,21 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// TODO: Doesn not work yet!
|
|
|
|
/// Returns an image from the given System.Drawing bitmap.
|
|
|
|
/// </summary>
|
|
|
|
internal static unsafe Image<TPixel> FromFromRgb24SystemDrawingBitmap<TPixel>(System.Drawing.Bitmap bmp) |
|
|
|
/// <param name="bmp">The input bitmap.</param>
|
|
|
|
/// <exception cref="ArgumentException">Thrown if the image pixel format is not of type <see cref="PixelFormat.Format24bppRgb"/></exception>
|
|
|
|
internal static unsafe Image<TPixel> From24bppRgbSystemDrawingBitmap<TPixel>(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); |
|
|
|
var fullRect = new Rectangle(0, 0, w, h); |
|
|
|
|
|
|
|
if (bmp.PixelFormat != PixelFormat.Format24bppRgb) |
|
|
|
{ |
|
|
|
throw new ArgumentException($"FromFromArgb32SystemDrawingBitmap(): pixel format should be Rgb24!", nameof(bmp)); |
|
|
|
throw new ArgumentException($"{nameof(From24bppRgbSystemDrawingBitmap)}: pixel format should be {PixelFormat.Format24bppRgb}!", nameof(bmp)); |
|
|
|
} |
|
|
|
|
|
|
|
BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat); |
|
|
|
@ -84,12 +95,10 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs |
|
|
|
{ |
|
|
|
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y); |
|
|
|
|
|
|
|
byte* sourcePtr = sourcePtrBase + data.Stride * y; |
|
|
|
byte* sourcePtr = sourcePtrBase + (data.Stride * y); |
|
|
|
|
|
|
|
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount); |
|
|
|
PixelOperations<TPixel>.Instance.PackFromBgr24(workBuffer.GetSpan(), row, row.Length); |
|
|
|
|
|
|
|
// FromRgb24(workBuffer.GetSpan(), row);
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -97,14 +106,14 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs |
|
|
|
return image; |
|
|
|
} |
|
|
|
|
|
|
|
internal static unsafe System.Drawing.Bitmap ToSystemDrawingBitmap<TPixel>(Image<TPixel> image) |
|
|
|
internal static unsafe Bitmap To32bppArgbSystemDrawingBitmap<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); |
|
|
|
var resultBitmap = new Bitmap(w, h, PixelFormat.Format32bppArgb); |
|
|
|
var fullRect = new Rectangle(0, 0, w, h); |
|
|
|
BitmapData data = resultBitmap.LockBits(fullRect, ImageLockMode.ReadWrite, resultBitmap.PixelFormat); |
|
|
|
byte* destPtrBase = (byte*)data.Scan0; |
|
|
|
|
|
|
|
@ -120,7 +129,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs |
|
|
|
{ |
|
|
|
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y); |
|
|
|
PixelOperations<TPixel>.Instance.ToBgra32(row, workBuffer.GetSpan(), row.Length); |
|
|
|
byte* destPtr = destPtrBase + data.Stride * y; |
|
|
|
byte* destPtr = destPtrBase + (data.Stride * y); |
|
|
|
|
|
|
|
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount); |
|
|
|
} |
|
|
|
|