From 3121135309bd3ca6491c9fa833f8ad43b350a3fd Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 1 Feb 2016 21:04:48 +1100 Subject: [PATCH] Color performance optimizations. Former-commit-id: 103031740de3d3b482d55800eac4910a29e882a0 Former-commit-id: 258d7739fcdbb02d79aa52265f1af8d4a0d630c9 Former-commit-id: 9ee758d7ccec4869fd0664f1379ec3b152be8d3e --- src/ImageProcessorCore/Colors/Color.cs | 4 +--- src/ImageProcessorCore/Colors/Colorspaces/CieLab.cs | 8 +++++--- src/ImageProcessorCore/Colors/Colorspaces/CieXyz.cs | 8 +++++--- src/ImageProcessorCore/Colors/Colorspaces/Cmyk.cs | 10 ++++++---- src/ImageProcessorCore/Colors/Colorspaces/Hsl.cs | 8 +++++--- src/ImageProcessorCore/Colors/Colorspaces/Hsv.cs | 8 +++++--- src/ImageProcessorCore/Colors/Colorspaces/YCbCr.cs | 8 +++++--- 7 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/ImageProcessorCore/Colors/Color.cs b/src/ImageProcessorCore/Colors/Color.cs index f4b774e25..36cb548fb 100644 --- a/src/ImageProcessorCore/Colors/Color.cs +++ b/src/ImageProcessorCore/Colors/Color.cs @@ -84,9 +84,7 @@ namespace ImageProcessorCore float a = Convert.ToByte(hex.Substring(0, 2), 16); // Do division of Vector4 instead of each component to utilize SIMD optimizations - this.backingVector = new Vector4(r, g, b, a) / 255f; - - this.backingVector = FromNonPremultiplied(this.backingVector, this.A); + this.backingVector = FromNonPremultiplied(new Vector4(r, g, b, a) / 255f, this.A); } else if (hex.Length == 6) diff --git a/src/ImageProcessorCore/Colors/Colorspaces/CieLab.cs b/src/ImageProcessorCore/Colors/Colorspaces/CieLab.cs index 2f684e45c..cce92601a 100644 --- a/src/ImageProcessorCore/Colors/Colorspaces/CieLab.cs +++ b/src/ImageProcessorCore/Colors/Colorspaces/CieLab.cs @@ -172,9 +172,11 @@ namespace ImageProcessorCore /// public bool AlmostEquals(CieLab other, float precision) { - return Math.Abs(this.L - other.L) < precision - && Math.Abs(this.B - other.B) < precision - && Math.Abs(this.B - other.B) < precision; + Vector3 result = Vector3.Abs(this.backingVector - other.backingVector); + + return result.X < precision + && result.Y < precision + && result.Z < precision; } /// diff --git a/src/ImageProcessorCore/Colors/Colorspaces/CieXyz.cs b/src/ImageProcessorCore/Colors/Colorspaces/CieXyz.cs index a14c04bdd..8f41c8abb 100644 --- a/src/ImageProcessorCore/Colors/Colorspaces/CieXyz.cs +++ b/src/ImageProcessorCore/Colors/Colorspaces/CieXyz.cs @@ -163,9 +163,11 @@ namespace ImageProcessorCore /// public bool AlmostEquals(CieXyz other, float precision) { - return Math.Abs(this.X - other.X) < precision - && Math.Abs(this.Y - other.Y) < precision - && Math.Abs(this.Z - other.Z) < precision; + Vector3 result = Vector3.Abs(this.backingVector - other.backingVector); + + return result.X < precision + && result.Y < precision + && result.Z < precision; } /// diff --git a/src/ImageProcessorCore/Colors/Colorspaces/Cmyk.cs b/src/ImageProcessorCore/Colors/Colorspaces/Cmyk.cs index 1d8e8d852..b343288a6 100644 --- a/src/ImageProcessorCore/Colors/Colorspaces/Cmyk.cs +++ b/src/ImageProcessorCore/Colors/Colorspaces/Cmyk.cs @@ -175,10 +175,12 @@ namespace ImageProcessorCore /// public bool AlmostEquals(Cmyk other, float precision) { - return Math.Abs(this.C - other.C) < precision - && Math.Abs(this.M - other.M) < precision - && Math.Abs(this.Y - other.Y) < precision - && Math.Abs(this.K - other.K) < precision; + Vector4 result = Vector4.Abs(this.backingVector - other.backingVector); + + return result.X < precision + && result.Y < precision + && result.Z < precision + && result.W < precision; } /// diff --git a/src/ImageProcessorCore/Colors/Colorspaces/Hsl.cs b/src/ImageProcessorCore/Colors/Colorspaces/Hsl.cs index e585ecf4d..e6eee42e3 100644 --- a/src/ImageProcessorCore/Colors/Colorspaces/Hsl.cs +++ b/src/ImageProcessorCore/Colors/Colorspaces/Hsl.cs @@ -192,9 +192,11 @@ namespace ImageProcessorCore /// public bool AlmostEquals(Hsl other, float precision) { - return Math.Abs(this.H - other.H) < precision - && Math.Abs(this.S - other.S) < precision - && Math.Abs(this.L - other.L) < precision; + Vector3 result = Vector3.Abs(this.backingVector - other.backingVector); + + return result.X < precision + && result.Y < precision + && result.Z < precision; } /// diff --git a/src/ImageProcessorCore/Colors/Colorspaces/Hsv.cs b/src/ImageProcessorCore/Colors/Colorspaces/Hsv.cs index 8ce2183de..914df51c9 100644 --- a/src/ImageProcessorCore/Colors/Colorspaces/Hsv.cs +++ b/src/ImageProcessorCore/Colors/Colorspaces/Hsv.cs @@ -186,9 +186,11 @@ namespace ImageProcessorCore /// public bool AlmostEquals(Hsv other, float precision) { - return Math.Abs(this.H - other.H) < precision - && Math.Abs(this.S - other.S) < precision - && Math.Abs(this.V - other.V) < precision; + Vector3 result = Vector3.Abs(this.backingVector - other.backingVector); + + return result.X < precision + && result.Y < precision + && result.Z < precision; } /// diff --git a/src/ImageProcessorCore/Colors/Colorspaces/YCbCr.cs b/src/ImageProcessorCore/Colors/Colorspaces/YCbCr.cs index 5cef084cc..5c47c6c08 100644 --- a/src/ImageProcessorCore/Colors/Colorspaces/YCbCr.cs +++ b/src/ImageProcessorCore/Colors/Colorspaces/YCbCr.cs @@ -162,9 +162,11 @@ namespace ImageProcessorCore /// public bool AlmostEquals(YCbCr other, float precision) { - return Math.Abs(this.Y - other.Y) < precision - && Math.Abs(this.Cb - other.Cb) < precision - && Math.Abs(this.Cr - other.Cr) < precision; + Vector3 result = Vector3.Abs(this.backingVector - other.backingVector); + + return result.X < precision + && result.Y < precision + && result.Z < precision; } ///