benscode-StitcherApi/Services/ImageService.cs

69 lines
2.3 KiB
C#
Raw Normal View History

2025-07-31 19:39:06 +07:00
using StitcherApi.Models;
using StitcherApi.Services.Utilities;
namespace StitcherApi.Services;
public class ImageService : IImageService
{
private readonly ILogger<ImageService> _logger;
private readonly StitchProcessor _stitchProcessor;
public ImageService(IConfiguration config, ILoggerFactory loggerFactory)
2025-07-31 19:39:06 +07:00
{
_logger = loggerFactory.CreateLogger<ImageService>();
2025-07-31 19:39:06 +07:00
string assetPath =
config["AssetPath"] ?? throw new InvalidOperationException("AssetPath not configured.");
_stitchProcessor = new StitchProcessor(
assetPath,
loggerFactory.CreateLogger<StitchProcessor>()
);
2025-07-31 19:39:06 +07:00
}
public async Task<byte[]> GenerateImageAsync(GenerateImageRequest request)
{
_logger.LogInformation("Processing request with the fast processor...");
(int minRow, int minCol, int maxRow, int maxCol) = CoordinateParser.ParseCanvasRect(
request.CanvasRect
);
2025-07-31 19:39:06 +07:00
StitchRequest stitchRequest = CreateStitchRequest(request, minRow, minCol, maxRow, maxCol);
return await _stitchProcessor.ProcessAsync(stitchRequest);
}
private StitchRequest CreateStitchRequest(
GenerateImageRequest request,
int minRow,
int minCol,
int maxRow,
int maxCol
)
{
const int TILE_SIZE = 720;
int stitchedCanvasWidth = (maxCol - minCol + 1) * TILE_SIZE;
int stitchedCanvasHeight = (maxRow - minRow + 1) * TILE_SIZE;
2025-07-31 19:39:06 +07:00
int cropX = (int)(request.CropOffset[0] * stitchedCanvasWidth);
int cropY = (int)(request.CropOffset[1] * stitchedCanvasHeight);
int cropW = (int)(request.CropSize[0] * stitchedCanvasWidth);
int cropH = (int)(request.CropSize[1] * stitchedCanvasHeight);
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;
2025-07-31 19:39:06 +07:00
return new StitchRequest(
2025-07-31 19:39:06 +07:00
minRow,
minCol,
startTileRow,
startTileCol,
endTileRow,
endTileCol,
cropX,
cropY,
cropW,
cropH,
request.OutputScale
);
}
}