MCPcopy Create free account
hub / github.com/carbonengine/trinity / CreateFromHeightData

Method CreateFromHeightData

trinity/Tr2HostBitmap.cpp:836–891  ·  view source on GitHub ↗

-------------------------------------------------------------------------------------- Description: Creates a height map by scaling up source data to fill the bitmap with bicubic interpolation. Imitates the result of contour layer from chartdirector used by planetary interaction. Arguments: data - source data, values from 0 to 1 width - width of the source data height - height of the source data R

Source from the content-addressed store, hash-verified

834// false Otherwise
835// --------------------------------------------------------------------------------------
836bool Tr2HostBitmap::CreateFromHeightData( const std::vector<float>& data, int32_t width, int32_t height )
837{
838 float scaleX = float( width - 1 ) / float( m_width );
839 float scaleY = float( height - 1 ) / float( m_height );
840
841 int32_t yIndex[4];
842 float yValues[4];
843
844 int components = GetBytesPerPixel( m_format );
845 if( !IsValid() || !( components == 4 || components == 1 ) || IsCompressed() )
846 {
847 return false;
848 }
849
850 for( uint32_t y = 0; y < m_height; y++ )
851 {
852 float sy = y * scaleY;
853 int32_t dataY = int32_t( sy );
854 sy = sy - dataY;
855
856 for( int32_t yi = 0; yi < 4; yi++ )
857 {
858 // Clamp the y axis
859 yIndex[yi] = ClampInt( dataY + yi - 1, 0, width - 1 ) * width;
860 }
861
862 for( uint32_t x = 0; x < m_width; x++ )
863 {
864 float sx = x * scaleX;
865 int32_t dataX = int32_t( sx );
866 sx = sx - dataX;
867
868 for( int32_t i = 0; i < 4; i++ )
869 {
870 int32_t index = dataX + i - 1;
871 // Wrap the x axis
872 int32_t xIdx = index < 0 ? width - ( ( -index ) % width ) : index % width;
873 yValues[i] = CubicInterpolate( data[yIndex[0] + xIdx], data[yIndex[1] + xIdx], data[yIndex[2] + xIdx], data[yIndex[3] + xIdx], sy );
874 }
875 uint8_t value = uint8_t( TriClamp( CubicInterpolate( yValues[0], yValues[1], yValues[2], yValues[3], sx ), 0.f, 1.f ) * 255 );
876
877 if( components == 4 )
878 {
879 uint32_t d = ( value << 24 ) + ( value << 16 ) + ( value << 8 ) + value;
880 char* dest = m_data.get() + y * m_width * 4 + x * 4;
881 memcpy( dest, &d, 4 );
882 }
883 else
884 {
885 char* dest = m_data.get() + y * m_width + x;
886 *dest = value;
887 }
888 }
889 }
890 return true;
891}
892#endif

Callers

nothing calls this directly

Calls 5

ClampIntFunction · 0.85
CubicInterpolateFunction · 0.85
TriClampFunction · 0.85
getMethod · 0.80
IsValidFunction · 0.70

Tested by

no test coverage detected