From c4e8bb2827091e50824d360589579478df7ed5aa Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 29 Jul 2016 10:35:10 +1000 Subject: [PATCH] Greyscale Former-commit-id: ccc16cc88844e4cd046130b7f9366937ae10d48a Former-commit-id: 0859791c265f40f2a1dc6e3d1e5c3e05a8726414 Former-commit-id: 6fe4767553f7f3776e7116f9c0b1eb1c63be0eae --- src/ImageProcessorCore/Filters/Greyscale.cs | 63 +++++++++++++++++++ .../Processors/Filters/GreyscaleTest.cs | 48 ++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/ImageProcessorCore/Filters/Greyscale.cs create mode 100644 tests/ImageProcessorCore.Tests/Processors/Filters/GreyscaleTest.cs diff --git a/src/ImageProcessorCore/Filters/Greyscale.cs b/src/ImageProcessorCore/Filters/Greyscale.cs new file mode 100644 index 000000000..90db30554 --- /dev/null +++ b/src/ImageProcessorCore/Filters/Greyscale.cs @@ -0,0 +1,63 @@ +// +// 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 greyscale toning to the image. + /// + /// The pixel format. + /// The packed format. long, float. + /// The image this method extends. + /// The formula to apply to perform the operation. + /// A delegate which is called as progress is made processing the image. + /// The . + public static Image Greyscale(this Image source, GreyscaleMode mode = GreyscaleMode.Bt709, ProgressEventHandler progressHandler = null) + where T : IPackedVector + where TP : struct + { + return Greyscale(source, source.Bounds, mode, progressHandler); + } + + /// + /// Applies greyscale 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. + /// + /// The formula to apply to perform the operation. + /// A delegate which is called as progress is made processing the image. + /// The . + public static Image Greyscale(this Image source, Rectangle rectangle, GreyscaleMode mode = GreyscaleMode.Bt709, ProgressEventHandler progressHandler = null) + where T : IPackedVector + where TP : struct + { + IImageProcessor processor = mode == GreyscaleMode.Bt709 + ? (IImageProcessor)new GreyscaleBt709Processor() + : new GreyscaleBt601Processor(); + + processor.OnProgress += progressHandler; + + try + { + return source.Process(rectangle, processor); + } + finally + { + processor.OnProgress -= progressHandler; + } + } + } +} diff --git a/tests/ImageProcessorCore.Tests/Processors/Filters/GreyscaleTest.cs b/tests/ImageProcessorCore.Tests/Processors/Filters/GreyscaleTest.cs new file mode 100644 index 000000000..427cc346a --- /dev/null +++ b/tests/ImageProcessorCore.Tests/Processors/Filters/GreyscaleTest.cs @@ -0,0 +1,48 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore.Tests +{ + using Processors; + using System.IO; + + using Xunit; + + public class GreyscaleTest : FileTestBase + { + public static readonly TheoryData GreyscaleValues + = new TheoryData + { + GreyscaleMode.Bt709 , + GreyscaleMode.Bt601 , + }; + + [Theory] + [MemberData("GreyscaleValues")] + public void ImageShouldApplyGreyscaleFilter(GreyscaleMode value) + { + const string path = "TestOutput/Greyscale"; + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + foreach (string file in Files) + { + using (FileStream stream = File.OpenRead(file)) + { + string filename = Path.GetFileNameWithoutExtension(file) + "-" + value + Path.GetExtension(file); + + Image image = new Image(stream); + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.Greyscale(value) + .Save(output); + } + } + } + } + } +} \ No newline at end of file