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

Support loading another DX10 DDS variant with permissive #588

Merged
merged 1 commit into from
Feb 20, 2025
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
50 changes: 49 additions & 1 deletion DirectXTex/DirectXTexDDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2006,10 +2006,36 @@ HRESULT DirectX::LoadFromDDSMemoryEx(
return E_FAIL;
}

size_t remaining = size - offset;
if (remaining == 0)
return E_FAIL;

hr = image.Initialize(mdata);
if (FAILED(hr))
return hr;

if (flags & DDS_FLAGS_PERMISSIVE)
{
// For cubemaps, DDS_HEADER_DXT10.arraySize is supposed to be 'number of cubes'.
// This handles cases where the value is incorrectly written as the original 6*numCubes value.
if ((mdata.miscFlags & TEX_MISC_TEXTURECUBE)
&& (convFlags & CONV_FLAGS_DX10)
&& (image.GetPixelsSize() > remaining)
&& ((mdata.arraySize % 6) == 0))
{
mdata.arraySize = mdata.arraySize / 6;
hr = image.Initialize(mdata);
if (FAILED(hr))
return hr;

if (image.GetPixelsSize() > remaining)
{
image.Release();
return HRESULT_E_HANDLE_EOF;
}
}
}

CP_FLAGS cflags = CP_FLAGS_NONE;
if (flags & DDS_FLAGS_LEGACY_DWORD)
{
Expand Down Expand Up @@ -2195,6 +2221,28 @@ HRESULT DirectX::LoadFromDDSFileEx(
if (FAILED(hr))
return hr;

if (flags & DDS_FLAGS_PERMISSIVE)
{
// For cubemaps, DDS_HEADER_DXT10.arraySize is supposed to be 'number of cubes'.
// This handles cases where the value is incorrectly written as the original 6*numCubes value.
if ((mdata.miscFlags & TEX_MISC_TEXTURECUBE)
&& (convFlags & CONV_FLAGS_DX10)
&& (image.GetPixelsSize() > remaining)
&& ((mdata.arraySize % 6) == 0))
{
mdata.arraySize = mdata.arraySize / 6;
hr = image.Initialize(mdata);
if (FAILED(hr))
return hr;

if (image.GetPixelsSize() > remaining)
{
image.Release();
return HRESULT_E_HANDLE_EOF;
}
}
}

if ((convFlags & CONV_FLAGS_EXPAND) || (flags & (DDS_FLAGS_LEGACY_DWORD | DDS_FLAGS_BAD_DXTN_TAILS)))
{
std::unique_ptr<uint8_t[]> temp(new (std::nothrow) uint8_t[remaining]);
Expand Down Expand Up @@ -2370,7 +2418,7 @@ HRESULT DirectX::SaveToDDSMemory(
size_t remaining = blob.GetBufferSize() - required;
pDestination += required;

if (!remaining)
if (remaining == 0)
{
blob.Release();
return E_FAIL;
Expand Down
Loading