Fix issue with non 32bpp skins crashing the client.

This commit is contained in:
UnknownShadow200 2015-09-19 19:39:12 +10:00
parent ee16d19678
commit c756094023
2 changed files with 25 additions and 8 deletions

View File

@ -19,10 +19,24 @@ namespace ClassicalSharp.GraphicsAPI {
public int CreateTexture( Bitmap bmp ) { public int CreateTexture( Bitmap bmp ) {
Rectangle rec = new Rectangle( 0, 0, bmp.Width, bmp.Height ); Rectangle rec = new Rectangle( 0, 0, bmp.Width, bmp.Height );
BitmapData data = bmp.LockBits( rec, ImageLockMode.ReadOnly, bmp.PixelFormat ); // Convert other pixel formats into 32bpp formats.
int texId = CreateTexture( data.Width, data.Height, data.Scan0 ); if( !FastBitmap.CheckFormat( bmp.PixelFormat ) ) {
bmp.UnlockBits( data ); Utils.LogDebug( "Converting " + bmp.PixelFormat + " into 32bpp image" );
return texId; using( Bitmap _32bmp = new Bitmap( bmp.Width, bmp.Height ) ) {
using( Graphics g = Graphics.FromImage( _32bmp ) )
g.DrawImage( bmp, 0, 0, bmp.Width, bmp.Height );
BitmapData data = _32bmp.LockBits( rec, ImageLockMode.ReadOnly, _32bmp.PixelFormat );
int texId = CreateTexture( data.Width, data.Height, data.Scan0 );
_32bmp.UnlockBits( data );
return texId;
}
} else {
BitmapData data = bmp.LockBits( rec, ImageLockMode.ReadOnly, bmp.PixelFormat );
int texId = CreateTexture( data.Width, data.Height, data.Scan0 );
bmp.UnlockBits( data );
return texId;
}
} }
public int CreateTexture( FastBitmap bmp ) { public int CreateTexture( FastBitmap bmp ) {

View File

@ -34,14 +34,17 @@ namespace ClassicalSharp {
public int Stride; public int Stride;
public int Width, Height; public int Width, Height;
public static bool CheckFormat( PixelFormat format ) {
return format == PixelFormat.Format32bppRgb || format == PixelFormat.Format32bppArgb;
}
public void LockBits() { public void LockBits() {
if( Bitmap == null ) throw new InvalidOperationException( "Underlying bitmap is null." ); if( Bitmap == null ) throw new InvalidOperationException( "Underlying bitmap is null." );
if( data != null ) return; if( data != null ) return;
PixelFormat format = Bitmap.PixelFormat; PixelFormat format = Bitmap.PixelFormat;
if( !( format == PixelFormat.Format32bppArgb || format == PixelFormat.Format32bppRgb ) ) { if( !CheckFormat( format ) )
throw new NotSupportedException( "Unsupported bitmap pixel format: " + format ); throw new NotSupportedException( "Unsupported bitmap pixel format: " + format );
}
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 );