Ejemplo n.º 1
0
        public void Test()
        {
            string      original = "Do you like our owl?";
            ShannonFano f        = new ShannonFano();
            string      encoded  = f.Encode(original);
            string      decoded  = f.Decode(encoded);

            Assert.AreEqual(original, decoded);
        }
Ejemplo n.º 2
0
        public static void ThatAlgorithWorks()
        {
            // Arrange
            var shannonFano = new ShannonFano();

            const string phrase = "This is a string";

            // Act
            var result = shannonFano.Compress(phrase);

            // Assert
            Assert.IsNotEmpty(result);
            Assert.AreEqual("1001111000110100001101011100100111001101001100101", result);
        }
Ejemplo n.º 3
0
        public static void ThatAlgorithWorksWord()
        {
            // Arrange
            var shannonFano = new ShannonFano();

            const string word = "Hello";

            // Act
            var result = shannonFano.Compress(word);

            // Assert
            Assert.IsNotEmpty(result);
            Assert.AreEqual("1111100010", result);
        }
Ejemplo n.º 4
0
        protected void CompressWithFano(InnerFile file)
        {
            int dataSize = 0;

            int compressedSize = BitConverter.ToInt32(file.header.compressedSize, 0);

            if (compressedSize == 0)
            {
                dataSize = BitConverter.ToInt32(file.header.uncompressedSize, 0);
            }
            else
            {
                dataSize = compressedSize;
            }

            List <byte> data = new List <byte>(file.data);
            ShannonFano sfc  = new ShannonFano(data);
            Dictionary <byte, string> symCode = sfc.GetTable();

            // конвертируем таблицу кодов в массив
            // первый байт - символ
            // второй байт - длина кода символа
            // последовательность байтов - код символа
            List <byte> symCodeArr = new List <byte>();

            foreach (KeyValuePair <byte, string> entry in symCode)
            {
                symCodeArr.Add(entry.Key);
                symCodeArr.Add(Convert.ToByte(entry.Value.Length));
                symCodeArr.AddRange(Encoding.UTF8.GetBytes(entry.Value));
            }
            file.symCodeTable = symCodeArr.ToArray();

            //
            // переводим исходные данные в массив битов //
            //
            List <bool> compressedData = new List <bool>();

            foreach (byte b in file.data)
            {
                //Console.Write((char)b);
                foreach (char c in symCode[b])
                {
                    if (c == '1')
                    {
                        compressedData.Add(true);
                    }
                    else
                    {
                        compressedData.Add(false);
                    }
                }
            }
            BitArray bits = new BitArray(compressedData.ToArray());

            //
            // затем переводим биты в байты //
            //
            byte[] newData = new byte[(bits.Length - 1) / 8 + 1];
            bits.CopyTo(newData, 0);

            file.data = newData;
            file.header.compressedSize = BitConverter.GetBytes(newData.Length);
            file.header.fileDataOffset = BitConverter.GetBytes(file.symCodeTable.Length);
        }