コード例 #1
0
        public static AnsCodingTable ReadTable(BitReader reader, byte[] encryptionKey = null)
        {
            AnsCodingTable ret = new AnsCodingTable();

            ret.Streamer            = new CodingTableStreamer(reader);
            ret.FrequencyDictionary = ret.Streamer.FreqDict;
            ret.Denominator         = ret.Streamer.Denominator;
            ret.SymbolType          = ret.Streamer.SymbolType;
            ret.GenerateTable(encryptionKey);
            return(ret);
        }
コード例 #2
0
        private void EncodeBlock(int blockContentLength, int targetDenominator)
        {
            Output.WriteLong(blockContentLength, 23);

            FrequencyDictionary <int> frequencies = new FrequencyDictionary <int>();

            for (int i = 0; i < blockContentLength; i++)
            {
                frequencies.AddData(Block[i]);
            }
            AnsCodingTable blockTable = new AnsCodingTable(frequencies, targetDenominator);

            blockTable.WriteTable(Output);
            WriteEncoding(blockContentLength, blockTable);
        }
コード例 #3
0
        private void DecodeBlock(BitReader readerIn)
        {
            int numCodedSymbols = readerIn.ReadInt(23);
            //Change to use stack only if the output stream is non-seekable
            //If it is seekable, then simply write the number of bytes that will be put into the stream
            //Then seek backward, writing them one by one
            Stack <int>    symbolsStack   = new Stack <int>();
            AnsCodingTable decodeTable    = AnsCodingTable.ReadTable(readerIn, EncryptionKey);
            int            bitCapacity    = BitUtilities.GetNumberBitsToRepresent(decodeTable.Denominator);
            AnsState       internalsState = new AnsState(bitCapacity, null, 0);

            internalsState.Underlying = readerIn.ReadUint(bitCapacity);
            KeyValuePair <int, int> symbolRow;

            for (int i = 0; i < numCodedSymbols; i++)
            {
                symbolRow = decodeTable.DecodePoint(internalsState.ToInt());
                symbolsStack.Push(symbolRow.Key);
                internalsState.Underlying = (uint)symbolRow.Value;
                int bitsToRead = bitCapacity - internalsState.Contained;
                for (int j = 0; j < bitsToRead; j++)
                {
                    internalsState.PushLower(readerIn.ReadBit());
                }
            }

            //Decoding finished...
            BitWriter symbolWriter = new BitWriter(Output);
            byte      numBitsToWrite;

            if (decodeTable.SymbolType == typeof(byte))
            {
                numBitsToWrite = 8;
            }
            else if (decodeTable.SymbolType == typeof(short))
            {
                numBitsToWrite = 16;
            }
            else
            {
                numBitsToWrite = 32;
            }
            while (symbolsStack.Count != 0)
            {
                int symbol = symbolsStack.Pop();
                symbolWriter.WriteLong(symbol, numBitsToWrite);
            }
        }
コード例 #4
0
        public void EncodeBlock(int[] blockIn, int count, int targetDenominator, byte[] encryptionKey = null)
        {
            Output.WriteLong(count, 23);
            Block = blockIn;
            FrequencyDictionary <int> frequencies = new FrequencyDictionary <int>();
            int offsetCount = count;

            for (int i = 0; i < offsetCount; i++)
            {
                frequencies.AddData(Block[i]);
            }
            AnsCodingTable blockTable = new AnsCodingTable(frequencies, targetDenominator, encryptionKey);

            blockTable.WriteTable(Output);
            WriteEncoding(count, blockTable);
        }
コード例 #5
0
        private void WriteEncoding(int blockContentLength, AnsCodingTable blockTable)
        {
            ReversedMemoryStream reversedStream = new ReversedMemoryStream();
            uint     initialState = ConstructMask(blockTable.Denominator);
            AnsState stateIn      = new AnsState(MaskBits, reversedStream, initialState);

            for (int i = 0; i < blockContentLength; i++)
            {
                int row       = stateIn.ToInt();
                int symbol    = Block[i];
                var validRows = blockTable[symbol];
                while (!validRows.ContainsKey(row))
                {
                    stateIn.PopLower();
                    row = stateIn.ToInt();
                }
                stateIn.Underlying = (uint)validRows[row];
            }
            stateIn.Flush();
            reversedStream.ReadToStream(Output);
        }