diff --git a/src/ImageSharp/IO/EndianBitConverter.ToType.cs b/src/ImageSharp/IO/EndianBitConverter.ToType.cs
index ee14ef33e..0c0e49911 100644
--- a/src/ImageSharp/IO/EndianBitConverter.ToType.cs
+++ b/src/ImageSharp/IO/EndianBitConverter.ToType.cs
@@ -67,6 +67,18 @@ namespace SixLabors.ImageSharp.IO
return unchecked((ulong)this.ToInt64(value, startIndex));
}
+ ///
+ /// Returns a Boolean value converted from one byte at a specified position in a byte array.
+ ///
+ /// An array of bytes.
+ /// The starting position within value.
+ /// true if the byte at startIndex in value is nonzero; otherwise, false.
+ public bool ToBoolean(byte[] value, int startIndex)
+ {
+ CheckByteArgument(value, startIndex, 1);
+ return value[startIndex] != 0;
+ }
+
///
/// Returns a Unicode character converted from two bytes at a specified position in a byte array.
///
diff --git a/tests/ImageSharp.Tests/IO/BigEndianBitConverter.ToTypeTests.cs b/tests/ImageSharp.Tests/IO/BigEndianBitConverter.ToTypeTests.cs
index 67fb7e2cc..19ef24c79 100644
--- a/tests/ImageSharp.Tests/IO/BigEndianBitConverter.ToTypeTests.cs
+++ b/tests/ImageSharp.Tests/IO/BigEndianBitConverter.ToTypeTests.cs
@@ -15,6 +15,7 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact]
public void CopyToWithNullBufferThrowsException()
{
+ Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToBoolean(null, 0));
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToInt16(null, 0));
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToUInt16(null, 0));
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToInt32(null, 0));
@@ -26,6 +27,7 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact]
public void CopyToWithIndexTooBigThrowsException()
{
+ Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToBoolean(new byte[1], 1));
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToInt16(new byte[2], 1));
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToUInt16(new byte[2], 1));
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToInt32(new byte[4], 1));
@@ -37,6 +39,7 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact]
public void CopyToWithBufferTooSmallThrowsException()
{
+ Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToBoolean(new byte[0], 0));
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToInt16(new byte[1], 0));
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToUInt16(new byte[1], 0));
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToInt32(new byte[3], 0));
@@ -45,6 +48,21 @@ namespace SixLabors.ImageSharp.Tests.IO
Assert.Throws(() => EndianBitConverter.BigEndianConverter.ToUInt64(new byte[7], 0));
}
+ ///
+ /// Tests that passing a returns the correct bytes.
+ ///
+ [Fact]
+ public void ToBoolean()
+ {
+ Assert.False(EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 0 }, 0));
+ Assert.True(EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 1 }, 0));
+ Assert.True(EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 42 }, 0));
+
+ Assert.False(EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 1, 0 }, 1));
+ Assert.True(EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 0, 1 }, 1));
+ Assert.True(EndianBitConverter.BigEndianConverter.ToBoolean(new byte[] { 0, 42 }, 1));
+ }
+
///
/// Tests that passing a returns the correct bytes.
///
diff --git a/tests/ImageSharp.Tests/IO/LittleEndianBitConverter.ToTypeTests.cs b/tests/ImageSharp.Tests/IO/LittleEndianBitConverter.ToTypeTests.cs
index 4c890b367..0e09d1d07 100644
--- a/tests/ImageSharp.Tests/IO/LittleEndianBitConverter.ToTypeTests.cs
+++ b/tests/ImageSharp.Tests/IO/LittleEndianBitConverter.ToTypeTests.cs
@@ -15,6 +15,7 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact]
public void CopyToWithNullBufferThrowsException()
{
+ Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToBoolean(null, 0));
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToInt16(null, 0));
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToUInt16(null, 0));
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToInt32(null, 0));
@@ -26,6 +27,7 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact]
public void CopyToWithIndexTooBigThrowsException()
{
+ Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[1], 1));
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToInt16(new byte[2], 1));
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[2], 1));
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToInt32(new byte[4], 1));
@@ -37,6 +39,7 @@ namespace SixLabors.ImageSharp.Tests.IO
[Fact]
public void CopyToWithBufferTooSmallThrowsException()
{
+ Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[0], 0));
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToInt16(new byte[1], 0));
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToUInt16(new byte[1], 0));
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToInt32(new byte[3], 0));
@@ -45,6 +48,19 @@ namespace SixLabors.ImageSharp.Tests.IO
Assert.Throws(() => EndianBitConverter.LittleEndianConverter.ToUInt64(new byte[7], 0));
}
+ ///
+ /// Tests that passing a returns the correct bytes.
+ ///
+ [Fact]
+ public void ToBoolean()
+ {
+ Assert.False(EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[] { 0 }, 0));
+ Assert.True(EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[] { 1 }, 0));
+
+ Assert.False(EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[] { 1, 0 }, 1));
+ Assert.True(EndianBitConverter.LittleEndianConverter.ToBoolean(new byte[] { 0, 1 }, 1));
+ }
+
///
/// Tests that passing a returns the correct bytes.
///