-
Notifications
You must be signed in to change notification settings - Fork 2
/
reader.go
202 lines (172 loc) · 5.86 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package vtf
import (
"bytes"
"encoding/binary"
"errors"
"github.com/galaco/vtf/format"
"github.com/galaco/vtf/internal"
"io"
)
const (
vtfSignature = "VTF\x00"
)
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")
)
// Reader reads from a vtf stream
type Reader struct {
stream io.Reader
}
// ReadHeader reads the header of a texture only.
func (reader *Reader) ReadHeader() (*Header, error) {
buf := bytes.Buffer{}
_, err := buf.ReadFrom(reader.stream)
if err != nil {
return nil, err
}
// Header
return reader.parseHeader(buf.Bytes())
}
// 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 *Reader) Read() (*Vtf, error) {
buf := bytes.Buffer{}
_, err := buf.ReadFrom(reader.stream)
if err != nil {
return nil, err
}
// Header
header, err := reader.parseHeader(buf.Bytes())
if err != nil {
return nil, err
}
// Resources - in vtf 7.3+ only
resourceData, err := reader.parseOtherResourceData(header, buf.Bytes())
if err != nil {
return nil, err
}
// Low resolution preview texture
lowResImage, err := reader.readLowResolutionMipmap(header, buf.Bytes())
if err != nil {
return nil, err
}
// Mipmaps
highResImage, err := reader.readMipmaps(header, buf.Bytes())
if err != nil {
return nil, err
}
return &Vtf{
header: *header,
resources: resourceData,
lowResolutionImageData: lowResImage,
highResolutionImageData: highResImage,
}, nil
}
// parseHeader reads vtf Header.
func (reader *Reader) parseHeader(buffer []byte) (*Header, error) {
// We know Header is 96 bytes maximum
// Note it *may* not be someday...
headerBytes := make([]byte, 96)
// Read bytes equal to Header size
byteReader := bytes.NewReader(buffer)
sectionReader := io.NewSectionReader(byteReader, 0, int64(len(headerBytes)))
_, err := sectionReader.Read(headerBytes)
if err != nil {
return nil, err
}
// Set Header data to read bytes
header := Header{}
err = binary.Read(bytes.NewBuffer(headerBytes[:]), binary.LittleEndian, &header)
if err != nil {
return nil, err
}
if string(header.Signature[:4]) != vtfSignature {
return nil, ErrorVtfSignatureMismatch
}
return &header, nil
}
// parseOtherResourceData reads resource data for 7.3+ images
func (reader *Reader) parseOtherResourceData(header *Header, buffer []byte) ([]byte, error) {
// validate Header version
if (header.Version[0]*10+header.Version[1] < 73) || header.NumResource == 0 {
header.Depth = 0
header.NumResource = 0
return []byte{}, nil
}
return []byte{}, nil
}
// readLowResolutionMipmap reads the low resolution texture information
// This is normally what you see previewed in Hammer texture browser.
// The largest axis should always be 16 wide/tall. The smallest can be any value,
// but is padded out to divisible by 4 for Dxt1 compressionn reasons
func (reader *Reader) readLowResolutionMipmap(header *Header, buffer []byte) ([]uint8, error) {
bufferSize := internal.ComputeSizeOfMipmapData(
int(header.LowResImageWidth),
int(header.LowResImageHeight),
format.Dxt1)
imgBuffer := make([]byte, bufferSize)
byteReader := bytes.NewReader(buffer[int(header.HeaderSize) : int(header.HeaderSize)+bufferSize])
sectionReader := io.NewSectionReader(byteReader, 0, int64(bufferSize))
_, err := sectionReader.Read(imgBuffer)
if err != nil {
return nil, err
}
return imgBuffer, nil
}
// readMipmaps reads all mipmaps
// Returned format is a bit odd, but is just a set of flat arrays containing arrays:
// mipmap[frame[face[slice[RGBA]]]
func (reader *Reader) readMipmaps(header *Header, buffer []byte) ([][][][][]uint8, error) {
if header.Depth > 1 {
return [][][][][]uint8{}, ErrorTextureDepthNotSupported
}
depth := header.Depth
// Occurs in texture versions before 7.2
if depth == 0 {
depth = 1
}
// Only support 1 ZSlice. No known Source game can use > 1 zslices
numZSlice := uint16(1)
bufferEnd := len(buffer)
storedFormat := format.Format(header.HighResImageFormat)
mipmapSizes := internal.ComputeMipmapSizes(int(header.MipmapCount), int(header.Width), int(header.Height))
// Iterate mipmap; smallest to largest
mipMaps := make([][][][][]uint8, header.MipmapCount)
for mipmapIdx := int8(header.MipmapCount - 1); mipmapIdx >= int8(0); mipmapIdx-- {
// Frame by frame; first to last
frames := make([][][][]uint8, header.Frames)
for frameIdx := uint16(0); frameIdx < header.Frames; frameIdx++ {
faces := make([][][]uint8, 1)
// Face by face; first to last
// @TODO is this correct to use depth? How to know how many faces there are
for faceIdx := uint16(0); faceIdx < depth; faceIdx++ {
zSlices := make([][]uint8, 1)
// Z Slice by Z Slice; first to last
// @TODO wtf is a z slice, and how do we know how many there are
for sliceIdx := uint16(0); sliceIdx < numZSlice; sliceIdx++ {
bufferSize := internal.ComputeSizeOfMipmapData(
mipmapSizes[mipmapIdx][0],
mipmapSizes[mipmapIdx][1],
storedFormat)
if len(buffer) < bufferEnd-bufferSize {
return mipMaps, ErrorMipmapSizeMismatch
}
img := buffer[bufferEnd-bufferSize : bufferEnd]
bufferEnd -= bufferSize
zSlices[sliceIdx] = img
}
faces[faceIdx] = zSlices
}
frames[frameIdx] = faces
}
mipMaps[mipmapIdx] = frames
// Ensure that we maintain aspect ratio when scaling up mipmaps
}
return mipMaps, nil
}