Skip to content

Simplify binary data handling in ps backend. #23294

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

Merged
merged 1 commit into from
Jun 19, 2022
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
32 changes: 10 additions & 22 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ def _nums_to_str(*args):

@_api.deprecated("3.6", alternative="Vendor the code")
def quote_ps_string(s):
return _quote_ps_string(s)


def _quote_ps_string(s):
"""
Quote dangerous characters of S for use in a PostScript string constant.
"""
Expand Down Expand Up @@ -447,17 +443,7 @@ def draw_image(self, gc, x, y, im, transform=None):
h, w = im.shape[:2]
imagecmd = "false 3 colorimage"
data = im[::-1, :, :3] # Vertically flipped rgb values.
# data.tobytes().hex() has no spaces, so can be linewrapped by simply
# splitting data every nchars. It's equivalent to textwrap.fill only
# much faster.
nchars = 128
data = data.tobytes().hex()
hexlines = "\n".join(
[
data[n * nchars:(n + 1) * nchars]
for n in range(math.ceil(len(data) / nchars))
]
)
hexdata = data.tobytes().hex("\n", -64) # Linewrap to 128 chars.

if transform is None:
matrix = "1 0 0 1 0 0"
Expand All @@ -479,7 +465,7 @@ def draw_image(self, gc, x, y, im, transform=None):
{{
currentfile DataString readhexstring pop
}} bind {imagecmd}
{hexlines}
{hexdata}
grestore
""")

Expand Down Expand Up @@ -737,13 +723,13 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
xmin, ymin = points_min
xmax, ymax = points_max

streamarr = np.empty(
data = np.empty(
shape[0] * shape[1],
dtype=[('flags', 'u1'), ('points', '2>u4'), ('colors', '3u1')])
streamarr['flags'] = 0
streamarr['points'] = (flat_points - points_min) * factor
streamarr['colors'] = flat_colors[:, :3] * 255.0
stream = _quote_ps_string(streamarr.tobytes())
data['flags'] = 0
data['points'] = (flat_points - points_min) * factor
data['colors'] = flat_colors[:, :3] * 255.0
hexdata = data.tobytes().hex("\n", -64) # Linewrap to 128 chars.

self._pswriter.write(f"""\
gsave
Expand All @@ -754,7 +740,9 @@ def draw_gouraud_triangles(self, gc, points, colors, trans):
/BitsPerFlag 8
/AntiAlias true
/Decode [ {xmin:g} {xmax:g} {ymin:g} {ymax:g} 0 1 0 1 0 1 ]
/DataSource ({stream})
/DataSource <
{hexdata}
>
>>
shfill
grestore
Expand Down
312 changes: 312 additions & 0 deletions lib/matplotlib/tests/baseline_images/test_axes/pcolormesh_small.eps

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading