21 extern bool _CalculateMipLevels( _In_
size_t width, _In_
size_t height, _Inout_
size_t& mipLevels );
22 extern bool _CalculateMipLevels3D( _In_
size_t width, _In_
size_t height, _In_
size_t depth, _Inout_
size_t& mipLevels );
28 _Use_decl_annotations_
36 size_t _pixelSize = 0;
43 for(
size_t item = 0; item < metadata.
arraySize; ++item )
45 size_t w = metadata.
width;
46 size_t h = metadata.
height;
48 for(
size_t level=0; level < metadata.
mipLevels; ++level )
50 size_t rowPitch, slicePitch;
53 _pixelSize += slicePitch;
67 size_t w = metadata.
width;
68 size_t h = metadata.
height;
69 size_t d = metadata.
depth;
71 for(
size_t level=0; level < metadata.
mipLevels; ++level )
73 size_t rowPitch, slicePitch;
76 for(
size_t slice=0; slice < d; ++slice )
78 _pixelSize += slicePitch;
100 pixelSize = _pixelSize;
107 _Use_decl_annotations_
132 for(
size_t item = 0; item < metadata.
arraySize; ++item )
134 size_t w = metadata.
width;
135 size_t h = metadata.
height;
137 for(
size_t level=0; level < metadata.
mipLevels; ++level )
139 if ( index >= nImages )
144 size_t rowPitch, slicePitch;
147 images[index].
width = w;
152 images[index].
pixels = pixels;
155 pixels += slicePitch;
156 if ( pixels > pEndBits )
177 size_t w = metadata.
width;
178 size_t h = metadata.
height;
179 size_t d = metadata.
depth;
181 for(
size_t level=0; level < metadata.
mipLevels; ++level )
183 size_t rowPitch, slicePitch;
186 for(
size_t slice=0; slice < d; ++slice )
188 if ( index >= nImages )
195 images[index].
width = w;
200 images[index].
pixels = pixels;
203 pixels += slicePitch;
204 if ( pixels > pEndBits )
234 if (
this != &moveFrom )
238 _nimages = moveFrom._nimages;
239 _size = moveFrom._size;
240 _metadata = moveFrom._metadata;
241 _image = moveFrom._image;
242 _memory = moveFrom._memory;
244 moveFrom._nimages = 0;
246 moveFrom._image =
nullptr;
247 moveFrom._memory =
nullptr;
256 _Use_decl_annotations_
263 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
274 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
290 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
302 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
309 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
327 _image =
new (std::nothrow) Image[ nimages ];
332 memset( _image, 0,
sizeof(Image) * nimages );
334 _memory =
reinterpret_cast<uint8_t*
>( _aligned_malloc( pixelSize, 16 ) );
341 if ( !
_SetupImageArray( _memory, pixelSize, _metadata, flags, _image, nimages ) )
350 _Use_decl_annotations_
353 if ( !length || !arraySize )
357 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
360 HRESULT hr =
Initialize2D( fmt, length, 1, arraySize, mipLevels, flags );
369 _Use_decl_annotations_
370 HRESULT
ScratchImage::Initialize2D( DXGI_FORMAT fmt,
size_t width,
size_t height,
size_t arraySize,
size_t mipLevels, DWORD flags )
372 if ( !
IsValid(fmt) || !width || !height || !arraySize )
376 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
383 _metadata.
width = width;
384 _metadata.
height = height;
396 _image =
new (std::nothrow) Image[ nimages ];
401 memset( _image, 0,
sizeof(Image) * nimages );
403 _memory =
reinterpret_cast<uint8_t*
>( _aligned_malloc( pixelSize, 16 ) );
410 if ( !
_SetupImageArray( _memory, pixelSize, _metadata, flags, _image, nimages ) )
419 _Use_decl_annotations_
420 HRESULT
ScratchImage::Initialize3D( DXGI_FORMAT fmt,
size_t width,
size_t height,
size_t depth,
size_t mipLevels, DWORD flags )
422 if ( !
IsValid(fmt) || !width || !height || !depth )
426 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
433 _metadata.
width = width;
434 _metadata.
height = height;
435 _metadata.
depth = depth;
446 _image =
new (std::nothrow) Image[ nimages ];
453 memset( _image, 0,
sizeof(Image) * nimages );
455 _memory =
reinterpret_cast<uint8_t*
>( _aligned_malloc( pixelSize, 16 ) );
463 if ( !
_SetupImageArray( _memory, pixelSize, _metadata, flags, _image, nimages ) )
472 _Use_decl_annotations_
475 if ( !width || !height || !nCubes )
479 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
482 HRESULT hr =
Initialize2D( fmt, width, height, nCubes * 6, mipLevels, flags );
491 _Use_decl_annotations_
494 HRESULT hr = ( srcImage.height > 1 || !allow1D )
495 ?
Initialize2D( srcImage.format, srcImage.width, srcImage.height, 1, 1, flags )
496 :
Initialize1D( srcImage.format, srcImage.width, 1, 1, flags );
505 const uint8_t* sptr =
reinterpret_cast<const uint8_t*
>( srcImage.pixels );
513 size_t spitch = srcImage.rowPitch;
516 size_t size = std::min<size_t>( dpitch, spitch );
518 for(
size_t y = 0;
y < rowCount; ++
y )
520 memcpy_s( dptr, dpitch, sptr, size );
528 _Use_decl_annotations_
531 if ( !images || !nImages )
535 size_t width = images[0].
width;
536 size_t height = images[0].
height;
538 for(
size_t index=0; index <
nImages; ++index )
540 if ( !images[index].pixels )
543 if ( images[index].format != format || images[index].width != width || images[index].height != height )
550 HRESULT hr = ( height > 1 || !allow1D )
551 ?
Initialize2D( format, width, height, nImages, 1, flags )
561 for(
size_t index=0; index <
nImages; ++index )
563 auto sptr =
reinterpret_cast<const uint8_t*
>( images[index].
pixels );
567 assert( index < _nimages );
568 auto dptr =
reinterpret_cast<uint8_t*
>( _image[index].
pixels );
572 size_t spitch = images[index].
rowPitch;
573 size_t dpitch = _image[index].
rowPitch;
575 size_t size = std::min<size_t>( dpitch, spitch );
577 for(
size_t y = 0;
y < rowCount; ++
y )
579 memcpy_s( dptr, dpitch, sptr, size );
588 _Use_decl_annotations_
591 if ( !images || !nImages )
595 if ( ( nImages % 6 ) != 0 )
599 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
610 _Use_decl_annotations_
613 if ( !images || !depth )
616 DXGI_FORMAT format = images[0].format;
617 size_t width = images[0].width;
618 size_t height = images[0].height;
620 for(
size_t slice=0; slice < depth; ++slice )
622 if ( !images[slice].pixels )
625 if ( images[slice].format != format || images[slice].width != width || images[slice].height != height )
632 HRESULT hr =
Initialize3D( format, width, height, depth, 1, flags );
640 for(
size_t slice=0; slice < depth; ++slice )
642 auto sptr =
reinterpret_cast<const uint8_t*
>( images[slice].pixels );
646 assert( slice < _nimages );
647 auto dptr =
reinterpret_cast<uint8_t*
>( _image[slice].
pixels );
651 size_t spitch = images[slice].rowPitch;
652 size_t dpitch = _image[slice].
rowPitch;
654 size_t size = std::min<size_t>( dpitch, spitch );
656 for(
size_t y = 0;
y < rowCount; ++
y )
658 memcpy_s( dptr, dpitch, sptr, size );
680 _aligned_free( _memory );
684 memset(&_metadata, 0,
sizeof(_metadata));
687 _Use_decl_annotations_
705 for(
size_t index = 0; index < _nimages; ++index )
707 _image[ index ].
format = f;
715 _Use_decl_annotations_
733 index = item*( _metadata.
mipLevels ) + mip;
744 size_t d = _metadata.
depth;
746 for(
size_t level = 0; level < mip; ++level )
764 return &_image[index];
777 for(
size_t index = 0; index < _nimages; ++index )
789 static const XMVECTORF32
threshold = { 0.99f, 0.99f, 0.99f, 0.99f };
791 for(
size_t index = 0; index < _nimages; ++index )
793 #pragma warning( suppress : 6011 )
794 const Image& img = _image[ index ];
799 for(
size_t h = 0; h < img.
height; ++h )
804 XMVECTOR* ptr = scanline.get();
805 for(
size_t w = 0; w < img.
width; ++w )
807 XMVECTOR alpha = XMVectorSplatW( *ptr );
808 if ( XMVector4Less( alpha, threshold ) )
bool _CalculateMipLevels3D(_In_ size_t width, _In_ size_t height, _In_ size_t depth, _Inout_ size_t &mipLevels)
std::unique_ptr< DirectX::XMVECTOR, aligned_deleter > ScopedAlignedArrayXMVECTOR
_In_ size_t _In_ const TexMetadata _In_ DWORD cpFlags
const Image * GetImage(_In_ size_t mip, _In_ size_t item, _In_ size_t slice) const
bool IsVideo(_In_ DXGI_FORMAT fmt)
bool _IsAlphaAllOpaqueBC(_In_ const Image &cImage)
bool HasAlpha(_In_ DXGI_FORMAT fmt)
HRESULT InitializeCube(_In_ DXGI_FORMAT fmt, _In_ size_t width, _In_ size_t height, _In_ size_t nCubes, _In_ size_t mipLevels, _In_ DWORD flags=CP_FLAGS_NONE)
bool OverrideFormat(_In_ DXGI_FORMAT f)
_In_ size_t _In_ const TexMetadata _In_ DWORD _In_ size_t nImages
bool IsPlanar(_In_ DXGI_FORMAT fmt)
HRESULT Initialize3DFromImages(_In_reads_(depth) const Image *images, _In_ size_t depth, _In_ DWORD flags=CP_FLAGS_NONE)
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ DXGI_FORMAT _In_ DWORD flags
bool IsPacked(_In_ DXGI_FORMAT fmt)
size_t _In_ DXGI_FORMAT size_t _In_ TEXP_LEGACY_FORMAT _In_ DWORD flags assert(pDestination &&outSize > 0)
bool IsAlphaAllOpaque() const
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float threshold
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
HRESULT Initialize(_In_ const TexMetadata &mdata, _In_ DWORD flags=CP_FLAGS_NONE)
HRESULT Initialize2D(_In_ DXGI_FORMAT fmt, _In_ size_t width, _In_ size_t height, _In_ size_t arraySize, _In_ size_t mipLevels, _In_ DWORD flags=CP_FLAGS_NONE)
_Use_decl_annotations_ bool _LoadScanline(XMVECTOR *pDestination, size_t count, LPCVOID pSource, size_t size, DXGI_FORMAT format)
bool IsCompressed(_In_ DXGI_FORMAT fmt)
HRESULT InitializeCubeFromImages(_In_reads_(nImages) const Image *images, _In_ size_t nImages, _In_ DWORD flags=CP_FLAGS_NONE)
HRESULT Initialize3D(_In_ DXGI_FORMAT fmt, _In_ size_t width, _In_ size_t height, _In_ size_t depth, _In_ size_t mipLevels, _In_ DWORD flags=CP_FLAGS_NONE)
_In_ size_t _In_ const TexMetadata & metadata
HRESULT InitializeFromImage(_In_ const Image &srcImage, _In_ bool allow1D=false, _In_ DWORD flags=CP_FLAGS_NONE)
void ComputePitch(_In_ DXGI_FORMAT fmt, _In_ size_t width, _In_ size_t height, _Out_ size_t &rowPitch, _Out_ size_t &slicePitch, _In_ DWORD flags=CP_FLAGS_NONE)
bool IsDepthStencil(_In_ DXGI_FORMAT fmt)
bool IsValid(_In_ DXGI_FORMAT fmt)
bool IsPalettized(_In_ DXGI_FORMAT fmt)
_Use_decl_annotations_ void _DetermineImageArray(const TexMetadata &metadata, DWORD cpFlags, size_t &nImages, size_t &pixelSize)
HRESULT Initialize1D(_In_ DXGI_FORMAT fmt, _In_ size_t length, _In_ size_t arraySize, _In_ size_t mipLevels, _In_ DWORD flags=CP_FLAGS_NONE)
HRESULT InitializeArrayFromImages(_In_reads_(nImages) const Image *images, _In_ size_t nImages, _In_ bool allow1D=false, _In_ DWORD flags=CP_FLAGS_NONE)
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
ScratchImage & operator=(ScratchImage &&moveFrom)
size_t BitsPerPixel(_In_ DXGI_FORMAT fmt)
_In_ size_t _In_ size_t size
_Use_decl_annotations_ bool _SetupImageArray(uint8_t *pMemory, size_t pixelSize, const TexMetadata &metadata, DWORD cpFlags, Image *images, size_t nImages)
bool _CalculateMipLevels(_In_ size_t width, _In_ size_t height, _Inout_ size_t &mipLevels)
size_t ComputeScanlines(_In_ DXGI_FORMAT fmt, _In_ size_t height)