ShareX/ShareX.ImageEffectsLib/Filters/GaussianBlur.cs

87 lines
2.4 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2022-01-12 05:32:17 +13:00
Copyright (c) 2007-2022 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
2018-12-12 20:28:34 +13:00
using System;
using System.ComponentModel;
using System.Drawing;
2014-12-11 09:25:20 +13:00
namespace ShareX.ImageEffectsLib
{
[Description("Gaussian blur")]
internal class GaussianBlur : ImageEffect
{
private double sigma;
private int size;
2018-12-12 20:28:34 +13:00
[DefaultValue(0.7955555)]
public double Sigma
{
get => sigma;
2018-12-13 12:38:16 +13:00
set => sigma = Math.Max(value, 0.1);
2018-12-12 20:28:34 +13:00
}
[DefaultValue(3)]
public int Size
{
get => size;
2018-12-12 20:28:34 +13:00
set
{
2019-06-13 00:29:16 +12:00
size = value.Max(1);
2018-12-12 20:28:34 +13:00
if (size.IsEvenNumber())
2018-12-12 20:28:34 +13:00
{
size++;
2018-12-12 20:28:34 +13:00
}
}
}
public GaussianBlur()
{
this.ApplyDefaultPropertyValues();
}
2020-03-22 10:41:08 +13:00
public override Bitmap Apply(Bitmap bmp)
2018-12-12 20:28:34 +13:00
{
2018-12-13 12:38:16 +13:00
ConvolutionMatrix kernelHoriz = ConvolutionMatrixManager.GaussianBlur(1, size, sigma);
2018-12-12 20:28:34 +13:00
ConvolutionMatrix kernelVert = new ConvolutionMatrix(size, 1)
{
ConsiderAlpha = kernelHoriz.ConsiderAlpha
};
for (int i = 0; i < size; i++)
2018-12-12 20:28:34 +13:00
{
2018-12-13 12:38:16 +13:00
kernelVert[i, 0] = kernelHoriz[0, i];
2018-12-12 20:28:34 +13:00
}
2020-03-22 10:41:08 +13:00
using (bmp)
using (Bitmap horizPass = kernelHoriz.Apply(bmp))
{
2018-12-13 12:38:16 +13:00
return kernelVert.Apply(horizPass);
}
}
}
}