Exemple #1
0
 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;
     this.CurrentChunkGroup = -1;
     this.ShouldCloseStream = true;
     this.MaxBytesMetadata = 5 * 1024 * 1024;
     this.MaxTotalBytesRead = 200 * 1024 * 1024;
     this.SkipChunkMaxSize = 2 * 1024 * 1024;
     this.SkipChunkIds = new string [] { "fdAT" };
     this.ChunkLoadBehaviour = Hjg.Pngcs.Chunks.ChunkLoadBehaviour.LOAD_CHUNK_ALWAYS;
     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;
     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 );
     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;
     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 );
 }
Exemple #2
0
        /// <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);
            }
        }