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

Method GetPixelColor

trinity/Resources/Tr2ImageRes.cpp:92–136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

90}
91
92Color Tr2ImageRes::GetPixelColor( int x, int y ) const
93{
94 if( !m_bitmap.IsValid() )
95 {
96 return Color( 0.0f, 0.0f, 0.0f, 0.0f );
97 }
98
99 // TODO: Support different formats
100 Tr2RenderContextEnum::PixelFormat format = m_bitmap.GetFormat();
101 if( format != Tr2RenderContextEnum::PIXEL_FORMAT_B8G8R8A8_UNORM &&
102 format != Tr2RenderContextEnum::PIXEL_FORMAT_B8G8R8X8_UNORM )
103 {
104 CCP_LOGERR( "Tr2ImageRes::GetPixelColor currently only supports PIXEL_FORMAT_B8G8R8A8_UNORM or PIXEL_FORMAT_B8G8R8X8_UNORM" );
105 return Color( 0.0f, 0.0f, 0.0f, 0.0f );
106 }
107
108 if( !m_bitmap.GetRawData() )
109 {
110 if( format == Tr2RenderContextEnum::PIXEL_FORMAT_B8G8R8A8_UNORM )
111 {
112 return Color( 0.0f, 0.0f, 0.0f, 0.0f );
113 }
114 else
115 {
116 return Color( 0.0f, 0.0f, 0.0f, 1.0f );
117 }
118 }
119
120 if( ( x < 0 ) || ( y < 0 ) || ( x >= int( m_bitmap.GetWidth() ) ) || ( y >= int( m_bitmap.GetHeight() ) ) )
121 {
122 return Color( 0.0f, 0.0f, 0.0f, 0.0f );
123 }
124
125 const unsigned char* p = reinterpret_cast<const unsigned char*>( m_bitmap.GetRawData() );
126
127 const int bytesPerPixel = 4; // only works for PIXEL_FORMAT_B8G8R8A8_UNORM, etc.
128 p += y * m_bitmap.GetWidth() * bytesPerPixel + x * bytesPerPixel;
129
130 Color color( *reinterpret_cast<const uint32_t*>( p ) );
131 if( format == Tr2RenderContextEnum::PIXEL_FORMAT_B8G8R8X8_UNORM )
132 {
133 color.a = 1.0f;
134 }
135 return color;
136}
137
138const ImageIO::HostBitmap& Tr2ImageRes::GetBitmap() const
139{

Callers

nothing calls this directly

Calls 4

IsValidMethod · 0.45
GetFormatMethod · 0.45
GetWidthMethod · 0.45
GetHeightMethod · 0.45

Tested by

no test coverage detected