public async Task DecompressAsync(Stream input, Stream output, long bitsLength, HuffmanNode root, CancellationTokenSource tokenSource, IProgress <ProgressArgs> progress, ProgressArgs args) { if (input == null) { throw new ArgumentNullException("input"); } if (output == null) { throw new ArgumentNullException("output"); } if (root == null) { throw new ArgumentNullException("root"); } await Task.Run(() => DecompressInternal(input, output, bitsLength, root, tokenSource, progress, args), tokenSource.Token); }
public async Task DecompressAsync(Stream input, Stream output, long bitsLength, HuffmanNode root) { if (input == null) { throw new ArgumentNullException("input"); } if (output == null) { throw new ArgumentNullException("output"); } if (root == null) { throw new ArgumentNullException("root"); } await Task.Run(() => DecompressInternal(input, output, bitsLength, root, null, null, null)); }
public void Decompress(Stream input, Stream output, long bitsLength, HuffmanNode root) { DecompressInternal(input, output, bitsLength, root, null, null, null); }
private void DecompressInternal(Stream input, Stream output, long bitsLength, HuffmanNode root, CancellationTokenSource tokenSource, IProgress <ProgressArgs> progress, ProgressArgs args) { if (tokenSource != null) { tokenSource.Token.ThrowIfCancellationRequested(); } var bitReader = new BitReadStream(input, bitsLength) { BufferSize = BUFFER_SIZE }; var buf = new byte[BUFFER_SIZE]; var index = 0; byte?b = null; do { var current = root; while (!current.IsSymbol) { b = bitReader.ReadBit(); if (b == null) { break; } if (b == 1) { current = current.Right; } else { current = current.Left; } } if (b != null) { buf[index] = (byte)current.Symbol; index++; } if (index >= BUFFER_SIZE) { if (tokenSource != null) { tokenSource.Token.ThrowIfCancellationRequested(); } output.Write(buf, 0, buf.Length); if (progress != null) { args.CurrentSize += index; progress.Report(args); } buf = new byte[BUFFER_SIZE]; index = 0; } } while (b != null); if (index != 0) { output.Write(buf, 0, index); if (progress != null) { args.CurrentSize += index; progress.Report(args); } } }
public HuffmanNode BuildTree(IEnumerable <SymbolCode> codes) { var root = new HuffmanNode(); var codesArr = codes.ToArray(); for (short i = 0; i < codesArr.Length; i++) { var code = codesArr[i]; if (code != null) { var current = root; for (var check = 1 << code.Length - 1; check != 0; check >>= 1) { if ((code.Bits & check) != 0) { if (check != 1) { if (current.Right == null) { current.Right = new HuffmanNode { Parent = current, IsSymbol = false } } ; current = current.Right; } else { current.Right = new HuffmanNode { Parent = current, Left = current.Right == null ? null : current.Right.Left, Right = current.Right == null ? null : current.Right.Right, Symbol = i, IsSymbol = true }; } } else { if (check != 1) { if (current.Left == null) { current.Left = new HuffmanNode { Parent = current, IsSymbol = false } } ; current = current.Left; } else { current.Left = new HuffmanNode { Parent = current, Left = current.Left == null ? null : current.Left.Left, Right = current.Left == null ? null : current.Left.Right, Symbol = i, IsSymbol = true }; } } } } } return(root); }
public static int Compare(HuffmanNode x, HuffmanNode y) { return(x.weight.CompareTo(y.weight)); }