mirror of https://github.com/ivanch/tcc.git
77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
using ImageMagick;
|
|
|
|
namespace TCC.Services
|
|
{
|
|
public class ImageService
|
|
{
|
|
public ImageService() { }
|
|
|
|
public MagickImage BoxBlurImage(Stream imageStream, int radius)
|
|
{
|
|
var image = new MagickImage(imageStream);
|
|
image.GaussianBlur(radius, radius);
|
|
return image;
|
|
//var blurredImage = new MagickImage(image);
|
|
|
|
//blurredImage = BoxBlurImageSeparable(image, blurredImage, radius, 0);
|
|
//blurredImage = BoxBlurImageSeparable(blurredImage, blurredImage, 0, radius);
|
|
//return blurredImage;
|
|
}
|
|
|
|
private MagickImage BoxBlurImageSeparable(MagickImage image, MagickImage blurredImage, int radiusX, int radiusY)
|
|
{
|
|
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 = -radiusY; offsetY <= radiusY; offsetY++)
|
|
{
|
|
for (int offsetX = -radiusX; offsetX <= radiusX; 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;
|
|
}
|
|
|
|
public void SaveImage(Stream fileStream)
|
|
{
|
|
var file = File.Create("image.png");
|
|
fileStream.CopyToAsync(file);
|
|
file.Close();
|
|
}
|
|
|
|
public byte[] GetSimpleImage()
|
|
{
|
|
return File.ReadAllBytes("static/small-image.png");
|
|
}
|
|
|
|
public byte[] GetBigImage()
|
|
{
|
|
return File.ReadAllBytes("static/big-image.png");
|
|
}
|
|
}
|
|
}
|