Browse Source

Refine alpha conversion and AOT seeding

Introduce explicit associated/unassociated pixel conversion APIs across `IPixel` and `PixelOperations`, and rework associated-alpha operations to use representation-aware bulk hooks. Update pixel implementations, converters, blenders, and generated code to consistently apply `Premultiply`/`UnPremultiply` semantics, plus SIMD-backed conversion paths for half and signed formats.

Fix codec and processing behavior where alpha handling was incorrect or lossy (notably TIFF/EXR decode/encode, convolution/resize/filter/normalization/dithering paths), and add targeted tests for these cases. Expand AOT compiler roots (including EXR and additional pixel/processor coverage), add a new public API test project for pixel-operations extensibility, and add broad alpha-representation and quantization regression coverage.
pull/3154/head
James Jackson-South 4 months ago
parent
commit
3d3f49fab8
  1. 7
      ImageSharp.sln
  2. 12
      src/ImageSharp/Advanced/AotCompilerTools.cs
  3. 19
      src/ImageSharp/Color/Color.cs
  4. 2
      src/ImageSharp/Common/Helpers/ColorNumerics.cs
  5. 210
      src/ImageSharp/Common/Helpers/Numerics.cs
  6. 3
      src/ImageSharp/Common/Helpers/Vector128Utilities.cs
  7. 3
      src/ImageSharp/Common/Helpers/Vector256Utilities.cs
  8. 11
      src/ImageSharp/Common/Helpers/Vector512Utilities.cs
  9. 10
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  10. 4
      src/ImageSharp/Formats/EncodingUtilities.cs
  11. 9
      src/ImageSharp/Formats/Exr/ExrDecoderCore.cs
  12. 7
      src/ImageSharp/Formats/Exr/ExrEncoderCore.cs
  13. 3
      src/ImageSharp/Formats/Exr/ExrMetadata.cs
  14. 2
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  15. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs
  16. 4
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero32FloatTiffColor{TPixel}.cs
  17. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor{TPixel}.cs
  18. 4
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLab16PlanarTiffColor{TPixel}.cs
  19. 4
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLab16TiffColor{TPixel}.cs
  20. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLab8PlanarTiffColor{TPixel}.cs
  21. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLab8TiffColor{TPixel}.cs
  22. 4
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CmykTiffColor{TPixel}.cs
  23. 4
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor{TPixel}.cs
  24. 4
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb444TiffColor{TPixel}.cs
  25. 4
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbFloat323232TiffColor{TPixel}.cs
  26. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs
  27. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor{TPixel}.cs
  28. 22
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba16161616TiffColor{TPixel}.cs
  29. 21
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba8888TiffColor{TPixel}.cs
  30. 20
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaFloat32323232TiffColor{TPixel}.cs
  31. 4
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaPlanarTiffColor{TPixel}.cs
  32. 6
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaTiffColor{TPixel}.cs
  33. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
  34. 4
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero32FloatTiffColor{TPixel}.cs
  35. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor{TPixel}.cs
  36. 45
      src/ImageSharp/Formats/Tiff/Utils/TiffUtilities.cs
  37. 327
      src/ImageSharp/PixelFormats/AssociatedAlphaPixelOperations{TPixel}.cs
  38. 266
      src/ImageSharp/PixelFormats/HalfTypeHelper.cs
  39. 118
      src/ImageSharp/PixelFormats/IPixel.cs
  40. 216
      src/ImageSharp/PixelFormats/PixelBlenders/AssociatedAlphaPixelBlenders.Generated.cs
  41. 2
      src/ImageSharp/PixelFormats/PixelBlenders/AssociatedAlphaPixelBlenders.Generated.tt
  42. 16
      src/ImageSharp/PixelFormats/PixelBlenders/AssociatedAlphaPixelBlender{TPixel}.cs
  43. 216
      src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs
  44. 2
      src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.tt
  45. 216
      src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.cs
  46. 2
      src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.tt
  47. 6
      src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs
  48. 20
      src/ImageSharp/PixelFormats/PixelConversionModifiers.cs
  49. 32
      src/ImageSharp/PixelFormats/PixelImplementations/A8.cs
  50. 41
      src/ImageSharp/PixelFormats/PixelImplementations/Abgr32.cs
  51. 56
      src/ImageSharp/PixelFormats/PixelImplementations/Abgr32P.cs
  52. 41
      src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs
  53. 56
      src/ImageSharp/PixelFormats/PixelImplementations/Argb32P.cs
  54. 42
      src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs
  55. 42
      src/ImageSharp/PixelFormats/PixelImplementations/Bgr565.cs
  56. 41
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs
  57. 56
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra32P.cs
  58. 41
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs
  59. 41
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra5551.cs
  60. 52
      src/ImageSharp/PixelFormats/PixelImplementations/Byte4.cs
  61. 42
      src/ImageSharp/PixelFormats/PixelImplementations/HalfSingle.cs
  62. 43
      src/ImageSharp/PixelFormats/PixelImplementations/HalfVector2.cs
  63. 55
      src/ImageSharp/PixelFormats/PixelImplementations/HalfVector4.cs
  64. 126
      src/ImageSharp/PixelFormats/PixelImplementations/HalfVector4P.cs
  65. 42
      src/ImageSharp/PixelFormats/PixelImplementations/L16.cs
  66. 42
      src/ImageSharp/PixelFormats/PixelImplementations/L8.cs
  67. 41
      src/ImageSharp/PixelFormats/PixelImplementations/La16.cs
  68. 41
      src/ImageSharp/PixelFormats/PixelImplementations/La32.cs
  69. 43
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte2.cs
  70. 68
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte4.cs
  71. 125
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte4P.cs
  72. 43
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedShort2.cs
  73. 68
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedShort4.cs
  74. 19
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/A8.PixelOperations.cs
  75. 54
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Abgr32P.PixelOperations.cs
  76. 54
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Argb32P.PixelOperations.cs
  77. 13
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgr565.PixelOperations.cs
  78. 54
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgra32P.PixelOperations.cs
  79. 94
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Byte4.PixelOperations.cs
  80. 25
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/HalfSingle.PixelOperations.cs
  81. 25
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/HalfVector2.PixelOperations.cs
  82. 90
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/HalfVector4.PixelOperations.cs
  83. 745
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/HalfVector4P.PixelOperations.cs
  84. 13
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/L16.PixelOperations.cs
  85. 13
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/L8.PixelOperations.cs
  86. 25
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedByte2.PixelOperations.cs
  87. 90
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedByte4.PixelOperations.cs
  88. 756
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedByte4P.PixelOperations.cs
  89. 25
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedShort2.PixelOperations.cs
  90. 90
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedShort4.PixelOperations.cs
  91. 13
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Rg32.PixelOperations.cs
  92. 13
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Rgb48.PixelOperations.cs
  93. 26
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Rgb96.PixelOperations.cs
  94. 54
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Rgba32P.PixelOperations.cs
  95. 13
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/RgbaVector.PixelOperations.cs
  96. 26
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Short2.PixelOperations.cs
  97. 91
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Short4.PixelOperations.cs
  98. 42
      src/ImageSharp/PixelFormats/PixelImplementations/Rg32.cs
  99. 42
      src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs
  100. 42
      src/ImageSharp/PixelFormats/PixelImplementations/Rgb48.cs

7
ImageSharp.sln

@ -556,6 +556,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Webp", "Webp", "{983A31E2-5
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageSharp.Tests", "tests\ImageSharp.Tests\ImageSharp.Tests.csproj", "{EA3000E9-2A91-4EC4-8A68-E566DEBDC4F6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageSharp.PublicApi.Tests", "tests\ImageSharp.PublicApi.Tests\ImageSharp.PublicApi.Tests.csproj", "{7D89D21A-0F54-4B36-BEAA-2B90994E840E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageSharp.Benchmarks", "tests\ImageSharp.Benchmarks\ImageSharp.Benchmarks.csproj", "{2BF743D8-2A06-412D-96D7-F448F00C5EA5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{C0D7754B-5277-438E-ABEB-2BA34401B5A7}"
@ -682,6 +684,10 @@ Global
{EA3000E9-2A91-4EC4-8A68-E566DEBDC4F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA3000E9-2A91-4EC4-8A68-E566DEBDC4F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA3000E9-2A91-4EC4-8A68-E566DEBDC4F6}.Release|Any CPU.Build.0 = Release|Any CPU
{7D89D21A-0F54-4B36-BEAA-2B90994E840E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D89D21A-0F54-4B36-BEAA-2B90994E840E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D89D21A-0F54-4B36-BEAA-2B90994E840E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D89D21A-0F54-4B36-BEAA-2B90994E840E}.Release|Any CPU.Build.0 = Release|Any CPU
{2BF743D8-2A06-412D-96D7-F448F00C5EA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2BF743D8-2A06-412D-96D7-F448F00C5EA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BF743D8-2A06-412D-96D7-F448F00C5EA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -714,6 +720,7 @@ Global
{E1C42A6F-913B-4A7B-B1A8-2BB62843B254} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
{983A31E2-5E26-4058-BD6E-03B4922D4BBF} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
{EA3000E9-2A91-4EC4-8A68-E566DEBDC4F6} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
{7D89D21A-0F54-4B36-BEAA-2B90994E840E} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
{2BF743D8-2A06-412D-96D7-F448F00C5EA5} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
{C0D7754B-5277-438E-ABEB-2BA34401B5A7} = {1799C43E-5C54-4A8F-8D64-B1475241DB0D}
{68A8CC40-6AED-4E96-B524-31B1158FDEEA} = {815C0625-CD3D-440F-9F80-2D83856AB7AE}

12
src/ImageSharp/Advanced/AotCompilerTools.cs

@ -7,6 +7,7 @@ using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Exr;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Jpeg.Components;
@ -107,7 +108,9 @@ internal static class AotCompilerTools
Seed<Rg32>();
Seed<Rgb24>();
Seed<Rgb48>();
Seed<Rgb96>();
Seed<Rgba1010102>();
Seed<Rgba128>();
Seed<Rgba32>();
Seed<Rgba32P>();
Seed<Rgba64>();
@ -190,7 +193,9 @@ internal static class AotCompilerTools
img.CloneAs<Rg32>(default);
img.CloneAs<Rgb24>(default);
img.CloneAs<Rgb48>(default);
img.CloneAs<Rgb96>(default);
img.CloneAs<Rgba1010102>(default);
img.CloneAs<Rgba128>(default);
img.CloneAs<Rgba32>(default);
img.CloneAs<Rgba32P>(default);
img.CloneAs<Rgba64>(default);
@ -220,6 +225,7 @@ internal static class AotCompilerTools
where TPixel : unmanaged, IPixel<TPixel>
{
default(BmpEncoderCore).Encode<TPixel>(default, default, default);
default(ExrEncoderCore).Encode<TPixel>(default, default, default);
default(GifEncoderCore).Encode<TPixel>(default, default, default);
default(JpegEncoderCore).Encode<TPixel>(default, default, default);
default(PbmEncoderCore).Encode<TPixel>(default, default, default);
@ -239,6 +245,7 @@ internal static class AotCompilerTools
where TPixel : unmanaged, IPixel<TPixel>
{
default(BmpDecoderCore).Decode<TPixel>(default, default, default);
default(ExrDecoderCore).Decode<TPixel>(default, default, default);
default(GifDecoderCore).Decode<TPixel>(default, default, default);
default(JpegDecoderCore).Decode<TPixel>(default, default, default);
default(PbmDecoderCore).Decode<TPixel>(default, default, default);
@ -259,6 +266,7 @@ internal static class AotCompilerTools
{
AotCompileImageEncoder<TPixel, WebpEncoder>();
AotCompileImageEncoder<TPixel, BmpEncoder>();
AotCompileImageEncoder<TPixel, ExrEncoder>();
AotCompileImageEncoder<TPixel, GifEncoder>();
AotCompileImageEncoder<TPixel, JpegEncoder>();
AotCompileImageEncoder<TPixel, PbmEncoder>();
@ -277,6 +285,7 @@ internal static class AotCompilerTools
{
AotCompileImageDecoder<TPixel, WebpDecoder>();
AotCompileImageDecoder<TPixel, BmpDecoder>();
AotCompileImageDecoder<TPixel, ExrDecoder>();
AotCompileImageDecoder<TPixel, GifDecoder>();
AotCompileImageDecoder<TPixel, JpegDecoder>();
AotCompileImageDecoder<TPixel, PbmDecoder>();
@ -348,6 +357,7 @@ internal static class AotCompilerTools
AotCompileImageProcessor<TPixel, VignetteProcessor>();
AotCompileImageProcessor<TPixel, AdaptiveHistogramEqualizationProcessor>();
AotCompileImageProcessor<TPixel, AdaptiveHistogramEqualizationSlidingWindowProcessor>();
AotCompileImageProcessor<TPixel, AutoLevelProcessor>();
AotCompileImageProcessor<TPixel, GlobalHistogramEqualizationProcessor>();
AotCompileImageProcessor<TPixel, AchromatomalyProcessor>();
AotCompileImageProcessor<TPixel, AchromatopsiaProcessor>();
@ -380,11 +390,13 @@ internal static class AotCompilerTools
AotCompileImageProcessor<TPixel, PaletteDitherProcessor>();
AotCompileImageProcessor<TPixel, BokehBlurProcessor>();
AotCompileImageProcessor<TPixel, BoxBlurProcessor>();
AotCompileImageProcessor<TPixel, ConvolutionProcessor>();
AotCompileImageProcessor<TPixel, EdgeDetector2DProcessor>();
AotCompileImageProcessor<TPixel, EdgeDetectorCompassProcessor>();
AotCompileImageProcessor<TPixel, EdgeDetectorProcessor>();
AotCompileImageProcessor<TPixel, GaussianBlurProcessor>();
AotCompileImageProcessor<TPixel, GaussianSharpenProcessor>();
AotCompileImageProcessor<TPixel, MedianBlurProcessor>();
AotCompileImageProcessor<TPixel, AdaptiveThresholdProcessor>();
AotCompileImageProcessor<TPixel, BinaryThresholdProcessor>();

19
src/ImageSharp/Color/Color.cs

@ -106,7 +106,7 @@ public readonly partial struct Color : IEquatable<Color>
{
// Associated formats with at most eight bits per component can be canonicalized without loss by their pixel-specific conversion.
// Higher-precision formats retain their associated values because unassociation can lose representable data.
Vector4 vector = PixelOperations<TPixel>.Instance.ToUnassociatedScaledVector4(source);
Vector4 vector = source.ToUnassociatedScaledVector4();
return new Color(vector, info.AlphaRepresentation, PixelAlphaRepresentation.Unassociated);
}
@ -187,11 +187,9 @@ public readonly partial struct Color : IEquatable<Color>
if (info.AlphaRepresentation == PixelAlphaRepresentation.Associated && maximumComponentPrecision <= (int)PixelComponentBitDepth.Bit8)
{
// Match the scalar conversion by retaining exact unassociated values from the format-specific operation.
PixelOperations<TPixel> operations = PixelOperations<TPixel>.Instance;
for (int i = 0; i < source.Length; i++)
{
Vector4 vector = operations.ToUnassociatedScaledVector4(source[i]);
Vector4 vector = source[i].ToUnassociatedScaledVector4();
destination[i] = new Color(vector, info.AlphaRepresentation, PixelAlphaRepresentation.Unassociated);
}
@ -398,21 +396,14 @@ public readonly partial struct Color : IEquatable<Color>
}
Vector4 vector = this.boxedHighPrecisionPixel?.ToScaledVector4() ?? this.data;
PixelOperations<TPixel> operations = PixelOperations<TPixel>.Instance;
if (this.dataIsAssociated)
{
if (operations is AssociatedAlphaPixelOperations<TPixel> associatedOperations)
{
// Preserve associated components directly while allowing the destination to quantize alpha to its own storage grid.
return associatedOperations.FromAssociatedScaledVector4(vector);
}
Numerics.UnPremultiply(ref vector);
// Preserve associated components directly while allowing the destination to quantize alpha to its own storage grid.
return TPixel.FromAssociatedScaledVector4(vector);
}
// Unassociated input lets an associated destination quantize alpha before it multiplies the color components.
return operations.FromUnassociatedScaledVector4(vector);
return TPixel.FromUnassociatedScaledVector4(vector);
}
/// <summary>

2
src/ImageSharp/Common/Helpers/ColorNumerics.cs

@ -26,7 +26,7 @@ internal static class ColorNumerics
/// The number of luminance levels (256 for 8 bit, 65536 for 16 bit grayscale images).
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBT709Luminance(ref Vector4 vector, int luminanceLevels)
public static int GetBT709Luminance(Vector4 vector, int luminanceLevels)
=> (int)MathF.Round(Vector4.Dot(vector, Bt709) * (luminanceLevels - 1));
/// <summary>

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

@ -6,6 +6,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using SixLabors.ImageSharp.Common.Helpers;
namespace SixLabors.ImageSharp;
@ -513,7 +514,7 @@ internal static class Numerics
/// <summary>
/// Pre-multiplies the "x", "y", "z" components of a vector by its "w" component leaving the "w" component intact.
/// </summary>
/// <param name="source">The <see cref="Vector4"/> to premultiply</param>
/// <param name="source">The <see cref="Vector4"/> to premultiply.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Premultiply(ref Vector4 source)
{
@ -524,50 +525,98 @@ internal static class Numerics
}
/// <summary>
/// Bulk variant of <see cref="Premultiply(ref Vector4)"/>
/// Clamps associated color components to the alpha component while preserving alpha.
/// </summary>
/// <param name="vectors">The span of vectors</param>
/// <param name="source">The associated vector to clamp.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ClampRgbToAlpha(ref Vector4 source)
{
Vector4 alpha = PermuteW(source);
source = WithW(Vector4.Min(Vector4.Max(source, Vector4.Zero), alpha), alpha);
}
/// <summary>
/// Premultiplies the X, Y, and Z components of each vector by its W component while preserving W.
/// </summary>
/// <param name="vectors">The vectors to premultiply.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Premultiply(Span<Vector4> vectors)
{
if (Avx.IsSupported && vectors.Length >= 2)
if (Vector512.IsHardwareAccelerated)
{
int vectorsPerVector = Vector512<float>.Count / Vector128<float>.Count;
ref Vector512<float> vectorsBase = ref Unsafe.As<Vector4, Vector512<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector512<float> vectorsEnd = ref Unsafe.Add(ref vectorsBase, (uint)(vectors.Length / vectorsPerVector));
Vector128<float> alphaMask128 = Vector128.Create(0, 0, 0, -1).AsSingle();
Vector256<float> alphaMask256 = Vector256.Create(alphaMask128, alphaMask128);
Vector512<float> alphaMask = Vector512.Create(alphaMask256, alphaMask256);
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsEnd))
{
Vector512<float> source = vectorsBase;
Vector512<float> alpha = Vector512_.ShuffleNative(source, ShuffleAlphaControl);
// Multiplication also squares W, so select the original W lanes to preserve alpha bit-for-bit.
vectorsBase = Vector512.ConditionalSelect(alphaMask, source, source * alpha);
vectorsBase = ref Unsafe.Add(ref vectorsBase, 1);
}
vectors = vectors[(vectors.Length - (vectors.Length % vectorsPerVector))..];
}
if (Vector256.IsHardwareAccelerated)
{
// Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float>
int vectorsPerVector = Vector256<float>.Count / Vector128<float>.Count;
ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length / 2u);
ref Vector256<float> vectorsEnd = ref Unsafe.Add(ref vectorsBase, (uint)(vectors.Length / vectorsPerVector));
Vector128<float> alphaMask128 = Vector128.Create(0, 0, 0, -1).AsSingle();
Vector256<float> alphaMask = Vector256.Create(alphaMask128, alphaMask128);
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast))
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsEnd))
{
Vector256<float> source = vectorsBase;
Vector256<float> alpha = Avx.Permute(source, ShuffleAlphaControl);
vectorsBase = Avx.Blend(Avx.Multiply(source, alpha), source, BlendAlphaControl);
Vector256<float> alpha = Vector256_.ShuffleNative(source, ShuffleAlphaControl);
// Multiplication also squares W, so select the original W lanes to preserve alpha bit-for-bit.
vectorsBase = Vector256.ConditionalSelect(alphaMask, source, source * alpha);
vectorsBase = ref Unsafe.Add(ref vectorsBase, 1);
}
if (Modulo2(vectors.Length) != 0)
{
// Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
Premultiply(ref MemoryMarshal.GetReference(vectors[^1..]));
}
vectors = vectors[(vectors.Length - (vectors.Length % vectorsPerVector))..];
}
else
if (Vector128.IsHardwareAccelerated)
{
ref Vector4 vectorsStart = ref MemoryMarshal.GetReference(vectors);
ref Vector4 vectorsEnd = ref Unsafe.Add(ref vectorsStart, (uint)vectors.Length);
ref Vector128<float> vectorsBase = ref Unsafe.As<Vector4, Vector128<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector128<float> vectorsEnd = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length);
Vector128<float> alphaMask = Vector128.Create(0, 0, 0, -1).AsSingle();
while (Unsafe.IsAddressLessThan(ref vectorsStart, ref vectorsEnd))
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsEnd))
{
Premultiply(ref vectorsStart);
Vector128<float> source = vectorsBase;
Vector128<float> alpha = Vector128_.ShuffleNative(source, ShuffleAlphaControl);
vectorsStart = ref Unsafe.Add(ref vectorsStart, 1);
// Multiplication also squares W, so select the original W lane to preserve alpha bit-for-bit.
vectorsBase = Vector128.ConditionalSelect(alphaMask, source, source * alpha);
vectorsBase = ref Unsafe.Add(ref vectorsBase, 1);
}
return;
}
ref Vector4 vectorsStart = ref MemoryMarshal.GetReference(vectors);
for (nuint i = 0; i < (uint)vectors.Length; i++)
{
Premultiply(ref Unsafe.Add(ref vectorsStart, i));
}
}
/// <summary>
/// Reverses the result of premultiplying a vector via <see cref="Premultiply(ref Vector4)"/>.
/// When alpha is zero, the RGB components remain unchanged because no unassociated value can be recovered.
/// </summary>
/// <param name="source">The <see cref="Vector4"/> to premultiply</param>
/// <param name="source">The <see cref="Vector4"/> to unpremultiply.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UnPremultiply(ref Vector4 source)
{
@ -575,84 +624,143 @@ internal static class Numerics
UnPremultiply(ref source, alpha);
}
/// <summary>
/// Unpremultiplies the X, Y, and Z components of a vector by the supplied alpha while preserving W.
/// When alpha is zero, the RGB components remain unchanged because no unassociated value can be recovered.
/// </summary>
/// <param name="source">The vector to unpremultiply.</param>
/// <param name="alpha">The source alpha replicated to every component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UnPremultiply(ref Vector4 source, Vector4 alpha)
{
// Zero alpha has no mathematical inverse, so preserve stored additive or hidden RGB data unchanged.
if (alpha == Vector4.Zero)
{
return;
}
// Divide source by alpha if alpha is nonzero, otherwise set all components to match the source value
// Blend the result with the alpha vector to ensure that the alpha component is unchanged
// Division would replace W with one, so restore the original alpha component exactly.
source = WithW(source / alpha, alpha);
}
/// <summary>
/// Bulk variant of <see cref="UnPremultiply(ref Vector4)"/>
/// Unpremultiplies the X, Y, and Z components of each vector by its W component while preserving W.
/// Vectors with zero W retain their RGB components because no unassociated value can be recovered.
/// </summary>
/// <param name="vectors">The span of vectors</param>
/// <param name="vectors">The vectors to unpremultiply.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UnPremultiply(Span<Vector4> vectors)
{
if (Avx.IsSupported && vectors.Length >= 2)
if (Vector512.IsHardwareAccelerated)
{
// Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float>
ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length / 2u);
Vector256<float> epsilon = Vector256.Create(Constants.Epsilon);
int vectorsPerVector = Vector512<float>.Count / Vector128<float>.Count;
ref Vector512<float> vectorsBase = ref Unsafe.As<Vector4, Vector512<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector512<float> vectorsEnd = ref Unsafe.Add(ref vectorsBase, (uint)(vectors.Length / vectorsPerVector));
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast))
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsEnd))
{
Vector256<float> source = vectorsBase;
Vector256<float> alpha = Avx.Permute(source, ShuffleAlphaControl);
Vector512<float> source = vectorsBase;
Vector512<float> alpha = Vector512_.ShuffleNative(source, ShuffleAlphaControl);
vectorsBase = UnPremultiply(source, alpha);
vectorsBase = ref Unsafe.Add(ref vectorsBase, 1);
}
if (Modulo2(vectors.Length) != 0)
vectors = vectors[(vectors.Length - (vectors.Length % vectorsPerVector))..];
}
if (Vector256.IsHardwareAccelerated)
{
int vectorsPerVector = Vector256<float>.Count / Vector128<float>.Count;
ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector256<float> vectorsEnd = ref Unsafe.Add(ref vectorsBase, (uint)(vectors.Length / vectorsPerVector));
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsEnd))
{
// Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
UnPremultiply(ref MemoryMarshal.GetReference(vectors[^1..]));
Vector256<float> source = vectorsBase;
Vector256<float> alpha = Vector256_.ShuffleNative(source, ShuffleAlphaControl);
vectorsBase = UnPremultiply(source, alpha);
vectorsBase = ref Unsafe.Add(ref vectorsBase, 1);
}
vectors = vectors[(vectors.Length - (vectors.Length % vectorsPerVector))..];
}
else
if (Vector128.IsHardwareAccelerated)
{
ref Vector4 vectorsStart = ref MemoryMarshal.GetReference(vectors);
ref Vector4 vectorsEnd = ref Unsafe.Add(ref vectorsStart, (uint)vectors.Length);
ref Vector128<float> vectorsBase = ref Unsafe.As<Vector4, Vector128<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector128<float> vectorsEnd = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length);
while (Unsafe.IsAddressLessThan(ref vectorsStart, ref vectorsEnd))
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsEnd))
{
UnPremultiply(ref vectorsStart);
vectorsStart = ref Unsafe.Add(ref vectorsStart, 1);
Vector128<float> source = vectorsBase;
Vector128<float> alpha = Vector128_.ShuffleNative(source, ShuffleAlphaControl);
vectorsBase = UnPremultiply(source, alpha);
vectorsBase = ref Unsafe.Add(ref vectorsBase, 1);
}
return;
}
ref Vector4 vectorsStart = ref MemoryMarshal.GetReference(vectors);
for (nuint i = 0; i < (uint)vectors.Length; i++)
{
UnPremultiply(ref Unsafe.Add(ref vectorsStart, i));
}
}
/// <summary>
/// Unpremultiplies the RGB lanes of a vector while preserving its alpha lane.
/// When alpha is zero, the RGB lanes remain unchanged because no unassociated value can be recovered.
/// </summary>
/// <param name="source">The associated vector.</param>
/// <param name="alpha">The source alpha replicated to every lane.</param>
/// <returns>The unassociated vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<float> UnPremultiply(Vector128<float> source, Vector128<float> alpha)
{
// Zero alpha has no mathematical inverse, so select the source lanes to preserve stored additive or hidden RGB data.
Vector128<float> zeroMask = Vector128.Equals(alpha, Vector128<float>.Zero);
Vector128<float> result = Vector128.ConditionalSelect(zeroMask, source, source / alpha);
// Division would replace W with one, so restore the original alpha lane exactly.
Vector128<float> alphaMask = Vector128.Create(0, 0, 0, -1).AsSingle();
return Vector128.ConditionalSelect(alphaMask, alpha, result);
}
/// <summary>
/// Unpremultiplies the RGB lanes of two vectors while preserving their alpha lanes.
/// Vectors with zero alpha retain their RGB lanes because no unassociated value can be recovered.
/// </summary>
/// <param name="source">The associated vectors.</param>
/// <param name="alpha">Each source alpha replicated across its four lanes.</param>
/// <returns>The unassociated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector256<float> UnPremultiply(Vector256<float> source, Vector256<float> alpha)
{
// Check if alpha is zero to avoid division by zero
// Zero alpha has no mathematical inverse, so select the source lanes to preserve stored additive or hidden RGB data.
Vector256<float> zeroMask = Avx.CompareEqual(alpha, Vector256<float>.Zero);
// Divide source by alpha if alpha is nonzero, otherwise set all components to match the source value
Vector256<float> result = Avx.BlendVariable(Avx.Divide(source, alpha), source, zeroMask);
// Blend the result with the alpha vector to ensure that the alpha component is unchanged
// Division would replace W with one, so restore both original alpha lanes exactly.
return Avx.Blend(result, alpha, BlendAlphaControl);
}
/// <summary>
/// Unpremultiplies the RGB lanes of four vectors while preserving their alpha lanes.
/// Vectors with zero alpha retain their RGB lanes because no unassociated value can be recovered.
/// </summary>
/// <param name="source">The associated vectors.</param>
/// <param name="alpha">Each source alpha replicated across its four lanes.</param>
/// <returns>The unassociated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector512<float> UnPremultiply(Vector512<float> source, Vector512<float> alpha)
{
// Check if alpha is zero to avoid division by zero
// Zero alpha has no mathematical inverse, so select the source lanes to preserve stored additive or hidden RGB data.
Vector512<float> zeroMask = Vector512.Equals(alpha, Vector512<float>.Zero);
// Divide source by alpha if alpha is nonzero, otherwise set all components to match the source value
Vector512<float> result = Vector512.ConditionalSelect(zeroMask, source, source / alpha);
// Blend the result with the alpha vector to ensure that the alpha component is unchanged
// Division would replace W with one, so restore all four original alpha lanes exactly.
Vector512<float> alphaMask = Vector512.Create(0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
return Vector512.ConditionalSelect(alphaMask, alpha, result);
}

3
src/ImageSharp/Common/Helpers/Vector128Utilities.cs

@ -336,7 +336,8 @@ internal static class Vector128_
/// Rounds all values in <paramref name="vector"/> to the nearest integer
/// following <see cref="MidpointRounding.ToEven"/> semantics.
/// </summary>
/// <param name="vector">The vector</param>
/// <param name="vector">The vector.</param>
/// <returns>The vector with each value rounded to the nearest integer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<float> RoundToNearestInteger(Vector128<float> vector)
{

3
src/ImageSharp/Common/Helpers/Vector256Utilities.cs

@ -97,7 +97,8 @@ internal static class Vector256_
/// Rounds all values in <paramref name="vector"/> to the nearest integer
/// following <see cref="MidpointRounding.ToEven"/> semantics.
/// </summary>
/// <param name="vector">The vector</param>
/// <param name="vector">The vector.</param>
/// <returns>The vector with each value rounded to the nearest integer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector256<float> RoundToNearestInteger(Vector256<float> vector)
{

11
src/ImageSharp/Common/Helpers/Vector512Utilities.cs

@ -76,14 +76,15 @@ internal static class Vector512_
/// Rounds all values in <paramref name="vector"/> to the nearest integer
/// following <see cref="MidpointRounding.ToEven"/> semantics.
/// </summary>
/// <param name="vector">The vector</param>
/// <param name="vector">The vector.</param>
/// <returns>The vector with each value rounded to the nearest integer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector512<float> RoundToNearestInteger(Vector512<float> vector)
// imm8 = 0b1000:
// imm8[7:4] = 0b0000 -> preserve 0 fractional bits (round to whole numbers)
// imm8[3:0] = 0b1000 -> _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC (round to nearest even, suppress exceptions)
=> Avx512F.RoundScale(vector, 0b0000_1000);
// imm8 = 0b1000:
// imm8[7:4] = 0b0000 -> preserve 0 fractional bits (round to whole numbers)
// imm8[3:0] = 0b1000 -> _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC (round to nearest even, suppress exceptions)
=> Avx512F.RoundScale(vector, 0b0000_1000);
/// <summary>
/// Computes an estimate of (<paramref name="left"/> * <paramref name="right"/>) + <paramref name="addend"/>.

10
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -350,10 +350,10 @@ internal sealed class BmpDecoderCore : ImageDecoderCore
pixelRow[x] = this.rleSkippedPixelHandling switch
{
RleSkippedPixelHandling.FirstColorOfPalette => TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[colorIdx * 4])),
RleSkippedPixelHandling.Transparent => TPixel.FromScaledVector4(Vector4.Zero),
RleSkippedPixelHandling.Transparent => TPixel.FromUnassociatedScaledVector4(Vector4.Zero),
// Default handling for skipped pixels is black (which is what System.Drawing is also doing).
_ => TPixel.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f)),
_ => TPixel.FromUnassociatedScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f)),
};
}
else
@ -411,10 +411,10 @@ internal sealed class BmpDecoderCore : ImageDecoderCore
pixelRow[x] = this.rleSkippedPixelHandling switch
{
RleSkippedPixelHandling.FirstColorOfPalette => TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx])),
RleSkippedPixelHandling.Transparent => TPixel.FromScaledVector4(Vector4.Zero),
RleSkippedPixelHandling.Transparent => TPixel.FromUnassociatedScaledVector4(Vector4.Zero),
// Default handling for skipped pixels is black (which is what System.Drawing is also doing).
_ => TPixel.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f)),
_ => TPixel.FromUnassociatedScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f)),
};
}
else
@ -1272,7 +1272,7 @@ internal sealed class BmpDecoderCore : ImageDecoderCore
g * invMaxValueGreen,
b * invMaxValueBlue,
alpha);
pixelRow[x] = TPixel.FromScaledVector4(vector4);
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(vector4);
}
else
{

4
src/ImageSharp/Formats/EncodingUtilities.cs

@ -65,9 +65,9 @@ internal static class EncodingUtilities
for (int y = 0; y < region.Height; y++)
{
Span<TPixel> span = region.DangerousGetRowSpan(y);
PixelOperations<TPixel>.Instance.ToVector4(configuration, span, vectorsSpan, PixelConversionModifiers.Scale);
PixelOperations<TPixel>.Instance.ToVector4(configuration, span, vectorsSpan, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
ReplaceTransparentPixels(vectorsSpan);
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorsSpan, span, PixelConversionModifiers.Scale);
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorsSpan, span, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
}
}

9
src/ImageSharp/Formats/Exr/ExrDecoderCore.cs

@ -209,7 +209,10 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
for (int x = 0; x < width; x++)
{
HalfVector4 pixelValue = new(redPixelData[x], greenPixelData[x], bluePixelData[x], hasAlpha ? alphaPixelData[x] : 1.0f);
pixelRow[x] = TPixel.FromVector4(pixelValue.ToVector4());
// OpenEXR channel values are associated. The destination conversion retains that representation for associated
// pixels and unassociates only when the destination stores straight alpha.
pixelRow[x] = TPixel.FromAssociatedVector4(pixelValue.ToVector4());
}
decodedRows++;
@ -288,7 +291,9 @@ internal sealed class ExrDecoderCore : ImageDecoderCore
for (int x = 0; x < width; x++)
{
Rgba128 pixelValue = new(redPixelData[x], greenPixelData[x], bluePixelData[x], hasAlpha ? alphaPixelData[x] : uint.MaxValue);
pixelRow[x] = TPixel.FromVector4(pixelValue.ToVector4());
// Preserve the file's associated-alpha convention while converting from the native unsigned channel domain.
pixelRow[x] = TPixel.FromAssociatedVector4(pixelValue.ToVector4());
}
decodedRows++;

7
src/ImageSharp/Formats/Exr/ExrEncoderCore.cs

@ -206,7 +206,9 @@ internal sealed class ExrEncoderCore
Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan((int)rowIndex);
for (int x = 0; x < width; x++)
{
Vector4 vector4 = pixelRowSpan[x].ToVector4();
// OpenEXR stores RGB associated with alpha. Use the native vector domain so floating-point and HDR component
// ranges are preserved instead of being clamped through the scaled [0, 1] representation.
Vector4 vector4 = pixelRowSpan[x].ToAssociatedVector4();
redBuffer[x] = vector4.X;
greenBuffer[x] = vector4.Y;
blueBuffer[x] = vector4.Z;
@ -303,7 +305,8 @@ internal sealed class ExrEncoderCore
Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan((int)rowIndex);
for (int x = 0; x < width; x++)
{
Vector4 vector4 = pixelRowSpan[x].ToVector4();
// OpenEXR channels use associated alpha; the native vector conversion also preserves the integer channel range.
Vector4 vector4 = pixelRowSpan[x].ToAssociatedVector4();
rgb = Rgba128.FromVector4(vector4);
redBuffer[x] = rgb.R;

3
src/ImageSharp/Formats/Exr/ExrMetadata.cs

@ -53,7 +53,8 @@ public class ExrMetadata : IFormatMetadata<ExrMetadata>
bitsPerPixel = hasAlpha ? bitsPerComponent * 4 : bitsPerComponent * 3;
}
PixelAlphaRepresentation alpha = hasAlpha ? PixelAlphaRepresentation.Unassociated : PixelAlphaRepresentation.None;
// OpenEXR defines RGBA color channels as premultiplied by alpha, so expose the association stored by the format.
PixelAlphaRepresentation alpha = hasAlpha ? PixelAlphaRepresentation.Associated : PixelAlphaRepresentation.None;
PixelColorType color = PixelColorType.RGB;
int componentsCount = 0;

2
src/ImageSharp/Formats/Gif/GifEncoderCore.cs

@ -541,7 +541,7 @@ internal sealed class GifEncoderCore
int index = -1;
if (quantized != null)
{
TPixel transparentPixel = TPixel.FromScaledVector4(Vector4.Zero);
TPixel transparentPixel = TPixel.FromUnassociatedScaledVector4(Vector4.Zero);
ReadOnlySpan<TPixel> palette = quantized.Palette.Span;
// Transparent pixels are much more likely to be found at the end of a palette.

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs

@ -34,7 +34,7 @@ internal class BlackIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{
L16 l16 = TiffUtilities.L16Default;
TPixel color = TPixel.FromScaledVector4(Vector4.Zero);
TPixel color = TPixel.FromUnassociatedScaledVector4(Vector4.Zero);
int offset = 0;
for (int y = top; y < top + height; y++)

4
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero32FloatTiffColor{TPixel}.cs

@ -40,7 +40,7 @@ internal class BlackIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
float intensity = BitConverter.ToSingle(buffer);
offset += 4;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
}
}
else
@ -50,7 +50,7 @@ internal class BlackIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
float intensity = BitConverter.ToSingle(data.Slice(offset, 4));
offset += 4;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
}
}
}

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor{TPixel}.cs

@ -36,7 +36,7 @@ internal class BlackIsZeroTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{
int value = bitReader.ReadBits(this.bitsPerSample0);
float intensity = value / this.factor;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
}
bitReader.NextRow();

4
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLab16PlanarTiffColor{TPixel}.cs

@ -109,7 +109,7 @@ internal class CieLab16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPix
// Convert CIE Lab -> Rgb -> Vector4 -> TPixel
this.colorProfileConverter.Convert<CieLab, Rgb>(cieLabRow, rgbRow);
Rgb.ToScaledVector4(rgbRow, vectorRow);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
}
return;
@ -138,7 +138,7 @@ internal class CieLab16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPix
// Convert CIE Lab -> Rgb -> Vector4 -> TPixel
this.colorProfileConverter.Convert<CieLab, Rgb>(cieLabRow, rgbRow);
Rgb.ToScaledVector4(rgbRow, vectorRow);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
}
}
}

4
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLab16TiffColor{TPixel}.cs

@ -105,7 +105,7 @@ internal class CieLab16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
// Convert CIE Lab -> Rgb -> Vector4 -> TPixel
this.colorProfileConverter.Convert<CieLab, Rgb>(cieLabRow, rgbRow);
Rgb.ToScaledVector4(rgbRow, vectorRow);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
}
return;
@ -134,7 +134,7 @@ internal class CieLab16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
// Convert CIE Lab -> Rgb -> Vector4 -> TPixel
this.colorProfileConverter.Convert<CieLab, Rgb>(cieLabRow, rgbRow);
Rgb.ToScaledVector4(rgbRow, vectorRow);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
}
}
}

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLab8PlanarTiffColor{TPixel}.cs

@ -35,7 +35,7 @@ internal class CieLab8PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixe
{
CieLab lab = new((l[offset] & 0xFF) * 100f * Inv255, (sbyte)a[offset], (sbyte)b[offset]);
Rgb rgb = ColorProfileConverter.Convert<CieLab, Rgb>(in lab);
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
offset++;
}

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLab8TiffColor{TPixel}.cs

@ -39,7 +39,7 @@ internal class CieLab8TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float l = (data[offset] & 0xFF) * 100f * Inv255;
CieLab lab = new(l, (sbyte)data[offset + 1], (sbyte)data[offset + 2]);
Rgb rgb = ColorProfileConverter.Convert<CieLab, Rgb>(in lab);
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1f));
offset += 3;
}

4
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CmykTiffColor{TPixel}.cs

@ -66,7 +66,7 @@ internal class CmykTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
for (int x = 0; x < pixelRow.Length; x++)
{
pixelRow[x] = TPixel.FromVector4(new Vector4(data[offset] * Inv255, data[offset + 1] * Inv255, data[offset + 2] * Inv255, 1.0f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(data[offset] * Inv255, data[offset + 1] * Inv255, data[offset + 2] * Inv255, 1.0f));
offset += 3;
}
@ -99,7 +99,7 @@ internal class CmykTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
// Convert CMYK -> RGB -> Vector4 -> TPixel
this.colorProfileConverter.Convert<Cmyk, Rgb>(cmykRow, rgbRow);
Rgb.ToScaledVector4(rgbRow, vectorRow);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorRow, pixelRow, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
}
}
}

4
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor{TPixel}.cs

@ -97,7 +97,7 @@ internal class PaletteTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
Vector4 color = this.vectorPallete[index];
color.W = alpha;
pixelRow[x] = TPixel.FromScaledVector4(color);
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(color);
// Best-effort palette update for downstream conversions.
// This is intentionally "last writer wins" with no per-pixel branch.
@ -163,7 +163,7 @@ internal class PaletteTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float r = colorMap[rOffset + i] * InvMax;
float g = colorMap[gOffset + i] * InvMax;
float b = colorMap[bOffset + i] * InvMax;
palette[i] = TPixel.FromScaledVector4(new Vector4(r, g, b, 1f));
palette[i] = TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, 1f));
}
return palette;

4
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb444TiffColor{TPixel}.cs

@ -30,7 +30,7 @@ internal class Rgb444TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
byte b = (byte)((data[offset] & 0xF0) >> 4);
Bgra4444 bgra = new() { PackedValue = ToBgraPackedValue(b, g, r) };
pixelRow[x] = TPixel.FromScaledVector4(bgra.ToScaledVector4());
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(bgra.ToUnassociatedScaledVector4());
if (x + 1 >= pixelRow.Length)
{
offset++;
@ -44,7 +44,7 @@ internal class Rgb444TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
offset++;
bgra.PackedValue = ToBgraPackedValue(b, g, r);
pixelRow[x + 1] = TPixel.FromScaledVector4(bgra.ToScaledVector4());
pixelRow[x + 1] = TPixel.FromUnassociatedScaledVector4(bgra.ToUnassociatedScaledVector4());
}
}
}

4
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbFloat323232TiffColor{TPixel}.cs

@ -51,7 +51,7 @@ internal class RgbFloat323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float b = BitConverter.ToSingle(buffer);
offset += 4;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(r, g, b, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, 1f));
}
}
else
@ -67,7 +67,7 @@ internal class RgbFloat323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float b = BitConverter.ToSingle(data.Slice(offset, 4));
offset += 4;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(r, g, b, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, 1f));
}
}
}

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs

@ -63,7 +63,7 @@ internal class RgbPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
float g = gBitReader.ReadBits(this.bitsPerSampleG) / this.gFactor;
float b = bBitReader.ReadBits(this.bitsPerSampleB) / this.bFactor;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(r, g, b, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, 1f));
}
rBitReader.NextRow();

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor{TPixel}.cs

@ -52,7 +52,7 @@ internal class RgbTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float g = bitReader.ReadBits(this.bitsPerSampleG) / this.gFactor;
float b = bitReader.ReadBits(this.bitsPerSampleB) / this.bFactor;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(r, g, b, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, 1f));
}
bitReader.NextRow();

22
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba16161616TiffColor{TPixel}.cs

@ -5,6 +5,7 @@
using System.Buffers;
using System.Numerics;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@ -96,16 +97,21 @@ internal class Rgba16161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
int byteCount = pixelRow.Length * 8;
PixelOperations<TPixel>.Instance.FromRgba64Bytes(
this.configuration,
data.Slice(offset, byteCount),
pixelRow,
pixelRow.Length);
if (hasAssociatedAlpha)
{
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, pixelRow, vectorsSpan);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorsSpan, pixelRow, PixelConversionModifiers.Premultiply | PixelConversionModifiers.Scale);
// This little-endian branch can view the interleaved samples as Rgba64. The values are already associated, so
// the Rgba64 operation performs only unpacking and normalization before the destination consumes that representation.
ReadOnlySpan<Rgba64> associatedSamples = MemoryMarshal.Cast<byte, Rgba64>(data.Slice(offset, byteCount));
PixelOperations<Rgba64>.Instance.ToVector4(this.configuration, associatedSamples, vectorsSpan, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorsSpan, pixelRow, PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply);
}
else
{
PixelOperations<TPixel>.Instance.FromRgba64Bytes(
this.configuration,
data.Slice(offset, byteCount),
pixelRow,
pixelRow.Length);
}
offset += byteCount;

21
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba8888TiffColor{TPixel}.cs

@ -4,6 +4,7 @@
using System.Buffers;
using System.Numerics;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@ -41,16 +42,22 @@ internal class Rgba8888TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
int byteCount = pixelRow.Length * 4;
PixelOperations<TPixel>.Instance.FromRgba32Bytes(
this.configuration,
data.Slice(offset, byteCount),
pixelRow,
pixelRow.Length);
if (hasAssociatedAlpha)
{
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, pixelRow, vectorsSpan);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorsSpan, pixelRow, PixelConversionModifiers.Premultiply | PixelConversionModifiers.Scale);
// The TIFF samples already contain associated RGB. Reinterpret them as Rgba32 only to normalize and unpack the
// channels; asking that unassociated storage type to associate them would multiply RGB a second time.
ReadOnlySpan<Rgba32> associatedSamples = MemoryMarshal.Cast<byte, Rgba32>(data.Slice(offset, byteCount));
PixelOperations<Rgba32>.Instance.ToVector4(this.configuration, associatedSamples, vectorsSpan, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorsSpan, pixelRow, PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply);
}
else
{
PixelOperations<TPixel>.Instance.FromRgba32Bytes(
this.configuration,
data.Slice(offset, byteCount),
pixelRow,
pixelRow.Length);
}
offset += byteCount;

20
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaFloat32323232TiffColor{TPixel}.cs

@ -16,16 +16,24 @@ internal class RgbaFloat32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{
private readonly bool isBigEndian;
private readonly TiffExtraSampleType? extraSamplesType;
/// <summary>
/// Initializes a new instance of the <see cref="RgbaFloat32323232TiffColor{TPixel}" /> class.
/// </summary>
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
public RgbaFloat32323232TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian;
/// <param name="extraSamplesType">The alpha representation declared by the TIFF extra samples field.</param>
public RgbaFloat32323232TiffColor(bool isBigEndian, TiffExtraSampleType? extraSamplesType)
{
this.isBigEndian = isBigEndian;
this.extraSamplesType = extraSamplesType;
}
/// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{
int offset = 0;
bool hasAssociatedAlpha = this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData;
Span<byte> buffer = stackalloc byte[4];
for (int y = top; y < top + height; y++)
@ -56,7 +64,10 @@ internal class RgbaFloat32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float a = BitConverter.ToSingle(buffer);
offset += 4;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(r, g, b, a));
Vector4 vector = new(r, g, b, a);
pixelRow[x] = hasAssociatedAlpha
? TPixel.FromAssociatedScaledVector4(vector)
: TPixel.FromUnassociatedScaledVector4(vector);
}
}
else
@ -75,7 +86,10 @@ internal class RgbaFloat32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float a = BitConverter.ToSingle(data.Slice(offset, 4));
offset += 4;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(r, g, b, a));
Vector4 vector = new(r, g, b, a);
pixelRow[x] = hasAssociatedAlpha
? TPixel.FromAssociatedScaledVector4(vector)
: TPixel.FromUnassociatedScaledVector4(vector);
}
}
}

4
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaPlanarTiffColor{TPixel}.cs

@ -80,11 +80,11 @@ internal class RgbaPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
Vector4 vector = new(r, g, b, a);
if (hasAssociatedAlpha)
{
pixelRow[x] = TiffUtilities.UnPremultiply<TPixel>(ref vector);
pixelRow[x] = TPixel.FromAssociatedScaledVector4(vector);
}
else
{
pixelRow[x] = TPixel.FromScaledVector4(vector);
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(vector);
}
}

6
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaTiffColor{TPixel}.cs

@ -63,16 +63,16 @@ internal class RgbaTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float r = bitReader.ReadBits(this.bitsPerSampleR) / this.rFactor;
float g = bitReader.ReadBits(this.bitsPerSampleG) / this.gFactor;
float b = bitReader.ReadBits(this.bitsPerSampleB) / this.bFactor;
float a = bitReader.ReadBits(this.bitsPerSampleB) / this.aFactor;
float a = bitReader.ReadBits(this.bitsPerSampleA) / this.aFactor;
Vector4 vector = new(r, g, b, a);
if (hasAssociatedAlpha)
{
pixelRow[x] = TiffUtilities.UnPremultiply<TPixel>(ref vector);
pixelRow[x] = TPixel.FromAssociatedScaledVector4(vector);
}
else
{
pixelRow[x] = TPixel.FromScaledVector4(vector);
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(vector);
}
}

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs

@ -383,7 +383,7 @@ internal static class TiffColorDecoderFactory<TPixel>
&& bitsPerSample.Channel0 == 32,
"bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new RgbaFloat32323232TiffColor<TPixel>(isBigEndian: byteOrder == ByteOrder.BigEndian);
return new RgbaFloat32323232TiffColor<TPixel>(byteOrder == ByteOrder.BigEndian, extraSampleType);
case TiffColorType.PaletteColor:
DebugGuard.NotNull(colorMap, "colorMap");

4
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero32FloatTiffColor{TPixel}.cs

@ -40,7 +40,7 @@ internal class WhiteIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
float intensity = 1.0f - BitConverter.ToSingle(buffer);
offset += 4;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
}
}
else
@ -50,7 +50,7 @@ internal class WhiteIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
float intensity = 1.0f - BitConverter.ToSingle(data.Slice(offset, 4));
offset += 4;
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1.0f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(intensity, intensity, intensity, 1.0f));
}
}
}

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor{TPixel}.cs

@ -36,7 +36,7 @@ internal class WhiteIsZeroTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{
int value = bitReader.ReadBits(this.bitsPerSample0);
float intensity = 1f - (value / this.factor);
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
pixelRow[x] = TPixel.FromUnassociatedScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
}
bitReader.NextRow();

45
src/ImageSharp/Formats/Tiff/Utils/TiffUtilities.cs

@ -40,72 +40,67 @@ internal static class TiffUtilities
public static TPixel ColorFromRgba64Premultiplied<TPixel>(ushort r, ushort g, ushort b, ushort a)
where TPixel : unmanaged, IPixel<TPixel>
{
// TIFF 6.0 requires associated RGB to be interpreted as zero when alpha is zero. Enforce that codec contract here
// while leaving the general associated-alpha conversion contract free to preserve hidden RGB for formats such as EXR.
if (a == 0)
{
return TPixel.FromRgba64(default);
return TPixel.FromAssociatedScaledVector4(Vector4.Zero);
}
float scale = 65535f / a;
ushort ur = (ushort)Math.Min(r * scale, 65535);
ushort ug = (ushort)Math.Min(g * scale, 65535);
ushort ub = (ushort)Math.Min(b * scale, 65535);
return TPixel.FromRgba64(new Rgba64(ur, ug, ub, a));
// TIFF marks these samples as already associated. Passing that representation directly avoids an unassociate/reassociate
// round trip for associated destinations while unassociated formats perform the required conversion in their pixel contract.
return TPixel.FromAssociatedScaledVector4(new Vector4(r, g, b, a) / ushort.MaxValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo24Bit<TPixel>(uint r, uint g, uint b)
where TPixel : unmanaged, IPixel<TPixel>
=> TPixel.FromScaledVector4(new Vector4(r, g, b, 1f) * Scale24BitVector);
=> TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, 1f) * Scale24BitVector);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo24Bit<TPixel>(uint r, uint g, uint b, uint a)
where TPixel : unmanaged, IPixel<TPixel>
=> TPixel.FromScaledVector4(new Vector4(r, g, b, a) * Scale24Bit);
=> TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, a) * Scale24Bit);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo24BitPremultiplied<TPixel>(uint r, uint g, uint b, uint a)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit;
return UnPremultiply<TPixel>(ref colorVector);
// TIFF 6.0 assigns no color to a fully transparent associated sample, even when malformed input stores nonzero RGB.
return a == 0
? TPixel.FromAssociatedScaledVector4(Vector4.Zero)
: TPixel.FromAssociatedScaledVector4(new Vector4(r, g, b, a) * Scale24Bit);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo32Bit<TPixel>(uint r, uint g, uint b)
where TPixel : unmanaged, IPixel<TPixel>
=> TPixel.FromScaledVector4(new Vector4(r, g, b, 1f) * Scale32BitVector);
=> TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, 1f) * Scale32BitVector);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo32Bit<TPixel>(uint r, uint g, uint b, uint a)
where TPixel : unmanaged, IPixel<TPixel>
=> TPixel.FromScaledVector4(new Vector4(r, g, b, a) * Scale32Bit);
=> TPixel.FromUnassociatedScaledVector4(new Vector4(r, g, b, a) * Scale32Bit);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo32BitPremultiplied<TPixel>(uint r, uint g, uint b, uint a)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 vector = new Vector4(r, g, b, a) * Scale32Bit;
return UnPremultiply<TPixel>(ref vector);
// TIFF 6.0 assigns no color to a fully transparent associated sample, even when malformed input stores nonzero RGB.
return a == 0
? TPixel.FromAssociatedScaledVector4(Vector4.Zero)
: TPixel.FromAssociatedScaledVector4(new Vector4(r, g, b, a) * Scale32Bit);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo24Bit<TPixel>(uint intensity)
where TPixel : unmanaged, IPixel<TPixel>
=> TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f) * Scale24BitVector);
=> TPixel.FromUnassociatedScaledVector4(new Vector4(intensity, intensity, intensity, 1f) * Scale24BitVector);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo32Bit<TPixel>(uint intensity)
where TPixel : unmanaged, IPixel<TPixel>
=> TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f) * Scale32BitVector);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel UnPremultiply<TPixel>(ref Vector4 vector)
where TPixel : unmanaged, IPixel<TPixel>
{
Numerics.UnPremultiply(ref vector);
return TPixel.FromScaledVector4(vector);
}
=> TPixel.FromUnassociatedScaledVector4(new Vector4(intensity, intensity, intensity, 1f) * Scale32BitVector);
/// <summary>
/// Finds the padding needed to round 'valueToRoundUp' to the next integer multiple of subSampling value.

327
src/ImageSharp/PixelFormats/AssociatedAlphaPixelOperations{TPixel}.cs

