Browse Source

Add Compand methods to Color

This will eventually replace the PixelOperations methods.


Former-commit-id: b146c617f33431ace1ca71b32a6ab743d4b96c6c
Former-commit-id: 85db58fe32a3e4c7cfb07972eb47ec86dceba9b2
Former-commit-id: b77b762a1295de23bb36a0ad4b4557e03b5998d9
af/merge-core
James South 11 years ago
parent
commit
16af180f9f
  1. 72
      src/ImageProcessor/Colors/Color.cs
  2. 1
      src/ImageProcessor/Common/Helpers/PixelOperations.cs
  3. 2
      src/ImageProcessor/project.lock.json.REMOVED.git-id

72
src/ImageProcessor/Colors/Color.cs

@ -473,6 +473,40 @@ namespace ImageProcessor
return (from * (1 - amount)) + (to * amount);
}
/// <summary>
/// Compresseses a linear color signal to its sRGB equivalent.
/// <see href="http://www.4p8.com/eric.brasseur/gamma.html#formulas"/>
/// <see href="http://entropymine.com/imageworsener/srgbformula/"/>
/// </summary>
/// <param name="linear">The <see cref="Color"/> whos signal to compress.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static Color Compand(Color linear)
{
// TODO: Is there a faster way to do this?
float r = Compand(linear.R);
float g = Compand(linear.G);
float b = Compand(linear.B);
return new Color(r, g, b, linear.A);
}
/// <summary>
/// Expands an sRGB color signal to its linear equivalent.
/// <see href="http://www.4p8.com/eric.brasseur/gamma.html#formulas"/>
/// <see href="http://entropymine.com/imageworsener/srgbformula/"/>
/// </summary>
/// <param name="gamma">The <see cref="Color"/> whos signal to expand.</param>
/// <returns>The <see cref="Color"/>.</returns>
public static Color InverseCompand(Color gamma)
{
// TODO: Is there a faster way to do this?
float r = InverseCompand(gamma.R);
float g = InverseCompand(gamma.G);
float b = InverseCompand(gamma.B);
return new Color(r, g, b, gamma.A);
}
/// <summary>
/// Gets a <see cref="Vector4"/> representation for this <see cref="Color"/>.
/// </summary>
@ -527,6 +561,44 @@ namespace ImageProcessor
return this.backingVector.Equals(other.backingVector);
}
/// <summary>
/// Gets the compressed sRGB value from an linear signal.
/// <see href="http://www.4p8.com/eric.brasseur/gamma.html#formulas"/>
/// <see href="http://entropymine.com/imageworsener/srgbformula/"/>
/// </summary>
/// <param name="signal">The signal value to compress.</param>
/// <returns>
/// The <see cref="float"/>.
/// </returns>
private static float Compand(float signal)
{
if (signal <= 0.0031308f)
{
return signal * 12.92f;
}
return (1.055f * (float)Math.Pow(signal, 0.41666666f)) - 0.055f;
}
/// <summary>
/// Gets the expanded linear value from an sRGB signal.
/// <see href="http://www.4p8.com/eric.brasseur/gamma.html#formulas"/>
/// <see href="http://entropymine.com/imageworsener/srgbformula/"/>
/// </summary>
/// <param name="signal">The signal value to expand.</param>
/// <returns>
/// The <see cref="float"/>.
/// </returns>
private static float InverseCompand(float signal)
{
if (signal <= 0.04045f)
{
return signal / 12.92f;
}
return (float)Math.Pow((signal + 0.055f) / 1.055f, 2.4f);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>

1
src/ImageProcessor/Common/Helpers/PixelOperations.cs

@ -6,6 +6,7 @@
namespace ImageProcessor
{
using System;
using System.Numerics;
/// <summary>
/// Performs per-pixel operations.

2
src/ImageProcessor/project.lock.json.REMOVED.git-id

@ -1 +1 @@
3f05708641eb3ed085d4689aae4a960eb067fd16
eb00c54ee74016c2b70f81963e7e8f83cb2dd54b
Loading…
Cancel
Save