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 ;
2019-12-29 02:54:12 +00:00
using System.Drawing ;
2009-04-19 18:07:22 +00:00
using System.IO ;
2016-02-22 08:04:06 +00:00
using CodeImp.DoomBuilder.IO ;
2009-04-19 18:07:22 +00:00
#endregion
namespace CodeImp.DoomBuilder.Data
{
internal class SimpleTextureImage : ImageData
{
#region = = = = = = = = = = = = = = = = = = Constants
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
2016-02-22 08:04:06 +00:00
private readonly string lumpname ;
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
public SimpleTextureImage ( string name , string lumpname , float scalex , float scaley )
{
// Initialize
2010-01-02 20:22:05 +00:00
this . scale . x = scalex ;
this . scale . y = scaley ;
2009-04-19 18:07:22 +00:00
this . lumpname = lumpname ;
SetName ( name ) ;
2015-03-22 20:43:05 +00:00
virtualname = "[Textures]/" + this . name ; //mxd
2009-04-19 18:07:22 +00:00
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
// This loads the image
2019-12-29 02:54:12 +00:00
protected override LocalLoadResult LocalLoadImage ( )
2009-04-19 18:07:22 +00:00
{
2019-12-29 02:54:12 +00:00
// Get the patch data stream
Bitmap bitmap = null ;
string error = null ;
string patchlocation = string . Empty ; //mxd
Stream patchdata = General . Map . Data . GetTextureData ( lumpname , hasLongName , ref patchlocation ) ;
if ( patchdata ! = null )
2009-04-19 18:07:22 +00:00
{
2019-12-29 02:54:12 +00:00
// Copy patch data to memory
byte [ ] membytes = new byte [ ( int ) patchdata . Length ] ;
2016-08-29 10:06:16 +00:00
2019-12-29 02:54:12 +00:00
lock ( patchdata ) //mxd
{
patchdata . Seek ( 0 , SeekOrigin . Begin ) ;
patchdata . Read ( membytes , 0 , ( int ) patchdata . Length ) ;
}
2016-08-29 10:06:16 +00:00
2019-12-29 02:54:12 +00:00
MemoryStream mem = new MemoryStream ( membytes ) ;
mem . Seek ( 0 , SeekOrigin . Begin ) ;
2009-04-19 18:07:22 +00:00
2019-12-29 02:54:12 +00:00
// Get a reader for the data
IImageReader reader = ImageDataFormat . GetImageReader ( mem , ImageDataFormat . DOOMPICTURE , General . Map . Data . Palette ) ;
if ( ! ( reader is UnknownImageReader ) )
{
// Load the image
mem . Seek ( 0 , SeekOrigin . Begin ) ;
try { bitmap = reader . ReadAsBitmap ( mem ) ; }
catch ( InvalidDataException )
2009-04-19 18:07:22 +00:00
{
2019-12-29 02:54:12 +00:00
// Data cannot be read!
bitmap = null ;
2009-04-19 18:07:22 +00:00
}
}
2019-12-29 02:54:12 +00:00
// Not loaded?
if ( bitmap = = null )
2009-04-19 18:07:22 +00:00
{
2019-12-29 02:54:12 +00:00
error = "Image lump \"" + Path . Combine ( patchlocation , lumpname ) + "\" data format could not be read, while loading texture \"" + this . Name + "\". Does this lump contain valid picture data at all?" ;
2009-04-19 18:07:22 +00:00
}
2019-12-29 02:54:12 +00:00
// Done
mem . Dispose ( ) ;
2009-04-19 18:07:22 +00:00
}
2019-12-29 02:54:12 +00:00
else
{
error = "Image lump \"" + lumpname + "\" could not be found, while loading texture \"" + this . Name + "\". Did you forget to include required resources?" ;
}
return new LocalLoadResult ( bitmap , error ) ;
2009-04-19 18:07:22 +00:00
}
#endregion
}
}