Rect absolute_frame(View* view) { Rect ret = view->frame; //find root view View* v = view; while (v->superview) { v = v->superview; ret = convert_frame(v, ret); } //find containing window Window* win = containing_window(v); ASSERT(win, "couldn't find container window!"); return convert_rect(win->frame, ret); }
static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc, char **argv) { Tk_PhotoHandle photo; Tk_PhotoImageBlock block; PyObject *bufferobj; // vars for blitting PyObject *bboxo; size_t aggl, bboxl; bool has_bbox; uint8_t *destbuffer; int destx, desty, destwidth, destheight, deststride; //unsigned long tmp_ptr; long mode; long nval; if (TK_MAIN_WINDOW(interp) == NULL) { // Will throw a _tkinter.TclError with "this isn't a Tk application" return TCL_ERROR; } if (argc != 5) { TCL_APPEND_RESULT(interp, "usage: ", argv[0], " destPhoto srcImage", (char *)NULL); return TCL_ERROR; } /* get Tcl PhotoImage handle */ photo = TK_FIND_PHOTO(interp, argv[1]); if (photo == NULL) { TCL_APPEND_RESULT(interp, "destination photo must exist", (char *)NULL); return TCL_ERROR; } /* get array (or object that can be converted to array) pointer */ if (sscanf(argv[2], SIZE_T_FORMAT, &aggl) != 1) { TCL_APPEND_RESULT(interp, "error casting pointer", (char *)NULL); return TCL_ERROR; } bufferobj = (PyObject *)aggl; numpy::array_view<uint8_t, 3> buffer; try { buffer = numpy::array_view<uint8_t, 3>(bufferobj); } catch (...) { TCL_APPEND_RESULT(interp, "buffer is of wrong type", (char *)NULL); PyErr_Clear(); return TCL_ERROR; } int srcheight = buffer.dim(0); /* 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_APPEND_RESULT(interp, "illegal image mode", (char *)NULL); return TCL_ERROR; } /* check for bbox/blitting */ if (sscanf(argv[4], SIZE_T_FORMAT, &bboxl) != 1) { TCL_APPEND_RESULT(interp, "error casting pointer", (char *)NULL); return TCL_ERROR; } bboxo = (PyObject *)bboxl; if (bboxo != NULL && bboxo != Py_None) { agg::rect_d rect; if (!convert_rect(bboxo, &rect)) { return TCL_ERROR; } has_bbox = true; destx = (int)rect.x1; desty = srcheight - (int)rect.y2; destwidth = (int)(rect.x2 - rect.x1); destheight = (int)(rect.y2 - rect.y1); deststride = 4 * destwidth; destbuffer = new agg::int8u[deststride * destheight]; if (destbuffer == NULL) { TCL_APPEND_RESULT(interp, "could not allocate memory", (char *)NULL); return TCL_ERROR; } for (int i = 0; i < destheight; ++i) { memcpy(destbuffer + (deststride * i), &buffer(i + desty, destx, 0), deststride); } } else { has_bbox = false; 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 (has_bbox) { block.width = destwidth; block.height = destheight; block.pitch = deststride; block.pixelPtr = destbuffer; TK_PHOTO_PUT_BLOCK_NO_COMPOSITE(photo, &block, destx, desty, destwidth, destheight); delete[] destbuffer; } else { block.width = buffer.dim(1); block.height = buffer.dim(0); block.pitch = (int)block.width * nval; block.pixelPtr = buffer.data(); /* Clear current contents */ TK_PHOTO_BLANK(photo); /* Copy opaque block to photo image, and leave the rest to TK */ TK_PHOTO_PUT_BLOCK_NO_COMPOSITE(photo, &block, 0, 0, block.width, block.height); } return TCL_OK; }
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; size_t aggl, bboxl; bool has_bbox; agg::int8u *destbuffer; int destx, desty, destwidth, destheight, deststride; //unsigned long tmp_ptr; long mode; long nval; if (Tk_MainWindow(interp) == NULL) { // Will throw a _tkinter.TclError with "this isn't a Tk application" return TCL_ERROR; } 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 */ if (sscanf(argv[2], SIZE_T_FORMAT, &aggl) != 1) { Tcl_AppendResult(interp, "error casting pointer", (char *)NULL); return TCL_ERROR; } aggo = (PyObject *)aggl; // TODO: This is really brittle and will break when RendererAgg // comes in multiple flavors RendererAgg *aggRenderer = ((PyRendererAgg *)(aggo))->x; 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 */ if (sscanf(argv[4], SIZE_T_FORMAT, &bboxl) != 1) { Tcl_AppendResult(interp, "error casting pointer", (char *)NULL); return TCL_ERROR; } bboxo = (PyObject *)bboxl; if (bboxo != NULL && bboxo != Py_None) { agg::rect_d rect; if (!convert_rect(bboxo, &rect)) { return TCL_ERROR; } has_bbox = true; destx = (int)rect.x1; desty = srcheight - (int)rect.y2; destwidth = (int)(rect.x2 - rect.x1); destheight = (int)(rect.y2 - rect.y1); deststride = 4 * destwidth; destbuffer = new agg::int8u[deststride * destheight]; if (destbuffer == NULL) { Tcl_AppendResult(interp, "could not allocate memory", (char *)NULL); return TCL_ERROR; } 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)rect.x2, srcheight - (int)rect.y1); destrb.copy_from(aggRenderer->renderingBuffer, ®ion, -destx, -desty); } else { has_bbox = false; 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 (has_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 = (int)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; }
Rect convert_frame(View* view, Rect frame) { if (!view) return frame; Rect ret = convert_rect(view->frame, frame); return ret; }