Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libktx/lib/ktxint.h
9906 views
1
/* -*- tab-width: 4; -*- */
2
/* vi: set sw=2 ts=4 expandtab: */
3
4
/* $Id$ */
5
6
/*
7
* Copyright 2010-2020 The Khronos Group Inc.
8
* SPDX-License-Identifier: Apache-2.0
9
*/
10
11
12
/*
13
* Author: Mark Callow from original code by Georg Kolling
14
*/
15
16
#ifndef KTXINT_H
17
#define KTXINT_H
18
19
#include <math.h>
20
21
/* Define this to include the ETC unpack software in the library. */
22
#ifndef SUPPORT_SOFTWARE_ETC_UNPACK
23
/* Include for all GL versions because have seen OpenGL ES 3
24
* implementaions that do not support ETC1 (ARM Mali emulator v1.0)!
25
*/
26
#define SUPPORT_SOFTWARE_ETC_UNPACK 1
27
#endif
28
29
#ifndef MAX
30
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
31
#endif
32
#ifndef MIN
33
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
34
#endif
35
36
#define QUOTE(x) #x
37
#define STR(x) QUOTE(x)
38
39
#define KTX2_IDENTIFIER_REF { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A }
40
#define KTX2_HEADER_SIZE (80)
41
42
#ifdef __cplusplus
43
extern "C" {
44
#endif
45
46
/**
47
* @internal
48
* @brief used to pass GL context capabilites to subroutines.
49
*/
50
#define _KTX_NO_R16_FORMATS 0x0
51
#define _KTX_R16_FORMATS_NORM 0x1
52
#define _KTX_R16_FORMATS_SNORM 0x2
53
#define _KTX_ALL_R16_FORMATS (_KTX_R16_FORMATS_NORM | _KTX_R16_FORMATS_SNORM)
54
extern GLint _ktxR16Formats;
55
extern GLboolean _ktxSupportsSRGB;
56
57
/**
58
* @internal
59
* @~English
60
* @brief KTX file header.
61
*
62
* See the KTX specification for descriptions.
63
*/
64
typedef struct KTX_header {
65
ktx_uint8_t identifier[12];
66
ktx_uint32_t endianness;
67
ktx_uint32_t glType;
68
ktx_uint32_t glTypeSize;
69
ktx_uint32_t glFormat;
70
ktx_uint32_t glInternalformat;
71
ktx_uint32_t glBaseInternalformat;
72
ktx_uint32_t pixelWidth;
73
ktx_uint32_t pixelHeight;
74
ktx_uint32_t pixelDepth;
75
ktx_uint32_t numberOfArrayElements;
76
ktx_uint32_t numberOfFaces;
77
ktx_uint32_t numberOfMipLevels;
78
ktx_uint32_t bytesOfKeyValueData;
79
} KTX_header;
80
81
/* This will cause compilation to fail if the struct size doesn't match */
82
typedef int KTX_header_SIZE_ASSERT [sizeof(KTX_header) == KTX_HEADER_SIZE];
83
84
/**
85
* @internal
86
* @~English
87
* @brief 32-bit KTX 2 index entry.
88
*/
89
typedef struct ktxIndexEntry32 {
90
ktx_uint32_t byteOffset; /*!< Offset of item from start of file. */
91
ktx_uint32_t byteLength; /*!< Number of bytes of data in the item. */
92
} ktxIndexEntry32;
93
/**
94
* @internal
95
* @~English
96
* @brief 64-bit KTX 2 index entry.
97
*/
98
typedef struct ktxIndexEntry64 {
99
ktx_uint64_t byteOffset; /*!< Offset of item from start of file. */
100
ktx_uint64_t byteLength; /*!< Number of bytes of data in the item. */
101
} ktxIndexEntry64;
102
103
/**
104
* @internal
105
* @~English
106
* @brief KTX 2 file header.
107
*
108
* See the KTX 2 specification for descriptions.
109
*/
110
typedef struct KTX_header2 {
111
ktx_uint8_t identifier[12];
112
ktx_uint32_t vkFormat;
113
ktx_uint32_t typeSize;
114
ktx_uint32_t pixelWidth;
115
ktx_uint32_t pixelHeight;
116
ktx_uint32_t pixelDepth;
117
ktx_uint32_t layerCount;
118
ktx_uint32_t faceCount;
119
ktx_uint32_t levelCount;
120
ktx_uint32_t supercompressionScheme;
121
ktxIndexEntry32 dataFormatDescriptor;
122
ktxIndexEntry32 keyValueData;
123
ktxIndexEntry64 supercompressionGlobalData;
124
} KTX_header2;
125
126
/* This will cause compilation to fail if the struct size doesn't match */
127
typedef int KTX_header2_SIZE_ASSERT [sizeof(KTX_header2) == KTX2_HEADER_SIZE];
128
129
/**
130
* @internal
131
* @~English
132
* @brief KTX 2 level index entry.
133
*/
134
typedef struct ktxLevelIndexEntry {
135
ktx_uint64_t byteOffset; /*!< Offset of level from start of file. */
136
ktx_uint64_t byteLength;
137
/*!< Number of bytes of compressed image data in the level. */
138
ktx_uint64_t uncompressedByteLength;
139
/*!< Number of bytes of uncompressed image data in the level. */
140
} ktxLevelIndexEntry;
141
142
/**
143
* @internal
144
* @~English
145
* @brief Structure for supplemental information about the texture.
146
*
147
* _ktxCheckHeader returns supplemental information about the texture in this
148
* structure that is derived during checking of the file header.
149
*/
150
typedef struct KTX_supplemental_info
151
{
152
ktx_uint8_t compressed;
153
ktx_uint8_t generateMipmaps;
154
ktx_uint16_t textureDimension;
155
} KTX_supplemental_info;
156
/**
157
* @internal
158
* @var ktx_uint8_t KTX_supplemental_info::compressed
159
* @~English
160
* @brief KTX_TRUE, if this a compressed texture, KTX_FALSE otherwise?
161
*/
162
/**
163
* @internal
164
* @var ktx_uint8_t KTX_supplemental_info::generateMipmaps
165
* @~English
166
* @brief KTX_TRUE, if mipmap generation is required, KTX_FALSE otherwise.
167
*/
168
/**
169
* @internal
170
* @var ktx_uint16_t KTX_supplemental_info::textureDimension
171
* @~English
172
* @brief The number of dimensions, 1, 2 or 3, of data in the texture image.
173
*/
174
175
/*
176
* @internal
177
* CheckHeader1
178
*
179
* Reads the KTX file header and performs some sanity checking on the values
180
*/
181
KTX_error_code ktxCheckHeader1_(KTX_header* pHeader,
182
KTX_supplemental_info* pSuppInfo);
183
184
/*
185
* @internal
186
* CheckHeader2
187
*
188
* Reads the KTX 2 file header and performs some sanity checking on the values
189
*/
190
KTX_error_code ktxCheckHeader2_(KTX_header2* pHeader,
191
KTX_supplemental_info* pSuppInfo);
192
193
/*
194
* SwapEndian16: Swaps endianness in an array of 16-bit values
195
*/
196
void _ktxSwapEndian16(ktx_uint16_t* pData16, ktx_size_t count);
197
198
/*
199
* SwapEndian32: Swaps endianness in an array of 32-bit values
200
*/
201
void _ktxSwapEndian32(ktx_uint32_t* pData32, ktx_size_t count);
202
203
/*
204
* SwapEndian32: Swaps endianness in an array of 64-bit values
205
*/
206
void _ktxSwapEndian64(ktx_uint64_t* pData64, ktx_size_t count);
207
208
/*
209
* UnpackETC: uncompresses an ETC compressed texture image
210
*/
211
KTX_error_code _ktxUnpackETC(const GLubyte* srcETC, const GLenum srcFormat,
212
ktx_uint32_t active_width, ktx_uint32_t active_height,
213
GLubyte** dstImage,
214
GLenum* format, GLenum* internalFormat, GLenum* type,
215
GLint R16Formats, GLboolean supportsSRGB);
216
217
/*
218
* @internal
219
* ktxCompressZLIBBounds
220
*
221
* Returns upper bound for compresses data using miniz (ZLIB)
222
*/
223
ktx_size_t ktxCompressZLIBBounds(ktx_size_t srcLength);
224
225
/*
226
* @internal
227
* ktxCompressZLIBInt
228
*
229
* Compresses data using miniz (ZLIB)
230
*/
231
KTX_error_code ktxCompressZLIBInt(unsigned char* pDest,
232
ktx_size_t* pDestLength,
233
const unsigned char* pSrc,
234
ktx_size_t srcLength,
235
ktx_uint32_t level);
236
237
/*
238
* @internal
239
* ktxUncompressZLIBInt
240
*
241
* Uncompresses data using miniz (ZLIB)
242
*/
243
KTX_error_code ktxUncompressZLIBInt(unsigned char* pDest,
244
ktx_size_t* pDestLength,
245
const unsigned char* pSrc,
246
ktx_size_t srcLength);
247
248
/*
249
* Pad nbytes to next multiple of n
250
*/
251
#define _KTX_PADN(n, nbytes) (ktx_uint32_t)(n * ceilf((float)(nbytes) / n))
252
/*
253
* Calculate bytes of of padding needed to reach next multiple of n.
254
*/
255
/* Equivalent to (n * ceil(nbytes / n)) - nbytes */
256
#define _KTX_PADN_LEN(n, nbytes) \
257
(ktx_uint32_t)((n * ceilf((float)(nbytes) / n)) - (nbytes))
258
259
/*
260
* Pad nbytes to next multiple of 4
261
*/
262
#define _KTX_PAD4(nbytes) _KTX_PADN(4, nbytes)
263
/*
264
* Calculate bytes of of padding needed to reach next multiple of 4.
265
*/
266
#define _KTX_PAD4_LEN(nbytes) _KTX_PADN_LEN(4, nbytes)
267
268
/*
269
* Pad nbytes to next multiple of 8
270
*/
271
#define _KTX_PAD8(nbytes) _KTX_PADN(8, nbytes)
272
/*
273
* Calculate bytes of of padding needed to reach next multiple of 8.
274
*/
275
#define _KTX_PAD8_LEN(nbytes) _KTX_PADN_LEN(8, nbytes)
276
277
/*
278
* Pad nbytes to KTX_GL_UNPACK_ALIGNMENT
279
*/
280
#define _KTX_PAD_UNPACK_ALIGN(nbytes) \
281
_KTX_PADN(KTX_GL_UNPACK_ALIGNMENT, nbytes)
282
/*
283
* Calculate bytes of of padding needed to reach KTX_GL_UNPACK_ALIGNMENT.
284
*/
285
#define _KTX_PAD_UNPACK_ALIGN_LEN(nbytes) \
286
_KTX_PADN_LEN(KTX_GL_UNPACK_ALIGNMENT, nbytes)
287
288
/*
289
======================================
290
Internal utility functions
291
======================================
292
*/
293
294
KTX_error_code printKTX2Info2(ktxStream* src, KTX_header2* header);
295
296
/*
297
* fopen a file identified by a UTF-8 path.
298
*/
299
#if defined(_WIN32)
300
#ifndef WIN32_LEAN_AND_MEAN
301
#define WIN32_LEAN_AND_MEAN
302
#endif
303
#ifndef NOMINMAX
304
#define NOMINMAX
305
#endif
306
#include <assert.h>
307
#include <windows.h>
308
#include <shellapi.h>
309
#include <stdlib.h>
310
311
// For Windows, we convert the UTF-8 path and mode to UTF-16 path and use
312
// _wfopen which correctly handles unicode characters.
313
static inline FILE* ktxFOpenUTF8(char const* path, char const* mode) {
314
int wpLen = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
315
int wmLen = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
316
FILE* fp = NULL;
317
if (wpLen > 0 && wmLen > 0)
318
{
319
wchar_t* wpath = (wchar_t*)malloc(wpLen * sizeof(wchar_t));
320
wchar_t* wmode = (wchar_t*)malloc(wmLen * sizeof(wchar_t));
321
MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, wpLen);
322
MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, wmLen);
323
// Returned errno_t value is also set in the global errno.
324
// Apps use that for error detail as libktx only returns
325
// KTX_FILE_OPEN_FAILED.
326
(void)_wfopen_s(&fp, wpath, wmode);
327
free(wpath);
328
free(wmode);
329
return fp;
330
} else {
331
assert(KTX_FALSE
332
&& "ktxFOpenUTF8 called with zero length path or mode.");
333
return NULL;
334
}
335
}
336
#else
337
// For other platforms there is no need for any conversion, they
338
// support UTF-8 natively.
339
static inline FILE* ktxFOpenUTF8(char const* path, char const* mode) {
340
return fopen(path, mode);
341
}
342
#endif
343
344
#ifdef __cplusplus
345
}
346
#endif
347
348
#endif /* KTXINT_H */
349
350