mojodds.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 10 May 2011 14:23:00 -0400
changeset 11 7e2785b01d10
parent 10 1e5f3d130b53
permissions -rw-r--r--
Fixed (I think?) linear size calculation for S3TC/DXTC data.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/**
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * MojoDDS; tools for dealing with DDS files.
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon.
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 */
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
// Specs on DDS format: http://msdn.microsoft.com/en-us/library/bb943991.aspx/
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <stdio.h>
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include <string.h>
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include <stdlib.h>
7
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
    14
#include <assert.h>
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#ifdef _MSC_VER
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
typedef unsigned __int8 uint8;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
typedef unsigned __int32 uint32;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#else
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
#include <stdint.h>
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
typedef uint8_t uint8;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
typedef uint32_t uint32;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#endif
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#define DDS_MAGIC 0x20534444  // 'DDS ' in littleendian.
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#define DDS_HEADERSIZE 124
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
#define DDS_PIXFMTSIZE 32
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#define DDSD_CAPS 0x1
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
#define DDSD_HEIGHT 0x2
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#define DDSD_WIDTH 0x4
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#define DDSD_PITCH 0x8
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#define DDSD_FMT 0x1000
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#define DDSD_MIPMAPCOUNT 0x20000
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#define DDSD_LINEARSIZE 0x80000
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#define DDSD_DEPTH 0x800000
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#define DDSD_REQ (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_FMT)
8
506dbbccee95 Ignore DDSCAPS_ALPHA flag.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    39
#define DDSCAPS_ALPHA 0x2
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
#define DDSCAPS_COMPLEX 0x8
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#define DDSCAPS_MIPMAP 0x400000
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
#define DDSCAPS_TEXTURE 0x1000
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#define DDPF_ALPHAPIXELS 0x1
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#define DDPF_ALPHA 0x2
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#define DDPF_FOURCC 0x4
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#define DDPF_RGB 0x40
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#define DDPF_YUV 0x200
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
#define DDPF_LUMINANCE 0x20000
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
#define FOURCC_DXT1 0x31545844
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
#define FOURCC_DXT2 0x32545844
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
#define FOURCC_DXT3 0x33545844
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
#define FOURCC_DXT4 0x34545844
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
#define FOURCC_DXT5 0x35545844
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
#define FOURCC_DX10 0x30315844
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
    57
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
    58
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
    59
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
    60
#define GL_BGR 0x80E0
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
    61
