📷 A modern, cross-platform, 2D Graphics library for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

51 lines
1.7 KiB

namespace ImageProcessor
{
using System;
internal static class ByteExtensions
{
/// <summary>
/// Converts a byte array to a new array where each value in the original array is represented
/// by a the specified number of bits.
/// </summary>
/// <param name="bytes">The bytes to convert from. Cannot be null.</param>
/// <param name="bits">The number of bits per value.</param>
/// <returns>The resulting <see cref="T:byte[]"/> array. Is never null.</returns>
/// <exception cref="ArgumentNullException"><paramref name="bytes"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="bits"/> is less than or equals than zero.</exception>
public static byte[] ToArrayByBitsLength(this byte[] bytes, int bits)
{
Guard.NotNull(bytes, "bytes");
Guard.GreaterThan(bits, 0, "bits");
byte[] result;
if (bits < 8)
{
result = new byte[bytes.Length * 8 / bits];
int factor = (int)Math.Pow(2, bits) - 1;
int mask = 0xFF >> (8 - bits);
int resultOffset = 0;
foreach (byte b in bytes)
{
for (int shift = 0; shift < 8; shift += bits)
{
int colorIndex = ((b >> (8 - bits - shift)) & mask) * (255 / factor);
result[resultOffset] = (byte)colorIndex;
resultOffset++;
}
}
}
else
{
result = bytes;
}
return result;
}
}
}