Browse Source

Merge branch 'main' into bp/Issue2411

pull/2412/head
Brian Popow 3 years ago
committed by GitHub
parent
commit
8441b23ded
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs
  2. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLab.cs
  3. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLch.cs
  4. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLchuv.cs
  5. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLuv.cs
  6. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieXyy.cs
  7. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieXyz.cs
  8. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Cmyk.cs
  9. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Hsl.cs
  10. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Hsv.cs
  11. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.HunterLab.cs
  12. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.LinearRgb.cs
  13. 28
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Lms.cs
  14. 26
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Rgb.cs
  15. 24
      src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.YCbCr.cs
  16. 10
      src/ImageSharp/ColorSpaces/Conversion/Implementation/Converters/CieXyzToCieLabConverter.cs
  17. 2
      src/ImageSharp/ColorSpaces/Conversion/Implementation/VonKriesChromaticAdaptation.cs
  18. 16
      src/ImageSharp/Common/Helpers/HexConverter.cs
  19. 36
      src/ImageSharp/Common/Helpers/Numerics.cs
  20. 26
      src/ImageSharp/Common/Helpers/Shuffle/IComponentShuffle.cs
  21. 8
      src/ImageSharp/Common/Helpers/Shuffle/IPad3Shuffle4.cs
  22. 6
      src/ImageSharp/Common/Helpers/Shuffle/IShuffle3.cs
  23. 12
      src/ImageSharp/Common/Helpers/Shuffle/IShuffle4Slice3.cs
  24. 8
      src/ImageSharp/Common/Helpers/SimdUtils.ExtendedIntrinsics.cs
  25. 8
      src/ImageSharp/Common/Helpers/SimdUtils.FallbackIntrinsics128.cs
  26. 96
      src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs
  27. 18
      src/ImageSharp/Common/Helpers/SimdUtils.Pack.cs
  28. 34
      src/ImageSharp/Common/Helpers/SimdUtils.Shuffle.cs
  29. 2
      src/ImageSharp/Compression/Zlib/Crc32.cs
  30. 5
      src/ImageSharp/Compression/Zlib/DeflaterEngine.cs
  31. 100
      src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs
  32. 4
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  33. 6
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  34. 20
      src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs
  35. 8
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  36. 2
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  37. 38
      src/ImageSharp/Formats/Gif/LzwDecoder.cs
  38. 22
      src/ImageSharp/Formats/Gif/LzwEncoder.cs
  39. 8
      src/ImageSharp/Formats/ImageExtensions.Save.tt
  40. 10
      src/ImageSharp/Formats/Jpeg/Components/Block8x8.cs
  41. 22
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.cs
  42. 4
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.tt
  43. 10
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Intrinsic.cs
  44. 36
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.ScaledCopy.cs
  45. 22
      src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs
  46. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykAvx.cs
  47. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykVector.cs
  48. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleAvx.cs
  49. 2
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleScalar.cs
  50. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleVector.cs
  51. 4
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.RgbArm.cs
  52. 4
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.RgbAvx.cs
  53. 4
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.RgbVector.cs
  54. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrAvx.cs
  55. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector.cs
  56. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKAvx.cs
  57. 8
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector.cs
  58. 2
      src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs
  59. 30
      src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs
  60. 4
      src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticStatistics.cs
  61. 24
      src/ImageSharp/Formats/Jpeg/Components/Decoder/ComponentProcessors/DownScalingComponentProcessor2.cs
  62. 24
      src/ImageSharp/Formats/Jpeg/Components/Decoder/ComponentProcessors/DownScalingComponentProcessor4.cs
  63. 14
      src/ImageSharp/Formats/Jpeg/Components/Decoder/ComponentProcessors/DownScalingComponentProcessor8.cs
  64. 12
      src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs
  65. 26
      src/ImageSharp/Formats/Jpeg/Components/Encoder/ComponentProcessor.cs
  66. 34
      src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs
  67. 4
      src/ImageSharp/Formats/Jpeg/Components/FloatingPointDCT.cs
  68. 26
      src/ImageSharp/Formats/Jpeg/Components/ScaledFloatingPointDCT.cs
  69. 26
      src/ImageSharp/Formats/Png/Adam7.cs
  70. 26
      src/ImageSharp/Formats/Png/Filters/AverageFilter.cs
  71. 28
      src/ImageSharp/Formats/Png/Filters/PaethFilter.cs
  72. 26
      src/ImageSharp/Formats/Png/Filters/SubFilter.cs
  73. 30
      src/ImageSharp/Formats/Png/Filters/UpFilter.cs
  74. 32
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  75. 18
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  76. 6
      src/ImageSharp/Formats/Png/PngEncoderHelpers.cs
  77. 129
      src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
  78. 7
      src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
  79. 2
      src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs
  80. 16
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs
  81. 12
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor{TPixel}.cs
  82. 2
      src/ImageSharp/Formats/Tiff/Writers/TiffBaseColorWriter{TPixel}.cs
  83. 27
      src/ImageSharp/Formats/Webp/AlphaDecoder.cs
  84. 2
      src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs
  85. 4
      src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs
  86. 24
      src/ImageSharp/Formats/Webp/Lossless/ColorSpaceTransformUtils.cs
  87. 8
      src/ImageSharp/Formats/Webp/Lossless/HistogramEncoder.cs
  88. 4
      src/ImageSharp/Formats/Webp/Lossless/HuffmanUtils.cs
  89. 98
      src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs
  90. 12
      src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs
  91. 10
      src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
  92. 12
      src/ImageSharp/Formats/Webp/Lossless/Vp8LHashChain.cs
  93. 33
      src/ImageSharp/Formats/Webp/Lossless/Vp8LHistogram.cs
  94. 118
      src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs
  95. 2
      src/ImageSharp/Formats/Webp/Lossy/PassStats.cs
  96. 6
      src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs
  97. 6
      src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs
  98. 12
      src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
  99. 2
      src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs
  100. 8
      src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs

4
src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs

@ -175,7 +175,7 @@ public static class SRgbCompanding
// Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float> // Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float>
ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors)); ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (IntPtr)((uint)vectors.Length / 2u)); ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length / 2u);
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast)) while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast))
{ {
@ -204,7 +204,7 @@ public static class SRgbCompanding
Vector4 zero = Vector4.Zero; Vector4 zero = Vector4.Zero;
var scale = new Vector4(Scale); var scale = new Vector4(Scale);
ref Vector4 vectorsBase = ref MemoryMarshal.GetReference(vectors); ref Vector4 vectorsBase = ref MemoryMarshal.GetReference(vectors);
ref Vector4 vectorsLast = ref Unsafe.Add(ref vectorsBase, vectors.Length); ref Vector4 vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length);
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast)) while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast))
{ {

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLab.cs

@ -37,7 +37,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -70,7 +70,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -103,7 +103,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -136,7 +136,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -169,7 +169,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -201,7 +201,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -266,7 +266,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -299,7 +299,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -332,7 +332,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -365,7 +365,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -398,7 +398,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);
@ -431,7 +431,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLab destRef = ref MemoryMarshal.GetReference(destination); ref CieLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i); ref CieLab dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLch.cs

@ -36,7 +36,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -69,7 +69,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -102,7 +102,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -135,7 +135,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -168,7 +168,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -200,7 +200,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -233,7 +233,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -266,7 +266,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -299,7 +299,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -332,7 +332,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -365,7 +365,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -398,7 +398,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);
@ -431,7 +431,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLch destRef = ref MemoryMarshal.GetReference(destination); ref CieLch destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i); ref CieLch dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLchuv.cs

@ -36,7 +36,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -69,7 +69,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -102,7 +102,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -135,7 +135,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -168,7 +168,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -201,7 +201,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -267,7 +267,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -300,7 +300,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -333,7 +333,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -366,7 +366,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -399,7 +399,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);
@ -431,7 +431,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLchuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLchuv dp = ref Unsafe.Add(ref destRef, i); ref CieLchuv dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLuv.cs

@ -35,7 +35,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -67,7 +67,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -102,7 +102,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -134,7 +134,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -169,7 +169,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -201,7 +201,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -233,7 +233,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -265,7 +265,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -297,7 +297,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -329,7 +329,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -361,7 +361,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -393,7 +393,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);
@ -425,7 +425,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref CieLuv destRef = ref MemoryMarshal.GetReference(destination); ref CieLuv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLuv dp = ref Unsafe.Add(ref destRef, i); ref CieLuv dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieXyy.cs

@ -36,7 +36,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -69,7 +69,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -102,7 +102,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -135,7 +135,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -163,7 +163,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -196,7 +196,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -229,7 +229,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -262,7 +262,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -295,7 +295,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -328,7 +328,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -361,7 +361,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -394,7 +394,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);
@ -427,7 +427,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyy destRef = ref MemoryMarshal.GetReference(destination); ref CieXyy destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyy dp = ref Unsafe.Add(ref destRef, i); ref CieXyy dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieXyz.cs

@ -41,7 +41,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -76,7 +76,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -111,7 +111,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -146,7 +146,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -177,7 +177,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -210,7 +210,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -243,7 +243,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -277,7 +277,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -310,7 +310,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -345,7 +345,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -374,7 +374,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -407,7 +407,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);
@ -440,7 +440,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Cmyk.cs

@ -36,7 +36,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -69,7 +69,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -102,7 +102,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -135,7 +135,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -168,7 +168,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -201,7 +201,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -267,7 +267,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -300,7 +300,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -333,7 +333,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -366,7 +366,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -394,7 +394,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);
@ -427,7 +427,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref Cmyk destRef = ref MemoryMarshal.GetReference(destination); ref Cmyk destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref Cmyk dp = ref Unsafe.Add(ref destRef, i); ref Cmyk dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Hsl.cs

@ -36,7 +36,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -69,7 +69,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -102,7 +102,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -135,7 +135,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -168,7 +168,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -201,7 +201,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -267,7 +267,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -300,7 +300,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -333,7 +333,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -366,7 +366,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -394,7 +394,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);
@ -427,7 +427,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsl destRef = ref MemoryMarshal.GetReference(destination); ref Hsl destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsl dp = ref Unsafe.Add(ref destRef, i); ref Hsl dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Hsv.cs

@ -36,7 +36,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -69,7 +69,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -102,7 +102,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -135,7 +135,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -168,7 +168,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -201,7 +201,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -267,7 +267,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -300,7 +300,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -333,7 +333,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -366,7 +366,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -394,7 +394,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);
@ -427,7 +427,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref Hsv destRef = ref MemoryMarshal.GetReference(destination); ref Hsv destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref Hsv dp = ref Unsafe.Add(ref destRef, i); ref Hsv dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.HunterLab.cs

@ -24,7 +24,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -45,7 +45,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -66,7 +66,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -87,7 +87,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -108,7 +108,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -129,7 +129,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -150,7 +150,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -171,7 +171,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -192,7 +192,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -213,7 +213,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -255,7 +255,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);
@ -276,7 +276,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref HunterLab destRef = ref MemoryMarshal.GetReference(destination); ref HunterLab destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref HunterLab dp = ref Unsafe.Add(ref destRef, i); ref HunterLab dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.LinearRgb.cs

@ -24,7 +24,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -45,7 +45,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -66,7 +66,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -87,7 +87,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -108,7 +108,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -129,7 +129,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -150,7 +150,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -171,7 +171,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -192,7 +192,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -213,7 +213,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -255,7 +255,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);
@ -276,7 +276,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination); ref LinearRgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref LinearRgb dp = ref Unsafe.Add(ref destRef, i); ref LinearRgb dp = ref Unsafe.Add(ref destRef, i);

28
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Lms.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 System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -24,7 +24,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -45,7 +45,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -66,7 +66,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -87,7 +87,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -108,7 +108,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -129,7 +129,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -150,7 +150,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -171,7 +171,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -192,7 +192,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -213,7 +213,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -255,7 +255,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);
@ -276,7 +276,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref Lms destRef = ref MemoryMarshal.GetReference(destination); ref Lms destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref Lms dp = ref Unsafe.Add(ref destRef, i); ref Lms dp = ref Unsafe.Add(ref destRef, i);

26
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.Rgb.cs

@ -24,7 +24,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -45,7 +45,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -66,7 +66,7 @@ public partial class ColorSpaceConverter
ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLchuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -87,7 +87,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -108,7 +108,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -129,7 +129,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -150,7 +150,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -171,7 +171,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -192,7 +192,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -213,7 +213,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -255,7 +255,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);
@ -276,7 +276,7 @@ public partial class ColorSpaceConverter
ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source); ref YCbCr sourceRef = ref MemoryMarshal.GetReference(source);
ref Rgb destRef = ref MemoryMarshal.GetReference(destination); ref Rgb destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i); ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb dp = ref Unsafe.Add(ref destRef, i); ref Rgb dp = ref Unsafe.Add(ref destRef, i);

24
src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.YCbCr.cs

@ -24,7 +24,7 @@ public partial class ColorSpaceConverter
ref CieLab sourceRef = ref MemoryMarshal.GetReference(source); ref CieLab sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i); ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -45,7 +45,7 @@ public partial class ColorSpaceConverter
ref CieLch sourceRef = ref MemoryMarshal.GetReference(source); ref CieLch sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i); ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -66,7 +66,7 @@ public partial class ColorSpaceConverter
ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source); ref CieLuv sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i); ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -87,7 +87,7 @@ public partial class ColorSpaceConverter
ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyy sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -108,7 +108,7 @@ public partial class ColorSpaceConverter
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -129,7 +129,7 @@ public partial class ColorSpaceConverter
ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source); ref Cmyk sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i); ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -150,7 +150,7 @@ public partial class ColorSpaceConverter
ref Hsl sourceRef = ref MemoryMarshal.GetReference(source); ref Hsl sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i); ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -171,7 +171,7 @@ public partial class ColorSpaceConverter
ref Hsv sourceRef = ref MemoryMarshal.GetReference(source); ref Hsv sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i); ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -192,7 +192,7 @@ public partial class ColorSpaceConverter
ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source); ref HunterLab sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i); ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -213,7 +213,7 @@ public partial class ColorSpaceConverter
ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source); ref LinearRgb sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i); ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -234,7 +234,7 @@ public partial class ColorSpaceConverter
ref Lms sourceRef = ref MemoryMarshal.GetReference(source); ref Lms sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Lms sp = ref Unsafe.Add(ref sourceRef, i); ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);
@ -255,7 +255,7 @@ public partial class ColorSpaceConverter
ref Rgb sourceRef = ref MemoryMarshal.GetReference(source); ref Rgb sourceRef = ref MemoryMarshal.GetReference(source);
ref YCbCr destRef = ref MemoryMarshal.GetReference(destination); ref YCbCr destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i); ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref YCbCr dp = ref Unsafe.Add(ref destRef, i); ref YCbCr dp = ref Unsafe.Add(ref destRef, i);

10
src/ImageSharp/ColorSpaces/Conversion/Implementation/Converters/CieXyzToCieLabConverter.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 System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -42,9 +42,11 @@ internal sealed class CieXyzToCieLabConverter
float xr = input.X / wx, yr = input.Y / wy, zr = input.Z / wz; float xr = input.X / wx, yr = input.Y / wy, zr = input.Z / wz;
float fx = xr > CieConstants.Epsilon ? MathF.Pow(xr, 0.3333333F) : ((CieConstants.Kappa * xr) + 16F) / 116F; const float inv116 = 1 / 116F;
float fy = yr > CieConstants.Epsilon ? MathF.Pow(yr, 0.3333333F) : ((CieConstants.Kappa * yr) + 16F) / 116F;
float fz = zr > CieConstants.Epsilon ? MathF.Pow(zr, 0.3333333F) : ((CieConstants.Kappa * zr) + 16F) / 116F; float fx = xr > CieConstants.Epsilon ? MathF.Pow(xr, 0.3333333F) : ((CieConstants.Kappa * xr) + 16F) * inv116;
float fy = yr > CieConstants.Epsilon ? MathF.Pow(yr, 0.3333333F) : ((CieConstants.Kappa * yr) + 16F) * inv116;
float fz = zr > CieConstants.Epsilon ? MathF.Pow(zr, 0.3333333F) : ((CieConstants.Kappa * zr) + 16F) * inv116;
float l = (116F * fy) - 16F; float l = (116F * fy) - 16F;
float a = 500F * (fx - fy); float a = 500F * (fx - fy);

2
src/ImageSharp/ColorSpaces/Conversion/Implementation/VonKriesChromaticAdaptation.cs

@ -81,7 +81,7 @@ public sealed class VonKriesChromaticAdaptation : IChromaticAdaptation
ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source); ref CieXyz sourceRef = ref MemoryMarshal.GetReference(source);
ref CieXyz destRef = ref MemoryMarshal.GetReference(destination); ref CieXyz destRef = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < count; i++) for (nuint i = 0; i < (uint)count; i++)
{ {
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i); ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref CieXyz dp = ref Unsafe.Add(ref destRef, i); ref CieXyz dp = ref Unsafe.Add(ref destRef, i);

16
src/ImageSharp/Common/Helpers/HexConverter.cs

@ -16,21 +16,19 @@ internal static class HexConverter
/// <returns>The number of bytes written to <paramref name="bytes"/>.</returns> /// <returns>The number of bytes written to <paramref name="bytes"/>.</returns>
public static int HexStringToBytes(ReadOnlySpan<char> chars, Span<byte> bytes) public static int HexStringToBytes(ReadOnlySpan<char> chars, Span<byte> bytes)
{ {
if ((chars.Length % 2) != 0) if (Numerics.Modulo2(chars.Length) != 0)
{ {
throw new ArgumentException("Input string length must be a multiple of 2", nameof(chars)); throw new ArgumentException("Input string length must be a multiple of 2", nameof(chars));
} }
if ((bytes.Length * 2) < chars.Length) if ((bytes.Length << 1 /* bit-hack for *2 */) < chars.Length)
{ {
throw new ArgumentException("Output span must be at least half the length of the input string"); throw new ArgumentException("Output span must be at least half the length of the input string");
} }
else
{ // Slightly better performance in the loop below, allows us to skip a bounds check
// Slightly better performance in the loop below, allows us to skip a bounds check // while still supporting output buffers that are larger than necessary
// while still supporting output buffers that are larger than necessary bytes = bytes[..(chars.Length >> 1)]; // bit-hack for / 2
bytes = bytes[..(chars.Length / 2)];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
static int FromChar(int c) static int FromChar(int c)
@ -57,7 +55,7 @@ internal static class HexConverter
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 255 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 255
}; };
return c >= charToHexLookup.Length ? 0xFF : charToHexLookup[c]; return (uint)c >= (uint)charToHexLookup.Length ? 0xFF : charToHexLookup[c];
} }
// See https://source.dot.net/#System.Private.CoreLib/HexConverter.cs,4681d45a0aa0b361 // See https://source.dot.net/#System.Private.CoreLib/HexConverter.cs,4681d45a0aa0b361

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

@ -55,6 +55,12 @@ internal static class Numerics
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Modulo4(int x) => x & 3; public static int Modulo4(int x) => x & 3;
/// <summary>
/// Calculates <paramref name="x"/> % 4
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint Modulo4(nint x) => x & 3;
/// <summary> /// <summary>
/// Calculates <paramref name="x"/> % 8 /// Calculates <paramref name="x"/> % 8
/// </summary> /// </summary>
@ -291,7 +297,7 @@ internal static class Numerics
if (remainder.Length > 0) if (remainder.Length > 0)
{ {
ref byte remainderStart = ref MemoryMarshal.GetReference(remainder); ref byte remainderStart = ref MemoryMarshal.GetReference(remainder);
ref byte remainderEnd = ref Unsafe.Add(ref remainderStart, remainder.Length); ref byte remainderEnd = ref Unsafe.Add(ref remainderStart, (uint)remainder.Length);
while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd)) while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd))
{ {
@ -316,7 +322,7 @@ internal static class Numerics
if (remainder.Length > 0) if (remainder.Length > 0)
{ {
ref uint remainderStart = ref MemoryMarshal.GetReference(remainder); ref uint remainderStart = ref MemoryMarshal.GetReference(remainder);
ref uint remainderEnd = ref Unsafe.Add(ref remainderStart, remainder.Length); ref uint remainderEnd = ref Unsafe.Add(ref remainderStart, (uint)remainder.Length);
while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd)) while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd))
{ {
@ -341,7 +347,7 @@ internal static class Numerics
if (remainder.Length > 0) if (remainder.Length > 0)
{ {
ref int remainderStart = ref MemoryMarshal.GetReference(remainder); ref int remainderStart = ref MemoryMarshal.GetReference(remainder);
ref int remainderEnd = ref Unsafe.Add(ref remainderStart, remainder.Length); ref int remainderEnd = ref Unsafe.Add(ref remainderStart, (uint)remainder.Length);
while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd)) while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd))
{ {
@ -366,7 +372,7 @@ internal static class Numerics
if (remainder.Length > 0) if (remainder.Length > 0)
{ {
ref float remainderStart = ref MemoryMarshal.GetReference(remainder); ref float remainderStart = ref MemoryMarshal.GetReference(remainder);
ref float remainderEnd = ref Unsafe.Add(ref remainderStart, remainder.Length); ref float remainderEnd = ref Unsafe.Add(ref remainderStart, (uint)remainder.Length);
while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd)) while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd))
{ {
@ -391,7 +397,7 @@ internal static class Numerics
if (remainder.Length > 0) if (remainder.Length > 0)
{ {
ref double remainderStart = ref MemoryMarshal.GetReference(remainder); ref double remainderStart = ref MemoryMarshal.GetReference(remainder);
ref double remainderEnd = ref Unsafe.Add(ref remainderStart, remainder.Length); ref double remainderEnd = ref Unsafe.Add(ref remainderStart, (uint)remainder.Length);
while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd)) while (Unsafe.IsAddressLessThan(ref remainderStart, ref remainderEnd))
{ {
@ -430,9 +436,9 @@ internal static class Numerics
var vmin = new Vector<T>(min); var vmin = new Vector<T>(min);
var vmax = new Vector<T>(max); var vmax = new Vector<T>(max);
int n = span.Length / Vector<T>.Count; nint n = (nint)(uint)span.Length / Vector<T>.Count;
int m = Modulo4(n); nint m = Modulo4(n);
int u = n - m; nint u = n - m;
ref Vector<T> vs0 = ref Unsafe.As<T, Vector<T>>(ref MemoryMarshal.GetReference(span)); ref Vector<T> vs0 = ref Unsafe.As<T, Vector<T>>(ref MemoryMarshal.GetReference(span));
ref Vector<T> vs1 = ref Unsafe.Add(ref vs0, 1); ref Vector<T> vs1 = ref Unsafe.Add(ref vs0, 1);
@ -491,7 +497,7 @@ internal static class Numerics
{ {
// Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float> // Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float>
ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors)); ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (IntPtr)((uint)vectors.Length / 2u)); ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length / 2u);
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast)) while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast))
{ {
@ -510,7 +516,7 @@ internal static class Numerics
else else
{ {
ref Vector4 vectorsStart = ref MemoryMarshal.GetReference(vectors); ref Vector4 vectorsStart = ref MemoryMarshal.GetReference(vectors);
ref Vector4 vectorsEnd = ref Unsafe.Add(ref vectorsStart, vectors.Length); ref Vector4 vectorsEnd = ref Unsafe.Add(ref vectorsStart, (uint)vectors.Length);
while (Unsafe.IsAddressLessThan(ref vectorsStart, ref vectorsEnd)) while (Unsafe.IsAddressLessThan(ref vectorsStart, ref vectorsEnd))
{ {
@ -556,7 +562,7 @@ internal static class Numerics
{ {
// Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float> // Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float>
ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors)); ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (IntPtr)((uint)vectors.Length / 2u)); ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length / 2u);
Vector256<float> epsilon = Vector256.Create(Constants.Epsilon); Vector256<float> epsilon = Vector256.Create(Constants.Epsilon);
while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast)) while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast))
@ -576,7 +582,7 @@ internal static class Numerics
else else
{ {
ref Vector4 vectorsStart = ref MemoryMarshal.GetReference(vectors); ref Vector4 vectorsStart = ref MemoryMarshal.GetReference(vectors);
ref Vector4 vectorsEnd = ref Unsafe.Add(ref vectorsStart, vectors.Length); ref Vector4 vectorsEnd = ref Unsafe.Add(ref vectorsStart, (uint)vectors.Length);
while (Unsafe.IsAddressLessThan(ref vectorsStart, ref vectorsEnd)) while (Unsafe.IsAddressLessThan(ref vectorsStart, ref vectorsEnd))
{ {
@ -650,7 +656,7 @@ internal static class Numerics
public static unsafe void CubePowOnXYZ(Span<Vector4> vectors) public static unsafe void CubePowOnXYZ(Span<Vector4> vectors)
{ {
ref Vector4 baseRef = ref MemoryMarshal.GetReference(vectors); ref Vector4 baseRef = ref MemoryMarshal.GetReference(vectors);
ref Vector4 endRef = ref Unsafe.Add(ref baseRef, vectors.Length); ref Vector4 endRef = ref Unsafe.Add(ref baseRef, (uint)vectors.Length);
while (Unsafe.IsAddressLessThan(ref baseRef, ref endRef)) while (Unsafe.IsAddressLessThan(ref baseRef, ref endRef))
{ {
@ -681,7 +687,7 @@ internal static class Numerics
if (Sse41.IsSupported) if (Sse41.IsSupported)
{ {
ref Vector128<float> vectors128Ref = ref Unsafe.As<Vector4, Vector128<float>>(ref MemoryMarshal.GetReference(vectors)); ref Vector128<float> vectors128Ref = ref Unsafe.As<Vector4, Vector128<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector128<float> vectors128End = ref Unsafe.Add(ref vectors128Ref, vectors.Length); ref Vector128<float> vectors128End = ref Unsafe.Add(ref vectors128Ref, (uint)vectors.Length);
var v128_341 = Vector128.Create(341); var v128_341 = Vector128.Create(341);
Vector128<int> v128_negativeZero = Vector128.Create(-0.0f).AsInt32(); Vector128<int> v128_negativeZero = Vector128.Create(-0.0f).AsInt32();
@ -730,7 +736,7 @@ internal static class Numerics
else else
{ {
ref Vector4 vectorsRef = ref MemoryMarshal.GetReference(vectors); ref Vector4 vectorsRef = ref MemoryMarshal.GetReference(vectors);
ref Vector4 vectorsEnd = ref Unsafe.Add(ref vectorsRef, vectors.Length); ref Vector4 vectorsEnd = ref Unsafe.Add(ref vectorsRef, (uint)vectors.Length);
// Fallback with scalar preprocessing and vectorized approximation steps // Fallback with scalar preprocessing and vectorized approximation steps
while (Unsafe.IsAddressLessThan(ref vectorsRef, ref vectorsEnd)) while (Unsafe.IsAddressLessThan(ref vectorsRef, ref vectorsEnd))

26
src/ImageSharp/Common/Helpers/Shuffle/IComponentShuffle.cs

@ -61,11 +61,11 @@ internal readonly struct DefaultShuffle4 : IShuffle4
ref byte sBase = ref MemoryMarshal.GetReference(source); ref byte sBase = ref MemoryMarshal.GetReference(source);
ref byte dBase = ref MemoryMarshal.GetReference(dest); ref byte dBase = ref MemoryMarshal.GetReference(dest);
Shuffle.InverseMMShuffle(this.Control, out int p3, out int p2, out int p1, out int p0); Shuffle.InverseMMShuffle(this.Control, out uint p3, out uint p2, out uint p1, out uint p0);
for (int i = 0; i < source.Length; i += 4) for (nuint i = 0; i < (uint)source.Length; i += 4)
{ {
Unsafe.Add(ref dBase, i) = Unsafe.Add(ref sBase, p0 + i); Unsafe.Add(ref dBase, i + 0) = Unsafe.Add(ref sBase, p0 + i);
Unsafe.Add(ref dBase, i + 1) = Unsafe.Add(ref sBase, p1 + i); Unsafe.Add(ref dBase, i + 1) = Unsafe.Add(ref sBase, p1 + i);
Unsafe.Add(ref dBase, i + 2) = Unsafe.Add(ref sBase, p2 + i); Unsafe.Add(ref dBase, i + 2) = Unsafe.Add(ref sBase, p2 + i);
Unsafe.Add(ref dBase, i + 3) = Unsafe.Add(ref sBase, p3 + i); Unsafe.Add(ref dBase, i + 3) = Unsafe.Add(ref sBase, p3 + i);
@ -84,9 +84,9 @@ internal readonly struct WXYZShuffle4 : IShuffle4
{ {
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source)); ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest)); ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
int n = source.Length / 4; uint n = (uint)source.Length / 4;
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
uint packed = Unsafe.Add(ref sBase, i); uint packed = Unsafe.Add(ref sBase, i);
@ -108,9 +108,9 @@ internal readonly struct WZYXShuffle4 : IShuffle4
{ {
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source)); ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest)); ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
int n = source.Length / 4; uint n = (uint)source.Length / 4;
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
uint packed = Unsafe.Add(ref sBase, i); uint packed = Unsafe.Add(ref sBase, i);
@ -132,9 +132,9 @@ internal readonly struct YZWXShuffle4 : IShuffle4
{ {
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source)); ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest)); ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
int n = source.Length / 4; uint n = (uint)source.Length / 4;
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
uint packed = Unsafe.Add(ref sBase, i); uint packed = Unsafe.Add(ref sBase, i);
@ -156,9 +156,9 @@ internal readonly struct ZYXWShuffle4 : IShuffle4
{ {
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source)); ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest)); ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
int n = source.Length / 4; uint n = (uint)source.Length / 4;
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
uint packed = Unsafe.Add(ref sBase, i); uint packed = Unsafe.Add(ref sBase, i);
@ -187,9 +187,9 @@ internal readonly struct XWZYShuffle4 : IShuffle4
{ {
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source)); ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest)); ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
int n = source.Length / 4; uint n = (uint)source.Length / 4;
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
uint packed = Unsafe.Add(ref sBase, i); uint packed = Unsafe.Add(ref sBase, i);

8
src/ImageSharp/Common/Helpers/Shuffle/IPad3Shuffle4.cs

@ -29,18 +29,18 @@ internal readonly struct DefaultPad3Shuffle4 : IPad3Shuffle4
ref byte sBase = ref MemoryMarshal.GetReference(source); ref byte sBase = ref MemoryMarshal.GetReference(source);
ref byte dBase = ref MemoryMarshal.GetReference(dest); ref byte dBase = ref MemoryMarshal.GetReference(dest);
Shuffle.InverseMMShuffle(this.Control, out int p3, out int p2, out int p1, out int p0); Shuffle.InverseMMShuffle(this.Control, out uint p3, out uint p2, out uint p1, out uint p0);
Span<byte> temp = stackalloc byte[4]; Span<byte> temp = stackalloc byte[4];
ref byte t = ref MemoryMarshal.GetReference(temp); ref byte t = ref MemoryMarshal.GetReference(temp);
ref uint tu = ref Unsafe.As<byte, uint>(ref t); ref uint tu = ref Unsafe.As<byte, uint>(ref t);
for (int i = 0, j = 0; i < source.Length; i += 3, j += 4) for (nuint i = 0, j = 0; i < (uint)source.Length; i += 3, j += 4)
{ {
ref byte s = ref Unsafe.Add(ref sBase, i); ref byte s = ref Unsafe.Add(ref sBase, i);
tu = Unsafe.As<byte, uint>(ref s) | 0xFF000000; tu = Unsafe.As<byte, uint>(ref s) | 0xFF000000;
Unsafe.Add(ref dBase, j) = Unsafe.Add(ref t, p0); Unsafe.Add(ref dBase, j + 0) = Unsafe.Add(ref t, p0);
Unsafe.Add(ref dBase, j + 1) = Unsafe.Add(ref t, p1); Unsafe.Add(ref dBase, j + 1) = Unsafe.Add(ref t, p1);
Unsafe.Add(ref dBase, j + 2) = Unsafe.Add(ref t, p2); Unsafe.Add(ref dBase, j + 2) = Unsafe.Add(ref t, p2);
Unsafe.Add(ref dBase, j + 3) = Unsafe.Add(ref t, p3); Unsafe.Add(ref dBase, j + 3) = Unsafe.Add(ref t, p3);
@ -60,7 +60,7 @@ internal readonly struct XYZWPad3Shuffle4 : IPad3Shuffle4
ref byte sBase = ref MemoryMarshal.GetReference(source); ref byte sBase = ref MemoryMarshal.GetReference(source);
ref byte dBase = ref MemoryMarshal.GetReference(dest); ref byte dBase = ref MemoryMarshal.GetReference(dest);
ref byte sEnd = ref Unsafe.Add(ref sBase, source.Length); ref byte sEnd = ref Unsafe.Add(ref sBase, (uint)source.Length);
ref byte sLoopEnd = ref Unsafe.Subtract(ref sEnd, 4); ref byte sLoopEnd = ref Unsafe.Subtract(ref sEnd, 4);
while (Unsafe.IsAddressLessThan(ref sBase, ref sLoopEnd)) while (Unsafe.IsAddressLessThan(ref sBase, ref sLoopEnd))

6
src/ImageSharp/Common/Helpers/Shuffle/IShuffle3.cs

@ -29,11 +29,11 @@ internal readonly struct DefaultShuffle3 : IShuffle3
ref byte sBase = ref MemoryMarshal.GetReference(source); ref byte sBase = ref MemoryMarshal.GetReference(source);
ref byte dBase = ref MemoryMarshal.GetReference(dest); ref byte dBase = ref MemoryMarshal.GetReference(dest);
Shuffle.InverseMMShuffle(this.Control, out _, out int p2, out int p1, out int p0); Shuffle.InverseMMShuffle(this.Control, out _, out uint p2, out uint p1, out uint p0);
for (int i = 0; i < source.Length; i += 3) for (nuint i = 0; i < (uint)source.Length; i += 3)
{ {
Unsafe.Add(ref dBase, i) = Unsafe.Add(ref sBase, p0 + i); Unsafe.Add(ref dBase, i + 0) = Unsafe.Add(ref sBase, p0 + i);
Unsafe.Add(ref dBase, i + 1) = Unsafe.Add(ref sBase, p1 + i); Unsafe.Add(ref dBase, i + 1) = Unsafe.Add(ref sBase, p1 + i);
Unsafe.Add(ref dBase, i + 2) = Unsafe.Add(ref sBase, p2 + i); Unsafe.Add(ref dBase, i + 2) = Unsafe.Add(ref sBase, p2 + i);
} }

12
src/ImageSharp/Common/Helpers/Shuffle/IShuffle4Slice3.cs

@ -29,11 +29,11 @@ internal readonly struct DefaultShuffle4Slice3 : IShuffle4Slice3
ref byte sBase = ref MemoryMarshal.GetReference(source); ref byte sBase = ref MemoryMarshal.GetReference(source);
ref byte dBase = ref MemoryMarshal.GetReference(dest); ref byte dBase = ref MemoryMarshal.GetReference(dest);
Shuffle.InverseMMShuffle(this.Control, out _, out int p2, out int p1, out int p0); Shuffle.InverseMMShuffle(this.Control, out _, out uint p2, out uint p1, out uint p0);
for (int i = 0, j = 0; i < dest.Length; i += 3, j += 4) for (nuint i = 0, j = 0; i < (uint)dest.Length; i += 3, j += 4)
{ {
Unsafe.Add(ref dBase, i) = Unsafe.Add(ref sBase, p0 + j); Unsafe.Add(ref dBase, i + 0) = Unsafe.Add(ref sBase, p0 + j);
Unsafe.Add(ref dBase, i + 1) = Unsafe.Add(ref sBase, p1 + j); Unsafe.Add(ref dBase, i + 1) = Unsafe.Add(ref sBase, p1 + j);
Unsafe.Add(ref dBase, i + 2) = Unsafe.Add(ref sBase, p2 + j); Unsafe.Add(ref dBase, i + 2) = Unsafe.Add(ref sBase, p2 + j);
} }
@ -52,9 +52,9 @@ internal readonly struct XYZWShuffle4Slice3 : IShuffle4Slice3
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source)); ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref Byte3 dBase = ref Unsafe.As<byte, Byte3>(ref MemoryMarshal.GetReference(dest)); ref Byte3 dBase = ref Unsafe.As<byte, Byte3>(ref MemoryMarshal.GetReference(dest));
int n = source.Length / 4; nint n = (nint)(uint)source.Length / 4;
int m = Numerics.Modulo4(n); nint m = Numerics.Modulo4(n);
int u = n - m; nint u = n - m;
ref uint sLoopEnd = ref Unsafe.Add(ref sBase, u); ref uint sLoopEnd = ref Unsafe.Add(ref sBase, u);
ref uint sEnd = ref Unsafe.Add(ref sBase, n); ref uint sEnd = ref Unsafe.Add(ref sBase, n);

8
src/ImageSharp/Common/Helpers/SimdUtils.ExtendedIntrinsics.cs

@ -97,12 +97,12 @@ internal static partial class SimdUtils
{ {
VerifySpanInput(source, dest, Vector<byte>.Count); VerifySpanInput(source, dest, Vector<byte>.Count);
int n = dest.Length / Vector<byte>.Count; nuint n = (uint)dest.Length / (uint)Vector<byte>.Count;
ref Vector<byte> sourceBase = ref Unsafe.As<byte, Vector<byte>>(ref MemoryMarshal.GetReference(source)); ref Vector<byte> sourceBase = ref Unsafe.As<byte, Vector<byte>>(ref MemoryMarshal.GetReference(source));
ref Vector<float> destBase = ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(dest)); ref Vector<float> destBase = ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(dest));
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
Vector<byte> b = Unsafe.Add(ref sourceBase, i); Vector<byte> b = Unsafe.Add(ref sourceBase, i);
@ -132,13 +132,13 @@ internal static partial class SimdUtils
{ {
VerifySpanInput(source, dest, Vector<byte>.Count); VerifySpanInput(source, dest, Vector<byte>.Count);
int n = dest.Length / Vector<byte>.Count; nuint n = (uint)dest.Length / (uint)Vector<byte>.Count;
ref Vector<float> sourceBase = ref Vector<float> sourceBase =
ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(source)); ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(source));
ref Vector<byte> destBase = ref Unsafe.As<byte, Vector<byte>>(ref MemoryMarshal.GetReference(dest)); ref Vector<byte> destBase = ref Unsafe.As<byte, Vector<byte>>(ref MemoryMarshal.GetReference(dest));
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector<float> s = ref Unsafe.Add(ref sourceBase, i * 4); ref Vector<float> s = ref Unsafe.Add(ref sourceBase, i * 4);

8
src/ImageSharp/Common/Helpers/SimdUtils.FallbackIntrinsics128.cs

@ -71,7 +71,7 @@ internal static partial class SimdUtils
{ {
VerifySpanInput(source, dest, 4); VerifySpanInput(source, dest, 4);
int count = dest.Length / 4; uint count = (uint)dest.Length / 4;
if (count == 0) if (count == 0)
{ {
return; return;
@ -83,7 +83,7 @@ internal static partial class SimdUtils
const float scale = 1f / 255f; const float scale = 1f / 255f;
Vector4 d = default; Vector4 d = default;
for (int i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
ref ByteVector4 s = ref Unsafe.Add(ref sBase, i); ref ByteVector4 s = ref Unsafe.Add(ref sBase, i);
d.X = s.X; d.X = s.X;
@ -105,7 +105,7 @@ internal static partial class SimdUtils
{ {
VerifySpanInput(source, dest, 4); VerifySpanInput(source, dest, 4);
int count = source.Length / 4; uint count = (uint)source.Length / 4;
if (count == 0) if (count == 0)
{ {
return; return;
@ -117,7 +117,7 @@ internal static partial class SimdUtils
var half = new Vector4(0.5f); var half = new Vector4(0.5f);
var maxBytes = new Vector4(255f); var maxBytes = new Vector4(255f);
for (int i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
Vector4 s = Unsafe.Add(ref sBase, i); Vector4 s = Unsafe.Add(ref sBase, i);
s *= maxBytes; s *= maxBytes;

96
src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs

@ -159,7 +159,7 @@ internal static partial class SimdUtils
int remainder = source.Length % (Vector128<byte>.Count * 3); int remainder = source.Length % (Vector128<byte>.Count * 3);
int sourceCount = source.Length - remainder; int sourceCount = source.Length - remainder;
int destCount = sourceCount * 4 / 3; int destCount = (int)((uint)sourceCount * 4 / 3);
if (sourceCount > 0) if (sourceCount > 0)
{ {
@ -192,7 +192,7 @@ internal static partial class SimdUtils
int remainder = source.Length % (Vector128<byte>.Count * 4); int remainder = source.Length % (Vector128<byte>.Count * 4);
int sourceCount = source.Length - remainder; int sourceCount = source.Length - remainder;
int destCount = sourceCount * 3 / 4; int destCount = (int)((uint)sourceCount * 3 / 4);
if (sourceCount > 0) if (sourceCount > 0)
{ {
@ -221,11 +221,11 @@ internal static partial class SimdUtils
ref Vector256<float> destBase = ref Vector256<float> destBase =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(dest)); ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(dest));
int n = dest.Length / Vector256<float>.Count; nint n = (nint)(uint)(dest.Length / Vector256<float>.Count);
int m = Numerics.Modulo4(n); nint m = Numerics.Modulo4(n);
int u = n - m; nint u = n - m;
for (int i = 0; i < u; i += 4) for (nint i = 0; i < u; i += 4)
{ {
ref Vector256<float> vd0 = ref Unsafe.Add(ref destBase, i); ref Vector256<float> vd0 = ref Unsafe.Add(ref destBase, i);
ref Vector256<float> vs0 = ref Unsafe.Add(ref sourceBase, i); ref Vector256<float> vs0 = ref Unsafe.Add(ref sourceBase, i);
@ -238,7 +238,7 @@ internal static partial class SimdUtils
if (m > 0) if (m > 0)
{ {
for (int i = u; i < n; i++) for (nint i = u; i < n; i++)
{ {
Unsafe.Add(ref destBase, i) = Avx.Permute(Unsafe.Add(ref sourceBase, i), control); Unsafe.Add(ref destBase, i) = Avx.Permute(Unsafe.Add(ref sourceBase, i), control);
} }
@ -253,11 +253,11 @@ internal static partial class SimdUtils
ref Vector128<float> destBase = ref Vector128<float> destBase =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(dest)); ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(dest));
int n = dest.Length / Vector128<float>.Count; nint n = (nint)(uint)dest.Length / Vector128<float>.Count;
int m = Numerics.Modulo4(n); nint m = Numerics.Modulo4(n);
int u = n - m; nint u = n - m;
for (int i = 0; i < u; i += 4) for (nint i = 0; i < u; i += 4)
{ {
ref Vector128<float> vd0 = ref Unsafe.Add(ref destBase, i); ref Vector128<float> vd0 = ref Unsafe.Add(ref destBase, i);
ref Vector128<float> vs0 = ref Unsafe.Add(ref sourceBase, i); ref Vector128<float> vs0 = ref Unsafe.Add(ref sourceBase, i);
@ -276,7 +276,7 @@ internal static partial class SimdUtils
if (m > 0) if (m > 0)
{ {
for (int i = u; i < n; i++) for (nint i = u; i < n; i++)
{ {
Vector128<float> vs = Unsafe.Add(ref sourceBase, i); Vector128<float> vs = Unsafe.Add(ref sourceBase, i);
Unsafe.Add(ref destBase, i) = Sse.Shuffle(vs, vs, control); Unsafe.Add(ref destBase, i) = Sse.Shuffle(vs, vs, control);
@ -306,11 +306,11 @@ internal static partial class SimdUtils
ref Vector256<byte> destBase = ref Vector256<byte> destBase =
ref Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(dest)); ref Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(dest));
int n = dest.Length / Vector256<byte>.Count; nint n = (nint)(uint)dest.Length / Vector256<byte>.Count;
int m = Numerics.Modulo4(n); nint m = Numerics.Modulo4(n);
int u = n - m; nint u = n - m;
for (int i = 0; i < u; i += 4) for (nint i = 0; i < u; i += 4)
{ {
ref Vector256<byte> vs0 = ref Unsafe.Add(ref sourceBase, i); ref Vector256<byte> vs0 = ref Unsafe.Add(ref sourceBase, i);
ref Vector256<byte> vd0 = ref Unsafe.Add(ref destBase, i); ref Vector256<byte> vd0 = ref Unsafe.Add(ref destBase, i);
@ -323,7 +323,7 @@ internal static partial class SimdUtils
if (m > 0) if (m > 0)
{ {
for (int i = u; i < n; i++) for (nint i = u; i < n; i++)
{ {
Unsafe.Add(ref destBase, i) = Avx2.Shuffle(Unsafe.Add(ref sourceBase, i), vshuffle); Unsafe.Add(ref destBase, i) = Avx2.Shuffle(Unsafe.Add(ref sourceBase, i), vshuffle);
} }
@ -342,11 +342,11 @@ internal static partial class SimdUtils
ref Vector128<byte> destBase = ref Vector128<byte> destBase =
ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest)); ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest));
int n = dest.Length / Vector128<byte>.Count; nint n = (nint)(uint)dest.Length / Vector128<byte>.Count;
int m = Numerics.Modulo4(n); nint m = Numerics.Modulo4(n);
int u = n - m; nint u = n - m;
for (int i = 0; i < u; i += 4) for (nint i = 0; i < u; i += 4)
{ {
ref Vector128<byte> vs0 = ref Unsafe.Add(ref sourceBase, i); ref Vector128<byte> vs0 = ref Unsafe.Add(ref sourceBase, i);
ref Vector128<byte> vd0 = ref Unsafe.Add(ref destBase, i); ref Vector128<byte> vd0 = ref Unsafe.Add(ref destBase, i);
@ -359,7 +359,7 @@ internal static partial class SimdUtils
if (m > 0) if (m > 0)
{ {
for (int i = u; i < n; i++) for (nint i = u; i < n; i++)
{ {
Unsafe.Add(ref destBase, i) = Ssse3.Shuffle(Unsafe.Add(ref sourceBase, i), vshuffle); Unsafe.Add(ref destBase, i) = Ssse3.Shuffle(Unsafe.Add(ref sourceBase, i), vshuffle);
} }
@ -391,9 +391,9 @@ internal static partial class SimdUtils
ref Vector128<byte> destBase = ref Vector128<byte> destBase =
ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest)); ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest));
int n = source.Length / Vector128<byte>.Count; nuint n = (uint)source.Length / (uint)Vector128<byte>.Count;
for (int i = 0; i < n; i += 3) for (nuint i = 0; i < n; i += 3)
{ {
ref Vector128<byte> vs = ref Unsafe.Add(ref sourceBase, i); ref Vector128<byte> vs = ref Unsafe.Add(ref sourceBase, i);
@ -454,9 +454,9 @@ internal static partial class SimdUtils
ref Vector128<byte> destBase = ref Vector128<byte> destBase =
ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest)); ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest));
int n = source.Length / Vector128<byte>.Count; nuint n = (uint)source.Length / (uint)Vector128<byte>.Count;
for (int i = 0, j = 0; i < n; i += 3, j += 4) for (nuint i = 0, j = 0; i < n; i += 3, j += 4)
{ {
ref Vector128<byte> v0 = ref Unsafe.Add(ref sourceBase, i); ref Vector128<byte> v0 = ref Unsafe.Add(ref sourceBase, i);
Vector128<byte> v1 = Unsafe.Add(ref v0, 1); Vector128<byte> v1 = Unsafe.Add(ref v0, 1);
@ -498,9 +498,9 @@ internal static partial class SimdUtils
ref Vector128<byte> destBase = ref Vector128<byte> destBase =
ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest)); ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest));
int n = source.Length / Vector128<byte>.Count; nuint n = (uint)source.Length / (uint)Vector128<byte>.Count;
for (int i = 0, j = 0; i < n; i += 4, j += 3) for (nuint i = 0, j = 0; i < n; i += 4, j += 3)
{ {
ref Vector128<byte> vs = ref Unsafe.Add(ref sourceBase, i); ref Vector128<byte> vs = ref Unsafe.Add(ref sourceBase, i);
@ -650,16 +650,16 @@ internal static partial class SimdUtils
{ {
VerifySpanInput(source, dest, Vector256<byte>.Count); VerifySpanInput(source, dest, Vector256<byte>.Count);
int n = dest.Length / Vector256<byte>.Count; nuint n = (uint)dest.Length / (uint)Vector256<byte>.Count;
ref Vector256<float> destBase = ref Vector256<float> destBase =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(dest)); ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(dest));
var scale = Vector256.Create(1 / (float)byte.MaxValue); var scale = Vector256.Create(1 / (float)byte.MaxValue);
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
int si = Vector256<byte>.Count * i; nuint si = (uint)Vector256<byte>.Count * i;
Vector256<int> i0 = Avx2.ConvertToVector256Int32(sourceBase + si); Vector256<int> i0 = Avx2.ConvertToVector256Int32(sourceBase + si);
Vector256<int> i1 = Avx2.ConvertToVector256Int32(sourceBase + si + Vector256<int>.Count); Vector256<int> i1 = Avx2.ConvertToVector256Int32(sourceBase + si + Vector256<int>.Count);
Vector256<int> i2 = Avx2.ConvertToVector256Int32(sourceBase + si + (Vector256<int>.Count * 2)); Vector256<int> i2 = Avx2.ConvertToVector256Int32(sourceBase + si + (Vector256<int>.Count * 2));
@ -683,7 +683,7 @@ internal static partial class SimdUtils
// Sse // Sse
VerifySpanInput(source, dest, Vector128<byte>.Count); VerifySpanInput(source, dest, Vector128<byte>.Count);
int n = dest.Length / Vector128<byte>.Count; nuint n = (uint)dest.Length / (uint)Vector128<byte>.Count;
ref Vector128<float> destBase = ref Vector128<float> destBase =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(dest)); ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(dest));
@ -691,9 +691,9 @@ internal static partial class SimdUtils
var scale = Vector128.Create(1 / (float)byte.MaxValue); var scale = Vector128.Create(1 / (float)byte.MaxValue);
Vector128<byte> zero = Vector128<byte>.Zero; Vector128<byte> zero = Vector128<byte>.Zero;
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
int si = Vector128<byte>.Count * i; nuint si = (uint)Vector128<byte>.Count * i;
Vector128<int> i0, i1, i2, i3; Vector128<int> i0, i1, i2, i3;
if (Sse41.IsSupported) if (Sse41.IsSupported)
@ -782,7 +782,7 @@ internal static partial class SimdUtils
{ {
VerifySpanInput(source, dest, Vector256<byte>.Count); VerifySpanInput(source, dest, Vector256<byte>.Count);
int n = dest.Length / Vector256<byte>.Count; nuint n = (uint)dest.Length / (uint)Vector256<byte>.Count;
ref Vector256<float> sourceBase = ref Vector256<float> sourceBase =
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(source)); ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(source));
@ -794,7 +794,7 @@ internal static partial class SimdUtils
ref byte maskBase = ref MemoryMarshal.GetReference(PermuteMaskDeinterleave8x32); ref byte maskBase = ref MemoryMarshal.GetReference(PermuteMaskDeinterleave8x32);
Vector256<int> mask = Unsafe.As<byte, Vector256<int>>(ref maskBase); Vector256<int> mask = Unsafe.As<byte, Vector256<int>>(ref maskBase);
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector256<float> s = ref Unsafe.Add(ref sourceBase, i * 4); ref Vector256<float> s = ref Unsafe.Add(ref sourceBase, i * 4);
@ -821,7 +821,7 @@ internal static partial class SimdUtils
// Sse // Sse
VerifySpanInput(source, dest, Vector128<byte>.Count); VerifySpanInput(source, dest, Vector128<byte>.Count);
int n = dest.Length / Vector128<byte>.Count; nuint n = (uint)dest.Length / (uint)Vector128<byte>.Count;
ref Vector128<float> sourceBase = ref Vector128<float> sourceBase =
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(source)); ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(source));
@ -831,7 +831,7 @@ internal static partial class SimdUtils
var scale = Vector128.Create((float)byte.MaxValue); var scale = Vector128.Create((float)byte.MaxValue);
for (int i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector128<float> s = ref Unsafe.Add(ref sourceBase, i * 4); ref Vector128<float> s = ref Unsafe.Add(ref sourceBase, i * 4);
@ -864,7 +864,7 @@ internal static partial class SimdUtils
ref Vector256<byte> bBase = ref Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(blueChannel)); ref Vector256<byte> bBase = ref Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(blueChannel));
ref byte dBase = ref Unsafe.As<Rgb24, byte>(ref MemoryMarshal.GetReference(destination)); ref byte dBase = ref Unsafe.As<Rgb24, byte>(ref MemoryMarshal.GetReference(destination));
int count = redChannel.Length / Vector256<byte>.Count; nuint count = (uint)redChannel.Length / (uint)Vector256<byte>.Count;
ref byte control1Bytes = ref MemoryMarshal.GetReference(PermuteMaskEvenOdd8x32); ref byte control1Bytes = ref MemoryMarshal.GetReference(PermuteMaskEvenOdd8x32);
Vector256<uint> control1 = Unsafe.As<byte, Vector256<uint>>(ref control1Bytes); Vector256<uint> control1 = Unsafe.As<byte, Vector256<uint>>(ref control1Bytes);
@ -875,7 +875,7 @@ internal static partial class SimdUtils
Vector256<byte> shuffleAlpha = Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(ShuffleMaskShiftAlpha)); Vector256<byte> shuffleAlpha = Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(ShuffleMaskShiftAlpha));
for (int i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
Vector256<byte> r0 = Unsafe.Add(ref rBase, i); Vector256<byte> r0 = Unsafe.Add(ref rBase, i);
Vector256<byte> g0 = Unsafe.Add(ref gBase, i); Vector256<byte> g0 = Unsafe.Add(ref gBase, i);
@ -918,7 +918,7 @@ internal static partial class SimdUtils
Unsafe.As<byte, Vector256<byte>>(ref d4) = rgb4; Unsafe.As<byte, Vector256<byte>>(ref d4) = rgb4;
} }
int slice = count * Vector256<byte>.Count; int slice = (int)count * Vector256<byte>.Count;
redChannel = redChannel[slice..]; redChannel = redChannel[slice..];
greenChannel = greenChannel[slice..]; greenChannel = greenChannel[slice..];
blueChannel = blueChannel[slice..]; blueChannel = blueChannel[slice..];
@ -936,12 +936,12 @@ internal static partial class SimdUtils
ref Vector256<byte> bBase = ref Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(blueChannel)); ref Vector256<byte> bBase = ref Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(blueChannel));
ref Vector256<byte> dBase = ref Unsafe.As<Rgba32, Vector256<byte>>(ref MemoryMarshal.GetReference(destination)); ref Vector256<byte> dBase = ref Unsafe.As<Rgba32, Vector256<byte>>(ref MemoryMarshal.GetReference(destination));
int count = redChannel.Length / Vector256<byte>.Count; nuint count = (uint)redChannel.Length / (uint)Vector256<byte>.Count;
ref byte control1Bytes = ref MemoryMarshal.GetReference(PermuteMaskEvenOdd8x32); ref byte control1Bytes = ref MemoryMarshal.GetReference(PermuteMaskEvenOdd8x32);
Vector256<uint> control1 = Unsafe.As<byte, Vector256<uint>>(ref control1Bytes); Vector256<uint> control1 = Unsafe.As<byte, Vector256<uint>>(ref control1Bytes);
var a = Vector256.Create((byte)255); var a = Vector256.Create((byte)255);
for (int i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
Vector256<byte> r0 = Unsafe.Add(ref rBase, i); Vector256<byte> r0 = Unsafe.Add(ref rBase, i);
Vector256<byte> g0 = Unsafe.Add(ref gBase, i); Vector256<byte> g0 = Unsafe.Add(ref gBase, i);
@ -970,7 +970,7 @@ internal static partial class SimdUtils
Unsafe.Add(ref d0, 3) = rgb4; Unsafe.Add(ref d0, 3) = rgb4;
} }
int slice = count * Vector256<byte>.Count; int slice = (int)count * Vector256<byte>.Count;
redChannel = redChannel[slice..]; redChannel = redChannel[slice..];
greenChannel = greenChannel[slice..]; greenChannel = greenChannel[slice..];
blueChannel = blueChannel[slice..]; blueChannel = blueChannel[slice..];
@ -994,10 +994,10 @@ internal static partial class SimdUtils
Vector256<float> r, g, b; Vector256<float> r, g, b;
const int bytesPerRgbStride = 24; const int bytesPerRgbStride = 24;
int count = (int)((uint)source.Length / 8); nuint count = (uint)source.Length / 8;
for (int i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
rgb = Avx2.PermuteVar8x32(Unsafe.AddByteOffset(ref rgbByteSpan, (IntPtr)(bytesPerRgbStride * i)).AsUInt32(), extractToLanesMask).AsByte(); rgb = Avx2.PermuteVar8x32(Unsafe.AddByteOffset(ref rgbByteSpan, (uint)(bytesPerRgbStride * i)).AsUInt32(), extractToLanesMask).AsByte();
rgb = Avx2.Shuffle(rgb, extractRgbMask); rgb = Avx2.Shuffle(rgb, extractRgbMask);
@ -1013,7 +1013,7 @@ internal static partial class SimdUtils
Unsafe.Add(ref destBRef, i) = b; Unsafe.Add(ref destBRef, i) = b;
} }
int sliceCount = count * 8; int sliceCount = (int)(count * 8);
redChannel = redChannel.Slice(sliceCount); redChannel = redChannel.Slice(sliceCount);
greenChannel = greenChannel.Slice(sliceCount); greenChannel = greenChannel.Slice(sliceCount);
blueChannel = blueChannel.Slice(sliceCount); blueChannel = blueChannel.Slice(sliceCount);

18
src/ImageSharp/Common/Helpers/SimdUtils.Pack.cs

@ -86,8 +86,8 @@ internal static partial class SimdUtils
ref ByteTuple4 b = ref Unsafe.As<byte, ByteTuple4>(ref MemoryMarshal.GetReference(blueChannel)); ref ByteTuple4 b = ref Unsafe.As<byte, ByteTuple4>(ref MemoryMarshal.GetReference(blueChannel));
ref Rgb24 rgb = ref MemoryMarshal.GetReference(destination); ref Rgb24 rgb = ref MemoryMarshal.GetReference(destination);
int count = redChannel.Length / 4; nuint count = (uint)redChannel.Length / 4;
for (int i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
ref Rgb24 d0 = ref Unsafe.Add(ref rgb, i * 4); ref Rgb24 d0 = ref Unsafe.Add(ref rgb, i * 4);
ref Rgb24 d1 = ref Unsafe.Add(ref d0, 1); ref Rgb24 d1 = ref Unsafe.Add(ref d0, 1);
@ -115,7 +115,7 @@ internal static partial class SimdUtils
d3.B = bb.V3; d3.B = bb.V3;
} }
int finished = count * 4; int finished = (int)(count * 4);
redChannel = redChannel[finished..]; redChannel = redChannel[finished..];
greenChannel = greenChannel[finished..]; greenChannel = greenChannel[finished..];
blueChannel = blueChannel[finished..]; blueChannel = blueChannel[finished..];
@ -133,9 +133,9 @@ internal static partial class SimdUtils
ref ByteTuple4 b = ref Unsafe.As<byte, ByteTuple4>(ref MemoryMarshal.GetReference(blueChannel)); ref ByteTuple4 b = ref Unsafe.As<byte, ByteTuple4>(ref MemoryMarshal.GetReference(blueChannel));
ref Rgba32 rgb = ref MemoryMarshal.GetReference(destination); ref Rgba32 rgb = ref MemoryMarshal.GetReference(destination);
int count = redChannel.Length / 4; nuint count = (uint)redChannel.Length / 4;
destination.Fill(new Rgba32(0, 0, 0, 255)); destination.Fill(new Rgba32(0, 0, 0, 255));
for (int i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
ref Rgba32 d0 = ref Unsafe.Add(ref rgb, i * 4); ref Rgba32 d0 = ref Unsafe.Add(ref rgb, i * 4);
ref Rgba32 d1 = ref Unsafe.Add(ref d0, 1); ref Rgba32 d1 = ref Unsafe.Add(ref d0, 1);
@ -163,7 +163,7 @@ internal static partial class SimdUtils
d3.B = bb.V3; d3.B = bb.V3;
} }
int finished = count * 4; int finished = (int)(count * 4);
redChannel = redChannel[finished..]; redChannel = redChannel[finished..];
greenChannel = greenChannel[finished..]; greenChannel = greenChannel[finished..];
blueChannel = blueChannel[finished..]; blueChannel = blueChannel[finished..];
@ -181,7 +181,7 @@ internal static partial class SimdUtils
ref byte b = ref MemoryMarshal.GetReference(blueChannel); ref byte b = ref MemoryMarshal.GetReference(blueChannel);
ref Rgb24 rgb = ref MemoryMarshal.GetReference(destination); ref Rgb24 rgb = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < destination.Length; i++) for (nuint i = 0; i < (uint)destination.Length; i++)
{ {
ref Rgb24 d = ref Unsafe.Add(ref rgb, i); ref Rgb24 d = ref Unsafe.Add(ref rgb, i);
d.R = Unsafe.Add(ref r, i); d.R = Unsafe.Add(ref r, i);
@ -201,7 +201,7 @@ internal static partial class SimdUtils
ref byte b = ref MemoryMarshal.GetReference(blueChannel); ref byte b = ref MemoryMarshal.GetReference(blueChannel);
ref Rgba32 rgba = ref MemoryMarshal.GetReference(destination); ref Rgba32 rgba = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < destination.Length; i++) for (nuint i = 0; i < (uint)destination.Length; i++)
{ {
ref Rgba32 d = ref Unsafe.Add(ref rgba, i); ref Rgba32 d = ref Unsafe.Add(ref rgba, i);
d.R = Unsafe.Add(ref r, i); d.R = Unsafe.Add(ref r, i);
@ -226,7 +226,7 @@ internal static partial class SimdUtils
ref float b = ref MemoryMarshal.GetReference(blueChannel); ref float b = ref MemoryMarshal.GetReference(blueChannel);
ref Rgb24 rgb = ref MemoryMarshal.GetReference(source); ref Rgb24 rgb = ref MemoryMarshal.GetReference(source);
for (int i = 0; i < source.Length; i++) for (nuint i = 0; i < (uint)source.Length; i++)
{ {
ref Rgb24 src = ref Unsafe.Add(ref rgb, i); ref Rgb24 src = ref Unsafe.Add(ref rgb, i);
Unsafe.Add(ref r, i) = src.R; Unsafe.Add(ref r, i) = src.R;

34
src/ImageSharp/Common/Helpers/SimdUtils.Shuffle.cs

@ -145,11 +145,11 @@ internal static partial class SimdUtils
{ {
ref float sBase = ref MemoryMarshal.GetReference(source); ref float sBase = ref MemoryMarshal.GetReference(source);
ref float dBase = ref MemoryMarshal.GetReference(dest); ref float dBase = ref MemoryMarshal.GetReference(dest);
Shuffle.InverseMMShuffle(control, out int p3, out int p2, out int p1, out int p0); Shuffle.InverseMMShuffle(control, out uint p3, out uint p2, out uint p1, out uint p0);
for (int i = 0; i < source.Length; i += 4) for (nuint i = 0; i < (uint)source.Length; i += 4)
{ {
Unsafe.Add(ref dBase, i) = Unsafe.Add(ref sBase, p0 + i); Unsafe.Add(ref dBase, i + 0) = Unsafe.Add(ref sBase, p0 + i);
Unsafe.Add(ref dBase, i + 1) = Unsafe.Add(ref sBase, p1 + i); Unsafe.Add(ref dBase, i + 1) = Unsafe.Add(ref sBase, p1 + i);
Unsafe.Add(ref dBase, i + 2) = Unsafe.Add(ref sBase, p2 + i); Unsafe.Add(ref dBase, i + 2) = Unsafe.Add(ref sBase, p2 + i);
Unsafe.Add(ref dBase, i + 3) = Unsafe.Add(ref sBase, p3 + i); Unsafe.Add(ref dBase, i + 3) = Unsafe.Add(ref sBase, p3 + i);
@ -492,16 +492,16 @@ internal static partial class SimdUtils
{ {
InverseMMShuffle( InverseMMShuffle(
control, control,
out int p3, out uint p3,
out int p2, out uint p2,
out int p1, out uint p1,
out int p0); out uint p0);
ref byte spanBase = ref MemoryMarshal.GetReference(span); ref byte spanBase = ref MemoryMarshal.GetReference(span);
for (int i = 0; i < span.Length; i += 4) for (nuint i = 0; i < (uint)span.Length; i += 4)
{ {
Unsafe.Add(ref spanBase, i) = (byte)(p0 + i); Unsafe.Add(ref spanBase, i + 0) = (byte)(p0 + i);
Unsafe.Add(ref spanBase, i + 1) = (byte)(p1 + i); Unsafe.Add(ref spanBase, i + 1) = (byte)(p1 + i);
Unsafe.Add(ref spanBase, i + 2) = (byte)(p2 + i); Unsafe.Add(ref spanBase, i + 2) = (byte)(p2 + i);
Unsafe.Add(ref spanBase, i + 3) = (byte)(p3 + i); Unsafe.Add(ref spanBase, i + 3) = (byte)(p3 + i);
@ -511,15 +511,15 @@ internal static partial class SimdUtils
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public static void InverseMMShuffle( public static void InverseMMShuffle(
byte control, byte control,
out int p3, out uint p3,
out int p2, out uint p2,
out int p1, out uint p1,
out int p0) out uint p0)
{ {
p3 = (control >> 6) & 0x3; p3 = (uint)((control >> 6) & 0x3);
p2 = (control >> 4) & 0x3; p2 = (uint)((control >> 4) & 0x3);
p1 = (control >> 2) & 0x3; p1 = (uint)((control >> 2) & 0x3);
p0 = (control >> 0) & 0x3; p0 = (uint)((control >> 0) & 0x3);
} }
} }
} }

2
src/ImageSharp/Compression/Zlib/Crc32.cs

@ -300,7 +300,7 @@ internal static partial class Crc32
for (int i = 0; i < buffer.Length; i++) for (int i = 0; i < buffer.Length; i++)
{ {
crc = Unsafe.Add(ref crcTableRef, (int)((crc ^ Unsafe.Add(ref bufferRef, i)) & 0xFF)) ^ (crc >> 8); crc = Unsafe.Add(ref crcTableRef, (crc ^ Unsafe.Add(ref bufferRef, i)) & 0xFF) ^ (crc >> 8);
} }
return crc; return crc;

5
src/ImageSharp/Compression/Zlib/DeflaterEngine.cs

@ -3,6 +3,7 @@
using System.Buffers; using System.Buffers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Compression.Zlib; namespace SixLabors.ImageSharp.Compression.Zlib;
@ -426,8 +427,8 @@ internal sealed unsafe class DeflaterEngine : IDisposable
private void SlideWindow() private void SlideWindow()
{ {
Unsafe.CopyBlockUnaligned( Unsafe.CopyBlockUnaligned(
ref this.window.Span[0], ref MemoryMarshal.GetReference(this.window.Span),
ref this.window.Span[DeflaterConstants.WSIZE], ref Unsafe.Add(ref MemoryMarshal.GetReference(this.window.Span), DeflaterConstants.WSIZE),
DeflaterConstants.WSIZE); DeflaterConstants.WSIZE);
this.matchStart -= DeflaterConstants.WSIZE; this.matchStart -= DeflaterConstants.WSIZE;

100
src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs

@ -206,8 +206,8 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
int lc = Lcode(litlen); int lc = Lcode(litlen);
this.literalTree.WriteSymbol(pendingBuffer, lc); this.literalTree.WriteSymbol(pendingBuffer, lc);
int bits = (lc - 261) / 4; int bits = (int)(((uint)lc - 261) / 4);
if (bits > 0 && bits <= 5) if (bits is > 0 and <= 5)
{ {
this.Pending.WriteBits(litlen & ((1 << bits) - 1), bits); this.Pending.WriteBits(litlen & ((1 << bits) - 1), bits);
} }
@ -286,13 +286,13 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
int static_len = this.extraBits; int static_len = this.extraBits;
ref byte staticLLengthRef = ref MemoryMarshal.GetReference(StaticLLength); ref byte staticLLengthRef = ref MemoryMarshal.GetReference(StaticLLength);
for (int i = 0; i < LiteralNumber; i++) for (nuint i = 0; i < LiteralNumber; i++)
{ {
static_len += this.literalTree.Frequencies[i] * Unsafe.Add(ref staticLLengthRef, i); static_len += this.literalTree.Frequencies[i] * Unsafe.Add(ref staticLLengthRef, i);
} }
ref byte staticDLengthRef = ref MemoryMarshal.GetReference(StaticDLength); ref byte staticDLengthRef = ref MemoryMarshal.GetReference(StaticDLength);
for (int i = 0; i < DistanceNumber; i++) for (nuint i = 0; i < DistanceNumber; i++)
{ {
static_len += this.distTree.Frequencies[i] * Unsafe.Add(ref staticDLengthRef, i); static_len += this.distTree.Frequencies[i] * Unsafe.Add(ref staticDLengthRef, i);
} }
@ -364,7 +364,7 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
this.literalTree.Frequencies[lc]++; this.literalTree.Frequencies[lc]++;
if (lc >= 265 && lc < 285) if (lc >= 265 && lc < 285)
{ {
this.extraBits += (lc - 261) / 4; this.extraBits += (int)(((uint)lc - 261) / 4);
} }
int dc = Dcode(distance - 1); int dc = Dcode(distance - 1);
@ -405,10 +405,10 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
ref byte bit4ReverseRef = ref MemoryMarshal.GetReference(Bit4Reverse); ref byte bit4ReverseRef = ref MemoryMarshal.GetReference(Bit4Reverse);
return (short)(Unsafe.Add(ref bit4ReverseRef, toReverse & 0xF) << 12 return (short)((Unsafe.Add(ref bit4ReverseRef, (uint)toReverse & 0xF) << 12)
| Unsafe.Add(ref bit4ReverseRef, (toReverse >> 4) & 0xF) << 8 | (Unsafe.Add(ref bit4ReverseRef, (uint)(toReverse >> 4) & 0xF) << 8)
| Unsafe.Add(ref bit4ReverseRef, (toReverse >> 8) & 0xF) << 4 | (Unsafe.Add(ref bit4ReverseRef, (uint)(toReverse >> 8) & 0xF) << 4)
| Unsafe.Add(ref bit4ReverseRef, toReverseRightShiftBy12)); | Unsafe.Add(ref bit4ReverseRef, (uint)toReverseRightShiftBy12));
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -551,8 +551,8 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
int code = 0; int code = 0;
for (int bits = 0; bits < this.maxLength; bits++) for (int bits = 0; bits < this.maxLength; bits++)
{ {
Unsafe.Add(ref nextCodeRef, bits) = code; Unsafe.Add(ref nextCodeRef, (uint)bits) = code;
code += Unsafe.Add(ref bitLengthCountsRef, bits) << (15 - bits); code += Unsafe.Add(ref bitLengthCountsRef, (uint)bits) << (15 - bits);
} }
for (int i = 0; i < this.NumCodes; i++) for (int i = 0; i < this.NumCodes; i++)
@ -560,8 +560,8 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
int bits = this.Length[i]; int bits = this.Length[i];
if (bits > 0) if (bits > 0)
{ {
this.codes[i] = BitReverse(Unsafe.Add(ref nextCodeRef, bits - 1)); this.codes[i] = BitReverse(Unsafe.Add(ref nextCodeRef, (uint)(bits - 1)));
Unsafe.Add(ref nextCodeRef, bits - 1) += 1 << (16 - bits); Unsafe.Add(ref nextCodeRef, (uint)(bits - 1)) += 1 << (16 - bits);
} }
} }
} }
@ -593,13 +593,13 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
// Insert n into heap // Insert n into heap
int pos = heapLen++; int pos = heapLen++;
int ppos; int ppos;
while (pos > 0 && this.Frequencies[Unsafe.Add(ref heapRef, ppos = (pos - 1) >> 1)] > freq) while (pos > 0 && this.Frequencies[Unsafe.Add(ref heapRef, (uint)(ppos = (pos - 1) >> 1))] > freq)
{ {
Unsafe.Add(ref heapRef, pos) = Unsafe.Add(ref heapRef, ppos); Unsafe.Add(ref heapRef, pos) = Unsafe.Add(ref heapRef, (uint)ppos);
pos = ppos; pos = ppos;
} }
Unsafe.Add(ref heapRef, pos) = n; Unsafe.Add(ref heapRef, (uint)pos) = n;
maxCode = n; maxCode = n;
} }
@ -611,7 +611,7 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
// this case, both literals get a 1 bit code. // this case, both literals get a 1 bit code.
while (heapLen < 2) while (heapLen < 2)
{ {
Unsafe.Add(ref heapRef, heapLen++) = maxCode < 2 ? ++maxCode : 0; Unsafe.Add(ref heapRef, (uint)heapLen++) = maxCode < 2 ? ++maxCode : 0;
} }
this.NumCodes = Math.Max(maxCode + 1, this.minNumCodes); this.NumCodes = Math.Max(maxCode + 1, this.minNumCodes);
@ -625,14 +625,14 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
ref int valuesRef = ref MemoryMarshal.GetReference(valuesMemoryOwner.Memory.Span); ref int valuesRef = ref MemoryMarshal.GetReference(valuesMemoryOwner.Memory.Span);
int numNodes = numLeafs; int numNodes = numLeafs;
for (int i = 0; i < heapLen; i++) for (nuint i = 0; i < (uint)heapLen; i++)
{ {
int node = Unsafe.Add(ref heapRef, i); int node = Unsafe.Add(ref heapRef, i);
int i2 = 2 * i; nuint i2 = 2 * i;
Unsafe.Add(ref childrenRef, i2) = node; Unsafe.Add(ref childrenRef, i2) = node;
Unsafe.Add(ref childrenRef, i2 + 1) = -1; Unsafe.Add(ref childrenRef, i2 + 1) = -1;
Unsafe.Add(ref valuesRef, i) = this.Frequencies[node] << 8; Unsafe.Add(ref valuesRef, i) = this.Frequencies[node] << 8;
Unsafe.Add(ref heapRef, i) = i; Unsafe.Add(ref heapRef, i) = (int)i;
} }
// Construct the Huffman tree by repeatedly combining the least two // Construct the Huffman tree by repeatedly combining the least two
@ -640,7 +640,7 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
do do
{ {
int first = Unsafe.Add(ref heapRef, 0); int first = Unsafe.Add(ref heapRef, 0);
int last = Unsafe.Add(ref heapRef, --heapLen); int last = Unsafe.Add(ref heapRef, (uint)--heapLen);
// Propagate the hole to the leafs of the heap // Propagate the hole to the leafs of the heap
int ppos = 0; int ppos = 0;
@ -648,35 +648,35 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
while (path < heapLen) while (path < heapLen)
{ {
if (path + 1 < heapLen && Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, path)) > Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, path + 1))) if (path + 1 < heapLen && Unsafe.Add(ref valuesRef, (uint)Unsafe.Add(ref heapRef, (uint)path)) > Unsafe.Add(ref valuesRef, (uint)Unsafe.Add(ref heapRef, (uint)(path + 1))))
{ {
path++; path++;
} }
Unsafe.Add(ref heapRef, ppos) = Unsafe.Add(ref heapRef, path); Unsafe.Add(ref heapRef, (uint)ppos) = Unsafe.Add(ref heapRef, (uint)path);
ppos = path; ppos = path;
path = (path * 2) + 1; path = (path * 2) + 1;
} }
// Now propagate the last element down along path. Normally // Now propagate the last element down along path. Normally
// it shouldn't go too deep. // it shouldn't go too deep.
int lastVal = Unsafe.Add(ref valuesRef, last); int lastVal = Unsafe.Add(ref valuesRef, (uint)last);
while ((path = ppos) > 0 while ((path = ppos) > 0
&& Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, ppos = (path - 1) >> 1)) > lastVal) && Unsafe.Add(ref valuesRef, (uint)Unsafe.Add(ref heapRef, (uint)(ppos = (path - 1) >> 1))) > lastVal)
{ {
Unsafe.Add(ref heapRef, path) = Unsafe.Add(ref heapRef, ppos); Unsafe.Add(ref heapRef, (uint)path) = Unsafe.Add(ref heapRef, (uint)ppos);
} }
Unsafe.Add(ref heapRef, path) = last; Unsafe.Add(ref heapRef, (uint)path) = last;
int second = Unsafe.Add(ref heapRef, 0); int second = Unsafe.Add(ref heapRef, 0);
// Create a new node father of first and second // Create a new node father of first and second
last = numNodes++; last = numNodes++;
Unsafe.Add(ref childrenRef, 2 * last) = first; Unsafe.Add(ref childrenRef, (uint)(2 * last)) = first;
Unsafe.Add(ref childrenRef, (2 * last) + 1) = second; Unsafe.Add(ref childrenRef, (uint)((2 * last) + 1)) = second;
int mindepth = Math.Min(Unsafe.Add(ref valuesRef, first) & 0xFF, Unsafe.Add(ref valuesRef, second) & 0xFF); int mindepth = Math.Min(Unsafe.Add(ref valuesRef, (uint)first) & 0xFF, Unsafe.Add(ref valuesRef, (uint)second) & 0xFF);
Unsafe.Add(ref valuesRef, last) = lastVal = Unsafe.Add(ref valuesRef, first) + Unsafe.Add(ref valuesRef, second) - mindepth + 1; Unsafe.Add(ref valuesRef, (uint)last) = lastVal = Unsafe.Add(ref valuesRef, (uint)first) + Unsafe.Add(ref valuesRef, (uint)second) - mindepth + 1;
// Again, propagate the hole to the leafs // Again, propagate the hole to the leafs
ppos = 0; ppos = 0;
@ -685,23 +685,23 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
while (path < heapLen) while (path < heapLen)
{ {
if (path + 1 < heapLen if (path + 1 < heapLen
&& Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, path)) > Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, path + 1))) && Unsafe.Add(ref valuesRef, (uint)Unsafe.Add(ref heapRef, (uint)path)) > Unsafe.Add(ref valuesRef, (uint)Unsafe.Add(ref heapRef, (uint)(path + 1))))
{ {
path++; path++;
} }
Unsafe.Add(ref heapRef, ppos) = Unsafe.Add(ref heapRef, path); Unsafe.Add(ref heapRef, (uint)ppos) = Unsafe.Add(ref heapRef, (uint)path);
ppos = path; ppos = path;
path = (ppos * 2) + 1; path = (ppos * 2) + 1;
} }
// Now propagate the new element down along path // Now propagate the new element down along path
while ((path = ppos) > 0 && Unsafe.Add(ref valuesRef, Unsafe.Add(ref heapRef, ppos = (path - 1) >> 1)) > lastVal) while ((path = ppos) > 0 && Unsafe.Add(ref valuesRef, (uint)Unsafe.Add(ref heapRef, (uint)(ppos = (path - 1) >> 1))) > lastVal)
{ {
Unsafe.Add(ref heapRef, path) = Unsafe.Add(ref heapRef, ppos); Unsafe.Add(ref heapRef, (uint)path) = Unsafe.Add(ref heapRef, (uint)ppos);
} }
Unsafe.Add(ref heapRef, path) = last; Unsafe.Add(ref heapRef, (uint)path) = last;
} }
while (heapLen > 1); while (heapLen > 1);
@ -886,21 +886,21 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
{ {
if (children[(2 * i) + 1] != -1) if (children[(2 * i) + 1] != -1)
{ {
int bitLength = Unsafe.Add(ref lengthsRef, i) + 1; int bitLength = Unsafe.Add(ref lengthsRef, (uint)i) + 1;
if (bitLength > maxLen) if (bitLength > maxLen)
{ {
bitLength = maxLen; bitLength = maxLen;
overflow++; overflow++;
} }
Unsafe.Add(ref lengthsRef, Unsafe.Add(ref childrenRef, 2 * i)) = Unsafe.Add(ref lengthsRef, Unsafe.Add(ref childrenRef, (2 * i) + 1)) = bitLength; Unsafe.Add(ref lengthsRef, (uint)Unsafe.Add(ref childrenRef, (uint)(2 * i))) = Unsafe.Add(ref lengthsRef, (uint)Unsafe.Add(ref childrenRef, (uint)((2 * i) + 1))) = bitLength;
} }
else else
{ {
// A leaf node // A leaf node
int bitLength = Unsafe.Add(ref lengthsRef, i); int bitLength = Unsafe.Add(ref lengthsRef, (uint)i);
Unsafe.Add(ref bitLengthCountsRef, bitLength - 1)++; Unsafe.Add(ref bitLengthCountsRef, (uint)(bitLength - 1))++;
lengthPtr[Unsafe.Add(ref childrenRef, 2 * i)] = (byte)Unsafe.Add(ref lengthsRef, i); lengthPtr[Unsafe.Add(ref childrenRef, (uint)(2 * i))] = (byte)Unsafe.Add(ref lengthsRef, (uint)i);
} }
} }
} }
@ -914,7 +914,7 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
do do
{ {
// Find the first bit length which could increase: // Find the first bit length which could increase:
while (Unsafe.Add(ref bitLengthCountsRef, --incrBitLen) == 0) while (Unsafe.Add(ref bitLengthCountsRef, (uint)--incrBitLen) == 0)
{ {
} }
@ -922,8 +922,8 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
// number of overflow nodes. // number of overflow nodes.
do do
{ {
Unsafe.Add(ref bitLengthCountsRef, incrBitLen)--; Unsafe.Add(ref bitLengthCountsRef, (uint)incrBitLen)--;
Unsafe.Add(ref bitLengthCountsRef, ++incrBitLen)++; Unsafe.Add(ref bitLengthCountsRef, (uint)++incrBitLen)++;
overflow -= 1 << (maxLen - 1 - incrBitLen); overflow -= 1 << (maxLen - 1 - incrBitLen);
} }
while (overflow > 0 && incrBitLen < maxLen - 1); while (overflow > 0 && incrBitLen < maxLen - 1);
@ -932,8 +932,8 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
// We may have overshot above. Move some nodes from maxLength to // We may have overshot above. Move some nodes from maxLength to
// maxLength-1 in that case. // maxLength-1 in that case.
Unsafe.Add(ref bitLengthCountsRef, maxLen - 1) += overflow; Unsafe.Add(ref bitLengthCountsRef, (uint)(maxLen - 1)) += overflow;
Unsafe.Add(ref bitLengthCountsRef, maxLen - 2) -= overflow; Unsafe.Add(ref bitLengthCountsRef, (uint)(maxLen - 2)) -= overflow;
// Now recompute all bit lengths, scanning in increasing // Now recompute all bit lengths, scanning in increasing
// frequency. It is simpler to reconstruct all lengths instead of // frequency. It is simpler to reconstruct all lengths instead of
@ -945,14 +945,14 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
int nodeIndex = 2 * numLeafs; int nodeIndex = 2 * numLeafs;
for (int bits = maxLen; bits != 0; bits--) for (int bits = maxLen; bits != 0; bits--)
{ {
int n = Unsafe.Add(ref bitLengthCountsRef, bits - 1); int n = Unsafe.Add(ref bitLengthCountsRef, (uint)(bits - 1));
while (n > 0) while (n > 0)
{ {
int childIndex = 2 * Unsafe.Add(ref childrenRef, nodeIndex++); int childIndex = 2 * Unsafe.Add(ref childrenRef, (uint)nodeIndex++);
if (Unsafe.Add(ref childrenRef, childIndex + 1) == -1) if (Unsafe.Add(ref childrenRef, (uint)(childIndex + 1)) == -1)
{ {
// We found another leaf // We found another leaf
lengthPtr[Unsafe.Add(ref childrenRef, childIndex)] = (byte)bits; lengthPtr[Unsafe.Add(ref childrenRef, (uint)childIndex)] = (byte)bits;
n--; n--;
} }
} }

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

@ -489,7 +489,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
// If the second byte > 2, we are in 'absolute mode'. // If the second byte > 2, we are in 'absolute mode'.
// The second byte contains the number of color indexes that follow. // The second byte contains the number of color indexes that follow.
int max = cmd[1]; int max = cmd[1];
int bytesToRead = (max + 1) / 2; int bytesToRead = (int)(((uint)max + 1) / 2);
byte[] run = new byte[bytesToRead]; byte[] run = new byte[bytesToRead];
@ -1361,7 +1361,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
this.metadata.VerticalResolution = Math.Round(UnitConverter.InchToMeter(ImageMetadata.DefaultVerticalResolution)); this.metadata.VerticalResolution = Math.Round(UnitConverter.InchToMeter(ImageMetadata.DefaultVerticalResolution));
} }
short bitsPerPixel = this.infoHeader.BitsPerPixel; ushort bitsPerPixel = this.infoHeader.BitsPerPixel;
this.bmpMetadata = this.metadata.GetBmpMetadata(); this.bmpMetadata = this.metadata.GetBmpMetadata();
this.bmpMetadata.InfoHeaderType = infoHeaderType; this.bmpMetadata.InfoHeaderType = infoHeaderType;
this.bmpMetadata.BitsPerPixel = (BmpBitsPerPixel)bitsPerPixel; this.bmpMetadata.BitsPerPixel = (BmpBitsPerPixel)bitsPerPixel;

6
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -123,8 +123,8 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals
BmpMetadata bmpMetadata = metadata.GetBmpMetadata(); BmpMetadata bmpMetadata = metadata.GetBmpMetadata();
this.bitsPerPixel ??= bmpMetadata.BitsPerPixel; this.bitsPerPixel ??= bmpMetadata.BitsPerPixel;
short bpp = (short)this.bitsPerPixel; ushort bpp = (ushort)this.bitsPerPixel;
int bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32); int bytesPerLine = (int)(4 * ((((uint)image.Width * bpp) + 31) / 32));
this.padding = bytesPerLine - (int)(image.Width * (bpp / 8F)); this.padding = bytesPerLine - (int)(image.Width * (bpp / 8F));
int colorPaletteSize = this.bitsPerPixel switch int colorPaletteSize = this.bitsPerPixel switch
@ -176,7 +176,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals
/// <param name="metadata">The metadata.</param> /// <param name="metadata">The metadata.</param>
/// <param name="iccProfileData">The icc profile data.</param> /// <param name="iccProfileData">The icc profile data.</param>
/// <returns>The bitmap information header.</returns> /// <returns>The bitmap information header.</returns>
private BmpInfoHeader CreateBmpInfoHeader(int width, int height, int infoHeaderSize, short bpp, int bytesPerLine, ImageMetadata metadata, byte[]? iccProfileData) private BmpInfoHeader CreateBmpInfoHeader(int width, int height, int infoHeaderSize, ushort bpp, int bytesPerLine, ImageMetadata metadata, byte[]? iccProfileData)
{ {
int hResolution = 0; int hResolution = 0;
int vResolution = 0; int vResolution = 0;

20
src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs

@ -70,7 +70,7 @@ internal struct BmpInfoHeader
int width, int width,
int height, int height,
short planes, short planes,
short bitsPerPixel, ushort bitsPerPixel,
BmpCompression compression = default, BmpCompression compression = default,
int imageSize = 0, int imageSize = 0,
int xPelsPerMeter = 0, int xPelsPerMeter = 0,
@ -157,7 +157,7 @@ internal struct BmpInfoHeader
/// Gets or sets the number of bits per pixel, which is the color depth of the image. /// Gets or sets the number of bits per pixel, which is the color depth of the image.
/// Typical values are 1, 4, 8, 16, 24 and 32. /// Typical values are 1, 4, 8, 16, 24 and 32.
/// </summary> /// </summary>
public short BitsPerPixel { get; set; } public ushort BitsPerPixel { get; set; }
/// <summary> /// <summary>
/// Gets or sets the compression method being used. /// Gets or sets the compression method being used.
@ -311,7 +311,7 @@ internal struct BmpInfoHeader
width: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(4, 2)), width: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(4, 2)),
height: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(6, 2)), height: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(6, 2)),
planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(8, 2)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(8, 2)),
bitsPerPixel: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(10, 2))); bitsPerPixel: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(10, 2)));
/// <summary> /// <summary>
/// Parses a short variant of the OS22XBITMAPHEADER. It is identical to the BITMAPCOREHEADER, except that the width and height /// Parses a short variant of the OS22XBITMAPHEADER. It is identical to the BITMAPCOREHEADER, except that the width and height
@ -325,7 +325,7 @@ internal struct BmpInfoHeader
width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)),
height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)),
planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)),
bitsPerPixel: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(14, 2))); bitsPerPixel: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(14, 2)));
/// <summary> /// <summary>
/// Parses the full BMP Version 3 BITMAPINFOHEADER header (40 bytes). /// Parses the full BMP Version 3 BITMAPINFOHEADER header (40 bytes).
@ -338,7 +338,7 @@ internal struct BmpInfoHeader
width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)),
height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)),
planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)),
bitsPerPixel: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(14, 2)), bitsPerPixel: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(14, 2)),
compression: (BmpCompression)BinaryPrimitives.ReadInt32LittleEndian(data.Slice(16, 4)), compression: (BmpCompression)BinaryPrimitives.ReadInt32LittleEndian(data.Slice(16, 4)),
imageSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(20, 4)), imageSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(20, 4)),
xPelsPerMeter: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(24, 4)), xPelsPerMeter: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(24, 4)),
@ -359,7 +359,7 @@ internal struct BmpInfoHeader
width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)),
height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)),
planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)),
bitsPerPixel: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(14, 2)), bitsPerPixel: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(14, 2)),
compression: (BmpCompression)BinaryPrimitives.ReadInt32LittleEndian(data.Slice(16, 4)), compression: (BmpCompression)BinaryPrimitives.ReadInt32LittleEndian(data.Slice(16, 4)),
imageSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(20, 4)), imageSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(20, 4)),
xPelsPerMeter: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(24, 4)), xPelsPerMeter: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(24, 4)),
@ -386,7 +386,7 @@ internal struct BmpInfoHeader
width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)),
height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)),
planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)),
bitsPerPixel: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(14, 2))); bitsPerPixel: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(14, 2)));
// The compression value in OS/2 bitmap has a different meaning than in windows bitmaps. // The compression value in OS/2 bitmap has a different meaning than in windows bitmaps.
// Map the OS/2 value to the windows values. // Map the OS/2 value to the windows values.
@ -431,7 +431,7 @@ internal struct BmpInfoHeader
width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)),
height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)),
planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)),
bitsPerPixel: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(14, 2)), bitsPerPixel: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(14, 2)),
compression: (BmpCompression)BinaryPrimitives.ReadInt32LittleEndian(data.Slice(16, 4)), compression: (BmpCompression)BinaryPrimitives.ReadInt32LittleEndian(data.Slice(16, 4)),
imageSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(20, 4)), imageSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(20, 4)),
xPelsPerMeter: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(24, 4)), xPelsPerMeter: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(24, 4)),
@ -484,7 +484,7 @@ internal struct BmpInfoHeader
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(4, 4), this.Width); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(4, 4), this.Width);
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(8, 4), this.Height); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(8, 4), this.Height);
BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(12, 2), this.Planes); BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(12, 2), this.Planes);
BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(14, 2), this.BitsPerPixel); BinaryPrimitives.WriteUInt16LittleEndian(buffer.Slice(14, 2), this.BitsPerPixel);
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(16, 4), (int)this.Compression); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(16, 4), (int)this.Compression);
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(20, 4), this.ImageSize); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(20, 4), this.ImageSize);
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(24, 4), this.XPelsPerMeter); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(24, 4), this.XPelsPerMeter);
@ -504,7 +504,7 @@ internal struct BmpInfoHeader
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(4, 4), this.Width); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(4, 4), this.Width);
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(8, 4), this.Height); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(8, 4), this.Height);
BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(12, 2), this.Planes); BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(12, 2), this.Planes);
BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(14, 2), this.BitsPerPixel); BinaryPrimitives.WriteUInt16LittleEndian(buffer.Slice(14, 2), this.BitsPerPixel);
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(16, 4), (int)this.Compression); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(16, 4), (int)this.Compression);
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(20, 4), this.ImageSize); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(20, 4), this.ImageSize);
BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(24, 4), this.XPelsPerMeter); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(24, 4), this.XPelsPerMeter);

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

