Main Page   Data Structures   File List   Data Fields   Globals  

import.c

Go to the documentation of this file.
00001 #include        <stdio.h>
00002 #include        "sadie.h"
00003 
00004 /*-Copyright Information------------------------------------------------------*/
00005 /* Copyright (c) 1990 by the University of Arizona Digital Image Analysis Lab */
00006 /*----------------------------------------------------------------------------*/
00007 /*-Programmer Information-----------------------------------------------------*/
00008 /*                                                                            */
00009 /*   This function imports an image from disk into main memory.               */
00010 /*   It is based on tape2img.c                                                */
00011 /*                                                                            */
00012 /*   This function depends entirely on the host computer and the operating    */
00013 /*   system. The code therefore needs to be changed whenever this function    */
00014 /*   is ported to a different environment.                                    */
00015 /*                                                                            */
00016 /*----------------------------------------------------------------------------*/
00017 /*-General Information--------------------------------------------------------*/
00018 /*                                                                            */
00019 /*   This function reads an image from disk into main memory.                 */
00020 /*                                                                            */
00021 /*----------------------------------------------------------------------------*/
00022 /*-Interface Information------------------------------------------------------*/
00023 void IMPORT (
00024 char *name,        /*  I   String, containing the name of the disk file.    */
00025 short option,        /*  I   BIL or BSQ                                       */
00026 short  offset,       /*  I   Number of header bytes to be skipped.            */
00027 short nbnd,          /*  I   Number of bands to be read.                      */
00028 short nlin,          /*  I   Number of lines to be read.                      */
00029 short npix,          /*  I   Number of pixels/line to be read.                */
00030 short nbit,          /*  I   Number of bits/pixel.                            */
00031 short ibgn,          /*  I   Index of the first band to be read.              */
00032 short jbgn,           /*  I   Index of the first line to be read.              */
00033 short kbgn,          /*  I   Index of the first pixel to be read.             */
00034 short iinc,          /*  I   Band increment during sampling.                  */
00035 short jinc,          /*  I   Line increment during sampling.                  */
00036 short kinc,          /*  I   Pixels/line increment during sampling.           */
00037 short iend,          /*  I   Index of the last band to be read.               */
00038 short jend,          /*  I   Index of the last line to be read.               */
00039 short kend,          /*  I   Index of the last pixel to be read.              */
00040 long filesize,       /*  I   Size of the image file.                          */
00041 IMAGE **out          /*  O   Address of a pointer to the output image.        */
00042 /*----------------------------------------------------------------------------*/
00043 ) { 
00044     register int i, j, k;
00045     char  msg[SLEN];
00046     short bands, lines, pixels, error;
00047     short  nbytes;
00048     PIXEL *line=NULL, *ptr=NULL;
00049     FILE *fp=NULL;
00050     long calcsize;
00051     long readbytes;
00052 
00053     if (TIMES) TIMING(T_IMPORT);
00054 
00055     /* check input */
00056     if (nbnd <= 0) {
00057         MESSAGE('E'," Number of bands must be greater than zero.");
00058         goto the_end;
00059     } else if (nlin <= 0) {
00060         MESSAGE('E'," Number of lines must be greater than zero.");
00061         goto the_end;
00062     } else if (npix <= 0) {
00063         MESSAGE('E'," Number of pixels/line must be greater than zero.");
00064         goto the_end;
00065     } else if (nbit != sizeof(PIXEL)*SBPB   &&  (sizeof(PIXEL)*SBPB < nbit  ||  nbit > sizeof(long)*SBPB)) {
00066         sprintf(msg," Can't unpack from %d bits/pixel.",nbit);
00067         MESSAGE('E',msg);
00068         goto the_end;
00069     } else if (0 > ibgn  ||  iend >= nbnd) {
00070         MESSAGE('E'," Image to be sampled must be located completely inside input image in the band dimension.");
00071         goto the_end;
00072     } else if (0 > jbgn  ||  jend >= nlin) {
00073         MESSAGE('E'," Image to be sampled must be located completely inside input image in the line dimension.");
00074         goto the_end;
00075     } else if (0 > kbgn  ||  kend >= npix) {
00076         MESSAGE('E'," Image to be sampled must be located completely inside input image in the pixel dimension.");
00077         goto the_end;
00078     } else if (iinc <= 0) {
00079         MESSAGE('E'," Band subsample increment must be greater than zero.");
00080         goto the_end;
00081     } else if (jinc <= 0) {
00082         MESSAGE('E'," Line subsample increment must be greater than zero.");
00083         goto the_end;
00084     } else if (kinc <= 0) {
00085         MESSAGE('E'," Pixel subsample increment must be greater than zero.");
00086         goto the_end;
00087     } else if (ibgn > iend) {
00088         MESSAGE('E'," Number of bands to be spanned must be greater than zero.");
00089         goto the_end;
00090     } else if (jbgn > jend) {
00091         MESSAGE('E'," Number of lines to be spanned must be greater than zero.");
00092         goto the_end;
00093     } else if (kbgn > kend) {
00094         MESSAGE('E'," Number of pixels/line to be spanned must be greater than zero.");
00095         goto the_end;
00096     }
00097 
00098     /* Make sure the size matches the number of bytes in the file */
00099     calcsize = (long)offset + (long)lrnd((double)nbnd*nlin*npix*((double)nbit / 8.0));
00100     if (filesize < calcsize || calcsize <= offset) {
00101         sprintf(msg,"File %s is not in the specified format!",name);
00102         MESSAGE('E',msg);
00103         goto the_end;
00104     }
00105 
00106     /* open image file */
00107 
00108     if (!(fp=fopen(name,"r"))) {
00109         sprintf(msg," Can't open file %s.",name);
00110         MESSAGE('E',msg);
00111         goto the_end;
00112     }
00113 
00114     /* Set file pointer to first byte after header */
00115     fseek(fp, (long) (offset), SEEK_SET);
00116 
00117 
00118     /* create image of appropriate size */
00119     bands  = max(1,(iend-ibgn+1)/iinc);
00120     lines  = max(1,(jend-jbgn+1)/jinc);
00121     pixels = max(1,(kend-kbgn+1)/kinc);
00122     if (!CHECKIMG(*out)) GETMEM(bands,lines,pixels,out);
00123     if (!*out) goto the_end;
00124     (*out)->nbit = nbit;
00125 
00126     /* read the image data from file */
00127     switch(option) {
00128         case BIL :
00129 
00130         case BSQ :
00131                 nbytes = (int)ceil((((double)npix*(double)nbit)/(double)SBPB));
00132                 if ((line=(PIXEL *)malloc((long)npix * sizeof(PIXEL))) == NULL) {
00133                         MESSAGE('E'," Memory request failed.");
00134                         goto the_end;
00135                 }       
00136                 break;
00137 
00138         case BIP :
00139                 nbytes = (int)ceil((((double)nbnd*(double)nbit)/(double)SBPB));
00140                 if ((line=(PIXEL *)malloc((long)nbnd * sizeof(PIXEL))) == NULL) {
00141                         MESSAGE('E'," Memory request failed.");
00142                         goto the_end;
00143                 }       
00144                 break;
00145     }
00146 
00147     if (option == BIL) {
00148         if (fseek(fp,(long)(nbytes * (long)nbnd * (long)jbgn),SEEK_CUR) != 0) {
00149                 sprintf(msg," Can't skip to line %d in file %s.",jbgn,name);
00150                 MESSAGE('E',msg);
00151                 goto the_end;
00152         }
00153             for (j = 0; j < lines; j++) {
00154                 if (fseek(fp,(long)(nbytes * (long)nbnd * (long)(jinc - 1)),SEEK_CUR) != 0) {
00155                         sprintf(msg," Can't line subsample by %d in file %s.",jinc,name);
00156                         MESSAGE('E',msg);
00157                         goto the_end;
00158                 }
00159                 if (fseek(fp,(long)(nbytes * (long)ibgn),SEEK_CUR) != 0) {
00160                         sprintf(msg," Can't skip to band %d in file %s.",ibgn,name);
00161                         MESSAGE('E',msg);
00162                         goto the_end;
00163                 }
00164                 for (i = 0; i < bands; i++) {
00165                     if (fseek(fp,(long)(nbytes * (long)(iinc - 1)),SEEK_CUR) != 0) {
00166                         sprintf(msg," Can't band subsample by %d in file %s.",iinc,name);
00167                                 MESSAGE('E',msg);
00168                         goto the_end;
00169                     }
00170                     readbytes = fread(line,1,(long)nbytes,fp);
00171                     if (readbytes < (long)nbytes) {
00172                         sprintf(msg," Can't read image data from file %s.",name);
00173                         MESSAGE('E',msg);
00174                         goto the_end;
00175                     }
00176                     if (nbit < sizeof(PIXEL)*SBPB) {
00177                         UNPACK(nbit,(long)npix,line);
00178                     }
00179                     for (k = 0, ptr = line + kbgn; k < pixels; k++, ptr += kinc) {
00180                         (*out)->data[i][j][k] = (PIXEL)*ptr;
00181                     }
00182                 }
00183                 if (fseek(fp,(long)(nbytes * (long)(nbnd - iend - 1)),SEEK_CUR) != 0) {
00184                         sprintf(msg," Can't skip to end of line in file %s.",name);
00185                         MESSAGE('E',msg);
00186                         goto the_end;
00187                 }
00188             }           
00189     } else if (option == BSQ) {
00190         if (fseek(fp,(long)(nbytes * (long)nlin * (long)ibgn),SEEK_CUR) != 0) {
00191                 sprintf(msg," Can't skip to band %d in file %s.",ibgn,name);
00192                 MESSAGE('E',msg);
00193                 goto the_end;
00194         }
00195                 for (i = 0; i < bands; i++) {
00196                 if (fseek(fp,(long)(nbytes * (long)nlin * (long)(iinc - 1)),SEEK_CUR) != 0) {
00197                         sprintf(msg," Can't band subsample by %d in file %s.",iinc,name);
00198                         MESSAGE('E',msg);
00199                         goto the_end;
00200                 }
00201                 if (fseek(fp,(long)(nbytes * (long)jbgn),SEEK_CUR) != 0) {
00202                         sprintf(msg," Can't skip to line %d in file %s.",jbgn,name);
00203                         MESSAGE('E',msg);
00204                         goto the_end;
00205                 }
00206                 for (j = 0; j < lines; j++) {
00207                         if (fseek(fp,(long)(nbytes * (long)(jinc - 1)),SEEK_CUR) != 0) {
00208                                 sprintf(msg," Can't line subsample by %d in file %s.",jinc,name);
00209                                 MESSAGE('E',msg);
00210                                 goto the_end;
00211                         }
00212                         if (fread(line,nbytes,1,fp) != 1) {
00213                                 sprintf(msg," Can't read image data from file %s.",name);
00214                                 MESSAGE('E',msg);
00215                                 goto the_end;
00216                         }
00217                 if (nbit < sizeof(PIXEL)*SBPB) {
00218                         UNPACK(nbit,(long)npix,line);
00219                         }
00220                         for (k = 0, ptr = line + kbgn; k < pixels; k++, ptr += kinc) {
00221                                 (*out)->data[i][j][k] = *ptr;
00222                         }
00223                 }
00224                 if (fseek(fp,(long)(nbytes * (long)(nlin - jend - 1)),SEEK_CUR) != 0) {
00225                         sprintf(msg," Can't skip to end of band in file %s.",name);
00226                         MESSAGE('E',msg);
00227                         goto the_end;
00228                 }
00229         }               
00230     } else if (option == BIP) {
00231         if (fseek(fp,(long)(nbytes * (long)npix * (long)jbgn),SEEK_CUR) != 0) {
00232                 sprintf(msg," Can't skip to line %d in file %s.",jbgn,name);
00233                 MESSAGE('E',msg);
00234                 goto the_end;
00235         }
00236         for (j = 0; j < lines; j++) {
00237                 if (fseek(fp,(long)(nbytes * (long)npix * (long)(jinc - 1)),SEEK_CUR) != 0) {
00238                         sprintf(msg," Can't line subsample by %d in file %s.",jinc,name);
00239                         MESSAGE('E',msg);
00240                         goto the_end;
00241                 }
00242                 if (fseek(fp,(long)(nbytes * (long)kbgn),SEEK_CUR) != 0) {
00243                         sprintf(msg," Can't skip to pixel %d in file %s.",kbgn,name);
00244                         MESSAGE('E',msg);
00245                         goto the_end;
00246                 }
00247                 for (k = 0; k < pixels; k++) {
00248                         if (fseek(fp,(long)(nbytes * (long)(kinc - 1)),SEEK_CUR) != 0) {
00249                                 sprintf(msg," Can't pixel subsample by %d in file %s.",kinc,name);
00250                                 MESSAGE('E',msg);
00251                                 goto the_end;
00252                         }
00253                         if (fread(line,nbytes,1,fp) != 1) {
00254                                 sprintf(msg," Can't read image data from file %s.",name);
00255                                 MESSAGE('E',msg);
00256                                 goto the_end;
00257                         }
00258                 if (nbit < sizeof(PIXEL)*SBPB) {
00259                         UNPACK(nbit,(long)nbnd,line);
00260                         }
00261                         for (i = 0, ptr = line + ibgn; i < bands; i++, ptr += iinc) {
00262                                 (*out)->data[i][j][k] = *ptr;
00263                         }
00264                 }
00265                 if (fseek(fp,(long)(nbytes * (long)(npix - kend - 1)),SEEK_CUR) != 0) {
00266                         sprintf(msg," Can't skip to end of line in file %s.",name);
00267                         MESSAGE('E',msg);
00268                         goto the_end;
00269                 }
00270         }               
00271     }
00272 
00273     if (NAMES) {
00274         RANGE(*out);
00275         MESSAGE('I',"");
00276         MESSAGE('I',"IMPORT");
00277         MESSAGE('I',"");
00278         sprintf(msg," Input file:                                 %s",name);
00279         MESSAGE('I',msg); 
00280         if (option == BIL) {
00281             MESSAGE('I'," Input format:                               Band-interleaved-by-line");
00282         } else if (option == BSQ) {
00283             MESSAGE('I'," Input format:                               Band-sequential");
00284         } else if (option == BIP) {
00285             MESSAGE('I'," Input format:                               Band-interleaved-by-pixel");
00286         }
00287         sprintf(msg," Header size:                                %ld",offset);
00288         MESSAGE('I',msg);
00289         sprintf(msg," Number of bands, lines, pixels:             %d, %d, %d",nbnd,nlin,npix);
00290         MESSAGE('I',msg);
00291         sprintf(msg," Number of bits/pixel/band:                  %d",nbit);
00292         MESSAGE('I',msg);
00293         sprintf(msg," Subsampled from band, line, pixel:          %d, %d, %d",ibgn+1,jbgn+1,kbgn+1);
00294         MESSAGE('I',msg);
00295         sprintf(msg," Subsampled to band, line, pixel:            %d, %d, %d",iend+1,jend+1,kend+1);
00296         MESSAGE('I',msg);
00297         sprintf(msg," Subsample increment: bands, lines, pixels:  %d, %d, %d",iinc,jinc,kinc);
00298         MESSAGE('I',msg);
00299         sprintf(msg," Graylevel minimum, maximum:                 %10.4e, %10.4e",(*out)->gmin,(*out)->gmax);
00300         MESSAGE('I',msg);
00301         MESSAGE('I'," ...............");
00302     }
00303 
00304     /* close image file */
00305     the_end:
00306     if (fp) {
00307         if (fclose(fp)) {
00308             sprintf(msg," Can't close file %s.",name);
00309             MESSAGE('W',msg);
00310         }
00311     }
00312     if (line)  free(line);
00313     if (TIMES) TIMING(T_EXIT);
00314 }

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