Class: Resedit::Huffman
- Inherits:
-
Object
- Object
- Resedit::Huffman
- Defined in:
- lib/resedit/text/huffman.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Instance Method Summary collapse
- #balancer ⇒ Object
- #debug ⇒ Object
- #decode(bitstream, endl = 0) ⇒ Object
- #decodeTable(recalc = false) ⇒ Object
- #encode(bytes, endl = 0) ⇒ Object
- #encodeTable(recalc = false) ⇒ Object
-
#initialize(zeroLeft, reverseBitsInByte = false) ⇒ Huffman
constructor
A new instance of Huffman.
- #revbyte(byte) ⇒ Object
Constructor Details
Instance Attribute Details
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
64 65 66 |
# File 'lib/resedit/text/huffman.rb', line 64 def tree @tree end |
Instance Method Details
#balancer ⇒ Object
95 96 97 |
# File 'lib/resedit/text/huffman.rb', line 95 def balancer() return Huffman::Balancer.new(self) end |
#debug ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/resedit/text/huffman.rb', line 99 def debug() tbl=decodeTable() info="---huffman table---\n" bts={} tbl.keys.sort.each{|sz| tbl[sz].each{|k,v| fmt = "0x%02X\t%2d\t%0" + sz.to_s + "b\n" info += sprintf(fmt,v,sz,k) if not bts[v] bts[v] = 1 else bts[v] += 1 end } } info += sprintf("---%d bytes---\n",bts.length) for i in 0..255 if !bts[i] info+=sprintf("0x%02X - ABSENT\n", i) else info+=sprintf("0x%02X - %d times\n", i, bts[i]) if bts[i]!=1 end end App.get().logd(info) end |
#decode(bitstream, endl = 0) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/resedit/text/huffman.rb', line 130 def decode(bitstream, endl=0) res = '' tbl = decodeTable() max = tbl.keys.max pos = 0 byte = bitstream[pos].ord bytelen = 8 buf = 0 buflen = 0 while true buf <<= 1 buflen += 1 raise "Huffman decode length overflow" if buflen>max buf |= byte & 1 byte >>= 1 bytelen -= 1 if tbl[buflen] && tbl[buflen][buf] return res if tbl[buflen][buf] == endl res += tbl[buflen][buf].chr buf = 0 buflen = 0 end next if bytelen>0 pos += 1 break if pos >= bitstream.length bytelen = 8 byte = revbyte(bitstream[pos].ord) end return res end |
#decodeTable(recalc = false) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/resedit/text/huffman.rb', line 71 def decodeTable(recalc=false) if @recalc || !@decodeTbl tbl = @tree.getLeafs(@zeroLeft ? '0' : '1', @zeroLeft ? '1' : '0') @decodeTbl = {} tbl.each{|k,v| @decodeTbl[k.length] = {} if !@decodeTbl[k.length] @decodeTbl[k.length][k.to_i(2)] = v } end return @decodeTbl end |
#encode(bytes, endl = 0) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/resedit/text/huffman.rb', line 162 def encode(bytes, endl=0) if endl != nil && bytes[-1] != endl bytes += endl.chr end res = '' tbl = encodeTable() byte = 0 bytelen = 0 bytes.each_byte{|b| raise sprintf("No byte in encode table: %02X", b) if !tbl[b] val = tbl[b][0] for _ in 0..tbl[b][1]-1 byte |= ((val & 1) << bytelen) val >>= 1 bytelen += 1 if bytelen == 8 res += revbyte(byte).chr bytelen = 0 byte = 0 end end } res += revbyte(byte).chr if bytelen > 0 return res end |
#encodeTable(recalc = false) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/resedit/text/huffman.rb', line 83 def encodeTable(recalc=false) if @recalc || !@encodeTbl tbl = @tree.getLeafs(@zeroLeft ? '0' : '1', @zeroLeft ? '1' : '0') @encodeTbl = {} tbl.each{|k,v| @encodeTbl[v] = [k.reverse.to_i(2), k.length] } end return @encodeTbl end |
#revbyte(byte) ⇒ Object
125 126 127 128 |
# File 'lib/resedit/text/huffman.rb', line 125 def revbyte(byte) return byte if !@reverseBits return sprintf("%08b", byte).reverse.to_i(2) end |