-
Notifications
You must be signed in to change notification settings - Fork 5
/
lzw.lisp
180 lines (171 loc) · 7.13 KB
/
lzw.lisp
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
(in-package :retrospectiff.compression)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defconstant +clear-code+ 256)
(defconstant +end-of-information-code+ 257)
(defconstant +first-entry+ 258)
(defconstant +last-code+ 4093)
(defconstant +max-code+ 4094))
(defun lzw-encode (raw-vector)
(typecase raw-vector
(string
(lzw-encode
(map 'vector #'char-code raw-vector)))
(vector
(let ((vector-hash (make-hash-table :test 'equalp))
(next-entry)
(bit-count 0)
(output (make-array 256
:element-type '(unsigned-byte 8)
:fill-pointer 0
:adjustable t)))
(labels ((initialize-vector-table ()
(clrhash vector-hash)
(setf next-entry +first-entry+))
(lookup-vector (vector)
(cond ((= (length vector) 1)
(elt vector 0))
((> (length vector) 1)
(gethash vector vector-hash))
(t (error "huh?"))))
(add-vector (vector)
(setf (gethash vector vector-hash) next-entry)
(incf next-entry))
(code-size ()
(integer-length next-entry))
(write-code (int)
(let ((new-fill-pointer
(1+ (ash (+ bit-count (code-size)) -3))))
(ensure-array-size-and-set-fill-pointer
output new-fill-pointer)
(set-bits output bit-count (+ bit-count (code-size))
int))
(incf bit-count (code-size))))
(initialize-vector-table)
(write-code +clear-code+)
(let ((omega))
(loop for k across raw-vector
do
(let ((cat (concatenate 'vector omega (vector k))))
(if (lookup-vector cat)
(setf omega cat)
(progn
(write-code (lookup-vector omega))
(add-vector cat)
(setf omega (vector k))
(when (= next-entry +max-code+)
(write-code +clear-code+)
(initialize-vector-table))))))
(write-code (lookup-vector omega)))
(write-code +end-of-information-code+)
output)))))
(defconstant +initial-code-size+ 9)
;; while ((Code = GetNextCode()) != EoiCode) {
;; if (Code == ClearCode) {
;; InitializeTable();
;; Code = GetNextCode();
;; if (Code == EoiCode)
;; break;
;; WriteString(StringFromCode(Code));
;; OldCode = Code;
;; } /* end of ClearCode case */
;; else {
;; if (IsInTable(Code)) {
;; WriteString(StringFromCode(Code));
;; AddStringToTable(StringFromCode(OldCode) +
;; FirstChar(StringFromCode(Code)));
;; OldCode = Code;
;; } else {
;; OutString = StringFromCode(OldCode) +
;; FirstChar(StringFromCode(OldCode));
;; WriteString(OutString);
;; AddStringToTable(OutString);
;; OldCode = Code;
;; }
;; } /* end of not-ClearCode case */
;; } /* end of while loop */
(defun lzw-decode-codes (compressed-vector)
(let ((bit-offset 0)
(code-size +initial-code-size+)
(next-entry +first-entry+))
(labels ((get-next-code ()
(let ((code (get-bits compressed-vector
bit-offset
(incf bit-offset code-size))))
(cond ((= next-entry (- (expt 2 code-size) 2))
(incf code-size)
code)
((= code +clear-code+)
(setf code-size +initial-code-size+)
(get-next-code))
(t code)))))
(loop for code = (get-next-code)
with old-code
while (not (= code +end-of-information-code+))
when old-code do (incf next-entry)
do (setf old-code code)
collect code))))
(defun lzw-decode (compressed-vector &key stream)
(let ((input-bit-offset 0)
(output-byte-offset 0)
(code-size +initial-code-size+)
(next-entry +first-entry+)
vector-hash
(output (make-array 256
:element-type '(unsigned-byte 8)
:fill-pointer 0
:adjustable t)))
(flet ((initialize-vector-table ()
(setf vector-hash (make-hash-table :test 'equal)
next-entry +first-entry+
code-size +initial-code-size+))
(get-next-code ()
(let ((code (get-bits compressed-vector
input-bit-offset
(incf input-bit-offset code-size))))
code))
(vector-from-code (code)
(if (< code +clear-code+)
(vector code)
(gethash code vector-hash)))
(add-vector-to-table (vector)
(setf (gethash next-entry vector-hash) vector)
(incf next-entry)
(when (= next-entry (- (expt 2 code-size) 1))
(incf code-size)))
(write-code (int)
(ensure-array-size-and-set-fill-pointer
output output-byte-offset)
(setf (aref output output-byte-offset) int)
(setf (fill-pointer output)
(incf output-byte-offset))))
(flet ((write-decoded-vector (vector)
(loop for code across vector
do
(if stream
(write-byte code stream)
(write-code code)))))
(loop for code = (get-next-code)
with old-code
while (not (= code +end-of-information-code+))
do
(cond ((= code +clear-code+)
(initialize-vector-table)
(setf old-code nil))
(t
(let ((string (vector-from-code code)))
(if string
(progn
(write-decoded-vector string)
(when old-code
(add-vector-to-table
(concatenate '(vector (unsigned-byte 8) *)
(vector-from-code old-code)
(subseq string 0 1)))))
(let ((v (concatenate '(vector (unsigned-byte 8) *)
(vector-from-code old-code)
(subseq (vector-from-code old-code)
0 1))))
(write-decoded-vector v)
(add-vector-to-table v))))
(setf old-code code))))))
(unless stream output)))