From d26bcf545cbe80f8c7ebd02e4e067cf8a6256f0e Mon Sep 17 00:00:00 2001 From: Meizar Rimadana Date: Sat, 26 Jul 2025 08:50:47 +0700 Subject: [PATCH] scale before stitch --- StitchATon.sln.DotSettings | 3 +++ WebApp/Coordinate.cs | 4 ++-- WebApp/ImageGenerator.cs | 42 +++++++++++++++++++++++++++++++++++++- WebApp/Program.cs | 2 +- 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 StitchATon.sln.DotSettings diff --git a/StitchATon.sln.DotSettings b/StitchATon.sln.DotSettings new file mode 100644 index 0000000..41c9b03 --- /dev/null +++ b/StitchATon.sln.DotSettings @@ -0,0 +1,3 @@ + + C:\Program Files\dotnet\sdk\6.0.407\MSBuild.dll + 4294967294 \ No newline at end of file diff --git a/WebApp/Coordinate.cs b/WebApp/Coordinate.cs index 7f5cd29..261cb85 100644 --- a/WebApp/Coordinate.cs +++ b/WebApp/Coordinate.cs @@ -20,9 +20,9 @@ internal record Coordinate { row = row * 26 + (item - 'A' + 1); } - else if (item >= '1') + else if (item >= '0') { - col = col * 10 + (item - '1' + 1); + col = col * 10 + (item - '0' + 1); } } diff --git a/WebApp/ImageGenerator.cs b/WebApp/ImageGenerator.cs index deaf9fe..44955f9 100644 --- a/WebApp/ImageGenerator.cs +++ b/WebApp/ImageGenerator.cs @@ -66,7 +66,47 @@ public class ImageGenerator double newWidth = temp.Width * scale; double newHeight = temp.Height * scale; - temp.Resize(new Size(newWidth, newHeight)); + var resized = temp.Resize(new Size(newWidth, newHeight)); + var result = resized.ImEncode(); + var end = DateTime.Now; + var elapsed = end - start; + Console.WriteLine($"Elapsed: {elapsed.TotalMilliseconds} ms"); + return result; + } + + public byte[] GenerateImage3(RequestBody requestBody) + { + var start = DateTime.Now; + string[] inputs = requestBody.CanvasRect.Split(":"); + double scale = requestBody.OutputScale; + Coordinate a1 = new Coordinate("A1"); + Mat a1Mat = new Mat(a1.Path); + int width = a1Mat.Width; + int height = a1Mat.Height; + if (scale < 1) + { + width = (int)(width * scale); + height = (int)(height * scale); + a1Mat = a1Mat.Resize(new Size(width, height)); + } + + Coordinate coord0 = new Coordinate(inputs[0]); + Coordinate coord1 = new Coordinate(inputs[1]); + + (var matrix, int rowCount, int colCount) = GenerateMatrix(coord0, coord1); + Mat temp = GenerateTempMat(a1Mat, rowCount, colCount); + + Parallel.ForEach(matrix, item => + { + Mat mat = new Mat(item.Path); + if (scale < 1) + { + mat = mat.Resize(new Size(width, height)); + } + Rect rect = new Rect((item.Col - 1) * a1Mat.Cols, (item.Row - 1) * a1Mat.Rows, a1Mat.Cols, a1Mat.Rows); + mat.CopyTo(temp[rect]); + }); + var result = temp.ImEncode(); var end = DateTime.Now; var elapsed = end - start; diff --git a/WebApp/Program.cs b/WebApp/Program.cs index f972a72..4664708 100644 --- a/WebApp/Program.cs +++ b/WebApp/Program.cs @@ -35,7 +35,7 @@ app.MapPost("/api/image/generate", ( RequestBody requestBody ) => { ImageGenerator imageGenerator = new ImageGenerator(); - var png = imageGenerator.GenerateImage2( requestBody ); + var png = imageGenerator.GenerateImage3( requestBody ); return Results.File(png, "image/png", "result.png"); }) .WithName("ImageGenerator")