public static Texture2D FromStream(GraphicsDevice gd, Stream str)
{
//if (!UseSoftLoad)
//{
//attempt monogame load of image
var magic = (str.ReadByte() | (str.ReadByte() << 8));
str.Seek(0, SeekOrigin.Begin);
magic += 0;
if (magic == 0x4D42)
{
try
{
//it's a bitmap.
var tex = Texture2D.FromStream(gd, str);
ManualTextureMaskSingleThreaded(ref tex, MASK_COLORS.ToArray());
return tex;
}
catch (Exception)
{
return null; //bad bitmap :(
}
}
else
{
//test for targa
str.Seek(-18, SeekOrigin.End);
byte[] sig = new byte[16];
str.Read(sig, 0, 16);
str.Seek(0, SeekOrigin.Begin);
if (ASCIIEncoding.Default.GetString(sig) == "TRUEVISION-XFILE")
{
try
{
var tga = new TargaImagePCL.TargaImage(str);
var tex = new Texture2D(gd, tga.Image.Width, tga.Image.Height);
tex.SetData(tga.Image.ToBGRA(true));
return tex;
}
catch (Exception)
{
return null; //bad tga
}
} else
{
//anything else
try
{
var tex = Texture2D.FromStream(gd, str);
return tex;
}
catch (Exception e)
{
Console.WriteLine("error: " + e.ToString());
return new Texture2D(gd, 1, 1);
}
}
}
}