1
0
mirror of https://github.com/microsoft/DirectXTex synced 2024-11-21 12:00:06 +00:00

Fix exporting R8 files as PNG using libpng (#503)

This commit is contained in:
Bruno Ancona 2024-09-02 20:46:13 -06:00 committed by GitHub
parent 327c200991
commit c401f2fe16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -126,6 +126,12 @@ namespace
void Update() noexcept(false) void Update() noexcept(false)
{ {
png_read_info(st, info); png_read_info(st, info);
// check for unsupported cases
png_byte interlacing = png_get_interlace_type(st, info);
if (interlacing != PNG_INTERLACE_NONE)
{
throw std::invalid_argument{ "interlacing not supported" };
}
// color handling // color handling
png_byte color_type = png_get_color_type(st, info); png_byte color_type = png_get_color_type(st, info);
if (color_type == PNG_COLOR_TYPE_GRAY) if (color_type == PNG_COLOR_TYPE_GRAY)
@ -185,6 +191,7 @@ namespace
/// @todo More correct DXGI_FORMAT mapping /// @todo More correct DXGI_FORMAT mapping
void GetHeader(TexMetadata& metadata) noexcept(false) void GetHeader(TexMetadata& metadata) noexcept(false)
{ {
metadata = {};
metadata.width = png_get_image_width(st, info); metadata.width = png_get_image_width(st, info);
metadata.height = png_get_image_height(st, info); metadata.height = png_get_image_height(st, info);
metadata.arraySize = 1; metadata.arraySize = 1;
@ -194,7 +201,7 @@ namespace
metadata.format = GuessFormat(); metadata.format = GuessFormat();
png_byte color_type = png_get_color_type(st, info); png_byte color_type = png_get_color_type(st, info);
bool have_alpha = (color_type & PNG_COLOR_MASK_ALPHA); bool have_alpha = (color_type & PNG_COLOR_MASK_ALPHA);
if (have_alpha == false) if (have_alpha == false && (metadata.format != DXGI_FORMAT_R8_UNORM))
metadata.miscFlags2 |= TEX_ALPHA_MODE_OPAQUE; metadata.miscFlags2 |= TEX_ALPHA_MODE_OPAQUE;
} }
@ -266,6 +273,7 @@ namespace
{ {
case DXGI_FORMAT_R8_UNORM: case DXGI_FORMAT_R8_UNORM:
color_type = PNG_COLOR_TYPE_GRAY; color_type = PNG_COLOR_TYPE_GRAY;
channel = 1;
break; break;
case DXGI_FORMAT_B8G8R8A8_UNORM: case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_B8G8R8X8_UNORM: case DXGI_FORMAT_B8G8R8X8_UNORM:
@ -275,7 +283,7 @@ namespace
color_type = PNG_COLOR_TYPE_RGBA; color_type = PNG_COLOR_TYPE_RGBA;
break; break;
default: default:
return E_INVALIDARG; return HRESULT_E_NOT_SUPPORTED;
} }
png_set_IHDR(st, info, png_set_IHDR(st, info,
@ -332,6 +340,10 @@ HRESULT DirectX::GetMetadataFromPNGFile(
return (ec.code().value() == ENOENT) ? HRESULT_ERROR_FILE_NOT_FOUND : E_FAIL; return (ec.code().value() == ENOENT) ? HRESULT_ERROR_FILE_NOT_FOUND : E_FAIL;
#endif #endif
} }
catch (const std::invalid_argument&)
{
return HRESULT_E_NOT_SUPPORTED;
}
catch (const std::exception&) catch (const std::exception&)
{ {
return E_FAIL; return E_FAIL;
@ -373,6 +385,10 @@ HRESULT DirectX::LoadFromPNGFile(
return (ec.code().value() == ENOENT) ? HRESULT_ERROR_FILE_NOT_FOUND : E_FAIL; return (ec.code().value() == ENOENT) ? HRESULT_ERROR_FILE_NOT_FOUND : E_FAIL;
#endif #endif
} }
catch (const std::invalid_argument&)
{
return HRESULT_E_NOT_SUPPORTED;
}
catch (const std::exception&) catch (const std::exception&)
{ {
image.Release(); image.Release();