@ -578,8 +578,8 @@ internal sealed class GifDecoderCore : IImageDecoderInternals
// #403 The left + width value can be larger than the image width // #403 The left + width value can be larger than the image width
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, 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, x); ref TPixel pixel = ref Unsafe.Add(ref rowRef, (uint)x);
Rgb24 rgb = colorTable[index]; Rgb24 rgb = colorTable[index];
pixel.FromRgb24(rgb); pixel.FromRgb24(rgb);
} }
@ -588,7 +588,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 rawIndex = Unsafe.Add(ref indicesRowRef, x - descriptorLeft); int rawIndex = Unsafe.Add(ref indicesRowRef, (uint)(x - descriptorLeft));
// Treat any out of bounds values as transparent. // Treat any out of bounds values as transparent.
if (rawIndex > colorTableMaxIdx || rawIndex == transIndex) if (rawIndex > colorTableMaxIdx || rawIndex == transIndex)
@ -597,7 +597,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, x); ref TPixel pixel = ref Unsafe.Add(ref rowRef, (uint)x);
Rgb24 rgb = colorTable[index]; Rgb24 rgb = colorTable[index];
pixel.FromRgb24(rgb); pixel.FromRgb24(rgb);
} }

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

@ -254,7 +254,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
for (int i = rgbaSpan.Length - 1; i >= 0; i--) for (int i = rgbaSpan.Length - 1; i >= 0; i--)
{ {
if (Unsafe.Add(ref rgbaSpanRef, i).Equals(default)) if (Unsafe.Add(ref rgbaSpanRef, (uint)i).Equals(default))
{ {
index = i; index = i;
} }

38
src/ImageSharp/Formats/Gif/LzwDecoder.cs

@ -115,7 +115,7 @@ internal sealed class LzwDecoder : IDisposable
for (code = 0; code < clearCode; code++) for (code = 0; code < clearCode; code++)
{ {
Unsafe.Add(ref suffixRef, code) = (byte)code; Unsafe.Add(ref suffixRef, (uint)code) = (byte)code;
} }
Span<byte> buffer = stackalloc byte[byte.MaxValue]; Span<byte> buffer = stackalloc byte[byte.MaxValue];
@ -182,7 +182,7 @@ internal sealed class LzwDecoder : IDisposable
if (oldCode == NullCode) if (oldCode == NullCode)
{ {
Unsafe.Add(ref pixelStackRef, top++) = Unsafe.Add(ref suffixRef, code); Unsafe.Add(ref pixelStackRef, (uint)top++) = Unsafe.Add(ref suffixRef, (uint)code);
oldCode = code; oldCode = code;
first = code; first = code;
continue; continue;
@ -191,27 +191,27 @@ internal sealed class LzwDecoder : IDisposable
int inCode = code; int inCode = code;
if (code == availableCode) if (code == availableCode)
{ {
Unsafe.Add(ref pixelStackRef, top++) = (byte)first; Unsafe.Add(ref pixelStackRef, (uint)top++) = (byte)first;
code = oldCode; code = oldCode;
} }
while (code > clearCode) while (code > clearCode)
{ {
Unsafe.Add(ref pixelStackRef, top++) = Unsafe.Add(ref suffixRef, code); Unsafe.Add(ref pixelStackRef, (uint)top++) = Unsafe.Add(ref suffixRef, (uint)code);
code = Unsafe.Add(ref prefixRef, code); code = Unsafe.Add(ref prefixRef, (uint)code);
} }
int suffixCode = Unsafe.Add(ref suffixRef, code); int suffixCode = Unsafe.Add(ref suffixRef, (uint)code);
first = suffixCode; first = suffixCode;
Unsafe.Add(ref pixelStackRef, top++) = suffixCode; Unsafe.Add(ref pixelStackRef, (uint)top++) = suffixCode;
// Fix for Gifs that have "deferred clear code" as per here : // Fix for Gifs that have "deferred clear code" as per here :
// https://bugzilla.mozilla.org/show_bug.cgi?id=55918 // https://bugzilla.mozilla.org/show_bug.cgi?id=55918
if (availableCode < MaxStackSize) if (availableCode < MaxStackSize)
{ {
Unsafe.Add(ref prefixRef, availableCode) = oldCode; Unsafe.Add(ref prefixRef, (uint)availableCode) = oldCode;
Unsafe.Add(ref suffixRef, availableCode) = first; Unsafe.Add(ref suffixRef, (uint)availableCode) = first;
availableCode++; availableCode++;
if (availableCode == codeMask + 1 && availableCode < MaxStackSize) if (availableCode == codeMask + 1 && availableCode < MaxStackSize)
{ {
@ -228,7 +228,7 @@ internal sealed class LzwDecoder : IDisposable
// Clear missing pixels // Clear missing pixels
xyz++; xyz++;
Unsafe.Add(ref pixelsRowRef, x++) = (byte)Unsafe.Add(ref pixelStackRef, top); Unsafe.Add(ref pixelsRowRef, (uint)x++) = (byte)Unsafe.Add(ref pixelStackRef, (uint)top);
} }
} }
@ -282,7 +282,7 @@ internal sealed class LzwDecoder : IDisposable
for (code = 0; code < clearCode; code++) for (code = 0; code < clearCode; code++)
{ {
Unsafe.Add(ref suffixRef, code) = (byte)code; Unsafe.Add(ref suffixRef, (uint)code) = (byte)code;
} }
Span<byte> buffer = stackalloc byte[byte.MaxValue]; Span<byte> buffer = stackalloc byte[byte.MaxValue];
@ -336,7 +336,7 @@ internal sealed class LzwDecoder : IDisposable
if (oldCode == NullCode) if (oldCode == NullCode)
{ {
Unsafe.Add(ref pixelStackRef, top++) = Unsafe.Add(ref suffixRef, code); Unsafe.Add(ref pixelStackRef, (uint)top++) = Unsafe.Add(ref suffixRef, (uint)code);
oldCode = code; oldCode = code;
first = code; first = code;
continue; continue;
@ -345,27 +345,27 @@ internal sealed class LzwDecoder : IDisposable
int inCode = code; int inCode = code;
if (code == availableCode) if (code == availableCode)
{ {
Unsafe.Add(ref pixelStackRef, top++) = (byte)first; Unsafe.Add(ref pixelStackRef, (uint)top++) = (byte)first;
code = oldCode; code = oldCode;
} }
while (code > clearCode) while (code > clearCode)
{ {
Unsafe.Add(ref pixelStackRef, top++) = Unsafe.Add(ref suffixRef, code); Unsafe.Add(ref pixelStackRef, (uint)top++) = Unsafe.Add(ref suffixRef, (uint)code);
code = Unsafe.Add(ref prefixRef, code); code = Unsafe.Add(ref prefixRef, (uint)code);
} }
int suffixCode = Unsafe.Add(ref suffixRef, code); int suffixCode = Unsafe.Add(ref suffixRef, (uint)code);
first = suffixCode; first = suffixCode;
Unsafe.Add(ref pixelStackRef, top++) = suffixCode; Unsafe.Add(ref pixelStackRef, (uint)top++) = suffixCode;
// Fix for Gifs that have "deferred clear code" as per here : // Fix for Gifs that have "deferred clear code" as per here :
// https://bugzilla.mozilla.org/show_bug.cgi?id=55918 // https://bugzilla.mozilla.org/show_bug.cgi?id=55918
if (availableCode < MaxStackSize) if (availableCode < MaxStackSize)
{ {
Unsafe.Add(ref prefixRef, availableCode) = oldCode; Unsafe.Add(ref prefixRef, (uint)availableCode) = oldCode;
Unsafe.Add(ref suffixRef, availableCode) = first; Unsafe.Add(ref suffixRef, (uint)availableCode) = first;
availableCode++; availableCode++;
if (availableCode == codeMask + 1 && availableCode < MaxStackSize) if (availableCode == codeMask + 1 && availableCode < MaxStackSize)
{ {

22
src/ImageSharp/Formats/Gif/LzwEncoder.cs

@ -216,7 +216,7 @@ internal sealed class LzwEncoder : IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AddCharacter(byte c, ref byte accumulatorsRef, Stream stream) private void AddCharacter(byte c, ref byte accumulatorsRef, Stream stream)
{ {
Unsafe.Add(ref accumulatorsRef, this.accumulatorCount++) = c; Unsafe.Add(ref accumulatorsRef, (uint)this.accumulatorCount++) = c;
if (this.accumulatorCount >= 254) if (this.accumulatorCount >= 254)
{ {
this.FlushPacket(stream); this.FlushPacket(stream);
@ -278,18 +278,18 @@ internal sealed class LzwEncoder : IDisposable
for (int x = offsetX; x < indexedPixels.Width; x++) for (int x = offsetX; x < indexedPixels.Width; x++)
{ {
int code = Unsafe.Add(ref rowSpanRef, x); int code = Unsafe.Add(ref rowSpanRef, (uint)x);
int freeCode = (code << MaxBits) + entry; int freeCode = (code << MaxBits) + entry;
int hashIndex = (code << HashShift) ^ entry; int hashIndex = (code << HashShift) ^ entry;
if (Unsafe.Add(ref hashTableRef, hashIndex) == freeCode) if (Unsafe.Add(ref hashTableRef, (uint)hashIndex) == freeCode)
{ {
entry = Unsafe.Add(ref codeTableRef, hashIndex); entry = Unsafe.Add(ref codeTableRef, (uint)hashIndex);
continue; continue;
} }
// Non-empty slot // Non-empty slot
if (Unsafe.Add(ref hashTableRef, hashIndex) >= 0) if (Unsafe.Add(ref hashTableRef, (uint)hashIndex) >= 0)
{ {
int disp = 1; int disp = 1;
if (hashIndex != 0) if (hashIndex != 0)
@ -304,15 +304,15 @@ internal sealed class LzwEncoder : IDisposable
hashIndex += HashSize; hashIndex += HashSize;
} }
if (Unsafe.Add(ref hashTableRef, hashIndex) == freeCode) if (Unsafe.Add(ref hashTableRef, (uint)hashIndex) == freeCode)
{ {
entry = Unsafe.Add(ref codeTableRef, hashIndex); entry = Unsafe.Add(ref codeTableRef, (uint)hashIndex);
break; break;
} }
} }
while (Unsafe.Add(ref hashTableRef, hashIndex) >= 0); while (Unsafe.Add(ref hashTableRef, (uint)hashIndex) >= 0);
if (Unsafe.Add(ref hashTableRef, hashIndex) == freeCode) if (Unsafe.Add(ref hashTableRef, (uint)hashIndex) == freeCode)
{ {
continue; continue;
} }
@ -322,8 +322,8 @@ internal sealed class LzwEncoder : IDisposable
entry = code; entry = code;
if (this.freeEntry < MaxMaxCode) if (this.freeEntry < MaxMaxCode)
{ {
Unsafe.Add(ref codeTableRef, hashIndex) = this.freeEntry++; // code -> hashtable Unsafe.Add(ref codeTableRef, (uint)hashIndex) = this.freeEntry++; // code -> hashtable
Unsafe.Add(ref hashTableRef, hashIndex) = freeCode; Unsafe.Add(ref hashTableRef, (uint)hashIndex) = freeCode;
} }
else else
{ {

8
src/ImageSharp/Formats/ImageExtensions.Save.tt

@ -77,7 +77,7 @@ public static partial class ImageExtensions
public static void SaveAs<#= fmt #>(this Image source, string path, <#= fmt #>Encoder encoder) => public static void SaveAs<#= fmt #>(this Image source, string path, <#= fmt #>Encoder encoder) =>
source.Save( source.Save(
path, path,
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(<#= fmt #>Format.Instance)); encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(<#= fmt #>Format.Instance));
/// <summary> /// <summary>
/// Saves the image to the given stream with the <#= fmt #> format. /// Saves the image to the given stream with the <#= fmt #> format.
@ -91,7 +91,7 @@ public static partial class ImageExtensions
public static Task SaveAs<#= fmt #>Async(this Image source, string path, <#= fmt #>Encoder encoder, CancellationToken cancellationToken = default) public static Task SaveAs<#= fmt #>Async(this Image source, string path, <#= fmt #>Encoder encoder, CancellationToken cancellationToken = default)
=> source.SaveAsync( => source.SaveAsync(
path, path,
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(<#= fmt #>Format.Instance), encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(<#= fmt #>Format.Instance),
cancellationToken); cancellationToken);
/// <summary> /// <summary>
@ -124,7 +124,7 @@ public static partial class ImageExtensions
public static void SaveAs<#= fmt #>(this Image source, Stream stream, <#= fmt #>Encoder encoder) public static void SaveAs<#= fmt #>(this Image source, Stream stream, <#= fmt #>Encoder encoder)
=> source.Save( => source.Save(
stream, stream,
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(<#= fmt #>Format.Instance)); encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(<#= fmt #>Format.Instance));
/// <summary> /// <summary>
/// Saves the image to the given stream with the <#= fmt #> format. /// Saves the image to the given stream with the <#= fmt #> format.
@ -138,7 +138,7 @@ public static partial class ImageExtensions
public static Task SaveAs<#= fmt #>Async(this Image source, Stream stream, <#= fmt #>Encoder encoder, CancellationToken cancellationToken = default) public static Task SaveAs<#= fmt #>Async(this Image source, Stream stream, <#= fmt #>Encoder encoder, CancellationToken cancellationToken = default)
=> source.SaveAsync( => source.SaveAsync(
stream, stream,
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(<#= fmt #>Format.Instance), encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(<#= fmt #>Format.Instance),
cancellationToken); cancellationToken);
<# <#

10
src/ImageSharp/Formats/Jpeg/Components/Block8x8.cs

@ -47,7 +47,7 @@ internal unsafe partial struct Block8x8
DebugGuard.MustBeBetweenOrEqualTo(idx, 0, Size - 1, nameof(idx)); DebugGuard.MustBeBetweenOrEqualTo(idx, 0, Size - 1, nameof(idx));
ref short selfRef = ref Unsafe.As<Block8x8, short>(ref this); ref short selfRef = ref Unsafe.As<Block8x8, short>(ref this);
return Unsafe.Add(ref selfRef, idx); return Unsafe.Add(ref selfRef, (uint)idx);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -56,7 +56,7 @@ internal unsafe partial struct Block8x8
DebugGuard.MustBeBetweenOrEqualTo(idx, 0, Size - 1, nameof(idx)); DebugGuard.MustBeBetweenOrEqualTo(idx, 0, Size - 1, nameof(idx));
ref short selfRef = ref Unsafe.As<Block8x8, short>(ref this); ref short selfRef = ref Unsafe.As<Block8x8, short>(ref this);
Unsafe.Add(ref selfRef, idx) = value; Unsafe.Add(ref selfRef, (uint)idx) = value;
} }
} }
@ -207,12 +207,12 @@ internal unsafe partial struct Block8x8
// Given mask is not actually suitable for lzcnt as 1's represent zero elements and 0's represent non-zero elements // Given mask is not actually suitable for lzcnt as 1's represent zero elements and 0's represent non-zero elements
// So we need to invert it // So we need to invert it
int lzcnt = BitOperations.LeadingZeroCount(~(uint)areEqual); uint lzcnt = (uint)BitOperations.LeadingZeroCount(~(uint)areEqual);
// As input number is represented by 2 bits in the mask, we need to divide lzcnt result by 2 // As input number is represented by 2 bits in the mask, we need to divide lzcnt result by 2
// to get the exact number of zero elements in the stride // to get the exact number of zero elements in the stride
int strideRelativeIndex = 15 - (lzcnt / 2); uint strideRelativeIndex = 15 - (lzcnt / 2);
return (i * 16) + strideRelativeIndex; return (i * 16) + (nint)strideRelativeIndex;
} }
} }

22
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.cs

@ -16,7 +16,7 @@ internal partial struct Block8x8F
{ {
var CMin4 = new Vector4(0F); var CMin4 = new Vector4(0F);
var CMax4 = new Vector4(maximum); var CMax4 = new Vector4(maximum);
var COff4 = new Vector4(MathF.Ceiling(maximum / 2)); var COff4 = new Vector4(MathF.Ceiling(maximum * 0.5F));
this.V0L = Numerics.Clamp(this.V0L + COff4, CMin4, CMax4); this.V0L = Numerics.Clamp(this.V0L + COff4, CMin4, CMax4);
this.V0R = Numerics.Clamp(this.V0R + COff4, CMin4, CMax4); this.V0R = Numerics.Clamp(this.V0R + COff4, CMin4, CMax4);
@ -42,33 +42,33 @@ internal partial struct Block8x8F
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public void NormalizeColorsAndRoundInPlaceVector8(float maximum) public void NormalizeColorsAndRoundInPlaceVector8(float maximum)
{ {
var off = new Vector<float>(MathF.Ceiling(maximum / 2)); var off = new Vector<float>(MathF.Ceiling(maximum * 0.5F));
var max = new Vector<float>(maximum); var max = new Vector<float>(maximum);
ref Vector<float> row0 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V0L); ref Vector<float> row0 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V0L);
row0 = NormalizeAndRound(row0, off, max); row0 = NormalizeAndRound(row0, off, max);
ref Vector<float> row1 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V1L); ref Vector<float> row1 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V1L);
row1 = NormalizeAndRound(row1, off, max); row1 = NormalizeAndRound(row1, off, max);
ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V2L); ref Vector<float> row2 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V2L);
row2 = NormalizeAndRound(row2, off, max); row2 = NormalizeAndRound(row2, off, max);
ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V3L); ref Vector<float> row3 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V3L);
row3 = NormalizeAndRound(row3, off, max); row3 = NormalizeAndRound(row3, off, max);
ref Vector<float> row4 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V4L); ref Vector<float> row4 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V4L);
row4 = NormalizeAndRound(row4, off, max); row4 = NormalizeAndRound(row4, off, max);
ref Vector<float> row5 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V5L); ref Vector<float> row5 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V5L);
row5 = NormalizeAndRound(row5, off, max); row5 = NormalizeAndRound(row5, off, max);
ref Vector<float> row6 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V6L); ref Vector<float> row6 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V6L);
row6 = NormalizeAndRound(row6, off, max); row6 = NormalizeAndRound(row6, off, max);
ref Vector<float> row7 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V7L); ref Vector<float> row7 = ref Unsafe.As<Vector4, Vector<float>>(ref this.V7L);
row7 = NormalizeAndRound(row7, off, max); row7 = NormalizeAndRound(row7, off, max);
} }
/// <summary> /// <summary>

4
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Generated.tt

@ -29,7 +29,7 @@ internal partial struct Block8x8F
{ {
var CMin4 = new Vector4(0F); var CMin4 = new Vector4(0F);
var CMax4 = new Vector4(maximum); var CMax4 = new Vector4(maximum);
var COff4 = new Vector4(MathF.Ceiling(maximum / 2)); var COff4 = new Vector4(MathF.Ceiling(maximum * 0.5F));
<# <#
@ -53,7 +53,7 @@ internal partial struct Block8x8F
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public void NormalizeColorsAndRoundInPlaceVector8(float maximum) public void NormalizeColorsAndRoundInPlaceVector8(float maximum)
{ {
var off = new Vector<float>(MathF.Ceiling(maximum / 2)); var off = new Vector<float>(MathF.Ceiling(maximum * 0.5F));
var max = new Vector<float>(maximum); var max = new Vector<float>(maximum);
<# <#

10
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.Intrinsic.cs

@ -41,9 +41,9 @@ internal partial struct Block8x8F
ref Vector256<float> bBase = ref b.V0; ref Vector256<float> bBase = ref b.V0;
ref Vector256<short> destRef = ref dest.V01; ref Vector256<short> destRef = ref dest.V01;
var multiplyIntoInt16ShuffleMask = Vector256.Create(0, 1, 4, 5, 2, 3, 6, 7); Vector256<int> multiplyIntoInt16ShuffleMask = Vector256.Create(0, 1, 4, 5, 2, 3, 6, 7);
for (nint i = 0; i < 8; i += 2) for (nuint i = 0; i < 8; i += 2)
{ {
Vector256<int> row0 = Avx.ConvertToVector256Int32(Avx.Multiply(Unsafe.Add(ref aBase, i + 0), Unsafe.Add(ref bBase, i + 0))); Vector256<int> row0 = Avx.ConvertToVector256Int32(Avx.Multiply(Unsafe.Add(ref aBase, i + 0), Unsafe.Add(ref bBase, i + 0)));
Vector256<int> row1 = Avx.ConvertToVector256Int32(Avx.Multiply(Unsafe.Add(ref aBase, i + 1), Unsafe.Add(ref bBase, i + 1))); Vector256<int> row1 = Avx.ConvertToVector256Int32(Avx.Multiply(Unsafe.Add(ref aBase, i + 1), Unsafe.Add(ref bBase, i + 1)));
@ -51,7 +51,7 @@ internal partial struct Block8x8F
Vector256<short> row = Avx2.PackSignedSaturate(row0, row1); Vector256<short> row = Avx2.PackSignedSaturate(row0, row1);
row = Avx2.PermuteVar8x32(row.AsInt32(), multiplyIntoInt16ShuffleMask).AsInt16(); row = Avx2.PermuteVar8x32(row.AsInt32(), multiplyIntoInt16ShuffleMask).AsInt16();
Unsafe.Add(ref destRef, (IntPtr)((uint)i / 2)) = row; Unsafe.Add(ref destRef, i / 2) = row;
} }
} }
@ -64,13 +64,13 @@ internal partial struct Block8x8F
ref Vector128<short> destBase = ref Unsafe.As<Block8x8, Vector128<short>>(ref dest); ref Vector128<short> destBase = ref Unsafe.As<Block8x8, Vector128<short>>(ref dest);
for (int i = 0; i < 16; i += 2) for (nuint i = 0; i < 16; i += 2)
{ {
Vector128<int> left = Sse2.ConvertToVector128Int32(Sse.Multiply(Unsafe.Add(ref aBase, i + 0), Unsafe.Add(ref bBase, i + 0))); Vector128<int> left = Sse2.ConvertToVector128Int32(Sse.Multiply(Unsafe.Add(ref aBase, i + 0), Unsafe.Add(ref bBase, i + 0)));
Vector128<int> right = Sse2.ConvertToVector128Int32(Sse.Multiply(Unsafe.Add(ref aBase, i + 1), Unsafe.Add(ref bBase, i + 1))); Vector128<int> right = Sse2.ConvertToVector128Int32(Sse.Multiply(Unsafe.Add(ref aBase, i + 1), Unsafe.Add(ref bBase, i + 1)));
Vector128<short> row = Sse2.PackSignedSaturate(left, right); Vector128<short> row = Sse2.PackSignedSaturate(left, right);
Unsafe.Add(ref destBase, (IntPtr)((uint)i / 2)) = row; Unsafe.Add(ref destBase, i / 2) = row;
} }
} }

36
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.ScaledCopy.cs

@ -30,13 +30,13 @@ internal partial struct Block8x8F
} }
// TODO: Optimize: implement all cases with scale-specific, loopless code! // TODO: Optimize: implement all cases with scale-specific, loopless code!
this.CopyArbitraryScale(ref areaOrigin, areaStride, horizontalScale, verticalScale); this.CopyArbitraryScale(ref areaOrigin, (uint)areaStride, (uint)horizontalScale, (uint)verticalScale);
} }
private void CopyTo2x2Scale(ref float areaOrigin, int areaStride) private void CopyTo2x2Scale(ref float areaOrigin, int areaStride)
{ {
ref Vector2 destBase = ref Unsafe.As<float, Vector2>(ref areaOrigin); ref Vector2 destBase = ref Unsafe.As<float, Vector2>(ref areaOrigin);
int destStride = (int)((uint)areaStride / 2); nuint destStride = (uint)areaStride / 2;
WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 0, destStride); WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 0, destStride);
WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 1, destStride); WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 1, destStride);
@ -48,12 +48,12 @@ internal partial struct Block8x8F
WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 7, destStride); WidenCopyRowImpl2x2(ref this.V0L, ref destBase, 7, destStride);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
static void WidenCopyRowImpl2x2(ref Vector4 selfBase, ref Vector2 destBase, nint row, nint destStride) static void WidenCopyRowImpl2x2(ref Vector4 selfBase, ref Vector2 destBase, nuint row, nuint destStride)
{ {
ref Vector4 sLeft = ref Unsafe.Add(ref selfBase, 2 * row); ref Vector4 sLeft = ref Unsafe.Add(ref selfBase, 2 * row);
ref Vector4 sRight = ref Unsafe.Add(ref sLeft, 1); ref Vector4 sRight = ref Unsafe.Add(ref sLeft, 1);
nint offset = 2 * row * destStride; nuint offset = 2 * row * destStride;
ref Vector4 dTopLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset)); ref Vector4 dTopLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset));
ref Vector4 dBottomLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset + destStride)); ref Vector4 dBottomLeft = ref Unsafe.As<Vector2, Vector4>(ref Unsafe.Add(ref destBase, offset + destStride));
@ -86,23 +86,23 @@ internal partial struct Block8x8F
} }
[MethodImpl(InliningOptions.ColdPath)] [MethodImpl(InliningOptions.ColdPath)]
private void CopyArbitraryScale(ref float areaOrigin, int areaStride, int horizontalScale, int verticalScale) private void CopyArbitraryScale(ref float areaOrigin, uint areaStride, uint horizontalScale, uint verticalScale)
{ {
for (int y = 0; y < 8; y++) for (nuint y = 0; y < 8; y++)
{ {
int yy = y * verticalScale; nuint yy = y * verticalScale;
int y8 = y * 8; nuint y8 = y * 8;
for (int x = 0; x < 8; x++) for (nuint x = 0; x < 8; x++)
{ {
int xx = x * horizontalScale; nuint xx = x * horizontalScale;
float value = this[y8 + x]; float value = this[(int)(y8 + x)];
nint baseIdx = (yy * areaStride) + xx; nuint baseIdx = (yy * areaStride) + xx;
for (nint i = 0; i < verticalScale; i++, baseIdx += areaStride) for (nuint i = 0; i < verticalScale; i++, baseIdx += areaStride)
{ {
for (nint j = 0; j < horizontalScale; j++) for (nuint j = 0; j < horizontalScale; j++)
{ {
// area[xx + j, yy + i] = value; // area[xx + j, yy + i] = value;
Unsafe.Add(ref areaOrigin, baseIdx + j) = value; Unsafe.Add(ref areaOrigin, baseIdx + j) = value;
@ -128,8 +128,8 @@ internal partial struct Block8x8F
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
static void CopyRowImpl(ref byte origin, ref byte dest, int destStride, int row) static void CopyRowImpl(ref byte origin, ref byte dest, int destStride, int row)
{ {
origin = ref Unsafe.Add(ref origin, row * 8 * sizeof(float)); origin = ref Unsafe.Add(ref origin, (uint)row * 8 * sizeof(float));
dest = ref Unsafe.Add(ref dest, row * destStride); dest = ref Unsafe.Add(ref dest, (uint)(row * destStride));
Unsafe.CopyBlock(ref dest, ref origin, 8 * sizeof(float)); Unsafe.CopyBlock(ref dest, ref origin, 8 * sizeof(float));
} }
} }
@ -150,8 +150,8 @@ internal partial struct Block8x8F
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
static void CopyRowImpl(ref byte origin, ref byte dest, int sourceStride, int row) static void CopyRowImpl(ref byte origin, ref byte dest, int sourceStride, int row)
{ {
origin = ref Unsafe.Add(ref origin, row * sourceStride); origin = ref Unsafe.Add(ref origin, (uint)(row * sourceStride));
dest = ref Unsafe.Add(ref dest, row * 8 * sizeof(float)); dest = ref Unsafe.Add(ref dest, (uint)row * 8 * sizeof(float));
Unsafe.CopyBlock(ref dest, ref origin, 8 * sizeof(float)); Unsafe.CopyBlock(ref dest, ref origin, 8 * sizeof(float));
} }
} }

22
src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs

@ -71,28 +71,34 @@ internal partial struct Block8x8F : IEquatable<Block8x8F>
/// <param name="idx">The index</param> /// <param name="idx">The index</param>
/// <returns>The float value at the specified index</returns> /// <returns>The float value at the specified index</returns>
public float this[int idx] public float this[int idx]
{
get => this[(uint)idx];
set => this[(uint)idx] = value;
}
internal float this[nuint idx]
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get get
{ {
DebugGuard.MustBeBetweenOrEqualTo(idx, 0, Size - 1, nameof(idx)); DebugGuard.MustBeBetweenOrEqualTo((int)idx, 0, Size - 1, nameof(idx));
ref float selfRef = ref Unsafe.As<Block8x8F, float>(ref this); ref float selfRef = ref Unsafe.As<Block8x8F, float>(ref this);
return Unsafe.Add(ref selfRef, (nint)(uint)idx); return Unsafe.Add(ref selfRef, idx);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
set set
{ {
DebugGuard.MustBeBetweenOrEqualTo(idx, 0, Size - 1, nameof(idx)); DebugGuard.MustBeBetweenOrEqualTo((int)idx, 0, Size - 1, nameof(idx));
ref float selfRef = ref Unsafe.As<Block8x8F, float>(ref this); ref float selfRef = ref Unsafe.As<Block8x8F, float>(ref this);
Unsafe.Add(ref selfRef, (nint)(uint)idx) = value; Unsafe.Add(ref selfRef, idx) = value;
} }
} }
public float this[int x, int y] public float this[int x, int y]
{ {
get => this[(y * 8) + x]; get => this[((uint)y * 8) + (uint)x];
set => this[(y * 8) + x] = value; set => this[((uint)y * 8) + (uint)x] = value;
} }
public static Block8x8F Load(Span<float> data) public static Block8x8F Load(Span<float> data)
@ -425,7 +431,7 @@ internal partial struct Block8x8F : IEquatable<Block8x8F>
Vector256<int> targetVector = Vector256.Create(value); Vector256<int> targetVector = Vector256.Create(value);
ref Vector256<float> blockStride = ref this.V0; ref Vector256<float> blockStride = ref this.V0;
for (int i = 0; i < RowCount; i++) for (nuint i = 0; i < RowCount; i++)
{ {
Vector256<int> areEqual = Avx2.CompareEqual(Avx.ConvertToVector256Int32WithTruncation(Unsafe.Add(ref this.V0, i)), targetVector); Vector256<int> areEqual = Avx2.CompareEqual(Avx.ConvertToVector256Int32WithTruncation(Unsafe.Add(ref this.V0, i)), targetVector);
if (Avx2.MoveMask(areEqual.AsByte()) != equalityMask) if (Avx2.MoveMask(areEqual.AsByte()) != equalityMask)
@ -439,7 +445,7 @@ internal partial struct Block8x8F : IEquatable<Block8x8F>
ref float scalars = ref Unsafe.As<Block8x8F, float>(ref this); ref float scalars = ref Unsafe.As<Block8x8F, float>(ref this);
for (int i = 0; i < Size; i++) for (nuint i = 0; i < Size; i++)
{ {
if ((int)Unsafe.Add(ref scalars, i) != value) if ((int)Unsafe.Add(ref scalars, i) != value)
{ {

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykAvx.cs

@ -32,8 +32,8 @@ internal abstract partial class JpegColorConverterBase
// Used for the color conversion // Used for the color conversion
var scale = Vector256.Create(1 / (this.MaximumValue * this.MaximumValue)); var scale = Vector256.Create(1 / (this.MaximumValue * this.MaximumValue));
nint n = values.Component0.Length / Vector256<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector256<float> c = ref Unsafe.Add(ref c0Base, i); ref Vector256<float> c = ref Unsafe.Add(ref c0Base, i);
ref Vector256<float> m = ref Unsafe.Add(ref c1Base, i); ref Vector256<float> m = ref Unsafe.Add(ref c1Base, i);
@ -71,8 +71,8 @@ internal abstract partial class JpegColorConverterBase
var scale = Vector256.Create(maxValue); var scale = Vector256.Create(maxValue);
nint n = values.Component0.Length / Vector256<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
Vector256<float> ctmp = Avx.Subtract(scale, Unsafe.Add(ref srcR, i)); Vector256<float> ctmp = Avx.Subtract(scale, Unsafe.Add(ref srcR, i));
Vector256<float> mtmp = Avx.Subtract(scale, Unsafe.Add(ref srcG, i)); Vector256<float> mtmp = Avx.Subtract(scale, Unsafe.Add(ref srcG, i));

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykVector.cs

@ -30,8 +30,8 @@ internal abstract partial class JpegColorConverterBase
var scale = new Vector<float>(1 / (this.MaximumValue * this.MaximumValue)); var scale = new Vector<float>(1 / (this.MaximumValue * this.MaximumValue));
nint n = values.Component0.Length / Vector<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector<float> c = ref Unsafe.Add(ref cBase, i); ref Vector<float> c = ref Unsafe.Add(ref cBase, i);
ref Vector<float> m = ref Unsafe.Add(ref mBase, i); ref Vector<float> m = ref Unsafe.Add(ref mBase, i);
@ -78,8 +78,8 @@ internal abstract partial class JpegColorConverterBase
// Used for the color conversion // Used for the color conversion
var scale = new Vector<float>(maxValue); var scale = new Vector<float>(maxValue);
nint n = values.Component0.Length / Vector<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
Vector<float> ctmp = scale - Unsafe.Add(ref srcR, i); Vector<float> ctmp = scale - Unsafe.Add(ref srcR, i);
Vector<float> mtmp = scale - Unsafe.Add(ref srcG, i); Vector<float> mtmp = scale - Unsafe.Add(ref srcG, i);

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleAvx.cs

@ -27,8 +27,8 @@ internal abstract partial class JpegColorConverterBase
// Used for the color conversion // Used for the color conversion
var scale = Vector256.Create(1 / this.MaximumValue); var scale = Vector256.Create(1 / this.MaximumValue);
nint n = values.Component0.Length / Vector256<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector256<float> c0 = ref Unsafe.Add(ref c0Base, i); ref Vector256<float> c0 = ref Unsafe.Add(ref c0Base, i);
c0 = Avx.Multiply(c0, scale); c0 = Avx.Multiply(c0, scale);
@ -53,8 +53,8 @@ internal abstract partial class JpegColorConverterBase
var f0587 = Vector256.Create(0.587f); var f0587 = Vector256.Create(0.587f);
var f0114 = Vector256.Create(0.114f); var f0114 = Vector256.Create(0.114f);
nint n = values.Component0.Length / Vector256<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector256<float> r = ref Unsafe.Add(ref srcRed, i); ref Vector256<float> r = ref Unsafe.Add(ref srcRed, i);
ref Vector256<float> g = ref Unsafe.Add(ref srcGreen, i); ref Vector256<float> g = ref Unsafe.Add(ref srcGreen, i);

2
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleScalar.cs

@ -28,7 +28,7 @@ internal abstract partial class JpegColorConverterBase
ref float valuesRef = ref MemoryMarshal.GetReference(values); ref float valuesRef = ref MemoryMarshal.GetReference(values);
float scale = 1 / maxValue; float scale = 1 / maxValue;
for (nint i = 0; i < values.Length; i++) for (nuint i = 0; i < (uint)values.Length; i++)
{ {
Unsafe.Add(ref valuesRef, i) *= scale; Unsafe.Add(ref valuesRef, i) *= scale;
} }

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.GrayScaleVector.cs

@ -24,8 +24,8 @@ internal abstract partial class JpegColorConverterBase
var scale = new Vector<float>(1 / this.MaximumValue); var scale = new Vector<float>(1 / this.MaximumValue);
nint n = values.Component0.Length / Vector<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector<float> c0 = ref Unsafe.Add(ref cBase, i); ref Vector<float> c0 = ref Unsafe.Add(ref cBase, i);
c0 *= scale; c0 *= scale;
@ -53,8 +53,8 @@ internal abstract partial class JpegColorConverterBase
var gMult = new Vector<float>(0.587f); var gMult = new Vector<float>(0.587f);
var bMult = new Vector<float>(0.114f); var bMult = new Vector<float>(0.114f);
nint n = values.Component0.Length / Vector<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
Vector<float> r = Unsafe.Add(ref srcR, i); Vector<float> r = Unsafe.Add(ref srcR, i);
Vector<float> g = Unsafe.Add(ref srcR, i); Vector<float> g = Unsafe.Add(ref srcR, i);

4
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.RgbArm.cs

@ -30,8 +30,8 @@ internal abstract partial class JpegColorConverterBase
// Used for the color conversion // Used for the color conversion
var scale = Vector128.Create(1 / this.MaximumValue); var scale = Vector128.Create(1 / this.MaximumValue);
nint n = (nint)(uint)values.Component0.Length / Vector128<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector128<float> r = ref Unsafe.Add(ref rBase, i); ref Vector128<float> r = ref Unsafe.Add(ref rBase, i);
ref Vector128<float> g = ref Unsafe.Add(ref gBase, i); ref Vector128<float> g = ref Unsafe.Add(ref gBase, i);

4
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.RgbAvx.cs

@ -29,8 +29,8 @@ internal abstract partial class JpegColorConverterBase
// Used for the color conversion // Used for the color conversion
var scale = Vector256.Create(1 / this.MaximumValue); var scale = Vector256.Create(1 / this.MaximumValue);
nint n = values.Component0.Length / Vector256<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector256<float> r = ref Unsafe.Add(ref rBase, i); ref Vector256<float> r = ref Unsafe.Add(ref rBase, i);
ref Vector256<float> g = ref Unsafe.Add(ref gBase, i); ref Vector256<float> g = ref Unsafe.Add(ref gBase, i);

4
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.RgbVector.cs

@ -28,8 +28,8 @@ internal abstract partial class JpegColorConverterBase
var scale = new Vector<float>(1 / this.MaximumValue); var scale = new Vector<float>(1 / this.MaximumValue);
nint n = values.Component0.Length / Vector<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
ref Vector<float> r = ref Unsafe.Add(ref rBase, i); ref Vector<float> r = ref Unsafe.Add(ref rBase, i);
ref Vector<float> g = ref Unsafe.Add(ref gBase, i); ref Vector<float> g = ref Unsafe.Add(ref gBase, i);

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrAvx.cs

@ -38,8 +38,8 @@ internal abstract partial class JpegColorConverterBase
var bCbMult = Vector256.Create(YCbCrScalar.BCbMult); var bCbMult = Vector256.Create(YCbCrScalar.BCbMult);
// Walking 8 elements at one step: // Walking 8 elements at one step:
nint n = values.Component0.Length / Vector256<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
// y = yVals[i]; // y = yVals[i];
// cb = cbVals[i] - 128F; // cb = cbVals[i] - 128F;
@ -98,8 +98,8 @@ internal abstract partial class JpegColorConverterBase
var fn0081312F = Vector256.Create(-0.081312F); var fn0081312F = Vector256.Create(-0.081312F);
var f05 = Vector256.Create(0.5f); var f05 = Vector256.Create(0.5f);
nint n = values.Component0.Length / Vector256<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
Vector256<float> r = Unsafe.Add(ref srcR, i); Vector256<float> r = Unsafe.Add(ref srcR, i);
Vector256<float> g = Unsafe.Add(ref srcG, i); Vector256<float> g = Unsafe.Add(ref srcG, i);

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YCbCrVector.cs

@ -35,8 +35,8 @@ internal abstract partial class JpegColorConverterBase
var gCrMult = new Vector<float>(-YCbCrScalar.GCrMult); var gCrMult = new Vector<float>(-YCbCrScalar.GCrMult);
var bCbMult = new Vector<float>(YCbCrScalar.BCbMult); var bCbMult = new Vector<float>(YCbCrScalar.BCbMult);
nint n = values.Component0.Length / Vector<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
// y = yVals[i]; // y = yVals[i];
// cb = cbVals[i] - 128F; // cb = cbVals[i] - 128F;
@ -103,8 +103,8 @@ internal abstract partial class JpegColorConverterBase
var gCrMult = new Vector<float>(0.418688f); var gCrMult = new Vector<float>(0.418688f);
var bCrMult = new Vector<float>(0.081312f); var bCrMult = new Vector<float>(0.081312f);
nint n = values.Component0.Length / Vector<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
Vector<float> r = Unsafe.Add(ref srcR, i); Vector<float> r = Unsafe.Add(ref srcR, i);
Vector<float> g = Unsafe.Add(ref srcG, i); Vector<float> g = Unsafe.Add(ref srcG, i);

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKAvx.cs

@ -40,8 +40,8 @@ internal abstract partial class JpegColorConverterBase
var bCbMult = Vector256.Create(YCbCrScalar.BCbMult); var bCbMult = Vector256.Create(YCbCrScalar.BCbMult);
// Walking 8 elements at one step: // Walking 8 elements at one step:
nint n = values.Component0.Length / Vector256<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
// y = yVals[i]; // y = yVals[i];
// cb = cbVals[i] - 128F; // cb = cbVals[i] - 128F;
@ -109,8 +109,8 @@ internal abstract partial class JpegColorConverterBase
var fn0081312F = Vector256.Create(-0.081312F); var fn0081312F = Vector256.Create(-0.081312F);
var f05 = Vector256.Create(0.5f); var f05 = Vector256.Create(0.5f);
nint n = values.Component0.Length / Vector256<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
Vector256<float> r = Avx.Subtract(maxSampleValue, Unsafe.Add(ref srcR, i)); Vector256<float> r = Avx.Subtract(maxSampleValue, Unsafe.Add(ref srcR, i));
Vector256<float> g = Avx.Subtract(maxSampleValue, Unsafe.Add(ref srcG, i)); Vector256<float> g = Avx.Subtract(maxSampleValue, Unsafe.Add(ref srcG, i));

8
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.YccKVector.cs

@ -36,8 +36,8 @@ internal abstract partial class JpegColorConverterBase
var gCrMult = new Vector<float>(-YCbCrScalar.GCrMult); var gCrMult = new Vector<float>(-YCbCrScalar.GCrMult);
var bCbMult = new Vector<float>(YCbCrScalar.BCbMult); var bCbMult = new Vector<float>(YCbCrScalar.BCbMult);
nint n = values.Component0.Length / Vector<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
// y = yVals[i]; // y = yVals[i];
// cb = cbVals[i] - 128F; // cb = cbVals[i] - 128F;
@ -107,8 +107,8 @@ internal abstract partial class JpegColorConverterBase
var gCrMult = new Vector<float>(0.418688f); var gCrMult = new Vector<float>(0.418688f);
var bCrMult = new Vector<float>(0.081312f); var bCrMult = new Vector<float>(0.081312f);
nint n = values.Component0.Length / Vector<float>.Count; nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < n; i++) for (nuint i = 0; i < n; i++)
{ {
Vector<float> r = maxSampleValue - Unsafe.Add(ref srcR, i); Vector<float> r = maxSampleValue - Unsafe.Add(ref srcR, i);
Vector<float> g = maxSampleValue - Unsafe.Add(ref srcG, i); Vector<float> g = maxSampleValue - Unsafe.Add(ref srcG, i);

2
src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterBase.cs

@ -26,7 +26,7 @@ internal abstract partial class JpegColorConverterBase
this.ColorSpace = colorSpace; this.ColorSpace = colorSpace;
this.Precision = precision; this.Precision = precision;
this.MaximumValue = MathF.Pow(2, precision) - 1; this.MaximumValue = MathF.Pow(2, precision) - 1;
this.HalfValue = MathF.Ceiling(this.MaximumValue / 2); this.HalfValue = MathF.Ceiling(this.MaximumValue * 0.5F); // /2
} }
/// <summary> /// <summary>

30
src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs

@ -231,7 +231,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
} }
} }
private ref byte GetFixedBinReference() => ref this.fixedBin[0]; private ref byte GetFixedBinReference() => ref MemoryMarshal.GetArrayDataReference(this.fixedBin);
/// <summary> /// <summary>
/// Decodes the entropy coded data. /// Decodes the entropy coded data.
@ -470,7 +470,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
this.DecodeBlockBaseline( this.DecodeBlockBaseline(
component, component,
ref Unsafe.Add(ref blockRef, (nint)(uint)blockCol), ref Unsafe.Add(ref blockRef, (uint)blockCol),
ref acDecodingTable, ref acDecodingTable,
ref dcDecodingTable); ref dcDecodingTable);
} }
@ -521,7 +521,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
this.DecodeBlockBaseline( this.DecodeBlockBaseline(
component, component,
ref Unsafe.Add(ref blockRef, (nint)(uint)k), ref Unsafe.Add(ref blockRef, (uint)k),
ref acDecodingTable, ref acDecodingTable,
ref dcDecodingTable); ref dcDecodingTable);
@ -560,7 +560,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
this.DecodeBlockBaseline( this.DecodeBlockBaseline(
component, component,
ref Unsafe.Add(ref blockRef, (nint)(uint)i), ref Unsafe.Add(ref blockRef, (uint)i),
ref acDecodingTable, ref acDecodingTable,
ref dcDecodingTable); ref dcDecodingTable);
@ -611,7 +611,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
this.DecodeBlockProgressiveDc( this.DecodeBlockProgressiveDc(
component, component,
ref Unsafe.Add(ref blockRef, (nint)(uint)blockCol), ref Unsafe.Add(ref blockRef, (uint)blockCol),
ref dcDecodingTable); ref dcDecodingTable);
} }
} }
@ -653,7 +653,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
this.DecodeBlockProgressiveDc( this.DecodeBlockProgressiveDc(
component, component,
ref Unsafe.Add(ref blockRef, (nint)(uint)i), ref Unsafe.Add(ref blockRef, (uint)i),
ref dcDecodingTable); ref dcDecodingTable);
this.HandleRestart(); this.HandleRestart();
@ -680,7 +680,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
this.DecodeBlockProgressiveAc( this.DecodeBlockProgressiveAc(
component, component,
ref Unsafe.Add(ref blockRef, (nint)(uint)i), ref Unsafe.Add(ref blockRef, (uint)i),
ref acDecodingTable); ref acDecodingTable);
this.HandleRestart(); this.HandleRestart();
@ -705,7 +705,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
// Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients. // Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients.
// Table F.4: Point to statistics bin S0 for DC coefficient coding. // Table F.4: Point to statistics bin S0 for DC coefficient coding.
ref byte st = ref Unsafe.Add(ref component.DcStatistics.GetReference(), component.DcContext); ref byte st = ref Unsafe.Add(ref component.DcStatistics.GetReference(), (uint)component.DcContext);
// Figure F.19: Decode_DC_DIFF // Figure F.19: Decode_DC_DIFF
if (this.DecodeBinaryDecision(ref reader, ref st) == 0) if (this.DecodeBinaryDecision(ref reader, ref st) == 0)
@ -717,7 +717,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
// Figure F.21: Decoding nonzero value v. // Figure F.21: Decoding nonzero value v.
// Figure F.22: Decoding the sign of v. // Figure F.22: Decoding the sign of v.
int sign = this.DecodeBinaryDecision(ref reader, ref Unsafe.Add(ref st, 1)); int sign = this.DecodeBinaryDecision(ref reader, ref Unsafe.Add(ref st, 1));
st = ref Unsafe.Add(ref st, (nint)(uint)(2 + sign)); st = ref Unsafe.Add(ref st, (uint)(2 + sign));
// Figure F.23: Decoding the magnitude category of v. // Figure F.23: Decoding the magnitude category of v.
int m = this.DecodeBinaryDecision(ref reader, ref st); int m = this.DecodeBinaryDecision(ref reader, ref st);
@ -761,7 +761,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
} }
} }
v += 1; v++;
if (sign != 0) if (sign != 0)
{ {
v = -v; v = -v;
@ -856,7 +856,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
} }
} }
v += 1; v++;
if (sign != 0) if (sign != 0)
{ {
v = -v; v = -v;
@ -955,7 +955,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
// Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients. // Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients.
// Table F.4: Point to statistics bin S0 for DC coefficient coding. // Table F.4: Point to statistics bin S0 for DC coefficient coding.
ref byte st = ref Unsafe.Add(ref component.DcStatistics.GetReference(), component.DcContext); ref byte st = ref Unsafe.Add(ref component.DcStatistics.GetReference(), (uint)component.DcContext);
/* Figure F.19: Decode_DC_DIFF */ /* Figure F.19: Decode_DC_DIFF */
if (this.DecodeBinaryDecision(ref reader, ref st) == 0) if (this.DecodeBinaryDecision(ref reader, ref st) == 0)
@ -967,7 +967,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
// Figure F.21: Decoding nonzero value v // Figure F.21: Decoding nonzero value v
// Figure F.22: Decoding the sign of v // Figure F.22: Decoding the sign of v
int sign = this.DecodeBinaryDecision(ref reader, ref Unsafe.Add(ref st, 1)); int sign = this.DecodeBinaryDecision(ref reader, ref Unsafe.Add(ref st, 1));
st = ref Unsafe.Add(ref st, (nint)(uint)(2 + sign)); st = ref Unsafe.Add(ref st, (uint)(2 + sign));
// Figure F.23: Decoding the magnitude category of v. // Figure F.23: Decoding the magnitude category of v.
int m = this.DecodeBinaryDecision(ref reader, ref st); int m = this.DecodeBinaryDecision(ref reader, ref st);
@ -1012,7 +1012,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
} }
} }
v += 1; v++;
if (sign != 0) if (sign != 0)
{ {
v = -v; v = -v;
@ -1082,7 +1082,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder
} }
} }
v += 1; v++;
if (sign != 0) if (sign != 0)
{ {
v = -v; v = -v;

4
src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticStatistics.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
internal class ArithmeticStatistics internal class ArithmeticStatistics
@ -18,7 +20,7 @@ internal class ArithmeticStatistics
public int Identifier { get; private set; } public int Identifier { get; private set; }
public ref byte GetReference() => ref this.statistics[0]; public ref byte GetReference() => ref MemoryMarshal.GetArrayDataReference(this.statistics);
public ref byte GetReference(int offset) => ref this.statistics[offset]; public ref byte GetReference(int offset) => ref this.statistics[offset];

24
src/ImageSharp/Formats/Jpeg/Components/Decoder/ComponentProcessors/DownScalingComponentProcessor2.cs

@ -25,7 +25,7 @@ internal sealed class DownScalingComponentProcessor2 : ComponentProcessor
Buffer2D<Block8x8> spectralBuffer = this.Component.SpectralBlocks; Buffer2D<Block8x8> spectralBuffer = this.Component.SpectralBlocks;
float maximumValue = this.Frame.MaxColorChannelValue; float maximumValue = this.Frame.MaxColorChannelValue;
float normalizationValue = MathF.Ceiling(maximumValue / 2); float normalizationValue = MathF.Ceiling(maximumValue * 0.5F);
int destAreaStride = this.ColorBuffer.Width; int destAreaStride = this.ColorBuffer.Width;
@ -67,30 +67,30 @@ internal sealed class DownScalingComponentProcessor2 : ComponentProcessor
public static void ScaledCopyTo(ref Block8x8F block, ref float destRef, int destStrideWidth, int horizontalScale, int verticalScale) public static void ScaledCopyTo(ref Block8x8F block, ref float destRef, int destStrideWidth, int horizontalScale, int verticalScale)
{ {
// TODO: Optimize: implement all cases with scale-specific, loopless code! // TODO: Optimize: implement all cases with scale-specific, loopless code!
CopyArbitraryScale(ref block, ref destRef, destStrideWidth, horizontalScale, verticalScale); CopyArbitraryScale(ref block, ref destRef, (uint)destStrideWidth, (uint)horizontalScale, (uint)verticalScale);
[MethodImpl(InliningOptions.ColdPath)] [MethodImpl(InliningOptions.ColdPath)]
static void CopyArbitraryScale(ref Block8x8F block, ref float areaOrigin, int areaStride, int horizontalScale, int verticalScale) static void CopyArbitraryScale(ref Block8x8F block, ref float areaOrigin, uint areaStride, uint horizontalScale, uint verticalScale)
{ {
for (int y = 0; y < 4; y++) for (nuint y = 0; y < 4; y++)
{ {
int yy = y * verticalScale; nuint yy = y * verticalScale;
int y8 = y * 8; nuint y8 = y * 8;
for (int x = 0; x < 4; x++) for (nuint x = 0; x < 4; x++)
{ {
int xx = x * horizontalScale; nuint xx = x * horizontalScale;
float value = block[y8 + x]; float value = block[y8 + x];
for (int i = 0; i < verticalScale; i++) for (nuint i = 0; i < verticalScale; i++)
{ {
int baseIdx = ((yy + i) * areaStride) + xx; nuint baseIdx = ((yy + i) * areaStride) + xx;
for (int j = 0; j < horizontalScale; j++) for (nuint j = 0; j < horizontalScale; j++)
{ {
// area[xx + j, yy + i] = value; // area[xx + j, yy + i] = value;
Unsafe.Add(ref areaOrigin, (nint)(uint)(baseIdx + j)) = value; Unsafe.Add(ref areaOrigin, baseIdx + j) = value;
} }
} }
} }

24
src/ImageSharp/Formats/Jpeg/Components/Decoder/ComponentProcessors/DownScalingComponentProcessor4.cs

@ -25,7 +25,7 @@ internal sealed class DownScalingComponentProcessor4 : ComponentProcessor
Buffer2D<Block8x8> spectralBuffer = this.Component.SpectralBlocks; Buffer2D<Block8x8> spectralBuffer = this.Component.SpectralBlocks;
float maximumValue = this.Frame.MaxColorChannelValue; float maximumValue = this.Frame.MaxColorChannelValue;
float normalizationValue = MathF.Ceiling(maximumValue / 2); float normalizationValue = MathF.Ceiling(maximumValue * 0.5F);
int destAreaStride = this.ColorBuffer.Width; int destAreaStride = this.ColorBuffer.Width;
@ -67,30 +67,30 @@ internal sealed class DownScalingComponentProcessor4 : ComponentProcessor
public static void ScaledCopyTo(ref Block8x8F block, ref float destRef, int destStrideWidth, int horizontalScale, int verticalScale) public static void ScaledCopyTo(ref Block8x8F block, ref float destRef, int destStrideWidth, int horizontalScale, int verticalScale)
{ {
// TODO: Optimize: implement all cases with scale-specific, loopless code! // TODO: Optimize: implement all cases with scale-specific, loopless code!
CopyArbitraryScale(ref block, ref destRef, destStrideWidth, horizontalScale, verticalScale); CopyArbitraryScale(ref block, ref destRef, (uint)destStrideWidth, (uint)horizontalScale, (uint)verticalScale);
[MethodImpl(InliningOptions.ColdPath)] [MethodImpl(InliningOptions.ColdPath)]
static void CopyArbitraryScale(ref Block8x8F block, ref float areaOrigin, int areaStride, int horizontalScale, int verticalScale) static void CopyArbitraryScale(ref Block8x8F block, ref float areaOrigin, uint areaStride, uint horizontalScale, uint verticalScale)
{ {
for (int y = 0; y < 2; y++) for (nuint y = 0; y < 2; y++)
{ {
int yy = y * verticalScale; nuint yy = y * verticalScale;
int y8 = y * 8; nuint y8 = y * 8;
for (int x = 0; x < 2; x++) for (nuint x = 0; x < 2; x++)
{ {
int xx = x * horizontalScale; nuint xx = x * horizontalScale;
float value = block[y8 + x]; float value = block[y8 + x];
for (int i = 0; i < verticalScale; i++) for (nuint i = 0; i < verticalScale; i++)
{ {
int baseIdx = ((yy + i) * areaStride) + xx; nuint baseIdx = ((yy + i) * areaStride) + xx;
for (int j = 0; j < horizontalScale; j++) for (nuint j = 0; j < horizontalScale; j++)
{ {
// area[xx + j, yy + i] = value; // area[xx + j, yy + i] = value;
Unsafe.Add(ref areaOrigin, (nint)(uint)(baseIdx + j)) = value; Unsafe.Add(ref areaOrigin, baseIdx + j) = value;
} }
} }
} }

14
src/ImageSharp/Formats/Jpeg/Components/Decoder/ComponentProcessors/DownScalingComponentProcessor8.cs

@ -22,7 +22,7 @@ internal sealed class DownScalingComponentProcessor8 : ComponentProcessor
Buffer2D<Block8x8> spectralBuffer = this.Component.SpectralBlocks; Buffer2D<Block8x8> spectralBuffer = this.Component.SpectralBlocks;
float maximumValue = this.Frame.MaxColorChannelValue; float maximumValue = this.Frame.MaxColorChannelValue;
float normalizationValue = MathF.Ceiling(maximumValue / 2); float normalizationValue = MathF.Ceiling(maximumValue * 0.5F);
int destAreaStride = this.ColorBuffer.Width; int destAreaStride = this.ColorBuffer.Width;
@ -67,20 +67,20 @@ internal sealed class DownScalingComponentProcessor8 : ComponentProcessor
{ {
destRef = value; destRef = value;
Unsafe.Add(ref destRef, 1) = value; Unsafe.Add(ref destRef, 1) = value;
Unsafe.Add(ref destRef, 0 + (nint)(uint)destStrideWidth) = value; Unsafe.Add(ref destRef, 0 + (uint)destStrideWidth) = value;
Unsafe.Add(ref destRef, 1 + (nint)(uint)destStrideWidth) = value; Unsafe.Add(ref destRef, 1 + (uint)destStrideWidth) = value;
return; return;
} }
// TODO: Optimize: implement all cases with scale-specific, loopless code! // TODO: Optimize: implement all cases with scale-specific, loopless code!
for (int y = 0; y < verticalScale; y++) for (nuint y = 0; y < (uint)verticalScale; y++)
{ {
for (int x = 0; x < horizontalScale; x++) for (nuint x = 0; x < (uint)horizontalScale; x++)
{ {
Unsafe.Add(ref destRef, (nint)(uint)x) = value; Unsafe.Add(ref destRef, x) = value;
} }
destRef = ref Unsafe.Add(ref destRef, (nint)(uint)destStrideWidth); destRef = ref Unsafe.Add(ref destRef, (uint)destStrideWidth);
} }
} }
} }

12
src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanDecoder.cs

@ -211,7 +211,7 @@ internal class HuffmanScanDecoder : IJpegScanDecoder
this.DecodeBlockBaseline( this.DecodeBlockBaseline(
component, component,
ref Unsafe.Add(ref blockRef, blockCol), ref Unsafe.Add(ref blockRef, (uint)blockCol),
ref dcHuffmanTable, ref dcHuffmanTable,
ref acHuffmanTable); ref acHuffmanTable);
} }
@ -255,7 +255,7 @@ internal class HuffmanScanDecoder : IJpegScanDecoder
this.DecodeBlockBaseline( this.DecodeBlockBaseline(
component, component,
ref Unsafe.Add(ref blockRef, i), ref Unsafe.Add(ref blockRef, (uint)i),
ref dcHuffmanTable, ref dcHuffmanTable,
ref acHuffmanTable); ref acHuffmanTable);
@ -297,7 +297,7 @@ internal class HuffmanScanDecoder : IJpegScanDecoder
this.DecodeBlockBaseline( this.DecodeBlockBaseline(
component, component,
ref Unsafe.Add(ref blockRef, k), ref Unsafe.Add(ref blockRef, (uint)k),
ref dcHuffmanTable, ref dcHuffmanTable,
ref acHuffmanTable); ref acHuffmanTable);
@ -417,7 +417,7 @@ internal class HuffmanScanDecoder : IJpegScanDecoder
this.DecodeBlockProgressiveDC( this.DecodeBlockProgressiveDC(
component, component,
ref Unsafe.Add(ref blockRef, blockCol), ref Unsafe.Add(ref blockRef, (uint)blockCol),
ref dcHuffmanTable); ref dcHuffmanTable);
} }
} }
@ -459,7 +459,7 @@ internal class HuffmanScanDecoder : IJpegScanDecoder
this.DecodeBlockProgressiveDC( this.DecodeBlockProgressiveDC(
component, component,
ref Unsafe.Add(ref blockRef, i), ref Unsafe.Add(ref blockRef, (uint)i),
ref dcHuffmanTable); ref dcHuffmanTable);
this.HandleRestart(); this.HandleRestart();
@ -485,7 +485,7 @@ internal class HuffmanScanDecoder : IJpegScanDecoder
} }
this.DecodeBlockProgressiveAC( this.DecodeBlockProgressiveAC(
ref Unsafe.Add(ref blockRef, i), ref Unsafe.Add(ref blockRef, (uint)i),
ref acHuffmanTable); ref acHuffmanTable);
this.HandleRestart(); this.HandleRestart();

26
src/ImageSharp/Formats/Jpeg/Components/Encoder/ComponentProcessor.cs

@ -122,8 +122,8 @@ internal class ComponentProcessor : IDisposable
ref Vector256<float> sourceVectorRef = ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(source)); ref Vector256<float> sourceVectorRef = ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(source));
// Spans are guaranteed to be multiple of 8 so no extra 'remainder' steps are needed // Spans are guaranteed to be multiple of 8 so no extra 'remainder' steps are needed
nint count = source.Length / Vector256<float>.Count; nuint count = (uint)source.Length / (uint)Vector256<float>.Count;
for (nint i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
Unsafe.Add(ref targetVectorRef, i) = Avx.Add(Unsafe.Add(ref targetVectorRef, i), Unsafe.Add(ref sourceVectorRef, i)); Unsafe.Add(ref targetVectorRef, i) = Avx.Add(Unsafe.Add(ref targetVectorRef, i), Unsafe.Add(ref sourceVectorRef, i));
} }
@ -133,15 +133,15 @@ internal class ComponentProcessor : IDisposable
ref Vector<float> targetVectorRef = ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(target)); ref Vector<float> targetVectorRef = ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(target));
ref Vector<float> sourceVectorRef = ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(source)); ref Vector<float> sourceVectorRef = ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(source));
nint count = source.Length / Vector<float>.Count; nuint count = (uint)source.Length / (uint)Vector<float>.Count;
for (nint i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
Unsafe.Add(ref targetVectorRef, i) += Unsafe.Add(ref sourceVectorRef, i); Unsafe.Add(ref targetVectorRef, i) += Unsafe.Add(ref sourceVectorRef, i);
} }
ref float targetRef = ref MemoryMarshal.GetReference(target); ref float targetRef = ref MemoryMarshal.GetReference(target);
ref float sourceRef = ref MemoryMarshal.GetReference(source); ref float sourceRef = ref MemoryMarshal.GetReference(source);
for (nint i = count * Vector<float>.Count; i < source.Length; i++) for (nuint i = count * (uint)Vector<float>.Count; i < (uint)source.Length; i++)
{ {
Unsafe.Add(ref targetRef, i) += Unsafe.Add(ref sourceRef, i); Unsafe.Add(ref targetRef, i) += Unsafe.Add(ref sourceRef, i);
} }
@ -157,7 +157,7 @@ internal class ComponentProcessor : IDisposable
// Ideally we need to use log2: Numerics.Log2((uint)factor) // Ideally we need to use log2: Numerics.Log2((uint)factor)
// but division by 2 works just fine in this case // but division by 2 works just fine in this case
int haddIterationsCount = (int)((uint)factor / 2); uint haddIterationsCount = (uint)factor / 2;
// Transform spans so that it only contains 'remainder' // Transform spans so that it only contains 'remainder'
// values for the scalar fallback code // values for the scalar fallback code
@ -166,9 +166,9 @@ internal class ComponentProcessor : IDisposable
source = source.Slice(touchedCount); source = source.Slice(touchedCount);
target = target.Slice(touchedCount / factor); target = target.Slice(touchedCount / factor);
uint length = (uint)touchedCount / (uint)Vector256<float>.Count; nuint length = (uint)touchedCount / (uint)Vector256<float>.Count;
for (int i = 0; i < haddIterationsCount; i++) for (uint i = 0; i < haddIterationsCount; i++)
{ {
length /= 2; length /= 2;
@ -200,9 +200,9 @@ internal class ComponentProcessor : IDisposable
ref Vector256<float> targetVectorRef = ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(target)); ref Vector256<float> targetVectorRef = ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(target));
// Spans are guaranteed to be multiple of 8 so no extra 'remainder' steps are needed // Spans are guaranteed to be multiple of 8 so no extra 'remainder' steps are needed
nint count = target.Length / Vector256<float>.Count; nuint count = (uint)target.Length / (uint)Vector256<float>.Count;
var multiplierVector = Vector256.Create(multiplier); var multiplierVector = Vector256.Create(multiplier);
for (nint i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
Unsafe.Add(ref targetVectorRef, i) = Avx.Multiply(Unsafe.Add(ref targetVectorRef, i), multiplierVector); Unsafe.Add(ref targetVectorRef, i) = Avx.Multiply(Unsafe.Add(ref targetVectorRef, i), multiplierVector);
} }
@ -211,15 +211,15 @@ internal class ComponentProcessor : IDisposable
{ {
ref Vector<float> targetVectorRef = ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(target)); ref Vector<float> targetVectorRef = ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(target));
nint count = target.Length / Vector<float>.Count; nuint count = (uint)target.Length / (uint)Vector<float>.Count;
var multiplierVector = new Vector<float>(multiplier); var multiplierVector = new Vector<float>(multiplier);
for (nint i = 0; i < count; i++) for (nuint i = 0; i < count; i++)
{ {
Unsafe.Add(ref targetVectorRef, i) *= multiplierVector; Unsafe.Add(ref targetVectorRef, i) *= multiplierVector;
} }
ref float targetRef = ref MemoryMarshal.GetReference(target); ref float targetRef = ref MemoryMarshal.GetReference(target);
for (nint i = count * Vector<float>.Count; i < target.Length; i++) for (nuint i = count * (uint)Vector<float>.Count; i < (uint)target.Length; i++)
{ {
Unsafe.Add(ref targetRef, i) *= multiplier; Unsafe.Add(ref targetRef, i) *= multiplier;
} }

