Browse Source

Merge pull request #59 from olivif/olivif/convertYCbCr

Optimizing YCbCr to use a backing vector. Refactoring namespaces for Color Spaces.
pull/65/head
Scott Williams 10 years ago
committed by GitHub
parent
commit
5031e14742
  1. 7
      src/ImageSharp/Colors/ColorspaceTransforms.cs
  2. 17
      src/ImageSharp/Colors/Spaces/Bgra32.cs
  3. 17
      src/ImageSharp/Colors/Spaces/CieLab.cs
  4. 17
      src/ImageSharp/Colors/Spaces/CieXyz.cs
  5. 17
      src/ImageSharp/Colors/Spaces/Cmyk.cs
  6. 17
      src/ImageSharp/Colors/Spaces/Hsl.cs
  7. 17
      src/ImageSharp/Colors/Spaces/Hsv.cs
  8. 2
      src/ImageSharp/Colors/Spaces/IAlmostEquatable.cs
  9. 33
      src/ImageSharp/Colors/Spaces/YCbCr.cs
  10. 2
      tests/ImageSharp.Tests/Colors/ColorConversionTests.cs

7
src/ImageSharp/Colors/ColorspaceTransforms.cs

@ -7,6 +7,7 @@ namespace ImageSharp
{
using System;
using System.Numerics;
using Colors.Spaces;
/// <summary>
/// Packed vector type containing four 8-bit unsigned normalized values ranging from 0 to 255.
@ -62,9 +63,9 @@ namespace ImageSharp
/// </returns>
public static implicit operator Color(YCbCr color)
{
byte y = color.Y;
int cb = color.Cb - 128;
int cr = color.Cr - 128;
float y = color.Y;
float cb = color.Cb - 128;
float cr = color.Cr - 128;
byte r = (byte)(y + (1.402F * cr)).Clamp(0, 255);
byte g = (byte)(y - (0.34414F * cb) - (0.71414F * cr)).Clamp(0, 255);

17
src/ImageSharp/Colors/Colorspaces/Bgra32.cs → src/ImageSharp/Colors/Spaces/Bgra32.cs

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
namespace ImageSharp.Colors.Spaces
{
using System;
using System.ComponentModel;
@ -22,7 +22,7 @@ namespace ImageSharp
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
private Vector4 backingVector;
private readonly Vector4 backingVector;
/// <summary>
/// Initializes a new instance of the <see cref="Bgra32"/> struct.
@ -133,7 +133,7 @@ namespace ImageSharp
/// <inheritdoc/>
public override int GetHashCode()
{
return GetHashCode(this);
return this.backingVector.GetHashCode();
}
/// <inheritdoc/>
@ -152,16 +152,5 @@ namespace ImageSharp
{
return this.backingVector.Equals(other.backingVector);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <param name="color">
/// The instance of <see cref="Bgra32"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
private static int GetHashCode(Bgra32 color) => color.backingVector.GetHashCode();
}
}

17
src/ImageSharp/Colors/Colorspaces/CieLab.cs → src/ImageSharp/Colors/Spaces/CieLab.cs

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
namespace ImageSharp.Colors.Spaces
{
using System;
using System.ComponentModel;
@ -28,7 +28,7 @@ namespace ImageSharp
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
private Vector3 backingVector;
private readonly Vector3 backingVector;
/// <summary>
/// Initializes a new instance of the <see cref="CieLab"/> struct.
@ -138,7 +138,7 @@ namespace ImageSharp
/// <inheritdoc/>
public override int GetHashCode()
{
return GetHashCode(this);
return this.backingVector.GetHashCode();
}
/// <inheritdoc/>
@ -178,16 +178,5 @@ namespace ImageSharp
&& result.Y < precision
&& result.Z < precision;
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <param name="color">
/// The instance of <see cref="CieLab"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
private static int GetHashCode(CieLab color) => color.backingVector.GetHashCode();
}
}

17
src/ImageSharp/Colors/Colorspaces/CieXyz.cs → src/ImageSharp/Colors/Spaces/CieXyz.cs

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
namespace ImageSharp.Colors.Spaces
{
using System;
using System.ComponentModel;
@ -28,7 +28,7 @@ namespace ImageSharp
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
private Vector3 backingVector;
private readonly Vector3 backingVector;
/// <summary>
/// Initializes a new instance of the <see cref="CieXyz"/> struct.
@ -129,7 +129,7 @@ namespace ImageSharp
/// <inheritdoc/>
public override int GetHashCode()
{
return GetHashCode(this);
return this.backingVector.GetHashCode();
}
/// <inheritdoc/>
@ -169,16 +169,5 @@ namespace ImageSharp
&& result.Y < precision
&& result.Z < precision;
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <param name="color">
/// The instance of <see cref="Hsv"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
private static int GetHashCode(CieXyz color) => color.backingVector.GetHashCode();
}
}

17
src/ImageSharp/Colors/Colorspaces/Cmyk.cs → src/ImageSharp/Colors/Spaces/Cmyk.cs

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
namespace ImageSharp.Colors.Spaces
{
using System;
using System.ComponentModel;
@ -27,7 +27,7 @@ namespace ImageSharp
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
private Vector4 backingVector;
private readonly Vector4 backingVector;
/// <summary>
/// Initializes a new instance of the <see cref="Cmyk"/> struct.
@ -139,7 +139,7 @@ namespace ImageSharp
/// <inheritdoc/>
public override int GetHashCode()
{
return GetHashCode(this);
return this.backingVector.GetHashCode();
}
/// <inheritdoc/>
@ -180,16 +180,5 @@ namespace ImageSharp
&& result.Z < precision
&& result.W < precision;
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <param name="color">
/// The instance of <see cref="Cmyk"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
private static int GetHashCode(Cmyk color) => color.backingVector.GetHashCode();
}
}

