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;
|
2014-12-03 09:06:05 +00:00
|
|
|
using CodeImp.DoomBuilder.Controls;
|
2009-04-19 18:07:22 +00:00
|
|
|
using CodeImp.DoomBuilder.IO;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Data
|
|
|
|
{
|
|
|
|
public sealed class PK3FileImage : ImageData
|
|
|
|
{
|
|
|
|
#region ================== Variables
|
|
|
|
|
2014-11-25 11:52:01 +00:00
|
|
|
private readonly PK3Reader datareader;
|
|
|
|
private readonly int probableformat;
|
2017-01-26 07:09:22 +00:00
|
|
|
private readonly string _c_filepathname;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
2014-11-25 11:52:01 +00:00
|
|
|
internal PK3FileImage(PK3Reader datareader, string filepathname, bool asflat)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
this.datareader = datareader;
|
2017-01-26 07:09:22 +00:00
|
|
|
_c_filepathname = filepathname; // this is used to call SetName later
|
2014-11-25 11:52:01 +00:00
|
|
|
this.isFlat = asflat; //mxd
|
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
|
|
|
}
|
2017-01-26 07:09:22 +00:00
|
|
|
|
|
|
|
SetName(filepathname);
|
|
|
|
|
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 07:09:22 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 07:09:22 +00:00
|
|
|
//mxd: filepathname is relative path to the image ("Textures\sometexture.png")
|
|
|
|
protected override void SetName(string filepathname)
|
|
|
|
{
|
|
|
|
SetName(filepathname, (probableformat == ImageDataFormat.DOOMFLAT) ? false : General.Map.Config.UseLongTextureNames);
|
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 07:09:22 +00:00
|
|
|
private void SetName(string filepathname, bool longtexturenames)
|
2014-11-25 11:52:01 +00:00
|
|
|
{
|
2017-01-26 07:09:22 +00:00
|
|
|
if(!longtexturenames || string.IsNullOrEmpty(Path.GetDirectoryName(filepathname)))
|
2014-11-25 11:52:01 +00:00
|
|
|
{
|
|
|
|
this.name = Path.GetFileNameWithoutExtension(filepathname.ToUpperInvariant());
|
2015-11-17 17:50:56 +00:00
|
|
|
if(this.name.Length > DataManager.CLASIC_IMAGE_NAME_LENGTH)
|
2014-12-03 09:06:05 +00:00
|
|
|
{
|
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.displayname = this.name;
|
2014-12-03 09:06:05 +00:00
|
|
|
this.shortname = this.name;
|
2017-01-26 07:09:22 +00:00
|
|
|
this.hasLongName = false;
|
|
|
|
}
|
2014-11-25 11:52:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.name = filepathname.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
|
|
|
this.displayname = Path.GetFileNameWithoutExtension(name);
|
2014-12-03 09:06:05 +00:00
|
|
|
this.shortname = this.displayname.ToUpperInvariant();
|
2015-11-17 17:50:56 +00:00
|
|
|
if(this.shortname.Length > DataManager.CLASIC_IMAGE_NAME_LENGTH)
|
2014-12-03 09:06:05 +00:00
|
|
|
{
|
|
|
|
this.shortname = this.shortname.Substring(0, DataManager.CLASIC_IMAGE_NAME_LENGTH);
|
|
|
|
}
|
2014-11-25 11:52:01 +00:00
|
|
|
this.hasLongName = true;
|
|
|
|
}
|
|
|
|
|
2014-12-03 09:06:05 +00:00
|
|
|
this.longname = Lump.MakeLongName(this.name);
|
2014-11-25 11:52:01 +00:00
|
|
|
this.virtualname = filepathname.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
2015-11-17 17:50:56 +00:00
|
|
|
this.filepathname = filepathname;
|
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;
|
2015-11-17 17:50:56 +00:00
|
|
|
MemoryStream filedata = datareader.LoadFile(filepathname); //mxd
|
2017-01-26 07:09:22 +00:00
|
|
|
|
|
|
|
bool isBadForLongTextureNames = false;
|
|
|
|
|
|
|
|
if (filedata != null)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
2014-11-25 11:52:01 +00:00
|
|
|
// Get a reader for the data
|
|
|
|
IImageReader reader = ImageDataFormat.GetImageReader(filedata, probableformat, General.Map.Data.Palette);
|
|
|
|
if(!(reader is UnknownImageReader))
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
2017-01-26 07:09:22 +00:00
|
|
|
// [ZZ] check for flat type
|
|
|
|
if (reader is DoomFlatReader)
|
|
|
|
isBadForLongTextureNames = true;
|
|
|
|
|
|
|
|
// Load the image
|
|
|
|
filedata.Seek(0, SeekOrigin.Begin);
|
2014-11-25 11:52:01 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
bitmap = reader.ReadAsBitmap(filedata);
|
|
|
|
}
|
2016-02-01 22:04:00 +00:00
|
|
|
catch(InvalidDataException)
|
2014-11-25 11:52:01 +00:00
|
|
|
{
|
|
|
|
// Data cannot be read!
|
|
|
|
bitmap = null;
|
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
2014-11-25 11:52:01 +00:00
|
|
|
|
|
|
|
// Not loaded?
|
|
|
|
if(bitmap == null)
|
|
|
|
{
|
2016-02-22 08:04:06 +00:00
|
|
|
General.ErrorLogger.Add(ErrorType.Error, "Image file \"" + filepathname + "\" data format could not be read, while loading texture \"" + this.Name + "\"");
|
2014-11-25 11:52:01 +00:00
|
|
|
loadfailed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Get width and height from image
|
|
|
|
width = bitmap.Size.Width;
|
|
|
|
height = bitmap.Size.Height;
|
|
|
|
}
|
|
|
|
|
|
|
|
filedata.Dispose();
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
2014-11-25 11:52:01 +00:00
|
|
|
|
2017-01-26 07:09:22 +00:00
|
|
|
SetName(_c_filepathname, isBadForLongTextureNames ? false : General.Map.Config.UseLongTextureNames);
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Pass on to base
|
|
|
|
base.LocalLoadImage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|