| 209 | } |
| 210 | |
| 211 | void CommandEncode::executeEncode() { |
| 212 | InputStream inputStream(options.inputFilepath, *this); |
| 213 | validateToolInput(inputStream, fmtInFile(options.inputFilepath), *this); |
| 214 | |
| 215 | KTXTexture2 texture{nullptr}; |
| 216 | StreambufStream<std::streambuf*> ktx2Stream{inputStream->rdbuf(), std::ios::in | std::ios::binary}; |
| 217 | auto ret = ktxTexture2_CreateFromStream(ktx2Stream.stream(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, texture.pHandle()); |
| 218 | if (ret != KTX_SUCCESS) |
| 219 | fatal(rc::INVALID_FILE, "Failed to create KTX2 texture: {}", ktxErrorString(ret)); |
| 220 | |
| 221 | if (texture->supercompressionScheme != KTX_SS_NONE) |
| 222 | fatal(rc::INVALID_FILE, "Cannot encode KTX2 file with {} supercompression.", |
| 223 | toString(ktxSupercmpScheme(texture->supercompressionScheme))); |
| 224 | |
| 225 | const auto* bdfd = texture->pDfd + 1; |
| 226 | if (khr_df_model_e(KHR_DFDVAL(bdfd, MODEL)) == KHR_DF_MODEL_ASTC && options.encodeASTC) |
| 227 | fatal_usage("Encoding from ASTC format {} to another ASTC format {} is not supported.", toString(VkFormat(texture->vkFormat)), toString(options.vkFormat)); |
| 228 | |
| 229 | switch (texture->vkFormat) { |
| 230 | case VK_FORMAT_R8_UNORM: |
| 231 | case VK_FORMAT_R8_SRGB: |
| 232 | case VK_FORMAT_R8G8_UNORM: |
| 233 | case VK_FORMAT_R8G8_SRGB: |
| 234 | case VK_FORMAT_R8G8B8_UNORM: |
| 235 | case VK_FORMAT_R8G8B8_SRGB: |
| 236 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 237 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 238 | // Allowed formats |
| 239 | break; |
| 240 | default: |
| 241 | fatal_usage("Only R8, RG8, RGB8, or RGBA8 UNORM and SRGB formats can be encoded, " |
| 242 | "but format is {}.", toString(VkFormat(texture->vkFormat))); |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | // Convert 1D textures to 2D (we could consider 1D as an invalid input) |
| 247 | texture->numDimensions = std::max(2u, texture->numDimensions); |
| 248 | |
| 249 | // Modify KTXwriter metadata |
| 250 | const auto writer = fmt::format("{} {}", commandName, version(options.testrun)); |
| 251 | ktxHashList_DeleteKVPair(&texture->kvDataHead, KTX_WRITER_KEY); |
| 252 | ktxHashList_AddKVPair(&texture->kvDataHead, KTX_WRITER_KEY, |
| 253 | static_cast<uint32_t>(writer.size() + 1), // +1 to include the \0 |
| 254 | writer.c_str()); |
| 255 | |
| 256 | khr_df_transfer_e tf = ktxTexture2_GetTransferFunction_e(texture); |
| 257 | if (options.ktxBasisParams::normalMap && tf != KHR_DF_TRANSFER_LINEAR) |
| 258 | fatal(rc::INVALID_FILE, |
| 259 | "--normal-mode specified but the input file uses non-linear transfer function {}.", |
| 260 | toString(tf)); |
| 261 | |
| 262 | MetricsCalculator metrics; |
| 263 | metrics.saveReferenceImages(texture, options, *this); |
| 264 | |
| 265 | if (options.vkFormat != VK_FORMAT_UNDEFINED) { |
| 266 | options.mode = KTX_PACK_ASTC_ENCODER_MODE_LDR; // TODO: Fix me for HDR textures |
| 267 | ret = ktxTexture2_CompressAstcEx(texture, &options); |
| 268 | if (ret != KTX_SUCCESS) |
nothing calls this directly
no test coverage detected