Dispose buffer

This commit is contained in:
dennisarfan 2025-07-30 08:35:03 +07:00
parent 969376cf97
commit eb97cfb57c

View file

@ -1,4 +1,3 @@
using System.Buffers;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.PixelFormats;
using StitchATon2.Infra;
@ -7,7 +6,7 @@ using StitchATon2.Infra.Encoders;
namespace StitchATon2.Domain.ImageCreators;
public class ImageCreator
public class ImageCreator : IDisposable
{
private readonly GridSection _section;
@ -29,12 +28,12 @@ public class ImageCreator
private TileManager TileManager => _section.TileManager;
private readonly Int32Pixel[] _mmfReadBuffer;
private readonly ArrayOwner<Int32Pixel> _mmfReadBuffer;
public ImageCreator(GridSection section)
{
_section = section;
_mmfReadBuffer = ArrayPool<Int32Pixel>.Shared.Rent(TileWidth);
_mmfReadBuffer = MemoryAllocator.AllocateArray<Int32Pixel>(TileWidth);
}
public async Task WriteToStream(Stream writableStream, float scale)
@ -109,7 +108,7 @@ public class ImageCreator
var written = 0;
while (true)
{
currentTile.Integral.Acquire(yOffset, _mmfReadBuffer);
currentTile.Integral.Acquire(yOffset, _mmfReadBuffer.Array);
int localX;
while (written < sourceMap.Length && (localX = sourceMap[written] - xOffset) < TileWidth)
{
@ -135,7 +134,7 @@ public class ImageCreator
var written = 0;
while (true)
{
currentTile.Integral.Acquire(yOffset, _mmfReadBuffer);
currentTile.Integral.Acquire(yOffset, _mmfReadBuffer.Array);
int localX;
while (written < sourceMap.Length && (localX = sourceMap[written] - xOffset) < TileWidth)
{
@ -152,4 +151,10 @@ public class ImageCreator
currentTile = TileManager.GetAdjacent(currentTile, 1, 0);
}
}
public void Dispose()
{
_mmfReadBuffer.Dispose();
GC.SuppressFinalize(this);
}
}