Browse Source

cleanup & fix stuff

af/merge-core
Anton Firszov 9 years ago
parent
commit
aba73e3167
  1. 32
      src/ImageSharp/PixelFormats/Alpha8.cs
  2. 36
      src/ImageSharp/PixelFormats/Argb32.cs
  3. 35
      src/ImageSharp/PixelFormats/Bgr24.cs
  4. 36
      src/ImageSharp/PixelFormats/Bgr565.cs
  5. 74
      src/ImageSharp/PixelFormats/Bgra32.cs
  6. 36
      src/ImageSharp/PixelFormats/Bgra4444.cs
  7. 47
      src/ImageSharp/PixelFormats/Bgra5551.cs
  8. 36
      src/ImageSharp/PixelFormats/Byte4.cs
  9. 70
      src/ImageSharp/PixelFormats/HalfSingle.cs
  10. 70
      src/ImageSharp/PixelFormats/HalfVector2.cs
  11. 70
      src/ImageSharp/PixelFormats/HalfVector4.cs
  12. 37
      src/ImageSharp/PixelFormats/IPixel.cs
  13. 80
      src/ImageSharp/PixelFormats/NormalizedByte2.cs
  14. 80
      src/ImageSharp/PixelFormats/NormalizedByte4.cs
  15. 80
      src/ImageSharp/PixelFormats/NormalizedShort2.cs
  16. 80
      src/ImageSharp/PixelFormats/NormalizedShort4.cs
  17. 76
      src/ImageSharp/PixelFormats/PixelConversionExtensions.cs
  18. 8
      src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs
  19. 51
      src/ImageSharp/PixelFormats/Rg32.cs
  20. 35
      src/ImageSharp/PixelFormats/Rgb24.cs
  21. 40
      src/ImageSharp/PixelFormats/Rgba1010102.cs
  22. 39
      src/ImageSharp/PixelFormats/Rgba32.cs
  23. 40
      src/ImageSharp/PixelFormats/Rgba64.cs
  24. 59
      src/ImageSharp/PixelFormats/RgbaVector.cs
  25. 80
      src/ImageSharp/PixelFormats/Short2.cs
  26. 80
      src/ImageSharp/PixelFormats/Short4.cs
  27. 6
      src/Shared/stylecop.json

32
src/ImageSharp/PixelFormats/Alpha8.cs

@ -87,40 +87,36 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
bytes[startIndex] = 0;
bytes[startIndex + 1] = 0;
bytes[startIndex + 2] = 0;
dest = default(Rgb24);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
bytes[startIndex] = 0;
bytes[startIndex + 1] = 0;
bytes[startIndex + 2] = 0;
bytes[startIndex + 3] = this.PackedValue;
dest.R = 0;
dest.G = 0;
dest.B = 0;
dest.A = this.PackedValue;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
bytes[startIndex] = 0;
bytes[startIndex + 1] = 0;
bytes[startIndex + 2] = 0;
dest = default(Bgr24);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
bytes[startIndex] = 0;
bytes[startIndex + 1] = 0;
bytes[startIndex + 2] = 0;
bytes[startIndex + 3] = this.PackedValue;
dest.R = 0;
dest.G = 0;
dest.B = 0;
dest.A = this.PackedValue;
}
/// <summary>

36
src/ImageSharp/PixelFormats/Argb32.cs

@ -255,40 +255,40 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
bytes[startIndex] = this.R;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.B;
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
bytes[startIndex] = this.R;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.B;
bytes[startIndex + 3] = this.A;
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = this.A;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
bytes[startIndex] = this.B;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.R;
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
bytes[startIndex] = this.B;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.R;
bytes[startIndex + 3] = this.A;
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = this.A;
}
/// <inheritdoc/>

35
src/ImageSharp/PixelFormats/Bgr24.cs

