Browse Source

Update library to use new pixel API

pull/2645/head
James Jackson-South 2 years ago
parent
commit
dfd983f17c
  1. 5
      src/ImageSharp.ruleset
  2. 15
      src/ImageSharp/Color/Color.cs
  3. 210
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  4. 10
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  5. 7
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  6. 9
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  7. 119
      src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
  8. 14
      src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs
  9. 100
      src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
  10. 10
      src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
  11. 140
      src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs
  12. 10
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs
  13. 6
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs
  14. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs
  15. 11
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero32FloatTiffColor{TPixel}.cs
  16. 13
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero32TiffColor{TPixel}.cs
  17. 25
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor{TPixel}.cs
  18. 13
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor{TPixel}.cs
  19. 11
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLabPlanarTiffColor{TPixel}.cs
  20. 10
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLabTiffColor{TPixel}.cs
  21. 8
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CmykTiffColor{TPixel}.cs
  22. 13
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor{TPixel}.cs
  23. 15
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs
  24. 22
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb16PlanarTiffColor{TPixel}.cs
  25. 20
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb242424TiffColor{TPixel}.cs
  26. 20
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb24PlanarTiffColor{TPixel}.cs
  27. 20
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb323232TiffColor{TPixel}.cs
  28. 21
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb32PlanarTiffColor{TPixel}.cs
  29. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb444TiffColor{TPixel}.cs
  30. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbFloat323232TiffColor{TPixel}.cs
  31. 13
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs
  32. 8
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor{TPixel}.cs
  33. 23
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba16161616TiffColor{TPixel}.cs
  34. 34
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba16PlanarTiffColor{TPixel}.cs
  35. 33
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24242424TiffColor{TPixel}.cs
  36. 32
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24PlanarTiffColor{TPixel}.cs
  37. 33
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba32323232TiffColor{TPixel}.cs
  38. 34
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba32PlanarTiffColor{TPixel}.cs
  39. 5
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba8888TiffColor{TPixel}.cs
  40. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaFloat32323232TiffColor{TPixel}.cs
  41. 18
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaPlanarTiffColor{TPixel}.cs
  42. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaTiffColor{TPixel}.cs
  43. 14
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero16TiffColor{TPixel}.cs
  44. 7
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor{TPixel}.cs
  45. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero24TiffColor{TPixel}.cs
  46. 11
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero32FloatTiffColor{TPixel}.cs
  47. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero32TiffColor{TPixel}.cs
  48. 25
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor{TPixel}.cs
  49. 8
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor{TPixel}.cs
  50. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor{TPixel}.cs
  51. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/YCbCrPlanarTiffColor{TPixel}.cs
  52. 11
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/YCbCrTiffColor{TPixel}.cs
  53. 6
      src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
  54. 120
      src/ImageSharp/Formats/Tiff/Utils/TiffUtilities.cs
  55. 176
      src/ImageSharp/Formats/Tiff/Utils/TiffUtils.cs
  56. 4
      src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs
  57. 40
      src/ImageSharp/PixelFormats/IPixel.cs
  58. 432
      src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs
  59. 4
      src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.tt
  60. 432
      src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.cs
  61. 4
      src/ImageSharp/PixelFormats/PixelBlenders/PorterDuffFunctions.Generated.tt
  62. 8
      src/ImageSharp/PixelFormats/PixelConversionModifiers.cs
  63. 12
      src/ImageSharp/PixelFormats/PixelImplementations/A8.cs
  64. 20
      src/ImageSharp/PixelFormats/PixelImplementations/Abgr32.cs
  65. 41
      src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs
  66. 43
      src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs
  67. 55
      src/ImageSharp/PixelFormats/PixelImplementations/Bgr565.cs
  68. 10
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs
  69. 70
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra4444.cs
  70. 72
      src/ImageSharp/PixelFormats/PixelImplementations/Bgra5551.cs
  71. 80
      src/ImageSharp/PixelFormats/PixelImplementations/Byte4.cs
  72. 70
      src/ImageSharp/PixelFormats/PixelImplementations/HalfSingle.cs
  73. 56
      src/ImageSharp/PixelFormats/PixelImplementations/HalfVector2.cs
  74. 74
      src/ImageSharp/PixelFormats/PixelImplementations/HalfVector4.cs
  75. 26
      src/ImageSharp/PixelFormats/PixelImplementations/L16.cs
  76. 29
      src/ImageSharp/PixelFormats/PixelImplementations/L8.cs
  77. 34
      src/ImageSharp/PixelFormats/PixelImplementations/La16.cs
  78. 40
      src/ImageSharp/PixelFormats/PixelImplementations/La32.cs
  79. 70
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte2.cs
  80. 70
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedByte4.cs
  81. 70
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedShort2.cs
  82. 70
      src/ImageSharp/PixelFormats/PixelImplementations/NormalizedShort4.cs
  83. 2
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/A8.PixelOperations.cs
  84. 2
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Abgr32.PixelOperations.cs
  85. 2
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Argb32.PixelOperations.cs
  86. 2
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgr24.PixelOperations.cs
  87. 2
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgr565.PixelOperations.cs
  88. 2
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgra32.PixelOperations.cs
  89. 2
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgra4444.PixelOperations.cs
  90. 2
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgra5551.PixelOperations.cs
  91. 2
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Byte4.PixelOperations.cs
  92. 267
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Abgr32.PixelOperations.Generated.cs
  93. 267
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Argb32.PixelOperations.Generated.cs
  94. 267
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Bgr24.PixelOperations.Generated.cs
  95. 267
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Bgra32.PixelOperations.Generated.cs
  96. 222
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Bgra5551.PixelOperations.Generated.cs
  97. 222
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/L16.PixelOperations.Generated.cs
  98. 222
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/L8.PixelOperations.Generated.cs
  99. 222
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/La16.PixelOperations.Generated.cs
  100. 222
      src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/La32.PixelOperations.Generated.cs

5
src/ImageSharp.ruleset

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="ImageSharp" ToolsVersion="17.0"> <RuleSet Name="ImageSharp" ToolsVersion="17.0">
<Include Path="..\shared-infrastructure\sixlabors.ruleset" Action="Default" /> <Include Path="..\shared-infrastructure\sixlabors.ruleset" Action="Default" />
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers"> <Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
<Rule Id="IDE0290" Action="None" />
</Rules> </Rules>
</RuleSet> </RuleSet>

15
src/ImageSharp/Color/Color.cs

@ -248,15 +248,12 @@ public readonly partial struct Color : IEquatable<Color>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public string ToHex() public string ToHex()
{ {
Rgba32 rgba = default;
if (this.boxedHighPrecisionPixel is not null) if (this.boxedHighPrecisionPixel is not null)
{ {
this.boxedHighPrecisionPixel.ToRgba32(ref rgba); return this.boxedHighPrecisionPixel.ToRgba32().ToHex();
return rgba.ToHex();
} }
rgba.FromScaledVector4(this.data); return Rgba32.FromScaledVector4(this.data).ToHex();
return rgba.ToHex();
} }
/// <inheritdoc /> /// <inheritdoc />
@ -278,14 +275,10 @@ public readonly partial struct Color : IEquatable<Color>
if (this.boxedHighPrecisionPixel is null) if (this.boxedHighPrecisionPixel is null)
{ {
pixel = default; return TPixel.FromScaledVector4(this.data);
pixel.FromScaledVector4(this.data);
return pixel;
} }
pixel = default; return TPixel.FromScaledVector4(this.boxedHighPrecisionPixel.ToScaledVector4());
pixel.FromScaledVector4(this.boxedHighPrecisionPixel.ToScaledVector4());
return pixel;
} }
/// <summary> /// <summary>

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

@ -294,72 +294,60 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
private void ReadRle<TPixel>(BufferedReadStream stream, BmpCompression compression, Buffer2D<TPixel> pixels, byte[] colors, int width, int height, bool inverted) private void ReadRle<TPixel>(BufferedReadStream stream, BmpCompression compression, Buffer2D<TPixel> pixels, byte[] colors, int width, int height, bool inverted)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel color = default; using IMemoryOwner<byte> buffer = this.memoryAllocator.Allocate<byte>(width * height, AllocationOptions.Clean);
using (IMemoryOwner<byte> buffer = this.memoryAllocator.Allocate<byte>(width * height, AllocationOptions.Clean)) using IMemoryOwner<bool> undefinedPixels = this.memoryAllocator.Allocate<bool>(width * height, AllocationOptions.Clean);
using (IMemoryOwner<bool> undefinedPixels = this.memoryAllocator.Allocate<bool>(width * height, AllocationOptions.Clean)) using IMemoryOwner<bool> rowsWithUndefinedPixels = this.memoryAllocator.Allocate<bool>(height, AllocationOptions.Clean);
using (IMemoryOwner<bool> rowsWithUndefinedPixels = this.memoryAllocator.Allocate<bool>(height, AllocationOptions.Clean)) Span<bool> rowsWithUndefinedPixelsSpan = rowsWithUndefinedPixels.Memory.Span;
Span<bool> undefinedPixelsSpan = undefinedPixels.Memory.Span;
Span<byte> bufferSpan = buffer.Memory.Span;
if (compression is BmpCompression.RLE8)
{ {
Span<bool> rowsWithUndefinedPixelsSpan = rowsWithUndefinedPixels.Memory.Span; this.UncompressRle8(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
Span<bool> undefinedPixelsSpan = undefinedPixels.Memory.Span; }
Span<byte> bufferSpan = buffer.Memory.Span; else
if (compression is BmpCompression.RLE8) {
{ this.UncompressRle4(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
this.UncompressRle8(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); }
}
else
{
this.UncompressRle4(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
}
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
int newY = Invert(y, height, inverted); int newY = Invert(y, height, inverted);
int rowStartIdx = y * width; int rowStartIdx = y * width;
Span<byte> bufferRow = bufferSpan.Slice(rowStartIdx, width); Span<byte> bufferRow = bufferSpan.Slice(rowStartIdx, width);
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(newY); Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(newY);
bool rowHasUndefinedPixels = rowsWithUndefinedPixelsSpan[y]; bool rowHasUndefinedPixels = rowsWithUndefinedPixelsSpan[y];
if (rowHasUndefinedPixels) if (rowHasUndefinedPixels)
{
// Slow path with undefined pixels.
for (int x = 0; x < width; x++)
{ {
// Slow path with undefined pixels. byte colorIdx = bufferRow[x];
for (int x = 0; x < width; x++) if (undefinedPixelsSpan[rowStartIdx + x])
{ {
byte colorIdx = bufferRow[x]; pixelRow[x] = this.rleSkippedPixelHandling switch
if (undefinedPixelsSpan[rowStartIdx + x])
{
switch (this.rleSkippedPixelHandling)
{
case RleSkippedPixelHandling.FirstColorOfPalette:
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[colorIdx * 4]));
break;
case RleSkippedPixelHandling.Transparent:
color.FromScaledVector4(Vector4.Zero);
break;
// Default handling for skipped pixels is black (which is what System.Drawing is also doing).
default:
color.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
break;
}
}
else
{ {
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[colorIdx * 4])); RleSkippedPixelHandling.FirstColorOfPalette => TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[colorIdx * 4])),
} RleSkippedPixelHandling.Transparent => TPixel.FromScaledVector4(Vector4.Zero),
pixelRow[x] = color; // 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)),
};
} }
} else
else
{
// Fast path without any undefined pixels.
for (int x = 0; x < width; x++)
{ {
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[bufferRow[x] * 4])); pixelRow[x] = TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[colorIdx * 4]));
pixelRow[x] = color;
} }
} }
} }
else
{
// Fast path without any undefined pixels.
for (int x = 0; x < width; x++)
{
pixelRow[x] = TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[bufferRow[x] * 4]));
}
}
} }
} }
@ -375,66 +363,54 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
private void ReadRle24<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels, int width, int height, bool inverted) private void ReadRle24<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels, int width, int height, bool inverted)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel color = default; using IMemoryOwner<byte> buffer = this.memoryAllocator.Allocate<byte>(width * height * 3, AllocationOptions.Clean);
using (IMemoryOwner<byte> buffer = this.memoryAllocator.Allocate<byte>(width * height * 3, AllocationOptions.Clean)) using IMemoryOwner<bool> undefinedPixels = this.memoryAllocator.Allocate<bool>(width * height, AllocationOptions.Clean);
using (IMemoryOwner<bool> undefinedPixels = this.memoryAllocator.Allocate<bool>(width * height, AllocationOptions.Clean)) using IMemoryOwner<bool> rowsWithUndefinedPixels = this.memoryAllocator.Allocate<bool>(height, AllocationOptions.Clean);
using (IMemoryOwner<bool> rowsWithUndefinedPixels = this.memoryAllocator.Allocate<bool>(height, AllocationOptions.Clean)) Span<bool> rowsWithUndefinedPixelsSpan = rowsWithUndefinedPixels.Memory.Span;
{ Span<bool> undefinedPixelsSpan = undefinedPixels.Memory.Span;
Span<bool> rowsWithUndefinedPixelsSpan = rowsWithUndefinedPixels.Memory.Span; Span<byte> bufferSpan = buffer.GetSpan();
Span<bool> undefinedPixelsSpan = undefinedPixels.Memory.Span;
Span<byte> bufferSpan = buffer.GetSpan();
this.UncompressRle24(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); this.UncompressRle24(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{
int newY = Invert(y, height, inverted);
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(newY);
bool rowHasUndefinedPixels = rowsWithUndefinedPixelsSpan[y];
if (rowHasUndefinedPixels)
{ {
int newY = Invert(y, height, inverted); // Slow path with undefined pixels.
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(newY); int yMulWidth = y * width;
bool rowHasUndefinedPixels = rowsWithUndefinedPixelsSpan[y]; int rowStartIdx = yMulWidth * 3;
if (rowHasUndefinedPixels) for (int x = 0; x < width; x++)
{ {
// Slow path with undefined pixels. int idx = rowStartIdx + (x * 3);
int yMulWidth = y * width; if (undefinedPixelsSpan[yMulWidth + x])
int rowStartIdx = yMulWidth * 3;
for (int x = 0; x < width; x++)
{ {
int idx = rowStartIdx + (x * 3); pixelRow[x] = this.rleSkippedPixelHandling switch
if (undefinedPixelsSpan[yMulWidth + x])
{
switch (this.rleSkippedPixelHandling)
{
case RleSkippedPixelHandling.FirstColorOfPalette:
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx]));
break;
case RleSkippedPixelHandling.Transparent:
color.FromScaledVector4(Vector4.Zero);
break;
// Default handling for skipped pixels is black (which is what System.Drawing is also doing).
default:
color.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
break;
}
}
else
{ {
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx])); RleSkippedPixelHandling.FirstColorOfPalette => TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx])),
} RleSkippedPixelHandling.Transparent => TPixel.FromScaledVector4(Vector4.Zero),
pixelRow[x] = color; // 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)),
};
} }
} else
else
{
// Fast path without any undefined pixels.
int rowStartIdx = y * width * 3;
for (int x = 0; x < width; x++)
{ {
int idx = rowStartIdx + (x * 3); pixelRow[x] = TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx]));
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx]));
pixelRow[x] = color;
} }
} }
} }
else
{
// Fast path without any undefined pixels.
int rowStartIdx = y * width * 3;
for (int x = 0; x < width; x++)
{
int idx = rowStartIdx + (x * 3);
pixelRow[x] = TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx]));
}
}
} }
} }
@ -492,7 +468,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
int max = cmd[1]; int max = cmd[1];
int bytesToRead = (int)(((uint)max + 1) / 2); int bytesToRead = (int)(((uint)max + 1) / 2);
Span<byte> run = bytesToRead <= 128 ? scratchBuffer.Slice(0, bytesToRead) : new byte[bytesToRead]; Span<byte> run = bytesToRead <= 128 ? scratchBuffer[..bytesToRead] : new byte[bytesToRead];
stream.Read(run); stream.Read(run);
@ -598,7 +574,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
// Take this number of bytes from the stream as uncompressed data. // Take this number of bytes from the stream as uncompressed data.
int length = cmd[1]; int length = cmd[1];
Span<byte> run = length <= 128 ? scratchBuffer.Slice(0, length) : new byte[length]; Span<byte> run = length <= 128 ? scratchBuffer[..length] : new byte[length];
stream.Read(run); stream.Read(run);
@ -680,7 +656,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
int length = cmd[1]; int length = cmd[1];
int length3 = length * 3; int length3 = length * 3;
Span<byte> run = length3 <= 128 ? scratchBuffer.Slice(0, length3) : new byte[length3]; Span<byte> run = length3 <= 128 ? scratchBuffer[..length3] : new byte[length3];
stream.Read(run); stream.Read(run);
@ -835,7 +811,6 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
} }
using IMemoryOwner<byte> row = this.memoryAllocator.Allocate<byte>(arrayWidth + padding, AllocationOptions.Clean); using IMemoryOwner<byte> row = this.memoryAllocator.Allocate<byte>(arrayWidth + padding, AllocationOptions.Clean);
TPixel color = default;
Span<byte> rowSpan = row.GetSpan(); Span<byte> rowSpan = row.GetSpan();
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
@ -856,8 +831,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
{ {
int colorIndex = ((rowSpan[offset] >> (8 - bitsPerPixel - (shift * bitsPerPixel))) & mask) * bytesPerColorMapEntry; int colorIndex = ((rowSpan[offset] >> (8 - bitsPerPixel - (shift * bitsPerPixel))) & mask) * bytesPerColorMapEntry;
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[colorIndex])); pixelRow[newX] = TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref colors[colorIndex]));
pixelRow[newX] = color;
} }
offset++; offset++;
@ -882,8 +856,6 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
{ {
int padding = CalculatePadding(width, 2); int padding = CalculatePadding(width, 2);
int stride = (width * 2) + padding; int stride = (width * 2) + padding;
TPixel color = default;
int rightShiftRedMask = CalculateRightShift((uint)redMask); int rightShiftRedMask = CalculateRightShift((uint)redMask);
int rightShiftGreenMask = CalculateRightShift((uint)greenMask); int rightShiftGreenMask = CalculateRightShift((uint)greenMask);
int rightShiftBlueMask = CalculateRightShift((uint)blueMask); int rightShiftBlueMask = CalculateRightShift((uint)blueMask);
@ -917,8 +889,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
int b = (blueMaskBits == 5) ? GetBytesFrom5BitValue((temp & blueMask) >> rightShiftBlueMask) : GetBytesFrom6BitValue((temp & blueMask) >> rightShiftBlueMask); int b = (blueMaskBits == 5) ? GetBytesFrom5BitValue((temp & blueMask) >> rightShiftBlueMask) : GetBytesFrom6BitValue((temp & blueMask) >> rightShiftBlueMask);
Rgb24 rgb = new((byte)r, (byte)g, (byte)b); Rgb24 rgb = new((byte)r, (byte)g, (byte)b);
color.FromRgb24(rgb); pixelRow[x] = TPixel.FromRgb24(rgb);
pixelRow[x] = color;
offset += 2; offset += 2;
} }
} }
@ -1107,8 +1078,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
{ {
Bgra32 bgra = bgraRowSpan[x]; Bgra32 bgra = bgraRowSpan[x];
bgra.A = byte.MaxValue; bgra.A = byte.MaxValue;
ref TPixel pixel = ref pixelSpan[x]; pixelSpan[x] = TPixel.FromBgra32(bgra);
pixel.FromBgra32(bgra);
} }
} }
} }
@ -1129,7 +1099,6 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
private void ReadRgb32BitFields<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels, int width, int height, bool inverted, int redMask, int greenMask, int blueMask, int alphaMask) private void ReadRgb32BitFields<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels, int width, int height, bool inverted, int redMask, int greenMask, int blueMask, int alphaMask)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel color = default;
int padding = CalculatePadding(width, 4); int padding = CalculatePadding(width, 4);
int stride = (width * 4) + padding; int stride = (width * 4) + padding;
@ -1179,18 +1148,17 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
g * invMaxValueGreen, g * invMaxValueGreen,
b * invMaxValueBlue, b * invMaxValueBlue,
alpha); alpha);
color.FromScaledVector4(vector4); pixelRow[x] = TPixel.FromScaledVector4(vector4);
} }
else else
{ {
byte r = (byte)((temp & redMask) >> rightShiftRedMask); byte r = (byte)((temp & redMask) >> rightShiftRedMask);
byte g = (byte)((temp & greenMask) >> rightShiftGreenMask); byte g = (byte)((temp & greenMask) >> rightShiftGreenMask);
byte b = (byte)((temp & blueMask) >> rightShiftBlueMask); byte b = (byte)((temp & blueMask) >> rightShiftBlueMask);
byte a = alphaMask != 0 ? (byte)((temp & alphaMask) >> rightShiftAlphaMask) : (byte)255; byte a = alphaMask != 0 ? (byte)((temp & alphaMask) >> rightShiftAlphaMask) : byte.MaxValue;
color.FromRgba32(new Rgba32(r, g, b, a)); pixelRow[x] = TPixel.FromRgba32(new(r, g, b, a));
} }
pixelRow[x] = color;
offset += 4; offset += 4;
} }
} }
@ -1468,7 +1436,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
colorMapSizeBytes = this.infoHeader.ClrUsed * bytesPerColorMapEntry; colorMapSizeBytes = this.infoHeader.ClrUsed * bytesPerColorMapEntry;
} }
palette = Array.Empty<byte>(); palette = [];
if (colorMapSizeBytes > 0) if (colorMapSizeBytes > 0)
{ {

10
src/ImageSharp/Formats/Gif/GifDecoderCore.cs

@ -185,7 +185,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals
{ {
uint frameCount = 0; uint frameCount = 0;
ImageFrameMetadata? previousFrame = null; ImageFrameMetadata? previousFrame = null;
List<ImageFrameMetadata> framesMetadata = new(); List<ImageFrameMetadata> framesMetadata = [];
try try
{ {
this.ReadLogicalScreenDescriptorAndGlobalColorTable(stream); this.ReadLogicalScreenDescriptorAndGlobalColorTable(stream);
@ -595,9 +595,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals
for (int x = descriptorLeft; x < descriptorRight && x < imageWidth; x++) for (int x = descriptorLeft; x < descriptorRight && x < imageWidth; x++)
{ {
int index = Numerics.Clamp(Unsafe.Add(ref indicesRowRef, (uint)(x - descriptorLeft)), 0, colorTableMaxIdx); int index = Numerics.Clamp(Unsafe.Add(ref indicesRowRef, (uint)(x - descriptorLeft)), 0, colorTableMaxIdx);
ref TPixel pixel = ref Unsafe.Add(ref rowRef, (uint)x); Unsafe.Add(ref rowRef, (uint)x) = TPixel.FromRgb24(colorTable[index]);
Rgb24 rgb = colorTable[index];
pixel.FromRgb24(rgb);
} }
} }
else else
@ -613,9 +611,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals
} }
int index = Numerics.Clamp(rawIndex, 0, colorTableMaxIdx); int index = Numerics.Clamp(rawIndex, 0, colorTableMaxIdx);
ref TPixel pixel = ref Unsafe.Add(ref rowRef, (uint)x); Unsafe.Add(ref rowRef, (uint)x) = TPixel.FromRgb24(colorTable[index]);
Rgb24 rgb = colorTable[index];
pixel.FromRgb24(rgb);
} }
} }
} }

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

