Simplify FastBitmap.

This commit is contained in:
UnknownShadow200 2015-04-10 08:42:52 +10:00
parent ee0542e19b
commit e89dfb9f56

View File

@ -4,15 +4,8 @@ using System.Drawing.Imaging;
namespace ClassicalSharp { namespace ClassicalSharp {
/// <summary> Represents a wrapper around a bitmap. Provides
/// a fast implementation for getting and setting pixels in that bitmap. </summary>
/// <remarks> Only supports 32 bit RGBA pixel format. </remarks>
public unsafe class FastBitmap : IDisposable { public unsafe class FastBitmap : IDisposable {
/// <summary> Constructs a new FastBitmap wrapped around the specified Bitmap. </summary>
/// <param name="bmp"> Bitmap which is wrapped. </param>
/// <param name="lockBits"> Whether to immediately lock the bits of the bitmap,
/// so that get and set pixel operations can be performed immediately after construction.</param>
public FastBitmap( Bitmap bmp, bool lockBits ) { public FastBitmap( Bitmap bmp, bool lockBits ) {
Bitmap = bmp; Bitmap = bmp;
if( lockBits ) { if( lockBits ) {
@ -22,41 +15,19 @@ namespace ClassicalSharp {
public Bitmap Bitmap; public Bitmap Bitmap;
BitmapData data; BitmapData data;
byte* scan0; byte* scan0Byte;
int stride;
public bool IsLocked { public bool IsLocked {
get { return data != null; } get { return data != null; }
} }
/// <summary> Gets the address of the first pixel in this bitmap. </summary> public IntPtr Scan0;
public IntPtr Scan0 { public int Stride;
get { return data.Scan0; } public int Width, Height;
}
/// <summary> Gets the stride width/scan width of the bitmap.
/// (i.e. the actual size of each scanline, including padding) </summary>
public int Stride {
get { return data.Stride; }
}
/// <summary> Gets the width of this bitmap, in pixels. </summary>
public int Width {
get { return data.Width; }
}
/// <summary> Gets the height of this bitmap, in pixels. </summary>
public int Height {
get { return data.Height; }
}
/// <summary> Locks the wrapped bitmap into system memory,
/// so that fast get/set pixel operations can be performed. </summary>
public void LockBits() { public void LockBits() {
if( Bitmap == null ) throw new InvalidOperationException( "Bmp is null." ); if( Bitmap == null ) throw new InvalidOperationException( "Underlying bitmap is null." );
if( data != null ) { if( data != null ) return;
Bitmap.UnlockBits( data );
}
PixelFormat format = Bitmap.PixelFormat; PixelFormat format = Bitmap.PixelFormat;
if( !( format == PixelFormat.Format32bppArgb || format == PixelFormat.Format32bppRgb ) ) { if( !( format == PixelFormat.Format32bppArgb || format == PixelFormat.Format32bppRgb ) ) {
@ -65,8 +36,11 @@ namespace ClassicalSharp {
Rectangle rec = new Rectangle( 0, 0, Bitmap.Width, Bitmap.Height ); Rectangle rec = new Rectangle( 0, 0, Bitmap.Width, Bitmap.Height );
data = Bitmap.LockBits( rec, ImageLockMode.ReadWrite, format ); data = Bitmap.LockBits( rec, ImageLockMode.ReadWrite, format );
scan0 = (byte*)data.Scan0; scan0Byte = (byte*)data.Scan0;
stride = data.Stride; Scan0 = data.Scan0;
Stride = data.Stride;
Width = data.Width;
Height = data.Height;
} }
public void Dispose() { public void Dispose() {
@ -77,23 +51,24 @@ namespace ClassicalSharp {
if( data != null ) { if( data != null ) {
Bitmap.UnlockBits( data ); Bitmap.UnlockBits( data );
data = null; data = null;
scan0 = (byte*)IntPtr.Zero; scan0Byte = (byte*)IntPtr.Zero;
stride = 0; Scan0 = IntPtr.Zero;
Width = Height = Stride = 0;
} }
} }
public int GetPixel( int x, int y ) { public int GetPixel( int x, int y ) {
// TODO: Does this work with big-endian systems? // TODO: Does this work with big-endian systems?
int* row = (int*)( scan0 + ( y * stride ) ); int* row = (int*)( scan0Byte + ( y * Stride ) );
return row[x]; // b g r a return row[x]; // b g r a
} }
public int* GetRowPtr( int y ) { public int* GetRowPtr( int y ) {
return (int*)( scan0 + ( y * stride ) ); return (int*)( scan0Byte + ( y * Stride ) );
} }
public void SetPixel( int x, int y, int col ) { public void SetPixel( int x, int y, int col ) {
int* row = (int*)( scan0 + ( y * stride ) ); int* row = (int*)( scan0Byte + ( y * Stride ) );
row[x] = col; row[x] = col;
} }
} }