Browse Source

added guards to unsafe PixelOperations methods

af/merge-core
Anton Firszov 9 years ago
parent
commit
5de5b91242
  1. 2
      src/ImageSharp/Common/Helpers/DebugGuard.cs
  2. 19
      src/ImageSharp/Common/Helpers/Guard.cs
  3. 30
      src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs
  4. 27
      src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs
  5. 3
      src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs

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

@ -170,6 +170,7 @@ namespace ImageSharp
/// <exception cref="ArgumentException">
/// <paramref name="target"/> is true
/// </exception>
[Conditional("DEBUG")]
public static void MustBeSameSized<T>(Span<T> target, Span<T> other, string parameterName)
where T : struct
{
@ -189,6 +190,7 @@ namespace ImageSharp
/// <exception cref="ArgumentException">
/// <paramref name="target"/> is true
/// </exception>
[Conditional("DEBUG")]
public static void MustBeSizedAtLeast<T>(Span<T> target, Span<T> minSpan, string parameterName)
where T : struct
{

19
src/ImageSharp/Common/Helpers/Guard.cs

@ -230,5 +230,24 @@ namespace ImageSharp
throw new ArgumentException(message, parameterName);
}
}
/// <summary>
/// Verifies, that the `target` span has the length of 'minSpan', or longer.
/// </summary>
/// <typeparam name="T">The element type of the spans</typeparam>
/// <param name="target">The target span.</param>
/// <param name="minLength">The minimum length.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <exception cref="ArgumentException">
/// <paramref name="target"/> is true
/// </exception>
public static void MustBeSizedAtLeast<T>(Span<T> target, int minLength, string parameterName)
where T : struct
{
if (target.Length < minLength)
{
throw new ArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
}
}
}
}

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

@ -30,6 +30,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromVector4(Span<Vector4> sourceVectors, Span<TPixel> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceVectors, count, nameof(sourceVectors));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
ref Vector4 sourceRef = ref sourceVectors.DangerousGetPinnableReference();
ref TPixel destRef = ref destColors.DangerousGetPinnableReference();
@ -49,6 +52,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void ToVector4(Span<TPixel> sourceColors, Span<Vector4> destVectors, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destVectors, count, nameof(destVectors));
ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference();
ref Vector4 destRef = ref destVectors.DangerousGetPinnableReference();
@ -68,6 +74,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromXyzBytes(Span<byte> sourceBytes, Span<TPixel> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceBytes, count * 3, nameof(sourceBytes));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference();
ref TPixel destRef = ref destColors.DangerousGetPinnableReference();
@ -91,6 +100,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void ToXyzBytes(Span<TPixel> sourceColors, Span<byte> destBytes, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destBytes, count * 3, nameof(destBytes));
ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference();
for (int i = 0; i < count; i++)
@ -108,6 +120,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromXyzwBytes(Span<byte> sourceBytes, Span<TPixel> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceBytes, count * 4, nameof(sourceBytes));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference();
ref TPixel destRef = ref destColors.DangerousGetPinnableReference();
@ -131,6 +146,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void ToXyzwBytes(Span<TPixel> sourceColors, Span<byte> destBytes, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destBytes, count * 4, nameof(destBytes));
ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference();
for (int i = 0; i < count; i++)
@ -148,6 +166,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromZyxBytes(Span<byte> sourceBytes, Span<TPixel> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceBytes, count * 3, nameof(sourceBytes));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference();
ref TPixel destRef = ref destColors.DangerousGetPinnableReference();
@ -171,6 +192,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void ToZyxBytes(Span<TPixel> sourceColors, Span<byte> destBytes, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destBytes, count * 3, nameof(destBytes));
ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference();
for (int i = 0; i < count; i++)
@ -188,6 +212,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromZyxwBytes(Span<byte> sourceBytes, Span<TPixel> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceBytes, count * 4, nameof(sourceBytes));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference();
ref TPixel destRef = ref destColors.DangerousGetPinnableReference();
@ -211,6 +238,9 @@ namespace ImageSharp.PixelFormats
/// <param name="count">The number of pixels to convert.</param>
internal virtual void ToZyxwBytes(Span<TPixel> sourceColors, Span<byte> destBytes, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destBytes, count * 4, nameof(destBytes));
ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference();
for (int i = 0; i < count; i++)

