Browse Source

Revert Number changes

pull/1760/head
Ildar Khayrutdinov 5 years ago
parent
commit
a6c038b52c
  1. 32
      src/ImageSharp/Common/Helpers/Numerics.cs
  2. 17
      src/ImageSharp/Metadata/Profiles/Exif/Values/ExifNumber.cs
  3. 37
      src/ImageSharp/Metadata/Profiles/Exif/Values/ExifNumberArray.cs
  4. 75
      src/ImageSharp/Primitives/Number.cs

32
src/ImageSharp/Common/Helpers/Numerics.cs

@ -226,38 +226,6 @@ namespace SixLabors.ImageSharp
return value; return value;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Clamp(ulong value, ulong min, ulong max)
{
if (value > max)
{
return max;
}
if (value < min)
{
return min;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Clamp(long value, long min, long max)
{
if (value > max)
{
return max;
}
if (value < min)
{
return min;
}
return value;
}
/// <summary> /// <summary>
/// Returns the value clamped to the inclusive range of min and max. /// Returns the value clamped to the inclusive range of min and max.
/// </summary> /// </summary>

17
src/ImageSharp/Metadata/Profiles/Exif/Values/ExifNumber.cs

@ -21,11 +21,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{ {
get get
{ {
if (this.Value > uint.MaxValue)
{
return ExifDataType.Long8;
}
if (this.Value > ushort.MaxValue) if (this.Value > ushort.MaxValue)
{ {
return ExifDataType.Long; return ExifDataType.Long;
@ -46,18 +41,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
switch (value) switch (value)
{ {
case long longValue:
if (longValue >= 0)
{
this.Value = (ulong)longValue;
return true;
}
return false;
case ulong ulongValue:
this.Value = ulongValue;
return true;
case int intValue: case int intValue:
if (intValue >= uint.MinValue) if (intValue >= uint.MinValue)
{ {

37
src/ImageSharp/Metadata/Profiles/Exif/Values/ExifNumberArray.cs

@ -26,11 +26,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
for (int i = 0; i < this.Value.Length; i++) for (int i = 0; i < this.Value.Length; i++)
{ {
if (this.Value[i] > uint.MaxValue)
{
return ExifDataType.Long8;
}
if (this.Value[i] > ushort.MaxValue) if (this.Value[i] > ushort.MaxValue)
{ {
return ExifDataType.Long; return ExifDataType.Long;
@ -50,10 +45,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
switch (value) switch (value)
{ {
case long val:
return this.SetSingle(val);
case ulong val:
return this.SetSingle(val);
case int val: case int val:
return this.SetSingle(val); return this.SetSingle(val);
case uint val: case uint val:
@ -62,10 +53,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
return this.SetSingle(val); return this.SetSingle(val);
case ushort val: case ushort val:
return this.SetSingle(val); return this.SetSingle(val);
case long[] array:
return this.SetArray(array);
case ulong[] array:
return this.SetArray(array);
case int[] array: case int[] array:
return this.SetArray(array); return this.SetArray(array);
case uint[] array: case uint[] array:
@ -87,30 +74,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
return true; return true;
} }
private bool SetArray(long[] values)
{
var numbers = new Number[values.Length];
for (int i = 0; i < values.Length; i++)
{
numbers[i] = values[i];
}
this.Value = numbers;
return true;
}
private bool SetArray(ulong[] values)
{
var numbers = new Number[values.Length];
for (int i = 0; i < values.Length; i++)
{
numbers[i] = values[i];
}
this.Value = numbers;
return true;
}
private bool SetArray(int[] values) private bool SetArray(int[] values)
{ {
var numbers = new Number[values.Length]; var numbers = new Number[values.Length];

75
src/ImageSharp/Primitives/Number.cs

@ -14,19 +14,19 @@ namespace SixLabors.ImageSharp
public struct Number : IEquatable<Number>, IComparable<Number> public struct Number : IEquatable<Number>, IComparable<Number>
{ {
[FieldOffset(0)] [FieldOffset(0)]
private readonly long signedValue; private readonly int signedValue;
[FieldOffset(0)] [FieldOffset(0)]
private readonly ulong unsignedValue; private readonly uint unsignedValue;
[FieldOffset(8)] [FieldOffset(4)]
private readonly bool isSigned; private readonly bool isSigned;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Number"/> struct. /// Initializes a new instance of the <see cref="Number"/> struct.
/// </summary> /// </summary>
/// <param name="value">The value of the number.</param> /// <param name="value">The value of the number.</param>
public Number(long value) public Number(int value)
: this() : this()
{ {
this.signedValue = value; this.signedValue = value;
@ -37,70 +37,30 @@ namespace SixLabors.ImageSharp
/// Initializes a new instance of the <see cref="Number"/> struct. /// Initializes a new instance of the <see cref="Number"/> struct.
/// </summary> /// </summary>
/// <param name="value">The value of the number.</param> /// <param name="value">The value of the number.</param>
public Number(ulong value) public Number(uint value)
: this() : this()
{ {
this.unsignedValue = value; this.unsignedValue = value;
this.isSigned = false; this.isSigned = false;
} }
/// <summary>
/// Converts the specified <see cref="long"/> to an instance of this type.
/// </summary>
/// <param name="value">The value.</param>
public static implicit operator Number(long value) => new Number(value);
/// <summary>
/// Converts the specified <see cref="ulong"/> to an instance of this type.
/// </summary>
/// <param name="value">The value.</param>
public static implicit operator Number(ulong value) => new Number(value);
/// <summary> /// <summary>
/// Converts the specified <see cref="int"/> to an instance of this type. /// Converts the specified <see cref="int"/> to an instance of this type.
/// </summary> /// </summary>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
public static implicit operator Number(int value) => new Number((long)value); public static implicit operator Number(int value) => new Number(value);
/// <summary> /// <summary>
/// Converts the specified <see cref="uint"/> to an instance of this type. /// Converts the specified <see cref="uint"/> to an instance of this type.
/// </summary> /// </summary>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
public static implicit operator Number(uint value) => new Number((ulong)value); public static implicit operator Number(uint value) => new Number(value);
/// <summary>
/// Converts the specified <see cref="short"/> to an instance of this type.
/// </summary>
/// <param name="value">The value.</param>
public static implicit operator Number(short value) => new Number((long)value);
/// <summary> /// <summary>
/// Converts the specified <see cref="ushort"/> to an instance of this type. /// Converts the specified <see cref="ushort"/> to an instance of this type.
/// </summary> /// </summary>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
public static implicit operator Number(ushort value) => new Number((ulong)value); public static implicit operator Number(ushort value) => new Number((uint)value);
/// <summary>
/// Converts the specified <see cref="long"/> to an instance of this type.
/// </summary>
/// <param name="number">The <see cref="Number"/> to convert.</param>
public static explicit operator long(Number number)
{
return number.isSigned
? number.signedValue
: (long)Numerics.Clamp(number.unsignedValue, 0, long.MaxValue);
}
/// <summary>
/// Converts the specified <see cref="ulong"/> to an instance of this type.
/// </summary>
/// <param name="number">The <see cref="Number"/> to convert.</param>
public static explicit operator ulong(Number number)
{
return number.isSigned
? (ulong)Numerics.Clamp(number.signedValue, 0, long.MaxValue)
: number.unsignedValue;
}
/// <summary> /// <summary>
/// Converts the specified <see cref="Number"/> to a <see cref="int"/>. /// Converts the specified <see cref="Number"/> to a <see cref="int"/>.
@ -109,8 +69,8 @@ namespace SixLabors.ImageSharp
public static explicit operator int(Number number) public static explicit operator int(Number number)
{ {
return number.isSigned return number.isSigned
? (int)Numerics.Clamp(number.signedValue, int.MinValue, int.MaxValue) ? number.signedValue
: (int)Numerics.Clamp(number.unsignedValue, 0, (uint)int.MaxValue); : (int)Numerics.Clamp(number.unsignedValue, 0, int.MaxValue);
} }
/// <summary> /// <summary>
@ -120,19 +80,8 @@ namespace SixLabors.ImageSharp
public static explicit operator uint(Number number) public static explicit operator uint(Number number)
{ {
return number.isSigned return number.isSigned
? (uint)Numerics.Clamp(number.signedValue, uint.MinValue, uint.MaxValue) ? (uint)Numerics.Clamp(number.signedValue, 0, int.MaxValue)
: (uint)Numerics.Clamp(number.unsignedValue, uint.MinValue, uint.MaxValue); : number.unsignedValue;
}
/// <summary>
/// Converts the specified <see cref="Number"/> to a <see cref="short"/>.
/// </summary>
/// <param name="number">The <see cref="Number"/> to convert.</param>
public static explicit operator short(Number number)
{
return number.isSigned
? (short)Numerics.Clamp(number.signedValue, short.MinValue, short.MaxValue)
: (short)Numerics.Clamp(number.unsignedValue, 0, (ushort)short.MaxValue);
} }
/// <summary> /// <summary>

Loading…
Cancel
Save