Skip to content

Commit

Permalink
Merge pull request #1790 from SixLabors/bp/pixelformattests
Browse files Browse the repository at this point in the history
Use Convert.To after rounding in Pack() to avoid different behavior on ARM vs x86/x64
  • Loading branch information
JimBobSquarePants authored Oct 27, 2021
2 parents 2add3e1 + a68aea3 commit 72904e1
Show file tree
Hide file tree
Showing 36 changed files with 149 additions and 269 deletions.
2 changes: 1 addition & 1 deletion src/ImageSharp/PixelFormats/PixelImplementations/A8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public partial struct A8 : IPixel<A8>, IPackedVector<byte>

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public readonly Vector4 ToVector4() => new Vector4(0, 0, 0, this.PackedValue / 255F);
public readonly Vector4 ToVector4() => new(0, 0, 0, this.PackedValue / 255F);

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public partial struct Argb32 : IPixel<Argb32>, IPackedVector<uint>
/// <summary>
/// The maximum byte value.
/// </summary>
private static readonly Vector4 MaxBytes = new Vector4(255);
private static readonly Vector4 MaxBytes = new(255);

/// <summary>
/// The half vector value.
/// </summary>
private static readonly Vector4 Half = new Vector4(0.5F);
private static readonly Vector4 Half = new(0.5F);

/// <summary>
/// Initializes a new instance of the <see cref="Argb32"/> struct.
Expand Down Expand Up @@ -151,7 +151,7 @@ public uint PackedValue
/// <param name="source">The <see cref="Argb32"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Color(Argb32 source) => new Color(source);
public static implicit operator Color(Argb32 source) => new(source);

/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Argb32"/>.
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Bgr24(byte r, byte g, byte b)
/// <param name="source">The <see cref="Bgr24"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Color(Bgr24 source) => new Color(source);
public static implicit operator Color(Bgr24 source) => new(source);

/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Bgr24"/>.
Expand Down Expand Up @@ -225,7 +225,7 @@ public void FromRgba64(Rgba64 source)
public override readonly bool Equals(object obj) => obj is Bgr24 other && this.Equals(other);

