Browse Source

Clamp values!

Former-commit-id: 83933734dde4c1545394533a62388835dfae0996
Former-commit-id: 63b7ec79f61945702174a51807695c71d3354f21
Former-commit-id: 43b34db2532e71a7f98913ecfe8c2ada1c43ee49
pull/1/head
James Jackson-South 10 years ago
parent
commit
db0dd3efac
  1. 103
      src/ImageProcessorCore/PackedVector/Bgra32.cs

103
src/ImageProcessorCore/PackedVector/Bgra32.cs

@ -7,6 +7,7 @@ namespace ImageProcessorCore
{
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
/// <summary>
@ -127,73 +128,73 @@ namespace ImageProcessorCore
/// <inheritdoc/>
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);
}
/// <inheritdoc/>
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);
}
/// <inheritdoc/>
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);
}
/// <inheritdoc/>
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);
}
/// <inheritdoc/>
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);
}
/// <inheritdoc/>
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);
}
/// <inheritdoc/>
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);
}
/// <inheritdoc/>
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);
}
/// <inheritdoc/>
@ -230,13 +231,7 @@ namespace ImageProcessorCore
/// <inheritdoc/>
public byte[] ToBytes()
{
return new[]
{
this.B,
this.G,
this.R,
this.A
};
return new[] { this.B, this.G, this.R, this.A };
}
/// <inheritdoc/>
@ -266,6 +261,36 @@ namespace ImageProcessorCore
return this.GetHashCode(this);
}
/// <summary>
/// Clamps the value to the acceptable byte range.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
/// The <see cref="float"/>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Clamp(float value)
{
value = (value > 255) ? 255 : value;
value = (value < 0) ? 0 : value;
return value;
}
/// <summary>
/// Clamps the value to the acceptable byte range.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double Clamp(double value)
{
value = (value > 255) ? 255 : value;
value = (value < 0) ? 0 : value;
return value;
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>

Loading…
Cancel
Save