Ejemplo n.º 1
0
        /// <summary>
        /// Reads chunks before first IDAT. Position before: after IDHR (crc included)
        /// Position after: just after the first IDAT chunk id Returns length of first
        /// IDAT chunk , -1 if not found
        /// </summary>
        ///
        private void ReadFirstChunks()
        {
            if (!FirstChunksNotYetRead())
            {
                return;
            }

            var clen    = 0;
            var found   = false;
            var chunkid = new byte[4]; // it's important to reallocate in each

            CurrentChunkGroup = ChunksList.CHUNK_GROUP_1_AFTERIDHR;
            while (!found)
            {
                clen    = PngHelperInternal.ReadInt4(inputStream);
                offset += 4;
                if (clen < 0)
                {
                    break;
                }

                PngHelperInternal.ReadBytes(inputStream, chunkid, 0, 4);
                offset += 4;
                if (PngCsUtils.arraysEqual4(chunkid, Hjg.Pngcs.Chunks.ChunkHelper.b_IDAT))
                {
                    found             = true;
                    CurrentChunkGroup = ChunksList.CHUNK_GROUP_4_IDAT;
                    // add dummy idat chunk to list
                    chunksList.AppendReadChunk(new PngChunkIDAT(ImgInfo, clen, offset - 8), CurrentChunkGroup);
                    break;
                }
                else if (PngCsUtils.arraysEqual4(chunkid, Hjg.Pngcs.Chunks.ChunkHelper.b_IEND))
                {
                    throw new PngjInputException("END chunk found before image data (IDAT) at offset=" + offset);
                }
                var chunkids = ChunkHelper.ToString(chunkid);
                if (chunkids.Equals(ChunkHelper.PLTE, System.StringComparison.Ordinal))
                {
                    CurrentChunkGroup = ChunksList.CHUNK_GROUP_2_PLTE;
                }

                ReadChunk(chunkid, clen, false);
                if (chunkids.Equals(ChunkHelper.PLTE, System.StringComparison.Ordinal))
                {
                    CurrentChunkGroup = ChunksList.CHUNK_GROUP_3_AFTERPLTE;
                }
            }

            var idatLen = found ? clen : -1;

            if (idatLen < 0)
            {
                throw new PngjInputException("first idat chunk not found!");
            }

            iIdatCstream = new PngIDatChunkInputStream(inputStream, idatLen, offset);
            idatIstream  = new ZlibInputStream(iIdatCstream, true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads chunks before first IDAT. Position before: after IDHR (crc included)
        /// Position after: just after the first IDAT chunk id Returns length of first
        /// IDAT chunk , -1 if not found
        /// </summary>
        ///
        void ReadFirstChunks()
        {
            if (!FirstChunksNotYetRead())
            {
                return;
            }
            int  clen  = 0;
            bool found = false;

            byte[] chunkid = new byte[4]; // it's important to reallocate in each
            this.CurrentChunkGroup = ChunksList.CHUNK_GROUP_1_AFTERIDHR;
            while (!found)
            {
                clen    = PngHelperInternal.ReadInt4(inputStream);
                offset += 4;
                if (clen < 0)
                {
                    break;
                }
                PngHelperInternal.ReadBytes(inputStream, chunkid, 0, 4);
                offset += 4;
                if (PngCsUtils.arraysEqual4(chunkid, Pngcs.Chunks.ChunkHelper.b_IDAT))
                {
                    found = true;
                    this.CurrentChunkGroup = ChunksList.CHUNK_GROUP_4_IDAT;
                    // add dummy idat chunk to list
                    chunksList.AppendReadChunk(new PngChunkIDAT(ImgInfo, clen, offset - 8), CurrentChunkGroup);
                    break;
                }
                else if (PngCsUtils.arraysEqual4(chunkid, Pngcs.Chunks.ChunkHelper.b_IEND))
                {
                    throw new PngjInputException("END chunk found before image data (IDAT) at offset=" + offset);
                }
                string chunkids = ChunkHelper.ToString(chunkid);
                if (chunkids.Equals(ChunkHelper.PLTE))
                {
                    this.CurrentChunkGroup = ChunksList.CHUNK_GROUP_2_PLTE;
                }
                ReadChunk(chunkid, clen, false);
                if (chunkids.Equals(ChunkHelper.PLTE))
                {
                    this.CurrentChunkGroup = ChunksList.CHUNK_GROUP_3_AFTERPLTE;
                }
            }
            int idatLen = found ? clen : -1;

            if (idatLen < 0)
            {
                throw new PngjInputException("first idat chunk not found!");
            }
            iIdatCstream = new PngIDatChunkInputStream(inputStream, idatLen, offset);
            idatIstream  = ZlibStreamFactory.createZlibInputStream(iIdatCstream, true);
            if (!crcEnabled)
            {
                iIdatCstream.DisableCrcCheck();
            }
        }
Ejemplo n.º 3
0
        public void ReadFirstChunks()
        {
            if (!FirstChunksNotYetRead())
            {
                return;
            }
            int  num  = 0;
            bool flag = false;

            byte[] array = new byte[4];
            CurrentChunkGroup = 1;
            while (!flag)
            {
                num     = PngHelperInternal.ReadInt4(inputStream);
                offset += 4L;
                if (num < 0)
                {
                    break;
                }
                PngHelperInternal.ReadBytes(inputStream, array, 0, 4);
                offset += 4L;
                if (PngCsUtils.arraysEqual4(array, ChunkHelper.b_IDAT))
                {
                    flag = true;
                    CurrentChunkGroup = 4;
                    chunksList.AppendReadChunk(new PngChunkIDAT(ImgInfo, num, offset - 8), CurrentChunkGroup);
                    break;
                }
                if (PngCsUtils.arraysEqual4(array, ChunkHelper.b_IEND))
                {
                    throw new PngjInputException("END chunk found before image data (IDAT) at offset=" + offset.ToString());
                }
                string text = ChunkHelper.ToString(array);
                if (text.Equals("PLTE"))
                {
                    CurrentChunkGroup = 2;
                }
                ReadChunk(array, num, skipforced: false);
                if (text.Equals("PLTE"))
                {
                    CurrentChunkGroup = 3;
                }
            }
            int num2 = flag ? num : (-1);

            if (num2 < 0)
            {
                throw new PngjInputException("first idat chunk not found!");
            }
            iIdatCstream = new PngIDatChunkInputStream(inputStream, num2, offset);
            idatIstream  = ZlibStreamFactory.createZlibInputStream(iIdatCstream, leaveOpen: true);
            if (!crcEnabled)
            {
                iIdatCstream.DisableCrcCheck();
            }
        }