change using NetVips to reduce memory load

This commit is contained in:
Bahru 2025-07-31 23:34:36 +07:00
parent 661c4b955c
commit 49b6f3810d
5 changed files with 210 additions and 220 deletions

13
.idea/.idea.lilo-stitcher-console/.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/contentModel.xml
/projectSettingsUpdater.xml
/.idea.lilo-stitcher-console.iml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View file

@ -1,172 +1,136 @@
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using SixLabors.ImageSharp; using NetVips;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing; namespace lilo_stitcher_console;
using SixLabors.ImageSharp.Formats.Png;
public record GenerateRequest(
namespace LiloStitcher; string CanvasRect,
double[] CropOffset,
public record GenerateRequest( double[] CropSize,
string CanvasRect, double OutputScale
double[] CropOffset, );
double[] CropSize,
double OutputScale public readonly record struct PlateCoordinate(int Row, int Col)
); {
public static PlateCoordinate Parse(string token)
public readonly record struct PlateCoordinate(int Row, int Col) {
{ var rowPart = new string(token.TakeWhile(char.IsLetter).ToArray()).ToUpperInvariant();
public static PlateCoordinate Parse(string token) var colPart = new string(token.SkipWhile(char.IsLetter).ToArray());
{
var rowPart = new string(token.TakeWhile(char.IsLetter).ToArray()).ToUpperInvariant(); int row = 0;
var colPart = new string(token.SkipWhile(char.IsLetter).ToArray()); foreach (var character in rowPart)
row = row * 26 + (character - 'A' + 1);
int row = 0;
foreach (var character in rowPart) int.TryParse(colPart, out int col);
row = row * 26 + (character - 'A' + 1); return new PlateCoordinate(row, col);
}
int.TryParse(colPart, out int col); }
return new PlateCoordinate(row, col);
} public class TileCache(IMemoryCache cache)
} {
private const long TileBytes = 720L * 720 * 3;
public class TileCache(IMemoryCache cache)
{ public Image? Get(string key) => cache.TryGetValue(key, out Image? img) ? img : null;
private const long TileBytes = 720L * 720 * 3;
public void Set(string key, Image img) =>
public Image<Rgb24>? Get(string key) => cache.TryGetValue(key, out Image<Rgb24>? img) ? img : null; cache.Set(key, img, new MemoryCacheEntryOptions
{
public void Set(string key, Image<Rgb24> img) => Size = TileBytes,
cache.Set(key, img, new MemoryCacheEntryOptions SlidingExpiration = TimeSpan.FromMinutes(20)
{ });
Size = TileBytes, }
SlidingExpiration = TimeSpan.FromMinutes(20)
}); public class TileLoader(TileCache cache, string assetDir)
} {
public async Task<Image> LoadAsync(string name, CancellationToken ct)
public class TileLoader(TileCache cache, string assetDir) {
{ if (cache.Get(name) is { } hit)
public async Task<Image<Rgb24>> LoadAsync(string name, CancellationToken ct) return hit;
{
if (cache.Get(name) is { } hit) var image = await Task.Run(() =>
return hit; Image.NewFromFile(Path.Combine(assetDir, $"{name}.png"), access: Enums.Access.Sequential), ct);
var path = Path.Combine(assetDir, $"{name}.png"); cache.Set(name, image);
await using var fs = File.OpenRead(path); return image;
var image = await Image.LoadAsync<Rgb24>(fs, ct).ConfigureAwait(false); }
cache.Set(name, image); }
return image;
} public class LiloStitcher(TileLoader loader)
} {
public async Task<string> CreateImageAsync(GenerateRequest req, CancellationToken ct)
public class LiloStitcher(TileLoader loader) {
{ (int rowMin, int colMin, int rows, int cols) = ParseCanvas(req.CanvasRect);
private const int TileSize = 720;
Validate(req.CropOffset, nameof(req.CropOffset));
private static readonly GraphicsOptions _copy = new() Validate(req.CropSize, nameof(req.CropSize));
{
Antialias = false, double scale = req.OutputScale;
AlphaCompositionMode = PixelAlphaCompositionMode.Src, if (scale <= 0 || scale > 1)
BlendPercentage = 1f throw new ArgumentOutOfRangeException(nameof(req.OutputScale));
};
var tiles = new List<Image>(rows * cols);
public async Task<byte[]> CreateImageAsync(GenerateRequest req, CancellationToken ct) for( int row = 0; row < rows; row++ )
{ {
var parts = req.CanvasRect.ToUpperInvariant().Split(':', StringSplitOptions.TrimEntries | for( int col = 0; col < cols; col++ )
StringSplitOptions.RemoveEmptyEntries); {
var part1 = PlateCoordinate.Parse(parts[0]); string id = $"{RowName( rowMin + row )}{colMin + col}";
var part2 = PlateCoordinate.Parse(parts[1]); var tile = await loader.LoadAsync( id, ct );
if( scale < 1 ) tile = tile.Resize( scale );
int rowMin = Math.Min(part1.Row, part2.Row); tiles.Add( tile );
int rowMax = Math.Max(part1.Row, part2.Row); }
int colMin = Math.Min(part1.Col, part2.Col); }
int colMax = Math.Max(part1.Col, part2.Col);
var mosaic = Image.Arrayjoin(tiles.ToArray(), across: cols);
int rows = rowMax - rowMin + 1;
int cols = colMax - colMin + 1; int offsetX = (int)Math.Truncate(req.CropOffset[0] * mosaic.Width);
int offsetY = (int)Math.Truncate(req.CropOffset[1] * mosaic.Height);
var names = Enumerable.Range(rowMin, rows)
.SelectMany(r => Enumerable.Range(colMin, cols).Select(c => $"{RowName(r)}{c}")) int restWidth = mosaic.Width - offsetX;
.ToArray(); int restHeight = mosaic.Height - offsetY;
var bitmaps = await Task.WhenAll(names.Select(n => loader.LoadAsync(n, ct))).ConfigureAwait(false); int cropWidth = Math.Max(1, (int)Math.Truncate(req.CropSize[0] * restWidth));
var stitched = new Image<Rgb24>(cols * TileSize, rows * TileSize); int cropHeight = Math.Max(1, (int)Math.Truncate(req.CropSize[1] * restHeight));
stitched.Mutate(context => int cropX = (int)Math.Truncate(offsetX / 2.0 + (restWidth - cropWidth) / 2.0);
{ int cropY = (int)Math.Truncate(offsetY / 2.0 + (restHeight - cropHeight) / 2.0);
int idx = 0;
for (int row = 0; row < rows; row++) var cropRect = mosaic.Crop(cropX, cropY, cropWidth, cropHeight);
{
for (int col = 0; col < cols; col++, idx++) string tmpPath = Path.Combine(Path.GetTempPath(), $"mosaic-{Guid.NewGuid():N}.png");
{ cropRect.WriteToFile(tmpPath);
context.DrawImage(bitmaps[idx], new Point(col * TileSize, row * TileSize), 1f);
} return tmpPath;
} }
});
private static (int rowMin, int colMin, int rows, int cols) ParseCanvas(string rect)
Validate(req.CropOffset, 2, nameof(req.CropOffset), inclusiveUpper: true); {
Validate(req.CropSize, 2, nameof(req.CropSize), inclusiveUpper: true); var parts = rect.ToUpperInvariant().Split(':', StringSplitOptions.RemoveEmptyEntries);
var part1 = PlateCoordinate.Parse(parts[0]);
int offsetX = (int)Math.Truncate(req.CropOffset[0] * stitched.Width); var part2 = PlateCoordinate.Parse(parts[1]);
int offsetY = (int)Math.Truncate(req.CropOffset[1] * stitched.Height); int rowMin = Math.Min(part1.Row, part2.Row);
int rowMax = Math.Max(part1.Row, part2.Row);
int restWidth = stitched.Width - offsetX; int colMin = Math.Min(part1.Col, part2.Col);
int restHeight = stitched.Height - offsetY; int colMax = Math.Max(part1.Col, part2.Col);
return (rowMin, colMin, rowMax - rowMin + 1, colMax - colMin + 1);
int cropWidth = Math.Max(1, (int)Math.Truncate(req.CropSize[0] * restWidth )); }
int cropHeight = Math.Max(1, (int)Math.Truncate(req.CropSize[1] * restHeight));
private static void Validate(double[] arr, string name)
int cropX = (int)Math.Truncate(offsetX / 2.0 + (restWidth - cropWidth) / 2.0); {
int cropY = (int)Math.Truncate(offsetY / 2.0 + (restHeight - cropHeight) / 2.0); if (arr is null || arr.Length < 2)
throw new ArgumentException($"{name} length");
var cropRect = new Rectangle(cropX, cropY, cropWidth, cropHeight); if (arr.Any(x => x < 0 || x > 1))
using var cropped = stitched.Clone(context => context.Crop(cropRect)); throw new ArgumentOutOfRangeException(name);
}
double scale = Math.Clamp(req.OutputScale, 0.0, 1.0);
if (scale <= 0 || scale > 1) static string RowName(int row)
throw new ArgumentOutOfRangeException(nameof(req.OutputScale), "OutputScale must be > 0 and ≤ 1.0"); {
var stringBuilder = new System.Text.StringBuilder();
Image<Rgb24> output; while (row > 0)
if (scale < 1.0) {
{ row--;
int width = Math.Max(1, (int)Math.Truncate(cropWidth * scale)); stringBuilder.Insert(0, (char)('A' + row % 26));
int height = Math.Max(1, (int)Math.Truncate(cropHeight * scale)); row /= 26;
output = cropped.Clone(ctx => ctx.Resize(width, height)); }
} return stringBuilder.ToString();
else }
{ }
output = cropped.Clone();
}
using var memStream = new MemoryStream();
await output.SaveAsync(memStream, new PngEncoder
{
CompressionLevel = PngCompressionLevel.Level1
}, ct).ConfigureAwait(false);
return memStream.ToArray();
}
static void Validate(double[] arr, int len, string name, bool inclusiveUpper)
{
if (arr is null || arr.Length < len)
throw new ArgumentException($"{name} must have length {len}");
double upper = inclusiveUpper ? 1.0 : 1.0 - double.Epsilon;
for (int i = 0; i < len; i++)
{
var v = arr[i];
if (v < 0 || v > upper)
throw new ArgumentOutOfRangeException($"{name}[{i}]= {v} outside [0,{upper}]");
}
}
static string RowName(int row)
{
var stringBuilder = new System.Text.StringBuilder();
while (row > 0)
{
row--;
stringBuilder.Insert(0, (char)('A' + row % 26));
row /= 26;
}
return stringBuilder.ToString();
}
}

