vtf

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2019 License: Unlicense Imports: 7 Imported by: 3

README

GoDoc Go report card GolangCI Build Status codecov CircleCI

vtf

0 dependency parser for Valves own .vtf (Valve Texture Format) Source Engine textures.

Features
  • Supports versions 7.1-7.5
  • Full header data
  • Low resolution thumbnail loading
  • Complete mipmap + high-resolution texture loading
Usage
import (  
	"github.com/galaco/vtf"
	"log"
	"os"
)

func main() {
  file,_ := os.LoadFile("foo.vtf")
  texture,err := vtf.ReadFromStream(file)
	if err != nil {
		log.Println(err)
	} else {
    log.Println(texture.Header().Width)
  }
}

Whats missing
  • Resource data is ignored (besides mipmaps) in 7.3+
  • Texture with depth > 1 are unsupported. This is very rare
  • Textures with zslices > 1 are unsupported. This is very rare
  • Modify/export functionality
What won't this ever do?
  • Colour format transformation. Header properties LowResImageFormat and HighResImageFormat will provide the format.
  • (Probably) support depths or zslices > 1
Contributing

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.

Documentation

Index

Constants

View Source
const (
	// FlagPointSampling
	FlagPointSampling = 0x0001
	// FlagTrilinearSampling
	FlagTrilinearSampling = 0x0002
	// FlagClampS
	FlagClampS = 0x0004
	// FlagClampT
	FlagClampT = 0x0008
	// FlagAnisotropicFiltering
	FlagAnisotropicFiltering = 0x0010
	// FlagHintDXT5
	FlagHintDXT5 = 0x0020
	// FlagPWLCorrected
	FlagPWLCorrected = 0x0040
	// FlagSRGB
	FlagSRGB = 0xFFFF // n/a
	// FlagNoCompress
	FlagNoCompress = 0x0040
	// FlagNormalMap
	FlagNormalMap = 0x0080
	// FlagNoMipmaps
	FlagNoMipmaps = 0x0100
	// FlagNoLevelOfDetail
	FlagNoLevelOfDetail = 0x0200
	// FlagNoMinimumMipmap
	FlagNoMinimumMipmap = 0x0400
	// FlagProcedural
	FlagProcedural = 0x0800
	// FlagOneBitAlpha
	FlagOneBitAlpha = 0x1000
	// FlagEightBitAlpha
	FlagEightBitAlpha = 0x2000
	// FlagEnvironmentMap
	FlagEnvironmentMap = 0x4000
	// FlagRenderTarget
	FlagRenderTarget = 0x8000
	// FlagDepthRenderTarget
	FlagDepthRenderTarget = 0x10000
	// FlagNoDebugOverride
	FlagNoDebugOverride = 0x20000
	// FlagSingleCopy
	FlagSingleCopy = 0x40000
	// FlagPreSRGB
	FlagPreSRGB = 0x80000
	// FlagOneOverMipmapLevelInAlpha
	FlagOneOverMipmapLevelInAlpha = 0x80000
	// FlagPreMultiplyColorByOneOverMipmapLevel
	FlagPreMultiplyColorByOneOverMipmapLevel = 0x100000
	// FlagNormalToDuDv
	FlagNormalToDuDv = 0x200000
	// FlagAlphaTestMipmapGeneration
	FlagAlphaTestMipmapGeneration = 0x400000
	// FlagNoDepthBuffer
	FlagNoDepthBuffer = 0x800000
	// FlagNiceFiltered
	FlagNiceFiltered = 0x1000000
	// FlagClampU
	FlagClampU = 0x2000000
	// FlagVertexTexture
	FlagVertexTexture = 0x4000000
	// FlagSSBump
	FlagSSBump = 0x8000000
	// FlagBorder
	FlagBorder = 0x20000000
)

Variables

View Source
var (
	// ErrorVtfSignatureMismatch occurs when a stream does not start with the VTF magic signature
	ErrorVtfSignatureMismatch = errors.New("header signature does not match: VTF\x00")
	// ErrorTextureDepthNotSupported occurs when attempting to parse a stream with depth > 1
	ErrorTextureDepthNotSupported = errors.New("only vtf textures with depth 1 are supported")
	// ErrorMipmapSizeMismatch occurs when filesize does not match calculated mipmap size
	ErrorMipmapSizeMismatch = errors.New("expected data size is smaller than actual")
)

