1 //-----------------------------------------------------------------------------
2 //
3 // ImageLib Sources
4 // Copyright (C) 2000-2008 by Denton Woods
5 // Last modified: 12/27/2008
6 //
7 // Filename: src-IL/include/il_dds.h
8 //
9 // Description: Reads from a DirectDraw Surface (.dds) file.
10 //
11 //-----------------------------------------------------------------------------
12 
13 
14 #ifndef DDS_H
15 #define DDS_H
16 
17 #include "il_internal.h"
18 
19 
20 #ifdef _WIN32
21 	#pragma pack(push, dds_struct, 1)
22 #endif
23 typedef struct DDSHEAD
24 {
25 	ILbyte	Signature[4];
26 
27 	ILuint	Size1;				// size of the structure (minus MagicNum)
28 	ILuint	Flags1; 			// determines what fields are valid
29 	ILuint	Height; 			// height of surface to be created
30 	ILuint	Width;				// width of input surface
31 	ILuint	LinearSize; 		// Formless late-allocated optimized surface size
32 	ILuint	Depth;				// Depth if a volume texture
33 	ILuint	MipMapCount;		// number of mip-map levels requested
34 	ILuint	AlphaBitDepth;		// depth of alpha buffer requested
35 
36 	ILuint	NotUsed[10];
37 
38 	ILuint	Size2;				// size of structure
39 	ILuint	Flags2;				// pixel format flags
40 	ILuint	FourCC;				// (FOURCC code)
41 	ILuint	RGBBitCount;		// how many bits per pixel
42 	ILuint	RBitMask;			// mask for red bit
43 	ILuint	GBitMask;			// mask for green bits
44 	ILuint	BBitMask;			// mask for blue bits
45 	ILuint	RGBAlphaBitMask;	// mask for alpha channel
46 
47 	ILuint	ddsCaps1, ddsCaps2, ddsCaps3, ddsCaps4; // direct draw surface capabilities
48 	ILuint	TextureStage;
49 } IL_PACKSTRUCT DDSHEAD;
50 #ifdef _WIN32
51 	#pragma pack(pop, dds_struct)
52 #endif
53 
54 
55 
56 // use cast to struct instead of RGBA_MAKE as struct is
57 //  much
58 typedef struct Color8888
59 {
60 	ILubyte r;		// change the order of names to change the
61 	ILubyte g;		//  order of the output ARGB or BGRA, etc...
62 	ILubyte b;		//  Last one is MSB, 1st is LSB.
63 	ILubyte a;
64 } Color8888;
65 
66 
67 typedef struct Color888
68 {
69 	ILubyte r;		// change the order of names to change the
70 	ILubyte g;		//  order of the output ARGB or BGRA, etc...
71 	ILubyte b;		//  Last one is MSB, 1st is LSB.
72 } Color888;
73 
74 
75 typedef struct Color565
76 {
77 	unsigned nBlue  : 5;		// order of names changes
78 	unsigned nGreen : 6;		//  byte order of output to 32 bit
79 	unsigned nRed	: 5;
80 } Color565;
81 
82 
83 typedef struct DXTColBlock
84 {
85 	ILshort col0;
86 	ILshort col1;
87 
88 	// no bit fields - use bytes
89 	ILbyte row[4];
90 } DXTColBlock;
91 
92 typedef struct DXTAlphaBlockExplicit
93 {
94 	ILshort row[4];
95 } DXTAlphaBlockExplicit;
96 
97 typedef struct DXTAlphaBlock3BitLinear
98 {
99 	ILbyte alpha0;
100 	ILbyte alpha1;
101 
102 	ILbyte stuff[6];
103 } DXTAlphaBlock3BitLinear;
104 
105 
106 // Defines
107 
108 //Those 4 were added on 20040516 to make
109 //the written dds files more standard compliant
110 #define DDS_CAPS				0x00000001L
111 #define DDS_HEIGHT				0x00000002L
112 #define DDS_WIDTH				0x00000004L
113 
114 #define DDS_RGB					0x00000040L
115 #define DDS_PIXELFORMAT			0x00001000L
116 
117 #define DDS_LUMINANCE			0x00020000L
118 
119 #define DDS_ALPHAPIXELS			0x00000001L
120 #define DDS_ALPHA				0x00000002L
121 #define DDS_FOURCC				0x00000004L
122 #define DDS_PITCH				0x00000008L
123 #define DDS_COMPLEX				0x00000008L
124 #define DDS_TEXTURE				0x00001000L
125 #define DDS_MIPMAPCOUNT			0x00020000L
126 #define DDS_LINEARSIZE			0x00080000L
127 #define DDS_VOLUME				0x00200000L
128 #define DDS_MIPMAP				0x00400000L
129 #define DDS_DEPTH				0x00800000L
130 
131 #define DDS_CUBEMAP				0x00000200L
132 #define DDS_CUBEMAP_POSITIVEX	0x00000400L
133 #define DDS_CUBEMAP_NEGATIVEX	0x00000800L
134 #define DDS_CUBEMAP_POSITIVEY	0x00001000L
135 #define DDS_CUBEMAP_NEGATIVEY	0x00002000L
136 #define DDS_CUBEMAP_POSITIVEZ	0x00004000L
137 #define DDS_CUBEMAP_NEGATIVEZ	0x00008000L
138 
139 
140 #define IL_MAKEFOURCC(ch0, ch1, ch2, ch3) \
141 			((ILint)(ILbyte)(ch0) | ((ILint)(ILbyte)(ch1) << 8) |	\
142 			((ILint)(ILbyte)(ch2) << 16) | ((ILint)(ILbyte)(ch3) << 24 ))
143 
144 enum PixFormat
145 {
146 	PF_ARGB,
147 	PF_RGB,
148 	PF_DXT1,
149 	PF_DXT2,
150 	PF_DXT3,
151 	PF_DXT4,
152 	PF_DXT5,
153 	PF_3DC,
154 	PF_ATI1N,
155 	PF_LUMINANCE,
156 	PF_LUMINANCE_ALPHA,
157 	PF_RXGB, //Doom3 normal maps
158 	PF_A16B16G16R16,
159 	PF_R16F,
160 	PF_G16R16F,
161 	PF_A16B16G16R16F,
162 	PF_R32F,
163 	PF_G32R32F,
164 	PF_A32B32G32R32F,
165 	PF_UNKNOWN = 0xFF
166 };
167 
168 #define CUBEMAP_SIDES 6
169 
170 // Internal functions
171 ILboolean	iLoadDdsInternal(void);
172 ILboolean	iIsValidDds(void);
173 ILboolean	iCheckDds(DDSHEAD *Head);
174 void		AdjustVolumeTexture(DDSHEAD *Head, ILuint CompFormat);
175 ILboolean	ReadData();
176 ILboolean	AllocImage(ILuint CompFormat);
177 ILboolean	DdsDecompress(ILuint CompFormat);
178 ILboolean	ReadMipmaps(ILuint CompFormat);
179 ILuint		DecodePixelFormat();
180 void		DxtcReadColor(ILushort Data, Color8888* Out);
181 void		DxtcReadColors(const ILubyte* Data, Color8888* Out);
182 ILboolean	DecompressARGB(ILuint CompFormat);
183 ILboolean	DecompressARGB16(ILuint CompFormat);
184 ILboolean	DecompressDXT1(ILimage *lImage, ILubyte *lCompData);
185 ILboolean	DecompressDXT2(ILimage *lImage, ILubyte *lCompData);
186 ILboolean	DecompressDXT3(ILimage *lImage, ILubyte *lCompData);
187 ILboolean	DecompressDXT4(ILimage *lImage, ILubyte *lCompData);
188 ILboolean	DecompressDXT5(ILimage *lImage, ILubyte *lCompData);
189 ILboolean	Decompress3Dc();
190 ILboolean	DecompressAti1n();
191 ILboolean	DecompressRXGB();
192 ILboolean	iConvFloat16ToFloat32(ILuint* dest, ILushort* src, ILuint size);
193 ILboolean	DecompressFloat(ILuint lCompFormat);
194 void		CorrectPreMult();
195 void		GetBitsFromMask(ILuint Mask, ILuint *ShiftLeft, ILuint *ShiftRight);
196 ILboolean	iSaveDdsInternal(void);
197 ILboolean	WriteHeader(ILimage *Image, ILenum DXTCFormat, ILuint CubeFlags);
198 ILushort	*CompressTo565(ILimage *Image);
199 ILubyte		*CompressTo88(ILimage *Image);
200 ILuint		Compress(ILimage *Image, ILenum DXTCFormat);
201 ILboolean	GetBlock(ILushort *Block, ILushort *Data, ILimage *Image, ILuint XPos, ILuint YPos);
202 ILboolean	GetAlphaBlock(ILubyte *Block, ILubyte *Data, ILimage *Image, ILuint XPos, ILuint YPos);
203 ILboolean	Get3DcBlock(ILubyte *Block, ILubyte *Data, ILimage *Image, ILuint XPos, ILuint YPos, int channel);
204 void		ShortToColor565(ILushort Pixel, Color565 *Colour);
205 void		ShortToColor888(ILushort Pixel, Color888 *Colour);
206 ILushort	Color565ToShort(Color565 *Colour);
207 ILushort	Color888ToShort(Color888 *Colour);
208 ILuint		GenBitMask(ILushort ex0, ILushort ex1, ILuint NumCols, ILushort *In, ILubyte *Alpha, Color888 *OutCol);
209 void		GenAlphaBitMask(ILubyte a0, ILubyte a1, ILubyte *In, ILubyte *Mask, ILubyte *Out);
210 ILuint		RMSAlpha(ILubyte *Orig, ILubyte *Test);
211 ILuint		Distance(Color888 *c1, Color888 *c2);
212 void		ChooseEndpoints(ILushort *Block, ILushort *ex0, ILushort *ex1);
213 void		ChooseAlphaEndpoints(ILubyte *Block, ILubyte *a0, ILubyte *a1);
214 void		CorrectEndDXT1(ILushort *ex0, ILushort *ex1, ILboolean HasAlpha);
215 void		PreMult(ILushort *Data, ILubyte *Alpha);
216 
217 
218 extern ILuint CubemapDirections[CUBEMAP_SIDES];
219 
220 
221 #endif//DDS_H
222