1
0
mirror of https://github.com/microsoft/DirectXTex synced 2024-11-22 04:20:07 +00:00

Fixed bugs in WIC loader for multframe images when resizing was required

- texconv & texassemble now load multi-frames if present from WIC files (tiff, gif)
This commit is contained in:
walbourn_cp 2015-03-17 18:07:21 -07:00
parent 5541a2cbcf
commit f618e9f19d
3 changed files with 52 additions and 31 deletions

View File

@ -439,18 +439,41 @@ static HRESULT _DecodeMultiframe( _In_ DWORD flags, _In_ const TexMetadata& meta
if ( FAILED(hr) )
return hr;
if ( memcmp( &pfGuid, &sourceGUID, sizeof(WICPixelFormatGUID) ) == 0 )
{
if ( w == metadata.width && h == metadata.height )
{
// This frame does not need resized or format converted, just copy...
// This frame does not need resized
if ( memcmp( &pfGuid, &sourceGUID, sizeof(WICPixelFormatGUID) ) == 0 )
{
hr = frame->CopyPixels( 0, static_cast<UINT>( img->rowPitch ), static_cast<UINT>( img->slicePitch ), img->pixels );
if ( FAILED(hr) )
return hr;
}
else
{
// This frame needs resizing, but not format converted
ComPtr<IWICFormatConverter> FC;
hr = pWIC->CreateFormatConverter( FC.GetAddressOf() );
if ( FAILED(hr) )
return hr;
BOOL canConvert = FALSE;
hr = FC->CanConvert( pfGuid, sourceGUID, &canConvert );
if ( FAILED(hr) || !canConvert )
{
return E_UNEXPECTED;
}
hr = FC->Initialize( frame.Get(), sourceGUID, _GetWICDither( flags ), 0, 0, WICBitmapPaletteTypeCustom );
if ( FAILED(hr) )
return hr;
hr = FC->CopyPixels( 0, static_cast<UINT>( img->rowPitch ), static_cast<UINT>( img->slicePitch ), img->pixels );
if ( FAILED(hr) )
return hr;
}
}
else
{
// This frame needs resizing
ComPtr<IWICBitmapScaler> scaler;
hr = pWIC->CreateBitmapScaler( scaler.GetAddressOf() );
if ( FAILED(hr) )
@ -460,53 +483,41 @@ static HRESULT _DecodeMultiframe( _In_ DWORD flags, _In_ const TexMetadata& meta
if ( FAILED(hr) )
return hr;
WICPixelFormatGUID pfScaler;
hr = scaler->GetPixelFormat( &pfScaler );
if ( FAILED(hr) )
return hr;
if ( memcmp( &pfScaler, &sourceGUID, sizeof(WICPixelFormatGUID) ) == 0 )
{
hr = scaler->CopyPixels( 0, static_cast<UINT>( img->rowPitch ), static_cast<UINT>( img->slicePitch ), img->pixels );
if ( FAILED(hr) )
return hr;
}
}
else
{
// This frame required format conversion
// The WIC bitmap scaler is free to return a different pixel format than the source image, so here we
// convert it to our desired format
ComPtr<IWICFormatConverter> FC;
hr = pWIC->CreateFormatConverter( FC.GetAddressOf() );
if ( FAILED(hr) )
return hr;
BOOL canConvert = FALSE;
hr = FC->CanConvert( sourceGUID, pfGuid, &canConvert );
hr = FC->CanConvert( pfScaler, sourceGUID, &canConvert );
if ( FAILED(hr) || !canConvert )
{
return E_UNEXPECTED;
}
hr = FC->Initialize( frame.Get(), pfGuid, _GetWICDither( flags ), 0, 0, WICBitmapPaletteTypeCustom );
hr = FC->Initialize( scaler.Get(), sourceGUID, _GetWICDither( flags ), 0, 0, WICBitmapPaletteTypeCustom );
if ( FAILED(hr) )
return hr;
if ( w == metadata.width && h == metadata.height )
{
// This frame is the same size, no need to scale
hr = FC->CopyPixels( 0, static_cast<UINT>( img->rowPitch ), static_cast<UINT>( img->slicePitch ), img->pixels );
if ( FAILED(hr) )
return hr;
}
else
{
// This frame needs resizing and format converted
ComPtr<IWICBitmapScaler> scaler;
hr = pWIC->CreateBitmapScaler( scaler.GetAddressOf() );
if ( FAILED(hr) )
return hr;
hr = scaler->Initialize( FC.Get(), static_cast<UINT>( metadata.width ), static_cast<UINT>( metadata.height ), _GetWICInterp( flags ) );
if ( FAILED(hr) )
return hr;
hr = scaler->CopyPixels( 0, static_cast<UINT>( img->rowPitch ), static_cast<UINT>( img->slicePitch ), img->pixels );
if ( FAILED(hr) )
return hr;
}
}
}

View File

@ -217,6 +217,9 @@ void PrintInfo( const TexMetadata& info )
if ( info.mipLevels > 1 )
wprintf( L",%Iu", info.mipLevels);
if ( info.arraySize > 1 )
wprintf( L",%Iu", info.arraySize);
wprintf( L" ");
PrintFormat( info.format );
@ -541,7 +544,7 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
static_assert( WIC_FLAGS_FILTER_CUBIC == TEX_FILTER_CUBIC, "WIC_FLAGS_* & TEX_FILTER_* should match" );
static_assert( WIC_FLAGS_FILTER_FANT == TEX_FILTER_FANT, "WIC_FLAGS_* & TEX_FILTER_* should match" );
hr = LoadFromWICFile( pConv->szSrc, dwFilter, &info, *image.get() );
hr = LoadFromWICFile( pConv->szSrc, dwFilter | WIC_FLAGS_ALL_FRAMES, &info, *image.get() );
if ( FAILED(hr) )
{
wprintf( L" FAILED (%x)\n", hr);

View File

@ -370,6 +370,9 @@ void PrintInfo( const TexMetadata& info )
if ( info.mipLevels > 1 )
wprintf( L",%Iu", info.mipLevels);
if ( info.arraySize > 1 )
wprintf( L",%Iu", info.arraySize);
wprintf( L" ");
PrintFormat( info.format );
@ -1045,7 +1048,11 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
static_assert( WIC_FLAGS_FILTER_CUBIC == TEX_FILTER_CUBIC, "WIC_FLAGS_* & TEX_FILTER_* should match" );
static_assert( WIC_FLAGS_FILTER_FANT == TEX_FILTER_FANT, "WIC_FLAGS_* & TEX_FILTER_* should match" );
hr = LoadFromWICFile( pConv->szSrc, dwFilter, &info, *image );
DWORD wicFlags = dwFilter;
if (FileType == CODEC_DDS)
wicFlags |= WIC_FLAGS_ALL_FRAMES;
hr = LoadFromWICFile( pConv->szSrc, wicFlags, &info, *image );
if ( FAILED(hr) )
{
wprintf( L" FAILED (%x)\n", hr);