-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comp_HuffNoGreed.bas
234 lines (228 loc) · 6.5 KB
/
Comp_HuffNoGreed.bas
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
Attribute VB_Name = "Comp_HuffNoGreed"
Option Explicit
'This huffman sheme works with 3 bytes
'the one which is most common
'and 2 wich are least common
'It creates a tree where the most common byte will be coded in 7 bits
'and the 2 least common in 9 bits
'all the other bytes will be coded in 8 bits
Private BitVal() As Long
Private CharVal() As Long
Public Sub Compress_Huffman_Non_Greedy(ByteArray() As Byte)
Dim OutStream() As Byte
Dim OutPos As Long
Dim X As Long
Dim Y As Integer
Dim BitValue(7) As Byte
Dim MostCommon As Long
Dim Least1 As Long
Dim Least2 As Long
Dim Dict As String
Dim TelBits As Integer
Dim Count(255) As Long
Dim ByteValue As Byte
ReDim OutStream(500)
OutPos = 0
For X = 0 To UBound(ByteArray)
Count(ByteArray(X)) = Count(ByteArray(X)) + 1
Next
Dict = "000"
Least1 = 0
Least2 = 0
MostCommon = 0
For X = 0 To 255
Select Case Count(X)
Case 0
'do nothing
Case Is > MostCommon
MostCommon = Count(X)
Mid(Dict, 1, 1) = Chr(X)
If Least1 = 0 Then
Least1 = Count(X)
Least2 = Count(X)
Mid(Dict, 2, 1) = Chr(X)
Mid(Dict, 3, 1) = Chr(X)
End If
Case Is < Least1
If Least1 < Least2 Then
Least2 = Count(X)
Mid(Dict, 3, 1) = Chr(X)
Else
Least1 = Count(X)
Mid(Dict, 2, 1) = Chr(X)
End If
Case Is < Least2
Least2 = Count(X)
Mid(Dict, 3, 1) = Chr(X)
End Select
Next
Call Create_Huffcodes(Dict, True)
For X = 0 To 7
BitValue(X) = 2 ^ X
Next
'send dictionary to output
For X = 1 To 3
Call AddCharToArray(OutStream, OutPos, ASC(Mid(Dict, X, 1)))
Next
TelBits = 7
ByteValue = 0
For X = 0 To UBound(ByteArray)
For Y = CharVal(ByteArray(X)) - 1 To 0 Step -1 'bitlengte
If (BitVal(ByteArray(X)) And 2 ^ Y) > 0 Then
ByteValue = ByteValue + BitValue(TelBits)
End If
TelBits = TelBits - 1
If TelBits = -1 Then
Call AddCharToArray(OutStream, OutPos, ByteValue)
TelBits = 7
ByteValue = 0
End If
Next
Next
If TelBits <> 7 Then
Call AddCharToArray(OutStream, OutPos, ByteValue)
End If
ReDim ByteArray(OutPos - 1)
For X = 0 To OutPos - 1
ByteArray(X) = OutStream(X)
Next
End Sub
Public Sub DeCompress_Huffman_Non_Greedy(ByteArray() As Byte)
Dim OutStream() As Byte
Dim OutPos As Long
Dim InpPos As Long
Dim X As Long
Dim TelBits As Integer
Dim Waarde As Long
Dim TotBits As Integer
Dim Dict As String
ReDim OutStream(500)
OutPos = 0
For X = 1 To 3
Dict = Dict & Chr(ByteArray(InpPos))
InpPos = InpPos + 1
Next
Call Create_Huffcodes(Dict, False)
TelBits = 7
Waarde = 0
TotBits = 0
Do While InpPos <= UBound(ByteArray)
Do
If TelBits = -1 Then
InpPos = InpPos + 1
TelBits = 7
If InpPos > UBound(ByteArray) Then Exit Do
End If
Waarde = Waarde * 2
TotBits = TotBits + 1
If (ByteArray(InpPos) And 2 ^ TelBits) > 0 Then
Waarde = Waarde + 1
End If
TelBits = TelBits - 1
Loop While TotBits < 7
If BitVal(Waarde) = TotBits Then 'gevonden
Call AddCharToArray(OutStream, OutPos, CByte(CharVal(Waarde)))
Waarde = 0
TotBits = 0
End If
Loop
ReDim ByteArray(OutPos - 1)
For X = 0 To OutPos - 1
ByteArray(X) = OutStream(X)
Next
End Sub
'this sub will add a char into the outputstream
Private Sub AddCharToArray(ToArray() As Byte, ToPos As Long, Char As Byte)
If ToPos > UBound(ToArray) Then ReDim Preserve ToArray(ToPos + 500)
ToArray(ToPos) = Char
ToPos = ToPos + 1
End Sub
Private Sub Create_Huffcodes(DictString As String, ForCompress As Boolean)
Dim Code As Long
Dim TotKars As Integer
Dim TotLengs As Integer
Dim ReadPos As Integer
Dim bl_count() As Integer
Dim TreeLang() As Integer
Dim MaxLang As Integer
Dim TreeCode() As Long
Dim next_code() As Long
Dim Chars() As Integer
' Dim Bits As Integer
Dim BitString As String
Dim Bitlen As Integer
Dim Numbits As Integer
Dim MaxBits As Integer
Dim maxcode As Long
Dim OtherChars As String
Dim N As Integer
Dim X As Integer
Dim Y As Integer
Dim Lang As Integer
' MaxBits = ASC(Mid(DictString, 1, 1))
MaxBits = 9
ReDim Preserve bl_count(MaxBits)
ReadPos = 2
MaxLang = -1
For X = 0 To 9
Select Case X
Case 7
Numbits = 1
Case 8
Numbits = 253
Case 9
Numbits = 2
Case Else
Numbits = 0
End Select
If Numbits > 0 Then
Bitlen = X
bl_count(Bitlen) = Numbits
ReDim Preserve TreeLang(MaxLang + Numbits)
For Y = 1 To Numbits
MaxLang = MaxLang + 1
TreeLang(MaxLang) = Bitlen
Next
End If
Next
ReDim TreeCode(MaxLang)
ReDim next_code(MaxBits)
ReDim Chars(MaxLang)
Chars(0) = ASC(Mid(DictString, 1, 1))
Chars(MaxLang - 1) = ASC(Mid(DictString, 2, 1))
Chars(MaxLang) = ASC(Mid(DictString, 3, 1))
ReadPos = 1
For X = 0 To 255
If InStr(DictString, Chr(X)) = 0 Then
Chars(ReadPos) = X
ReadPos = ReadPos + 1
End If
Next
maxcode = 0
Code = 0
For N = 1 To 9
Code = (Code + bl_count(N - 1)) * 2
next_code(N) = Code
Next
For N = 0 To MaxLang
Lang = TreeLang(N)
TreeCode(N) = next_code(Lang)
next_code(Lang) = next_code(Lang) + 1
If maxcode < next_code(Lang) Then maxcode = next_code(Lang)
Next
If ForCompress = True Then
ReDim BitVal(255)
ReDim CharVal(255)
For X = 0 To MaxLang
BitVal(Chars(X)) = TreeCode(X)
CharVal(Chars(X)) = TreeLang(X)
Next
Else
ReDim BitVal(maxcode)
ReDim CharVal(maxcode)
For X = 0 To MaxLang
BitVal(TreeCode(X)) = TreeLang(X)
CharVal(TreeCode(X)) = Chars(X)
Next
End If
End Sub