summaryrefslogtreecommitdiff
path: root/DevIL/src-IL/src/il_iff.cpp
blob: d4a2afeb00b3c756019d1564976db8ee0340f7fb (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Last modified: 03/0/2009
//
// Filename: src-IL/src/il_iff.cpp
//
// Description: Reads from an .iff file.  Contribution from GAIA:
//				http://gaia.fdi.ucm.es/grupo/projects/javy/devzone.html#DevILNotes.
//
//-----------------------------------------------------------------------------

#include "il_internal.h"

#ifndef IL_NO_IFF

// Chunk type, data and functions:
typedef struct _iff_chunk {
	ILuint	tag;
	ILuint	start;
	ILuint	size;
	ILuint	chunkType;
} iff_chunk;

#define CHUNK_STACK_SIZE (32)
static iff_chunk chunkStack[CHUNK_STACK_SIZE];
static int chunkDepth = -1;

iff_chunk iff_begin_read_chunk();
void iff_end_read_chunk();
char *iff_read_data(int size);
ILboolean iLoadIffInternal(void);


/* Define the IFF tags we are looking for in the file. */
const ILuint IFF_TAG_CIMG = ('C' << 24) | ('I' << 16) | ('M' << 8) | ('G'); 
const ILuint IFF_TAG_FOR4 = ('F' << 24) | ('O' << 16) | ('R' << 8) | ('4'); 
const ILuint IFF_TAG_TBHD = ('T' << 24) | ('B' << 16) | ('H' << 8) | ('D'); 
const ILuint IFF_TAG_TBMP = ('T' << 24) | ('B' << 16) | ('M' << 8) | ('P');
const ILuint IFF_TAG_RGBA = ('R' << 24) | ('G' << 16) | ('B' << 8) | ('A');
const ILuint IFF_TAG_CLPZ = ('C' << 24) | ('L' << 16) | ('P' << 8) | ('Z');
const ILuint IFF_TAG_ESXY = ('E' << 24) | ('S' << 16) | ('X' << 8) | ('Y');
const ILuint IFF_TAG_ZBUF = ('Z' << 24) | ('B' << 16) | ('U' << 8) | ('F');
const ILuint IFF_TAG_BLUR = ('B' << 24) | ('L' << 16) | ('U' << 8) | ('R');
const ILuint IFF_TAG_BLRT = ('B' << 24) | ('L' << 16) | ('R' << 8) | ('T');
const ILuint IFF_TAG_HIST = ('H' << 24) | ('I' << 16) | ('S' << 8) | ('T');

// Flags
#define RGB_FLAG     (1)
#define ALPHA_FLAG   (2)
#define ZBUFFER_FLAG (4)

// Function for decompress the file.
char *iff_decompress_rle(ILuint numBytes, char *compressedData, 
							ILuint compressedDataSize, 
							ILuint *compressedStartIndex);

char *iffReadUncompressedTile(ILushort width, ILushort height, ILbyte depth);
char *iff_decompress_tile_rle(ILushort width, ILushort height, ILushort depth, 
				 char *compressedData, ILuint compressedDataSize);


//! Reads an IFF file
ILboolean ilLoadIff(const ILstring FileName)
{
	ILHANDLE iffFile;
	ILboolean ret = IL_FALSE;

	iffFile = iopenr(FileName);
	if (iffFile == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return ret;
	}
	ret = ilLoadIffF(iffFile);
	icloser(iffFile);
	return ret;
}


//! Reads an already-opened IFF file
ILboolean ilLoadIffF(ILHANDLE File)
{
	ILuint		FirstPos;
	ILboolean	bRet;

	iSetInputFile(File);
	FirstPos = itell();
	bRet = iLoadIffInternal();
	iseek(FirstPos, IL_SEEK_SET);

	// Lbm files can have the .iff extension as well, so if Iff-loading failed,
	//  try to load it as a Lbm.
	if (bRet == IL_FALSE)
		return ilLoadIlbmF(File);

	return bRet;
}


//! Reads from a memory "lump" that contains an IFF
ILboolean ilLoadIffL(const void *Lump, ILuint Size)
{
	ILuint		FirstPos;
	ILboolean	bRet;

	iSetInputLump(Lump, Size);
	FirstPos = itell();
	bRet = iLoadIffInternal();
	iseek(FirstPos, IL_SEEK_SET);

	// Lbm files can have the .iff extension as well, so if Iff-loading failed,
	//  try to load it as a Lbm.
	if (bRet == IL_FALSE)
		return ilLoadIlbmL(Lump, Size);

	return IL_TRUE;
}

ILboolean iLoadIffInternal(void)
{
	iff_chunk chunkInfo;
    
	// -- Header info.
	ILuint width, height;
	ILuint flags, compress;
	ILushort tiles;

	ILenum	format;
	ILubyte bpp;

	ILboolean tileImageDataFound;

	// -- Initialize the top of the chunk stack.
	chunkDepth = -1;

	// -- File should begin with a FOR4 chunk of type CIMG
	chunkInfo = iff_begin_read_chunk();
	if (chunkInfo.chunkType != IFF_TAG_CIMG) {
		ilSetError(IL_ILLEGAL_FILE_VALUE);
		return IL_FALSE;
	}


	/*
	* Read the image header
	* OK, we have a FOR4 of type CIMG, look for the following tags
	*		FVER	
	*		TBHD	bitmap header, definition of size, etc.
	*		AUTH
	*		DATE
	*/
	while (1) {

		chunkInfo = iff_begin_read_chunk();
		
		// -- Right now, the only info we need about the image is in TBHD
		// -- so search this level until we find it.
		if( chunkInfo.tag == IFF_TAG_TBHD ) {
			// -- Header chunk found
			width = GetBigUInt();
			height = GetBigUInt();
			GetBigShort(); // -- Don't support 
			GetBigShort(); // -- Don't support 
			flags = GetBigUInt();
			GetBigShort(); // -- Don't support
			tiles = GetBigUShort();
			compress	= GetBigUInt();
			
			iff_end_read_chunk();
		
			if( compress > 1 ) {
				ilSetError(IL_ILLEGAL_FILE_VALUE);
				return	IL_FALSE;
			}
   				break;
		} else
			iff_end_read_chunk();
	} /* END find TBHD while loop */

	if (!(flags & RGB_FLAG)) {
		ilSetError(IL_ILLEGAL_FILE_VALUE);
		return	IL_FALSE;
	}

	if (flags & ALPHA_FLAG) {
		format = IL_RGBA; bpp = 4;
	} else {
		format = IL_RGB; bpp = 3;
	}

	if (!ilTexImage(width, height, 1, bpp, format, IL_UNSIGNED_BYTE, NULL))
		return IL_FALSE;

	iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;

	tileImageDataFound = IL_FALSE;

	while (!tileImageDataFound) {
		ILuint tileImage;
		ILuint tileZ;
	
		chunkInfo = iff_begin_read_chunk();

		/*
		 * OK, we have a FOR4 of type TBMP, (embedded FOR4)
		 * look for the following tags
		 *		RGBA	color data,	RLE compressed tiles of 32 bbp data
		 *		ZBUF	z-buffer data, 32 bit float values
		 *		CLPZ	depth map specific, clipping planes, 2 float values
		 *		ESXY	depth map specific, eye x-y ratios, 2 float values
		 *		HIST	
		 *		VERS
		 *		FOR4 <size>	BLUR (twice embedded FOR4)
		 */
		if (chunkInfo.chunkType != IFF_TAG_TBMP) {
			iff_end_read_chunk();
			continue;
		}
		tileImageDataFound = IL_TRUE;
		tileImage = 0;	// Si no RGBA, tileImage = tiles...
		if (flags & ZBUFFER_FLAG)
			tileZ = 0;
		else
			tileZ = tiles;

		// Read tiles
		while ( (tileImage < tiles) || (tileZ < tiles)) {
			char	 *tileData;
			ILushort x1, x2, y1, y2, tile_width, tile_height;
			ILuint remainingDataSize;
			ILushort	tile_area;
			ILuint	tileCompressed;

			chunkInfo = iff_begin_read_chunk();
			if ((chunkInfo.tag != IFF_TAG_RGBA) && (chunkInfo.tag != IFF_TAG_ZBUF)) {
				ilSetError(IL_ILLEGAL_FILE_VALUE);
				return IL_FALSE;
			}
			x1 = GetBigUShort();	y1 = GetBigUShort();
			x2 = GetBigUShort();	y2 = GetBigUShort();

			remainingDataSize = chunkInfo.size - 4*sizeof(ILushort);
			tile_width = x2 - x1 + 1;
			tile_height = y2 - y1 + 1;
			tile_area = tile_width * tile_height;

			if ((ILint)remainingDataSize >= (tile_width * tile_height * bpp))
				tileCompressed = 0;
			else
				tileCompressed = 1;

			if (chunkInfo.tag == IFF_TAG_RGBA) {
				if (tileCompressed) {
					char	*data = iff_read_data(remainingDataSize);
					if (data) {
						tileData = iff_decompress_tile_rle(tile_width, tile_height,
															bpp, data, remainingDataSize);
						ifree(data);
					}
				} else {
					tileData = iffReadUncompressedTile(tile_width, tile_height, bpp);
				}

				if (tileData) {
					// Dump RGBA data to our data structure 
					ILushort	i;
					ILuint		base;
					base = bpp*(width * y1 + x1);
					for (i = 0; i < tile_height; i++) {
						memcpy(&iCurImage->Data[base + bpp*i*width],
								&tileData[bpp*i*tile_width],
								tile_width*bpp*sizeof(char));
					}
					ifree(tileData);
					tileData = NULL;
	    
					iff_end_read_chunk();
					tileImage++;
				} else
					return IL_FALSE;
			} else if (chunkInfo.tag == IFF_TAG_ZBUF) {
				tileZ++;
				iff_end_read_chunk();
			}

		}
	}
	//ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);  // Why was this here?
	return ilFixImage();
}

/*
 * IFF Chunking Routines.
 *
 */

iff_chunk iff_begin_read_chunk()
{
	chunkDepth++;
	if (chunkDepth >= CHUNK_STACK_SIZE){
		ilSetError(IL_STACK_OVERFLOW);
		return chunkStack[0];
	}
	if (chunkDepth < 0) {
		ilSetError(IL_STACK_UNDERFLOW);
		return chunkStack[0];
	}

	chunkStack[chunkDepth].start = itell();
	chunkStack[chunkDepth].tag = GetBigInt();
	chunkStack[chunkDepth].size = GetBigInt();
    
	if (chunkStack[chunkDepth].tag == IFF_TAG_FOR4) {
		// -- We have a form, so read the form type tag as well. 
		chunkStack[chunkDepth].chunkType = GetBigInt();
	} else {
		chunkStack[chunkDepth].chunkType = 0;
	} 
	
	return chunkStack[chunkDepth];
}

void iff_end_read_chunk()
{
	ILuint	end;
	int		part;
  
	end = chunkStack[chunkDepth].start + chunkStack[chunkDepth].size + 8;
    
	if (chunkStack[chunkDepth].chunkType != 0) {
		end += 4;
	}
	// Add padding 
	part = end % 4;
	if (part != 0) {
	    end += 4 - part;   
	}

	iseek(end, IL_SEEK_SET);

	chunkDepth--;
}

char * iff_read_data(int size)
{
	char *buffer = (char*)ialloc(size * sizeof(char));
	if (buffer == NULL)
		return NULL;
	
	if (iread(buffer, size*sizeof(char), 1) != 1) {
		ifree(buffer);
		return NULL;
	}

	return buffer;
}

/*
	IFF decompress functions
*/

char *iffReadUncompressedTile(ILushort width, ILushort height, ILbyte depth)
{

	char	*data = NULL;
	char	*iniPixel;
	char	*finPixel;
	int		i, j;
	int		tam = width* height * depth * sizeof(char);

	data = (char*)ialloc(tam);
	if (data == NULL)
		return NULL;

	if (iread(data, tam, 1) != 1) {
		ifree(data);
		return NULL;
	}

	iniPixel = data;
	for (i = 0; i < tam / depth; i++) {
		finPixel = iniPixel + depth;
		for (j = 0; j < (depth /2); j++) {
			char aux;
			aux = *iniPixel; 
			*(finPixel--) = *iniPixel;
			*(iniPixel++) = aux;
		}
	}
	return data;
}


char *iff_decompress_tile_rle(ILushort width, ILushort height, ILushort depth, 
							  char *compressedData, ILuint compressedDataSize)
{

	char	*channels[4];
	char	*data;
	int		i, k, row, column;
	ILuint	compressedStart = 0;

	// Decompress only in RGBA.
	if (depth != 4) {
		ilSetError(IL_ILLEGAL_FILE_VALUE);
		return NULL;
	}

	for (i = depth-1; i >= 0; --i) {
		channels[i] = iff_decompress_rle(width * height, compressedData, 
				      compressedDataSize, &compressedStart);
		if (channels[i] == NULL)
			return NULL;
	}

    // Build all the channels from the decompression into an RGBA array.
	data = (char*)ialloc(width * height * depth * sizeof(char));
	if (data == NULL)
		return NULL;

	for (row = 0; row < height; row++)
		for (column = 0; column < width; column++)
			for (k = 0; k < depth; k++)
				data[depth*(row*width + column) + k] =
					channels[k][row*width + column];
	
	ifree(channels[0]); ifree(channels[1]);
	ifree(channels[2]); ifree(channels[3]);

	return data;
}

char *iff_decompress_rle(ILuint numBytes, char *compressedData, 
							ILuint compressedDataSize, 
							ILuint *compressedStartIndex)
{

	char	*data = (char*)ialloc(numBytes * sizeof(char));
	unsigned char	nextChar, count;
	int		i;
	ILuint	byteCount = 0;

	if (data == NULL)
		return NULL;

	memset(data, 0, numBytes*sizeof(char));

	while (byteCount < numBytes) {
		if (*compressedStartIndex >= compressedDataSize)
			break;
		nextChar = compressedData[*compressedStartIndex];
		(*compressedStartIndex)++;
		count = (nextChar & 0x7f) + 1;
		if ((byteCount + count) > numBytes) break;
		if (nextChar & 0x80) {
			// Duplication run
			nextChar = compressedData[*compressedStartIndex];
			(*compressedStartIndex)++;
			// assert ((byteCount + count) <= numBytes);
			for (i = 0; i < count; i++) {
				data[byteCount]= nextChar;
				byteCount++;
			}
		} else {
			// Verbatim run
			for (i = 0; i < count; i++) {
				data[byteCount] = compressedData[*compressedStartIndex];
				(*compressedStartIndex)++;
				byteCount++;
			}
		}
		//assert(byteCount <= numBytes);
	}

	return data;

}

#endif //IL_NO_IFF