@ -3,8 +3,7 @@
using System.Buffers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.ColorProfiles.Companding;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats.PixelBlenders;
@ -14,7 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Provides bulk operations for pixel formats that store associated alpha.
/// </summary>
/// <typeparam name="TPixel">The associated-alpha pixel format.</typeparam>
internal class AssociatedAlphaPixelOperations<TPixel> : PixelOperations<TPixel>
public abstract class AssociatedAlphaPixelOperations<TPixel> : PixelOperations<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
/// <inheritdoc />
@ -22,73 +21,49 @@ internal class AssociatedAlphaPixelOperations<TPixel> : PixelOperations<TPixel>
=> AssociatedAlphaPixelBlenders<TPixel>.GetPixelBlender(colorMode, alphaMode);
/// <inheritdoc />
internal override Vector4 ToUnassociatedScaledVector4(TPixel source)
{
Vector4 vector = source.ToScaledVector4();
Numerics.UnPremultiply(ref vector);
return vector;
}
protected abstract override void ToUnassociatedVector4(
Configuration configuration,
ReadOnlySpan<TPixel> source,
Span<Vector4> destination);
/// <inheritdoc />
internal override TPixel FromUnassociatedScaledVector4(Vector4 source) => TPixel.FromScaledVector4(Associate(source));
protected abstract override void ToAssociatedVector4(
Configuration configuration,
ReadOnlySpan<TPixel> source,
Span<Vector4> destination);
/// <summary>
/// Converts an associated scaled vector to a destination pixel after associating RGB with the alpha value the destination stores.
/// </summary>
/// <param name="source">The associated scaled vector.</param>
/// <returns>The destination pixel.</returns>
public virtual TPixel FromAssociatedScaledVector4(Vector4 source) => TPixel.FromScaledVector4(Reassociate(source));
/// <inheritdoc />
protected abstract override void FromUnassociatedVector4Destructive(
Configuration configuration,
Span<Vector4> source,
Span<TPixel> destination);
/// <inheritdoc />
protected abstract override void FromAssociatedVector4Destructive(
Configuration configuration,
Span<Vector4> source,
Span<TPixel> destination);
/// <inheritdoc />
internal override void ToUnassociatedScaledVector4(
protected abstract override void ToUnassociatedScaledVector4(
Configuration configuration,
ReadOnlySpan<TPixel> source,
Span<Vector4> destination)
{
this.ToVector4(configuration, source, destination, PixelConversionModifiers.Scale);
Numerics.UnPremultiply(destination[..source.Length]);
}
Span<Vector4> destination);
/// <inheritdoc />
internal override void ToAssociatedScaledVector4(
protected abstract override void ToAssociatedScaledVector4(
Configuration configuration,
ReadOnlySpan<TPixel> source,
Span<Vector4> destination)
=> this.ToVector4(configuration, source, destination, PixelConversionModifiers.Scale);
Span<Vector4> destination);
/// <inheritdoc />
internal override void FromUnassociatedScaledVector4(
protected abstract override void FromUnassociatedScaledVector4Destructive(
Configuration configuration,
Span<Vector4> source,
Span<TPixel> destination)
{
source = source[..destination.Length];
for (int i = 0; i < source.Length; i++)
{
source[i] = Associate(source[i]);
}
Span<TPixel> destination);
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
}
/// <summary>
/// Converts associated scaled vectors to destination pixels after associating RGB with the alpha values the destination stores.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="source">The associated scaled vectors.</param>
/// <param name="destination">The destination pixels.</param>
public virtual void FromAssociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<TPixel> destination)
{
source = source[..destination.Length];
for (int i = 0; i < source.Length; i++)
{
source[i] = Reassociate(source[i]);
}
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
}
/// <inheritdoc />
protected abstract override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<TPixel> destination);
/// <inheritdoc />
public override void From<TSourcePixel>(
@ -96,11 +71,17 @@ internal class AssociatedAlphaPixelOperations<TPixel> : PixelOperations<TPixel>
ReadOnlySpan<TSourcePixel> source,
Span<TPixel> destination)
{
const int sliceLength = 1024;
if (source.IsEmpty)
{
return;
}
// Cap large conversions at 1,024 vectors while avoiding a 16 KiB rental for short spans.
int sliceLength = Math.Min(source.Length, 1024);
int numberOfSlices = source.Length / sliceLength;
using IMemoryOwner<Vector4> tempVectors = configuration.MemoryAllocator.Allocate<Vector4>(sliceLength);
Span<Vector4> vectorSpan = tempVectors.GetSpan();
Span<Vector4> vectorSpan = tempVectors.GetSpan()[..sliceLength];
// Convert through unassociated vectors so the destination operation can quantize alpha to its own storage before associating RGB.
for (int i = 0; i < numberOfSlices; i++)
@ -108,8 +89,13 @@ internal class AssociatedAlphaPixelOperations<TPixel> : PixelOperations<TPixel>
int start = i * sliceLength;
ReadOnlySpan<TSourcePixel> sourceSlice = source.Slice(start, sliceLength);
Span<TPixel> destinationSlice = destination.Slice(start, sliceLength);
PixelOperations<TSourcePixel>.Instance.ToUnassociatedScaledVector4(configuration, sourceSlice, vectorSpan);
this.FromUnassociatedScaledVector4(configuration, vectorSpan, destinationSlice);
PixelOperations<TSourcePixel>.Instance.ToVector4(
configuration,
sourceSlice,
vectorSpan,
PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
this.FromUnassociatedScaledVector4Destructive(configuration, vectorSpan, destinationSlice);
}
int endOfCompleteSlices = numberOfSlices * sliceLength;
@ -118,10 +104,15 @@ internal class AssociatedAlphaPixelOperations<TPixel> : PixelOperations<TPixel>
if (remainder > 0)
{
ReadOnlySpan<TSourcePixel> sourceSlice = source[endOfCompleteSlices..];
Span<TPixel> destinationSlice = destination[endOfCompleteSlices..];
Span<TPixel> destinationSlice = destination.Slice(endOfCompleteSlices, remainder);
vectorSpan = vectorSpan[..remainder];
PixelOperations<TSourcePixel>.Instance.ToUnassociatedScaledVector4(configuration, sourceSlice, vectorSpan);
this.FromUnassociatedScaledVector4(configuration, vectorSpan, destinationSlice);
PixelOperations<TSourcePixel>.Instance.ToVector4(
configuration,
sourceSlice,
vectorSpan,
PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
this.FromUnassociatedScaledVector4Destructive(configuration, vectorSpan, destinationSlice);
}
}
@ -131,7 +122,55 @@ internal class AssociatedAlphaPixelOperations<TPixel> : PixelOperations<TPixel>
Span<Vector4> sourceVectors,
Span<TPixel> destination,
PixelConversionModifiers modifiers)
=> base.FromVector4Destructive(configuration, sourceVectors, destination, modifiers.Remove(PixelConversionModifiers.Premultiply));
{
Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourceVectors, destination, nameof(destination));
bool associated = modifiers.IsDefined(PixelConversionModifiers.Premultiply) || !modifiers.IsDefined(PixelConversionModifiers.UnPremultiply);
bool scaled = modifiers.IsDefined(PixelConversionModifiers.Scale);
if (modifiers.IsDefined(PixelConversionModifiers.SRgbCompand))
{
// Transfer functions operate on straight color components. Associated input must therefore be unassociated before companding.
if (associated)
{
Numerics.UnPremultiply(sourceVectors);
}
SRgbCompanding.Compress(sourceVectors);
if (scaled)
{
this.FromUnassociatedScaledVector4Destructive(configuration, sourceVectors, destination);
}
else
{
this.FromUnassociatedVector4Destructive(configuration, sourceVectors, destination);
}
return;
}
if (scaled)
{
if (associated)
{
this.FromAssociatedScaledVector4Destructive(configuration, sourceVectors, destination);
}
else
{
this.FromUnassociatedScaledVector4Destructive(configuration, sourceVectors, destination);
}
}
else if (associated)
{
this.FromAssociatedVector4Destructive(configuration, sourceVectors, destination);
}
else
{
this.FromUnassociatedVector4Destructive(configuration, sourceVectors, destination);
}
}
/// <inheritdoc />
public override void ToVector4(
@ -139,7 +178,108 @@ internal class AssociatedAlphaPixelOperations<TPixel> : PixelOperations<TPixel>
ReadOnlySpan<TPixel> source,
Span<Vector4> destinationVectors,
PixelConversionModifiers modifiers)
=> base.ToVector4(configuration, source, destinationVectors, modifiers.Remove(PixelConversionModifiers.Premultiply));
{
Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationVectors, nameof(destinationVectors));
bool associated = modifiers.IsDefined(PixelConversionModifiers.Premultiply) || !modifiers.IsDefined(PixelConversionModifiers.UnPremultiply);
bool scaled = modifiers.IsDefined(PixelConversionModifiers.Scale);
if (modifiers.IsDefined(PixelConversionModifiers.SRgbCompand))
{
// Extract straight color before applying the transfer function; companding associated components would make RGB depend on alpha.
if (scaled)
{
this.ToUnassociatedScaledVector4(configuration, source, destinationVectors);
}
else
{
this.ToUnassociatedVector4(configuration, source, destinationVectors);
}
Span<Vector4> converted = destinationVectors[..source.Length];
SRgbCompanding.Expand(converted);
if (associated)
{
Numerics.Premultiply(converted);
}
return;
}
if (scaled)
{
if (associated)
{
this.ToAssociatedScaledVector4(configuration, source, destinationVectors);
}
else
{
this.ToUnassociatedScaledVector4(configuration, source, destinationVectors);
}
}
else if (associated)
{
this.ToAssociatedVector4(configuration, source, destinationVectors);
}
else
{
this.ToUnassociatedVector4(configuration, source, destinationVectors);
}
}
/// <inheritdoc />
public override void FromArgb32(Configuration configuration, ReadOnlySpan<Argb32> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromAbgr32(Configuration configuration, ReadOnlySpan<Abgr32> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromBgr24(Configuration configuration, ReadOnlySpan<Bgr24> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromBgra32(Configuration configuration, ReadOnlySpan<Bgra32> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromL8(Configuration configuration, ReadOnlySpan<L8> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromL16(Configuration configuration, ReadOnlySpan<L16> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromLa16(Configuration configuration, ReadOnlySpan<La16> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromLa32(Configuration configuration, ReadOnlySpan<La32> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromRgb24(Configuration configuration, ReadOnlySpan<Rgb24> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromRgba32(Configuration configuration, ReadOnlySpan<Rgba32> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromRgb48(Configuration configuration, ReadOnlySpan<Rgb48> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromRgba64(Configuration configuration, ReadOnlySpan<Rgba64> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void FromBgra5551(Configuration configuration, ReadOnlySpan<Bgra5551> source, Span<TPixel> destination)
=> this.From(configuration, source, destination);
/// <inheritdoc />
public override void ToArgb32(Configuration configuration, ReadOnlySpan<TPixel> source, Span<Argb32> destination)
@ -209,50 +349,39 @@ internal class AssociatedAlphaPixelOperations<TPixel> : PixelOperations<TPixel>
Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref TPixel sourceBase = ref MemoryMarshal.GetReference(source);
ref TDestinationPixel destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)source.Length; i++)
if (source.IsEmpty)
{
Vector4 vector = this.ToUnassociatedScaledVector4(Unsafe.Add(ref sourceBase, i));
Unsafe.Add(ref destinationBase, i) = TDestinationPixel.FromScaledVector4(vector);
return;
}
}
/// <summary>
/// Converts an unassociated scaled vector to the associated representation of the destination pixel type.
/// </summary>
/// <param name="source">The unassociated scaled vector.</param>
/// <returns>The associated scaled vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector4 Associate(Vector4 source)
{
// Round-trip alpha through TPixel so the generic fallback associates RGB with the value the destination actually stores.
source.W = TPixel.FromScaledVector4(new Vector4(0, 0, 0, source.W)).ToScaledVector4().W;
Numerics.Premultiply(ref source);
return source;
}
int sliceLength = Math.Min(source.Length, 1024);
int numberOfSlices = source.Length / sliceLength;
/// <summary>
/// Reassociates a scaled vector with the alpha value the destination pixel can store.
/// </summary>
/// <param name="source">The associated scaled vector.</param>
/// <returns>The reassociated scaled vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector4 Reassociate(Vector4 source)
{
float alpha = source.W;
using IMemoryOwner<Vector4> tempVectors = configuration.MemoryAllocator.Allocate<Vector4>(sliceLength);
Span<Vector4> vectorSpan = tempVectors.GetSpan()[..sliceLength];
PixelOperations<TDestinationPixel> destinationOperations = PixelOperations<TDestinationPixel>.Instance;
if (alpha == 0)
// Generated destination dispatch routes conversion back through this source operation's ToX override. Extract through this
// operation's protected bulk hook, then use the destination's public modifier contract to avoid recursive dispatch.
for (int i = 0; i < numberOfSlices; i++)
{
return Vector4.Zero;
int start = i * sliceLength;
ReadOnlySpan<TPixel> sourceSlice = source.Slice(start, sliceLength);
Span<TDestinationPixel> destinationSlice = destination.Slice(start, sliceLength);
this.ToUnassociatedScaledVector4(configuration, sourceSlice, vectorSpan);
destinationOperations.FromVector4Destructive(configuration, vectorSpan, destinationSlice, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
}
float storedAlpha = TPixel.FromScaledVector4(new Vector4(0, 0, 0, alpha)).ToScaledVector4().W;
int endOfCompleteSlices = numberOfSlices * sliceLength;
int remainder = source.Length - endOfCompleteSlices;
// Associated RGB scales by the same ratio as alpha. Applying that ratio directly avoids the extra division and multiplication of an unpremultiply/premultiply round trip and preserves exact midpoints when alpha needs no quantization.
source *= storedAlpha / alpha;
source.W = storedAlpha;
return source;
if (remainder > 0)
{
ReadOnlySpan<TPixel> sourceSlice = source[endOfCompleteSlices..];
Span<TDestinationPixel> destinationSlice = destination.Slice(endOfCompleteSlices, remainder);
vectorSpan = vectorSpan[..remainder];
this.ToUnassociatedScaledVector4(configuration, sourceSlice, vectorSpan);
destinationOperations.FromVector4Destructive(configuration, vectorSpan, destinationSlice, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
}
}
}

266
src/ImageSharp/PixelFormats/HalfTypeHelper.cs

@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License.
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
namespace SixLabors.ImageSharp.PixelFormats;
@ -10,6 +11,19 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// </summary>
internal static class HalfTypeHelper
{
// These constants mirror the binary16 conversion used by System.Half. Keeping the vector conversion
// bit-for-bit equivalent to the scalar runtime conversion makes SIMD a pure throughput optimization.
private const uint HalfExponentMask = 0x7C00;
private const uint HalfSignMask = 0x8000;
private const uint HalfToSingleBitsMask = 0x0FFF_E000;
private const uint SingleExponentLowerBound = 0x3880_0000;
private const uint SingleExponentOffset = 0x3800_0000;
private const uint SingleExponent126 = 0x3F00_0000;
private const uint SingleBiasedExponentMask = 0x7F80_0000;
private const uint SingleExponent13 = 0x0680_0000;
private const uint SingleSignMask = 0x8000_0000;
private const float MaxHalfValueBelowInfinity = 65520F;
/// <summary>
/// Packs a <see cref="float"/> into an <see cref="ushort"/>
/// </summary>
@ -25,4 +39,256 @@ internal static class HalfTypeHelper
/// <returns>The <see cref="float"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static float Unpack(ushort value) => (float)BitConverter.UInt16BitsToHalf(value);
/// <summary>
/// Unpacks eight binary16 values into two vectors of single-precision values.
/// </summary>
/// <param name="value">The packed binary16 values.</param>
/// <returns>The unpacked lower and upper values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static (Vector128<float> Lower, Vector128<float> Upper) Unpack(Vector128<ushort> value)
{
(Vector128<uint> lower, Vector128<uint> upper) = Vector128.Widen(value);
return (ConvertHalfBitsToSingle(lower), ConvertHalfBitsToSingle(upper));
}
/// <summary>
/// Unpacks sixteen binary16 values into two vectors of single-precision values.
/// </summary>
/// <param name="value">The packed binary16 values.</param>
/// <returns>The unpacked lower and upper values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static (Vector256<float> Lower, Vector256<float> Upper) Unpack(Vector256<ushort> value)
{
(Vector256<uint> lower, Vector256<uint> upper) = Vector256.Widen(value);
return (ConvertHalfBitsToSingle(lower), ConvertHalfBitsToSingle(upper));
}
/// <summary>
/// Unpacks thirty-two binary16 values into two vectors of single-precision values.
/// </summary>
/// <param name="value">The packed binary16 values.</param>
/// <returns>The unpacked lower and upper values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static (Vector512<float> Lower, Vector512<float> Upper) Unpack(Vector512<ushort> value)
{
(Vector512<uint> lower, Vector512<uint> upper) = Vector512.Widen(value);
return (ConvertHalfBitsToSingle(lower), ConvertHalfBitsToSingle(upper));
}
/// <summary>
/// Packs eight single-precision values into binary16 storage.
/// </summary>
/// <param name="lower">The lower single-precision values.</param>
/// <param name="upper">The upper single-precision values.</param>
/// <returns>The packed binary16 values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector128<ushort> Pack(Vector128<float> lower, Vector128<float> upper)
=> Vector128.Narrow(ConvertSingleToHalfBits(lower), ConvertSingleToHalfBits(upper));
/// <summary>
/// Packs sixteen single-precision values into binary16 storage.
/// </summary>
/// <param name="lower">The lower single-precision values.</param>
/// <param name="upper">The upper single-precision values.</param>
/// <returns>The packed binary16 values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector256<ushort> Pack(Vector256<float> lower, Vector256<float> upper)
=> Vector256.Narrow(ConvertSingleToHalfBits(lower), ConvertSingleToHalfBits(upper));
/// <summary>
/// Packs thirty-two single-precision values into binary16 storage.
/// </summary>
/// <param name="lower">The lower single-precision values.</param>
/// <param name="upper">The upper single-precision values.</param>
/// <returns>The packed binary16 values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector512<ushort> Pack(Vector512<float> lower, Vector512<float> upper)
=> Vector512.Narrow(ConvertSingleToHalfBits(lower), ConvertSingleToHalfBits(upper));
/// <summary>
/// Rounds single-precision values through binary16 without changing the vector width.
/// </summary>
/// <param name="value">The single-precision values.</param>
/// <returns>The values after binary16 quantization.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector128<float> RoundToHalf(Vector128<float> value)
=> ConvertHalfBitsToSingle(ConvertSingleToHalfBits(value));
/// <summary>
/// Rounds single-precision values through binary16 without changing the vector width.
/// </summary>
/// <param name="value">The single-precision values.</param>
/// <returns>The values after binary16 quantization.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector256<float> RoundToHalf(Vector256<float> value)
=> ConvertHalfBitsToSingle(ConvertSingleToHalfBits(value));
/// <summary>
/// Rounds single-precision values through binary16 without changing the vector width.
/// </summary>
/// <param name="value">The single-precision values.</param>
/// <returns>The values after binary16 quantization.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Vector512<float> RoundToHalf(Vector512<float> value)
=> ConvertHalfBitsToSingle(ConvertSingleToHalfBits(value));
/// <summary>
/// Converts zero-extended binary16 bit patterns to single-precision values.
/// </summary>
/// <param name="value">The binary16 bit patterns.</param>
/// <returns>The converted single-precision values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<float> ConvertHalfBitsToSingle(Vector128<uint> value)
{
Vector128<uint> sign = Vector128.ShiftLeft(value & Vector128.Create(HalfSignMask), 16);
Vector128<uint> exponent = value & Vector128.Create(HalfExponentMask);
Vector128<uint> subnormalMask = Vector128.Equals(exponent, Vector128<uint>.Zero);
Vector128<uint> infinityOrNaNMask = Vector128.Equals(exponent, Vector128.Create(HalfExponentMask));
Vector128<uint> maskedExponentLowerBound = subnormalMask & Vector128.Create(SingleExponentLowerBound);
Vector128<uint> exponentOffset = Vector128.Create(SingleExponentOffset) | maskedExponentLowerBound;
// Binary16 and binary32 fraction fields differ by thirteen bits. Subnormals and special values
// need different exponent offsets before that shared field layout can be reinterpreted as float.
Vector128<uint> bits = Vector128.ShiftLeft(value, 13) & Vector128.Create(HalfToSingleBitsMask);
exponentOffset = Vector128.ConditionalSelect(infinityOrNaNMask, Vector128.ShiftLeft(exponentOffset, 1), exponentOffset);
bits += exponentOffset;
Vector128<uint> absoluteValue = (bits.AsSingle() - maskedExponentLowerBound.AsSingle()).AsUInt32();
return (absoluteValue | sign).AsSingle();
}
/// <summary>
/// Converts zero-extended binary16 bit patterns to single-precision values.
/// </summary>
/// <param name="value">The binary16 bit patterns.</param>
/// <returns>The converted single-precision values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<float> ConvertHalfBitsToSingle(Vector256<uint> value)
{
Vector256<uint> sign = Vector256.ShiftLeft(value & Vector256.Create(HalfSignMask), 16);
Vector256<uint> exponent = value & Vector256.Create(HalfExponentMask);
Vector256<uint> subnormalMask = Vector256.Equals(exponent, Vector256<uint>.Zero);
Vector256<uint> infinityOrNaNMask = Vector256.Equals(exponent, Vector256.Create(HalfExponentMask));
Vector256<uint> maskedExponentLowerBound = subnormalMask & Vector256.Create(SingleExponentLowerBound);
Vector256<uint> exponentOffset = Vector256.Create(SingleExponentOffset) | maskedExponentLowerBound;
// Binary16 and binary32 fraction fields differ by thirteen bits. Subnormals and special values
// need different exponent offsets before that shared field layout can be reinterpreted as float.
Vector256<uint> bits = Vector256.ShiftLeft(value, 13) & Vector256.Create(HalfToSingleBitsMask);
exponentOffset = Vector256.ConditionalSelect(infinityOrNaNMask, Vector256.ShiftLeft(exponentOffset, 1), exponentOffset);
bits += exponentOffset;
Vector256<uint> absoluteValue = (bits.AsSingle() - maskedExponentLowerBound.AsSingle()).AsUInt32();
return (absoluteValue | sign).AsSingle();
}
/// <summary>
/// Converts zero-extended binary16 bit patterns to single-precision values.
/// </summary>
/// <param name="value">The binary16 bit patterns.</param>
/// <returns>The converted single-precision values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<float> ConvertHalfBitsToSingle(Vector512<uint> value)
{
Vector512<uint> sign = Vector512.ShiftLeft(value & Vector512.Create(HalfSignMask), 16);
Vector512<uint> exponent = value & Vector512.Create(HalfExponentMask);
Vector512<uint> subnormalMask = Vector512.Equals(exponent, Vector512<uint>.Zero);
Vector512<uint> infinityOrNaNMask = Vector512.Equals(exponent, Vector512.Create(HalfExponentMask));
Vector512<uint> maskedExponentLowerBound = subnormalMask & Vector512.Create(SingleExponentLowerBound);
Vector512<uint> exponentOffset = Vector512.Create(SingleExponentOffset) | maskedExponentLowerBound;
// Binary16 and binary32 fraction fields differ by thirteen bits. Subnormals and special values
// need different exponent offsets before that shared field layout can be reinterpreted as float.
Vector512<uint> bits = Vector512.ShiftLeft(value, 13) & Vector512.Create(HalfToSingleBitsMask);
exponentOffset = Vector512.ConditionalSelect(infinityOrNaNMask, Vector512.ShiftLeft(exponentOffset, 1), exponentOffset);
bits += exponentOffset;
Vector512<uint> absoluteValue = (bits.AsSingle() - maskedExponentLowerBound.AsSingle()).AsUInt32();
return (absoluteValue | sign).AsSingle();
}
/// <summary>
/// Converts single-precision values to zero-extended binary16 bit patterns.
/// </summary>
/// <param name="value">The single-precision values.</param>
/// <returns>The binary16 bit patterns in 32-bit lanes.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<uint> ConvertSingleToHalfBits(Vector128<float> value)
{
Vector128<uint> bits = value.AsUInt32();
Vector128<uint> sign = Vector128.ShiftRightLogical(bits & Vector128.Create(SingleSignMask), 16);
Vector128<uint> realMask = Vector128.Equals(value, value).AsUInt32();
value = Vector128.Abs(value);
value = Vector128.Min(Vector128.Create(MaxHalfValueBelowInfinity), value);
Vector128<uint> exponentOffset = Vector128.Max(value, Vector128.Create(SingleExponentLowerBound).AsSingle()).AsUInt32();
exponentOffset &= Vector128.Create(SingleBiasedExponentMask);
exponentOffset += Vector128.Create(SingleExponent13);
// Adding an exponent-sized float rounds the significand to binary16 precision using IEEE
// round-to-nearest-even. The remaining integer operations realign the exponent and sign fields.
value += exponentOffset.AsSingle();
bits = value.AsUInt32() - Vector128.Create(SingleExponent126);
Vector128<uint> newExponent = Vector128.ShiftRightLogical(bits, 13);
Vector128<uint> maskedHalfExponentForNaN = ~realMask & Vector128.Create(HalfExponentMask);
bits &= realMask;
bits += newExponent;
bits &= ~maskedHalfExponentForNaN;
return bits | maskedHalfExponentForNaN | sign;
}
/// <summary>
/// Converts single-precision values to zero-extended binary16 bit patterns.
/// </summary>
/// <param name="value">The single-precision values.</param>
/// <returns>The binary16 bit patterns in 32-bit lanes.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<uint> ConvertSingleToHalfBits(Vector256<float> value)
{
Vector256<uint> bits = value.AsUInt32();
Vector256<uint> sign = Vector256.ShiftRightLogical(bits & Vector256.Create(SingleSignMask), 16);
Vector256<uint> realMask = Vector256.Equals(value, value).AsUInt32();
value = Vector256.Abs(value);
value = Vector256.Min(Vector256.Create(MaxHalfValueBelowInfinity), value);
Vector256<uint> exponentOffset = Vector256.Max(value, Vector256.Create(SingleExponentLowerBound).AsSingle()).AsUInt32();
exponentOffset &= Vector256.Create(SingleBiasedExponentMask);
exponentOffset += Vector256.Create(SingleExponent13);
// Adding an exponent-sized float rounds the significand to binary16 precision using IEEE
// round-to-nearest-even. The remaining integer operations realign the exponent and sign fields.
value += exponentOffset.AsSingle();
bits = value.AsUInt32() - Vector256.Create(SingleExponent126);
Vector256<uint> newExponent = Vector256.ShiftRightLogical(bits, 13);
Vector256<uint> maskedHalfExponentForNaN = ~realMask & Vector256.Create(HalfExponentMask);
bits &= realMask;
bits += newExponent;
bits &= ~maskedHalfExponentForNaN;
return bits | maskedHalfExponentForNaN | sign;
}
/// <summary>
/// Converts single-precision values to zero-extended binary16 bit patterns.
/// </summary>
/// <param name="value">The single-precision values.</param>
/// <returns>The binary16 bit patterns in 32-bit lanes.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<uint> ConvertSingleToHalfBits(Vector512<float> value)
{
Vector512<uint> bits = value.AsUInt32();
Vector512<uint> sign = Vector512.ShiftRightLogical(bits & Vector512.Create(SingleSignMask), 16);
Vector512<uint> realMask = Vector512.Equals(value, value).AsUInt32();
value = Vector512.Abs(value);
value = Vector512.Min(Vector512.Create(MaxHalfValueBelowInfinity), value);
Vector512<uint> exponentOffset = Vector512.Max(value, Vector512.Create(SingleExponentLowerBound).AsSingle()).AsUInt32();
exponentOffset &= Vector512.Create(SingleBiasedExponentMask);
exponentOffset += Vector512.Create(SingleExponent13);
// Adding an exponent-sized float rounds the significand to binary16 precision using IEEE
// round-to-nearest-even. The remaining integer operations realign the exponent and sign fields.
value += exponentOffset.AsSingle();
bits = value.AsUInt32() - Vector512.Create(SingleExponent126);
Vector512<uint> newExponent = Vector512.ShiftRightLogical(bits, 13);
Vector512<uint> maskedHalfExponentForNaN = ~realMask & Vector512.Create(HalfExponentMask);
bits &= realMask;
bits += newExponent;
bits &= ~maskedHalfExponentForNaN;
return bits | maskedHalfExponentForNaN | sign;
}
}

118
src/ImageSharp/PixelFormats/IPixel.cs

@ -14,120 +14,150 @@ namespace SixLabors.ImageSharp.PixelFormats;
public interface IPixel<TSelf> : IPixel, IEquatable<TSelf>
where TSelf : unmanaged, IPixel<TSelf>
{
#pragma warning disable CA1000 // Do not declare static members on generic types
/// <summary>
/// Creates a <see cref="PixelOperations{TPixel}"/> instance for this pixel type.
/// This method is not intended to be consumed directly. Use <see cref="PixelOperations{TPixel}.Instance"/> instead.
/// </summary>
/// <returns>The <see cref="PixelOperations{TPixel}"/> instance.</returns>
static abstract PixelOperations<TSelf> CreatePixelOperations();
public static abstract PixelOperations<TSelf> CreatePixelOperations();
/// <summary>
/// Initializes the pixel instance from a generic a generic ("scaled") <see cref="Vector4"/> representation
/// with values scaled and clamped between <value>0</value> and <value>1</value>
/// Initializes the pixel instance from a generic ("scaled") <see cref="Vector4"/> representation using the pixel type's native alpha representation.
/// The scaled representation uses <value>0</value> and <value>1</value> as its nominal component bounds.
/// </summary>
/// <param name="source">The vector to load the pixel from.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromScaledVector4(Vector4 source);
public static abstract TSelf FromScaledVector4(Vector4 source);
/// <summary>
/// Initializes the pixel instance from a <see cref="Vector4"/> which is specific to the current pixel type.
/// Initializes the pixel instance from a generic ("scaled") <see cref="Vector4"/> whose color components use unassociated alpha.
/// The scaled representation uses <value>0</value> and <value>1</value> as its nominal component bounds.
/// </summary>
/// <param name="source">The vector to load the pixel from.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromVector4(Vector4 source);
public static abstract TSelf FromUnassociatedScaledVector4(Vector4 source);
/// <summary>
/// Initializes the pixel instance from a generic ("scaled") <see cref="Vector4"/> whose color components use associated alpha,
/// representing color multiplied by the logical opacity represented by alpha.
/// The scaled representation uses <value>0</value> and <value>1</value> as its nominal component bounds.
/// </summary>
/// <param name="source">The vector to load the pixel from.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
public static abstract TSelf FromAssociatedScaledVector4(Vector4 source);
/// <summary>
/// Initializes the pixel instance from a <see cref="Vector4"/> which is specific to the current pixel type and uses its native alpha representation.
/// </summary>
/// <param name="source">The vector to load the pixel from.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
public static abstract TSelf FromVector4(Vector4 source);
/// <summary>
/// Initializes the pixel instance from a <see cref="Vector4"/> which is specific to the current pixel type and whose color components use unassociated alpha.
/// </summary>
/// <param name="source">The vector to load the pixel from.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
public static abstract TSelf FromUnassociatedVector4(Vector4 source);
/// <summary>
/// Initializes the pixel instance from a <see cref="Vector4"/> which is specific to the current pixel type and whose color components use associated alpha,
/// representing color multiplied by the logical opacity represented by alpha.
/// </summary>
/// <param name="source">The vector to load the pixel from.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
public static abstract TSelf FromAssociatedVector4(Vector4 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="Abgr32"/> value.
/// </summary>
/// <param name="source">The <see cref="Abgr32"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromAbgr32(Abgr32 source);
public static abstract TSelf FromAbgr32(Abgr32 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="Argb32"/> value.
/// </summary>
/// <param name="source">The <see cref="Argb32"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromArgb32(Argb32 source);
public static abstract TSelf FromArgb32(Argb32 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="Bgra5551"/> value.
/// </summary>
/// <param name="source">The <see cref="Bgra5551"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromBgra5551(Bgra5551 source);
public static abstract TSelf FromBgra5551(Bgra5551 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="Bgr24"/> value.
/// </summary>
/// <param name="source">The <see cref="Bgr24"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromBgr24(Bgr24 source);
public static abstract TSelf FromBgr24(Bgr24 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="Bgra32"/> value.
/// </summary>
/// <param name="source">The <see cref="Bgra32"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromBgra32(Bgra32 source);
public static abstract TSelf FromBgra32(Bgra32 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="L8"/> value.
/// </summary>
/// <param name="source">The <see cref="L8"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromL8(L8 source);
public static abstract TSelf FromL8(L8 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="L16"/> value.
/// </summary>
/// <param name="source">The <see cref="L16"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromL16(L16 source);
public static abstract TSelf FromL16(L16 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="La16"/> value.
/// </summary>
/// <param name="source">The <see cref="La16"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromLa16(La16 source);
public static abstract TSelf FromLa16(La16 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="La32"/> value.
/// </summary>
/// <param name="source">The <see cref="La32"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromLa32(La32 source);
public static abstract TSelf FromLa32(La32 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="Rgb24"/> value.
/// </summary>
/// <param name="source">The <see cref="Rgb24"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromRgb24(Rgb24 source);
public static abstract TSelf FromRgb24(Rgb24 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="Rgba32"/> value.
/// </summary>
/// <param name="source">The <see cref="Rgba32"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromRgba32(Rgba32 source);
public static abstract TSelf FromRgba32(Rgba32 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="Rgb48"/> value.
/// </summary>
/// <param name="source">The <see cref="Rgb48"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromRgb48(Rgb48 source);
public static abstract TSelf FromRgb48(Rgb48 source);
/// <summary>
/// Initializes the pixel instance from an <see cref="Rgba64"/> value.
/// </summary>
/// <param name="source">The <see cref="Rgba64"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromRgba64(Rgba64 source);
#pragma warning restore CA1000 // Do not declare static members on generic types
public static abstract TSelf FromRgba64(Rgba64 source);
}
/// <summary>
@ -139,26 +169,58 @@ public interface IPixel
/// Gets the pixel type information.
/// </summary>
/// <returns>The <see cref="PixelTypeInfo"/>.</returns>
static abstract PixelTypeInfo GetPixelTypeInfo();
public static abstract PixelTypeInfo GetPixelTypeInfo();
/// <summary>
/// Convert the pixel instance into <see cref="Rgba32"/> representation.
/// </summary>
/// <returns>The <see cref="Rgba32"/></returns>
Rgba32 ToRgba32();
public Rgba32 ToRgba32();
/// <summary>
/// Expands the pixel into a generic ("scaled") <see cref="Vector4"/> representation
/// with values scaled and clamped between <value>0</value> and <value>1</value>.
/// Expands the pixel into a generic ("scaled") <see cref="Vector4"/> representation using the pixel type's native alpha representation.
/// The scaled representation uses <value>0</value> and <value>1</value> as its nominal component bounds.
/// The vector components are typically expanded in least to greatest significance order.
/// </summary>
/// <returns>The <see cref="Vector4"/>.</returns>
Vector4 ToScaledVector4();
public Vector4 ToScaledVector4();
/// <summary>
/// Expands the pixel into a generic ("scaled") <see cref="Vector4"/> whose color components use unassociated alpha.
/// The scaled representation uses <value>0</value> and <value>1</value> as its nominal component bounds.
/// When alpha is zero and the pixel's native representation is associated, the color components remain unchanged
/// because no unassociated value can be recovered.
/// </summary>
/// <returns>The <see cref="Vector4"/>.</returns>
public Vector4 ToUnassociatedScaledVector4();
/// <summary>
/// Expands the pixel into a generic ("scaled") <see cref="Vector4"/> whose color components use associated alpha,
/// representing color multiplied by the logical opacity represented by alpha.
/// The scaled representation uses <value>0</value> and <value>1</value> as its nominal component bounds.
/// </summary>
/// <returns>The <see cref="Vector4"/>.</returns>
public Vector4 ToAssociatedScaledVector4();
/// <summary>
/// Expands the pixel into a <see cref="Vector4"/> which is specific to the current pixel type.
/// Expands the pixel into a <see cref="Vector4"/> which is specific to the current pixel type and uses its native alpha representation.
/// The vector components are typically expanded in least to greatest significance order.
/// </summary>
/// <returns>The <see cref="Vector4"/>.</returns>
Vector4 ToVector4();
public Vector4 ToVector4();
/// <summary>
/// Expands the pixel into a <see cref="Vector4"/> which is specific to the current pixel type and whose color components use unassociated alpha.
/// When alpha is zero and the pixel's native representation is associated, the color components remain unchanged
/// because no unassociated value can be recovered.
/// </summary>
/// <returns>The <see cref="Vector4"/>.</returns>
public Vector4 ToUnassociatedVector4();
/// <summary>
/// Expands the pixel into a <see cref="Vector4"/> which is specific to the current pixel type and whose color components use associated alpha,
/// representing color multiplied by the logical opacity represented by alpha.
/// </summary>
/// <returns>The <see cref="Vector4"/>.</returns>
public Vector4 ToAssociatedVector4();
}

216
src/ImageSharp/PixelFormats/PixelBlenders/AssociatedAlphaPixelBlenders.Generated.cs

@ -30,7 +30,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalSrc(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -809,7 +809,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrc(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -1588,7 +1588,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddSrc(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -2367,7 +2367,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrc(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -3146,7 +3146,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrc(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -3925,7 +3925,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrc(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -4704,7 +4704,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenSrc(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -5483,7 +5483,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrc(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -6262,7 +6262,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrc(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -7041,7 +7041,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalSrcAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -7820,7 +7820,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrcAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -8599,7 +8599,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddSrcAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -9378,7 +9378,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrcAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -10157,7 +10157,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrcAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -10936,7 +10936,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrcAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -11715,7 +11715,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenSrcAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -12494,7 +12494,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrcAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -13273,7 +13273,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrcAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -14052,7 +14052,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalSrcOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -14831,7 +14831,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrcOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -15610,7 +15610,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddSrcOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -16389,7 +16389,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrcOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -17168,7 +17168,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrcOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -17947,7 +17947,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrcOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -18726,7 +18726,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenSrcOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -19505,7 +19505,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrcOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -20284,7 +20284,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrcOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -21063,7 +21063,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalSrcIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -21842,7 +21842,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrcIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -22621,7 +22621,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddSrcIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -23400,7 +23400,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrcIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -24179,7 +24179,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrcIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -24958,7 +24958,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrcIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -25737,7 +25737,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenSrcIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -26516,7 +26516,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrcIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -27295,7 +27295,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrcIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -28074,7 +28074,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalSrcOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -28853,7 +28853,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplySrcOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -29632,7 +29632,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddSrcOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -30411,7 +30411,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractSrcOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -31190,7 +31190,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenSrcOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -31969,7 +31969,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenSrcOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -32748,7 +32748,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenSrcOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -33527,7 +33527,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlaySrcOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -34306,7 +34306,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightSrcOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -35085,7 +35085,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalDest(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -35864,7 +35864,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDest(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -36643,7 +36643,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddDest(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -37422,7 +37422,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractDest(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -38201,7 +38201,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenDest(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -38980,7 +38980,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenDest(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -39759,7 +39759,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenDest(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -40538,7 +40538,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlayDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlayDest(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -41317,7 +41317,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightDest(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -42096,7 +42096,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalDestAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -42875,7 +42875,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDestAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -43654,7 +43654,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddDestAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -44433,7 +44433,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractDestAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -45212,7 +45212,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenDestAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -45991,7 +45991,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenDestAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -46770,7 +46770,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenDestAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -47549,7 +47549,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlayDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlayDestAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -48328,7 +48328,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightDestAtop(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -49107,7 +49107,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalDestOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -49886,7 +49886,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDestOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -50665,7 +50665,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddDestOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -51444,7 +51444,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractDestOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -52223,7 +52223,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenDestOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -53002,7 +53002,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenDestOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -53781,7 +53781,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenDestOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -54560,7 +54560,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlayDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlayDestOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -55339,7 +55339,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightDestOver(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -56118,7 +56118,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalDestIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -56897,7 +56897,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDestIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -57676,7 +57676,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddDestIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -58455,7 +58455,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractDestIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -59234,7 +59234,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenDestIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -60013,7 +60013,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenDestIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -60792,7 +60792,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenDestIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -61571,7 +61571,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlayDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlayDestIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -62350,7 +62350,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightDestIn(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -63129,7 +63129,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalDestOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -63908,7 +63908,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplyDestOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -64687,7 +64687,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddDestOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -65466,7 +65466,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractDestOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -66245,7 +66245,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenDestOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -67024,7 +67024,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenDestOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -67803,7 +67803,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenDestOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -68582,7 +68582,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlayDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlayDestOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -69361,7 +69361,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightDestOut(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -70140,7 +70140,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalClear(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -70919,7 +70919,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplyClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplyClear(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -71698,7 +71698,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddClear(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -72477,7 +72477,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractClear(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -73256,7 +73256,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenClear(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -74035,7 +74035,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenClear(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -74814,7 +74814,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenClear(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -75593,7 +75593,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlayClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlayClear(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -76372,7 +76372,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightClear(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -77151,7 +77151,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.NormalXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.NormalXor(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -77930,7 +77930,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.MultiplyXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.MultiplyXor(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -78709,7 +78709,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.AddXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.AddXor(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -79488,7 +79488,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.SubtractXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.SubtractXor(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -80267,7 +80267,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.ScreenXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.ScreenXor(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -81046,7 +81046,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.DarkenXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.DarkenXor(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -81825,7 +81825,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.LightenXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.LightenXor(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -82604,7 +82604,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.OverlayXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.OverlayXor(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -83383,7 +83383,7 @@ internal static partial class AssociatedAlphaPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.HardLightXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.HardLightXor(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />

2
src/ImageSharp/PixelFormats/PixelBlenders/AssociatedAlphaPixelBlenders.Generated.tt

@ -73,7 +73,7 @@ var blenders = new []{
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return FromBlendVector4(AssociatedAlphaPorterDuffFunctions.<#=blender_composer#>(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromAssociatedScaledVector4(AssociatedAlphaPorterDuffFunctions.<#=blender_composer#>(background.ToAssociatedScaledVector4(), source.ToAssociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />

16
src/ImageSharp/PixelFormats/PixelBlenders/AssociatedAlphaPixelBlender{TPixel}.cs

@ -12,8 +12,7 @@ namespace SixLabors.ImageSharp.PixelFormats.PixelBlenders;
internal abstract class AssociatedAlphaPixelBlender<TPixel> : PixelBlender<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
// Associated blenders are created only by AssociatedAlphaPixelOperations, so the cached cast establishes the format-specific output boundary once per closed pixel type.
private static readonly AssociatedAlphaPixelOperations<TPixel> Operations = (AssociatedAlphaPixelOperations<TPixel>)PixelOperations<TPixel>.Instance;
private static readonly PixelOperations<TPixel> Operations = PixelOperations<TPixel>.Instance;
/// <inheritdoc />
protected override void ToBlendVector4<TPixelSource>(
@ -22,18 +21,11 @@ internal abstract class AssociatedAlphaPixelBlender<TPixel> : PixelBlender<TPixe
Span<Vector4> destination)
{
// Selecting the source representation once per row avoids a format check for every blended pixel.
PixelOperations<TPixelSource>.Instance.ToAssociatedScaledVector4(configuration, source, destination);
PixelOperations<TPixelSource>.Instance.ToVector4(configuration, source, destination, PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply);
}
/// <inheritdoc />
protected override Vector4 ToBlendVector4(TPixel source) => source.ToScaledVector4();
/// <summary>
/// Converts an associated blend result to the destination pixel representation.
/// </summary>
/// <param name="source">The associated blend result.</param>
/// <returns>The destination pixel.</returns>
public static TPixel FromBlendVector4(Vector4 source) => Operations.FromAssociatedScaledVector4(source);
protected override Vector4 ToBlendVector4(TPixel source) => source.ToAssociatedScaledVector4();
/// <inheritdoc />
protected override void FromBlendVector4(
@ -41,6 +33,6 @@ internal abstract class AssociatedAlphaPixelBlender<TPixel> : PixelBlender<TPixe
Span<Vector4> source,
Span<TPixel> destination)
{
Operations.FromAssociatedScaledVector4(configuration, source, destination);
Operations.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply);
}
}

216
src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs

@ -38,7 +38,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalSrc(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -817,7 +817,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplySrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplySrc(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -1596,7 +1596,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddSrc(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -2375,7 +2375,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractSrc(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -3154,7 +3154,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenSrc(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -3933,7 +3933,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenSrc(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -4712,7 +4712,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenSrc(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -5491,7 +5491,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlaySrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlaySrc(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -6270,7 +6270,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightSrc(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightSrc(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -7049,7 +7049,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalSrcAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -7828,7 +7828,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplySrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplySrcAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -8607,7 +8607,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddSrcAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -9386,7 +9386,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractSrcAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -10165,7 +10165,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenSrcAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -10944,7 +10944,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenSrcAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -11723,7 +11723,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenSrcAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -12502,7 +12502,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlaySrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlaySrcAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -13281,7 +13281,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightSrcAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightSrcAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -14060,7 +14060,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalSrcOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -14839,7 +14839,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplySrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplySrcOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -15618,7 +15618,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddSrcOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -16397,7 +16397,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractSrcOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -17176,7 +17176,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenSrcOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -17955,7 +17955,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenSrcOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -18734,7 +18734,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenSrcOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -19513,7 +19513,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlaySrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlaySrcOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -20292,7 +20292,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightSrcOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightSrcOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -21071,7 +21071,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalSrcIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -21850,7 +21850,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplySrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplySrcIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -22629,7 +22629,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddSrcIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -23408,7 +23408,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractSrcIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -24187,7 +24187,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenSrcIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -24966,7 +24966,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenSrcIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -25745,7 +25745,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenSrcIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -26524,7 +26524,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlaySrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlaySrcIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -27303,7 +27303,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightSrcIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightSrcIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -28082,7 +28082,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalSrcOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -28861,7 +28861,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplySrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplySrcOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -29640,7 +29640,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddSrcOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -30419,7 +30419,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractSrcOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -31198,7 +31198,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenSrcOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -31977,7 +31977,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenSrcOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -32756,7 +32756,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenSrcOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -33535,7 +33535,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlaySrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlaySrcOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -34314,7 +34314,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightSrcOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightSrcOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -35093,7 +35093,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalDest(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -35872,7 +35872,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplyDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplyDest(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -36651,7 +36651,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddDest(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -37430,7 +37430,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractDest(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -38209,7 +38209,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenDest(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -38988,7 +38988,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenDest(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -39767,7 +39767,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenDest(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -40546,7 +40546,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlayDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlayDest(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -41325,7 +41325,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightDest(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightDest(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -42104,7 +42104,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalDestAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -42883,7 +42883,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplyDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplyDestAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -43662,7 +43662,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddDestAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -44441,7 +44441,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractDestAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -45220,7 +45220,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenDestAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -45999,7 +45999,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenDestAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -46778,7 +46778,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenDestAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -47557,7 +47557,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlayDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlayDestAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -48336,7 +48336,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightDestAtop(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightDestAtop(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -49115,7 +49115,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalDestOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -49894,7 +49894,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplyDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplyDestOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -50673,7 +50673,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddDestOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -51452,7 +51452,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractDestOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -52231,7 +52231,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenDestOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -53010,7 +53010,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenDestOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -53789,7 +53789,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenDestOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -54568,7 +54568,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlayDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlayDestOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -55347,7 +55347,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightDestOver(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightDestOver(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -56126,7 +56126,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalDestIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -56905,7 +56905,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplyDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplyDestIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -57684,7 +57684,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddDestIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -58463,7 +58463,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractDestIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -59242,7 +59242,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenDestIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -60021,7 +60021,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenDestIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -60800,7 +60800,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenDestIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -61579,7 +61579,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlayDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlayDestIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -62358,7 +62358,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightDestIn(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightDestIn(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -63137,7 +63137,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalDestOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -63916,7 +63916,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplyDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplyDestOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -64695,7 +64695,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddDestOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -65474,7 +65474,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractDestOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -66253,7 +66253,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenDestOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -67032,7 +67032,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenDestOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -67811,7 +67811,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenDestOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -68590,7 +68590,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlayDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlayDestOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -69369,7 +69369,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightDestOut(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightDestOut(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -70148,7 +70148,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalClear(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -70927,7 +70927,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplyClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplyClear(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -71706,7 +71706,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddClear(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -72485,7 +72485,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractClear(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -73264,7 +73264,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenClear(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -74043,7 +74043,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenClear(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -74822,7 +74822,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenClear(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -75601,7 +75601,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlayClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlayClear(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -76380,7 +76380,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightClear(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightClear(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -77159,7 +77159,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.NormalXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.NormalXor(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -77938,7 +77938,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.MultiplyXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.MultiplyXor(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -78717,7 +78717,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.AddXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.AddXor(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -79496,7 +79496,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.SubtractXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.SubtractXor(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -80275,7 +80275,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.ScreenXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.ScreenXor(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -81054,7 +81054,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.DarkenXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.DarkenXor(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -81833,7 +81833,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.LightenXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.LightenXor(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -82612,7 +82612,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.OverlayXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.OverlayXor(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />
@ -83391,7 +83391,7 @@ internal static class DefaultPixelBlenders<TPixel>
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.HardLightXor(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.HardLightXor(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />

2
src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.tt

@ -81,7 +81,7 @@ var blenders = new []{
/// <inheritdoc />
public override TPixel Blend(TPixel background, TPixel source, float amount)
{
return TPixel.FromScaledVector4(PorterDuffFunctions.<#=blender_composer#>(background.ToScaledVector4(), source.ToScaledVector4(), Numerics.Clamp(amount, 0, 1)));
return TPixel.FromUnassociatedScaledVector4(PorterDuffFunctions.<#=blender_composer#>(background.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), Numerics.Clamp(amount, 0, 1)));
}
/// <inheritdoc />

216
src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.cs

@ -505,7 +505,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalSrc(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalSrc(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -521,7 +521,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalSrcAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalSrcAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -537,7 +537,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalSrcOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalSrcOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -553,7 +553,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalSrcIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalSrcIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -569,7 +569,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalSrcOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalSrcOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -585,7 +585,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalDest(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalDest(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -601,7 +601,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalDestAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalDestAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -617,7 +617,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalDestOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalDestOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -633,7 +633,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalDestIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalDestIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -649,7 +649,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalDestOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalDestOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -665,7 +665,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalClear(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalClear(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -681,7 +681,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(NormalXor(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(NormalXor(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1176,7 +1176,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplySrc(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplySrc(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1192,7 +1192,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplySrcAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplySrcAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1208,7 +1208,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplySrcOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplySrcOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1224,7 +1224,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplySrcIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplySrcIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1240,7 +1240,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplySrcOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplySrcOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1256,7 +1256,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplyDest(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplyDest(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1272,7 +1272,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplyDestAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplyDestAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1288,7 +1288,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplyDestOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplyDestOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1304,7 +1304,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplyDestIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplyDestIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1320,7 +1320,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplyDestOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplyDestOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1336,7 +1336,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplyClear(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplyClear(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1352,7 +1352,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(MultiplyXor(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(MultiplyXor(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1847,7 +1847,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddSrc(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddSrc(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1863,7 +1863,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddSrcAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddSrcAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1879,7 +1879,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddSrcOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddSrcOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1895,7 +1895,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddSrcIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddSrcIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1911,7 +1911,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddSrcOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddSrcOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1927,7 +1927,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddDest(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddDest(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1943,7 +1943,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddDestAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddDestAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1959,7 +1959,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddDestOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddDestOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1975,7 +1975,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddDestIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddDestIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -1991,7 +1991,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddDestOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddDestOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2007,7 +2007,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddClear(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddClear(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2023,7 +2023,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(AddXor(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(AddXor(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2518,7 +2518,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractSrc(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractSrc(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2534,7 +2534,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractSrcAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractSrcAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2550,7 +2550,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractSrcOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractSrcOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2566,7 +2566,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractSrcIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractSrcIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2582,7 +2582,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractSrcOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractSrcOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2598,7 +2598,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractDest(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractDest(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2614,7 +2614,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractDestAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractDestAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2630,7 +2630,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractDestOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractDestOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2646,7 +2646,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractDestIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractDestIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2662,7 +2662,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractDestOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractDestOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2678,7 +2678,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractClear(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractClear(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -2694,7 +2694,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(SubtractXor(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(SubtractXor(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3189,7 +3189,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenSrc(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenSrc(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3205,7 +3205,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenSrcAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenSrcAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3221,7 +3221,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenSrcOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenSrcOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3237,7 +3237,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenSrcIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenSrcIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3253,7 +3253,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenSrcOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenSrcOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3269,7 +3269,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenDest(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenDest(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3285,7 +3285,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenDestAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenDestAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3301,7 +3301,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenDestOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenDestOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3317,7 +3317,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenDestIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenDestIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3333,7 +3333,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenDestOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenDestOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3349,7 +3349,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenClear(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenClear(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3365,7 +3365,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(ScreenXor(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(ScreenXor(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3860,7 +3860,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenSrc(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenSrc(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3876,7 +3876,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenSrcAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenSrcAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3892,7 +3892,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenSrcOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenSrcOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3908,7 +3908,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenSrcIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenSrcIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3924,7 +3924,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenSrcOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenSrcOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3940,7 +3940,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenDest(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenDest(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3956,7 +3956,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenDestAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenDestAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3972,7 +3972,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenDestOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenDestOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -3988,7 +3988,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenDestIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenDestIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4004,7 +4004,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenDestOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenDestOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4020,7 +4020,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenClear(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenClear(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4036,7 +4036,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(DarkenXor(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(DarkenXor(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4531,7 +4531,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenSrc(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenSrc(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4547,7 +4547,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenSrcAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenSrcAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4563,7 +4563,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenSrcOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenSrcOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4579,7 +4579,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenSrcIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenSrcIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4595,7 +4595,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenSrcOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenSrcOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4611,7 +4611,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenDest(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenDest(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4627,7 +4627,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenDestAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenDestAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4643,7 +4643,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenDestOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenDestOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4659,7 +4659,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenDestIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenDestIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4675,7 +4675,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenDestOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenDestOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4691,7 +4691,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenClear(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenClear(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -4707,7 +4707,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(LightenXor(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(LightenXor(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5202,7 +5202,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlaySrc(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlaySrc(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5218,7 +5218,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlaySrcAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlaySrcAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5234,7 +5234,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlaySrcOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlaySrcOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5250,7 +5250,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlaySrcIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlaySrcIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5266,7 +5266,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlaySrcOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlaySrcOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5282,7 +5282,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlayDest(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlayDest(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5298,7 +5298,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlayDestAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlayDestAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5314,7 +5314,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlayDestOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlayDestOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5330,7 +5330,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlayDestIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlayDestIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5346,7 +5346,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlayDestOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlayDestOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5362,7 +5362,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlayClear(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlayClear(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5378,7 +5378,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(OverlayXor(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(OverlayXor(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5873,7 +5873,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightSrc(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightSrc(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5889,7 +5889,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightSrcAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightSrcAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5905,7 +5905,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightSrcOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightSrcOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5921,7 +5921,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightSrcIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightSrcIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5937,7 +5937,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightSrcOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightSrcOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5953,7 +5953,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightDest(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightDest(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5969,7 +5969,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightDestAtop(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightDestAtop(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -5985,7 +5985,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightDestOver(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightDestOver(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -6001,7 +6001,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightDestIn(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightDestIn(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -6017,7 +6017,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightDestOut(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightDestOut(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -6033,7 +6033,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightClear(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightClear(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
/// <summary>
@ -6049,6 +6049,6 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(HardLightXor(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(HardLightXor(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
}

2
src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.tt

@ -518,7 +518,7 @@ internal static partial class PorterDuffFunctions
where TPixel : unmanaged, IPixel<TPixel>
{
opacity = Numerics.Clamp(opacity, 0, 1);
return TPixel.FromScaledVector4(<#=blender#><#=composer#>(backdrop.ToScaledVector4(), source.ToScaledVector4(), opacity));
return TPixel.FromUnassociatedScaledVector4(<#=blender#><#=composer#>(backdrop.ToUnassociatedScaledVector4(), source.ToUnassociatedScaledVector4(), opacity));
}
<# } #>
<#

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

@ -718,7 +718,7 @@ public abstract class PixelBlender<TPixel>
ReadOnlySpan<TPixelSource> source,
Span<Vector4> destination)
where TPixelSource : unmanaged, IPixel<TPixelSource>
=> PixelOperations<TPixelSource>.Instance.ToUnassociatedScaledVector4(configuration, source, destination);
=> PixelOperations<TPixelSource>.Instance.ToVector4(configuration, source, destination, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
/// <summary>
/// Converts a source pixel to the vector representation consumed by the blend functions.
@ -726,7 +726,7 @@ public abstract class PixelBlender<TPixel>
/// <param name="source">The source pixel.</param>
/// <returns>The source vector.</returns>
protected virtual Vector4 ToBlendVector4(TPixel source)
=> PixelOperations<TPixel>.Instance.ToUnassociatedScaledVector4(source);
=> source.ToUnassociatedScaledVector4();
/// <summary>
/// Converts blend results from this blender's scaled-vector representation to destination pixels.
@ -738,7 +738,7 @@ public abstract class PixelBlender<TPixel>
Configuration configuration,
Span<Vector4> source,
Span<TPixel> destination)
=> PixelOperations<TPixel>.Instance.FromUnassociatedScaledVector4(configuration, source, destination);
=> PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
/// <summary>
/// Blend 2 rows together.

20
src/ImageSharp/PixelFormats/PixelConversionModifiers.cs

@ -6,32 +6,38 @@ using SixLabors.ImageSharp.ColorProfiles.Companding;
namespace SixLabors.ImageSharp.PixelFormats;
/// <summary>
/// Flags responsible to select additional operations which could be efficiently applied in
/// Specifies vector representation transformations applied by
/// <see cref="PixelOperations{TPixel}.ToVector4(Configuration,ReadOnlySpan{TPixel},Span{System.Numerics.Vector4},PixelConversionModifiers)"/>
/// or
/// and
/// <see cref="PixelOperations{TPixel}.FromVector4Destructive(Configuration,Span{System.Numerics.Vector4},Span{TPixel},PixelConversionModifiers)"/>
/// knowing the pixel type.
/// during bulk pixel conversion.
/// </summary>
[Flags]
public enum PixelConversionModifiers
{
/// <summary>
/// No special operation is selected
/// Preserves the pixel format's native numeric range and alpha representation.
/// </summary>
None = 0,
/// <summary>
/// Select <see cref="IPixel.ToScaledVector4"/> and <see cref="IPixel{T}.FromScaledVector4"/> instead the standard (non scaled) variants.
/// Requests the scaled numeric range represented by <see cref="IPixel.ToScaledVector4"/> and <see cref="IPixel{T}.FromScaledVector4"/>.
/// </summary>
Scale = 1 << 0,
/// <summary>
/// Enable alpha premultiplication / unpremultiplication
/// Requests associated alpha for the vector representation.
/// When combined with <see cref="UnPremultiply"/>, associated alpha takes precedence.
/// </summary>
Premultiply = 1 << 1,
/// <summary>
/// Enable SRGB companding (defined in <see cref="SRgbCompanding"/>).
/// Expands sRGB color components when converting pixels to vectors and compresses linear RGB color components when converting vectors to pixels.
/// </summary>
SRgbCompand = 1 << 2,
/// <summary>
/// Requests unassociated alpha for the vector representation unless <see cref="Premultiply"/> is also specified.
/// </summary>
UnPremultiply = 1 << 3,
}

32
src/ImageSharp/PixelFormats/PixelImplementations/A8.cs

@ -77,6 +77,38 @@ public partial struct A8 : IPixel<A8>, IPackedVector<byte>
/// <inheritdoc />
public static PixelOperations<A8> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromAssociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromAssociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromScaledVector4(Vector4 source) => FromVector4(source);

41
src/ImageSharp/PixelFormats/PixelImplementations/Abgr32.cs

@ -194,6 +194,47 @@ public partial struct Abgr32 : IPixel<Abgr32>, IPackedVector<uint>
/// <inheritdoc />
public static PixelOperations<Abgr32> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToAssociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32 FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32 FromScaledVector4(Vector4 source) => FromVector4(source);

56
src/ImageSharp/PixelFormats/PixelImplementations/Abgr32P.cs

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Components are stored in alpha, blue, green, and red order from least to most significant byte.
/// </summary>
/// <remarks>
/// Component, packed, and vector values use associated alpha representation.
/// The native component, packed, and vector representations use associated alpha.
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
public partial struct Abgr32P : IPixel<Abgr32P>, IPackedVector<uint>
@ -147,9 +147,29 @@ public partial struct Abgr32P : IPixel<Abgr32P>, IPackedVector<uint>
/// <inheritdoc />
public readonly Vector4 ToScaledVector4() => new Vector4(this.R, this.G, this.B, this.A) / byte.MaxValue;
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4()
{
// Divide the stored byte magnitudes before normalization so unassociation cannot move an exact byte conversion across its rounding midpoint.
return Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(this.R, this.G, this.B, this.A);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
public readonly Vector4 ToVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToUnassociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
public static PixelTypeInfo GetPixelTypeInfo()
=> PixelTypeInfo.Create<Abgr32P>(
@ -162,10 +182,31 @@ public partial struct Abgr32P : IPixel<Abgr32P>, IPackedVector<uint>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32P FromScaledVector4(Vector4 source) => Pack(source);
public static Abgr32P FromScaledVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc />
public static Abgr32P FromVector4(Vector4 source) => FromScaledVector4(source);
public static Abgr32P FromVector4(Vector4 source) => FromAssociatedVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32P FromUnassociatedVector4(Vector4 source) => FromUnassociatedScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32P FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32P FromUnassociatedScaledVector4(Vector4 source)
=> Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4ToAbgr32P(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32P FromAssociatedScaledVector4(Vector4 source)
{
// Rescale associated RGB when alpha rounds to a different byte so the stored channels remain associated with the alpha actually written.
return Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4ToAbgr32P(source);
}
/// <inheritdoc />
public static Abgr32P FromAbgr32(Abgr32 source) => FromUnassociatedScaledVector4(source.ToScaledVector4());
@ -218,14 +259,6 @@ public partial struct Abgr32P : IPixel<Abgr32P>, IPackedVector<uint>
/// <inheritdoc />
public override readonly string ToString() => $"Abgr32P({this.R}, {this.G}, {this.B}, {this.A})";
/// <summary>
/// Converts an unassociated scaled vector to associated representation.
/// </summary>
/// <param name="source">The unassociated scaled vector.</param>
/// <returns>The associated pixel.</returns>
private static Abgr32P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Vector4Converters.AssociatedRgbaCompatible.Associate(source));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Abgr32P Pack(Vector4 vector)
{
@ -233,6 +266,7 @@ public partial struct Abgr32P : IPixel<Abgr32P>, IPackedVector<uint>
vector += Half;
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
// Each converted component occupies one 32-bit lane. Reinterpreting those lanes as bytes exposes their low bytes at offsets 0, 4, 8, and 12.
Vector128<byte> result = Vector128.ConvertToInt32(vector.AsVector128()).AsByte();
return new Abgr32P(result.GetElement(0), result.GetElement(4), result.GetElement(8), result.GetElement(12));
}

41
src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs

@ -187,6 +187,47 @@ public partial struct Argb32 : IPixel<Argb32>, IPackedVector<uint>
/// <inheritdoc />
public static PixelOperations<Argb32> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToAssociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromScaledVector4(Vector4 source) => FromVector4(source);

56
src/ImageSharp/PixelFormats/PixelImplementations/Argb32P.cs

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Components are stored in alpha, red, green, and blue order from least to most significant byte.
/// </summary>
/// <remarks>
/// Component, packed, and vector values use associated alpha representation.
/// The native component, packed, and vector representations use associated alpha.
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
public partial struct Argb32P : IPixel<Argb32P>, IPackedVector<uint>
@ -147,9 +147,29 @@ public partial struct Argb32P : IPixel<Argb32P>, IPackedVector<uint>
/// <inheritdoc />
public readonly Vector4 ToScaledVector4() => new Vector4(this.R, this.G, this.B, this.A) / byte.MaxValue;
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4()
{
// Divide the stored byte magnitudes before normalization so unassociation cannot move an exact byte conversion across its rounding midpoint.
return Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(this.R, this.G, this.B, this.A);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
public readonly Vector4 ToVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToUnassociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
public static PixelTypeInfo GetPixelTypeInfo()
=> PixelTypeInfo.Create<Argb32P>(
@ -162,10 +182,31 @@ public partial struct Argb32P : IPixel<Argb32P>, IPackedVector<uint>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32P FromScaledVector4(Vector4 source) => Pack(source);
public static Argb32P FromScaledVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc />
public static Argb32P FromVector4(Vector4 source) => FromScaledVector4(source);
public static Argb32P FromVector4(Vector4 source) => FromAssociatedVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32P FromUnassociatedVector4(Vector4 source) => FromUnassociatedScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32P FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32P FromUnassociatedScaledVector4(Vector4 source)
=> Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4ToArgb32P(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32P FromAssociatedScaledVector4(Vector4 source)
{
// Rescale associated RGB when alpha rounds to a different byte so the stored channels remain associated with the alpha actually written.
return Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4ToArgb32P(source);
}
/// <inheritdoc />
public static Argb32P FromAbgr32(Abgr32 source) => FromUnassociatedScaledVector4(source.ToScaledVector4());
@ -218,14 +259,6 @@ public partial struct Argb32P : IPixel<Argb32P>, IPackedVector<uint>
/// <inheritdoc />
public override readonly string ToString() => $"Argb32P({this.R}, {this.G}, {this.B}, {this.A})";
/// <summary>
/// Converts an unassociated scaled vector to associated representation.
/// </summary>
/// <param name="source">The unassociated scaled vector.</param>
/// <returns>The associated pixel.</returns>
private static Argb32P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Vector4Converters.AssociatedRgbaCompatible.Associate(source));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Argb32P Pack(Vector4 vector)
{
@ -233,6 +266,7 @@ public partial struct Argb32P : IPixel<Argb32P>, IPackedVector<uint>
vector += Half;
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
// Each converted component occupies one 32-bit lane. Reinterpreting those lanes as bytes exposes their low bytes at offsets 0, 4, 8, and 12.
Vector128<byte> result = Vector128.ConvertToInt32(vector.AsVector128()).AsByte();
return new Argb32P(result.GetElement(0), result.GetElement(4), result.GetElement(8), result.GetElement(12));
}

42
src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs

@ -97,6 +97,48 @@ public partial struct Bgr24 : IPixel<Bgr24>
/// <inheritdoc/>
public static PixelOperations<Bgr24> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromAssociatedVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromScaledVector4(Vector4 source) => FromVector4(source);

42
src/ImageSharp/PixelFormats/PixelImplementations/Bgr565.cs

@ -75,6 +75,48 @@ public partial struct Bgr565(Vector3 vector) : IPixel<Bgr565>, IPackedVector<ush
/// <inheritdoc />
public static PixelOperations<Bgr565> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromAssociatedVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromVector4(source);
}
/// <inheritdoc />
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());

41
src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs

@ -136,6 +136,47 @@ public partial struct Bgra32 : IPixel<Bgra32>, IPackedVector<uint>
/// <inheritdoc/>
public static PixelOperations<Bgra32> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToAssociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32 FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32 FromScaledVector4(Vector4 source) => FromVector4(source);

56
src/ImageSharp/PixelFormats/PixelImplementations/Bgra32P.cs

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Components are stored in blue, green, red, and alpha order from least to most significant byte.
/// </summary>
/// <remarks>
/// Component, packed, and vector values use associated alpha representation.
/// The native component, packed, and vector representations use associated alpha.
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
public partial struct Bgra32P : IPixel<Bgra32P>, IPackedVector<uint>
@ -147,9 +147,29 @@ public partial struct Bgra32P : IPixel<Bgra32P>, IPackedVector<uint>
/// <inheritdoc />
public readonly Vector4 ToScaledVector4() => new Vector4(this.R, this.G, this.B, this.A) / byte.MaxValue;
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4()
{
// Divide the stored byte magnitudes before normalization so unassociation cannot move an exact byte conversion across its rounding midpoint.
return Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(this.R, this.G, this.B, this.A);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
public readonly Vector4 ToVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToUnassociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
public static PixelTypeInfo GetPixelTypeInfo()
=> PixelTypeInfo.Create<Bgra32P>(
@ -162,10 +182,31 @@ public partial struct Bgra32P : IPixel<Bgra32P>, IPackedVector<uint>
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32P FromScaledVector4(Vector4 source) => Pack(source);
public static Bgra32P FromScaledVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc />
public static Bgra32P FromVector4(Vector4 source) => FromScaledVector4(source);
public static Bgra32P FromVector4(Vector4 source) => FromAssociatedVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32P FromUnassociatedVector4(Vector4 source) => FromUnassociatedScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32P FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32P FromUnassociatedScaledVector4(Vector4 source)
=> Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4ToBgra32P(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32P FromAssociatedScaledVector4(Vector4 source)
{
// Rescale associated RGB when alpha rounds to a different byte so the stored channels remain associated with the alpha actually written.
return Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4ToBgra32P(source);
}
/// <inheritdoc />
public static Bgra32P FromAbgr32(Abgr32 source) => FromUnassociatedScaledVector4(source.ToScaledVector4());
@ -218,14 +259,6 @@ public partial struct Bgra32P : IPixel<Bgra32P>, IPackedVector<uint>
/// <inheritdoc />
public override readonly string ToString() => $"Bgra32P({this.R}, {this.G}, {this.B}, {this.A})";
/// <summary>
/// Converts an unassociated scaled vector to associated representation.
/// </summary>
/// <param name="source">The unassociated scaled vector.</param>
/// <returns>The associated pixel.</returns>
private static Bgra32P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Vector4Converters.AssociatedRgbaCompatible.Associate(source));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Bgra32P Pack(Vector4 vector)
{
@ -233,6 +266,7 @@ public partial struct Bgra32P : IPixel<Bgra32P>, IPackedVector<uint>
vector += Half;
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
// Each converted component occupies one 32-bit lane. Reinterpreting those lanes as bytes exposes their low bytes at offsets 0, 4, 8, and 12.
Vector128<byte> result = Vector128.ConvertToInt32(vector.AsVector128()).AsByte();
return new Bgra32P(result.GetElement(0), result.GetElement(4), result.GetElement(8), result.GetElement(12));
}

41
src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs

@ -88,6 +88,47 @@ public partial struct Bgra4444 : IPixel<Bgra4444>, IPackedVector<ushort>
/// <inheritdoc />
public static PixelOperations<Bgra4444> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToAssociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromScaledVector4(Vector4 source) => FromVector4(source);

41
src/ImageSharp/PixelFormats/PixelImplementations/Bgra5551.cs

@ -86,6 +86,47 @@ public partial struct Bgra5551 : IPixel<Bgra5551>, IPackedVector<ushort>
/// <inheritdoc />
public static PixelOperations<Bgra5551> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToAssociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromScaledVector4(Vector4 source) => FromVector4(source);

52
src/ImageSharp/PixelFormats/PixelImplementations/Byte4.cs

@ -88,6 +88,58 @@ public partial struct Byte4 : IPixel<Byte4>, IPackedVector<uint>
/// <inheritdoc />
public static PixelOperations<Byte4> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4()
{
Vector4 vector = this.ToAssociatedScaledVector4();
// Native components use [0, 255], so association occurs in scaled [0, 1] space before mapping the result back.
return vector * MaxBytes;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromAssociatedVector4(Vector4 source)
{
// Map every native component, including alpha, to scaled [0, 1] space before unassociating.
source /= MaxBytes;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromScaledVector4(Vector4 source) => FromVector4(source * 255f);

42
src/ImageSharp/PixelFormats/PixelImplementations/HalfSingle.cs

@ -72,6 +72,48 @@ public partial struct HalfSingle : IPixel<HalfSingle>, IPackedVector<ushort>
/// <inheritdoc />
public static PixelOperations<HalfSingle> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromAssociatedVector4(Vector4 source)
{
// Only the stored color component uses the native [-1, 1] encoding; W remains the normalized source alpha.
source.X = (source.X + 1F) / 2F;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromScaledVector4(Vector4 source)

43
src/ImageSharp/PixelFormats/PixelImplementations/HalfVector2.cs

@ -84,6 +84,49 @@ public partial struct HalfVector2 : IPixel<HalfVector2>, IPackedVector<uint>
/// <inheritdoc />
public static PixelOperations<HalfVector2> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromAssociatedVector4(Vector4 source)
{
// Only the stored color components use the native [-1, 1] encoding; W remains the normalized source alpha.
source.X = (source.X + 1F) / 2F;
source.Y = (source.Y + 1F) / 2F;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromScaledVector4(Vector4 source)

55
src/ImageSharp/PixelFormats/PixelImplementations/HalfVector4.cs

@ -89,6 +89,61 @@ public partial struct HalfVector4 : IPixel<HalfVector4>, IPackedVector<ulong>
/// <inheritdoc />
public static PixelOperations<HalfVector4> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4()
{
Vector4 vector = this.ToAssociatedScaledVector4();
// Native components use an affine [-1, 1] encoding, so direct multiplication would use the wrong zero point.
vector *= 2F;
vector -= Vector4.One;
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromAssociatedVector4(Vector4 source)
{
// Map the affine native encoding to logical [0, 1] space before unassociating.
source += Vector4.One;
source /= 2F;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromScaledVector4(Vector4 source)

126
src/ImageSharp/PixelFormats/PixelImplementations/HalfVector4P.cs

@ -3,7 +3,6 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.PixelFormats;
@ -11,7 +10,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Packed pixel type containing four associated 16-bit floating-point values.
/// </summary>
/// <remarks>
/// Packed and vector values use associated alpha representation.
/// The native packed and vector representations use associated alpha.
/// </remarks>
public partial struct HalfVector4P : IPixel<HalfVector4P>, IPackedVector<ulong>
{
@ -55,9 +54,7 @@ public partial struct HalfVector4P : IPixel<HalfVector4P>, IPackedVector<ulong>
/// <inheritdoc />
public readonly Rgba32 ToRgba32()
{
Vector4 vector = this.ToScaledVector4();
Numerics.UnPremultiply(ref vector);
return Rgba32.FromScaledVector4(vector);
return Rgba32.FromScaledVector4(this.ToUnassociatedScaledVector4());
}
/// <inheritdoc />
@ -69,6 +66,19 @@ public partial struct HalfVector4P : IPixel<HalfVector4P>, IPackedVector<ulong>
return scaled;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.UnPremultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
public readonly Vector4 ToVector4() => new(
HalfTypeHelper.Unpack((ushort)this.PackedValue),
@ -76,6 +86,21 @@ public partial struct HalfVector4P : IPixel<HalfVector4P>, IPackedVector<ulong>
HalfTypeHelper.Unpack((ushort)(this.PackedValue >> 0x20)),
HalfTypeHelper.Unpack((ushort)(this.PackedValue >> 0x30)));
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4()
{
// Association is defined in the common scaled domain. Binary16-native W is an affine encoding of alpha, so it cannot be used as a divisor directly.
Vector4 vector = this.ToUnassociatedScaledVector4();
vector *= 2F;
vector -= Vector4.One;
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
public static PixelTypeInfo GetPixelTypeInfo()
=> PixelTypeInfo.Create<HalfVector4P>(
@ -87,15 +112,52 @@ public partial struct HalfVector4P : IPixel<HalfVector4P>, IPackedVector<ulong>
public static PixelOperations<HalfVector4P> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
public static HalfVector4P FromScaledVector4(Vector4 source)
public static HalfVector4P FromScaledVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc />
public static HalfVector4P FromVector4(Vector4 source) => FromAssociatedVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4P FromUnassociatedVector4(Vector4 source)
{
// Convert the binary16-native range to the common scaled domain before associating because native W is not opacity.
source += Vector4.One;
source /= 2F;
return FromUnassociatedScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4P FromAssociatedVector4(Vector4 source)
{
source *= 2F;
source -= Vector4.One;
return FromVector4(source);
// Reassociation must also operate in scaled space so RGB follows the quantized scaled alpha rather than the binary16-native W component.
source += Vector4.One;
source /= 2F;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc />
public static HalfVector4P FromVector4(Vector4 source) => new() { PackedValue = Pack(source) };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4P FromUnassociatedScaledVector4(Vector4 source) => PackAssociatedScaledVector4(Associate(source));
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4P FromAssociatedScaledVector4(Vector4 source) => PackAssociatedScaledVector4(Reassociate(source));
/// <summary>
/// Packs an associated scaled vector into binary16-native storage.
/// </summary>
/// <param name="source">The associated scaled vector.</param>
/// <returns>The packed pixel.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static HalfVector4P PackAssociatedScaledVector4(Vector4 source)
{
// Binary16 storage uses the native [-1, 1] range even though association is defined in scaled opacity space.
source *= 2F;
source -= Vector4.One;
return new() { PackedValue = Pack(source) };
}
/// <inheritdoc />
public static HalfVector4P FromAbgr32(Abgr32 source) => FromUnassociatedScaledVector4(source.ToScaledVector4());
@ -152,14 +214,6 @@ public partial struct HalfVector4P : IPixel<HalfVector4P>, IPackedVector<ulong>
return FormattableString.Invariant($"HalfVector4P({vector.X:#0.##}, {vector.Y:#0.##}, {vector.Z:#0.##}, {vector.W:#0.##})");
}
/// <summary>
/// Converts an unassociated scaled vector to associated representation.
/// </summary>
/// <param name="source">The unassociated scaled vector.</param>
/// <returns>The associated pixel.</returns>
private static HalfVector4P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Associate(source));
/// <summary>
/// Converts an unassociated scaled vector to the associated representation of a half-precision destination.
/// </summary>
@ -168,6 +222,8 @@ public partial struct HalfVector4P : IPixel<HalfVector4P>, IPackedVector<ulong>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector4 Associate(Vector4 source)
{
source = Numerics.Clamp(source, Vector4.Zero, Vector4.One);
// Quantize alpha through the destination's native half representation before RGB is associated with it.
float nativeAlpha = (source.W * 2F) - 1F;
source.W = (HalfTypeHelper.Unpack(HalfTypeHelper.Pack(nativeAlpha)) + 1F) / 2F;
@ -175,20 +231,6 @@ public partial struct HalfVector4P : IPixel<HalfVector4P>, IPackedVector<ulong>
return source;
}
/// <summary>
/// Converts unassociated scaled vectors to the associated representation of a half-precision destination.
/// </summary>
/// <param name="source">The vectors to convert in place.</param>
private static void Associate(Span<Vector4> source)
{
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(source);
for (nuint i = 0; i < (uint)source.Length; i++)
{
Unsafe.Add(ref sourceBase, i) = Associate(Unsafe.Add(ref sourceBase, i));
}
}
/// <summary>
/// Reassociates a scaled vector with the alpha value the destination stores.
/// </summary>
@ -199,34 +241,22 @@ public partial struct HalfVector4P : IPixel<HalfVector4P>, IPackedVector<ulong>
{
float alpha = source.W;
if (alpha == 0)
if (alpha <= 0)
{
return Vector4.Zero;
}
float nativeAlpha = (alpha * 2F) - 1F;
// The binary16-native alpha range is [-1, 1]. Clamp before packing so out-of-range scaled alpha cannot escape the pixel's declared [0, 1] opacity range.
float nativeAlpha = Numerics.Clamp((alpha * 2F) - 1F, -1F, 1F);
float storedAlpha = (HalfTypeHelper.Unpack(HalfTypeHelper.Pack(nativeAlpha)) + 1F) / 2F;
// Associated RGB scales by the same ratio as alpha. Applying that ratio directly avoids the extra division and multiplication of an unpremultiply/premultiply round trip and preserves exact midpoints when alpha needs no quantization.
source *= storedAlpha / alpha;
source.W = storedAlpha;
Numerics.ClampRgbToAlpha(ref source);
return source;
}
/// <summary>
/// Reassociates scaled vectors with the alpha values the destination stores.
/// </summary>
/// <param name="source">The vectors to convert in place.</param>
private static void Reassociate(Span<Vector4> source)
{
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(source);
for (nuint i = 0; i < (uint)source.Length; i++)
{
Unsafe.Add(ref sourceBase, i) = Reassociate(Unsafe.Add(ref sourceBase, i));
}
}
/// <summary>
/// Packs native half-precision components into a 64-bit value.
/// </summary>

42
src/ImageSharp/PixelFormats/PixelImplementations/L16.cs

@ -77,6 +77,48 @@ public partial struct L16 : IPixel<L16>, IPackedVector<ushort>
/// <inheritdoc />
public static PixelOperations<L16> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromAssociatedVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromScaledVector4(Vector4 source) => FromVector4(source);

42
src/ImageSharp/PixelFormats/PixelImplementations/L8.cs

@ -79,6 +79,48 @@ public partial struct L8 : IPixel<L8>, IPackedVector<byte>
/// <inheritdoc />
public static PixelOperations<L8> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromAssociatedVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromScaledVector4(Vector4 source) => FromVector4(source);

41
src/ImageSharp/PixelFormats/PixelImplementations/La16.cs

@ -106,6 +106,47 @@ public partial struct La16 : IPixel<La16>, IPackedVector<ushort>
/// <inheritdoc/>
public static PixelOperations<La16> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToAssociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromScaledVector4(Vector4 source) => Pack(source);

41
src/ImageSharp/PixelFormats/PixelImplementations/La32.cs

@ -103,6 +103,47 @@ public partial struct La32 : IPixel<La32>, IPackedVector<uint>
/// <inheritdoc/>
public static PixelOperations<La32> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToAssociatedScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromAssociatedVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromScaledVector4(Vector4 source) => Pack(source);

43
src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte2.cs

@ -87,6 +87,49 @@ public partial struct NormalizedByte2 : IPixel<NormalizedByte2>, IPackedVector<u
/// <inheritdoc />
public static PixelOperations<NormalizedByte2> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromAssociatedVector4(Vector4 source)
{
// Only the stored color components use the native [-1, 1] encoding; W remains the normalized source alpha.
source.X = (source.X + 1F) / 2F;
source.Y = (source.Y + 1F) / 2F;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromScaledVector4(Vector4 source)

68
src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte4.cs

@ -16,6 +16,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
public partial struct NormalizedByte4 : IPixel<NormalizedByte4>, IPackedVector<uint>
{
private const float MaxPos = 127f;
private const float ScaledMagnitude = MaxPos * 2F;
private static readonly Vector4 Half = Vector128.Create(MaxPos).AsVector4();
private static readonly Vector4 MinusOne = Vector128.Create(-1f).AsVector4();
@ -70,10 +71,14 @@ public partial struct NormalizedByte4 : IPixel<NormalizedByte4>, IPackedVector<u
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4()
{
Vector4 scaled = this.ToVector4();
scaled += Vector4.One;
scaled /= 2f;
return scaled;
// Offset the exact signed components before division. Mapping an already normalized value through (value + 1) / 2 loses precision near -1 through cancellation.
Vector4 scaled = new(
(sbyte)((this.PackedValue >> 0) & 0xFF) + MaxPos,
(sbyte)((this.PackedValue >> 8) & 0xFF) + MaxPos,
(sbyte)((this.PackedValue >> 16) & 0xFF) + MaxPos,
(sbyte)((this.PackedValue >> 24) & 0xFF) + MaxPos);
return scaled / ScaledMagnitude;
}
/// <inheritdoc />
@ -94,6 +99,61 @@ public partial struct NormalizedByte4 : IPixel<NormalizedByte4>, IPackedVector<u
/// <inheritdoc />
public static PixelOperations<NormalizedByte4> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4()
{
Vector4 vector = this.ToAssociatedScaledVector4();
// Native components use an affine [-1, 1] encoding, so direct multiplication would use the wrong zero point.
vector *= 2F;
vector -= Vector4.One;
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromAssociatedVector4(Vector4 source)
{
// Map the affine native encoding to logical [0, 1] space before unassociating.
source += Vector4.One;
source /= 2F;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromScaledVector4(Vector4 source)

125
src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte4P.cs

@ -3,7 +3,6 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
namespace SixLabors.ImageSharp.PixelFormats;
@ -12,7 +11,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Packed pixel type containing four associated 8-bit signed normalized values ranging from -1 to 1.
/// </summary>
/// <remarks>
/// Packed and vector values use associated alpha representation.
/// The native packed and vector representations use associated alpha.
/// </remarks>
public partial struct NormalizedByte4P : IPixel<NormalizedByte4P>, IPackedVector<uint>
{
@ -64,12 +63,24 @@ public partial struct NormalizedByte4P : IPixel<NormalizedByte4P>, IPackedVector
/// <inheritdoc />
public readonly Vector4 ToScaledVector4()
{
Vector4 scaled = this.ToVector4();
scaled += Vector4.One;
scaled /= 2F;
return scaled;
// Offset the exact signed components before division. Mapping an already normalized value through (value + 1) / 2 loses precision near -1 through cancellation.
Vector4 scaled = new(
(sbyte)((this.PackedValue >> 0) & 0xFF) + MaxPos,
(sbyte)((this.PackedValue >> 8) & 0xFF) + MaxPos,
(sbyte)((this.PackedValue >> 16) & 0xFF) + MaxPos,
(sbyte)((this.PackedValue >> 24) & 0xFF) + MaxPos);
return scaled / ScaledMagnitude;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => ToUnassociatedScaledVector4(this);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
public readonly Vector4 ToVector4() => new(
(sbyte)((this.PackedValue >> 0) & 0xFF) / MaxPos,
@ -77,6 +88,21 @@ public partial struct NormalizedByte4P : IPixel<NormalizedByte4P>, IPackedVector
(sbyte)((this.PackedValue >> 16) & 0xFF) / MaxPos,
(sbyte)((this.PackedValue >> 24) & 0xFF) / MaxPos);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4()
{
// Association is defined in the common scaled domain. Signed-native W is an affine encoding of alpha, so it cannot be used as a divisor directly.
Vector4 vector = this.ToUnassociatedScaledVector4();
vector *= 2F;
vector -= Vector4.One;
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
public static PixelTypeInfo GetPixelTypeInfo()
=> PixelTypeInfo.Create<NormalizedByte4P>(
@ -88,15 +114,52 @@ public partial struct NormalizedByte4P : IPixel<NormalizedByte4P>, IPackedVector
public static PixelOperations<NormalizedByte4P> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
public static NormalizedByte4P FromScaledVector4(Vector4 source)
public static NormalizedByte4P FromScaledVector4(Vector4 source) => FromAssociatedScaledVector4(source);
/// <inheritdoc />
public static NormalizedByte4P FromVector4(Vector4 source) => FromAssociatedVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4P FromUnassociatedVector4(Vector4 source)
{
source *= 2F;
source -= Vector4.One;
return FromVector4(source);
// Convert the signed-native range to the common scaled domain before associating because native W is not opacity.
source += Vector4.One;
source /= 2F;
return FromUnassociatedScaledVector4(source);
}
/// <inheritdoc />
public static NormalizedByte4P FromVector4(Vector4 source) => new() { PackedValue = Pack(source) };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4P FromAssociatedVector4(Vector4 source)
{
// Reassociation must also operate in scaled space so RGB follows the quantized scaled alpha rather than the signed-native W component.
source += Vector4.One;
source /= 2F;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4P FromUnassociatedScaledVector4(Vector4 source) => PackAssociatedScaledVector4(Associate(source));
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4P FromAssociatedScaledVector4(Vector4 source) => PackAssociatedScaledVector4(Reassociate(source));
/// <summary>
/// Packs an associated scaled vector into signed-normalized storage.
/// </summary>
/// <param name="source">The associated scaled vector.</param>
/// <returns>The packed pixel.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static NormalizedByte4P PackAssociatedScaledVector4(Vector4 source)
{
// Signed-normalized storage uses the native [-1, 1] range even though association is defined in scaled opacity space.
source *= 2F;
source -= Vector4.One;
return new() { PackedValue = Pack(source) };
}
/// <inheritdoc />
public static NormalizedByte4P FromAbgr32(Abgr32 source) => FromUnassociatedScaledVector4(source.ToScaledVector4());
@ -153,13 +216,6 @@ public partial struct NormalizedByte4P : IPixel<NormalizedByte4P>, IPackedVector
return FormattableString.Invariant($"NormalizedByte4P({vector.X:#0.##}, {vector.Y:#0.##}, {vector.Z:#0.##}, {vector.W:#0.##})");
}
/// <summary>
/// Converts an unassociated scaled vector to associated representation.
/// </summary>
/// <param name="source">The unassociated scaled vector.</param>
/// <returns>The associated pixel.</returns>
private static NormalizedByte4P FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(Associate(source));
/// <summary>
/// Converts an unassociated scaled vector to the associated representation of a signed-normalized-byte destination.
/// </summary>
@ -168,6 +224,8 @@ public partial struct NormalizedByte4P : IPixel<NormalizedByte4P>, IPackedVector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector4 Associate(Vector4 source)
{
source = Numerics.Clamp(source, Vector4.Zero, Vector4.One);
// Reproduce the signed-normalized packer's alpha quantization, then associate RGB with the exact alpha that will be stored.
float nativeAlpha = Numerics.Clamp((source.W * 2F) - 1F, -1F, 1F);
float storedAlpha = MathF.Round(nativeAlpha * MaxPos);
@ -202,20 +260,6 @@ public partial struct NormalizedByte4P : IPixel<NormalizedByte4P>, IPackedVector
return vector;
}
/// <summary>
/// Converts unassociated scaled vectors to the associated representation of a signed-normalized-byte destination.
/// </summary>
/// <param name="source">The vectors to convert in place.</param>
private static void Associate(Span<Vector4> source)
{
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(source);
for (nuint i = 0; i < (uint)source.Length; i++)
{
Unsafe.Add(ref sourceBase, i) = Associate(Unsafe.Add(ref sourceBase, i));
}
}
/// <summary>
/// Reassociates a scaled vector with the alpha value the destination stores.
/// </summary>
@ -226,7 +270,7 @@ public partial struct NormalizedByte4P : IPixel<NormalizedByte4P>, IPackedVector
{
float alpha = source.W;
if (alpha == 0)
if (alpha <= 0)
{
return Vector4.Zero;
}
@ -237,23 +281,10 @@ public partial struct NormalizedByte4P : IPixel<NormalizedByte4P>, IPackedVector
// Associated RGB scales by the same ratio as alpha. Applying that ratio directly avoids the extra division and multiplication of an unpremultiply/premultiply round trip and preserves exact midpoints when alpha needs no quantization.
source *= storedAlpha / alpha;
source.W = storedAlpha;
Numerics.ClampRgbToAlpha(ref source);
return source;
}
/// <summary>
/// Reassociates scaled vectors with the alpha values the destination stores.
/// </summary>
/// <param name="source">The vectors to convert in place.</param>
private static void Reassociate(Span<Vector4> source)
{
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(source);
for (nuint i = 0; i < (uint)source.Length; i++)
{
Unsafe.Add(ref sourceBase, i) = Reassociate(Unsafe.Add(ref sourceBase, i));
}
}
/// <summary>
/// Packs native signed normalized components into a 32-bit value.
/// </summary>

43
src/ImageSharp/PixelFormats/PixelImplementations/NormalizedShort2.cs

@ -89,6 +89,49 @@ public partial struct NormalizedShort2 : IPixel<NormalizedShort2>, IPackedVector
/// <inheritdoc />
public static PixelOperations<NormalizedShort2> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromAssociatedVector4(Vector4 source)
{
// Only the stored color components use the native [-1, 1] encoding; W remains the normalized source alpha.
source.X = (source.X + 1F) / 2F;
source.Y = (source.Y + 1F) / 2F;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromScaledVector4(Vector4 source)

68
src/ImageSharp/PixelFormats/PixelImplementations/NormalizedShort4.cs

@ -17,6 +17,7 @@ public partial struct NormalizedShort4 : IPixel<NormalizedShort4>, IPackedVector
{
// Largest two byte positive number 0xFFFF >> 1;
private const float MaxPos = 0x7FFF;
private const float ScaledMagnitude = MaxPos * 2F;
private static readonly Vector4 Max = Vector128.Create(MaxPos).AsVector4();
private static readonly Vector4 Min = Vector4.Negate(Max);
@ -71,10 +72,14 @@ public partial struct NormalizedShort4 : IPixel<NormalizedShort4>, IPackedVector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4()
{
Vector4 scaled = this.ToVector4();
scaled += Vector4.One;
scaled /= 2f;
return scaled;
// Offset the exact signed components before division. Mapping an already normalized value through (value + 1) / 2 loses precision near -1 through cancellation.
Vector4 scaled = new(
(short)((this.PackedValue >> 0x00) & 0xFFFF) + MaxPos,
(short)((this.PackedValue >> 0x10) & 0xFFFF) + MaxPos,
(short)((this.PackedValue >> 0x20) & 0xFFFF) + MaxPos,
(short)((this.PackedValue >> 0x30) & 0xFFFF) + MaxPos);
return scaled / ScaledMagnitude;
}
/// <inheritdoc />
@ -95,6 +100,61 @@ public partial struct NormalizedShort4 : IPixel<NormalizedShort4>, IPackedVector
/// <inheritdoc />
public static PixelOperations<NormalizedShort4> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4()
{
Vector4 vector = this.ToScaledVector4();
Numerics.Premultiply(ref vector);
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4()
{
Vector4 vector = this.ToAssociatedScaledVector4();
// Native components use an affine [-1, 1] encoding, so direct multiplication would use the wrong zero point.
vector *= 2F;
vector -= Vector4.One;
return vector;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromAssociatedScaledVector4(Vector4 source)
{
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromAssociatedVector4(Vector4 source)
{
// Map the affine native encoding to logical [0, 1] space before unassociating.
source += Vector4.One;
source /= 2F;
return FromAssociatedScaledVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromScaledVector4(Vector4 source)

19
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/A8.PixelOperations.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +13,20 @@ public partial struct A8
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<A8>;
internal class PixelOperations : PixelOperations<A8>
{
// A8 stores alpha without color components, so association cannot alter any emitted or consumed vector component.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<A8> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<A8> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<A8> destination) => this.FromUnassociatedVector4Destructive(configuration, source, destination);
/// <inheritdoc />
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<A8> destination) => this.FromUnassociatedScaledVector4Destructive(configuration, source, destination);
}
}

54
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Abgr32P.PixelOperations.cs

@ -17,50 +17,48 @@ public partial struct Abgr32P
internal class PixelOperations : AssociatedAlphaPixelOperations<Abgr32P>
{
/// <inheritdoc />
internal override Vector4 ToUnassociatedScaledVector4(Abgr32P source)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source.R, source.G, source.B, source.A);
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<Abgr32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source, destination);
/// <inheritdoc />
internal override Abgr32P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Vector4Converters.AssociatedRgbaCompatible.Associate(source));
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Abgr32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToAssociatedVector4(source, destination);
/// <inheritdoc />
public override Abgr32P FromAssociatedScaledVector4(Vector4 source)
=> Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4ToAbgr32P(source);
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Abgr32P> destination)
{
// Native and scaled vectors have the same range for this format, so the byte-specialized converter is valid for both contracts.
Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4(source, destination);
}
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Abgr32P> destination)
{
// The converter rescales RGB when alpha rounds so the channels remain associated with the byte alpha actually stored.
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
}
/// <inheritdoc />
internal override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Abgr32P> source, Span<Vector4> destination)
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Abgr32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source, destination);
/// <inheritdoc />
internal override void FromUnassociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<Abgr32P> destination)
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Abgr32P> source, Span<Vector4> destination)
{
source = source[..destination.Length];
Vector4Converters.AssociatedRgbaCompatible.Associate(source);
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
// This byte format has the same normalized native and scaled ranges, so the unmodified converter satisfies both contracts.
Vector4Converters.AssociatedRgbaCompatible.ToAssociatedVector4(source, destination);
}
/// <inheritdoc />
public override void FromAssociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<Abgr32P> destination)
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Abgr32P> destination)
{
source = source[..destination.Length];
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4(source, destination);
}
/// <inheritdoc />
public override void ToVector4(
Configuration configuration,
ReadOnlySpan<Abgr32P> source,
Span<Vector4> destinationVectors,
PixelConversionModifiers modifiers)
=> Vector4Converters.AssociatedRgbaCompatible.ToVector4(source, destinationVectors, modifiers);
/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Span<Vector4> sourceVectors,
Span<Abgr32P> destination,
PixelConversionModifiers modifiers)
=> Vector4Converters.AssociatedRgbaCompatible.FromVector4(sourceVectors, destination, modifiers);
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Abgr32P> destination)
{
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
}
}
}

54
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Argb32P.PixelOperations.cs

@ -17,50 +17,48 @@ public partial struct Argb32P
internal class PixelOperations : AssociatedAlphaPixelOperations<Argb32P>
{
/// <inheritdoc />
internal override Vector4 ToUnassociatedScaledVector4(Argb32P source)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source.R, source.G, source.B, source.A);
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<Argb32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source, destination);
/// <inheritdoc />
internal override Argb32P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Vector4Converters.AssociatedRgbaCompatible.Associate(source));
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Argb32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToAssociatedVector4(source, destination);
/// <inheritdoc />
public override Argb32P FromAssociatedScaledVector4(Vector4 source)
=> Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4ToArgb32P(source);
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Argb32P> destination)
{
// Native and scaled vectors have the same range for this format, so the byte-specialized converter is valid for both contracts.
Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4(source, destination);
}
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Argb32P> destination)
{
// The converter rescales RGB when alpha rounds so the channels remain associated with the byte alpha actually stored.
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
}
/// <inheritdoc />
internal override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Argb32P> source, Span<Vector4> destination)
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Argb32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source, destination);
/// <inheritdoc />
internal override void FromUnassociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<Argb32P> destination)
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Argb32P> source, Span<Vector4> destination)
{
source = source[..destination.Length];
Vector4Converters.AssociatedRgbaCompatible.Associate(source);
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
// This byte format has the same normalized native and scaled ranges, so the unmodified converter satisfies both contracts.
Vector4Converters.AssociatedRgbaCompatible.ToAssociatedVector4(source, destination);
}
/// <inheritdoc />
public override void FromAssociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<Argb32P> destination)
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Argb32P> destination)
{
source = source[..destination.Length];
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4(source, destination);
}
/// <inheritdoc />
public override void ToVector4(
Configuration configuration,
ReadOnlySpan<Argb32P> source,
Span<Vector4> destinationVectors,
PixelConversionModifiers modifiers)
=> Vector4Converters.AssociatedRgbaCompatible.ToVector4(source, destinationVectors, modifiers);
/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Span<Vector4> sourceVectors,
Span<Argb32P> destination,
PixelConversionModifiers modifiers)
=> Vector4Converters.AssociatedRgbaCompatible.FromVector4(sourceVectors, destination, modifiers);
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Argb32P> destination)
{
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
}
}
}

13
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgr565.PixelOperations.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +13,14 @@ public partial struct Bgr565
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<Bgr565>;
internal class PixelOperations : PixelOperations<Bgr565>
{
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Bgr565> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Bgr565> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
}
}

54
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgra32P.PixelOperations.cs

@ -17,50 +17,48 @@ public partial struct Bgra32P
internal class PixelOperations : AssociatedAlphaPixelOperations<Bgra32P>
{
/// <inheritdoc />
internal override Vector4 ToUnassociatedScaledVector4(Bgra32P source)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source.R, source.G, source.B, source.A);
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<Bgra32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source, destination);
/// <inheritdoc />
internal override Bgra32P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Vector4Converters.AssociatedRgbaCompatible.Associate(source));
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Bgra32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToAssociatedVector4(source, destination);
/// <inheritdoc />
public override Bgra32P FromAssociatedScaledVector4(Vector4 source)
=> Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4ToBgra32P(source);
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Bgra32P> destination)
{
// Native and scaled vectors have the same range for this format, so the byte-specialized converter is valid for both contracts.
Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4(source, destination);
}
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Bgra32P> destination)
{
// The converter rescales RGB when alpha rounds so the channels remain associated with the byte alpha actually stored.
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
}
/// <inheritdoc />
internal override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Bgra32P> source, Span<Vector4> destination)
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Bgra32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source, destination);
/// <inheritdoc />
internal override void FromUnassociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<Bgra32P> destination)
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Bgra32P> source, Span<Vector4> destination)
{
source = source[..destination.Length];
Vector4Converters.AssociatedRgbaCompatible.Associate(source);
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
// This byte format has the same normalized native and scaled ranges, so the unmodified converter satisfies both contracts.
Vector4Converters.AssociatedRgbaCompatible.ToAssociatedVector4(source, destination);
}
/// <inheritdoc />
public override void FromAssociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<Bgra32P> destination)
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Bgra32P> destination)
{
source = source[..destination.Length];
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4(source, destination);
}
/// <inheritdoc />
public override void ToVector4(
Configuration configuration,
ReadOnlySpan<Bgra32P> source,
Span<Vector4> destinationVectors,
PixelConversionModifiers modifiers)
=> Vector4Converters.AssociatedRgbaCompatible.ToVector4(source, destinationVectors, modifiers);
/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Span<Vector4> sourceVectors,
Span<Bgra32P> destination,
PixelConversionModifiers modifiers)
=> Vector4Converters.AssociatedRgbaCompatible.FromVector4(sourceVectors, destination, modifiers);
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Bgra32P> destination)
{
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
}
}
}

94
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Byte4.PixelOperations.cs

@ -1,6 +1,10 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +15,93 @@ public partial struct Byte4
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<Byte4>;
internal class PixelOperations : PixelOperations<Byte4>
{
// Protected hooks are same-instance extension points. Cross-format reuse must request the representation through public modifier dispatch.
private const PixelConversionModifiers UnassociatedScaledModifiers = PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply;
private const PixelConversionModifiers AssociatedScaledModifiers = PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply;
private static readonly Vector4 NativeMaximum = new(byte.MaxValue);
/// <inheritdoc />
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<Byte4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// Byte4 and Rgba32 have the same packed layout. Reusing the optimized Rgba32 unpacker avoids four scalar byte
// extractions per pixel; the affine transform then restores Byte4's native [0, 255] vector range.
PixelOperations<Rgba32>.Instance.ToVector4(configuration, MemoryMarshal.Cast<Byte4, Rgba32>(source), destination, UnassociatedScaledModifiers);
Vector4Converters.MultiplyThenAdd(destination, NativeMaximum, Vector4.Zero);
}
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Byte4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// Association is performed in normalized color space before mapping all four components back to Byte4's native range.
PixelOperations<Rgba32>.Instance.ToVector4(configuration, MemoryMarshal.Cast<Byte4, Rgba32>(source), destination, AssociatedScaledModifiers);
Vector4Converters.MultiplyThenAdd(destination, NativeMaximum, Vector4.Zero);
}
/// <inheritdoc />
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Byte4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
PixelOperations<Rgba32>.Instance.ToVector4(configuration, MemoryMarshal.Cast<Byte4, Rgba32>(source), destination, UnassociatedScaledModifiers);
}
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Byte4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
PixelOperations<Rgba32>.Instance.ToVector4(configuration, MemoryMarshal.Cast<Byte4, Rgba32>(source), destination, AssociatedScaledModifiers);
}
/// <inheritdoc />
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Byte4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
Vector4Converters.AddThenDivide(source, Vector4.Zero, NativeMaximum);
PixelOperations<Rgba32>.Instance.FromVector4Destructive(configuration, source, MemoryMarshal.Cast<Byte4, Rgba32>(destination), UnassociatedScaledModifiers);
}
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Byte4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// Native alpha is normalized with RGB so the Rgba32 bulk path can unassociate in a single, consistent coordinate space.
Vector4Converters.AddThenDivide(source, Vector4.Zero, NativeMaximum);
PixelOperations<Rgba32>.Instance.FromVector4Destructive(configuration, source, MemoryMarshal.Cast<Byte4, Rgba32>(destination), AssociatedScaledModifiers);
}
/// <inheritdoc />
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Byte4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
PixelOperations<Rgba32>.Instance.FromVector4Destructive(configuration, source, MemoryMarshal.Cast<Byte4, Rgba32>(destination), UnassociatedScaledModifiers);
}
/// <inheritdoc />
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Byte4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
PixelOperations<Rgba32>.Instance.FromVector4Destructive(configuration, source, MemoryMarshal.Cast<Byte4, Rgba32>(destination), AssociatedScaledModifiers);
}
}
}

25
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/HalfSingle.PixelOperations.cs

@ -1,6 +1,9 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +14,25 @@ public partial struct HalfSingle
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<HalfSingle>;
internal class PixelOperations : PixelOperations<HalfSingle>
{
private static readonly Vector4 NativeOffset = new(1F, 0F, 0F, 0F);
private static readonly Vector4 NativeDivisor = new(2F, 1F, 1F, 1F);
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<HalfSingle> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<HalfSingle> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfSingle> destination)
{
// Only X uses the affine native range; W must remain normalized so the scaled path can unassociate the source color.
Vector4Converters.AddThenDivide(source, NativeOffset, NativeDivisor);
this.FromAssociatedScaledVector4Destructive(configuration, source, destination);
}
}
}

25
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/HalfVector2.PixelOperations.cs

@ -1,6 +1,9 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +14,25 @@ public partial struct HalfVector2
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<HalfVector2>;
internal class PixelOperations : PixelOperations<HalfVector2>
{
private static readonly Vector4 NativeOffset = new(1F, 1F, 0F, 0F);
private static readonly Vector4 NativeDivisor = new(2F, 2F, 1F, 1F);
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<HalfVector2> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<HalfVector2> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfVector2> destination)
{
// Only X and Y use affine native coordinates. W is the normalized alpha used by the scaled unassociation step.
Vector4Converters.AddThenDivide(source, NativeOffset, NativeDivisor);
this.FromAssociatedScaledVector4Destructive(configuration, source, destination);
}
}
}

90
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/HalfVector4.PixelOperations.cs

@ -1,6 +1,10 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +15,89 @@ public partial struct HalfVector4
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<HalfVector4>;
internal class PixelOperations : PixelOperations<HalfVector4>
{
private static readonly Vector4 NativeScale = new(2F);
/// <inheritdoc />
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<HalfVector4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
HalfVector4P.PixelOperations.Unpack(MemoryMarshal.Cast<HalfVector4, HalfVector4P>(source), destination[..source.Length], false);
}
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<HalfVector4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// HalfVector4P has the same four-half layout, allowing both formats to share the SIMD half expansion. Association must
// still occur in scaled space because the native half values use an affine [-1, 1] color coordinate system.
HalfVector4P.PixelOperations.Unpack(MemoryMarshal.Cast<HalfVector4, HalfVector4P>(source), destination, true);
Numerics.Premultiply(destination);
Vector4Converters.MultiplyThenAdd(destination, NativeScale, -Vector4.One);
}
/// <inheritdoc />
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<HalfVector4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
HalfVector4P.PixelOperations.Unpack(MemoryMarshal.Cast<HalfVector4, HalfVector4P>(source), destination[..source.Length], true);
}
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<HalfVector4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
HalfVector4P.PixelOperations.Unpack(MemoryMarshal.Cast<HalfVector4, HalfVector4P>(source), destination, true);
Numerics.Premultiply(destination);
}
/// <inheritdoc />
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfVector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
HalfVector4P.PixelOperations.Pack(source, MemoryMarshal.Cast<HalfVector4, HalfVector4P>(destination), false);
}
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfVector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// Normalize native alpha with RGB before unassociation, then reuse the shared half packing kernel in scaled mode.
Vector4Converters.AddThenDivide(source, Vector4.One, NativeScale);
Numerics.UnPremultiply(source);
HalfVector4P.PixelOperations.Pack(source, MemoryMarshal.Cast<HalfVector4, HalfVector4P>(destination), true);
}
/// <inheritdoc />
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfVector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
HalfVector4P.PixelOperations.Pack(source, MemoryMarshal.Cast<HalfVector4, HalfVector4P>(destination), true);
}
/// <inheritdoc />
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfVector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
Numerics.UnPremultiply(source);
HalfVector4P.PixelOperations.Pack(source, MemoryMarshal.Cast<HalfVector4, HalfVector4P>(destination), true);
}
}
}

745
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/HalfVector4P.PixelOperations.cs

@ -2,56 +2,759 @@
// Licensed under the Six Labors Split License.
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using SixLabors.ImageSharp.Common.Helpers;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
/// Provides bulk operations.
/// Provides optimized overrides for bulk operations.
/// </content>
public partial struct HalfVector4P
{
/// <summary>
/// Provides bulk operations for <see cref="HalfVector4P"/>.
/// Provides optimized bulk operations for <see cref="HalfVector4P"/>.
/// </summary>
internal class PixelOperations : AssociatedAlphaPixelOperations<HalfVector4P>
{
/// <inheritdoc />
internal override Vector4 ToUnassociatedScaledVector4(HalfVector4P source)
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<HalfVector4P> source, Span<Vector4> destination)
{
Vector4 vector = source.ToScaledVector4();
Numerics.UnPremultiply(ref vector);
return vector;
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
UnpackUnassociated(source, destination[..source.Length], false);
}
/// <inheritdoc />
internal override HalfVector4P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Associate(source));
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<HalfVector4P> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
Unpack(source, destination[..source.Length], false);
}
/// <inheritdoc />
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfVector4P> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
PackUnassociated(source, destination, false);
}
/// <inheritdoc />
public override HalfVector4P FromAssociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Reassociate(source));
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfVector4P> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
PackAssociated(source, destination, false);
}
/// <inheritdoc />
internal override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<HalfVector4P> source, Span<Vector4> destination)
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<HalfVector4P> source, Span<Vector4> destination)
{
this.ToVector4(configuration, source, destination, PixelConversionModifiers.Scale);
Numerics.UnPremultiply(destination[..source.Length]);
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
UnpackUnassociated(source, destination[..source.Length], true);
}
/// <inheritdoc />
internal override void FromUnassociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<HalfVector4P> destination)
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<HalfVector4P> source, Span<Vector4> destination)
{
source = source[..destination.Length];
Associate(source);
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
Unpack(source, destination[..source.Length], true);
}
/// <inheritdoc />
public override void FromAssociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<HalfVector4P> destination)
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfVector4P> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
PackUnassociated(source, destination, true);
}
/// <inheritdoc />
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<HalfVector4P> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
PackAssociated(source, destination, true);
}
/// <summary>
/// Unpacks native or scaled binary16 components into vectors.
/// </summary>
/// <param name="source">The packed source pixels.</param>
/// <param name="destination">The destination vectors.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
internal static void Unpack(ReadOnlySpan<HalfVector4P> source, Span<Vector4> destination, bool scaled)
{
ref ushort sourceBase = ref Unsafe.As<HalfVector4P, ushort>(ref MemoryMarshal.GetReference(source));
ref float destinationBase = ref Unsafe.As<Vector4, float>(ref MemoryMarshal.GetReference(destination));
int componentCount = source.Length * Vector128<float>.Count;
int i = 0;
if (Vector512.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector512<ushort>.Count; i += Vector512<ushort>.Count)
{
Vector512<ushort> packed = Vector512.LoadUnsafe(ref sourceBase, (nuint)i);
(Vector512<float> lower, Vector512<float> upper) = HalfTypeHelper.Unpack(packed);
if (scaled)
{
lower = ToScaled(lower);
upper = ToScaled(upper);
}
Vector512.StoreUnsafe(lower, ref destinationBase, (nuint)i);
Vector512.StoreUnsafe(upper, ref destinationBase, (nuint)(i + Vector512<float>.Count));
}
}
if (Vector256.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector256<ushort>.Count; i += Vector256<ushort>.Count)
{
Vector256<ushort> packed = Vector256.LoadUnsafe(ref sourceBase, (nuint)i);
(Vector256<float> lower, Vector256<float> upper) = HalfTypeHelper.Unpack(packed);
if (scaled)
{
lower = ToScaled(lower);
upper = ToScaled(upper);
}
Vector256.StoreUnsafe(lower, ref destinationBase, (nuint)i);
Vector256.StoreUnsafe(upper, ref destinationBase, (nuint)(i + Vector256<float>.Count));
}
}
if (Vector128.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector128<ushort>.Count; i += Vector128<ushort>.Count)
{
Vector128<ushort> packed = Vector128.LoadUnsafe(ref sourceBase, (nuint)i);
(Vector128<float> lower, Vector128<float> upper) = HalfTypeHelper.Unpack(packed);
if (scaled)
{
lower = ToScaled(lower);
upper = ToScaled(upper);
}
Vector128.StoreUnsafe(lower, ref destinationBase, (nuint)i);
Vector128.StoreUnsafe(upper, ref destinationBase, (nuint)(i + Vector128<float>.Count));
}
if (i < componentCount)
{
// A pixel contains four halves, so the only possible remainder is one complete pixel.
ulong remainder = Unsafe.ReadUnaligned<ulong>(ref Unsafe.As<ushort, byte>(ref Unsafe.Add(ref sourceBase, (uint)i)));
Vector128<ushort> packed = Vector128.CreateScalarUnsafe(remainder).AsUInt16();
Vector128<float> vector = HalfTypeHelper.Unpack(packed).Lower;
if (scaled)
{
vector = ToScaled(vector);
}
Vector128.StoreUnsafe(vector, ref destinationBase, (nuint)i);
}
return;
}
ref HalfVector4P pixelBase = ref Unsafe.As<ushort, HalfVector4P>(ref sourceBase);
ref Vector4 vectorBase = ref Unsafe.As<float, Vector4>(ref destinationBase);
for (int pixelIndex = 0; pixelIndex < source.Length; pixelIndex++)
{
HalfVector4P pixel = Unsafe.Add(ref pixelBase, (uint)pixelIndex);
Unsafe.Add(ref vectorBase, (uint)pixelIndex) = scaled ? pixel.ToScaledVector4() : pixel.ToVector4();
}
}
/// <summary>
/// Unpacks associated binary16 storage directly into unassociated native or scaled vectors.
/// </summary>
/// <param name="source">The packed source pixels.</param>
/// <param name="destination">The destination vectors.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
private static void UnpackUnassociated(ReadOnlySpan<HalfVector4P> source, Span<Vector4> destination, bool scaled)
{
ref ushort sourceBase = ref Unsafe.As<HalfVector4P, ushort>(ref MemoryMarshal.GetReference(source));
ref float destinationBase = ref Unsafe.As<Vector4, float>(ref MemoryMarshal.GetReference(destination));
int componentCount = source.Length * Vector128<float>.Count;
int i = 0;
if (Vector512.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector512<ushort>.Count; i += Vector512<ushort>.Count)
{
Vector512<ushort> packed = Vector512.LoadUnsafe(ref sourceBase, (nuint)i);
(Vector512<float> lower, Vector512<float> upper) = HalfTypeHelper.Unpack(packed);
Vector512.StoreUnsafe(ToUnassociated(lower, scaled), ref destinationBase, (nuint)i);
Vector512.StoreUnsafe(ToUnassociated(upper, scaled), ref destinationBase, (nuint)(i + Vector512<float>.Count));
}
}
if (Vector256.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector256<ushort>.Count; i += Vector256<ushort>.Count)
{
Vector256<ushort> packed = Vector256.LoadUnsafe(ref sourceBase, (nuint)i);
(Vector256<float> lower, Vector256<float> upper) = HalfTypeHelper.Unpack(packed);
Vector256.StoreUnsafe(ToUnassociated(lower, scaled), ref destinationBase, (nuint)i);
Vector256.StoreUnsafe(ToUnassociated(upper, scaled), ref destinationBase, (nuint)(i + Vector256<float>.Count));
}
}
if (Vector128.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector128<ushort>.Count; i += Vector128<ushort>.Count)
{
Vector128<ushort> packed = Vector128.LoadUnsafe(ref sourceBase, (nuint)i);
(Vector128<float> lower, Vector128<float> upper) = HalfTypeHelper.Unpack(packed);
Vector128.StoreUnsafe(ToUnassociated(lower, scaled), ref destinationBase, (nuint)i);
Vector128.StoreUnsafe(ToUnassociated(upper, scaled), ref destinationBase, (nuint)(i + Vector128<float>.Count));
}
if (i < componentCount)
{
// A pixel contains four halves, so the only possible remainder is one complete pixel.
ulong remainder = Unsafe.ReadUnaligned<ulong>(ref Unsafe.As<ushort, byte>(ref Unsafe.Add(ref sourceBase, (uint)i)));
Vector128<ushort> packed = Vector128.CreateScalarUnsafe(remainder).AsUInt16();
Vector128<float> vector = HalfTypeHelper.Unpack(packed).Lower;
Vector128.StoreUnsafe(ToUnassociated(vector, scaled), ref destinationBase, (nuint)i);
}
return;
}
ref HalfVector4P pixelBase = ref Unsafe.As<ushort, HalfVector4P>(ref sourceBase);
ref Vector4 vectorBase = ref Unsafe.As<float, Vector4>(ref destinationBase);
for (int pixelIndex = 0; pixelIndex < source.Length; pixelIndex++)
{
HalfVector4P pixel = Unsafe.Add(ref pixelBase, (uint)pixelIndex);
Unsafe.Add(ref vectorBase, (uint)pixelIndex) = scaled ? pixel.ToUnassociatedScaledVector4() : pixel.ToUnassociatedVector4();
}
}
/// <summary>
/// Packs native or scaled vectors into binary16 storage.
/// </summary>
/// <param name="source">The source vectors.</param>
/// <param name="destination">The destination pixels.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
internal static void Pack(Span<Vector4> source, Span<HalfVector4P> destination, bool scaled)
{
ref float sourceBase = ref Unsafe.As<Vector4, float>(ref MemoryMarshal.GetReference(source));
ref ushort destinationBase = ref Unsafe.As<HalfVector4P, ushort>(ref MemoryMarshal.GetReference(destination));
int componentCount = source.Length * Vector128<float>.Count;
int i = 0;
if (Vector512.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector512<ushort>.Count; i += Vector512<ushort>.Count)
{
Vector512<float> lower = Vector512.LoadUnsafe(ref sourceBase, (nuint)i);
Vector512<float> upper = Vector512.LoadUnsafe(ref sourceBase, (nuint)(i + Vector512<float>.Count));
Vector512<ushort> packed = scaled ? PackScaled(lower, upper) : HalfTypeHelper.Pack(lower, upper);
Vector512.StoreUnsafe(packed, ref destinationBase, (nuint)i);
}
}
if (Vector256.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector256<ushort>.Count; i += Vector256<ushort>.Count)
{
Vector256<float> lower = Vector256.LoadUnsafe(ref sourceBase, (nuint)i);
Vector256<float> upper = Vector256.LoadUnsafe(ref sourceBase, (nuint)(i + Vector256<float>.Count));
Vector256<ushort> packed = scaled ? PackScaled(lower, upper) : HalfTypeHelper.Pack(lower, upper);
Vector256.StoreUnsafe(packed, ref destinationBase, (nuint)i);
}
}
if (Vector128.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector128<ushort>.Count; i += Vector128<ushort>.Count)
{
Vector128<float> lower = Vector128.LoadUnsafe(ref sourceBase, (nuint)i);
Vector128<float> upper = Vector128.LoadUnsafe(ref sourceBase, (nuint)(i + Vector128<float>.Count));
Vector128<ushort> packed = scaled ? PackScaled(lower, upper) : HalfTypeHelper.Pack(lower, upper);
Vector128.StoreUnsafe(packed, ref destinationBase, (nuint)i);
}
if (i < componentCount)
{
// Duplicate the final vector to use the two-input narrowing primitive, then store only its lower pixel.
Vector128<float> vector = Vector128.LoadUnsafe(ref sourceBase, (nuint)i);
Vector128<ushort> packed = scaled ? PackScaled(vector, vector) : HalfTypeHelper.Pack(vector, vector);
Unsafe.WriteUnaligned(ref Unsafe.As<ushort, byte>(ref Unsafe.Add(ref destinationBase, (uint)i)), packed.AsUInt64().GetElement(0));
}
return;
}
ref Vector4 vectorBase = ref Unsafe.As<float, Vector4>(ref sourceBase);
ref HalfVector4P pixelBase = ref Unsafe.As<ushort, HalfVector4P>(ref destinationBase);
for (int pixelIndex = 0; pixelIndex < source.Length; pixelIndex++)
{
Vector4 vector = Unsafe.Add(ref vectorBase, (uint)pixelIndex);
Unsafe.Add(ref pixelBase, (uint)pixelIndex) = scaled ? FromScaledVector4(vector) : FromVector4(vector);
}
}
/// <summary>
/// Associates unassociated native or scaled vectors and packs them into binary16 storage in one pass.
/// </summary>
/// <param name="source">The unassociated vectors.</param>
/// <param name="destination">The destination pixels.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
private static void PackUnassociated(Span<Vector4> source, Span<HalfVector4P> destination, bool scaled)
{
ref float sourceBase = ref Unsafe.As<Vector4, float>(ref MemoryMarshal.GetReference(source));
ref ushort destinationBase = ref Unsafe.As<HalfVector4P, ushort>(ref MemoryMarshal.GetReference(destination));
int componentCount = source.Length * Vector128<float>.Count;
int i = 0;
// Quantize alpha to the binary16 value the destination will store before associating RGB. The kernels replace W separately
// so alpha is stored unchanged instead of being multiplied by itself.
if (Vector512.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector512<ushort>.Count; i += Vector512<ushort>.Count)
{
Vector512<float> lower = Associate(Vector512.LoadUnsafe(ref sourceBase, (nuint)i), scaled);
Vector512<float> upper = Associate(Vector512.LoadUnsafe(ref sourceBase, (nuint)(i + Vector512<float>.Count)), scaled);
Vector512.StoreUnsafe(PackScaled(lower, upper), ref destinationBase, (nuint)i);
}
}
if (Vector256.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector256<ushort>.Count; i += Vector256<ushort>.Count)
{
Vector256<float> lower = Associate(Vector256.LoadUnsafe(ref sourceBase, (nuint)i), scaled);
Vector256<float> upper = Associate(Vector256.LoadUnsafe(ref sourceBase, (nuint)(i + Vector256<float>.Count)), scaled);
Vector256.StoreUnsafe(PackScaled(lower, upper), ref destinationBase, (nuint)i);
}
}
if (Vector128.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector128<ushort>.Count; i += Vector128<ushort>.Count)
{
Vector128<float> lower = Associate(Vector128.LoadUnsafe(ref sourceBase, (nuint)i), scaled);
Vector128<float> upper = Associate(Vector128.LoadUnsafe(ref sourceBase, (nuint)(i + Vector128<float>.Count)), scaled);
Vector128.StoreUnsafe(PackScaled(lower, upper), ref destinationBase, (nuint)i);
}
if (i < componentCount)
{
// Duplicate the final vector to use the two-input narrowing primitive, then store only its lower pixel.
Vector128<float> vector = Associate(Vector128.LoadUnsafe(ref sourceBase, (nuint)i), scaled);
Vector128<ushort> packed = PackScaled(vector, vector);
Unsafe.WriteUnaligned(ref Unsafe.As<ushort, byte>(ref Unsafe.Add(ref destinationBase, (uint)i)), packed.AsUInt64().GetElement(0));
}
return;
}
ref Vector4 vectorBase = ref Unsafe.As<float, Vector4>(ref sourceBase);
ref HalfVector4P pixelBase = ref Unsafe.As<ushort, HalfVector4P>(ref destinationBase);
for (int pixelIndex = 0; pixelIndex < source.Length; pixelIndex++)
{
Vector4 vector = Unsafe.Add(ref vectorBase, (uint)pixelIndex);
// Qualify the containing pixel type because this nested class also declares bulk overloads with these names.
Unsafe.Add(ref pixelBase, (uint)pixelIndex) = scaled ? HalfVector4P.FromUnassociatedScaledVector4(vector) : HalfVector4P.FromUnassociatedVector4(vector);
}
}
/// <summary>
/// Reassociates associated native or scaled vectors and packs them into binary16 storage in one pass.
/// </summary>
/// <param name="source">The associated vectors.</param>
/// <param name="destination">The destination pixels.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
private static void PackAssociated(Span<Vector4> source, Span<HalfVector4P> destination, bool scaled)
{
ref float sourceBase = ref Unsafe.As<Vector4, float>(ref MemoryMarshal.GetReference(source));
ref ushort destinationBase = ref Unsafe.As<HalfVector4P, ushort>(ref MemoryMarshal.GetReference(destination));
int componentCount = source.Length * Vector128<float>.Count;
int i = 0;
// Preserve straight color across binary16 alpha quantization by scaling RGB by storedAlpha / inputAlpha. The kernels replace
// W, clamp RGB to stored alpha, and clear zero-alpha vectors so the packed result remains valid associated color.
if (Vector512.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector512<ushort>.Count; i += Vector512<ushort>.Count)
{
Vector512<float> lower = Reassociate(Vector512.LoadUnsafe(ref sourceBase, (nuint)i), scaled);
Vector512<float> upper = Reassociate(Vector512.LoadUnsafe(ref sourceBase, (nuint)(i + Vector512<float>.Count)), scaled);
Vector512.StoreUnsafe(PackScaled(lower, upper), ref destinationBase, (nuint)i);
}
}
if (Vector256.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector256<ushort>.Count; i += Vector256<ushort>.Count)
{
Vector256<float> lower = Reassociate(Vector256.LoadUnsafe(ref sourceBase, (nuint)i), scaled);
Vector256<float> upper = Reassociate(Vector256.LoadUnsafe(ref sourceBase, (nuint)(i + Vector256<float>.Count)), scaled);
Vector256.StoreUnsafe(PackScaled(lower, upper), ref destinationBase, (nuint)i);
}
}
if (Vector128.IsHardwareAccelerated)
{
for (; i <= componentCount - Vector128<ushort>.Count; i += Vector128<ushort>.Count)
{
Vector128<float> lower = Reassociate(Vector128.LoadUnsafe(ref sourceBase, (nuint)i), scaled);
Vector128<float> upper = Reassociate(Vector128.LoadUnsafe(ref sourceBase, (nuint)(i + Vector128<float>.Count)), scaled);
Vector128.StoreUnsafe(PackScaled(lower, upper), ref destinationBase, (nuint)i);
}
if (i < componentCount)
{
// Duplicate the final vector to use the two-input narrowing primitive, then store only its lower pixel.
Vector128<float> vector = Reassociate(Vector128.LoadUnsafe(ref sourceBase, (nuint)i), scaled);
Vector128<ushort> packed = PackScaled(vector, vector);
Unsafe.WriteUnaligned(ref Unsafe.As<ushort, byte>(ref Unsafe.Add(ref destinationBase, (uint)i)), packed.AsUInt64().GetElement(0));
}
return;
}
ref Vector4 vectorBase = ref Unsafe.As<float, Vector4>(ref sourceBase);
ref HalfVector4P pixelBase = ref Unsafe.As<ushort, HalfVector4P>(ref destinationBase);
for (int pixelIndex = 0; pixelIndex < source.Length; pixelIndex++)
{
Vector4 vector = Unsafe.Add(ref vectorBase, (uint)pixelIndex);
// Qualify the containing pixel type because this nested class also declares bulk overloads with these names.
Unsafe.Add(ref pixelBase, (uint)pixelIndex) = scaled ? HalfVector4P.FromAssociatedScaledVector4(vector) : HalfVector4P.FromAssociatedVector4(vector);
}
}
/// <summary>
/// Converts native binary16 values to the normalized range used by pixel conversions.
/// </summary>
/// <param name="source">The native values.</param>
/// <returns>The scaled values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<float> ToScaled(Vector128<float> source) => (source + Vector128<float>.One) / Vector128.Create(2F);
/// <summary>
/// Converts native binary16 values to the normalized range used by pixel conversions.
/// </summary>
/// <param name="source">The native values.</param>
/// <returns>The scaled values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<float> ToScaled(Vector256<float> source) => (source + Vector256<float>.One) / Vector256.Create(2F);
/// <summary>
/// Converts native binary16 values to the normalized range used by pixel conversions.
/// </summary>
/// <param name="source">The native values.</param>
/// <returns>The scaled values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<float> ToScaled(Vector512<float> source) => (source + Vector512<float>.One) / Vector512.Create(2F);
/// <summary>
/// Converts associated native binary16 values to unassociated native or scaled vectors.
/// </summary>
/// <param name="source">The associated native values.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
/// <returns>The unassociated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<float> ToUnassociated(Vector128<float> source, bool scaled)
{
source = ToScaled(source);
Vector128<float> alpha = Vector128_.ShuffleNative(source, 0b_11_11_11_11);
Vector128<float> result = Vector128.ConditionalSelect(Vector128.Equals(alpha, Vector128<float>.Zero), source, source / alpha);
Vector128<float> alphaMask = Vector128.Create(0, 0, 0, -1).AsSingle();
result = Vector128.ConditionalSelect(alphaMask, alpha, result);
// Binary16-native W is an affine encoding rather than opacity, so map back only after unassociation in scaled space.
return scaled ? result : (result * Vector128.Create(2F)) - Vector128<float>.One;
}
/// <summary>
/// Converts associated native binary16 values to unassociated native or scaled vectors.
/// </summary>
/// <param name="source">The associated native values.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
/// <returns>The unassociated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<float> ToUnassociated(Vector256<float> source, bool scaled)
{
source = ToScaled(source);
Vector256<float> alpha = Vector256_.ShuffleNative(source, 0b_11_11_11_11);
Vector256<float> result = Vector256.ConditionalSelect(Vector256.Equals(alpha, Vector256<float>.Zero), source, source / alpha);
Vector256<float> alphaMask = Vector256.Create(0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
result = Vector256.ConditionalSelect(alphaMask, alpha, result);
// Binary16-native W is an affine encoding rather than opacity, so map back only after unassociation in scaled space.
return scaled ? result : (result * Vector256.Create(2F)) - Vector256<float>.One;
}
/// <summary>
/// Converts associated native binary16 values to unassociated native or scaled vectors.
/// </summary>
/// <param name="source">The associated native values.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
/// <returns>The unassociated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<float> ToUnassociated(Vector512<float> source, bool scaled)
{
source = ToScaled(source);
Vector512<float> alpha = Vector512_.ShuffleNative(source, 0b_11_11_11_11);
Vector512<float> result = Vector512.ConditionalSelect(Vector512.Equals(alpha, Vector512<float>.Zero), source, source / alpha);
Vector512<float> alphaMask = Vector512.Create(0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
result = Vector512.ConditionalSelect(alphaMask, alpha, result);
// Binary16-native W is an affine encoding rather than opacity, so map back only after unassociation in scaled space.
return scaled ? result : (result * Vector512.Create(2F)) - Vector512<float>.One;
}
/// <summary>
/// Converts unassociated native or scaled vectors to the destination's associated scaled representation.
/// </summary>
/// <param name="source">The unassociated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The associated scaled vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<float> Associate(Vector128<float> source, bool scaled)
{
Vector128<float> zero = Vector128<float>.Zero;
Vector128<float> one = Vector128<float>.One;
if (!scaled)
{
// Binary16-native W is an affine encoding rather than opacity, so association must happen after mapping to scaled space.
source = ToScaled(source);
}
source = Vector128.Min(Vector128.Max(source, zero), one);
Vector128<float> alpha = Vector128_.ShuffleNative(source, 0b_11_11_11_11);
Vector128<float> storedAlpha = (HalfTypeHelper.RoundToHalf((alpha * Vector128.Create(2F)) - one) + one) / Vector128.Create(2F);
Vector128<float> result = source * storedAlpha;
Vector128<float> alphaMask = Vector128.Create(0, 0, 0, -1).AsSingle();
return Vector128.ConditionalSelect(alphaMask, storedAlpha, result);
}
/// <summary>
/// Converts unassociated native or scaled vectors to the destination's associated scaled representation.
/// </summary>
/// <param name="source">The unassociated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The associated scaled vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<float> Associate(Vector256<float> source, bool scaled)
{
Vector256<float> zero = Vector256<float>.Zero;
Vector256<float> one = Vector256<float>.One;
if (!scaled)
{
// Binary16-native W is an affine encoding rather than opacity, so association must happen after mapping to scaled space.
source = ToScaled(source);
}
source = Vector256.Min(Vector256.Max(source, zero), one);
Vector256<float> alpha = Vector256_.ShuffleNative(source, 0b_11_11_11_11);
Vector256<float> storedAlpha = (HalfTypeHelper.RoundToHalf((alpha * Vector256.Create(2F)) - one) + one) / Vector256.Create(2F);
Vector256<float> result = source * storedAlpha;
Vector256<float> alphaMask = Vector256.Create(0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
return Vector256.ConditionalSelect(alphaMask, storedAlpha, result);
}
/// <summary>
/// Converts unassociated native or scaled vectors to the destination's associated scaled representation.
/// </summary>
/// <param name="source">The unassociated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The associated scaled vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<float> Associate(Vector512<float> source, bool scaled)
{
Vector512<float> zero = Vector512<float>.Zero;
Vector512<float> one = Vector512<float>.One;
if (!scaled)
{
// Binary16-native W is an affine encoding rather than opacity, so association must happen after mapping to scaled space.
source = ToScaled(source);
}
source = Vector512.Min(Vector512.Max(source, zero), one);
Vector512<float> alpha = Vector512_.ShuffleNative(source, 0b_11_11_11_11);
Vector512<float> storedAlpha = (HalfTypeHelper.RoundToHalf((alpha * Vector512.Create(2F)) - one) + one) / Vector512.Create(2F);
Vector512<float> result = source * storedAlpha;
Vector512<float> alphaMask = Vector512.Create(0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
return Vector512.ConditionalSelect(alphaMask, storedAlpha, result);
}
/// <summary>
/// Reassociates native or scaled vectors with the scaled alpha values the destination stores.
/// </summary>
/// <param name="source">The associated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The reassociated scaled vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<float> Reassociate(Vector128<float> source, bool scaled)
{
Vector128<float> zero = Vector128<float>.Zero;
Vector128<float> one = Vector128<float>.One;
if (!scaled)
{
// Convert every component together so RGB and alpha enter reassociation in the same scaled coordinate system.
source = ToScaled(source);
}
Vector128<float> alpha = Vector128_.ShuffleNative(source, 0b_11_11_11_11);
Vector128<float> nativeAlpha = (alpha * Vector128.Create(2F)) - one;
Vector128<float> clampedNativeAlpha = Vector128.Min(Vector128.Max(nativeAlpha, -one), one);
// Hardware Min/Max replace NaN on .NET 8, whereas the scalar clamp preserves it. Restore those lanes before half quantization.
nativeAlpha = Vector128.ConditionalSelect(Vector128.Equals(nativeAlpha, nativeAlpha), clampedNativeAlpha, nativeAlpha);
// Clamp before binary16 quantization because the scaled alpha contract cannot represent opacity outside [0, 1].
Vector128<float> storedAlpha = (HalfTypeHelper.RoundToHalf(nativeAlpha) + one) / Vector128.Create(2F);
Vector128<float> result = source * (storedAlpha / alpha);
Vector128<float> alphaMask = Vector128.Create(0, 0, 0, -1).AsSingle();
result = Vector128.ConditionalSelect(alphaMask, storedAlpha, result);
result = Vector128.Min(Vector128.Max(result, zero), storedAlpha);
return Vector128.ConditionalSelect(Vector128.LessThanOrEqual(alpha, zero), zero, result);
}
/// <summary>
/// Reassociates native or scaled vectors with the scaled alpha values the destination stores.
/// </summary>
/// <param name="source">The associated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The reassociated scaled vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<float> Reassociate(Vector256<float> source, bool scaled)
{
Vector256<float> zero = Vector256<float>.Zero;
Vector256<float> one = Vector256<float>.One;
if (!scaled)
{
// Convert every component together so RGB and alpha enter reassociation in the same scaled coordinate system.
source = ToScaled(source);
}
Vector256<float> alpha = Vector256_.ShuffleNative(source, 0b_11_11_11_11);
Vector256<float> nativeAlpha = (alpha * Vector256.Create(2F)) - one;
Vector256<float> clampedNativeAlpha = Vector256.Min(Vector256.Max(nativeAlpha, -one), one);
// Hardware Min/Max replace NaN on .NET 8, whereas the scalar clamp preserves it. Restore those lanes before half quantization.
nativeAlpha = Vector256.ConditionalSelect(Vector256.Equals(nativeAlpha, nativeAlpha), clampedNativeAlpha, nativeAlpha);
// Clamp before binary16 quantization because the scaled alpha contract cannot represent opacity outside [0, 1].
Vector256<float> storedAlpha = (HalfTypeHelper.RoundToHalf(nativeAlpha) + one) / Vector256.Create(2F);
Vector256<float> result = source * (storedAlpha / alpha);
Vector256<float> alphaMask = Vector256.Create(0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
result = Vector256.ConditionalSelect(alphaMask, storedAlpha, result);
result = Vector256.Min(Vector256.Max(result, zero), storedAlpha);
return Vector256.ConditionalSelect(Vector256.LessThanOrEqual(alpha, zero), zero, result);
}
/// <summary>
/// Reassociates native or scaled vectors with the scaled alpha values the destination stores.
/// </summary>
/// <param name="source">The associated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The reassociated scaled vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<float> Reassociate(Vector512<float> source, bool scaled)
{
Vector512<float> zero = Vector512<float>.Zero;
Vector512<float> one = Vector512<float>.One;
if (!scaled)
{
// Convert every component together so RGB and alpha enter reassociation in the same scaled coordinate system.
source = ToScaled(source);
}
Vector512<float> alpha = Vector512_.ShuffleNative(source, 0b_11_11_11_11);
Vector512<float> nativeAlpha = (alpha * Vector512.Create(2F)) - one;
Vector512<float> clampedNativeAlpha = Vector512.Min(Vector512.Max(nativeAlpha, -one), one);
// Hardware Min/Max replace NaN on .NET 8, whereas the scalar clamp preserves it. Restore those lanes before half quantization.
nativeAlpha = Vector512.ConditionalSelect(Vector512.Equals(nativeAlpha, nativeAlpha), clampedNativeAlpha, nativeAlpha);
// Clamp before binary16 quantization because the scaled alpha contract cannot represent opacity outside [0, 1].
Vector512<float> storedAlpha = (HalfTypeHelper.RoundToHalf(nativeAlpha) + one) / Vector512.Create(2F);
Vector512<float> result = source * (storedAlpha / alpha);
Vector512<float> alphaMask = Vector512.Create(0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
result = Vector512.ConditionalSelect(alphaMask, storedAlpha, result);
result = Vector512.Min(Vector512.Max(result, zero), storedAlpha);
return Vector512.ConditionalSelect(Vector512.LessThanOrEqual(alpha, zero), zero, result);
}
/// <summary>
/// Converts two scaled vectors to binary16 storage while preserving scalar operation order.
/// </summary>
/// <param name="lower">The lower scaled values.</param>
/// <param name="upper">The upper scaled values.</param>
/// <returns>The packed binary16 values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<ushort> PackScaled(Vector128<float> lower, Vector128<float> upper)
{
lower *= Vector128.Create(2F);
lower -= Vector128<float>.One;
upper *= Vector128.Create(2F);
upper -= Vector128<float>.One;
return HalfTypeHelper.Pack(lower, upper);
}
/// <summary>
/// Converts two scaled vectors to binary16 storage while preserving scalar operation order.
/// </summary>
/// <param name="lower">The lower scaled values.</param>
/// <param name="upper">The upper scaled values.</param>
/// <returns>The packed binary16 values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<ushort> PackScaled(Vector256<float> lower, Vector256<float> upper)
{
lower *= Vector256.Create(2F);
lower -= Vector256<float>.One;
upper *= Vector256.Create(2F);
upper -= Vector256<float>.One;
return HalfTypeHelper.Pack(lower, upper);
}
/// <summary>
/// Converts two scaled vectors to binary16 storage while preserving scalar operation order.
/// </summary>
/// <param name="lower">The lower scaled values.</param>
/// <param name="upper">The upper scaled values.</param>
/// <returns>The packed binary16 values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<ushort> PackScaled(Vector512<float> lower, Vector512<float> upper)
{
source = source[..destination.Length];
Reassociate(source);
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
lower *= Vector512.Create(2F);
lower -= Vector512<float>.One;
upper *= Vector512.Create(2F);
upper -= Vector512<float>.One;
return HalfTypeHelper.Pack(lower, upper);
}
}
}

13
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/L16.PixelOperations.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +13,14 @@ public partial struct L16
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal partial class PixelOperations : PixelOperations<L16>;
internal partial class PixelOperations : PixelOperations<L16>
{
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<L16> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<L16> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
}
}

13
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/L8.PixelOperations.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +13,14 @@ public partial struct L8
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal partial class PixelOperations : PixelOperations<L8>;
internal partial class PixelOperations : PixelOperations<L8>
{
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<L8> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<L8> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
}
}

25
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedByte2.PixelOperations.cs

@ -1,6 +1,9 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +14,25 @@ public partial struct NormalizedByte2
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<NormalizedByte2>;
internal class PixelOperations : PixelOperations<NormalizedByte2>
{
private static readonly Vector4 NativeOffset = new(1F, 1F, 0F, 0F);
private static readonly Vector4 NativeDivisor = new(2F, 2F, 1F, 1F);
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<NormalizedByte2> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<NormalizedByte2> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedByte2> destination)
{
// Only X and Y use affine native coordinates. Preserve W as normalized alpha for the scaled unassociation step.
Vector4Converters.AddThenDivide(source, NativeOffset, NativeDivisor);
this.FromAssociatedScaledVector4Destructive(configuration, source, destination);
}
}
}

90
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedByte4.PixelOperations.cs

@ -1,6 +1,10 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +15,89 @@ public partial struct NormalizedByte4
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<NormalizedByte4>;
internal class PixelOperations : PixelOperations<NormalizedByte4>
{
private static readonly Vector4 NativeScale = new(2F);
/// <inheritdoc />
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<NormalizedByte4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
NormalizedByte4P.PixelOperations.ToVector4(MemoryMarshal.Cast<NormalizedByte4, NormalizedByte4P>(source), destination[..source.Length], false);
}
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<NormalizedByte4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// The packed P and non-P layouts are identical, so the shared SIMD unpacker supplies normalized values. Association
// happens in [0, 1] before the result is mapped back to the signed-native [-1, 1] coordinate system.
NormalizedByte4P.PixelOperations.ToVector4(MemoryMarshal.Cast<NormalizedByte4, NormalizedByte4P>(source), destination, true);
Numerics.Premultiply(destination);
Vector4Converters.MultiplyThenAdd(destination, NativeScale, -Vector4.One);
}
/// <inheritdoc />
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<NormalizedByte4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
NormalizedByte4P.PixelOperations.ToVector4(MemoryMarshal.Cast<NormalizedByte4, NormalizedByte4P>(source), destination[..source.Length], true);
}
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<NormalizedByte4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
NormalizedByte4P.PixelOperations.ToVector4(MemoryMarshal.Cast<NormalizedByte4, NormalizedByte4P>(source), destination, true);
Numerics.Premultiply(destination);
}
/// <inheritdoc />
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
NormalizedByte4P.PixelOperations.Pack(source, MemoryMarshal.Cast<NormalizedByte4, NormalizedByte4P>(destination), false);
}
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// Convert all native components together so alpha and RGB enter unassociation in the same normalized coordinate system.
Vector4Converters.AddThenDivide(source, Vector4.One, NativeScale);
Numerics.UnPremultiply(source);
NormalizedByte4P.PixelOperations.Pack(source, MemoryMarshal.Cast<NormalizedByte4, NormalizedByte4P>(destination), true);
}
/// <inheritdoc />
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
NormalizedByte4P.PixelOperations.Pack(source, MemoryMarshal.Cast<NormalizedByte4, NormalizedByte4P>(destination), true);
}
/// <inheritdoc />
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
Numerics.UnPremultiply(source);
NormalizedByte4P.PixelOperations.Pack(source, MemoryMarshal.Cast<NormalizedByte4, NormalizedByte4P>(destination), true);
}
}
}

756
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedByte4P.PixelOperations.cs

@ -4,59 +4,773 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using SixLabors.ImageSharp.Common.Helpers;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
/// Provides bulk operations.
/// Provides optimized overrides for bulk operations.
/// </content>
public partial struct NormalizedByte4P
{
/// <summary>
/// Provides bulk operations for <see cref="NormalizedByte4P"/>.
/// Provides optimized bulk operations for <see cref="NormalizedByte4P"/>.
/// </summary>
internal class PixelOperations : AssociatedAlphaPixelOperations<NormalizedByte4P>
{
/// <inheritdoc />
internal override Vector4 ToUnassociatedScaledVector4(NormalizedByte4P source)
=> NormalizedByte4P.ToUnassociatedScaledVector4(source);
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<NormalizedByte4P> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ToUnassociatedVector4(source, destination[..source.Length], false);
}
/// <inheritdoc />
internal override NormalizedByte4P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Associate(source));
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<NormalizedByte4P> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ToVector4(source, destination[..source.Length], false);
}
/// <inheritdoc />
public override NormalizedByte4P FromAssociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Reassociate(source));
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4P> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
Associate(source, false);
Pack(source, destination, true);
}
/// <inheritdoc />
internal override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<NormalizedByte4P> source, Span<Vector4> destination)
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4P> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref NormalizedByte4P sourceBase = ref MemoryMarshal.GetReference(source);
destination = destination[..source.Length];
Reassociate(source, false);
Pack(source, destination, true);
}
/// <inheritdoc />
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<NormalizedByte4P> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ToUnassociatedVector4(source, destination[..source.Length], true);
}
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<NormalizedByte4P> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ToVector4(source, destination[..source.Length], true);
}
/// <inheritdoc />
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4P> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
Associate(source, true);
Pack(source, destination, true);
}
/// <inheritdoc />
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4P> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
Reassociate(source, true);
Pack(source, destination, true);
}
/// <summary>
/// Converts packed signed-normalized pixels to native or scaled vectors.
/// </summary>
/// <param name="source">The source pixels.</param>
/// <param name="destination">The destination vectors.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
internal static void ToVector4(ReadOnlySpan<NormalizedByte4P> source, Span<Vector4> destination, bool scaled)
{
ref byte sourceBase = ref Unsafe.As<NormalizedByte4P, byte>(ref MemoryMarshal.GetReference(source));
ref Vector4 destinationBase = ref MemoryMarshal.GetReference(destination);
// Each packed component occupies one byte, so the destination Vector4 lane count also defines the source byte stride.
int componentsPerPixel = Vector128<float>.Count;
int i = 0;
if (Vector512.IsHardwareAccelerated && Avx512F.IsSupported)
{
int pixelsPerVector = Vector512<float>.Count / Vector128<float>.Count;
for (; i <= source.Length - pixelsPerVector; i += pixelsPerVector)
{
Vector128<sbyte> packed = Vector128.LoadUnsafe(ref sourceBase, (nuint)(i * componentsPerPixel)).AsSByte();
Vector512<int> integers = Avx512F.ConvertToVector512Int32(packed);
Unsafe.As<Vector4, Vector512<float>>(ref Unsafe.Add(ref destinationBase, (uint)i)) = ToVector4(Avx512F.ConvertToVector512Single(integers), scaled);
}
}
if (Avx2.IsSupported)
{
int pixelsPerVector = Vector256<float>.Count / Vector128<float>.Count;
for (; i <= source.Length - pixelsPerVector; i += pixelsPerVector)
{
ulong packed = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref sourceBase, (uint)(i * componentsPerPixel)));
Vector256<int> integers = Avx2.ConvertToVector256Int32(Vector128.CreateScalarUnsafe(packed).AsSByte());
Unsafe.As<Vector4, Vector256<float>>(ref Unsafe.Add(ref destinationBase, (uint)i)) = ToVector4(Avx.ConvertToVector256Single(integers), scaled);
}
}
if (Vector128.IsHardwareAccelerated)
{
for (; i < source.Length; i++)
{
uint packed = Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref sourceBase, (uint)(i * componentsPerPixel)));
Vector128<short> shorts = Vector128.WidenLower(Vector128.CreateScalarUnsafe(packed).AsSByte());
Vector128<int> integers = Vector128.WidenLower(shorts);
Unsafe.Add(ref destinationBase, (uint)i) = ToVector4(Vector128.ConvertToSingle(integers), scaled).AsVector4();
}
return;
}
ref NormalizedByte4P pixelBase = ref Unsafe.As<byte, NormalizedByte4P>(ref sourceBase);
for (; i < source.Length; i++)
{
NormalizedByte4P pixel = Unsafe.Add(ref pixelBase, (uint)i);
Unsafe.Add(ref destinationBase, (uint)i) = scaled ? pixel.ToScaledVector4() : pixel.ToVector4();
}
}
/// <summary>
/// Converts signed-normalized storage to native or scaled vectors.
/// </summary>
/// <param name="source">The signed storage components.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
/// <returns>The converted vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<float> ToVector4(Vector512<float> source, bool scaled)
{
if (scaled)
{
// Offset exact integer components before division to avoid cancellation near the signed-normalized lower bound.
return (source + Vector512.Create(MaxPos)) / Vector512.Create(ScaledMagnitude);
}
return source / Vector512.Create(MaxPos);
}
/// <summary>
/// Converts signed-normalized storage to native or scaled vectors.
/// </summary>
/// <param name="source">The signed storage components.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
/// <returns>The converted vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<float> ToVector4(Vector256<float> source, bool scaled)
{
if (scaled)
{
// Offset exact integer components before division to avoid cancellation near the signed-normalized lower bound.
return (source + Vector256.Create(MaxPos)) / Vector256.Create(ScaledMagnitude);
}
return source / Vector256.Create(MaxPos);
}
/// <summary>
/// Converts signed-normalized storage to native or scaled vectors.
/// </summary>
/// <param name="source">The signed storage components.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
/// <returns>The converted vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<float> ToVector4(Vector128<float> source, bool scaled)
{
if (scaled)
{
// Offset exact integer components before division to avoid cancellation near the signed-normalized lower bound.
return (source + Vector128.Create(MaxPos)) / Vector128.Create(ScaledMagnitude);
}
return source / Vector128.Create(MaxPos);
}
/// <summary>
/// Converts packed associated pixels directly to unassociated native or scaled vectors.
/// </summary>
/// <param name="source">The source pixels.</param>
/// <param name="destination">The destination vectors.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
private static void ToUnassociatedVector4(ReadOnlySpan<NormalizedByte4P> source, Span<Vector4> destination, bool scaled)
{
ref byte sourceBase = ref Unsafe.As<NormalizedByte4P, byte>(ref MemoryMarshal.GetReference(source));
ref Vector4 destinationBase = ref MemoryMarshal.GetReference(destination);
// Each packed component occupies one byte, so the destination Vector4 lane count also defines the source byte stride.
int componentsPerPixel = Vector128<float>.Count;
int i = 0;
if (Vector512.IsHardwareAccelerated && Avx512F.IsSupported)
{
int pixelsPerVector = Vector512<float>.Count / Vector128<float>.Count;
for (; i <= source.Length - pixelsPerVector; i += pixelsPerVector)
{
Vector128<sbyte> packed = Vector128.LoadUnsafe(ref sourceBase, (nuint)(i * componentsPerPixel)).AsSByte();
Vector512<int> integers = Avx512F.ConvertToVector512Int32(packed);
Unsafe.As<Vector4, Vector512<float>>(ref Unsafe.Add(ref destinationBase, (uint)i)) = ToUnassociatedVector4(Avx512F.ConvertToVector512Single(integers), scaled);
}
}
if (Avx2.IsSupported)
{
int pixelsPerVector = Vector256<float>.Count / Vector128<float>.Count;
for (; i <= source.Length - pixelsPerVector; i += pixelsPerVector)
{
ulong packed = Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref sourceBase, (uint)(i * componentsPerPixel)));
Vector256<int> integers = Avx2.ConvertToVector256Int32(Vector128.CreateScalarUnsafe(packed).AsSByte());
Unsafe.As<Vector4, Vector256<float>>(ref Unsafe.Add(ref destinationBase, (uint)i)) = ToUnassociatedVector4(Avx.ConvertToVector256Single(integers), scaled);
}
}
if (Vector128.IsHardwareAccelerated)
{
for (; i < source.Length; i++)
{
uint packed = Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref sourceBase, (uint)(i * componentsPerPixel)));
Vector128<short> shorts = Vector128.WidenLower(Vector128.CreateScalarUnsafe(packed).AsSByte());
Vector128<int> integers = Vector128.WidenLower(shorts);
Unsafe.Add(ref destinationBase, (uint)i) = ToUnassociatedVector4(Vector128.ConvertToSingle(integers), scaled).AsVector4();
}
return;
}
ref NormalizedByte4P pixelBase = ref Unsafe.As<byte, NormalizedByte4P>(ref sourceBase);
for (; i < source.Length; i++)
{
NormalizedByte4P pixel = Unsafe.Add(ref pixelBase, (uint)i);
Unsafe.Add(ref destinationBase, (uint)i) = scaled ? pixel.ToUnassociatedScaledVector4() : pixel.ToUnassociatedVector4();
}
}
/// <summary>
/// Converts four associated signed-byte vectors to unassociated native or scaled vectors.
/// </summary>
/// <param name="source">The signed storage components.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
/// <returns>The unassociated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<float> ToUnassociatedVector4(Vector512<float> source, bool scaled)
{
source += Vector512.Create(MaxPos);
Vector512<float> alpha = Vector512_.ShuffleNative(source, 0b_11_11_11_11);
Vector512<float> alphaMask = Vector512.Create(0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
Vector512<float> normalizeMask = Vector512.Equals(alpha, Vector512<float>.Zero) | alphaMask;
Vector512<float> divisor = Vector512.ConditionalSelect(normalizeMask, Vector512.Create(ScaledMagnitude), alpha);
Vector512<float> result = source / divisor;
// Unassociation is performed in scaled space; map the result back only after alpha has served as a true opacity value.
return scaled ? result : (result * Vector512.Create(2F)) - Vector512<float>.One;
}
/// <summary>
/// Converts two associated signed-byte vectors to unassociated native or scaled vectors.
/// </summary>
/// <param name="source">The signed storage components.</param>
/// <param name="scaled">Whether to produce scaled vectors.</param>
/// <returns>The unassociated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<float> ToUnassociatedVector4(Vector256<float> source, bool scaled)
{
source += Vector256.Create(MaxPos);
Vector256<float> alpha = Vector256_.ShuffleNative(source, 0b_11_11_11_11);
Vector256<float> alphaMask = Vector256.Create(0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
Vector256<float> normalizeMask = Vector256.Equals(alpha, Vector256<float>.Zero) | alphaMask;
Vector256<float> divisor = Vector256.ConditionalSelect(normalizeMask, Vector256.Create(ScaledMagnitude), alpha);
Vector256<float> result = source / divisor;
// Unassociation is performed in scaled space; map the result back only after alpha has served as a true opacity value.
return scaled ? result : (result * Vector256.Create(2F)) - Vector256<float>.One;
}
/// <summary>
/// Converts an associated signed-byte vector to an unassociated native or scaled vector.
/// </summary>
/// <param name="source">The signed storage components.</param>
/// <param name="scaled">Whether to produce a scaled vector.</param>
/// <returns>The unassociated vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<float> ToUnassociatedVector4(Vector128<float> source, bool scaled)
{
source += Vector128.Create(MaxPos);
Vector128<float> alpha = Vector128_.ShuffleNative(source, 0b_11_11_11_11);
Vector128<float> alphaMask = Vector128.Create(0, 0, 0, -1).AsSingle();
Vector128<float> normalizeMask = Vector128.Equals(alpha, Vector128<float>.Zero) | alphaMask;
Vector128<float> divisor = Vector128.ConditionalSelect(normalizeMask, Vector128.Create(ScaledMagnitude), alpha);
Vector128<float> result = source / divisor;
// Unassociation is performed in scaled space; map the result back only after alpha has served as a true opacity value.
return scaled ? result : (result * Vector128.Create(2F)) - Vector128<float>.One;
}
/// <summary>
/// Converts unassociated native or scaled vectors to the destination's associated scaled representation.
/// </summary>
/// <param name="source">The vectors to convert in place.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
private static void Associate(Span<Vector4> source, bool scaled)
{
// Quantize alpha through signed-normalized destination storage before associating RGB. The kernels replace W separately
// so alpha is stored unchanged instead of being multiplied by itself.
if (Vector512.IsHardwareAccelerated)
{
int pixelsPerVector = Vector512<float>.Count / Vector128<float>.Count;
ref Vector512<float> sourceBase = ref Unsafe.As<Vector4, Vector512<float>>(ref MemoryMarshal.GetReference(source));
ref Vector512<float> sourceEnd = ref Unsafe.Add(ref sourceBase, (uint)source.Length / (uint)pixelsPerVector);
while (Unsafe.IsAddressLessThan(ref sourceBase, ref sourceEnd))
{
sourceBase = Associate(sourceBase, scaled);
sourceBase = ref Unsafe.Add(ref sourceBase, 1);
}
// SIMD widths are powers of two, so masking finds the start of the unprocessed remainder without division.
source = source[(source.Length & ~(pixelsPerVector - 1))..];
}
if (Vector256.IsHardwareAccelerated)
{
int pixelsPerVector = Vector256<float>.Count / Vector128<float>.Count;
ref Vector256<float> sourceBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(source));
ref Vector256<float> sourceEnd = ref Unsafe.Add(ref sourceBase, (uint)source.Length / (uint)pixelsPerVector);
while (Unsafe.IsAddressLessThan(ref sourceBase, ref sourceEnd))
{
sourceBase = Associate(sourceBase, scaled);
sourceBase = ref Unsafe.Add(ref sourceBase, 1);
}
// SIMD widths are powers of two, so masking finds the start of the unprocessed remainder without division.
source = source[(source.Length & ~(pixelsPerVector - 1))..];
}
if (Vector128.IsHardwareAccelerated)
{
ref Vector128<float> sourceBase = ref Unsafe.As<Vector4, Vector128<float>>(ref MemoryMarshal.GetReference(source));
ref Vector128<float> sourceEnd = ref Unsafe.Add(ref sourceBase, (uint)source.Length);
while (Unsafe.IsAddressLessThan(ref sourceBase, ref sourceEnd))
{
sourceBase = Associate(sourceBase, scaled);
sourceBase = ref Unsafe.Add(ref sourceBase, 1);
}
return;
}
ref Vector4 tailBase = ref MemoryMarshal.GetReference(source);
for (nuint i = 0; i < (uint)source.Length; i++)
{
Unsafe.Add(ref destinationBase, i) = NormalizedByte4P.ToUnassociatedScaledVector4(Unsafe.Add(ref sourceBase, i));
Vector4 vector = Unsafe.Add(ref tailBase, i);
if (!scaled)
{
// Signed-native W is an affine encoding rather than opacity, so association must happen after mapping to scaled space.
vector += Vector4.One;
vector /= 2F;
}
// Qualify the containing pixel type because this nested class also declares SIMD helpers named Associate.
Unsafe.Add(ref tailBase, i) = NormalizedByte4P.Associate(vector);
}
}
/// <inheritdoc />
internal override void FromUnassociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4P> destination)
/// <summary>
/// Converts four unassociated native or scaled vectors to the destination's associated scaled representation.
/// </summary>
/// <param name="source">The unassociated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The associated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<float> Associate(Vector512<float> source, bool scaled)
{
source = source[..destination.Length];
Associate(source);
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
Vector512<float> zero = Vector512<float>.Zero;
Vector512<float> one = Vector512<float>.One;
if (!scaled)
{
// Signed-native W is an affine encoding rather than opacity, so association must happen after mapping to scaled space.
source = (source + one) / Vector512.Create(2F);
}
source = Vector512.Min(Vector512.Max(source, zero), one);
Vector512<float> alpha = Vector512_.ShuffleNative(source, 0b_11_11_11_11);
Vector512<float> nativeAlpha = Vector512.Min(Vector512.Max((alpha * Vector512.Create(2F)) - one, -one), one);
Vector512<float> storedAlpha = Vector512_.RoundToNearestInteger(nativeAlpha * Vector512.Create(MaxPos));
storedAlpha += Vector512.Create(MaxPos);
storedAlpha /= Vector512.Create(ScaledMagnitude);
Vector512<float> result = source * storedAlpha;
Vector512<float> alphaMask = Vector512.Create(0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
return Vector512.ConditionalSelect(alphaMask, storedAlpha, result);
}
/// <inheritdoc />
public override void FromAssociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<NormalizedByte4P> destination)
/// <summary>
/// Converts two unassociated native or scaled vectors to the destination's associated scaled representation.
/// </summary>
/// <param name="source">The unassociated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The associated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<float> Associate(Vector256<float> source, bool scaled)
{
Vector256<float> zero = Vector256<float>.Zero;
Vector256<float> one = Vector256<float>.One;
if (!scaled)
{
// Signed-native W is an affine encoding rather than opacity, so association must happen after mapping to scaled space.
source = (source + one) / Vector256.Create(2F);
}
source = Vector256.Min(Vector256.Max(source, zero), one);
Vector256<float> alpha = Vector256_.ShuffleNative(source, 0b_11_11_11_11);
Vector256<float> nativeAlpha = Vector256.Min(Vector256.Max((alpha * Vector256.Create(2F)) - one, -one), one);
Vector256<float> storedAlpha = Vector256_.RoundToNearestInteger(nativeAlpha * Vector256.Create(MaxPos));
storedAlpha += Vector256.Create(MaxPos);
storedAlpha /= Vector256.Create(ScaledMagnitude);
Vector256<float> result = source * storedAlpha;
Vector256<float> alphaMask = Vector256.Create(0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
return Vector256.ConditionalSelect(alphaMask, storedAlpha, result);
}
/// <summary>
/// Converts an unassociated native or scaled vector to the destination's associated scaled representation.
/// </summary>
/// <param name="source">The unassociated vector.</param>
/// <param name="scaled">Whether the source contains a scaled vector.</param>
/// <returns>The associated vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<float> Associate(Vector128<float> source, bool scaled)
{
Vector128<float> zero = Vector128<float>.Zero;
Vector128<float> one = Vector128<float>.One;
if (!scaled)
{
// Signed-native W is an affine encoding rather than opacity, so association must happen after mapping to scaled space.
source = (source + one) / Vector128.Create(2F);
}
source = Vector128.Min(Vector128.Max(source, zero), one);
Vector128<float> alpha = Vector128_.ShuffleNative(source, 0b_11_11_11_11);
Vector128<float> nativeAlpha = Vector128.Min(Vector128.Max((alpha * Vector128.Create(2F)) - one, -one), one);
Vector128<float> storedAlpha = Vector128_.RoundToNearestInteger(nativeAlpha * Vector128.Create(MaxPos));
storedAlpha += Vector128.Create(MaxPos);
storedAlpha /= Vector128.Create(ScaledMagnitude);
Vector128<float> result = source * storedAlpha;
Vector128<float> alphaMask = Vector128.Create(0, 0, 0, -1).AsSingle();
return Vector128.ConditionalSelect(alphaMask, storedAlpha, result);
}
/// <summary>
/// Reassociates native or scaled vectors with the scaled alpha values the destination stores.
/// </summary>
/// <param name="source">The vectors to convert in place.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
private static void Reassociate(Span<Vector4> source, bool scaled)
{
// Preserve straight color across destination-alpha quantization by scaling RGB by storedAlpha / inputAlpha. The kernels
// replace W, clamp RGB to stored alpha, and clear zero-alpha vectors so the stored result remains valid associated color.
if (Vector512.IsHardwareAccelerated)
{
int pixelsPerVector = Vector512<float>.Count / Vector128<float>.Count;
ref Vector512<float> sourceBase = ref Unsafe.As<Vector4, Vector512<float>>(ref MemoryMarshal.GetReference(source));
ref Vector512<float> sourceEnd = ref Unsafe.Add(ref sourceBase, (uint)source.Length / (uint)pixelsPerVector);
while (Unsafe.IsAddressLessThan(ref sourceBase, ref sourceEnd))
{
sourceBase = Reassociate(sourceBase, scaled);
sourceBase = ref Unsafe.Add(ref sourceBase, 1);
}
// SIMD widths are powers of two, so masking finds the start of the unprocessed remainder without division.
source = source[(source.Length & ~(pixelsPerVector - 1))..];
}
if (Vector256.IsHardwareAccelerated)
{
int pixelsPerVector = Vector256<float>.Count / Vector128<float>.Count;
ref Vector256<float> sourceBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(source));
ref Vector256<float> sourceEnd = ref Unsafe.Add(ref sourceBase, (uint)source.Length / (uint)pixelsPerVector);
while (Unsafe.IsAddressLessThan(ref sourceBase, ref sourceEnd))
{
sourceBase = Reassociate(sourceBase, scaled);
sourceBase = ref Unsafe.Add(ref sourceBase, 1);
}
// SIMD widths are powers of two, so masking finds the start of the unprocessed remainder without division.
source = source[(source.Length & ~(pixelsPerVector - 1))..];
}
if (Vector128.IsHardwareAccelerated)
{
ref Vector128<float> sourceBase = ref Unsafe.As<Vector4, Vector128<float>>(ref MemoryMarshal.GetReference(source));
ref Vector128<float> sourceEnd = ref Unsafe.Add(ref sourceBase, (uint)source.Length);
while (Unsafe.IsAddressLessThan(ref sourceBase, ref sourceEnd))
{
sourceBase = Reassociate(sourceBase, scaled);
sourceBase = ref Unsafe.Add(ref sourceBase, 1);
}
return;
}
ref Vector4 tailBase = ref MemoryMarshal.GetReference(source);
for (nuint i = 0; i < (uint)source.Length; i++)
{
Vector4 vector = Unsafe.Add(ref tailBase, i);
if (!scaled)
{
// Convert native components together so RGB and alpha enter reassociation in the same scaled coordinate system.
vector += Vector4.One;
vector /= 2F;
}
// Qualify the containing pixel type because this nested class also declares SIMD helpers named Reassociate.
Unsafe.Add(ref tailBase, i) = NormalizedByte4P.Reassociate(vector);
}
}
/// <summary>
/// Reassociates four native or scaled vectors with the scaled alpha values the destination stores.
/// </summary>
/// <param name="source">The associated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The reassociated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<float> Reassociate(Vector512<float> source, bool scaled)
{
source = source[..destination.Length];
Reassociate(source);
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
Vector512<float> zero = Vector512<float>.Zero;
Vector512<float> one = Vector512<float>.One;
if (!scaled)
{
// Convert native components together so RGB and alpha enter reassociation in the same scaled coordinate system.
source = (source + one) / Vector512.Create(2F);
}
Vector512<float> alpha = Vector512_.ShuffleNative(source, 0b_11_11_11_11);
Vector512<float> nativeAlpha = Vector512.Min(Vector512.Max((alpha * Vector512.Create(2F)) - one, -one), one);
Vector512<float> storedAlpha = Vector512_.RoundToNearestInteger(nativeAlpha * Vector512.Create(MaxPos));
storedAlpha += Vector512.Create(MaxPos);
storedAlpha /= Vector512.Create(ScaledMagnitude);
Vector512<float> result = source * (storedAlpha / alpha);
Vector512<float> alphaMask = Vector512.Create(0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
result = Vector512.ConditionalSelect(alphaMask, storedAlpha, result);
result = Vector512.Min(Vector512.Max(result, zero), storedAlpha);
return Vector512.ConditionalSelect(Vector512.LessThanOrEqual(alpha, zero), zero, result);
}
/// <summary>
/// Reassociates two native or scaled vectors with the scaled alpha values the destination stores.
/// </summary>
/// <param name="source">The associated vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The reassociated vectors.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<float> Reassociate(Vector256<float> source, bool scaled)
{
Vector256<float> zero = Vector256<float>.Zero;
Vector256<float> one = Vector256<float>.One;
if (!scaled)
{
// Convert native components together so RGB and alpha enter reassociation in the same scaled coordinate system.
source = (source + one) / Vector256.Create(2F);
}
Vector256<float> alpha = Vector256_.ShuffleNative(source, 0b_11_11_11_11);
Vector256<float> nativeAlpha = Vector256.Min(Vector256.Max((alpha * Vector256.Create(2F)) - one, -one), one);
Vector256<float> storedAlpha = Vector256_.RoundToNearestInteger(nativeAlpha * Vector256.Create(MaxPos));
storedAlpha += Vector256.Create(MaxPos);
storedAlpha /= Vector256.Create(ScaledMagnitude);
Vector256<float> result = source * (storedAlpha / alpha);
Vector256<float> alphaMask = Vector256.Create(0, 0, 0, -1, 0, 0, 0, -1).AsSingle();
result = Vector256.ConditionalSelect(alphaMask, storedAlpha, result);
result = Vector256.Min(Vector256.Max(result, zero), storedAlpha);
return Vector256.ConditionalSelect(Vector256.LessThanOrEqual(alpha, zero), zero, result);
}
/// <summary>
/// Reassociates a native or scaled vector with the scaled alpha value the destination stores.
/// </summary>
/// <param name="source">The associated vector.</param>
/// <param name="scaled">Whether the source contains a scaled vector.</param>
/// <returns>The reassociated vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<float> Reassociate(Vector128<float> source, bool scaled)
{
Vector128<float> zero = Vector128<float>.Zero;
Vector128<float> one = Vector128<float>.One;
if (!scaled)
{
// Convert native components together so RGB and alpha enter reassociation in the same scaled coordinate system.
source = (source + one) / Vector128.Create(2F);
}
Vector128<float> alpha = Vector128_.ShuffleNative(source, 0b_11_11_11_11);
Vector128<float> nativeAlpha = Vector128.Min(Vector128.Max((alpha * Vector128.Create(2F)) - one, -one), one);
Vector128<float> storedAlpha = Vector128_.RoundToNearestInteger(nativeAlpha * Vector128.Create(MaxPos));
storedAlpha += Vector128.Create(MaxPos);
storedAlpha /= Vector128.Create(ScaledMagnitude);
Vector128<float> result = source * (storedAlpha / alpha);
Vector128<float> alphaMask = Vector128.Create(0, 0, 0, -1).AsSingle();
result = Vector128.ConditionalSelect(alphaMask, storedAlpha, result);
result = Vector128.Min(Vector128.Max(result, zero), storedAlpha);
return Vector128.ConditionalSelect(Vector128.LessThanOrEqual(alpha, zero), zero, result);
}
/// <summary>
/// Packs native or scaled vectors into signed-normalized storage.
/// </summary>
/// <param name="source">The source vectors.</param>
/// <param name="destination">The destination pixels.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
internal static void Pack(Span<Vector4> source, Span<NormalizedByte4P> destination, bool scaled)
{
ref Vector4 sourceBase = ref MemoryMarshal.GetReference(source);
ref NormalizedByte4P destinationBase = ref MemoryMarshal.GetReference(destination);
int i = 0;
if (Vector512.IsHardwareAccelerated && Avx512F.IsSupported)
{
int pixelsPerVector = Vector512<float>.Count / Vector128<float>.Count;
for (; i <= source.Length - pixelsPerVector; i += pixelsPerVector)
{
Vector512<float> vector = Unsafe.As<Vector4, Vector512<float>>(ref Unsafe.Add(ref sourceBase, (uint)i));
Vector128<sbyte> packed = Avx512F.ConvertToVector128SByteWithSaturation(ConvertToPackedInt32(vector, scaled));
Unsafe.As<NormalizedByte4P, Vector128<sbyte>>(ref Unsafe.Add(ref destinationBase, (uint)i)) = packed;
}
}
if (Vector256.IsHardwareAccelerated)
{
int pixelsPerVector = Vector256<float>.Count / Vector128<float>.Count;
for (; i <= source.Length - pixelsPerVector; i += pixelsPerVector)
{
Vector256<float> vector = Unsafe.As<Vector4, Vector256<float>>(ref Unsafe.Add(ref sourceBase, (uint)i));
Vector256<int> integers = ConvertToPackedInt32(vector, scaled);
Vector128<short> shorts = Vector128_.PackSignedSaturate(integers.GetLower(), integers.GetUpper());
Vector128<sbyte> packed = Vector128_.PackSignedSaturate(shorts, shorts);
Unsafe.As<NormalizedByte4P, ulong>(ref Unsafe.Add(ref destinationBase, (uint)i)) = packed.AsUInt64().GetElement(0);
}
}
if (Vector128.IsHardwareAccelerated)
{
for (; i < source.Length; i++)
{
Vector128<float> vector = Unsafe.As<Vector4, Vector128<float>>(ref Unsafe.Add(ref sourceBase, (uint)i));
Vector128<int> integers = ConvertToPackedInt32(vector, scaled);
Vector128<short> shorts = Vector128_.PackSignedSaturate(integers, integers);
Vector128<sbyte> packed = Vector128_.PackSignedSaturate(shorts, shorts);
Unsafe.Add(ref destinationBase, (uint)i).PackedValue = packed.AsUInt32().GetElement(0);
}
return;
}
for (; i < source.Length; i++)
{
Vector4 vector = Unsafe.Add(ref sourceBase, (uint)i);
Unsafe.Add(ref destinationBase, (uint)i) = scaled ? FromScaledVector4(vector) : FromVector4(vector);
}
}
/// <summary>
/// Converts four vectors to signed integers using the destination's packing contract.
/// </summary>
/// <param name="source">The source vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The packed integer components.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector512<int> ConvertToPackedInt32(Vector512<float> source, bool scaled)
{
Vector512<float> one = Vector512<float>.One;
if (scaled)
{
source *= Vector512.Create(2F);
source -= one;
}
source = Vector512.Min(Vector512.Max(source, -one), one) * Vector512.Create(MaxPos);
return Vector512_.ConvertToInt32RoundToEven(source);
}
/// <summary>
/// Converts two vectors to signed integers using the destination's packing contract.
/// </summary>
/// <param name="source">The source vectors.</param>
/// <param name="scaled">Whether the source contains scaled vectors.</param>
/// <returns>The packed integer components.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector256<int> ConvertToPackedInt32(Vector256<float> source, bool scaled)
{
Vector256<float> one = Vector256<float>.One;
if (scaled)
{
source *= Vector256.Create(2F);
source -= one;
}
source = Vector256.Min(Vector256.Max(source, -one), one) * Vector256.Create(MaxPos);
return Vector256_.ConvertToInt32RoundToEven(source);
}
/// <summary>
/// Converts a vector to signed integers using the destination's packing contract.
/// </summary>
/// <param name="source">The source vector.</param>
/// <param name="scaled">Whether the source contains a scaled vector.</param>
/// <returns>The packed integer components.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<int> ConvertToPackedInt32(Vector128<float> source, bool scaled)
{
Vector128<float> one = Vector128<float>.One;
if (scaled)
{
source *= Vector128.Create(2F);
source -= one;
}
source = Vector128.Min(Vector128.Max(source, -one), one) * Vector128.Create(MaxPos);
return Vector128_.ConvertToInt32RoundToEven(source);
}
}
}

25
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedShort2.PixelOperations.cs

@ -1,6 +1,9 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +14,25 @@ public partial struct NormalizedShort2
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<NormalizedShort2>;
internal class PixelOperations : PixelOperations<NormalizedShort2>
{
private static readonly Vector4 NativeOffset = new(1F, 1F, 0F, 0F);
private static readonly Vector4 NativeDivisor = new(2F, 2F, 1F, 1F);
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<NormalizedShort2> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<NormalizedShort2> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedShort2> destination)
{
// Only X and Y use affine native coordinates. Preserve W as normalized alpha for the scaled unassociation step.
Vector4Converters.AddThenDivide(source, NativeOffset, NativeDivisor);
this.FromAssociatedScaledVector4Destructive(configuration, source, destination);
}
}
}

90
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/NormalizedShort4.PixelOperations.cs

@ -1,6 +1,10 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +15,89 @@ public partial struct NormalizedShort4
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<NormalizedShort4>;
internal class PixelOperations : PixelOperations<NormalizedShort4>
{
private static readonly Vector4 NativeScale = new(2F);
/// <inheritdoc />
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<NormalizedShort4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
SignedShort4PixelOperations.ToVector4(MemoryMarshal.Cast<NormalizedShort4, short>(source), destination[..source.Length], true, false);
}
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<NormalizedShort4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// Association is defined in scaled color space, not around the signed-native zero point. The shared signed-short SIMD
// kernel handles unpacking before the result is mapped back to the format's native [-1, 1] range.
SignedShort4PixelOperations.ToVector4(MemoryMarshal.Cast<NormalizedShort4, short>(source), destination, true, true);
Numerics.Premultiply(destination);
Vector4Converters.MultiplyThenAdd(destination, NativeScale, -Vector4.One);
}
/// <inheritdoc />
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<NormalizedShort4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
SignedShort4PixelOperations.ToVector4(MemoryMarshal.Cast<NormalizedShort4, short>(source), destination[..source.Length], true, true);
}
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<NormalizedShort4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
SignedShort4PixelOperations.ToVector4(MemoryMarshal.Cast<NormalizedShort4, short>(source), destination, true, true);
Numerics.Premultiply(destination);
}
/// <inheritdoc />
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedShort4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
SignedShort4PixelOperations.FromVector4(source, MemoryMarshal.Cast<NormalizedShort4, short>(destination), true, false);
}
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedShort4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// Convert native RGB and alpha together before unassociation so the subsequent scaled pack retains the scalar contract.
Vector4Converters.AddThenDivide(source, Vector4.One, NativeScale);
Numerics.UnPremultiply(source);
SignedShort4PixelOperations.FromVector4(source, MemoryMarshal.Cast<NormalizedShort4, short>(destination), true, true);
}
/// <inheritdoc />
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedShort4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
SignedShort4PixelOperations.FromVector4(source, MemoryMarshal.Cast<NormalizedShort4, short>(destination), true, true);
}
/// <inheritdoc />
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<NormalizedShort4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
Numerics.UnPremultiply(source);
SignedShort4PixelOperations.FromVector4(source, MemoryMarshal.Cast<NormalizedShort4, short>(destination), true, true);
}
}
}

13
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Rg32.PixelOperations.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +13,14 @@ public partial struct Rg32
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<Rg32>;
internal class PixelOperations : PixelOperations<Rg32>
{
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Rg32> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Rg32> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
}
}

13
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Rgb48.PixelOperations.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +13,14 @@ public partial struct Rgb48
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal partial class PixelOperations : PixelOperations<Rgb48>;
internal partial class PixelOperations : PixelOperations<Rgb48>
{
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Rgb48> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Rgb48> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
}
}

26
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Rgb96.PixelOperations.cs

@ -0,0 +1,26 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
/// The alpha component is always fully opaque.
/// </content>
public partial struct Rgb96
{
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<Rgb96>
{
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Rgb96> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Rgb96> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
}
}

54
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Rgba32P.PixelOperations.cs

@ -17,50 +17,48 @@ public partial struct Rgba32P
internal class PixelOperations : AssociatedAlphaPixelOperations<Rgba32P>
{
/// <inheritdoc />
internal override Vector4 ToUnassociatedScaledVector4(Rgba32P source)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source.R, source.G, source.B, source.A);
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<Rgba32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source, destination);
/// <inheritdoc />
internal override Rgba32P FromUnassociatedScaledVector4(Vector4 source)
=> FromScaledVector4(Vector4Converters.AssociatedRgbaCompatible.Associate(source));
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Rgba32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToAssociatedVector4(source, destination);
/// <inheritdoc />
public override Rgba32P FromAssociatedScaledVector4(Vector4 source)
=> Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4ToRgba32P(source);
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Rgba32P> destination)
{
// Native and scaled vectors have the same range for this format, so the byte-specialized converter is valid for both contracts.
Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4(source, destination);
}
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Rgba32P> destination)
{
// The converter rescales RGB when alpha rounds so the channels remain associated with the byte alpha actually stored.
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
}
/// <inheritdoc />
internal override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Rgba32P> source, Span<Vector4> destination)
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Rgba32P> source, Span<Vector4> destination)
=> Vector4Converters.AssociatedRgbaCompatible.ToUnassociatedVector4(source, destination);
/// <inheritdoc />
internal override void FromUnassociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<Rgba32P> destination)
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Rgba32P> source, Span<Vector4> destination)
{
source = source[..destination.Length];
Vector4Converters.AssociatedRgbaCompatible.Associate(source);
this.FromVector4Destructive(configuration, source, destination, PixelConversionModifiers.Scale);
// This byte format has the same normalized native and scaled ranges, so the unmodified converter satisfies both contracts.
Vector4Converters.AssociatedRgbaCompatible.ToAssociatedVector4(source, destination);
}
/// <inheritdoc />
public override void FromAssociatedScaledVector4(Configuration configuration, Span<Vector4> source, Span<Rgba32P> destination)
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Rgba32P> destination)
{
source = source[..destination.Length];
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
Vector4Converters.AssociatedRgbaCompatible.FromUnassociatedVector4(source, destination);
}
/// <inheritdoc />
public override void ToVector4(
Configuration configuration,
ReadOnlySpan<Rgba32P> source,
Span<Vector4> destinationVectors,
PixelConversionModifiers modifiers)
=> Vector4Converters.AssociatedRgbaCompatible.ToVector4(source, destinationVectors, modifiers);
/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Span<Vector4> sourceVectors,
Span<Rgba32P> destination,
PixelConversionModifiers modifiers)
=> Vector4Converters.AssociatedRgbaCompatible.FromVector4(sourceVectors, destination, modifiers);
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Rgba32P> destination)
{
Vector4Converters.AssociatedRgbaCompatible.FromAssociatedVector4(source, destination);
}
}
}

13
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/RgbaVector.PixelOperations.cs

@ -23,9 +23,13 @@ public partial struct RgbaVector
ReadOnlySpan<TSourcePixel> sourcePixels,
Span<RgbaVector> destinationPixels)
{
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels));
destinationPixels = destinationPixels[..sourcePixels.Length];
Span<Vector4> destinationVectors = MemoryMarshal.Cast<RgbaVector, Vector4>(destinationPixels);
PixelOperations<TSourcePixel>.Instance.ToUnassociatedScaledVector4(configuration, sourcePixels, destinationVectors);
// Cross-format conversion uses public dispatch so associated source pixels are unassociated before entering RgbaVector storage.
PixelOperations<TSourcePixel>.Instance.ToVector4(configuration, sourcePixels, destinationVectors, PixelConversionModifiers.Scale | PixelConversionModifiers.UnPremultiply);
// RgbaVector.FromScaledVector4 clamps scaled input, so the optimized bulk path must preserve that behavior after unassociating.
Numerics.Clamp(MemoryMarshal.Cast<Vector4, float>(destinationVectors), 0F, 1F);
@ -41,7 +45,11 @@ public partial struct RgbaVector
Guard.DestinationShouldNotBeTooShort(sourceVectors, destinationPixels, nameof(destinationPixels));
Vector4Converters.ApplyBackwardConversionModifiers(sourceVectors, modifiers);
MemoryMarshal.Cast<Vector4, RgbaVector>(sourceVectors).CopyTo(destinationPixels);
// RgbaVector.FromVector4 and FromScaledVector4 both clamp to the representable [0, 1] range. Preserve that scalar
// contract before the zero-copy representation cast so bulk processor output cannot retain HDR or negative values.
Numerics.Clamp(MemoryMarshal.Cast<Vector4, float>(sourceVectors), 0F, 1F);
MemoryMarshal.Cast<Vector4, RgbaVector>(sourceVectors).CopyTo(destinationPixels[..sourceVectors.Length]);
}
/// <inheritdoc />
@ -53,6 +61,7 @@ public partial struct RgbaVector
{
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationVectors, nameof(destinationVectors));
destinationVectors = destinationVectors[..sourcePixels.Length];
MemoryMarshal.Cast<RgbaVector, Vector4>(sourcePixels).CopyTo(destinationVectors);
Vector4Converters.ApplyForwardConversionModifiers(destinationVectors, modifiers);
}

26
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Short2.PixelOperations.cs

@ -1,6 +1,9 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +14,26 @@ public partial struct Short2
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<Short2>;
internal class PixelOperations : PixelOperations<Short2>
{
private static readonly Vector4 NativeOffset = new(MaxPos, MaxPos, 0F, 0F);
private static readonly Vector4 NativeDivisor = new(MaxPos * 2F, MaxPos * 2F, 1F, 1F);
// Alpha is implicitly one, so both outward representations already contain associated color components.
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Short2> source, Span<Vector4> destination) => this.ToUnassociatedVector4(configuration, source, destination);
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Short2> source, Span<Vector4> destination) => this.ToUnassociatedScaledVector4(configuration, source, destination);
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Short2> destination)
{
// X and Y use signed-native coordinates while incoming W remains normalized alpha. Map only the stored components before
// the scaled bulk path unassociates RGB; transforming W would change the opacity that must be removed.
Vector4Converters.AddThenDivide(source, NativeOffset, NativeDivisor);
this.FromAssociatedScaledVector4Destructive(configuration, source, destination);
}
}
}

91
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Short4.PixelOperations.cs

@ -1,6 +1,10 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.PixelFormats.Utils;
namespace SixLabors.ImageSharp.PixelFormats;
/// <content>
@ -11,5 +15,90 @@ public partial struct Short4
/// <summary>
/// Provides optimized overrides for bulk operations.
/// </summary>
internal class PixelOperations : PixelOperations<Short4>;
internal class PixelOperations : PixelOperations<Short4>
{
private static readonly Vector4 NativeOffset = new(MaxPos);
private static readonly Vector4 NativeMagnitude = new(MaxPos * 2F);
/// <inheritdoc />
protected override void ToUnassociatedVector4(Configuration configuration, ReadOnlySpan<Short4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
SignedShort4PixelOperations.ToVector4(MemoryMarshal.Cast<Short4, short>(source), destination[..source.Length], false, false);
}
/// <inheritdoc />
protected override void ToAssociatedVector4(Configuration configuration, ReadOnlySpan<Short4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// Signed integer components use an affine native range. Expand to scaled vectors, associate there, then restore the
// native zero point so RGB and alpha remain in the same coordinate system.
SignedShort4PixelOperations.ToVector4(MemoryMarshal.Cast<Short4, short>(source), destination, false, true);
Numerics.Premultiply(destination);
Vector4Converters.MultiplyThenAdd(destination, NativeMagnitude, -NativeOffset);
}
/// <inheritdoc />
protected override void ToUnassociatedScaledVector4(Configuration configuration, ReadOnlySpan<Short4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
SignedShort4PixelOperations.ToVector4(MemoryMarshal.Cast<Short4, short>(source), destination[..source.Length], false, true);
}
/// <inheritdoc />
protected override void ToAssociatedScaledVector4(Configuration configuration, ReadOnlySpan<Short4> source, Span<Vector4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
SignedShort4PixelOperations.ToVector4(MemoryMarshal.Cast<Short4, short>(source), destination, false, true);
Numerics.Premultiply(destination);
}
/// <inheritdoc />
protected override void FromUnassociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Short4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
SignedShort4PixelOperations.FromVector4(source, MemoryMarshal.Cast<Short4, short>(destination), false, false);
}
/// <inheritdoc />
protected override void FromAssociatedVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Short4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
// Normalize all four components before unassociating, then use the scaled packing path to preserve Short4's affine mapping.
Vector4Converters.AddThenDivide(source, NativeOffset, NativeMagnitude);
Numerics.UnPremultiply(source);
SignedShort4PixelOperations.FromVector4(source, MemoryMarshal.Cast<Short4, short>(destination), false, true);
}
/// <inheritdoc />
protected override void FromUnassociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Short4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
SignedShort4PixelOperations.FromVector4(source, MemoryMarshal.Cast<Short4, short>(destination), false, true);
}
/// <inheritdoc />
protected override void FromAssociatedScaledVector4Destructive(Configuration configuration, Span<Vector4> source, Span<Short4> destination)
{
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
destination = destination[..source.Length];
Numerics.UnPremultiply(source);
SignedShort4PixelOperations.FromVector4(source, MemoryMarshal.Cast<Short4, short>(destination), false, true);
}
}
}

42
src/ImageSharp/PixelFormats/PixelImplementations/Rg32.cs

@ -84,6 +84,48 @@ public partial struct Rg32 : IPixel<Rg32>, IPackedVector<uint>
/// <inheritdoc />
public static PixelOperations<Rg32> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rg32 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rg32 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rg32 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rg32 FromAssociatedVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rg32 FromScaledVector4(Vector4 source) => FromVector4(source);

42
src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs

@ -108,6 +108,48 @@ public partial struct Rgb24 : IPixel<Rgb24>
/// <inheritdoc/>
public static PixelOperations<Rgb24> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb24 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb24 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb24 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb24 FromAssociatedVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb24 FromScaledVector4(Vector4 source) => FromVector4(source);

42
src/ImageSharp/PixelFormats/PixelImplementations/Rgb48.cs

@ -91,6 +91,48 @@ public partial struct Rgb48 : IPixel<Rgb48>
/// <inheritdoc />
public static PixelOperations<Rgb48> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedScaledVector4() => this.ToScaledVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToUnassociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToAssociatedVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb48 FromUnassociatedScaledVector4(Vector4 source) => FromScaledVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb48 FromAssociatedScaledVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromScaledVector4(source);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb48 FromUnassociatedVector4(Vector4 source) => FromVector4(source);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb48 FromAssociatedVector4(Vector4 source)
{
// The destination has implicit alpha one, but associated input must be restored before its alpha is discarded.
Numerics.UnPremultiply(ref source);
return FromVector4(source);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rgb48 FromScaledVector4(Vector4 source) => FromVector4(source);

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save