summaryrefslogtreecommitdiff
path: root/DevIL/src-IL/src/il_ktx.cpp
blob: 7871ac7ca4548af736f4d25a420d3d7d79ee55c8 (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
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2000-2016 by Denton Woods
// Last modified: 05/15/2016
//
// Filename: src-IL/src/il_ktx.cpp
//
// Description: Reads a Khronos Texture .ktx file
//
//-----------------------------------------------------------------------------


#include "il_internal.h"
#ifndef IL_NO_KTX
#include "il_bits.h"
#include "rg_etc1.h"


ILboolean	iIsValidKtx(void);
ILboolean	iLoadKtxInternal();
ILboolean	iKtxReadMipmaps(ILboolean Compressed, ILuint NumMips);


#ifdef _MSC_VER
#pragma pack(push, packed_struct, 1)
#endif
typedef struct KTX_HEAD
{
	ILubyte	identifier[12];
	ILuint	endianness;
	ILuint	glType;
	ILuint	glTypeSize;
	ILuint	glFormat;
	ILuint	glInternalFormat;
	ILuint	glBaseInternalFormat;
	ILuint	pixelWidth;
	ILuint	pixelHeight;
	ILuint	pixelDepth;
	ILuint	numberOfArrayElements;
	ILuint	numberOfFaces;
	ILuint	numberOfMipmapLevels;
	ILuint	bytesOfKeyValueData;
} IL_PACKSTRUCT KTX_HEAD;
#ifdef _MSC_VER
#pragma pack(pop,  packed_struct)
#endif


// From GL/GL.h
#define I_GL_BYTE                           0x1400
#define I_GL_UNSIGNED_BYTE                  0x1401
#define I_GL_SHORT                          0x1402
#define I_GL_UNSIGNED_SHORT                 0x1403
#define I_GL_INT                            0x1404
#define I_GL_UNSIGNED_INT                   0x1405
#define I_GL_FLOAT                          0x1406
//#define I_GL_2_BYTES                        0x1407
//#define I_GL_3_BYTES                        0x1408
//#define I_GL_4_BYTES                        0x1409
#define I_GL_DOUBLE                         0x140A
#define I_GL_HALF                           0x140B

#define I_GL_ALPHA                          0x1906
#define I_GL_RGB                            0x1907
#define I_GL_RGBA                           0x1908
#define I_GL_LUMINANCE                      0x1909
#define I_GL_LUMINANCE_ALPHA                0x190A
// From gl2ext.h
#define I_GL_ETC1_RGB8_OES                  0x8D64
// From https://www.opengl.org/registry/specs/ARB/ES3_compatibility.txt
#define I_GL_COMPRESSED_RGB8_ETC2                             0x9274
#define I_GL_COMPRESSED_SRGB8_ETC2                            0x9275
#define I_GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2         0x9276
#define I_GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2        0x9277
#define I_GL_COMPRESSED_RGBA8_ETC2_EAC                        0x9278
#define I_GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC                 0x9279
#define I_GL_COMPRESSED_R11_EAC                               0x9270
#define I_GL_COMPRESSED_SIGNED_R11_EAC                        0x9271
#define I_GL_COMPRESSED_RG11_EAC                              0x9272
#define I_GL_COMPRESSED_SIGNED_RG11_EAC                       0x9273



ILboolean ilIsValidKtx(ILconst_string FileName)
{
	ILHANDLE	KtxFile;
	ILboolean	bKtx = IL_FALSE;

	if (!iCheckExtension(FileName, IL_TEXT("ktx"))) {
		ilSetError(IL_INVALID_EXTENSION);
		return bKtx;
	}

	KtxFile = iopenr(FileName);
	if (KtxFile == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return bKtx;
	}

	bKtx = ilIsValidKtxF(KtxFile);
	icloser(KtxFile);

	return bKtx;
}


ILboolean ilIsValidKtxF(ILHANDLE File)
{
	ILuint		FirstPos;
	ILboolean	bRet;

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

	return bRet;
}


ILboolean ilIsValidKtxL(const void *Lump, ILuint Size)
{
	iSetInputLump(Lump, Size);
	return iIsValidKtx();
}


ILboolean iIsValidKtx()
{
	ILubyte 	Signature[8];
	ILint		Read;

	/*Read = iread(Signature, 1, 8);
	iseek(-Read, IL_SEEK_CUR);*/

	return IL_FALSE;
}


//! Reads a .ktx file
ILboolean ilLoadKtx(ILconst_string FileName)
{
	ILHANDLE	KtxFile;
	ILboolean	bKtx = IL_FALSE;

	KtxFile = iopenr(FileName);
	if (KtxFile == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return bKtx;
	}

	bKtx = ilLoadKtxF(KtxFile);
	icloser(KtxFile);

	return bKtx;
}


//! Reads an already-opened .ktx file
ILboolean ilLoadKtxF(ILHANDLE File)
{
	ILuint		FirstPos;
	ILboolean	bRet;

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

	return bRet;
}


//! Reads from a memory "lump" that contains a .ktx
ILboolean ilLoadKtxL(const void *Lump, ILuint Size)
{
	iSetInputLump(Lump, Size);
	return iLoadKtxInternal();
}


// Note:  .Ktx support has not been tested yet!
//  https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
ILboolean iLoadKtxInternal()
{
	KTX_HEAD	Header;
	ILuint		imageSize;
	ILenum		Format, Type;
	ILubyte		Bpp, Bpc;
	ILboolean	Compressed;
	char		FileIdentifier[12] = {
		//0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
		'«', 'K', 'T', 'X', ' ', '1', '1', '»', '\r', '\n', '\x1A', '\n'
	};

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

	/*Header.Width = GetLittleShort();
	Header.Height = GetLittleShort();
	Header.Dummy = GetLittleInt();*/

	if (iread(Header.identifier, 1, 12) != 12)
		return IL_FALSE;
	Header.endianness = GetLittleUInt();
	Header.glType = GetLittleUInt();
	Header.glTypeSize = GetLittleUInt();
	Header.glFormat = GetLittleUInt();
	Header.glInternalFormat = GetLittleUInt();
	Header.glBaseInternalFormat = GetLittleUInt();
	Header.pixelWidth = GetLittleUInt();
	Header.pixelHeight = GetLittleUInt();
	Header.pixelDepth = GetLittleUInt();
	Header.numberOfArrayElements = GetLittleUInt();
	Header.numberOfFaces = GetLittleUInt();
	Header.numberOfMipmapLevels = GetLittleUInt();
	Header.bytesOfKeyValueData = GetLittleUInt();

	if (memcmp(Header.identifier, FileIdentifier, 12) || Header.endianness != 0x04030201) {
		ilSetError(IL_ILLEGAL_FILE_VALUE);
		return IL_FALSE;
	}

	if (Header.glTypeSize != 1) {  //@TODO: Cases when this is different?
		ilSetError(IL_ILLEGAL_FILE_VALUE);
		return IL_FALSE;
	}
	//@TODO: Really needed?
	/*if (Header.glInternalFormat != Header.glFormat || Header.glBaseInternalFormat != Header.glFormat)
	{
		ilSetError(IL_ILLEGAL_FILE_VALUE);
		return IL_FALSE;
	}*/

	//@TODO: Mipmaps, etc.
	if (Header.numberOfArrayElements != 0 || Header.numberOfFaces != 1 /*|| Header.numberOfMipmapLevels != 1*/)
	{
		ilSetError(IL_ILLEGAL_FILE_VALUE);
		return IL_FALSE;
	}
	//@TODO: Parse this data
	if (iseek(Header.bytesOfKeyValueData, IL_SEEK_CUR))
		return IL_FALSE;

	//@TODO: Additional formats
	if (Header.glFormat != 0)  // Uncompressed formats
	{
		switch (Header.glFormat)
		{
			case I_GL_LUMINANCE:
				Bpp = 1;
				Format = IL_LUMINANCE;
				Compressed = IL_FALSE;
				break;
			case I_GL_LUMINANCE_ALPHA:
				Bpp = 2;
				Format = IL_LUMINANCE_ALPHA;
				Compressed = IL_FALSE;
				break;
			case I_GL_RGB:
				Bpp = 3;
				Format = IL_RGB;
				Compressed = IL_FALSE;
				break;
			case I_GL_RGBA:
				Bpp = 4;
				Format = IL_RGBA;
				Compressed = IL_FALSE;
				break;
			default:
				ilSetError(IL_ILLEGAL_FILE_VALUE);
				return IL_FALSE;
		}
	}
	else  // Compressed formats - Note that type must be set here!
	{
		switch (Header.glInternalFormat)
		{
			case I_GL_ETC1_RGB8_OES:
				Bpp = 4;
				Format = IL_RGBA;
				Compressed = IL_TRUE;
				Type = IL_UNSIGNED_BYTE;
				break;
			default:
				ilSetError(IL_ILLEGAL_FILE_VALUE);
				return IL_FALSE;
		}
	}

	if (!Compressed)
	{
		//@TODO: Additional types
		switch (Header.glType)
		{
			case I_GL_UNSIGNED_BYTE:
				Bpc = 1;
				Type = IL_UNSIGNED_BYTE;
				break;
			case I_GL_HALF:
				Bpc = 2;
				Type = IL_HALF;
				break;
			default:
				ilSetError(IL_ILLEGAL_FILE_VALUE);
				return IL_FALSE;
		}

		if (!ilTexImage(Header.pixelWidth, Header.pixelHeight, 1, Bpp, Format, Type, NULL))
			return IL_FALSE;

		if (!iKtxReadMipmaps(Compressed, Header.numberOfMipmapLevels))
			return IL_FALSE;

		//iCurImage->Origin = IL_ORIGIN_LOWER_LEFT;  //@TODO: Correct for all?
	}
	else  // Compressed formats require different handling
	{
		//@TODO: Add support for compressed mipmaps
		if (Header.numberOfMipmapLevels != 1)
		{
			ilSetError(IL_ILLEGAL_FILE_VALUE);
			return IL_FALSE;
		}

		imageSize = GetLittleUInt();

		if (!ilTexImage(Header.pixelWidth, Header.pixelHeight, 1, Bpp, Format, Type, NULL))
			return IL_FALSE;

		switch (Header.glInternalFormat)
		{
			case I_GL_ETC1_RGB8_OES:
				ILubyte *FullBuffer = (ILubyte*)ialloc(Header.pixelWidth * Header.pixelHeight / 2);
				ILuint UnpackBuff[16*4];
				ILuint *Data = (ILuint*)iCurImage->Data;  // Easier copies

				if (FullBuffer == NULL)
					return IL_FALSE;
				if (imageSize != Header.pixelWidth * Header.pixelHeight / 2) {
					ilSetError(IL_ILLEGAL_FILE_VALUE);
					return IL_FALSE;
				}
				if (iread(FullBuffer, 1, imageSize) != imageSize)
					return IL_FALSE;

				ILuint BlockPos = 0;
				for (ILuint y = 0; y < iCurImage->Height; y+=4)
				{
					for (ILuint x = 0; x < iCurImage->Width; x+=4)
					{
						ILuint ImagePos = y * iCurImage->Width + x;
						rg_etc1::unpack_etc1_block(&FullBuffer[BlockPos], (ILuint*)UnpackBuff, false);
						ILuint c = 0;
						for (ILuint y1 = 0; y1 < 4; y1++)
						{
							for (ILuint x1 = 0; x1 < 4; x1++)
							{
								Data[ImagePos + y1*iCurImage->Width + x1] = UnpackBuff[c++];
							}
						}
						BlockPos += 8;
					}
				}

				ifree(FullBuffer);
				break;
		}

		iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;  //@TODO: Correct for all?
	}

	return ilFixImage();
}


ILboolean iKtxReadMipmaps(ILboolean Compressed, ILuint NumMips)
{
	ILimage *Image = iCurImage;
	ILuint Width = iCurImage->Width, Height = iCurImage->Height;
	ILuint imageSize, Padding, Pos;

	for (ILuint Mip = 0; Mip < NumMips; Mip++)
	{
		imageSize = GetLittleUInt();
		Padding = 3 - ((Image->Bps + 3) % 4);

		if (imageSize != Image->SizeOfData + Padding*Image->Height)
		{
			ilSetError(IL_ILLEGAL_FILE_VALUE);
			goto mip_fail;
		}

		// Note: The KTX spec at https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
		//  seems to imply dword-alignment of the entire image.
		if (Image->Bps % 4 == 0)  // Required to be dword-aligned
		{
			if (iread(Image->Data, 1, Image->SizeOfData) != Image->SizeOfData)
				return IL_FALSE;
		}
		else  // Since not dword-aligned, have to read each line separately.
		{
			Pos = 0;
			for (ILuint h = 0; h < Height; h++)
			{
				if (iread(&Image->Data[Pos], 1, Image->Bps) != Image->Bps)
					return IL_FALSE;
				iseek(Padding, IL_SEEK_CUR);
				Pos += Image->Bps;
			}

		}

		Image->Origin = IL_ORIGIN_LOWER_LEFT;  //@TODO: Correct for all?
		Width = Width / 2;
		Height = Height / 2;

		if (Mip < NumMips - 1)
		{
			Image->Mipmaps = ilNewImage(Width, Height, 1, Image->Bpp, Image->Bpc);
			if (Image->Mipmaps == NULL)
				goto mip_fail;
			Image->Mipmaps->Format = Image->Format;
			Image->Mipmaps->Type = Image->Type;
			Image = Image->Mipmaps;
		}
	}

	return IL_TRUE;

mip_fail:
	Image = iCurImage;
	ILimage *StartImage = Image->Mipmaps, *TempImage;
	while (StartImage) {
		TempImage = StartImage;
		StartImage = StartImage->Mipmaps;
		ifree(TempImage);
	}

	Image->Mipmaps = NULL;
	return IL_FALSE;
}

#endif//IL_NO_KTX