View file

@ -1,61 +1,68 @@
using System.Diagnostics; using lilo_stitcher_console;
using System.Diagnostics;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
namespace LiloStitcher; namespace LiloStitcher;
public static class Program public static class Program
{ {
public static async Task<int> Main(string[] args) public static async Task<int> Main( string[] args )
{
try
{ {
try NetVips.NetVips.Init();
{ NetVips.NetVips.Concurrency = 3;
Stopwatch sw = Stopwatch.StartNew();
var begin = sw.ElapsedMilliseconds; Stopwatch sw = Stopwatch.StartNew();
var opt = new Options var begin = sw.ElapsedMilliseconds;
{ var opt = new Options
CanvasRect = "A1:AE55", {
CropOffset = [0.4, 0.4], CanvasRect = "A1:AE55",
CropSize = [0.8, 0.8], CropOffset = new[] { 0.4, 0.4 },
OutputScale = 0.5, CropSize = new[] { 0.8, 0.8 },
OutputPath = "stitched.png" OutputScale = 0.5,
}; OutputPath = "stitched.png"
};
string tileFilePath = "../tiles1705"; // should later be directed to the read-only `ASSET_PATH_RO` environment variable string tileFilePath = "../tiles1705"; // should later be directed to the read-only `ASSET_PATH_RO` environment variable
string assetDir = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), tileFilePath)); string assetDir = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), tileFilePath));
using var memCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 256L * 1024 * 1024 }); using var memCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 128L * 1024 * 1024 });
var tileCache = new TileCache(memCache); var tileCache = new TileCache(memCache);
var loader = new TileLoader(tileCache, assetDir); var loader = new TileLoader(tileCache, assetDir);
var stitcher = new LiloStitcher(loader); var stitcher = new lilo_stitcher_console.LiloStitcher(loader);
var req = new GenerateRequest( var req = new GenerateRequest(
opt.CanvasRect!, opt.CanvasRect!,
opt.CropOffset!, opt.CropOffset!,
opt.CropSize!, opt.CropSize!,
opt.OutputScale opt.OutputScale
); );
Console.WriteLine("Stitching..."); Console.WriteLine("Stitching...");
var pngBytes = await stitcher.CreateImageAsync(req, CancellationToken.None); var png = await stitcher.CreateImageAsync(req, CancellationToken.None);
File.Move( png, opt.OutputPath!, overwrite: true );
long bytes = new FileInfo( opt.OutputPath! ).Length;
Console.WriteLine($"Done. Wrote {opt.OutputPath} ({bytes / 1024.0:F1} KB)");
File.WriteAllBytes(opt.OutputPath!, pngBytes); Console.WriteLine(sw.ElapsedMilliseconds - begin);
Console.WriteLine($"Done. Wrote {opt.OutputPath} ({pngBytes.Length / 1024.0:F1} KB)");
return 0;
Console.WriteLine(sw.ElapsedMilliseconds - begin);
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine("ERROR: " + ex.Message);
return 1;
}
} }
catch( Exception ex )
private struct Options
{ {
public string? CanvasRect { get; set; } Console.Error.WriteLine( "ERROR: " + ex.Message );
public double[]? CropOffset { get; set; } return 1;
public double[]? CropSize { get; set; }
public double OutputScale { get; set; }
public string? OutputPath { get; set; }
} }
}
private struct Options
{
public string? CanvasRect { get; set; }
public double[]? CropOffset { get; set; }
public double[]? CropSize { get; set; }
public double OutputScale { get; set; }
public string? OutputPath { get; set; }
}
} }

View file

@ -2,15 +2,17 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<RootNamespace>lilo_stitcher_console</RootNamespace> <RootNamespace>lilo_stitcher_console</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.7" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.7" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" /> <PackageReference Include="NetVips" Version="3.1.0" />
<PackageReference Include="NetVips.Native.linux-x64" Version="8.17.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>