namespace Uwaa.PNG;
///
/// A PNG image. Call to open from file or bytes.
///
public class Png
{
readonly RawPngData data;
readonly bool hasTransparencyChunk;
///
/// The header data from the PNG image.
///
public ImageHeader Header { get; }
///
/// The width of the image in pixels.
///
public int Width => Header.Width;
///
/// The height of the image in pixels.
///
public int Height => Header.Height;
///
/// Whether the image has an alpha (transparency) layer.
///
public bool HasAlphaChannel => (Header.ColorType & ColorType.AlphaChannelUsed) != 0 || hasTransparencyChunk;
internal Png(ImageHeader header, RawPngData data, bool hasTransparencyChunk)
{
Header = header;
this.data = data ?? throw new ArgumentNullException(nameof(data));
this.hasTransparencyChunk = hasTransparencyChunk;
}
///
/// Get the pixel at the given column and row (x, y).
///
///
/// Pixel values are generated on demand from the underlying data to prevent holding many items in memory at once, so consumers
/// should cache values if they're going to be looped over many time.
///
/// The x coordinate (column).
/// The y coordinate (row).
/// The pixel at the coordinate.
public Pixel GetPixel(int x, int y) => data.GetPixel(x, y);
///
/// Read the PNG image from the stream.
///
/// The stream containing PNG data to be read.
/// The data from the stream.
public static Png Open(Stream stream)
=> PngOpener.Open(stream);
///
/// Read the PNG image from the bytes.
///
/// The bytes of the PNG data to be read.
/// The data from the bytes.
public static Png Open(byte[] bytes)
{
using var memoryStream = new MemoryStream(bytes);
return PngOpener.Open(memoryStream);
}
///
/// Read the PNG from the file path.
///
/// The path to the PNG file to open.
/// This will open the file to obtain a so will lock the file during reading.
/// The data from the file.
public static Png Open(string path)
{
using var fileStream = File.OpenRead(path);
return Open(fileStream);
}
}