Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        public long Encode(IEncodingInputStream inputStream, IEncodingOutputStream outputStream, EncodingToken encodingToken, CancellationToken cancellationToken, IProgressHandler progress)
        {
            Guard.IsNotNull(inputStream, nameof(inputStream));
            Guard.IsNotNull(outputStream, nameof(outputStream));
            Guard.IsNotNull(encodingToken, nameof(encodingToken));

            CodingTable codingTable = encodingToken.CodingTable;

            inputStream.Reset();
            long sequenceLength = 0;
            long progressValue  = progress?.State.CastTo <CodingProgressState>()?.Value ?? 0;

            while (inputStream.ReadSymbol(out byte symbol))
            {
                BitSequence codingSequence = codingTable[symbol];
                for (int n = 0; n < codingSequence.Size; n++)
                {
                    Bit bit = codingSequence[n];
                    sequenceLength++;
                    outputStream.WriteBit(bit);
                }
                // Throttling
                if (++progressValue == ChunkSize)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    progress?.Report(ChunkSize, inputStream.Path);
                    progressValue = 0;
                }
            }
            if (progress != null)
            {
                CodingProgressState progressState = (CodingProgressState)progress.State ?? new CodingProgressState();
                progress.State = progressState.WithValue(progressValue);
            }
            return(sequenceLength);
        }
Ejemplo n.º 3
0
 public EncodingToken(WeightsTable weightsTable, HuffmanTreeBase huffmanTree, CodingTable codingTable)
 {
     CodingTable  = codingTable;
     WeightsTable = weightsTable;
     HuffmanTree  = huffmanTree;
 }
Ejemplo n.º 4
0
 public TreeVisitor()
 {
     this.codingTable = new CodingTable();
     this.bitSequence = new BitSequence();
 }