/// <summary> /// Create a new <see cref="ImageHeader"/>. /// </summary> public ImageHeader(int width, int height, byte bitDepth, ColorType colorType, CompressionMethod compressionMethod, FilterMethod filterMethod, 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; CompressionMethod = compressionMethod; FilterMethod = filterMethod; InterlaceMethod = interlaceMethod; }
/// <summary> /// Creates a new instance of PngHeader /// </summary> public PngHeader(int width, int height) { _width = width; _height = height; _bitdepth = BitDepth.Eight; _colortype = ColorType.TruecolorAlpha; _interlaceMethod = InterlaceMethod.NoInterlacing; }
public Ihdr(UInt32 width, UInt32 height, BitDepth bitDepth, ColorType colorType, CompressionMethod compressionMethod = CompressionMethod.Default, FilterMethod filterMethod = FilterMethod.Default, InterlaceMethod interlaceMethod = InterlaceMethod.None) : base(ChunkType.IHDR) { #region Sanity if(width == 0 || width > Int32.MaxValue) throw new ArgumentOutOfRangeException("width", "width must be greater than 0 and smaller than In32.MaxValue(2^31-1)"); if(height == 0 || height > Int32.MaxValue) throw new ArgumentOutOfRangeException("height", "height must be greater than 0 and smaller than In32.MaxValue(2^31-1)"); BitDepth[] allowedBitDepths; switch (colorType) { case ColorType.Grayscale: if(!(allowedBitDepths = new[] { BitDepth._1, BitDepth._2, BitDepth._4, BitDepth._8, BitDepth._16 }).Contains(bitDepth)) throw new ArgumentOutOfRangeException("bitDepth", String.Format("bitDepth must be one of {0} for colorType {1}", allowedBitDepths.Aggregate("", (s, bd) => s + bd + ", ", s => s.Trim().Substring(0, s.Length - 2)), colorType)); break; case ColorType.Rgb: if(!(allowedBitDepths = new[]{BitDepth._8, BitDepth._16}).Contains(bitDepth)) throw new ArgumentOutOfRangeException("bitDepth", String.Format("bitDepth must be one of {0} for colorType {1}", allowedBitDepths.Aggregate("", (s, bd) => s + bd + ", ", s => s.Trim().Substring(0, s.Length - 2)), colorType)); break; case ColorType.Palette: if(!(allowedBitDepths = new[] { BitDepth._1, BitDepth._2, BitDepth._4, BitDepth._8}).Contains(bitDepth)) throw new ArgumentOutOfRangeException("bitDepth", String.Format("bitDepth must be one of {0} for colorType {1}", allowedBitDepths.Aggregate("", (s, bd) => s + bd + ", ", s => s.Trim().Substring(0, s.Length - 2)), colorType)); break; case ColorType.GrayscaleWithAlpha: if(!(allowedBitDepths = new[] { BitDepth._8, BitDepth._16}).Contains(bitDepth)) throw new ArgumentOutOfRangeException("bitDepth", String.Format("bitDepth must be one of {0} for colorType {1}", allowedBitDepths.Aggregate("", (s, bd) => s + bd + ", ", s => s.Trim().Substring(0, s.Length - 2)), colorType)); break; case ColorType.Rgba: if(!(allowedBitDepths = new[] { BitDepth._8, BitDepth._16}).Contains(bitDepth)) throw new ArgumentOutOfRangeException("bitDepth", String.Format("bitDepth must be one of {0} for colorType {1}", allowedBitDepths.Aggregate("", (s, bd) => s + bd + ", ", s => s.Trim().Substring(0, s.Length - 2)), colorType)); break; default: throw new ArgumentOutOfRangeException("colorType", String.Format("Unknown colorType: {0}", colorType)); } if(compressionMethod != CompressionMethod.Default) throw new ArgumentOutOfRangeException("compressionMethod", String.Format("Unknown compressionMethod: {0}", compressionMethod)); if(filterMethod != FilterMethod.Default) throw new ArgumentOutOfRangeException("filterMethod", String.Format("Unknown filterMethod: {0}", filterMethod)); var allowedInterlaceMethods = new[] {InterlaceMethod.None, InterlaceMethod.Adam7}; if(!allowedInterlaceMethods.Contains(interlaceMethod)) throw new ArgumentOutOfRangeException("interlaceMethod", String.Format("interlaceMethod must be one of {0}", allowedInterlaceMethods.Aggregate("", (s, bd) => s + bd + ", ", s => s.Trim().Substring(0, s.Length - 2)))); #endregion Width = width; Height = height; BitDepth = bitDepth; ColorType = colorType; CompressionMethod = compressionMethod; FilterMethod = filterMethod; InterlaceMethod = interlaceMethod; }
/// <summary> /// Create a new <see cref="RawPngData"/>. /// </summary> /// <param name="data">The decoded pixel data as bytes.</param> /// <param name="bytesPerPixel">The number of bytes in each pixel.</param> /// <param name="width">The width of the image in pixels.</param> /// <param name="interlaceMethod">The interlace method used.</param> /// <param name="palette">The palette for images using indexed colors.</param> /// <param name="colorType">The color type.</param> public RawPngData(byte[] data, int bytesPerPixel, int width, InterlaceMethod interlaceMethod, Palette palette, ColorType colorType) { if (width < 0) { throw new ArgumentOutOfRangeException($"Width must be greater than or equal to 0, got {width}."); } this.data = data ?? throw new ArgumentNullException(nameof(data)); this.bytesPerPixel = bytesPerPixel; this.width = width; this.palette = palette; this.colorType = colorType; rowOffset = interlaceMethod == InterlaceMethod.Adam7 ? 0 : 1; }
public override void Load(byte [] data) { Width = BitConverter.ToUInt32 (data, 0, false); Height = BitConverter.ToUInt32 (data, 4, false); Depth = data [8]; Color = (ColorType) data [9]; //if (Color != ColorType.Rgb) // throw new System.Exception (System.String.Format ("unsupported {0}", Color)); this.Compression = (CompressionMethod) data [10]; if (this.Compression != CompressionMethod.Zlib) throw new System.Exception (System.String.Format ("unsupported {0}", Compression)); Filter = (FilterMethod) data [11]; if (Filter != FilterMethod.Adaptive) throw new System.Exception (System.String.Format ("unsupported {0}", Filter)); Interlace = (InterlaceMethod) data [12]; //if (Interlace != InterlaceMethod.None) // throw new System.Exception (System.String.Format ("unsupported {0}", Interlace)); }