int read_image (Tcl_Interp* interp, char path[128], unsigned char *img) { int i, j; Tk_PhotoHandle img_handle; Tk_PhotoImageBlock img_block; if (tiff_flag) { sprintf(val, "temp read %s", path); Tcl_Eval(interp, val); img_handle = Tk_FindPhoto( interp, "temp"); Tk_PhotoGetImage (img_handle, &img_block); for ( j=0;j<imgsize;j++) { i=4*j; *(img +j) = *(img_block.pixelPtr + i); } } else { fp1 = fopen_rp (path); // replaced fopen_r, ad holten 12-2012 if (!fp1) return 0; fread (img, 1, imgsize, fp1); fclose (fp1); img_handle = Tk_FindPhoto( interp, "temp"); Tk_PhotoGetImage (img_handle, &img_block); tclimg2cimg (interp, img, &img_block); } return 1; }
void camltk_setimgdata_native (value imgname, value pixmap, value x, value y, value w, value h) /* ML */ { Tk_PhotoHandle ph; Tk_PhotoImageBlock pib; int code; #if (TK_MAJOR_VERSION < 8) if (NULL == (ph = Tk_FindPhoto(String_val(imgname)))) tk_error("no such image"); #else if (NULL == (ph = Tk_FindPhoto(cltclinterp, String_val(imgname)))) tk_error("no such image"); #endif pib.pixelPtr = String_val(pixmap); pib.width = Int_val(w); pib.height = Int_val(h); pib.pitch = pib.width * 3; pib.pixelSize = 3; pib.offset[0] = 0; pib.offset[1] = 1; pib.offset[2] = 2; Tk_PhotoPutBlock( #if (TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION >= 5 || TK_MAJOR_VERSION > 8) NULL, #endif ph,&pib,Int_val(x),Int_val(y),Int_val(w),Int_val(h) #if (TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION >= 4 || TK_MAJOR_VERSION > 8) , TK_PHOTO_COMPOSITE_SET #endif ); }
value camltk_getimgdata (value imgname) /* ML */ { Tk_PhotoHandle ph; Tk_PhotoImageBlock pib; int code,size; value res; #if (TK_MAJOR_VERSION < 8) if (NULL == (ph = Tk_FindPhoto(String_val(imgname)))) tk_error("no such image"); #else if (NULL == (ph = Tk_FindPhoto(cltclinterp, String_val(imgname)))) tk_error("no such image"); #endif code = Tk_PhotoGetImage(ph,&pib); /* never fails ? */ size = pib.width * pib.height * pib.pixelSize; res = alloc_string(size); /* no holes, default format ? */ if ((pib.pixelSize == 3) && (pib.pitch == (pib.width * pib.pixelSize)) && (pib.offset[0] == 0) && (pib.offset[1] == 1) && (pib.offset[2] == 2)) { memcpy(pib.pixelPtr, String_val(res),size); return res; } else { int y; /* varies from 0 to height - 1 */ int yoffs = 0; /* byte offset of line in src */ int yidx = 0; /* byte offset of line in dst */ for (y=0; y<pib.height; y++,yoffs+=pib.pitch,yidx+=pib.width * 3) { int x; /* varies from 0 to width - 1 */ int xoffs = yoffs; /* byte offset of pxl in src */ int xidx = yidx; /* byte offset of pxl in dst */ for (x=0; x<pib.width; x++,xoffs+=pib.pixelSize,xidx+=3) { Byte(res, xidx) = pib.pixelPtr[xoffs+pib.offset[0]]; Byte(res, xidx + 1) = pib.pixelPtr[xoffs+pib.offset[1]]; Byte(res, xidx + 2) = pib.pixelPtr[xoffs+pib.offset[2]]; }; } return res; } }
static void UpdateImage( TreeDragImage dragImage /* Drag image record. */ ) { TreeCtrl *tree = dragImage->tree; Tk_PhotoHandle photoH; XImage *ximage; int width = dragImage->bounds[2] - dragImage->bounds[0]; int height = dragImage->bounds[3] - dragImage->bounds[1]; int alpha = 128; XColor *colorPtr; if (dragImage->image != NULL) { Tk_FreeImage(dragImage->image); dragImage->image = NULL; } photoH = Tk_FindPhoto(tree->interp, "::TreeCtrl::ImageDrag"); if (photoH == NULL) { Tcl_GlobalEval(tree->interp, "image create photo ::TreeCtrl::ImageDrag"); photoH = Tk_FindPhoto(tree->interp, "::TreeCtrl::ImageDrag"); if (photoH == NULL) return; } /* Pixmap -> XImage */ ximage = XGetImage(tree->display, dragImage->pixmap, 0, 0, (unsigned int)width, (unsigned int)height, AllPlanes, ZPixmap); if (ximage == NULL) panic("tkTreeDrag.c:UpdateImage() ximage is NULL"); /* XImage -> Tk_Image */ colorPtr = Tk_GetColor(tree->interp, tree->tkwin, "pink"); Tree_XImage2Photo(tree->interp, photoH, ximage, colorPtr->pixel, alpha); XDestroyImage(ximage); dragImage->image = Tk_GetImage(tree->interp, tree->tkwin, "::TreeCtrl::ImageDrag", NULL, (ClientData) NULL); }
/* *--------------------------------------------------------------------------- * * HtmlImageAlphaChannel -- * * Results: * * 1 if there are one or more pixels in the image with an alpha * alpha-channel value of other than 100%. Otherwise 0. * * Side effects: * None. * *--------------------------------------------------------------------------- */ int HtmlImageAlphaChannel (HtmlImage2 *pImage) { HtmlImage2 *p = (pImage->pUnscaled ? pImage->pUnscaled : pImage); if (p->eAlpha == ALPHA_CHANNEL_UNKNOWN) { HtmlTree *pTree= pImage->pImageServer->pTree; Tk_PhotoHandle photo; Tk_PhotoImageBlock block; int x, y; int w = p->width; int h = p->height; Tcl_Obj *pCompressed = getImageCompressed(pImage); unsigned char *zCompressed; int nCompressed; int i; assert(pCompressed); zCompressed = Tcl_GetByteArrayFromObj(pCompressed, &nCompressed); for(i = 0; 1 && i < 16 && i < (nCompressed-4); i++){ if (zCompressed[i] == 'J' && zCompressed[i+1] == 'F' && zCompressed[i+2] == 'I' && zCompressed[i+3] == 'F' ) { p->eAlpha = ALPHA_CHANNEL_FALSE; return 0; } } p->eAlpha = ALPHA_CHANNEL_FALSE; photo = Tk_FindPhoto(pTree->interp, Tcl_GetString(p->pImageName)); if (!photo) return 0; Tk_PhotoGetImage(photo, &block); if (!block.pixelPtr) return 0; for (y = 0; y < h; y++) { unsigned char *z = &block.pixelPtr[block.pitch*y+block.offset[3]]; for (x = 0; x < w; x++) { if (*z != 255) { p->eAlpha = ALPHA_CHANNEL_TRUE; return 1; } z += block.pixelSize; } } } return ((p->eAlpha == ALPHA_CHANNEL_TRUE) ? 1 : 0); }
int pre_processing_c () { int i_img, sup; //Tk_PhotoHandle img_handle; //Tk_PhotoImageBlock img_block; if( verbose ) printf( "Filtering with Highpass"); //Tcl_SetVar(interp, "tbuf", val, TCL_GLOBAL_ONLY); //Tcl_Eval(interp, ".text delete 2"); //Tcl_Eval(interp, ".text insert 2 $tbuf"); /* read support of unsharp mask */ fpp = fopen ("parameters/unsharp_mask.par", "r"); if ( fpp == 0) { sup = 12;} else { fscanf (fpp, "%d\n", &sup); fclose (fpp); } for (i_img=0; i_img<n_img; i_img++) { highpass (img_name[i_img], img[i_img], img[i_img], sup, 0, chfield, i_img); if (display) { #if 0 img_handle = Tk_FindPhoto( interp, "temp"); Tk_PhotoGetImage (img_handle, &img_block); tclimg2cimg (interp, img[i_img], &img_block); sprintf(val, "newimage %d", i_img+1); Tcl_GlobalEval(interp, val); #endif } } if(verbose)printf( "...done\n"); //Tcl_SetVar(interp, "tbuf", val, TCL_GLOBAL_ONLY); //Tcl_Eval(interp, ".text delete 3"); //Tcl_Eval(interp, ".text insert 3 $tbuf"); return TCL_OK; }
static int PyImagingPhotoGet(ClientData clientdata, Tcl_Interp* interp, int argc, char **argv) { Tk_PhotoHandle photo; Tk_PhotoImageBlock block; if (argc != 2) { Tcl_AppendResult(interp, "usage: ", argv[0], " srcPhoto", (char *) NULL); return TCL_ERROR; } /* get Tcl PhotoImage handle */ photo = Tk_FindPhoto(interp, argv[1]); if (photo == NULL) { Tcl_AppendResult( interp, "source photo must exist", (char *) NULL ); return TCL_ERROR; } Tk_PhotoGetImage(photo, &block); printf("pixelPtr = %p\n", block.pixelPtr); printf("width = %d\n", block.width); printf("height = %d\n", block.height); printf("pitch = %d\n", block.pitch); printf("pixelSize = %d\n", block.pixelSize); printf("offset = %d %d %d %d\n", block.offset[0], block.offset[1], block.offset[2], block.offset[3]); Tcl_AppendResult( interp, "this function is not yet supported", (char *) NULL ); return TCL_ERROR; }
int oddeven_c () { int i_img, sup=12; //Tk_PhotoHandle img_handle; //Tk_PhotoImageBlock img_block; if( verbose ) printf( "Odd/even"); //Tcl_SetVar(interp, "tbuf", val, TCL_GLOBAL_ONLY); //Tcl_Eval(interp, ".text delete 2"); //Tcl_Eval(interp, ".text insert 2 $tbuf"); for (i_img=0; i_img<n_img; i_img++) { oddeven (img_name[i_img], img[i_img], img[i_img], sup, 0, chfield, i_img); if (display) { #if 0 img_handle = Tk_FindPhoto( interp, "temp"); Tk_PhotoGetImage (img_handle, &img_block); tclimg2cimg (interp, img[i_img], &img_block); sprintf(val, "newimage %d", i_img+1); Tcl_GlobalEval(interp, val); #endif } } if(verbose) printf( "...done\n"); //Tcl_SetVar(interp, "tbuf", val, TCL_GLOBAL_ONLY); //Tcl_Eval(interp, ".text delete 3"); //Tcl_Eval(interp, ".text insert 3 $tbuf"); return TCL_OK; }
static int PyImagingPhotoPut(ClientData clientdata, Tcl_Interp* interp, int argc, char **argv) { Imaging im; Tk_PhotoHandle photo; Tk_PhotoImageBlock block; if (argc != 3) { 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 PIL Image handle */ im = ImagingFind(argv[2]); if (!im) { Tcl_AppendResult(interp, "bad name", (char*) NULL); return TCL_ERROR; } if (!im->block) { Tcl_AppendResult(interp, "bad display memory", (char*) NULL); return TCL_ERROR; } /* Active region */ #if 0 if (src_xoffset + xsize > im->xsize) xsize = im->xsize - src_xoffset; if (src_yoffset + ysize > im->ysize) ysize = im->ysize - src_yoffset; if (xsize < 0 || ysize < 0 || src_xoffset >= im->xsize || src_yoffset >= im->ysize) return TCL_OK; #endif /* Mode */ if (strcmp(im->mode, "1") == 0 || strcmp(im->mode, "L") == 0) { block.pixelSize = 1; block.offset[0] = block.offset[1] = block.offset[2] = 0; } else if (strncmp(im->mode, "RGB", 3) == 0) { block.pixelSize = 4; block.offset[0] = 0; block.offset[1] = 1; block.offset[2] = 2; if (strcmp(im->mode, "RGBA") == 0) block.offset[3] = 3; /* alpha (or reserved, under 8.2) */ else block.offset[3] = 0; /* no alpha */ } else { Tcl_AppendResult(interp, "Bad mode", (char*) NULL); return TCL_ERROR; } block.width = im->xsize; block.height = im->ysize; block.pitch = im->linesize; block.pixelPtr = (unsigned char*) im->block; #if 0 block.pixelPtr = (unsigned char*) im->block + src_yoffset * im->linesize + src_xoffset * im->pixelsize; #endif #if TK < 84 /* < 8.4.0 */ if (strcmp(im->mode, "RGBA") == 0) { /* Copy non-transparent pixels to photo image */ int x, y; Tk_PhotoImageBlock run; /* Clear current contents */ Tk_PhotoBlank(photo); /* Setup run descriptor */ run.height = 1; run.pitch = block.pitch; run.pixelSize = block.pixelSize; run.offset[0] = 0; run.offset[1] = 1; run.offset[2] = 2; run.offset[3] = 0; /* no alpha (or reserved, under 8.2) */ /* Copy opaque runs to photo image */ for (y = 0; y < block.height; y++) { unsigned char* p = block.pixelPtr + y*block.pitch; unsigned char* s = p; int w = 0; for (x = 0; x < block.width; x++) { if (p[3]) { /* opaque: add pixel to current run */ if (w == 0) s = p; w = w + 1; } else if (s) { /* copy run to photo image */ if (w > 0) { run.width = w; run.pixelPtr = s; Tk_PhotoPutBlock(photo, &run, x-w, y, run.width, 1); } w = 0; } p += block.pixelSize; } if (w > 0) { /* copy final run, if any */ run.width = w; run.pixelPtr = s; Tk_PhotoPutBlock(photo, &run, x-w, y, run.width, 1); } } } else /* Copy opaque block to photo image, and leave the rest to TK */ Tk_PhotoPutBlock(photo, &block, 0, 0, block.width, block.height); #else /* Tk 8.4 and newer */ #if TK < 85 /* Tk 8.4 */ Tk_PhotoPutBlock(photo, &block, 0, 0, block.width, block.height, TK_PHOTO_COMPOSITE_SET); if (strcmp(im->mode, "RGBA") == 0) /* Tk workaround: we need apply ToggleComplexAlphaIfNeeded */ /* (fixed in Tk 8.5a3) */ Tk_PhotoSetSize(photo, block.width, block.height); #else /* Tk 8.5 */ Tk_PhotoPutBlock(interp, photo, &block, 0, 0, block.width, block.height, TK_PHOTO_COMPOSITE_SET); #endif #endif return TCL_OK; }
int flow_demo_c (ClientData clientData, Tcl_Interp* interp, int argc, const char** argv) { int i, i_seq, nr, j,pft_version=3,num_points,dumy; char name[128]; unsigned char *imgf; Tk_PhotoHandle img_handle; Tk_PhotoImageBlock img_block; nr = atoi(argv[1]); fpp = fopen_r ("parameters/sequence.par"); for (i=0; i<4; i++) fscanf (fpp, "%s\n", seq_name[i]); /* name of sequence */ fscanf (fpp,"%d\n", &seq_first); fscanf (fpp,"%d\n", &seq_last); fclose (fpp); /* allocate memory */ imgf = (unsigned char *) calloc (imgsize, 1); fpp = fopen ("parameters/pft_version.par", "r"); if (fpp){ fscanf (fpp, "%d\n", &pft_version); pft_version=pft_version+3; fclose (fpp); } else{ fpp = fopen ("parameters/pft_version.par", "w"); fprintf(fpp,"%d\n", 0); fclose(fpp); } /* load and display images */ for (i_seq=seq_first; i_seq<=seq_last; i_seq++){ compose_name_plus_nr (seq_name[nr], "", i_seq, name); fp1 = fopen_r (name); if (! fp1) return TCL_OK; sprintf (buf, "display camera %d, image %d", nr+1, i_seq); Tcl_SetVar(interp, "tbuf", buf, TCL_GLOBAL_ONLY); Tcl_Eval(interp, ".text delete 2"); Tcl_Eval(interp, ".text insert 2 $tbuf"); read_image (interp, name, imgf); fclose (fp1); img_handle = Tk_FindPhoto( interp, "temp"); Tk_PhotoGetImage (img_handle, &img_block); sprintf(buf, "newimage %d", nr+1); Tcl_Eval(interp, buf); if(pft_version==4){ sprintf (filename, "%s%s", name,"_targets"); /* read targets of camera nr*/ nt4[3][nr]=0; fp1= fopen (filename, "r"); if (! fp1) printf("Can't open ascii file: %s\n", filename); fscanf (fp1, "%d\n", &nt4[3][nr]); for (j=0; j<nt4[3][nr]; j++){ fscanf (fp1, "%4d %lf %lf %d %d %d %d %d\n", &pix[nr][j].pnr, &pix[nr][j].x, &pix[nr][j].y, &pix[nr][j].n , &pix[nr][j].nx ,&pix[nr][j].ny, &pix[nr][j].sumg, &pix[nr][j].tnr); } fclose (fp1); num[nr] = nt4[3][nr]; if (display){ for (j=0; j<num[nr]; j++){ drawcross (interp, (int) pix[nr][j].x, (int) pix[nr][j].y,cr_sz, nr, "blue"); } printf ("drawing %d 2d ", num[nr]); } sprintf (filename, "res/rt_is.%d", i_seq); fp1= fopen (filename, "r"); if (fp1){ fscanf (fp1, "%d\n", &num_points); for (j=0; j<num_points; j++){ if (n_img==4){ fscanf(fp1, "%d %lf %lf %lf %d %d %d %d\n", &dumy, &fix[j].x, &fix[j].y, &fix[j].z, &geo[0][j].pnr, &geo[1][j].pnr, &geo[2][j].pnr, &geo[3][j].pnr); } if (n_img==3){ fscanf(fp1, "%d %lf %lf %lf %d %d %d %d\n", &dumy, &fix[j].x, &fix[j].y, &fix[j].z, &geo[0][j].pnr, &geo[1][j].pnr, &geo[2][j].pnr); } if (n_img==2){ // Alex's patch. 24.09.09. Working on Wesleyan data of 2 cameras only fscanf(fp1, "%d %lf %lf %lf %d %d %d %d\n", &dumy, &fix[j].x, &fix[j].y, &fix[j].z, &geo[0][j].pnr, &geo[1][j].pnr); } } fclose (fp1); if (display){ for (j=0; j<num_points; j++){ img_coord (fix[j].x,fix[j].y,fix[j].z, Ex[nr], I[nr], G[nr], ap[nr], mmp, &pix[nr][j].x,&pix[nr][j].y); metric_to_pixel (pix[nr][j].x,pix[nr][j].y, imx,imy, pix_x,pix_y, &pix[nr][j].x,&pix[nr][j].y, chfield); //if(geo[nr][j].pnr>-1){ // drawcross (interp, (int) pix[nr][geo[nr][j].pnr].x, (int) pix[nr][geo[nr][j].pnr].y,cr_sz, nr, "yellow"); //} drawcross (interp, (int) pix[nr][j].x, (int) pix[nr][j].y,cr_sz, nr, "red"); } printf ("and %d corresponding 3d positions for frame %d\n", num_points,i_seq); } } } Tcl_Eval(interp, "update idletasks"); } printf ("done\n\n"); free (imgf); 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; unsigned long aggl, bboxl; bool has_bbox; agg::int8u *destbuffer; double l, b, r, t; 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], "%lu", &aggl) != 1) { Tcl_AppendResult(interp, "error casting pointer", (char *) NULL); return TCL_ERROR; } aggo = (PyObject*)aggl; //aggo = (PyObject*)atol(argv[2]); //std::stringstream agg_ptr_ss; //agg_ptr_ss.str(argv[2]); //agg_ptr_ss >> tmp_ptr; //aggo = (PyObject*)tmp_ptr; 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 */ if (sscanf(argv[4], "%lu", &bboxl) != 1) { Tcl_AppendResult(interp, "error casting pointer", (char *) NULL); return TCL_ERROR; } bboxo = (PyObject*)bboxl; //bboxo = (PyObject*)atol(argv[4]); //std::stringstream bbox_ptr_ss; //bbox_ptr_ss.str(argv[4]); //bbox_ptr_ss >> tmp_ptr; //bboxo = (PyObject*)tmp_ptr; if (py_convert_bbox(bboxo, l, b, r, t)) { has_bbox = true; 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 { 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 = 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; }
Tk_Image HtmlImageImage(HtmlImage2 *pImage) { assert(pImage && (pImage->isValid == 1 || pImage->isValid == 0)); if (!pImage->isValid) { /* pImage->image is invalid. This happens if the underlying Tk * image, or the image that this is a scaled copy of, is changed * or deleted. It also happens the first time this function is * called after a call to HtmlImageScale(). */ Tk_PhotoHandle photo; Tk_PhotoImageBlock block; Tcl_Interp *interp = pImage->pImageServer->pTree->interp; HtmlImage2 *pUnscaled = pImage->pUnscaled; if (pUnscaled->pixmap) { Tcl_Obj *apObj[4]; int rc; /*printf("TODO: BAD. Have to recreate image to make scaled copy.\n");*/ apObj[0] = pUnscaled->pImageName; apObj[1] = Tcl_NewStringObj("configure", -1); apObj[2] = Tcl_NewStringObj("-data", -1); apObj[3] = pUnscaled->pCompressed; Tcl_IncrRefCount(apObj[1]); Tcl_IncrRefCount(apObj[2]); Tcl_IncrRefCount(apObj[3]); pUnscaled->nIgnoreChange++; rc = Tcl_EvalObjv(interp, 4, apObj, TCL_EVAL_GLOBAL); pUnscaled->nIgnoreChange--; assert(rc==TCL_OK); Tcl_IncrRefCount(apObj[3]); Tcl_DecrRefCount(apObj[2]); Tcl_DecrRefCount(apObj[1]); } assert(pUnscaled); if (!pImage->pImageName) { /* If pImageName is still NULL, then create a new photo * image to write the scaled data to. Todo: Is it possible * to do this without invoking a script, creating the Tcl * command etc.? */ Tk_Window win = pImage->pImageServer->pTree->tkwin; Tcl_Interp *interp = pImage->pImageServer->pTree->interp; const char *z; Tcl_Eval(interp, "image create photo"); pImage->pImageName = Tcl_GetObjResult(interp); Tcl_IncrRefCount(pImage->pImageName); assert(0 == pImage->pDelete); assert(0 == pImage->image); z = Tcl_GetString(pImage->pImageName); pImage->image = Tk_GetImage(interp, win, z, imageChanged, pImage); } assert(pImage->image); CHECK_INTEGER_PLAUSIBILITY(pImage->width); CHECK_INTEGER_PLAUSIBILITY(pImage->height); CHECK_INTEGER_PLAUSIBILITY(pUnscaled->width); CHECK_INTEGER_PLAUSIBILITY(pUnscaled->height); /* Write the scaled data into image pImage->image */ photo = Tk_FindPhoto(interp, Tcl_GetString(pUnscaled->pImageName)); if (photo) { Tk_PhotoGetImage(photo, &block); } if (photo && block.pixelPtr) { int x, y; /* Iterator variables */ int w, h; /* Width and height of unscaled image */ int sw, sh; /* Width and height of scaled image */ Tk_PhotoHandle s_photo; Tk_PhotoImageBlock s_block; sw = pImage->width; sh = pImage->height; w = pUnscaled->width; h = pUnscaled->height; s_photo = Tk_FindPhoto(interp, Tcl_GetString(pImage->pImageName)); s_block.pixelPtr = (unsigned char *)HtmlAlloc("temp", sw * sh * 4); s_block.width = sw; s_block.height = sh; s_block.pitch = sw * 4; s_block.pixelSize = 4; s_block.offset[0] = 0; s_block.offset[1] = 1; s_block.offset[2] = 2; s_block.offset[3] = 3; for (x=0; x<sw; x++) { int orig_x = ((x * w) / sw); for (y=0; y<sh; y++) { unsigned char *zOrig; unsigned char *zScale; int orig_y = ((y * h) / sh); zOrig = &block.pixelPtr[ orig_x * block.pixelSize + orig_y * block.pitch]; zScale = &s_block.pixelPtr[ x * s_block.pixelSize + y * s_block.pitch]; zScale[0] = zOrig[block.offset[0]]; zScale[1] = zOrig[block.offset[1]]; zScale[2] = zOrig[block.offset[2]]; zScale[3] = zOrig[block.offset[3]]; } } photoputblock(interp, s_photo, &s_block, 0, 0, sw, sh, 0); HtmlFree(s_block.pixelPtr); } else { return HtmlImageImage(pImage->pUnscaled); } pImage->isValid = 1; if (pUnscaled->pixmap) { Tcl_Obj *apObj[4]; apObj[0] = Tcl_NewStringObj("image", -1); apObj[1] = Tcl_NewStringObj("create", -1); apObj[2] = Tcl_NewStringObj("photo", -1); apObj[3] = pUnscaled->pImageName; Tcl_IncrRefCount(apObj[0]); Tcl_IncrRefCount(apObj[1]); Tcl_IncrRefCount(apObj[2]); pUnscaled->nIgnoreChange++; Tcl_EvalObjv(interp, 4, apObj, TCL_EVAL_GLOBAL); pUnscaled->nIgnoreChange--; Tcl_DecrRefCount(apObj[2]); Tcl_DecrRefCount(apObj[1]); Tcl_IncrRefCount(apObj[0]); } } return pImage->image; }
Tcl_Obj *HtmlXImageToImage( HtmlTree *pTree, XImage *pXImage, int w, int h ) { Tcl_Interp *interp = pTree->interp; Tcl_Obj *pImage; Tk_PhotoHandle photo; Tk_PhotoImageBlock block; int x; int y; unsigned long redmask, redshift; unsigned long greenmask, greenshift; unsigned long bluemask, blueshift; Visual *pVisual; Tcl_Eval(interp, "image create photo"); pImage = Tcl_GetObjResult(interp); Tcl_IncrRefCount(pImage); block.pixelPtr = (unsigned char *)HtmlAlloc("temp", w * h * 4); block.width = w; block.height = h; block.pitch = w*4; block.pixelSize = 4; block.offset[0] = 0; block.offset[1] = 1; block.offset[2] = 2; block.offset[3] = 3; pVisual = Tk_Visual(pTree->tkwin); redmask = pVisual->red_mask; bluemask = pVisual->blue_mask; greenmask = pVisual->green_mask; for (redshift=0; !((redmask>>redshift)&0x000000001); redshift++); for (greenshift=0; !((greenmask>>greenshift)&0x00000001); greenshift++); for (blueshift=0; !((bluemask>>blueshift)&0x00000001); blueshift++); for (x=0; x<w; x++) { for (y=0; y<h; y++) { unsigned char *pOut; unsigned long pixel = XGetPixel(pXImage, x, y); pOut = &block.pixelPtr[x*block.pixelSize + y*block.pitch]; pOut[0] = (pixel&redmask)>>redshift; pOut[1] = (pixel&greenmask)>>greenshift; pOut[2] = (pixel&bluemask)>>blueshift; pOut[3] = 0xFF; } } photo = Tk_FindPhoto(interp, Tcl_GetString(pImage)); photoputblock(interp, photo, &block, 0, 0, w, h, 0); HtmlFree(block.pixelPtr); return pImage; }
/* *--------------------------------------------------------------------------- * * HtmlImageTile -- * * Results: * * Side effects: * None. * *--------------------------------------------------------------------------- */ Tk_Image HtmlImageTile( HtmlImage2 *pImage, /* Image object */ int *pW, int *pH ) { HtmlTree *pTree = pImage->pImageServer->pTree; Tcl_Interp *interp = pTree->interp; Tcl_Obj *pTileName; /* Name of tile image at the script level */ Tk_PhotoHandle tilephoto; /* Photo of tile */ Tk_PhotoImageBlock tileblock; /* Block of tile image */ int iTileWidth; int iTileHeight; Tk_PhotoHandle origphoto; Tk_PhotoImageBlock origblock; int x; int y; /* The tile has already been generated. Return it. */ if (pImage->pTileName) { goto return_tile; } /* The image is too big to bother with a tile. Return the original. */ if (!tilesize(pImage, &iTileWidth, &iTileHeight)) { goto return_original; } /* Retrieve the block for the original image */ origphoto = Tk_FindPhoto(interp, Tcl_GetString(pImage->pImageName)); if (!origphoto) goto return_original; Tk_PhotoGetImage(origphoto, &origblock); if (!origblock.pixelPtr) goto return_original; /* Create the tile image. Surely there is a way to do this without * invoking a script, but I haven't found it yet. */ Tcl_Eval(interp, "image create photo"); pTileName = Tcl_GetObjResult(interp); Tcl_IncrRefCount(pTileName); tilephoto = Tk_FindPhoto(interp, Tcl_GetString(pTileName)); Tk_PhotoGetImage(tilephoto, &tileblock); pImage->pTileName = pTileName; pImage->tile = Tk_GetImage( interp, pTree->tkwin, Tcl_GetString(pTileName), imageChanged, 0 ); /* Allocate a block to write the tile data into. */ tileblock.pixelPtr = (unsigned char *)HtmlAlloc( "temp", iTileWidth * iTileHeight * 4 ); tileblock.width = iTileWidth; tileblock.height = iTileHeight; tileblock.pitch = iTileWidth * 4; tileblock.pixelSize = 4; tileblock.offset[0] = 0; tileblock.offset[1] = 1; tileblock.offset[2] = 2; tileblock.offset[3] = 3; for (x = 0; x < iTileWidth; x++) { for (y = 0; y < iTileHeight; y++) { unsigned char *zOrig; unsigned char *zScale; zOrig = &origblock.pixelPtr[ (x % pImage->width) *origblock.pixelSize + (y % pImage->height) * origblock.pitch ]; zScale = &tileblock.pixelPtr[x * 4 + y * tileblock.pitch]; zScale[0] = zOrig[origblock.offset[0]]; zScale[1] = zOrig[origblock.offset[1]]; zScale[2] = zOrig[origblock.offset[2]]; zScale[3] = zOrig[origblock.offset[3]]; } } photoputblock(interp,tilephoto,&tileblock,0,0,iTileWidth,iTileHeight,0); HtmlFree(tileblock.pixelPtr); pImage->iTileWidth = iTileWidth; pImage->iTileHeight = iTileHeight; return_tile: *pW = pImage->iTileWidth; *pH = pImage->iTileHeight; return pImage->tile; return_original: HtmlImageSize(pImage, pW, pH); return HtmlImageImage(pImage); }
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; }
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; }
static int PyImagingPhotoPut(ClientData clientdata, Tcl_Interp* interp, int argc, char **argv) { Imaging im; Tk_PhotoHandle photo; Tk_PhotoImageBlock block; if (argc != 3) { 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 PIL Image handle */ im = ImagingFind(argv[2]); if (!im) { Tcl_AppendResult(interp, "bad name", (char*) NULL); return TCL_ERROR; } if (!im->block) { Tcl_AppendResult(interp, "bad display memory", (char*) NULL); return TCL_ERROR; } /* Active region */ #if 0 if (src_xoffset + xsize > im->xsize) xsize = im->xsize - src_xoffset; if (src_yoffset + ysize > im->ysize) ysize = im->ysize - src_yoffset; if (xsize < 0 || ysize < 0 || src_xoffset >= im->xsize || src_yoffset >= im->ysize) return TCL_OK; #endif /* Mode */ if (strcmp(im->mode, "1") == 0 || strcmp(im->mode, "L") == 0) { block.pixelSize = 1; block.offset[0] = block.offset[1] = block.offset[2] = 0; } else if (strncmp(im->mode, "RGB", 3) == 0) { block.pixelSize = 4; block.offset[0] = 0; block.offset[1] = 1; block.offset[2] = 2; if (strcmp(im->mode, "RGBA") == 0) block.offset[3] = 3; /* alpha (or reserved, under 8.2) */ else block.offset[3] = 0; /* no alpha */ } else { Tcl_AppendResult(interp, "Bad mode", (char*) NULL); return TCL_ERROR; } block.width = im->xsize; block.height = im->ysize; block.pitch = im->linesize; block.pixelPtr = (unsigned char*) im->block; #if 0 block.pixelPtr = (unsigned char*) im->block + src_yoffset * im->linesize + src_xoffset * im->pixelsize; #endif #if TK < 85 /* Tk 8.4 */ Tk_PhotoPutBlock(photo, &block, 0, 0, block.width, block.height, TK_PHOTO_COMPOSITE_SET); if (strcmp(im->mode, "RGBA") == 0) /* Tk workaround: we need apply ToggleComplexAlphaIfNeeded */ /* (fixed in Tk 8.5a3) */ Tk_PhotoSetSize(photo, block.width, block.height); #else /* Tk 8.5 */ Tk_PhotoPutBlock(interp, photo, &block, 0, 0, block.width, block.height, TK_PHOTO_COMPOSITE_SET); #endif return TCL_OK; }