00001 #include "sadie.h"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 void
00012 DISK2IMG (char *name,
00013 long size,
00014 IMAGE ** out
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
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
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
00049 if (*(short *) array != 1)
00050 {
00051
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
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
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
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
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
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
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
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 }