Beispiel #1
0
        static void Main(string[] args)
        {
            string msg;

            Console.Write("Enter the string you want run through Huffman: ");
            msg = Console.ReadLine();
            Huffman Huff = new Huffman(msg);

            Huff.RunHuffman();
        }
Beispiel #2
0
        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            huffman = new Huffman();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        public bool Compress(ref Huffman.HuffmanData data)
        {
            // Récupération de la taille de la chaîne
            data.sizeOfUncompressedData = data.uncompressedData.Length;

            // Création d'un dictionnaire permettant de calculer le nombre d'occurences d'une lettre
            Dictionary<byte, int> dictionary = new Dictionary<byte, int>();

            foreach(byte b in data.uncompressedData)
            {
                if (dictionary.ContainsKey(b))
                {
                    dictionary[b] = dictionary[b] + 1;
                }
                else
                {
                    dictionary.Add(b, 1);
                }
            }

            // Remplissage du tableau de fréquences ainsi que des feuilles dans le noeud
            data.frequency = new List<KeyValuePair<byte, int>>();
            List<Node> nodes = new List<Node>();

            foreach (byte b in dictionary.Keys)
            {
                data.frequency.Add(new KeyValuePair<byte,int>(b, dictionary[b]));
                nodes.Add(new Occurence(new KeyValuePair<byte,int>(b, dictionary[b])));
            }

            // Création de l'arbre à une racine
            this.GetTree(ref nodes);

            // Création du code Huffman pour chaque lettre
            List<KeyValuePair<byte, List<bool>>> l_kvp = new List<KeyValuePair<byte, List<bool>>>();
            this.GetHuffmanCode(nodes[0], ref l_kvp, new List<bool>());

            // Remplacement de chaque caractère ASCII par son code Huffman
            this.CompressData(ref data, ref l_kvp);

            // Oubli de la valeur initiale de la chaîne
            data.uncompressedData = null;

            return true;
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            Console.Write("Enter text: ");

            string text = Console.ReadLine();

            string encodedText = Huffman.Encode(text);

            Huffman.PrintTableCode();

            Console.WriteLine($"encoded text: {encodedText}");

            Console.WriteLine($"Decoded text: {Huffman.Decode(encodedText)}");

            Console.WriteLine($"Source text memory size: {text.Length * 8}");

            Console.WriteLine($"Encoded text memory size: {encodedText.Length}");

            Console.WriteLine($"Compression ratio: {Huffman.CompressionRatio(text, encodedText)}");
        }
Beispiel #5
0
    public void TestHuffman()
    {
      var t = new Huffman();

      Assert.AreEqual("BDC", t.Decode("101101", new[] { "00", "10", "01", "11" }));
      Assert.AreEqual("CBAC", t.Decode("10111010", new[] { "0", "111", "10" }));
      Assert.AreEqual("BBBABBAABBABBAAABBA", t.Decode("0001001100100111001", new[] { "1", "0" }));
      Assert.AreEqual("EGGFAC", t.Decode("111011011000100110", new[] { "010", "00", "0110", "0111", "11", "100", "101" }));
      Assert.AreEqual("DBHABBACAIAIC", t.Decode("001101100101100110111101011001011001010", new[] { "110", "011", "10", "0011", "00011", "111", "00010", "0010", "010", "0000" }));

      Assert.AreEqual("NITXOQRE",
      t.Decode("01001111010010010011001000001010001101001000010",
      new[]
      {
 "01101", "01110", "01001110", "0100110", "00010", "01000", "0101", "0000",
 "001001", "111", "010011111", "1010", "100", "0100111101", "00101", "01100",
 "00011", "010010", "1011", "0011", "1101", "0100111100", "01111", "001000",
 "1100"
      }));
      Assert.AreEqual("BBBABBAABBABBAA", t.Decode("000100110010011", new[] { "1", "0" }));
    }
Beispiel #6
0
        public void TestHuffman()
        {
            var t = new Huffman();

            Assert.AreEqual("BDC", t.Decode("101101", new[] { "00", "10", "01", "11" }));
            Assert.AreEqual("CBAC", t.Decode("10111010", new[] { "0", "111", "10" }));
            Assert.AreEqual("BBBABBAABBABBAAABBA", t.Decode("0001001100100111001", new[] { "1", "0" }));
            Assert.AreEqual("EGGFAC", t.Decode("111011011000100110", new[] { "010", "00", "0110", "0111", "11", "100", "101" }));
            Assert.AreEqual("DBHABBACAIAIC", t.Decode("001101100101100110111101011001011001010", new[] { "110", "011", "10", "0011", "00011", "111", "00010", "0010", "010", "0000" }));

            Assert.AreEqual("NITXOQRE",
                            t.Decode("01001111010010010011001000001010001101001000010",
                                     new[]
            {
                "01101", "01110", "01001110", "0100110", "00010", "01000", "0101", "0000",
                "001001", "111", "010011111", "1010", "100", "0100111101", "00101", "01100",
                "00011", "010010", "1011", "0011", "1101", "0100111100", "01111", "001000",
                "1100"
            }));
            Assert.AreEqual("BBBABBAABBABBAA", t.Decode("000100110010011", new[] { "1", "0" }));
        }
        public bool Decompress(ref Huffman.HuffmanData data)
        {
            // Remplissage du tableau de fréquences ainsi que des feuilles dans le noeud            
            List<Node> nodes = new List<Node>();

            foreach (KeyValuePair<byte, int> kvp in data.frequency)
            {
                nodes.Add(new Occurence(new KeyValuePair<byte, int>(kvp.Key, kvp.Value)));
            }

            // Création de l'arbre à une racine
            this.GetTree(ref nodes);

            // Création du code Huffman pour chaque lettre
            List<KeyValuePair<byte, List<bool>>> l_kvp = new List<KeyValuePair<byte, List<bool>>>();
            this.GetHuffmanCode(nodes[0], ref l_kvp, new List<bool>());

            // Remplacement de chaque code Huffman par son caractère ASCII
            DecompressData(ref data, ref l_kvp);

            return true;
        }
