Esempio n. 1
0
        /// <summary>
        /// Builds a new 8-bit huffmantree. The used algorithm is based on 
        /// http://wiki.multimedia.cx/index.php?title=Smacker#Packed_Huffman_Trees
        /// </summary>
        /// <param name="m">The stream to build the tree from</param>
        public virtual void BuildTree(BitStream m)
        {
            
            //Read tag
            int tag = m.ReadBits(1);
            //If tag is zero, finish
            if (tag == 0) return;

            //Init tree
            rootNode = new Node();
            BuildTree(m, rootNode);
            //For some reason we have to skip a bit
            m.ReadBits(1);

        }
Esempio n. 2
0
 /// <summary>
 /// Decodes a value using this tree based on the next bits in the specified stream
 /// </summary>
 /// <param name="m">The stream to read bits from</param>
 public virtual int Decode(BitStream m)
 {
     Node currentNode = RootNode;
     if (currentNode == null)
         return 0;
     while (!currentNode.isLeaf)
     {
         int bit = m.ReadBits(1);
         if (bit == 0)
         {
             currentNode = currentNode.Left;
         }
         else
         {
             currentNode = currentNode.Right;
         }
     }
     return currentNode.Value;
 }
Esempio n. 3
0
        protected override void BuildTree(BitStream m, Node current)
        {
            //Read flag
            int flag = m.ReadBits(1);
            //If flag is nonzero
            if (flag != 0)
            {
                //Advance to "0"-branch
                Node left = new Node();
                //Recursive call
                BuildTree(m, left);

                //The first left-node is actually the root
                if (current == null)
                {
                    RootNode = left;
                    return;
                }
                else
                    current.Left = left;
            }
            else //If flag is zero
            {
                if (current == null)
                {
                    current = new Node();
                    RootNode = current;
                }
                //Read 16 bit leaf by decoding the low byte, then the high byte
                int lower = lowByteTree.Decode(m);
                int higher = highByteTree.Decode(m);
                int leaf = lower | (higher << 8);
                //System.Console.WriteLine("Decoded: " + leaf);
                //If we found one of the markers, store pointers to those nodes.
                if (leaf == iMarker1)
                {
                    leaf = 0;
                    marker1 = current;
                }
                if (leaf == iMarker2)
                {
                    leaf = 0;
                    marker2 = current;
                }
                if (leaf == iMarker3)
                {
                    leaf = 0;
                    marker3 = current;
                }

                current.IsLeaf = true;
                current.Value = leaf;
                return;
            }

            //Continue on the "1"-branch
            current.Right = new Node();
            BuildTree(m, current.Right);
        }
Esempio n. 4
0
        public override void BuildTree(BitStream m)
        {
            //Read tag
            int tag = m.ReadBits(1);
            //If tag is zero, finish
            if (tag == 0) return;
            lowByteTree = new Huffmantree();
            lowByteTree.BuildTree(m);
 

            highByteTree = new Huffmantree();
            highByteTree.BuildTree(m);

           
            iMarker1 = m.ReadBits(16);
            //System.Console.WriteLine("M1:" + iMarker1);
            iMarker2 = m.ReadBits(16);
            //System.Console.WriteLine("M2:" + iMarker2);
            iMarker3 = m.ReadBits(16);
            //System.Console.WriteLine("M3:" + iMarker3);
            RootNode = new Node();
            BuildTree(m, RootNode);

            //For some reason we have to skip a bit
            m.ReadBits(1);

            if (marker1 == null)
	    {
               // System.Console.WriteLine("Not using marker 1");
                marker1 = new Node();
	    }
            if (marker2 == null)
            {
              //  System.Console.WriteLine("Not using marker 2");
                marker2 = new Node();
            }
            if (marker3 == null)
            {
             //   System.Console.WriteLine("Not using marker 3");
                marker3 = new Node();
	    }

        }
Esempio n. 5
0
        /// <summary>
        /// Decodes a value using this tree based on the next bits in the specified stream
        /// </summary>
        /// <param name="m">The stream to read bits from</param>
        public override int Decode(BitStream m)
        {
            //int v = base.Decode(m);
            Node currentNode = RootNode;
            if (currentNode == null)
                return 0;
            while (!currentNode.isLeaf)
            {
                int bit = m.ReadBits(1);
                if (bit == 0)
                {
                    currentNode = currentNode.Left;
                }
                else
                {
                    currentNode = currentNode.Right;
                }
            }
      
            int v = currentNode.Value;

            if (v != iMarker1)
            {
                iMarker3 = iMarker2;
                iMarker2 = iMarker1;
                iMarker1 = v;
                
                marker3.Value = marker2.Value;                
                marker2.Value = marker1.Value;
                marker1.Value = v;
                
            }
            return v;
        }
Esempio n. 6
0
        protected virtual void BuildTree(BitStream m, Node current)
        {
            //Read flag
            int flag = m.ReadBits(1);
            //If flag is nonzero
            if (flag != 0)
            {
                //Advance to "0"-branch
                Node left = new Node();
                //Recursive call
                BuildTree(m, left);

                //The first left-node is actually the root
                if (current == null)
                {
                    rootNode = left;
                    return;
                }
                else
                    current.Left = left;
            }
            else //If flag is zero
            {
                if (current == null)
                {
                    current = new Node();
                    rootNode = current;
                   
                }
                //Read 8 bit leaf
                int leaf = m.ReadBits(8);
                //Console.WriteLine("Decoded :" + leaf);
                current.IsLeaf = true;
                current.Value = leaf;
                return;
            }

            //Continue on the "1"-branch
            current.Right = new Node();
            BuildTree(m, current.Right);
        }