From 32dfb7cb611050cc02e69ce14e8f8ee67277fc31 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 18 Dec 2017 14:07:26 +1100 Subject: [PATCH] Convert photo effects --- .../Processing/ColorMatrix/BlackWhite.cs | 2 +- .../ColorMatrix/Matrix4x4Extensions.cs | 72 +++++++++++++++++++ .../ColorMatrix/BlackWhiteProcessor.cs | 29 +++----- .../ColorMatrix/KodachromeProcessor.cs | 23 +++--- .../ColorMatrix/LomographProcessor.cs | 15 +--- .../ColorMatrix/PolaroidProcessor.cs | 24 +------ 6 files changed, 96 insertions(+), 69 deletions(-) diff --git a/src/ImageSharp/Processing/ColorMatrix/BlackWhite.cs b/src/ImageSharp/Processing/ColorMatrix/BlackWhite.cs index 300c07381..a8c946a73 100644 --- a/src/ImageSharp/Processing/ColorMatrix/BlackWhite.cs +++ b/src/ImageSharp/Processing/ColorMatrix/BlackWhite.cs @@ -43,4 +43,4 @@ namespace SixLabors.ImageSharp return source; } } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Processing/ColorMatrix/Matrix4x4Extensions.cs b/src/ImageSharp/Processing/ColorMatrix/Matrix4x4Extensions.cs index 21c7f05e5..37aa0f629 100644 --- a/src/ImageSharp/Processing/ColorMatrix/Matrix4x4Extensions.cs +++ b/src/ImageSharp/Processing/ColorMatrix/Matrix4x4Extensions.cs @@ -12,6 +12,78 @@ namespace SixLabors.ImageSharp.Processing // ReSharper disable once InconsistentNaming public static class Matrix4x4Extensions { + /// + /// Gets an approximated black and white filter + /// + public static Matrix4x4 BlackWhiteFilter { get; } = new Matrix4x4() + { + M11 = 1.5F, + M12 = 1.5F, + M13 = 1.5F, + M21 = 1.5F, + M22 = 1.5F, + M23 = 1.5F, + M31 = 1.5F, + M32 = 1.5F, + M33 = 1.5F, + M41 = -1F, + M42 = -1F, + M43 = -1F, + M44 = 1 + }; + + /// + /// Gets a filter recreating an old Kodachrome camera effect. + /// + public static Matrix4x4 KodachromeFilter { get; } = new Matrix4x4 + { + M11 = 0.7297023F, + M22 = 0.6109577F, + M33 = 0.597218F, + M41 = 0.105F, + M42 = 0.145F, + M43 = 0.155F, + M44 = 1 + } + + * CreateSaturateFilter(1.2F) * CreateContrastFilter(1.35F); + + /// + /// Gets a filter recreating an old Lomograph camera effect. + /// + public static Matrix4x4 LomographFilter { get; } = new Matrix4x4 + { + M11 = 1.5F, + M22 = 1.45F, + M33 = 1.16F, + M41 = -.1F, + M42 = -.02F, + M43 = -.07F, + M44 = 1 + } + + * CreateSaturateFilter(1.1F) * CreateContrastFilter(1.33F); + + /// + /// Gets a filter recreating an old Polaroid camera effect. + /// + public static Matrix4x4 PolaroidFilter { get; } = new Matrix4x4 + { + M11 = 1.538F, + M12 = -0.062F, + M13 = -0.262F, + M21 = -0.022F, + M22 = 1.578F, + M23 = -0.022F, + M31 = .216F, + M32 = -.16F, + M33 = 1.5831F, + M41 = 0.02F, + M42 = -0.05F, + M43 = -0.05F, + M44 = 1 + }; + /// /// Create a brightness filter matrix using the given amount. /// diff --git a/src/ImageSharp/Processing/Processors/ColorMatrix/BlackWhiteProcessor.cs b/src/ImageSharp/Processing/Processors/ColorMatrix/BlackWhiteProcessor.cs index 9f8127343..0745b7ba7 100644 --- a/src/ImageSharp/Processing/Processors/ColorMatrix/BlackWhiteProcessor.cs +++ b/src/ImageSharp/Processing/Processors/ColorMatrix/BlackWhiteProcessor.cs @@ -1,34 +1,23 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Numerics; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Processing.Processors { /// - /// Converts the colors of the image to their black and white equivalent. + /// Applies a black and white filter matrix to the image /// /// The pixel format. - internal class BlackWhiteProcessor : ColorMatrixProcessor - where TPixel : struct, IPixel + internal class BlackWhiteProcessor : FilterProcessor + where TPixel : struct, IPixel { - /// - public override Matrix4x4 Matrix => new Matrix4x4() + /// + /// Initializes a new instance of the class. + /// + public BlackWhiteProcessor() + : base(Matrix4x4Extensions.BlackWhiteFilter) { - M11 = 1.5F, - M12 = 1.5F, - M13 = 1.5F, - M21 = 1.5F, - M22 = 1.5F, - M23 = 1.5F, - M31 = 1.5F, - M32 = 1.5F, - M33 = 1.5F, - M41 = -1F, - M42 = -1F, - M43 = -1F, - M44 = 1 - }; + } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/ColorMatrix/KodachromeProcessor.cs b/src/ImageSharp/Processing/Processors/ColorMatrix/KodachromeProcessor.cs index 4277e1fc2..1f644247b 100644 --- a/src/ImageSharp/Processing/Processors/ColorMatrix/KodachromeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/ColorMatrix/KodachromeProcessor.cs @@ -1,28 +1,23 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Numerics; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Processing.Processors { /// - /// Converts the colors of the image recreating an old Kodachrome camera effect. + /// Applies a filter matrix recreating an old Kodachrome camera effect matrix to the image /// /// The pixel format. - internal class KodachromeProcessor : ColorMatrixProcessor - where TPixel : struct, IPixel + internal class KodachromeProcessor : FilterProcessor + where TPixel : struct, IPixel { - /// - public override Matrix4x4 Matrix => new Matrix4x4 + /// + /// Initializes a new instance of the class. + /// + public KodachromeProcessor() + : base(Matrix4x4Extensions.KodachromeFilter) { - M11 = 0.6997023F, - M22 = 0.4609577F, - M33 = 0.397218F, - M41 = 0.005F, - M42 = -0.005F, - M43 = 0.005F, - M44 = 1 - }; + } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/ColorMatrix/LomographProcessor.cs b/src/ImageSharp/Processing/Processors/ColorMatrix/LomographProcessor.cs index 1ec76bf55..62e9e7d62 100644 --- a/src/ImageSharp/Processing/Processors/ColorMatrix/LomographProcessor.cs +++ b/src/ImageSharp/Processing/Processors/ColorMatrix/LomographProcessor.cs @@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Converts the colors of the image recreating an old Lomograph effect. /// /// The pixel format. - internal class LomographProcessor : ColorMatrixProcessor + internal class LomographProcessor : FilterProcessor where TPixel : struct, IPixel { private static readonly TPixel VeryDarkGreen = ColorBuilder.FromRGBA(0, 10, 0, 255); @@ -22,22 +22,11 @@ namespace SixLabors.ImageSharp.Processing.Processors /// /// The options effecting blending and composition. public LomographProcessor(GraphicsOptions options) + : base(Matrix4x4Extensions.LomographFilter) { this.options = options; } - /// - public override Matrix4x4 Matrix => new Matrix4x4 - { - M11 = 1.5F, - M22 = 1.45F, - M33 = 1.11F, - M41 = -.1F, - M42 = .0F, - M43 = -.08F, - M44 = 1 - }; - /// protected override void AfterApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) { diff --git a/src/ImageSharp/Processing/Processors/ColorMatrix/PolaroidProcessor.cs b/src/ImageSharp/Processing/Processors/ColorMatrix/PolaroidProcessor.cs index f910562e6..60a48e131 100644 --- a/src/ImageSharp/Processing/Processors/ColorMatrix/PolaroidProcessor.cs +++ b/src/ImageSharp/Processing/Processors/ColorMatrix/PolaroidProcessor.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Numerics; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Primitives; @@ -11,11 +10,11 @@ namespace SixLabors.ImageSharp.Processing.Processors /// Converts the colors of the image recreating an old Polaroid effect. /// /// The pixel format. - internal class PolaroidProcessor : ColorMatrixProcessor + internal class PolaroidProcessor : FilterProcessor where TPixel : struct, IPixel { private static readonly TPixel VeryDarkOrange = ColorBuilder.FromRGB(102, 34, 0); - private static readonly TPixel LightOrange = ColorBuilder.FromRGBA(255, 153, 102, 178); + private static readonly TPixel LightOrange = ColorBuilder.FromRGBA(255, 153, 102, 128); private readonly GraphicsOptions options; /// @@ -23,28 +22,11 @@ namespace SixLabors.ImageSharp.Processing.Processors /// /// The options effecting blending and composition. public PolaroidProcessor(GraphicsOptions options) + : base(Matrix4x4Extensions.PolaroidFilter) { this.options = options; } - /// - public override Matrix4x4 Matrix => new Matrix4x4 - { - M11 = 1.538F, - M12 = -0.062F, - M13 = -0.262F, - M21 = -0.022F, - M22 = 1.578F, - M23 = -0.022F, - M31 = .216F, - M32 = -.16F, - M33 = 1.5831F, - M41 = 0.02F, - M42 = -0.05F, - M43 = -0.05F, - M44 = 1 - }; - /// protected override void AfterApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration) {