Browse Source

Refactor ColorMatrixFilter

Former-commit-id: 1ead8cfe8162d009894eab0d501863a86a5a35e6
Former-commit-id: 647185c9a23e154ba4db0cc269900f89d562e2eb
Former-commit-id: 1d3d8d15fc54f3ec460ebf5f2c7aa228801a29a1
af/merge-core
James Jackson-South 11 years ago
parent
commit
2eb70d60ed
  1. 4
      src/ImageProcessor/Filters/ColorMatrix/BlackWhite.cs
  2. 12
      src/ImageProcessor/Filters/ColorMatrix/ColorMatrixFilter.cs
  3. 4
      src/ImageProcessor/Filters/ColorMatrix/GreyscaleBt601.cs
  4. 4
      src/ImageProcessor/Filters/ColorMatrix/GreyscaleBt709.cs
  5. 21
      src/ImageProcessor/Filters/ColorMatrix/IColorMatrixFilter.cs
  6. 4
      src/ImageProcessor/Filters/ColorMatrix/Kodachrome.cs
  7. 4
      src/ImageProcessor/Filters/ColorMatrix/Lomograph.cs
  8. 5
      src/ImageProcessor/Filters/ColorMatrix/Polaroid.cs
  9. 2
      src/ImageProcessor/Filters/ColorMatrix/Saturation.cs
  10. 4
      src/ImageProcessor/Filters/ColorMatrix/Sepia.cs
  11. 1
      src/ImageProcessor/Settings.StyleCop
  12. 32
      tests/ImageProcessor.Tests/Processors/Filters/FilterTests.cs
  13. 22
      tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs

4
src/ImageProcessor/Filters/ColorMatrix/BlackWhite.cs

@ -15,7 +15,7 @@ namespace ImageProcessor.Filters
/// <summary> /// <summary>
/// The BlackWhite matrix. /// The BlackWhite matrix.
/// </summary> /// </summary>
private static readonly Matrix4x4 Matrix = new Matrix4x4() private static readonly Matrix4x4 ColorMatrix = new Matrix4x4()
{ {
M11 = 1.5f, M11 = 1.5f,
M12 = 1.5f, M12 = 1.5f,
@ -35,7 +35,7 @@ namespace ImageProcessor.Filters
/// Initializes a new instance of the <see cref="BlackWhite"/> class. /// Initializes a new instance of the <see cref="BlackWhite"/> class.
/// </summary> /// </summary>
public BlackWhite() public BlackWhite()
: base(Matrix) : base(ColorMatrix)
{ {
} }
} }

12
src/ImageProcessor/Filters/ColorMatrix/ColorMatrixFilter.cs