@ -169,7 +169,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
this.EncodeFirstFrame(stream, frameMetadata, quantized); this.EncodeFirstFrame(stream, frameMetadata, quantized);
// Capture the global palette for reuse on subsequent frames and cleanup the quantized frame. // Capture the global palette for reuse on subsequent frames and cleanup the quantized frame.
TPixel[] globalPalette = image.Frames.Count == 1 ? Array.Empty<TPixel>() : quantized.Palette.ToArray(); TPixel[] globalPalette = image.Frames.Count == 1 ? [] : quantized.Palette.ToArray();
this.EncodeAdditionalFrames(stream, image, globalPalette, derivedTransparencyIndex, frameMetadata.DisposalMethod); this.EncodeAdditionalFrames(stream, image, globalPalette, derivedTransparencyIndex, frameMetadata.DisposalMethod);
@ -488,8 +488,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
int index = -1; int index = -1;
if (quantized != null) if (quantized != null)
{ {
TPixel transparentPixel = default; TPixel transparentPixel = TPixel.FromScaledVector4(Vector4.Zero);
transparentPixel.FromScaledVector4(Vector4.Zero);
ReadOnlySpan<TPixel> palette = quantized.Palette.Span; ReadOnlySpan<TPixel> palette = quantized.Palette.Span;
// Transparent pixels are much more likely to be found at the end of a palette. // Transparent pixels are much more likely to be found at the end of a palette.
@ -693,7 +692,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
} }
IMemoryOwner<byte>? owner = null; IMemoryOwner<byte>? owner = null;
Span<byte> extensionBuffer = stackalloc byte[0]; // workaround compiler limitation scoped Span<byte> extensionBuffer = []; // workaround compiler limitation
if (extensionSize > 128) if (extensionSize > 128)
{ {
owner = this.memoryAllocator.Allocate<byte>(extensionSize + 3); owner = this.memoryAllocator.Allocate<byte>(extensionSize + 3);

9
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -328,18 +328,17 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
=> clone.ProcessPixelRows(accessor => => clone.ProcessPixelRows(accessor =>
{ {
// TODO: We should be able to speed this up with SIMD and masking. // TODO: We should be able to speed this up with SIMD and masking.
Rgba32 rgba32 = default;
Rgba32 transparent = Color.Transparent.ToPixel<Rgba32>(); Rgba32 transparent = Color.Transparent.ToPixel<Rgba32>();
for (int y = 0; y < accessor.Height; y++) for (int y = 0; y < accessor.Height; y++)
{ {
Span<TPixel> span = accessor.GetRowSpan(y); Span<TPixel> span = accessor.GetRowSpan(y);
for (int x = 0; x < accessor.Width; x++) for (int x = 0; x < accessor.Width; x++)
{ {
span[x].ToRgba32(ref rgba32); ref TPixel pixel = ref span[x];
Rgba32 rgba = pixel.ToRgba32();
if (rgba32.A is 0) if (rgba.A is 0)
{ {
span[x].FromRgba32(transparent); pixel = TPixel.FromRgba32(transparent);
} }
} }
} }

119
src/ImageSharp/Formats/Png/PngScanlineProcessor.cs

@ -42,7 +42,6 @@ internal static class PngScanlineProcessor
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
uint offset = pixelOffset + frameControl.XOffset; uint offset = pixelOffset + frameControl.XOffset;
TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
int scaleFactor = 255 / (ColorNumerics.GetColorCountForBitDepth(bitDepth) - 1); int scaleFactor = 255 / (ColorNumerics.GetColorCountForBitDepth(bitDepth) - 1);
@ -55,8 +54,7 @@ internal static class PngScanlineProcessor
for (nuint x = offset; x < frameControl.XMax; x += increment, o += 2) for (nuint x = offset; x < frameControl.XMax; x += increment, o += 2)
{ {
ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
pixel.FromL16(Unsafe.As<ushort, L16>(ref luminance)); Unsafe.Add(ref rowSpanRef, x) = TPixel.FromL16(Unsafe.As<ushort, L16>(ref luminance));
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
else else
@ -64,8 +62,7 @@ internal static class PngScanlineProcessor
for (nuint x = offset, o = 0; x < frameControl.XMax; x += increment, o++) for (nuint x = offset, o = 0; x < frameControl.XMax; x += increment, o++)
{ {
byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, o) * scaleFactor); byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, o) * scaleFactor);
pixel.FromL8(Unsafe.As<byte, L8>(ref luminance)); Unsafe.Add(ref rowSpanRef, x) = TPixel.FromL8(Unsafe.As<byte, L8>(ref luminance));
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
@ -75,30 +72,22 @@ internal static class PngScanlineProcessor
if (bitDepth == 16) if (bitDepth == 16)
{ {
L16 transparent = transparentColor.Value.ToPixel<L16>(); L16 transparent = transparentColor.Value.ToPixel<L16>();
La32 source = default;
int o = 0; int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += 2) for (nuint x = offset; x < frameControl.XMax; x += increment, o += 2)
{ {
ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
source.L = luminance; La32 source = new(luminance, luminance.Equals(transparent.PackedValue) ? ushort.MinValue : ushort.MaxValue);
source.A = luminance.Equals(transparent.PackedValue) ? ushort.MinValue : ushort.MaxValue; Unsafe.Add(ref rowSpanRef, x) = TPixel.FromLa32(source);
pixel.FromLa32(source);
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
else else
{ {
byte transparent = (byte)(transparentColor.Value.ToPixel<L8>().PackedValue * scaleFactor); byte transparent = (byte)(transparentColor.Value.ToPixel<L8>().PackedValue * scaleFactor);
La16 source = default;
for (nuint x = offset, o = 0; x < frameControl.XMax; x += increment, o++) for (nuint x = offset, o = 0; x < frameControl.XMax; x += increment, o++)
{ {
byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, o) * scaleFactor); byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, o) * scaleFactor);
source.L = luminance; La16 source = new(luminance, luminance.Equals(transparent) ? byte.MinValue : byte.MaxValue);
source.A = luminance.Equals(transparent) ? byte.MinValue : byte.MaxValue; Unsafe.Add(ref rowSpanRef, x) = TPixel.FromLa16(source);
pixel.FromLa16(source);
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
} }
@ -133,34 +122,28 @@ internal static class PngScanlineProcessor
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
uint offset = pixelOffset + frameControl.XOffset; uint offset = pixelOffset + frameControl.XOffset;
TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
if (bitDepth == 16) if (bitDepth == 16)
{ {
La32 source = default;
int o = 0; int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += 4) for (nuint x = offset; x < frameControl.XMax; x += increment, o += 4)
{ {
source.L = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); ushort l = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
source.A = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + 2, 2)); ushort a = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + 2, 2));
pixel.FromLa32(source); Unsafe.Add(ref rowSpanRef, (uint)x) = TPixel.FromLa32(new(l, a));
Unsafe.Add(ref rowSpanRef, (uint)x) = pixel;
} }
} }
else else
{ {
La16 source = default;
nuint offset2 = 0; nuint offset2 = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment) for (nuint x = offset; x < frameControl.XMax; x += increment)
{ {
source.L = Unsafe.Add(ref scanlineSpanRef, offset2); byte l = Unsafe.Add(ref scanlineSpanRef, offset2);
source.A = Unsafe.Add(ref scanlineSpanRef, offset2 + bytesPerSample); byte a = Unsafe.Add(ref scanlineSpanRef, offset2 + bytesPerSample);
Unsafe.Add(ref rowSpanRef, x) = TPixel.FromLa16(new(l, a));
pixel.FromLa16(source);
Unsafe.Add(ref rowSpanRef, x) = pixel;
offset2 += bytesPerPixel; offset2 += bytesPerPixel;
} }
} }
@ -194,7 +177,6 @@ internal static class PngScanlineProcessor
PngThrowHelper.ThrowMissingPalette(); PngThrowHelper.ThrowMissingPalette();
} }
TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
ref Color paletteBase = ref MemoryMarshal.GetReference(palette.Value.Span); ref Color paletteBase = ref MemoryMarshal.GetReference(palette.Value.Span);
@ -202,8 +184,7 @@ internal static class PngScanlineProcessor
for (nuint x = pixelOffset, o = 0; x < frameControl.XMax; x += increment, o++) for (nuint x = pixelOffset, o = 0; x < frameControl.XMax; x += increment, o++)
{ {
uint index = Unsafe.Add(ref scanlineSpanRef, o); uint index = Unsafe.Add(ref scanlineSpanRef, o);
pixel.FromRgba32(Unsafe.Add(ref paletteBase, index).ToPixel<Rgba32>()); Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba32(Unsafe.Add(ref paletteBase, index).ToPixel<Rgba32>());
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
@ -243,8 +224,6 @@ internal static class PngScanlineProcessor
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
uint offset = pixelOffset + frameControl.XOffset; uint offset = pixelOffset + frameControl.XOffset;
TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
@ -252,16 +231,13 @@ internal static class PngScanlineProcessor
{ {
if (bitDepth == 16) if (bitDepth == 16)
{ {
Rgb48 rgb48 = default;
int o = 0; int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel) for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{ {
rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample)); ushort r = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample)); ushort g = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
rgb48.B = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample)); ushort b = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample));
Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgb48(new(r, g, b));
pixel.FromRgb48(rgb48);
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
else if (pixelOffset == 0 && increment == 1) else if (pixelOffset == 0 && increment == 1)
@ -274,16 +250,13 @@ internal static class PngScanlineProcessor
} }
else else
{ {
Rgb24 rgb = default;
int o = 0; int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel) for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{ {
rgb.R = Unsafe.Add(ref scanlineSpanRef, (uint)o); byte r = Unsafe.Add(ref scanlineSpanRef, (uint)o);
rgb.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample)); byte g = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
rgb.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample))); byte b = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgb24(new(r, g, b));
pixel.FromRgb24(rgb);
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
@ -293,27 +266,20 @@ internal static class PngScanlineProcessor
if (bitDepth == 16) if (bitDepth == 16)
{ {
Rgb48 transparent = transparentColor.Value.ToPixel<Rgb48>(); Rgb48 transparent = transparentColor.Value.ToPixel<Rgb48>();
Rgba64 rgba = default;
Rgb48 rgb48 = default;
Rgba64 rgba64 = default;
int o = 0; int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel) for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{ {
rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample)); rgba.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample)); rgba.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
rgb48.B = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample)); rgba.B = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample));
rgba.A = rgba.Rgb.Equals(transparent) ? ushort.MinValue : ushort.MaxValue;
rgba64.Rgb = rgb48; Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba64(rgba);
rgba64.A = rgb48.Equals(transparent) ? ushort.MinValue : ushort.MaxValue;
pixel.FromRgba64(rgba64);
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
else else
{ {
Rgb24 transparent = transparentColor.Value.ToPixel<Rgb24>(); Rgb24 transparent = transparentColor.Value.ToPixel<Rgb24>();
Rgba32 rgba = default; Rgba32 rgba = default;
int o = 0; int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel) for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
@ -322,9 +288,7 @@ internal static class PngScanlineProcessor
rgba.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample)); rgba.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
rgba.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample))); rgba.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
rgba.A = transparent.Equals(rgba.Rgb) ? byte.MinValue : byte.MaxValue; rgba.A = transparent.Equals(rgba.Rgb) ? byte.MinValue : byte.MaxValue;
Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba32(rgba);
pixel.FromRgba32(rgba);
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
} }
@ -362,22 +326,18 @@ internal static class PngScanlineProcessor
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
uint offset = pixelOffset + frameControl.XOffset; uint offset = pixelOffset + frameControl.XOffset;
TPixel pixel = default;
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
if (bitDepth == 16) if (bitDepth == 16)
{ {
Rgba64 rgba64 = default;
int o = 0; int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel) for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{ {
rgba64.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample)); ushort r = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
rgba64.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample)); ushort g = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
rgba64.B = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample)); ushort b = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample));
rgba64.A = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (3 * bytesPerSample), bytesPerSample)); ushort a = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (3 * bytesPerSample), bytesPerSample));
Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba64(new(r, g, b, a));
pixel.FromRgba64(rgba64);
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
else if (pixelOffset == 0 && increment == 1) else if (pixelOffset == 0 && increment == 1)
@ -391,17 +351,14 @@ internal static class PngScanlineProcessor
else else
{ {
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
Rgba32 rgba = default;
int o = 0; int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel) for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{ {
rgba.R = Unsafe.Add(ref scanlineSpanRef, (uint)o); byte r = Unsafe.Add(ref scanlineSpanRef, (uint)o);
rgba.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample)); byte g = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
rgba.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample))); byte b = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
rgba.A = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (3 * bytesPerSample))); byte a = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (3 * bytesPerSample)));
Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba32(new(r, g, b, a));
pixel.FromRgba32(rgba);
Unsafe.Add(ref rowSpanRef, x) = pixel;
} }
} }
} }

14
src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

@ -149,7 +149,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
Span<Rgba32> previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan(); Span<Rgba32> previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan();
Rgba32 previousPixel = new(0, 0, 0, 255); Rgba32 previousPixel = new(0, 0, 0, 255);
// We save the pixel to avoid loosing the fully opaque black pixel // We save the pixel to avoid losing the fully opaque black pixel
// See https://github.com/phoboslab/qoi/issues/258 // See https://github.com/phoboslab/qoi/issues/258
int pixelArrayPosition = GetArrayPosition(previousPixel); int pixelArrayPosition = GetArrayPosition(previousPixel);
previouslySeenPixels[pixelArrayPosition] = previousPixel; previouslySeenPixels[pixelArrayPosition] = previousPixel;
@ -174,7 +174,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
} }
readPixel.A = previousPixel.A; readPixel.A = previousPixel.A;
pixel.FromRgba32(readPixel); pixel = TPixel.FromRgba32(readPixel);
pixelArrayPosition = GetArrayPosition(readPixel); pixelArrayPosition = GetArrayPosition(readPixel);
previouslySeenPixels[pixelArrayPosition] = readPixel; previouslySeenPixels[pixelArrayPosition] = readPixel;
break; break;
@ -186,7 +186,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
ThrowInvalidImageContentException(); ThrowInvalidImageContentException();
} }
pixel.FromRgba32(readPixel); pixel = TPixel.FromRgba32(readPixel);
pixelArrayPosition = GetArrayPosition(readPixel); pixelArrayPosition = GetArrayPosition(readPixel);
previouslySeenPixels[pixelArrayPosition] = readPixel; previouslySeenPixels[pixelArrayPosition] = readPixel;
break; break;
@ -197,7 +197,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
// Getting one pixel from previously seen pixels // Getting one pixel from previously seen pixels
case QoiChunk.QoiOpIndex: case QoiChunk.QoiOpIndex:
readPixel = previouslySeenPixels[operationByte]; readPixel = previouslySeenPixels[operationByte];
pixel.FromRgba32(readPixel); pixel = TPixel.FromRgba32(readPixel);
break; break;
// Get one pixel from the difference (-2..1) of the previous pixel // Get one pixel from the difference (-2..1) of the previous pixel
@ -211,7 +211,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
G = (byte)Numerics.Modulo256(previousPixel.G + (greenDifference - 2)), G = (byte)Numerics.Modulo256(previousPixel.G + (greenDifference - 2)),
B = (byte)Numerics.Modulo256(previousPixel.B + (blueDifference - 2)) B = (byte)Numerics.Modulo256(previousPixel.B + (blueDifference - 2))
}; };
pixel.FromRgba32(readPixel); pixel = TPixel.FromRgba32(readPixel);
pixelArrayPosition = GetArrayPosition(readPixel); pixelArrayPosition = GetArrayPosition(readPixel);
previouslySeenPixels[pixelArrayPosition] = readPixel; previouslySeenPixels[pixelArrayPosition] = readPixel;
break; break;
@ -227,7 +227,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
int currentRed = Numerics.Modulo256(diffRedDG - 8 + (diffGreen - 32) + previousPixel.R); int currentRed = Numerics.Modulo256(diffRedDG - 8 + (diffGreen - 32) + previousPixel.R);
int currentBlue = Numerics.Modulo256(diffBlueDG - 8 + (diffGreen - 32) + previousPixel.B); int currentBlue = Numerics.Modulo256(diffBlueDG - 8 + (diffGreen - 32) + previousPixel.B);
readPixel = previousPixel with { R = (byte)currentRed, B = (byte)currentBlue, G = (byte)currentGreen }; readPixel = previousPixel with { R = (byte)currentRed, B = (byte)currentBlue, G = (byte)currentGreen };
pixel.FromRgba32(readPixel); pixel = TPixel.FromRgba32(readPixel);
pixelArrayPosition = GetArrayPosition(readPixel); pixelArrayPosition = GetArrayPosition(readPixel);
previouslySeenPixels[pixelArrayPosition] = readPixel; previouslySeenPixels[pixelArrayPosition] = readPixel;
break; break;
@ -241,7 +241,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
} }
readPixel = previousPixel; readPixel = previousPixel;
pixel.FromRgba32(readPixel); pixel = TPixel.FromRgba32(readPixel);
for (int k = -1; k < repetitions; k++, j++) for (int k = -1; k < repetitions; k++, j++)
{ {
if (j == row.Length) if (j == row.Length)

100
src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

@ -222,7 +222,6 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadPaletted<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, Span<byte> palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) private void ReadPaletted<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, Span<byte> palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel color = default;
bool invertX = InvertX(origin); bool invertX = InvertX(origin);
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
@ -237,14 +236,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
{ {
for (int x = width - 1; x >= 0; x--) for (int x = width - 1; x >= 0; x--)
{ {
this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
} }
} }
else else
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
} }
} }
@ -255,14 +254,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
{ {
for (int x = width - 1; x >= 0; x--) for (int x = width - 1; x >= 0; x--)
{ {
ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
} }
} }
else else
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
} }
} }
@ -273,14 +272,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
{ {
for (int x = width - 1; x >= 0; x--) for (int x = width - 1; x >= 0; x--)
{ {
ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
} }
} }
else else
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
} }
} }
@ -319,16 +318,16 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
switch (colorMapPixelSizeInBytes) switch (colorMapPixelSizeInBytes)
{ {
case 1: case 1:
color.FromL8(Unsafe.As<byte, L8>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); color = TPixel.FromL8(Unsafe.As<byte, L8>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
break; break;
case 2: case 2:
this.ReadPalettedBgra16Pixel(palette, bufferSpan[idx], colorMapPixelSizeInBytes, ref color); color = this.ReadPalettedBgra16Pixel<TPixel>(palette, bufferSpan[idx], colorMapPixelSizeInBytes);
break; break;
case 3: case 3:
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); color = TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
break; break;
case 4: case 4:
color.FromBgra32(Unsafe.As<byte, Bgra32>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); color = TPixel.FromBgra32(Unsafe.As<byte, Bgra32>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
break; break;
} }
@ -350,17 +349,15 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadMonoChrome<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, TgaImageOrigin origin) private void ReadMonoChrome<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
bool invertX = InvertX(origin); if (InvertX(origin))
if (invertX)
{ {
TPixel color = default;
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
int newY = InvertY(y, height, origin); int newY = InvertY(y, height, origin);
Span<TPixel> pixelSpan = pixels.DangerousGetRowSpan(newY); Span<TPixel> pixelSpan = pixels.DangerousGetRowSpan(newY);
for (int x = width - 1; x >= 0; x--) for (int x = width - 1; x >= 0; x--)
{ {
ReadL8Pixel(stream, color, x, pixelSpan); ReadL8Pixel(stream, x, pixelSpan);
} }
} }
@ -369,8 +366,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
using IMemoryOwner<byte> row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 1, 0); using IMemoryOwner<byte> row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 1, 0);
Span<byte> rowSpan = row.GetSpan(); Span<byte> rowSpan = row.GetSpan();
bool invertY = InvertY(origin); if (InvertY(origin))
if (invertY)
{ {
for (int y = height - 1; y >= 0; y--) for (int y = height - 1; y >= 0; y--)
{ {
@ -398,7 +394,6 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadBgra16<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, TgaImageOrigin origin) private void ReadBgra16<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel color = default;
bool invertX = InvertX(origin); bool invertX = InvertX(origin);
using IMemoryOwner<byte> row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 2, 0); using IMemoryOwner<byte> row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 2, 0);
Span<byte> rowSpan = row.GetSpan(); Span<byte> rowSpan = row.GetSpan();
@ -426,14 +421,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
if (this.fileHeader.ImageType == TgaImageType.BlackAndWhite) if (this.fileHeader.ImageType == TgaImageType.BlackAndWhite)
{ {
color.FromLa16(Unsafe.As<byte, La16>(ref MemoryMarshal.GetReference(scratchBuffer))); pixelSpan[x] = TPixel.FromLa16(Unsafe.As<byte, La16>(ref MemoryMarshal.GetReference(scratchBuffer)));
} }
else else
{ {
color.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref MemoryMarshal.GetReference(scratchBuffer))); pixelSpan[x] = TPixel.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref MemoryMarshal.GetReference(scratchBuffer)));
} }
pixelSpan[x] = color;
} }
} }
else else
@ -477,18 +470,16 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadBgr24<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, TgaImageOrigin origin) private void ReadBgr24<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
bool invertX = InvertX(origin); if (InvertX(origin))
if (invertX)
{ {
Span<byte> scratchBuffer = stackalloc byte[4]; Span<byte> scratchBuffer = stackalloc byte[4];
TPixel color = default;
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
int newY = InvertY(y, height, origin); int newY = InvertY(y, height, origin);
Span<TPixel> pixelSpan = pixels.DangerousGetRowSpan(newY); Span<TPixel> pixelSpan = pixels.DangerousGetRowSpan(newY);
for (int x = width - 1; x >= 0; x--) for (int x = width - 1; x >= 0; x--)
{ {
ReadBgr24Pixel(stream, color, x, pixelSpan, scratchBuffer); ReadBgr24Pixel(stream, x, pixelSpan, scratchBuffer);
} }
} }
@ -497,9 +488,8 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
using IMemoryOwner<byte> row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 3, 0); using IMemoryOwner<byte> row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 3, 0);
Span<byte> rowSpan = row.GetSpan(); Span<byte> rowSpan = row.GetSpan();
bool invertY = InvertY(origin);
if (invertY) if (InvertY(origin))
{ {
for (int y = height - 1; y >= 0; y--) for (int y = height - 1; y >= 0; y--)
{ {
@ -527,7 +517,6 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadBgra32<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, TgaImageOrigin origin) private void ReadBgra32<TPixel>(BufferedReadStream stream, int width, int height, Buffer2D<TPixel> pixels, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel color = default;
bool invertX = InvertX(origin); bool invertX = InvertX(origin);
Guard.NotNull(this.tgaMetadata); Guard.NotNull(this.tgaMetadata);
@ -565,14 +554,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
{ {
for (int x = width - 1; x >= 0; x--) for (int x = width - 1; x >= 0; x--)
{ {
this.ReadBgra32Pixel(stream, x, color, pixelRow, scratchBuffer); this.ReadBgra32Pixel(stream, x, pixelRow, scratchBuffer);
} }
} }
else else
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
this.ReadBgra32Pixel(stream, x, color, pixelRow, scratchBuffer); this.ReadBgra32Pixel(stream, x, pixelRow, scratchBuffer);
} }
} }
} }
@ -610,7 +599,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
switch (bytesPerPixel) switch (bytesPerPixel)
{ {
case 1: case 1:
color.FromL8(Unsafe.As<byte, L8>(ref bufferSpan[idx])); color = TPixel.FromL8(Unsafe.As<byte, L8>(ref bufferSpan[idx]));
break; break;
case 2: case 2:
if (!this.hasAlpha) if (!this.hasAlpha)
@ -621,26 +610,26 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
if (this.fileHeader.ImageType == TgaImageType.RleBlackAndWhite) if (this.fileHeader.ImageType == TgaImageType.RleBlackAndWhite)
{ {
color.FromLa16(Unsafe.As<byte, La16>(ref bufferSpan[idx])); color = TPixel.FromLa16(Unsafe.As<byte, La16>(ref bufferSpan[idx]));
} }
else else
{ {
color.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref bufferSpan[idx])); color = TPixel.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref bufferSpan[idx]));
} }
break; break;
case 3: case 3:
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx])); color = TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref bufferSpan[idx]));
break; break;
case 4: case 4:
if (this.hasAlpha) if (this.hasAlpha)
{ {
color.FromBgra32(Unsafe.As<byte, Bgra32>(ref bufferSpan[idx])); color = TPixel.FromBgra32(Unsafe.As<byte, Bgra32>(ref bufferSpan[idx]));
} }
else else
{ {
byte alpha = alphaBits == 0 ? byte.MaxValue : bufferSpan[idx + 3]; byte alpha = alphaBits == 0 ? byte.MaxValue : bufferSpan[idx + 3];
color.FromBgra32(new Bgra32(bufferSpan[idx + 2], bufferSpan[idx + 1], bufferSpan[idx], alpha)); color = TPixel.FromBgra32(new Bgra32(bufferSpan[idx + 2], bufferSpan[idx + 1], bufferSpan[idx], alpha));
} }
break; break;
@ -677,16 +666,15 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ReadL8Pixel<TPixel>(BufferedReadStream stream, TPixel color, int x, Span<TPixel> pixelSpan) private static void ReadL8Pixel<TPixel>(BufferedReadStream stream, int x, Span<TPixel> pixelSpan)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
byte pixelValue = (byte)stream.ReadByte(); byte pixelValue = (byte)stream.ReadByte();
color.FromL8(Unsafe.As<byte, L8>(ref pixelValue)); pixelSpan[x] = TPixel.FromL8(Unsafe.As<byte, L8>(ref pixelValue));
pixelSpan[x] = color;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ReadBgr24Pixel<TPixel>(BufferedReadStream stream, TPixel color, int x, Span<TPixel> pixelSpan, Span<byte> scratchBuffer) private static void ReadBgr24Pixel<TPixel>(BufferedReadStream stream, int x, Span<TPixel> pixelSpan, Span<byte> scratchBuffer)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
int bytesRead = stream.Read(scratchBuffer, 0, 3); int bytesRead = stream.Read(scratchBuffer, 0, 3);
@ -695,8 +683,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a bgr pixel"); TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a bgr pixel");
} }
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref MemoryMarshal.GetReference(scratchBuffer))); pixelSpan[x] = TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref MemoryMarshal.GetReference(scratchBuffer)));
pixelSpan[x] = color;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -714,7 +701,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ReadBgra32Pixel<TPixel>(BufferedReadStream stream, int x, TPixel color, Span<TPixel> pixelRow, Span<byte> scratchBuffer) private void ReadBgra32Pixel<TPixel>(BufferedReadStream stream, int x, Span<TPixel> pixelRow, Span<byte> scratchBuffer)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
int bytesRead = stream.Read(scratchBuffer, 0, 4); int bytesRead = stream.Read(scratchBuffer, 0, 4);
@ -726,8 +713,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
Guard.NotNull(this.tgaMetadata); Guard.NotNull(this.tgaMetadata);
byte alpha = this.tgaMetadata.AlphaChannelBits == 0 ? byte.MaxValue : scratchBuffer[3]; byte alpha = this.tgaMetadata.AlphaChannelBits == 0 ? byte.MaxValue : scratchBuffer[3];
color.FromBgra32(new Bgra32(scratchBuffer[2], scratchBuffer[1], scratchBuffer[0], alpha)); pixelRow[x] = TPixel.FromBgra32(new Bgra32(scratchBuffer[2], scratchBuffer[1], scratchBuffer[0], alpha));
pixelRow[x] = color;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -745,7 +731,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ReadPalettedBgra16Pixel<TPixel>(BufferedReadStream stream, Span<byte> palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span<TPixel> pixelRow) private void ReadPalettedBgra16Pixel<TPixel>(BufferedReadStream stream, Span<byte> palette, int colorMapPixelSizeInBytes, int x, Span<TPixel> pixelRow)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
int colorIndex = stream.ReadByte(); int colorIndex = stream.ReadByte();
@ -754,16 +740,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index"); TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index");
} }
this.ReadPalettedBgra16Pixel(palette, colorIndex, colorMapPixelSizeInBytes, ref color); pixelRow[x] = this.ReadPalettedBgra16Pixel<TPixel>(palette, colorIndex, colorMapPixelSizeInBytes);
pixelRow[x] = color;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ReadPalettedBgra16Pixel<TPixel>(Span<byte> palette, int index, int colorMapPixelSizeInBytes, ref TPixel color) private TPixel ReadPalettedBgra16Pixel<TPixel>(Span<byte> palette, int index, int colorMapPixelSizeInBytes)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
Bgra5551 bgra = default; Bgra5551 bgra = Unsafe.As<byte, Bgra5551>(ref palette[index * colorMapPixelSizeInBytes]);
bgra.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref palette[index * colorMapPixelSizeInBytes]));
if (!this.hasAlpha) if (!this.hasAlpha)
{ {
@ -771,11 +755,11 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000); bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000);
} }
color.FromBgra5551(bgra); return TPixel.FromBgra5551(bgra);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ReadPalettedBgr24Pixel<TPixel>(BufferedReadStream stream, Span<byte> palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span<TPixel> pixelRow) private static void ReadPalettedBgr24Pixel<TPixel>(BufferedReadStream stream, Span<byte> palette, int colorMapPixelSizeInBytes, int x, Span<TPixel> pixelRow)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
int colorIndex = stream.ReadByte(); int colorIndex = stream.ReadByte();
@ -784,12 +768,11 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index"); TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index");
} }
color.FromBgr24(Unsafe.As<byte, Bgr24>(ref palette[colorIndex * colorMapPixelSizeInBytes])); pixelRow[x] = TPixel.FromBgr24(Unsafe.As<byte, Bgr24>(ref palette[colorIndex * colorMapPixelSizeInBytes]));
pixelRow[x] = color;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ReadPalettedBgra32Pixel<TPixel>(BufferedReadStream stream, Span<byte> palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span<TPixel> pixelRow) private static void ReadPalettedBgra32Pixel<TPixel>(BufferedReadStream stream, Span<byte> palette, int colorMapPixelSizeInBytes, int x, Span<TPixel> pixelRow)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
int colorIndex = stream.ReadByte(); int colorIndex = stream.ReadByte();
@ -798,8 +781,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index"); TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index");
} }
color.FromBgra32(Unsafe.As<byte, Bgra32>(ref palette[colorIndex * colorMapPixelSizeInBytes])); pixelRow[x] = TPixel.FromBgra32(Unsafe.As<byte, Bgra32>(ref palette[colorIndex * colorMapPixelSizeInBytes]));
pixelRow[x] = color;
} }
/// <summary> /// <summary>

10
src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

