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

36
src/ImageSharp/PixelFormats/IPixel.cs

@ -24,93 +24,93 @@ namespace SixLabors.ImageSharp.PixelFormats
} }
/// <summary> /// <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> /// </summary>
public interface IPixel public interface IPixel
{ {
/// <summary> /// <summary>
/// Sets the packed representation from a scaled <see cref="Vector4"/>. /// Initializes the pixel instance from a generic ("scaled") <see cref="Vector4"/>.
/// </summary> /// </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); void FromScaledVector4(Vector4 vector);
/// <summary> /// <summary>
/// Expands the packed representation into a scaled <see cref="Vector4"/> /// Expands the pixel into a generic ("scaled") <see cref="Vector4"/> representation
/// with values clamped between <value>0</value> and <value>1</value>. /// 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. /// The vector components are typically expanded in least to greatest significance order.
/// </summary> /// </summary>
/// <returns>The <see cref="Vector4"/>.</returns> /// <returns>The <see cref="Vector4"/>.</returns>
Vector4 ToScaledVector4(); Vector4 ToScaledVector4();
/// <summary> /// <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> /// </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); void FromVector4(Vector4 vector);
/// <summary> /// <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. /// The vector components are typically expanded in least to greatest significance order.
/// </summary> /// </summary>
/// <returns>The <see cref="Vector4"/>.</returns> /// <returns>The <see cref="Vector4"/>.</returns>
Vector4 ToVector4(); Vector4 ToVector4();
/// <summary> /// <summary>
/// Packs the pixel from an <see cref="Argb32"/> value. /// Initializes the pixel instance from an <see cref="Argb32"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Argb32"/> value.</param> /// <param name="source">The <see cref="Argb32"/> value.</param>
void FromArgb32(Argb32 source); void FromArgb32(Argb32 source);
/// <summary> /// <summary>
/// Packs the pixel from an <see cref="Bgr24"/> value. /// Initializes the pixel instance from an <see cref="Bgr24"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Bgr24"/> value.</param> /// <param name="source">The <see cref="Bgr24"/> value.</param>
void FromBgr24(Bgr24 source); void FromBgr24(Bgr24 source);
/// <summary> /// <summary>
/// Packs the pixel from an <see cref="Bgra32"/> value. /// Initializes the pixel instance from an <see cref="Bgra32"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Bgra32"/> value.</param> /// <param name="source">The <see cref="Bgra32"/> value.</param>
void FromBgra32(Bgra32 source); void FromBgra32(Bgra32 source);
/// <summary> /// <summary>
/// Packs the Pixel from an <see cref="Gray8"/> value. /// Initializes the pixel instance from an <see cref="Gray8"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Gray8"/> value.</param> /// <param name="source">The <see cref="Gray8"/> value.</param>
void FromGray8(Gray8 source); void FromGray8(Gray8 source);
/// <summary> /// <summary>
/// Packs the Pixel from an <see cref="Gray16"/> value. /// Initializes the pixel instance from an <see cref="Gray16"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Gray16"/> value.</param> /// <param name="source">The <see cref="Gray16"/> value.</param>
void FromGray16(Gray16 source); void FromGray16(Gray16 source);
/// <summary> /// <summary>
/// Packs the pixel from an <see cref="Rgb24"/> value. /// Initializes the pixel instance from an <see cref="Rgb24"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Rgb24"/> value.</param> /// <param name="source">The <see cref="Rgb24"/> value.</param>
void FromRgb24(Rgb24 source); void FromRgb24(Rgb24 source);
/// <summary> /// <summary>
/// Packs the pixel from an <see cref="Rgba32"/> value. /// Initializes the pixel instance from an <see cref="Rgba32"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Rgba32"/> value.</param> /// <param name="source">The <see cref="Rgba32"/> value.</param>
void FromRgba32(Rgba32 source); void FromRgba32(Rgba32 source);
/// <summary> /// <summary>
/// Expands the packed representation into an <see cref="Rgba32"/>. /// Convert the pixel instance into <see cref="Rgba32"/> representation.
/// </summary> /// </summary>
/// <param name="dest">The reference to the destination <see cref="Rgba32"/> pixel</param> /// <param name="dest">The reference to the destination <see cref="Rgba32"/> pixel</param>
void ToRgba32(ref Rgba32 dest); void ToRgba32(ref Rgba32 dest);
/// <summary> /// <summary>
/// Packs the pixel from an <see cref="Rgb48"/> value. /// Initializes the pixel instance from an <see cref="Rgb48"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Rgb48"/> value.</param> /// <param name="source">The <see cref="Rgb48"/> value.</param>
void FromRgb48(Rgb48 source); void FromRgb48(Rgb48 source);
/// <summary> /// <summary>
/// Packs the pixel from an <see cref="Rgba64"/> value. /// Initializes the pixel instance from an <see cref="Rgba64"/> value.
/// </summary> /// </summary>
/// <param name="source">The <see cref="Rgba64"/> value.</param> /// <param name="source">The <see cref="Rgba64"/> value.</param>
void FromRgba64(Rgba64 source); void FromRgba64(Rgba64 source);

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

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

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

@ -1,9 +1,9 @@
using System; // Copyright (c) Six Labors and contributors.
using System.Runtime.CompilerServices; // Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
using Xunit; using Xunit;
namespace SixLabors.ImageSharp.Tests.Helpers 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. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.MetaData.Profiles.Exif;
using SixLabors.ImageSharp.Primitives; using SixLabors.ImageSharp.Primitives;
using Xunit; 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; using System.Numerics;
namespace SixLabors.ImageSharp.Tests namespace SixLabors.ImageSharp.Tests
{ {
/// <summary>
/// Helper methods that allow the creation of random test data.
/// </summary>
internal static class TestDataGenerator 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) public static float[] GenerateRandomFloatArray(this Random rnd, int length, float minVal, float maxVal)
{ {
float[] values = new float[length]; float[] values = new float[length];
@ -17,6 +31,14 @@ namespace SixLabors.ImageSharp.Tests
return values; 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) public static Vector4[] GenerateRandomVectorArray(this Random rnd, int length, float minVal, float maxVal)
{ {
var values = new Vector4[length]; var values = new Vector4[length];
@ -33,20 +55,32 @@ namespace SixLabors.ImageSharp.Tests
return values; 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) public static float[] GenerateRandomRoundedFloatArray(this Random rnd, int length, float minVal, float maxVal)
{ {
float[] values = new float[length]; float[] values = new float[length];
for (int i = 0; i < length; i++) 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; 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) public static byte[] GenerateRandomByteArray(this Random rnd, int length)
{ {
byte[] values = new byte[length]; byte[] values = new byte[length];
@ -54,9 +88,6 @@ namespace SixLabors.ImageSharp.Tests
return values; return values;
} }
private static float GetRandomFloat(this Random rnd, float minVal, float maxVal) private static float GetRandomFloat(this Random rnd, float minVal, float maxVal) => ((float)rnd.NextDouble() * (maxVal - minVal)) + minVal;
{
return (float)rnd.NextDouble() * (maxVal - minVal) + minVal;
}
} }
} }
Loading…
Cancel
Save