@ -11,7 +11,7 @@ namespace ImageProcessor.Filters
/// <summary> /// <summary>
/// The color matrix filter. /// The color matrix filter.
/// </summary> /// </summary>
public class ColorMatrixFilter : ParallelImageProcessor public class ColorMatrixFilter : ParallelImageProcessor, IColorMatrixFilter
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ColorMatrixFilter"/> class. /// Initializes a new instance of the <see cref="ColorMatrixFilter"/> class.
@ -26,13 +26,11 @@ namespace ImageProcessor.Filters
/// <param name="matrix">The <see cref="Matrix4x4"/> to apply.</param> /// <param name="matrix">The <see cref="Matrix4x4"/> to apply.</param>
public ColorMatrixFilter(Matrix4x4 matrix) public ColorMatrixFilter(Matrix4x4 matrix)
{ {
this.Value = matrix; this.Matrix = matrix;
} }
/// <summary> /// <inheritdoc/>
/// Gets or sets the matrix value. public Matrix4x4 Matrix { get; set; }
/// </summary>
public Matrix4x4 Value { get; set; }
/// <inheritdoc/> /// <inheritdoc/>
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY)
@ -41,7 +39,7 @@ namespace ImageProcessor.Filters
int sourceBottom = sourceRectangle.Bottom; int sourceBottom = sourceRectangle.Bottom;
int startX = sourceRectangle.X; int startX = sourceRectangle.X;
int endX = sourceRectangle.Right; int endX = sourceRectangle.Right;
Matrix4x4 matrix = this.Value; Matrix4x4 matrix = this.Matrix;
Parallel.For( Parallel.For(
startY, startY,

4
src/ImageProcessor/Filters/ColorMatrix/GreyscaleBt601.cs

@ -16,7 +16,7 @@ namespace ImageProcessor.Filters
/// <summary> /// <summary>
/// The greyscale matrix. /// The greyscale matrix.
/// </summary> /// </summary>
private static readonly Matrix4x4 Matrix = new Matrix4x4() private static readonly Matrix4x4 ColorMatrix = new Matrix4x4()
{ {
M11 = .299f, M11 = .299f,
M12 = .299f, M12 = .299f,
@ -33,7 +33,7 @@ namespace ImageProcessor.Filters
/// Initializes a new instance of the <see cref="GreyscaleBt601"/> class. /// Initializes a new instance of the <see cref="GreyscaleBt601"/> class.
/// </summary> /// </summary>
public GreyscaleBt601() public GreyscaleBt601()
: base(Matrix) : base(ColorMatrix)
{ {
} }
} }

4
src/ImageProcessor/Filters/ColorMatrix/GreyscaleBt709.cs

@ -16,7 +16,7 @@ namespace ImageProcessor.Filters
/// <summary> /// <summary>
/// The greyscale matrix. /// The greyscale matrix.
/// </summary> /// </summary>
private static readonly Matrix4x4 Matrix = new Matrix4x4() private static readonly Matrix4x4 ColorMatrix = new Matrix4x4()
{ {
M11 = .2126f, M11 = .2126f,
M12 = .2126f, M12 = .2126f,
@ -33,7 +33,7 @@ namespace ImageProcessor.Filters
/// Initializes a new instance of the <see cref="GreyscaleBt709"/> class. /// Initializes a new instance of the <see cref="GreyscaleBt709"/> class.
/// </summary> /// </summary>
public GreyscaleBt709() public GreyscaleBt709()
: base(Matrix) : base(ColorMatrix)
{ {
} }
} }

21
src/ImageProcessor/Filters/ColorMatrix/IColorMatrixFilter.cs

@ -0,0 +1,21 @@
// <copyright file="IColorMatrixFilter.cs" company="James South">
// Copyright (c) James South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessor.Filters
{
using System.Numerics;
/// <summary>
/// Encapsulates properties and methods for creating processors that utilize a matrix to
/// alter the image pixels.
/// </summary>
public interface IColorMatrixFilter
{
/// <summary>
/// Gets the <see cref="Matrix4x4"/> used to alter the image.
/// </summary>
Matrix4x4 Matrix { get; }
}
}

4
src/ImageProcessor/Filters/ColorMatrix/Kodachrome.cs

@ -15,7 +15,7 @@ namespace ImageProcessor.Filters
/// <summary> /// <summary>
/// The Kodachrome matrix. Purely artistic in composition. /// The Kodachrome matrix. Purely artistic in composition.
/// </summary> /// </summary>
private static readonly Matrix4x4 Matrix = new Matrix4x4() private static readonly Matrix4x4 ColorMatrix = new Matrix4x4()
{ {
M11 = 0.6997023f, M11 = 0.6997023f,
M22 = 0.4609577f, M22 = 0.4609577f,
@ -29,7 +29,7 @@ namespace ImageProcessor.Filters
/// Initializes a new instance of the <see cref="Kodachrome"/> class. /// Initializes a new instance of the <see cref="Kodachrome"/> class.
/// </summary> /// </summary>
public Kodachrome() public Kodachrome()
: base(Matrix) : base(ColorMatrix)
{ {
} }
} }

4
src/ImageProcessor/Filters/ColorMatrix/Lomograph.cs

@ -15,7 +15,7 @@ namespace ImageProcessor.Filters
/// <summary> /// <summary>
/// The Lomograph matrix. Purely artistic in composition. /// The Lomograph matrix. Purely artistic in composition.
/// </summary> /// </summary>
private static readonly Matrix4x4 Matrix = new Matrix4x4() private static readonly Matrix4x4 ColorMatrix = new Matrix4x4()
{ {
M11 = 1.5f, M11 = 1.5f,
M22 = 1.45f, M22 = 1.45f,
@ -29,7 +29,7 @@ namespace ImageProcessor.Filters
/// Initializes a new instance of the <see cref="Lomograph"/> class. /// Initializes a new instance of the <see cref="Lomograph"/> class.
/// </summary> /// </summary>
public Lomograph() public Lomograph()
: base(Matrix) : base(ColorMatrix)
{ {
} }
} }

5
src/ImageProcessor/Filters/ColorMatrix/Polaroid.cs

@ -14,9 +14,8 @@ namespace ImageProcessor.Filters
{ {
/// <summary> /// <summary>
/// The Polaroid matrix. Purely artistic in composition. /// The Polaroid matrix. Purely artistic in composition.
/// TODO: Calculate a matrix that works in the linear color space.
/// </summary> /// </summary>
private static readonly Matrix4x4 Matrix = new Matrix4x4() private static readonly Matrix4x4 ColorMatrix = new Matrix4x4()
{ {
M11 = 1.538f, M11 = 1.538f,
M12 = -0.062f, M12 = -0.062f,
@ -36,7 +35,7 @@ namespace ImageProcessor.Filters
/// Initializes a new instance of the <see cref="Polaroid"/> class. /// Initializes a new instance of the <see cref="Polaroid"/> class.
/// </summary> /// </summary>
public Polaroid() public Polaroid()
: base(Matrix) : base(ColorMatrix)
{ {
} }
} }

2
src/ImageProcessor/Filters/ColorMatrix/Saturation.cs

@ -50,7 +50,7 @@ namespace ImageProcessor.Filters
M33 = saturationComplementB + saturationFactor, M33 = saturationComplementB + saturationFactor,
}; };
this.Value = matrix; this.Matrix = matrix;
} }
} }
} }

4
src/ImageProcessor/Filters/ColorMatrix/Sepia.cs

@ -15,7 +15,7 @@ namespace ImageProcessor.Filters
/// <summary> /// <summary>
/// The sepia matrix. /// The sepia matrix.
/// </summary> /// </summary>
private static readonly Matrix4x4 Matrix = new Matrix4x4() private static readonly Matrix4x4 ColorMatrix = new Matrix4x4()
{ {
M11 = .393f, M11 = .393f,
M12 = .349f, M12 = .349f,
@ -32,7 +32,7 @@ namespace ImageProcessor.Filters
/// Initializes a new instance of the <see cref="Sepia"/> class. /// Initializes a new instance of the <see cref="Sepia"/> class.
/// </summary> /// </summary>
public Sepia() public Sepia()
: base(Matrix) : base(ColorMatrix)
{ {
} }
} }

1
src/ImageProcessor/Settings.StyleCop

@ -18,6 +18,7 @@
<Value>lomograph</Value> <Value>lomograph</Value>
<Value>polaroid</Value> <Value>polaroid</Value>
<Value>colorspace</Value> <Value>colorspace</Value>
<Value>kodachrome</Value>
</CollectionProperty> </CollectionProperty>
</GlobalSettings> </GlobalSettings>
</StyleCopSettings> </StyleCopSettings>

32
tests/ImageProcessor.Tests/Processors/Filters/FilterTests.cs

