/// <summary> /// Write id signature and also "IHDR" chunk /// </summary> /// private void WriteSignatureAndIHDR() { CurrentChunkGroup = ChunksList.CHUNK_GROUP_0_IDHR; PngHelperInternal.WriteBytes(outputStream, Hjg.Pngcs.PngHelperInternal.PNG_ID_SIGNATURE); // signature PngChunkIHDR ihdr = new PngChunkIHDR(ImgInfo); // http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html ihdr.Cols = ImgInfo.Cols; ihdr.Rows = ImgInfo.Rows; ihdr.Bitspc = ImgInfo.BitDepth; int colormodel = 0; if (ImgInfo.Alpha) { colormodel += 0x04; } if (ImgInfo.Indexed) { colormodel += 0x01; } if (!ImgInfo.Greyscale) { colormodel += 0x02; } ihdr.Colormodel = colormodel; ihdr.Compmeth = 0; // compression method 0=deflate ihdr.Filmeth = 0; // filter method (0) ihdr.Interlaced = 0; // never interlace ihdr.CreateRawChunk().WriteChunk(outputStream); }
public void WriteSignatureAndIHDR() { CurrentChunkGroup = 0; PngHelperInternal.WriteBytes(outputStream, PngHelperInternal.PNG_ID_SIGNATURE); PngChunkIHDR obj = new PngChunkIHDR(ImgInfo) { Cols = ImgInfo.Cols, Rows = ImgInfo.Rows, Bitspc = ImgInfo.BitDepth }; int num = 0; if (ImgInfo.Alpha) { num += 4; } if (ImgInfo.Indexed) { num++; } if (!ImgInfo.Greyscale) { num += 2; } obj.Colormodel = num; obj.Compmeth = 0; obj.Filmeth = 0; obj.Interlaced = 0; obj.CreateRawChunk().WriteChunk(outputStream); }
private void WriteSignatureAndIHDR() { CurrentChunkGroup = ChunksList.CHUNK_GROUP_0_IDHR; PngHelperInternal.WriteBytes ( outputStream, Hjg.Pngcs.PngHelperInternal.PNG_ID_SIGNATURE ); // signature PngChunkIHDR ihdr = new PngChunkIHDR ( ImgInfo ); ihdr.Cols = ImgInfo.Cols; ihdr.Rows = ImgInfo.Rows; ihdr.Bitspc = ImgInfo.BitDepth; int colormodel = 0; if ( ImgInfo.Alpha ) colormodel += 0x04; if ( ImgInfo.Indexed ) colormodel += 0x01; if ( !ImgInfo.Greyscale ) colormodel += 0x02; ihdr.Colormodel = colormodel; ihdr.Compmeth = 0; ihdr.Filmeth = 0; ihdr.Interlaced = 0; ihdr.CreateRawChunk ().WriteChunk ( outputStream ); }
/// <summary> /// Constructs a PNGReader objet from a opened Stream /// </summary> /// <remarks>The constructor reads the signature and first chunk (IDHR)<seealso cref="FileHelper.CreatePngReader(string)"/> /// </remarks> /// /// <param name="inputStream"></param> /// <param name="filename">Optional, can be the filename or a description.</param> public PngReader(Stream inputStream, String filename) { this.filename = (filename == null) ? "" : filename; this.inputStream = inputStream; this.chunksList = new ChunksList(null); this.metadata = new PngMetadata(chunksList); this.offset = 0; // set default options this.CurrentChunkGroup = -1; this.ShouldCloseStream = true; this.MaxBytesMetadata = 5 * 1024 * 1024; this.MaxTotalBytesRead = 200 * 1024 * 1024; // 200MB this.SkipChunkMaxSize = 2 * 1024 * 1024; this.SkipChunkIds = new string[] { "fdAT" }; this.ChunkLoadBehaviour = Hjg.Pngcs.Chunks.ChunkLoadBehaviour.LOAD_CHUNK_ALWAYS; // starts reading: signature byte[] pngid = new byte[8]; PngHelperInternal.ReadBytes(inputStream, pngid, 0, pngid.Length); offset += pngid.Length; if (!PngCsUtils.arraysEqual(pngid, PngHelperInternal.PNG_ID_SIGNATURE)) { throw new PngjInputException("Bad PNG signature"); } CurrentChunkGroup = ChunksList.CHUNK_GROUP_0_IDHR; // reads first chunk IDHR int clen = PngHelperInternal.ReadInt4(inputStream); offset += 4; if (clen != 13) { throw new Exception("IDHR chunk len != 13 ?? " + clen); } byte[] chunkid = new byte[4]; PngHelperInternal.ReadBytes(inputStream, chunkid, 0, 4); if (!PngCsUtils.arraysEqual4(chunkid, ChunkHelper.b_IHDR)) { throw new PngjInputException("IHDR not found as first chunk??? [" + ChunkHelper.ToString(chunkid) + "]"); } offset += 4; PngChunkIHDR ihdr = (PngChunkIHDR)ReadChunk(chunkid, clen, false); bool alpha = (ihdr.Colormodel & 0x04) != 0; bool palette = (ihdr.Colormodel & 0x01) != 0; bool grayscale = (ihdr.Colormodel == 0 || ihdr.Colormodel == 4); // creates ImgInfo and imgLine, and allocates buffers ImgInfo = new ImageInfo(ihdr.Cols, ihdr.Rows, ihdr.Bitspc, alpha, grayscale, palette); rowb = new byte[ImgInfo.BytesPerRow + 1]; rowbprev = new byte[rowb.Length]; rowbfilter = new byte[rowb.Length]; interlaced = ihdr.Interlaced == 1; deinterlacer = interlaced ? new PngDeinterlacer(ImgInfo) : null; // some checks if (ihdr.Filmeth != 0 || ihdr.Compmeth != 0 || (ihdr.Interlaced & 0xFFFE) != 0) { throw new PngjInputException("compmethod or filtermethod or interlaced unrecognized"); } if (ihdr.Colormodel < 0 || ihdr.Colormodel > 6 || ihdr.Colormodel == 1 || ihdr.Colormodel == 5) { throw new PngjInputException("Invalid colormodel " + ihdr.Colormodel); } if (ihdr.Bitspc != 1 && ihdr.Bitspc != 2 && ihdr.Bitspc != 4 && ihdr.Bitspc != 8 && ihdr.Bitspc != 16) { throw new PngjInputException("Invalid bit depth " + ihdr.Bitspc); } }
/// <summary> /// Write id signature and also "IHDR" chunk /// </summary> /// private void WriteSignatureAndIHDR() { CurrentChunkGroup = ChunksList.CHUNK_GROUP_0_IDHR; PngHelperInternal.WriteBytes(outputStream, Hjg.Pngcs.PngHelperInternal.PNG_ID_SIGNATURE); // signature PngChunkIHDR ihdr = new PngChunkIHDR(ImgInfo); // http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html ihdr.Cols = ImgInfo.Cols; ihdr.Rows = ImgInfo.Rows; ihdr.Bitspc = ImgInfo.BitDepth; int colormodel = 0; if (ImgInfo.Alpha) colormodel += 0x04; if (ImgInfo.Indexed) colormodel += 0x01; if (!ImgInfo.Greyscale) colormodel += 0x02; ihdr.Colormodel = colormodel; ihdr.Compmeth = 0; // compression method 0=deflate ihdr.Filmeth = 0; // filter method (0) ihdr.Interlaced = 0; // never interlace ihdr.CreateRawChunk().WriteChunk(outputStream); }
public PngReader(Stream inputStream, string filename) { this.filename = ((filename == null) ? "" : filename); this.inputStream = inputStream; chunksList = new ChunksList(null); metadata = new PngMetadata(chunksList); offset = 0L; CurrentChunkGroup = -1; ShouldCloseStream = true; MaxBytesMetadata = 5242880L; MaxTotalBytesRead = 209715200L; SkipChunkMaxSize = 2097152; SkipChunkIds = new string[1] { "fdAT" }; ChunkLoadBehaviour = ChunkLoadBehaviour.LOAD_CHUNK_ALWAYS; byte[] array = new byte[8]; PngHelperInternal.ReadBytes(inputStream, array, 0, array.Length); offset += array.Length; if (!PngCsUtils.arraysEqual(array, PngHelperInternal.PNG_ID_SIGNATURE)) { throw new PngjInputException("Bad PNG signature"); } CurrentChunkGroup = 0; int num = PngHelperInternal.ReadInt4(inputStream); offset += 4L; if (num != 13) { throw new Exception("IDHR chunk len != 13 ?? " + num.ToString()); } byte[] array2 = new byte[4]; PngHelperInternal.ReadBytes(inputStream, array2, 0, 4); if (!PngCsUtils.arraysEqual4(array2, ChunkHelper.b_IHDR)) { throw new PngjInputException("IHDR not found as first chunk??? [" + ChunkHelper.ToString(array2) + "]"); } offset += 4L; PngChunkIHDR pngChunkIHDR = (PngChunkIHDR)ReadChunk(array2, num, skipforced: false); bool alpha = (pngChunkIHDR.Colormodel & 4) != 0; bool palette = (pngChunkIHDR.Colormodel & 1) != 0; bool grayscale = pngChunkIHDR.Colormodel == 0 || pngChunkIHDR.Colormodel == 4; ImgInfo = new ImageInfo(pngChunkIHDR.Cols, pngChunkIHDR.Rows, pngChunkIHDR.Bitspc, alpha, grayscale, palette); rowb = new byte[ImgInfo.BytesPerRow + 1]; rowbprev = new byte[rowb.Length]; rowbfilter = new byte[rowb.Length]; interlaced = (pngChunkIHDR.Interlaced == 1); deinterlacer = (interlaced ? new PngDeinterlacer(ImgInfo) : null); if (pngChunkIHDR.Filmeth != 0 || pngChunkIHDR.Compmeth != 0 || (pngChunkIHDR.Interlaced & 0xFFFE) != 0) { throw new PngjInputException("compmethod or filtermethod or interlaced unrecognized"); } if (pngChunkIHDR.Colormodel < 0 || pngChunkIHDR.Colormodel > 6 || pngChunkIHDR.Colormodel == 1 || pngChunkIHDR.Colormodel == 5) { throw new PngjInputException("Invalid colormodel " + pngChunkIHDR.Colormodel.ToString()); } if (pngChunkIHDR.Bitspc != 1 && pngChunkIHDR.Bitspc != 2 && pngChunkIHDR.Bitspc != 4 && pngChunkIHDR.Bitspc != 8 && pngChunkIHDR.Bitspc != 16) { throw new PngjInputException("Invalid bit depth " + pngChunkIHDR.Bitspc.ToString()); } }