From e2702c00ee9976095b90283dfecde5d9f1df6d7c Mon Sep 17 00:00:00 2001 From: Meizar Date: Wed, 30 Jul 2025 21:59:04 +0700 Subject: [PATCH] cleanup --- WebApp/Coordinate.cs | 5 +- WebApp/ImageGenerator.cs | 108 ++----------------------------------- WebApp/RequestBody.cs | 6 +-- WebApp/WebApplication.http | 6 --- 4 files changed, 8 insertions(+), 117 deletions(-) delete mode 100644 WebApp/WebApplication.http diff --git a/WebApp/Coordinate.cs b/WebApp/Coordinate.cs index e672d4e..2998d29 100644 --- a/WebApp/Coordinate.cs +++ b/WebApp/Coordinate.cs @@ -2,12 +2,11 @@ internal record Coordinate { + private static readonly string BasePath = Environment.GetEnvironmentVariable("ASSET_PATH_RO") ?? ""; public string Name { get; } public int Row { get; } public int Col { get; } - public string Path => PATH + Name + ".png"; - - private const string PATH = "D:/tiles1705/"; + public string Path => BasePath + Name + ".png"; public Coordinate(string name) { diff --git a/WebApp/ImageGenerator.cs b/WebApp/ImageGenerator.cs index 5ef919c..3883476 100644 --- a/WebApp/ImageGenerator.cs +++ b/WebApp/ImageGenerator.cs @@ -1,79 +1,9 @@ -using System.Collections.Concurrent; -using OpenCvSharp; +using OpenCvSharp; namespace WebApp; public class ImageGenerator { - private const string PATH = "D:/tiles1705/"; - - public byte[] GenerateImage(RequestBody requestBody) - { - var start = DateTime.Now; - - string[] inputs = requestBody.CanvasRect.Split(":"); - Coordinate coord0 = new Coordinate(inputs[0]); - Coordinate coord1 = new Coordinate(inputs[1]); - (var matrix, _, _) = GenerateDict(coord0, coord1); - - ConcurrentDictionary rows = new(); - Parallel.ForEach(matrix, pair => - { - Mat row = new Mat(); - List mats = new(); - foreach (var coord in pair.Value) - { - string fileName = PATH + coord.Name + ".png"; - mats.Add(new Mat( fileName ) ); - } - - Cv2.HConcat(mats, row); - rows[pair.Key] = row; - }); - - Mat output = new Mat(); - Cv2.VConcat(rows.Values, output); - - var result = output.ImEncode(); - var end = DateTime.Now; - var elapsed = end - start; - Console.WriteLine($"Elapsed: {elapsed.TotalMilliseconds} ms"); - return result; - } - - public byte[] GenerateImage2(RequestBody requestBody) - { - var start = DateTime.Now; - Coordinate a1 = new Coordinate("A1"); - Mat a1Mat = new Mat(a1.Path); - - string[] inputs = requestBody.CanvasRect.Split(":"); - Coordinate coord0 = new Coordinate(inputs[0]); - Coordinate coord1 = new Coordinate(inputs[1]); - - double scale = requestBody.OutputScale; - - (var matrix, int rowCount, int colCount) = GenerateMatrix(coord0, coord1); - Mat temp = GenerateMatCanvas(a1Mat, rowCount, colCount, new Rect()); - - Parallel.ForEach(matrix, item => - { - Mat mat = new Mat(item.Path); - Rect rect = new Rect((item.Col - 1) * a1Mat.Cols, (item.Row - 1) * a1Mat.Rows, a1Mat.Cols, a1Mat.Rows); - mat.CopyTo(temp[rect]); - }); - - double newWidth = temp.Width * scale; - double newHeight = temp.Height * scale; - - 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; @@ -136,7 +66,7 @@ public class ImageGenerator return result; } - internal (List Matrix, int RowCount, int ColCount) GenerateMatrix(Coordinate coordinate1, Coordinate coordinate2) + private (List Matrix, int RowCount, int ColCount) GenerateMatrix(Coordinate coordinate1, Coordinate coordinate2) { int minRow = Math.Min(coordinate1.Row, coordinate2.Row); int maxRow = Math.Max(coordinate1.Row, coordinate2.Row); @@ -159,38 +89,6 @@ public class ImageGenerator return (results, rowCount, colCount); } - internal (ConcurrentDictionary> Matrix, int RowCount, int ColCount) GenerateDict(Coordinate coordinate1, Coordinate coordinate2) - { - ConcurrentDictionary> results = new(); - int minRow = Math.Min(coordinate1.Row, coordinate2.Row); - int maxRow = Math.Max(coordinate1.Row, coordinate2.Row); - int minCol = Math.Min(coordinate1.Col, coordinate2.Col); - int maxCol = Math.Max(coordinate1.Col, coordinate2.Col); - - int rowCount = maxRow - minRow + 1; - int colCount = maxCol - minCol + 1; - - for (int i = 0; i < rowCount; i++) - { - List result = new(); - for (int j = 0; j < colCount; j++) - { - int row = minRow + i; - int col = minCol + j; - result.Add(new Coordinate(row, col)); - } - results[i] = result; - } - - return (results, rowCount, colCount); - } - - internal Mat GenerateMatCanvas(Mat reference, int rowCount, int colCount, Rect canvas) - { - return new Mat(canvas.Width, canvas.Height, reference.Type()); - // return new Mat(reference.Rows * rowCount - offset.X, reference.Cols * colCount - offset.Y, reference.Type()); - } - private Rect GenerateInitialRectCanvas(Mat reference, int rowCount, int colCount) { int width = reference.Width * colCount; @@ -198,7 +96,7 @@ public class ImageGenerator return new Rect(0, 0, width, height); } - internal Rect GenerateGlobalRoI(double[] cropOffset, double[] cropSize, int width, int height) + private Rect GenerateGlobalRoI(float[] cropOffset, float[] cropSize, int width, int height) { int x = (int)(cropOffset[0] * width); int y = (int)(cropOffset[1] * height); diff --git a/WebApp/RequestBody.cs b/WebApp/RequestBody.cs index 9a821fa..3dd7a7c 100644 --- a/WebApp/RequestBody.cs +++ b/WebApp/RequestBody.cs @@ -6,8 +6,8 @@ public record struct RequestBody( [property: JsonPropertyName("canvas_rect")] string CanvasRect, [property: JsonPropertyName("crop_offset")] - double[] CropOffset, + float[] CropOffset, [property: JsonPropertyName("crop_size")] - double[] CropSize, + float[] CropSize, [property: JsonPropertyName("output_scale")] - double OutputScale ); \ No newline at end of file + float OutputScale ); \ No newline at end of file diff --git a/WebApp/WebApplication.http b/WebApp/WebApplication.http deleted file mode 100644 index d4de5da..0000000 --- a/WebApp/WebApplication.http +++ /dev/null @@ -1,6 +0,0 @@ -@WebApplication_HostAddress = http://localhost:5184 - -GET {{WebApplication_HostAddress}}/weatherforecast/ -Accept: application/json - -###