From 64841332e009c64f601a24b23783e05113f4d8ee Mon Sep 17 00:00:00 2001 From: gelaws-hub Date: Thu, 31 Jul 2025 20:41:48 +0700 Subject: [PATCH] chore : replaced var with explicit types --- Controllers/ImageController.cs | 1 - Services/ImageService.cs | 18 +++++++------ Services/Utilities/CoordinateParser.cs | 12 ++++----- Services/Utilities/ImageProcessor.cs | 36 +++++++++++++------------- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/Controllers/ImageController.cs b/Controllers/ImageController.cs index 63d90b0..0c9d6ca 100644 --- a/Controllers/ImageController.cs +++ b/Controllers/ImageController.cs @@ -38,7 +38,6 @@ public static class ImageController } catch (Exception) { - // In a real app, log the exception here. return Results.Problem( detail: "An internal error occurred.", statusCode: StatusCodes.Status500InternalServerError diff --git a/Services/ImageService.cs b/Services/ImageService.cs index 73e82ba..0f8651e 100644 --- a/Services/ImageService.cs +++ b/Services/ImageService.cs @@ -19,11 +19,13 @@ public class ImageService : IImageService public async Task GenerateImageAsync(GenerateImageRequest request) { // 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 - var stitchedCanvasWidth = (maxCol - minCol + 1) * TILE_SIZE; - var stitchedCanvasHeight = (maxRow - minRow + 1) * TILE_SIZE; + int stitchedCanvasWidth = (maxCol - minCol + 1) * TILE_SIZE; + int stitchedCanvasHeight = (maxRow - minRow + 1) * TILE_SIZE; int cropX = (int)(request.CropOffset[0] * stitchedCanvasWidth); int cropY = (int)(request.CropOffset[1] * stitchedCanvasHeight); @@ -35,13 +37,13 @@ public class ImageService : IImageService throw new ArgumentException("Calculated crop dimensions are invalid."); } - var startTileCol = minCol + (cropX / TILE_SIZE); - var endTileCol = minCol + ((cropX + cropW - 1) / TILE_SIZE); - var startTileRow = minRow + (cropY / TILE_SIZE); - var endTileRow = minRow + ((cropY + cropH - 1) / TILE_SIZE); + int startTileCol = minCol + (cropX / TILE_SIZE); + int endTileCol = minCol + ((cropX + cropW - 1) / TILE_SIZE); + int startTileRow = minRow + (cropY / TILE_SIZE); + int endTileRow = minRow + ((cropY + cropH - 1) / TILE_SIZE); // 3. Create a parameter object for the processor - var stitchRequest = new StitchRequest( + StitchRequest stitchRequest = new StitchRequest( minRow, minCol, startTileRow, diff --git a/Services/Utilities/CoordinateParser.cs b/Services/Utilities/CoordinateParser.cs index a162631..f07e79b 100644 --- a/Services/Utilities/CoordinateParser.cs +++ b/Services/Utilities/CoordinateParser.cs @@ -16,14 +16,14 @@ public static class CoordinateParser throw new ArgumentException("canvas_rect cannot be null or empty."); } - var parts = rect.Split(':'); + string[] parts = rect.Split(':'); if (parts.Length != 2) { throw new ArgumentException("Invalid canvas_rect format."); } - var (row1, col1) = ParseSingleCoordinate(parts[0]); - var (row2, col2) = ParseSingleCoordinate(parts[1]); + (int row1, int col1) = ParseSingleCoordinate(parts[0]); + (int row2, int col2) = ParseSingleCoordinate(parts[1]); return ( Math.Min(row1, row2), @@ -35,14 +35,14 @@ public static class CoordinateParser private static (int Row, int Col) ParseSingleCoordinate(string coord) { - var match = CoordRegex.Match(coord); + Match match = CoordRegex.Match(coord); if (!match.Success) { throw new ArgumentException($"Invalid coordinate format: {coord}"); } - var rowStr = match.Groups[1].Value.ToUpper(); - var colStr = match.Groups[2].Value; + string rowStr = match.Groups[1].Value.ToUpper(); + string colStr = match.Groups[2].Value; int row = rowStr.Length == 1 ? rowStr[0] - 'A' : 26 + (rowStr[1] - 'A'); int col = int.Parse(colStr) - 1; diff --git a/Services/Utilities/ImageProcessor.cs b/Services/Utilities/ImageProcessor.cs index 9c41251..393f012 100644 --- a/Services/Utilities/ImageProcessor.cs +++ b/Services/Utilities/ImageProcessor.cs @@ -16,36 +16,36 @@ internal class ImageProcessor public async Task StitchAndCropAsync(StitchRequest stitchRequest) { - using var finalImage = new Image(stitchRequest.CropW, stitchRequest.CropH); + using Image 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); - var tileFilePath = Path.Combine(_assetPath, tileFileName); + string tileFileName = TileHelper.GetTileFileName(r, c); + string tileFilePath = Path.Combine(_assetPath, tileFileName); if (!File.Exists(tileFilePath)) { 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; - var tileOriginY = (r - stitchRequest.MinRow) * TILE_SIZE; + int tileOriginX = (c - stitchRequest.MinCol) * TILE_SIZE; + int tileOriginY = (r - stitchRequest.MinRow) * TILE_SIZE; - var srcX = Math.Max(0, stitchRequest.CropX - tileOriginX); - var srcY = Math.Max(0, stitchRequest.CropY - tileOriginY); - var destX = Math.Max(0, tileOriginX - stitchRequest.CropX); - var destY = Math.Max(0, tileOriginY - stitchRequest.CropY); + int srcX = Math.Max(0, stitchRequest.CropX - tileOriginX); + int srcY = Math.Max(0, stitchRequest.CropY - tileOriginY); + int destX = Math.Max(0, tileOriginX - stitchRequest.CropX); + int destY = Math.Max(0, tileOriginY - stitchRequest.CropY); - var overlapW = Math.Max( + int overlapW = Math.Max( 0, Math.Min(stitchRequest.CropX + stitchRequest.CropW, tileOriginX + TILE_SIZE) - Math.Max(stitchRequest.CropX, tileOriginX) ); - var overlapH = Math.Max( + int overlapH = Math.Max( 0, Math.Min(stitchRequest.CropY + stitchRequest.CropH, tileOriginY + TILE_SIZE) - Math.Max(stitchRequest.CropY, tileOriginY) @@ -53,7 +53,7 @@ internal class ImageProcessor if (overlapW > 0 && overlapH > 0) { - var sourceRect = new Rectangle(srcX, srcY, overlapW, overlapH); + Rectangle sourceRect = new Rectangle(srcX, srcY, overlapW, overlapH); finalImage.Mutate(ctx => ctx.DrawImage( tileImage, @@ -68,12 +68,12 @@ internal class ImageProcessor if (stitchRequest.OutputScale > 0 && stitchRequest.OutputScale < 1.0) { - var newWidth = (int)(stitchRequest.CropW * stitchRequest.OutputScale); - var newHeight = (int)(stitchRequest.CropH * stitchRequest.OutputScale); + int newWidth = (int)(stitchRequest.CropW * stitchRequest.OutputScale); + int newHeight = (int)(stitchRequest.CropH * stitchRequest.OutputScale); finalImage.Mutate(x => x.Resize(newWidth, newHeight, KnownResamplers.Bicubic)); } - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new MemoryStream(); await finalImage.SaveAsPngAsync(memoryStream); return memoryStream.ToArray(); }