27
src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs

@ -93,6 +93,9 @@ namespace ImageSharp
/// <inheritdoc />
internal override void ToVector4(Span<Rgba32> sourceColors, Span<Vector4> destVectors, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destVectors, count, nameof(destVectors));
if (count < 256 || !Vector.IsHardwareAccelerated)
{
// Doesn't worth to bother with SIMD:
@ -120,6 +123,9 @@ namespace ImageSharp
/// <inheritdoc />
internal override void PackFromXyzBytes(Span<byte> sourceBytes, Span<Rgba32> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceBytes, count * 3, nameof(sourceBytes));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
ref RGB24 sourceRef = ref Unsafe.As<byte, RGB24>(ref sourceBytes.DangerousGetPinnableReference());
ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference();
@ -136,6 +142,9 @@ namespace ImageSharp
/// <inheritdoc />
internal override void ToXyzBytes(Span<Rgba32> sourceColors, Span<byte> destBytes, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destBytes, count * 3, nameof(destBytes));
ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference();
ref RGB24 destRef = ref Unsafe.As<byte, RGB24>(ref destBytes.DangerousGetPinnableReference());
@ -151,18 +160,27 @@ namespace ImageSharp
/// <inheritdoc />
internal override unsafe void PackFromXyzwBytes(Span<byte> sourceBytes, Span<Rgba32> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceBytes, count * 4, nameof(sourceBytes));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
SpanHelper.Copy(sourceBytes, destColors.AsBytes(), count * sizeof(Rgba32));
}
/// <inheritdoc />
internal override unsafe void ToXyzwBytes(Span<Rgba32> sourceColors, Span<byte> destBytes, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destBytes, count * 4, nameof(destBytes));
SpanHelper.Copy(sourceColors.AsBytes(), destBytes, count * sizeof(Rgba32));
}
/// <inheritdoc />
internal override void PackFromZyxBytes(Span<byte> sourceBytes, Span<Rgba32> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceBytes, count * 3, nameof(sourceBytes));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
ref RGB24 sourceRef = ref Unsafe.As<byte, RGB24>(ref sourceBytes.DangerousGetPinnableReference());
ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference();
@ -179,6 +197,9 @@ namespace ImageSharp
/// <inheritdoc />
internal override void ToZyxBytes(Span<Rgba32> sourceColors, Span<byte> destBytes, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destBytes, count * 3, nameof(destBytes));
ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference();
ref RGB24 destRef = ref Unsafe.As<byte, RGB24>(ref destBytes.DangerousGetPinnableReference());
@ -194,6 +215,9 @@ namespace ImageSharp
/// <inheritdoc />
internal override void PackFromZyxwBytes(Span<byte> sourceBytes, Span<Rgba32> destColors, int count)
{
Guard.MustBeSizedAtLeast(sourceBytes, count * 4, nameof(sourceBytes));
Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors));
ref RGBA32 sourceRef = ref Unsafe.As<byte, RGBA32>(ref sourceBytes.DangerousGetPinnableReference());
ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference();
@ -209,6 +233,9 @@ namespace ImageSharp
/// <inheritdoc />
internal override void ToZyxwBytes(Span<Rgba32> sourceColors, Span<byte> destBytes, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destBytes, count * 4, nameof(destBytes));
ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference();
ref RGBA32 destRef = ref Unsafe.As<byte, RGBA32>(ref destBytes.DangerousGetPinnableReference());

3
src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs

@ -23,6 +23,9 @@ namespace ImageSharp.PixelFormats
/// <inheritdoc />
internal override unsafe void ToVector4(Span<RgbaVector> sourceColors, Span<Vector4> destVectors, int count)
{
Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
Guard.MustBeSizedAtLeast(destVectors, count, nameof(destVectors));
SpanHelper.Copy(sourceColors.AsBytes(), destVectors.AsBytes(), count * sizeof(Vector4));
}
}

Loading…
Cancel
Save