// static void D3D11Utils_Texture::copyImageToTexture( const Image4f& im, std::shared_ptr< DynamicTexture2D > tex, bool flipUD ) { int width = im.width(); int height = im.height(); D3D11_MAPPED_SUBRESOURCE mapping = tex->mapForWriteDiscard(); const float* sourceData = im.pixels(); ubyte* destDataBytes = reinterpret_cast< ubyte* >( mapping.pData ); // if the pitch matches and no flip is requested // then just directly copy if( mapping.RowPitch == 4 * width * sizeof( float ) && !flipUD ) { float* destDataFloat = reinterpret_cast< float* >( mapping.pData ); memcpy( destDataFloat, sourceData, 4 * width * height * sizeof( float ) ); } // otherwise, have to go row by row else { for( int y = 0; y < height; ++y ) { int yy = flipUD ? height - y - 1 : y; const float* sourceRow = &( sourceData[ 4 * yy * width ] ); ubyte* destRow = &( destDataBytes[ y * mapping.RowPitch ] ); memcpy( destRow, sourceRow, 4 * width * sizeof( float ) ); } } tex->unmap(); }
// static void FormatConversion::image4fToImage3ub( const Image4f& source, Image3ub& destination, bool flipUpDown ) { int width = source.width(); int height = source.height(); for( int y = 0; y < height; ++y ) { int yy = y; if( flipUpDown ) { yy = height - y - 1; } for( int x = 0; x < width; ++x ) { Vector4f input = source.pixel( x, y ); Vector3i output = ColorUtils::floatToInt( input ).xyz(); destination.setPixel( x, yy, output ); } } }
// static void FormatConversion::image3ubToImage4f( const Image3ub& source, Image4f& destination, bool flipUpDown, float fillAlpha ) { int width = source.width(); int height = source.height(); for( int y = 0; y < height; ++y ) { int yy = y; if( flipUpDown ) { yy = height - y - 1; } for( int x = 0; x < width; ++x ) { Vector3i input = source.pixel( x, y ); Vector3f output = ColorUtils::intToFloat( input ); destination.setPixel( x, yy, Vector4f( output, fillAlpha ) ); } } }
// static void D3D11Utils_Texture::copyTextureToImage( ID3D11Device* pDevice, ID3D11Texture2D* pTexture, Image4f& im ) { D3D11_TEXTURE2D_DESC desc; pTexture->GetDesc( &desc ); int width = desc.Width; int height = desc.Height; std::shared_ptr< StagingTexture2D > pST; if( desc.Format == DXGI_FORMAT_R32G32_FLOAT ) { pST.reset( StagingTexture2D::createFloat2( pDevice, width, height ) ); } else if( desc.Format == DXGI_FORMAT_R32G32B32A32_FLOAT ) { pST.reset( StagingTexture2D::createFloat4( pDevice, width, height ) ); } if( desc.Format == DXGI_FORMAT_R32G32_FLOAT ) { pST->copyFrom( pTexture ); D3D11_MAPPED_SUBRESOURCE mt = pST->mapForReadWrite(); ubyte* sourceData = reinterpret_cast< ubyte* >( mt.pData ); for( int y = 0; y < height; ++y ) { float* sourceRow = reinterpret_cast< float* >( &( sourceData[ y * mt.RowPitch ] ) ); for( int x = 0; x < width; ++x ) { float r = sourceRow[ 2 * x ]; float g = sourceRow[ 2 * x + 1 ]; im.setPixel( x, y, Vector4f( r, g, 0, 1 ) ); } } pST->unmap(); } else if( desc.Format == DXGI_FORMAT_R32G32B32A32_FLOAT ) { pST->copyFrom( pTexture ); D3D11_MAPPED_SUBRESOURCE mapping = pST->mapForReadWrite(); ubyte* sourceData = reinterpret_cast< ubyte* >( mapping.pData ); if( mapping.RowPitch == 4 * width * sizeof( float ) ) { memcpy( im.pixels(), sourceData, 4 * width * height * sizeof( float ) ); } else { for( int y = 0; y < height; ++y ) { float* sourceRow = reinterpret_cast< float* >( &( sourceData[ y * mapping.RowPitch ] ) ); float* destinationRow = im.rowPointer( y ); memcpy( destinationRow, sourceRow, 4 * width * sizeof( float ) ); } } pST->unmap(); } else { printf( "Warning: unable to copy texture to image, format is unsupported\n" ); } }
// static std::shared_ptr< DynamicTexture2D > D3D11Utils_Texture::createTextureFromImage( ID3D11Device* pDevice, const Image4f& im, bool flipUD ) { std::shared_ptr< DynamicTexture2D > pTexture( DynamicTexture2D::createFloat4( pDevice, im.width(), im.height() ) ); copyImageToTexture( im, pTexture, flipUD ); return pTexture; }