chore : replaced var with explicit types

This commit is contained in:
gelaws-hub 2025-07-31 20:41:48 +07:00
parent b344b6a03f
commit 64841332e0
4 changed files with 34 additions and 33 deletions

View file

@ -38,7 +38,6 @@ public static class ImageController
} }
catch (Exception) catch (Exception)
{ {
// In a real app, log the exception here.
return Results.Problem( return Results.Problem(
detail: "An internal error occurred.", detail: "An internal error occurred.",
statusCode: StatusCodes.Status500InternalServerError statusCode: StatusCodes.Status500InternalServerError

View file

@ -19,11 +19,13 @@ public class ImageService : IImageService
public async Task<byte[]> GenerateImageAsync(GenerateImageRequest request) public async Task<byte[]> GenerateImageAsync(GenerateImageRequest request)
{ {
// 1. Delegate parsing to the CoordinateParser // 1. Delegate parsing to the CoordinateParser
var (minRow, minCol, maxRow, maxCol) = CoordinateParser.ParseCanvasRect(request.CanvasRect); (int minRow, int minCol, int maxRow, int maxCol) = CoordinateParser.ParseCanvasRect(
request.CanvasRect
);
// 2. Perform high-level calculations // 2. Perform high-level calculations
var stitchedCanvasWidth = (maxCol - minCol + 1) * TILE_SIZE; int stitchedCanvasWidth = (maxCol - minCol + 1) * TILE_SIZE;
var stitchedCanvasHeight = (maxRow - minRow + 1) * TILE_SIZE; int stitchedCanvasHeight = (maxRow - minRow + 1) * TILE_SIZE;
int cropX = (int)(request.CropOffset[0] * stitchedCanvasWidth); int cropX = (int)(request.CropOffset[0] * stitchedCanvasWidth);
int cropY = (int)(request.CropOffset[1] * stitchedCanvasHeight); int cropY = (int)(request.CropOffset[1] * stitchedCanvasHeight);
@ -35,13 +37,13 @@ public class ImageService : IImageService
throw new ArgumentException("Calculated crop dimensions are invalid."); throw new ArgumentException("Calculated crop dimensions are invalid.");
} }
var startTileCol = minCol + (cropX / TILE_SIZE); int startTileCol = minCol + (cropX / TILE_SIZE);
var endTileCol = minCol + ((cropX + cropW - 1) / TILE_SIZE); int endTileCol = minCol + ((cropX + cropW - 1) / TILE_SIZE);
var startTileRow = minRow + (cropY / TILE_SIZE); int startTileRow = minRow + (cropY / TILE_SIZE);
var endTileRow = minRow + ((cropY + cropH - 1) / TILE_SIZE); int endTileRow = minRow + ((cropY + cropH - 1) / TILE_SIZE);
// 3. Create a parameter object for the processor // 3. Create a parameter object for the processor
var stitchRequest = new StitchRequest( StitchRequest stitchRequest = new StitchRequest(
minRow, minRow,
minCol, minCol,
startTileRow, startTileRow,

View file

@ -16,14 +16,14 @@ public static class CoordinateParser
throw new ArgumentException("canvas_rect cannot be null or empty."); throw new ArgumentException("canvas_rect cannot be null or empty.");
} }
var parts = rect.Split(':'); string[] parts = rect.Split(':');
if (parts.Length != 2) if (parts.Length != 2)
{ {
throw new ArgumentException("Invalid canvas_rect format."); throw new ArgumentException("Invalid canvas_rect format.");
} }
var (row1, col1) = ParseSingleCoordinate(parts[0]); (int row1, int col1) = ParseSingleCoordinate(parts[0]);
var (row2, col2) = ParseSingleCoordinate(parts[1]); (int row2, int col2) = ParseSingleCoordinate(parts[1]);
return ( return (
Math.Min(row1, row2), Math.Min(row1, row2),
@ -35,14 +35,14 @@ public static class CoordinateParser
private static (int Row, int Col) ParseSingleCoordinate(string coord) private static (int Row, int Col) ParseSingleCoordinate(string coord)
{ {
var match = CoordRegex.Match(coord); Match match = CoordRegex.Match(coord);
if (!match.Success) if (!match.Success)
{ {
throw new ArgumentException($"Invalid coordinate format: {coord}"); throw new ArgumentException($"Invalid coordinate format: {coord}");
} }
var rowStr = match.Groups[1].Value.ToUpper(); string rowStr = match.Groups[1].Value.ToUpper();
var colStr = match.Groups[2].Value; string colStr = match.Groups[2].Value;
int row = rowStr.Length == 1 ? rowStr[0] - 'A' : 26 + (rowStr[1] - 'A'); int row = rowStr.Length == 1 ? rowStr[0] - 'A' : 26 + (rowStr[1] - 'A');
int col = int.Parse(colStr) - 1; int col = int.Parse(colStr) - 1;

View file

@ -16,36 +16,36 @@ internal class ImageProcessor
public async Task<byte[]> StitchAndCropAsync(StitchRequest stitchRequest) public async Task<byte[]> StitchAndCropAsync(StitchRequest stitchRequest)
{ {
using var finalImage = new Image<Rgba32>(stitchRequest.CropW, stitchRequest.CropH); using Image<Rgba32> finalImage = new(stitchRequest.CropW, stitchRequest.CropH);
for (var r = stitchRequest.StartTileRow; r <= stitchRequest.EndTileRow; r++) for (int r = stitchRequest.StartTileRow; r <= stitchRequest.EndTileRow; r++)
{ {
for (var c = stitchRequest.StartTileCol; c <= stitchRequest.EndTileCol; c++) for (int c = stitchRequest.StartTileCol; c <= stitchRequest.EndTileCol; c++)
{ {
var tileFileName = TileHelper.GetTileFileName(r, c); string tileFileName = TileHelper.GetTileFileName(r, c);
var tileFilePath = Path.Combine(_assetPath, tileFileName); string tileFilePath = Path.Combine(_assetPath, tileFileName);
if (!File.Exists(tileFilePath)) if (!File.Exists(tileFilePath))
{ {
throw new FileNotFoundException($"Asset not found: {tileFileName}"); throw new FileNotFoundException($"Asset not found: {tileFileName}");
} }
using var tileImage = await Image.LoadAsync(tileFilePath); using Image tileImage = await Image.LoadAsync(tileFilePath);
var tileOriginX = (c - stitchRequest.MinCol) * TILE_SIZE; int tileOriginX = (c - stitchRequest.MinCol) * TILE_SIZE;
var tileOriginY = (r - stitchRequest.MinRow) * TILE_SIZE; int tileOriginY = (r - stitchRequest.MinRow) * TILE_SIZE;
var srcX = Math.Max(0, stitchRequest.CropX - tileOriginX); int srcX = Math.Max(0, stitchRequest.CropX - tileOriginX);
var srcY = Math.Max(0, stitchRequest.CropY - tileOriginY); int srcY = Math.Max(0, stitchRequest.CropY - tileOriginY);
var destX = Math.Max(0, tileOriginX - stitchRequest.CropX); int destX = Math.Max(0, tileOriginX - stitchRequest.CropX);
var destY = Math.Max(0, tileOriginY - stitchRequest.CropY); int destY = Math.Max(0, tileOriginY - stitchRequest.CropY);
var overlapW = Math.Max( int overlapW = Math.Max(
0, 0,
Math.Min(stitchRequest.CropX + stitchRequest.CropW, tileOriginX + TILE_SIZE) Math.Min(stitchRequest.CropX + stitchRequest.CropW, tileOriginX + TILE_SIZE)
- Math.Max(stitchRequest.CropX, tileOriginX) - Math.Max(stitchRequest.CropX, tileOriginX)
); );
var overlapH = Math.Max( int overlapH = Math.Max(
0, 0,
Math.Min(stitchRequest.CropY + stitchRequest.CropH, tileOriginY + TILE_SIZE) Math.Min(stitchRequest.CropY + stitchRequest.CropH, tileOriginY + TILE_SIZE)
- Math.Max(stitchRequest.CropY, tileOriginY) - Math.Max(stitchRequest.CropY, tileOriginY)
@ -53,7 +53,7 @@ internal class ImageProcessor
if (overlapW > 0 && overlapH > 0) if (overlapW > 0 && overlapH > 0)
{ {
var sourceRect = new Rectangle(srcX, srcY, overlapW, overlapH); Rectangle sourceRect = new Rectangle(srcX, srcY, overlapW, overlapH);
finalImage.Mutate(ctx => finalImage.Mutate(ctx =>
ctx.DrawImage( ctx.DrawImage(
tileImage, tileImage,
@ -68,12 +68,12 @@ internal class ImageProcessor
if (stitchRequest.OutputScale > 0 && stitchRequest.OutputScale < 1.0) if (stitchRequest.OutputScale > 0 && stitchRequest.OutputScale < 1.0)
{ {
var newWidth = (int)(stitchRequest.CropW * stitchRequest.OutputScale); int newWidth = (int)(stitchRequest.CropW * stitchRequest.OutputScale);
var newHeight = (int)(stitchRequest.CropH * stitchRequest.OutputScale); int newHeight = (int)(stitchRequest.CropH * stitchRequest.OutputScale);
finalImage.Mutate(x => x.Resize(newWidth, newHeight, KnownResamplers.Bicubic)); finalImage.Mutate(x => x.Resize(newWidth, newHeight, KnownResamplers.Bicubic));
} }
using var memoryStream = new MemoryStream(); using MemoryStream memoryStream = new MemoryStream();
await finalImage.SaveAsPngAsync(memoryStream); await finalImage.SaveAsPngAsync(memoryStream);
return memoryStream.ToArray(); return memoryStream.ToArray();
} }