00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #define USE_OLD_IMAGE
00022
00023
00024 #include <tcl.h>
00025 #include <tk.h>
00026 #include <string.h>
00027 #include "sadie.h"
00028
00029
00030
00031 static int MatchSADIE _ANSI_ARGS_((char *string,
00032 char *formatString,
00033 int *widthPtr,
00034 int *heightPtr));
00035
00036 static int ReadSADIE _ANSI_ARGS_((Tcl_Interp *interp,
00037 char *string,
00038 char *formatString,
00039 Tk_PhotoHandle imageHandle,
00040 int destX, int destY,
00041 int width, int height,
00042 int srcX, int srcY));
00043
00044 static int WriteSADIE _ANSI_ARGS_((Tcl_Interp *interp,
00045 char *ImgName,
00046 char *formatString,
00047 Tk_PhotoImageBlock *blockPtr));
00048
00049
00050 Tk_PhotoImageFormat tkImgFmtSADIE = {
00051 "SADIE",
00052 NULL,
00053 MatchSADIE,
00054 NULL,
00055 ReadSADIE,
00056 WriteSADIE,
00057 NULL,
00058 };
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 static int MatchSADIE (
00079 char *string,
00080 char *formatString,
00081 int *widthPtr,
00082 int *heightPtr
00083
00084 ) {
00085 int imgaddr;
00086 IMAGE *img;
00087
00088 sscanf(string, "%x", &imgaddr);
00089 img = (IMAGE *) imgaddr;
00090
00091 if (CHECKIMG(img)) {
00092 *widthPtr = img->npix;
00093 *heightPtr = img->nlin;
00094 return(1);
00095 } else
00096 return(0);
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 static int ReadSADIE (
00119 Tcl_Interp *interp,
00120 char *string,
00121 char *formatString,
00122 Tk_PhotoHandle imageHandle,
00123 int destX,
00124 int destY,
00125 int width,
00126 int height,
00127 int srcX,
00128 int srcY
00129
00130 ) {
00131 int nLines, nBytes, h, type, count;
00132 unsigned char *pixelPtr;
00133 int j, k;
00134 int stretchmin = DMIN;
00135 int stretchmax = DMAX;
00136 int stretchMin = 0;
00137 int stretchMax = 255;
00138 double offset, factor;
00139 int imgaddr;
00140 int option;
00141 int var_exists;
00142 Tk_PhotoImageBlock block;
00143 IMAGE *img;
00144 BYTE pixel;
00145
00146
00147 Tcl_Eval(interp, "info exists SadieVar(getstretched)");
00148 Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &var_exists);
00149 if (var_exists) {
00150 Tcl_GetInt(interp,Tcl_GetVar2(interp, "SadieVar", "getstretched", TCL_GLOBAL_ONLY),&option);
00151 } else {
00152 option = 1;
00153 }
00154
00155 sscanf(string, "%x", &imgaddr);
00156 img = (IMAGE *) imgaddr;
00157
00158 if (!CHECKIMG(img)) {
00159 Tcl_AppendResult(interp, "Couldn't identify SADIE image from input string.", NULL);
00160 return TCL_ERROR;
00161 }
00162 if ((img->nbnd < 1)) {
00163 Tcl_AppendResult(interp, "Image bands not within display range.", NULL);
00164 return TCL_ERROR;
00165 }
00166 if (img->nlin <= 0) {
00167 Tcl_AppendResult(interp, "Image lines not within display range.", NULL);
00168 return TCL_ERROR;
00169 }
00170 if (img->npix <= 0) {
00171 Tcl_AppendResult(interp, "Image pixels not within display range.", NULL);
00172 return TCL_ERROR;
00173 }
00174
00175
00176
00177 if (width > img->npix) {
00178 width = img->npix;
00179 }
00180 if (height > img->nlin) {
00181 height = img->nlin;
00182 }
00183 if ((width <= 0) || (height <= 0)) {
00184 return TCL_OK;
00185 }
00186
00187 if (img->nbnd != 3) {
00188 block.pixelSize = 1;
00189 block.offset[0] = 0;
00190 block.offset[1] = 0;
00191 block.offset[2] = 0;
00192 }
00193 else {
00194 block.pixelSize = 3;
00195 block.offset[0] = 0;
00196 block.offset[1] = 1;
00197 block.offset[2] = 2;
00198 }
00199
00200 block.width = width;
00201 block.pitch = block.pixelSize * img->npix;
00202
00203 Tk_PhotoExpand(imageHandle, destX + width, destY + height);
00204
00205 nLines = height;
00206 if (nLines <= 0) {
00207 nLines = 1;
00208 }
00209
00210 nBytes = nLines * block.pitch;
00211
00212 pixelPtr = (unsigned char *) ckalloc((unsigned) nBytes);
00213 block.pixelPtr = pixelPtr;
00214
00215 RANGE(img);
00216
00217 if (block.pixelSize == 1) {
00218 if (option == 1) {
00219
00220 if ((PIXEL)stretchMin == img->gmin && img->gmax == (PIXEL)stretchMax) {
00221 for (j=0; j<img->nlin; j++) {
00222 for (k=0; k<img->npix ; k++) {
00223 pixel = (BYTE)img->data[0][j][k];
00224 *pixelPtr++ = pixel;
00225 }
00226 }
00227 } else if (img->gmin < img->gmax) {
00228 factor = (double)(stretchMax - stretchMin)/(double)(img->gmax - img->gmin);
00229 offset = (double)(stretchMin) - (double)(img->gmin)*factor;
00230
00231 for (j=0; j<img->nlin; j++) {
00232 for (k=0; k<img->npix ; k++) {
00233 pixel = (BYTE)((double)(img->data[0][j][k])*factor + offset);
00234 *pixelPtr++ = pixel;
00235 }
00236 }
00237 } else {
00238 factor = (double)(stretchMax - stretchMin)/(double)(SMAX - SMIN);
00239 pixel = (BYTE)((double)(min(max(SMIN,img->data[0][0][0]),SMAX)-SMIN)*factor + (double)stretchMin);
00240
00241 for (j=0; j<img->nlin; j++)
00242 {
00243 for (k=0; k<img->npix ; k++) {
00244 *pixelPtr++ = pixel;
00245 }
00246 }
00247 }
00248 } else {
00249
00250 for (j=0; j<img->nlin; j++) {
00251 for (k=0; k<img->npix ; k++) {
00252 pixel = (BYTE)img->data[0][j][k];
00253 *pixelPtr++ = pixel;
00254 }
00255 }
00256 }
00257
00258 block.height = height;
00259 Tk_PhotoPutBlock(imageHandle, &block, destX, destY, width, height);
00260
00261 } else if (block.pixelSize == 3) {
00262 if (option == 1) {
00263
00264 if ((PIXEL)stretchMin == img->gmin && img->gmax == (PIXEL)stretchMax) {
00265 for (j=0; j<img->nlin; j++) {
00266 for (k=0; k<img->npix ; k++) {
00267 pixel = (BYTE)img->data[0][j][k];
00268 *pixelPtr++ = pixel;
00269 pixel = (BYTE)img->data[1][j][k];
00270 *pixelPtr++ = pixel;
00271 pixel = (BYTE)img->data[2][j][k];
00272 *pixelPtr++ = pixel;
00273 }
00274 }
00275 } else if (img->gmin < img->gmax) {
00276 factor = (double)(stretchMax - stretchMin)/(double)(img->gmax - img->gmin);
00277 offset = (double)(stretchMin) - (double)(img->gmin)*factor;
00278
00279 for (j=0; j<img->nlin; j++) {
00280 for (k=0; k<img->npix ; k++) {
00281 pixel = (BYTE)((double)(img->data[0][j][k])*factor + offset);
00282 *pixelPtr++ = pixel;
00283 pixel = (BYTE)((double)(img->data[1][j][k])*factor + offset);
00284 *pixelPtr++ = pixel;
00285 pixel = (BYTE)((double)(img->data[2][j][k])*factor + offset);
00286 *pixelPtr++ = pixel;
00287 }
00288 }
00289 } else {
00290
00291 factor = (double)(stretchMax - stretchMin)/(double)(SMAX - SMIN);
00292 pixel = (BYTE)((double)(min(max(SMIN,img->data[0][0][0]),SMAX)-SMIN)*factor + (double)stretchMin);
00293
00294 for (j=0; j<img->nlin; j++) {
00295 for (k=0; k<img->npix ; k++) {
00296 *pixelPtr++ = pixel;
00297 *pixelPtr++ = pixel;
00298 *pixelPtr++ = pixel;
00299 }
00300 }
00301 }
00302 } else {
00303
00304 for (j=0; j<img->nlin; j++) {
00305 for (k=0; k<img->npix ; k++) {
00306 pixel = (BYTE)img->data[0][j][k];
00307 *pixelPtr++ = pixel;
00308 pixel = (BYTE)img->data[1][j][k];
00309 *pixelPtr++ = pixel;
00310 pixel = (BYTE)img->data[2][j][k];
00311 *pixelPtr++ = pixel;
00312 }
00313 }
00314 }
00315
00316 block.height = height;
00317 Tk_PhotoPutBlock(imageHandle, &block, destX, destY, width, height);
00318
00319 } else printf("tkImgSADIE does not support a pixel size of: %d \n", block.pixelSize);
00320
00321 return TCL_OK;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 static int WriteSADIE (
00342 Tcl_Interp *interp,
00343 char *ImgName,
00344 char *formatString,
00345 Tk_PhotoImageBlock *blockPtr
00346 ) {
00347 char msg[SLEN];
00348 unsigned char *pixelPtr, *pixLinePtr;
00349 int w, h;
00350 int greenOffset, blueOffset, nBytes;
00351 int addrstr;
00352 int pixint;
00353 IMAGE* img;
00354 PIXEL pixflt;
00355
00356
00357 if (!CHECKIMG(img)) GETMEM(3,
00358 blockPtr->height,
00359 blockPtr->width,
00360 &img);
00361 if (!img) return TCL_ERROR;
00362
00363 pixLinePtr = blockPtr->pixelPtr + blockPtr->offset[0];
00364 greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
00365 blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
00366
00367
00368
00369
00370 if ( greenOffset == 1 && blueOffset == 2 && blockPtr->pixelSize == 4 && blockPtr->pitch == (blockPtr->width * 4)) {
00371 pixelPtr = pixLinePtr;
00372
00373 for (h=0; h<blockPtr->height; h++) {
00374 for (w=0; w<blockPtr->width; w++) {
00375 pixint = *pixelPtr;
00376 pixflt = (PIXEL) pixint;
00377 img->data[0][h][w] = pixflt;
00378
00379 pixelPtr++;
00380 pixint = *pixelPtr;
00381 pixflt = (PIXEL) pixint;
00382 img->data[1][h][w] = pixflt;
00383
00384 pixelPtr++;
00385 pixint = *pixelPtr;
00386 pixflt = (PIXEL) pixint;
00387 img->data[2][h][w] = pixflt;
00388 pixelPtr+=2;
00389 }
00390 }
00391
00392 if (NAMES) {
00393 MESSAGE('I',"");
00394 MESSAGE('I',"TKIMGSADIE");
00395 MESSAGE('I',"");
00396 sprintf(msg," Input file: %s",ImgName);
00397 MESSAGE('I',msg);
00398 sprintf(msg," Number of bands, lines, pixels: %d, %d, %d",img->nbnd,img->nlin,img->npix);
00399 MESSAGE('I',msg);
00400 sprintf(msg," Number of bits/pixel/band: %d",8);
00401 MESSAGE('I',msg);
00402 sprintf(msg," Graylevel minimum, maximum: %10.4e, %10.4e",img->gmin,img->gmax);
00403 MESSAGE('I',msg);
00404 MESSAGE('I'," ...............");
00405 }
00406
00407 addrstr = (int) img;
00408
00409 sprintf(img->text, "%s", ImgName);
00410 sprintf(msg, "%x", addrstr);
00411 Tcl_SetResult(interp, msg, TCL_VOLATILE);
00412
00413 return TCL_OK;
00414 } else if (blockPtr->pixelSize == 1) {
00415
00416 img->nbnd = 1;
00417 img->npix = blockPtr->width;
00418 img->nlin = blockPtr->height;
00419 img->nbit = 32;
00420
00421 pixelPtr = pixLinePtr;
00422
00423 for (h=0; h<blockPtr->height; h++) {
00424 for (w=0; w<blockPtr->width; w++) {
00425 img->data[0][h][w] = (PIXEL)pixelPtr[0];
00426 pixelPtr++;
00427 }
00428 }
00429
00430 if (NAMES) {
00431 MESSAGE('I',"");
00432 MESSAGE('I',"TKIMGSADIE");
00433 MESSAGE('I',"");
00434 sprintf(msg," Input file: %s",ImgName);
00435 MESSAGE('I',msg);
00436 sprintf(msg," Number of bands, lines, pixels: %d, %d, %d",img->nbnd,img->nlin,img->npix);
00437 MESSAGE('I',msg);
00438 sprintf(msg," Number of bits/pixel/band: %d",8);
00439 MESSAGE('I',msg);
00440 sprintf(msg," Graylevel minimum, maximum: %10.4e, %10.4e",img->gmin,img->gmax);
00441 MESSAGE('I',msg);
00442 MESSAGE('I'," ...............");
00443 }
00444
00445
00446 addrstr = (int) img;
00447
00448 sprintf(img->text, "%s", ImgName);
00449 sprintf(msg, "%x", addrstr);
00450 Tcl_SetResult(interp, msg, TCL_VOLATILE);
00451
00452 return TCL_OK;
00453 } else return TCL_ERROR;
00454
00455 }
00456
00457
00458 int Sadie_Image_Init (Tcl_Interp *interp) {
00459
00460 Tk_CreatePhotoImageFormat(&tkImgFmtSADIE);
00461
00462 return TCL_OK;
00463 }