Main Page   Data Structures   File List   Data Fields   Globals  

bytedisk2img.c

Go to the documentation of this file.
00001 #include       "sadie.h"
00002 #include        "sadie_byte.h"
00003 
00004 
00005 
00006 
00007 
00008 /*-Copyright Information------------------------------------------------------*/
00009 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00010 /*-General Information--------------------------------------------------------*/
00011 /*                                                                            */
00012 /*   This function reads a SADIE image from disk into main memory.            */
00013 /*                                                                            */
00014 /*----------------------------------------------------------------------------*/
00015 /*-Interface Information------------------------------------------------------*/
00016 void BYTEDISK2IMG(
00017 unsigned char  *name,    /*  I   String, containing the name of the disk file.         */
00018 IMAGE_BYTE **out    /*  O   Address of a pointer to the image.                    */
00019 )
00020 /*----------------------------------------------------------------------------*/
00021 {   
00022     static char array[2] = { 0x00, 0x01 };
00023     char msg[SLEN];
00024     short  nbnd, nlin, npix, nbit;
00025     long nbytes;
00026     FILE *fp=NULL;
00027     PIXEL gmin,gmax;
00028 
00029 
00030     if (TIMES) TIMING(T_DISK2IMG);
00031 
00032 
00033     /* open image file */
00034     if (!(fp=fopen(name,"r"))) {
00035         sprintf(msg," Can't open file %s.",name);
00036         MESSAGE('E',msg);
00037         goto the_end;
00038     }
00039 
00040 
00041 
00042     /* read image size parameters from file */
00043     if (fread(&nbnd,sizeof(nbnd),1,fp) != 1  ||  fread(&nlin,sizeof(nlin),1,fp) != 1  ||  fread(&npix,sizeof(npix),1,fp) != 1  ||  fread(&nbit,sizeof(nbit),1,fp) != 1) {
00044         sprintf(msg," Can't read image header from file %s.",name);
00045         MESSAGE('E',msg);
00046         goto the_end;
00047     }
00048 
00049 
00050     /* swap bytes, if necessary */
00051     if (*(short *)array != 1) {
00052         //printf("DISK2IMG: Swapping bytes...\n");
00053     
00054         nbnd = SWAP((unsigned char *)&nbnd);
00055         nlin = SWAP((unsigned char *)&nlin);
00056         npix = SWAP((unsigned char *)&npix);
00057         nbit = SWAP((unsigned char *)&nbit);
00058     } 
00059     //else {
00060     //    printf("DISK2IMG: No need to swap bytes...\n");
00061     //}
00062 
00063 
00064     /* create image of appropriate size */
00065     if (!CHECKIMG_BYTE(*out)) GETMEM_BYTE(nbnd,nlin,npix,out);
00066     if (!CHECKIMG_BYTE(*out)) {
00067         sprintf(msg," Can't read image from file %s.",name);
00068         MESSAGE('E',msg);
00069         sprintf(msg,"    Are you sure %s is in SADIE format?",name);
00070         MESSAGE('E',msg);
00071         goto the_end;
00072     }
00073 
00074 
00075 
00076     if (fread(&gmin,sizeof(gmin),1,fp) != 1  ||  fread(&gmax,sizeof(gmax),1,fp) != 1) {
00077         sprintf(msg," Can't read image header from file %s.",name);
00078         MESSAGE('E',msg);
00079         goto the_end;
00080     }
00081     (*out)->gmin = (PIXEL_BYTE)rnd(gmin);
00082     (*out)->gmax = (PIXEL_BYTE)rnd(gmax);
00083 
00084     /* read the rest of the header data from file */
00085     (*out)->nbit = nbit;
00086     nbytes = (long)(sizeof((*out)->pstr)+sizeof((*out)->text));
00087     if (fread(&((*out)->pstr),nbytes,1,fp) != 1) {
00088         sprintf(msg," Can't read image header from file %s.",name);
00089         MESSAGE('E',msg);
00090         goto the_end;
00091     }
00092 
00093 
00094 
00095     /* read the image data from file */
00096     nbytes = (long)ceil((double)nbnd*(double)nlin*(double)npix);
00097     if (fread((*out)->data[0][0],1,nbytes,fp) <= 0) {
00098         sprintf(msg," Can't read image data from file %s.",name);
00099         MESSAGE('E',msg);
00100     }
00101 
00102     /* Set image name to match file name */
00103     sprintf((*out)->text,"%s",name);
00104     printf("out(0,0) --> %d\n",(*out)->data[0][0][0]);
00105 
00106 
00107     if (NAMES) {
00108         MESSAGE('I',"");
00109         MESSAGE('I',"BYTEDISK2IMG");
00110         MESSAGE('I',"");
00111         sprintf(msg," Input file:                                 %s",name);
00112         MESSAGE('I',msg); 
00113         MESSAGE('I'," Input format:                               SADIE");
00114         sprintf(msg," Header size:                                %d",(int)(sizeof((*out)->nbnd)+sizeof((*out)->nlin)+sizeof((*out)->npix)+sizeof((*out)->nbit)+sizeof((*out)->gmin)+sizeof((*out)->gmax)+sizeof((*out)->pstr)+sizeof((*out)->text)));
00115         MESSAGE('I',msg);
00116         sprintf(msg," Number of bands, lines, pixels:             %d, %d, %d",(*out)->nbnd,(*out)->nlin,(*out)->npix);
00117         MESSAGE('I',msg);
00118         sprintf(msg," Number of bits/pixel/band:                  %d",(*out)->nbit);
00119         MESSAGE('I',msg);
00120         sprintf(msg," Graylevel minimum, maximum:                 %d, %d",(*out)->gmin,(*out)->gmax);
00121         MESSAGE('I',msg);
00122         MESSAGE('I'," ...............");
00123     }
00124 
00125     /* close image file */
00126 
00127     the_end:
00128     if (fp) {
00129         if (fclose(fp)) {
00130             sprintf(msg," Can't close file %s.",name);
00131             MESSAGE('W',msg);
00132         }
00133     }
00134 
00135     if (TIMES) TIMING(T_EXIT);
00136 
00137 }
00138 

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