From f9e7fd1f08c93b8da99fea504aed535d3afd1cdf Mon Sep 17 00:00:00 2001 From: James South Date: Mon, 29 Sep 2014 17:41:53 +0100 Subject: [PATCH] Refactoring filters to not require ImageFactory Former-commit-id: 79da6e8a53cc8f4b79007db15218ebf77a942050 --- .../ImageFactoryUnitTests.cs | 47 ++++--- .../RegularExpressionUnitTests.cs | 2 +- src/ImageProcessor.Web/Processors/Filter.cs | 2 +- src/ImageProcessor.sln | 4 +- src/ImageProcessor/ImageFactory.cs | 4 +- src/ImageProcessor/ImageProcessor.csproj | 48 +++---- .../Binarization/BinaryThreshold.cs | 3 +- .../EdgeDetection/ConvolutionFilter.cs | 9 +- .../EdgeDetection/IEdgeFilter.cs | 2 +- .../EdgeDetection/KayyaliEdgeFilter.cs | 2 +- .../EdgeDetection/KirschEdgeFilter.cs | 2 +- .../EdgeDetection/PrewittEdgeFilter.cs | 2 +- .../EdgeDetection/RobertsCrossEdgeFilter.cs | 2 +- .../EdgeDetection/ScharrEdgeFilter.cs | 2 +- .../EdgeDetection/SobelEdgeFilter.cs | 2 +- .../{ => Photo}/BlackWhiteMatrixFilter.cs | 12 +- .../Filters/{ => Photo}/ColorMatrixes.cs | 2 +- .../Filters/{ => Photo}/ComicMatrixFilter.cs | 10 +- .../Filters/{ => Photo}/GothamMatrixFilter.cs | 24 +--- .../{ => Photo}/GreyScaleMatrixFilter.cs | 12 +- .../{ => Photo}/HiSatchMatrixFilter.cs | 12 +- .../Filters/{ => Photo}/IMatrixFilter.cs | 12 +- .../Filters/{ => Photo}/InvertMatrixFilter.cs | 12 +- .../{ => Photo}/LoSatchMatrixFilter.cs | 12 +- .../{ => Photo}/LomographMatrixFilter.cs | 19 +-- .../Filters/{ => Photo}/MatrixFilterBase.cs | 11 +- .../Filters/{ => Photo}/MatrixFilters.cs | 2 +- .../Filters/Photo/PolaroidMatrixFilter.cs | 69 ++++++++++ .../Filters/{ => Photo}/SepiaMatrixFilter.cs | 12 +- .../Imaging/Filters/PolaroidMatrixFilter.cs | 99 -------------- .../Imaging/Helpers/Adjustments.cs | 126 ++++++++++++++++++ src/ImageProcessor/Imaging/Helpers/Effects.cs | 112 ++++++++++++++++ src/ImageProcessor/Processors/Brightness.cs | 38 +----- src/ImageProcessor/Processors/Contrast.cs | 36 +---- src/ImageProcessor/Processors/DetectEdges.cs | 2 +- src/ImageProcessor/Processors/EntropyCrop.cs | 23 +++- src/ImageProcessor/Processors/Filter.cs | 4 +- src/ImageProcessor/Processors/Vignette.cs | 53 +------- ...sproj => ImageProcessor.Playground.csproj} | 4 +- src/ImageProcessorConsole/Program.cs | 13 +- .../images/input/new-york.jpg.REMOVED.git-id | 1 + .../output/monster-whitebg.png.REMOVED.git-id | 1 - 42 files changed, 465 insertions(+), 401 deletions(-) rename src/ImageProcessor/Imaging/{ => Filters}/Binarization/BinaryThreshold.cs (96%) rename src/ImageProcessor/Imaging/{ => Filters}/EdgeDetection/ConvolutionFilter.cs (95%) rename src/ImageProcessor/Imaging/{ => Filters}/EdgeDetection/IEdgeFilter.cs (94%) rename src/ImageProcessor/Imaging/{ => Filters}/EdgeDetection/KayyaliEdgeFilter.cs (96%) rename src/ImageProcessor/Imaging/{ => Filters}/EdgeDetection/KirschEdgeFilter.cs (96%) rename src/ImageProcessor/Imaging/{ => Filters}/EdgeDetection/PrewittEdgeFilter.cs (96%) rename src/ImageProcessor/Imaging/{ => Filters}/EdgeDetection/RobertsCrossEdgeFilter.cs (96%) rename src/ImageProcessor/Imaging/{ => Filters}/EdgeDetection/ScharrEdgeFilter.cs (96%) rename src/ImageProcessor/Imaging/{ => Filters}/EdgeDetection/SobelEdgeFilter.cs (96%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/BlackWhiteMatrixFilter.cs (80%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/ColorMatrixes.cs (99%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/ComicMatrixFilter.cs (97%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/GothamMatrixFilter.cs (78%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/GreyScaleMatrixFilter.cs (80%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/HiSatchMatrixFilter.cs (80%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/IMatrixFilter.cs (76%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/InvertMatrixFilter.cs (79%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/LoSatchMatrixFilter.cs (80%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/LomographMatrixFilter.cs (75%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/MatrixFilterBase.cs (82%) rename src/ImageProcessor/Imaging/Filters/{ => Photo}/MatrixFilters.cs (98%) create mode 100644 src/ImageProcessor/Imaging/Filters/Photo/PolaroidMatrixFilter.cs rename src/ImageProcessor/Imaging/Filters/{ => Photo}/SepiaMatrixFilter.cs (79%) delete mode 100644 src/ImageProcessor/Imaging/Filters/PolaroidMatrixFilter.cs create mode 100644 src/ImageProcessor/Imaging/Helpers/Adjustments.cs create mode 100644 src/ImageProcessor/Imaging/Helpers/Effects.cs rename src/ImageProcessorConsole/{ImageProcessorConsole.csproj => ImageProcessor.Playground.csproj} (96%) create mode 100644 src/ImageProcessorConsole/images/input/new-york.jpg.REMOVED.git-id delete mode 100644 src/ImageProcessorConsole/images/output/monster-whitebg.png.REMOVED.git-id diff --git a/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs b/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs index a408ead68..dd7d00563 100644 --- a/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs +++ b/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs @@ -3,11 +3,7 @@ // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // -// -// Unit tests for the ImageFactory (loading of images) -// // -------------------------------------------------------------------------------------------------------------------- - namespace ImageProcessor.UnitTests { using System; @@ -17,6 +13,7 @@ namespace ImageProcessor.UnitTests using System.Linq; using ImageProcessor.Imaging; + using ImageProcessor.Imaging.Filters.Photo; using NUnit.Framework; @@ -249,9 +246,9 @@ namespace ImageProcessor.UnitTests Image original = (Image)imageFactory.Image.Clone(); imageFactory.Watermark(new TextLayer { - FontFamily = new FontFamily("Arial"), - FontSize = 10, - Position = new Point(10, 10), + FontFamily = new FontFamily("Arial"), + FontSize = 10, + Position = new Point(10, 10), Text = "Lorem ipsum dolor" }); Assert.AreNotEqual(original, imageFactory.Image); @@ -344,43 +341,43 @@ namespace ImageProcessor.UnitTests imageFactory.Load(file.FullName); Image original = (Image)imageFactory.Image.Clone(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.BlackWhite); + imageFactory.Filter(MatrixFilters.BlackWhite); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.Comic); + imageFactory.Filter(MatrixFilters.Comic); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.Gotham); + imageFactory.Filter(MatrixFilters.Gotham); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.GreyScale); + imageFactory.Filter(MatrixFilters.GreyScale); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.HiSatch); + imageFactory.Filter(MatrixFilters.HiSatch); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.Invert); + imageFactory.Filter(MatrixFilters.Invert); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.Lomograph); + imageFactory.Filter(MatrixFilters.Lomograph); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.LoSatch); + imageFactory.Filter(MatrixFilters.LoSatch); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.Polaroid); + imageFactory.Filter(MatrixFilters.Polaroid); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); - imageFactory.Filter(ImageProcessor.Imaging.Filters.MatrixFilters.Sepia); + imageFactory.Filter(MatrixFilters.Sepia); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); } @@ -552,10 +549,18 @@ namespace ImageProcessor.UnitTests /// /// Gets the files matching the given extensions. /// - /// The . - /// The extensions. - /// A collection of - /// The extensions variable is null. + /// + /// The . + /// + /// + /// The extensions. + /// + /// + /// A collection of + /// + /// + /// The extensions variable is null. + /// private static IEnumerable GetFilesByExtensions(DirectoryInfo dir, params string[] extensions) { if (extensions == null) diff --git a/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs b/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs index 2f66b1057..2ad22302a 100644 --- a/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs +++ b/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs @@ -14,7 +14,7 @@ namespace ImageProcessor.Web.UnitTests using System.Collections.Generic; using System.Drawing; using ImageProcessor.Imaging; - using ImageProcessor.Imaging.Filters; + using ImageProcessor.Imaging.Filters.Photo; using ImageProcessor.Imaging.Formats; using ImageProcessor.Plugins.WebP.Imaging.Formats; diff --git a/src/ImageProcessor.Web/Processors/Filter.cs b/src/ImageProcessor.Web/Processors/Filter.cs index ab9353c78..533bf2e1d 100644 --- a/src/ImageProcessor.Web/Processors/Filter.cs +++ b/src/ImageProcessor.Web/Processors/Filter.cs @@ -16,7 +16,7 @@ namespace ImageProcessor.Web.Processors using System.Reflection; using System.Text; using System.Text.RegularExpressions; - using ImageProcessor.Imaging.Filters; + using ImageProcessor.Imaging.Filters.Photo; using ImageProcessor.Processors; /// diff --git a/src/ImageProcessor.sln b/src/ImageProcessor.sln index 0909ba654..52fa46752 100644 --- a/src/ImageProcessor.sln +++ b/src/ImageProcessor.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2013 -VisualStudioVersion = 12.0.30501.0 +VisualStudioVersion = 12.0.30723.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C427A497-74DC-49B1-8420-D6E68354F29B}" ProjectSection(SolutionItems) = preProject @@ -22,7 +22,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageProcessor.Web", "Image EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageProcessor.Plugins.WebP", "Plugins\ImageProcessor\ImageProcessor.Plugins.WebP\ImageProcessor.Plugins.WebP.csproj", "{2CF69699-959A-44DC-A281-4E2596C25043}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageProcessorConsole", "ImageProcessorConsole\ImageProcessorConsole.csproj", "{7BF5274B-56A7-4B62-8105-E9BDF25BAFE7}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageProcessor.Playground", "ImageProcessorConsole\ImageProcessor.Playground.csproj", "{7BF5274B-56A7-4B62-8105-E9BDF25BAFE7}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageProcessor.Web.UnitTests", "ImageProcessor.Web.UnitTests\ImageProcessor.Web.UnitTests.csproj", "{961340C8-8C93-401D-A0A2-FF9EC61E5260}" EndProject diff --git a/src/ImageProcessor/ImageFactory.cs b/src/ImageProcessor/ImageFactory.cs index 5c9e0efa7..08fb876b4 100644 --- a/src/ImageProcessor/ImageFactory.cs +++ b/src/ImageProcessor/ImageFactory.cs @@ -20,8 +20,8 @@ namespace ImageProcessor using ImageProcessor.Common.Exceptions; using ImageProcessor.Imaging; - using ImageProcessor.Imaging.EdgeDetection; - using ImageProcessor.Imaging.Filters; + using ImageProcessor.Imaging.Filters.EdgeDetection; + using ImageProcessor.Imaging.Filters.Photo; using ImageProcessor.Imaging.Formats; using ImageProcessor.Processors; #endregion diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index 7fb1cd9e2..ee7f45267 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -76,14 +76,14 @@ - - - - - - - - + + + + + + + + Code @@ -97,8 +97,8 @@ - - + + @@ -110,21 +110,23 @@ + + - - - - - - - - - - - - + + + + + + + + + + + + @@ -133,7 +135,7 @@ - + diff --git a/src/ImageProcessor/Imaging/Binarization/BinaryThreshold.cs b/src/ImageProcessor/Imaging/Filters/Binarization/BinaryThreshold.cs similarity index 96% rename from src/ImageProcessor/Imaging/Binarization/BinaryThreshold.cs rename to src/ImageProcessor/Imaging/Filters/Binarization/BinaryThreshold.cs index f0b1271b7..b420a6588 100644 --- a/src/ImageProcessor/Imaging/Binarization/BinaryThreshold.cs +++ b/src/ImageProcessor/Imaging/Filters/Binarization/BinaryThreshold.cs @@ -8,10 +8,9 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Binarization +namespace ImageProcessor.Imaging.Filters.Binarization { using System.Drawing; - using System.Drawing.Imaging; /// /// Performs binary threshold filtering against a given greyscale image. diff --git a/src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs b/src/ImageProcessor/Imaging/Filters/EdgeDetection/ConvolutionFilter.cs similarity index 95% rename from src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs rename to src/ImageProcessor/Imaging/Filters/EdgeDetection/ConvolutionFilter.cs index 97d5b8542..9d6681ffc 100644 --- a/src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/EdgeDetection/ConvolutionFilter.cs @@ -8,14 +8,14 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.EdgeDetection +namespace ImageProcessor.Imaging.Filters.EdgeDetection { using System; using System.Drawing; using System.Drawing.Imaging; using ImageProcessor.Common.Extensions; - using ImageProcessor.Imaging.Filters; + using ImageProcessor.Imaging.Filters.Photo; /// /// The convolution filter for applying gradient operators to detect edges within an image. @@ -65,7 +65,7 @@ namespace ImageProcessor.Imaging.EdgeDetection Rectangle rectangle = new Rectangle(0, 0, width, height); if (this.greyscale) { - // If it's greyscale apply a colormatix to the image. + // If it's greyscale apply a colormatrix to the image. using (ImageAttributes attributes = new ImageAttributes()) { attributes.SetColorMatrix(ColorMatrixes.GreyScale); @@ -170,12 +170,13 @@ namespace ImageProcessor.Imaging.EdgeDetection input.Dispose(); } + // Draw a black rectangle around the area to ensure that the first row/column in covered. using (Graphics graphics = Graphics.FromImage(destination)) { // Draw an edge around the image. using (Pen blackPen = new Pen(Color.Black)) { - blackPen.Width = 4; + blackPen.Width = 1; graphics.DrawRectangle(blackPen, new Rectangle(0, 0, destination.Width, destination.Height)); } } diff --git a/src/ImageProcessor/Imaging/EdgeDetection/IEdgeFilter.cs b/src/ImageProcessor/Imaging/Filters/EdgeDetection/IEdgeFilter.cs similarity index 94% rename from src/ImageProcessor/Imaging/EdgeDetection/IEdgeFilter.cs rename to src/ImageProcessor/Imaging/Filters/EdgeDetection/IEdgeFilter.cs index 4ddee1b46..18a3bc8cc 100644 --- a/src/ImageProcessor/Imaging/EdgeDetection/IEdgeFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/EdgeDetection/IEdgeFilter.cs @@ -8,7 +8,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.EdgeDetection +namespace ImageProcessor.Imaging.Filters.EdgeDetection { /// /// Describes properties for creating edge detection filters. diff --git a/src/ImageProcessor/Imaging/EdgeDetection/KayyaliEdgeFilter.cs b/src/ImageProcessor/Imaging/Filters/EdgeDetection/KayyaliEdgeFilter.cs similarity index 96% rename from src/ImageProcessor/Imaging/EdgeDetection/KayyaliEdgeFilter.cs rename to src/ImageProcessor/Imaging/Filters/EdgeDetection/KayyaliEdgeFilter.cs index cbedae2a6..f597b9f5e 100644 --- a/src/ImageProcessor/Imaging/EdgeDetection/KayyaliEdgeFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/EdgeDetection/KayyaliEdgeFilter.cs @@ -9,7 +9,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.EdgeDetection +namespace ImageProcessor.Imaging.Filters.EdgeDetection { /// /// The Kayyali operator filter. diff --git a/src/ImageProcessor/Imaging/EdgeDetection/KirschEdgeFilter.cs b/src/ImageProcessor/Imaging/Filters/EdgeDetection/KirschEdgeFilter.cs similarity index 96% rename from src/ImageProcessor/Imaging/EdgeDetection/KirschEdgeFilter.cs rename to src/ImageProcessor/Imaging/Filters/EdgeDetection/KirschEdgeFilter.cs index 3a57d13a4..9069870d8 100644 --- a/src/ImageProcessor/Imaging/EdgeDetection/KirschEdgeFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/EdgeDetection/KirschEdgeFilter.cs @@ -9,7 +9,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.EdgeDetection +namespace ImageProcessor.Imaging.Filters.EdgeDetection { /// /// The Kirsch operator filter. diff --git a/src/ImageProcessor/Imaging/EdgeDetection/PrewittEdgeFilter.cs b/src/ImageProcessor/Imaging/Filters/EdgeDetection/PrewittEdgeFilter.cs similarity index 96% rename from src/ImageProcessor/Imaging/EdgeDetection/PrewittEdgeFilter.cs rename to src/ImageProcessor/Imaging/Filters/EdgeDetection/PrewittEdgeFilter.cs index da6650208..d13f3ae40 100644 --- a/src/ImageProcessor/Imaging/EdgeDetection/PrewittEdgeFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/EdgeDetection/PrewittEdgeFilter.cs @@ -9,7 +9,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.EdgeDetection +namespace ImageProcessor.Imaging.Filters.EdgeDetection { /// /// The Prewitt operator filter. diff --git a/src/ImageProcessor/Imaging/EdgeDetection/RobertsCrossEdgeFilter.cs b/src/ImageProcessor/Imaging/Filters/EdgeDetection/RobertsCrossEdgeFilter.cs similarity index 96% rename from src/ImageProcessor/Imaging/EdgeDetection/RobertsCrossEdgeFilter.cs rename to src/ImageProcessor/Imaging/Filters/EdgeDetection/RobertsCrossEdgeFilter.cs index b05b5836d..db75c03ab 100644 --- a/src/ImageProcessor/Imaging/EdgeDetection/RobertsCrossEdgeFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/EdgeDetection/RobertsCrossEdgeFilter.cs @@ -9,7 +9,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.EdgeDetection +namespace ImageProcessor.Imaging.Filters.EdgeDetection { /// /// The Roberts Cross operator filter. diff --git a/src/ImageProcessor/Imaging/EdgeDetection/ScharrEdgeFilter.cs b/src/ImageProcessor/Imaging/Filters/EdgeDetection/ScharrEdgeFilter.cs similarity index 96% rename from src/ImageProcessor/Imaging/EdgeDetection/ScharrEdgeFilter.cs rename to src/ImageProcessor/Imaging/Filters/EdgeDetection/ScharrEdgeFilter.cs index 89645f54f..2d5915d36 100644 --- a/src/ImageProcessor/Imaging/EdgeDetection/ScharrEdgeFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/EdgeDetection/ScharrEdgeFilter.cs @@ -9,7 +9,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.EdgeDetection +namespace ImageProcessor.Imaging.Filters.EdgeDetection { /// /// The Scharr operator filter. diff --git a/src/ImageProcessor/Imaging/EdgeDetection/SobelEdgeFilter.cs b/src/ImageProcessor/Imaging/Filters/EdgeDetection/SobelEdgeFilter.cs similarity index 96% rename from src/ImageProcessor/Imaging/EdgeDetection/SobelEdgeFilter.cs rename to src/ImageProcessor/Imaging/Filters/EdgeDetection/SobelEdgeFilter.cs index 6fa59a8fb..bf116164d 100644 --- a/src/ImageProcessor/Imaging/EdgeDetection/SobelEdgeFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/EdgeDetection/SobelEdgeFilter.cs @@ -9,7 +9,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.EdgeDetection +namespace ImageProcessor.Imaging.Filters.EdgeDetection { /// /// The Sobel operator filter. diff --git a/src/ImageProcessor/Imaging/Filters/BlackWhiteMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/BlackWhiteMatrixFilter.cs similarity index 80% rename from src/ImageProcessor/Imaging/Filters/BlackWhiteMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/BlackWhiteMatrixFilter.cs index 47cd4c540..cf8d7f7a2 100644 --- a/src/ImageProcessor/Imaging/Filters/BlackWhiteMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/BlackWhiteMatrixFilter.cs @@ -8,12 +8,10 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { - #region Using using System.Drawing; using System.Drawing.Imaging; - #endregion /// /// Encapsulates methods with which to add a black and white filter to an image. @@ -31,16 +29,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) + public override Image TransformImage(Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { diff --git a/src/ImageProcessor/Imaging/Filters/ColorMatrixes.cs b/src/ImageProcessor/Imaging/Filters/Photo/ColorMatrixes.cs similarity index 99% rename from src/ImageProcessor/Imaging/Filters/ColorMatrixes.cs rename to src/ImageProcessor/Imaging/Filters/Photo/ColorMatrixes.cs index 4861d2481..b1ab14b1c 100644 --- a/src/ImageProcessor/Imaging/Filters/ColorMatrixes.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/ColorMatrixes.cs @@ -8,7 +8,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { using System.Drawing.Imaging; diff --git a/src/ImageProcessor/Imaging/Filters/ComicMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/ComicMatrixFilter.cs similarity index 97% rename from src/ImageProcessor/Imaging/Filters/ComicMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/ComicMatrixFilter.cs index 18895b4a0..6c63b49cb 100644 --- a/src/ImageProcessor/Imaging/Filters/ComicMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/ComicMatrixFilter.cs @@ -8,7 +8,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { #region Using using System; @@ -37,16 +37,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) + public override Image TransformImage(Image image, Image newImage) { // Bitmaps for comic pattern Bitmap highBitmap = null; diff --git a/src/ImageProcessor/Imaging/Filters/GothamMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/GothamMatrixFilter.cs similarity index 78% rename from src/ImageProcessor/Imaging/Filters/GothamMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/GothamMatrixFilter.cs index 7db4d3400..debe24e91 100644 --- a/src/ImageProcessor/Imaging/Filters/GothamMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/GothamMatrixFilter.cs @@ -8,16 +8,13 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { - #region Using using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; - using ImageProcessor.Processors; - - #endregion + using ImageProcessor.Imaging.Helpers; /// /// Encapsulates methods with which to add a gotham filter to an image. @@ -35,16 +32,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) + public override Image TransformImage(Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { @@ -79,13 +72,8 @@ namespace ImageProcessor.Imaging.Filters } // Add brightness and contrast to finish the effect. - factory.Image = newImage; - Brightness brightness = new Brightness { DynamicParameter = 5 }; - newImage = brightness.ProcessImage(factory); - - factory.Image = newImage; - Contrast contrast = new Contrast { DynamicParameter = 85 }; - newImage = contrast.ProcessImage(factory); + newImage = Adjustments.Brightness((Bitmap)newImage, 5); + newImage = Adjustments.Contrast((Bitmap)newImage, 85); // Reassign the image. image.Dispose(); diff --git a/src/ImageProcessor/Imaging/Filters/GreyScaleMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/GreyScaleMatrixFilter.cs similarity index 80% rename from src/ImageProcessor/Imaging/Filters/GreyScaleMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/GreyScaleMatrixFilter.cs index 11cee5b2f..db9b5cdfe 100644 --- a/src/ImageProcessor/Imaging/Filters/GreyScaleMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/GreyScaleMatrixFilter.cs @@ -8,12 +8,10 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { - #region Using using System.Drawing; using System.Drawing.Imaging; - #endregion /// /// Encapsulates methods with which to add a greyscale filter to an image. @@ -31,16 +29,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) + public override Image TransformImage(Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { diff --git a/src/ImageProcessor/Imaging/Filters/HiSatchMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/HiSatchMatrixFilter.cs similarity index 80% rename from src/ImageProcessor/Imaging/Filters/HiSatchMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/HiSatchMatrixFilter.cs index 035d43f73..ad1b621a2 100644 --- a/src/ImageProcessor/Imaging/Filters/HiSatchMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/HiSatchMatrixFilter.cs @@ -8,12 +8,10 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { - #region Using using System.Drawing; using System.Drawing.Imaging; - #endregion /// /// Encapsulates methods with which to add a high saturated filter to an image. @@ -31,16 +29,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) + public override Image TransformImage(Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { diff --git a/src/ImageProcessor/Imaging/Filters/IMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/IMatrixFilter.cs similarity index 76% rename from src/ImageProcessor/Imaging/Filters/IMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/IMatrixFilter.cs index 04d0977b4..32f370355 100644 --- a/src/ImageProcessor/Imaging/Filters/IMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/IMatrixFilter.cs @@ -8,12 +8,10 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { - #region Using using System.Drawing; using System.Drawing.Imaging; - #endregion /// /// Defines properties and methods for ColorMatrix based filters. @@ -25,20 +23,14 @@ namespace ImageProcessor.Imaging.Filters /// ColorMatrix Matrix { get; } - #region Methods /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// /// The processed image from the current instance of the class. /// - Image TransformImage(ImageFactory factory, Image image, Image newImage); - #endregion + Image TransformImage(Image image, Image newImage); } } diff --git a/src/ImageProcessor/Imaging/Filters/InvertMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/InvertMatrixFilter.cs similarity index 79% rename from src/ImageProcessor/Imaging/Filters/InvertMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/InvertMatrixFilter.cs index c36c607e5..08826159a 100644 --- a/src/ImageProcessor/Imaging/Filters/InvertMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/InvertMatrixFilter.cs @@ -8,12 +8,10 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { - #region Using using System.Drawing; using System.Drawing.Imaging; - #endregion /// /// Encapsulates methods with which to add an inverted filter to an image. @@ -31,16 +29,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) + public override Image TransformImage(Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { diff --git a/src/ImageProcessor/Imaging/Filters/LoSatchMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/LoSatchMatrixFilter.cs similarity index 80% rename from src/ImageProcessor/Imaging/Filters/LoSatchMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/LoSatchMatrixFilter.cs index 040aa9ef9..5d0a603de 100644 --- a/src/ImageProcessor/Imaging/Filters/LoSatchMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/LoSatchMatrixFilter.cs @@ -8,12 +8,10 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { - #region Using using System.Drawing; using System.Drawing.Imaging; - #endregion /// /// Encapsulates methods with which to add a low saturated filter to an image. @@ -31,16 +29,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) + public override Image TransformImage(Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { diff --git a/src/ImageProcessor/Imaging/Filters/LomographMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/LomographMatrixFilter.cs similarity index 75% rename from src/ImageProcessor/Imaging/Filters/LomographMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/LomographMatrixFilter.cs index 24d86c76f..1b26d2a70 100644 --- a/src/ImageProcessor/Imaging/Filters/LomographMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/LomographMatrixFilter.cs @@ -8,13 +8,12 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { - #region Using using System.Drawing; using System.Drawing.Imaging; - using ImageProcessor.Processors; - #endregion + + using ImageProcessor.Imaging.Helpers; /// /// Encapsulates methods with which to add a lomograph filter to an image. @@ -32,16 +31,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) + public override Image TransformImage(Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { @@ -55,9 +50,7 @@ namespace ImageProcessor.Imaging.Filters } // Add a vignette to finish the effect. - factory.Image = newImage; - Vignette vignette = new Vignette(); - newImage = vignette.ProcessImage(factory); + newImage = Effects.Vignette((Bitmap)newImage, Color.Black); // Reassign the image. image.Dispose(); diff --git a/src/ImageProcessor/Imaging/Filters/MatrixFilterBase.cs b/src/ImageProcessor/Imaging/Filters/Photo/MatrixFilterBase.cs similarity index 82% rename from src/ImageProcessor/Imaging/Filters/MatrixFilterBase.cs rename to src/ImageProcessor/Imaging/Filters/Photo/MatrixFilterBase.cs index bff9d621e..482dfd5e3 100644 --- a/src/ImageProcessor/Imaging/Filters/MatrixFilterBase.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/MatrixFilterBase.cs @@ -8,7 +8,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { using System.Drawing; using System.Drawing.Imaging; @@ -26,15 +26,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// The current instance of the - /// class containing - /// the image to process. /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public abstract Image TransformImage(ImageFactory factory, Image image, Image newImage); + public abstract Image TransformImage(Image image, Image newImage); /// /// Determines whether the specified , is equal to this instance. @@ -64,7 +61,7 @@ namespace ImageProcessor.Imaging.Filters /// public override int GetHashCode() { - return this.GetType().Name.GetHashCode() + this.Matrix.GetHashCode(); + return this.GetType().Name.GetHashCode() ^ this.Matrix.GetHashCode(); } } } \ No newline at end of file diff --git a/src/ImageProcessor/Imaging/Filters/MatrixFilters.cs b/src/ImageProcessor/Imaging/Filters/Photo/MatrixFilters.cs similarity index 98% rename from src/ImageProcessor/Imaging/Filters/MatrixFilters.cs rename to src/ImageProcessor/Imaging/Filters/Photo/MatrixFilters.cs index 99b7a705b..3acc45468 100644 --- a/src/ImageProcessor/Imaging/Filters/MatrixFilters.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/MatrixFilters.cs @@ -8,7 +8,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { using ImageProcessor.Processors; diff --git a/src/ImageProcessor/Imaging/Filters/Photo/PolaroidMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/PolaroidMatrixFilter.cs new file mode 100644 index 000000000..2a5cd617f --- /dev/null +++ b/src/ImageProcessor/Imaging/Filters/Photo/PolaroidMatrixFilter.cs @@ -0,0 +1,69 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Encapsulates methods with which to add a Polaroid filter to an image. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Imaging.Filters.Photo +{ + using System.Drawing; + using System.Drawing.Imaging; + + using ImageProcessor.Imaging.Helpers; + + /// + /// Encapsulates methods with which to add a Polaroid filter to an image. + /// + internal class PolaroidMatrixFilter : MatrixFilterBase + { + /// + /// Gets the for this filter instance. + /// + public override ColorMatrix Matrix + { + get { return ColorMatrixes.Polaroid; } + } + + /// + /// Processes the image. + /// + /// The current image to process + /// The new Image to return + /// + /// The processed image. + /// + public override Image TransformImage(Image image, Image newImage) + { + using (Graphics graphics = Graphics.FromImage(newImage)) + { + using (ImageAttributes attributes = new ImageAttributes()) + { + attributes.SetColorMatrix(this.Matrix); + + Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height); + + graphics.DrawImage(image, rectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); + } + } + + // Fade the contrast + newImage = Adjustments.Contrast((Bitmap)newImage, -30); + + // Add a glow to the image. + newImage = Effects.Glow((Bitmap)newImage, Color.FromArgb(70, 255, 153, 102)); + + // Add a vignette to finish the effect. + newImage = Effects.Vignette((Bitmap)newImage, Color.FromArgb(80, 0, 0)); + + // Reassign the image. + image.Dispose(); + image = newImage; + + return image; + } + } +} diff --git a/src/ImageProcessor/Imaging/Filters/SepiaMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/Photo/SepiaMatrixFilter.cs similarity index 79% rename from src/ImageProcessor/Imaging/Filters/SepiaMatrixFilter.cs rename to src/ImageProcessor/Imaging/Filters/Photo/SepiaMatrixFilter.cs index d2f263bb7..ad6f2a87b 100644 --- a/src/ImageProcessor/Imaging/Filters/SepiaMatrixFilter.cs +++ b/src/ImageProcessor/Imaging/Filters/Photo/SepiaMatrixFilter.cs @@ -8,12 +8,10 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessor.Imaging.Filters +namespace ImageProcessor.Imaging.Filters.Photo { - #region Using using System.Drawing; using System.Drawing.Imaging; - #endregion /// /// Encapsulates methods with which to add a sepia filter to an image. @@ -31,16 +29,12 @@ namespace ImageProcessor.Imaging.Filters /// /// Processes the image. /// - /// - /// The current instance of the class containing - /// the image to process. - /// /// The current image to process /// The new Image to return /// - /// The processed image from the current instance of the class. + /// The processed image. /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) + public override Image TransformImage(Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { diff --git a/src/ImageProcessor/Imaging/Filters/PolaroidMatrixFilter.cs b/src/ImageProcessor/Imaging/Filters/PolaroidMatrixFilter.cs deleted file mode 100644 index 39ccdceea..000000000 --- a/src/ImageProcessor/Imaging/Filters/PolaroidMatrixFilter.cs +++ /dev/null @@ -1,99 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James South. -// Licensed under the Apache License, Version 2.0. -// -// -// Encapsulates methods with which to add a Polaroid filter to an image. -// -// -------------------------------------------------------------------------------------------------------------------- - -namespace ImageProcessor.Imaging.Filters -{ - #region Using - using System.Drawing; - using System.Drawing.Drawing2D; - using System.Drawing.Imaging; - using ImageProcessor.Processors; - #endregion - - /// - /// Encapsulates methods with which to add a Polaroid filter to an image. - /// - internal class PolaroidMatrixFilter : MatrixFilterBase - { - /// - /// Gets the for this filter instance. - /// - public override ColorMatrix Matrix - { - get { return ColorMatrixes.Polaroid; } - } - - /// - /// Processes the image. - /// - /// - /// The current instance of the class containing - /// the image to process. - /// - /// The current image to process - /// The new Image to return - /// - /// The processed image from the current instance of the class. - /// - public override Image TransformImage(ImageFactory factory, Image image, Image newImage) - { - using (Graphics graphics = Graphics.FromImage(newImage)) - { - using (ImageAttributes attributes = new ImageAttributes()) - { - attributes.SetColorMatrix(this.Matrix); - - Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height); - - graphics.DrawImage(image, rectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); - - // Add a glow to the image. - using (GraphicsPath path = new GraphicsPath()) - { - path.AddEllipse(rectangle); - using (PathGradientBrush brush = new PathGradientBrush(path)) - { - // Fill a rectangle with an elliptical gradient brush that goes from orange to transparent. - // This has the effect of painting the far corners transparent and fading in to orange on the - // way in to the centre. - brush.WrapMode = WrapMode.Tile; - brush.CenterColor = Color.FromArgb(70, 255, 153, 102); - brush.SurroundColors = new[] { Color.FromArgb(0, 0, 0, 0) }; - - Blend blend = new Blend - { - Positions = new[] { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0F }, - Factors = new[] { 0.0f, 0.5f, 1f, 1f, 1.0f, 1.0f } - }; - - brush.Blend = blend; - - Region oldClip = graphics.Clip; - graphics.Clip = new Region(rectangle); - graphics.FillRectangle(brush, rectangle); - graphics.Clip = oldClip; - } - } - } - } - - // Add a vignette to finish the effect. - factory.Image = newImage; - Vignette vignette = new Vignette(); - newImage = vignette.ProcessImage(factory); - - // Reassign the image. - image.Dispose(); - image = newImage; - - return image; - } - } -} diff --git a/src/ImageProcessor/Imaging/Helpers/Adjustments.cs b/src/ImageProcessor/Imaging/Helpers/Adjustments.cs new file mode 100644 index 000000000..a1d5c53be --- /dev/null +++ b/src/ImageProcessor/Imaging/Helpers/Adjustments.cs @@ -0,0 +1,126 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Provides reusable adjustment methods to apply to images. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Imaging.Helpers +{ + using System; + using System.Drawing; + using System.Drawing.Imaging; + + /// + /// Provides reusable adjustment methods to apply to images. + /// + public static class Adjustments + { + /// + /// Adjusts the brightness component of the given image. + /// + /// + /// The source to adjust. + /// + /// + /// The threshold value between -100 and 100 for adjusting the brightness. + /// + /// The rectangle to define the bounds of the area to adjust the brightness. + /// If null then the effect is applied to the entire image. + /// + /// The with the brightness adjusted. + /// + /// + /// Thrown if the threshold value falls outside the acceptable range. + /// + public static Bitmap Brightness(Bitmap source, int threshold, Rectangle? rectangle = null) + { + if (threshold > 100 || threshold < -100) + { + throw new ArgumentOutOfRangeException("threshold", "Threshold should be between -100 and 100."); + } + + float brightnessFactor = (float)threshold / 100; + Rectangle bounds = rectangle.HasValue ? rectangle.Value : new Rectangle(0, 0, source.Width, source.Height); + + ColorMatrix colorMatrix = + new ColorMatrix( + new[] + { + new float[] { 1, 0, 0, 0, 0 }, + new float[] { 0, 1, 0, 0, 0 }, + new float[] { 0, 0, 1, 0, 0 }, + new float[] { 0, 0, 0, 1, 0 }, + new[] { brightnessFactor, brightnessFactor, brightnessFactor, 0, 1 } + }); + + using (Graphics graphics = Graphics.FromImage(source)) + { + using (ImageAttributes imageAttributes = new ImageAttributes()) + { + imageAttributes.SetColorMatrix(colorMatrix); + graphics.DrawImage(source, bounds, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, imageAttributes); + } + } + + return source; + } + + /// + /// Adjusts the contrast component of the given image. + /// + /// + /// The source to adjust. + /// + /// + /// The threshold value between -100 and 100 for adjusting the contrast. + /// + /// The rectangle to define the bounds of the area to adjust the contrast. + /// If null then the effect is applied to the entire image. + /// + /// The with the contrast adjusted. + /// + /// + /// Thrown if the threshold value falls outside the acceptable range. + /// + public static Bitmap Contrast(Bitmap source, int threshold, Rectangle? rectangle = null) + { + if (threshold > 100 || threshold < -100) + { + throw new ArgumentOutOfRangeException("threshold", "Threshold should be between -100 and 100."); + } + + Rectangle bounds = rectangle.HasValue ? rectangle.Value : new Rectangle(0, 0, source.Width, source.Height); + + float contrastFactor = (float)threshold / 100; + + // Stop at -1 to prevent inversion. + contrastFactor++; + float factorTransform = 0.5f * (1.0f - contrastFactor); + + ColorMatrix colorMatrix = new ColorMatrix( + new[] + { + new[] { contrastFactor, 0, 0, 0, 0 }, + new[] { 0, contrastFactor, 0, 0, 0 }, + new[] { 0, 0, contrastFactor, 0, 0 }, + new float[] { 0, 0, 0, 1, 0 }, + new[] { factorTransform, factorTransform, factorTransform, 0, 1 } + }); + + using (Graphics graphics = Graphics.FromImage(source)) + { + using (ImageAttributes imageAttributes = new ImageAttributes()) + { + imageAttributes.SetColorMatrix(colorMatrix); + graphics.DrawImage(source, bounds, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, imageAttributes); + } + } + + return source; + } + } +} diff --git a/src/ImageProcessor/Imaging/Helpers/Effects.cs b/src/ImageProcessor/Imaging/Helpers/Effects.cs new file mode 100644 index 000000000..1161caacb --- /dev/null +++ b/src/ImageProcessor/Imaging/Helpers/Effects.cs @@ -0,0 +1,112 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Provides reusable effect methods to apply to images. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Imaging.Helpers +{ + using System; + using System.Drawing; + using System.Drawing.Drawing2D; + + /// + /// Provides reusable effect methods to apply to images. + /// + public static class Effects + { + /// + /// Adds a vignette effect to the source image based on the given color. + /// + /// + /// The source. + /// + /// + /// to base the vignette on. + /// + /// + /// The rectangle to define the bounds of the area to vignette. If null then the effect is applied + /// to the entire image. + /// + /// + /// Whether to invert the vignette. + /// + /// + /// The with the vignette applied. + /// + public static Bitmap Vignette(Bitmap source, Color baseColor, Rectangle? rectangle = null, bool invert = false) + { + using (Graphics graphics = Graphics.FromImage(source)) + { + Rectangle bounds = rectangle.HasValue ? rectangle.Value : new Rectangle(0, 0, source.Width, source.Height); + Rectangle ellipsebounds = bounds; + + // Increase the rectangle size by the difference between the rectangle dimensions and sqrt(2)/2 * the rectangle dimensions. + // Why sqrt(2)/2? Because the point (sqrt(2)/2, sqrt(2)/2) is the 45 degree angle point on a unit circle. Scaling by the width + // and height gives the distance needed to inflate the rectangle to make sure it's fully covered. + ellipsebounds.Offset(-ellipsebounds.X, -ellipsebounds.Y); + int x = ellipsebounds.Width - (int)Math.Floor(.70712 * ellipsebounds.Width); + int y = ellipsebounds.Height - (int)Math.Floor(.70712 * ellipsebounds.Height); + ellipsebounds.Inflate(x, y); + + using (GraphicsPath path = new GraphicsPath()) + { + path.AddEllipse(ellipsebounds); + using (PathGradientBrush brush = new PathGradientBrush(path)) + { + // Fill a rectangle with an elliptical gradient brush that goes from transparent to opaque. + // This has the effect of painting the far corners with the given color and shade less on the way in to the centre. + Color centerColor; + Color edgeColor; + if (invert) + { + centerColor = Color.FromArgb(50, baseColor.R, baseColor.G, baseColor.B); + edgeColor = Color.FromArgb(0, baseColor.R, baseColor.G, baseColor.B); + } + else + { + centerColor = Color.FromArgb(0, baseColor.R, baseColor.G, baseColor.B); + edgeColor = Color.FromArgb(255, baseColor.R, baseColor.G, baseColor.B); + } + + brush.WrapMode = WrapMode.Tile; + brush.CenterColor = centerColor; + brush.SurroundColors = new[] { edgeColor }; + + Blend blend = new Blend + { + Positions = new[] { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0F }, + Factors = new[] { 0.0f, 0.5f, 1f, 1f, 1.0f, 1.0f } + }; + + brush.Blend = blend; + + Region oldClip = graphics.Clip; + graphics.Clip = new Region(bounds); + graphics.FillRectangle(brush, ellipsebounds); + graphics.Clip = oldClip; + } + } + } + + return source; + } + + /// + /// Adds a diffused glow (inverted vignette) effect to the source image based on the given color. + /// + /// The source. + /// to base the vignette on. + /// The rectangle to define the bounds of the area to vignette. If null then the effect is applied + /// to the entire image. + /// The with the vignette applied. + public static Bitmap Glow(Bitmap source, Color baseColor, Rectangle? rectangle = null) + { + return Vignette(source, baseColor, rectangle, true); + } + } +} diff --git a/src/ImageProcessor/Processors/Brightness.cs b/src/ImageProcessor/Processors/Brightness.cs index a16f33e5c..8dd37aeb0 100644 --- a/src/ImageProcessor/Processors/Brightness.cs +++ b/src/ImageProcessor/Processors/Brightness.cs @@ -16,6 +16,7 @@ namespace ImageProcessor.Processors using System.Drawing.Imaging; using ImageProcessor.Common.Exceptions; + using ImageProcessor.Imaging.Helpers; /// /// Encapsulates methods to change the brightness component of the image. @@ -65,39 +66,12 @@ namespace ImageProcessor.Processors try { - float brightnessFactor = (float)this.DynamicParameter / 100; + int threshold = (int)this.DynamicParameter; + newImage = new Bitmap(image); + newImage = Adjustments.Brightness(newImage, threshold); - newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb); - - ColorMatrix colorMatrix = - new ColorMatrix( - new[] - { - new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, - new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, - new[] { brightnessFactor, brightnessFactor, brightnessFactor, 0, 1 } - }); - - using (Graphics graphics = Graphics.FromImage(newImage)) - { - using (ImageAttributes imageAttributes = new ImageAttributes()) - { - imageAttributes.SetColorMatrix(colorMatrix); - - graphics.DrawImage( - image, - new Rectangle(0, 0, image.Width, image.Height), - 0, - 0, - image.Width, - image.Height, - GraphicsUnit.Pixel, - imageAttributes); - - image.Dispose(); - image = newImage; - } - } + image.Dispose(); + image = newImage; } catch (Exception ex) { diff --git a/src/ImageProcessor/Processors/Contrast.cs b/src/ImageProcessor/Processors/Contrast.cs index 4170e660a..8a6491257 100644 --- a/src/ImageProcessor/Processors/Contrast.cs +++ b/src/ImageProcessor/Processors/Contrast.cs @@ -13,9 +13,9 @@ namespace ImageProcessor.Processors using System; using System.Collections.Generic; using System.Drawing; - using System.Drawing.Imaging; using ImageProcessor.Common.Exceptions; + using ImageProcessor.Imaging.Helpers; /// /// Encapsulates methods to change the contrast component of the image. @@ -65,36 +65,12 @@ namespace ImageProcessor.Processors try { - float contrastFactor = (float)this.DynamicParameter / 100; + int threshold = (int)this.DynamicParameter; + newImage = new Bitmap(image); + newImage = Adjustments.Contrast(newImage, threshold); - // Stop at -1 to prevent inversion. - contrastFactor++; - float factorTransform = 0.5f * (1.0f - contrastFactor); - - newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb); - - ColorMatrix colorMatrix = new ColorMatrix( - new[] - { - new[] { contrastFactor, 0, 0, 0, 0 }, - new[] { 0, contrastFactor, 0, 0, 0 }, - new[] { 0, 0, contrastFactor, 0, 0 }, - new float[] { 0, 0, 0, 1, 0 }, - new[] { factorTransform, factorTransform, factorTransform, 0, 1 } - }); - - using (Graphics graphics = Graphics.FromImage(newImage)) - { - using (ImageAttributes imageAttributes = new ImageAttributes()) - { - imageAttributes.SetColorMatrix(colorMatrix); - - graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); - - image.Dispose(); - image = newImage; - } - } + image.Dispose(); + image = newImage; } catch (Exception ex) { diff --git a/src/ImageProcessor/Processors/DetectEdges.cs b/src/ImageProcessor/Processors/DetectEdges.cs index 9df1e5184..db5c27836 100644 --- a/src/ImageProcessor/Processors/DetectEdges.cs +++ b/src/ImageProcessor/Processors/DetectEdges.cs @@ -15,7 +15,7 @@ namespace ImageProcessor.Processors using System.Drawing; using ImageProcessor.Common.Exceptions; - using ImageProcessor.Imaging.EdgeDetection; + using ImageProcessor.Imaging.Filters.EdgeDetection; /// /// Produces an image with the detected edges highlighted. diff --git a/src/ImageProcessor/Processors/EntropyCrop.cs b/src/ImageProcessor/Processors/EntropyCrop.cs index 9b80d09ec..e92ccb64e 100644 --- a/src/ImageProcessor/Processors/EntropyCrop.cs +++ b/src/ImageProcessor/Processors/EntropyCrop.cs @@ -14,8 +14,8 @@ namespace ImageProcessor.Processors using ImageProcessor.Common.Exceptions; using ImageProcessor.Imaging; - using ImageProcessor.Imaging.Binarization; - using ImageProcessor.Imaging.EdgeDetection; + using ImageProcessor.Imaging.Filters.Binarization; + using ImageProcessor.Imaging.Filters.EdgeDetection; /// /// The auto crop. @@ -62,7 +62,7 @@ namespace ImageProcessor.Processors grey = new ConvolutionFilter(new SobelEdgeFilter(), true).ProcessFilter((Bitmap)image); grey = new BinaryThreshold(threshold).ProcessFilter(grey); - Rectangle rectangle = this.FindBox(grey, 0); + Rectangle rectangle = this.FindBoundingBox(grey, 0); newImage = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb); using (Graphics graphics = Graphics.FromImage(newImage)) @@ -100,7 +100,20 @@ namespace ImageProcessor.Processors return image; } - private Rectangle FindBox(Bitmap bitmap, byte componentToRemove) + /// + /// Finds the bounding rectangle based on the first instance of any color component other + /// than the given one. + /// + /// + /// The bitmap. + /// + /// + /// The color component to remove. + /// + /// + /// The . + /// + private Rectangle FindBoundingBox(Bitmap bitmap, byte componentToRemove) { int width = bitmap.Width; int height = bitmap.Height; @@ -115,8 +128,6 @@ namespace ImageProcessor.Processors { for (int x = 0; x < width; x++) { - Color c = fastBitmap.GetPixel(x, y); - if (fastBitmap.GetPixel(x, y).B != componentToRemove) { return y; diff --git a/src/ImageProcessor/Processors/Filter.cs b/src/ImageProcessor/Processors/Filter.cs index e8130afa3..ff93a6fd3 100644 --- a/src/ImageProcessor/Processors/Filter.cs +++ b/src/ImageProcessor/Processors/Filter.cs @@ -16,7 +16,7 @@ namespace ImageProcessor.Processors using System.Drawing.Imaging; using ImageProcessor.Common.Exceptions; - using ImageProcessor.Imaging.Filters; + using ImageProcessor.Imaging.Filters.Photo; /// /// Encapsulates methods with which to add filters to an image. @@ -71,7 +71,7 @@ namespace ImageProcessor.Processors if (matrix != null) { - return matrix.TransformImage(factory, image, newImage); + return matrix.TransformImage(image, newImage); } } catch (Exception ex) diff --git a/src/ImageProcessor/Processors/Vignette.cs b/src/ImageProcessor/Processors/Vignette.cs index 3be2beee1..65075a3d2 100644 --- a/src/ImageProcessor/Processors/Vignette.cs +++ b/src/ImageProcessor/Processors/Vignette.cs @@ -13,9 +13,9 @@ namespace ImageProcessor.Processors using System; using System.Collections.Generic; using System.Drawing; - using System.Drawing.Drawing2D; using ImageProcessor.Common.Exceptions; + using ImageProcessor.Imaging.Helpers; /// /// Encapsulates methods with which to add a vignette image effect to an image. @@ -66,55 +66,14 @@ namespace ImageProcessor.Processors try { + Color baseColor = (Color)this.DynamicParameter; newImage = new Bitmap(image); - using (Graphics graphics = Graphics.FromImage(newImage)) - { - Rectangle bounds = new Rectangle(0, 0, newImage.Width, newImage.Height); - Rectangle ellipsebounds = bounds; - - // Increase the rectangle size by the difference between the rectangle dimensions and sqrt(2)/2 * the rectangle dimensions. - // Why sqrt(2)/2? Because the point (sqrt(2)/2, sqrt(2)/2) is the 45 degree angle point on a unit circle. Scaling by the width - // and height gives the distance needed to inflate the rect to make sure it's fully covered. - ellipsebounds.Offset(-ellipsebounds.X, -ellipsebounds.Y); - int x = ellipsebounds.Width - (int)Math.Floor(.70712 * ellipsebounds.Width); - int y = ellipsebounds.Height - (int)Math.Floor(.70712 * ellipsebounds.Height); - ellipsebounds.Inflate(x, y); - - using (GraphicsPath path = new GraphicsPath()) - { - path.AddEllipse(ellipsebounds); - using (PathGradientBrush brush = new PathGradientBrush(path)) - { - // Fill a rectangle with an elliptical gradient brush that goes from transparent to opaque. - // This has the effect of painting the far corners with the given color and shade less on the way in to the centre. - Color baseColor = (Color)this.DynamicParameter; - Color centerColor = Color.FromArgb(0, baseColor.R, baseColor.G, baseColor.B); - Color edgeColor = Color.FromArgb(255, baseColor.R, baseColor.G, baseColor.B); - - brush.WrapMode = WrapMode.Tile; - brush.CenterColor = centerColor; - brush.SurroundColors = new[] { edgeColor }; - - Blend blend = new Blend - { - Positions = new[] { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0F }, - Factors = new[] { 0.0f, 0.5f, 1f, 1f, 1.0f, 1.0f } - }; + newImage = Effects.Vignette(newImage, baseColor); - brush.Blend = blend; - - Region oldClip = graphics.Clip; - graphics.Clip = new Region(bounds); - graphics.FillRectangle(brush, ellipsebounds); - graphics.Clip = oldClip; - } - - // Reassign the image. - image.Dispose(); - image = newImage; - } - } + // Reassign the image. + image.Dispose(); + image = newImage; } catch (Exception ex) { diff --git a/src/ImageProcessorConsole/ImageProcessorConsole.csproj b/src/ImageProcessorConsole/ImageProcessor.Playground.csproj similarity index 96% rename from src/ImageProcessorConsole/ImageProcessorConsole.csproj rename to src/ImageProcessorConsole/ImageProcessor.Playground.csproj index 3d3b992cc..22044e950 100644 --- a/src/ImageProcessorConsole/ImageProcessorConsole.csproj +++ b/src/ImageProcessorConsole/ImageProcessor.Playground.csproj @@ -7,8 +7,8 @@ {7BF5274B-56A7-4B62-8105-E9BDF25BAFE7} Exe Properties - ImageProcessorConsole - ImageProcessorConsole + ImageProcessor.PlayGround + ImageProcessor.PlayGround v4.5 512 true diff --git a/src/ImageProcessorConsole/Program.cs b/src/ImageProcessorConsole/Program.cs index 2e0a6e89c..2d15f83d4 100644 --- a/src/ImageProcessorConsole/Program.cs +++ b/src/ImageProcessorConsole/Program.cs @@ -8,7 +8,7 @@ // // -------------------------------------------------------------------------------------------------------------------- -namespace ImageProcessorConsole +namespace ImageProcessor.PlayGround { using System; using System.Collections.Generic; @@ -16,12 +16,10 @@ namespace ImageProcessorConsole using System.Drawing; using System.IO; using System.Linq; + using ImageProcessor; using ImageProcessor.Imaging; - using ImageProcessor.Imaging.EdgeDetection; - using ImageProcessor.Imaging.Filters; - using ImageProcessor.Plugins.Cair; - using ImageProcessor.Plugins.Cair.Imaging; + using ImageProcessor.Imaging.Filters.Photo; /// /// The program. @@ -46,7 +44,7 @@ namespace ImageProcessorConsole di.Create(); } - IEnumerable files = GetFilesByExtensions(di, ".png2"); + IEnumerable files = GetFilesByExtensions(di, ".jpg"); //IEnumerable files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png", ".tif"); foreach (FileInfo fileInfo in files) @@ -78,7 +76,8 @@ namespace ImageProcessorConsole //.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80) //.Resize(layer) //.DetectEdges(new KirschEdgeFilter()) - .EntropyCrop() + //.EntropyCrop() + .Filter(MatrixFilters.Polaroid) //.Filter(MatrixFilters.Comic) //.Filter(MatrixFilters.HiSatch) //.Pixelate(8) diff --git a/src/ImageProcessorConsole/images/input/new-york.jpg.REMOVED.git-id b/src/ImageProcessorConsole/images/input/new-york.jpg.REMOVED.git-id new file mode 100644 index 000000000..ce679dd1d --- /dev/null +++ b/src/ImageProcessorConsole/images/input/new-york.jpg.REMOVED.git-id @@ -0,0 +1 @@ +d04aa90445e483d5614cf597cbd7caa6d55d9aaa \ No newline at end of file diff --git a/src/ImageProcessorConsole/images/output/monster-whitebg.png.REMOVED.git-id b/src/ImageProcessorConsole/images/output/monster-whitebg.png.REMOVED.git-id deleted file mode 100644 index 51a0019e9..000000000 --- a/src/ImageProcessorConsole/images/output/monster-whitebg.png.REMOVED.git-id +++ /dev/null @@ -1 +0,0 @@ -e40fa501c49374e25d137627084dfa993931205f \ No newline at end of file