17
src/ImageSharp/Colors/Colorspaces/Hsl.cs → src/ImageSharp/Colors/Spaces/Hsl.cs

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
namespace ImageSharp.Colors.Spaces
{
using System;
using System.ComponentModel;
@ -27,7 +27,7 @@ namespace ImageSharp
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
private Vector3 backingVector;
private readonly Vector3 backingVector;
/// <summary>
/// Initializes a new instance of the <see cref="Hsl"/> struct.
@ -158,7 +158,7 @@ namespace ImageSharp
/// <inheritdoc/>
public override int GetHashCode()
{
return GetHashCode(this);
return this.backingVector.GetHashCode();
}
/// <inheritdoc/>
@ -198,16 +198,5 @@ namespace ImageSharp
&& result.Y < precision
&& result.Z < precision;
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <param name="color">
/// The instance of <see cref="Hsl"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
private static int GetHashCode(Hsl color) => color.backingVector.GetHashCode();
}
}

17
src/ImageSharp/Colors/Colorspaces/Hsv.cs → src/ImageSharp/Colors/Spaces/Hsv.cs

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
namespace ImageSharp.Colors.Spaces
{
using System;
using System.ComponentModel;
@ -27,7 +27,7 @@ namespace ImageSharp
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
private Vector3 backingVector;
private readonly Vector3 backingVector;
/// <summary>
/// Initializes a new instance of the <see cref="Hsv"/> struct.
@ -151,7 +151,7 @@ namespace ImageSharp
/// <inheritdoc/>
public override int GetHashCode()
{
return GetHashCode(this);
return this.backingVector.GetHashCode();
}
/// <inheritdoc/>
@ -191,16 +191,5 @@ namespace ImageSharp
&& result.Y < precision
&& result.Z < precision;
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <param name="color">
/// The instance of <see cref="Hsv"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
private static int GetHashCode(Hsv color) => color.backingVector.GetHashCode();
}
}

2
src/ImageSharp/Colors/Colorspaces/IAlmostEquatable.cs → src/ImageSharp/Colors/Spaces/IAlmostEquatable.cs

@ -3,7 +3,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
namespace ImageSharp.Colors.Spaces
{
using System;

33
src/ImageSharp/Colors/Colorspaces/YCbCr.cs → src/ImageSharp/Colors/Spaces/YCbCr.cs

@ -3,10 +3,11 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
namespace ImageSharp.Colors.Spaces
{
using System;
using System.ComponentModel;
using System.Numerics;
/// <summary>
/// Represents an YCbCr (luminance, blue chroma, red chroma) color conforming to the full range standard used in digital imaging systems.
@ -19,6 +20,16 @@ namespace ImageSharp
/// </summary>
public static readonly YCbCr Empty = default(YCbCr);
/// <summary>
/// Vector which is used in clamping to the max value
/// </summary>
private static readonly Vector3 VectorMax = new Vector3(255);
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
private readonly Vector3 backingVector;
/// <summary>
/// Initializes a new instance of the <see cref="YCbCr"/> struct.
/// </summary>
@ -28,28 +39,26 @@ namespace ImageSharp
public YCbCr(byte y, byte cb, byte cr)
: this()
{
this.Y = y;
this.Cb = cb;
this.Cr = cr;
this.backingVector = Vector3.Clamp(new Vector3(y, cb, cr), Vector3.Zero, VectorMax);
}
/// <summary>
/// Gets the Y luminance component.
/// <remarks>A value ranging between 0 and 255.</remarks>
/// </summary>
public byte Y { get; }
public float Y => this.backingVector.X;
/// <summary>
/// Gets the Cb chroma component.
/// <remarks>A value ranging between 0 and 255.</remarks>
/// </summary>
public byte Cb { get; }
public float Cb => this.backingVector.Y;
/// <summary>
/// Gets the Cr chroma component.
/// <remarks>A value ranging between 0 and 255.</remarks>
/// </summary>
public byte Cr { get; }
public float Cr => this.backingVector.Z;
/// <summary>
/// Gets a value indicating whether this <see cref="YCbCr"/> is empty.
@ -117,13 +126,7 @@ namespace ImageSharp
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
{
int hashCode = this.Y.GetHashCode();
hashCode = (hashCode * 397) ^ this.Cb.GetHashCode();
hashCode = (hashCode * 397) ^ this.Cr.GetHashCode();
return hashCode;
}
return this.backingVector.GetHashCode();
}
/// <inheritdoc/>
@ -151,7 +154,7 @@ namespace ImageSharp
/// <inheritdoc/>
public bool Equals(YCbCr other)
{
return this.Y == other.Y && this.Cb == other.Cb && this.Cr == other.Cr;
return this.backingVector.Equals(other.backingVector);
}
}
}

2
tests/ImageSharp.Tests/Colors/ColorConversionTests.cs

@ -7,7 +7,7 @@ namespace ImageSharp.Tests
{
using System;
using System.Diagnostics.CodeAnalysis;
using ImageSharp.Colors.Spaces;
using Xunit;
/// <summary>

Loading…
Cancel
Save