示例#1
0
 static void Main(string[] args)
 {
     HuffmanData data = new HuffmanData();
     data.uncompressedData = new byte[] { 0, 0, 1, 21, 1, 2, 1, 48, 4, 54, 1, 2, 255 };
     HuffmanLibrary hufflib = new HuffmanLibrary();
     hufflib.Compress(ref data);
 }
示例#2
0
        public bool Decompress(ref HuffmanData data)
        {
            if (data.compressedData.Count() > 0)
            {
                Data tmp = new Data();
                tmp.CreateDicoFreq(data.frequency);
                tmp.CreateTabHuff();

                data.uncompressedData = Decode(data.compressedData, tmp);
                return(true);
            }
            return(false);
        }
示例#3
0
        public bool Compress(ref HuffmanData data)
        {
            Data tmp = new Data();

            if (data.uncompressedData.Count() > 0)
            {
                tmp.ReadFreq(data.uncompressedData);
                tmp.CreateTabHuff();

                data.compressedData         = Encode(data.uncompressedData, tmp);
                data.frequency              = tmp.SendList();
                data.sizeOfUncompressedData = data.compressedData.Count();
                return(true);
            }
            return(false);
        }
示例#4
0
        public bool Compress(ref HuffmanData data)
        {
            Dictionary<byte, int> dictOcc = lettreOcc(data.uncompressedData);
            Dictionary<byte, List<bool>> huffcode = new Dictionary<byte, List<bool>>();
            List<Noeud> nodelist = new List<Noeud>();
            List<Noeud> leaflist = new List<Noeud>();
            List<bool> finalList = new List<bool>();

            // convert dictOcc to List<KVP> for huffman data
            data.frequency = dictOcc.ToList();

            foreach (var pair in dictOcc)
            {
                Noeud n = new Noeud(pair.Value, pair.Key);
                nodelist.Add(n);
                leaflist.Add(n);
            }

            while (nodelist.Count > 1)
            {
                iteration(ref nodelist);
            }

            foreach (Noeud leaf in leaflist)
            {
                byte currentLetter = leaf.getLetter();
                List<bool> code = new List<bool>();
                Noeud n = leaf;
                while (n.getParent() != null)
                {
                    code.Insert(0, n.getIsRight());
                    n = n.getParent();
                }
                huffcode.Add(currentLetter, code);
            }

            /*foreach (KeyValuePair<byte, List<bool>> pair in huffcode)
            {
                Console.WriteLine("{0} - {1}", Convert.ToChar(pair.Key), pair.Value);
            }*/

            data.sizeOfUncompressedData = data.uncompressedData.Length;

            for (int i = 0; i < data.sizeOfUncompressedData; i++)
            {
                finalList.AddRange(huffcode[data.uncompressedData[i]]);
            }

            // zero padding
            // il faut ajouter (8 - finalList.Count % 8) zeros ou 0 si c'est deja un multiple de 8
            int nbZeros = (finalList.Count % 8 == 0) ? 0 : (8 - finalList.Count % 8);
            while (nbZeros > 0)
            {
                finalList.Add(false);
                nbZeros--;
            }

            // determiner la taille de la compressed byte array
            int finalsize = finalList.Count / 8;
            data.compressedData = new byte[finalsize];
            // boucle de remplissage de la byte array
            for (int j = 0; j < finalsize; j++)
            {
                // une iteration pour un byte
                byte b = 0;
                for (int i = 0; i < 8; i++)
                {
                    b = (byte)(b | Convert.ToByte(finalList[0]));
                    finalList.RemoveAt(0);
                    if (i < 7)
                    {
                        b = (byte)(b << 1);
                    }
                }
                data.compressedData[j] = b;
            }

            return true;
        }
示例#5
0
        bool IPlugin.Decompress(ref HuffmanData data)
        {
            throw new NotImplementedException();
            int size = data.sizeOfUncompressedData;
            while (size > 0)
            {

                size--;
            }
        }