Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exporting R8 files as PNG using libpng #503

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Auxiliary/DirectXTexPNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ namespace
void Update() noexcept(false)
{
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
png_byte color_type = png_get_color_type(st, info);
if (color_type == PNG_COLOR_TYPE_GRAY)
Expand Down Expand Up @@ -185,6 +191,7 @@ namespace
/// @todo More correct DXGI_FORMAT mapping
void GetHeader(TexMetadata& metadata) noexcept(false)
{
metadata = {};
metadata.width = png_get_image_width(st, info);
metadata.height = png_get_image_height(st, info);
metadata.arraySize = 1;
Expand All @@ -194,7 +201,7 @@ namespace
metadata.format = GuessFormat();
png_byte color_type = png_get_color_type(st, info);
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;
}

Expand Down Expand Up @@ -266,6 +273,7 @@ namespace
{
case DXGI_FORMAT_R8_UNORM:
color_type = PNG_COLOR_TYPE_GRAY;
channel = 1;
break;
case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_B8G8R8X8_UNORM:
Expand All @@ -275,7 +283,7 @@ namespace
color_type = PNG_COLOR_TYPE_RGBA;
break;
default:
return E_INVALIDARG;
return HRESULT_E_NOT_SUPPORTED;
}

png_set_IHDR(st, info,
Expand Down Expand Up @@ -332,6 +340,10 @@ HRESULT DirectX::GetMetadataFromPNGFile(
return (ec.code().value() == ENOENT) ? HRESULT_ERROR_FILE_NOT_FOUND : E_FAIL;
#endif
}
catch (const std::invalid_argument&)
{
return HRESULT_E_NOT_SUPPORTED;
}
catch (const std::exception&)
{
return E_FAIL;
Expand Down Expand Up @@ -373,6 +385,10 @@ HRESULT DirectX::LoadFromPNGFile(
return (ec.code().value() == ENOENT) ? HRESULT_ERROR_FILE_NOT_FOUND : E_FAIL;
#endif
}
catch (const std::invalid_argument&)
{
return HRESULT_E_NOT_SUPPORTED;
}
catch (const std::exception&)
{
image.Release();
Expand Down
Loading