Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8540e92

Browse files
committed
Add clip path support on images. Add a new example and update the dolphin fest.
svn path=/trunk/matplotlib/; revision=6302
1 parent 2525493 commit 8540e92

3 files changed

Lines changed: 79 additions & 7 deletions

File tree

examples/pylab_examples/dolphin.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1+
import matplotlib.cm as cm
12
import matplotlib.pyplot as plt
23
from matplotlib.patches import Circle, PathPatch
34
from matplotlib.path import Path
45
from matplotlib.transforms import Affine2D
56
import numpy as np
67

8+
79
r = np.random.rand(50)
810
t = np.random.rand(50) * np.pi * 2.0
911
x = r * np.cos(t)
1012
y = r * np.sin(t)
1113

1214
fig = plt.figure(figsize=(6,6))
1315
ax = plt.subplot(111)
14-
circle = Circle((0, 0), 1, facecolor=(0,0,0.8),
16+
circle = Circle((0, 0), 1, facecolor='none',
1517
edgecolor=(0,0.8,0.8), linewidth=3, alpha=0.5)
1618
ax.add_patch(circle)
1719

20+
im = plt.imshow(np.random.random((100, 100)),
21+
origin='lower', cmap=cm.winter,
22+
interpolation='spline36',
23+
extent=([-1, 1, -1, 1]), zorder=1000)
24+
im.set_clip_path(circle)
25+
1826
plt.plot(x, y, 'o', color=(0.9, 0.9, 1.0), alpha=0.8)
1927

2028
# Dolphin from OpenClipart library by Andy Fitzsimon
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
import numpy as np
3+
import matplotlib.cm as cm
4+
import matplotlib.mlab as mlab
5+
import matplotlib.pyplot as plt
6+
from matplotlib.path import Path
7+
from matplotlib.patches import PathPatch
8+
9+
delta = 0.025
10+
x = y = np.arange(-3.0, 3.0, delta)
11+
X, Y = np.meshgrid(x, y)
12+
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
13+
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
14+
Z = Z2-Z1 # difference of Gaussians
15+
16+
path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]])
17+
patch = PathPatch(path, facecolor='none')
18+
plt.gca().add_patch(patch)
19+
20+
im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
21+
origin='lower', extent=[-3,3,-3,3],
22+
clip_path=patch, clip_on=True)
23+
im.set_clip_path(patch)
24+
25+
plt.show()
26+

src/_backend_agg.cpp

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -788,20 +788,58 @@ RendererAgg::draw_image(const Py::Tuple& args) {
788788
Py::Object box_obj = args[3];
789789
Py::Object clippath;
790790
agg::trans_affine clippath_trans;
791+
bool has_clippath = false;
792+
793+
theRasterizer->reset_clipping();
794+
rendererBase->reset_clipping(true);
791795
if (args.size() == 6) {
792796
clippath = args[4];
793797
clippath_trans = py_to_agg_transformation_matrix(args[5], false);
798+
has_clippath = render_clippath(clippath, clippath_trans);
794799
}
795800

796-
theRasterizer->reset_clipping();
797-
rendererBase->reset_clipping(true);
798-
set_clipbox(box_obj, rendererBase);
799-
800801
Py::Tuple empty;
801-
pixfmt pixf(*(image->rbufOut));
802802
image->flipud_out(empty);
803+
pixfmt pixf(*(image->rbufOut));
803804

804-
rendererBase->blend_from(pixf, 0, (int)x, (int)(height-(y+image->rowsOut)));
805+
if (has_clippath) {
806+
agg::trans_affine mtx;
807+
mtx *= agg::trans_affine_translation((int)x, (int)(height-(y+image->rowsOut)));
808+
809+
agg::path_storage rect;
810+
rect.move_to(0, 0);
811+
rect.line_to(image->colsOut, 0);
812+
rect.line_to(image->colsOut, image->rowsOut);
813+
rect.line_to(0, image->rowsOut);
814+
rect.line_to(0, 0);
815+
agg::conv_transform<agg::path_storage> rect2(rect, mtx);
816+
817+
agg::trans_affine inv_mtx(mtx);
818+
inv_mtx.invert();
819+
820+
typedef agg::span_allocator<agg::rgba8> color_span_alloc_type;
821+
typedef agg::pixfmt_amask_adaptor<pixfmt, alpha_mask_type> pixfmt_amask_type;
822+
typedef agg::renderer_base<pixfmt_amask_type> amask_ren_type;
823+
typedef agg::image_accessor_clip<agg::pixfmt_rgba32> image_accessor_type;
824+
typedef agg::span_interpolator_linear<> interpolator_type;
825+
typedef agg::span_image_filter_rgba_nn<image_accessor_type, interpolator_type> image_span_gen_type;
826+
typedef agg::renderer_scanline_aa_solid<amask_ren_type> amask_aa_renderer_type;
827+
typedef agg::renderer_scanline_aa<amask_ren_type, color_span_alloc_type, image_span_gen_type> renderer_type;
828+
829+
color_span_alloc_type sa;
830+
image_accessor_type ia(pixf, agg::rgba8(0, 0, 0, 0));
831+
interpolator_type interpolator(inv_mtx);
832+
image_span_gen_type image_span_generator(ia, interpolator);
833+
pixfmt_amask_type pfa(*pixFmt, *alphaMask);
834+
amask_ren_type r(pfa);
835+
renderer_type ri(r, sa, image_span_generator);
836+
837+
theRasterizer->add_path(rect2);
838+
agg::render_scanlines(*theRasterizer, *slineP8, ri);
839+
} else {
840+
set_clipbox(box_obj, rendererBase);
841+
rendererBase->blend_from(pixf, 0, (int)x, (int)(height-(y+image->rowsOut)));
842+
}
805843

806844
image->flipud_out(empty);
807845

0 commit comments

Comments
 (0)