diff --git a/src/ImageProcessor/Common/Exceptions/ImageFormatException.cs b/src/ImageProcessor/Common/Exceptions/ImageFormatException.cs index bcc477192..1a3e2baf4 100644 --- a/src/ImageProcessor/Common/Exceptions/ImageFormatException.cs +++ b/src/ImageProcessor/Common/Exceptions/ImageFormatException.cs @@ -1,13 +1,7 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright (c) James South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// The exception that is thrown when the library tries to load -// an image, which has an invalid format. -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor { @@ -27,7 +21,7 @@ namespace ImageProcessor } /// - /// Initializes a new instance of the class with the name of the + /// Initializes a new instance of the class with the name of the /// parameter that causes this exception. /// /// The error message that explains the reason for this exception. @@ -37,11 +31,11 @@ namespace ImageProcessor } /// - /// Initializes a new instance of the class with a specified + /// Initializes a new instance of the class with a specified /// error message and the exception that is the cause of this exception. /// /// The error message that explains the reason for this exception. - /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) /// if no inner exception is specified. public ImageFormatException(string errorMessage, Exception innerException) : base(errorMessage, innerException) diff --git a/src/ImageProcessor/Filters/ColorMatrix/GreyscaleMode.cs b/src/ImageProcessor/Filters/ColorMatrix/GreyscaleMode.cs new file mode 100644 index 000000000..e96c8175b --- /dev/null +++ b/src/ImageProcessor/Filters/ColorMatrix/GreyscaleMode.cs @@ -0,0 +1,23 @@ +// +// Copyright (c) James South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessor.Filters +{ + /// + /// Provides enumeration over the various greyscale methods available. + /// + public enum GreyscaleMode + { + /// + /// ITU-R Recommendation BT.709 + /// + Bt709, + + /// + /// ITU-R Recommendation BT.601 + /// + Bt601 + } +} diff --git a/src/ImageProcessor/Filters/ImageFilterExtensions.cs b/src/ImageProcessor/Filters/ImageFilterExtensions.cs index 6ee04a53c..73fe8c34c 100644 --- a/src/ImageProcessor/Filters/ImageFilterExtensions.cs +++ b/src/ImageProcessor/Filters/ImageFilterExtensions.cs @@ -10,6 +10,31 @@ namespace ImageProcessor.Filters /// public static class ImageFilterExtensions { + /// + /// Alters the alpha component of the image. + /// + /// The image this method extends. + /// The new opacity of the image. Must be between 0 and 100. + /// The . + public static Image Alpha(this Image source, int percent) + { + return Alpha(source, percent, source.Bounds); + } + + /// + /// Alters the alpha component of the image. + /// + /// The image this method extends. + /// The new opacity of the image. Must be between 0 and 100. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// The . + public static Image Alpha(this Image source, int percent, Rectangle rectangle) + { + return source.Process(rectangle, new Alpha(percent)); + } + /// /// Alters the contrast component of the image. /// @@ -26,61 +51,109 @@ namespace ImageProcessor.Filters /// /// The image this method extends. /// The new contrast of the image. Must be between -100 and 100. - /// - /// The structure that specifies the portion of the image object to draw. + /// + /// The structure that specifies the portion of the image object to alter. /// /// The . - public static Image Contrast(this Image source, int amount, Rectangle sourceRectangle) + public static Image Contrast(this Image source, int amount, Rectangle rectangle) { - return source.Process(sourceRectangle, new Contrast(amount)); + return source.Process(rectangle, new Contrast(amount)); } /// - /// Alters the alpha component of the image. + /// Inverts the colors of the image. /// /// The image this method extends. - /// The new opacity of the image. Must be between 0 and 100. /// The . - public static Image Alpha(this Image source, int percent) + public static Image Invert(this Image source) { - return Alpha(source, percent, source.Bounds); + return Invert(source, source.Bounds); } /// - /// Alters the alpha component of the image. + /// Inverts the colors of the image. /// /// The image this method extends. - /// The new opacity of the image. Must be between 0 and 100. - /// - /// The structure that specifies the portion of the image object to draw. + /// + /// The structure that specifies the portion of the image object to alter. /// /// The . - public static Image Alpha(this Image source, int percent, Rectangle sourceRectangle) + public static Image Invert(this Image source, Rectangle rectangle) { - return source.Process(sourceRectangle, new Alpha(percent)); + return source.Process(rectangle, new Invert()); } /// - /// Alters the alpha component of the image. + /// Applies sepia toning to the image. /// /// The image this method extends. /// The . - public static Image Invert(this Image source) + public static Image Sepia(this Image source) { - return Invert(source, source.Bounds); + return Sepia(source, source.Bounds); } /// - /// Alters the alpha component of the image. + /// Applies sepia toning to the image. + /// + /// The image this method extends. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// The . + public static Image Sepia(this Image source, Rectangle rectangle) + { + return source.Process(rectangle, new Sepia()); + } + + /// + /// Applies black and white toning to the image. + /// + /// The image this method extends. + /// The . + public static Image BlackWhite(this Image source) + { + return BlackWhite(source, source.Bounds); + } + + /// + /// Applies black and white toning to the image. + /// + /// The image this method extends. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// The . + public static Image BlackWhite(this Image source, Rectangle rectangle) + { + return source.Process(rectangle, new BlackWhite()); + } + + /// + /// Applies greyscale toning to the image. + /// + /// The image this method extends. + /// The formula to apply to perform the operation. + /// The . + public static Image Greyscale(this Image source, GreyscaleMode mode = GreyscaleMode.Bt709) + { + return Greyscale(source, source.Bounds, mode); + } + + /// + /// Applies greyscale toning to the image. /// /// The image this method extends. - /// - /// The structure that specifies the portion of the image object to draw. + /// + /// The structure that specifies the portion of the image object to alter. /// + /// The formula to apply to perform the operation. /// The . - public static Image Invert(this Image source, Rectangle sourceRectangle) + public static Image Greyscale(this Image source, Rectangle rectangle, GreyscaleMode mode = GreyscaleMode.Bt709) { - return source.Process(sourceRectangle, new Invert()); + return mode == GreyscaleMode.Bt709 + ? source.Process(rectangle, new GreyscaleBt709()) + : source.Process(rectangle, new GreyscaleBt601()); } } } diff --git a/src/ImageProcessor/Formats/IImageDecoder.cs b/src/ImageProcessor/Formats/IImageDecoder.cs index 434b2ea6d..bcdbb95b0 100644 --- a/src/ImageProcessor/Formats/IImageDecoder.cs +++ b/src/ImageProcessor/Formats/IImageDecoder.cs @@ -1,12 +1,7 @@ -// -------------------------------------------------------------------------------------------------------------------- -// -// Copyright (c) James South and contributors. -// Licensed under the Apache License, Version 2.0. +// +// Copyright (c) James South and contributors. +// Licensed under the Apache License, Version 2.0. // -// -// Encapsulates properties and methods required for decoding an image from a stream. -// -// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Formats { diff --git a/src/ImageProcessor/Samplers/Resize.cs b/src/ImageProcessor/Samplers/Resize.cs index b63e54bcf..1d1484417 100644 --- a/src/ImageProcessor/Samplers/Resize.cs +++ b/src/ImageProcessor/Samplers/Resize.cs @@ -17,7 +17,7 @@ namespace ImageProcessor.Samplers /// /// The epsilon for comparing floating point numbers. /// - private const float Epsilon = 0.0000001f; + private const float Epsilon = 0.00001f; /// /// The horizontal weights. @@ -99,6 +99,11 @@ namespace ImageProcessor.Samplers int originX = xw.Index; Color sourceColor = PixelOperations.ToLinear(source[originX, originY]); + if (Math.Abs(sourceColor.A) < Epsilon) + { + continue; + } + float weight = (yw.Value / verticalSum) * (xw.Value / horizontalSum); destination.R += sourceColor.R * weight;