mirror of https://github.com/ivanch/tcc.git
mini refactor
This commit is contained in:
parent
6755d55342
commit
6f411c3c52
|
@ -1,55 +1,30 @@
|
||||||
using ImageMagick;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using TCC.Services;
|
||||||
|
|
||||||
namespace tcc_app.Controllers
|
namespace TCC.Controllers
|
||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("")]
|
[Route("")]
|
||||||
public class ImageController : ControllerBase
|
public class ImageController : ControllerBase
|
||||||
{
|
{
|
||||||
|
private ImageService ImageService { get; set; }
|
||||||
|
|
||||||
|
public ImageController(ImageService imageService)
|
||||||
|
{
|
||||||
|
this.ImageService = imageService;
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost("blur")]
|
[HttpPost("blur")]
|
||||||
public async Task<IActionResult> BlurImage()
|
public async Task<IActionResult> BlurImage()
|
||||||
{
|
{
|
||||||
MemoryStream mstream = new MemoryStream();
|
MemoryStream mstream = new MemoryStream();
|
||||||
await HttpContext.Request.Body.CopyToAsync(mstream);
|
await HttpContext.Request.Body.CopyToAsync(mstream);
|
||||||
|
mstream.Position = 0;
|
||||||
|
|
||||||
var image = new MagickImage(new MemoryStream(mstream.ToArray()));
|
var result = ImageService.BoxBlurImage(mstream, 5);
|
||||||
var blurredImage = new MagickImage(image);
|
|
||||||
var pixels = image.GetPixels();
|
|
||||||
var blurredPixels = blurredImage.GetPixelsUnsafe();
|
|
||||||
|
|
||||||
foreach (var pixel in pixels)
|
|
||||||
{
|
|
||||||
int x = pixel.X,
|
|
||||||
y = pixel.Y;
|
|
||||||
|
|
||||||
long rTotal = 0, gTotal = 0, bTotal = 0;
|
|
||||||
int pixelCount = 0;
|
|
||||||
for (short offsetY = -5; offsetY <= 5; offsetY++)
|
|
||||||
{
|
|
||||||
for (short offsetX = -5; offsetX <= 5; offsetX++)
|
|
||||||
{
|
|
||||||
int newX = x + offsetX;
|
|
||||||
int newY = y + offsetY;
|
|
||||||
|
|
||||||
if (newX >= 0 && newX < image.Width && newY >= 0 && newY < image.Height)
|
|
||||||
{
|
|
||||||
var pixelColor = pixels[newX, newY];
|
|
||||||
rTotal += pixelColor.GetChannel(0);
|
|
||||||
gTotal += pixelColor.GetChannel(1);
|
|
||||||
bTotal += pixelColor.GetChannel(2);
|
|
||||||
pixelCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
blurredPixels[x, y].SetChannel(0, Convert.ToUInt16(rTotal / pixelCount));
|
|
||||||
blurredPixels[x, y].SetChannel(1, Convert.ToUInt16(gTotal / pixelCount));
|
|
||||||
blurredPixels[x, y].SetChannel(2, Convert.ToUInt16(bTotal / pixelCount));
|
|
||||||
}
|
|
||||||
|
|
||||||
var blurredImageStream = new MemoryStream();
|
var blurredImageStream = new MemoryStream();
|
||||||
blurredImage.Write(blurredImageStream);
|
result.Write(blurredImageStream);
|
||||||
blurredImageStream.Position = 0;
|
blurredImageStream.Position = 0;
|
||||||
|
|
||||||
return File(blurredImageStream, "image/png");
|
return File(blurredImageStream, "image/png");
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
namespace tcc_app
|
using TCC.Services;
|
||||||
|
|
||||||
|
namespace TCC
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
@ -6,6 +8,8 @@ namespace tcc_app
|
||||||
{
|
{
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
builder.Services.AddScoped<ImageService>();
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
// </auto-generated>
|
// </auto-generated>
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
namespace tcc_app.Properties {
|
namespace TCC.Properties {
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
using ImageMagick;
|
||||||
|
|
||||||
|
namespace TCC.Services
|
||||||
|
{
|
||||||
|
public class ImageService
|
||||||
|
{
|
||||||
|
public ImageService() { }
|
||||||
|
|
||||||
|
public MagickImage BoxBlurImage(Stream imageStream, int radius)
|
||||||
|
{
|
||||||
|
var image = new MagickImage(imageStream);
|
||||||
|
var blurredImage = new MagickImage(image);
|
||||||
|
var pixels = image.GetPixels();
|
||||||
|
var blurredPixels = blurredImage.GetPixelsUnsafe();
|
||||||
|
|
||||||
|
foreach (var pixel in pixels)
|
||||||
|
{
|
||||||
|
int x = pixel.X,
|
||||||
|
y = pixel.Y;
|
||||||
|
|
||||||
|
long rTotal = 0, gTotal = 0, bTotal = 0;
|
||||||
|
int pixelCount = 0;
|
||||||
|
for (int offsetY = -radius; offsetY <= radius; offsetY++)
|
||||||
|
{
|
||||||
|
for (int offsetX = -radius; offsetX <= radius; offsetX++)
|
||||||
|
{
|
||||||
|
int newX = x + offsetX;
|
||||||
|
int newY = y + offsetY;
|
||||||
|
|
||||||
|
if (newX >= 0 && newX < image.Width && newY >= 0 && newY < image.Height)
|
||||||
|
{
|
||||||
|
var pixelColor = pixels[newX, newY];
|
||||||
|
rTotal += pixelColor.GetChannel(0);
|
||||||
|
gTotal += pixelColor.GetChannel(1);
|
||||||
|
bTotal += pixelColor.GetChannel(2);
|
||||||
|
pixelCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
blurredPixels[x, y].SetChannel(0, Convert.ToUInt16(rTotal / pixelCount));
|
||||||
|
blurredPixels[x, y].SetChannel(1, Convert.ToUInt16(gTotal / pixelCount));
|
||||||
|
blurredPixels[x, y].SetChannel(2, Convert.ToUInt16(bTotal / pixelCount));
|
||||||
|
}
|
||||||
|
|
||||||
|
return blurredImage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue