Browse Source

Added IPixel.PackFromArgb32 and ToArgb32.

Added IPixel.PackFromArgb32 and ToArgb32. Also refactored Argb32 to be closer to Rgba32 using pixel component byte fields.
af/merge-core
woutware 8 years ago
parent
commit
14d9583c8c
  1. 17
      src/ImageSharp/PixelFormats/Alpha8.cs
  2. 208
      src/ImageSharp/PixelFormats/Argb32.cs
  3. 17
      src/ImageSharp/PixelFormats/Bgr24.cs
  4. 16
      src/ImageSharp/PixelFormats/Bgr565.cs
  5. 19
      src/ImageSharp/PixelFormats/Bgra32.cs
  6. 17
      src/ImageSharp/PixelFormats/Bgra4444.cs
  7. 17
      src/ImageSharp/PixelFormats/Bgra5551.cs
  8. 17
      src/ImageSharp/PixelFormats/Byte4.cs
  9. 17
      src/ImageSharp/PixelFormats/HalfSingle.cs
  10. 17
      src/ImageSharp/PixelFormats/HalfVector2.cs
  11. 17
      src/ImageSharp/PixelFormats/HalfVector4.cs
  12. 12
      src/ImageSharp/PixelFormats/IPixel.cs
  13. 22
      src/ImageSharp/PixelFormats/NormalizedByte2.cs
  14. 21
      src/ImageSharp/PixelFormats/NormalizedByte4.cs
  15. 21
      src/ImageSharp/PixelFormats/NormalizedShort2.cs
  16. 22
      src/ImageSharp/PixelFormats/NormalizedShort4.cs
  17. 42
      src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs
  18. 17
      src/ImageSharp/PixelFormats/Rg32.cs
  19. 17
      src/ImageSharp/PixelFormats/Rgb24.cs
  20. 17
      src/ImageSharp/PixelFormats/Rgba1010102.cs
  21. 26
      src/ImageSharp/PixelFormats/Rgba32.cs
  22. 17
      src/ImageSharp/PixelFormats/Rgba64.cs
  23. 17
      src/ImageSharp/PixelFormats/RgbaVector.cs
  24. 20
      src/ImageSharp/PixelFormats/Short2.cs
  25. 20
      src/ImageSharp/PixelFormats/Short4.cs

17
src/ImageSharp/PixelFormats/Alpha8.cs

@ -97,6 +97,12 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackedValue = source.A;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source) {
this.PackedValue = source.A;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -108,10 +114,13 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgba32(ref Rgba32 dest)
{
dest.R = 0;
dest.G = 0;
dest.B = 0;
dest.A = this.PackedValue;
dest.PackedValue = this.PackedValue;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
dest.PackedValue = (uint)this.PackedValue << 24;
}
/// <inheritdoc />

208
src/ImageSharp/PixelFormats/Argb32.cs

@ -1,9 +1,9 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.PixelFormats
{
@ -18,8 +18,29 @@ namespace SixLabors.ImageSharp.PixelFormats
/// This struct is fully mutable. This is done (against the guidelines) for the sake of performance,
/// as it avoids the need to create new values for modification operations.
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
public struct Argb32 : IPixel<Argb32>, IPackedVector<uint>
{
/// <summary>
/// Gets or sets the alpha component.
/// </summary>
public byte A;
/// <summary>
/// Gets or sets the red component.
/// </summary>
public byte R;
/// <summary>
/// Gets or sets the green component.
/// </summary>
public byte G;
/// <summary>
/// Gets or sets the blue component.
/// </summary>
public byte B;
/// <summary>
/// The shift count for the blue component
/// </summary>
@ -56,11 +77,13 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Argb32(byte r, byte g, byte b, byte a)
public Argb32(byte r, byte g, byte b)
{
this.PackedValue = Pack(r, g, b, a);
this.R = r;
this.G = g;
this.B = b;
this.A = 255;
}
/// <summary>
@ -69,10 +92,14 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Argb32(byte r, byte g, byte b)
public Argb32(byte r, byte g, byte b, byte a)
{
this.PackedValue = Pack(r, g, b, 255);
this.R = r;
this.G = g;
this.B = b;
this.A = a;
}
/// <summary>
@ -82,9 +109,11 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Argb32(float r, float g, float b, float a = 1)
: this()
{
this.PackedValue = Pack(r, g, b, a);
this.Pack(r, g, b, a);
}
/// <summary>
@ -93,9 +122,11 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="vector">
/// The vector containing the components for the packed vector.
/// </param>
public Argb32(Vector3 vector)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Argb32(Vector3 vector)
: this()
{
this.PackedValue = Pack(ref vector);
this.Pack(ref vector);
}
/// <summary>
@ -104,9 +135,11 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="vector">
/// The vector containing the components for the packed vector.
/// </param>
public Argb32(Vector4 vector)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Argb32(Vector4 vector)
: this()
{
this.PackedValue = Pack(ref vector);
this.Pack(ref vector);
}
/// <summary>
@ -115,84 +148,33 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="packed">
/// The packed value.
/// </param>
public Argb32(uint packed = 0)
{
this.PackedValue = packed;
}
/// <inheritdoc/>
public uint PackedValue { get; set; }
/// <summary>
/// Gets or sets the red component.
/// </summary>
public byte R
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return (byte)(this.PackedValue >> RedShift);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.PackedValue = this.PackedValue & 0xFF00FFFF | (uint)value << RedShift;
}
}
/// <summary>
/// Gets or sets the green component.
/// </summary>
public byte G
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Argb32(uint packed)
: this()
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return (byte)(this.PackedValue >> GreenShift);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.PackedValue = this.PackedValue & 0xFFFF00FF | (uint)value << GreenShift;
}
this.Argb = packed;
}
/// <summary>
/// Gets or sets the blue component.
/// Gets or sets the packed representation of the Argb32 struct.
/// </summary>
public byte B
{
public uint Argb {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return (byte)(this.PackedValue >> BlueShift);
get {
return Unsafe.As<Argb32, uint>(ref this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.PackedValue = this.PackedValue & 0xFFFFFF00 | (uint)value << BlueShift;
set {
Unsafe.As<Argb32, uint>(ref this) = value;
}
}
/// <summary>
/// Gets or sets the alpha component.
/// </summary>
public byte A
/// <inheritdoc/>
public uint PackedValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return (byte)(this.PackedValue >> AlphaShift);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.PackedValue = this.PackedValue & 0x00FFFFFF | (uint)value << AlphaShift;
}
get => this.Argb;
set => this.Argb = value;
}
/// <summary>
@ -210,7 +192,7 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Argb32 left, Argb32 right)
{
return left.PackedValue == right.PackedValue;
return left.Argb == right.Argb;
}
/// <summary>
@ -224,14 +206,14 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Argb32 left, Argb32 right)
{
return left.PackedValue != right.PackedValue;
return left.Argb != right.Argb;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromVector4(Vector4 vector)
{
this.PackedValue = Pack(ref vector);
this.Pack(ref vector);
}
/// <inheritdoc />
@ -265,6 +247,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackedValue = Pack(source.R, source.G, source.B, source.A);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackedValue = source.PackedValue;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -284,6 +273,12 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = this.A;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
dest = this;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBgr24(ref Bgr24 dest)
@ -313,7 +308,16 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Argb32 other)
{
return this.PackedValue == other.PackedValue;
return this.Argb == other.Argb;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return $"({this.R},{this.G},{this.B},{this.A})";
}
/// <inheritdoc/>
@ -321,7 +325,17 @@ namespace SixLabors.ImageSharp.PixelFormats
public override int GetHashCode()
{
// ReSharper disable once NonReadonlyMemberInGetHashCode
return this.PackedValue.GetHashCode();
return this.Argb.GetHashCode();
}
/// <summary>
/// Gets the <see cref="Vector4"/> representation without normalizing to [0, 1]
/// </summary>
/// <returns>A <see cref="Vector4"/> of values in [0, 255] </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Vector4 ToByteScaledVector4()
{
return new Vector4(this.R, this.G, this.B, this.A);
}
/// <summary>
@ -333,53 +347,51 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="w">The w-component</param>
/// <returns>The <see cref="uint"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint Pack(float x, float y, float z, float w)
private static uint Pack(byte x, byte y, byte z, byte w)
{
var value = new Vector4(x, y, z, w);
return Pack(ref value);
return (uint)(x << RedShift | y << GreenShift | z << BlueShift | w << AlphaShift);
}
/// <summary>
/// Packs the four floats into a <see cref="uint"/>.
/// Packs the four floats into a color.
/// </summary>
/// <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>
/// <returns>The <see cref="uint"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint Pack(byte x, byte y, byte z, byte w)
private void Pack(float x, float y, float z, float w)
{
return (uint)(x << RedShift | y << GreenShift | z << BlueShift | w << AlphaShift);
var value = new Vector4(x, y, z, w);
this.Pack(ref value);
}
/// <summary>
/// Packs a <see cref="Vector3"/> into a uint.
/// </summary>
/// <param name="vector">The vector containing the values to pack.</param>
/// <returns>The <see cref="uint"/> containing the packed values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint Pack(ref Vector3 vector)
private void Pack(ref Vector3 vector)
{
var value = new Vector4(vector, 1);
return Pack(ref value);
this.Pack(ref value);
}
/// <summary>
/// Packs a <see cref="Vector4"/> into a uint.
/// Packs a <see cref="Vector4"/> into a color.
/// </summary>
/// <param name="vector">The vector containing the values to pack.</param>
/// <returns>The <see cref="uint"/> containing the packed values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint Pack(ref Vector4 vector)
private void Pack(ref Vector4 vector)
{
vector *= MaxBytes;
vector += Half;
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
return (uint)(((byte)vector.X << RedShift)
| ((byte)vector.Y << GreenShift)
| ((byte)vector.Z << BlueShift)
| (byte)vector.W << AlphaShift);
this.R = (byte)vector.X;
this.G = (byte)vector.Y;
this.B = (byte)vector.Z;
this.A = (byte)vector.W;
}
}
}

17
src/ImageSharp/PixelFormats/Bgr24.cs

@ -82,6 +82,14 @@ namespace SixLabors.ImageSharp.PixelFormats
this = source.Bgr;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source) {
R = source.R;
G = source.G;
B = source.B;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromScaledVector4(Vector4 vector)
@ -131,6 +139,15 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = 255;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = 255;
}
/// <inheritdoc/>
public void ToBgr24(ref Bgr24 dest)
{

16
src/ImageSharp/PixelFormats/Bgr565.cs

@ -120,6 +120,12 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source) {
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -141,6 +147,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToVector4() * 255F;
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 ToBgr24(ref Bgr24 dest)

19
src/ImageSharp/PixelFormats/Bgra32.cs

@ -159,6 +159,16 @@ namespace SixLabors.ImageSharp.PixelFormats
this.A = source.A;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.R = source.R;
this.G = source.G;
this.B = source.B;
this.A = source.A;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -178,6 +188,15 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = this.A;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = this.A;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBgr24(ref Bgr24 dest)

17
src/ImageSharp/PixelFormats/Bgra4444.cs

@ -111,6 +111,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -132,6 +139,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToVector4() * 255F;
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 ToBgr24(ref Bgr24 dest)

17
src/ImageSharp/PixelFormats/Bgra5551.cs

@ -111,6 +111,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -132,6 +139,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
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 ToBgr24(ref Bgr24 dest)

17
src/ImageSharp/PixelFormats/Byte4.cs

@ -112,6 +112,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToByteScaledVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackFromVector4(source.ToByteScaledVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -133,6 +140,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
var vector = this.ToVector4();
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 ToBgr24(ref Bgr24 dest)

17
src/ImageSharp/PixelFormats/HalfSingle.cs

@ -125,6 +125,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -146,6 +153,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
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 ToBgr24(ref Bgr24 dest)

17
src/ImageSharp/PixelFormats/HalfVector2.cs

@ -140,6 +140,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -161,6 +168,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = 0;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBgr24(ref Bgr24 dest)

17
src/ImageSharp/PixelFormats/HalfVector4.cs

@ -133,6 +133,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -154,6 +161,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
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 ToBgr24(ref Bgr24 dest)

12
src/ImageSharp/PixelFormats/IPixel.cs

@ -59,6 +59,12 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="source">The <see cref="Rgba32"/> value.</param>
void PackFromRgba32(Rgba32 source);
/// <summary>
/// Packs the pixel from an <see cref="Argb32"/> value.
/// </summary>
/// <param name="source">The <see cref="Argb32"/> value.</param>
void PackFromArgb32(Argb32 source);
/// <summary>
/// Converts the pixel to <see cref="Rgb24"/> format.
/// </summary>
@ -71,6 +77,12 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="dest">The destination pixel to write to</param>
void ToRgba32(ref Rgba32 dest);
/// <summary>
/// Converts the pixel to <see cref="Argb32"/> format.
/// </summary>
/// <param name="dest">The destination pixel to write to</param>
void ToArgb32(ref Argb32 dest);
/// <summary>
/// Converts the pixel to <see cref="Bgr24"/> format.
/// </summary>

22
src/ImageSharp/PixelFormats/NormalizedByte2.cs

@ -149,6 +149,18 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(vector);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
Vector4 vector = source.ToByteScaledVector4();
vector -= Round;
vector -= Half;
vector -= Round;
vector /= Half;
this.PackFromVector4(vector);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -170,6 +182,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
dest.R = (byte)vector.X;
dest.G = (byte)vector.Y;
dest.B = 0;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBgr24(ref Bgr24 dest)

21
src/ImageSharp/PixelFormats/NormalizedByte4.cs

@ -142,6 +142,17 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(vector);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source) {
Vector4 vector = source.ToByteScaledVector4();
vector -= Round;
vector -= Half;
vector -= Round;
vector /= Half;
this.PackFromVector4(vector);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -163,6 +174,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
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 ToBgr24(ref Bgr24 dest)

21
src/ImageSharp/PixelFormats/NormalizedShort2.cs

@ -136,6 +136,17 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(vector);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source) {
Vector4 vector = source.ToByteScaledVector4();
vector -= Round;
vector -= Half;
vector -= Round;
vector /= Half;
this.PackFromVector4(vector);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -157,6 +168,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
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 ToBgr24(ref Bgr24 dest)

22
src/ImageSharp/PixelFormats/NormalizedShort4.cs

@ -144,6 +144,18 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(vector);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
Vector4 vector = source.ToByteScaledVector4();
vector -= Round;
vector -= Half;
vector -= Round;
vector /= Half;
this.PackFromVector4(vector);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -165,6 +177,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
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 ToBgr24(ref Bgr24 dest)

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

@ -63,6 +63,48 @@ namespace SixLabors.ImageSharp.PixelFormats
}
}
/// <summary>
/// Bulk version of <see cref="IPixel.PackFromArgb32(Argb32)"/>
/// </summary>
/// <param name="sourceVectors">The <see cref="Span{T}"/> to the source vectors.</param>
/// <param name="destinationColors">The <see cref="Span{T}"/> to the destination colors.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromArgb32(ReadOnlySpan<Argb32> sourceVectors, Span<TPixel> destinationColors, int count)
{
GuardSpans(sourceVectors, nameof(sourceVectors), destinationColors, nameof(destinationColors), count);
ref Argb32 sourceRef = ref MemoryMarshal.GetReference(sourceVectors);
ref TPixel destRef = ref MemoryMarshal.GetReference(destinationColors);
for (int i = 0; i < count; i++)
{
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i);
ref TPixel dp = ref Unsafe.Add(ref destRef, i);
dp.PackFromArgb32(sp);
}
}
/// <summary>
/// Bulk version of <see cref="IPixel.ToArgb32()"/>.
/// </summary>
/// <param name="sourceColors">The <see cref="Span{T}"/> to the source colors.</param>
/// <param name="destinationVectors">The <see cref="Span{T}"/> to the destination vectors.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void ToArgb32(ReadOnlySpan<TPixel> sourceColors, Span<Argb32> destinationVectors, int count)
{
GuardSpans(sourceColors, nameof(sourceColors), destinationVectors, nameof(destinationVectors), count);
ref TPixel sourceRef = ref MemoryMarshal.GetReference(sourceColors);
ref Argb32 destRef = ref MemoryMarshal.GetReference(destinationVectors);
for (int i = 0; i < count; i++)
{
ref TPixel sp = ref Unsafe.Add(ref sourceRef, i);
ref Argb32 dp = ref Unsafe.Add(ref destRef, i);
sp.ToArgb32(ref dp);
}
}
/// <summary>
/// Bulk version of <see cref="IPixel.PackFromScaledVector4(Vector4)"/>
/// </summary>

17
src/ImageSharp/PixelFormats/Rg32.cs

@ -124,6 +124,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -145,6 +152,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)vector.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
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 ToBgr24(ref Bgr24 dest)

17
src/ImageSharp/PixelFormats/Rgb24.cs

@ -83,6 +83,15 @@ namespace SixLabors.ImageSharp.PixelFormats
this = Unsafe.As<Rgba32, Rgb24>(ref source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
R = source.R;
G = source.G;
B = source.B;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromScaledVector4(Vector4 vector)
@ -127,6 +136,14 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = 255;
}
/// <inheritdoc/>
public void ToArgb32(ref Argb32 dest) {
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = 255;
}
/// <inheritdoc/>
public void ToBgr24(ref Bgr24 dest)
{

17
src/ImageSharp/PixelFormats/Rgba1010102.cs

@ -118,6 +118,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -139,6 +146,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToVector4() * 255F;
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 ToBgr24(ref Bgr24 dest)

26
src/ImageSharp/PixelFormats/Rgba32.cs

@ -275,6 +275,12 @@ namespace SixLabors.ImageSharp.PixelFormats
this = source;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source) {
Pack(source.R, source.G, source.B, source.A);
}
/// <summary>
/// Converts the value of this instance to a hexadecimal string.
/// </summary>
@ -299,6 +305,15 @@ namespace SixLabors.ImageSharp.PixelFormats
dest = this;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = this.A;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBgr24(ref Bgr24 dest)
@ -380,16 +395,11 @@ namespace SixLabors.ImageSharp.PixelFormats
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
unchecked
{
int hashCode = this.R;
hashCode = (hashCode * 397) ^ this.G;
hashCode = (hashCode * 397) ^ this.B;
hashCode = (hashCode * 397) ^ this.A;
return hashCode;
}
// ReSharper disable once NonReadonlyMemberInGetHashCode
return this.Rgba.GetHashCode();
}
/// <summary>

17
src/ImageSharp/PixelFormats/Rgba64.cs

@ -117,6 +117,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackFromVector4(source.ToVector4());
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -138,6 +145,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToVector4() * 255F;
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 ToBgr24(ref Bgr24 dest)

17
src/ImageSharp/PixelFormats/RgbaVector.cs

@ -218,6 +218,13 @@ namespace SixLabors.ImageSharp.PixelFormats
this.backingVector = source.ToVector4();
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.backingVector = source.ToVector4();
}
/// <summary>
/// Converts the value of this instance to a hexadecimal string.
/// </summary>
@ -252,6 +259,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
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 ToBgr24(ref Bgr24 dest)

20
src/ImageSharp/PixelFormats/Short2.cs

@ -134,6 +134,16 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackedValue = Pack(vector.X, vector.Y);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
Vector2 vector = new Vector2(source.R, source.G) / 255;
vector *= 65534;
vector -= new Vector2(32767);
this.PackedValue = Pack(vector.X, vector.Y);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -155,6 +165,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector2 vector = this.ToByteScaledVector2();
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 ToBgr24(ref Bgr24 dest)

20
src/ImageSharp/PixelFormats/Short4.cs

@ -140,6 +140,16 @@ namespace SixLabors.ImageSharp.PixelFormats
this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
var vector = source.ToVector4();
vector *= 65534;
vector -= new Vector4(32767);
this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
@ -161,6 +171,16 @@ namespace SixLabors.ImageSharp.PixelFormats
dest.A = (byte)MathF.Round(vector.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest) {
Vector4 vector = this.ToByteScaledVector4();
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 ToBgr24(ref Bgr24 dest)

Loading…
Cancel
Save