@ -5,24 +5,34 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
/// <summary>
/// Pixel type containing three 8-bit unsigned normalized values ranging from 0 to 255.
/// The color components are stored in blue, green, red order.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Bgr24 : IPixel<Bgr24>
{
/// <summary>
/// Gets or sets the blue component.
/// The blue component.
/// </summary>
public byte B;
/// <summary>
/// Gets or sets the green component.
/// The green component.
/// </summary>
public byte G;
/// <summary>
/// Gets or sets the red component.
/// The red component.
/// </summary>
public byte R;
/// <summary>
/// Initializes a new instance of the <see cref="Bgr24"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Bgr24(byte r, byte g, byte b)
{
@ -31,19 +41,23 @@
this.B = b;
}
/// <inheritdoc/>
public PixelOperations<Bgr24> CreatePixelOperations() => new PixelOperations<Bgr24>();
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Bgr24 other)
{
return this.R == other.R && this.G == other.G && this.B == other.B;
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return obj?.GetType() == typeof(Bgr24) && this.Equals((Bgr24)obj);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
@ -56,37 +70,44 @@
}
}
/// <inheritdoc/>
public void PackFromBytes(byte x, byte y, byte z, byte w)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public void PackFromVector4(Vector4 vector)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public Vector4 ToVector4()
{
throw new NotImplementedException();
}
public void ToXyzBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToRgb24(ref Rgb24 dest)
{
throw new NotImplementedException();
}
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToRgba32(ref Rgba32 dest)
{
throw new NotImplementedException();
}
public void ToZyxBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToBgr24(ref Bgr24 dest)
{
throw new NotImplementedException();
}
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToBgra32(ref Bgra32 dest)
{
throw new NotImplementedException();
}

36
src/ImageSharp/PixelFormats/Bgr565.cs

@ -110,44 +110,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />

74
src/ImageSharp/PixelFormats/Bgra32.cs

@ -7,6 +7,10 @@
using ImageSharp.PixelFormats;
/// <summary>
/// Packed pixel type containing four 8-bit unsigned normalized values ranging from 0 to 255.
/// The color components are stored in blue, green, red, and alpha order.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Bgra32 : IPixel<Bgra32>, IPackedVector<uint>
{
@ -45,7 +49,6 @@
this.A = 255;
}
/// <summary>
/// Initializes a new instance of the <see cref="Bgra32"/> struct.
/// </summary>
@ -62,15 +65,44 @@
this.A = a;
}
/// <summary>
/// Gets or sets the packed representation of the Bgra32 struct.
/// </summary>
public uint Bgra
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return Unsafe.As<Bgra32, uint>(ref this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
Unsafe.As<Bgra32, uint>(ref this) = value;
}
}
/// <inheritdoc/>
public uint PackedValue
{
get => this.Bgra;
set => this.Bgra = value;
}
/// <inheritdoc/>
public PixelOperations<Bgra32> CreatePixelOperations() => new PixelOperations<Bgra32>();
/// <inheritdoc/>
public bool Equals(Bgra32 other)
{
return this.R == other.R && this.G == other.G && this.B == other.B && this.A == other.A;
}
/// <inheritdoc/>
public override bool Equals(object obj) => obj?.GetType() == typeof(Bgra32) && this.Equals((Bgra32)obj);
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
@ -83,64 +115,46 @@
}
}
/// <inheritdoc/>
public void PackFromVector4(Vector4 vector)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public Vector4 ToVector4()
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public void PackFromBytes(byte x, byte y, byte z, byte w)
{
throw new NotImplementedException();
}
public void ToXyzBytes(Span<byte> bytes, int startIndex)
{
throw new NotImplementedException();
}
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToRgb24(ref Rgb24 dest)
{
throw new NotImplementedException();
}
public void ToZyxBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToRgba32(ref Rgba32 dest)
{
throw new NotImplementedException();
}
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToBgr24(ref Bgr24 dest)
{
throw new NotImplementedException();
}
/// <summary>
/// Gets or sets the packed representation of the Bgra32 struct.
/// </summary>
public uint Bgra
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return Unsafe.As<Bgra32, uint>(ref this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
Unsafe.As<Bgra32, uint>(ref this) = value;
}
}
/// <inheritdoc/>
public uint PackedValue
public void ToBgra32(ref Bgra32 dest)
{
get => this.Bgra;
set => this.Bgra = value;
throw new NotImplementedException();
}
}
}

36
src/ImageSharp/PixelFormats/Bgra4444.cs

@ -101,44 +101,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
bytes[startIndex + 3] = (byte)vector.W;
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = (byte)vector.W;
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />

47
src/ImageSharp/PixelFormats/Bgra5551.cs

