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.IO;
|
|
|
|
using CodeImp.DoomBuilder.IO;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Data
|
|
|
|
{
|
|
|
|
internal static class ImageDataFormat
|
|
|
|
{
|
|
|
|
// Input guess formats
|
|
|
|
public const int UNKNOWN = 0; // No clue.
|
|
|
|
public const int DOOMPICTURE = 1; // Could be Doom Picture format (column list rendered data)
|
|
|
|
public const int DOOMFLAT = 2; // Could be Doom Flat format (raw 8-bit pixel data)
|
2009-05-12 09:50:08 +00:00
|
|
|
public const int DOOMCOLORMAP = 3; // Could be Doom Colormap format (raw 8-bit pixel palette mapping)
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// File format signatures
|
2014-11-25 11:52:01 +00:00
|
|
|
private static readonly int[] PNG_SIGNATURE = new[] { 137, 80, 78, 71, 13, 10, 26, 10 };
|
|
|
|
private static readonly int[] GIF_SIGNATURE = new[] { 71, 73, 70 };
|
|
|
|
private static readonly int[] BMP_SIGNATURE = new[] { 66, 77 };
|
|
|
|
private static readonly int[] DDS_SIGNATURE = new[] { 68, 68, 83, 32 };
|
|
|
|
private static readonly int[] JPG_SIGNATURE = new[] { 255, 216, 255 }; //mxd
|
|
|
|
private static readonly int[] TGA_SIGNATURE = new[] { 0, 0, 2, 0 }; //mxd
|
|
|
|
private static readonly int[] PCX_SIGNATURE = new[] { 10, 5, 1, 8 }; //mxd
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// This check image data and returns the appropriate image reader
|
|
|
|
public static IImageReader GetImageReader(Stream data, int guessformat, Playpal palette)
|
|
|
|
{
|
2014-11-25 11:52:01 +00:00
|
|
|
if(data == null) return new UnknownImageReader(); //mxd
|
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Data long enough to check for signatures?
|
2014-11-25 11:52:01 +00:00
|
|
|
if(data.Length > 10)
|
|
|
|
{
|
2009-04-19 18:07:22 +00:00
|
|
|
// Check for PNG signature
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
2013-12-02 15:02:01 +00:00
|
|
|
if(CheckSignature(data, PNG_SIGNATURE))
|
|
|
|
return new FileImageReader(DevilImageType.IL_PNG);
|
2012-06-01 19:53:14 +00:00
|
|
|
|
|
|
|
// Check for DDS signature
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
2013-12-02 15:02:01 +00:00
|
|
|
if(CheckSignature(data, DDS_SIGNATURE))
|
|
|
|
return new FileImageReader(DevilImageType.IL_DDS);
|
2012-06-01 19:53:14 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Check for GIF signature
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
2013-12-02 15:02:01 +00:00
|
|
|
if(CheckSignature(data, GIF_SIGNATURE))
|
2015-12-07 21:57:59 +00:00
|
|
|
return new UnknownImageReader(); //mxd. Not supported by (G)ZDoom
|
2009-04-19 18:07:22 +00:00
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
//mxd. Check for PCX signature
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
2013-12-02 15:02:01 +00:00
|
|
|
if(CheckSignature(data, PCX_SIGNATURE))
|
|
|
|
return new FileImageReader(DevilImageType.IL_PCX);
|
2012-08-05 19:18:05 +00:00
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
//mxd. Check for JPG signature
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
2013-12-02 15:02:01 +00:00
|
|
|
if(CheckSignature(data, JPG_SIGNATURE))
|
|
|
|
return new FileImageReader(DevilImageType.IL_JPG);
|
2012-08-05 19:18:05 +00:00
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
//mxd. Check for TGA signature
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
2013-12-02 15:02:01 +00:00
|
|
|
if(CheckSignature(data, TGA_SIGNATURE))
|
|
|
|
return new FileImageReader(DevilImageType.IL_TGA);
|
2012-06-03 23:36:53 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Check for BMP signature
|
2012-08-05 19:18:05 +00:00
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
2013-12-02 15:02:01 +00:00
|
|
|
if(CheckSignature(data, BMP_SIGNATURE))
|
2015-12-07 21:57:59 +00:00
|
|
|
return new UnknownImageReader(); //mxd. Not supported by (G)ZDoom
|
2009-05-12 09:50:08 +00:00
|
|
|
}
|
2013-12-20 09:24:43 +00:00
|
|
|
|
|
|
|
// Could it be a doom picture?
|
2014-11-25 11:52:01 +00:00
|
|
|
switch(guessformat)
|
|
|
|
{
|
2013-12-20 09:24:43 +00:00
|
|
|
case DOOMPICTURE:
|
|
|
|
// Check if data is valid for a doom picture
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
|
|
DoomPictureReader picreader = new DoomPictureReader(palette);
|
|
|
|
if(picreader.Validate(data)) return picreader;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOOMFLAT:
|
|
|
|
// Check if data is valid for a doom flat
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
|
|
DoomFlatReader flatreader = new DoomFlatReader(palette);
|
|
|
|
if(flatreader.Validate(data)) return flatreader;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOOMCOLORMAP:
|
|
|
|
// Check if data is valid for a doom colormap
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
|
|
DoomColormapReader colormapreader = new DoomColormapReader(palette);
|
|
|
|
if(colormapreader.Validate(data)) return colormapreader;
|
|
|
|
break;
|
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
// Format not supported
|
|
|
|
return new UnknownImageReader();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This checks a signature as byte array
|
|
|
|
// NOTE: Expects the stream position to be at the start of the
|
|
|
|
// signature, and expects the stream to be long enough.
|
|
|
|
private static bool CheckSignature(Stream data, int[] sig)
|
|
|
|
{
|
|
|
|
int b;
|
|
|
|
|
|
|
|
// Go for all bytes
|
|
|
|
for(int i = 0; i < sig.Length; i++)
|
|
|
|
{
|
|
|
|
// When byte doesnt match the signature, leave
|
|
|
|
b = data.ReadByte();
|
|
|
|
if(b != sig[i]) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signature matches
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|