Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using Catel.MVVM;
- namespace ConvolutionWpf.Commands
- {
- public class BlurCommand : Command
- {
- private readonly Func<WriteableBitmap> _imageFactory;
- public BlurCommand(Func<WriteableBitmap> imageFactory)
- : base(() => { })
- {
- _imageFactory = imageFactory;
- }
- public void ExecuteCommand()
- {
- var image = _imageFactory();
- if (image == null)
- return;
- var pixels = new byte[image.PixelHeight * image.BackBufferStride];
- image.CopyPixels(pixels, image.BackBufferStride, 0);
- var resultPixels = new byte[image.PixelHeight * image.BackBufferStride];
- double[] kernel = { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 };
- var blurPixels = new byte[resultPixels.Length / kernel.Length];
- for (int i = 0; i < image.PixelWidth - 1; ++i)
- {
- for (int j = 0; j < image.PixelHeight - 1; ++j)
- {
- int index = j * image.BackBufferStride + 4 * i;
- for (int c = 0; c < 4; ++c)
- {
- resultPixels[index + c] = pixels[index + c];
- }
- }
- }
- for (int i = 0; i < resultPixels.Length; i += 9)
- {
- double[] sum = new double[kernel.Length];
- for (int c = 0; c < kernel.Length; c++)
- {
- sum[c] = resultPixels[i + c] * kernel[c];
- }
- var result = sum.Sum();
- var index = 0;
- for (; index < blurPixels.Length; index++)
- {
- blurPixels[index] = (byte)result;
- }
- }
- image.WritePixels(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight), blurPixels, image.BackBufferStride, 0);
- }
- protected override void Execute(object parameter, bool ignoreCanExecuteCheck)
- {
- ExecuteCommand();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement