31
31
__author_email__ = "biziqe@mathieu.fenniak.net"
32
32
33
33
import math
34
+ from sys import version_info
35
+
36
+ from PyPDF2 .constants import CcittFaxDecodeParameters as CCITT
37
+ from PyPDF2 .constants import ColorSpaces
38
+ from PyPDF2 .constants import FilterTypeAbbreviations as FTA
39
+ from PyPDF2 .constants import FilterTypes as FT
40
+ from PyPDF2 .constants import ImageAttributes as IA
41
+ from PyPDF2 .constants import LzwFilterParameters as LZW
42
+ from PyPDF2 .constants import StreamAttributes as SA
34
43
35
44
from .utils import PdfReadError , ord_ , paethPredictor
36
- from sys import version_info
45
+
37
46
if version_info < ( 3 , 0 ):
38
47
from cStringIO import StringIO
39
48
else :
40
49
from io import StringIO
50
+
41
51
import struct
42
52
43
53
try :
@@ -110,13 +120,13 @@ def decode(data, decodeParms):
110
120
predictor = 1
111
121
if decodeParms :
112
122
try :
113
- predictor = decodeParms .get ("/Predictor" , 1 )
123
+ predictor = decodeParms .get (LZW . PREDICTOR , 1 )
114
124
except AttributeError :
115
125
pass # usually an array with a null object was read
116
126
117
127
# predictor 1 == no predictor
118
128
if predictor != 1 :
119
- columns = decodeParms ["/Columns" ]
129
+ columns = decodeParms [LZW . COLUMNS ]
120
130
# PNG prediction:
121
131
if predictor >= 10 and predictor <= 15 :
122
132
output = StringIO ()
@@ -261,7 +271,7 @@ def decode(self):
261
271
return baos
262
272
263
273
@staticmethod
264
- def decode (data ,decodeParams = None ):
274
+ def decode (data , decodeParms = None ):
265
275
return LZWDecode .decoder (data ).decode ()
266
276
267
277
@@ -363,7 +373,7 @@ def decode(data, decodeParms=None, height=0):
363
373
else :
364
374
CCITTgroup = 3
365
375
366
- width = decodeParms ["/Columns" ]
376
+ width = decodeParms [CCITT . COLUMNS ]
367
377
imgSize = len (data )
368
378
tiff_header_struct = '<2shlh' + 'hhll' * 8 + 'h'
369
379
tiffHeader = struct .pack (tiff_header_struct ,
@@ -388,7 +398,7 @@ def decode(data, decodeParms=None, height=0):
388
398
389
399
def decodeStreamData (stream ):
390
400
from .generic import NameObject
391
- filters = stream .get ("/Filter" , ())
401
+ filters = stream .get (SA . FILTER , ())
392
402
393
403
if len (filters ) and not isinstance (filters [0 ], NameObject ):
394
404
# we have a single filter instance
@@ -397,24 +407,24 @@ def decodeStreamData(stream):
397
407
# If there is not data to decode we should not try to decode the data.
398
408
if data :
399
409
for filterType in filters :
400
- if filterType == "/FlateDecode" or filterType == "/Fl" :
401
- data = FlateDecode .decode (data , stream .get ("/DecodeParms" ))
402
- elif filterType == "/ASCIIHexDecode" or filterType == "/ AHx" :
410
+ if filterType == FT . FLATE_DECODE or filterType == FTA . FL :
411
+ data = FlateDecode .decode (data , stream .get (SA . DECODE_PARMS ))
412
+ elif filterType == FT . ASCII_HEX_DECODE or filterType == FTA . AHx :
403
413
data = ASCIIHexDecode .decode (data )
404
- elif filterType == "/LZWDecode" or filterType == "/ LZW" :
405
- data = LZWDecode .decode (data , stream .get ("/DecodeParms" ))
406
- elif filterType == "/ASCII85Decode" or filterType == "/ A85" :
414
+ elif filterType == FT . LZW_DECODE or filterType == FTA . LZW :
415
+ data = LZWDecode .decode (data , stream .get (SA . DECODE_PARMS ))
416
+ elif filterType == FT . ASCII_85_DECODE or filterType == FTA . A85 :
407
417
data = ASCII85Decode .decode (data )
408
- elif filterType == "/DCTDecode" :
418
+ elif filterType == FT . DCT_DECODE :
409
419
data = DCTDecode .decode (data )
410
420
elif filterType == "/JPXDecode" :
411
421
data = JPXDecode .decode (data )
412
- elif filterType == "/CCITTFaxDecode" :
413
- height = stream .get ("/Height" , ())
414
- data = CCITTFaxDecode .decode (data , stream .get ("/DecodeParms" ), height )
422
+ elif filterType == FT . CCITT_FAX_DECODE :
423
+ height = stream .get (IA . HEIGHT , ())
424
+ data = CCITTFaxDecode .decode (data , stream .get (SA . DECODE_PARMS ), height )
415
425
elif filterType == "/Crypt" :
416
- decodeParams = stream .get ("/DecodeParams" , {})
417
- if "/Name" not in decodeParams and "/Type" not in decodeParams :
426
+ decodeParms = stream .get (SA . DECODE_PARMS , {})
427
+ if "/Name" not in decodeParms and "/Type" not in decodeParms :
418
428
pass
419
429
else :
420
430
raise NotImplementedError ("/Crypt filter with /Name or /Type not supported yet" )
@@ -434,34 +444,37 @@ def _xobj_to_image(x_object_obj):
434
444
:return: Tuple[file extension, bytes]
435
445
"""
436
446
import io
447
+
437
448
from PIL import Image
438
449
439
- size = (x_object_obj ["/Width" ], x_object_obj ["/Height" ])
450
+ from PyPDF2 .constants import GraphicsStateParameters as G
451
+
452
+ size = (x_object_obj [IA .WIDTH ], x_object_obj [IA .HEIGHT ])
440
453
data = x_object_obj .getData ()
441
- if x_object_obj ["/ColorSpace" ] == "/DeviceRGB" :
454
+ if x_object_obj [IA . COLOR_SPACE ] == ColorSpaces . DEVICE_RGB :
442
455
mode = "RGB"
443
456
else :
444
457
mode = "P"
445
458
extension = None
446
- if "/Filter" in x_object_obj :
447
- if x_object_obj ["/Filter" ] == "/FlateDecode" :
459
+ if SA . FILTER in x_object_obj :
460
+ if x_object_obj [SA . FILTER ] == FT . FLATE_DECODE :
448
461
extension = ".png"
449
462
img = Image .frombytes (mode , size , data )
450
- if "/SMask" in x_object_obj : # add alpha channel
451
- alpha = Image .frombytes ("L" , size , x_object_obj ["/SMask" ].getData ())
463
+ if G . S_MASK in x_object_obj : # add alpha channel
464
+ alpha = Image .frombytes ("L" , size , x_object_obj [G . S_MASK ].getData ())
452
465
img .putalpha (alpha )
453
466
img_byte_arr = io .BytesIO ()
454
467
img .save (img_byte_arr , format = "PNG" )
455
468
data = img_byte_arr .getvalue ()
456
- elif x_object_obj ["/Filter" ] in (["/LZWDecode" ], ['/ASCII85Decode' ], ['/CCITTFaxDecode' ]):
469
+ elif x_object_obj [SA . FILTER ] in ([FT . LZW_DECODE ], [FT . ASCII_85_DECODE ], [FT . CCITT_FAX_DECODE ]):
457
470
from PyPDF2 .utils import b_
458
471
extension = ".png"
459
472
data = b_ (data )
460
- elif x_object_obj ["/Filter" ] == "/DCTDecode" :
473
+ elif x_object_obj [SA . FILTER ] == FT . DCT_DECODE :
461
474
extension = ".jpg"
462
- elif x_object_obj ["/Filter" ] == "/JPXDecode" :
475
+ elif x_object_obj [SA . FILTER ] == "/JPXDecode" :
463
476
extension = ".jp2"
464
- elif x_object_obj ["/Filter" ] == "/CCITTFaxDecode" :
477
+ elif x_object_obj [SA . FILTER ] == FT . CCITT_FAX_DECODE :
465
478
extension = ".tiff"
466
479
else :
467
480
extension = ".png"
0 commit comments