/// <summary>
/// Loads an image from a stream.
/// </summary>
/// <remarks>
/// This method allows loading an image from a stream, which is helpful for when
/// images are being decompressed from an archive into a stream, which needs to be
/// loaded as is.
/// </remarks>
/// <param name="stream">Stream serving as the data source.</param>
/// <param name="type">
/// Type (i.e. file format) of image. Used to decide which image decompression codec to use.
/// </param>
public static Image FromStream(Stream stream, string type)
{
// find the codec for this file type
ICodec codec = CodecManager.Instance.GetCodec(type);
MemoryStream decoded = new MemoryStream();
ImageCodec.ImageData data = (ImageCodec.ImageData)codec.Decode(stream, decoded);
Image image = new Image();
// copy the image data
image.height = data.height;
image.width = data.width;
image.depth = data.depth;
image.format = data.format;
image.flags = data.flags;
image.numMipMaps = data.numMipMaps;
// stuff the image data into an array
byte[] buffer = new byte[decoded.Length];
decoded.Position = 0;
decoded.Read(buffer, 0, buffer.Length);
decoded.Close();
image.SetBuffer(buffer);
return(image);
}