/// <summary> /// Converts the underlying data to the image type T specified. /// If the underlying image data cannot be converted to the specified type an InvalidCastException will be thrown. /// Currently limited to assets of type Texture as C# doesn't have TGA support natively and I've not gone looking for a lib. /// </summary> /// <returns>The image in the type specified..</returns> /// <param name="asset">Asset.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T ToImage <T>(this StratusAsset asset) { if (!asset.HasAssetData()) { return(default(T)); } switch (asset.Type) { case (sbyte)AssetType.Texture: return(CSJ2K.J2kImage.FromBytes(asset.Data).As <T>()); case (sbyte)AssetType.ImageJPEG: System.Drawing.Image image; using (var stream = new System.IO.MemoryStream(asset.Data)) { image = System.Drawing.Image.FromStream(stream); } if (!image.GetType().IsAssignableFrom(typeof(T))) { throw new InvalidCastException($"Cannot cast to '{typeof(T).Name}'; type must be assignable from '{image.GetType().Name}'"); } return((T)(object)image); default: return(default(T)); } }
public static void TestStratusAsset_HasAssetData_Null() { var asset = new StratusAsset { Data = null }; Assert.False(asset.HasAssetData()); }
public static void TestStratusAsset_HasAssetData_One() { var asset = new StratusAsset { Data = new byte[] { 1 } }; Assert.True(asset.HasAssetData()); }
public static void TestStratusAsset_HasAssetData_Empty() { var asset = new StratusAsset { Data = new byte[] { } }; Assert.False(asset.HasAssetData()); }