/// <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; }
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)); }