diff --git a/src/ImageSharp/Filters/Effects/Pixelate.cs b/src/ImageSharp/Filters/Effects/Pixelate.cs index e69de29bb2..84afb00cb4 100644 --- a/src/ImageSharp/Filters/Effects/Pixelate.cs +++ b/src/ImageSharp/Filters/Effects/Pixelate.cs @@ -0,0 +1,55 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System; + + using Processors; + + /// + /// Extension methods for the type. + /// + public static partial class ImageExtensions + { + /// + /// Pixelates an image with the given pixel size. + /// + /// The pixel format. + /// The packed format. uint, long, float. + /// The image this method extends. + /// The size of the pixels. + /// The . + public static Image Pixelate(this Image source, int size = 4) + where TColor : struct, IPackedPixel + where TPacked : struct + { + return Pixelate(source, size, source.Bounds); + } + + /// + /// Pixelates an image with the given pixel size. + /// + /// The pixel format. + /// The packed format. uint, long, float. + /// The image this method extends. + /// The size of the pixels. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// The . + public static Image Pixelate(this Image source, int size, Rectangle rectangle) + where TColor : struct, IPackedPixel + where TPacked : struct + { + if (size <= 0 || size > source.Height || size > source.Width) + { + throw new ArgumentOutOfRangeException(nameof(size)); + } + + return source.Process(rectangle, new PixelateProcessor(size)); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processors/Filters/OilPaintTest.cs b/tests/ImageSharp.Tests/Processors/Filters/OilPaintTest.cs index e69de29bb2..bb607b839a 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/OilPaintTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/OilPaintTest.cs @@ -0,0 +1,60 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System; + using System.IO; + + using Xunit; + + public class OilPaintTest : FileTestBase + { + public static readonly TheoryData> OilPaintValues + = new TheoryData> + { + new Tuple(15,10), + new Tuple(6,5) + }; + + [Theory] + [MemberData(nameof(OilPaintValues))] + public void ImageShouldApplyOilPaintFilter(Tuple value) + { + string path = CreateOutputDirectory("OilPaint"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(value); + Image image = file.CreateImage(); + + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.OilPaint(value.Item1, value.Item2) + .Save(output); + } + } + } + + [Theory] + [MemberData(nameof(OilPaintValues))] + public void ImageShouldApplyOilPaintFilterInBox(Tuple value) + { + string path = CreateOutputDirectory("OilPaint"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(value + "-InBox"); + Image image = file.CreateImage(); + + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.OilPaint(value.Item1, value.Item2, new Rectangle(10, 10, image.Width / 2, image.Height / 2)) + .Save(output); + } + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processors/Filters/PixelateTest.cs b/tests/ImageSharp.Tests/Processors/Filters/PixelateTest.cs index e69de29bb2..38ec406ac5 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/PixelateTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/PixelateTest.cs @@ -0,0 +1,59 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests +{ + using System.IO; + + using Xunit; + + public class PixelateTest : FileTestBase + { + public static readonly TheoryData PixelateValues + = new TheoryData + { + 4 , + 8 + }; + + [Theory] + [MemberData(nameof(PixelateValues))] + public void ImageShouldApplyPixelateFilter(int value) + { + string path = CreateOutputDirectory("Pixelate"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(value); + Image image = file.CreateImage(); + + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Pixelate(value) + .Save(output); + } + } + } + + [Theory] + [MemberData(nameof(PixelateValues))] + public void ImageShouldApplyPixelateFilterInBox(int value) + { + string path = CreateOutputDirectory("Pixelate"); + + foreach (TestFile file in Files) + { + string filename = file.GetFileName(value + "-InBox"); + Image image = file.CreateImage(); + + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Pixelate(value, new Rectangle(10, 10, image.Width / 2, image.Height / 2)) + .Save(output); + } + } + } + } +} \ No newline at end of file