Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nv_image_dds.cpp « Images « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d8262148c2889297ad833ed48fb5bd9f1deaed2c (plain)
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
//-----------------------------------------------------------------------------
//           Name: nv_image_dds.h
//      Developer: Wolfire Games LLC
//    Description: This is a file from an Nvidia image library, with some 
//                 modification made by Wolfire.
//        License: Read below
//-----------------------------------------------------------------------------
//
// nvImageDDS.cpp - Image support class
//
// The nvImage class implements an interface for a multipurpose image
// object. This class is useful for loading and formating images
// for use as textures. The class supports dds, png, and hdr formats.
//
// This file implements the DDS specific functionality.
//
// Author: Evan Hart
// Email: sdkfeedback@nvidia.com
//
// Copyright (c) NVIDIA Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////////////////

#include <Images/nv_image.h>
#include <Compat/fileio.h>
#include <Internal/integer.h>

#include <cstdio>
#include <cstring>

using std::vector;

namespace nv2 {

//
//  Structure defines and constants from nvdds
//
//////////////////////////////////////////////////////////////////////

// surface description flags
const uint32_t DDSF_CAPS           = 0x00000001l;
const uint32_t DDSF_HEIGHT         = 0x00000002l;
const uint32_t DDSF_WIDTH          = 0x00000004l;
const uint32_t DDSF_PITCH          = 0x00000008l;
const uint32_t DDSF_PIXELFORMAT    = 0x00001000l;
const uint32_t DDSF_MIPMAPCOUNT    = 0x00020000l;
const uint32_t DDSF_LINEARSIZE     = 0x00080000l;
const uint32_t DDSF_DEPTH          = 0x00800000l;

// pixel format flags
const uint32_t DDSF_ALPHAPIXELS    = 0x00000001l;
const uint32_t DDSF_FOURCC         = 0x00000004l;
const uint32_t DDSF_RGB            = 0x00000040l;
const uint32_t DDSF_RGBA           = 0x00000041l;

// dwCaps1 flags
const uint32_t DDSF_COMPLEX         = 0x00000008l;
const uint32_t DDSF_TEXTURE         = 0x00001000l;
const uint32_t DDSF_MIPMAP          = 0x00400000l;

// dwCaps2 flags
const uint32_t DDSF_CUBEMAP         = 0x00000200l;
const uint32_t DDSF_CUBEMAP_POSITIVEX  = 0x00000400l;
const uint32_t DDSF_CUBEMAP_NEGATIVEX  = 0x00000800l;
const uint32_t DDSF_CUBEMAP_POSITIVEY  = 0x00001000l;
const uint32_t DDSF_CUBEMAP_NEGATIVEY  = 0x00002000l;
const uint32_t DDSF_CUBEMAP_POSITIVEZ  = 0x00004000l;
const uint32_t DDSF_CUBEMAP_NEGATIVEZ  = 0x00008000l;
const uint32_t DDSF_CUBEMAP_ALL_FACES  = 0x0000FC00l;
const uint32_t DDSF_VOLUME          = 0x00200000l;

// compressed texture types
const uint32_t FOURCC_UNKNOWN       = 0;

#ifndef MAKEFOURCC
#define MAKEFOURCC(c0,c1,c2,c3) \
    ((uint32_t)(unsigned char)(c0)| \
    ((uint32_t)(unsigned char)(c1) << 8)| \
    ((uint32_t)(unsigned char)(c2) << 16)| \
    ((uint32_t)(unsigned char)(c3) << 24))
#endif

const uint32_t FOURCC_R8G8B8        = 20;
const uint32_t FOURCC_A8R8G8B8      = 21;
const uint32_t FOURCC_X8R8G8B8      = 22;
const uint32_t FOURCC_R5G6B5        = 23;
const uint32_t FOURCC_X1R5G5B5      = 24;
const uint32_t FOURCC_A1R5G5B5      = 25;
const uint32_t FOURCC_A4R4G4B4      = 26;
const uint32_t FOURCC_R3G3B2        = 27;
const uint32_t FOURCC_A8            = 28;
const uint32_t FOURCC_A8R3G3B2      = 29;
const uint32_t FOURCC_X4R4G4B4      = 30;
const uint32_t FOURCC_A2B10G10R10   = 31;
const uint32_t FOURCC_A8B8G8R8      = 32;
const uint32_t FOURCC_X8B8G8R8      = 33;
const uint32_t FOURCC_G16R16        = 34;
const uint32_t FOURCC_A2R10G10B10   = 35;
const uint32_t FOURCC_A16B16G16R16  = 36;

const uint32_t FOURCC_L8            = 50;
const uint32_t FOURCC_A8L8          = 51;
const uint32_t FOURCC_A4L4          = 52;
const uint32_t FOURCC_DXT1          = 0x31545844l; //(MAKEFOURCC('D','X','T','1'))
const uint32_t FOURCC_DXT2          = 0x32545844l; //(MAKEFOURCC('D','X','T','1'))
const uint32_t FOURCC_DXT3          = 0x33545844l; //(MAKEFOURCC('D','X','T','3'))
const uint32_t FOURCC_DXT4          = 0x34545844l; //(MAKEFOURCC('D','X','T','3'))
const uint32_t FOURCC_DXT5          = 0x35545844l; //(MAKEFOURCC('D','X','T','5'))
const uint32_t FOURCC_ATI1          = MAKEFOURCC('A','T','I','1');
const uint32_t FOURCC_ATI2          = MAKEFOURCC('A','T','I','2');

const uint32_t FOURCC_D16_LOCKABLE  = 70;
const uint32_t FOURCC_D32           = 71;
const uint32_t FOURCC_D24X8         = 77;
const uint32_t FOURCC_D16           = 80;

const uint32_t FOURCC_D32F_LOCKABLE = 82;

const uint32_t FOURCC_L16           = 81;

// Floating point surface formats

// s10e5 formats (16-bits per channel)
const uint32_t FOURCC_R16F          = 111;
const uint32_t FOURCC_G16R16F       = 112;
const uint32_t FOURCC_A16B16G16R16F = 113;

// IEEE s23e8 formats (32-bits per channel)
const uint32_t FOURCC_R32F          = 114;
const uint32_t FOURCC_G32R32F       = 115;
const uint32_t FOURCC_A32B32G32R32F = 116;

struct DXTColBlock
{
    GLushort col0;
    GLushort col1;

