Пример #1
0
        public static List <Chunk> CompressIDATs(byte[] cipheredBytes)
        {
            byte[]       compressedBytes = ZlibCompression.Compress(cipheredBytes);
            List <Chunk> resultIdats     = IDATConverter.SplitToIDATs(compressedBytes).Select(idat => (Chunk)idat).ToList();

            return(resultIdats);
        }
Пример #2
0
        public void ShouldConcatDecompressCompressAndThenSplitImage()
        {
            string       filePath       = @"../../../Data/lena.png";
            List <Chunk> chunks         = PNGFile.Read(filePath);
            List <Chunk> parsedChunks   = ChunkParser.Parse(chunks);
            int          firstIdatIndex = parsedChunks.TakeWhile(chunk => !IsIDAT(chunk)).Count();
            List <IDAT>  idats          = parsedChunks.Where(IsIDAT).Select(chunk => (IDAT)chunk).ToList();

            byte[]       bytes             = IDATConverter.ConcatToBytes(idats);
            byte[]       decompressedBytes = ZlibCompression.Decompress(bytes);
            byte[]       compressedBytes   = ZlibCompression.Compress(decompressedBytes);
            List <Chunk> resultIdats       = IDATConverter.SplitToIDATs(compressedBytes).Select(idat => (Chunk)idat).ToList();
            List <Chunk> resultChunks      = parsedChunks.Where(chunk => !IsIDAT(chunk)).ToList();

            resultChunks.InsertRange(firstIdatIndex, resultIdats);
            PNGFile.Write(@"../../../Data/lenaCompressed.png", resultChunks);
        }
Пример #3
0
    public CompiledMetafile(Metafile file)
    {
        Name   = file.Name;
        Source = file;

        using (var metaFileStream = new MemoryStream())
        {
            using (var metaFileWriter = new BinaryWriter(metaFileStream, CodePagesEncodingProvider.Instance.GetEncoding(949), true))
            {
                metaFileWriter.Write((byte)(file.Nodes.Count / 256));
                metaFileWriter.Write((byte)(file.Nodes.Count % 256));
                foreach (var node in file.Nodes)
                {
                    byte[] nodeBuffer = CodePagesEncodingProvider.Instance.GetEncoding(949).GetBytes(node.Text);
                    metaFileWriter.Write((byte)nodeBuffer.Length);
                    metaFileWriter.Write(nodeBuffer);
                    metaFileWriter.Write((byte)(node.Properties.Count / 256));
                    metaFileWriter.Write((byte)(node.Properties.Count % 256));
                    foreach (var property in node.Properties)
                    {
                        byte[] propertyBuffer = CodePagesEncodingProvider.Instance.GetEncoding(949).GetBytes(property);
                        metaFileWriter.Write((byte)(propertyBuffer.Length / 256));
                        metaFileWriter.Write((byte)(propertyBuffer.Length % 256));
                        metaFileWriter.Write(propertyBuffer);
                    }
                }
            }

            Checksum = ~Crc32.Calculate(metaFileStream.ToArray());
            metaFileStream.Seek(0, SeekOrigin.Begin);

            using (var compressedMetaFileStream = new MemoryStream())
            {
                ZlibCompression.Compress(metaFileStream, compressedMetaFileStream);
                Data = compressedMetaFileStream.ToArray();
            }
        }
    }
Пример #4
0
 private static void CompressAndDecompress(byte[] bytes)
 {
     byte[] compressed   = ZlibCompression.Compress(bytes);
     byte[] decompressed = ZlibCompression.Decompress(compressed);
     Assert.AreEqual(bytes, decompressed);
 }