| 19 | } |
| 20 | |
| 21 | BeResultChoice<BlueStdResult, ImageIO::Result> Tr2TextureReference::Save( const std::wstring& resPath ) |
| 22 | { |
| 23 | if( !m_texture.IsValid() ) |
| 24 | { |
| 25 | return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "Referenced texture is invalid" ); |
| 26 | } |
| 27 | |
| 28 | USE_MAIN_THREAD_RENDER_CONTEXT(); |
| 29 | const void* src; |
| 30 | uint32_t srcPitch; |
| 31 | if( FAILED( m_texture.MapForReading( Tr2TextureSubresource(), src, srcPitch, renderContext ) ) ) |
| 32 | { |
| 33 | return BlueStdResult( BLUE_STD_RESULT_RUNTIME_ERROR, "Failed to map referenced texture" ); |
| 34 | } |
| 35 | |
| 36 | ImageIO::HostBitmap bmp; |
| 37 | bmp.CreateFromBitmapDimensions( m_texture.GetDesc() ); |
| 38 | |
| 39 | uint32_t dstPitch = bmp.GetPitch(); |
| 40 | auto srcRow = static_cast<const uint8_t*>( src ); |
| 41 | auto dstRow = reinterpret_cast<uint8_t*>( bmp.GetRawData() ); |
| 42 | auto rowSize = std::min( dstPitch, srcPitch ); |
| 43 | for( uint32_t k = 0; k < std::max( m_texture.GetDesc().GetDepth(), 1u ); ++k ) |
| 44 | { |
| 45 | for( uint32_t j = 0; j < m_texture.GetDesc().GetHeight(); ++j ) |
| 46 | { |
| 47 | memcpy( dstRow, srcRow, rowSize ); |
| 48 | srcRow += srcPitch; |
| 49 | dstRow += dstPitch; |
| 50 | } |
| 51 | } |
| 52 | m_texture.UnmapForReading( renderContext ); |
| 53 | |
| 54 | auto path = BePaths->ResolvePathForWritingW( resPath ); |
| 55 | Be::Clsid resFileClsid( "blue", "ResFile" ); |
| 56 | IResFilePtr stream( resFileClsid ); |
| 57 | if( !( stream->FileExistsW( path.c_str() ) ? stream->OpenW( path.c_str(), false ) : stream->CreateW( path.c_str() ) ) ) |
| 58 | { |
| 59 | return BlueStdResult( BLUE_STD_RESULT_OS_ERROR, "Could not open the destination file" ); |
| 60 | } |
| 61 | |
| 62 | return ImageIO::SaveImage( path.c_str(), bmp, *stream ); |
| 63 | } |
| 64 | |
| 65 | uint32_t Tr2TextureReference::GetWidth() const |
| 66 | { |
nothing calls this directly
no test coverage detected