@ -5,7 +5,6 @@ using System.Buffers;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -160,7 +159,6 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals
private void WriteRunLengthEncodedImage<TPixel>(Stream stream, ImageFrame<TPixel> image) private void WriteRunLengthEncodedImage<TPixel>(Stream stream, ImageFrame<TPixel> image)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
Rgba32 color = default;
Buffer2D<TPixel> pixels = image.PixelBuffer; Buffer2D<TPixel> pixels = image.PixelBuffer;
for (int y = 0; y < image.Height; y++) for (int y = 0; y < image.Height; y++)
{ {
@ -168,14 +166,13 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals
for (int x = 0; x < image.Width;) for (int x = 0; x < image.Width;)
{ {
TPixel currentPixel = pixelRow[x]; TPixel currentPixel = pixelRow[x];
currentPixel.ToRgba32(ref color);
byte equalPixelCount = FindEqualPixels(pixelRow, x); byte equalPixelCount = FindEqualPixels(pixelRow, x);
if (equalPixelCount > 0) if (equalPixelCount > 0)
{ {
// Write the number of equal pixels, with the high bit set, indicating ist a compressed pixel run. // Write the number of equal pixels, with the high bit set, indicating ist a compressed pixel run.
stream.WriteByte((byte)(equalPixelCount | 128)); stream.WriteByte((byte)(equalPixelCount | 128));
this.WritePixel(stream, currentPixel, color); this.WritePixel(stream, currentPixel, currentPixel.ToRgba32());
x += equalPixelCount + 1; x += equalPixelCount + 1;
} }
else else
@ -183,13 +180,12 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals
// Write Raw Packet (i.e., Non-Run-Length Encoded): // Write Raw Packet (i.e., Non-Run-Length Encoded):
byte unEqualPixelCount = FindUnEqualPixels(pixelRow, x); byte unEqualPixelCount = FindUnEqualPixels(pixelRow, x);
stream.WriteByte(unEqualPixelCount); stream.WriteByte(unEqualPixelCount);
this.WritePixel(stream, currentPixel, color); this.WritePixel(stream, currentPixel, currentPixel.ToRgba32());
x++; x++;
for (int i = 0; i < unEqualPixelCount; i++) for (int i = 0; i < unEqualPixelCount; i++)
{ {
currentPixel = pixelRow[x]; currentPixel = pixelRow[x];
currentPixel.ToRgba32(ref color); this.WritePixel(stream, currentPixel, currentPixel.ToRgba32());
this.WritePixel(stream, currentPixel, color);
x++; x++;
} }
} }

140
src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs

@ -102,8 +102,7 @@ internal static class HorizontalPredictor
byte r = (byte)(rowRgb[x].R - rowRgb[x - 1].R); byte r = (byte)(rowRgb[x].R - rowRgb[x - 1].R);
byte g = (byte)(rowRgb[x].G - rowRgb[x - 1].G); byte g = (byte)(rowRgb[x].G - rowRgb[x - 1].G);
byte b = (byte)(rowRgb[x].B - rowRgb[x - 1].B); byte b = (byte)(rowRgb[x].B - rowRgb[x - 1].B);
var rgb = new Rgb24(r, g, b); rowRgb[x] = new Rgb24(r, g, b);
rowRgb[x].FromRgb24(rgb);
} }
} }
} }
@ -128,8 +127,7 @@ internal static class HorizontalPredictor
for (int x = rowL16.Length - 1; x >= 1; x--) for (int x = rowL16.Length - 1; x >= 1; x--)
{ {
ushort val = (ushort)(rowL16[x].PackedValue - rowL16[x - 1].PackedValue); rowL16[x].PackedValue = (ushort)(rowL16[x].PackedValue - rowL16[x - 1].PackedValue);
rowL16[x].PackedValue = val;
} }
} }
} }
@ -181,13 +179,13 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
ushort pixelValue = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2)); ushort pixelValue = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 2); Span<byte> rowSpan = rowBytes.Slice(offset, 2);
ushort diff = TiffUtils.ConvertToUShortBigEndian(rowSpan); ushort diff = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
pixelValue += diff; pixelValue += diff;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, pixelValue); BinaryPrimitives.WriteUInt16BigEndian(rowSpan, pixelValue);
offset += 2; offset += 2;
@ -200,13 +198,13 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
ushort pixelValue = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2)); ushort pixelValue = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 2); Span<byte> rowSpan = rowBytes.Slice(offset, 2);
ushort diff = TiffUtils.ConvertToUShortLittleEndian(rowSpan); ushort diff = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
pixelValue += diff; pixelValue += diff;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, pixelValue); BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, pixelValue);
offset += 2; offset += 2;
@ -225,13 +223,13 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
uint pixelValue = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4)); uint pixelValue = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 4); Span<byte> rowSpan = rowBytes.Slice(offset, 4);
uint diff = TiffUtils.ConvertToUIntBigEndian(rowSpan); uint diff = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
pixelValue += diff; pixelValue += diff;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, pixelValue); BinaryPrimitives.WriteUInt32BigEndian(rowSpan, pixelValue);
offset += 4; offset += 4;
@ -244,13 +242,13 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
uint pixelValue = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4)); uint pixelValue = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 4); Span<byte> rowSpan = rowBytes.Slice(offset, 4);
uint diff = TiffUtils.ConvertToUIntLittleEndian(rowSpan); uint diff = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
pixelValue += diff; pixelValue += diff;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, pixelValue); BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, pixelValue);
offset += 4; offset += 4;
@ -278,8 +276,7 @@ internal static class HorizontalPredictor
r += pixel.R; r += pixel.R;
g += pixel.G; g += pixel.G;
b += pixel.B; b += pixel.B;
var rgb = new Rgb24(r, g, b); pixel = new Rgb24(r, g, b);
pixel.FromRgb24(rgb);
} }
} }
} }
@ -305,8 +302,7 @@ internal static class HorizontalPredictor
g += pixel.G; g += pixel.G;
b += pixel.B; b += pixel.B;
a += pixel.A; a += pixel.A;
var rgb = new Rgba32(r, g, b, a); pixel = new Rgba32(r, g, b, a);
pixel.FromRgba32(rgb);
} }
} }
} }
@ -321,29 +317,29 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
ushort r = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort g = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort b = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 2); Span<byte> rowSpan = rowBytes.Slice(offset, 2);
ushort deltaR = TiffUtils.ConvertToUShortBigEndian(rowSpan); ushort deltaR = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
r += deltaR; r += deltaR;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, r); BinaryPrimitives.WriteUInt16BigEndian(rowSpan, r);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaG = TiffUtils.ConvertToUShortBigEndian(rowSpan); ushort deltaG = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
g += deltaG; g += deltaG;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, g); BinaryPrimitives.WriteUInt16BigEndian(rowSpan, g);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaB = TiffUtils.ConvertToUShortBigEndian(rowSpan); ushort deltaB = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
b += deltaB; b += deltaB;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, b); BinaryPrimitives.WriteUInt16BigEndian(rowSpan, b);
offset += 2; offset += 2;
@ -356,29 +352,29 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
ushort r = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort g = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort b = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 2); Span<byte> rowSpan = rowBytes.Slice(offset, 2);
ushort deltaR = TiffUtils.ConvertToUShortLittleEndian(rowSpan); ushort deltaR = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
r += deltaR; r += deltaR;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, r); BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, r);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaG = TiffUtils.ConvertToUShortLittleEndian(rowSpan); ushort deltaG = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
g += deltaG; g += deltaG;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, g); BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, g);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaB = TiffUtils.ConvertToUShortLittleEndian(rowSpan); ushort deltaB = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
b += deltaB; b += deltaB;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, b); BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, b);
offset += 2; offset += 2;
@ -397,37 +393,37 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
ushort r = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort g = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort b = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort a = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2)); ushort a = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 2); Span<byte> rowSpan = rowBytes.Slice(offset, 2);
ushort deltaR = TiffUtils.ConvertToUShortBigEndian(rowSpan); ushort deltaR = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
r += deltaR; r += deltaR;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, r); BinaryPrimitives.WriteUInt16BigEndian(rowSpan, r);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaG = TiffUtils.ConvertToUShortBigEndian(rowSpan); ushort deltaG = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
g += deltaG; g += deltaG;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, g); BinaryPrimitives.WriteUInt16BigEndian(rowSpan, g);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaB = TiffUtils.ConvertToUShortBigEndian(rowSpan); ushort deltaB = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
b += deltaB; b += deltaB;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, b); BinaryPrimitives.WriteUInt16BigEndian(rowSpan, b);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaA = TiffUtils.ConvertToUShortBigEndian(rowSpan); ushort deltaA = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
a += deltaA; a += deltaA;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, a); BinaryPrimitives.WriteUInt16BigEndian(rowSpan, a);
offset += 2; offset += 2;
@ -440,37 +436,37 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
ushort r = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort g = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort b = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
ushort a = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2)); ushort a = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2; offset += 2;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 2); Span<byte> rowSpan = rowBytes.Slice(offset, 2);
ushort deltaR = TiffUtils.ConvertToUShortLittleEndian(rowSpan); ushort deltaR = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
r += deltaR; r += deltaR;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, r); BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, r);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaG = TiffUtils.ConvertToUShortLittleEndian(rowSpan); ushort deltaG = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
g += deltaG; g += deltaG;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, g); BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, g);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaB = TiffUtils.ConvertToUShortLittleEndian(rowSpan); ushort deltaB = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
b += deltaB; b += deltaB;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, b); BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, b);
offset += 2; offset += 2;
rowSpan = rowBytes.Slice(offset, 2); rowSpan = rowBytes.Slice(offset, 2);
ushort deltaA = TiffUtils.ConvertToUShortLittleEndian(rowSpan); ushort deltaA = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
a += deltaA; a += deltaA;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, a); BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, a);
offset += 2; offset += 2;
@ -489,29 +485,29 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
uint r = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint g = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint b = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 4); Span<byte> rowSpan = rowBytes.Slice(offset, 4);
uint deltaR = TiffUtils.ConvertToUIntBigEndian(rowSpan); uint deltaR = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
r += deltaR; r += deltaR;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, r); BinaryPrimitives.WriteUInt32BigEndian(rowSpan, r);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaG = TiffUtils.ConvertToUIntBigEndian(rowSpan); uint deltaG = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
g += deltaG; g += deltaG;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, g); BinaryPrimitives.WriteUInt32BigEndian(rowSpan, g);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaB = TiffUtils.ConvertToUIntBigEndian(rowSpan); uint deltaB = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
b += deltaB; b += deltaB;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, b); BinaryPrimitives.WriteUInt32BigEndian(rowSpan, b);
offset += 4; offset += 4;
@ -524,29 +520,29 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
uint r = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint g = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint b = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 4); Span<byte> rowSpan = rowBytes.Slice(offset, 4);
uint deltaR = TiffUtils.ConvertToUIntLittleEndian(rowSpan); uint deltaR = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
r += deltaR; r += deltaR;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, r); BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, r);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaG = TiffUtils.ConvertToUIntLittleEndian(rowSpan); uint deltaG = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
g += deltaG; g += deltaG;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, g); BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, g);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaB = TiffUtils.ConvertToUIntLittleEndian(rowSpan); uint deltaB = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
b += deltaB; b += deltaB;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, b); BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, b);
offset += 4; offset += 4;
@ -565,37 +561,37 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
uint r = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint g = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint b = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint a = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4)); uint a = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 4); Span<byte> rowSpan = rowBytes.Slice(offset, 4);
uint deltaR = TiffUtils.ConvertToUIntBigEndian(rowSpan); uint deltaR = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
r += deltaR; r += deltaR;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, r); BinaryPrimitives.WriteUInt32BigEndian(rowSpan, r);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaG = TiffUtils.ConvertToUIntBigEndian(rowSpan); uint deltaG = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
g += deltaG; g += deltaG;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, g); BinaryPrimitives.WriteUInt32BigEndian(rowSpan, g);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaB = TiffUtils.ConvertToUIntBigEndian(rowSpan); uint deltaB = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
b += deltaB; b += deltaB;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, b); BinaryPrimitives.WriteUInt32BigEndian(rowSpan, b);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaA = TiffUtils.ConvertToUIntBigEndian(rowSpan); uint deltaA = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
a += deltaA; a += deltaA;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, a); BinaryPrimitives.WriteUInt32BigEndian(rowSpan, a);
offset += 4; offset += 4;
@ -608,37 +604,37 @@ internal static class HorizontalPredictor
{ {
int offset = 0; int offset = 0;
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
uint r = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint g = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint b = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
uint a = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4)); uint a = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4; offset += 4;
for (int x = 1; x < width; x++) for (int x = 1; x < width; x++)
{ {
Span<byte> rowSpan = rowBytes.Slice(offset, 4); Span<byte> rowSpan = rowBytes.Slice(offset, 4);
uint deltaR = TiffUtils.ConvertToUIntLittleEndian(rowSpan); uint deltaR = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
r += deltaR; r += deltaR;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, r); BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, r);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaG = TiffUtils.ConvertToUIntLittleEndian(rowSpan); uint deltaG = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
g += deltaG; g += deltaG;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, g); BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, g);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaB = TiffUtils.ConvertToUIntLittleEndian(rowSpan); uint deltaB = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
b += deltaB; b += deltaB;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, b); BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, b);
offset += 4; offset += 4;
rowSpan = rowBytes.Slice(offset, 4); rowSpan = rowBytes.Slice(offset, 4);
uint deltaA = TiffUtils.ConvertToUIntLittleEndian(rowSpan); uint deltaA = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
a += deltaA; a += deltaA;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, a); BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, a);
offset += 4; offset += 4;

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

@ -11,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'BlackIsZero' photometric interpretation for 16-bit grayscale images. /// Implements the 'BlackIsZero' photometric interpretation for 16-bit grayscale images.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class BlackIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class BlackIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -32,9 +33,8 @@ internal class BlackIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
L16 l16 = TiffUtils.L16Default; L16 l16 = TiffUtilities.L16Default;
var color = default(TPixel); TPixel color = TPixel.FromScaledVector4(Vector4.Zero);
color.FromScaledVector4(Vector4.Zero);
int offset = 0; int offset = 0;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
@ -44,10 +44,10 @@ internal class BlackIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ushort intensity = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); ushort intensity = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); pixelRow[x] = TPixel.FromL16(new(intensity));
} }
} }
else else

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

@ -19,11 +19,9 @@ internal class BlackIsZero1TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
nuint offset = 0; nuint offset = 0;
TPixel colorBlack = default; TPixel colorBlack = TPixel.FromRgba32(Color.Black.ToPixel<Rgba32>());
TPixel colorWhite = default; TPixel colorWhite = TPixel.FromRgba32(Color.White.ToPixel<Rgba32>());
colorBlack.FromRgba32(Color.Black.ToPixel<Rgba32>());
colorWhite.FromRgba32(Color.White.ToPixel<Rgba32>());
ref byte dataRef = ref MemoryMarshal.GetReference(data); ref byte dataRef = ref MemoryMarshal.GetReference(data);
for (nuint y = (uint)top; y < (uint)(top + height); y++) for (nuint y = (uint)top; y < (uint)(top + height); y++)
{ {

12
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'BlackIsZero' photometric interpretation for 24-bit grayscale images. /// Implements the 'BlackIsZero' photometric interpretation for 24-bit grayscale images.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class BlackIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class BlackIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -25,8 +25,6 @@ internal class BlackIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
Span<byte> buffer = stackalloc byte[4]; Span<byte> buffer = stackalloc byte[4];
int bufferStartIdx = this.isBigEndian ? 1 : 0; int bufferStartIdx = this.isBigEndian ? 1 : 0;
@ -40,10 +38,10 @@ internal class BlackIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong intensity = TiffUtils.ConvertToUIntBigEndian(buffer); uint intensity = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(intensity, color); pixelRow[x] = TiffUtilities.ColorScaleTo24Bit<TPixel>(intensity);
} }
} }
else else
@ -51,10 +49,10 @@ internal class BlackIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong intensity = TiffUtils.ConvertToUIntLittleEndian(buffer); uint intensity = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(intensity, color); pixelRow[x] = TiffUtilities.ColorScaleTo24Bit<TPixel>(intensity);
} }
} }
} }

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

@ -10,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'BlackIsZero' photometric interpretation for 32-bit float grayscale images. /// Implements the 'BlackIsZero' photometric interpretation for 32-bit float grayscale images.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class BlackIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class BlackIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -24,8 +25,6 @@ internal class BlackIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
TPixel color = default;
color.FromScaledVector4(Vector4.Zero);
Span<byte> buffer = stackalloc byte[4]; Span<byte> buffer = stackalloc byte[4];
int offset = 0; int offset = 0;
@ -41,9 +40,7 @@ internal class BlackIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
float intensity = BitConverter.ToSingle(buffer); float intensity = BitConverter.ToSingle(buffer);
offset += 4; offset += 4;
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f); pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
color.FromScaledVector4(colorVector);
pixelRow[x] = color;
} }
} }
else else
@ -53,9 +50,7 @@ internal class BlackIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
float intensity = BitConverter.ToSingle(data.Slice(offset, 4)); float intensity = BitConverter.ToSingle(data.Slice(offset, 4));
offset += 4; offset += 4;
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f); pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
color.FromScaledVector4(colorVector);
pixelRow[x] = color;
} }
} }
} }

13
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero32TiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'BlackIsZero' photometric interpretation for 32-bit grayscale images. /// Implements the 'BlackIsZero' photometric interpretation for 32-bit grayscale images.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class BlackIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class BlackIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -25,9 +25,6 @@ internal class BlackIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
int offset = 0; int offset = 0;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -36,20 +33,20 @@ internal class BlackIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong intensity = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); uint intensity = TiffUtilities.ConvertToUIntBigEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(intensity, color); pixelRow[x] = TiffUtilities.ColorScaleTo32Bit<TPixel>(intensity);
} }
} }
else else
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong intensity = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); uint intensity = TiffUtilities.ConvertToUIntLittleEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(intensity, color); pixelRow[x] = TiffUtilities.ColorScaleTo32Bit<TPixel>(intensity);
} }
} }
} }

25
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor{TPixel}.cs

@ -9,47 +9,30 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'BlackIsZero' photometric interpretation (optimized for 4-bit grayscale images). /// Implements the 'BlackIsZero' photometric interpretation (optimized for 4-bit grayscale images).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class BlackIsZero4TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class BlackIsZero4TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
int offset = 0; int offset = 0;
bool isOddWidth = (width & 1) == 1; bool isOddWidth = (width & 1) == 1;
var l8 = default(L8);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan(y); Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan(y);
for (int x = left; x < left + width - 1;) for (int x = left; x < left + width - 1;)
{ {
byte byteData = data[offset++]; byte byteData = data[offset++];
pixelRowSpan[x++] = TPixel.FromL8(new((byte)(((byteData & 0xF0) >> 4) * 17)));
byte intensity1 = (byte)(((byteData & 0xF0) >> 4) * 17); pixelRowSpan[x++] = TPixel.FromL8(new((byte)((byteData & 0x0F) * 17)));
l8.PackedValue = intensity1;
color.FromL8(l8);
pixelRowSpan[x++] = color;
byte intensity2 = (byte)((byteData & 0x0F) * 17);
l8.PackedValue = intensity2;
color.FromL8(l8);
pixelRowSpan[x++] = color;
} }
if (isOddWidth) if (isOddWidth)
{ {
byte byteData = data[offset++]; byte byteData = data[offset++];
pixelRowSpan[left + width - 1] = TPixel.FromL8(new((byte)(((byteData & 0xF0) >> 4) * 17)));
byte intensity1 = (byte)(((byteData & 0xF0) >> 4) * 17);
l8.PackedValue = intensity1;
color.FromL8(l8);
pixelRowSpan[left + width - 1] = color;
} }
} }
} }

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

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,25 +10,23 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'BlackIsZero' photometric interpretation (for all bit depths). /// Implements the 'BlackIsZero' photometric interpretation (for all bit depths).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class BlackIsZeroTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class BlackIsZeroTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
private readonly ushort bitsPerSample0; private readonly ushort bitsPerSample0;
private readonly float factor; private readonly float factor;
public BlackIsZeroTiffColor(TiffBitsPerSample bitsPerSample) public BlackIsZeroTiffColor(TiffBitsPerSample bitsPerSample)
{ {
this.bitsPerSample0 = bitsPerSample.Channel0; this.bitsPerSample0 = bitsPerSample.Channel0;
this.factor = (1 << this.bitsPerSample0) - 1.0f; this.factor = (1 << this.bitsPerSample0) - 1f;
} }
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel); BitReader bitReader = new(data);
var bitReader = new BitReader(data);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -38,9 +35,7 @@ internal class BlackIsZeroTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
int value = bitReader.ReadBits(this.bitsPerSample0); int value = bitReader.ReadBits(this.bitsPerSample0);
float intensity = value / this.factor; float intensity = value / this.factor;
pixelRow[x] = TPixel.FromScaledVector4(new(intensity, intensity, intensity, 1f));
color.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1.0f));
pixelRow[x] = color;
} }
bitReader.NextRow(); bitReader.NextRow();

11
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLabPlanarTiffColor{TPixel}.cs

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers; using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.ColorSpaces; using SixLabors.ImageSharp.ColorSpaces;
using SixLabors.ImageSharp.ColorSpaces.Conversion; using SixLabors.ImageSharp.ColorSpaces.Conversion;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
@ -13,6 +12,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements decoding pixel data with photometric interpretation of type 'CieLab' with the planar configuration. /// Implements decoding pixel data with photometric interpretation of type 'CieLab' with the planar configuration.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class CieLabPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class CieLabPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -23,11 +23,10 @@ internal class CieLabPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
Span<byte> l = data[0].GetSpan();
Span<byte> a = data[1].GetSpan();
Span<byte> b = data[2].GetSpan(); Span<byte> b = data[2].GetSpan();
Span<byte> a = data[1].GetSpan();
Span<byte> l = data[0].GetSpan();
TPixel color = default;
int offset = 0; int offset = 0;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -36,9 +35,7 @@ internal class CieLabPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel
{ {
CieLab lab = new((l[offset] & 0xFF) * 100f * Inv255, (sbyte)a[offset], (sbyte)b[offset]); CieLab lab = new((l[offset] & 0xFF) * 100f * Inv255, (sbyte)a[offset], (sbyte)b[offset]);
Rgb rgb = ColorSpaceConverter.ToRgb(lab); Rgb rgb = ColorSpaceConverter.ToRgb(lab);
pixelRow[x] = TPixel.FromScaledVector4(new(rgb.R, rgb.G, rgb.B, 1.0f));
color.FromScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
pixelRow[x] = color;
offset++; offset++;
} }

10
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/CieLabTiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.ColorSpaces; using SixLabors.ImageSharp.ColorSpaces;
using SixLabors.ImageSharp.ColorSpaces.Conversion; using SixLabors.ImageSharp.ColorSpaces.Conversion;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
@ -12,17 +11,16 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements decoding pixel data with photometric interpretation of type 'CieLab'. /// Implements decoding pixel data with photometric interpretation of type 'CieLab'.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class CieLabTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class CieLabTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
private static readonly ColorSpaceConverter ColorSpaceConverter = new(); private static readonly ColorSpaceConverter ColorSpaceConverter = new();
private const float Inv255 = 1f / 255f;
private const float Inv255 = 1.0f / 255.0f;
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
TPixel color = default;
int offset = 0; int offset = 0;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -33,9 +31,7 @@ internal class CieLabTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float l = (data[offset] & 0xFF) * 100f * Inv255; float l = (data[offset] & 0xFF) * 100f * Inv255;
CieLab lab = new(l, (sbyte)data[offset + 1], (sbyte)data[offset + 2]); CieLab lab = new(l, (sbyte)data[offset + 1], (sbyte)data[offset + 2]);
Rgb rgb = ColorSpaceConverter.ToRgb(lab); Rgb rgb = ColorSpaceConverter.ToRgb(lab);
pixelRow[x] = TPixel.FromScaledVector4(new(rgb.R, rgb.G, rgb.B, 1f));
color.FromScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
pixelRow[x] = color;
offset += 3; offset += 3;
} }

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

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.ColorSpaces; using SixLabors.ImageSharp.ColorSpaces;
using SixLabors.ImageSharp.ColorSpaces.Conversion; using SixLabors.ImageSharp.ColorSpaces.Conversion;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
@ -12,12 +11,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
internal class CmykTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class CmykTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
private const float Inv255 = 1 / 255.0f; private const float Inv255 = 1f / 255f;
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
TPixel color = default;
int offset = 0; int offset = 0;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -26,9 +24,7 @@ internal class CmykTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
Cmyk cmyk = new(data[offset] * Inv255, data[offset + 1] * Inv255, data[offset + 2] * Inv255, data[offset + 3] * Inv255); Cmyk cmyk = new(data[offset] * Inv255, data[offset + 1] * Inv255, data[offset + 2] * Inv255, data[offset + 3] * Inv255);
Rgb rgb = ColorSpaceConverter.ToRgb(in cmyk); Rgb rgb = ColorSpaceConverter.ToRgb(in cmyk);
pixelRow[x] = TPixel.FromScaledVector4(new(rgb.R, rgb.G, rgb.B, 1.0f));
color.FromScaledVector4(new Vector4(rgb.R, rgb.G, rgb.B, 1.0f));
pixelRow[x] = color;
offset += 4; offset += 4;
} }

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

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'PaletteTiffColor' photometric interpretation (for all bit depths). /// Implements the 'PaletteTiffColor' photometric interpretation (for all bit depths).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class PaletteTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class PaletteTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -18,8 +18,11 @@ internal class PaletteTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
private readonly TPixel[] palette; private readonly TPixel[] palette;
private const float InvMax = 1.0f / 65535F; private const float InvMax = 1f / 65535f;
/// <summary>
/// Initializes a new instance of the <see cref="PaletteTiffColor{TPixel}"/> class.
/// </summary>
/// <param name="bitsPerSample">The number of bits per sample for each pixel.</param> /// <param name="bitsPerSample">The number of bits per sample for each pixel.</param>
/// <param name="colorMap">The RGB color lookup table to use for decoding the image.</param> /// <param name="colorMap">The RGB color lookup table to use for decoding the image.</param>
public PaletteTiffColor(TiffBitsPerSample bitsPerSample, ushort[] colorMap) public PaletteTiffColor(TiffBitsPerSample bitsPerSample, ushort[] colorMap)
@ -32,7 +35,7 @@ internal class PaletteTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var bitReader = new BitReader(data); BitReader bitReader = new(data);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -49,7 +52,7 @@ internal class PaletteTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
private static TPixel[] GeneratePalette(ushort[] colorMap, int colorCount) private static TPixel[] GeneratePalette(ushort[] colorMap, int colorCount)
{ {
var palette = new TPixel[colorCount]; TPixel[] palette = new TPixel[colorCount];
const int rOffset = 0; const int rOffset = 0;
int gOffset = colorCount; int gOffset = colorCount;
@ -60,7 +63,7 @@ internal class PaletteTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float r = colorMap[rOffset + i] * InvMax; float r = colorMap[rOffset + i] * InvMax;
float g = colorMap[gOffset + i] * InvMax; float g = colorMap[gOffset + i] * InvMax;
float b = colorMap[bOffset + i] * InvMax; float b = colorMap[bOffset + i] * InvMax;
palette[i].FromScaledVector4(new Vector4(r, g, b, 1.0f)); palette[i] = TPixel.FromScaledVector4(new(r, g, b, 1f));
} }
return palette; return palette;

