示例#1
0
        public static void TestBitReaderAndBitWriter(string inputFile, string outputFile)
        {
            FileInfo file     = new FileInfo(inputFile);
            int      nbr      = 8 * (int)file.Length;
            Random   randomNb = new Random();

            using (BitReader bitReader = new BitReader(inputFile))
                using (BitWriter bitWriter = new BitWriter(outputFile))
                {
                    while (nbr > 0)
                    {
                        int nb = randomNb.Next(31) + 1;

                        if (nb > nbr)
                        {
                            nb = nbr;
                        }

                        int bitSequence = bitReader.readNBits(nb);
                        bitWriter.WriteNBits(bitSequence, nb);
                        nbr -= nb;
                    }

                    bitWriter.WriteNBits(0, 7);
                }
        }
示例#2
0
        public void Compress(string inputFile, string outputFile)
        {
            FileInfo file         = new FileInfo(inputFile);
            int      numberOfBits = 8 * (int)file.Length;
            int      indexMaxSize = Convert.ToInt32(Math.Pow(2, _index)) - 1;

            using (BitReader bitReader = new BitReader(inputFile))
                using (BitWriter bitWriter = new BitWriter(outputFile))
                {
                    bitWriter.WriteBit(_inghetare ? 1 : 0);
                    bitWriter.WriteNBits(_index, 4);

                    string s = "";
                    char   ch;

                    while (numberOfBits > 0)
                    {
                        ch            = Convert.ToChar(bitReader.readNBits(8));
                        numberOfBits -= 8;
                        int firstOccurence = _symbolList.IndexOf(s + ch);

                        if (firstOccurence != -1 && firstOccurence <= indexMaxSize)
                        {
                            s += ch;
                        }
                        else
                        {
                            Encode(s, bitWriter);

                            if (_symbolList.Count > indexMaxSize)
                            {
                                if (!_inghetare)
                                {
                                    _symbolList.Clear();
                                    for (int i = 0; i < 256; i++)
                                    {
                                        _symbolList.Add(((char)i).ToString());
                                    }
                                }
                            }

                            _symbolList.Add(s + ch);
                            s = Convert.ToString(ch);
                        }
                    }

                    Encode(s, bitWriter);
                    bitWriter.WriteNBits(0, 7);
                }
        }
示例#3
0
 private void Write(string s, BitWriter bitWriter)
 {
     for (int i = 0; i < s.Length; i++)
     {
         bitWriter.WriteNBits(Convert.ToInt32(Convert.ToChar(s[i])), 8);
     }
 }
示例#4
0
 private void WriteCharFrequencyOn4Bytes(BitWriter bitWriter)
 {
     foreach (int cFrequency in _charFrequency)
     {
         if (cFrequency != 0)
         {
             bitWriter.WriteNBits(cFrequency, 32);
         }
     }
 }
示例#5
0
        public void Decompress(string inputFile, string outputFile)
        {
            FileInfo file         = new FileInfo(inputFile);
            int      numberOfBits = (int)file.Length * 8;

            bufferD = new byte[file.Length];

            using (BitReader bitReader = new BitReader(inputFile))
                using (BitWriter bitWriter = new BitWriter(outputFile))
                {
                    int searchbufferLength, lookAheadBufferlength;

                    searchbufferLength    = bitReader.readNBits(5);
                    lookAheadBufferlength = bitReader.readNBits(3);

                    int position, length, ch, indexD = 0;

                    while (numberOfBits > 0)
                    {
                        position      = bitReader.readNBits(8);
                        length        = bitReader.readNBits(8);
                        ch            = bitReader.readNBits(8);
                        numberOfBits -= 24;

                        if (length == 0)
                        {
                            bitWriter.WriteNBits(ch, 8);
                            bufferD[indexD++] = Convert.ToByte(ch);
                        }
                        else
                        {
                            for (int i = indexD - position - 1; i < length; i++)
                            {
                                bufferD[indexD++] = bufferD[i];
                                bitWriter.WriteNBits(bufferD[i], 8);
                            }

                            bufferD[indexD++] = Convert.ToByte(ch);
                            bitWriter.WriteNBits(ch, 8);
                        }
                    }
                }
        }
