Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize (#911) #912

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `ValueError` wrong error message when specifying codec for text output ([#902](https://github.com/pdfminer/pdfminer.six/pull/902))
- Resolve stream filter parameters ([#906](https://github.com/pdfminer/pdfminer.six/pull/906))
- Reading cmap's with whitespace in the name ([#935](https://github.com/pdfminer/pdfminer.six/pull/935))
- Optimize `apply_png_predictor` by using lists ([#912](https://github.com/pdfminer/pdfminer.six/pull/912))

## [20231228]

Expand Down
20 changes: 10 additions & 10 deletions pdfminer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ def apply_png_predictor(

nbytes = colors * columns * bitspercomponent // 8
bpp = colors * bitspercomponent // 8 # number of bytes per complete pixel
buf = b""
line_above = b"\x00" * columns
buf = []
line_above = list(b"\x00" * columns)
for scanline_i in range(0, len(data), nbytes + 1):
filter_type = data[scanline_i]
line_encoded = data[scanline_i + 1 : scanline_i + 1 + nbytes]
raw = b""
raw = []

if filter_type == 0:
# Filter type 0: None
raw += line_encoded
raw = list(line_encoded)

elif filter_type == 1:
# Filter type 1: Sub
Expand All @@ -162,7 +162,7 @@ def apply_png_predictor(
else:
raw_x_bpp = int(raw[j - bpp])
raw_x = (sub_x + raw_x_bpp) & 255
raw += bytes((raw_x,))
raw.append(raw_x)

elif filter_type == 2:
# Filter type 2: Up
Expand All @@ -173,7 +173,7 @@ def apply_png_predictor(
# the prior scanline.
for (up_x, prior_x) in zip(line_encoded, line_above):
raw_x = (up_x + prior_x) & 255
raw += bytes((raw_x,))
raw.append(raw_x)

elif filter_type == 3:
# Filter type 3: Average
Expand All @@ -191,7 +191,7 @@ def apply_png_predictor(
raw_x_bpp = int(raw[j - bpp])
prior_x = int(line_above[j])
raw_x = (average_x + (raw_x_bpp + prior_x) // 2) & 255
raw += bytes((raw_x,))
raw.append(raw_x)

elif filter_type == 4:
# Filter type 4: Paeth
Expand All @@ -212,14 +212,14 @@ def apply_png_predictor(
prior_x = int(line_above[j])
paeth = paeth_predictor(raw_x_bpp, prior_x, prior_x_bpp)
raw_x = (paeth_x + paeth) & 255
raw += bytes((raw_x,))
raw.append(raw_x)

else:
raise ValueError("Unsupported predictor value: %d" % filter_type)

buf += raw
buf.extend(raw)
line_above = raw
return buf
return bytes(buf)


Point = Tuple[float, float]
Expand Down
Loading