15
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,11 +10,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with 16 bits for each channel. /// Implements the 'RGB' photometric interpretation with 16 bits for each channel.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgb161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class Rgb161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
private readonly bool isBigEndian; private readonly bool isBigEndian;
private readonly Configuration configuration; private readonly Configuration configuration;
/// <summary> /// <summary>
@ -32,10 +31,6 @@ internal class Rgb161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
Rgba64 rgba = TiffUtils.Rgba64Default;
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
int offset = 0; int offset = 0;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
@ -46,14 +41,14 @@ internal class Rgb161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
ulong g = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
ulong b = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
pixelRow[x] = TiffUtils.ColorFromRgb64(rgba, r, g, b, color); pixelRow[x] = TPixel.FromRgb48(new(r, g, b));
} }
} }
else else

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

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers; using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -12,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with 'Planar' layout for each color channel with 16 bit. /// Implements the 'RGB' photometric interpretation with 'Planar' layout for each color channel with 16 bit.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgb16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class Rgb16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -26,10 +26,6 @@ internal class Rgb16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
Rgba64 rgba = TiffUtils.Rgba64Default;
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
Span<byte> redData = data[0].GetSpan(); Span<byte> redData = data[0].GetSpan();
Span<byte> greenData = data[1].GetSpan(); Span<byte> greenData = data[1].GetSpan();
Span<byte> blueData = data[2].GetSpan(); Span<byte> blueData = data[2].GetSpan();
@ -42,26 +38,26 @@ internal class Rgb16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUShortBigEndian(redData.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortBigEndian(redData.Slice(offset, 2));
ulong g = TiffUtils.ConvertToUShortBigEndian(greenData.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortBigEndian(greenData.Slice(offset, 2));
ulong b = TiffUtils.ConvertToUShortBigEndian(blueData.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortBigEndian(blueData.Slice(offset, 2));
offset += 2; offset += 2;
pixelRow[x] = TiffUtils.ColorFromRgb64(rgba, r, g, b, color); pixelRow[x] = TPixel.FromRgb48(new(r, g, b));
} }
} }
else else
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUShortLittleEndian(redData.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortLittleEndian(redData.Slice(offset, 2));
ulong g = TiffUtils.ConvertToUShortLittleEndian(greenData.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortLittleEndian(greenData.Slice(offset, 2));
ulong b = TiffUtils.ConvertToUShortLittleEndian(blueData.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortLittleEndian(blueData.Slice(offset, 2));
offset += 2; offset += 2;
pixelRow[x] = TiffUtils.ColorFromRgb64(rgba, r, g, b, color); pixelRow[x] = TPixel.FromRgb48(new(r, g, b));
} }
} }
} }

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

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with 24 bits for each channel. /// Implements the 'RGB' photometric interpretation with 24 bits for each channel.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgb242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class Rgb242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -25,8 +25,6 @@ internal class Rgb242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
int offset = 0; int offset = 0;
Span<byte> buffer = stackalloc byte[4]; Span<byte> buffer = stackalloc byte[4];
int bufferStartIdx = this.isBigEndian ? 1 : 0; int bufferStartIdx = this.isBigEndian ? 1 : 0;
@ -41,18 +39,18 @@ internal class Rgb242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong r = TiffUtils.ConvertToUIntBigEndian(buffer); uint r = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong g = TiffUtils.ConvertToUIntBigEndian(buffer); uint g = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong b = TiffUtils.ConvertToUIntBigEndian(buffer); uint b = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(r, g, b, color); pixelRow[x] = TiffUtilities.ColorScaleTo24Bit<TPixel>(r, g, b);
} }
} }
else else
@ -60,18 +58,18 @@ internal class Rgb242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong r = TiffUtils.ConvertToUIntLittleEndian(buffer); uint r = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong g = TiffUtils.ConvertToUIntLittleEndian(buffer); uint g = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong b = TiffUtils.ConvertToUIntLittleEndian(buffer); uint b = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(r, g, b, color); pixelRow[x] = TiffUtilities.ColorScaleTo24Bit<TPixel>(r, g, b);
} }
} }
} }

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

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers; using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -12,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with 'Planar' layout for each color channel with 24 bit. /// Implements the 'RGB' photometric interpretation with 'Planar' layout for each color channel with 24 bit.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgb24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class Rgb24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -26,8 +26,6 @@ internal class Rgb24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
Span<byte> buffer = stackalloc byte[4]; Span<byte> buffer = stackalloc byte[4];
int bufferStartIdx = this.isBigEndian ? 1 : 0; int bufferStartIdx = this.isBigEndian ? 1 : 0;
@ -45,15 +43,15 @@ internal class Rgb24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
redData.Slice(offset, 3).CopyTo(bufferSpan); redData.Slice(offset, 3).CopyTo(bufferSpan);
ulong r = TiffUtils.ConvertToUIntBigEndian(buffer); uint r = TiffUtilities.ConvertToUIntBigEndian(buffer);
greenData.Slice(offset, 3).CopyTo(bufferSpan); greenData.Slice(offset, 3).CopyTo(bufferSpan);
ulong g = TiffUtils.ConvertToUIntBigEndian(buffer); uint g = TiffUtilities.ConvertToUIntBigEndian(buffer);
blueData.Slice(offset, 3).CopyTo(bufferSpan); blueData.Slice(offset, 3).CopyTo(bufferSpan);
ulong b = TiffUtils.ConvertToUIntBigEndian(buffer); uint b = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(r, g, b, color); pixelRow[x] = TiffUtilities.ColorScaleTo24Bit<TPixel>(r, g, b);
} }
} }
else else
@ -61,15 +59,15 @@ internal class Rgb24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
redData.Slice(offset, 3).CopyTo(bufferSpan); redData.Slice(offset, 3).CopyTo(bufferSpan);
ulong r = TiffUtils.ConvertToUIntLittleEndian(buffer); uint r = TiffUtilities.ConvertToUIntLittleEndian(buffer);
greenData.Slice(offset, 3).CopyTo(bufferSpan); greenData.Slice(offset, 3).CopyTo(bufferSpan);
ulong g = TiffUtils.ConvertToUIntLittleEndian(buffer); uint g = TiffUtilities.ConvertToUIntLittleEndian(buffer);
blueData.Slice(offset, 3).CopyTo(bufferSpan); blueData.Slice(offset, 3).CopyTo(bufferSpan);
ulong b = TiffUtils.ConvertToUIntLittleEndian(buffer); uint b = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(r, g, b, color); pixelRow[x] = TiffUtilities.ColorScaleTo24Bit<TPixel>(r, g, b);
} }
} }
} }

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

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with 32 bits for each channel. /// Implements the 'RGB' photometric interpretation with 32 bits for each channel.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgb323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class Rgb323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -25,8 +25,6 @@ internal class Rgb323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
int offset = 0; int offset = 0;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
@ -37,32 +35,32 @@ internal class Rgb323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntBigEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong g = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntBigEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong b = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntBigEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, color); pixelRow[x] = TiffUtilities.ColorScaleTo32Bit<TPixel>(r, g, b);
} }
} }
else else
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntLittleEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong g = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntLittleEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong b = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntLittleEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, color); pixelRow[x] = TiffUtilities.ColorScaleTo32Bit<TPixel>(r, g, b);
} }
} }
} }

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

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers; using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -12,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with 'Planar' layout for each color channel with 32 bit. /// Implements the 'RGB' photometric interpretation with 'Planar' layout for each color channel with 32 bit.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgb32PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class Rgb32PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -26,9 +26,6 @@ internal class Rgb32PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
Span<byte> redData = data[0].GetSpan(); Span<byte> redData = data[0].GetSpan();
Span<byte> greenData = data[1].GetSpan(); Span<byte> greenData = data[1].GetSpan();
Span<byte> blueData = data[2].GetSpan(); Span<byte> blueData = data[2].GetSpan();
@ -41,26 +38,26 @@ internal class Rgb32PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUIntBigEndian(redData.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntBigEndian(redData.Slice(offset, 4));
ulong g = TiffUtils.ConvertToUIntBigEndian(greenData.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntBigEndian(greenData.Slice(offset, 4));
ulong b = TiffUtils.ConvertToUIntBigEndian(blueData.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntBigEndian(blueData.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, color); pixelRow[x] = TiffUtilities.ColorScaleTo32Bit<TPixel>(r, g, b);
} }
} }
else else
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUIntLittleEndian(redData.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntLittleEndian(redData.Slice(offset, 4));
ulong g = TiffUtils.ConvertToUIntLittleEndian(greenData.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntLittleEndian(greenData.Slice(offset, 4));
ulong b = TiffUtils.ConvertToUIntLittleEndian(blueData.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntLittleEndian(blueData.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(r, g, b, color); pixelRow[x] = TiffUtilities.ColorScaleTo32Bit<TPixel>(r, g, b);
} }
} }
} }

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

@ -9,17 +9,15 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation for 4 bits per color channel images. /// Implements the 'RGB' photometric interpretation for 4 bits per color channel images.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgb444TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class Rgb444TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
int offset = 0; int offset = 0;
var bgra = default(Bgra4444);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y); Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y);
@ -31,9 +29,8 @@ internal class Rgb444TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
offset++; offset++;
byte b = (byte)((data[offset] & 0xF0) >> 4); byte b = (byte)((data[offset] & 0xF0) >> 4);
bgra.PackedValue = ToBgraPackedValue(b, g, r); Bgra4444 bgra = new() { PackedValue = ToBgraPackedValue(b, g, r) };
color.FromScaledVector4(bgra.ToScaledVector4()); pixelRow[x] = TPixel.FromScaledVector4(bgra.ToScaledVector4());
pixelRow[x] = color;
if (x + 1 >= pixelRow.Length) if (x + 1 >= pixelRow.Length)
{ {
offset++; offset++;
@ -47,8 +44,7 @@ internal class Rgb444TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
offset++; offset++;
bgra.PackedValue = ToBgraPackedValue(b, g, r); bgra.PackedValue = ToBgraPackedValue(b, g, r);
color.FromScaledVector4(bgra.ToScaledVector4()); pixelRow[x + 1] = TPixel.FromScaledVector4(bgra.ToScaledVector4());
pixelRow[x + 1] = color;
} }
} }
} }

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

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -10,6 +9,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with 32 bits for each channel. /// Implements the 'RGB' photometric interpretation with 32 bits for each channel.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class RgbFloat323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class RgbFloat323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -24,8 +24,6 @@ internal class RgbFloat323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
int offset = 0; int offset = 0;
Span<byte> buffer = stackalloc byte[4]; Span<byte> buffer = stackalloc byte[4];
@ -52,9 +50,7 @@ internal class RgbFloat323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float b = BitConverter.ToSingle(buffer); float b = BitConverter.ToSingle(buffer);
offset += 4; offset += 4;
var colorVector = new Vector4(r, g, b, 1.0f); pixelRow[x] = TPixel.FromScaledVector4(new(r, g, b, 1f));
color.FromScaledVector4(colorVector);
pixelRow[x] = color;
} }
} }
else else
@ -70,9 +66,7 @@ internal class RgbFloat323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float b = BitConverter.ToSingle(data.Slice(offset, 4)); float b = BitConverter.ToSingle(data.Slice(offset, 4));
offset += 4; offset += 4;
var colorVector = new Vector4(r, g, b, 1.0f); pixelRow[x] = TPixel.FromScaledVector4(new(r, g, b, 1f));
color.FromScaledVector4(colorVector);
pixelRow[x] = color;
} }
} }
} }

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

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers; using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -12,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with 'Planar' layout (for all bit depths). /// Implements the 'RGB' photometric interpretation with 'Planar' layout (for all bit depths).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class RgbPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class RgbPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -49,11 +49,9 @@ internal class RgbPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
/// <param name="height">The height of the image block.</param> /// <param name="height">The height of the image block.</param>
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel); BitReader rBitReader = new(data[0].GetSpan());
BitReader gBitReader = new(data[1].GetSpan());
var rBitReader = new BitReader(data[0].GetSpan()); BitReader bBitReader = new(data[2].GetSpan());
var gBitReader = new BitReader(data[1].GetSpan());
var bBitReader = new BitReader(data[2].GetSpan());
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -64,8 +62,7 @@ internal class RgbPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
float g = gBitReader.ReadBits(this.bitsPerSampleG) / this.gFactor; float g = gBitReader.ReadBits(this.bitsPerSampleG) / this.gFactor;
float b = bBitReader.ReadBits(this.bitsPerSampleB) / this.bFactor; float b = bBitReader.ReadBits(this.bitsPerSampleB) / this.bFactor;
color.FromScaledVector4(new Vector4(r, g, b, 1.0f)); pixelRow[x] = TPixel.FromScaledVector4(new(r, g, b, 1f));
pixelRow[x] = color;
} }
rBitReader.NextRow(); rBitReader.NextRow();

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

@ -11,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation (for all bit depths). /// Implements the 'RGB' photometric interpretation (for all bit depths).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class RgbTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class RgbTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -40,9 +41,7 @@ internal class RgbTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel); BitReader bitReader = new(data);
var bitReader = new BitReader(data);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -53,8 +52,7 @@ internal class RgbTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float g = bitReader.ReadBits(this.bitsPerSampleG) / this.gFactor; float g = bitReader.ReadBits(this.bitsPerSampleG) / this.gFactor;
float b = bitReader.ReadBits(this.bitsPerSampleB) / this.bFactor; float b = bitReader.ReadBits(this.bitsPerSampleB) / this.bFactor;
color.FromScaledVector4(new Vector4(r, g, b, 1.0f)); pixelRow[x] = TPixel.FromScaledVector4(new Vector4(r, g, b, 1f));
pixelRow[x] = color;
} }
bitReader.NextRow(); bitReader.NextRow();

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

@ -13,6 +13,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with an alpha channel and with 16 bits for each channel. /// Implements the 'RGB' photometric interpretation with an alpha channel and with 16 bits for each channel.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgba16161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class Rgba16161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -29,8 +30,8 @@ internal class Rgba16161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// </summary> /// </summary>
/// <param name="configuration">The configuration.</param> /// <param name="configuration">The configuration.</param>
/// <param name="memoryAllocator">The memory allocator.</param> /// <param name="memoryAllocator">The memory allocator.</param>
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
/// <param name="extraSamplesType">The type of the extra samples.</param> /// <param name="extraSamplesType">The type of the extra samples.</param>
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
public Rgba16161616TiffColor(Configuration configuration, MemoryAllocator memoryAllocator, TiffExtraSampleType? extraSamplesType, bool isBigEndian) public Rgba16161616TiffColor(Configuration configuration, MemoryAllocator memoryAllocator, TiffExtraSampleType? extraSamplesType, bool isBigEndian)
{ {
this.configuration = configuration; this.configuration = configuration;
@ -42,15 +43,11 @@ internal class Rgba16161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
Rgba64 rgba = TiffUtils.Rgba64Default;
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData; bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData;
int offset = 0; int offset = 0;
using IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null; using IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null;
Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : Span<Vector4>.Empty; Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : [];
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
@ -59,18 +56,18 @@ internal class Rgba16161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
ulong g = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
ulong b = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
ulong a = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2)); ushort a = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorFromRgba64Premultiplied(rgba, r, g, b, a, color) : ? TiffUtilities.ColorFromRgba64Premultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorFromRgba64(rgba, r, g, b, a, color); : TPixel.FromRgba64(new(r, g, b, a));
} }
} }
else else

34
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba16PlanarTiffColor{TPixel}.cs

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers; using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -12,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with an alpha channel and with 'Planar' layout for each color channel with 16 bit. /// Implements the 'RGB' photometric interpretation with an alpha channel and with 'Planar' layout for each color channel with 16 bit.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgba16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class Rgba16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -33,10 +33,6 @@ internal class Rgba16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
Rgba64 rgba = TiffUtils.Rgba64Default;
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
Span<byte> redData = data[0].GetSpan(); Span<byte> redData = data[0].GetSpan();
Span<byte> greenData = data[1].GetSpan(); Span<byte> greenData = data[1].GetSpan();
Span<byte> blueData = data[2].GetSpan(); Span<byte> blueData = data[2].GetSpan();
@ -51,32 +47,32 @@ internal class Rgba16PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUShortBigEndian(redData.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortBigEndian(redData.Slice(offset, 2));
ulong g = TiffUtils.ConvertToUShortBigEndian(greenData.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortBigEndian(greenData.Slice(offset, 2));
ulong b = TiffUtils.ConvertToUShortBigEndian(blueData.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortBigEndian(blueData.Slice(offset, 2));
ulong a = TiffUtils.ConvertToUShortBigEndian(alphaData.Slice(offset, 2)); ushort a = TiffUtilities.ConvertToUShortBigEndian(alphaData.Slice(offset, 2));
offset += 2; offset += 2;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorFromRgba64Premultiplied(rgba, r, g, b, a, color) : ? TiffUtilities.ColorFromRgba64Premultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorFromRgba64(rgba, r, g, b, a, color); : TPixel.FromRgba64(new(r, g, b, a));
} }
} }
else else
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUShortLittleEndian(redData.Slice(offset, 2)); ushort r = TiffUtilities.ConvertToUShortLittleEndian(redData.Slice(offset, 2));
ulong g = TiffUtils.ConvertToUShortLittleEndian(greenData.Slice(offset, 2)); ushort g = TiffUtilities.ConvertToUShortLittleEndian(greenData.Slice(offset, 2));
ulong b = TiffUtils.ConvertToUShortLittleEndian(blueData.Slice(offset, 2)); ushort b = TiffUtilities.ConvertToUShortLittleEndian(blueData.Slice(offset, 2));
ulong a = TiffUtils.ConvertToUShortLittleEndian(alphaData.Slice(offset, 2)); ushort a = TiffUtilities.ConvertToUShortLittleEndian(alphaData.Slice(offset, 2));
offset += 2; offset += 2;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorFromRgba64Premultiplied(rgba, r, g, b, a, color) : ? TiffUtilities.ColorFromRgba64Premultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorFromRgba64(rgba, r, g, b, a, color); : TPixel.FromRgba64(new(r, g, b, a));
} }
} }
} }

33
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24242424TiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with an alpha channel and with 24 bits for each channel. /// Implements the 'RGB' photometric interpretation with an alpha channel and with 24 bits for each channel.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgba24242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class Rgba24242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -32,9 +32,6 @@ internal class Rgba24242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData; bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData;
int offset = 0; int offset = 0;
@ -51,24 +48,24 @@ internal class Rgba24242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong r = TiffUtils.ConvertToUIntBigEndian(buffer); uint r = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong g = TiffUtils.ConvertToUIntBigEndian(buffer); uint g = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong b = TiffUtils.ConvertToUIntBigEndian(buffer); uint b = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong a = TiffUtils.ConvertToUIntBigEndian(buffer); uint a = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorScaleTo24BitPremultiplied(r, g, b, a, color) : ? TiffUtilities.ColorScaleTo24BitPremultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorScaleTo24Bit(r, g, b, a, color); : TiffUtilities.ColorScaleTo24Bit<TPixel>(r, g, b, a);
} }
} }
else else
@ -76,24 +73,24 @@ internal class Rgba24242424TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong r = TiffUtils.ConvertToUIntLittleEndian(buffer); uint r = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong g = TiffUtils.ConvertToUIntLittleEndian(buffer); uint g = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong b = TiffUtils.ConvertToUIntLittleEndian(buffer); uint b = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong a = TiffUtils.ConvertToUIntLittleEndian(buffer); uint a = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorScaleTo24BitPremultiplied(r, g, b, a, color) : ? TiffUtilities.ColorScaleTo24BitPremultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorScaleTo24Bit(r, g, b, a, color); : TiffUtilities.ColorScaleTo24Bit<TPixel>(r, g, b, a);
} }
} }
} }

32
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24PlanarTiffColor{TPixel}.cs

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers; using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -12,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with an alpha channel and with 'Planar' layout for each color channel with 24 bit. /// Implements the 'RGB' photometric interpretation with an alpha channel and with 'Planar' layout for each color channel with 24 bit.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgba24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class Rgba24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -33,8 +33,6 @@ internal class Rgba24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
Span<byte> buffer = stackalloc byte[4]; Span<byte> buffer = stackalloc byte[4];
int bufferStartIdx = this.isBigEndian ? 1 : 0; int bufferStartIdx = this.isBigEndian ? 1 : 0;
@ -54,19 +52,19 @@ internal class Rgba24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
redData.Slice(offset, 3).CopyTo(bufferSpan); redData.Slice(offset, 3).CopyTo(bufferSpan);
ulong r = TiffUtils.ConvertToUIntBigEndian(buffer); uint r = TiffUtilities.ConvertToUIntBigEndian(buffer);
greenData.Slice(offset, 3).CopyTo(bufferSpan); greenData.Slice(offset, 3).CopyTo(bufferSpan);
ulong g = TiffUtils.ConvertToUIntBigEndian(buffer); uint g = TiffUtilities.ConvertToUIntBigEndian(buffer);
blueData.Slice(offset, 3).CopyTo(bufferSpan); blueData.Slice(offset, 3).CopyTo(bufferSpan);
ulong b = TiffUtils.ConvertToUIntBigEndian(buffer); uint b = TiffUtilities.ConvertToUIntBigEndian(buffer);
alphaData.Slice(offset, 3).CopyTo(bufferSpan); alphaData.Slice(offset, 3).CopyTo(bufferSpan);
ulong a = TiffUtils.ConvertToUIntBigEndian(buffer); uint a = TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorScaleTo24BitPremultiplied(r, g, b, a, color) : ? TiffUtilities.ColorScaleTo24BitPremultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorScaleTo24Bit(r, g, b, a, color); : TiffUtilities.ColorScaleTo24Bit<TPixel>(r, g, b, a);
} }
} }
else else
@ -74,19 +72,19 @@ internal class Rgba24PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
redData.Slice(offset, 3).CopyTo(bufferSpan); redData.Slice(offset, 3).CopyTo(bufferSpan);
ulong r = TiffUtils.ConvertToUIntLittleEndian(buffer); uint r = TiffUtilities.ConvertToUIntLittleEndian(buffer);
greenData.Slice(offset, 3).CopyTo(bufferSpan); greenData.Slice(offset, 3).CopyTo(bufferSpan);
ulong g = TiffUtils.ConvertToUIntLittleEndian(buffer); uint g = TiffUtilities.ConvertToUIntLittleEndian(buffer);
blueData.Slice(offset, 3).CopyTo(bufferSpan); blueData.Slice(offset, 3).CopyTo(bufferSpan);
ulong b = TiffUtils.ConvertToUIntLittleEndian(buffer); uint b = TiffUtilities.ConvertToUIntLittleEndian(buffer);
alphaData.Slice(offset, 3).CopyTo(bufferSpan); alphaData.Slice(offset, 3).CopyTo(bufferSpan);
ulong a = TiffUtils.ConvertToUIntLittleEndian(buffer); uint a = TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorScaleTo24BitPremultiplied(r, g, b, a, color) : ? TiffUtilities.ColorScaleTo24BitPremultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorScaleTo24Bit(r, g, b, a, color); : TiffUtilities.ColorScaleTo24Bit<TPixel>(r, g, b, a);
} }
} }
} }

33
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba32323232TiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with an alpha channel and with 32 bits for each channel. /// Implements the 'RGB' photometric interpretation with an alpha channel and with 32 bits for each channel.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgba32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class Rgba32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -32,9 +32,6 @@ internal class Rgba32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData; bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData;
int offset = 0; int offset = 0;
@ -46,42 +43,42 @@ internal class Rgba32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntBigEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong g = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntBigEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong b = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntBigEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong a = TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); uint a = TiffUtilities.ConvertToUIntBigEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorScaleTo32BitPremultiplied(r, g, b, a, color) : ? TiffUtilities.ColorScaleTo32BitPremultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorScaleTo32Bit(r, g, b, a, color); : TiffUtilities.ColorScaleTo32Bit<TPixel>(r, g, b, a);
} }
} }
else else
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntLittleEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong g = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntLittleEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong b = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntLittleEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
ulong a = TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); uint a = TiffUtilities.ConvertToUIntLittleEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorScaleTo32BitPremultiplied(r, g, b, a, color) : ? TiffUtilities.ColorScaleTo32BitPremultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorScaleTo32Bit(r, g, b, a, color); : TiffUtilities.ColorScaleTo32Bit<TPixel>(r, g, b, a);
} }
} }
} }

34
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba32PlanarTiffColor{TPixel}.cs

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers; using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -12,11 +11,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with an alpha channel and a 'Planar' layout for each color channel with 32 bit. /// Implements the 'RGB' photometric interpretation with an alpha channel and a 'Planar' layout for each color channel with 32 bit.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgba32PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class Rgba32PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
private readonly bool isBigEndian; private readonly bool isBigEndian;
private readonly TiffExtraSampleType? extraSamplesType; private readonly TiffExtraSampleType? extraSamplesType;
/// <summary> /// <summary>
@ -33,9 +32,6 @@ internal class Rgba32PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
Span<byte> redData = data[0].GetSpan(); Span<byte> redData = data[0].GetSpan();
Span<byte> greenData = data[1].GetSpan(); Span<byte> greenData = data[1].GetSpan();
Span<byte> blueData = data[2].GetSpan(); Span<byte> blueData = data[2].GetSpan();
@ -50,32 +46,32 @@ internal class Rgba32PlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUIntBigEndian(redData.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntBigEndian(redData.Slice(offset, 4));
ulong g = TiffUtils.ConvertToUIntBigEndian(greenData.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntBigEndian(greenData.Slice(offset, 4));
ulong b = TiffUtils.ConvertToUIntBigEndian(blueData.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntBigEndian(blueData.Slice(offset, 4));
ulong a = TiffUtils.ConvertToUIntBigEndian(alphaData.Slice(offset, 4)); uint a = TiffUtilities.ConvertToUIntBigEndian(alphaData.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorScaleTo32BitPremultiplied(r, g, b, a, color) : ? TiffUtilities.ColorScaleTo32BitPremultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorScaleTo32Bit(r, g, b, a, color); : TiffUtilities.ColorScaleTo32Bit<TPixel>(r, g, b, a);
} }
} }
else else
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong r = TiffUtils.ConvertToUIntLittleEndian(redData.Slice(offset, 4)); uint r = TiffUtilities.ConvertToUIntLittleEndian(redData.Slice(offset, 4));
ulong g = TiffUtils.ConvertToUIntLittleEndian(greenData.Slice(offset, 4)); uint g = TiffUtilities.ConvertToUIntLittleEndian(greenData.Slice(offset, 4));
ulong b = TiffUtils.ConvertToUIntLittleEndian(blueData.Slice(offset, 4)); uint b = TiffUtilities.ConvertToUIntLittleEndian(blueData.Slice(offset, 4));
ulong a = TiffUtils.ConvertToUIntLittleEndian(alphaData.Slice(offset, 4)); uint a = TiffUtilities.ConvertToUIntLittleEndian(alphaData.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = hasAssociatedAlpha ? pixelRow[x] = hasAssociatedAlpha
TiffUtils.ColorScaleTo32BitPremultiplied(r, g, b, a, color) : ? TiffUtilities.ColorScaleTo32BitPremultiplied<TPixel>(r, g, b, a)
TiffUtils.ColorScaleTo32Bit(r, g, b, a, color); : TiffUtilities.ColorScaleTo32Bit<TPixel>(r, g, b, a);
} }
} }
} }

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

@ -12,6 +12,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with an alpha channel and 8 bits per channel. /// Implements the 'RGB' photometric interpretation with an alpha channel and 8 bits per channel.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class Rgba8888TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class Rgba8888TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -34,10 +35,8 @@ internal class Rgba8888TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
int offset = 0; int offset = 0;
bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData; bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData;
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
using IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null; using IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null;
Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : Span<Vector4>.Empty; Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : [];
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);

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

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -10,6 +9,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with an alpha channel and with 32 bits for each channel. /// Implements the 'RGB' photometric interpretation with an alpha channel and with 32 bits for each channel.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class RgbaFloat32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class RgbaFloat32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -24,8 +24,6 @@ internal class RgbaFloat32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
int offset = 0; int offset = 0;
Span<byte> buffer = stackalloc byte[4]; Span<byte> buffer = stackalloc byte[4];
@ -57,9 +55,7 @@ internal class RgbaFloat32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float a = BitConverter.ToSingle(buffer); float a = BitConverter.ToSingle(buffer);
offset += 4; offset += 4;
var colorVector = new Vector4(r, g, b, a); pixelRow[x] = TPixel.FromScaledVector4(new(r, g, b, a));
color.FromScaledVector4(colorVector);
pixelRow[x] = color;
} }
} }
else else
@ -78,9 +74,7 @@ internal class RgbaFloat32323232TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float a = BitConverter.ToSingle(data.Slice(offset, 4)); float a = BitConverter.ToSingle(data.Slice(offset, 4));
offset += 4; offset += 4;
var colorVector = new Vector4(r, g, b, a); pixelRow[x] = TPixel.FromScaledVector4(new(r, g, b, a));
color.FromScaledVector4(colorVector);
pixelRow[x] = color;
} }
} }
} }

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

@ -12,6 +12,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with an alpha channel and with 'Planar' layout (for all bit depths). /// Implements the 'RGB' photometric interpretation with an alpha channel and with 'Planar' layout (for all bit depths).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class RgbaPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class RgbaPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -59,13 +60,12 @@ internal class RgbaPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
/// <param name="height">The height of the image block.</param> /// <param name="height">The height of the image block.</param>
public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
bool hasAssociatedAlpha = this.extraSampleType.HasValue && this.extraSampleType == TiffExtraSampleType.AssociatedAlphaData; bool hasAssociatedAlpha = this.extraSampleType.HasValue && this.extraSampleType == TiffExtraSampleType.AssociatedAlphaData;
var rBitReader = new BitReader(data[0].GetSpan()); BitReader rBitReader = new(data[0].GetSpan());
var gBitReader = new BitReader(data[1].GetSpan()); BitReader gBitReader = new(data[1].GetSpan());
var bBitReader = new BitReader(data[2].GetSpan()); BitReader bBitReader = new(data[2].GetSpan());
var aBitReader = new BitReader(data[3].GetSpan()); BitReader aBitReader = new(data[3].GetSpan());
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -77,17 +77,15 @@ internal class RgbaPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
float b = bBitReader.ReadBits(this.bitsPerSampleB) / this.bFactor; float b = bBitReader.ReadBits(this.bitsPerSampleB) / this.bFactor;
float a = aBitReader.ReadBits(this.bitsPerSampleA) / this.aFactor; float a = aBitReader.ReadBits(this.bitsPerSampleA) / this.aFactor;
var vec = new Vector4(r, g, b, a); Vector4 vector = new(r, g, b, a);
if (hasAssociatedAlpha) if (hasAssociatedAlpha)
{ {
color = TiffUtils.UnPremultiply(ref vec, color); pixelRow[x] = TiffUtilities.UnPremultiply<TPixel>(ref vector);
} }
else else
{ {
color.FromScaledVector4(vec); pixelRow[x] = TPixel.FromScaledVector4(vector);
} }
pixelRow[x] = color;
} }
rBitReader.NextRow(); rBitReader.NextRow();

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

