mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: 81f83cc9f0f167fb3bde367bbd36fe9aa76504b0 Former-commit-id: 44d4145471664034876eb2afa05431a792ea0b78 Former-commit-id: 11968713a68322487ff287d8c30e039f0b83e302af/merge-core
16 changed files with 100 additions and 417 deletions
@ -1,273 +0,0 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="ColorMatrix.cs" company="James South">
|
|||
// Copyright (c) James South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Filters |
|||
{ |
|||
/// <summary>
|
|||
/// Defines a 5x5 matrix that contains the coordinates for the RGBAW color space.
|
|||
/// </summary>
|
|||
public sealed class ColorMatrix |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="ColorMatrix"/> class.
|
|||
/// </summary>
|
|||
public ColorMatrix() |
|||
{ |
|||
// Setup the identity matrix by default
|
|||
this.Matrix00 = 1.0f; |
|||
this.Matrix11 = 1.0f; |
|||
this.Matrix22 = 1.0f; |
|||
this.Matrix33 = 1.0f; |
|||
this.Matrix44 = 1.0f; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="ColorMatrix"/> class with the
|
|||
/// elements in the specified matrix.
|
|||
/// </summary>
|
|||
/// <param name="colorMatrix">
|
|||
/// The elements defining the new Color Matrix.
|
|||
/// </param>
|
|||
public ColorMatrix(float[][] colorMatrix) |
|||
{ |
|||
this.SetMatrix(colorMatrix); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 0th row and 0th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix00 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 0th row and 1st column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix01 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 0th row and 2nd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix02 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 0th row and 3rd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix03 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 0th row and 4th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix04 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 1st row and 0th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix10 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 1st row and 1st column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix11 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 1st row and 2nd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix12 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 1st row and 3rd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix13 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 1st row and 4th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix14 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 2nd row and 0th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix20 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 2nd row and 1st column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix21 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 2nd row and 2nd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix22 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 2nd row and 3rd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix23 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 2nd row and 4th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix24 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 3rd row and 0th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix30 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 3rd row and 1st column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix31 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 3rd row and 2nd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix32 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 3rd row and 3rd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix33 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 3rd row and 4th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix34 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 4th row and 0th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix40 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 4th row and 1st column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix41 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 4th row and 2nd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix42 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 4th row and 3rd column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix43 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the element at the 4th row and 4th column of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
public float Matrix44 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the value of the specified element of this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
/// <param name="row">
|
|||
/// The row index.
|
|||
/// </param>
|
|||
/// <param name="column">
|
|||
/// The column index.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The <see cref="float"/>.
|
|||
/// </returns>
|
|||
public float this[int row, int column] |
|||
{ |
|||
get |
|||
{ |
|||
return this.GetMatrix()[row][column]; |
|||
} |
|||
|
|||
set |
|||
{ |
|||
float[][] tempMatrix = this.GetMatrix(); |
|||
|
|||
tempMatrix[row][column] = value; |
|||
|
|||
this.SetMatrix(tempMatrix); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the values of this <see cref="ColorMatrix"/> to the values contained within the elements.
|
|||
/// </summary>
|
|||
/// <param name="colorMatrix">
|
|||
/// The new color matrix.
|
|||
/// </param>
|
|||
internal void SetMatrix(float[][] colorMatrix) |
|||
{ |
|||
this.Matrix00 = colorMatrix[0][0]; |
|||
this.Matrix01 = colorMatrix[0][1]; |
|||
this.Matrix02 = colorMatrix[0][2]; |
|||
this.Matrix03 = colorMatrix[0][3]; |
|||
this.Matrix04 = colorMatrix[0][4]; |
|||
this.Matrix10 = colorMatrix[1][0]; |
|||
this.Matrix11 = colorMatrix[1][1]; |
|||
this.Matrix12 = colorMatrix[1][2]; |
|||
this.Matrix13 = colorMatrix[1][3]; |
|||
this.Matrix14 = colorMatrix[1][4]; |
|||
this.Matrix20 = colorMatrix[2][0]; |
|||
this.Matrix21 = colorMatrix[2][1]; |
|||
this.Matrix22 = colorMatrix[2][2]; |
|||
this.Matrix23 = colorMatrix[2][3]; |
|||
this.Matrix24 = colorMatrix[2][4]; |
|||
this.Matrix30 = colorMatrix[3][0]; |
|||
this.Matrix31 = colorMatrix[3][1]; |
|||
this.Matrix32 = colorMatrix[3][2]; |
|||
this.Matrix33 = colorMatrix[3][3]; |
|||
this.Matrix34 = colorMatrix[3][4]; |
|||
this.Matrix40 = colorMatrix[4][0]; |
|||
this.Matrix41 = colorMatrix[4][1]; |
|||
this.Matrix42 = colorMatrix[4][2]; |
|||
this.Matrix43 = colorMatrix[4][3]; |
|||
this.Matrix44 = colorMatrix[4][4]; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets this <see cref="ColorMatrix"/>.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The <see cref="T:float[][]"/>.
|
|||
/// </returns>
|
|||
internal float[][] GetMatrix() |
|||
{ |
|||
float[][] returnMatrix = new float[5][]; |
|||
|
|||
for (int i = 0; i < 5; i++) |
|||
{ |
|||
returnMatrix[i] = new float[5]; |
|||
} |
|||
|
|||
returnMatrix[0][0] = this.Matrix00; |
|||
returnMatrix[0][1] = this.Matrix01; |
|||
returnMatrix[0][2] = this.Matrix02; |
|||
returnMatrix[0][3] = this.Matrix03; |
|||
returnMatrix[0][4] = this.Matrix04; |
|||
returnMatrix[1][0] = this.Matrix10; |
|||
returnMatrix[1][1] = this.Matrix11; |
|||
returnMatrix[1][2] = this.Matrix12; |
|||
returnMatrix[1][3] = this.Matrix13; |
|||
returnMatrix[1][4] = this.Matrix14; |
|||
returnMatrix[2][0] = this.Matrix20; |
|||
returnMatrix[2][1] = this.Matrix21; |
|||
returnMatrix[2][2] = this.Matrix22; |
|||
returnMatrix[2][3] = this.Matrix23; |
|||
returnMatrix[2][4] = this.Matrix24; |
|||
returnMatrix[3][0] = this.Matrix30; |
|||
returnMatrix[3][1] = this.Matrix31; |
|||
returnMatrix[3][2] = this.Matrix32; |
|||
returnMatrix[3][3] = this.Matrix33; |
|||
returnMatrix[3][4] = this.Matrix34; |
|||
returnMatrix[4][0] = this.Matrix40; |
|||
returnMatrix[4][1] = this.Matrix41; |
|||
returnMatrix[4][2] = this.Matrix42; |
|||
returnMatrix[4][3] = this.Matrix43; |
|||
returnMatrix[4][4] = this.Matrix44; |
|||
|
|||
return returnMatrix; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
// <copyright file="Kodachrome.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>
|
|||
/// Converts the colors of the image recreating an old Kodachrome camera effect.
|
|||
/// </summary>
|
|||
public class Kodachrome : ColorMatrixFilter |
|||
{ |
|||
/// <summary>
|
|||
/// The Kodachrome matrix. Purely artistic in composition.
|
|||
/// </summary>
|
|||
private static readonly Matrix4x4 Matrix = new Matrix4x4() |
|||
{ |
|||
M11 = 0.6997023f, |
|||
M22 = 0.4609577f, |
|||
M33 = 0.397218f, |
|||
M41 = 0.005f, |
|||
M42 = -0.005f, |
|||
M43 = 0.005f |
|||
}; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Kodachrome"/> class.
|
|||
/// </summary>
|
|||
public Kodachrome() |
|||
: base(Matrix) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,77 +0,0 @@ |
|||
// <copyright file="ColorMatrixFilter.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; |
|||
using System.Threading.Tasks; |
|||
|
|||
/// <summary>
|
|||
/// The color matrix filter.
|
|||
/// </summary>
|
|||
public class MatrixFilter : ParallelImageProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="MatrixFilter"/> class.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The <see cref="Matrix4x4"/> to apply.</param>
|
|||
public MatrixFilter(Matrix4x4 matrix) |
|||
{ |
|||
this.Value = matrix; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the matrix value.
|
|||
/// </summary>
|
|||
public Matrix4x4 Value { get; } |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) |
|||
{ |
|||
int sourceY = sourceRectangle.Y; |
|||
int sourceBottom = sourceRectangle.Bottom; |
|||
int startX = sourceRectangle.X; |
|||
int endX = sourceRectangle.Right; |
|||
Matrix4x4 matrix = this.Value; |
|||
|
|||
Parallel.For( |
|||
startY, |
|||
endY, |
|||
y => |
|||
{ |
|||
if (y >= sourceY && y < sourceBottom) |
|||
{ |
|||
for (int x = startX; x < endX; x++) |
|||
{ |
|||
target[x, y] = ApplyMatrix(source[x, y], matrix); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Applies the color matrix against the given color.
|
|||
/// </summary>
|
|||
/// <param name="color">The source color.</param>
|
|||
/// <param name="matrix">The matrix.</param>
|
|||
/// <returns>
|
|||
/// The <see cref="Color"/>.
|
|||
/// </returns>
|
|||
private static Color ApplyMatrix(Color color, Matrix4x4 matrix) |
|||
{ |
|||
color = PixelOperations.ToLinear(color); |
|||
|
|||
float sr = color.R; |
|||
float sg = color.G; |
|||
float sb = color.B; |
|||
|
|||
color.R = (sr * matrix.M11) + (sg * matrix.M21) + (sb * matrix.M31) + matrix.M41; |
|||
color.G = (sr * matrix.M12) + (sg * matrix.M22) + (sb * matrix.M32) + matrix.M42; |
|||
color.B = (sr * matrix.M13) + (sg * matrix.M23) + (sb * matrix.M33) + matrix.M43; |
|||
|
|||
return PixelOperations.ToSrgb(color); |
|||
} |
|||
} |
|||
} |
|||
@ -1 +1 @@ |
|||
3f05708641eb3ed085d4689aae4a960eb067fd16 |
|||
eb00c54ee74016c2b70f81963e7e8f83cb2dd54b |
|||
@ -0,0 +1,3 @@ |
|||
version https://git-lfs.github.com/spec/v1 |
|||
oid sha256:bdfad9ce367629518c374b27b8ce311d1f0326f61b9a0ed113b5b966dd3dd1bc |
|||
size 45508 |
|||
@ -0,0 +1 @@ |
|||
2eabac96326130ba423b3fecb5a163b734278370 |
|||
Loading…
Reference in new issue