34
src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs

@ -123,7 +123,7 @@ internal class HuffmanScanEncoder
private bool IsStreamFlushNeeded private bool IsStreamFlushNeeded
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.emitWriteIndex < (uint)this.emitBuffer.Length / 2; get => this.emitWriteIndex < (int)((uint)this.emitBuffer.Length / 2);
} }
public void BuildHuffmanTable(JpegHuffmanTableConfig tableConfig) public void BuildHuffmanTable(JpegHuffmanTableConfig tableConfig)
@ -180,7 +180,7 @@ internal class HuffmanScanEncoder
Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y: 0); Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y: 0);
ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan); ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan);
for (nint k = 0; k < w; k++) for (nuint k = 0; k < (uint)w; k++)
{ {
this.WriteBlock( this.WriteBlock(
component, component,
@ -219,7 +219,7 @@ internal class HuffmanScanEncoder
Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y: i); Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y: i);
ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan); ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan);
for (nint k = 0; k < w; k++) for (nuint k = 0; k < (uint)w; k++)
{ {
this.WriteBlock( this.WriteBlock(
component, component,
@ -246,9 +246,9 @@ internal class HuffmanScanEncoder
private void EncodeScanBaselineInterleaved<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken) private void EncodeScanBaselineInterleaved<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
nint mcu = 0; int mcu = 0;
nint mcusPerColumn = frame.McusPerColumn; int mcusPerColumn = frame.McusPerColumn;
nint mcusPerLine = frame.McusPerLine; int mcusPerLine = frame.McusPerLine;
for (int j = 0; j < mcusPerColumn; j++) for (int j = 0; j < mcusPerColumn; j++)
{ {
@ -258,21 +258,21 @@ internal class HuffmanScanEncoder
converter.ConvertStrideBaseline(); converter.ConvertStrideBaseline();
// Encode spectral to binary // Encode spectral to binary
for (nint i = 0; i < mcusPerLine; i++) for (int i = 0; i < mcusPerLine; i++)
{ {
// Scan an interleaved mcu... process components in order // Scan an interleaved mcu... process components in order
nint mcuCol = mcu % mcusPerLine; int mcuCol = mcu % mcusPerLine;
for (nint k = 0; k < frame.Components.Length; k++) for (int k = 0; k < frame.Components.Length; k++)
{ {
Component component = frame.Components[k]; Component component = frame.Components[k];
ref HuffmanLut dcHuffmanTable = ref this.dcHuffmanTables[component.DcTableId]; ref HuffmanLut dcHuffmanTable = ref this.dcHuffmanTables[component.DcTableId];
ref HuffmanLut acHuffmanTable = ref this.acHuffmanTables[component.AcTableId]; ref HuffmanLut acHuffmanTable = ref this.acHuffmanTables[component.AcTableId];
nint h = component.HorizontalSamplingFactor; int h = component.HorizontalSamplingFactor;
int v = component.VerticalSamplingFactor; int v = component.VerticalSamplingFactor;
nint blockColBase = mcuCol * h; nuint blockColBase = (uint)(mcuCol * h);
// Scan out an mcu's worth of this component; that's just determined // Scan out an mcu's worth of this component; that's just determined
// by the basic H and V specified for the component // by the basic H and V specified for the component
@ -281,9 +281,9 @@ internal class HuffmanScanEncoder
Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y); Span<Block8x8> blockSpan = component.SpectralBlocks.DangerousGetRowSpan(y);
ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan); ref Block8x8 blockRef = ref MemoryMarshal.GetReference(blockSpan);
for (nint x = 0; x < h; x++) for (nuint x = 0; x < (uint)h; x++)
{ {
nint blockCol = blockColBase + x; nuint blockCol = blockColBase + x;
this.WriteBlock( this.WriteBlock(
component, component,
@ -315,8 +315,8 @@ internal class HuffmanScanEncoder
private void EncodeThreeComponentBaselineInterleavedScanNoSubsampling<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken) private void EncodeThreeComponentBaselineInterleavedScanNoSubsampling<TPixel>(JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
nint mcusPerColumn = frame.McusPerColumn; nuint mcusPerColumn = (uint)frame.McusPerColumn;
nint mcusPerLine = frame.McusPerLine; nuint mcusPerLine = (uint)frame.McusPerLine;
Component c2 = frame.Components[2]; Component c2 = frame.Components[2];
Component c1 = frame.Components[1]; Component c1 = frame.Components[1];
@ -333,7 +333,7 @@ internal class HuffmanScanEncoder
ref Block8x8 c1BlockRef = ref MemoryMarshal.GetReference(c1.SpectralBlocks.DangerousGetRowSpan(y: 0)); ref Block8x8 c1BlockRef = ref MemoryMarshal.GetReference(c1.SpectralBlocks.DangerousGetRowSpan(y: 0));
ref Block8x8 c2BlockRef = ref MemoryMarshal.GetReference(c2.SpectralBlocks.DangerousGetRowSpan(y: 0)); ref Block8x8 c2BlockRef = ref MemoryMarshal.GetReference(c2.SpectralBlocks.DangerousGetRowSpan(y: 0));
for (nint j = 0; j < mcusPerColumn; j++) for (nuint j = 0; j < mcusPerColumn; j++)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@ -341,7 +341,7 @@ internal class HuffmanScanEncoder
converter.ConvertStrideBaseline(); converter.ConvertStrideBaseline();
// Encode spectral to binary // Encode spectral to binary
for (nint i = 0; i < mcusPerLine; i++) for (nuint i = 0; i < mcusPerLine; i++)
{ {
this.WriteBlock( this.WriteBlock(
c0, c0,

4
src/ImageSharp/Formats/Jpeg/Components/FloatingPointDCT.cs

@ -69,7 +69,7 @@ internal static partial class FloatingPointDCT
{ {
ref float tableRef = ref Unsafe.As<Block8x8F, float>(ref quantTable); ref float tableRef = ref Unsafe.As<Block8x8F, float>(ref quantTable);
ref float multipliersRef = ref MemoryMarshal.GetReference<float>(AdjustmentCoefficients); ref float multipliersRef = ref MemoryMarshal.GetReference<float>(AdjustmentCoefficients);
for (nint i = 0; i < Block8x8F.Size; i++) for (nuint i = 0; i < Block8x8F.Size; i++)
{ {
ref float elemRef = ref Unsafe.Add(ref tableRef, i); ref float elemRef = ref Unsafe.Add(ref tableRef, i);
elemRef = 0.125f * elemRef * Unsafe.Add(ref multipliersRef, i); elemRef = 0.125f * elemRef * Unsafe.Add(ref multipliersRef, i);
@ -88,7 +88,7 @@ internal static partial class FloatingPointDCT
{ {
ref float tableRef = ref Unsafe.As<Block8x8F, float>(ref quantTable); ref float tableRef = ref Unsafe.As<Block8x8F, float>(ref quantTable);
ref float multipliersRef = ref MemoryMarshal.GetReference<float>(AdjustmentCoefficients); ref float multipliersRef = ref MemoryMarshal.GetReference<float>(AdjustmentCoefficients);
for (nint i = 0; i < Block8x8F.Size; i++) for (nuint i = 0; i < Block8x8F.Size; i++)
{ {
ref float elemRef = ref Unsafe.Add(ref tableRef, i); ref float elemRef = ref Unsafe.Add(ref tableRef, i);
elemRef = 0.125f / (elemRef * Unsafe.Add(ref multipliersRef, i)); elemRef = 0.125f / (elemRef * Unsafe.Add(ref multipliersRef, i));

26
src/ImageSharp/Formats/Jpeg/Components/ScaledFloatingPointDCT.cs

@ -40,7 +40,7 @@ internal static class ScaledFloatingPointDCT
public static void AdjustToIDCT(ref Block8x8F quantTable) public static void AdjustToIDCT(ref Block8x8F quantTable)
{ {
ref float tableRef = ref Unsafe.As<Block8x8F, float>(ref quantTable); ref float tableRef = ref Unsafe.As<Block8x8F, float>(ref quantTable);
for (nint i = 0; i < Block8x8F.Size; i++) for (nuint i = 0; i < Block8x8F.Size; i++)
{ {
ref float elemRef = ref Unsafe.Add(ref tableRef, i); ref float elemRef = ref Unsafe.Add(ref tableRef, i);
elemRef = 0.125f * elemRef; elemRef = 0.125f * elemRef;
@ -103,10 +103,10 @@ internal static class ScaledFloatingPointDCT
// temporal result is saved to +4 shifted indices // temporal result is saved to +4 shifted indices
// because result is saved into the top left 2x2 region of the // because result is saved into the top left 2x2 region of the
// input block // input block
block[(ctr * 8) + 0 + 4] = (tmp10 + tmp2) / 2; block[(ctr * 8) + 0 + 4] = (tmp10 + tmp2) * 0.5F;
block[(ctr * 8) + 3 + 4] = (tmp10 - tmp2) / 2; block[(ctr * 8) + 3 + 4] = (tmp10 - tmp2) * 0.5F;
block[(ctr * 8) + 1 + 4] = (tmp12 + tmp0) / 2; block[(ctr * 8) + 1 + 4] = (tmp12 + tmp0) * 0.5F;
block[(ctr * 8) + 2 + 4] = (tmp12 - tmp0) / 2; block[(ctr * 8) + 2 + 4] = (tmp12 - tmp0) * 0.5F;
} }
for (int ctr = 0; ctr < 4; ctr++) for (int ctr = 0; ctr < 4; ctr++)
@ -136,10 +136,10 @@ internal static class ScaledFloatingPointDCT
(z4 * FP32_2_562915447); (z4 * FP32_2_562915447);
// Save results to the top left 4x4 subregion // Save results to the top left 4x4 subregion
block[(ctr * 8) + 0] = MathF.Round(Numerics.Clamp(((tmp10 + tmp2) / 2) + normalizationValue, 0, maxValue)); block[(ctr * 8) + 0] = MathF.Round(Numerics.Clamp(((tmp10 + tmp2) * 0.5F) + normalizationValue, 0, maxValue));
block[(ctr * 8) + 3] = MathF.Round(Numerics.Clamp(((tmp10 - tmp2) / 2) + normalizationValue, 0, maxValue)); block[(ctr * 8) + 3] = MathF.Round(Numerics.Clamp(((tmp10 - tmp2) * 0.5F) + normalizationValue, 0, maxValue));
block[(ctr * 8) + 1] = MathF.Round(Numerics.Clamp(((tmp12 + tmp0) / 2) + normalizationValue, 0, maxValue)); block[(ctr * 8) + 1] = MathF.Round(Numerics.Clamp(((tmp12 + tmp0) * 0.5F) + normalizationValue, 0, maxValue));
block[(ctr * 8) + 2] = MathF.Round(Numerics.Clamp(((tmp12 - tmp0) / 2) + normalizationValue, 0, maxValue)); block[(ctr * 8) + 2] = MathF.Round(Numerics.Clamp(((tmp12 - tmp0) * 0.5F) + normalizationValue, 0, maxValue));
} }
} }
@ -183,8 +183,8 @@ internal static class ScaledFloatingPointDCT
// temporal result is saved to +2 shifted indices // temporal result is saved to +2 shifted indices
// because result is saved into the top left 2x2 region of the // because result is saved into the top left 2x2 region of the
// input block // input block
block[(ctr * 8) + 2] = (tmp10 + tmp0) / 4; block[(ctr * 8) + 2] = (tmp10 + tmp0) * 0.25F;
block[(ctr * 8) + 3] = (tmp10 - tmp0) / 4; block[(ctr * 8) + 3] = (tmp10 - tmp0) * 0.25F;
} }
for (int ctr = 0; ctr < 2; ctr++) for (int ctr = 0; ctr < 2; ctr++)
@ -199,8 +199,8 @@ internal static class ScaledFloatingPointDCT
(block[ctr + (8 * 1) + 2] * FP32_3_624509785); (block[ctr + (8 * 1) + 2] * FP32_3_624509785);
// Save results to the top left 2x2 subregion // Save results to the top left 2x2 subregion
block[(ctr * 8) + 0] = MathF.Round(Numerics.Clamp(((tmp10 + tmp0) / 4) + normalizationValue, 0, maxValue)); block[(ctr * 8) + 0] = MathF.Round(Numerics.Clamp(((tmp10 + tmp0) * 0.25F) + normalizationValue, 0, maxValue));
block[(ctr * 8) + 1] = MathF.Round(Numerics.Clamp(((tmp10 - tmp0) / 4) + normalizationValue, 0, maxValue)); block[(ctr * 8) + 1] = MathF.Round(Numerics.Clamp(((tmp10 - tmp0) * 0.25F) + normalizationValue, 0, maxValue));
} }
} }

26
src/ImageSharp/Formats/Png/Adam7.cs

@ -67,16 +67,22 @@ internal static class Adam7
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ComputeColumns(int width, int passIndex) public static int ComputeColumns(int width, int passIndex)
{ {
switch (passIndex) uint w = (uint)width;
uint result = passIndex switch
{ {
case 0: return (width + 7) / 8; 0 => (w + 7) / 8,
case 1: return (width + 3) / 8; 1 => (w + 3) / 8,
case 2: return (width + 3) / 4; 2 => (w + 3) / 4,
case 3: return (width + 1) / 4; 3 => (w + 1) / 4,
case 4: return (width + 1) / 2; 4 => (w + 1) / 2,
case 5: return width / 2; 5 => w / 2,
case 6: return width; 6 => w,
default: throw new ArgumentException($"Not a valid pass index: {passIndex}"); _ => Throw(passIndex)
} };
return (int)result;
static uint Throw(int passIndex) => throw new ArgumentException($"Not a valid pass index: {passIndex}");
} }
} }

26
src/ImageSharp/Formats/Png/Filters/AverageFilter.cs

@ -42,7 +42,7 @@ internal static class AverageFilter
} }
else else
{ {
DecodeScalar(scanline, previousScanline, bytesPerPixel); DecodeScalar(scanline, previousScanline, (uint)bytesPerPixel);
} }
} }
@ -56,7 +56,7 @@ internal static class AverageFilter
Vector128<byte> ones = Vector128.Create((byte)1); Vector128<byte> ones = Vector128.Create((byte)1);
int rb = scanline.Length; int rb = scanline.Length;
nint offset = 1; nuint offset = 1;
while (rb >= 4) while (rb >= 4)
{ {
ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset);
@ -88,7 +88,7 @@ internal static class AverageFilter
Vector64<byte> d = Vector64<byte>.Zero; Vector64<byte> d = Vector64<byte>.Zero;
int rb = scanline.Length; int rb = scanline.Length;
int offset = 1; nuint offset = 1;
const int bytesPerBatch = 4; const int bytesPerBatch = 4;
while (rb >= bytesPerBatch) while (rb >= bytesPerBatch)
{ {
@ -108,12 +108,12 @@ internal static class AverageFilter
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void DecodeScalar(Span<byte> scanline, Span<byte> previousScanline, int bytesPerPixel) private static void DecodeScalar(Span<byte> scanline, Span<byte> previousScanline, uint bytesPerPixel)
{ {
ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline); ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline);
ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline); ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline);
nint x = 1; nuint x = 1;
for (; x <= bytesPerPixel /* Note the <= because x starts at 1 */; ++x) for (; x <= bytesPerPixel /* Note the <= because x starts at 1 */; ++x)
{ {
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); ref byte scan = ref Unsafe.Add(ref scanBaseRef, x);
@ -121,7 +121,7 @@ internal static class AverageFilter
scan = (byte)(scan + (above >> 1)); scan = (byte)(scan + (above >> 1));
} }
for (; x < scanline.Length; ++x) for (; x < (uint)scanline.Length; ++x)
{ {
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); ref byte scan = ref Unsafe.Add(ref scanBaseRef, x);
byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel);
@ -139,7 +139,7 @@ internal static class AverageFilter
/// <param name="bytesPerPixel">The bytes per pixel.</param> /// <param name="bytesPerPixel">The bytes per pixel.</param>
/// <param name="sum">The sum of the total variance of the filtered row.</param> /// <param name="sum">The sum of the total variance of the filtered row.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Encode(ReadOnlySpan<byte> scanline, ReadOnlySpan<byte> previousScanline, Span<byte> result, int bytesPerPixel, out int sum) public static void Encode(ReadOnlySpan<byte> scanline, ReadOnlySpan<byte> previousScanline, Span<byte> result, uint bytesPerPixel, out int sum)
{ {
DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline));
DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result));
@ -152,7 +152,7 @@ internal static class AverageFilter
// Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2) // Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2)
resultBaseRef = (byte)FilterType.Average; resultBaseRef = (byte)FilterType.Average;
nint x = 0; nuint x = 0;
for (; x < bytesPerPixel; /* Note: ++x happens in the body to avoid one add operation */) for (; x < bytesPerPixel; /* Note: ++x happens in the body to avoid one add operation */)
{ {
byte scan = Unsafe.Add(ref scanBaseRef, x); byte scan = Unsafe.Add(ref scanBaseRef, x);
@ -169,7 +169,7 @@ internal static class AverageFilter
Vector256<int> sumAccumulator = Vector256<int>.Zero; Vector256<int> sumAccumulator = Vector256<int>.Zero;
Vector256<byte> allBitsSet = Avx2.CompareEqual(sumAccumulator, sumAccumulator).AsByte(); Vector256<byte> allBitsSet = Avx2.CompareEqual(sumAccumulator, sumAccumulator).AsByte();
for (nint xLeft = x - bytesPerPixel; x <= scanline.Length - Vector256<byte>.Count; xLeft += Vector256<byte>.Count) for (nuint xLeft = x - bytesPerPixel; x <= (uint)(scanline.Length - Vector256<byte>.Count); xLeft += (uint)Vector256<byte>.Count)
{ {
Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x)); Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
Vector256<byte> left = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft)); Vector256<byte> left = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));
@ -179,7 +179,7 @@ internal static class AverageFilter
Vector256<byte> res = Avx2.Subtract(scan, avg); Vector256<byte> res = Avx2.Subtract(scan, avg);
Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type
x += Vector256<byte>.Count; x += (uint)Vector256<byte>.Count;
sumAccumulator = Avx2.Add(sumAccumulator, Avx2.SumAbsoluteDifferences(Avx2.Abs(res.AsSByte()), zero).AsInt32()); sumAccumulator = Avx2.Add(sumAccumulator, Avx2.SumAbsoluteDifferences(Avx2.Abs(res.AsSByte()), zero).AsInt32());
} }
@ -192,7 +192,7 @@ internal static class AverageFilter
Vector128<int> sumAccumulator = Vector128<int>.Zero; Vector128<int> sumAccumulator = Vector128<int>.Zero;
Vector128<byte> allBitsSet = Sse2.CompareEqual(sumAccumulator, sumAccumulator).AsByte(); Vector128<byte> allBitsSet = Sse2.CompareEqual(sumAccumulator, sumAccumulator).AsByte();
for (nint xLeft = x - bytesPerPixel; x <= scanline.Length - Vector128<byte>.Count; xLeft += Vector128<byte>.Count) for (nuint xLeft = x - bytesPerPixel; x <= (uint)(scanline.Length - Vector128<byte>.Count); xLeft += (uint)Vector128<byte>.Count)
{ {
Vector128<byte> scan = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref scanBaseRef, x)); Vector128<byte> scan = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
Vector128<byte> left = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft)); Vector128<byte> left = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));
@ -202,7 +202,7 @@ internal static class AverageFilter
Vector128<byte> res = Sse2.Subtract(scan, avg); Vector128<byte> res = Sse2.Subtract(scan, avg);
Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type
x += Vector128<byte>.Count; x += (uint)Vector128<byte>.Count;
Vector128<byte> absRes; Vector128<byte> absRes;
if (Ssse3.IsSupported) if (Ssse3.IsSupported)
@ -221,7 +221,7 @@ internal static class AverageFilter
sum += Numerics.EvenReduceSum(sumAccumulator); sum += Numerics.EvenReduceSum(sumAccumulator);
} }
for (nint xLeft = x - bytesPerPixel; x < scanline.Length; ++xLeft /* Note: ++x happens in the body to avoid one add operation */) for (nuint xLeft = x - bytesPerPixel; x < (uint)scanline.Length; ++xLeft /* Note: ++x happens in the body to avoid one add operation */)
{ {
byte scan = Unsafe.Add(ref scanBaseRef, x); byte scan = Unsafe.Add(ref scanBaseRef, x);
byte left = Unsafe.Add(ref scanBaseRef, xLeft); byte left = Unsafe.Add(ref scanBaseRef, xLeft);

28
src/ImageSharp/Formats/Png/Filters/PaethFilter.cs

@ -45,7 +45,7 @@ internal static class PaethFilter
} }
else else
{ {
DecodeScalar(scanline, previousScanline, bytesPerPixel); DecodeScalar(scanline, previousScanline, (uint)bytesPerPixel);
} }
} }
@ -59,7 +59,7 @@ internal static class PaethFilter
Vector128<byte> d = Vector128<byte>.Zero; Vector128<byte> d = Vector128<byte>.Zero;
int rb = scanline.Length; int rb = scanline.Length;
nint offset = 1; nuint offset = 1;
while (rb >= 4) while (rb >= 4)
{ {
ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset);
@ -113,7 +113,7 @@ internal static class PaethFilter
Vector128<byte> d = Vector128<byte>.Zero; Vector128<byte> d = Vector128<byte>.Zero;
int rb = scanline.Length; int rb = scanline.Length;
nint offset = 1; nuint offset = 1;
const int bytesPerBatch = 4; const int bytesPerBatch = 4;
while (rb >= bytesPerBatch) while (rb >= bytesPerBatch)
{ {
@ -179,14 +179,14 @@ internal static class PaethFilter
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void DecodeScalar(Span<byte> scanline, Span<byte> previousScanline, int bytesPerPixel) private static void DecodeScalar(Span<byte> scanline, Span<byte> previousScanline, uint bytesPerPixel)
{ {
ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline); ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline);
ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline); ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline);
// Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp)) // Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp))
int offset = bytesPerPixel + 1; // Add one because x starts at one. nuint offset = bytesPerPixel + 1; // Add one because x starts at one.
nint x = 1; nuint x = 1;
for (; x < offset; x++) for (; x < offset; x++)
{ {
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); ref byte scan = ref Unsafe.Add(ref scanBaseRef, x);
@ -194,7 +194,7 @@ internal static class PaethFilter
scan = (byte)(scan + above); scan = (byte)(scan + above);
} }
for (; x < scanline.Length; x++) for (; x < (uint)scanline.Length; x++)
{ {
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); ref byte scan = ref Unsafe.Add(ref scanBaseRef, x);
byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel);
@ -226,8 +226,8 @@ internal static class PaethFilter
// Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp)) // Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp))
resultBaseRef = (byte)FilterType.Paeth; resultBaseRef = (byte)FilterType.Paeth;
nint x = 0; nuint x = 0;
for (; x < bytesPerPixel; /* Note: ++x happens in the body to avoid one add operation */) for (; x < (uint)bytesPerPixel; /* Note: ++x happens in the body to avoid one add operation */)
{ {
byte scan = Unsafe.Add(ref scanBaseRef, x); byte scan = Unsafe.Add(ref scanBaseRef, x);
byte above = Unsafe.Add(ref prevBaseRef, x); byte above = Unsafe.Add(ref prevBaseRef, x);
@ -242,7 +242,7 @@ internal static class PaethFilter
Vector256<byte> zero = Vector256<byte>.Zero; Vector256<byte> zero = Vector256<byte>.Zero;
Vector256<int> sumAccumulator = Vector256<int>.Zero; Vector256<int> sumAccumulator = Vector256<int>.Zero;
for (nint xLeft = x - bytesPerPixel; x <= scanline.Length - Vector256<byte>.Count; xLeft += Vector256<byte>.Count) for (nuint xLeft = x - (uint)bytesPerPixel; (int)x <= scanline.Length - Vector256<byte>.Count; xLeft += (uint)Vector256<byte>.Count)
{ {
Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x)); Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
Vector256<byte> left = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft)); Vector256<byte> left = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));
@ -251,7 +251,7 @@ internal static class PaethFilter
Vector256<byte> res = Avx2.Subtract(scan, PaethPredictor(left, above, upperLeft)); Vector256<byte> res = Avx2.Subtract(scan, PaethPredictor(left, above, upperLeft));
Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type
x += Vector256<byte>.Count; x += (uint)Vector256<byte>.Count;
sumAccumulator = Avx2.Add(sumAccumulator, Avx2.SumAbsoluteDifferences(Avx2.Abs(res.AsSByte()), zero).AsInt32()); sumAccumulator = Avx2.Add(sumAccumulator, Avx2.SumAbsoluteDifferences(Avx2.Abs(res.AsSByte()), zero).AsInt32());
} }
@ -262,7 +262,7 @@ internal static class PaethFilter
{ {
Vector<uint> sumAccumulator = Vector<uint>.Zero; Vector<uint> sumAccumulator = Vector<uint>.Zero;
for (nint xLeft = x - bytesPerPixel; x <= scanline.Length - Vector<byte>.Count; xLeft += Vector<byte>.Count) for (nuint xLeft = x - (uint)bytesPerPixel; (int)x <= scanline.Length - Vector<byte>.Count; xLeft += (uint)Vector<byte>.Count)
{ {
Vector<byte> scan = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, x)); Vector<byte> scan = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
Vector<byte> left = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft)); Vector<byte> left = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));
@ -271,7 +271,7 @@ internal static class PaethFilter
Vector<byte> res = scan - PaethPredictor(left, above, upperLeft); Vector<byte> res = scan - PaethPredictor(left, above, upperLeft);
Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type
x += Vector<byte>.Count; x += (uint)Vector<byte>.Count;
Numerics.Accumulate(ref sumAccumulator, Vector.AsVectorByte(Vector.Abs(Vector.AsVectorSByte(res)))); Numerics.Accumulate(ref sumAccumulator, Vector.AsVectorByte(Vector.Abs(Vector.AsVectorSByte(res))));
} }
@ -282,7 +282,7 @@ internal static class PaethFilter
} }
} }
for (nint xLeft = x - bytesPerPixel; x < scanline.Length; ++xLeft /* Note: ++x happens in the body to avoid one add operation */) for (nuint xLeft = x - (uint)bytesPerPixel; (int)x < scanline.Length; ++xLeft /* Note: ++x happens in the body to avoid one add operation */)
{ {
byte scan = Unsafe.Add(ref scanBaseRef, x); byte scan = Unsafe.Add(ref scanBaseRef, x);
byte left = Unsafe.Add(ref scanBaseRef, xLeft); byte left = Unsafe.Add(ref scanBaseRef, xLeft);

26
src/ImageSharp/Formats/Png/Filters/SubFilter.cs

@ -36,7 +36,7 @@ internal static class SubFilter
} }
else else
{ {
DecodeScalar(scanline, bytesPerPixel); DecodeScalar(scanline, (uint)bytesPerPixel);
} }
} }
@ -47,7 +47,7 @@ internal static class SubFilter
Vector128<byte> d = Vector128<byte>.Zero; Vector128<byte> d = Vector128<byte>.Zero;
int rb = scanline.Length; int rb = scanline.Length;
nint offset = 1; nuint offset = 1;
while (rb >= 4) while (rb >= 4)
{ {
ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset);
@ -70,7 +70,7 @@ internal static class SubFilter
Vector64<byte> d = Vector64<byte>.Zero; Vector64<byte> d = Vector64<byte>.Zero;
int rb = scanline.Length; int rb = scanline.Length;
int offset = 1; nuint offset = 1;
const int bytesPerBatch = 4; const int bytesPerBatch = 4;
while (rb >= bytesPerBatch) while (rb >= bytesPerBatch)
{ {
@ -87,14 +87,14 @@ internal static class SubFilter
} }
} }
private static void DecodeScalar(Span<byte> scanline, int bytesPerPixel) private static void DecodeScalar(Span<byte> scanline, nuint bytesPerPixel)
{ {
ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline); ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline);
// Sub(x) + Raw(x-bpp) // Sub(x) + Raw(x-bpp)
nint x = bytesPerPixel + 1; nuint x = bytesPerPixel + 1;
Unsafe.Add(ref scanBaseRef, x); Unsafe.Add(ref scanBaseRef, x);
for (; x < scanline.Length; ++x) for (; x < (uint)scanline.Length; ++x)
{ {
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); ref byte scan = ref Unsafe.Add(ref scanBaseRef, x);
byte prev = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); byte prev = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel);
@ -121,8 +121,8 @@ internal static class SubFilter
// Sub(x) = Raw(x) - Raw(x-bpp) // Sub(x) = Raw(x) - Raw(x-bpp)
resultBaseRef = (byte)FilterType.Sub; resultBaseRef = (byte)FilterType.Sub;
nint x = 0; nuint x = 0;
for (; x < bytesPerPixel; /* Note: ++x happens in the body to avoid one add operation */) for (; x < (uint)bytesPerPixel; /* Note: ++x happens in the body to avoid one add operation */)
{ {
byte scan = Unsafe.Add(ref scanBaseRef, x); byte scan = Unsafe.Add(ref scanBaseRef, x);
++x; ++x;
@ -136,14 +136,14 @@ internal static class SubFilter
Vector256<byte> zero = Vector256<byte>.Zero; Vector256<byte> zero = Vector256<byte>.Zero;
Vector256<int> sumAccumulator = Vector256<int>.Zero; Vector256<int> sumAccumulator = Vector256<int>.Zero;
for (nint xLeft = x - bytesPerPixel; x <= scanline.Length - Vector256<byte>.Count; xLeft += Vector256<byte>.Count) for (nuint xLeft = x - (uint)bytesPerPixel; x <= (uint)(scanline.Length - Vector256<byte>.Count); xLeft += (uint)Vector256<byte>.Count)
{ {
Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x)); Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
Vector256<byte> prev = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft)); Vector256<byte> prev = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));
Vector256<byte> res = Avx2.Subtract(scan, prev); Vector256<byte> res = Avx2.Subtract(scan, prev);
Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type
x += Vector256<byte>.Count; x += (uint)Vector256<byte>.Count;
sumAccumulator = Avx2.Add(sumAccumulator, Avx2.SumAbsoluteDifferences(Avx2.Abs(res.AsSByte()), zero).AsInt32()); sumAccumulator = Avx2.Add(sumAccumulator, Avx2.SumAbsoluteDifferences(Avx2.Abs(res.AsSByte()), zero).AsInt32());
} }
@ -154,14 +154,14 @@ internal static class SubFilter
{ {
Vector<uint> sumAccumulator = Vector<uint>.Zero; Vector<uint> sumAccumulator = Vector<uint>.Zero;
for (nint xLeft = x - bytesPerPixel; x <= scanline.Length - Vector<byte>.Count; xLeft += Vector<byte>.Count) for (nuint xLeft = x - (uint)bytesPerPixel; x <= (uint)(scanline.Length - Vector<byte>.Count); xLeft += (uint)Vector<byte>.Count)
{ {
Vector<byte> scan = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, x)); Vector<byte> scan = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
Vector<byte> prev = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft)); Vector<byte> prev = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, xLeft));
Vector<byte> res = scan - prev; Vector<byte> res = scan - prev;
Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type
x += Vector<byte>.Count; x += (uint)Vector<byte>.Count;
Numerics.Accumulate(ref sumAccumulator, Vector.AsVectorByte(Vector.Abs(Vector.AsVectorSByte(res)))); Numerics.Accumulate(ref sumAccumulator, Vector.AsVectorByte(Vector.Abs(Vector.AsVectorSByte(res))));
} }
@ -172,7 +172,7 @@ internal static class SubFilter
} }
} }
for (nint xLeft = x - bytesPerPixel; x < scanline.Length; ++xLeft /* Note: ++x happens in the body to avoid one add operation */) for (nuint xLeft = x - (uint)bytesPerPixel; x < (uint)scanline.Length; ++xLeft /* Note: ++x happens in the body to avoid one add operation */)
{ {
byte scan = Unsafe.Add(ref scanBaseRef, x); byte scan = Unsafe.Add(ref scanBaseRef, x);
byte prev = Unsafe.Add(ref scanBaseRef, xLeft); byte prev = Unsafe.Add(ref scanBaseRef, xLeft);

30
src/ImageSharp/Formats/Png/Filters/UpFilter.cs

@ -52,7 +52,7 @@ internal static class UpFilter
// Up(x) + Prior(x) // Up(x) + Prior(x)
int rb = scanline.Length; int rb = scanline.Length;
nint offset = 1; nuint offset = 1;
while (rb >= Vector256<byte>.Count) while (rb >= Vector256<byte>.Count)
{ {
ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset);
@ -61,12 +61,12 @@ internal static class UpFilter
Unsafe.As<byte, Vector256<byte>>(ref scanRef) = Avx2.Add(up, prior); Unsafe.As<byte, Vector256<byte>>(ref scanRef) = Avx2.Add(up, prior);
offset += Vector256<byte>.Count; offset += (uint)Vector256<byte>.Count;
rb -= Vector256<byte>.Count; rb -= Vector256<byte>.Count;
} }
// Handle left over. // Handle left over.
for (nint i = offset; i < scanline.Length; i++) for (nuint i = offset; i < (uint)scanline.Length; i++)
{ {
ref byte scan = ref Unsafe.Add(ref scanBaseRef, offset); ref byte scan = ref Unsafe.Add(ref scanBaseRef, offset);
byte above = Unsafe.Add(ref prevBaseRef, offset); byte above = Unsafe.Add(ref prevBaseRef, offset);
@ -82,7 +82,7 @@ internal static class UpFilter
// Up(x) + Prior(x) // Up(x) + Prior(x)
int rb = scanline.Length; int rb = scanline.Length;
nint offset = 1; nuint offset = 1;
while (rb >= Vector128<byte>.Count) while (rb >= Vector128<byte>.Count)
{ {
ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset);
@ -91,12 +91,12 @@ internal static class UpFilter
Unsafe.As<byte, Vector128<byte>>(ref scanRef) = Sse2.Add(up, prior); Unsafe.As<byte, Vector128<byte>>(ref scanRef) = Sse2.Add(up, prior);
offset += Vector128<byte>.Count; offset += (uint)Vector128<byte>.Count;
rb -= Vector128<byte>.Count; rb -= Vector128<byte>.Count;
} }
// Handle left over. // Handle left over.
for (nint i = offset; i < scanline.Length; i++) for (nuint i = offset; i < (uint)scanline.Length; i++)
{ {
ref byte scan = ref Unsafe.Add(ref scanBaseRef, offset); ref byte scan = ref Unsafe.Add(ref scanBaseRef, offset);
byte above = Unsafe.Add(ref prevBaseRef, offset); byte above = Unsafe.Add(ref prevBaseRef, offset);
@ -112,7 +112,7 @@ internal static class UpFilter
// Up(x) + Prior(x) // Up(x) + Prior(x)
int rb = scanline.Length; int rb = scanline.Length;
nint offset = 1; nuint offset = 1;
const int bytesPerBatch = 16; const int bytesPerBatch = 16;
while (rb >= bytesPerBatch) while (rb >= bytesPerBatch)
{ {
@ -127,7 +127,7 @@ internal static class UpFilter
} }
// Handle left over. // Handle left over.
for (nint i = offset; i < scanline.Length; i++) for (nuint i = offset; i < (uint)scanline.Length; i++)
{ {
ref byte scan = ref Unsafe.Add(ref scanBaseRef, offset); ref byte scan = ref Unsafe.Add(ref scanBaseRef, offset);
byte above = Unsafe.Add(ref prevBaseRef, offset); byte above = Unsafe.Add(ref prevBaseRef, offset);
@ -143,7 +143,7 @@ internal static class UpFilter
ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline); ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline);
// Up(x) + Prior(x) // Up(x) + Prior(x)
for (nint x = 1; x < scanline.Length; x++) for (nuint x = 1; x < (uint)scanline.Length; x++)
{ {
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); ref byte scan = ref Unsafe.Add(ref scanBaseRef, x);
byte above = Unsafe.Add(ref prevBaseRef, x); byte above = Unsafe.Add(ref prevBaseRef, x);
@ -172,21 +172,21 @@ internal static class UpFilter
// Up(x) = Raw(x) - Prior(x) // Up(x) = Raw(x) - Prior(x)
resultBaseRef = (byte)FilterType.Up; resultBaseRef = (byte)FilterType.Up;
nint x = 0; nuint x = 0;
if (Avx2.IsSupported) if (Avx2.IsSupported)
{ {
Vector256<byte> zero = Vector256<byte>.Zero; Vector256<byte> zero = Vector256<byte>.Zero;
Vector256<int> sumAccumulator = Vector256<int>.Zero; Vector256<int> sumAccumulator = Vector256<int>.Zero;
for (; x <= scanline.Length - Vector256<byte>.Count;) for (; x <= (uint)(scanline.Length - Vector256<byte>.Count);)
{ {
Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x)); Vector256<byte> scan = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
Vector256<byte> above = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref prevBaseRef, x)); Vector256<byte> above = Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref prevBaseRef, x));
Vector256<byte> res = Avx2.Subtract(scan, above); Vector256<byte> res = Avx2.Subtract(scan, above);
Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type Unsafe.As<byte, Vector256<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type
x += Vector256<byte>.Count; x += (uint)Vector256<byte>.Count;
sumAccumulator = Avx2.Add(sumAccumulator, Avx2.SumAbsoluteDifferences(Avx2.Abs(res.AsSByte()), zero).AsInt32()); sumAccumulator = Avx2.Add(sumAccumulator, Avx2.SumAbsoluteDifferences(Avx2.Abs(res.AsSByte()), zero).AsInt32());
} }
@ -197,14 +197,14 @@ internal static class UpFilter
{ {
Vector<uint> sumAccumulator = Vector<uint>.Zero; Vector<uint> sumAccumulator = Vector<uint>.Zero;
for (; x <= scanline.Length - Vector<byte>.Count;) for (; x <= (uint)(scanline.Length - Vector<byte>.Count);)
{ {
Vector<byte> scan = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, x)); Vector<byte> scan = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref scanBaseRef, x));
Vector<byte> above = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref prevBaseRef, x)); Vector<byte> above = Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref prevBaseRef, x));
Vector<byte> res = scan - above; Vector<byte> res = scan - above;
Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type Unsafe.As<byte, Vector<byte>>(ref Unsafe.Add(ref resultBaseRef, x + 1)) = res; // +1 to skip filter type
x += Vector<byte>.Count; x += (uint)Vector<byte>.Count;
Numerics.Accumulate(ref sumAccumulator, Vector.AsVectorByte(Vector.Abs(Vector.AsVectorSByte(res)))); Numerics.Accumulate(ref sumAccumulator, Vector.AsVectorByte(Vector.Abs(Vector.AsVectorSByte(res))));
} }
@ -215,7 +215,7 @@ internal static class UpFilter
} }
} }
for (; x < scanline.Length; /* Note: ++x happens in the body to avoid one add operation */) for (; x < (uint)scanline.Length; /* Note: ++x happens in the body to avoid one add operation */)
{ {
byte scan = Unsafe.Add(ref scanBaseRef, x); byte scan = Unsafe.Add(ref scanBaseRef, x);
byte above = Unsafe.Add(ref prevBaseRef, x); byte above = Unsafe.Add(ref prevBaseRef, x);

32
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -414,11 +414,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
for (int i = 0; i < bytesPerScanline; i++) for (int i = 0; i < bytesPerScanline; i++)
{ {
byte b = Unsafe.Add(ref sourceRef, i); byte b = Unsafe.Add(ref sourceRef, (uint)i);
for (int shift = 0; shift < 8; shift += bits) for (int shift = 0; shift < 8; shift += bits)
{ {
int colorIndex = (b >> (8 - bits - shift)) & mask; int colorIndex = (b >> (8 - bits - shift)) & mask;
Unsafe.Add(ref resultRef, resultOffset) = (byte)colorIndex; Unsafe.Add(ref resultRef, (uint)resultOffset) = (byte)colorIndex;
resultOffset++; resultOffset++;
} }
} }
@ -777,8 +777,8 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
this.header, this.header,
scanlineSpan, scanlineSpan,
rowSpan, rowSpan,
this.bytesPerPixel, (uint)this.bytesPerPixel,
this.bytesPerSample); (uint)this.bytesPerSample);
break; break;
@ -858,8 +858,8 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
this.header, this.header,
scanlineSpan, scanlineSpan,
rowSpan, rowSpan,
pixelOffset, (uint)pixelOffset,
increment, (uint)increment,
pngMetadata.HasTransparency, pngMetadata.HasTransparency,
pngMetadata.TransparentL16.GetValueOrDefault(), pngMetadata.TransparentL16.GetValueOrDefault(),
pngMetadata.TransparentL8.GetValueOrDefault()); pngMetadata.TransparentL8.GetValueOrDefault());
@ -871,10 +871,10 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
this.header, this.header,
scanlineSpan, scanlineSpan,
rowSpan, rowSpan,
pixelOffset, (uint)pixelOffset,
increment, (uint)increment,
this.bytesPerPixel, (uint)this.bytesPerPixel,
this.bytesPerSample); (uint)this.bytesPerSample);
break; break;
@ -883,8 +883,8 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
this.header, this.header,
scanlineSpan, scanlineSpan,
rowSpan, rowSpan,
pixelOffset, (uint)pixelOffset,
increment, (uint)increment,
this.palette, this.palette,
this.paletteAlpha); this.paletteAlpha);
@ -895,8 +895,8 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
this.header, this.header,
scanlineSpan, scanlineSpan,
rowSpan, rowSpan,
pixelOffset, (uint)pixelOffset,
increment, (uint)increment,
this.bytesPerPixel, this.bytesPerPixel,
this.bytesPerSample, this.bytesPerSample,
pngMetadata.HasTransparency, pngMetadata.HasTransparency,
@ -910,8 +910,8 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
this.header, this.header,
scanlineSpan, scanlineSpan,
rowSpan, rowSpan,
pixelOffset, (uint)pixelOffset,
increment, (uint)increment,
this.bytesPerPixel, this.bytesPerPixel,
this.bytesPerSample); this.bytesPerSample);

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

