This is an alternate way to perform patching bu using libktx to create a ktxTexture. Kept for reference. This was has the following advantage: 1. it can be used for operations that will change the length of the file; and the following disadvantages: 1. it will only work on valid (per libktx's checks) KTX files; 2. KTXwriter will be updated to show either the current version of libktx or "__defa
| 236 | // 2. KTXwriter will be updated to show either the current version of libktx or "__default__". |
| 237 | // The latter happens if the current or newly provided app id contains "__default__". |
| 238 | void CommandPatch::patchWithLibKtx(std::FILE* input) { |
| 239 | ktxTexture2* texture; |
| 240 | auto ret = ktxTexture2_CreateFromStdioStream(input, KTX_TEXTURE_CREATE_NO_FLAGS, &texture); |
| 241 | if (ret != KTX_SUCCESS) |
| 242 | fatal(rc::INVALID_FILE, "Failed to create KTX2 texture: {}", ktxErrorString(ret)); |
| 243 | // Creating the texture2 object fixes the bytesPlane fields. |
| 244 | switch (texture->supercompressionScheme) { |
| 245 | case KTX_SS_BASIS_LZ: |
| 246 | { |
| 247 | uint32_t* pBdb = texture->pDfd + 1; |
| 248 | if (KHR_DFDVAL(pBdb, MODEL) != KHR_DF_MODEL_ETC1S) { |
| 249 | std::fclose(input); |
| 250 | fatal(rc::DFD_FAILURE, "Input file \"{}\" has invalid color model for supercompression scheme", |
| 251 | options.inputFilepath); |
| 252 | } |
| 253 | } |
| 254 | [[fallthrough]]; |
| 255 | case KTX_SS_ZLIB: |
| 256 | case KTX_SS_ZSTD: |
| 257 | // Loading the data causes any unsized DFDs to be reconstructed into sized. |
| 258 | ret = ktxTexture2_LoadDeflatedImageData(texture, nullptr, 0); |
| 259 | if (ret != KTX_SUCCESS) |
| 260 | fatal(rc::INVALID_FILE, "Failed to load data for KTX2 texture: {}", ktxErrorString(ret)); |
| 261 | break; |
| 262 | case KTX_SS_NONE: |
| 263 | if (options.verbose) |
| 264 | warning("Input file \"{}\" is not supercompressed.", |
| 265 | options.inputFilepath); |
| 266 | [[fallthrough]]; |
| 267 | default: |
| 268 | std::fclose(input); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | if (options.operation == Operation::makeUnsized_e) { |
| 273 | KHR_DFDSETVAL(texture->pDfd+1, BYTESPLANE0, 0); |
| 274 | KHR_DFDSETVAL(texture->pDfd+1, BYTESPLANE1, 0); |
| 275 | } |
| 276 | |
| 277 | const auto updateMetadataValue = [&](const char* const key, |
| 278 | const std::string& value) { |
| 279 | ktxHashList_DeleteKVPair(&texture->kvDataHead, key); |
| 280 | ktxHashList_AddKVPair(&texture->kvDataHead, key, |
| 281 | static_cast<uint32_t>(value.size() + 1), // +1 to include \0 |
| 282 | value.c_str()); |
| 283 | }; |
| 284 | |
| 285 | if (options.changeWriter) { |
| 286 | // Create or modify KTXwriter metadata. |
| 287 | const auto writer = fmt::format("{} {}", commandName, version(options.testrun)); |
| 288 | updateMetadataValue(KTX_WRITER_KEY, writer); |
| 289 | } |
| 290 | |
| 291 | std::fseek(input, 0, SEEK_SET); // Seek to start. |
| 292 | ktxTexture_WriteToStdioStream(ktxTexture(texture), input); |
| 293 | } |
| 294 | #endif |
| 295 |
nothing calls this directly
no test coverage detected