|
|
|
@ -1,7 +1,8 @@ |
|
|
|
// Copyright (c) Six Labors and contributors.
|
|
|
|
// Copyright (c) Six Labors and contributors.
|
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Numerics; |
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
|
|
|
|
using SixLabors.ImageSharp.PixelFormats; |
|
|
|
@ -14,6 +15,20 @@ namespace SixLabors.ImageSharp |
|
|
|
/// </summary>
|
|
|
|
internal static class ImageMaths |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Vector for converting pixel to gray value as specified by ITU-R Recommendation BT.709.
|
|
|
|
/// </summary>
|
|
|
|
private static readonly Vector4 Bt709 = new Vector4(.2126f, .7152f, .0722f, 0.0f); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Convert a pixel value to grayscale using ITU-R Recommendation BT.709.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="vector">The vector to get the luminance from.</param>
|
|
|
|
/// <param name="luminanceLevels">The number of luminance levels (256 for 8 bit, 65536 for 16 bit grayscale images)</param>
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
public static int GetBT709Luminance(ref Vector4 vector, int luminanceLevels) |
|
|
|
=> (int)MathF.Round(Vector4.Dot(vector, Bt709) * (luminanceLevels - 1)); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the luminance from the rgb components using the formula as specified by ITU-R Recommendation BT.709.
|
|
|
|
/// </summary>
|
|
|
|
|