scale before stitch

This commit is contained in:
Meizar Rimadana 2025-07-26 08:50:47 +07:00
parent 8d4b2bc943
commit d26bcf545c
4 changed files with 47 additions and 4 deletions

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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")