Skip to content

Commit 8000cbe

Browse files
authored
MAINT: Remove duplicate CCITT processing (#3415)
1 parent ad85a22 commit 8000cbe

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

pypdf/filters.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -796,15 +796,15 @@ def decode_stream_data(stream: Any) -> bytes:
796796
return data
797797

798798

799-
def _xobj_to_image(x_object_obj: dict[str, Any]) -> tuple[Optional[str], bytes, Any]:
799+
def _xobj_to_image(x_object: dict[str, Any]) -> tuple[Optional[str], bytes, Any]:
800800
"""
801801
Users need to have the pillow package installed.
802802
803803
It's unclear if pypdf will keep this function here, hence it's private.
804804
It might get removed at any point.
805805
806806
Args:
807-
x_object_obj:
807+
x_object:
808808
809809
Returns:
810810
Tuple[file extension, bytes, PIL.Image.Image]
@@ -822,20 +822,20 @@ def _xobj_to_image(x_object_obj: dict[str, Any]) -> tuple[Optional[str], bytes,
822822

823823
def _apply_alpha(
824824
img: Image.Image,
825-
x_object_obj: dict[str, Any],
825+
x_object: dict[str, Any],
826826
obj_as_text: str,
827827
image_format: str,
828828
extension: str,
829829
) -> tuple[Image.Image, str, str]:
830830
alpha = None
831-
if IA.S_MASK in x_object_obj: # add alpha channel
832-
alpha = _xobj_to_image(x_object_obj[IA.S_MASK])[2]
831+
if IA.S_MASK in x_object: # add alpha channel
832+
alpha = _xobj_to_image(x_object[IA.S_MASK])[2]
833833
if img.size != alpha.size:
834834
logger_warning(
835835
f"image and mask size not matching: {obj_as_text}", __name__
836836
)
837837
else:
838-
# TODO : implement mask
838+
# TODO: implement mask
839839
if alpha.mode != "L":
840840
alpha = alpha.convert("L")
841841
if img.mode == "P":
@@ -844,40 +844,40 @@ def _apply_alpha(
844844
img = img.convert("L")
845845
img.putalpha(alpha)
846846
if "JPEG" in image_format:
847-
extension = ".jp2"
848847
image_format = "JPEG2000"
848+
extension = ".jp2"
849849
else:
850-
extension = ".png"
851850
image_format = "PNG"
851+
extension = ".png"
852852
return img, extension, image_format
853853

854-
# for error reporting
854+
# For error reporting
855855
obj_as_text = (
856-
x_object_obj.indirect_reference.__repr__()
857-
if x_object_obj is None # pragma: no cover
858-
else x_object_obj.__repr__()
856+
x_object.indirect_reference.__repr__()
857+
if x_object is None # pragma: no cover
858+
else x_object.__repr__()
859859
)
860860

861861
# Get size and data
862-
size = (cast(int, x_object_obj[IA.WIDTH]), cast(int, x_object_obj[IA.HEIGHT]))
863-
data = x_object_obj.get_data() # type: ignore
862+
size = (cast(int, x_object[IA.WIDTH]), cast(int, x_object[IA.HEIGHT]))
863+
data = x_object.get_data() # type: ignore
864864
if isinstance(data, str): # pragma: no cover
865865
data = data.encode()
866866
if len(data) % (size[0] * size[1]) == 1 and data[-1] == 0x0A: # ie. '\n'
867867
data = data[:-1]
868868

869869
# Get color properties
870-
colors = x_object_obj.get("/Colors", 1)
871-
color_space: Any = x_object_obj.get("/ColorSpace", NullObject()).get_object()
870+
colors = x_object.get("/Colors", 1)
871+
color_space: Any = x_object.get("/ColorSpace", NullObject()).get_object()
872872
if isinstance(color_space, list) and len(color_space) == 1:
873873
color_space = color_space[0].get_object()
874874

875-
mode, invert_color = _get_mode_and_invert_color(x_object_obj, colors, color_space)
875+
mode, invert_color = _get_mode_and_invert_color(x_object, colors, color_space)
876876

877877
# Get filters
878-
filters = x_object_obj.get(SA.FILTER, NullObject()).get_object()
878+
filters = x_object.get(SA.FILTER, NullObject()).get_object()
879879
lfilters = filters[-1] if isinstance(filters, list) else filters
880-
decode_parms = x_object_obj.get(SA.DECODE_PARMS, None)
880+
decode_parms = x_object.get(SA.DECODE_PARMS, None)
881881
if decode_parms and isinstance(decode_parms, (tuple, list)):
882882
decode_parms = decode_parms[0]
883883
else:
@@ -895,16 +895,16 @@ def _apply_alpha(
895895
colors,
896896
obj_as_text,
897897
)
898-
elif lfilters in (FT.LZW_DECODE, FT.ASCII_85_DECODE, FT.CCITT_FAX_DECODE):
898+
elif lfilters in (FT.LZW_DECODE, FT.ASCII_85_DECODE):
899899
# I'm not sure if the following logic is correct.
900900
# There might not be any relationship between the filters and the
901901
# extension
902-
if lfilters in (FT.LZW_DECODE, FT.CCITT_FAX_DECODE):
903-
extension = ".tiff" # mime_type = "image/tiff"
902+
if lfilters == FT.LZW_DECODE:
904903
image_format = "TIFF"
904+
extension = ".tiff" # mime_type = "image/tiff"
905905
else:
906-
extension = ".png" # mime_type = "image/png"
907906
image_format = "PNG"
907+
extension = ".png" # mime_type = "image/png"
908908
try:
909909
img = Image.open(BytesIO(data), formats=("TIFF", "PNG"))
910910
except UnidentifiedImageError:
@@ -938,7 +938,7 @@ def _apply_alpha(
938938
False,
939939
)
940940
elif mode == "":
941-
raise PdfReadError(f"ColorSpace field not found in {x_object_obj}")
941+
raise PdfReadError(f"ColorSpace field not found in {x_object}")
942942
else:
943943
img, image_format, extension, invert_color = (
944944
_extended_image_frombytes(mode, size, data),
@@ -947,9 +947,9 @@ def _apply_alpha(
947947
False,
948948
)
949949

950-
img = _apply_decode(img, x_object_obj, lfilters, color_space, invert_color)
950+
img = _apply_decode(img, x_object, lfilters, color_space, invert_color)
951951
img, extension, image_format = _apply_alpha(
952-
img, x_object_obj, obj_as_text, image_format, extension
952+
img, x_object, obj_as_text, image_format, extension
953953
)
954954

955955
if lfilters == FT.CCITT_FAX_DECODE and decode_parms.get("/BlackIs1", BooleanObject(False)).value is True:

0 commit comments

Comments
 (0)