From 6fcd81158fa825787d28b3de9e9837d503c8b846 Mon Sep 17 00:00:00 2001 From: Olivia Date: Wed, 28 Dec 2016 12:42:37 +0200 Subject: [PATCH] Adding backing vector3 to YCbCr. --- src/ImageSharp/Colors/ColorspaceTransforms.cs | 12 +++--- src/ImageSharp/Colors/Spaces/YCbCr.cs | 37 ++++++++++++------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Colors/ColorspaceTransforms.cs b/src/ImageSharp/Colors/ColorspaceTransforms.cs index 7315b9e9d2..3994e2407b 100644 --- a/src/ImageSharp/Colors/ColorspaceTransforms.cs +++ b/src/ImageSharp/Colors/ColorspaceTransforms.cs @@ -63,13 +63,13 @@ namespace ImageSharp /// 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); - byte b = (byte)(y + (1.772F * cb)).Clamp(0, 255); + float r = (y + (1.402F * cr)).Clamp(0, 255); + float g = (y - (0.34414F * cb) - (0.71414F * cr)).Clamp(0, 255); + float b = (y + (1.772F * cb)).Clamp(0, 255); return new Color(r, g, b); } diff --git a/src/ImageSharp/Colors/Spaces/YCbCr.cs b/src/ImageSharp/Colors/Spaces/YCbCr.cs index 8d94ebccaf..ada36c831e 100644 --- a/src/ImageSharp/Colors/Spaces/YCbCr.cs +++ b/src/ImageSharp/Colors/Spaces/YCbCr.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Colors.Spaces { using System; using System.ComponentModel; + using System.Numerics; /// /// Represents an YCbCr (luminance, blue chroma, red chroma) color conforming to the full range standard used in digital imaging systems. @@ -19,6 +20,11 @@ namespace ImageSharp.Colors.Spaces /// public static readonly YCbCr Empty = default(YCbCr); + /// + /// The backing vector for SIMD support. + /// + private Vector3 backingVector; + /// /// Initializes a new instance of the struct. /// @@ -28,28 +34,26 @@ namespace ImageSharp.Colors.Spaces 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, new Vector3(255)); } /// /// Gets the Y luminance component. /// A value ranging between 0 and 255. /// - public byte Y { get; } + public float Y => this.backingVector.X; /// /// Gets the Cb chroma component. /// A value ranging between 0 and 255. /// - public byte Cb { get; } + public float Cb => this.backingVector.Y; /// /// Gets the Cr chroma component. /// A value ranging between 0 and 255. /// - public byte Cr { get; } + public float Cr => this.backingVector.Z; /// /// Gets a value indicating whether this is empty. @@ -117,13 +121,7 @@ namespace ImageSharp.Colors.Spaces /// 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 GetHashCode(this); } /// @@ -151,7 +149,18 @@ namespace ImageSharp.Colors.Spaces /// public bool Equals(YCbCr other) { - return this.Y == other.Y && this.Cb == other.Cb && this.Cr == other.Cr; + return this.backingVector.Equals(other.backingVector); } + + /// + /// Returns the hash code for this instance. + /// + /// + /// The instance of to return the hash code for. + /// + /// + /// A 32-bit signed integer that is the hash code for this instance. + /// + private static int GetHashCode(YCbCr color) => color.backingVector.GetHashCode(); } } \ No newline at end of file