@ -266,7 +266,7 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
// Can't map directly to byte array as it's big-endian. // Can't map directly to byte array as it's big-endian.
for (int x = 0, o = 0; x < luminanceSpan.Length; x++, o += 2) for (int x = 0, o = 0; x < luminanceSpan.Length; x++, o += 2)
{ {
L16 luminance = Unsafe.Add(ref luminanceRef, x); L16 luminance = Unsafe.Add(ref luminanceRef, (uint)x);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), luminance.PackedValue); BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), luminance.PackedValue);
} }
} }
@ -306,7 +306,7 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
// Can't map directly to byte array as it's big endian. // Can't map directly to byte array as it's big endian.
for (int x = 0, o = 0; x < laSpan.Length; x++, o += 4) for (int x = 0, o = 0; x < laSpan.Length; x++, o += 4)
{ {
La32 la = Unsafe.Add(ref laRef, x); La32 la = Unsafe.Add(ref laRef, (uint)x);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), la.L); BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), la.L);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 2, 2), la.A); BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 2, 2), la.A);
} }
@ -366,7 +366,7 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
// Can't map directly to byte array as it's big endian. // Can't map directly to byte array as it's big endian.
for (int x = 0, o = 0; x < rowSpan.Length; x++, o += 8) for (int x = 0, o = 0; x < rowSpan.Length; x++, o += 8)
{ {
Rgba64 rgba = Unsafe.Add(ref rgbaRef, x); Rgba64 rgba = Unsafe.Add(ref rgbaRef, (uint)x);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), rgba.R); BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), rgba.R);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 2, 2), rgba.G); BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 2, 2), rgba.G);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 4, 2), rgba.B); BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 4, 2), rgba.B);
@ -388,7 +388,7 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
// Can't map directly to byte array as it's big endian. // Can't map directly to byte array as it's big endian.
for (int x = 0, o = 0; x < rowSpan.Length; x++, o += 6) for (int x = 0, o = 0; x < rowSpan.Length; x++, o += 6)
{ {
Rgb48 rgb = Unsafe.Add(ref rgbRef, x); Rgb48 rgb = Unsafe.Add(ref rgbRef, (uint)x);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), rgb.R); BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), rgb.R);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 2, 2), rgb.G); BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 2, 2), rgb.G);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 4, 2), rgb.B); BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 4, 2), rgb.B);
@ -455,7 +455,7 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
break; break;
case PngFilterMethod.Average: case PngFilterMethod.Average:
AverageFilter.Encode(this.currentScanline.GetSpan(), this.previousScanline.GetSpan(), filter, this.bytesPerPixel, out int _); AverageFilter.Encode(this.currentScanline.GetSpan(), this.previousScanline.GetSpan(), filter, (uint)this.bytesPerPixel, out int _);
break; break;
case PngFilterMethod.Paeth: case PngFilterMethod.Paeth:
@ -547,7 +547,7 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
RuntimeUtility.Swap(ref filter, ref attempt); RuntimeUtility.Swap(ref filter, ref attempt);
} }
AverageFilter.Encode(current, previous, attempt, this.bytesPerPixel, out sum); AverageFilter.Encode(current, previous, attempt, (uint)this.bytesPerPixel, out sum);
if (sum < min) if (sum < min)
{ {
min = sum; min = sum;
@ -617,17 +617,17 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
// Loop, assign, and extract alpha values from the palette. // Loop, assign, and extract alpha values from the palette.
for (int i = 0; i < paletteLength; i++) for (int i = 0; i < paletteLength; i++)
{ {
Rgba32 rgba = Unsafe.Add(ref rgbaPaletteRef, i); Rgba32 rgba = Unsafe.Add(ref rgbaPaletteRef, (uint)i);
byte alpha = rgba.A; byte alpha = rgba.A;
Unsafe.Add(ref colorTableRef, i) = rgba.Rgb; Unsafe.Add(ref colorTableRef, (uint)i) = rgba.Rgb;
if (alpha > this.encoder.Threshold) if (alpha > this.encoder.Threshold)
{ {
alpha = byte.MaxValue; alpha = byte.MaxValue;
} }
hasAlpha = hasAlpha || alpha < byte.MaxValue; hasAlpha = hasAlpha || alpha < byte.MaxValue;
Unsafe.Add(ref alphaTableRef, i) = alpha; Unsafe.Add(ref alphaTableRef, (uint)i) = alpha;
} }
this.WriteChunk(stream, PngChunkType.Palette, colorTable.GetSpan(), 0, colorTableLength); this.WriteChunk(stream, PngChunkType.Palette, colorTable.GetSpan(), 0, colorTableLength);

6
src/ImageSharp/Formats/Png/PngEncoderHelpers.cs

@ -31,13 +31,13 @@ internal static class PngEncoderHelpers
for (int i = 0; i < source.Length; i++) for (int i = 0; i < source.Length; i++)
{ {
int value = ((int)MathF.Round(Unsafe.Add(ref sourceRef, i) / scale)) & mask; int value = ((int)MathF.Round(Unsafe.Add(ref sourceRef, (uint)i) / scale)) & mask;
v |= value << shift; v |= value << shift;
if (shift == 0) if (shift == 0)
{ {
shift = shift0; shift = shift0;
Unsafe.Add(ref resultRef, resultOffset) = (byte)v; Unsafe.Add(ref resultRef, (uint)resultOffset) = (byte)v;
resultOffset++; resultOffset++;
v = 0; v = 0;
} }
@ -49,7 +49,7 @@ internal static class PngEncoderHelpers
if (shift != shift0) if (shift != shift0)
{ {
Unsafe.Add(ref resultRef, resultOffset) = (byte)v; Unsafe.Add(ref resultRef, (uint)resultOffset) = (byte)v;
} }
} }
} }

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

