| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef KTXINT_H |
| 17 |
#define KTXINT_H |
| 18 |
|
| 19 |
#include <math.h> |
| 20 |
|
| 21 |
|
| 22 |
#ifndef SUPPORT_SOFTWARE_ETC_UNPACK |
| 23 |
|
| 24 |
|
| 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 |
|
| 48 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 82 |
typedef int KTX_header_SIZE_ASSERT [sizeof(KTX_header) == KTX_HEADER_SIZE]; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
typedef struct ktxIndexEntry32 { |
| 90 |
ktx_uint32_t byteOffset; |
| 91 |
ktx_uint32_t byteLength; |
| 92 |
} ktxIndexEntry32; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
typedef struct ktxIndexEntry64 { |
| 99 |
ktx_uint64_t byteOffset; |
| 100 |
ktx_uint64_t byteLength; |
| 101 |
} ktxIndexEntry64; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 127 |
typedef int KTX_header2_SIZE_ASSERT [sizeof(KTX_header2) == KTX2_HEADER_SIZE]; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
typedef struct ktxLevelIndexEntry { |
| 135 |
ktx_uint64_t byteOffset; |
| 136 |
ktx_uint64_t byteLength; |
| 137 |
|
| 138 |
ktx_uint64_t uncompressedByteLength; |
| 139 |
|
| 140 |
} ktxLevelIndexEntry; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
KTX_error_code ktxCheckHeader1_(KTX_header* pHeader, |
| 182 |
KTX_supplemental_info* pSuppInfo); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
KTX_error_code ktxCheckHeader2_(KTX_header2* pHeader, |
| 191 |
KTX_supplemental_info* pSuppInfo); |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
void _ktxSwapEndian16(ktx_uint16_t* pData16, ktx_size_t count); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
void _ktxSwapEndian32(ktx_uint32_t* pData32, ktx_size_t count); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
void _ktxSwapEndian64(ktx_uint64_t* pData64, ktx_size_t count); |
| 207 |
|
| 208 |
|
| 209 |
|
| 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 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
ktx_size_t ktxCompressZLIBBounds(ktx_size_t srcLength); |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 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 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 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 |
|
| 250 |
|
| 251 |
#define _KTX_PADN(n, nbytes) (ktx_uint32_t)(n * ceilf((float)(nbytes) / n)) |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
#define _KTX_PADN_LEN(n, nbytes) \ |
| 257 |
(ktx_uint32_t)((n * ceilf((float)(nbytes) / n)) - (nbytes)) |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
#define _KTX_PAD4(nbytes) _KTX_PADN(4, nbytes) |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
#define _KTX_PAD4_LEN(nbytes) _KTX_PADN_LEN(4, nbytes) |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
#define _KTX_PAD8(nbytes) _KTX_PADN(8, nbytes) |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
#define _KTX_PAD8_LEN(nbytes) _KTX_PADN_LEN(8, nbytes) |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
#define _KTX_PAD_UNPACK_ALIGN(nbytes) \ |
| 281 |
_KTX_PADN(KTX_GL_UNPACK_ALIGNMENT, nbytes) |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
#define _KTX_PAD_UNPACK_ALIGN_LEN(nbytes) \ |
| 286 |
_KTX_PADN_LEN(KTX_GL_UNPACK_ALIGNMENT, nbytes) |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
KTX_error_code printKTX2Info2(ktxStream* src, KTX_header2* header); |
| 295 |
|
| 296 |
|
| 297 |
|
| 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 |
|
| 312 |
|
| 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 |
|
| 324 |
|
| 325 |
|
| 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 |
|
| 338 |
|
| 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 |