Skip to content

Broken alpha handling in images #545

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 2 commits into from
Oct 28, 2011
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
4 changes: 4 additions & 0 deletions lib/matplotlib/backends/backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,10 @@ def draw_image(self, gc, x, y, im, dx=None, dy=None, transform=None):
im.flipud_out()
attrib['xlink:href'] = filename

alpha = gc.get_alpha()
if alpha != 1.0:
attrib['opacity'] = str(alpha)

if transform is None:
self.writer.element(
'image',
Expand Down
14 changes: 9 additions & 5 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def _get_unsampled_image(self, A, image_extents, viewlim):
im.is_grayscale = False
else:
if self._rgbacache is None:
x = self.to_rgba(self._A, self._alpha, bytes=True)
x = self.to_rgba(self._A, bytes=True)
self._rgbacache = x
else:
x = self._rgbacache
Expand Down Expand Up @@ -345,6 +345,7 @@ def draw(self, renderer, *args, **kwargs):
gc = renderer.new_gc()
gc.set_clip_rectangle(self.axes.bbox.frozen())
gc.set_clip_path(self.get_clip_path())
gc.set_alpha(self.get_alpha())

if self._check_unsampled_image(renderer):
self._draw_unsampled_image(renderer, gc)
Expand Down Expand Up @@ -722,7 +723,7 @@ def set_data(self, x, y, A):
A.shape = A.shape[0:2]
if len(A.shape) == 2:
if A.dtype != np.uint8:
A = self.to_rgba(A, alpha=self._alpha, bytes=True)
A = self.to_rgba(A, bytes=True)
self.is_grayscale = self.cmap.is_gray()
else:
A = np.repeat(A[:,:,np.newaxis], 4, 2)
Expand Down Expand Up @@ -824,7 +825,7 @@ def make_image(self, magnification=1.0):
width = width * magnification
height = height * magnification
if self._rgbacache is None:
A = self.to_rgba(self._A, alpha=self._alpha, bytes=True)
A = self.to_rgba(self._A, bytes=True)
self._rgbacache = A
if self._A.ndim == 2:
self.is_grayscale = self.cmap.is_gray()
Expand All @@ -851,6 +852,7 @@ def draw(self, renderer, *args, **kwargs):
gc = renderer.new_gc()
gc.set_clip_rectangle(self.axes.bbox.frozen())
gc.set_clip_path(self.get_clip_path())
gc.set_alpha(self.get_alpha())
renderer.draw_image(gc,
round(self.axes.bbox.xmin),
round(self.axes.bbox.ymin),
Expand Down Expand Up @@ -974,7 +976,7 @@ def make_image(self, magnification=1.0):
if self._A is None:
raise RuntimeError('You must first set the image array')

x = self.to_rgba(self._A, self._alpha, bytes=True)
x = self.to_rgba(self._A, bytes=True)
self.magnification = magnification
# if magnification is not one, we need to resize
ismag = magnification!=1
Expand Down Expand Up @@ -1008,6 +1010,7 @@ def draw(self, renderer, *args, **kwargs):
gc = renderer.new_gc()
gc.set_clip_rectangle(self.figure.bbox)
gc.set_clip_path(self.get_clip_path())
gc.set_alpha(self.get_alpha())
renderer.draw_image(gc, round(self.ox), round(self.oy), im)
gc.restore()

Expand Down Expand Up @@ -1096,7 +1099,7 @@ def make_image(self, renderer, magnification=1.0):
im.is_grayscale = False
else:
if self._rgbacache is None:
x = self.to_rgba(self._A, self._alpha, bytes=True)
x = self.to_rgba(self._A, bytes=True)
self._rgbacache = x
else:
x = self._rgbacache
Expand Down Expand Up @@ -1148,6 +1151,7 @@ def draw(self, renderer, *args, **kwargs):
l, b, r, t = self.get_window_extent(renderer).extents
gc = renderer.new_gc()
self._set_gc_clip(gc)
gc.set_alpha(self.get_alpha())
#gc.set_clip_path(self.get_clip_path())
renderer.draw_image(gc, round(l), round(b), im)
gc.restore()
Expand Down
6 changes: 5 additions & 1 deletion src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ RendererAgg::draw_image(const Py::Tuple& args)
agg::trans_affine affine_trans;
bool has_affine = false;
double x, y, w, h;
double alpha;

if (args.size() == 7)
{
Expand All @@ -1006,6 +1007,8 @@ RendererAgg::draw_image(const Py::Tuple& args)
warnings from the compiler */
}

alpha = gc.alpha;

theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(gc.cliprect, theRasterizer);
Expand Down Expand Up @@ -1097,7 +1100,8 @@ RendererAgg::draw_image(const Py::Tuple& args)
else
{
set_clipbox(gc.cliprect, rendererBase);
rendererBase.blend_from(pixf, 0, (int)x, (int)(height - (y + image->rowsOut)));
rendererBase.blend_from(
pixf, 0, (int)x, (int)(height - (y + image->rowsOut)), alpha * 255);
}

rendererBase.reset_clipping(true);
Expand Down
2 changes: 1 addition & 1 deletion src/_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "mplutils.h"


typedef agg::pixfmt_rgba32 pixfmt;
typedef agg::pixfmt_rgba32_pre pixfmt;
typedef agg::renderer_base<pixfmt> renderer_base;
typedef agg::span_interpolator_linear<> interpolator_type;
typedef agg::rasterizer_scanline_aa<agg::rasterizer_sl_clip_dbl> rasterizer;
Expand Down