43
43
from ._utils import (
44
44
b_ ,
45
45
deprecate_with_replacement ,
46
+ deprecation_no_replacement ,
46
47
logger_warning ,
47
48
ord_ ,
48
49
)
53
54
from .constants import ImageAttributes as IA
54
55
from .constants import LzwFilterParameters as LZW
55
56
from .constants import StreamAttributes as SA
56
- from .errors import PdfReadError , PdfStreamError
57
+ from .errors import DeprecationError , PdfReadError , PdfStreamError
57
58
from .generic import (
58
59
ArrayObject ,
59
60
DictionaryObject ,
@@ -93,7 +94,7 @@ class FlateDecode:
93
94
@staticmethod
94
95
def decode (
95
96
data : bytes ,
96
- decode_parms : Union [ None , ArrayObject , DictionaryObject ] = None ,
97
+ decode_parms : Optional [ DictionaryObject ] = None ,
97
98
** kwargs : Any ,
98
99
) -> bytes :
99
100
"""
@@ -113,42 +114,37 @@ def decode(
113
114
if "decodeParms" in kwargs : # deprecated
114
115
deprecate_with_replacement ("decodeParms" , "parameters" , "4.0.0" )
115
116
decode_parms = kwargs ["decodeParms" ]
117
+ if isinstance (decode_parms , ArrayObject ): # type: ignore
118
+ raise DeprecationError ("decode_parms as ArrayObject is depreciated" )
119
+
116
120
str_data = decompress (data )
117
121
predictor = 1
118
122
119
123
if decode_parms :
120
124
try :
121
- if isinstance (decode_parms , ArrayObject ):
122
- for decode_parm in decode_parms :
123
- if "/Predictor" in decode_parm :
124
- predictor = decode_parm ["/Predictor" ]
125
- else :
126
- predictor = decode_parms .get ("/Predictor" , 1 )
125
+ predictor = decode_parms .get ("/Predictor" , 1 )
127
126
except (AttributeError , TypeError ): # Type Error is NullObject
128
127
pass # Usually an array with a null object was read
129
128
# predictor 1 == no predictor
130
129
if predictor != 1 :
131
130
# The /Columns param. has 1 as the default value; see ISO 32000,
132
131
# §7.4.4.3 LZWDecode and FlateDecode Parameters, Table 8
133
132
DEFAULT_BITS_PER_COMPONENT = 8
134
- if isinstance (decode_parms , ArrayObject ):
133
+ try :
134
+ columns = cast (int , decode_parms [LZW .COLUMNS ].get_object ()) # type: ignore
135
+ except (TypeError , KeyError ):
135
136
columns = 1
136
- bits_per_component = DEFAULT_BITS_PER_COMPONENT
137
- for decode_parm in decode_parms :
138
- if "/Columns" in decode_parm :
139
- columns = decode_parm ["/Columns" ]
140
- if LZW .BITS_PER_COMPONENT in decode_parm :
141
- bits_per_component = decode_parm [LZW .BITS_PER_COMPONENT ]
142
- else :
143
- columns = (
144
- 1 if decode_parms is None else decode_parms .get (LZW .COLUMNS , 1 )
145
- )
146
- colors = 1 if decode_parms is None else decode_parms .get (LZW .COLORS , 1 )
147
- bits_per_component = (
148
- decode_parms .get (LZW .BITS_PER_COMPONENT , DEFAULT_BITS_PER_COMPONENT )
149
- if decode_parms
150
- else DEFAULT_BITS_PER_COMPONENT
137
+ try :
138
+ colors = cast (int , decode_parms [LZW .COLORS ].get_object ()) # type: ignore
139
+ except (TypeError , KeyError ):
140
+ colors = 1
141
+ try :
142
+ bits_per_component = cast (
143
+ int ,
144
+ decode_parms [LZW .BITS_PER_COMPONENT ].get_object (), # type: ignore
151
145
)
146
+ except (TypeError , KeyError ):
147
+ bits_per_component = DEFAULT_BITS_PER_COMPONENT
152
148
153
149
# PNG predictor can vary by row and so is the lead byte on each row
154
150
rowlength = (
@@ -259,7 +255,7 @@ class ASCIIHexDecode:
259
255
@staticmethod
260
256
def decode (
261
257
data : Union [str , bytes ],
262
- decode_parms : Union [ None , ArrayObject , DictionaryObject ] = None ,
258
+ decode_parms : Optional [ DictionaryObject ] = None ,
263
259
** kwargs : Any ,
264
260
) -> bytes :
265
261
"""
@@ -278,9 +274,8 @@ def decode(
278
274
Raises:
279
275
PdfStreamError:
280
276
"""
281
- if "decodeParms" in kwargs : # deprecated
282
- deprecate_with_replacement ("decodeParms" , "parameters" , "4.0.0" )
283
- decode_parms = kwargs ["decodeParms" ] # noqa: F841
277
+ # decode_parms is unused here
278
+
284
279
if isinstance (data , str ):
285
280
data = data .encode ()
286
281
retval = b""
@@ -321,7 +316,7 @@ class RunLengthDecode:
321
316
@staticmethod
322
317
def decode (
323
318
data : bytes ,
324
- decode_parms : Union [ None , ArrayObject , DictionaryObject ] = None ,
319
+ decode_parms : Optional [ DictionaryObject ] = None ,
325
320
** kwargs : Any ,
326
321
) -> bytes :
327
322
"""
@@ -337,9 +332,8 @@ def decode(
337
332
Raises:
338
333
PdfStreamError:
339
334
"""
340
- if "decodeParms" in kwargs : # deprecated
341
- deprecate_with_replacement ("decodeParms" , "parameters" , "4.0.0" )
342
- decode_parms = kwargs ["decodeParms" ] # noqa: F841
335
+ # decode_parms is unused here
336
+
343
337
lst = []
344
338
index = 0
345
339
while True :
@@ -453,7 +447,7 @@ def decode(self) -> str:
453
447
@staticmethod
454
448
def decode (
455
449
data : bytes ,
456
- decode_parms : Union [ None , ArrayObject , DictionaryObject ] = None ,
450
+ decode_parms : Optional [ DictionaryObject ] = None ,
457
451
** kwargs : Any ,
458
452
) -> str :
459
453
"""
@@ -466,9 +460,8 @@ def decode(
466
460
Returns:
467
461
decoded data.
468
462
"""
469
- if "decodeParms" in kwargs : # deprecated
470
- deprecate_with_replacement ("decodeParms" , "parameters" , "4.0.0" )
471
- decode_parms = kwargs ["decodeParms" ] # noqa: F841
463
+ # decode_parms is unused here
464
+
472
465
return LZWDecode .Decoder (data ).decode ()
473
466
474
467
@@ -478,12 +471,11 @@ class ASCII85Decode:
478
471
@staticmethod
479
472
def decode (
480
473
data : Union [str , bytes ],
481
- decode_parms : Union [ None , ArrayObject , DictionaryObject ] = None ,
474
+ decode_parms : Optional [ DictionaryObject ] = None ,
482
475
** kwargs : Any ,
483
476
) -> bytes :
484
- if "decodeParms" in kwargs : # deprecated
485
- deprecate_with_replacement ("decodeParms" , "parameters" , "4.0.0" )
486
- decode_parms = kwargs ["decodeParms" ] # noqa: F841
477
+ # decode_parms is unused here
478
+
487
479
if isinstance (data , str ):
488
480
data = data .encode ("ascii" )
489
481
group_index = b = 0
@@ -511,25 +503,21 @@ class DCTDecode:
511
503
@staticmethod
512
504
def decode (
513
505
data : bytes ,
514
- decode_parms : Union [ None , ArrayObject , DictionaryObject ] = None ,
506
+ decode_parms : Optional [ DictionaryObject ] = None ,
515
507
** kwargs : Any ,
516
508
) -> bytes :
517
- if "decodeParms" in kwargs : # deprecated
518
- deprecate_with_replacement ("decodeParms" , "parameters" , "4.0.0" )
519
- decode_parms = kwargs ["decodeParms" ] # noqa: F841
509
+ # decode_parms is unused here
520
510
return data
521
511
522
512
523
513
class JPXDecode :
524
514
@staticmethod
525
515
def decode (
526
516
data : bytes ,
527
- decode_parms : Union [ None , ArrayObject , DictionaryObject ] = None ,
517
+ decode_parms : Optional [ DictionaryObject ] = None ,
528
518
** kwargs : Any ,
529
519
) -> bytes :
530
- if "decodeParms" in kwargs : # deprecated
531
- deprecate_with_replacement ("decodeParms" , "parameters" , "4.0.0" )
532
- decode_parms = kwargs ["decodeParms" ] # noqa: F841
520
+ # decode_parms is unused here
533
521
return data
534
522
535
523
@@ -591,13 +579,18 @@ def _get_parameters(
591
579
@staticmethod
592
580
def decode (
593
581
data : bytes ,
594
- decode_parms : Union [ None , ArrayObject , DictionaryObject ] = None ,
582
+ decode_parms : Optional [ DictionaryObject ] = None ,
595
583
height : int = 0 ,
596
584
** kwargs : Any ,
597
585
) -> bytes :
586
+ # decode_parms is unused here
598
587
if "decodeParms" in kwargs : # deprecated
599
588
deprecate_with_replacement ("decodeParms" , "parameters" , "4.0.0" )
600
589
decode_parms = kwargs ["decodeParms" ]
590
+ if isinstance (decode_parms , ArrayObject ): # deprecated
591
+ deprecation_no_replacement (
592
+ "decode_parms being an ArrayObject" , removed_in = "3.15.5"
593
+ )
601
594
parms = CCITTFaxDecode ._get_parameters (decode_parms , height )
602
595
603
596
img_size = len (data )
0 commit comments