98 lines
2.5 KiB
C#
98 lines
2.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.ObjectPool;
|
|
using OpenCvSharp;
|
|
using StitchATon.DTO;
|
|
using StitchATon.Services;
|
|
using StitchATon.Utility;
|
|
|
|
namespace StitchATon;
|
|
|
|
[ApiController]
|
|
[Route("api/image/")]
|
|
public class ImageController( ImageProvider ip, ObjectPool<PngNamedPipe> npPool, ILogger<Controller> logger ) : ControllerBase
|
|
{
|
|
[HttpPost]
|
|
[Route("generate")]
|
|
public async Task<IActionResult> GetImage([FromBody] GenerateInput generateInput)
|
|
{
|
|
logger.LogInformation( $"GetImage requested at {generateInput.CanvasRect}" );
|
|
|
|
var namedPipe = npPool.Get();
|
|
var imagePath = namedPipe.PipeFullname;
|
|
|
|
using var roiIm = await ip.GetImage(
|
|
generateInput.ParsedCanvasRect(),
|
|
generateInput.ParsedCropOffset(),
|
|
generateInput.ParsedCropSize()
|
|
);
|
|
|
|
var scaledSize = new Size(
|
|
roiIm.Cols * generateInput.OutputScale,
|
|
roiIm.Rows * generateInput.OutputScale
|
|
);
|
|
Mat resizedIm = new();
|
|
|
|
if( scaledSize == roiIm.Size() )
|
|
resizedIm = roiIm;
|
|
else
|
|
Cv2.Resize( roiIm, resizedIm, scaledSize );
|
|
|
|
// Spawn new task to write to the named pipe
|
|
Task.Run( () =>
|
|
{
|
|
Cv2.ImWrite( imagePath, resizedIm );
|
|
resizedIm.Dispose();
|
|
} );
|
|
|
|
// Stream the named pipe as output
|
|
var fileStream = new FileStream(
|
|
imagePath,
|
|
FileMode.Open,
|
|
FileAccess.Read,
|
|
FileShare.Read,
|
|
4096,
|
|
FileOptions.Asynchronous | FileOptions.SequentialScan);
|
|
|
|
return File(fileStream, "image/png");
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("sanity")]
|
|
public async Task<IActionResult> GetImageSanityTest()
|
|
{
|
|
var namedPipe = npPool.Get();
|
|
var imagePath = namedPipe.PipeFullname;
|
|
|
|
using var roiIm = await ip.GetImage(
|
|
new Rect( new Point( 5, 6 ), new Size( 3, 3 ) ),
|
|
new Point2f( .1f, .1f ),
|
|
new Point2f( .8f, .8f ) );
|
|
|
|
var mul = .7;
|
|
var scaledSize = new Size(roiIm.Cols * mul , roiIm.Rows * mul);
|
|
Mat resizedIm = new();
|
|
|
|
if( scaledSize == roiIm.Size() )
|
|
resizedIm = roiIm;
|
|
else
|
|
Cv2.Resize( roiIm, resizedIm, scaledSize );
|
|
|
|
// Spawn new task to write to the named pipe
|
|
Task.Run( () =>
|
|
{
|
|
Cv2.ImWrite( imagePath, resizedIm );
|
|
resizedIm.Dispose();
|
|
} );
|
|
|
|
// Stream the named pipe as output
|
|
var fileStream = new FileStream(
|
|
imagePath,
|
|
FileMode.Open,
|
|
FileAccess.Read,
|
|
FileShare.Read,
|
|
4096,
|
|
FileOptions.Asynchronous | FileOptions.SequentialScan);
|
|
|
|
return File(fileStream, "image/png");
|
|
}
|
|
}
|