Main Page   Data Structures   File List   Data Fields   Globals  

disk2img.c

Go to the documentation of this file.
00001 #include        "sadie.h"
00002 
00003 /*-Copyright Information------------------------------------------------------*/
00004 /* Copyright (c) 1988 by the University of Arizona Digital Image Analysis Lab */
00005 /*-General Information--------------------------------------------------------*/
00006 /*                                                                            */
00007 /*   This function reads a SADIE image from disk into main memory.            */
00008 /*                                                                            */
00009 /*----------------------------------------------------------------------------*/
00010 /*-Interface Information------------------------------------------------------*/
00011 void
00012 DISK2IMG (char *name,           /*  I   String, containing the name of the disk file.         */
00013           long size,            /*  I   The size of the image file.                                    */
00014           IMAGE ** out          /*  O   Address of a pointer to the image.                    */
00015 /*----------------------------------------------------------------------------*/
00016   )
00017 {
00018   static char array[2] = { 0x00, 0x01 };
00019   char msg[SLEN];
00020   short nbnd, nlin, npix, nbit;
00021   long nbytes;
00022   FILE *fp = NULL;
00023   long calcsize;
00024   long readbytes;
00025 
00026   if (TIMES)
00027     TIMING (T_DISK2IMG);
00028 
00029   /* open image file */
00030   if (!(fp = fopen (name, "r")))
00031     {
00032       sprintf (msg, " Can't open file %s.", name);
00033       MESSAGE ('E', msg);
00034       goto the_end;
00035     }
00036 
00037   /* read image size parameters from file */
00038   if (fread (&nbnd, sizeof (nbnd), 1, fp) != 1
00039       || fread (&nlin, sizeof (nlin), 1, fp) != 1
00040       || fread (&npix, sizeof (npix), 1, fp) != 1
00041       || fread (&nbit, sizeof (nbit), 1, fp) != 1)
00042     {
00043       sprintf (msg, " Can't read image header from file %s.", name);
00044       MESSAGE ('E', msg);
00045       goto the_end;
00046     }
00047 
00048   /* swap bytes, if necessary */
00049   if (*(short *) array != 1)
00050     {
00051 /*        //printf("DISK2IMG: Swapping bytes...\n"); */
00052 
00053       nbnd = SWAP ((unsigned char *) &nbnd);
00054       nlin = SWAP ((unsigned char *) &nlin);
00055       npix = SWAP ((unsigned char *) &npix);
00056       nbit = SWAP ((unsigned char *) &nbit);
00057     }
00058 
00059   /* Make sure the size matches the number of bytes in the file */
00060   if (size > 0)
00061     {
00062       calcsize =
00063         512L +
00064         lrnd ((double) nbnd * (double) nlin * (double) npix *
00065               ((double) nbit / 8.0));
00066       if (size < calcsize || calcsize <= 512L)
00067         {
00068           printf ("size = %d, calcsize = %d\n", size, calcsize);
00069           printf ("nbnd = %d, nlin = %d, npix = %d, nbit = %d\n", nbnd, nlin,
00070                   npix, nbit);
00071           sprintf (msg, "File %s is not in SADIE format!", name);
00072           MESSAGE ('E', msg);
00073           goto the_end;
00074         }
00075     }
00076 
00077   /* create image of appropriate size */
00078   if (!CHECKIMG (*out))
00079     GETMEM (nbnd, nlin, npix, out);
00080   if (!CHECKIMG (*out))
00081     {
00082       sprintf (msg, "Can't allocate memory for image.");
00083       MESSAGE ('E', msg);
00084       goto the_end;
00085     }
00086 
00087   /* read the rest of the header data from file */
00088   (*out)->nbit = nbit;
00089   nbytes =
00090     (long) (sizeof ((*out)->gmin) + sizeof ((*out)->gmax) +
00091             sizeof ((*out)->pstr) + sizeof ((*out)->text));
00092   if (fread (&((*out)->gmin), nbytes, 1, fp) != 1)
00093     {
00094       sprintf (msg, " Can't read image header from file %s.", name);
00095       MESSAGE ('E', msg);
00096       goto the_end;
00097     }
00098 
00099   /* check number of bits per pixel */
00100   if ((*out)->nbit != sizeof (PIXEL) * SBPB
00101       && (sizeof (PIXEL) * SBPB < (*out)->nbit
00102           || (*out)->nbit > sizeof (long) * SBPB))
00103     {
00104       sprintf (msg, " Can't unpack from %d bits/pixel.", (*out)->nbit);
00105       MESSAGE ('E', msg);
00106       goto the_end;
00107     }
00108 
00109   /* read the image data from file */
00110   nbytes =
00111     (long)
00112     ceil (((double) nbnd * (double) nlin * (double) npix * (*out)->nbit) /
00113           (double) SBPB);
00114   readbytes = fread ((*out)->data[0][0], 1, nbytes, fp);
00115   if (readbytes < nbytes)
00116     {
00117       sprintf (msg, " Can't read image data from file %s.", name);
00118       MESSAGE ('E', msg);
00119       sprintf (msg, " Read %ld bytes instead of %ld bytes.", readbytes,
00120                (long) nbytes);
00121       MESSAGE ('E', msg);
00122     }
00123 
00124   if ((*out)->nbit < sizeof (PIXEL) * SBPB)
00125     {
00126       UNPACK ((*out)->nbit, (long) nbnd * (long) nlin * (long) npix,
00127               (*out)->data[0][0]);
00128     }
00129 
00130   /* Set image name to match file name */
00131   sprintf ((*out)->text, "%s", name);
00132 
00133   if (NAMES)
00134     {
00135       MESSAGE ('I', "");
00136       MESSAGE ('I', "DISK2IMG");
00137       MESSAGE ('I', "");
00138       sprintf (msg, " Input file:                                 %s", name);
00139       MESSAGE ('I', msg);
00140       MESSAGE ('I', " Input format:                               SADIE");
00141       sprintf (msg, " Header size:                                %d",
00142                (int) (sizeof ((*out)->nbnd) + sizeof ((*out)->nlin) +
00143                       sizeof ((*out)->npix) + sizeof ((*out)->nbit) +
00144                       sizeof ((*out)->gmin) + sizeof ((*out)->gmax) +
00145                       sizeof ((*out)->pstr) + sizeof ((*out)->text)));
00146       MESSAGE ('I', msg);
00147       sprintf (msg, " Number of bands, lines, pixels:             %d, %d, %d",
00148                (*out)->nbnd, (*out)->nlin, (*out)->npix);
00149       MESSAGE ('I', msg);
00150       sprintf (msg, " Number of bits/pixel/band:                  %d",
00151                (*out)->nbit);
00152       MESSAGE ('I', msg);
00153       sprintf (msg,
00154                " Graylevel minimum, maximum:                 %10.4e, %10.4e",
00155                (*out)->gmin, (*out)->gmax);
00156       MESSAGE ('I', msg);
00157       MESSAGE ('I', " ...............");
00158     }
00159 
00160   /* close image file */
00161 
00162 the_end:
00163   if (fp)
00164     {
00165       if (fclose (fp))
00166         {
00167           sprintf (msg, " Can't close file %s.", name);
00168           MESSAGE ('W', msg);
00169         }
00170     }
00171 
00172   if (TIMES)
00173     TIMING (T_EXIT);
00174 
00175 }

Generated on Sun May 18 15:36:09 2003 for tclSadie by doxygen1.3