-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnv_dds_common.cpp
749 lines (623 loc) · 18.4 KB
/
nv_dds_common.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
/*********************************************************************NVMH4****
Path: NVSDK\Common\src\dds_common
File: dds_common.cpp
Copyright NVIDIA Corporation 2002
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Comments:
dds loader.
******************************************************************************/
///////////////////////////////////////////////////////////////////////////////
//
// 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: 23/10/2002
//
// CMAUGHAN
// A branch from dds to dds_common to make a common file that doens't have the
// API specific stuff in, so that DX8/9 can use this file's useful capability of
// loading dds files into memory.
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include "nv_dds_common.h"
using namespace std;
using namespace dds_common;
///////////////////////////////////////////////////////////////////////////////
// CDDSImage public functions
///////////////////////////////////////////////////////////////////////////////
// default constructor
CDDSImage::CDDSImage()
: format(0),
components(0),
compressed(false),
cubemap(false),
volume(false),
valid(false)
{
}
CDDSImage::~CDDSImage()
{
}
///////////////////////////////////////////////////////////////////////////////
// loads DDS image
//
// filename - fully qualified name of DDS image
// flipImage - specifies whether image is flipped on load, default is true
bool CDDSImage::load(std::string filename, bool flipImage)
{
DDS_HEADER ddsh;
char filecode[4];
FILE *fp;
int width, height, depth;
int (CDDSImage::*sizefunc)(int, int);
// clear any previously loaded images
clear();
// open file
fp = fopen(filename.data(), "rb");
if (fp == NULL)
return false;
// read in file marker, make sure its a DDS file
fread(filecode, 1, 4, fp);
if (strncmp(filecode, "DDS ", 4) != 0)
{
fclose(fp);
return false;
}
// read in DDS header
fread(&ddsh, sizeof(ddsh), 1, fp);
// check if image is a cubempa
if (ddsh.dwCaps2 & DDS_CUBEMAP)
cubemap = true;
// check if image is a volume texture
if ((ddsh.dwCaps2 & DDS_VOLUME) && (ddsh.dwDepth > 0))
volume = true;
// figure out what the image format is
if (ddsh.ddspf.dwFlags & DDS_FOURCC)
{
d3dformat = ddsh.ddspf.dwFourCC;
switch(ddsh.ddspf.dwFourCC)
{
case FOURCC_DXT1:
format = FORMAT_DXT1;
components = 3;
compressed = true;
break;
case FOURCC_DXT3:
format = FORMAT_DXT3;
components = 4;
compressed = true;
break;
case FOURCC_DXT5:
format = FORMAT_DXT5;
components = 4;
compressed = true;
break;
default:
fclose(fp);
return false;
}
}
else if (ddsh.ddspf.dwFlags == DDS_RGBA && ddsh.ddspf.dwRGBBitCount == 32)
{
format = FORMAT_RGBA;
d3dformat = D3DFMT_A8R8G8B8;
compressed = false;
components = 4;
}
else if (ddsh.ddspf.dwFlags == DDS_RGB && ddsh.ddspf.dwRGBBitCount == 32)
{
format = FORMAT_RGBA;
d3dformat = D3DFMT_X8R8G8B8;
compressed = false;
components = 4;
}
else if (ddsh.ddspf.dwFlags == DDS_RGB && ddsh.ddspf.dwRGBBitCount == 24)
{
format = FORMAT_RGB;
d3dformat = D3DFMT_R8G8B8;
compressed = false;
components = 3;
}
else if (ddsh.ddspf.dwRGBBitCount == 8)
{
format = FORMAT_L8;
d3dformat = D3DFMT_L8;
compressed = false;
components = 1;
}
else
{
fclose(fp);
return false;
}
// store primary surface 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
sizefunc = (compressed ? &CDDSImage::size_dxtc : &CDDSImage::size_rgb);
// load all surfaces for the image (6 surfaces for cubemaps)
for (int n = 0; n < (cubemap ? 6 : 1); n++)
{
int size;
// calculate surface size
size = (this->*sizefunc)(width, height)*depth;
// load surface
CTexture img(width, height, depth, size);
fread(img, 1, img.size, fp);
align_memory(&img);
if (flipImage)
flip(img, img.width, img.height, img.depth, img.size);
int w = clamp_size(width >> 1);
int h = clamp_size(height >> 1);
int d = clamp_size(depth >> 1);
// store number of mipmaps
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 (int i = 0; i < numMipmaps && (w || h); i++)
{
// calculate mipmap size
size = (this->*sizefunc)(w, h)*d;
CSurface mipmap(w, h, d, size);
fread(mipmap, 1, mipmap.size, fp);
if (flipImage)
{
flip(mipmap, mipmap.width, mipmap.height, mipmap.depth,
mipmap.size);
}
img.mipmaps.push_back(mipmap);
// shrink to next power of 2
w = clamp_size(w >> 1);
h = clamp_size(h >> 1);
d = clamp_size(d >> 1);
}
images.push_back(img);
}
// swap cubemaps on y axis (since image is flipped in OGL)
if (cubemap && flipImage)
{
CTexture tmp;
tmp = images[3];
images[3] = images[2];
images[2] = tmp;
}
fclose(fp);
valid = true;
return true;
}
///////////////////////////////////////////////////////////////////////////////
// free image memory
void CDDSImage::clear()
{
components = 0;
format = 0;
compressed = false;
cubemap = false;
volume = false;
valid = false;
images.clear();
}
///////////////////////////////////////////////////////////////////////////////
// returns individual texture when multiple textures are loaded (as is the case
// with volume textures and cubemaps)
CTexture &CDDSImage::operator[](int index)
{
// make sure an image has been loaded
assert(valid);
assert(index < (int)images.size());
return images[index];
}
///////////////////////////////////////////////////////////////////////////////
// returns pointer to main image
CDDSImage::operator char*()
{
assert(valid);
return images[0];
}
///////////////////////////////////////////////////////////////////////////////
// clamps input size to [1-size]
inline int CDDSImage::clamp_size(int size)
{
if (size <= 0)
size = 1;
return size;
}
///////////////////////////////////////////////////////////////////////////////
// CDDSImage private functions
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// calculates 4-byte aligned width of image
inline int CDDSImage::get_line_width(int width, int bpp)
{
return ((width * bpp + 31) & -32) >> 3;
}
///////////////////////////////////////////////////////////////////////////////
// calculates size of DXTC texture in bytes
inline int CDDSImage::size_dxtc(int width, int height)
{
return ((width+3)/4)*((height+3)/4)*
(format == FORMAT_DXT1 ? 8 : 16);
}
///////////////////////////////////////////////////////////////////////////////
// calculates size of uncompressed RGB texture in bytes
inline int CDDSImage::size_rgb(int width, int height)
{
return width*height*components;
}
///////////////////////////////////////////////////////////////////////////////
// align to 4 byte boundary (add pad bytes to end of each line in the image)
void CDDSImage::align_memory(CTexture *surface)
{
// don't bother with compressed images, volume textures, or cubemaps
if (compressed || volume || cubemap)
return;
// calculate new image size
int linesize = get_line_width(surface->width, components*8);
int imagesize = linesize*surface->height;
// exit if already aligned
if (surface->size == imagesize)
return;
// create new image of new size
CTexture newSurface(surface->width, surface->height, surface->depth,
imagesize);
// add pad bytes to end of each line
char *srcimage = (char*)*surface;
char *dstimage = (char*)newSurface;
for (int n = 0; n < surface->depth; n++)
{
char *curline = srcimage;
char *newline = dstimage;
int imsize = surface->size / surface->depth;
int lnsize = imsize / surface->height;
for (int i = 0; i < surface->height; i++)
{
memcpy(newline, curline, lnsize);
newline += linesize;
curline += lnsize;
}
}
// save padded image
*surface = newSurface;
}
///////////////////////////////////////////////////////////////////////////////
// flip image around X axis
void CDDSImage::flip(char *image, int width, int height, int depth, int size)
{
int linesize;
int offset;
if (!compressed)
{
assert(depth > 0);
int imagesize = size/depth;
linesize = imagesize / height;
for (int n = 0; n < depth; n++)
{
offset = imagesize*n;
char *top = image + offset;
char *bottom = top + (imagesize-linesize);
for (int i = 0; i < (height >> 1); i++)
{
swap(bottom, top, linesize);
top += linesize;
bottom -= linesize;
}
}
}
else
{
void (CDDSImage::*flipblocks)(DXTColBlock*, int);
int xblocks = width / 4;
int yblocks = height / 4;
int blocksize;
switch (format)
{
case FORMAT_DXT1:
blocksize = 8;
flipblocks = &CDDSImage::flip_blocks_dxtc1;
break;
case FORMAT_DXT3:
blocksize = 16;
flipblocks = &CDDSImage::flip_blocks_dxtc3;
break;
case FORMAT_DXT5:
blocksize = 16;
flipblocks = &CDDSImage::flip_blocks_dxtc5;
break;
default:
return;
}
linesize = xblocks * blocksize;
DXTColBlock *top;
DXTColBlock *bottom;
for (int j = 0; j < (yblocks >> 1); j++)
{
top = (DXTColBlock*)(image + j * linesize);
bottom = (DXTColBlock*)(image + (((yblocks-j)-1) * linesize));
(this->*flipblocks)(top, xblocks);
(this->*flipblocks)(bottom, xblocks);
swap(bottom, top, linesize);
}
}
}
///////////////////////////////////////////////////////////////////////////////
// swap to sections of memory
void CDDSImage::swap(void *byte1, void *byte2, int size)
{
unsigned char *tmp = new unsigned char[size];
memcpy(tmp, byte1, size);
memcpy(byte1, byte2, size);
memcpy(byte2, tmp, size);
delete [] tmp;
}
///////////////////////////////////////////////////////////////////////////////
// flip a DXT1 color block
void CDDSImage::flip_blocks_dxtc1(DXTColBlock *line, int numBlocks)
{
DXTColBlock *curblock = line;
for (int i = 0; i < numBlocks; i++)
{
swap(&curblock->row[0], &curblock->row[3], sizeof(unsigned char));
swap(&curblock->row[1], &curblock->row[2], sizeof(unsigned char));
curblock++;
}
}
///////////////////////////////////////////////////////////////////////////////
// flip a DXT3 color block
void CDDSImage::flip_blocks_dxtc3(DXTColBlock *line, int numBlocks)
{
DXTColBlock *curblock = line;
DXT3AlphaBlock *alphablock;
for (int i = 0; i < numBlocks; i++)
{
alphablock = (DXT3AlphaBlock*)curblock;
swap(&alphablock->row[0], &alphablock->row[3], sizeof(unsigned short));
swap(&alphablock->row[1], &alphablock->row[2], sizeof(unsigned short));
curblock++;
swap(&curblock->row[0], &curblock->row[3], sizeof(unsigned char));
swap(&curblock->row[1], &curblock->row[2], sizeof(unsigned char));
curblock++;
}
}
///////////////////////////////////////////////////////////////////////////////
// flip a DXT5 alpha block
void CDDSImage::flip_dxt5_alpha(DXT5AlphaBlock *block)
{
unsigned char gBits[4][4];
const unsigned long mask = 0x00000007; // bits = 00 00 01 11
unsigned long bits = 0;
memcpy(&bits, &block->row[0], sizeof(unsigned char) * 3);
gBits[0][0] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[0][1] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[0][2] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[0][3] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[1][0] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[1][1] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[1][2] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[1][3] = (unsigned char)(bits & mask);
bits = 0;
memcpy(&bits, &block->row[3], sizeof(unsigned char) * 3);
gBits[2][0] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[2][1] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[2][2] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[2][3] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[3][0] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[3][1] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[3][2] = (unsigned char)(bits & mask);
bits >>= 3;
gBits[3][3] = (unsigned char)(bits & mask);
unsigned long *pBits = ((unsigned long*) &(block->row[0]));
*pBits &= 0xff000000;
*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 = ((unsigned long*) &(block->row[3]));
*pBits &= 0xff000000;
*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 CDDSImage::flip_blocks_dxtc5(DXTColBlock *line, int numBlocks)
{
DXTColBlock *curblock = line;
DXT5AlphaBlock *alphablock;
for (int i = 0; i < numBlocks; i++)
{
alphablock = (DXT5AlphaBlock*)curblock;
flip_dxt5_alpha(alphablock);
curblock++;
swap(&curblock->row[0], &curblock->row[3], sizeof(unsigned char));
swap(&curblock->row[1], &curblock->row[2], sizeof(unsigned char));
curblock++;
}
}
///////////////////////////////////////////////////////////////////////////////
// CTexture implementation
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// default constructor
CTexture::CTexture()
: CSurface() // initialize base class part
{
}
///////////////////////////////////////////////////////////////////////////////
// creates an empty texture
CTexture::CTexture(int w, int h, int d, int imgSize)
: CSurface(w, h, d, imgSize) // initialize base class part
{
}
///////////////////////////////////////////////////////////////////////////////
// copy constructor
CTexture::CTexture(const CTexture ©)
: CSurface(copy)
{
for (unsigned int i = 0; i < copy.mipmaps.size(); i++)
mipmaps.push_back(copy.mipmaps[i]);
}
///////////////////////////////////////////////////////////////////////////////
// assignment operator
CTexture &CTexture::operator= (const CTexture &rhs)
{
if (this != &rhs)
{
CSurface::operator = (rhs);
mipmaps.clear();
for (unsigned int i = 0; i < rhs.mipmaps.size(); i++)
{
mipmaps.push_back(rhs.mipmaps[i]);
}
}
return *this;
}
///////////////////////////////////////////////////////////////////////////////
// clean up texture memory
CTexture::~CTexture()
{
mipmaps.clear();
}
///////////////////////////////////////////////////////////////////////////////
// CSurface implementation
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// default constructor
CSurface::CSurface()
: width(0),
height(0),
depth(0),
size(0),
pixels(NULL)
{
}
///////////////////////////////////////////////////////////////////////////////
// creates an empty image
CSurface::CSurface(int w, int h, int d, int imgsize)
{
pixels = NULL;
create(w, h, d, imgsize);
}
///////////////////////////////////////////////////////////////////////////////
// copy constructor
CSurface::CSurface(const CSurface ©)
: width(0),
height(0),
depth(0),
size(0),
pixels(NULL)
{
if (copy.pixels)
{
size = copy.size;
width = copy.width;
height = copy.height;
depth = copy.depth;
pixels = new char[size];
memcpy(pixels, copy.pixels, copy.size);
}
}
///////////////////////////////////////////////////////////////////////////////
// assignment operator
CSurface &CSurface::operator= (const CSurface &rhs)
{
if (this != &rhs)
{
clear();
if (rhs.pixels)
{
size = rhs.size;
width = rhs.width;
height = rhs.height;
depth = rhs.depth;
pixels = new char[size];
memcpy(pixels, rhs.pixels, size);
}
}
return *this;
}
///////////////////////////////////////////////////////////////////////////////
// clean up image memory
CSurface::~CSurface()
{
clear();
}
///////////////////////////////////////////////////////////////////////////////
// returns a pointer to image
CSurface::operator char*()
{
return pixels;
}
///////////////////////////////////////////////////////////////////////////////
// creates an empty image
void CSurface::create(int w, int h, int d, int imgsize)
{
clear();
width = w;
height = h;
depth = d;
size = imgsize;
pixels = new char[imgsize];
}
///////////////////////////////////////////////////////////////////////////////
// free surface memory
void CSurface::clear()
{
delete [] pixels;
pixels = NULL;
}