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.
This commit is contained in:
98
Misaki.HighPerformance.Image/ImageResult.cs
Normal file
98
Misaki.HighPerformance.Image/ImageResult.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using Misaki.HighPerformance.Image.Runtime;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Image;
|
||||
|
||||
public unsafe class ImageResult : IDisposable
|
||||
{
|
||||
private byte* _buffer;
|
||||
|
||||
public uint Width
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public uint 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 void SetData(byte* data)
|
||||
{
|
||||
CRuntime.free(_buffer);
|
||||
_buffer = data;
|
||||
}
|
||||
|
||||
internal static unsafe ImageResult FromResult(byte* result, uint width, uint height, ColorComponents comp,
|
||||
ColorComponents req_comp)
|
||||
{
|
||||
if (result == null)
|
||||
throw new InvalidOperationException(StbImage.stbi__g_failure_reason);
|
||||
|
||||
var image = new ImageResult
|
||||
{
|
||||
Width = width,
|
||||
Height = height,
|
||||
SourceComponent = comp,
|
||||
Component = req_comp == ColorComponents.Default ? comp : req_comp
|
||||
};
|
||||
|
||||
image._buffer = result;
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
public static unsafe ImageResult FromStream(Stream stream,
|
||||
ColorComponents requiredComponents = ColorComponents.Default)
|
||||
{
|
||||
int x, y, comp;
|
||||
|
||||
var context = new StbImage.stbi__context(stream);
|
||||
var result = StbImage.stbi__load_and_postprocess_8bit(context, &x, &y, &comp, (int)requiredComponents);
|
||||
|
||||
return FromResult(result, (uint)x, (uint)y, (ColorComponents)comp, requiredComponents);
|
||||
}
|
||||
|
||||
public static ImageResult FromMemory(byte[] data, ColorComponents requiredComponents = ColorComponents.Default)
|
||||
{
|
||||
using var stream = new MemoryStream(data);
|
||||
return FromStream(stream, requiredComponents);
|
||||
}
|
||||
|
||||
public static IEnumerable<AnimatedFrameResult> AnimatedGifFramesFromStream(Stream stream, ColorComponents requiredComponents = ColorComponents.Default)
|
||||
{
|
||||
return new AnimatedGifEnumerable(stream, requiredComponents);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public byte* GetUnsafePtr()
|
||||
{
|
||||
return _buffer;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_buffer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CRuntime.free(_buffer);
|
||||
_buffer = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user