@ -11,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'RGB' photometric interpretation with alpha channel (for all bit depths). /// Implements the 'RGB' photometric interpretation with alpha channel (for all bit depths).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class RgbaTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class RgbaTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -50,9 +51,7 @@ internal class RgbaTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel); BitReader bitReader = new(data);
var bitReader = new BitReader(data);
bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData; bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData;
@ -66,15 +65,14 @@ internal class RgbaTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
float b = bitReader.ReadBits(this.bitsPerSampleB) / this.bFactor; float b = bitReader.ReadBits(this.bitsPerSampleB) / this.bFactor;
float a = bitReader.ReadBits(this.bitsPerSampleB) / this.aFactor; float a = bitReader.ReadBits(this.bitsPerSampleB) / this.aFactor;
var vec = new Vector4(r, g, b, a); Vector4 vector = new(r, g, b, a);
if (hasAssociatedAlpha) if (hasAssociatedAlpha)
{ {
pixelRow[x] = TiffUtils.UnPremultiply(ref vec, color); pixelRow[x] = TiffUtilities.UnPremultiply<TPixel>(ref vector);
} }
else else
{ {
color.FromScaledVector4(vec); pixelRow[x] = TPixel.FromScaledVector4(vector);
pixelRow[x] = color;
} }
} }

14
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero16TiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'WhiteIsZero' photometric interpretation for 16-bit grayscale images. /// Implements the 'WhiteIsZero' photometric interpretation for 16-bit grayscale images.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class WhiteIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class WhiteIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -25,10 +25,6 @@ internal class WhiteIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
L16 l16 = TiffUtils.L16Default;
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
int offset = 0; int offset = 0;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -37,20 +33,20 @@ internal class WhiteIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2))); ushort intensity = (ushort)(ushort.MaxValue - TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2)));
offset += 2; offset += 2;
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); pixelRow[x] = TPixel.FromL16(new(intensity));
} }
} }
else else
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToUShortLittleEndian(data.Slice(offset, 2))); ushort intensity = (ushort)(ushort.MaxValue - TiffUtilities.ConvertToUShortLittleEndian(data.Slice(offset, 2)));
offset += 2; offset += 2;
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); pixelRow[x] = TPixel.FromL16(new(intensity));
} }
} }
} }

7
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor{TPixel}.cs

@ -11,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'WhiteIsZero' photometric interpretation (optimized for bilevel images). /// Implements the 'WhiteIsZero' photometric interpretation (optimized for bilevel images).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class WhiteIsZero1TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class WhiteIsZero1TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -18,11 +19,9 @@ internal class WhiteIsZero1TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
nuint offset = 0; nuint offset = 0;
var colorBlack = default(TPixel); TPixel colorBlack = TPixel.FromRgba32(Color.Black.ToPixel<Rgba32>());
var colorWhite = default(TPixel); TPixel colorWhite = TPixel.FromRgba32(Color.White.ToPixel<Rgba32>());
colorBlack.FromRgba32(Color.Black.ToPixel<Rgba32>());
colorWhite.FromRgba32(Color.White.ToPixel<Rgba32>());
ref byte dataRef = ref MemoryMarshal.GetReference(data); ref byte dataRef = ref MemoryMarshal.GetReference(data);
for (nuint y = (uint)top; y < (uint)(top + height); y++) for (nuint y = (uint)top; y < (uint)(top + height); y++)
{ {

12
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero24TiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'WhiteIsZero' photometric interpretation for 24-bit grayscale images. /// Implements the 'WhiteIsZero' photometric interpretation for 24-bit grayscale images.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class WhiteIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class WhiteIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -25,8 +25,6 @@ internal class WhiteIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
Span<byte> buffer = stackalloc byte[4]; Span<byte> buffer = stackalloc byte[4];
int bufferStartIdx = this.isBigEndian ? 1 : 0; int bufferStartIdx = this.isBigEndian ? 1 : 0;
const uint maxValue = 0xFFFFFF; const uint maxValue = 0xFFFFFF;
@ -41,10 +39,10 @@ internal class WhiteIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong intensity = maxValue - TiffUtils.ConvertToUIntBigEndian(buffer); uint intensity = maxValue - TiffUtilities.ConvertToUIntBigEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(intensity, color); pixelRow[x] = TiffUtilities.ColorScaleTo24Bit<TPixel>(intensity);
} }
} }
else else
@ -52,10 +50,10 @@ internal class WhiteIsZero24TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
data.Slice(offset, 3).CopyTo(bufferSpan); data.Slice(offset, 3).CopyTo(bufferSpan);
ulong intensity = maxValue - TiffUtils.ConvertToUIntLittleEndian(buffer); uint intensity = maxValue - TiffUtilities.ConvertToUIntLittleEndian(buffer);
offset += 3; offset += 3;
pixelRow[x] = TiffUtils.ColorScaleTo24Bit(intensity, color); pixelRow[x] = TiffUtilities.ColorScaleTo24Bit<TPixel>(intensity);
} }
} }
} }

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

@ -10,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'WhiteIsZero' photometric interpretation for 32-bit float grayscale images. /// Implements the 'WhiteIsZero' photometric interpretation for 32-bit float grayscale images.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class WhiteIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class WhiteIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -24,8 +25,6 @@ internal class WhiteIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
Span<byte> buffer = stackalloc byte[4]; Span<byte> buffer = stackalloc byte[4];
int offset = 0; int offset = 0;
@ -41,9 +40,7 @@ internal class WhiteIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
float intensity = 1.0f - BitConverter.ToSingle(buffer); float intensity = 1.0f - BitConverter.ToSingle(buffer);
offset += 4; offset += 4;
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f); pixelRow[x] = TPixel.FromScaledVector4(new(intensity, intensity, intensity, 1f));
color.FromScaledVector4(colorVector);
pixelRow[x] = color;
} }
} }
else else
@ -53,9 +50,7 @@ internal class WhiteIsZero32FloatTiffColor<TPixel> : TiffBaseColorDecoder<TPixel
float intensity = 1.0f - BitConverter.ToSingle(data.Slice(offset, 4)); float intensity = 1.0f - BitConverter.ToSingle(data.Slice(offset, 4));
offset += 4; offset += 4;
var colorVector = new Vector4(intensity, intensity, intensity, 1.0f); pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1.0f));
color.FromScaledVector4(colorVector);
pixelRow[x] = color;
} }
} }
} }

12
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero32TiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'WhiteIsZero' photometric interpretation for 32-bit grayscale images. /// Implements the 'WhiteIsZero' photometric interpretation for 32-bit grayscale images.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class WhiteIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class WhiteIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -25,8 +25,6 @@ internal class WhiteIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
color.FromScaledVector4(Vector4.Zero);
const uint maxValue = 0xFFFFFFFF; const uint maxValue = 0xFFFFFFFF;
int offset = 0; int offset = 0;
@ -37,20 +35,20 @@ internal class WhiteIsZero32TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong intensity = maxValue - TiffUtils.ConvertToUIntBigEndian(data.Slice(offset, 4)); uint intensity = maxValue - TiffUtilities.ConvertToUIntBigEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(intensity, color); pixelRow[x] = TiffUtilities.ColorScaleTo32Bit<TPixel>(intensity);
} }
} }
else else
{ {
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
ulong intensity = maxValue - TiffUtils.ConvertToUIntLittleEndian(data.Slice(offset, 4)); uint intensity = maxValue - TiffUtilities.ConvertToUIntLittleEndian(data.Slice(offset, 4));
offset += 4; offset += 4;
pixelRow[x] = TiffUtils.ColorScaleTo32Bit(intensity, color); pixelRow[x] = TiffUtilities.ColorScaleTo32Bit<TPixel>(intensity);
} }
} }
} }

25
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor{TPixel}.cs

@ -9,47 +9,30 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'WhiteIsZero' photometric interpretation (optimized for 4-bit grayscale images). /// Implements the 'WhiteIsZero' photometric interpretation (optimized for 4-bit grayscale images).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class WhiteIsZero4TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class WhiteIsZero4TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
int offset = 0; int offset = 0;
bool isOddWidth = (width & 1) == 1; bool isOddWidth = (width & 1) == 1;
var l8 = default(L8);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan(y); Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan(y);
for (int x = left; x < left + width - 1;) for (int x = left; x < left + width - 1;)
{ {
byte byteData = data[offset++]; byte byteData = data[offset++];
pixelRowSpan[x++] = TPixel.FromL8(new((byte)((15 - ((byteData & 0xF0) >> 4)) * 17)));
byte intensity1 = (byte)((15 - ((byteData & 0xF0) >> 4)) * 17); pixelRowSpan[x++] = TPixel.FromL8(new((byte)((15 - (byteData & 0x0F)) * 17)));
l8.PackedValue = intensity1;
color.FromL8(l8);
pixelRowSpan[x++] = color;
byte intensity2 = (byte)((15 - (byteData & 0x0F)) * 17);
l8.PackedValue = intensity2;
color.FromL8(l8);
pixelRowSpan[x++] = color;
} }
if (isOddWidth) if (isOddWidth)
{ {
byte byteData = data[offset++]; byte byteData = data[offset++];
pixelRowSpan[left + width - 1] = TPixel.FromL8(new((byte)((15 - ((byteData & 0xF0) >> 4)) * 17)));
byte intensity1 = (byte)((15 - ((byteData & 0xF0) >> 4)) * 17);
l8.PackedValue = intensity1;
color.FromL8(l8);
pixelRowSpan[left + width - 1] = color;
} }
} }
} }

8
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor{TPixel}.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -10,24 +9,21 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'WhiteIsZero' photometric interpretation (optimized for 8-bit grayscale images). /// Implements the 'WhiteIsZero' photometric interpretation (optimized for 8-bit grayscale images).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class WhiteIsZero8TiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class WhiteIsZero8TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel);
int offset = 0; int offset = 0;
var l8 = default(L8);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
byte intensity = (byte)(byte.MaxValue - data[offset++]); byte intensity = (byte)(byte.MaxValue - data[offset++]);
pixelRow[x] = TiffUtils.ColorFromL8(l8, intensity, color); pixelRow[x] = TPixel.FromL8(new(intensity));
} }
} }
} }

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

@ -11,11 +11,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements the 'WhiteIsZero' photometric interpretation (for all bit depths). /// Implements the 'WhiteIsZero' photometric interpretation (for all bit depths).
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class WhiteIsZeroTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class WhiteIsZeroTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
private readonly ushort bitsPerSample0; private readonly ushort bitsPerSample0;
private readonly float factor; private readonly float factor;
public WhiteIsZeroTiffColor(TiffBitsPerSample bitsPerSample) public WhiteIsZeroTiffColor(TiffBitsPerSample bitsPerSample)
@ -27,9 +27,7 @@ internal class WhiteIsZeroTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
/// <inheritdoc/> /// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height) public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{ {
var color = default(TPixel); BitReader bitReader = new(data);
var bitReader = new BitReader(data);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -37,10 +35,8 @@ internal class WhiteIsZeroTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
int value = bitReader.ReadBits(this.bitsPerSample0); int value = bitReader.ReadBits(this.bitsPerSample0);
float intensity = 1.0f - (value / this.factor); float intensity = 1f - (value / this.factor);
pixelRow[x] = TPixel.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1f));
color.FromScaledVector4(new Vector4(intensity, intensity, intensity, 1.0f));
pixelRow[x] = color;
} }
bitReader.NextRow(); bitReader.NextRow();

12
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/YCbCrPlanarTiffColor{TPixel}.cs

