| 31 | namespace cinder { namespace gl { |
| 32 | |
| 33 | void parseKtx( const DataSourceRef &dataSource, TextureData *resultData ) |
| 34 | { |
| 35 | typedef struct { |
| 36 | uint8_t identifier[12]; |
| 37 | uint32_t endianness; |
| 38 | uint32_t glType; |
| 39 | uint32_t glTypeSize; |
| 40 | uint32_t glFormat; |
| 41 | uint32_t glInternalFormat; |
| 42 | uint32_t glBaseInternalFormat; |
| 43 | uint32_t pixelWidth; |
| 44 | uint32_t pixelHeight; |
| 45 | uint32_t pixelDepth; |
| 46 | uint32_t numberOfArrayElements; |
| 47 | uint32_t numberOfFaces; |
| 48 | uint32_t numberOfMipmapLevels; |
| 49 | uint32_t bytesOfKeyValueData; |
| 50 | } KtxHeader; |
| 51 | |
| 52 | static const uint8_t FileIdentifier[12] = { |
| 53 | 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A |
| 54 | }; |
| 55 | |
| 56 | KtxHeader header; |
| 57 | auto ktxStream = dataSource->createStream(); |
| 58 | ktxStream->readData( &header, sizeof(header) ); |
| 59 | |
| 60 | if( memcmp( header.identifier, FileIdentifier, sizeof(FileIdentifier) ) ) |
| 61 | throw KtxParseExc( "File identifier mismatch" ); |
| 62 | |
| 63 | if( header.endianness != 0x04030201 ) |
| 64 | throw KtxParseExc( "Only little endian currently supported" ); |
| 65 | |
| 66 | if( header.numberOfArrayElements != 0 ) |
| 67 | throw KtxParseExc( "Array textures not currently supported" ); |
| 68 | |
| 69 | if( ( header.numberOfFaces != 1 ) && ( header.numberOfFaces != 6 ) ) |
| 70 | throw KtxParseExc( "Unsupported number of faces" ); |
| 71 | |
| 72 | if( header.pixelDepth != 0 ) |
| 73 | throw KtxParseExc( "3D textures not currently supported" ); |
| 74 | |
| 75 | resultData->setWidth( header.pixelWidth ); |
| 76 | resultData->setHeight( header.pixelHeight ); |
| 77 | resultData->setDepth( header.pixelDepth ); |
| 78 | resultData->setNumFaces( header.numberOfFaces ); |
| 79 | resultData->setInternalFormat( header.glInternalFormat ); |
| 80 | resultData->setDataFormat( header.glFormat ); |
| 81 | resultData->setDataType( header.glType ); |
| 82 | resultData->setUnpackAlignment( 4 ); |
| 83 | |
| 84 | ktxStream->seekRelative( header.bytesOfKeyValueData ); |
| 85 | |
| 86 | // clear output containers |
| 87 | resultData->clear(); |
| 88 | size_t byteOffset = 0; |
| 89 | for( int levelIdx = 0; levelIdx < std::max<int>( 1, header.numberOfMipmapLevels ); ++levelIdx ) { |
| 90 |
no test coverage detected