/
DDSLib.cs
2008 lines (1732 loc) · 77.6 KB
/
DDSLib.cs
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//for compatibility with the The Nvidia Photoshop DDS Plugin as it can't read correctly ABGR textures.
//coment this if you want to save color textures as ABGR.
#define COLOR_SAVE_TO_ARGB
#region File Description + License
//-----------------------------------------------------------------------------
// DDSFromFile.cs
// Read/Write dds files from/to files or from streams.
// Version 1.86
// Popescu Alexandru Cristian(kiki_karon@yahoo.com)
// Copyright (C) Popescu Alexandru Cristian All rights reserved.
//-----------------------------------------------------------------------------
//The MIT License
//Copyright (c) 2010 Popescu Alexandru Cristian
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
namespace AlphaSubmarines
{
/// <summary>
/// Read/Write dds files from/to files or from streams.
/// </summary>
public static class DDSLib
{
private const int DDSD_CAPS = 0x1; //Required in every .dds file.
private const int DDSD_HEIGHT = 0x2; //Required in every .dds file.
private const int DDSD_WIDTH = 0x4; //Required in every .dds file.
private const int DDSD_PITCH = 0x8; //Required when pitch is provided for an uncompressed texture.
private const int DDSD_PIXELFORMAT = 0x1000; //Required in every .dds file.
private const int DDSD_MIPMAPCOUNT = 0x20000; //Required in a mipmapped texture.
private const int DDSD_LINEARSIZE = 0x80000; //Required when pitch is provided for a compressed texture.
private const int DDSD_DEPTH = 0x800000; //Required in a depth texture.
private const int DDPF_ALPHAPIXELS = 0x1; //Texture contains alpha data; dwRGBAlphaBitMask contains valid data.
private const int DDPF_ALPHA = 0x2; //Used in some older DDS files for alpha channel only uncompressed data (dwRGBBitCount contains the alpha channel bitcount; dwABitMask contains valid data)
private const int DDPF_FOURCC = 0x4; //Texture contains compressed RGB data; dwFourCC contains valid data.
private const int DDPF_RGB = 0x40; //Texture contains uncompressed RGB data; dwRGBBitCount and the RGB masks (dwRBitMask, dwRBitMask, dwRBitMask) contain valid data.
private const int DDPF_YUV = 0x200; //Used in some older DDS files for YUV uncompressed data (dwRGBBitCount contains the YUV bit count; dwRBitMask contains the Y mask, dwGBitMask contains the U mask, dwBBitMask contains the V mask)
private const int DDPF_LUMINANCE = 0x2000; //Used in some older DDS files for single channel color uncompressed data (dwRGBBitCount contains the luminance channel bit count; dwRBitMask contains the channel mask). Can be combined with DDPF_ALPHAPIXELS for a two channel DDS file.
private const int DDPF_Q8W8V8U8 = 0x00080000; //Used by Microsoft tools when a Q8W8V8U8 is present, this is not a documeneted flag.
private const int DDSCAPS_COMPLEX = 0x8; //Optional; must be used on any file that contains more than one surface (a mipmap, a cubic environment map, or mipmapped volume texture).
private const int DDSCAPS_MIPMAP = 0x400000; //Optional; should be used for a mipmap.
private const int DDSCAPS_TEXTURE = 0x1000; //Required
private const int DDSCAPS2_CUBEMAP = 0x200; //Required for a cube map.
private const int DDSCAPS2_CUBEMAP_POSITIVEX = 0x400; //Required when these surfaces are stored in a cube map.
private const int DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800; //Required when these surfaces are stored in a cube map.
private const int DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000; //Required when these surfaces are stored in a cube map.
private const int DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000; //Required when these surfaces are stored in a cube map.
private const int DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000; //Required when these surfaces are stored in a cube map.
private const int DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000; //Required when these surfaces are stored in a cube map.
private const int DDSCAPS2_VOLUME = 0x200000; //Required for a volume texture.
private const uint DDS_MAGIC = 0x20534444; // "DDS "
//Compression formats.
[Flags()]
private enum FourCC : uint
{
D3DFMT_DXT1 = 0x31545844,
D3DFMT_DXT2 = 0x32545844,
D3DFMT_DXT3 = 0x33545844,
D3DFMT_DXT4 = 0x34545844,
D3DFMT_DXT5 = 0x35545844,
DX10 = 0x30315844,
DXGI_FORMAT_BC4_UNORM = 0x55344342,
DXGI_FORMAT_BC4_SNORM = 0x53344342,
DXGI_FORMAT_BC5_UNORM = 0x32495441,
DXGI_FORMAT_BC5_SNORM = 0x53354342,
//DXGI_FORMAT_R8G8_B8G8_UNORM
D3DFMT_R8G8_B8G8 = 0x47424752,
//DXGI_FORMAT_G8R8_G8B8_UNORM
D3DFMT_G8R8_G8B8 = 0x42475247,
//DXGI_FORMAT_R16G16B16A16_UNORM
D3DFMT_A16B16G16R16 = 36,
//DXGI_FORMAT_R16G16B16A16_SNORM
D3DFMT_Q16W16V16U16 = 110,
//DXGI_FORMAT_R16_FLOAT
D3DFMT_R16F = 111,
//DXGI_FORMAT_R16G16_FLOAT
D3DFMT_G16R16F = 112,
//DXGI_FORMAT_R16G16B16A16_FLOAT
D3DFMT_A16B16G16R16F = 113,
//DXGI_FORMAT_R32_FLOAT
D3DFMT_R32F = 114,
//DXGI_FORMAT_R32G32_FLOAT
D3DFMT_G32R32F = 115,
//DXGI_FORMAT_R32G32B32A32_FLOAT
D3DFMT_A32B32G32R32F = 116,
D3DFMT_UYVY = 0x59565955,
D3DFMT_YUY2 = 0x32595559,
D3DFMT_CxV8U8 = 117,
//This is set only by the nvidia exporter, it is not set by the dx texture tool
//,it is ignored by the dx texture tool but it returns the ability to be opened in photoshop so I decided to keep it.
D3DFMT_Q8W8V8U8 = 63,
}
// Indicates whether this texture is cube map.
private static bool IsCubemapTest(int ddsCaps1, int ddsCaps2)
{
return ((ddsCaps1 & DDSCAPS_COMPLEX) != 0) && ((ddsCaps2 & DDSCAPS2_CUBEMAP) != 0);
}
// Indicates whether this texture is volume map.
private static bool IsVolumeTextureTest(int ddsCaps1, int ddsCaps2)
{
//return ((ddsCaps1 & DDSCAPS_COMPLEX) != 0) && ((ddsCaps2 & DDSCAPS2_VOLUME) != 0);
return ((ddsCaps2 & DDSCAPS2_VOLUME) != 0);
}
//Test if the texture is using any compression.
private static bool IsCompressedTest(uint pfFlags)
{
return ((pfFlags & DDPF_FOURCC) != 0);
}
private static bool HasAlphaTest(uint pfFlags)
{
return ((pfFlags & DDPF_ALPHAPIXELS) != 0);
}
//We need the the mip size, we shift until we get there but the smallest mip must be at least of 1 pixel.
private static int MipMapSize(int map, int size)
{
for (int i = 0; i < map; i++)
size >>= 1;
if (size <= 0)
return 1;
return size;
}
//Surface formats that we can load from a dds
//I am not using the XNA SurfaceFormat as that one is missing a few formats.
private enum LoadSurfaceFormat
{
Unknown,
Dxt1,
Dxt3,
Dxt5,
R8G8B8,
B8G8R8,
Bgra5551,
Bgra4444,
Bgr565,
Alpha8,
X8R8G8B8,
A8R8G8B8,
A8B8G8R8,
X8B8G8R8,
RGB555,
R32F,
R16F,
A32B32G32R32F,
A16B16G16R16F,
Q8W8V8U8,
CxV8U8,
G16R16F,
G32R32F,
G16R16,
A2B10G10R10,
A16B16G16R16,
}
//Get pixel format from hader.
private static LoadSurfaceFormat GetLoadSurfaceFormat(uint pixelFlags, uint pixelFourCC, int bitCount, uint rBitMask, uint gBitMask, uint bBitMask, uint aBitMask, FourCC compressionFormat)
{
FourCC givenFourCC = (FourCC)pixelFourCC;
if (givenFourCC == FourCC.D3DFMT_A16B16G16R16)
{
return LoadSurfaceFormat.A16B16G16R16;
}
if (givenFourCC == FourCC.D3DFMT_G32R32F)
{
return LoadSurfaceFormat.G32R32F;
}
if (givenFourCC == FourCC.D3DFMT_G16R16F)
{
return LoadSurfaceFormat.G16R16F;
}
if (givenFourCC == FourCC.D3DFMT_Q8W8V8U8)
{
//This is true if the file was generated with the nvidia tools.
return LoadSurfaceFormat.Q8W8V8U8;
}
if (givenFourCC == FourCC.D3DFMT_CxV8U8)
{
return LoadSurfaceFormat.CxV8U8;
}
if (givenFourCC == FourCC.D3DFMT_A16B16G16R16F)
{
return LoadSurfaceFormat.A16B16G16R16F;
}
if (givenFourCC == FourCC.D3DFMT_A32B32G32R32F)
{
return LoadSurfaceFormat.A32B32G32R32F;
}
if (givenFourCC == FourCC.D3DFMT_R32F)
{
return LoadSurfaceFormat.R32F;
}
if (givenFourCC == FourCC.D3DFMT_R16F)
{
return LoadSurfaceFormat.R16F;
}
if ((pixelFlags & DDPF_FOURCC) != 0)
{
//The texture is compressed(Dxt1,Dxt3/Dxt2,Dxt5/Dxt4).
if (pixelFourCC == 0x31545844)
{
return LoadSurfaceFormat.Dxt1;
}
if (pixelFourCC == 0x33545844 || pixelFourCC == 0x32545844)
{
return LoadSurfaceFormat.Dxt3;
}
if (pixelFourCC == 0x35545844 || pixelFourCC == 0x34545844)
{
return LoadSurfaceFormat.Dxt5;
}
}
if ((pixelFlags & DDPF_RGB) != 0)
{
if (pixelFlags == 0x40 &&
bitCount == 0x00000010 &&
pixelFourCC == 0 &&
rBitMask == 0x00007c00 &&
gBitMask == 0x000003e0 &&
bBitMask == 0x0000001f &&
aBitMask == 0x00000000)
{
return LoadSurfaceFormat.RGB555;
}
if (pixelFlags == 0x41 &&
bitCount == 0x20 &&
pixelFourCC == 0 &&
rBitMask == 0xff0000 &&
gBitMask == 0xff00 &&
bBitMask == 0xff &&
aBitMask == 0xff000000)
{
return LoadSurfaceFormat.A8R8G8B8;
}
if (pixelFlags == 0x40 &&
bitCount == 0x20 &&
pixelFourCC == 0 &&
rBitMask == 0xff0000 &&
gBitMask == 0xff00 &&
bBitMask == 0xff &&
aBitMask == 0)
{
//DDS_FORMAT_X8R8G8B8
return LoadSurfaceFormat.X8R8G8B8;
}
if (pixelFlags == 0x41 &&
bitCount == 0x20 &&
pixelFourCC == 0 &&
rBitMask == 0xff &&
gBitMask == 0xff00 &&
bBitMask == 0xff0000 &&
aBitMask == 0xff000000)
{
//DDS_FORMAT_A8B8G8R8
return LoadSurfaceFormat.A8B8G8R8;
}
if (pixelFlags == 0x40 &&
bitCount == 0x20 &&
pixelFourCC == 0 &&
rBitMask == 0xff &&
gBitMask == 0xff00 &&
bBitMask == 0xff0000 &&
aBitMask == 0)
{
//DDS_FORMAT_X8B8G8R8
return LoadSurfaceFormat.X8B8G8R8;
}
if (pixelFlags == 0x41 &&
bitCount == 0x10 &&
pixelFourCC == 0 &&
rBitMask == 0x7c00 &&
gBitMask == 0x3e0 &&
bBitMask == 0x1f &&
aBitMask == 0x8000)
{
return LoadSurfaceFormat.Bgra5551;
}
if (pixelFlags == 0x41 &&
bitCount == 0x10 &&
pixelFourCC == 0 &&
rBitMask == 0xf00 &&
gBitMask == 240 &&
bBitMask == 15 &&
aBitMask == 0xf000)
{
return LoadSurfaceFormat.Bgra4444;
}
if (pixelFlags == 0x40 &&
bitCount == 0x18 &&
pixelFourCC == 0 &&
rBitMask == 0xff0000 &&
gBitMask == 0xff00 &&
bBitMask == 0xff &&
aBitMask == 0)
{
//DDS_FORMAT_R8G8B8
return LoadSurfaceFormat.R8G8B8;
}
if (pixelFlags == 0x40 &&
bitCount == 0x10 &&
pixelFourCC == 0 &&
rBitMask == 0xf800 &&
gBitMask == 0x7e0 &&
bBitMask == 0x1f &&
aBitMask == 0)
{
return LoadSurfaceFormat.Bgr565;
}
if (pixelFlags == 2 &&
bitCount == 8 &&
pixelFourCC == 0 &&
rBitMask == 0 &&
gBitMask == 0 &&
bBitMask == 0 &&
aBitMask == 255)
{
return LoadSurfaceFormat.Alpha8;
}
if (pixelFlags == 0x40 &&
bitCount == 32 &&
pixelFourCC == 0 &&
rBitMask == 0x0000ffff &&
gBitMask == 0xffff0000 &&
bBitMask == 0 &&
aBitMask == 0)
{
return LoadSurfaceFormat.G16R16;
}
if (pixelFlags == 0x41 &&
bitCount == 32 &&
pixelFourCC == 0 &&
rBitMask == 0x3ff00000 &&
gBitMask == 0x000ffc00 &&
bBitMask == 0x000003ff &&
aBitMask == 0xc0000000)
{
return LoadSurfaceFormat.A2B10G10R10;
}
}
//We consider the standard dx pixelFourCC + pixelFourCC == 63(nvidia tools generated dds)
if (pixelFlags == 0x00080000 &&
bitCount == 32 &&
(pixelFourCC == 0 || pixelFourCC == 63) &&
rBitMask == 0x000000ff &&
gBitMask == 0x0000ff00 &&
bBitMask == 0x00ff0000 &&
aBitMask == 0xff000000)
{
return LoadSurfaceFormat.Q8W8V8U8;
}
return LoadSurfaceFormat.Unknown;
}
//Get compression format.
private static FourCC GetCompressionFormat(uint pixelFlags, uint pixelFourCC)
{
if ((pixelFlags & DDPF_FOURCC) != 0)
return (FourCC)pixelFourCC;
else return 0;
}
//Get the size in bytes for a mip-map level.
private static int MipMapSizeInBytes(int map, int width, int height, bool isCompressed, FourCC compressionFormat, int depth)
{
width = MipMapSize(map, width);
height = MipMapSize(map, height);
//We hardcoded some compression formats as some flags are not set by all the tools for them,
//as a result for this formats we must hardcode the outcome.
if (compressionFormat == FourCC.D3DFMT_R32F)
{
return width * height * 4;
}
if (compressionFormat == FourCC.D3DFMT_R16F)
{
return width * height * 2;
}
if (compressionFormat == FourCC.D3DFMT_A32B32G32R32F)
{
return width * height * 16;
}
if (compressionFormat == FourCC.D3DFMT_A16B16G16R16F)
{
return width * height * 8;
}
if (compressionFormat == FourCC.D3DFMT_CxV8U8)
{
return width * height * 2;
}
if (compressionFormat == FourCC.D3DFMT_Q8W8V8U8)
{
return width * height * 4;
}
if (compressionFormat == FourCC.D3DFMT_G16R16F)
{
return width * height * 4;
}
if (compressionFormat == FourCC.D3DFMT_G32R32F)
{
return width * height * 8;
}
if (compressionFormat == FourCC.D3DFMT_A16B16G16R16)
{
return width * height * 8;
}
if (isCompressed)
{
int blockSize = (compressionFormat == FourCC.D3DFMT_DXT1 ? 8 : 16);
return ((width + 3) / 4) * ((height + 3) / 4) * blockSize;
}
else
{
return width * height * (depth / 8);
}
}
//Get the byte data from a mip-map level.
private static void GetMipMaps(int offsetInStream, int map, bool hasMipMaps,int width, int height, bool isCompressed, FourCC compressionFormat, int rgbBitCount, bool partOfCubeMap, BinaryReader reader,LoadSurfaceFormat loadSurfaceFormat, ref byte[] data, out int numBytes)
{
int seek = 128 + offsetInStream;
for (int i = 0; i < map; i++)
{
seek += MipMapSizeInBytes(i, width, height, isCompressed, compressionFormat, rgbBitCount);
}
reader.BaseStream.Seek(seek, SeekOrigin.Begin);
numBytes = MipMapSizeInBytes(map, width, height, isCompressed, compressionFormat, rgbBitCount);
if (isCompressed == false && rgbBitCount == 24)
{
numBytes += (numBytes / 3);
}
if (data == null || data.Length < numBytes)
{
data = new byte[numBytes];
}
if (isCompressed == false && loadSurfaceFormat == LoadSurfaceFormat.R8G8B8)
{
for (int i = 0; i < numBytes; i += 4)
{
data[i] = reader.ReadByte();
data[i + 1] = reader.ReadByte();
data[i + 2] = reader.ReadByte();
data[i + 3] = 255;
}
}
else
{
reader.Read(data, 0, numBytes);
}
if (loadSurfaceFormat == LoadSurfaceFormat.X8R8G8B8 || loadSurfaceFormat == LoadSurfaceFormat.X8B8G8R8)
{
for (int i = 0; i < numBytes; i += 4)
{
data[i + 3] = 255;
}
}
if (loadSurfaceFormat == LoadSurfaceFormat.A8R8G8B8 ||
loadSurfaceFormat == LoadSurfaceFormat.X8R8G8B8 ||
loadSurfaceFormat == LoadSurfaceFormat.R8G8B8)
{
int bytesPerPixel = (rgbBitCount == 32 || rgbBitCount == 24) ? 4 : 3;
byte g, b;
if (bytesPerPixel == 3)
{
for (int i = 0; i < numBytes - 2; i += 3)
{
g = data[i];
b = data[i + 2];
data[i] = b;
data[i + 2] = g;
}
}
else
{
for (int i = 0; i < numBytes - 3; i += 4)
{
g = data[i];
b = data[i + 2];
data[i] = b;
data[i + 2] = g;
}
}
}
}
//Xna only supporst mip-map on textures with full chains == last-mip is 1x1
private static bool CheckFullMipChain(int width,int height,int numMip)
{
int max = Math.Max(width, height);
int imaginariMipMax = 0;
while (max > 1)
{
max /= 2;
imaginariMipMax++;
}
if (imaginariMipMax <= numMip)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Open a dds from file.
/// (Supported formats : Dxt1,Dxt2,Dxt3,Dxt4,Dxt5,A8R8G8B8/Color,X8R8G8B8,R8G8B8,A4R4G4B4,A1R5G5B5,R5G6B5,A8,
/// FP32/Single,FP16/HalfSingle,FP32x4/Vector4,FP16x4/HalfVector4,CxV8U8/NormalizedByte2/CxVU,Q8VW8V8U8/NormalizedByte4/8888QWVU
/// ,HalfVector2/G16R16F/16.16fGR,Vector2/G32R32F,G16R16/RG32/1616GB,B8G8R8,X8B8G8R8,A8B8G8R8/Color,L8,A2B10G10R10/Rgba1010102,A16B16G16R16/Rgba64)
/// </summary>
/// <param name="fileName">File containing the data.</param>
/// <param name="device">Graphic device where you want the texture to be loaded.</param>
/// <param name="texture">The reference to the loaded texture.</param>
/// <param name="streamOffset">Offset in the stream to where the DDS is located.</param>
/// <param name="loadMipMap">If true it will load the mip-map chain for this texture.</param>
public static void DDSFromFile(string fileName, GraphicsDevice device, bool loadMipMap, out Texture2D texture)
{
Stream stream = File.OpenRead(fileName);
Texture tex;
InternalDDSFromStream(stream, device, 0, loadMipMap, out tex);
stream.Close();
texture = tex as Texture2D;
if (texture == null)
{
throw new Exception("The data in the stream contains a TextureCube not Texture2D");
}
}
/// <summary>
/// Open a dds from file.
/// (Supported formats : Dxt1,Dxt2,Dxt3,Dxt4,Dxt5,A8R8G8B8/Color,X8R8G8B8,R8G8B8,A4R4G4B4,A1R5G5B5,R5G6B5,A8,
/// FP32/Single,FP16/HalfSingle,FP32x4/Vector4,FP16x4/HalfVector4,CxV8U8/NormalizedByte2/CxVU,Q8VW8V8U8/NormalizedByte4/8888QWVU
/// ,HalfVector2/G16R16F/16.16fGR,Vector2/G32R32F,G16R16/RG32/1616GB,B8G8R8,X8B8G8R8,A8B8G8R8/Color,L8,A2B10G10R10/Rgba1010102,A16B16G16R16/Rgba64)
/// </summary>
/// <param name="fileName">File containing the data.</param>
/// <param name="device">Graphic device where you want the texture to be loaded.</param>
/// <param name="texture">The reference to the loaded texture.</param>
/// <param name="streamOffset">Offset in the stream to where the DDS is located.</param>
/// <param name="loadMipMap">If true it will load the mip-map chain for this texture.</param>
public static void DDSFromFile(string fileName, GraphicsDevice device, bool loadMipMap, out TextureCube texture)
{
Stream stream = File.OpenRead(fileName);
Texture tex;
InternalDDSFromStream(stream, device, 0, loadMipMap, out tex);
stream.Close();
texture = tex as TextureCube;
if (texture == null)
{
throw new Exception("The data in the stream contains a Texture2D not TextureCube");
}
}
/// <summary>
/// Open a dds from file.
/// (Supported formats : Dxt1,Dxt2,Dxt3,Dxt4,Dxt5,A8R8G8B8/Color,X8R8G8B8,R8G8B8,A4R4G4B4,A1R5G5B5,R5G6B5,A8,
/// FP32/Single,FP16/HalfSingle,FP32x4/Vector4,FP16x4/HalfVector4,CxV8U8/NormalizedByte2/CxVU,Q8VW8V8U8/NormalizedByte4/8888QWVU
/// ,HalfVector2/G16R16F/16.16fGR,Vector2/G32R32F,G16R16/RG32/1616GB,B8G8R8,X8B8G8R8,A8B8G8R8/Color,L8,A2B10G10R10/Rgba1010102,A16B16G16R16/Rgba64)
/// </summary>
/// <param name="fileName">File containing the data.</param>
/// <param name="device">Graphic device where you want the texture to be loaded.</param>
/// <param name="texture">The reference to the loaded texture.</param>
/// <param name="streamOffset">Offset in the stream to where the DDS is located.</param>
/// <param name="loadMipMap">If true it will load the mip-map chain for this texture.</param>
public static void DDSFromFile(string fileName, GraphicsDevice device, bool loadMipMap, out Texture3D texture)
{
Stream stream = File.OpenRead(fileName);
Texture tex;
InternalDDSFromStream(stream, device, 0, loadMipMap, out tex);
stream.Close();
texture = tex as Texture3D;
if (texture == null)
{
throw new Exception("The data in the stream contains a Texture2D not TextureCube");
}
}
/// <summary>
/// Open a dds from a stream.
/// (Supported formats : Dxt1,Dxt2,Dxt3,Dxt4,Dxt5,A8R8G8B8/Color,X8R8G8B8,R8G8B8,A4R4G4B4,A1R5G5B5,R5G6B5,A8,
/// FP32/Single,FP16/HalfSingle,FP32x4/Vector4,FP16x4/HalfVector4,CxV8U8/NormalizedByte2/CxVU,Q8VW8V8U8/NormalizedByte4/8888QWVU
/// ,HalfVector2/G16R16F/16.16fGR,Vector2/G32R32F,G16R16/RG32/1616GB,B8G8R8,X8B8G8R8,A8B8G8R8/Color,L8,A2B10G10R10/Rgba1010102,A16B16G16R16/Rgba64)
/// </summary>
/// <param name="stream">Stream containing the data.</param>
/// <param name="device">Graphic device where you want the texture to be loaded.</param>
/// <param name="texture">The reference to the loaded texture.</param>
/// <param name="streamOffset">Offset in the stream to where the DDS is located.</param>
/// <param name="loadMipMap">If true it will load the mip-map chain for this texture.</param>
public static void DDSFromStream(Stream stream, GraphicsDevice device, int streamOffset, bool loadMipMap, out Texture2D texture)
{
Texture tex;
InternalDDSFromStream(stream, device, streamOffset, loadMipMap, out tex);
texture = tex as Texture2D;
if (texture == null)
{
throw new Exception("The data in the stream contains a TextureCube not Texture2D");
}
}
/// <summary>
/// Open a dds from a stream.
/// (Supported formats : Dxt1,Dxt2,Dxt3,Dxt4,Dxt5,A8R8G8B8/Color,X8R8G8B8,R8G8B8,A4R4G4B4,A1R5G5B5,R5G6B5,A8,
/// FP32/Single,FP16/HalfSingle,FP32x4/Vector4,FP16x4/HalfVector4,CxV8U8/NormalizedByte2/CxVU,Q8VW8V8U8/NormalizedByte4/8888QWVU
/// ,HalfVector2/G16R16F/16.16fGR,Vector2/G32R32F,G16R16/RG32/1616GB,B8G8R8,X8B8G8R8,A8B8G8R8/Color,L8,A2B10G10R10/Rgba1010102,A16B16G16R16/Rgba64)
/// </summary>
/// <param name="stream">Stream containing the data.</param>
/// <param name="device">Graphic device where you want the texture to be loaded.</param>
/// <param name="texture">The reference to the loaded texture.</param>
/// <param name="streamOffset">Offset in the stream to where the DDS is located.</param>
/// <param name="loadMipMap">If true it will load the mip-map chain for this texture.</param>
public static void DDSFromStream(Stream stream, GraphicsDevice device, int streamOffset, bool loadMipMap, out TextureCube texture)
{
Texture tex;
InternalDDSFromStream(stream, device, streamOffset, loadMipMap, out tex);
texture = tex as TextureCube;
if (texture == null)
{
throw new Exception("The data in the stream contains a Texture2D not TextureCube");
}
}
/// <summary>
/// Open a dds from a stream.
/// (Supported formats : Dxt1,Dxt2,Dxt3,Dxt4,Dxt5,A8R8G8B8/Color,X8R8G8B8,R8G8B8,A4R4G4B4,A1R5G5B5,R5G6B5,A8,
/// FP32/Single,FP16/HalfSingle,FP32x4/Vector4,FP16x4/HalfVector4,CxV8U8/NormalizedByte2/CxVU,Q8VW8V8U8/NormalizedByte4/8888QWVU
/// ,HalfVector2/G16R16F/16.16fGR,Vector2/G32R32F,G16R16/RG32/1616GB,B8G8R8,X8B8G8R8,A8B8G8R8/Color,L8,A2B10G10R10/Rgba1010102,A16B16G16R16/Rgba64)
/// </summary>
/// <param name="stream">Stream containing the data.</param>
/// <param name="device">Graphic device where you want the texture to be loaded.</param>
/// <param name="texture">The reference to the loaded texture.</param>
/// <param name="streamOffset">Offset in the stream to where the DDS is located.</param>
/// <param name="loadMipMap">If true it will load the mip-map chain for this texture.</param>
public static void DDSFromStream(Stream stream, GraphicsDevice device, int streamOffset, bool loadMipMap, out Texture3D texture)
{
Texture tex;
InternalDDSFromStream(stream, device, streamOffset, loadMipMap, out tex);
texture = tex as Texture3D;
if (texture == null)
{
throw new Exception("The data in the stream contains a Texture2D not TextureCube");
}
}
#if WINDOWS
[ThreadStatic]
private static byte[] mipData;
#else
static DDSLib()
{
mipDataSet = new Dictionary<int, byte[]>();
}
private static Dictionary<int, byte[]> mipDataSet;
private static byte[] mipData
{
get
{
int localThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
if (mipDataSet.ContainsKey(localThreadID))
{
return mipDataSet[localThreadID];
}
else
{
lock (mipDataSet)
{
mipDataSet.Add(localThreadID, new byte[1]);
}
return mipDataSet[localThreadID];
}
}
set
{
lock (mipDataSet)
{
int localThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
mipDataSet[localThreadID] = value;
}
}
}
#endif
//try to evaluate the xna compatible surface for the present data
private static SurfaceFormat SurfaceFormatFromLoadFormat(LoadSurfaceFormat loadSurfaceFormat, FourCC compressionFormat, uint pixelFlags, int rgbBitCount)
{
if (loadSurfaceFormat == LoadSurfaceFormat.Unknown)
{
switch (compressionFormat)
{
case FourCC.D3DFMT_DXT1:
return SurfaceFormat.Dxt1;
case FourCC.D3DFMT_DXT3:
return SurfaceFormat.Dxt3;
case FourCC.D3DFMT_DXT5:
return SurfaceFormat.Dxt5;
case 0:
if (rgbBitCount == 8)
{
return SurfaceFormat.Alpha8;
}
if (rgbBitCount == 16)
{
if (HasAlphaTest(pixelFlags))
{
return SurfaceFormat.Bgr565;
}
else
{
return SurfaceFormat.Bgra4444;
}
}
if (rgbBitCount == 32 || rgbBitCount == 24)
{
return SurfaceFormat.Color;
}
break;
default:
throw new Exception("Unsuported format");
}
}
else
{
switch (loadSurfaceFormat)
{
case LoadSurfaceFormat.Alpha8:
return SurfaceFormat.Alpha8;
case LoadSurfaceFormat.Bgr565:
return SurfaceFormat.Bgr565;
case LoadSurfaceFormat.Bgra4444:
return SurfaceFormat.Bgra4444;
case LoadSurfaceFormat.Bgra5551:
return SurfaceFormat.Bgra5551;
case LoadSurfaceFormat.A8R8G8B8:
return SurfaceFormat.Color;
case LoadSurfaceFormat.Dxt1:
return SurfaceFormat.Dxt1;
case LoadSurfaceFormat.Dxt3:
return SurfaceFormat.Dxt3;
case LoadSurfaceFormat.Dxt5:
return SurfaceFormat.Dxt5;
//Updated at load time to X8R8B8B8
case LoadSurfaceFormat.R8G8B8:
return SurfaceFormat.Color;
case LoadSurfaceFormat.X8B8G8R8:
return SurfaceFormat.Color;
case LoadSurfaceFormat.X8R8G8B8:
return SurfaceFormat.Color;
case LoadSurfaceFormat.A8B8G8R8:
return SurfaceFormat.Color;
case LoadSurfaceFormat.R32F:
return SurfaceFormat.Single;
case LoadSurfaceFormat.A32B32G32R32F:
return SurfaceFormat.Vector4;
case LoadSurfaceFormat.G32R32F:
return SurfaceFormat.Vector2;
case LoadSurfaceFormat.R16F:
return SurfaceFormat.HalfSingle;
case LoadSurfaceFormat.G16R16F:
return SurfaceFormat.HalfVector2;
case LoadSurfaceFormat.A16B16G16R16F:
return SurfaceFormat.HalfVector4;
case LoadSurfaceFormat.CxV8U8:
return SurfaceFormat.NormalizedByte2;
case LoadSurfaceFormat.Q8W8V8U8:
return SurfaceFormat.NormalizedByte4;
case LoadSurfaceFormat.G16R16:
return SurfaceFormat.Rg32;
case LoadSurfaceFormat.A2B10G10R10:
return SurfaceFormat.Rgba1010102;
case LoadSurfaceFormat.A16B16G16R16:
return SurfaceFormat.Rgba64;
default:
throw new Exception(loadSurfaceFormat.ToString() + " is an unsuported format");
}
}
throw new Exception("Unsuported format");
}
//new cube-map texture
private static TextureCube GenerateNewCubeTexture(LoadSurfaceFormat loadSurfaceFormat, FourCC compressionFormat, GraphicsDevice device, int width, bool hasMipMaps, uint pixelFlags, int rgbBitCount)
{
SurfaceFormat surfaceFormat = SurfaceFormatFromLoadFormat(loadSurfaceFormat, compressionFormat, pixelFlags, rgbBitCount);
TextureCube tx = new TextureCube(device, width, 1, TextureUsage.AutoGenerateMipMap, surfaceFormat); //hasMipMaps
if (tx.Format != surfaceFormat)
{
throw new Exception("Can't generate a " +surfaceFormat.ToString()+" surface.");
}
return tx;
}
//new 2d-map texture
private static Texture2D GenerateNewTexture2D(LoadSurfaceFormat loadSurfaceFormat, FourCC compressionFormat, GraphicsDevice device, int width,int height, bool hasMipMaps, uint pixelFlags, int rgbBitCount)
{
SurfaceFormat surfaceFormat = SurfaceFormatFromLoadFormat(loadSurfaceFormat, compressionFormat, pixelFlags, rgbBitCount);
Texture2D tx = new Texture2D(device, width, height); // new Texture2D(device, width, height, hasMipMaps, surfaceFormat);
if (tx.Format != surfaceFormat)
{
throw new Exception("Can't generate a " + surfaceFormat.ToString() + " surface.");
}
return tx;
}
//new 3d-map texture
private static Texture3D GenerateNewTexture3D(LoadSurfaceFormat loadSurfaceFormat, FourCC compressionFormat, GraphicsDevice device, int width, int height,int depth, bool hasMipMaps, uint pixelFlags, int rgbBitCount)
{
SurfaceFormat surfaceFormat = SurfaceFormatFromLoadFormat(loadSurfaceFormat, compressionFormat, pixelFlags, rgbBitCount);
Texture3D tx = new Texture3D(device, width, height,depth, hasMipMaps, surfaceFormat);
if (tx.Format != surfaceFormat)
{
throw new Exception("Can't generate a " + surfaceFormat.ToString() + " surface.");
}
return tx;
}
//loads the data from a stream in to a texture object.
private static void InternalDDSFromStream(Stream stream,GraphicsDevice device,int streamOffset,bool loadMipMap,out Texture texture)
{
if (stream == null)
{
throw new Exception("Can't read from a null stream");
}
BinaryReader reader = new BinaryReader(stream);
if (streamOffset > reader.BaseStream.Length)
{
throw new Exception("The stream you offered is smaller then the offset you are proposing for it.");
}
reader.BaseStream.Seek(streamOffset, SeekOrigin.Begin);
//First element of a dds file is a "magic-number" a system to identify that the file is a dds if translated as asci chars the first 4 charachters should be 'DDS '
bool isDDS = reader.ReadUInt32() == DDS_MAGIC;
//bool isDDS = (reader.ReadChar() == 'D' && reader.ReadChar() == 'D' && reader.ReadChar() == 'S');
//empty char space.
//reader.ReadChar();
if (!isDDS)
{
throw new Exception("Can't open non DDS data.");
}
// size of the DDSURFACEDESC.
//reader.ReadInt32();
// validation flags.
//reader.ReadInt32();
reader.BaseStream.Position += 8;
//size in pixels for the texture.
int height = reader.ReadInt32();
int width = reader.ReadInt32();
//linear size.
//reader.ReadInt32();
reader.BaseStream.Position += 4;
//depth
int depth = reader.ReadInt32();
//number of mip-maps.
int numMips = reader.ReadInt32();
//alpha bit depth.
//reader.ReadInt32();
//empty space.
//reader.ReadInt32();
//pointer to associated surface.
//reader.ReadInt32();
//cubemap not present bitmaps colors.
//colorSpaceLowValue
//reader.ReadInt32();
//colorSpaceHighValue
//reader.ReadInt32();
//destBltColorSpaceLowValue
//reader.ReadInt32();
//destBltColorSpaceHighValue
//reader.ReadInt32();
//srcOverlayColorSpaceLowValue
//reader.ReadInt32();
//srcOverlayColorSpaceHighValue
//reader.ReadInt32();
//srcBltColorSpaceLowValue
//reader.ReadInt32();
//srcBltColorSpaceHighValue
//reader.ReadInt32();
// size of DDPIXELFORMAT structure
//reader.ReadInt32();
reader.BaseStream.Position += 4 * 12;
//pixel format flags
uint pixelFlags = reader.ReadUInt32();
// (FOURCC code)
uint pixelFourCC = reader.ReadUInt32();