@ -101,44 +101,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
dest.A = (byte)vector.W;
}
/// <inheritdoc />
@ -190,5 +190,8 @@ namespace ImageSharp.PixelFormats
(((int)Math.Round(z.Clamp(0, 1) * 31F) & 0x1F) << 0) |
(((int)Math.Round(w.Clamp(0, 1)) & 0x1) << 15));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4() => this.ToVector4() * 255f;
}
}

36
src/ImageSharp/PixelFormats/Byte4.cs

@ -102,44 +102,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4();
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4();
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
bytes[startIndex + 3] = (byte)vector.W;
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4();
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4();
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = (byte)vector.W;
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
dest.A = (byte)vector.W;
}
/// <inheritdoc />

70
src/ImageSharp/PixelFormats/HalfSingle.cs

@ -111,60 +111,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
@ -192,5 +176,15 @@ namespace ImageSharp.PixelFormats
{
return this.PackedValue.GetHashCode();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4()
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
return vector;
}
}
}

70
src/ImageSharp/PixelFormats/HalfVector2.cs

@ -125,60 +125,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
dest.A = 255;
}
/// <inheritdoc />
@ -220,5 +204,15 @@ namespace ImageSharp.PixelFormats
uint num = (uint)(HalfTypeHelper.Pack(y) << 0x10);
return num2 | num;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4()
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
return vector;
}
}
}

70
src/ImageSharp/PixelFormats/HalfVector4.cs

@ -118,60 +118,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
dest.A = (byte)vector.W;
}
/// <inheritdoc />
@ -214,5 +198,15 @@ namespace ImageSharp.PixelFormats
ulong num1 = (ulong)HalfTypeHelper.Pack(vector.W) << 0x30;
return num4 | num3 | num2 | num1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4()
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
return vector;
}
}
}

37
src/ImageSharp/PixelFormats/IPixel.cs

@ -44,42 +44,37 @@ namespace ImageSharp.PixelFormats
/// <summary>
/// Sets the packed representation from the given byte array.
/// </summary>
/// <remarks>
/// TODO: Consider defining multiple PackFrom***() methods instead. (Similar to the opposite direction API.)
/// </remarks>
/// <param name="x">The x-component.</param>
/// <param name="y">The y-component.</param>
/// <param name="z">The z-component.</param>
/// <param name="w">The w-component.</param>
void PackFromBytes(byte x, byte y, byte z, byte w);
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to X-> Y-> Z order. Equivalent to R-> G-> B in <see cref="Rgba32"/>
/// Converts the pixel to <see cref="Rgb24"/> format.
/// </summary>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
void ToXyzBytes(Span<byte> bytes, int startIndex);
/// <param name="dest">The destination pixel to write to</param>
void ToRgb24(ref Rgb24 dest);
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to X-> Y-> Z-> W order. Equivalent to R-> G-> B-> A in <see cref="Rgba32"/>
/// Converts the pixel to <see cref="Rgba32"/> format.
/// </summary>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
void ToXyzwBytes(Span<byte> bytes, int startIndex);
/// <param name="dest">The destination pixel to write to</param>
void ToRgba32(ref Rgba32 dest);
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to Z-> Y-> X order. Equivalent to B-> G-> R in <see cref="Rgba32"/>
/// Converts the pixel to <see cref="Bgr24"/> format.
/// </summary>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
void ToZyxBytes(Span<byte> bytes, int startIndex);
/// <param name="dest">The destination pixel to write to</param>
void ToBgr24(ref Bgr24 dest);
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to Z-> Y-> X-> W order. Equivalent to B-> G-> R-> A in <see cref="Rgba32"/>
/// Converts the pixel to <see cref="Bgra32"/> format.
/// </summary>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
void ToZyxwBytes(Span<byte> bytes, int startIndex);
/// <param name="dest">The destination pixel to write to</param>
void ToBgra32(ref Bgra32 dest);
}
}

80
src/ImageSharp/PixelFormats/NormalizedByte2.cs

@ -134,68 +134,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = 0;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = 0;
bytes[startIndex + 3] = 255;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = 0;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = 0;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = 255;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
dest.A = 255;
}
/// <inheritdoc />
@ -238,5 +214,17 @@ namespace ImageSharp.PixelFormats
return (ushort)(byte2 | byte1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4()
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
return vector;
}
}
}