/// <inheritdoc />
public override readonly string ToString() => $"Bgra({this.B}, {this.G}, {this.R})";
public override readonly string ToString() => $"Bgr24({this.B}, {this.G}, {this.R})";

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
12 changes: 3 additions & 9 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgr565.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void FromVector4(Vector4 vector)

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public readonly Vector4 ToVector4() => new Vector4(this.ToVector3(), 1F);
public readonly Vector4 ToVector4() => new(this.ToVector3(), 1F);

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down Expand Up @@ -125,10 +125,7 @@ public void FromVector4(Vector4 vector)

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public void ToRgba32(ref Rgba32 dest)
{
dest.FromScaledVector4(this.ToScaledVector4());
}
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand All @@ -144,13 +141,10 @@ public void ToRgba32(ref Rgba32 dest)
/// </summary>
/// <returns>The <see cref="Vector3"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public readonly Vector3 ToVector3()
{
return new Vector3(
public readonly Vector3 ToVector3() => new(
((this.PackedValue >> 11) & 0x1F) * (1F / 31F),
((this.PackedValue >> 5) & 0x3F) * (1F / 63F),
(this.PackedValue & 0x1F) * (1F / 31F));
}

/// <inheritdoc />
public override readonly bool Equals(object obj) => obj is Bgr565 other && this.Equals(other);
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public partial struct Bgra32 : IPixel<Bgra32>, IPackedVector<uint>
/// <summary>
/// The maximum byte value.
/// </summary>
private static readonly Vector4 MaxBytes = new Vector4(255);
private static readonly Vector4 MaxBytes = new(255);

/// <summary>
/// The half vector value.
/// </summary>
private static readonly Vector4 Half = new Vector4(0.5F);
private static readonly Vector4 Half = new(0.5F);

/// <summary>
/// Initializes a new instance of the <see cref="Bgra32"/> struct.
Expand Down Expand Up @@ -104,7 +104,7 @@ public uint PackedValue
/// <param name="source">The <see cref="Bgra32"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static implicit operator Color(Bgra32 source) => new Color(source);
public static implicit operator Color(Bgra32 source) => new(source);

/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Bgra32"/>.
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ public readonly Vector4 ToVector4()

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public void ToRgba32(ref Rgba32 dest)
{
dest.FromScaledVector4(this.ToScaledVector4());
}
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
10 changes: 2 additions & 8 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgra5551.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,11 @@ public Bgra5551(float x, float y, float z, float w)

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public readonly Vector4 ToVector4()
{
return new Vector4(
public readonly Vector4 ToVector4() => new(
((this.PackedValue >> 10) & 0x1F) / 31F,
((this.PackedValue >> 5) & 0x1F) / 31F,
((this.PackedValue >> 0) & 0x1F) / 31F,
(this.PackedValue >> 15) & 0x01);
}

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down Expand Up @@ -129,10 +126,7 @@ public readonly Vector4 ToVector4()

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public void ToRgba32(ref Rgba32 dest)
{
dest.FromScaledVector4(this.ToScaledVector4());
}
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
10 changes: 2 additions & 8 deletions src/ImageSharp/PixelFormats/PixelImplementations/Byte4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,11 @@ public Byte4(float x, float y, float z, float w)

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public readonly Vector4 ToVector4()
{
return new Vector4(
public readonly Vector4 ToVector4() => new(
this.PackedValue & 0xFF,
(this.PackedValue >> 0x8) & 0xFF,
(this.PackedValue >> 0x10) & 0xFF,
(this.PackedValue >> 0x18) & 0xFF);
}

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down Expand Up @@ -129,10 +126,7 @@ public readonly Vector4 ToVector4()

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public void ToRgba32(ref Rgba32 dest)
{
dest.FromScaledVector4(this.ToScaledVector4());
}
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public readonly Vector4 ToScaledVector4()

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public readonly Vector4 ToVector4() => new Vector4(this.ToSingle(), 0, 0, 1F);
public readonly Vector4 ToVector4() => new(this.ToSingle(), 0, 0, 1F);

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down Expand Up @@ -118,10 +118,7 @@ public readonly Vector4 ToScaledVector4()

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public void ToRgba32(ref Rgba32 dest)
{
dest.FromScaledVector4(this.ToScaledVector4());
}
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ public readonly Vector4 ToVector4()

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public void ToRgba32(ref Rgba32 dest)
{
dest.FromScaledVector4(this.ToScaledVector4());
}
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
10 changes: 2 additions & 8 deletions src/ImageSharp/PixelFormats/PixelImplementations/HalfVector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,11 @@ public readonly Vector4 ToScaledVector4()

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public readonly Vector4 ToVector4()
{
return new Vector4(
public readonly Vector4 ToVector4() => new(
HalfTypeHelper.Unpack((ushort)this.PackedValue),
HalfTypeHelper.Unpack((ushort)(this.PackedValue >> 0x10)),
HalfTypeHelper.Unpack((ushort)(this.PackedValue >> 0x20)),
HalfTypeHelper.Unpack((ushort)(this.PackedValue >> 0x30)));
}

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down Expand Up @@ -137,10 +134,7 @@ public readonly Vector4 ToVector4()

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public void ToRgba32(ref Rgba32 dest)
{
dest.FromScaledVector4(this.ToScaledVector4());
}
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
25 changes: 5 additions & 20 deletions src/ImageSharp/PixelFormats/PixelImplementations/L16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,24 @@ public readonly Vector4 ToVector4()

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void FromArgb32(Argb32 source)
{
this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
public void FromArgb32(Argb32 source) => this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
ColorNumerics.UpscaleFrom8BitTo16Bit(source.R),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.G),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.B));
}

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void FromBgr24(Bgr24 source)
{
this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
public void FromBgr24(Bgr24 source) => this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
ColorNumerics.UpscaleFrom8BitTo16Bit(source.R),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.G),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.B));
}

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void FromBgra32(Bgra32 source)
{
this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
public void FromBgra32(Bgra32 source) => this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
ColorNumerics.UpscaleFrom8BitTo16Bit(source.R),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.G),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.B));
}

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand All @@ -122,23 +113,17 @@ public void FromBgra32(Bgra32 source)

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public void FromRgb24(Rgb24 source)
{
this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
public void FromRgb24(Rgb24 source) => this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
ColorNumerics.UpscaleFrom8BitTo16Bit(source.R),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.G),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.B));
}

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public void FromRgba32(Rgba32 source)
{
this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
public void FromRgba32(Rgba32 source) => this.PackedValue = ColorNumerics.Get16BitBT709Luminance(
ColorNumerics.UpscaleFrom8BitTo16Bit(source.R),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.G),
ColorNumerics.UpscaleFrom8BitTo16Bit(source.B));
}

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/PixelFormats/PixelImplementations/L8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace SixLabors.ImageSharp.PixelFormats
/// </summary>
public partial struct L8 : IPixel<L8>, IPackedVector<byte>
{
private static readonly Vector4 MaxBytes = new Vector4(255F);
private static readonly Vector4 Half = new Vector4(0.5F);
private static readonly Vector4 MaxBytes = new(255F);
private static readonly Vector4 Half = new(0.5F);

/// <summary>
/// Initializes a new instance of the <see cref="L8"/> struct.
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/PixelFormats/PixelImplementations/La16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace SixLabors.ImageSharp.PixelFormats
[StructLayout(LayoutKind.Explicit)]
public partial struct La16 : IPixel<La16>, IPackedVector<ushort>
{
private static readonly Vector4 MaxBytes = new Vector4(255F);
private static readonly Vector4 Half = new Vector4(0.5F);
private static readonly Vector4 MaxBytes = new(255F);
private static readonly Vector4 Half = new(0.5F);

/// <summary>
/// Gets or sets the luminance component.
Expand All @@ -35,7 +35,7 @@ public partial struct La16 : IPixel<La16>, IPackedVector<ushort>
/// Initializes a new instance of the <see cref="La16"/> struct.
/// </summary>
/// <param name="l">The luminance component.</param>
/// <param name="a">The alpha componant.</param>
/// <param name="a">The alpha component.</param>
public La16(byte l, byte a)
{
this.L = l;
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/PixelFormats/PixelImplementations/La32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public partial struct La32 : IPixel<La32>, IPackedVector<uint>
/// Initializes a new instance of the <see cref="La32"/> struct.
/// </summary>
/// <param name="l">The luminance component.</param>
/// <param name="a">The alpha componant.</param>
/// <param name="a">The alpha component.</param>
public La32(ushort l, ushort a)
{
this.L = l;
Expand Down
21 changes: 10 additions & 11 deletions src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// </summary>
public partial struct NormalizedByte2 : IPixel<NormalizedByte2>, IPackedVector<ushort>
{
private static readonly Vector2 Half = new Vector2(127);
private static readonly Vector2 MinusOne = new Vector2(-1F);
private const float MaxPos = 127F;

private static readonly Vector2 Half = new(MaxPos);
private static readonly Vector2 MinusOne = new(-1F);

/// <summary>
/// Initializes a new instance of the <see cref="NormalizedByte2"/> struct.
Expand Down Expand Up @@ -91,7 +93,7 @@ public void FromVector4(Vector4 vector)

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public readonly Vector4 ToVector4() => new Vector4(this.ToVector2(), 0F, 1F);
public readonly Vector4 ToVector4() => new(this.ToVector2(), 0F, 1F);

/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down Expand Up @@ -151,12 +153,9 @@ public void FromVector4(Vector4 vector)
/// </summary>
/// <returns>The <see cref="Vector2"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public readonly Vector2 ToVector2()
{
return new Vector2(
(sbyte)((this.PackedValue >> 0) & 0xFF) / 127F,
(sbyte)((this.PackedValue >> 8) & 0xFF) / 127F);
}
public readonly Vector2 ToVector2() => new(
(sbyte)((this.PackedValue >> 0) & 0xFF) / MaxPos,
(sbyte)((this.PackedValue >> 8) & 0xFF) / MaxPos);

/// <inheritdoc />
public override readonly bool Equals(object obj) => obj is NormalizedByte2 other && this.Equals(other);
Expand All @@ -181,8 +180,8 @@ private static ushort Pack(Vector2 vector)
{
vector = Vector2.Clamp(vector, MinusOne, Vector2.One) * Half;

int byte2 = ((ushort)Math.Round(vector.X) & 0xFF) << 0;
int byte1 = ((ushort)Math.Round(vector.Y) & 0xFF) << 8;
int byte2 = ((ushort)Convert.ToInt16(Math.Round(vector.X)) & 0xFF) << 0;
int byte1 = ((ushort)Convert.ToInt16(Math.Round(vector.Y)) & 0xFF) << 8;

return (ushort)(byte2 | byte1);
}
Expand Down
Loading

0 comments on commit 72904e1

Please sign in to comment.