diff --git a/DirectXTex/DirectXTex.h b/DirectXTex/DirectXTex.h index f2605bf..978c11f 100644 --- a/DirectXTex/DirectXTex.h +++ b/DirectXTex/DirectXTex.h @@ -45,6 +45,7 @@ #define _Inout_updates_all_(exp) #define _Inout_updates_all_opt_(exp) #define _Outptr_ +#define _COM_Outptr_ #define _When_(a,b) #endif diff --git a/DirectXTex/DirectXTexWIC.cpp b/DirectXTex/DirectXTexWIC.cpp index fc3caec..3ef490b 100644 --- a/DirectXTex/DirectXTexWIC.cpp +++ b/DirectXTex/DirectXTexWIC.cpp @@ -15,6 +15,56 @@ #include "directxtexp.h" +//------------------------------------------------------------------------------------- +// IStream support for WIC Memory routines +//------------------------------------------------------------------------------------- + +#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) && (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) + + #include + #pragma comment(lib,"shcore.lib") + +#ifdef __cplusplus_winrt + + static inline HRESULT CreateMemoryStream( _COM_Outptr_ IStream** stream ) + { + auto randomAccessStream = ref new ::Windows::Storage::Streams::InMemoryRandomAccessStream(); + return CreateStreamOverRandomAccessStream( randomAccessStream, __uuidof(IStream), reinterpret_cast( &stream ) ); + } + +#else + + #include + #include + #include + + static inline HRESULT CreateMemoryStream( _COM_Outptr_ IStream** stream ) + { + *stream = nullptr; + Microsoft::WRL::ComPtr abiStream; + HRESULT hr = Windows::Foundation::ActivateInstance( + Microsoft::WRL::Wrappers::HStringReference( RuntimeClass_Windows_Storage_Streams_InMemoryRandomAccessStream ).Get(), + &abiStream); + + if (SUCCEEDED(hr)) + { + hr = CreateStreamOverRandomAccessStream( abiStream.Get(), __uuidof(IStream), reinterpret_cast( &stream ) ); + } + return hr; + } + +#endif // __cplusplus_winrt + +#else + + static inline HRESULT CreateMemoryStream( _COM_Outptr_ IStream** stream ) + { + return CreateStreamOnHGlobal( 0, TRUE, stream ); + } + +#endif + + //------------------------------------------------------------------------------------- // WIC Pixel Format nearest conversion table //------------------------------------------------------------------------------------- @@ -918,7 +968,7 @@ HRESULT SaveToWICMemory( const Image& image, DWORD flags, REFGUID containerForma blob.Release(); ScopedObject stream; - HRESULT hr = CreateStreamOnHGlobal( 0, TRUE, &stream ); + HRESULT hr = CreateMemoryStream( &stream ); if ( FAILED(hr) ) return hr; @@ -965,7 +1015,7 @@ HRESULT SaveToWICMemory( const Image* images, size_t nimages, DWORD flags, REFGU blob.Release(); ScopedObject stream; - HRESULT hr = CreateStreamOnHGlobal( 0, TRUE, &stream ); + HRESULT hr = CreateMemoryStream( &stream ); if ( FAILED(hr) ) return hr;