public void generateCodes(BitStream bs, int index, int depth) { return; //skip this not nessecary for our purposes if (index < 0) { HuffLeaf rLeaf = this.m_huffLeaves[-(index + 1)]; rLeaf.code = BitStream.LEToInt32(bs.dataBuffer); rLeaf.numBits = depth; } else { HuffNode rNode = this.m_huffNodes[index]; int pos = bs.getCurPos(); bs.writeFlag(false); this.generateCodes(bs, rNode.index0, depth + 1); bs.setCurPos(pos); bs.writeFlag(true); this.generateCodes(bs, rNode.index1, depth + 1); bs.setCurPos(pos); } }
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); }
public void set(HuffLeaf in_leaf) { this.pNode = null; this.pLeaf = in_leaf; }
public void set(HuffNode in_node) { this.pLeaf = null; this.pNode = in_node; }
public HuffWrap() { this.pNode = null; this.pLeaf = null; }