示例#6
0
        public void Decompress(string inputFile, string outputFile)
        {
            FileInfo file         = new FileInfo(inputFile);
            int      numberOfBits = 8 * (int)file.Length;

            using (BitReader bitReader = new BitReader(inputFile))
                using (BitWriter bitWriter = new BitWriter(outputFile))
                {
                    bool inghetareD = bitReader.readNBits(1) == 1;
                    int  indexD     = bitReader.readNBits(4);
                    numberOfBits -= 5;

                    string s            = "";
                    int    indexMaxSize = Convert.ToInt32(Math.Pow(2, indexD)) - 1;
                    int    chIndex;
                    chIndex       = bitReader.readNBits(indexD);
                    numberOfBits -= indexD;
                    s             = _decompressSymbolList[chIndex];
                    bitWriter.WriteNBits(Convert.ToInt32(Convert.ToChar(s)), s.Length * 8);
                    string entry = "";

                    while (numberOfBits >= indexD)
                    {
                        chIndex       = bitReader.readNBits(indexD);
                        numberOfBits -= indexD;

                        if (chIndex >= _decompressSymbolList.Count)
                        {
                            entry = s + s[0];
                        }
                        else
                        {
                            entry = _decompressSymbolList[chIndex];
                        }

                        Write(entry, bitWriter);

                        if (_decompressSymbolList.Count > indexMaxSize)
                        {
                            if (!inghetareD)
                            {
                                _decompressSymbolList.Clear();
                                for (int i = 0; i < 256; i++)
                                {
                                    _decompressSymbolList.Add(((char)i).ToString());
                                }
                            }
                        }

                        _decompressSymbolList.Add(s + entry[0]);
                        s = entry;
                    }
                }
        }
示例#7
0
        private void Write(string inputFile, string outputFile)
        {
            using (BitWriter bitWriter = new BitWriter(outputFile))
            {
                //header
                WriteAlocationMap(bitWriter);
                WriteCharFrequencyOn4Bytes(bitWriter);

                FileInfo file         = new FileInfo(inputFile);
                int      numberOfBits = 8 * (int)file.Length;
                using (BitReader bitReader = new BitReader(inputFile))
                {
                    while (numberOfBits > 0)
                    {
                        int character = bitReader.readNBits(8);
                        bitWriter.WriteNBits(_charCode[character], _codeLength[character]);
                        numberOfBits -= 8;
                    }
                    bitWriter.WriteNBits(0, 7);
                }
            }
        }
示例#8
0
        private void DecodeAndWrite(BitReader bitReader, int fileLength, TreeNode <KeyValuePair <int, int> > huffmanTree, BitWriter bitWriter)
        {
            TreeNode <KeyValuePair <int, int> > currentNode;

            for (int i = 0; i < fileLength; i++)
            {
                currentNode = huffmanTree;

                while (!currentNode.IsLeaf())
                {
                    int bit = bitReader.readBit();
                    currentNode = bit == 0 ? currentNode.leftChild : currentNode.rightChild;
                }

                //scrie caracterul
                int decodedChar = currentNode.value.Key;
                bitWriter.WriteNBits(decodedChar, 8);
            }
        }
示例#9
0
 private void Encode(string s, BitWriter bitWriter)
 {
     bitWriter.WriteNBits(_symbolList.IndexOf(s.ToString()), _index);
 }
示例#10
0
 private void WriteToken(BitWriter bitWriter, int position, int length, int character)
 {
     bitWriter.WriteNBits(position, _p);
     bitWriter.WriteNBits(length, _l);
     bitWriter.WriteNBits(character, 8);
 }