Skip to content

Commit

Permalink
:s/unmultiplied/straight.
Browse files Browse the repository at this point in the history
  • Loading branch information
anntzer committed Dec 8, 2018
1 parent 3d4f189 commit 30db91f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ISSUES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ Matplotlib's draw_path_collection has inconsistent semantics across backends
Issues with cairo
-----------------

Precision is lost when roundtripping between unmultiplied and premultiplied
alpha. ::
Precision is lost when roundtripping between straight and premultiplied alpha.
::

test_agg::test_repeated_save_with_alpha

Expand Down
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ e.g. ``matplotlib.use("module://mplcairo.macosx")``.
Alternatively, set the ``MPLCAIRO_PATCH_AGG`` environment variable to a
non-empty value to fully replace the Agg renderer by the cairo renderer
throughout Matplotlib. However, this approach is inefficient (due to the need
of copies and conversions between premultiplied ARGB32 and non-premultiplied
RGBA8888 buffers); additionally, it does not work with the wx and macosx
backends due to peculiarities of the corresponding canvas classes. On the
other hand, this is currently the only way in which the webagg-based backends
(e.g., Jupyter's inline widget) are supported.
of copies and conversions between premultiplied ARGB32 and straight RGBA8888
buffers); additionally, it does not work with the wx and macosx backends due
to peculiarities of the corresponding canvas classes. On the other hand, this
is currently the only way in which the webagg-based backends (e.g., Jupyter's
inline widget) are supported.

At import-time, mplcairo will attempt to load Raqm_. The use of that library
can be controlled and checked using the ``set_options`` and ``get_options``
Expand Down
6 changes: 3 additions & 3 deletions lib/mplcairo/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def to_premultiplied_rgba8888(buf):
[2, 1, 0, 3] if sys.byteorder == "little" else [1, 2, 3, 0], axis=2)


def to_unmultiplied_rgba8888(buf):
"""Convert a buffer from premultiplied ARGB32 to unmultiplied RGBA8888."""
def to_straight_rgba8888(buf):
"""Convert a buffer from premultiplied ARGB32 to straight RGBA8888."""
rgba = to_premultiplied_rgba8888(buf)
# Un-premultiply alpha. The formula is the same as in cairo-png.c.
# The straightening formula is from cairo-png.c.
rgb = rgba[..., :-1]
alpha = rgba[..., -1]
mask = alpha != 0
Expand Down
20 changes: 10 additions & 10 deletions lib/mplcairo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath="TeX!", mtext=None):
mb._draw(self, x, y, angle)

def stop_filter(self, filter_func):
img = _util.to_unmultiplied_rgba8888(self._stop_filter_get_buffer())
img = _util.to_straight_rgba8888(self._stop_filter_get_buffer())
img, (l, b, w, h) = _get_drawn_subarray_and_bounds(img)
if not (w and h):
return
Expand All @@ -148,13 +148,13 @@ def stop_rasterizing(self):
lock = _LOCK # Needed for webagg_core; fixed by matplotlib#10708.

def buffer_rgba(self): # Needed for webagg_core.
return _util.to_unmultiplied_rgba8888(self._get_buffer())
return _util.to_straight_rgba8888(self._get_buffer())

_renderer = property(buffer_rgba) # Needed for tkagg.

def tostring_rgba_minimized(self): # Needed for MixedModeRenderer.
img, bounds = _get_drawn_subarray_and_bounds(
_util.to_unmultiplied_rgba8888(self._get_buffer()))
_util.to_straight_rgba8888(self._get_buffer()))
return img.tobytes(), bounds


Expand Down Expand Up @@ -272,22 +272,22 @@ def _print_ps_impl(self, is_eps, path_or_stream,
print_ps = partialmethod(_print_ps_impl, False)
print_eps = partialmethod(_print_ps_impl, True)

def _get_fresh_unmultiplied_rgba8888(self):
def _get_fresh_straight_rgba8888(self):
# Swap out the cache, as savefig may be playing with the background
# color.
last_renderer_call = self._last_renderer_call
self._last_renderer_call = (None, None)
with _LOCK:
renderer = self.get_renderer(_draw_if_new=True)
self._last_renderer_call = last_renderer_call
return _util.to_unmultiplied_rgba8888(renderer._get_buffer())
return _util.to_straight_rgba8888(renderer._get_buffer())

def print_rgba(
self, path_or_stream, *, metadata=None,
# These arguments are already taken care of by print_figure().
dpi=72, facecolor=None, edgecolor=None, orientation="portrait",
dryrun=False, bbox_inches_restore=None):
img = self._get_fresh_unmultiplied_rgba8888()
img = self._get_fresh_straight_rgba8888()
if dryrun:
return
with cbook.open_file_cm(path_or_stream, "wb") as stream:
Expand All @@ -300,7 +300,7 @@ def print_png(
# These arguments are already taken care of by print_figure().
dpi=72, facecolor=None, edgecolor=None, orientation="portrait",
dryrun=False, bbox_inches_restore=None):
img = self._get_fresh_unmultiplied_rgba8888()
img = self._get_fresh_straight_rgba8888()
if dryrun:
return
full_metadata = OrderedDict(
Expand All @@ -320,13 +320,13 @@ def print_jpeg(
dryrun=False, bbox_inches_restore=None,
# Remaining kwargs are passed to PIL.
**kwargs):
buf = self._get_fresh_unmultiplied_rgba8888()
buf = self._get_fresh_straight_rgba8888()
if dryrun:
return
img = Image.frombuffer(
"RGBA", buf.shape[:2][::-1], buf, "raw", "RGBA", 0, 1)
# Composite against the background (actually we could just skip the
# conversion to unpremultiplied RGBA earlier).
# conversion to straight RGBA earlier).
# NOTE: Agg composites against rcParams["savefig.facecolor"].
background = tuple(
(np.array(colors.to_rgb(facecolor)) * 255).astype(int))
Expand All @@ -343,7 +343,7 @@ def print_tiff(
# These arguments are already taken care of by print_figure().
dpi=72, facecolor=None, edgecolor=None, orientation="portrait",
dryrun=False, bbox_inches_restore=None):
buf = self._get_fresh_unmultiplied_rgba8888()
buf = self._get_fresh_straight_rgba8888()
if dryrun:
return
(Image.frombuffer(
Expand Down
2 changes: 1 addition & 1 deletion lib/mplcairo/macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, figure):
def _draw(self):
if self.figure.stale:
self._last_renderer_call = None, None
self._renderer = _util.to_unmultiplied_rgba8888(
self._renderer = _util.to_straight_rgba8888(
self.get_renderer(_draw_if_new=True)._get_buffer())
return self

Expand Down

0 comments on commit 30db91f

Please sign in to comment.