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 {
/// <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 {
/// <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 ) {
Bitmap = bmp;
if( lockBits ) {
@ -22,41 +15,19 @@ namespace ClassicalSharp {
public Bitmap Bitmap;
BitmapData data;
byte* scan0;
int stride;
byte* scan0Byte;
public bool IsLocked {
get { return data != null; }
}
/// <summary> Gets the address of the first pixel in this bitmap. </summary>
public IntPtr Scan0 {
get { return data.Scan0; }
}
public IntPtr Scan0;
public int Stride;
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() {
if( Bitmap == null ) throw new InvalidOperationException( "Bmp is null." );
if( data != null ) {
Bitmap.UnlockBits( data );
}
if( Bitmap == null ) throw new InvalidOperationException( "Underlying bitmap is null." );
if( data != null ) return;
PixelFormat format = Bitmap.PixelFormat;
if( !( format == PixelFormat.Format32bppArgb || format == PixelFormat.Format32bppRgb ) ) {
@ -65,8 +36,11 @@ namespace ClassicalSharp {
Rectangle rec = new Rectangle( 0, 0, Bitmap.Width, Bitmap.Height );
data = Bitmap.LockBits( rec, ImageLockMode.ReadWrite, format );
scan0 = (byte*)data.Scan0;
stride = data.Stride;
scan0Byte = (byte*)data.Scan0;
Scan0 = data.Scan0;
Stride = data.Stride;
Width = data.Width;
Height = data.Height;
}
public void Dispose() {
@ -77,23 +51,24 @@ namespace ClassicalSharp {
if( data != null ) {
Bitmap.UnlockBits( data );
data = null;
scan0 = (byte*)IntPtr.Zero;
stride = 0;
scan0Byte = (byte*)IntPtr.Zero;
Scan0 = IntPtr.Zero;
Width = Height = Stride = 0;
}
}
public int GetPixel( int x, int y ) {
// 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
}
public int* GetRowPtr( int y ) {
return (int*)( scan0 + ( y * stride ) );
return (int*)( scan0Byte + ( y * Stride ) );
}
public void SetPixel( int x, int y, int col ) {
int* row = (int*)( scan0 + ( y * stride ) );
int* row = (int*)( scan0Byte + ( y * Stride ) );
row[x] = col;
}
}