forked from Nominom/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBcEncoder.cs
More file actions
521 lines (452 loc) · 17.4 KB
/
Copy pathBcEncoder.cs
File metadata and controls
521 lines (452 loc) · 17.4 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using BCnEncoder.Encoder.Bc7;
using BCnEncoder.Shared;
namespace BCnEncoder.Encoder
{
public class EncoderInputOptions
{
/// <summary>
/// If true, when encoding to a format that only includes a red channel,
/// use the pixel luminance instead of just the red channel. Default is false.
/// </summary>
public bool luminanceAsRed = false;
}
public class EncoderOutputOptions
{
/// <summary>
/// The compression format to use. Default is BC1.
/// </summary>
public CompressionFormat format = CompressionFormat.BC1;
/// <summary>
/// The quality of the compression. Use either fast or balanced for testing.
/// Fast can be used for near real-time encoding for most algorithms.
/// Use bestQuality when needed. Default is balanced.
/// </summary>
public CompressionQuality quality = CompressionQuality.Balanced;
/// <summary>
/// The output file format of the data. Either Ktx or Dds.
/// Default is Ktx.
/// </summary>
public OutputFileFormat fileFormat = OutputFileFormat.Ktx;
/// <summary>
/// The DDS file format doesn't seem to have a standard for indicating whether a BC1 texture
/// includes 1bit of alpha. This option will write DDPF_ALPHAPIXELS flag to the header
/// to indicate the presence of an alpha channel. Some programs read and write this flag,
/// but some programs don't like it and get confused. Your mileage may vary.
/// Default is false.
/// </summary>
public bool ddsBc1WriteAlphaFlag = false;
}
public class EncoderOptions {
/// <summary>
/// Whether the blocks should be encoded in parallel. This can be much faster than single-threaded encoding,
/// but is slow if multiple textures are being processed at the same time.
/// When a debugger is attached, the encoder defaults to single-threaded operation to ease debugging.
/// Default is true.
/// </summary>
public bool multiThreaded = true;
}
/// <summary>
/// Handles all encoding of images into compressed or uncompressed formats. For decoding, <see cref="Decoder.BcDecoder"/>
/// </summary>
public class BcEncoder
{
public EncoderInputOptions InputOptions { get; set; } = new EncoderInputOptions();
public EncoderOutputOptions OutputOptions { get; set; } = new EncoderOutputOptions();
public EncoderOptions Options { get; set; } = new EncoderOptions();
public BcEncoder() { }
public BcEncoder(CompressionFormat format)
{
OutputOptions.format = format;
}
private IBcBlockEncoder GetEncoder(CompressionFormat format)
{
switch (format)
{
case CompressionFormat.BC1:
return new Bc1BlockEncoder();
case CompressionFormat.BC1WithAlpha:
return new Bc1AlphaBlockEncoder();
case CompressionFormat.BC2:
return new Bc2BlockEncoder();
case CompressionFormat.BC3:
return new Bc3BlockEncoder();
case CompressionFormat.BC4:
return new Bc4BlockEncoder(InputOptions.luminanceAsRed);
case CompressionFormat.BC5:
return new Bc5BlockEncoder();
case CompressionFormat.BC7:
return new Bc7Encoder();
default:
return null;
}
}
/// <summary>
/// Encodes all mipmap levels into a ktx or a dds file and writes it to the output stream.
/// </summary>
public void Encode(byte[] inputImageRgba, int inputImageWidth, int inputImageHeight, Stream outputStream)
{
if (OutputOptions.fileFormat == OutputFileFormat.Ktx)
{
KtxFile output = EncodeToKtx(inputImageRgba, inputImageWidth, inputImageHeight);
output.Write(outputStream);
}
else if (OutputOptions.fileFormat == OutputFileFormat.Dds)
{
DdsFile output = EncodeToDds(inputImageRgba, inputImageWidth, inputImageHeight);
output.Write(outputStream);
}
}
/// <summary>
/// Encodes all mipmap levels into a Ktx file.
/// </summary>
public KtxFile EncodeToKtx(byte[] inputImageRgba, int inputImageWidth, int inputImageHeight)
{
KtxFile output;
IBcBlockEncoder compressedEncoder = null;
compressedEncoder = GetEncoder(OutputOptions.format);
if (compressedEncoder == null)
{
throw new NotSupportedException($"This format is not supported: {OutputOptions.format}");
}
output = new KtxFile(
KtxHeader.InitializeCompressed(inputImageWidth, inputImageHeight,
compressedEncoder.GetInternalFormat(),
compressedEncoder.GetBaseInternalFormat()));
uint numMipMaps = 1;
byte[] encoded = null;
if (OutputOptions.format.IsCompressedFormat())
{
var blocks = ImageToBlocks.ImageTo4X4(inputImageRgba, inputImageWidth, inputImageHeight, out int blocksWidth, out int blocksHeight);
encoded = compressedEncoder.Encode(blocks, blocksWidth, blocksHeight, OutputOptions.quality,
!Debugger.IsAttached && Options.multiThreaded);
}
output.MipMaps.Add(new KtxMipmap((uint)encoded.Length,
(uint)inputImageWidth,
(uint)inputImageHeight, 1));
output.MipMaps[0].Faces[0] = new KtxMipFace(encoded,
(uint)inputImageWidth,
(uint)inputImageHeight);
output.Header.NumberOfFaces = 1;
output.Header.NumberOfMipmapLevels = numMipMaps;
return output;
}
/// <summary>
/// Encodes Rgba image into a Dds file.
/// </summary>
public DdsFile EncodeToDds(byte[] inputImageRgba, int inputImageWidth, int inputImageHeight)
{
DdsFile output;
IBcBlockEncoder compressedEncoder = null;
compressedEncoder = GetEncoder(OutputOptions.format);
if (compressedEncoder == null)
{
throw new NotSupportedException($"This format is not supported: {OutputOptions.format}");
}
DdsHeaderDxt10 dxt10Header;
var ddsHeader = DdsHeader.InitializeCompressed(inputImageWidth, inputImageHeight,
compressedEncoder.GetDxgiFormat(), out dxt10Header);
output = new DdsFile(ddsHeader, dxt10Header);
if (OutputOptions.ddsBc1WriteAlphaFlag &&
OutputOptions.format == CompressionFormat.BC1WithAlpha)
{
output.Header.ddsPixelFormat.dwFlags |= PixelFormatFlags.DDPF_ALPHAPIXELS;
}
uint numMipMaps = 1;
var blocks = ImageToBlocks.ImageTo4X4(inputImageRgba, inputImageWidth, inputImageHeight, out int blocksWidth, out int blocksHeight);
var encoded = compressedEncoder.Encode(blocks, blocksWidth, blocksHeight, OutputOptions.quality,
!Debugger.IsAttached && Options.multiThreaded);
output.Faces.Add(new DdsFace((uint)inputImageWidth, (uint)inputImageHeight,
(uint)encoded.Length, (int)numMipMaps));
output.Faces[0].MipMaps[0] = new DdsMipMap(encoded,
(uint)inputImageWidth,
(uint)inputImageHeight);
output.Header.dwMipMapCount = numMipMaps;
if (numMipMaps > 1)
{
output.Header.dwCaps |= HeaderCaps.DDSCAPS_COMPLEX | HeaderCaps.DDSCAPS_MIPMAP;
}
return output;
}
/// <summary>
/// Encodes an RGBA byte buffer to raw compressed bytes
/// </summary>
public byte[] EncodeToRawBytes(byte[] inputImageRgba, int inputImageWidth, int inputImageHeight, out int outputImageWidth, out int outputImageHeight)
{
IBcBlockEncoder compressedEncoder = GetEncoder(OutputOptions.format);
if (compressedEncoder == null)
{
throw new NotSupportedException($"This format is not supported: {OutputOptions.format}");
}
var blocks = ImageToBlocks.ImageTo4X4(inputImageRgba, inputImageWidth, inputImageHeight, out int blocksWidth, out int blocksHeight);
var encoded = compressedEncoder.Encode(blocks, blocksWidth, blocksHeight, OutputOptions.quality,
!Debugger.IsAttached && Options.multiThreaded);
outputImageWidth = blocksWidth * 4;
outputImageHeight = blocksHeight * 4;
return encoded;
}
/// <summary>
/// Encodes a single mip level of the input image to a byte buffer.
/// </summary>
//public byte[] EncodeToRawBytes(Image<Rgba32> inputImage, int mipLevel, out int mipWidth, out int mipHeight)
//{
// if (mipLevel < 0)
// {
// throw new ArgumentException($"{nameof(mipLevel)} cannot be less than zero.");
// }
// IBcBlockEncoder compressedEncoder = null;
// IRawEncoder uncompressedEncoder = null;
// if (OutputOptions.format.IsCompressedFormat())
// {
// compressedEncoder = GetEncoder(OutputOptions.format);
// if (compressedEncoder == null)
// {
// throw new NotSupportedException($"This format is not supported: {OutputOptions.format}");
// }
// }
// else
// {
// uncompressedEncoder = GetRawEncoder(OutputOptions.format);
// }
// uint numMipMaps = (uint)OutputOptions.maxMipMapLevel;
// if (!OutputOptions.generateMipMaps)
// {
// numMipMaps = 1;
// }
// var mipChain = MipMapper.GenerateMipChain(inputImage, ref numMipMaps);
// if (mipLevel > numMipMaps - 1)
// {
// foreach (var image in mipChain)
// {
// image.Dispose();
// }
// throw new ArgumentException($"{nameof(mipLevel)} cannot be more than number of mipmaps");
// }
// byte[] encoded = null;
// if (OutputOptions.format.IsCompressedFormat())
// {
// var blocks = ImageToBlocks.ImageTo4X4(mipChain[mipLevel].Frames[0], out int blocksWidth, out int blocksHeight);
// encoded = compressedEncoder.Encode(blocks, blocksWidth, blocksHeight, OutputOptions.quality,
// !Debugger.IsAttached && Options.multiThreaded);
// }
// else
// {
// if (!mipChain[mipLevel].TryGetSinglePixelSpan(out var mipPixels)) {
// throw new Exception("Cannot get pixel span.");
// }
// encoded = uncompressedEncoder.Encode(mipPixels);
// }
// mipWidth = mipChain[mipLevel].Width;
// mipHeight = mipChain[mipLevel].Height;
// foreach (var image in mipChain)
// {
// image.Dispose();
// }
// return encoded;
//}
/// <summary>
/// Encodes all cubemap faces and mipmap levels into Ktx file and writes it to the output stream.
/// Order is +X, -X, +Y, -Y, +Z, -Z
/// </summary>
//public void EncodeCubeMap(Image<Rgba32> right, Image<Rgba32> left, Image<Rgba32> top, Image<Rgba32> down,
// Image<Rgba32> back, Image<Rgba32> front, Stream outputStream)
//{
// if (OutputOptions.fileFormat == OutputFileFormat.Ktx)
// {
// KtxFile output = EncodeCubeMapToKtx(right, left, top, down, back, front);
// output.Write(outputStream);
// }
// else if (OutputOptions.fileFormat == OutputFileFormat.Dds)
// {
// DdsFile output = EncodeCubeMapToDds(right, left, top, down, back, front);
// output.Write(outputStream);
// }
//}
/// <summary>
/// Encodes all cubemap faces and mipmap levels into a Ktx file.
/// Order is +X, -X, +Y, -Y, +Z, -Z. Back maps to positive Z and front to negative Z.
/// </summary>
//public KtxFile EncodeCubeMapToKtx(Image<Rgba32> right, Image<Rgba32> left, Image<Rgba32> top, Image<Rgba32> down,
// Image<Rgba32> back, Image<Rgba32> front)
//{
// KtxFile output;
// IBcBlockEncoder compressedEncoder = null;
// IRawEncoder uncompressedEncoder = null;
// if (right.Width != left.Width || right.Width != top.Width || right.Width != down.Width
// || right.Width != back.Width || right.Width != front.Width ||
// right.Height != left.Height || right.Height != top.Height || right.Height != down.Height
// || right.Height != back.Height || right.Height != front.Height)
// {
// throw new ArgumentException("All input images of a cubemap should be the same size.");
// }
// Image<Rgba32>[] faces = new[] { right, left, top, down, back, front };
// if (OutputOptions.format.IsCompressedFormat())
// {
// compressedEncoder = GetEncoder(OutputOptions.format);
// if (compressedEncoder == null)
// {
// throw new NotSupportedException($"This format is not supported: {OutputOptions.format}");
// }
// output = new KtxFile(
// KtxHeader.InitializeCompressed(right.Width, right.Height,
// compressedEncoder.GetInternalFormat(),
// compressedEncoder.GetBaseInternalFormat()));
// }
// else
// {
// uncompressedEncoder = GetRawEncoder(OutputOptions.format);
// output = new KtxFile(
// KtxHeader.InitializeUncompressed(right.Width, right.Height,
// uncompressedEncoder.GetGlType(),
// uncompressedEncoder.GetGlFormat(),
// uncompressedEncoder.GetGlTypeSize(),
// uncompressedEncoder.GetInternalFormat(),
// uncompressedEncoder.GetBaseInternalFormat()));
// }
// uint numMipMaps = (uint)OutputOptions.maxMipMapLevel;
// if (!OutputOptions.generateMipMaps)
// {
// numMipMaps = 1;
// }
// uint mipLength = MipMapper.CalculateMipChainLength(right.Width, right.Height, numMipMaps);
// for (uint i = 0; i < mipLength; i++)
// {
// output.MipMaps.Add(new KtxMipmap(0, 0, 0, (uint)faces.Length));
// }
// for (int f = 0; f < faces.Length; f++)
// {
// var mipChain = MipMapper.GenerateMipChain(faces[f], ref numMipMaps);
// for (int i = 0; i < numMipMaps; i++)
// {
// byte[] encoded = null;
// if (OutputOptions.format.IsCompressedFormat())
// {
// var blocks = ImageToBlocks.ImageTo4X4(mipChain[i].Frames[0], out int blocksWidth, out int blocksHeight);
// encoded = compressedEncoder.Encode(blocks, blocksWidth, blocksHeight, OutputOptions.quality,
// !Debugger.IsAttached && Options.multiThreaded);
// }
// else
// {
// if (!mipChain[i].TryGetSinglePixelSpan(out var mipPixels)) {
// throw new Exception("Cannot get pixel span.");
// }
// encoded = uncompressedEncoder.Encode(mipPixels);
// }
// if (f == 0)
// {
// output.MipMaps[i] = new KtxMipmap((uint)encoded.Length,
// (uint)mipChain[i].Width,
// (uint)mipChain[i].Height, (uint)faces.Length);
// }
// output.MipMaps[i].Faces[f] = new KtxMipFace(encoded,
// (uint)mipChain[i].Width,
// (uint)mipChain[i].Height);
// }
// foreach (var image in mipChain)
// {
// image.Dispose();
// }
// }
// output.Header.NumberOfFaces = (uint)faces.Length;
// output.Header.NumberOfMipmapLevels = mipLength;
// return output;
//}
/// <summary>
/// Encodes all cubemap faces and mipmap levels into a Dds file.
/// Order is +X, -X, +Y, -Y, +Z, -Z. Back maps to positive Z and front to negative Z.
/// </summary>
//public DdsFile EncodeCubeMapToDds(Image<Rgba32> right, Image<Rgba32> left, Image<Rgba32> top, Image<Rgba32> down,
// Image<Rgba32> back, Image<Rgba32> front)
//{
// DdsFile output;
// IBcBlockEncoder compressedEncoder = null;
// IRawEncoder uncompressedEncoder = null;
// if (right.Width != left.Width || right.Width != top.Width || right.Width != down.Width
// || right.Width != back.Width || right.Width != front.Width ||
// right.Height != left.Height || right.Height != top.Height || right.Height != down.Height
// || right.Height != back.Height || right.Height != front.Height)
// {
// throw new ArgumentException("All input images of a cubemap should be the same size.");
// }
// Image<Rgba32>[] faces = new[] { right, left, top, down, back, front };
// if (OutputOptions.format.IsCompressedFormat())
// {
// compressedEncoder = GetEncoder(OutputOptions.format);
// if (compressedEncoder == null)
// {
// throw new NotSupportedException($"This format is not supported: {OutputOptions.format}");
// }
// var (ddsHeader, dxt10Header) = DdsHeader.InitializeCompressed(right.Width, right.Height,
// compressedEncoder.GetDxgiFormat());
// output = new DdsFile(ddsHeader, dxt10Header);
// if (OutputOptions.ddsBc1WriteAlphaFlag &&
// OutputOptions.format == CompressionFormat.BC1WithAlpha)
// {
// output.Header.ddsPixelFormat.dwFlags |= PixelFormatFlags.DDPF_ALPHAPIXELS;
// }
// }
// else
// {
// uncompressedEncoder = GetRawEncoder(OutputOptions.format);
// var ddsHeader = DdsHeader.InitializeUncompressed(right.Width, right.Height,
// uncompressedEncoder.GetDxgiFormat());
// output = new DdsFile(ddsHeader);
// }
// uint numMipMaps = (uint)OutputOptions.maxMipMapLevel;
// if (!OutputOptions.generateMipMaps)
// {
// numMipMaps = 1;
// }
// for (int f = 0; f < faces.Length; f++)
// {
// var mipChain = MipMapper.GenerateMipChain(faces[f], ref numMipMaps);
// for (int mip = 0; mip < numMipMaps; mip++)
// {
// byte[] encoded = null;
// if (OutputOptions.format.IsCompressedFormat())
// {
// var blocks = ImageToBlocks.ImageTo4X4(mipChain[mip].Frames[0], out int blocksWidth, out int blocksHeight);
// encoded = compressedEncoder.Encode(blocks, blocksWidth, blocksHeight, OutputOptions.quality,
// !Debugger.IsAttached && Options.multiThreaded);
// }
// else
// {
// if (!mipChain[mip].TryGetSinglePixelSpan(out var mipPixels)) {
// throw new Exception("Cannot get pixel span.");
// }
// encoded = uncompressedEncoder.Encode(mipPixels);
// }
// if (mip == 0)
// {
// output.Faces.Add(new DdsFace((uint)mipChain[mip].Width, (uint)mipChain[mip].Height,
// (uint)encoded.Length, mipChain.Count));
// }
// output.Faces[f].MipMaps[mip] = new DdsMipMap(encoded,
// (uint)mipChain[mip].Width,
// (uint)mipChain[mip].Height);
// }
// foreach (var image in mipChain)
// {
// image.Dispose();
// }
// }
// output.Header.dwCaps |= HeaderCaps.DDSCAPS_COMPLEX;
// output.Header.dwMipMapCount = numMipMaps;
// if (numMipMaps > 1)
// {
// output.Header.dwCaps |= HeaderCaps.DDSCAPS_MIPMAP;
// }
// output.Header.dwCaps2 |= HeaderCaps2.DDSCAPS2_CUBEMAP |
// HeaderCaps2.DDSCAPS2_CUBEMAP_POSITIVEX |
// HeaderCaps2.DDSCAPS2_CUBEMAP_NEGATIVEX |
// HeaderCaps2.DDSCAPS2_CUBEMAP_POSITIVEY |
// HeaderCaps2.DDSCAPS2_CUBEMAP_NEGATIVEY |
// HeaderCaps2.DDSCAPS2_CUBEMAP_POSITIVEZ |
// HeaderCaps2.DDSCAPS2_CUBEMAP_NEGATIVEZ;
// return output;
//}
}
}