1 | /* |
2 | Jonathan Dummer |
3 | 2007-07-31-10.32 |
4 | |
5 | simple DXT compression / decompression code |
6 | |
7 | public domain |
8 | */ |
9 | |
10 | #ifndef HEADER_IMAGE_DXT |
11 | #define |
12 | |
13 | /** |
14 | Converts an image from an array of unsigned chars (RGB or RGBA) to |
15 | DXT1 or DXT5, then saves the converted image to disk. |
16 | \return 0 if failed, otherwise returns 1 |
17 | **/ |
18 | int |
19 | save_image_as_DDS |
20 | ( |
21 | const char *filename, |
22 | int width, int height, int channels, |
23 | const unsigned char *const data |
24 | ); |
25 | |
26 | /** |
27 | take an image and convert it to DXT1 (no alpha) |
28 | **/ |
29 | unsigned char* |
30 | convert_image_to_DXT1 |
31 | ( |
32 | const unsigned char *const uncompressed, |
33 | int width, int height, int channels, |
34 | int *out_size |
35 | ); |
36 | |
37 | /** |
38 | take an image and convert it to DXT5 (with alpha) |
39 | **/ |
40 | unsigned char* |
41 | convert_image_to_DXT5 |
42 | ( |
43 | const unsigned char *const uncompressed, |
44 | int width, int height, int channels, |
45 | int *out_size |
46 | ); |
47 | |
48 | /** A bunch of DirectDraw Surface structures and flags **/ |
49 | typedef struct |
50 | { |
51 | unsigned int dwMagic; |
52 | unsigned int dwSize; |
53 | unsigned int dwFlags; |
54 | unsigned int dwHeight; |
55 | unsigned int dwWidth; |
56 | unsigned int dwPitchOrLinearSize; |
57 | unsigned int dwDepth; |
58 | unsigned int dwMipMapCount; |
59 | unsigned int dwReserved1[ 11 ]; |
60 | |
61 | /* DDPIXELFORMAT */ |
62 | struct |
63 | { |
64 | unsigned int dwSize; |
65 | unsigned int dwFlags; |
66 | unsigned int dwFourCC; |
67 | unsigned int dwRGBBitCount; |
68 | unsigned int dwRBitMask; |
69 | unsigned int dwGBitMask; |
70 | unsigned int dwBBitMask; |
71 | unsigned int dwAlphaBitMask; |
72 | } |
73 | sPixelFormat; |
74 | |
75 | /* DDCAPS2 */ |
76 | struct |
77 | { |
78 | unsigned int dwCaps1; |
79 | unsigned int dwCaps2; |
80 | unsigned int dwDDSX; |
81 | unsigned int dwReserved; |
82 | } |
83 | sCaps; |
84 | unsigned int dwReserved2; |
85 | } |
86 | ; |
87 | |
88 | /* the following constants were copied directly off the MSDN website */ |
89 | |
90 | /* The dwFlags member of the original DDSURFACEDESC2 structure |
91 | can be set to one or more of the following values. */ |
92 | #define DDSD_CAPS 0x00000001 |
93 | #define DDSD_HEIGHT 0x00000002 |
94 | #define DDSD_WIDTH 0x00000004 |
95 | #define DDSD_PITCH 0x00000008 |
96 | #define DDSD_PIXELFORMAT 0x00001000 |
97 | #define DDSD_MIPMAPCOUNT 0x00020000 |
98 | #define DDSD_LINEARSIZE 0x00080000 |
99 | #define DDSD_DEPTH 0x00800000 |
100 | |
101 | /* DirectDraw Pixel Format */ |
102 | #define DDPF_ALPHAPIXELS 0x00000001 |
103 | #define DDPF_FOURCC 0x00000004 |
104 | #define DDPF_RGB 0x00000040 |
105 | |
106 | /* The dwCaps1 member of the DDSCAPS2 structure can be |
107 | set to one or more of the following values. */ |
108 | #define DDSCAPS_COMPLEX 0x00000008 |
109 | #define DDSCAPS_TEXTURE 0x00001000 |
110 | #define DDSCAPS_MIPMAP 0x00400000 |
111 | |
112 | /* The dwCaps2 member of the DDSCAPS2 structure can be |
113 | set to one or more of the following values. */ |
114 | #define DDSCAPS2_CUBEMAP 0x00000200 |
115 | #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400 |
116 | #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800 |
117 | #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000 |
118 | #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000 |
119 | #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000 |
120 | #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000 |
121 | #define DDSCAPS2_VOLUME 0x00200000 |
122 | |
123 | #endif /* HEADER_IMAGE_DXT */ |
124 | |