Functions

This section is empty.

Types

type Header struct {
	HeaderCommon
	Header72
	Header73
}

Header for VTF Header format Contents includes information for all versions Its up to the implementee to decide what properties they need based on the version

type Header72

type Header72 struct {
	// Depth of the largest mipmap in pixels (^2) ushort
	Depth uint16
}

Header72 is v7.2+ includes these properties

type Header73

type Header73 struct {

	// NumResource is number of resources this vtf has
	NumResource uint32
	// contains filtered or unexported fields
}

Header73 is v7.3+ includes these properties

type HeaderCommon

type HeaderCommon struct {
	//Signature is file signature char
	Signature [4]byte
	// Version - Version[0].version[1] e.g. 7.2 uint
	Version [2]uint32
	// HeaderSize is size of Header (16 byte aligned, currently 80bytes) uint
	HeaderSize uint32
	// Width of largest mipmap (^2) ushort
	Width uint16
	// Height of largest mipmap (^2) ushort
	Height uint16
	// Flags are VTF Flags uint
	Flags uint32
	// Frames in number of frames (if animated) default: 1 ushort
	Frames uint16
	// FirstFrame is first frame in animation (0 based) ushort
	FirstFrame uint16

	// Reflectivity vector float
	Reflectivity [3]float32

	// BumpmapScale is bumpmap scale float
	BumpmapScale float32
	// HighResImageFormat is high resolution image format uint (probably 4?)
	HighResImageFormat uint32
	// MipmapCount is number of mipmaps uchar
	MipmapCount uint8
	// LowResImageFormat is low resolution image format (always DXT1 [=14]) uint
	LowResImageFormat uint32
	// LowResImageWidth is low resolution image width uchar
	LowResImageWidth uint8
	// LowResImageHeight is low resolution image height uchar
	LowResImageHeight uint8
	// contains filtered or unexported fields
}

HeaderCommon: All VTF versions start with these properties

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads from a vtf stream

func (*Reader) Read

func (reader *Reader) Read() (*Vtf, error)

Read parses vtf image from stream into a usable structure The only error to expect would be if mipmap data size overflows the total file size; normally due to tampered Header data.

func (*Reader) ReadHeader added in v1.2.0

func (reader *Reader) ReadHeader() (*Header, error)

ReadHeader reads the header of a texture only.

type Vtf

type Vtf struct {
	// contains filtered or unexported fields
}

Vtf: Exported vtf format Contains a Header, resources (v7.3+), low res thumbnail & high-res mipmaps

func ReadFromFile

func ReadFromFile(filepath string) (*Vtf, error)

ReadFromFile is a wrapper for ReadFromStream wrapper to load directly from filesystem. Exists for convenience

func ReadFromStream

func ReadFromStream(stream io.Reader) (*Vtf, error)

ReadFromStream loads a vtf from standard io.Reader stream

func (*Vtf) Header added in v1.1.0

func (vtf *Vtf) Header() Header

Header returns vtf Header

func (*Vtf) HighResImageData added in v1.1.0

func (vtf *Vtf) HighResImageData() [][][][][]uint8

HighResImageData returns all data for all mipmaps

func (*Vtf) HighestResolutionImageForFrame added in v1.1.0

func (vtf *Vtf) HighestResolutionImageForFrame(frame int) []byte

HighestResolutionImageForFrame returns the best possible resolution for a single frame in the vtf

func (*Vtf) Image added in v1.2.0

func (vtf *Vtf) Image() []uint8

Image returns raw data of the first frame of the highest resolution mipmap

func (*Vtf) LowResImageData added in v1.1.0

func (vtf *Vtf) LowResImageData() []uint8

LowResImageData returns raw data of low-resolution thumbnail

func (*Vtf) MipmapsForFrame added in v1.1.0

func (vtf *Vtf) MipmapsForFrame(frame int) [][]uint8

MipmapsForFrame returns all mipmap sizes for a single frame

Directories

Path Synopsis
samples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL