-
Notifications
You must be signed in to change notification settings - Fork 3
/
lzw.py
284 lines (214 loc) · 8.79 KB
/
lzw.py
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
# Based on JCivED PIC handling code, fixed and optimized
class LZWDictionary:
def __init__(self, dicIndexMaxBits):
self.dicTable = []
self.curPos = 0
self.dicTableLen = (0x1 << dicIndexMaxBits)
# to optimize both directions:
self.dict = {}
self.table = []
for i in range(0, 256):
self.dict[chr(i)] = i
self.table.append([i])
self.table.append([])# 256th item
self.curPos = 0x0101 # 257... For some reason...
def serialize_key(self, entry):
return ''.join(map(chr, entry))
def getIndexOfEntry(self, entry):
entry_s = self.serialize_key(entry)
return self.dict.get(entry_s, -1)
def addEntry(self, entry):
entry_s = self.serialize_key(entry)
if self.curPos < self.dicTableLen:
self.dict[entry_s] = self.curPos
self.table.append(entry)
self.curPos += 1
return self.curPos - 1
def getEntry(self, pos):
if pos < self.curPos:
return self.table[pos]
return None
def getCurPos(self):
return self.curPos
def getSize(self):
return self.dicTableLen
def isFull(self):
return self.getCurPos() >= self.getSize()
def getLastEntry(self):
return self.getEntry(self.getCurPos()-1)
def toString(self, fr, to):
sb = ''
for i in range(fr, to):#self.curPos):
sb += "[" + str(i) + "] => " + str(self.getEntry(i)) + "\n"
return sb
# ENCODING:
def encode(inputData, dicIndexMaxBits = 0x0B):
plainData = inputData
codedData = []
i = 0
plainDataLen = len(plainData)
while i < plainDataLen:
dic = LZWDictionary(dicIndexMaxBits)
buff = []
while i < plainDataLen:
testChunk = list(buff)
testChunk.append(plainData[i])
if dic.getIndexOfEntry(testChunk) != -1:
buff = testChunk
else:
codedData.append(dic.getIndexOfEntry(buff))
if dic.isFull():
break
dic.addEntry(testChunk)
buff = [plainData[i]]
i += 1
if not dic.isFull():
codedData.append(dic.getIndexOfEntry(buff))
i += 1
#print dic.toString(250, 260)
return codedData;
def ints2bytes(lzwIndexes, mode):
output = [] #[0]*(2*len(lzwIndexes.length)) # each input int will take *at most* 2 bytes, so initial size is a sufficient guess
usableBits = 0
usableBitCount = 0
indicatorLength = 1 # /* to increment with ++; rule is that 8+indicatorLength must be <= ubyte_mode, otherwise reset */
#//int indicatorFlag = 0x001; /* to increment with <<=1 followed by |= 1 */
nextThreshold = 0x0100 #/*255*/; /* to increment with <<=1, or *=2 */
codedCounter = 0
dicCounter = 0
remainingIndexesToCode = len(lzwIndexes)
#//while(decodedCounter<256) {
#//for(int i=0;i<imageData.length;i++) {
while remainingIndexesToCode>0:
#/* get enough coded bits to work with */
while usableBitCount<8:
#//usableBits|=(ledis.readUnsignedByte()<<usableBitCount);
usableBits |= (lzwIndexes[codedCounter]<<usableBitCount)
codedCounter += 1
remainingIndexesToCode -= 1
usableBitCount += (8+indicatorLength)
dicCounter+=1
if dicCounter==nextThreshold:
dicCounter = 0
indicatorLength+=1 #/* to increment with ++; rule is that 8+indicatorLength must be <= ubyte_mode, otherwise reset */
#//indicatorFlag<<=1; indicatorFlag|=1;/* to increment with <<=1 followed by |= 1 */
nextThreshold<<=1 #/* to increment with <<=1, or *=2 */
if 8+indicatorLength>mode:
dicCounter = 0
indicatorLength = 1
#//indicatorFlag = 0x001;
nextThreshold = 0x0100 #/*256*/;
#/* decode bytes and indicators */
while usableBitCount>=8:
byteToWrite = usableBits & 0xFF
output.append(byteToWrite)
usableBits >>= 8
usableBitCount -= 8
# Write remnant bits
if usableBitCount>0:
byteToWrite = usableBits & 0xFF
output.append(byteToWrite)
#for x in range(0,26): output.append(0)
return output;
def compress(data, mode=11):
enc_data = encode(data)
#print 'ENC', enc_data[500:800], len(enc_data)
return ints2bytes(enc_data, mode)
# DECODING:
def decode(inputData, dicIndexMaxBits=0x0B):
codedData = inputData
plainData = []
i = 0
codedDataLength = len(codedData)
while i < codedDataLength:
dic = LZWDictionary(dicIndexMaxBits)
w = [codedData[i]]
plainData.append(codedData[i])
while not dic.isFull() and i < codedDataLength-1:
i += 1
k = codedData[i]
entry = []
de = dic.getEntry(k)
#print i, k, de, w
if de is not None:
entry = de
elif k == dic.getCurPos():
entry = w + [w[0]]
else:
print "No dictionary entry in LZW dict !!! (", i, k, de, w ,")"
return plainData
#print 'ent', entry
# Append entry to the result stream (plainData)
plainData += entry
# Add w+entry[0] to the dictionary.
dic.addEntry(w + [entry[0]])
w = entry
#print dic.toString(250, 260)
i += 1
#/* First append plainData to decodedData */
#int ddl = decodedData.length;
#decodedData = Arrays.copyOf(decodedData, decodedData.length + plainDataLen);
#for(int k=0; k<plainDataLen; k++) {
# decodedData[ddl+k] = plainData[k];
#}
#/* Second truncate codedData to keep only undecoded stuff before continuing the loop */
#codedData = Arrays.copyOfRange(codedData, i+1, codedData.length);
return plainData;
def bytes2ints(data, ubyte_mode):
remainingCodedBytes = len(data)
parsedIndexes = []
usableBits = 0
usableBitCount = 0
indicatorLength = 1 # /* to increment with ++; rule is that 8+indicatorLength must be <= ubyte_mode, otherwise reset */
indicatorFlag = 0x001 # /* to increment with <<=1 followed by |= 1 */
nextThreshold = 0x0100 #/*256*/; /* to increment with <<=1, or *=2 */
decodedCounter = 0
#/** Legacy #1 */
Index = 0
#/** Legacy #1 end */
while remainingCodedBytes > 0:
#/* get enough coded bits to work with */
while usableBitCount < 8+indicatorLength:
usableBits|=(data.pop(0)<<usableBitCount)
remainingCodedBytes-=1
usableBitCount += 8
#/* decode bytes and indicators */
while usableBitCount>=8+indicatorLength:
#/** Legacy #2
# ** For us, Index = decodedIndicator<<8 | decodedByte; Or also: Index = usableBits & (indicatorFlag<<8 | 0xFF)
# ** TempIndex = Index; fine...
# ** Testing legacy code
# */
Index = usableBits & ( ( (indicatorFlag<<8) & 0xFF00) | 0x00FF )
#/** Legacy #2 end */
#//int decodedByte = usableBits & 0xFF;
usableBits>>=8
usableBitCount-=8
#//int decodedIndicator = usableBits & indicatorFlag;
usableBits>>=indicatorLength
usableBitCount-=indicatorLength
decodedCounter+=1
if(decodedCounter==nextThreshold):
decodedCounter = 0
indicatorLength+=1 # /* to increment with ++; rule is that 8+indicatorLength must be <= ubyte_mode, otherwise reset */
indicatorFlag<<=1
indicatorFlag|=1 #/* to increment with <<=1 followed by |= 1 */
nextThreshold<<=1 # /* to increment with <<=1, or *=2 */
if 8+indicatorLength>ubyte_mode:
decodedCounter = 0
indicatorLength = 1
indicatorFlag = 0x001
nextThreshold = 0x0100 # /*256*/;
parsedIndexes.append(Index)
#/* Creating an int[] from Indexes */
'''
int[] intIndexes = new int[parsedIndexes.size()];
for(int i=0; i<intIndexes.length; i++) {
intIndexes[i] = parsedIndexes.get(i);
}'''
#print 'DEC', parsedIndexes[500:800], len(parsedIndexes)
return parsedIndexes #intIndexes;
def decompress(data, mode=11):
lzw_data = bytes2ints(data, mode)
#print 'DEC', lzw_data[500:800], len(lzw_data)
return decode(lzw_data)