Skip to content

Commit c401f2f

Browse files
authored
Fix exporting R8 files as PNG using libpng (#503)
1 parent 327c200 commit c401f2f

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Auxiliary/DirectXTexPNG.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ namespace
126126
void Update() noexcept(false)
127127
{
128128
png_read_info(st, info);
129+
// check for unsupported cases
130+
png_byte interlacing = png_get_interlace_type(st, info);
131+
if (interlacing != PNG_INTERLACE_NONE)
132+
{
133+
throw std::invalid_argument{ "interlacing not supported" };
134+
}
129135
// color handling
130136
png_byte color_type = png_get_color_type(st, info);
131137
if (color_type == PNG_COLOR_TYPE_GRAY)
@@ -185,6 +191,7 @@ namespace
185191
/// @todo More correct DXGI_FORMAT mapping
186192
void GetHeader(TexMetadata& metadata) noexcept(false)
187193
{
194+
metadata = {};
188195
metadata.width = png_get_image_width(st, info);
189196
metadata.height = png_get_image_height(st, info);
190197
metadata.arraySize = 1;
@@ -194,7 +201,7 @@ namespace
194201
metadata.format = GuessFormat();
195202
png_byte color_type = png_get_color_type(st, info);
196203
bool have_alpha = (color_type & PNG_COLOR_MASK_ALPHA);
197-
if (have_alpha == false)
204+
if (have_alpha == false && (metadata.format != DXGI_FORMAT_R8_UNORM))
198205
metadata.miscFlags2 |= TEX_ALPHA_MODE_OPAQUE;
199206
}
200207

@@ -266,6 +273,7 @@ namespace
266273
{
267274
case DXGI_FORMAT_R8_UNORM:
268275
color_type = PNG_COLOR_TYPE_GRAY;
276+
channel = 1;
269277
break;
270278
case DXGI_FORMAT_B8G8R8A8_UNORM:
271279
case DXGI_FORMAT_B8G8R8X8_UNORM:
@@ -275,7 +283,7 @@ namespace
275283
color_type = PNG_COLOR_TYPE_RGBA;
276284
break;
277285
default:
278-
return E_INVALIDARG;
286+
return HRESULT_E_NOT_SUPPORTED;
279287
}
280288

281289
png_set_IHDR(st, info,
@@ -332,6 +340,10 @@ HRESULT DirectX::GetMetadataFromPNGFile(
332340
return (ec.code().value() == ENOENT) ? HRESULT_ERROR_FILE_NOT_FOUND : E_FAIL;
333341
#endif
334342
}
343+
catch (const std::invalid_argument&)
344+
{
345+
return HRESULT_E_NOT_SUPPORTED;
346+
}
335347
catch (const std::exception&)
336348
{
337349
return E_FAIL;
@@ -373,6 +385,10 @@ HRESULT DirectX::LoadFromPNGFile(
373385
return (ec.code().value() == ENOENT) ? HRESULT_ERROR_FILE_NOT_FOUND : E_FAIL;
374386
#endif
375387
}
388+
catch (const std::invalid_argument&)
389+
{
390+
return HRESULT_E_NOT_SUPPORTED;
391+
}
376392
catch (const std::exception&)
377393
{
378394
image.Release();

0 commit comments

Comments
 (0)