-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffmanCode.pas
253 lines (233 loc) · 5.57 KB
/
HuffmanCode.pas
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
unit HuffmanCode;
interface
uses
Windows, Classes, Contnrs;
type
THuffmanTree = array [0..255] of Real;
THuffmanNode = class
public
One, Zero: THuffmanNode;
Weight: Single;
Value: Byte;
Len: Byte;
Bits: DWord;
Parent: THuffmanNode;
destructor Destroy; override;
end;
THuffmanCode = class
private
FLookupNodes: array [0..255] of THuffmanNode;
FRootNode: THuffmanNode;
protected
procedure BuildTree(const Tree: THuffmanTree);
function SetBit(const Bits: Byte; const Index: Byte): Byte; overload;
function SetBit(const Bits: DWord; const Index: Byte): DWord; overload;
public
constructor Create(const Tree: THuffmanTree); reintroduce;
destructor Destroy; override;
function Encode(Source, Dest: Pointer; const SLen, DLen: Integer): Integer;
function Decode(Source, Dest: Pointer; const SLen, DLen: Integer): Integer;
end;
implementation
uses
SysUtils;
function HuffmanCodeSoft(Item1, Item2: Pointer): Integer;
begin
if THuffmanNode(Item1).Weight > THuffmanNode(Item2).Weight then
Result := -1
else
if THuffmanNode(Item1).Weight < THuffmanNode(Item2).Weight then
Result := 1
else
Result := 0;
end;
{ THuffmanCode }
procedure THuffmanCode.BuildTree(const Tree: THuffmanTree);
var
FNodes: TObjectList;
Node: THuffmanNode;
i: Byte;
begin
// Tree
FNodes := TObjectList.Create(False);
try
for i := Low(Tree) to High(Tree) do
begin
Node := THuffmanNode.Create;
FNodes.Add(Node);
Node.One := nil;
Node.Zero := nil;
Node.Parent := nil;
Node.Weight := Tree[i];
Node.Value := i;
FLookupNodes[i] := Node;
end;
while FNodes.Count > 1 do
begin
FNodes.Sort(HuffmanCodeSoft);
Node := THuffmanNode.Create;
Node.One := THuffmanNode(FNodes[FNodes.Count - 1]);
THuffmanNode(FNodes[FNodes.Count - 1]).Parent := Node;
Node.Zero := THuffmanNode(FNodes[FNodes.Count - 2]);
THuffmanNode(FNodes[FNodes.Count - 2]).Parent := Node;
Node.Weight := THuffmanNode(FNodes[FNodes.Count - 1]).Weight + THuffmanNode(FNodes[FNodes.Count - 2]).Weight;
FNodes.Delete(FNodes.Count - 1);
FNodes.Delete(FNodes.Count - 1);
FNodes.Add(Node);
end;
FRootNode := THuffmanNode(FNodes[0]);
finally
FNodes.Free;
end;
// Lookup table
for i := Low(Tree) to High(Tree) do
begin
Node := FLookupNodes[i];
Node.Len := 0;
Node.Bits := 0;
while Assigned(Node.Parent) do
begin
if Node.Parent.One = Node then
FLookupNodes[i].Bits := SetBit(FLookupNodes[i].Bits, FLookupNodes[i].Len);
FLookupNodes[i].Len := FLookupNodes[i].Len + 1;
Node := Node.Parent;
end;
end;
end;
constructor THuffmanCode.Create(const Tree: THuffmanTree);
begin
inherited Create;
BuildTree(Tree);
end;
function THuffmanCode.Decode(Source, Dest: Pointer; const SLen, DLen: Integer): Integer;
var
NumBits: Integer;
BitIndex: Integer;
S: PByte;
k: Integer;
Node: THuffmanNode;
begin
if PByte(Source)^ = $FF then
begin
Result := SLen - 1;
if Assigned(Dest) then
begin
Inc(PByte(Source));
CopyMemory(Dest, Source, SLen - 1);
end;
end
else
begin
Result := 0;
Node := FRootNode;
S := Source;
NumBits := (SLen - 1) * 8 - S^;
Inc(S);
BitIndex := 0;
k := 0;
while BitIndex < NumBits do
begin
while Assigned(Node.One) do
begin
if ((S^ shr k) AND 1) = 1 then
Node := Node.One
else
Node := Node.Zero;
Inc(BitIndex);
Inc(k);
if k >=8 then
begin
k := 0;
Inc(S);
end;
end;
Inc(Result);
if Assigned(Dest) then
begin
PByte(Dest)^ := Node.Value;
Inc(PByte(Dest));
end;
Node := FRootNode;
end;
end;
end;
destructor THuffmanCode.Destroy;
begin
if Assigned(FRootNode) then
FRootNode.Free;
inherited;
end;
function THuffmanCode.Encode(Source, Dest: Pointer; const SLen, DLen: Integer): Integer;
var
i, j: Integer;
BitIndex: Integer;
Bits: DWord;
ByteIndex: Integer;
D: PByte;
FirstByte: PByte;
begin
if not Assigned(Dest) then
begin
Result := 0;
for i := 1 to SLen do
begin
Result := Result + FLookupNodes[PByte(Source)^].Len;
Inc(PByte(Source));
end;
Result := 1 + (Result + 7) div 8;
end
else
if DLen >= SLen + 1 then
begin
FirstByte := Dest;
FirstByte^ := $FF;
Inc(FirstByte);
CopyMemory(FirstByte, Source, SLen);
Result := SLen + 1;
end
else
begin
Result := 0;
BitIndex := 0;
ZeroMemory(Dest, DLen);
FirstByte := Dest;
Inc(PByte(Dest));
for i := 1 to SLen do
begin
Result := Result + FLookupNodes[PByte(Source)^].Len;
Bits := FLookupNodes[PByte(Source)^].Bits;
for j := FLookupNodes[PByte(Source)^].Len - 1 downto 0 do
begin
if ((Bits shr j) AND 1) = 1 then
begin
ByteIndex := BitIndex div 8;
D := Dest;
Inc(D, ByteIndex);
D^ := SetBit(D^, BitIndex mod 8);
end;
Inc(BitIndex);
end;
Inc(PByte(Source));
end;
FirstByte^ := (8 - (BitIndex mod 8)) mod 8;
Result := 1 + (Result + 7) div 8;
end;
end;
function THuffmanCode.SetBit(const Bits, Index: Byte): Byte;
begin
Result := Bits OR (1 shl Index);
end;
function THuffmanCode.SetBit(const Bits: DWord; const Index: Byte): DWord;
begin
Result := Bits OR (1 shl Index);
end;
{ THuffmanNode }
destructor THuffmanNode.Destroy;
begin
if Assigned(One) then
One.Free;
if Assigned(Zero) then
Zero.Free;
inherited;
end;
end.