Beispiel #1
0
 internal static byte[] CompressBytes(byte[] ori, int offset, int len, bool compress)
 {
     try
     {
         MemoryStream inb = new MemoryStream(ori, offset, len);
         Stream       inx = inb;
         if (!compress)
         {
             inx = ZlibStreamFactory.CreateZlibInputStream(inb);
         }
         MemoryStream outb = new MemoryStream();
         Stream       outx = outb;
         if (compress)
         {
             outx = ZlibStreamFactory.CreateZlibOutputStream(outb);
         }
         ShovelInToOut(inx, outx);
         inx.Close();
         outx.Close();
         byte[] res = outb.ToArray();
         return(res);
     }
     catch (Exception e)
     {
         throw new PngjException(e);
     }
 }
Beispiel #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>
        ///
        private 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, Hjg.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, Hjg.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();
            }
        }