예제 #1
0
        private readonly bool ReadOnly;// readonly

        internal PngMetadata(ChunksList chunks)
        {
            chunkList = chunks;
            if (chunks is ChunksListForWrite)
            {
                ReadOnly = false;
            }
            else
            {
                ReadOnly = true;
            }
        }
예제 #2
0
        public string GetHeader(string icon)
        {
            string ztxt = string.Empty;

            PngReader pngr = FileHelper.CreatePngReader(FileName);

            width  = pngr.ImgInfo.Cols;
            height = pngr.ImgInfo.Rows;
            ChunksList clist = pngr.GetChunksList();

            /*The File should only have one zTxt, this chunk stores our DMI information such as
             * sprite width, height, iconstates, directionals and frames among other things.
             */
            foreach (PngChunkZTXT desc in clist.GetById("zTXt"))
            {
                ztxt = desc.GetVal();
                break;
            }
            pngr.ShouldCloseStream = true;
            pngr.End();

            return(ztxt);
        }
예제 #3
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);
            }
        }
예제 #4
0
        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());
            }
        }