public void DeflateCodec_CompressionDecompressionRoundTrip() { var codec = new DeflateCodec(); using (var memoryStream = new MemoryStream()) using (var compressedStream = codec.GetCompressedStreamOver(memoryStream)) { Assert.IsTrue(compressedStream.CanRead); Assert.IsTrue(compressedStream.CanWrite); Assert.IsTrue(compressedStream.CanSeek); Assert.AreEqual(0, compressedStream.Position); var expected = Utilities.GetRandom <byte[]>(false); compressedStream.Write(expected, 0, expected.Length); compressedStream.Flush(); compressedStream.Seek(0, SeekOrigin.Begin); using (var decompressedStream = codec.GetDecompressedStreamOver(compressedStream)) { var actual = new byte[expected.Length]; decompressedStream.Read(actual, 0, actual.Length); Assert.IsTrue(expected.SequenceEqual(actual)); Assert.IsTrue(decompressedStream.CanRead); Assert.IsFalse(decompressedStream.CanWrite); //TODO CanSeek should be 'false' however it is 'true' now although an exception is thrown when Seek() is called //Assert.IsFalse(decompressedStream.CanSeek); Utilities.ShouldThrow <NotSupportedException>(() => { var result = decompressedStream.Position; }); Utilities.ShouldThrow <NotSupportedException>(() => { var result = decompressedStream.Length; }); Utilities.ShouldThrow <NotSupportedException>(() => { decompressedStream.Seek(0, SeekOrigin.Begin); }); Utilities.ShouldThrow <NotSupportedException>(() => { decompressedStream.SetLength(0); }); Utilities.ShouldThrow <NotSupportedException>(() => { decompressedStream.Position = 0; }); Utilities.ShouldThrow <NotSupportedException>(() => { decompressedStream.Write(expected, 0, expected.Length); }); compressedStream.Flush(); } } }
public void CompressionLevels(int compressionLevel) { var codec = new DeflateCodec(null, compressionLevel); Assert.True(codec.CompressionLevel <= 9); Assert.True(codec.CompressionLevel >= 1); }
public void DeflateCodec_GetDecompressedStreamOver() { Assert.Throws <ArgumentNullException>(() => { var codec = new DeflateCodec(); codec.GetDecompressedStreamOver(null); } ); }
public Bitmap DecodeImage(Stream inps) { if (inps == null) { return(null); } // !!!{{ TODO: add the decoding code here IEntropyCodec c = new DeflateCodec(); c.BinaryStream = inps; c.Open(false); uint magic = (uint)c.GetBits(32); if (magic != MAGIC) { return(null); } int width, height; width = (int)c.GetBits(16); height = (int)c.GetBits(16); if (width < 1 || height < 1) { return(null); } Bitmap result = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int gr = c.Get(); if (gr < 0) { goto fin; } result.SetPixel(x, y, Color.FromArgb(gr, gr, gr)); } } fin: c.Close(); return(result); // !!!}} }
public void EncodeImage(Bitmap inp, Stream outs) { if (inp == null || outs == null) { return; } int width = inp.Width; int height = inp.Height; if (width < 1 || height < 1) { return; } // !!!{{ TODO: add the encoding code here IEntropyCodec c = new DeflateCodec(); c.MaxSymbol = 255; if (c.MaxSymbol < 255) { throw new Exception("Unappropriate codec used (alphabet too small)!"); } c.BinaryStream = outs; c.Open(true); // file header: [ MAGIC, width, height ] c.PutBits(MAGIC, 32); c.PutBits(width, 16); c.PutBits(height, 16); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color col = inp.GetPixel(x, y); int gr = Draw.RgbToGray(col.R, col.G, col.B); c.Put(gr & 0xff); } } c.Close(); // !!!}} }
public IEntropyCodec EncodeHeader(int width, int height, float fps, Stream outs) { frameWidth = width; frameHeight = height; framesPerSecond = fps; if (outs == null) { return(null); } IEntropyCodec c = new DeflateCodec(); c.MaxSymbol = 255; if (c.MaxSymbol < 255) { throw new Exception("Unappropriate codec used (alphabet too small)!"); } c.BinaryStream = outs; c.Open(true); // !!!{{ TODO: add the header construction/encoding here // video header: [ MAGIC, width, height, fps ] c.PutBits(MAGIC, 32); c.PutBits(width, 16); c.PutBits(height, 16); int fpsInt = (int)(100.0f * fps); c.PutBits(fpsInt, 16); // !!!}} return(c); }
public IEntropyCodec DecodeHeader(Stream inps) { if (inps == null) { return(null); } IEntropyCodec c = new DeflateCodec(); c.BinaryStream = inps; c.Open(false); // !!!{{ TODO: add the header decoding here // Check the global header: uint magic = (uint)c.GetBits(32); if (magic != MAGIC) { return(null); } frameWidth = (int)c.GetBits(16); frameHeight = (int)c.GetBits(16); if (frameWidth < 1 || frameHeight < 1) { return(null); } framesPerSecond = (int)c.GetBits(16); // !!!}} return(c); }
public DeflateCodecTests() { _deflateCodec = new DeflateCodec(Encoding.UTF8); }
public void DeflateCodec_GetDecompressedStreamOver() { var codec = new DeflateCodec(); codec.GetDecompressedStreamOver(null); }
public void DeflateCodec_GetCompressedStreamFromNull() { var codec = new DeflateCodec(); codec.GetCompressedStreamOver(null); }
public void DeflateCodec_GetCompressedStreamFromNull() { var codec = new DeflateCodec(); Assert.ThrowsException <ArgumentNullException>(() => codec.GetCompressedStreamOver(null)); }