| 139 | } |
| 140 | |
| 141 | void CommandTranscode::executeTranscode() { |
| 142 | InputStream inputStream(options.inputFilepath, *this); |
| 143 | validateToolInput(inputStream, fmtInFile(options.inputFilepath), *this); |
| 144 | |
| 145 | KTXTexture2 texture{nullptr}; |
| 146 | StreambufStream<std::streambuf*> ktx2Stream{inputStream->rdbuf(), std::ios::in | std::ios::binary}; |
| 147 | auto ret = ktxTexture2_CreateFromStream(ktx2Stream.stream(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, texture.pHandle()); |
| 148 | if (ret != KTX_SUCCESS) |
| 149 | fatal(rc::INVALID_FILE, "Failed to create KTX2 texture: {}", ktxErrorString(ret)); |
| 150 | |
| 151 | if (!ktxTexture2_NeedsTranscoding(texture)) |
| 152 | fatal(rc::INVALID_FILE, "KTX file is not transcodable."); |
| 153 | |
| 154 | texture = transcode(std::move(texture), options, *this); |
| 155 | |
| 156 | if (options.zstd) { |
| 157 | ret = ktxTexture2_DeflateZstd(texture, *options.zstd); |
| 158 | if (ret != KTX_SUCCESS) |
| 159 | fatal(rc::KTX_FAILURE, "Zstd deflation failed. KTX Error: {}", ktxErrorString(ret)); |
| 160 | } |
| 161 | |
| 162 | if (options.zlib) { |
| 163 | ret = ktxTexture2_DeflateZLIB(texture, *options.zlib); |
| 164 | if (ret != KTX_SUCCESS) |
| 165 | fatal(rc::KTX_FAILURE, "ZLIB deflation failed. KTX Error: {}", ktxErrorString(ret)); |
| 166 | } |
| 167 | |
| 168 | // Modify KTXwriter metadata |
| 169 | const auto writer = fmt::format("{} {}", commandName, version(options.testrun)); |
| 170 | ktxHashList_DeleteKVPair(&texture->kvDataHead, KTX_WRITER_KEY); |
| 171 | ktxHashList_AddKVPair(&texture->kvDataHead, KTX_WRITER_KEY, |
| 172 | static_cast<uint32_t>(writer.size() + 1), // +1 to include the \0 |
| 173 | writer.c_str()); |
| 174 | |
| 175 | // Add KTXwriterScParams metadata if supercompression was used |
| 176 | const auto writerScParams = options.compressOptions; |
| 177 | ktxHashList_DeleteKVPair(&texture->kvDataHead, KTX_WRITER_SCPARAMS_KEY); |
| 178 | if (writerScParams.size() > 0) { |
| 179 | // Options always contain a leading space |
| 180 | assert(writerScParams[0] == ' '); |
| 181 | ktxHashList_AddKVPair(&texture->kvDataHead, KTX_WRITER_SCPARAMS_KEY, |
| 182 | static_cast<uint32_t>(writerScParams.size()), |
| 183 | writerScParams.c_str() + 1); // +1 to exclude leading space |
| 184 | } |
| 185 | |
| 186 | // Save output file |
| 187 | const auto outputPath = std::filesystem::path(DecodeUTF8Path(options.outputFilepath)); |
| 188 | if (outputPath.has_parent_path()) |
| 189 | std::filesystem::create_directories(outputPath.parent_path()); |
| 190 | |
| 191 | OutputStream outputFile(options.outputFilepath, *this); |
| 192 | outputFile.writeKTX2(texture, *this); |
| 193 | } |
| 194 | |
| 195 | } // namespace ktx |
| 196 |
nothing calls this directly
no test coverage detected