2009-04-19 18:07:22 +00:00
#region = = = = = = = = = = = = = = = = = = Copyright ( c ) 2007 Pascal vd Heiden
/ *
* Copyright ( c ) 2007 Pascal vd Heiden , www . codeimp . com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* /
#endregion
#region = = = = = = = = = = = = = = = = = = Namespaces
using System ;
using System.IO ;
using CodeImp.DoomBuilder.IO ;
#endregion
namespace CodeImp.DoomBuilder.Data
{
public sealed class FileImage : ImageData
{
#region = = = = = = = = = = = = = = = = = = Variables
2012-07-23 21:28:23 +00:00
//private string filepathname;
2009-04-19 18:07:22 +00:00
private int probableformat ;
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
public FileImage ( string name , string filepathname , bool asflat )
{
// Initialize
2012-07-23 21:28:23 +00:00
//this.filepathname = filepathname;
2012-08-05 19:18:05 +00:00
this . fullName = filepathname ; //mxd
2009-04-19 18:07:22 +00:00
SetName ( name ) ;
if ( asflat )
{
probableformat = ImageDataFormat . DOOMFLAT ;
2010-01-02 20:22:05 +00:00
this . scale . x = General . Map . Config . DefaultFlatScale ;
this . scale . y = General . Map . Config . DefaultFlatScale ;
2009-04-19 18:07:22 +00:00
}
else
{
probableformat = ImageDataFormat . DOOMPICTURE ;
2010-01-02 20:22:05 +00:00
this . scale . x = General . Map . Config . DefaultTextureScale ;
this . scale . y = General . Map . Config . DefaultTextureScale ;
2009-04-19 18:07:22 +00:00
}
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
// Constructor
public FileImage ( string name , string filepathname , bool asflat , float scalex , float scaley )
{
// Initialize
2012-08-05 19:18:05 +00:00
this . fullName = filepathname ; //mxd
2010-01-02 20:22:05 +00:00
this . scale . x = scalex ;
this . scale . y = scaley ;
2009-04-19 18:07:22 +00:00
SetName ( name ) ;
if ( asflat )
probableformat = ImageDataFormat . DOOMFLAT ;
else
probableformat = ImageDataFormat . DOOMPICTURE ;
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
// This loads the image
protected override void LocalLoadImage ( )
{
// Leave when already loaded
if ( this . IsImageLoaded ) return ;
lock ( this )
{
// Load file data
if ( bitmap ! = null ) bitmap . Dispose ( ) ; bitmap = null ;
2012-07-23 21:28:23 +00:00
MemoryStream filedata = new MemoryStream ( File . ReadAllBytes ( fullName ) ) ;
2009-04-19 18:07:22 +00:00
// Get a reader for the data
IImageReader reader = ImageDataFormat . GetImageReader ( filedata , probableformat , General . Map . Data . Palette ) ;
if ( ! ( reader is UnknownImageReader ) )
{
// Load the image
filedata . Seek ( 0 , SeekOrigin . Begin ) ;
try { bitmap = reader . ReadAsBitmap ( filedata ) ; }
catch ( InvalidDataException )
{
// Data cannot be read!
bitmap = null ;
}
}
// Not loaded?
if ( bitmap = = null )
{
2012-07-23 21:28:23 +00:00
General . ErrorLogger . Add ( ErrorType . Error , "Image file '" + fullName + "' data format could not be read, while loading image '" + this . Name + "'. Is this a valid picture file at all?" ) ;
2009-04-19 18:07:22 +00:00
loadfailed = true ;
}
else
{
2010-01-02 20:22:05 +00:00
// Get width and height
2009-04-19 18:07:22 +00:00
width = bitmap . Size . Width ;
height = bitmap . Size . Height ;
}
// Pass on to base
filedata . Dispose ( ) ;
base . LocalLoadImage ( ) ;
}
}
#endregion
}
}