    GLubyte row[4];
};

struct DXT3AlphaBlock
{
    GLushort row[4];
};

struct DXT5AlphaBlock
{
    GLubyte alpha0;
    GLubyte alpha1;
    
    GLubyte row[6];
};

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];
};

//
//
////////////////////////////////////////////////////////////
bool Image::readDDS( const char *file, Image& i) {

    // open file
    FILE *fp = my_fopen(file, "rb");
    if (fp == NULL)
        return false;

    // read in file marker, make sure its a DDS file
    char filecode[4];
    fread(filecode, 1, 4, fp);
    if (strncmp(filecode, "DDS ", 4) != 0)
    {
        fclose(fp);
        return false;
    }

    // read in DDS header
    DDS_HEADER ddsh;
    fread(&ddsh, sizeof(DDS_HEADER), 1, fp);

    // check if image is a volume texture
    if ((ddsh.dwCaps2 & DDSF_VOLUME) && (ddsh.dwDepth > 0))
        i._depth = ddsh.dwDepth;
    else
        i._depth = 0;

    // There are flags that are supposed to mark these fields as valid, but some dds files don't set them properly
    i._width = ddsh.dwWidth;
    i._height = ddsh.dwHeight;
    
    if (ddsh.dwFlags & DDSF_MIPMAPCOUNT) {
        i._levelCount = ddsh.dwMipMapCount;
    }
    else
        i._levelCount = 1;

    //check cube-map faces
    if ( ddsh.dwCaps2 & DDSF_CUBEMAP) {
        //this is a cubemap, count the faces
        i._faces = 0;
        i._faces += (ddsh.dwCaps2 & DDSF_CUBEMAP_POSITIVEX) ? 1 : 0;
        i._faces += (ddsh.dwCaps2 & DDSF_CUBEMAP_NEGATIVEX) ? 1 : 0;
        i._faces += (ddsh.dwCaps2 & DDSF_CUBEMAP_POSITIVEY) ? 1 : 0;
        i._faces += (ddsh.dwCaps2 & DDSF_CUBEMAP_NEGATIVEY) ? 1 : 0;
        i._faces += (ddsh.dwCaps2 & DDSF_CUBEMAP_POSITIVEZ) ? 1 : 0;
        i._faces += (ddsh.dwCaps2 & DDSF_CUBEMAP_NEGATIVEZ) ? 1 : 0;

        //check for a complete cubemap
        if ( (i._faces != 6) || (i._width != i._height) ) {
            fclose(fp);
            return false;
        }
    }
    else {
        //not a cubemap
        i._faces = 0;
    }

    bool btcCompressed = false;
    int bytesPerElement = 0;

    // figure out what the image format is
    if (ddsh.ddspf.dwFlags & DDSF_FOURCC) 
    {
        switch(ddsh.ddspf.dwFourCC)
        {
            case FOURCC_DXT1:
                i._format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
                i._internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
                i._type = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
                bytesPerElement = 8;
                btcCompressed = true;
                break;

            case FOURCC_DXT2:
            case FOURCC_DXT3:
                i._format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
                i._internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
                i._type = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
                bytesPerElement = 16;
                btcCompressed = true;
                break;

            case FOURCC_DXT4:
            case FOURCC_DXT5:
                i._format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
                i._internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
                i._type = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
                bytesPerElement = 16;
                btcCompressed = true;
                break;

            case FOURCC_ATI1:
                i._format = GL_COMPRESSED_LUMINANCE_LATC1_EXT;
                i._internalFormat = GL_COMPRESSED_LUMINANCE_LATC1_EXT;
                i._type = GL_COMPRESSED_LUMINANCE_LATC1_EXT;
                bytesPerElement = 8;
                btcCompressed = true;
                break;

            case FOURCC_ATI2:
                i._format = GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
                i._internalFormat = GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
                i._type = GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT;
                bytesPerElement = 16;
                btcCompressed = true;
                break;

            case FOURCC_R8G8B8:
                i._format = GL_BGR;
                i._internalFormat = GL_RGB8;
                i._type = GL_UNSIGNED_BYTE;
                bytesPerElement = 3;
                break;

            case FOURCC_A8R8G8B8:
                i._format = GL_BGRA;
                i._internalFormat = GL_RGBA8;
                i._type = GL_UNSIGNED_BYTE;
                bytesPerElement = 4;
                break;

            case FOURCC_X8R8G8B8:
                i._format = GL_BGRA;
                i._internalFormat = GL_RGB8;
                i._type = GL_UNSIGNED_INT_8_8_8_8;
                bytesPerElement = 4;
                break;

            case FOURCC_R5G6B5:
                i._format = GL_BGR;
                i._internalFormat = GL_RGB5;
                i._type = GL_UNSIGNED_SHORT_5_6_5;
                bytesPerElement = 2;
                break;

            case FOURCC_A8:
                i._format = GL_ALPHA;
                i._internalFormat = GL_R8;
                i._type = GL_UNSIGNED_BYTE;
                bytesPerElement = 1;
                break;

            case FOURCC_A2B10G10R10:
                i._format = GL_RGBA;
                i._internalFormat = GL_RGB10_A2;
                i._type = GL_UNSIGNED_INT_10_10_10_2;
                bytesPerElement = 4;
                break;

            case FOURCC_A8B8G8R8:
                i._format = GL_RGBA;
                i._internalFormat = GL_RGBA8;
                i._type = GL_UNSIGNED_BYTE;
                bytesPerElement = 4;
                break;

            case FOURCC_X8B8G8R8:
                i._format = GL_RGBA;
                i._internalFormat = GL_RGB8;
                i._type = GL_UNSIGNED_INT_8_8_8_8;
                bytesPerElement = 4;
                break;

            case FOURCC_A2R10G10B10:
                i._format = GL_BGRA;
                i._internalFormat = GL_RGB10_A2;
                i._type = GL_UNSIGNED_INT_10_10_10_2;
                bytesPerElement = 4;
                break;

            case FOURCC_A16B16G16R16:
                i._format = GL_RGBA;
                i._internalFormat = GL_RGBA16;
                i._type = GL_UNSIGNED_SHORT;
                bytesPerElement = 8;
                break;

            case FOURCC_L8:
                i._format = GL_RED;
                i._internalFormat = GL_R8;
                i._type = GL_UNSIGNED_BYTE;
                bytesPerElement = 1;
                break;

            case FOURCC_A8L8:
                i._format = GL_RG;
                i._internalFormat = GL_RG8;
                i._type = GL_UNSIGNED_BYTE;
                bytesPerElement = 2;
                break;

            case FOURCC_L16:
                i._format = GL_RED;
                i._internalFormat = GL_R16;
                i._type = GL_UNSIGNED_SHORT;
                bytesPerElement = 2;
                break;

            case FOURCC_R16F:
                i._format = GL_RED; //should use red, once it is available
                i._internalFormat = GL_R16F; 
                i._type = GL_HALF_FLOAT;
                bytesPerElement = 2;
                break;

            case FOURCC_A16B16G16R16F:
                i._format = GL_RGBA;
                i._internalFormat = GL_RGBA16F;
                i._type = GL_HALF_FLOAT;
                bytesPerElement = 8;
                break;

            case FOURCC_R32F:
                i._format = GL_RED; //should use red, once it is available
                i._internalFormat = GL_R32F; 
                i._type = GL_FLOAT;
                bytesPerElement = 4;
                break;

            case FOURCC_A32B32G32R32F:
                i._format = GL_RGBA;
                i._internalFormat = GL_RGBA32F;
                i._type = GL_FLOAT;
                bytesPerElement = 16;
                break;

            case FOURCC_UNKNOWN:
            case FOURCC_X1R5G5B5:
            case FOURCC_A1R5G5B5:
            case FOURCC_A4R4G4B4:
            case FOURCC_R3G3B2:
            case FOURCC_A8R3G3B2:
            case FOURCC_X4R4G4B4:
            case FOURCC_A4L4:
            case FOURCC_D16_LOCKABLE:
            case FOURCC_D32:
            case FOURCC_D24X8:
            case FOURCC_D16:
            case FOURCC_D32F_LOCKABLE:
            case FOURCC_G16R16:
            case FOURCC_G16R16F:
            case FOURCC_G32R32F:
                //these are unsupported for now
            default:
                fclose(fp);
                return false;
        }
    }
    else if (ddsh.ddspf.dwFlags == DDSF_RGBA && ddsh.ddspf.dwRGBBitCount == 32)
    {
        i._format = GL_BGRA;
        i._internalFormat = GL_RGBA8;
        i._type = GL_UNSIGNED_BYTE;
        bytesPerElement = 4;
    }
    else if (ddsh.ddspf.dwFlags == DDSF_RGB  && ddsh.ddspf.dwRGBBitCount == 32)
    {
        i._format = GL_BGR;
        i._internalFormat = GL_RGBA8;
        i._type = GL_UNSIGNED_BYTE;
        bytesPerElement = 4;
    }
    else if (ddsh.ddspf.dwFlags == DDSF_RGB  && ddsh.ddspf.dwRGBBitCount == 24)
    {
        i._format = GL_BGR;
        i._internalFormat = GL_RGB8;
        i._type = GL_UNSIGNED_BYTE;
        bytesPerElement = 3;
    }
    else if (ddsh.ddspf.dwRGBBitCount == 8)
    {
        i._format = GL_RED; 
        i._internalFormat = GL_R8; 
        i._type = GL_UNSIGNED_BYTE;
        bytesPerElement = 1;
    }
    else 
    {
        fclose(fp);
        return false;
    }

    i._elementSize = bytesPerElement;

    for (int face = 0; face < ((i._faces) ? i._faces : 1); face++) {
        int w = i._width, h = i._height, d = (i._depth) ? i._depth : 1;
        for (int level = 0; level < i._levelCount; level++) {
            int bw = (btcCompressed) ? (w+3)/4 : w;
            int bh = (btcCompressed) ? (h+3)/4 : h;
            int size = bw*bh*d*bytesPerElement;

            GLubyte *data = new GLubyte[size];

            fread( data, size, 1, fp);

            i._data.push_back(data);

            if (i._faces != 6)
                i.flipSurface( data, w, h, d);

            //reduce mip sizes
            w = ( w > 1) ? w >> 1 : 1;
            h = ( h > 1) ? h >> 1 : 1;
            d = ( d > 1) ? d >> 1 : 1;
        }
    }
/*
    //reverse cube map y faces
    if (i._faces == 6) {
        for (int level = 0; level < i._levelCount; level++) {
            GLubyte *temp = i._data[2*i._levelCount + level];
            i._data[2*i._levelCount + level] = i._data[3*i._levelCount + level];
            i._data[3*i._levelCount + level] = temp;
        }
    }
  */  

    fclose(fp);
    return true;
}

