Browse Source

Merge branch 'af/refactor-pixel-api' into af/extend-bulk-conversion

pull/751/head
Anton Firszov 8 years ago
parent
commit
8b6b0b64b6
  1. 13
      src/ImageSharp/Common/Helpers/Guard.cs
  2. 36
      src/ImageSharp/PixelFormats/IPixel.cs
  3. 2
      src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs
  4. 10
      tests/ImageSharp.Tests/Helpers/RowIntervalTests.cs
  5. 1
      tests/ImageSharp.Tests/Numerics/RationalTests.cs
  6. 47
      tests/ImageSharp.Tests/TestUtilities/TestDataGenerator.cs

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

@ -21,7 +21,6 @@ namespace SixLabors.ImageSharp
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void NotNull<T>(T value, string parameterName)
where T : class
{
@ -39,7 +38,6 @@ namespace SixLabors.ImageSharp
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or contains only blanks.</exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void NotNullOrWhiteSpace(string value, string parameterName)
{
if (value is null)
@ -62,7 +60,6 @@ namespace SixLabors.ImageSharp
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="value"/> is empty.</exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void NotNullOrEmpty<T>(ICollection<T> value, string parameterName)
{
if (value is null)
@ -87,7 +84,6 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName)
where TValue : IComparable<TValue>
{
@ -109,7 +105,6 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName)
where TValue : IComparable<TValue>
{
@ -131,7 +126,6 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName)
where TValue : IComparable<TValue>
{
@ -155,7 +149,6 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName)
where TValue : IComparable<TValue>
{
@ -178,7 +171,6 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is less than the minimum value of greater than the maximum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName)
where TValue : IComparable<TValue>
{
@ -199,7 +191,6 @@ namespace SixLabors.ImageSharp
/// <paramref name="target"/> is false
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void IsTrue(bool target, string parameterName, string message)
{
if (!target)
@ -219,7 +210,6 @@ namespace SixLabors.ImageSharp
/// <paramref name="target"/> is true
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void IsFalse(bool target, string parameterName, string message)
{
if (target)
@ -239,7 +229,6 @@ namespace SixLabors.ImageSharp
/// <paramref name="source"/> has less than <paramref name="minLength"/> items
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> source, int minLength, string parameterName)
{
if (source.Length < minLength)
@ -257,7 +246,6 @@ namespace SixLabors.ImageSharp
/// <param name="destination">The destination span</param>
/// <param name="destinationParamName">The name of the argument for 'destination'</param>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void DestinationShouldNotBeTooShort<TSource, TDest>(
ReadOnlySpan<TSource> source,
Span<TDest> destination,
@ -280,7 +268,6 @@ namespace SixLabors.ImageSharp
/// <paramref name="source"/> has less than <paramref name="minLength"/> items
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeSizedAtLeast<T>(Span<T> source, int minLength, string parameterName)
{
if (source.Length < minLength)

36
src/ImageSharp/PixelFormats/IPixel.cs

@ -24,93 +24,93 @@ namespace SixLabors.ImageSharp.PixelFormats
}
/// <summary>
/// An interface that represents a pixel type.
/// A base interface for all pixels, defining the mandatory operations to be implemented by a pixel type.
/// </summary>
public interface IPixel
{
/// <summary>
/// Sets the packed representation from a scaled <see cref="Vector4"/>.
/// Initializes the pixel instance from a generic ("scaled") <see cref="Vector4"/>.
/// </summary>
/// <param name="vector">The vector to create the packed representation from.</param>
/// <param name="vector">The vector to load the pixel from.</param>
void FromScaledVector4(Vector4 vector);
/// <summary>
/// Expands the packed representation into a scaled <see cref="Vector4"/>
/// with values clamped between <value>0</value> and <value>1</value>.
/// Expands the pixel into a generic ("scaled") <see cref="Vector4"/> representation
/// with values scaled and clamped between <value>0</value> and <value>1</value>.
/// The vector components are typically expanded in least to greatest significance order.
/// </summary>
/// <returns>The <see cref="Vector4"/>.</returns>
Vector4 ToScaledVector4();
/// <summary>
/// Sets the packed representation from a <see cref="Vector4"/>.
/// Initializes the pixel instance from a <see cref="Vector4"/> which is specific to the current pixel type.
/// </summary>
/// <param name="vector">The vector to create the packed representation from.</param>
/// <param name="vector">The vector to load the pixel from.</param>
void FromVector4(Vector4 vector);
/// <summary>
/// Expands the packed representation into a <see cref="Vector4"/>.
/// Expands the pixel into a <see cref="Vector4"/> which is specific to the current pixel type.
/// The vector components are typically expanded in least to greatest significance order.
/// </summary>
/// <returns>The <see cref="Vector4"/>.</returns>
Vector4 ToVector4();
/// <summary>
/// Packs the pixel from an <see cref="Argb32"/> value.
/// Initializes the pixel instance from an <see cref="Argb32"/> value.
/// </summary>
/// <param name="source">The <see cref="Argb32"/> value.</param>
void FromArgb32(Argb32 source);
/// <summary>
/// Packs the pixel from an <see cref="Bgr24"/> value.
/// Initializes the pixel instance from an <see cref="Bgr24"/> value.
/// </summary>
/// <param name="source">The <see cref="Bgr24"/> value.</param>
void FromBgr24(Bgr24 source);
/// <summary>
/// Packs the pixel from an <see cref="Bgra32"/> value.
/// Initializes the pixel instance from an <see cref="Bgra32"/> value.
/// </summary>
/// <param name="source">The <see cref="Bgra32"/> value.</param>
void FromBgra32(Bgra32 source);
/// <summary>
/// Packs the Pixel from an <see cref="Gray8"/> value.
/// Initializes the pixel instance from an <see cref="Gray8"/> value.
/// </summary>
/// <param name="source">The <see cref="Gray8"/> value.</param>
void FromGray8(Gray8 source);
/// <summary>
/// Packs the Pixel from an <see cref="Gray16"/> value.
/// Initializes the pixel instance from an <see cref="Gray16"/> value.
/// </summary>
/// <param name="source">The <see cref="Gray16"/> value.</param>
void FromGray16(Gray16 source);
/// <summary>
/// Packs the pixel from an <see cref="Rgb24"/> value.
/// Initializes the pixel instance from an <see cref="Rgb24"/> value.
/// </summary>
/// <param name="source">The <see cref="Rgb24"/> value.</param>
void FromRgb24(Rgb24 source);
/// <summary>
/// Packs the pixel from an <see cref="Rgba32"/> value.
/// Initializes the pixel instance from an <see cref="Rgba32"/> value.
/// </summary>
/// <param name="source">The <see cref="Rgba32"/> value.</param>
void FromRgba32(Rgba32 source);
/// <summary>
/// Expands the packed representation into an <see cref="Rgba32"/>.
/// Convert the pixel instance into <see cref="Rgba32"/> representation.
/// </summary>
/// <param name="dest">The reference to the destination <see cref="Rgba32"/> pixel</param>
void ToRgba32(ref Rgba32 dest);
/// <summary>
/// Packs the pixel from an <see cref="Rgb48"/> value.
/// Initializes the pixel instance from an <see cref="Rgb48"/> value.
/// </summary>
/// <param name="source">The <see cref="Rgb48"/> value.</param>
void FromRgb48(Rgb48 source);
/// <summary>
/// Packs the pixel from an <see cref="Rgba64"/> value.
/// Initializes the pixel instance from an <see cref="Rgba64"/> value.
/// </summary>
/// <param name="source">The <see cref="Rgba64"/> value.</param>
void FromRgba64(Rgba64 source);

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

@ -169,7 +169,7 @@ namespace SixLabors.ImageSharp.PixelFormats
return;
}
// Normal converson
// Normal conversion
ref TDestinationPixel destRef = ref MemoryMarshal.GetReference(destinationColors);
for (int i = 0; i < count; i++)
{

10
tests/ImageSharp.Tests/Helpers/RowIntervalTests.cs

@ -1,9 +1,9 @@
using System;
using System.Runtime.CompilerServices;
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Helpers
@ -35,4 +35,4 @@ namespace SixLabors.ImageSharp.Tests.Helpers
}
}
}
}
}

1
tests/ImageSharp.Tests/Numerics/RationalTests.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.MetaData.Profiles.Exif;
using SixLabors.ImageSharp.Primitives;
using Xunit;

47
tests/ImageSharp.Tests/TestUtilities/TestDataGenerator.cs

@ -1,10 +1,24 @@
using System;
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
namespace SixLabors.ImageSharp.Tests
{
/// <summary>
/// Helper methods that allow the creation of random test data.
/// </summary>
internal static class TestDataGenerator
{
/// <summary>
/// Creates an <see cref="float[]"/> of the given length consisting of random values between the two ranges.
/// </summary>
/// <param name="rnd">The pseudo-random number generator.</param>
/// <param name="length">The length.</param>
/// <param name="minVal">The minimum value.</param>
/// <param name="maxVal">The maximum value.</param>
/// <returns>The <see cref="float[]"/>.</returns>
public static float[] GenerateRandomFloatArray(this Random rnd, int length, float minVal, float maxVal)
{
float[] values = new float[length];
@ -17,6 +31,14 @@ namespace SixLabors.ImageSharp.Tests
return values;
}
/// <summary>
/// Creates an <see cref="Vector4[]"/> of the given length consisting of random values between the two ranges.
/// </summary>
/// <param name="rnd">The pseudo-random number generator.</param>
/// <param name="length">The length.</param>
/// <param name="minVal">The minimum value.</param>
/// <param name="maxVal">The maximum value.</param>
/// <returns>The <see cref="Vector4[]"/>.</returns>
public static Vector4[] GenerateRandomVectorArray(this Random rnd, int length, float minVal, float maxVal)
{
var values = new Vector4[length];
@ -33,20 +55,32 @@ namespace SixLabors.ImageSharp.Tests
return values;
}
/// <summary>
/// Creates an <see cref="float[]"/> of the given length consisting of rounded random values between the two ranges.
/// </summary>
/// <param name="rnd">The pseudo-random number generator.</param>
/// <param name="length">The length.</param>
/// <param name="minVal">The minimum value.</param>
/// <param name="maxVal">The maximum value.</param>
/// <returns>The <see cref="float[]"/>.</returns>
public static float[] GenerateRandomRoundedFloatArray(this Random rnd, int length, float minVal, float maxVal)
{
float[] values = new float[length];
for (int i = 0; i < length; i++)
{
values[i] = (float) Math.Round(rnd.GetRandomFloat(minVal, maxVal));
values[i] = (float)Math.Round(rnd.GetRandomFloat(minVal, maxVal));
}
return values;
}
/// <summary>
/// Creates an <see cref="byte[]"/> of the given length consisting of random values.
/// </summary>
/// <param name="rnd">The pseudo-random number generator.</param>
/// <param name="length">The length.</param>
/// <returns>The <see cref="byte[]"/>.</returns>
public static byte[] GenerateRandomByteArray(this Random rnd, int length)
{
byte[] values = new byte[length];
@ -54,9 +88,6 @@ namespace SixLabors.ImageSharp.Tests
return values;
}
private static float GetRandomFloat(this Random rnd, float minVal, float maxVal)
{
return (float)rnd.NextDouble() * (maxVal - minVal) + minVal;
}
private static float GetRandomFloat(this Random rnd, float minVal, float maxVal) => ((float)rnd.NextDouble() * (maxVal - minVal)) + minVal;
}
}
Loading…
Cancel
Save