-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comp_HuffNoGreed2.bas
221 lines (212 loc) · 6.43 KB
/
Comp_HuffNoGreed2.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
Attribute VB_Name = "Comp_HuffNoGreed2"
Option Explicit
'This huffman sheme works with 3 bitlenght's and an adaptive dictionary
'the dictionary will be updated during the process
'the byte which is most common will be stored at the first position
'the second at the second position etc. etc.
'the first byte (most common) will be stored in 1 bit
'the following 127 positions will be stored in 8 bits
'and the last 128 positions will be stored in 9 bits
Private BitVal() As Long
Private CharVal() As Long
Private CharCount(256) As Long
Private Dictionary As String
Public Sub Compress_Huffman_Non_Greedy2(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 TelBits As Integer
Dim ByteValue As Byte
Dim Char As Byte
Dim DictPos As Integer
ReDim OutStream(500)
OutPos = 0
Call Create_Huffcodes(True)
For X = 0 To 7
BitValue(X) = 2 ^ X
Next
Call AddCharToArray(OutStream, OutPos, Int(UBound(ByteArray) / &H1000000) And &HFF)
Call AddCharToArray(OutStream, OutPos, Int(UBound(ByteArray) / &H10000) And &HFF)
Call AddCharToArray(OutStream, OutPos, Int(UBound(ByteArray) / &H100) And &HFF)
Call AddCharToArray(OutStream, OutPos, Int(UBound(ByteArray) And &HFF))
'send dictionary to output
TelBits = 7
ByteValue = 0
For X = 0 To UBound(ByteArray)
Char = ByteArray(X)
DictPos = InStr(Dictionary, Chr(Char)) - 1
Call update_Model(Char)
For Y = CharVal(DictPos) - 1 To 0 Step -1 'bitlengte
If (BitVal(DictPos) 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_Greedy2(ByteArray() As Byte)
Dim OutStream() As Byte
Dim OutPos As Long
Dim InpPos As Long
Dim X As Long
Dim TelBits As Integer
Dim FileLenght As Long
Dim Waarde As Long
Dim TotBits As Integer
Dim Dict As String
Dim Char As Byte
ReDim OutStream(500)
Call Create_Huffcodes(False)
OutPos = 0
InpPos = 0
For X = 0 To 3
FileLenght = CLng(FileLenght) * 256 + ByteArray(InpPos)
InpPos = InpPos + 1
Next
TelBits = 7
Waarde = 0
TotBits = 0
Do While OutPos <= FileLenght
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
If BitVal(Waarde) = TotBits Then 'gevonden
Char = ASC(Mid(Dictionary, CharVal(Waarde) + 1, 1))
Call AddCharToArray(OutStream, OutPos, Char)
Call update_Model(Char)
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 update_Model(Char As Byte)
Dim DictPos As Integer
Dim OldPos As Integer
Dim Temp As Long
DictPos = InStr(Dictionary, Chr(Char)) - 1
OldPos = DictPos
CharCount(DictPos) = CharCount(DictPos) + 1
Do While DictPos > 0
If CharCount(DictPos) < CharCount(DictPos - 1) Then Exit Do
Temp = CharCount(DictPos - 1)
CharCount(DictPos - 1) = CharCount(DictPos)
CharCount(DictPos) = Temp
DictPos = DictPos - 1
Loop
If OldPos = DictPos Then Exit Sub
Dictionary = Left(Dictionary, DictPos) & Chr(Char) & Mid(Dictionary, DictPos + 1, OldPos - DictPos) & Mid(Dictionary, OldPos + 2)
End Sub
Private Sub Create_Huffcodes(ForCompress As Boolean)
Dim Code As Long
Dim TotKars As Integer
Dim TotLengs 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 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)
MaxLang = -1
For X = 0 To 9
Select Case X
Case 1
Numbits = 1
Case 8
Numbits = 127
Case 9
Numbits = 128
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)
Dictionary = ""
For X = 0 To 255
CharCount(X) = 0
Dictionary = Dictionary & Chr(X)
Chars(X) = X
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