public unsafe void GenerateThumbnail( bool _ForceRegenerate )
{
if ( m_Thumbnail != null && !_ForceRegenerate )
return; // Already generated
if ( m_Manifest == null )
throw new Exception( "Invalid manifest to load textures from!" );
// Attempt to load an existing thumbnail
FileInfo FileName = ThumbnailFileName;
if ( FileName.Exists )
{
if ( !_ForceRegenerate )
{ // It does!
m_Thumbnail = Bitmap.FromFile( FileName.FullName ) as Bitmap;
return;
}
// Erase existing thumbnail
if ( m_Thumbnail != null )
m_Thumbnail.Dispose();
FileName.Delete();
}
// Make sure the textures are ready
m_Manifest.LoadTextures();
Bitmap Source = m_Manifest.m_Texture;
BitmapData LockedBitmap = Source.LockBits( new Rectangle( 0, 0, Source.Width, Source.Height ), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb );
int TargetHeight = THUMBNAIL_WIDTH * Source.Height / Source.Width;
Bitmap Target = new Bitmap( THUMBNAIL_WIDTH, TargetHeight, PixelFormat.Format32bppArgb );
BitmapData LockedBitmap2 = Target.LockBits( new Rectangle( 0, 0, Target.Width, Target.Height ), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb );
for ( int Y=0; Y < TargetHeight; Y++ )
{
byte* pScanlineSource = (byte*) LockedBitmap.Scan0.ToPointer() + (Y * Source.Height / TargetHeight) * LockedBitmap.Stride;
byte* pScanlineTarget = (byte*) LockedBitmap2.Scan0.ToPointer() + Y * LockedBitmap2.Stride;
for ( int X=0; X < THUMBNAIL_WIDTH; X++ )
{
byte* pPixelSource = pScanlineSource + (X * Source.Width / THUMBNAIL_WIDTH) * 4;
*pScanlineTarget++ = *pPixelSource++;
*pScanlineTarget++ = *pPixelSource++;
*pScanlineTarget++ = *pPixelSource++;
*pScanlineTarget++ = *pPixelSource++;
}
}
Target.UnlockBits( LockedBitmap2 );
Source.UnlockBits( LockedBitmap );
// We have a new thumbnail!
m_Thumbnail = Target;
m_Thumbnail.Save( FileName.FullName, ImageFormat.Jpeg );
m_Thumbnail.Dispose();
m_Thumbnail = null;
}