Main Page   Data Structures   File List   Data Fields   Globals  

Sadie_Image.c

Go to the documentation of this file.
00001 /*-Copyright Information------------------------------------------------------*/
00002 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00003 /*-General Information--------------------------------------------------------*/
00004 /*                                                                            */
00005 
00006 /*   This function reads a SADIE image from disk into main memory.  A photo   */
00007 /*   image  string  handler for  SADIE  image  strings.   This function  is   */
00008 /*   modeled after  the file tkImgPPM.c  which is contained in  the generic   */
00009 /*   directory of the Tk source distribution.                                 */
00010 /*                                                                            */
00011 
00012 /*   Rather than taking  an image file name or pixel  data string as input,   */
00013 /*   the memory address of a SADIE image that is already loaded into memory   */
00014 /*   is passed  in through the string  interface. This is  using the string   */
00015 /*   interface  in a  way other  than it  was intended,  but it  provides a   */
00016 /*   convenient interface none the less.                                      */
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 /* The format record for the SADIE string format: */
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",                    /* Name of the image format */
00052     NULL,                       /* File match procedure     */
00053     MatchSADIE,                 /* String match procedure   */
00054     NULL,                       /* File read procedure      */
00055     ReadSADIE,                  /* String read procedure    */
00056     WriteSADIE,                 /* File write procedure     */
00057     NULL,                       /* String write procedure   */
00058 };
00059 
00060 
00061 /*-Copyright Information------------------------------------------------------*/
00062 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00063 /*-General Information--------------------------------------------------------*/
00064 /* MatchSADIE -                                                               */
00065 /*                                                                            */
00066 /*   This procedure is  invoked by the photo image type  to see if image      */
00067 /*   data is in SADIE format.                                                 */
00068 /*                                                                            */
00069 /* Results:                                                                   */
00070 /*   The return value  is >0 if the first  characters in string "string"      */
00071 /*   look like SADIE data, and 0 otherwise.                                   */
00072 /*                                                                            */
00073 /* Side effects:                                                              */
00074 /*   The access position in file may change.                                  */
00075 /*                                                                            */
00076 /*----------------------------------------------------------------------------*/
00077 /*-Interface Information------------------------------------------------------*/
00078 static int MatchSADIE (
00079 char *string,           /*  I   The string, open for reading.                 */
00080 char *formatString,     /*  I   User-specified format string, or NULL.        */
00081 int *widthPtr,          /*  O   The dimensions of the image are returned here */
00082 int *heightPtr          /*  O   if the string is a valid raw SADIE string.    */
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 /*-Copyright Information------------------------------------------------------*/
00101 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00102 /*-General Information--------------------------------------------------------*/
00103 /* ReadSADIE -                                                               */
00104 /*                                                                            */
00105 /*   This procedure is called by the  photo image type to read SADIE format   */
00106 /*   data from a string and write it into a given photo image.                */
00107 /*                                                                            */
00108 /* Results:                                                                   */
00109 /*   A standard TCL completion code. If TCL_ERROR is returned then an error   */
00110 /*   message is left in interp->result.                                       */
00111 /*                                                                            */
00112 /* Side effects:                                                              */
00113 /*   The access  position in  string "string" is  changed, and new  data is   */
00114 /*   added to the image given by imageHandle.                                 */
00115 /*                                                                            */
00116 /*----------------------------------------------------------------------------*/
00117 /*-Interface Information------------------------------------------------------*/
00118 static int ReadSADIE (
00119 Tcl_Interp *interp,         /*  I   Interpreter to use for reporting errors.  */
00120 char *string,               /*  I   The image string, open for reading.       */
00121 char *formatString,         /*  I   User-specified format string, or NULL.    */
00122 Tk_PhotoHandle imageHandle, /*  I   The photo image to write into.            */
00123 int destX,                  /*  I   Coordinates of top-left pixel in          */
00124 int destY,                  /*         photo image to be written to.          */
00125 int width,                  /*  I   Dimensions of block of photo image to     */
00126 int height,                 /*         be written to.                         */
00127 int srcX,                   /*  I   Coordinates of top-left pixel to be used  */
00128 int srcY                    /*         in image being read.                   */
00129 /*----------------------------------------------------------------------------*/
00130 ) {
00131     int nLines, nBytes, h, type, count;
00132     unsigned char *pixelPtr;
00133     int j, k;
00134     int stretchmin = DMIN;     /* Linear stretch minimum value, used for displayed image */
00135     int stretchmax = DMAX;     /* Linear stretch maximum value, used for displayed image */
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) {       /* Grayscale image */
00188         block.pixelSize = 1;
00189         block.offset[0] = 0;
00190         block.offset[1] = 0;
00191         block.offset[2] = 0;
00192     }
00193     else {                      /* RGB image */
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) {     /* Stretch the image */
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 {    /* (img->gmin == img->gmax) */
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 {     /* Don't stretch the image */
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) {     /* Stretch the image */
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 {     /* (img->gmin == img->gmax) */
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 {     /* Don't stretch the image */
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 /*-Copyright Information------------------------------------------------------*/
00328 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00329 /*-General Information--------------------------------------------------------*/
00330 /* WriteSADIE -                                                               */
00331 /*                                                                            */
00332 /*   This procedure  is called  by the  photo image type  to write  a photo   */
00333 /*   image to a SADIE image.                                                  */
00334 /*                                                                            */
00335 /* Results:                                                                   */
00336 /*   A  standard TCL  completion code.   If TCL_ERROR  is returned  then an   */
00337 /*   error message is left in interp->result.                                 */
00338 /*                                                                            */
00339 /*----------------------------------------------------------------------------*/
00340 /*-Interface Information------------------------------------------------------*/
00341 static int WriteSADIE (
00342 Tcl_Interp *interp,          /*  I   Interpreter to use for reporting errors. */
00343 char *ImgName,               /*  I   The address of image to write to.        */
00344 char *formatString,          /*  I   User-specified format string, or NULL.   */
00345 Tk_PhotoImageBlock *blockPtr /*  I   pointer to photo image data.             */
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     /* create image of appropriate size */
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 /*    if ((greenOffset == 1) && (blueOffset == 2) && (blockPtr->pixelSize == 3)
00369         && (blockPtr->pitch == (blockPtr->width * 3))) { */
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) {     /* If Grayscale image */
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 }

Generated on Wed Apr 9 08:56:12 2003 for TREES by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002