|
From: <lee...@us...> - 2010-02-08 16:54:32
|
Revision: 8118
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8118&view=rev
Author: leejjoon
Date: 2010-02-08 16:54:26 +0000 (Mon, 08 Feb 2010)
Log Message:
-----------
RendererAgg.draw_image supports affine transform
Modified Paths:
--------------
trunk/matplotlib/examples/api/demo_affine_image.py
trunk/matplotlib/lib/matplotlib/backends/backend_agg.py
trunk/matplotlib/src/_backend_agg.cpp
Modified: trunk/matplotlib/examples/api/demo_affine_image.py
===================================================================
--- trunk/matplotlib/examples/api/demo_affine_image.py 2010-02-08 16:00:23 UTC (rev 8117)
+++ trunk/matplotlib/examples/api/demo_affine_image.py 2010-02-08 16:54:26 UTC (rev 8118)
@@ -3,7 +3,7 @@
"""
For the backends that supports draw_image with optional affine
-transform (e.g., ps backend), the image of the output should
+transform (e.g., agg, ps backend), the image of the output should
have its boundary matches the red rectangles.
"""
@@ -33,7 +33,8 @@
ax = plt.subplot(111)
Z = get_image()
im = imshow_affine(ax, Z, interpolation='nearest', cmap=cm.jet,
- origin='lower', extent=[-2, 4, -3, 2])
+ origin='lower',
+ extent=[-2, 4, -3, 2], clip_on=True)
trans_data2 = mtransforms.Affine2D().rotate_deg(30) + ax.transData
im.set_transform(trans_data2)
@@ -48,4 +49,5 @@
ax.set_xlim(-3, 5)
ax.set_ylim(-4, 4)
- plt.savefig("demo_affine_image")
+ plt.show()
+ #plt.savefig("demo_affine_image")
Modified: trunk/matplotlib/lib/matplotlib/backends/backend_agg.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backends/backend_agg.py 2010-02-08 16:00:23 UTC (rev 8117)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_agg.py 2010-02-08 16:54:26 UTC (rev 8118)
@@ -261,6 +261,12 @@
# with the Agg backend
return True
+ def option_scale_image(self):
+ """
+ agg backend support arbitrary scaling of image.
+ """
+ return True
+
def restore_region(self, region, bbox=None, xy=None):
"""
restore the saved region. if bbox (instance of BboxBase, or
Modified: trunk/matplotlib/src/_backend_agg.cpp
===================================================================
--- trunk/matplotlib/src/_backend_agg.cpp 2010-02-08 16:00:23 UTC (rev 8117)
+++ trunk/matplotlib/src/_backend_agg.cpp 2010-02-08 16:54:26 UTC (rev 8118)
@@ -814,55 +814,99 @@
RendererAgg::draw_image(const Py::Tuple& args) {
_VERBOSE("RendererAgg::draw_image");
- args.verify_length(4);
+ args.verify_length(4, 7); // 7 if affine matrix if given
GCAgg gc(args[0], dpi);
- double x = mpl_round(Py::Float(args[1]));
- double y = mpl_round(Py::Float(args[2]));
Image *image = static_cast<Image*>(args[3].ptr());
bool has_clippath = false;
+ agg::trans_affine affine_trans;
+ bool has_affine = false;
+ double x, y, w, h;
+
+ if (args.size() == 7) {
+ has_affine = true;
+ x = Py::Float(args[1]);
+ y = Py::Float(args[2]);
+ w = Py::Float(args[4]);
+ h = Py::Float(args[5]);
+ affine_trans = py_to_agg_transformation_matrix(args[6].ptr());
+ } else {
+ x = mpl_round(Py::Float(args[1]));
+ y = mpl_round(Py::Float(args[2]));
+ }
+
+
theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
+ set_clipbox(gc.cliprect, theRasterizer);
has_clippath = render_clippath(gc.clippath, gc.clippath_trans);
Py::Tuple empty;
image->flipud_out(empty);
pixfmt pixf(*(image->rbufOut));
- if (has_clippath) {
+ if (has_affine | has_clippath) {
agg::trans_affine mtx;
- mtx *= agg::trans_affine_translation((int)x, (int)(height-(y+image->rowsOut)));
+ agg::path_storage rect;
- agg::path_storage rect;
+ if (has_affine) {
+ mtx *= agg::trans_affine_scaling(1, -1);
+ mtx *= agg::trans_affine_translation(0, image->rowsOut);
+ mtx *= agg::trans_affine_scaling(w/(image->colsOut), h/(image->rowsOut));
+ mtx *= agg::trans_affine_translation(x, y);
+ mtx *= affine_trans;
+ mtx *= agg::trans_affine_scaling(1.0, -1.0);
+ mtx *= agg::trans_affine_translation(0.0, (double) height);
+ } else {
+ mtx *= agg::trans_affine_translation((int)x, (int)(height-(y+image->rowsOut)));
+ }
+
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<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);
+
+ if (has_clippath) {
+ typedef agg::pixfmt_amask_adaptor<pixfmt, alpha_mask_type> pixfmt_amask_type;
+ typedef agg::renderer_base<pixfmt_amask_type> amask_ren_type;
+ typedef agg::renderer_scanline_aa<amask_ren_type, color_span_alloc_type, image_span_gen_type> renderer_type_alpha;
+
+ pixfmt_amask_type pfa(pixFmt, alphaMask);
+ amask_ren_type r(pfa);
+ renderer_type_alpha ri(r, sa, image_span_generator);
+
+ theRasterizer.add_path(rect2);
+ agg::render_scanlines(theRasterizer, slineP8, ri);
+ } else {
+ typedef agg::renderer_base<pixfmt> ren_type;
+ typedef agg::renderer_scanline_aa<ren_type, color_span_alloc_type, image_span_gen_type> renderer_type;
+ ren_type r(pixFmt);
+ renderer_type ri(r, sa, image_span_generator);
+
+ theRasterizer.add_path(rect2);
+ agg::render_scanlines(theRasterizer, slineP8, ri);
+ }
+
} else {
set_clipbox(gc.cliprect, rendererBase);
rendererBase.blend_from(pixf, 0, (int)x, (int)(height-(y+image->rowsOut)));
@@ -873,6 +917,9 @@
return Py::Object();
}
+
+
+
template<class path_t>
void RendererAgg::_draw_path(path_t& path, bool has_clippath,
const facepair_t& face, const GCAgg& gc) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|