Пример #1
0
        public void TestZlibHeadersAreAddedBack()
        {
            IEnumerable <PNGChunk> chunks = PNGChunkParser.ChunksFromStream(new MemoryStream(Crushed10x10White));

            IEnumerable <PNGChunk> decrushedChunks = PNGDecrusher.DecrushChunks(chunks);
            PNGChunk decrushedIdatChunk            = decrushedChunks.First(c => c.Type == PNGChunk.ChunkType.IDAT);

            using (MemoryStream idatDataStream = new MemoryStream(decrushedIdatChunk.Data))
            {
                // basic check of the zlib header
                // http://tools.ietf.org/html/rfc1950#page-4
                int CMF = idatDataStream.ReadByte();
                int FLG = idatDataStream.ReadByte();

                int compressionMethod = CMF & 0x0F;
                int compressionInfo   = (CMF & 0xF0) >> 4;

                int COMPRESSION_METHOD_DEFLATE = 8;
                Assert.AreEqual(COMPRESSION_METHOD_DEFLATE, compressionMethod);

                using (DeflateStream deflateStream = new DeflateStream(idatDataStream, CompressionMode.Decompress))
                    using (MemoryStream decompressedStream = new MemoryStream())
                    {
                        deflateStream.CopyTo(decompressedStream);
                        Assert.AreEqual(410, decompressedStream.Length);
                    }
            }
        }
Пример #2
0
        public void TestChunksAreCorrectlyParsed()
        {
            using (MemoryStream stream = new MemoryStream(Simple10x10WhitePNG))
            {
                PNGChunk[] chunks = PNGChunkParser.ChunksFromStream(stream).ToArray();
                Assert.AreEqual(6, chunks.Length);

                PNGChunk idatChunk = chunks[4];
                Assert.AreEqual("IDAT", idatChunk.TypeString);
                Assert.AreEqual(PNGChunk.ChunkType.IDAT, idatChunk.Type);
                Assert.AreEqual(17, idatChunk.Data.Length);
            }
        }
Пример #3
0
        /// <summary>
        /// Determines whether the <see cref="Stream"/> represents a crushed PNG image.
        /// </summary>
        /// <param name="input">
        /// A <see cref="Stream"/> which contains a PNG image, which can be crushed or not.
        /// </param>
        /// <returns>
        /// <see langword="true"/> if <paramref name="input"/> represents a crushed PNG image;
        /// otherwise, <see langword="false"/>.
        /// </returns>
        public static bool CanDecrush(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            input.Seek(0, SeekOrigin.Begin);

            var chunks = PNGChunkParser.ChunksFromStream(input);

            return(chunks.Any(c => c.Type == PNGChunk.ChunkType.CgBI));
        }
Пример #4
0
        public void TestWritingChunksProducesIdenticalBytesAsInput()
        {
            byte[] input = Simple10x10WhitePNG;
            using (MemoryStream stream = new MemoryStream(Simple10x10WhitePNG))
            {
                PNGChunk[] chunks = PNGChunkParser.ChunksFromStream(stream).ToArray();

                MemoryStream output = new MemoryStream();
                PNGChunkParser.WriteChunksAsPNG(chunks, output);

                byte[] outputBytes = output.ToArray();

                CollectionAssert.AreEqual(input, outputBytes);
            }
        }
Пример #5
0
        public void TestWritingChunksAfterRecalculatingCRCProducesIdenticalBytesAsInput()
        {
            byte[] input = Simple10x10WhitePNG;
            using (MemoryStream stream = new MemoryStream(Simple10x10WhitePNG))
            {
                PNGChunk[] chunks = PNGChunkParser.ChunksFromStream(stream).ToArray();
                chunks = chunks.Select(c =>
                {
                    return(new PNGChunk(c.TypeString, c.Data, PNGDecrusher.CalculateCRCForChunk(c.TypeString, c.Data)));
                }).ToArray();

                MemoryStream output = new MemoryStream();
                PNGChunkParser.WriteChunksAsPNG(chunks, output);

                byte[] outputBytes = output.ToArray();

                CollectionAssert.AreEqual(input, outputBytes);
            }
        }
Пример #6
0
        public void TestCRCCalculation()
        {
            byte[][] validPNGs =
            {
                Simple10x10WhitePNG,
                Crushed10x10White,
                CrushedFiftyPercentAlphaKindaBlue10x10
            };

            foreach (byte[] png in validPNGs)
            {
                IEnumerable <PNGChunk> chunks = PNGChunkParser.ChunksFromStream(new MemoryStream(Simple10x10WhitePNG));
                foreach (PNGChunk chunk in chunks)
                {
                    uint expectedCRC     = chunk.DataCRC;
                    uint recalculatedCRC = PNGDecrusher.CalculateCRCForChunk(chunk.TypeString, chunk.Data);
                    Assert.AreEqual(expectedCRC, recalculatedCRC);
                }
            }
        }
Пример #7
0
        private static void DecrushAtChunkLevel(Stream input, Stream output)
        {
            IEnumerable <PNGChunk> fixedChunks = DecrushChunks(PNGChunkParser.ChunksFromStream(input));

            PNGChunkParser.WriteChunksAsPNG(fixedChunks, output);
        }