Skip to content

Commit

Permalink
Merge pull request #14 from maxpmaxp/multiple-filters-inline-images
Browse files Browse the repository at this point in the history
multiple filters support for inline images
  • Loading branch information
maxpmaxp committed Dec 19, 2019
2 parents bf832f3 + e32de9d commit ba6cbc1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
7 changes: 2 additions & 5 deletions pdfreader/types/content.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..filters import apply_filter
from ..pillow import PILImageMixin
from ..types.native import apply_filter_multi


class StreamContent(object):
Expand Down Expand Up @@ -70,10 +70,7 @@ def ImageMask(self):
@property
def filtered(self):
""" :return: bytes, decoded image stream as it defined by image properties """
binary = self.data
if self.Filter:
binary = apply_filter(self.Filter, binary, self.dictionary.get('DecodeParms'))
return binary
return apply_filter_multi(self.Filter, self.data, self.DecodeParms)


class Operator(StreamContent):
Expand Down
49 changes: 27 additions & 22 deletions pdfreader/types/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ def to_bytes(self):
return bytes.fromhex(self)


def apply_filter_multi(filters, binary, params):
if not filters:
return binary

if isinstance(filters, Array):
farr = filters
elif isinstance(filters, Name):
farr = Array()
farr.append(filters)
else:
raise TypeError("Incorrect filter type: {}".format(filters))

filters_applied = []
for fname in farr:
try:
binary = apply_filter(fname, binary, params)
filters_applied.append(fname)
except NotImplementedError:
logging.exception("Partially decoded. Filters applied: {}".format(filters_applied))
raise

return binary


class Stream(object):
""" binary stream: dictionary and binary data
common keys:
Expand Down Expand Up @@ -81,29 +105,10 @@ def type(self):
@cached_property
def filtered(self):
""" :return: bytes, decoded image stream as it defined by image properties """
filters = self.get('Filter')
if not filters:
return self.stream

if isinstance(filters, Array):
farr = filters
elif isinstance(filters, Name):
farr = Array()
farr.append(filters)
else:
raise TypeError("Incorrect filter type: {}".format(filters))

binary = self.stream
filters_applied = []
for fname in farr:
try:
binary = apply_filter(fname, binary, self.dictionary.get("DecodeParms"))
filters_applied.append(fname)
except NotImplementedError:
logging.exception("Partially decoded. Filters applied: {}".format(filters_applied))
raise
return apply_filter_multi(self.get('Filter'),
self.stream,
self.dictionary.get("DecodeParms"))

return binary

def __eq__(self, other):
return self.dictionary == other.dictionary and self.stream == other.stream
Expand Down

0 comments on commit ba6cbc1

Please sign in to comment.