From: <md...@us...> - 2008-10-22 20:03:28
|
Revision: 6302 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6302&view=rev Author: mdboom Date: 2008-10-22 20:03:12 +0000 (Wed, 22 Oct 2008) Log Message: ----------- Add clip path support on images. Add a new example and update the dolphin fest. Modified Paths: -------------- trunk/matplotlib/examples/pylab_examples/dolphin.py trunk/matplotlib/src/_backend_agg.cpp Added Paths: ----------- trunk/matplotlib/examples/pylab_examples/image_clip_path.py Modified: trunk/matplotlib/examples/pylab_examples/dolphin.py =================================================================== --- trunk/matplotlib/examples/pylab_examples/dolphin.py 2008-10-22 19:43:40 UTC (rev 6301) +++ trunk/matplotlib/examples/pylab_examples/dolphin.py 2008-10-22 20:03:12 UTC (rev 6302) @@ -1,3 +1,4 @@ +import matplotlib.cm as cm import matplotlib.pyplot as plt from matplotlib.patches import Circle, PathPatch from matplotlib.path import Path @@ -4,6 +5,7 @@ from matplotlib.transforms import Affine2D import numpy as np + r = np.random.rand(50) t = np.random.rand(50) * np.pi * 2.0 x = r * np.cos(t) @@ -11,10 +13,16 @@ fig = plt.figure(figsize=(6,6)) ax = plt.subplot(111) -circle = Circle((0, 0), 1, facecolor=(0,0,0.8), +circle = Circle((0, 0), 1, facecolor='none', edgecolor=(0,0.8,0.8), linewidth=3, alpha=0.5) ax.add_patch(circle) +im = plt.imshow(np.random.random((100, 100)), + origin='lower', cmap=cm.winter, + interpolation='spline36', + extent=([-1, 1, -1, 1]), zorder=1000) +im.set_clip_path(circle) + plt.plot(x, y, 'o', color=(0.9, 0.9, 1.0), alpha=0.8) # Dolphin from OpenClipart library by Andy Fitzsimon Added: trunk/matplotlib/examples/pylab_examples/image_clip_path.py =================================================================== --- trunk/matplotlib/examples/pylab_examples/image_clip_path.py (rev 0) +++ trunk/matplotlib/examples/pylab_examples/image_clip_path.py 2008-10-22 20:03:12 UTC (rev 6302) @@ -0,0 +1,26 @@ +#!/usr/bin/env python +import numpy as np +import matplotlib.cm as cm +import matplotlib.mlab as mlab +import matplotlib.pyplot as plt +from matplotlib.path import Path +from matplotlib.patches import PathPatch + +delta = 0.025 +x = y = np.arange(-3.0, 3.0, delta) +X, Y = np.meshgrid(x, y) +Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) +Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) +Z = Z2-Z1 # difference of Gaussians + +path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]]) +patch = PathPatch(path, facecolor='none') +plt.gca().add_patch(patch) + +im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray, + origin='lower', extent=[-3,3,-3,3], + clip_path=patch, clip_on=True) +im.set_clip_path(patch) + +plt.show() + Property changes on: trunk/matplotlib/examples/pylab_examples/image_clip_path.py ___________________________________________________________________ Added: svn:executable + * Modified: trunk/matplotlib/src/_backend_agg.cpp =================================================================== --- trunk/matplotlib/src/_backend_agg.cpp 2008-10-22 19:43:40 UTC (rev 6301) +++ trunk/matplotlib/src/_backend_agg.cpp 2008-10-22 20:03:12 UTC (rev 6302) @@ -788,21 +788,59 @@ Py::Object box_obj = args[3]; Py::Object clippath; agg::trans_affine clippath_trans; + bool has_clippath = false; + + theRasterizer->reset_clipping(); + rendererBase->reset_clipping(true); if (args.size() == 6) { clippath = args[4]; clippath_trans = py_to_agg_transformation_matrix(args[5], false); + has_clippath = render_clippath(clippath, clippath_trans); } - theRasterizer->reset_clipping(); - rendererBase->reset_clipping(true); - set_clipbox(box_obj, rendererBase); - Py::Tuple empty; + image->flipud_out(empty); pixfmt pixf(*(image->rbufOut)); - image->flipud_out(empty); - rendererBase->blend_from(pixf, 0, (int)x, (int)(height-(y+image->rowsOut))); + if (has_clippath) { + agg::trans_affine mtx; + mtx *= agg::trans_affine_translation((int)x, (int)(height-(y+image->rowsOut))); + agg::path_storage rect; + rect.move_to(0, 0); + rect.line_to(image->colsOut, 0); + rect.line_to(image->colsOut, image->rowsOut); + rect.line_to(0, image->rowsOut); + rect.line_to(0, 0); + agg::conv_transform<agg::path_storage> rect2(rect, mtx); + + agg::trans_affine inv_mtx(mtx); + inv_mtx.invert(); + + typedef agg::span_allocator<agg::rgba8> color_span_alloc_type; + typedef agg::pixfmt_amask_adaptor<pixfmt, alpha_mask_type> pixfmt_amask_type; + typedef agg::renderer_base<pixfmt_amask_type> amask_ren_type; + typedef agg::image_accessor_clip<agg::pixfmt_rgba32> image_accessor_type; + typedef agg::span_interpolator_linear<> interpolator_type; + typedef agg::span_image_filter_rgba_nn<image_accessor_type, interpolator_type> image_span_gen_type; + typedef agg::renderer_scanline_aa_solid<amask_ren_type> amask_aa_renderer_type; + typedef agg::renderer_scanline_aa<amask_ren_type, color_span_alloc_type, image_span_gen_type> renderer_type; + + color_span_alloc_type sa; + image_accessor_type ia(pixf, agg::rgba8(0, 0, 0, 0)); + interpolator_type interpolator(inv_mtx); + image_span_gen_type image_span_generator(ia, interpolator); + pixfmt_amask_type pfa(*pixFmt, *alphaMask); + amask_ren_type r(pfa); + renderer_type ri(r, sa, image_span_generator); + + theRasterizer->add_path(rect2); + agg::render_scanlines(*theRasterizer, *slineP8, ri); + } else { + set_clipbox(box_obj, rendererBase); + rendererBase->blend_from(pixf, 0, (int)x, (int)(height-(y+image->rowsOut))); + } + image->flipud_out(empty); return Py::Object(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |