Browse Source

refactored ToRgba32() on most pixel types

af/merge-core
Anton Firszov 7 years ago
parent
commit
7caf6642f4
  1. 6
      src/ImageSharp/PixelFormats/Alpha8.cs
  2. 8
      src/ImageSharp/PixelFormats/Argb32.cs
  3. 8
      src/ImageSharp/PixelFormats/Bgr24.cs
  4. 5
      src/ImageSharp/PixelFormats/Bgr565.cs
  5. 8
      src/ImageSharp/PixelFormats/Bgra32.cs
  6. 5
      src/ImageSharp/PixelFormats/Bgra4444.cs
  7. 5
      src/ImageSharp/PixelFormats/Bgra5551.cs
  8. 5
      src/ImageSharp/PixelFormats/Byte4.cs
  9. 5
      src/ImageSharp/PixelFormats/HalfSingle.cs
  10. 5
      src/ImageSharp/PixelFormats/HalfVector2.cs
  11. 5
      src/ImageSharp/PixelFormats/HalfVector4.cs
  12. 18
      src/ImageSharp/PixelFormats/IPixel.cs
  13. 5
      src/ImageSharp/PixelFormats/NormalizedByte2.cs
  14. 5
      src/ImageSharp/PixelFormats/NormalizedByte4.cs
  15. 5
      src/ImageSharp/PixelFormats/NormalizedShort2.cs
  16. 5
      src/ImageSharp/PixelFormats/NormalizedShort4.cs
  17. 84
      src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs
  18. 5
      src/ImageSharp/PixelFormats/Rg32.cs
  19. 5
      src/ImageSharp/PixelFormats/Rgba1010102.cs
  20. 5
      src/ImageSharp/PixelFormats/RgbaVector.cs
  21. 5
      src/ImageSharp/PixelFormats/Short2.cs
  22. 5
      src/ImageSharp/PixelFormats/Short4.cs

6
src/ImageSharp/PixelFormats/Alpha8.cs

@ -105,7 +105,11 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(0, 0, 0, this.PackedValue);
public void ToRgba32(ref Rgba32 dest)
{
dest = default;
dest.A = this.PackedValue;
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

8
src/ImageSharp/PixelFormats/Argb32.cs

@ -250,7 +250,13 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.R, this.G, this.B, this.A);
public void ToRgba32(ref Rgba32 dest)
{
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = this.A;
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

8
src/ImageSharp/PixelFormats/Bgr24.cs

@ -151,7 +151,13 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.R, this.G, this.B, byte.MaxValue);
public void ToRgba32(ref Rgba32 dest)
{
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = byte.MaxValue;
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/Bgr565.cs

@ -113,7 +113,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

8
src/ImageSharp/PixelFormats/Bgra32.cs

@ -206,7 +206,13 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.R, this.G, this.B, this.A);
public void ToRgba32(ref Rgba32 dest)
{
dest.R = this.R;
dest.G = this.G;
dest.B = this.B;
dest.A = this.A;
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/Bgra4444.cs

@ -116,7 +116,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/Bgra5551.cs

@ -117,7 +117,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/Byte4.cs

@ -117,7 +117,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/HalfSingle.cs

@ -106,7 +106,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/HalfVector2.cs

@ -117,7 +117,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/HalfVector4.cs

@ -125,7 +125,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

18
src/ImageSharp/PixelFormats/IPixel.cs

@ -100,8 +100,8 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <summary>
/// Expands the packed representation into an <see cref="Rgba32"/>.
/// </summary>
/// <returns>The <see cref="Rgba32"/>.</returns>
Rgba32 ToRgba32();
/// <param name="dest">The reference to the destination <see cref="Rgba32"/> pixel</param>
void ToRgba32(ref Rgba32 dest);
/// <summary>
/// Packs the pixel from an <see cref="Rgb48"/> value.
@ -115,4 +115,18 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="source">The <see cref="Rgba64"/> value.</param>
void PackFromRgba64(Rgba64 source);
}
/// <summary>
/// Temporary extension methods for compatibility
/// </summary>
internal static class PixelExtensions
{
public static Rgba32 ToRgba32<TPixel>(this TPixel pixel)
where TPixel : struct, IPixel<TPixel>
{
Rgba32 result = default;
pixel.ToRgba32(ref result);
return result;
}
}
}

5
src/ImageSharp/PixelFormats/NormalizedByte2.cs

@ -107,7 +107,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/NormalizedByte4.cs

@ -128,7 +128,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/NormalizedShort2.cs

@ -123,7 +123,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/NormalizedShort4.cs

@ -130,7 +130,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

84
src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs

@ -1,31 +1,31 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.PixelFormats
{
/// <summary>
/// Abstract base class for calling pixel composition functions
/// </summary>
/// <typeparam name="TPixel">The type of the pixel</typeparam>
internal abstract class PixelBlender<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
/// Blend 2 pixels together.
/// </summary>
/// <param name="background">The background color.</param>
/// <param name="source">The source color.</param>
/// <param name="amount">
/// A value between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// </param>
/// <returns>The final pixel value after composition</returns>
using SixLabors.Memory;
namespace SixLabors.ImageSharp.PixelFormats
{
/// <summary>
/// Abstract base class for calling pixel composition functions
/// </summary>
/// <typeparam name="TPixel">The type of the pixel</typeparam>
internal abstract class PixelBlender<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
/// Blend 2 pixels together.
/// </summary>
/// <param name="background">The background color.</param>
/// <param name="source">The source color.</param>
/// <param name="amount">
/// A value between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// </param>
/// <returns>The final pixel value after composition</returns>
public abstract TPixel Blend(TPixel background, TPixel source, float amount);
/// <summary>
@ -34,9 +34,9 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="destination">destination span</param>
/// <param name="background">the background span</param>
/// <param name="source">the source span</param>
/// <param name="amount">
/// A value between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// <param name="amount">
/// A value between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// </param>
protected abstract void BlendFunction(Span<Vector4> destination, ReadOnlySpan<Vector4> background, ReadOnlySpan<Vector4> source, float amount);
@ -46,9 +46,9 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="destination">destination span</param>
/// <param name="background">the background span</param>
/// <param name="source">the source span</param>
/// <param name="amount">
/// A span with values between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// <param name="amount">
/// A span with values between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// </param>
protected abstract void BlendFunction(Span<Vector4> destination, ReadOnlySpan<Vector4> background, ReadOnlySpan<Vector4> source, ReadOnlySpan<float> amount);
@ -59,9 +59,9 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="destination">the destination span</param>
/// <param name="background">the background span</param>
/// <param name="source">the source span</param>
/// <param name="amount">
/// A span with values between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// <param name="amount">
/// A span with values between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// </param>
public void Blend(MemoryAllocator memoryManager, Span<TPixel> destination, ReadOnlySpan<TPixel> background, ReadOnlySpan<TPixel> source, ReadOnlySpan<float> amount)
{
@ -76,9 +76,9 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="destination">the destination span</param>
/// <param name="background">the background span</param>
/// <param name="source">the source span</param>
/// <param name="amount">
/// A span with values between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// <param name="amount">
/// A span with values between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// </param>
public void Blend<TPixelSrc>(MemoryAllocator memoryManager, Span<TPixel> destination, ReadOnlySpan<TPixel> background, ReadOnlySpan<TPixelSrc> source, ReadOnlySpan<float> amount)
where TPixelSrc : struct, IPixel<TPixelSrc>
@ -110,9 +110,9 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <param name="destination">the destination span</param>
/// <param name="background">the background span</param>
/// <param name="source">the source span</param>
/// <param name="amount">
/// A value between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// <param name="amount">
/// A value between 0 and 1 indicating the weight of the second source vector.
/// At amount = 0, "from" is returned, at amount = 1, "to" is returned.
/// </param>
public void Blend<TPixelSrc>(MemoryAllocator memoryManager, Span<TPixel> destination, ReadOnlySpan<TPixel> background, ReadOnlySpan<TPixelSrc> source, float amount)
where TPixelSrc : struct, IPixel<TPixelSrc>
@ -135,5 +135,5 @@ namespace SixLabors.ImageSharp.PixelFormats
PixelOperations<TPixel>.Instance.PackFromScaledVector4(destinationSpan, destination, destination.Length);
}
}
}
}
}
}

5
src/ImageSharp/PixelFormats/Rg32.cs

@ -111,7 +111,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/Rgba1010102.cs

@ -117,7 +117,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/RgbaVector.cs

@ -152,7 +152,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/Short2.cs

@ -129,7 +129,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

5
src/ImageSharp/PixelFormats/Short4.cs

@ -134,7 +134,10 @@ namespace SixLabors.ImageSharp.PixelFormats
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public Rgba32 ToRgba32() => new Rgba32(this.ToScaledVector4());
public void ToRgba32(ref Rgba32 dest)
{
dest.PackFromScaledVector4(this.ToScaledVector4());
}
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]

Loading…
Cancel
Save