-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpacker.pas
151 lines (132 loc) · 3.64 KB
/
packer.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
(****************************************************************************
* Modula-2 Software Construction Set *
* (c) 1986 by Leon Frenkel *
****************************************************************************
* Name: Packer.MOD Version: Amiga.00.00 *
* Created: 04/20/87 Updated: 12/25/87 Author: Leon Frenkel *
* Description: This module implements the run compression algorithm *
* "cmpByteRun1"; the same encoding generated by Mac's PackBits. *
* control bytes: *
* [0..127] : followed by n+1 bytes of data. *
* [-1..-127] : followed by byte to be repeated (-n)+1 times. *
* -128 : NOOP. *
****************************************************************************)
Unit Packer;
interface
type
pointertoByte = ^Byte;
function PackRow( pSource, pDest: pointertobyte; rowSize: CARDINAL): LONGINT;
implementation
CONST
DUMP = 0;
RUN = 1;
MinRun = 3;
MaxRun = 128;
MaxDat = 128;
VAR
putSize : CARDINAL;
buf : ARRAY [0..255] OF BYTE;
source : pointertobyte;
dest : pointertobyte;
(* ======== PRIVATE ======= *)
PROCEDURE OutDump(nn: INTEGER);
VAR
i : INTEGER;
BEGIN
dest^ := BYTE(nn - 1); INC(dest); INC(putSize);
i := 0;
WHILE i < nn DO
begin
dest^ := buf[i]; INC(dest); INC(putSize);
INC(i);
END;
END;
PROCEDURE OutRun(nn: INTEGER; cc: BYTE);
BEGIN
dest^ := BYTE(-(nn-1)); INC(dest); INC(putSize);
dest^ := cc; INC(dest); INC(putSize);
END;
(* ======== PUBLIC ======= *)
Function MaxPackedSize(rowSize: CARDINAL): CARDINAL;
BEGIN
MaxPackedSize:= (rowSize + (((rowSize) + 127) DIV 128));
END;
Function PackRow( pSource, pDest: pointertobyte; rowSize: CARDINAL): LONGINT;
VAR
c, lastc : BYTE;
mode : CARDINAL;
nbuf : CARDINAL; (* number of chars in buffer *)
rstart : CARDINAL; (* buffer index current run starts *)
BEGIN
mode := DUMP;
rstart := 0;
source := pSource;
dest := pDest;
putSize := 0;
c := source^; INC(source); (* so have valid lastc *)
lastc := c;
buf[0] := c;
nbuf := 1;
DEC(rowSize); (* since on byte eaten *)
WHILE rowSize <> 0 DO
begin
c := source^; INC(source);
buf[nbuf] := c; INC(nbuf);
IF mode = DUMP THEN
begin
(* If the buffer is full, write the length byte, then the data *)
IF nbuf > MaxDat THEN
begin
OutDump(nbuf-1);
buf[0] := c;
nbuf := 1; rstart := 0;
end
ELSE IF CARDINAL(c) = CARDINAL(lastc) THEN
begin
IF nbuf-rstart >= MinRun THEN
begin
IF rstart > 0 THEN
begin
OutDump(rstart);
END;
mode := RUN;
end
ELSE IF rstart = 0 THEN
begin
(* no dump in progress, so can't lose by making these 2 a run. *)
mode := RUN;
END;
end
ELSE
begin
rstart := nbuf - 1; (* first of run *)
END;
end
ELSE IF mode = RUN THEN
begin
IF (CARDINAL(c) <> CARDINAL(lastc)) OR (nbuf-rstart > MaxRun) THEN
begin
(* output run *)
OutRun(nbuf-1-rstart, lastc);
buf[0] := c;
nbuf := 1; rstart := 0;
mode := DUMP;
END;
END;
lastc := c;
DEC(rowSize);
END;
IF mode = DUMP THEN
begin
OutDump(nbuf);
end
ELSE IF mode = RUN THEN
begin
OutRun(nbuf-rstart, lastc);
END;
pSource := source;
pDest := dest;
PackRow:=putSize;
END;
begin
end.