using System;
namespace Unity.Cloud.UserReporting
{
///
/// Provides static methods for helping with PNG images.
///
public static class PngHelper
{
#region Static Methods
///
/// Gets a PNG image's height from base 64 encoded data.
///
/// The data.
/// The height.
public static int GetPngHeightFromBase64Data(string data)
{
// Preconditions
if (data == null || data.Length < 32)
{
return 0;
}
// Implementation
byte[] bytes = Convert.FromBase64String(data.Substring(0, 32));
byte[] heightBytes = PngHelper.Slice(bytes, 20, 24);
Array.Reverse(heightBytes);
int height = BitConverter.ToInt32(heightBytes, 0);
return height;
}
///
/// Gets a PNG image's width from base 64 encoded data.
///
/// The data.
/// The width.
public static int GetPngWidthFromBase64Data(string data)
{
// Preconditions
if (data == null || data.Length < 32)
{
return 0;
}
// Implementation
byte[] bytes = Convert.FromBase64String(data.Substring(0, 32));
byte[] widthBytes = PngHelper.Slice(bytes, 16, 20);
Array.Reverse(widthBytes);
int width = BitConverter.ToInt32(widthBytes, 0);
return width;
}
///
/// Slices a byte array.
///
/// The source.
/// The start.
/// The end.
/// The slices byte array.
private static byte[] Slice(byte[] source, int start, int end)
{
if (end < 0)
{
end = source.Length + end;
}
int len = end - start;
byte[] res = new byte[len];
for (int i = 0; i < len; i++)
{
res[i] = source[i + start];
}
return res;
}
#endregion
}
}