-
Notifications
You must be signed in to change notification settings - Fork 0
/
LZW.PAS
458 lines (405 loc) · 10.6 KB
/
LZW.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
{$I COMPILER.INC}
unit Lzw;
interface
uses
AplTypes,
AplObj,
Compress,
AplConst,
Streams,
AplUtils;
type
PDictEntry = ^TDictEntry;
PLzw = ^TLzw;
PLzwCodes = ^TLzwCodes;
PSetLzwCodesProc = ^TSetLzwCodesProc;
TLzwCompressionLevels = array[TCompressionLevel] of byte;
TDictEntry = record
Code: word;
Prefix: word;
Char: byte;
end;
TLzwCodes = record
EndOfStream: word;
IncreaseCodeSize: word;
ClearDict: word;
EmptyCode: word;
FirstCode: word;
DeferredClear: boolean;
{ If true, the bit size of the data is encoded in the stream. }
{ If false, data is assumed to be 8-bit bytes. }
{ TODO: Implement this for GIF. }
EncodeDataBitSize: boolean;
end;
TSetLzwCodesProc = procedure(var ACodes: TLzwCodes; ABitSize: byte);
TLzw = object(TCompressor)
private
FDict: PDictEntry;
FDictEntries: word;
FDictSize: word;
FDecodeBuffer: PByte;
FCurrentBitSize: byte;
FMaxCode: word;
FCurrentMaxCode: word;
FNextCode: word;
FOverflow: boolean;
FHashShift: byte;
FCodes: TLzwCodes;
function AllocateDictionary: boolean;
function AllocateDecodeBuffer: boolean;
function FindEntry(APrefix: word; AChar: byte): word;
function DecodeString(ACount: word; ACode: word): word;
procedure DeallocateDictionary;
procedure DeallocateDecodeBuffer;
procedure InitCoder;
procedure InitCompression; virtual;
public
BitSize: byte;
SetLzwCodesProc: PSetLzwCodesProc;
constructor CreateBitSize(ABitSize: byte);
constructor CreateCodes(ASetCodesProc: PSetLzwCodesProc);
constructor CreateAll(ABitSize: byte; ASetCodesProc: PSetLzwCodesProc);
destructor Free; virtual;
function CompressStream(ASource, ADest: PStream; ALength: longint): longint; virtual;
function GetOriginalSize(ASource: PStream): longint; virtual;
procedure Init; virtual;
procedure DecompressStream(ASource, ADest: PStream; AOriginalSize: longint); virtual;
procedure SetCompressionLevel(ALevel: TCompressionLevel); virtual;
end;
procedure SetLzwCodes(var ACodes: TLzwCodes; ABitSize: byte); far;
procedure SetGifLzwCodes(var ACodes: TLzwCodes; ABitSize: byte); far;
const
LzwCompressionBitSize: TLzwCompressionLevels = (12, 12, 13);
implementation
procedure SetLzwCodes(var ACodes: TLzwCodes; ABitSize: byte);
begin
ACodes.EndOfStream := 256;
ACodes.IncreaseCodeSize := 257;
ACodes.ClearDict := 258;
ACodes.EmptyCode := 259;
ACodes.FirstCode := 260;
ACodes.DeferredClear := false;
ACodes.EncodeDataBitSize := false;
end;
procedure SetGifLzwCodes(var ACodes: TLzwCodes; ABitSize: byte);
begin
ACodes.ClearDict := 256;
ACodes.EndOfStream := ACodes.ClearDict + 1;
ACodes.FirstCode := ACodes.ClearDict + 2;
ACodes.IncreaseCodeSize := ACodes.ClearDict + 3;
ACodes.EmptyCode := $FFFF;
ACodes.DeferredClear := true;
ACodes.EncodeDataBitSize := true;
end;
const
SupportedBitSizes = [12, 13];
const
DefaultBitSize = 13;
StartBitSize = 9;
constructor TLzw.CreateBitSize(ABitSize: byte);
begin
inherited Create;
if not (ABitSize in [12, 13]) then begin
Raise(ecStreamFormatNotSupported);
exit;
end;
BitSize := ABitSize;
end;
destructor TLzw.Free;
begin
DeallocateDecodeBuffer;
DeallocateDictionary;
inherited Free;
end;
procedure TLzw.Init;
begin
inherited Init;
BitSize := DefaultBitSize;
FMaxCode := 0;
FDict := nil;
FDictEntries := 0;
FDictSize := 0;
FDecodeBuffer := nil;
FOverflow := false;
FNextCode := 0;
FCurrentBitSize := BitSize;
SetLzwCodesProc := nil;
AddTotalToStream := true;
end;
function TLzw.AllocateDecodeBuffer: boolean;
begin
AllocateDecodeBuffer := true;
DeallocateDecodeBuffer;
GetMem(FDecodeBuffer, FDictEntries);
if not Assigned(FDecodeBuffer) then begin
Raise(ecNotEnoughMemory);
AllocateDecodeBuffer := false;
end;
end;
procedure TLzw.DeallocateDecodeBuffer;
begin
if Assigned(FDecodeBuffer) then
FreeMem(FDecodeBuffer, FDictEntries);
FDecodeBuffer := nil;
end;
function TLzw.AllocateDictionary: boolean;
begin
AllocateDictionary := true;
DeallocateDictionary;
GetMem(FDict, FDictSize);
if not Assigned(FDict) then begin
Raise(ecNotEnoughMemory);
AllocateDictionary := false;
end;
end;
procedure TLzw.DeallocateDictionary;
begin
if Assigned(FDict) then
FreeMem(FDict, FDictSize);
FDict := nil;
end;
function TLzw.FindEntry(APrefix: word; AChar: byte): word;
var
index, offset: longint;
entry: PDictEntry;
begin
index := (AChar shl FHashShift) xor APrefix;
offset := 1;
if index <> 0 then
offset := FDictEntries - index;
repeat
entry := FDict;
Inc(entry, index);
if entry^.Code = FCodes.EmptyCode then
break;
if (entry^.Prefix = APrefix) and (entry^.Char = AChar) then
break;
Dec(index, offset);
if index < 0 then
Inc(index, FDictEntries);
until false;
FindEntry := index;
end;
function TLzw.DecodeString(ACount: word; ACode: word): word;
var
bufferPtr: PByte;
entry: PDictEntry;
begin
bufferPtr := FDecodeBuffer;
Inc(bufferPtr, ACount);
while ACode > High(byte) do begin
entry := FDict;
Inc(entry, ACode);
ACode := entry^.Prefix;
bufferPtr^ := entry^.Char;
Inc(ACount);
Inc(bufferPtr);
end;
bufferPtr^ := ACode;
Inc(ACount);
DecodeString := ACount;
end;
constructor TLzw.CreateCodes(ASetCodesProc: PSetLzwCodesProc);
begin
Create;
SetLzwCodesProc := ASetCodesProc;
end;
constructor TLzw.CreateAll(ABitSize: byte; ASetCodesProc: PSetLzwCodesProc);
begin
CreateBitSize(ABitSize);
SetLzwCodesProc := ASetCodesProc;
end;
procedure TLzw.SetCompressionLevel(ALevel: TCompressionLevel);
begin
BitSize := LzwCompressionBitSize[ALevel];
end;
procedure TLzw.InitCompression;
begin
inherited InitCompression;
if Assigned(SetLzwCodesProc) then
TSetLzwCodesProc(SetLzwCodesProc)(FCodes, BitSize)
else
SetLzwCodes(FCodes, BitSize);
if BitSize = 12 then
FDictEntries := 5021
else if BitSize = 13 then
FDictEntries := 9029
else if BitSize = 14 then
FDictEntries := 18041
else if BitSize = 15 then
FDictEntries := 49063;
FHashShift := BitSize - 8;
FDictSize := FDictEntries * SizeOf(TDictEntry);
AllocateDictionary;
InitCoder;
end;
procedure TLzw.InitCoder;
var
index: word;
entry: PDictEntry;
begin
FCurrentBitSize := StartBitSize;
FMaxCode := 1 shl BitSize - 1;
FCurrentMaxCode := 1 shl FCurrentBitSize - 1;
FNextCode := FCodes.FirstCode;
entry := FDict;
for index := 0 to FDictEntries - 1 do begin
entry^.Code := FCodes.EmptyCode;
Inc(entry);
end;
FOverflow := false;
end;
function TLzw.GetOriginalSize(ASource: PStream): longint;
var
bits: byte;
total: longint;
startPos: longint;
begin
GetOriginalSize := 0;
startPos := ASource^.Position;
bits := ASource^.ReadByte;
total := ASource^.ReadLong;
ASource^.Seek(startPos, soFromBeginning);
CheckReraise(ASource);
if HasException then
exit;
GetOriginalSize := total;
end;
function TLzw.CompressStream(ASource, ADest: PStream; ALength: longint): longint;
var
character: byte;
code: word;
index: word;
entry: PDictEntry;
endPos: longint;
written: longint;
destStart: longint;
begin
inherited CompressStream(ASource, ADest, ALength);
destStart := ADest^.Position;
if Source^.Position + ALength > Source^.Size then
Raise(ecReadPastStreamEnd);
if HasException then
exit;
ADest^.WriteByte(BitSize);
code := ReadByte;
if ASource^.HasException then begin
Raise(ADest^.Exception^.Code);
exit;
end;
if AddTotalToStream then
ADest^.WriteLong(ALength);
while ReadTotal < ALength do begin
character:= ReadByte;
if HasException then
exit;
UpdateProgress(ALength, ReadTotal);
index := FindEntry(code, character);
entry := FDict;
Inc(entry, index);
if entry^.Code <> FCodes.EmptyCode then begin
code := Entry^.Code;
continue;
end;
if FNextCode < FMaxCode then begin
entry^.Code := FNextCode;
entry^.Prefix := code;
entry^.Char := character;
Inc(FNextCode);
end
else
FOverflow := true;
if (code >= FCurrentMaxCode) and (FCurrentBitSize < BitSize) then begin
WriteBits(FCodes.IncreaseCodeSize, FCurrentBitSize);
Inc(FCurrentBitSize);
FCurrentMaxCode := 1 shl FCurrentBitSize - 1;
end;
WriteBits(code, FCurrentBitSize);
code := character;
if FOverflow then begin
WriteBits(FCodes.ClearDict, FCurrentBitSize);
InitCoder;
end;
end;
WriteBits(code, FCurrentBitSize);
WriteBits(FCodes.EndOfStream, FCurrentBitSize);
EndWriteBits;
UpdateProgress(ALength, ReadTotal);
CompressStream := ADest^.Position - destStart;
end;
procedure TLzw.DecompressStream(ASource, ADest: PStream; AOriginalSize: longint);
var
oldCode, code: word;
character: word;
count, index: word;
decodePtr: PByte;
entry: PDictEntry;
begin
inherited DecompressStream(ASource, ADest, AOriginalSize);
if HasException then
exit;
BitSize := ASource^.ReadByte;
if not (BitSize in SupportedBitSizes) then begin
Raise(ecStreamFormatNotSupported);
exit;
end;
if AddTotalToStream then
AOriginalSize := Source^.ReadLong;
InitCoder;
AllocateDecodeBuffer;
oldCode := ReadBits(FCurrentBitSize);
if HasException or (oldCode = FCodes.EndOfStream) then
exit;
character := oldCode;
WriteByte(oldCode);
if HasException then
exit;
repeat
code := ReadBits(FCurrentBitSize);
if code = FCodes.IncreaseCodeSize then begin
Inc(FCurrentBitSize);
continue;
end
else if code = FCodes.ClearDict then begin
InitCoder;
oldCode := ReadBits(FCurrentBitSize);
if oldCode = FCodes.EndOfStream then
break;
WriteByte(oldCode);
continue;
end
else if code = FCodes.EndOfStream then
break;
if code >= FNextCode then begin
FDecodeBuffer^ := character;
count := DecodeString(1, oldCode);
end
else
count := DecodeString(0, code);
decodePtr := FDecodeBuffer;
Inc(decodePtr, count - 1);
character := word(decodePtr^);
while count > 0 do begin
WriteByte(decodePtr^);
Dec(decodePtr);
Dec(count);
end;
if HasException then
break;
UpdateProgress(AOriginalSize, WriteTotal);
if FNextCode <= FMaxCode then begin
entry := FDict;
Inc(entry, FNextCode);
entry^.Prefix := oldCode;
entry^.Char := character;
Inc(FNextCode);
if FCodes.DeferredClear and (FNextCode >= FMaxCode) and (FCurrentBitSize < BitSize) then begin
FCurrentBitSize := BitSize;
FMaxCode := FMaxCode shl 1;
end;
end;
oldCode := code;
until false;
FlushWriteBuffer;
end;
end.