DirectXTexP.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //-------------------------------------------------------------------------------------
  2. // DirectXTexp.h
  3. //
  4. // DirectX Texture Library - Private header
  5. //
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  7. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  8. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  9. // PARTICULAR PURPOSE.
  10. //
  11. // Copyright (c) Microsoft Corporation. All rights reserved.
  12. //
  13. // http://go.microsoft.com/fwlink/?LinkId=248926
  14. //-------------------------------------------------------------------------------------
  15. #if defined(_MSC_VER) && (_MSC_VER > 1000)
  16. #pragma once
  17. #endif
  18. #define NOMINMAX
  19. #include <windows.h>
  20. #include <directxmath.h>
  21. #include <directxpackedvector.h>
  22. #include <assert.h>
  23. #include <malloc.h>
  24. #include <memory>
  25. #include <vector>
  26. #include <stdlib.h>
  27. #include <search.h>
  28. #include <ole2.h>
  29. #include "directxtex.h"
  30. // VS 2010's stdint.h conflicts with intsafe.h
  31. #pragma warning(push)
  32. #pragma warning(disable : 4005)
  33. #include <wincodec.h>
  34. #include <wrl.h>
  35. #pragma warning(pop)
  36. #include "scoped.h"
  37. struct IWICImagingFactory;
  38. #define TEX_FILTER_MASK 0xF00000
  39. namespace DirectX
  40. {
  41. //---------------------------------------------------------------------------------
  42. // WIC helper functions
  43. DXGI_FORMAT _WICToDXGI( _In_ const GUID& guid );
  44. bool _DXGIToWIC( _In_ DXGI_FORMAT format, _Out_ GUID& guid, _In_ bool ignoreRGBvsBGR = false );
  45. DWORD _CheckWICColorSpace( _In_ const GUID& sourceGUID, _In_ const GUID& targetGUID );
  46. IWICImagingFactory* _GetWIC();
  47. bool _IsWIC2();
  48. inline WICBitmapDitherType _GetWICDither( _In_ DWORD flags )
  49. {
  50. static_assert( TEX_FILTER_DITHER == 0x10000, "TEX_FILTER_DITHER* flag values don't match mask" );
  51. static_assert( TEX_FILTER_DITHER == WIC_FLAGS_DITHER, "TEX_FILTER_DITHER* should match WIC_FLAGS_DITHER*" );
  52. static_assert( TEX_FILTER_DITHER_DIFFUSION == WIC_FLAGS_DITHER_DIFFUSION, "TEX_FILTER_DITHER* should match WIC_FLAGS_DITHER*" );
  53. switch( flags & 0xF0000 )
  54. {
  55. case TEX_FILTER_DITHER:
  56. return WICBitmapDitherTypeOrdered4x4;
  57. case TEX_FILTER_DITHER_DIFFUSION:
  58. return WICBitmapDitherTypeErrorDiffusion;
  59. default:
  60. return WICBitmapDitherTypeNone;
  61. }
  62. }
  63. inline WICBitmapInterpolationMode _GetWICInterp( _In_ DWORD flags )
  64. {
  65. static_assert( TEX_FILTER_POINT == 0x100000, "TEX_FILTER_ flag values don't match TEX_FILTER_MASK" );
  66. static_assert( TEX_FILTER_POINT == WIC_FLAGS_FILTER_POINT, "TEX_FILTER_* flags should match WIC_FLAGS_FILTER_*" );
  67. static_assert( TEX_FILTER_LINEAR == WIC_FLAGS_FILTER_LINEAR, "TEX_FILTER_* flags should match WIC_FLAGS_FILTER_*" );
  68. static_assert( TEX_FILTER_CUBIC == WIC_FLAGS_FILTER_CUBIC, "TEX_FILTER_* flags should match WIC_FLAGS_FILTER_*" );
  69. static_assert( TEX_FILTER_FANT == WIC_FLAGS_FILTER_FANT, "TEX_FILTER_* flags should match WIC_FLAGS_FILTER_*" );
  70. switch( flags & TEX_FILTER_MASK )
  71. {
  72. case TEX_FILTER_POINT:
  73. return WICBitmapInterpolationModeNearestNeighbor;
  74. case TEX_FILTER_LINEAR:
  75. return WICBitmapInterpolationModeLinear;
  76. case TEX_FILTER_CUBIC:
  77. return WICBitmapInterpolationModeCubic;
  78. case TEX_FILTER_FANT:
  79. default:
  80. return WICBitmapInterpolationModeFant;
  81. }
  82. }
  83. //---------------------------------------------------------------------------------
  84. // Image helper functions
  85. void _DetermineImageArray( _In_ const TexMetadata& metadata, _In_ DWORD cpFlags,
  86. _Out_ size_t& nImages, _Out_ size_t& pixelSize );
  87. _Success_(return != false)
  88. bool _SetupImageArray( _In_reads_bytes_(pixelSize) uint8_t *pMemory, _In_ size_t pixelSize,
  89. _In_ const TexMetadata& metadata, _In_ DWORD cpFlags,
  90. _Out_writes_(nImages) Image* images, _In_ size_t nImages );
  91. //---------------------------------------------------------------------------------
  92. // Conversion helper functions
  93. enum TEXP_SCANLINE_FLAGS
  94. {
  95. TEXP_SCANLINE_NONE = 0,
  96. TEXP_SCANLINE_SETALPHA = 0x1, // Set alpha channel to known opaque value
  97. TEXP_SCANLINE_LEGACY = 0x2, // Enables specific legacy format conversion cases
  98. };
  99. enum CONVERT_FLAGS
  100. {
  101. CONVF_FLOAT = 0x1,
  102. CONVF_UNORM = 0x2,
  103. CONVF_UINT = 0x4,
  104. CONVF_SNORM = 0x8,
  105. CONVF_SINT = 0x10,
  106. CONVF_DEPTH = 0x20,
  107. CONVF_STENCIL = 0x40,
  108. CONVF_SHAREDEXP = 0x80,
  109. CONVF_BGR = 0x100,
  110. CONVF_XR = 0x200,
  111. CONVF_PACKED = 0x400,
  112. CONVF_BC = 0x800,
  113. CONVF_YUV = 0x1000,
  114. CONVF_R = 0x10000,
  115. CONVF_G = 0x20000,
  116. CONVF_B = 0x40000,
  117. CONVF_A = 0x80000,
  118. CONVF_RGB_MASK = 0x70000,
  119. CONVF_RGBA_MASK = 0xF0000,
  120. };
  121. DWORD _GetConvertFlags( _In_ DXGI_FORMAT format );
  122. void _CopyScanline( _When_(pDestination == pSource, _Inout_updates_bytes_(outSize))
  123. _When_(pDestination != pSource, _Out_writes_bytes_(outSize))
  124. LPVOID pDestination, _In_ size_t outSize,
  125. _In_reads_bytes_(inSize) LPCVOID pSource, _In_ size_t inSize,
  126. _In_ DXGI_FORMAT format, _In_ DWORD flags );
  127. void _SwizzleScanline( _When_(pDestination == pSource, _In_)
  128. _When_(pDestination != pSource, _Out_writes_bytes_(outSize))
  129. LPVOID pDestination, _In_ size_t outSize,
  130. _In_reads_bytes_(inSize) LPCVOID pSource, _In_ size_t inSize,
  131. _In_ DXGI_FORMAT format, _In_ DWORD flags );
  132. _Success_(return != false)
  133. bool _ExpandScanline( _Out_writes_bytes_(outSize) LPVOID pDestination, _In_ size_t outSize,
  134. _In_ DXGI_FORMAT outFormat,
  135. _In_reads_bytes_(inSize) LPCVOID pSource, _In_ size_t inSize,
  136. _In_ DXGI_FORMAT inFormat, _In_ DWORD flags );
  137. _Success_(return != false)
  138. bool _LoadScanline( _Out_writes_(count) XMVECTOR* pDestination, _In_ size_t count,
  139. _In_reads_bytes_(size) LPCVOID pSource, _In_ size_t size, _In_ DXGI_FORMAT format );
  140. _Success_(return != false)
  141. bool _LoadScanlineLinear( _Out_writes_(count) XMVECTOR* pDestination, _In_ size_t count,
  142. _In_reads_bytes_(size) LPCVOID pSource, _In_ size_t size, _In_ DXGI_FORMAT format, _In_ DWORD flags );
  143. _Success_(return != false)
  144. bool _StoreScanline( LPVOID pDestination, _In_ size_t size, _In_ DXGI_FORMAT format,
  145. _In_reads_(count) const XMVECTOR* pSource, _In_ size_t count, _In_ float threshold = 0 );
  146. _Success_(return != false)
  147. bool _StoreScanlineLinear( LPVOID pDestination, _In_ size_t size, _In_ DXGI_FORMAT format,
  148. _Inout_updates_all_(count) XMVECTOR* pSource, _In_ size_t count, _In_ DWORD flags );
  149. _Success_(return != false)
  150. bool _StoreScanlineDither( LPVOID pDestination, _In_ size_t size, _In_ DXGI_FORMAT format,
  151. _Inout_updates_all_(count) XMVECTOR* pSource, _In_ size_t count, _In_ float threshold, size_t y, size_t z,
  152. _Inout_updates_all_opt_(count+2) XMVECTOR* pDiffusionErrors );
  153. HRESULT _ConvertToR32G32B32A32( _In_ const Image& srcImage, _Inout_ ScratchImage& image );
  154. HRESULT _ConvertFromR32G32B32A32( _In_ const Image& srcImage, _In_ const Image& destImage );
  155. HRESULT _ConvertFromR32G32B32A32( _In_ const Image& srcImage, _In_ DXGI_FORMAT format, _Inout_ ScratchImage& image );
  156. HRESULT _ConvertFromR32G32B32A32( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  157. _In_ DXGI_FORMAT format, _Out_ ScratchImage& result );
  158. void _ConvertScanline( _Inout_updates_all_(count) XMVECTOR* pBuffer, _In_ size_t count,
  159. _In_ DXGI_FORMAT outFormat, _In_ DXGI_FORMAT inFormat, _In_ DWORD flags );
  160. //---------------------------------------------------------------------------------
  161. // DDS helper functions
  162. HRESULT _EncodeDDSHeader( _In_ const TexMetadata& metadata, DWORD flags,
  163. _Out_writes_bytes_to_opt_(maxsize, required) LPVOID pDestination, _In_ size_t maxsize, _Out_ size_t& required );
  164. }; // namespace