@ -32,7 +32,8 @@ internal static class PngScanlineProcessor
{ {
if (header.BitDepth == 16) if (header.BitDepth == 16)
{ {
for (int x = 0, o = 0; x < header.Width; x++, o += 2) int o = 0;
for (nuint x = 0; x < (uint)header.Width; x++, 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)); pixel.FromL16(Unsafe.As<ushort, L16>(ref luminance));
@ -41,7 +42,7 @@ internal static class PngScanlineProcessor
} }
else else
{ {
for (int x = 0; x < header.Width; x++) for (nuint x = 0; x < (uint)header.Width; x++)
{ {
byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, x) * scaleFactor); byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, x) * scaleFactor);
pixel.FromL8(Unsafe.As<byte, L8>(ref luminance)); pixel.FromL8(Unsafe.As<byte, L8>(ref luminance));
@ -55,7 +56,8 @@ internal static class PngScanlineProcessor
if (header.BitDepth == 16) if (header.BitDepth == 16)
{ {
La32 source = default; La32 source = default;
for (int x = 0, o = 0; x < header.Width; x++, o += 2) int o = 0;
for (nuint x = 0; x < (uint)header.Width; x++, o += 2)
{ {
ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
source.L = luminance; source.L = luminance;
@ -69,7 +71,7 @@ internal static class PngScanlineProcessor
{ {
La16 source = default; La16 source = default;
byte scaledLuminanceTrans = (byte)(luminanceTrans.PackedValue * scaleFactor); byte scaledLuminanceTrans = (byte)(luminanceTrans.PackedValue * scaleFactor);
for (int x = 0; x < header.Width; x++) for (nuint x = 0; x < (uint)header.Width; x++)
{ {
byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, x) * scaleFactor); byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, x) * scaleFactor);
source.L = luminance; source.L = luminance;
@ -85,8 +87,8 @@ internal static class PngScanlineProcessor
in PngHeader header, in PngHeader header,
ReadOnlySpan<byte> scanlineSpan, ReadOnlySpan<byte> scanlineSpan,
Span<TPixel> rowSpan, Span<TPixel> rowSpan,
int pixelOffset, uint pixelOffset,
int increment, uint increment,
bool hasTrans, bool hasTrans,
L16 luminance16Trans, L16 luminance16Trans,
L8 luminanceTrans) L8 luminanceTrans)
@ -101,7 +103,8 @@ internal static class PngScanlineProcessor
{ {
if (header.BitDepth == 16) if (header.BitDepth == 16)
{ {
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o += 2) int o = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; 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)); pixel.FromL16(Unsafe.As<ushort, L16>(ref luminance));
@ -110,7 +113,7 @@ internal static class PngScanlineProcessor
} }
else else
{ {
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o++) for (nuint x = pixelOffset, o = 0; x < (uint)header.Width; 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)); pixel.FromL8(Unsafe.As<byte, L8>(ref luminance));
@ -124,7 +127,8 @@ internal static class PngScanlineProcessor
if (header.BitDepth == 16) if (header.BitDepth == 16)
{ {
La32 source = default; La32 source = default;
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o += 2) int o = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; x += increment, o += 2)
{ {
ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
source.L = luminance; source.L = luminance;
@ -138,7 +142,7 @@ internal static class PngScanlineProcessor
{ {
La16 source = default; La16 source = default;
byte scaledLuminanceTrans = (byte)(luminanceTrans.PackedValue * scaleFactor); byte scaledLuminanceTrans = (byte)(luminanceTrans.PackedValue * scaleFactor);
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o++) for (nuint x = pixelOffset, o = 0; x < (uint)header.Width; x += increment, o++)
{ {
byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, o) * scaleFactor); byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, o) * scaleFactor);
source.L = luminance; source.L = luminance;
@ -154,8 +158,8 @@ internal static class PngScanlineProcessor
in PngHeader header, in PngHeader header,
ReadOnlySpan<byte> scanlineSpan, ReadOnlySpan<byte> scanlineSpan,
Span<TPixel> rowSpan, Span<TPixel> rowSpan,
int bytesPerPixel, uint bytesPerPixel,
int bytesPerSample) uint bytesPerSample)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel pixel = default; TPixel pixel = default;
@ -165,7 +169,8 @@ internal static class PngScanlineProcessor
if (header.BitDepth == 16) if (header.BitDepth == 16)
{ {
La32 source = default; La32 source = default;
for (int x = 0, o = 0; x < header.Width; x++, o += 4) int o = 0;
for (nuint x = 0; x < (uint)header.Width; x++, o += 4)
{ {
source.L = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); source.L = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
source.A = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + 2, 2)); source.A = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + 2, 2));
@ -177,9 +182,9 @@ internal static class PngScanlineProcessor
else else
{ {
La16 source = default; La16 source = default;
for (int x = 0; x < header.Width; x++) for (nuint x = 0; x < (uint)header.Width; x++)
{ {
int offset = x * bytesPerPixel; nuint offset = x * bytesPerPixel;
source.L = Unsafe.Add(ref scanlineSpanRef, offset); source.L = Unsafe.Add(ref scanlineSpanRef, offset);
source.A = Unsafe.Add(ref scanlineSpanRef, offset + bytesPerSample); source.A = Unsafe.Add(ref scanlineSpanRef, offset + bytesPerSample);
@ -193,10 +198,10 @@ internal static class PngScanlineProcessor
in PngHeader header, in PngHeader header,
ReadOnlySpan<byte> scanlineSpan, ReadOnlySpan<byte> scanlineSpan,
Span<TPixel> rowSpan, Span<TPixel> rowSpan,
int pixelOffset, uint pixelOffset,
int increment, uint increment,
int bytesPerPixel, uint bytesPerPixel,
int bytesPerSample) uint bytesPerSample)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel pixel = default; TPixel pixel = default;
@ -206,20 +211,21 @@ internal static class PngScanlineProcessor
if (header.BitDepth == 16) if (header.BitDepth == 16)
{ {
La32 source = default; La32 source = default;
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o += 4) int o = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; x += increment, o += 4)
{ {
source.L = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); source.L = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
source.A = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + 2, 2)); source.A = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + 2, 2));
pixel.FromLa32(source); pixel.FromLa32(source);
Unsafe.Add(ref rowSpanRef, x) = pixel; Unsafe.Add(ref rowSpanRef, (uint)x) = pixel;
} }
} }
else else
{ {
int offset = 0;
La16 source = default; La16 source = default;
for (int x = pixelOffset; x < header.Width; x += increment) nuint offset = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; x += increment)
{ {
source.L = Unsafe.Add(ref scanlineSpanRef, offset); source.L = Unsafe.Add(ref scanlineSpanRef, offset);
source.A = Unsafe.Add(ref scanlineSpanRef, offset + bytesPerSample); source.A = Unsafe.Add(ref scanlineSpanRef, offset + bytesPerSample);
@ -255,11 +261,11 @@ internal static class PngScanlineProcessor
// If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha // If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha
// channel and we should try to read it. // channel and we should try to read it.
Rgba32 rgba = default; Rgba32 rgba = default;
ref byte paletteAlphaRef = ref paletteAlpha[0]; ref byte paletteAlphaRef = ref MemoryMarshal.GetArrayDataReference(paletteAlpha);
for (int x = 0; x < header.Width; x++) for (nuint x = 0; x < (uint)header.Width; x++)
{ {
int index = Unsafe.Add(ref scanlineSpanRef, x); uint index = Unsafe.Add(ref scanlineSpanRef, x);
rgba.Rgb = Unsafe.Add(ref palettePixelsRef, index); rgba.Rgb = Unsafe.Add(ref palettePixelsRef, index);
rgba.A = paletteAlpha.Length > index ? Unsafe.Add(ref paletteAlphaRef, index) : byte.MaxValue; rgba.A = paletteAlpha.Length > index ? Unsafe.Add(ref paletteAlphaRef, index) : byte.MaxValue;
@ -269,7 +275,7 @@ internal static class PngScanlineProcessor
} }
else else
{ {
for (int x = 0; x < header.Width; x++) for (nuint x = 0; x < (uint)header.Width; x++)
{ {
int index = Unsafe.Add(ref scanlineSpanRef, x); int index = Unsafe.Add(ref scanlineSpanRef, x);
Rgb24 rgb = Unsafe.Add(ref palettePixelsRef, index); Rgb24 rgb = Unsafe.Add(ref palettePixelsRef, index);
@ -284,8 +290,8 @@ internal static class PngScanlineProcessor
in PngHeader header, in PngHeader header,
ReadOnlySpan<byte> scanlineSpan, ReadOnlySpan<byte> scanlineSpan,
Span<TPixel> rowSpan, Span<TPixel> rowSpan,
int pixelOffset, uint pixelOffset,
int increment, uint increment,
ReadOnlySpan<byte> palette, ReadOnlySpan<byte> palette,
byte[] paletteAlpha) byte[] paletteAlpha)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
@ -301,10 +307,10 @@ internal static class PngScanlineProcessor
// If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha // If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha
// channel and we should try to read it. // channel and we should try to read it.
Rgba32 rgba = default; Rgba32 rgba = default;
ref byte paletteAlphaRef = ref paletteAlpha[0]; ref byte paletteAlphaRef = ref MemoryMarshal.GetArrayDataReference(paletteAlpha);
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o++) for (nuint x = pixelOffset, o = 0; x < (uint)header.Width; x += increment, o++)
{ {
int index = Unsafe.Add(ref scanlineSpanRef, o); uint index = Unsafe.Add(ref scanlineSpanRef, o);
rgba.A = paletteAlpha.Length > index ? Unsafe.Add(ref paletteAlphaRef, index) : byte.MaxValue; rgba.A = paletteAlpha.Length > index ? Unsafe.Add(ref paletteAlphaRef, index) : byte.MaxValue;
rgba.Rgb = Unsafe.Add(ref palettePixelsRef, index); rgba.Rgb = Unsafe.Add(ref palettePixelsRef, index);
@ -314,7 +320,7 @@ internal static class PngScanlineProcessor
} }
else else
{ {
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o++) for (nuint x = pixelOffset, o = 0; x < (uint)header.Width; x += increment, o++)
{ {
int index = Unsafe.Add(ref scanlineSpanRef, o); int index = Unsafe.Add(ref scanlineSpanRef, o);
Rgb24 rgb = Unsafe.Add(ref palettePixelsRef, index); Rgb24 rgb = Unsafe.Add(ref palettePixelsRef, index);
@ -345,7 +351,8 @@ internal static class PngScanlineProcessor
if (header.BitDepth == 16) if (header.BitDepth == 16)
{ {
Rgb48 rgb48 = default; Rgb48 rgb48 = default;
for (int x = 0, o = 0; x < header.Width; x++, o += bytesPerPixel) int o = 0;
for (nuint x = 0; x < (uint)header.Width; x++, o += bytesPerPixel)
{ {
rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample)); rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample)); rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
@ -367,7 +374,8 @@ internal static class PngScanlineProcessor
{ {
Rgb48 rgb48 = default; Rgb48 rgb48 = default;
Rgba64 rgba64 = default; Rgba64 rgba64 = default;
for (int x = 0, o = 0; x < header.Width; x++, o += bytesPerPixel) int o = 0;
for (nuint x = 0; x < (uint)header.Width; x++, o += bytesPerPixel)
{ {
rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample)); rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample)); rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
@ -385,7 +393,7 @@ internal static class PngScanlineProcessor
Rgba32 rgba32 = default; Rgba32 rgba32 = default;
ReadOnlySpan<Rgb24> rgb24Span = MemoryMarshal.Cast<byte, Rgb24>(scanlineSpan); ReadOnlySpan<Rgb24> rgb24Span = MemoryMarshal.Cast<byte, Rgb24>(scanlineSpan);
ref Rgb24 rgb24SpanRef = ref MemoryMarshal.GetReference(rgb24Span); ref Rgb24 rgb24SpanRef = ref MemoryMarshal.GetReference(rgb24Span);
for (int x = 0; x < header.Width; x++) for (nuint x = 0; x < (uint)header.Width; x++)
{ {
ref readonly Rgb24 rgb24 = ref Unsafe.Add(ref rgb24SpanRef, x); ref readonly Rgb24 rgb24 = ref Unsafe.Add(ref rgb24SpanRef, x);
rgba32.Rgb = rgb24; rgba32.Rgb = rgb24;
@ -401,8 +409,8 @@ internal static class PngScanlineProcessor
in PngHeader header, in PngHeader header,
ReadOnlySpan<byte> scanlineSpan, ReadOnlySpan<byte> scanlineSpan,
Span<TPixel> rowSpan, Span<TPixel> rowSpan,
int pixelOffset, uint pixelOffset,
int increment, uint increment,
int bytesPerPixel, int bytesPerPixel,
int bytesPerSample, int bytesPerSample,
bool hasTrans, bool hasTrans,
@ -420,7 +428,8 @@ internal static class PngScanlineProcessor
{ {
Rgb48 rgb48 = default; Rgb48 rgb48 = default;
Rgba64 rgba64 = default; Rgba64 rgba64 = default;
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o += bytesPerPixel) int o = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; x += increment, o += bytesPerPixel)
{ {
rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample)); rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample)); rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
@ -436,7 +445,8 @@ internal static class PngScanlineProcessor
else else
{ {
Rgb48 rgb48 = default; Rgb48 rgb48 = default;
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o += bytesPerPixel) int o = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; x += increment, o += bytesPerPixel)
{ {
rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample)); rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample)); rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
@ -453,11 +463,12 @@ internal static class PngScanlineProcessor
if (hasTrans) if (hasTrans)
{ {
Rgba32 rgba = default; Rgba32 rgba = default;
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o += bytesPerPixel) int o = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; x += increment, o += bytesPerPixel)
{ {
rgba.R = Unsafe.Add(ref scanlineSpanRef, o); rgba.R = Unsafe.Add(ref scanlineSpanRef, (uint)o);
rgba.G = Unsafe.Add(ref scanlineSpanRef, o + bytesPerSample); rgba.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
rgba.B = Unsafe.Add(ref scanlineSpanRef, o + (2 * bytesPerSample)); rgba.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
rgba.A = rgb24Trans.Equals(rgba.Rgb) ? byte.MinValue : byte.MaxValue; rgba.A = rgb24Trans.Equals(rgba.Rgb) ? byte.MinValue : byte.MaxValue;
pixel.FromRgba32(rgba); pixel.FromRgba32(rgba);
@ -467,11 +478,12 @@ internal static class PngScanlineProcessor
else else
{ {
Rgb24 rgb = default; Rgb24 rgb = default;
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o += bytesPerPixel) int o = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; x += increment, o += bytesPerPixel)
{ {
rgb.R = Unsafe.Add(ref scanlineSpanRef, o); rgb.R = Unsafe.Add(ref scanlineSpanRef, (uint)o);
rgb.G = Unsafe.Add(ref scanlineSpanRef, o + bytesPerSample); rgb.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
rgb.B = Unsafe.Add(ref scanlineSpanRef, o + (2 * bytesPerSample)); rgb.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
pixel.FromRgb24(rgb); pixel.FromRgb24(rgb);
Unsafe.Add(ref rowSpanRef, x) = pixel; Unsafe.Add(ref rowSpanRef, x) = pixel;
@ -494,7 +506,8 @@ internal static class PngScanlineProcessor
if (header.BitDepth == 16) if (header.BitDepth == 16)
{ {
Rgba64 rgba64 = default; Rgba64 rgba64 = default;
for (int x = 0, o = 0; x < header.Width; x++, o += bytesPerPixel) int o = 0;
for (nuint x = 0; x < (uint)header.Width; x++, o += bytesPerPixel)
{ {
rgba64.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample)); rgba64.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
rgba64.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample)); rgba64.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
@ -515,8 +528,8 @@ internal static class PngScanlineProcessor
in PngHeader header, in PngHeader header,
ReadOnlySpan<byte> scanlineSpan, ReadOnlySpan<byte> scanlineSpan,
Span<TPixel> rowSpan, Span<TPixel> rowSpan,
int pixelOffset, uint pixelOffset,
int increment, uint increment,
int bytesPerPixel, int bytesPerPixel,
int bytesPerSample) int bytesPerSample)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
@ -528,7 +541,8 @@ internal static class PngScanlineProcessor
if (header.BitDepth == 16) if (header.BitDepth == 16)
{ {
Rgba64 rgba64 = default; Rgba64 rgba64 = default;
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o += bytesPerPixel) int o = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; x += increment, o += bytesPerPixel)
{ {
rgba64.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample)); rgba64.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
rgba64.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample)); rgba64.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
@ -542,12 +556,13 @@ internal static class PngScanlineProcessor
else else
{ {
Rgba32 rgba = default; Rgba32 rgba = default;
for (int x = pixelOffset, o = 0; x < header.Width; x += increment, o += bytesPerPixel) int o = 0;
for (nuint x = pixelOffset; x < (uint)header.Width; x += increment, o += bytesPerPixel)
{ {
rgba.R = Unsafe.Add(ref scanlineSpanRef, o); rgba.R = Unsafe.Add(ref scanlineSpanRef, (uint)o);
rgba.G = Unsafe.Add(ref scanlineSpanRef, o + bytesPerSample); rgba.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
rgba.B = Unsafe.Add(ref scanlineSpanRef, o + (2 * bytesPerSample)); rgba.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
rgba.A = Unsafe.Add(ref scanlineSpanRef, o + (3 * bytesPerSample)); rgba.A = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (3 * bytesPerSample)));
pixel.FromRgba32(rgba); pixel.FromRgba32(rgba);
Unsafe.Add(ref rowSpanRef, x) = pixel; Unsafe.Add(ref rowSpanRef, x) = pixel;

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

@ -4,6 +4,7 @@
using System.Buffers; using System.Buffers;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata;
@ -429,11 +430,11 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
if (this.fileHeader.ImageType == TgaImageType.BlackAndWhite) if (this.fileHeader.ImageType == TgaImageType.BlackAndWhite)
{ {
color.FromLa16(Unsafe.As<byte, La16>(ref this.scratchBuffer[0])); color.FromLa16(Unsafe.As<byte, La16>(ref MemoryMarshal.GetArrayDataReference(this.scratchBuffer)));
} }
else else
{ {
color.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref this.scratchBuffer[0])); color.FromBgra5551(Unsafe.As<byte, Bgra5551>(ref MemoryMarshal.GetArrayDataReference(this.scratchBuffer)));
} }
pixelSpan[x] = color; pixelSpan[x] = color;
@ -695,7 +696,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 this.scratchBuffer[0])); color.FromBgr24(Unsafe.As<byte, Bgr24>(ref MemoryMarshal.GetArrayDataReference(this.scratchBuffer)));
pixelSpan[x] = color; pixelSpan[x] = color;
} }

2
src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs

@ -78,7 +78,7 @@ internal sealed class T6TiffCompression : TiffBaseDecompressor
nint bitPos = Numerics.Modulo8(bitsWritten); nint bitPos = Numerics.Modulo8(bitsWritten);
nint bufferPos = bitsWritten / 8; nint bufferPos = bitsWritten / 8;
ref byte scanLineRef = ref MemoryMarshal.GetReference(scanLine); ref byte scanLineRef = ref MemoryMarshal.GetReference(scanLine);
for (nint i = 0; i < scanLine.Length; i++) for (nuint i = 0; i < (uint)scanLine.Length; i++)
{ {
if (Unsafe.Add(ref scanLineRef, i) != this.white) if (Unsafe.Add(ref scanLineRef, i) != this.white)
{ {

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

@ -18,21 +18,21 @@ internal class BlackIsZero1TiffColor<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)
{ {
nint offset = 0; nuint offset = 0;
var colorBlack = default(TPixel); TPixel colorBlack = default;
var colorWhite = default(TPixel); TPixel colorWhite = default;
colorBlack.FromRgba32(Color.Black); colorBlack.FromRgba32(Color.Black);
colorWhite.FromRgba32(Color.White); colorWhite.FromRgba32(Color.White);
ref byte dataRef = ref MemoryMarshal.GetReference(data); ref byte dataRef = ref MemoryMarshal.GetReference(data);
for (nint y = top; y < top + height; y++) for (nuint y = (uint)top; y < (uint)(top + height); y++)
{ {
Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan((int)y); Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan((int)y);
ref TPixel pixelRowRef = ref MemoryMarshal.GetReference(pixelRowSpan); ref TPixel pixelRowRef = ref MemoryMarshal.GetReference(pixelRowSpan);
for (nint x = left; x < left + width; x += 8) for (nuint x = (uint)left; x < (uint)(left + width); x += 8)
{ {
byte b = Unsafe.Add(ref dataRef, offset++); byte b = Unsafe.Add(ref dataRef, offset++);
nint maxShift = Math.Min(left + width - x, 8); nuint maxShift = Math.Min((uint)(left + width) - x, 8);
if (maxShift == 8) if (maxShift == 8)
{ {
@ -70,9 +70,9 @@ internal class BlackIsZero1TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
} }
else else
{ {
for (int shift = 0; shift < maxShift; shift++) for (nuint shift = 0; shift < maxShift; shift++)
{ {
int bit = (b >> (7 - shift)) & 1; int bit = (b >> (7 - (int)shift)) & 1;
ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift); ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift);
pixel = bit == 0 ? colorBlack : colorWhite; pixel = bit == 0 ? colorBlack : colorWhite;

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

@ -17,21 +17,21 @@ internal class WhiteIsZero1TiffColor<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)
{ {
nint offset = 0; nuint offset = 0;
var colorBlack = default(TPixel); var colorBlack = default(TPixel);
var colorWhite = default(TPixel); var colorWhite = default(TPixel);
colorBlack.FromRgba32(Color.Black); colorBlack.FromRgba32(Color.Black);
colorWhite.FromRgba32(Color.White); colorWhite.FromRgba32(Color.White);
ref byte dataRef = ref MemoryMarshal.GetReference(data); ref byte dataRef = ref MemoryMarshal.GetReference(data);
for (nint y = top; y < top + height; y++) for (nuint y = (uint)top; y < (uint)(top + height); y++)
{ {
Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan((int)y); Span<TPixel> pixelRowSpan = pixels.DangerousGetRowSpan((int)y);
ref TPixel pixelRowRef = ref MemoryMarshal.GetReference(pixelRowSpan); ref TPixel pixelRowRef = ref MemoryMarshal.GetReference(pixelRowSpan);
for (nint x = left; x < left + width; x += 8) for (nuint x = (uint)left; x < (uint)(left + width); x += 8)
{ {
byte b = Unsafe.Add(ref dataRef, offset++); byte b = Unsafe.Add(ref dataRef, offset++);
nint maxShift = Math.Min(left + width - x, 8); nuint maxShift = Math.Min((uint)(left + width) - x, 8);
if (maxShift == 8) if (maxShift == 8)
{ {
@ -69,9 +69,9 @@ internal class WhiteIsZero1TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
} }
else else
{ {
for (int shift = 0; shift < maxShift; shift++) for (nuint shift = 0; shift < maxShift; shift++)
{ {
int bit = (b >> (7 - shift)) & 1; int bit = (b >> (7 - (int)shift)) & 1;
ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift); ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift);
pixel = bit == 0 ? colorWhite : colorBlack; pixel = bit == 0 ? colorWhite : colorBlack;

2
src/ImageSharp/Formats/Tiff/Writers/TiffBaseColorWriter{TPixel}.cs

@ -29,7 +29,7 @@ internal abstract class TiffBaseColorWriter<TPixel> : IDisposable
/// <summary> /// <summary>
/// Gets the bytes per row. /// Gets the bytes per row.
/// </summary> /// </summary>
public int BytesPerRow => ((this.Image.Width * this.BitsPerPixel) + 7) / 8; public int BytesPerRow => (int)(((uint)(this.Image.Width * this.BitsPerPixel) + 7) / 8);
protected ImageFrame<TPixel> Image { get; } protected ImageFrame<TPixel> Image { get; }

27
src/ImageSharp/Formats/Webp/AlphaDecoder.cs

@ -319,10 +319,11 @@ internal class AlphaDecoder : IDisposable
return; return;
} }
nint i; nuint i;
Vector128<int> last = Vector128<int>.Zero.WithElement(0, dst[0]); Vector128<int> last = Vector128<int>.Zero.WithElement(0, dst[0]);
ref byte srcRef = ref MemoryMarshal.GetReference(input); ref byte srcRef = ref MemoryMarshal.GetReference(input);
for (i = 1; i + 8 <= width; i += 8) ref byte dstRef = ref MemoryMarshal.GetReference(dst);
for (i = 1; i <= (uint)width - 8; i += 8)
{ {
Vector128<long> a0 = Vector128.Create(Unsafe.As<byte, long>(ref Unsafe.Add(ref srcRef, i)), 0); Vector128<long> a0 = Vector128.Create(Unsafe.As<byte, long>(ref Unsafe.Add(ref srcRef, i)), 0);
Vector128<byte> a1 = Sse2.Add(a0.AsByte(), last.AsByte()); Vector128<byte> a1 = Sse2.Add(a0.AsByte(), last.AsByte());
@ -333,12 +334,12 @@ internal class AlphaDecoder : IDisposable
Vector128<byte> a6 = Sse2.ShiftLeftLogical128BitLane(a5, 4); Vector128<byte> a6 = Sse2.ShiftLeftLogical128BitLane(a5, 4);
Vector128<byte> a7 = Sse2.Add(a5, a6); Vector128<byte> a7 = Sse2.Add(a5, a6);
ref byte outputRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(dst), i); ref byte outputRef = ref Unsafe.Add(ref dstRef, i);
Unsafe.As<byte, Vector64<byte>>(ref outputRef) = a7.GetLower(); Unsafe.As<byte, Vector64<byte>>(ref outputRef) = a7.GetLower();
last = Sse2.ShiftRightLogical(a7.AsInt64(), 56).AsInt32(); last = Sse2.ShiftRightLogical(a7.AsInt64(), 56).AsInt32();
} }
for (; i < width; ++i) for (; i < (uint)width; ++i)
{ {
dst[(int)i] = (byte)(input[(int)i] + dst[(int)i - 1]); dst[(int)i] = (byte)(input[(int)i] + dst[(int)i - 1]);
} }
@ -364,20 +365,24 @@ internal class AlphaDecoder : IDisposable
} }
else if (Avx2.IsSupported) else if (Avx2.IsSupported)
{ {
nint i; ref byte inputRef = ref MemoryMarshal.GetReference(input);
ref byte prevRef = ref MemoryMarshal.GetReference(prev);
ref byte dstRef = ref MemoryMarshal.GetReference(dst);
nuint i;
int maxPos = width & ~31; int maxPos = width & ~31;
for (i = 0; i < maxPos; i += 32) for (i = 0; i < (uint)maxPos; i += 32)
{ {
Vector256<int> a0 = Unsafe.As<byte, Vector256<int>>(ref Unsafe.Add(ref MemoryMarshal.GetReference(input), i)); Vector256<int> a0 = Unsafe.As<byte, Vector256<int>>(ref Unsafe.Add(ref inputRef, i));
Vector256<int> b0 = Unsafe.As<byte, Vector256<int>>(ref Unsafe.Add(ref MemoryMarshal.GetReference(prev), i)); Vector256<int> b0 = Unsafe.As<byte, Vector256<int>>(ref Unsafe.Add(ref prevRef, i));
Vector256<byte> c0 = Avx2.Add(a0.AsByte(), b0.AsByte()); Vector256<byte> c0 = Avx2.Add(a0.AsByte(), b0.AsByte());
ref byte outputRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(dst), i); ref byte outputRef = ref Unsafe.Add(ref dstRef, i);
Unsafe.As<byte, Vector256<byte>>(ref outputRef) = c0; Unsafe.As<byte, Vector256<byte>>(ref outputRef) = c0;
} }
for (; i < width; i++) for (; i < (uint)width; i++)
{ {
dst[(int)i] = (byte)(prev[(int)i] + input[(int)i]); Unsafe.Add(ref dstRef, i) = (byte)(Unsafe.Add(ref prevRef, i) + Unsafe.Add(ref inputRef, i));
} }
} }
else else

2
src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs

@ -457,7 +457,7 @@ internal class Vp8BitWriter : BitWriterBase
this.Finish(); this.Finish();
uint numBytes = (uint)this.NumBytes(); uint numBytes = (uint)this.NumBytes();
int mbSize = this.enc.Mbw * this.enc.Mbh; int mbSize = this.enc.Mbw * this.enc.Mbh;
int expectedSize = mbSize * 7 / 8; int expectedSize = (int)((uint)mbSize * 7 / 8);
Vp8BitWriter bitWriterPartZero = new(expectedSize, this.enc); Vp8BitWriter bitWriterPartZero = new(expectedSize, this.enc);

4
src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs

@ -38,7 +38,7 @@ internal static class BackwardReferenceEncoder
int width, int width,
int height, int height,
ReadOnlySpan<uint> bgra, ReadOnlySpan<uint> bgra,
int quality, uint quality,
int lz77TypesToTry, int lz77TypesToTry,
ref int cacheBits, ref int cacheBits,
MemoryAllocator memoryAllocator, MemoryAllocator memoryAllocator,
@ -123,7 +123,7 @@ internal static class BackwardReferenceEncoder
/// The local color cache is also disabled for the lower (smaller then 25) quality. /// The local color cache is also disabled for the lower (smaller then 25) quality.
/// </summary> /// </summary>
/// <returns>Best cache size.</returns> /// <returns>Best cache size.</returns>
private static int CalculateBestCacheSize(ReadOnlySpan<uint> bgra, int quality, Vp8LBackwardRefs refs, int bestCacheBits) private static int CalculateBestCacheSize(ReadOnlySpan<uint> bgra, uint quality, Vp8LBackwardRefs refs, int bestCacheBits)
{ {
int cacheBitsMax = quality <= 25 ? 0 : bestCacheBits; int cacheBitsMax = quality <= 25 ? 0 : bestCacheBits;
if (cacheBitsMax == 0) if (cacheBitsMax == 0)

24
src/ImageSharp/Formats/Webp/Lossless/ColorSpaceTransformUtils.cs

@ -27,10 +27,10 @@ internal static class ColorSpaceTransformUtils
{ {
Span<uint> srcSpan = bgra[(y * stride)..]; Span<uint> srcSpan = bgra[(y * stride)..];
ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan); ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan);
for (nint x = 0; x <= tileWidth - span; x += span) for (nuint x = 0; x <= (uint)tileWidth - span; x += span)
{ {
nint input0Idx = x; nuint input0Idx = x;
nint input1Idx = x + (span / 2); nuint input1Idx = x + (span / 2);
Vector256<byte> input0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte(); Vector256<byte> input0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte();
Vector256<byte> input1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte(); Vector256<byte> input1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte();
Vector256<byte> r0 = Avx2.Shuffle(input0, collectColorBlueTransformsShuffleLowMask256); Vector256<byte> r0 = Avx2.Shuffle(input0, collectColorBlueTransformsShuffleLowMask256);
@ -77,10 +77,10 @@ internal static class ColorSpaceTransformUtils
{ {
Span<uint> srcSpan = bgra[(y * stride)..]; Span<uint> srcSpan = bgra[(y * stride)..];
ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan); ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan);
for (nint x = 0; x <= tileWidth - span; x += span) for (nuint x = 0; (int)x <= tileWidth - span; x += span)
{ {
nint input0Idx = x; nuint input0Idx = x;
nint input1Idx = x + (span / 2); nuint input1Idx = x + (span / 2);
Vector128<byte> input0 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte(); Vector128<byte> input0 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte();
Vector128<byte> input1 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte(); Vector128<byte> input1 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte();
Vector128<byte> r0 = Ssse3.Shuffle(input0, collectColorBlueTransformsShuffleLowMask); Vector128<byte> r0 = Ssse3.Shuffle(input0, collectColorBlueTransformsShuffleLowMask);
@ -146,10 +146,10 @@ internal static class ColorSpaceTransformUtils
{ {
Span<uint> srcSpan = bgra[(y * stride)..]; Span<uint> srcSpan = bgra[(y * stride)..];
ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan); ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan);
for (nint x = 0; x <= tileWidth - span; x += span) for (nuint x = 0; x <= (uint)tileWidth - span; x += span)
{ {
nint input0Idx = x; nuint input0Idx = x;
nint input1Idx = x + (span / 2); nuint input1Idx = x + (span / 2);
Vector256<byte> input0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte(); Vector256<byte> input0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte();
Vector256<byte> input1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte(); Vector256<byte> input1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte();
Vector256<byte> g0 = Avx2.And(input0, collectColorRedTransformsGreenMask256); // 0 0 | g 0 Vector256<byte> g0 = Avx2.And(input0, collectColorRedTransformsGreenMask256); // 0 0 | g 0
@ -189,10 +189,10 @@ internal static class ColorSpaceTransformUtils
{ {
Span<uint> srcSpan = bgra[(y * stride)..]; Span<uint> srcSpan = bgra[(y * stride)..];
ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan); ref uint inputRef = ref MemoryMarshal.GetReference(srcSpan);
for (nint x = 0; x <= tileWidth - span; x += span) for (nuint x = 0; (int)x <= tileWidth - span; x += span)
{ {
nint input0Idx = x; nuint input0Idx = x;
nint input1Idx = x + (span / 2); nuint input1Idx = x + (span / 2);
Vector128<byte> input0 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte(); Vector128<byte> input0 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input0Idx)).AsByte();
Vector128<byte> input1 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte(); Vector128<byte> input1 = Unsafe.As<uint, Vector128<uint>>(ref Unsafe.Add(ref inputRef, input1Idx)).AsByte();
Vector128<byte> g0 = Sse2.And(input0, collectColorRedTransformsGreenMask); // 0 0 | g 0 Vector128<byte> g0 = Sse2.And(input0, collectColorRedTransformsGreenMask); // 0 0 | g 0

8
src/ImageSharp/Formats/Webp/Lossless/HistogramEncoder.cs

@ -27,7 +27,7 @@ internal class HistogramEncoder
private const ushort InvalidHistogramSymbol = ushort.MaxValue; private const ushort InvalidHistogramSymbol = ushort.MaxValue;
public static void GetHistoImageSymbols(int xSize, int ySize, Vp8LBackwardRefs refs, int quality, int histoBits, int cacheBits, List<Vp8LHistogram> imageHisto, Vp8LHistogram tmpHisto, ushort[] histogramSymbols) public static void GetHistoImageSymbols(int xSize, int ySize, Vp8LBackwardRefs refs, uint quality, int histoBits, int cacheBits, List<Vp8LHistogram> imageHisto, Vp8LHistogram tmpHisto, ushort[] histogramSymbols)
{ {
int histoXSize = histoBits > 0 ? LosslessUtils.SubSampleSize(xSize, histoBits) : 1; int histoXSize = histoBits > 0 ? LosslessUtils.SubSampleSize(xSize, histoBits) : 1;
int histoYSize = histoBits > 0 ? LosslessUtils.SubSampleSize(ySize, histoBits) : 1; int histoYSize = histoBits > 0 ? LosslessUtils.SubSampleSize(ySize, histoBits) : 1;
@ -316,7 +316,7 @@ internal class HistogramEncoder
int triesWithNoSuccess = 0; int triesWithNoSuccess = 0;
int numUsed = histograms.Count(h => h != null); int numUsed = histograms.Count(h => h != null);
int outerIters = numUsed; int outerIters = numUsed;
int numTriesNoSuccess = outerIters / 2; int numTriesNoSuccess = (int)((uint)outerIters / 2);
var stats = new Vp8LStreaks(); var stats = new Vp8LStreaks();
var bitsEntropy = new Vp8LBitEntropy(); var bitsEntropy = new Vp8LBitEntropy();
@ -346,7 +346,7 @@ internal class HistogramEncoder
for (int iter = 0; iter < outerIters && numUsed >= minClusterSize && ++triesWithNoSuccess < numTriesNoSuccess; iter++) for (int iter = 0; iter < outerIters && numUsed >= minClusterSize && ++triesWithNoSuccess < numTriesNoSuccess; iter++)
{ {
double bestCost = histoPriorityList.Count == 0 ? 0.0d : histoPriorityList[0].CostDiff; double bestCost = histoPriorityList.Count == 0 ? 0.0d : histoPriorityList[0].CostDiff;
int numTries = numUsed / 2; int numTries = (int)((uint)numUsed / 2);
uint randRange = (uint)((numUsed - 1) * numUsed); uint randRange = (uint)((numUsed - 1) * numUsed);
// Pick random samples. // Pick random samples.
@ -660,7 +660,7 @@ internal class HistogramEncoder
output.TrivialSymbol = a.TrivialSymbol == b.TrivialSymbol ? a.TrivialSymbol : NonTrivialSym; output.TrivialSymbol = a.TrivialSymbol == b.TrivialSymbol ? a.TrivialSymbol : NonTrivialSym;
} }
private static double GetCombineCostFactor(int histoSize, int quality) private static double GetCombineCostFactor(int histoSize, uint quality)
{ {
double combineCostFactor = 0.16d; double combineCostFactor = 0.16d;
if (quality < 90) if (quality < 90)

4
src/ImageSharp/Formats/Webp/Lossless/HuffmanUtils.cs

@ -100,7 +100,7 @@ internal static class HuffmanUtils
uint k; uint k;
// The stride must end, collapse what we have, if we have enough (4). // The stride must end, collapse what we have, if we have enough (4).
uint count = (uint)((sum + (stride / 2)) / stride); uint count = (sum + ((uint)stride / 2)) / (uint)stride;
if (count < 1) if (count < 1)
{ {
count = 1; count = 1;
@ -144,7 +144,7 @@ internal static class HuffmanUtils
sum += counts[i]; sum += counts[i];
if (stride >= 4) if (stride >= 4)
{ {
limit = (uint)((sum + (stride / 2)) / stride); limit = (sum + ((uint)stride / 2)) / (uint)stride;
} }
} }
} }

98
src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs

@ -51,7 +51,7 @@ internal static unsafe class LosslessUtils
ref uint array1Ref = ref MemoryMarshal.GetReference(array1); ref uint array1Ref = ref MemoryMarshal.GetReference(array1);
ref uint array2Ref = ref MemoryMarshal.GetReference(array2); ref uint array2Ref = ref MemoryMarshal.GetReference(array2);
while (matchLen < length && Unsafe.Add(ref array1Ref, matchLen) == Unsafe.Add(ref array2Ref, matchLen)) while (matchLen < length && Unsafe.Add(ref array1Ref, (uint)matchLen) == Unsafe.Add(ref array2Ref, (uint)matchLen))
{ {
matchLen++; matchLen++;
} }
@ -94,49 +94,53 @@ internal static unsafe class LosslessUtils
/// <param name="pixelData">The pixel data to apply the transformation.</param> /// <param name="pixelData">The pixel data to apply the transformation.</param>
public static void AddGreenToBlueAndRed(Span<uint> pixelData) public static void AddGreenToBlueAndRed(Span<uint> pixelData)
{ {
if (Avx2.IsSupported) if (Avx2.IsSupported && pixelData.Length >= 8)
{ {
Vector256<byte> addGreenToBlueAndRedMaskAvx2 = Vector256.Create(1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255, 17, 255, 17, 255, 21, 255, 21, 255, 25, 255, 25, 255, 29, 255, 29, 255); Vector256<byte> addGreenToBlueAndRedMaskAvx2 = Vector256.Create(1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255, 17, 255, 17, 255, 21, 255, 21, 255, 25, 255, 25, 255, 29, 255, 29, 255);
int numPixels = pixelData.Length; nuint numPixels = (uint)pixelData.Length;
nint i; nuint i = 0;
for (i = 0; i <= numPixels - 8; i += 8) do
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i);
Vector256<byte> input = Unsafe.As<uint, Vector256<uint>>(ref pos).AsByte(); Vector256<byte> input = Unsafe.As<uint, Vector256<uint>>(ref pos).AsByte();
Vector256<byte> in0g0g = Avx2.Shuffle(input, addGreenToBlueAndRedMaskAvx2); Vector256<byte> in0g0g = Avx2.Shuffle(input, addGreenToBlueAndRedMaskAvx2);
Vector256<byte> output = Avx2.Add(input, in0g0g); Vector256<byte> output = Avx2.Add(input, in0g0g);
Unsafe.As<uint, Vector256<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector256<uint>>(ref pos) = output.AsUInt32();
i += 8;
} }
while (i <= numPixels - 8);
if (i != numPixels) if (i != numPixels)
{ {
AddGreenToBlueAndRedScalar(pixelData[(int)i..]); AddGreenToBlueAndRedScalar(pixelData[(int)i..]);
} }
} }
else if (Ssse3.IsSupported) else if (Ssse3.IsSupported && pixelData.Length >= 4)
{ {
Vector128<byte> addGreenToBlueAndRedMaskSsse3 = Vector128.Create(1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255); Vector128<byte> addGreenToBlueAndRedMaskSsse3 = Vector128.Create(1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255);
int numPixels = pixelData.Length; nuint numPixels = (uint)pixelData.Length;
nint i; nuint i = 0;
for (i = 0; i <= numPixels - 4; i += 4) do
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i);
Vector128<byte> input = Unsafe.As<uint, Vector128<uint>>(ref pos).AsByte(); Vector128<byte> input = Unsafe.As<uint, Vector128<uint>>(ref pos).AsByte();
Vector128<byte> in0g0g = Ssse3.Shuffle(input, addGreenToBlueAndRedMaskSsse3); Vector128<byte> in0g0g = Ssse3.Shuffle(input, addGreenToBlueAndRedMaskSsse3);
Vector128<byte> output = Sse2.Add(input, in0g0g); Vector128<byte> output = Sse2.Add(input, in0g0g);
Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32();
i += 4;
} }
while (i <= numPixels - 4);
if (i != numPixels) if (i != numPixels)
{ {
AddGreenToBlueAndRedScalar(pixelData[(int)i..]); AddGreenToBlueAndRedScalar(pixelData[(int)i..]);
} }
} }
else if (Sse2.IsSupported) else if (Sse2.IsSupported && pixelData.Length >= 4)
{ {
int numPixels = pixelData.Length; nuint numPixels = (uint)pixelData.Length;
nint i; nuint i = 0;
for (i = 0; i <= numPixels - 4; i += 4) do
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i);
Vector128<byte> input = Unsafe.As<uint, Vector128<uint>>(ref pos).AsByte(); Vector128<byte> input = Unsafe.As<uint, Vector128<uint>>(ref pos).AsByte();
@ -145,7 +149,9 @@ internal static unsafe class LosslessUtils
Vector128<ushort> c = Sse2.ShuffleHigh(b, SimdUtils.Shuffle.MMShuffle2200); // 0g0g Vector128<ushort> c = Sse2.ShuffleHigh(b, SimdUtils.Shuffle.MMShuffle2200); // 0g0g
Vector128<byte> output = Sse2.Add(input.AsByte(), c.AsByte()); Vector128<byte> output = Sse2.Add(input.AsByte(), c.AsByte());
Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32();
i += 4;
} }
while (i <= numPixels - 4);
if (i != numPixels) if (i != numPixels)
{ {
@ -174,49 +180,53 @@ internal static unsafe class LosslessUtils
public static void SubtractGreenFromBlueAndRed(Span<uint> pixelData) public static void SubtractGreenFromBlueAndRed(Span<uint> pixelData)
{ {
if (Avx2.IsSupported) if (Avx2.IsSupported && pixelData.Length >= 8)
{ {
Vector256<byte> subtractGreenFromBlueAndRedMaskAvx2 = Vector256.Create(1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255, 17, 255, 17, 255, 21, 255, 21, 255, 25, 255, 25, 255, 29, 255, 29, 255); Vector256<byte> subtractGreenFromBlueAndRedMaskAvx2 = Vector256.Create(1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255, 17, 255, 17, 255, 21, 255, 21, 255, 25, 255, 25, 255, 29, 255, 29, 255);
int numPixels = pixelData.Length; nuint numPixels = (uint)pixelData.Length;
nint i; nuint i = 0;
for (i = 0; i <= numPixels - 8; i += 8) do
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i);
Vector256<byte> input = Unsafe.As<uint, Vector256<uint>>(ref pos).AsByte(); Vector256<byte> input = Unsafe.As<uint, Vector256<uint>>(ref pos).AsByte();
Vector256<byte> in0g0g = Avx2.Shuffle(input, subtractGreenFromBlueAndRedMaskAvx2); Vector256<byte> in0g0g = Avx2.Shuffle(input, subtractGreenFromBlueAndRedMaskAvx2);
Vector256<byte> output = Avx2.Subtract(input, in0g0g); Vector256<byte> output = Avx2.Subtract(input, in0g0g);
Unsafe.As<uint, Vector256<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector256<uint>>(ref pos) = output.AsUInt32();
i += 8;
} }
while (i <= numPixels - 8);
if (i != numPixels) if (i != numPixels)
{ {
SubtractGreenFromBlueAndRedScalar(pixelData[(int)i..]); SubtractGreenFromBlueAndRedScalar(pixelData[(int)i..]);
} }
} }
else if (Ssse3.IsSupported) else if (Ssse3.IsSupported && pixelData.Length >= 4)
{ {
Vector128<byte> subtractGreenFromBlueAndRedMaskSsse3 = Vector128.Create(1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255); Vector128<byte> subtractGreenFromBlueAndRedMaskSsse3 = Vector128.Create(1, 255, 1, 255, 5, 255, 5, 255, 9, 255, 9, 255, 13, 255, 13, 255);
int numPixels = pixelData.Length; nuint numPixels = (uint)pixelData.Length;
nint i; nuint i = 0;
for (i = 0; i <= numPixels - 4; i += 4) do
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i);
Vector128<byte> input = Unsafe.As<uint, Vector128<uint>>(ref pos).AsByte(); Vector128<byte> input = Unsafe.As<uint, Vector128<uint>>(ref pos).AsByte();
Vector128<byte> in0g0g = Ssse3.Shuffle(input, subtractGreenFromBlueAndRedMaskSsse3); Vector128<byte> in0g0g = Ssse3.Shuffle(input, subtractGreenFromBlueAndRedMaskSsse3);
Vector128<byte> output = Sse2.Subtract(input, in0g0g); Vector128<byte> output = Sse2.Subtract(input, in0g0g);
Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32();
i += 4;
} }
while (i <= numPixels - 4);
if (i != numPixels) if (i != numPixels)
{ {
SubtractGreenFromBlueAndRedScalar(pixelData[(int)i..]); SubtractGreenFromBlueAndRedScalar(pixelData[(int)i..]);
} }
} }
else if (Sse2.IsSupported) else if (Sse2.IsSupported && pixelData.Length >= 4)
{ {
int numPixels = pixelData.Length; nuint numPixels = (uint)pixelData.Length;
nint i; nuint i = 0;
for (i = 0; i <= numPixels - 4; i += 4) do
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), i);
Vector128<byte> input = Unsafe.As<uint, Vector128<uint>>(ref pos).AsByte(); Vector128<byte> input = Unsafe.As<uint, Vector128<uint>>(ref pos).AsByte();
@ -225,7 +235,9 @@ internal static unsafe class LosslessUtils
Vector128<ushort> c = Sse2.ShuffleHigh(b, SimdUtils.Shuffle.MMShuffle2200); // 0g0g Vector128<ushort> c = Sse2.ShuffleHigh(b, SimdUtils.Shuffle.MMShuffle2200); // 0g0g
Vector128<byte> output = Sse2.Subtract(input.AsByte(), c.AsByte()); Vector128<byte> output = Sse2.Subtract(input.AsByte(), c.AsByte());
Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32();
i += 4;
} }
while (i <= numPixels - 4);
if (i != numPixels) if (i != numPixels)
{ {
@ -372,8 +384,8 @@ internal static unsafe class LosslessUtils
Vector256<int> multsrb = MkCst32(Cst5b(m.GreenToRed), Cst5b(m.GreenToBlue)); Vector256<int> multsrb = MkCst32(Cst5b(m.GreenToRed), Cst5b(m.GreenToBlue));
Vector256<int> multsb2 = MkCst32(Cst5b(m.RedToBlue), 0); Vector256<int> multsb2 = MkCst32(Cst5b(m.RedToBlue), 0);
nint idx; nuint idx = 0;
for (idx = 0; idx <= numPixels - 8; idx += 8) do
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), idx); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), idx);
Vector256<uint> input = Unsafe.As<uint, Vector256<uint>>(ref pos); Vector256<uint> input = Unsafe.As<uint, Vector256<uint>>(ref pos);
@ -388,21 +400,23 @@ internal static unsafe class LosslessUtils
Vector256<byte> i = Avx2.And(h, transformColorRedBlueMask256); Vector256<byte> i = Avx2.And(h, transformColorRedBlueMask256);
Vector256<byte> output = Avx2.Subtract(input.AsByte(), i); Vector256<byte> output = Avx2.Subtract(input.AsByte(), i);
Unsafe.As<uint, Vector256<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector256<uint>>(ref pos) = output.AsUInt32();
idx += 8;
} }
while (idx <= (uint)numPixels - 8);
if (idx != numPixels) if (idx != (uint)numPixels)
{ {
TransformColorScalar(m, pixelData[(int)idx..], numPixels - (int)idx); TransformColorScalar(m, pixelData[(int)idx..], numPixels - (int)idx);
} }
} }
else if (Sse2.IsSupported) else if (Sse2.IsSupported && numPixels >= 4)
{ {
Vector128<byte> transformColorAlphaGreenMask = Vector128.Create(0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255); Vector128<byte> transformColorAlphaGreenMask = Vector128.Create(0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255);
Vector128<byte> transformColorRedBlueMask = Vector128.Create(255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0); Vector128<byte> transformColorRedBlueMask = Vector128.Create(255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0);
Vector128<int> multsrb = MkCst16(Cst5b(m.GreenToRed), Cst5b(m.GreenToBlue)); Vector128<int> multsrb = MkCst16(Cst5b(m.GreenToRed), Cst5b(m.GreenToBlue));
Vector128<int> multsb2 = MkCst16(Cst5b(m.RedToBlue), 0); Vector128<int> multsb2 = MkCst16(Cst5b(m.RedToBlue), 0);
nint idx; nuint idx = 0;
for (idx = 0; idx <= numPixels - 4; idx += 4) do
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), idx); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), idx);
Vector128<uint> input = Unsafe.As<uint, Vector128<uint>>(ref pos); Vector128<uint> input = Unsafe.As<uint, Vector128<uint>>(ref pos);
@ -417,9 +431,11 @@ internal static unsafe class LosslessUtils
Vector128<byte> i = Sse2.And(h, transformColorRedBlueMask); Vector128<byte> i = Sse2.And(h, transformColorRedBlueMask);
Vector128<byte> output = Sse2.Subtract(input.AsByte(), i); Vector128<byte> output = Sse2.Subtract(input.AsByte(), i);
Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32();
idx += 4;
} }
while ((int)idx <= numPixels - 4);
if (idx != numPixels) if ((int)idx != numPixels)
{ {
TransformColorScalar(m, pixelData[(int)idx..], numPixels - (int)idx); TransformColorScalar(m, pixelData[(int)idx..], numPixels - (int)idx);
} }
@ -460,8 +476,8 @@ internal static unsafe class LosslessUtils
Vector256<byte> transformColorInverseAlphaGreenMask256 = Vector256.Create(0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255); Vector256<byte> transformColorInverseAlphaGreenMask256 = Vector256.Create(0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255);
Vector256<int> multsrb = MkCst32(Cst5b(m.GreenToRed), Cst5b(m.GreenToBlue)); Vector256<int> multsrb = MkCst32(Cst5b(m.GreenToRed), Cst5b(m.GreenToBlue));
Vector256<int> multsb2 = MkCst32(Cst5b(m.RedToBlue), 0); Vector256<int> multsb2 = MkCst32(Cst5b(m.RedToBlue), 0);
nint idx; nuint idx;
for (idx = 0; idx <= pixelData.Length - 8; idx += 8) for (idx = 0; idx <= (uint)pixelData.Length - 8; idx += 8)
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), idx); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), idx);
Vector256<uint> input = Unsafe.As<uint, Vector256<uint>>(ref pos); Vector256<uint> input = Unsafe.As<uint, Vector256<uint>>(ref pos);
@ -479,19 +495,19 @@ internal static unsafe class LosslessUtils
Unsafe.As<uint, Vector256<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector256<uint>>(ref pos) = output.AsUInt32();
} }
if (idx != pixelData.Length) if (idx != (uint)pixelData.Length)
{ {
TransformColorInverseScalar(m, pixelData[(int)idx..]); TransformColorInverseScalar(m, pixelData[(int)idx..]);
} }
} }
else if (Sse2.IsSupported) else if (Sse2.IsSupported && pixelData.Length >= 4)
{ {
Vector128<byte> transformColorInverseAlphaGreenMask = Vector128.Create(0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255); Vector128<byte> transformColorInverseAlphaGreenMask = Vector128.Create(0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255);
Vector128<int> multsrb = MkCst16(Cst5b(m.GreenToRed), Cst5b(m.GreenToBlue)); Vector128<int> multsrb = MkCst16(Cst5b(m.GreenToRed), Cst5b(m.GreenToBlue));
Vector128<int> multsb2 = MkCst16(Cst5b(m.RedToBlue), 0); Vector128<int> multsb2 = MkCst16(Cst5b(m.RedToBlue), 0);
nint idx; nuint idx;
for (idx = 0; idx <= pixelData.Length - 4; idx += 4) for (idx = 0; idx <= (uint)pixelData.Length - 4; idx += 4)
{ {
ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), idx); ref uint pos = ref Unsafe.Add(ref MemoryMarshal.GetReference(pixelData), idx);
Vector128<uint> input = Unsafe.As<uint, Vector128<uint>>(ref pos); Vector128<uint> input = Unsafe.As<uint, Vector128<uint>>(ref pos);
@ -509,7 +525,7 @@ internal static unsafe class LosslessUtils
Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32(); Unsafe.As<uint, Vector128<uint>>(ref pos) = output.AsUInt32();
} }
if (idx != pixelData.Length) if (idx != (uint)pixelData.Length)
{ {
TransformColorInverseScalar(m, pixelData[(int)idx..]); TransformColorInverseScalar(m, pixelData[(int)idx..]);
} }
@ -740,7 +756,7 @@ internal static unsafe class LosslessUtils
Vector256<int> sumXY256 = Vector256<int>.Zero; Vector256<int> sumXY256 = Vector256<int>.Zero;
Vector256<int> sumX256 = Vector256<int>.Zero; Vector256<int> sumX256 = Vector256<int>.Zero;
ref int tmpRef = ref Unsafe.As<Vector256<int>, int>(ref tmp); ref int tmpRef = ref Unsafe.As<Vector256<int>, int>(ref tmp);
for (nint i = 0; i < 256; i += 8) for (nuint i = 0; i < 256; i += 8)
{ {
Vector256<int> xVec = Unsafe.As<int, Vector256<int>>(ref Unsafe.Add(ref xRef, i)); Vector256<int> xVec = Unsafe.As<int, Vector256<int>>(ref Unsafe.Add(ref xRef, i));
Vector256<int> yVec = Unsafe.As<int, Vector256<int>>(ref Unsafe.Add(ref yRef, i)); Vector256<int> yVec = Unsafe.As<int, Vector256<int>>(ref Unsafe.Add(ref yRef, i));
@ -1440,7 +1456,7 @@ internal static unsafe class LosslessUtils
} }
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private static int AddSubtractComponentHalf(int a, int b) => (int)Clip255((uint)(a + ((a - b) / 2))); private static int AddSubtractComponentHalf(int a, int b) => (int)Clip255((uint)(a + ((a - b) >> 1))); // >> 1 is bit-hack for / 2
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private static int AddSubtractComponentFull(int a, int b, int c) => (int)Clip255((uint)(a + b - c)); private static int AddSubtractComponentFull(int a, int b, int c) => (int)Clip255((uint)(a + b - c));