#define GL_BGRA 0x80E1
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
    62
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
typedef struct
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
{
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    uint32 dwSize;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    uint32 dwFlags;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    uint32 dwFourCC;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    uint32 dwRGBBitCount;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    uint32 dwRBitMask;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    uint32 dwGBitMask;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
    uint32 dwBBitMask;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    uint32 dwABitMask;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
} MOJODDS_PixelFormat;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
typedef struct
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
{
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    uint32 dwSize;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    uint32 dwFlags;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    uint32 dwHeight;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    uint32 dwWidth;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    uint32 dwPitchOrLinearSize;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    uint32 dwDepth;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    uint32 dwMipMapCount;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    uint32 dwReserved1[11];
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    MOJODDS_PixelFormat ddspf;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    uint32 dwCaps;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    uint32 dwCaps2;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    uint32 dwCaps3;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    uint32 dwCaps4;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    uint32 dwReserved2;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
} MOJODDS_Header;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
static uint32 readui32(const uint8 **_ptr, size_t *_len)
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
{
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    uint32 retval = 0;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    if (*_len < sizeof (retval))
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
        *_len = 0;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    else
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    {
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
        const uint8 *ptr = *_ptr;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
        retval = (((uint32) ptr[0]) <<  0) | (((uint32) ptr[1]) <<  8) |
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
                 (((uint32) ptr[2]) << 16) | (((uint32) ptr[3]) << 24) ;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
        *_ptr += sizeof (retval);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
        *_len -= sizeof (retval);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
    } // else
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    return retval;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
} // readui32
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   110
static int parse_dds(MOJODDS_Header *header, const uint8 **ptr, size_t *len,
10
1e5f3d130b53 MipMap support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
   111
                     unsigned int *_glfmt, unsigned int *_miplevels)
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
{
7
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   113
    const uint32 pitchAndLinear = (DDSD_PITCH | DDSD_LINEARSIZE);
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   114
    uint32 width = 0;
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   115
    uint32 height = 0;
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   116
    uint32 calcSize = 0;
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   117
    uint32 calcSizeFlag = DDSD_LINEARSIZE;
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    int i;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    // Files start with magic value...
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    if (readui32(ptr, len) != DDS_MAGIC)
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
        return 0;  // not a DDS file.
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
    // Then comes the DDS header...
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    if (*len < DDS_HEADERSIZE)
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
        return 0;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
    header->dwSize = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
    header->dwFlags = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    header->dwHeight = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    header->dwWidth = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    header->dwPitchOrLinearSize = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    header->dwDepth = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    header->dwMipMapCount = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
    for (i = 0; i < STATICARRAYLEN(header->dwReserved1); i++)
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
        header->dwReserved1[i] = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    header->ddspf.dwSize = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    header->ddspf.dwFlags = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    header->ddspf.dwFourCC = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    header->ddspf.dwRGBBitCount = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    header->ddspf.dwRBitMask = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    header->ddspf.dwGBitMask = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    header->ddspf.dwBBitMask = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    header->ddspf.dwABitMask = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    header->dwCaps = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    header->dwCaps2 = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    header->dwCaps3 = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    header->dwCaps4 = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    header->dwReserved2 = readui32(ptr, len);
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
7
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   151
    width = header->dwWidth;
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   152
    height = header->dwHeight;
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   153
8
506dbbccee95 Ignore DDSCAPS_ALPHA flag.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   154
    header->dwCaps &= ~DDSCAPS_ALPHA;  // we'll get this from the pixel format.
506dbbccee95 Ignore DDSCAPS_ALPHA flag.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   155
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    if (header->dwSize != DDS_HEADERSIZE)   // header size must be 124.
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
        return 0;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    else if (header->ddspf.dwSize != DDS_PIXFMTSIZE)   // size must be 32.
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
        return 0;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    else if ((header->dwFlags & DDSD_REQ) != DDSD_REQ)  // must have these bits.
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
        return 0;
10
1e5f3d130b53 MipMap support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
   162
    else if ((header->dwCaps & DDSCAPS_TEXTURE) == 0)
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
        return 0;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    else if (header->dwCaps2 != 0)  // !!! FIXME (non-zero with other bits in dwCaps set)
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
        return 0;
7
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   166
    else if ((header->dwFlags & pitchAndLinear) == pitchAndLinear)
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   167
        return 0;  // can't specify both.
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
10
1e5f3d130b53 MipMap support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
   169
    *_miplevels = (header->dwCaps & DDSCAPS_MIPMAP) ? header->dwMipMapCount : 1;
1e5f3d130b53 MipMap support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
   170
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    if (header->ddspf.dwFlags & DDPF_FOURCC)
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    {
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
        switch (header->ddspf.dwFourCC)
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
        {
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
            case FOURCC_DXT1:
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   176
                *_glfmt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
11
7e2785b01d10 Fixed (I think?) linear size calculation for S3TC/DXTC data.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
   177
                calcSize = ((width ? ((width + 3) / 4) : 1) * 8) *
7e2785b01d10 Fixed (I think?) linear size calculation for S3TC/DXTC data.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
   178
                           (height ? ((height + 3) / 4) : 1);
7
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   179
                break;
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
            case FOURCC_DXT3:
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   181
                *_glfmt = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
11
7e2785b01d10 Fixed (I think?) linear size calculation for S3TC/DXTC data.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
   182
                calcSize = ((width ? ((width + 3) / 4) : 1) * 16) *
7e2785b01d10 Fixed (I think?) linear size calculation for S3TC/DXTC data.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
   183
                           (height ? ((height + 3) / 4) : 1);
7
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   184
                break;
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   185
            case FOURCC_DXT5:
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   186
                *_glfmt = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
11
7e2785b01d10 Fixed (I think?) linear size calculation for S3TC/DXTC data.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
   187
                calcSize = ((width ? ((width + 3) / 4) : 1) * 16) *
7e2785b01d10 Fixed (I think?) linear size calculation for S3TC/DXTC data.
Ryan C. Gordon <icculus@icculus.org>
parents: 10
diff changeset
   188
                           (height ? ((height + 3) / 4) : 1);
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   189
                break;
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   190
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
            // !!! FIXME: DX10 is an extended header, introduced by DirectX 10.
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   192
            //case FOURCC_DX10: do_something(); break;
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   193
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   194
            //case FOURCC_DXT2:  // premultiplied alpha unsupported.
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   195
            //case FOURCC_DXT4:  // premultiplied alpha unsupported.
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
            default:
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
                return 0;  // unsupported data format.
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
        } // switch
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
    } // if
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   201
    // no FourCC...uncompressed data.
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   202
    else if (header->ddspf.dwFlags & DDPF_RGB)
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   203
    {
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   204
        if ( (header->ddspf.dwRBitMask != 0x00FF0000) ||
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   205
             (header->ddspf.dwGBitMask != 0x0000FF00) ||
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   206
             (header->ddspf.dwBBitMask != 0x000000FF) )
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   207
            return 0;  // !!! FIXME: deal with this.
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   208
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   209
        if (header->ddspf.dwFlags & DDPF_ALPHAPIXELS)
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   210
        {
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   211
            if ( (header->ddspf.dwRGBBitCount != 32) ||
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   212
                 (header->ddspf.dwABitMask != 0xFF000000) )
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   213
                return 0;  // unsupported.
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   214
            *_glfmt = GL_BGRA;
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   215
        } // if
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   216
        else
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   217
        {
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   218
            if (header->ddspf.dwRGBBitCount != 24)
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   219
                return 0;  // unsupported.
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   220
            *_glfmt = GL_BGR;
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   221
        } // else
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   222
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   223
        calcSizeFlag = DDSD_PITCH;
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   224
        calcSize = ((width * header->ddspf.dwRGBBitCount) + 7) / 8;
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   225
    } // else if
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   226
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   227
    //else if (header->ddspf.dwFlags & DDPF_LUMINANCE)  // !!! FIXME
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   228
    //else if (header->ddspf.dwFlags & DDPF_YUV)  // !!! FIXME
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   229
    //else if (header->ddspf.dwFlags & DDPF_ALPHA)  // !!! FIXME
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   230
    else
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   231
    {
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   232
        return 0;  // unsupported data format.
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   233
    } // else if
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   234
7
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   235
    // no pitch or linear size? Calculate it.
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   236
    if ((header->dwFlags & pitchAndLinear) == 0)
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   237
    {
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   238
        if (!calcSizeFlag)
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   239
        {
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   240
            assert(0 && "should have caught this up above");
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   241
            return 0;  // uh oh.
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   242
        } // if
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   243
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   244
        header->dwPitchOrLinearSize = calcSize;
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   245
        header->dwFlags |= calcSizeFlag;
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   246
    } // if
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   247
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
    return 1;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
} // parse_dds
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
// !!! FIXME: improve the crap out of this API later.
5
ab7382a45d11 Added MOJODDS_isDDS().
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   253
int MOJODDS_isDDS(const void *_ptr, const unsigned long _len)
ab7382a45d11 Added MOJODDS_isDDS().
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   254
{
ab7382a45d11 Added MOJODDS_isDDS().
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   255
    size_t len = (size_t) _len;
ab7382a45d11 Added MOJODDS_isDDS().
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   256
    const uint8 *ptr = (const uint8 *) _ptr;
ab7382a45d11 Added MOJODDS_isDDS().
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   257
    return (readui32(&ptr, &len) == DDS_MAGIC);
7
341c4de6ffd3 Deal with DDS files that specify neither pitch nor linearsize.
Ryan C. Gordon <icculus@icculus.org>
parents: 5
diff changeset
   258
} // MOJODDS_isDDS
5
ab7382a45d11 Added MOJODDS_isDDS().
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   259
1
90603fda7444 Make buffer sizes unsigned.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   260
int MOJODDS_getTexture(const void *_ptr, const unsigned long _len,
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   261
                       const void **_tex, unsigned long *_texlen,
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   262
                       unsigned int *_glfmt, unsigned int *_w,
10
1e5f3d130b53 MipMap support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
   263
                       unsigned int *_h, unsigned int *_miplevels)
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
{
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    size_t len = (size_t) _len;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    const uint8 *ptr = (const uint8 *) _ptr;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
    MOJODDS_Header header;
10
1e5f3d130b53 MipMap support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
   268
    if (!parse_dds(&header, &ptr, &len, _glfmt, _miplevels))
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
        return 0;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
    *_tex = (const void *) ptr;
2
3c16e81ac188 Added a width and height query.
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
   272
    *_w = (unsigned int) header.dwWidth;
3c16e81ac188 Added a width and height query.
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
   273
    *_h = (unsigned int) header.dwHeight;
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   274
    *_texlen = (unsigned long) header.dwPitchOrLinearSize;
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
9
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   276
    if (header.dwFlags & DDSD_PITCH)
50157857e7d8 Deal with uncompressed data, and pass GL format value.
Ryan C. Gordon <icculus@icculus.org>
parents: 8
diff changeset
   277
        *_texlen *= header.dwHeight;
0
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    return 1;
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
} // MOJODDS_getTexture
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
// end of mojodds.c ...
8daba3cc1efb Initial bit of code I scratched out. Not nearly complete, no real API yet.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283