71 lines
2.3 KiB
C#
71 lines
2.3 KiB
C#
|
namespace Uwaa.PNG;
|
||
|
|
||
|
/// <summary>
|
||
|
/// The high level information about the image.
|
||
|
/// </summary>
|
||
|
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<ColorType, HashSet<byte>> PermittedBitDepths = new Dictionary<ColorType, HashSet<byte>>
|
||
|
{
|
||
|
{ColorType.None, new HashSet<byte> {1, 2, 4, 8, 16}},
|
||
|
{ColorType.ColorUsed, new HashSet<byte> {8, 16}},
|
||
|
{ColorType.PaletteUsed | ColorType.ColorUsed, new HashSet<byte> {1, 2, 4, 8}},
|
||
|
{ColorType.AlphaChannelUsed, new HashSet<byte> {8, 16}},
|
||
|
{ColorType.AlphaChannelUsed | ColorType.ColorUsed, new HashSet<byte> {8, 16}},
|
||
|
};
|
||
|
|
||
|
/// <summary>
|
||
|
/// The width of the image in pixels.
|
||
|
/// </summary>
|
||
|
public int Width { get; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// The height of the image in pixels.
|
||
|
/// </summary>
|
||
|
public int Height { get; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// The bit depth of the image.
|
||
|
/// </summary>
|
||
|
public byte BitDepth { get; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// The color type of the image.
|
||
|
/// </summary>
|
||
|
public ColorType ColorType { get; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// The interlace method used by the image..
|
||
|
/// </summary>
|
||
|
public InterlaceMethod InterlaceMethod { get; }
|
||
|
|
||
|
/// <summary>
|
||
|
/// Create a new <see cref="ImageHeader"/>.
|
||
|
/// </summary>
|
||
|
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}.";
|
||
|
}
|
||
|
}
|