public TreeWalker(HuffmanTreeBase tree, IDecodingInputStream input, IDecodingOutputStream output, long seqLength) { this.tree = tree; this.input = input; this.output = output; this.seqLength = seqLength; }
public CodingTable BuildCodingTable(HuffmanTreeBase huffmanTree) { Guard.IsNotNull(huffmanTree, nameof(huffmanTree)); TreeVisitor visitor = new TreeVisitor(); huffmanTree.DoPOT(visitor); return(visitor.CodingTable); }
const int ChunkSize = 128 * 1024; // 128Kb throttling public EncodingToken CreateEncodingToken(IEncodingInputStream inputStream, CancellationToken cancellationToken) { Guard.IsNotNull(inputStream, nameof(inputStream)); WeightsTable weightsTable = BuildWeightsTable(inputStream, cancellationToken); HuffmanTreeBase huffmanTree = BuildHuffmanTree(weightsTable); CodingTable codingTable = BuildCodingTable(huffmanTree); return(new EncodingToken(weightsTable, huffmanTree, codingTable)); }
public void Decode(IDecodingInputStream inputStream, HuffmanTreeBase tree, IDecodingOutputStream outputStream, long sequenceLength, CancellationToken cancellationToken, IProgressHandler progress) { Guard.IsNotNull(inputStream, nameof(inputStream)); Guard.IsNotNull(tree, nameof(tree)); Guard.IsNotNull(outputStream, nameof(outputStream)); Guard.IsNotNegative(sequenceLength, nameof(sequenceLength)); const int chunkSize = 0x20000 * 8; // 128Kb long progressValue = progress?.State.CastTo <CodingProgressState>()?.Value ?? 0; long streamPosition = inputStream.Position; TreeWalker walker = new TreeWalker(tree, inputStream, outputStream, sequenceLength); while (!walker.Exhausted && !inputStream.IsEmpty) { if (!tree.Walk(walker)) { throw new ArgumentException(); } // Throttling progressValue += inputStream.Position - streamPosition; streamPosition = inputStream.Position; if (progressValue >= chunkSize) { cancellationToken.ThrowIfCancellationRequested(); progress?.Report(progressValue / 8, outputStream.Path); progressValue %= 8; } } if (progress != null) { CodingProgressState progressState = (CodingProgressState)progress.State ?? new CodingProgressState(); progress.State = progressState.WithValue(progressValue); } }
public EncodingToken(WeightsTable weightsTable, HuffmanTreeBase huffmanTree, CodingTable codingTable) { CodingTable = codingTable; WeightsTable = weightsTable; HuffmanTree = huffmanTree; }