-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnv_dds.cpp
1218 lines (1012 loc) · 38.6 KB
/
nv_dds.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
// This software contains source code provided by NVIDIA Corporation.
// License: http://developer.download.nvidia.com/licenses/general_license.txt
///////////////////////////////////////////////////////////////////////////////
//
// Description:
//
// Loads DDS images (DXTC1, DXTC3, DXTC5, RGB (888, 888X), and RGBA (8888) are
// supported) for use in OpenGL. Image is flipped when its loaded as DX images
// are stored with different coordinate system. If file has mipmaps and/or
// cubemaps then these are loaded as well. Volume textures can be loaded as
// well but they must be uncompressed.
//
// When multiple textures are loaded (i.e a volume or cubemap texture),
// additional faces can be accessed using the array operator.
//
// The mipmaps for each face are also stored in a list and can be accessed like
// so: image.get_mipmap() (which accesses the first mipmap of the first
// image). To get the number of mipmaps call the get_num_mipmaps function for
// a given texture.
//
// Call the is_volume() or is_cubemap() function to check that a loaded image
// is a volume or cubemap texture respectively. If a volume texture is loaded
// then the get_depth() function should return a number greater than 1.
// Mipmapped volume textures and DXTC compressed volume textures are supported.
//
///////////////////////////////////////////////////////////////////////////////
//
// Update: 9/15/2003
//
// Added functions to create new image from a buffer of pixels. Added function
// to save current image to disk.
//
// Update: 6/11/2002
//
// Added some convenience functions to handle uploading textures to OpenGL. The
// following functions have been added:
//
// bool upload_texture1D();
// bool upload_texture2D(unsigned int imageIndex = 0, GLenum target = GL_TEXTURE_2D);
// bool upload_textureRectangle();
// bool upload_texture3D();
// bool upload_textureCubemap();
//
// See function implementation below for instructions/comments on using each
// function.
//
// The open function has also been updated to take an optional second parameter
// specifying whether the image should be flipped on load. This defaults to
// true.
//
///////////////////////////////////////////////////////////////////////////////
// Sample usage
///////////////////////////////////////////////////////////////////////////////
//
// Loading a compressed texture:
//
// CDDSImage image;
// GLuint texobj;
//
// image.load("compressed.dds");
//
// glGenTextures(1, &texobj);
// glEnable(GL_TEXTURE_2D);
// glBindTexture(GL_TEXTURE_2D, texobj);
//
// glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, image.get_format(),
// image.get_width(), image.get_height(), 0, image.get_size(),
// image);
//
// for (int i = 0; i < image.get_num_mipmaps(); i++)
// {
// CSurface mipmap = image.get_mipmap(i);
//
// glCompressedTexImage2DARB(GL_TEXTURE_2D, i+1, image.get_format(),
// mipmap.get_width(), mipmap.get_height(), 0, mipmap.get_size(),
// mipmap);
// }
///////////////////////////////////////////////////////////////////////////////
//
// Loading an uncompressed texture:
//
// CDDSImage image;
// GLuint texobj;
//
// image.load("uncompressed.dds");
//
// glGenTextures(1, &texobj);
// glEnable(GL_TEXTURE_2D);
// glBindTexture(GL_TEXTURE_2D, texobj);
//
// glTexImage2D(GL_TEXTURE_2D, 0, image.get_components(), image.get_width(),
// image.get_height(), 0, image.get_format(), GL_UNSIGNED_BYTE, image);
//
// for (int i = 0; i < image.get_num_mipmaps(); i++)
// {
// glTexImage2D(GL_TEXTURE_2D, i+1, image.get_components(),
// image.get_mipmap(i).get_width(), image.get_mipmap(i).get_height(),
// 0, image.get_format(), GL_UNSIGNED_BYTE, image.get_mipmap(i));
// }
//
///////////////////////////////////////////////////////////////////////////////
//
// Loading an uncompressed cubemap texture:
//
// CDDSImage image;
// GLuint texobj;
// GLenum target;
//
// image.load("cubemap.dds");
//
// glGenTextures(1, &texobj);
// glEnable(GL_TEXTURE_CUBE_MAP);
// glBindTexture(GL_TEXTURE_CUBE_MAP, texobj);
//
// for (int n = 0; n < 6; n++)
// {
// target = GL_TEXTURE_CUBE_MAP_POSITIVE_X+n;
//
// glTexImage2D(target, 0, image.get_components(), image[n].get_width(),
// image[n].get_height(), 0, image.get_format(), GL_UNSIGNED_BYTE,
// image[n]);
//
// for (int i = 0; i < image[n].get_num_mipmaps(); i++)
// {
// glTexImage2D(target, i+1, image.get_components(),
// image[n].get_mipmap(i).get_width(),
// image[n].get_mipmap(i).get_height(), 0,
// image.get_format(), GL_UNSIGNED_BYTE, image[n].get_mipmap(i));
// }
// }
//
///////////////////////////////////////////////////////////////////////////////
//
// Loading a volume texture:
//
// CDDSImage image;
// GLuint texobj;
//
// image.load("volume.dds");
//
// glGenTextures(1, &texobj);
// glEnable(GL_TEXTURE_3D);
// glBindTexture(GL_TEXTURE_3D, texobj);
//
// PFNGLTEXIMAGE3DPROC glTexImage3D;
// glTexImage3D(GL_TEXTURE_3D, 0, image.get_components(), image.get_width(),
// image.get_height(), image.get_depth(), 0, image.get_format(),
// GL_UNSIGNED_BYTE, image);
//
// for (int i = 0; i < image.get_num_mipmaps(); i++)
// {
// glTexImage3D(GL_TEXTURE_3D, i+1, image.get_components(),
// image[0].get_mipmap(i).get_width(),
// image[0].get_mipmap(i).get_height(),
// image[0].get_mipmap(i).get_depth(), 0, image.get_format(),
// GL_UNSIGNED_BYTE, image[0].get_mipmap(i));
// }
#include "nv_dds.h"
#include <cstring>
#include <cassert>
#include <fstream>
#include <stdexcept>
using namespace std;
using namespace nv_dds;
#define GL_BGR_EXT 0x80E0
#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
///////////////////////////////////////////////////////////////////////////////
// CDDSImage private functions
namespace {
// surface description flags
const uint32_t DDSF_CAPS = 0x00000001;
const uint32_t DDSF_HEIGHT = 0x00000002;
const uint32_t DDSF_WIDTH = 0x00000004;
const uint32_t DDSF_PITCH = 0x00000008;
const uint32_t DDSF_PIXELFORMAT = 0x00001000;
const uint32_t DDSF_MIPMAPCOUNT = 0x00020000;
const uint32_t DDSF_LINEARSIZE = 0x00080000;
const uint32_t DDSF_DEPTH = 0x00800000;
// pixel format flags
const uint32_t DDSF_ALPHAPIXELS = 0x00000001;
const uint32_t DDSF_FOURCC = 0x00000004;
const uint32_t DDSF_RGB = 0x00000040;
const uint32_t DDSF_RGBA = 0x00000041;
// dwCaps1 flags
const uint32_t DDSF_COMPLEX = 0x00000008;
const uint32_t DDSF_TEXTURE = 0x00001000;
const uint32_t DDSF_MIPMAP = 0x00400000;
// dwCaps2 flags
const uint32_t DDSF_CUBEMAP = 0x00000200;
const uint32_t DDSF_CUBEMAP_POSITIVEX = 0x00000400;
const uint32_t DDSF_CUBEMAP_NEGATIVEX = 0x00000800;
const uint32_t DDSF_CUBEMAP_POSITIVEY = 0x00001000;
const uint32_t DDSF_CUBEMAP_NEGATIVEY = 0x00002000;
const uint32_t DDSF_CUBEMAP_POSITIVEZ = 0x00004000;
const uint32_t DDSF_CUBEMAP_NEGATIVEZ = 0x00008000;
const uint32_t DDSF_CUBEMAP_ALL_FACES = 0x0000FC00;
const uint32_t DDSF_VOLUME = 0x00200000;
// compressed texture types
const uint32_t FOURCC_DXT1 = 0x31545844; //(MAKEFOURCC('D','X','T','1'))
const uint32_t FOURCC_DXT3 = 0x33545844; //(MAKEFOURCC('D','X','T','3'))
const uint32_t FOURCC_DXT5 = 0x35545844; //(MAKEFOURCC('D','X','T','5'))
struct DDS_PIXELFORMAT {
uint32_t dwSize;
uint32_t dwFlags;
uint32_t dwFourCC;
uint32_t dwRGBBitCount;
uint32_t dwRBitMask;
uint32_t dwGBitMask;
uint32_t dwBBitMask;
uint32_t dwABitMask;
};
struct DDS_HEADER {
uint32_t dwSize;
uint32_t dwFlags;
uint32_t dwHeight;
uint32_t dwWidth;
uint32_t dwPitchOrLinearSize;
uint32_t dwDepth;
uint32_t dwMipMapCount;
uint32_t dwReserved1[11];
DDS_PIXELFORMAT ddspf;
uint32_t dwCaps1;
uint32_t dwCaps2;
uint32_t dwReserved2[3];
};
string fourcc(uint32_t enc) {
char c[5] = { '\0' };
c[0] = enc >> 0 & 0xFF;
c[1] = enc >> 8 & 0xFF;
c[2] = enc >> 16 & 0xFF;
c[3] = enc >> 24 & 0xFF;
return c;
}
struct DXTColBlock {
uint16_t col0;
uint16_t col1;
uint8_t row[4];
};
struct DXT3AlphaBlock {
uint16_t row[4];
};
struct DXT5AlphaBlock {
uint8_t alpha0;
uint8_t alpha1;
uint8_t row[6];
};
///////////////////////////////////////////////////////////////////////////////
// flip a DXT1 color block
void flip_blocks_dxtc1(DXTColBlock *line, unsigned int numBlocks) {
DXTColBlock *curblock = line;
for (unsigned int i = 0; i < numBlocks; i++) {
std::swap(curblock->row[0], curblock->row[3]);
std::swap(curblock->row[1], curblock->row[2]);
curblock++;
}
}
///////////////////////////////////////////////////////////////////////////////
// flip a DXT3 color block
void flip_blocks_dxtc3(DXTColBlock *line, unsigned int numBlocks) {
DXTColBlock *curblock = line;
DXT3AlphaBlock *alphablock;
for (unsigned int i = 0; i < numBlocks; i++) {
alphablock = (DXT3AlphaBlock*) curblock;
std::swap(alphablock->row[0], alphablock->row[3]);
std::swap(alphablock->row[1], alphablock->row[2]);
curblock++;
std::swap(curblock->row[0], curblock->row[3]);
std::swap(curblock->row[1], curblock->row[2]);
curblock++;
}
}
///////////////////////////////////////////////////////////////////////////////
// flip a DXT5 alpha block
void flip_dxt5_alpha(DXT5AlphaBlock *block) {
uint8_t gBits[4][4];
const uint32_t mask = 0x00000007; // bits = 00 00 01 11
uint32_t bits = 0;
memcpy(&bits, &block->row[0], sizeof(uint8_t) * 3);
gBits[0][0] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[0][1] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[0][2] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[0][3] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[1][0] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[1][1] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[1][2] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[1][3] = (uint8_t) (bits & mask);
bits = 0;
memcpy(&bits, &block->row[3], sizeof(uint8_t) * 3);
gBits[2][0] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[2][1] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[2][2] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[2][3] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[3][0] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[3][1] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[3][2] = (uint8_t) (bits & mask);
bits >>= 3;
gBits[3][3] = (uint8_t) (bits & mask);
uint32_t *pBits = ((uint32_t*) &(block->row[0]));
*pBits = *pBits | (gBits[3][0] << 0);
*pBits = *pBits | (gBits[3][1] << 3);
*pBits = *pBits | (gBits[3][2] << 6);
*pBits = *pBits | (gBits[3][3] << 9);
*pBits = *pBits | (gBits[2][0] << 12);
*pBits = *pBits | (gBits[2][1] << 15);
*pBits = *pBits | (gBits[2][2] << 18);
*pBits = *pBits | (gBits[2][3] << 21);
pBits = ((uint32_t*) &(block->row[3]));
#ifdef MACOS
*pBits &= 0x000000ff;
#else
*pBits &= 0xff000000;
#endif
*pBits = *pBits | (gBits[1][0] << 0);
*pBits = *pBits | (gBits[1][1] << 3);
*pBits = *pBits | (gBits[1][2] << 6);
*pBits = *pBits | (gBits[1][3] << 9);
*pBits = *pBits | (gBits[0][0] << 12);
*pBits = *pBits | (gBits[0][1] << 15);
*pBits = *pBits | (gBits[0][2] << 18);
*pBits = *pBits | (gBits[0][3] << 21);
}
///////////////////////////////////////////////////////////////////////////////
// flip a DXT5 color block
void flip_blocks_dxtc5(DXTColBlock *line, unsigned int numBlocks) {
DXTColBlock *curblock = line;
DXT5AlphaBlock *alphablock;
for (unsigned int i = 0; i < numBlocks; i++) {
alphablock = (DXT5AlphaBlock*) curblock;
flip_dxt5_alpha(alphablock);
curblock++;
std::swap(curblock->row[0], curblock->row[3]);
std::swap(curblock->row[1], curblock->row[2]);
curblock++;
}
}
}
///////////////////////////////////////////////////////////////////////////////
// CDDSImage public functions
///////////////////////////////////////////////////////////////////////////////
// default constructor
CDDSImage::CDDSImage() :
m_format(0), m_components(0), m_type(TextureNone), m_valid(false) {
}
CDDSImage::~CDDSImage() {
}
void CDDSImage::create_textureFlat(unsigned int format, unsigned int components, const CTexture &baseImage) {
assert(format != 0);
assert(components != 0);
assert(baseImage.get_depth() == 1);
// remove any existing images
clear();
m_format = format;
m_components = components;
m_type = TextureFlat;
m_images.push_back(baseImage);
m_valid = true;
}
void CDDSImage::create_texture3D(unsigned int format, unsigned int components, const CTexture &baseImage) {
assert(format != 0);
assert(components != 0);
assert(baseImage.get_depth() > 1);
// remove any existing images
clear();
m_format = format;
m_components = components;
m_type = Texture3D;
m_images.push_back(baseImage);
m_valid = true;
}
inline bool same_size(const CTexture &a, const CTexture &b) {
if (a.get_width() != b.get_width())
return false;
if (a.get_height() != b.get_height())
return false;
if (a.get_depth() != b.get_depth())
return false;
return true;
}
void CDDSImage::create_textureCubemap(unsigned int format, unsigned int components, const CTexture &positiveX, const CTexture &negativeX,
const CTexture &positiveY, const CTexture &negativeY, const CTexture &positiveZ, const CTexture &negativeZ) {
assert(format != 0);
assert(components != 0);
assert(positiveX.get_depth() == 1);
// verify that all dimensions are the same
assert(same_size(positiveX, negativeX));
assert(same_size(positiveX, positiveY));
assert(same_size(positiveX, negativeY));
assert(same_size(positiveX, positiveZ));
assert(same_size(positiveX, negativeZ));
// remove any existing images
clear();
m_format = format;
m_components = components;
m_type = TextureCubemap;
m_images.push_back(positiveX);
m_images.push_back(negativeX);
m_images.push_back(positiveY);
m_images.push_back(negativeY);
m_images.push_back(positiveZ);
m_images.push_back(negativeZ);
m_valid = true;
}
///////////////////////////////////////////////////////////////////////////////
// loads DDS image
//
// filename - fully qualified name of DDS image
// flipImage - specifies whether image is flipped on load, default is true
void CDDSImage::load(const string& filename, bool flipImage) {
assert(!filename.empty());
ifstream fs(filename.c_str(), ios::binary);
load(fs, flipImage);
}
///////////////////////////////////////////////////////////////////////////////
// loads DDS image
//
// is - istream to read the image from
// flipImage - specifies whether image is flipped on load, default is true
void CDDSImage::load(istream& is, bool flipImage) {
// clear any previously loaded images
clear();
// read in file marker, make sure its a DDS file
char filecode[4];
is.read(filecode, 4);
if (strncmp(filecode, "DDS ", 4) != 0) {
throw runtime_error("not a DDS file");
}
// read in DDS header
DDS_HEADER ddsh;
is.read((char*)&ddsh, sizeof(DDS_HEADER));
// default to flat texture type (1D, 2D, or rectangle)
m_type = TextureFlat;
// check if image is a cubemap
if (ddsh.dwCaps2 & DDSF_CUBEMAP)
m_type = TextureCubemap;
// check if image is a volume texture
if ((ddsh.dwCaps2 & DDSF_VOLUME) && (ddsh.dwDepth > 0))
m_type = Texture3D;
// figure out what the image format is
if (ddsh.ddspf.dwFlags & DDSF_FOURCC) {
switch (ddsh.ddspf.dwFourCC) {
case FOURCC_DXT1:
m_format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
m_components = 3;
break;
case FOURCC_DXT3:
m_format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
m_components = 4;
break;
case FOURCC_DXT5:
m_format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
m_components = 4;
break;
default:
throw runtime_error("unknown texture compression '"+fourcc(ddsh.ddspf.dwFourCC)+"'");
}
} else if (ddsh.ddspf.dwRGBBitCount == 32 &&
ddsh.ddspf.dwRBitMask == 0x00FF0000 &&
ddsh.ddspf.dwGBitMask == 0x0000FF00 &&
ddsh.ddspf.dwBBitMask == 0x000000FF &&
ddsh.ddspf.dwABitMask == 0xFF000000) {
m_format = GL_BGRA_EXT;
m_components = 4;
} else if (ddsh.ddspf.dwRGBBitCount == 32 &&
ddsh.ddspf.dwRBitMask == 0x000000FF &&
ddsh.ddspf.dwGBitMask == 0x0000FF00 &&
ddsh.ddspf.dwBBitMask == 0x00FF0000 &&
ddsh.ddspf.dwABitMask == 0xFF000000) {
m_format = GL_RGBA;
m_components = 4;
} else if (ddsh.ddspf.dwRGBBitCount == 24 &&
ddsh.ddspf.dwRBitMask == 0x000000FF &&
ddsh.ddspf.dwGBitMask == 0x0000FF00 &&
ddsh.ddspf.dwBBitMask == 0x00FF0000) {
m_format = GL_RGB;
m_components = 3;
} else if (ddsh.ddspf.dwRGBBitCount == 24 &&
ddsh.ddspf.dwRBitMask == 0x00FF0000 &&
ddsh.ddspf.dwGBitMask == 0x0000FF00 &&
ddsh.ddspf.dwBBitMask == 0x000000FF) {
m_format = GL_BGR_EXT;
m_components = 3;
} else if (ddsh.ddspf.dwRGBBitCount == 8) {
m_format = GL_LUMINANCE;
m_components = 1;
} else {
throw runtime_error("unknow texture format");
}
// store primary surface width/height/depth
unsigned int width, height, depth;
width = ddsh.dwWidth;
height = ddsh.dwHeight;
depth = clamp_size(ddsh.dwDepth); // set to 1 if 0
// use correct size calculation function depending on whether image is
// compressed
unsigned int (CDDSImage::*sizefunc)(unsigned int, unsigned int);
sizefunc = (is_compressed() ? &CDDSImage::size_dxtc : &CDDSImage::size_rgb);
// load all surfaces for the image (6 surfaces for cubemaps)
for (unsigned int n = 0; n < (unsigned int) (m_type == TextureCubemap ? 6 : 1); n++) {
// add empty texture object
m_images.push_back(CTexture());
// get reference to newly added texture object
CTexture &img = m_images[n];
// calculate surface size
unsigned int size = (this->*sizefunc)(width, height) * depth;
// load surface
uint8_t *pixels = new uint8_t[size];
is.read((char*)pixels, size);
img.create(width, height, depth, size, pixels);
delete[] pixels;
if (flipImage)
flip(img);
unsigned int w = clamp_size(width >> 1);
unsigned int h = clamp_size(height >> 1);
unsigned int d = clamp_size(depth >> 1);
// store number of mipmaps
unsigned int numMipmaps = ddsh.dwMipMapCount;
// number of mipmaps in file includes main surface so decrease count
// by one
if (numMipmaps != 0)
numMipmaps--;
// load all mipmaps for current surface
for (unsigned int i = 0; i < numMipmaps && (w || h); i++) {
// add empty surface
img.add_mipmap(CSurface());
// get reference to newly added mipmap
CSurface &mipmap = img.get_mipmap(i);
// calculate mipmap size
size = (this->*sizefunc)(w, h) * d;
uint8_t *pixels = new uint8_t[size];
is.read((char*)pixels, size);
mipmap.create(w, h, d, size, pixels);
delete[] pixels;
if (flipImage)
flip(mipmap);
// shrink to next power of 2
w = clamp_size(w >> 1);
h = clamp_size(h >> 1);
d = clamp_size(d >> 1);
}
}
// swap cubemaps on y axis (since image is flipped in OGL)
if (m_type == TextureCubemap && flipImage) {
CTexture tmp;
tmp = m_images[3];
m_images[3] = m_images[2];
m_images[2] = tmp;
}
m_valid = true;
}
void CDDSImage::write_texture(const CTexture &texture, ostream& os) {
assert(get_num_mipmaps() == texture.get_num_mipmaps());
os.write((char*)(uint8_t*)texture, texture.get_size());
for (unsigned int i = 0; i < texture.get_num_mipmaps(); i++) {
const CSurface &mipmap = texture.get_mipmap(i);
os.write((char*)(uint8_t*)mipmap, mipmap.get_size());
}
}
void CDDSImage::save(const std::string& filename, bool flipImage) {
assert(m_valid);
assert(m_type != TextureNone);
DDS_HEADER ddsh;
unsigned int headerSize = sizeof(DDS_HEADER);
memset(&ddsh, 0, headerSize);
ddsh.dwSize = headerSize;
ddsh.dwFlags = DDSF_CAPS | DDSF_WIDTH | DDSF_HEIGHT | DDSF_PIXELFORMAT;
ddsh.dwHeight = get_height();
ddsh.dwWidth = get_width();
if (is_compressed()) {
ddsh.dwFlags |= DDSF_LINEARSIZE;
ddsh.dwPitchOrLinearSize = get_size();
} else {
ddsh.dwFlags |= DDSF_PITCH;
ddsh.dwPitchOrLinearSize = get_dword_aligned_linesize(get_width(), m_components * 8);
}
if (m_type == Texture3D) {
ddsh.dwFlags |= DDSF_DEPTH;
ddsh.dwDepth = get_depth();
}
if (get_num_mipmaps() > 0) {
ddsh.dwFlags |= DDSF_MIPMAPCOUNT;
ddsh.dwMipMapCount = get_num_mipmaps() + 1;
}
ddsh.ddspf.dwSize = sizeof(DDS_PIXELFORMAT);
if (is_compressed()) {
ddsh.ddspf.dwFlags = DDSF_FOURCC;
if (m_format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)
ddsh.ddspf.dwFourCC = FOURCC_DXT1;
if (m_format == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT)
ddsh.ddspf.dwFourCC = FOURCC_DXT3;
if (m_format == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
ddsh.ddspf.dwFourCC = FOURCC_DXT5;
} else {
ddsh.ddspf.dwFlags = (m_components == 4) ? DDSF_RGBA : DDSF_RGB;
ddsh.ddspf.dwRGBBitCount = m_components * 8;
ddsh.ddspf.dwRBitMask = 0x00ff0000;
ddsh.ddspf.dwGBitMask = 0x0000ff00;
ddsh.ddspf.dwBBitMask = 0x000000ff;
if (m_components == 4) {
ddsh.ddspf.dwFlags |= DDSF_ALPHAPIXELS;
ddsh.ddspf.dwABitMask = 0xff000000;
}
}
ddsh.dwCaps1 = DDSF_TEXTURE;
if (m_type == TextureCubemap) {
ddsh.dwCaps1 |= DDSF_COMPLEX;
ddsh.dwCaps2 = DDSF_CUBEMAP | DDSF_CUBEMAP_ALL_FACES;
}
if (m_type == Texture3D) {
ddsh.dwCaps1 |= DDSF_COMPLEX;
ddsh.dwCaps2 = DDSF_VOLUME;
}
if (get_num_mipmaps() > 0)
ddsh.dwCaps1 |= DDSF_COMPLEX | DDSF_MIPMAP;
// open file
ofstream of;
of.exceptions(ios::failbit);
of.open(filename.c_str(), ios::binary);
// write file header
of.write("DDS ", 4);
// write dds header
of.write((char*)&ddsh, sizeof(DDS_HEADER));
if (m_type != TextureCubemap) {
CTexture tex = m_images[0];
if (flipImage)
flip_texture(tex);
write_texture(tex, of);
} else {
assert(m_images.size() == 6);
for (unsigned int i = 0; i < m_images.size(); i++) {
CTexture cubeFace;
if (i == 2)
cubeFace = m_images[3];
else if (i == 3)
cubeFace = m_images[2];
else
cubeFace = m_images[i];
if (flipImage)
flip_texture(cubeFace);
write_texture(cubeFace, of);
}
}
}
///////////////////////////////////////////////////////////////////////////////
// free image memory
void CDDSImage::clear() {
m_components = 0;
m_format = 0;
m_type = TextureNone;
m_valid = false;
m_images.clear();
}
#ifndef NV_DDS_NO_GL_SUPPORT
#if !defined(GL_ES_VERSION_2_0) && !defined(GL_ES_VERSION_3_0)
///////////////////////////////////////////////////////////////////////////////
// uploads a compressed/uncompressed 1D texture
void CDDSImage::upload_texture1D() {
assert(m_valid);
assert(!m_images.empty());
const CTexture &baseImage = m_images[0];
assert(baseImage.get_height() == 1);
assert(baseImage.get_width() > 0);
if (is_compressed()) {
glCompressedTexImage1D(GL_TEXTURE_1D, 0, m_format, baseImage.get_width(), 0, baseImage.get_size(), baseImage);
// load all mipmaps
for (unsigned int i = 0; i < baseImage.get_num_mipmaps(); i++) {
const CSurface &mipmap = baseImage.get_mipmap(i);
glCompressedTexImage1D(GL_TEXTURE_1D, i + 1, m_format, mipmap.get_width(), 0, mipmap.get_size(), mipmap);
}
} else {
GLint alignment = -1;
if (!is_dword_aligned()) {
glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
glTexImage1D(GL_TEXTURE_1D, 0, m_components, baseImage.get_width(), 0, m_format, GL_UNSIGNED_BYTE, baseImage);
// load all mipmaps
for (unsigned int i = 0; i < baseImage.get_num_mipmaps(); i++) {
const CSurface &mipmap = baseImage.get_mipmap(i);
glTexImage1D(GL_TEXTURE_1D, i + 1, m_components, mipmap.get_width(), 0, m_format, GL_UNSIGNED_BYTE, mipmap);
}
if (alignment != -1)
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
}
}
#endif
///////////////////////////////////////////////////////////////////////////////
// uploads a compressed/uncompressed 2D texture
//
// imageIndex - allows you to optionally specify other loaded surfaces for 2D
// textures such as a face in a cubemap or a slice in a volume
//
// default: 0
//
// target - allows you to optionally specify a different texture target for
// the 2D texture such as a specific face of a cubemap
//
// default: GL_TEXTURE_2D
void CDDSImage::upload_texture2D(uint32_t imageIndex, uint32_t target) {
assert(m_valid);
assert(!m_images.empty());
assert(imageIndex < m_images.size());
assert(m_images[imageIndex]);
const CTexture &image = m_images[imageIndex];
assert(image.get_height() > 0);
assert(image.get_width() > 0);
assert(
target == GL_TEXTURE_2D || (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z));
if (is_compressed()) {
glCompressedTexImage2D(target, 0, m_format, image.get_width(), image.get_height(), 0, image.get_size(), image);
// load all mipmaps
for (unsigned int i = 0; i < image.get_num_mipmaps(); i++) {
const CSurface &mipmap = image.get_mipmap(i);
glCompressedTexImage2D(target, i + 1, m_format, mipmap.get_width(), mipmap.get_height(), 0, mipmap.get_size(), mipmap);
}
} else {
GLint alignment = -1;
if (!is_dword_aligned()) {
glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
glTexImage2D(target, 0, m_components, image.get_width(), image.get_height(), 0, m_format, GL_UNSIGNED_BYTE, image);
// load all mipmaps
for (unsigned int i = 0; i < image.get_num_mipmaps(); i++) {
const CSurface &mipmap = image.get_mipmap(i);
glTexImage2D(target, i + 1, m_components, mipmap.get_width(), mipmap.get_height(), 0, m_format, GL_UNSIGNED_BYTE, mipmap);
}
if (alignment != -1)
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
}
}
#ifndef GL_ES_VERSION_2_0
///////////////////////////////////////////////////////////////////////////////
// uploads a compressed/uncompressed 3D texture
void CDDSImage::upload_texture3D() {
assert(m_valid);
assert(!m_images.empty());
assert(m_type == Texture3D);
const CTexture &baseImage = m_images[0];
assert(baseImage.get_depth() >= 1);
if (is_compressed()) {
glCompressedTexImage3D(GL_TEXTURE_3D, 0, m_format, baseImage.get_width(), baseImage.get_height(), baseImage.get_depth(), 0, baseImage.get_size(),
baseImage);
// load all mipmap volumes
for (unsigned int i = 0; i < baseImage.get_num_mipmaps(); i++) {
const CSurface &mipmap = baseImage.get_mipmap(i);
glCompressedTexImage3D(GL_TEXTURE_3D, i + 1, m_format, mipmap.get_width(), mipmap.get_height(), mipmap.get_depth(), 0, mipmap.get_size(),
mipmap);
}
} else {
GLint alignment = -1;
if (!is_dword_aligned()) {
glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
glTexImage3D(GL_TEXTURE_3D, 0, m_components, baseImage.get_width(), baseImage.get_height(), baseImage.get_depth(), 0, m_format, GL_UNSIGNED_BYTE,
baseImage);
// load all mipmap volumes
for (unsigned int i = 0; i < baseImage.get_num_mipmaps(); i++) {
const CSurface &mipmap = baseImage.get_mipmap(i);
glTexImage3D(GL_TEXTURE_3D, i + 1, m_components, mipmap.get_width(), mipmap.get_height(), mipmap.get_depth(), 0, m_format, GL_UNSIGNED_BYTE,
mipmap);
}
if (alignment != -1)
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
}
}
#endif
///////////////////////////////////////////////////////////////////////////////
// uploads a compressed/uncompressed cubemap texture
void CDDSImage::upload_textureCubemap() {
assert(m_valid);
assert(!m_images.empty());
assert(m_type == TextureCubemap);
assert(m_images.size() == 6);
GLenum target;
// loop through cubemap faces and load them as 2D textures
for (unsigned int n = 0; n < 6; n++) {
// specify cubemap face
target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + n;
upload_texture2D(n, target);
}
}
#endif
bool CDDSImage::is_compressed() {
return (m_format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)
|| (m_format == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT)
|| (m_format == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT);
}
///////////////////////////////////////////////////////////////////////////////
// clamps input size to [1-size]
inline unsigned int CDDSImage::clamp_size(unsigned int size) {
if (size <= 0)
size = 1;
return size;
}
///////////////////////////////////////////////////////////////////////////////
// CDDSImage private functions
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// calculates size of DXTC texture in bytes
inline unsigned int CDDSImage::size_dxtc(unsigned int width, unsigned int height) {
return ((width + 3) / 4) * ((height + 3) / 4) * (m_format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ? 8 : 16);
}
///////////////////////////////////////////////////////////////////////////////
// calculates size of uncompressed RGB texture in bytes
inline unsigned int CDDSImage::size_rgb(unsigned int width, unsigned int height) {
return width * height * m_components;
}
///////////////////////////////////////////////////////////////////////////////
// flip image around X axis
void CDDSImage::flip(CSurface &surface) {
unsigned int linesize;
unsigned int offset;
if (!is_compressed()) {
assert(surface.get_depth() > 0);
unsigned int imagesize = surface.get_size() / surface.get_depth();
linesize = imagesize / surface.get_height();
uint8_t *tmp = new uint8_t[linesize];