@ -11,11 +11,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements decoding pixel data with photometric interpretation of type 'YCbCr' with the planar configuration. /// Implements decoding pixel data with photometric interpretation of type 'YCbCr' with the planar configuration.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class YCbCrPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel> internal class YCbCrPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
private readonly YCbCrConverter converter; private readonly YCbCrConverter converter;
private readonly ushort[] ycbcrSubSampling; private readonly ushort[] ycbcrSubSampling;
public YCbCrPlanarTiffColor(Rational[] referenceBlackAndWhite, Rational[] coefficients, ushort[] ycbcrSubSampling) public YCbCrPlanarTiffColor(Rational[] referenceBlackAndWhite, Rational[] coefficients, ushort[] ycbcrSubSampling)
@ -36,13 +36,12 @@ internal class YCbCrPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
ReverseChromaSubSampling(width, height, this.ycbcrSubSampling[0], this.ycbcrSubSampling[1], cbData, crData); ReverseChromaSubSampling(width, height, this.ycbcrSubSampling[0], this.ycbcrSubSampling[1], cbData, crData);
} }
var color = default(TPixel);
int offset = 0; int offset = 0;
int widthPadding = 0; int widthPadding = 0;
if (this.ycbcrSubSampling != null) if (this.ycbcrSubSampling != null)
{ {
// Round to the next integer multiple of horizontalSubSampling. // Round to the next integer multiple of horizontalSubSampling.
widthPadding = TiffUtils.PaddingToNextInteger(width, this.ycbcrSubSampling[0]); widthPadding = TiffUtilities.PaddingToNextInteger(width, this.ycbcrSubSampling[0]);
} }
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
@ -51,8 +50,7 @@ internal class YCbCrPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
Rgba32 rgba = this.converter.ConvertToRgba32(yData[offset], cbData[offset], crData[offset]); Rgba32 rgba = this.converter.ConvertToRgba32(yData[offset], cbData[offset], crData[offset]);
color.FromRgba32(rgba); pixelRow[x] = TPixel.FromRgba32(rgba);
pixelRow[x] = color;
offset++; offset++;
} }
@ -64,8 +62,8 @@ internal class YCbCrPlanarTiffColor<TPixel> : TiffBasePlanarColorDecoder<TPixel>
{ {
// If width and height are not multiples of ChromaSubsampleHoriz and ChromaSubsampleVert respectively, // If width and height are not multiples of ChromaSubsampleHoriz and ChromaSubsampleVert respectively,
// then the source data will be padded. // then the source data will be padded.
width += TiffUtils.PaddingToNextInteger(width, horizontalSubSampling); width += TiffUtilities.PaddingToNextInteger(width, horizontalSubSampling);
height += TiffUtils.PaddingToNextInteger(height, verticalSubSampling); height += TiffUtilities.PaddingToNextInteger(height, verticalSubSampling);
for (int row = height - 1; row >= 0; row--) for (int row = height - 1; row >= 0; row--)
{ {

11
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/YCbCrTiffColor{TPixel}.cs

@ -11,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
/// <summary> /// <summary>
/// Implements decoding pixel data with photometric interpretation of type 'YCbCr'. /// Implements decoding pixel data with photometric interpretation of type 'YCbCr'.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The type of pixel format.</typeparam>
internal class YCbCrTiffColor<TPixel> : TiffBaseColorDecoder<TPixel> internal class YCbCrTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
@ -50,13 +51,12 @@ internal class YCbCrTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
private void DecodeYCbCrData(Buffer2D<TPixel> pixels, int left, int top, int width, int height, ReadOnlySpan<byte> ycbcrData) private void DecodeYCbCrData(Buffer2D<TPixel> pixels, int left, int top, int width, int height, ReadOnlySpan<byte> ycbcrData)
{ {
var color = default(TPixel);
int offset = 0; int offset = 0;
int widthPadding = 0; int widthPadding = 0;
if (this.ycbcrSubSampling != null) if (this.ycbcrSubSampling != null)
{ {
// Round to the next integer multiple of horizontalSubSampling. // Round to the next integer multiple of horizontalSubSampling.
widthPadding = TiffUtils.PaddingToNextInteger(width, this.ycbcrSubSampling[0]); widthPadding = TiffUtilities.PaddingToNextInteger(width, this.ycbcrSubSampling[0]);
} }
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
@ -65,8 +65,7 @@ internal class YCbCrTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
for (int x = 0; x < pixelRow.Length; x++) for (int x = 0; x < pixelRow.Length; x++)
{ {
Rgba32 rgba = this.converter.ConvertToRgba32(ycbcrData[offset], ycbcrData[offset + 1], ycbcrData[offset + 2]); Rgba32 rgba = this.converter.ConvertToRgba32(ycbcrData[offset], ycbcrData[offset + 1], ycbcrData[offset + 2]);
color.FromRgba32(rgba); pixelRow[x] = TPixel.FromRgba32(rgba);
pixelRow[x] = color;
offset += 3; offset += 3;
} }
@ -78,8 +77,8 @@ internal class YCbCrTiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{ {
// If width and height are not multiples of ChromaSubsampleHoriz and ChromaSubsampleVert respectively, // If width and height are not multiples of ChromaSubsampleHoriz and ChromaSubsampleVert respectively,
// then the source data will be padded. // then the source data will be padded.
width += TiffUtils.PaddingToNextInteger(width, horizontalSubSampling); width += TiffUtilities.PaddingToNextInteger(width, horizontalSubSampling);
height += TiffUtils.PaddingToNextInteger(height, verticalSubSampling); height += TiffUtilities.PaddingToNextInteger(height, verticalSubSampling);
int blockWidth = width / horizontalSubSampling; int blockWidth = width / horizontalSubSampling;
int blockHeight = height / verticalSubSampling; int blockHeight = height / verticalSubSampling;
int cbCrOffsetInBlock = horizontalSubSampling * verticalSubSampling; int cbCrOffsetInBlock = horizontalSubSampling * verticalSubSampling;

6
src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs

@ -163,8 +163,8 @@ internal class TiffDecoderCore : IImageDecoderInternals
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken) public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
List<ImageFrame<TPixel>> frames = new(); List<ImageFrame<TPixel>> frames = [];
List<ImageFrameMetadata> framesMetadata = new(); List<ImageFrameMetadata> framesMetadata = [];
try try
{ {
this.inputStream = stream; this.inputStream = stream;
@ -221,7 +221,7 @@ internal class TiffDecoderCore : IImageDecoderInternals
DirectoryReader reader = new(stream, this.configuration.MemoryAllocator); DirectoryReader reader = new(stream, this.configuration.MemoryAllocator);
IList<ExifProfile> directories = reader.Read(); IList<ExifProfile> directories = reader.Read();
List<ImageFrameMetadata> framesMetadata = new(); List<ImageFrameMetadata> framesMetadata = [];
foreach (ExifProfile dir in directories) foreach (ExifProfile dir in directories)
{ {
framesMetadata.Add(this.CreateFrameMetadata(dir)); framesMetadata.Add(this.CreateFrameMetadata(dir));

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

@ -0,0 +1,120 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Buffers.Binary;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Tiff.Utils;
/// <summary>
/// Helper methods for TIFF decoding.
/// </summary>
internal static class TiffUtilities
{
private const float Scale24Bit = 1f / 0xFFFFFF;
private static readonly Vector4 Scale24BitVector = Vector128.Create(Scale24Bit, Scale24Bit, Scale24Bit, 1f).AsVector4();
private const float Scale32Bit = 1f / 0xFFFFFFFF;
private static readonly Vector4 Scale32BitVector = Vector128.Create(Scale32Bit, Scale32Bit, Scale32Bit, 1f).AsVector4();
public static Rgba64 Rgba64Default { get; } = new(0, 0, 0, 0);
public static L16 L16Default { get; } = new(0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ConvertToUShortBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16BigEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ConvertToUShortLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16LittleEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ConvertToUIntBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32BigEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ConvertToUIntLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32LittleEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorFromRgba64Premultiplied<TPixel>(ushort r, ushort g, ushort b, ushort a)
where TPixel : unmanaged, IPixel<TPixel>
{
if (a == 0)
{
return TPixel.FromRgba64(default);
}
return TPixel.FromRgba64(new((ushort)(r / a), (ushort)(g / a), (ushort)(b / a), a));
}
[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);
[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);
[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);
}
[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);
[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);
[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);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo24Bit<TPixel>(uint intensity)
where TPixel : unmanaged, IPixel<TPixel>
=> TPixel.FromScaledVector4(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);
}
/// <summary>
/// Finds the padding needed to round 'valueToRoundUp' to the next integer multiple of subSampling value.
/// </summary>
/// <param name="valueToRoundUp">The width or height to round up.</param>
/// <param name="subSampling">The sub sampling.</param>
/// <returns>The padding.</returns>
public static int PaddingToNextInteger(int valueToRoundUp, int subSampling)
{
if (valueToRoundUp % subSampling == 0)
{
return 0;
}
return subSampling - (valueToRoundUp % subSampling);
}
}

176
src/ImageSharp/Formats/Tiff/Utils/TiffUtils.cs

@ -1,176 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Buffers.Binary;
using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Tiff.Utils;
/// <summary>
/// Helper methods for TIFF decoding.
/// </summary>
internal static class TiffUtils
{
private const float Scale24Bit = 1.0f / 0xFFFFFF;
private const float Scale32Bit = 1.0f / 0xFFFFFFFF;
public static Rgba64 Rgba64Default { get; } = new(0, 0, 0, 0);
public static L16 L16Default { get; } = new(0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ConvertToUShortBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16BigEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ConvertToUShortLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt16LittleEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ConvertToUIntBigEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32BigEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ConvertToUIntLittleEndian(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadUInt32LittleEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorFromL8<TPixel>(L8 l8, byte intensity, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
l8.PackedValue = intensity;
color.FromL8(l8);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorFromRgb64<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48);
color.FromRgba64(rgba);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorFromRgba64<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
rgba.PackedValue = r | (g << 16) | (b << 32) | (a << 48);
color.FromRgba64(rgba);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorFromRgba64Premultiplied<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
rgba.PackedValue = r | (g << 16) | (b << 32) | (a << 48);
var vec = rgba.ToVector4();
return UnPremultiply(ref vec, color);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
var colorVector = new Vector4(r * Scale24Bit, g * Scale24Bit, b * Scale24Bit, 1.0f);
color.FromScaledVector4(colorVector);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit;
color.FromScaledVector4(colorVector);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo24BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit;
return UnPremultiply(ref colorVector, color);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
var colorVector = new Vector4(r * Scale32Bit, g * Scale32Bit, b * Scale32Bit, 1.0f);
color.FromScaledVector4(colorVector);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 colorVector = new Vector4(r, g, b, a) * Scale32Bit;
color.FromScaledVector4(colorVector);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo32BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
Vector4 colorVector = new Vector4(r, g, b, a) * Scale32Bit;
return UnPremultiply(ref colorVector, color);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorFromL16<TPixel>(L16 l16, ushort intensity, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
l16.PackedValue = intensity;
color.FromL16(l16);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo24Bit<TPixel>(ulong intensity, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
var colorVector = new Vector4(intensity * Scale24Bit, intensity * Scale24Bit, intensity * Scale24Bit, 1.0f);
color.FromScaledVector4(colorVector);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorScaleTo32Bit<TPixel>(ulong intensity, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
var colorVector = new Vector4(intensity * Scale32Bit, intensity * Scale32Bit, intensity * Scale32Bit, 1.0f);
color.FromScaledVector4(colorVector);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel UnPremultiply<TPixel>(ref Vector4 vector, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
Numerics.UnPremultiply(ref vector);
color.FromScaledVector4(vector);
return color;
}
/// <summary>
/// Finds the padding needed to round 'valueToRoundUp' to the next integer multiple of subSampling value.
/// </summary>
/// <param name="valueToRoundUp">The width or height to round up.</param>
/// <param name="subSampling">The sub sampling.</param>
/// <returns>The padding.</returns>
public static int PaddingToNextInteger(int valueToRoundUp, int subSampling)
{
if (valueToRoundUp % subSampling == 0)
{
return 0;
}
return subSampling - (valueToRoundUp % subSampling);
}
}

4
src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs

@ -140,7 +140,6 @@ internal sealed class WebpLossyDecoder
private static void DecodePixelValues<TPixel>(int width, int height, Span<byte> pixelData, Buffer2D<TPixel> decodedPixels, IMemoryOwner<byte> alpha) private static void DecodePixelValues<TPixel>(int width, int height, Span<byte> pixelData, Buffer2D<TPixel> decodedPixels, IMemoryOwner<byte> alpha)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel color = default;
Span<byte> alphaSpan = alpha.Memory.Span; Span<byte> alphaSpan = alpha.Memory.Span;
Span<Bgr24> pixelsBgr = MemoryMarshal.Cast<byte, Bgr24>(pixelData); Span<Bgr24> pixelsBgr = MemoryMarshal.Cast<byte, Bgr24>(pixelData);
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
@ -151,8 +150,7 @@ internal sealed class WebpLossyDecoder
{ {
int offset = yMulWidth + x; int offset = yMulWidth + x;
Bgr24 bgr = pixelsBgr[offset]; Bgr24 bgr = pixelsBgr[offset];
color.FromBgra32(new Bgra32(bgr.R, bgr.G, bgr.B, alphaSpan[offset])); decodedPixelRow[x] = TPixel.FromBgra32(new(bgr.R, bgr.G, bgr.B, alphaSpan[offset]));
decodedPixelRow[x] = color;
} }
} }
} }

40
src/ImageSharp/PixelFormats/IPixel.cs

@ -43,96 +43,96 @@ public interface IPixel<TSelf> : IPixel, IEquatable<TSelf>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static abstract TSelf FromVector4(Vector4 source); static abstract TSelf FromVector4(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);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="Argb32"/> value. /// Initializes the pixel instance from an <see cref="Argb32"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Argb32"/> value.</param> /// <param name="source">The <see cref="Argb32"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromArgb32(Argb32 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromArgb32(Argb32 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="Bgra5551"/> value. /// Initializes the pixel instance from an <see cref="Bgra5551"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Bgra5551"/> value.</param> /// <param name="source">The <see cref="Bgra5551"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromBgra5551(Bgra5551 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromBgra5551(Bgra5551 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="Bgr24"/> value. /// Initializes the pixel instance from an <see cref="Bgr24"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Bgr24"/> value.</param> /// <param name="source">The <see cref="Bgr24"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromBgr24(Bgr24 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromBgr24(Bgr24 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="Bgra32"/> value. /// Initializes the pixel instance from an <see cref="Bgra32"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Bgra32"/> value.</param> /// <param name="source">The <see cref="Bgra32"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromBgra32(Bgra32 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromBgra32(Bgra32 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 virtual TSelf FromAbgr32(Abgr32 source) => TSelf.FromScaledVector4(source.ToScaledVector4());
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="L8"/> value. /// Initializes the pixel instance from an <see cref="L8"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="L8"/> value.</param> /// <param name="source">The <see cref="L8"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromL8(L8 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromL8(L8 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="L16"/> value. /// Initializes the pixel instance from an <see cref="L16"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="L16"/> value.</param> /// <param name="source">The <see cref="L16"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromL16(L16 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromL16(L16 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="La16"/> value. /// Initializes the pixel instance from an <see cref="La16"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="La16"/> value.</param> /// <param name="source">The <see cref="La16"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromLa16(La16 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromLa16(La16 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="La32"/> value. /// Initializes the pixel instance from an <see cref="La32"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="La32"/> value.</param> /// <param name="source">The <see cref="La32"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromLa32(La32 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromLa32(La32 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="Rgb24"/> value. /// Initializes the pixel instance from an <see cref="Rgb24"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Rgb24"/> value.</param> /// <param name="source">The <see cref="Rgb24"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromRgb24(Rgb24 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromRgb24(Rgb24 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="Rgba32"/> value. /// Initializes the pixel instance from an <see cref="Rgba32"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Rgba32"/> value.</param> /// <param name="source">The <see cref="Rgba32"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromRgba32(Rgba32 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromRgba32(Rgba32 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="Rgb48"/> value. /// Initializes the pixel instance from an <see cref="Rgb48"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Rgb48"/> value.</param> /// <param name="source">The <see cref="Rgb48"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromRgb48(Rgb48 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromRgb48(Rgb48 source);
/// <summary> /// <summary>
/// Initializes the pixel instance from an <see cref="Rgba64"/> value. /// Initializes the pixel instance from an <see cref="Rgba64"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Rgba64"/> value.</param> /// <param name="source">The <see cref="Rgba64"/> value.</param>
/// <returns>The <typeparamref name="TSelf"/>.</returns> /// <returns>The <typeparamref name="TSelf"/>.</returns>
static virtual TSelf FromRgba64(Rgba64 source) => TSelf.FromScaledVector4(source.ToScaledVector4()); static abstract TSelf FromRgba64(Rgba64 source);
#pragma warning restore CA1000 // Do not declare static members on generic types #pragma warning restore CA1000 // Do not declare static members on generic types
} }
@ -145,7 +145,7 @@ public interface IPixel
/// Convert the pixel instance into <see cref="Rgba32"/> representation. /// Convert the pixel instance into <see cref="Rgba32"/> representation.
/// </summary> /// </summary>
/// <returns>The <see cref="Rgba32"/></returns> /// <returns>The <see cref="Rgba32"/></returns>
virtual Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToVector4()); Rgba32 ToRgba32();
/// <summary> /// <summary>
/// Expands the pixel into a generic ("scaled") <see cref="Vector4"/> representation /// Expands the pixel into a generic ("scaled") <see cref="Vector4"/> representation

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

File diff suppressed because it is too large

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

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

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

File diff suppressed because it is too large

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

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

8
src/ImageSharp/PixelFormats/PixelConversionModifiers.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.ColorSpaces.Companding; using SixLabors.ImageSharp.ColorSpaces.Companding;
@ -7,9 +7,9 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// <summary> /// <summary>
/// Flags responsible to select additional operations which could be efficiently applied in /// Flags responsible to select additional operations which could be efficiently applied in
/// <see cref="PixelOperations{TPixel}.ToVector4(SixLabors.ImageSharp.Configuration,System.ReadOnlySpan{TPixel},System.Span{System.Numerics.Vector4},SixLabors.ImageSharp.PixelFormats.PixelConversionModifiers)"/> /// <see cref="PixelOperations{TPixel}.ToVector4(Configuration,ReadOnlySpan{TPixel},Span{System.Numerics.Vector4},PixelConversionModifiers)"/>
/// or /// or
/// <see cref="PixelOperations{TPixel}.FromVector4Destructive(SixLabors.ImageSharp.Configuration,System.Span{System.Numerics.Vector4},System.Span{TPixel},SixLabors.ImageSharp.PixelFormats.PixelConversionModifiers)"/> /// <see cref="PixelOperations{TPixel}.FromVector4Destructive(Configuration,Span{System.Numerics.Vector4},Span{TPixel},PixelConversionModifiers)"/>
/// knowing the pixel type. /// knowing the pixel type.
/// </summary> /// </summary>
[Flags] [Flags]
@ -21,7 +21,7 @@ public enum PixelConversionModifiers
None = 0, None = 0,
/// <summary> /// <summary>
/// Select <see cref="IPixel.ToScaledVector4"/> and <see cref="IPixel.FromScaledVector4"/> instead the standard (non scaled) variants. /// Select <see cref="IPixel.ToScaledVector4"/> and <see cref="IPixel{T}.FromScaledVector4"/> instead the standard (non scaled) variants.
/// </summary> /// </summary>
Scale = 1 << 0, Scale = 1 << 0,

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

@ -87,19 +87,23 @@ public partial struct A8 : IPixel<A8>, IPackedVector<byte>
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromArgb32(Argb32 source) => new(source.A); public static A8 FromAbgr32(Abgr32 source) => new(source.A);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromBgr24(Bgr24 source) => new(byte.MaxValue); public static A8 FromArgb32(Argb32 source) => new(source.A);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromBgra32(Bgra32 source) => new(source.A); public static A8 FromBgr24(Bgr24 source) => new(byte.MaxValue);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static A8 FromAbgr32(Abgr32 source) => new(source.A); public static A8 FromBgra32(Bgra32 source) => new(source.A);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

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

@ -92,7 +92,9 @@ public partial struct Abgr32 : IPixel<Abgr32>, IPackedVector<uint>
/// <param name="a">The alpha component.</param> /// <param name="a">The alpha component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Abgr32(float r, float g, float b, float a = 1) public Abgr32(float r, float g, float b, float a = 1)
: this() => Pack(r, g, b, a); : this(new Vector4(r, g, b, a))
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Abgr32"/> struct. /// Initializes a new instance of the <see cref="Abgr32"/> struct.
@ -102,7 +104,9 @@ public partial struct Abgr32 : IPixel<Abgr32>, IPackedVector<uint>
/// </param> /// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Abgr32(Vector3 vector) public Abgr32(Vector3 vector)
: this() => Pack(vector); : this(new Vector4(vector, 1f))
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Abgr32"/> struct. /// Initializes a new instance of the <see cref="Abgr32"/> struct.
@ -112,7 +116,7 @@ public partial struct Abgr32 : IPixel<Abgr32>, IPackedVector<uint>
/// </param> /// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Abgr32(Vector4 vector) public Abgr32(Vector4 vector)
: this() => Pack(vector); : this() => this = Pack(vector);
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Abgr32"/> struct. /// Initializes a new instance of the <see cref="Abgr32"/> struct.
@ -170,7 +174,7 @@ public partial struct Abgr32 : IPixel<Abgr32>, IPackedVector<uint>
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => new(this.R, this.G, this.B, this.A); public readonly Rgba32 ToRgba32() => Rgba32.FromAbgr32(this);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -204,11 +208,15 @@ public partial struct Abgr32 : IPixel<Abgr32>, IPackedVector<uint>
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32 FromBgr24(Bgr24 source) => new(source.R, source.G, source.B); public static Abgr32 FromArgb32(Argb32 source) => new(source.R, source.G, source.B, source.A);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Abgr32 FromArgb32(Argb32 source) => new(source.R, source.G, source.B, source.A); public static Abgr32 FromBgr24(Bgr24 source) => new(source.R, source.G, source.B);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

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

@ -85,7 +85,9 @@ public partial struct Argb32 : IPixel<Argb32>, IPackedVector<uint>
/// <param name="a">The alpha component.</param> /// <param name="a">The alpha component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Argb32(float r, float g, float b, float a = 1) public Argb32(float r, float g, float b, float a = 1)
: this() => Pack(r, g, b, a); : this(new Vector4(r, g, b, a))
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Argb32"/> struct. /// Initializes a new instance of the <see cref="Argb32"/> struct.
@ -95,7 +97,9 @@ public partial struct Argb32 : IPixel<Argb32>, IPackedVector<uint>
/// </param> /// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Argb32(Vector3 vector) public Argb32(Vector3 vector)
: this() => Pack(vector); : this(new Vector4(vector, 1f))
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Argb32"/> struct. /// Initializes a new instance of the <see cref="Argb32"/> struct.
@ -105,7 +109,7 @@ public partial struct Argb32 : IPixel<Argb32>, IPackedVector<uint>
/// </param> /// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public Argb32(Vector4 vector) public Argb32(Vector4 vector)
: this() => Pack(vector); : this() => this = Pack(vector);
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Argb32"/> struct. /// Initializes a new instance of the <see cref="Argb32"/> struct.
@ -163,7 +167,7 @@ public partial struct Argb32 : IPixel<Argb32>, IPackedVector<uint>
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => new(this.R, this.G, this.B, this.A); public readonly Rgba32 ToRgba32() => Rgba32.FromArgb32(this);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -193,19 +197,23 @@ public partial struct Argb32 : IPixel<Argb32>, IPackedVector<uint>
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromArgb32(Argb32 source) => new() { PackedValue = source.PackedValue }; public static Argb32 FromAbgr32(Abgr32 source) => new(source.R, source.G, source.B, source.A);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromBgr24(Bgr24 source) => new(source.R, source.G, source.B); public static Argb32 FromArgb32(Argb32 source) => new() { PackedValue = source.PackedValue };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromBgra32(Bgra32 source) => new(source.R, source.G, source.B, source.A); public static Argb32 FromBgr24(Bgr24 source) => new(source.R, source.G, source.B);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Argb32 FromAbgr32(Abgr32 source) => new(source.R, source.G, source.B, source.A); public static Argb32 FromBgra32(Bgra32 source) => new(source.R, source.G, source.B, source.A);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -276,23 +284,6 @@ public partial struct Argb32 : IPixel<Argb32>, IPackedVector<uint>
/// <inheritdoc/> /// <inheritdoc/>
public override readonly int GetHashCode() => this.Argb.GetHashCode(); public override readonly int GetHashCode() => this.Argb.GetHashCode();
/// <summary>
/// Packs the four floats into a color.
/// </summary>
/// <param name="x">The x-component</param>
/// <param name="y">The y-component</param>
/// <param name="z">The z-component</param>
/// <param name="w">The w-component</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Argb32 Pack(float x, float y, float z, float w) => Pack(new Vector4(x, y, z, w));
/// <summary>
/// Packs a <see cref="Vector3"/> into a uint.
/// </summary>
/// <param name="vector">The vector containing the values to pack.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Argb32 Pack(Vector3 vector) => Pack(new Vector4(vector, 1f));
/// <summary> /// <summary>
/// Packs a <see cref="Vector4"/> into a color. /// Packs a <see cref="Vector4"/> into a color.
/// </summary> /// </summary>

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

@ -15,37 +15,44 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form. /// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="Bgr24"/> struct.
/// </remarks>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
[StructLayout(LayoutKind.Explicit)] [StructLayout(LayoutKind.Explicit)]
[method: MethodImpl(MethodImplOptions.AggressiveInlining)] public partial struct Bgr24 : IPixel<Bgr24>
public partial struct Bgr24(byte r, byte g, byte b) : IPixel<Bgr24>
{ {
/// <summary> /// <summary>
/// The blue component. /// The blue component.
/// </summary> /// </summary>
[FieldOffset(0)] [FieldOffset(0)]
public byte B = b; public byte B;
/// <summary> /// <summary>
/// The green component. /// The green component.
/// </summary> /// </summary>
[FieldOffset(1)] [FieldOffset(1)]
public byte G = g; public byte G;
/// <summary> /// <summary>
/// The red component. /// The red component.
/// </summary> /// </summary>
[FieldOffset(2)] [FieldOffset(2)]
public byte R = r; public byte R;
private static readonly Vector4 MaxBytes = Vector128.Create(255f).AsVector4(); private static readonly Vector4 MaxBytes = Vector128.Create(255f).AsVector4();
private static readonly Vector4 Half = Vector128.Create(.5f).AsVector4(); private static readonly Vector4 Half = Vector128.Create(.5f).AsVector4();
/// <summary>
/// Initializes a new instance of the <see cref="Bgr24"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Bgr24(byte r, byte g, byte b)
{
this.R = r;
this.G = g;
this.B = b;
}
/// <summary> /// <summary>
/// Compares two <see cref="Bgr24"/> objects for equality. /// Compares two <see cref="Bgr24"/> objects for equality.
/// </summary> /// </summary>
@ -70,7 +77,7 @@ public partial struct Bgr24(byte r, byte g, byte b) : IPixel<Bgr24>
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => new(this.R, this.G, this.B); public readonly Rgba32 ToRgba32() => Rgba32.FromBgr24(this);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -106,10 +113,18 @@ public partial struct Bgr24(byte r, byte g, byte b) : IPixel<Bgr24>
return new(result.GetElement(0), result.GetElement(4), result.GetElement(8)); return new(result.GetElement(0), result.GetElement(4), result.GetElement(8));
} }
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromAbgr32(Abgr32 source) => new(source.R, source.G, source.B);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromArgb32(Argb32 source) => new(source.R, source.G, source.B); public static Bgr24 FromArgb32(Argb32 source) => new(source.R, source.G, source.B);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromBgr24(Bgr24 source) => new(source.R, source.G, source.B); public static Bgr24 FromBgr24(Bgr24 source) => new(source.R, source.G, source.B);
@ -146,10 +161,6 @@ public partial struct Bgr24(byte r, byte g, byte b) : IPixel<Bgr24>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromRgb24(Rgb24 source) => new(source.R, source.G, source.B); public static Bgr24 FromRgb24(Rgb24 source) => new(source.R, source.G, source.B);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromAbgr32(Abgr32 source) => new(source.R, source.G, source.B);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr24 FromRgba32(Rgba32 source) => new(source.R, source.G, source.B); public static Bgr24 FromRgba32(Rgba32 source) => new(source.R, source.G, source.B);

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

@ -75,6 +75,9 @@ public partial struct Bgr565(Vector3 vector) : IPixel<Bgr565>, IPackedVector<ush
/// <inheritdoc /> /// <inheritdoc />
public readonly PixelOperations<Bgr565> CreatePixelOperations() => new PixelOperations(); public readonly PixelOperations<Bgr565> CreatePixelOperations() => new PixelOperations();
/// <inheritdoc />
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromScaledVector4(Vector4 source) => FromVector4(source); public static Bgr565 FromScaledVector4(Vector4 source) => FromVector4(source);
@ -83,6 +86,58 @@ public partial struct Bgr565(Vector3 vector) : IPixel<Bgr565>, IPackedVector<ush
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromVector4(Vector4 source) => new() { PackedValue = Pack(new Vector3(source.X, source.Y, source.Z)) }; public static Bgr565 FromVector4(Vector4 source) => new() { PackedValue = Pack(new Vector3(source.X, source.Y, source.Z)) };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgr565 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <summary> /// <summary>
/// Expands the packed representation into a <see cref="Vector3"/>. /// Expands the packed representation into a <see cref="Vector3"/>.
/// The vector components are typically expanded in least to greatest significance order. /// The vector components are typically expanded in least to greatest significance order.

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

@ -116,7 +116,7 @@ public partial struct Bgra32 : IPixel<Bgra32>, IPackedVector<uint>
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => new(this.R, this.G, this.B, this.A); public readonly Rgba32 ToRgba32() => Rgba32.FromBgra32(this);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -146,11 +146,15 @@ public partial struct Bgra32 : IPixel<Bgra32>, IPackedVector<uint>
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32 FromArgb32(Argb32 source) => new(source.R, source.G, source.B, source.A); public static Bgra32 FromAbgr32(Abgr32 source) => new(source.R, source.G, source.B, source.A);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32 FromAbgr32(Abgr32 source) => new(source.R, source.G, source.B, source.A); public static Bgra32 FromArgb32(Argb32 source) => new(source.R, source.G, source.B, source.A);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra32 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

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

@ -12,11 +12,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct Bgra4444 : IPixel<Bgra4444>, IPackedVector<ushort>
/// Initializes a new instance of the <see cref="Bgra4444"/> struct.
/// </remarks>
/// <param name="vector">The vector containing the components for the packed vector.</param>
public partial struct Bgra4444(Vector4 vector) : IPixel<Bgra4444>, IPackedVector<ushort>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Bgra4444"/> struct. /// Initializes a new instance of the <see cref="Bgra4444"/> struct.
@ -30,8 +26,14 @@ public partial struct Bgra4444(Vector4 vector) : IPixel<Bgra4444>, IPackedVector
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="Bgra4444"/> struct.
/// </summary>
/// <param name="vector">The vector containing the components for the packed vector.</param>
public Bgra4444(Vector4 vector) => this.PackedValue = Pack(vector);
/// <inheritdoc/> /// <inheritdoc/>
public ushort PackedValue { get; set; } = Pack(vector); public ushort PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="Bgra4444"/> objects for equality. /// Compares two <see cref="Bgra4444"/> objects for equality.
@ -55,6 +57,10 @@ public partial struct Bgra4444(Vector4 vector) : IPixel<Bgra4444>, IPackedVector
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Bgra4444 left, Bgra4444 right) => !left.Equals(right); public static bool operator !=(Bgra4444 left, Bgra4444 right) => !left.Equals(right);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4() => this.ToVector4(); public readonly Vector4 ToScaledVector4() => this.ToVector4();
@ -90,6 +96,58 @@ public partial struct Bgra4444(Vector4 vector) : IPixel<Bgra4444>, IPackedVector
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) }; public static Bgra4444 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra4444 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc /> /// <inheritdoc />
public override readonly bool Equals(object? obj) => obj is Bgra4444 other && this.Equals(other); public override readonly bool Equals(object? obj) => obj is Bgra4444 other && this.Equals(other);

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

@ -13,13 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct Bgra5551 : IPixel<Bgra5551>, IPackedVector<ushort>
/// Initializes a new instance of the <see cref="Bgra5551"/> struct.
/// </remarks>
/// <param name="vector">
/// The vector containing the components for the packed vector.
/// </param>
public partial struct Bgra5551(Vector4 vector) : IPixel<Bgra5551>, IPackedVector<ushort>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Bgra5551"/> struct. /// Initializes a new instance of the <see cref="Bgra5551"/> struct.
@ -33,8 +27,16 @@ public partial struct Bgra5551(Vector4 vector) : IPixel<Bgra5551>, IPackedVector
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="Bgra5551"/> struct.
/// </summary>
/// <param name="vector">
/// The vector containing the components for the packed vector.
/// </param>
public Bgra5551(Vector4 vector) => this.PackedValue = Pack(vector);
/// <inheritdoc/> /// <inheritdoc/>
public ushort PackedValue { get; set; } = Pack(vector); public ushort PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="Bgra5551"/> objects for equality. /// Compares two <see cref="Bgra5551"/> objects for equality.
@ -58,6 +60,10 @@ public partial struct Bgra5551(Vector4 vector) : IPixel<Bgra5551>, IPackedVector
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Bgra5551 left, Bgra5551 right) => !left.Equals(right); public static bool operator !=(Bgra5551 left, Bgra5551 right) => !left.Equals(right);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4() => this.ToVector4(); public readonly Vector4 ToScaledVector4() => this.ToVector4();
@ -90,7 +96,55 @@ public partial struct Bgra5551(Vector4 vector) : IPixel<Bgra5551>, IPackedVector
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromBgra5551(Bgra5551 source) => new() { PackedValue = source.PackedValue }; public static Bgra5551 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Bgra5551 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc /> /// <inheritdoc />
public override readonly bool Equals(object? obj) => obj is Bgra5551 other && this.Equals(other); public override readonly bool Equals(object? obj) => obj is Bgra5551 other && this.Equals(other);

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

@ -15,19 +15,8 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// </summary> /// </summary>
public partial struct Byte4 : IPixel<Byte4>, IPackedVector<uint> public partial struct Byte4 : IPixel<Byte4>, IPackedVector<uint>
{ {
/// <summary>
/// The maximum byte value.
/// </summary>
private static readonly Vector4 MaxBytes = Vector128.Create(255f).AsVector4(); private static readonly Vector4 MaxBytes = Vector128.Create(255f).AsVector4();
/// <summary>
/// Initializes a new instance of the <see cref="Byte4"/> struct.
/// </summary>
/// <param name="vector">
/// A vector containing the initial values for the components of the Byte4 structure.
/// </param>
public Byte4(Vector4 vector) => this.PackedValue = Pack(vector);
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Byte4"/> struct. /// Initializes a new instance of the <see cref="Byte4"/> struct.
/// </summary> /// </summary>
@ -36,11 +25,18 @@ public partial struct Byte4 : IPixel<Byte4>, IPackedVector<uint>
/// <param name="z">The z-component</param> /// <param name="z">The z-component</param>
/// <param name="w">The w-component</param> /// <param name="w">The w-component</param>
public Byte4(float x, float y, float z, float w) public Byte4(float x, float y, float z, float w)
: this(new Vector4(x, y, z, w))
{ {
Vector4 vector = new(x, y, z, w);
this.PackedValue = Pack(vector);
} }
/// <summary>
/// Initializes a new instance of the <see cref="Byte4"/> struct.
/// </summary>
/// <param name="vector">
/// A vector containing the initial values for the components of the Byte4 structure.
/// </param>
public Byte4(Vector4 vector) => this.PackedValue = Pack(vector);
/// <inheritdoc/> /// <inheritdoc/>
public uint PackedValue { get; set; } public uint PackedValue { get; set; }
@ -100,10 +96,58 @@ public partial struct Byte4 : IPixel<Byte4>, IPackedVector<uint>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) }; public static Byte4 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromRgba32(Rgba32 source) => new() { PackedValue = source.PackedValue }; public static Byte4 FromRgba32(Rgba32 source) => new() { PackedValue = source.PackedValue };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte4 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc /> /// <inheritdoc />
public override readonly bool Equals(object? obj) => obj is Byte4 byte4 && this.Equals(byte4); public override readonly bool Equals(object? obj) => obj is Byte4 byte4 && this.Equals(byte4);
@ -130,12 +174,10 @@ public partial struct Byte4 : IPixel<Byte4>, IPackedVector<uint>
{ {
vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes); vector = Numerics.Clamp(vector, Vector4.Zero, MaxBytes);
Vector128<uint> result = Vector128.ConvertToUInt32(vector.AsVector128()); uint byte4 = (uint)Math.Round(vector.X) & 0xFF;
uint byte3 = ((uint)Math.Round(vector.Y) & 0xFF) << 0x8;
uint byte4 = result.GetElement(0) & 0xFF; uint byte2 = ((uint)Math.Round(vector.Z) & 0xFF) << 0x10;
uint byte3 = result.GetElement(1) << 8; uint byte1 = ((uint)Math.Round(vector.W) & 0xFF) << 0x18;
uint byte2 = result.GetElement(2) << 16;
uint byte1 = result.GetElement(3) << 24;
return byte4 | byte3 | byte2 | byte1; return byte4 | byte3 | byte2 | byte1;
} }

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

@ -12,14 +12,16 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [-1, 0, 0, 1] to [1, 0, 0, 1] in vector form. /// Ranges from [-1, 0, 0, 1] to [1, 0, 0, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct HalfSingle : IPixel<HalfSingle>, IPackedVector<ushort>
/// Initializes a new instance of the <see cref="HalfSingle"/> struct.
/// </remarks>
/// <param name="value">The single component value.</param>
public partial struct HalfSingle(float value) : IPixel<HalfSingle>, IPackedVector<ushort>
{ {
/// <summary>
/// Initializes a new instance of the <see cref="HalfSingle"/> struct.
/// </summary>
/// <param name="value">The single component value.</param>
public HalfSingle(float value) => this.PackedValue = HalfTypeHelper.Pack(value);
/// <inheritdoc/> /// <inheritdoc/>
public ushort PackedValue { get; set; } = HalfTypeHelper.Pack(value); public ushort PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="HalfSingle"/> objects for equality. /// Compares two <see cref="HalfSingle"/> objects for equality.
@ -43,6 +45,10 @@ public partial struct HalfSingle(float value) : IPixel<HalfSingle>, IPackedVecto
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(HalfSingle left, HalfSingle right) => !left.Equals(right); public static bool operator !=(HalfSingle left, HalfSingle right) => !left.Equals(right);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4() public readonly Vector4 ToScaledVector4()
@ -80,6 +86,58 @@ public partial struct HalfSingle(float value) : IPixel<HalfSingle>, IPackedVecto
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromVector4(Vector4 source) => new() { PackedValue = HalfTypeHelper.Pack(source.X) }; public static HalfSingle FromVector4(Vector4 source) => new() { PackedValue = HalfTypeHelper.Pack(source.X) };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfSingle FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <summary> /// <summary>
/// Expands the packed representation into a <see cref="float"/>. /// Expands the packed representation into a <see cref="float"/>.
/// </summary> /// </summary>

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

@ -52,6 +52,10 @@ public partial struct HalfVector2 : IPixel<HalfVector2>, IPackedVector<uint>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(HalfVector2 left, HalfVector2 right) => !left.Equals(right); public static bool operator !=(HalfVector2 left, HalfVector2 right) => !left.Equals(right);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4() public readonly Vector4 ToScaledVector4()
@ -93,6 +97,58 @@ public partial struct HalfVector2 : IPixel<HalfVector2>, IPackedVector<uint>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromVector4(Vector4 source) => new() { PackedValue = Pack(source.X, source.Y) }; public static HalfVector2 FromVector4(Vector4 source) => new() { PackedValue = Pack(source.X, source.Y) };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector2 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <summary> /// <summary>
/// Expands the packed representation into a <see cref="Vector2"/>. /// Expands the packed representation into a <see cref="Vector2"/>.
/// </summary> /// </summary>

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

@ -12,11 +12,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [-1, -1, -1, -1] to [1, 1, 1, 1] in vector form. /// Ranges from [-1, -1, -1, -1] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct HalfVector4 : IPixel<HalfVector4>, IPackedVector<ulong>
/// Initializes a new instance of the <see cref="HalfVector4"/> struct.
/// </remarks>
/// <param name="vector">A vector containing the initial values for the components</param>
public partial struct HalfVector4(Vector4 vector) : IPixel<HalfVector4>, IPackedVector<ulong>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="HalfVector4"/> struct. /// Initializes a new instance of the <see cref="HalfVector4"/> struct.
@ -30,8 +26,14 @@ public partial struct HalfVector4(Vector4 vector) : IPixel<HalfVector4>, IPacked
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="HalfVector4"/> struct.
/// </summary>
/// <param name="vector">A vector containing the initial values for the components</param>
public HalfVector4(Vector4 vector) => this.PackedValue = Pack(vector);
/// <inheritdoc/> /// <inheritdoc/>
public ulong PackedValue { get; set; } = Pack(vector); public ulong PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="HalfVector4"/> objects for equality. /// Compares two <see cref="HalfVector4"/> objects for equality.
@ -55,13 +57,17 @@ public partial struct HalfVector4(Vector4 vector) : IPixel<HalfVector4>, IPacked
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(HalfVector4 left, HalfVector4 right) => !left.Equals(right); public static bool operator !=(HalfVector4 left, HalfVector4 right) => !left.Equals(right);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4() public readonly Vector4 ToScaledVector4()
{ {
Vector4 scaled = this.ToVector4(); Vector4 scaled = this.ToVector4();
scaled += Vector4.One; scaled += Vector4.One;
scaled /= 2F; scaled /= 2f;
return scaled; return scaled;
} }
@ -87,7 +93,7 @@ public partial struct HalfVector4(Vector4 vector) : IPixel<HalfVector4>, IPacked
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromScaledVector4(Vector4 source) public static HalfVector4 FromScaledVector4(Vector4 source)
{ {
source *= 2F; source *= 2f;
source -= Vector4.One; source -= Vector4.One;
return FromVector4(source); return FromVector4(source);
} }
@ -96,6 +102,58 @@ public partial struct HalfVector4(Vector4 vector) : IPixel<HalfVector4>, IPacked
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) }; public static HalfVector4 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HalfVector4 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc /> /// <inheritdoc />
public override readonly bool Equals(object? obj) => obj is HalfVector4 other && this.Equals(other); public override readonly bool Equals(object? obj) => obj is HalfVector4 other && this.Equals(other);

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

@ -12,16 +12,18 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form. /// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct L16 : IPixel<L16>, IPackedVector<ushort>
/// Initializes a new instance of the <see cref="L16"/> struct.
/// </remarks>
/// <param name="luminance">The luminance component</param>
public partial struct L16(ushort luminance) : IPixel<L16>, IPackedVector<ushort>
{ {
private const float Max = ushort.MaxValue; private const float Max = ushort.MaxValue;
/// <summary>
/// Initializes a new instance of the <see cref="L16"/> struct.
/// </summary>
/// <param name="luminance">The luminance component</param>
public L16(ushort luminance) => this.PackedValue = luminance;
/// <inheritdoc /> /// <inheritdoc />
public ushort PackedValue { get; set; } = luminance; public ushort PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="L16"/> objects for equality. /// Compares two <see cref="L16"/> objects for equality.
@ -85,19 +87,23 @@ public partial struct L16(ushort luminance) : IPixel<L16>, IPackedVector<ushort>
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromArgb32(Argb32 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B)); public static L16 FromAbgr32(Abgr32 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B));
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromBgr24(Bgr24 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B)); public static L16 FromArgb32(Argb32 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B));
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromBgra32(Bgra32 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B)); public static L16 FromBgr24(Bgr24 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B));
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L16 FromAbgr32(Abgr32 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B)); public static L16 FromBgra32(Bgra32 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B));
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

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

@ -13,24 +13,19 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form. /// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct L8 : IPixel<L8>, IPackedVector<byte>
/// Initializes a new instance of the <see cref="L8"/> struct.
/// </remarks>
/// <param name="luminance">The luminance component.</param>
public partial struct L8(byte luminance) : IPixel<L8>, IPackedVector<byte>
{ {
/// <summary>
/// The maximum byte value.
/// </summary>
private static readonly Vector4 MaxBytes = Vector128.Create(255f).AsVector4(); private static readonly Vector4 MaxBytes = Vector128.Create(255f).AsVector4();
private static readonly Vector4 Half = Vector128.Create(.5f).AsVector4();
/// <summary> /// <summary>
/// The half vector value. /// Initializes a new instance of the <see cref="L8"/> struct.
/// </summary> /// </summary>
private static readonly Vector4 Half = Vector128.Create(.5f).AsVector4(); /// <param name="luminance">The luminance component.</param>
public L8(byte luminance) => this.PackedValue = luminance;
/// <inheritdoc /> /// <inheritdoc />
public byte PackedValue { get; set; } = luminance; public byte PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="L8"/> objects for equality. /// Compares two <see cref="L8"/> objects for equality.
@ -94,19 +89,23 @@ public partial struct L8(byte luminance) : IPixel<L8>, IPackedVector<byte>
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromArgb32(Argb32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B)); public static L8 FromAbgr32(Abgr32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B));
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromBgr24(Bgr24 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B)); public static L8 FromArgb32(Argb32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B));
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromBgra32(Bgra32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B)); public static L8 FromBgr24(Bgr24 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B));
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static L8 FromAbgr32(Abgr32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B)); public static L8 FromBgra32(Bgra32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B));
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

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

