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;
|
2019-12-29 02:54:12 +00:00
|
|
|
using System.Drawing;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Data
|
|
|
|
{
|
2017-01-26 10:01:11 +00:00
|
|
|
public sealed class FileImage : ImageData
|
|
|
|
{
|
|
|
|
#region ================== Variables
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 10:01:11 +00:00
|
|
|
private readonly int probableformat;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 10:01:11 +00:00
|
|
|
#endregion
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 10:01:11 +00:00
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public FileImage(string name, string filepathname, bool asflat)
|
|
|
|
{
|
2017-01-26 07:09:22 +00:00
|
|
|
// Initialize
|
2017-01-26 10:01:11 +00:00
|
|
|
this.isFlat = asflat; //mxd
|
|
|
|
|
|
|
|
if (asflat)
|
|
|
|
{
|
|
|
|
probableformat = ImageDataFormat.DOOMFLAT;
|
|
|
|
this.scale.x = General.Map.Config.DefaultFlatScale;
|
|
|
|
this.scale.y = General.Map.Config.DefaultFlatScale;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
probableformat = ImageDataFormat.DOOMPICTURE;
|
|
|
|
this.scale.x = General.Map.Config.DefaultTextureScale;
|
|
|
|
this.scale.y = General.Map.Config.DefaultTextureScale;
|
|
|
|
}
|
2017-01-26 07:09:22 +00:00
|
|
|
|
|
|
|
SetName(name, filepathname);
|
|
|
|
|
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
2017-01-26 10:01:11 +00:00
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 10:01:11 +00:00
|
|
|
// Constructor
|
|
|
|
public FileImage(string name, string filepathname, bool asflat, float scalex, float scaley)
|
|
|
|
{
|
2017-01-26 07:09:22 +00:00
|
|
|
// Initialize
|
|
|
|
this.scale.x = scalex;
|
2017-01-26 10:01:11 +00:00
|
|
|
this.scale.y = scaley;
|
|
|
|
this.isFlat = asflat; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 10:01:11 +00:00
|
|
|
probableformat = (asflat ? ImageDataFormat.DOOMFLAT : ImageDataFormat.DOOMPICTURE);
|
2014-11-25 11:52:01 +00:00
|
|
|
|
2017-01-26 07:09:22 +00:00
|
|
|
SetName(name, filepathname);
|
|
|
|
|
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
2017-01-26 10:01:11 +00:00
|
|
|
}
|
2014-11-25 11:52:01 +00:00
|
|
|
|
2017-01-26 10:01:11 +00:00
|
|
|
//mxd. Constructor for loading internal images
|
|
|
|
internal FileImage(string name, string filepathname)
|
|
|
|
{
|
|
|
|
probableformat = ImageDataFormat.DOOMPICTURE;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 07:09:22 +00:00
|
|
|
SetName(name, filepathname, true, 1);
|
|
|
|
|
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
2017-01-26 10:01:11 +00:00
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 10:01:11 +00:00
|
|
|
#endregion
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 10:01:11 +00:00
|
|
|
#region ================== Methods
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2017-01-26 10:01:11 +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.
|
|
|
|
//mxd: and doesn't recognize long texture names in a root folder / pk3/7 root
|
2017-01-26 07:09:22 +00:00
|
|
|
//[ZZ] and doesn't work with flats in Doom format (added SetName call to post-load to validate this)
|
2019-11-30 16:05:00 +00:00
|
|
|
// biwa. It works (again?) with Doom flat format
|
2017-01-26 10:01:11 +00:00
|
|
|
private void SetName(string name, string filepathname)
|
|
|
|
{
|
2019-11-30 16:05:00 +00:00
|
|
|
//SetName(name, filepathname, General.Map.Config.UseLongTextureNames, (probableformat == ImageDataFormat.DOOMFLAT) ? -1 : 0);
|
|
|
|
SetName(name, filepathname, General.Map.Config.UseLongTextureNames, 0);
|
|
|
|
}
|
2014-11-25 11:52:01 +00:00
|
|
|
|
2017-01-26 07:09:22 +00:00
|
|
|
// prevent long texture names by forcelongtexturename=-1
|
|
|
|
private void SetName(string name, string filepathname, bool uselongtexturenames, int forcelongtexturename)
|
2017-01-26 10:01:11 +00:00
|
|
|
{
|
|
|
|
name = name.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
2014-11-25 11:52:01 +00:00
|
|
|
|
2017-01-26 10:01:11 +00:00
|
|
|
if (forcelongtexturename < 0 || (forcelongtexturename == 0 && (!uselongtexturenames || string.IsNullOrEmpty(Path.GetDirectoryName(name)))))
|
|
|
|
{
|
|
|
|
this.name = Path.GetFileNameWithoutExtension(name.ToUpperInvariant());
|
|
|
|
if (this.name.Length > DataManager.CLASIC_IMAGE_NAME_LENGTH)
|
|
|
|
{
|
|
|
|
this.name = this.name.Substring(0, DataManager.CLASIC_IMAGE_NAME_LENGTH);
|
|
|
|
}
|
|
|
|
this.virtualname = Path.Combine(Path.GetDirectoryName(name), this.name).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
|
|
|
this.displayname = this.name;
|
|
|
|
this.shortname = this.name;
|
2017-01-26 07:09:22 +00:00
|
|
|
hasLongName = false;
|
2017-01-26 10:01:11 +00:00
|
|
|
}
|
2014-11-25 11:52:01 +00:00
|
|
|
else
|
|
|
|
{
|
2017-01-26 10:01:11 +00:00
|
|
|
this.name = name;
|
|
|
|
this.virtualname = name;
|
|
|
|
this.displayname = Path.GetFileNameWithoutExtension(name);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
hasLongName = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.longname = Lump.MakeLongName(this.name, uselongtexturenames);
|
|
|
|
this.filepathname = filepathname;
|
2019-05-30 22:20:12 +00:00
|
|
|
|
|
|
|
ComputeNamesWidth(); // biwa
|
2017-01-26 10:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This loads the image
|
2019-12-29 02:54:12 +00:00
|
|
|
protected override LocalLoadResult LocalLoadImage()
|
2017-01-26 10:01:11 +00:00
|
|
|
{
|
2019-12-29 02:54:12 +00:00
|
|
|
// Load file data
|
|
|
|
Bitmap bitmap = null;
|
|
|
|
string error = null;
|
2017-01-26 10:01:11 +00:00
|
|
|
|
2019-12-29 02:54:12 +00:00
|
|
|
MemoryStream filedata = null;
|
|
|
|
try
|
2017-01-26 10:01:11 +00:00
|
|
|
{
|
2019-12-29 02:54:12 +00:00
|
|
|
filedata = new MemoryStream(File.ReadAllBytes(filepathname));
|
|
|
|
}
|
|
|
|
catch (IOException)
|
|
|
|
{
|
|
|
|
error = "Image file \"" + filepathname + "\" could not be read, while loading image \"" + this.Name + "\". Consider reloading resources.";
|
|
|
|
}
|
2017-01-15 03:12:19 +00:00
|
|
|
|
2019-12-29 02:54:12 +00:00
|
|
|
if (filedata != null)
|
|
|
|
{
|
|
|
|
// Get a reader for the data
|
2020-01-14 16:25:35 +00:00
|
|
|
bitmap = ImageDataFormat.TryLoadImage(filedata, probableformat, General.Map.Data.Palette);
|
2017-01-15 03:12:19 +00:00
|
|
|
|
2019-12-29 02:54:12 +00:00
|
|
|
// Not loaded?
|
|
|
|
if (bitmap == null)
|
|
|
|
{
|
|
|
|
error = "Image file \"" + filepathname + "\" data format could not be read, while loading image \"" + this.Name + "\". Is this a valid picture file at all?";
|
2017-01-15 03:12:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 02:54:12 +00:00
|
|
|
filedata.Dispose();
|
2017-01-26 10:01:11 +00:00
|
|
|
}
|
2019-12-29 02:54:12 +00:00
|
|
|
|
|
|
|
return new LocalLoadResult(bitmap, error);
|
2017-01-26 10:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|