PETSC_EXTERN PetscErrorCode PetscDrawCreate_Image(PetscDraw draw) { PetscImage img; int w = draw->w, h = draw->h; PetscInt size[2], nsize = 2; PetscBool set; PetscErrorCode ierr; PetscFunctionBegin; draw->pause = 0; draw->coor_xl = 0; draw->coor_xr = 1; draw->coor_yl = 0; draw->coor_yr = 1; draw->port_xl = 0; draw->port_xr = 1; draw->port_yl = 0; draw->port_yr = 1; size[0] = w; if (size[0] < 1) size[0] = 300; size[1] = h; if (size[1] < 1) size[1] = size[0]; ierr = PetscOptionsGetIntArray(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_size",size,&nsize,&set);CHKERRQ(ierr); if (set && nsize == 1) size[1] = size[0]; if (size[0] < 1) size[0] = 300; if (size[1] < 1) size[1] = size[0]; draw->w = w = size[0]; draw->x = 0; draw->h = h = size[1]; draw->x = 0; ierr = PetscNewLog(draw,&img);CHKERRQ(ierr); ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr); draw->data = (void*)img; img->w = w; img->h = h; ierr = PetscCalloc1((size_t)(img->w*img->h),&img->buffer);CHKERRQ(ierr); PetscImageSetClip(img,0,0,img->w,img->h); { int i,k,ncolors = 256-PETSC_DRAW_BASIC_COLORS; unsigned char R[256-PETSC_DRAW_BASIC_COLORS]; unsigned char G[256-PETSC_DRAW_BASIC_COLORS]; unsigned char B[256-PETSC_DRAW_BASIC_COLORS]; ierr = PetscDrawUtilitySetCmap(NULL,ncolors,R,G,B);CHKERRQ(ierr); for (k=0; k<PETSC_DRAW_BASIC_COLORS; k++) { img->palette[k][0] = BasicColors[k][0]; img->palette[k][1] = BasicColors[k][1]; img->palette[k][2] = BasicColors[k][2]; } for (i=0; i<ncolors; i++, k++) { img->palette[k][0] = R[i]; img->palette[k][1] = G[i]; img->palette[k][2] = B[i]; } } if (!draw->savefilename) {ierr = PetscDrawSetSave(draw,draw->title);CHKERRQ(ierr);} PetscFunctionReturn(0); }
PetscErrorCode PetscDrawSetUpColormap_Shared(Display *display,int screen,Visual *visual,Colormap colormap) { int i; XColor colordef,ecolordef; PetscBool fast = PETSC_FALSE; PetscErrorCode ierr; PetscFunctionBegin; if (colormap) gColormap = colormap; else gColormap = DefaultColormap(display,screen); /* set the basic colors into the color map */ for (i=0; i<PETSC_DRAW_BASIC_COLORS; i++) { XAllocNamedColor(display,gColormap,colornames[i],&colordef,&ecolordef); gCmapping[i] = colordef.pixel; } /* set the contour colors into the colormap */ ierr = PetscOptionsGetBool(NULL,NULL,"-draw_fast",&fast,NULL);CHKERRQ(ierr); if (!fast) { int ncolors = 256-PETSC_DRAW_BASIC_COLORS; unsigned char *red,*green,*blue; ierr = PetscMalloc3(ncolors,&red,ncolors,&green,ncolors,&blue);CHKERRQ(ierr); ierr = PetscDrawUtilitySetCmap(NULL,ncolors,red,green,blue);CHKERRQ(ierr); for (i=0; i<ncolors; i++) { colordef.red = (unsigned short)(red[i] << 8); colordef.green = (unsigned short)(green[i] << 8); colordef.blue = (unsigned short)(blue[i] << 8); colordef.flags = DoRed|DoGreen|DoBlue; XAllocColor(display,gColormap,&colordef); gCmapping[PETSC_DRAW_BASIC_COLORS + i] = colordef.pixel; } ierr = PetscFree3(red,green,blue);CHKERRQ(ierr); } ierr = PetscInfo(0,"Successfully allocated colors\n");CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode PetscDrawSetUpColormap_Private(Display *display,int screen,Visual *visual,Colormap colormap) { int i,found; Colormap defaultmap = DefaultColormap(display,screen); XColor colordef; PetscBool fast = PETSC_FALSE; PetscErrorCode ierr; PetscFunctionBegin; if (colormap) gColormap = colormap; else gColormap = XCreateColormap(display,RootWindow(display,screen),visual,AllocAll); cmap_base = 0; ierr = PetscMemzero(cmap_pixvalues_used,256*sizeof(PetscBool));CHKERRQ(ierr); /* set the basic colors into the color map */ for (i=0; i<PETSC_DRAW_BASIC_COLORS; i++) { XParseColor(display,gColormap,colornames[i],&colordef); /* try to allocate the color in the default-map */ found = XAllocColor(display,defaultmap,&colordef); /* use it, if it it exists and is not already used in the new colormap */ if (found && colordef.pixel < 256 && !cmap_pixvalues_used[colordef.pixel]) { cmap_pixvalues_used[colordef.pixel] = PETSC_TRUE; /* otherwise search for the next available slot */ } else { while (cmap_pixvalues_used[cmap_base]) cmap_base++; colordef.pixel = cmap_base; cmap_pixvalues_used[cmap_base++] = PETSC_TRUE; } XStoreColor(display,gColormap,&colordef); gCmapping[i] = colordef.pixel; } /* set the contour colors into the colormap */ ierr = PetscOptionsGetBool(NULL,NULL,"-draw_fast",&fast,NULL);CHKERRQ(ierr); if (!fast) { int ncolors = 256-PETSC_DRAW_BASIC_COLORS; unsigned char *red,*green,*blue; ierr = PetscMalloc3(ncolors,&red,ncolors,&green,ncolors,&blue);CHKERRQ(ierr); ierr = PetscDrawUtilitySetCmap(NULL,ncolors,red,green,blue);CHKERRQ(ierr); for (i=0; i<ncolors; i++) { colordef.red = (unsigned short)(red[i] << 8); colordef.green = (unsigned short)(green[i] << 8); colordef.blue = (unsigned short)(blue[i] << 8); colordef.flags = DoRed|DoGreen|DoBlue; /* try to allocate the color in the default-map */ found = XAllocColor(display,defaultmap,&colordef); /* use it, if it it exists and is not already used in the new colormap */ if (found && colordef.pixel < 256 && !cmap_pixvalues_used[colordef.pixel]) { cmap_pixvalues_used[colordef.pixel] = PETSC_TRUE; /* otherwise search for the next available slot */ } else { while (cmap_pixvalues_used[cmap_base]) cmap_base++; colordef.pixel = cmap_base; cmap_pixvalues_used[cmap_base++] = PETSC_TRUE; } XStoreColor(display,gColormap,&colordef); gCmapping[PETSC_DRAW_BASIC_COLORS + i] = colordef.pixel; } ierr = PetscFree3(red,green,blue);CHKERRQ(ierr); } ierr = PetscInfo(0,"Successfully allocated colors\n");CHKERRQ(ierr); PetscFunctionReturn(0); }