@ -12,22 +12,22 @@ namespace ImageProcessor.Tests
{ {
public static readonly TheoryData<string, IImageProcessor> Filters = new TheoryData<string, IImageProcessor> public static readonly TheoryData<string, IImageProcessor> Filters = new TheoryData<string, IImageProcessor>
{ {
//{ "Brightness-50", new Brightness(50) }, { "Brightness-50", new Brightness(50) },
//{ "Brightness--50", new Brightness(-50) }, { "Brightness--50", new Brightness(-50) },
//{ "Contrast-50", new Contrast(50) }, { "Contrast-50", new Contrast(50) },
//{ "Contrast--50", new Contrast(-50) }, { "Contrast--50", new Contrast(-50) },
//{ "Blend", new Blend(new Image(File.OpenRead("../../TestImages/Formats/Bmp/Car.bmp")),15)}, { "Blend", new Blend(new Image(File.OpenRead("../../TestImages/Formats/Bmp/Car.bmp")),15)},
//{ "Saturation-50", new Saturation(50) }, { "Saturation-50", new Saturation(50) },
//{ "Saturation--50", new Saturation(-50) }, { "Saturation--50", new Saturation(-50) },
//{ "Alpha--50", new Alpha(50) }, { "Alpha--50", new Alpha(50) },
//{ "Invert", new Invert() }, { "Invert", new Invert() },
//{ "Sepia", new Sepia() }, { "Sepia", new Sepia() },
//{ "BlackWhite", new BlackWhite() }, { "BlackWhite", new BlackWhite() },
//{ "Lomograph", new Lomograph() }, { "Lomograph", new Lomograph() },
//{ "Polaroid", new Polaroid() }, { "Polaroid", new Polaroid() },
//{ "Kodachrome", new Kodachrome() }, { "Kodachrome", new Kodachrome() },
//{ "GreyscaleBt709", new GreyscaleBt709() }, { "GreyscaleBt709", new GreyscaleBt709() },
//{ "GreyscaleBt601", new GreyscaleBt601() },` { "GreyscaleBt601", new GreyscaleBt601() },
{ "Kayyali", new Kayyali() }, { "Kayyali", new Kayyali() },
{ "Kirsch", new Kirsch() }, { "Kirsch", new Kirsch() },
{ "Laplacian3X3", new Laplacian3X3() }, { "Laplacian3X3", new Laplacian3X3() },

22
tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs

@ -19,20 +19,20 @@ namespace ImageProcessor.Tests
/// </summary> /// </summary>
public static readonly List<string> Files = new List<string> public static readonly List<string> Files = new List<string>
{ {
//"../../TestImages/Formats/Jpg/Backdrop.jpg", "../../TestImages/Formats/Jpg/Backdrop.jpg",
"../../TestImages/Formats/Jpg/Calliphora.jpg", "../../TestImages/Formats/Jpg/Calliphora.jpg",
"../../TestImages/Formats/Jpg/lomo.jpg", "../../TestImages/Formats/Jpg/lomo.jpg",
"../../TestImages/Formats/Jpg/shaftesbury.jpg", "../../TestImages/Formats/Jpg/shaftesbury.jpg",
//"../../TestImages/Formats/Jpg/gamma_dalai_lama_gray.jpg", "../../TestImages/Formats/Jpg/gamma_dalai_lama_gray.jpg",
//"../../TestImages/Formats/Jpg/greyscale.jpg", "../../TestImages/Formats/Jpg/greyscale.jpg",
//"../../TestImages/Formats/Bmp/Car.bmp", "../../TestImages/Formats/Bmp/Car.bmp",
//"../../TestImages/Formats/Png/cmyk.png", "../../TestImages/Formats/Png/cmyk.png",
//"../../TestImages/Formats/Png/gamma-1.0-or-2.2.png", "../../TestImages/Formats/Png/gamma-1.0-or-2.2.png",
//"../../TestImages/Formats/Png/splash.png", "../../TestImages/Formats/Png/splash.png",
//"../../TestImages/Formats/Gif/leaf.gif", "../../TestImages/Formats/Gif/leaf.gif",
//"../../TestImages/Formats/Gif/rings.gif", "../../TestImages/Formats/Gif/rings.gif",
//"../../TestImages/Formats/Gif/ani2.gif", "../../TestImages/Formats/Gif/ani2.gif",
//"../../TestImages/Formats/Gif/giphy.gif" "../../TestImages/Formats/Gif/giphy.gif"
}; };
} }
} }

Loading…
Cancel
Save