Unit test class for VtfReader that builds minimal VTF 7.1 binary blobs in memory and verifies BGRA8888 decoding and thumbnail skipping behavior.
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GmodAssetImporter;
[TestClass]
public class VtfReaderTests
{
[TestMethod]
public void DecodesBgra8888Vtf()
{
var image = VtfReader.Load( BuildVtf71( withThumbnail: false ) );
Assert.AreEqual( 2, image.Width );
Assert.AreEqual( 2, image.Height );
Assert.AreEqual( 255, image.Rgba[0] ); // R
Assert.AreEqual( 0, image.Rgba[1] );
Assert.AreEqual( 0, image.Rgba[2] );
}
[TestMethod]
public void SkipsLowResThumbnailBeforeHighResImage()
{
var image = VtfReader.Load( BuildVtf71( withThumbnail: true ) );
Assert.AreEqual( 255, image.Rgba[0] ); // first high-res pixel is red, not the thumbnail
Assert.AreEqual( 0, image.Rgba[1] );
Assert.AreEqual( 0, image.Rgba[2] );
Assert.AreEqual( 255, image.Rgba[13] ); // last pixel white
}
/// <summary>Minimal VTF 7.1 with a 2x2 BGRA8888 image and optional 1x1 BGRA8888 thumbnail.</summary>
static byte[] BuildVtf71( bool withThumbnail )
{
using var ms = new MemoryStream();
using ( var bw = new BinaryWriter( ms, System.Text.Encoding.ASCII, leaveOpen: true ) )
{
bw.Write( new byte[] { (byte)'V', (byte)'T', (byte)'F', 0 } );
bw.Write( 7u ); // major
bw.Write( 1u ); // minor
bw.Write( 64u ); // headerSize
bw.Write( (ushort)2 ); // width
bw.Write( (ushort)2 ); // height
bw.Write( 0u ); // flags
bw.Write( (ushort)1 ); // frames
bw.Write( (ushort)0 ); // firstFrame
bw.Write( new byte[4] ); // pad
bw.Write( 1f ); bw.Write( 1f ); bw.Write( 1f ); // reflectivity
bw.Write( new byte[4] ); // pad
bw.Write( 1f ); // bump
bw.Write( 12 ); // high format BGRA8888
bw.Write( (byte)1 ); // mipcount
bw.Write( withThumbnail ? 12 : -1 ); // low format (int)
bw.Write( (byte)(withThumbnail ? 1 : 0) ); // low w
bw.Write( (byte)(withThumbnail ? 1 : 0) ); // low h
bw.Flush();
while ( ms.Length < 64 )
ms.WriteByte( 0 );
ms.Position = 64;
if ( withThumbnail )
bw.Write( new byte[] { 9, 9, 9, 255 } );
bw.Write( new byte[]
{
0, 0, 255, 255, // red in BGRA
0, 255, 0, 255, // green
255, 0, 0, 255, // blue
255, 255, 255, 255 // white
} );
bw.Flush();
}
return ms.ToArray();
}
}