Files
Misaki.HighPerformance/Misaki.HighPerformance.Image/ImageResultFloat.cs
Misaki eeff3313b5 Add image processing and memory management features
Added new namespace `Misaki.HighPerformance.Image` for image processing, including classes for animated GIF handling and memory management.
Added `AnimatedFrameResult` class for individual frames in animated images.
Added `AnimatedGifEnumerator` class for enumerating frames in animated GIFs.
Added `ColorComponents` enum for different color formats.
Added `ImageInfo` struct for image dimensions and color components.
Added `CRuntime` class for low-level memory management functions.
Added `MemoryStats` class to track memory allocation statistics.
Added utility functions for creating multi-dimensional arrays.
Added new structures for fixed-size UTF-8 encoded strings.
Added benchmarking classes to test new memory management features.

Changed `StbImage.cs` to include new namespaces and functionality for image data manipulation.
Changed project files to target .NET 9.0 and enable new features.
Changed `Arena.cs` and `DynamicArena.cs` to use `nuint` for size parameters.
Changed `BitSet.cs` to enhance bit manipulation methods.
Changed `Program.cs` to run `FunctionPtrBenchmark` for performance testing.

Removed memory tracking code from `AllocationManager.cs`, including the `_allocated` dictionary and related logic.
Removed `Free` method from `IAllocator.cs` interface.
Removed `UNSAFE_COLLECTION_CHECK` preprocessor directive from the codebase.

Refactored various files to improve organization, moving from `Unsafe` to `LowLevel` namespace.
Refactored `MemoryUtilities` class to include new memory operation methods.
Refactored `UnsafeUtilities.cs` to support new collection structures.
2025-07-12 19:48:42 +09:00

89 lines
2.1 KiB
C#

using Misaki.HighPerformance.Image.Runtime;
using System;
using System.IO;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Image;
public unsafe class ImageResultFloat : IDisposable
{
private float* _buffer;
public int Width
{
get; init;
}
public int Height
{
get; init;
}
public ColorComponents SourceComponent
{
get; init;
}
public ColorComponents Component
{
get; init;
}
public Span<byte> Data => new(_buffer, (int)(Width * Height * (uint)Component));
internal static unsafe ImageResultFloat FromResult(float* result, int width, int height, ColorComponents comp,
ColorComponents req_comp)
{
if (result == null)
throw new InvalidOperationException(StbImage.stbi__g_failure_reason);
var image = new ImageResultFloat
{
Width = width,
Height = height,
SourceComponent = comp,
Component = req_comp == ColorComponents.Default ? comp : req_comp
};
image._buffer = result;
return image;
}
public static unsafe ImageResultFloat FromStream(Stream stream,
ColorComponents requiredComponents = ColorComponents.Default)
{
int x, y, comp;
var context = new StbImage.stbi__context(stream);
var result = StbImage.stbi__loadf_main(context, &x, &y, &comp, (int)requiredComponents);
return FromResult(result, x, y, (ColorComponents)comp, requiredComponents);
}
public static ImageResultFloat FromMemory(byte[] data,
ColorComponents requiredComponents = ColorComponents.Default)
{
using (var stream = new MemoryStream(data))
{
return FromStream(stream, requiredComponents);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float* GetUnsafePtr()
{
return _buffer;
}
public void Dispose()
{
if (_buffer == null)
{
return;
}
CRuntime.free(_buffer);
_buffer = null;
}
}