Files
sbox-public/engine/Sandbox.Engine/Utility/FloatBitmap.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

76 lines
1.4 KiB
C#

using NativeEngine;
namespace Sandbox.Utility;
public class FloatBitmap : IDisposable
{
FloatBitMap_t native;
internal FloatBitmap( FloatBitMap_t fbm )
{
native = fbm;
}
internal unsafe FloatBitmap( int width, int height, ImageFormat format, IntPtr data, int dataSize )
{
native = FloatBitMap_t.Create( width, height );
if ( data != default && dataSize > 0 )
{
native.LoadFromBuffer( data, dataSize, format, FBMGammaType_t.FBM_GAMMA_LINEAR );
}
}
~FloatBitmap()
{
Dispose();
}
public void Dispose()
{
if ( native.IsNull )
return;
GC.SuppressFinalize( this );
native.Shutdown();
native.Delete();
native = IntPtr.Zero;
}
public int Width => native.Width();
public int Height => native.Height();
public int Depth => native.Depth();
public void Resize( int width, int height, bool clamp = true )
{
if ( native.IsNull )
return;
native.Resize2D( width, height, clamp );
}
public unsafe byte[] EncodeTo( ImageFormat format )
{
if ( native.IsNull )
return null;
var dataSize = ImageLoader.GetMemRequired( Width, Height, 1, 1, format );
if ( dataSize <= 0 )
return null;
var data = new byte[dataSize];
fixed ( byte* pData = data )
{
// uint FLOAT_BITMAP_PREFER_RUNTIME_FRIENDLY_DXT_ENCODER = 1;
if ( !native.WriteToBuffer( (IntPtr)pData, data.Length, format, false, false, 0 ) )
{
return default;
}
}
return data;
}
}