Ejemplo n.º 1
0
        bool Extract()
        {
            fsin.Seek(0, SeekOrigin.Begin);

            for (int i = 0; i < 256; i++)
            {
                huffLength[i] = fsin.ReadByte();
                huffCode[i]   = (byte)fsin.ReadByte();

                if (huffLength[i] > 0)
                {
                    InsertIntoHuffTree(i);
                }
            }

            oriFileSize = 0;

            for (int i = 0; i < 8; i++)
            {
                oriFileSize <<= 8;
                oriFileSize  |= (long)(fsin.ReadByte() & 0xFF);
            }

            uint        bit  = 0;
            HuffmanNode node = headNode;

            ReadBitFromFile(ref bit);

            do
            {
                while (node.mLeft != null && node.mRight != null)
                {
                    if (bit == 0)
                    {
                        node = node.mLeft;
                    }
                    else
                    {
                        node = node.mRight;
                    }

                    ReadBitFromFile(ref bit);
                }

                fsout.WriteByte(node.mValue);
                node = headNode;
            }while (fsout.Position < oriFileSize);

            return(true);
        }
Ejemplo n.º 2
0
        void InsertIntoHuffTree(int value)
        {
            try
            {
                if (headNode == null)
                {
                    headNode = new HuffmanNode();
                }

                HuffmanNode curNode = headNode;

                int  length = huffLength[value];
                uint code   = huffCode[value];

                while (length > 0)
                {
                    uint bit = (code >> length - 1) & ~(~0 << 1);

                    if (bit == 0)
                    {
                        if (curNode.mLeft == null)
                        {
                            curNode.mLeft = new HuffmanNode();
                        }
                        curNode = curNode.mLeft;
                    }
                    else
                    {
                        if (curNode.mRight == null)
                        {
                            curNode.mRight = new HuffmanNode();
                        }
                        curNode = curNode.mRight;
                    }

                    length--;
                }

                curNode.mValue = (byte)value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace + ", (" + ex.Message + ")");
            }
        }
Ejemplo n.º 3
0
        public void MakeHuffmanTree()
        {
            nodeCount = 0;

            for (int i = 0; i < 256; i++)
            {
                if (freq[i] > 0)
                {
                    HuffmanNode node = new HuffmanNode();
                    node.mValue = (byte)i;
                    node.mFreq  = freq[i];
                    node.mRight = null;
                    node.mLeft  = null;

                    nodeCount++;

                    huffList[nodeCount - 1] = node;
                }
            }

            int head = nodeCount;

            while (head > 1)
            {
                int         min   = FindMinFrequency(head);
                HuffmanNode node1 = huffList[min];

                head--;
                huffList[min] = huffList[head];

                min = FindMinFrequency(head);
                HuffmanNode node2 = huffList[min];

                HuffmanNode node = new HuffmanNode();

                node.mValue = 0;
                node.mFreq  = node1.mFreq + node2.mFreq;
                node.mLeft  = node1;
                node.mRight = node2;

                huffList[min] = node;
            }

            headNode = huffList[0];
        }
Ejemplo n.º 4
0
        public void MakeCode(HuffmanNode node, uint code, int length)
        {
            if (node.mLeft == null && node.mRight == null)
            {
                huffCode[node.mValue]   = code;
                huffLength[node.mValue] = length;
            }
            else
            {
                code = code << 1;
                length++;
                MakeCode(node.mLeft, code, length);

                code = code | 1u;
                MakeCode(node.mRight, code, length);
                code = code >> 1;
                length--;
            }
        }
Ejemplo n.º 5
0
        private void DeleteHuffTree()
        {
            Stack <HuffmanNode> nodes = new Stack <HuffmanNode>();

            nodes.Push(headNode);

            while (nodes.Count > 0)
            {
                HuffmanNode i = nodes.Pop();

                if (i.mRight != null)
                {
                    nodes.Push(i.mRight);
                }

                if (i.mLeft != null)
                {
                    nodes.Push(i.mLeft);
                }

                i.DeleteNode();
            }

            for (int i = 0; i < 256; i++)
            {
                huffList[i]   = null;
                freq[i]       = 0;
                huffCode[i]   = 0;
                huffLength[i] = 0;
            }

            cmpByte = 0;
            cmpBit  = 7;

            extByte = 0;
            extBit  = -1;
        }