12
src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs

@ -113,7 +113,7 @@ internal static unsafe class PredictorEncoder
lowEffort); lowEffort);
} }
public static void ColorSpaceTransform(int width, int height, int bits, int quality, Span<uint> bgra, Span<uint> image, Span<int> scratch) public static void ColorSpaceTransform(int width, int height, int bits, uint quality, Span<uint> bgra, Span<uint> image, Span<int> scratch)
{ {
int maxTileSize = 1 << bits; int maxTileSize = 1 << bits;
int tileXSize = LosslessUtils.SubSampleSize(width, bits); int tileXSize = LosslessUtils.SubSampleSize(width, bits);
@ -837,7 +837,7 @@ internal static unsafe class PredictorEncoder
int bits, int bits,
Vp8LMultipliers prevX, Vp8LMultipliers prevX,
Vp8LMultipliers prevY, Vp8LMultipliers prevY,
int quality, uint quality,
int xSize, int xSize,
int ySize, int ySize,
int[] accumulatedRedHisto, int[] accumulatedRedHisto,
@ -871,14 +871,14 @@ internal static unsafe class PredictorEncoder
int tileHeight, int tileHeight,
Vp8LMultipliers prevX, Vp8LMultipliers prevX,
Vp8LMultipliers prevY, Vp8LMultipliers prevY,
int quality, uint quality,
int[] accumulatedRedHisto, int[] accumulatedRedHisto,
ref Vp8LMultipliers bestTx) ref Vp8LMultipliers bestTx)
{ {
int maxIters = 4 + ((7 * quality) >> 8); // in range [4..6] uint maxIters = 4 + ((7 * quality) / 256); // in range [4..6]
int greenToRedBest = 0; int greenToRedBest = 0;
double bestDiff = GetPredictionCostCrossColorRed(argb, stride, scratch, tileWidth, tileHeight, prevX, prevY, greenToRedBest, accumulatedRedHisto); double bestDiff = GetPredictionCostCrossColorRed(argb, stride, scratch, tileWidth, tileHeight, prevX, prevY, greenToRedBest, accumulatedRedHisto);
for (int iter = 0; iter < maxIters; iter++) for (int iter = 0; iter < (int)maxIters; iter++)
{ {
// ColorTransformDelta is a 3.5 bit fixed point, so 32 is equal to // ColorTransformDelta is a 3.5 bit fixed point, so 32 is equal to
// one in color computation. Having initial delta here as 1 is sufficient // one in color computation. Having initial delta here as 1 is sufficient
@ -901,7 +901,7 @@ internal static unsafe class PredictorEncoder
bestTx.GreenToRed = (byte)(greenToRedBest & 0xff); bestTx.GreenToRed = (byte)(greenToRedBest & 0xff);
} }
private static void GetBestGreenRedToBlue(Span<uint> argb, int stride, Span<int> scratch, int tileWidth, int tileHeight, Vp8LMultipliers prevX, Vp8LMultipliers prevY, int quality, int[] accumulatedBlueHisto, ref Vp8LMultipliers bestTx) private static void GetBestGreenRedToBlue(Span<uint> argb, int stride, Span<int> scratch, int tileWidth, int tileHeight, Vp8LMultipliers prevX, Vp8LMultipliers prevY, uint quality, int[] accumulatedBlueHisto, ref Vp8LMultipliers bestTx)
{ {
int iters = (quality < 25) ? 1 : (quality > 50) ? GreenRedToBlueMaxIters : 4; int iters = (quality < 25) ? 1 : (quality > 50) ? GreenRedToBlueMaxIters : 4;
int greenToBlueBest = 0; int greenToBlueBest = 0;

10
src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

@ -57,7 +57,7 @@ internal class Vp8LEncoder : IDisposable
/// <summary> /// <summary>
/// The quality, that will be used to encode the image. /// The quality, that will be used to encode the image.
/// </summary> /// </summary>
private readonly int quality; private readonly uint quality;
/// <summary> /// <summary>
/// Quality/speed trade-off (0=fast, 6=slower-better). /// Quality/speed trade-off (0=fast, 6=slower-better).
@ -110,7 +110,7 @@ internal class Vp8LEncoder : IDisposable
Configuration configuration, Configuration configuration,
int width, int width,
int height, int height,
int quality, uint quality,
bool skipMetadata, bool skipMetadata,
WebpEncodingMethod method, WebpEncodingMethod method,
WebpTransparentColorMode transparentColorMode, WebpTransparentColorMode transparentColorMode,
@ -122,7 +122,7 @@ internal class Vp8LEncoder : IDisposable
this.memoryAllocator = memoryAllocator; this.memoryAllocator = memoryAllocator;
this.configuration = configuration; this.configuration = configuration;
this.quality = Numerics.Clamp(quality, 0, 100); this.quality = Math.Min(quality, 100u);
this.skipMetadata = skipMetadata; this.skipMetadata = skipMetadata;
this.method = method; this.method = method;
this.transparentColorMode = transparentColorMode; this.transparentColorMode = transparentColorMode;
@ -772,7 +772,7 @@ internal class Vp8LEncoder : IDisposable
this.EncodeImageNoHuffman(this.TransformData.GetSpan(), this.HashChain, this.Refs[0], this.Refs[1], transformWidth, transformHeight, this.quality, lowEffort); this.EncodeImageNoHuffman(this.TransformData.GetSpan(), this.HashChain, this.Refs[0], this.Refs[1], transformWidth, transformHeight, this.quality, lowEffort);
} }
private void EncodeImageNoHuffman(Span<uint> bgra, Vp8LHashChain hashChain, Vp8LBackwardRefs refsTmp1, Vp8LBackwardRefs refsTmp2, int width, int height, int quality, bool lowEffort) private void EncodeImageNoHuffman(Span<uint> bgra, Vp8LHashChain hashChain, Vp8LBackwardRefs refsTmp1, Vp8LBackwardRefs refsTmp2, int width, int height, uint quality, bool lowEffort)
{ {
int cacheBits = 0; int cacheBits = 0;
ushort[] histogramSymbols = new ushort[1]; // Only one tree, one symbol. ushort[] histogramSymbols = new ushort[1]; // Only one tree, one symbol.
@ -963,7 +963,7 @@ internal class Vp8LEncoder : IDisposable
else else
{ {
int nBits = BitOperations.Log2((uint)trimmedLength - 2); int nBits = BitOperations.Log2((uint)trimmedLength - 2);
int nBitPairs = (nBits / 2) + 1; int nBitPairs = (int)(((uint)nBits / 2) + 1);
this.bitWriter.PutBits((uint)nBitPairs - 1, 3); this.bitWriter.PutBits((uint)nBitPairs - 1, 3);
this.bitWriter.PutBits((uint)trimmedLength - 2, nBitPairs * 2); this.bitWriter.PutBits((uint)trimmedLength - 2, nBitPairs * 2);
} }

12
src/ImageSharp/Formats/Webp/Lossless/Vp8LHashChain.cs

@ -56,7 +56,7 @@ internal sealed class Vp8LHashChain : IDisposable
/// </summary> /// </summary>
public int Size { get; } public int Size { get; }
public void Fill(ReadOnlySpan<uint> bgra, int quality, int xSize, int ySize, bool lowEffort) public void Fill(ReadOnlySpan<uint> bgra, uint quality, int xSize, int ySize, bool lowEffort)
{ {
int size = xSize * ySize; int size = xSize * ySize;
int iterMax = GetMaxItersForQuality(quality); int iterMax = GetMaxItersForQuality(quality);
@ -272,14 +272,14 @@ internal sealed class Vp8LHashChain : IDisposable
/// <param name="quality">The quality.</param> /// <param name="quality">The quality.</param>
/// <returns>Number of hash chain lookups.</returns> /// <returns>Number of hash chain lookups.</returns>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private static int GetMaxItersForQuality(int quality) => 8 + (quality * quality / 128); private static int GetMaxItersForQuality(uint quality) => (int)(8 + (quality * quality / 128));
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private static int GetWindowSizeForHashChain(int quality, int xSize) private static int GetWindowSizeForHashChain(uint quality, int xSize)
{ {
int maxWindowSize = quality > 75 ? WindowSize int maxWindowSize = quality > 75u ? WindowSize
: quality > 50 ? xSize << 8 : quality > 50u ? xSize << 8
: quality > 25 ? xSize << 6 : quality > 25u ? xSize << 6
: xSize << 4; : xSize << 4;
return maxWindowSize > WindowSize ? WindowSize : maxWindowSize; return maxWindowSize > WindowSize ? WindowSize : maxWindowSize;

33
src/ImageSharp/Formats/Webp/Lossless/Vp8LHistogram.cs

@ -513,33 +513,36 @@ internal sealed class Vp8LHistogram : IDeepCloneable
DebugGuard.MustBeGreaterThanOrEqualTo(b.Length, count, nameof(b.Length)); DebugGuard.MustBeGreaterThanOrEqualTo(b.Length, count, nameof(b.Length));
DebugGuard.MustBeGreaterThanOrEqualTo(output.Length, count, nameof(output.Length)); DebugGuard.MustBeGreaterThanOrEqualTo(output.Length, count, nameof(output.Length));
if (Avx2.IsSupported) if (Avx2.IsSupported && count >= 32)
{ {
ref uint aRef = ref MemoryMarshal.GetReference(a); ref uint aRef = ref MemoryMarshal.GetReference(a);
ref uint bRef = ref MemoryMarshal.GetReference(b); ref uint bRef = ref MemoryMarshal.GetReference(b);
ref uint outputRef = ref MemoryMarshal.GetReference(output); ref uint outputRef = ref MemoryMarshal.GetReference(output);
int i;
for (i = 0; i + 32 <= count; i += 32) nuint idx = 0;
do
{ {
// Load values. // Load values.
Vector256<uint> a0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref aRef, i)); Vector256<uint> a0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref aRef, idx + 0));
Vector256<uint> a1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref aRef, i + 8)); Vector256<uint> a1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref aRef, idx + 8));
Vector256<uint> a2 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref aRef, i + 16)); Vector256<uint> a2 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref aRef, idx + 16));
Vector256<uint> a3 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref aRef, i + 24)); Vector256<uint> a3 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref aRef, idx + 24));
Vector256<uint> b0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref bRef, i)); Vector256<uint> b0 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref bRef, idx + 0));
Vector256<uint> b1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref bRef, i + 8)); Vector256<uint> b1 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref bRef, idx + 8));
Vector256<uint> b2 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref bRef, i + 16)); Vector256<uint> b2 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref bRef, idx + 16));
Vector256<uint> b3 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref bRef, i + 24)); Vector256<uint> b3 = Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref bRef, idx + 24));
// Note we are adding uint32_t's as *signed* int32's (using _mm_add_epi32). But // Note we are adding uint32_t's as *signed* int32's (using _mm_add_epi32). But
// that's ok since the histogram values are less than 1<<28 (max picture count). // that's ok since the histogram values are less than 1<<28 (max picture count).
Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref outputRef, i)) = Avx2.Add(a0, b0); Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref outputRef, idx + 0)) = Avx2.Add(a0, b0);
Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref outputRef, i + 8)) = Avx2.Add(a1, b1); Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref outputRef, idx + 8)) = Avx2.Add(a1, b1);
Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref outputRef, i + 16)) = Avx2.Add(a2, b2); Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref outputRef, idx + 16)) = Avx2.Add(a2, b2);
Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref outputRef, i + 24)) = Avx2.Add(a3, b3); Unsafe.As<uint, Vector256<uint>>(ref Unsafe.Add(ref outputRef, idx + 24)) = Avx2.Add(a3, b3);
idx += 32;
} }
while (idx <= (uint)count - 32);
int i = (int)idx;
for (; i < count; i++) for (; i < count; i++)
{ {
output[i] = a[i] + b[i]; output[i] = a[i] + b[i];

118
src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs

@ -161,7 +161,7 @@ internal static class LossyUtils
private static int Vp8_Sse16xN_Sse2(Span<byte> a, Span<byte> b, int numPairs) private static int Vp8_Sse16xN_Sse2(Span<byte> a, Span<byte> b, int numPairs)
{ {
Vector128<int> sum = Vector128<int>.Zero; Vector128<int> sum = Vector128<int>.Zero;
nint offset = 0; nuint offset = 0;
ref byte aRef = ref MemoryMarshal.GetReference(a); ref byte aRef = ref MemoryMarshal.GetReference(a);
ref byte bRef = ref MemoryMarshal.GetReference(b); ref byte bRef = ref MemoryMarshal.GetReference(b);
for (int i = 0; i < numPairs; i++) for (int i = 0; i < numPairs; i++)
@ -186,7 +186,7 @@ internal static class LossyUtils
private static int Vp8_Sse16xN_Avx2(Span<byte> a, Span<byte> b, int numPairs) private static int Vp8_Sse16xN_Avx2(Span<byte> a, Span<byte> b, int numPairs)
{ {
Vector256<int> sum = Vector256<int>.Zero; Vector256<int> sum = Vector256<int>.Zero;
nint offset = 0; nuint offset = 0;
ref byte aRef = ref MemoryMarshal.GetReference(a); ref byte aRef = ref MemoryMarshal.GetReference(a);
ref byte bRef = ref MemoryMarshal.GetReference(b); ref byte bRef = ref MemoryMarshal.GetReference(b);
for (int i = 0; i < numPairs; i++) for (int i = 0; i < numPairs; i++)
@ -1427,17 +1427,17 @@ internal static class LossyUtils
if (Sse2.IsSupported) if (Sse2.IsSupported)
{ {
// Load. // Load.
ref byte pRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(p), offset); ref byte pRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(p), (uint)offset);
Vector128<byte> p1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Subtract(ref pRef, 2 * stride)); Vector128<byte> p1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Subtract(ref pRef, 2 * stride));
Vector128<byte> p0 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Subtract(ref pRef, stride)); Vector128<byte> p0 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Subtract(ref pRef, stride));
Vector128<byte> q0 = Unsafe.As<byte, Vector128<byte>>(ref pRef); Vector128<byte> q0 = Unsafe.As<byte, Vector128<byte>>(ref pRef);
Vector128<byte> q1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, stride)); Vector128<byte> q1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)stride));
DoFilter2Sse2(ref p1, ref p0, ref q0, ref q1, thresh); DoFilter2Sse2(ref p1, ref p0, ref q0, ref q1, thresh);
// Store. // Store.
ref byte outputRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(p), offset); ref byte outputRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(p), (uint)offset);
Unsafe.As<byte, Vector128<sbyte>>(ref Unsafe.Subtract(ref outputRef, stride)) = p0.AsSByte(); Unsafe.As<byte, Vector128<sbyte>>(ref Unsafe.Subtract(ref outputRef, stride)) = p0.AsSByte();
Unsafe.As<byte, Vector128<sbyte>>(ref outputRef) = q0.AsSByte(); Unsafe.As<byte, Vector128<sbyte>>(ref outputRef) = q0.AsSByte();
} }
@ -1460,11 +1460,11 @@ internal static class LossyUtils
if (Sse2.IsSupported) if (Sse2.IsSupported)
{ {
// Beginning of p1 // Beginning of p1
ref byte pRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(p), offset - 2); ref byte pRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(p), (uint)(offset - 2));
Load16x4(ref pRef, ref Unsafe.Add(ref pRef, 8 * stride), stride, out Vector128<byte> p1, out Vector128<byte> p0, out Vector128<byte> q0, out Vector128<byte> q1); Load16x4(ref pRef, ref Unsafe.Add(ref pRef, 8 * (uint)stride), stride, out Vector128<byte> p1, out Vector128<byte> p0, out Vector128<byte> q0, out Vector128<byte> q1);
DoFilter2Sse2(ref p1, ref p0, ref q0, ref q1, thresh); DoFilter2Sse2(ref p1, ref p0, ref q0, ref q1, thresh);
Store16x4(p1, p0, q0, q1, ref pRef, ref Unsafe.Add(ref pRef, 8 * stride), stride); Store16x4(p1, p0, q0, q1, ref pRef, ref Unsafe.Add(ref pRef, 8 * (uint)stride), stride);
} }
else else
{ {
@ -1527,19 +1527,19 @@ internal static class LossyUtils
if (Sse2.IsSupported) if (Sse2.IsSupported)
{ {
ref byte pRef = ref MemoryMarshal.GetReference(p); ref byte pRef = ref MemoryMarshal.GetReference(p);
Vector128<byte> t1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset - (4 * stride))); Vector128<byte> t1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset - (4 * stride))));
Vector128<byte> p2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset - (3 * stride))); Vector128<byte> p2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset - (3 * stride))));
Vector128<byte> p1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset - (2 * stride))); Vector128<byte> p1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset - (2 * stride))));
Vector128<byte> p0 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset - stride)); Vector128<byte> p0 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset - stride)));
Vector128<byte> mask = Abs(p1, p0); Vector128<byte> mask = Abs(p1, p0);
mask = Sse2.Max(mask, Abs(t1, p2)); mask = Sse2.Max(mask, Abs(t1, p2));
mask = Sse2.Max(mask, Abs(p2, p1)); mask = Sse2.Max(mask, Abs(p2, p1));
Vector128<byte> q0 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset)); Vector128<byte> q0 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)offset));
Vector128<byte> q1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset + stride)); Vector128<byte> q1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset + stride)));
Vector128<byte> q2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset + (2 * stride))); Vector128<byte> q2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset + (2 * stride))));
t1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset + (3 * stride))); t1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset + (3 * stride))));
mask = Sse2.Max(mask, Abs(q1, q0)); mask = Sse2.Max(mask, Abs(q1, q0));
mask = Sse2.Max(mask, Abs(t1, q2)); mask = Sse2.Max(mask, Abs(t1, q2));
@ -1550,12 +1550,12 @@ internal static class LossyUtils
// Store. // Store.
ref byte outputRef = ref MemoryMarshal.GetReference(p); ref byte outputRef = ref MemoryMarshal.GetReference(p);
Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, offset - (3 * stride))) = p2.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, (uint)(offset - (3 * stride)))) = p2.AsInt32();
Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, offset - (2 * stride))) = p1.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, (uint)(offset - (2 * stride)))) = p1.AsInt32();
Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, offset - stride)) = p0.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, (uint)(offset - stride))) = p0.AsInt32();
Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, offset)) = q0.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, (uint)offset)) = q0.AsInt32();
Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, offset + stride)) = q1.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, (uint)(offset + stride))) = q1.AsInt32();
Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, offset + (2 * stride))) = q2.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, (uint)(offset + (2 * stride)))) = q2.AsInt32();
} }
else else
{ {
@ -1569,14 +1569,14 @@ internal static class LossyUtils
if (Sse2.IsSupported) if (Sse2.IsSupported)
{ {
ref byte pRef = ref MemoryMarshal.GetReference(p); ref byte pRef = ref MemoryMarshal.GetReference(p);
ref byte bRef = ref Unsafe.Add(ref pRef, offset - 4); ref byte bRef = ref Unsafe.Add(ref pRef, (uint)offset - 4);
Load16x4(ref bRef, ref Unsafe.Add(ref bRef, 8 * stride), stride, out Vector128<byte> p3, out Vector128<byte> p2, out Vector128<byte> p1, out Vector128<byte> p0); Load16x4(ref bRef, ref Unsafe.Add(ref bRef, 8 * (uint)stride), stride, out Vector128<byte> p3, out Vector128<byte> p2, out Vector128<byte> p1, out Vector128<byte> p0);
Vector128<byte> mask = Abs(p1, p0); Vector128<byte> mask = Abs(p1, p0);
mask = Sse2.Max(mask, Abs(p3, p2)); mask = Sse2.Max(mask, Abs(p3, p2));
mask = Sse2.Max(mask, Abs(p2, p1)); mask = Sse2.Max(mask, Abs(p2, p1));
Load16x4(ref Unsafe.Add(ref pRef, offset), ref Unsafe.Add(ref pRef, offset + (8 * stride)), stride, out Vector128<byte> q0, out Vector128<byte> q1, out Vector128<byte> q2, out Vector128<byte> q3); Load16x4(ref Unsafe.Add(ref pRef, (uint)offset), ref Unsafe.Add(ref pRef, (uint)(offset + (8 * stride))), stride, out Vector128<byte> q0, out Vector128<byte> q1, out Vector128<byte> q2, out Vector128<byte> q3);
mask = Sse2.Max(mask, Abs(q1, q0)); mask = Sse2.Max(mask, Abs(q1, q0));
mask = Sse2.Max(mask, Abs(q3, q2)); mask = Sse2.Max(mask, Abs(q3, q2));
@ -1585,8 +1585,8 @@ internal static class LossyUtils
ComplexMask(p1, p0, q0, q1, thresh, ithresh, ref mask); ComplexMask(p1, p0, q0, q1, thresh, ithresh, ref mask);
DoFilter6Sse2(ref p2, ref p1, ref p0, ref q0, ref q1, ref q2, mask, hevThresh); DoFilter6Sse2(ref p2, ref p1, ref p0, ref q0, ref q1, ref q2, mask, hevThresh);
Store16x4(p3, p2, p1, p0, ref bRef, ref Unsafe.Add(ref bRef, 8 * stride), stride); Store16x4(p3, p2, p1, p0, ref bRef, ref Unsafe.Add(ref bRef, 8 * (uint)stride), stride);
Store16x4(q0, q1, q2, q3, ref Unsafe.Add(ref pRef, offset), ref Unsafe.Add(ref pRef, offset + (8 * stride)), stride); Store16x4(q0, q1, q2, q3, ref Unsafe.Add(ref pRef, (uint)offset), ref Unsafe.Add(ref pRef, (uint)(offset + (8 * stride))), stride);
} }
else else
{ {
@ -1599,10 +1599,10 @@ internal static class LossyUtils
if (Sse2.IsSupported) if (Sse2.IsSupported)
{ {
ref byte pRef = ref MemoryMarshal.GetReference(p); ref byte pRef = ref MemoryMarshal.GetReference(p);
Vector128<byte> p3 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset)); Vector128<byte> p3 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)offset));
Vector128<byte> p2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset + stride)); Vector128<byte> p2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset + stride)));
Vector128<byte> p1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset + (2 * stride))); Vector128<byte> p1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset + (2 * stride))));
Vector128<byte> p0 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset + (3 * stride))); Vector128<byte> p0 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset + (3 * stride))));
for (int k = 3; k > 0; k--) for (int k = 3; k > 0; k--)
{ {
@ -1614,10 +1614,10 @@ internal static class LossyUtils
mask = Sse2.Max(mask, Abs(p3, p2)); mask = Sse2.Max(mask, Abs(p3, p2));
mask = Sse2.Max(mask, Abs(p2, p1)); mask = Sse2.Max(mask, Abs(p2, p1));
p3 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset)); p3 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)offset));
p2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset + stride)); p2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset + stride)));
Vector128<byte> tmp1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset + (2 * stride))); Vector128<byte> tmp1 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset + (2 * stride))));
Vector128<byte> tmp2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, offset + (3 * stride))); Vector128<byte> tmp2 = Unsafe.As<byte, Vector128<byte>>(ref Unsafe.Add(ref pRef, (uint)(offset + (3 * stride))));
mask = Sse2.Max(mask, Abs(tmp1, tmp2)); mask = Sse2.Max(mask, Abs(tmp1, tmp2));
mask = Sse2.Max(mask, Abs(p3, p2)); mask = Sse2.Max(mask, Abs(p3, p2));
@ -1631,9 +1631,9 @@ internal static class LossyUtils
// Store. // Store.
ref byte outputRef = ref MemoryMarshal.GetReference(b); ref byte outputRef = ref MemoryMarshal.GetReference(b);
Unsafe.As<byte, Vector128<int>>(ref outputRef) = p1.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref outputRef) = p1.AsInt32();
Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, stride)) = p0.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, (uint)stride)) = p0.AsInt32();
Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, stride * 2)) = p3.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, (uint)(stride * 2))) = p3.AsInt32();
Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, stride * 3)) = p2.AsInt32(); Unsafe.As<byte, Vector128<int>>(ref Unsafe.Add(ref outputRef, (uint)(stride * 3))) = p2.AsInt32();
// Rotate samples. // Rotate samples.
p1 = tmp1; p1 = tmp1;
@ -1655,13 +1655,13 @@ internal static class LossyUtils
if (Sse2.IsSupported) if (Sse2.IsSupported)
{ {
ref byte pRef = ref MemoryMarshal.GetReference(p); ref byte pRef = ref MemoryMarshal.GetReference(p);
Load16x4(ref Unsafe.Add(ref pRef, offset), ref Unsafe.Add(ref pRef, offset + (8 * stride)), stride, out Vector128<byte> p3, out Vector128<byte> p2, out Vector128<byte> p1, out Vector128<byte> p0); Load16x4(ref Unsafe.Add(ref pRef, (uint)offset), ref Unsafe.Add(ref pRef, (uint)(offset + (8 * stride))), stride, out Vector128<byte> p3, out Vector128<byte> p2, out Vector128<byte> p1, out Vector128<byte> p0);
Vector128<byte> mask; Vector128<byte> mask;
for (int k = 3; k > 0; k--) for (int k = 3; k > 0; k--)
{ {
// Beginning of p1. // Beginning of p1.
ref byte bRef = ref Unsafe.Add(ref pRef, offset + 2); ref byte bRef = ref Unsafe.Add(ref pRef, (uint)offset + 2);
// Beginning of q0 (and next span). // Beginning of q0 (and next span).
offset += 4; offset += 4;
@ -1671,7 +1671,7 @@ internal static class LossyUtils
mask = Sse2.Max(mask, Abs(p3, p2)); mask = Sse2.Max(mask, Abs(p3, p2));
mask = Sse2.Max(mask, Abs(p2, p1)); mask = Sse2.Max(mask, Abs(p2, p1));
Load16x4(ref Unsafe.Add(ref pRef, offset), ref Unsafe.Add(ref pRef, offset + (8 * stride)), stride, out p3, out p2, out Vector128<byte> tmp1, out Vector128<byte> tmp2); Load16x4(ref Unsafe.Add(ref pRef, (uint)offset), ref Unsafe.Add(ref pRef, (uint)(offset + (8 * stride))), stride, out p3, out p2, out Vector128<byte> tmp1, out Vector128<byte> tmp2);
mask = Sse2.Max(mask, Abs(tmp1, tmp2)); mask = Sse2.Max(mask, Abs(tmp1, tmp2));
mask = Sse2.Max(mask, Abs(p3, p2)); mask = Sse2.Max(mask, Abs(p3, p2));
@ -1680,7 +1680,7 @@ internal static class LossyUtils
ComplexMask(p1, p0, p3, p2, thresh, ithresh, ref mask); ComplexMask(p1, p0, p3, p2, thresh, ithresh, ref mask);
DoFilter4Sse2(ref p1, ref p0, ref p3, ref p2, mask, hevThresh); DoFilter4Sse2(ref p1, ref p0, ref p3, ref p2, mask, hevThresh);
Store16x4(p1, p0, p3, p2, ref bRef, ref Unsafe.Add(ref bRef, 8 * stride), stride); Store16x4(p1, p0, p3, p2, ref bRef, ref Unsafe.Add(ref bRef, 8 * (uint)stride), stride);
// Rotate samples. // Rotate samples.
p1 = tmp1; p1 = tmp1;
@ -1749,13 +1749,13 @@ internal static class LossyUtils
{ {
ref byte uRef = ref MemoryMarshal.GetReference(u); ref byte uRef = ref MemoryMarshal.GetReference(u);
ref byte vRef = ref MemoryMarshal.GetReference(v); ref byte vRef = ref MemoryMarshal.GetReference(v);
Load16x4(ref Unsafe.Add(ref uRef, offset - 4), ref Unsafe.Add(ref vRef, offset - 4), stride, out Vector128<byte> p3, out Vector128<byte> p2, out Vector128<byte> p1, out Vector128<byte> p0); Load16x4(ref Unsafe.Add(ref uRef, (uint)offset - 4), ref Unsafe.Add(ref vRef, (uint)offset - 4), stride, out Vector128<byte> p3, out Vector128<byte> p2, out Vector128<byte> p1, out Vector128<byte> p0);
Vector128<byte> mask = Abs(p1, p0); Vector128<byte> mask = Abs(p1, p0);
mask = Sse2.Max(mask, Abs(p3, p2)); mask = Sse2.Max(mask, Abs(p3, p2));
mask = Sse2.Max(mask, Abs(p2, p1)); mask = Sse2.Max(mask, Abs(p2, p1));
Load16x4(ref Unsafe.Add(ref uRef, offset), ref Unsafe.Add(ref vRef, offset), stride, out Vector128<byte> q0, out Vector128<byte> q1, out Vector128<byte> q2, out Vector128<byte> q3); Load16x4(ref Unsafe.Add(ref uRef, (uint)offset), ref Unsafe.Add(ref vRef, (uint)offset), stride, out Vector128<byte> q0, out Vector128<byte> q1, out Vector128<byte> q2, out Vector128<byte> q3);
mask = Sse2.Max(mask, Abs(q1, q0)); mask = Sse2.Max(mask, Abs(q1, q0));
mask = Sse2.Max(mask, Abs(q3, q2)); mask = Sse2.Max(mask, Abs(q3, q2));
@ -1764,8 +1764,8 @@ internal static class LossyUtils
ComplexMask(p1, p0, q0, q1, thresh, ithresh, ref mask); ComplexMask(p1, p0, q0, q1, thresh, ithresh, ref mask);
DoFilter6Sse2(ref p2, ref p1, ref p0, ref q0, ref q1, ref q2, mask, hevThresh); DoFilter6Sse2(ref p2, ref p1, ref p0, ref q0, ref q1, ref q2, mask, hevThresh);
Store16x4(p3, p2, p1, p0, ref Unsafe.Add(ref uRef, offset - 4), ref Unsafe.Add(ref vRef, offset - 4), stride); Store16x4(p3, p2, p1, p0, ref Unsafe.Add(ref uRef, (uint)offset - 4), ref Unsafe.Add(ref vRef, (uint)offset - 4), stride);
Store16x4(q0, q1, q2, q3, ref Unsafe.Add(ref uRef, offset), ref Unsafe.Add(ref vRef, offset), stride); Store16x4(q0, q1, q2, q3, ref Unsafe.Add(ref uRef, (uint)offset), ref Unsafe.Add(ref vRef, (uint)offset), stride);
} }
else else
{ {
@ -1826,7 +1826,7 @@ internal static class LossyUtils
{ {
ref byte uRef = ref MemoryMarshal.GetReference(u); ref byte uRef = ref MemoryMarshal.GetReference(u);
ref byte vRef = ref MemoryMarshal.GetReference(v); ref byte vRef = ref MemoryMarshal.GetReference(v);
Load16x4(ref Unsafe.Add(ref uRef, offset), ref Unsafe.Add(ref vRef, offset), stride, out Vector128<byte> t2, out Vector128<byte> t1, out Vector128<byte> p1, out Vector128<byte> p0); Load16x4(ref Unsafe.Add(ref uRef, (uint)offset), ref Unsafe.Add(ref vRef, (uint)offset), stride, out Vector128<byte> t2, out Vector128<byte> t1, out Vector128<byte> p1, out Vector128<byte> p0);
Vector128<byte> mask = Abs(p1, p0); Vector128<byte> mask = Abs(p1, p0);
mask = Sse2.Max(mask, Abs(t2, t1)); mask = Sse2.Max(mask, Abs(t2, t1));
@ -1835,7 +1835,7 @@ internal static class LossyUtils
// Beginning of q0. // Beginning of q0.
offset += 4; offset += 4;
Load16x4(ref Unsafe.Add(ref uRef, offset), ref Unsafe.Add(ref vRef, offset), stride, out Vector128<byte> q0, out Vector128<byte> q1, out t1, out t2); Load16x4(ref Unsafe.Add(ref uRef, (uint)offset), ref Unsafe.Add(ref vRef, (uint)offset), stride, out Vector128<byte> q0, out Vector128<byte> q1, out t1, out t2);
mask = Sse2.Max(mask, Abs(q1, q0)); mask = Sse2.Max(mask, Abs(q1, q0));
mask = Sse2.Max(mask, Abs(t2, t1)); mask = Sse2.Max(mask, Abs(t2, t1));
@ -1846,7 +1846,7 @@ internal static class LossyUtils
// Beginning of p1. // Beginning of p1.
offset -= 2; offset -= 2;
Store16x4(p1, p0, q0, q1, ref Unsafe.Add(ref uRef, offset), ref Unsafe.Add(ref vRef, offset), stride); Store16x4(p1, p0, q0, q1, ref Unsafe.Add(ref uRef, (uint)offset), ref Unsafe.Add(ref vRef, (uint)offset), stride);
} }
else else
{ {
@ -2278,8 +2278,8 @@ internal static class LossyUtils
// q0 = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02 // q0 = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02
// p0 = f1 e1 d1 c1 b1 a1 91 81 f0 e0 d0 c0 b0 a0 90 80 // p0 = f1 e1 d1 c1 b1 a1 91 81 f0 e0 d0 c0 b0 a0 90 80
// q1 = f3 e3 d3 c3 b3 a3 93 83 f2 e2 d2 c2 b2 a2 92 82 // q1 = f3 e3 d3 c3 b3 a3 93 83 f2 e2 d2 c2 b2 a2 92 82
Load8x4(ref r0, stride, out Vector128<byte> t1, out Vector128<byte> t2); Load8x4(ref r0, (uint)stride, out Vector128<byte> t1, out Vector128<byte> t2);
Load8x4(ref r8, stride, out p0, out q1); Load8x4(ref r8, (uint)stride, out p0, out q1);
// p1 = f0 e0 d0 c0 b0 a0 90 80 70 60 50 40 30 20 10 00 // p1 = f0 e0 d0 c0 b0 a0 90 80 70 60 50 40 30 20 10 00
// p0 = f1 e1 d1 c1 b1 a1 91 81 71 61 51 41 31 21 11 01 // p0 = f1 e1 d1 c1 b1 a1 91 81 71 61 51 41 31 21 11 01
@ -2292,7 +2292,7 @@ internal static class LossyUtils
} }
// Reads 8 rows across a vertical edge. // Reads 8 rows across a vertical edge.
private static void Load8x4(ref byte bRef, int stride, out Vector128<byte> p, out Vector128<byte> q) private static void Load8x4(ref byte bRef, nuint stride, out Vector128<byte> p, out Vector128<byte> q)
{ {
// A0 = 63 62 61 60 23 22 21 20 43 42 41 40 03 02 01 00 // A0 = 63 62 61 60 23 22 21 20 43 42 41 40 03 02 01 00
// A1 = 73 72 71 70 33 32 31 30 53 52 51 50 13 12 11 10 // A1 = 73 72 71 70 33 32 31 30 53 52 51 50 13 12 11 10
@ -2349,10 +2349,10 @@ internal static class LossyUtils
q1s = Sse2.UnpackHigh(t1.AsInt16(), q1s.AsInt16()).AsByte(); q1s = Sse2.UnpackHigh(t1.AsInt16(), q1s.AsInt16()).AsByte();
Store4x4(p0s, ref r0Ref, stride); Store4x4(p0s, ref r0Ref, stride);
Store4x4(q0s, ref Unsafe.Add(ref r0Ref, 4 * stride), stride); Store4x4(q0s, ref Unsafe.Add(ref r0Ref, 4 * (uint)stride), stride);
Store4x4(p1s, ref r8Ref, stride); Store4x4(p1s, ref r8Ref, stride);
Store4x4(q1s, ref Unsafe.Add(ref r8Ref, 4 * stride), stride); Store4x4(q1s, ref Unsafe.Add(ref r8Ref, 4 * (uint)stride), stride);
} }
private static void Store4x4(Vector128<byte> x, ref byte dstRef, int stride) private static void Store4x4(Vector128<byte> x, ref byte dstRef, int stride)
@ -2360,7 +2360,7 @@ internal static class LossyUtils
int offset = 0; int offset = 0;
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
Unsafe.As<byte, int>(ref Unsafe.Add(ref dstRef, offset)) = Sse2.ConvertToInt32(x.AsInt32()); Unsafe.As<byte, int>(ref Unsafe.Add(ref dstRef, (uint)offset)) = Sse2.ConvertToInt32(x.AsInt32());
x = Sse2.ShiftRightLogical128BitLane(x, 4); x = Sse2.ShiftRightLogical128BitLane(x, 4);
offset += stride; offset += stride;
} }
@ -2421,16 +2421,16 @@ internal static class LossyUtils
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private static Vector128<byte> LoadUvEdge(ref byte uRef, ref byte vRef, int offset) private static Vector128<byte> LoadUvEdge(ref byte uRef, ref byte vRef, int offset)
{ {
var uVec = Vector128.Create(Unsafe.As<byte, long>(ref Unsafe.Add(ref uRef, offset)), 0); var uVec = Vector128.Create(Unsafe.As<byte, long>(ref Unsafe.Add(ref uRef, (uint)offset)), 0);
var vVec = Vector128.Create(Unsafe.As<byte, long>(ref Unsafe.Add(ref vRef, offset)), 0); var vVec = Vector128.Create(Unsafe.As<byte, long>(ref Unsafe.Add(ref vRef, (uint)offset)), 0);
return Sse2.UnpackLow(uVec, vVec).AsByte(); return Sse2.UnpackLow(uVec, vVec).AsByte();
} }
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private static void StoreUv(Vector128<byte> x, ref byte uRef, ref byte vRef, int offset) private static void StoreUv(Vector128<byte> x, ref byte uRef, ref byte vRef, int offset)
{ {
Unsafe.As<byte, Vector64<byte>>(ref Unsafe.Add(ref uRef, offset)) = x.GetLower(); Unsafe.As<byte, Vector64<byte>>(ref Unsafe.Add(ref uRef, (uint)offset)) = x.GetLower();
Unsafe.As<byte, Vector64<byte>>(ref Unsafe.Add(ref vRef, offset)) = x.GetUpper(); Unsafe.As<byte, Vector64<byte>>(ref Unsafe.Add(ref vRef, (uint)offset)) = x.GetUpper();
} }
// Compute abs(p - q) = subs(p - q) OR subs(q - p) // Compute abs(p - q) = subs(p - q) OR subs(q - p)

2
src/ImageSharp/Formats/Webp/Lossy/PassStats.cs

@ -8,7 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy;
/// </summary> /// </summary>
internal class PassStats internal class PassStats
{ {
public PassStats(long targetSize, float targetPsnr, int qMin, int qMax, int quality) public PassStats(long targetSize, float targetPsnr, int qMin, int qMax, uint quality)
{ {
bool doSizeSearch = targetSize != 0; bool doSizeSearch = targetSize != 0;

6
src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs

@ -770,7 +770,7 @@ internal static unsafe class QuantEnc
{ {
uint v = src[0] * 0x01010101u; uint v = src[0] * 0x01010101u;
Span<byte> vSpan = BitConverter.GetBytes(v).AsSpan(); Span<byte> vSpan = BitConverter.GetBytes(v).AsSpan();
for (nint i = 0; i < 16; i++) for (nuint i = 0; i < 16; i++)
{ {
if (!src[..4].SequenceEqual(vSpan) || !src.Slice(4, 4).SequenceEqual(vSpan) || if (!src[..4].SequenceEqual(vSpan) || !src.Slice(4, 4).SequenceEqual(vSpan) ||
!src.Slice(8, 4).SequenceEqual(vSpan) || !src.Slice(12, 4).SequenceEqual(vSpan)) !src.Slice(8, 4).SequenceEqual(vSpan) || !src.Slice(12, 4).SequenceEqual(vSpan))
@ -789,10 +789,10 @@ internal static unsafe class QuantEnc
{ {
int score = 0; int score = 0;
ref short levelsRef = ref MemoryMarshal.GetReference(levels); ref short levelsRef = ref MemoryMarshal.GetReference(levels);
int offset = 0; nuint offset = 0;
while (numBlocks-- > 0) while (numBlocks-- > 0)
{ {
for (nint i = 1; i < 16; i++) for (nuint i = 1; i < 16; i++)
{ {
// omit DC, we're only interested in AC // omit DC, we're only interested in AC
score += Unsafe.Add(ref levelsRef, offset) != 0 ? 1 : 0; score += Unsafe.Add(ref levelsRef, offset) != 0 ? 1 : 0;

6
src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs

@ -347,12 +347,12 @@ internal class Vp8EncIterator
} }
} }
public int FastMbAnalyze(int quality) public int FastMbAnalyze(uint quality)
{ {
// Empirical cut-off value, should be around 16 (~=block size). We use the // Empirical cut-off value, should be around 16 (~=block size). We use the
// [8-17] range and favor intra4 at high quality, intra16 for low quality. // [8-17] range and favor intra4 at high quality, intra16 for low quality.
int q = quality; uint q = quality;
int kThreshold = 8 + ((17 - 8) * q / 100); uint kThreshold = 8 + ((17 - 8) * q / 100);
int k; int k;
Span<uint> dc = stackalloc uint[16]; Span<uint> dc = stackalloc uint[16];
uint m; uint m;

12
src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs

@ -31,7 +31,7 @@ internal class Vp8Encoder : IDisposable
/// <summary> /// <summary>
/// The quality, that will be used to encode the image. /// The quality, that will be used to encode the image.
/// </summary> /// </summary>
private readonly int quality; private readonly uint quality;
/// <summary> /// <summary>
/// Quality/speed trade-off (0=fast, 6=slower-better). /// Quality/speed trade-off (0=fast, 6=slower-better).
@ -113,7 +113,7 @@ internal class Vp8Encoder : IDisposable
Configuration configuration, Configuration configuration,
int width, int width,
int height, int height,
int quality, uint quality,
bool skipMetadata, bool skipMetadata,
WebpEncodingMethod method, WebpEncodingMethod method,
int entropyPasses, int entropyPasses,
@ -125,7 +125,7 @@ internal class Vp8Encoder : IDisposable
this.configuration = configuration; this.configuration = configuration;
this.Width = width; this.Width = width;
this.Height = height; this.Height = height;
this.quality = Numerics.Clamp(quality, 0, 100); this.quality = Math.Min(quality, 100);
this.skipMetadata = skipMetadata; this.skipMetadata = skipMetadata;
this.method = method; this.method = method;
this.entropyPasses = Numerics.Clamp(entropyPasses, 1, 10); this.entropyPasses = Numerics.Clamp(entropyPasses, 1, 10);
@ -691,7 +691,7 @@ internal class Vp8Encoder : IDisposable
{ {
if (accum[n] != 0) if (accum[n] != 0)
{ {
int newCenter = (distAccum[n] + (accum[n] / 2)) / accum[n]; int newCenter = (distAccum[n] + (accum[n] >> 1)) / accum[n]; // >> 1 is bit-hack for / 2
displaced += Math.Abs(centers[n] - newCenter); displaced += Math.Abs(centers[n] - newCenter);
centers[n] = newCenter; centers[n] = newCenter;
weightedAverage += newCenter * accum[n]; weightedAverage += newCenter * accum[n];
@ -699,7 +699,7 @@ internal class Vp8Encoder : IDisposable
} }
} }
weightedAverage = (weightedAverage + (totalWeight / 2)) / totalWeight; weightedAverage = (weightedAverage + (totalWeight >> 1)) / totalWeight; // >> 1 is bit-hack for / 2
if (displaced < 5) if (displaced < 5)
{ {
break; // no need to keep on looping... break; // no need to keep on looping...
@ -1185,6 +1185,6 @@ internal class Vp8Encoder : IDisposable
{ {
int total = a + b; int total = a + b;
return total == 0 ? 255 // that's the default probability. return total == 0 ? 255 // that's the default probability.
: ((255 * a) + (total / 2)) / total; // rounded proba : ((255 * a) + (total >> 1)) / total; // rounded proba
} }
} }

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

@ -731,7 +731,7 @@ internal sealed class WebpLossyDecoder
Span<byte> dst = buf[dstStartIdx..]; Span<byte> dst = buf[dstStartIdx..];
int yEnd = io.MbY + io.MbH; int yEnd = io.MbY + io.MbH;
int mbw = io.MbW; int mbw = io.MbW;
int uvw = (mbw + 1) / 2; int uvw = (mbw + 1) >> 1; // >> 1 is bit-hack for / 2
int y = io.MbY; int y = io.MbY;
byte[] uvBuffer = new byte[(14 * 32) + 15]; byte[] uvBuffer = new byte[(14 * 32) + 15];

8
src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs

@ -138,8 +138,8 @@ internal static class YuvConversion
{ {
for (pos = 1, uvPos = 0; pos + 32 + 1 <= len; pos += 32, uvPos += 16) for (pos = 1, uvPos = 0; pos + 32 + 1 <= len; pos += 32, uvPos += 16)
{ {
UpSample32Pixels(ref Unsafe.Add(ref topURef, uvPos), ref Unsafe.Add(ref curURef, uvPos), ru); UpSample32Pixels(ref Unsafe.Add(ref topURef, (uint)uvPos), ref Unsafe.Add(ref curURef, (uint)uvPos), ru);
UpSample32Pixels(ref Unsafe.Add(ref topVRef, uvPos), ref Unsafe.Add(ref curVRef, uvPos), rv); UpSample32Pixels(ref Unsafe.Add(ref topVRef, (uint)uvPos), ref Unsafe.Add(ref curVRef, (uint)uvPos), rv);
ConvertYuvToBgrWithBottomYSse41(topY, bottomY, topDst, bottomDst, ru, rv, pos, xStep); ConvertYuvToBgrWithBottomYSse41(topY, bottomY, topDst, bottomDst, ru, rv, pos, xStep);
} }
} }
@ -147,8 +147,8 @@ internal static class YuvConversion
{ {
for (pos = 1, uvPos = 0; pos + 32 + 1 <= len; pos += 32, uvPos += 16) for (pos = 1, uvPos = 0; pos + 32 + 1 <= len; pos += 32, uvPos += 16)
{ {
UpSample32Pixels(ref Unsafe.Add(ref topURef, uvPos), ref Unsafe.Add(ref curURef, uvPos), ru); UpSample32Pixels(ref Unsafe.Add(ref topURef, (uint)uvPos), ref Unsafe.Add(ref curURef, (uint)uvPos), ru);
UpSample32Pixels(ref Unsafe.Add(ref topVRef, uvPos), ref Unsafe.Add(ref curVRef, uvPos), rv); UpSample32Pixels(ref Unsafe.Add(ref topVRef, (uint)uvPos), ref Unsafe.Add(ref curVRef, (uint)uvPos), rv);
ConvertYuvToBgrSse41(topY, topDst, ru, rv, pos, xStep); ConvertYuvToBgrSse41(topY, topDst, ru, rv, pos, xStep);
} }
} }

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

Loading…
Cancel
Save