//
// flip a DXT1 color block
////////////////////////////////////////////////////////////
void Image::flip_blocks_dxtc1(GLubyte *ptr, unsigned int numBlocks)
{
    DXTColBlock *curblock = (DXTColBlock*)ptr;
    GLubyte temp;

    for (unsigned int i = 0; i < numBlocks; i++) {
        temp = curblock->row[0];
        curblock->row[0] = curblock->row[3];
        curblock->row[3] = temp;
        temp = curblock->row[1];
        curblock->row[1] = curblock->row[2];
        curblock->row[2] = temp;

        curblock++;
    }
}

//
// flip a DXT3 color block
////////////////////////////////////////////////////////////
void Image::flip_blocks_dxtc3(GLubyte *ptr, unsigned int numBlocks)
{
    DXTColBlock *curblock = (DXTColBlock*)ptr;
    DXT3AlphaBlock *alphablock;
    GLushort tempS;
    GLubyte tempB;

    for (unsigned int i = 0; i < numBlocks; i++)
    {
        alphablock = (DXT3AlphaBlock*)curblock;

        tempS = alphablock->row[0];
        alphablock->row[0] = alphablock->row[3];
        alphablock->row[3] = tempS;
        tempS = alphablock->row[1];
        alphablock->row[1] = alphablock->row[2];
        alphablock->row[2] = tempS;

        curblock++;

        tempB = curblock->row[0];
        curblock->row[0] = curblock->row[3];
        curblock->row[3] = tempB;
        tempB = curblock->row[1];
        curblock->row[1] = curblock->row[2];
        curblock->row[2] = tempB;

        curblock++;
    }
}

