00001 #include <stdio.h>
00002 #include "sadie.h"
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 void
00024 IMPORT (char *name,
00025 short option,
00026 short offset,
00027 short nbnd,
00028 short nlin,
00029 short npix,
00030 short nbit,
00031 short ibgn,
00032 short jbgn,
00033 short kbgn,
00034 short iinc,
00035 short jinc,
00036 short kinc,
00037 short iend,
00038 short jend,
00039 short kend,
00040 long filesize,
00041 IMAGE ** out
00042
00043 )
00044 {
00045 register int i, j, k;
00046 char msg[SLEN];
00047 short bands, lines, pixels, error;
00048 short nbytes;
00049 PIXEL *line = NULL, *ptr = NULL;
00050 FILE *fp = NULL;
00051 long calcsize;
00052 long readbytes;
00053
00054 if (TIMES)
00055 TIMING (T_IMPORT);
00056
00057
00058 if (nbnd <= 0)
00059 {
00060 MESSAGE ('E', " Number of bands must be greater than zero.");
00061 goto the_end;
00062 }
00063 else if (nlin <= 0)
00064 {
00065 MESSAGE ('E', " Number of lines must be greater than zero.");
00066 goto the_end;
00067 }
00068 else if (npix <= 0)
00069 {
00070 MESSAGE ('E', " Number of pixels/line must be greater than zero.");
00071 goto the_end;
00072 }
00073 else if (nbit != sizeof (PIXEL) * SBPB
00074 && (sizeof (PIXEL) * SBPB < nbit || nbit > sizeof (long) * SBPB))
00075 {
00076 sprintf (msg, " Can't unpack from %d bits/pixel.", nbit);
00077 MESSAGE ('E', msg);
00078 goto the_end;
00079 }
00080 else if (0 > ibgn || iend >= nbnd)
00081 {
00082 MESSAGE ('E',
00083 " Image to be sampled must be located completely inside input image in the band dimension.");
00084 goto the_end;
00085 }
00086 else if (0 > jbgn || jend >= nlin)
00087 {
00088 MESSAGE ('E',
00089 " Image to be sampled must be located completely inside input image in the line dimension.");
00090 goto the_end;
00091 }
00092 else if (0 > kbgn || kend >= npix)
00093 {
00094 MESSAGE ('E',
00095 " Image to be sampled must be located completely inside input image in the pixel dimension.");
00096 goto the_end;
00097 }
00098 else if (iinc <= 0)
00099 {
00100 MESSAGE ('E', " Band subsample increment must be greater than zero.");
00101 goto the_end;
00102 }
00103 else if (jinc <= 0)
00104 {
00105 MESSAGE ('E', " Line subsample increment must be greater than zero.");
00106 goto the_end;
00107 }
00108 else if (kinc <= 0)
00109 {
00110 MESSAGE ('E', " Pixel subsample increment must be greater than zero.");
00111 goto the_end;
00112 }
00113 else if (ibgn > iend)
00114 {
00115 MESSAGE ('E',
00116 " Number of bands to be spanned must be greater than zero.");
00117 goto the_end;
00118 }
00119 else if (jbgn > jend)
00120 {
00121 MESSAGE ('E',
00122 " Number of lines to be spanned must be greater than zero.");
00123 goto the_end;
00124 }
00125 else if (kbgn > kend)
00126 {
00127 MESSAGE ('E',
00128 " Number of pixels/line to be spanned must be greater than zero.");
00129 goto the_end;
00130 }
00131
00132
00133 calcsize =
00134 (long) offset +
00135 (long) lrnd ((double) nbnd * nlin * npix * ((double) nbit / 8.0));
00136 if (filesize < calcsize || calcsize <= offset)
00137 {
00138 sprintf (msg, "File %s is not in the specified format!", name);
00139 MESSAGE ('E', msg);
00140 goto the_end;
00141 }
00142
00143
00144
00145 if (!(fp = fopen (name, "r")))
00146 {
00147 sprintf (msg, " Can't open file %s.", name);
00148 MESSAGE ('E', msg);
00149 goto the_end;
00150 }
00151
00152
00153 fseek (fp, (long) (offset), SEEK_SET);
00154
00155
00156 bands = max (1, (iend - ibgn + 1) / iinc);
00157 lines = max (1, (jend - jbgn + 1) / jinc);
00158 pixels = max (1, (kend - kbgn + 1) / kinc);
00159 if (!CHECKIMG (*out))
00160 GETMEM (bands, lines, pixels, out);
00161 if (!*out)
00162 goto the_end;
00163 (*out)->nbit = nbit;
00164
00165
00166 switch (option)
00167 {
00168 case BIL:
00169
00170 case BSQ:
00171 nbytes = (int) ceil ((((double) npix * (double) nbit) / (double) SBPB));
00172 if ((line = (PIXEL *) malloc ((long) npix * sizeof (PIXEL))) == NULL)
00173 {
00174 MESSAGE ('E', " Memory request failed.");
00175 goto the_end;
00176 }
00177 break;
00178
00179 case BIP:
00180 nbytes = (int) ceil ((((double) nbnd * (double) nbit) / (double) SBPB));
00181 if ((line = (PIXEL *) malloc ((long) nbnd * sizeof (PIXEL))) == NULL)
00182 {
00183 MESSAGE ('E', " Memory request failed.");
00184 goto the_end;
00185 }
00186 break;
00187 }
00188
00189 if (option == BIL)
00190 {
00191 if (fseek (fp, (long) (nbytes * (long) nbnd * (long) jbgn), SEEK_CUR) !=
00192 0)
00193 {
00194 sprintf (msg, " Can't skip to line %d in file %s.", jbgn, name);
00195 MESSAGE ('E', msg);
00196 goto the_end;
00197 }
00198 for (j = 0; j < lines; j++)
00199 {
00200 if (fseek
00201 (fp, (long) (nbytes * (long) nbnd * (long) (jinc - 1)),
00202 SEEK_CUR) != 0)
00203 {
00204 sprintf (msg, " Can't line subsample by %d in file %s.", jinc,
00205 name);
00206 MESSAGE ('E', msg);
00207 goto the_end;
00208 }
00209 if (fseek (fp, (long) (nbytes * (long) ibgn), SEEK_CUR) != 0)
00210 {
00211 sprintf (msg, " Can't skip to band %d in file %s.", ibgn, name);
00212 MESSAGE ('E', msg);
00213 goto the_end;
00214 }
00215 for (i = 0; i < bands; i++)
00216 {
00217 if (fseek (fp, (long) (nbytes * (long) (iinc - 1)), SEEK_CUR) !=
00218 0)
00219 {
00220 sprintf (msg, " Can't band subsample by %d in file %s.",
00221 iinc, name);
00222 MESSAGE ('E', msg);
00223 goto the_end;
00224 }
00225 readbytes = fread (line, 1, (long) nbytes, fp);
00226 if (readbytes < (long) nbytes)
00227 {
00228 sprintf (msg, " Can't read image data from file %s.", name);
00229 MESSAGE ('E', msg);
00230 goto the_end;
00231 }
00232 if (nbit < sizeof (PIXEL) * SBPB)
00233 {
00234 UNPACK (nbit, (long) npix, line);
00235 }
00236 for (k = 0, ptr = line + kbgn; k < pixels; k++, ptr += kinc)
00237 {
00238 (*out)->data[i][j][k] = (PIXEL) * ptr;
00239 }
00240 }
00241 if (fseek (fp, (long) (nbytes * (long) (nbnd - iend - 1)), SEEK_CUR)
00242 != 0)
00243 {
00244 sprintf (msg, " Can't skip to end of line in file %s.", name);
00245 MESSAGE ('E', msg);
00246 goto the_end;
00247 }
00248 }
00249 }
00250 else if (option == BSQ)
00251 {
00252 if (fseek (fp, (long) (nbytes * (long) nlin * (long) ibgn), SEEK_CUR) !=
00253 0)
00254 {
00255 sprintf (msg, " Can't skip to band %d in file %s.", ibgn, name);
00256 MESSAGE ('E', msg);
00257 goto the_end;
00258 }
00259 for (i = 0; i < bands; i++)
00260 {
00261 if (fseek
00262 (fp, (long) (nbytes * (long) nlin * (long) (iinc - 1)),
00263 SEEK_CUR) != 0)
00264 {
00265 sprintf (msg, " Can't band subsample by %d in file %s.", iinc,
00266 name);
00267 MESSAGE ('E', msg);
00268 goto the_end;
00269 }
00270 if (fseek (fp, (long) (nbytes * (long) jbgn), SEEK_CUR) != 0)
00271 {
00272 sprintf (msg, " Can't skip to line %d in file %s.", jbgn, name);
00273 MESSAGE ('E', msg);
00274 goto the_end;
00275 }
00276 for (j = 0; j < lines; j++)
00277 {
00278 if (fseek (fp, (long) (nbytes * (long) (jinc - 1)), SEEK_CUR) !=
00279 0)
00280 {
00281 sprintf (msg, " Can't line subsample by %d in file %s.",
00282 jinc, name);
00283 MESSAGE ('E', msg);
00284 goto the_end;
00285 }
00286 if (fread (line, nbytes, 1, fp) != 1)
00287 {
00288 sprintf (msg, " Can't read image data from file %s.", name);
00289 MESSAGE ('E', msg);
00290 goto the_end;
00291 }
00292 if (nbit < sizeof (PIXEL) * SBPB)
00293 {
00294 UNPACK (nbit, (long) npix, line);
00295 }
00296 for (k = 0, ptr = line + kbgn; k < pixels; k++, ptr += kinc)
00297 {
00298 (*out)->data[i][j][k] = *ptr;
00299 }
00300 }
00301 if (fseek (fp, (long) (nbytes * (long) (nlin - jend - 1)), SEEK_CUR)
00302 != 0)
00303 {
00304 sprintf (msg, " Can't skip to end of band in file %s.", name);
00305 MESSAGE ('E', msg);
00306 goto the_end;
00307 }
00308 }
00309 }
00310 else if (option == BIP)
00311 {
00312 if (fseek (fp, (long) (nbytes * (long) npix * (long) jbgn), SEEK_CUR) !=
00313 0)
00314 {
00315 sprintf (msg, " Can't skip to line %d in file %s.", jbgn, name);
00316 MESSAGE ('E', msg);
00317 goto the_end;
00318 }
00319 for (j = 0; j < lines; j++)
00320 {
00321 if (fseek
00322 (fp, (long) (nbytes * (long) npix * (long) (jinc - 1)),
00323 SEEK_CUR) != 0)
00324 {
00325 sprintf (msg, " Can't line subsample by %d in file %s.", jinc,
00326 name);
00327 MESSAGE ('E', msg);
00328 goto the_end;
00329 }
00330 if (fseek (fp, (long) (nbytes * (long) kbgn), SEEK_CUR) != 0)
00331 {
00332 sprintf (msg, " Can't skip to pixel %d in file %s.", kbgn,
00333 name);
00334 MESSAGE ('E', msg);
00335 goto the_end;
00336 }
00337 for (k = 0; k < pixels; k++)
00338 {
00339 if (fseek (fp, (long) (nbytes * (long) (kinc - 1)), SEEK_CUR) !=
00340 0)
00341 {
00342 sprintf (msg, " Can't pixel subsample by %d in file %s.",
00343 kinc, name);
00344 MESSAGE ('E', msg);
00345 goto the_end;
00346 }
00347 if (fread (line, nbytes, 1, fp) != 1)
00348 {
00349 sprintf (msg, " Can't read image data from file %s.", name);
00350 MESSAGE ('E', msg);
00351 goto the_end;
00352 }
00353 if (nbit < sizeof (PIXEL) * SBPB)
00354 {
00355 UNPACK (nbit, (long) nbnd, line);
00356 }
00357 for (i = 0, ptr = line + ibgn; i < bands; i++, ptr += iinc)
00358 {
00359 (*out)->data[i][j][k] = *ptr;
00360 }
00361 }
00362 if (fseek (fp, (long) (nbytes * (long) (npix - kend - 1)), SEEK_CUR)
00363 != 0)
00364 {
00365 sprintf (msg, " Can't skip to end of line in file %s.", name);
00366 MESSAGE ('E', msg);
00367 goto the_end;
00368 }
00369 }
00370 }
00371
00372 if (NAMES)
00373 {
00374 RANGE (*out);
00375 MESSAGE ('I', "");
00376 MESSAGE ('I', "IMPORT");
00377 MESSAGE ('I', "");
00378 sprintf (msg, " Input file: %s", name);
00379 MESSAGE ('I', msg);
00380 if (option == BIL)
00381 {
00382 MESSAGE ('I',
00383 " Input format: Band-interleaved-by-line");
00384 }
00385 else if (option == BSQ)
00386 {
00387 MESSAGE ('I',
00388 " Input format: Band-sequential");
00389 }
00390 else if (option == BIP)
00391 {
00392 MESSAGE ('I',
00393 " Input format: Band-interleaved-by-pixel");
00394 }
00395 sprintf (msg, " Header size: %ld",
00396 offset);
00397 MESSAGE ('I', msg);
00398 sprintf (msg, " Number of bands, lines, pixels: %d, %d, %d",
00399 nbnd, nlin, npix);
00400 MESSAGE ('I', msg);
00401 sprintf (msg, " Number of bits/pixel/band: %d", nbit);
00402 MESSAGE ('I', msg);
00403 sprintf (msg, " Subsampled from band, line, pixel: %d, %d, %d",
00404 ibgn + 1, jbgn + 1, kbgn + 1);
00405 MESSAGE ('I', msg);
00406 sprintf (msg, " Subsampled to band, line, pixel: %d, %d, %d",
00407 iend + 1, jend + 1, kend + 1);
00408 MESSAGE ('I', msg);
00409 sprintf (msg, " Subsample increment: bands, lines, pixels: %d, %d, %d",
00410 iinc, jinc, kinc);
00411 MESSAGE ('I', msg);
00412 sprintf (msg,
00413 " Graylevel minimum, maximum: %10.4e, %10.4e",
00414 (*out)->gmin, (*out)->gmax);
00415 MESSAGE ('I', msg);
00416 MESSAGE ('I', " ...............");
00417 }
00418
00419
00420 the_end:
00421 if (fp)
00422 {
00423 if (fclose (fp))
00424 {
00425 sprintf (msg, " Can't close file %s.", name);
00426 MESSAGE ('W', msg);
00427 }
00428 }
00429 if (line)
00430 free (line);
00431 if (TIMES)
00432 TIMING (T_EXIT);
00433 }