@ -14,13 +14,8 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="La16"/> struct.
/// </remarks>
/// <param name="l">The luminance component.</param>
/// <param name="a">The alpha component.</param>
[StructLayout(LayoutKind.Explicit)] [StructLayout(LayoutKind.Explicit)]
public partial struct La16(byte l, byte a) : IPixel<La16>, IPackedVector<ushort> public partial struct La16 : IPixel<La16>, IPackedVector<ushort>
{ {
/// <summary> /// <summary>
/// The maximum byte value. /// The maximum byte value.
@ -36,13 +31,24 @@ public partial struct La16(byte l, byte a) : IPixel<La16>, IPackedVector<ushort>
/// Gets or sets the luminance component. /// Gets or sets the luminance component.
/// </summary> /// </summary>
[FieldOffset(0)] [FieldOffset(0)]
public byte L = l; public byte L;
/// <summary> /// <summary>
/// Gets or sets the alpha component. /// Gets or sets the alpha component.
/// </summary> /// </summary>
[FieldOffset(1)] [FieldOffset(1)]
public byte A = a; public byte A;
/// <summary>
/// Initializes a new instance of the <see cref="La16"/> struct.
/// </summary>
/// <param name="l">The luminance component.</param>
/// <param name="a">The alpha component.</param>
public La16(byte l, byte a)
{
this.L = l;
this.A = a;
}
/// <inheritdoc/> /// <inheritdoc/>
public ushort PackedValue public ushort PackedValue
@ -110,19 +116,23 @@ public partial struct La16(byte l, byte a) : IPixel<La16>, IPackedVector<ushort>
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromArgb32(Argb32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B), source.A); public static La16 FromAbgr32(Abgr32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B), source.A);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromBgr24(Bgr24 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B), byte.MaxValue); public static La16 FromArgb32(Argb32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B), source.A);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromBgra32(Bgra32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B), source.A); public static La16 FromBgr24(Bgr24 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B), byte.MaxValue);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La16 FromAbgr32(Abgr32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B), source.A); public static La16 FromBgra32(Bgra32 source) => new(ColorNumerics.Get8BitBT709Luminance(source.R, source.G, source.B), source.A);
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

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

@ -13,13 +13,8 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="La32"/> struct.
/// </remarks>
/// <param name="l">The luminance component.</param>
/// <param name="a">The alpha component.</param>
[StructLayout(LayoutKind.Explicit)] [StructLayout(LayoutKind.Explicit)]
public partial struct La32(ushort l, ushort a) : IPixel<La32>, IPackedVector<uint> public partial struct La32 : IPixel<La32>, IPackedVector<uint>
{ {
private const float Max = ushort.MaxValue; private const float Max = ushort.MaxValue;
@ -27,13 +22,24 @@ public partial struct La32(ushort l, ushort a) : IPixel<La32>, IPackedVector<uin
/// Gets or sets the luminance component. /// Gets or sets the luminance component.
/// </summary> /// </summary>
[FieldOffset(0)] [FieldOffset(0)]
public ushort L = l; public ushort L;
/// <summary> /// <summary>
/// Gets or sets the alpha component. /// Gets or sets the alpha component.
/// </summary> /// </summary>
[FieldOffset(2)] [FieldOffset(2)]
public ushort A = a; public ushort A;
/// <summary>
/// Initializes a new instance of the <see cref="La32"/> struct.
/// </summary>
/// <param name="l">The luminance component.</param>
/// <param name="a">The alpha component.</param>
public La32(ushort l, ushort a)
{
this.L = l;
this.A = a;
}
/// <inheritdoc/> /// <inheritdoc/>
public uint PackedValue public uint PackedValue
@ -107,7 +113,7 @@ public partial struct La32(ushort l, ushort a) : IPixel<La32>, IPackedVector<uin
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromArgb32(Argb32 source) public static La32 FromAbgr32(Abgr32 source)
{ {
ushort l = ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B); ushort l = ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B);
ushort a = ColorNumerics.From8BitTo16Bit(source.A); ushort a = ColorNumerics.From8BitTo16Bit(source.A);
@ -116,20 +122,24 @@ public partial struct La32(ushort l, ushort a) : IPixel<La32>, IPackedVector<uin
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromBgr24(Bgr24 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B), ushort.MaxValue); public static La32 FromArgb32(Argb32 source)
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromBgra32(Bgra32 source)
{ {
ushort l = ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B); ushort l = ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B);
ushort a = ColorNumerics.From8BitTo16Bit(source.A); ushort a = ColorNumerics.From8BitTo16Bit(source.A);
return new(l, a); return new(l, a);
} }
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromAbgr32(Abgr32 source) public static La32 FromBgr24(Bgr24 source) => new(ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B), ushort.MaxValue);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static La32 FromBgra32(Bgra32 source)
{ {
ushort l = ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B); ushort l = ColorNumerics.Get16BitBT709Luminance(source.R, source.G, source.B);
ushort a = ColorNumerics.From8BitTo16Bit(source.A); ushort a = ColorNumerics.From8BitTo16Bit(source.A);

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

@ -12,11 +12,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [-1, -1, 0, 1] to [1, 1, 0, 1] in vector form. /// Ranges from [-1, -1, 0, 1] to [1, 1, 0, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct NormalizedByte2 : IPixel<NormalizedByte2>, IPackedVector<ushort>
/// Initializes a new instance of the <see cref="NormalizedByte2"/> struct.
/// </remarks>
/// <param name="vector">The vector containing the component values.</param>
public partial struct NormalizedByte2(Vector2 vector) : IPixel<NormalizedByte2>, IPackedVector<ushort>
{ {
private const float MaxPos = 127f; private const float MaxPos = 127f;
private static readonly Vector2 Half = new(MaxPos); private static readonly Vector2 Half = new(MaxPos);
@ -32,8 +28,14 @@ public partial struct NormalizedByte2(Vector2 vector) : IPixel<NormalizedByte2>,
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="NormalizedByte2"/> struct.
/// </summary>
/// <param name="vector">The vector containing the component values.</param>
public NormalizedByte2(Vector2 vector) => this.PackedValue = Pack(vector);
/// <inheritdoc/> /// <inheritdoc/>
public ushort PackedValue { get; set; } = Pack(vector); public ushort PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="NormalizedByte2"/> objects for equality. /// Compares two <see cref="NormalizedByte2"/> objects for equality.
@ -57,6 +59,10 @@ public partial struct NormalizedByte2(Vector2 vector) : IPixel<NormalizedByte2>,
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(NormalizedByte2 left, NormalizedByte2 right) => !left.Equals(right); public static bool operator !=(NormalizedByte2 left, NormalizedByte2 right) => !left.Equals(right);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4() public readonly Vector4 ToScaledVector4()
@ -94,6 +100,58 @@ public partial struct NormalizedByte2(Vector2 vector) : IPixel<NormalizedByte2>,
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromVector4(Vector4 source) => new() { PackedValue = Pack(new Vector2(source.X, source.Y)) }; public static NormalizedByte2 FromVector4(Vector4 source) => new() { PackedValue = Pack(new Vector2(source.X, source.Y)) };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte2 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <summary> /// <summary>
/// Expands the packed representation into a <see cref="Vector2"/>. /// Expands the packed representation into a <see cref="Vector2"/>.
/// The vector components are typically expanded in least to greatest significance order. /// The vector components are typically expanded in least to greatest significance order.

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

@ -13,11 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [-1, -1, -1, -1] to [1, 1, 1, 1] in vector form. /// Ranges from [-1, -1, -1, -1] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct NormalizedByte4 : IPixel<NormalizedByte4>, IPackedVector<uint>
/// Initializes a new instance of the <see cref="NormalizedByte4"/> struct.
/// </remarks>
/// <param name="vector">The vector containing the component values.</param>
public partial struct NormalizedByte4(Vector4 vector) : IPixel<NormalizedByte4>, IPackedVector<uint>
{ {
private const float MaxPos = 127f; private const float MaxPos = 127f;
private static readonly Vector4 Half = Vector128.Create(MaxPos).AsVector4(); private static readonly Vector4 Half = Vector128.Create(MaxPos).AsVector4();
@ -35,8 +31,14 @@ public partial struct NormalizedByte4(Vector4 vector) : IPixel<NormalizedByte4>,
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="NormalizedByte4"/> struct.
/// </summary>
/// <param name="vector">The vector containing the component values.</param>
public NormalizedByte4(Vector4 vector) => this.PackedValue = Pack(vector);
/// <inheritdoc/> /// <inheritdoc/>
public uint PackedValue { get; set; } = Pack(vector); public uint PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="NormalizedByte4"/> objects for equality. /// Compares two <see cref="NormalizedByte4"/> objects for equality.
@ -60,6 +62,10 @@ public partial struct NormalizedByte4(Vector4 vector) : IPixel<NormalizedByte4>,
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(NormalizedByte4 left, NormalizedByte4 right) => !left.Equals(right); public static bool operator !=(NormalizedByte4 left, NormalizedByte4 right) => !left.Equals(right);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4() public readonly Vector4 ToScaledVector4()
@ -97,6 +103,58 @@ public partial struct NormalizedByte4(Vector4 vector) : IPixel<NormalizedByte4>,
return FromVector4(source); return FromVector4(source);
} }
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedByte4 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) }; public static NormalizedByte4 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) };

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

