From e89dfb9f56401a3a2909ac2fa6d3f23779dbce0a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 10 Apr 2015 08:42:52 +1000 Subject: [PATCH] Simplify FastBitmap. --- Utils/FastBitmap.cs | 59 +++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/Utils/FastBitmap.cs b/Utils/FastBitmap.cs index 33a6d76c7..d43de1aeb 100644 --- a/Utils/FastBitmap.cs +++ b/Utils/FastBitmap.cs @@ -4,15 +4,8 @@ using System.Drawing.Imaging; namespace ClassicalSharp { - /// Represents a wrapper around a bitmap. Provides - /// a fast implementation for getting and setting pixels in that bitmap. - /// Only supports 32 bit RGBA pixel format. public unsafe class FastBitmap : IDisposable { - /// Constructs a new FastBitmap wrapped around the specified Bitmap. - /// Bitmap which is wrapped. - /// Whether to immediately lock the bits of the bitmap, - /// so that get and set pixel operations can be performed immediately after construction. 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; } } - /// Gets the address of the first pixel in this bitmap. - public IntPtr Scan0 { - get { return data.Scan0; } - } + public IntPtr Scan0; + public int Stride; + public int Width, Height; - /// Gets the stride width/scan width of the bitmap. - /// (i.e. the actual size of each scanline, including padding) - public int Stride { - get { return data.Stride; } - } - - /// Gets the width of this bitmap, in pixels. - public int Width { - get { return data.Width; } - } - - /// Gets the height of this bitmap, in pixels. - public int Height { - get { return data.Height; } - } - - /// Locks the wrapped bitmap into system memory, - /// so that fast get/set pixel operations can be performed. 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; } }