@@ -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