Beispiel #1
0
        private void BuildNodes()
        {
            for (int i = 0; i <= 255; i++)
            {
                int currentOccurences = occurences[i];

                HuffmanNode node = new HuffmanNode();
                node.Data        = (byte)i;
                node.Occurrences = currentOccurences;

                flatNodes.Add(node);

                nodes.Add(node);
            }
        }
Beispiel #2
0
        private byte[] HuffmanCompress(byte[] data)
        {
            for (int i = 0; i < data.Length; i++)
            {
                int         current = data[i];
                HuffmanNode node    = (HuffmanNode)nodes[current];
                WriteBits(node.BitMask, node.BitCount);
            }
            FlushByte();
            stream.WriteByte((byte)bufferBytePos);

            byte[] compressedBytes = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(compressedBytes, 0, compressedBytes.Length);
            return(compressedBytes);
        }
        public int Compare(object x, object y)
        {
            HuffmanNode a = (HuffmanNode)x;
            HuffmanNode b = (HuffmanNode)y;

            if (a.Occurrences == b.Occurrences)
            {
                return(0);
            }

            if (a.Occurrences > b.Occurrences)
            {
                return(1);
            }
            else
            {
                return(-1);
            }
        }
Beispiel #4
0
        private void BuildNodeMask(HuffmanNode node, int lastMask, int bits)
        {
            if (node == null)
            {
                return;
            }

            if (node.Occurrences == 0)
            {
                return;
            }



            node.BitMask = (lastMask << 1) | node.Bit;
            bits++;
            node.BitCount = bits;

            BuildNodeMask(node.LeftNode, node.BitMask, node.BitCount);
            BuildNodeMask(node.RightNode, node.BitMask, node.BitCount);
        }
Beispiel #5
0
        private void BuildNodeTree()
        {
            while (flatNodes.Count > 1)
            {
                SortFlatNodes();

                HuffmanNode a = flatNodes[0] as HuffmanNode;
                HuffmanNode b = flatNodes[1] as HuffmanNode;

                HuffmanNode c = new HuffmanNode();
                c.IsLeaf      = false;
                c.Occurrences = a.Occurrences + b.Occurrences;
                c.LeftNode    = a;
                c.RightNode   = b;
                a.Bit         = 0;
                b.Bit         = 1;
                flatNodes.RemoveAt(0);
                flatNodes.RemoveAt(0);
                flatNodes.Add(c);
            }

            rootNode = flatNodes[0] as HuffmanNode;
        }
        public byte[] Decompress(byte[] bytes)
        {
            nodes.Clear() ;
            for (int i = 0 ; i < 256;i++)
            {
                int occurence = BitConverter.ToInt32(bytes,i*4);
                occurences[i] = occurence;
            }
            nodes = new ArrayList() ;
            flatNodes = new ArrayList() ;
            rootNode = null;
            BuildNodes();
            BuildNodeTree();
            BuildNodeMasks();

            MemoryStream data = new MemoryStream(bytes,256*4,bytes.Length-256*4) ;

            data.Position = data.Length-1;
            int endBufferPos = data.ReadByte() ;

            data.Position = 0;
            MemoryStream result = new MemoryStream();
            int bufferPos = -1;

            int currentByte = 0;
            HuffmanNode currentNode = null;

            while (true)
            {
                while(true)
                {
                    if (bufferPos == -1)
                    {
                        currentByte = data.ReadByte();
                        bufferPos = 7;
                    }

                    int bitmask = 1 << (bufferPos);
                    int currentBit = ((currentByte & bitmask) >> (bufferPos));
                    currentByte = currentByte & 255-bitmask;

                    bufferPos--;

                    if (currentNode == null)
                        currentNode = rootNode;
                    else if (currentBit == 0)
                        currentNode = currentNode.LeftNode;
                    else
                        currentNode = currentNode.RightNode;

                    if (currentNode.IsLeaf)
                    {
                        break;
                    }
                }
                result.WriteByte(currentNode.Data);
                currentNode = null;

                if (endBufferPos == 7)
                    endBufferPos = -1;

                if (data.Position == data.Length-1 && bufferPos == endBufferPos)
                    break;
            }

            byte[] compressedBytes = new byte[result.Length] ;
            result.Position = 0;
            result.Read(compressedBytes,0,compressedBytes.Length);
            return compressedBytes;
        }
        private void BuildNodeTree()
        {
            while (flatNodes.Count > 1)
            {
                SortFlatNodes ();

                HuffmanNode a = flatNodes[0] as HuffmanNode;
                HuffmanNode b = flatNodes[1] as HuffmanNode;

                HuffmanNode c = new HuffmanNode() ;
                c.IsLeaf = false;
                c.Occurrences =  a.Occurrences + b.Occurrences;
                c.LeftNode = a;
                c.RightNode = b;
                a.Bit = 0;
                b.Bit = 1;
                flatNodes.RemoveAt(0) ;
                flatNodes.RemoveAt(0) ;
                flatNodes.Add(c);

            }

            rootNode = flatNodes[0] as HuffmanNode;
        }
        private void BuildNodes()
        {
            for (int i=0;i<=255;i++)
            {
                int currentOccurences = occurences[i];

                HuffmanNode node = new HuffmanNode();
                node.Data = (byte)i;
                node.Occurrences = currentOccurences;

                flatNodes.Add(node);

                nodes.Add(node);
            }
        }
        private void BuildNodeMask(HuffmanNode node,int lastMask,int bits)
        {
            if (node == null)
                return;

            if (node.Occurrences ==0)
                return;

            node.BitMask = (lastMask << 1) | node.Bit;
            bits ++;
            node.BitCount = bits;

            BuildNodeMask (node.LeftNode,node.BitMask,node.BitCount);
            BuildNodeMask (node.RightNode,node.BitMask,node.BitCount);
        }
Beispiel #10
0
        public byte[] Decompress(byte[] bytes)
        {
            nodes.Clear();
            for (int i = 0; i < 256; i++)
            {
                int occurence = BitConverter.ToInt32(bytes, i * 4);
                occurences[i] = occurence;
            }
            nodes     = new ArrayList();
            flatNodes = new ArrayList();
            rootNode  = null;
            BuildNodes();
            BuildNodeTree();
            BuildNodeMasks();


            MemoryStream data = new MemoryStream(bytes, 256 * 4, bytes.Length - 256 * 4);

            data.Position = data.Length - 1;
            int endBufferPos = data.ReadByte();

            data.Position = 0;
            MemoryStream result    = new MemoryStream();
            int          bufferPos = -1;

            int         currentByte = 0;
            HuffmanNode currentNode = null;

            while (true)
            {
                while (true)
                {
                    if (bufferPos == -1)
                    {
                        currentByte = data.ReadByte();
                        bufferPos   = 7;
                    }

                    int bitmask    = 1 << (bufferPos);
                    int currentBit = ((currentByte & bitmask) >> (bufferPos));
                    currentByte = currentByte & 255 - bitmask;

                    bufferPos--;

                    if (currentNode == null)
                    {
                        currentNode = rootNode;
                    }
                    else if (currentBit == 0)
                    {
                        currentNode = currentNode.LeftNode;
                    }
                    else
                    {
                        currentNode = currentNode.RightNode;
                    }

                    if (currentNode.IsLeaf)
                    {
                        break;
                    }
                }
                result.WriteByte(currentNode.Data);
                currentNode = null;

                if (endBufferPos == 7)
                {
                    endBufferPos = -1;
                }

                if (data.Position == data.Length - 1 && bufferPos == endBufferPos)
                {
                    break;
                }
            }

            byte[] compressedBytes = new byte[result.Length];
            result.Position = 0;
            result.Read(compressedBytes, 0, compressedBytes.Length);
            return(compressedBytes);
        }