mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: 6c33dc9aeeba41b9a63b1bbe9ed2de46f76a8fb9 Former-commit-id: 6b6ea3ade8da09c08ce934fe7d1bad473c9c75d4 Former-commit-id: 9b32ae418574b5bcf3af6eb7c28c715e60ac51a9af/merge-core
8 changed files with 162 additions and 59 deletions
@ -0,0 +1,77 @@ |
|||||
|
// <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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue