/
ReaderWriterDDS.cpp
1465 lines (1312 loc) · 54.8 KB
/
ReaderWriterDDS.cpp
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
/**********************************************************************
*
* FILE: ReaderWriterdds.cpp
*
* DESCRIPTION: Class for reading a DDS file into an osg::Image.
*
* Example on reading a DDS file code can be found at:
* http://developer.nvidia.com/docs/IO/1327/ATT/
* ARB_texture_compression.pdf
* Author: Sebastien Domine, NVIDIA Corporation
*
* CREATED BY: Rune Schmidt Jensen, rsj@uni-dk
*
* HISTORY: Created 31.03.2003
* Modified 13.05.2004
* by George Tarantilis, gtaranti@nps.navy.mil
* Modified 22.05.2009
* Wojtek Lewandowski, lewandowski@ai.com.pl
*
* WARNING:
* Bit Masks in the WrtiteDDS are set for 8 bit components
* write with 4 or 16 bit components will
* probably produce corrupted file
* Wojtek Lewandowski 2009-05-22
*
**********************************************************************/
#include <osg/Texture>
#include <osg/Notify>
#include <osgDB/Registry>
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osgDB/fstream>
#include <iomanip>
#include <stdio.h>
#include <string.h>
// Macro similar to what's in FLT/TRP plugins (except it uses wide char under Windows if OSG_USE_UTF8_FILENAME)
#if defined(_WIN32)
#include <windows.h>
#include <osg/Config>
#include <osgDB/ConvertUTF>
#ifdef OSG_USE_UTF8_FILENAME
#define DELETEFILE(file) DeleteFileW(osgDB::convertUTF8toUTF16((file)).c_str())
#else
#define DELETEFILE(file) DeleteFileA((file))
#endif
#else // Unix
#include <stdio.h>
#define DELETEFILE(file) remove((file))
#endif
#if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE)
#define GL_RED 0x1903
#define GL_LUMINANCE4_ALPHA4 0x8043
#endif
#if defined(OSG_GL3_AVAILABLE)
#define GL_LUMINANCE4_ALPHA4 0x8043
#endif
// NOTICE ON WIN32:
// typedef DWORD unsigned long;
// sizeof(DWORD) = 4
typedef unsigned int UI32;
typedef int I32;
struct DDCOLORKEY
{
DDCOLORKEY():
dwColorSpaceLowValue(0),
dwColorSpaceHighValue(0) {}
UI32 dwColorSpaceLowValue;
UI32 dwColorSpaceHighValue;
};
struct DDPIXELFORMAT
{
DDPIXELFORMAT():
dwSize(0),
dwFlags(0),
dwFourCC(0),
dwRGBBitCount(0),
dwRBitMask(0),
dwGBitMask(0),
dwBBitMask(0),
dwRGBAlphaBitMask(0) {}
UI32 dwSize;
UI32 dwFlags;
UI32 dwFourCC;
union
{
UI32 dwRGBBitCount;
UI32 dwYUVBitCount;
UI32 dwZBufferBitDepth;
UI32 dwAlphaBitDepth;
UI32 dwLuminanceBitDepth;
};
union
{
UI32 dwRBitMask;
UI32 dwYBitMask;
};
union
{
UI32 dwGBitMask;
UI32 dwUBitMask;
};
union
{
UI32 dwBBitMask;
UI32 dwVBitMask;
};
union
{
UI32 dwRGBAlphaBitMask;
UI32 dwYUVAlphaBitMask;
UI32 dwRGBZBitMask;
UI32 dwYUVZBitMask;
};
};
struct DDSCAPS2
{
DDSCAPS2():
dwCaps(0),
dwCaps2(0),
dwCaps3(0),
dwCaps4(0) {}
UI32 dwCaps;
UI32 dwCaps2;
UI32 dwCaps3;
union
{
UI32 dwCaps4;
UI32 dwVolumeDepth;
};
};
struct DDSURFACEDESC2
{
DDSURFACEDESC2():
dwSize(0),
dwFlags(0),
dwHeight(0),
dwWidth(0),
lPitch(0),
dwBackBufferCount(0),
dwMipMapCount(0),
dwAlphaBitDepth(0),
dwReserved(0),
lpSurface(0),
dwTextureStage(0) {}
UI32 dwSize;
UI32 dwFlags;
UI32 dwHeight;
UI32 dwWidth;
union
{
I32 lPitch;
UI32 dwLinearSize;
};
union
{
UI32 dwBackBufferCount;
UI32 dwDepth;
};
union
{
UI32 dwMipMapCount;
UI32 dwRefreshRate;
};
UI32 dwAlphaBitDepth;
UI32 dwReserved;
UI32 lpSurface; //Fred Marmond: removed from pointer type to UI32 for 64bits compatibility. it is unused data
DDCOLORKEY ddckCKDestOverlay;
DDCOLORKEY ddckCKDestBlt;
DDCOLORKEY ddckCKSrcOverlay;
DDCOLORKEY ddckCKSrcBlt;
DDPIXELFORMAT ddpfPixelFormat;
DDSCAPS2 ddsCaps;
UI32 dwTextureStage;
};
//
// Structure of a DXT-1 compressed texture block
// see page "Opaque and 1-Bit Alpha Textures (Direct3D 9)" on http://msdn.microsoft.com
// url at time of writing http://msdn.microsoft.com/en-us/library/bb147243(v=VS.85).aspx
//
struct DXT1TexelsBlock
{
unsigned short color_0; // colors at their
unsigned short color_1; // extreme
unsigned int texels4x4; // interpolated colors (2 bits per texel)
};
//
// DDSURFACEDESC2 flags that mark the validity of the struct data
//
#define DDSD_CAPS 0x00000001l // default
#define DDSD_HEIGHT 0x00000002l // default
#define DDSD_WIDTH 0x00000004l // default
#define DDSD_PIXELFORMAT 0x00001000l // default
#define DDSD_PITCH 0x00000008l // For uncompressed formats
#define DDSD_MIPMAPCOUNT 0x00020000l
#define DDSD_LINEARSIZE 0x00080000l // For compressed formats
#define DDSD_DEPTH 0x00800000l // Volume Textures
//
// DDPIXELFORMAT flags
//
#define DDPF_ALPHAPIXELS 0x00000001l
#define DDPF_FOURCC 0x00000004l // Compressed formats
#define DDPF_RGB 0x00000040l // Uncompressed formats
#define DDPF_ALPHA 0x00000002l
#define DDPF_COMPRESSED 0x00000080l
#define DDPF_LUMINANCE 0x00020000l
#define DDPF_BUMPLUMINANCE 0x00040000l // L,U,V
#define DDPF_BUMPDUDV 0x00080000l // U,V
//
// DDSCAPS flags
//
#define DDSCAPS_TEXTURE 0x00001000l // default
#define DDSCAPS_COMPLEX 0x00000008l
#define DDSCAPS_MIPMAP 0x00400000l
#define DDSCAPS2_VOLUME 0x00200000l
#ifndef MAKEFOURCC
#define MAKEFOURCC(ch0, ch1, ch2, ch3) \
((UI32)(char)(ch0) | ((UI32)(char)(ch1) << 8) | \
((UI32)(char)(ch2) << 16) | ((UI32)(char)(ch3) << 24 ))
#endif //defined(MAKEFOURCC)
/*
* FOURCC codes for DX compressed-texture pixel formats
*/
#define FOURCC_DXT1 (MAKEFOURCC('D','X','T','1'))
#define FOURCC_DXT2 (MAKEFOURCC('D','X','T','2'))
#define FOURCC_DXT3 (MAKEFOURCC('D','X','T','3'))
#define FOURCC_DXT4 (MAKEFOURCC('D','X','T','4'))
#define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5'))
/*
* FOURCC codes for 3dc compressed-texture pixel formats
*/
#define FOURCC_ATI1 (MAKEFOURCC('A','T','I','1'))
#define FOURCC_ATI2 (MAKEFOURCC('A','T','I','2'))
/*
* FOURCC codes for DX10 files
*/
#define FOURCC_DX10 (MAKEFOURCC('D','X','1','0'))
typedef enum OSG_DXGI_FORMAT {
OSG_DXGI_FORMAT_UNKNOWN = 0,
OSG_DXGI_FORMAT_R32G32B32A32_TYPELESS = 1,
OSG_DXGI_FORMAT_R32G32B32A32_FLOAT = 2,
OSG_DXGI_FORMAT_R32G32B32A32_UINT = 3,
OSG_DXGI_FORMAT_R32G32B32A32_SINT = 4,
OSG_DXGI_FORMAT_R32G32B32_TYPELESS = 5,
OSG_DXGI_FORMAT_R32G32B32_FLOAT = 6,
OSG_DXGI_FORMAT_R32G32B32_UINT = 7,
OSG_DXGI_FORMAT_R32G32B32_SINT = 8,
OSG_DXGI_FORMAT_R16G16B16A16_TYPELESS = 9,
OSG_DXGI_FORMAT_R16G16B16A16_FLOAT = 10,
OSG_DXGI_FORMAT_R16G16B16A16_UNORM = 11,
OSG_DXGI_FORMAT_R16G16B16A16_UINT = 12,
OSG_DXGI_FORMAT_R16G16B16A16_SNORM = 13,
OSG_DXGI_FORMAT_R16G16B16A16_SINT = 14,
OSG_DXGI_FORMAT_R32G32_TYPELESS = 15,
OSG_DXGI_FORMAT_R32G32_FLOAT = 16,
OSG_DXGI_FORMAT_R32G32_UINT = 17,
OSG_DXGI_FORMAT_R32G32_SINT = 18,
OSG_DXGI_FORMAT_R32G8X24_TYPELESS = 19,
OSG_DXGI_FORMAT_D32_FLOAT_S8X24_UINT = 20,
OSG_DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS = 21,
OSG_DXGI_FORMAT_X32_TYPELESS_G8X24_UINT = 22,
OSG_DXGI_FORMAT_R10G10B10A2_TYPELESS = 23,
OSG_DXGI_FORMAT_R10G10B10A2_UNORM = 24,
OSG_DXGI_FORMAT_R10G10B10A2_UINT = 25,
OSG_DXGI_FORMAT_R11G11B10_FLOAT = 26,
OSG_DXGI_FORMAT_R8G8B8A8_TYPELESS = 27,
OSG_DXGI_FORMAT_R8G8B8A8_UNORM = 28,
OSG_DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29,
OSG_DXGI_FORMAT_R8G8B8A8_UINT = 30,
OSG_DXGI_FORMAT_R8G8B8A8_SNORM = 31,
OSG_DXGI_FORMAT_R8G8B8A8_SINT = 32,
OSG_DXGI_FORMAT_R16G16_TYPELESS = 33,
OSG_DXGI_FORMAT_R16G16_FLOAT = 34,
OSG_DXGI_FORMAT_R16G16_UNORM = 35,
OSG_DXGI_FORMAT_R16G16_UINT = 36,
OSG_DXGI_FORMAT_R16G16_SNORM = 37,
OSG_DXGI_FORMAT_R16G16_SINT = 38,
OSG_DXGI_FORMAT_R32_TYPELESS = 39,
OSG_DXGI_FORMAT_D32_FLOAT = 40,
OSG_DXGI_FORMAT_R32_FLOAT = 41,
OSG_DXGI_FORMAT_R32_UINT = 42,
OSG_DXGI_FORMAT_R32_SINT = 43,
OSG_DXGI_FORMAT_R24G8_TYPELESS = 44,
OSG_DXGI_FORMAT_D24_UNORM_S8_UINT = 45,
OSG_DXGI_FORMAT_R24_UNORM_X8_TYPELESS = 46,
OSG_DXGI_FORMAT_X24_TYPELESS_G8_UINT = 47,
OSG_DXGI_FORMAT_R8G8_TYPELESS = 48,
OSG_DXGI_FORMAT_R8G8_UNORM = 49,
OSG_DXGI_FORMAT_R8G8_UINT = 50,
OSG_DXGI_FORMAT_R8G8_SNORM = 51,
OSG_DXGI_FORMAT_R8G8_SINT = 52,
OSG_DXGI_FORMAT_R16_TYPELESS = 53,
OSG_DXGI_FORMAT_R16_FLOAT = 54,
OSG_DXGI_FORMAT_D16_UNORM = 55,
OSG_DXGI_FORMAT_R16_UNORM = 56,
OSG_DXGI_FORMAT_R16_UINT = 57,
OSG_DXGI_FORMAT_R16_SNORM = 58,
OSG_DXGI_FORMAT_R16_SINT = 59,
OSG_DXGI_FORMAT_R8_TYPELESS = 60,
OSG_DXGI_FORMAT_R8_UNORM = 61,
OSG_DXGI_FORMAT_R8_UINT = 62,
OSG_DXGI_FORMAT_R8_SNORM = 63,
OSG_DXGI_FORMAT_R8_SINT = 64,
OSG_DXGI_FORMAT_A8_UNORM = 65,
OSG_DXGI_FORMAT_R1_UNORM = 66,
OSG_DXGI_FORMAT_R9G9B9E5_SHAREDEXP = 67,
OSG_DXGI_FORMAT_R8G8_B8G8_UNORM = 68,
OSG_DXGI_FORMAT_G8R8_G8B8_UNORM = 69,
OSG_DXGI_FORMAT_BC1_TYPELESS = 70,
OSG_DXGI_FORMAT_BC1_UNORM = 71,
OSG_DXGI_FORMAT_BC1_UNORM_SRGB = 72,
OSG_DXGI_FORMAT_BC2_TYPELESS = 73,
OSG_DXGI_FORMAT_BC2_UNORM = 74,
OSG_DXGI_FORMAT_BC2_UNORM_SRGB = 75,
OSG_DXGI_FORMAT_BC3_TYPELESS = 76,
OSG_DXGI_FORMAT_BC3_UNORM = 77,
OSG_DXGI_FORMAT_BC3_UNORM_SRGB = 78,
OSG_DXGI_FORMAT_BC4_TYPELESS = 79,
OSG_DXGI_FORMAT_BC4_UNORM = 80,
OSG_DXGI_FORMAT_BC4_SNORM = 81,
OSG_DXGI_FORMAT_BC5_TYPELESS = 82,
OSG_DXGI_FORMAT_BC5_UNORM = 83,
OSG_DXGI_FORMAT_BC5_SNORM = 84,
OSG_DXGI_FORMAT_B5G6R5_UNORM = 85,
OSG_DXGI_FORMAT_B5G5R5A1_UNORM = 86,
OSG_DXGI_FORMAT_B8G8R8A8_UNORM = 87,
OSG_DXGI_FORMAT_B8G8R8X8_UNORM = 88,
OSG_DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM = 89,
OSG_DXGI_FORMAT_B8G8R8A8_TYPELESS = 90,
OSG_DXGI_FORMAT_B8G8R8A8_UNORM_SRGB = 91,
OSG_DXGI_FORMAT_B8G8R8X8_TYPELESS = 92,
OSG_DXGI_FORMAT_B8G8R8X8_UNORM_SRGB = 93,
OSG_DXGI_FORMAT_BC6H_TYPELESS = 94,
OSG_DXGI_FORMAT_BC6H_UF16 = 95,
OSG_DXGI_FORMAT_BC6H_SF16 = 96,
OSG_DXGI_FORMAT_BC7_TYPELESS = 97,
OSG_DXGI_FORMAT_BC7_UNORM = 98,
OSG_DXGI_FORMAT_BC7_UNORM_SRGB = 99,
OSG_DXGI_FORMAT_AYUV = 100,
OSG_DXGI_FORMAT_Y410 = 101,
OSG_DXGI_FORMAT_Y416 = 102,
OSG_DXGI_FORMAT_NV12 = 103,
OSG_DXGI_FORMAT_P010 = 104,
OSG_DXGI_FORMAT_P016 = 105,
OSG_DXGI_FORMAT_420_OPAQUE = 106,
OSG_DXGI_FORMAT_YUY2 = 107,
OSG_DXGI_FORMAT_Y210 = 108,
OSG_DXGI_FORMAT_Y216 = 109,
OSG_DXGI_FORMAT_NV11 = 110,
OSG_DXGI_FORMAT_AI44 = 111,
OSG_DXGI_FORMAT_IA44 = 112,
OSG_DXGI_FORMAT_P8 = 113,
OSG_DXGI_FORMAT_A8P8 = 114,
OSG_DXGI_FORMAT_B4G4R4A4_UNORM = 115,
OSG_DXGI_FORMAT_FORCE_UINT = 0xffffffffUL
} OSG_DXGI_FORMAT;
typedef enum OSG_D3D10_RESOURCE_DIMENSION {
OSG_D3D10_RESOURCE_DIMENSION_UNKNOWN = 0,
OSG_D3D10_RESOURCE_DIMENSION_BUFFER = 1,
OSG_D3D10_RESOURCE_DIMENSION_TEXTURE1D = 2,
OSG_D3D10_RESOURCE_DIMENSION_TEXTURE2D = 3,
OSG_D3D10_RESOURCE_DIMENSION_TEXTURE3D = 4
} OSG_D3D10_RESOURCE_DIMENSION;
typedef struct {
OSG_DXGI_FORMAT dxgiFormat;
OSG_D3D10_RESOURCE_DIMENSION resourceDimension;
UI32 miscFlag;
UI32 arraySize;
UI32 reserved;
} OSG_DDS_HEADER_DXT10;
static unsigned int ComputeImageSizeInBytes( int width, int height, int depth,
unsigned int pixelFormat, unsigned int pixelType,
int packing = 1, int slice_packing = 1, int image_packing = 1 )
{
if( width < 1 ) width = 1;
if( height < 1 ) height = 1;
if( depth < 1 ) depth = 1;
return osg::Image::computeImageSizeInBytes(width, height, depth, pixelFormat, pixelType, packing, slice_packing, image_packing);
}
osg::Image* ReadDDSFile(std::istream& _istream, bool flipDDSRead)
{
DDSURFACEDESC2 ddsd;
char filecode[4];
_istream.read(filecode, 4);
if (strncmp(filecode, "DDS ", 4) != 0) {
return NULL;
}
// Get the surface desc.
_istream.read((char*)(&ddsd), sizeof(ddsd));
osg::ref_ptr<osg::Image> osgImage = new osg::Image();
//Check valid structure sizes
if(ddsd.dwSize != 124 && ddsd.ddpfPixelFormat.dwSize != 32)
{
return NULL;
}
int depth = 1;
// Check for volume image
if( ddsd.dwDepth > 0 && (ddsd.dwFlags & DDSD_DEPTH))
{
depth = ddsd.dwDepth;
}
// Retreive image properties.
int s = ddsd.dwWidth;
int t = ddsd.dwHeight;
int r = depth;
unsigned int dataType = GL_UNSIGNED_BYTE;
unsigned int pixelFormat = 0;
unsigned int internalFormat = 0;
// Handle some esoteric formats
if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPDUDV)
{
OSG_WARN << "ReadDDSFile warning: DDPF_BUMPDUDV format is not supported" << std::endl;
return NULL;
// ddsd.ddpfPixelFormat.dwFlags =
// DDPF_LUMINANCE + DDPF_ALPHAPIXELS;
// // handle V8U8 as A8L8
// // handle V16U16 as A16L16
// // but Q8W8U8L8 as RGB?
// // A2W10U10V10 as RGBA (dwFlags == DDPF_BUMPDUDV + DDPF_ALPHAPIXELS)
}
if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPLUMINANCE)
{
OSG_WARN << "ReadDDSFile warning: DDPF_BUMPLUMINANCE format is not supported" << std::endl;
return NULL;
// ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
// // handle as RGB
// // L6V5U5 -- 655 is not supported data type in GL
// // X8L8V8U8 -- just as RGB
}
// Uncompressed formats will usually use DDPF_RGB to indicate an RGB format,
// while compressed formats will use DDPF_FOURCC with a four-character code.
bool usingAlpha = ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS;
int packing(1);
bool isDXTC(false);
// Uncompressed formats.
if(ddsd.ddpfPixelFormat.dwFlags & DDPF_RGB)
{
struct RGBFormat
{
const char* name;
UI32 bitCount;
UI32 rBitMask;
UI32 gBitMask;
UI32 bBitMask;
UI32 aBitMask;
unsigned int internalFormat;
unsigned int pixelFormat;
unsigned int dataType;
};
const unsigned int UNSUPPORTED = 0;
static const RGBFormat rgbFormats[] =
{
{ "R3G3B2" , 8, 0xe0, 0x1c, 0x03, 0x00,
GL_RGB , GL_RGB , GL_UNSIGNED_BYTE_3_3_2 },
{ "R5G6B5" , 16, 0xf800, 0x07e0, 0x001f, 0x0000,
GL_RGB , GL_RGB , GL_UNSIGNED_SHORT_5_6_5 },
{ "A1R5G5B5" , 16, 0x7c00, 0x03e0, 0x001f, 0x8000,
GL_RGBA, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV },
{ "X1R5G5B5" , 16, 0x7c00, 0x03e0, 0x001f, 0x0000,
GL_RGB , GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV },
{ "A4R4G4B4" , 16, 0x0f00, 0x00f0, 0x000f, 0xf000,
GL_RGBA, GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV },
{ "X4R4G4B4" , 16, 0x0f00, 0x00f0, 0x000f, 0x0000,
GL_RGB , GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV },
{ "A8R3G3B2" , 16, 0x00e0, 0x001c, 0x0003, 0xff00,
GL_RGBA, GL_BGRA, UNSUPPORTED },
{ "R8G8B8", 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000,
GL_RGB , GL_BGR , GL_UNSIGNED_BYTE },
{ "B8G8R8", 24, 0x0000ff, 0x00ff00, 0xff0000, 0x000000,
GL_RGB , GL_RGB , GL_UNSIGNED_BYTE },
{ "A8R8G8B8", 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000,
GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE },
{ "X8R8G8B8", 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000,
GL_RGB , GL_BGRA, GL_UNSIGNED_BYTE },
{ "A8B8G8R8", 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000,
GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE },
{ "X8B8G8R8", 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000,
GL_RGB , GL_RGBA, GL_UNSIGNED_BYTE },
{ "A2R10G10B10", 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000,
GL_RGBA, GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV },
{ "A2B10G10R10", 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000,
GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV },
{ "G16R16", 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000,
GL_RGB, UNSUPPORTED, GL_UNSIGNED_SHORT },
};
bool found = false;
for ( unsigned int i = 0; i < sizeof ( rgbFormats ) / sizeof ( RGBFormat ); i++ )
{
const RGBFormat& f = rgbFormats[ i ];
if ( ddsd.ddpfPixelFormat.dwRGBBitCount == f.bitCount &&
ddsd.ddpfPixelFormat.dwRBitMask == f.rBitMask &&
ddsd.ddpfPixelFormat.dwGBitMask == f.gBitMask &&
ddsd.ddpfPixelFormat.dwBBitMask == f.bBitMask &&
ddsd.ddpfPixelFormat.dwRGBAlphaBitMask == f.aBitMask )
{
if ( f.internalFormat != UNSUPPORTED &&
f.pixelFormat != UNSUPPORTED &&
f.dataType != UNSUPPORTED )
{
OSG_INFO << "ReadDDSFile info : format = " << f.name << std::endl;
internalFormat = f.internalFormat;
pixelFormat = f.pixelFormat;
dataType = f.dataType;
found = true;
break;
}
else
{
OSG_INFO << "ReadDDSFile info : " << f.name
<< " format is not supported" << std::endl;
return NULL;
}
}
}
if ( !found )
{
OSG_WARN << "ReadDDSFile warning: unhandled RGB pixel format in dds file, image not loaded" << std::endl;
OSG_INFO << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwRGBBitCount = "
<< ddsd.ddpfPixelFormat.dwRGBBitCount << std::endl;
OSG_INFO << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwRBitMask = 0x"
<< std::hex << std::setw(8) << std::setfill('0')
<< ddsd.ddpfPixelFormat.dwRBitMask << std::endl;
OSG_INFO << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwGBitMask = 0x"
<< std::hex << std::setw(8) << std::setfill('0')
<< ddsd.ddpfPixelFormat.dwGBitMask << std::endl;
OSG_INFO << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwBBitMask = 0x"
<< std::hex << std::setw(8) << std::setfill('0')
<< ddsd.ddpfPixelFormat.dwBBitMask << std::endl;
OSG_INFO << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0x"
<< std::hex << std::setw(8) << std::setfill('0')
<< ddsd.ddpfPixelFormat.dwRGBAlphaBitMask << std::dec << std::endl;
return NULL;
}
}
else if(ddsd.ddpfPixelFormat.dwFlags & DDPF_LUMINANCE)
{
internalFormat = usingAlpha ? GL_LUMINANCE_ALPHA : GL_LUMINANCE;
pixelFormat = usingAlpha ? GL_LUMINANCE_ALPHA : GL_LUMINANCE;
if ( usingAlpha && ddsd.ddpfPixelFormat.dwLuminanceBitDepth == 8 )
{
OSG_INFO << "ReadDDSFile info : format = L4A4" << std::endl;
pixelFormat = GL_LUMINANCE4_ALPHA4; // invalid enumerant?
}
else if ( usingAlpha && ddsd.ddpfPixelFormat.dwLuminanceBitDepth == 32 )
{
OSG_INFO << "ReadDDSFile info : format = L16A16" << std::endl;
dataType = GL_UNSIGNED_SHORT;
}
else if ( !usingAlpha && ddsd.ddpfPixelFormat.dwLuminanceBitDepth == 16 )
{
OSG_INFO << "ReadDDSFile info : format = L16" << std::endl;
dataType = GL_UNSIGNED_SHORT;
}
else if ( usingAlpha )
{
OSG_INFO << "ReadDDSFile info : format = L8A8" << std::endl;
}
else
{
OSG_INFO << "ReadDDSFile info : format = L8" << std::endl;
}
// else if ( ddsd.ddpfPixelFormat.dwLuminanceBitDepth == (usingAlpha ? 64 : 32) )
// {
// dataType = GL_UNSIGNED_INT;
// }
}
else if(ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHA)
{
OSG_INFO << "ReadDDSFile info : format = ALPHA" << std::endl;
internalFormat = GL_ALPHA;
pixelFormat = GL_ALPHA;
}
// Compressed formats
else if(ddsd.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
{
switch(ddsd.ddpfPixelFormat.dwFourCC)
{
case FOURCC_DXT1:
OSG_INFO << "ReadDDSFile info : format = DXT1, usingAlpha=" <<usingAlpha<< std::endl;
if (usingAlpha)
{
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
}
else
{
internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
pixelFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
}
packing = 2; // 4 bits/pixel. 4 px = 2 bytes
isDXTC = true;
break;
case FOURCC_DXT3:
OSG_INFO << "ReadDDSFile info : format = DXT3" << std::endl;
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
packing = 4; // 8 bits/pixel. 4 px = 4 bytes
isDXTC = true;
break;
case FOURCC_DXT5:
OSG_INFO << "ReadDDSFile info : format = DXT5" << std::endl;
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
packing = 4; // 8 bits/pixel. 4 px = 4 bytes
isDXTC = true;
break;
case FOURCC_ATI1:
OSG_INFO << "ReadDDSFile info : format = ATI1" << std::endl;
internalFormat = GL_COMPRESSED_RED_RGTC1_EXT;
pixelFormat = GL_COMPRESSED_RED_RGTC1_EXT;
break;
case FOURCC_ATI2:
OSG_INFO << "ReadDDSFile info : format = ATI2" << std::endl;
internalFormat = GL_COMPRESSED_RED_GREEN_RGTC2_EXT;
pixelFormat = GL_COMPRESSED_RED_GREEN_RGTC2_EXT;
break;
case 0x00000024: // A16B16G16R16
OSG_INFO << "ReadDDSFile info : format = A16B16G16R16" << std::endl;
internalFormat = GL_RGBA;
pixelFormat = GL_RGBA;
dataType = GL_UNSIGNED_SHORT;
break;
case 0x00000071: // A16B16G16R16F
OSG_INFO << "ReadDDSFile info : format = A16B16G16R16F" << std::endl;
internalFormat = GL_RGBA; // why no transparency?
pixelFormat = GL_RGBA;
dataType = GL_HALF_FLOAT;
break;
case 0x0000006E: // Q16W16V16U16
OSG_INFO << "ReadDDSFile info : format = Q16W16V16U16" << std::endl;
internalFormat = GL_RGBA;
pixelFormat = GL_RGBA;
dataType = GL_UNSIGNED_SHORT;
break;
case 0x00000070: // G16R16F
OSG_INFO << "ReadDDSFile info : G16R16F format is not supported"
<< std::endl;
return NULL;
// internalFormat = GL_RGB;
// pixelFormat = must be GL_RED and GL_GREEN
// dataType = GL_HALF_FLOAT;
break;
case 0x00000073: // G32R32F
OSG_INFO << "ReadDDSFile info : G32R32F format is not supported"
<< std::endl;
return NULL;
// internalFormat = GL_RGB;
// pixelFormat = must be GL_RED and GL_GREEN
// dataType = GL_FLOAT;
break;
case 0x00000072: // R32F
OSG_INFO << "ReadDDSFile info : format = R32F" << std::endl;
internalFormat = GL_RGB;
pixelFormat = GL_RED;
dataType = GL_FLOAT;
break;
case 0x0000006F: // R16F
OSG_INFO << "ReadDDSFile info : format = R16F" << std::endl;
internalFormat = GL_RGB;
pixelFormat = GL_RED;
dataType = GL_HALF_FLOAT;
break;
case 0x00000074: // A32B32G32R32F
OSG_INFO << "ReadDDSFile info : format = A32B32G32R32F" << std::endl;
internalFormat = GL_RGBA;
pixelFormat = GL_RGBA;
dataType = GL_FLOAT;
break;
case 0x00000075: // CxV8U8
OSG_INFO << "ReadDDSFile info : CxV8U8 format is not supported" << std::endl;
return NULL;
case FOURCC_DX10:
OSG_INFO << "ReadDDSFile info : format = DX10 file" << std::endl;
{
OSG_DDS_HEADER_DXT10 header10;
_istream.read((char*)(&header10), sizeof(header10));
switch (header10.dxgiFormat) {
case OSG_DXGI_FORMAT_R32G32B32A32_FLOAT:
internalFormat = GL_RGBA32F_ARB;
pixelFormat = GL_RGBA;
dataType = GL_FLOAT;
break;
case OSG_DXGI_FORMAT_R32G32B32A32_UINT:
internalFormat = GL_RGBA32UI_EXT;
pixelFormat = GL_RGBA;
dataType = GL_UNSIGNED_INT;
break;
case OSG_DXGI_FORMAT_R32G32B32A32_SINT:
internalFormat = GL_RGBA32I_EXT;
pixelFormat = GL_RGBA;
dataType = GL_INT;
break;
case OSG_DXGI_FORMAT_R32G32B32_FLOAT:
internalFormat = GL_RGB32F_ARB;
pixelFormat = GL_RGB;
dataType = GL_FLOAT;
break;
case OSG_DXGI_FORMAT_R32G32B32_UINT:
internalFormat = GL_RGB32UI_EXT;
pixelFormat = GL_RGB;
dataType = GL_UNSIGNED_INT;
break;
case OSG_DXGI_FORMAT_R32G32B32_SINT:
internalFormat = GL_RGB32I_EXT;
pixelFormat = GL_RGB;
dataType = GL_INT;
break;
case OSG_DXGI_FORMAT_R16G16B16A16_FLOAT:
internalFormat = GL_RGBA16F_ARB;
pixelFormat = GL_RGBA;
dataType = GL_HALF_FLOAT;
break;
case OSG_DXGI_FORMAT_R16G16B16A16_UNORM:
internalFormat = GL_RGBA16;
pixelFormat = GL_RGBA;
dataType = GL_UNSIGNED_SHORT;
break;
case OSG_DXGI_FORMAT_R16G16B16A16_UINT:
internalFormat = GL_RGBA16UI_EXT;
pixelFormat = GL_RGBA;
dataType = GL_UNSIGNED_SHORT;
break;
case OSG_DXGI_FORMAT_R16G16B16A16_SNORM:
internalFormat = GL_RGBA16_SNORM;
pixelFormat = GL_RGBA;
dataType = GL_SHORT;
break;
case OSG_DXGI_FORMAT_R16G16B16A16_SINT:
internalFormat = GL_RGBA16I_EXT;
pixelFormat = GL_RGBA;
dataType = GL_SHORT;
break;
case OSG_DXGI_FORMAT_R32G32_FLOAT:
internalFormat = GL_RG32F;
pixelFormat = GL_RG;
dataType = GL_FLOAT;
break;
case OSG_DXGI_FORMAT_R32G32_UINT:
internalFormat = GL_RG32UI;
pixelFormat = GL_RG;
dataType = GL_UNSIGNED_INT;
break;
case OSG_DXGI_FORMAT_R32G32_SINT:
internalFormat = GL_RG32I;
pixelFormat = GL_RG;
dataType = GL_INT;
break;
case OSG_DXGI_FORMAT_R16G16_FLOAT:
internalFormat = GL_RG16F;
pixelFormat = GL_RG;
dataType = GL_HALF_FLOAT;
break;
case OSG_DXGI_FORMAT_R16G16_UNORM:
internalFormat = GL_RG16;
pixelFormat = GL_RG;
dataType = GL_UNSIGNED_SHORT;
break;
case OSG_DXGI_FORMAT_R16G16_UINT:
internalFormat = GL_RG16UI;
pixelFormat = GL_RG;
dataType = GL_UNSIGNED_SHORT;
break;
case OSG_DXGI_FORMAT_R16G16_SNORM:
internalFormat = GL_RG16_SNORM;
pixelFormat = GL_RG;
dataType = GL_UNSIGNED_SHORT;
break;
case OSG_DXGI_FORMAT_R16G16_SINT:
internalFormat = GL_RG16I;
pixelFormat = GL_RG;
dataType = GL_SHORT;
break;
case OSG_DXGI_FORMAT_R32_FLOAT:
internalFormat = GL_R32F;
pixelFormat = GL_RED;
dataType = GL_FLOAT;
break;
case OSG_DXGI_FORMAT_R32_UINT:
internalFormat = GL_R32UI;
pixelFormat = GL_RED;
dataType = GL_UNSIGNED_INT;
break;
case OSG_DXGI_FORMAT_R32_SINT:
internalFormat = GL_R32I;
pixelFormat = GL_RED;
dataType = GL_INT;
break;
case OSG_DXGI_FORMAT_R8G8_UNORM:
internalFormat = GL_RG;
pixelFormat = GL_RG;
dataType = GL_UNSIGNED_BYTE;
break;
case OSG_DXGI_FORMAT_R8G8_UINT:
internalFormat = GL_RG8UI;
pixelFormat = GL_RG;
dataType = GL_UNSIGNED_BYTE;
break;
case OSG_DXGI_FORMAT_R8G8_SNORM:
internalFormat = GL_RG_SNORM;
pixelFormat = GL_RG;
dataType = GL_BYTE;
break;
case OSG_DXGI_FORMAT_R8G8_SINT:
internalFormat = GL_RG8I;
pixelFormat = GL_RG;
dataType = GL_BYTE;
break;
case OSG_DXGI_FORMAT_R16_FLOAT:
internalFormat = GL_R16F;
pixelFormat = GL_RED;
dataType = GL_HALF_FLOAT;
break;
case OSG_DXGI_FORMAT_R16_UNORM:
internalFormat = GL_RED;
pixelFormat = GL_RED;
dataType = GL_HALF_FLOAT;
break;
case OSG_DXGI_FORMAT_R16_UINT:
internalFormat = GL_R16UI;
pixelFormat = GL_RED;
dataType = GL_UNSIGNED_SHORT;
break;
case OSG_DXGI_FORMAT_R16_SNORM:
internalFormat = GL_RED_SNORM;
pixelFormat = GL_RED;
dataType = GL_SHORT;
break;
case OSG_DXGI_FORMAT_R16_SINT:
internalFormat = GL_R16I;
pixelFormat = GL_RED;
dataType = GL_SHORT;
break;
default:
OSG_WARN << "ReadDDSFile warning: unhandled DX10 pixel format 0x"
<< std::hex << std::setw(8) << std::setfill('0')
<< header10.dxgiFormat << std::dec
<< " in dds file, image not loaded." << std::endl;
return NULL;
}
}
break;
case MAKEFOURCC( 'U', 'Y', 'V', 'Y' ): // not supported in OSG
case MAKEFOURCC( 'U', 'Y', 'V', '2' ): // not supported in OSG
case MAKEFOURCC( 'R', 'G', 'B', 'G' ): // R8G8_B8G8 -- what is it?
case MAKEFOURCC( 'G', 'R', 'G', 'B' ): // G8R8_G8B8 -- what is it?
//break;
default:
OSG_WARN << "ReadDDSFile warning: unhandled FOURCC pixel format ("
<< (char)((ddsd.ddpfPixelFormat.dwFourCC & 0x000000ff))
<< (char)((ddsd.ddpfPixelFormat.dwFourCC & 0x0000ff00) >> 8)
<< (char)((ddsd.ddpfPixelFormat.dwFourCC & 0x00ff0000) >> 16)
<< (char)((ddsd.ddpfPixelFormat.dwFourCC & 0xff000000) >> 24)
<< " = 0x" << std::hex << std::setw(8) << std::setfill('0')
<< ddsd.ddpfPixelFormat.dwFourCC << std::dec
<< ") in dds file, image not loaded." << std::endl;
return NULL;
}
}
else
{
OSG_WARN << "ReadDDSFile warning: unhandled pixel format (ddsd.ddpfPixelFormat.dwFlags"
<< " = 0x" << std::hex << std::setw(8) << std::setfill('0')
<< ddsd.ddpfPixelFormat.dwFlags << std::dec
<< ") in dds file, image not loaded."<<std::endl;
return NULL;
}
unsigned int size = ComputeImageSizeInBytes( s, t, r, pixelFormat, dataType, packing );
// Take care of mipmaps if any.
unsigned int sizeWithMipmaps = size;
osg::Image::MipmapDataType mipmap_offsets;
if ( ddsd.dwMipMapCount>1 )
{
unsigned numMipmaps = osg::Image::computeNumberOfMipmapLevels( s, t, r );
if( numMipmaps > ddsd.dwMipMapCount ) numMipmaps = ddsd.dwMipMapCount;
// array starts at 1 level offset, 0 level skipped
mipmap_offsets.resize( numMipmaps - 1 );
int width = s;
int height = t;
int depth = r;
for( unsigned int k = 0; k < mipmap_offsets.size(); ++k )
{
mipmap_offsets[k] = sizeWithMipmaps;
width = osg::maximum( width >> 1, 1 );
height = osg::maximum( height >> 1, 1 );
depth = osg::maximum( depth >> 1, 1 );
sizeWithMipmaps +=
ComputeImageSizeInBytes( width, height, depth, pixelFormat, dataType, packing );
}
}
unsigned char* imageData = new unsigned char [sizeWithMipmaps];
if(!imageData)
{
OSG_WARN << "ReadDDSFile warning: imageData == NULL" << std::endl;
return NULL;
}
// Read pixels in two chunks. First main image, next mipmaps.
if ( !_istream.read( (char*)imageData, size ) )
{
delete [] imageData;
OSG_WARN << "ReadDDSFile warning: couldn't read imageData" << std::endl;
return NULL;
}
// If loading mipmaps in second chunk fails we may still use main image