Beispiel #1
0
        private void Handle_IDAT(PNGChunk chunk)
        {
            IDATChunk idatC = new IDATChunk();

            idatC.ChunkData = chunk.ChunkData;
            IDATList.Add(idatC);
        }
Beispiel #2
0
        protected PNGChunk GetNextChunk(Stream stream)
        {
            PNGChunk value = new PNGChunk();

            byte[] size = new byte[sizeof(uint)];
            stream.Read(size, 0, sizeof(uint));
            uint readLength = PNGUtils.ParseUint(size);

            byte[] type = new byte[4];
            stream.Read(type, 0, 4);
            value.ChunkType = PNGUtils.ParseString(type, 4);

            byte[] data = new byte[readLength];
            stream.Read(data, 0, (int)readLength);
            value.ChunkData = data;

            byte[] crc = new byte[sizeof(uint)];
            stream.Read(crc, 0, sizeof(uint));
            uint readCRC = PNGUtils.ParseUint(crc);

            uint calcCRC = value.CalculateCRC();

            if (readCRC != calcCRC)
            {
                throw new ApplicationException($"PNG Chunk CRC Mismatch.  Chunk CRC = {readCRC}, Calculated CRC = {calcCRC}.");
            }
            return(value);
        }
Beispiel #3
0
        private void Handle_iTXt(PNGChunk chunk)
        {
            iTXtChunk it = new iTXtChunk();

            it.ChunkData = chunk.ChunkData;
            iTXtList.Add(it);
        }
Beispiel #4
0
        private void Handle_tEXt(PNGChunk chunk)
        {
            tEXtChunk txt = new tEXtChunk();

            txt.ChunkData = chunk.ChunkData;
            tEXtList.Add(txt);
        }
Beispiel #5
0
        private void Handle_tRNS(PNGChunk chunk)
        {
            if (tRNS != null)
            {
                throw new ApplicationException("tRNS chunk encountered more than once");
            }
            switch (IHDR.ColorType)
            {
            case 0:
                tRNS = new tRNSChunkType0();
                break;

            case 2:
                tRNS = new tRNSChunkType2();
                break;

            case 3:
                tRNS = new tRNSChunkType3();
                break;

            case 4:
            case 6:
                throw new ApplicationException("tRNS chunk encountered, Colour type does not support");

            default:
                throw new ApplicationException("Colour type is not supported");
            }
            tRNS.ChunkData = chunk.ChunkData;
        }
Beispiel #6
0
        private void Handle_sBIT(PNGChunk chunk)
        {
            if (sBIT != null)
            {
                throw new ApplicationException("sBIT chunk encountered more than once");
            }
            switch (IHDR.ColorType)
            {
            case 0:
                sBIT = new sBITChunkType0();
                break;

            case 2:
                sBIT = new sBITChunkType2();
                break;

            case 3:
                sBIT = new sBITChunkType3();
                break;

            case 4:
                sBIT = new sBITChunkType4();
                break;

            case 6:
                sBIT = new sBITChunkType6();
                break;

            default:
                throw new ApplicationException("Colour type is not supported");
            }
            sBIT.ChunkData = chunk.ChunkData;
        }
Beispiel #7
0
        public void Load(Stream stream)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            Console.WriteLine("\nReading file...");
            byte[] sig = new byte[PNGSignature.Signature.Length];
            stream.Read(sig, 0, PNGSignature.Signature.Length);
            PNGSignature.Compare(sig);

            PNGChunk chunk = GetNextChunk(stream);

            Console.WriteLine($"Reading {chunk.ChunkType} chunk... ({chunk.ChunkLength} bytes)");
            if (chunk.ChunkType != IHDRChunk.NAME)
            {
                throw new ApplicationException("First chunk is not IHDR chunk");
            }
            Handle_IHDR(chunk);

            do
            {
                chunk = GetNextChunk(stream);
                Console.WriteLine($"Reading {chunk.ChunkType} chunk... ({chunk.ChunkLength} bytes)");
                if (!HandleChunk(chunk))
                {
                    HandleDefaultChunk(chunk);
                }
            } while (chunk.ChunkType != IENDChunk.NAME);
            Validate();
            stopWatch.Stop();
            string elapsedTime = stopWatch.Elapsed.FormatTime();

            Console.WriteLine($"Total read time = {elapsedTime}");
        }
Beispiel #8
0
        private void Handle_bKGD(PNGChunk chunk)
        {
            if (bKGD != null)
            {
                throw new ApplicationException("bKGD chunk encountered more than once");
            }
            switch (IHDR.ColorType)
            {
            case 0:
                bKGD = new bKGDChunkType0();
                break;

            case 2:
                bKGD = new bKGDChunkType2();
                break;

            case 3:
                bKGD = new bKGDChunkType3();
                break;

            case 4:
                bKGD = new bKGDChunkType4();
                break;

            case 6:
                bKGD = new bKGDChunkType6();
                break;

            default:
                throw new ApplicationException("Colour type is not supported");
            }
            bKGD.ChunkData = chunk.ChunkData;
        }
Beispiel #9
0
        private void Handle_zTXt(PNGChunk chunk)
        {
            zTXtChunk zt = new zTXtChunk();

            zt.ChunkData = chunk.ChunkData;
            zTXtList.Add(zt);
        }
