1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
#include "pow.h"
void PowCreateImage(char *image_name, char *data_name, int *xoffset,
int *yoffset, int *width, int *height, double *xorigin,
double *xinc, double *yorigin, double *yinc, char *xunits,
char *yunits, char *zunits, int *status) {
/* xinc or yinc == 0 will mean count by integers */
PowImage *image_instance;
PowData *dataptr;
#if !(defined(__WIN32__) || defined(macintosh))
Tk_PictHandle pict_image_handle;
Tk_PictImageBlock pict_block;
PictMaster *masterPtr;
#endif
Tk_PhotoHandle photo_image_handle;
Tk_PhotoImageBlock photo_block;
Tcl_HashEntry *entry_ptr;
int new = 0;
char *str_ptr;
int pseudoImages;
double min,max;
char smin[30];
char smax[30];
double datum;
int i, wcsStatus;
const char *WCSstring;
char powWCS[7]="powWCS";
Tcl_GetInt(interp,Tcl_GetVar(interp,"powPseudoImages",TCL_GLOBAL_ONLY),
&pseudoImages);
entry_ptr = Tcl_CreateHashEntry(&PowImageTable, image_name, &new);
if (!new) {
/*
fprintf(stdout, "Reusing image name: %s\n", image_name);
*/
#ifdef DEBUG
printf("Reusing image name: %s",image_name);
#endif
#if !(defined(__WIN32__) || defined(macintosh))
/* zero out the data ptr field so VISU won't throw away our data */
/* when you reuse an image name, VISU calls ImgPictDelete on it */
/* this frees the data pointer. We don't want that.... */
if( pseudoImages ) {
masterPtr = (PictMaster *)Tk_FindPict(image_name);
if ( (unsigned char*)masterPtr->data == masterPtr->bytedata) {
masterPtr->bytedata = NULL;
}
masterPtr->data = NULL;
}
#endif
}
image_instance = (PowImage *) ckalloc(sizeof(PowImage));
if(image_instance == NULL) {
*status = TCL_ERROR;
fprintf(stderr, "Couldn't malloc image structure space");
Tcl_DeleteHashEntry(entry_ptr);
return;
}
Tcl_SetHashValue( entry_ptr, image_instance);
str_ptr = ckalloc(strlen(image_name)+1);
strncpy(str_ptr,image_name,strlen(image_name)+1);
image_instance->image_name = str_ptr;
image_instance->xoffset = *xoffset;
image_instance->yoffset = *yoffset;
image_instance->width = *width;
image_instance->height = *height;
image_instance->xorigin = *xorigin;
image_instance->xinc = *xinc;
image_instance->yorigin = *yorigin;
image_instance->yinc = *yinc;
str_ptr = ckalloc(strlen(xunits)+1);
strncpy(str_ptr,xunits,strlen(xunits)+1);
image_instance->xunits = str_ptr;
str_ptr = ckalloc(strlen(yunits)+1);
strncpy(str_ptr,yunits,strlen(yunits)+1);
image_instance->yunits = str_ptr;
str_ptr = ckalloc(strlen(zunits)+1);
strncpy(str_ptr,zunits,strlen(zunits)+1);
image_instance->zunits = str_ptr;
/* Now set up the image */
if (pseudoImages != 0) {
#if !(defined(__WIN32__) || defined(macintosh))
/* use Pict widget (Visu) */
if (Tcl_VarEval(interp,"image create pict ",image_instance->image_name,(char *) NULL) == TCL_ERROR) {
*status = TCL_ERROR;
fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
Tcl_DeleteHashEntry(entry_ptr);
return;
}
pict_image_handle = Tk_FindPict(image_instance->image_name);
if(pict_image_handle == NULL) {
*status = TCL_ERROR;
fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
Tcl_DeleteHashEntry(entry_ptr);
return;
}
image_instance->image_handle = (void *) pict_image_handle;
#else
fprintf(stderr,"You should not see this. Pict images disabled in Win32.\n");
return;
#endif /*__WIN32__ || macintosh*/
} else {
/* use Photo widget */
if (Tcl_VarEval(interp,"image create photo ",image_instance->image_name,(char *) NULL) == TCL_ERROR) {
*status = TCL_ERROR;
fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
Tcl_DeleteHashEntry(entry_ptr);
return;
}
photo_image_handle = Tk_FindPhoto(interp,image_instance->image_name);
if(photo_image_handle == NULL) {
*status = TCL_ERROR;
fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
Tcl_DeleteHashEntry(entry_ptr);
return;
}
image_instance->image_handle = (void *) photo_image_handle;
}
/* Get the data address out of the hash table */
entry_ptr = Tcl_FindHashEntry (&PowDataTable, data_name);
if(entry_ptr == NULL) {
*status = TCL_ERROR;
fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
Tcl_DeleteHashEntry(entry_ptr);
return;
}
dataptr = (PowData *) Tcl_GetHashValue(entry_ptr);
/*Setup displayed min and max stuff */
min = DBL_MAX;
max = -DBL_MAX;
for (i = 0; i < image_instance->width * image_instance->height; i++) {
datum = PowExtractDatum(dataptr,i);
if (datum != DBL_MAX) {
min = (datum < min) ? datum : min;
max = (datum > max) ? datum : max;
}
}
if( max==-DBL_MAX ) min=max=0.0;
sprintf(smin,"%.17lg",min);
sprintf(smax,"%.17lg",max);
Tcl_SetVar2(interp,"powRBmin",image_name,smin,TCL_GLOBAL_ONLY);
Tcl_SetVar2(interp,"powRBmax",image_name,smax,TCL_GLOBAL_ONLY);
image_instance->dataptr = dataptr;
/* notice that the "offsets" are not implemented here yet*/
/* this will require some serious tweaking to pixelPtr and skip fields */
if (pseudoImages != 0) {
#if !(defined(__WIN32__) || defined(macintosh))
/*Pict*/
pict_block.datatype = dataptr->data_type;
pict_block.pixelPtr = (unsigned char *) dataptr->data_array;
pict_block.width = image_instance->width;
pict_block.height = image_instance->height;
pict_block.pixelSize = pixelSizes[dataptr->data_type];
pict_block.pitch = pict_block.pixelSize;
pict_block.skip = 0;
pict_block.copy = NO_COPY;
Tk_PictExpand(pict_image_handle,image_instance->width,image_instance->height);
Tk_PictPutBlock(pict_image_handle,&pict_block,0,0,image_instance->width,image_instance->height);
#else
fprintf(stderr,"You should not see this\n");
return;
#endif /*__WIN32__ || macintosh*/
} else {
PowDitherToPhoto(image_instance,&photo_block,min,max);
photo_block.pixelSize = 3;
photo_block.width = image_instance->width;
photo_block.height = image_instance->height;
photo_block.pitch = image_instance->width * 3;
photo_block.offset[0] = 0;
photo_block.offset[1] = 1;
photo_block.offset[2] = 2;
Tk_PhotoExpand(interp, photo_image_handle,
image_instance->width, image_instance->height);
Tk_PhotoPutBlock(interp, photo_image_handle, &photo_block, 0, 0,
image_instance->width, image_instance->height, TK_PHOTO_COMPOSITE_SET);
ckfree(photo_block.pixelPtr);
}
/* Call WCS init procedure if applicable */
wcsStatus = TCL_ERROR;
WCSstring = Tcl_GetVar2(interp,powWCS,image_name,TCL_GLOBAL_ONLY);
if( (WCSstring != NULL) && strcmp(WCSstring,"") ) {
wcsStatus = Tcl_VarEval(interp, "powWCSInitImage ", image_name, " ",
WCSstring, (char *) NULL);
}
if( wcsStatus == TCL_ERROR ) {
image_instance->xorigin -= 0.5*image_instance->xinc;
image_instance->yorigin -= 0.5*image_instance->yinc;
image_instance->xotherend = image_instance->xorigin +
image_instance->width*image_instance->xinc;
image_instance->yotherend = image_instance->yorigin +
image_instance->height*image_instance->yinc;
memset(image_instance->WCS.type, '\0', 6);
image_instance->WCS.nAxis = 2;
image_instance->WCS.refVal[0] = image_instance->xorigin;
image_instance->WCS.refVal[1] = image_instance->yorigin;
image_instance->WCS.refPix[0] = -0.5;
image_instance->WCS.refPix[1] = -0.5;
image_instance->WCS.cdFrwd[0][0] = image_instance->xinc;
image_instance->WCS.cdFrwd[1][1] = image_instance->yinc;
image_instance->WCS.cdFrwd[0][1] = 0.0;
image_instance->WCS.cdFrwd[1][0] = 0.0;
image_instance->WCS.cdRvrs[0][0] = 1.0/image_instance->xinc;
image_instance->WCS.cdRvrs[1][1] = 1.0/image_instance->yinc;
image_instance->WCS.cdRvrs[0][1] = 0.0;
image_instance->WCS.cdRvrs[1][0] = 0.0;
}
return;
}
void PowDestroyImage(char *image_name, int *status) {
Tcl_HashEntry *entry_ptr;
char errormsg[1024];
PowImage *image_ptr;
entry_ptr = Tcl_FindHashEntry(&PowImageTable,image_name);
if (entry_ptr == NULL) {
*status = TCL_ERROR;
sprintf(errormsg,"Can't find POWImage Object %s to destroy",image_name);
Tcl_SetResult(interp,errormsg,TCL_VOLATILE);
return;
}
image_ptr = (PowImage *)Tcl_GetHashValue(entry_ptr);
/*Delete the entry from the master POWData Hash*/
Tcl_DeleteHashEntry(entry_ptr);
/*free the PowImage memory itself and the string holding the name,
although this is small change*/
ckfree(image_ptr->image_name);
ckfree(image_ptr->xunits);
ckfree(image_ptr->yunits);
ckfree(image_ptr->zunits);
ckfree((char*)image_ptr);
return;
}
|