80
src/ImageSharp/PixelFormats/NormalizedByte4.cs

@ -127,68 +127,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
@ -235,5 +211,17 @@ namespace ImageSharp.PixelFormats
return byte4 | byte3 | byte2 | byte1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4()
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
return vector;
}
}
}

80
src/ImageSharp/PixelFormats/NormalizedShort2.cs

@ -121,68 +121,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = 0;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = 0;
bytes[startIndex + 3] = 255;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = 0;
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = 0;
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
bytes[startIndex + 3] = 255;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
dest.A = 255;
}
/// <summary>
@ -245,5 +221,17 @@ namespace ImageSharp.PixelFormats
return word2 | word1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4()
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
return vector;
}
}
}

80
src/ImageSharp/PixelFormats/NormalizedShort4.cs

@ -129,68 +129,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
@ -241,5 +217,17 @@ namespace ImageSharp.PixelFormats
return word4 | word3 | word2 | word1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4()
{
Vector4 vector = this.ToVector4();
vector *= Half;
vector += Round;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
return vector;
}
}
}

76
src/ImageSharp/PixelFormats/PixelConversionExtensions.cs

@ -0,0 +1,76 @@
namespace ImageSharp.PixelFormats
{
using System;
using System.Runtime.CompilerServices;
/// <summary>
/// Extension methods for copying single pixel data into byte Spans.
/// TODO: This utility class exists for legacy reasons. Need to do a lot of chore work to remove it (mostly in test classes).
/// </summary>
internal static class PixelConversionExtensions
{
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to X-> Y-> Z order. Equivalent to R-> G-> B in <see cref="Rgb24"/>
/// </summary>
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixel">The pixel to copy the data from.</param>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToXyzBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex)
where TPixel : struct, IPixel<TPixel>
{
ref Rgb24 dest = ref Unsafe.As<byte, Rgb24>(ref bytes[startIndex]);
pixel.ToRgb24(ref dest);
}
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to X-> Y-> Z-> W order. Equivalent to R-> G-> B-> A in <see cref="Rgba32"/>
/// </summary>
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixel">The pixel to copy the data from.</param>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToXyzwBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex)
where TPixel : struct, IPixel<TPixel>
{
ref Rgba32 dest = ref Unsafe.As<byte, Rgba32>(ref bytes[startIndex]);
pixel.ToRgba32(ref dest);
}
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to Z-> Y-> X order. Equivalent to B-> G-> R in <see cref="Bgr24"/>
/// </summary>
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixel">The pixel to copy the data from.</param>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToZyxBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex)
where TPixel : struct, IPixel<TPixel>
{
ref Bgr24 dest = ref Unsafe.As<byte, Bgr24>(ref bytes[startIndex]);
pixel.ToBgr24(ref dest);
}
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to Z-> Y-> X-> W order. Equivalent to B-> G-> R-> A in <see cref="Bgra32"/>
/// </summary>
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixel">The pixel to copy the data from.</param>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToZyxwBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex)
where TPixel : struct, IPixel<TPixel>
{
ref Bgra32 dest = ref Unsafe.As<byte, Bgra32>(ref bytes[startIndex]);
pixel.ToBgra32(ref dest);
}
}
}

8
src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs

@ -93,7 +93,7 @@ namespace ImageSharp.PixelFormats
}
/// <summary>
/// Bulk version of <see cref="IPixel.ToXyzBytes(Span{byte}, int)"/>.
/// Bulk version of <see cref="PixelConversionExtensions.ToXyzBytes{TPixel}(TPixel, Span{byte}, int)"/>.
/// </summary>
/// <param name="sourceColors">The <see cref="Span{T}"/> to the source colors.</param>
/// <param name="destBytes">The <see cref="Span{T}"/> to the destination bytes.</param>
@ -139,7 +139,7 @@ namespace ImageSharp.PixelFormats
}
/// <summary>
/// Bulk version of <see cref="IPixel.ToXyzwBytes(Span{byte}, int)"/>
/// Bulk version of <see cref="PixelConversionExtensions.ToXyzwBytes{TPixel}(TPixel, Span{byte}, int)"/>
/// </summary>
/// <param name="sourceColors">The <see cref="Span{T}"/> to the source colors.</param>
/// <param name="destBytes">The <see cref="Span{T}"/> to the destination bytes.</param>
@ -185,7 +185,7 @@ namespace ImageSharp.PixelFormats
}
/// <summary>
/// Bulk version of <see cref="IPixel.ToZyxBytes(Span{byte}, int)"/>.
/// Bulk version of <see cref="PixelConversionExtensions.ToZyxBytes{TPixel}(TPixel, Span{byte}, int)"/>.
/// </summary>
/// <param name="sourceColors">The <see cref="Span{T}"/> to the source colors.</param>
/// <param name="destBytes">The <see cref="Span{T}"/> to the destination bytes.</param>
@ -231,7 +231,7 @@ namespace ImageSharp.PixelFormats
}
/// <summary>
/// Bulk version of <see cref="IPixel.ToZyxwBytes(Span{byte}, int)"/>.
/// Bulk version of <see cref="PixelConversionExtensions.ToZyxwBytes{TPixel}(TPixel, Span{byte}, int)"/>.
/// </summary>
/// <param name="sourceColors">The <see cref="Span{T}"/> to the source colors.</param>
/// <param name="destBytes">The <see cref="Span{T}"/> to the destination bytes.</param>

51
src/ImageSharp/PixelFormats/Rg32.cs

@ -114,48 +114,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = (byte)vector.Z;
dest.A = (byte)vector.W;
}
/// <inheritdoc />
@ -197,5 +193,8 @@ namespace ImageSharp.PixelFormats
((int)Math.Round(x.Clamp(0, 1) * 65535F) & 0xFFFF) |
(((int)Math.Round(y.Clamp(0, 1) * 65535F) & 0xFFFF) << 16));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4() => this.ToVector4() * 255f;
}
}

35
src/ImageSharp/PixelFormats/Rgb24.cs

@ -5,24 +5,34 @@ namespace ImageSharp.PixelFormats
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
/// <summary>
/// Pixel type containing three 8-bit unsigned normalized values ranging from 0 to 255.
/// The color components are stored in red, green, blue order.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Rgb24 : IPixel<Rgb24>
{
/// <summary>
/// Gets or sets the red component.
/// The red component.
/// </summary>
public byte R;
/// <summary>
/// Gets or sets the green component.
/// The green component.
/// </summary>
public byte G;
/// <summary>
/// Gets or sets the blue component.
/// The blue component.
/// </summary>
public byte B;
/// <summary>
/// Initializes a new instance of the <see cref="Rgb24"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Rgb24(byte r, byte g, byte b)
{
@ -31,19 +41,23 @@ namespace ImageSharp.PixelFormats
this.B = b;
}
/// <inheritdoc/>
public PixelOperations<Rgb24> CreatePixelOperations() => new PixelOperations<Rgb24>();
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Rgb24 other)
{
return this.R == other.R && this.G == other.G && this.B == other.B;
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return obj?.GetType() == typeof(Rgb24) && this.Equals((Rgb24)obj);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
@ -56,37 +70,44 @@ namespace ImageSharp.PixelFormats
}
}
/// <inheritdoc/>
public void PackFromBytes(byte x, byte y, byte z, byte w)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public void PackFromVector4(Vector4 vector)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public Vector4 ToVector4()
{
throw new NotImplementedException();
}
public void ToXyzBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToRgb24(ref Rgb24 dest)
{
throw new NotImplementedException();
}
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToRgba32(ref Rgba32 dest)
{
throw new NotImplementedException();
}
public void ToZyxBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToBgr24(ref Bgr24 dest)
{
throw new NotImplementedException();
}
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
/// <inheritdoc/>
public void ToBgra32(ref Bgra32 dest)
{
throw new NotImplementedException();
}

40
src/ImageSharp/PixelFormats/Rgba1010102.cs

@ -108,48 +108,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />

39
src/ImageSharp/PixelFormats/Rgba32.cs

@ -255,42 +255,37 @@ namespace ImageSharp
return hexOrder.ToString("X8");
}
/// <inheritdoc/>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
bytes[startIndex] = this.R;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.B;
dest = Unsafe.As<Rgba32, Rgb24>(ref this);
}
/// <inheritdoc/>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
bytes[startIndex] = this.R;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.B;
bytes[startIndex + 3] = this.A;
dest = this;
}
/// <inheritdoc/>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
bytes[startIndex] = this.B;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.R;
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
}
/// <inheritdoc/>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
bytes[startIndex] = this.B;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.R;
bytes[startIndex + 3] = this.A;
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = this.A;
}
/// <inheritdoc/>

40
src/ImageSharp/PixelFormats/Rgba64.cs

@ -107,48 +107,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4() * 255F;
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />

59
src/ImageSharp/PixelFormats/RgbaVector.cs

@ -233,50 +233,46 @@ namespace ImageSharp.PixelFormats
return hexOrder.ToString("X8");
}
/// <inheritdoc/>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes;
vector += Half;
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc/>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes;
vector += Half;
bytes[startIndex] = (byte)vector.X;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.Z;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc/>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes;
vector += Half;
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc/>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes;
vector += Half;
bytes[startIndex] = (byte)vector.Z;
bytes[startIndex + 1] = (byte)vector.Y;
bytes[startIndex + 2] = (byte)vector.X;
bytes[startIndex + 3] = (byte)vector.W;
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc/>
@ -320,5 +316,8 @@ namespace ImageSharp.PixelFormats
{
return this.backingVector.GetHashCode();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4() => Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes;
}
}

80
src/ImageSharp/PixelFormats/Short2.cs

@ -119,68 +119,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector2 vector = this.ToVector2();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = 0;
Vector2 vector = this.ToScaledVector2();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector2 vector = this.ToVector2();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = 0;
bytes[startIndex + 3] = 255;
Vector2 vector = this.ToScaledVector2();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector2 vector = this.ToVector2();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes);
bytes[startIndex] = 0;
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
Vector2 vector = this.ToScaledVector2();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector2 vector = this.ToVector2();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes);
bytes[startIndex] = 0;
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
bytes[startIndex + 3] = 255;
Vector2 vector = this.ToScaledVector2();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = 0;
dest.A = 255;
}
/// <summary>
@ -239,5 +215,17 @@ namespace ImageSharp.PixelFormats
return word2 | word1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector2 ToScaledVector2()
{
Vector2 vector = this.ToVector2();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes);
return vector;
}
}
}

80
src/ImageSharp/PixelFormats/Short4.cs

@ -125,68 +125,44 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzBytes(Span<byte> bytes, int startIndex)
public void ToRgb24(ref Rgb24 dest)
{
Vector4 vector = this.ToVector4();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToXyzwBytes(Span<byte> bytes, int startIndex)
public void ToRgba32(ref Rgba32 dest)
{
Vector4 vector = this.ToVector4();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.X);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxBytes(Span<byte> bytes, int startIndex)
public void ToBgr24(ref Bgr24 dest)
{
Vector4 vector = this.ToVector4();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToZyxwBytes(Span<byte> bytes, int startIndex)
public void ToBgra32(ref Bgra32 dest)
{
Vector4 vector = this.ToVector4();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
bytes[startIndex] = (byte)MathF.Round(vector.Z);
bytes[startIndex + 1] = (byte)MathF.Round(vector.Y);
bytes[startIndex + 2] = (byte)MathF.Round(vector.X);
bytes[startIndex + 3] = (byte)MathF.Round(vector.W);
Vector4 vector = this.ToScaledVector4();
dest.R = (byte)MathF.Round(vector.X);
dest.G = (byte)MathF.Round(vector.Y);
dest.B = (byte)MathF.Round(vector.Z);
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
@ -246,5 +222,17 @@ namespace ImageSharp.PixelFormats
return word4 | word3 | word2 | word1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToScaledVector4()
{
Vector4 vector = this.ToVector4();
vector /= 65534;
vector *= 255;
vector += Half;
vector += Round;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
return vector;
}
}
}

6
src/Shared/stylecop.json

@ -3,11 +3,7 @@
"settings": {
"documentationRules": {
"companyName": "James Jackson-South",
"copyrightText": "Copyright (c) James Jackson-South and contributors.\nLicensed under the Apache License, Version 2.0.",
"documentInterfaces": false,
"documentInternalElements": false,
"documentExposedElements": false
"copyrightText": "Copyright (c) James Jackson-South and contributors.\nLicensed under the Apache License, Version 2.0."
}
}
}
Loading…
Cancel
Save