2023-08-16 18:58:01 +00:00
|
|
|
|
using ImageMagick;
|
2023-08-20 20:21:58 +00:00
|
|
|
|
using tcc_app;
|
2023-08-16 18:58:01 +00:00
|
|
|
|
|
|
|
|
|
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);
|
2023-08-20 20:21:58 +00:00
|
|
|
|
|
|
|
|
|
blurredImage = BoxBlurImageSeparable(image, blurredImage, radius, 0);
|
|
|
|
|
blurredImage = BoxBlurImageSeparable(image, blurredImage, 0, radius);
|
|
|
|
|
return blurredImage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MagickImage BoxBlurImageSeparable(MagickImage image, MagickImage blurredImage, int radiusX, int radiusY)
|
|
|
|
|
{
|
2023-08-16 18:58:01 +00:00
|
|
|
|
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;
|
2023-08-20 20:21:58 +00:00
|
|
|
|
for (int offsetY = -radiusY; offsetY <= radiusY; offsetY++)
|
2023-08-16 18:58:01 +00:00
|
|
|
|
{
|
2023-08-20 20:21:58 +00:00
|
|
|
|
for (int offsetX = -radiusX; offsetX <= radiusX; offsetX++)
|
2023-08-16 18:58:01 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2023-08-20 20:21:58 +00:00
|
|
|
|
|
|
|
|
|
public void SaveImage(Stream fileStream)
|
|
|
|
|
{
|
|
|
|
|
var file = File.Create("image.png");
|
|
|
|
|
fileStream.CopyToAsync(file);
|
|
|
|
|
file.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 19:20:40 +00:00
|
|
|
|
public byte[] GetSimpleImage()
|
2023-08-20 20:21:58 +00:00
|
|
|
|
{
|
|
|
|
|
return ImageHelper.SimpleImage;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 19:20:40 +00:00
|
|
|
|
public byte[] GetBigImage()
|
2023-08-20 20:21:58 +00:00
|
|
|
|
{
|
|
|
|
|
return ImageHelper.BigImage;
|
|
|
|
|
}
|
2023-08-16 18:58:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|