solve 'edge' case and pass cancellation token
This commit is contained in:
parent
0472bfe58e
commit
d3dfdd6a74
15 changed files with 208 additions and 551 deletions
|
|
@ -20,25 +20,20 @@ public class Configuration
|
|||
|
||||
public int TileCount => Columns * Rows;
|
||||
|
||||
public required int ImageCacheCapacity { get; init; }
|
||||
public required int IntegralCacheCapacity { get; init; }
|
||||
|
||||
public static Configuration Default
|
||||
{
|
||||
get
|
||||
{
|
||||
var assetPath = Environment.GetEnvironmentVariable("ASSET_PATH_RO")!;
|
||||
var assetPath = Environment.GetEnvironmentVariable("ASSET_PATH_RO");
|
||||
var cachePath = Path.Combine(Path.GetTempPath(), "d42df2a2-60ac-4dc3-a6b9-d4c04f2e08e6");
|
||||
return new Configuration
|
||||
{
|
||||
AssetPath = assetPath,
|
||||
AssetPath = assetPath!,
|
||||
CachePath = cachePath,
|
||||
Columns = 55,
|
||||
Rows = 31,
|
||||
Width = 720,
|
||||
Height = 720,
|
||||
ImageCacheCapacity = 5,
|
||||
IntegralCacheCapacity = 10,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,33 +39,21 @@ public sealed class DangerousImageCreator : IDisposable
|
|||
|
||||
~DangerousImageCreator() => Dispose();
|
||||
|
||||
public async Task WriteToPipe2(PipeWriter outputPipe, float scale, CancellationToken cancellationToken = default)
|
||||
private Task Create(float scale, Action<IBuffer<byte>> writeRowCallback, CancellationToken cancellationToken)
|
||||
{
|
||||
var scaleFactor = MathF.ReciprocalEstimate(scale);
|
||||
var targetWidth = (int)(Width / scaleFactor);
|
||||
var targetHeight = (int)(Height / scaleFactor);
|
||||
if (targetHeight == 0 || targetWidth == 0)
|
||||
return;
|
||||
|
||||
var encoder = new PngPipeEncoder(outputPipe, targetWidth, targetHeight);
|
||||
encoder.WriteHeader();
|
||||
return Task.CompletedTask;
|
||||
|
||||
using var xLookup = Utils.BoundsMatrix(scaleFactor, targetWidth, FullWidth, OffsetX);
|
||||
using var yLookup = Utils.BoundsMatrix(scaleFactor, targetHeight, FullHeight, OffsetY);
|
||||
|
||||
using var yStartMap = MemoryAllocator.Allocate<Int32Pixel>(targetWidth + 1);
|
||||
using var yEndMap = MemoryAllocator.Allocate<Int32Pixel>(targetWidth + 1);
|
||||
|
||||
var outputBufferSize = targetWidth * Unsafe.SizeOf<Rgb24>();
|
||||
|
||||
var xLookup = Utils.DoubleBoundsMatrix(scaleFactor, targetWidth, FullWidth, OffsetX);
|
||||
var yLookup = Utils.DoubleBoundsMatrix(scaleFactor, targetHeight, FullHeight, OffsetY);
|
||||
|
||||
using var xLookup0 = xLookup.Item1;
|
||||
using var xLookup1 = xLookup.Item2;
|
||||
using var yLookup0 = yLookup.Item1;
|
||||
using var yLookup1 = yLookup.Item2;
|
||||
|
||||
using var yStartMap0 = MemoryAllocator.Allocate<Int32Pixel>(targetWidth);
|
||||
using var yStartMap1 = MemoryAllocator.Allocate<Int32Pixel>(targetWidth);
|
||||
using var yEndMap0 = MemoryAllocator.Allocate<Int32Pixel>(targetWidth);
|
||||
using var yEndMap1 = MemoryAllocator.Allocate<Int32Pixel>(targetWidth);
|
||||
|
||||
// var yStart = OffsetY;
|
||||
|
||||
// Use pixel referencing to eliminate type casting
|
||||
var pxInt32 = Int32Pixel.Zero;
|
||||
|
|
@ -73,42 +61,38 @@ public sealed class DangerousImageCreator : IDisposable
|
|||
ref var rChannel = ref Unsafe.As<Int32Pixel, byte>(ref px);
|
||||
ref var gChannel = ref Unsafe.Add(ref rChannel, 4);
|
||||
ref var bChannel = ref Unsafe.Add(ref rChannel, 8);
|
||||
|
||||
var taskQueue = TaskHelper.SynchronizedTaskFactory.StartNew(() => { }, cancellationToken);
|
||||
|
||||
var outputTaskQueue = TaskHelper.SynchronizedTaskFactory.StartNew(() => { }, cancellationToken);
|
||||
for (var y = 0; y < targetHeight; y++)
|
||||
for (var y = 1; y <= targetHeight; y++)
|
||||
{
|
||||
var yStart = yLookup0[y];
|
||||
var yEnd = yLookup1[y];
|
||||
var yStart = yLookup[y - 1];
|
||||
var yEnd = yLookup[y];
|
||||
|
||||
var (localRow0, localOffsetY0) = int.DivRem(yStart, TileHeight);
|
||||
MapRow(localRow0, localOffsetY0, xLookup0, targetWidth, yStartMap0);
|
||||
MapRow(localRow0, localOffsetY0, xLookup1, targetWidth, yStartMap1);
|
||||
MapRow(localRow0, localOffsetY0, xLookup, targetWidth + 1, yStartMap);
|
||||
|
||||
var (localRow1, localOffsetY1) = int.DivRem(yEnd, TileHeight);
|
||||
MapRow(localRow1, localOffsetY1, xLookup0, targetWidth, yEndMap0);
|
||||
MapRow(localRow1, localOffsetY1, xLookup1, targetWidth, yEndMap1);
|
||||
MapRow(localRow1, localOffsetY1, xLookup, targetWidth + 1, yEndMap);
|
||||
|
||||
// Cross row
|
||||
if (localRow0 != localRow1)
|
||||
{
|
||||
MapRow(localRow0, BottomPixelIndex, xLookup0, targetWidth, yEndMap0, true);
|
||||
MapRow(localRow0, BottomPixelIndex, xLookup1, targetWidth, yEndMap1, true);
|
||||
}
|
||||
|
||||
// int xStart = OffsetX, x0 = 0;
|
||||
MapRow(localRow0, BottomPixelIndex, xLookup, targetWidth + 1, yEndMap, true);
|
||||
|
||||
var outputBuffer = MemoryAllocator.Allocate<byte>(outputBufferSize);
|
||||
ref var outputChannel = ref outputBuffer.Span[0];
|
||||
var boxHeight = Math.Max(1, yEnd - yStart);
|
||||
for (int x = 0; x < targetWidth; x++)
|
||||
var boxHeight = yEnd - yStart;
|
||||
|
||||
for (int x1 = 1, x0 = 0; x1 <= targetWidth; x0 = x1++)
|
||||
{
|
||||
var xStart = xLookup0[x];
|
||||
var xEnd = xLookup1[x];
|
||||
var xStart = xLookup[x1 - 1];
|
||||
var xEnd = xLookup[x1];
|
||||
|
||||
px = yEndMap1[x];
|
||||
px += yStartMap0[x];
|
||||
px -= yEndMap0[x];
|
||||
px -= yStartMap1[x];
|
||||
px /= Math.Max(1, xEnd - xStart) * boxHeight;
|
||||
px = yEndMap[x1];
|
||||
px += yStartMap[x0];
|
||||
px -= yEndMap[x0];
|
||||
px -= yStartMap[x1];
|
||||
px /= Math.Max(1, (xEnd - xStart) * boxHeight);
|
||||
|
||||
outputChannel = rChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
|
@ -118,22 +102,24 @@ public sealed class DangerousImageCreator : IDisposable
|
|||
|
||||
outputChannel = bChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
// xStart = xEnd;
|
||||
// x0 = x;
|
||||
}
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
outputBuffer.Dispose();
|
||||
return Task.FromCanceled(cancellationToken);
|
||||
}
|
||||
|
||||
outputTaskQueue = outputTaskQueue
|
||||
.ContinueWith(async _ =>
|
||||
{
|
||||
await encoder.WriteDataAsync(outputBuffer, cancellationToken: cancellationToken);
|
||||
}, cancellationToken);
|
||||
|
||||
yStart = yEnd;
|
||||
cancellationToken.Register(outputBuffer.Dispose);
|
||||
taskQueue = taskQueue
|
||||
.ContinueWith(
|
||||
_ => writeRowCallback.Invoke(outputBuffer),
|
||||
cancellationToken,
|
||||
TaskContinuationOptions.OnlyOnRanToCompletion,
|
||||
TaskScheduler.Current);
|
||||
}
|
||||
|
||||
await outputTaskQueue;
|
||||
await encoder.WriteEndOfFileAsync(cancellationToken);
|
||||
return taskQueue;
|
||||
}
|
||||
|
||||
public async Task WriteToPipe(PipeWriter outputPipe, float scale, CancellationToken cancellationToken = default)
|
||||
|
|
@ -147,181 +133,32 @@ public sealed class DangerousImageCreator : IDisposable
|
|||
var encoder = new PngPipeEncoder(outputPipe, targetWidth, targetHeight);
|
||||
encoder.WriteHeader();
|
||||
|
||||
Task outputTaskQueue;
|
||||
|
||||
var outputBufferSize = targetWidth * Unsafe.SizeOf<Rgb24>();
|
||||
|
||||
using var xLookup = Utils.BoundsMatrix(scaleFactor, targetWidth, FullWidth, OffsetX);
|
||||
using var yLookup = Utils.BoundsMatrix(scaleFactor, targetHeight, FullHeight, OffsetY);
|
||||
|
||||
using var yStartMap = MemoryAllocator.Allocate<Int32Pixel>(targetWidth + 1);
|
||||
using var yEndMap = MemoryAllocator.Allocate<Int32Pixel>(targetWidth + 1);
|
||||
// OffsetX-(int)float.Ceiling(scaleFactor)
|
||||
int yStart = OffsetY,
|
||||
yEnd = yLookup[0],
|
||||
xStart = OffsetX,
|
||||
x0 = 0;
|
||||
await Create(scale,
|
||||
output => encoder
|
||||
.WriteDataAsync(output, cancellationToken: cancellationToken)
|
||||
.Wait(cancellationToken),
|
||||
cancellationToken);
|
||||
|
||||
|
||||
|
||||
// Use pixel referencing to eliminate type casting
|
||||
var pxInt32 = Int32Pixel.Zero;
|
||||
ref var px = ref pxInt32;
|
||||
ref var rChannel = ref Unsafe.As<Int32Pixel, byte>(ref px);
|
||||
ref var gChannel = ref Unsafe.Add(ref rChannel, 4);
|
||||
ref var bChannel = ref Unsafe.Add(ref rChannel, 8);
|
||||
|
||||
// First row
|
||||
var (localRow0, localOffsetY0) = int.DivRem(yStart, TileHeight);
|
||||
var (localRow1, localOffsetY1) = int.DivRem(yEnd, TileHeight);
|
||||
{
|
||||
switch (localOffsetY0)
|
||||
{
|
||||
// Cross row tile, no need to handle if it's first row tile for now
|
||||
// the provided asset is bordered black anyway
|
||||
case 0 when TileOrigin.Row > 1:
|
||||
localOffsetY0 = BottomPixelIndex;
|
||||
localRow0--;
|
||||
break;
|
||||
case > 0:
|
||||
localOffsetY0--;
|
||||
break;
|
||||
}
|
||||
|
||||
MapRow(localRow0, localOffsetY0, xLookup, targetWidth, yStartMap);
|
||||
MapRow(localRow1, localOffsetY1, xLookup, targetWidth, yEndMap);
|
||||
|
||||
// Cross row
|
||||
if (localRow0 != localRow1)
|
||||
MapRow(localRow0, BottomPixelIndex, xLookup, targetWidth, yEndMap, true);
|
||||
await encoder.WriteEndOfFileAsync(cancellationToken);
|
||||
}
|
||||
|
||||
var outputBuffer = MemoryAllocator.Allocate<byte>(outputBufferSize);
|
||||
ref var outputChannel = ref outputBuffer.Span[0];
|
||||
var boxHeight = yEnd - yStart;
|
||||
|
||||
// Render first pixel row
|
||||
var xEnd = xLookup[0];
|
||||
px = yEndMap[0];
|
||||
px += yStartMap[^1];
|
||||
px -= yEndMap[^1];
|
||||
px -= yStartMap[0];
|
||||
px /= Math.Max(1, (xEnd - xStart) * boxHeight);
|
||||
|
||||
outputChannel = rChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
outputChannel = gChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
outputChannel = bChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
xStart = xEnd;
|
||||
|
||||
// Render entire pixel row
|
||||
for (int x1 = 1; x1 < targetWidth; x1++)
|
||||
{
|
||||
xEnd = xLookup[x1];
|
||||
|
||||
px = yEndMap[x1];
|
||||
px += yStartMap[x0];
|
||||
px -= yEndMap[x0];
|
||||
px -= yStartMap[x1];
|
||||
px /= Math.Max(1, (xEnd - xStart) * boxHeight);
|
||||
|
||||
outputChannel = rChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
outputChannel = gChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
outputChannel = bChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
xStart = xEnd;
|
||||
x0 = x1;
|
||||
}
|
||||
|
||||
outputTaskQueue = TaskHelper.SynchronizedTaskFactory.StartNew(async _ =>
|
||||
{
|
||||
await encoder.WriteDataAsync(outputBuffer, cancellationToken: cancellationToken);
|
||||
}, null, cancellationToken);
|
||||
|
||||
yStart = yEnd;
|
||||
}
|
||||
public async Task WriteToStream(Stream outputStream, float scale, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var scaleFactor = MathF.ReciprocalEstimate(scale);
|
||||
var targetWidth = (int)(Width / scaleFactor);
|
||||
var targetHeight = (int)(Height / scaleFactor);
|
||||
if (targetHeight == 0 || targetWidth == 0)
|
||||
return;
|
||||
|
||||
var encoder = new PngStreamEncoder(outputStream, targetWidth, targetHeight);
|
||||
await encoder.WriteHeader(cancellationToken);
|
||||
|
||||
await Create(scale,
|
||||
output => encoder
|
||||
.WriteDataAsync(output, cancellationToken: cancellationToken)
|
||||
.Wait(cancellationToken),
|
||||
cancellationToken);
|
||||
|
||||
for (var y = 1; y < targetHeight; y++)
|
||||
{
|
||||
yEnd = yLookup[y];
|
||||
|
||||
(localRow0, localOffsetY0) = int.DivRem(yStart, TileHeight);
|
||||
MapRow(localRow0, localOffsetY0, xLookup, targetWidth, yStartMap);
|
||||
|
||||
(localRow1, localOffsetY1) = int.DivRem(yEnd, TileHeight);
|
||||
MapRow(localRow1, localOffsetY1, xLookup, targetWidth, yEndMap);
|
||||
|
||||
// Cross row
|
||||
if (localRow0 != localRow1)
|
||||
MapRow(localRow0, BottomPixelIndex, xLookup, targetWidth, yEndMap, true);
|
||||
|
||||
xStart = OffsetX;
|
||||
x0 = 0;
|
||||
|
||||
var outputBuffer = MemoryAllocator.Allocate<byte>(outputBufferSize);
|
||||
ref var outputChannel = ref outputBuffer.Span[0];
|
||||
var boxHeight = yEnd - yStart;
|
||||
|
||||
var xEnd = xLookup[0];
|
||||
px = yEndMap[0];
|
||||
px += yStartMap[^1];
|
||||
px -= yEndMap[^1];
|
||||
px -= yStartMap[0];
|
||||
px /= Math.Max(1, (xEnd - xStart) * boxHeight);
|
||||
|
||||
outputChannel = rChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
outputChannel = gChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
outputChannel = bChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
xStart = xEnd;
|
||||
|
||||
for (int x1 = 1; x1 < targetWidth; x1++)
|
||||
{
|
||||
xEnd = xLookup[x1];
|
||||
|
||||
px = yEndMap[x1];
|
||||
px += yStartMap[x0];
|
||||
px -= yEndMap[x0];
|
||||
px -= yStartMap[x1];
|
||||
px /= Math.Max(1, (xEnd - xStart) * boxHeight);
|
||||
|
||||
outputChannel = rChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
outputChannel = gChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
outputChannel = bChannel;
|
||||
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
|
||||
|
||||
xStart = xEnd;
|
||||
x0 = x1;
|
||||
}
|
||||
|
||||
outputTaskQueue = outputTaskQueue
|
||||
.ContinueWith(async _ =>
|
||||
{
|
||||
await encoder.WriteDataAsync(outputBuffer, cancellationToken: cancellationToken);
|
||||
}, cancellationToken);
|
||||
|
||||
yStart = yEnd;
|
||||
}
|
||||
|
||||
await outputTaskQueue;
|
||||
await encoder.WriteEndOfFileAsync(cancellationToken);
|
||||
}
|
||||
|
||||
|
|
@ -333,33 +170,23 @@ public sealed class DangerousImageCreator : IDisposable
|
|||
IBuffer<Int32Pixel> destination,
|
||||
bool appendMode = false)
|
||||
{
|
||||
var currentTile = TileManager.TryGetAdjacent(TileOrigin, 0, rowOffset);
|
||||
if (currentTile == null)
|
||||
{
|
||||
if (appendMode) return;
|
||||
for (var i = 0; i < count; i++)
|
||||
destination[i] = Int32Pixel.Zero;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var sourceMap = boundsMatrix.Span[..count];
|
||||
var currentTile = TileManager.GetAdjacent(TileOrigin, 0, rowOffset);
|
||||
var xAdder = Int32Pixel.Zero;
|
||||
var xOffset = 0;
|
||||
var written = 0;
|
||||
var destinationSpan = destination.Span;
|
||||
var readBufferSpan = _mmfReadBuffer.Span;
|
||||
|
||||
var negative = sourceMap[0] - 1;
|
||||
var negativePixel = appendMode ? destinationSpan[^1] : Int32Pixel.Zero;
|
||||
if (negative >= 0)
|
||||
{
|
||||
negativePixel = readBufferSpan[negative];
|
||||
}
|
||||
else if(currentTile.Column > 1)
|
||||
{
|
||||
// Cross row tile, no need to handle if it's first column tile for now
|
||||
// the provided asset is bordered black anyway
|
||||
TileManager.GetAdjacent(currentTile, -1, 0)
|
||||
.Integral
|
||||
.Acquire(yOffset, readBufferSpan);
|
||||
negativePixel = readBufferSpan[RightmostPixelIndex];
|
||||
xAdder = readBufferSpan[RightmostPixelIndex];
|
||||
}
|
||||
|
||||
destinationSpan[^1] = negativePixel;
|
||||
|
||||
while (true)
|
||||
{
|
||||
currentTile.Integral.Acquire(yOffset, readBufferSpan);
|
||||
|
|
@ -383,13 +210,19 @@ public sealed class DangerousImageCreator : IDisposable
|
|||
written++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (written >= sourceMap.Length)
|
||||
break;
|
||||
|
||||
currentTile = TileManager.TryGetAdjacent(currentTile, 1, 0);
|
||||
if (currentTile == null)
|
||||
{
|
||||
destinationSpan[written] = destinationSpan[written - 1];
|
||||
break;
|
||||
}
|
||||
|
||||
xAdder += readBufferSpan[RightmostPixelIndex];
|
||||
xOffset += TileWidth;
|
||||
currentTile = TileManager.GetAdjacent(currentTile, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,160 +0,0 @@
|
|||
using System.Runtime.CompilerServices;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using StitchATon2.Infra;
|
||||
using StitchATon2.Infra.Buffers;
|
||||
using StitchATon2.Infra.Encoders;
|
||||
|
||||
namespace StitchATon2.Domain.ImageCreators;
|
||||
|
||||
public class ImageCreator : IDisposable
|
||||
{
|
||||
private readonly GridSection _section;
|
||||
|
||||
private int FullWidth => _section.TileManager.Configuration.FullWidth;
|
||||
private int FullHeight => _section.TileManager.Configuration.FullHeight;
|
||||
|
||||
private int OffsetX => _section.OffsetX;
|
||||
private int OffsetY => _section.OffsetY;
|
||||
|
||||
private int Width => _section.Width;
|
||||
private int Height => _section.Height;
|
||||
|
||||
private int TileWidth => _section.TileManager.Configuration.Width;
|
||||
private int TileHeight => _section.TileManager.Configuration.Height;
|
||||
private Tile TileOrigin => _section.Origin;
|
||||
|
||||
private int RightmostPixelIndex => _section.TileManager.Configuration.RightTileIndex;
|
||||
private int BottomPixelIndex => _section.TileManager.Configuration.BottomTileIndex;
|
||||
|
||||
private TileManager TileManager => _section.TileManager;
|
||||
|
||||
private readonly ArrayOwner<Int32Pixel> _mmfReadBuffer;
|
||||
|
||||
public ImageCreator(GridSection section)
|
||||
{
|
||||
_section = section;
|
||||
_mmfReadBuffer = MemoryAllocator.AllocateArray<Int32Pixel>(TileWidth);
|
||||
}
|
||||
|
||||
public async Task WriteToStream(Stream writableStream, float scale)
|
||||
{
|
||||
var scaleFactor = MathF.ReciprocalEstimate(scale);
|
||||
var targetWidth = (int)(Width / scaleFactor);
|
||||
var targetHeight = (int)(Height / scaleFactor);
|
||||
|
||||
var encoder = new PngStreamEncoder(writableStream, targetWidth, targetHeight);
|
||||
await encoder.WriteHeader();
|
||||
|
||||
var outputBufferSize = targetWidth * Unsafe.SizeOf<Rgb24>();
|
||||
using var outputBuffer = MemoryAllocator.AllocateManaged<byte>(outputBufferSize);
|
||||
|
||||
using var xLookup = Utils.BoundsMatrix(scaleFactor, targetWidth, FullWidth, OffsetX);
|
||||
using var yLookup = Utils.BoundsMatrix(scaleFactor, targetHeight, FullHeight, OffsetY);
|
||||
|
||||
using var yStartMap = MemoryAllocator.Allocate<Int32Pixel>(targetWidth);
|
||||
using var yEndMap = MemoryAllocator.Allocate<Int32Pixel>(targetWidth);
|
||||
|
||||
var yStart = OffsetY;
|
||||
Task? outputTask = null;
|
||||
for (var y = 0; y < targetHeight; y++)
|
||||
{
|
||||
var yEnd = yLookup[y];
|
||||
|
||||
var (localRow0, localOffsetY0) = int.DivRem(yStart, TileHeight);
|
||||
MapRow(localRow0, localOffsetY0, xLookup.Span[..targetWidth], yStartMap);
|
||||
|
||||
var (localRow1, localOffsetY1) = int.DivRem(yEnd, TileHeight);
|
||||
MapRow(localRow1, localOffsetY1, xLookup.Span[..targetWidth], yEndMap);
|
||||
|
||||
if (localRow0 != localRow1)
|
||||
{
|
||||
MapRowAppend(localRow0, BottomPixelIndex, xLookup.Span[..targetWidth], yEndMap);
|
||||
}
|
||||
|
||||
if(outputTask != null)
|
||||
await outputTask;
|
||||
|
||||
int xStart = OffsetX, x0 = 0;
|
||||
for (int x1 = 0, i = 0; x1 < targetWidth; x1++)
|
||||
{
|
||||
var xEnd = xLookup[x1];
|
||||
|
||||
var pixel = yEndMap[x1];
|
||||
pixel += yStartMap[x0];
|
||||
pixel -= yEndMap[x0];
|
||||
pixel -= yStartMap[x1];
|
||||
|
||||
pixel /= Math.Max(1, (xEnd - xStart) * (yEnd - yStart));
|
||||
outputBuffer.Memory.Span[i++] = (byte)pixel.R;
|
||||
outputBuffer.Memory.Span[i++] = (byte)pixel.G;
|
||||
outputBuffer.Memory.Span[i++] = (byte)pixel.B;
|
||||
|
||||
xStart = xEnd;
|
||||
x0 = x1;
|
||||
}
|
||||
|
||||
outputTask = encoder.WriteData(outputBuffer.Memory[..outputBufferSize]);
|
||||
yStart = yEnd;
|
||||
}
|
||||
|
||||
await encoder.WriteEndOfFile();
|
||||
}
|
||||
|
||||
private void MapRow(int rowOffset, int yOffset, Span<int> sourceMap, IBuffer<Int32Pixel> destination)
|
||||
{
|
||||
var currentTile = TileManager.GetAdjacent(TileOrigin, 0, rowOffset);
|
||||
var xAdder = Int32Pixel.Zero;
|
||||
var xOffset = 0;
|
||||
var written = 0;
|
||||
while (true)
|
||||
{
|
||||
currentTile.Integral.Acquire(yOffset, _mmfReadBuffer.Array);
|
||||
int localX;
|
||||
while (written < sourceMap.Length && (localX = sourceMap[written] - xOffset) < TileWidth)
|
||||
{
|
||||
destination.Span[written] = _mmfReadBuffer[localX];
|
||||
destination.Span[written] += xAdder;
|
||||
written++;
|
||||
}
|
||||
|
||||
if (written >= sourceMap.Length)
|
||||
break;
|
||||
|
||||
xAdder += _mmfReadBuffer[RightmostPixelIndex];
|
||||
xOffset += TileWidth;
|
||||
currentTile = TileManager.GetAdjacent(currentTile, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void MapRowAppend(int rowOffset, int yOffset, Span<int> sourceMap, IBuffer<Int32Pixel> destination)
|
||||
{
|
||||
var currentTile = TileManager.GetAdjacent(TileOrigin, 0, rowOffset);
|
||||
var xAdder = Int32Pixel.Zero;
|
||||
var xOffset = 0;
|
||||
var written = 0;
|
||||
while (true)
|
||||
{
|
||||
currentTile.Integral.Acquire(yOffset, _mmfReadBuffer.Array);
|
||||
int localX;
|
||||
while (written < sourceMap.Length && (localX = sourceMap[written] - xOffset) < TileWidth)
|
||||
{
|
||||
destination.Span[written] += _mmfReadBuffer[localX];
|
||||
destination.Span[written] += xAdder;
|
||||
written++;
|
||||
}
|
||||
|
||||
if (written >= sourceMap.Length)
|
||||
break;
|
||||
|
||||
xAdder += _mmfReadBuffer[RightmostPixelIndex];
|
||||
xOffset += TileWidth;
|
||||
currentTile = TileManager.GetAdjacent(currentTile, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_mmfReadBuffer.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ public sealed class TileManager
|
|||
private Tile CreateTile(int id)
|
||||
{
|
||||
var (row, column) = int.DivRem(id, Configuration.Columns);
|
||||
var coordinate = $"{Utils.GetSBSNotationRow(++row)}{++column}";
|
||||
var coordinate = $"{Utils.GetSbsNotationRow(++row)}{++column}";
|
||||
return new Tile
|
||||
{
|
||||
Id = id,
|
||||
|
|
@ -51,7 +51,7 @@ public sealed class TileManager
|
|||
|
||||
public Tile GetTile(string coordinate)
|
||||
{
|
||||
var (column, row) = Utils.GetSBSCoordinate(coordinate);
|
||||
var (column, row) = Utils.GetSbsNotationCoordinate(coordinate);
|
||||
return GetTile(column, row);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ namespace StitchATon2.Domain;
|
|||
public static class Utils
|
||||
{
|
||||
[Pure]
|
||||
public static string GetSBSNotationRow(int row)
|
||||
public static string GetSbsNotationRow(int row)
|
||||
=> row <= 26
|
||||
? new string([(char)(row + 'A' - 1)])
|
||||
: new string(['A', (char)(row + 'A' - 27)]);
|
||||
|
|
@ -21,7 +21,7 @@ public static class Utils
|
|||
}
|
||||
|
||||
[Pure]
|
||||
public static (int Column, int Row) GetSBSCoordinate(string coordinate)
|
||||
public static (int Column, int Row) GetSbsNotationCoordinate(string coordinate)
|
||||
{
|
||||
var column = coordinate[^1] - '0';
|
||||
if(char.IsDigit(coordinate[^2]))
|
||||
|
|
@ -43,18 +43,18 @@ public static class Utils
|
|||
/// <param name="offset">The offset to apply before clamping.</param>
|
||||
public static IBuffer<int> BoundsMatrix(float scaleFactor, int length, int max, int offset)
|
||||
{
|
||||
var vectorSize = DivCeil(length, Vector<float>.Count);
|
||||
var vectorSize = DivCeil(length + 1, Vector<float>.Count);
|
||||
using var buffer = MemoryAllocator.Allocate<Vector<float>>(vectorSize);
|
||||
|
||||
var span = buffer.Span;
|
||||
var vectorMin = Vector<int>.Zero;
|
||||
var vectorOffset = new Vector<int>(offset - 1);
|
||||
var vectorOffset = new Vector<int>(offset);
|
||||
var vectorMax = new Vector<int>(max - 1);
|
||||
var vectorScale = new Vector<float>(scaleFactor);
|
||||
|
||||
var vectorSequence = Vector.CreateSequence(0f, 1f);
|
||||
|
||||
var seq = 0f;
|
||||
var seq = -1f;
|
||||
for (var i = 0; i < vectorSize; i++, seq += Vector<float>.Count)
|
||||
{
|
||||
var sequence = new Vector<float>(seq) + vectorSequence;
|
||||
|
|
@ -70,54 +70,7 @@ public static class Utils
|
|||
resultSpan[i] = Vector.Add(resultSpan[i], vectorOffset);
|
||||
resultSpan[i] = Vector.ClampNative(resultSpan[i], vectorMin, vectorMax);
|
||||
}
|
||||
|
||||
var negative = float.Ceiling(scaleFactor);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static (IBuffer<int>, IBuffer<int>) DoubleBoundsMatrix(float scaleFactor, int length, int max, int offset)
|
||||
{
|
||||
var vectorSize = DivCeil(length, Vector<float>.Count);
|
||||
using var startBuffer = MemoryAllocator.Allocate<Vector<float>>(vectorSize);
|
||||
using var endBuffer = MemoryAllocator.Allocate<Vector<float>>(vectorSize);
|
||||
|
||||
var startSpan = startBuffer.Span;
|
||||
var endSpan = endBuffer.Span;
|
||||
|
||||
var vectorMin = Vector<int>.Zero;
|
||||
var vectorOne = Vector<int>.One;
|
||||
var vectorMax = Vector.Create(max);
|
||||
var vectorScale = Vector.Create(scaleFactor);
|
||||
var vectorOffset = new Vector<int>(offset - 1);
|
||||
|
||||
for (int i = 0, seq = 0; i < vectorSize; i++, seq += Vector<float>.Count)
|
||||
{
|
||||
startSpan[i] = Vector.CreateSequence(seq, 1f);
|
||||
startSpan[i] = Vector.Multiply(startSpan[i], vectorScale);
|
||||
endSpan[i] = Vector.Add(vectorScale, startSpan[i]);
|
||||
endSpan[i] = Vector.Ceiling(endSpan[i]);
|
||||
}
|
||||
|
||||
var resultStart = MemoryAllocator.Allocate<int>(vectorSize * Vector<int>.Count);
|
||||
var resultEnd = MemoryAllocator.Allocate<int>(vectorSize * Vector<int>.Count);
|
||||
|
||||
var resultStartSpan = MemoryMarshal.Cast<int, Vector<int>>(resultStart.Span);
|
||||
var resultEndSpan = MemoryMarshal.Cast<int, Vector<int>>(resultEnd.Span);
|
||||
|
||||
for (var i = 0; i < vectorSize; i++)
|
||||
{
|
||||
resultStartSpan[i] = Vector.ConvertToInt32(startSpan[i]);
|
||||
resultStartSpan[i] = Vector.Subtract(resultStartSpan[i], vectorOne);
|
||||
resultStartSpan[i] = Vector.Add(resultStartSpan[i], vectorOffset);
|
||||
resultStartSpan[i] = Vector.Clamp(resultStartSpan[i], vectorMin, vectorMax);
|
||||
|
||||
resultEndSpan[i] = Vector.ConvertToInt32(endSpan[i]);
|
||||
resultEndSpan[i] = Vector.Subtract(resultEndSpan[i], vectorOne);
|
||||
resultEndSpan[i] = Vector.Add(resultEndSpan[i], vectorOffset);
|
||||
resultEndSpan[i] = Vector.Clamp(resultEndSpan[i], vectorMin, vectorMax);
|
||||
}
|
||||
|
||||
return (resultStart, resultEnd);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue