// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Applies a Gaussian blur to the image.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using ImageProcessor.Core.Common.Exceptions;
using ImageProcessor.Imaging;
///
/// Applies a Gaussian blur to the image.
///
public class GaussianBlur : IGraphicsProcessor
{
///
/// Initializes a new instance of the class.
///
public GaussianBlur()
{
this.Settings = new Dictionary();
}
///
/// Gets or sets the DynamicParameter.
///
public dynamic DynamicParameter { get; set; }
///
/// Gets or sets any additional settings required by the processor.
///
public Dictionary Settings { get; set; }
///
/// Processes the image.
///
/// The current instance of the class containing
/// the image to process.
///
/// The processed image from the current instance of the class.
///
public Image ProcessImage(ImageFactory factory)
{
Bitmap newImage = null;
Bitmap image = (Bitmap)factory.Image;
try
{
newImage = new Bitmap(image);
GaussianLayer gaussianLayer = this.DynamicParameter;
Convolution convolution = new Convolution(gaussianLayer.Sigma) { Threshold = gaussianLayer.Threshold };
double[,] kernel = convolution.CreateGuassianBlurFilter(gaussianLayer.Size);
newImage = convolution.ProcessKernel(newImage, kernel);
image.Dispose();
image = newImage;
}
catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
finally
{
if (newImage != null)
{
newImage.Dispose();
}
}
return image;
}
}
}