コード例 #1
0
 public int determineIndex(HuffWrap rWrap)
 {
     if (rWrap.pLeaf != null)
     {
         if (rWrap.pNode != null)
         {
             throw new Exception("Got a non-NULL pNode in a HuffWrap with a non-NULL leaf.");
         }
         return(-(Array.IndexOf(this.m_huffLeaves, rWrap.pLeaf) + 1));
     }
     else
     {
         if (rWrap.pNode == null)
         {
             throw new Exception("Got a NULL pNode in a HuffWrap with a NULL leaf.");
         }
         return(Array.IndexOf(this.m_huffNodes, rWrap.pNode));
     }
 }
コード例 #2
0
        public void buildTables()
        {
            if (m_tablesBuilt)
            {
                return;
            }
            m_tablesBuilt      = true;
            this.m_huffLeaves  = new HuffLeaf[256];
            this.m_huffNodes   = new HuffNode[256];
            this.m_huffNodeCnt = 0;
            for (int i = 0; i < 256; i++)
            {
                this.m_huffLeaves[i]         = new HuffLeaf();
                this.m_huffLeaves[i].pop     = this.csm_charFreqs[i] + 1;
                this.m_huffLeaves[i].symbol  = i;
                this.m_huffLeaves[i].code    = 0;
                this.m_huffLeaves[i].numBits = 0;
            }
            int currWraps = 256;

            HuffWrap[] pWrap = new HuffWrap[256];
            for (int i = 0; i < 256; i++)
            {
                pWrap[i] = new HuffWrap();
                pWrap[i].set(this.m_huffLeaves[i]);
            }
            while (currWraps != 1)
            {
                uint min1   = 0xfffffffe;
                uint min2   = 0xffffffff;
                int  index1 = -1;
                int  index2 = -1;
                for (int i = 0; i < currWraps; i++)
                {
                    if (pWrap[i].getPop() < min1)
                    {
                        min2   = min1;
                        index2 = index1;
                        min1   = (uint)pWrap[i].getPop();
                        index1 = i;
                    }
                    else if (pWrap[i].getPop() < min2)
                    {
                        min2   = (uint)pWrap[i].getPop();
                        index2 = i;
                    }
                }
                if (!(index1 != -1 && index2 != -1 && index1 != index2))
                {
                    throw new Exception("hrph");
                }
                HuffNode rNode = new HuffNode();
                this.m_huffNodes[this.m_huffNodeCnt]        = rNode;
                this.m_huffNodes[this.m_huffNodeCnt].pop    = pWrap[index1].getPop() + pWrap[index2].getPop();
                this.m_huffNodes[this.m_huffNodeCnt].index0 = this.determineIndex(pWrap[index1]);
                this.m_huffNodes[this.m_huffNodeCnt].index1 = this.determineIndex(pWrap[index2]);

                this.m_huffNodeCnt++;

                int mergeIndex = (index1 > index2 ? index2 : index1);
                int nukeIndex  = (index1 > index2 ? index1 : index2);
                pWrap[mergeIndex].set(rNode);
                if (index2 != (currWraps - 1))
                {
                    pWrap[nukeIndex] = pWrap[currWraps - 1];
                }
                currWraps--;
            }
            if (currWraps != 1)
            {
                throw new Exception("wrong wraps?");
            }
            if (!(pWrap[0].pNode != null && pWrap[0].pLeaf == null))
            {
                throw new Exception("Wrong wrap type!");
            }
            this.m_huffNodes[0] = pWrap[0].pNode;

            byte[]    code = new byte[6];
            BitStream bs   = new BitStream(code);

            this.generateCodes(bs, 0, 0);
        }