-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
913 lines (799 loc) · 34.7 KB
/
filters.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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
# Original Copyright (c) 2006, Mathieu Fenniak
# Modified by Chris Johnson and others
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Implementation of stream filters for PDF."""
import base64
import math
import struct
import zlib
from io import BytesIO
from .constants import CcittFaxDecodeParameters as CCITT # noqa F401
from .constants import ColorSpaces # noqa F401
from .constants import FilterTypeAbbreviations as FTA
from .constants import FilterTypes as FT
from .constants import ImageAttributes as IA
from .constants import LzwFilterParameters as LZW # noqa F401
from .constants import StreamAttributes as SA
from .errors import PdfReadError
import codecs
HEX_CODEC = "HEX"
HEXSTART = b"<"
HEXEND = b">"
HEXCHARS = b"0123456789ABCDEFabcdef"
WHITESPACE = b" \00\t\n\r\f"
EOF = b""
def decompress(data):
try:
return zlib.decompress(data)
except zlib.error:
d = zlib.decompressobj(zlib.MAX_WBITS | 32)
result_str = b""
for b in [data[i : i + 1] for i in range(len(data))]:
try:
result_str += d.decompress(b)
except zlib.error:
pass
return result_str
def compress(data):
return zlib.compress(data)
class FlateDecode(object):
"""zipped object with optional png pre-compression"""
@staticmethod
def decode(data, decodeParms):
"""
:param data: flate-encoded data.
:param decodeParms: a dictionary of values, understanding the
"/Predictor":<int> key only
:return: the flate-decoded data.
"""
data = decompress(data)
if decodeParms is None:
return data
# apply additional (probably PNG based) decoding
try:
predictor = decodeParms.get("/Predictor", 1)
except AttributeError:
# no predictor - usually an array with a null object was read
predictor = 1
if predictor == 1:
return data
if predictor >= 10 and predictor <= 15:
# PNG prediction. Can vary from row to row and each row
# starts with a one byte indicator followed by n bytes of
# picture elements (not necessarily one for RGB)
columns = decodeParms.get("/Columns")
bpc = decodeParms.get("/BitsPerComponent")
colors = decodeParms.get("/Colors")
try:
rowlength = (
int(bpc * colors * columns / 8) + 1
if bpc is not None
else columns + 1
)
except Exception:
# error here is the result of a badly formed stream or
# previous parser error
print("FlateDecode error in decode parms handling ")
raise
assert len(data) % rowlength == 0
output = BytesIO()
prev_rowdata = bytes(rowlength)
for row in range(len(data) // rowlength):
rowdata = bytearray(data[(row * rowlength) : ((row + 1) * rowlength)])
filterByte = rowdata[0]
if filterByte == 0:
pass
elif filterByte == 1:
for i in range(2, rowlength):
rowdata[i] = (rowdata[i] + rowdata[i - 1]) % 256
elif filterByte == 2:
for i in range(1, rowlength):
rowdata[i] = (rowdata[i] + prev_rowdata[i]) % 256
elif filterByte == 3:
for i in range(1, rowlength):
left = rowdata[i - 1] if i > 1 else 0
floor = math.floor(left + prev_rowdata[i]) / 2
rowdata[i] = (rowdata[i] + int(floor)) % 256
elif filterByte == 4:
for i in range(1, rowlength):
left = rowdata[i - 1] if i > 1 else 0
up = prev_rowdata[i]
up_left = prev_rowdata[i - 1] if i > 1 else 0
paeth = paethPredictor(left, up, up_left)
rowdata[i] = (rowdata[i] + paeth) % 256
else:
# unsupported PNG filter
raise PdfReadError("Unsupported PNG filter %r" % filterByte)
prev_rowdata = rowdata
output.write(rowdata[1:])
data = output.getvalue()
else:
# unsupported predictor
raise PdfReadError("Unsupported flatedecode predictor %r" % predictor)
return data
@staticmethod
def encode(data):
return compress(data)
class ASCIIHexDecode(object):
# rewritten for Python 3 - may possibly work in Py2.7
# see section 7.4.2 and also generic.py
# think that idea was that hex enncoded streams would pass
# unchanged through 7bit email systems
@staticmethod
def decode(data, decodeParms=None):
stream = BytesIO(data)
tok = stream.read(1)
run = b""
while tok not in (HEXEND, EOF):
if tok in WHITESPACE:
pass
elif tok in HEXCHARS:
run += tok
else:
error_message = "Bad character {} in hex stream".format(tok)
raise PdfReadError(error_message)
tok = stream.read(1)
if len(run) % 2 == 1:
run += b"0"
return codecs.decode(run, HEX_CODEC)
@staticmethod
def encode(data):
# import codecs
# HEX_CODEC = "HEX"
return codecs.encode(data, HEX_CODEC)
class LZWDecode(object):
"""Taken from:
http://www.java2s.com/Open-Source/Java-Document/PDF/PDF-Renderer/com/sun/pdfview/decode/LZWDecode.java.htm
"""
class decoder(object):
def __init__(self, data):
self.STOP = 257
self.CLEARDICT = 256
self.data = data
self.bytepos = 0
self.bitpos = 0
self.dict = [bytes([i]) if i < 256 else b"" for i in range(4096)]
self.resetDict()
def resetDict(self):
self.dictlen = 258
self.bitspercode = 9
def nextCode(self):
fillbits = self.bitspercode
value = 0
while fillbits > 0:
if self.bytepos >= len(self.data):
return -1
nextbits = self.data[self.bytepos]
bitsfromhere = 8 - self.bitpos
if bitsfromhere > fillbits:
bitsfromhere = fillbits
value |= (
(nextbits >> (8 - self.bitpos - bitsfromhere))
& (0xFF >> (8 - bitsfromhere))
) << (fillbits - bitsfromhere)
fillbits -= bitsfromhere
self.bitpos += bitsfromhere
if self.bitpos >= 8:
self.bitpos = 0
self.bytepos = self.bytepos + 1
return value
def decode(self):
"""
TIFF 6.0 specification explains in sufficient details the steps to
implement the LZW encode() and decode() algorithms.
http://www.rasip.fer.hr/research/compress/algorithms/fund/lz/lzw.html
and the PDFReference
:rtype: bytes
"""
cW = self.CLEARDICT
baos = b"" # output is bytestring for Python 3
while True:
pW = cW
cW = self.nextCode()
if cW == -1:
raise PdfReadError("Missed the stop code in LZWDecode!")
if cW == self.STOP:
break
elif cW == self.CLEARDICT:
self.resetDict()
elif pW == self.CLEARDICT:
baos += self.dict[cW]
else:
if cW < self.dictlen:
baos += self.dict[cW]
p = self.dict[pW] + self.dict[cW][:1]
self.dict[self.dictlen] = p
self.dictlen += 1
else:
p = self.dict[pW] + self.dict[pW][:1]
baos += p
self.dict[self.dictlen] = p
self.dictlen += 1
if (
self.dictlen >= (1 << self.bitspercode) - 1
and self.bitspercode < 12
):
self.bitspercode += 1
return baos
@staticmethod
def decode(data, decodeParms=None):
"""
:param data: ``bytes`` or ``str`` text to decode.
:param decodeParms: a dictionary of parameter values.
:return: decoded data.
:rtype: bytes
"""
return LZWDecode.decoder(data).decode()
class ASCII85Decode(object):
"""
ASCII85 stream encoding / decoding. Python 3 only
the @staticmethod gives an issue with python < 3.10
"""
def decode(data, decodeParms=None):
assert isinstance(data, bytes)
if data[-2:] == b"~>":
data = data[:-2]
return base64.a85decode(data)
decode = staticmethod(decode)
def encode(data):
assert isinstance(data, bytes)
return base64.a85encode(data)
encode = staticmethod(encode)
class DCTDecode(object):
@staticmethod
def decode(data, decodeParms=None):
return data
class JPXDecode(object):
@staticmethod
def decode(data, decodeParms=None):
return data
class CCITTFaxDecode(object):
@staticmethod
def decode(data, decodeParms=None, height=0):
if decodeParms:
if decodeParms.get("/K", 1) == -1:
CCITTgroup = 4
else:
CCITTgroup = 3
width = decodeParms["/Columns"]
imgSize = len(data)
tiff_header_struct = "<" + "2s" + "h" + "l" + "h" + "hhll" * 8 + "h"
tiffHeader = struct.pack(
tiff_header_struct,
b"II", # Byte order indication: Little endian
42, # Version number (always 42)
8, # Offset to first IFD
8, # Number of tags in IFD
256,
4,
1,
width, # ImageWidth, LONG, 1, width
257,
4,
1,
height, # ImageLength, LONG, 1, length
258,
3,
1,
1, # BitsPerSample, SHORT, 1, 1
259,
3,
1,
CCITTgroup, # Compression, SHORT, 1, 4 = CCITT Group 4 fax encoding
262,
3,
1,
0, # Thresholding, SHORT, 1, 0 = WhiteIsZero
273,
4,
1,
struct.calcsize(
tiff_header_struct
), # StripOffsets, LONG, 1, length of header
278,
4,
1,
height, # RowsPerStrip, LONG, 1, length
279,
4,
1,
imgSize, # StripByteCounts, LONG, 1, size of image
0, # last IFD
)
return tiffHeader + data
def decodeStreamData(stream):
from .generic import NameObject
filters = stream.get(SA.FILTER, ())
if len(filters) and not isinstance(filters[0], NameObject):
# we have a single filter instance
filters = (filters,)
data = stream._data
# If there is not data to decode we should not try to decode the data.
if data:
for filterType in filters:
if filterType == FT.FLATE_DECODE or filterType == FTA.FL:
data = FlateDecode.decode(data, stream.get(SA.DECODE_PARMS))
elif filterType == FT.ASCII_HEX_DECODE or filterType == FTA.AHx:
data = ASCIIHexDecode.decode(data)
elif filterType == FT.LZW_DECODE or filterType == FTA.LZW:
data = LZWDecode.decode(data, stream.get(SA.DECODE_PARMS))
elif filterType == FT.ASCII_85_DECODE or filterType == FTA.A85:
data = ASCII85Decode.decode(data)
elif filterType == FT.DCT_DECODE:
data = DCTDecode.decode(data)
elif filterType == "/JPXDecode":
data = JPXDecode.decode(data)
elif filterType == FT.CCITT_FAX_DECODE:
height = stream.get(IA.HEIGHT, ())
data = CCITTFaxDecode.decode(data, stream.get(SA.DECODE_PARMS), height)
elif filterType == "/Crypt":
decodeParms = stream.get(SA.DECODE_PARMS, {})
if "/Name" not in decodeParms and "/Type" not in decodeParms:
pass
else:
raise NotImplementedError(
"/Crypt filter with /Name or /Type not supported yet"
)
else:
# unsupported filter
raise NotImplementedError("unsupported filter %s" % filterType)
return data
# change the transitions to character string
#
class Fax(object):
# class constants # type(self).tableV
tableV = [(None, None, None) for i in range(8192)]
tableW = [(None, None, None) for i in range(8192)]
tableB = [(None, None, None) for i in range(8192)]
modeU = "U" # uncompressed
modeV = "V" # 2D mode
modeBlack = "B"
modeWhite = "W"
modeFinished = "Z"
def initialize(self, cols, rows, stripsize):
self.Result = b""
self.linect = 0
self.bitpos = 0
self.bytepos = 0
# bitmap lines and columns
self.Columns = cols
self.Rows = rows
self.K = stripsize
# current and reference line transitions (positions of each w,b,w,b run
self.Result = b""
self.currentPos = 0
self.cT = [None for j in range(self.Columns + 1)]
self.cTP = 0
self.cT[0] = 0
self.rT = [None for i in range(self.Columns)]
self.rT[0] = 0
self.rTP = 0 # spec suggests that this should be -1 but I think first white run starts with first pel
self.currentMode = None
self.startingMode = None
def decode(self, stream, DecodeParms):
print("fax decode:stream length {} parms {}".format(len(stream), DecodeParms))
self.initialize(
DecodeParms["/Columns"], DecodeParms["/Rows"], DecodeParms["/K"]
)
if self.K >= 0:
print("Cannot handle G3 compression yet")
return
self.Process(stream)
return self.Result
def __init__(self):
# print("Initializing instance and class variables" )
self.currentMode = None
self.startingMode = None
self.Result = b""
self.bitpos = 0
self.bytepos = 0
self.LoadTables(type(self).tableV, type(self).tableB, type(self).tableW)
def getbits(self, data, fillbits):
value = 0
while fillbits > 0:
nextbits = data[self.bytepos]
bitsfromhere = 8 - self.bitpos
if bitsfromhere > fillbits:
bitsfromhere = fillbits
value |= (
(nextbits >> (8 - self.bitpos - bitsfromhere))
& (0xFF >> (8 - bitsfromhere))
) << (fillbits - bitsfromhere)
fillbits -= bitsfromhere
self.bitpos += bitsfromhere
if self.bitpos >= 8:
self.bitpos = 0
self.bytepos = self.bytepos + 1
return value
def flush_line(self):
pixW = b"\xff\xff\xff"
pixB = b"\x00\x00\x00"
Ssave = self.cT[self.cTP]
self.cT[self.cTP] = self.Columns
linedata = b"".join(
[
[pixB, pixW][i % 2] * (self.cT[i] - self.cT[i - 1])
for i in range(1, self.cTP + 1)
]
)
self.Result += linedata
self.linect += 1
self.cT[self.cTP] = Ssave
self.rT = list(self.cT)
for i in range(self.cTP, len(self.cT)):
self.rT[i] = None
self.rTP = 0
self.cTP = 0
self.currentPos = 0
def switch_uncompressed(self, p):
print("uncompressed mode", end=" ")
pass
def copy_down(self, p):
while True:
if self.rT[self.rTP] is None:
break
if self.rT[self.rTP] >= self.currentPos:
break
self.rTP += 2
if self.rT[self.rTP] is None:
self.flush_line()
print("v{}".format(p), end=" ")
self.cT[self.cTP] = self.rT[self.rTP] + p
self.currentPos = self.cT[self.cTP]
self.cTP += 1
self.rTP += 1
def do_pass(self, p):
self.rTP += 2
self.currentPos = self.rT[self.rTP]
def switch_horizontal(self, p):
# Horizontal mode consists of two runs
# white and black or black and white
# they may extend beyond existing transitions on the reference line
# H next mode black white
# white end black
# black white end
self.currentPos = self.currentPos + 0
self.startingMode = [self.modeBlack, self.modeWhite][self.cTP % 2]
self.currentMode = self.startingMode
def rle_black(self, p):
self.rle(p)
def rle_white(self, p):
self.rle(p)
def rle(self, p):
self.currentPos += p
if p > 63:
return
if self.currentPos >= self.Columns:
tPos = self.currentPos - self.Columns
self.flush_line()
self.currentPos = tPos
self.cTP = 0
if self.rT[self.rTP] <= self.cT[self.cTP]:
self.rTP += 1
else:
pass
self.cT[self.cTP] = self.currentPos
self.cTP += 1
if self.currentMode != self.startingMode:
self.currentMode = self.rle_end
elif self.currentMode == self.modeBlack:
self.currentMode = self.modeWhite
elif self.currentMode == self.modeWhite:
self.currentMode = self.modeBlack
else:
raise PdfReadError("Fax decode mode error")
def rle_end(self, p):
assert self.currentPos < self.Columns
while True:
if self.rT[self.rTP] is None:
break
if self.rT[self.rTP] >= self.currentPos:
break
self.rTP += 2
self.currentMode = self.modeV
def pterm(self, p):
self.flush_line()
# print( 'Fax {} bytes'.format( len(self.Result)) )
# print( 'that is {} lines'.format( len(self.Result) / 3 / self.Columns ) )
# print( 'count of lines as we went along ' , self.linect )
self.currentMode = self.modeFinished
def AddEntry(self, tableX, bits, op, opa):
bita = bits + "0000000000000"
bitz = bits + "1111111111111"
valmin = 0
valmax = 0
for i in range(13):
valmin = valmin * 2 + (bita[i] == "1")
valmax = valmax * 2 + (bitz[i] == "1")
for t in range(valmin, valmax + 1):
tableX[t] = (len(bits), op, opa)
def LoadTables(self, tableV, tableB, tableW):
AddEntry = self.AddEntry
AddEntry(tableV, "0000001111", self.switch_uncompressed, 0)
AddEntry(tableV, "0000010", self.copy_down, -3)
AddEntry(tableV, "0000011", self.copy_down, +3)
AddEntry(tableV, "000010", self.copy_down, -2)
AddEntry(tableV, "000011", self.copy_down, 2)
AddEntry(tableV, "0001", self.do_pass, 0) # Pass
AddEntry(tableV, "010", self.copy_down, -1) # a1 to the left of b1 by 1
AddEntry(tableV, "011", self.copy_down, +1) # a1 to the right of b1 by 1
AddEntry(tableV, "1", self.copy_down, 0) # a1 under b1 a1b1=0
AddEntry(
tableV, "001", self.switch_horizontal, 0
) # horrizontal e.g run length doding for #White and then black
AddEntry(tableW, "00000010", self.rle_white, 29)
AddEntry(tableW, "00000011", self.rle_white, 30)
AddEntry(tableW, "00000100", self.rle_white, 45)
AddEntry(tableW, "00000101", self.rle_white, 46)
AddEntry(tableW, "0000011", self.rle_white, 22)
AddEntry(tableW, "0000100", self.rle_white, 23)
AddEntry(tableW, "00001010", self.rle_white, 47)
AddEntry(tableW, "00001011", self.rle_white, 48)
AddEntry(tableW, "000011", self.rle_white, 13)
AddEntry(tableW, "0001000", self.rle_white, 20)
AddEntry(tableW, "00010010", self.rle_white, 33)
AddEntry(tableW, "00010011", self.rle_white, 34)
AddEntry(tableW, "00010100", self.rle_white, 35)
AddEntry(tableW, "00010101", self.rle_white, 36)
AddEntry(tableW, "00010110", self.rle_white, 37)
AddEntry(tableW, "00010111", self.rle_white, 38)
AddEntry(tableW, "0001100", self.rle_white, 19)
AddEntry(tableW, "00011010", self.rle_white, 31)
AddEntry(tableW, "00011011", self.rle_white, 32)
AddEntry(tableW, "000111", self.rle_white, 1)
AddEntry(tableW, "001000", self.rle_white, 12)
AddEntry(tableW, "00100100", self.rle_white, 53)
AddEntry(tableW, "00100101", self.rle_white, 54)
AddEntry(tableW, "0010011", self.rle_white, 26)
AddEntry(tableW, "00101000", self.rle_white, 39)
AddEntry(tableW, "00101001", self.rle_white, 40)
AddEntry(tableW, "00101010", self.rle_white, 41)
AddEntry(tableW, "00101011", self.rle_white, 42)
AddEntry(tableW, "00101100", self.rle_white, 43)
AddEntry(tableW, "00101101", self.rle_white, 44)
AddEntry(tableW, "0010111", self.rle_white, 21)
AddEntry(tableW, "0011000", self.rle_white, 28)
AddEntry(tableW, "00110010", self.rle_white, 61)
AddEntry(tableW, "00110011", self.rle_white, 62)
AddEntry(tableW, "00110100", self.rle_white, 63)
AddEntry(tableW, "00110101", self.rle_white, 00)
AddEntry(tableW, "00111", self.rle_white, 10)
AddEntry(tableW, "01000", self.rle_white, 11)
AddEntry(tableW, "0100100", self.rle_white, 27)
AddEntry(tableW, "01001010", self.rle_white, 59)
AddEntry(tableW, "01001011", self.rle_white, 60)
AddEntry(tableW, "0100111", self.rle_white, 18)
AddEntry(tableW, "0101000", self.rle_white, 24)
AddEntry(tableW, "01010010", self.rle_white, 49)
AddEntry(tableW, "01010011", self.rle_white, 50)
AddEntry(tableW, "01010100", self.rle_white, 51)
AddEntry(tableW, "01010101", self.rle_white, 52)
AddEntry(tableW, "0101011", self.rle_white, 25)
AddEntry(tableW, "01011000", self.rle_white, 55)
AddEntry(tableW, "01011001", self.rle_white, 56)
AddEntry(tableW, "01011010", self.rle_white, 57)
AddEntry(tableW, "01011011", self.rle_white, 58)
AddEntry(tableW, "0111", self.rle_white, 2)
AddEntry(tableW, "1000", self.rle_white, 3)
AddEntry(tableW, "10011", self.rle_white, 8)
AddEntry(tableW, "10100", self.rle_white, 9)
AddEntry(tableW, "101010", self.rle_white, 16)
AddEntry(tableW, "101011", self.rle_white, 17)
AddEntry(tableW, "1011", self.rle_white, 4)
AddEntry(tableW, "1100", self.rle_white, 5)
AddEntry(tableW, "110100", self.rle_white, 14)
AddEntry(tableW, "110101", self.rle_white, 15)
AddEntry(tableW, "1110", self.rle_white, 6)
AddEntry(tableW, "1111", self.rle_white, 7)
#
AddEntry(tableW, "11011", self.rle_white, 64)
AddEntry(tableW, "10010", self.rle_white, 128)
AddEntry(tableW, "010111", self.rle_white, 192)
AddEntry(tableW, "0110111", self.rle_white, 256)
AddEntry(tableW, "00110110", self.rle_white, 320)
AddEntry(tableW, "00110111", self.rle_white, 384)
AddEntry(tableW, "01100100", self.rle_white, 448)
AddEntry(tableW, "01100101", self.rle_white, 512)
AddEntry(tableW, "01101000", self.rle_white, 576)
AddEntry(tableW, "01100111", self.rle_white, 640)
AddEntry(tableW, "011001100", self.rle_white, 704)
AddEntry(tableW, "011001101", self.rle_white, 768)
AddEntry(tableW, "011010010", self.rle_white, 832)
AddEntry(tableW, "011010011", self.rle_white, 896)
AddEntry(tableW, "011010100", self.rle_white, 960)
AddEntry(tableW, "011010101", self.rle_white, 1024)
AddEntry(tableW, "011010110", self.rle_white, 1088)
AddEntry(tableW, "011010111", self.rle_white, 1152)
AddEntry(tableW, "011011000", self.rle_white, 1216)
AddEntry(tableW, "011011001", self.rle_white, 1280)
AddEntry(tableW, "011011010", self.rle_white, 1344)
AddEntry(tableW, "011011011", self.rle_white, 1408)
AddEntry(tableW, "010011000", self.rle_white, 1472)
AddEntry(tableW, "010011001", self.rle_white, 1536)
AddEntry(tableW, "010011010", self.rle_white, 1600)
AddEntry(tableW, "011000", self.rle_white, 1664)
AddEntry(tableW, "010011011", self.rle_white, 1728)
AddEntry(tableW, "00000001000", self.rle_white, 1792)
AddEntry(tableW, "00000001100", self.rle_white, 1856)
AddEntry(tableW, "00000001101", self.rle_white, 1920)
AddEntry(tableW, "000000010010", self.rle_white, 1984)
AddEntry(tableW, "000000010011", self.rle_white, 2048)
AddEntry(tableW, "000000010100", self.rle_white, 2112)
AddEntry(tableW, "000000010101", self.rle_white, 2176)
AddEntry(tableW, "000000010110", self.rle_white, 2240)
AddEntry(tableW, "000000010111", self.rle_white, 2304)
AddEntry(tableW, "000000011100", self.rle_white, 2368)
AddEntry(tableW, "000000011101", self.rle_white, 2432)
AddEntry(tableW, "000000011110", self.rle_white, 2496)
AddEntry(tableW, "000000011111", self.rle_white, 2560)
AddEntry(tableB, "0000001000", self.rle_black, 18)
AddEntry(tableB, "000000100100", self.rle_black, 52)
AddEntry(tableB, "000000100111", self.rle_black, 55)
AddEntry(tableB, "000000101000", self.rle_black, 56)
AddEntry(tableB, "000000101011", self.rle_black, 59)
AddEntry(tableB, "000000101100", self.rle_black, 60)
AddEntry(tableB, "00000010111", self.rle_black, 24)
AddEntry(tableB, "00000011000", self.rle_black, 25)
AddEntry(tableB, "000000110111", self.rle_black, 53)
AddEntry(tableB, "000000111000", self.rle_black, 54)
AddEntry(tableB, "00000100", self.rle_black, 13)
AddEntry(tableB, "00000101000", self.rle_black, 23)
AddEntry(tableB, "000001010010", self.rle_black, 50)
AddEntry(tableB, "000001010011", self.rle_black, 51)
AddEntry(tableB, "000001010100", self.rle_black, 44)
AddEntry(tableB, "000001010101", self.rle_black, 45)
AddEntry(tableB, "000001010110", self.rle_black, 46)
AddEntry(tableB, "000001010111", self.rle_black, 47)
AddEntry(tableB, "000001011000", self.rle_black, 57)
AddEntry(tableB, "000001011001", self.rle_black, 58)
AddEntry(tableB, "000001011010", self.rle_black, 61)
AddEntry(tableB, "0000010111", self.rle_black, 16)
AddEntry(tableB, "0000011000", self.rle_black, 17)
AddEntry(tableB, "000001100100", self.rle_black, 48)
AddEntry(tableB, "000001100101", self.rle_black, 49)
AddEntry(tableB, "000001100110", self.rle_black, 62)
AddEntry(tableB, "000001100111", self.rle_black, 63)
AddEntry(tableB, "000001101000", self.rle_black, 30)
AddEntry(tableB, "000001101001", self.rle_black, 31)
AddEntry(tableB, "000001101010", self.rle_black, 32)
AddEntry(tableB, "000001101011", self.rle_black, 33)
AddEntry(tableB, "000001101100", self.rle_black, 40)
AddEntry(tableB, "000001101101", self.rle_black, 41)
AddEntry(tableB, "00000110111", self.rle_black, 22)
AddEntry(tableB, "00000111", self.rle_black, 14)
AddEntry(tableB, "0000100", self.rle_black, 10)
AddEntry(tableB, "0000101", self.rle_black, 11)
AddEntry(tableB, "000011000", self.rle_black, 15)
AddEntry(tableB, "000011001010", self.rle_black, 26)
AddEntry(tableB, "000011001011", self.rle_black, 27)
AddEntry(tableB, "000011001100", self.rle_black, 28)
AddEntry(tableB, "000011001101", self.rle_black, 29)
AddEntry(tableB, "00001100111", self.rle_black, 19)
AddEntry(tableB, "00001101000", self.rle_black, 20)
AddEntry(tableB, "000011010010", self.rle_black, 34)
AddEntry(tableB, "000011010011", self.rle_black, 35)
AddEntry(tableB, "000011010100", self.rle_black, 36)
AddEntry(tableB, "000011010101", self.rle_black, 37)
AddEntry(tableB, "000011010110", self.rle_black, 38)
AddEntry(tableB, "000011010111", self.rle_black, 39)
AddEntry(tableB, "00001101100", self.rle_black, 21)
AddEntry(tableB, "000011011010", self.rle_black, 42)
AddEntry(tableB, "000011011011", self.rle_black, 43)
AddEntry(tableB, "0000110111", self.rle_black, 00)
AddEntry(tableB, "0000111", self.rle_black, 12)
AddEntry(tableB, "000100", self.rle_black, 9)
AddEntry(tableB, "000101", self.rle_black, 8)
AddEntry(tableB, "00011", self.rle_black, 7)
AddEntry(tableB, "0010", self.rle_black, 6)
AddEntry(tableB, "0011", self.rle_black, 5)
AddEntry(tableB, "010", self.rle_black, 1)
AddEntry(tableB, "011", self.rle_black, 4)
AddEntry(tableB, "10", self.rle_black, 3)
AddEntry(tableB, "11", self.rle_black, 2)
AddEntry(tableB, "000011001000", self.rle_black, 128)
AddEntry(tableB, "000011001001", self.rle_black, 192)
AddEntry(tableB, "000001011011", self.rle_black, 256)
AddEntry(tableB, "000000110011", self.rle_black, 320)
AddEntry(tableB, "000000110100", self.rle_black, 384)
AddEntry(tableB, "000000110101", self.rle_black, 448)
AddEntry(tableB, "0000001101100", self.rle_black, 512)
AddEntry(tableB, "0000001101101", self.rle_black, 576)
AddEntry(tableB, "0000001001010", self.rle_black, 640)
AddEntry(tableB, "0000001001011", self.rle_black, 704)
AddEntry(tableB, "0000001001100", self.rle_black, 768)
AddEntry(tableB, "0000001001101", self.rle_black, 832)
AddEntry(tableB, "0000001110010", self.rle_black, 896)
AddEntry(tableB, "0000001110011", self.rle_black, 960)
AddEntry(tableB, "0000001110100", self.rle_black, 1024)
AddEntry(tableB, "0000001110101", self.rle_black, 1088)
AddEntry(tableB, "0000001110110", self.rle_black, 1152)
AddEntry(tableB, "0000001110111", self.rle_black, 1216)
AddEntry(tableB, "0000001010010", self.rle_black, 1280)
AddEntry(tableB, "0000001010011", self.rle_black, 1344)
AddEntry(tableB, "0000001010100", self.rle_black, 1408)
AddEntry(tableB, "0000001010101", self.rle_black, 1472)
AddEntry(tableB, "0000001011010", self.rle_black, 1536)
AddEntry(tableB, "0000001011011", self.rle_black, 1600)
AddEntry(tableB, "0000001100100", self.rle_black, 1664)
AddEntry(tableB, "0000001100101", self.rle_black, 1728)
AddEntry(tableB, "00000001000", self.rle_black, 1792)
AddEntry(tableB, "00000001100", self.rle_black, 1856)
AddEntry(tableB, "00000001101", self.rle_black, 1920)
AddEntry(tableB, "000000010010", self.rle_black, 1984)
AddEntry(tableB, "000000010011", self.rle_black, 2048)
AddEntry(tableB, "000000010100", self.rle_black, 2112)
AddEntry(tableB, "000000010101", self.rle_black, 2176)
AddEntry(tableB, "000000010110", self.rle_black, 2240)
AddEntry(tableB, "000000010111", self.rle_black, 2304)
AddEntry(tableB, "000000011100", self.rle_black, 2368)
AddEntry(tableB, "000000011101", self.rle_black, 2432)
AddEntry(tableB, "000000011110", self.rle_black, 2496)
AddEntry(tableB, "000000011111", self.rle_black, 2560)
AddEntry(tableV, "0000000000010", self.pterm, 0)
AddEntry(tableW, "0000000000010", self.pterm, 0)
AddEntry(tableB, "0000000000010", self.pterm, 0)
def Process(self, stream):
self.currentMode = self.modeV
nextbits = self.getbits(stream, 13)
while self.bytepos < len(stream):
if self.currentMode == self.modeFinished:
print("CCITT decode finished")
print(
"Data len {} = {} rows ".format(
len(self.Result), len(self.Result) / 3 / self.Columns
)
)
return self.Result
if self.currentMode == self.modeU:
print("cannot handle uncompresssed mode")
if self.currentMode == self.modeV:
(code_len, operator, operand) = self.tableV[nextbits]
elif self.currentMode == self.modeWhite:
(code_len, operator, operand) = self.tableW[nextbits]
elif self.currentMode == self.modeBlack:
(code_len, operator, operand) = self.tableB[nextbits]
if operator is None:
print("None!!!")
else:
operator(operand)
if self.currentMode == self.rle_end:
self.rle_end(1)
nextbits = nextbits << (code_len)
nextbits = nextbits & 0x1FFF
nextbits = nextbits + self.getbits(stream, code_len)
return self.Result
def paethPredictor(left, up, up_left):
p = left + up - up_left
dist_left = abs(p - left)
dist_up = abs(p - up)
dist_up_left = abs(p - up_left)
if dist_left <= dist_up and dist_left <= dist_up_left:
return left
elif dist_up <= dist_up_left:
return up
else:
return up_left