From 4540b426521d024a7e87a1e84d23c66ae7ca6eff Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 28 Jul 2016 13:45:19 +1000 Subject: [PATCH] BlackWhite Former-commit-id: 08361487c9988e874329dee40c21725ae6cc3a90 Former-commit-id: da89003e33f7ab41ee727e787c39d898af0b1e8d Former-commit-id: 7e90879b76b3a162d664e2c5bb0cd782e6dd6e77 --- src/ImageProcessorCore/Filters/BlackWhite.cs | 58 +++++++++++++++++++ .../Processors/Filters/BlackWhiteTest.cs | 38 ++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/ImageProcessorCore/Filters/BlackWhite.cs create mode 100644 tests/ImageProcessorCore.Tests/Processors/Filters/BlackWhiteTest.cs diff --git a/src/ImageProcessorCore/Filters/BlackWhite.cs b/src/ImageProcessorCore/Filters/BlackWhite.cs new file mode 100644 index 000000000..8799cfe64 --- /dev/null +++ b/src/ImageProcessorCore/Filters/BlackWhite.cs @@ -0,0 +1,58 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore +{ + using Processors; + + /// + /// Extension methods for the type. + /// + public static partial class ImageExtensions + { + /// + /// Applies black and white toning to the image. + /// + /// The pixel format. + /// The packed format. long, float. + /// The image this method extends. + /// A delegate which is called as progress is made processing the image. + /// The . + public static Image BlackWhite(this Image source, ProgressEventHandler progressHandler = null) + where T : IPackedVector + where TP : struct + { + return BlackWhite(source, source.Bounds, progressHandler); + } + + /// + /// Applies black and white toning to the image. + /// + /// The pixel format. + /// The packed format. long, float. + /// The image this method extends. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// A delegate which is called as progress is made processing the image. + /// The . + public static Image BlackWhite(this Image source, Rectangle rectangle, ProgressEventHandler progressHandler = null) + where T : IPackedVector + where TP : struct + { + BlackWhiteProcessor processor = new BlackWhiteProcessor(); + processor.OnProgress += progressHandler; + + try + { + return source.Process(rectangle, processor); + } + finally + { + processor.OnProgress -= progressHandler; + } + } + } +} diff --git a/tests/ImageProcessorCore.Tests/Processors/Filters/BlackWhiteTest.cs b/tests/ImageProcessorCore.Tests/Processors/Filters/BlackWhiteTest.cs new file mode 100644 index 000000000..c40224ef7 --- /dev/null +++ b/tests/ImageProcessorCore.Tests/Processors/Filters/BlackWhiteTest.cs @@ -0,0 +1,38 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore.Tests +{ + using System.IO; + + using Xunit; + + public class BlackWhiteTest : FileTestBase + { + [Fact] + public void ImageShouldApplyBlackWhiteFilter() + { + const string path = "TestOutput/BlackWhite"; + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + foreach (string file in Files) + { + using (FileStream stream = File.OpenRead(file)) + { + string filename = Path.GetFileName(file); + Image image = new Image(stream); + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.BlackWhite() + .Save(output); + } + } + } + } + } +} \ No newline at end of file