//
// flip a DXT5 alpha block
////////////////////////////////////////////////////////////
void flip_dxt5_alpha(DXT5AlphaBlock *block)
{
    GLubyte gBits[4][4];
    
    const uint32_t mask = 0x00000007;          // bits = 00 00 01 11
    uint32_t bits = 0;
    memcpy(&bits, &block->row[0], sizeof(unsigned char) * 3);

    gBits[0][0] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[0][1] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[0][2] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[0][3] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[1][0] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[1][1] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[1][2] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[1][3] = (GLubyte)(bits & mask);

    bits = 0;
    memcpy(&bits, &block->row[3], sizeof(GLubyte) * 3);

    gBits[2][0] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[2][1] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[2][2] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[2][3] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[3][0] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[3][1] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[3][2] = (GLubyte)(bits & mask);
    bits >>= 3;
    gBits[3][3] = (GLubyte)(bits & mask);

    // clear existing alpha bits
    memset(block->row, 0, sizeof(GLubyte) * 6);

    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]));

    *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 Image::flip_blocks_dxtc5(GLubyte *ptr, unsigned int numBlocks)
{
    DXTColBlock *curblock = (DXTColBlock*)ptr;
    DXT5AlphaBlock *alphablock;
    GLubyte temp;
    
    for (unsigned int i = 0; i < numBlocks; i++)
    {
        alphablock = (DXT5AlphaBlock*)curblock;
        
        flip_dxt5_alpha(alphablock);

        curblock++;

        temp = curblock->row[0];
        curblock->row[0] = curblock->row[3];
        curblock->row[3] = temp;
        temp = curblock->row[1];
        curblock->row[1] = curblock->row[2];
        curblock->row[2] = temp;

        curblock++;
    }
}


}