Beispiel #10
0
 private void Handle_PLTE(PNGChunk chunk)
 {
     if (PLTE != null)
     {
         throw new ApplicationException("PLTE chunk encountered more than once");
     }
     PLTE           = new PLTEChunk();
     PLTE.ChunkData = chunk.ChunkData;
 }
Beispiel #11
0
 private void Handle_cHRM(PNGChunk chunk)
 {
     if (cHRM != null)
     {
         throw new ApplicationException("cHRM chunk encountered more than once");
     }
     cHRM           = new cHRMChunk();
     cHRM.ChunkData = chunk.ChunkData;
 }
Beispiel #12
0
 private void Handle_gAMA(PNGChunk chunk)
 {
     if (gAMA != null)
     {
         throw new ApplicationException("gAMA chunk encountered more than once");
     }
     gAMA           = new gAMAChunk();
     gAMA.ChunkData = chunk.ChunkData;
 }
Beispiel #13
0
 private void Handle_iCCP(PNGChunk chunk)
 {
     if (iCCP != null)
     {
         throw new ApplicationException("iCCP chunk encountered more than once");
     }
     iCCP           = new iCCPChunk();
     iCCP.ChunkData = chunk.ChunkData;
 }
Beispiel #14
0
 private void Handle_sRGB(PNGChunk chunk)
 {
     if (sRGB != null)
     {
         throw new ApplicationException("sRGB chunk encountered more than once");
     }
     sRGB           = new sRGBChunk();
     sRGB.ChunkData = chunk.ChunkData;
 }
Beispiel #15
0
 private void Handle_IHDR(PNGChunk chunk)
 {
     if (IHDR != null)
     {
         throw new ApplicationException("IHDR defined more than once");
     }
     IHDR           = new IHDRChunk();
     IHDR.ChunkData = chunk.ChunkData;
 }
Beispiel #16
0
 private void Handle_hIST(PNGChunk chunk)
 {
     if (hIST != null)
     {
         throw new ApplicationException("hIST chunk encountered more than once");
     }
     hIST           = new hISTChunk();
     hIST.ChunkData = chunk.ChunkData;
 }
Beispiel #17
0
 private void Handle_pHYs(PNGChunk chunk)
 {
     if (pHYs != null)
     {
         throw new ApplicationException("pHYs chunk encountered more than once");
     }
     pHYs           = new pHYsChunk();
     pHYs.ChunkData = chunk.ChunkData;
 }
Beispiel #18
0
 private void Handle_sPLT(PNGChunk chunk)
 {
     if (sPLT != null)
     {
         throw new ApplicationException("sPLT chunk encountered more than once");
     }
     sPLT           = new sPLTChunk();
     sPLT.ChunkData = chunk.ChunkData;
 }
Beispiel #19
0
 private void Handle_tIME(PNGChunk chunk)
 {
     if (tIME != null)
     {
         throw new ApplicationException("tIME chunk encountered more than once");
     }
     tIME           = new tIMEChunk();
     tIME.ChunkData = chunk.ChunkData;
 }
Beispiel #20
0
 protected static void WriteChunk(Stream stream, PNGChunk chunk)
 {
     if (chunk != null)
     {
         Console.WriteLine($"Writing {chunk.ChunkType} chunk... ({chunk.ChunkLength} bytes)");
         byte[] chArray = chunk.Chunk;
         stream.Write(chArray, 0, chArray.Length);
     }
 }
Beispiel #21
0
 private void Handle_IEND(PNGChunk chunk)
 {
     if (IEND != null)
     {
         throw new ApplicationException("IEND defined more than once");
     }
     IEND           = new IENDChunk();
     IEND.ChunkData = chunk.ChunkData;
 }
Beispiel #22
0
 private void HandleDefaultChunk(PNGChunk chunk)
 {
     chunks.Add(chunk);
 }
Beispiel #23
0
        protected virtual bool HandleChunk(PNGChunk chunk)
        {
            switch (chunk.ChunkType)
            {
            case IHDRChunk.NAME:
                Handle_IHDR(chunk);
                break;

            case PLTEChunk.NAME:
                Handle_PLTE(chunk);
                break;

            case IDATChunk.NAME:
                Handle_IDAT(chunk);
                break;

            case IENDChunk.NAME:
                Handle_IEND(chunk);
                break;

            case tRNSChunk.NAME:
                Handle_tRNS(chunk);
                break;

            case cHRMChunk.NAME:
                Handle_cHRM(chunk);
                break;

            case gAMAChunk.NAME:
                Handle_gAMA(chunk);
                break;

            case iCCPChunk.NAME:
                Handle_iCCP(chunk);
                break;

            case sBITChunk.NAME:
                Handle_sBIT(chunk);
                break;

            case sRGBChunk.NAME:
                Handle_sRGB(chunk);
                break;

            case tEXtChunk.NAME:
                Handle_tEXt(chunk);
                break;

            case zTXtChunk.NAME:
                Handle_zTXt(chunk);
                break;

            case iTXtChunk.NAME:
                Handle_iTXt(chunk);
                break;

            case bKGDChunk.NAME:
                Handle_bKGD(chunk);
                break;

            case hISTChunk.NAME:
                Handle_hIST(chunk);
                break;

            case pHYsChunk.NAME:
                Handle_pHYs(chunk);
                break;

            case sPLTChunk.NAME:
                Handle_sPLT(chunk);
                break;

            case tIMEChunk.NAME:
                Handle_tIME(chunk);
                break;

            default:
                return(false);
            }
            return(true);
        }