solve edge case

This commit is contained in:
dennisarfan 2025-08-01 09:51:39 +07:00
parent 741d34a5e0
commit 0472bfe58e
15 changed files with 685 additions and 47 deletions

View file

@ -47,5 +47,10 @@ public class GridSection
(var rowOffset, OffsetY) = Math.DivRem(y0, config.Height);
Origin = tileManager.GetTile(col0 + columnOffset, row0 + rowOffset);
Console.Write($"Origin: {Origin.Coordinate} ({Origin.Column}, {Origin.Row}) ");
Console.Write($"Tile offset: [{columnOffset} {rowOffset}] ");
Console.Write($"Pixel offset: [{OffsetX} {OffsetY}] ");
Console.Write($"Size: [{Width}x{Height}]");
Console.WriteLine();
}
}

View file

@ -39,25 +39,35 @@ public sealed class DangerousImageCreator : IDisposable
~DangerousImageCreator() => Dispose();
public async Task WriteToPipe(PipeWriter outputPipe, float scale, CancellationToken cancellationToken = default)
public async Task WriteToPipe2(PipeWriter outputPipe, 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 PngPipeEncoder(outputPipe, targetWidth, targetHeight);
encoder.WriteHeader();
var outputBufferSize = targetWidth * Unsafe.SizeOf<Rgb24>();
using var xLookup = Utils.BoundsMatrix(scaleFactor, targetWidth, FullWidth, OffsetX);
using var yLookup = Utils.BoundsMatrix(scaleFactor, targetHeight, FullHeight, OffsetY);
var xLookup = Utils.DoubleBoundsMatrix(scaleFactor, targetWidth, FullWidth, OffsetX);
var yLookup = Utils.DoubleBoundsMatrix(scaleFactor, targetHeight, FullHeight, OffsetY);
using var yStartMap = MemoryAllocator.Allocate<Int32Pixel>(targetWidth);
using var yEndMap = MemoryAllocator.Allocate<Int32Pixel>(targetWidth);
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;
// var yStart = OffsetY;
// 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);
@ -67,27 +77,151 @@ public sealed class DangerousImageCreator : IDisposable
var outputTaskQueue = TaskHelper.SynchronizedTaskFactory.StartNew(() => { }, cancellationToken);
for (var y = 0; y < targetHeight; y++)
{
var yEnd = yLookup[y];
var yStart = yLookup0[y];
var yEnd = yLookup1[y];
var (localRow0, localOffsetY0) = int.DivRem(yStart, TileHeight);
MapRow(localRow0, localOffsetY0, xLookup, targetWidth, yStartMap);
MapRow(localRow0, localOffsetY0, xLookup0, targetWidth, yStartMap0);
MapRow(localRow0, localOffsetY0, xLookup1, targetWidth, yStartMap1);
var (localRow1, localOffsetY1) = int.DivRem(yEnd, TileHeight);
MapRow(localRow1, localOffsetY1, xLookup, targetWidth, yEndMap);
MapRow(localRow1, localOffsetY1, xLookup0, targetWidth, yEndMap0);
MapRow(localRow1, localOffsetY1, xLookup1, targetWidth, yEndMap1);
if (localRow0 != localRow1)
{
MapRow(localRow0, BottomPixelIndex, xLookup, targetWidth, yEndMap, true);
MapRow(localRow0, BottomPixelIndex, xLookup0, targetWidth, yEndMap0, true);
MapRow(localRow0, BottomPixelIndex, xLookup1, targetWidth, yEndMap1, true);
}
int xStart = OffsetX, x0 = 0;
// int xStart = OffsetX, x0 = 0;
var outputBuffer = MemoryAllocator.Allocate<byte>(outputBufferSize);
ref var outputChannel = ref outputBuffer.Span[0];
var boxHeight = yEnd - yStart;
for (int x1 = 0; x1 < targetWidth; x1++)
var boxHeight = Math.Max(1, yEnd - yStart);
for (int x = 0; x < targetWidth; x++)
{
var xEnd = xLookup[x1];
var xStart = xLookup0[x];
var xEnd = xLookup1[x];
px = yEndMap1[x];
px += yStartMap0[x];
px -= yEndMap0[x];
px -= yStartMap1[x];
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 = x;
}
outputTaskQueue = outputTaskQueue
.ContinueWith(async _ =>
{
await encoder.WriteDataAsync(outputBuffer, cancellationToken: cancellationToken);
}, cancellationToken);
yStart = yEnd;
}
await outputTaskQueue;
await encoder.WriteEndOfFileAsync(cancellationToken);
}
public async Task WriteToPipe(PipeWriter outputPipe, 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 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;
// 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);
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];
@ -96,13 +230,83 @@ public sealed class DangerousImageCreator : IDisposable
px /= Math.Max(1, (xEnd - xStart) * boxHeight);
outputChannel = rChannel;
outputChannel = ref Unsafe.Add(ref outputChannel, 1);
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
outputChannel = gChannel;
outputChannel = ref Unsafe.Add(ref outputChannel, 1);
outputChannel = ref Unsafe.AddByteOffset(ref outputChannel, 1);
outputChannel = bChannel;
outputChannel = ref Unsafe.Add(ref outputChannel, 1);
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;
}
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;
@ -136,10 +340,31 @@ public sealed class DangerousImageCreator : IDisposable
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);
int localX;
if (appendMode)
{
while (written < sourceMap.Length && (localX = sourceMap[written] - xOffset) < TileWidth)

View file

@ -5,29 +5,26 @@ using StitchATon2.Infra.Buffers;
namespace StitchATon2.Domain;
public sealed class TileManager : IDisposable
public sealed class TileManager
{
private readonly IMemoryOwner<Tile> _tiles;
private readonly Tile[] _tiles;
public Configuration Configuration { get; }
public TileManager(Configuration config)
{
Configuration = config;
_tiles = MemoryAllocator.AllocateManaged<Tile>(config.TileCount);
var tilesSpan = _tiles.Memory.Span;
_tiles = new Tile[Configuration.TileCount];
for (var id = 0; id < config.TileCount; id++)
tilesSpan[id] = CreateTile(id);
_tiles[id] = CreateTile(id);
Console.WriteLine("Tile manager created");
}
~TileManager() => Dispose();
private Tile CreateTile(int id)
{
var (row, column) = int.DivRem(id, Configuration.Columns);
var coordinate = $"{Utils.GetSBSNotation(++row)}{++column}";
var coordinate = $"{Utils.GetSBSNotationRow(++row)}{++column}";
return new Tile
{
Id = id,
@ -47,7 +44,7 @@ public sealed class TileManager : IDisposable
private int GetId(int column, int row) => column - 1 + (row - 1) * Configuration.Columns;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Tile GetTile(int id) => _tiles.Memory.Span[id];
public Tile GetTile(int id) => _tiles[id];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Tile GetTile(int column, int row) => GetTile(GetId(column, row));
@ -99,10 +96,4 @@ public sealed class TileManager : IDisposable
cropY,
cropWidth,
cropHeight);
public void Dispose()
{
_tiles.Dispose();
GC.SuppressFinalize(this);
}
}

View file

@ -9,7 +9,7 @@ namespace StitchATon2.Domain;
public static class Utils
{
[Pure]
public static string GetSBSNotation(int row)
public static string GetSBSNotationRow(int row)
=> row <= 26
? new string([(char)(row + 'A' - 1)])
: new string(['A', (char)(row + 'A' - 27)]);
@ -70,7 +70,54 @@ 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);
}
}