diff --git a/src/ImageProcessorCore/PackedVector/Bgra32.cs b/src/ImageProcessorCore/PackedVector/Bgra32.cs index 46c96bf2f..626031d0e 100644 --- a/src/ImageProcessorCore/PackedVector/Bgra32.cs +++ b/src/ImageProcessorCore/PackedVector/Bgra32.cs @@ -7,6 +7,7 @@ namespace ImageProcessorCore { using System; using System.Numerics; + using System.Runtime.CompilerServices; using System.Runtime.InteropServices; /// @@ -127,73 +128,73 @@ namespace ImageProcessorCore /// public void Add(Bgra32 value) { - this.B = (byte)(this.B + value.B); - this.G = (byte)(this.G + value.G); - this.R = (byte)(this.R + value.R); - this.A = (byte)(this.A + value.A); + this.B = (byte)Clamp(this.B + value.B); + this.G = (byte)Clamp(this.G + value.G); + this.R = (byte)Clamp(this.R + value.R); + this.A = (byte)Clamp(this.A + value.A); } /// public void Subtract(Bgra32 value) { - this.B = (byte)(this.B - value.B); - this.G = (byte)(this.G - value.G); - this.R = (byte)(this.R - value.R); - this.A = (byte)(this.A - value.A); + this.B = (byte)Clamp(this.B - value.B); + this.G = (byte)Clamp(this.G - value.G); + this.R = (byte)Clamp(this.R - value.R); + this.A = (byte)Clamp(this.A - value.A); } /// public void Multiply(Bgra32 value) { - this.B = (byte)(this.B * value.B); - this.G = (byte)(this.G * value.G); - this.R = (byte)(this.R * value.R); - this.A = (byte)(this.A * value.A); + this.B = (byte)Clamp(this.B * value.B); + this.G = (byte)Clamp(this.G * value.G); + this.R = (byte)Clamp(this.R * value.R); + this.A = (byte)Clamp(this.A * value.A); } /// public void Multiply(float value) { - this.B = (byte)(this.B * value); - this.G = (byte)(this.G * value); - this.R = (byte)(this.R * value); - this.A = (byte)(this.A * value); + this.B = (byte)Clamp(this.B * value); + this.G = (byte)Clamp(this.G * value); + this.R = (byte)Clamp(this.R * value); + this.A = (byte)Clamp(this.A * value); } /// public void Multiply(double value) { - this.B = (byte)(this.B * value); - this.G = (byte)(this.G * value); - this.R = (byte)(this.R * value); - this.A = (byte)(this.A * value); + this.B = (byte)Clamp(this.B * value); + this.G = (byte)Clamp(this.G * value); + this.R = (byte)Clamp(this.R * value); + this.A = (byte)Clamp(this.A * value); } /// public void Divide(Bgra32 value) { - this.B = (byte)(this.B / value.B); - this.G = (byte)(this.G / value.G); - this.R = (byte)(this.R / value.R); - this.A = (byte)(this.A / value.A); + this.B = (byte)Clamp((float)this.B / value.B); + this.G = (byte)Clamp((float)this.G / value.G); + this.R = (byte)Clamp((float)this.R / value.R); + this.A = (byte)Clamp((float)this.A / value.A); } /// public void Divide(float value) { - this.B = (byte)(this.B / value); - this.G = (byte)(this.G / value); - this.R = (byte)(this.R / value); - this.A = (byte)(this.A / value); + this.B = (byte)Clamp(this.B / value); + this.G = (byte)Clamp(this.G / value); + this.R = (byte)Clamp(this.R / value); + this.A = (byte)Clamp(this.A / value); } /// public void Divide(double value) { - this.B = (byte)(this.B / value); - this.G = (byte)(this.G / value); - this.R = (byte)(this.R / value); - this.A = (byte)(this.A / value); + this.B = (byte)Clamp(this.B / value); + this.G = (byte)Clamp(this.G / value); + this.R = (byte)Clamp(this.R / value); + this.A = (byte)Clamp(this.A / value); } /// @@ -230,13 +231,7 @@ namespace ImageProcessorCore /// public byte[] ToBytes() { - return new[] - { - this.B, - this.G, - this.R, - this.A - }; + return new[] { this.B, this.G, this.R, this.A }; } /// @@ -266,6 +261,36 @@ namespace ImageProcessorCore return this.GetHashCode(this); } + /// + /// Clamps the value to the acceptable byte range. + /// + /// The value. + /// + /// The . + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static float Clamp(float value) + { + value = (value > 255) ? 255 : value; + value = (value < 0) ? 0 : value; + return value; + } + + /// + /// Clamps the value to the acceptable byte range. + /// + /// The value. + /// + /// The . + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static double Clamp(double value) + { + value = (value > 255) ? 255 : value; + value = (value < 0) ? 0 : value; + return value; + } + /// /// Returns the hash code for this instance. ///