pdfreader 0.1.14__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pdfreader/__init__.py +8 -0
- pdfreader/buffer.py +188 -0
- pdfreader/codecs/__init__.py +13 -0
- pdfreader/codecs/agl.py +4289 -0
- pdfreader/codecs/cmaps/Adobe-CNS1-0 +134 -0
- pdfreader/codecs/cmaps/Adobe-CNS1-3 +151 -0
- pdfreader/codecs/cmaps/Adobe-CNS1-4 +152 -0
- pdfreader/codecs/cmaps/Adobe-GB1-0 +109 -0
- pdfreader/codecs/cmaps/Adobe-GB1-2 +165 -0
- pdfreader/codecs/cmaps/Adobe-GB1-4 +195 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-1 +111 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-2 +113 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-4 +138 -0
- pdfreader/codecs/cmaps/Adobe-Japan1-5 +157 -0
- pdfreader/codecs/cmaps/Adobe-Korea1-0 +114 -0
- pdfreader/codecs/cmaps/Adobe-Korea1-1 +149 -0
- pdfreader/codecs/cmaps/Adobe-Korea1-2 +149 -0
- pdfreader/codecs/cmaps/Identity-H +337 -0
- pdfreader/codecs/cmaps/README.md +1 -0
- pdfreader/codecs/codec.py +137 -0
- pdfreader/codecs/decoder.py +195 -0
- pdfreader/codecs/differences.py +75 -0
- pdfreader/codecs/macroman.py +69 -0
- pdfreader/codecs/pdfdoc.py +78 -0
- pdfreader/codecs/standard.py +50 -0
- pdfreader/codecs/tests.py +22 -0
- pdfreader/codecs/winansi.py +72 -0
- pdfreader/codecs/zapfdingbatsgl.py +205 -0
- pdfreader/constants.py +128 -0
- pdfreader/document.py +169 -0
- pdfreader/exceptions.py +12 -0
- pdfreader/filters/__init__.py +20 -0
- pdfreader/filters/ascii85.py +38 -0
- pdfreader/filters/asciihex.py +54 -0
- pdfreader/filters/ccittfax.py +554 -0
- pdfreader/filters/crypt.py +5 -0
- pdfreader/filters/dct.py +5 -0
- pdfreader/filters/flate.py +32 -0
- pdfreader/filters/jbig2.py +5 -0
- pdfreader/filters/jpx.py +5 -0
- pdfreader/filters/lzw.py +304 -0
- pdfreader/filters/predictors.py +33 -0
- pdfreader/filters/runlength.py +55 -0
- pdfreader/filters/test_ccittf_fax.py +144 -0
- pdfreader/filters/tests.py +22 -0
- pdfreader/parsers/__init__.py +4 -0
- pdfreader/parsers/base.py +741 -0
- pdfreader/parsers/cmap.py +268 -0
- pdfreader/parsers/content.py +88 -0
- pdfreader/parsers/document.py +627 -0
- pdfreader/parsers/inlineimage.py +73 -0
- pdfreader/parsers/objstm.py +16 -0
- pdfreader/parsers/tests.py +24 -0
- pdfreader/pillow.py +113 -0
- pdfreader/registry.py +48 -0
- pdfreader/securityhandler.py +333 -0
- pdfreader/tests.py +18 -0
- pdfreader/types/__init__.py +7 -0
- pdfreader/types/cmap.py +395 -0
- pdfreader/types/constants.py +235 -0
- pdfreader/types/content.py +92 -0
- pdfreader/types/filestructure.py +52 -0
- pdfreader/types/native.py +198 -0
- pdfreader/types/objects.py +523 -0
- pdfreader/types/tests.py +20 -0
- pdfreader/types/xref.py +165 -0
- pdfreader/utils.py +57 -0
- pdfreader/viewer/__init__.py +5 -0
- pdfreader/viewer/canvas.py +61 -0
- pdfreader/viewer/graphicsstate.py +99 -0
- pdfreader/viewer/pdfviewer.py +331 -0
- pdfreader/viewer/resources.py +64 -0
- pdfreader/viewer/simple.py +233 -0
- pdfreader/viewer/tests.py +20 -0
- pdfreader-0.1.14.dist-info/LICENSE +21 -0
- pdfreader-0.1.14.dist-info/METADATA +121 -0
- pdfreader-0.1.14.dist-info/RECORD +79 -0
- pdfreader-0.1.14.dist-info/WHEEL +5 -0
- pdfreader-0.1.14.dist-info/top_level.txt +1 -0
pdfreader/filters/lzw.py
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# Taken from https://github.com/joeatwork/python-lzw/blob/master/lzw/__init__.py
|
|
2
|
+
# As the most recent version was not found on pypi
|
|
3
|
+
# - Removed six dependency as no py2 compatibility required.
|
|
4
|
+
# - Removed encoder part as we don't need it
|
|
5
|
+
# - added decode() for PDF streams support
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
import struct, logging
|
|
9
|
+
|
|
10
|
+
from .predictors import _remove_predictors
|
|
11
|
+
|
|
12
|
+
filter_names = ('LZWDecode', 'LZW')
|
|
13
|
+
|
|
14
|
+
CLEAR_CODE = 256
|
|
15
|
+
END_OF_INFO_CODE = 257
|
|
16
|
+
|
|
17
|
+
DEFAULT_MIN_BITS = 9
|
|
18
|
+
DEFAULT_MAX_BITS = 12
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def decode(data, params):
|
|
22
|
+
"""
|
|
23
|
+
>>> data = b'9\\x98M\\xa7\\x03a\\x94@t2\\x9e\\x0e\\x90\\x00'
|
|
24
|
+
>>> decode(data, dict(Predictor=1))
|
|
25
|
+
b'sample text'
|
|
26
|
+
|
|
27
|
+
"""
|
|
28
|
+
try:
|
|
29
|
+
data = decompress(data)
|
|
30
|
+
data = _remove_predictors(data, params.get("Predictor"), params.get("Columns"))
|
|
31
|
+
except ValueError:
|
|
32
|
+
logging.exception("Skipping broken stream")
|
|
33
|
+
data = b''
|
|
34
|
+
return data
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def decompress(compressed_bytes):
|
|
38
|
+
"""
|
|
39
|
+
>>> decompress(b'9\\x98M\\xa7\\x03a\\x94@t2\\x9e\\x0e\\x90\\x00')
|
|
40
|
+
b'sample text'
|
|
41
|
+
|
|
42
|
+
"""
|
|
43
|
+
decoder = ByteDecoder()
|
|
44
|
+
return decoder.decodefrombytes(compressed_bytes)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ByteDecoder(object):
|
|
48
|
+
"""
|
|
49
|
+
Decodes, combines bit-unpacking and interpreting a codepoint
|
|
50
|
+
stream, suitable for use with bytes generated by
|
|
51
|
+
L{ByteEncoder}.
|
|
52
|
+
|
|
53
|
+
See L{ByteDecoder} for a usage example.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(self):
|
|
57
|
+
"""
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
self._decoder = Decoder()
|
|
61
|
+
self._unpacker = BitUnpacker(initial_code_size=self._decoder.code_size())
|
|
62
|
+
self.remaining = []
|
|
63
|
+
|
|
64
|
+
def decodefrombytes(self, bytesource):
|
|
65
|
+
"""
|
|
66
|
+
Given an iterator over BitPacked, Encoded bytes, Returns an
|
|
67
|
+
iterator over the uncompressed bytes. Dual of
|
|
68
|
+
L{ByteEncoder.encodetobytes}. See L{ByteEncoder} for an
|
|
69
|
+
example of use.
|
|
70
|
+
"""
|
|
71
|
+
codepoints = self._unpacker.unpack(bytesource)
|
|
72
|
+
clearbytes = self._decoder.decode(codepoints)
|
|
73
|
+
|
|
74
|
+
return clearbytes
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class BitUnpacker(object):
|
|
78
|
+
"""
|
|
79
|
+
An adaptive-width bit unpacker, intended to decode streams written
|
|
80
|
+
by L{BitPacker} into integer codepoints. Like L{BitPacker}, knows
|
|
81
|
+
about code size changes and control codes.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
def __init__(self, initial_code_size):
|
|
85
|
+
"""
|
|
86
|
+
initial_code_size is the starting size of the codebook
|
|
87
|
+
associated with the to-be-unpacked stream.
|
|
88
|
+
"""
|
|
89
|
+
self._initial_code_size = initial_code_size
|
|
90
|
+
|
|
91
|
+
def unpack(self, bytesource):
|
|
92
|
+
"""
|
|
93
|
+
Given an iterator of bytes, returns an iterator of integer
|
|
94
|
+
code points. Auto-magically adjusts point width when it sees
|
|
95
|
+
an almost-overflow in the input stream, or an LZW CLEAR_CODE
|
|
96
|
+
or END_OF_INFO_CODE
|
|
97
|
+
|
|
98
|
+
Trailing bits at the end of the given iterator, after the last
|
|
99
|
+
codepoint, will be dropped on the floor.
|
|
100
|
+
|
|
101
|
+
At the end of the iteration, or when an END_OF_INFO_CODE seen
|
|
102
|
+
the unpacker will ignore the bits after the code until it
|
|
103
|
+
reaches the next aligned byte. END_OF_INFO_CODE will *not*
|
|
104
|
+
stop the generator, just reset the alignment and the width
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
>>> unpk = BitUnpacker(initial_code_size=258)
|
|
108
|
+
>>> [ i for i in unpk.unpack([0x00, 0xC0, 0x40]) ]
|
|
109
|
+
[1, 257]
|
|
110
|
+
"""
|
|
111
|
+
bits = []
|
|
112
|
+
offset = 0
|
|
113
|
+
ignore = 0
|
|
114
|
+
|
|
115
|
+
codesize = self._initial_code_size
|
|
116
|
+
minwidth = 8
|
|
117
|
+
while (1 << minwidth) < codesize:
|
|
118
|
+
minwidth = minwidth + 1
|
|
119
|
+
|
|
120
|
+
pointwidth = minwidth
|
|
121
|
+
|
|
122
|
+
for nextbit in bytestobits(bytesource):
|
|
123
|
+
|
|
124
|
+
offset = (offset + 1) % 8
|
|
125
|
+
if ignore > 0:
|
|
126
|
+
ignore = ignore - 1
|
|
127
|
+
continue
|
|
128
|
+
|
|
129
|
+
bits.append(nextbit)
|
|
130
|
+
|
|
131
|
+
if len(bits) == pointwidth:
|
|
132
|
+
codepoint = intfrombits(bits)
|
|
133
|
+
bits = []
|
|
134
|
+
|
|
135
|
+
yield codepoint
|
|
136
|
+
|
|
137
|
+
codesize = codesize + 1
|
|
138
|
+
|
|
139
|
+
if codepoint in [CLEAR_CODE, END_OF_INFO_CODE]:
|
|
140
|
+
codesize = self._initial_code_size
|
|
141
|
+
pointwidth = minwidth
|
|
142
|
+
else:
|
|
143
|
+
# is this too late?
|
|
144
|
+
while codesize >= (2 ** pointwidth):
|
|
145
|
+
pointwidth = pointwidth + 1
|
|
146
|
+
|
|
147
|
+
if codepoint == END_OF_INFO_CODE:
|
|
148
|
+
ignore = (8 - offset) % 8
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class Decoder(object):
|
|
152
|
+
"""
|
|
153
|
+
Uncompresses a stream of lzw code points, as created by
|
|
154
|
+
L{Encoder}. Given a list of integer code points, with all
|
|
155
|
+
unpacking foolishness complete, turns that list of codepoints into
|
|
156
|
+
a list of uncompressed bytes. See L{BitUnpacker} for what this
|
|
157
|
+
doesn't do.
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
def __init__(self):
|
|
161
|
+
"""
|
|
162
|
+
Creates a new Decoder. Decoders should not be reused for
|
|
163
|
+
different streams.
|
|
164
|
+
"""
|
|
165
|
+
self._clear_codes()
|
|
166
|
+
self.remainder = []
|
|
167
|
+
|
|
168
|
+
def code_size(self):
|
|
169
|
+
"""
|
|
170
|
+
Returns the current size of the Decoder's code book, that is,
|
|
171
|
+
it's mapping of codepoints to byte strings. The return value of
|
|
172
|
+
this method will change as the decode encounters more encoded
|
|
173
|
+
input, or control codes.
|
|
174
|
+
"""
|
|
175
|
+
return len(self._codepoints)
|
|
176
|
+
|
|
177
|
+
def decode(self, codepoints):
|
|
178
|
+
"""
|
|
179
|
+
Given an iterable of integer codepoints, yields the
|
|
180
|
+
corresponding bytes, one at a time, as byte strings of length
|
|
181
|
+
E{1}. Retains the state of the codebook from call to call, so
|
|
182
|
+
if you have another stream, you'll likely need another
|
|
183
|
+
decoder!
|
|
184
|
+
|
|
185
|
+
Decoders will NOT handle END_OF_INFO_CODE (rather, they will
|
|
186
|
+
handle the code by throwing an exception); END_OF_INFO should
|
|
187
|
+
be handled by the upstream codepoint generator (see
|
|
188
|
+
L{BitUnpacker}, for example)
|
|
189
|
+
|
|
190
|
+
>>> dec = Decoder()
|
|
191
|
+
>>> dec.decode([103, 97, 98, 98, 97, 32, 258, 260, 262, 121, 111, 263, 259, 261, 256])
|
|
192
|
+
b'gabba gabba yo gabba'
|
|
193
|
+
|
|
194
|
+
"""
|
|
195
|
+
codepoints = [cp for cp in codepoints]
|
|
196
|
+
|
|
197
|
+
decoded = b''
|
|
198
|
+
for cp in codepoints:
|
|
199
|
+
decoded += self._decode_codepoint(cp)
|
|
200
|
+
return decoded
|
|
201
|
+
|
|
202
|
+
def _decode_codepoint(self, codepoint):
|
|
203
|
+
"""
|
|
204
|
+
Will raise a ValueError if given an END_OF_INFORMATION
|
|
205
|
+
code. EOI codes should be handled by callers if they're
|
|
206
|
+
present in our source stream.
|
|
207
|
+
|
|
208
|
+
>>> dec = Decoder()
|
|
209
|
+
>>> beforesize = dec.code_size()
|
|
210
|
+
>>> dec._decode_codepoint(0x80) == b'\\x80'
|
|
211
|
+
True
|
|
212
|
+
>>> dec._decode_codepoint(0x81) == b'\\x81'
|
|
213
|
+
True
|
|
214
|
+
>>> beforesize + 1 == dec.code_size()
|
|
215
|
+
True
|
|
216
|
+
>>> dec._decode_codepoint(256) == b''
|
|
217
|
+
True
|
|
218
|
+
>>> beforesize == dec.code_size()
|
|
219
|
+
True
|
|
220
|
+
"""
|
|
221
|
+
|
|
222
|
+
ret = b""
|
|
223
|
+
|
|
224
|
+
if codepoint == CLEAR_CODE:
|
|
225
|
+
self._clear_codes()
|
|
226
|
+
elif codepoint == END_OF_INFO_CODE:
|
|
227
|
+
raise ValueError("End of information code not supported directly by this Decoder")
|
|
228
|
+
else:
|
|
229
|
+
if codepoint in self._codepoints:
|
|
230
|
+
ret = self._codepoints[codepoint]
|
|
231
|
+
if None != self._prefix:
|
|
232
|
+
self._codepoints[len(self._codepoints)] = self._prefix + ret[0:1]
|
|
233
|
+
|
|
234
|
+
else:
|
|
235
|
+
ret = self._prefix + ret[0:1]
|
|
236
|
+
self._codepoints[len(self._codepoints)] = ret
|
|
237
|
+
|
|
238
|
+
self._prefix = ret
|
|
239
|
+
|
|
240
|
+
return ret
|
|
241
|
+
|
|
242
|
+
def _clear_codes(self):
|
|
243
|
+
self._codepoints = dict((pt, struct.pack("B", pt)) for pt in range(256))
|
|
244
|
+
self._codepoints[CLEAR_CODE] = CLEAR_CODE
|
|
245
|
+
self._codepoints[END_OF_INFO_CODE] = END_OF_INFO_CODE
|
|
246
|
+
self._prefix = None
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
#########################################
|
|
250
|
+
# Conveniences.
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def filebytes(fileobj, buffersize=1024):
|
|
254
|
+
"""
|
|
255
|
+
Convenience for iterating over the bytes in a file. Given a
|
|
256
|
+
file-like object (with a read(int) method), returns an iterator
|
|
257
|
+
over the bytes of that file.
|
|
258
|
+
"""
|
|
259
|
+
buff = fileobj.read(buffersize)
|
|
260
|
+
while buff:
|
|
261
|
+
for byte in buff: yield byte
|
|
262
|
+
buff = fileobj.read(buffersize)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def intfrombits(bits):
|
|
266
|
+
"""
|
|
267
|
+
Given a list of boolean values, interprets them as a binary
|
|
268
|
+
encoded, MSB-first unsigned integer (with True == 1 and False
|
|
269
|
+
== 0) and returns the result.
|
|
270
|
+
|
|
271
|
+
>>> intfrombits([ 1, 0, 0, 1, 1, 0, 0, 0, 0 ])
|
|
272
|
+
304
|
|
273
|
+
"""
|
|
274
|
+
ret = 0
|
|
275
|
+
lsb_first = [b for b in bits]
|
|
276
|
+
lsb_first.reverse()
|
|
277
|
+
|
|
278
|
+
for bit_index in range(len(lsb_first)):
|
|
279
|
+
if lsb_first[bit_index]:
|
|
280
|
+
ret = ret | (1 << bit_index)
|
|
281
|
+
|
|
282
|
+
return ret
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def bytestobits(bytesource):
|
|
286
|
+
"""
|
|
287
|
+
Breaks a given iterable of bytes into an iterable of boolean
|
|
288
|
+
values representing those bytes as unsigned integers.
|
|
289
|
+
|
|
290
|
+
>>> [ x for x in bytestobits(b"\\x01\\x30") ]
|
|
291
|
+
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0]
|
|
292
|
+
"""
|
|
293
|
+
for value in bytesource:
|
|
294
|
+
|
|
295
|
+
for bitplusone in range(8, 0, -1):
|
|
296
|
+
bitindex = bitplusone - 1
|
|
297
|
+
nextbit = 1 & (value >> bitindex)
|
|
298
|
+
yield nextbit
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
if __name__ == "__main__":
|
|
302
|
+
import doctest
|
|
303
|
+
|
|
304
|
+
doctest.testmod()
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
log = logging.getLogger(__name__)
|
|
3
|
+
|
|
4
|
+
def _remove_predictors(data, predictor=None, columns=None):
|
|
5
|
+
""" Remove LZW/Flate predictors
|
|
6
|
+
1 - No prediction
|
|
7
|
+
2 - TIFF predictor 2
|
|
8
|
+
10 - PNG None
|
|
9
|
+
11 - PNG Sub
|
|
10
|
+
12 - PNG Up
|
|
11
|
+
13 - PNG Average
|
|
12
|
+
14 - PNG Paeth
|
|
13
|
+
15 - PNG Optimum
|
|
14
|
+
"""
|
|
15
|
+
if predictor is None:
|
|
16
|
+
predictor = 1
|
|
17
|
+
|
|
18
|
+
if predictor == 1:
|
|
19
|
+
res = data
|
|
20
|
+
elif predictor == 2:
|
|
21
|
+
raise ValueError("TIFF prediction not implemented")
|
|
22
|
+
elif 10 <= predictor <= 15:
|
|
23
|
+
row_size = columns + 1
|
|
24
|
+
res = b''
|
|
25
|
+
for i in range(0, len(data), row_size):
|
|
26
|
+
if data[i] + 10 != predictor:
|
|
27
|
+
log.debug("Unexpected predictor {} in row {}. Expected value {}, columns {}"
|
|
28
|
+
.format(data[0] + 10, i, predictor, columns))
|
|
29
|
+
res += data[i + 1:i + row_size] # skip leading predictor byte
|
|
30
|
+
|
|
31
|
+
else:
|
|
32
|
+
raise ValueError("Unknown predictor type {}".format(predictor))
|
|
33
|
+
return res
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
log = logging.getLogger(__name__)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
filter_names = ('RunLengthDecode', 'RL')
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def decode(data, *_):
|
|
9
|
+
"""
|
|
10
|
+
>>> data = bytes([5, 65, 66, 67, 68, 69, 70, 250, 55, 2, 65, 66, 67, 252, 53, 128])
|
|
11
|
+
>>> decode(data)
|
|
12
|
+
b'ABCDEF7777777ABC55555'
|
|
13
|
+
|
|
14
|
+
>>> data = bytes([5, 65])
|
|
15
|
+
>>> decode(data)
|
|
16
|
+
b''
|
|
17
|
+
|
|
18
|
+
>>> data = bytes([128])
|
|
19
|
+
>>> decode(data)
|
|
20
|
+
b''
|
|
21
|
+
"""
|
|
22
|
+
res = b''
|
|
23
|
+
buffer = []
|
|
24
|
+
state = 'need_length'
|
|
25
|
+
for c in data:
|
|
26
|
+
if state == 'need_length':
|
|
27
|
+
if c == 128:
|
|
28
|
+
state = 'done'
|
|
29
|
+
break
|
|
30
|
+
buffer = []
|
|
31
|
+
length = c
|
|
32
|
+
if c >= 129:
|
|
33
|
+
state = 'need_one'
|
|
34
|
+
else:
|
|
35
|
+
state = 'need_many'
|
|
36
|
+
elif state == 'need_one':
|
|
37
|
+
res += bytes([c] * (257 - length))
|
|
38
|
+
state = 'need_length'
|
|
39
|
+
elif state == 'need_many':
|
|
40
|
+
buffer.append(c)
|
|
41
|
+
if len(buffer) == length + 1:
|
|
42
|
+
res += bytes(buffer)
|
|
43
|
+
buffer = []
|
|
44
|
+
state = 'need_length'
|
|
45
|
+
|
|
46
|
+
if state != 'done':
|
|
47
|
+
log.error("Skipping broken stream")
|
|
48
|
+
res = b''
|
|
49
|
+
|
|
50
|
+
return res
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
import doctest
|
|
55
|
+
doctest.testmod()
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
from .ccittfax import CCITTG4Parser
|
|
4
|
+
|
|
5
|
+
## Test cases
|
|
6
|
+
##
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TestCCITTG4Parser(unittest.TestCase):
|
|
10
|
+
|
|
11
|
+
@staticmethod
|
|
12
|
+
def get_parser(bits):
|
|
13
|
+
parser = CCITTG4Parser(len(bits))
|
|
14
|
+
parser._curline = [int(c) for c in bits]
|
|
15
|
+
parser._reset_line()
|
|
16
|
+
return parser
|
|
17
|
+
|
|
18
|
+
def test_b1(self):
|
|
19
|
+
parser = self.get_parser('00000')
|
|
20
|
+
parser._do_vertical(0)
|
|
21
|
+
self.assertEqual(parser._curpos, 0)
|
|
22
|
+
|
|
23
|
+
def test_b2(self):
|
|
24
|
+
parser = self.get_parser('10000')
|
|
25
|
+
parser._do_vertical(-1)
|
|
26
|
+
self.assertEqual(parser._curpos, 0)
|
|
27
|
+
|
|
28
|
+
def test_b3(self):
|
|
29
|
+
parser = self.get_parser('000111')
|
|
30
|
+
parser._do_pass()
|
|
31
|
+
self.assertEqual(parser._curpos, 3)
|
|
32
|
+
self.assertEqual(parser._get_bits(), '111')
|
|
33
|
+
|
|
34
|
+
def test_b4(self):
|
|
35
|
+
parser = self.get_parser('00000')
|
|
36
|
+
parser._do_vertical(+2)
|
|
37
|
+
self.assertEqual(parser._curpos, 2)
|
|
38
|
+
self.assertEqual(parser._get_bits(), '11')
|
|
39
|
+
|
|
40
|
+
def test_b5(self):
|
|
41
|
+
parser = self.get_parser('11111111100')
|
|
42
|
+
parser._do_horizontal(0, 3)
|
|
43
|
+
self.assertEqual(parser._curpos, 3)
|
|
44
|
+
parser._do_vertical(1)
|
|
45
|
+
self.assertEqual(parser._curpos, 10)
|
|
46
|
+
self.assertEqual(parser._get_bits(), '0001111111')
|
|
47
|
+
|
|
48
|
+
def test_e1(self):
|
|
49
|
+
parser = self.get_parser('10000')
|
|
50
|
+
parser._do_vertical(0)
|
|
51
|
+
self.assertEqual(parser._curpos, 1)
|
|
52
|
+
parser._do_vertical(0)
|
|
53
|
+
self.assertEqual(parser._curpos, 5)
|
|
54
|
+
self.assertEqual(parser._get_bits(), '10000')
|
|
55
|
+
|
|
56
|
+
def test_e2(self):
|
|
57
|
+
parser = self.get_parser('10011')
|
|
58
|
+
parser._do_vertical(0)
|
|
59
|
+
self.assertEqual(parser._curpos, 1)
|
|
60
|
+
parser._do_vertical(2)
|
|
61
|
+
self.assertEqual(parser._curpos, 5)
|
|
62
|
+
self.assertEqual(parser._get_bits(), '10000')
|
|
63
|
+
|
|
64
|
+
def test_e3(self):
|
|
65
|
+
parser = self.get_parser('011111')
|
|
66
|
+
parser._color = 0
|
|
67
|
+
parser._do_vertical(0)
|
|
68
|
+
self.assertEqual(parser._color, 1)
|
|
69
|
+
self.assertEqual(parser._curpos, 1)
|
|
70
|
+
parser._do_vertical(-2)
|
|
71
|
+
self.assertEqual(parser._color, 0)
|
|
72
|
+
self.assertEqual(parser._curpos, 4)
|
|
73
|
+
parser._do_vertical(0)
|
|
74
|
+
self.assertEqual(parser._curpos, 6)
|
|
75
|
+
self.assertEqual(parser._get_bits(), '011100')
|
|
76
|
+
|
|
77
|
+
def test_e4(self):
|
|
78
|
+
parser = self.get_parser('10000')
|
|
79
|
+
parser._do_vertical(0)
|
|
80
|
+
self.assertEqual(parser._curpos, 1)
|
|
81
|
+
parser._do_vertical(-2)
|
|
82
|
+
self.assertEqual(parser._curpos, 3)
|
|
83
|
+
parser._do_vertical(0)
|
|
84
|
+
self.assertEqual(parser._curpos, 5)
|
|
85
|
+
self.assertEqual(parser._get_bits(), '10011')
|
|
86
|
+
|
|
87
|
+
def test_e5(self):
|
|
88
|
+
parser = self.get_parser('011000')
|
|
89
|
+
parser._color = 0
|
|
90
|
+
parser._do_vertical(0)
|
|
91
|
+
self.assertEqual(parser._curpos, 1)
|
|
92
|
+
parser._do_vertical(3)
|
|
93
|
+
self.assertEqual(parser._curpos, 6)
|
|
94
|
+
self.assertEqual(parser._get_bits(), '011111')
|
|
95
|
+
|
|
96
|
+
def test_e6(self):
|
|
97
|
+
parser = self.get_parser('11001')
|
|
98
|
+
parser._do_pass()
|
|
99
|
+
self.assertEqual(parser._curpos, 4)
|
|
100
|
+
parser._do_vertical(0)
|
|
101
|
+
self.assertEqual(parser._curpos, 5)
|
|
102
|
+
self.assertEqual(parser._get_bits(), '11111')
|
|
103
|
+
|
|
104
|
+
def test_e7(self):
|
|
105
|
+
parser = self.get_parser('0000000000')
|
|
106
|
+
parser._curpos = 2
|
|
107
|
+
parser._color = 1
|
|
108
|
+
parser._do_horizontal(2, 6)
|
|
109
|
+
self.assertEqual(parser._curpos, 10)
|
|
110
|
+
self.assertEqual(parser._get_bits(), '1111000000')
|
|
111
|
+
|
|
112
|
+
def test_e8(self):
|
|
113
|
+
parser = self.get_parser('001100000')
|
|
114
|
+
parser._curpos = 1
|
|
115
|
+
parser._color = 0
|
|
116
|
+
parser._do_vertical(0)
|
|
117
|
+
self.assertEqual(parser._curpos, 2)
|
|
118
|
+
parser._do_horizontal(7, 0)
|
|
119
|
+
self.assertEqual(parser._curpos, 9)
|
|
120
|
+
self.assertEqual(parser._get_bits(), '101111111')
|
|
121
|
+
|
|
122
|
+
def test_m1(self):
|
|
123
|
+
parser = self.get_parser('10101')
|
|
124
|
+
parser._do_pass()
|
|
125
|
+
self.assertEqual(parser._curpos, 2)
|
|
126
|
+
parser._do_pass()
|
|
127
|
+
self.assertEqual(parser._curpos, 4)
|
|
128
|
+
self.assertEqual(parser._get_bits(), '1111')
|
|
129
|
+
|
|
130
|
+
def test_m2(self):
|
|
131
|
+
parser = self.get_parser('101011')
|
|
132
|
+
parser._do_vertical(-1)
|
|
133
|
+
parser._do_vertical(-1)
|
|
134
|
+
parser._do_vertical(1)
|
|
135
|
+
parser._do_horizontal(1, 1)
|
|
136
|
+
self.assertEqual(parser._get_bits(), '011101')
|
|
137
|
+
|
|
138
|
+
def test_m3(self):
|
|
139
|
+
parser = self.get_parser('10111011')
|
|
140
|
+
parser._do_vertical(-1)
|
|
141
|
+
parser._do_pass()
|
|
142
|
+
parser._do_vertical(1)
|
|
143
|
+
parser._do_vertical(1)
|
|
144
|
+
self.assertEqual(parser._get_bits(), '00000001')
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
import doctest
|
|
3
|
+
|
|
4
|
+
from . import decoders
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def suite():
|
|
8
|
+
loader = unittest.TestLoader()
|
|
9
|
+
suite = loader.discover('.', 'test_*')
|
|
10
|
+
for m in decoders:
|
|
11
|
+
suite.addTests(doctest.DocTestSuite(m))
|
|
12
|
+
return suite
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def load_tests(loader, tests, ignore):
|
|
16
|
+
tests.addTests(suite())
|
|
17
|
+
return tests
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
if __name__ == '__main__':
|
|
21
|
+
runner = unittest.TextTestRunner()
|
|
22
|
+
runner.run(suite())
|