@ -12,11 +12,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [-1, -1, 0, 1] to [1, 1, 0, 1] in vector form. /// Ranges from [-1, -1, 0, 1] to [1, 1, 0, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct NormalizedShort2 : IPixel<NormalizedShort2>, IPackedVector<uint>
/// Initializes a new instance of the <see cref="NormalizedShort2"/> struct.
/// </remarks>
/// <param name="vector">The vector containing the component values.</param>
public partial struct NormalizedShort2(Vector2 vector) : IPixel<NormalizedShort2>, IPackedVector<uint>
{ {
// Largest two byte positive number 0xFFFF >> 1; // Largest two byte positive number 0xFFFF >> 1;
private const float MaxPos = 0x7FFF; private const float MaxPos = 0x7FFF;
@ -34,8 +30,14 @@ public partial struct NormalizedShort2(Vector2 vector) : IPixel<NormalizedShort2
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="NormalizedShort2"/> struct.
/// </summary>
/// <param name="vector">The vector containing the component values.</param>
public NormalizedShort2(Vector2 vector) => this.PackedValue = Pack(vector);
/// <inheritdoc/> /// <inheritdoc/>
public uint PackedValue { get; set; } = Pack(vector); public uint PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="NormalizedShort2"/> objects for equality. /// Compares two <see cref="NormalizedShort2"/> objects for equality.
@ -59,6 +61,10 @@ public partial struct NormalizedShort2(Vector2 vector) : IPixel<NormalizedShort2
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(NormalizedShort2 left, NormalizedShort2 right) => !left.Equals(right); public static bool operator !=(NormalizedShort2 left, NormalizedShort2 right) => !left.Equals(right);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4() public readonly Vector4 ToScaledVector4()
@ -96,6 +102,58 @@ public partial struct NormalizedShort2(Vector2 vector) : IPixel<NormalizedShort2
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromVector4(Vector4 source) => new() { PackedValue = Pack(new Vector2(source.X, source.Y)) }; public static NormalizedShort2 FromVector4(Vector4 source) => new() { PackedValue = Pack(new Vector2(source.X, source.Y)) };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort2 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <summary> /// <summary>
/// Expands the packed representation into a <see cref="Vector2"/>. /// Expands the packed representation into a <see cref="Vector2"/>.
/// The vector components are typically expanded in least to greatest significance order. /// The vector components are typically expanded in least to greatest significance order.

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

@ -13,11 +13,7 @@ namespace SixLabors.ImageSharp.PixelFormats;
/// Ranges from [-1, -1, -1, -1] to [1, 1, 1, 1] in vector form. /// Ranges from [-1, -1, -1, -1] to [1, 1, 1, 1] in vector form.
/// </para> /// </para>
/// </summary> /// </summary>
/// <remarks> public partial struct NormalizedShort4 : IPixel<NormalizedShort4>, IPackedVector<ulong>
/// Initializes a new instance of the <see cref="NormalizedShort4"/> struct.
/// </remarks>
/// <param name="vector">The vector containing the component values.</param>
public partial struct NormalizedShort4(Vector4 vector) : IPixel<NormalizedShort4>, IPackedVector<ulong>
{ {
// Largest two byte positive number 0xFFFF >> 1; // Largest two byte positive number 0xFFFF >> 1;
private const float MaxPos = 0x7FFF; private const float MaxPos = 0x7FFF;
@ -36,8 +32,14 @@ public partial struct NormalizedShort4(Vector4 vector) : IPixel<NormalizedShort4
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="NormalizedShort4"/> struct.
/// </summary>
/// <param name="vector">The vector containing the component values.</param>
public NormalizedShort4(Vector4 vector) => this.PackedValue = Pack(vector);
/// <inheritdoc/> /// <inheritdoc/>
public ulong PackedValue { get; set; } = Pack(vector); public ulong PackedValue { get; set; }
/// <summary> /// <summary>
/// Compares two <see cref="NormalizedShort4"/> objects for equality. /// Compares two <see cref="NormalizedShort4"/> objects for equality.
@ -61,6 +63,10 @@ public partial struct NormalizedShort4(Vector4 vector) : IPixel<NormalizedShort4
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(NormalizedShort4 left, NormalizedShort4 right) => !left.Equals(right); public static bool operator !=(NormalizedShort4 left, NormalizedShort4 right) => !left.Equals(right);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Rgba32 ToRgba32() => Rgba32.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/> /// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Vector4 ToScaledVector4() public readonly Vector4 ToScaledVector4()
@ -102,6 +108,58 @@ public partial struct NormalizedShort4(Vector4 vector) : IPixel<NormalizedShort4
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) }; public static NormalizedShort4 FromVector4(Vector4 source) => new() { PackedValue = Pack(source) };
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromAbgr32(Abgr32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromArgb32(Argb32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromBgra5551(Bgra5551 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromBgr24(Bgr24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromBgra32(Bgra32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromL8(L8 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromL16(L16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromLa16(La16 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromLa32(La32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromRgb24(Rgb24 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromRgba32(Rgba32 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromRgb48(Rgb48 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NormalizedShort4 FromRgba64(Rgba64 source) => FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc /> /// <inheritdoc />
public override readonly bool Equals(object? obj) => obj is NormalizedShort4 other && this.Equals(other); public override readonly bool Equals(object? obj) => obj is NormalizedShort4 other && this.Equals(other);

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

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.PixelFormats;
/// <content> /// <content>

2
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Abgr32.PixelOperations.cs

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.PixelFormats;
/// <content> /// <content>

2
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Argb32.PixelOperations.cs

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.PixelFormats;
/// <content> /// <content>

2
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgr24.PixelOperations.cs

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.PixelFormats;
/// <content> /// <content>

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

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.PixelFormats;
/// <content> /// <content>

2
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgra32.PixelOperations.cs

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.PixelFormats;
/// <content> /// <content>

2
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgra4444.PixelOperations.cs

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.PixelFormats;
/// <content> /// <content>

2
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Bgra5551.PixelOperations.cs

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.PixelFormats;
/// <content> /// <content>

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

@ -1,8 +1,6 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.PixelFormats;
/// <content> /// <content>

267
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Abgr32.PixelOperations.Generated.cs

@ -21,337 +21,316 @@ public partial struct Abgr32
internal partial class PixelOperations : PixelOperations<Abgr32> internal partial class PixelOperations : PixelOperations<Abgr32>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override void FromAbgr32(Configuration configuration, ReadOnlySpan<Abgr32> source, Span<Abgr32> destinationPixels) public override void FromAbgr32(Configuration configuration, ReadOnlySpan<Abgr32> source, Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
source.CopyTo(destinationPixels.Slice(0, source.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToAbgr32(Configuration configuration, ReadOnlySpan<Abgr32> sourcePixels, Span<Abgr32> destinationPixels) public override void ToAbgr32(Configuration configuration, ReadOnlySpan<Abgr32> source, Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromVector4Destructive( public override void FromVector4Destructive(
Configuration configuration, Configuration configuration,
Span<Vector4> sourceVectors, Span<Vector4> sourceVectors,
Span<Abgr32> destinationPixels, Span<Abgr32> destination,
PixelConversionModifiers modifiers) PixelConversionModifiers modifiers)
{ {
Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destinationPixels, modifiers.Remove(PixelConversionModifiers.Scale)); Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destination, modifiers.Remove(PixelConversionModifiers.Scale));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToVector4( public override void ToVector4(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Vector4> destVectors, Span<Vector4> destination,
PixelConversionModifiers modifiers) PixelConversionModifiers modifiers)
{ {
Vector4Converters.RgbaCompatible.ToVector4(configuration, this, sourcePixels, destVectors, modifiers.Remove(PixelConversionModifiers.Scale)); Vector4Converters.RgbaCompatible.ToVector4(configuration, this, source, destination, modifiers.Remove(PixelConversionModifiers.Scale));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba32( public override void ToRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Rgba32> destinationPixels) Span<Rgba32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Abgr32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Abgr32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Rgba32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Rgba32, byte>(destination);
PixelConverter.FromAbgr32.ToRgba32(source, dest); PixelConverter.FromAbgr32.ToRgba32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromRgba32( public override void FromRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Rgba32> sourcePixels, ReadOnlySpan<Rgba32> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Rgba32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Rgba32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Abgr32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Abgr32, byte>(destination);
PixelConverter.FromRgba32.ToAbgr32(source, dest); PixelConverter.FromRgba32.ToAbgr32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToArgb32( public override void ToArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Abgr32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Abgr32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Argb32, byte>(destination);
PixelConverter.FromAbgr32.ToArgb32(source, dest); PixelConverter.FromAbgr32.ToArgb32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromArgb32( public override void FromArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Argb32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Argb32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Abgr32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Abgr32, byte>(destination);
PixelConverter.FromArgb32.ToAbgr32(source, dest); PixelConverter.FromArgb32.ToAbgr32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra32( public override void ToBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Abgr32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Abgr32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgra32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgra32, byte>(destination);
PixelConverter.FromAbgr32.ToBgra32(source, dest); PixelConverter.FromAbgr32.ToBgra32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromBgra32( public override void FromBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgra32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgra32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Abgr32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Abgr32, byte>(destination);
PixelConverter.FromBgra32.ToAbgr32(source, dest); PixelConverter.FromBgra32.ToAbgr32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb24( public override void ToRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Rgb24> destinationPixels) Span<Rgb24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Abgr32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Abgr32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Rgb24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Rgb24, byte>(destination);
PixelConverter.FromAbgr32.ToRgb24(source, dest); PixelConverter.FromAbgr32.ToRgb24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromRgb24( public override void FromRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Rgb24> sourcePixels, ReadOnlySpan<Rgb24> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Rgb24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Rgb24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Abgr32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Abgr32, byte>(destination);
PixelConverter.FromRgb24.ToAbgr32(source, dest); PixelConverter.FromRgb24.ToAbgr32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgr24( public override void ToBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Abgr32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Abgr32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgr24, byte>(destination);
PixelConverter.FromAbgr32.ToBgr24(source, dest); PixelConverter.FromAbgr32.ToBgr24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromBgr24( public override void FromBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgr24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgr24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Abgr32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Abgr32, byte>(destination);
PixelConverter.FromBgr24.ToAbgr32(source, dest); PixelConverter.FromBgr24.ToAbgr32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL8( public override void ToL8(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<L8> destinationPixels) Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Abgr32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Abgr32 sourceBase = ref MemoryMarshal.GetReference(source);
ref L8 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L8 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L8.FromAbgr32(Unsafe.Add(ref sourceBase, i));
ref L8 dp = ref Unsafe.Add(ref destRef, i);
dp.FromAbgr32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL16( public override void ToL16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<L16> destinationPixels) Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Abgr32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Abgr32 sourceBase = ref MemoryMarshal.GetReference(source);
ref L16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L16.FromAbgr32(Unsafe.Add(ref sourceBase, i));
ref L16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromAbgr32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa16( public override void ToLa16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<La16> destinationPixels) Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Abgr32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Abgr32 sourceBase = ref MemoryMarshal.GetReference(source);
ref La16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La16.FromAbgr32(Unsafe.Add(ref sourceBase, i));
ref La16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromAbgr32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa32( public override void ToLa32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<La32> destinationPixels) Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Abgr32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Abgr32 sourceBase = ref MemoryMarshal.GetReference(source);
ref La32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La32.FromAbgr32(Unsafe.Add(ref sourceBase, i));
ref La32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromAbgr32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb48( public override void ToRgb48(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Rgb48> destinationPixels) Span<Rgb48> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Abgr32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Abgr32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb48 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb48 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb48.FromAbgr32(Unsafe.Add(ref sourceBase, i));
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);
dp.FromAbgr32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba64( public override void ToRgba64(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Rgba64> destinationPixels) Span<Rgba64> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Abgr32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Abgr32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba64 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba64 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba64.FromAbgr32(Unsafe.Add(ref sourceBase, i));
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);
dp.FromAbgr32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra5551( public override void ToBgra5551(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Bgra5551> destinationPixels) Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Abgr32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Abgr32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra5551 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra5551 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra5551.FromAbgr32(Unsafe.Add(ref sourceBase, i));
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);
dp.FromAbgr32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void From<TSourcePixel>( public override void From<TSourcePixel>(
Configuration configuration, Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels, ReadOnlySpan<TSourcePixel> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
PixelOperations<TSourcePixel>.Instance.ToAbgr32(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length)); PixelOperations<TSourcePixel>.Instance.ToAbgr32(configuration, source, destination.Slice(0, source.Length));
} }
} }
} }

267
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Argb32.PixelOperations.Generated.cs

@ -21,337 +21,316 @@ public partial struct Argb32
internal partial class PixelOperations : PixelOperations<Argb32> internal partial class PixelOperations : PixelOperations<Argb32>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override void FromArgb32(Configuration configuration, ReadOnlySpan<Argb32> source, Span<Argb32> destinationPixels) public override void FromArgb32(Configuration configuration, ReadOnlySpan<Argb32> source, Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
source.CopyTo(destinationPixels.Slice(0, source.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToArgb32(Configuration configuration, ReadOnlySpan<Argb32> sourcePixels, Span<Argb32> destinationPixels) public override void ToArgb32(Configuration configuration, ReadOnlySpan<Argb32> source, Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromVector4Destructive( public override void FromVector4Destructive(
Configuration configuration, Configuration configuration,
Span<Vector4> sourceVectors, Span<Vector4> sourceVectors,
Span<Argb32> destinationPixels, Span<Argb32> destination,
PixelConversionModifiers modifiers) PixelConversionModifiers modifiers)
{ {
Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destinationPixels, modifiers.Remove(PixelConversionModifiers.Scale)); Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destination, modifiers.Remove(PixelConversionModifiers.Scale));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToVector4( public override void ToVector4(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Vector4> destVectors, Span<Vector4> destination,
PixelConversionModifiers modifiers) PixelConversionModifiers modifiers)
{ {
Vector4Converters.RgbaCompatible.ToVector4(configuration, this, sourcePixels, destVectors, modifiers.Remove(PixelConversionModifiers.Scale)); Vector4Converters.RgbaCompatible.ToVector4(configuration, this, source, destination, modifiers.Remove(PixelConversionModifiers.Scale));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba32( public override void ToRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Rgba32> destinationPixels) Span<Rgba32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Argb32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Argb32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Rgba32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Rgba32, byte>(destination);
PixelConverter.FromArgb32.ToRgba32(source, dest); PixelConverter.FromArgb32.ToRgba32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromRgba32( public override void FromRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Rgba32> sourcePixels, ReadOnlySpan<Rgba32> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Rgba32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Rgba32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Argb32, byte>(destination);
PixelConverter.FromRgba32.ToArgb32(source, dest); PixelConverter.FromRgba32.ToArgb32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToAbgr32( public override void ToAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Argb32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Argb32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Abgr32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Abgr32, byte>(destination);
PixelConverter.FromArgb32.ToAbgr32(source, dest); PixelConverter.FromArgb32.ToAbgr32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromAbgr32( public override void FromAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Abgr32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Abgr32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Argb32, byte>(destination);
PixelConverter.FromAbgr32.ToArgb32(source, dest); PixelConverter.FromAbgr32.ToArgb32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra32( public override void ToBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Argb32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Argb32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgra32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgra32, byte>(destination);
PixelConverter.FromArgb32.ToBgra32(source, dest); PixelConverter.FromArgb32.ToBgra32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromBgra32( public override void FromBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgra32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgra32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Argb32, byte>(destination);
PixelConverter.FromBgra32.ToArgb32(source, dest); PixelConverter.FromBgra32.ToArgb32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb24( public override void ToRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Rgb24> destinationPixels) Span<Rgb24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Argb32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Argb32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Rgb24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Rgb24, byte>(destination);
PixelConverter.FromArgb32.ToRgb24(source, dest); PixelConverter.FromArgb32.ToRgb24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromRgb24( public override void FromRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Rgb24> sourcePixels, ReadOnlySpan<Rgb24> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Rgb24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Rgb24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Argb32, byte>(destination);
PixelConverter.FromRgb24.ToArgb32(source, dest); PixelConverter.FromRgb24.ToArgb32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgr24( public override void ToBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Argb32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Argb32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgr24, byte>(destination);
PixelConverter.FromArgb32.ToBgr24(source, dest); PixelConverter.FromArgb32.ToBgr24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromBgr24( public override void FromBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgr24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgr24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Argb32, byte>(destination);
PixelConverter.FromBgr24.ToArgb32(source, dest); PixelConverter.FromBgr24.ToArgb32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL8( public override void ToL8(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<L8> destinationPixels) Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Argb32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Argb32 sourceBase = ref MemoryMarshal.GetReference(source);
ref L8 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L8 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L8.FromArgb32(Unsafe.Add(ref sourceBase, i));
ref L8 dp = ref Unsafe.Add(ref destRef, i);
dp.FromArgb32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL16( public override void ToL16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<L16> destinationPixels) Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Argb32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Argb32 sourceBase = ref MemoryMarshal.GetReference(source);
ref L16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L16.FromArgb32(Unsafe.Add(ref sourceBase, i));
ref L16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromArgb32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa16( public override void ToLa16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<La16> destinationPixels) Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Argb32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Argb32 sourceBase = ref MemoryMarshal.GetReference(source);
ref La16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La16.FromArgb32(Unsafe.Add(ref sourceBase, i));
ref La16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromArgb32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa32( public override void ToLa32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<La32> destinationPixels) Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Argb32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Argb32 sourceBase = ref MemoryMarshal.GetReference(source);
ref La32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La32.FromArgb32(Unsafe.Add(ref sourceBase, i));
ref La32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromArgb32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb48( public override void ToRgb48(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Rgb48> destinationPixels) Span<Rgb48> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Argb32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Argb32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb48 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb48 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb48.FromArgb32(Unsafe.Add(ref sourceBase, i));
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);
dp.FromArgb32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba64( public override void ToRgba64(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Rgba64> destinationPixels) Span<Rgba64> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Argb32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Argb32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba64 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba64 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba64.FromArgb32(Unsafe.Add(ref sourceBase, i));
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);
dp.FromArgb32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra5551( public override void ToBgra5551(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Bgra5551> destinationPixels) Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Argb32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Argb32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra5551 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra5551 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra5551.FromArgb32(Unsafe.Add(ref sourceBase, i));
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);
dp.FromArgb32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void From<TSourcePixel>( public override void From<TSourcePixel>(
Configuration configuration, Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels, ReadOnlySpan<TSourcePixel> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
PixelOperations<TSourcePixel>.Instance.ToArgb32(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length)); PixelOperations<TSourcePixel>.Instance.ToArgb32(configuration, source, destination.Slice(0, source.Length));
} }
} }
} }

267
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Bgr24.PixelOperations.Generated.cs

@ -21,337 +21,316 @@ public partial struct Bgr24
internal partial class PixelOperations : PixelOperations<Bgr24> internal partial class PixelOperations : PixelOperations<Bgr24>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override void FromBgr24(Configuration configuration, ReadOnlySpan<Bgr24> source, Span<Bgr24> destinationPixels) public override void FromBgr24(Configuration configuration, ReadOnlySpan<Bgr24> source, Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
source.CopyTo(destinationPixels.Slice(0, source.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgr24(Configuration configuration, ReadOnlySpan<Bgr24> sourcePixels, Span<Bgr24> destinationPixels) public override void ToBgr24(Configuration configuration, ReadOnlySpan<Bgr24> source, Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromVector4Destructive( public override void FromVector4Destructive(
Configuration configuration, Configuration configuration,
Span<Vector4> sourceVectors, Span<Vector4> sourceVectors,
Span<Bgr24> destinationPixels, Span<Bgr24> destination,
PixelConversionModifiers modifiers) PixelConversionModifiers modifiers)
{ {
Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destinationPixels, modifiers.Remove(PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply)); Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destination, modifiers.Remove(PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToVector4( public override void ToVector4(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Vector4> destVectors, Span<Vector4> destination,
PixelConversionModifiers modifiers) PixelConversionModifiers modifiers)
{ {
Vector4Converters.RgbaCompatible.ToVector4(configuration, this, sourcePixels, destVectors, modifiers.Remove(PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply)); Vector4Converters.RgbaCompatible.ToVector4(configuration, this, source, destination, modifiers.Remove(PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba32( public override void ToRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Rgba32> destinationPixels) Span<Rgba32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgr24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgr24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Rgba32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Rgba32, byte>(destination);
PixelConverter.FromBgr24.ToRgba32(source, dest); PixelConverter.FromBgr24.ToRgba32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromRgba32( public override void FromRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Rgba32> sourcePixels, ReadOnlySpan<Rgba32> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Rgba32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Rgba32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgr24, byte>(destination);
PixelConverter.FromRgba32.ToBgr24(source, dest); PixelConverter.FromRgba32.ToBgr24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToArgb32( public override void ToArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgr24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgr24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Argb32, byte>(destination);
PixelConverter.FromBgr24.ToArgb32(source, dest); PixelConverter.FromBgr24.ToArgb32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromArgb32( public override void FromArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Argb32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Argb32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgr24, byte>(destination);
PixelConverter.FromArgb32.ToBgr24(source, dest); PixelConverter.FromArgb32.ToBgr24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToAbgr32( public override void ToAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgr24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgr24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Abgr32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Abgr32, byte>(destination);
PixelConverter.FromBgr24.ToAbgr32(source, dest); PixelConverter.FromBgr24.ToAbgr32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromAbgr32( public override void FromAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Abgr32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Abgr32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgr24, byte>(destination);
PixelConverter.FromAbgr32.ToBgr24(source, dest); PixelConverter.FromAbgr32.ToBgr24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra32( public override void ToBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgr24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgr24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgra32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgra32, byte>(destination);
PixelConverter.FromBgr24.ToBgra32(source, dest); PixelConverter.FromBgr24.ToBgra32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromBgra32( public override void FromBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgra32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgra32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgr24, byte>(destination);
PixelConverter.FromBgra32.ToBgr24(source, dest); PixelConverter.FromBgra32.ToBgr24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb24( public override void ToRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Rgb24> destinationPixels) Span<Rgb24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgr24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgr24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Rgb24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Rgb24, byte>(destination);
PixelConverter.FromBgr24.ToRgb24(source, dest); PixelConverter.FromBgr24.ToRgb24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromRgb24( public override void FromRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Rgb24> sourcePixels, ReadOnlySpan<Rgb24> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Rgb24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Rgb24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgr24, byte>(destination);
PixelConverter.FromRgb24.ToBgr24(source, dest); PixelConverter.FromRgb24.ToBgr24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL8( public override void ToL8(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<L8> destinationPixels) Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgr24 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgr24 sourceBase = ref MemoryMarshal.GetReference(source);
ref L8 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L8 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L8.FromBgr24(Unsafe.Add(ref sourceBase, i));
ref L8 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgr24(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL16( public override void ToL16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<L16> destinationPixels) Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgr24 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgr24 sourceBase = ref MemoryMarshal.GetReference(source);
ref L16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L16.FromBgr24(Unsafe.Add(ref sourceBase, i));
ref L16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgr24(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa16( public override void ToLa16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<La16> destinationPixels) Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgr24 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgr24 sourceBase = ref MemoryMarshal.GetReference(source);
ref La16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La16.FromBgr24(Unsafe.Add(ref sourceBase, i));
ref La16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgr24(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa32( public override void ToLa32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<La32> destinationPixels) Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgr24 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgr24 sourceBase = ref MemoryMarshal.GetReference(source);
ref La32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La32.FromBgr24(Unsafe.Add(ref sourceBase, i));
ref La32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgr24(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb48( public override void ToRgb48(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Rgb48> destinationPixels) Span<Rgb48> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgr24 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgr24 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb48 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb48 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb48.FromBgr24(Unsafe.Add(ref sourceBase, i));
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgr24(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba64( public override void ToRgba64(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Rgba64> destinationPixels) Span<Rgba64> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgr24 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgr24 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba64 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba64 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba64.FromBgr24(Unsafe.Add(ref sourceBase, i));
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgr24(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra5551( public override void ToBgra5551(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Bgra5551> destinationPixels) Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgr24 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgr24 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra5551 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra5551 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra5551.FromBgr24(Unsafe.Add(ref sourceBase, i));
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgr24(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void From<TSourcePixel>( public override void From<TSourcePixel>(
Configuration configuration, Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels, ReadOnlySpan<TSourcePixel> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
PixelOperations<TSourcePixel>.Instance.ToBgr24(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length)); PixelOperations<TSourcePixel>.Instance.ToBgr24(configuration, source, destination.Slice(0, source.Length));
} }
} }
} }

267
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Bgra32.PixelOperations.Generated.cs

@ -21,337 +21,316 @@ public partial struct Bgra32
internal partial class PixelOperations : PixelOperations<Bgra32> internal partial class PixelOperations : PixelOperations<Bgra32>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override void FromBgra32(Configuration configuration, ReadOnlySpan<Bgra32> source, Span<Bgra32> destinationPixels) public override void FromBgra32(Configuration configuration, ReadOnlySpan<Bgra32> source, Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
source.CopyTo(destinationPixels.Slice(0, source.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra32(Configuration configuration, ReadOnlySpan<Bgra32> sourcePixels, Span<Bgra32> destinationPixels) public override void ToBgra32(Configuration configuration, ReadOnlySpan<Bgra32> source, Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromVector4Destructive( public override void FromVector4Destructive(
Configuration configuration, Configuration configuration,
Span<Vector4> sourceVectors, Span<Vector4> sourceVectors,
Span<Bgra32> destinationPixels, Span<Bgra32> destination,
PixelConversionModifiers modifiers) PixelConversionModifiers modifiers)
{ {
Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destinationPixels, modifiers.Remove(PixelConversionModifiers.Scale)); Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destination, modifiers.Remove(PixelConversionModifiers.Scale));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToVector4( public override void ToVector4(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Vector4> destVectors, Span<Vector4> destination,
PixelConversionModifiers modifiers) PixelConversionModifiers modifiers)
{ {
Vector4Converters.RgbaCompatible.ToVector4(configuration, this, sourcePixels, destVectors, modifiers.Remove(PixelConversionModifiers.Scale)); Vector4Converters.RgbaCompatible.ToVector4(configuration, this, source, destination, modifiers.Remove(PixelConversionModifiers.Scale));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba32( public override void ToRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Rgba32> destinationPixels) Span<Rgba32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgra32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgra32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Rgba32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Rgba32, byte>(destination);
PixelConverter.FromBgra32.ToRgba32(source, dest); PixelConverter.FromBgra32.ToRgba32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromRgba32( public override void FromRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Rgba32> sourcePixels, ReadOnlySpan<Rgba32> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Rgba32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Rgba32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgra32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgra32, byte>(destination);
PixelConverter.FromRgba32.ToBgra32(source, dest); PixelConverter.FromRgba32.ToBgra32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToArgb32( public override void ToArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgra32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgra32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Argb32, byte>(destination);
PixelConverter.FromBgra32.ToArgb32(source, dest); PixelConverter.FromBgra32.ToArgb32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromArgb32( public override void FromArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Argb32> sourcePixels, ReadOnlySpan<Argb32> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Argb32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Argb32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgra32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgra32, byte>(destination);
PixelConverter.FromArgb32.ToBgra32(source, dest); PixelConverter.FromArgb32.ToBgra32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToAbgr32( public override void ToAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgra32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgra32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Abgr32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Abgr32, byte>(destination);
PixelConverter.FromBgra32.ToAbgr32(source, dest); PixelConverter.FromBgra32.ToAbgr32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromAbgr32( public override void FromAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Abgr32> sourcePixels, ReadOnlySpan<Abgr32> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Abgr32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Abgr32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgra32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgra32, byte>(destination);
PixelConverter.FromAbgr32.ToBgra32(source, dest); PixelConverter.FromAbgr32.ToBgra32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb24( public override void ToRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Rgb24> destinationPixels) Span<Rgb24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgra32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgra32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Rgb24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Rgb24, byte>(destination);
PixelConverter.FromBgra32.ToRgb24(source, dest); PixelConverter.FromBgra32.ToRgb24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromRgb24( public override void FromRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Rgb24> sourcePixels, ReadOnlySpan<Rgb24> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Rgb24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Rgb24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgra32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgra32, byte>(destination);
PixelConverter.FromRgb24.ToBgra32(source, dest); PixelConverter.FromRgb24.ToBgra32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgr24( public override void ToBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgra32, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgra32, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgr24, byte>(destination);
PixelConverter.FromBgra32.ToBgr24(source, dest); PixelConverter.FromBgra32.ToBgr24(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void FromBgr24( public override void FromBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgr24> sourcePixels, ReadOnlySpan<Bgr24> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ReadOnlySpan<byte> source = MemoryMarshal.Cast<Bgr24, byte>(sourcePixels); ReadOnlySpan<byte> sourceBytes = MemoryMarshal.Cast<Bgr24, byte>(source);
Span<byte> dest = MemoryMarshal.Cast<Bgra32, byte>(destinationPixels); Span<byte> destinationBytes = MemoryMarshal.Cast<Bgra32, byte>(destination);
PixelConverter.FromBgr24.ToBgra32(source, dest); PixelConverter.FromBgr24.ToBgra32(sourceBytes, destinationBytes);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL8( public override void ToL8(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<L8> destinationPixels) Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra32 sourceBase = ref MemoryMarshal.GetReference(source);
ref L8 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L8 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L8.FromBgra32(Unsafe.Add(ref sourceBase, i));
ref L8 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL16( public override void ToL16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<L16> destinationPixels) Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra32 sourceBase = ref MemoryMarshal.GetReference(source);
ref L16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L16.FromBgra32(Unsafe.Add(ref sourceBase, i));
ref L16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa16( public override void ToLa16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<La16> destinationPixels) Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra32 sourceBase = ref MemoryMarshal.GetReference(source);
ref La16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La16.FromBgra32(Unsafe.Add(ref sourceBase, i));
ref La16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa32( public override void ToLa32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<La32> destinationPixels) Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra32 sourceBase = ref MemoryMarshal.GetReference(source);
ref La32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La32.FromBgra32(Unsafe.Add(ref sourceBase, i));
ref La32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb48( public override void ToRgb48(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Rgb48> destinationPixels) Span<Rgb48> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb48 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb48 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb48.FromBgra32(Unsafe.Add(ref sourceBase, i));
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba64( public override void ToRgba64(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Rgba64> destinationPixels) Span<Rgba64> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba64 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba64 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba64.FromBgra32(Unsafe.Add(ref sourceBase, i));
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra5551( public override void ToBgra5551(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra32> sourcePixels, ReadOnlySpan<Bgra32> source,
Span<Bgra5551> destinationPixels) Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra5551 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra5551 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra5551.FromBgra32(Unsafe.Add(ref sourceBase, i));
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void From<TSourcePixel>( public override void From<TSourcePixel>(
Configuration configuration, Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels, ReadOnlySpan<TSourcePixel> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
PixelOperations<TSourcePixel>.Instance.ToBgra32(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length)); PixelOperations<TSourcePixel>.Instance.ToBgra32(configuration, source, destination.Slice(0, source.Length));
} }
} }
} }

222
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/Bgra5551.PixelOperations.Generated.cs

@ -21,282 +21,246 @@ public partial struct Bgra5551
internal partial class PixelOperations : PixelOperations<Bgra5551> internal partial class PixelOperations : PixelOperations<Bgra5551>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override void FromBgra5551(Configuration configuration, ReadOnlySpan<Bgra5551> source, Span<Bgra5551> destinationPixels) public override void FromBgra5551(Configuration configuration, ReadOnlySpan<Bgra5551> source, Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
source.CopyTo(destinationPixels.Slice(0, source.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra5551(Configuration configuration, ReadOnlySpan<Bgra5551> sourcePixels, Span<Bgra5551> destinationPixels) public override void ToBgra5551(Configuration configuration, ReadOnlySpan<Bgra5551> source, Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToArgb32( public override void ToArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref Argb32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Argb32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Argb32.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref Argb32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToAbgr32( public override void ToAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref Abgr32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Abgr32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Abgr32.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref Abgr32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgr24( public override void ToBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgr24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgr24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgr24.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref Bgr24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra32( public override void ToBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra32.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref Bgra32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL8( public override void ToL8(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<L8> destinationPixels) Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref L8 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L8 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L8.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref L8 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL16( public override void ToL16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<L16> destinationPixels) Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref L16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L16.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref L16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa16( public override void ToLa16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<La16> destinationPixels) Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref La16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La16.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref La16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa32( public override void ToLa32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<La32> destinationPixels) Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref La32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La32.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref La32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb24( public override void ToRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<Rgb24> destinationPixels) Span<Rgb24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb24.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref Rgb24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba32( public override void ToRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<Rgba32> destinationPixels) Span<Rgba32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba32.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb48( public override void ToRgb48(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<Rgb48> destinationPixels) Span<Rgb48> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb48 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb48 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb48.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba64( public override void ToRgba64(
Configuration configuration, Configuration configuration,
ReadOnlySpan<Bgra5551> sourcePixels, ReadOnlySpan<Bgra5551> source,
Span<Rgba64> destinationPixels) Span<Rgba64> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref Bgra5551 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref Bgra5551 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba64 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba64 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Bgra5551 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba64.FromBgra5551(Unsafe.Add(ref sourceBase, i));
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);
dp.FromBgra5551(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void From<TSourcePixel>( public override void From<TSourcePixel>(
Configuration configuration, Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels, ReadOnlySpan<TSourcePixel> source,
Span<Bgra5551> destinationPixels) Span<Bgra5551> destination)
{ {
PixelOperations<TSourcePixel>.Instance.ToBgra5551(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length)); PixelOperations<TSourcePixel>.Instance.ToBgra5551(configuration, source, destination.Slice(0, source.Length));
} }
} }
} }

222
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/L16.PixelOperations.Generated.cs

@ -21,282 +21,246 @@ public partial struct L16
internal partial class PixelOperations : PixelOperations<L16> internal partial class PixelOperations : PixelOperations<L16>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override void FromL16(Configuration configuration, ReadOnlySpan<L16> source, Span<L16> destinationPixels) public override void FromL16(Configuration configuration, ReadOnlySpan<L16> source, Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
source.CopyTo(destinationPixels.Slice(0, source.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL16(Configuration configuration, ReadOnlySpan<L16> sourcePixels, Span<L16> destinationPixels) public override void ToL16(Configuration configuration, ReadOnlySpan<L16> source, Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToArgb32( public override void ToArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Argb32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Argb32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Argb32.FromL16(Unsafe.Add(ref sourceBase, i));
ref Argb32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToAbgr32( public override void ToAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Abgr32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Abgr32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Abgr32.FromL16(Unsafe.Add(ref sourceBase, i));
ref Abgr32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgr24( public override void ToBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgr24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgr24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgr24.FromL16(Unsafe.Add(ref sourceBase, i));
ref Bgr24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra32( public override void ToBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra32.FromL16(Unsafe.Add(ref sourceBase, i));
ref Bgra32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL8( public override void ToL8(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<L8> destinationPixels) Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref L8 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L8 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L8.FromL16(Unsafe.Add(ref sourceBase, i));
ref L8 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa16( public override void ToLa16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<La16> destinationPixels) Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref La16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La16.FromL16(Unsafe.Add(ref sourceBase, i));
ref La16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa32( public override void ToLa32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<La32> destinationPixels) Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref La32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La32.FromL16(Unsafe.Add(ref sourceBase, i));
ref La32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb24( public override void ToRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<Rgb24> destinationPixels) Span<Rgb24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb24.FromL16(Unsafe.Add(ref sourceBase, i));
ref Rgb24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba32( public override void ToRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<Rgba32> destinationPixels) Span<Rgba32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba32.FromL16(Unsafe.Add(ref sourceBase, i));
ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb48( public override void ToRgb48(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<Rgb48> destinationPixels) Span<Rgb48> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb48 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb48 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb48.FromL16(Unsafe.Add(ref sourceBase, i));
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba64( public override void ToRgba64(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<Rgba64> destinationPixels) Span<Rgba64> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba64 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba64 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba64.FromL16(Unsafe.Add(ref sourceBase, i));
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra5551( public override void ToBgra5551(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L16> sourcePixels, ReadOnlySpan<L16> source,
Span<Bgra5551> destinationPixels) Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra5551 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra5551 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra5551.FromL16(Unsafe.Add(ref sourceBase, i));
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void From<TSourcePixel>( public override void From<TSourcePixel>(
Configuration configuration, Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels, ReadOnlySpan<TSourcePixel> source,
Span<L16> destinationPixels) Span<L16> destination)
{ {
PixelOperations<TSourcePixel>.Instance.ToL16(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length)); PixelOperations<TSourcePixel>.Instance.ToL16(configuration, source, destination.Slice(0, source.Length));
} }
} }
} }

222
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/L8.PixelOperations.Generated.cs

@ -21,282 +21,246 @@ public partial struct L8
internal partial class PixelOperations : PixelOperations<L8> internal partial class PixelOperations : PixelOperations<L8>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override void FromL8(Configuration configuration, ReadOnlySpan<L8> source, Span<L8> destinationPixels) public override void FromL8(Configuration configuration, ReadOnlySpan<L8> source, Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
source.CopyTo(destinationPixels.Slice(0, source.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL8(Configuration configuration, ReadOnlySpan<L8> sourcePixels, Span<L8> destinationPixels) public override void ToL8(Configuration configuration, ReadOnlySpan<L8> source, Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToArgb32( public override void ToArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref Argb32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Argb32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Argb32.FromL8(Unsafe.Add(ref sourceBase, i));
ref Argb32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToAbgr32( public override void ToAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref Abgr32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Abgr32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Abgr32.FromL8(Unsafe.Add(ref sourceBase, i));
ref Abgr32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgr24( public override void ToBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgr24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgr24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgr24.FromL8(Unsafe.Add(ref sourceBase, i));
ref Bgr24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra32( public override void ToBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra32.FromL8(Unsafe.Add(ref sourceBase, i));
ref Bgra32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL16( public override void ToL16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<L16> destinationPixels) Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref L16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L16.FromL8(Unsafe.Add(ref sourceBase, i));
ref L16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa16( public override void ToLa16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<La16> destinationPixels) Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref La16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La16.FromL8(Unsafe.Add(ref sourceBase, i));
ref La16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa32( public override void ToLa32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<La32> destinationPixels) Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref La32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La32.FromL8(Unsafe.Add(ref sourceBase, i));
ref La32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb24( public override void ToRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<Rgb24> destinationPixels) Span<Rgb24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb24.FromL8(Unsafe.Add(ref sourceBase, i));
ref Rgb24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba32( public override void ToRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<Rgba32> destinationPixels) Span<Rgba32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba32.FromL8(Unsafe.Add(ref sourceBase, i));
ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb48( public override void ToRgb48(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<Rgb48> destinationPixels) Span<Rgb48> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb48 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb48 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb48.FromL8(Unsafe.Add(ref sourceBase, i));
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba64( public override void ToRgba64(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<Rgba64> destinationPixels) Span<Rgba64> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba64 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba64 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba64.FromL8(Unsafe.Add(ref sourceBase, i));
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra5551( public override void ToBgra5551(
Configuration configuration, Configuration configuration,
ReadOnlySpan<L8> sourcePixels, ReadOnlySpan<L8> source,
Span<Bgra5551> destinationPixels) Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref L8 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref L8 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra5551 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra5551 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref L8 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra5551.FromL8(Unsafe.Add(ref sourceBase, i));
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);
dp.FromL8(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void From<TSourcePixel>( public override void From<TSourcePixel>(
Configuration configuration, Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels, ReadOnlySpan<TSourcePixel> source,
Span<L8> destinationPixels) Span<L8> destination)
{ {
PixelOperations<TSourcePixel>.Instance.ToL8(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length)); PixelOperations<TSourcePixel>.Instance.ToL8(configuration, source, destination.Slice(0, source.Length));
} }
} }
} }

222
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/La16.PixelOperations.Generated.cs

@ -21,282 +21,246 @@ public partial struct La16
internal partial class PixelOperations : PixelOperations<La16> internal partial class PixelOperations : PixelOperations<La16>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override void FromLa16(Configuration configuration, ReadOnlySpan<La16> source, Span<La16> destinationPixels) public override void FromLa16(Configuration configuration, ReadOnlySpan<La16> source, Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
source.CopyTo(destinationPixels.Slice(0, source.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa16(Configuration configuration, ReadOnlySpan<La16> sourcePixels, Span<La16> destinationPixels) public override void ToLa16(Configuration configuration, ReadOnlySpan<La16> source, Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToArgb32( public override void ToArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Argb32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Argb32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Argb32.FromLa16(Unsafe.Add(ref sourceBase, i));
ref Argb32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToAbgr32( public override void ToAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Abgr32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Abgr32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Abgr32.FromLa16(Unsafe.Add(ref sourceBase, i));
ref Abgr32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgr24( public override void ToBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgr24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgr24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgr24.FromLa16(Unsafe.Add(ref sourceBase, i));
ref Bgr24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra32( public override void ToBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra32.FromLa16(Unsafe.Add(ref sourceBase, i));
ref Bgra32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL8( public override void ToL8(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<L8> destinationPixels) Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref L8 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L8 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L8.FromLa16(Unsafe.Add(ref sourceBase, i));
ref L8 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL16( public override void ToL16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<L16> destinationPixels) Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref L16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L16.FromLa16(Unsafe.Add(ref sourceBase, i));
ref L16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa32( public override void ToLa32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<La32> destinationPixels) Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref La32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La32.FromLa16(Unsafe.Add(ref sourceBase, i));
ref La32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb24( public override void ToRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<Rgb24> destinationPixels) Span<Rgb24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb24.FromLa16(Unsafe.Add(ref sourceBase, i));
ref Rgb24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba32( public override void ToRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<Rgba32> destinationPixels) Span<Rgba32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba32.FromLa16(Unsafe.Add(ref sourceBase, i));
ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb48( public override void ToRgb48(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<Rgb48> destinationPixels) Span<Rgb48> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb48 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb48 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb48.FromLa16(Unsafe.Add(ref sourceBase, i));
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba64( public override void ToRgba64(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<Rgba64> destinationPixels) Span<Rgba64> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba64 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba64 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba64.FromLa16(Unsafe.Add(ref sourceBase, i));
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra5551( public override void ToBgra5551(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La16> sourcePixels, ReadOnlySpan<La16> source,
Span<Bgra5551> destinationPixels) Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La16 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La16 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra5551 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra5551 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La16 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra5551.FromLa16(Unsafe.Add(ref sourceBase, i));
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa16(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void From<TSourcePixel>( public override void From<TSourcePixel>(
Configuration configuration, Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels, ReadOnlySpan<TSourcePixel> source,
Span<La16> destinationPixels) Span<La16> destination)
{ {
PixelOperations<TSourcePixel>.Instance.ToLa16(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length)); PixelOperations<TSourcePixel>.Instance.ToLa16(configuration, source, destination.Slice(0, source.Length));
} }
} }
} }

222
src/ImageSharp/PixelFormats/PixelImplementations/PixelOperations/Generated/La32.PixelOperations.Generated.cs

@ -21,282 +21,246 @@ public partial struct La32
internal partial class PixelOperations : PixelOperations<La32> internal partial class PixelOperations : PixelOperations<La32>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override void FromLa32(Configuration configuration, ReadOnlySpan<La32> source, Span<La32> destinationPixels) public override void FromLa32(Configuration configuration, ReadOnlySpan<La32> source, Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
source.CopyTo(destinationPixels.Slice(0, source.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa32(Configuration configuration, ReadOnlySpan<La32> sourcePixels, Span<La32> destinationPixels) public override void ToLa32(Configuration configuration, ReadOnlySpan<La32> source, Span<La32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length)); source.CopyTo(destination.Slice(0, source.Length));
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToArgb32( public override void ToArgb32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<Argb32> destinationPixels) Span<Argb32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Argb32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Argb32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Argb32.FromLa32(Unsafe.Add(ref sourceBase, i));
ref Argb32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToAbgr32( public override void ToAbgr32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<Abgr32> destinationPixels) Span<Abgr32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Abgr32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Abgr32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Abgr32.FromLa32(Unsafe.Add(ref sourceBase, i));
ref Abgr32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgr24( public override void ToBgr24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<Bgr24> destinationPixels) Span<Bgr24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgr24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgr24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgr24.FromLa32(Unsafe.Add(ref sourceBase, i));
ref Bgr24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra32( public override void ToBgra32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<Bgra32> destinationPixels) Span<Bgra32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra32.FromLa32(Unsafe.Add(ref sourceBase, i));
ref Bgra32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL8( public override void ToL8(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<L8> destinationPixels) Span<L8> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref L8 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L8 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L8.FromLa32(Unsafe.Add(ref sourceBase, i));
ref L8 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToL16( public override void ToL16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<L16> destinationPixels) Span<L16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref L16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref L16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = L16.FromLa32(Unsafe.Add(ref sourceBase, i));
ref L16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToLa16( public override void ToLa16(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<La16> destinationPixels) Span<La16> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref La16 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref La16 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = La16.FromLa32(Unsafe.Add(ref sourceBase, i));
ref La16 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb24( public override void ToRgb24(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<Rgb24> destinationPixels) Span<Rgb24> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb24 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb24 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb24.FromLa32(Unsafe.Add(ref sourceBase, i));
ref Rgb24 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba32( public override void ToRgba32(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<Rgba32> destinationPixels) Span<Rgba32> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba32 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba32 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba32.FromLa32(Unsafe.Add(ref sourceBase, i));
ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgb48( public override void ToRgb48(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<Rgb48> destinationPixels) Span<Rgb48> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgb48 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgb48 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgb48.FromLa32(Unsafe.Add(ref sourceBase, i));
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToRgba64( public override void ToRgba64(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<Rgba64> destinationPixels) Span<Rgba64> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Rgba64 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Rgba64 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Rgba64.FromLa32(Unsafe.Add(ref sourceBase, i));
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ToBgra5551( public override void ToBgra5551(
Configuration configuration, Configuration configuration,
ReadOnlySpan<La32> sourcePixels, ReadOnlySpan<La32> source,
Span<Bgra5551> destinationPixels) Span<Bgra5551> destination)
{ {
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels)); Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
ref La32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); ref La32 sourceBase = ref MemoryMarshal.GetReference(source);
ref Bgra5551 destRef = ref MemoryMarshal.GetReference(destinationPixels); ref Bgra5551 destinationBase = ref MemoryMarshal.GetReference(destination);
for (nuint i = 0; i < (uint)sourcePixels.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref La32 sp = ref Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref destinationBase, i) = Bgra5551.FromLa32(Unsafe.Add(ref sourceBase, i));
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);
dp.FromLa32(sp);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void From<TSourcePixel>( public override void From<TSourcePixel>(
Configuration configuration, Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels, ReadOnlySpan<TSourcePixel> source,
Span<La32> destinationPixels) Span<La32> destination)
{ {
PixelOperations<TSourcePixel>.Instance.ToLa32(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length)); PixelOperations<TSourcePixel>.Instance.ToLa32(configuration, source, destination.Slice(0, source.Length));
} }
} }
} }

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

Loading…
Cancel
Save