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 ;
2014-12-03 09:06:05 +00:00
using CodeImp.DoomBuilder.Controls ;
2009-04-19 18:07:22 +00:00
#endregion
namespace CodeImp.DoomBuilder.Data
{
public sealed class FileImage : ImageData
{
#region = = = = = = = = = = = = = = = = = = Variables
2014-11-25 11:52:01 +00:00
private readonly int probableformat ;
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
public FileImage ( string name , string filepathname , bool asflat )
{
// Initialize
2014-11-25 11:52:01 +00:00
this . isFlat = asflat ; //mxd
SetName ( name , filepathname ) ;
2009-04-19 18:07:22 +00:00
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
2010-01-02 20:22:05 +00:00
this . scale . x = scalex ;
this . scale . y = scaley ;
2014-11-25 11:52:01 +00:00
this . isFlat = asflat ; //mxd
SetName ( name , filepathname ) ;
2009-04-19 18:07:22 +00:00
2014-11-25 11:52:01 +00:00
probableformat = ( asflat ? ImageDataFormat . DOOMFLAT : ImageDataFormat . DOOMPICTURE ) ;
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
//mxd. Constructor for loading internal images
internal FileImage ( string name , string filepathname )
{
// Initialize
SetName ( name , filepathname , true ) ;
probableformat = ImageDataFormat . DOOMPICTURE ;
2009-04-19 18:07:22 +00:00
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
2014-11-25 11:52:01 +00:00
//mxd: name is relative path to the image ("\Textures\sometexture.png")
//mxd: filepathname is absolute path to the image ("D:\Doom\MyCoolProject\Textures\sometexture.png")
//mxd: also, zdoom uses '/' as directory separator char.
private void SetName ( string name , string filepathname )
{
2014-12-03 09:06:05 +00:00
SetName ( name , filepathname , General . Map . Config . UseLongTextureNames ) ;
2014-11-25 11:52:01 +00:00
}
private void SetName ( string name , string filepathname , bool uselongtexturenames )
{
name = name . Replace ( Path . DirectorySeparatorChar , Path . AltDirectorySeparatorChar ) ;
if ( ! uselongtexturenames )
{
this . name = Path . GetFileNameWithoutExtension ( name . ToUpperInvariant ( ) ) ;
2014-12-03 09:06:05 +00:00
if ( this . name . Length > DataManager . CLASIC_IMAGE_NAME_LENGTH )
{
2014-11-25 11:52:01 +00:00
this . name = this . name . Substring ( 0 , DataManager . CLASIC_IMAGE_NAME_LENGTH ) ;
2014-12-03 09:06:05 +00:00
}
2014-11-25 11:52:01 +00:00
this . virtualname = Path . Combine ( Path . GetDirectoryName ( name ) , this . name ) . Replace ( Path . DirectorySeparatorChar , Path . AltDirectorySeparatorChar ) ;
this . displayname = this . name ;
2014-12-03 09:06:05 +00:00
this . shortname = this . name ;
2014-11-25 11:52:01 +00:00
}
else
{
this . name = name ;
this . virtualname = name ;
this . displayname = Path . GetFileNameWithoutExtension ( name ) ;
2014-12-03 09:06:05 +00:00
this . shortname = this . displayname . ToUpperInvariant ( ) ;
if ( this . shortname . Length > DataManager . CLASIC_IMAGE_NAME_LENGTH )
{
this . shortname = this . shortname . Substring ( 0 , DataManager . CLASIC_IMAGE_NAME_LENGTH ) ;
}
2014-11-25 11:52:01 +00:00
hasLongName = true ;
}
2014-12-03 09:06:05 +00:00
this . longname = Lump . MakeLongName ( this . name , uselongtexturenames ) ; //mxd
2014-11-25 11:52:01 +00:00
this . fullname = filepathname ;
2014-12-03 09:06:05 +00:00
2014-11-25 11:52:01 +00:00
if ( General . Settings . CapitalizeTextureNames & & ! string . IsNullOrEmpty ( this . displayname ) )
{
this . displayname = this . displayname . ToUpperInvariant ( ) ;
}
2014-12-03 09:06:05 +00:00
if ( this . displayname . Length > ImageBrowserItem . MAX_NAME_LENGTH )
{
this . displayname = this . displayname . Substring ( 0 , ImageBrowserItem . MAX_NAME_LENGTH ) ;
}
2014-11-25 11:52:01 +00:00
}
2009-04-19 18:07:22 +00:00
// 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 ;
2013-07-29 08:50:50 +00:00
2014-11-25 11:52:01 +00:00
MemoryStream filedata = new MemoryStream ( File . ReadAllBytes ( fullname ) ) ;
2013-07-30 09:25:27 +00:00
// Get a reader for the data
IImageReader reader = ImageDataFormat . GetImageReader ( filedata , probableformat , General . Map . Data . Palette ) ;
2014-11-25 11:52:01 +00:00
if ( ! ( reader is UnknownImageReader ) )
{
2013-07-30 09:25:27 +00:00
// Load the image
filedata . Seek ( 0 , SeekOrigin . Begin ) ;
2014-11-25 11:52:01 +00:00
try { bitmap = reader . ReadAsBitmap ( filedata ) ; }
catch ( InvalidDataException )
{
2013-07-30 09:25:27 +00:00
// Data cannot be read!
bitmap = null ;
2013-07-29 08:50:50 +00:00
}
2013-07-30 09:25:27 +00:00
}
2013-07-29 08:50:50 +00:00
2013-07-30 09:25:27 +00:00
// Not loaded?
2014-11-25 11:52:01 +00:00
if ( bitmap = = null )
{
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?" ) ;
2013-07-30 09:25:27 +00:00
loadfailed = true ;
2014-11-25 11:52:01 +00:00
}
else
{
2013-07-30 09:25:27 +00:00
// Get width and height
width = bitmap . Size . Width ;
height = bitmap . Size . Height ;
2009-04-19 18:07:22 +00:00
}
2013-07-30 09:25:27 +00:00
// Pass on to base
filedata . Dispose ( ) ;
base . LocalLoadImage ( ) ;
2009-04-19 18:07:22 +00:00
}
}
#endregion
}
}