|
Before Width: | Height: | Size: 6.4 KiB After Width: | Height: | Size: 7.9 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 6.8 KiB |
@ -0,0 +1,144 @@ |
|||
// <copyright file="MathF.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp |
|||
{ |
|||
using System; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
/// <summary>
|
|||
/// Provides single-precision floating point constants and static methods for trigonometric, logarithmic, and other common mathematical functions.
|
|||
/// </summary>
|
|||
// ReSharper disable InconsistentNaming
|
|||
internal static class MathF |
|||
{ |
|||
/// <summary>
|
|||
/// Represents the ratio of the circumference of a circle to its diameter, specified by the constant, π.
|
|||
/// </summary>
|
|||
public const float PI = (float)Math.PI; |
|||
|
|||
/// <summary>Returns the absolute value of a single-precision floating-point number.</summary>
|
|||
/// <param name="f">A number that is greater than or equal to <see cref="F:System.Single.MinValue" />, but less than or equal to <see cref="F:System.Single.MaxValue" />.</param>
|
|||
/// <returns>A single-precision floating-point number, x, such that 0 ≤ x ≤<see cref="F:System.Single.MaxValue" />.</returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Abs(float f) |
|||
{ |
|||
return Math.Abs(f); |
|||
} |
|||
|
|||
/// <summary>Returns the smallest integral value that is greater than or equal to the specified single-precision floating-point number.</summary>
|
|||
/// <param name="f">A single-precision floating-point number. </param>
|
|||
/// <returns>The smallest integral value that is greater than or equal to <paramref name="f" />.
|
|||
/// If <paramref name="f" /> is equal to <see cref="F:System.Single.NaN" />, <see cref="F:System.Single.NegativeInfinity" />,
|
|||
/// or <see cref="F:System.Single.PositiveInfinity" />, that value is returned.
|
|||
/// Note that this method returns a <see cref="T:System.Single" /> instead of an integral type.</returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Ceiling(float f) |
|||
{ |
|||
return (float)Math.Ceiling(f); |
|||
} |
|||
|
|||
/// <summary>Returns e raised to the specified power.</summary>
|
|||
/// <param name="f">A number specifying a power.</param>
|
|||
/// <returns>
|
|||
/// The number e raised to the power <paramref name="f" />.
|
|||
/// If <paramref name="f" /> equals <see cref="F:System.Single.NaN" /> or <see cref="F:System.Single.PositiveInfinity" />, that value is returned.
|
|||
/// If <paramref name="f" /> equals <see cref="F:System.Single.NegativeInfinity" />, 0 is returned.
|
|||
/// </returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Exp(float f) |
|||
{ |
|||
return (float)Math.Exp(f); |
|||
} |
|||
|
|||
/// <summary>Returns the largest integer less than or equal to the specified single-precision floating-point number.</summary>
|
|||
/// <param name="f">A single-precision floating-point number. </param>
|
|||
/// <returns>The largest integer less than or equal to <paramref name="f" />.
|
|||
/// If <paramref name="f" /> is equal to <see cref="F:System.Single.NaN" />, <see cref="F:System.Single.NegativeInfinity" />,
|
|||
/// or <see cref="F:System.Single.PositiveInfinity" />, that value is returned.
|
|||
/// </returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Floor(float f) |
|||
{ |
|||
return (float)Math.Floor(f); |
|||
} |
|||
|
|||
/// <summary>Returns the larger of two single-precision floating-point numbers.</summary>
|
|||
/// <param name="val1">The first of two single-precision floating-point numbers to compare. </param>
|
|||
/// <param name="val2">The second of two single-precision floating-point numbers to compare. </param>
|
|||
/// <returns>Parameter <paramref name="val1" /> or <paramref name="val2" />, whichever is larger.
|
|||
/// If <paramref name="val1" />, or <paramref name="val2" />, or both <paramref name="val1" /> and <paramref name="val2" /> are
|
|||
/// equal to <see cref="F:System.Single.NaN" />, <see cref="F:System.Single.NaN" /> is returned.
|
|||
/// </returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Max(float val1, float val2) |
|||
{ |
|||
return Math.Max(val1, val2); |
|||
} |
|||
|
|||
/// <summary>Returns the smaller of two single-precision floating-point numbers.</summary>
|
|||
/// <param name="val1">The first of two single-precision floating-point numbers to compare. </param>
|
|||
/// <param name="val2">The second of two single-precision floating-point numbers to compare. </param>
|
|||
/// <returns>Parameter <paramref name="val1" /> or <paramref name="val2" />, whichever is smaller.
|
|||
/// If <paramref name="val1" />, <paramref name="val2" />, or both <paramref name="val1" /> and <paramref name="val2" /> are equal
|
|||
/// to <see cref="F:System.Single.NaN" />, <see cref="F:System.Single.NaN" /> is returned.</returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Min(float val1, float val2) |
|||
{ |
|||
return Math.Min(val1, val2); |
|||
} |
|||
|
|||
/// <summary>Returns a specified number raised to the specified power.</summary>
|
|||
/// <param name="x">A single-precision floating-point number to be raised to a power. </param>
|
|||
/// <param name="y">A single-precision floating-point number that specifies a power. </param>
|
|||
/// <returns>The number <paramref name="x" /> raised to the power <paramref name="y" />.</returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Pow(float x, float y) |
|||
{ |
|||
return (float)Math.Pow(x, y); |
|||
} |
|||
|
|||
/// <summary>Rounds a single-precision floating-point value to the nearest integral value.</summary>
|
|||
/// <param name="f">A single-precision floating-point number to be rounded. </param>
|
|||
/// <returns>
|
|||
/// The integer nearest <paramref name="f" />.
|
|||
/// If the fractional component of <paramref name="f" /> is halfway between two integers, one of which is even and the other odd, then the even number is returned.
|
|||
/// Note that this method returns a <see cref="T:System.Single" /> instead of an integral type.
|
|||
/// </returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Round(float f) |
|||
{ |
|||
return (float)Math.Round(f); |
|||
} |
|||
|
|||
/// <summary>Returns the sine of the specified angle.</summary>
|
|||
/// <param name="f">An angle, measured in radians. </param>
|
|||
/// <returns>
|
|||
/// The sine of <paramref name="f" />.
|
|||
/// If <paramref name="f" /> is equal to <see cref="F:System.Single.NaN" />, <see cref="F:System.Single.NegativeInfinity" />,
|
|||
/// or <see cref="F:System.Single.PositiveInfinity" />, this method returns <see cref="F:System.Single.NaN" />.
|
|||
/// </returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Sin(float f) |
|||
{ |
|||
return (float)Math.Sin(f); |
|||
} |
|||
|
|||
/// <summary>Returns the square root of a specified number.</summary>
|
|||
/// <param name="f">The number whose square root is to be found. </param>
|
|||
/// <returns>
|
|||
/// One of the values in the following table.
|
|||
/// <paramref name="f" /> parameter Return value Zero or positive The positive square root of <paramref name="f" />.
|
|||
/// Negative <see cref="F:System.Single.NaN" />Equals <see cref="F:System.Single.NaN" />
|
|||
/// <see cref="F:System.Single.NaN" />Equals <see cref="F:System.Single.PositiveInfinity" />
|
|||
/// <see cref="F:System.Single.PositiveInfinity" />
|
|||
/// </returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static float Sqrt(float f) |
|||
{ |
|||
return (float)Math.Sqrt(f); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
// <copyright file="EndianBitConverter.Conversion.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.IO |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Equivalent of <see cref="BitConverter"/>, but with either endianness.
|
|||
/// </summary>
|
|||
internal abstract partial class EndianBitConverter |
|||
{ |
|||
/// <summary>
|
|||
/// Converts the specified double-precision floating point number to a
|
|||
/// 64-bit signed integer. Note: the endianness of this converter does not
|
|||
/// affect the returned value.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert. </param>
|
|||
/// <returns>A 64-bit signed integer whose value is equivalent to value.</returns>
|
|||
public unsafe long DoubleToInt64Bits(double value) |
|||
{ |
|||
return *((long*)&value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the specified 64-bit signed integer to a double-precision
|
|||
/// floating point number. Note: the endianness of this converter does not
|
|||
/// affect the returned value.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert. </param>
|
|||
/// <returns>A double-precision floating point number whose value is equivalent to value.</returns>
|
|||
public unsafe double Int64BitsToDouble(long value) |
|||
{ |
|||
return *((double*)&value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the specified single-precision floating point number to a
|
|||
/// 32-bit signed integer. Note: the endianness of this converter does not
|
|||
/// affect the returned value.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert. </param>
|
|||
/// <returns>A 32-bit signed integer whose value is equivalent to value.</returns>
|
|||
public unsafe int SingleToInt32Bits(float value) |
|||
{ |
|||
return *((int*)&value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the specified 32-bit signed integer to a single-precision floating point
|
|||
/// number. Note: the endianness of this converter does not
|
|||
/// affect the returned value.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert. </param>
|
|||
/// <returns>A single-precision floating point number whose value is equivalent to value.</returns>
|
|||
public unsafe float Int32BitsToSingle(int value) |
|||
{ |
|||
return *((float*)&value); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,145 @@ |
|||
// <copyright file="EndianBitConverter.CopyBytes.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.IO |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Equivalent of <see cref="BitConverter"/>, but with either endianness.
|
|||
/// </summary>
|
|||
internal abstract partial class EndianBitConverter |
|||
{ |
|||
/// <summary>
|
|||
/// Copies the specified 16-bit signed integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public abstract void CopyBytes(short value, byte[] buffer, int index); |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 32-bit signed integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public abstract void CopyBytes(int value, byte[] buffer, int index); |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 64-bit signed integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public abstract void CopyBytes(long value, byte[] buffer, int index); |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 16-bit unsigned integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(ushort value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(unchecked((short)value), buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 32-bit unsigned integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(uint value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(unchecked((int)value), buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified 64-bit unsigned integer value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(ulong value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(unchecked((long)value), buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified Boolean value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">A Boolean value.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(bool value, byte[] buffer, int index) |
|||
{ |
|||
CheckByteArgument(buffer, index, 1); |
|||
buffer[index] = value ? (byte)1 : (byte)0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified Unicode character value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">A character to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public void CopyBytes(char value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(unchecked((short)value), buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified double-precision floating point value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public unsafe void CopyBytes(double value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(*((long*)&value), buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified single-precision floating point value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public unsafe void CopyBytes(float value, byte[] buffer, int index) |
|||
{ |
|||
this.CopyBytes(*((int*)&value), buffer, index); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the specified decimal value into the specified byte array,
|
|||
/// beginning at the specified index.
|
|||
/// </summary>
|
|||
/// <param name="value">A character to convert.</param>
|
|||
/// <param name="buffer">The byte array to copy the bytes into</param>
|
|||
/// <param name="index">The first index into the array to copy the bytes into</param>
|
|||
public unsafe void CopyBytes(decimal value, byte[] buffer, int index) |
|||
{ |
|||
CheckByteArgument(buffer, index, 16); |
|||
|
|||
int* pvalue = (int*)&value; |
|||
this.CopyBytes(pvalue[0], buffer, index); |
|||
this.CopyBytes(pvalue[1], buffer, index + 4); |
|||
this.CopyBytes(pvalue[2], buffer, index + 8); |
|||
this.CopyBytes(pvalue[3], buffer, index + 12); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,139 @@ |
|||
// <copyright file="EndianBitConverter.GetBytes.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.IO |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Equivalent of <see cref="BitConverter"/>, but with either endianness.
|
|||
/// </summary>
|
|||
internal abstract partial class EndianBitConverter |
|||
{ |
|||
/// <summary>
|
|||
/// Returns the specified 16-bit signed integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 2.</returns>
|
|||
public byte[] GetBytes(short value) |
|||
{ |
|||
byte[] result = new byte[2]; |
|||
this.CopyBytes(value, result, 0); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 32-bit signed integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 4.</returns>
|
|||
public byte[] GetBytes(int value) |
|||
{ |
|||
byte[] result = new byte[4]; |
|||
this.CopyBytes(value, result, 0); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 64-bit signed integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 8.</returns>
|
|||
public byte[] GetBytes(long value) |
|||
{ |
|||
byte[] result = new byte[8]; |
|||
this.CopyBytes(value, result, 0); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 16-bit unsigned integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 2.</returns>
|
|||
public byte[] GetBytes(ushort value) |
|||
{ |
|||
return this.GetBytes(unchecked((short)value)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 32-bit unsigned integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 4.</returns>
|
|||
public byte[] GetBytes(uint value) |
|||
{ |
|||
return this.GetBytes(unchecked((int)value)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified 64-bit unsigned integer value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 8.</returns>
|
|||
public byte[] GetBytes(ulong value) |
|||
{ |
|||
return this.GetBytes(unchecked((long)value)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified Boolean value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">A Boolean value.</param>
|
|||
/// <returns>An array of bytes with length 1.</returns>
|
|||
/// <returns>
|
|||
/// The <see cref="T:byte[]"/>.
|
|||
/// </returns>
|
|||
public byte[] GetBytes(bool value) |
|||
{ |
|||
return new byte[1] { value ? (byte)1 : (byte)0 }; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified Unicode character value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">A character to convert.</param>
|
|||
/// <returns>An array of bytes with length 2.</returns>
|
|||
/// <returns>
|
|||
/// The <see cref="T:byte[]"/>.
|
|||
/// </returns>
|
|||
public byte[] GetBytes(char value) |
|||
{ |
|||
return this.GetBytes((short)value); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified double-precision floating point value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 8.</returns>
|
|||
public unsafe byte[] GetBytes(double value) |
|||
{ |
|||
return this.GetBytes(*((long*)&value)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified single-precision floating point value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 4.</returns>
|
|||
public unsafe byte[] GetBytes(float value) |
|||
{ |
|||
return this.GetBytes(*((int*)&value)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the specified decimal value as an array of bytes.
|
|||
/// </summary>
|
|||
/// <param name="value">The number to convert.</param>
|
|||
/// <returns>An array of bytes with length 16.</returns>
|
|||
public byte[] GetBytes(decimal value) |
|||
{ |
|||
byte[] result = new byte[16]; |
|||
this.CopyBytes(value, result, 0); |
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,141 @@ |
|||
// <copyright file="EndianBitConverter.ToType.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.IO |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Equivalent of <see cref="BitConverter"/>, but with either endianness.
|
|||
/// </summary>
|
|||
internal abstract partial class EndianBitConverter |
|||
{ |
|||
/// <summary>
|
|||
/// Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 16-bit signed integer formed by two bytes beginning at startIndex.</returns>
|
|||
public abstract short ToInt16(byte[] value, int startIndex); |
|||
|
|||
/// <summary>
|
|||
/// Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 32-bit signed integer formed by four bytes beginning at startIndex.</returns>
|
|||
public abstract int ToInt32(byte[] value, int startIndex); |
|||
|
|||
/// <summary>
|
|||
/// Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 64-bit signed integer formed by eight bytes beginning at startIndex.</returns>
|
|||
public abstract long ToInt64(byte[] value, int startIndex); |
|||
|
|||
/// <summary>
|
|||
/// Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 16-bit unsigned integer formed by two bytes beginning at startIndex.</returns>
|
|||
public ushort ToUInt16(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((ushort)this.ToInt16(value, startIndex)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 32-bit unsigned integer formed by four bytes beginning at startIndex.</returns>
|
|||
public uint ToUInt32(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((uint)this.ToInt32(value, startIndex)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A 64-bit unsigned integer formed by eight bytes beginning at startIndex.</returns>
|
|||
public ulong ToUInt64(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((ulong)this.ToInt64(value, startIndex)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a Boolean value converted from one byte at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>true if the byte at startIndex in value is nonzero; otherwise, false.</returns>
|
|||
public bool ToBoolean(byte[] value, int startIndex) |
|||
{ |
|||
CheckByteArgument(value, startIndex, 1); |
|||
return value[startIndex] != 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a Unicode character converted from two bytes at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A character formed by two bytes beginning at startIndex.</returns>
|
|||
public char ToChar(byte[] value, int startIndex) |
|||
{ |
|||
return unchecked((char)this.ToInt16(value, startIndex)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a double-precision floating point number converted from eight bytes
|
|||
/// at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A double precision floating point number formed by eight bytes beginning at startIndex.</returns>
|
|||
public unsafe double ToDouble(byte[] value, int startIndex) |
|||
{ |
|||
long intValue = this.ToInt64(value, startIndex); |
|||
return *((double*)&intValue); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a single-precision floating point number converted from four bytes
|
|||
/// at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A single precision floating point number formed by four bytes beginning at startIndex.</returns>
|
|||
public unsafe float ToSingle(byte[] value, int startIndex) |
|||
{ |
|||
int intValue = this.ToInt32(value, startIndex); |
|||
return *((float*)&intValue); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a decimal value converted from sixteen bytes
|
|||
/// at a specified position in a byte array.
|
|||
/// </summary>
|
|||
/// <param name="value">An array of bytes.</param>
|
|||
/// <param name="startIndex">The starting position within value.</param>
|
|||
/// <returns>A decimal formed by sixteen bytes beginning at startIndex.</returns>
|
|||
public unsafe decimal ToDecimal(byte[] value, int startIndex) |
|||
{ |
|||
CheckByteArgument(value, startIndex, 16); |
|||
|
|||
decimal result = 0m; |
|||
int* presult = (int*)&result; |
|||
presult[0] = this.ToInt32(value, startIndex); |
|||
presult[1] = this.ToInt32(value, startIndex + 4); |
|||
presult[2] = this.ToInt32(value, startIndex + 8); |
|||
presult[3] = this.ToInt32(value, startIndex + 12); |
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,83 @@ |
|||
// <copyright file="PngSmokeTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests.Formats.Png |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.IO; |
|||
using Xunit; |
|||
using ImageSharp.Formats; |
|||
using System.Linq; |
|||
using ImageSharp.IO; |
|||
|
|||
public class PngSmokeTests |
|||
{ |
|||
[Theory] |
|||
[WithTestPatternImages(300, 300, PixelTypes.All)] |
|||
public void GeneralTest<TColor>(TestImageProvider<TColor> provider) |
|||
where TColor : struct, IPixel<TColor> |
|||
{ |
|||
// does saving a file then repoening mean both files are identical???
|
|||
using (Image<TColor> image = provider.GetImage()) |
|||
using (MemoryStream ms = new MemoryStream()) |
|||
{ |
|||
// image.Save(provider.Utility.GetTestOutputFileName("bmp"));
|
|||
|
|||
image.Save(ms, new PngEncoder()); |
|||
ms.Position = 0; |
|||
using (Image img2 = Image.Load(ms, new PngDecoder())) |
|||
{ |
|||
// img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder());
|
|||
ImageComparer.CheckSimilarity(image, img2); |
|||
} |
|||
} |
|||
} |
|||
|
|||
[Theory] |
|||
[WithTestPatternImages(100, 100, PixelTypes.All)] |
|||
public void CanSaveIndexedPng<TColor>(TestImageProvider<TColor> provider) |
|||
where TColor : struct, IPixel<TColor> |
|||
{ |
|||
// does saving a file then repoening mean both files are identical???
|
|||
using (Image<TColor> image = provider.GetImage()) |
|||
using (MemoryStream ms = new MemoryStream()) |
|||
{ |
|||
// image.Save(provider.Utility.GetTestOutputFileName("bmp"));
|
|||
image.MetaData.Quality = 256; |
|||
image.Save(ms, new PngEncoder()); |
|||
ms.Position = 0; |
|||
using (Image img2 = Image.Load(ms, new PngDecoder())) |
|||
{ |
|||
// img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder());
|
|||
ImageComparer.CheckSimilarity(image, img2); |
|||
} |
|||
} |
|||
} |
|||
|
|||
[Theory] |
|||
[WithTestPatternImages(300, 300, PixelTypes.All)] |
|||
public void Resize<TColor>(TestImageProvider<TColor> provider) |
|||
where TColor : struct, IPixel<TColor> |
|||
{ |
|||
// does saving a file then repoening mean both files are identical???
|
|||
using (Image<TColor> image = provider.GetImage()) |
|||
using (MemoryStream ms = new MemoryStream()) |
|||
{ |
|||
// image.Save(provider.Utility.GetTestOutputFileName("png"));
|
|||
image.Resize(100, 100); |
|||
// image.Save(provider.Utility.GetTestOutputFileName("png", "resize"));
|
|||
|
|||
image.Save(ms, new PngEncoder()); |
|||
ms.Position = 0; |
|||
using (Image img2 = Image.Load(ms, new PngDecoder())) |
|||
{ |
|||
ImageComparer.CheckSimilarity(image, img2); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
namespace ImageSharp.Tests.Helpers |
|||
{ |
|||
using System; |
|||
|
|||
using Xunit; |
|||
|
|||
public class MathFTests |
|||
{ |
|||
[Fact] |
|||
public void MathF_PI_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.PI, (float)Math.PI); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MathF_Ceililng_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.Ceiling(0.3333F), (float)Math.Ceiling(0.3333F)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MathF_Abs_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.Abs(-0.3333F), (float)Math.Abs(-0.3333F)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MathF_Exp_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.Exp(1.2345F), (float)Math.Exp(1.2345F)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MathF_Floor_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.Floor(1.2345F), (float)Math.Floor(1.2345F)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MathF_Min_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.Min(1.2345F, 5.4321F), (float)Math.Min(1.2345F, 5.4321F)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MathF_Max_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.Max(1.2345F, 5.4321F), (float)Math.Max(1.2345F, 5.4321F)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MathF_Pow_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.Pow(1.2345F, 5.4321F), (float)Math.Pow(1.2345F, 5.4321F)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MathF_Sin_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.Sin(1.2345F), (float)Math.Sin(1.2345F)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MathF_Sqrt_Is_Equal() |
|||
{ |
|||
Assert.Equal(MathF.Sqrt(2F), (float)Math.Sqrt(2F)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,230 @@ |
|||
// <copyright file="BigEndianBitConverter.CopyBytesTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests.IO |
|||
{ |
|||
using System; |
|||
using ImageSharp.IO; |
|||
using Xunit; |
|||
|
|||
/// <summary>
|
|||
/// The <see cref="BigEndianBitConverter"/> tests.
|
|||
/// </summary>
|
|||
public class BigEndianBitConverterCopyBytesTests |
|||
{ |
|||
[Fact] |
|||
public void CopyToWithNullBufferThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(false, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.CopyBytes((short)42, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.CopyBytes((ushort)42, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(42, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(42u, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(42L, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.CopyBytes((ulong)42L, null, 0)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CopyToWithIndexTooBigThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(false, new byte[1], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes((short)42, new byte[2], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes((ushort)42, new byte[2], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(42, new byte[4], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(42u, new byte[4], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(42L, new byte[8], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes((ulong)42L, new byte[8], 1)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CopyToWithBufferTooSmallThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(false, new byte[0], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes((short)42, new byte[1], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes((ushort)42, new byte[1], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(42, new byte[3], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(42u, new byte[3], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes(42L, new byte[7], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.CopyBytes((ulong)42L, new byte[7], 0)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="bool"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesBoolean() |
|||
{ |
|||
byte[] buffer = new byte[1]; |
|||
|
|||
EndianBitConverter.BigEndianConverter.CopyBytes(false, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(true, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="short"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesShort() |
|||
{ |
|||
byte[] buffer = new byte[2]; |
|||
|
|||
EndianBitConverter.BigEndianConverter.CopyBytes((short)0, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((short)1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((short)256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((short)-1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((short)257, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="ushort"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesUShort() |
|||
{ |
|||
byte[] buffer = new byte[2]; |
|||
|
|||
EndianBitConverter.BigEndianConverter.CopyBytes((ushort)0, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((ushort)1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((ushort)256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(ushort.MaxValue, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((ushort)257, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="int"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesInt() |
|||
{ |
|||
byte[] buffer = new byte[4]; |
|||
|
|||
EndianBitConverter.BigEndianConverter.CopyBytes(0, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 1 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(65536, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(16777216, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(-1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255, 255, 255 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(257, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="uint"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesUInt() |
|||
{ |
|||
byte[] buffer = new byte[4]; |
|||
|
|||
EndianBitConverter.BigEndianConverter.CopyBytes((uint)0, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((uint)1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 1 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((uint)256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((uint)65536, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((uint)16777216, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(uint.MaxValue, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255, 255, 255 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes((uint)257, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="long"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesLong() |
|||
{ |
|||
byte[] buffer = new byte[8]; |
|||
|
|||
EndianBitConverter.BigEndianConverter.CopyBytes(0L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(1L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(256L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 1, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(65536L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(16777216L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(4294967296L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 1, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(1099511627776L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(1099511627776L * 256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(1099511627776L * 256 * 256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(-1L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(257L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 1, 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="ulong"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesULong() |
|||
{ |
|||
byte[] buffer = new byte[8]; |
|||
|
|||
EndianBitConverter.BigEndianConverter.CopyBytes(0UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(1UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(256UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 1, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(65536UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(16777216UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(4294967296UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 1, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(1099511627776UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(1099511627776UL * 256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(1099511627776UL * 256 * 256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(ulong.MaxValue, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255 }, buffer); |
|||
EndianBitConverter.BigEndianConverter.CopyBytes(257UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 1, 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the two byte arrays for equality.
|
|||
/// </summary>
|
|||
/// <param name="expected">The expected bytes.</param>
|
|||
/// <param name="actual">The actual bytes.</param>
|
|||
private void CheckBytes(byte[] expected, byte[] actual) |
|||
{ |
|||
Assert.Equal(expected.Length, actual.Length); |
|||
Assert.Equal(expected, actual); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,214 @@ |
|||
// <copyright file="BigEndianBitConverter.ToTypeTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests.IO |
|||
{ |
|||
using System; |
|||
using ImageSharp.IO; |
|||
using Xunit; |
|||
|
|||
/// <summary>
|
|||
/// The <see cref="BigEndianBitConverter"/> tests.
|
|||
/// </summary>
|
|||
public class BigEndianBitConverterTests |
|||
{ |
|||
[Fact] |
|||
public void CopyToWithNullBufferThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.ToBoolean(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.ToInt16(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.ToUInt16(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.ToInt32(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.ToUInt32(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.ToInt64(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.BigEndianConverter.ToUInt64(null, 0)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CopyToWithIndexTooBigThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToBoolean(new byte[1], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToInt16(new byte[2], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToUInt16(new byte[2], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToInt32(new byte[4], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToUInt32(new byte[4], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToInt64(new byte[8], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToUInt64(new byte[8], 1)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CopyToWithBufferTooSmallThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToBoolean(new byte[0], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToInt16(new byte[1], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToUInt16(new byte[1], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToInt32(new byte[3], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToUInt32(new byte[3], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToInt64(new byte[7], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.BigEndianConverter.ToUInt64(new byte[7], 0)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="bool"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToBoolean() |
|||
{ |
|||
Assert.Equal(false, EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 0 }, 0)); |
|||
Assert.Equal(true, EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 1 }, 0)); |
|||
Assert.Equal(true, EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 42 }, 0)); |
|||
|
|||
Assert.Equal(false, EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 1, 0 }, 1)); |
|||
Assert.Equal(true, EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 0, 1 }, 1)); |
|||
Assert.Equal(true, EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 0, 42 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="short"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToInt16() |
|||
{ |
|||
Assert.Equal((short)0, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 0, 0 }, 0)); |
|||
Assert.Equal((short)1, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 0, 1 }, 0)); |
|||
Assert.Equal((short)256, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 1, 0 }, 0)); |
|||
Assert.Equal((short)-1, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 255, 255 }, 0)); |
|||
Assert.Equal((short)257, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 1, 1 }, 0)); |
|||
|
|||
Assert.Equal((short)0, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 1, 0, 0 }, 1)); |
|||
Assert.Equal((short)1, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 1, 0, 1 }, 1)); |
|||
Assert.Equal((short)256, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 0, 1, 0 }, 1)); |
|||
Assert.Equal((short)-1, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 0, 255, 255 }, 1)); |
|||
Assert.Equal((short)257, EndianBitConverter.BigEndianConverter.ToInt16(new byte[] { 0, 1, 1 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="ushort"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToUInt16() |
|||
{ |
|||
Assert.Equal((ushort)0, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 0, 0 }, 0)); |
|||
Assert.Equal((ushort)1, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 0, 1 }, 0)); |
|||
Assert.Equal((ushort)256, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 1, 0 }, 0)); |
|||
Assert.Equal(ushort.MaxValue, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 255, 255 }, 0)); |
|||
Assert.Equal((ushort)257, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 1, 1 }, 0)); |
|||
|
|||
Assert.Equal((ushort)0, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 1, 0, 0 }, 1)); |
|||
Assert.Equal((ushort)1, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 1, 0, 1 }, 1)); |
|||
Assert.Equal((ushort)256, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 0, 1, 0 }, 1)); |
|||
Assert.Equal(ushort.MaxValue, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 0, 255, 255 }, 1)); |
|||
Assert.Equal((ushort)257, EndianBitConverter.BigEndianConverter.ToUInt16(new byte[] { 0, 1, 1 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="int"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToInt32() |
|||
{ |
|||
Assert.Equal(0, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 0, 0, 0, 1 }, 0)); |
|||
Assert.Equal(256, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 0, 0, 1, 0 }, 0)); |
|||
Assert.Equal(65536, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 0, 1, 0, 0 }, 0)); |
|||
Assert.Equal(16777216, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 1, 0, 0, 0 }, 0)); |
|||
Assert.Equal(-1, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 255, 255, 255, 255 }, 0)); |
|||
Assert.Equal(257, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 0, 0, 1, 1 }, 0)); |
|||
|
|||
Assert.Equal(0, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 1, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 1, 0, 0, 0, 1 }, 1)); |
|||
Assert.Equal(256, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 1, 0, 0, 1, 0 }, 1)); |
|||
Assert.Equal(65536, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 1, 0, 1, 0, 0 }, 1)); |
|||
Assert.Equal(16777216, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 0, 1, 0, 0, 0 }, 1)); |
|||
Assert.Equal(-1, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 0, 255, 255, 255, 255 }, 1)); |
|||
Assert.Equal(257, EndianBitConverter.BigEndianConverter.ToInt32(new byte[] { 1, 0, 0, 1, 1 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="uint"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToUInt32() |
|||
{ |
|||
Assert.Equal((uint)0, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal((uint)1, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 0, 0, 0, 1 }, 0)); |
|||
Assert.Equal((uint)256, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 0, 0, 1, 0 }, 0)); |
|||
Assert.Equal((uint)65536, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 0, 1, 0, 0 }, 0)); |
|||
Assert.Equal((uint)16777216, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 1, 0, 0, 0 }, 0)); |
|||
Assert.Equal(uint.MaxValue, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 255, 255, 255, 255 }, 0)); |
|||
Assert.Equal((uint)257, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 0, 0, 1, 1 }, 0)); |
|||
|
|||
Assert.Equal((uint)0, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 1, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal((uint)1, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 1, 0, 0, 0, 1 }, 1)); |
|||
Assert.Equal((uint)256, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 1, 0, 0, 1, 0 }, 1)); |
|||
Assert.Equal((uint)65536, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 1, 0, 1, 0, 0 }, 1)); |
|||
Assert.Equal((uint)16777216, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 0, 1, 0, 0, 0 }, 1)); |
|||
Assert.Equal(uint.MaxValue, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 0, 255, 255, 255, 255 }, 1)); |
|||
Assert.Equal((uint)257, EndianBitConverter.BigEndianConverter.ToUInt32(new byte[] { 1, 0, 0, 1, 1 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="long"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToInt64() |
|||
{ |
|||
Assert.Equal(0L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }, 0)); |
|||
Assert.Equal(256L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 0, 0, 1, 0 }, 0)); |
|||
Assert.Equal(65536L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, 0)); |
|||
Assert.Equal(16777216L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, 0)); |
|||
Assert.Equal(4294967296L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 0, 0, 1, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776L * 256, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776L * 256 * 256, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(-1L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255 }, 0)); |
|||
Assert.Equal(257L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 0, 0, 1, 1 }, 0)); |
|||
|
|||
Assert.Equal(0L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 }, 1)); |
|||
Assert.Equal(256L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 1, 0 }, 1)); |
|||
Assert.Equal(65536L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 1, 0, 0 }, 1)); |
|||
Assert.Equal(16777216L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 1, 0, 0, 0 }, 1)); |
|||
Assert.Equal(4294967296L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 0, 1, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776L * 256, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 1, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776L * 256 * 256, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(-1L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 0, 255, 255, 255, 255, 255, 255, 255, 255 }, 1)); |
|||
Assert.Equal(257L, EndianBitConverter.BigEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 1, 1 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="ulong"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToUInt64() |
|||
{ |
|||
Assert.Equal(0UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }, 0)); |
|||
Assert.Equal(256UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 0, 0, 1, 0 }, 0)); |
|||
Assert.Equal(65536UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, 0)); |
|||
Assert.Equal(16777216UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, 0)); |
|||
Assert.Equal(4294967296UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 1, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776UL * 256, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776UL * 256 * 256, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(ulong.MaxValue, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255 }, 0)); |
|||
Assert.Equal(257UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 0, 0, 1, 1 }, 0)); |
|||
|
|||
Assert.Equal(0UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 }, 1)); |
|||
Assert.Equal(256UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 1, 0 }, 1)); |
|||
Assert.Equal(65536UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 1, 0, 0 }, 1)); |
|||
Assert.Equal(16777216UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 1, 0, 0, 0 }, 1)); |
|||
Assert.Equal(4294967296UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 1, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776UL * 256, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 1, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776UL * 256 * 256, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(ulong.MaxValue, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 0, 255, 255, 255, 255, 255, 255, 255, 255 }, 1)); |
|||
Assert.Equal(257UL, EndianBitConverter.BigEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 1, 1 }, 1)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,230 @@ |
|||
// <copyright file="LittleEndianBitConverter.CopyBytesTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests.IO |
|||
{ |
|||
using System; |
|||
using ImageSharp.IO; |
|||
using Xunit; |
|||
|
|||
/// <summary>
|
|||
/// The <see cref="LittleEndianBitConverter"/> tests.
|
|||
/// </summary>
|
|||
public class LittleEndianBitConverterCopyBytesTests |
|||
{ |
|||
[Fact] |
|||
public void CopyToWithNullBufferThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(false, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes((short)42, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes((ushort)42, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(42, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(42u, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(42L, null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes((ulong)42L, null, 0)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CopyToWithIndexTooBigThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(false, new byte[1], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes((short)42, new byte[2], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes((ushort)42, new byte[2], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(42, new byte[4], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(42u, new byte[4], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(42L, new byte[8], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes((ulong)42L, new byte[8], 1)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CopyToWithBufferTooSmallThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(false, new byte[0], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes((short)42, new byte[1], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes((ushort)42, new byte[1], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(42, new byte[3], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(42u, new byte[3], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes(42L, new byte[7], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.CopyBytes((ulong)42L, new byte[7], 0)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="bool"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesBoolean() |
|||
{ |
|||
byte[] buffer = new byte[1]; |
|||
|
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(false, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(true, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="short"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesShort() |
|||
{ |
|||
byte[] buffer = new byte[2]; |
|||
|
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((short)0, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((short)1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((short)256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((short)-1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((short)257, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="ushort"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesUShort() |
|||
{ |
|||
byte[] buffer = new byte[2]; |
|||
|
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((ushort)0, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((ushort)1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((ushort)256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(ushort.MaxValue, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((ushort)257, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 1 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="int"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesInt() |
|||
{ |
|||
byte[] buffer = new byte[4]; |
|||
|
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(0, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(65536, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(16777216, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 1 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(-1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255, 255, 255 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(257, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 1, 0, 0 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="uint"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesUInt() |
|||
{ |
|||
byte[] buffer = new byte[4]; |
|||
|
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((uint)0, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((uint)1, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((uint)256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((uint)65536, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((uint)16777216, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 1 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(uint.MaxValue, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255, 255, 255 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes((uint)257, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 1, 0, 0 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="long"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesLong() |
|||
{ |
|||
byte[] buffer = new byte[8]; |
|||
|
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(0L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(1L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(256L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(65536L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(16777216L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 1, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(4294967296L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(1099511627776L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(1099511627776L * 256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 1, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(1099511627776L * 256 * 256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(-1L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(257L, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 1, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="ulong"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void CopyBytesULong() |
|||
{ |
|||
byte[] buffer = new byte[8]; |
|||
|
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(0UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(1UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(256UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(65536UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(16777216UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 1, 0, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(4294967296UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(1099511627776UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(1099511627776UL * 256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 1, 0 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(1099511627776UL * 256 * 256, buffer, 0); |
|||
this.CheckBytes(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(ulong.MaxValue, buffer, 0); |
|||
this.CheckBytes(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255 }, buffer); |
|||
EndianBitConverter.LittleEndianConverter.CopyBytes(257UL, buffer, 0); |
|||
this.CheckBytes(new byte[] { 1, 1, 0, 0, 0, 0, 0, 0 }, buffer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the two byte arrays for equality.
|
|||
/// </summary>
|
|||
/// <param name="expected">The expected bytes.</param>
|
|||
/// <param name="actual">The actual bytes.</param>
|
|||
private void CheckBytes(byte[] expected, byte[] actual) |
|||
{ |
|||
Assert.Equal(expected.Length, actual.Length); |
|||
Assert.Equal(expected, actual); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,212 @@ |
|||
// <copyright file="LittleEndianBitConverter.ToTypeTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests.IO |
|||
{ |
|||
using System; |
|||
using ImageSharp.IO; |
|||
using Xunit; |
|||
|
|||
/// <summary>
|
|||
/// The <see cref="LittleEndianBitConverter"/> tests.
|
|||
/// </summary>
|
|||
public class LittleEndianBitConverterToTypeTests |
|||
{ |
|||
[Fact] |
|||
public void CopyToWithNullBufferThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.ToBoolean(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.ToInt16(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.ToUInt16(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.ToInt32(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.ToUInt32(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.ToInt64(null, 0)); |
|||
Assert.Throws<ArgumentNullException>(() => EndianBitConverter.LittleEndianConverter.ToUInt64(null, 0)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CopyToWithIndexTooBigThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[1], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToInt16(new byte[2], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[2], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToInt32(new byte[4], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[4], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToInt64(new byte[8], 1)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[8], 1)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CopyToWithBufferTooSmallThrowsException() |
|||
{ |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[0], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToInt16(new byte[1], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[1], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToInt32(new byte[3], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[3], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToInt64(new byte[7], 0)); |
|||
Assert.Throws<ArgumentOutOfRangeException>(() => EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[7], 0)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="bool"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToBoolean() |
|||
{ |
|||
Assert.Equal(false, EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[] { 0 }, 0)); |
|||
Assert.Equal(true, EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[] { 1 }, 0)); |
|||
|
|||
Assert.Equal(false, EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[] { 1, 0 }, 1)); |
|||
Assert.Equal(true, EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[] { 0, 1 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="short"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToInt16() |
|||
{ |
|||
Assert.Equal((short)0, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 0, 0 }, 0)); |
|||
Assert.Equal((short)1, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 1, 0 }, 0)); |
|||
Assert.Equal((short)256, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 0, 1 }, 0)); |
|||
Assert.Equal((short)-1, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 255, 255 }, 0)); |
|||
Assert.Equal((short)257, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 1, 1 }, 0)); |
|||
|
|||
Assert.Equal((short)0, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 1, 0, 0 }, 1)); |
|||
Assert.Equal((short)1, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 0, 1, 0 }, 1)); |
|||
Assert.Equal((short)256, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 1, 0, 1 }, 1)); |
|||
Assert.Equal((short)-1, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 0, 255, 255 }, 1)); |
|||
Assert.Equal((short)257, EndianBitConverter.LittleEndianConverter.ToInt16(new byte[] { 0, 1, 1 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="ushort"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToUInt16() |
|||
{ |
|||
Assert.Equal((ushort)0, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 0, 0 }, 0)); |
|||
Assert.Equal((ushort)1, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 1, 0 }, 0)); |
|||
Assert.Equal((ushort)256, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 0, 1 }, 0)); |
|||
Assert.Equal(ushort.MaxValue, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 255, 255 }, 0)); |
|||
Assert.Equal((ushort)257, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 1, 1 }, 0)); |
|||
|
|||
Assert.Equal((ushort)0, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 1, 0, 0 }, 1)); |
|||
Assert.Equal((ushort)1, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 0, 1, 0 }, 1)); |
|||
Assert.Equal((ushort)256, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 1, 0, 1 }, 1)); |
|||
Assert.Equal(ushort.MaxValue, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 0, 255, 255 }, 1)); |
|||
Assert.Equal((ushort)257, EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[] { 0, 1, 1 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="int"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToInt32() |
|||
{ |
|||
Assert.Equal(0, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 1, 0, 0, 0 }, 0)); |
|||
Assert.Equal(256, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 0, 1, 0, 0 }, 0)); |
|||
Assert.Equal(65536, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 0, 0, 1, 0 }, 0)); |
|||
Assert.Equal(16777216, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 0, 0, 0, 1 }, 0)); |
|||
Assert.Equal(-1, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 255, 255, 255, 255 }, 0)); |
|||
Assert.Equal(257, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 1, 1, 0, 0 }, 0)); |
|||
|
|||
Assert.Equal(0, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 1, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 0, 1, 0, 0, 0 }, 1)); |
|||
Assert.Equal(256, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 1, 0, 1, 0, 0 }, 1)); |
|||
Assert.Equal(65536, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 1, 0, 0, 1, 0 }, 1)); |
|||
Assert.Equal(16777216, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 1, 0, 0, 0, 1 }, 1)); |
|||
Assert.Equal(-1, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 0, 255, 255, 255, 255 }, 1)); |
|||
Assert.Equal(257, EndianBitConverter.LittleEndianConverter.ToInt32(new byte[] { 0, 1, 1, 0, 0 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="uint"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToUInt32() |
|||
{ |
|||
Assert.Equal((uint)0, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal((uint)1, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 1, 0, 0, 0 }, 0)); |
|||
Assert.Equal((uint)256, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 0, 1, 0, 0 }, 0)); |
|||
Assert.Equal((uint)65536, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 0, 0, 1, 0 }, 0)); |
|||
Assert.Equal((uint)16777216, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 0, 0, 0, 1 }, 0)); |
|||
Assert.Equal(uint.MaxValue, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 255, 255, 255, 255 }, 0)); |
|||
Assert.Equal((uint)257, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 1, 1, 0, 0 }, 0)); |
|||
|
|||
Assert.Equal((uint)0, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 1, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal((uint)1, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 0, 1, 0, 0, 0 }, 1)); |
|||
Assert.Equal((uint)256, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 1, 0, 1, 0, 0 }, 1)); |
|||
Assert.Equal((uint)65536, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 1, 0, 0, 1, 0 }, 1)); |
|||
Assert.Equal((uint)16777216, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 1, 0, 0, 0, 1 }, 1)); |
|||
Assert.Equal(uint.MaxValue, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 0, 255, 255, 255, 255 }, 1)); |
|||
Assert.Equal((uint)257, EndianBitConverter.LittleEndianConverter.ToUInt32(new byte[] { 0, 1, 1, 0, 0 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="long"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToInt64() |
|||
{ |
|||
Assert.Equal(0L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(256L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(65536L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(16777216L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 0, 0, 1, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(4294967296L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776L * 256, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 0, 0, 1, 0 }, 0)); |
|||
Assert.Equal(1099511627776L * 256 * 256, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }, 0)); |
|||
Assert.Equal(-1L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255 }, 0)); |
|||
Assert.Equal(257L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 1, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
|
|||
Assert.Equal(0L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(256L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 0, 1, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(65536L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 0, 0, 1, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(16777216L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(4294967296L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 1, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 1, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776L * 256, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 1, 0 }, 1)); |
|||
Assert.Equal(1099511627776L * 256 * 256, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 }, 1)); |
|||
Assert.Equal(-1L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 255, 255, 255, 255, 255, 255, 255, 255 }, 1)); |
|||
Assert.Equal(257L, EndianBitConverter.LittleEndianConverter.ToInt64(new byte[] { 0, 1, 1, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests that passing a <see cref="ulong"/> returns the correct bytes.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void ToUInt64() |
|||
{ |
|||
Assert.Equal(0UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(256UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(65536UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(16777216UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 1, 0, 0, 0, 0 }, 0)); |
|||
Assert.Equal(4294967296UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, 0)); |
|||
Assert.Equal(1099511627776UL * 256, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 0, 0, 1, 0 }, 0)); |
|||
Assert.Equal(1099511627776UL * 256 * 256, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }, 0)); |
|||
Assert.Equal(ulong.MaxValue, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255 }, 0)); |
|||
Assert.Equal(257UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 1, 0, 0, 0, 0, 0, 0 }, 0)); |
|||
|
|||
Assert.Equal(0UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(256UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 0, 1, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(65536UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 1, 0, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(16777216UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0, 0 }, 1)); |
|||
Assert.Equal(4294967296UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 1, 0, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 1, 0, 0 }, 1)); |
|||
Assert.Equal(1099511627776UL * 256, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 1, 0 }, 1)); |
|||
Assert.Equal(1099511627776UL * 256 * 256, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 }, 1)); |
|||
Assert.Equal(ulong.MaxValue, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 255, 255, 255, 255, 255, 255, 255, 255 }, 1)); |
|||
Assert.Equal(257UL, EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[] { 0, 1, 1, 0, 0, 0, 0, 0, 0 }, 1)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
using System; |
|||
using System.IO; |
|||
|
|||
namespace ImageSharp.Tests |
|||
{ |
|||
internal class NoneSeekableStream : Stream |
|||
{ |
|||
private Stream dataStream; |
|||
|
|||
public NoneSeekableStream(Stream dataStream) |
|||
{ |
|||
this.dataStream = dataStream; |
|||
} |
|||
|
|||
public override bool CanRead => this.dataStream.CanRead; |
|||
|
|||
public override bool CanSeek => false; |
|||
|
|||
public override bool CanWrite => false; |
|||
|
|||
public override long Length => this.dataStream.Length; |
|||
|
|||
public override long Position { get => this.dataStream.Position; set => throw new NotImplementedException(); } |
|||
|
|||
public override void Flush() |
|||
{ |
|||
this.dataStream.Flush(); |
|||
} |
|||
|
|||
public override int Read(byte[] buffer, int offset, int count) |
|||
{ |
|||
return this.dataStream.Read(buffer, offset, count); |
|||
} |
|||
|
|||
public override long Seek(long offset, SeekOrigin origin) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
public override void SetLength(long value) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
public override void Write(byte[] buffer, int offset, int count) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,119 @@ |
|||
namespace ImageSharp.Tests |
|||
{ |
|||
using System; |
|||
using ImageSharp; |
|||
using Xunit; |
|||
|
|||
/// <summary>
|
|||
/// Class to perform simple image comparisons.
|
|||
/// </summary>
|
|||
public static class ImageComparer |
|||
{ |
|||
const int DefaultScalingFactor = 32; // this is means the images get scaled into a 32x32 image to sample pixels
|
|||
const int DefaultSegmentThreshold = 3; // the greyscale difference between 2 segements my be > 3 before it influances the overall difference
|
|||
const float DefaultImageThreshold = 0.000f; // after segment threasholds the images must have no differences
|
|||
|
|||
/// <summary>
|
|||
/// Does a visual comparison between 2 images and then asserts the difference is less then a configurable threshold
|
|||
/// </summary>
|
|||
/// <typeparam name="TColorA">The color of the expected image</typeparam>
|
|||
/// <typeparam name="TColorB">The color type fo the the actual image</typeparam>
|
|||
/// <param name="expected">The expected image</param>
|
|||
/// <param name="actual">The actual image</param>
|
|||
/// <param name="imageTheshold">
|
|||
/// The threshold for the percentage difference where the images are asumed to be the same.
|
|||
/// The default/undefined value is <see cref="ImageComparer.DefaultImageThreshold"/>
|
|||
/// </param>
|
|||
/// <param name="segmentThreshold">
|
|||
/// The threashold of the individual segments before it acumulates towards the overall difference.
|
|||
/// The default undefined value is <see cref="ImageComparer.DefaultSegmentThreshold"/>
|
|||
/// </param>
|
|||
/// <param name="scalingFactor">
|
|||
/// This is a sampling factor we sample a grid of average pixels <paramref name="scalingFactor"/> width by <paramref name="scalingFactor"/> high
|
|||
/// The default undefined value is <see cref="ImageComparer.DefaultScalingFactor"/>
|
|||
/// </param>
|
|||
public static void CheckSimilarity<TColorA, TColorB>(Image<TColorA> expected, Image<TColorB> actual, float imageTheshold = DefaultImageThreshold, byte segmentThreshold = DefaultSegmentThreshold, int scalingFactor = DefaultScalingFactor) |
|||
where TColorA : struct, IPixel<TColorA> |
|||
where TColorB : struct, IPixel<TColorB> |
|||
{ |
|||
float percentage = expected.PercentageDifference(actual, segmentThreshold, scalingFactor); |
|||
|
|||
Assert.InRange(percentage, 0, imageTheshold); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Does a visual comparison between 2 images and then and returns the percentage diffence between the 2
|
|||
/// </summary>
|
|||
/// <typeparam name="TColorA">The color of the source image</typeparam>
|
|||
/// <typeparam name="TColorB">The color type for the target image</typeparam>
|
|||
/// <param name="source">The source image</param>
|
|||
/// <param name="target">The target image</param>
|
|||
/// <param name="segmentThreshold">
|
|||
/// The threashold of the individual segments before it acumulates towards the overall difference.
|
|||
/// The default undefined value is <see cref="ImageComparer.DefaultSegmentThreshold"/>
|
|||
/// </param>
|
|||
/// <param name="scalingFactor">
|
|||
/// This is a sampling factor we sample a grid of average pixels <paramref name="scalingFactor"/> width by <paramref name="scalingFactor"/> high
|
|||
/// The default undefined value is <see cref="ImageComparer.DefaultScalingFactor"/>
|
|||
/// </param>
|
|||
/// <returns>Returns a number from 0 - 1 which represents the diference focter between the images.</returns>
|
|||
public static float PercentageDifference<TColorA, TColorB>(this Image<TColorA> source, Image<TColorB> target, byte segmentThreshold = DefaultSegmentThreshold, int scalingFactor = DefaultScalingFactor) |
|||
where TColorA : struct, IPixel<TColorA> |
|||
where TColorB : struct, IPixel<TColorB> |
|||
{ |
|||
// code adapted from https://www.codeproject.com/Articles/374386/Simple-image-comparison-in-NET
|
|||
Fast2DArray<byte> differences = GetDifferences(source, target, scalingFactor); |
|||
|
|||
int diffPixels = 0; |
|||
|
|||
foreach (byte b in differences.Data) |
|||
{ |
|||
if (b > segmentThreshold) { diffPixels++; } |
|||
} |
|||
|
|||
return diffPixels / (scalingFactor * scalingFactor); |
|||
} |
|||
|
|||
private static Fast2DArray<byte> GetDifferences<TColorA, TColorB>(Image<TColorA> source, Image<TColorB> target, int scalingFactor) |
|||
where TColorA : struct, IPixel<TColorA> |
|||
where TColorB : struct, IPixel<TColorB> |
|||
{ |
|||
Fast2DArray<byte> differences = new Fast2DArray<byte>(scalingFactor, scalingFactor); |
|||
Fast2DArray<byte> firstGray = source.GetGrayScaleValues(scalingFactor); |
|||
Fast2DArray<byte> secondGray = target.GetGrayScaleValues(scalingFactor); |
|||
|
|||
for (int y = 0; y < scalingFactor; y++) |
|||
{ |
|||
for (int x = 0; x < scalingFactor; x++) |
|||
{ |
|||
differences[x, y] = (byte)Math.Abs(firstGray[x, y] - secondGray[x, y]); |
|||
} |
|||
} |
|||
|
|||
return differences; |
|||
} |
|||
|
|||
private static Fast2DArray<byte> GetGrayScaleValues<TColorA>(this Image<TColorA> source, int scalingFactor) |
|||
where TColorA : struct, IPixel<TColorA> |
|||
{ |
|||
byte[] buffer = new byte[4]; |
|||
using (Image<TColorA> img = new Image<TColorA>(source).Resize(scalingFactor, scalingFactor).Grayscale()) |
|||
{ |
|||
using (PixelAccessor<TColorA> pixels = img.Lock()) |
|||
{ |
|||
Fast2DArray<byte> grayScale = new Fast2DArray<byte>(scalingFactor, scalingFactor); |
|||
for (int y = 0; y < scalingFactor; y++) |
|||
{ |
|||
for (int x = 0; x < scalingFactor; x++) |
|||
{ |
|||
pixels[x, y].ToXyzBytes(buffer, 0); |
|||
grayScale[x, y] = buffer[1]; |
|||
} |
|||
} |
|||
|
|||
return grayScale; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 6.4 KiB |
@ -0,0 +1,38 @@ |
|||
// <copyright file="WithBlankImagesAttribute.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests |
|||
{ |
|||
using System; |
|||
using System.Reflection; |
|||
|
|||
/// <summary>
|
|||
/// Triggers passing <see cref="TestImageProvider{TColor}"/> instances which produce a blank image of size width * height.
|
|||
/// One <see cref="TestImageProvider{TColor}"/> instance will be passed for each the pixel format defined by the pixelTypes parameter
|
|||
/// </summary>
|
|||
public class WithTestPatternImagesAttribute : ImageDataAttributeBase |
|||
{ |
|||
/// <summary>
|
|||
/// Triggers passing an <see cref="TestImageProvider{TColor}"/> that produces a test pattern image of size width * height
|
|||
/// </summary>
|
|||
/// <param name="width">The required width</param>
|
|||
/// <param name="height">The required height</param>
|
|||
/// <param name="pixelTypes">The requested parameter</param>
|
|||
/// <param name="additionalParameters">Additional theory parameter values</param>
|
|||
public WithTestPatternImagesAttribute(int width, int height, PixelTypes pixelTypes, params object[] additionalParameters) |
|||
: base(pixelTypes, additionalParameters) |
|||
{ |
|||
this.Width = width; |
|||
this.Height = height; |
|||
} |
|||
|
|||
public int Width { get; } |
|||
public int Height { get; } |
|||
|
|||
protected override string GetFactoryMethodName(MethodInfo testMethod) => "TestPattern"; |
|||
|
|||
protected override object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType) => new object[] { this.Width, this.Height }; |
|||
} |
|||
} |
|||
@ -0,0 +1,210 @@ |
|||
// <copyright file="BlankProvider.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Numerics; |
|||
using Xunit.Abstractions; |
|||
|
|||
public abstract partial class TestImageProvider<TColor> |
|||
where TColor : struct, IPixel<TColor> |
|||
{ |
|||
|
|||
/// <summary>
|
|||
/// A test image provider that produces test patterns.
|
|||
/// </summary>
|
|||
/// <typeparam name="TColor"></typeparam>
|
|||
private class TestPatternProvider : BlankProvider |
|||
{ |
|||
static Dictionary<string, Image<TColor>> testImages = new Dictionary<string, Image<TColor>>(); |
|||
|
|||
public TestPatternProvider(int width, int height) |
|||
: base(width, height) |
|||
{ |
|||
} |
|||
|
|||
public TestPatternProvider() |
|||
: base() |
|||
{ |
|||
} |
|||
|
|||
public override string SourceFileOrDescription => $"TestPattern{this.Width}x{this.Height}"; |
|||
|
|||
public override Image<TColor> GetImage() |
|||
{ |
|||
lock (testImages) |
|||
{ |
|||
if (!testImages.ContainsKey(this.SourceFileOrDescription)) |
|||
{ |
|||
Image<TColor> image = new Image<TColor>(this.Width, this.Height); |
|||
DrawTestPattern(image); |
|||
testImages.Add(this.SourceFileOrDescription, image); |
|||
} |
|||
} |
|||
|
|||
return new Image<TColor>(testImages[this.SourceFileOrDescription]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Draws the test pattern on an image by drawing 4 other patterns in the for quadrants of the image.
|
|||
/// </summary>
|
|||
/// <param name="image"></param>
|
|||
private static void DrawTestPattern(Image<TColor> image) |
|||
{ |
|||
// first lets split the image into 4 quadrants
|
|||
using (PixelAccessor<TColor> pixels = image.Lock()) |
|||
{ |
|||
BlackWhiteChecker(pixels); // top left
|
|||
VirticalBars(pixels); // top right
|
|||
TransparentGradients(pixels); // bottom left
|
|||
Rainbow(pixels); // bottom right
|
|||
} |
|||
} |
|||
/// <summary>
|
|||
/// Fills the top right quadrant with alternating solid vertical bars.
|
|||
/// </summary>
|
|||
/// <param name="pixels"></param>
|
|||
private static void VirticalBars(PixelAccessor<TColor> pixels) |
|||
{ |
|||
// topLeft
|
|||
int left = pixels.Width / 2; |
|||
int right = pixels.Width; |
|||
int top = 0; |
|||
int bottom = pixels.Height / 2; |
|||
int stride = pixels.Width / 12; |
|||
TColor[] c = { |
|||
NamedColors<TColor>.HotPink, |
|||
NamedColors<TColor>.Blue |
|||
}; |
|||
int p = 0; |
|||
for (int y = top; y < bottom; y++) |
|||
{ |
|||
for (int x = left; x < right; x++) |
|||
{ |
|||
if (x % stride == 0) |
|||
{ |
|||
p++; |
|||
p = p % c.Length; |
|||
} |
|||
pixels[x, y] = c[p]; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// fills the top left quadrant with a black and white checker board.
|
|||
/// </summary>
|
|||
/// <param name="pixels"></param>
|
|||
private static void BlackWhiteChecker(PixelAccessor<TColor> pixels) |
|||
{ |
|||
// topLeft
|
|||
int left = 0; |
|||
int right = pixels.Width / 2; |
|||
int top = 0; |
|||
int bottom = pixels.Height / 2; |
|||
int stride = pixels.Width / 6; |
|||
TColor[] c = { |
|||
NamedColors<TColor>.Black, |
|||
NamedColors<TColor>.White |
|||
}; |
|||
|
|||
int p = 0; |
|||
for (int y = top; y < bottom; y++) |
|||
{ |
|||
if (y % stride == 0) |
|||
{ |
|||
p++; |
|||
p = p % c.Length; |
|||
} |
|||
int pstart = p; |
|||
for (int x = left; x < right; x++) |
|||
{ |
|||
if (x % stride == 0) |
|||
{ |
|||
p++; |
|||
p = p % c.Length; |
|||
} |
|||
pixels[x, y] = c[p]; |
|||
} |
|||
p = pstart; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Fills the bottom left quadrent with 3 horizental bars in Red, Green and Blue with a alpha gradient from left (transparent) to right (solid).
|
|||
/// </summary>
|
|||
/// <param name="pixels"></param>
|
|||
private static void TransparentGradients(PixelAccessor<TColor> pixels) |
|||
{ |
|||
// topLeft
|
|||
int left = 0; |
|||
int right = pixels.Width / 2; |
|||
int top = pixels.Height / 2; |
|||
int bottom = pixels.Height; |
|||
int height = (int)Math.Ceiling(pixels.Height / 6f); |
|||
|
|||
Vector4 red = Color.Red.ToVector4(); // use real color so we can see har it translates in the test pattern
|
|||
Vector4 green = Color.Green.ToVector4(); // use real color so we can see har it translates in the test pattern
|
|||
Vector4 blue = Color.Blue.ToVector4(); // use real color so we can see har it translates in the test pattern
|
|||
|
|||
TColor c = default(TColor); |
|||
|
|||
for (int x = left; x < right; x++) |
|||
{ |
|||
blue.W = red.W = green.W = (float)x / (float)right; |
|||
|
|||
c.PackFromVector4(red); |
|||
int topBand = top; |
|||
for (int y = topBand; y < top + height; y++) |
|||
{ |
|||
pixels[x, y] = c; |
|||
} |
|||
topBand = topBand + height; |
|||
c.PackFromVector4(green); |
|||
for (int y = topBand; y < topBand + height; y++) |
|||
{ |
|||
pixels[x, y] = c; |
|||
} |
|||
topBand = topBand + height; |
|||
c.PackFromVector4(blue); |
|||
for (int y = topBand; y < bottom; y++) |
|||
{ |
|||
pixels[x, y] = c; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Fills the bottom right quadrant with all the colors producable by converting itterating over a uint and unpacking it.
|
|||
/// A better algorithm could be used but it works
|
|||
/// </summary>
|
|||
/// <param name="pixels"></param>
|
|||
private static void Rainbow(PixelAccessor<TColor> pixels) |
|||
{ |
|||
int left = pixels.Width / 2; |
|||
int right = pixels.Width; |
|||
int top = pixels.Height / 2; |
|||
int bottom = pixels.Height; |
|||
|
|||
int pixelCount = left * top; |
|||
uint stepsPerPixel = (uint)(uint.MaxValue / pixelCount); |
|||
TColor c = default(TColor); |
|||
Color t = new Color(0); |
|||
|
|||
for (int x = left; x < right; x++) |
|||
for (int y = top; y < bottom; y++) |
|||
{ |
|||
t.PackedValue += stepsPerPixel; |
|||
Vector4 v = t.ToVector4(); |
|||
//v.W = (x - left) / (float)left;
|
|||
c.PackFromVector4(v); |
|||
pixels[x, y] = c; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
|
|||
namespace ImageSharp.Tests |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
public static class TestImageExtensions |
|||
{ |
|||
public static void DebugSave<TColor>(this Image<TColor> img, TestImageProvider<TColor> provider, string extension = "png") |
|||
where TColor : struct, IPixel<TColor> |
|||
{ |
|||
if(!bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCI) || !isCI) |
|||
{ |
|||
// we are running locally then we want to save it out
|
|||
provider.Utility.SaveTestOutputFile(img, extension); |
|||
} |
|||
} |
|||
} |
|||
} |
|||