public static void Compress(byte[] file, string destination, CompressionType cmp) { switch (cmp) { case CompressionType.Uncompressed: File.WriteAllBytes(destination, file); break; case CompressionType.Kosinski: using (MemoryStream input = new MemoryStream(file)) { using (FileStream output = File.Create(destination)) { using (PaddedStream paddedOutput = new PaddedStream(output, 2, PaddedStreamMode.Write)) { Kosinski.Compress(input, paddedOutput); } } } break; case CompressionType.KosinskiM: using (MemoryStream input = new MemoryStream(file)) { using (FileStream output = File.Create(destination)) { using (PaddedStream paddedOutput = new PaddedStream(output, 2, PaddedStreamMode.Write)) { ModuledKosinski.Compress(input, paddedOutput, LevelData.littleendian ? Endianness.LittleEndian : Endianness.BigEndian); } } } break; case CompressionType.Nemesis: Nemesis.Compress(file, destination); break; case CompressionType.Enigma: Enigma.Compress(file, destination, LevelData.littleendian ? Endianness.LittleEndian : Endianness.BigEndian); break; case CompressionType.SZDD: SZDDComp.SZDDComp.Compress(file, destination); break; default: break; } }
public void PaddedStream_PadsInnerStreamData(byte[] data, byte paddingValue, int paddingLength) { PaddedStream paddedStream = new PaddedStream(new MemoryStream(data) { Position = 0 }, paddingValue, paddingLength); Assert.AreEqual(0, paddedStream.Position); Assert.AreEqual(data.Length + paddingLength, paddedStream.Length); MemoryStream ouputStream = new MemoryStream(); paddedStream.CopyTo(ouputStream); Assert.AreEqual(paddedStream.Length, paddedStream.Position); CollectionAssert.AreEqual( data.Concat(Enumerable.Repeat(paddingValue, paddingLength)).ToArray(), ouputStream.ToArray()); }
public void PaddedStream_SeekingWorks() { PaddedStream paddedStream = new PaddedStream( new MemoryStream(Enumerable.Range(0, 20).Select(i => (byte)i).ToArray()) { Position = 0 }, 0, 30); MemoryStream ouputStream = new MemoryStream(); paddedStream.CopyTo(ouputStream); Assert.AreEqual(paddedStream.Length, paddedStream.Position); CollectionAssert.AreEqual( Enumerable.Range(0, 20).Select(i => (byte)i).Concat(Enumerable.Repeat((byte)0, 30)).ToArray(), ouputStream.ToArray()); paddedStream.Position = 10; Assert.AreEqual(10, paddedStream.Position); ouputStream = new MemoryStream(); paddedStream.CopyTo(ouputStream); CollectionAssert.AreEqual( Enumerable.Range(10, 10).Select(i => (byte)i).Concat(Enumerable.Repeat((byte)0, 30)).ToArray(), ouputStream.ToArray()); paddedStream.Position = 20; Assert.AreEqual(20, paddedStream.Position); ouputStream = new MemoryStream(); paddedStream.CopyTo(ouputStream); CollectionAssert.AreEqual( Enumerable.Repeat((byte)0, 30).ToArray(), ouputStream.ToArray()); }
public PixelStorageWriterStream(Stream imageFormatterStream, Stream embeddedImagedDataStream, PixelStorageOptions storageOptions, bool leaveOpen, int bufferSize = 4096) : base(imageFormatterStream, storageOptions, leaveOpen) { if (embeddedImagedDataStream != null) { long bytesLeftInEmbeddedImage = embeddedImagedDataStream.Length - embeddedImagedDataStream.Position; if (bytesLeftInEmbeddedImage < Length) { embeddedImagedDataStream = new PaddedStream( embeddedImagedDataStream, paddingValue: 0, paddingLength: Length - bytesLeftInEmbeddedImage); } _embeddedImageDataStream = new BufferedStream(embeddedImagedDataStream, bufferSize); } else { _embeddedImageDataStream = new ConstantStream(0); } _bufferSize = bufferSize; }
public static void Compress(byte[] file, string destination, CompressionType cmp) { try { switch (cmp) { case CompressionType.Uncompressed: File.WriteAllBytes(destination, file); break; case CompressionType.Kosinski: using (MemoryStream input = new MemoryStream(file)) { using (FileStream output = File.Create(destination)) { using (PaddedStream paddedOutput = new PaddedStream(output, 2, PaddedStreamMode.Write)) { Kosinski.Compress(input, paddedOutput); } } } break; case CompressionType.KosinskiM: using (MemoryStream input = new MemoryStream(file)) { using (FileStream output = File.Create(destination)) { using (PaddedStream paddedOutput = new PaddedStream(output, 2, PaddedStreamMode.Write)) { ModuledKosinski.Compress(input, paddedOutput, LevelData.littleendian ? Endianness.LittleEndian : Endianness.BigEndian); } } } break; case CompressionType.Nemesis: Nemesis.Compress(file, destination); break; case CompressionType.Enigma: Enigma.Compress(file, destination, LevelData.littleendian ? Endianness.LittleEndian : Endianness.BigEndian); break; case CompressionType.SZDD: SZDDComp.SZDDComp.Compress(file, destination); break; case CompressionType.Comper: Comper.Compress(file, destination); break; default: throw new ArgumentOutOfRangeException("cmp", "Invalid compression type " + cmp + "!"); } } catch { LevelData.Log("Unable to write file \"" + destination + "\" with compression " + cmp.ToString() + ":"); throw; } }