FS2_Open
Open source remastering of the Freespace 2 engine
ddsutils.h
Go to the documentation of this file.
1 //header file to load .dds files
2 //these use dxtc texture compression
3 
4 #ifndef __DDS_H
5 #define __DDS_H
6 
7 #include "globalincs/pstypes.h"
8 #include "cfile/cfile.h"
9 
10 
11 #define DDS_ERROR_NONE 0 // everything went fine
12 #define DDS_ERROR_INVALID_FILENAME 1 // bad or missing file
13 #define DDS_ERROR_UNSUPPORTED 2 // a known format but one we don't support
14 #define DDS_ERROR_INVALID_FORMAT 3 // format that's not supported
15 #define DDS_ERROR_BAD_HEADER 4 // header was not "DDS "
16 #define DDS_ERROR_NO_COMPRESSION 5 // file is compressed, compression isn't supported
17 #define DDS_ERROR_NON_POWER_OF_2 6 // file is not a power of 2 in dimensions
18 #define DDS_ERROR_CUBEMAP_FACES 7 // file is a cubemap, but doesn't have all six faces
19 
20 
21 #define DDS_DXT_INVALID -1
22 #define DDS_UNCOMPRESSED 0
23 #define DDS_DXT1 1
24 #define DDS_DXT3 3
25 #define DDS_DXT5 5
26 #define DDS_CUBEMAP_UNCOMPRESSED 10
27 #define DDS_CUBEMAP_DXT1 11
28 #define DDS_CUBEMAP_DXT3 13
29 #define DDS_CUBEMAP_DXT5 15
30 
31 #ifndef MAKEFOURCC
32  #define MAKEFOURCC(ch0, ch1, ch2, ch3) \
33  ((uint)(ubyte)(ch0) | ((uint)(ubyte)(ch1) << 8) | \
34  ((uint)(ubyte)(ch2) << 16) | ((uint)(ubyte)(ch3) << 24 ))
35 #endif //defined(MAKEFOURCC)
36 
37 // FOURCC codes for DX compressed-texture pixel formats
38 #define FOURCC_DXT1 (MAKEFOURCC('D','X','T','1'))
39 #define FOURCC_DXT2 (MAKEFOURCC('D','X','T','2'))
40 #define FOURCC_DXT3 (MAKEFOURCC('D','X','T','3'))
41 #define FOURCC_DXT4 (MAKEFOURCC('D','X','T','4'))
42 #define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5'))
43 
44 #define DDS_FILECODE 0x20534444 // "DDS " in file
45 
46 // DDS format stuff ...
47 #define DDSD_LINEARSIZE 0x00080000
48 #define DDSD_PITCH 0x00000008
49 #define DDPF_ALPHAPIXELS 0x00000001
50 #define DDPF_FOURCC 0x00000004
51 #define DDPF_PALETTEINDEXED4 0x00000008
52 #define DDPF_PALETTEINDEXEDTO8 0x00000010
53 #define DDPF_PALETTEINDEXED8 0x00000020
54 #define DDPF_RGB 0x00000040
55 #define DDSD_PIXELFORMAT 0x00001000
56 #define DDSD_WIDTH 0x00000004
57 #define DDSD_HEIGHT 0x00000002
58 #define DDSD_CAPS 0x00000001
59 #define DDSD_MIPMAPCOUNT 0x00020000
60 
61 #define DDSCAPS_COMPLEX 0x00000008
62 #define DDSCAPS_PRIMARYSURFACE 0x00000200
63 #define DDSCAPS_MIPMAP 0x00400000
64 #define DDSCAPS_TEXTURE 0x00001000
65 
66 #define DDSCAPS2_CUBEMAP 0x00000200
67 #define DDSCAPS2_VOLUME 0x00200000
68 #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
69 #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
70 #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
71 #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
72 #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
73 #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
74 
75 #define DDSCAPS2_CUBEMAP_ALLFACES ( DDSCAPS2_CUBEMAP_POSITIVEX | \
76  DDSCAPS2_CUBEMAP_NEGATIVEX | \
77  DDSCAPS2_CUBEMAP_POSITIVEY | \
78  DDSCAPS2_CUBEMAP_NEGATIVEY | \
79  DDSCAPS2_CUBEMAP_POSITIVEZ | \
80  DDSCAPS2_CUBEMAP_NEGATIVEZ )
81 
82 #pragma pack(1)
83 // these structures are the headers for a dds file
84 /*typedef struct _DDPIXELFORMAT {
85  uint dwSize;
86  uint dwFlags;
87  uint dwFourCC;
88  uint dwRGBBitCount;
89  uint dwRBitMask;
90  uint dwGBitMask;
91  uint dwBBitMask;
92  uint dwRGBAlphaBitMask;
93 } DDPIXELFORMAT;
94 
95 typedef struct _DDSCAPS2
96 {
97  uint dwCaps1;
98  uint dwCaps2;
99  uint Reserved[2];
100 } DDSCAPS2;*/
101 
102 typedef struct _DDSURFACEDESC2
103 {
104  uint dwSize; // size of the DDSURFACEDESC structure
105  uint dwFlags; // determines what fields are valid
106  uint dwHeight; // height of surface to be created
107  uint dwWidth; // width of input surface
112 
113  struct {
114  uint dwSize;
115  uint dwFlags;
122  } ddpfPixelFormat;
123 
124  struct {
128  } ddsCaps;
129 
130 // DDPIXELFORMAT ddpfPixelFormat;
131 // DDSCAPS2 ddsCaps; // direct draw surface capabilities
134 #pragma pack()
135 
136 #define DDS_OFFSET 4+sizeof(DDSURFACEDESC2) //place where the data starts -- should be 128
137 
138 //reads a dds header
139 //returns one of the error values
140 //'compression_type' comes back as one of the DDS_DXTC* defines
141 int dds_read_header(const char *filename, CFILE *img_cfp = NULL, int *width = 0, int *height = 0, int *bpp = 0, int *compression_type = 0, int *levels = 0, int *size = 0, ubyte *palette = NULL);
142 
143 //reads bitmap
144 //size of the data it stored in size
145 int dds_read_bitmap(const char *filename, ubyte *data, ubyte *bpp = NULL, int cf_type = CF_TYPE_ANY);
146 
147 // writes a DDS file using given data
148 void dds_save_image(int width, int height, int bpp, int num_mipmaps, ubyte *data = NULL, int cubemap = 0, const char *filename = NULL);
149 
150 //returns a string from a DDS error code
151 const char *dds_error_string(int code);
152 
154 extern int Use_compressed_textures;
155 
156 #endif //__DDS_H
uint dwRGBAlphaBitMask
Definition: ddsutils.h:121
Definition: cfile.h:28
int Texture_compression_available
Definition: ddsutils.cpp:19
GLsizeiptr size
Definition: Glext.h:5496
void dds_save_image(int width, int height, int bpp, int num_mipmaps, ubyte *data=NULL, int cubemap=0, const char *filename=NULL)
Definition: ddsutils.cpp:324
GLint GLsizei width
Definition: Gl.h:1505
HWND DWORD code
Definition: vddraw.h:425
int dds_read_header(const char *filename, CFILE *img_cfp=NULL, int *width=0, int *height=0, int *bpp=0, int *compression_type=0, int *levels=0, int *size=0, ubyte *palette=NULL)
Definition: ddsutils.cpp:30
unsigned int uint
Definition: pstypes.h:64
char * filename
uint dwMipMapCount
Definition: ddsutils.h:110
int dds_read_bitmap(const char *filename, ubyte *data, ubyte *bpp=NULL, int cf_type=CF_TYPE_ANY)
Definition: ddsutils.cpp:275
unsigned char ubyte
Definition: pstypes.h:62
uint dwRGBBitCount
Definition: ddsutils.h:117
uint Reserved[2]
Definition: ddsutils.h:127
int Use_compressed_textures
Definition: ddsutils.cpp:20
const char * dds_error_string(int code)
Definition: ddsutils.cpp:467
struct _DDSURFACEDESC2 DDSURFACEDESC2
GLint GLsizei GLsizei height
Definition: Gl.h:1505
struct _DDSURFACEDESC2::@14 ddsCaps
uint dwReserved2
Definition: ddsutils.h:132
struct _DDSURFACEDESC2::@13 ddpfPixelFormat
GLenum GLsizei GLenum GLenum const GLvoid * data
Definition: Gl.h:1509
uint dwPitchOrLinearSize
Definition: ddsutils.h:108
#define CF_TYPE_ANY
Definition: cfile.h:42
uint dwReserved1[11]
Definition: ddsutils.h:111