private static LinkedNode Decode(BitStream input, LinkedNode head) { LinkedNode node = head; while (node.Child0 != null) { int bit = input.ReadBits(1); if (bit == -1) { throw new Exception("Unexpected end of file"); } node = bit == 0 ? node.Child0 : node.Child1; } return(node); }
public static MemoryStream Decompress(Stream data) { int comptype = data.ReadByte(); if (comptype == 0) { throw new NotImplementedException("Compression type 0 is not currently supported"); } LinkedNode tail = BuildList(sPrime[comptype]); LinkedNode head = BuildTree(tail); MemoryStream outputstream = new MemoryStream(); BitStream bitstream = new BitStream(data); int decoded; do { LinkedNode node = Decode(bitstream, head); decoded = node.DecompressedValue; switch (decoded) { case 256: break; case 257: int newvalue = bitstream.ReadBits(8); outputstream.WriteByte((byte)newvalue); tail = InsertNode(tail, newvalue); break; default: outputstream.WriteByte((byte)decoded); break; } } while (decoded != 256); outputstream.Seek(0, SeekOrigin.Begin); return(outputstream); }
private static LinkedNode Decode(BitStream input, LinkedNode head) { LinkedNode node = head; while (node.Child0 != null) { int bit = input.ReadBits(1); if (bit == -1) { throw new Exception("Unexpected end of file"); } node = bit == 0 ? node.Child0 : node.Child1; } return node; }
public static MemoryStream Decompress(Stream data) { int comptype = data.ReadByte(); if (comptype == 0) { throw new NotImplementedException("Compression type 0 is not currently supported"); } LinkedNode tail = BuildList(sPrime[comptype]); LinkedNode head = BuildTree(tail); MemoryStream outputstream = new MemoryStream(); BitStream bitstream = new BitStream(data); int decoded; do { LinkedNode node = Decode(bitstream, head); decoded = node.DecompressedValue; switch (decoded) { case 256: break; case 257: int newvalue = bitstream.ReadBits(8); outputstream.WriteByte((byte)newvalue); tail = InsertNode(tail, newvalue); break; default: outputstream.WriteByte((byte)decoded); break; } } while (decoded != 256); outputstream.Seek(0, SeekOrigin.Begin); return outputstream; }