forked from Nominom/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKtxFile.cs
More file actions
374 lines (320 loc) · 10.1 KB
/
Copy pathKtxFile.cs
File metadata and controls
374 lines (320 loc) · 10.1 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using static System.Text.Encoding;
namespace BCnEncoder.Shared.ImageFiles
{
/// <summary>
/// A representation of a ktx file.
/// This class handles loading and saving ktx files into streams.
/// The full spec can be found here: https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
/// </summary>
public class KtxFile
{
public KtxHeader header;
public List<KtxKeyValuePair> KeyValuePairs { get; } = new List<KtxKeyValuePair>();
public List<KtxMipmap> MipMaps { get; } = new List<KtxMipmap>();
public KtxFile() { }
public KtxFile(KtxHeader header)
{
this.header = header;
}
/// <summary>
/// Writes this ktx file into a stream.
/// </summary>
public void Write(Stream s)
{
if (MipMaps.Count < 1 || MipMaps[0].NumberOfFaces < 1)
{
throw new InvalidOperationException("The KTX structure should have at least 1 mipmap level and 1 Face before writing to file.");
}
using (var bw = new BinaryWriter(s, UTF8, true))
{
var bytesOfKeyValueData = (uint)KeyValuePairs.Sum(x => x.GetSizeWithPadding());
header.BytesOfKeyValueData = bytesOfKeyValueData;
header.NumberOfFaces = MipMaps[0].NumberOfFaces;
header.NumberOfMipmapLevels = (uint)MipMaps.Count;
header.NumberOfArrayElements = 0;
if (!header.VerifyHeader())
{
throw new InvalidOperationException("Please verify the header validity before writing to file.");
}
bw.WriteStruct(header);
foreach (var keyValuePair in KeyValuePairs)
{
KtxKeyValuePair.WriteKeyValuePair(bw, keyValuePair);
}
for (var mip = 0; mip < header.NumberOfMipmapLevels; mip++)
{
var imageSize = MipMaps[mip].SizeInBytes;
bw.Write(imageSize);
var isCubemap = header.NumberOfFaces == 6 && header.NumberOfArrayElements == 0;
for (var f = 0; f < header.NumberOfFaces; f++)
{
bw.Write(MipMaps[mip].Faces[f].Data);
var cubePadding = 0u;
if (isCubemap)
{
cubePadding = 3 - (imageSize + 3) % 4;
}
bw.AddPadding(cubePadding);
}
var mipPaddingBytes = 3 - (imageSize + 3) % 4;
bw.AddPadding(mipPaddingBytes);
}
}
}
/// <summary>
/// Loads a KTX file from a stream.
/// </summary>
public static KtxFile Load(Stream s)
{
using (var br = new BinaryReader(s, UTF8, true))
{
var header = br.ReadStruct<KtxHeader>();
if (header.NumberOfArrayElements > 0)
{
throw new NotSupportedException("KTX files with arrays are not supported.");
}
var ktx = new KtxFile(header);
var keyValuePairBytesRead = 0;
while (keyValuePairBytesRead < header.BytesOfKeyValueData)
{
var kvp = KtxKeyValuePair.ReadKeyValuePair(br, out var read);
keyValuePairBytesRead += read;
ktx.KeyValuePairs.Add(kvp);
}
var numberOfFaces = Math.Max(1, header.NumberOfFaces);
ktx.MipMaps.Capacity = (int)header.NumberOfMipmapLevels;
for (uint mipLevel = 0; mipLevel < header.NumberOfMipmapLevels; mipLevel++)
{
var imageSize = br.ReadUInt32();
var mipWidth = header.PixelWidth / (uint)Math.Pow(2, mipLevel);
var mipHeight = header.PixelHeight / (uint)Math.Pow(2, mipLevel);
ktx.MipMaps.Add(new KtxMipmap(imageSize, mipWidth, mipHeight, numberOfFaces));
var cubemap = header.NumberOfFaces > 1 && header.NumberOfArrayElements == 0;
for (uint face = 0; face < numberOfFaces; face++)
{
var faceData = br.ReadBytes((int)imageSize);
ktx.MipMaps[(int)mipLevel].Faces[(int)face] = new KtxMipFace(faceData, mipWidth, mipHeight);
if (cubemap)
{
var cubePadding = 0u;
cubePadding = 3 - (imageSize + 3) % 4;
br.SkipPadding(cubePadding);
}
}
var mipPaddingBytes = 3 - (imageSize + 3) % 4;
br.SkipPadding(mipPaddingBytes);
}
return ktx;
}
}
/// <summary>
/// Gets the total size of all mipmaps and faces.
/// </summary>
public ulong GetTotalSize()
{
ulong totalSize = 0;
for (var mipLevel = 0; mipLevel < header.NumberOfMipmapLevels; mipLevel++)
{
for (var face = 0; face < header.NumberOfFaces; face++)
{
var ktxface = MipMaps[mipLevel].Faces[face];
totalSize += ktxface.SizeInBytes;
}
}
return totalSize;
}
/// <summary>
/// Gets all texture data of the file in face-major order (face0_mip0 ... face0_mip1 ... face1_mip0 ...)
/// </summary>
public byte[] GetAllTextureDataFaceMajor()
{
var result = new byte[GetTotalSize()];
uint start = 0;
for (var face = 0; face < header.NumberOfFaces; face++)
{
for (var mipLevel = 0; mipLevel < header.NumberOfMipmapLevels; mipLevel++)
{
var ktxMipFace = MipMaps[mipLevel].Faces[face];
ktxMipFace.Data.CopyTo(result, (int)start);
start += ktxMipFace.SizeInBytes;
}
}
return result;
}
/// <summary>
/// Gets all texture data of the file in MipMap-major order (face0_mip0 ... face1_mip0 ... face0_mip1 ...)
/// </summary>
public byte[] GetAllTextureDataMipMajor()
{
var result = new byte[GetTotalSize()];
uint start = 0;
for (var mipLevel = 0; mipLevel < header.NumberOfMipmapLevels; mipLevel++)
{
for (var face = 0; face < header.NumberOfFaces; face++)
{
var ktxMipFace = MipMaps[mipLevel].Faces[face];
ktxMipFace.Data.CopyTo(result, (int)start);
start += ktxMipFace.SizeInBytes;
}
}
return result;
}
}
public class KtxKeyValuePair
{
public string Key { get; }
public byte[] Value { get; }
public KtxKeyValuePair(string key, byte[] value)
{
Key = key;
Value = value;
}
public uint GetSizeWithPadding()
{
var keySpanLength = UTF8.GetByteCount(Key);
var totalSize = (uint)(keySpanLength + 1 + Value.Length);
var paddingBytes = (int)(3 - (totalSize + 3) % 4);
return (uint)(totalSize + paddingBytes);
}
public static KtxKeyValuePair ReadKeyValuePair(BinaryReader br, out int bytesRead)
{
var totalSize = br.ReadUInt32();
Span<byte> keyValueBytes = stackalloc byte[(int)totalSize];
br.Read(keyValueBytes);
// Find the key's null terminator
int i;
for (i = 0; i < totalSize; i++)
{
if (keyValueBytes[i] == 0)
{
break;
}
if (i >= totalSize)
{
throw new InvalidDataException();
}
}
var keySize = i;
var key = UTF8.GetString(keyValueBytes.Slice(0, keySize));
var valueSize = (int)(totalSize - keySize - 1);
var valueBytes = keyValueBytes.Slice(i + 1, valueSize);
var value = new byte[valueSize];
valueBytes.CopyTo(value);
var paddingBytes = (int)(3 - (totalSize + 3) % 4);
br.SkipPadding(paddingBytes);
bytesRead = (int)(totalSize + paddingBytes + sizeof(uint));
return new KtxKeyValuePair(key, value);
}
public static uint WriteKeyValuePair(BinaryWriter bw, KtxKeyValuePair pair)
{
var keySpanLength = UTF8.GetByteCount(pair.Key);
Span<byte> keySpan = stackalloc byte[keySpanLength];
Span<byte> valueSpan = pair.Value;
var totalSize = (uint)(keySpan.Length + 1 + valueSpan.Length);
var paddingBytes = (int)(3 - (totalSize + 3) % 4);
bw.Write(totalSize);
bw.Write(keySpan);
bw.Write((byte)0);
bw.Write(valueSpan);
return (uint)(totalSize + paddingBytes);
}
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct KtxHeader
{
public fixed byte Identifier[12];
public uint Endianness;
public GlType GlType;
public uint GlTypeSize;
public GlFormat GlFormat;
public GlInternalFormat GlInternalFormat;
public GlFormat GlBaseInternalFormat;
public uint PixelWidth;
public uint PixelHeight;
public uint PixelDepth;
public uint NumberOfArrayElements;
public uint NumberOfFaces;
public uint NumberOfMipmapLevels;
public uint BytesOfKeyValueData;
public bool VerifyHeader()
{
Span<byte> id = stackalloc byte[] { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };
for (var i = 0; i < id.Length; i++)
{
if (Identifier[i] != id[i]) return false;
}
return true;
}
public static KtxHeader InitializeCompressed(int width, int height, GlInternalFormat internalFormat, GlFormat baseInternalFormat)
{
var header = new KtxHeader();
Span<byte> id = stackalloc byte[] { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };
for (var i = 0; i < id.Length; i++)
{
header.Identifier[i] = id[i];
}
header.Endianness = 0x04030201;
header.PixelWidth = (uint)width;
header.PixelHeight = (uint)height;
header.GlType = 0;
header.GlTypeSize = 1;
header.GlFormat = 0;
header.GlInternalFormat = internalFormat;
header.GlBaseInternalFormat = baseInternalFormat;
return header;
}
public static KtxHeader InitializeUncompressed(int width, int height, GlType type, GlFormat format, uint glTypeSize, GlInternalFormat internalFormat, GlFormat baseInternalFormat)
{
var header = new KtxHeader();
Span<byte> id = stackalloc byte[] { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };
for (var i = 0; i < id.Length; i++)
{
header.Identifier[i] = id[i];
}
header.Endianness = 0x04030201;
header.PixelWidth = (uint)width;
header.PixelHeight = (uint)height;
header.GlType = type;
header.GlTypeSize = glTypeSize;
header.GlFormat = format;
header.GlInternalFormat = internalFormat;
header.GlBaseInternalFormat = baseInternalFormat;
return header;
}
}
public class KtxMipFace
{
public uint Width { get; set; }
public uint Height { get; set; }
public uint SizeInBytes { get; }
public byte[] Data { get; }
public KtxMipFace(byte[] data, uint width, uint height)
{
Width = width;
Height = height;
SizeInBytes = (uint)data.Length;
Data = data;
}
}
public class KtxMipmap
{
public uint SizeInBytes { get; }
public uint Width { get; }
public uint Height { get; }
public uint NumberOfFaces { get; }
public KtxMipFace[] Faces { get; }
public KtxMipmap(uint sizeInBytes, uint width, uint height, uint numberOfFaces)
{
SizeInBytes = sizeInBytes;
Width = Math.Max(1, width);
Height = Math.Max(1, height);
NumberOfFaces = numberOfFaces;
Faces = new KtxMipFace[numberOfFaces];
}
}
}