0 dependency parser for Valves own .vtf (Valve Texture Format) Source Engine textures.
go get github.com/galaco/vtf/v2
- Supports versions 7.0-7.5
- Full header data
- Low resolution thumbnail loading
- Complete mipmap + high-resolution texture loading, including animated frames, cubemaps and volume textures
- v7.3+ resources (key values, sprite sheets, CRCs etc.)
- Streaming reads: only the bytes the texture occupies are read, and sizes claimed by a header are never trusted for allocation
- Optional conversion to
image.Image, with thevtfimagepackage
package main
import (
"log"
"github.com/galaco/vtf/v2"
)
func main() {
texture, err := vtf.ReadFromFile("foo.vtf")
if err != nil {
log.Fatal(err)
}
header := texture.Header()
log.Println(header.Width, header.Height, header.HighResImageFormat)
// Raw data of the largest mipmap of the first frame, face and slice
data := texture.Image()
log.Println(len(data), "bytes")
// Any mipmap (0 is the largest), frame, cubemap face or volume slice
smallest := texture.MipmapCount() - 1
mipmap, err := texture.Mipmap(smallest, 0, 0, 0)
if err != nil {
log.Fatal(err)
}
width, height, depth := texture.MipmapDimensions(smallest)
log.Println(width, height, depth, len(mipmap))
}Image data is returned in its stored format (Header().HighResImageFormat), and shares memory with the texture.
To read only the header:
reader := vtf.NewReader(file)
header, err := reader.ReadHeader()
// Optionally, continue reading the rest of the texture
texture, err := reader.Read()v7.3+ resources:
if kv, ok := texture.Resource(vtf.ResourceTagKeyValues); ok {
log.Println(string(kv.Data))
}The vtfimage package converts image data to an image.Image, and registers the format with the image package:
import (
"image"
"image/png"
_ "github.com/galaco/vtf/v2/vtfimage"
)
img, _, err := image.Decode(file)
err = png.Encode(out, img)Or convert a specific mipmap, or the thumbnail, with vtfimage.Mipmap, vtfimage.LowResImage and vtfimage.Convert.
Supported formats: all 8 bit RGB(A) orderings, 565/5551/4444 packed formats, I8, IA88, A8, blue screen formats, DXT1/DXT3/DXT5, ATI1N/ATI2N, RGBA16161616 and floating point formats (clamped to [0, 1]). P8, UV and depth buffer formats are not supported.
- Import
github.com/galaco/vtf/v2. HighResImageData(),MipmapsForFrame()andHighestResolutionImageForFrame()are replaced byMipmap(level, frame, face, slice),MipmapCount(),FrameCount(),FaceCount()andMipmapDimensions(). Mipmap level 0 is now the largest, where v1 indexed mipmaps smallest first.Header.Flagsisvtf.Flags, andHeader.HighResImageFormat/LowResImageFormatareformat.Format. Useuint32(...)where a plain integer is needed, andFlags.Hasto test flags.- Errors are named
ErrXrather thanErrorX, e.g.ErrSignatureMismatch,ErrMipmapSizeMismatch. FlagSRGBis now0x40; v1 incorrectly defined it as0xFFFF.
- Modify/export functionality
- Depth buffer formats (
NV_DST16etc.), which are only used for render targets
No where near all the possible texture configurations have been tested. It's possible some could cause issues. Any issues (including offending file) are greatly appreciated.