mirror of
https://github.com/ivanch/tcc.git
synced 2025-08-25 23:31:49 +00:00
mini refactor
This commit is contained in:
49
Services/ImageService.cs
Normal file
49
Services/ImageService.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user