summaryrefslogtreecommitdiff
path: root/DevIL/src-IL/src/il_iwi.c
blob: dac2dfcfa54a5c564c723204d5515ea2297f83b5 (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
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2000-2009 by Denton Woods
// Last modified: 03/04/2009
//
// Filename: src-IL/src/il_iwi.c
//
// Description: Reads from an Infinity Ward Image (.iwi) file from Call of Duty.
//
//-----------------------------------------------------------------------------


#include "il_internal.h"
#ifndef IL_NO_IWI
#include "il_dds.h"

typedef struct IWIHEAD
{
	ILuint		Signature;
	ILubyte		Format;
	ILubyte		Flags;
	ILushort	Width;
	ILushort	Height;
} IWIHEAD;

#define IWI_ARGB8	0x01
#define IWI_RGB8	0x02
#define IWI_ARGB4	0x03
#define IWI_A8		0x04
#define IWI_JPG		0x07
#define IWI_DXT1	0x0B
#define IWI_DXT3	0x0C
#define IWI_DXT5	0x0D

ILboolean iIsValidIwi(void);
ILboolean iCheckIwi(IWIHEAD *Header);
ILboolean iLoadIwiInternal(void);
ILboolean IwiInitMipmaps(ILimage *BaseImage, ILuint *NumMips);
ILboolean IwiReadImage(ILimage *BaseImage, IWIHEAD *Header, ILuint NumMips);
ILenum IwiGetFormat(ILubyte Format, ILubyte *Bpp);

//! Checks if the file specified in FileName is a valid IWI file.
ILboolean ilIsValidIwi(ILconst_string FileName)
{
	ILHANDLE	IwiFile;
	ILboolean	bIwi = IL_FALSE;
	
	if (!iCheckExtension(FileName, IL_TEXT("iwi"))) {
		ilSetError(IL_INVALID_EXTENSION);
		return bIwi;
	}
	
	IwiFile = iopenr(FileName);
	if (IwiFile == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return bIwi;
	}
	
	bIwi = ilIsValidIwiF(IwiFile);
	icloser(IwiFile);
	
	return bIwi;
}


//! Checks if the ILHANDLE contains a valid IWI file at the current position.
ILboolean ilIsValidIwiF(ILHANDLE File)
{
	ILuint		FirstPos;
	ILboolean	bRet;
	
	iSetInputFile(File);
	FirstPos = itell();
	bRet = iIsValidIwi();
	iseek(FirstPos, IL_SEEK_SET);
	
	return bRet;
}


//! Checks if Lump is a valid IWI lump.
ILboolean ilIsValidIwiL(const void *Lump, ILuint Size)
{
	iSetInputLump(Lump, Size);
	return iIsValidIwi();
}


// Internal function used to get the IWI header from the current file.
ILboolean iGetIwiHead(IWIHEAD *Header)
{
	Header->Signature = GetLittleUInt();
	Header->Format = igetc();
	Header->Flags = igetc();  //@TODO: Find out what the flags mean.
	Header->Width = GetLittleUShort();
	Header->Height = GetLittleUShort();

	// @TODO: Find out what is in the rest of the header.
	iseek(18, IL_SEEK_CUR);

	return IL_TRUE;
}


// Internal function to get the header and check it.
ILboolean iIsValidIwi(void)
{
	IWIHEAD		Header;
	ILuint		Pos = itell();

	if (!iGetIwiHead(&Header))
		return IL_FALSE;
	// The length of the header varies, so we just go back to the original position.
	iseek(Pos, IL_SEEK_CUR);

	return iCheckIwi(&Header);
}


// Internal function used to check if the HEADER is a valid IWI header.
ILboolean iCheckIwi(IWIHEAD *Header)
{
	if (Header->Signature != 0x06695749 && Header->Signature != 0x05695749)  // 'IWi-' (version 6, and version 5 is the second).
		return IL_FALSE;
	if (Header->Width == 0 || Header->Height == 0)
		return IL_FALSE;
	// DXT images must have power-of-2 dimensions.
	if (Header->Format == IWI_DXT1 || Header->Format == IWI_DXT3 || Header->Format == IWI_DXT5)
		if (Header->Width != ilNextPower2(Header->Width) || Header->Height != ilNextPower2(Header->Height))
			return IL_FALSE;
	// 0x0B, 0x0C and 0x0D are DXT formats.
	if (Header->Format != IWI_ARGB4 && Header->Format != IWI_RGB8 && Header->Format != IWI_ARGB8 && Header->Format != IWI_A8 
		&& Header->Format != IWI_DXT1 && Header->Format != IWI_DXT3 && Header->Format != IWI_DXT5)
		return IL_FALSE;

	return IL_TRUE;
}


//! Reads a IWI file
ILboolean ilLoadIwi(ILconst_string FileName)
{
	ILHANDLE	IwiFile;
	ILboolean	bIwi = IL_FALSE;
	
	IwiFile = iopenr(FileName);
	if (IwiFile == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return bIwi;
	}

	bIwi = ilLoadIwiF(IwiFile);
	icloser(IwiFile);

	return bIwi;
}


//! Reads an already-opened IWI file
ILboolean ilLoadIwiF(ILHANDLE File)
{
	ILuint		FirstPos;
	ILboolean	bRet;
	
	iSetInputFile(File);
	FirstPos = itell();
	bRet = iLoadIwiInternal();
	iseek(FirstPos, IL_SEEK_SET);
	
	return bRet;
}


//! Reads from a memory "lump" that contains a IWI
ILboolean ilLoadIwiL(const void *Lump, ILuint Size)
{
	iSetInputLump(Lump, Size);
	return iLoadIwiInternal();
}


// Internal function used to load the IWI.
ILboolean iLoadIwiInternal(void)
{
	IWIHEAD		Header;
	ILuint		NumMips = 0;
	ILboolean	HasMipmaps = IL_TRUE;
	ILenum		Format;
	ILubyte		Bpp;

	if (iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	// Read the header and check it.
	if (!iGetIwiHead(&Header))
		return IL_FALSE;
	if (!iCheckIwi(&Header)) {
		ilSetError(IL_INVALID_FILE_HEADER);
		return IL_FALSE;
	}

	// From a post by Pointy on http://iwnation.com/forums/index.php?showtopic=27903,
	//  flags ending with 0x3 have no mipmaps.
	HasMipmaps = ((Header.Flags & 0x03) == 0x03) ? IL_FALSE : IL_TRUE;

	// Create the image, then create the mipmaps, then finally read the image.
	Format = IwiGetFormat(Header.Format, &Bpp);
	if (!ilTexImage(Header.Width, Header.Height, 1, Bpp, Format, IL_UNSIGNED_BYTE, NULL))
		return IL_FALSE;
	iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
	if (HasMipmaps)
		if (!IwiInitMipmaps(iCurImage, &NumMips))
			return IL_FALSE;
	if (!IwiReadImage(iCurImage, &Header, NumMips))
		return IL_FALSE;

	return ilFixImage();
}


// Helper function to convert IWI formats to DevIL formats and Bpp.
ILenum IwiGetFormat(ILubyte Format, ILubyte *Bpp)
{
	switch (Format)
	{
		case IWI_ARGB8:
			*Bpp = 4;
			return IL_BGRA;
		case IWI_RGB8:
			*Bpp = 3;
			return IL_BGR;
		case IWI_ARGB4:
			*Bpp = 4;
			return IL_BGRA;
		case IWI_A8:
			*Bpp = 1;
			return IL_ALPHA;
		case IWI_DXT1:
			*Bpp = 4;
			return IL_RGBA;
		case IWI_DXT3:
			*Bpp = 4;
			return IL_RGBA;
		case IWI_DXT5:
			*Bpp = 4;
			return IL_RGBA;
	}

	return 0;  // Will never reach this.
}


// Function to intialize the mipmaps and determine the number of mipmaps.
ILboolean IwiInitMipmaps(ILimage *BaseImage, ILuint *NumMips)
{
	ILimage	*Image;
	ILuint	Width, Height, Mipmap;

	Image = BaseImage;
	Width = BaseImage->Width;  Height = BaseImage->Height;
	Image->Origin = IL_ORIGIN_UPPER_LEFT;

	for (Mipmap = 0; Width != 1 && Height != 1; Mipmap++) {
		// 1 is the smallest dimension possible.
		Width = (Width >> 1) == 0 ? 1 : (Width >> 1);
		Height = (Height >> 1) == 0 ? 1 : (Height >> 1);

		Image->Mipmaps = ilNewImageFull(Width, Height, 1, BaseImage->Bpp, BaseImage->Format, BaseImage->Type, NULL);
		if (Image->Mipmaps == NULL)
			return IL_FALSE;
		Image = Image->Mipmaps;

		// ilNewImage does not set these.
		Image->Format = BaseImage->Format;
		Image->Type = BaseImage->Type;
		// The origin is in the upper left.
		Image->Origin = IL_ORIGIN_UPPER_LEFT;
	}

	*NumMips = Mipmap;
	return IL_TRUE;
}


ILboolean IwiReadImage(ILimage *BaseImage, IWIHEAD *Header, ILuint NumMips)
{
	ILimage	*Image;
	ILuint	SizeOfData;
	ILubyte	*CompData = NULL;
	ILint	i, j, k, m;

	for (i = NumMips; i >= 0; i--) {
		Image = BaseImage;
		// Go to the ith mipmap level.
		//  The mipmaps go from smallest to the largest.
		for (j = 0; j < i; j++)
			Image = Image->Mipmaps;

		switch (Header->Format)
		{
			case IWI_ARGB8: // These are all
			case IWI_RGB8:  //  uncompressed data,
			case IWI_A8:    //  so just read it.
				if (iread(Image->Data, 1, Image->SizeOfData) != Image->SizeOfData)
					return IL_FALSE;
				break;

			case IWI_ARGB4:  //@TODO: Find some test images for this.
				// Data is in ARGB4 format - 4 bits per component.
				SizeOfData = Image->Width * Image->Height * 2;
				CompData = ialloc(SizeOfData);  // Not really compressed - just in ARGB4 format.
				if (CompData == NULL)
					return IL_FALSE;
				if (iread(CompData, 1, SizeOfData) != SizeOfData) {
					ifree(CompData);
					return IL_FALSE;
				}
				for (k = 0, m = 0; k < (ILint)Image->SizeOfData; k += 4, m += 2) {
					// @TODO: Double the image data into the low and high nibbles for a better range of values.
					Image->Data[k+0] = CompData[m] & 0xF0;
					Image->Data[k+1] = (CompData[m] & 0x0F) << 4;
					Image->Data[k+2] = CompData[m+1] & 0xF0;
					Image->Data[k+3] = (CompData[m+1] & 0x0F) << 4;
				}
				break;

			case IWI_DXT1:
				// DXT1 data has at least 8 bytes, even for one pixel.
				SizeOfData = IL_MAX(Image->Width * Image->Height / 2, 8);
				CompData = ialloc(SizeOfData);  // Gives a 6:1 compression ratio (or 8:1 for DXT1 with alpha)
				if (CompData == NULL)
					return IL_FALSE;
				if (iread(CompData, 1, SizeOfData) != SizeOfData) {
					ifree(CompData);
					return IL_FALSE;
				}

				// Decompress the DXT1 data into Image (ith mipmap).
				if (!DecompressDXT1(Image, CompData)) {
					ifree(CompData);
					return IL_FALSE;
				}

				// Keep a copy of the DXTC data if the user wants it.
				if (ilGetInteger(IL_KEEP_DXTC_DATA) == IL_TRUE) {
					Image->DxtcSize = SizeOfData;
					Image->DxtcData = CompData;
					Image->DxtcFormat = IL_DXT1;
					CompData = NULL;
				}

				break;

			case IWI_DXT3:
				// DXT3 data has at least 16 bytes, even for one pixel.
				SizeOfData = IL_MAX(Image->Width * Image->Height, 16);
				CompData = ialloc(SizeOfData);  // Gives a 4:1 compression ratio
				if (CompData == NULL)
					return IL_FALSE;
				if (iread(CompData, 1, SizeOfData) != SizeOfData) {
					ifree(CompData);
					return IL_FALSE;
				}

				// Decompress the DXT3 data into Image (ith mipmap).
				if (!DecompressDXT3(Image, CompData)) {
					ifree(CompData);
					return IL_FALSE;
				}
				break;

			case IWI_DXT5:
				// DXT5 data has at least 16 bytes, even for one pixel.
				SizeOfData = IL_MAX(Image->Width * Image->Height, 16);
				CompData = ialloc(SizeOfData);  // Gives a 4:1 compression ratio
				if (CompData == NULL)
					return IL_FALSE;
				if (iread(CompData, 1, SizeOfData) != SizeOfData) {
					ifree(CompData);
					return IL_FALSE;
				}

				// Decompress the DXT5 data into Image (ith mipmap).
				if (!DecompressDXT5(Image, CompData)) {
					ifree(CompData);
					return IL_FALSE;
				}
				break;
		}
	
		ifree(CompData);
	}

	return IL_TRUE;
}


#endif//IL_NO_IWI