Handling exceptions and destroying texture on exception, added else branch in case if classId is something else
This commit is contained in:
@@ -18,33 +18,53 @@ namespace OpenVulkano::Image
|
||||
std::unique_ptr<Image> ImageLoaderKtx::loadFromFile(const std::string& filePath)
|
||||
{
|
||||
ktxTexture* texture;
|
||||
KTX_error_code result =
|
||||
KTX_error_code error_code =
|
||||
ktxTexture_CreateFromNamedFile(filePath.c_str(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &texture);
|
||||
|
||||
if (result != KTX_SUCCESS)
|
||||
if (error_code != KTX_SUCCESS)
|
||||
{
|
||||
throw std::runtime_error("Failed to load KTX texture: " + std::string(ktxErrorString(result)));
|
||||
throw std::runtime_error("Failed to load KTX texture: " + std::string(ktxErrorString(error_code)));
|
||||
}
|
||||
|
||||
auto image = ExtractImage(texture);
|
||||
ktxTexture_Destroy(texture);
|
||||
return image;
|
||||
try
|
||||
{
|
||||
auto result = ExtractImage(texture);
|
||||
ktxTexture_Destroy(texture);
|
||||
return result;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
ktxTexture_Destroy(texture);
|
||||
throw;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::unique_ptr<Image> ImageLoaderKtx::loadFromMemory(const std::vector<uint8_t>& buffer)
|
||||
{
|
||||
ktxTexture* texture;
|
||||
KTX_error_code result =
|
||||
KTX_error_code error_code =
|
||||
ktxTexture_CreateFromMemory(buffer.data(), buffer.size(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &texture);
|
||||
|
||||
if (result != KTX_SUCCESS)
|
||||
if (error_code != KTX_SUCCESS)
|
||||
{
|
||||
throw std::runtime_error("Failed to load KTX texture from memory: " + std::string(ktxErrorString(result)));
|
||||
throw std::runtime_error("Failed to load KTX texture from memory: " + std::string(ktxErrorString(error_code)));
|
||||
}
|
||||
|
||||
auto image = ExtractImage(texture);
|
||||
ktxTexture_Destroy(texture);
|
||||
return image;
|
||||
try
|
||||
{
|
||||
auto result = ExtractImage(texture);
|
||||
ktxTexture_Destroy(texture);
|
||||
return result;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
ktxTexture_Destroy(texture);
|
||||
throw;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Math::Vector2i ImageLoaderKtx::GetImageDimensions(const std::string& filename)
|
||||
@@ -98,7 +118,11 @@ namespace OpenVulkano::Image
|
||||
else if (texture->classId == ktxTexture2_c)
|
||||
{
|
||||
ktxTexture2* tx = reinterpret_cast<ktxTexture2*>(texture);
|
||||
image->dataFormat = *reinterpret_cast<DataFormat::Format*>(&tx->vkFormat);
|
||||
image->dataFormat = reinterpret_cast<DataFormat::Format&>(tx->vkFormat);
|
||||
}
|
||||
else [[unlikely]]
|
||||
{
|
||||
throw std::runtime_error("ktxTexture has unhandled classId!");
|
||||
}
|
||||
|
||||
image->data = Array<uint8_t>(texture->dataSize);
|
||||
|
||||
Reference in New Issue
Block a user