Beispiel #8
0
 public static void Decode(string[] args)
 {
     if (args.Length != 2 || !args[1].EndsWith(".huff") || args[1].Length <= ".huff".Length)
     {
         Console.WriteLine("Argument Error");
     }
     else
     {
         try
         {
             Huffman.SetReader(args[1]);
             using (Huffman.writerOut)
             {
                 Huffman.PrintDecodedTree(args[1].Substring(0, args[1].Length - ".huff".Length));
                 Huffman.writerOut.Close();
             }
         }
         catch (Exception)
         {
             Console.WriteLine("File Error");
         }
     }
 }
Beispiel #9
0
        private void BtnCompact_Click(object sender, EventArgs e)
        {
            var text       = richTxtBxSource.Text;
            var caracteres = Cantidad_de_Informacion.analizarTexto(text);
            var arbol      = Huffman.crearNodos(caracteres);

            treeGraph.Nodes.Clear();

            treeGraph.BeginUpdate();
            foreach (var nodo in arbol)
            {
                treeGraph.Nodes.Add(nodo.NodoId.ToString(), nodo.Nombre);
            }
            treeGraph.EndUpdate();
            treeGraph.ExpandAll();

            Huffman.valorCompuesto(arbol);
            Huffman.palabraCodigo(arbol.inicial(caracteres), arbol.FindLast(x => true));

            treeGraph.BeginUpdate();
            treeGraph.Nodes.Clear();
            treeGraph.Nodes.AddRange(arbol.FindLast(x => true).arbolUI(arbol.inicial(caracteres)).ToArray());
            treeGraph.EndUpdate();
            treeGraph.ExpandAll();

            richTxtBxCompact.Text = Huffman.codigoHuffman(arbol.inicial(caracteres), text);
            richTxtBxResult.Text  = Huffman.information(text, richTxtBxCompact.Text);

            toolStripStLblText.Text    = text.Length + " caracteres analizados";
            toolStripStLblCompact.Text =
                Huffman.porcentaje(text.Length * 8, richTxtBxCompact.TextLength) + " compactado";

            splitContSide.Panel2Collapsed = false;

            btnShow.Text    = "OCULTAR";
            btnShow.Enabled = true;
        }
Beispiel #10
0
        public void DecompressData(ref Huffman.HuffmanData data, ref List<KeyValuePair<byte, List<bool>>> huffman)
        {
            // Récupération de code Huffman et transformation en liste de booléens
            BitArray ba = new BitArray(data.compressedData);
            bool[] tabBools = new bool[data.compressedData.Length*8];
            ba.CopyTo(tabBools, 0);
            List<bool> l_b = tabBools.ToList();

            // Création du tableau de la chaîne décompressée
            byte[] decompressed = new byte[data.sizeOfUncompressedData];
            int i = 0;

            // Liste de booléens comparée au tableau code Huffman
            List<bool> l = new List<bool>();

            // Parcours de la liste de booléens
            foreach (bool b in l_b)
            {
                // Vérification si on est pas hors index dans le tableau
                if (i < decompressed.Length)
                {
                    l.Add(b);
                    
                    foreach (KeyValuePair<byte, List<bool>> kvp in huffman)
                    {
                        // Si les deux listes sont égales, on a retrouvé le code Huffman donc le caractère ASCII
                        if (AreEquals(kvp.Value, l))
                        {
                            decompressed[i] = kvp.Key;
                            i++;
                            l.Clear();
                        }
                    }
                }
            }

            // On stocke la donnée dans la structure data de HuffmanData
            data.uncompressedData = decompressed;
        }
Beispiel #11
0
        public void CompressData(ref Huffman.HuffmanData data, ref List<KeyValuePair<byte, List<bool>>> huffman)
        {
            //Création de la liste booléenne représentant le code Huffman de la chaîne ASCII
            List<bool> l = new List<bool>();

            foreach (byte b in data.uncompressedData)
            {
                l.AddRange(GetValueOfKeyInList(ref huffman, b));
            }

            int end = 8 - l.Count % 8;

            // On ajoute des booléens jusqu'à avoir un nombre multiple de 8 de booléens
            for (int i = 0; i < end; i++)
            {
                l.Add(false);
            }

            // On transforme la liste de booléens en tableau de bytes
            BitArray compressed = new BitArray(l.ToArray());
            int nb = l.Count / 8;
            data.compressedData = new byte[nb];
            compressed.CopyTo(data.compressedData, 0);
        }