namespace Uwaa.PNG;
///
/// The high level information about the image.
///
public readonly struct ImageHeader
{
internal static readonly byte[] HeaderBytes = { 73, 72, 68, 82 };
internal static readonly byte[] ValidationHeader = { 137, 80, 78, 71, 13, 10, 26, 10 };
static readonly Dictionary> PermittedBitDepths = new Dictionary>
{
{ColorType.None, new HashSet {1, 2, 4, 8, 16}},
{ColorType.ColorUsed, new HashSet {8, 16}},
{ColorType.PaletteUsed | ColorType.ColorUsed, new HashSet {1, 2, 4, 8}},
{ColorType.AlphaChannelUsed, new HashSet {8, 16}},
{ColorType.AlphaChannelUsed | ColorType.ColorUsed, new HashSet {8, 16}},
};
///
/// The width of the image in pixels.
///
public int Width { get; }
///
/// The height of the image in pixels.
///
public int Height { get; }
///
/// The bit depth of the image.
///
public byte BitDepth { get; }
///
/// The color type of the image.
///
public ColorType ColorType { get; }
///
/// The interlace method used by the image..
///
public InterlaceMethod InterlaceMethod { get; }
///
/// Create a new .
///
public ImageHeader(int width, int height, byte bitDepth, ColorType colorType, InterlaceMethod interlaceMethod)
{
if (width == 0)
throw new ArgumentOutOfRangeException(nameof(width), "Invalid width (0) for image.");
if (height == 0)
throw new ArgumentOutOfRangeException(nameof(height), "Invalid height (0) for image.");
if (!PermittedBitDepths.TryGetValue(colorType, out var permitted) || !permitted.Contains(bitDepth))
throw new ArgumentException($"The bit depth {bitDepth} is not permitted for color type {colorType}.");
Width = width;
Height = height;
BitDepth = bitDepth;
ColorType = colorType;
InterlaceMethod = interlaceMethod;
}
public override string ToString()
{
return $"w: {Width}, h: {Height}, bitDepth: {BitDepth}, colorType: {ColorType}, interlace: {InterlaceMethod}.";
}
}