Py::Object RendererAgg::draw_image(const Py::Tuple& args) { _VERBOSE("RendererAgg::draw_image"); theRasterizer->reset_clipping(); args.verify_length(5); float x = Py::Float(args[0]); float y = Py::Float(args[1]); Image *image = static_cast<Image*>(args[2].ptr()); std::string origin = Py::String(args[3]); if (origin!="lower" && origin!="upper") throw Py::ValueError("origin must be upper|lower"); bool isUpper = origin=="upper"; size_t ind = 0; size_t thisx, thisy; float oy = isUpper ? y : height-y; float minx(0), maxx(width), miny(0), maxy(height); if (args[4].ptr() != Py_None) { Bbox* bbox = static_cast<Bbox*>(args[4].ptr()); minx = bbox->ll_api()->x_api()->val(); maxy = height-bbox->ll_api()->y_api()->val(); maxx = bbox->ur_api()->x_api()->val(); miny = height-bbox->ur_api()->y_api()->val(); } //if (isUpper) oy -= image->rowsOut; //start at top //std::cout << minx << " " << maxx << " " << miny << " " << maxy << std::endl; for (size_t j=0; j<image->rowsOut; j++) { thisy = (size_t)(isUpper ? oy+j : oy-j-0.5); if (thisy<miny || thisy>=maxy) { ind += 4*image->colsOut; continue; } for (size_t i=0; i<image->colsOut; i++) { thisx = (size_t)(i+x); if (thisx<minx || thisx>=maxx) { ind += 4; continue; } pixfmt::color_type p; p.r = *(image->bufferOut+ind++); p.g = *(image->bufferOut+ind++); p.b = *(image->bufferOut+ind++); p.a = *(image->bufferOut+ind++); pixFmt->blend_pixel(thisx, thisy, p, p.a); } } return Py::Object(); }
void RendererAgg::set_clip_from_bbox(const Py::Object& o) { if (o.ptr() != Py_None) { //using clip // Bbox::check(args[0]) failing; something about cross module? // set the clip rectangle // flipy Bbox* clipbox = static_cast<Bbox*>(o.ptr()); double l = clipbox->ll_api()->x_api()->val() ; double b = clipbox->ll_api()->y_api()->val(); double r = clipbox->ur_api()->x_api()->val() ; double t = clipbox->ur_api()->y_api()->val() ; ; theRasterizer->clip_box(l, height-t, r, height-b); } }
static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp* interp, int argc, char **argv) { Tk_PhotoHandle photo; Tk_PhotoImageBlock block; PyObject* aggo; // vars for blitting PyObject* bboxo; Bbox* bbox; agg::int8u *destbuffer; double l,b,r,t; int destx, desty, destwidth, destheight, deststride; long mode; long nval; if (argc != 5) { Tcl_AppendResult(interp, "usage: ", argv[0], " destPhoto srcImage", (char *) NULL); return TCL_ERROR; } /* get Tcl PhotoImage handle */ photo = Tk_FindPhoto(interp, argv[1]); if (photo == NULL) { Tcl_AppendResult(interp, "destination photo must exist", (char *) NULL); return TCL_ERROR; } /* get array (or object that can be converted to array) pointer */ aggo = (PyObject*)atol(argv[2]); RendererAgg *aggRenderer = (RendererAgg *)aggo; int srcheight = (int)aggRenderer->get_height(); /* XXX insert aggRenderer type check */ /* get array mode (0=mono, 1=rgb, 2=rgba) */ mode = atol(argv[3]); if ((mode != 0) && (mode != 1) && (mode != 2)) { Tcl_AppendResult(interp, "illegal image mode", (char *) NULL); return TCL_ERROR; } /* check for bbox/blitting */ bboxo = (PyObject*)atol(argv[4]); if (bboxo != Py_None) { bbox = (Bbox*)bboxo; l = bbox->ll_api()->x_api()->val(); b = bbox->ll_api()->y_api()->val(); r = bbox->ur_api()->x_api()->val(); t = bbox->ur_api()->y_api()->val(); destx = (int)l; desty = srcheight-(int)t; destwidth = (int)(r-l); destheight = (int)(t-b); deststride = 4*destwidth; destbuffer = new agg::int8u[deststride*destheight]; if (destbuffer == NULL) { throw Py::MemoryError("_tkagg could not allocate memory for destbuffer"); } agg::rendering_buffer destrbuf; destrbuf.attach(destbuffer, destwidth, destheight, deststride); pixfmt destpf(destrbuf); renderer_base destrb(destpf); agg::rect_base<int> region(destx, desty, (int)r, srcheight-(int)b); destrb.copy_from(*aggRenderer->renderingBuffer, ®ion, -destx, -desty); } else { bbox = NULL; destbuffer = NULL; destx = desty = destwidth = destheight = deststride = 0; } /* setup tkblock */ block.pixelSize = 1; if (mode == 0) { block.offset[0]= block.offset[1] = block.offset[2] =0; nval = 1; } else { block.offset[0] = 0; block.offset[1] = 1; block.offset[2] = 2; if (mode == 1) { block.offset[3] = 0; block.pixelSize = 3; nval = 3; } else { block.offset[3] = 3; block.pixelSize = 4; nval = 4; } } if (bbox) { block.width = destwidth; block.height = destheight; block.pitch = deststride; block.pixelPtr = destbuffer; Tk_PhotoPutBlock(photo, &block, destx, desty, destwidth, destheight); delete [] destbuffer; } else { block.width = aggRenderer->get_width(); block.height = aggRenderer->get_height(); block.pitch = block.width * nval; block.pixelPtr = aggRenderer->pixBuffer; /* Clear current contents */ Tk_PhotoBlank(photo); /* Copy opaque block to photo image, and leave the rest to TK */ Tk_PhotoPutBlock(photo, &block, 0, 0, block.width, block.height); } return TCL_OK; }