#if NET452
namespace Unosquare.Swan.Formatters
{
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
///
/// Represents a buffer of bytes containing pixels in BGRA byte order
/// loaded from an image that is passed on to the constructor.
/// Data contains all the raw bytes (without scanline left-over bytes)
/// where they can be quickly changed and then a new bitmap
/// can be created from the byte data.
///
public class BitmapBuffer
{
///
/// A constant representing the number of
/// bytes per pixel in the pixel data. This is
/// always 4 but it is kept here for readability.
///
public const int BytesPerPixel = 4;
///
/// The blue byte offset within a pixel offset. This is 0.
///
public const int BOffset = 0;
///
/// The green byte offset within a pixel offset. This is 1.
///
public const int GOffset = 1;
///
/// The red byte offset within a pixel offset. This is 2.
///
public const int ROffset = 2;
///
/// The alpha byte offset within a pixel offset. This is 3.
///
public const int AOffset = 3;
///
/// Initializes a new instance of the class.
/// Data will not contain left-over stride bytes
///
/// The source image.
public BitmapBuffer(Image sourceImage)
{
// Acquire or create the source bitmap in a manageable format
var disposeSourceBitmap = false;
if (!(sourceImage is Bitmap sourceBitmap) || sourceBitmap.PixelFormat != PixelFormat)
{
sourceBitmap = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat);
using (var g = Graphics.FromImage(sourceBitmap))
{
g.DrawImage(sourceImage, 0, 0);
}
// We created this bitmap. Make sure we clear it from memory
disposeSourceBitmap = true;
}
// Lock the bits
var sourceDataLocker = sourceBitmap.LockBits(
new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height),
ImageLockMode.ReadOnly,
sourceBitmap.PixelFormat);
// Set basic properties
ImageWidth = sourceBitmap.Width;
ImageHeight = sourceBitmap.Height;
LineStride = sourceDataLocker.Stride;
// State variables
LineLength = sourceBitmap.Width * BytesPerPixel; // may or may not be equal to the Stride
Data = new byte[LineLength * sourceBitmap.Height];
// copy line by line in order to ignore the useless left-over stride
Parallel.For(0, sourceBitmap.Height, y =>
{
var sourceAddress = sourceDataLocker.Scan0 + (sourceDataLocker.Stride * y);
var targetAddress = y * LineLength;
Marshal.Copy(sourceAddress, Data, targetAddress, LineLength);
});
// finally unlock the bitmap
sourceBitmap.UnlockBits(sourceDataLocker);
// dispose the source bitmap if we had to create it
if (disposeSourceBitmap)
{
sourceBitmap.Dispose();
}
}
///
/// Contains all the bytes of the pixel data
/// Each horizontal scanline is represented by LineLength
/// rather than by LinceStride. The left-over stride bytes
/// are removed.
///
public byte[] Data { get; }
///
/// Gets the width of the image.
///
public int ImageWidth { get; }
///
/// Gets the height of the image.
///
public int ImageHeight { get; }
///
/// Gets the pixel format. This will always be Format32bppArgb.
///
public PixelFormat PixelFormat { get; } = PixelFormat.Format32bppArgb;
///
/// Gets the length in bytes of a line of pixel data.
/// Basically the same as Line Length except Stride might be a little larger as
/// some bitmaps might be DWORD-algned.
///
public int LineStride { get; }
///
/// Gets the length in bytes of a line of pixel data.
/// Basically the same as Stride except Stride might be a little larger as
/// some bitmaps might be DWORD-algned.
///
public int LineLength { get; }
///
/// Gets the index of the first byte in the BGRA pixel data for the given image coordinates.
///
/// The x.
/// The y.
/// Index of the first byte in the BGRA pixel.
///
/// x
/// or
/// y.
///
public int GetPixelOffset(int x, int y)
{
if (x < 0 || x > ImageWidth) throw new ArgumentOutOfRangeException(nameof(x));
if (y < 0 || y > ImageHeight) throw new ArgumentOutOfRangeException(nameof(y));
return (y * LineLength) + (x * BytesPerPixel);
}
///
/// Converts the pixel data bytes held in the buffer
/// to a 32-bit RGBA bitmap.
///
/// Pixel data for a graphics image and its attribute.
public Bitmap ToBitmap()
{
var bitmap = new Bitmap(ImageWidth, ImageHeight, PixelFormat);
var bitLocker = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
Parallel.For(0, bitmap.Height, y =>
{
var sourceOffset = GetPixelOffset(0, y);
var targetOffset = bitLocker.Scan0 + (y * bitLocker.Stride);
Marshal.Copy(Data, sourceOffset, targetOffset, bitLocker.Width);
});
bitmap.UnlockBits(bitLocker);
return bitmap;
}
}
}
#endif