|
42 | 42 | from matplotlib.transforms import Affine2D |
43 | 43 |
|
44 | 44 |
|
45 | | -# Cairo's image buffers are premultiplied ARGB32, |
46 | | -# Matplotlib's are unmultiplied RGBA8888. |
47 | | - |
48 | | - |
49 | | -def _premultiplied_argb32_to_unmultiplied_rgba8888(buf): |
50 | | - """ |
51 | | - Convert a premultiplied ARGB32 buffer to an unmultiplied RGBA8888 buffer. |
52 | | - """ |
53 | | - rgba = np.take( # .take() ensures C-contiguity of the result. |
54 | | - buf, |
55 | | - [2, 1, 0, 3] if sys.byteorder == "little" else [1, 2, 3, 0], axis=2) |
56 | | - rgb = rgba[..., :-1] |
57 | | - alpha = rgba[..., -1] |
58 | | - # Un-premultiply alpha. The formula is the same as in cairo-png.c. |
59 | | - mask = alpha != 0 |
60 | | - for channel in np.rollaxis(rgb, -1): |
61 | | - channel[mask] = ( |
62 | | - (channel[mask].astype(int) * 255 + alpha[mask] // 2) |
63 | | - // alpha[mask]) |
64 | | - return rgba |
65 | | - |
66 | | - |
67 | | -def _unmultipled_rgba8888_to_premultiplied_argb32(rgba8888): |
68 | | - """ |
69 | | - Convert an unmultiplied RGBA8888 buffer to a premultiplied ARGB32 buffer. |
70 | | - """ |
71 | | - if sys.byteorder == "little": |
72 | | - argb32 = np.take(rgba8888, [2, 1, 0, 3], axis=2) |
73 | | - rgb24 = argb32[..., :-1] |
74 | | - alpha8 = argb32[..., -1:] |
75 | | - else: |
76 | | - argb32 = np.take(rgba8888, [3, 0, 1, 2], axis=2) |
77 | | - alpha8 = argb32[..., :1] |
78 | | - rgb24 = argb32[..., 1:] |
79 | | - # Only bother premultiplying when the alpha channel is not fully opaque, |
80 | | - # as the cost is not negligible. The unsafe cast is needed to do the |
81 | | - # multiplication in-place in an integer buffer. |
82 | | - if alpha8.min() != 0xff: |
83 | | - np.multiply(rgb24, alpha8 / 0xff, out=rgb24, casting="unsafe") |
84 | | - return argb32 |
85 | | - |
86 | | - |
87 | 45 | if cairo.__name__ == "cairocffi": |
88 | 46 | # Convert a pycairo context to a cairocffi one. |
89 | 47 | def _to_context(ctx): |
@@ -380,7 +338,7 @@ def _draw_paths(): |
380 | 338 | _draw_paths() |
381 | 339 |
|
382 | 340 | def draw_image(self, gc, x, y, im): |
383 | | - im = _unmultipled_rgba8888_to_premultiplied_argb32(im[::-1]) |
| 341 | + im = cbook._unmultipled_rgba8888_to_premultiplied_argb32(im[::-1]) |
384 | 342 | surface = cairo.ImageSurface.create_for_data( |
385 | 343 | im.ravel().data, cairo.FORMAT_ARGB32, |
386 | 344 | im.shape[1], im.shape[0], im.shape[1] * 4) |
@@ -586,7 +544,7 @@ def print_png(self, fobj, *args, **kwargs): |
586 | 544 | def print_rgba(self, fobj, *args, **kwargs): |
587 | 545 | width, height = self.get_width_height() |
588 | 546 | buf = self._get_printed_image_surface().get_data() |
589 | | - fobj.write(_premultiplied_argb32_to_unmultiplied_rgba8888( |
| 547 | + fobj.write(cbook._premultiplied_argb32_to_unmultiplied_rgba8888( |
590 | 548 | np.asarray(buf).reshape((width, height, 4)))) |
591 | 549 |
|
592 | 550 | print_raw = print_rgba |
|
0 commit comments