-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhuffman.go
executable file
·207 lines (170 loc) · 4.49 KB
/
huffman.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
124
125
126
127
128
129
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
161
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package nativewebp
import (
//------------------------------
//general
//------------------------------
"container/heap"
"sort"
)
type huffmanCode struct {
Symbol int
Bits int
Depth int
}
type node struct {
IsBranch bool
Weight int
Symbol int
BranchLeft *node
BranchRight *node
}
type nodeHeap []*node
func (h nodeHeap) Len() int { return len(h) }
func (h nodeHeap) Less(i, j int) bool { return h[i].Weight < h[j].Weight }
func (h nodeHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *nodeHeap) Push(x interface{}) { *h = append(*h, x.(*node)) }
func (h *nodeHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func buildHuffmanTree(histo []int, maxDepth int) *node {
sum := 0
for _, x := range histo {
sum += x
}
minWeight := sum >> (maxDepth - 2)
nHeap := &nodeHeap{}
heap.Init(nHeap)
for s, w := range histo {
if w > 0 {
if w < minWeight {
w = minWeight
}
heap.Push(nHeap, &node{
Weight: w,
Symbol: s,
})
}
}
for nHeap.Len() < 1 {
heap.Push(nHeap, &node{
Weight: minWeight,
Symbol: 0,
})
}
for nHeap.Len() > 1 {
n1 := heap.Pop(nHeap).(*node)
n2 := heap.Pop(nHeap).(*node)
heap.Push(nHeap, &node{
IsBranch: true,
Weight: n1.Weight + n2.Weight,
BranchLeft: n1,
BranchRight: n2,
})
}
return heap.Pop(nHeap).(*node)
}
func buildhuffmanCodes(histo []int, maxDepth int) []huffmanCode {
codes := make([]huffmanCode, len(histo))
tree := buildHuffmanTree(histo, maxDepth)
if !tree.IsBranch {
codes[tree.Symbol] = huffmanCode{tree.Symbol, 0, -1}
return codes
}
var symbols []huffmanCode
setBitDepths(tree, &symbols, 0)
sort.Slice(symbols, func(i, j int) bool {
if symbols[i].Depth == symbols[j].Depth {
return symbols[i].Symbol < symbols[j].Symbol
}
return symbols[i].Depth < symbols[j].Depth
})
bits := 0
prevDepth := 0
for _, sym := range symbols {
bits <<= (sym.Depth - prevDepth)
codes[sym.Symbol].Symbol = sym.Symbol
codes[sym.Symbol].Bits = bits
codes[sym.Symbol].Depth = sym.Depth
bits++
prevDepth = sym.Depth
}
return codes
}
func setBitDepths(node *node, codes *[]huffmanCode, level int) {
if node == nil {
return
}
if !node.IsBranch {
*codes = append(*codes, huffmanCode{
Symbol: node.Symbol,
Depth: level,
})
return
}
setBitDepths(node.BranchLeft, codes, level + 1)
setBitDepths(node.BranchRight, codes, level + 1)
}
func writehuffmanCodes(w *bitWriter, codes []huffmanCode) {
var symbols [2]int
cnt := 0
for _, code := range codes {
if code.Depth != 0 {
if cnt < 2 {
symbols[cnt] = code.Symbol
}
cnt++
}
if cnt > 2 {
break
}
}
if cnt == 0 {
w.writeBits(1, 1)
w.writeBits(0, 3)
} else if cnt <= 2 && symbols[0] < 1 << 8 && symbols[1] < 1 << 8 {
w.writeBits(1, 1)
w.writeBits(uint64(cnt - 1), 1)
if symbols[0] <= 1 {
w.writeBits(0, 1)
w.writeBits(uint64(symbols[0]), 1)
} else {
w.writeBits(1, 1)
w.writeBits(uint64(symbols[0]), 8)
}
if cnt > 1 {
w.writeBits(uint64(symbols[1]), 8)
}
} else {
writeFullhuffmanCode(w, codes)
}
}
func writeFullhuffmanCode(w *bitWriter, codes []huffmanCode) {
histo := make([]int, 19)
for _, c := range codes {
histo[c.Depth]++
}
// lengthCodeOrder comes directly from the WebP specs!
var lengthCodeOrder = []int{
17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
}
cnt := 0
for i, c := range lengthCodeOrder {
if histo[c] > 0 {
cnt = max(i + 1, 4)
}
}
w.writeBits(0, 1)
w.writeBits(uint64(cnt - 4), 4)
lengths := buildhuffmanCodes(histo, 7)
for i := 0; i < cnt; i++ {
w.writeBits(uint64(lengths[lengthCodeOrder[i]].Depth), 3)
}
w.writeBits(0, 1)
for _, c := range codes {
w.writeCode(lengths[c.Depth])
}
}